@bastani/atomic 0.9.26-alpha.1 → 0.9.26-alpha.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/CHANGELOG.md +10 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/core/extensions/api-types.d.ts +2 -1
- package/dist/core/extensions/api-types.d.ts.map +1 -1
- package/dist/core/extensions/api-types.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader-api.d.ts.map +1 -1
- package/dist/core/extensions/loader-api.js +7 -0
- package/dist/core/extensions/loader-api.js.map +1 -1
- package/dist/core/extensions/provider-types.d.ts +2 -0
- package/dist/core/extensions/provider-types.d.ts.map +1 -1
- package/dist/core/extensions/provider-types.js.map +1 -1
- package/dist/core/model-runtime-streaming.d.ts +2 -1
- package/dist/core/model-runtime-streaming.d.ts.map +1 -1
- package/dist/core/model-runtime-streaming.js +4 -1
- package/dist/core/model-runtime-streaming.js.map +1 -1
- package/dist/core/model-runtime.d.ts +2 -0
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +69 -5
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/oauth-provider-metadata.d.ts.map +1 -1
- package/dist/core/oauth-provider-metadata.js +4 -17
- package/dist/core/oauth-provider-metadata.js.map +1 -1
- package/dist/core/provider-alias.d.ts +8 -0
- package/dist/core/provider-alias.d.ts.map +1 -0
- package/dist/core/provider-alias.js +119 -0
- package/dist/core/provider-alias.js.map +1 -0
- package/dist/core/provider-composer-internal.d.ts +1 -0
- package/dist/core/provider-composer-internal.d.ts.map +1 -1
- package/dist/core/provider-composer-internal.js.map +1 -1
- package/dist/core/provider-composer.d.ts.map +1 -1
- package/dist/core/provider-composer.js +9 -0
- package/dist/core/provider-composer.js.map +1 -1
- package/dist/core/sdk.js +2 -1
- package/dist/core/sdk.js.map +1 -1
- package/dist/index-extensions.d.ts +1 -1
- package/dist/index-extensions.d.ts.map +1 -1
- package/dist/index-extensions.js.map +1 -1
- package/docs/custom-provider/oauth.md +2 -0
- package/docs/custom-provider/registration.md +32 -0
- package/docs/custom-provider.md +1 -0
- package/examples/extensions/provider-accounts.ts +11 -0
- package/npm-shrinkwrap.json +32 -32
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider-types.js","sourceRoot":"","sources":["../../../src/core/extensions/provider-types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n\tAnyModel,\n\tClassifierApi,\n\tImageApi,\n\tProviderClassifier,\n\tProviderImages,\n\tRefreshModelsContext,\n} from \"@bastani/pi-ai\";\nimport type {\n\tApi,\n\tAssistantMessageEventStream,\n\tContext,\n\tModel,\n\tOAuthCredentials,\n\tOAuthLoginCallbacks,\n\tSimpleStreamOptions,\n} from \"@bastani/pi-ai/compat\";\nimport type { AtomicProviderCompat } from \"../model-capabilities.ts\";\nimport type { ExtensionAPI } from \"./api-types.ts\";\n\nexport type { AtomicProviderCompat } from \"../model-capabilities.ts\";\n\n/** Configuration for registering a provider via pi.registerProvider(). */\nexport interface ProviderConfig {\n\t/** Display name for the provider in UI. */\n\tname?: string;\n\t/** Base URL for the API endpoint. Required when defining models. */\n\tbaseUrl?: string;\n\t/** API key or environment variable name. Required when defining models (unless oauth provided). */\n\tapiKey?: string;\n\t/** API type. Required at provider or model level when defining models. */\n\tapi?: Api;\n\t/**\n\t * Optional streamSimple handler for custom APIs. Implementations may invoke\n\t * `options.onProviderStreamEvent(data, model)` with parsed stream events before normalization.\n\t * Event data is adapter-owned and must be treated as read-only.\n\t */\n\tstreamSimple?: (model: Model<Api>, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream;\n\t/** Image API implementations keyed by each image model's api. */\n\timages?: Partial<Record<ImageApi, ProviderImages>>;\n\t/** Classifier API implementations keyed by each classifier model's api. */\n\tclassifiers?: Partial<Record<ClassifierApi, ProviderClassifier>>;\n\t/** Custom headers to include in requests. */\n\theaders?: Record<string, string>;\n\t/** If true, adds Authorization: Bearer header with the resolved API key. */\n\tauthHeader?: boolean;\n\t/** Models to register. If provided, replaces all existing models for this provider. */\n\tmodels?: ProviderModelConfig[];\n\t/** Refresh this provider's catalog. Successful results replace its extension-provided models. */\n\trefreshModels?(context: RefreshModelsContext): Promise<ProviderModelConfig[]>;\n\t/** OAuth provider for /login support. The `id` is set automatically from the provider name. */\n\toauth?: {\n\t\t/** Display name for the provider in login UI. */\n\t\tname: string;\n\t\t/** Optional provider-specific login label. */\n\t\tloginLabel?: string;\n\t\t/** Whether the flow accepts a browser callback/manual redirect. */\n\t\tusesCallbackServer?: boolean;\n\t\t/** Run the login flow, return credentials to persist. */\n\t\tlogin(callbacks: OAuthLoginCallbacks, signal: AbortSignal): Promise<OAuthCredentials>;\n\t\t/**\n\t\t * Refresh expired credentials, return updated credentials to persist.\n\t\t * `signal` is the caller's cancellation signal — forward it into the\n\t\t * network call so an aborted refresh is actually abandoned.\n\t\t */\n\t\trefreshToken(credentials: OAuthCredentials, signal: AbortSignal): Promise<OAuthCredentials>;\n\t\t/** Convert credentials to API key string for the provider. */\n\t\tgetApiKey(credentials: OAuthCredentials): string;\n\t\t/** Optional: modify models for this provider (e.g., update baseUrl based on credentials). */\n\t\tmodifyModels?(models: Model<Api>[], credentials: OAuthCredentials): Model<Api>[];\n\t};\n}\n\ninterface ProviderModelConfigBase {\n\t/** Model ID (e.g., \"claude-sonnet-4-20250514\"). */\n\tid: string;\n\t/** Display name. */\n\tname: string;\n\tapi?: string;\n\tbaseUrl?: string;\n\tinput: AnyModel[\"input\"];\n\tinputLimits?: AnyModel[\"inputLimits\"];\n\tcost: AnyModel[\"cost\"];\n\theaders?: Record<string, string>;\n}\n\n/** Chat models are the default when `type` is omitted. */\nexport interface ProviderChatModelConfig extends ProviderModelConfigBase {\n\ttype?: \"chat\";\n\tapi?: Api;\n\t/** Whether the model supports extended thinking. */\n\treasoning: boolean;\n\t/** Maps pi thinking levels to provider/model-specific values; null marks a level unsupported. */\n\tthinkingLevelMap?: Model<Api>[\"thinkingLevelMap\"];\n\t/** Default/effective context window size in tokens. */\n\tcontextWindow: number;\n\t/** Maximum output tokens. */\n\tmaxTokens: number;\n\t/** Default sampling parameters merged into OpenAI-compatible request bodies. */\n\tsamplingParams?: Record<string, unknown>;\n\t/** Provider capability settings, including Atomic's `supportsGrammarTools` alias. */\n\tcompat?: AtomicProviderCompat;\n}\n\nexport interface ProviderImageModelConfig extends ProviderModelConfigBase {\n\ttype: \"image\";\n\tapi?: ImageApi;\n\toutput: (\"text\" | \"image\")[];\n}\n\nexport interface ProviderClassifierModelConfig extends ProviderModelConfigBase {\n\ttype: \"classifier\";\n\tapi?: ClassifierApi;\n\tcontextWindow: number;\n}\n\n/** Models for all supported operations in a provider registration. */\nexport type ProviderModelConfig = ProviderChatModelConfig | ProviderImageModelConfig | ProviderClassifierModelConfig;\n\n/** Extension factory function type. Supports both sync and async initialization. */\nexport type ExtensionFactory = (pi: ExtensionAPI) => void | Promise<void>;\n\n/** Inline extension factory, optionally carrying a stable name and display visibility. */\nexport type InlineExtension =\n\t| ExtensionFactory\n\t| {\n\t\t\tname: string;\n\t\t\tfactory: ExtensionFactory;\n\t\t\thidden?: boolean;\n\t\t\tbundled?: boolean;\n\t\t\tbuiltin?: boolean;\n\t\t\treplaceable?: boolean;\n\t };\n"]}
|
|
1
|
+
{"version":3,"file":"provider-types.js","sourceRoot":"","sources":["../../../src/core/extensions/provider-types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n\tAnyModel,\n\tClassifierApi,\n\tImageApi,\n\tProviderClassifier,\n\tProviderImages,\n\tRefreshModelsContext,\n} from \"@bastani/pi-ai\";\nimport type {\n\tApi,\n\tAssistantMessageEventStream,\n\tContext,\n\tModel,\n\tOAuthCredentials,\n\tOAuthLoginCallbacks,\n\tSimpleStreamOptions,\n} from \"@bastani/pi-ai/compat\";\nimport type { AtomicProviderCompat } from \"../model-capabilities.ts\";\nimport type { ExtensionAPI } from \"./api-types.ts\";\n\nexport type { AtomicProviderCompat } from \"../model-capabilities.ts\";\nexport type { ProviderAliasConfig } from \"../provider-alias.js\";\n\n/** Configuration for registering a provider via pi.registerProvider(). */\nexport interface ProviderConfig {\n\t/** Display name for the provider in UI. */\n\tname?: string;\n\taliasOf?: string;\n\t/** Base URL for the API endpoint. Required when defining models. */\n\tbaseUrl?: string;\n\t/** API key or environment variable name. Required when defining models (unless oauth provided). */\n\tapiKey?: string;\n\t/** API type. Required at provider or model level when defining models. */\n\tapi?: Api;\n\t/**\n\t * Optional streamSimple handler for custom APIs. Implementations may invoke\n\t * `options.onProviderStreamEvent(data, model)` with parsed stream events before normalization.\n\t * Event data is adapter-owned and must be treated as read-only.\n\t */\n\tstreamSimple?: (model: Model<Api>, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream;\n\t/** Image API implementations keyed by each image model's api. */\n\timages?: Partial<Record<ImageApi, ProviderImages>>;\n\t/** Classifier API implementations keyed by each classifier model's api. */\n\tclassifiers?: Partial<Record<ClassifierApi, ProviderClassifier>>;\n\t/** Custom headers to include in requests. */\n\theaders?: Record<string, string>;\n\t/** If true, adds Authorization: Bearer header with the resolved API key. */\n\tauthHeader?: boolean;\n\t/** Models to register. If provided, replaces all existing models for this provider. */\n\tmodels?: ProviderModelConfig[];\n\t/** Refresh this provider's catalog. Successful results replace its extension-provided models. */\n\trefreshModels?(context: RefreshModelsContext): Promise<ProviderModelConfig[]>;\n\t/** OAuth provider for /login support. The `id` is set automatically from the provider name. */\n\toauth?: {\n\t\t/** Display name for the provider in login UI. */\n\t\tname: string;\n\t\t/** Optional provider-specific login label. */\n\t\tloginLabel?: string;\n\t\t/** Whether the flow accepts a browser callback/manual redirect. */\n\t\tusesCallbackServer?: boolean;\n\t\t/** Run the login flow, return credentials to persist. */\n\t\tlogin(callbacks: OAuthLoginCallbacks, signal: AbortSignal): Promise<OAuthCredentials>;\n\t\t/**\n\t\t * Refresh expired credentials, return updated credentials to persist.\n\t\t * `signal` is the caller's cancellation signal — forward it into the\n\t\t * network call so an aborted refresh is actually abandoned.\n\t\t */\n\t\trefreshToken(credentials: OAuthCredentials, signal: AbortSignal): Promise<OAuthCredentials>;\n\t\t/** Convert credentials to API key string for the provider. */\n\t\tgetApiKey(credentials: OAuthCredentials): string;\n\t\t/** Optional: modify models for this provider (e.g., update baseUrl based on credentials). */\n\t\tmodifyModels?(models: Model<Api>[], credentials: OAuthCredentials): Model<Api>[];\n\t};\n}\n\ninterface ProviderModelConfigBase {\n\t/** Model ID (e.g., \"claude-sonnet-4-20250514\"). */\n\tid: string;\n\t/** Display name. */\n\tname: string;\n\tapi?: string;\n\tbaseUrl?: string;\n\tinput: AnyModel[\"input\"];\n\tinputLimits?: AnyModel[\"inputLimits\"];\n\tcost: AnyModel[\"cost\"];\n\theaders?: Record<string, string>;\n}\n\n/** Chat models are the default when `type` is omitted. */\nexport interface ProviderChatModelConfig extends ProviderModelConfigBase {\n\ttype?: \"chat\";\n\tapi?: Api;\n\t/** Whether the model supports extended thinking. */\n\treasoning: boolean;\n\t/** Maps pi thinking levels to provider/model-specific values; null marks a level unsupported. */\n\tthinkingLevelMap?: Model<Api>[\"thinkingLevelMap\"];\n\t/** Default/effective context window size in tokens. */\n\tcontextWindow: number;\n\t/** Maximum output tokens. */\n\tmaxTokens: number;\n\t/** Default sampling parameters merged into OpenAI-compatible request bodies. */\n\tsamplingParams?: Record<string, unknown>;\n\t/** Provider capability settings, including Atomic's `supportsGrammarTools` alias. */\n\tcompat?: AtomicProviderCompat;\n}\n\nexport interface ProviderImageModelConfig extends ProviderModelConfigBase {\n\ttype: \"image\";\n\tapi?: ImageApi;\n\toutput: (\"text\" | \"image\")[];\n}\n\nexport interface ProviderClassifierModelConfig extends ProviderModelConfigBase {\n\ttype: \"classifier\";\n\tapi?: ClassifierApi;\n\tcontextWindow: number;\n}\n\n/** Models for all supported operations in a provider registration. */\nexport type ProviderModelConfig = ProviderChatModelConfig | ProviderImageModelConfig | ProviderClassifierModelConfig;\n\n/** Extension factory function type. Supports both sync and async initialization. */\nexport type ExtensionFactory = (pi: ExtensionAPI) => void | Promise<void>;\n\n/** Inline extension factory, optionally carrying a stable name and display visibility. */\nexport type InlineExtension =\n\t| ExtensionFactory\n\t| {\n\t\t\tname: string;\n\t\t\tfactory: ExtensionFactory;\n\t\t\thidden?: boolean;\n\t\t\tbundled?: boolean;\n\t\t\tbuiltin?: boolean;\n\t\t\treplaceable?: boolean;\n\t };\n"]}
|
|
@@ -19,7 +19,8 @@ export declare class ModelRuntimeStreaming {
|
|
|
19
19
|
private readonly models;
|
|
20
20
|
private readonly resolveAuth;
|
|
21
21
|
private readonly ownsExtensionTransport;
|
|
22
|
-
|
|
22
|
+
private readonly transportModel;
|
|
23
|
+
constructor(models: MutableModels, resolveAuth: ResolveAuth, ownsExtensionTransport: OwnsExtensionTransport, transportModel?: (model: Model<Api>) => Model<Api>);
|
|
23
24
|
/**
|
|
24
25
|
* Attach the first-party ChatGPT Codex routing identity to a prepared request.
|
|
25
26
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-runtime-streaming.d.ts","sourceRoot":"","sources":["../../src/core/model-runtime-streaming.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,GAAG,EAER,KAAK,gBAAgB,EACrB,KAAK,2BAA2B,EAChC,KAAK,UAAU,EAEf,KAAK,OAAO,EAGZ,KAAK,cAAc,EAEnB,KAAK,KAAK,EACV,KAAK,sBAAsB,EAC3B,KAAK,2BAA2B,EAChC,KAAK,0BAA0B,EAE/B,KAAK,uBAAuB,EAC5B,KAAK,yBAAyB,EAC9B,KAAK,aAAa,EAGlB,KAAK,eAAe,EAEpB,KAAK,aAAa,EAClB,MAAM,gBAAg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,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,GAAG,CAAoB,EAMpE;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;YAUV,cAAc;IAqC5B,MAAM,CAAC,IAAI,SAAS,GAAG,EACtB,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,sBAAsB,CAAC,IAAI,CAAC,GACpC,2BAA2B,CAa7B;IAED,QAAQ,CAAC,IAAI,SAAS,GAAG,EACxB,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,sBAAsB,CAAC,IAAI,CAAC,GACpC,OAAO,CAAC,gBAAgB,CAAC,CAE3B;IAED,YAAY,CACX,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,+BAA+B,GACvC,2BAA2B,CAU7B;IAED,cAAc,CACb,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,+BAA+B,GACvC,OAAO,CAAC,gBAAgB,CAAC,CAE3B;IAEK,aAAa,CAClB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,MAAM,EAAE,cAAc,EACtB,OAAO,CAAC,EAAE,0BAA0B,GAClC,OAAO,CAAC,gBAAgB,CAAC,CAQ3B;IAEK,cAAc,CACnB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,MAAM,EAAE,cAAc,EACtB,OAAO,CAAC,EAAE,2BAA2B,GACnC,OAAO,CAAC,IAAI,CAAC,CAMf;CACD"}
|
|
@@ -20,10 +20,12 @@ export class ModelRuntimeStreaming {
|
|
|
20
20
|
models;
|
|
21
21
|
resolveAuth;
|
|
22
22
|
ownsExtensionTransport;
|
|
23
|
-
|
|
23
|
+
transportModel;
|
|
24
|
+
constructor(models, resolveAuth, ownsExtensionTransport, transportModel = (model) => model) {
|
|
24
25
|
this.models = models;
|
|
25
26
|
this.resolveAuth = resolveAuth;
|
|
26
27
|
this.ownsExtensionTransport = ownsExtensionTransport;
|
|
28
|
+
this.transportModel = transportModel;
|
|
27
29
|
}
|
|
28
30
|
/**
|
|
29
31
|
* Attach the first-party ChatGPT Codex routing identity to a prepared request.
|
|
@@ -35,6 +37,7 @@ export class ModelRuntimeStreaming {
|
|
|
35
37
|
* with `originator: pi` and no routing hint.
|
|
36
38
|
*/
|
|
37
39
|
withCodexRouting(model, options) {
|
|
40
|
+
model = this.transportModel(model);
|
|
38
41
|
if (!usesChatGptCodexTransport(model) || this.ownsExtensionTransport(model))
|
|
39
42
|
return options;
|
|
40
43
|
// The WebSocket handshake builds its headers inside pi-ai and the constructor can be cached, so
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-runtime-streaming.js","sourceRoot":"","sources":["../../src/core/model-runtime-streaming.ts"],"names":[],"mappings":"AAAA,OAAO,EAMN,eAAe,EAKf,UAAU,EAKV,WAAW,EAIX,gBAAgB,GAKhB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,gCAAgC,EAAE,MAAM,yBAAyB,CAAC;AACtG,OAAO,EAAE,sCAAsC,EAAE,MAAM,mCAAmC,CAAC;AAG3F,MAAM,UAAU,YAAY,CAC3B,IAAiC,EACjC,QAAqC;IAErC,IAAI,CAAC,IAAI,IAAI,CAAC,QAAQ;QAAE,OAAO,SAAS,CAAC;IACzC,MAAM,MAAM,GAAG,EAAE,GAAG,IAAI,EAAE,CAAC;IAC3B,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC,EAAE,CAAC;QAC5D,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QACrC,KAAK,MAAM,YAAY,IAAI,MAAM,CAAC,IAAI,CAAC,MA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,CAAC,CAAC,MAAM,EAAE,CAAC;IAC5D,CAAC;IAED,KAAK,CAAC,aAAa,CAClB,KAAiB,EACjB,MAAsB,EACtB,OAAoC;QAEpC,OAAO,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE;YACnC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YAC3D,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAC;gBACtC,MAAM,IAAI,WAAW,CAAC,UAAU,EAAE,YAAY,KAAK,CAAC,QAAQ,sCAAsC,CAAC,CAAC;YACrG,CAAC;YACD,OAAO,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,OAA+B,CAAC,CAAC;QAC1G,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACb,CAAC;IAED,KAAK,CAAC,cAAc,CACnB,KAAiB,EACjB,MAAsB,EACtB,OAAqC;QAErC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC3D,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAc,EAAE,CAAC;YACvC,MAAM,IAAI,WAAW,CAAC,UAAU,EAAE,YAAY,KAAK,CAAC,QAAQ,sCAAsC,CAAC,CAAC;QACrG,CAAC;QACD,MAAM,QAAQ,CAAC,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,OAAgC,CAAC,CAAC;IAC3G,CAAC;CACD","sourcesContent":["import {\n\ttype Api,\n\ttype ApiStreamOptions,\n\ttype AssistantMessage,\n\ttype AssistantMessageEventStream,\n\ttype AuthResult,\n\tassertChatModel,\n\ttype Context,\n\ttype DeferredCancelOptions,\n\ttype DeferredFetchOptions,\n\ttype DeferredHandle,\n\tlazyStream,\n\ttype Model,\n\ttype ModelsApiStreamOptions,\n\ttype ModelsDeferredCancelOptions,\n\ttype ModelsDeferredFetchOptions,\n\tModelsError,\n\ttype ModelsRequestTransforms,\n\ttype ModelsSimpleStreamOptions,\n\ttype MutableModels,\n\tnormalizeContext,\n\ttype Provider,\n\ttype ProviderHeaders,\n\ttype SimpleStreamOptions,\n\ttype StreamOptions,\n} from \"@bastani/pi-ai\";\nimport { usesChatGptCodexTransport, withChatGptCodexTransportRouting } from \"./fast-model-routing.ts\";\nimport { installCodexFastRouteWebSocketIdentity } from \"./fast-model-routing-transport.ts\";\nimport type { ModelRuntimeAuthOverrides } from \"./model-runtime-types.ts\";\n\nexport function mergeHeaders(\n\tbase: ProviderHeaders | undefined,\n\toverride: ProviderHeaders | undefined,\n): ProviderHeaders | undefined {\n\tif (!base && !override) return undefined;\n\tconst merged = { ...base };\n\tfor (const [name, value] of Object.entries(override ?? {})) {\n\t\tconst lowerName = name.toLowerCase();\n\t\tfor (const existingName of Object.keys(merged)) {\n\t\t\tif (existingName.toLowerCase() === lowerName) delete merged[existingName];\n\t\t}\n\t\tmerged[name] = value;\n\t}\n\treturn merged;\n}\n\n/** SDK-owned request-auth result so transport preparation does not start a second deadline. */\nexport interface PreparedRequestAuth {\n\treadonly resolution: AuthResult | undefined;\n}\n\nexport type ModelRuntimePreparedStreamOptions = StreamOptions &\n\tModelsRequestTransforms & {\n\t\tpreparedRequestAuth?: PreparedRequestAuth;\n\t};\n\nexport type ModelRuntimeSimpleStreamOptions = ModelsSimpleStreamOptions & {\n\tpreparedRequestAuth?: PreparedRequestAuth;\n};\n\ntype ResolveAuth = (model: Model<Api>, overrides?: ModelRuntimeAuthOverrides) => Promise<AuthResult | undefined>;\n/** Whether an extension owns this model's transport, in which case it owns its serialization too. */\ntype OwnsExtensionTransport = (model: Model<Api>) => boolean;\n\n/** Streaming request preparation split from ModelRuntime solely for Atomic's 500-line source gate. */\nexport class ModelRuntimeStreaming {\n\tprivate readonly models: MutableModels;\n\tprivate readonly resolveAuth: ResolveAuth;\n\tprivate readonly ownsExtensionTransport: OwnsExtensionTransport;\n\tconstructor(models: MutableModels, resolveAuth: ResolveAuth, ownsExtensionTransport: OwnsExtensionTransport) {\n\t\tthis.models = models;\n\t\tthis.resolveAuth = resolveAuth;\n\t\tthis.ownsExtensionTransport = ownsExtensionTransport;\n\t}\n\n\t/**\n\t * Attach the first-party ChatGPT Codex routing identity to a prepared request.\n\t *\n\t * This must happen *after* `prepareRequest`, not before it. The wrapper works by mutating the\n\t * header object it captures at construction, and `prepareRequest` rebuilds headers through\n\t * `mergeHeaders`, which copies — so a wrapper applied upstream is silently inert. The wrapper\n\t * decides from the final payload whether the request is priority, so a normal model still ends up\n\t * with `originator: pi` and no routing hint.\n\t */\n\tprivate withCodexRouting<TOptions extends StreamOptions>(model: Model<Api>, options: TOptions): TOptions {\n\t\tif (!usesChatGptCodexTransport(model) || this.ownsExtensionTransport(model)) return options;\n\t\t// The WebSocket handshake builds its headers inside pi-ai and the constructor can be cached, so\n\t\t// the identity is repaired through the global constructor rather than per-request options. CLI\n\t\t// entrypoints install this through the HTTP dispatcher; a pure SDK embedder never does.\n\t\tif (options.transport !== \"sse\") installCodexFastRouteWebSocketIdentity();\n\t\treturn withChatGptCodexTransportRouting(model, options);\n\t}\n\n\tprivate async prepareRequest(\n\t\tmodel: Model<Api>,\n\t\toptions: ModelRuntimePreparedStreamOptions | undefined,\n\t): Promise<{ provider: Provider; model: Model<Api>; options: StreamOptions }> {\n\t\tassertChatModel(model);\n\t\tconst provider = this.models.getProvider(model.provider);\n\t\tif (!provider) throw new ModelsError(\"provider\", `Unknown provider: ${model.provider}`);\n\t\tconst { transformHeaders, preparedRequestAuth, ...providerOptions } = options ?? {};\n\t\tconst resolution = preparedRequestAuth\n\t\t\t? preparedRequestAuth.resolution\n\t\t\t: await this.resolveAuth(model, {\n\t\t\t\t\tapiKey: options?.apiKey,\n\t\t\t\t\tenv: options?.env,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t});\n\t\toptions?.signal?.throwIfAborted();\n\t\tif (!resolution) throw new ModelsError(\"auth\", `Provider is not configured: ${model.provider}`);\n\n\t\tlet headers = mergeHeaders(resolution.auth.headers, providerOptions.headers);\n\t\tif (transformHeaders) headers = await transformHeaders(headers ?? {});\n\t\toptions?.signal?.throwIfAborted();\n\t\tconst env =\n\t\t\tresolution.env || providerOptions.env\n\t\t\t\t? { ...(resolution.env ?? {}), ...(providerOptions.env ?? {}) }\n\t\t\t\t: undefined;\n\t\treturn {\n\t\t\tprovider,\n\t\t\tmodel: resolution.auth.baseUrl ? { ...model, baseUrl: resolution.auth.baseUrl } : model,\n\t\t\toptions: {\n\t\t\t\t...providerOptions,\n\t\t\t\tapiKey: providerOptions.apiKey ?? resolution.auth.apiKey,\n\t\t\t\theaders,\n\t\t\t\tenv,\n\t\t\t},\n\t\t};\n\t}\n\n\tstream<TApi extends Api>(\n\t\tmodel: Model<TApi>,\n\t\tcontext: Context,\n\t\toptions?: ModelsApiStreamOptions<TApi>,\n\t): AssistantMessageEventStream {\n\t\tconst transcript = normalizeContext(context);\n\t\treturn lazyStream(model, async () => {\n\t\t\tconst prepared = await this.prepareRequest(\n\t\t\t\tmodel,\n\t\t\t\toptions as (StreamOptions & ModelsRequestTransforms) | undefined,\n\t\t\t);\n\t\t\treturn prepared.provider.stream(\n\t\t\t\tprepared.model as Model<TApi>,\n\t\t\t\ttranscript,\n\t\t\t\tthis.withCodexRouting(prepared.model, prepared.options) as ApiStreamOptions<TApi>,\n\t\t\t);\n\t\t});\n\t}\n\n\tcomplete<TApi extends Api>(\n\t\tmodel: Model<TApi>,\n\t\tcontext: Context,\n\t\toptions?: ModelsApiStreamOptions<TApi>,\n\t): Promise<AssistantMessage> {\n\t\treturn this.stream(model, context, options).result();\n\t}\n\n\tstreamSimple(\n\t\tmodel: Model<Api>,\n\t\tcontext: Context,\n\t\toptions?: ModelRuntimeSimpleStreamOptions,\n\t): AssistantMessageEventStream {\n\t\tconst transcript = normalizeContext(context);\n\t\treturn lazyStream(model, async () => {\n\t\t\tconst prepared = await this.prepareRequest(model, options);\n\t\t\treturn prepared.provider.streamSimple(\n\t\t\t\tprepared.model,\n\t\t\t\ttranscript,\n\t\t\t\tthis.withCodexRouting(prepared.model, prepared.options) as SimpleStreamOptions,\n\t\t\t);\n\t\t});\n\t}\n\n\tcompleteSimple(\n\t\tmodel: Model<Api>,\n\t\tcontext: Context,\n\t\toptions?: ModelRuntimeSimpleStreamOptions,\n\t): Promise<AssistantMessage> {\n\t\treturn this.streamSimple(model, context, options).result();\n\t}\n\n\tasync fetchDeferred(\n\t\tmodel: Model<Api>,\n\t\thandle: DeferredHandle,\n\t\toptions?: ModelsDeferredFetchOptions,\n\t): Promise<AssistantMessage> {\n\t\treturn lazyStream(model, async () => {\n\t\t\tconst prepared = await this.prepareRequest(model, options);\n\t\t\tif (!prepared.provider.fetchDeferred) {\n\t\t\t\tthrow new ModelsError(\"provider\", `Provider ${model.provider} does not support deferred responses`);\n\t\t\t}\n\t\t\treturn prepared.provider.fetchDeferred(prepared.model, handle, prepared.options as DeferredFetchOptions);\n\t\t}).result();\n\t}\n\n\tasync cancelDeferred(\n\t\tmodel: Model<Api>,\n\t\thandle: DeferredHandle,\n\t\toptions?: ModelsDeferredCancelOptions,\n\t): Promise<void> {\n\t\tconst prepared = await this.prepareRequest(model, options);\n\t\tif (!prepared.provider.cancelDeferred) {\n\t\t\tthrow new ModelsError(\"provider\", `Provider ${model.provider} does not support deferred responses`);\n\t\t}\n\t\tawait prepared.provider.cancelDeferred(prepared.model, handle, prepared.options as DeferredCancelOptions);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"model-runtime-streaming.js","sourceRoot":"","sources":["../../src/core/model-runtime-streaming.ts"],"names":[],"mappings":"AAAA,OAAO,EAMN,eAAe,EAKf,UAAU,EAKV,WAAW,EAIX,gBAAgB,GAKhB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,gCAAgC,EA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;IACb,CAAC;IAED,KAAK,CAAC,cAAc,CACnB,KAAiB,EACjB,MAAsB,EACtB,OAAqC;QAErC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC3D,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAc,EAAE,CAAC;YACvC,MAAM,IAAI,WAAW,CAAC,UAAU,EAAE,YAAY,KAAK,CAAC,QAAQ,sCAAsC,CAAC,CAAC;QACrG,CAAC;QACD,MAAM,QAAQ,CAAC,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,OAAgC,CAAC,CAAC;IAC3G,CAAC;CACD","sourcesContent":["import {\n\ttype Api,\n\ttype ApiStreamOptions,\n\ttype AssistantMessage,\n\ttype AssistantMessageEventStream,\n\ttype AuthResult,\n\tassertChatModel,\n\ttype Context,\n\ttype DeferredCancelOptions,\n\ttype DeferredFetchOptions,\n\ttype DeferredHandle,\n\tlazyStream,\n\ttype Model,\n\ttype ModelsApiStreamOptions,\n\ttype ModelsDeferredCancelOptions,\n\ttype ModelsDeferredFetchOptions,\n\tModelsError,\n\ttype ModelsRequestTransforms,\n\ttype ModelsSimpleStreamOptions,\n\ttype MutableModels,\n\tnormalizeContext,\n\ttype Provider,\n\ttype ProviderHeaders,\n\ttype SimpleStreamOptions,\n\ttype StreamOptions,\n} from \"@bastani/pi-ai\";\nimport { usesChatGptCodexTransport, withChatGptCodexTransportRouting } from \"./fast-model-routing.ts\";\nimport { installCodexFastRouteWebSocketIdentity } from \"./fast-model-routing-transport.ts\";\nimport type { ModelRuntimeAuthOverrides } from \"./model-runtime-types.ts\";\n\nexport function mergeHeaders(\n\tbase: ProviderHeaders | undefined,\n\toverride: ProviderHeaders | undefined,\n): ProviderHeaders | undefined {\n\tif (!base && !override) return undefined;\n\tconst merged = { ...base };\n\tfor (const [name, value] of Object.entries(override ?? {})) {\n\t\tconst lowerName = name.toLowerCase();\n\t\tfor (const existingName of Object.keys(merged)) {\n\t\t\tif (existingName.toLowerCase() === lowerName) delete merged[existingName];\n\t\t}\n\t\tmerged[name] = value;\n\t}\n\treturn merged;\n}\n\n/** SDK-owned request-auth result so transport preparation does not start a second deadline. */\nexport interface PreparedRequestAuth {\n\treadonly resolution: AuthResult | undefined;\n}\n\nexport type ModelRuntimePreparedStreamOptions = StreamOptions &\n\tModelsRequestTransforms & {\n\t\tpreparedRequestAuth?: PreparedRequestAuth;\n\t};\n\nexport type ModelRuntimeSimpleStreamOptions = ModelsSimpleStreamOptions & {\n\tpreparedRequestAuth?: PreparedRequestAuth;\n};\n\ntype ResolveAuth = (model: Model<Api>, overrides?: ModelRuntimeAuthOverrides) => Promise<AuthResult | undefined>;\n/** Whether an extension owns this model's transport, in which case it owns its serialization too. */\ntype OwnsExtensionTransport = (model: Model<Api>) => boolean;\n\n/** Streaming request preparation split from ModelRuntime solely for Atomic's 500-line source gate. */\nexport class ModelRuntimeStreaming {\n\tprivate readonly models: MutableModels;\n\tprivate readonly resolveAuth: ResolveAuth;\n\tprivate readonly ownsExtensionTransport: OwnsExtensionTransport;\n\tprivate readonly transportModel: (model: Model<Api>) => Model<Api>;\n\tconstructor(\n\t\tmodels: MutableModels,\n\t\tresolveAuth: ResolveAuth,\n\t\townsExtensionTransport: OwnsExtensionTransport,\n\t\ttransportModel: (model: Model<Api>) => Model<Api> = (model) => model,\n\t) {\n\t\tthis.models = models;\n\t\tthis.resolveAuth = resolveAuth;\n\t\tthis.ownsExtensionTransport = ownsExtensionTransport;\n\t\tthis.transportModel = transportModel;\n\t}\n\n\t/**\n\t * Attach the first-party ChatGPT Codex routing identity to a prepared request.\n\t *\n\t * This must happen *after* `prepareRequest`, not before it. The wrapper works by mutating the\n\t * header object it captures at construction, and `prepareRequest` rebuilds headers through\n\t * `mergeHeaders`, which copies — so a wrapper applied upstream is silently inert. The wrapper\n\t * decides from the final payload whether the request is priority, so a normal model still ends up\n\t * with `originator: pi` and no routing hint.\n\t */\n\tprivate withCodexRouting<TOptions extends StreamOptions>(model: Model<Api>, options: TOptions): TOptions {\n\t\tmodel = this.transportModel(model);\n\t\tif (!usesChatGptCodexTransport(model) || this.ownsExtensionTransport(model)) return options;\n\t\t// The WebSocket handshake builds its headers inside pi-ai and the constructor can be cached, so\n\t\t// the identity is repaired through the global constructor rather than per-request options. CLI\n\t\t// entrypoints install this through the HTTP dispatcher; a pure SDK embedder never does.\n\t\tif (options.transport !== \"sse\") installCodexFastRouteWebSocketIdentity();\n\t\treturn withChatGptCodexTransportRouting(model, options);\n\t}\n\n\tprivate async prepareRequest(\n\t\tmodel: Model<Api>,\n\t\toptions: ModelRuntimePreparedStreamOptions | undefined,\n\t): Promise<{ provider: Provider; model: Model<Api>; options: StreamOptions }> {\n\t\tassertChatModel(model);\n\t\tconst provider = this.models.getProvider(model.provider);\n\t\tif (!provider) throw new ModelsError(\"provider\", `Unknown provider: ${model.provider}`);\n\t\tconst { transformHeaders, preparedRequestAuth, ...providerOptions } = options ?? {};\n\t\tconst resolution = preparedRequestAuth\n\t\t\t? preparedRequestAuth.resolution\n\t\t\t: await this.resolveAuth(model, {\n\t\t\t\t\tapiKey: options?.apiKey,\n\t\t\t\t\tenv: options?.env,\n\t\t\t\t\tsignal: options?.signal,\n\t\t\t\t});\n\t\toptions?.signal?.throwIfAborted();\n\t\tif (!resolution) throw new ModelsError(\"auth\", `Provider is not configured: ${model.provider}`);\n\n\t\tlet headers = mergeHeaders(resolution.auth.headers, providerOptions.headers);\n\t\tif (transformHeaders) headers = await transformHeaders(headers ?? {});\n\t\toptions?.signal?.throwIfAborted();\n\t\tconst env =\n\t\t\tresolution.env || providerOptions.env\n\t\t\t\t? { ...(resolution.env ?? {}), ...(providerOptions.env ?? {}) }\n\t\t\t\t: undefined;\n\t\treturn {\n\t\t\tprovider,\n\t\t\tmodel: resolution.auth.baseUrl ? { ...model, baseUrl: resolution.auth.baseUrl } : model,\n\t\t\toptions: {\n\t\t\t\t...providerOptions,\n\t\t\t\tapiKey: providerOptions.apiKey ?? resolution.auth.apiKey,\n\t\t\t\theaders,\n\t\t\t\tenv,\n\t\t\t},\n\t\t};\n\t}\n\n\tstream<TApi extends Api>(\n\t\tmodel: Model<TApi>,\n\t\tcontext: Context,\n\t\toptions?: ModelsApiStreamOptions<TApi>,\n\t): AssistantMessageEventStream {\n\t\tconst transcript = normalizeContext(context);\n\t\treturn lazyStream(model, async () => {\n\t\t\tconst prepared = await this.prepareRequest(\n\t\t\t\tmodel,\n\t\t\t\toptions as (StreamOptions & ModelsRequestTransforms) | undefined,\n\t\t\t);\n\t\t\treturn prepared.provider.stream(\n\t\t\t\tprepared.model as Model<TApi>,\n\t\t\t\ttranscript,\n\t\t\t\tthis.withCodexRouting(prepared.model, prepared.options) as ApiStreamOptions<TApi>,\n\t\t\t);\n\t\t});\n\t}\n\n\tcomplete<TApi extends Api>(\n\t\tmodel: Model<TApi>,\n\t\tcontext: Context,\n\t\toptions?: ModelsApiStreamOptions<TApi>,\n\t): Promise<AssistantMessage> {\n\t\treturn this.stream(model, context, options).result();\n\t}\n\n\tstreamSimple(\n\t\tmodel: Model<Api>,\n\t\tcontext: Context,\n\t\toptions?: ModelRuntimeSimpleStreamOptions,\n\t): AssistantMessageEventStream {\n\t\tconst transcript = normalizeContext(context);\n\t\treturn lazyStream(model, async () => {\n\t\t\tconst prepared = await this.prepareRequest(model, options);\n\t\t\treturn prepared.provider.streamSimple(\n\t\t\t\tprepared.model,\n\t\t\t\ttranscript,\n\t\t\t\tthis.withCodexRouting(prepared.model, prepared.options) as SimpleStreamOptions,\n\t\t\t);\n\t\t});\n\t}\n\n\tcompleteSimple(\n\t\tmodel: Model<Api>,\n\t\tcontext: Context,\n\t\toptions?: ModelRuntimeSimpleStreamOptions,\n\t): Promise<AssistantMessage> {\n\t\treturn this.streamSimple(model, context, options).result();\n\t}\n\n\tasync fetchDeferred(\n\t\tmodel: Model<Api>,\n\t\thandle: DeferredHandle,\n\t\toptions?: ModelsDeferredFetchOptions,\n\t): Promise<AssistantMessage> {\n\t\treturn lazyStream(model, async () => {\n\t\t\tconst prepared = await this.prepareRequest(model, options);\n\t\t\tif (!prepared.provider.fetchDeferred) {\n\t\t\t\tthrow new ModelsError(\"provider\", `Provider ${model.provider} does not support deferred responses`);\n\t\t\t}\n\t\t\treturn prepared.provider.fetchDeferred(prepared.model, handle, prepared.options as DeferredFetchOptions);\n\t\t}).result();\n\t}\n\n\tasync cancelDeferred(\n\t\tmodel: Model<Api>,\n\t\thandle: DeferredHandle,\n\t\toptions?: ModelsDeferredCancelOptions,\n\t): Promise<void> {\n\t\tconst prepared = await this.prepareRequest(model, options);\n\t\tif (!prepared.provider.cancelDeferred) {\n\t\t\tthrow new ModelsError(\"provider\", `Provider ${model.provider} does not support deferred responses`);\n\t\t}\n\t\tawait prepared.provider.cancelDeferred(prepared.model, handle, prepared.options as DeferredCancelOptions);\n\t}\n}\n"]}
|
|
@@ -67,6 +67,7 @@ export declare class ModelRuntime implements Models {
|
|
|
67
67
|
*/
|
|
68
68
|
private publishProvider;
|
|
69
69
|
private recomposeProvider;
|
|
70
|
+
private composeProvider;
|
|
70
71
|
private rebuildProviders;
|
|
71
72
|
private updateModelSnapshot;
|
|
72
73
|
private runAvailabilityRefresh;
|
|
@@ -229,6 +230,7 @@ export declare class ModelRuntime implements Models {
|
|
|
229
230
|
*/
|
|
230
231
|
private scheduleRegistrationRefresh;
|
|
231
232
|
private runRefresh;
|
|
233
|
+
private validateAliasRegistration;
|
|
232
234
|
createExtensionProviderTransaction(replacedProviderIds?: Iterable<string>): ExtensionProviderTransaction;
|
|
233
235
|
registerNativeProvider(provider: Provider): void;
|
|
234
236
|
registerProvider(providerId: string, config: ProviderConfigInput): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-runtime.d.ts","sourceRoot":"","sources":["../../src/core/model-runtime.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,2BAA2B,EAChC,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,UAAU,EACf,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,cAAc,EAEnB,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,MAAM,EACX,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,EAC5B,KAAK,2BAA2B,EAChC,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EAExB,KAAK,SAAS,EACd,KAAK,YAAY,EAEjB,KAAK,QAAQ,EAEb,MAAM,gBAAgB,CAAC;AAOxB,OAAO,EAIN,KAAK,0BAA0B,EAG/B,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"model-runtime.d.ts","sourceRoot":"","sources":["../../src/core/model-runtime.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,2BAA2B,EAChC,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,UAAU,EACf,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,cAAc,EAEnB,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,M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ider, createEmptyModel
|
|
|
15
15
|
import { FileModelsStore, InMemoryCodingAgentModelsStore } from "./models-store.js";
|
|
16
16
|
import { collectOAuthProviderMetadata } from "./oauth-provider-metadata.js";
|
|
17
17
|
import { isOfflineModeEnabled } from "./package-manager-env.js";
|
|
18
|
+
import { createProviderAlias } from "./provider-alias.js";
|
|
18
19
|
import { composeModelProvider, configuredRequestAuthStatus, resolveCompatibilityRequestConfig, resolveConfiguredModelHeaders, validateExtensionProvider, } from "./provider-composer.js";
|
|
19
20
|
import { withRemoteCatalog } from "./remote-catalog-provider.js";
|
|
20
21
|
import { RuntimeCredentials } from "./runtime-credentials.js";
|
|
@@ -80,7 +81,10 @@ export class ModelRuntime {
|
|
|
80
81
|
for (const [providerId, provider] of this.defaultBuiltins)
|
|
81
82
|
this.builtins.set(providerId, provider);
|
|
82
83
|
this.models = createModels({ credentials, modelsStore });
|
|
83
|
-
this.streaming = new ModelRuntimeStreaming(this.models, (model, overrides) => this.getRequestAuth(model, overrides), (model) => this.ownsExtensionTransport(model))
|
|
84
|
+
this.streaming = new ModelRuntimeStreaming(this.models, (model, overrides) => this.getRequestAuth(model, overrides), (model) => this.ownsExtensionTransport(model), (model) => {
|
|
85
|
+
const source = this.extensionProviders.get(model.provider)?.aliasOf;
|
|
86
|
+
return source ? { ...model, provider: source } : model;
|
|
87
|
+
});
|
|
84
88
|
this.rebuildProviders();
|
|
85
89
|
}
|
|
86
90
|
static async create(options = {}) {
|
|
@@ -177,8 +181,38 @@ export class ModelRuntime {
|
|
|
177
181
|
this.models.setProvider(this.withDerivedFastModels(provider));
|
|
178
182
|
}
|
|
179
183
|
recomposeProvider(providerId) {
|
|
184
|
+
this.composeProvider(providerId);
|
|
185
|
+
for (const [id, config] of this.extensionProviders) {
|
|
186
|
+
if (config.aliasOf === providerId)
|
|
187
|
+
this.composeProvider(id);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
composeProvider(providerId) {
|
|
180
191
|
const base = this.nativeExtensionProviders.get(providerId) ?? this.builtins.get(providerId);
|
|
181
192
|
const extension = this.extensionProviders.get(providerId);
|
|
193
|
+
if (extension?.aliasOf !== undefined) {
|
|
194
|
+
try {
|
|
195
|
+
if (this.config.getProvider(providerId)) {
|
|
196
|
+
throw new Error(`Provider alias ${providerId}: configure models.json on source ${extension.aliasOf}, not the alias.`);
|
|
197
|
+
}
|
|
198
|
+
const sourceId = extension.aliasOf;
|
|
199
|
+
this.publishProvider(createProviderAlias({ id: providerId, name: extension.name, provider: sourceId }, () => {
|
|
200
|
+
if (this.extensionProviders.get(sourceId)?.aliasOf !== undefined) {
|
|
201
|
+
throw new Error("Provider aliases must refer directly to a source provider, not another alias.");
|
|
202
|
+
}
|
|
203
|
+
const source = this.models.getProvider(sourceId);
|
|
204
|
+
if (!source)
|
|
205
|
+
throw new Error(`Provider alias ${providerId}: source provider ${sourceId} is unavailable.`);
|
|
206
|
+
return source;
|
|
207
|
+
}));
|
|
208
|
+
this.compositionErrors.delete(providerId);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
this.compositionErrors.set(providerId, error instanceof Error ? error.message : String(error));
|
|
212
|
+
this.models.deleteProvider(providerId);
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
182
216
|
if (!base && !this.config.getProvider(providerId) && !extension) {
|
|
183
217
|
this.models.deleteProvider(providerId);
|
|
184
218
|
this.compositionErrors.delete(providerId);
|
|
@@ -207,8 +241,13 @@ export class ModelRuntime {
|
|
|
207
241
|
this.models.clearProviders();
|
|
208
242
|
this.compositionErrors.clear();
|
|
209
243
|
this.fastModelVariantDiagnostics.clear();
|
|
210
|
-
|
|
244
|
+
const ids = [...this.providerIds()];
|
|
245
|
+
for (const providerId of ids.filter((id) => this.extensionProviders.get(id)?.aliasOf === undefined)) {
|
|
246
|
+
this.recomposeProvider(providerId);
|
|
247
|
+
}
|
|
248
|
+
for (const providerId of ids.filter((id) => this.extensionProviders.get(id)?.aliasOf !== undefined)) {
|
|
211
249
|
this.recomposeProvider(providerId);
|
|
250
|
+
}
|
|
212
251
|
this.updateModelSnapshot();
|
|
213
252
|
}
|
|
214
253
|
updateModelSnapshot() {
|
|
@@ -827,6 +866,26 @@ export class ModelRuntime {
|
|
|
827
866
|
}
|
|
828
867
|
return { aborted: result.aborted || (options.signal?.aborted ?? false), errors };
|
|
829
868
|
}
|
|
869
|
+
validateAliasRegistration(id, config, providers, nativeProviders) {
|
|
870
|
+
const source = config.aliasOf;
|
|
871
|
+
if (source === undefined)
|
|
872
|
+
return;
|
|
873
|
+
if (this.builtins.has(id) ||
|
|
874
|
+
nativeProviders.has(id) ||
|
|
875
|
+
this.config.getProvider(id) ||
|
|
876
|
+
(providers.has(id) && providers.get(id)?.aliasOf === undefined)) {
|
|
877
|
+
throw new Error(`Provider alias ${id} cannot replace an existing provider.`);
|
|
878
|
+
}
|
|
879
|
+
if (providers.get(source)?.aliasOf !== undefined) {
|
|
880
|
+
throw new Error("Provider aliases must refer directly to a source provider, not another alias.");
|
|
881
|
+
}
|
|
882
|
+
if (!this.builtins.has(source) &&
|
|
883
|
+
!nativeProviders.has(source) &&
|
|
884
|
+
!providers.has(source) &&
|
|
885
|
+
!this.config.getProvider(source)) {
|
|
886
|
+
throw new Error(`Provider alias ${id}: source provider ${source} is unavailable.`);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
830
889
|
createExtensionProviderTransaction(replacedProviderIds = []) {
|
|
831
890
|
const nativeProviders = new Map(this.nativeExtensionProviders);
|
|
832
891
|
const providers = new Map(this.extensionProviders);
|
|
@@ -848,8 +907,10 @@ export class ModelRuntime {
|
|
|
848
907
|
},
|
|
849
908
|
registerProvider: (providerId, config) => {
|
|
850
909
|
validateExtensionProvider(providerId, this.builtins.get(providerId), this.config.getProvider(providerId), config);
|
|
910
|
+
this.validateAliasRegistration(providerId, config, providers, nativeProviders);
|
|
851
911
|
nativeProviders.delete(providerId);
|
|
852
|
-
const
|
|
912
|
+
const previous = providers.get(providerId);
|
|
913
|
+
const effective = config.aliasOf !== undefined || previous?.aliasOf !== undefined ? {} : { ...previous };
|
|
853
914
|
for (const [key, value] of Object.entries(config)) {
|
|
854
915
|
if (value !== undefined)
|
|
855
916
|
effective[key] = value;
|
|
@@ -923,11 +984,12 @@ export class ModelRuntime {
|
|
|
923
984
|
// Validate the incoming registration on its own, like the legacy registry:
|
|
924
985
|
// a broken re-registration must throw without touching the stored config.
|
|
925
986
|
validateExtensionProvider(providerId, this.builtins.get(providerId), this.config.getProvider(providerId), config);
|
|
987
|
+
this.validateAliasRegistration(providerId, config, this.extensionProviders, this.nativeExtensionProviders);
|
|
926
988
|
this.nativeExtensionProviders.delete(providerId);
|
|
927
989
|
// Re-registration merges defined values over the previous registration and
|
|
928
990
|
// preserves undefined ones, matching the legacy ModelRegistry contract.
|
|
929
991
|
const previous = this.extensionProviders.get(providerId);
|
|
930
|
-
const effective = { ...previous };
|
|
992
|
+
const effective = config.aliasOf !== undefined || previous?.aliasOf !== undefined ? {} : { ...previous };
|
|
931
993
|
for (const [key, value] of Object.entries(config)) {
|
|
932
994
|
if (value !== undefined)
|
|
933
995
|
effective[key] = value;
|
|
@@ -935,7 +997,9 @@ export class ModelRuntime {
|
|
|
935
997
|
this.extensionProviders.set(providerId, effective);
|
|
936
998
|
this.recomposeProvider(providerId);
|
|
937
999
|
this.updateModelSnapshot();
|
|
938
|
-
this.markProvisionallyConfigured(providerId, configuredRequestAuthStatus(this.config.getProvider(providerId), effective),
|
|
1000
|
+
this.markProvisionallyConfigured(providerId, configuredRequestAuthStatus(this.config.getProvider(providerId), effective), this.models.getProvider(providerId)?.auth.oauth && !this.models.getProvider(providerId)?.auth.apiKey
|
|
1001
|
+
? "oauth"
|
|
1002
|
+
: "api_key");
|
|
939
1003
|
this.scheduleRegistrationRefresh();
|
|
940
1004
|
}
|
|
941
1005
|
markProvisionallyConfigured(providerId, configuredStatus, type) {
|