@livekit/agents 1.0.33 → 1.0.35
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/dist/cli.cjs.map +1 -1
- package/dist/inference/api_protos.d.cts +4 -4
- package/dist/inference/api_protos.d.ts +4 -4
- package/dist/inference/llm.cjs +30 -5
- package/dist/inference/llm.cjs.map +1 -1
- package/dist/inference/llm.d.cts +3 -1
- package/dist/inference/llm.d.ts +3 -1
- package/dist/inference/llm.d.ts.map +1 -1
- package/dist/inference/llm.js +30 -5
- package/dist/inference/llm.js.map +1 -1
- package/dist/ipc/inference_proc_executor.cjs.map +1 -1
- package/dist/ipc/job_proc_executor.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.js +1 -1
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/llm/chat_context.cjs +20 -2
- package/dist/llm/chat_context.cjs.map +1 -1
- package/dist/llm/chat_context.d.cts +9 -0
- package/dist/llm/chat_context.d.ts +9 -0
- package/dist/llm/chat_context.d.ts.map +1 -1
- package/dist/llm/chat_context.js +20 -2
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/fallback_adapter.cjs +278 -0
- package/dist/llm/fallback_adapter.cjs.map +1 -0
- package/dist/llm/fallback_adapter.d.cts +73 -0
- package/dist/llm/fallback_adapter.d.ts +73 -0
- package/dist/llm/fallback_adapter.d.ts.map +1 -0
- package/dist/llm/fallback_adapter.js +254 -0
- package/dist/llm/fallback_adapter.js.map +1 -0
- package/dist/llm/fallback_adapter.test.cjs +176 -0
- package/dist/llm/fallback_adapter.test.cjs.map +1 -0
- package/dist/llm/fallback_adapter.test.js +175 -0
- package/dist/llm/fallback_adapter.test.js.map +1 -0
- package/dist/llm/index.cjs +3 -0
- package/dist/llm/index.cjs.map +1 -1
- package/dist/llm/index.d.cts +1 -0
- package/dist/llm/index.d.ts +1 -0
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +4 -0
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/llm.cjs +1 -1
- package/dist/llm/llm.cjs.map +1 -1
- package/dist/llm/llm.d.cts +1 -0
- package/dist/llm/llm.d.ts +1 -0
- package/dist/llm/llm.d.ts.map +1 -1
- package/dist/llm/llm.js +1 -1
- package/dist/llm/llm.js.map +1 -1
- package/dist/llm/provider_format/openai.cjs +43 -20
- package/dist/llm/provider_format/openai.cjs.map +1 -1
- package/dist/llm/provider_format/openai.d.ts.map +1 -1
- package/dist/llm/provider_format/openai.js +43 -20
- package/dist/llm/provider_format/openai.js.map +1 -1
- package/dist/llm/provider_format/openai.test.cjs +35 -0
- package/dist/llm/provider_format/openai.test.cjs.map +1 -1
- package/dist/llm/provider_format/openai.test.js +35 -0
- package/dist/llm/provider_format/openai.test.js.map +1 -1
- package/dist/llm/provider_format/utils.cjs +1 -1
- package/dist/llm/provider_format/utils.cjs.map +1 -1
- package/dist/llm/provider_format/utils.d.ts.map +1 -1
- package/dist/llm/provider_format/utils.js +1 -1
- package/dist/llm/provider_format/utils.js.map +1 -1
- package/dist/stt/stt.cjs +1 -1
- package/dist/stt/stt.cjs.map +1 -1
- package/dist/stt/stt.js +1 -1
- package/dist/stt/stt.js.map +1 -1
- package/dist/tts/tts.cjs +2 -2
- package/dist/tts/tts.cjs.map +1 -1
- package/dist/tts/tts.js +2 -2
- package/dist/tts/tts.js.map +1 -1
- package/dist/voice/background_audio.cjs.map +1 -1
- package/dist/voice/generation.cjs +2 -1
- package/dist/voice/generation.cjs.map +1 -1
- package/dist/voice/generation.d.ts.map +1 -1
- package/dist/voice/generation.js +2 -1
- package/dist/voice/generation.js.map +1 -1
- package/package.json +1 -1
- package/src/inference/llm.ts +42 -5
- package/src/ipc/job_proc_lazy_main.ts +1 -1
- package/src/llm/chat_context.ts +32 -2
- package/src/llm/fallback_adapter.test.ts +238 -0
- package/src/llm/fallback_adapter.ts +391 -0
- package/src/llm/index.ts +6 -0
- package/src/llm/llm.ts +2 -1
- package/src/llm/provider_format/openai.test.ts +40 -0
- package/src/llm/provider_format/openai.ts +46 -19
- package/src/llm/provider_format/utils.ts +5 -1
- package/src/stt/stt.ts +1 -1
- package/src/tts/tts.ts +2 -2
- package/src/voice/generation.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat_context.d.ts","sourceRoot":"","sources":["../../src/llm/chat_context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEhE,OAAO,EAAE,KAAK,cAAc,EAAa,MAAM,4BAA4B,CAAC;AAC5E,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE5E,MAAM,MAAM,QAAQ,GAAG,WAAW,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AACrE,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAC;IAEX,IAAI,EAAE,eAAe,CAAC;IAEtB;;OAEG;IACH,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC;IAE3B,eAAe,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IAEzC,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,MAAM,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;CAC1B;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,eAAe,CAAC;IAEtB,KAAK,EAAE,UAAU,EAAE,CAAC;IAEpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,YAAY,GAAG,MAAM,CAAC;AAE/D,wBAAgB,kBAAkB,CAAC,MAAM,EAAE;IACz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9B,mBAAmB,CAAC,EAAE,OAAO,CAAC;QAC9B,mBAAmB,CAAC,EAAE,OAAO,CAAC;QAC9B,OAAO,CAAC,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;KACvB,GACL,WAAW;IAwCd,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,WAAW;IA6BvC,MAAM,CACJ,OAAO,GAAE;QACP,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAC3B,mBAAmB,CAAC,EAAE,OAAO,CAAC;KAC1B,GAEL,UAAU;IAiDP,gBAAgB,CAAC,MAAM,EAAE,cAAc,EAAE,sBAAsB,GAAE,OAAc;IAIrF;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;;;;;;;;;OAUG;IACH,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAmDzC;;OAEG;IACH,OAAO,CAAC,cAAc;IAgDtB;;OAEG;IACH,IAAI,QAAQ,IAAI,OAAO,CAEtB;CACF;AAED,qBAAa,mBAAoB,SAAQ,WAAW;IAClD,MAAM,CAAC,QAAQ,CAAC,QAAQ,gFACuD;gBAEnE,KAAK,EAAE,QAAQ,EAAE;IAI7B,IAAI,KAAK,IAAI,QAAQ,EAAE,CAEtB;IAED,IAAI,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,EAI1B;IAED,IAAI,QAAQ,IAAI,OAAO,CAEtB;CACF"}
|
package/dist/llm/chat_context.js
CHANGED
|
@@ -105,6 +105,11 @@ class FunctionCall {
|
|
|
105
105
|
args;
|
|
106
106
|
name;
|
|
107
107
|
createdAt;
|
|
108
|
+
extra;
|
|
109
|
+
/**
|
|
110
|
+
* Optional grouping identifier for parallel tool calls.
|
|
111
|
+
*/
|
|
112
|
+
groupId;
|
|
108
113
|
/**
|
|
109
114
|
* Opaque signature for Gemini thinking mode.
|
|
110
115
|
* When using Gemini 3+ models with thinking enabled, this signature must be
|
|
@@ -118,6 +123,8 @@ class FunctionCall {
|
|
|
118
123
|
args,
|
|
119
124
|
id = shortuuid("item_"),
|
|
120
125
|
createdAt = Date.now(),
|
|
126
|
+
extra = {},
|
|
127
|
+
groupId,
|
|
121
128
|
thoughtSignature
|
|
122
129
|
} = params;
|
|
123
130
|
this.id = id;
|
|
@@ -125,7 +132,12 @@ class FunctionCall {
|
|
|
125
132
|
this.args = args;
|
|
126
133
|
this.name = name;
|
|
127
134
|
this.createdAt = createdAt;
|
|
128
|
-
this.
|
|
135
|
+
this.extra = { ...extra };
|
|
136
|
+
this.groupId = groupId;
|
|
137
|
+
this.thoughtSignature = thoughtSignature ?? (typeof this.extra.google === "object" && this.extra.google !== null ? (
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
139
|
+
this.extra.google.thoughtSignature || this.extra.google.thought_signature
|
|
140
|
+
) : void 0);
|
|
129
141
|
}
|
|
130
142
|
static create(params) {
|
|
131
143
|
return new FunctionCall(params);
|
|
@@ -139,6 +151,12 @@ class FunctionCall {
|
|
|
139
151
|
name: this.name,
|
|
140
152
|
args: this.args
|
|
141
153
|
};
|
|
154
|
+
if (Object.keys(this.extra).length > 0) {
|
|
155
|
+
result.extra = this.extra;
|
|
156
|
+
}
|
|
157
|
+
if (this.groupId) {
|
|
158
|
+
result.groupId = this.groupId;
|
|
159
|
+
}
|
|
142
160
|
if (this.thoughtSignature) {
|
|
143
161
|
result.thoughtSignature = this.thoughtSignature;
|
|
144
162
|
}
|
|
@@ -392,7 +410,7 @@ class ChatContext {
|
|
|
392
410
|
return false;
|
|
393
411
|
}
|
|
394
412
|
} else if (a.type === "function_call" && b.type === "function_call") {
|
|
395
|
-
if (a.name !== b.name || a.callId !== b.callId || a.args !== b.args || a.thoughtSignature !== b.thoughtSignature) {
|
|
413
|
+
if (a.name !== b.name || a.callId !== b.callId || a.args !== b.args || a.thoughtSignature !== b.thoughtSignature || a.groupId !== b.groupId || JSON.stringify(a.extra) !== JSON.stringify(b.extra)) {
|
|
396
414
|
return false;
|
|
397
415
|
}
|
|
398
416
|
} else if (a.type === "function_call_output" && b.type === "function_call_output") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/llm/chat_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame, VideoFrame } from '@livekit/rtc-node';\nimport { createImmutableArray, shortuuid } from '../utils.js';\nimport { type ProviderFormat, toChatCtx } from './provider_format/index.js';\nimport type { JSONObject, JSONValue, ToolContext } from './tool_context.js';\n\nexport type ChatRole = 'developer' | 'system' | 'user' | 'assistant';\nexport interface ImageContent {\n id: string;\n\n type: 'image_content';\n\n /**\n * Either a string URL or a VideoFrame object.\n */\n image: string | VideoFrame;\n\n inferenceDetail: 'auto' | 'high' | 'low';\n\n inferenceWidth?: number;\n\n inferenceHeight?: number;\n\n mimeType?: string;\n\n _cache: Record<any, any>; // eslint-disable-line @typescript-eslint/no-explicit-any\n}\n\nexport interface AudioContent {\n type: 'audio_content';\n\n frame: AudioFrame[];\n\n transcript?: string;\n}\n\nexport type ChatContent = ImageContent | AudioContent | string;\n\nexport function createImageContent(params: {\n image: string | VideoFrame;\n id?: string;\n inferenceDetail?: 'auto' | 'high' | 'low';\n inferenceWidth?: number;\n inferenceHeight?: number;\n mimeType?: string;\n}): ImageContent {\n const {\n image,\n id = shortuuid('img_'),\n inferenceDetail = 'auto',\n inferenceWidth,\n inferenceHeight,\n mimeType,\n } = params;\n\n return {\n id,\n type: 'image_content',\n image,\n inferenceDetail,\n inferenceWidth,\n inferenceHeight,\n mimeType,\n _cache: {},\n };\n}\n\nexport function createAudioContent(params: {\n frame: AudioFrame[];\n transcript?: string;\n}): AudioContent {\n const { frame, transcript } = params;\n\n return {\n type: 'audio_content',\n frame,\n transcript,\n };\n}\n\nexport class ChatMessage {\n readonly id: string;\n\n readonly type = 'message' as const;\n\n readonly role: ChatRole;\n\n content: ChatContent[];\n\n interrupted: boolean;\n\n hash?: Uint8Array;\n\n createdAt: number;\n\n constructor(params: {\n role: ChatRole;\n content: ChatContent[] | string;\n id?: string;\n interrupted?: boolean;\n createdAt?: number;\n }) {\n const {\n role,\n content,\n id = shortuuid('item_'),\n interrupted = false,\n createdAt = Date.now(),\n } = params;\n this.id = id;\n this.role = role;\n this.content = Array.isArray(content) ? content : [content];\n this.interrupted = interrupted;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n role: ChatRole;\n content: ChatContent[] | string;\n id?: string;\n interrupted?: boolean;\n createdAt?: number;\n }) {\n return new ChatMessage(params);\n }\n\n /**\n * Returns a single string with all text parts of the message joined by new\n * lines. If no string content is present, returns `null`.\n */\n get textContent(): string | undefined {\n const parts = this.content.filter((c): c is string => typeof c === 'string');\n return parts.length > 0 ? parts.join('\\n') : undefined;\n }\n\n toJSONContent(): JSONValue[] {\n return this.content.map((c) => {\n if (typeof c === 'string') {\n return c as JSONValue;\n } else if (c.type === 'image_content') {\n return {\n id: c.id,\n type: c.type,\n image: c.image,\n inferenceDetail: c.inferenceDetail,\n inferenceWidth: c.inferenceWidth,\n inferenceHeight: c.inferenceHeight,\n mimeType: c.mimeType,\n } as JSONObject;\n } else {\n return {\n type: c.type,\n transcript: c.transcript,\n } as JSONObject;\n }\n });\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n role: this.role,\n content: this.toJSONContent(),\n interrupted: this.interrupted,\n };\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport class FunctionCall {\n readonly id: string;\n\n readonly type = 'function_call' as const;\n\n callId: string;\n\n args: string;\n\n name: string;\n\n createdAt: number;\n\n /**\n * Opaque signature for Gemini thinking mode.\n * When using Gemini 3+ models with thinking enabled, this signature must be\n * preserved and returned with function responses to maintain thought context.\n */\n thoughtSignature?: string;\n\n constructor(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\n thoughtSignature?: string;\n }) {\n const {\n callId,\n name,\n args,\n id = shortuuid('item_'),\n createdAt = Date.now(),\n thoughtSignature,\n } = params;\n this.id = id;\n this.callId = callId;\n this.args = args;\n this.name = name;\n this.createdAt = createdAt;\n this.thoughtSignature = thoughtSignature;\n }\n\n static create(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\n thoughtSignature?: string;\n }) {\n return new FunctionCall(params);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n callId: this.callId,\n name: this.name,\n args: this.args,\n };\n\n if (this.thoughtSignature) {\n result.thoughtSignature = this.thoughtSignature;\n }\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport class FunctionCallOutput {\n readonly id: string;\n\n readonly type = 'function_call_output' as const;\n\n name = '';\n\n callId: string;\n\n output: string;\n\n isError: boolean;\n\n createdAt: number;\n\n constructor(params: {\n callId: string;\n output: string;\n isError: boolean;\n id?: string;\n createdAt?: number;\n name?: string;\n }) {\n const {\n callId,\n output,\n isError,\n id = shortuuid('item_'),\n createdAt = Date.now(),\n name = '',\n } = params;\n this.id = id;\n this.callId = callId;\n this.output = output;\n this.isError = isError;\n this.name = name;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n callId: string;\n output: string;\n isError: boolean;\n id?: string;\n createdAt?: number;\n name?: string;\n }) {\n return new FunctionCallOutput(params);\n }\n\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n name: this.name,\n callId: this.callId,\n output: this.output,\n isError: this.isError,\n };\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport class AgentHandoffItem {\n readonly id: string;\n\n readonly type = 'agent_handoff' as const;\n\n oldAgentId: string | undefined;\n\n newAgentId: string;\n\n createdAt: number;\n\n constructor(params: {\n oldAgentId?: string;\n newAgentId: string;\n id?: string;\n createdAt?: number;\n }) {\n const { oldAgentId, newAgentId, id = shortuuid('item_'), createdAt = Date.now() } = params;\n this.id = id;\n this.oldAgentId = oldAgentId;\n this.newAgentId = newAgentId;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n oldAgentId?: string;\n newAgentId: string;\n id?: string;\n createdAt?: number;\n }) {\n return new AgentHandoffItem(params);\n }\n\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n newAgentId: this.newAgentId,\n };\n\n if (this.oldAgentId !== undefined) {\n result.oldAgentId = this.oldAgentId;\n }\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport type ChatItem = ChatMessage | FunctionCall | FunctionCallOutput | AgentHandoffItem;\n\nexport class ChatContext {\n protected _items: ChatItem[];\n\n constructor(items?: ChatItem[]) {\n this._items = items ? items : [];\n }\n\n static empty(): ChatContext {\n return new ChatContext([]);\n }\n\n get items(): ChatItem[] {\n return this._items;\n }\n\n set items(items: ChatItem[]) {\n this._items = items;\n }\n\n /**\n * Add a new message to the context and return it.\n */\n addMessage(params: {\n role: ChatRole;\n content: ChatContent[] | string;\n id?: string;\n interrupted?: boolean;\n createdAt?: number;\n }): ChatMessage {\n const msg = new ChatMessage(params);\n if (params.createdAt !== undefined) {\n const idx = this.findInsertionIndex(params.createdAt);\n this._items.splice(idx, 0, msg);\n } else {\n this._items.push(msg);\n }\n return msg;\n }\n\n /**\n * Insert a single item or multiple items based on their `createdAt` field so\n * that the array keeps its chronological order.\n */\n insert(item: ChatItem | ChatItem[]): void {\n const arr = Array.isArray(item) ? item : [item];\n for (const it of arr) {\n const idx = this.findInsertionIndex(it.createdAt);\n this._items.splice(idx, 0, it);\n }\n }\n\n getById(itemId: string): ChatItem | undefined {\n return this._items.find((i) => i.id === itemId);\n }\n\n indexById(itemId: string): number | undefined {\n const idx = this._items.findIndex((i) => i.id === itemId);\n return idx !== -1 ? idx : undefined;\n }\n\n copy(\n options: {\n excludeFunctionCall?: boolean;\n excludeInstructions?: boolean;\n excludeEmptyMessage?: boolean;\n toolCtx?: ToolContext<any>; // eslint-disable-line @typescript-eslint/no-explicit-any\n } = {},\n ): ChatContext {\n const {\n excludeFunctionCall = false,\n excludeInstructions = false,\n excludeEmptyMessage = false,\n toolCtx,\n } = options;\n const items: ChatItem[] = [];\n\n const isToolCallOrOutput = (item: ChatItem): item is FunctionCall | FunctionCallOutput =>\n ['function_call', 'function_call_output'].includes(item.type);\n const isChatMessage = (item: ChatItem): item is ChatMessage => item.type === 'message';\n\n for (const item of this._items) {\n if (excludeFunctionCall && isToolCallOrOutput(item)) {\n continue;\n }\n\n if (\n excludeInstructions &&\n isChatMessage(item) &&\n ['system', 'developer'].includes(item.role)\n ) {\n continue;\n }\n\n if (excludeEmptyMessage && isChatMessage(item) && item.content.length === 0) {\n continue;\n }\n\n if (toolCtx !== undefined && isToolCallOrOutput(item) && toolCtx[item.name] === undefined) {\n continue;\n }\n\n items.push(item);\n }\n\n return new ChatContext(items);\n }\n\n truncate(maxItems: number): ChatContext {\n if (maxItems <= 0) return this;\n\n const instructions = this._items.find((i) => i.type === 'message' && i.role === 'system') as\n | ChatMessage\n | undefined;\n\n let newItems = this._items.slice(-maxItems);\n\n // Ensure the first item is not a function-call artefact.\n while (\n newItems.length > 0 &&\n ['function_call', 'function_call_output'].includes(newItems[0]!.type)\n ) {\n newItems.shift();\n }\n\n if (instructions) {\n // At this point `instructions` is defined, so it is safe to pass to `includes`.\n if (!newItems.includes(instructions)) {\n newItems = [instructions, ...newItems];\n }\n }\n\n // replace the items in place to keep the reference\n this._items.splice(0, this._items.length, ...newItems);\n return this;\n }\n\n toJSON(\n options: {\n excludeImage?: boolean;\n excludeAudio?: boolean;\n excludeTimestamp?: boolean;\n excludeFunctionCall?: boolean;\n } = {},\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ): JSONObject {\n const {\n excludeImage = true,\n excludeAudio = true,\n excludeTimestamp = true,\n excludeFunctionCall = false,\n } = options;\n\n const items: ChatItem[] = [];\n\n for (const item of this._items) {\n let processedItem = item;\n\n if (excludeFunctionCall && ['function_call', 'function_call_output'].includes(item.type)) {\n continue;\n }\n\n if (item.type === 'message') {\n processedItem = ChatMessage.create({\n role: item.role,\n content: item.content,\n id: item.id,\n interrupted: item.interrupted,\n createdAt: item.createdAt,\n });\n\n // Filter content based on options\n if (excludeImage) {\n processedItem.content = processedItem.content.filter((c) => {\n return !(typeof c === 'object' && c.type === 'image_content');\n });\n }\n\n if (excludeAudio) {\n processedItem.content = processedItem.content.filter((c) => {\n return !(typeof c === 'object' && c.type === 'audio_content');\n });\n }\n }\n\n items.push(processedItem);\n }\n\n return {\n items: items.map((item) => item.toJSON(excludeTimestamp)),\n };\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n async toProviderFormat(format: ProviderFormat, injectDummyUserMessage: boolean = true) {\n return await toChatCtx(format, this, injectDummyUserMessage);\n }\n\n /**\n * Internal helper used by `truncate` & `addMessage` to find the correct\n * insertion index for a timestamp so the list remains sorted.\n */\n private findInsertionIndex(createdAt: number): number {\n for (let i = this._items.length - 1; i >= 0; i -= 1) {\n const item = this._items[i];\n if (item!.createdAt <= createdAt) {\n return i + 1;\n }\n }\n return 0;\n }\n\n /**\n * Return true if `other` has the same sequence of items with matching\n * essential fields (IDs, types, and payload) as this context.\n *\n * Comparison rules:\n * - Messages: compares the full `content` list, `role` and `interrupted`.\n * - Function calls: compares `name`, `callId`, and `args`.\n * - Function call outputs: compares `name`, `callId`, `output`, and `isError`.\n *\n * Does not consider timestamps or other metadata.\n */\n isEquivalent(other: ChatContext): boolean {\n if (this === other) {\n return true;\n }\n\n if (this.items.length !== other.items.length) {\n return false;\n }\n\n for (let i = 0; i < this.items.length; i++) {\n const a = this.items[i]!;\n const b = other.items[i]!;\n\n if (a.id !== b.id || a.type !== b.type) {\n return false;\n }\n\n if (a.type === 'message' && b.type === 'message') {\n if (\n a.role !== b.role ||\n a.interrupted !== b.interrupted ||\n !this.compareContent(a.content, b.content)\n ) {\n return false;\n }\n } else if (a.type === 'function_call' && b.type === 'function_call') {\n if (\n a.name !== b.name ||\n a.callId !== b.callId ||\n a.args !== b.args ||\n a.thoughtSignature !== b.thoughtSignature\n ) {\n return false;\n }\n } else if (a.type === 'function_call_output' && b.type === 'function_call_output') {\n if (\n a.name !== b.name ||\n a.callId !== b.callId ||\n a.output !== b.output ||\n a.isError !== b.isError\n ) {\n return false;\n }\n }\n }\n\n return true;\n }\n\n /**\n * Compare two content arrays for equality.\n */\n private compareContent(a: ChatContent[], b: ChatContent[]): boolean {\n if (a.length !== b.length) {\n return false;\n }\n\n for (let i = 0; i < a.length; i++) {\n const contentA = a[i]!;\n const contentB = b[i]!;\n\n if (typeof contentA === 'string' && typeof contentB === 'string') {\n if (contentA !== contentB) {\n return false;\n }\n continue;\n }\n\n if (typeof contentA !== typeof contentB) {\n return false;\n }\n\n if (typeof contentA === 'object' && typeof contentB === 'object') {\n if (contentA.type === 'image_content' && contentB.type === 'image_content') {\n if (\n contentA.id !== contentB.id ||\n contentA.image !== contentB.image ||\n contentA.inferenceDetail !== contentB.inferenceDetail ||\n contentA.inferenceWidth !== contentB.inferenceWidth ||\n contentA.inferenceHeight !== contentB.inferenceHeight ||\n contentA.mimeType !== contentB.mimeType\n ) {\n return false;\n }\n } else if (contentA.type === 'audio_content' && contentB.type === 'audio_content') {\n if (contentA.frame.length !== contentB.frame.length) {\n return false;\n }\n if (contentA.transcript !== contentB.transcript) {\n return false;\n }\n } else {\n return false;\n }\n }\n }\n\n return true;\n }\n\n /**\n * Indicates whether the context is read-only\n */\n get readonly(): boolean {\n return false;\n }\n}\n\nexport class ReadonlyChatContext extends ChatContext {\n static readonly errorMsg =\n 'Please use .copy() and agent.update_chat_ctx() to modify the chat context.';\n\n constructor(items: ChatItem[]) {\n super(createImmutableArray(items, ReadonlyChatContext.errorMsg));\n }\n\n get items(): ChatItem[] {\n return this._items;\n }\n\n set items(items: ChatItem[]) {\n throw new Error(\n `Cannot set items on a read-only chat context. ${ReadonlyChatContext.errorMsg}`,\n );\n }\n\n get readonly(): boolean {\n return true;\n }\n}\n"],"mappings":"AAIA,SAAS,sBAAsB,iBAAiB;AAChD,SAA8B,iBAAiB;AAmCxC,SAAS,mBAAmB,QAOlB;AACf,QAAM;AAAA,IACJ;AAAA,IACA,KAAK,UAAU,MAAM;AAAA,IACrB,kBAAkB;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,SAAO;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ,CAAC;AAAA,EACX;AACF;AAEO,SAAS,mBAAmB,QAGlB;AACf,QAAM,EAAE,OAAO,WAAW,IAAI;AAE9B,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACF;AACF;AAEO,MAAM,YAAY;AAAA,EACd;AAAA,EAEA,OAAO;AAAA,EAEP;AAAA,EAET;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA;AAAA,EAEA,YAAY,QAMT;AACD,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA,KAAK,UAAU,OAAO;AAAA,MACtB,cAAc;AAAA,MACd,YAAY,KAAK,IAAI;AAAA,IACvB,IAAI;AACJ,SAAK,KAAK;AACV,SAAK,OAAO;AACZ,SAAK,UAAU,MAAM,QAAQ,OAAO,IAAI,UAAU,CAAC,OAAO;AAC1D,SAAK,cAAc;AACnB,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,OAAO,OAAO,QAMX;AACD,WAAO,IAAI,YAAY,MAAM;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,cAAkC;AACpC,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,YAAY;AAC/C,mBAAO;AAAA,UACT;AAAA,QACF,OAAO;AACL,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO;AAAA,EACT;AACF;AAEO,MAAM,4BAA4B,YAAY;AAAA,EACnD,OAAgB,WACd;AAAA,EAEF,YAAY,OAAmB;AAC7B,UAAM,qBAAqB,OAAO,oBAAoB,QAAQ,CAAC;AAAA,EACjE;AAAA,EAEA,IAAI,QAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,MAAM,OAAmB;AAC3B,UAAM,IAAI;AAAA,MACR,iDAAiD,oBAAoB,QAAQ;AAAA,IAC/E;AAAA,EACF;AAAA,EAEA,IAAI,WAAoB;AACtB,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/llm/chat_context.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { AudioFrame, VideoFrame } from '@livekit/rtc-node';\nimport { createImmutableArray, shortuuid } from '../utils.js';\nimport { type ProviderFormat, toChatCtx } from './provider_format/index.js';\nimport type { JSONObject, JSONValue, ToolContext } from './tool_context.js';\n\nexport type ChatRole = 'developer' | 'system' | 'user' | 'assistant';\nexport interface ImageContent {\n id: string;\n\n type: 'image_content';\n\n /**\n * Either a string URL or a VideoFrame object.\n */\n image: string | VideoFrame;\n\n inferenceDetail: 'auto' | 'high' | 'low';\n\n inferenceWidth?: number;\n\n inferenceHeight?: number;\n\n mimeType?: string;\n\n _cache: Record<any, any>; // eslint-disable-line @typescript-eslint/no-explicit-any\n}\n\nexport interface AudioContent {\n type: 'audio_content';\n\n frame: AudioFrame[];\n\n transcript?: string;\n}\n\nexport type ChatContent = ImageContent | AudioContent | string;\n\nexport function createImageContent(params: {\n image: string | VideoFrame;\n id?: string;\n inferenceDetail?: 'auto' | 'high' | 'low';\n inferenceWidth?: number;\n inferenceHeight?: number;\n mimeType?: string;\n}): ImageContent {\n const {\n image,\n id = shortuuid('img_'),\n inferenceDetail = 'auto',\n inferenceWidth,\n inferenceHeight,\n mimeType,\n } = params;\n\n return {\n id,\n type: 'image_content',\n image,\n inferenceDetail,\n inferenceWidth,\n inferenceHeight,\n mimeType,\n _cache: {},\n };\n}\n\nexport function createAudioContent(params: {\n frame: AudioFrame[];\n transcript?: string;\n}): AudioContent {\n const { frame, transcript } = params;\n\n return {\n type: 'audio_content',\n frame,\n transcript,\n };\n}\n\nexport class ChatMessage {\n readonly id: string;\n\n readonly type = 'message' as const;\n\n readonly role: ChatRole;\n\n content: ChatContent[];\n\n interrupted: boolean;\n\n hash?: Uint8Array;\n\n createdAt: number;\n\n constructor(params: {\n role: ChatRole;\n content: ChatContent[] | string;\n id?: string;\n interrupted?: boolean;\n createdAt?: number;\n }) {\n const {\n role,\n content,\n id = shortuuid('item_'),\n interrupted = false,\n createdAt = Date.now(),\n } = params;\n this.id = id;\n this.role = role;\n this.content = Array.isArray(content) ? content : [content];\n this.interrupted = interrupted;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n role: ChatRole;\n content: ChatContent[] | string;\n id?: string;\n interrupted?: boolean;\n createdAt?: number;\n }) {\n return new ChatMessage(params);\n }\n\n /**\n * Returns a single string with all text parts of the message joined by new\n * lines. If no string content is present, returns `null`.\n */\n get textContent(): string | undefined {\n const parts = this.content.filter((c): c is string => typeof c === 'string');\n return parts.length > 0 ? parts.join('\\n') : undefined;\n }\n\n toJSONContent(): JSONValue[] {\n return this.content.map((c) => {\n if (typeof c === 'string') {\n return c as JSONValue;\n } else if (c.type === 'image_content') {\n return {\n id: c.id,\n type: c.type,\n image: c.image,\n inferenceDetail: c.inferenceDetail,\n inferenceWidth: c.inferenceWidth,\n inferenceHeight: c.inferenceHeight,\n mimeType: c.mimeType,\n } as JSONObject;\n } else {\n return {\n type: c.type,\n transcript: c.transcript,\n } as JSONObject;\n }\n });\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n role: this.role,\n content: this.toJSONContent(),\n interrupted: this.interrupted,\n };\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport class FunctionCall {\n readonly id: string;\n\n readonly type = 'function_call' as const;\n\n callId: string;\n\n args: string;\n\n name: string;\n\n createdAt: number;\n\n extra: Record<string, unknown>;\n /**\n * Optional grouping identifier for parallel tool calls.\n */\n groupId?: string;\n\n /**\n * Opaque signature for Gemini thinking mode.\n * When using Gemini 3+ models with thinking enabled, this signature must be\n * preserved and returned with function responses to maintain thought context.\n */\n thoughtSignature?: string;\n\n constructor(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\n extra?: Record<string, unknown>;\n groupId?: string;\n thoughtSignature?: string;\n }) {\n const {\n callId,\n name,\n args,\n id = shortuuid('item_'),\n createdAt = Date.now(),\n extra = {},\n groupId,\n thoughtSignature,\n } = params;\n this.id = id;\n this.callId = callId;\n this.args = args;\n this.name = name;\n this.createdAt = createdAt;\n this.extra = { ...extra };\n this.groupId = groupId;\n this.thoughtSignature =\n thoughtSignature ??\n (typeof this.extra.google === 'object' && this.extra.google !== null\n ? // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (this.extra.google as any).thoughtSignature ||\n (this.extra.google as any).thought_signature\n : undefined);\n }\n\n static create(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\n extra?: Record<string, unknown>;\n groupId?: string;\n thoughtSignature?: string;\n }) {\n return new FunctionCall(params);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n callId: this.callId,\n name: this.name,\n args: this.args,\n };\n\n if (Object.keys(this.extra).length > 0) {\n result.extra = this.extra as JSONValue;\n }\n\n if (this.groupId) {\n result.groupId = this.groupId;\n }\n\n if (this.thoughtSignature) {\n result.thoughtSignature = this.thoughtSignature;\n }\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport class FunctionCallOutput {\n readonly id: string;\n\n readonly type = 'function_call_output' as const;\n\n name = '';\n\n callId: string;\n\n output: string;\n\n isError: boolean;\n\n createdAt: number;\n\n constructor(params: {\n callId: string;\n output: string;\n isError: boolean;\n id?: string;\n createdAt?: number;\n name?: string;\n }) {\n const {\n callId,\n output,\n isError,\n id = shortuuid('item_'),\n createdAt = Date.now(),\n name = '',\n } = params;\n this.id = id;\n this.callId = callId;\n this.output = output;\n this.isError = isError;\n this.name = name;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n callId: string;\n output: string;\n isError: boolean;\n id?: string;\n createdAt?: number;\n name?: string;\n }) {\n return new FunctionCallOutput(params);\n }\n\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n name: this.name,\n callId: this.callId,\n output: this.output,\n isError: this.isError,\n };\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport class AgentHandoffItem {\n readonly id: string;\n\n readonly type = 'agent_handoff' as const;\n\n oldAgentId: string | undefined;\n\n newAgentId: string;\n\n createdAt: number;\n\n constructor(params: {\n oldAgentId?: string;\n newAgentId: string;\n id?: string;\n createdAt?: number;\n }) {\n const { oldAgentId, newAgentId, id = shortuuid('item_'), createdAt = Date.now() } = params;\n this.id = id;\n this.oldAgentId = oldAgentId;\n this.newAgentId = newAgentId;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n oldAgentId?: string;\n newAgentId: string;\n id?: string;\n createdAt?: number;\n }) {\n return new AgentHandoffItem(params);\n }\n\n toJSON(excludeTimestamp: boolean = false): JSONValue {\n const result: JSONValue = {\n id: this.id,\n type: this.type,\n newAgentId: this.newAgentId,\n };\n\n if (this.oldAgentId !== undefined) {\n result.oldAgentId = this.oldAgentId;\n }\n\n if (!excludeTimestamp) {\n result.createdAt = this.createdAt;\n }\n\n return result;\n }\n}\n\nexport type ChatItem = ChatMessage | FunctionCall | FunctionCallOutput | AgentHandoffItem;\n\nexport class ChatContext {\n protected _items: ChatItem[];\n\n constructor(items?: ChatItem[]) {\n this._items = items ? items : [];\n }\n\n static empty(): ChatContext {\n return new ChatContext([]);\n }\n\n get items(): ChatItem[] {\n return this._items;\n }\n\n set items(items: ChatItem[]) {\n this._items = items;\n }\n\n /**\n * Add a new message to the context and return it.\n */\n addMessage(params: {\n role: ChatRole;\n content: ChatContent[] | string;\n id?: string;\n interrupted?: boolean;\n createdAt?: number;\n }): ChatMessage {\n const msg = new ChatMessage(params);\n if (params.createdAt !== undefined) {\n const idx = this.findInsertionIndex(params.createdAt);\n this._items.splice(idx, 0, msg);\n } else {\n this._items.push(msg);\n }\n return msg;\n }\n\n /**\n * Insert a single item or multiple items based on their `createdAt` field so\n * that the array keeps its chronological order.\n */\n insert(item: ChatItem | ChatItem[]): void {\n const arr = Array.isArray(item) ? item : [item];\n for (const it of arr) {\n const idx = this.findInsertionIndex(it.createdAt);\n this._items.splice(idx, 0, it);\n }\n }\n\n getById(itemId: string): ChatItem | undefined {\n return this._items.find((i) => i.id === itemId);\n }\n\n indexById(itemId: string): number | undefined {\n const idx = this._items.findIndex((i) => i.id === itemId);\n return idx !== -1 ? idx : undefined;\n }\n\n copy(\n options: {\n excludeFunctionCall?: boolean;\n excludeInstructions?: boolean;\n excludeEmptyMessage?: boolean;\n toolCtx?: ToolContext<any>; // eslint-disable-line @typescript-eslint/no-explicit-any\n } = {},\n ): ChatContext {\n const {\n excludeFunctionCall = false,\n excludeInstructions = false,\n excludeEmptyMessage = false,\n toolCtx,\n } = options;\n const items: ChatItem[] = [];\n\n const isToolCallOrOutput = (item: ChatItem): item is FunctionCall | FunctionCallOutput =>\n ['function_call', 'function_call_output'].includes(item.type);\n const isChatMessage = (item: ChatItem): item is ChatMessage => item.type === 'message';\n\n for (const item of this._items) {\n if (excludeFunctionCall && isToolCallOrOutput(item)) {\n continue;\n }\n\n if (\n excludeInstructions &&\n isChatMessage(item) &&\n ['system', 'developer'].includes(item.role)\n ) {\n continue;\n }\n\n if (excludeEmptyMessage && isChatMessage(item) && item.content.length === 0) {\n continue;\n }\n\n if (toolCtx !== undefined && isToolCallOrOutput(item) && toolCtx[item.name] === undefined) {\n continue;\n }\n\n items.push(item);\n }\n\n return new ChatContext(items);\n }\n\n truncate(maxItems: number): ChatContext {\n if (maxItems <= 0) return this;\n\n const instructions = this._items.find((i) => i.type === 'message' && i.role === 'system') as\n | ChatMessage\n | undefined;\n\n let newItems = this._items.slice(-maxItems);\n\n // Ensure the first item is not a function-call artefact.\n while (\n newItems.length > 0 &&\n ['function_call', 'function_call_output'].includes(newItems[0]!.type)\n ) {\n newItems.shift();\n }\n\n if (instructions) {\n // At this point `instructions` is defined, so it is safe to pass to `includes`.\n if (!newItems.includes(instructions)) {\n newItems = [instructions, ...newItems];\n }\n }\n\n // replace the items in place to keep the reference\n this._items.splice(0, this._items.length, ...newItems);\n return this;\n }\n\n toJSON(\n options: {\n excludeImage?: boolean;\n excludeAudio?: boolean;\n excludeTimestamp?: boolean;\n excludeFunctionCall?: boolean;\n } = {},\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ): JSONObject {\n const {\n excludeImage = true,\n excludeAudio = true,\n excludeTimestamp = true,\n excludeFunctionCall = false,\n } = options;\n\n const items: ChatItem[] = [];\n\n for (const item of this._items) {\n let processedItem = item;\n\n if (excludeFunctionCall && ['function_call', 'function_call_output'].includes(item.type)) {\n continue;\n }\n\n if (item.type === 'message') {\n processedItem = ChatMessage.create({\n role: item.role,\n content: item.content,\n id: item.id,\n interrupted: item.interrupted,\n createdAt: item.createdAt,\n });\n\n // Filter content based on options\n if (excludeImage) {\n processedItem.content = processedItem.content.filter((c) => {\n return !(typeof c === 'object' && c.type === 'image_content');\n });\n }\n\n if (excludeAudio) {\n processedItem.content = processedItem.content.filter((c) => {\n return !(typeof c === 'object' && c.type === 'audio_content');\n });\n }\n }\n\n items.push(processedItem);\n }\n\n return {\n items: items.map((item) => item.toJSON(excludeTimestamp)),\n };\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n async toProviderFormat(format: ProviderFormat, injectDummyUserMessage: boolean = true) {\n return await toChatCtx(format, this, injectDummyUserMessage);\n }\n\n /**\n * Internal helper used by `truncate` & `addMessage` to find the correct\n * insertion index for a timestamp so the list remains sorted.\n */\n private findInsertionIndex(createdAt: number): number {\n for (let i = this._items.length - 1; i >= 0; i -= 1) {\n const item = this._items[i];\n if (item!.createdAt <= createdAt) {\n return i + 1;\n }\n }\n return 0;\n }\n\n /**\n * Return true if `other` has the same sequence of items with matching\n * essential fields (IDs, types, and payload) as this context.\n *\n * Comparison rules:\n * - Messages: compares the full `content` list, `role` and `interrupted`.\n * - Function calls: compares `name`, `callId`, and `args`.\n * - Function call outputs: compares `name`, `callId`, `output`, and `isError`.\n *\n * Does not consider timestamps or other metadata.\n */\n isEquivalent(other: ChatContext): boolean {\n if (this === other) {\n return true;\n }\n\n if (this.items.length !== other.items.length) {\n return false;\n }\n\n for (let i = 0; i < this.items.length; i++) {\n const a = this.items[i]!;\n const b = other.items[i]!;\n\n if (a.id !== b.id || a.type !== b.type) {\n return false;\n }\n\n if (a.type === 'message' && b.type === 'message') {\n if (\n a.role !== b.role ||\n a.interrupted !== b.interrupted ||\n !this.compareContent(a.content, b.content)\n ) {\n return false;\n }\n } else if (a.type === 'function_call' && b.type === 'function_call') {\n if (\n a.name !== b.name ||\n a.callId !== b.callId ||\n a.args !== b.args ||\n a.thoughtSignature !== b.thoughtSignature ||\n a.groupId !== b.groupId ||\n JSON.stringify(a.extra) !== JSON.stringify(b.extra)\n ) {\n return false;\n }\n } else if (a.type === 'function_call_output' && b.type === 'function_call_output') {\n if (\n a.name !== b.name ||\n a.callId !== b.callId ||\n a.output !== b.output ||\n a.isError !== b.isError\n ) {\n return false;\n }\n }\n }\n\n return true;\n }\n\n /**\n * Compare two content arrays for equality.\n */\n private compareContent(a: ChatContent[], b: ChatContent[]): boolean {\n if (a.length !== b.length) {\n return false;\n }\n\n for (let i = 0; i < a.length; i++) {\n const contentA = a[i]!;\n const contentB = b[i]!;\n\n if (typeof contentA === 'string' && typeof contentB === 'string') {\n if (contentA !== contentB) {\n return false;\n }\n continue;\n }\n\n if (typeof contentA !== typeof contentB) {\n return false;\n }\n\n if (typeof contentA === 'object' && typeof contentB === 'object') {\n if (contentA.type === 'image_content' && contentB.type === 'image_content') {\n if (\n contentA.id !== contentB.id ||\n contentA.image !== contentB.image ||\n contentA.inferenceDetail !== contentB.inferenceDetail ||\n contentA.inferenceWidth !== contentB.inferenceWidth ||\n contentA.inferenceHeight !== contentB.inferenceHeight ||\n contentA.mimeType !== contentB.mimeType\n ) {\n return false;\n }\n } else if (contentA.type === 'audio_content' && contentB.type === 'audio_content') {\n if (contentA.frame.length !== contentB.frame.length) {\n return false;\n }\n if (contentA.transcript !== contentB.transcript) {\n return false;\n }\n } else {\n return false;\n }\n }\n }\n\n return true;\n }\n\n /**\n * Indicates whether the context is read-only\n */\n get readonly(): boolean {\n return false;\n }\n}\n\nexport class ReadonlyChatContext extends ChatContext {\n static readonly errorMsg =\n 'Please use .copy() and agent.update_chat_ctx() to modify the chat context.';\n\n constructor(items: ChatItem[]) {\n super(createImmutableArray(items, ReadonlyChatContext.errorMsg));\n }\n\n get items(): ChatItem[] {\n return this._items;\n }\n\n set items(items: ChatItem[]) {\n throw new Error(\n `Cannot set items on a read-only chat context. ${ReadonlyChatContext.errorMsg}`,\n );\n }\n\n get readonly(): boolean {\n return true;\n }\n}\n"],"mappings":"AAIA,SAAS,sBAAsB,iBAAiB;AAChD,SAA8B,iBAAiB;AAmCxC,SAAS,mBAAmB,QAOlB;AACf,QAAM;AAAA,IACJ;AAAA,IACA,KAAK,UAAU,MAAM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AmB,SAAS,SAAS,iBAAiB;AACjF,cAAI,SAAS,MAAM,WAAW,SAAS,MAAM,QAAQ;AACnD,mBAAO;AAAA,UACT;AACA,cAAI,SAAS,eAAe,SAAS,YAAY;AAC/C,mBAAO;AAAA,UACT;AAAA,QACF,OAAO;AACL,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO;AAAA,EACT;AACF;AAEO,MAAM,4BAA4B,YAAY;AAAA,EACnD,OAAgB,WACd;AAAA,EAEF,YAAY,OAAmB;AAC7B,UAAM,qBAAqB,OAAO,oBAAoB,QAAQ,CAAC;AAAA,EACjE;AAAA,EAEA,IAAI,QAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,MAAM,OAAmB;AAC3B,UAAM,IAAI;AAAA,MACR,iDAAiD,oBAAoB,QAAQ;AAAA,IAC/E;AAAA,EACF;AAAA,EAEA,IAAI,WAAoB;AACtB,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var fallback_adapter_exports = {};
|
|
20
|
+
__export(fallback_adapter_exports, {
|
|
21
|
+
FallbackAdapter: () => FallbackAdapter
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(fallback_adapter_exports);
|
|
24
|
+
var import_exceptions = require("../_exceptions.cjs");
|
|
25
|
+
var import_log = require("../log.cjs");
|
|
26
|
+
var import_types = require("../types.cjs");
|
|
27
|
+
var import_llm = require("./llm.cjs");
|
|
28
|
+
const DEFAULT_FALLBACK_API_CONNECT_OPTIONS = {
|
|
29
|
+
maxRetry: 0,
|
|
30
|
+
timeoutMs: import_types.DEFAULT_API_CONNECT_OPTIONS.timeoutMs,
|
|
31
|
+
retryIntervalMs: import_types.DEFAULT_API_CONNECT_OPTIONS.retryIntervalMs
|
|
32
|
+
};
|
|
33
|
+
class FallbackAdapter extends import_llm.LLM {
|
|
34
|
+
llms;
|
|
35
|
+
attemptTimeout;
|
|
36
|
+
maxRetryPerLLM;
|
|
37
|
+
retryInterval;
|
|
38
|
+
retryOnChunkSent;
|
|
39
|
+
/** @internal */
|
|
40
|
+
_status;
|
|
41
|
+
logger = (0, import_log.log)();
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super();
|
|
44
|
+
if (!options.llms || options.llms.length < 1) {
|
|
45
|
+
throw new Error("at least one LLM instance must be provided.");
|
|
46
|
+
}
|
|
47
|
+
this.llms = options.llms;
|
|
48
|
+
this.attemptTimeout = options.attemptTimeout ?? 5;
|
|
49
|
+
this.maxRetryPerLLM = options.maxRetryPerLLM ?? 0;
|
|
50
|
+
this.retryInterval = options.retryInterval ?? 0.5;
|
|
51
|
+
this.retryOnChunkSent = options.retryOnChunkSent ?? false;
|
|
52
|
+
this._status = this.llms.map(() => ({
|
|
53
|
+
available: true,
|
|
54
|
+
recoveringTask: null
|
|
55
|
+
}));
|
|
56
|
+
for (const llm of this.llms) {
|
|
57
|
+
llm.on("metrics_collected", (metrics) => {
|
|
58
|
+
this.emit("metrics_collected", metrics);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
get model() {
|
|
63
|
+
return "FallbackAdapter";
|
|
64
|
+
}
|
|
65
|
+
label() {
|
|
66
|
+
return "FallbackAdapter";
|
|
67
|
+
}
|
|
68
|
+
chat(opts) {
|
|
69
|
+
return new FallbackLLMStream(this, {
|
|
70
|
+
chatCtx: opts.chatCtx,
|
|
71
|
+
toolCtx: opts.toolCtx,
|
|
72
|
+
connOptions: opts.connOptions || DEFAULT_FALLBACK_API_CONNECT_OPTIONS,
|
|
73
|
+
parallelToolCalls: opts.parallelToolCalls,
|
|
74
|
+
toolChoice: opts.toolChoice,
|
|
75
|
+
extraKwargs: opts.extraKwargs
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Emit availability changed event.
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
_emitAvailabilityChanged(llm, available) {
|
|
83
|
+
const event = { llm, available };
|
|
84
|
+
this.emit(
|
|
85
|
+
"llm_availability_changed",
|
|
86
|
+
event
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
class FallbackLLMStream extends import_llm.LLMStream {
|
|
91
|
+
adapter;
|
|
92
|
+
parallelToolCalls;
|
|
93
|
+
toolChoice;
|
|
94
|
+
extraKwargs;
|
|
95
|
+
_currentStream;
|
|
96
|
+
_log = (0, import_log.log)();
|
|
97
|
+
constructor(adapter, opts) {
|
|
98
|
+
super(adapter, {
|
|
99
|
+
chatCtx: opts.chatCtx,
|
|
100
|
+
toolCtx: opts.toolCtx,
|
|
101
|
+
connOptions: opts.connOptions
|
|
102
|
+
});
|
|
103
|
+
this.adapter = adapter;
|
|
104
|
+
this.parallelToolCalls = opts.parallelToolCalls;
|
|
105
|
+
this.toolChoice = opts.toolChoice;
|
|
106
|
+
this.extraKwargs = opts.extraKwargs;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Override chatCtx to return current stream's context if available.
|
|
110
|
+
*/
|
|
111
|
+
get chatCtx() {
|
|
112
|
+
var _a;
|
|
113
|
+
return ((_a = this._currentStream) == null ? void 0 : _a.chatCtx) ?? super.chatCtx;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Try to generate with a single LLM.
|
|
117
|
+
* Returns an async generator that yields chunks.
|
|
118
|
+
*/
|
|
119
|
+
async *tryGenerate(llm, checkRecovery = false) {
|
|
120
|
+
const connOptions = {
|
|
121
|
+
...this.connOptions,
|
|
122
|
+
maxRetry: this.adapter.maxRetryPerLLM,
|
|
123
|
+
timeoutMs: this.adapter.attemptTimeout * 1e3,
|
|
124
|
+
retryIntervalMs: this.adapter.retryInterval * 1e3
|
|
125
|
+
};
|
|
126
|
+
const stream = llm.chat({
|
|
127
|
+
chatCtx: super.chatCtx,
|
|
128
|
+
toolCtx: this.toolCtx,
|
|
129
|
+
connOptions,
|
|
130
|
+
parallelToolCalls: this.parallelToolCalls,
|
|
131
|
+
toolChoice: this.toolChoice,
|
|
132
|
+
extraKwargs: this.extraKwargs
|
|
133
|
+
});
|
|
134
|
+
let streamError;
|
|
135
|
+
const errorHandler = (ev) => {
|
|
136
|
+
streamError = ev.error;
|
|
137
|
+
};
|
|
138
|
+
llm.on("error", errorHandler);
|
|
139
|
+
try {
|
|
140
|
+
let shouldSetCurrent = !checkRecovery;
|
|
141
|
+
for await (const chunk of stream) {
|
|
142
|
+
if (shouldSetCurrent) {
|
|
143
|
+
shouldSetCurrent = false;
|
|
144
|
+
this._currentStream = stream;
|
|
145
|
+
}
|
|
146
|
+
yield chunk;
|
|
147
|
+
}
|
|
148
|
+
if (streamError) {
|
|
149
|
+
throw streamError;
|
|
150
|
+
}
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (error instanceof import_exceptions.APIError) {
|
|
153
|
+
if (checkRecovery) {
|
|
154
|
+
this._log.warn({ llm: llm.label(), error }, "recovery failed");
|
|
155
|
+
} else {
|
|
156
|
+
this._log.warn({ llm: llm.label(), error }, "failed, switching to next LLM");
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
161
|
+
if (checkRecovery) {
|
|
162
|
+
this._log.warn({ llm: llm.label() }, "recovery timed out");
|
|
163
|
+
} else {
|
|
164
|
+
this._log.warn({ llm: llm.label() }, "timed out, switching to next LLM");
|
|
165
|
+
}
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
if (checkRecovery) {
|
|
169
|
+
this._log.error({ llm: llm.label(), error }, "recovery unexpected error");
|
|
170
|
+
} else {
|
|
171
|
+
this._log.error({ llm: llm.label(), error }, "unexpected error, switching to next LLM");
|
|
172
|
+
}
|
|
173
|
+
throw error;
|
|
174
|
+
} finally {
|
|
175
|
+
llm.off("error", errorHandler);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Start background recovery task for an LLM.
|
|
180
|
+
*/
|
|
181
|
+
tryRecovery(llm, index) {
|
|
182
|
+
const status = this.adapter._status[index];
|
|
183
|
+
if (status.recoveringTask !== null) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const recoverTask = async () => {
|
|
187
|
+
try {
|
|
188
|
+
for await (const _chunk of this.tryGenerate(llm, true)) {
|
|
189
|
+
}
|
|
190
|
+
status.available = true;
|
|
191
|
+
this._log.info({ llm: llm.label() }, "LLM recovered");
|
|
192
|
+
this.adapter._emitAvailabilityChanged(llm, true);
|
|
193
|
+
} catch {
|
|
194
|
+
} finally {
|
|
195
|
+
status.recoveringTask = null;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
status.recoveringTask = recoverTask();
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Main run method - iterates through LLMs with fallback logic.
|
|
202
|
+
*/
|
|
203
|
+
async run() {
|
|
204
|
+
const startTime = Date.now();
|
|
205
|
+
const allFailed = this.adapter._status.every((s) => !s.available);
|
|
206
|
+
if (allFailed) {
|
|
207
|
+
this._log.error("all LLMs are unavailable, retrying...");
|
|
208
|
+
}
|
|
209
|
+
for (let i = 0; i < this.adapter.llms.length; i++) {
|
|
210
|
+
const llm = this.adapter.llms[i];
|
|
211
|
+
const status = this.adapter._status[i];
|
|
212
|
+
this._log.debug(
|
|
213
|
+
{ llm: llm.label(), index: i, available: status.available, allFailed },
|
|
214
|
+
"checking LLM"
|
|
215
|
+
);
|
|
216
|
+
if (status.available || allFailed) {
|
|
217
|
+
let textSent = "";
|
|
218
|
+
const toolCallsSent = [];
|
|
219
|
+
try {
|
|
220
|
+
this._log.info({ llm: llm.label() }, "FallbackAdapter: Attempting provider");
|
|
221
|
+
let chunkCount = 0;
|
|
222
|
+
for await (const chunk of this.tryGenerate(llm, false)) {
|
|
223
|
+
chunkCount++;
|
|
224
|
+
if (chunk.delta) {
|
|
225
|
+
if (chunk.delta.content) {
|
|
226
|
+
textSent += chunk.delta.content;
|
|
227
|
+
}
|
|
228
|
+
if (chunk.delta.toolCalls) {
|
|
229
|
+
for (const tc of chunk.delta.toolCalls) {
|
|
230
|
+
if (tc.name) {
|
|
231
|
+
toolCallsSent.push(tc.name);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
this._log.debug({ llm: llm.label(), chunkCount }, "run: forwarding chunk to queue");
|
|
237
|
+
this.queue.put(chunk);
|
|
238
|
+
}
|
|
239
|
+
this._log.info(
|
|
240
|
+
{ llm: llm.label(), totalChunks: chunkCount, textLength: textSent.length },
|
|
241
|
+
"FallbackAdapter: Provider succeeded"
|
|
242
|
+
);
|
|
243
|
+
return;
|
|
244
|
+
} catch (error) {
|
|
245
|
+
if (status.available) {
|
|
246
|
+
status.available = false;
|
|
247
|
+
this.adapter._emitAvailabilityChanged(llm, false);
|
|
248
|
+
}
|
|
249
|
+
if (textSent || toolCallsSent.length > 0) {
|
|
250
|
+
const extra = { textSent, toolCallsSent };
|
|
251
|
+
if (!this.adapter.retryOnChunkSent) {
|
|
252
|
+
this._log.error(
|
|
253
|
+
{ llm: llm.label(), ...extra },
|
|
254
|
+
"failed after sending chunk, skip retrying. Set `retryOnChunkSent` to `true` to enable."
|
|
255
|
+
);
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
this._log.warn(
|
|
259
|
+
{ llm: llm.label(), ...extra },
|
|
260
|
+
"failed after sending chunk, retrying..."
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
this.tryRecovery(llm, i);
|
|
266
|
+
}
|
|
267
|
+
const duration = (Date.now() - startTime) / 1e3;
|
|
268
|
+
const labels = this.adapter.llms.map((l) => l.label()).join(", ");
|
|
269
|
+
throw new import_exceptions.APIConnectionError({
|
|
270
|
+
message: `all LLMs failed (${labels}) after ${duration.toFixed(2)}s`
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
275
|
+
0 && (module.exports = {
|
|
276
|
+
FallbackAdapter
|
|
277
|
+
});
|
|
278
|
+
//# sourceMappingURL=fallback_adapter.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/llm/fallback_adapter.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { APIConnectionError, APIError } from '../_exceptions.js';\nimport { log } from '../log.js';\nimport { type APIConnectOptions, DEFAULT_API_CONNECT_OPTIONS } from '../types.js';\nimport type { ChatContext } from './chat_context.js';\nimport type { ChatChunk } from './llm.js';\nimport { LLM, LLMStream } from './llm.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\n/**\n * Default connection options for FallbackAdapter.\n * Uses max_retry=0 since fallback handles retries at a higher level.\n */\nconst DEFAULT_FALLBACK_API_CONNECT_OPTIONS: APIConnectOptions = {\n maxRetry: 0,\n timeoutMs: DEFAULT_API_CONNECT_OPTIONS.timeoutMs,\n retryIntervalMs: DEFAULT_API_CONNECT_OPTIONS.retryIntervalMs,\n};\n\n/**\n * Internal status tracking for each LLM instance.\n */\ninterface LLMStatus {\n available: boolean;\n recoveringTask: Promise<void> | null;\n}\n\n/**\n * Event emitted when an LLM's availability changes.\n */\nexport interface AvailabilityChangedEvent {\n llm: LLM;\n available: boolean;\n}\n\n/**\n * Options for creating a FallbackAdapter.\n */\nexport interface FallbackAdapterOptions {\n /** List of LLM instances to fallback to (in order). */\n llms: LLM[];\n /** Timeout for each LLM attempt in seconds. Defaults to 5.0. */\n attemptTimeout?: number;\n /** Internal retries per LLM before moving to next. Defaults to 0. */\n maxRetryPerLLM?: number;\n /** Interval between retries in seconds. Defaults to 0.5. */\n retryInterval?: number;\n /** Whether to retry when LLM fails after chunks are sent. Defaults to false. */\n retryOnChunkSent?: boolean;\n}\n\n/**\n * FallbackAdapter is an LLM that can fallback to a different LLM if the current LLM fails.\n *\n * @example\n * ```typescript\n * const fallbackLLM = new FallbackAdapter({\n * llms: [primaryLLM, secondaryLLM, tertiaryLLM],\n * attemptTimeout: 5.0,\n * maxRetryPerLLM: 1,\n * });\n * ```\n */\nexport class FallbackAdapter extends LLM {\n readonly llms: LLM[];\n readonly attemptTimeout: number;\n readonly maxRetryPerLLM: number;\n readonly retryInterval: number;\n readonly retryOnChunkSent: boolean;\n\n /** @internal */\n _status: LLMStatus[];\n\n private logger = log();\n\n constructor(options: FallbackAdapterOptions) {\n super();\n\n if (!options.llms || options.llms.length < 1) {\n throw new Error('at least one LLM instance must be provided.');\n }\n\n this.llms = options.llms;\n this.attemptTimeout = options.attemptTimeout ?? 5.0;\n this.maxRetryPerLLM = options.maxRetryPerLLM ?? 0;\n this.retryInterval = options.retryInterval ?? 0.5;\n this.retryOnChunkSent = options.retryOnChunkSent ?? false;\n\n // Initialize status for each LLM\n this._status = this.llms.map(() => ({\n available: true,\n recoveringTask: null,\n }));\n\n // Forward metrics_collected events from child LLMs\n for (const llm of this.llms) {\n llm.on('metrics_collected', (metrics) => {\n this.emit('metrics_collected', metrics);\n });\n }\n }\n\n get model(): string {\n return 'FallbackAdapter';\n }\n\n label(): string {\n return 'FallbackAdapter';\n }\n\n chat(opts: {\n chatCtx: ChatContext;\n toolCtx?: ToolContext;\n connOptions?: APIConnectOptions;\n parallelToolCalls?: boolean;\n toolChoice?: ToolChoice;\n extraKwargs?: Record<string, unknown>;\n }): LLMStream {\n return new FallbackLLMStream(this, {\n chatCtx: opts.chatCtx,\n toolCtx: opts.toolCtx,\n connOptions: opts.connOptions || DEFAULT_FALLBACK_API_CONNECT_OPTIONS,\n parallelToolCalls: opts.parallelToolCalls,\n toolChoice: opts.toolChoice,\n extraKwargs: opts.extraKwargs,\n });\n }\n\n /**\n * Emit availability changed event.\n * @internal\n */\n _emitAvailabilityChanged(llm: LLM, available: boolean): void {\n const event: AvailabilityChangedEvent = { llm, available };\n // Use type assertion for custom event\n (this as unknown as { emit: (event: string, data: AvailabilityChangedEvent) => void }).emit(\n 'llm_availability_changed',\n event,\n );\n }\n}\n\n/**\n * LLMStream implementation for FallbackAdapter.\n * Handles fallback logic between multiple LLM providers.\n */\nclass FallbackLLMStream extends LLMStream {\n private adapter: FallbackAdapter;\n private parallelToolCalls?: boolean;\n private toolChoice?: ToolChoice;\n private extraKwargs?: Record<string, unknown>;\n private _currentStream?: LLMStream;\n private _log = log();\n\n constructor(\n adapter: FallbackAdapter,\n opts: {\n chatCtx: ChatContext;\n toolCtx?: ToolContext;\n connOptions: APIConnectOptions;\n parallelToolCalls?: boolean;\n toolChoice?: ToolChoice;\n extraKwargs?: Record<string, unknown>;\n },\n ) {\n super(adapter, {\n chatCtx: opts.chatCtx,\n toolCtx: opts.toolCtx,\n connOptions: opts.connOptions,\n });\n this.adapter = adapter;\n this.parallelToolCalls = opts.parallelToolCalls;\n this.toolChoice = opts.toolChoice;\n this.extraKwargs = opts.extraKwargs;\n }\n\n /**\n * Override chatCtx to return current stream's context if available.\n */\n override get chatCtx(): ChatContext {\n return this._currentStream?.chatCtx ?? super.chatCtx;\n }\n\n /**\n * Try to generate with a single LLM.\n * Returns an async generator that yields chunks.\n */\n private async *tryGenerate(\n llm: LLM,\n checkRecovery: boolean = false,\n ): AsyncGenerator<ChatChunk, void, unknown> {\n const connOptions: APIConnectOptions = {\n ...this.connOptions,\n maxRetry: this.adapter.maxRetryPerLLM,\n timeoutMs: this.adapter.attemptTimeout * 1000,\n retryIntervalMs: this.adapter.retryInterval * 1000,\n };\n\n const stream = llm.chat({\n chatCtx: super.chatCtx,\n toolCtx: this.toolCtx,\n connOptions,\n parallelToolCalls: this.parallelToolCalls,\n toolChoice: this.toolChoice,\n extraKwargs: this.extraKwargs,\n });\n\n // Listen for error events - child LLMs emit errors via their LLM instance, not the stream\n let streamError: Error | undefined;\n const errorHandler = (ev: { error: Error }) => {\n streamError = ev.error;\n };\n llm.on('error', errorHandler);\n\n try {\n let shouldSetCurrent = !checkRecovery;\n for await (const chunk of stream) {\n if (shouldSetCurrent) {\n shouldSetCurrent = false;\n this._currentStream = stream;\n }\n yield chunk;\n }\n\n // If an error was emitted but not thrown through iteration, throw it now\n if (streamError) {\n throw streamError;\n }\n } catch (error) {\n if (error instanceof APIError) {\n if (checkRecovery) {\n this._log.warn({ llm: llm.label(), error }, 'recovery failed');\n } else {\n this._log.warn({ llm: llm.label(), error }, 'failed, switching to next LLM');\n }\n throw error;\n }\n\n // Handle timeout errors\n if (error instanceof Error && error.name === 'AbortError') {\n if (checkRecovery) {\n this._log.warn({ llm: llm.label() }, 'recovery timed out');\n } else {\n this._log.warn({ llm: llm.label() }, 'timed out, switching to next LLM');\n }\n throw error;\n }\n\n // Unexpected error\n if (checkRecovery) {\n this._log.error({ llm: llm.label(), error }, 'recovery unexpected error');\n } else {\n this._log.error({ llm: llm.label(), error }, 'unexpected error, switching to next LLM');\n }\n throw error;\n } finally {\n llm.off('error', errorHandler);\n }\n }\n\n /**\n * Start background recovery task for an LLM.\n */\n private tryRecovery(llm: LLM, index: number): void {\n const status = this.adapter._status[index]!;\n\n // Skip if already recovering\n if (status.recoveringTask !== null) {\n return;\n }\n\n const recoverTask = async (): Promise<void> => {\n try {\n // Try to generate (just iterate to check if it works)\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n for await (const _chunk of this.tryGenerate(llm, true)) {\n // Just consume the stream to verify it works\n }\n\n // Recovery successful\n status.available = true;\n this._log.info({ llm: llm.label() }, 'LLM recovered');\n this.adapter._emitAvailabilityChanged(llm, true);\n } catch {\n // Recovery failed, stay unavailable\n } finally {\n status.recoveringTask = null;\n }\n };\n\n // Fire and forget\n status.recoveringTask = recoverTask();\n }\n\n /**\n * Main run method - iterates through LLMs with fallback logic.\n */\n protected async run(): Promise<void> {\n const startTime = Date.now();\n\n // Check if all LLMs are unavailable\n const allFailed = this.adapter._status.every((s) => !s.available);\n if (allFailed) {\n this._log.error('all LLMs are unavailable, retrying...');\n }\n\n for (let i = 0; i < this.adapter.llms.length; i++) {\n const llm = this.adapter.llms[i]!;\n const status = this.adapter._status[i]!;\n\n this._log.debug(\n { llm: llm.label(), index: i, available: status.available, allFailed },\n 'checking LLM',\n );\n\n if (status.available || allFailed) {\n let textSent = '';\n const toolCallsSent: string[] = [];\n\n try {\n this._log.info({ llm: llm.label() }, 'FallbackAdapter: Attempting provider');\n\n let chunkCount = 0;\n for await (const chunk of this.tryGenerate(llm, false)) {\n chunkCount++;\n // Track what's been sent\n if (chunk.delta) {\n if (chunk.delta.content) {\n textSent += chunk.delta.content;\n }\n if (chunk.delta.toolCalls) {\n for (const tc of chunk.delta.toolCalls) {\n if (tc.name) {\n toolCallsSent.push(tc.name);\n }\n }\n }\n }\n\n // Forward chunk to queue\n this._log.debug({ llm: llm.label(), chunkCount }, 'run: forwarding chunk to queue');\n this.queue.put(chunk);\n }\n\n // Success!\n this._log.info(\n { llm: llm.label(), totalChunks: chunkCount, textLength: textSent.length },\n 'FallbackAdapter: Provider succeeded',\n );\n return;\n } catch (error) {\n // Mark as unavailable if it was available before\n if (status.available) {\n status.available = false;\n this.adapter._emitAvailabilityChanged(llm, false);\n }\n\n // Check if we sent data before failing\n if (textSent || toolCallsSent.length > 0) {\n const extra = { textSent, toolCallsSent };\n\n if (!this.adapter.retryOnChunkSent) {\n this._log.error(\n { llm: llm.label(), ...extra },\n 'failed after sending chunk, skip retrying. Set `retryOnChunkSent` to `true` to enable.',\n );\n throw error;\n }\n\n this._log.warn(\n { llm: llm.label(), ...extra },\n 'failed after sending chunk, retrying...',\n );\n }\n }\n }\n\n // Trigger background recovery for this LLM\n this.tryRecovery(llm, i);\n }\n\n // All LLMs failed\n const duration = (Date.now() - startTime) / 1000;\n const labels = this.adapter.llms.map((l) => l.label()).join(', ');\n throw new APIConnectionError({\n message: `all LLMs failed (${labels}) after ${duration.toFixed(2)}s`,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAA6C;AAC7C,iBAAoB;AACpB,mBAAoE;AAGpE,iBAA+B;AAO/B,MAAM,uCAA0D;AAAA,EAC9D,UAAU;AAAA,EACV,WAAW,yCAA4B;AAAA,EACvC,iBAAiB,yCAA4B;AAC/C;AA8CO,MAAM,wBAAwB,eAAI;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGT;AAAA,EAEQ,aAAS,gBAAI;AAAA,EAErB,YAAY,SAAiC;AAC3C,UAAM;AAEN,QAAI,CAAC,QAAQ,QAAQ,QAAQ,KAAK,SAAS,GAAG;AAC5C,YAAM,IAAI,MAAM,6CAA6C;AAAA,IAC/D;AAEA,SAAK,OAAO,QAAQ;AACpB,SAAK,iBAAiB,QAAQ,kBAAkB;AAChD,SAAK,iBAAiB,QAAQ,kBAAkB;AAChD,SAAK,gBAAg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;AAAA,MACF;AAGA,WAAK,YAAY,KAAK,CAAC;AAAA,IACzB;AAGA,UAAM,YAAY,KAAK,IAAI,IAAI,aAAa;AAC5C,UAAM,SAAS,KAAK,QAAQ,KAAK,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,KAAK,IAAI;AAChE,UAAM,IAAI,qCAAmB;AAAA,MAC3B,SAAS,oBAAoB,MAAM,WAAW,SAAS,QAAQ,CAAC,CAAC;AAAA,IACnE,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type APIConnectOptions } from '../types.js';
|
|
2
|
+
import type { ChatContext } from './chat_context.js';
|
|
3
|
+
import { LLM, LLMStream } from './llm.js';
|
|
4
|
+
import type { ToolChoice, ToolContext } from './tool_context.js';
|
|
5
|
+
/**
|
|
6
|
+
* Internal status tracking for each LLM instance.
|
|
7
|
+
*/
|
|
8
|
+
interface LLMStatus {
|
|
9
|
+
available: boolean;
|
|
10
|
+
recoveringTask: Promise<void> | null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Event emitted when an LLM's availability changes.
|
|
14
|
+
*/
|
|
15
|
+
export interface AvailabilityChangedEvent {
|
|
16
|
+
llm: LLM;
|
|
17
|
+
available: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Options for creating a FallbackAdapter.
|
|
21
|
+
*/
|
|
22
|
+
export interface FallbackAdapterOptions {
|
|
23
|
+
/** List of LLM instances to fallback to (in order). */
|
|
24
|
+
llms: LLM[];
|
|
25
|
+
/** Timeout for each LLM attempt in seconds. Defaults to 5.0. */
|
|
26
|
+
attemptTimeout?: number;
|
|
27
|
+
/** Internal retries per LLM before moving to next. Defaults to 0. */
|
|
28
|
+
maxRetryPerLLM?: number;
|
|
29
|
+
/** Interval between retries in seconds. Defaults to 0.5. */
|
|
30
|
+
retryInterval?: number;
|
|
31
|
+
/** Whether to retry when LLM fails after chunks are sent. Defaults to false. */
|
|
32
|
+
retryOnChunkSent?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* FallbackAdapter is an LLM that can fallback to a different LLM if the current LLM fails.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const fallbackLLM = new FallbackAdapter({
|
|
40
|
+
* llms: [primaryLLM, secondaryLLM, tertiaryLLM],
|
|
41
|
+
* attemptTimeout: 5.0,
|
|
42
|
+
* maxRetryPerLLM: 1,
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class FallbackAdapter extends LLM {
|
|
47
|
+
readonly llms: LLM[];
|
|
48
|
+
readonly attemptTimeout: number;
|
|
49
|
+
readonly maxRetryPerLLM: number;
|
|
50
|
+
readonly retryInterval: number;
|
|
51
|
+
readonly retryOnChunkSent: boolean;
|
|
52
|
+
/** @internal */
|
|
53
|
+
_status: LLMStatus[];
|
|
54
|
+
private logger;
|
|
55
|
+
constructor(options: FallbackAdapterOptions);
|
|
56
|
+
get model(): string;
|
|
57
|
+
label(): string;
|
|
58
|
+
chat(opts: {
|
|
59
|
+
chatCtx: ChatContext;
|
|
60
|
+
toolCtx?: ToolContext;
|
|
61
|
+
connOptions?: APIConnectOptions;
|
|
62
|
+
parallelToolCalls?: boolean;
|
|
63
|
+
toolChoice?: ToolChoice;
|
|
64
|
+
extraKwargs?: Record<string, unknown>;
|
|
65
|
+
}): LLMStream;
|
|
66
|
+
/**
|
|
67
|
+
* Emit availability changed event.
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
_emitAvailabilityChanged(llm: LLM, available: boolean): void;
|
|
71
|
+
}
|
|
72
|
+
export {};
|
|
73
|
+
//# sourceMappingURL=fallback_adapter.d.ts.map
|