@livekit/agents 1.0.15 → 1.0.16

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.
Files changed (92) hide show
  1. package/dist/cli.cjs +12 -12
  2. package/dist/cli.cjs.map +1 -1
  3. package/dist/cli.d.cts +3 -3
  4. package/dist/cli.d.ts +3 -3
  5. package/dist/cli.d.ts.map +1 -1
  6. package/dist/cli.js +13 -13
  7. package/dist/cli.js.map +1 -1
  8. package/dist/inference/stt.cjs.map +1 -1
  9. package/dist/inference/stt.d.ts.map +1 -1
  10. package/dist/inference/stt.js +1 -1
  11. package/dist/inference/stt.js.map +1 -1
  12. package/dist/inference/tts.cjs.map +1 -1
  13. package/dist/inference/tts.d.cts +2 -1
  14. package/dist/inference/tts.d.ts +2 -1
  15. package/dist/inference/tts.d.ts.map +1 -1
  16. package/dist/inference/tts.js +1 -5
  17. package/dist/inference/tts.js.map +1 -1
  18. package/dist/llm/chat_context.cjs +78 -0
  19. package/dist/llm/chat_context.cjs.map +1 -1
  20. package/dist/llm/chat_context.d.cts +16 -0
  21. package/dist/llm/chat_context.d.ts +16 -0
  22. package/dist/llm/chat_context.d.ts.map +1 -1
  23. package/dist/llm/chat_context.js +78 -0
  24. package/dist/llm/chat_context.js.map +1 -1
  25. package/dist/llm/chat_context.test.cjs +531 -0
  26. package/dist/llm/chat_context.test.cjs.map +1 -1
  27. package/dist/llm/chat_context.test.js +531 -0
  28. package/dist/llm/chat_context.test.js.map +1 -1
  29. package/dist/llm/tool_context.cjs +40 -0
  30. package/dist/llm/tool_context.cjs.map +1 -1
  31. package/dist/llm/tool_context.d.cts +2 -0
  32. package/dist/llm/tool_context.d.ts +2 -0
  33. package/dist/llm/tool_context.d.ts.map +1 -1
  34. package/dist/llm/tool_context.js +38 -0
  35. package/dist/llm/tool_context.js.map +1 -1
  36. package/dist/metrics/base.cjs.map +1 -1
  37. package/dist/metrics/base.d.cts +7 -0
  38. package/dist/metrics/base.d.ts +7 -0
  39. package/dist/metrics/base.d.ts.map +1 -1
  40. package/dist/stt/stt.cjs +1 -0
  41. package/dist/stt/stt.cjs.map +1 -1
  42. package/dist/stt/stt.d.cts +7 -1
  43. package/dist/stt/stt.d.ts +7 -1
  44. package/dist/stt/stt.d.ts.map +1 -1
  45. package/dist/stt/stt.js +1 -0
  46. package/dist/stt/stt.js.map +1 -1
  47. package/dist/voice/agent_activity.cjs +83 -8
  48. package/dist/voice/agent_activity.cjs.map +1 -1
  49. package/dist/voice/agent_activity.d.cts +6 -2
  50. package/dist/voice/agent_activity.d.ts +6 -2
  51. package/dist/voice/agent_activity.d.ts.map +1 -1
  52. package/dist/voice/agent_activity.js +83 -8
  53. package/dist/voice/agent_activity.js.map +1 -1
  54. package/dist/voice/agent_session.cjs +3 -2
  55. package/dist/voice/agent_session.cjs.map +1 -1
  56. package/dist/voice/agent_session.d.cts +2 -1
  57. package/dist/voice/agent_session.d.ts +2 -1
  58. package/dist/voice/agent_session.d.ts.map +1 -1
  59. package/dist/voice/agent_session.js +3 -2
  60. package/dist/voice/agent_session.js.map +1 -1
  61. package/dist/voice/audio_recognition.cjs +138 -16
  62. package/dist/voice/audio_recognition.cjs.map +1 -1
  63. package/dist/voice/audio_recognition.d.cts +11 -0
  64. package/dist/voice/audio_recognition.d.ts +11 -0
  65. package/dist/voice/audio_recognition.d.ts.map +1 -1
  66. package/dist/voice/audio_recognition.js +138 -16
  67. package/dist/voice/audio_recognition.js.map +1 -1
  68. package/dist/voice/room_io/_input.cjs.map +1 -1
  69. package/dist/voice/room_io/_input.d.ts.map +1 -1
  70. package/dist/voice/room_io/_input.js +0 -1
  71. package/dist/voice/room_io/_input.js.map +1 -1
  72. package/dist/worker.cjs +17 -11
  73. package/dist/worker.cjs.map +1 -1
  74. package/dist/worker.d.cts +16 -9
  75. package/dist/worker.d.ts +16 -9
  76. package/dist/worker.d.ts.map +1 -1
  77. package/dist/worker.js +16 -12
  78. package/dist/worker.js.map +1 -1
  79. package/package.json +1 -1
  80. package/src/cli.ts +17 -17
  81. package/src/inference/stt.ts +2 -1
  82. package/src/inference/tts.ts +2 -5
  83. package/src/llm/chat_context.test.ts +607 -0
  84. package/src/llm/chat_context.ts +106 -0
  85. package/src/llm/tool_context.ts +44 -0
  86. package/src/metrics/base.ts +7 -0
  87. package/src/stt/stt.ts +6 -0
  88. package/src/voice/agent_activity.ts +119 -9
  89. package/src/voice/agent_session.ts +3 -1
  90. package/src/voice/audio_recognition.ts +235 -57
  91. package/src/voice/room_io/_input.ts +1 -1
  92. package/src/worker.ts +29 -18
