modelfusion 0.104.0 → 0.106.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 (292) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +8 -10
  3. package/core/DefaultRun.cjs +0 -4
  4. package/core/DefaultRun.d.ts +0 -2
  5. package/core/DefaultRun.js +0 -4
  6. package/core/ExtensionFunctionEvent.d.ts +11 -0
  7. package/core/FunctionEvent.d.ts +2 -2
  8. package/extension/index.cjs +22 -3
  9. package/extension/index.d.ts +5 -1
  10. package/extension/index.js +4 -1
  11. package/index.cjs +0 -3
  12. package/index.d.ts +0 -3
  13. package/index.js +0 -3
  14. package/model-function/Delta.d.ts +1 -2
  15. package/model-function/executeStreamCall.cjs +6 -4
  16. package/model-function/executeStreamCall.d.ts +2 -2
  17. package/model-function/executeStreamCall.js +6 -4
  18. package/model-function/generate-speech/streamSpeech.cjs +1 -2
  19. package/model-function/generate-speech/streamSpeech.js +1 -2
  20. package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +25 -29
  21. package/model-function/generate-structure/StructureFromTextStreamingModel.d.ts +3 -1
  22. package/model-function/generate-structure/StructureFromTextStreamingModel.js +25 -29
  23. package/model-function/generate-structure/StructureGenerationModel.d.ts +2 -0
  24. package/model-function/generate-structure/jsonStructurePrompt.cjs +42 -6
  25. package/model-function/generate-structure/jsonStructurePrompt.d.ts +12 -1
  26. package/model-function/generate-structure/jsonStructurePrompt.js +42 -5
  27. package/model-function/generate-structure/streamStructure.cjs +7 -8
  28. package/model-function/generate-structure/streamStructure.d.ts +1 -1
  29. package/model-function/generate-structure/streamStructure.js +7 -8
  30. package/model-function/generate-text/PromptTemplateFullTextModel.cjs +35 -0
  31. package/model-function/generate-text/PromptTemplateFullTextModel.d.ts +41 -0
  32. package/model-function/generate-text/PromptTemplateFullTextModel.js +31 -0
  33. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +2 -1
  34. package/model-function/generate-text/PromptTemplateTextGenerationModel.js +1 -1
  35. package/model-function/generate-text/PromptTemplateTextStreamingModel.cjs +3 -0
  36. package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +2 -1
  37. package/model-function/generate-text/PromptTemplateTextStreamingModel.js +3 -0
  38. package/model-function/generate-text/TextGenerationModel.d.ts +2 -1
  39. package/model-function/generate-text/index.cjs +1 -0
  40. package/model-function/generate-text/index.d.ts +1 -0
  41. package/model-function/generate-text/index.js +1 -0
  42. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.cjs +2 -2
  43. package/model-function/generate-text/prompt-template/AlpacaPromptTemplate.js +1 -1
  44. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.cjs +8 -5
  45. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.js +7 -4
  46. package/model-function/generate-text/prompt-template/ChatPrompt.cjs +42 -0
  47. package/model-function/generate-text/prompt-template/ChatPrompt.d.ts +27 -5
  48. package/model-function/generate-text/prompt-template/ChatPrompt.js +41 -1
  49. package/model-function/generate-text/prompt-template/{Content.cjs → ContentPart.cjs} +1 -1
  50. package/model-function/generate-text/prompt-template/ContentPart.d.ts +30 -0
  51. package/model-function/generate-text/prompt-template/{Content.js → ContentPart.js} +1 -1
  52. package/model-function/generate-text/prompt-template/InstructionPrompt.d.ts +3 -2
  53. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +8 -5
  54. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.d.ts +1 -1
  55. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +6 -3
  56. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.cjs +8 -4
  57. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.js +6 -2
  58. package/model-function/generate-text/prompt-template/TextPromptTemplate.cjs +8 -4
  59. package/model-function/generate-text/prompt-template/TextPromptTemplate.js +6 -2
  60. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.cjs +7 -3
  61. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.js +6 -2
  62. package/model-function/generate-text/prompt-template/index.cjs +1 -1
  63. package/model-function/generate-text/prompt-template/index.d.ts +1 -1
  64. package/model-function/generate-text/prompt-template/index.js +1 -1
  65. package/model-function/generate-text/streamText.cjs +27 -28
  66. package/model-function/generate-text/streamText.d.ts +1 -0
  67. package/model-function/generate-text/streamText.js +27 -28
  68. package/model-function/index.cjs +0 -1
  69. package/model-function/index.d.ts +0 -1
  70. package/model-function/index.js +0 -1
  71. package/model-provider/anthropic/AnthropicPromptTemplate.cjs +7 -3
  72. package/model-provider/anthropic/AnthropicPromptTemplate.js +5 -1
  73. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +8 -14
  74. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +11 -2
  75. package/model-provider/anthropic/AnthropicTextGenerationModel.js +8 -14
  76. package/model-provider/anthropic/AnthropicTextGenerationModel.test.cjs +44 -0
  77. package/model-provider/anthropic/AnthropicTextGenerationModel.test.js +42 -0
  78. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  79. package/model-provider/cohere/CohereTextGenerationModel.cjs +6 -44
  80. package/model-provider/cohere/CohereTextGenerationModel.d.ts +49 -15
  81. package/model-provider/cohere/CohereTextGenerationModel.js +7 -45
  82. package/model-provider/cohere/CohereTextGenerationModel.test.cjs +33 -0
  83. package/model-provider/cohere/CohereTextGenerationModel.test.js +31 -0
  84. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +1 -2
  85. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +1 -2
  86. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.cjs +6 -1
  87. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.js +6 -1
  88. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +7 -14
  89. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +171 -20
  90. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +8 -15
  91. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.cjs +37 -0
  92. package/model-provider/llamacpp/LlamaCppTextGenerationModel.test.js +35 -0
  93. package/model-provider/mistral/MistralChatModel.cjs +30 -104
  94. package/model-provider/mistral/MistralChatModel.d.ts +47 -14
  95. package/model-provider/mistral/MistralChatModel.js +30 -104
  96. package/model-provider/mistral/MistralChatModel.test.cjs +51 -0
  97. package/model-provider/mistral/MistralChatModel.test.js +49 -0
  98. package/model-provider/mistral/MistralPromptTemplate.cjs +11 -4
  99. package/model-provider/mistral/MistralPromptTemplate.js +9 -2
  100. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +13 -13
  101. package/model-provider/ollama/OllamaChatModel.cjs +7 -43
  102. package/model-provider/ollama/OllamaChatModel.d.ts +67 -14
  103. package/model-provider/ollama/OllamaChatModel.js +8 -44
  104. package/model-provider/ollama/OllamaChatModel.test.cjs +27 -0
  105. package/model-provider/ollama/OllamaChatModel.test.js +25 -0
  106. package/model-provider/ollama/OllamaChatPromptTemplate.cjs +34 -4
  107. package/model-provider/ollama/OllamaChatPromptTemplate.js +34 -4
  108. package/model-provider/ollama/OllamaCompletionModel.cjs +22 -43
  109. package/model-provider/ollama/OllamaCompletionModel.d.ts +67 -10
  110. package/model-provider/ollama/OllamaCompletionModel.js +24 -45
  111. package/model-provider/ollama/OllamaCompletionModel.test.cjs +95 -13
  112. package/model-provider/ollama/OllamaCompletionModel.test.js +72 -13
  113. package/model-provider/openai/{chat/AbstractOpenAIChatModel.cjs → AbstractOpenAIChatModel.cjs} +71 -15
  114. package/model-provider/openai/{chat/AbstractOpenAIChatModel.d.ts → AbstractOpenAIChatModel.d.ts} +273 -19
  115. package/model-provider/openai/{chat/AbstractOpenAIChatModel.js → AbstractOpenAIChatModel.js} +71 -15
  116. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.cjs → OpenAIChatFunctionCallStructureGenerationModel.cjs} +18 -2
  117. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts → OpenAIChatFunctionCallStructureGenerationModel.d.ts} +41 -11
  118. package/model-provider/openai/{chat/OpenAIChatFunctionCallStructureGenerationModel.js → OpenAIChatFunctionCallStructureGenerationModel.js} +18 -2
  119. package/model-provider/openai/{chat/OpenAIChatMessage.d.ts → OpenAIChatMessage.d.ts} +3 -3
  120. package/model-provider/openai/{chat/OpenAIChatModel.cjs → OpenAIChatModel.cjs} +5 -5
  121. package/model-provider/openai/{chat/OpenAIChatModel.d.ts → OpenAIChatModel.d.ts} +12 -12
  122. package/model-provider/openai/{chat/OpenAIChatModel.js → OpenAIChatModel.js} +5 -5
  123. package/model-provider/openai/OpenAIChatModel.test.cjs +94 -0
  124. package/model-provider/openai/OpenAIChatModel.test.js +92 -0
  125. package/model-provider/openai/OpenAIChatPromptTemplate.cjs +114 -0
  126. package/model-provider/openai/{chat/OpenAIChatPromptTemplate.d.ts → OpenAIChatPromptTemplate.d.ts} +3 -3
  127. package/model-provider/openai/OpenAIChatPromptTemplate.js +107 -0
  128. package/model-provider/openai/OpenAICompletionModel.cjs +32 -84
  129. package/model-provider/openai/OpenAICompletionModel.d.ts +27 -10
  130. package/model-provider/openai/OpenAICompletionModel.js +33 -85
  131. package/model-provider/openai/OpenAICompletionModel.test.cjs +53 -0
  132. package/model-provider/openai/OpenAICompletionModel.test.js +51 -0
  133. package/model-provider/openai/OpenAIFacade.cjs +2 -2
  134. package/model-provider/openai/OpenAIFacade.d.ts +3 -3
  135. package/model-provider/openai/OpenAIFacade.js +2 -2
  136. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -12
  137. package/model-provider/openai/OpenAITranscriptionModel.d.ts +17 -17
  138. package/model-provider/openai/TikTokenTokenizer.d.ts +1 -1
  139. package/model-provider/openai/{chat/countOpenAIChatMessageTokens.cjs → countOpenAIChatMessageTokens.cjs} +2 -2
  140. package/model-provider/openai/{chat/countOpenAIChatMessageTokens.js → countOpenAIChatMessageTokens.js} +2 -2
  141. package/model-provider/openai/index.cjs +6 -7
  142. package/model-provider/openai/index.d.ts +5 -7
  143. package/model-provider/openai/index.js +5 -6
  144. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +4 -4
  145. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +6 -6
  146. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +4 -4
  147. package/model-provider/stability/StabilityImageGenerationModel.d.ts +5 -5
  148. package/package.json +13 -24
  149. package/test/JsonTestServer.cjs +33 -0
  150. package/test/JsonTestServer.d.ts +7 -0
  151. package/test/JsonTestServer.js +29 -0
  152. package/test/StreamingTestServer.cjs +55 -0
  153. package/test/StreamingTestServer.d.ts +7 -0
  154. package/test/StreamingTestServer.js +51 -0
  155. package/test/arrayFromAsync.cjs +13 -0
  156. package/test/arrayFromAsync.d.ts +1 -0
  157. package/test/arrayFromAsync.js +9 -0
  158. package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +1 -1
  159. package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +1 -1
  160. package/tool/generate-tool-call/TextGenerationToolCallModel.js +1 -1
  161. package/tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.d.ts +1 -11
  162. package/tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.d.ts +12 -0
  163. package/tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.js +1 -0
  164. package/tool/generate-tool-calls-or-text/index.cjs +1 -0
  165. package/tool/generate-tool-calls-or-text/index.d.ts +1 -0
  166. package/tool/generate-tool-calls-or-text/index.js +1 -0
  167. package/util/index.cjs +0 -1
  168. package/util/index.d.ts +0 -1
  169. package/util/index.js +0 -1
  170. package/util/streaming/createEventSourceResponseHandler.cjs +9 -0
  171. package/util/streaming/createEventSourceResponseHandler.d.ts +4 -0
  172. package/util/streaming/createEventSourceResponseHandler.js +5 -0
  173. package/util/streaming/createJsonStreamResponseHandler.cjs +9 -0
  174. package/util/streaming/createJsonStreamResponseHandler.d.ts +4 -0
  175. package/util/streaming/createJsonStreamResponseHandler.js +5 -0
  176. package/util/streaming/parseEventSourceStreamAsAsyncIterable.cjs +52 -0
  177. package/util/streaming/parseEventSourceStreamAsAsyncIterable.d.ts +6 -0
  178. package/util/streaming/parseEventSourceStreamAsAsyncIterable.js +48 -0
  179. package/util/streaming/parseJsonStreamAsAsyncIterable.cjs +21 -0
  180. package/util/streaming/parseJsonStreamAsAsyncIterable.d.ts +6 -0
  181. package/util/streaming/parseJsonStreamAsAsyncIterable.js +17 -0
  182. package/browser/MediaSourceAppender.cjs +0 -54
  183. package/browser/MediaSourceAppender.d.ts +0 -11
  184. package/browser/MediaSourceAppender.js +0 -50
  185. package/browser/convertAudioChunksToBase64.cjs +0 -8
  186. package/browser/convertAudioChunksToBase64.d.ts +0 -4
  187. package/browser/convertAudioChunksToBase64.js +0 -4
  188. package/browser/convertBlobToBase64.cjs +0 -23
  189. package/browser/convertBlobToBase64.d.ts +0 -1
  190. package/browser/convertBlobToBase64.js +0 -19
  191. package/browser/index.cjs +0 -22
  192. package/browser/index.d.ts +0 -6
  193. package/browser/index.js +0 -6
  194. package/browser/invokeFlow.cjs +0 -23
  195. package/browser/invokeFlow.d.ts +0 -8
  196. package/browser/invokeFlow.js +0 -19
  197. package/browser/readEventSource.cjs +0 -29
  198. package/browser/readEventSource.d.ts +0 -9
  199. package/browser/readEventSource.js +0 -25
  200. package/browser/readEventSourceStream.cjs +0 -35
  201. package/browser/readEventSourceStream.d.ts +0 -7
  202. package/browser/readEventSourceStream.js +0 -31
  203. package/composed-function/index.cjs +0 -19
  204. package/composed-function/index.d.ts +0 -3
  205. package/composed-function/index.js +0 -3
  206. package/composed-function/summarize/SummarizationFunction.d.ts +0 -4
  207. package/composed-function/summarize/summarizeRecursively.cjs +0 -19
  208. package/composed-function/summarize/summarizeRecursively.d.ts +0 -11
  209. package/composed-function/summarize/summarizeRecursively.js +0 -15
  210. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +0 -25
  211. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +0 -24
  212. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +0 -21
  213. package/cost/Cost.cjs +0 -38
  214. package/cost/Cost.d.ts +0 -16
  215. package/cost/Cost.js +0 -34
  216. package/cost/CostCalculator.d.ts +0 -8
  217. package/cost/calculateCost.cjs +0 -28
  218. package/cost/calculateCost.d.ts +0 -7
  219. package/cost/calculateCost.js +0 -24
  220. package/cost/index.cjs +0 -19
  221. package/cost/index.d.ts +0 -3
  222. package/cost/index.js +0 -3
  223. package/guard/GuardEvent.cjs +0 -2
  224. package/guard/GuardEvent.d.ts +0 -7
  225. package/guard/fixStructure.cjs +0 -75
  226. package/guard/fixStructure.d.ts +0 -64
  227. package/guard/fixStructure.js +0 -71
  228. package/guard/guard.cjs +0 -79
  229. package/guard/guard.d.ts +0 -29
  230. package/guard/guard.js +0 -75
  231. package/guard/index.cjs +0 -19
  232. package/guard/index.d.ts +0 -3
  233. package/guard/index.js +0 -3
  234. package/model-function/SuccessfulModelCall.cjs +0 -10
  235. package/model-function/SuccessfulModelCall.d.ts +0 -12
  236. package/model-function/SuccessfulModelCall.js +0 -6
  237. package/model-function/generate-text/prompt-template/Content.d.ts +0 -25
  238. package/model-provider/openai/OpenAICostCalculator.cjs +0 -89
  239. package/model-provider/openai/OpenAICostCalculator.d.ts +0 -6
  240. package/model-provider/openai/OpenAICostCalculator.js +0 -85
  241. package/model-provider/openai/chat/OpenAIChatModel.test.cjs +0 -61
  242. package/model-provider/openai/chat/OpenAIChatModel.test.js +0 -59
  243. package/model-provider/openai/chat/OpenAIChatPromptTemplate.cjs +0 -70
  244. package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +0 -63
  245. package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +0 -156
  246. package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +0 -19
  247. package/model-provider/openai/chat/OpenAIChatStreamIterable.js +0 -152
  248. package/server/fastify/AssetStorage.cjs +0 -2
  249. package/server/fastify/AssetStorage.d.ts +0 -17
  250. package/server/fastify/DefaultFlow.cjs +0 -22
  251. package/server/fastify/DefaultFlow.d.ts +0 -16
  252. package/server/fastify/DefaultFlow.js +0 -18
  253. package/server/fastify/FileSystemAssetStorage.cjs +0 -60
  254. package/server/fastify/FileSystemAssetStorage.d.ts +0 -19
  255. package/server/fastify/FileSystemAssetStorage.js +0 -56
  256. package/server/fastify/FileSystemLogger.cjs +0 -49
  257. package/server/fastify/FileSystemLogger.d.ts +0 -18
  258. package/server/fastify/FileSystemLogger.js +0 -45
  259. package/server/fastify/Flow.cjs +0 -2
  260. package/server/fastify/Flow.d.ts +0 -9
  261. package/server/fastify/FlowRun.cjs +0 -71
  262. package/server/fastify/FlowRun.d.ts +0 -28
  263. package/server/fastify/FlowRun.js +0 -67
  264. package/server/fastify/FlowSchema.cjs +0 -2
  265. package/server/fastify/FlowSchema.d.ts +0 -5
  266. package/server/fastify/Logger.cjs +0 -2
  267. package/server/fastify/Logger.d.ts +0 -13
  268. package/server/fastify/PathProvider.cjs +0 -34
  269. package/server/fastify/PathProvider.d.ts +0 -12
  270. package/server/fastify/PathProvider.js +0 -30
  271. package/server/fastify/index.cjs +0 -24
  272. package/server/fastify/index.d.ts +0 -8
  273. package/server/fastify/index.js +0 -8
  274. package/server/fastify/modelFusionFlowPlugin.cjs +0 -103
  275. package/server/fastify/modelFusionFlowPlugin.d.ts +0 -12
  276. package/server/fastify/modelFusionFlowPlugin.js +0 -99
  277. package/util/getAudioFileExtension.cjs +0 -29
  278. package/util/getAudioFileExtension.d.ts +0 -1
  279. package/util/getAudioFileExtension.js +0 -25
  280. /package/{composed-function/summarize/SummarizationFunction.cjs → core/ExtensionFunctionEvent.cjs} +0 -0
  281. /package/{composed-function/summarize/SummarizationFunction.js → core/ExtensionFunctionEvent.js} +0 -0
  282. /package/{cost/CostCalculator.js → model-provider/anthropic/AnthropicTextGenerationModel.test.d.ts} +0 -0
  283. /package/{guard/GuardEvent.js → model-provider/cohere/CohereTextGenerationModel.test.d.ts} +0 -0
  284. /package/model-provider/{openai/chat/OpenAIChatModel.test.d.ts → llamacpp/LlamaCppTextGenerationModel.test.d.ts} +0 -0
  285. /package/{server/fastify/AssetStorage.js → model-provider/mistral/MistralChatModel.test.d.ts} +0 -0
  286. /package/{server/fastify/Flow.js → model-provider/ollama/OllamaChatModel.test.d.ts} +0 -0
  287. /package/model-provider/openai/{chat/OpenAIChatMessage.cjs → OpenAIChatMessage.cjs} +0 -0
  288. /package/model-provider/openai/{chat/OpenAIChatMessage.js → OpenAIChatMessage.js} +0 -0
  289. /package/{server/fastify/FlowSchema.js → model-provider/openai/OpenAIChatModel.test.d.ts} +0 -0
  290. /package/{server/fastify/Logger.js → model-provider/openai/OpenAICompletionModel.test.d.ts} +0 -0
  291. /package/model-provider/openai/{chat/countOpenAIChatMessageTokens.d.ts → countOpenAIChatMessageTokens.d.ts} +0 -0
  292. /package/{cost/CostCalculator.cjs → tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.cjs} +0 -0
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readEventSourceStream = void 0;
4
- const parseJSON_js_1 = require("../core/schema/parseJSON.cjs");
5
- const AsyncQueue_js_1 = require("../util/AsyncQueue.cjs");
6
- const parseEventSourceStream_js_1 = require("../util/streaming/parseEventSourceStream.cjs");
7
- function readEventSourceStream({ stream, schema, errorHandler, }) {
8
- const queue = new AsyncQueue_js_1.AsyncQueue();
9
- // run async (no await on purpose):
10
- (0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
11
- .then(async (events) => {
12
- try {
13
- for await (const event of events) {
14
- const validationResult = (0, parseJSON_js_1.safeParseJSON)({ text: event.data, schema });
15
- if (!validationResult.success) {
16
- errorHandler?.(validationResult.error);
17
- continue;
18
- }
19
- queue.push(validationResult.data);
20
- }
21
- }
22
- catch (error) {
23
- errorHandler?.(error);
24
- }
25
- finally {
26
- queue.close();
27
- }
28
- })
29
- .catch((error) => {
30
- errorHandler?.(error);
31
- queue.close();
32
- });
33
- return queue;
34
- }
35
- exports.readEventSourceStream = readEventSourceStream;
@@ -1,7 +0,0 @@
1
- import { Schema } from "../core/schema/Schema.js";
2
- import { ErrorHandler } from "../util/ErrorHandler.js";
3
- export declare function readEventSourceStream<T>({ stream, schema, errorHandler, }: {
4
- stream: ReadableStream<Uint8Array>;
5
- schema: Schema<T>;
6
- errorHandler?: ErrorHandler;
7
- }): AsyncIterable<T>;
@@ -1,31 +0,0 @@
1
- import { safeParseJSON } from "../core/schema/parseJSON.js";
2
- import { AsyncQueue } from "../util/AsyncQueue.js";
3
- import { parseEventSourceStream } from "../util/streaming/parseEventSourceStream.js";
4
- export function readEventSourceStream({ stream, schema, errorHandler, }) {
5
- const queue = new AsyncQueue();
6
- // run async (no await on purpose):
7
- parseEventSourceStream({ stream })
8
- .then(async (events) => {
9
- try {
10
- for await (const event of events) {
11
- const validationResult = safeParseJSON({ text: event.data, schema });
12
- if (!validationResult.success) {
13
- errorHandler?.(validationResult.error);
14
- continue;
15
- }
16
- queue.push(validationResult.data);
17
- }
18
- }
19
- catch (error) {
20
- errorHandler?.(error);
21
- }
22
- finally {
23
- queue.close();
24
- }
25
- })
26
- .catch((error) => {
27
- errorHandler?.(error);
28
- queue.close();
29
- });
30
- return queue;
31
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./summarize/SummarizationFunction.cjs"), exports);
18
- __exportStar(require("./summarize/summarizeRecursively.cjs"), exports);
19
- __exportStar(require("./summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs"), exports);
@@ -1,3 +0,0 @@
1
- export * from "./summarize/SummarizationFunction.js";
2
- export * from "./summarize/summarizeRecursively.js";
3
- export * from "./summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js";
@@ -1,3 +0,0 @@
1
- export * from "./summarize/SummarizationFunction.js";
2
- export * from "./summarize/summarizeRecursively.js";
3
- export * from "./summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js";
@@ -1,4 +0,0 @@
1
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- export type SummarizationFunction = (input: {
3
- text: string;
4
- }, options?: FunctionOptions) => PromiseLike<string>;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.summarizeRecursively = void 0;
4
- async function summarizeRecursively({ summarize, split, join = (texts) => texts.join("\n\n"), text, }, options) {
5
- const chunks = await split({ text });
6
- const summarizedTexts = await Promise.all(chunks.map((chunk) => summarize({ text: chunk }, options)));
7
- if (summarizedTexts.length === 1) {
8
- return summarizedTexts[0];
9
- }
10
- // recursive mapping: will split joined results as needed to stay
11
- // within the allowed size limit of the splitter.
12
- return summarizeRecursively({
13
- text: join(summarizedTexts),
14
- summarize,
15
- split,
16
- join,
17
- }, options);
18
- }
19
- exports.summarizeRecursively = summarizeRecursively;
@@ -1,11 +0,0 @@
1
- import { Run } from "../../core/Run.js";
2
- import { SplitFunction } from "../../text-chunk/split/SplitFunction.js";
3
- import { SummarizationFunction } from "./SummarizationFunction.js";
4
- export declare function summarizeRecursively({ summarize, split, join, text, }: {
5
- summarize: SummarizationFunction;
6
- split: SplitFunction;
7
- join?: (texts: Array<string>) => string;
8
- text: string;
9
- }, options?: {
10
- run?: Run;
11
- }): Promise<string>;
@@ -1,15 +0,0 @@
1
- export async function summarizeRecursively({ summarize, split, join = (texts) => texts.join("\n\n"), text, }, options) {
2
- const chunks = await split({ text });
3
- const summarizedTexts = await Promise.all(chunks.map((chunk) => summarize({ text: chunk }, options)));
4
- if (summarizedTexts.length === 1) {
5
- return summarizedTexts[0];
6
- }
7
- // recursive mapping: will split joined results as needed to stay
8
- // within the allowed size limit of the splitter.
9
- return summarizeRecursively({
10
- text: join(summarizedTexts),
11
- summarize,
12
- split,
13
- join,
14
- }, options);
15
- }
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.summarizeRecursivelyWithTextGenerationAndTokenSplitting = void 0;
4
- const generateText_js_1 = require("../../model-function/generate-text/generateText.cjs");
5
- const splitRecursively_js_1 = require("../../text-chunk/split/splitRecursively.cjs");
6
- const summarizeRecursively_js_1 = require("./summarizeRecursively.cjs");
7
- /**
8
- * Recursively summarizes a text using a text generation model, e.g. for summarization or text extraction.
9
- * It automatically splits the text into optimal chunks that are small enough to be processed by the model,
10
- * while leaving enough space for the model to generate text.
11
- */
12
- async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
13
- (model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), join, }, options) {
14
- const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
15
- return (0, summarizeRecursively_js_1.summarizeRecursively)({
16
- split: (0, splitRecursively_js_1.splitAtToken)({
17
- tokenizer: model.tokenizer,
18
- maxTokensPerChunk: tokenLimit - emptyPromptTokens,
19
- }),
20
- summarize: async (input) => (0, generateText_js_1.generateText)(model, await prompt(input), options),
21
- join,
22
- text,
23
- }, options);
24
- }
25
- exports.summarizeRecursivelyWithTextGenerationAndTokenSplitting = summarizeRecursivelyWithTextGenerationAndTokenSplitting;
@@ -1,24 +0,0 @@
1
- import { Run } from "../../core/Run.js";
2
- import { TextGenerationModel } from "../../model-function/generate-text/TextGenerationModel.js";
3
- import { FullTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
4
- /**
5
- * Recursively summarizes a text using a text generation model, e.g. for summarization or text extraction.
6
- * It automatically splits the text into optimal chunks that are small enough to be processed by the model,
7
- * while leaving enough space for the model to generate text.
8
- */
9
- export declare function summarizeRecursivelyWithTextGenerationAndTokenSplitting<PROMPT>({ text, model, prompt, tokenLimit, join, }: {
10
- text: string;
11
- model: TextGenerationModel<PROMPT> & {
12
- contextWindowSize: number;
13
- tokenizer: FullTokenizer;
14
- countPromptTokens: (prompt: PROMPT) => PromiseLike<number>;
15
- };
16
- prompt: (input: {
17
- text: string;
18
- }) => Promise<PROMPT>;
19
- tokenLimit?: number;
20
- join?: (texts: Array<string>) => string;
21
- }, options?: {
22
- functionId?: string;
23
- run?: Run;
24
- }): Promise<string>;
@@ -1,21 +0,0 @@
1
- import { generateText } from "../../model-function/generate-text/generateText.js";
2
- import { splitAtToken } from "../../text-chunk/split/splitRecursively.js";
3
- import { summarizeRecursively } from "./summarizeRecursively.js";
4
- /**
5
- * Recursively summarizes a text using a text generation model, e.g. for summarization or text extraction.
6
- * It automatically splits the text into optimal chunks that are small enough to be processed by the model,
7
- * while leaving enough space for the model to generate text.
8
- */
9
- export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
10
- (model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), join, }, options) {
11
- const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
12
- return summarizeRecursively({
13
- split: splitAtToken({
14
- tokenizer: model.tokenizer,
15
- maxTokensPerChunk: tokenLimit - emptyPromptTokens,
16
- }),
17
- summarize: async (input) => generateText(model, await prompt(input), options),
18
- join,
19
- text,
20
- }, options);
21
- }
package/cost/Cost.cjs DELETED
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Cost = void 0;
4
- class Cost {
5
- constructor({ costInMillicents, hasUnknownCost, callsWithUnknownCost, }) {
6
- Object.defineProperty(this, "costInMillicents", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: void 0
11
- });
12
- Object.defineProperty(this, "hasUnknownCost", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: void 0
17
- });
18
- Object.defineProperty(this, "callsWithUnknownCost", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: void 0
23
- });
24
- this.costInMillicents = costInMillicents;
25
- this.hasUnknownCost = hasUnknownCost;
26
- this.callsWithUnknownCost = callsWithUnknownCost;
27
- }
28
- get costInCent() {
29
- return this.costInMillicents / 1000;
30
- }
31
- get costInDollar() {
32
- return this.costInCent / 100;
33
- }
34
- formatAsDollarAmount({ decimals = 2 } = {}) {
35
- return `$${this.costInDollar.toFixed(decimals)}`;
36
- }
37
- }
38
- exports.Cost = Cost;
package/cost/Cost.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
2
- export declare class Cost {
3
- readonly costInMillicents: number;
4
- readonly hasUnknownCost: boolean;
5
- readonly callsWithUnknownCost: SuccessfulModelCall[];
6
- constructor({ costInMillicents, hasUnknownCost, callsWithUnknownCost, }: {
7
- costInMillicents: number;
8
- hasUnknownCost: boolean;
9
- callsWithUnknownCost: SuccessfulModelCall[];
10
- });
11
- get costInCent(): number;
12
- get costInDollar(): number;
13
- formatAsDollarAmount({ decimals }?: {
14
- decimals?: number;
15
- }): string;
16
- }
package/cost/Cost.js DELETED
@@ -1,34 +0,0 @@
1
- export class Cost {
2
- constructor({ costInMillicents, hasUnknownCost, callsWithUnknownCost, }) {
3
- Object.defineProperty(this, "costInMillicents", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- Object.defineProperty(this, "hasUnknownCost", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- Object.defineProperty(this, "callsWithUnknownCost", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- this.costInMillicents = costInMillicents;
22
- this.hasUnknownCost = hasUnknownCost;
23
- this.callsWithUnknownCost = callsWithUnknownCost;
24
- }
25
- get costInCent() {
26
- return this.costInMillicents / 1000;
27
- }
28
- get costInDollar() {
29
- return this.costInCent / 100;
30
- }
31
- formatAsDollarAmount({ decimals = 2 } = {}) {
32
- return `$${this.costInDollar.toFixed(decimals)}`;
33
- }
34
- }
@@ -1,8 +0,0 @@
1
- import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
2
- export interface CostCalculator {
3
- readonly provider: string;
4
- /**
5
- * @return null if the cost is unknown, otherwise the cost in Millicents (0 if free)
6
- */
7
- calculateCostInMillicents(call: SuccessfulModelCall): PromiseLike<number | null>;
8
- }
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateCost = void 0;
4
- const Cost_js_1 = require("./Cost.cjs");
5
- async function calculateCost({ calls, costCalculators, }) {
6
- let costInMillicents = 0;
7
- const callsWithUnknownCost = [];
8
- for (const call of calls) {
9
- const model = call.model;
10
- const providerCostCalculator = costCalculators.find((providerCostCalculator) => providerCostCalculator.provider === model.provider);
11
- if (!providerCostCalculator) {
12
- callsWithUnknownCost.push(call);
13
- continue;
14
- }
15
- const cost = await providerCostCalculator.calculateCostInMillicents(call);
16
- if (cost === null) {
17
- callsWithUnknownCost.push(call);
18
- continue;
19
- }
20
- costInMillicents += cost;
21
- }
22
- return new Cost_js_1.Cost({
23
- costInMillicents,
24
- hasUnknownCost: callsWithUnknownCost.length > 0,
25
- callsWithUnknownCost,
26
- });
27
- }
28
- exports.calculateCost = calculateCost;
@@ -1,7 +0,0 @@
1
- import { Cost } from "./Cost.js";
2
- import { CostCalculator } from "./CostCalculator.js";
3
- import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
4
- export declare function calculateCost({ calls, costCalculators, }: {
5
- calls: SuccessfulModelCall[];
6
- costCalculators: CostCalculator[];
7
- }): Promise<Cost>;
@@ -1,24 +0,0 @@
1
- import { Cost } from "./Cost.js";
2
- export async function calculateCost({ calls, costCalculators, }) {
3
- let costInMillicents = 0;
4
- const callsWithUnknownCost = [];
5
- for (const call of calls) {
6
- const model = call.model;
7
- const providerCostCalculator = costCalculators.find((providerCostCalculator) => providerCostCalculator.provider === model.provider);
8
- if (!providerCostCalculator) {
9
- callsWithUnknownCost.push(call);
10
- continue;
11
- }
12
- const cost = await providerCostCalculator.calculateCostInMillicents(call);
13
- if (cost === null) {
14
- callsWithUnknownCost.push(call);
15
- continue;
16
- }
17
- costInMillicents += cost;
18
- }
19
- return new Cost({
20
- costInMillicents,
21
- hasUnknownCost: callsWithUnknownCost.length > 0,
22
- callsWithUnknownCost,
23
- });
24
- }
package/cost/index.cjs DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Cost.cjs"), exports);
18
- __exportStar(require("./calculateCost.cjs"), exports);
19
- __exportStar(require("./CostCalculator.cjs"), exports);
package/cost/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./Cost.js";
2
- export * from "./calculateCost.js";
3
- export * from "./CostCalculator.js";
package/cost/index.js DELETED
@@ -1,3 +0,0 @@
1
- export * from "./Cost.js";
2
- export * from "./calculateCost.js";
3
- export * from "./CostCalculator.js";
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +0,0 @@
1
- import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
2
- export interface GuardStartedEvent extends BaseFunctionStartedEvent {
3
- functionType: "guard";
4
- }
5
- export interface GuardFinishedEvent extends BaseFunctionFinishedEvent {
6
- functionType: "guard";
7
- }
@@ -1,75 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fixStructure = void 0;
4
- const StructureParseError_js_1 = require("../model-function/generate-structure/StructureParseError.cjs");
5
- const StructureValidationError_js_1 = require("../model-function/generate-structure/StructureValidationError.cjs");
6
- /**
7
- * Attempts to correct and retry structure generation when encountering parsing or validation errors.
8
- *
9
- * This function acts as a guard within the structure generation process. If the generation results in
10
- * an error, identified as either a `StructureParseError` or `StructureValidationError`, the function
11
- * triggers a retry mechanism. It uses the `modifyInputForRetry` method, provided via options, to adjust
12
- * the input parameters, aiming to resolve the issue that caused the initial error. The process continues
13
- * until a valid structure is generated, or it exhausts the predefined retry limits.
14
- *
15
- * @template INPUT - The expected format/type of the input data used for structure generation.
16
- * @template OUTPUT - The expected format/type of the output data, i.e., the successfully generated structure.
17
- *
18
- * @param {Object} options - Configuration options for modifying the input before retrying structure generation.
19
- * @param {function} options.modifyInputForRetry - A function that takes the error type, original input, and error object.
20
- * It modifies the input data based on the error information, aiming to correct the issue for the retry attempt.
21
- * This function must return a promise that resolves with the modified input.
22
- *
23
- * @returns {Guard<INPUT, OUTPUT>} A guard function that intercepts the structure generation process, checking for
24
- * errors, and initiating retries by modifying the input parameters. The guard can trigger multiple retries
25
- * if the issues persist. If the process succeeds, it returns the valid structure; otherwise, it returns
26
- * undefined, indicating the exhaustion of retry attempts or a non-recoverable error.
27
- *
28
- * @example
29
- * const result = await guard(
30
- * (input) =>
31
- * generateStructure(
32
- * openai
33
- * .ChatTextGenerator(/* ... * /)
34
- * .asFunctionCallStructureGenerationModel(/* ... * /),
35
- *
36
- * new ZodSchema({
37
- * // ...
38
- * }),
39
- *
40
- * input
41
- * ),
42
- * [
43
- * // ...
44
- * ],
45
- * fixStructure({
46
- * modifyInputForRetry: async ({ input, error }) => [
47
- * ...input,
48
- * openai.ChatMessage.assistant(null, {
49
- * functionCall: {
50
- * name: "sentiment",
51
- * arguments: JSON.stringify(error.valueText),
52
- * },
53
- * }),
54
- * openai.ChatMessage.user(error.message),
55
- * openai.ChatMessage.user("Please fix the error and try again."),
56
- * ],
57
- * })
58
- * );
59
- */
60
- const fixStructure = ({ modifyInputForRetry }) => async (result) => {
61
- if (result.type === "error" &&
62
- (result.error instanceof StructureValidationError_js_1.StructureValidationError ||
63
- result.error instanceof StructureParseError_js_1.StructureParseError)) {
64
- return {
65
- action: "retry",
66
- input: await modifyInputForRetry({
67
- type: "error",
68
- input: result.input,
69
- error: result.error,
70
- }),
71
- };
72
- }
73
- return undefined;
74
- };
75
- exports.fixStructure = fixStructure;
@@ -1,64 +0,0 @@
1
- import { StructureParseError } from "../model-function/generate-structure/StructureParseError.js";
2
- import { StructureValidationError } from "../model-function/generate-structure/StructureValidationError.js";
3
- import { Guard } from "./guard.js";
4
- /**
5
- * Attempts to correct and retry structure generation when encountering parsing or validation errors.
6
- *
7
- * This function acts as a guard within the structure generation process. If the generation results in
8
- * an error, identified as either a `StructureParseError` or `StructureValidationError`, the function
9
- * triggers a retry mechanism. It uses the `modifyInputForRetry` method, provided via options, to adjust
10
- * the input parameters, aiming to resolve the issue that caused the initial error. The process continues
11
- * until a valid structure is generated, or it exhausts the predefined retry limits.
12
- *
13
- * @template INPUT - The expected format/type of the input data used for structure generation.
14
- * @template OUTPUT - The expected format/type of the output data, i.e., the successfully generated structure.
15
- *
16
- * @param {Object} options - Configuration options for modifying the input before retrying structure generation.
17
- * @param {function} options.modifyInputForRetry - A function that takes the error type, original input, and error object.
18
- * It modifies the input data based on the error information, aiming to correct the issue for the retry attempt.
19
- * This function must return a promise that resolves with the modified input.
20
- *
21
- * @returns {Guard<INPUT, OUTPUT>} A guard function that intercepts the structure generation process, checking for
22
- * errors, and initiating retries by modifying the input parameters. The guard can trigger multiple retries
23
- * if the issues persist. If the process succeeds, it returns the valid structure; otherwise, it returns
24
- * undefined, indicating the exhaustion of retry attempts or a non-recoverable error.
25
- *
26
- * @example
27
- * const result = await guard(
28
- * (input) =>
29
- * generateStructure(
30
- * openai
31
- * .ChatTextGenerator(/* ... * /)
32
- * .asFunctionCallStructureGenerationModel(/* ... * /),
33
- *
34
- * new ZodSchema({
35
- * // ...
36
- * }),
37
- *
38
- * input
39
- * ),
40
- * [
41
- * // ...
42
- * ],
43
- * fixStructure({
44
- * modifyInputForRetry: async ({ input, error }) => [
45
- * ...input,
46
- * openai.ChatMessage.assistant(null, {
47
- * functionCall: {
48
- * name: "sentiment",
49
- * arguments: JSON.stringify(error.valueText),
50
- * },
51
- * }),
52
- * openai.ChatMessage.user(error.message),
53
- * openai.ChatMessage.user("Please fix the error and try again."),
54
- * ],
55
- * })
56
- * );
57
- */
58
- export declare const fixStructure: <INPUT, OUTPUT>(options: {
59
- modifyInputForRetry: (options: {
60
- type: "error";
61
- input: INPUT;
62
- error: StructureValidationError | StructureParseError;
63
- }) => PromiseLike<INPUT>;
64
- }) => Guard<INPUT, OUTPUT>;
@@ -1,71 +0,0 @@
1
- import { StructureParseError } from "../model-function/generate-structure/StructureParseError.js";
2
- import { StructureValidationError } from "../model-function/generate-structure/StructureValidationError.js";
3
- /**
4
- * Attempts to correct and retry structure generation when encountering parsing or validation errors.
5
- *
6
- * This function acts as a guard within the structure generation process. If the generation results in
7
- * an error, identified as either a `StructureParseError` or `StructureValidationError`, the function
8
- * triggers a retry mechanism. It uses the `modifyInputForRetry` method, provided via options, to adjust
9
- * the input parameters, aiming to resolve the issue that caused the initial error. The process continues
10
- * until a valid structure is generated, or it exhausts the predefined retry limits.
11
- *
12
- * @template INPUT - The expected format/type of the input data used for structure generation.
13
- * @template OUTPUT - The expected format/type of the output data, i.e., the successfully generated structure.
14
- *
15
- * @param {Object} options - Configuration options for modifying the input before retrying structure generation.
16
- * @param {function} options.modifyInputForRetry - A function that takes the error type, original input, and error object.
17
- * It modifies the input data based on the error information, aiming to correct the issue for the retry attempt.
18
- * This function must return a promise that resolves with the modified input.
19
- *
20
- * @returns {Guard<INPUT, OUTPUT>} A guard function that intercepts the structure generation process, checking for
21
- * errors, and initiating retries by modifying the input parameters. The guard can trigger multiple retries
22
- * if the issues persist. If the process succeeds, it returns the valid structure; otherwise, it returns
23
- * undefined, indicating the exhaustion of retry attempts or a non-recoverable error.
24
- *
25
- * @example
26
- * const result = await guard(
27
- * (input) =>
28
- * generateStructure(
29
- * openai
30
- * .ChatTextGenerator(/* ... * /)
31
- * .asFunctionCallStructureGenerationModel(/* ... * /),
32
- *
33
- * new ZodSchema({
34
- * // ...
35
- * }),
36
- *
37
- * input
38
- * ),
39
- * [
40
- * // ...
41
- * ],
42
- * fixStructure({
43
- * modifyInputForRetry: async ({ input, error }) => [
44
- * ...input,
45
- * openai.ChatMessage.assistant(null, {
46
- * functionCall: {
47
- * name: "sentiment",
48
- * arguments: JSON.stringify(error.valueText),
49
- * },
50
- * }),
51
- * openai.ChatMessage.user(error.message),
52
- * openai.ChatMessage.user("Please fix the error and try again."),
53
- * ],
54
- * })
55
- * );
56
- */
57
- export const fixStructure = ({ modifyInputForRetry }) => async (result) => {
58
- if (result.type === "error" &&
59
- (result.error instanceof StructureValidationError ||
60
- result.error instanceof StructureParseError)) {
61
- return {
62
- action: "retry",
63
- input: await modifyInputForRetry({
64
- type: "error",
65
- input: result.input,
66
- error: result.error,
67
- }),
68
- };
69
- }
70
- return undefined;
71
- };