The best way to control Claude Code, Codex and more from any computer or phone

main
ts 51 lines 1.98 KB
Raw
1 import * as bedrock from "@distilled.cloud/aws/bedrock-agent-runtime";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import { AWSEnvironment } from "../Environment.ts";
6 import { bedrockModelArns } from "./ModelArns.ts";
7 import { Rerank, type RerankRequest } from "./Rerank.ts";
8
9 // Bespoke (not the shared model-scoped scaffold): Rerank authorizes the
10 // `bedrock:Rerank` action only against `*`, alongside model-scoped
11 // `bedrock:InvokeModel`, and the model is referenced inside
12 // `rerankingConfiguration` rather than a top-level `modelId`.
13 export const RerankHttp = Layer.effect(
14 Rerank,
15 Effect.gen(function* () {
16 const bind = yield* AWSBindings.Bind;
17 const rerank = yield* AWSBindings.withCredentials(bedrock.rerank);
18
19 return Effect.fn(function* (model: string, ...additionalModels: string[]) {
20 if (!globalThis.__ALCHEMY_RUNTIME__) {
21 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
22 accountId: string;
23 region: string;
24 }>;
25 // Sort so the binding identity (SID + ARN list) is deterministic
26 // regardless of argument order.
27 const sorted = [...new Set([model, ...additionalModels])].sort();
28 yield* bind`AWS.Bedrock.Rerank(${sorted.join(",")})`({
29 policyStatements: [
30 {
31 // bedrock:Rerank does not support resource-level scoping.
32 Effect: "Allow",
33 Action: ["bedrock:Rerank"],
34 Resource: ["*"],
35 },
36 {
37 Effect: "Allow",
38 Action: ["bedrock:InvokeModel"],
39 Resource: [
40 ...new Set(sorted.flatMap((id) => bedrockModelArns(region, accountId, id))),
41 ],
42 },
43 ],
44 });
45 }
46 return Effect.fn(`AWS.Bedrock.Rerank(${model})`)(function* (request: RerankRequest) {
47 return yield* rerank(request);
48 });
49 });
50 }),
51 );