llm-exe 2.2.1 → 2.3.0

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/index.mjs CHANGED
@@ -346,12 +346,56 @@ function assert(condition, message) {
346
346
  }
347
347
  }
348
348
 
349
+ // src/utils/guards.ts
350
+ var guards_exports = {};
351
+ __export(guards_exports, {
352
+ hasFunctionCall: () => hasFunctionCall,
353
+ hasToolCall: () => hasToolCall,
354
+ isAssistantMessage: () => isAssistantMessage,
355
+ isFunctionCall: () => isFunctionCall,
356
+ isOutputResult: () => isOutputResult,
357
+ isOutputResultContentText: () => isOutputResultContentText,
358
+ isSystemMessage: () => isSystemMessage,
359
+ isToolCall: () => isToolCall,
360
+ isUserMessage: () => isUserMessage
361
+ });
362
+ function isOutputResult(obj) {
363
+ return !!(obj && typeof obj === "object" && "id" in obj && "stopReason" in obj && "content" in obj && Array.isArray(obj.content));
364
+ }
365
+ function isOutputResultContentText(obj) {
366
+ return !!(obj && typeof obj === "object" && "text" in obj && "type" in obj && obj.type === "text" && typeof obj.text === "string");
367
+ }
368
+ function isFunctionCall(result) {
369
+ return !!(result && typeof result === "object" && "functionId" in result && "type" in result && result.type === "function_use");
370
+ }
371
+ function isToolCall(result) {
372
+ return isFunctionCall(result);
373
+ }
374
+ function hasFunctionCall(results) {
375
+ return !!(results && Array.isArray(results) && results.some(isToolCall));
376
+ }
377
+ function hasToolCall(results) {
378
+ return hasFunctionCall(results);
379
+ }
380
+ function isUserMessage(message) {
381
+ return message.role === "user";
382
+ }
383
+ function isAssistantMessage(message) {
384
+ return message.role === "assistant" || message.role === "model";
385
+ }
386
+ function isSystemMessage(message) {
387
+ return message.role === "system";
388
+ }
389
+
349
390
  // src/parser/parsers/StringParser.ts
