@copilotkit/runtime 1.3.12 → 1.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/{chunk-IXVCESAR.mjs → chunk-3LFBIWWK.mjs} +3 -3
  3. package/dist/{chunk-F2KJWMGD.mjs → chunk-76K222WC.mjs} +3 -3
  4. package/dist/{chunk-F2KJWMGD.mjs.map → chunk-76K222WC.mjs.map} +1 -1
  5. package/dist/{chunk-ZEHCLFJ2.mjs → chunk-CPAHDRLS.mjs} +28 -4
  6. package/dist/chunk-CPAHDRLS.mjs.map +1 -0
  7. package/dist/{chunk-IZP72K7I.mjs → chunk-DEZQR4EM.mjs} +2 -2
  8. package/dist/{chunk-WBLYFYMZ.mjs → chunk-MCUYYSCJ.mjs} +2 -2
  9. package/dist/{chunk-PRG6VWHR.mjs → chunk-SPZSO3T3.mjs} +2 -2
  10. package/dist/{chunk-24WEOOFX.mjs → chunk-Y6RQG5HF.mjs} +2 -2
  11. package/dist/index.js +28 -4
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +7 -7
  14. package/dist/lib/index.js +28 -4
  15. package/dist/lib/index.js.map +1 -1
  16. package/dist/lib/index.mjs +7 -7
  17. package/dist/lib/integrations/index.js +1 -1
  18. package/dist/lib/integrations/index.js.map +1 -1
  19. package/dist/lib/integrations/index.mjs +5 -5
  20. package/dist/lib/integrations/nest/index.js +1 -1
  21. package/dist/lib/integrations/nest/index.js.map +1 -1
  22. package/dist/lib/integrations/nest/index.mjs +3 -3
  23. package/dist/lib/integrations/node-express/index.js +1 -1
  24. package/dist/lib/integrations/node-express/index.js.map +1 -1
  25. package/dist/lib/integrations/node-express/index.mjs +3 -3
  26. package/dist/lib/integrations/node-http/index.js +1 -1
  27. package/dist/lib/integrations/node-http/index.js.map +1 -1
  28. package/dist/lib/integrations/node-http/index.mjs +2 -2
  29. package/dist/service-adapters/index.js +27 -3
  30. package/dist/service-adapters/index.js.map +1 -1
  31. package/dist/service-adapters/index.mjs +2 -2
  32. package/package.json +4 -4
  33. package/src/service-adapters/langchain/utils.ts +31 -3
  34. package/dist/chunk-ZEHCLFJ2.mjs.map +0 -1
  35. /package/dist/{chunk-IXVCESAR.mjs.map → chunk-3LFBIWWK.mjs.map} +0 -0
  36. /package/dist/{chunk-IZP72K7I.mjs.map → chunk-DEZQR4EM.mjs.map} +0 -0
  37. /package/dist/{chunk-WBLYFYMZ.mjs.map → chunk-MCUYYSCJ.mjs.map} +0 -0
  38. /package/dist/{chunk-PRG6VWHR.mjs.map → chunk-SPZSO3T3.mjs.map} +0 -0
  39. /package/dist/{chunk-24WEOOFX.mjs.map → chunk-Y6RQG5HF.mjs.map} +0 -0
@@ -121,20 +121,39 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
121
121
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
122
122
  let reader = result.getReader();
123
123
  let mode = null;