@@ -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 constructor(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\n }) {\n const { callId, name, args, id = shortuuid('item_'), createdAt = Date.now() } = params;\n this.id = id;\n this.callId = callId;\n this.args = args;\n this.name = name;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\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 (!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 type ChatItem = ChatMessage | FunctionCall | FunctionCallOutput;\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 * 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,EA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mBAAmB,WAA2B;AACpD,aAAS,IAAI,KAAK,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;AACnD,YAAM,OAAO,KAAK,OAAO,CAAC;AAC1B,UAAI,KAAM,aAAa,WAAW;AAChC,eAAO,IAAI;AAAA,MACb;AAAA,IACF;AACA,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 constructor(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\n }) {\n const { callId, name, args, id = shortuuid('item_'), createdAt = Date.now() } = params;\n this.id = id;\n this.callId = callId;\n this.args = args;\n this.name = name;\n this.createdAt = createdAt;\n }\n\n static create(params: {\n callId: string;\n name: string;\n args: string;\n id?: string;\n createdAt?: number;\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 (!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 type ChatItem = ChatMessage | FunctionCall | FunctionCallOutput;\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 (a.name !== b.name || a.callId !== b.callId || a.args !== b.args) {\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,I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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":[]}
@@ -377,4 +377,535 @@ var import_chat_context = require("./chat_context.cjs");
377
377
  (0, import_vitest.expect)(ids).toEqual(["msg_1", "msg_2"]);
378
378
  });
379
379
  });
380
+ (0, import_vitest.describe)("ChatContext.isEquivalent", () => {
381
+ (0, import_vitest.it)("should return true for same reference", () => {
382
+ const ctx = new import_chat_context.ChatContext();
383
+ ctx.addMessage({
384
+ id: "msg_1",
385
+ role: "user",
386
+ content: "Hello"
387
+ });
388
+ (0, import_vitest.expect)(ctx.isEquivalent(ctx)).toBe(true);
389
+ });
390
+ (0, import_vitest.it)("should return true for identical empty contexts", () => {
391
+ const ctx1 = new import_chat_context.ChatContext();
392
+ const ctx2 = new import_chat_context.ChatContext();
393
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
394
+ });
395
+ (0, import_vitest.it)("should return false for contexts with different lengths", () => {
396
+ const ctx1 = new import_chat_context.ChatContext();
397
+ ctx1.addMessage({
398
+ id: "msg_1",
399
+ role: "user",
400
+ content: "Hello"
401
+ });
402
+ const ctx2 = new import_chat_context.ChatContext();
403
+ ctx2.addMessage({
404
+ id: "msg_1",
405
+ role: "user",
406
+ content: "Hello"
407
+ });
408
+ ctx2.addMessage({
409
+ id: "msg_2",
410
+ role: "assistant",
411
+ content: "Hi"
412
+ });
413
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
414
+ });
415
+ (0, import_vitest.it)("should return false for contexts with different item IDs", () => {
416
+ const ctx1 = new import_chat_context.ChatContext();
417
+ ctx1.addMessage({
418
+ id: "msg_1",
419
+ role: "user",
420
+ content: "Hello"
421
+ });
422
+ const ctx2 = new import_chat_context.ChatContext();
423
+ ctx2.addMessage({
424
+ id: "msg_2",
425
+ role: "user",
426
+ content: "Hello"
427
+ });
428
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
429
+ });
430
+ (0, import_vitest.it)("should return false for contexts with different item types", () => {
431
+ const ctx1 = new import_chat_context.ChatContext();
432
+ ctx1.addMessage({
433
+ id: "msg_1",
434
+ role: "user",
435
+ content: "Hello"
436
+ });
437
+ const ctx2 = new import_chat_context.ChatContext();
438
+ ctx2.insert(
439
+ new import_chat_context.FunctionCall({
440
+ id: "msg_1",
441
+ callId: "call_1",
442
+ name: "test",
443
+ args: "{}"
444
+ })
445
+ );
446
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
447
+ });
448
+ (0, import_vitest.describe)("message comparison", () => {
449
+ (0, import_vitest.it)("should return true for identical messages", () => {
450
+ const ctx1 = new import_chat_context.ChatContext();
451
+ ctx1.addMessage({
452
+ id: "msg_1",
453
+ role: "user",
454
+ content: "Hello",
455
+ interrupted: false
456
+ });
457
+ const ctx2 = new import_chat_context.ChatContext();
458
+ ctx2.addMessage({
459
+ id: "msg_1",
460
+ role: "user",
461
+ content: "Hello",
462
+ interrupted: false
463
+ });
464
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
465
+ });
466
+ (0, import_vitest.it)("should return false for messages with different roles", () => {
467
+ const ctx1 = new import_chat_context.ChatContext();
468
+ ctx1.addMessage({
469
+ id: "msg_1",
470
+ role: "user",
471
+ content: "Hello"
472
+ });
473
+ const ctx2 = new import_chat_context.ChatContext();
474
+ ctx2.addMessage({
475
+ id: "msg_1",
476
+ role: "assistant",
477
+ content: "Hello"
478
+ });
479
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
480
+ });
481
+ (0, import_vitest.it)("should return false for messages with different interrupted flags", () => {
482
+ const ctx1 = new import_chat_context.ChatContext();
483
+ ctx1.addMessage({
484
+ id: "msg_1",
485
+ role: "user",
486
+ content: "Hello",
487
+ interrupted: false
488
+ });
489
+ const ctx2 = new import_chat_context.ChatContext();
490
+ ctx2.addMessage({
491
+ id: "msg_1",
492
+ role: "user",
493
+ content: "Hello",
494
+ interrupted: true
495
+ });
496
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
497
+ });
498
+ (0, import_vitest.it)("should return false for messages with different content", () => {
499
+ const ctx1 = new import_chat_context.ChatContext();
500
+ ctx1.addMessage({
501
+ id: "msg_1",
502
+ role: "user",
503
+ content: "Hello"
504
+ });
505
+ const ctx2 = new import_chat_context.ChatContext();
506
+ ctx2.addMessage({
507
+ id: "msg_1",
508
+ role: "user",
509
+ content: "World"
510
+ });
511
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
512
+ });
513
+ (0, import_vitest.it)("should return true for messages with identical array content", () => {
514
+ const ctx1 = new import_chat_context.ChatContext();
515
+ ctx1.addMessage({
516
+ id: "msg_1",
517
+ role: "user",
518
+ content: ["Hello", "World"]
519
+ });
520
+ const ctx2 = new import_chat_context.ChatContext();
521
+ ctx2.addMessage({
522
+ id: "msg_1",
523
+ role: "user",
524
+ content: ["Hello", "World"]
525
+ });
526
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
527
+ });
528
+ (0, import_vitest.it)("should return false for messages with different array content", () => {
529
+ const ctx1 = new import_chat_context.ChatContext();
530
+ ctx1.addMessage({
531
+ id: "msg_1",
532
+ role: "user",
533
+ content: ["Hello", "World"]
534
+ });
535
+ const ctx2 = new import_chat_context.ChatContext();
536
+ ctx2.addMessage({
537
+ id: "msg_1",
538
+ role: "user",
539
+ content: ["Hello"]
540
+ });
541
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
542
+ });
543
+ (0, import_vitest.it)("should return true for messages with identical image content", () => {
544
+ const imageContent = {
545
+ id: "img_1",
546
+ type: "image_content",
547
+ image: "https://example.com/image.jpg",
548
+ inferenceDetail: "high",
549
+ inferenceWidth: 1024,
550
+ inferenceHeight: 768,
551
+ mimeType: "image/jpeg",
552
+ _cache: {}
553
+ };
554
+ const ctx1 = new import_chat_context.ChatContext();
555
+ ctx1.addMessage({
556
+ id: "msg_1",
557
+ role: "user",
558
+ content: ["Check this:", imageContent]
559
+ });
560
+ const ctx2 = new import_chat_context.ChatContext();
561
+ ctx2.addMessage({
562
+ id: "msg_1",
563
+ role: "user",
564
+ content: ["Check this:", { ...imageContent }]
565
+ });
566
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
567
+ });
568
+ (0, import_vitest.it)("should return false for messages with different image content", () => {
569
+ const imageContent1 = {
570
+ id: "img_1",
571
+ type: "image_content",
572
+ image: "https://example.com/image1.jpg",
573
+ inferenceDetail: "high",
574
+ _cache: {}
575
+ };
576
+ const imageContent2 = {
577
+ id: "img_2",
578
+ type: "image_content",
579
+ image: "https://example.com/image2.jpg",
580
+ inferenceDetail: "high",
581
+ _cache: {}
582
+ };
583
+ const ctx1 = new import_chat_context.ChatContext();
584
+ ctx1.addMessage({
585
+ id: "msg_1",
586
+ role: "user",
587
+ content: ["Check this:", imageContent1]
588
+ });
589
+ const ctx2 = new import_chat_context.ChatContext();
590
+ ctx2.addMessage({
591
+ id: "msg_1",
592
+ role: "user",
593
+ content: ["Check this:", imageContent2]
594
+ });
595
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
596
+ });
597
+ });
598
+ (0, import_vitest.describe)("function call comparison", () => {
599
+ (0, import_vitest.it)("should return true for identical function calls", () => {
600
+ const ctx1 = new import_chat_context.ChatContext();
601
+ ctx1.insert(
602
+ new import_chat_context.FunctionCall({
603
+ id: "func_1",
604
+ callId: "call_1",
605
+ name: "get_weather",
606
+ args: '{"location": "Paris"}'
607
+ })
608
+ );
609
+ const ctx2 = new import_chat_context.ChatContext();
610
+ ctx2.insert(
611
+ new import_chat_context.FunctionCall({
612
+ id: "func_1",
613
+ callId: "call_1",
614
+ name: "get_weather",
615
+ args: '{"location": "Paris"}'
616
+ })
617
+ );
618
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
619
+ });
620
+ (0, import_vitest.it)("should return false for function calls with different names", () => {
621
+ const ctx1 = new import_chat_context.ChatContext();
622
+ ctx1.insert(
623
+ new import_chat_context.FunctionCall({
624
+ id: "func_1",
625
+ callId: "call_1",
626
+ name: "get_weather",
627
+ args: "{}"
628
+ })
629
+ );
630
+ const ctx2 = new import_chat_context.ChatContext();
631
+ ctx2.insert(
632
+ new import_chat_context.FunctionCall({
633
+ id: "func_1",
634
+ callId: "call_1",
635
+ name: "get_time",
636
+ args: "{}"
637
+ })
638
+ );
639
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
640
+ });
641
+ (0, import_vitest.it)("should return false for function calls with different call IDs", () => {
642
+ const ctx1 = new import_chat_context.ChatContext();
643
+ ctx1.insert(
644
+ new import_chat_context.FunctionCall({
645
+ id: "func_1",
646
+ callId: "call_1",
647
+ name: "get_weather",
648
+ args: "{}"
649
+ })
650
+ );
651
+ const ctx2 = new import_chat_context.ChatContext();
652
+ ctx2.insert(
653
+ new import_chat_context.FunctionCall({
654
+ id: "func_1",
655
+ callId: "call_2",
656
+ name: "get_weather",
657
+ args: "{}"
658
+ })
659
+ );
660
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
661
+ });
662
+ (0, import_vitest.it)("should return false for function calls with different arguments", () => {
663
+ const ctx1 = new import_chat_context.ChatContext();
664
+ ctx1.insert(
665
+ new import_chat_context.FunctionCall({
666
+ id: "func_1",
667
+ callId: "call_1",
668
+ name: "get_weather",
669
+ args: '{"location": "Paris"}'
670
+ })
671
+ );
672
+ const ctx2 = new import_chat_context.ChatContext();
673
+ ctx2.insert(
674
+ new import_chat_context.FunctionCall({
675
+ id: "func_1",
676
+ callId: "call_1",
677
+ name: "get_weather",
678
+ args: '{"location": "London"}'
679
+ })
680
+ );
681
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
682
+ });
683
+ (0, import_vitest.it)("should ignore timestamps", () => {
684
+ const ctx1 = new import_chat_context.ChatContext();
685
+ ctx1.insert(
686
+ new import_chat_context.FunctionCall({
687
+ id: "func_1",
688
+ callId: "call_1",
689
+ name: "get_weather",
690
+ args: "{}",
691
+ createdAt: 1e3
692
+ })
693
+ );
694
+ const ctx2 = new import_chat_context.ChatContext();
695
+ ctx2.insert(
696
+ new import_chat_context.FunctionCall({
697
+ id: "func_1",
698
+ callId: "call_1",
699
+ name: "get_weather",
700
+ args: "{}",
701
+ createdAt: 2e3
702
+ })
703
+ );
704
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
705
+ });
706
+ });
707
+ (0, import_vitest.describe)("function call output comparison", () => {
708
+ (0, import_vitest.it)("should return true for identical function call outputs", () => {
709
+ const ctx1 = new import_chat_context.ChatContext();
710
+ ctx1.insert(
711
+ new import_chat_context.FunctionCallOutput({
712
+ id: "output_1",
713
+ callId: "call_1",
714
+ name: "get_weather",
715
+ output: '{"temperature": 22}',
716
+ isError: false
717
+ })
718
+ );
719
+ const ctx2 = new import_chat_context.ChatContext();
720
+ ctx2.insert(
721
+ new import_chat_context.FunctionCallOutput({
722
+ id: "output_1",
723
+ callId: "call_1",
724
+ name: "get_weather",
725
+ output: '{"temperature": 22}',
726
+ isError: false
727
+ })
728
+ );
729
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
730
+ });
731
+ (0, import_vitest.it)("should return false for function call outputs with different names", () => {
732
+ const ctx1 = new import_chat_context.ChatContext();
733
+ ctx1.insert(
734
+ new import_chat_context.FunctionCallOutput({
735
+ id: "output_1",
736
+ callId: "call_1",
737
+ name: "get_weather",
738
+ output: "{}",
739
+ isError: false
740
+ })
741
+ );
742
+ const ctx2 = new import_chat_context.ChatContext();
743
+ ctx2.insert(
744
+ new import_chat_context.FunctionCallOutput({
745
+ id: "output_1",
746
+ callId: "call_1",
747
+ name: "get_time",
748
+ output: "{}",
749
+ isError: false
750
+ })
751
+ );
752
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
753
+ });
754
+ (0, import_vitest.it)("should return false for function call outputs with different call IDs", () => {
755
+ const ctx1 = new import_chat_context.ChatContext();
756
+ ctx1.insert(
757
+ new import_chat_context.FunctionCallOutput({
758
+ id: "output_1",
759
+ callId: "call_1",
760
+ name: "get_weather",
761
+ output: "{}",
762
+ isError: false
763
+ })
764
+ );
765
+ const ctx2 = new import_chat_context.ChatContext();
766
+ ctx2.insert(
767
+ new import_chat_context.FunctionCallOutput({
768
+ id: "output_1",
769
+ callId: "call_2",
770
+ name: "get_weather",
771
+ output: "{}",
772
+ isError: false
773
+ })
774
+ );
775
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
776
+ });
777
+ (0, import_vitest.it)("should return false for function call outputs with different output values", () => {
778
+ const ctx1 = new import_chat_context.ChatContext();
779
+ ctx1.insert(
780
+ new import_chat_context.FunctionCallOutput({
781
+ id: "output_1",
782
+ callId: "call_1",
783
+ name: "get_weather",
784
+ output: '{"temperature": 22}',
785
+ isError: false
786
+ })
787
+ );
788
+ const ctx2 = new import_chat_context.ChatContext();
789
+ ctx2.insert(
790
+ new import_chat_context.FunctionCallOutput({
791
+ id: "output_1",
792
+ callId: "call_1",
793
+ name: "get_weather",
794
+ output: '{"temperature": 25}',
795
+ isError: false
796
+ })
797
+ );
798
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
799
+ });
800
+ (0, import_vitest.it)("should return false for function call outputs with different error flags", () => {
801
+ const ctx1 = new import_chat_context.ChatContext();
802
+ ctx1.insert(
803
+ new import_chat_context.FunctionCallOutput({
804
+ id: "output_1",
805
+ callId: "call_1",
806
+ name: "get_weather",
807
+ output: "Error occurred",
808
+ isError: false
809
+ })
810
+ );
811
+ const ctx2 = new import_chat_context.ChatContext();
812
+ ctx2.insert(
813
+ new import_chat_context.FunctionCallOutput({
814
+ id: "output_1",
815
+ callId: "call_1",
816
+ name: "get_weather",
817
+ output: "Error occurred",
818
+ isError: true
819
+ })
820
+ );
821
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(false);
822
+ });
823
+ (0, import_vitest.it)("should ignore timestamps", () => {
824
+ const ctx1 = new import_chat_context.ChatContext();
825
+ ctx1.insert(
826
+ new import_chat_context.FunctionCallOutput({
827
+ id: "output_1",
828
+ callId: "call_1",
829
+ name: "get_weather",
830
+ output: "{}",
831
+ isError: false,
832
+ createdAt: 1e3
833
+ })
834
+ );
835
+ const ctx2 = new import_chat_context.ChatContext();
836
+ ctx2.insert(
837
+ new import_chat_context.FunctionCallOutput({
838
+ id: "output_1",
839
+ callId: "call_1",
840
+ name: "get_weather",
841
+ output: "{}",
842
+ isError: false,
843
+ createdAt: 2e3
844
+ })
845
+ );
846
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
847
+ });
848
+ });
849
+ (0, import_vitest.describe)("complex context comparison", () => {
850
+ (0, import_vitest.it)("should return true for identical complex contexts", () => {
851
+ const ctx1 = new import_chat_context.ChatContext();
852
+ ctx1.addMessage({
853
+ id: "msg_1",
854
+ role: "user",
855
+ content: "What is the weather?"
856
+ });
857
+ ctx1.insert(
858
+ new import_chat_context.FunctionCall({
859
+ id: "func_1",
860
+ callId: "call_1",
861
+ name: "get_weather",
862
+ args: '{"location": "Paris"}'
863
+ })
864
+ );
865
+ ctx1.insert(
866
+ new import_chat_context.FunctionCallOutput({
867
+ id: "output_1",
868
+ callId: "call_1",
869
+ name: "get_weather",
870
+ output: '{"temperature": 22}',
871
+ isError: false
872
+ })
873
+ );
874
+ ctx1.addMessage({
875
+ id: "msg_2",
876
+ role: "assistant",
877
+ content: "The weather is 22\xB0C"
878
+ });
879
+ const ctx2 = new import_chat_context.ChatContext();
880
+ ctx2.addMessage({
881
+ id: "msg_1",
882
+ role: "user",
883
+ content: "What is the weather?"
884
+ });
885
+ ctx2.insert(
886
+ new import_chat_context.FunctionCall({
887
+ id: "func_1",
888
+ callId: "call_1",
889
+ name: "get_weather",
890
+ args: '{"location": "Paris"}'
891
+ })
892
+ );
893
+ ctx2.insert(
894
+ new import_chat_context.FunctionCallOutput({
895
+ id: "output_1",
896
+ callId: "call_1",
897
+ name: "get_weather",
898
+ output: '{"temperature": 22}',
899
+ isError: false
900
+ })
901
+ );
902
+ ctx2.addMessage({
903
+ id: "msg_2",
904
+ role: "assistant",
905
+ content: "The weather is 22\xB0C"
906
+ });
907
+ (0, import_vitest.expect)(ctx1.isEquivalent(ctx2)).toBe(true);
908
+ });
909
+ });
910
+ });
380
911
  //# sourceMappingURL=chat_context.test.cjs.map