@code-yeongyu/senpi-ai 2026.9.12 → 2026.9.13
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 +39 -4
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +8 -1
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/cloudflare-ai-binding.d.ts +77 -0
- package/dist/api/cloudflare-ai-binding.d.ts.map +1 -0
- package/dist/api/cloudflare-ai-binding.js +72 -0
- package/dist/api/cloudflare-ai-binding.js.map +1 -0
- package/dist/api/cursor-agent/measure.d.ts +11 -0
- package/dist/api/cursor-agent/measure.d.ts.map +1 -1
- package/dist/api/cursor-agent/measure.js +18 -0
- package/dist/api/cursor-agent/measure.js.map +1 -1
- package/dist/api/cursor-agent.d.ts +1 -1
- package/dist/api/cursor-agent.d.ts.map +1 -1
- package/dist/api/cursor-agent.js +5 -1
- package/dist/api/cursor-agent.js.map +1 -1
- package/dist/api/mistral-conversations.js +4 -1
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +6 -3
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +4 -1
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +13 -3
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/cursor/context-limit-store.d.ts +32 -0
- package/dist/cursor/context-limit-store.d.ts.map +1 -0
- package/dist/cursor/context-limit-store.js +62 -0
- package/dist/cursor/context-limit-store.js.map +1 -0
- package/dist/cursor/store-migration.d.ts.map +1 -1
- package/dist/cursor/store-migration.js +2 -1
- package/dist/cursor/store-migration.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +1 -0
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +5 -2
- package/dist/models.js.map +1 -1
- package/dist/openai-responses-compat.d.ts +1 -1
- package/dist/openai-responses-compat.d.ts.map +1 -1
- package/dist/openai-responses-compat.js.map +1 -1
- package/dist/providers/cloudflare-ai-gateway.d.ts.map +1 -1
- package/dist/providers/cloudflare-ai-gateway.js +4 -0
- package/dist/providers/cloudflare-ai-gateway.js.map +1 -1
- package/dist/providers/cursor.d.ts.map +1 -1
- package/dist/providers/cursor.js +2 -1
- package/dist/providers/cursor.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/azure-openai-responses.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/deepseek.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/openai-codex.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/dist/providers/data/qwen-token-plan.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/data/xai.json +1 -1
- package/dist/providers/faux.js +5 -5
- package/dist/providers/faux.js.map +1 -1
- package/dist/providers/opencode-go.d.ts.map +1 -1
- package/dist/providers/opencode-go.js +4 -3
- package/dist/providers/opencode-go.js.map +1 -1
- package/dist/providers/opencode-headers.d.ts +4 -0
- package/dist/providers/opencode-headers.d.ts.map +1 -0
- package/dist/providers/opencode-headers.js +22 -0
- package/dist/providers/opencode-headers.js.map +1 -0
- package/dist/providers/opencode.d.ts.map +1 -1
- package/dist/providers/opencode.js +5 -4
- package/dist/providers/opencode.js.map +1 -1
- package/dist/types.d.ts +16 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/assistant-message-frame.d.ts +77 -0
- package/dist/utils/assistant-message-frame.d.ts.map +1 -0
- package/dist/utils/assistant-message-frame.js +427 -0
- package/dist/utils/assistant-message-frame.js.map +1 -0
- package/dist/utils/cursor-context-limit.d.ts +12 -0
- package/dist/utils/cursor-context-limit.d.ts.map +1 -0
- package/dist/utils/cursor-context-limit.js +93 -0
- package/dist/utils/cursor-context-limit.js.map +1 -0
- package/dist/utils/event-stream.d.ts.map +1 -1
- package/dist/utils/event-stream.js +25 -5
- package/dist/utils/event-stream.js.map +1 -1
- package/dist/utils/prompt-cache-ttl.d.ts +1 -1
- package/dist/utils/prompt-cache-ttl.d.ts.map +1 -1
- package/dist/utils/prompt-cache-ttl.js +5 -1
- package/dist/utils/prompt-cache-ttl.js.map +1 -1
- package/dist/utils/retry-profile/types.d.ts +10 -1
- package/dist/utils/retry-profile/types.d.ts.map +1 -1
- package/dist/utils/retry-profile/types.js.map +1 -1
- package/dist/utils/retry.d.ts +13 -2
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +18 -7
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/uuid.d.ts +2 -2
- package/dist/utils/uuid.d.ts.map +1 -1
- package/dist/utils/uuid.js +36 -31
- package/dist/utils/uuid.js.map +1 -1
- package/package.json +6 -6
- package/dist/api/cloudflare-gateway-binding.d.ts +0 -69
- package/dist/api/cloudflare-gateway-binding.d.ts.map +0 -1
- package/dist/api/cloudflare-gateway-binding.js +0 -159
- package/dist/api/cloudflare-gateway-binding.js.map +0 -1
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
class FifoQueue {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.incoming = [];
|
|
4
|
+
this.outgoing = [];
|
|
5
|
+
}
|
|
6
|
+
get length() {
|
|
7
|
+
return this.incoming.length + this.outgoing.length;
|
|
8
|
+
}
|
|
9
|
+
enqueue(value) {
|
|
10
|
+
this.incoming.push(value);
|
|
11
|
+
}
|
|
12
|
+
dequeue() {
|
|
13
|
+
if (this.outgoing.length === 0) {
|
|
14
|
+
while (this.incoming.length > 0) {
|
|
15
|
+
this.outgoing.push(this.incoming.pop());
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return this.outgoing.pop();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
1
21
|
// Generic event stream class for async iteration
|
|
2
22
|
export class EventStream {
|
|
3
23
|
#queue;
|
|
@@ -8,7 +28,7 @@ export class EventStream {
|
|
|
8
28
|
constructor(isComplete, extractResult) {
|
|
9
29
|
this.#queue = [];
|
|
10
30
|
this.#queueHead = 0;
|
|
11
|
-
this.waiting =
|
|
31
|
+
this.waiting = new FifoQueue();
|
|
12
32
|
this.done = false;
|
|
13
33
|
this.#failed = false;
|
|
14
34
|
this.#localWorkDepth = 0;
|
|
@@ -46,7 +66,7 @@ export class EventStream {
|
|
|
46
66
|
this.resolveFinalResult(this.extractResult(event));
|
|
47
67
|
}
|
|
48
68
|
// Deliver to waiting consumer or queue it
|
|
49
|
-
const waiter = this.waiting.
|
|
69
|
+
const waiter = this.waiting.dequeue();
|
|
50
70
|
if (waiter) {
|
|
51
71
|
waiter.resolve({ value: event, done: false });
|
|
52
72
|
}
|
|
@@ -61,7 +81,7 @@ export class EventStream {
|
|
|
61
81
|
}
|
|
62
82
|
// Notify all waiting consumers that we're done
|
|
63
83
|
while (this.waiting.length > 0) {
|
|
64
|
-
const waiter = this.waiting.
|
|
84
|
+
const waiter = this.waiting.dequeue();
|
|
65
85
|
if (waiter)
|
|
66
86
|
waiter.resolve({ value: undefined, done: true });
|
|
67
87
|
}
|
|
@@ -74,7 +94,7 @@ export class EventStream {
|
|
|
74
94
|
this.#error = error;
|
|
75
95
|
this.rejectFinalResult(error);
|
|
76
96
|
while (this.waiting.length > 0) {
|
|
77
|
-
const waiter = this.waiting.
|
|
97
|
+
const waiter = this.waiting.dequeue();
|
|
78
98
|
if (waiter)
|
|
79
99
|
waiter.reject(error);
|
|
80
100
|
}
|
|
@@ -91,7 +111,7 @@ export class EventStream {
|
|
|
91
111
|
if (this.done) {
|
|
92
112
|
return Promise.resolve({ value: undefined, done: true });
|
|
93
113
|
}
|
|
94
|
-
return new Promise((resolve, reject) => this.waiting.
|
|
114
|
+
return new Promise((resolve, reject) => this.waiting.enqueue({ resolve, reject }));
|
|
95
115
|
},
|
|
96
116
|
};
|
|
97
117
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-stream.js","sourceRoot":"","sources":["../../src/utils/event-stream.ts"],"names":[],"mappings":"AAEA,iDAAiD;AACjD,MAAM,OAAO,WAAW;IACvB,MAAM,CAAW;IACjB,UAAU,CAAK;IAGf,OAAO,CAAS;IAChB,MAAM,CAAU;IAChB,eAAe,CAAK;IAOpB,YAAY,UAAiC,EAAE,aAA8B;QAb7E,WAAM,GAAQ,EAAE,CAAC;QACjB,eAAU,GAAG,CAAC,CAAC;QACP,YAAO,
|
|
1
|
+
{"version":3,"file":"event-stream.js","sourceRoot":"","sources":["../../src/utils/event-stream.ts"],"names":[],"mappings":"AAEA,MAAM,SAAS;IAAf;QACS,aAAQ,GAAQ,EAAE,CAAC;QACnB,aAAQ,GAAQ,EAAE,CAAC;IAkB5B,CAAC;IAhBA,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IACpD,CAAC;IAED,OAAO,CAAC,KAAQ;QACf,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO;QACN,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACjC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAG,CAAC,CAAC;YAC1C,CAAC;QACF,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,E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AE,MAAM,EAAE,CAAC,CAAC,CAAC;YACvG,CAAC;SACD,CAAC;IACH,CAAC;IAED,MAAM;QACL,OAAO,IAAI,CAAC,kBAAkB,CAAC;IAChC,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,cAAc,CAAQ,IAAoB;QAC/C,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,IAAI,CAAC;YACJ,OAAO,MAAM,IAAI,CAAC;QACnB,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,eAAe,EAAE,CAAC;QACxB,CAAC;IACF,CAAC;IAED,2EAA2E;IAC3E,mBAAmB;QAClB,OAAO,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC;IACjC,CAAC;CACD;AAED,MAAM,OAAO,2BAA4B,SAAQ,WAAoD;IACpG;QACC,KAAK,CACJ,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAC1D,CAAC,KAAK,EAAE,EAAE;YACT,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC3B,OAAO,KAAK,CAAC,OAAO,CAAC;YACtB,CAAC;iBAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gBACnC,OAAO,KAAK,CAAC,KAAK,CAAC;YACpB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;QAC3D,CAAC,CACD,CAAC;IACH,CAAC;CACD;AAED,+EAA+E;AAC/E,MAAM,UAAU,iCAAiC;IAChD,OAAO,IAAI,2BAA2B,EAAE,CAAC;AAC1C,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent } from \"../types.ts\";\n\nclass FifoQueue<T> {\n\tprivate incoming: T[] = [];\n\tprivate outgoing: T[] = [];\n\n\tget length(): number {\n\t\treturn this.incoming.length + this.outgoing.length;\n\t}\n\n\tenqueue(value: T): void {\n\t\tthis.incoming.push(value);\n\t}\n\n\tdequeue(): T | undefined {\n\t\tif (this.outgoing.length === 0) {\n\t\t\twhile (this.incoming.length > 0) {\n\t\t\t\tthis.outgoing.push(this.incoming.pop()!);\n\t\t\t}\n\t\t}\n\t\treturn this.outgoing.pop();\n\t}\n}\n\n// Generic event stream class for async iteration\nexport class EventStream<T, R = T> implements AsyncIterable<T> {\n\t#queue: T[] = [];\n\t#queueHead = 0;\n\tprivate waiting = new FifoQueue<{ resolve: (value: IteratorResult<T>) => void; reject: (error: unknown) => void }>();\n\tprivate done = false;\n\t#failed = false;\n\t#error: unknown;\n\t#localWorkDepth = 0;\n\tprivate finalResultPromise: Promise<R>;\n\tprivate resolveFinalResult!: (result: R) => void;\n\tprivate rejectFinalResult!: (error: unknown) => void;\n\tprivate isComplete: (event: T) => boolean;\n\tprivate extractResult: (event: T) => R;\n\n\tconstructor(isComplete: (event: T) => boolean, extractResult: (event: T) => R) {\n\t\tthis.isComplete = isComplete;\n\t\tthis.extractResult = extractResult;\n\t\tthis.finalResultPromise = new Promise((resolve, reject) => {\n\t\t\tthis.resolveFinalResult = resolve;\n\t\t\tthis.rejectFinalResult = reject;\n\t\t});\n\t\tthis.finalResultPromise.catch(() => {});\n\t}\n\n\tget queue(): T[] {\n\t\treturn this.#queue.slice(this.#queueHead);\n\t}\n\n\t#enqueue(event: T): void {\n\t\tthis.#queue.push(event);\n\t}\n\n\t#dequeue(): T {\n\t\tconst event = this.#queue[this.#queueHead];\n\t\tif (event === undefined) {\n\t\t\tthrow new Error(\"EventStream queue underflow\");\n\t\t}\n\t\tthis.#queueHead++;\n\t\tif (this.#queueHead > 1024 && this.#queueHead * 2 >= this.#queue.length) {\n\t\t\tthis.#queue = this.#queue.slice(this.#queueHead);\n\t\t\tthis.#queueHead = 0;\n\t\t}\n\t\treturn event;\n\t}\n\n\tpush(event: T): void {\n\t\tif (this.done) return;\n\n\t\tif (this.isComplete(event)) {\n\t\t\tthis.done = true;\n\t\t\tthis.resolveFinalResult(this.extractResult(event));\n\t\t}\n\n\t\t// Deliver to waiting consumer or queue it\n\t\tconst waiter = this.waiting.dequeue();\n\t\tif (waiter) {\n\t\t\twaiter.resolve({ value: event, done: false });\n\t\t} else {\n\t\t\tthis.#enqueue(event);\n\t\t}\n\t}\n\n\tend(result?: R): void {\n\t\tthis.done = true;\n\t\tif (result !== undefined) {\n\t\t\tthis.resolveFinalResult(result);\n\t\t}\n\t\t// Notify all waiting consumers that we're done\n\t\twhile (this.waiting.length > 0) {\n\t\t\tconst waiter = this.waiting.dequeue();\n\t\t\tif (waiter) waiter.resolve({ value: undefined, done: true });\n\t\t}\n\t}\n\n\tfail(error: unknown): void {\n\t\tif (this.done) return;\n\t\tthis.done = true;\n\t\tthis.#failed = true;\n\t\tthis.#error = error;\n\t\tthis.rejectFinalResult(error);\n\t\twhile (this.waiting.length > 0) {\n\t\t\tconst waiter = this.waiting.dequeue();\n\t\t\tif (waiter) waiter.reject(error);\n\t\t}\n\t}\n\n\t[Symbol.asyncIterator](): AsyncIterator<T> {\n\t\treturn {\n\t\t\tnext: () => {\n\t\t\t\tif (this.#queueHead < this.#queue.length) {\n\t\t\t\t\treturn Promise.resolve({ value: this.#dequeue(), done: false });\n\t\t\t\t}\n\t\t\t\tif (this.#failed) {\n\t\t\t\t\treturn Promise.reject(this.#error);\n\t\t\t\t}\n\t\t\t\tif (this.done) {\n\t\t\t\t\treturn Promise.resolve({ value: undefined, done: true });\n\t\t\t\t}\n\t\t\t\treturn new Promise<IteratorResult<T>>((resolve, reject) => this.waiting.enqueue({ resolve, reject }));\n\t\t\t},\n\t\t};\n\t}\n\n\tresult(): Promise<R> {\n\t\treturn this.finalResultPromise;\n\t}\n\n\t/**\n\t * Track a locally executing unit of work (for example a server-requested\n\t * tool run on Cursor's exec channel) during which the stream legitimately\n\t * emits no events. Idle watchdogs consult {@link hasPendingLocalWork} to\n\t * attribute the silence to the tool run instead of aborting a healthy\n\t * stream. Rejections propagate to the caller unchanged.\n\t */\n\tasync trackLocalWork<TWork>(work: Promise<TWork>): Promise<TWork> {\n\t\tthis.#localWorkDepth++;\n\t\ttry {\n\t\t\treturn await work;\n\t\t} finally {\n\t\t\tthis.#localWorkDepth--;\n\t\t}\n\t}\n\n\t/** True while at least one {@link trackLocalWork} promise is unsettled. */\n\thasPendingLocalWork(): boolean {\n\t\treturn this.#localWorkDepth > 0;\n\t}\n}\n\nexport class AssistantMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {\n\tconstructor() {\n\t\tsuper(\n\t\t\t(event) => event.type === \"done\" || event.type === \"error\",\n\t\t\t(event) => {\n\t\t\t\tif (event.type === \"done\") {\n\t\t\t\t\treturn event.message;\n\t\t\t\t} else if (event.type === \"error\") {\n\t\t\t\t\treturn event.error;\n\t\t\t\t}\n\t\t\t\tthrow new Error(\"Unexpected event type for final result\");\n\t\t\t},\n\t\t);\n\t}\n}\n\n/** Factory function for AssistantMessageEventStream (for use in extensions) */\nexport function createAssistantMessageEventStream(): AssistantMessageEventStream {\n\treturn new AssistantMessageEventStream();\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import type { AnthropicMessagesCompat, Api, Model, OpenAICompletionsCompat, Prov
|
|
|
2
2
|
export declare const PROMPT_CACHE_TTL_SHORT_SECONDS = 300;
|
|
3
3
|
export declare const PROMPT_CACHE_TTL_LONG_SECONDS = 3600;
|
|
4
4
|
export declare function isAnthropicApiBaseUrl(baseUrl: string): boolean;
|
|
5
|
-
export declare function getAnthropicCompat(model: Model<"anthropic-messages">): Required<Omit<AnthropicMessagesCompat, "forceAdaptiveThinking" | "supportsMidConvoEffort"
|
|
5
|
+
export declare function getAnthropicCompat(model: Model<"anthropic-messages">): Required<Omit<AnthropicMessagesCompat, "forceAdaptiveThinking" | "supportsMidConvoEffort" | "sessionAffinityFormat">> & Pick<AnthropicMessagesCompat, "sessionAffinityFormat">;
|
|
6
6
|
export type ResolvedOpenAICompletionsCompat = Omit<Required<OpenAICompletionsCompat>, "vllmPriority" | "cacheControlFormat" | "toolCallFormat" | "deferredToolsMode" | "toolSchemaFlavor" | "supportsPromptCacheKey" | "chatTemplateArgs" | "supportsThinkingTokenBudget" | "thinkingTokenBudgetField" | "veniceParameters"> & {
|
|
7
7
|
cacheControlFormat?: OpenAICompletionsCompat["cacheControlFormat"];
|
|
8
8
|
supportsPromptCacheKey?: OpenAICompletionsCompat["supportsPromptCacheKey"];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt-cache-ttl.d.ts","sourceRoot":"","sources":["../../src/utils/prompt-cache-ttl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,uBAAuB,EACvB,GAAG,EAEH,KAAK,EACL,uBAAuB,EACvB,WAAW,EACX,MAAM,aAAa,CAAC;AAGrB,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAClD,eAAO,MAAM,6BAA6B,OAAO,CAAC;AAElD,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAM9D;AAkBD,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,KAAK,CAAC,oBAAoB,CAAC,GAChC,QAAQ,
|
|
1
|
+
{"version":3,"file":"prompt-cache-ttl.d.ts","sourceRoot":"","sources":["../../src/utils/prompt-cache-ttl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,uBAAuB,EACvB,GAAG,EAEH,KAAK,EACL,uBAAuB,EACvB,WAAW,EACX,MAAM,aAAa,CAAC;AAGrB,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAClD,eAAO,MAAM,6BAA6B,OAAO,CAAC;AAElD,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAM9D;AAkBD,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,KAAK,CAAC,oBAAoB,CAAC,GAChC,QAAQ,CACV,IAAI,CAAC,uBAAuB,EAAE,uBAAuB,GAAG,wBAAwB,GAAG,uBAAuB,CAAC,CAC3G,GACA,IAAI,CAAC,uBAAuB,EAAE,uBAAuB,CAAC,CA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AAK,EAAE,KAAK,CAAC,yBAAyB,CAAC,EAAE,GAAG,CAAC,EAAE,WAAW,GAAG,OAAO,CAmBzG;AAiDD,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,CA4CrG"}
|
|
@@ -30,10 +30,14 @@ export function getAnthropicCompat(model) {
|
|
|
30
30
|
const isFireworks = model.provider === "fireworks";
|
|
31
31
|
const isCloudflareAiGatewayAnthropic = model.provider === "cloudflare-ai-gateway" && model.baseUrl.includes("anthropic");
|
|
32
32
|
const isXiaomi = model.provider === "xiaomi" || model.provider.startsWith("xiaomi-token-plan-");
|
|
33
|
+
// OpenRouter carries prompt-cache affinity on its own x-session-id header and
|
|
34
|
+
// rejects x-session-affinity (earendil-works/pi#9102).
|
|
35
|
+
const isOpenRouter = model.provider === "openrouter" || model.baseUrl.includes("openrouter.ai");
|
|
33
36
|
return {
|
|
34
37
|
supportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? !isFireworks,
|
|
35
38
|
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? !isFireworks,
|
|
36
|
-
sendSessionAffinityHeaders: model.compat?.sendSessionAffinityHeaders ?? !!(isFireworks || isCloudflareAiGatewayAnthropic),
|
|
39
|
+
sendSessionAffinityHeaders: model.compat?.sendSessionAffinityHeaders ?? !!(isFireworks || isCloudflareAiGatewayAnthropic || isOpenRouter),
|
|
40
|
+
sessionAffinityFormat: model.compat?.sessionAffinityFormat ?? (isOpenRouter ? "openrouter" : undefined),
|
|
37
41
|
supportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? !isFireworks,
|
|
38
42
|
supportsDisabledThinking: model.compat?.supportsDisabledThinking ?? !isXiaomi,
|
|
39
43
|
supportsTemperature: model.compat?.supportsTemperature ?? true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prompt-cache-ttl.js","sourceR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B,EAAE,CAAC;YAC3B,MAAM,gBAAgB,GAAG,KAAoC,CAAC;YAC9D,MAAM,SAAS,GAAG,sCAAsC,CAAC,gBAAgB,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;YAC/F,IAAI,SAAS,KAAK,MAAM;gBAAE,OAAO,SAAS,CAAC;YAC3C,MAAM,MAAM,GAAG,0BAA0B,CAAC,gBAAgB,CAAC,CAAC;YAC5D,IAAI,MAAM,CAAC,kBAAkB,KAAK,WAAW,EAAE,CAAC;gBAC/C,OAAO,SAAS,KAAK,MAAM,IAAI,MAAM,CAAC,0BAA0B;oBAC/D,CAAC,CAAC,6BAA6B;oBAC/B,CAAC,CAAC,8BAA8B,CAAC;YACnC,CAAC;YACD,OAAO,8BAA8B,CAAC;QACvC,CAAC;QACD,KAAK,kBAAkB,CAAC;QACxB,KAAK,wBAAwB,CAAC;QAC9B,KAAK,wBAAwB,EAAE,CAAC;YAC/B,MAAM,SAAS,GAAG,oCAAoC,CAAC,KAAK,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;YAClF,OAAO,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,8BAA8B,CAAC;QAC1E,CAAC;QACD;YACC,OAAO,SAAS,CAAC;IACnB,CAAC;AACF,CAAC","sourcesContent":["import type {\n\tAnthropicMessagesCompat,\n\tApi,\n\tCacheRetention,\n\tModel,\n\tOpenAICompletionsCompat,\n\tProviderEnv,\n} from \"../types.ts\";\nimport { getProviderEnvValue } from \"./provider-env.ts\";\n\nexport const PROMPT_CACHE_TTL_SHORT_SECONDS = 300;\nexport const PROMPT_CACHE_TTL_LONG_SECONDS = 3600;\n\nexport function isAnthropicApiBaseUrl(baseUrl: string): boolean {\n\ttry {\n\t\treturn new URL(baseUrl).hostname === \"api.anthropic.com\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nconst CLAUDE_FABLE_OR_MYTHOS_MODEL_ID = /^claude-(?:fable|mythos)(?:-|$)/i;\n\n/**\n * Default for `supportsToolReferences`: first-party Anthropic models except\n * Haiku (rejects client-side tool_reference blocks) and models that predate\n * tool search (Claude 3.x, Opus/Sonnet 4.0, Opus 4.1).\n */\nfunction defaultSupportsToolReferences(model: Model<\"anthropic-messages\">): boolean {\n\tif (model.provider !== \"anthropic\" || model.id.includes(\"haiku\")) return false;\n\tconst version = model.id.match(/^claude-(?:opus|sonnet|fable)-(\\d+)(?:-(\\d+))?(?:-|$)/);\n\tif (!version) return false;\n\tconst major = Number(version[1]);\n\tconst minor = version[2] && version[2].length < 8 ? Number(version[2]) : 0;\n\treturn major > 4 || (major === 4 && minor >= 5);\n}\n\nexport function getAnthropicCompat(\n\tmodel: Model<\"anthropic-messages\">,\n): Required<Omit<AnthropicMessagesCompat, \"forceAdaptiveThinking\" | \"supportsMidConvoEffort\">> {\n\t// Auto-detect session affinity and cache control support from provider\n\tconst isFireworks = model.provider === \"fireworks\";\n\tconst isCloudflareAiGatewayAnthropic =\n\t\tmodel.provider === \"cloudflare-ai-gateway\" && model.baseUrl.includes(\"anthropic\");\n\tconst isXiaomi = model.provider === \"xiaomi\" || model.provider.startsWith(\"xiaomi-token-plan-\");\n\treturn {\n\t\tsupportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? !isFireworks,\n\t\tsupportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? !isFireworks,\n\t\tsendSessionAffinityHeaders:\n\t\t\tmodel.compat?.sendSessionAffinityHeaders ?? !!(isFireworks || isCloudflareAiGatewayAnthropic),\n\t\tsupportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? !isFireworks,\n\t\tsupportsDisabledThinking: model.compat?.supportsDisabledThinking ?? !isXiaomi,\n\t\tsupportsTemperature: model.compat?.supportsTemperature ?? true,\n\t\tsupportsToolChoice: model.compat?.supportsToolChoice ?? true,\n\t\tsupportsForcedToolChoice:\n\t\t\tmodel.compat?.supportsForcedToolChoice ?? !CLAUDE_FABLE_OR_MYTHOS_MODEL_ID.test(model.id),\n\t\tallowEmptySignature: model.compat?.allowEmptySignature ?? false,\n\t\tunsignedThinkingReplay:\n\t\t\tmodel.compat?.unsignedThinkingReplay ?? (model.compat?.allowEmptySignature ? \"empty-signature\" : \"text\"),\n\t\tallowedFallbackModels: model.compat?.allowedFallbackModels ?? [],\n\t\tsupportsStrictTools: model.compat?.supportsStrictTools ?? false,\n\t\tsupportsToolReferences: model.compat?.supportsToolReferences ?? defaultSupportsToolReferences(model),\n\t\t// Default: first-party Anthropic only. Anthropic-compatible providers\n\t\t// (kimi-coding, fireworks, copilot, gateways) may execute the server-side\n\t\t// search but reject the replayed server_tool_use / web_search_tool_result\n\t\t// blocks on the next request (kimi-coding 400s with `tool_call_id is not\n\t\t// found`).\n\t\tsupportsWebSearch: model.compat?.supportsWebSearch ?? isAnthropicApiBaseUrl(model.baseUrl),\n\t};\n}\n\nexport type ResolvedOpenAICompletionsCompat = Omit<\n\tRequired<OpenAICompletionsCompat>,\n\t| \"vllmPriority\"\n\t| \"cacheControlFormat\"\n\t| \"toolCallFormat\"\n\t| \"deferredToolsMode\"\n\t| \"toolSchemaFlavor\"\n\t| \"supportsPromptCacheKey\"\n\t| \"chatTemplateArgs\"\n\t| \"supportsThinkingTokenBudget\"\n\t| \"thinkingTokenBudgetField\"\n\t| \"veniceParameters\"\n> & {\n\tcacheControlFormat?: OpenAICompletionsCompat[\"cacheControlFormat\"];\n\tsupportsPromptCacheKey?: OpenAICompletionsCompat[\"supportsPromptCacheKey\"];\n\ttoolCallFormat?: OpenAICompletionsCompat[\"toolCallFormat\"];\n\tdeferredToolsMode?: OpenAICompletionsCompat[\"deferredToolsMode\"];\n\ttoolSchemaFlavor?: OpenAICompletionsCompat[\"toolSchemaFlavor\"];\n\tchatTemplateArgs?: OpenAICompletionsCompat[\"chatTemplateArgs\"];\n\tsupportsThinkingTokenBudget?: OpenAICompletionsCompat[\"supportsThinkingTokenBudget\"];\n\tthinkingTokenBudgetField?: OpenAICompletionsCompat[\"thinkingTokenBudgetField\"];\n\t/** vLLM `priority`; off by default and never set on the generated catalog. */\n\tvllmPriority?: OpenAICompletionsCompat[\"vllmPriority\"];\n\t/** Venice `venice_parameters`; only set on the generated Venice catalog. */\n\tveniceParameters?: OpenAICompletionsCompat[\"veniceParameters\"];\n};\n\n/**\n * Detect compatibility settings from provider and baseUrl for known providers.\n * Provider takes precedence over URL-based detection since it's explicitly configured.\n * Returns a fully resolved OpenAICompletionsCompat object with all fields set.\n */\nfunction detectOpenAICompletionsCompat(model: Model<\"openai-completions\">): ResolvedOpenAICompletionsCompat {\n\tconst provider = model.provider;\n\tconst baseUrl = model.baseUrl;\n\n\tconst isZai =\n\t\tprovider === \"zai\" ||\n\t\tprovider === \"zai-coding-cn\" ||\n\t\tbaseUrl.includes(\"api.z.ai\") ||\n\t\tbaseUrl.includes(\"open.bigmodel.cn\");\n\tconst isTogether =\n\t\tprovider === \"together\" || baseUrl.includes(\"api.together.ai\") || baseUrl.includes(\"api.together.xyz\");\n\tconst isMoonshot = provider === \"moonshotai\" || provider === \"moonshotai-cn\" || baseUrl.includes(\"api.moonshot.\");\n\tconst isOpenRouter = provider === \"openrouter\" || baseUrl.includes(\"openrouter.ai\");\n\tconst isCloudflareWorkersAI = provider === \"cloudflare-workers-ai\" || baseUrl.includes(\"api.cloudflare.com\");\n\tconst isCloudflareAiGateway = provider === \"cloudflare-ai-gateway\" || baseUrl.includes(\"gateway.ai.cloudflare.com\");\n\tconst isNvidia = provider === \"nvidia\" || baseUrl.includes(\"integrate.api.nvidia.com\");\n\tconst isAntLing = provider === \"ant-ling\" || baseUrl.includes(\"api.ant-ling.com\");\n\tconst isDeepSeek = provider === \"deepseek\" || baseUrl.toLowerCase().includes(\"deepseek.com\");\n\n\tconst isNonStandard =\n\t\tisNvidia ||\n\t\tprovider === \"cerebras\" ||\n\t\tbaseUrl.includes(\"cerebras.ai\") ||\n\t\tprovider === \"xai\" ||\n\t\tbaseUrl.includes(\"api.x.ai\") ||\n\t\tisTogether ||\n\t\tbaseUrl.includes(\"chutes.ai\") ||\n\t\tisDeepSeek ||\n\t\tisZai ||\n\t\tisMoonshot ||\n\t\tprovider === \"opencode\" ||\n\t\tbaseUrl.includes(\"opencode.ai\") ||\n\t\tisCloudflareWorkersAI ||\n\t\tisCloudflareAiGateway ||\n\t\tisAntLing;\n\n\tconst useMaxTokens =\n\t\tbaseUrl.includes(\"chutes.ai\") ||\n\t\tisDeepSeek ||\n\t\tisMoonshot ||\n\t\tisCloudflareAiGateway ||\n\t\tisTogether ||\n\t\tisNvidia ||\n\t\tisAntLing ||\n\t\tisZai;\n\n\tconst isGrok = provider === \"xai\" || baseUrl.includes(\"api.x.ai\");\n\tconst isOpenRouterDeveloperRoleModel =\n\t\tisOpenRouter && (model.id.startsWith(\"anthropic/\") || model.id.startsWith(\"openai/\"));\n\tconst openRouterCacheControlPrefixes = [\"anthropic/\", \"qwen/\", \"google/\"];\n\tconst cacheControlModelId = model.id.startsWith(\"~\") ? model.id.slice(1) : model.id;\n\tconst supportsOpenRouterCacheControl = openRouterCacheControlPrefixes.some((prefix) =>\n\t\tcacheControlModelId.startsWith(prefix),\n\t);\n\tconst cacheControlFormat = provider === \"openrouter\" && supportsOpenRouterCacheControl ? \"anthropic\" : undefined;\n\n\treturn {\n\t\tsupportsStore: !isNonStandard,\n\t\tsupportsDeveloperRole: isOpenRouterDeveloperRoleModel || (!isNonStandard && !isOpenRouter),\n\t\tsupportsReasoningEffort:\n\t\t\t!isGrok && !isZai && !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia && !isAntLing,\n\t\tsupportsUsageInStreaming: true,\n\t\tsupportsFinishReason: true,\n\t\tmaxTokensField: useMaxTokens ? \"max_tokens\" : \"max_completion_tokens\",\n\t\trequiresToolResultName: false,\n\t\trequiresAssistantAfterToolResult: false,\n\t\trequiresThinkingAsText: false,\n\t\trequiresReasoningContentOnAssistantMessages: isDeepSeek,\n\t\tthinkingFormat: isDeepSeek\n\t\t\t? \"deepseek\"\n\t\t\t: isZai\n\t\t\t\t? \"zai\"\n\t\t\t\t: isTogether\n\t\t\t\t\t? \"together\"\n\t\t\t\t\t: isAntLing\n\t\t\t\t\t\t? \"ant-ling\"\n\t\t\t\t\t\t: isOpenRouter\n\t\t\t\t\t\t\t? \"openrouter\"\n\t\t\t\t\t\t\t: \"openai\",\n\t\topenRouterRouting: {},\n\t\tvercelGatewayRouting: {},\n\t\tchatTemplateKwargs: {},\n\t\tzaiToolStream: false,\n\t\tsupportsStrictMode: !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia,\n\t\ttoolSchemaFlavor: isMoonshot ? \"moonshot-mfjs\" : undefined,\n\t\tsupportsDisabledThinking: true,\n\t\ttoolCallFormat: undefined,\n\t\tsupportsOpenAIGrammarTools: false,\n\t\tcacheControlFormat,\n\t\tsendSessionAffinityHeaders: isOpenRouter,\n\t\tdeferredToolsMode: undefined,\n\t\tsessionAffinityFormat: isOpenRouter ? \"openrouter\" : \"openai\",\n\t\tsupportsPromptCacheKey: isMoonshot || baseUrl.includes(\"api.openai.com\"),\n\t\tsupportsMaxOutputTokens: true,\n\t\tsupportsLongCacheRetention: !(\n\t\t\tisTogether ||\n\t\t\tisCloudflareWorkersAI ||\n\t\t\tisCloudflareAiGateway ||\n\t\t\tisNvidia ||\n\t\t\tisAntLing\n\t\t),\n\t};\n}\n\n/**\n * Get resolved compatibility settings for a model.\n * Auto-detects from provider/URL then overrides with explicit model.compat.\n */\nexport function getOpenAICompletionsCompat(model: Model<\"openai-completions\">): ResolvedOpenAICompletionsCompat {\n\tconst detected = detectOpenAICompletionsCompat(model);\n\tif (!model.compat) return detected;\n\n\treturn {\n\t\tsupportsStore: model.compat.supportsStore ?? detected.supportsStore,\n\t\tsupportsDeveloperRole: model.compat.supportsDeveloperRole ?? detected.supportsDeveloperRole,\n\t\tsupportsReasoningEffort: model.compat.supportsReasoningEffort ?? detected.supportsReasoningEffort,\n\t\tsupportsUsageInStreaming: model.compat.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,\n\t\tsupportsFinishReason: model.compat.supportsFinishReason ?? detected.supportsFinishReason,\n\t\tmaxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,\n\t\trequiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,\n\t\trequiresAssistantAfterToolResult:\n\t\t\tmodel.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,\n\t\trequiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,\n\t\trequiresReasoningContentOnAssistantMessages:\n\t\t\tmodel.compat.requiresReasoningContentOnAssistantMessages ??\n\t\t\tdetected.requiresReasoningContentOnAssistantMessages,\n\t\tthinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,\n\t\tsupportsDisabledThinking: model.compat.supportsDisabledThinking ?? detected.supportsDisabledThinking,\n\t\topenRouterRouting: model.compat.openRouterRouting ?? detected.openRouterRouting,\n\t\tvercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,\n\t\tchatTemplateKwargs: model.compat.chatTemplateKwargs ?? detected.chatTemplateKwargs,\n\t\tchatTemplateArgs: model.compat.chatTemplateArgs ?? detected.chatTemplateArgs,\n\t\tzaiToolStream: model.compat.zaiToolStream ?? detected.zaiToolStream,\n\t\tsupportsThinkingTokenBudget: model.compat.supportsThinkingTokenBudget ?? detected.supportsThinkingTokenBudget,\n\t\tthinkingTokenBudgetField: model.compat.thinkingTokenBudgetField ?? detected.thinkingTokenBudgetField,\n\t\tsupportsStrictMode: model.compat.supportsStrictMode ?? detected.supportsStrictMode,\n\t\ttoolSchemaFlavor: model.compat.toolSchemaFlavor ?? detected.toolSchemaFlavor,\n\t\ttoolCallFormat: model.compat.toolCallFormat ?? detected.toolCallFormat,\n\t\tsupportsOpenAIGrammarTools: model.compat.supportsOpenAIGrammarTools ?? detected.supportsOpenAIGrammarTools,\n\t\tcacheControlFormat: model.compat.cacheControlFormat ?? detected.cacheControlFormat,\n\t\tsendSessionAffinityHeaders: model.compat.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,\n\t\tdeferredToolsMode: model.compat.deferredToolsMode ?? detected.deferredToolsMode,\n\t\tsessionAffinityFormat: model.compat.sessionAffinityFormat ?? detected.sessionAffinityFormat,\n\t\tsupportsPromptCacheKey: model.compat.supportsPromptCacheKey ?? detected.supportsPromptCacheKey,\n\t\tsupportsMaxOutputTokens: model.compat.supportsMaxOutputTokens ?? detected.supportsMaxOutputTokens,\n\t\tvllmPriority: model.compat.vllmPriority ?? detected.vllmPriority,\n\t\tsupportsLongCacheRetention: model.compat.supportsLongCacheRetention ?? detected.supportsLongCacheRetention,\n\t};\n}\n\nexport function getBedrockModelMatchCandidates(modelId: string, modelName?: string): string[] {\n\tconst values = modelName ? [modelId, modelName] : [modelId];\n\treturn values.flatMap((value) => {\n\t\tconst lower = value.toLowerCase();\n\t\treturn [lower, lower.replace(/[\\s_.:]+/g, \"-\")];\n\t});\n}\n\nexport function supportsOneHourCacheTtl(model: Model<\"bedrock-converse-stream\">): boolean {\n\tconst candidates = getBedrockModelMatchCandidates(model.id, model.name);\n\treturn candidates.some((candidate) =>\n\t\t[\"opus-4-5\", \"sonnet-4-5\", \"haiku-4-5\"].some((modelVersion) => candidate.includes(modelVersion)),\n\t);\n}\n\n/**\n * Check if the model supports prompt caching.\n * Supported: Claude 3.5 Haiku, Claude 3.7 Sonnet, Claude 4.x models, Claude 5 models\n *\n * For base models and system-defined inference profiles the model ID / ARN\n * contains the model name, so we can decide locally.\n *\n * For application inference profiles (whose ARNs don't contain the model name),\n * also checks model.name which is user-controlled via models.json or registerProvider.\n * As a last resort, set AWS_BEDROCK_FORCE_CACHE=1 to enable cache points.\n * Amazon Nova models have automatic caching and don't need explicit cache points.\n */\nexport function supportsPromptCaching(model: Model<\"bedrock-converse-stream\">, env?: ProviderEnv): boolean {\n\tconst candidates = getBedrockModelMatchCandidates(model.id, model.name);\n\n\tconst hasClaudeRef = candidates.some((s) => s.includes(\"claude\"));\n\tif (!hasClaudeRef) {\n\t\t// Application inference profiles don't contain the model name in the ARN.\n\t\t// Allow users to force cache points via environment variable.\n\t\tif (getProviderEnvValue(\"AWS_BEDROCK_FORCE_CACHE\", env) === \"1\") return true;\n\t\treturn false;\n\t}\n\t// Claude 5 models (fable-5, opus-5, sonnet-5)\n\tif (candidates.some((s) => s.includes(\"fable-5\") || s.includes(\"opus-5\") || s.includes(\"sonnet-5\"))) return true;\n\t// Claude 4.x models (opus-4, sonnet-4, haiku-4)\n\tif (candidates.some((s) => s.includes(\"-4-\"))) return true;\n\t// Claude 3.7 Sonnet\n\tif (candidates.some((s) => s.includes(\"claude-3-7-sonnet\"))) return true;\n\t// Claude 3.5 Haiku\n\tif (candidates.some((s) => s.includes(\"claude-3-5-haiku\"))) return true;\n\treturn false;\n}\n\nfunction resolveAnthropicCacheRetention(\n\tcacheRetention?: CacheRetention,\n\tenv?: ProviderEnv,\n\tfallback: CacheRetention = \"short\",\n): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\tif (typeof process !== \"undefined\" && process.env.PI_CACHE_RETENTION !== undefined) {\n\t\treturn \"short\";\n\t}\n\treturn fallback;\n}\n\nfunction resolveOpenAICompletionsCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nfunction resolveBedrockCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nfunction resolveOpenAIResponsesCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nexport function resolvePromptCacheTtlSeconds(model: Model<Api>, env?: ProviderEnv): number | undefined {\n\tswitch (model.api) {\n\t\tcase \"claude-sdk-oauth\":\n\t\t\t// The Claude SDK owns prompt caching for this lane and uses Anthropic's default 5m TTL.\n\t\t\treturn PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\tcase \"anthropic-messages\": {\n\t\t\tconst anthropicModel = model as Model<\"anthropic-messages\">;\n\t\t\tconst retention = resolveAnthropicCacheRetention(anthropicModel.cacheRetention, env, \"short\");\n\t\t\tif (retention === \"none\") return undefined;\n\t\t\treturn retention === \"long\" &&\n\t\t\t\tisAnthropicApiBaseUrl(anthropicModel.baseUrl) &&\n\t\t\t\tgetAnthropicCompat(anthropicModel).supportsLongCacheRetention\n\t\t\t\t? PROMPT_CACHE_TTL_LONG_SECONDS\n\t\t\t\t: PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tcase \"bedrock-converse-stream\": {\n\t\t\tconst bedrockModel = model as Model<\"bedrock-converse-stream\">;\n\t\t\tconst retention = resolveBedrockCacheRetention(bedrockModel.cacheRetention, env);\n\t\t\tif (retention === \"none\" || !supportsPromptCaching(bedrockModel, env)) return undefined;\n\t\t\treturn retention === \"long\" && supportsOneHourCacheTtl(bedrockModel)\n\t\t\t\t? PROMPT_CACHE_TTL_LONG_SECONDS\n\t\t\t\t: PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tcase \"openai-completions\": {\n\t\t\tconst completionsModel = model as Model<\"openai-completions\">;\n\t\t\tconst retention = resolveOpenAICompletionsCacheRetention(completionsModel.cacheRetention, env);\n\t\t\tif (retention === \"none\") return undefined;\n\t\t\tconst compat = getOpenAICompletionsCompat(completionsModel);\n\t\t\tif (compat.cacheControlFormat === \"anthropic\") {\n\t\t\t\treturn retention === \"long\" && compat.supportsLongCacheRetention\n\t\t\t\t\t? PROMPT_CACHE_TTL_LONG_SECONDS\n\t\t\t\t\t: PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t\t}\n\t\t\treturn PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tcase \"openai-responses\":\n\t\tcase \"openai-codex-responses\":\n\t\tcase \"azure-openai-responses\": {\n\t\t\tconst retention = resolveOpenAIResponsesCacheRetention(model.cacheRetention, env);\n\t\t\treturn retention === \"none\" ? undefined : PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tdefault:\n\t\t\treturn undefined;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"prompt-cache-ttl.js","sourceRoot":"","sources":["../../src/utils/prompt-cache-ttl.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,CAAC;AAClD,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAC;AAElD,MAAM,UAAU,qBAAqB,CAAC,OAAe;IACpD,IAAI,CAAC;QACJ,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,mBAAmB,CAAC;IAC1D,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,KAAK,CAAC;IACd,CAAC;AACF,CAAC;AAED,MAAM,+BAA+B,GAAG,kCAAkC,CAAC;AAE3E;;;;GAIG;AACH,SAAS,6BAA6B,CAAC,KAAkC;IACxE,IAAI,KAAK,CAAC,QAAQ,KAAK,WAAW,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,KAAK,CAAC;IAC/E,MAAM,OAAO,GAAG,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,uDAAuD,CAAC,CA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AAC,8BAA8B,CAAC;YACnC,CAAC;YACD,OAAO,8BAA8B,CAAC;QACvC,CAAC;QACD,KAAK,kBAAkB,CAAC;QACxB,KAAK,wBAAwB,CAAC;QAC9B,KAAK,wBAAwB,EAAE,CAAC;YAC/B,MAAM,SAAS,GAAG,oCAAoC,CAAC,KAAK,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;YAClF,OAAO,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,8BAA8B,CAAC;QAC1E,CAAC;QACD;YACC,OAAO,SAAS,CAAC;IACnB,CAAC;AACF,CAAC","sourcesContent":["import type {\n\tAnthropicMessagesCompat,\n\tApi,\n\tCacheRetention,\n\tModel,\n\tOpenAICompletionsCompat,\n\tProviderEnv,\n} from \"../types.ts\";\nimport { getProviderEnvValue } from \"./provider-env.ts\";\n\nexport const PROMPT_CACHE_TTL_SHORT_SECONDS = 300;\nexport const PROMPT_CACHE_TTL_LONG_SECONDS = 3600;\n\nexport function isAnthropicApiBaseUrl(baseUrl: string): boolean {\n\ttry {\n\t\treturn new URL(baseUrl).hostname === \"api.anthropic.com\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nconst CLAUDE_FABLE_OR_MYTHOS_MODEL_ID = /^claude-(?:fable|mythos)(?:-|$)/i;\n\n/**\n * Default for `supportsToolReferences`: first-party Anthropic models except\n * Haiku (rejects client-side tool_reference blocks) and models that predate\n * tool search (Claude 3.x, Opus/Sonnet 4.0, Opus 4.1).\n */\nfunction defaultSupportsToolReferences(model: Model<\"anthropic-messages\">): boolean {\n\tif (model.provider !== \"anthropic\" || model.id.includes(\"haiku\")) return false;\n\tconst version = model.id.match(/^claude-(?:opus|sonnet|fable)-(\\d+)(?:-(\\d+))?(?:-|$)/);\n\tif (!version) return false;\n\tconst major = Number(version[1]);\n\tconst minor = version[2] && version[2].length < 8 ? Number(version[2]) : 0;\n\treturn major > 4 || (major === 4 && minor >= 5);\n}\n\nexport function getAnthropicCompat(\n\tmodel: Model<\"anthropic-messages\">,\n): Required<\n\tOmit<AnthropicMessagesCompat, \"forceAdaptiveThinking\" | \"supportsMidConvoEffort\" | \"sessionAffinityFormat\">\n> &\n\tPick<AnthropicMessagesCompat, \"sessionAffinityFormat\"> {\n\t// Auto-detect session affinity and cache control support from provider\n\tconst isFireworks = model.provider === \"fireworks\";\n\tconst isCloudflareAiGatewayAnthropic =\n\t\tmodel.provider === \"cloudflare-ai-gateway\" && model.baseUrl.includes(\"anthropic\");\n\tconst isXiaomi = model.provider === \"xiaomi\" || model.provider.startsWith(\"xiaomi-token-plan-\");\n\t// OpenRouter carries prompt-cache affinity on its own x-session-id header and\n\t// rejects x-session-affinity (earendil-works/pi#9102).\n\tconst isOpenRouter = model.provider === \"openrouter\" || model.baseUrl.includes(\"openrouter.ai\");\n\treturn {\n\t\tsupportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? !isFireworks,\n\t\tsupportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? !isFireworks,\n\t\tsendSessionAffinityHeaders:\n\t\t\tmodel.compat?.sendSessionAffinityHeaders ?? !!(isFireworks || isCloudflareAiGatewayAnthropic || isOpenRouter),\n\t\tsessionAffinityFormat: model.compat?.sessionAffinityFormat ?? (isOpenRouter ? \"openrouter\" : undefined),\n\t\tsupportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? !isFireworks,\n\t\tsupportsDisabledThinking: model.compat?.supportsDisabledThinking ?? !isXiaomi,\n\t\tsupportsTemperature: model.compat?.supportsTemperature ?? true,\n\t\tsupportsToolChoice: model.compat?.supportsToolChoice ?? true,\n\t\tsupportsForcedToolChoice:\n\t\t\tmodel.compat?.supportsForcedToolChoice ?? !CLAUDE_FABLE_OR_MYTHOS_MODEL_ID.test(model.id),\n\t\tallowEmptySignature: model.compat?.allowEmptySignature ?? false,\n\t\tunsignedThinkingReplay:\n\t\t\tmodel.compat?.unsignedThinkingReplay ?? (model.compat?.allowEmptySignature ? \"empty-signature\" : \"text\"),\n\t\tallowedFallbackModels: model.compat?.allowedFallbackModels ?? [],\n\t\tsupportsStrictTools: model.compat?.supportsStrictTools ?? false,\n\t\tsupportsToolReferences: model.compat?.supportsToolReferences ?? defaultSupportsToolReferences(model),\n\t\t// Default: first-party Anthropic only. Anthropic-compatible providers\n\t\t// (kimi-coding, fireworks, copilot, gateways) may execute the server-side\n\t\t// search but reject the replayed server_tool_use / web_search_tool_result\n\t\t// blocks on the next request (kimi-coding 400s with `tool_call_id is not\n\t\t// found`).\n\t\tsupportsWebSearch: model.compat?.supportsWebSearch ?? isAnthropicApiBaseUrl(model.baseUrl),\n\t};\n}\n\nexport type ResolvedOpenAICompletionsCompat = Omit<\n\tRequired<OpenAICompletionsCompat>,\n\t| \"vllmPriority\"\n\t| \"cacheControlFormat\"\n\t| \"toolCallFormat\"\n\t| \"deferredToolsMode\"\n\t| \"toolSchemaFlavor\"\n\t| \"supportsPromptCacheKey\"\n\t| \"chatTemplateArgs\"\n\t| \"supportsThinkingTokenBudget\"\n\t| \"thinkingTokenBudgetField\"\n\t| \"veniceParameters\"\n> & {\n\tcacheControlFormat?: OpenAICompletionsCompat[\"cacheControlFormat\"];\n\tsupportsPromptCacheKey?: OpenAICompletionsCompat[\"supportsPromptCacheKey\"];\n\ttoolCallFormat?: OpenAICompletionsCompat[\"toolCallFormat\"];\n\tdeferredToolsMode?: OpenAICompletionsCompat[\"deferredToolsMode\"];\n\ttoolSchemaFlavor?: OpenAICompletionsCompat[\"toolSchemaFlavor\"];\n\tchatTemplateArgs?: OpenAICompletionsCompat[\"chatTemplateArgs\"];\n\tsupportsThinkingTokenBudget?: OpenAICompletionsCompat[\"supportsThinkingTokenBudget\"];\n\tthinkingTokenBudgetField?: OpenAICompletionsCompat[\"thinkingTokenBudgetField\"];\n\t/** vLLM `priority`; off by default and never set on the generated catalog. */\n\tvllmPriority?: OpenAICompletionsCompat[\"vllmPriority\"];\n\t/** Venice `venice_parameters`; only set on the generated Venice catalog. */\n\tveniceParameters?: OpenAICompletionsCompat[\"veniceParameters\"];\n};\n\n/**\n * Detect compatibility settings from provider and baseUrl for known providers.\n * Provider takes precedence over URL-based detection since it's explicitly configured.\n * Returns a fully resolved OpenAICompletionsCompat object with all fields set.\n */\nfunction detectOpenAICompletionsCompat(model: Model<\"openai-completions\">): ResolvedOpenAICompletionsCompat {\n\tconst provider = model.provider;\n\tconst baseUrl = model.baseUrl;\n\n\tconst isZai =\n\t\tprovider === \"zai\" ||\n\t\tprovider === \"zai-coding-cn\" ||\n\t\tbaseUrl.includes(\"api.z.ai\") ||\n\t\tbaseUrl.includes(\"open.bigmodel.cn\");\n\tconst isTogether =\n\t\tprovider === \"together\" || baseUrl.includes(\"api.together.ai\") || baseUrl.includes(\"api.together.xyz\");\n\tconst isMoonshot = provider === \"moonshotai\" || provider === \"moonshotai-cn\" || baseUrl.includes(\"api.moonshot.\");\n\tconst isOpenRouter = provider === \"openrouter\" || baseUrl.includes(\"openrouter.ai\");\n\tconst isCloudflareWorkersAI = provider === \"cloudflare-workers-ai\" || baseUrl.includes(\"api.cloudflare.com\");\n\tconst isCloudflareAiGateway = provider === \"cloudflare-ai-gateway\" || baseUrl.includes(\"gateway.ai.cloudflare.com\");\n\tconst isNvidia = provider === \"nvidia\" || baseUrl.includes(\"integrate.api.nvidia.com\");\n\tconst isAntLing = provider === \"ant-ling\" || baseUrl.includes(\"api.ant-ling.com\");\n\tconst isDeepSeek = provider === \"deepseek\" || baseUrl.toLowerCase().includes(\"deepseek.com\");\n\n\tconst isNonStandard =\n\t\tisNvidia ||\n\t\tprovider === \"cerebras\" ||\n\t\tbaseUrl.includes(\"cerebras.ai\") ||\n\t\tprovider === \"xai\" ||\n\t\tbaseUrl.includes(\"api.x.ai\") ||\n\t\tisTogether ||\n\t\tbaseUrl.includes(\"chutes.ai\") ||\n\t\tisDeepSeek ||\n\t\tisZai ||\n\t\tisMoonshot ||\n\t\tprovider === \"opencode\" ||\n\t\tbaseUrl.includes(\"opencode.ai\") ||\n\t\tisCloudflareWorkersAI ||\n\t\tisCloudflareAiGateway ||\n\t\tisAntLing;\n\n\tconst useMaxTokens =\n\t\tbaseUrl.includes(\"chutes.ai\") ||\n\t\tisDeepSeek ||\n\t\tisMoonshot ||\n\t\tisCloudflareAiGateway ||\n\t\tisTogether ||\n\t\tisNvidia ||\n\t\tisAntLing ||\n\t\tisZai;\n\n\tconst isGrok = provider === \"xai\" || baseUrl.includes(\"api.x.ai\");\n\tconst isOpenRouterDeveloperRoleModel =\n\t\tisOpenRouter && (model.id.startsWith(\"anthropic/\") || model.id.startsWith(\"openai/\"));\n\tconst openRouterCacheControlPrefixes = [\"anthropic/\", \"qwen/\", \"google/\"];\n\tconst cacheControlModelId = model.id.startsWith(\"~\") ? model.id.slice(1) : model.id;\n\tconst supportsOpenRouterCacheControl = openRouterCacheControlPrefixes.some((prefix) =>\n\t\tcacheControlModelId.startsWith(prefix),\n\t);\n\tconst cacheControlFormat = provider === \"openrouter\" && supportsOpenRouterCacheControl ? \"anthropic\" : undefined;\n\n\treturn {\n\t\tsupportsStore: !isNonStandard,\n\t\tsupportsDeveloperRole: isOpenRouterDeveloperRoleModel || (!isNonStandard && !isOpenRouter),\n\t\tsupportsReasoningEffort:\n\t\t\t!isGrok && !isZai && !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia && !isAntLing,\n\t\tsupportsUsageInStreaming: true,\n\t\tsupportsFinishReason: true,\n\t\tmaxTokensField: useMaxTokens ? \"max_tokens\" : \"max_completion_tokens\",\n\t\trequiresToolResultName: false,\n\t\trequiresAssistantAfterToolResult: false,\n\t\trequiresThinkingAsText: false,\n\t\trequiresReasoningContentOnAssistantMessages: isDeepSeek,\n\t\tthinkingFormat: isDeepSeek\n\t\t\t? \"deepseek\"\n\t\t\t: isZai\n\t\t\t\t? \"zai\"\n\t\t\t\t: isTogether\n\t\t\t\t\t? \"together\"\n\t\t\t\t\t: isAntLing\n\t\t\t\t\t\t? \"ant-ling\"\n\t\t\t\t\t\t: isOpenRouter\n\t\t\t\t\t\t\t? \"openrouter\"\n\t\t\t\t\t\t\t: \"openai\",\n\t\topenRouterRouting: {},\n\t\tvercelGatewayRouting: {},\n\t\tchatTemplateKwargs: {},\n\t\tzaiToolStream: false,\n\t\tsupportsStrictMode: !isMoonshot && !isTogether && !isCloudflareAiGateway && !isNvidia,\n\t\ttoolSchemaFlavor: isMoonshot ? \"moonshot-mfjs\" : undefined,\n\t\tsupportsDisabledThinking: true,\n\t\ttoolCallFormat: undefined,\n\t\tsupportsOpenAIGrammarTools: false,\n\t\tcacheControlFormat,\n\t\tsendSessionAffinityHeaders: isOpenRouter,\n\t\tdeferredToolsMode: undefined,\n\t\tsessionAffinityFormat: isOpenRouter ? \"openrouter\" : \"openai\",\n\t\tsupportsPromptCacheKey: isMoonshot || baseUrl.includes(\"api.openai.com\"),\n\t\tsupportsMaxOutputTokens: true,\n\t\tsupportsLongCacheRetention: !(\n\t\t\tisTogether ||\n\t\t\tisCloudflareWorkersAI ||\n\t\t\tisCloudflareAiGateway ||\n\t\t\tisNvidia ||\n\t\t\tisAntLing\n\t\t),\n\t};\n}\n\n/**\n * Get resolved compatibility settings for a model.\n * Auto-detects from provider/URL then overrides with explicit model.compat.\n */\nexport function getOpenAICompletionsCompat(model: Model<\"openai-completions\">): ResolvedOpenAICompletionsCompat {\n\tconst detected = detectOpenAICompletionsCompat(model);\n\tif (!model.compat) return detected;\n\n\treturn {\n\t\tsupportsStore: model.compat.supportsStore ?? detected.supportsStore,\n\t\tsupportsDeveloperRole: model.compat.supportsDeveloperRole ?? detected.supportsDeveloperRole,\n\t\tsupportsReasoningEffort: model.compat.supportsReasoningEffort ?? detected.supportsReasoningEffort,\n\t\tsupportsUsageInStreaming: model.compat.supportsUsageInStreaming ?? detected.supportsUsageInStreaming,\n\t\tsupportsFinishReason: model.compat.supportsFinishReason ?? detected.supportsFinishReason,\n\t\tmaxTokensField: model.compat.maxTokensField ?? detected.maxTokensField,\n\t\trequiresToolResultName: model.compat.requiresToolResultName ?? detected.requiresToolResultName,\n\t\trequiresAssistantAfterToolResult:\n\t\t\tmodel.compat.requiresAssistantAfterToolResult ?? detected.requiresAssistantAfterToolResult,\n\t\trequiresThinkingAsText: model.compat.requiresThinkingAsText ?? detected.requiresThinkingAsText,\n\t\trequiresReasoningContentOnAssistantMessages:\n\t\t\tmodel.compat.requiresReasoningContentOnAssistantMessages ??\n\t\t\tdetected.requiresReasoningContentOnAssistantMessages,\n\t\tthinkingFormat: model.compat.thinkingFormat ?? detected.thinkingFormat,\n\t\tsupportsDisabledThinking: model.compat.supportsDisabledThinking ?? detected.supportsDisabledThinking,\n\t\topenRouterRouting: model.compat.openRouterRouting ?? detected.openRouterRouting,\n\t\tvercelGatewayRouting: model.compat.vercelGatewayRouting ?? detected.vercelGatewayRouting,\n\t\tchatTemplateKwargs: model.compat.chatTemplateKwargs ?? detected.chatTemplateKwargs,\n\t\tchatTemplateArgs: model.compat.chatTemplateArgs ?? detected.chatTemplateArgs,\n\t\tzaiToolStream: model.compat.zaiToolStream ?? detected.zaiToolStream,\n\t\tsupportsThinkingTokenBudget: model.compat.supportsThinkingTokenBudget ?? detected.supportsThinkingTokenBudget,\n\t\tthinkingTokenBudgetField: model.compat.thinkingTokenBudgetField ?? detected.thinkingTokenBudgetField,\n\t\tsupportsStrictMode: model.compat.supportsStrictMode ?? detected.supportsStrictMode,\n\t\ttoolSchemaFlavor: model.compat.toolSchemaFlavor ?? detected.toolSchemaFlavor,\n\t\ttoolCallFormat: model.compat.toolCallFormat ?? detected.toolCallFormat,\n\t\tsupportsOpenAIGrammarTools: model.compat.supportsOpenAIGrammarTools ?? detected.supportsOpenAIGrammarTools,\n\t\tcacheControlFormat: model.compat.cacheControlFormat ?? detected.cacheControlFormat,\n\t\tsendSessionAffinityHeaders: model.compat.sendSessionAffinityHeaders ?? detected.sendSessionAffinityHeaders,\n\t\tdeferredToolsMode: model.compat.deferredToolsMode ?? detected.deferredToolsMode,\n\t\tsessionAffinityFormat: model.compat.sessionAffinityFormat ?? detected.sessionAffinityFormat,\n\t\tsupportsPromptCacheKey: model.compat.supportsPromptCacheKey ?? detected.supportsPromptCacheKey,\n\t\tsupportsMaxOutputTokens: model.compat.supportsMaxOutputTokens ?? detected.supportsMaxOutputTokens,\n\t\tvllmPriority: model.compat.vllmPriority ?? detected.vllmPriority,\n\t\tsupportsLongCacheRetention: model.compat.supportsLongCacheRetention ?? detected.supportsLongCacheRetention,\n\t};\n}\n\nexport function getBedrockModelMatchCandidates(modelId: string, modelName?: string): string[] {\n\tconst values = modelName ? [modelId, modelName] : [modelId];\n\treturn values.flatMap((value) => {\n\t\tconst lower = value.toLowerCase();\n\t\treturn [lower, lower.replace(/[\\s_.:]+/g, \"-\")];\n\t});\n}\n\nexport function supportsOneHourCacheTtl(model: Model<\"bedrock-converse-stream\">): boolean {\n\tconst candidates = getBedrockModelMatchCandidates(model.id, model.name);\n\treturn candidates.some((candidate) =>\n\t\t[\"opus-4-5\", \"sonnet-4-5\", \"haiku-4-5\"].some((modelVersion) => candidate.includes(modelVersion)),\n\t);\n}\n\n/**\n * Check if the model supports prompt caching.\n * Supported: Claude 3.5 Haiku, Claude 3.7 Sonnet, Claude 4.x models, Claude 5 models\n *\n * For base models and system-defined inference profiles the model ID / ARN\n * contains the model name, so we can decide locally.\n *\n * For application inference profiles (whose ARNs don't contain the model name),\n * also checks model.name which is user-controlled via models.json or registerProvider.\n * As a last resort, set AWS_BEDROCK_FORCE_CACHE=1 to enable cache points.\n * Amazon Nova models have automatic caching and don't need explicit cache points.\n */\nexport function supportsPromptCaching(model: Model<\"bedrock-converse-stream\">, env?: ProviderEnv): boolean {\n\tconst candidates = getBedrockModelMatchCandidates(model.id, model.name);\n\n\tconst hasClaudeRef = candidates.some((s) => s.includes(\"claude\"));\n\tif (!hasClaudeRef) {\n\t\t// Application inference profiles don't contain the model name in the ARN.\n\t\t// Allow users to force cache points via environment variable.\n\t\tif (getProviderEnvValue(\"AWS_BEDROCK_FORCE_CACHE\", env) === \"1\") return true;\n\t\treturn false;\n\t}\n\t// Claude 5 models (fable-5, opus-5, sonnet-5)\n\tif (candidates.some((s) => s.includes(\"fable-5\") || s.includes(\"opus-5\") || s.includes(\"sonnet-5\"))) return true;\n\t// Claude 4.x models (opus-4, sonnet-4, haiku-4)\n\tif (candidates.some((s) => s.includes(\"-4-\"))) return true;\n\t// Claude 3.7 Sonnet\n\tif (candidates.some((s) => s.includes(\"claude-3-7-sonnet\"))) return true;\n\t// Claude 3.5 Haiku\n\tif (candidates.some((s) => s.includes(\"claude-3-5-haiku\"))) return true;\n\treturn false;\n}\n\nfunction resolveAnthropicCacheRetention(\n\tcacheRetention?: CacheRetention,\n\tenv?: ProviderEnv,\n\tfallback: CacheRetention = \"short\",\n): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\tif (typeof process !== \"undefined\" && process.env.PI_CACHE_RETENTION !== undefined) {\n\t\treturn \"short\";\n\t}\n\treturn fallback;\n}\n\nfunction resolveOpenAICompletionsCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nfunction resolveBedrockCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nfunction resolveOpenAIResponsesCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\tif (getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\") {\n\t\treturn \"long\";\n\t}\n\treturn \"short\";\n}\n\nexport function resolvePromptCacheTtlSeconds(model: Model<Api>, env?: ProviderEnv): number | undefined {\n\tswitch (model.api) {\n\t\tcase \"claude-sdk-oauth\":\n\t\t\t// The Claude SDK owns prompt caching for this lane and uses Anthropic's default 5m TTL.\n\t\t\treturn PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\tcase \"anthropic-messages\": {\n\t\t\tconst anthropicModel = model as Model<\"anthropic-messages\">;\n\t\t\tconst retention = resolveAnthropicCacheRetention(anthropicModel.cacheRetention, env, \"short\");\n\t\t\tif (retention === \"none\") return undefined;\n\t\t\treturn retention === \"long\" &&\n\t\t\t\tisAnthropicApiBaseUrl(anthropicModel.baseUrl) &&\n\t\t\t\tgetAnthropicCompat(anthropicModel).supportsLongCacheRetention\n\t\t\t\t? PROMPT_CACHE_TTL_LONG_SECONDS\n\t\t\t\t: PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tcase \"bedrock-converse-stream\": {\n\t\t\tconst bedrockModel = model as Model<\"bedrock-converse-stream\">;\n\t\t\tconst retention = resolveBedrockCacheRetention(bedrockModel.cacheRetention, env);\n\t\t\tif (retention === \"none\" || !supportsPromptCaching(bedrockModel, env)) return undefined;\n\t\t\treturn retention === \"long\" && supportsOneHourCacheTtl(bedrockModel)\n\t\t\t\t? PROMPT_CACHE_TTL_LONG_SECONDS\n\t\t\t\t: PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tcase \"openai-completions\": {\n\t\t\tconst completionsModel = model as Model<\"openai-completions\">;\n\t\t\tconst retention = resolveOpenAICompletionsCacheRetention(completionsModel.cacheRetention, env);\n\t\t\tif (retention === \"none\") return undefined;\n\t\t\tconst compat = getOpenAICompletionsCompat(completionsModel);\n\t\t\tif (compat.cacheControlFormat === \"anthropic\") {\n\t\t\t\treturn retention === \"long\" && compat.supportsLongCacheRetention\n\t\t\t\t\t? PROMPT_CACHE_TTL_LONG_SECONDS\n\t\t\t\t\t: PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t\t}\n\t\t\treturn PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tcase \"openai-responses\":\n\t\tcase \"openai-codex-responses\":\n\t\tcase \"azure-openai-responses\": {\n\t\t\tconst retention = resolveOpenAIResponsesCacheRetention(model.cacheRetention, env);\n\t\t\treturn retention === \"none\" ? undefined : PROMPT_CACHE_TTL_SHORT_SECONDS;\n\t\t}\n\t\tdefault:\n\t\t\treturn undefined;\n\t}\n}\n"]}
|
|
@@ -67,7 +67,16 @@ export interface RetryBackoffPolicy {
|
|
|
67
67
|
readonly perAttemptCapMs: number | null;
|
|
68
68
|
readonly jitter: RetryJitterPolicy;
|
|
69
69
|
}
|
|
70
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* Ceiling on a server-requested wait for `override`-mode hint policies.
|
|
72
|
+
*
|
|
73
|
+
* On the turn stage this ceiling is profile-owned end to end: besides driving
|
|
74
|
+
* the planner's `over-ceiling` verdict, an override-mode `maxDelayMs` also
|
|
75
|
+
* governs the coding-agent session's agent-level wait clamp — `null` passes
|
|
76
|
+
* every admitted wait through verbatim, a number is the profile's own cap, and
|
|
77
|
+
* the session's `retry.maxAgentDelayMs` default applies only to profiles that
|
|
78
|
+
* declare no ceiling of their own.
|
|
79
|
+
*/
|
|
71
80
|
export interface RetryHintCeiling {
|
|
72
81
|
/** `null` means no ceiling, so `over-ceiling` is unreachable. */
|
|
73
82
|
readonly maxDelayMs: number | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/utils/retry-profile/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,iFAAiF;AACjF,MAAM,MAAM,gBAAgB,GACzB,OAAO,GACP,YAAY,GACZ,SAAS,GACT,gBAAgB,GAChB,iBAAiB,GACjB,aAAa,GACb,cAAc,GACd,UAAU,GACV,SAAS,GACT,WA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AAC,OAAO,EAAE,gBAAgB,CAAC;CAClC,GACD;IACA,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,uBAAuB,CAAC;CAC1C,CAAC;AAEL,MAAM,WAAW,gBAAgB;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,QAAQ,CAAC,iBAAiB,EAAE,kBAAkB,CAAC;IAC/C,QAAQ,CAAC,UAAU,EAAE,qBAAqB,CAAC;IAC3C,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC;CACnC;AAED,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,eAAe,EAAE,gBAAgB,CAAC;IAC3C,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,QAAQ,EAAE;QAClB,QAAQ,CAAC,QAAQ,EAAE,uBAAuB,CAAC;QAC3C,QAAQ,CAAC,SAAS,EAAE,mBAAmB,CAAC;QACxC,QAAQ,CAAC,WAAW,EAAE,QAAQ,GAAG,mBAAmB,CAAC;QACrD,QAAQ,CAAC,wBAAwB,EAAE,IAAI,CAAC;KACxC,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/retry-profile/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG","sourcesContent":["/**\n * Pure value types for provider-declared retry policy profiles.\n *\n * A {@link RetryPolicyProfile} describes how one provider's requests are\n * retried at two independent stages — the provider/transport request and the\n * assistant turn — plus how the model-fallback chain interacts with the turn\n * budget. These types carry no behaviour; scheduling functions live in sibling\n * modules (`backoff.ts`, and later waves) so the contract stays pure data.\n */\n\n/** Normalized failure category; the classifier union is exhaustive over this. */\nexport type RetryFailureKind =\n\t| \"abort\"\n\t| \"connection\"\n\t| \"timeout\"\n\t| \"empty-response\"\n\t| \"quota-exhausted\"\n\t| \"http-status\"\n\t| \"image-format\"\n\t| \"provider\"\n\t| \"refusal\"\n\t| \"sensitive\"\n\t| \"unknown\";\n\n/**\n * Whitelisted facts about one provider failure, normalized at the API\n * boundary. Only these fields are ever retained: raw `Headers` objects,\n * authorization values, and response bodies are never carried here.\n */\nexport interface RetryFailure {\n\t/** API surface that captured the failure (e.g. \"anthropic-messages\"). */\n\treadonly origin: string;\n\treadonly kind: RetryFailureKind;\n\t/** Byte-identical to the error string today's regex classifier matches on. */\n\treadonly message: string;\n\treadonly statusCode?: number;\n\t/** Nested provider `error.type` / `error.code` strings when present. */\n\treadonly providerCodes?: readonly string[];\n\treadonly finishReason?: string;\n\t/** Parsed server-requested wait in ms when a hint exists. */\n\treadonly retryAfterMs?: number;\n\t/** Provider `x-should-retry` verdict when the transport exposed one. */\n\treadonly shouldRetry?: boolean;\n}\n\n/** Verdict of a stage classifier: how the retry loop may treat a failure. */\nexport type RetryClassification =\n\t| { readonly verdict: \"transient\" }\n\t| { readonly verdict: \"rate-limited\" }\n\t| { readonly verdict: \"terminal\" };\n\n/** Classifies a normalized failure into a retry verdict. */\nexport type RetryClassifier = (failure: RetryFailure) => RetryClassification;\n\n/**\n * Narrow server-hint extractor: returns a positive server-requested wait in\n * milliseconds, or undefined when the failure carries none. Profiles declare\n * their own; senpi's richer five-source precedence lives in `../retry-hint.ts`\n * and is not part of this contract.\n */\nexport type RetryHintExtractor = (failure: RetryFailure) => number | undefined;\n\n/**\n * Jitter applied AFTER the per-attempt cap: `additive` grows the delay by up\n * to `ratio` (random * ratio * capped), `subtractive` shrinks it by the same\n * amount. `none` returns the capped delay unchanged.\n */\nexport type RetryJitterPolicy =\n\t| { readonly mode: \"none\" }\n\t| { readonly mode: \"additive\" | \"subtractive\"; readonly ratio: number };\n\nexport interface RetryBackoffPolicy {\n\treadonly baseDelayMs: number;\n\treadonly growthFactor: number;\n\t/**\n\t * Per-attempt cap applied to `baseDelayMs * growthFactor ** (retryNumber - 1)`\n\t * BEFORE jitter. `null` means no cap; `0` is a literal zero cap.\n\t */\n\treadonly perAttemptCapMs: number | null;\n\treadonly jitter: RetryJitterPolicy;\n}\n\n
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/utils/retry-profile/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG","sourcesContent":["/**\n * Pure value types for provider-declared retry policy profiles.\n *\n * A {@link RetryPolicyProfile} describes how one provider's requests are\n * retried at two independent stages — the provider/transport request and the\n * assistant turn — plus how the model-fallback chain interacts with the turn\n * budget. These types carry no behaviour; scheduling functions live in sibling\n * modules (`backoff.ts`, and later waves) so the contract stays pure data.\n */\n\n/** Normalized failure category; the classifier union is exhaustive over this. */\nexport type RetryFailureKind =\n\t| \"abort\"\n\t| \"connection\"\n\t| \"timeout\"\n\t| \"empty-response\"\n\t| \"quota-exhausted\"\n\t| \"http-status\"\n\t| \"image-format\"\n\t| \"provider\"\n\t| \"refusal\"\n\t| \"sensitive\"\n\t| \"unknown\";\n\n/**\n * Whitelisted facts about one provider failure, normalized at the API\n * boundary. Only these fields are ever retained: raw `Headers` objects,\n * authorization values, and response bodies are never carried here.\n */\nexport interface RetryFailure {\n\t/** API surface that captured the failure (e.g. \"anthropic-messages\"). */\n\treadonly origin: string;\n\treadonly kind: RetryFailureKind;\n\t/** Byte-identical to the error string today's regex classifier matches on. */\n\treadonly message: string;\n\treadonly statusCode?: number;\n\t/** Nested provider `error.type` / `error.code` strings when present. */\n\treadonly providerCodes?: readonly string[];\n\treadonly finishReason?: string;\n\t/** Parsed server-requested wait in ms when a hint exists. */\n\treadonly retryAfterMs?: number;\n\t/** Provider `x-should-retry` verdict when the transport exposed one. */\n\treadonly shouldRetry?: boolean;\n}\n\n/** Verdict of a stage classifier: how the retry loop may treat a failure. */\nexport type RetryClassification =\n\t| { readonly verdict: \"transient\" }\n\t| { readonly verdict: \"rate-limited\" }\n\t| { readonly verdict: \"terminal\" };\n\n/** Classifies a normalized failure into a retry verdict. */\nexport type RetryClassifier = (failure: RetryFailure) => RetryClassification;\n\n/**\n * Narrow server-hint extractor: returns a positive server-requested wait in\n * milliseconds, or undefined when the failure carries none. Profiles declare\n * their own; senpi's richer five-source precedence lives in `../retry-hint.ts`\n * and is not part of this contract.\n */\nexport type RetryHintExtractor = (failure: RetryFailure) => number | undefined;\n\n/**\n * Jitter applied AFTER the per-attempt cap: `additive` grows the delay by up\n * to `ratio` (random * ratio * capped), `subtractive` shrinks it by the same\n * amount. `none` returns the capped delay unchanged.\n */\nexport type RetryJitterPolicy =\n\t| { readonly mode: \"none\" }\n\t| { readonly mode: \"additive\" | \"subtractive\"; readonly ratio: number };\n\nexport interface RetryBackoffPolicy {\n\treadonly baseDelayMs: number;\n\treadonly growthFactor: number;\n\t/**\n\t * Per-attempt cap applied to `baseDelayMs * growthFactor ** (retryNumber - 1)`\n\t * BEFORE jitter. `null` means no cap; `0` is a literal zero cap.\n\t */\n\treadonly perAttemptCapMs: number | null;\n\treadonly jitter: RetryJitterPolicy;\n}\n\n/**\n * Ceiling on a server-requested wait for `override`-mode hint policies.\n *\n * On the turn stage this ceiling is profile-owned end to end: besides driving\n * the planner's `over-ceiling` verdict, an override-mode `maxDelayMs` also\n * governs the coding-agent session's agent-level wait clamp — `null` passes\n * every admitted wait through verbatim, a number is the profile's own cap, and\n * the session's `retry.maxAgentDelayMs` default applies only to profiles that\n * declare no ceiling of their own.\n */\nexport interface RetryHintCeiling {\n\t/** `null` means no ceiling, so `over-ceiling` is unreachable. */\n\treadonly maxDelayMs: number | null;\n\treadonly onExceeded: \"error-with-marker\";\n}\n\n/** Tier verdict forwarded unchanged by the delay planner for tiered stages. */\nexport interface RetryTieredHintDecision {\n\t/** Tier label owned by the injected strategy (senpi's tiers live in coding-agent). */\n\treadonly tier: string;\n\treadonly delayMs: number;\n}\n\n/**\n * Tiered-hint strategy, injected from the coding-agent side. packages/ai must\n * not import packages/coding-agent, so the senpi-default profile receives its\n * tier engine (`classifyRateLimitedWait` / `nextInTurnDelayMs` /\n * `degradeWithoutFallback`) as a function value.\n */\nexport type RetryTieredHintStrategy = (\n\tfailure: RetryFailure,\n\tretryNumber: number,\n\tbackoff: RetryBackoffPolicy,\n\tnow: number,\n) => RetryTieredHintDecision;\n\n/**\n * How a stage treats a server-requested wait. `override` replaces the computed\n * backoff with the hint (positive by default; `acceptZero` opts into zero);\n * `tiered` delegates the decision to the injected tier strategy.\n */\nexport type RetryServerHintPolicy =\n\t| {\n\t\t\treadonly mode: \"override\";\n\t\t\treadonly acceptZero: boolean;\n\t\t\treadonly ceiling: RetryHintCeiling;\n\t }\n\t| {\n\t\t\treadonly mode: \"tiered\";\n\t\t\treadonly strategy: RetryTieredHintStrategy;\n\t };\n\nexport interface RetryStagePolicy {\n\treadonly enabled: boolean;\n\treadonly maxRetries: number;\n\treadonly backoff: RetryBackoffPolicy;\n\treadonly extractServerHint: RetryHintExtractor;\n\treadonly serverHint: RetryServerHintPolicy;\n\treadonly classify: RetryClassifier;\n}\n\nexport interface RetryPolicyProfile {\n\treadonly id: string;\n\treadonly providerRequest: RetryStagePolicy;\n\treadonly turn: RetryStagePolicy;\n\treadonly fallback: {\n\t\treadonly terminal: \"immediate-if-eligible\";\n\t\treadonly transient: \"after-turn-budget\";\n\t\treadonly rateLimited: \"tiered\" | \"after-turn-budget\";\n\t\treadonly resetBudgetOnModelChange: true;\n\t};\n}\n"]}
|
package/dist/utils/retry.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { AssistantMessage } from "../types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Retry policy: bounded attempts with exponential backoff (`baseDelayMs * 2^(attempt-1)`).
|
|
4
|
-
*
|
|
4
|
+
* `maxAgentDelayMs` caps each computed delay and defaults to 60 seconds.
|
|
5
|
+
* Matches `settings.retry` (`enabled`, `maxRetries`, `baseDelayMs`, `maxAgentDelayMs`) in coding-agent; kept
|
|
5
6
|
* here so the classifier and the policy-driven retry loop live together and stay reusable
|
|
6
7
|
* by the SDK and other callers.
|
|
7
8
|
*/
|
|
@@ -11,10 +12,20 @@ export interface RetryPolicy {
|
|
|
11
12
|
maxRetries: number;
|
|
12
13
|
/** Base delay in ms. Per-attempt delay is `baseDelayMs * 2^(attempt-1)` before jitter. */
|
|
13
14
|
baseDelayMs: number;
|
|
15
|
+
/** Optional cap for agent-level retry delays in ms. Defaults to 60 seconds. */
|
|
16
|
+
maxAgentDelayMs?: number;
|
|
14
17
|
/** Injectable source used for Codex-style +/-10% backoff jitter. */
|
|
15
18
|
random?: () => number;
|
|
16
19
|
}
|
|
17
|
-
export declare
|
|
20
|
+
export declare const DEFAULT_MAX_AGENT_RETRY_DELAY_MS = 60000;
|
|
21
|
+
/**
|
|
22
|
+
* Per-attempt backoff: `baseDelayMs * 2^(attempt-1)`, jittered by +/-10% through the
|
|
23
|
+
* injectable `random` source, then clamped to `maxAgentDelayMs` (60s by default). The
|
|
24
|
+
* jitter runs before the clamp so a capped delay stays exactly at the cap instead of
|
|
25
|
+
* scattering above it, and the safe-integer guard keeps a large `attempt` from producing
|
|
26
|
+
* `Infinity` before the clamp.
|
|
27
|
+
*/
|
|
28
|
+
export declare function retryDelayMs(policy: Pick<RetryPolicy, "baseDelayMs" | "maxAgentDelayMs" | "random">, attempt: number): number;
|
|
18
29
|
/** Optional callbacks emitted by {@link retryAssistantCall} around each retry. */
|
|
19
30
|
export interface RetryCallbacks {
|
|
20
31
|
/** Emitted before the backoff sleep of each retry attempt (1-indexed). */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AA8IpD
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/utils/retry.ts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AW,GAAG,SAAS,EAC/B,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,SAAS,CAAC,EAAE,cAAc,GACxB,OAAO,CAAC,CAAC,CAAC,CAiCZ;AAOD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,kBAAkB,CACvC,OAAO,EAAE,MAAM,OAAO,CAAC,gBAAgB,CAAC,EACxC,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,SAAS,CAAC,EAAE,cAAc,GACxB,OAAO,CAAC,gBAAgB,CAAC,CA6C3B;AAED;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAU5E;AAYD,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAKzE;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAErE;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,YAAY,EAAE,MAAM,GAAG,eAAe,GAAG,WAAW,GAAG,SAAS,CAKpG"}
|
package/dist/utils/retry.js
CHANGED
|
@@ -121,10 +121,20 @@ const RETRYABLE_PROVIDER_ERROR_PATTERN = buildProviderErrorPattern([
|
|
|
121
121
|
// Same-process retry recovers; hopping providers cannot release that lock.
|
|
122
122
|
"Lock file is already being held",
|
|
123
123
|
]);
|
|
124
|
-
export
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
export const DEFAULT_MAX_AGENT_RETRY_DELAY_MS = 60_000;
|
|
125
|
+
/**
|
|
126
|
+
* Per-attempt backoff: `baseDelayMs * 2^(attempt-1)`, jittered by +/-10% through the
|
|
127
|
+
* injectable `random` source, then clamped to `maxAgentDelayMs` (60s by default). The
|
|
128
|
+
* jitter runs before the clamp so a capped delay stays exactly at the cap instead of
|
|
129
|
+
* scattering above it, and the safe-integer guard keeps a large `attempt` from producing
|
|
130
|
+
* `Infinity` before the clamp.
|
|
131
|
+
*/
|
|
132
|
+
export function retryDelayMs(policy, attempt) {
|
|
133
|
+
const scheduledDelayMs = policy.baseDelayMs * 2 ** Math.max(0, attempt - 1);
|
|
134
|
+
const sample = Math.min(1, Math.max(0, (policy.random ?? Math.random)()));
|
|
135
|
+
const jitteredDelayMs = Math.round(scheduledDelayMs * (0.9 + sample * 0.2));
|
|
136
|
+
const safeDelay = Number.isSafeInteger(jitteredDelayMs) ? jitteredDelayMs : Number.MAX_SAFE_INTEGER;
|
|
137
|
+
return Math.min(safeDelay, policy.maxAgentDelayMs ?? DEFAULT_MAX_AGENT_RETRY_DELAY_MS);
|
|
128
138
|
}
|
|
129
139
|
class RetrySleepAbortError extends Error {
|
|
130
140
|
constructor() {
|
|
@@ -190,7 +200,7 @@ export async function retryTransientCall(produce, isRetryable, policy, signal, c
|
|
|
190
200
|
}
|
|
191
201
|
attempt++;
|
|
192
202
|
lastRetry = { attempt, errorMessage: errorMessageOf(error) };
|
|
193
|
-
const delayMs = retryDelayMs(policy
|
|
203
|
+
const delayMs = retryDelayMs(policy, attempt);
|
|
194
204
|
await callbacks?.onRetryScheduled?.(attempt, maxAttempts, delayMs, lastRetry.errorMessage);
|
|
195
205
|
try {
|
|
196
206
|
await sleep(delayMs, signal);
|
|
@@ -252,7 +262,7 @@ export async function retryAssistantCall(produce, policy, signal, callbacks) {
|
|
|
252
262
|
}
|
|
253
263
|
attempt++;
|
|
254
264
|
lastRetry = { attempt, errorMessage: response.errorMessage || "Unknown error" };
|
|
255
|
-
const delayMs = retryDelayMs(policy
|
|
265
|
+
const delayMs = retryDelayMs(policy, attempt);
|
|
256
266
|
await callbacks?.onRetryScheduled?.(attempt, maxAttempts, delayMs, lastRetry.errorMessage);
|
|
257
267
|
// Normalize aborts during retry backoff to the same AssistantMessage shape as
|
|
258
268
|
// provider stream aborts, so callers do not need to care when cancellation happened.
|
|
@@ -262,7 +272,8 @@ export async function retryAssistantCall(produce, policy, signal, callbacks) {
|
|
|
262
272
|
catch (error) {
|
|
263
273
|
await callbacks?.onRetryFinished?.(false, attempt, lastRetry.errorMessage);
|
|
264
274
|
if (error instanceof RetrySleepAbortError) {
|
|
265
|
-
|
|
275
|
+
const { errorMessage: _errorMessage, ...rest } = response;
|
|
276
|
+
return { ...rest, stopReason: "aborted" };
|
|
266
277
|
}
|
|
267
278
|
throw error;
|
|
268
279
|
}
|