350
391
  var StringParser = class extends BaseParser {
351
392
  constructor(options) {
352
393
  super("string", options);
353
394
  }
354
395
  parse(text, _options) {
396
+ if (isOutputResult(text)) {
397
+ return text.content?.[0]?.text ?? "";
398
+ }
355
399
  assert(
356
400
  typeof text === "string",
357
401
  `Invalid input. Expected string. Received ${typeof text}.`
@@ -1661,15 +1705,26 @@ function createCustomParser(name, parserFn) {
1661
1705
  return new CustomParser(name, parserFn);
1662
1706
  }
1663
1707
 
1664
- // src/llm/output/_utils/getResultText.ts
1665
- function getResultText(content) {
1666
- if (content.length === 1 && content.every((a) => a.type === "text")) {
1667
- return content[0]?.text || "";
1668
- }
1669
- return "";
1670
- }
1671
-
1672
1708
  // src/parser/parsers/LlmNativeFunctionParser.ts
1709
+ var LlmFunctionParser = class extends BaseParser {
1710
+ constructor(options) {
1711
+ super("functionCall", options, "function_call");
1712
+ __publicField(this, "parser");
1713
+ this.parser = options.parser;
1714
+ }
1715
+ parse(text, _options) {
1716
+ if (typeof text === "string") {
1717
+ return this.parser.parse(text);
1718
+ }
1719
+ const { content } = text;
1720
+ const functionUses = content?.filter((a) => a.type === "function_use") || [];
1721
+ if (functionUses.length === 0) {
1722
+ const [item] = content;
1723
+ return this.parser.parse(item.text);
1724
+ }
1725
+ return content;
1726
+ }
1727
+ };
1673
1728
  var LlmNativeFunctionParser = class extends BaseParser {
1674
1729
  constructor(options) {
1675
1730
  super("openAiFunction", options, "function_call");
@@ -1677,14 +1732,15 @@ var LlmNativeFunctionParser = class extends BaseParser {
1677
1732
  this.parser = options.parser;
1678
1733
  }
1679
1734
  parse(text, _options) {
1680
- const functionUse = text?.find((a) => a.type === "function_use");
1735
+ const { content } = text;
1736
+ const functionUse = content?.find((a) => a.type === "function_use");
1681
1737
  if (functionUse && "name" in functionUse && "input" in functionUse) {
1682
1738
  return {
1683
1739
  name: functionUse.name,
1684
1740
  arguments: maybeParseJSON(functionUse.input)
1685
1741
  };
1686
1742
  }
1687
- return this.parser.parse(getResultText(text));
1743
+ return this.parser.parse(text?.text ?? text);
1688
1744
  }
1689
1745
  };
1690
1746
  var OpenAiFunctionParser = LlmNativeFunctionParser;
@@ -1744,7 +1800,7 @@ var LlmExecutor = class extends BaseExecutor {
1744
1800
  }
1745
1801
  getHandlerOutput(out, _metadata) {
1746
1802
  if (this.parser.target === "function_call") {
1747
- const outToStr = out.getResultContent();
1803
+ const outToStr = out.getResult();
1748
1804
  return this.parser.parse(outToStr, _metadata);
1749
1805
  } else {
1750
1806
  const outToStr = out.getResultText();
@@ -1769,7 +1825,7 @@ var LlmExecutorWithFunctions = class extends LlmExecutor {
1769
1825
  constructor(llmConfiguration, options) {
1770
1826
  super(
1771
1827
  Object.assign({}, llmConfiguration, {
1772
- parser: new LlmNativeFunctionParser({
1828
+ parser: new LlmFunctionParser({
1773
1829
  parser: llmConfiguration.parser || new StringParser()
1774
1830
  })
1775
1831
  }),
@@ -1780,7 +1836,23 @@ var LlmExecutorWithFunctions = class extends LlmExecutor {
1780
1836
  return super.execute(_input, _options);
1781
1837
  }
1782
1838
  };
1783
- var LlmExecutorOpenAiFunctions = class extends LlmExecutorWithFunctions {
1839
+ var LlmExecutorOpenAiFunctions = class extends LlmExecutor {
1840
+ constructor(llmConfiguration, options) {
1841
+ super(
1842
+ Object.assign({}, llmConfiguration, {
1843
+ parser: new LlmNativeFunctionParser({
1844
+ parser: llmConfiguration.parser || new StringParser()
1845
+ })
1846
+ }),
1847
+ options
1848
+ );
1849
+ console.warn(
1850
+ `LlmExecutorOpenAiFunctions is deprecated. Please migrate to LlmExecutorWithFunctions`
1851
+ );
1852
+ }
1853
+ async execute(_input, _options) {
1854
+ return super.execute(_input, _options);
1855
+ }
1784
1856
  };
1785
1857
 
1786
1858
  // src/executor/_functions.ts
@@ -1985,6 +2057,39 @@ function getEnvironmentVariable(name) {
1985
2057
  }
1986
2058
  }
1987
2059
 
2060
+ // src/llm/config/openai/promptSanitizeMessageCallback.ts
2061
+ function openaiPromptMessageCallback(_message) {
2062
+ let message = { ..._message };
2063
+ if (message.role === "function") {
2064
+ message.role = "tool";
2065
+ message.tool_call_id = message.id;
2066
+ delete message.id;
2067
+ }
2068
+ if (message?.function_call) {
2069
+ const { function_call } = message;
2070
+ const toolsArr = Array.isArray(function_call) ? function_call : [function_call];
2071
+ message.role = "assistant";
2072
+ message.tool_calls = toolsArr.map((call) => {
2073
+ const { id, ...functionCall } = call;
2074
+ return {
2075
+ id,
2076
+ type: "function",
2077
+ function: functionCall
2078
+ };
2079
+ });
2080
+ delete message.function_call;
2081
+ }
2082
+ return message;
2083
+ }
2084
+
2085
+ // src/llm/config/openai/promptSanitize.ts
2086
+ function openaiPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2087
+ if (typeof _messages === "string") {
2088
+ return [{ role: "user", content: _messages }];
2089
+ }
2090
+ return _messages.map(openaiPromptMessageCallback);
2091
+ }
2092
+
1988
2093
  // src/llm/config/openai/index.ts
1989
2094
  var openAiChatV1 = {
1990
2095
  key: "openai.chat.v1",
@@ -2003,12 +2108,7 @@ var openAiChatV1 = {
2003
2108
  mapBody: {
2004
2109
  prompt: {
2005
2110
  key: "messages",
2006
- sanitize: (v) => {
2007
- if (typeof v === "string") {
2008
- return [{ role: "user", content: v }];
2009
- }
2010
- return v;
2011
- }
2111
+ sanitize: openaiPromptSanitize
2012
2112
  },
2013
2113
  model: {
2014
2114
  key: "model"
@@ -2059,14 +2159,54 @@ var openai = {
2059
2159
  "openai.gpt-4o-mini": withDefaultModel(openAiChatV1, "gpt-4o-mini")
2060
2160
  };
2061
2161
 
2162
+ // src/llm/config/anthropic/promptSanitizeMessageCallback.ts
2163
+ function anthropicPromptMessageCallback(_message) {
2164
+ let message = { ..._message };
2165
+ if (message.role === "function") {
2166
+ message.role = "user";
2167
+ message.content = [
2168
+ {
2169
+ type: "tool_result",
2170
+ tool_use_id: message.id,
2171
+ content: maybeStringifyJSON(message.content)
2172
+ }
2173
+ ];
2174
+ delete message.name;
2175
+ delete message.id;
2176
+ }
2177
+ if (message?.function_call) {
2178
+ const { function_call } = message;
2179
+ const toolsArr = Array.isArray(function_call) ? function_call : [function_call];
2180
+ message.role = "assistant";
2181
+ message.content = toolsArr.map((call) => {
2182
+ const { id, name, arguments: input } = call;
2183
+ return {
2184
+ type: "tool_use",
2185
+ id,
2186
+ name,
2187
+ input: maybeParseJSON(input)
2188
+ };
2189
+ });
2190
+ delete message.function_call;
2191
+ }
2192
+ return message;
2193
+ }
2194
+
2062
2195
  // src/llm/config/anthropic/promptSanitize.ts
2063
2196
  function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2064
2197
  if (typeof _messages === "string") {
2065
- return [{ role: "user", content: _messages }];
2198
+ return [{ role: "user", content: _messages }].map(
2199
+ anthropicPromptMessageCallback
2200
+ );
2066
2201
  }
2067
- const [first, ...messages] = [..._messages.map((a) => ({ ...a }))];
2202
+ const [first, ...messages] = [
2203
+ ..._messages.map((a) => ({ ...a }))
2204
+ ];
2068
2205
  if (first.role === "system" && messages.length === 0) {
2069
- return [{ role: "user", content: first.content }, ...messages];
2206
+ return [
2207
+ { role: "user", content: first.content },
2208
+ ...messages
2209
+ ].map(anthropicPromptMessageCallback);
2070
2210
  }
2071
2211
  if (first.role === "system" && messages.length > 0) {
2072
2212
  _outputObj.system = first.content;
@@ -2075,7 +2215,7 @@ function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2075
2215
  return { ...m, role: "user" };
2076
2216
  }
2077
2217
  return m;
2078
- });
2218
+ }).map(anthropicPromptMessageCallback);
2079
2219
  }
2080
2220
  return [
2081
2221
  first,
@@ -2085,7 +2225,7 @@ function anthropicPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2085
2225
  }
2086
2226
  return m;
2087
2227
  })
2088
- ];
2228
+ ].map(anthropicPromptMessageCallback);
2089
2229
  }
2090
2230
 
2091
2231
  // src/llm/config/bedrock/index.ts
@@ -2304,30 +2444,49 @@ var ollama = {
2304
2444
  "ollama.qwq": withDefaultModel(ollamaChatV1, "qwq")
2305
2445
  };
2306
2446
 
2307
- // src/utils/modules/modifyPromptRoleChange.ts
2308
- function modifyPromptRoleChange(messages, roleChanges) {
2309
- const roleChangeMap = new Map(roleChanges.map(({ from, to }) => [from, to]));
2310
- if (Array.isArray(messages)) {
2311
- return messages.map((message) => {
2312
- const newRole2 = roleChangeMap.get(message.role);
2313
- return newRole2 ? { ...message, role: newRole2 } : message;
2314
- });
2315
- }
2316
- const newRole = roleChangeMap.get(messages.role);
2317
- return newRole ? { ...messages, role: newRole } : messages;
2318
- }
2319
-
2320
2447
  // src/llm/config/google/promptSanitizeMessageCallback.ts
2321
2448
  function googleGeminiPromptMessageCallback(_message) {
2322
2449
  let message = { ..._message };
2323
- message = modifyPromptRoleChange(_message, [
2324
- { from: "assistant", to: "model" }
2325
- ]);
2326
- const { role, ...payload } = message;
2327
2450
  const parts = [];
2451
+ if (message.role === "assistant") {
2452
+ message.role = "model";
2453
+ }
2454
+ if (message.role === "system") {
2455
+ message.role = "model";
2456
+ }
2457
+ let { role, ...payload } = message;
2328
2458
  if (typeof payload.content === "string") {
2329
2459
  parts.push({ text: message.content });
2330
2460
  }
2461
+ if (message.role === "function") {
2462
+ role = "user";
2463
+ parts.push({
2464
+ functionResponse: {
2465
+ name: message.name,
2466
+ response: {
2467
+ result: message.content
2468
+ }
2469
+ }
2470
+ });
2471
+ delete message.id;
2472
+ }
2473
+ if (message?.function_call) {
2474
+ const { function_call } = message;
2475
+ const toolsArr = Array.isArray(function_call) ? function_call : [function_call];
2476
+ role = "model";
2477
+ parts.push(
2478
+ ...toolsArr.map((call) => {
2479
+ const { name, arguments: input } = call;
2480
+ return {
2481
+ functionCall: {
2482
+ name,
2483
+ args: maybeParseJSON(input)
2484
+ }
2485
+ };
2486
+ })
2487
+ );
2488
+ delete message.function_call;
2489
+ }
2331
2490
  return {
2332
2491
  role,
2333
2492
  parts
@@ -2357,7 +2516,9 @@ function googleGeminiPromptSanitize(_messages, _inputBodyObj, _outputObj) {
2357
2516
  (message) => message.role !== "system"
2358
2517
  );
2359
2518
  _outputObj.system_instruction = {
2360
- parts: theSystemInstructions.map((message) => ({ text: message.content }))
2519
+ parts: theSystemInstructions.map((message) => ({
2520
+ text: message.content
2521
+ }))
2361
2522
  };
2362
2523
  return withoutSystemInstructions.map(googleGeminiPromptMessageCallback);
2363
2524
  }
@@ -2478,7 +2639,7 @@ function getLlmConfig(provider) {
2478
2639
 
2479
2640
  // src/llm/output/_utils/getResultContent.ts
2480
2641
  function getResultContent(result, index) {
2481
- if (index && index > 0) {
2642
+ if (typeof index === "number" && index > 0) {
2482
2643
  const arr = result?.options || [];
2483
2644
  const val = arr[index];
2484
2645
  return val ? val : [];
@@ -2486,6 +2647,16 @@ function getResultContent(result, index) {
2486
2647
  return [...result.content];
2487
2648
  }
2488
2649
 
2650
+ // src/llm/output/_utils/getResultText.ts
2651
+ function getResultText(result, index) {
2652
+ if (typeof index === "number" && index > 0) {
2653
+ const arr = result?.options || [];
2654
+ const val = arr[index];
2655
+ return isOutputResultContentText(val?.[0]) ? val[0]?.text : "";
2656
+ }
2657
+ return isOutputResultContentText(result?.content?.[0]) ? result.content[0]?.text : "";
2658
+ }
2659
+
2489
2660
  // src/llm/output/base.ts
2490
2661
  function BaseLlmOutput2(result) {
2491
2662
  const __result = Object.freeze({
@@ -2510,7 +2681,7 @@ function BaseLlmOutput2(result) {
2510
2681
  }
2511
2682
  return {
2512
2683
  getResultContent: (index) => getResultContent(__result, index),
2513
- getResultText: () => getResultText(__result.content),
2684
+ getResultText: (index) => getResultText(__result, index),
2514
2685
  getResult
2515
2686
  };
2516
2687
  }
@@ -2545,25 +2716,24 @@ function formatContent(response, handler) {
2545
2716
 
2546
2717
  // src/llm/output/openai.ts
2547
2718
  function formatResult(result) {
2719
+ const out = [];
2548
2720
  if (typeof result?.message?.content === "string") {
2549
- return {
2721
+ out.push({
2550
2722
  type: "text",
2551
2723
  text: result.message.content
2552
- };
2553
- } else if (result?.message && "tool_calls" in result.message) {
2554
- const tool_calls = result.message.tool_calls;
2555
- for (const call of tool_calls) {
2556
- return {
2724
+ });
2725
+ }
2726
+ if (result?.message?.tool_calls) {
2727
+ for (const call of result.message.tool_calls) {
2728
+ out.push({
2729
+ functionId: call.id,
2557
2730
  type: "function_use",
2558
2731
  name: call.function.name,
2559
- input: JSON.parse(call.function.arguments)
2560
- };
2732
+ input: maybeParseJSON(call.function.arguments)
2733
+ });
2561
2734
  }
2562
2735
  }
2563
- return {
2564
- type: "text",
2565
- text: ""
2566
- };
2736
+ return out;
2567
2737
  }
2568
2738
  function OutputOpenAIChat(result, _config) {
2569
2739
  const id = result.id;
@@ -2571,7 +2741,7 @@ function OutputOpenAIChat(result, _config) {
2571
2741
  const created = result.created;
2572
2742
  const [_content, ..._options] = result?.choices || [];
2573
2743
  const stopReason = _content?.finish_reason;
2574
- const content = formatContent(_content, formatResult);
2744
+ const content = formatResult(_content);
2575
2745
  const options = formatOptions(_options, formatResult);
2576
2746
  const usage = {
2577
2747
  output_tokens: result?.usage?.completion_tokens,
@@ -2602,6 +2772,7 @@ function formatResult2(response) {
2602
2772
  });
2603
2773
  } else if (result.type === "tool_use") {
2604
2774
  out.push({
2775
+ functionId: result.id,
2605
2776
  type: "function_use",
2606
2777
  name: result.name,
2607
2778
  input: result.input
@@ -2679,12 +2850,15 @@ function formatResult3(result) {
2679
2850
  };
2680
2851
  } else if (result?.message && "tool_calls" in result.message) {
2681
2852
  const tool_calls = result.message.tool_calls;
2682
- for (const call of tool_calls) {
2683
- return {
2684
- type: "function_use",
2685
- name: call.function.name,
2686
- input: JSON.parse(call.function.arguments)
2687
- };
2853
+ if (tool_calls) {
2854
+ for (const call of tool_calls) {
2855
+ return {
2856
+ functionId: call.id,
2857
+ type: "function_use",
2858
+ name: call.function.name,
2859
+ input: JSON.parse(call.function.arguments)
2860
+ };
2861
+ }
2688
2862
  }
2689
2863
  }
2690
2864
  return {
@@ -2789,37 +2963,36 @@ function OutputOllamaChat(result, _config) {
2789
2963
  }
2790
2964
 
2791
2965
  // src/llm/output/google.gemini/formatResult.ts
2792
- function formatResult4(result) {
2966
+ function formatResult4(result, id) {
2793
2967
  const { parts = [] } = result?.content || {};
2794
- if (parts.length === 1) {
2795
- const answer = parts[0];
2796
- if (!!answer?.functionCall && typeof answer?.functionCall === "object") {
2797
- return {
2798
- type: "function_use",
2799
- name: answer.functionCall.name,
2800
- input: JSON.parse(answer.functionCall.args)
2801
- };
2802
- } else {
2803
- return {
2968
+ const out = [];
2969
+ for (let i = 0; i < parts.length; i++) {
2970
+ const part = parts[i];
2971
+ if (typeof part.text === "string") {
2972
+ out.push({
2804
2973
  type: "text",
2805
- text: answer.text || ""
2806
- };
2974
+ text: part.text
2975
+ });
2976
+ } else if (part?.functionCall) {
2977
+ out.push({
2978
+ functionId: `${id || uuidv4()}-${i}`,
2979
+ type: "function_use",
2980
+ name: part.functionCall.name,
2981
+ input: maybeParseJSON(part.functionCall.args)
2982
+ });
2807
2983
  }
2808
2984
  }
2809
- return {
2810
- type: "text",
2811
- text: ""
2812
- };
2985
+ return out;
2813
2986
  }
2814
2987
 
2815
2988
  // src/llm/output/google.gemini/index.ts
2816
2989
  function OutputGoogleGeminiChat(result, _config) {
2817
- const id = "result.id";
2818
- const name = result.modelVersion;
2990
+ const id = result.responseId;
2991
+ const name = result.modelVersion || _config?.model || "gemini";
2819
2992
  const created = (/* @__PURE__ */ new Date()).getTime();
2820
2993
  const [_content, ..._options] = result?.candidates || [];
2821
2994
  const stopReason = _content?.finishReason?.toLowerCase();
2822
- const content = formatContent(_content, formatResult4);
2995
+ const content = formatResult4(_content, id);
2823
2996
  const options = formatOptions(_options, formatResult4);
2824
2997
  const usage = {
2825
2998
  output_tokens: result?.usageMetadata?.candidatesTokenCount,
@@ -2838,7 +3011,7 @@ function OutputGoogleGeminiChat(result, _config) {
2838
3011
  }
2839
3012
 
2840
3013
  // src/llm/output/index.ts
2841
- function getOutputParser(config, response) {
3014
+ function normalizeLlmOutputToInternalFormat(config, response) {
2842
3015
  switch (config?.key) {
2843
3016
  case "openai.chat.v1":
2844
3017
  case "openai.chat-mock.v1":
@@ -3154,6 +3327,12 @@ async function useLlm_call(state, messages, _options) {
3154
3327
  _options?.functionCall,
3155
3328
  "openai"
3156
3329
  );
3330
+ } else if (state.provider.startsWith("google")) {
3331
+ input["toolConfig"] = {
3332
+ functionCallingConfig: {
3333
+ mode: normalizeFunctionCall(_options?.functionCall, "google")
3334
+ }
3335
+ };
3157
3336
  }
3158
3337
  }
3159
3338
  if (_options && _options?.functions?.length) {
@@ -3181,6 +3360,16 @@ async function useLlm_call(state, messages, _options) {
3181
3360
  )
3182
3361
  };
3183
3362
  });
3363
+ } else if (state.provider.startsWith("google")) {
3364
+ input["tools"] = [
3365
+ {
3366
+ functionDeclarations: _options.functions.map((f) => ({
3367
+ name: f.name,
3368
+ description: f.description,
3369
+ parameters: cleanJsonSchemaFor(f.parameters, "google.chat")
3370
+ }))
3371
+ }
3372
+ ];
3184
3373
  }
3185
3374
  }
3186
3375
  const body = JSON.stringify(input);
@@ -3210,7 +3399,7 @@ async function useLlm_call(state, messages, _options) {
3210
3399
  body,
3211
3400
  headers
3212
3401
  });
3213
- return getOutputParser(state, response);
3402
+ return normalizeLlmOutputToInternalFormat(state, response);
3214
3403
  }
3215
3404
 
3216
3405
  // src/llm/_utils.stateFromOptions.ts
@@ -3782,7 +3971,7 @@ var ChatPrompt = class extends BasePrompt {
3782
3971
  this.parseUserTemplates = options?.allowUnsafeUserTemplate;
3783
3972
  }
3784
3973
  }
3785
- addToPrompt(content, role, name) {
3974
+ addToPrompt(content, role, name, id) {
3786
3975
  if (content) {
3787
3976
  switch (role) {
3788
3977
  case "system":
@@ -3796,11 +3985,11 @@ var ChatPrompt = class extends BasePrompt {
3796
3985
  break;
3797
3986
  case "function":
3798
3987
  assert(name, "Function message requires name");
3799
- this.addFunctionMessage(content, name);
3988
+ this.addFunctionMessage(content, name, id);
3800
3989
  break;
3801
3990
  case "function_call":
3802
3991
  assert(name, "Function message requires name");
3803
- this.addFunctionCallMessage({ name, arguments: content });
3992
+ this.addFunctionCallMessage({ name, arguments: content, id });
3804
3993
  break;
3805
3994
  }
3806
3995
  }
@@ -3840,11 +4029,12 @@ var ChatPrompt = class extends BasePrompt {
3840
4029
  * @param content The message content.
3841
4030
  * @return ChatPrompt so it can be chained.
3842
4031
  */
3843
- addFunctionMessage(content, name) {
4032
+ addFunctionMessage(content, name, id) {
3844
4033
  this.messages.push({
3845
4034
  role: "function",
3846
4035
  name,
3847
- content
4036
+ content,
4037
+ id
3848
4038
  });
3849
4039
  return this;
3850
4040
  }
@@ -3856,8 +4046,9 @@ var ChatPrompt = class extends BasePrompt {
3856
4046
  addFunctionCallMessage(function_call) {
3857
4047
  if (function_call) {
3858
4048
  this.messages.push({
3859
- role: "assistant",
4049
+ role: "function_call",
3860
4050
  function_call: {
4051
+ id: function_call.id,
3861
4052
  name: function_call.name,
3862
4053
  arguments: maybeStringifyJSON(function_call.arguments)
3863
4054
  },
@@ -3879,17 +4070,16 @@ var ChatPrompt = class extends BasePrompt {
3879
4070
  this.addUserMessage(message.content, message?.name);
3880
4071
  break;
3881
4072
  case "assistant":
3882
- if (message.function_call) {
3883
- this.addFunctionCallMessage(message.function_call);
3884
- } else if (message?.content) {
3885
- this.addAssistantMessage(message?.content);
3886
- }
4073
+ this.addAssistantMessage(message?.content);
3887
4074
  break;
3888
4075
  case "system":
3889
4076
  this.addSystemMessage(message.content);
3890
4077
  break;
4078
+ case "function_call":
4079
+ this.addFunctionCallMessage(message.function_call);
4080
+ break;
3891
4081
  case "function":
3892
- this.addFunctionMessage(message.content, message.name);
4082
+ this.addFunctionMessage(message.content, message.name, message.id);
3893
4083
  break;
3894
4084
  }
3895
4085
  }
@@ -3953,20 +4143,19 @@ var ChatPrompt = class extends BasePrompt {
3953
4143
  break;
3954
4144
  }
3955
4145
  case "assistant": {
3956
- if (message.function_call) {
3957
- messagesOut.push({
3958
- role: "assistant",
3959
- content: null,
3960
- function_call: message.function_call
3961
- });
3962
- } else if (message?.content) {
3963
- messagesOut.push({
3964
- role: "assistant",
3965
- content: message.content
3966
- });
3967
- }
4146
+ messagesOut.push({
4147
+ role: "assistant",
4148
+ content: message.content
4149
+ });
3968
4150
  break;
3969
4151
  }
4152
+ case "function_call":
4153
+ messagesOut.push({
4154
+ role: "function_call",
4155
+ content: null,
4156
+ function_call: message.function_call
4157
+ });
4158
+ break;
3970
4159
  case "function":
3971
4160
  messagesOut.push({
3972
4161
  role: "function",
@@ -4145,20 +4334,19 @@ var ChatPrompt = class extends BasePrompt {
4145
4334
  break;
4146
4335
  }
4147
4336
  case "assistant": {
4148
- if (message2.function_call) {
4149
- messagesOut.push({
4150
- role: "assistant",
4151
- content: null,
4152
- function_call: message2.function_call
4153
- });
4154
- } else if (message2?.content) {
4155
- messagesOut.push({
4156
- role: "assistant",
4157
- content: message2.content
4158
- });
4159
- }
4337
+ messagesOut.push({
4338
+ role: "assistant",
4339
+ content: message2.content
4340
+ });
4160
4341
  break;
4161
4342
  }
4343
+ case "function_call":
4344
+ messagesOut.push({
4345
+ role: "function_call",
4346
+ content: null,
4347
+ function_call: message2.function_call
4348
+ });
4349
+ break;
4162
4350
  case "function":
4163
4351
  messagesOut.push({
4164
4352
  role: "function",
@@ -4382,10 +4570,17 @@ var Dialogue = class extends BaseStateItem {
4382
4570
  }
4383
4571
  setAssistantMessage(content) {
4384
4572
  if (content) {
4385
- this.value.push({
4386
- role: "assistant",
4387
- content
4388
- });
4573
+ if (isOutputResultContentText(content)) {
4574
+ this.value.push({
4575
+ role: "assistant",
4576
+ content: content.text
4577
+ });
4578
+ } else {
4579
+ this.value.push({
4580
+ role: "assistant",
4581
+ content
4582
+ });
4583
+ }
4389
4584
  }
4390
4585
  return this;
4391
4586
  }
@@ -4398,28 +4593,57 @@ var Dialogue = class extends BaseStateItem {
4398
4593
  }
4399
4594
  return this;
4400
4595
  }
4401
- setFunctionMessage(content, name) {
4596
+ setToolMessage(content, name, id) {
4597
+ this.setFunctionMessage(content, name, id);
4598
+ }
4599
+ setFunctionMessage(content, name, id) {
4402
4600
  if (content) {
4403
4601
  this.value.push({
4404
4602
  role: "function",
4603
+ id,
4405
4604
  name,
4406
4605
  content
4407
4606
  });
4408
4607
  }
4409
4608
  return this;
4410
4609
  }
4610
+ /**
4611
+ * Set
4612
+ */
4613
+ setToolCallMessage(input) {
4614
+ this.setFunctionCallMessage(input);
4615
+ }
4411
4616
  setFunctionCallMessage(input) {
4412
- if (!input?.function_call) {
4617
+ if (!input || typeof input !== "object") {
4413
4618
  throw new LlmExeError(`Invalid arguments`, "state", {
4414
- error: `Invalid arguments: missing required function_call`,
4619
+ error: `Invalid arguments: input must be an object`,
4415
4620
  module: "dialogue"
4416
4621
  });
4417
4622
  }
4623
+ if ("function_call" in input) {
4624
+ if (!input.function_call || typeof input.function_call !== "object") {
4625
+ throw new LlmExeError(`Invalid arguments`, "state", {
4626
+ error: `Invalid arguments: input must be an object`,
4627
+ module: "dialogue"
4628
+ });
4629
+ }
4630
+ this.value.push({
4631
+ role: "function_call",
4632
+ function_call: {
4633
+ id: input?.function_call.id,
4634
+ name: input?.function_call.name,
4635
+ arguments: maybeStringifyJSON(input?.function_call.arguments)
4636
+ },
4637
+ content: null
4638
+ });
4639
+ return this;
4640
+ }
4418
4641
  this.value.push({
4419
- role: "assistant",
4642
+ role: "function_call",
4420
4643
  function_call: {
4421
- name: input?.function_call.name,
4422
- arguments: maybeStringifyJSON(input?.function_call.arguments)
4644
+ id: input?.id,
4645
+ name: input?.name,
4646
+ arguments: maybeStringifyJSON(input?.arguments)
4423
4647
  },
4424
4648
  content: null
4425
4649
  });
@@ -4437,21 +4661,26 @@ var Dialogue = class extends BaseStateItem {
4437
4661
  case "user":
4438
4662
  this.setUserMessage(message?.content, message?.name);
4439
4663
  break;
4664
+ case "model":
4440
4665
  case "assistant":
4441
- if (message.function_call) {
4442
- this.setFunctionCallMessage({
4443
- function_call: message.function_call
4444
- });
4445
- } else if (message?.content) {
4446
- this.setAssistantMessage(message?.content);
4447
- }
4666
+ this.setAssistantMessage(message?.content);
4448
4667
  break;
4449
4668
  case "system":
4450
4669
  this.setSystemMessage(message?.content);
4451
4670
  break;
4671
+ case "function_call":
4672
+ this.setFunctionCallMessage({
4673
+ function_call: message.function_call
4674
+ });
4675
+ break;
4452
4676
  case "function":
4453
4677
  this.setFunctionMessage(message?.content, message.name);
4454
4678
  break;
4679
+ default:
4680
+ this.value.push({
4681
+ role: message.role,
4682
+ content: message.content || ""
4683
+ });
4455
4684
  }
4456
4685
  }
4457
4686
  return this;
@@ -4467,6 +4696,30 @@ var Dialogue = class extends BaseStateItem {
4467
4696
  };
4468
4697
  }
4469
4698
  // deserialize() {}
4699
+ /**
4700
+ * Add LLM output to dialogue history in the order it was returned
4701
+ *
4702
+ * @param output - The LLM output result from llm.call()
4703
+ * @returns this for chaining
4704
+ */
4705
+ addFromOutput(output) {
4706
+ const result = "getResult" in output ? output.getResult() : output;
4707
+ if (!result || typeof result !== "object") {
4708
+ return this;
4709
+ }
4710
+ for (const item of result.content) {
4711
+ if (item.type === "text") {
4712
+ this.setAssistantMessage(item.text);
4713
+ } else if (item.type === "function_use") {
4714
+ this.setToolCallMessage({
4715
+ name: item.name,
4716
+ arguments: maybeStringifyJSON(item.input),
4717
+ id: item.functionId
4718
+ });
4719
+ }
4720
+ }
4721
+ return this;
4722
+ }
4470
4723
  };
4471
4724
 
4472
4725
  // src/state/_base.ts
@@ -4590,6 +4843,7 @@ export {
4590
4843
  createState,
4591
4844
  createStateItem,
4592
4845
  defineSchema,
4846
+ guards_exports as guards,
4593
4847
  registerHelpers,
4594
4848
  registerPartials,
4595
4849
  useExecutors,