pi-claude-agent-sdk 0.9.0 → 0.9.2
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/README.md +1 -1
- package/package.json +1 -1
- package/src/child-env.ts +40 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ Fable 5.1 needs Claude Code **2.1.251 or newer**. If an explicitly configured CL
|
|
|
29
29
|
|
|
30
30
|
Behind the scenes, pi's tools are bridged to Claude Code but everything works like normal in pi. Bash commands get Claude Code's 120-second default timeout since pi's bash has none. Skills are forwarded to Claude Code's system prompt, and steering mid-turn reaches Claude at the next tool boundary.
|
|
31
31
|
|
|
32
|
-
**Authentication:** the bridge requires an Anthropic OAuth credential (or API key) configured in Pi and uses Pi's
|
|
32
|
+
**Authentication:** the bridge requires an Anthropic OAuth credential (or API key) configured in Pi and uses Pi's token refresh, renewing an OAuth token with less than two hours left before it starts a Claude Code turn (a turn keeps the token it started with). Claude Code login and inherited Claude/Anthropic authentication settings are deliberately ignored, so configure Anthropic authentication in Pi before using the provider.
|
|
33
33
|
|
|
34
34
|
The model list comes from pi-ai's Anthropic catalog automatically — when pi-ai adds a new Claude model, it appears in `/model` after updating the package, no bridge update needed. Dated snapshot ids (e.g. `claude-opus-4-5-20251101`) are not shown.
|
|
35
35
|
|
package/package.json
CHANGED
package/src/child-env.ts
CHANGED
|
@@ -65,10 +65,49 @@ export function buildClaudeChildEnv(
|
|
|
65
65
|
return env;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/** Remaining validity an OAuth token must have before it is handed to a child.
|
|
69
|
+
*
|
|
70
|
+
* A child keeps the token it was spawned with for its whole turn and cannot
|
|
71
|
+
* refresh it (it never sees the refresh token), so a turn that outlives the
|
|
72
|
+
* token fails with "401 OAuth access token has expired". Pi's default refresh
|
|
73
|
+
* window is five minutes; asking for more refreshes early instead. Anthropic
|
|
74
|
+
* access tokens last about eight hours, so this costs one extra refresh per
|
|
75
|
+
* cycle and leaves only turns longer than this exposed.
|
|
76
|
+
*/
|
|
77
|
+
export const CHILD_OAUTH_MIN_VALIDITY_MS = 2 * 60 * 60 * 1000;
|
|
78
|
+
|
|
79
|
+
interface AnthropicAuthRuntime {
|
|
80
|
+
getAuth(provider: string, overrides?: { minOAuthValidityMs?: number }): Promise<AuthResult | undefined>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Latched when the provider issues tokens shorter than the minimum: pi refreshes
|
|
84
|
+
// and then rejects the result, so retrying would refresh on every child spawn.
|
|
85
|
+
let minValidityUnsatisfiable = false;
|
|
86
|
+
|
|
87
|
+
/** Pi's public getProviderAuth() takes no overrides, but is a pass-through to
|
|
88
|
+
* ModelRuntime.getAuth(), which accepts minOAuthValidityMs. Reach the runtime
|
|
89
|
+
* when it is there and fall back to the public call otherwise. */
|
|
90
|
+
async function resolveAnthropicAuth(registry: AnthropicAuthRegistry): Promise<AuthResult | undefined> {
|
|
91
|
+
const runtime = (registry as { runtime?: Partial<AnthropicAuthRuntime> }).runtime;
|
|
92
|
+
if (!minValidityUnsatisfiable && typeof runtime?.getAuth === "function") {
|
|
93
|
+
try {
|
|
94
|
+
return await runtime.getAuth("anthropic", { minOAuthValidityMs: CHILD_OAUTH_MIN_VALIDITY_MS });
|
|
95
|
+
} catch (err) {
|
|
96
|
+
if (/expires too soon/i.test(err instanceof Error ? err.message : String(err))) minValidityUnsatisfiable = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return registry.getProviderAuth("anthropic");
|
|
100
|
+
}
|
|
101
|
+
|
|
68
102
|
export async function resolveClaudeChildEnv(
|
|
69
103
|
registry: AnthropicAuthRegistry | null | undefined,
|
|
70
104
|
base: NodeJS.ProcessEnv = process.env,
|
|
71
105
|
): Promise<NodeJS.ProcessEnv> {
|
|
72
|
-
const resolved = registry ? await registry
|
|
106
|
+
const resolved = registry ? await resolveAnthropicAuth(registry) : undefined;
|
|
73
107
|
return buildClaudeChildEnv(base, resolved);
|
|
74
108
|
}
|
|
109
|
+
|
|
110
|
+
/** Test hook: clear the unsatisfiable-minimum latch. */
|
|
111
|
+
export function resetChildAuthState(): void {
|
|
112
|
+
minValidityUnsatisfiable = false;
|
|
113
|
+
}
|