modelfusion 0.70.0 → 0.72.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.
Files changed (138) hide show
  1. package/README.md +61 -49
  2. package/core/FunctionEvent.d.ts +5 -5
  3. package/guard/guard.cjs +5 -4
  4. package/guard/guard.d.ts +1 -1
  5. package/guard/guard.js +5 -4
  6. package/model-function/ModelCallEvent.d.ts +4 -3
  7. package/model-function/generate-structure/StructureGenerationEvent.d.ts +2 -2
  8. package/model-function/generate-structure/StructureParseError.cjs +10 -0
  9. package/model-function/generate-structure/StructureParseError.d.ts +8 -0
  10. package/model-function/generate-structure/StructureParseError.js +10 -0
  11. package/model-function/generate-structure/index.cjs +0 -3
  12. package/model-function/generate-structure/index.d.ts +0 -3
  13. package/model-function/generate-structure/index.js +0 -3
  14. package/model-function/index.cjs +0 -1
  15. package/model-function/index.d.ts +0 -1
  16. package/model-function/index.js +0 -1
  17. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  18. package/model-provider/openai/chat/OpenAIChatMessage.cjs +16 -25
  19. package/model-provider/openai/chat/OpenAIChatMessage.d.ts +14 -29
  20. package/model-provider/openai/chat/OpenAIChatMessage.js +16 -25
  21. package/model-provider/openai/chat/OpenAIChatModel.cjs +28 -48
  22. package/model-provider/openai/chat/OpenAIChatModel.d.ts +23 -68
  23. package/model-provider/openai/chat/OpenAIChatModel.js +28 -48
  24. package/package.json +2 -2
  25. package/{model-function/generate-tool-call/ToolCallParametersValidationError.js → tool/NoSuchToolDefinitionError.cjs} +9 -8
  26. package/{model-function/generate-tool-call/ToolCallParametersValidationError.d.ts → tool/NoSuchToolDefinitionError.d.ts} +2 -3
  27. package/tool/NoSuchToolDefinitionError.js +37 -0
  28. package/tool/Tool.cjs +5 -16
  29. package/tool/Tool.d.ts +9 -14
  30. package/tool/Tool.js +5 -16
  31. package/tool/ToolCall.d.ts +15 -0
  32. package/tool/ToolCallArgumentsValidationError.cjs +49 -0
  33. package/tool/ToolCallArgumentsValidationError.d.ts +23 -0
  34. package/tool/ToolCallArgumentsValidationError.js +45 -0
  35. package/tool/ToolCallError.cjs +34 -0
  36. package/tool/ToolCallError.d.ts +17 -0
  37. package/tool/ToolCallError.js +30 -0
  38. package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.cjs +4 -4
  39. package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.d.ts +1 -1
  40. package/{model-function/generate-tool-call → tool}/ToolCallGenerationError.js +2 -2
  41. package/{model-function/generate-tool-call/ToolCallParametersValidationError.cjs → tool/ToolCallParseError.cjs} +13 -13
  42. package/tool/ToolCallParseError.d.ts +18 -0
  43. package/tool/ToolCallParseError.js +40 -0
  44. package/tool/ToolCallResult.d.ts +13 -0
  45. package/tool/ToolDefinition.d.ts +7 -0
  46. package/tool/ToolExecutionError.cjs +5 -4
  47. package/tool/ToolExecutionError.d.ts +4 -4
  48. package/tool/ToolExecutionError.js +5 -4
  49. package/tool/WebSearchTool.cjs +4 -4
  50. package/tool/WebSearchTool.d.ts +2 -2
  51. package/tool/WebSearchTool.js +4 -4
  52. package/tool/{ExecuteToolEvent.d.ts → execute-tool/ExecuteToolEvent.d.ts} +1 -1
  53. package/tool/{executeTool.cjs → execute-tool/executeTool.cjs} +15 -15
  54. package/tool/{executeTool.d.ts → execute-tool/executeTool.d.ts} +6 -6
  55. package/tool/{executeTool.js → execute-tool/executeTool.js} +15 -15
  56. package/tool/execute-tool/index.cjs +18 -0
  57. package/tool/execute-tool/index.d.ts +2 -0
  58. package/tool/execute-tool/index.js +2 -0
  59. package/tool/execute-tool/safeExecuteToolCall.cjs +34 -0
  60. package/tool/execute-tool/safeExecuteToolCall.d.ts +5 -0
  61. package/tool/execute-tool/safeExecuteToolCall.js +30 -0
  62. package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +59 -0
  63. package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +35 -0
  64. package/tool/generate-tool-call/TextGenerationToolCallModel.js +55 -0
  65. package/{model-function → tool}/generate-tool-call/ToolCallGenerationEvent.d.ts +1 -1
  66. package/{model-function → tool}/generate-tool-call/ToolCallGenerationModel.d.ts +5 -5
  67. package/{model-function → tool}/generate-tool-call/generateToolCall.cjs +13 -12
  68. package/{model-function → tool}/generate-tool-call/generateToolCall.d.ts +7 -12
  69. package/{model-function → tool}/generate-tool-call/generateToolCall.js +13 -12
  70. package/{model-function → tool}/generate-tool-call/index.cjs +1 -2
  71. package/{model-function → tool}/generate-tool-call/index.d.ts +1 -2
  72. package/{model-function → tool}/generate-tool-call/index.js +1 -2
  73. package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.d.ts +23 -0
  74. package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.cjs +2 -0
  75. package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.d.ts +21 -0
  76. package/tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationModel.js +1 -0
  77. package/tool/generate-tool-calls-or-text/generateToolCallsOrText.cjs +63 -0
  78. package/tool/generate-tool-calls-or-text/generateToolCallsOrText.d.ts +33 -0
  79. package/tool/generate-tool-calls-or-text/generateToolCallsOrText.js +59 -0
  80. package/tool/generate-tool-calls-or-text/index.cjs +19 -0
  81. package/tool/generate-tool-calls-or-text/index.d.ts +3 -0
  82. package/tool/generate-tool-calls-or-text/index.js +3 -0
  83. package/tool/index.cjs +12 -7
  84. package/tool/index.d.ts +12 -7
  85. package/tool/index.js +12 -7
  86. package/tool/use-tool/UseToolEvent.cjs +2 -0
  87. package/tool/{UseToolEvent.d.ts → use-tool/UseToolEvent.d.ts} +1 -1
  88. package/tool/use-tool/UseToolEvent.js +1 -0
  89. package/tool/use-tool/index.cjs +18 -0
  90. package/tool/use-tool/index.d.ts +2 -0
  91. package/tool/use-tool/index.js +2 -0
  92. package/tool/use-tool/useTool.cjs +30 -0
  93. package/tool/use-tool/useTool.d.ts +16 -0
  94. package/tool/use-tool/useTool.js +26 -0
  95. package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.cjs +2 -0
  96. package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.d.ts +7 -0
  97. package/tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js +1 -0
  98. package/tool/use-tools-or-generate-text/index.cjs +18 -0
  99. package/tool/use-tools-or-generate-text/index.d.ts +2 -0
  100. package/tool/use-tools-or-generate-text/index.js +2 -0
  101. package/tool/use-tools-or-generate-text/useToolsOrGenerateText.cjs +48 -0
  102. package/tool/use-tools-or-generate-text/useToolsOrGenerateText.d.ts +18 -0
  103. package/tool/use-tools-or-generate-text/useToolsOrGenerateText.js +44 -0
  104. package/util/getErrorMessage.cjs +3 -0
  105. package/util/getErrorMessage.d.ts +1 -1
  106. package/util/getErrorMessage.js +3 -0
  107. package/model-function/generate-structure/NoSuchStructureError.cjs +0 -17
  108. package/model-function/generate-structure/NoSuchStructureError.d.ts +0 -4
  109. package/model-function/generate-structure/NoSuchStructureError.js +0 -13
  110. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +0 -26
  111. package/model-function/generate-structure/generateStructureOrText.cjs +0 -54
  112. package/model-function/generate-structure/generateStructureOrText.d.ts +0 -97
  113. package/model-function/generate-structure/generateStructureOrText.js +0 -50
  114. package/model-function/generate-tool-call/ToolCallDefinition.d.ts +0 -7
  115. package/tool/NoSuchToolError.cjs +0 -25
  116. package/tool/NoSuchToolError.d.ts +0 -10
  117. package/tool/NoSuchToolError.js +0 -21
  118. package/tool/UseToolOrGenerateTextEvent.d.ts +0 -7
  119. package/tool/useTool.cjs +0 -40
  120. package/tool/useTool.d.ts +0 -15
  121. package/tool/useTool.js +0 -36
  122. package/tool/useToolOrGenerateText.cjs +0 -50
  123. package/tool/useToolOrGenerateText.d.ts +0 -23
  124. package/tool/useToolOrGenerateText.js +0 -46
  125. /package/{model-function/generate-structure/StructureOrTextGenerationModel.cjs → tool/ToolCall.cjs} +0 -0
  126. /package/{model-function/generate-structure/StructureOrTextGenerationModel.js → tool/ToolCall.js} +0 -0
  127. /package/{model-function/generate-tool-call/ToolCallDefinition.cjs → tool/ToolCallResult.cjs} +0 -0
  128. /package/{model-function/generate-tool-call/ToolCallDefinition.js → tool/ToolCallResult.js} +0 -0
  129. /package/{model-function/generate-tool-call/ToolCallGenerationEvent.cjs → tool/ToolDefinition.cjs} +0 -0
  130. /package/{model-function/generate-tool-call/ToolCallGenerationEvent.js → tool/ToolDefinition.js} +0 -0
  131. /package/tool/{ExecuteToolEvent.cjs → execute-tool/ExecuteToolEvent.cjs} +0 -0
  132. /package/tool/{ExecuteToolEvent.js → execute-tool/ExecuteToolEvent.js} +0 -0
  133. /package/{model-function/generate-tool-call/ToolCallGenerationModel.cjs → tool/generate-tool-call/ToolCallGenerationEvent.cjs} +0 -0
  134. /package/{model-function/generate-tool-call/ToolCallGenerationModel.js → tool/generate-tool-call/ToolCallGenerationEvent.js} +0 -0
  135. /package/tool/{UseToolEvent.cjs → generate-tool-call/ToolCallGenerationModel.cjs} +0 -0
  136. /package/tool/{UseToolEvent.js → generate-tool-call/ToolCallGenerationModel.js} +0 -0
  137. /package/tool/{UseToolOrGenerateTextEvent.cjs → generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.cjs} +0 -0
  138. /package/tool/{UseToolOrGenerateTextEvent.js → generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.js} +0 -0
