@providerprotocol/ai 0.0.39 → 0.0.40
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 +269 -34
- package/dist/anthropic/index.d.ts +3 -3
- package/dist/anthropic/index.js +7 -5
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +3 -3
- package/dist/cerebras/index.js +7 -5
- package/dist/cerebras/index.js.map +1 -1
- package/dist/{chunk-WU4U6IHF.js → chunk-6QCV4WXF.js} +4 -13
- package/dist/chunk-6QCV4WXF.js.map +1 -0
- package/dist/{chunk-5XPRVUOK.js → chunk-AC3VHSZJ.js} +2 -2
- package/dist/{chunk-5XPRVUOK.js.map → chunk-AC3VHSZJ.js.map} +1 -1
- package/dist/{chunk-ZDYEDI2A.js → chunk-CWGTARDE.js} +2 -2
- package/dist/{chunk-KNBODIQU.js → chunk-DI47UY2H.js} +2 -2
- package/dist/{chunk-KNBODIQU.js.map → chunk-DI47UY2H.js.map} +1 -1
- package/dist/{chunk-IDZR4ROP.js → chunk-EHR3LIPS.js} +2 -2
- package/dist/{chunk-IDZR4ROP.js.map → chunk-EHR3LIPS.js.map} +1 -1
- package/dist/chunk-EY2LLDGY.js +94 -0
- package/dist/chunk-EY2LLDGY.js.map +1 -0
- package/dist/{chunk-MJI74VEJ.js → chunk-F5ENANMJ.js} +18 -2
- package/dist/chunk-F5ENANMJ.js.map +1 -0
- package/dist/chunk-IKJH5ZSJ.js +1 -0
- package/dist/chunk-IKJH5ZSJ.js.map +1 -0
- package/dist/{chunk-IIMTP3XC.js → chunk-KBI45OXI.js} +2 -2
- package/dist/{chunk-SAMIK4WZ.js → chunk-KVUOTFYZ.js} +2 -2
- package/dist/{chunk-U6M3MXNI.js → chunk-L6QWKFGE.js} +3 -2
- package/dist/chunk-L6QWKFGE.js.map +1 -0
- package/dist/{chunk-RDC5GYST.js → chunk-N4LAFGLX.js} +7 -7
- package/dist/{chunk-ZKNPQBIE.js → chunk-R3T2IYOU.js} +5 -3
- package/dist/{chunk-ZKNPQBIE.js.map → chunk-R3T2IYOU.js.map} +1 -1
- package/dist/chunk-U2G5PHHL.js +25 -0
- package/dist/chunk-U2G5PHHL.js.map +1 -0
- package/dist/{chunk-SBGZJVTJ.js → chunk-VQZPADW6.js} +100 -33
- package/dist/chunk-VQZPADW6.js.map +1 -0
- package/dist/{chunk-O32SBS6S.js → chunk-XTWBAL42.js} +2 -2
- package/dist/{chunk-O32SBS6S.js.map → chunk-XTWBAL42.js.map} +1 -1
- package/dist/{chunk-WNB5PSY6.js → chunk-ZMESKGUY.js} +2 -2
- package/dist/{chunk-7ULSRWDH.js → chunk-ZSZVWLGE.js} +2 -2
- package/dist/{embedding-iNQCeXfk.d.ts → embedding-ts1npsDg.d.ts} +1 -1
- package/dist/google/index.d.ts +38 -4
- package/dist/google/index.js +5 -4
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +3 -3
- package/dist/groq/index.js +7 -5
- package/dist/groq/index.js.map +1 -1
- package/dist/http/index.d.ts +5 -5
- package/dist/http/index.js +19 -22
- package/dist/{image-stream-ARno6XlS.d.ts → image-stream-BPml2YZZ.d.ts} +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +306 -112
- package/dist/index.js.map +1 -1
- package/dist/{llm-CZqlijjK.d.ts → llm-BWLaTzzY.d.ts} +75 -29
- package/dist/middleware/logging/index.d.ts +3 -3
- package/dist/middleware/logging/index.js +3 -0
- package/dist/middleware/logging/index.js.map +1 -1
- package/dist/middleware/parsed-object/index.d.ts +3 -3
- package/dist/middleware/parsed-object/index.js +5 -1
- package/dist/middleware/parsed-object/index.js.map +1 -1
- package/dist/middleware/persistence/index.d.ts +3 -3
- package/dist/middleware/persistence/index.js +3 -2
- package/dist/middleware/persistence/index.js.map +1 -1
- package/dist/middleware/pipeline/index.d.ts +195 -0
- package/dist/middleware/pipeline/index.js +61 -0
- package/dist/middleware/pipeline/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +13 -11
- package/dist/middleware/pubsub/index.js +31 -5
- package/dist/middleware/pubsub/index.js.map +1 -1
- package/dist/middleware/pubsub/server/express/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/express/index.js +2 -2
- package/dist/middleware/pubsub/server/fastify/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/fastify/index.js +2 -2
- package/dist/middleware/pubsub/server/h3/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/h3/index.js +2 -2
- package/dist/middleware/pubsub/server/index.d.ts +50 -9
- package/dist/middleware/pubsub/server/index.js +5 -5
- package/dist/middleware/pubsub/server/index.js.map +1 -1
- package/dist/middleware/pubsub/server/webapi/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/webapi/index.js +2 -2
- package/dist/moonshot/index.d.ts +3 -3
- package/dist/moonshot/index.js +7 -5
- package/dist/moonshot/index.js.map +1 -1
- package/dist/ollama/index.d.ts +24 -4
- package/dist/ollama/index.js +5 -4
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +65 -4
- package/dist/openai/index.js +7 -5
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +4 -4
- package/dist/openrouter/index.js +7 -5
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +5 -5
- package/dist/proxy/index.js +16 -15
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +8 -9
- package/dist/proxy/server/express/index.js +4 -3
- package/dist/proxy/server/fastify/index.d.ts +8 -9
- package/dist/proxy/server/fastify/index.js +4 -3
- package/dist/proxy/server/h3/index.d.ts +8 -9
- package/dist/proxy/server/h3/index.js +4 -3
- package/dist/proxy/server/index.d.ts +5 -5
- package/dist/proxy/server/index.js +14 -13
- package/dist/proxy/server/webapi/index.d.ts +8 -9
- package/dist/proxy/server/webapi/index.js +4 -3
- package/dist/responses/index.d.ts +3 -3
- package/dist/responses/index.js +7 -5
- package/dist/responses/index.js.map +1 -1
- package/dist/retry-DVfdPLIB.d.ts +322 -0
- package/dist/{stream-DVVUIKpz.d.ts → stream-bBd_4Ipu.d.ts} +27 -4
- package/dist/{tool-D22EhP5F.d.ts → tool-BmAfKNBq.d.ts} +1 -1
- package/dist/{types-CyXF0J7C.d.ts → types-nTwlpyJE.d.ts} +13 -1
- package/dist/utils/index.d.ts +66 -2
- package/dist/utils/index.js +13 -0
- package/dist/xai/index.d.ts +3 -3
- package/dist/xai/index.js +7 -5
- package/dist/xai/index.js.map +1 -1
- package/package.json +6 -1
- package/dist/chunk-ARVM24K2.js +0 -128
- package/dist/chunk-ARVM24K2.js.map +0 -1
- package/dist/chunk-MJI74VEJ.js.map +0 -1
- package/dist/chunk-SBGZJVTJ.js.map +0 -1
- package/dist/chunk-U6M3MXNI.js.map +0 -1
- package/dist/chunk-WU4U6IHF.js.map +0 -1
- package/dist/chunk-Y5H7C5J4.js +0 -263
- package/dist/chunk-Y5H7C5J4.js.map +0 -1
- package/dist/retry-C1eJbEMV.d.ts +0 -531
- /package/dist/{chunk-ZDYEDI2A.js.map → chunk-CWGTARDE.js.map} +0 -0
- /package/dist/{chunk-IIMTP3XC.js.map → chunk-KBI45OXI.js.map} +0 -0
- /package/dist/{chunk-SAMIK4WZ.js.map → chunk-KVUOTFYZ.js.map} +0 -0
- /package/dist/{chunk-RDC5GYST.js.map → chunk-N4LAFGLX.js.map} +0 -0
- /package/dist/{chunk-WNB5PSY6.js.map → chunk-ZMESKGUY.js.map} +0 -0
- /package/dist/{chunk-7ULSRWDH.js.map → chunk-ZSZVWLGE.js.map} +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ErrorCode,
|
|
3
|
+
UPPError
|
|
4
|
+
} from "./chunk-COS4ON4G.js";
|
|
5
|
+
|
|
6
|
+
// src/http/keys.ts
|
|
7
|
+
function roundRobinKeys(keys) {
|
|
8
|
+
if (keys.length === 0) {
|
|
9
|
+
throw new Error("roundRobinKeys requires at least one key");
|
|
10
|
+
}
|
|
11
|
+
const snapshot = [...keys];
|
|
12
|
+
let index = 0;
|
|
13
|
+
return {
|
|
14
|
+
getKey() {
|
|
15
|
+
const key = snapshot[index];
|
|
16
|
+
index = (index + 1) % snapshot.length;
|
|
17
|
+
return key;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function weightedKeys(keys) {
|
|
22
|
+
if (keys.length === 0) {
|
|
23
|
+
throw new Error("weightedKeys requires at least one key");
|
|
24
|
+
}
|
|
25
|
+
const snapshot = keys.map((k) => ({ ...k }));
|
|
26
|
+
const totalWeight = snapshot.reduce((sum, k) => sum + k.weight, 0);
|
|
27
|
+
if (totalWeight <= 0) {
|
|
28
|
+
throw new Error("weightedKeys requires at least one key with a positive weight");
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
getKey() {
|
|
32
|
+
const random = Math.random() * totalWeight;
|
|
33
|
+
let cumulative = 0;
|
|
34
|
+
for (const entry of snapshot) {
|
|
35
|
+
cumulative += entry.weight;
|
|
36
|
+
if (random <= cumulative) {
|
|
37
|
+
return entry.key;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return snapshot[snapshot.length - 1].key;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function dynamicKey(selector) {
|
|
45
|
+
return {
|
|
46
|
+
async getKey() {
|
|
47
|
+
return selector();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function maskApiKey(key) {
|
|
52
|
+
if (key.length <= 8) {
|
|
53
|
+
return "***";
|
|
54
|
+
}
|
|
55
|
+
return `${key.slice(0, 4)}...${key.slice(-4)}`;
|
|
56
|
+
}
|
|
57
|
+
function isKeyStrategy(value) {
|
|
58
|
+
return typeof value === "object" && value !== null && "getKey" in value && typeof value.getKey === "function";
|
|
59
|
+
}
|
|
60
|
+
async function resolveApiKey(config, envVar, provider = "unknown", modality = "llm") {
|
|
61
|
+
const { apiKey } = config;
|
|
62
|
+
if (apiKey !== void 0) {
|
|
63
|
+
if (typeof apiKey === "string") {
|
|
64
|
+
return apiKey;
|
|
65
|
+
}
|
|
66
|
+
if (typeof apiKey === "function") {
|
|
67
|
+
return apiKey();
|
|
68
|
+
}
|
|
69
|
+
if (isKeyStrategy(apiKey)) {
|
|
70
|
+
return apiKey.getKey();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (envVar) {
|
|
74
|
+
const envValue = process.env[envVar];
|
|
75
|
+
if (envValue) {
|
|
76
|
+
return envValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
throw new UPPError(
|
|
80
|
+
envVar ? `API key not found. Set ${envVar} environment variable or provide apiKey in config.` : "API key not found. Provide apiKey in config.",
|
|
81
|
+
ErrorCode.AuthenticationFailed,
|
|
82
|
+
provider,
|
|
83
|
+
modality
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export {
|
|
88
|
+
roundRobinKeys,
|
|
89
|
+
weightedKeys,
|
|
90
|
+
dynamicKey,
|
|
91
|
+
maskApiKey,
|
|
92
|
+
resolveApiKey
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=chunk-EY2LLDGY.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/http/keys.ts"],"sourcesContent":["/**\n * API key management strategies for load balancing and dynamic key selection.\n * @module http/keys\n */\n\nimport type { ProviderConfig, KeyStrategy } from '../types/provider.ts';\nimport { ErrorCode, UPPError, type Modality } from '../types/errors.ts';\n\n/**\n * Creates a key strategy that distributes API requests across multiple keys\n * using round-robin selection.\n *\n * Each call to `getKey()` returns the next key in sequence, cycling back to\n * the first key after reaching the end. This provides even distribution of requests\n * across all available keys, which is useful for:\n * - Spreading rate limits across multiple API keys\n * - Load balancing between different accounts\n * - Maximizing throughput when multiple keys are available\n *\n * @param keys - Array of API keys to rotate through\n * @returns A {@link KeyStrategy} that cycles through the provided keys\n * @throws {Error} When the keys array is empty\n *\n * @example\n * ```typescript\n * const keys = roundRobinKeys([\n * 'sk-key-1',\n * 'sk-key-2',\n * 'sk-key-3'\n * ]);\n *\n * keys.getKey(); // Returns 'sk-key-1'\n * keys.getKey(); // Returns 'sk-key-2'\n * keys.getKey(); // Returns 'sk-key-3'\n * keys.getKey(); // Returns 'sk-key-1' (cycles back)\n * ```\n */\nexport function roundRobinKeys(keys: string[]): KeyStrategy {\n if (keys.length === 0) {\n throw new Error('roundRobinKeys requires at least one key');\n }\n const snapshot = [...keys];\n let index = 0;\n return {\n getKey(): string {\n const key = snapshot[index]!;\n index = (index + 1) % snapshot.length;\n return key;\n },\n };\n}\n\n/**\n * Creates a key strategy that selects API keys using weighted random probability.\n *\n * Each key is assigned a weight that determines its probability of being selected.\n * Higher weights mean higher selection probability. This is useful for:\n * - Preferring higher-tier API keys with better rate limits\n * - Gradually migrating traffic between old and new keys\n * - A/B testing different API accounts\n * - Directing more traffic to keys with higher quotas\n *\n * The selection probability for each key is: weight / totalWeight\n *\n * @param keys - Array of key-weight pairs defining selection probabilities\n * @returns A {@link KeyStrategy} that selects keys by weighted probability\n * @throws {Error} When the keys array is empty\n *\n * @example\n * ```typescript\n * const keys = weightedKeys([\n * { key: 'sk-premium', weight: 70 }, // 70% of requests\n * { key: 'sk-standard', weight: 20 }, // 20% of requests\n * { key: 'sk-backup', weight: 10 } // 10% of requests\n * ]);\n *\n * // Configure provider with weighted key selection\n * const provider = createOpenAI({\n * apiKey: keys\n * });\n * ```\n */\nexport function weightedKeys(keys: Array<{ key: string; weight: number }>): KeyStrategy {\n if (keys.length === 0) {\n throw new Error('weightedKeys requires at least one key');\n }\n const snapshot = keys.map((k) => ({ ...k }));\n const totalWeight = snapshot.reduce((sum, k) => sum + k.weight, 0);\n if (totalWeight <= 0) {\n throw new Error('weightedKeys requires at least one key with a positive weight');\n }\n return {\n getKey(): string {\n const random = Math.random() * totalWeight;\n let cumulative = 0;\n\n for (const entry of snapshot) {\n cumulative += entry.weight;\n if (random <= cumulative) {\n return entry.key;\n }\n }\n\n return snapshot[snapshot.length - 1]!.key;\n },\n };\n}\n\n/**\n * Creates a key strategy that delegates key selection to a user-provided function,\n * enabling advanced scenarios such as:\n * - Fetching keys from a secrets manager (AWS Secrets Manager, HashiCorp Vault)\n * - Rotating keys based on external state or configuration\n * - Selecting keys based on request context or time of day\n * - Implementing custom load balancing algorithms\n *\n * The selector function can be synchronous or asynchronous.\n *\n * @param selector - Function that returns an API key (sync or async)\n * @returns A {@link KeyStrategy} that delegates to the selector function\n *\n * @example\n * ```typescript\n * // Fetch key from environment based on current mode\n * const envKey = dynamicKey(() => {\n * return process.env.NODE_ENV === 'production'\n * ? process.env.PROD_API_KEY!\n * : process.env.DEV_API_KEY!;\n * });\n *\n * // Async key fetching from a secrets manager\n * const vaultKey = dynamicKey(async () => {\n * const secret = await vault.read('secret/openai');\n * return secret.data.apiKey;\n * });\n *\n * // Time-based key rotation\n * const timedKey = dynamicKey(() => {\n * const hour = new Date().getHours();\n * return hour < 12 ? morningKey : afternoonKey;\n * });\n * ```\n */\nexport function dynamicKey(selector: () => string | Promise<string>): KeyStrategy {\n return {\n async getKey(): Promise<string> {\n return selector();\n },\n };\n}\n\n/**\n * Masks an API key for safe logging.\n * Shows first 4 and last 4 characters with ellipsis, or '***' for short keys.\n *\n * @param key - The API key to mask\n * @returns Masked key like \"sk-ab...yz12\" or \"***\" for short keys\n *\n * @example\n * ```typescript\n * maskApiKey('sk-abc123def456xyz789'); // 'sk-a...z789'\n * maskApiKey('short'); // '***'\n * ```\n */\nexport function maskApiKey(key: string): string {\n if (key.length <= 8) {\n return '***';\n }\n return `${key.slice(0, 4)}...${key.slice(-4)}`;\n}\n\n/**\n * Type guard to check if a value implements the KeyStrategy interface.\n *\n * @param value - The value to check\n * @returns True if the value has a getKey method\n */\nfunction isKeyStrategy(value: unknown): value is KeyStrategy {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'getKey' in value &&\n typeof (value as KeyStrategy).getKey === 'function'\n );\n}\n\n/**\n * Resolves an API key from provider configuration with multiple fallback options.\n *\n * This function handles various key specification methods in priority order:\n * 1. Direct string key in config.apiKey\n * 2. Function returning a key (sync or async) in config.apiKey\n * 3. KeyStrategy instance in config.apiKey (roundRobinKeys, weightedKeys, dynamicKey)\n * 4. Environment variable fallback (if envVar parameter is provided)\n *\n * @param config - Provider configuration containing the apiKey option\n * @param envVar - Optional environment variable name to check as fallback\n * @param provider - Provider identifier for error context (default: 'unknown')\n * @param modality - Request modality for error context (default: 'llm')\n * @returns The resolved API key string\n *\n * @throws {UPPError} AUTHENTICATION_FAILED - When no valid key is found\n *\n * @example\n * ```typescript\n * // Direct key in config\n * const key1 = await resolveApiKey({ apiKey: 'sk-...' }, 'OPENAI_API_KEY', 'openai');\n *\n * // Function-based key\n * const key2 = await resolveApiKey({ apiKey: () => getKeyFromVault() }, undefined, 'anthropic');\n *\n * // KeyStrategy instance\n * const key3 = await resolveApiKey({\n * apiKey: roundRobinKeys(['sk-1', 'sk-2', 'sk-3'])\n * }, 'OPENAI_API_KEY', 'openai');\n *\n * // Environment variable fallback\n * const key4 = await resolveApiKey({}, 'ANTHROPIC_API_KEY', 'anthropic');\n * ```\n */\nexport async function resolveApiKey(\n config: ProviderConfig,\n envVar?: string,\n provider = 'unknown',\n modality: Modality = 'llm'\n): Promise<string> {\n const { apiKey } = config;\n\n if (apiKey !== undefined) {\n if (typeof apiKey === 'string') {\n return apiKey;\n }\n\n if (typeof apiKey === 'function') {\n return apiKey();\n }\n\n if (isKeyStrategy(apiKey)) {\n return apiKey.getKey();\n }\n }\n\n if (envVar) {\n const envValue = process.env[envVar];\n if (envValue) {\n return envValue;\n }\n }\n\n throw new UPPError(\n envVar\n ? `API key not found. Set ${envVar} environment variable or provide apiKey in config.`\n : 'API key not found. Provide apiKey in config.',\n ErrorCode.AuthenticationFailed,\n provider,\n modality\n );\n}\n"],"mappings":";;;;;;AAqCO,SAAS,eAAe,MAA6B;AAC1D,MAAI,KAAK,W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WACX,WAAqB,OACJ;AACjB,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI,WAAW,QAAW;AACxB,QAAI,OAAO,WAAW,UAAU;AAC9B,aAAO;AAAA,IACT;AAEA,QAAI,OAAO,WAAW,YAAY;AAChC,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,cAAc,MAAM,GAAG;AACzB,aAAO,OAAO,OAAO;AAAA,IACvB;AAAA,EACF;AAEA,MAAI,QAAQ;AACV,UAAM,WAAW,QAAQ,IAAI,MAAM;AACnC,QAAI,UAAU;AACZ,aAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,IAAI;AAAA,IACR,SACI,0BAA0B,MAAM,uDAChC;AAAA,IACJ,UAAU;AAAA,IACV;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -18,6 +18,8 @@ var StreamEventType = {
|
|
|
18
18
|
ToolExecutionStart: "tool_execution_start",
|
|
19
19
|
/** Tool execution has completed */
|
|
20
20
|
ToolExecutionEnd: "tool_execution_end",
|
|
21
|
+
/** Stream is being retried after an error */
|
|
22
|
+
StreamRetry: "stream_retry",
|
|
21
23
|
/** Beginning of a message */
|
|
22
24
|
MessageStart: "message_start",
|
|
23
25
|
/** End of a message */
|
|
@@ -137,6 +139,19 @@ function toolExecutionEnd(toolCallId, toolName, result, isError, timestamp, inde
|
|
|
137
139
|
delta: { toolCallId, toolName, result, isError, timestamp }
|
|
138
140
|
};
|
|
139
141
|
}
|
|
142
|
+
function streamRetry(attempt, maxAttempts, error, timestamp) {
|
|
143
|
+
const serializedError = {
|
|
144
|
+
message: error.message
|
|
145
|
+
};
|
|
146
|
+
if ("code" in error && typeof error.code === "string") {
|
|
147
|
+
serializedError.code = error.code;
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
type: StreamEventType.StreamRetry,
|
|
151
|
+
index: 0,
|
|
152
|
+
delta: { attempt, maxAttempts, error: serializedError, timestamp }
|
|
153
|
+
};
|
|
154
|
+
}
|
|
140
155
|
|
|
141
156
|
export {
|
|
142
157
|
StreamEventType,
|
|
@@ -149,6 +164,7 @@ export {
|
|
|
149
164
|
contentBlockStart,
|
|
150
165
|
contentBlockStop,
|
|
151
166
|
toolExecutionStart,
|
|
152
|
-
toolExecutionEnd
|
|
167
|
+
toolExecutionEnd,
|
|
168
|
+
streamRetry
|
|
153
169
|
};
|
|
154
|
-
//# sourceMappingURL=chunk-
|
|
170
|
+
//# sourceMappingURL=chunk-F5ENANMJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types/stream.ts"],"sourcesContent":["/**\n * @fileoverview Streaming types for real-time LLM responses.\n *\n * Defines the event types and interfaces for streaming LLM inference,\n * including text deltas, tool call deltas, and control events.\n *\n * @module types/stream\n */\n\nimport type { Turn } from './turn.ts';\n\n/**\n * Stream event type constants.\n *\n * Use these constants instead of raw strings for type-safe event handling:\n *\n * @example\n * ```typescript\n * import { StreamEventType } from 'upp';\n *\n * for await (const event of stream) {\n * if (event.type === StreamEventType.TextDelta) {\n * process.stdout.write(event.delta.text ?? '');\n * }\n * }\n * ```\n */\nexport const StreamEventType = {\n /** Incremental text output */\n TextDelta: 'text_delta',\n /** Incremental reasoning/thinking output */\n ReasoningDelta: 'reasoning_delta',\n /** Incremental image data */\n ImageDelta: 'image_delta',\n /** Incremental audio data */\n AudioDelta: 'audio_delta',\n /** Incremental video data */\n VideoDelta: 'video_delta',\n /** Incremental tool call data (arguments being streamed) */\n ToolCallDelta: 'tool_call_delta',\n /** Incremental structured object data (for structured output responses) */\n ObjectDelta: 'object_delta',\n /** Tool execution has started (may be emitted after completion in some implementations) */\n ToolExecutionStart: 'tool_execution_start',\n /** Tool execution has completed */\n ToolExecutionEnd: 'tool_execution_end',\n /** Stream is being retried after an error */\n StreamRetry: 'stream_retry',\n /** Beginning of a message */\n MessageStart: 'message_start',\n /** End of a message */\n MessageStop: 'message_stop',\n /** Beginning of a content block */\n ContentBlockStart: 'content_block_start',\n /** End of a content block */\n ContentBlockStop: 'content_block_stop',\n} as const;\n\n/**\n * Stream event type discriminator union.\n *\n * This type is derived from {@link StreamEventType} constants. Use `StreamEventType.TextDelta`\n * for constants or `type MyType = StreamEventType` for type annotations.\n */\nexport type StreamEventType = (typeof StreamEventType)[keyof typeof StreamEventType];\n\n/**\n * Event delta data payload.\n *\n * Contains the type-specific data for a streaming event.\n * Different fields are populated depending on the event type:\n *\n * | Event Type | Fields |\n * |------------|--------|\n * | `text_delta` | `text` |\n * | `reasoning_delta` | `text` |\n * | `object_delta` | `text` |\n * | `image_delta` | `data` |\n * | `audio_delta` | `data` |\n * | `video_delta` | `data` |\n * | `tool_call_delta` | `toolCallId`, `toolName`, `argumentsJson` |\n * | `tool_execution_start` | `toolCallId`, `toolName`, `timestamp` |\n * | `tool_execution_end` | `toolCallId`, `toolName`, `result`, `isError`, `timestamp` |\n * | `stream_retry` | `attempt`, `maxAttempts`, `error`, `timestamp` |\n * | `message_start` | (none) |\n * | `message_stop` | (none) |\n * | `content_block_start` | (none) |\n * | `content_block_stop` | (none) |\n *\n * Custom event types (via middleware) may extend EventDelta with additional fields.\n * See {@link @providerprotocol/ai/middleware/flow!FlowStageDelta} for an example.\n */\nexport interface EventDelta {\n /** Incremental text content (text_delta, reasoning_delta, object_delta) */\n text?: string;\n\n /** Incremental binary data (image_delta, audio_delta, video_delta) */\n data?: Uint8Array;\n\n /** Tool call identifier (tool_call_delta, tool_execution_start/end) */\n toolCallId?: string;\n\n /** Tool name (tool_call_delta, tool_execution_start/end) */\n toolName?: string;\n\n /** Incremental JSON arguments string (tool_call_delta) */\n argumentsJson?: string;\n\n /** Tool execution result (tool_execution_end) */\n result?: unknown;\n\n /** Whether tool execution resulted in an error (tool_execution_end) */\n isError?: boolean;\n\n /** Timestamp in milliseconds (tool_execution_start/end, stream_retry) */\n timestamp?: number;\n\n /** Current retry attempt number (stream_retry, 1-indexed) */\n attempt?: number;\n\n /** Maximum number of retry attempts configured (stream_retry) */\n maxAttempts?: number;\n\n /** Error that triggered the retry (stream_retry) - serialized for JSON transport */\n error?: { message: string; code?: string };\n}\n\n/**\n * A single streaming event from the LLM.\n *\n * Events are emitted in order as the model generates output,\n * allowing for real-time display of responses.\n *\n * @example\n * ```typescript\n * import { StreamEventType } from 'upp';\n *\n * for await (const event of stream) {\n * if (event.type === StreamEventType.TextDelta) {\n * process.stdout.write(event.delta.text ?? '');\n * } else if (event.type === StreamEventType.ToolCallDelta) {\n * console.log('Tool:', event.delta.toolName);\n * }\n * }\n * ```\n */\nexport interface StreamEvent {\n /**\n * Event type discriminator.\n *\n * Uses `StreamEventType | (string & Record<never, never>)` to allow custom\n * event types (like 'flow_stage') while preserving autocomplete for known types.\n * The `(string & Record<never, never>)` pattern is a TypeScript idiom that\n * widens the type to accept any string without losing the literal type union\n * in IDE autocomplete suggestions.\n */\n type: StreamEventType | (string & Record<never, never>);\n\n /** Index of the content block this event belongs to */\n index: number;\n\n /** Event-specific data payload */\n delta: EventDelta;\n}\n\n/**\n * Stream result - an async iterable that also provides the final turn.\n *\n * Allows consuming streaming events while also awaiting the complete\n * Turn result after streaming finishes. Implements `PromiseLike<Turn>`\n * for direct awaiting with automatic stream consumption.\n *\n * @typeParam TData - Type of the structured output data\n *\n * @example\n * ```typescript\n * import { StreamEventType } from 'upp';\n *\n * const stream = instance.stream('Tell me a story');\n *\n * // Option 1: Consume streaming events manually\n * for await (const event of stream) {\n * if (event.type === StreamEventType.TextDelta) {\n * process.stdout.write(event.delta.text ?? '');\n * }\n * }\n * const turn = await stream.turn;\n *\n * // Option 2: Just await the turn (auto-drains the stream)\n * const turn = await instance.stream('Tell me a story');\n *\n * // Option 3: Fire-and-forget with callback\n * instance.stream('Tell me a story').then(turn => saveToDB(turn));\n * ```\n */\nexport interface StreamResult<TData = unknown>\n extends AsyncIterable<StreamEvent>, PromiseLike<Turn<TData>> {\n /**\n * Promise that resolves to the complete Turn after streaming finishes.\n * Rejects if the stream is aborted or terminated early.\n *\n * Accessing `turn` auto-drains the stream if it has not been iterated yet.\n */\n readonly turn: Promise<Turn<TData>>;\n\n /**\n * Aborts the stream, stopping further events and cancelling the request.\n * This will cause {@link StreamResult.turn} to reject.\n */\n abort(): void;\n}\n\n/**\n * Creates a StreamResult from an async generator and completion promise.\n *\n * @typeParam TData - Type of the structured output data\n * @param generator - Async generator that yields stream events\n * @param turnPromiseOrFactory - Promise or factory that resolves to the complete Turn\n * @param abortController - Controller for aborting the stream\n * @returns A StreamResult that can be iterated and awaited\n *\n * @example\n * ```typescript\n * const abortController = new AbortController();\n * const stream = createStreamResult(\n * eventGenerator(),\n * turnPromise,\n * abortController\n * );\n *\n * // Can be awaited directly (auto-drains)\n * const turn = await stream;\n *\n * // Or iterated manually\n * for await (const event of stream) { ... }\n * const turn = await stream.turn;\n * ```\n */\nexport function createStreamResult<TData = unknown>(\n generator: AsyncGenerator<StreamEvent, void, unknown>,\n turnPromiseOrFactory: Promise<Turn<TData>> | (() => Promise<Turn<TData>>),\n abortController: AbortController\n): StreamResult<TData> {\n let cachedTurn: Promise<Turn<TData>> | null = null;\n let drainStarted = false;\n let iteratorStarted = false;\n\n const getTurn = (): Promise<Turn<TData>> => {\n if (typeof turnPromiseOrFactory === 'function') {\n if (!cachedTurn) {\n cachedTurn = turnPromiseOrFactory();\n }\n return cachedTurn;\n }\n return turnPromiseOrFactory;\n };\n\n const drain = (): void => {\n if (drainStarted) return;\n drainStarted = true;\n void (async () => {\n try {\n let done = false;\n while (!done) {\n const result = await generator.next();\n done = result.done ?? false;\n }\n } catch {\n // Errors are surfaced via turn promise\n }\n })();\n };\n\n return {\n [Symbol.asyncIterator]() {\n iteratorStarted = true;\n return generator;\n },\n get turn() {\n if (!iteratorStarted) {\n drain();\n }\n return getTurn();\n },\n abort() {\n abortController.abort();\n },\n then<TResult1 = Turn<TData>, TResult2 = never>(\n onfulfilled?: ((value: Turn<TData>) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null\n ): Promise<TResult1 | TResult2> {\n drain();\n return getTurn().then(onfulfilled, onrejected);\n },\n };\n}\n\n/**\n * Creates a text delta stream event.\n *\n * @param text - The incremental text content\n * @param index - Content block index (default: 0)\n * @returns A text_delta StreamEvent\n */\nexport function textDelta(text: string, index = 0): StreamEvent {\n return {\n type: StreamEventType.TextDelta,\n index,\n delta: { text },\n };\n}\n\n/**\n * Creates a tool call delta stream event.\n *\n * @param toolCallId - Unique identifier for the tool call\n * @param toolName - Name of the tool being called\n * @param argumentsJson - Incremental JSON arguments string\n * @param index - Content block index (default: 0)\n * @returns A tool_call_delta StreamEvent\n */\nexport function toolCallDelta(\n toolCallId: string,\n toolName: string,\n argumentsJson: string,\n index = 0\n): StreamEvent {\n return {\n type: StreamEventType.ToolCallDelta,\n index,\n delta: { toolCallId, toolName, argumentsJson },\n };\n}\n\n/**\n * Creates an object delta stream event for structured output responses.\n *\n * @param text - The incremental text content\n * @param index - Content block index (default: 0)\n * @returns An object_delta StreamEvent\n */\nexport function objectDelta(text: string, index = 0): StreamEvent {\n return {\n type: StreamEventType.ObjectDelta,\n index,\n delta: { text },\n };\n}\n\n/**\n * Creates a message start stream event.\n *\n * @returns A message_start StreamEvent\n */\nexport function messageStart(): StreamEvent {\n return {\n type: StreamEventType.MessageStart,\n index: 0,\n delta: {},\n };\n}\n\n/**\n * Creates a message stop stream event.\n *\n * @returns A message_stop StreamEvent\n */\nexport function messageStop(): StreamEvent {\n return {\n type: StreamEventType.MessageStop,\n index: 0,\n delta: {},\n };\n}\n\n/**\n * Creates a content block start stream event.\n *\n * @param index - The content block index starting\n * @returns A content_block_start StreamEvent\n */\nexport function contentBlockStart(index: number): StreamEvent {\n return {\n type: StreamEventType.ContentBlockStart,\n index,\n delta: {},\n };\n}\n\n/**\n * Creates a content block stop stream event.\n *\n * @param index - The content block index stopping\n * @returns A content_block_stop StreamEvent\n */\nexport function contentBlockStop(index: number): StreamEvent {\n return {\n type: StreamEventType.ContentBlockStop,\n index,\n delta: {},\n };\n}\n\n/**\n * Creates a tool execution start stream event.\n *\n * @param toolCallId - Unique identifier for the tool call\n * @param toolName - Name of the tool being executed\n * @param timestamp - Start timestamp in milliseconds\n * @param index - Content block index (default: 0)\n * @returns A tool_execution_start StreamEvent\n */\nexport function toolExecutionStart(\n toolCallId: string,\n toolName: string,\n timestamp: number,\n index = 0\n): StreamEvent {\n return {\n type: StreamEventType.ToolExecutionStart,\n index,\n delta: { toolCallId, toolName, timestamp },\n };\n}\n\n/**\n * Creates a tool execution end stream event.\n *\n * @param toolCallId - Unique identifier for the tool call\n * @param toolName - Name of the tool that was executed\n * @param result - The result from the tool execution\n * @param isError - Whether the execution resulted in an error\n * @param timestamp - End timestamp in milliseconds\n * @param index - Content block index (default: 0)\n * @returns A tool_execution_end StreamEvent\n */\nexport function toolExecutionEnd(\n toolCallId: string,\n toolName: string,\n result: unknown,\n isError: boolean,\n timestamp: number,\n index = 0\n): StreamEvent {\n return {\n type: StreamEventType.ToolExecutionEnd,\n index,\n delta: { toolCallId, toolName, result, isError, timestamp },\n };\n}\n\n/**\n * Creates a stream retry event.\n *\n * Emitted when a streaming request is being retried after an error.\n * This allows consumers to reset UI state or notify users of retry attempts.\n *\n * @param attempt - Current retry attempt number (1-indexed)\n * @param maxAttempts - Maximum number of retry attempts configured\n * @param error - The error that triggered the retry\n * @param timestamp - Timestamp in milliseconds when retry was initiated\n * @returns A stream_retry StreamEvent\n */\nexport function streamRetry(\n attempt: number,\n maxAttempts: number,\n error: Error,\n timestamp: number\n): StreamEvent {\n // Serialize error for JSON transport (Error properties are non-enumerable)\n const serializedError: { message: string; code?: string } = {\n message: error.message,\n };\n // Include error code if present (e.g., UPPError)\n if ('code' in error && typeof error.code === 'string') {\n serializedError.code = error.code;\n }\n\n return {\n type: StreamEventType.StreamRetry,\n index: 0,\n delta: { attempt, maxAttempts, error: serializedError, timestamp },\n };\n}\n"],"mappings":";AA2BO,IAAM,kBAAkB;AAAA;AAAA,EAE7B,WAAW;AAAA;AAAA,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S,MAAM;AAAA,EACjB;AAEA,MAAI,UAAU,SAAS,OAAO,MAAM,SAAS,UAAU;AACrD,oBAAgB,OAAO,MAAM;AAAA,EAC/B;AAEA,SAAO;AAAA,IACL,MAAM,gBAAgB;AAAA,IACtB,OAAO;AAAA,IACP,OAAO,EAAE,SAAS,aAAa,OAAO,iBAAiB,UAAU;AAAA,EACnE;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-IKJH5ZSJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runSubscriberStream
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-L6QWKFGE.js";
|
|
4
4
|
|
|
5
5
|
// src/middleware/pubsub/server/h3.ts
|
|
6
6
|
async function streamSubscriber(streamId, adapter, event) {
|
|
@@ -28,4 +28,4 @@ export {
|
|
|
28
28
|
streamSubscriber,
|
|
29
29
|
h3
|
|
30
30
|
};
|
|
31
|
-
//# sourceMappingURL=chunk-
|
|
31
|
+
//# sourceMappingURL=chunk-KBI45OXI.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runSubscriberStream
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-L6QWKFGE.js";
|
|
4
4
|
|
|
5
5
|
// src/middleware/pubsub/server/express.ts
|
|
6
6
|
async function streamSubscriber(streamId, adapter, res) {
|
|
@@ -27,4 +27,4 @@ export {
|
|
|
27
27
|
streamSubscriber,
|
|
28
28
|
express
|
|
29
29
|
};
|
|
30
|
-
//# sourceMappingURL=chunk-
|
|
30
|
+
//# sourceMappingURL=chunk-KVUOTFYZ.js.map
|
|
@@ -79,11 +79,12 @@ async function runSubscriberStream(streamId, adapter, writer, options = {}) {
|
|
|
79
79
|
});
|
|
80
80
|
try {
|
|
81
81
|
const events = await adapter.getEvents(streamId);
|
|
82
|
+
const cursorBase = adapter.getCursorBase(streamId);
|
|
82
83
|
for (const event of events) {
|
|
83
84
|
if (signal?.aborted) break;
|
|
84
85
|
writer.write(formatSSE(event));
|
|
85
86
|
}
|
|
86
|
-
lastSentCursor = events.length - 1;
|
|
87
|
+
lastSentCursor = cursorBase + events.length - 1;
|
|
87
88
|
dropReplayDuplicates();
|
|
88
89
|
if (signal?.aborted) {
|
|
89
90
|
writer.end();
|
|
@@ -124,4 +125,4 @@ async function runSubscriberStream(streamId, adapter, writer, options = {}) {
|
|
|
124
125
|
export {
|
|
125
126
|
runSubscriberStream
|
|
126
127
|
};
|
|
127
|
-
//# sourceMappingURL=chunk-
|
|
128
|
+
//# sourceMappingURL=chunk-L6QWKFGE.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/middleware/pubsub/server/shared.ts"],"sourcesContent":["/**\n * @fileoverview Shared utilities for pub-sub server adapters.\n *\n * @module middleware/pubsub/server/shared\n * @internal\n */\n\nimport type { StreamEvent } from '../../../types/stream.ts';\nimport type { PubSubAdapter } from '../types.ts';\nimport { serializeStreamEvent } from '../../../stream/serialization.ts';\n\n/**\n * Writer interface for abstracting how data is written to responses.\n * @internal\n */\nexport interface StreamWriter {\n write(data: string): void;\n end(): void;\n}\n\n/**\n * Options for runSubscriberStream.\n * @internal\n */\nexport interface StreamOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Formats a stream event as an SSE data line.\n */\nexport function formatSSE(event: StreamEvent): string {\n const serialized = serializeStreamEvent(event);\n return `data: ${JSON.stringify(serialized)}\\n\\n`;\n}\n\n/**\n * Core subscriber stream logic shared across all adapters.\n *\n * Handles:\n * 1. Subscribing to live events and completion signal\n * 2. Replaying buffered events (empty if stream just started)\n * 3. Processing live events until completion signal\n * 4. Final cleanup\n * 5. Client disconnect via AbortSignal\n *\n * @internal\n */\nexport async function runSubscriberStream(\n streamId: string,\n adapter: PubSubAdapter,\n writer: StreamWriter,\n options: StreamOptions = {}\n): Promise<void> {\n const { signal } = options;\n\n if (signal?.aborted) {\n writer.end();\n return;\n }\n\n try {\n if (signal?.aborted) {\n writer.end();\n return;\n }\n\n const queue: Array<{ event: StreamEvent; cursor: number | null }> = [];\n let resolveWait: (() => void) | null = null;\n let completed = false;\n let lastSentCursor = -1;\n let finalData: unknown = undefined;\n\n const onEvent = (event: StreamEvent, cursor?: number): void => {\n queue.push({ event, cursor: cursor ?? null });\n resolveWait?.();\n };\n\n const onComplete = (): void => {\n completed = true;\n resolveWait?.();\n };\n\n const onFinalData = (data: unknown): void => {\n finalData = data;\n };\n\n const unsubscribe = adapter.subscribe(streamId, onEvent, onComplete, onFinalData);\n\n const onAbort = (): void => {\n completed = true;\n resolveWait?.();\n };\n signal?.addEventListener('abort', onAbort);\n\n const drainQueue = (): void => {\n while (queue.length > 0 && !signal?.aborted) {\n const item = queue.shift();\n if (!item) break;\n const { event, cursor } = item;\n if (cursor !== null && cursor <= lastSentCursor) continue;\n writer.write(formatSSE(event));\n if (cursor !== null && cursor > lastSentCursor) {\n lastSentCursor = cursor;\n }\n }\n };\n\n const dropReplayDuplicates = (): void => {\n if (queue.length === 0) return;\n const filtered: Array<{ event: StreamEvent; cursor: number | null }> = [];\n for (const item of queue) {\n if (item.cursor !== null && item.cursor <= lastSentCursor) continue;\n filtered.push(item);\n }\n queue.length = 0;\n queue.push(...filtered);\n };\n\n const waitForSignal = (): Promise<void> => new Promise((resolve) => {\n let settled = false;\n\n const settle = (): void => {\n if (settled) return;\n settled = true;\n resolveWait = null;\n resolve();\n };\n\n resolveWait = settle;\n\n if (completed || signal?.aborted || queue.length > 0) {\n settle();\n }\n });\n\n try {\n const events = await adapter.getEvents(streamId);\n const cursorBase = adapter.getCursorBase(streamId);\n\n for (const event of events) {\n if (signal?.aborted) break;\n writer.write(formatSSE(event));\n }\n\n // Use cursor base to set lastSentCursor correctly after clear operations\n // Events have cursors of cursorBase + index, so after replay the last cursor is cursorBase + length - 1\n lastSentCursor = cursorBase + events.length - 1;\n dropReplayDuplicates();\n\n if (signal?.aborted) {\n writer.end();\n return;\n }\n\n // Wait for events or completion signal\n while (!completed && !signal?.aborted) {\n drainQueue();\n if (completed || signal?.aborted) break;\n await waitForSignal();\n }\n\n if (!signal?.aborted) {\n drainQueue();\n }\n } finally {\n signal?.removeEventListener('abort', onAbort);\n unsubscribe();\n }\n\n if (!signal?.aborted) {\n // Emit final data (Turn) if available before [DONE]\n if (finalData !== undefined) {\n writer.write(`data: ${JSON.stringify(finalData)}\\n\\n`);\n }\n writer.write('data: [DONE]\\n\\n');\n }\n writer.end();\n } catch (error) {\n if (!signal?.aborted) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n writer.write(`data: ${JSON.stringify({ error: errorMsg })}\\n\\n`);\n }\n writer.end();\n }\n}\n"],"mappings":";;;;;AA+BO,SAAS,UAAU,OAA4B;AACpD,QAAM,aAAa,qBAAqB,KAAK;AAC7C,SAAO,SAAS,KAAK,UAAU,UAAU,CAAC;AAAA;AAAA;AAC5C;AAcA,eAAsB,oBACpB,UACA,SACA,QACA,UAAyB,CAAC,GACX;AACf,QAAM,EAAE,OAAO,IAAI;AAEnB,MAAI,QAAQ,SAAS;AACnB,WAAO,IAAI;AACX;AAAA,EACF;AAEA,MAAI;AACF,QAAI,QAAQ,SAAS;AACnB,aAAO,IAAI;AACX;AAAA,IACF;AAEA,UAAM,QAA8D,CAAC;AACrE,QAAI,cAAmC;AACvC,QAAI,YAAY;AAChB,QAAI,iBAAiB;AACrB,QAAI,YAAqB;AAEzB,UAAM,UAAU,CAAC,OAAoB,WAA0B;AAC7D,YAAM,KAAK,EAAE,OAAO,QAAQ,UAAU,KAAK,CAAC;AAC5C,oBAAc;AAAA,IAChB;AAEA,UAAM,aAAa,MAAY;AAC7B,kBAAY;AACZ,oBAAc;AAAA,IAChB;AAEA,UAAM,cAAc,CAAC,SAAwB;AAC3C,kBAAY;AAAA,IACd;AAEA,UAAM,cAAc,Q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AErB,UAAI,QAAQ,SAAS;AACnB,eAAO,IAAI;AACX;AAAA,MACF;AAGA,aAAO,CAAC,aAAa,CAAC,QAAQ,SAAS;AACrC,mBAAW;AACX,YAAI,aAAa,QAAQ,QAAS;AAClC,cAAM,cAAc;AAAA,MACtB;AAEA,UAAI,CAAC,QAAQ,SAAS;AACpB,mBAAW;AAAA,MACb;AAAA,IACF,UAAE;AACA,cAAQ,oBAAoB,SAAS,OAAO;AAC5C,kBAAY;AAAA,IACd;AAEA,QAAI,CAAC,QAAQ,SAAS;AAEpB,UAAI,cAAc,QAAW;AAC3B,eAAO,MAAM,SAAS,KAAK,UAAU,SAAS,CAAC;AAAA;AAAA,CAAM;AAAA,MACvD;AACA,aAAO,MAAM,kBAAkB;AAAA,IACjC;AACA,WAAO,IAAI;AAAA,EACb,SAAS,OAAO;AACd,QAAI,CAAC,QAAQ,SAAS;AACpB,YAAM,WAAW,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACtE,aAAO,MAAM,SAAS,KAAK,UAAU,EAAE,OAAO,SAAS,CAAC,CAAC;AAAA;AAAA,CAAM;AAAA,IACjE;AACA,WAAO,IAAI;AAAA,EACb;AACF;","names":[]}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
webapi
|
|
3
|
+
} from "./chunk-AC3VHSZJ.js";
|
|
1
4
|
import {
|
|
2
5
|
express
|
|
3
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-XTWBAL42.js";
|
|
4
7
|
import {
|
|
5
8
|
h3
|
|
6
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-DI47UY2H.js";
|
|
7
10
|
import {
|
|
8
11
|
fastify
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import {
|
|
11
|
-
webapi
|
|
12
|
-
} from "./chunk-5XPRVUOK.js";
|
|
12
|
+
} from "./chunk-EHR3LIPS.js";
|
|
13
13
|
|
|
14
14
|
// src/providers/proxy/server/index.ts
|
|
15
15
|
var server = {
|
|
@@ -26,4 +26,4 @@ var server = {
|
|
|
26
26
|
export {
|
|
27
27
|
server
|
|
28
28
|
};
|
|
29
|
-
//# sourceMappingURL=chunk-
|
|
29
|
+
//# sourceMappingURL=chunk-N4LAFGLX.js.map
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AssistantMessage,
|
|
3
3
|
ToolResultMessage,
|
|
4
|
-
UserMessage
|
|
4
|
+
UserMessage
|
|
5
|
+
} from "./chunk-6QCV4WXF.js";
|
|
6
|
+
import {
|
|
5
7
|
generateId
|
|
6
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-U2G5PHHL.js";
|
|
7
9
|
|
|
8
10
|
// src/types/thread.ts
|
|
9
11
|
var Thread = class _Thread {
|
|
@@ -262,4 +264,4 @@ var Thread = class _Thread {
|
|
|
262
264
|
export {
|
|
263
265
|
Thread
|
|
264
266
|
};
|
|
265
|
-
//# sourceMappingURL=chunk-
|
|
267
|
+
//# sourceMappingURL=chunk-R3T2IYOU.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types/thread.ts"],"sourcesContent":["/**\n * @fileoverview Thread class for managing conversation history.\n *\n * Provides a utility class for building and manipulating conversation\n * message sequences, with support for serialization and deserialization.\n *\n * @module types/thread\n */\n\nimport { generateId } from '../utils/id.ts';\nimport {\n Message,\n UserMessage,\n AssistantMessage,\n ToolResultMessage,\n type MessageJSON,\n type MessageType,\n} from './messages.ts';\nimport type { UserContent, AssistantContent } from './content.ts';\nimport type { Turn } from './turn.ts';\n\n/**\n * Thread serialized to JSON format.\n * Picks id from Thread, converts dates to strings.\n */\nexport type ThreadJSON = Pick<Thread, 'id'> & {\n messages: MessageJSON[];\n createdAt: string;\n updatedAt: string;\n};\n\n/**\n * Thread - A utility class for managing conversation history.\n *\n * Provides methods for building, manipulating, and persisting\n * conversation message sequences. This class is optional; users\n * can also manage their own `Message[]` arrays directly.\n *\n * @example\n * ```typescript\n * // Create a new thread and add messages\n * const thread = new Thread();\n * thread.user('Hello!');\n * thread.assistant('Hi there! How can I help?');\n *\n * // Use with LLM inference\n * const turn = await instance.generate(thread, 'What is 2+2?');\n * thread.append(turn);\n *\n * // Serialize for storage\n * const json = thread.toJSON();\n * localStorage.setItem('chat', JSON.stringify(json));\n *\n * // Restore from storage\n * const restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));\n * ```\n */\nexport class Thread {\n /** Unique thread identifier */\n readonly id: string;\n\n /** Internal message storage */\n private _messages: Message[];\n\n /** Creation timestamp */\n private _createdAt: Date;\n\n /** Last update timestamp */\n private _updatedAt: Date;\n\n /**\n * Creates a new thread instance.\n *\n * @param messages - Optional initial messages to populate the thread\n */\n constructor(messages?: Message[]) {\n this.id = generateId();\n this._messages = messages ? [...messages] : [];\n this._createdAt = new Date();\n this._updatedAt = new Date();\n }\n\n /**\n * All messages in the thread (readonly).\n */\n get messages(): readonly Message[] {\n return this._messages;\n }\n\n /**\n * Number of messages in the thread.\n */\n get length(): number {\n return this._messages.length;\n }\n\n /**\n * Appends all messages from a Turn to the thread.\n *\n * @param turn - The Turn containing messages to append\n * @returns This thread instance for chaining\n */\n append(turn: Turn): this {\n this._messages.push(...turn.messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Adds raw messages to the thread.\n *\n * @param messages - Messages to add\n * @returns This thread instance for chaining\n */\n push(...messages: Message[]): this {\n this._messages.push(...messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Adds a user message to the thread.\n *\n * @param content - String or array of content blocks\n * @returns This thread instance for chaining\n *\n * @example\n * ```typescript\n * thread.user('Hello, world!');\n * thread.user([\n * { type: 'text', text: 'Describe this image:' },\n * { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }\n * ]);\n * ```\n */\n user(content: string | UserContent[]): this {\n this._messages.push(new UserMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Adds an assistant message to the thread.\n *\n * @param content - String or array of content blocks\n * @returns This thread instance for chaining\n *\n * @example\n * ```typescript\n * thread.assistant('I can help with that!');\n * ```\n */\n assistant(content: string | AssistantContent[]): this {\n this._messages.push(new AssistantMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Filters messages by type.\n *\n * @param type - The message type to filter by\n * @returns Array of messages matching the type\n *\n * @example\n * ```typescript\n * const userMessages = thread.filter('user');\n * const assistantMessages = thread.filter('assistant');\n * ```\n */\n filter(type: MessageType): Message[] {\n return this._messages.filter((m) => m.type === type);\n }\n\n /**\n * Returns the last N messages from the thread.\n *\n * @param count - Number of messages to return\n * @returns Array of the last N messages\n *\n * @example\n * ```typescript\n * const recent = thread.tail(5);\n * ```\n */\n tail(count: number): Message[] {\n return this._messages.slice(-count);\n }\n\n /**\n * Creates a new thread with a subset of messages.\n *\n * @param start - Start index (inclusive)\n * @param end - End index (exclusive)\n * @returns New Thread containing the sliced messages\n *\n * @example\n * ```typescript\n * const subset = thread.slice(0, 10);\n * ```\n */\n slice(start?: number, end?: number): Thread {\n return new Thread(this._messages.slice(start, end));\n }\n\n /**\n * Removes all messages from the thread.\n *\n * @returns This thread instance for chaining\n */\n clear(): this {\n this._messages = [];\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Converts the thread to a plain message array.\n *\n * @returns Copy of the internal message array\n */\n toMessages(): Message[] {\n return [...this._messages];\n }\n\n /**\n * Serializes the thread to JSON format.\n *\n * @returns JSON-serializable representation of the thread\n *\n * @example\n * ```typescript\n * const json = thread.toJSON();\n * localStorage.setItem('thread', JSON.stringify(json));\n * ```\n */\n toJSON(): ThreadJSON {\n return {\n id: this.id,\n messages: this._messages.map((m) => this.messageToJSON(m)),\n createdAt: this._createdAt.toISOString(),\n updatedAt: this._updatedAt.toISOString(),\n };\n }\n\n /**\n * Deserializes a thread from JSON format.\n *\n * @param json - The JSON representation to deserialize\n * @returns Reconstructed Thread instance\n *\n * @example\n * ```typescript\n * const json = JSON.parse(localStorage.getItem('thread'));\n * const thread = Thread.fromJSON(json);\n * ```\n */\n static fromJSON(json: ThreadJSON): Thread {\n const messages = json.messages.map((m) => Thread.messageFromJSON(m));\n const thread = new Thread(messages);\n (thread as { id: string }).id = json.id;\n thread._createdAt = new Date(json.createdAt);\n thread._updatedAt = new Date(json.updatedAt);\n return thread;\n }\n\n /**\n * Enables iteration over messages with for...of loops.\n *\n * @returns Iterator over the thread's messages\n *\n * @example\n * ```typescript\n * for (const message of thread) {\n * console.log(message.text);\n * }\n * ```\n */\n [Symbol.iterator](): Iterator<Message> {\n return this._messages[Symbol.iterator]();\n }\n\n /**\n * Converts a message to JSON format.\n */\n private messageToJSON(m: Message): MessageJSON {\n const base: MessageJSON = {\n id: m.id,\n type: m.type,\n content: [],\n metadata: m.metadata,\n timestamp: m.timestamp.toISOString(),\n };\n\n if (m instanceof UserMessage) {\n base.content = m.content;\n } else if (m instanceof AssistantMessage) {\n base.content = m.content;\n base.toolCalls = m.toolCalls;\n } else if (m instanceof ToolResultMessage) {\n base.results = m.results;\n }\n\n return base;\n }\n\n /**\n * Reconstructs a message from JSON format.\n */\n private static messageFromJSON(json: MessageJSON): Message {\n const options = {\n id: json.id,\n metadata: json.metadata,\n timestamp: new Date(json.timestamp),\n };\n\n switch (json.type) {\n case 'user':\n return new UserMessage(json.content as UserContent[], options);\n case 'assistant':\n return new AssistantMessage(\n json.content as AssistantContent[],\n json.toolCalls,\n options\n );\n case 'tool_result':\n return new ToolResultMessage(json.results ?? [], options);\n default:\n throw new Error(`Unknown message type: ${json.type}`);\n }\n }\n}\n"],"mappings":";;;;;;;;AAyDO,IAAM,SAAN,MAAM,QAAO;AAAA;AAAA,EAET;AAAA;AAAA,EAGD;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOR,YAAY,UAAsB;AAChC,SAAK,KAAK,WAAW;AACrB,SAAK,YAAY,WAAW,CAAC,GAAG,QAAQ,IAAI,CAAC;AAC7C,SAAK,aAAa,oBAAI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EACzC;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAc,GAAyB;AAC7C,UAAM,OAAoB;AAAA,MACxB,IAAI,EAAE;AAAA,MACN,MAAM,EAAE;AAAA,MACR,SAAS,CAAC;AAAA,MACV,UAAU,EAAE;AAAA,MACZ,WAAW,EAAE,UAAU,YAAY;AAAA,IACrC;AAEA,QAAI,aAAa,aAAa;AAC5B,WAAK,UAAU,EAAE;AAAA,IACnB,WAAW,aAAa,kBAAkB;AACxC,WAAK,UAAU,EAAE;AACjB,WAAK,YAAY,EAAE;AAAA,IACrB,WAAW,aAAa,mBAAmB;AACzC,WAAK,UAAU,EAAE;AAAA,IACnB;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,OAAe,gBAAgB,MAA4B;AACzD,UAAM,UAAU;AAAA,MACd,IAAI,KAAK;AAAA,MACT,UAAU,KAAK;AAAA,MACf,WAAW,IAAI,KAAK,KAAK,SAAS;AAAA,IACpC;AAEA,YAAQ,KAAK,MAAM;AAAA,MACjB,KAAK;AACH,eAAO,IAAI,YAAY,KAAK,SAA0B,OAAO;AAAA,MAC/D,KAAK;AACH,eAAO,IAAI;AAAA,UACT,KAAK;AAAA,UACL,KAAK;AAAA,UACL;AAAA,QACF;AAAA,MACF,KAAK;AACH,eAAO,IAAI,kBAAkB,KAAK,WAAW,CAAC,GAAG,OAAO;AAAA,MAC1D;AACE,cAAM,IAAI,MAAM,yBAAyB,KAAK,IAAI,EAAE;AAAA,IACxD;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/types/thread.ts"],"sourcesContent":["/**\n * @fileoverview Thread class for managing conversation history.\n *\n * Provides a utility class for building and manipulating conversation\n * message sequences, with support for serialization and deserialization.\n *\n * @module types/thread\n */\n\nimport { generateId } from '../utils/id.ts';\nimport {\n Message,\n UserMessage,\n AssistantMessage,\n ToolResultMessage,\n type MessageJSON,\n type MessageType,\n} from './messages.ts';\nimport type { UserContent, AssistantContent } from './content.ts';\nimport type { Turn } from './turn.ts';\n\n/**\n * Thread serialized to JSON format.\n * Picks id from Thread, converts dates to strings.\n */\nexport type ThreadJSON = Pick<Thread, 'id'> & {\n messages: MessageJSON[];\n createdAt: string;\n updatedAt: string;\n};\n\n/**\n * Thread - A utility class for managing conversation history.\n *\n * Provides methods for building, manipulating, and persisting\n * conversation message sequences. This class is optional; users\n * can also manage their own `Message[]` arrays directly.\n *\n * @example\n * ```typescript\n * // Create a new thread and add messages\n * const thread = new Thread();\n * thread.user('Hello!');\n * thread.assistant('Hi there! How can I help?');\n *\n * // Use with LLM inference\n * const turn = await instance.generate(thread, 'What is 2+2?');\n * thread.append(turn);\n *\n * // Serialize for storage\n * const json = thread.toJSON();\n * localStorage.setItem('chat', JSON.stringify(json));\n *\n * // Restore from storage\n * const restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));\n * ```\n */\nexport class Thread {\n /** Unique thread identifier */\n readonly id: string;\n\n /** Internal message storage */\n private _messages: Message[];\n\n /** Creation timestamp */\n private _createdAt: Date;\n\n /** Last update timestamp */\n private _updatedAt: Date;\n\n /**\n * Creates a new thread instance.\n *\n * @param messages - Optional initial messages to populate the thread\n */\n constructor(messages?: Message[]) {\n this.id = generateId();\n this._messages = messages ? [...messages] : [];\n this._createdAt = new Date();\n this._updatedAt = new Date();\n }\n\n /**\n * All messages in the thread (readonly).\n */\n get messages(): readonly Message[] {\n return this._messages;\n }\n\n /**\n * Number of messages in the thread.\n */\n get length(): number {\n return this._messages.length;\n }\n\n /**\n * Appends all messages from a Turn to the thread.\n *\n * @param turn - The Turn containing messages to append\n * @returns This thread instance for chaining\n */\n append(turn: Turn): this {\n this._messages.push(...turn.messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Adds raw messages to the thread.\n *\n * @param messages - Messages to add\n * @returns This thread instance for chaining\n */\n push(...messages: Message[]): this {\n this._messages.push(...messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Adds a user message to the thread.\n *\n * @param content - String or array of content blocks\n * @returns This thread instance for chaining\n *\n * @example\n * ```typescript\n * thread.user('Hello, world!');\n * thread.user([\n * { type: 'text', text: 'Describe this image:' },\n * { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }\n * ]);\n * ```\n */\n user(content: string | UserContent[]): this {\n this._messages.push(new UserMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Adds an assistant message to the thread.\n *\n * @param content - String or array of content blocks\n * @returns This thread instance for chaining\n *\n * @example\n * ```typescript\n * thread.assistant('I can help with that!');\n * ```\n */\n assistant(content: string | AssistantContent[]): this {\n this._messages.push(new AssistantMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Filters messages by type.\n *\n * @param type - The message type to filter by\n * @returns Array of messages matching the type\n *\n * @example\n * ```typescript\n * const userMessages = thread.filter('user');\n * const assistantMessages = thread.filter('assistant');\n * ```\n */\n filter(type: MessageType): Message[] {\n return this._messages.filter((m) => m.type === type);\n }\n\n /**\n * Returns the last N messages from the thread.\n *\n * @param count - Number of messages to return\n * @returns Array of the last N messages\n *\n * @example\n * ```typescript\n * const recent = thread.tail(5);\n * ```\n */\n tail(count: number): Message[] {\n return this._messages.slice(-count);\n }\n\n /**\n * Creates a new thread with a subset of messages.\n *\n * @param start - Start index (inclusive)\n * @param end - End index (exclusive)\n * @returns New Thread containing the sliced messages\n *\n * @example\n * ```typescript\n * const subset = thread.slice(0, 10);\n * ```\n */\n slice(start?: number, end?: number): Thread {\n return new Thread(this._messages.slice(start, end));\n }\n\n /**\n * Removes all messages from the thread.\n *\n * @returns This thread instance for chaining\n */\n clear(): this {\n this._messages = [];\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Converts the thread to a plain message array.\n *\n * @returns Copy of the internal message array\n */\n toMessages(): Message[] {\n return [...this._messages];\n }\n\n /**\n * Serializes the thread to JSON format.\n *\n * @returns JSON-serializable representation of the thread\n *\n * @example\n * ```typescript\n * const json = thread.toJSON();\n * localStorage.setItem('thread', JSON.stringify(json));\n * ```\n */\n toJSON(): ThreadJSON {\n return {\n id: this.id,\n messages: this._messages.map((m) => this.messageToJSON(m)),\n createdAt: this._createdAt.toISOString(),\n updatedAt: this._updatedAt.toISOString(),\n };\n }\n\n /**\n * Deserializes a thread from JSON format.\n *\n * @param json - The JSON representation to deserialize\n * @returns Reconstructed Thread instance\n *\n * @example\n * ```typescript\n * const json = JSON.parse(localStorage.getItem('thread'));\n * const thread = Thread.fromJSON(json);\n * ```\n */\n static fromJSON(json: ThreadJSON): Thread {\n const messages = json.messages.map((m) => Thread.messageFromJSON(m));\n const thread = new Thread(messages);\n (thread as { id: string }).id = json.id;\n thread._createdAt = new Date(json.createdAt);\n thread._updatedAt = new Date(json.updatedAt);\n return thread;\n }\n\n /**\n * Enables iteration over messages with for...of loops.\n *\n * @returns Iterator over the thread's messages\n *\n * @example\n * ```typescript\n * for (const message of thread) {\n * console.log(message.text);\n * }\n * ```\n */\n [Symbol.iterator](): Iterator<Message> {\n return this._messages[Symbol.iterator]();\n }\n\n /**\n * Converts a message to JSON format.\n */\n private messageToJSON(m: Message): MessageJSON {\n const base: MessageJSON = {\n id: m.id,\n type: m.type,\n content: [],\n metadata: m.metadata,\n timestamp: m.timestamp.toISOString(),\n };\n\n if (m instanceof UserMessage) {\n base.content = m.content;\n } else if (m instanceof AssistantMessage) {\n base.content = m.content;\n base.toolCalls = m.toolCalls;\n } else if (m instanceof ToolResultMessage) {\n base.results = m.results;\n }\n\n return base;\n }\n\n /**\n * Reconstructs a message from JSON format.\n */\n private static messageFromJSON(json: MessageJSON): Message {\n const options = {\n id: json.id,\n metadata: json.metadata,\n timestamp: new Date(json.timestamp),\n };\n\n switch (json.type) {\n case 'user':\n return new UserMessage(json.content as UserContent[], options);\n case 'assistant':\n return new AssistantMessage(\n json.content as AssistantContent[],\n json.toolCalls,\n options\n );\n case 'tool_result':\n return new ToolResultMessage(json.results ?? [], options);\n default:\n throw new Error(`Unknown message type: ${json.type}`);\n }\n }\n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]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/utils/id.ts
|
|
2
|
+
function generateId() {
|
|
3
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
4
|
+
return crypto.randomUUID();
|
|
5
|
+
}
|
|
6
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
7
|
+
const r = Math.random() * 16 | 0;
|
|
8
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
9
|
+
return v.toString(16);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function generateShortId(prefix = "") {
|
|
13
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
14
|
+
let result = prefix;
|
|
15
|
+
for (let i = 0; i < 12; i++) {
|
|
16
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
generateId,
|
|
23
|
+
generateShortId
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=chunk-U2G5PHHL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/id.ts"],"sourcesContent":["/**\n * @fileoverview ID generation utilities for the Universal Provider Protocol.\n *\n * Provides functions for generating unique identifiers used throughout UPP,\n * including message IDs, tool call IDs, and other internal references.\n *\n * @module utils/id\n */\n\n/**\n * Generates a unique UUID v4 identifier.\n *\n * Uses the native `crypto.randomUUID()` when available for cryptographically\n * secure randomness. Falls back to a Math.random-based implementation for\n * environments without Web Crypto API support.\n *\n * @returns A UUID v4 string in the format `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`\n *\n * @example\n * ```typescript\n * const messageId = generateId();\n * // => \"f47ac10b-58cc-4372-a567-0e02b2c3d479\"\n * ```\n */\nexport function generateId(): string {\n if (typeof crypto !== 'undefined' && crypto.randomUUID) {\n return crypto.randomUUID();\n }\n\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n\n/**\n * Generates a short alphanumeric identifier.\n *\n * Creates a 12-character random string using alphanumeric characters (a-z, A-Z, 0-9).\n * Useful for tool call IDs and other cases where a full UUID is not required.\n *\n * @param prefix - Optional prefix to prepend to the generated ID\n * @returns A string containing the prefix followed by 12 random alphanumeric characters\n *\n * @example\n * ```typescript\n * const toolCallId = generateShortId('call_');\n * // => \"call_aB3xY9mK2pQr\"\n *\n * const simpleId = generateShortId();\n * // => \"Tz4wN8vL1sHj\"\n * ```\n */\nexport function generateShortId(prefix = ''): string {\n const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';\n let result = prefix;\n for (let i = 0; i < 12; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length));\n }\n return result;\n}\n"],"mappings":";AAwBO,SAAS,aAAqB;AACnC,MAAI,OAAO,WAAW,eAAe,OAAO,YAAY;AACtD,WAAO,OAAO,WAAW;AAAA,EAC3B;AAEA,SAAO,uCAAuC,QAAQ,SAAS,CAAC,MAAM;AACpE,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AACtC,WAAO,EAAE,SAAS,EAAE;AAAA,EACtB,CAAC;AACH;AAoBO,SAAS,gBAAgB,SAAS,IAAY;AACnD,QAAM,QAAQ;AACd,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,IAAI,KAAK;AAC3B,cAAU,MAAM,OAAO,KAAK,MAAM,KAAK,OAAO,IAAI,MAAM,MAAM,CAAC;AAAA,EACjE;AACA,SAAO;AACT;","names":[]}
|
|
@@ -6,6 +6,82 @@ import {
|
|
|
6
6
|
UPPError
|
|
7
7
|
} from "./chunk-COS4ON4G.js";
|
|
8
8
|
|
|
9
|
+
// src/http/retry.ts
|
|
10
|
+
function isRetryable(error) {
|
|
11
|
+
return error.code === ErrorCode.RateLimited || error.code === ErrorCode.NetworkError || error.code === ErrorCode.Timeout || error.code === ErrorCode.ProviderError;
|
|
12
|
+
}
|
|
13
|
+
function exponentialBackoff(options = {}) {
|
|
14
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
15
|
+
const baseDelay = options.baseDelay ?? 1e3;
|
|
16
|
+
const maxDelay = options.maxDelay ?? 3e4;
|
|
17
|
+
const jitter = options.jitter ?? true;
|
|
18
|
+
return () => ({
|
|
19
|
+
maxAttempts,
|
|
20
|
+
onRetry(error, attempt) {
|
|
21
|
+
if (attempt > maxAttempts) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (!isRetryable(error)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
let delay = baseDelay * Math.pow(2, attempt - 1);
|
|
28
|
+
delay = Math.min(delay, maxDelay);
|
|
29
|
+
if (jitter) {
|
|
30
|
+
delay = delay * (0.5 + Math.random());
|
|
31
|
+
}
|
|
32
|
+
return Math.floor(delay);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function linearBackoff(options = {}) {
|
|
37
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
38
|
+
const delay = options.delay ?? 1e3;
|
|
39
|
+
return () => ({
|
|
40
|
+
maxAttempts,
|
|
41
|
+
onRetry(error, attempt) {
|
|
42
|
+
if (attempt > maxAttempts) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
if (!isRetryable(error)) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return delay * attempt;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function noRetry() {
|
|
53
|
+
return () => ({
|
|
54
|
+
maxAttempts: 0,
|
|
55
|
+
onRetry() {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function retryAfterStrategy(options = {}) {
|
|
61
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
62
|
+
const fallbackDelay = options.fallbackDelay ?? 5e3;
|
|
63
|
+
return () => {
|
|
64
|
+
let lastRetryAfter;
|
|
65
|
+
return {
|
|
66
|
+
maxAttempts,
|
|
67
|
+
setRetryAfter(seconds) {
|
|
68
|
+
lastRetryAfter = seconds * 1e3;
|
|
69
|
+
},
|
|
70
|
+
onRetry(error, attempt) {
|
|
71
|
+
if (attempt > maxAttempts) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
if (error.code !== ErrorCode.RateLimited) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
const delay = lastRetryAfter ?? fallbackDelay;
|
|
78
|
+
lastRetryAfter = void 0;
|
|
79
|
+
return delay;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
9
85
|
// src/http/errors.ts
|
|
10
86
|
function statusToErrorCode(status) {
|
|
11
87
|
switch (status) {
|
|
@@ -100,9 +176,6 @@ function warnInsecureUrl(url, provider) {
|
|
|
100
176
|
);
|
|
101
177
|
}
|
|
102
178
|
}
|
|
103
|
-
function hasFork(strategy) {
|
|
104
|
-
return !!strategy && typeof strategy.fork === "function";
|
|
105
|
-
}
|
|
106
179
|
function sleep(ms) {
|
|
107
180
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
108
181
|
}
|
|
@@ -160,13 +233,12 @@ async function fetchWithTimeout(fetchFn, url, init, timeout, provider, modality)
|
|
|
160
233
|
async function doFetch(url, init, config, provider, modality) {
|
|
161
234
|
const fetchFn = config.fetch ?? fetch;
|
|
162
235
|
const timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
163
|
-
const
|
|
164
|
-
const strategy = hasFork(baseStrategy) ? baseStrategy.fork() : baseStrategy;
|
|
236
|
+
const strategy = (config.retryStrategy ?? noRetry())();
|
|
165
237
|
warnInsecureUrl(url, provider);
|
|
166
238
|
let attempt = 0;
|
|
167
239
|
while (true) {
|
|
168
240
|
attempt++;
|
|
169
|
-
if (strategy
|
|
241
|
+
if (strategy.beforeRequest) {
|
|
170
242
|
const delay = await strategy.beforeRequest();
|
|
171
243
|
if (delay > 0) {
|
|
172
244
|
await sleep(delay);
|
|
@@ -184,22 +256,18 @@ async function doFetch(url, init, config, provider, modality) {
|
|
|
184
256
|
);
|
|
185
257
|
} catch (error) {
|
|
186
258
|
if (error instanceof UPPError) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
259
|
+
const delay2 = await strategy.onRetry(error, attempt);
|
|
260
|
+
if (delay2 !== null) {
|
|
261
|
+
await sleep(delay2);
|
|
262
|
+
continue;
|
|
193
263
|
}
|
|
194
264
|
throw error;
|
|
195
265
|
}
|
|
196
266
|
const uppError = networkError(toError(error), provider, modality);
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
267
|
+
const delay = await strategy.onRetry(uppError, attempt);
|
|
268
|
+
if (delay !== null) {
|
|
269
|
+
await sleep(delay);
|
|
270
|
+
continue;
|
|
203
271
|
}
|
|
204
272
|
throw uppError;
|
|
205
273
|
}
|
|
@@ -209,30 +277,25 @@ async function doFetch(url, init, config, provider, modality) {
|
|
|
209
277
|
response.headers.get("Retry-After"),
|
|
210
278
|
config.retryAfterMaxSeconds ?? MAX_RETRY_AFTER_SECONDS
|
|
211
279
|
);
|
|
212
|
-
if (retryAfterSeconds !== null && strategy
|
|
213
|
-
strategy.setRetryAfter(
|
|
214
|
-
retryAfterSeconds
|
|
215
|
-
);
|
|
280
|
+
if (retryAfterSeconds !== null && strategy.setRetryAfter) {
|
|
281
|
+
strategy.setRetryAfter(retryAfterSeconds);
|
|
216
282
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
283
|
+
const delay = await strategy.onRetry(error, attempt);
|
|
284
|
+
if (delay !== null) {
|
|
285
|
+
await sleep(delay);
|
|
286
|
+
continue;
|
|
223
287
|
}
|
|
224
288
|
throw error;
|
|
225
289
|
}
|
|
226
|
-
strategy
|
|
290
|
+
strategy.reset?.();
|
|
227
291
|
return response;
|
|
228
292
|
}
|
|
229
293
|
}
|
|
230
294
|
async function doStreamFetch(url, init, config, provider, modality) {
|
|
231
295
|
const fetchFn = config.fetch ?? fetch;
|
|
232
296
|
const timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
if (strategy?.beforeRequest) {
|
|
297
|
+
const strategy = (config.retryStrategy ?? noRetry())();
|
|
298
|
+
if (strategy.beforeRequest) {
|
|
236
299
|
const delay = await strategy.beforeRequest();
|
|
237
300
|
if (delay > 0) {
|
|
238
301
|
await sleep(delay);
|
|
@@ -257,6 +320,10 @@ async function doStreamFetch(url, init, config, provider, modality) {
|
|
|
257
320
|
}
|
|
258
321
|
|
|
259
322
|
export {
|
|
323
|
+
exponentialBackoff,
|
|
324
|
+
linearBackoff,
|
|
325
|
+
noRetry,
|
|
326
|
+
retryAfterStrategy,
|
|
260
327
|
statusToErrorCode,
|
|
261
328
|
normalizeHttpError,
|
|
262
329
|
networkError,
|
|
@@ -266,4 +333,4 @@ export {
|
|
|
266
333
|
doFetch,
|
|
267
334
|
doStreamFetch
|
|
268
335
|
};
|
|
269
|
-
//# sourceMappingURL=chunk-
|
|
336
|
+
//# sourceMappingURL=chunk-VQZPADW6.js.map
|