124
+ const toolCallDetails = {
125
+ name: null,
126
+ id: null,
127
+ index: null,
128
+ prevIndex: null
129
+ };
124
130
  while (true) {
125
131
  try {
126
132
  const { done, value } = await reader.read();
127
133
  let toolCallName = void 0;
128
134
  let toolCallId = void 0;
129
135
  let toolCallArgs = void 0;
136
+ let toolCallIndex = void 0;
137
+ let toolCallPrevIndex = void 0;
130
138
  let hasToolCall = false;
131
139
  let content = value == null ? void 0 : value.content;
132
140
  if (isAIMessageChunk(value)) {
133
141
  let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
134
- toolCallName = chunk == null ? void 0 : chunk.name;
135
- toolCallId = chunk == null ? void 0 : chunk.id;
136
142
  toolCallArgs = chunk == null ? void 0 : chunk.args;
137
143
  hasToolCall = chunk != void 0;
144
+ if (chunk == null ? void 0 : chunk.name)
145
+ toolCallDetails.name = chunk.name;
146
+ if ((chunk == null ? void 0 : chunk.index) != null) {
147
+ toolCallDetails.index = chunk.index;
148
+ if (toolCallDetails.prevIndex == null)
149
+ toolCallDetails.prevIndex = chunk.index;
150
+ }
151
+ if (chunk == null ? void 0 : chunk.id)
152
+ toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;
153
+ toolCallName = toolCallDetails.name;
154
+ toolCallId = toolCallDetails.id;
155
+ toolCallIndex = toolCallDetails.index;
156
+ toolCallPrevIndex = toolCallDetails.prevIndex;
138
157
  } else if (isBaseMessageChunk(value)) {
139
158
  let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
140
159
  toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
@@ -153,7 +172,7 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
153
172
  break;
154
173
  }
155
174
  if (mode === null) {
156
- if (hasToolCall) {
175
+ if (hasToolCall && toolCallId && toolCallName) {
157
176
  mode = "function";
158
177
  eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
159
178
  } else if (content) {
@@ -164,6 +183,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
164
183
  if (mode === "message" && content) {
165
184
  eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
166
185
  } else if (mode === "function" && toolCallArgs) {
186
+ if (toolCallIndex !== toolCallPrevIndex) {
187
+ eventStream$.sendActionExecutionEnd();
188
+ eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
189
+ toolCallDetails.prevIndex = toolCallDetails.index;
190
+ }
167
191
  eventStream$.sendActionExecutionArgs(toolCallArgs);
168
192
  }
169
193
  } catch (error) {
@@ -195,4 +219,4 @@ export {
195
219
  convertActionInputToLangChainTool,
196
220
  streamLangChainResponse
197
221
  };
198
- //# sourceMappingURL=chunk-ZEHCLFJ2.mjs.map
222
+ //# sourceMappingURL=chunk-CPAHDRLS.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/service-adapters/langchain/utils.ts"],"sourcesContent":["import {\n ActionExecutionMessage,\n Message,\n ResultMessage,\n TextMessage,\n} from \"../../graphql/types/converted\";\nimport {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageChunk,\n HumanMessage,\n SystemMessage,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { z } from \"zod\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { LangChainReturnType } from \"./types\";\nimport { RuntimeEventSubject } from \"../events\";\nimport { randomId } from \"@copilotkit/shared\";\n\nexport function convertMessageToLangChainMessage(message: Message): BaseMessage {\n if (message.isTextMessage()) {\n if (message.role == \"user\") {\n return new HumanMessage(message.content);\n } else if (message.role == \"assistant\") {\n return new AIMessage(message.content);\n } else if (message.role === \"system\") {\n return new SystemMessage(message.content);\n }\n } else if (message.isActionExecutionMessage()) {\n return new AIMessage({\n content: \"\",\n tool_calls: [\n {\n id: message.id,\n args: message.arguments,\n name: message.name,\n },\n ],\n });\n } else if (message.isResultMessage()) {\n return new ToolMessage({\n content: message.result,\n tool_call_id: message.actionExecutionId,\n });\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n\n if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {\n return !required ? z.object(spec).optional() : z.object(spec);\n }\n\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return required ? schema : schema.optional();\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);\n let schema = z.array(itemSchema);\n return required ? schema : schema.optional();\n }\n}\n\nexport function convertActionInputToLangChainTool(actionInput: ActionInput): any {\n return new DynamicStructuredTool({\n name: actionInput.name,\n description: actionInput.description,\n schema: convertJsonSchemaToZodSchema(\n JSON.parse(actionInput.jsonSchema),\n true,\n ) as z.ZodObject<any>,\n func: async () => {\n return \"\";\n },\n });\n}\n\ninterface StreamLangChainResponseParams {\n result: LangChainReturnType;\n eventStream$: RuntimeEventSubject;\n actionExecution?: {\n id: string;\n name: string;\n };\n}\n\nfunction getConstructorName(object: any): string {\n if (object && typeof object === \"object\" && object.constructor && object.constructor.name) {\n return object.constructor.name;\n }\n return \"\";\n}\n\nfunction isAIMessage(message: any): message is AIMessage {\n return Object.prototype.toString.call(message) === \"[object AIMessage]\";\n}\n\nfunction isAIMessageChunk(message: any): message is AIMessageChunk {\n return Object.prototype.toString.call(message) === \"[object AIMessageChunk]\";\n}\n\nfunction isBaseMessageChunk(message: any): message is BaseMessageChunk {\n return Object.prototype.toString.call(message) === \"[object BaseMessageChunk]\";\n}\n\nfunction maybeSendActionExecutionResultIsMessage(\n eventStream$: RuntimeEventSubject,\n actionExecution?: { id: string; name: string },\n) {\n // language models need a result after the function call\n // we simply let them know that we are sending a message\n if (actionExecution) {\n eventStream$.sendActionExecutionResult(\n actionExecution.id,\n actionExecution.name,\n \"Sending a message\",\n );\n }\n}\n\nexport async function streamLangChainResponse({\n result,\n eventStream$,\n actionExecution,\n}: StreamLangChainResponseParams) {\n // We support several types of return values from LangChain functions:\n\n // 1. string\n\n if (typeof result === \"string\") {\n if (!actionExecution) {\n // Just send one chunk with the string as the content.\n eventStream$.sendTextMessage(randomId(), result);\n } else {\n // Send as a result\n eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);\n }\n }\n\n // 2. AIMessage\n // Send the content and function call of the AIMessage as the content of the chunk.\n else if (isAIMessage(result)) {\n maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);\n\n if (result.content) {\n eventStream$.sendTextMessage(randomId(), result.content as string);\n }\n for (const toolCall of result.tool_calls) {\n eventStream$.sendActionExecution(\n toolCall.id || randomId(),\n toolCall.name,\n JSON.stringify(toolCall.args),\n );\n }\n }\n\n // 3. BaseMessageChunk\n // Send the content and function call of the AIMessage as the content of the chunk.\n else if (isBaseMessageChunk(result)) {\n maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);\n\n if (result.lc_kwargs?.content) {\n eventStream$.sendTextMessage(randomId(), result.content as string);\n }\n if (result.lc_kwargs?.tool_calls) {\n for (const toolCall of result.lc_kwargs?.tool_calls) {\n eventStream$.sendActionExecution(\n toolCall.id || randomId(),\n toolCall.name,\n JSON.stringify(toolCall.args),\n );\n }\n }\n }\n\n // 4. IterableReadableStream\n // Stream the result of the LangChain function.\n else if (result && \"getReader\" in result) {\n maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);\n\n let reader = result.getReader();\n\n let mode: \"function\" | \"message\" | null = null;\n\n const toolCallDetails = {\n name: null,\n id: null,\n index: null,\n prevIndex: null,\n };\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n let toolCallName: string | undefined = undefined;\n let toolCallId: string | undefined = undefined;\n let toolCallArgs: string | undefined = undefined;\n let toolCallIndex: number | undefined = undefined;\n let toolCallPrevIndex: number | undefined = undefined;\n let hasToolCall: boolean = false;\n let content = value?.content as string;\n\n if (isAIMessageChunk(value)) {\n let chunk = value.tool_call_chunks?.[0];\n toolCallArgs = chunk?.args;\n hasToolCall = chunk != undefined;\n if (chunk?.name) toolCallDetails.name = chunk.name;\n // track different index on the same tool cool\n if (chunk?.index != null) {\n toolCallDetails.index = chunk.index; // 1\n if (toolCallDetails.prevIndex == null) toolCallDetails.prevIndex = chunk.index;\n }\n // Differentiate when calling the same tool but with different index\n if (chunk?.id)\n toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;\n\n // Assign to internal variables that the entire script here knows how to work with\n toolCallName = toolCallDetails.name;\n toolCallId = toolCallDetails.id;\n toolCallIndex = toolCallDetails.index;\n toolCallPrevIndex = toolCallDetails.prevIndex;\n } else if (isBaseMessageChunk(value)) {\n let chunk = value.additional_kwargs?.tool_calls?.[0];\n toolCallName = chunk?.function?.name;\n toolCallId = chunk?.id;\n toolCallArgs = chunk?.function?.arguments;\n hasToolCall = chunk?.function != undefined;\n }\n\n // When switching from message to function or vice versa,\n // send the respective end event.\n // If toolCallName is defined, it means a new tool call starts.\n if (mode === \"message\" && (toolCallId || done)) {\n mode = null;\n eventStream$.sendTextMessageEnd();\n } else if (mode === \"function\" && (!hasToolCall || done)) {\n mode = null;\n eventStream$.sendActionExecutionEnd();\n }\n\n if (done) {\n break;\n }\n\n // If we send a new message type, send the appropriate start event.\n if (mode === null) {\n if (hasToolCall && toolCallId && toolCallName) {\n mode = \"function\";\n eventStream$.sendActionExecutionStart(toolCallId, toolCallName);\n } else if (content) {\n mode = \"message\";\n eventStream$.sendTextMessageStart(randomId());\n }\n }\n\n // send the content events\n if (mode === \"message\" && content) {\n eventStream$.sendTextMessageContent(\n Array.isArray(content) ? (content[0]?.text ?? \"\") : content,\n );\n } else if (mode === \"function\" && toolCallArgs) {\n // For calls of the same tool with different index, we seal last tool call and register a new one\n if (toolCallIndex !== toolCallPrevIndex) {\n eventStream$.sendActionExecutionEnd();\n eventStream$.sendActionExecutionStart(toolCallId, toolCallName);\n toolCallDetails.prevIndex = toolCallDetails.index;\n }\n eventStream$.sendActionExecutionArgs(toolCallArgs);\n }\n } catch (error) {\n console.error(\"Error reading from stream\", error);\n break;\n }\n }\n } else if (actionExecution) {\n eventStream$.sendActionExecutionResult(\n actionExecution.id,\n actionExecution.name,\n encodeResult(result),\n );\n }\n\n // unsupported type\n else {\n throw new Error(\"Invalid return type from LangChain function.\");\n }\n\n eventStream$.complete();\n}\n\nfunction encodeResult(result: any): string {\n if (result === undefined) {\n return \"\";\n } else if (typeof result === \"string\") {\n return result;\n } else {\n return JSON.stringify(result);\n }\n}\n"],"mappings":";;;;;AAMA,SACEA,WAIAC,cACAC,eACAC,mBACK;AACP,SAASC,6BAA6B;AACtC,SAASC,SAAS;AAIlB,SAASC,gBAAgB;AAElB,SAASC,iCAAiCC,SAAgB;AAC/D,MAAIA,QAAQC,cAAa,GAAI;AAC3B,QAAID,QAAQE,QAAQ,QAAQ;AAC1B,aAAO,IAAIC,aAAaH,QAAQI,OAAO;IACzC,WAAWJ,QAAQE,QAAQ,aAAa;AACtC,aAAO,IAAIG,UAAUL,QAAQI,OAAO;IACtC,WAAWJ,QAAQE,SAAS,UAAU;AACpC,aAAO,IAAII,cAAcN,QAAQI,OAAO;IAC1C;EACF,WAAWJ,QAAQO,yBAAwB,GAAI;AAC7C,WAAO,IAAIF,UAAU;MACnBD,SAAS;MACTI,YAAY;QACV;UACEC,IAAIT,QAAQS;UACZC,MAAMV,QAAQW;UACdC,MAAMZ,QAAQY;QAChB;;IAEJ,CAAA;EACF,WAAWZ,QAAQa,gBAAe,GAAI;AACpC,WAAO,IAAIC,YAAY;MACrBV,SAASJ,QAAQe;MACjBC,cAAchB,QAAQiB;IACxB,CAAA;EACF;AACF;AA1BgBlB;AA4BT,SAASmB,6BAA6BC,YAAiBC,UAAiB;AAC7E,MAAID,WAAWE,SAAS,UAAU;AAChC,UAAMC,OAAuC,CAAC;AAE9C,QAAI,CAACH,WAAWI,cAAc,CAACC,OAAOC,KAAKN,WAAWI,UAAU,EAAEG,QAAQ;AACxE,aAAO,CAACN,WAAWO,EAAEC,OAAON,IAAAA,EAAMO,SAAQ,IAAKF,EAAEC,OAAON,IAAAA;IAC1D;AAEA,eAAW,CAACQ,KAAKC,KAAAA,KAAUP,OAAOQ,QAAQb,WAAWI,UAAU,GAAG;AAChED,WAAKQ,GAAAA,IAAOZ,6BACVa,OACAZ,WAAWC,WAAWD,WAAWC,SAASa,SAASH,GAAAA,IAAO,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B;QACrC;AAEA,YAAIhB,MAAM;AACR;QACF;AAGA,YAAIJ,SAAS,MAAM;AACjB,cAAIa,eAAeJ,cAAcF,cAAc;AAC7CP,mBAAO;AACPnB,yBAAawC,yBAAyBZ,YAAYF,YAAAA;UACpD,WAAWjB,SAAS;AAClBU,mBAAO;AACPnB,yBAAayC,qBAAqBjC,SAAAA,CAAAA;UACpC;QACF;AAGA,YAAIW,SAAS,aAAaV,SAAS;AACjCT,uBAAa0C,uBACXC,MAAMC,QAAQnC,OAAAA,MAAYA,aAAQ,CAAA,MAARA,mBAAYoC,SAAQ,KAAMpC,OAAAA;QAExD,WAAWU,SAAS,cAAcU,cAAc;AAE9C,cAAIC,kBAAkBC,mBAAmB;AACvC/B,yBAAauC,uBAAsB;AACnCvC,yBAAawC,yBAAyBZ,YAAYF,YAAAA;AAClDN,4BAAgBE,YAAYF,gBAAgBC;UAC9C;AACArB,uBAAa8C,wBAAwBjB,YAAAA;QACvC;MACF,SAASkB,OAAP;AACAC,gBAAQD,MAAM,6BAA6BA,KAAAA;AAC3C;MACF;IACF;EACF,WAAW9C,iBAAiB;AAC1BD,iBAAaE,0BACXD,gBAAgBE,IAChBF,gBAAgBG,MAChB6C,aAAa3C,MAAAA,CAAAA;EAEjB,OAGK;AACH,UAAM,IAAI4C,MAAM,8CAAA;EAClB;AAEAlD,eAAamD,SAAQ;AACvB;AAzKsB9C;AA2KtB,SAAS4C,aAAa3C,QAAW;AAC/B,MAAIA,WAAWqB,QAAW;AACxB,WAAO;EACT,WAAW,OAAOrB,WAAW,UAAU;AACrC,WAAOA;EACT,OAAO;AACL,WAAOO,KAAKC,UAAUR,MAAAA;EACxB;AACF;AARS2C;","names":["AIMessage","HumanMessage","SystemMessage","ToolMessage","DynamicStructuredTool","z","randomId","convertMessageToLangChainMessage","message","isTextMessage","role","HumanMessage","content","AIMessage","SystemMessage","isActionExecutionMessage","tool_calls","id","args","arguments","name","isResultMessage","ToolMessage","result","tool_call_id","actionExecutionId","convertJsonSchemaToZodSchema","jsonSchema","required","type","spec","properties","Object","keys","length","z","object","optional","key","value","entries","includes","schema","string","describe","description","number","boolean","itemSchema","items","array","convertActionInputToLangChainTool","actionInput","DynamicStructuredTool","JSON","parse","func","isAIMessage","message","Object","prototype","toString","call","isAIMessageChunk","isBaseMessageChunk","maybeSendActionExecutionResultIsMessage","eventStream$","actionExecution","sendActionExecutionResult","id","name","streamLangChainResponse","result","sendTextMessage","randomId","content","toolCall","tool_calls","sendActionExecution","JSON","stringify","args","lc_kwargs","reader","getReader","mode","toolCallDetails","index","prevIndex","done","value","read","toolCallName","undefined","toolCallId","toolCallArgs","toolCallIndex","toolCallPrevIndex","hasToolCall","chunk","tool_call_chunks","additional_kwargs","function","arguments","sendTextMessageEnd","sendActionExecutionEnd","sendActionExecutionStart","sendTextMessageStart","sendTextMessageContent","Array","isArray","text","sendActionExecutionArgs","error","console","encodeResult","Error","complete"]}
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  getCommonConfig,
3
3
  telemetry_client_default
4
- } from "./chunk-F2KJWMGD.mjs";
4
+ } from "./chunk-76K222WC.mjs";
5
5
  import {
6
6
  __name
7
7
  } from "./chunk-44O2JGUY.mjs";
@@ -76,4 +76,4 @@ export {
76
76
  config,
77
77
  copilotRuntimeNextJSPagesRouterEndpoint
78
78
  };
79
- //# sourceMappingURL=chunk-IZP72K7I.mjs.map
79
+ //# sourceMappingURL=chunk-DEZQR4EM.mjs.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  telemetry_client_default
4
- } from "./chunk-F2KJWMGD.mjs";
4
+ } from "./chunk-76K222WC.mjs";
5
5
  import {
6
6
  __name
7
7
  } from "./chunk-44O2JGUY.mjs";
@@ -21,4 +21,4 @@ __name(copilotRuntimeNodeExpressEndpoint, "copilotRuntimeNodeExpressEndpoint");
21
21
  export {
22
22
  copilotRuntimeNodeExpressEndpoint
23
23
  };
24
- //# sourceMappingURL=chunk-WBLYFYMZ.mjs.map
24
+ //# sourceMappingURL=chunk-MCUYYSCJ.mjs.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  telemetry_client_default
4
- } from "./chunk-F2KJWMGD.mjs";
4
+ } from "./chunk-76K222WC.mjs";
5
5
  import {
6
6
  __name
7
7
  } from "./chunk-44O2JGUY.mjs";
