@clawdbuilder/jig-bedrock 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jig contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,173 @@
1
- # Temporary Holding Version
1
+ # jig-bedrock: Claude on AWS Bedrock for Pi
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A Pi provider for Claude on AWS Bedrock. It discovers the application inference profiles your account can use, sends
4
+ Claude through Bedrock's Anthropic Messages route where possible, and falls back to Converse per model.
5
+
6
+ ## The gap it fills
7
+
8
+ Pi's built-in Bedrock provider uses Converse only, has a fixed model list, and has no profile discovery, SSO repair or
9
+ budget footer. This package adds the Messages route with SigV4, discovery by an AWS tag, repair through `aws sso login`,
10
+ and an optional spend line. It works in plain Pi; when [Jig](https://github.com/ClawdBuilder/jig) is installed it also
11
+ uses Jig's records and footer, but Jig is not required.
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ pi install npm:@clawdbuilder/jig-bedrock
17
+ ```
18
+
19
+ Requirements: Node 22.19 or later, Pi 1.5 or later, and the AWS CLI v2 for discovery. You need an AWS profile the CLI can
20
+ use; **SSO is one option, not a requirement**. Linux, WSL2 and macOS are supported; Windows is not, for the reason below.
21
+
22
+ Run `/bedrock-doctor` after installing; it checks Node, the AWS CLI, `bash`, `jq` and the optional usage program without
23
+ calling AWS.
24
+
25
+ ## Minimum settings
26
+
27
+ With Jig, add a `bedrock` section to `jig.json` (or use the environment):
28
+
29
+ ```json
30
+ { "bedrock": { "team": "my-team" } }
31
+ ```
32
+
33
+ A plain Pi install has no `jig.json`: put the same keys in `~/.pi/agent/bedrock.json` (or in a `bedrock` object in that
34
+ file). With Jig, that file is read first and Jig's `bedrock` section is merged over it. A project's `jig.json` may set the
35
+ catalog keys, but `ssoLoginCommand`, `usageDir` and `browser` are read only from the user's file or the environment.
36
+
37
+ `team` is the value of the AWS tag on your inference profiles. The package discovers active profiles with that value.
38
+
39
+ | Setting | Default | Meaning |
40
+ | --------------------------------- | ----------------------------------------------- | -------------------------------------------------------------------------------------------------- |
41
+ | `team` | required | The exact AWS tag value on your application inference profiles |
42
+ | `tagKey` | `Team` | The AWS tag that names your team on the inference profiles |
43
+ | `tagValue` | the value of `team` | The tag value to discover; `team` is kept for compatibility |
44
+ | `activeTagKey` / `activeTagValue` | `Active` / `true` | The tag that marks a profile as usable |
45
+ | `profile` | `my-bedrock-profile` | The AWS profile for discovery, requests and sign-in |
46
+ | `regions` | `["us-east-1"]` | Where to look for profiles |
47
+ | `usageDir` | `~/.local/share/usage-tool` | Optional directory holding an executable `usage` that prints the usage JSON; omit if you have none |
48
+ | `monthlyBudgetUsd` | none | A figure for the footer, not an AWS limit |
49
+ | `ssoLoginCommand` | `["aws","sso","login","--profile","{profile}"]` | The owning tool starts the sign-in; this package never handles a password or token |
50
+ | `signInAtStart` | `true` | Refresh an expired SSO session when Pi starts |
51
+ | `browser` | WSL's Windows browser | Program that opens the sign-in address |
52
+ | `api` | `messages` | `messages` (cache-friendly) or `converse` |
53
+
54
+ Environment: `JIG_BEDROCK_TEAM`, `JIG_BEDROCK_PROFILE`, `JIG_BEDROCK_REGIONS`, `JIG_BEDROCK_USAGE_DIR`,
55
+ `JIG_BEDROCK_TAG_KEY`, `JIG_BEDROCK_TAG_VALUE`. `PI_HOST_DIR` points at the Pi install when it is not beside `node`.
56
+ `PI_CACHE_RETENTION` sets Pi's own cache retention; the provider does not change it.
57
+
58
+ ## How it works
59
+
60
+ `jig-bedrock` reads the `bedrock` settings once at Pi start. Discovery and requests happen later: on the first request,
61
+ when Pi starts an SSO check, or when you run a command.
62
+
63
+ ### Discovery
64
+
65
+ Discovery runs the AWS CLI with the configured `profile` and `regions`:
66
+
67
+ - `aws sts get-caller-identity` learns the 12-digit account.
68
+ - `aws resourcegroupstaggingapi get-resources --resource-type-filters bedrock:application-inference-profile` asks for the
69
+ profiles in each region. `--tag-filters` keeps the profile's `Team` tag (or `tagKey`) and its `Active=true` tag (or
70
+ `activeTagKey`/`activeTagValue`).
71
+ - `aws bedrock get-inference-profile --inference-profile-identifier <arn>` reads each profile's name, status and model.
72
+
73
+ Only `ACTIVE` profiles are listed. Their foundation model is read from the profile's model ARN; the provider does not call
74
+ `GetFoundationModel`. A successful catalog is cached for **24 hours** in `~/.cache/jig-bedrock/catalog.json`, with private
75
+ permissions and an atomic rename. A changed account or settings discards it; `/bedrock-refresh` forces a new run.
76
+
77
+ ### Routes
78
+
79
+ There are two routes to the same Claude models:
80
+
81
+ - **Messages.** Pi's Anthropic Messages client calls
82
+ `https://bedrock-runtime.<region>.amazonaws.com/anthropic/v1/messages`. `bedrock-fetch.ts` removes the client's API-key
83
+ headers and signs each request with SigV4 (`sigv4.ts`). The signature uses the credentials the AWS SDK resolves for the
84
+ configured profile.
85
+ - **Converse.** Pi's Bedrock provider uses the AWS SDK's `ConverseStream` operation at the bedrock-runtime endpoint.
86
+
87
+ A profile is put on Messages only when `api` is `messages` (the default) and its foundation model is an Anthropic model.
88
+ Every other model uses Converse. `api: "converse"` puts every model on Converse. Both routes stream. On Messages the tools array stays
89
+ stable, so the prompt cache survives a tool change; on Converse a changed tool list rewrites it. The footer and usage
90
+ figures are the same.
91
+
92
+ A child agent (a Pi session with `PI_SUBAGENT_ID`, or the older `PI_SUBAGENT_CHILD=1`) skips the catalog refresh, the
93
+ usage program and its events, and the sign-in at start. The parent does those; the child still sends requests and reads
94
+ the parent's cached catalog.
95
+
96
+ ## Credentials
97
+
98
+ Requests use the AWS SDK's default credential chain, loaded from Pi's install (`pi-host.ts` loads
99
+ `@aws-sdk/credential-provider-node`). Discovery is different: it runs the AWS CLI with `--profile`, so the configured
100
+ profile must be usable by the CLI as well as by the SDK.
101
+
102
+ | What you have | Requests | Discovery | Sign-in repair |
103
+ | -------------------------------------------- | -------------- | -------------------------------------- | ---------------------------------------------------- |
104
+ | SSO / IAM Identity Center | yes, SDK chain | yes, `--profile` | yes: checked at start, one retry before any response |
105
+ | Access keys in a profile | yes | yes, `--profile` | no |
106
+ | Assume role in a profile | yes | yes, `--profile` | no |
107
+ | `credential_process` in a profile | yes | yes, `--profile` | no |
108
+ | Web identity or instance role | yes | only through a profile the CLI can use | no |
109
+ | Bedrock API key (`AWS_BEARER_TOKEN_BEDROCK`) | not supported | no | no |
110
+
111
+ Only the SSO path has been tested against real AWS. The other rows are supported by the SDK and CLI, but not by a real
112
+ run in this package.
113
+
114
+ For a Bedrock API key: `sso.ts`'s `profileFor` declines to choose an SSO profile when the bearer variable is set, so no
115
+ repair is attempted. With a configured profile, the request path clears that variable and refuses an explicit API key or
116
+ bearer token with `Unapproved Bedrock authentication scope`. The package does not fall back to API-key auth.
117
+
118
+ ## AWS permissions
119
+
120
+ The profile used for discovery needs these actions:
121
+
122
+ - `sts:GetCallerIdentity` for the account id;
123
+ - `tag:GetResources` for the tagged inference profiles;
124
+ - `bedrock:GetInferenceProfile` for each profile.
125
+
126
+ The provider always streams: Messages uses Bedrock's Anthropic Messages route, and Converse uses the SDK's
127
+ `ConverseStream` operation. The request therefore needs:
128
+
129
+ - `bedrock:InvokeModelWithResponseStream` for both routes. The SDK's `ConverseStream` is authorized by this action, not by
130
+ an action named after Converse.
131
+
132
+ `bedrock:InvokeModel` is not needed: this provider has no non-streaming call. If a future call adds one, it needs that
133
+ action too.
134
+
135
+ For an application inference profile, the policy must allow the action on the profile's ARN **and** on every foundation
136
+ model ARN the profile routes to.
137
+
138
+ No `List` or `GetFoundationModel` permission is needed: the code gets the foundation model from the profile's ARN.
139
+
140
+ ## Why not Windows
141
+
142
+ The SSO lock needs a POSIX owner and private `0700` directory (`ssoRuntime` in `src/login.ts` checks `process.getuid` and
143
+ mode bits), and the no-browser fallback is the WSL-only `bin/windows-browser` shell script. Native Windows has neither.
144
+
145
+ ## Commands
146
+
147
+ | Command | What it does |
148
+ | ------------------------- | ---------------------------------------------------------------------------------- |
149
+ | `/bedrock-refresh` | Refresh the discovered profiles |
150
+ | `/bedrock-usage [period]` | Show the usage program's daily, weekly or monthly figures |
151
+ | `/bedrock-stats` | Show the same cached figures as a short line |
152
+ | `/bedrock-profiles` | List the discovered profiles and why any are unavailable |
153
+ | `/aws-login` | Start the owning AWS CLI's `sso login` for the configured profile |
154
+ | `/bedrock-doctor` | Probe Node, AWS CLI, bash, jq and the optional usage program; it does not call AWS |
155
+
156
+ The footer and `/bedrock-usage` read an executable named `usage` in `usageDir`; it is called with `--daily`,
157
+ `--weekly` or `--monthly` and `--output json`, and prints the spend as JSON.
158
+
159
+ ## Tests
160
+
161
+ ```sh
162
+ npm test
163
+ npm run check
164
+ npm run scan
165
+ ```
166
+
167
+ WSL uses `bin/windows-browser` when `browser` is unset, so the sign-in opens on Windows.
168
+
169
+ Real-AWS tests are manual and need the user's approval; unit tests use recorded shapes and a fake CLI.
170
+
171
+ ## Licence
172
+
173
+ MIT.
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ # Opens the address in $1 in the Windows default browser, from WSL. jig-bedrock gives this to `aws sso login` as its BROWSER when
3
+ # Pi runs under WSL and nobody set one (src/wsl.ts). rundll32 passes the whole address on, `&` included; cmd.exe would split it there.
4
+ exec /mnt/c/Windows/System32/rundll32.exe url.dll,FileProtocolHandler "$1"
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ // The provider's extension entry, the second of the two in package.json's `pi.extensions`. It sits at bedrock/ so Pi loads the
2
+ // folder as its own extension, and the rest of the provider stays under bedrock/ with it. The code is in src/.
3
+ export { default } from "./src/index.ts";
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
- "name": "@clawdbuilder/jig-bedrock",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@clawdbuilder/jig-bedrock",
3
+ "version": "0.1.1",
4
+ "description": "Claude on AWS Bedrock for Pi: inference-profile discovery, the Messages route with cache-friendly tools, Converse fallback, SSO repair and a budget footer.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "pi-package",
9
+ "pi",
10
+ "bedrock",
11
+ "aws",
12
+ "claude",
13
+ "anthropic"
14
+ ],
15
+ "files": [
16
+ "index.ts",
17
+ "src/**",
18
+ "bin/**",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "pi": {
23
+ "extensions": [
24
+ "index.ts"
25
+ ]
26
+ },
27
+ "scripts": {
28
+ "check": "tsc -p tsconfig.json && biome ci .",
29
+ "test": "node --import ./test/support/host.mjs --test \"test/*.test.ts\"",
30
+ "scan": "node scripts/scan-company-strings.mjs"
31
+ },
32
+ "devDependencies": {
33
+ "@biomejs/biome": "2.6.4",
34
+ "@types/node": "26.6.6",
35
+ "typescript": "7.0.2",
36
+ "@earendil-works/pi-ai": "1.5.0",
37
+ "@earendil-works/pi-coding-agent": "1.5.0"
38
+ },
39
+ "publishConfig": {
40
+ "provenance": true,
41
+ "access": "public"
42
+ },
43
+ "repository": "git+https://github.com/ClawdBuilder/jig-bedrock.git",
44
+ "homepage": "https://github.com/ClawdBuilder/jig-bedrock#readme",
45
+ "engines": {
46
+ "node": ">=22.19.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@earendil-works/pi-ai": "1.5.0",
50
+ "@earendil-works/pi-coding-agent": "1.5.0"
51
+ }
52
+ }
@@ -0,0 +1,226 @@
1
+ import { execFile } from "node:child_process";
2
+ import { rmSync } from "node:fs";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { atomicCache } from "./cache.ts";
6
+ import { type Catalog, type Profile, readCatalog, validArn } from "./catalog.ts";
7
+ import type { ResolvedConfig } from "./config.ts";
8
+
9
+ // biome-ignore lint/suspicious/noExplicitAny: parsed JSON from the AWS CLI or usage program, checked field by field where it is used
10
+ type Json = any;
11
+ export type Status =
12
+ | "ok"
13
+ | "sso_expired"
14
+ | "permission_denied"
15
+ | "configuration"
16
+ | "network"
17
+ | "timeout"
18
+ | "unavailable";
19
+ export interface CatalogSource {
20
+ load(): Catalog | undefined;
21
+ refresh(force?: boolean): Promise<Status>;
22
+ }
23
+ export type Runner = (
24
+ program: string,
25
+ args: string[],
26
+ options: { timeout: number; maxBuffer: number },
27
+ ) => Promise<string>;
28
+ export const runFile: Runner = (program, args, options) =>
29
+ new Promise((resolve, reject) => {
30
+ execFile(
31
+ program,
32
+ args,
33
+ { ...options, encoding: "utf8", env: { ...process.env, AWS_CLI_AUTO_PROMPT: "off", AWS_PAGER: "" } },
34
+ (error, stdout, stderr) =>
35
+ error ? reject(new Error(error.killed ? "timeout" : stderr || error.message)) : resolve(stdout),
36
+ );
37
+ });
38
+ export function category(error: unknown): Status {
39
+ const m = String(error).toLowerCase();
40
+ return /access.?denied|not authorized to perform|unauthorizedoperation/.test(m)
41
+ ? "permission_denied"
42
+ : /could not be found|config profile|profile.*not found|missing.*(?:sso|region)|enoent/.test(m)
43
+ ? "configuration"
44
+ : /sso|token/.test(m) && /expired|invalid|login|unauthorized|does not exist/.test(m)
45
+ ? "sso_expired"
46
+ : /timeout|timed out/.test(m)
47
+ ? "timeout"
48
+ : /network|connect|dns|resolve|unreachable|proxy/.test(m)
49
+ ? "network"
50
+ : "unavailable";
51
+ }
52
+ export function discoveryHelp(status: Status): string {
53
+ switch (status) {
54
+ case "sso_expired":
55
+ return "Run /aws-login, finish browser sign-in, then /bedrock-refresh.";
56
+ case "permission_denied":
57
+ return "Ask your AWS administrator to check catalog read permissions for the configured profile; then /bedrock-refresh.";
58
+ case "configuration":
59
+ return "Check AWS CLI v2 on PATH and the configured AWS profile and regions; then /bedrock-refresh.";
60
+ case "network":
61
+ return "Check network access to AWS, then /bedrock-refresh.";
62
+ case "timeout":
63
+ return "AWS discovery timed out; check network access, then /bedrock-refresh.";
64
+ default:
65
+ return "Check AWS access and the configuration in README.md, then /bedrock-refresh.";
66
+ }
67
+ }
68
+ export function createAwsDiscovery(
69
+ config: ResolvedConfig,
70
+ runner: Runner = runFile,
71
+ file = join(homedir(), ".cache/jig-bedrock/catalog.json"),
72
+ now = Date.now,
73
+ ): CatalogSource {
74
+ let flight: Promise<Status> | undefined,
75
+ invalidAccount = false;
76
+ const load = () => (invalidAccount ? undefined : readCatalog(file, config, now()));
77
+ const perform = async (force = false): Promise<Status> => {
78
+ let remaining = 8 * 1_048_576,
79
+ pages = 0,
80
+ deadline = now() + 90_000;
81
+ const aws = async (args: string[]) => {
82
+ const timeout = Math.min(20_000, deadline - now());
83
+ if (timeout <= 0) throw new Error("timeout");
84
+ const output = await runner(
85
+ "aws",
86
+ [...args, "--profile", config.profile, "--output", "json", "--no-cli-pager"],
87
+ { timeout, maxBuffer: Math.min(remaining, 1_048_576) },
88
+ );
89
+ remaining -= Buffer.byteLength(output);
90
+ if (remaining < 0 || Buffer.byteLength(output) > 1_048_576) throw new Error("byte limit");
91
+ if (now() > deadline) throw new Error("timeout");
92
+ return JSON.parse(output);
93
+ };
94
+ try {
95
+ // A catalog younger than the TTL needs no AWS call at all, not even to learn who the caller is; /bedrock-refresh forces one.
96
+ if (!force && !invalidAccount) {
97
+ const cached = readCatalog(file, config, now());
98
+ if (cached && now() / 1000 - cached.fetched_at < 86_400) return "ok";
99
+ }
100
+ const account = (await aws(["sts", "get-caller-identity"])).Account;
101
+ if (typeof account !== "string" || !/^\d{12}$/.test(account)) throw new Error("identity");
102
+ const old = readCatalog(file, config, now());
103
+ if (old && old.account_id !== account) {
104
+ invalidAccount = true;
105
+ rmSync(file, { force: true });
106
+ } else invalidAccount = false;
107
+ const profiles: Profile[] = [],
108
+ seen = new Set<string>();
109
+ const info = (arn: string, region: string) =>
110
+ aws(["bedrock", "get-inference-profile", "--inference-profile-identifier", arn, "--region", region]);
111
+ const foundation = async (value: Json, depth = 0): Promise<string> => {
112
+ if (!Array.isArray(value.models) || !value.models.length || value.models.length > 32)
113
+ throw new Error("models");
114
+ const ids = new Set<string>();
115
+ for (const row of value.models) {
116
+ const match =
117
+ typeof row.modelArn === "string" &&
118
+ row.modelArn.match(/^arn:(aws|aws-cn|aws-us-gov):bedrock:([^:]+):([^:]*):(.+)$/);
119
+ if (!match) throw new Error("model ARN");
120
+ const resource = match[4];
121
+ if (resource.startsWith("foundation-model/")) {
122
+ const id = resource.slice(17);
123
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: reject control characters in an AWS model id
124
+ if (!id || id.length > 256 || /[\x00-\x1f]/.test(id)) throw new Error("model ID");
125
+ ids.add(id);
126
+ } else if (
127
+ resource.startsWith("inference-profile/") &&
128
+ depth < 2 &&
129
+ (!match[3] || match[3] === account)
130
+ )
131
+ ids.add(await foundation(await info(row.modelArn, match[2]), depth + 1));
132
+ else throw new Error("model scope");
133
+ }
134
+ if (ids.size !== 1) throw new Error("ambiguous model");
135
+ return [...ids][0] as string;
136
+ };
137
+ for (const region of config.regions) {
138
+ let token = "";
139
+ const tokens = new Set<string>();
140
+ for (let page = 0; page < 32; page++) {
141
+ if (++pages > 64) throw new Error("pagination");
142
+ const result = await aws([
143
+ "resourcegroupstaggingapi",
144
+ "get-resources",
145
+ "--region",
146
+ region,
147
+ "--resource-type-filters",
148
+ "bedrock:application-inference-profile",
149
+ "--tag-filters",
150
+ JSON.stringify([
151
+ { Key: config.tagKey ?? "Team", Values: [config.tagValue ?? config.team] },
152
+ { Key: config.activeTagKey ?? "Active", Values: [config.activeTagValue ?? "true"] },
153
+ ]),
154
+ "--no-paginate",
155
+ ...(token ? ["--pagination-token", token] : []),
156
+ ]);
157
+ if (!Array.isArray(result.ResourceTagMappingList) || result.ResourceTagMappingList.length > 256)
158
+ throw new Error("resources");
159
+ for (const row of result.ResourceTagMappingList) {
160
+ const arn = row.ResourceARN;
161
+ if (
162
+ typeof arn !== "string" ||
163
+ !validArn(arn, region, account) ||
164
+ seen.has(arn) ||
165
+ seen.size >= 256
166
+ )
167
+ throw new Error("profile scope");
168
+ if (
169
+ !Array.isArray(row.Tags) ||
170
+ ![
171
+ [config.tagKey ?? "Team", config.tagValue ?? config.team],
172
+ [config.activeTagKey ?? "Active", config.activeTagValue ?? "true"],
173
+ ].every(
174
+ ([key, value]) =>
175
+ row.Tags.filter((tag: Json) => tag?.Key === key).length === 1 &&
176
+ row.Tags.some((tag: Json) => tag?.Key === key && tag.Value === value),
177
+ )
178
+ )
179
+ throw new Error("profile tags");
180
+ seen.add(arn);
181
+ const value = await info(arn, region);
182
+ if (value.inferenceProfileArn !== undefined && value.inferenceProfileArn !== arn)
183
+ throw new Error("profile mismatch");
184
+ if (value.status !== "ACTIVE") continue;
185
+ const name = value.inferenceProfileName;
186
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: reject control characters in an AWS profile name
187
+ if (typeof name !== "string" || !name || name.length > 256 || /[\x00-\x1f\x7f]/.test(name))
188
+ throw new Error("profile name");
189
+ profiles.push({
190
+ arn,
191
+ profile_name: name,
192
+ foundation_model_id: await foundation(value),
193
+ region,
194
+ });
195
+ }
196
+ token = result.PaginationToken ?? "";
197
+ if (!token) break;
198
+ if (typeof token !== "string" || token.length > 2048 || tokens.has(token) || page === 31)
199
+ throw new Error("pagination");
200
+ tokens.add(token);
201
+ }
202
+ }
203
+ profiles.sort((a, b) => (a.arn < b.arn ? -1 : a.arn > b.arn ? 1 : 0));
204
+ atomicCache(file, {
205
+ schema: 1,
206
+ team: config.team,
207
+ aws_profile: config.profile,
208
+ account_id: account,
209
+ regions: config.regions,
210
+ fetched_at: Math.floor(now() / 1000),
211
+ profiles,
212
+ });
213
+ invalidAccount = false;
214
+ return "ok";
215
+ } catch (e) {
216
+ return category(e);
217
+ }
218
+ };
219
+ return {
220
+ load,
221
+ refresh: (force = false) =>
222
+ (flight ??= perform(force).finally(() => {
223
+ flight = undefined;
224
+ })),
225
+ };
226
+ }
@@ -0,0 +1,77 @@
1
+ // A fetch for Pi's Anthropic Messages client that signs every request with the configured AWS profile's SSO credentials. The
2
+ // provider's route is Bedrock's Anthropic endpoint, which authenticates with SigV4 and not with an Anthropic key, so the client's
3
+ // own auth headers are removed before signing. Credentials stay in the credential chain's memory and are never written by this
4
+ // module.
5
+ import type { AwsCredentials } from "./sigv4.ts";
6
+ import { signRequest } from "./sigv4.ts";
7
+
8
+ /** The region in a Bedrock runtime host, or undefined when the URL is not one. */
9
+ export function regionOf(url: string): string | undefined {
10
+ return /^bedrock-runtime(?:-fips)?\.([a-z0-9-]+)\.amazonaws\.com(?:\.cn)?$/.exec(new URL(url).host)?.[1];
11
+ }
12
+
13
+ /** A body this signer can hash as one buffer: what the Anthropic SDK sends for a JSON request. */
14
+ function bodyOf(body: RequestInit["body"]): string | Uint8Array {
15
+ if (body === undefined || body === null) return "";
16
+ if (typeof body === "string") return body;
17
+ if (body instanceof Uint8Array) return body;
18
+ if (body instanceof ArrayBuffer) return new Uint8Array(body);
19
+ throw new Error("Bedrock signing needs the whole request body, not a stream");
20
+ }
21
+
22
+ export interface SignedFetchOptions {
23
+ /** Resolves the profile's credentials; the AWS SDK's default provider in production. */
24
+ credentials: () => Promise<AwsCredentials>;
25
+ /** The fetch requests go through, so a test can watch them. */
26
+ fetch?: typeof globalThis.fetch;
27
+ now?: () => Date;
28
+ /** A credential failure that means the SSO session must be restored: the message has to reach Pi in one piece. */
29
+ signInHint?: string;
30
+ }
31
+
32
+ const SSO_FAILURE = /aws sso login|sso session|token is expired|expired or is otherwise invalid/i;
33
+
34
+ /** `fetch` with the SigV4 headers of the profile's credentials, and without the client's own auth headers. */
35
+ export function createSignedFetch(options: SignedFetchOptions): typeof globalThis.fetch {
36
+ const inner = options.fetch ?? globalThis.fetch;
37
+ const now = options.now ?? (() => new Date());
38
+ return async (input, init) => {
39
+ const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
40
+ const method = init?.method ?? (typeof input === "string" || input instanceof URL ? "GET" : input.method);
41
+ const region = regionOf(url);
42
+ if (region === undefined) throw new Error(`Not a Bedrock runtime URL: ${new URL(url).host}`);
43
+
44
+ const headers = new Headers(init?.headers);
45
+ // The Anthropic SDK always sends one of these because it is constructed with a key; neither may reach Bedrock.
46
+ headers.delete("x-api-key");
47
+ headers.delete("authorization");
48
+ const given: Record<string, string> = {};
49
+ headers.forEach((value, name) => {
50
+ given[name] = value;
51
+ });
52
+
53
+ let credentials: AwsCredentials;
54
+ try {
55
+ credentials = await options.credentials();
56
+ } catch (error) {
57
+ const message = error instanceof Error ? error.message : String(error);
58
+ if (SSO_FAILURE.test(message)) {
59
+ throw new Error(
60
+ `SSO session for the configured AWS profile is expired or invalid; run aws sso login with the configured profile. ${message}`,
61
+ );
62
+ }
63
+ throw error;
64
+ }
65
+ const signed = signRequest({
66
+ method,
67
+ url,
68
+ headers: given,
69
+ body: bodyOf(init?.body),
70
+ region,
71
+ credentials,
72
+ now: now(),
73
+ });
74
+ for (const [name, value] of Object.entries(signed)) headers.set(name, value);
75
+ return inner(url, { ...init, headers });
76
+ };
77
+ }