@illuma-ai/agents 1.0.81
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/README.md +485 -0
- package/dist/cjs/agents/AgentContext.cjs +734 -0
- package/dist/cjs/agents/AgentContext.cjs.map +1 -0
- package/dist/cjs/common/enum.cjs +190 -0
- package/dist/cjs/common/enum.cjs.map +1 -0
- package/dist/cjs/events.cjs +172 -0
- package/dist/cjs/events.cjs.map +1 -0
- package/dist/cjs/graphs/Graph.cjs +1615 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs +890 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
- package/dist/cjs/instrumentation.cjs +21 -0
- package/dist/cjs/instrumentation.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/index.cjs +292 -0
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/types.cjs +50 -0
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +630 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +218 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
- package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
- package/dist/cjs/llm/bedrock/index.cjs +282 -0
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -0
- package/dist/cjs/llm/fake.cjs +97 -0
- package/dist/cjs/llm/fake.cjs.map +1 -0
- package/dist/cjs/llm/google/index.cjs +216 -0
- package/dist/cjs/llm/google/index.cjs.map +1 -0
- package/dist/cjs/llm/google/utils/common.cjs +647 -0
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
- package/dist/cjs/llm/openai/index.cjs +1028 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -0
- package/dist/cjs/llm/openai/utils/index.cjs +765 -0
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
- package/dist/cjs/llm/openrouter/index.cjs +212 -0
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +43 -0
- package/dist/cjs/llm/providers.cjs.map +1 -0
- package/dist/cjs/llm/text.cjs +69 -0
- package/dist/cjs/llm/text.cjs.map +1 -0
- package/dist/cjs/llm/vertexai/index.cjs +329 -0
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
- package/dist/cjs/main.cjs +240 -0
- package/dist/cjs/main.cjs.map +1 -0
- package/dist/cjs/messages/cache.cjs +387 -0
- package/dist/cjs/messages/cache.cjs.map +1 -0
- package/dist/cjs/messages/content.cjs +53 -0
- package/dist/cjs/messages/content.cjs.map +1 -0
- package/dist/cjs/messages/core.cjs +367 -0
- package/dist/cjs/messages/core.cjs.map +1 -0
- package/dist/cjs/messages/format.cjs +761 -0
- package/dist/cjs/messages/format.cjs.map +1 -0
- package/dist/cjs/messages/ids.cjs +23 -0
- package/dist/cjs/messages/ids.cjs.map +1 -0
- package/dist/cjs/messages/prune.cjs +398 -0
- package/dist/cjs/messages/prune.cjs.map +1 -0
- package/dist/cjs/messages/tools.cjs +96 -0
- package/dist/cjs/messages/tools.cjs.map +1 -0
- package/dist/cjs/run.cjs +328 -0
- package/dist/cjs/run.cjs.map +1 -0
- package/dist/cjs/schemas/validate.cjs +324 -0
- package/dist/cjs/schemas/validate.cjs.map +1 -0
- package/dist/cjs/splitStream.cjs +210 -0
- package/dist/cjs/splitStream.cjs.map +1 -0
- package/dist/cjs/stream.cjs +620 -0
- package/dist/cjs/stream.cjs.map +1 -0
- package/dist/cjs/tools/BrowserTools.cjs +248 -0
- package/dist/cjs/tools/BrowserTools.cjs.map +1 -0
- package/dist/cjs/tools/Calculator.cjs +66 -0
- package/dist/cjs/tools/Calculator.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +234 -0
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -0
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +636 -0
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +548 -0
- package/dist/cjs/tools/ToolNode.cjs.map +1 -0
- package/dist/cjs/tools/ToolSearch.cjs +909 -0
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -0
- package/dist/cjs/tools/handlers.cjs +255 -0
- package/dist/cjs/tools/handlers.cjs.map +1 -0
- package/dist/cjs/tools/schema.cjs +31 -0
- package/dist/cjs/tools/schema.cjs.map +1 -0
- package/dist/cjs/tools/search/anthropic.cjs +40 -0
- package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
- package/dist/cjs/tools/search/content.cjs +140 -0
- package/dist/cjs/tools/search/content.cjs.map +1 -0
- package/dist/cjs/tools/search/firecrawl.cjs +179 -0
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -0
- package/dist/cjs/tools/search/format.cjs +203 -0
- package/dist/cjs/tools/search/format.cjs.map +1 -0
- package/dist/cjs/tools/search/highlights.cjs +245 -0
- package/dist/cjs/tools/search/highlights.cjs.map +1 -0
- package/dist/cjs/tools/search/rerankers.cjs +174 -0
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -0
- package/dist/cjs/tools/search/schema.cjs +117 -0
- package/dist/cjs/tools/search/schema.cjs.map +1 -0
- package/dist/cjs/tools/search/search.cjs +566 -0
- package/dist/cjs/tools/search/search.cjs.map +1 -0
- package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
- package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
- package/dist/cjs/tools/search/tool.cjs +456 -0
- package/dist/cjs/tools/search/tool.cjs.map +1 -0
- package/dist/cjs/tools/search/utils.cjs +66 -0
- package/dist/cjs/tools/search/utils.cjs.map +1 -0
- package/dist/cjs/types/graph.cjs +29 -0
- package/dist/cjs/types/graph.cjs.map +1 -0
- package/dist/cjs/utils/contextAnalytics.cjs +66 -0
- package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
- package/dist/cjs/utils/events.cjs +31 -0
- package/dist/cjs/utils/events.cjs.map +1 -0
- package/dist/cjs/utils/graph.cjs +16 -0
- package/dist/cjs/utils/graph.cjs.map +1 -0
- package/dist/cjs/utils/handlers.cjs +70 -0
- package/dist/cjs/utils/handlers.cjs.map +1 -0
- package/dist/cjs/utils/llm.cjs +27 -0
- package/dist/cjs/utils/llm.cjs.map +1 -0
- package/dist/cjs/utils/misc.cjs +56 -0
- package/dist/cjs/utils/misc.cjs.map +1 -0
- package/dist/cjs/utils/run.cjs +73 -0
- package/dist/cjs/utils/run.cjs.map +1 -0
- package/dist/cjs/utils/schema.cjs +27 -0
- package/dist/cjs/utils/schema.cjs.map +1 -0
- package/dist/cjs/utils/title.cjs +125 -0
- package/dist/cjs/utils/title.cjs.map +1 -0
- package/dist/cjs/utils/tokens.cjs +125 -0
- package/dist/cjs/utils/tokens.cjs.map +1 -0
- package/dist/cjs/utils/toonFormat.cjs +388 -0
- package/dist/cjs/utils/toonFormat.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +732 -0
- package/dist/esm/agents/AgentContext.mjs.map +1 -0
- package/dist/esm/common/enum.mjs +190 -0
- package/dist/esm/common/enum.mjs.map +1 -0
- package/dist/esm/events.mjs +164 -0
- package/dist/esm/events.mjs.map +1 -0
- package/dist/esm/graphs/Graph.mjs +1612 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs +888 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
- package/dist/esm/instrumentation.mjs +19 -0
- package/dist/esm/instrumentation.mjs.map +1 -0
- package/dist/esm/llm/anthropic/index.mjs +290 -0
- package/dist/esm/llm/anthropic/index.mjs.map +1 -0
- package/dist/esm/llm/anthropic/types.mjs +48 -0
- package/dist/esm/llm/anthropic/types.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +627 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +216 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
- package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
- package/dist/esm/llm/bedrock/index.mjs +280 -0
- package/dist/esm/llm/bedrock/index.mjs.map +1 -0
- package/dist/esm/llm/fake.mjs +94 -0
- package/dist/esm/llm/fake.mjs.map +1 -0
- package/dist/esm/llm/google/index.mjs +214 -0
- package/dist/esm/llm/google/index.mjs.map +1 -0
- package/dist/esm/llm/google/utils/common.mjs +638 -0
- package/dist/esm/llm/google/utils/common.mjs.map +1 -0
- package/dist/esm/llm/openai/index.mjs +1018 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -0
- package/dist/esm/llm/openai/utils/index.mjs +759 -0
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
- package/dist/esm/llm/openrouter/index.mjs +210 -0
- package/dist/esm/llm/openrouter/index.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +39 -0
- package/dist/esm/llm/providers.mjs.map +1 -0
- package/dist/esm/llm/text.mjs +67 -0
- package/dist/esm/llm/text.mjs.map +1 -0
- package/dist/esm/llm/vertexai/index.mjs +327 -0
- package/dist/esm/llm/vertexai/index.mjs.map +1 -0
- package/dist/esm/main.mjs +37 -0
- package/dist/esm/main.mjs.map +1 -0
- package/dist/esm/messages/cache.mjs +382 -0
- package/dist/esm/messages/cache.mjs.map +1 -0
- package/dist/esm/messages/content.mjs +51 -0
- package/dist/esm/messages/content.mjs.map +1 -0
- package/dist/esm/messages/core.mjs +359 -0
- package/dist/esm/messages/core.mjs.map +1 -0
- package/dist/esm/messages/format.mjs +752 -0
- package/dist/esm/messages/format.mjs.map +1 -0
- package/dist/esm/messages/ids.mjs +21 -0
- package/dist/esm/messages/ids.mjs.map +1 -0
- package/dist/esm/messages/prune.mjs +393 -0
- package/dist/esm/messages/prune.mjs.map +1 -0
- package/dist/esm/messages/tools.mjs +93 -0
- package/dist/esm/messages/tools.mjs.map +1 -0
- package/dist/esm/run.mjs +325 -0
- package/dist/esm/run.mjs.map +1 -0
- package/dist/esm/schemas/validate.mjs +317 -0
- package/dist/esm/schemas/validate.mjs.map +1 -0
- package/dist/esm/splitStream.mjs +207 -0
- package/dist/esm/splitStream.mjs.map +1 -0
- package/dist/esm/stream.mjs +616 -0
- package/dist/esm/stream.mjs.map +1 -0
- package/dist/esm/tools/BrowserTools.mjs +244 -0
- package/dist/esm/tools/BrowserTools.mjs.map +1 -0
- package/dist/esm/tools/Calculator.mjs +41 -0
- package/dist/esm/tools/Calculator.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +226 -0
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -0
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +622 -0
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +545 -0
- package/dist/esm/tools/ToolNode.mjs.map +1 -0
- package/dist/esm/tools/ToolSearch.mjs +870 -0
- package/dist/esm/tools/ToolSearch.mjs.map +1 -0
- package/dist/esm/tools/handlers.mjs +250 -0
- package/dist/esm/tools/handlers.mjs.map +1 -0
- package/dist/esm/tools/schema.mjs +28 -0
- package/dist/esm/tools/schema.mjs.map +1 -0
- package/dist/esm/tools/search/anthropic.mjs +37 -0
- package/dist/esm/tools/search/anthropic.mjs.map +1 -0
- package/dist/esm/tools/search/content.mjs +119 -0
- package/dist/esm/tools/search/content.mjs.map +1 -0
- package/dist/esm/tools/search/firecrawl.mjs +176 -0
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -0
- package/dist/esm/tools/search/format.mjs +201 -0
- package/dist/esm/tools/search/format.mjs.map +1 -0
- package/dist/esm/tools/search/highlights.mjs +243 -0
- package/dist/esm/tools/search/highlights.mjs.map +1 -0
- package/dist/esm/tools/search/rerankers.mjs +168 -0
- package/dist/esm/tools/search/rerankers.mjs.map +1 -0
- package/dist/esm/tools/search/schema.mjs +104 -0
- package/dist/esm/tools/search/schema.mjs.map +1 -0
- package/dist/esm/tools/search/search.mjs +563 -0
- package/dist/esm/tools/search/search.mjs.map +1 -0
- package/dist/esm/tools/search/serper-scraper.mjs +129 -0
- package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
- package/dist/esm/tools/search/tool.mjs +454 -0
- package/dist/esm/tools/search/tool.mjs.map +1 -0
- package/dist/esm/tools/search/utils.mjs +61 -0
- package/dist/esm/tools/search/utils.mjs.map +1 -0
- package/dist/esm/types/graph.mjs +26 -0
- package/dist/esm/types/graph.mjs.map +1 -0
- package/dist/esm/utils/contextAnalytics.mjs +64 -0
- package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
- package/dist/esm/utils/events.mjs +29 -0
- package/dist/esm/utils/events.mjs.map +1 -0
- package/dist/esm/utils/graph.mjs +13 -0
- package/dist/esm/utils/graph.mjs.map +1 -0
- package/dist/esm/utils/handlers.mjs +68 -0
- package/dist/esm/utils/handlers.mjs.map +1 -0
- package/dist/esm/utils/llm.mjs +24 -0
- package/dist/esm/utils/llm.mjs.map +1 -0
- package/dist/esm/utils/misc.mjs +53 -0
- package/dist/esm/utils/misc.mjs.map +1 -0
- package/dist/esm/utils/run.mjs +70 -0
- package/dist/esm/utils/run.mjs.map +1 -0
- package/dist/esm/utils/schema.mjs +24 -0
- package/dist/esm/utils/schema.mjs.map +1 -0
- package/dist/esm/utils/title.mjs +122 -0
- package/dist/esm/utils/title.mjs.map +1 -0
- package/dist/esm/utils/tokens.mjs +121 -0
- package/dist/esm/utils/tokens.mjs.map +1 -0
- package/dist/esm/utils/toonFormat.mjs +381 -0
- package/dist/esm/utils/toonFormat.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +293 -0
- package/dist/types/common/enum.d.ts +155 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/events.d.ts +31 -0
- package/dist/types/graphs/Graph.d.ts +216 -0
- package/dist/types/graphs/MultiAgentGraph.d.ts +104 -0
- package/dist/types/graphs/index.d.ts +2 -0
- package/dist/types/index.d.ts +21 -0
- package/dist/types/instrumentation.d.ts +1 -0
- package/dist/types/llm/anthropic/index.d.ts +39 -0
- package/dist/types/llm/anthropic/types.d.ts +37 -0
- package/dist/types/llm/anthropic/utils/message_inputs.d.ts +14 -0
- package/dist/types/llm/anthropic/utils/message_outputs.d.ts +14 -0
- package/dist/types/llm/anthropic/utils/output_parsers.d.ts +22 -0
- package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
- package/dist/types/llm/bedrock/index.d.ts +141 -0
- package/dist/types/llm/bedrock/types.d.ts +27 -0
- package/dist/types/llm/bedrock/utils/index.d.ts +5 -0
- package/dist/types/llm/bedrock/utils/message_inputs.d.ts +31 -0
- package/dist/types/llm/bedrock/utils/message_outputs.d.ts +33 -0
- package/dist/types/llm/fake.d.ts +31 -0
- package/dist/types/llm/google/index.d.ts +24 -0
- package/dist/types/llm/google/types.d.ts +42 -0
- package/dist/types/llm/google/utils/common.d.ts +34 -0
- package/dist/types/llm/google/utils/tools.d.ts +10 -0
- package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
- package/dist/types/llm/openai/index.d.ts +127 -0
- package/dist/types/llm/openai/types.d.ts +10 -0
- package/dist/types/llm/openai/utils/index.d.ts +29 -0
- package/dist/types/llm/openrouter/index.d.ts +15 -0
- package/dist/types/llm/providers.d.ts +5 -0
- package/dist/types/llm/text.d.ts +21 -0
- package/dist/types/llm/vertexai/index.d.ts +293 -0
- package/dist/types/messages/cache.d.ts +54 -0
- package/dist/types/messages/content.d.ts +7 -0
- package/dist/types/messages/core.d.ts +14 -0
- package/dist/types/messages/format.d.ts +137 -0
- package/dist/types/messages/ids.d.ts +3 -0
- package/dist/types/messages/index.d.ts +7 -0
- package/dist/types/messages/prune.d.ts +52 -0
- package/dist/types/messages/reducer.d.ts +9 -0
- package/dist/types/messages/tools.d.ts +17 -0
- package/dist/types/mockStream.d.ts +32 -0
- package/dist/types/prompts/collab.d.ts +1 -0
- package/dist/types/prompts/index.d.ts +2 -0
- package/dist/types/prompts/taskmanager.d.ts +41 -0
- package/dist/types/run.d.ts +41 -0
- package/dist/types/schemas/index.d.ts +1 -0
- package/dist/types/schemas/validate.d.ts +59 -0
- package/dist/types/splitStream.d.ts +37 -0
- package/dist/types/stream.d.ts +15 -0
- package/dist/types/test/mockTools.d.ts +28 -0
- package/dist/types/tools/BrowserTools.d.ts +87 -0
- package/dist/types/tools/Calculator.d.ts +34 -0
- package/dist/types/tools/CodeExecutor.d.ts +57 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +138 -0
- package/dist/types/tools/ToolNode.d.ts +51 -0
- package/dist/types/tools/ToolSearch.d.ts +219 -0
- package/dist/types/tools/handlers.d.ts +22 -0
- package/dist/types/tools/schema.d.ts +12 -0
- package/dist/types/tools/search/anthropic.d.ts +16 -0
- package/dist/types/tools/search/content.d.ts +4 -0
- package/dist/types/tools/search/firecrawl.d.ts +54 -0
- package/dist/types/tools/search/format.d.ts +5 -0
- package/dist/types/tools/search/highlights.d.ts +13 -0
- package/dist/types/tools/search/index.d.ts +3 -0
- package/dist/types/tools/search/rerankers.d.ts +38 -0
- package/dist/types/tools/search/schema.d.ts +103 -0
- package/dist/types/tools/search/search.d.ts +8 -0
- package/dist/types/tools/search/serper-scraper.d.ts +59 -0
- package/dist/types/tools/search/test.d.ts +1 -0
- package/dist/types/tools/search/tool.d.ts +3 -0
- package/dist/types/tools/search/types.d.ts +575 -0
- package/dist/types/tools/search/utils.d.ts +10 -0
- package/dist/types/types/graph.d.ts +399 -0
- package/dist/types/types/index.d.ts +5 -0
- package/dist/types/types/llm.d.ts +105 -0
- package/dist/types/types/messages.d.ts +4 -0
- package/dist/types/types/run.d.ts +112 -0
- package/dist/types/types/stream.d.ts +308 -0
- package/dist/types/types/tools.d.ts +296 -0
- package/dist/types/utils/contextAnalytics.d.ts +37 -0
- package/dist/types/utils/events.d.ts +6 -0
- package/dist/types/utils/graph.d.ts +2 -0
- package/dist/types/utils/handlers.d.ts +34 -0
- package/dist/types/utils/index.d.ts +9 -0
- package/dist/types/utils/llm.d.ts +3 -0
- package/dist/types/utils/llmConfig.d.ts +3 -0
- package/dist/types/utils/logging.d.ts +1 -0
- package/dist/types/utils/misc.d.ts +7 -0
- package/dist/types/utils/run.d.ts +27 -0
- package/dist/types/utils/schema.d.ts +8 -0
- package/dist/types/utils/title.d.ts +4 -0
- package/dist/types/utils/tokens.d.ts +28 -0
- package/dist/types/utils/toonFormat.d.ts +111 -0
- package/package.json +190 -0
- package/src/agents/AgentContext.test.ts +458 -0
- package/src/agents/AgentContext.ts +972 -0
- package/src/agents/__tests__/AgentContext.test.ts +805 -0
- package/src/agents/__tests__/resolveStructuredOutputMode.test.ts +137 -0
- package/src/common/enum.ts +203 -0
- package/src/common/index.ts +2 -0
- package/src/events.ts +223 -0
- package/src/graphs/Graph.ts +2228 -0
- package/src/graphs/MultiAgentGraph.ts +1063 -0
- package/src/graphs/__tests__/structured-output.integration.test.ts +809 -0
- package/src/graphs/__tests__/structured-output.test.ts +183 -0
- package/src/graphs/index.ts +2 -0
- package/src/index.ts +34 -0
- package/src/instrumentation.ts +22 -0
- package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
- package/src/llm/anthropic/index.ts +413 -0
- package/src/llm/anthropic/llm.spec.ts +1442 -0
- package/src/llm/anthropic/types.ts +140 -0
- package/src/llm/anthropic/utils/message_inputs.ts +757 -0
- package/src/llm/anthropic/utils/message_outputs.ts +289 -0
- package/src/llm/anthropic/utils/output_parsers.ts +133 -0
- package/src/llm/anthropic/utils/tools.ts +29 -0
- package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -0
- package/src/llm/bedrock/index.ts +411 -0
- package/src/llm/bedrock/llm.spec.ts +616 -0
- package/src/llm/bedrock/types.ts +51 -0
- package/src/llm/bedrock/utils/index.ts +18 -0
- package/src/llm/bedrock/utils/message_inputs.ts +563 -0
- package/src/llm/bedrock/utils/message_outputs.ts +310 -0
- package/src/llm/fake.ts +133 -0
- package/src/llm/google/data/gettysburg10.wav +0 -0
- package/src/llm/google/data/hotdog.jpg +0 -0
- package/src/llm/google/index.ts +337 -0
- package/src/llm/google/llm.spec.ts +934 -0
- package/src/llm/google/types.ts +56 -0
- package/src/llm/google/utils/common.ts +873 -0
- package/src/llm/google/utils/tools.ts +160 -0
- package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -0
- package/src/llm/openai/index.ts +1366 -0
- package/src/llm/openai/types.ts +24 -0
- package/src/llm/openai/utils/index.ts +1035 -0
- package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
- package/src/llm/openrouter/index.ts +291 -0
- package/src/llm/providers.ts +52 -0
- package/src/llm/text.ts +94 -0
- package/src/llm/vertexai/index.ts +359 -0
- package/src/messages/__tests__/tools.test.ts +473 -0
- package/src/messages/cache.test.ts +1261 -0
- package/src/messages/cache.ts +518 -0
- package/src/messages/content.test.ts +362 -0
- package/src/messages/content.ts +63 -0
- package/src/messages/core.ts +473 -0
- package/src/messages/ensureThinkingBlock.test.ts +468 -0
- package/src/messages/format.ts +1029 -0
- package/src/messages/formatAgentMessages.test.ts +1513 -0
- package/src/messages/formatAgentMessages.tools.test.ts +419 -0
- package/src/messages/formatMessage.test.ts +693 -0
- package/src/messages/ids.ts +26 -0
- package/src/messages/index.ts +7 -0
- package/src/messages/labelContentByAgent.test.ts +887 -0
- package/src/messages/prune.ts +568 -0
- package/src/messages/reducer.ts +80 -0
- package/src/messages/shiftIndexTokenCountMap.test.ts +81 -0
- package/src/messages/tools.ts +108 -0
- package/src/mockStream.ts +99 -0
- package/src/prompts/collab.ts +6 -0
- package/src/prompts/index.ts +2 -0
- package/src/prompts/taskmanager.ts +61 -0
- package/src/run.ts +467 -0
- package/src/schemas/index.ts +2 -0
- package/src/schemas/schema-preparation.test.ts +500 -0
- package/src/schemas/validate.test.ts +358 -0
- package/src/schemas/validate.ts +454 -0
- package/src/scripts/abort.ts +157 -0
- package/src/scripts/ant_web_search.ts +158 -0
- package/src/scripts/ant_web_search_edge_case.ts +162 -0
- package/src/scripts/ant_web_search_error_edge_case.ts +148 -0
- package/src/scripts/args.ts +48 -0
- package/src/scripts/caching.ts +132 -0
- package/src/scripts/cli.ts +172 -0
- package/src/scripts/cli2.ts +133 -0
- package/src/scripts/cli3.ts +184 -0
- package/src/scripts/cli4.ts +191 -0
- package/src/scripts/cli5.ts +191 -0
- package/src/scripts/code_exec.ts +213 -0
- package/src/scripts/code_exec_files.ts +236 -0
- package/src/scripts/code_exec_multi_session.ts +241 -0
- package/src/scripts/code_exec_ptc.ts +334 -0
- package/src/scripts/code_exec_session.ts +282 -0
- package/src/scripts/code_exec_simple.ts +147 -0
- package/src/scripts/content.ts +138 -0
- package/src/scripts/empty_input.ts +137 -0
- package/src/scripts/handoff-test.ts +135 -0
- package/src/scripts/image.ts +178 -0
- package/src/scripts/memory.ts +97 -0
- package/src/scripts/multi-agent-chain.ts +331 -0
- package/src/scripts/multi-agent-conditional.ts +221 -0
- package/src/scripts/multi-agent-document-review-chain.ts +197 -0
- package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
- package/src/scripts/multi-agent-parallel-start.ts +265 -0
- package/src/scripts/multi-agent-parallel.ts +394 -0
- package/src/scripts/multi-agent-sequence.ts +217 -0
- package/src/scripts/multi-agent-supervisor.ts +365 -0
- package/src/scripts/multi-agent-test.ts +186 -0
- package/src/scripts/parallel-asymmetric-tools-test.ts +274 -0
- package/src/scripts/parallel-full-metadata-test.ts +240 -0
- package/src/scripts/parallel-tools-test.ts +340 -0
- package/src/scripts/programmatic_exec.ts +396 -0
- package/src/scripts/programmatic_exec_agent.ts +231 -0
- package/src/scripts/search.ts +146 -0
- package/src/scripts/sequential-full-metadata-test.ts +197 -0
- package/src/scripts/simple.ts +225 -0
- package/src/scripts/single-agent-metadata-test.ts +198 -0
- package/src/scripts/stream.ts +140 -0
- package/src/scripts/test-custom-prompt-key.ts +145 -0
- package/src/scripts/test-handoff-input.ts +170 -0
- package/src/scripts/test-handoff-preamble.ts +277 -0
- package/src/scripts/test-multi-agent-list-handoff.ts +417 -0
- package/src/scripts/test-parallel-agent-labeling.ts +325 -0
- package/src/scripts/test-parallel-handoffs.ts +291 -0
- package/src/scripts/test-thinking-handoff-bedrock.ts +153 -0
- package/src/scripts/test-thinking-handoff.ts +155 -0
- package/src/scripts/test-tools-before-handoff.ts +226 -0
- package/src/scripts/test_code_api.ts +361 -0
- package/src/scripts/thinking-bedrock.ts +159 -0
- package/src/scripts/thinking.ts +171 -0
- package/src/scripts/tool_search.ts +162 -0
- package/src/scripts/tools.ts +177 -0
- package/src/specs/agent-handoffs.test.ts +888 -0
- package/src/specs/anthropic.simple.test.ts +387 -0
- package/src/specs/azure.simple.test.ts +364 -0
- package/src/specs/cache.simple.test.ts +396 -0
- package/src/specs/deepseek.simple.test.ts +283 -0
- package/src/specs/emergency-prune.test.ts +407 -0
- package/src/specs/moonshot.simple.test.ts +358 -0
- package/src/specs/openai.simple.test.ts +311 -0
- package/src/specs/openrouter.simple.test.ts +107 -0
- package/src/specs/prune.test.ts +901 -0
- package/src/specs/reasoning.test.ts +201 -0
- package/src/specs/spec.utils.ts +3 -0
- package/src/specs/thinking-handoff.test.ts +620 -0
- package/src/specs/thinking-prune.test.ts +703 -0
- package/src/specs/token-distribution-edge-case.test.ts +316 -0
- package/src/specs/token-memoization.test.ts +32 -0
- package/src/specs/tool-error.test.ts +198 -0
- package/src/splitStream.test.ts +691 -0
- package/src/splitStream.ts +234 -0
- package/src/stream.test.ts +94 -0
- package/src/stream.ts +801 -0
- package/src/test/mockTools.ts +386 -0
- package/src/tools/BrowserTools.ts +393 -0
- package/src/tools/Calculator.test.ts +278 -0
- package/src/tools/Calculator.ts +46 -0
- package/src/tools/CodeExecutor.ts +270 -0
- package/src/tools/ProgrammaticToolCalling.ts +785 -0
- package/src/tools/ToolNode.ts +674 -0
- package/src/tools/ToolSearch.ts +1095 -0
- package/src/tools/__tests__/BrowserTools.test.ts +265 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +319 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +1006 -0
- package/src/tools/__tests__/ToolSearch.integration.test.ts +162 -0
- package/src/tools/__tests__/ToolSearch.test.ts +1003 -0
- package/src/tools/handlers.ts +363 -0
- package/src/tools/schema.ts +37 -0
- package/src/tools/search/anthropic.ts +51 -0
- package/src/tools/search/content.test.ts +173 -0
- package/src/tools/search/content.ts +147 -0
- package/src/tools/search/firecrawl.ts +210 -0
- package/src/tools/search/format.ts +250 -0
- package/src/tools/search/highlights.ts +320 -0
- package/src/tools/search/index.ts +3 -0
- package/src/tools/search/jina-reranker.test.ts +130 -0
- package/src/tools/search/output.md +2775 -0
- package/src/tools/search/rerankers.ts +242 -0
- package/src/tools/search/schema.ts +113 -0
- package/src/tools/search/search.ts +768 -0
- package/src/tools/search/serper-scraper.ts +155 -0
- package/src/tools/search/test.html +884 -0
- package/src/tools/search/test.md +643 -0
- package/src/tools/search/test.ts +159 -0
- package/src/tools/search/tool.ts +657 -0
- package/src/tools/search/types.ts +665 -0
- package/src/tools/search/utils.ts +79 -0
- package/src/types/graph.test.ts +218 -0
- package/src/types/graph.ts +533 -0
- package/src/types/index.ts +6 -0
- package/src/types/llm.ts +140 -0
- package/src/types/messages.ts +4 -0
- package/src/types/run.ts +128 -0
- package/src/types/stream.ts +417 -0
- package/src/types/tools.ts +355 -0
- package/src/utils/contextAnalytics.ts +103 -0
- package/src/utils/events.ts +32 -0
- package/src/utils/graph.ts +11 -0
- package/src/utils/handlers.ts +107 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/llm.ts +26 -0
- package/src/utils/llmConfig.ts +208 -0
- package/src/utils/logging.ts +48 -0
- package/src/utils/misc.ts +57 -0
- package/src/utils/run.ts +106 -0
- package/src/utils/schema.ts +35 -0
- package/src/utils/title.ts +177 -0
- package/src/utils/tokens.ts +142 -0
- package/src/utils/toonFormat.ts +475 -0
|
@@ -0,0 +1,1028 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var openai$1 = require('openai');
|
|
4
|
+
var xai = require('@langchain/xai');
|
|
5
|
+
var outputs = require('@langchain/core/outputs');
|
|
6
|
+
var messages = require('@langchain/core/messages');
|
|
7
|
+
require('@langchain/core/utils/function_calling');
|
|
8
|
+
var deepseek = require('@langchain/deepseek');
|
|
9
|
+
var openai = require('@langchain/openai');
|
|
10
|
+
var index = require('./utils/index.cjs');
|
|
11
|
+
require('../../common/enum.cjs');
|
|
12
|
+
require('nanoid');
|
|
13
|
+
require('../../messages/core.cjs');
|
|
14
|
+
require('../../utils/toonFormat.cjs');
|
|
15
|
+
var run = require('../../utils/run.cjs');
|
|
16
|
+
require('js-tiktoken');
|
|
17
|
+
require('zod-to-json-schema');
|
|
18
|
+
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
20
|
+
const iife = (fn) => fn();
|
|
21
|
+
function isHeaders(headers) {
|
|
22
|
+
return (typeof Headers !== 'undefined' &&
|
|
23
|
+
headers !== null &&
|
|
24
|
+
typeof headers === 'object' &&
|
|
25
|
+
Object.prototype.toString.call(headers) === '[object Headers]');
|
|
26
|
+
}
|
|
27
|
+
function normalizeHeaders(headers) {
|
|
28
|
+
const output = iife(() => {
|
|
29
|
+
// If headers is a Headers instance
|
|
30
|
+
if (isHeaders(headers)) {
|
|
31
|
+
return headers;
|
|
32
|
+
}
|
|
33
|
+
// If headers is an array of [key, value] pairs
|
|
34
|
+
else if (Array.isArray(headers)) {
|
|
35
|
+
return new Headers(headers);
|
|
36
|
+
}
|
|
37
|
+
// If headers is a NullableHeaders-like object (has 'values' property that is a Headers)
|
|
38
|
+
else if (typeof headers === 'object' &&
|
|
39
|
+
headers !== null &&
|
|
40
|
+
'values' in headers &&
|
|
41
|
+
isHeaders(headers.values)) {
|
|
42
|
+
return headers.values;
|
|
43
|
+
}
|
|
44
|
+
// If headers is a plain object
|
|
45
|
+
else if (typeof headers === 'object' && headers !== null) {
|
|
46
|
+
const entries = Object.entries(headers)
|
|
47
|
+
.filter(([, v]) => typeof v === 'string')
|
|
48
|
+
.map(([k, v]) => [k, v]);
|
|
49
|
+
return new Headers(entries);
|
|
50
|
+
}
|
|
51
|
+
return new Headers();
|
|
52
|
+
});
|
|
53
|
+
return Object.fromEntries(output.entries());
|
|
54
|
+
}
|
|
55
|
+
function createAbortHandler(controller) {
|
|
56
|
+
return function () {
|
|
57
|
+
controller.abort();
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
class CustomOpenAIClient extends openai.OpenAIClient {
|
|
61
|
+
abortHandler;
|
|
62
|
+
async fetchWithTimeout(url, init, ms, controller) {
|
|
63
|
+
const { signal, ...options } = init || {};
|
|
64
|
+
const handler = createAbortHandler(controller);
|
|
65
|
+
this.abortHandler = handler;
|
|
66
|
+
if (signal)
|
|
67
|
+
signal.addEventListener('abort', handler, { once: true });
|
|
68
|
+
const timeout = setTimeout(() => handler, ms);
|
|
69
|
+
const fetchOptions = {
|
|
70
|
+
signal: controller.signal,
|
|
71
|
+
...options,
|
|
72
|
+
};
|
|
73
|
+
if (fetchOptions.method != null) {
|
|
74
|
+
// Custom methods like 'patch' need to be uppercased
|
|
75
|
+
// See https://github.com/nodejs/undici/issues/2294
|
|
76
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
77
|
+
}
|
|
78
|
+
return (
|
|
79
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
81
|
+
/** @ts-ignore */
|
|
82
|
+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
83
|
+
clearTimeout(timeout);
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
class CustomAzureOpenAIClient extends openai$1.AzureOpenAI {
|
|
88
|
+
abortHandler;
|
|
89
|
+
async fetchWithTimeout(url, init, ms, controller) {
|
|
90
|
+
const { signal, ...options } = init || {};
|
|
91
|
+
const handler = createAbortHandler(controller);
|
|
92
|
+
this.abortHandler = handler;
|
|
93
|
+
if (signal)
|
|
94
|
+
signal.addEventListener('abort', handler, { once: true });
|
|
95
|
+
const timeout = setTimeout(() => handler, ms);
|
|
96
|
+
const fetchOptions = {
|
|
97
|
+
signal: controller.signal,
|
|
98
|
+
...options,
|
|
99
|
+
};
|
|
100
|
+
if (fetchOptions.method != null) {
|
|
101
|
+
// Custom methods like 'patch' need to be uppercased
|
|
102
|
+
// See https://github.com/nodejs/undici/issues/2294
|
|
103
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
104
|
+
}
|
|
105
|
+
return (
|
|
106
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
108
|
+
/** @ts-ignore */
|
|
109
|
+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
110
|
+
clearTimeout(timeout);
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** @ts-expect-error We are intentionally overriding `getReasoningParams` */
|
|
115
|
+
class ChatOpenAI extends openai.ChatOpenAI {
|
|
116
|
+
_lc_stream_delay;
|
|
117
|
+
constructor(fields) {
|
|
118
|
+
super(fields);
|
|
119
|
+
this._lc_stream_delay = fields?._lc_stream_delay;
|
|
120
|
+
}
|
|
121
|
+
get exposedClient() {
|
|
122
|
+
return this.client;
|
|
123
|
+
}
|
|
124
|
+
static lc_name() {
|
|
125
|
+
return 'IllumaOpenAI';
|
|
126
|
+
}
|
|
127
|
+
_getClientOptions(options) {
|
|
128
|
+
if (!this.client) {
|
|
129
|
+
const openAIEndpointConfig = {
|
|
130
|
+
baseURL: this.clientConfig.baseURL,
|
|
131
|
+
};
|
|
132
|
+
const endpoint = openai.getEndpoint(openAIEndpointConfig);
|
|
133
|
+
const params = {
|
|
134
|
+
...this.clientConfig,
|
|
135
|
+
baseURL: endpoint,
|
|
136
|
+
timeout: this.timeout,
|
|
137
|
+
maxRetries: 0,
|
|
138
|
+
};
|
|
139
|
+
if (params.baseURL == null) {
|
|
140
|
+
delete params.baseURL;
|
|
141
|
+
}
|
|
142
|
+
this.client = new CustomOpenAIClient(params);
|
|
143
|
+
}
|
|
144
|
+
const requestOptions = {
|
|
145
|
+
...this.clientConfig,
|
|
146
|
+
...options,
|
|
147
|
+
};
|
|
148
|
+
return requestOptions;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Returns backwards compatible reasoning parameters from constructor params and call options
|
|
152
|
+
* @internal
|
|
153
|
+
*/
|
|
154
|
+
getReasoningParams(options) {
|
|
155
|
+
// apply options in reverse order of importance -- newer options supersede older options
|
|
156
|
+
let reasoning;
|
|
157
|
+
if (this.reasoning !== undefined) {
|
|
158
|
+
reasoning = {
|
|
159
|
+
...reasoning,
|
|
160
|
+
...this.reasoning,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (options?.reasoning !== undefined) {
|
|
164
|
+
reasoning = {
|
|
165
|
+
...reasoning,
|
|
166
|
+
...options.reasoning,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return reasoning;
|
|
170
|
+
}
|
|
171
|
+
_getReasoningParams(options) {
|
|
172
|
+
return this.getReasoningParams(options);
|
|
173
|
+
}
|
|
174
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
175
|
+
if (!this._useResponseApi(options)) {
|
|
176
|
+
return yield* this._streamResponseChunks2(messages, options, runManager);
|
|
177
|
+
}
|
|
178
|
+
const streamIterable = await this.responseApiWithRetry({
|
|
179
|
+
...this.invocationParams(options, { streaming: true }),
|
|
180
|
+
input: index._convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled),
|
|
181
|
+
stream: true,
|
|
182
|
+
}, options);
|
|
183
|
+
for await (const data of streamIterable) {
|
|
184
|
+
const chunk = index._convertOpenAIResponsesDeltaToBaseMessageChunk(data);
|
|
185
|
+
if (chunk == null)
|
|
186
|
+
continue;
|
|
187
|
+
yield chunk;
|
|
188
|
+
if (this._lc_stream_delay != null) {
|
|
189
|
+
await run.sleep(this._lc_stream_delay);
|
|
190
|
+
}
|
|
191
|
+
await runManager?.handleLLMNewToken(chunk.text || '', undefined, undefined, undefined, undefined, { chunk });
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
async *_streamResponseChunks2(messages$1, options, runManager) {
|
|
196
|
+
const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model);
|
|
197
|
+
const params = {
|
|
198
|
+
...this.invocationParams(options, {
|
|
199
|
+
streaming: true,
|
|
200
|
+
}),
|
|
201
|
+
messages: messagesMapped,
|
|
202
|
+
stream: true,
|
|
203
|
+
};
|
|
204
|
+
let defaultRole;
|
|
205
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
206
|
+
let usage;
|
|
207
|
+
for await (const data of streamIterable) {
|
|
208
|
+
const choice = data.choices[0];
|
|
209
|
+
if (data.usage) {
|
|
210
|
+
usage = data.usage;
|
|
211
|
+
}
|
|
212
|
+
if (!choice) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const { delta } = choice;
|
|
216
|
+
if (!delta) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
220
|
+
if ('reasoning_content' in delta) {
|
|
221
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
|
|
222
|
+
}
|
|
223
|
+
else if ('reasoning' in delta) {
|
|
224
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning;
|
|
225
|
+
}
|
|
226
|
+
if ('provider_specific_fields' in delta) {
|
|
227
|
+
chunk.additional_kwargs.provider_specific_fields =
|
|
228
|
+
delta.provider_specific_fields;
|
|
229
|
+
}
|
|
230
|
+
defaultRole = delta.role ?? defaultRole;
|
|
231
|
+
const newTokenIndices = {
|
|
232
|
+
prompt: options.promptIndex ?? 0,
|
|
233
|
+
completion: choice.index ?? 0,
|
|
234
|
+
};
|
|
235
|
+
if (typeof chunk.content !== 'string') {
|
|
236
|
+
// eslint-disable-next-line no-console
|
|
237
|
+
console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
241
|
+
const generationInfo = { ...newTokenIndices };
|
|
242
|
+
if (choice.finish_reason != null) {
|
|
243
|
+
generationInfo.finish_reason = choice.finish_reason;
|
|
244
|
+
// Only include system fingerprint in the last chunk for now
|
|
245
|
+
// to avoid concatenation issues
|
|
246
|
+
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
247
|
+
generationInfo.model_name = data.model;
|
|
248
|
+
generationInfo.service_tier = data.service_tier;
|
|
249
|
+
}
|
|
250
|
+
if (this.logprobs == true) {
|
|
251
|
+
generationInfo.logprobs = choice.logprobs;
|
|
252
|
+
}
|
|
253
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
254
|
+
message: chunk,
|
|
255
|
+
text: chunk.content,
|
|
256
|
+
generationInfo,
|
|
257
|
+
});
|
|
258
|
+
yield generationChunk;
|
|
259
|
+
if (this._lc_stream_delay != null) {
|
|
260
|
+
await run.sleep(this._lc_stream_delay);
|
|
261
|
+
}
|
|
262
|
+
await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
263
|
+
}
|
|
264
|
+
if (usage) {
|
|
265
|
+
const inputTokenDetails = {
|
|
266
|
+
...(usage.prompt_tokens_details?.audio_tokens != null && {
|
|
267
|
+
audio: usage.prompt_tokens_details.audio_tokens,
|
|
268
|
+
}),
|
|
269
|
+
...(usage.prompt_tokens_details?.cached_tokens != null && {
|
|
270
|
+
cache_read: usage.prompt_tokens_details.cached_tokens,
|
|
271
|
+
}),
|
|
272
|
+
};
|
|
273
|
+
const outputTokenDetails = {
|
|
274
|
+
...(usage.completion_tokens_details?.audio_tokens != null && {
|
|
275
|
+
audio: usage.completion_tokens_details.audio_tokens,
|
|
276
|
+
}),
|
|
277
|
+
...(usage.completion_tokens_details?.reasoning_tokens != null && {
|
|
278
|
+
reasoning: usage.completion_tokens_details.reasoning_tokens,
|
|
279
|
+
}),
|
|
280
|
+
};
|
|
281
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
282
|
+
message: new messages.AIMessageChunk({
|
|
283
|
+
content: '',
|
|
284
|
+
response_metadata: {
|
|
285
|
+
usage: { ...usage },
|
|
286
|
+
},
|
|
287
|
+
usage_metadata: {
|
|
288
|
+
input_tokens: usage.prompt_tokens,
|
|
289
|
+
output_tokens: usage.completion_tokens,
|
|
290
|
+
total_tokens: usage.total_tokens,
|
|
291
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
292
|
+
input_token_details: inputTokenDetails,
|
|
293
|
+
}),
|
|
294
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
295
|
+
output_token_details: outputTokenDetails,
|
|
296
|
+
}),
|
|
297
|
+
},
|
|
298
|
+
}),
|
|
299
|
+
text: '',
|
|
300
|
+
});
|
|
301
|
+
yield generationChunk;
|
|
302
|
+
if (this._lc_stream_delay != null) {
|
|
303
|
+
await run.sleep(this._lc_stream_delay);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (options.signal?.aborted === true) {
|
|
307
|
+
throw new Error('AbortError');
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/** @ts-expect-error We are intentionally overriding `getReasoningParams` */
|
|
312
|
+
class AzureChatOpenAI extends openai.AzureChatOpenAI {
|
|
313
|
+
_lc_stream_delay;
|
|
314
|
+
constructor(fields) {
|
|
315
|
+
super(fields);
|
|
316
|
+
this._lc_stream_delay = fields?._lc_stream_delay;
|
|
317
|
+
}
|
|
318
|
+
get exposedClient() {
|
|
319
|
+
return this.client;
|
|
320
|
+
}
|
|
321
|
+
static lc_name() {
|
|
322
|
+
return 'IllumaAzureOpenAI';
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Returns backwards compatible reasoning parameters from constructor params and call options
|
|
326
|
+
* @internal
|
|
327
|
+
*/
|
|
328
|
+
getReasoningParams(options) {
|
|
329
|
+
if (!index.isReasoningModel(this.model)) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
// apply options in reverse order of importance -- newer options supersede older options
|
|
333
|
+
let reasoning;
|
|
334
|
+
if (this.reasoning !== undefined) {
|
|
335
|
+
reasoning = {
|
|
336
|
+
...reasoning,
|
|
337
|
+
...this.reasoning,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
if (options?.reasoning !== undefined) {
|
|
341
|
+
reasoning = {
|
|
342
|
+
...reasoning,
|
|
343
|
+
...options.reasoning,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return reasoning;
|
|
347
|
+
}
|
|
348
|
+
_getReasoningParams(options) {
|
|
349
|
+
return this.getReasoningParams(options);
|
|
350
|
+
}
|
|
351
|
+
_getClientOptions(options) {
|
|
352
|
+
if (!this.client) {
|
|
353
|
+
const openAIEndpointConfig = {
|
|
354
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
355
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
356
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
357
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
358
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
359
|
+
baseURL: this.clientConfig.baseURL,
|
|
360
|
+
};
|
|
361
|
+
const endpoint = openai.getEndpoint(openAIEndpointConfig);
|
|
362
|
+
const params = {
|
|
363
|
+
...this.clientConfig,
|
|
364
|
+
baseURL: endpoint,
|
|
365
|
+
timeout: this.timeout,
|
|
366
|
+
maxRetries: 0,
|
|
367
|
+
};
|
|
368
|
+
if (!this.azureADTokenProvider) {
|
|
369
|
+
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
|
|
370
|
+
}
|
|
371
|
+
if (params.baseURL == null) {
|
|
372
|
+
delete params.baseURL;
|
|
373
|
+
}
|
|
374
|
+
const defaultHeaders = normalizeHeaders(params.defaultHeaders);
|
|
375
|
+
params.defaultHeaders = {
|
|
376
|
+
...params.defaultHeaders,
|
|
377
|
+
'User-Agent': defaultHeaders['User-Agent'] != null
|
|
378
|
+
? `${defaultHeaders['User-Agent']}: illuma-azure-openai-v2`
|
|
379
|
+
: 'illuma-azure-openai-v2',
|
|
380
|
+
};
|
|
381
|
+
this.client = new CustomAzureOpenAIClient({
|
|
382
|
+
apiVersion: this.azureOpenAIApiVersion,
|
|
383
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
384
|
+
...params,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
const requestOptions = {
|
|
388
|
+
...this.clientConfig,
|
|
389
|
+
...options,
|
|
390
|
+
};
|
|
391
|
+
if (this.azureOpenAIApiKey != null) {
|
|
392
|
+
requestOptions.headers = {
|
|
393
|
+
'api-key': this.azureOpenAIApiKey,
|
|
394
|
+
...requestOptions.headers,
|
|
395
|
+
};
|
|
396
|
+
requestOptions.query = {
|
|
397
|
+
'api-version': this.azureOpenAIApiVersion,
|
|
398
|
+
...requestOptions.query,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
return requestOptions;
|
|
402
|
+
}
|
|
403
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
404
|
+
if (!this._useResponseApi(options)) {
|
|
405
|
+
return yield* super._streamResponseChunks(messages, options, runManager);
|
|
406
|
+
}
|
|
407
|
+
const streamIterable = await this.responseApiWithRetry({
|
|
408
|
+
...this.invocationParams(options, { streaming: true }),
|
|
409
|
+
input: index._convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled),
|
|
410
|
+
stream: true,
|
|
411
|
+
}, options);
|
|
412
|
+
for await (const data of streamIterable) {
|
|
413
|
+
const chunk = index._convertOpenAIResponsesDeltaToBaseMessageChunk(data);
|
|
414
|
+
if (chunk == null)
|
|
415
|
+
continue;
|
|
416
|
+
yield chunk;
|
|
417
|
+
if (this._lc_stream_delay != null) {
|
|
418
|
+
await run.sleep(this._lc_stream_delay);
|
|
419
|
+
}
|
|
420
|
+
await runManager?.handleLLMNewToken(chunk.text || '', undefined, undefined, undefined, undefined, { chunk });
|
|
421
|
+
}
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
class ChatDeepSeek extends deepseek.ChatDeepSeek {
|
|
426
|
+
get exposedClient() {
|
|
427
|
+
return this.client;
|
|
428
|
+
}
|
|
429
|
+
static lc_name() {
|
|
430
|
+
return 'IllumaDeepSeek';
|
|
431
|
+
}
|
|
432
|
+
_convertMessages(messages) {
|
|
433
|
+
return index._convertMessagesToOpenAIParams(messages, this.model, {
|
|
434
|
+
includeReasoningContent: true,
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
async _generate(messages, options, runManager) {
|
|
438
|
+
const params = this.invocationParams(options);
|
|
439
|
+
if (params.stream === true) {
|
|
440
|
+
return super._generate(messages, options ?? {}, runManager);
|
|
441
|
+
}
|
|
442
|
+
const messagesMapped = this._convertMessages(messages);
|
|
443
|
+
const data = await this.completionWithRetry({
|
|
444
|
+
...params,
|
|
445
|
+
stream: false,
|
|
446
|
+
messages: messagesMapped,
|
|
447
|
+
}, {
|
|
448
|
+
signal: options?.signal,
|
|
449
|
+
...options?.options,
|
|
450
|
+
});
|
|
451
|
+
const { completion_tokens, prompt_tokens, total_tokens } = data.usage ?? {};
|
|
452
|
+
const generations = [];
|
|
453
|
+
for (const part of data.choices ?? []) {
|
|
454
|
+
const text = part.message.content ?? '';
|
|
455
|
+
const generation = {
|
|
456
|
+
text: typeof text === 'string' ? text : '',
|
|
457
|
+
message: this._convertResponseToMessage(part, data),
|
|
458
|
+
};
|
|
459
|
+
generation.generationInfo = {
|
|
460
|
+
...(part.finish_reason != null
|
|
461
|
+
? { finish_reason: part.finish_reason }
|
|
462
|
+
: {}),
|
|
463
|
+
...(part.logprobs ? { logprobs: part.logprobs } : {}),
|
|
464
|
+
};
|
|
465
|
+
generations.push(generation);
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
generations,
|
|
469
|
+
llmOutput: {
|
|
470
|
+
tokenUsage: {
|
|
471
|
+
completionTokens: completion_tokens,
|
|
472
|
+
promptTokens: prompt_tokens,
|
|
473
|
+
totalTokens: total_tokens,
|
|
474
|
+
},
|
|
475
|
+
},
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
_convertResponseToMessage(choice, data) {
|
|
479
|
+
const { message } = choice;
|
|
480
|
+
const rawToolCalls = message.tool_calls;
|
|
481
|
+
const toolCalls = rawToolCalls?.map((tc) => ({
|
|
482
|
+
id: tc.id,
|
|
483
|
+
name: tc.function.name,
|
|
484
|
+
args: JSON.parse(tc.function.arguments || '{}'),
|
|
485
|
+
type: 'tool_call',
|
|
486
|
+
}));
|
|
487
|
+
const additional_kwargs = {};
|
|
488
|
+
if (rawToolCalls) {
|
|
489
|
+
additional_kwargs.tool_calls = rawToolCalls;
|
|
490
|
+
}
|
|
491
|
+
if ('reasoning_content' in message &&
|
|
492
|
+
message.reasoning_content != null &&
|
|
493
|
+
message.reasoning_content !== '') {
|
|
494
|
+
additional_kwargs.reasoning_content = message.reasoning_content;
|
|
495
|
+
}
|
|
496
|
+
return new messages.AIMessage({
|
|
497
|
+
content: message.content ?? '',
|
|
498
|
+
tool_calls: toolCalls,
|
|
499
|
+
additional_kwargs,
|
|
500
|
+
usage_metadata: data.usage
|
|
501
|
+
? {
|
|
502
|
+
input_tokens: data.usage.prompt_tokens,
|
|
503
|
+
output_tokens: data.usage.completion_tokens,
|
|
504
|
+
total_tokens: data.usage.total_tokens,
|
|
505
|
+
}
|
|
506
|
+
: undefined,
|
|
507
|
+
response_metadata: {
|
|
508
|
+
model_name: data.model,
|
|
509
|
+
system_fingerprint: data.system_fingerprint,
|
|
510
|
+
finish_reason: choice.finish_reason,
|
|
511
|
+
},
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
_getClientOptions(options) {
|
|
515
|
+
if (!this.client) {
|
|
516
|
+
const openAIEndpointConfig = {
|
|
517
|
+
baseURL: this.clientConfig.baseURL,
|
|
518
|
+
};
|
|
519
|
+
const endpoint = openai.getEndpoint(openAIEndpointConfig);
|
|
520
|
+
const params = {
|
|
521
|
+
...this.clientConfig,
|
|
522
|
+
baseURL: endpoint,
|
|
523
|
+
timeout: this.timeout,
|
|
524
|
+
maxRetries: 0,
|
|
525
|
+
};
|
|
526
|
+
if (params.baseURL == null) {
|
|
527
|
+
delete params.baseURL;
|
|
528
|
+
}
|
|
529
|
+
this.client = new CustomOpenAIClient(params);
|
|
530
|
+
}
|
|
531
|
+
const requestOptions = {
|
|
532
|
+
...this.clientConfig,
|
|
533
|
+
...options,
|
|
534
|
+
};
|
|
535
|
+
return requestOptions;
|
|
536
|
+
}
|
|
537
|
+
async *_streamResponseChunks(messages$1, options, runManager) {
|
|
538
|
+
const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model, {
|
|
539
|
+
includeReasoningContent: true,
|
|
540
|
+
});
|
|
541
|
+
const params = {
|
|
542
|
+
...this.invocationParams(options, {
|
|
543
|
+
streaming: true,
|
|
544
|
+
}),
|
|
545
|
+
messages: messagesMapped,
|
|
546
|
+
stream: true,
|
|
547
|
+
};
|
|
548
|
+
let defaultRole;
|
|
549
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
550
|
+
let usage;
|
|
551
|
+
for await (const data of streamIterable) {
|
|
552
|
+
const choice = data.choices[0];
|
|
553
|
+
if (data.usage) {
|
|
554
|
+
usage = data.usage;
|
|
555
|
+
}
|
|
556
|
+
if (!choice) {
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
const { delta } = choice;
|
|
560
|
+
if (!delta) {
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
564
|
+
if ('reasoning_content' in delta) {
|
|
565
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
|
|
566
|
+
}
|
|
567
|
+
defaultRole = delta.role ?? defaultRole;
|
|
568
|
+
const newTokenIndices = {
|
|
569
|
+
prompt: options.promptIndex ?? 0,
|
|
570
|
+
completion: choice.index ?? 0,
|
|
571
|
+
};
|
|
572
|
+
if (typeof chunk.content !== 'string') {
|
|
573
|
+
// eslint-disable-next-line no-console
|
|
574
|
+
console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
578
|
+
const generationInfo = { ...newTokenIndices };
|
|
579
|
+
if (choice.finish_reason != null) {
|
|
580
|
+
generationInfo.finish_reason = choice.finish_reason;
|
|
581
|
+
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
582
|
+
generationInfo.model_name = data.model;
|
|
583
|
+
generationInfo.service_tier = data.service_tier;
|
|
584
|
+
}
|
|
585
|
+
if (this.logprobs == true) {
|
|
586
|
+
generationInfo.logprobs = choice.logprobs;
|
|
587
|
+
}
|
|
588
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
589
|
+
message: chunk,
|
|
590
|
+
text: chunk.content,
|
|
591
|
+
generationInfo,
|
|
592
|
+
});
|
|
593
|
+
yield generationChunk;
|
|
594
|
+
await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
595
|
+
}
|
|
596
|
+
if (usage) {
|
|
597
|
+
const inputTokenDetails = {
|
|
598
|
+
...(usage.prompt_tokens_details?.audio_tokens != null && {
|
|
599
|
+
audio: usage.prompt_tokens_details.audio_tokens,
|
|
600
|
+
}),
|
|
601
|
+
...(usage.prompt_tokens_details?.cached_tokens != null && {
|
|
602
|
+
cache_read: usage.prompt_tokens_details.cached_tokens,
|
|
603
|
+
}),
|
|
604
|
+
};
|
|
605
|
+
const outputTokenDetails = {
|
|
606
|
+
...(usage.completion_tokens_details?.audio_tokens != null && {
|
|
607
|
+
audio: usage.completion_tokens_details.audio_tokens,
|
|
608
|
+
}),
|
|
609
|
+
...(usage.completion_tokens_details?.reasoning_tokens != null && {
|
|
610
|
+
reasoning: usage.completion_tokens_details.reasoning_tokens,
|
|
611
|
+
}),
|
|
612
|
+
};
|
|
613
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
614
|
+
message: new messages.AIMessageChunk({
|
|
615
|
+
content: '',
|
|
616
|
+
response_metadata: {
|
|
617
|
+
usage: { ...usage },
|
|
618
|
+
},
|
|
619
|
+
usage_metadata: {
|
|
620
|
+
input_tokens: usage.prompt_tokens,
|
|
621
|
+
output_tokens: usage.completion_tokens,
|
|
622
|
+
total_tokens: usage.total_tokens,
|
|
623
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
624
|
+
input_token_details: inputTokenDetails,
|
|
625
|
+
}),
|
|
626
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
627
|
+
output_token_details: outputTokenDetails,
|
|
628
|
+
}),
|
|
629
|
+
},
|
|
630
|
+
}),
|
|
631
|
+
text: '',
|
|
632
|
+
});
|
|
633
|
+
yield generationChunk;
|
|
634
|
+
}
|
|
635
|
+
if (options.signal?.aborted === true) {
|
|
636
|
+
throw new Error('AbortError');
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
class ChatMoonshot extends ChatOpenAI {
|
|
641
|
+
static lc_name() {
|
|
642
|
+
return 'LibreChatMoonshot';
|
|
643
|
+
}
|
|
644
|
+
_convertMessages(messages) {
|
|
645
|
+
return index._convertMessagesToOpenAIParams(messages, this.model, {
|
|
646
|
+
includeReasoningContent: true,
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
async _generate(messages, options, runManager) {
|
|
650
|
+
const params = this.invocationParams(options);
|
|
651
|
+
if (params.stream === true) {
|
|
652
|
+
return super._generate(messages, options, runManager);
|
|
653
|
+
}
|
|
654
|
+
const messagesMapped = this._convertMessages(messages);
|
|
655
|
+
const data = await this.completionWithRetry({
|
|
656
|
+
...params,
|
|
657
|
+
stream: false,
|
|
658
|
+
messages: messagesMapped,
|
|
659
|
+
}, {
|
|
660
|
+
signal: options.signal,
|
|
661
|
+
...options.options,
|
|
662
|
+
});
|
|
663
|
+
const { completion_tokens, prompt_tokens, total_tokens } = data.usage ?? {};
|
|
664
|
+
const generations = [];
|
|
665
|
+
for (const part of data.choices ?? []) {
|
|
666
|
+
const text = part.message.content ?? '';
|
|
667
|
+
const generation = {
|
|
668
|
+
text: typeof text === 'string' ? text : '',
|
|
669
|
+
message: this._convertResponseToMessage(part, data),
|
|
670
|
+
};
|
|
671
|
+
generation.generationInfo = {
|
|
672
|
+
...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
|
|
673
|
+
...(part.logprobs ? { logprobs: part.logprobs } : {}),
|
|
674
|
+
};
|
|
675
|
+
generations.push(generation);
|
|
676
|
+
}
|
|
677
|
+
return {
|
|
678
|
+
generations,
|
|
679
|
+
llmOutput: {
|
|
680
|
+
tokenUsage: {
|
|
681
|
+
completionTokens: completion_tokens,
|
|
682
|
+
promptTokens: prompt_tokens,
|
|
683
|
+
totalTokens: total_tokens,
|
|
684
|
+
},
|
|
685
|
+
},
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
_convertResponseToMessage(choice, data) {
|
|
689
|
+
const { message } = choice;
|
|
690
|
+
const rawToolCalls = message.tool_calls;
|
|
691
|
+
const toolCalls = rawToolCalls?.map((tc) => ({
|
|
692
|
+
id: tc.id,
|
|
693
|
+
name: tc.function.name,
|
|
694
|
+
args: JSON.parse(tc.function.arguments || '{}'),
|
|
695
|
+
type: 'tool_call',
|
|
696
|
+
}));
|
|
697
|
+
const additional_kwargs = {};
|
|
698
|
+
if (rawToolCalls) {
|
|
699
|
+
additional_kwargs.tool_calls = rawToolCalls;
|
|
700
|
+
}
|
|
701
|
+
if ('reasoning_content' in message &&
|
|
702
|
+
message.reasoning_content != null &&
|
|
703
|
+
message.reasoning_content !== '') {
|
|
704
|
+
additional_kwargs.reasoning_content = message.reasoning_content;
|
|
705
|
+
}
|
|
706
|
+
return new messages.AIMessage({
|
|
707
|
+
content: message.content ?? '',
|
|
708
|
+
tool_calls: toolCalls,
|
|
709
|
+
additional_kwargs,
|
|
710
|
+
usage_metadata: data.usage
|
|
711
|
+
? {
|
|
712
|
+
input_tokens: data.usage.prompt_tokens,
|
|
713
|
+
output_tokens: data.usage.completion_tokens,
|
|
714
|
+
total_tokens: data.usage.total_tokens,
|
|
715
|
+
}
|
|
716
|
+
: undefined,
|
|
717
|
+
response_metadata: {
|
|
718
|
+
model_name: data.model,
|
|
719
|
+
system_fingerprint: data.system_fingerprint,
|
|
720
|
+
finish_reason: choice.finish_reason,
|
|
721
|
+
},
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
async *_streamResponseChunks(messages$1, options, runManager) {
|
|
725
|
+
const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model, {
|
|
726
|
+
includeReasoningContent: true,
|
|
727
|
+
});
|
|
728
|
+
const params = {
|
|
729
|
+
...this.invocationParams(options, {
|
|
730
|
+
streaming: true,
|
|
731
|
+
}),
|
|
732
|
+
messages: messagesMapped,
|
|
733
|
+
stream: true,
|
|
734
|
+
};
|
|
735
|
+
let defaultRole;
|
|
736
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
737
|
+
let usage;
|
|
738
|
+
for await (const data of streamIterable) {
|
|
739
|
+
const choice = data.choices[0];
|
|
740
|
+
if (data.usage) {
|
|
741
|
+
usage = data.usage;
|
|
742
|
+
}
|
|
743
|
+
if (!choice) {
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
const { delta } = choice;
|
|
747
|
+
if (!delta) {
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
751
|
+
if ('reasoning_content' in delta) {
|
|
752
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
|
|
753
|
+
}
|
|
754
|
+
defaultRole = delta.role ?? defaultRole;
|
|
755
|
+
const newTokenIndices = {
|
|
756
|
+
prompt: options.promptIndex ?? 0,
|
|
757
|
+
completion: choice.index ?? 0,
|
|
758
|
+
};
|
|
759
|
+
if (typeof chunk.content !== 'string') {
|
|
760
|
+
// eslint-disable-next-line no-console
|
|
761
|
+
console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
765
|
+
const generationInfo = { ...newTokenIndices };
|
|
766
|
+
if (choice.finish_reason != null) {
|
|
767
|
+
generationInfo.finish_reason = choice.finish_reason;
|
|
768
|
+
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
769
|
+
generationInfo.model_name = data.model;
|
|
770
|
+
generationInfo.service_tier = data.service_tier;
|
|
771
|
+
}
|
|
772
|
+
if (this.logprobs == true) {
|
|
773
|
+
generationInfo.logprobs = choice.logprobs;
|
|
774
|
+
}
|
|
775
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
776
|
+
message: chunk,
|
|
777
|
+
text: chunk.content,
|
|
778
|
+
generationInfo,
|
|
779
|
+
});
|
|
780
|
+
yield generationChunk;
|
|
781
|
+
if (this._lc_stream_delay != null) {
|
|
782
|
+
await run.sleep(this._lc_stream_delay);
|
|
783
|
+
}
|
|
784
|
+
await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
785
|
+
}
|
|
786
|
+
if (usage) {
|
|
787
|
+
const inputTokenDetails = {
|
|
788
|
+
...(usage.prompt_tokens_details?.audio_tokens != null && {
|
|
789
|
+
audio: usage.prompt_tokens_details.audio_tokens,
|
|
790
|
+
}),
|
|
791
|
+
...(usage.prompt_tokens_details?.cached_tokens != null && {
|
|
792
|
+
cache_read: usage.prompt_tokens_details.cached_tokens,
|
|
793
|
+
}),
|
|
794
|
+
};
|
|
795
|
+
const outputTokenDetails = {
|
|
796
|
+
...(usage.completion_tokens_details?.audio_tokens != null && {
|
|
797
|
+
audio: usage.completion_tokens_details.audio_tokens,
|
|
798
|
+
}),
|
|
799
|
+
...(usage.completion_tokens_details?.reasoning_tokens != null && {
|
|
800
|
+
reasoning: usage.completion_tokens_details.reasoning_tokens,
|
|
801
|
+
}),
|
|
802
|
+
};
|
|
803
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
804
|
+
message: new messages.AIMessageChunk({
|
|
805
|
+
content: '',
|
|
806
|
+
response_metadata: {
|
|
807
|
+
usage: { ...usage },
|
|
808
|
+
},
|
|
809
|
+
usage_metadata: {
|
|
810
|
+
input_tokens: usage.prompt_tokens,
|
|
811
|
+
output_tokens: usage.completion_tokens,
|
|
812
|
+
total_tokens: usage.total_tokens,
|
|
813
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
814
|
+
input_token_details: inputTokenDetails,
|
|
815
|
+
}),
|
|
816
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
817
|
+
output_token_details: outputTokenDetails,
|
|
818
|
+
}),
|
|
819
|
+
},
|
|
820
|
+
}),
|
|
821
|
+
text: '',
|
|
822
|
+
});
|
|
823
|
+
yield generationChunk;
|
|
824
|
+
if (this._lc_stream_delay != null) {
|
|
825
|
+
await run.sleep(this._lc_stream_delay);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
if (options.signal?.aborted === true) {
|
|
829
|
+
throw new Error('AbortError');
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
class ChatXAI extends xai.ChatXAI {
|
|
834
|
+
_lc_stream_delay;
|
|
835
|
+
constructor(fields) {
|
|
836
|
+
super(fields);
|
|
837
|
+
this._lc_stream_delay = fields?._lc_stream_delay;
|
|
838
|
+
const customBaseURL = fields?.configuration?.baseURL ?? fields?.clientConfig?.baseURL;
|
|
839
|
+
if (customBaseURL != null && customBaseURL) {
|
|
840
|
+
this.clientConfig = {
|
|
841
|
+
...this.clientConfig,
|
|
842
|
+
baseURL: customBaseURL,
|
|
843
|
+
};
|
|
844
|
+
// Reset the client to force recreation with new config
|
|
845
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
846
|
+
this.client = undefined;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
static lc_name() {
|
|
850
|
+
return 'IllumaXAI';
|
|
851
|
+
}
|
|
852
|
+
get exposedClient() {
|
|
853
|
+
return this.client;
|
|
854
|
+
}
|
|
855
|
+
_getClientOptions(options) {
|
|
856
|
+
if (!this.client) {
|
|
857
|
+
const openAIEndpointConfig = {
|
|
858
|
+
baseURL: this.clientConfig.baseURL,
|
|
859
|
+
};
|
|
860
|
+
const endpoint = openai.getEndpoint(openAIEndpointConfig);
|
|
861
|
+
const params = {
|
|
862
|
+
...this.clientConfig,
|
|
863
|
+
baseURL: endpoint,
|
|
864
|
+
timeout: this.timeout,
|
|
865
|
+
maxRetries: 0,
|
|
866
|
+
};
|
|
867
|
+
if (params.baseURL == null) {
|
|
868
|
+
delete params.baseURL;
|
|
869
|
+
}
|
|
870
|
+
this.client = new CustomOpenAIClient(params);
|
|
871
|
+
}
|
|
872
|
+
const requestOptions = {
|
|
873
|
+
...this.clientConfig,
|
|
874
|
+
...options,
|
|
875
|
+
};
|
|
876
|
+
return requestOptions;
|
|
877
|
+
}
|
|
878
|
+
async *_streamResponseChunks(messages$1, options, runManager) {
|
|
879
|
+
const messagesMapped = index._convertMessagesToOpenAIParams(messages$1, this.model);
|
|
880
|
+
const params = {
|
|
881
|
+
...this.invocationParams(options, {
|
|
882
|
+
streaming: true,
|
|
883
|
+
}),
|
|
884
|
+
messages: messagesMapped,
|
|
885
|
+
stream: true,
|
|
886
|
+
};
|
|
887
|
+
let defaultRole;
|
|
888
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
889
|
+
let usage;
|
|
890
|
+
for await (const data of streamIterable) {
|
|
891
|
+
const choice = data.choices[0];
|
|
892
|
+
if (data.usage) {
|
|
893
|
+
usage = data.usage;
|
|
894
|
+
}
|
|
895
|
+
if (!choice) {
|
|
896
|
+
continue;
|
|
897
|
+
}
|
|
898
|
+
const { delta } = choice;
|
|
899
|
+
if (!delta) {
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
|
|
903
|
+
if (chunk.usage_metadata != null) {
|
|
904
|
+
chunk.usage_metadata = {
|
|
905
|
+
input_tokens: chunk.usage_metadata.input_tokens ?? 0,
|
|
906
|
+
output_tokens: chunk.usage_metadata.output_tokens ?? 0,
|
|
907
|
+
total_tokens: chunk.usage_metadata.total_tokens ?? 0,
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
if ('reasoning_content' in delta) {
|
|
911
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
|
|
912
|
+
}
|
|
913
|
+
defaultRole = delta.role ?? defaultRole;
|
|
914
|
+
const newTokenIndices = {
|
|
915
|
+
prompt: options.promptIndex ?? 0,
|
|
916
|
+
completion: choice.index ?? 0,
|
|
917
|
+
};
|
|
918
|
+
if (typeof chunk.content !== 'string') {
|
|
919
|
+
// eslint-disable-next-line no-console
|
|
920
|
+
console.log('[WARNING]: Received non-string content from OpenAI. This is currently not supported.');
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
924
|
+
const generationInfo = { ...newTokenIndices };
|
|
925
|
+
if (choice.finish_reason != null) {
|
|
926
|
+
generationInfo.finish_reason = choice.finish_reason;
|
|
927
|
+
// Only include system fingerprint in the last chunk for now
|
|
928
|
+
// to avoid concatenation issues
|
|
929
|
+
generationInfo.system_fingerprint = data.system_fingerprint;
|
|
930
|
+
generationInfo.model_name = data.model;
|
|
931
|
+
generationInfo.service_tier = data.service_tier;
|
|
932
|
+
}
|
|
933
|
+
if (this.logprobs == true) {
|
|
934
|
+
generationInfo.logprobs = choice.logprobs;
|
|
935
|
+
}
|
|
936
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
937
|
+
message: chunk,
|
|
938
|
+
text: chunk.content,
|
|
939
|
+
generationInfo,
|
|
940
|
+
});
|
|
941
|
+
yield generationChunk;
|
|
942
|
+
if (this._lc_stream_delay != null) {
|
|
943
|
+
await run.sleep(this._lc_stream_delay);
|
|
944
|
+
}
|
|
945
|
+
await runManager?.handleLLMNewToken(generationChunk.text || '', newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
946
|
+
}
|
|
947
|
+
if (usage) {
|
|
948
|
+
// Type assertion for xAI-specific usage structure
|
|
949
|
+
const xaiUsage = usage;
|
|
950
|
+
const inputTokenDetails = {
|
|
951
|
+
// Standard OpenAI fields
|
|
952
|
+
...(usage.prompt_tokens_details?.audio_tokens != null && {
|
|
953
|
+
audio: usage.prompt_tokens_details.audio_tokens,
|
|
954
|
+
}),
|
|
955
|
+
...(usage.prompt_tokens_details?.cached_tokens != null && {
|
|
956
|
+
cache_read: usage.prompt_tokens_details.cached_tokens,
|
|
957
|
+
}),
|
|
958
|
+
// Add xAI-specific prompt token details if they exist
|
|
959
|
+
...(xaiUsage.prompt_tokens_details?.text_tokens != null && {
|
|
960
|
+
text: xaiUsage.prompt_tokens_details.text_tokens,
|
|
961
|
+
}),
|
|
962
|
+
...(xaiUsage.prompt_tokens_details?.image_tokens != null && {
|
|
963
|
+
image: xaiUsage.prompt_tokens_details.image_tokens,
|
|
964
|
+
}),
|
|
965
|
+
};
|
|
966
|
+
const outputTokenDetails = {
|
|
967
|
+
// Standard OpenAI fields
|
|
968
|
+
...(usage.completion_tokens_details?.audio_tokens != null && {
|
|
969
|
+
audio: usage.completion_tokens_details.audio_tokens,
|
|
970
|
+
}),
|
|
971
|
+
...(usage.completion_tokens_details?.reasoning_tokens != null && {
|
|
972
|
+
reasoning: usage.completion_tokens_details.reasoning_tokens,
|
|
973
|
+
}),
|
|
974
|
+
// Add xAI-specific completion token details if they exist
|
|
975
|
+
...(xaiUsage.completion_tokens_details?.accepted_prediction_tokens !=
|
|
976
|
+
null && {
|
|
977
|
+
accepted_prediction: xaiUsage.completion_tokens_details.accepted_prediction_tokens,
|
|
978
|
+
}),
|
|
979
|
+
...(xaiUsage.completion_tokens_details?.rejected_prediction_tokens !=
|
|
980
|
+
null && {
|
|
981
|
+
rejected_prediction: xaiUsage.completion_tokens_details.rejected_prediction_tokens,
|
|
982
|
+
}),
|
|
983
|
+
};
|
|
984
|
+
const generationChunk = new outputs.ChatGenerationChunk({
|
|
985
|
+
message: new messages.AIMessageChunk({
|
|
986
|
+
content: '',
|
|
987
|
+
response_metadata: {
|
|
988
|
+
usage: { ...usage },
|
|
989
|
+
// Include xAI-specific metadata if it exists
|
|
990
|
+
...(xaiUsage.num_sources_used != null && {
|
|
991
|
+
num_sources_used: xaiUsage.num_sources_used,
|
|
992
|
+
}),
|
|
993
|
+
},
|
|
994
|
+
usage_metadata: {
|
|
995
|
+
input_tokens: usage.prompt_tokens,
|
|
996
|
+
output_tokens: usage.completion_tokens,
|
|
997
|
+
total_tokens: usage.total_tokens,
|
|
998
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
999
|
+
input_token_details: inputTokenDetails,
|
|
1000
|
+
}),
|
|
1001
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
1002
|
+
output_token_details: outputTokenDetails,
|
|
1003
|
+
}),
|
|
1004
|
+
},
|
|
1005
|
+
}),
|
|
1006
|
+
text: '',
|
|
1007
|
+
});
|
|
1008
|
+
yield generationChunk;
|
|
1009
|
+
if (this._lc_stream_delay != null) {
|
|
1010
|
+
await run.sleep(this._lc_stream_delay);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
if (options.signal?.aborted === true) {
|
|
1014
|
+
throw new Error('AbortError');
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
exports.AzureChatOpenAI = AzureChatOpenAI;
|
|
1020
|
+
exports.ChatDeepSeek = ChatDeepSeek;
|
|
1021
|
+
exports.ChatMoonshot = ChatMoonshot;
|
|
1022
|
+
exports.ChatOpenAI = ChatOpenAI;
|
|
1023
|
+
exports.ChatXAI = ChatXAI;
|
|
1024
|
+
exports.CustomAzureOpenAIClient = CustomAzureOpenAIClient;
|
|
1025
|
+
exports.CustomOpenAIClient = CustomOpenAIClient;
|
|
1026
|
+
exports.isHeaders = isHeaders;
|
|
1027
|
+
exports.normalizeHeaders = normalizeHeaders;
|
|
1028
|
+
//# sourceMappingURL=index.cjs.map
|