@@ -21,4 +21,4 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
21
21
  export {
22
22
  copilotRuntimeNestEndpoint
23
23
  };
24
- //# sourceMappingURL=chunk-PRG6VWHR.mjs.map
24
+ //# sourceMappingURL=chunk-SPZSO3T3.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  convertActionInputToLangChainTool,
3
3
  convertMessageToLangChainMessage,
4
4
  streamLangChainResponse
5
- } from "./chunk-ZEHCLFJ2.mjs";
5
+ } from "./chunk-CPAHDRLS.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-44O2JGUY.mjs";
@@ -981,4 +981,4 @@ export {
981
981
  GroqAdapter,
982
982
  AnthropicAdapter
983
983
  };
984
- //# sourceMappingURL=chunk-24WEOOFX.mjs.map
984
+ //# sourceMappingURL=chunk-Y6RQG5HF.mjs.map
package/dist/index.js CHANGED
@@ -44,7 +44,7 @@ var require_package = __commonJS({
44
44
  publishConfig: {
45
45
  access: "public"
46
46
  },
47
- version: "1.3.12",
47
+ version: "1.3.13",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -583,20 +583,39 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
583
583
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
584
584
  let reader = result.getReader();
585
585
  let mode = null;
586
+ const toolCallDetails = {
587
+ name: null,
588
+ id: null,
589
+ index: null,
590
+ prevIndex: null
591
+ };
586
592
  while (true) {
587
593
  try {
588
594
  const { done, value } = await reader.read();
589
595
  let toolCallName = void 0;
590
596
  let toolCallId = void 0;
591
597
  let toolCallArgs = void 0;
598
+ let toolCallIndex = void 0;
599
+ let toolCallPrevIndex = void 0;
592
600
  let hasToolCall = false;
593
601
  let content = value == null ? void 0 : value.content;
594
602
  if (isAIMessageChunk(value)) {
595
603
  let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
596
- toolCallName = chunk == null ? void 0 : chunk.name;
597
- toolCallId = chunk == null ? void 0 : chunk.id;
598
604
  toolCallArgs = chunk == null ? void 0 : chunk.args;
599
605
  hasToolCall = chunk != void 0;
606
+ if (chunk == null ? void 0 : chunk.name)
607
+ toolCallDetails.name = chunk.name;
608
+ if ((chunk == null ? void 0 : chunk.index) != null) {
609
+ toolCallDetails.index = chunk.index;
610
+ if (toolCallDetails.prevIndex == null)
611
+ toolCallDetails.prevIndex = chunk.index;
612
+ }
613
+ if (chunk == null ? void 0 : chunk.id)
614
+ toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;
615
+ toolCallName = toolCallDetails.name;
616
+ toolCallId = toolCallDetails.id;
617
+ toolCallIndex = toolCallDetails.index;
618
+ toolCallPrevIndex = toolCallDetails.prevIndex;
600
619
  } else if (isBaseMessageChunk(value)) {
601
620
  let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
602
621
  toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
@@ -615,7 +634,7 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
615
634
  break;
616
635
  }
617
636
  if (mode === null) {
618
- if (hasToolCall) {
637
+ if (hasToolCall && toolCallId && toolCallName) {
619
638
  mode = "function";
620
639
  eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
621
640
  } else if (content) {
@@ -626,6 +645,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
626
645
  if (mode === "message" && content) {
627
646
  eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
628
647
  } else if (mode === "function" && toolCallArgs) {
648
+ if (toolCallIndex !== toolCallPrevIndex) {
649
+ eventStream$.sendActionExecutionEnd();
650
+ eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
651
+ toolCallDetails.prevIndex = toolCallDetails.index;
652
+ }
629
653
  eventStream$.sendActionExecutionArgs(toolCallArgs);
630
654
  }
631
655
  } catch (error) {