package/README.md CHANGED
@@ -15,7 +15,7 @@
15
15
  **ModelFusion** is a TypeScript library for building AI applications, chatbots, and agents.
16
16
 
17
17
  - **Vendor-neutral**: ModelFusion is a non-commercial open source project that is community-driven. You can use it with any supported provider.
18
- - **Multimodal**: ModelFusion supports a wide range of models including text generation, image generation, vision, text-to-speech, speech-to-text, and embedding models.
18
+ - **Multi-modal**: ModelFusion supports a wide range of models including text generation, image generation, vision, text-to-speech, speech-to-text, and embedding models.
19
19
  - **Streaming**: ModelFusion supports streaming for many generation models, e.g. text streaming, structure streaming, and full duplex speech streaming.
20
20
  - **Utility functions**: ModelFusion provides functionality for tools and tool usage, vector indices, and guards functions.
21
21
  - **Type inference and validation**: ModelFusion infers TypeScript types wherever possible and to validates model responses.
@@ -52,7 +52,7 @@ const text = await generateText(
52
52
  );
53
53
  ```
54
54
 
55
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface)
55
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic)
56
56
 
57
57
  #### streamText
58
58
 
@@ -67,7 +67,7 @@ for await (const textPart of textStream) {
67
67
  }
68
68
  ```
