@bastani/atomic 0.9.11-alpha.7 → 0.9.11-alpha.8
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/CHANGELOG.md +24 -0
- package/README.md +3 -5
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/mcp/sampling-handler.ts +1 -5
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +1 -0
- package/dist/builtin/workflows/README.md +1 -1
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/extension/extension-runtime-state.ts +1 -24
- package/dist/builtin/workflows/src/extension/index.bundle.mjs +31 -28
- package/dist/builtin/workflows/src/extension/workflow-model-catalog.ts +26 -0
- package/dist/builtin/workflows/src/extension/workflow-prompts.ts +3 -2
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-controller.ts +5 -5
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-session-options.ts +3 -3
- package/dist/builtin/workflows/src/runs/foreground/stage-runner-session.ts +2 -2
- package/dist/cli/list-models.d.ts +2 -2
- package/dist/cli/list-models.d.ts.map +1 -1
- package/dist/cli/list-models.js +5 -5
- package/dist/cli/list-models.js.map +1 -1
- package/dist/core/agent-session-accessors.js +1 -1
- package/dist/core/agent-session-accessors.js.map +1 -1
- package/dist/core/agent-session-auto-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-auto-compaction.js +2 -5
- package/dist/core/agent-session-auto-compaction.js.map +1 -1
- package/dist/core/agent-session-compaction.js +1 -1
- package/dist/core/agent-session-compaction.js.map +1 -1
- package/dist/core/agent-session-extension-bindings.js +5 -5
- package/dist/core/agent-session-extension-bindings.js.map +1 -1
- package/dist/core/agent-session-methods.d.ts +4 -4
- package/dist/core/agent-session-methods.d.ts.map +1 -1
- package/dist/core/agent-session-methods.js.map +1 -1
- package/dist/core/agent-session-models.d.ts +1 -1
- package/dist/core/agent-session-models.d.ts.map +1 -1
- package/dist/core/agent-session-models.js +18 -14
- package/dist/core/agent-session-models.js.map +1 -1
- package/dist/core/agent-session-post-tool-compaction.d.ts.map +1 -1
- package/dist/core/agent-session-post-tool-compaction.js +2 -4
- package/dist/core/agent-session-post-tool-compaction.js.map +1 -1
- package/dist/core/agent-session-prompt.d.ts.map +1 -1
- package/dist/core/agent-session-prompt.js +3 -7
- package/dist/core/agent-session-prompt.js.map +1 -1
- package/dist/core/agent-session-retry.js +1 -1
- package/dist/core/agent-session-retry.js.map +1 -1
- package/dist/core/agent-session-runtime-auth.d.ts +3 -3
- package/dist/core/agent-session-runtime-auth.d.ts.map +1 -1
- package/dist/core/agent-session-runtime-auth.js +4 -14
- package/dist/core/agent-session-runtime-auth.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +1 -1
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +3 -3
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +3 -8
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +16 -23
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session-tool-registry.d.ts.map +1 -1
- package/dist/core/agent-session-tool-registry.js +2 -1
- package/dist/core/agent-session-tool-registry.js.map +1 -1
- package/dist/core/agent-session-types.d.ts +2 -2
- package/dist/core/agent-session-types.d.ts.map +1 -1
- package/dist/core/agent-session-types.js.map +1 -1
- package/dist/core/agent-session.d.ts +2 -2
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +1 -1
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage-backends.d.ts +1 -0
- package/dist/core/auth-storage-backends.d.ts.map +1 -1
- package/dist/core/auth-storage-backends.js +18 -4
- package/dist/core/auth-storage-backends.js.map +1 -1
- package/dist/core/auth-storage.d.ts +12 -139
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +68 -374
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/copilot-env-routing.d.ts +36 -0
- package/dist/core/copilot-env-routing.d.ts.map +1 -0
- package/dist/core/copilot-env-routing.js +94 -0
- package/dist/core/copilot-env-routing.js.map +1 -0
- package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.js +10 -2
- package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
- package/dist/core/extensions/provider-types.d.ts +0 -40
- package/dist/core/extensions/provider-types.d.ts.map +1 -1
- package/dist/core/extensions/provider-types.js.map +1 -1
- package/dist/core/fallback-models.d.ts +4 -4
- package/dist/core/fallback-models.d.ts.map +1 -1
- package/dist/core/fallback-models.js +3 -3
- package/dist/core/fallback-models.js.map +1 -1
- package/dist/core/model-config.d.ts +542 -0
- package/dist/core/model-config.d.ts.map +1 -0
- package/dist/core/{model-registry-schemas.js → model-config.js} +85 -34
- package/dist/core/model-config.js.map +1 -0
- package/dist/core/model-registry.d.ts +25 -85
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +65 -388
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver-cli.d.ts +2 -2
- package/dist/core/model-resolver-cli.d.ts.map +1 -1
- package/dist/core/model-resolver-cli.js +2 -2
- package/dist/core/model-resolver-cli.js.map +1 -1
- package/dist/core/model-resolver-initial.d.ts +4 -4
- package/dist/core/model-resolver-initial.d.ts.map +1 -1
- package/dist/core/model-resolver-initial.js +17 -17
- package/dist/core/model-resolver-initial.js.map +1 -1
- package/dist/core/model-resolver-scope.d.ts +3 -3
- package/dist/core/model-resolver-scope.d.ts.map +1 -1
- package/dist/core/model-resolver-scope.js +4 -4
- package/dist/core/model-resolver-scope.js.map +1 -1
- package/dist/core/model-runtime-auth.d.ts +7 -0
- package/dist/core/model-runtime-auth.d.ts.map +1 -0
- package/dist/core/model-runtime-auth.js +14 -0
- package/dist/core/model-runtime-auth.js.map +1 -0
- package/dist/core/model-runtime-providers.d.ts +6 -0
- package/dist/core/model-runtime-providers.d.ts.map +1 -0
- package/dist/core/model-runtime-providers.js +22 -0
- package/dist/core/model-runtime-providers.js.map +1 -0
- package/dist/core/model-runtime-restoration.d.ts +6 -0
- package/dist/core/model-runtime-restoration.d.ts.map +1 -0
- package/dist/core/model-runtime-restoration.js +21 -0
- package/dist/core/model-runtime-restoration.js.map +1 -0
- package/dist/core/model-runtime-snapshot.d.ts +16 -0
- package/dist/core/model-runtime-snapshot.d.ts.map +1 -0
- package/dist/core/model-runtime-snapshot.js +52 -0
- package/dist/core/model-runtime-snapshot.js.map +1 -0
- package/dist/core/model-runtime-streaming.d.ts +17 -0
- package/dist/core/model-runtime-streaming.d.ts.map +1 -0
- package/dist/core/model-runtime-streaming.js +66 -0
- package/dist/core/model-runtime-streaming.js.map +1 -0
- package/dist/core/model-runtime-types.d.ts +21 -0
- package/dist/core/model-runtime-types.d.ts.map +1 -0
- package/dist/core/model-runtime-types.js +2 -0
- package/dist/core/model-runtime-types.js.map +1 -0
- package/dist/core/model-runtime.d.ts +50 -29
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +330 -90
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/models-store.d.ts +2 -10
- package/dist/core/models-store.d.ts.map +1 -1
- package/dist/core/models-store.js +2 -29
- package/dist/core/models-store.js.map +1 -1
- package/dist/core/oauth-login.d.ts +24 -0
- package/dist/core/oauth-login.d.ts.map +1 -0
- package/dist/core/oauth-login.js +87 -0
- package/dist/core/oauth-login.js.map +1 -0
- package/dist/core/oauth-provider-metadata.d.ts +5 -0
- package/dist/core/oauth-provider-metadata.d.ts.map +1 -0
- package/dist/core/oauth-provider-metadata.js +19 -0
- package/dist/core/oauth-provider-metadata.js.map +1 -0
- package/dist/core/provider-composer-internal.d.ts +53 -0
- package/dist/core/provider-composer-internal.d.ts.map +1 -0
- package/dist/core/provider-composer-internal.js +273 -0
- package/dist/core/provider-composer-internal.js.map +1 -0
- package/dist/core/provider-composer.d.ts +16 -0
- package/dist/core/provider-composer.d.ts.map +1 -0
- package/dist/core/provider-composer.js +108 -0
- package/dist/core/provider-composer.js.map +1 -0
- package/dist/core/remote-catalog-provider.d.ts.map +1 -1
- package/dist/core/remote-catalog-provider.js +74 -122
- package/dist/core/remote-catalog-provider.js.map +1 -1
- package/dist/core/resolve-config-value.d.ts +8 -7
- package/dist/core/resolve-config-value.d.ts.map +1 -1
- package/dist/core/resolve-config-value.js +26 -23
- package/dist/core/resolve-config-value.js.map +1 -1
- package/dist/core/runtime-credentials.d.ts +16 -0
- package/dist/core/runtime-credentials.d.ts.map +1 -0
- package/dist/core/runtime-credentials.js +42 -0
- package/dist/core/runtime-credentials.js.map +1 -0
- package/dist/core/sdk-types.d.ts +1 -7
- package/dist/core/sdk-types.d.ts.map +1 -1
- package/dist/core/sdk-types.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +27 -28
- package/dist/core/sdk.js.map +1 -1
- package/dist/extensions/llama/index.js +1 -1
- package/dist/extensions/llama/index.js.map +1 -1
- package/dist/extensions/llama/provider.d.ts +2 -4
- package/dist/extensions/llama/provider.d.ts.map +1 -1
- package/dist/extensions/llama/provider.js +25 -17
- package/dist/extensions/llama/provider.js.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -4
- package/dist/index.js.map +1 -1
- package/dist/main-runtime-api-key.d.ts +6 -0
- package/dist/main-runtime-api-key.d.ts.map +1 -0
- package/dist/main-runtime-api-key.js +6 -0
- package/dist/main-runtime-api-key.js.map +1 -0
- package/dist/main-session-options.d.ts +2 -2
- package/dist/main-session-options.d.ts.map +1 -1
- package/dist/main-session-options.js +3 -3
- package/dist/main-session-options.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +9 -11
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/footer.js +1 -1
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
- package/dist/modes/interactive/components/login-dialog.js +1 -3
- package/dist/modes/interactive/components/login-dialog.js.map +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts +4 -3
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +56 -73
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts +4 -3
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +15 -24
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-agent-events.js +1 -1
- package/dist/modes/interactive/interactive-agent-events.js.map +1 -1
- package/dist/modes/interactive/interactive-auth-login.js +20 -31
- package/dist/modes/interactive/interactive-auth-login.js.map +1 -1
- package/dist/modes/interactive/interactive-auth-routing.d.ts +1 -1
- package/dist/modes/interactive/interactive-auth-routing.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-auth-routing.js +21 -47
- package/dist/modes/interactive/interactive-auth-routing.js.map +1 -1
- package/dist/modes/interactive/interactive-autocomplete.js +3 -3
- package/dist/modes/interactive/interactive-autocomplete.js.map +1 -1
- package/dist/modes/interactive/interactive-deferred-startup.js +8 -8
- package/dist/modes/interactive/interactive-deferred-startup.js.map +1 -1
- package/dist/modes/interactive/interactive-extension-runtime.js +2 -1
- package/dist/modes/interactive/interactive-extension-runtime.js.map +1 -1
- package/dist/modes/interactive/interactive-model-catalog-startup.js +2 -2
- package/dist/modes/interactive/interactive-model-catalog-startup.js.map +1 -1
- package/dist/modes/interactive/interactive-model-routing.js +11 -11
- package/dist/modes/interactive/interactive-model-routing.js.map +1 -1
- package/dist/modes/interactive/interactive-render-chat.js +1 -1
- package/dist/modes/interactive/interactive-render-chat.js.map +1 -1
- package/dist/modes/interactive/interactive-slash-commands.js +2 -2
- package/dist/modes/interactive/interactive-slash-commands.js.map +1 -1
- package/dist/modes/interactive/interactive-startup.js +1 -1
- package/dist/modes/interactive/interactive-startup.js.map +1 -1
- package/dist/modes/interactive-engine/isolated-auth.d.ts +2 -2
- package/dist/modes/interactive-engine/isolated-auth.d.ts.map +1 -1
- package/dist/modes/interactive-engine/isolated-auth.js +4 -3
- package/dist/modes/interactive-engine/isolated-auth.js.map +1 -1
- package/dist/modes/interactive-engine/isolated-runtime.d.ts.map +1 -1
- package/dist/modes/interactive-engine/isolated-runtime.js +4 -3
- package/dist/modes/interactive-engine/isolated-runtime.js.map +1 -1
- package/dist/modes/interactive-engine/remote-model-catalog.d.ts +0 -1
- package/dist/modes/interactive-engine/remote-model-catalog.d.ts.map +1 -1
- package/dist/modes/interactive-engine/remote-model-catalog.js +11 -50
- package/dist/modes/interactive-engine/remote-model-catalog.js.map +1 -1
- package/dist/modes/rpc/rpc-client-api.d.ts +2 -2
- package/dist/modes/rpc/rpc-client-api.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client-api.js.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-command-handler.js +26 -19
- package/dist/modes/rpc/rpc-command-handler.js.map +1 -1
- package/dist/modes/rpc/rpc-oauth-client.d.ts +1 -1
- package/dist/modes/rpc/rpc-oauth-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-oauth-client.js +1 -1
- package/dist/modes/rpc/rpc-oauth-client.js.map +1 -1
- package/dist/modes/rpc/rpc-oauth-interaction.d.ts +1 -1
- package/dist/modes/rpc/rpc-oauth-interaction.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-oauth-interaction.js +1 -1
- package/dist/modes/rpc/rpc-oauth-interaction.js.map +1 -1
- package/dist/modes/rpc/rpc-provider-auth.d.ts +4 -5
- package/dist/modes/rpc/rpc-provider-auth.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-provider-auth.js +19 -66
- package/dist/modes/rpc/rpc-provider-auth.js.map +1 -1
- package/dist/modes/rpc/rpc-session-binding.js +1 -1
- package/dist/modes/rpc/rpc-session-binding.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +7 -4
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/package-manager-cli.d.ts.map +1 -1
- package/dist/package-manager-cli.js +5 -7
- package/dist/package-manager-cli.js.map +1 -1
- package/docs/changelog.mdx +1 -1
- package/docs/custom-provider.md +1 -1
- package/docs/models.md +4 -6
- package/docs/providers.md +13 -0
- package/docs/quickstart.md +2 -1
- package/docs/sdk.md +32 -49
- package/docs/workflows.md +3 -3
- package/examples/sdk/02-custom-model.ts +8 -10
- package/examples/sdk/09-api-keys-and-oauth.ts +17 -23
- package/examples/sdk/12-full-control.ts +9 -11
- package/examples/sdk/README.md +17 -21
- package/npm-shrinkwrap.json +23 -23
- package/package.json +2 -2
- package/dist/core/model-registry-auth.d.ts +0 -10
- package/dist/core/model-registry-auth.d.ts.map +0 -1
- package/dist/core/model-registry-auth.js +0 -98
- package/dist/core/model-registry-auth.js.map +0 -1
- package/dist/core/model-registry-builtins.d.ts +0 -8
- package/dist/core/model-registry-builtins.d.ts.map +0 -1
- package/dist/core/model-registry-builtins.js +0 -104
- package/dist/core/model-registry-builtins.js.map +0 -1
- package/dist/core/model-registry-custom-loader.d.ts +0 -3
- package/dist/core/model-registry-custom-loader.d.ts.map +0 -1
- package/dist/core/model-registry-custom-loader.js +0 -255
- package/dist/core/model-registry-custom-loader.js.map +0 -1
- package/dist/core/model-registry-dynamic.d.ts +0 -7
- package/dist/core/model-registry-dynamic.d.ts.map +0 -1
- package/dist/core/model-registry-dynamic.js +0 -189
- package/dist/core/model-registry-dynamic.js.map +0 -1
- package/dist/core/model-registry-extension-refresh.d.ts +0 -23
- package/dist/core/model-registry-extension-refresh.d.ts.map +0 -1
- package/dist/core/model-registry-extension-refresh.js +0 -32
- package/dist/core/model-registry-extension-refresh.js.map +0 -1
- package/dist/core/model-registry-loader.d.ts +0 -5
- package/dist/core/model-registry-loader.d.ts.map +0 -1
- package/dist/core/model-registry-loader.js +0 -29
- package/dist/core/model-registry-loader.js.map +0 -1
- package/dist/core/model-registry-schemas.d.ts +0 -1399
- package/dist/core/model-registry-schemas.d.ts.map +0 -1
- package/dist/core/model-registry-schemas.js.map +0 -1
- package/dist/core/model-registry-types.d.ts +0 -91
- package/dist/core/model-registry-types.d.ts.map +0 -1
- package/dist/core/model-registry-types.js +0 -2
- package/dist/core/model-registry-types.js.map +0 -1
- package/dist/core/model-registry-validation.d.ts +0 -7
- package/dist/core/model-registry-validation.d.ts.map +0 -1
- package/dist/core/model-registry-validation.js +0 -12
- package/dist/core/model-registry-validation.js.map +0 -1
- package/dist/core/oauth-compat.d.ts +0 -17
- package/dist/core/oauth-compat.d.ts.map +0 -1
- package/dist/core/oauth-compat.js +0 -2
- package/dist/core/oauth-compat.js.map +0 -1
- package/dist/core/oauth-provider-bridge.d.ts +0 -63
- package/dist/core/oauth-provider-bridge.d.ts.map +0 -1
- package/dist/core/oauth-provider-bridge.js +0 -232
- package/dist/core/oauth-provider-bridge.js.map +0 -1
|
@@ -6,23 +6,14 @@ export class InMemoryCodingAgentModelsStore {
|
|
|
6
6
|
this.entries = new Map();
|
|
7
7
|
}
|
|
8
8
|
async read(providerId) {
|
|
9
|
-
|
|
10
|
-
return entry === undefined ? undefined : structuredClone(entry);
|
|
9
|
+
return this.entries.get(providerId);
|
|
11
10
|
}
|
|
12
11
|
async write(providerId, entry) {
|
|
13
|
-
this.entries.set(providerId,
|
|
12
|
+
this.entries.set(providerId, entry);
|
|
14
13
|
}
|
|
15
14
|
async delete(providerId) {
|
|
16
15
|
this.entries.delete(providerId);
|
|
17
16
|
}
|
|
18
|
-
async writeIf(providerId, entry, predicate) {
|
|
19
|
-
if (predicate())
|
|
20
|
-
await this.write(providerId, entry);
|
|
21
|
-
}
|
|
22
|
-
async deleteIf(providerId, predicate) {
|
|
23
|
-
if (predicate())
|
|
24
|
-
await this.delete(providerId);
|
|
25
|
-
}
|
|
26
17
|
}
|
|
27
18
|
/** Locked JSON-backed storage for dynamically refreshed provider catalogs. */
|
|
28
19
|
export class FileModelsStore {
|
|
@@ -51,23 +42,5 @@ export class FileModelsStore {
|
|
|
51
42
|
return { result: undefined, next: JSON.stringify(current, null, 2) };
|
|
52
43
|
});
|
|
53
44
|
}
|
|
54
|
-
async writeIf(providerId, entry, predicate) {
|
|
55
|
-
await this.storage.withLockAsync(async (content) => {
|
|
56
|
-
if (!predicate())
|
|
57
|
-
return { result: undefined };
|
|
58
|
-
const current = this.parse(content);
|
|
59
|
-
current[providerId] = structuredClone(entry);
|
|
60
|
-
return { result: undefined, next: JSON.stringify(current, null, 2) };
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
async deleteIf(providerId, predicate) {
|
|
64
|
-
await this.storage.withLockAsync(async (content) => {
|
|
65
|
-
if (!predicate())
|
|
66
|
-
return { result: undefined };
|
|
67
|
-
const current = this.parse(content);
|
|
68
|
-
delete current[providerId];
|
|
69
|
-
return { result: undefined, next: JSON.stringify(current, null, 2) };
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
45
|
}
|
|
73
46
|
//# sourceMappingURL=models-store.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"models-store.js","sourceRoot":"","sources":["../../src/core/models-store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"models-store.js","sourceRoot":"","sources":["../../src/core/models-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAA2B,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAI7F,MAAM,OAAO,8BAA8B;IAA3C;QACkB,YAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;IAahE,CAAC;IAXA,KAAK,CAAC,IA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,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;QACtE,CAAC,CAAC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,UAAkB;QAC9B,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;YAClD,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACpC,OAAO,OAAO,CAAC,UAAU,CAAC,CAAC;YAC3B,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;QACtE,CAAC,CAAC,CAAC;IACJ,CAAC;CACD","sourcesContent":["import { join } from \"node:path\";\nimport type { ModelsStore, ModelsStoreEntry } from \"@earendil-works/pi-ai\";\nimport { getAgentDir } from \"../config.ts\";\nimport { type AuthStorageBackend, FileAuthStorageBackend } from \"./auth-storage-backends.ts\";\n\ntype StoredModels = Record<string, ModelsStoreEntry>;\n\nexport class InMemoryCodingAgentModelsStore implements ModelsStore {\n\tprivate readonly entries = new Map<string, ModelsStoreEntry>();\n\n\tasync read(providerId: string): Promise<ModelsStoreEntry | undefined> {\n\t\treturn this.entries.get(providerId);\n\t}\n\n\tasync write(providerId: string, entry: ModelsStoreEntry): Promise<void> {\n\t\tthis.entries.set(providerId, entry);\n\t}\n\n\tasync delete(providerId: string): Promise<void> {\n\t\tthis.entries.delete(providerId);\n\t}\n}\n\n/** Locked JSON-backed storage for dynamically refreshed provider catalogs. */\nexport class FileModelsStore implements ModelsStore {\n\tprivate readonly storage: AuthStorageBackend;\n\n\tconstructor(path: string = join(getAgentDir(), \"models-store.json\")) {\n\t\tthis.storage = new FileAuthStorageBackend(path);\n\t}\n\n\tprivate parse(content: string | undefined): StoredModels {\n\t\treturn content ? (JSON.parse(content) as StoredModels) : {};\n\t}\n\n\tasync read(providerId: string): Promise<ModelsStoreEntry | undefined> {\n\t\treturn this.storage.withLock((content) => ({\n\t\t\tresult: structuredClone(this.parse(content)[providerId]),\n\t\t}));\n\t}\n\n\tasync write(providerId: string, entry: ModelsStoreEntry): Promise<void> {\n\t\tawait this.storage.withLockAsync(async (content) => {\n\t\t\tconst current = this.parse(content);\n\t\t\tcurrent[providerId] = structuredClone(entry);\n\t\t\treturn { result: undefined, next: JSON.stringify(current, null, 2) };\n\t\t});\n\t}\n\n\tasync delete(providerId: string): Promise<void> {\n\t\tawait this.storage.withLockAsync(async (content) => {\n\t\t\tconst current = this.parse(content);\n\t\t\tdelete current[providerId];\n\t\t\treturn { result: undefined, next: JSON.stringify(current, null, 2) };\n\t\t});\n\t}\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AuthInfoLink, AuthInteraction, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
|
2
|
+
export interface AtomicOAuthLoginCallbacks extends OAuthLoginCallbacks {
|
|
3
|
+
onManualCodeCancel?(): void;
|
|
4
|
+
onInfo?(message: string, links: readonly AuthInfoLink[]): void;
|
|
5
|
+
}
|
|
6
|
+
/** JSON-safe provider-owned OAuth metadata transported across isolated-engine boundaries. */
|
|
7
|
+
export interface OAuthProviderMetadata {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
loginLabel?: string;
|
|
11
|
+
usesCallbackServer?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Marks failures after credential acquisition so presentation never mistakes nested AbortErrors for cancellation. */
|
|
14
|
+
export declare class OAuthLoginTransactionError extends Error {
|
|
15
|
+
constructor(cause: unknown);
|
|
16
|
+
}
|
|
17
|
+
export declare function isOAuthLoginCancelled(error: unknown, signal?: AbortSignal, options?: {
|
|
18
|
+
includeActiveSignal?: boolean;
|
|
19
|
+
}): boolean;
|
|
20
|
+
export declare function normalizeOAuthLoginError(error: unknown, signal?: AbortSignal, options?: {
|
|
21
|
+
includeActiveSignal?: boolean;
|
|
22
|
+
}): unknown;
|
|
23
|
+
export declare function createAuthInteraction(callbacks: AtomicOAuthLoginCallbacks): AuthInteraction;
|
|
24
|
+
//# sourceMappingURL=oauth-login.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-login.d.ts","sourceRoot":"","sources":["../../src/core/oauth-login.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAEhG,MAAM,WAAW,yBAA0B,SAAQ,mBAAmB;IACrE,kBAAkB,CAAC,IAAI,IAAI,CAAC;IAC5B,MAAM,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,YAAY,EAAE,GAAG,IAAI,CAAC;CAC/D;AAED,6FAA6F;AAC7F,MAAM,WAAW,qBAAqB;IACrC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,sHAAsH;AACtH,qBAAa,0BAA2B,SAAQ,KAAK;IACpD,YAAY,KAAK,EAAE,OAAO,EAGzB;CACD;AAED,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,WAAW,EACpB,OAAO,GAAE;IAAE,mBAAmB,CAAC,EAAE,OAAO,CAAA;CAAO,GAC7C,OAAO,CAiBT;AAED,wBAAgB,wBAAwB,CACvC,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,WAAW,EACpB,OAAO,CAAC,EAAE;IAAE,mBAAmB,CAAC,EAAE,OAAO,CAAA;CAAE,GACzC,OAAO,CAIT;AAkBD,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,yBAAyB,GAAG,eAAe,CAsB3F"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Marks failures after credential acquisition so presentation never mistakes nested AbortErrors for cancellation. */
|
|
2
|
+
export class OAuthLoginTransactionError extends Error {
|
|
3
|
+
constructor(cause) {
|
|
4
|
+
super(cause instanceof Error ? cause.message : String(cause), { cause });
|
|
5
|
+
this.name = "OAuthLoginTransactionError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function isOAuthLoginCancelled(error, signal, options = {}) {
|
|
9
|
+
if (error instanceof OAuthLoginTransactionError)
|
|
10
|
+
return false;
|
|
11
|
+
if (options.includeActiveSignal !== false && signal?.aborted)
|
|
12
|
+
return true;
|
|
13
|
+
const reason = signal?.reason;
|
|
14
|
+
const seen = new Set();
|
|
15
|
+
let current = error;
|
|
16
|
+
while (current !== undefined && current !== null) {
|
|
17
|
+
if (current === reason && reason !== undefined)
|
|
18
|
+
return true;
|
|
19
|
+
if (current === "Login cancelled")
|
|
20
|
+
return true;
|
|
21
|
+
if (typeof current !== "object")
|
|
22
|
+
return false;
|
|
23
|
+
if (seen.has(current))
|
|
24
|
+
return false;
|
|
25
|
+
seen.add(current);
|
|
26
|
+
const candidate = current;
|
|
27
|
+
if (candidate.name === "AbortError" || candidate.message === "Login cancelled")
|
|
28
|
+
return true;
|
|
29
|
+
current = candidate.cause;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
export function normalizeOAuthLoginError(error, signal, options) {
|
|
34
|
+
if (error instanceof Error && error.message === "Login cancelled" && error.cause !== undefined)
|
|
35
|
+
return error;
|
|
36
|
+
if (!isOAuthLoginCancelled(error, signal, options))
|
|
37
|
+
return error;
|
|
38
|
+
return new Error("Login cancelled", { cause: error });
|
|
39
|
+
}
|
|
40
|
+
function abortable(promise, signal, onAbort) {
|
|
41
|
+
if (!signal)
|
|
42
|
+
return promise;
|
|
43
|
+
if (signal.aborted) {
|
|
44
|
+
onAbort?.();
|
|
45
|
+
return Promise.reject(signal.reason ?? new Error("Authentication prompt cancelled"));
|
|
46
|
+
}
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
const abort = () => {
|
|
49
|
+
onAbort?.();
|
|
50
|
+
reject(signal.reason ?? new Error("Authentication prompt cancelled"));
|
|
51
|
+
};
|
|
52
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
53
|
+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export function createAuthInteraction(callbacks) {
|
|
57
|
+
return {
|
|
58
|
+
signal: callbacks.signal,
|
|
59
|
+
prompt: async (prompt) => {
|
|
60
|
+
switch (prompt.type) {
|
|
61
|
+
case "select":
|
|
62
|
+
return (await callbacks.onSelect({ message: prompt.message, options: prompt.options.map(({ id, label }) => ({ id, label })) })) ?? "";
|
|
63
|
+
case "manual_code":
|
|
64
|
+
return abortable(callbacks.onManualCodeInput ? callbacks.onManualCodeInput() : callbacks.onPrompt({ message: prompt.message, placeholder: prompt.placeholder }), prompt.signal, callbacks.onManualCodeCancel);
|
|
65
|
+
default:
|
|
66
|
+
return callbacks.onPrompt({ message: prompt.message, placeholder: prompt.placeholder });
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
notify: (event) => {
|
|
70
|
+
switch (event.type) {
|
|
71
|
+
case "auth_url":
|
|
72
|
+
callbacks.onAuth({ url: event.url, instructions: event.instructions });
|
|
73
|
+
break;
|
|
74
|
+
case "device_code":
|
|
75
|
+
callbacks.onDeviceCode(event);
|
|
76
|
+
break;
|
|
77
|
+
case "progress":
|
|
78
|
+
callbacks.onProgress?.(event.message);
|
|
79
|
+
break;
|
|
80
|
+
case "info":
|
|
81
|
+
callbacks.onInfo?.(event.message, event.links ?? []);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=oauth-login.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-login.js","sourceRoot":"","sources":["../../src/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AE,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;oBAAC,MAAM;gBACzD,KAAK,UAAU;oBAAE,SAAS,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;oBAAC,MAAM;gBAC9D,KAAK,MAAM;oBAAE,SAAS,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;oBAAC,MAAM;YAC1E,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { AuthInfoLink, AuthInteraction, OAuthLoginCallbacks } from \"@earendil-works/pi-ai\";\n\nexport interface AtomicOAuthLoginCallbacks extends OAuthLoginCallbacks {\n\tonManualCodeCancel?(): void;\n\tonInfo?(message: string, links: readonly AuthInfoLink[]): void;\n}\n\n/** JSON-safe provider-owned OAuth metadata transported across isolated-engine boundaries. */\nexport interface OAuthProviderMetadata {\n\tid: string;\n\tname: string;\n\tloginLabel?: string;\n\tusesCallbackServer?: boolean;\n}\n\n/** Marks failures after credential acquisition so presentation never mistakes nested AbortErrors for cancellation. */\nexport class OAuthLoginTransactionError extends Error {\n\tconstructor(cause: unknown) {\n\t\tsuper(cause instanceof Error ? cause.message : String(cause), { cause });\n\t\tthis.name = \"OAuthLoginTransactionError\";\n\t}\n}\n\nexport function isOAuthLoginCancelled(\n\terror: unknown,\n\tsignal?: AbortSignal,\n\toptions: { includeActiveSignal?: boolean } = {},\n): boolean {\n\tif (error instanceof OAuthLoginTransactionError) return false;\n\tif (options.includeActiveSignal !== false && signal?.aborted) return true;\n\tconst reason = signal?.reason;\n\tconst seen = new Set<object>();\n\tlet current: unknown = error;\n\twhile (current !== undefined && current !== null) {\n\t\tif (current === reason && reason !== undefined) return true;\n\t\tif (current === \"Login cancelled\") return true;\n\t\tif (typeof current !== \"object\") return false;\n\t\tif (seen.has(current)) return false;\n\t\tseen.add(current);\n\t\tconst candidate = current as { name?: unknown; message?: unknown; cause?: unknown };\n\t\tif (candidate.name === \"AbortError\" || candidate.message === \"Login cancelled\") return true;\n\t\tcurrent = candidate.cause;\n\t}\n\treturn false;\n}\n\nexport function normalizeOAuthLoginError(\n\terror: unknown,\n\tsignal?: AbortSignal,\n\toptions?: { includeActiveSignal?: boolean },\n): unknown {\n\tif (error instanceof Error && error.message === \"Login cancelled\" && error.cause !== undefined) return error;\n\tif (!isOAuthLoginCancelled(error, signal, options)) return error;\n\treturn new Error(\"Login cancelled\", { cause: error });\n}\n\nfunction abortable<T>(promise: Promise<T>, signal: AbortSignal | undefined, onAbort?: () => void): Promise<T> {\n\tif (!signal) return promise;\n\tif (signal.aborted) {\n\t\tonAbort?.();\n\t\treturn Promise.reject(signal.reason ?? new Error(\"Authentication prompt cancelled\"));\n\t}\n\treturn new Promise<T>((resolve, reject) => {\n\t\tconst abort = () => {\n\t\t\tonAbort?.();\n\t\t\treject(signal.reason ?? new Error(\"Authentication prompt cancelled\"));\n\t\t};\n\t\tsignal.addEventListener(\"abort\", abort, { once: true });\n\t\tpromise.then(resolve, reject).finally(() => signal.removeEventListener(\"abort\", abort));\n\t});\n}\n\nexport function createAuthInteraction(callbacks: AtomicOAuthLoginCallbacks): AuthInteraction {\n\treturn {\n\t\tsignal: callbacks.signal,\n\t\tprompt: async (prompt) => {\n\t\t\tswitch (prompt.type) {\n\t\t\t\tcase \"select\":\n\t\t\t\t\treturn (await callbacks.onSelect({ message: prompt.message, options: prompt.options.map(({ id, label }) => ({ id, label })) })) ?? \"\";\n\t\t\t\tcase \"manual_code\":\n\t\t\t\t\treturn abortable(callbacks.onManualCodeInput ? callbacks.onManualCodeInput() : callbacks.onPrompt({ message: prompt.message, placeholder: prompt.placeholder }), prompt.signal, callbacks.onManualCodeCancel);\n\t\t\t\tdefault:\n\t\t\t\t\treturn callbacks.onPrompt({ message: prompt.message, placeholder: prompt.placeholder });\n\t\t\t}\n\t\t},\n\t\tnotify: (event) => {\n\t\t\tswitch (event.type) {\n\t\t\t\tcase \"auth_url\": callbacks.onAuth({ url: event.url, instructions: event.instructions }); break;\n\t\t\t\tcase \"device_code\": callbacks.onDeviceCode(event); break;\n\t\t\t\tcase \"progress\": callbacks.onProgress?.(event.message); break;\n\t\t\t\tcase \"info\": callbacks.onInfo?.(event.message, event.links ?? []); break;\n\t\t\t}\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Provider } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { OAuthProviderMetadata } from "./oauth-login.ts";
|
|
3
|
+
import type { ProviderConfigInput } from "./provider-composer.ts";
|
|
4
|
+
export declare function collectOAuthProviderMetadata(providers: readonly Provider[], extensions: ReadonlyMap<string, ProviderConfigInput>): OAuthProviderMetadata[];
|
|
5
|
+
//# sourceMappingURL=oauth-provider-metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-provider-metadata.d.ts","sourceRoot":"","sources":["../../src/core/oauth-provider-metadata.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAIlE,wBAAgB,4BAA4B,CAC3C,SAAS,EAAE,SAAS,QAAQ,EAAE,EAC9B,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,mBAAmB,CAAC,GAClD,qBAAqB,EAAE,CAgBzB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const CALLBACK_SERVER_PROVIDERS = new Set(["anthropic", "openai-codex"]);
|
|
2
|
+
export function collectOAuthProviderMetadata(providers, extensions) {
|
|
3
|
+
return providers.filter((provider) => provider.auth.oauth).map((provider) => {
|
|
4
|
+
const providerOAuth = provider.auth.oauth;
|
|
5
|
+
const extensionOAuth = extensions.get(provider.id)?.oauth;
|
|
6
|
+
const loginLabel = extensionOAuth?.loginLabel ?? providerOAuth?.loginLabel;
|
|
7
|
+
const hasCallbackServerMetadata = extensionOAuth?.usesCallbackServer !== undefined
|
|
8
|
+
|| CALLBACK_SERVER_PROVIDERS.has(provider.id);
|
|
9
|
+
const usesCallbackServer = extensionOAuth?.usesCallbackServer
|
|
10
|
+
?? CALLBACK_SERVER_PROVIDERS.has(provider.id);
|
|
11
|
+
return {
|
|
12
|
+
id: provider.id,
|
|
13
|
+
name: provider.name ?? provider.id,
|
|
14
|
+
...(loginLabel ? { loginLabel } : {}),
|
|
15
|
+
...(hasCallbackServerMetadata ? { usesCallbackServer } : {}),
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=oauth-provider-metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-provider-metadata.js","sourceRoot":"","sources":["../../src/core/oauth-provider-metadata.ts"],"names":[],"mappings":"AAIA,MAAM,yBAAyB,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,cAAc,CAAC,CAAC,CAAC;AAEzE,MAAM,UAAU,4BAA4B,CAC3C,SAA8B,EAC9B,UAAoD;IAEpD,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC3E,MAAM,aAAa,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC;QAC1C,MAAM,cAAc,GAAG,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC;QAC1D,MAAM,UAAU,GAAG,cAAc,EAAE,UAAU,IAAI,aAAa,EAAE,UAAU,CAAC;QAC3E,MAAM,yBAAyB,GAAG,cAAc,EAAE,kBAAkB,KAAK,SAAS;eAC9E,yBAAyB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC/C,MAAM,kBAAkB,GAAG,cAAc,EAAE,kBAAkB;eACzD,yBAAyB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC/C,OAAO;YACN,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,IAAI,EAAE,QAAQ,CAAC,IAAI,IAAI,QAAQ,CAAC,EAAE;YAClC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACrC,GAAG,CAAC,yBAAyB,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC5D,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { Provider } from \"@earendil-works/pi-ai\";\nimport type { OAuthProviderMetadata } from \"./oauth-login.ts\";\nimport type { ProviderConfigInput } from \"./provider-composer.ts\";\n\nconst CALLBACK_SERVER_PROVIDERS = new Set([\"anthropic\", \"openai-codex\"]);\n\nexport function collectOAuthProviderMetadata(\n\tproviders: readonly Provider[],\n\textensions: ReadonlyMap<string, ProviderConfigInput>,\n): OAuthProviderMetadata[] {\n\treturn providers.filter((provider) => provider.auth.oauth).map((provider) => {\n\t\tconst providerOAuth = provider.auth.oauth;\n\t\tconst extensionOAuth = extensions.get(provider.id)?.oauth;\n\t\tconst loginLabel = extensionOAuth?.loginLabel ?? providerOAuth?.loginLabel;\n\t\tconst hasCallbackServerMetadata = extensionOAuth?.usesCallbackServer !== undefined\n\t\t\t|| CALLBACK_SERVER_PROVIDERS.has(provider.id);\n\t\tconst usesCallbackServer = extensionOAuth?.usesCallbackServer\n\t\t\t?? CALLBACK_SERVER_PROVIDERS.has(provider.id);\n\t\treturn {\n\t\t\tid: provider.id,\n\t\t\tname: provider.name ?? provider.id,\n\t\t\t...(loginLabel ? { loginLabel } : {}),\n\t\t\t...(hasCallbackServerMetadata ? { usesCallbackServer } : {}),\n\t\t};\n\t});\n}\n"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type Api, type ApiKeyAuth, type AssistantMessageEventStream, type Context, type Model, type OAuthAuth, type OAuthCredentials, type OAuthLoginCallbacks, type Provider, type RefreshModelsContext, type SimpleStreamOptions } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ModelsJsonModelOverride, ModelsJsonProvider } from "./model-config.ts";
|
|
3
|
+
import { clearConfigValueCache } from "./resolve-config-value.ts";
|
|
4
|
+
export interface ExtensionOAuthConfig {
|
|
5
|
+
name: string;
|
|
6
|
+
loginLabel?: string;
|
|
7
|
+
usesCallbackServer?: boolean;
|
|
8
|
+
login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials>;
|
|
9
|
+
refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials>;
|
|
10
|
+
getApiKey(credentials: OAuthCredentials): string;
|
|
11
|
+
modifyModels?(models: Model<Api>[], credentials: OAuthCredentials): Model<Api>[];
|
|
12
|
+
}
|
|
13
|
+
/** Input type for the extension registerProvider API. */
|
|
14
|
+
export interface ProviderConfigInput {
|
|
15
|
+
name?: string;
|
|
16
|
+
baseUrl?: string;
|
|
17
|
+
apiKey?: string;
|
|
18
|
+
api?: Api;
|
|
19
|
+
streamSimple?: (model: Model<Api>, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream;
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
authHeader?: boolean;
|
|
22
|
+
oauth?: ExtensionOAuthConfig;
|
|
23
|
+
models?: Array<{
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
api?: Api;
|
|
27
|
+
baseUrl?: string;
|
|
28
|
+
reasoning: boolean;
|
|
29
|
+
thinkingLevelMap?: Model<Api>["thinkingLevelMap"];
|
|
30
|
+
input: ("text" | "image")[];
|
|
31
|
+
cost: Model<Api>["cost"];
|
|
32
|
+
contextWindow: number;
|
|
33
|
+
maxTokens: number;
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
compat?: Model<Api>["compat"];
|
|
36
|
+
}>;
|
|
37
|
+
refreshModels?(context: RefreshModelsContext): Promise<NonNullable<ProviderConfigInput["models"]>>;
|
|
38
|
+
}
|
|
39
|
+
export type AuthStatus = {
|
|
40
|
+
configured: boolean;
|
|
41
|
+
source?: "stored" | "runtime" | "environment" | "fallback" | "models_json_key" | "models_json_command";
|
|
42
|
+
label?: string;
|
|
43
|
+
};
|
|
44
|
+
export declare const clearApiKeyCache: typeof clearConfigValueCache;
|
|
45
|
+
export declare function applyModelOverride(model: Model<Api>, override: ModelsJsonModelOverride): Model<Api>;
|
|
46
|
+
export declare function applyModelsJson(providerId: string, baseModels: readonly Model<Api>[], config: ModelsJsonProvider | undefined): Model<Api>[];
|
|
47
|
+
export declare function applyExtension(providerId: string, models: readonly Model<Api>[], config: ProviderConfigInput | undefined): Model<Api>[];
|
|
48
|
+
export declare function configuredApiKey(config: ModelsJsonProvider | undefined, extension: ProviderConfigInput | undefined): string | undefined;
|
|
49
|
+
export declare function configuredHeaders(config: ModelsJsonProvider | undefined, extension: ProviderConfigInput | undefined): Record<string, string> | undefined;
|
|
50
|
+
export declare function composeApiKeyAuth(providerId: string, base: Provider | undefined, config: ModelsJsonProvider | undefined, extension: ProviderConfigInput | undefined): ApiKeyAuth | undefined;
|
|
51
|
+
export declare function composeOAuthAuth(providerId: string, base: Provider | undefined, config: ModelsJsonProvider | undefined, extension: ProviderConfigInput | undefined): OAuthAuth | undefined;
|
|
52
|
+
export declare function rawModelHeaders(model: Model<Api>, config: ModelsJsonProvider | undefined, extension: ProviderConfigInput | undefined): Record<string, string> | undefined;
|
|
53
|
+
//# sourceMappingURL=provider-composer-internal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-composer-internal.d.ts","sourceRoot":"","sources":["../../src/core/provider-composer-internal.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,GAAG,EACR,KAAK,UAAU,EACf,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figValueCache, getConfigValueEnvVarNames, isCommandConfigValue, resolveConfigValueOrThrow, resolveHeadersOrThrow, } from "./resolve-config-value.js";
|
|
2
|
+
export const clearApiKeyCache = clearConfigValueCache;
|
|
3
|
+
function mergeCompat(base, override) {
|
|
4
|
+
if (!override)
|
|
5
|
+
return base;
|
|
6
|
+
const merged = { ...base, ...override };
|
|
7
|
+
const baseNested = base;
|
|
8
|
+
const overrideNested = override;
|
|
9
|
+
const mergedNested = merged;
|
|
10
|
+
for (const key of ["openRouterRouting", "vercelGatewayRouting", "chatTemplateKwargs"]) {
|
|
11
|
+
const baseValue = baseNested?.[key];
|
|
12
|
+
const overrideValue = overrideNested[key];
|
|
13
|
+
if ((typeof baseValue === "object" && baseValue !== null) ||
|
|
14
|
+
(typeof overrideValue === "object" && overrideValue !== null)) {
|
|
15
|
+
mergedNested[key] = { ...baseValue, ...overrideValue };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return merged;
|
|
19
|
+
}
|
|
20
|
+
export function applyModelOverride(model, override) {
|
|
21
|
+
return {
|
|
22
|
+
...model,
|
|
23
|
+
name: override.name ?? model.name,
|
|
24
|
+
reasoning: override.reasoning ?? model.reasoning,
|
|
25
|
+
thinkingLevelMap: override.thinkingLevelMap
|
|
26
|
+
? { ...model.thinkingLevelMap, ...override.thinkingLevelMap }
|
|
27
|
+
: model.thinkingLevelMap,
|
|
28
|
+
input: override.input ?? model.input,
|
|
29
|
+
cost: override.cost
|
|
30
|
+
? {
|
|
31
|
+
input: override.cost.input ?? model.cost.input,
|
|
32
|
+
output: override.cost.output ?? model.cost.output,
|
|
33
|
+
cacheRead: override.cost.cacheRead ?? model.cost.cacheRead,
|
|
34
|
+
cacheWrite: override.cost.cacheWrite ?? model.cost.cacheWrite,
|
|
35
|
+
tiers: override.cost.tiers ?? model.cost.tiers,
|
|
36
|
+
}
|
|
37
|
+
: model.cost,
|
|
38
|
+
contextWindow: override.contextWindow ?? model.contextWindow,
|
|
39
|
+
maxTokens: override.maxTokens ?? model.maxTokens,
|
|
40
|
+
compat: mergeCompat(model.compat, override.compat),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function modelFromJson(providerId, definition, providerConfig, defaults) {
|
|
44
|
+
const api = definition.api ?? providerConfig.api ?? defaults?.api;
|
|
45
|
+
if (!api) {
|
|
46
|
+
throw new Error(`Provider ${providerId}, model ${definition.id}: no "api" specified. Set at provider or model level.`);
|
|
47
|
+
}
|
|
48
|
+
const baseUrl = definition.baseUrl ?? providerConfig.baseUrl ?? defaults?.baseUrl;
|
|
49
|
+
if (!baseUrl)
|
|
50
|
+
throw new Error(`Provider ${providerId}: "baseUrl" is required when defining custom models.`);
|
|
51
|
+
if (definition.contextWindow !== undefined && definition.contextWindow <= 0) {
|
|
52
|
+
throw new Error(`Provider ${providerId}, model ${definition.id}: invalid contextWindow`);
|
|
53
|
+
}
|
|
54
|
+
if (definition.maxTokens !== undefined && definition.maxTokens <= 0) {
|
|
55
|
+
throw new Error(`Provider ${providerId}, model ${definition.id}: invalid maxTokens`);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
id: definition.id,
|
|
59
|
+
name: definition.name ?? definition.id,
|
|
60
|
+
api: api,
|
|
61
|
+
provider: providerId,
|
|
62
|
+
baseUrl,
|
|
63
|
+
reasoning: definition.reasoning ?? false,
|
|
64
|
+
thinkingLevelMap: definition.thinkingLevelMap,
|
|
65
|
+
input: (definition.input ?? ["text"]),
|
|
66
|
+
cost: definition.cost ?? { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
67
|
+
contextWindow: definition.contextWindow ?? 128000,
|
|
68
|
+
maxTokens: definition.maxTokens ?? 16384,
|
|
69
|
+
headers: undefined,
|
|
70
|
+
compat: mergeCompat(providerConfig.compat, definition.compat),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export function applyModelsJson(providerId, baseModels, config) {
|
|
74
|
+
if (!config)
|
|
75
|
+
return [...baseModels];
|
|
76
|
+
if (config.oauth && !config.baseUrl) {
|
|
77
|
+
throw new Error(`Provider ${providerId}: "baseUrl" is required when "oauth" is set.`);
|
|
78
|
+
}
|
|
79
|
+
const hasOverrides = config.modelOverrides && Object.keys(config.modelOverrides).length > 0;
|
|
80
|
+
if (!config.models?.length &&
|
|
81
|
+
!config.baseUrl &&
|
|
82
|
+
!config.headers &&
|
|
83
|
+
!config.compat &&
|
|
84
|
+
!hasOverrides &&
|
|
85
|
+
!config.apiKey &&
|
|
86
|
+
!config.oauth &&
|
|
87
|
+
config.authHeader === undefined) {
|
|
88
|
+
throw new Error(`Provider ${providerId}: must specify "baseUrl", "headers", "compat", "modelOverrides", or "models".`);
|
|
89
|
+
}
|
|
90
|
+
const models = baseModels.map((model) => ({
|
|
91
|
+
...model,
|
|
92
|
+
baseUrl: config.oauth === "radius" ? model.baseUrl : (config.baseUrl ?? model.baseUrl),
|
|
93
|
+
compat: mergeCompat(model.compat, config.compat),
|
|
94
|
+
}));
|
|
95
|
+
for (const definition of config.models ?? []) {
|
|
96
|
+
const existingIndex = models.findIndex((model) => model.id === definition.id);
|
|
97
|
+
const defaults = existingIndex >= 0 ? models[existingIndex] : models[0];
|
|
98
|
+
const model = modelFromJson(providerId, definition, config, defaults);
|
|
99
|
+
if (existingIndex >= 0)
|
|
100
|
+
models[existingIndex] = model;
|
|
101
|
+
else
|
|
102
|
+
models.push(model);
|
|
103
|
+
}
|
|
104
|
+
return models;
|
|
105
|
+
}
|
|
106
|
+
export function applyExtension(providerId, models, config) {
|
|
107
|
+
if (!config)
|
|
108
|
+
return [...models];
|
|
109
|
+
if (!config.models) {
|
|
110
|
+
return config.baseUrl ? models.map((model) => ({ ...model, baseUrl: config.baseUrl })) : [...models];
|
|
111
|
+
}
|
|
112
|
+
return config.models.map((definition) => {
|
|
113
|
+
const defaults = models.find((model) => model.id === definition.id) ?? models[0];
|
|
114
|
+
const api = definition.api ?? config.api ?? defaults?.api;
|
|
115
|
+
if (!api) {
|
|
116
|
+
throw new Error(`Provider ${providerId}, model ${definition.id}: no "api" specified. Set at provider or model level.`);
|
|
117
|
+
}
|
|
118
|
+
const baseUrl = definition.baseUrl ?? config.baseUrl ?? defaults?.baseUrl;
|
|
119
|
+
if (!baseUrl)
|
|
120
|
+
throw new Error(`Provider ${providerId}: "baseUrl" is required when defining custom models.`);
|
|
121
|
+
return {
|
|
122
|
+
...definition,
|
|
123
|
+
api,
|
|
124
|
+
provider: providerId,
|
|
125
|
+
baseUrl,
|
|
126
|
+
headers: undefined,
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function adaptOAuth(config) {
|
|
131
|
+
return {
|
|
132
|
+
name: config.name,
|
|
133
|
+
login: async (callbacks) => {
|
|
134
|
+
const legacyCallbacks = {
|
|
135
|
+
onAuth: (info) => callbacks.notify({ type: "auth_url", ...info }),
|
|
136
|
+
onDeviceCode: (info) => callbacks.notify({ type: "device_code", ...info }),
|
|
137
|
+
onPrompt: (prompt) => callbacks.prompt({ type: "text", ...prompt }),
|
|
138
|
+
onProgress: (message) => callbacks.notify({ type: "progress", message }),
|
|
139
|
+
onInfo: (message, links) => callbacks.notify({ type: "info", message, links }),
|
|
140
|
+
onManualCodeInput: () => callbacks.prompt({ type: "manual_code", message: "Paste the authorization code" }),
|
|
141
|
+
onSelect: (prompt) => callbacks.prompt({ type: "select", ...prompt }),
|
|
142
|
+
signal: callbacks.signal,
|
|
143
|
+
};
|
|
144
|
+
const credential = await config.login(legacyCallbacks);
|
|
145
|
+
return { ...credential, type: "oauth" };
|
|
146
|
+
},
|
|
147
|
+
refresh: async (credential) => ({ ...(await config.refreshToken(credential)), type: "oauth" }),
|
|
148
|
+
toAuth: async (credential) => ({ apiKey: config.getApiKey(credential) }),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function withConfiguredAuth(auth, headers, authHeader) {
|
|
152
|
+
let mergedHeaders = auth.headers || headers ? { ...auth.headers, ...headers } : undefined;
|
|
153
|
+
if (authHeader) {
|
|
154
|
+
if (!auth.apiKey)
|
|
155
|
+
throw new Error("authHeader requires a resolved API key");
|
|
156
|
+
mergedHeaders = { ...mergedHeaders, Authorization: `Bearer ${auth.apiKey}` };
|
|
157
|
+
}
|
|
158
|
+
return { ...auth, headers: mergedHeaders };
|
|
159
|
+
}
|
|
160
|
+
export function configuredApiKey(config, extension) {
|
|
161
|
+
return extension?.apiKey ?? config?.apiKey;
|
|
162
|
+
}
|
|
163
|
+
export function configuredHeaders(config, extension) {
|
|
164
|
+
if (!config?.headers && !extension?.headers)
|
|
165
|
+
return undefined;
|
|
166
|
+
return { ...config?.headers, ...extension?.headers };
|
|
167
|
+
}
|
|
168
|
+
async function configContextEnv(values, ctx, explicit) {
|
|
169
|
+
const env = { ...explicit };
|
|
170
|
+
for (const name of new Set(values.flatMap(getConfigValueEnvVarNames))) {
|
|
171
|
+
if (env[name] !== undefined)
|
|
172
|
+
continue;
|
|
173
|
+
const value = await ctx.env(name);
|
|
174
|
+
if (value !== undefined)
|
|
175
|
+
env[name] = value;
|
|
176
|
+
}
|
|
177
|
+
return Object.keys(env).length > 0 ? env : undefined;
|
|
178
|
+
}
|
|
179
|
+
export function composeApiKeyAuth(providerId, base, config, extension) {
|
|
180
|
+
const inherited = base?.auth.apiKey;
|
|
181
|
+
const rawKey = configuredApiKey(config, extension);
|
|
182
|
+
const oauth = extension?.oauth ?? base?.auth.oauth;
|
|
183
|
+
// OAuth-only providers get no fabricated API-key login method.
|
|
184
|
+
if (!inherited && rawKey === undefined && oauth)
|
|
185
|
+
return undefined;
|
|
186
|
+
const rawHeaders = configuredHeaders(config, extension);
|
|
187
|
+
const authHeader = extension?.authHeader ?? config?.authHeader ?? false;
|
|
188
|
+
return {
|
|
189
|
+
name: inherited?.name ?? "API key",
|
|
190
|
+
login: inherited?.login ??
|
|
191
|
+
(async (interaction) => ({
|
|
192
|
+
type: "api_key",
|
|
193
|
+
key: await interaction.prompt({ type: "secret", message: "Enter API key" }),
|
|
194
|
+
})),
|
|
195
|
+
check: async (input) => {
|
|
196
|
+
if (input.credential) {
|
|
197
|
+
if (inherited?.check)
|
|
198
|
+
return inherited.check(input);
|
|
199
|
+
if (input.credential.key)
|
|
200
|
+
return { type: "api_key", source: "stored credential" };
|
|
201
|
+
const resolved = await inherited?.resolve(input);
|
|
202
|
+
return resolved ? { type: "api_key", source: resolved.source } : undefined;
|
|
203
|
+
}
|
|
204
|
+
if (rawKey !== undefined) {
|
|
205
|
+
if (isCommandConfigValue(rawKey))
|
|
206
|
+
return { type: "api_key", source: "configured API key" };
|
|
207
|
+
const envNames = getConfigValueEnvVarNames(rawKey);
|
|
208
|
+
for (const name of envNames) {
|
|
209
|
+
if ((await input.ctx.env(name)) === undefined)
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
return { type: "api_key", source: "configured API key" };
|
|
213
|
+
}
|
|
214
|
+
if (inherited?.check)
|
|
215
|
+
return inherited.check(input);
|
|
216
|
+
const resolved = await inherited?.resolve(input);
|
|
217
|
+
return resolved ? { type: "api_key", source: resolved.source } : undefined;
|
|
218
|
+
},
|
|
219
|
+
resolve: async (input) => {
|
|
220
|
+
let result;
|
|
221
|
+
if (input.credential) {
|
|
222
|
+
result = inherited
|
|
223
|
+
? await inherited.resolve(input)
|
|
224
|
+
: input.credential.key
|
|
225
|
+
? { auth: { apiKey: input.credential.key }, env: input.credential.env, source: "stored credential" }
|
|
226
|
+
: undefined;
|
|
227
|
+
}
|
|
228
|
+
else if (rawKey !== undefined) {
|
|
229
|
+
const env = await configContextEnv([rawKey], input.ctx);
|
|
230
|
+
const key = resolveConfigValueOrThrow(rawKey, `API key for provider "${providerId}"`, env);
|
|
231
|
+
result = inherited
|
|
232
|
+
? await inherited.resolve({ ...input, credential: { type: "api_key", key } })
|
|
233
|
+
: { auth: { apiKey: key }, source: "configured API key" };
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
result = await inherited?.resolve(input);
|
|
237
|
+
}
|
|
238
|
+
if (!result)
|
|
239
|
+
return undefined;
|
|
240
|
+
const explicitEnv = { ...(input.credential?.env ?? {}), ...(result.env ?? {}) };
|
|
241
|
+
const headerEnv = await configContextEnv(Object.values(rawHeaders ?? {}), input.ctx, explicitEnv);
|
|
242
|
+
const headers = resolveHeadersOrThrow(rawHeaders, `provider "${providerId}"`, headerEnv);
|
|
243
|
+
return { ...result, auth: withConfiguredAuth(result.auth, headers, authHeader) };
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
export function composeOAuthAuth(providerId, base, config, extension) {
|
|
248
|
+
const oauth = extension?.oauth ? adaptOAuth(extension.oauth) : base?.auth.oauth;
|
|
249
|
+
if (!oauth)
|
|
250
|
+
return undefined;
|
|
251
|
+
const rawHeaders = configuredHeaders(config, extension);
|
|
252
|
+
const authHeader = extension?.authHeader ?? config?.authHeader ?? false;
|
|
253
|
+
return {
|
|
254
|
+
...oauth,
|
|
255
|
+
toAuth: async (credential) => {
|
|
256
|
+
const auth = await oauth.toAuth(credential);
|
|
257
|
+
const env = credential.env;
|
|
258
|
+
const headers = resolveHeadersOrThrow(rawHeaders, `provider "${providerId}"`, typeof env === "object" && env !== null ? env : undefined);
|
|
259
|
+
return withConfiguredAuth(auth, headers, authHeader);
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
export function rawModelHeaders(model, config, extension) {
|
|
264
|
+
const definition = config?.models?.find((entry) => entry.id === model.id);
|
|
265
|
+
const extensionModel = extension?.models?.find((entry) => entry.id === model.id);
|
|
266
|
+
const headers = {
|
|
267
|
+
...config?.modelOverrides?.[model.id]?.headers,
|
|
268
|
+
...definition?.headers,
|
|
269
|
+
...extensionModel?.headers,
|
|
270
|
+
};
|
|
271
|
+
return Object.keys(headers).length > 0 ? headers : undefined;
|
|
272
|
+
}
|
|
273
|
+
//# sourceMappingURL=provider-composer-internal.js.map
|