typeclaw 0.48.12 → 0.49.0
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/package.json +6 -3
- package/scripts/dump-system-prompt.ts +52 -110
- package/src/agent/attention-escalation.ts +8 -5
- package/src/agent/auth-reloadable.ts +2 -3
- package/src/agent/auth.ts +59 -79
- package/src/agent/compaction.ts +2 -2
- package/src/agent/index.ts +173 -85
- package/src/agent/live-sessions.ts +2 -2
- package/src/agent/live-subagents.ts +1 -0
- package/src/agent/llm-replay-sanitizer.ts +1 -1
- package/src/agent/model-overrides.ts +1 -1
- package/src/agent/multimodal/look-at.ts +3 -3
- package/src/agent/multimodal/read-redirect.ts +1 -1
- package/src/agent/plugin-tools.ts +76 -39
- package/src/agent/prompt-cache-key.ts +42 -0
- package/src/agent/provider-error.ts +179 -10
- package/src/agent/reload-tool.ts +1 -1
- package/src/agent/restart-handoff/index.ts +7 -14
- package/src/agent/reviewer-activity.ts +68 -0
- package/src/agent/session-origin.ts +33 -0
- package/src/agent/subagents.ts +14 -2
- package/src/agent/system-prompt.ts +68 -202
- package/src/agent/todo/continuation-policy.ts +12 -4
- package/src/agent/tool-error-remediation.ts +1 -1
- package/src/agent/tool-file-safety.ts +113 -14
- package/src/agent/tool-not-found-nudge.ts +1 -1
- package/src/agent/tool-result-budget.ts +2 -2
- package/src/agent/tools/channel-disengage.ts +2 -2
- package/src/agent/tools/channel-edit.ts +2 -2
- package/src/agent/tools/channel-fetch-attachment.ts +2 -2
- package/src/agent/tools/channel-history-render.ts +2 -1
- package/src/agent/tools/channel-history.ts +2 -2
- package/src/agent/tools/channel-react.ts +2 -2
- package/src/agent/tools/channel-read.ts +2 -2
- package/src/agent/tools/channel-reply.ts +25 -7
- package/src/agent/tools/channel-send.ts +51 -9
- package/src/agent/tools/curl-impersonate.ts +11 -2
- package/src/agent/tools/grant-role.ts +2 -2
- package/src/agent/tools/post-github-review.ts +79 -17
- package/src/agent/tools/restart.ts +2 -2
- package/src/agent/tools/skip-response.ts +4 -4
- package/src/agent/tools/spawn-subagent.ts +165 -14
- package/src/agent/tools/stream-snapshot.ts +2 -2
- package/src/agent/tools/subagent-cancel.ts +2 -2
- package/src/agent/tools/subagent-output.ts +10 -2
- package/src/agent/tools/todo/index.ts +2 -2
- package/src/agent/tools/webfetch/tool.ts +2 -2
- package/src/agent/tools/websearch.ts +2 -2
- package/src/bundled-plugins/backup/README.md +10 -9
- package/src/bundled-plugins/backup/runner.ts +140 -58
- package/src/bundled-plugins/backup/subagents.ts +58 -26
- package/src/bundled-plugins/github-cli-auth/gh-command.ts +34 -2
- package/src/bundled-plugins/github-cli-auth/granted-push.ts +135 -0
- package/src/bundled-plugins/github-cli-auth/index.ts +83 -11
- package/src/bundled-plugins/github-cli-auth/review-account.ts +23 -0
- package/src/bundled-plugins/github-cli-auth/review-checkout.ts +9 -2
- package/src/bundled-plugins/github-cli-auth/review-recorder.ts +45 -7
- package/src/bundled-plugins/operator/operator.ts +15 -4
- package/src/bundled-plugins/reviewer/skills/code-review.ts +1 -1
- package/src/bundled-plugins/security/index.ts +50 -2
- package/src/bundled-plugins/security/policies/git-exfil.ts +24 -12
- package/src/channels/adapters/discord-bot-reactions.ts +18 -0
- package/src/channels/adapters/discord-bot.ts +122 -67
- package/src/channels/adapters/discord-reactions.ts +18 -0
- package/src/channels/adapters/discord.ts +114 -8
- package/src/channels/adapters/github/inbound.ts +103 -8
- package/src/channels/adapters/github/index.ts +251 -19
- package/src/channels/adapters/github/last-alive.ts +27 -0
- package/src/channels/adapters/github/outbound.ts +54 -18
- package/src/channels/adapters/github/pending-review-store.ts +141 -0
- package/src/channels/adapters/github/permission-guidance.ts +1 -0
- package/src/channels/adapters/github/reactions.ts +100 -0
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +197 -67
- package/src/channels/adapters/github/reconcile-open-prs.ts +418 -33
- package/src/channels/adapters/github/recovery.ts +106 -0
- package/src/channels/adapters/github/review-check.ts +510 -0
- package/src/channels/adapters/github/review-submitter.ts +34 -2
- package/src/channels/adapters/instagram.ts +32 -14
- package/src/channels/adapters/kakaotalk.ts +22 -7
- package/src/channels/adapters/line.ts +18 -3
- package/src/channels/adapters/outbound-account.ts +20 -0
- package/src/channels/adapters/own-reactions.ts +66 -0
- package/src/channels/adapters/recovery-correlation.ts +214 -0
- package/src/channels/adapters/slack-bot-classify.ts +18 -11
- package/src/channels/adapters/slack-bot-dedupe.ts +111 -28
- package/src/channels/adapters/slack-bot-reactions.ts +18 -0
- package/src/channels/adapters/slack-bot.ts +125 -57
- package/src/channels/adapters/slack-classify.ts +25 -4
- package/src/channels/adapters/slack-inbound-revision.ts +96 -0
- package/src/channels/adapters/slack-reactions.ts +18 -0
- package/src/channels/adapters/slack-thread-ts.ts +6 -0
- package/src/channels/adapters/slack.ts +48 -3
- package/src/channels/adapters/teams.ts +14 -4
- package/src/channels/adapters/telegram-bot.ts +56 -10
- package/src/channels/adapters/webex-bot.ts +12 -2
- package/src/channels/adapters/webex.ts +12 -2
- package/src/channels/background-handoff.ts +434 -0
- package/src/channels/background-obligations.ts +893 -0
- package/src/channels/continuity-types.ts +271 -0
- package/src/channels/github-review-check.ts +598 -0
- package/src/channels/github-review-turn-ledger.ts +102 -28
- package/src/channels/github-review-verdict-coordinator.ts +96 -13
- package/src/channels/github-token-bridge.ts +11 -5
- package/src/channels/inbound-journal.ts +1738 -0
- package/src/channels/index.ts +6 -1
- package/src/channels/manager.ts +17 -7
- package/src/channels/recovery-dispatcher.ts +264 -0
- package/src/channels/recovery-notice.ts +94 -0
- package/src/channels/recovery-outbox.ts +459 -0
- package/src/channels/router.ts +2582 -633
- package/src/channels/schema.ts +8 -1
- package/src/channels/subagent-completion-bridge.ts +17 -14
- package/src/channels/types.ts +121 -16
- package/src/channels/waiting-reactions.ts +664 -0
- package/src/cli/compose.ts +7 -4
- package/src/cli/hostd.ts +49 -3
- package/src/cli/inspect.ts +11 -14
- package/src/cli/model.ts +7 -5
- package/src/cli/oauth-callbacks.ts +38 -23
- package/src/coding-agent/broker-body.ts +38 -0
- package/src/coding-agent/broker.ts +290 -0
- package/src/coding-agent/claude.ts +95 -0
- package/src/coding-agent/codex.ts +104 -0
- package/src/coding-agent/index.ts +506 -0
- package/src/coding-agent/omo-state.ts +69 -0
- package/src/coding-agent/omo-stream.ts +98 -0
- package/src/coding-agent/omo.ts +192 -0
- package/src/coding-agent/runner.ts +263 -0
- package/src/coding-agent/tool.ts +62 -0
- package/src/coding-agent/worktree.ts +355 -0
- package/src/compose/restart.ts +10 -3
- package/src/compose/start.ts +20 -3
- package/src/config/config.ts +70 -26
- package/src/config/providers.ts +278 -50
- package/src/container/exit-reason.ts +44 -0
- package/src/container/index.ts +5 -0
- package/src/container/log-archive.ts +51 -13
- package/src/container/memory-limit.ts +1 -1
- package/src/container/memory-oversubscription.ts +36 -6
- package/src/container/oom-events.ts +86 -0
- package/src/container/restart.ts +2 -0
- package/src/container/shared.ts +8 -1
- package/src/container/start.ts +25 -13
- package/src/container/stop.ts +6 -2
- package/src/container/tool-oom-priority.ts +32 -0
- package/src/cron/consumer.ts +10 -4
- package/src/cron/reloadable.ts +23 -2
- package/src/doctor/checks.ts +14 -2
- package/src/hostd/daemon.ts +20 -10
- package/src/hostd/spawn.ts +106 -5
- package/src/hostd/version.ts +20 -0
- package/src/hostd/watchdog.ts +241 -0
- package/src/init/dockerfile.ts +57 -592
- package/src/init/index.ts +6 -7
- package/src/init/oauth-login.ts +57 -17
- package/src/inspect/transcript-view.ts +6 -3
- package/src/mcp/client.ts +3 -1
- package/src/operations/classify-tool-outcome.ts +45 -1
- package/src/operations/incidents.ts +10 -5
- package/src/plugin/core-tool-names.ts +1 -1
- package/src/plugin/define.ts +1 -0
- package/src/plugin/index.ts +1 -0
- package/src/plugin/types.ts +6 -1
- package/src/run/background-handoff-boot.ts +86 -0
- package/src/run/channel-session-factory.ts +5 -4
- package/src/run/index.ts +93 -93
- package/src/run/resource-sample.ts +19 -0
- package/src/sandbox/build.ts +4 -0
- package/src/sandbox/coding-agent.ts +214 -0
- package/src/sandbox/policy.ts +1 -0
- package/src/sandbox/runtime-private.ts +4 -1
- package/src/secrets/coding-agent-auth.ts +97 -0
- package/src/secrets/index.ts +0 -16
- package/src/secrets/refresh-provider-oauth.ts +30 -76
- package/src/secrets/schema.ts +3 -4
- package/src/secrets/storage.ts +133 -234
- package/src/server/command-runner.ts +6 -2
- package/src/server/index.ts +102 -41
- package/src/sessions/index.ts +1 -1
- package/src/skills/typeclaw-channel-github/SKILL.md +7 -5
- package/src/skills/typeclaw-claude-code/SKILL.md +25 -260
- package/src/skills/typeclaw-claude-code/references/auth-flow.md +7 -23
- package/src/skills/typeclaw-claude-code/references/headless-sessions.md +11 -0
- package/src/skills/typeclaw-claude-code/references/results-and-worktrees.md +13 -0
- package/src/skills/typeclaw-codex-cli/SKILL.md +35 -270
- package/src/skills/typeclaw-codex-cli/references/auth-flow.md +13 -20
- package/src/skills/typeclaw-codex-cli/references/headless-sessions.md +26 -0
- package/src/skills/typeclaw-codex-cli/references/results-and-worktrees.md +28 -0
- package/src/skills/typeclaw-config/SKILL.md +24 -24
- package/src/skills/typeclaw-config/references/dockerfile.md +14 -13
- package/src/skills/typeclaw-cron/SKILL.md +1 -1
- package/src/skills/typeclaw-github-contributing/SKILL.md +2 -2
- package/src/skills/typeclaw-memory/SKILL.md +3 -3
- package/src/skills/typeclaw-omo-cli/SKILL.md +52 -0
- package/src/skills/typeclaw-omo-cli/references/auth-flow.md +11 -0
- package/src/skills/typeclaw-omo-cli/references/headless-sessions.md +11 -0
- package/src/skills/typeclaw-omo-cli/references/results-and-worktrees.md +23 -0
- package/src/skills/typeclaw-plugins/SKILL.md +20 -20
- package/src/skills/typeclaw-skills/SKILL.md +19 -2
- package/src/skills/typeclaw-troubleshooting/SKILL.md +15 -2
- package/src/tui/index.ts +5 -3
- package/src/tui/theme.ts +1 -1
- package/typeclaw.schema.json +44 -2
- package/src/agent/adaptive-thinking-compat.ts +0 -79
- package/src/secrets/claude-credentials-json.ts +0 -129
- package/src/secrets/codex-auth-json.ts +0 -102
- package/src/secrets/export-claude-credentials-file.ts +0 -270
- package/src/secrets/export-codex-auth-file.ts +0 -250
- package/src/secrets/oauth-xai.ts +0 -348
- package/src/skills/typeclaw-claude-code/references/stop-hook.md +0 -101
- package/src/skills/typeclaw-claude-code/references/tmux-driving.md +0 -243
- package/src/skills/typeclaw-codex-cli/references/stop-hook.md +0 -92
- package/src/skills/typeclaw-codex-cli/references/tmux-driving.md +0 -239
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import type { Credential, CredentialStore } from '@earendil-works/pi-ai'
|
|
4
|
+
import { ModelRuntime } from '@earendil-works/pi-coding-agent'
|
|
5
|
+
|
|
6
|
+
import { createSecretsStoreForAgent } from './storage'
|
|
7
|
+
|
|
8
|
+
/** Runtime-only value. Never serialize this into a tool receipt or child configuration. */
|
|
9
|
+
export type CodingAgentProvider = 'anthropic' | 'openai-codex'
|
|
10
|
+
export type CodingAgentAuth = {
|
|
11
|
+
readonly token: string
|
|
12
|
+
readonly secrets: readonly string[]
|
|
13
|
+
} & (
|
|
14
|
+
| { readonly provider?: 'anthropic'; readonly type: 'api_key' | 'oauth' }
|
|
15
|
+
| { readonly provider: 'openai-codex'; readonly type: 'oauth'; readonly accountId: string }
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
export class CodingAgentAuthError extends Error {
|
|
19
|
+
constructor(provider: CodingAgentProvider) {
|
|
20
|
+
super(
|
|
21
|
+
`${provider} authentication is unavailable. On the host, run \`typeclaw provider add ${provider}\`, then retry this task.`,
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type CodingAgentAuthResolver = {
|
|
27
|
+
resolve(signal?: AbortSignal, rejected?: CodingAgentAuth, provider?: CodingAgentProvider): Promise<CodingAgentAuth>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function createCodingAgentAuthResolver(
|
|
31
|
+
agentDir: string,
|
|
32
|
+
dependencies?: { credentials: CredentialStore; runtime: ModelRuntime },
|
|
33
|
+
): Promise<CodingAgentAuthResolver> {
|
|
34
|
+
const credentials = dependencies?.credentials ?? createSecretsStoreForAgent(join(agentDir, 'secrets.json'))
|
|
35
|
+
const runtime =
|
|
36
|
+
dependencies?.runtime ?? (await ModelRuntime.create({ credentials, modelsPath: null, refreshOnCreate: false }))
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
async resolve(signal, rejected, provider = 'anthropic') {
|
|
40
|
+
try {
|
|
41
|
+
// Invalidate only the rejected generation, under the SAME lock used by pi.
|
|
42
|
+
// A concurrent refresh or host re-login must never be overwritten.
|
|
43
|
+
if (rejected?.provider === provider && rejected.type === 'oauth') {
|
|
44
|
+
await credentials.modify(
|
|
45
|
+
provider,
|
|
46
|
+
async (current) => {
|
|
47
|
+
if (current?.type === 'oauth' && current.access === rejected.token) {
|
|
48
|
+
return { ...current, expires: 0 }
|
|
49
|
+
}
|
|
50
|
+
return undefined
|
|
51
|
+
},
|
|
52
|
+
{ signal },
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
const stored = await credentials.read(provider, { signal })
|
|
56
|
+
// Subscription delegation must never fall back to separately billed API auth.
|
|
57
|
+
if (provider === 'openai-codex' && stored?.type !== 'oauth') throw new CodingAgentAuthError(provider)
|
|
58
|
+
const resolved = await runtime.getAuth(provider, {
|
|
59
|
+
signal,
|
|
60
|
+
// Persisted OAuth wins over ambient API keys, as on the main-agent path.
|
|
61
|
+
...(stored?.type === 'oauth' || dependencies ? { env: {} } : {}),
|
|
62
|
+
})
|
|
63
|
+
if (!resolved?.auth.apiKey) throw new CodingAgentAuthError(provider)
|
|
64
|
+
const token = resolved.auth.apiKey
|
|
65
|
+
const current = await credentials.read(provider, { signal })
|
|
66
|
+
const secrets = [token, ...credentialSecrets(stored), ...credentialSecrets(current)].filter(Boolean)
|
|
67
|
+
if (provider === 'openai-codex') {
|
|
68
|
+
// Bind the account to the exact resolved token generation, not a racing re-login.
|
|
69
|
+
const credential = [current, stored].find((value) => value?.type === 'oauth' && value.access === token)
|
|
70
|
+
if (
|
|
71
|
+
credential?.type !== 'oauth' ||
|
|
72
|
+
typeof credential.accountId !== 'string' ||
|
|
73
|
+
!/^[\x21-\x7e]+$/.test(credential.accountId)
|
|
74
|
+
)
|
|
75
|
+
throw new CodingAgentAuthError(provider)
|
|
76
|
+
return { provider, type: 'oauth', token, accountId: credential.accountId, secrets }
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
provider,
|
|
80
|
+
type: current?.type === 'oauth' ? 'oauth' : 'api_key',
|
|
81
|
+
token,
|
|
82
|
+
secrets,
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// OAuth transports can include token endpoint bodies in their errors.
|
|
86
|
+
throw new CodingAgentAuthError(provider)
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function credentialSecrets(credential: Credential | undefined): string[] {
|
|
93
|
+
if (credential?.type !== 'oauth') return []
|
|
94
|
+
return [credential.access, credential.refresh, credential.idToken, credential.id_token].filter(
|
|
95
|
+
(value): value is string => typeof value === 'string' && value.length > 0,
|
|
96
|
+
)
|
|
97
|
+
}
|
package/src/secrets/index.ts
CHANGED
|
@@ -24,22 +24,6 @@ export {
|
|
|
24
24
|
exportGithubCliStoreIfApplicable,
|
|
25
25
|
} from './export-github-cli-store'
|
|
26
26
|
|
|
27
|
-
export {
|
|
28
|
-
type ExportCodexAuthFileResult,
|
|
29
|
-
exportCodexAuthFileForAgent,
|
|
30
|
-
exportCodexAuthFileIfApplicable,
|
|
31
|
-
} from './export-codex-auth-file'
|
|
32
|
-
|
|
33
|
-
export {
|
|
34
|
-
CLAUDE_CREDENTIALS_FILE_NAME,
|
|
35
|
-
CLAUDE_CREDENTIALS_RELATIVE_PATH,
|
|
36
|
-
CLAUDE_DEFAULT_CONFIG_DIR_NAME,
|
|
37
|
-
type ExportClaudeCredentialsFileResult,
|
|
38
|
-
exportClaudeCredentialsFileForAgent,
|
|
39
|
-
exportClaudeCredentialsFileIfApplicable,
|
|
40
|
-
resolveClaudeCredentialsPath,
|
|
41
|
-
} from './export-claude-credentials-file'
|
|
42
|
-
|
|
43
27
|
export {
|
|
44
28
|
type ProviderRefreshEntry,
|
|
45
29
|
type ProviderRefreshOutcome,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { join } from 'node:path'
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { CredentialStore } from '@earendil-works/pi-ai'
|
|
4
|
+
import { ModelRuntime } from '@earendil-works/pi-coding-agent'
|
|
4
5
|
|
|
5
6
|
import { createSecretsStoreForAgent } from './storage'
|
|
6
7
|
|
|
@@ -15,9 +16,8 @@ export type ProviderRefreshOutcome = 'valid-or-refreshed' | 'refresh-failed'
|
|
|
15
16
|
export type ProviderRefreshEntry = {
|
|
16
17
|
providerId: string
|
|
17
18
|
outcome: ProviderRefreshOutcome
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
// token endpoint, revoked refresh token, unknown OAuth provider).
|
|
19
|
+
// ModelRuntime returns the refresh rejection directly. This field is the
|
|
20
|
+
// operator-visible reason; there is no process-global error queue.
|
|
21
21
|
error?: string
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -26,103 +26,57 @@ export type RefreshProviderOAuthResult = {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export type RefreshProviderOAuthOptions = {
|
|
29
|
-
|
|
29
|
+
modelRuntime: ModelRuntime
|
|
30
|
+
credentials: CredentialStore
|
|
30
31
|
log?: (message: string) => void
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
// WHY THIS EXISTS. `AuthStorage.getApiKey()` is the SDK's only refresh trigger,
|
|
38
|
-
// and it is lazy: it fires on the first LLM call for a provider. A container
|
|
39
|
-
// that restarts with an already-expired access token therefore doesn't refresh
|
|
40
|
-
// until a real turn runs — and if that refresh then fails (a wedged network
|
|
41
|
-
// stack, a rotated refresh token), the failure surfaces as a generic
|
|
42
|
-
// provider-error notice posted into a live channel/PR thread. Channel-adapter
|
|
43
|
-
// OAuth already has host-side renewal crons (Kakao/Webex/Teams); provider OAuth
|
|
44
|
-
// had nothing. This closes that gap by paying the refresh cost at boot, where a
|
|
45
|
-
// failure is logged for the operator rather than shown to end users.
|
|
46
|
-
//
|
|
47
|
-
// Reuses the SDK path verbatim rather than re-implementing token refresh: a
|
|
48
|
-
// single `getApiKey()` call per provider runs the exact locked
|
|
49
|
-
// refresh-then-persist-to-secrets.json flow the runtime uses on every turn, so
|
|
50
|
-
// a boot-refreshed token is written back through the same atomic writer and no
|
|
51
|
-
// second refresh implementation can drift from it. `includeFallback: false`
|
|
52
|
-
// keeps an unrelated env var from masking an OAuth refresh failure as success.
|
|
53
|
-
//
|
|
54
|
-
// Never throws: a boot-time credential probe must not block the agent from
|
|
55
|
-
// starting. Each provider is isolated — one provider's failure doesn't stop the
|
|
56
|
-
// others from being probed — and every failure is returned for the caller to
|
|
57
|
-
// log.
|
|
34
|
+
// Container-stage boot probe: ModelRuntime owns signal-aware OAuth refresh and
|
|
35
|
+
// runs it inside CredentialStore.modify, so this shares the exact live-request
|
|
36
|
+
// lock/persist path without reimplementing provider token exchange.
|
|
58
37
|
export async function refreshProviderOAuthCredentials(
|
|
59
38
|
options: RefreshProviderOAuthOptions,
|
|
60
39
|
): Promise<RefreshProviderOAuthResult> {
|
|
61
|
-
const { authStorage } = options
|
|
62
40
|
const entries: ProviderRefreshEntry[] = []
|
|
41
|
+
const credentials = await options.credentials.list()
|
|
63
42
|
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// Always drain so one provider's recorded error can't leak into the next
|
|
75
|
-
// provider's outcome. drainErrors clears the buffer as it reads.
|
|
76
|
-
const drained = authStorage.drainErrors()
|
|
77
|
-
|
|
78
|
-
if (apiKey !== undefined && apiKey !== '') {
|
|
79
|
-
entries.push({ providerId, outcome: 'valid-or-refreshed' })
|
|
80
|
-
continue
|
|
43
|
+
for (const credential of credentials) {
|
|
44
|
+
if (credential.type !== 'oauth') continue
|
|
45
|
+
try {
|
|
46
|
+
const auth = await options.modelRuntime.getAuth(credential.providerId)
|
|
47
|
+
if (!auth?.auth.apiKey) throw new Error('refresh returned no API key')
|
|
48
|
+
entries.push({ providerId: credential.providerId, outcome: 'valid-or-refreshed' })
|
|
49
|
+
} catch (error) {
|
|
50
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
51
|
+
options.log?.(`refreshProviderOAuth: ${credential.providerId} refresh failed: ${reason}`)
|
|
52
|
+
entries.push({ providerId: credential.providerId, outcome: 'refresh-failed', error: reason })
|
|
81
53
|
}
|
|
82
|
-
|
|
83
|
-
const error = drained.length > 0 ? drained.map((e) => e.message).join('; ') : 'refresh returned no API key'
|
|
84
|
-
options.log?.(`refreshProviderOAuth: ${providerId} refresh failed: ${error}`)
|
|
85
|
-
entries.push({ providerId, outcome: 'refresh-failed', error })
|
|
86
54
|
}
|
|
87
|
-
|
|
88
55
|
return { entries }
|
|
89
56
|
}
|
|
90
57
|
|
|
91
|
-
// getApiKey already swallows refresh errors into drainErrors and returns
|
|
92
|
-
// undefined, but guard the call itself so an unexpected throw (e.g. a corrupt
|
|
93
|
-
// credential shape the SDK doesn't expect) degrades to a failed entry instead
|
|
94
|
-
// of aborting the whole boot-time sweep.
|
|
95
|
-
async function resolveApiKeySafely(authStorage: AuthStorage, providerId: string): Promise<string | undefined> {
|
|
96
|
-
try {
|
|
97
|
-
return await authStorage.getApiKey(providerId, { includeFallback: false })
|
|
98
|
-
} catch {
|
|
99
|
-
return undefined
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
58
|
export type RefreshProviderOAuthForAgentOptions = {
|
|
104
59
|
agentDir: string
|
|
105
60
|
log?: (message: string) => void
|
|
106
61
|
}
|
|
107
62
|
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
// stage uses per provider (createSecretsStoreForAgent) so the refresh writes
|
|
112
|
-
// back through the bind-mounted secrets.json exactly as a live turn would.
|
|
63
|
+
// Container-stage convenience wrapper. Host CLI never calls this path; it
|
|
64
|
+
// reads the bind-mounted v2 envelope and asks ModelRuntime to refresh before
|
|
65
|
+
// the first user turn.
|
|
113
66
|
export async function refreshProviderOAuthForAgent(
|
|
114
67
|
options: RefreshProviderOAuthForAgentOptions,
|
|
115
68
|
): Promise<RefreshProviderOAuthResult> {
|
|
116
|
-
let authStorage: AuthStorage
|
|
117
69
|
try {
|
|
118
|
-
|
|
70
|
+
const credentials = createSecretsStoreForAgent(join(options.agentDir, 'secrets.json'))
|
|
71
|
+
const modelRuntime = await ModelRuntime.create({ credentials, modelsPath: null, refreshOnCreate: false })
|
|
72
|
+
return await refreshProviderOAuthCredentials({
|
|
73
|
+
credentials,
|
|
74
|
+
modelRuntime,
|
|
75
|
+
...(options.log !== undefined ? { log: options.log } : {}),
|
|
76
|
+
})
|
|
119
77
|
} catch (err) {
|
|
120
78
|
const reason = err instanceof Error ? err.message : String(err)
|
|
121
79
|
options.log?.(`refreshProviderOAuth: ${reason}`)
|
|
122
80
|
return { entries: [] }
|
|
123
81
|
}
|
|
124
|
-
return refreshProviderOAuthCredentials({
|
|
125
|
-
authStorage,
|
|
126
|
-
...(options.log !== undefined ? { log: options.log } : {}),
|
|
127
|
-
})
|
|
128
82
|
}
|
package/src/secrets/schema.ts
CHANGED
|
@@ -3,10 +3,9 @@ import { z } from 'zod'
|
|
|
3
3
|
import { secretFieldSchema } from './resolve'
|
|
4
4
|
|
|
5
5
|
// providers.<id> for api-key credentials: the `key` field is a Secret (string
|
|
6
|
-
// shorthand or `{ value?, env? }` object).
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
// are not env-injectable, so they pass through unchanged via catchall.
|
|
6
|
+
// shorthand or `{ value?, env? }` object). CredentialStore resolves this at
|
|
7
|
+
// request time; OAuth credentials carry stateful refresh/access tokens and
|
|
8
|
+
// are never env-injectable.
|
|
10
9
|
const apiKeyProviderSchema = z.object({
|
|
11
10
|
type: z.literal('api_key'),
|
|
12
11
|
key: secretFieldSchema,
|