69
69
 
70
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama)
70
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic)
71
71
 
72
72
  #### streamText with multi-modal prompt
73
73
 
@@ -250,39 +250,6 @@ for await (const part of structureStream) {
250
250
 
251
251
  Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
252
252
 
253
- ### [Generate Structure or Text](https://modelfusion.dev/guide/function/generate-structure-or-text)
254
-
255
- Generate a structure (or text as a fallback) using a prompt and multiple schemas.
256
- It either matches one of the schemas or is text reponse.
257
-
258
- ```ts
259
- const { structure, value, text } = await generateStructureOrText(
260
- new OpenAIChatModel({ model: "gpt-3.5-turbo", maxCompletionTokens: 1000 }),
261
- [
262
- new ZodStructureDefinition({
263
- name: "getCurrentWeather" as const, // mark 'as const' for type inference
264
- description: "Get the current weather in a given location",
265
- schema: z.object({
266
- location: z
267
- .string()
268
- .describe("The city and state, e.g. San Francisco, CA"),
269
- unit: z.enum(["celsius", "fahrenheit"]).optional(),
270
- }),
271
- }),
272
- new ZodStructureDefinition({
273
- name: "getContactInformation" as const,
274
- description: "Get the contact information for a given person",
275
- schema: z.object({
276
- name: z.string().describe("The name of the person"),
277
- }),
278
- }),
279
- ],
280
- [OpenAIChatMessage.user(query)]
281
- );
282
- ```
283
-
284
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
285
-
286
253
  ### [Embed Value](https://modelfusion.dev/guide/function/embed)
287
254
 
288
255
  Create embeddings for text and other values. Embeddings are vectors that represent the essence of the values in the context of the model.
@@ -304,7 +271,7 @@ const embeddings = await embedMany(
304
271
  );
305
272
  ```
306
273
 
307
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface)
274
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Ollama](https://modelfusion.dev/integration/model-provider/ollama), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface), [Cohere](https://modelfusion.dev/integration/model-provider/cohere)
308
275
 
309
276
  ### [Tokenize Text](https://modelfusion.dev/guide/function/tokenize-text)
310
277
 
@@ -322,7 +289,7 @@ const tokensAndTokenTexts = await tokenizer.tokenizeWithTexts(text);
322
289
  const reconstructedText = await tokenizer.detokenize(tokens);
323
290
  ```
324
291
 
325
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp)
292
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Cohere](https://modelfusion.dev/integration/model-provider/cohere)
326
293
 
327
294
  ### [Guards](https://modelfusion.dev/guide/guard)
328
295
 
@@ -366,16 +333,16 @@ Tools are functions that can be executed by an AI model. They are useful for bui
366
333
 
367
334
  Predefined tools: [SerpAPI](https://modelfusion.dev/integration/tool/serpapi), [Google Custom Search](https://modelfusion.dev/integration/tool/google-custom-search)
368
335
 
369
- #### Create Tool
336
+ #### [Creating Tools](https://modelfusion.dev/guide/tools/create-tools)
370
337
 
371
- A tool is a function with a name, a description, and a schema for the input parameters.
338
+ A tool is comprised of an async execute function, a name, a description, and a schema for the input parameters.
372
339
 
373
340
  ```ts
374
341
  const calculator = new Tool({
375
342
  name: "calculator",
376
343
  description: "Execute a calculation",
377
344
 
378
- inputSchema: new ZodSchema(
345
+ parameters: new ZodSchema(
379
346
  z.object({
380
347
  a: z.number().describe("The first number."),
381
348
  b: z.number().describe("The second number."),
@@ -402,31 +369,76 @@ const calculator = new Tool({
402
369
  });
403
370
  ```
404
371
 
405
- #### useTool
372
+ #### [generateToolCall](https://modelfusion.dev/guide/tools/generate-tool-call)
406
373
 
407
- The model determines the parameters for the tool from the prompt and then executes it.
374
+ With `generateToolCall`, you can generate a tool call for a specific tool with a language model that supports tools calls (e.g. OpenAI Chat). This function does not execute the tools.
408
375
 
409
376
  ```ts
410
- const { tool, parameters, result } = await useTool(
377
+ const { id, name, args } = await generateToolCall(
411
378
  new OpenAIChatModel({ model: "gpt-3.5-turbo" }),
412
379
  calculator,
413
380
  [OpenAIChatMessage.user("What's fourteen times twelve?")]
414
381
  );
415
382
  ```
416
383
 
417
- #### useToolOrGenerateText
384
+ #### [generateToolCallsOrText](https://modelfusion.dev/guide/tools/generate-tool-calls-or-text)
418
385
 
419
- The model determines which tool to use and its parameters from the prompt and then executes it.
420
- Text is generated as a fallback.
386
+ With `generateToolCallsOrText`, you can ask a language model to generate several tool calls as well as text. The model will choose which tools (if any) should be called with which arguments. Both the text and the tool calls are optional. This function does not execute the tools.
421
387
 
422
388
  ```ts
423
- const { tool, parameters, result, text } = await useToolOrGenerateText(
389
+ const { text, toolCalls } = await generateToolCallsOrText(
424
390
  new OpenAIChatModel({ model: "gpt-3.5-turbo" }),
425
- [calculator /* and other tools... */],
391
+ [toolA, toolB, toolC],
392
+ [OpenAIChatMessage.user(query)]
393
+ );
394
+ ```
395
+
396
+ #### [executeTool](https://modelfusion.dev/guide/tools/execute-tool)
397
+
398
+ You can directly invoke a tool with `executeTool`:
399
+
400
+ ```ts
401
+ const result = await executeTool(calculator, {
402
+ a: 14,
403
+ b: 12,
404
+ operator: "*",
405
+ });
406
+ ```
407
+
408
+ #### [useTool](https://modelfusion.dev/guide/tools/use-tool)
409
+
410
+ With `useTool`, you can use a tool with a language model that supports tools calls (e.g. OpenAI Chat). `useTool` first generates a tool call and then executes the tool with the arguments.
411
+
412
+ ```ts
413
+ const { tool, toolCall, args, ok, result } = await useTool(
414
+ new OpenAIChatModel({ model: "gpt-3.5-turbo" }),
415
+ calculator,
416
+ [OpenAIChatMessage.user("What's fourteen times twelve?")]
417
+ );
418
+
419
+ console.log(`Tool call:`, toolCall);
420
+ console.log(`Tool:`, tool);
421
+ console.log(`Arguments:`, args);
422
+ console.log(`Ok:`, ok);
423
+ console.log(`Result or Error:`, result);
424
+ ```
425
+
426
+ #### [useToolsOrGenerateText](https://modelfusion.dev/guide/tools/use-tools-or-generate-text)
427
+
428
+ With `useToolsOrGenerateText`, you can ask a language model to generate several tool calls as well as text. The model will choose which tools (if any) should be called with which arguments. Both the text and the tool calls are optional. This function executes the tools.
429
+
430
+ ```ts
431
+ const { text, toolResults } = await useToolsOrGenerateText(
432
+ new OpenAIChatModel({ model: "gpt-3.5-turbo" }),
433
+ [calculator /* ... */],
426
434
  [OpenAIChatMessage.user("What's fourteen times twelve?")]
427
435
  );
428
436
  ```
429
437
 
438
+ #### [Agent Loop](https://modelfusion.dev/guide/tools/agent-loop)
439
+
440
+ You can use `useToolsOrGenerateText` to implement an agent loop that responds to user messages and executes tools. [Learn more](https://modelfusion.dev/guide/tools/agent-loop).
441
+
430
442
  ### [Vector Indices](https://modelfusion.dev/guide/vector-index)
431
443
 
432
444
  ```ts
@@ -1,11 +1,11 @@
1
- import { ExecuteFunctionFinishedEvent, ExecuteFunctionStartedEvent } from "./ExecuteFunctionEvent.js";
2
1
  import { GuardFinishedEvent, GuardStartedEvent } from "../guard/GuardEvent.js";
3
2
  import { ModelCallFinishedEvent, ModelCallStartedEvent } from "../model-function/ModelCallEvent.js";
4
3
  import { RetrieveFinishedEvent, RetrieveStartedEvent } from "../retriever/RetrieveEvent.js";
5
- import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/ExecuteToolEvent.js";
6
- import { UseToolFinishedEvent, UseToolStartedEvent } from "../tool/UseToolEvent.js";
7
- import { UseToolOrGenerateTextFinishedEvent, UseToolOrGenerateTextStartedEvent } from "../tool/UseToolOrGenerateTextEvent.js";
4
+ import { ExecuteToolFinishedEvent, ExecuteToolStartedEvent } from "../tool/execute-tool/ExecuteToolEvent.js";
5
+ import { UseToolFinishedEvent, UseToolStartedEvent } from "../tool/use-tool/UseToolEvent.js";
6
+ import { UseToolsOrGenerateTextFinishedEvent, UseToolsOrGenerateTextStartedEvent } from "../tool/use-tools-or-generate-text/UseToolsOrGenerateTextEvent.js";
8
7
  import { UpsertIntoVectorIndexFinishedEvent, UpsertIntoVectorIndexStartedEvent } from "../vector-index/UpsertIntoVectorIndexEvent.js";
8
+ import { ExecuteFunctionFinishedEvent, ExecuteFunctionStartedEvent } from "./ExecuteFunctionEvent.js";
9
9
  export interface BaseFunctionEvent {
10
10
  /**
11
11
  * Unique identifier for the function call.
@@ -82,4 +82,4 @@ export interface BaseFunctionFinishedEvent extends BaseFunctionEvent {
82
82
  */
83
83
  result: BaseFunctionFinishedEventResult;
84
84
  }
85
- export type FunctionEvent = ExecuteFunctionStartedEvent | ExecuteFunctionFinishedEvent | ExecuteToolStartedEvent | ExecuteToolFinishedEvent | GuardStartedEvent | GuardFinishedEvent | ModelCallStartedEvent | ModelCallFinishedEvent | RetrieveStartedEvent | RetrieveFinishedEvent | UpsertIntoVectorIndexStartedEvent | UpsertIntoVectorIndexFinishedEvent | UseToolStartedEvent | UseToolFinishedEvent | UseToolOrGenerateTextStartedEvent | UseToolOrGenerateTextFinishedEvent;
85
+ export type FunctionEvent = ExecuteFunctionStartedEvent | ExecuteFunctionFinishedEvent | ExecuteToolStartedEvent | ExecuteToolFinishedEvent | GuardStartedEvent | GuardFinishedEvent | ModelCallStartedEvent | ModelCallFinishedEvent | RetrieveStartedEvent | RetrieveFinishedEvent | UpsertIntoVectorIndexStartedEvent | UpsertIntoVectorIndexFinishedEvent | UseToolStartedEvent | UseToolFinishedEvent | UseToolsOrGenerateTextStartedEvent | UseToolsOrGenerateTextFinishedEvent;
package/guard/guard.cjs CHANGED
@@ -4,14 +4,14 @@ exports.guard = void 0;
4
4
  const executeFunctionCall_js_1 = require("../core/executeFunctionCall.cjs");
5
5
  async function guard(execute, input, guards, options) {
6
6
  const guardList = Array.isArray(guards) ? guards : [guards];
7
- const maxRetries = options?.maxRetries ?? 1;
7
+ const maxAttempts = options?.maxAttempts ?? 2;
8
8
  return (0, executeFunctionCall_js_1.executeFunctionCall)({
9
9
  options,
10
10
  input,
11
11
  functionType: "guard",
12
12
  execute: async (options) => {
13
13
  let attempts = 0;
14
- while (attempts <= maxRetries) {
14
+ while (attempts < maxAttempts) {
15
15
  let result;
16
16
  try {
17
17
  result = {
@@ -70,8 +70,9 @@ async function guard(execute, input, guards, options) {
70
70
  }
71
71
  attempts++;
72
72
  }
73
- throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
74
- `without producing a valid output or handling an error after ${attempts} attempts.`);
73
+ // TODO dedicated error type
74
+ throw new Error(`Maximum attempts of ${maxAttempts} reached ` +
75
+ `without producing a valid output or handling an error.`);
75
76
  },
76
77
  });
77
78
  }
package/guard/guard.d.ts CHANGED
@@ -24,6 +24,6 @@ export type Guard<INPUT, OUTPUT> = ({ type, input, output, error, }: OutputResul
24
24
  action: "passThrough";
25
25
  } | undefined>;
26
26
  export declare function guard<INPUT, OUTPUT>(execute: (input: INPUT, options?: FunctionOptions) => PromiseLike<OUTPUT>, input: INPUT, guards: Guard<INPUT, OUTPUT> | Array<Guard<INPUT, OUTPUT>>, options?: FunctionOptions & {
27
- maxRetries: number;
27
+ maxAttempts: number;
28
28
  }): Promise<OUTPUT | undefined>;
29
29
  export {};
package/guard/guard.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import { executeFunctionCall } from "../core/executeFunctionCall.js";
2
2
  export async function guard(execute, input, guards, options) {
3
3
  const guardList = Array.isArray(guards) ? guards : [guards];
4
- const maxRetries = options?.maxRetries ?? 1;
4
+ const maxAttempts = options?.maxAttempts ?? 2;
5
5
  return executeFunctionCall({
6
6
  options,
7
7
  input,
8
8
  functionType: "guard",
9
9
  execute: async (options) => {
10
10
  let attempts = 0;
11
- while (attempts <= maxRetries) {
11
+ while (attempts < maxAttempts) {
12
12
  let result;
13
13
  try {
14
14
  result = {
@@ -67,8 +67,9 @@ export async function guard(execute, input, guards, options) {
67
67
  }
68
68
  attempts++;
69
69
  }
70
- throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
71
- `without producing a valid output or handling an error after ${attempts} attempts.`);
70
+ // TODO dedicated error type
71
+ throw new Error(`Maximum attempts of ${maxAttempts} reached ` +
72
+ `without producing a valid output or handling an error.`);
72
73
  },
73
74
  });
74
75
  }
@@ -1,4 +1,6 @@
1
1
  import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
2
+ import { ToolCallGenerationFinishedEvent, ToolCallGenerationStartedEvent } from "../tool/generate-tool-call/ToolCallGenerationEvent.js";
3
+ import { ToolCallsOrTextGenerationFinishedEvent, ToolCallsOrTextGenerationStartedEvent } from "../tool/generate-tool-calls-or-text/ToolCallsOrTextGenerationEvent.js";
2
4
  import { ModelInformation } from "./ModelInformation.js";
3
5
  import { EmbeddingFinishedEvent, EmbeddingStartedEvent } from "./embed/EmbeddingEvent.js";
4
6
  import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
@@ -6,7 +8,6 @@ import { SpeechGenerationFinishedEvent, SpeechGenerationStartedEvent, SpeechStre
6
8
  import { StructureGenerationFinishedEvent, StructureGenerationStartedEvent } from "./generate-structure/StructureGenerationEvent.js";
7
9
  import { StructureStreamingFinishedEvent, StructureStreamingStartedEvent } from "./generate-structure/StructureStreamingEvent.js";
8
10
  import { TextGenerationFinishedEvent, TextGenerationStartedEvent, TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextGenerationEvent.js";
9
- import { ToolCallGenerationFinishedEvent, ToolCallGenerationStartedEvent } from "./generate-tool-call/ToolCallGenerationEvent.js";
10
11
  import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./generate-transcription/TranscriptionEvent.js";
11
12
  export interface BaseModelCallStartedEvent extends BaseFunctionStartedEvent {
12
13
  model: ModelInformation;
@@ -48,5 +49,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
48
49
  */
49
50
  result: BaseModelCallFinishedEventResult;
50
51
  }
51
- export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | TranscriptionStartedEvent;
52
- export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | TranscriptionFinishedEvent;
52
+ export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | ToolCallGenerationStartedEvent | ToolCallsOrTextGenerationStartedEvent | TranscriptionStartedEvent;
53
+ export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | ToolCallGenerationFinishedEvent | ToolCallsOrTextGenerationFinishedEvent | TranscriptionFinishedEvent;
@@ -1,6 +1,6 @@
1
1
  import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
2
2
  export interface StructureGenerationStartedEvent extends BaseModelCallStartedEvent {
3
- functionType: "generate-structure" | "generate-structure-or-text";
3
+ functionType: "generate-structure";
4
4
  }
5
5
  export type StructureGenerationFinishedEventResult = {
6
6
  status: "success";
@@ -18,6 +18,6 @@ export type StructureGenerationFinishedEventResult = {
18
18
  status: "abort";
19
19
  };
20
20
  export type StructureGenerationFinishedEvent = BaseModelCallFinishedEvent & {
21
- functionType: "generate-structure" | "generate-structure-or-text";
21
+ functionType: "generate-structure";
22
22
  result: StructureGenerationFinishedEventResult;
23
23
  };
@@ -30,5 +30,15 @@ class StructureParseError extends Error {
30
30
  this.cause = cause;
31
31
  this.valueText = valueText;
32
32
  }
33
+ toJSON() {
34
+ return {
35
+ name: this.name,
36
+ cause: this.cause,
37
+ message: this.message,
38
+ stack: this.stack,
39
+ structureName: this.structureName,
40
+ valueText: this.valueText,
41
+ };
42
+ }
33
43
  }
34
44
  exports.StructureParseError = StructureParseError;
@@ -7,4 +7,12 @@ export declare class StructureParseError extends Error {
7
7
  valueText: string;
8
8
  cause: unknown;
9
9
  });
10
+ toJSON(): {
11
+ name: string;
12
+ cause: unknown;
13
+ message: string;
14
+ stack: string | undefined;
15
+ structureName: string;
16
+ valueText: string;
17
+ };
10
18
  }
@@ -27,4 +27,14 @@ export class StructureParseError extends Error {
27
27
  this.cause = cause;
28
28
  this.valueText = valueText;
29
29
  }
30
+ toJSON() {
31
+ return {
32
+ name: this.name,
33
+ cause: this.cause,
34
+ message: this.message,
35
+ stack: this.stack,
36
+ structureName: this.structureName,
37
+ valueText: this.valueText,
38
+ };
39
+ }
30
40
  }
@@ -14,14 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./NoSuchStructureError.cjs"), exports);
18
17
  __exportStar(require("./StructureFromTextGenerationModel.cjs"), exports);
19
18
  __exportStar(require("./StructureGenerationEvent.cjs"), exports);
20
19
  __exportStar(require("./StructureGenerationModel.cjs"), exports);
21
- __exportStar(require("./StructureOrTextGenerationModel.cjs"), exports);
22
20
  __exportStar(require("./StructureParseError.cjs"), exports);
23
21
  __exportStar(require("./StructureStreamingEvent.cjs"), exports);
24
22
  __exportStar(require("./StructureValidationError.cjs"), exports);
25
23
  __exportStar(require("./generateStructure.cjs"), exports);
26
- __exportStar(require("./generateStructureOrText.cjs"), exports);
27
24
  __exportStar(require("./streamStructure.cjs"), exports);
@@ -1,11 +1,8 @@
1
- export * from "./NoSuchStructureError.js";
2
1
  export * from "./StructureFromTextGenerationModel.js";
3
2
  export * from "./StructureGenerationEvent.js";
4
3
  export * from "./StructureGenerationModel.js";
5
- export * from "./StructureOrTextGenerationModel.js";
6
4
  export * from "./StructureParseError.js";
7
5
  export * from "./StructureStreamingEvent.js";
8
6
  export * from "./StructureValidationError.js";
9
7
  export * from "./generateStructure.js";
10
- export * from "./generateStructureOrText.js";
11
8
  export * from "./streamStructure.js";
@@ -1,11 +1,8 @@
1
- export * from "./NoSuchStructureError.js";
2
1
  export * from "./StructureFromTextGenerationModel.js";
3
2
  export * from "./StructureGenerationEvent.js";
4
3
  export * from "./StructureGenerationModel.js";
5
- export * from "./StructureOrTextGenerationModel.js";
6
4
  export * from "./StructureParseError.js";
7
5
  export * from "./StructureStreamingEvent.js";
8
6
  export * from "./StructureValidationError.js";
9
7
  export * from "./generateStructure.js";
10
- export * from "./generateStructureOrText.js";
11
8
  export * from "./streamStructure.js";
@@ -31,7 +31,6 @@ __exportStar(require("./generate-image/generateImage.cjs"), exports);
31
31
  __exportStar(require("./generate-speech/index.cjs"), exports);
32
32
  __exportStar(require("./generate-structure/index.cjs"), exports);
33
33
  __exportStar(require("./generate-text/index.cjs"), exports);
34
- __exportStar(require("./generate-tool-call/index.cjs"), exports);
35
34
  __exportStar(require("./generate-transcription/TranscriptionEvent.cjs"), exports);
36
35
  __exportStar(require("./generate-transcription/TranscriptionModel.cjs"), exports);
37
36
  __exportStar(require("./generate-transcription/generateTranscription.cjs"), exports);
@@ -15,7 +15,6 @@ export * from "./generate-image/generateImage.js";
15
15
  export * from "./generate-speech/index.js";
16
16
  export * from "./generate-structure/index.js";
17
17
  export * from "./generate-text/index.js";
18
- export * from "./generate-tool-call/index.js";
19
18
  export * from "./generate-transcription/TranscriptionEvent.js";
20
19
  export * from "./generate-transcription/TranscriptionModel.js";
21
20
  export * from "./generate-transcription/generateTranscription.js";
@@ -15,7 +15,6 @@ export * from "./generate-image/generateImage.js";
15
15
  export * from "./generate-speech/index.js";
16
16
  export * from "./generate-structure/index.js";
17
17
  export * from "./generate-text/index.js";
18
- export * from "./generate-tool-call/index.js";
19
18
  export * from "./generate-transcription/TranscriptionEvent.js";
20
19
  export * from "./generate-transcription/TranscriptionModel.js";
21
20
  export * from "./generate-transcription/generateTranscription.js";
@@ -19,8 +19,8 @@ export declare class Automatic1111ImageGenerationModel extends AbstractModel<Aut
19
19
  get settingsForEvent(): Partial<Automatic1111ImageGenerationSettings>;
20
20
  doGenerateImage(prompt: Automatic1111ImageGenerationPrompt, options?: FunctionOptions): Promise<{
21
21
  response: {
22
- parameters: {};
23
22
  images: string[];
23
+ parameters: {};
24
24
  info: string;
25
25
  };
26
26
  base64Image: string;
@@ -42,12 +42,12 @@ declare const Automatic1111ImageGenerationResponseSchema: z.ZodObject<{
42
42
  parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
43
43
  info: z.ZodString;
44
44
  }, "strip", z.ZodTypeAny, {
45
- parameters: {};
46
45
  images: string[];
46
+ parameters: {};
47
47
  info: string;
48
48
  }, {
49
- parameters: {};
50
49
  images: string[];
50
+ parameters: {};
51
51
  info: string;
52
52
  }>;
53
53
  export type Automatic1111ImageGenerationResponse = z.infer<typeof Automatic1111ImageGenerationResponseSchema>;
@@ -21,40 +21,31 @@ exports.OpenAIChatMessage = {
21
21
  }
22
22
  return { role: "user", content, name: options?.name };
23
23
  },
24
- assistant(content) {
25
- return { role: "assistant", content };
26
- },
27
- functionCall(content, functionCall) {
28
- return {
29
- role: "assistant",
30
- content,
31
- function_call: functionCall,
32
- };
33
- },
34
- functionResult(name, content) {
35
- return { role: "function", name, content };
36
- },
37
24
  /**
38
- * Creates a function call chat message for tool calls.
25
+ * Creates an assistant chat message. The assistant message can optionally contain tool calls.
39
26
  */
40
- toolCall({ text, tool, parameters, }) {
27
+ assistant(content, options) {
41
28
  return {
42
29
  role: "assistant",
43
- content: text,
44
- function_call: {
45
- name: tool,
46
- arguments: JSON.stringify(parameters),
47
- },
30
+ content,
31
+ tool_calls: options?.toolCalls?.map((toolCall) => ({
32
+ id: toolCall.id,
33
+ type: "function",
34
+ function: {
35
+ name: toolCall.name,
36
+ arguments: JSON.stringify(toolCall.args),
37
+ },
38
+ })) ?? undefined,
48
39
  };
49
40
  },
50
41
  /**
51
- * Creates a function result chat message for tool call results.
42
+ * Creates a tool result chat message with the result of a tool call.
52
43
  */
53
- toolResult({ tool, result }) {
44
+ tool({ toolCallId, content, }) {
54
45
  return {
55
- role: "function",
56
- name: tool,
57
- content: JSON.stringify(result),
46
+ role: "tool",
47
+ tool_call_id: toolCallId,
48
+ content: JSON.stringify(content),
58
49
  };
59
50
  },
60
51
  };
@@ -1,3 +1,4 @@
1
+ import { ToolCall } from "../../../tool/ToolCall.js";
1
2
  export type OpenAIChatMessage = {
2
3
  role: "system";
3
4
  content: string;
@@ -9,7 +10,10 @@ export type OpenAIChatMessage = {
9
10
  text: string;
10
11
  } | {
11
12
  type: "image_url";
12
- image_url: string;
13
+ image_url: string | {
14
+ url: string;
15
+ detail: "low" | "high" | "auto";
16
+ };
13
17
  }>;
14
18
  name?: string;
15
19
  } | {
@@ -46,36 +50,17 @@ export declare const OpenAIChatMessage: {
46
50
  mimeType?: string;
47
51
  };
48
52
  }): OpenAIChatMessage;
49
- assistant(content: string): OpenAIChatMessage;
50
- functionCall(content: string | null, functionCall: {
51
- name: string;
52
- arguments: string;
53
- }): OpenAIChatMessage;
54
- functionResult(name: string, content: string): OpenAIChatMessage;
55
53
  /**
56
- * Creates a function call chat message for tool calls.
54
+ * Creates an assistant chat message. The assistant message can optionally contain tool calls.
57
55
  */
58
- toolCall({ text, tool, parameters, }: {
59
- text: string | null;
60
- tool: string;
61
- parameters: unknown;
62
- }): {
63
- role: "assistant";
64
- content: string | null;
65
- function_call: {
66
- name: string;
67
- arguments: string;
68
- };
69
- };
56
+ assistant(content: string | null, options?: {
57
+ toolCalls: Array<ToolCall<string, unknown>> | null | undefined;
58
+ }): OpenAIChatMessage;
70
59
  /**
71
- * Creates a function result chat message for tool call results.
60
+ * Creates a tool result chat message with the result of a tool call.
72
61
  */
73
- toolResult({ tool, result }: {
74
- tool: string;
75
- result: unknown;
76
- }): {
77
- role: "function";
78
- name: string;
79
- content: string;
80
- };
62
+ tool({ toolCallId, content, }: {
63
+ toolCallId: string;
64
+ content: unknown;
65
+ }): OpenAIChatMessage;
81
66
  };
@@ -18,40 +18,31 @@ export const OpenAIChatMessage = {
18
18
  }
19
19
  return { role: "user", content, name: options?.name };
20
20
  },
21
- assistant(content) {
22
- return { role: "assistant", content };
23
- },
24
- functionCall(content, functionCall) {
25
- return {
26
- role: "assistant",
27
- content,
28
- function_call: functionCall,
29
- };
30
- },
31
- functionResult(name, content) {
32
- return { role: "function", name, content };
33
- },
34
21
  /**
35
- * Creates a function call chat message for tool calls.
22
+ * Creates an assistant chat message. The assistant message can optionally contain tool calls.
36
23
  */
37
- toolCall({ text, tool, parameters, }) {
24
+ assistant(content, options) {
38
25
  return {
39
26
  role: "assistant",
40
- content: text,
41
- function_call: {
42
- name: tool,
43
- arguments: JSON.stringify(parameters),
44
- },
27
+ content,
28
+ tool_calls: options?.toolCalls?.map((toolCall) => ({
29
+ id: toolCall.id,
30
+ type: "function",
31
+ function: {
32
+ name: toolCall.name,
33
+ arguments: JSON.stringify(toolCall.args),
34
+ },
35
+ })) ?? undefined,
45
36
  };
46
37
  },
47
38
  /**
48
- * Creates a function result chat message for tool call results.
39
+ * Creates a tool result chat message with the result of a tool call.
49
40
  */
50
- toolResult({ tool, result }) {
41
+ tool({ toolCallId, content, }) {
51
42
  return {
52
- role: "function",
53
- name: tool,
54
- content: JSON.stringify(result),
43
+ role: "tool",
44
+ tool_call_id: toolCallId,
45
+ content: JSON.stringify(content),
55
46
  };
56
47
  },
57
48
  };