@x12i/ai-dispatcher 2.2.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -11
- package/dist/index.cjs +36 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -5
- package/dist/index.d.ts +19 -5
- package/dist/index.js +35 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
2
|
+
export { ReasoningEffort, ReasoningOutcome, registerHostIntents } from '@x12i/ai-profiles';
|
|
1
3
|
import * as _x12i_openrouter_runtime from '@x12i/openrouter-runtime';
|
|
2
4
|
import { RuntimeToolExecutor, RuntimeLogger, RuntimeRequest, RuntimeResponse, RuntimeUsage, RuntimeStreamEvent, CompiledOpenRouterRequest, OpenRouterRuntimeOptions } from '@x12i/openrouter-runtime';
|
|
3
5
|
export { CompiledOpenRouterRequest, METADATA_NORMALIZATION_MAP, NormalizedMetadataKey, OpenRouterRuntimeOptions, RuntimeDefaults, RuntimeLogger, RuntimeMessage, RuntimeRequest, RuntimeResponse, RuntimeServerToolsPolicy, RuntimeStreamEvent, canonicalMetadataKey, decodeProviderMetadata, normalizeMetadata } from '@x12i/openrouter-runtime';
|
|
4
|
-
import { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
5
|
-
export { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
6
6
|
import { CompiledBedrockRequest, BedrockRuntimeOptions } from '@x12i/bedrock-runtime';
|
|
7
7
|
export { BedrockClientLike, BedrockCredentials, BedrockRuntimeOptions, CompiledBedrockRequest } from '@x12i/bedrock-runtime';
|
|
8
8
|
|
|
@@ -93,6 +93,13 @@ interface OpenAiRuntimeOptions {
|
|
|
93
93
|
logger?: RuntimeLogger;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/** Caller role stamp. Omitted or blank fields are not stored. */
|
|
97
|
+
interface RoleStamp {
|
|
98
|
+
role?: string;
|
|
99
|
+
roleLayer?: string;
|
|
100
|
+
roleDigest?: string;
|
|
101
|
+
profileChoice?: string;
|
|
102
|
+
}
|
|
96
103
|
/**
|
|
97
104
|
* Catalog resolution for one call.
|
|
98
105
|
* `outcome` is present only when the catalog resolver ran.
|
|
@@ -152,6 +159,12 @@ interface AiDispatcherRequest extends RuntimeRequest {
|
|
|
152
159
|
* Caller skill id. Stored in the one provider metadata record as `skillId`.
|
|
153
160
|
*/
|
|
154
161
|
skillId?: string;
|
|
162
|
+
/**
|
|
163
|
+
* Role resolution stamp. Expanded into the metadata record as `role`, `roleLayer`,
|
|
164
|
+
* `roleDigest`, and `profileChoice`. A set field wins over the same key in `metadata`.
|
|
165
|
+
* Stripped before the provider payload is built.
|
|
166
|
+
*/
|
|
167
|
+
role?: RoleStamp;
|
|
155
168
|
/**
|
|
156
169
|
* Provider-neutral reasoning intent. The bundled catalog chooses the wire action.
|
|
157
170
|
* An explicit provider control on the same request wins and the catalog is not called.
|
|
@@ -219,7 +232,8 @@ interface AiDispatcherOptions {
|
|
|
219
232
|
* Highest priority first for the one metadata record.
|
|
220
233
|
* Listed keys are kept ahead of keys that are not in the list.
|
|
221
234
|
* The dispatcher trims on every call and adds a `METADATA_RECORD_TRIMMED` warning when it drops keys.
|
|
222
|
-
* The default keeps `agentId`, `orgId`, `stepId`, `skillId`, `
|
|
235
|
+
* The default keeps `agentId`, `orgId`, `stepId`, `skillId`, `role`, `roleLayer`,
|
|
236
|
+
* `roleDigest`, `profileChoice`, `recordId`, and `objectType`.
|
|
223
237
|
*/
|
|
224
238
|
metadataPriority?: readonly string[];
|
|
225
239
|
logger?: RuntimeLogger;
|
|
@@ -302,7 +316,7 @@ interface AiDispatcher {
|
|
|
302
316
|
}
|
|
303
317
|
|
|
304
318
|
/** Identity keys stay when a record has to be trimmed. Earlier entries are kept ahead of later ones. */
|
|
305
|
-
declare const DEFAULT_METADATA_PRIORITY: readonly ["agentId", "orgId", "stepId", "skillId", "recordId", "objectType"];
|
|
319
|
+
declare const DEFAULT_METADATA_PRIORITY: readonly ["agentId", "orgId", "stepId", "skillId", "role", "roleLayer", "roleDigest", "profileChoice", "recordId", "objectType"];
|
|
306
320
|
declare function createAiDispatcher(options?: AiDispatcherOptions): AiDispatcher;
|
|
307
321
|
|
|
308
322
|
declare class AiDispatcherError extends Error {
|
|
@@ -320,4 +334,4 @@ declare function stripReasoningHistoryFields<T>(message: T, paths: readonly stri
|
|
|
320
334
|
|
|
321
335
|
declare function resolveProvider(requestProvider: AiProviderId | undefined, defaultProvider: AiProviderId): ImplementedAiProvider;
|
|
322
336
|
|
|
323
|
-
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiDispatcherUsage, type AiProviderAdapter, type AiProviderId, type CacheMode, type CacheOutcome, type CachePolicy, type CacheResolution, type CacheTtl, type CloudflareBackoff, type CloudflareCallContext, type CloudflareEndpoint, type CloudflareEndpointChoice, type CloudflareGatewayControls, type CloudflareRequestOptions, type CloudflareRunOptions, type CloudflareRuntimeOptions, type CloudflareWebhookFormat, type CompiledAiRequest, type CompiledCloudflareRequest, type CompiledOpenAiRequest, DEFAULT_METADATA_PRIORITY, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
|
337
|
+
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiDispatcherUsage, type AiProviderAdapter, type AiProviderId, type CacheMode, type CacheOutcome, type CachePolicy, type CacheResolution, type CacheTtl, type CloudflareBackoff, type CloudflareCallContext, type CloudflareEndpoint, type CloudflareEndpointChoice, type CloudflareGatewayControls, type CloudflareRequestOptions, type CloudflareRunOptions, type CloudflareRuntimeOptions, type CloudflareWebhookFormat, type CompiledAiRequest, type CompiledCloudflareRequest, type CompiledOpenAiRequest, DEFAULT_METADATA_PRIORITY, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, type RoleStamp, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
2
|
+
export { ReasoningEffort, ReasoningOutcome, registerHostIntents } from '@x12i/ai-profiles';
|
|
1
3
|
import * as _x12i_openrouter_runtime from '@x12i/openrouter-runtime';
|
|
2
4
|
import { RuntimeToolExecutor, RuntimeLogger, RuntimeRequest, RuntimeResponse, RuntimeUsage, RuntimeStreamEvent, CompiledOpenRouterRequest, OpenRouterRuntimeOptions } from '@x12i/openrouter-runtime';
|
|
3
5
|
export { CompiledOpenRouterRequest, METADATA_NORMALIZATION_MAP, NormalizedMetadataKey, OpenRouterRuntimeOptions, RuntimeDefaults, RuntimeLogger, RuntimeMessage, RuntimeRequest, RuntimeResponse, RuntimeServerToolsPolicy, RuntimeStreamEvent, canonicalMetadataKey, decodeProviderMetadata, normalizeMetadata } from '@x12i/openrouter-runtime';
|
|
4
|
-
import { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
5
|
-
export { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
6
6
|
import { CompiledBedrockRequest, BedrockRuntimeOptions } from '@x12i/bedrock-runtime';
|
|
7
7
|
export { BedrockClientLike, BedrockCredentials, BedrockRuntimeOptions, CompiledBedrockRequest } from '@x12i/bedrock-runtime';
|
|
8
8
|
|
|
@@ -93,6 +93,13 @@ interface OpenAiRuntimeOptions {
|
|
|
93
93
|
logger?: RuntimeLogger;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
/** Caller role stamp. Omitted or blank fields are not stored. */
|
|
97
|
+
interface RoleStamp {
|
|
98
|
+
role?: string;
|
|
99
|
+
roleLayer?: string;
|
|
100
|
+
roleDigest?: string;
|
|
101
|
+
profileChoice?: string;
|
|
102
|
+
}
|
|
96
103
|
/**
|
|
97
104
|
* Catalog resolution for one call.
|
|
98
105
|
* `outcome` is present only when the catalog resolver ran.
|
|
@@ -152,6 +159,12 @@ interface AiDispatcherRequest extends RuntimeRequest {
|
|
|
152
159
|
* Caller skill id. Stored in the one provider metadata record as `skillId`.
|
|
153
160
|
*/
|
|
154
161
|
skillId?: string;
|
|
162
|
+
/**
|
|
163
|
+
* Role resolution stamp. Expanded into the metadata record as `role`, `roleLayer`,
|
|
164
|
+
* `roleDigest`, and `profileChoice`. A set field wins over the same key in `metadata`.
|
|
165
|
+
* Stripped before the provider payload is built.
|
|
166
|
+
*/
|
|
167
|
+
role?: RoleStamp;
|
|
155
168
|
/**
|
|
156
169
|
* Provider-neutral reasoning intent. The bundled catalog chooses the wire action.
|
|
157
170
|
* An explicit provider control on the same request wins and the catalog is not called.
|
|
@@ -219,7 +232,8 @@ interface AiDispatcherOptions {
|
|
|
219
232
|
* Highest priority first for the one metadata record.
|
|
220
233
|
* Listed keys are kept ahead of keys that are not in the list.
|
|
221
234
|
* The dispatcher trims on every call and adds a `METADATA_RECORD_TRIMMED` warning when it drops keys.
|
|
222
|
-
* The default keeps `agentId`, `orgId`, `stepId`, `skillId`, `
|
|
235
|
+
* The default keeps `agentId`, `orgId`, `stepId`, `skillId`, `role`, `roleLayer`,
|
|
236
|
+
* `roleDigest`, `profileChoice`, `recordId`, and `objectType`.
|
|
223
237
|
*/
|
|
224
238
|
metadataPriority?: readonly string[];
|
|
225
239
|
logger?: RuntimeLogger;
|
|
@@ -302,7 +316,7 @@ interface AiDispatcher {
|
|
|
302
316
|
}
|
|
303
317
|
|
|
304
318
|
/** Identity keys stay when a record has to be trimmed. Earlier entries are kept ahead of later ones. */
|
|
305
|
-
declare const DEFAULT_METADATA_PRIORITY: readonly ["agentId", "orgId", "stepId", "skillId", "recordId", "objectType"];
|
|
319
|
+
declare const DEFAULT_METADATA_PRIORITY: readonly ["agentId", "orgId", "stepId", "skillId", "role", "roleLayer", "roleDigest", "profileChoice", "recordId", "objectType"];
|
|
306
320
|
declare function createAiDispatcher(options?: AiDispatcherOptions): AiDispatcher;
|
|
307
321
|
|
|
308
322
|
declare class AiDispatcherError extends Error {
|
|
@@ -320,4 +334,4 @@ declare function stripReasoningHistoryFields<T>(message: T, paths: readonly stri
|
|
|
320
334
|
|
|
321
335
|
declare function resolveProvider(requestProvider: AiProviderId | undefined, defaultProvider: AiProviderId): ImplementedAiProvider;
|
|
322
336
|
|
|
323
|
-
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiDispatcherUsage, type AiProviderAdapter, type AiProviderId, type CacheMode, type CacheOutcome, type CachePolicy, type CacheResolution, type CacheTtl, type CloudflareBackoff, type CloudflareCallContext, type CloudflareEndpoint, type CloudflareEndpointChoice, type CloudflareGatewayControls, type CloudflareRequestOptions, type CloudflareRunOptions, type CloudflareRuntimeOptions, type CloudflareWebhookFormat, type CompiledAiRequest, type CompiledCloudflareRequest, type CompiledOpenAiRequest, DEFAULT_METADATA_PRIORITY, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
|
337
|
+
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiDispatcherUsage, type AiProviderAdapter, type AiProviderId, type CacheMode, type CacheOutcome, type CachePolicy, type CacheResolution, type CacheTtl, type CloudflareBackoff, type CloudflareCallContext, type CloudflareEndpoint, type CloudflareEndpointChoice, type CloudflareGatewayControls, type CloudflareRequestOptions, type CloudflareRunOptions, type CloudflareRuntimeOptions, type CloudflareWebhookFormat, type CompiledAiRequest, type CompiledCloudflareRequest, type CompiledOpenAiRequest, DEFAULT_METADATA_PRIORITY, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, type RoleStamp, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { registerHostIntents } from "@x12i/ai-profiles";
|
|
2
3
|
import {
|
|
3
4
|
decodeProviderMetadata,
|
|
4
5
|
METADATA_NORMALIZATION_MAP,
|
|
@@ -2328,6 +2329,7 @@ function isRecord5(value) {
|
|
|
2328
2329
|
// src/prepare-request.ts
|
|
2329
2330
|
function prepareDispatchRequest(params) {
|
|
2330
2331
|
const cloned = cloneRequest(params.request);
|
|
2332
|
+
applyRoleStamp(cloned);
|
|
2331
2333
|
applyDispatchIdentity(cloned);
|
|
2332
2334
|
absorbOverrideMetadata(cloned);
|
|
2333
2335
|
const provider = params.provider;
|
|
@@ -2635,6 +2637,23 @@ function defaultModelFor(provider, options) {
|
|
|
2635
2637
|
return options.openai?.defaultModel;
|
|
2636
2638
|
}
|
|
2637
2639
|
var DISPATCH_IDENTITY_FIELDS = ["orgId", "stepId", "skillId"];
|
|
2640
|
+
var ROLE_STAMP_FIELDS = ["role", "roleLayer", "roleDigest", "profileChoice"];
|
|
2641
|
+
function applyRoleStamp(request) {
|
|
2642
|
+
const stamp = request.role;
|
|
2643
|
+
delete request.role;
|
|
2644
|
+
if (!isRecord6(stamp)) return;
|
|
2645
|
+
const identity = {};
|
|
2646
|
+
for (const field of ROLE_STAMP_FIELDS) {
|
|
2647
|
+
const value = stamp[field];
|
|
2648
|
+
if (typeof value !== "string") continue;
|
|
2649
|
+
const trimmed = value.trim();
|
|
2650
|
+
if (trimmed) identity[field] = trimmed;
|
|
2651
|
+
}
|
|
2652
|
+
if (!Object.keys(identity).length) return;
|
|
2653
|
+
const rest = { ...request.metadata ?? {} };
|
|
2654
|
+
for (const key of Object.keys(identity)) delete rest[key];
|
|
2655
|
+
request.metadata = { ...identity, ...rest };
|
|
2656
|
+
}
|
|
2638
2657
|
function absorbOverrideMetadata(request) {
|
|
2639
2658
|
const overrides = request.rawOpenRouterOverrides;
|
|
2640
2659
|
if (!isRecord6(overrides) || !Object.prototype.hasOwnProperty.call(overrides, "metadata")) return;
|
|
@@ -3240,8 +3259,7 @@ async function readFailure2(response) {
|
|
|
3240
3259
|
function classifyStatus2(status) {
|
|
3241
3260
|
if (status === 401 || status === 403) return "PROVIDER_AUTH_FAILED";
|
|
3242
3261
|
if (status === 404) return "PROVIDER_MODEL_NOT_FOUND";
|
|
3243
|
-
if (status === 429) return "
|
|
3244
|
-
if (status === 408 || status >= 500) return "PROVIDER_RETRYABLE";
|
|
3262
|
+
if (status === 429 || status === 408 || status >= 500) return "PROVIDER_RETRYABLE";
|
|
3245
3263
|
return "PROVIDER_REQUEST_FAILED";
|
|
3246
3264
|
}
|
|
3247
3265
|
function isConfigError2(code) {
|
|
@@ -3249,6 +3267,8 @@ function isConfigError2(code) {
|
|
|
3249
3267
|
}
|
|
3250
3268
|
function asTerminalError2(error) {
|
|
3251
3269
|
if (error instanceof AiDispatcherError && error.code === "PROVIDER_RETRYABLE") {
|
|
3270
|
+
const status = isRecord7(error.details) ? error.details.status : void 0;
|
|
3271
|
+
if (status === 429) return new AiDispatcherError("PROVIDER_RATE_LIMITED", error.message, error.details);
|
|
3252
3272
|
return new AiDispatcherError("PROVIDER_GATEWAY_UNREACHABLE", error.message, error.details);
|
|
3253
3273
|
}
|
|
3254
3274
|
if (error instanceof AiDispatcherError) return error;
|
|
@@ -3510,7 +3530,18 @@ function resolveProvider(requestProvider, defaultProvider) {
|
|
|
3510
3530
|
|
|
3511
3531
|
// src/create-dispatcher.ts
|
|
3512
3532
|
var SECRET_HEADERS = /* @__PURE__ */ new Set(["authorization", "x-api-key", "api-key"]);
|
|
3513
|
-
var DEFAULT_METADATA_PRIORITY = [
|
|
3533
|
+
var DEFAULT_METADATA_PRIORITY = [
|
|
3534
|
+
"agentId",
|
|
3535
|
+
"orgId",
|
|
3536
|
+
"stepId",
|
|
3537
|
+
"skillId",
|
|
3538
|
+
"role",
|
|
3539
|
+
"roleLayer",
|
|
3540
|
+
"roleDigest",
|
|
3541
|
+
"profileChoice",
|
|
3542
|
+
"recordId",
|
|
3543
|
+
"objectType"
|
|
3544
|
+
];
|
|
3514
3545
|
function createAiDispatcher(options = {}) {
|
|
3515
3546
|
const packer = createMetadataPacker({ priority: options.metadataPriority ?? DEFAULT_METADATA_PRIORITY });
|
|
3516
3547
|
const defaultProvider = resolveProvider(options.provider, "openrouter");
|
|
@@ -3675,6 +3706,7 @@ export {
|
|
|
3675
3706
|
isImplementedAiProvider,
|
|
3676
3707
|
normalizeMetadata,
|
|
3677
3708
|
providerNotImplemented,
|
|
3709
|
+
registerHostIntents,
|
|
3678
3710
|
resolveProvider,
|
|
3679
3711
|
stripReasoningHistoryFields
|
|
3680
3712
|
};
|