illuma-agents 1.0.2
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/LICENSE +21 -0
- package/dist/cjs/common/enum.cjs +163 -0
- package/dist/cjs/common/enum.cjs.map +1 -0
- package/dist/cjs/events.cjs +143 -0
- package/dist/cjs/events.cjs.map +1 -0
- package/dist/cjs/graphs/Graph.cjs +581 -0
- package/dist/cjs/graphs/Graph.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 +553 -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/fake.cjs +97 -0
- package/dist/cjs/llm/fake.cjs.map +1 -0
- package/dist/cjs/llm/google/index.cjs +147 -0
- package/dist/cjs/llm/google/index.cjs.map +1 -0
- package/dist/cjs/llm/google/utils/common.cjs +490 -0
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
- package/dist/cjs/llm/ollama/index.cjs +70 -0
- package/dist/cjs/llm/ollama/index.cjs.map +1 -0
- package/dist/cjs/llm/ollama/utils.cjs +158 -0
- package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
- package/dist/cjs/llm/openai/index.cjs +613 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -0
- package/dist/cjs/llm/openai/utils/index.cjs +677 -0
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
- package/dist/cjs/llm/openrouter/index.cjs +29 -0
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -0
- package/dist/cjs/llm/providers.cjs +47 -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 +330 -0
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
- package/dist/cjs/main.cjs +127 -0
- package/dist/cjs/main.cjs.map +1 -0
- package/dist/cjs/messages/core.cjs +359 -0
- package/dist/cjs/messages/core.cjs.map +1 -0
- package/dist/cjs/messages/format.cjs +455 -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/run.cjs +264 -0
- package/dist/cjs/run.cjs.map +1 -0
- package/dist/cjs/splitStream.cjs +210 -0
- package/dist/cjs/splitStream.cjs.map +1 -0
- package/dist/cjs/stream.cjs +504 -0
- package/dist/cjs/stream.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +192 -0
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +125 -0
- package/dist/cjs/tools/ToolNode.cjs.map +1 -0
- package/dist/cjs/tools/handlers.cjs +250 -0
- package/dist/cjs/tools/handlers.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 +70 -0
- package/dist/cjs/tools/search/schema.cjs.map +1 -0
- package/dist/cjs/tools/search/search.cjs +561 -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 +331 -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/utils/graph.cjs +16 -0
- package/dist/cjs/utils/graph.cjs.map +1 -0
- package/dist/cjs/utils/llm.cjs +28 -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 +69 -0
- package/dist/cjs/utils/run.cjs.map +1 -0
- package/dist/cjs/utils/title.cjs +111 -0
- package/dist/cjs/utils/title.cjs.map +1 -0
- package/dist/cjs/utils/tokens.cjs +65 -0
- package/dist/cjs/utils/tokens.cjs.map +1 -0
- package/dist/esm/common/enum.mjs +163 -0
- package/dist/esm/common/enum.mjs.map +1 -0
- package/dist/esm/events.mjs +135 -0
- package/dist/esm/events.mjs.map +1 -0
- package/dist/esm/graphs/Graph.mjs +578 -0
- package/dist/esm/graphs/Graph.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 +550 -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/fake.mjs +94 -0
- package/dist/esm/llm/fake.mjs.map +1 -0
- package/dist/esm/llm/google/index.mjs +145 -0
- package/dist/esm/llm/google/index.mjs.map +1 -0
- package/dist/esm/llm/google/utils/common.mjs +484 -0
- package/dist/esm/llm/google/utils/common.mjs.map +1 -0
- package/dist/esm/llm/ollama/index.mjs +68 -0
- package/dist/esm/llm/ollama/index.mjs.map +1 -0
- package/dist/esm/llm/ollama/utils.mjs +155 -0
- package/dist/esm/llm/ollama/utils.mjs.map +1 -0
- package/dist/esm/llm/openai/index.mjs +604 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -0
- package/dist/esm/llm/openai/utils/index.mjs +671 -0
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
- package/dist/esm/llm/openrouter/index.mjs +27 -0
- package/dist/esm/llm/openrouter/index.mjs.map +1 -0
- package/dist/esm/llm/providers.mjs +43 -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 +328 -0
- package/dist/esm/llm/vertexai/index.mjs.map +1 -0
- package/dist/esm/main.mjs +20 -0
- package/dist/esm/main.mjs.map +1 -0
- package/dist/esm/messages/core.mjs +351 -0
- package/dist/esm/messages/core.mjs.map +1 -0
- package/dist/esm/messages/format.mjs +447 -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/run.mjs +261 -0
- package/dist/esm/run.mjs.map +1 -0
- package/dist/esm/splitStream.mjs +207 -0
- package/dist/esm/splitStream.mjs.map +1 -0
- package/dist/esm/stream.mjs +500 -0
- package/dist/esm/stream.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +188 -0
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +122 -0
- package/dist/esm/tools/ToolNode.mjs.map +1 -0
- package/dist/esm/tools/handlers.mjs +245 -0
- package/dist/esm/tools/handlers.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 +61 -0
- package/dist/esm/tools/search/schema.mjs.map +1 -0
- package/dist/esm/tools/search/search.mjs +558 -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 +329 -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/utils/graph.mjs +13 -0
- package/dist/esm/utils/graph.mjs.map +1 -0
- package/dist/esm/utils/llm.mjs +25 -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 +66 -0
- package/dist/esm/utils/run.mjs.map +1 -0
- package/dist/esm/utils/title.mjs +108 -0
- package/dist/esm/utils/title.mjs.map +1 -0
- package/dist/esm/utils/tokens.mjs +62 -0
- package/dist/esm/utils/tokens.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +128 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/events.d.ts +29 -0
- package/dist/types/graphs/Graph.d.ts +122 -0
- package/dist/types/graphs/index.d.ts +1 -0
- package/dist/types/index.d.ts +13 -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/fake.d.ts +31 -0
- package/dist/types/llm/google/index.d.ts +14 -0
- package/dist/types/llm/google/types.d.ts +32 -0
- package/dist/types/llm/google/utils/common.d.ts +19 -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/ollama/index.d.ts +8 -0
- package/dist/types/llm/ollama/utils.d.ts +7 -0
- package/dist/types/llm/openai/index.d.ts +103 -0
- package/dist/types/llm/openai/types.d.ts +10 -0
- package/dist/types/llm/openai/utils/index.d.ts +20 -0
- package/dist/types/llm/openrouter/index.d.ts +12 -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/core.d.ts +14 -0
- package/dist/types/messages/format.d.ts +113 -0
- package/dist/types/messages/ids.d.ts +3 -0
- package/dist/types/messages/index.d.ts +4 -0
- package/dist/types/messages/prune.d.ts +51 -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 +30 -0
- package/dist/types/scripts/abort.d.ts +1 -0
- package/dist/types/scripts/ant_web_search.d.ts +1 -0
- package/dist/types/scripts/args.d.ts +7 -0
- package/dist/types/scripts/caching.d.ts +1 -0
- package/dist/types/scripts/cli.d.ts +1 -0
- package/dist/types/scripts/cli2.d.ts +1 -0
- package/dist/types/scripts/cli3.d.ts +1 -0
- package/dist/types/scripts/cli4.d.ts +1 -0
- package/dist/types/scripts/cli5.d.ts +1 -0
- package/dist/types/scripts/code_exec.d.ts +1 -0
- package/dist/types/scripts/code_exec_files.d.ts +1 -0
- package/dist/types/scripts/code_exec_simple.d.ts +1 -0
- package/dist/types/scripts/content.d.ts +1 -0
- package/dist/types/scripts/empty_input.d.ts +1 -0
- package/dist/types/scripts/image.d.ts +1 -0
- package/dist/types/scripts/memory.d.ts +1 -0
- package/dist/types/scripts/search.d.ts +1 -0
- package/dist/types/scripts/simple.d.ts +1 -0
- package/dist/types/scripts/stream.d.ts +1 -0
- package/dist/types/scripts/thinking.d.ts +1 -0
- package/dist/types/scripts/tools.d.ts +1 -0
- package/dist/types/specs/spec.utils.d.ts +1 -0
- package/dist/types/splitStream.d.ts +37 -0
- package/dist/types/stream.d.ts +14 -0
- package/dist/types/tools/CodeExecutor.d.ts +23 -0
- package/dist/types/tools/ToolNode.d.ts +22 -0
- package/dist/types/tools/example.d.ts +78 -0
- package/dist/types/tools/handlers.d.ts +19 -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 +2 -0
- package/dist/types/tools/search/rerankers.d.ts +38 -0
- package/dist/types/tools/search/schema.d.ts +16 -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 +54 -0
- package/dist/types/tools/search/types.d.ts +591 -0
- package/dist/types/tools/search/utils.d.ts +10 -0
- package/dist/types/types/graph.d.ts +138 -0
- package/dist/types/types/index.d.ts +5 -0
- package/dist/types/types/llm.d.ts +102 -0
- package/dist/types/types/run.d.ts +74 -0
- package/dist/types/types/stream.d.ts +293 -0
- package/dist/types/types/tools.d.ts +61 -0
- package/dist/types/utils/graph.d.ts +2 -0
- package/dist/types/utils/index.d.ts +5 -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/title.d.ts +4 -0
- package/dist/types/utils/tokens.d.ts +3 -0
- package/package.json +145 -0
- package/src/common/enum.ts +176 -0
- package/src/common/index.ts +2 -0
- package/src/events.ts +191 -0
- package/src/graphs/Graph.ts +846 -0
- package/src/graphs/index.ts +1 -0
- package/src/index.ts +24 -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 +660 -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/fake.ts +133 -0
- package/src/llm/google/index.ts +222 -0
- package/src/llm/google/types.ts +43 -0
- package/src/llm/google/utils/common.ts +660 -0
- package/src/llm/google/utils/tools.ts +160 -0
- package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
- package/src/llm/ollama/index.ts +92 -0
- package/src/llm/ollama/utils.ts +193 -0
- package/src/llm/openai/index.ts +853 -0
- package/src/llm/openai/types.ts +24 -0
- package/src/llm/openai/utils/index.ts +918 -0
- package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
- package/src/llm/openrouter/index.ts +60 -0
- package/src/llm/providers.ts +57 -0
- package/src/llm/text.ts +94 -0
- package/src/llm/vertexai/index.ts +360 -0
- package/src/messages/core.ts +463 -0
- package/src/messages/format.ts +625 -0
- package/src/messages/formatAgentMessages.test.ts +917 -0
- package/src/messages/formatAgentMessages.tools.test.ts +400 -0
- package/src/messages/formatMessage.test.ts +693 -0
- package/src/messages/ids.ts +26 -0
- package/src/messages/index.ts +4 -0
- package/src/messages/prune.ts +567 -0
- package/src/messages/shiftIndexTokenCountMap.test.ts +81 -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/proto/CollabGraph.ts +269 -0
- package/src/proto/TaskManager.ts +243 -0
- package/src/proto/collab.ts +200 -0
- package/src/proto/collab_design.ts +184 -0
- package/src/proto/collab_design_v2.ts +224 -0
- package/src/proto/collab_design_v3.ts +255 -0
- package/src/proto/collab_design_v4.ts +220 -0
- package/src/proto/collab_design_v5.ts +251 -0
- package/src/proto/collab_graph.ts +181 -0
- package/src/proto/collab_original.ts +123 -0
- package/src/proto/example.ts +93 -0
- package/src/proto/example_new.ts +68 -0
- package/src/proto/example_old.ts +201 -0
- package/src/proto/example_test.ts +152 -0
- package/src/proto/example_test_anthropic.ts +100 -0
- package/src/proto/log_stream.ts +202 -0
- package/src/proto/main_collab_community_event.ts +133 -0
- package/src/proto/main_collab_design_v2.ts +96 -0
- package/src/proto/main_collab_design_v4.ts +100 -0
- package/src/proto/main_collab_design_v5.ts +135 -0
- package/src/proto/main_collab_global_analysis.ts +122 -0
- package/src/proto/main_collab_hackathon_event.ts +153 -0
- package/src/proto/main_collab_space_mission.ts +153 -0
- package/src/proto/main_philosophy.ts +210 -0
- package/src/proto/original_script.ts +126 -0
- package/src/proto/standard.ts +100 -0
- package/src/proto/stream.ts +56 -0
- package/src/proto/tasks.ts +118 -0
- package/src/proto/tools/global_analysis_tools.ts +86 -0
- package/src/proto/tools/space_mission_tools.ts +60 -0
- package/src/proto/vertexai.ts +54 -0
- package/src/run.ts +381 -0
- package/src/scripts/abort.ts +138 -0
- package/src/scripts/ant_web_search.ts +158 -0
- package/src/scripts/args.ts +48 -0
- package/src/scripts/caching.ts +124 -0
- package/src/scripts/cli.ts +167 -0
- package/src/scripts/cli2.ts +125 -0
- package/src/scripts/cli3.ts +178 -0
- package/src/scripts/cli4.ts +184 -0
- package/src/scripts/cli5.ts +184 -0
- package/src/scripts/code_exec.ts +214 -0
- package/src/scripts/code_exec_files.ts +193 -0
- package/src/scripts/code_exec_simple.ts +129 -0
- package/src/scripts/content.ts +120 -0
- package/src/scripts/empty_input.ts +137 -0
- package/src/scripts/image.ts +178 -0
- package/src/scripts/memory.ts +97 -0
- package/src/scripts/search.ts +150 -0
- package/src/scripts/simple.ts +225 -0
- package/src/scripts/stream.ts +122 -0
- package/src/scripts/thinking.ts +150 -0
- package/src/scripts/tools.ts +155 -0
- package/src/specs/anthropic.simple.test.ts +317 -0
- package/src/specs/azure.simple.test.ts +316 -0
- package/src/specs/openai.simple.test.ts +316 -0
- package/src/specs/prune.test.ts +763 -0
- package/src/specs/reasoning.test.ts +165 -0
- package/src/specs/spec.utils.ts +3 -0
- package/src/specs/thinking-prune.test.ts +703 -0
- package/src/specs/token-distribution-edge-case.test.ts +316 -0
- package/src/specs/tool-error.test.ts +193 -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 +651 -0
- package/src/tools/CodeExecutor.ts +220 -0
- package/src/tools/ToolNode.ts +170 -0
- package/src/tools/example.ts +129 -0
- package/src/tools/handlers.ts +336 -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 +2 -0
- package/src/tools/search/jina-reranker.test.ts +126 -0
- package/src/tools/search/output.md +2775 -0
- package/src/tools/search/rerankers.ts +242 -0
- package/src/tools/search/schema.ts +63 -0
- package/src/tools/search/search.ts +759 -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 +471 -0
- package/src/tools/search/types.ts +687 -0
- package/src/tools/search/utils.ts +79 -0
- package/src/types/graph.ts +185 -0
- package/src/types/index.ts +6 -0
- package/src/types/llm.ts +140 -0
- package/src/types/run.ts +89 -0
- package/src/types/stream.ts +400 -0
- package/src/types/tools.ts +80 -0
- package/src/utils/graph.ts +11 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/llm.ts +27 -0
- package/src/utils/llmConfig.ts +183 -0
- package/src/utils/logging.ts +48 -0
- package/src/utils/misc.ts +57 -0
- package/src/utils/run.ts +101 -0
- package/src/utils/title.ts +165 -0
- package/src/utils/tokens.ts +70 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
function isPresent(value) {
|
|
2
|
+
return value != null && value !== '';
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Unescapes a c-escaped string
|
|
6
|
+
* @param str The string to unescape
|
|
7
|
+
* @returns The unescaped string
|
|
8
|
+
*/
|
|
9
|
+
const unescapeString = (string) => string.replace(/\\(.)/g, (_, char) => {
|
|
10
|
+
switch (char) {
|
|
11
|
+
case 'n':
|
|
12
|
+
return '\n';
|
|
13
|
+
case 't':
|
|
14
|
+
return '\t';
|
|
15
|
+
case 'r':
|
|
16
|
+
return '\r';
|
|
17
|
+
case '"':
|
|
18
|
+
return '"';
|
|
19
|
+
case '\'':
|
|
20
|
+
return '\'';
|
|
21
|
+
case '\\':
|
|
22
|
+
return '\\';
|
|
23
|
+
default:
|
|
24
|
+
return char;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Recursively unescapes all string values in an object
|
|
29
|
+
* @param obj The object to unescape
|
|
30
|
+
* @returns The unescaped object
|
|
31
|
+
*/
|
|
32
|
+
function unescapeObject(obj, key) {
|
|
33
|
+
if (typeof obj === 'string') {
|
|
34
|
+
let unescaped = unescapeString(obj);
|
|
35
|
+
if (key === 'filePath' && unescaped.match(/^"(.+)"$/)) {
|
|
36
|
+
unescaped = unescaped.substring(1, unescaped.length - 1);
|
|
37
|
+
}
|
|
38
|
+
return unescaped;
|
|
39
|
+
}
|
|
40
|
+
if (Array.isArray(obj)) {
|
|
41
|
+
return obj.map((value) => unescapeObject(value, key === 'contextPaths' ? 'filePath' : ''));
|
|
42
|
+
}
|
|
43
|
+
if (typeof obj === 'object' && obj !== null) {
|
|
44
|
+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
45
|
+
key,
|
|
46
|
+
unescapeObject(value, key),
|
|
47
|
+
]));
|
|
48
|
+
}
|
|
49
|
+
return obj;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { isPresent, unescapeObject };
|
|
53
|
+
//# sourceMappingURL=misc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"misc.mjs","sources":["../../../src/utils/misc.ts"],"sourcesContent":["export function isPresent(value: string | null | undefined): value is string {\r\n return value != null && value !== '';\r\n}\r\n\r\n/**\r\n * Unescapes a c-escaped string\r\n * @param str The string to unescape\r\n * @returns The unescaped string\r\n */\r\nconst unescapeString = (string: string): string =>\r\n string.replace(/\\\\(.)/g, (_, char) => {\r\n switch (char) {\r\n case 'n':\r\n return '\\n';\r\n case 't':\r\n return '\\t';\r\n case 'r':\r\n return '\\r';\r\n case '\"':\r\n return '\"';\r\n case '\\'':\r\n return '\\'';\r\n case '\\\\':\r\n return '\\\\';\r\n default:\r\n return char;\r\n }\r\n });\r\n\r\n/**\r\n * Recursively unescapes all string values in an object\r\n * @param obj The object to unescape\r\n * @returns The unescaped object\r\n */\r\nexport function unescapeObject(obj: unknown, key?: string): unknown {\r\n if (typeof obj === 'string') {\r\n let unescaped = unescapeString(obj);\r\n if (key === 'filePath' && unescaped.match(/^\"(.+)\"$/)) {\r\n unescaped = unescaped.substring(1, unescaped.length - 1);\r\n }\r\n return unescaped;\r\n }\r\n if (Array.isArray(obj)) {\r\n return obj.map((value) =>\r\n unescapeObject(value, key === 'contextPaths' ? 'filePath' : '')\r\n );\r\n }\r\n if (typeof obj === 'object' && obj !== null) {\r\n return Object.fromEntries(\r\n Object.entries(obj).map(([key, value]) => [\r\n key,\r\n unescapeObject(value, key),\r\n ])\r\n );\r\n }\r\n return obj;\r\n}\r\n"],"names":[],"mappings":"AAAM,SAAU,SAAS,CAAC,KAAgC,EAAA;AACxD,IAAA,OAAO,KAAK,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE;AACtC;AAEA;;;;AAIG;AACH,MAAM,cAAc,GAAG,CAAC,MAAc,KACpC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,IAAI,KAAI;IACnC,QAAQ,IAAI;AACZ,QAAA,KAAK,GAAG;AACN,YAAA,OAAO,IAAI;AACb,QAAA,KAAK,GAAG;AACN,YAAA,OAAO,IAAI;AACb,QAAA,KAAK,GAAG;AACN,YAAA,OAAO,IAAI;AACb,QAAA,KAAK,GAAG;AACN,YAAA,OAAO,GAAG;AACZ,QAAA,KAAK,IAAI;AACP,YAAA,OAAO,IAAI;AACb,QAAA,KAAK,IAAI;AACP,YAAA,OAAO,IAAI;AACb,QAAA;AACE,YAAA,OAAO,IAAI;;AAEf,CAAC,CAAC;AAEJ;;;;AAIG;AACa,SAAA,cAAc,CAAC,GAAY,EAAE,GAAY,EAAA;AACvD,IAAA,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;AAC3B,QAAA,IAAI,SAAS,GAAG,cAAc,CAAC,GAAG,CAAC;QACnC,IAAI,GAAG,KAAK,UAAU,IAAI,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE;AACrD,YAAA,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;;AAE1D,QAAA,OAAO,SAAS;;AAElB,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QACtB,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,KAAK,KACnB,cAAc,CAAC,KAAK,EAAE,GAAG,KAAK,cAAc,GAAG,UAAU,GAAG,EAAE,CAAC,CAChE;;IAEH,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE;QAC3C,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK;YACxC,GAAG;AACH,YAAA,cAAc,CAAC,KAAK,EAAE,GAAG,CAAC;AAC3B,SAAA,CAAC,CACH;;AAEH,IAAA,OAAO,GAAG;AACZ;;;;"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Runnable, patchConfig, mergeConfigs } from '@langchain/core/runnables';
|
|
2
|
+
import { AsyncLocalStorageProviderSingleton } from '@langchain/core/singletons';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Delays the execution for a specified number of milliseconds.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} ms - The number of milliseconds to delay.
|
|
8
|
+
* @return {Promise<void>} A promise that resolves after the specified delay.
|
|
9
|
+
*/
|
|
10
|
+
function sleep(ms) {
|
|
11
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
12
|
+
}
|
|
13
|
+
class RunnableCallable extends Runnable {
|
|
14
|
+
lc_namespace = ['langgraph'];
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
func;
|
|
17
|
+
tags;
|
|
18
|
+
config;
|
|
19
|
+
trace = true;
|
|
20
|
+
recurse = true;
|
|
21
|
+
constructor(fields) {
|
|
22
|
+
super();
|
|
23
|
+
this.name = fields.name ?? fields.func.name;
|
|
24
|
+
this.func = fields.func;
|
|
25
|
+
this.config = fields.tags ? { tags: fields.tags } : undefined;
|
|
26
|
+
this.trace = fields.trace ?? this.trace;
|
|
27
|
+
this.recurse = fields.recurse ?? this.recurse;
|
|
28
|
+
}
|
|
29
|
+
async _tracedInvoke(input, config, runManager) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const childConfig = patchConfig(config, {
|
|
32
|
+
callbacks: runManager?.getChild(),
|
|
33
|
+
});
|
|
34
|
+
void AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
35
|
+
try {
|
|
36
|
+
const output = await this.func(input, childConfig);
|
|
37
|
+
resolve(output);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
reject(e);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async invoke(
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
+
input, options
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
) {
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
let returnValue;
|
|
52
|
+
if (this.trace) {
|
|
53
|
+
returnValue = await this._callWithConfig(this._tracedInvoke, input, mergeConfigs(this.config, options));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
returnValue = await this.func(input, mergeConfigs(this.config, options));
|
|
57
|
+
}
|
|
58
|
+
if (Runnable.isRunnable(returnValue) && this.recurse) {
|
|
59
|
+
return await returnValue.invoke(input, options);
|
|
60
|
+
}
|
|
61
|
+
return returnValue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { RunnableCallable, sleep };
|
|
66
|
+
//# sourceMappingURL=run.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.mjs","sources":["../../../src/utils/run.ts"],"sourcesContent":["import { CallbackManagerForChainRun } from '@langchain/core/callbacks/manager';\r\nimport {\r\n mergeConfigs,\r\n patchConfig,\r\n Runnable,\r\n RunnableConfig,\r\n} from '@langchain/core/runnables';\r\nimport { AsyncLocalStorageProviderSingleton } from '@langchain/core/singletons';\r\n\r\n/**\r\n * Delays the execution for a specified number of milliseconds.\r\n *\r\n * @param {number} ms - The number of milliseconds to delay.\r\n * @return {Promise<void>} A promise that resolves after the specified delay.\r\n */\r\nexport function sleep(ms: number): Promise<void> {\r\n return new Promise((resolve) => setTimeout(resolve, ms));\r\n}\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport interface RunnableCallableArgs extends Partial<any> {\r\n name?: string;\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n func: (...args: any[]) => any;\r\n tags?: string[];\r\n trace?: boolean;\r\n recurse?: boolean;\r\n}\r\n\r\nexport class RunnableCallable<I = unknown, O = unknown> extends Runnable<I, O> {\r\n lc_namespace: string[] = ['langgraph'];\r\n\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n func: (...args: any[]) => any;\r\n\r\n tags?: string[];\r\n\r\n config?: RunnableConfig;\r\n\r\n trace: boolean = true;\r\n\r\n recurse: boolean = true;\r\n\r\n constructor(fields: RunnableCallableArgs) {\r\n super();\r\n this.name = fields.name ?? fields.func.name;\r\n this.func = fields.func;\r\n this.config = fields.tags ? { tags: fields.tags } : undefined;\r\n this.trace = fields.trace ?? this.trace;\r\n this.recurse = fields.recurse ?? this.recurse;\r\n }\r\n\r\n protected async _tracedInvoke(\r\n input: I,\r\n config?: Partial<RunnableConfig>,\r\n runManager?: CallbackManagerForChainRun\r\n ): Promise<O> {\r\n return new Promise<O>((resolve, reject) => {\r\n const childConfig = patchConfig(config, {\r\n callbacks: runManager?.getChild(),\r\n });\r\n void AsyncLocalStorageProviderSingleton.runWithConfig(\r\n childConfig,\r\n async () => {\r\n try {\r\n const output = await this.func(input, childConfig);\r\n resolve(output);\r\n } catch (e) {\r\n reject(e);\r\n }\r\n }\r\n );\r\n });\r\n }\r\n\r\n async invoke(\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n input: any,\r\n options?: Partial<RunnableConfig> | undefined\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n ): Promise<any> {\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n let returnValue: any;\r\n\r\n if (this.trace) {\r\n returnValue = await this._callWithConfig(\r\n this._tracedInvoke,\r\n input,\r\n mergeConfigs(this.config, options)\r\n );\r\n } else {\r\n returnValue = await this.func(input, mergeConfigs(this.config, options));\r\n }\r\n\r\n if (Runnable.isRunnable(returnValue) && this.recurse) {\r\n return await returnValue.invoke(input, options);\r\n }\r\n\r\n return returnValue;\r\n }\r\n}"],"names":[],"mappings":";;;AASA;;;;;AAKG;AACG,SAAU,KAAK,CAAC,EAAU,EAAA;AAC9B,IAAA,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAC1D;AAYM,MAAO,gBAA2C,SAAQ,QAAc,CAAA;AAC5E,IAAA,YAAY,GAAa,CAAC,WAAW,CAAC;;AAGtC,IAAA,IAAI;AAEJ,IAAA,IAAI;AAEJ,IAAA,MAAM;IAEN,KAAK,GAAY,IAAI;IAErB,OAAO,GAAY,IAAI;AAEvB,IAAA,WAAA,CAAY,MAA4B,EAAA;AACt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G,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;;QAG1E,IAAI,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE;YACpD,OAAO,MAAM,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC;;AAGjD,QAAA,OAAO,WAAW;;AAErB;;;;"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ChatPromptTemplate } from '@langchain/core/prompts';
|
|
3
|
+
import { RunnableLambda, RunnableSequence } from '@langchain/core/runnables';
|
|
4
|
+
import { ContentTypes } from '../common/enum.mjs';
|
|
5
|
+
|
|
6
|
+
const defaultTitlePrompt = `Analyze this conversation and provide:
|
|
7
|
+
1. The detected language of the conversation
|
|
8
|
+
2. A concise title in the detected language (5 words or less, no punctuation or quotation)
|
|
9
|
+
|
|
10
|
+
{convo}`;
|
|
11
|
+
const titleSchema = z.object({
|
|
12
|
+
title: z
|
|
13
|
+
.string()
|
|
14
|
+
.describe('A concise title for the conversation in 5 words or less, without punctuation or quotation'),
|
|
15
|
+
});
|
|
16
|
+
const combinedSchema = z.object({
|
|
17
|
+
language: z.string().describe('The detected language of the conversation'),
|
|
18
|
+
title: z
|
|
19
|
+
.string()
|
|
20
|
+
.describe('A concise title for the conversation in 5 words or less, without punctuation or quotation'),
|
|
21
|
+
});
|
|
22
|
+
const createTitleRunnable = async (model, _titlePrompt) => {
|
|
23
|
+
// Disabled since this works fine
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
25
|
+
/* @ts-ignore */
|
|
26
|
+
const titleLLM = model.withStructuredOutput(titleSchema);
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
28
|
+
/* @ts-ignore */
|
|
29
|
+
const combinedLLM = model.withStructuredOutput(combinedSchema);
|
|
30
|
+
const titlePrompt = ChatPromptTemplate.fromTemplate(_titlePrompt ?? defaultTitlePrompt).withConfig({ runName: 'TitlePrompt' });
|
|
31
|
+
const titleOnlyInnerChain = RunnableSequence.from([titlePrompt, titleLLM]);
|
|
32
|
+
const combinedInnerChain = RunnableSequence.from([titlePrompt, combinedLLM]);
|
|
33
|
+
/** Wrap titleOnlyChain in RunnableLambda to create parent span */
|
|
34
|
+
const titleOnlyChain = new RunnableLambda({
|
|
35
|
+
func: async (input, config) => {
|
|
36
|
+
return await titleOnlyInnerChain.invoke(input, config);
|
|
37
|
+
},
|
|
38
|
+
}).withConfig({ runName: 'TitleOnlyChain' });
|
|
39
|
+
/** Wrap combinedChain in RunnableLambda to create parent span */
|
|
40
|
+
const combinedChain = new RunnableLambda({
|
|
41
|
+
func: async (input, config) => {
|
|
42
|
+
return await combinedInnerChain.invoke(input, config);
|
|
43
|
+
},
|
|
44
|
+
}).withConfig({ runName: 'TitleLanguageChain' });
|
|
45
|
+
/** Runnable to add default values if needed */
|
|
46
|
+
const addDefaults = new RunnableLambda({
|
|
47
|
+
func: (result) => ({
|
|
48
|
+
language: result?.language ?? 'English',
|
|
49
|
+
title: result?.title ?? '',
|
|
50
|
+
}),
|
|
51
|
+
}).withConfig({ runName: 'AddDefaults' });
|
|
52
|
+
const combinedChainInner = RunnableSequence.from([
|
|
53
|
+
combinedChain,
|
|
54
|
+
addDefaults,
|
|
55
|
+
]);
|
|
56
|
+
/** Wrap combinedChainWithDefaults in RunnableLambda to create parent span */
|
|
57
|
+
const combinedChainWithDefaults = new RunnableLambda({
|
|
58
|
+
func: async (input, config) => {
|
|
59
|
+
return await combinedChainInner.invoke(input, config);
|
|
60
|
+
},
|
|
61
|
+
}).withConfig({ runName: 'CombinedChainWithDefaults' });
|
|
62
|
+
return new RunnableLambda({
|
|
63
|
+
func: async (input, config) => {
|
|
64
|
+
const invokeInput = { convo: input.convo };
|
|
65
|
+
if (input.skipLanguage) {
|
|
66
|
+
return (await titleOnlyChain.invoke(invokeInput, config));
|
|
67
|
+
}
|
|
68
|
+
return await combinedChainWithDefaults.invoke(invokeInput, config);
|
|
69
|
+
},
|
|
70
|
+
}).withConfig({ runName: 'TitleGenerator' });
|
|
71
|
+
};
|
|
72
|
+
const defaultCompletionPrompt = `Provide a concise, 5-word-or-less title for the conversation, using title case conventions. Only return the title itself.
|
|
73
|
+
|
|
74
|
+
Conversation:
|
|
75
|
+
{convo}`;
|
|
76
|
+
const createCompletionTitleRunnable = async (model, titlePrompt) => {
|
|
77
|
+
const completionPrompt = ChatPromptTemplate.fromTemplate(titlePrompt ?? defaultCompletionPrompt).withConfig({ runName: 'CompletionTitlePrompt' });
|
|
78
|
+
/** Runnable to extract content from model response */
|
|
79
|
+
const extractContent = new RunnableLambda({
|
|
80
|
+
func: (response) => {
|
|
81
|
+
let content = '';
|
|
82
|
+
if (typeof response.content === 'string') {
|
|
83
|
+
content = response.content;
|
|
84
|
+
}
|
|
85
|
+
else if (Array.isArray(response.content)) {
|
|
86
|
+
content = response.content
|
|
87
|
+
.filter((part) => part.type === ContentTypes.TEXT)
|
|
88
|
+
.map((part) => part.text)
|
|
89
|
+
.join('');
|
|
90
|
+
}
|
|
91
|
+
return { title: content.trim() };
|
|
92
|
+
},
|
|
93
|
+
}).withConfig({ runName: 'ExtractTitle' });
|
|
94
|
+
const innerChain = RunnableSequence.from([
|
|
95
|
+
completionPrompt,
|
|
96
|
+
model,
|
|
97
|
+
extractContent,
|
|
98
|
+
]);
|
|
99
|
+
/** Wrap in RunnableLambda to create a parent span for LangFuse */
|
|
100
|
+
return new RunnableLambda({
|
|
101
|
+
func: async (input, config) => {
|
|
102
|
+
return await innerChain.invoke(input, config);
|
|
103
|
+
},
|
|
104
|
+
}).withConfig({ runName: 'CompletionTitleChain' });
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export { createCompletionTitleRunnable, createTitleRunnable };
|
|
108
|
+
//# sourceMappingURL=title.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"title.mjs","sources":["../../../src/utils/title.ts"],"sourcesContent":["import { z } from 'zod';\r\nimport { ChatPromptTemplate } from '@langchain/core/prompts';\r\nimport { RunnableLambda, RunnableSequence } from '@langchain/core/runnables';\r\nimport type { Runnable, RunnableConfig } from '@langchain/core/runnables';\r\nimport type { AIMessage } from '@langchain/core/messages';\r\nimport type * as t from '@/types';\r\nimport { ContentTypes } from '@/common';\r\n\r\nconst defaultTitlePrompt = `Analyze this conversation and provide:\r\n1. The detected language of the conversation\r\n2. A concise title in the detected language (5 words or less, no punctuation or quotation)\r\n\r\n{convo}`;\r\n\r\nconst titleSchema = z.object({\r\n title: z\r\n .string()\r\n .describe(\r\n 'A concise title for the conversation in 5 words or less, without punctuation or quotation'\r\n ),\r\n});\r\n\r\nconst combinedSchema = z.object({\r\n language: z.string().describe('The detected language of the conversation'),\r\n title: z\r\n .string()\r\n .describe(\r\n 'A concise title for the conversation in 5 words or less, without punctuation or quotation'\r\n ),\r\n});\r\n\r\nexport const createTitleRunnable = async (\r\n model: t.ChatModelInstance,\r\n _titlePrompt?: string\r\n): Promise<Runnable> => {\r\n // Disabled since this works fine\r\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\r\n /* @ts-ignore */\r\n const titleLLM = model.withStructuredOutput(titleSchema);\r\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\r\n /* @ts-ignore */\r\n const combinedLLM = model.withStructuredOutput(combinedSchema);\r\n\r\n const titlePrompt = ChatPromptTemplate.fromTemplate(\r\n _titlePrompt ?? defaultTitlePrompt\r\n ).withConfig({ runName: 'TitlePrompt' });\r\n\r\n const titleOnlyInnerChain = RunnableSequence.from([titlePrompt, titleLLM]);\r\n const combinedInnerChain = RunnableSequence.from([titlePrompt, combinedLLM]);\r\n\r\n /** Wrap titleOnlyChain in RunnableLambda to create parent span */\r\n const titleOnlyChain = new RunnableLambda({\r\n func: async (\r\n input: { convo: string },\r\n config?: Partial<RunnableConfig>\r\n ): Promise<{ title: string }> => {\r\n return await titleOnlyInnerChain.invoke(input, config);\r\n },\r\n }).withConfig({ runName: 'TitleOnlyChain' });\r\n\r\n /** Wrap combinedChain in RunnableLambda to create parent span */\r\n const combinedChain = new RunnableLambda({\r\n func: async (\r\n input: { convo: string },\r\n config?: Partial<RunnableConfig>\r\n ): Promise<{ language: string; title: string }> => {\r\n return await combinedInnerChain.invoke(input, config);\r\n },\r\n }).withConfig({ runName: 'TitleLanguageChain' });\r\n\r\n /** Runnable to add default values if needed */\r\n const addDefaults = new RunnableLambda({\r\n func: (\r\n result: { language: string; title: string } | undefined\r\n ): { language: string; title: string } => ({\r\n language: result?.language ?? 'English',\r\n title: result?.title ?? '',\r\n }),\r\n }).withConfig({ runName: 'AddDefaults' });\r\n\r\n const combinedChainInner = RunnableSequence.from([\r\n combinedChain,\r\n addDefaults,\r\n ]);\r\n\r\n /** Wrap combinedChainWithDefaults in RunnableLambda to create parent span */\r\n const combinedChainWithDefaults = new RunnableLambda({\r\n func: async (\r\n input: { convo: string },\r\n config?: Partial<RunnableConfig>\r\n ): Promise<{ language: string; title: string }> => {\r\n return await combinedChainInner.invoke(input, config);\r\n },\r\n }).withConfig({ runName: 'CombinedChainWithDefaults' });\r\n\r\n return new RunnableLambda({\r\n func: async (\r\n input: {\r\n convo: string;\r\n inputText: string;\r\n skipLanguage: boolean;\r\n },\r\n config?: Partial<RunnableConfig>\r\n ): Promise<{ language: string; title: string } | { title: string }> => {\r\n const invokeInput = { convo: input.convo };\r\n\r\n if (input.skipLanguage) {\r\n return (await titleOnlyChain.invoke(invokeInput, config)) as {\r\n title: string;\r\n };\r\n }\r\n\r\n return await combinedChainWithDefaults.invoke(invokeInput, config);\r\n },\r\n }).withConfig({ runName: 'TitleGenerator' });\r\n};\r\n\r\nconst defaultCompletionPrompt = `Provide a concise, 5-word-or-less title for the conversation, using title case conventions. Only return the title itself.\r\n\r\nConversation:\r\n{convo}`;\r\n\r\nexport const createCompletionTitleRunnable = async (\r\n model: t.ChatModelInstance,\r\n titlePrompt?: string\r\n): Promise<Runnable> => {\r\n const completionPrompt = ChatPromptTemplate.fromTemplate(\r\n titlePrompt ?? defaultCompletionPrompt\r\n ).withConfig({ runName: 'CompletionTitlePrompt' });\r\n\r\n /** Runnable to extract content from model response */\r\n const extractContent = new RunnableLambda({\r\n func: (response: AIMessage): { title: string } => {\r\n let content = '';\r\n if (typeof response.content === 'string') {\r\n content = response.content;\r\n } else if (Array.isArray(response.content)) {\r\n content = response.content\r\n .filter(\r\n (part): part is { type: ContentTypes.TEXT; text: string } =>\r\n part.type === ContentTypes.TEXT\r\n )\r\n .map((part) => part.text)\r\n .join('');\r\n }\r\n return { title: content.trim() };\r\n },\r\n }).withConfig({ runName: 'ExtractTitle' });\r\n\r\n const innerChain = RunnableSequence.from([\r\n completionPrompt,\r\n model,\r\n extractContent,\r\n ]);\r\n\r\n /** Wrap in RunnableLambda to create a parent span for LangFuse */\r\n return new RunnableLambda({\r\n func: async (\r\n input: { convo: string },\r\n config?: Partial<RunnableConfig>\r\n ): Promise<{ title: string }> => {\r\n return await innerChain.invoke(input, config);\r\n },\r\n }).withConfig({ runName: 'CompletionTitleChain' });\r\n};\r\n"],"names":[],"mappings":";;;;;AAQA,MAAM,kBAAkB,GAAG,CAAA;;;;QAInB;AAER,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC;AAC3B,IAAA,KAAK,EAAE;AACJ,SAAA,MAAM;SACN,QAAQ,CACP,2FAA2F,CAC5F;AACJ,CAAA,CAAC;AAEF,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC;IAC9B,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,2CAA2C,CAAC;AAC1E,IAAA,KAAK,EAAE;AACJ,SAAA,MAAM;SACN,QAAQ,CACP,2FAA2F,CAC5F;AACJ,CAAA,CAAC;AAEW,MAAA,mBAAmB,GAAG,OACjC,KAA0B,EAC1B,YAAqB,KACA;;;;IAIrB,MAAM,Q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C,OAAO;;iBACrB,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;gBAC1C,OAAO,GAAG,QAAQ,CAAC;AAChB,qBAAA,MAAM,CACL,CAAC,IAAI,KACH,IAAI,CAAC,IAAI,KAAK,YAAY,CAAC,IAAI;qBAElC,GAAG,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI;qBACvB,IAAI,CAAC,EAAE,CAAC;;YAEb,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE;SACjC;KACF,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1C,IAAA,MAAM,UAAU,GAAG,gBAAgB,CAAC,IAAI,CAAC;QACvC,gBAAgB;QAChB,KAAK;QACL,cAAc;AACf,KAAA,CAAC;;IAGF,OAAO,IAAI,cAAc,CAAC;AACxB,QAAA,IAAI,EAAE,OACJ,KAAwB,EACxB,MAAgC,KACF;YAC9B,OAAO,MAAM,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC;SAC9C;KACF,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,sBAAsB,EAAE,CAAC;AACpD;;;;"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Tiktoken } from 'js-tiktoken/lite';
|
|
2
|
+
import { ContentTypes } from '../common/enum.mjs';
|
|
3
|
+
|
|
4
|
+
function getTokenCountForMessage(message, getTokenCount) {
|
|
5
|
+
const tokensPerMessage = 3;
|
|
6
|
+
const processValue = (value) => {
|
|
7
|
+
if (Array.isArray(value)) {
|
|
8
|
+
for (const item of value) {
|
|
9
|
+
if (!item ||
|
|
10
|
+
!item.type ||
|
|
11
|
+
item.type === ContentTypes.ERROR ||
|
|
12
|
+
item.type === ContentTypes.IMAGE_URL) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (item.type === ContentTypes.TOOL_CALL && item.tool_call != null) {
|
|
16
|
+
const toolName = item.tool_call?.name || '';
|
|
17
|
+
if (toolName != null && toolName && typeof toolName === 'string') {
|
|
18
|
+
numTokens += getTokenCount(toolName);
|
|
19
|
+
}
|
|
20
|
+
const args = item.tool_call?.args || '';
|
|
21
|
+
if (args != null && args && typeof args === 'string') {
|
|
22
|
+
numTokens += getTokenCount(args);
|
|
23
|
+
}
|
|
24
|
+
const output = item.tool_call?.output || '';
|
|
25
|
+
if (output != null && output && typeof output === 'string') {
|
|
26
|
+
numTokens += getTokenCount(output);
|
|
27
|
+
}
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const nestedValue = item[item.type];
|
|
31
|
+
if (!nestedValue) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
processValue(nestedValue);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (typeof value === 'string') {
|
|
38
|
+
numTokens += getTokenCount(value);
|
|
39
|
+
}
|
|
40
|
+
else if (typeof value === 'number') {
|
|
41
|
+
numTokens += getTokenCount(value.toString());
|
|
42
|
+
}
|
|
43
|
+
else if (typeof value === 'boolean') {
|
|
44
|
+
numTokens += getTokenCount(value.toString());
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
let numTokens = tokensPerMessage;
|
|
48
|
+
processValue(message.content);
|
|
49
|
+
return numTokens;
|
|
50
|
+
}
|
|
51
|
+
const createTokenCounter = async () => {
|
|
52
|
+
const res = await fetch('https://tiktoken.pages.dev/js/o200k_base.json');
|
|
53
|
+
const o200k_base = await res.json();
|
|
54
|
+
const countTokens = (text) => {
|
|
55
|
+
const enc = new Tiktoken(o200k_base);
|
|
56
|
+
return enc.encode(text).length;
|
|
57
|
+
};
|
|
58
|
+
return (message) => getTokenCountForMessage(message, countTokens);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { createTokenCounter, getTokenCountForMessage };
|
|
62
|
+
//# sourceMappingURL=tokens.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.mjs","sources":["../../../src/utils/tokens.ts"],"sourcesContent":["import { Tiktoken } from 'js-tiktoken/lite';\r\nimport type { BaseMessage } from '@langchain/core/messages';\r\nimport { ContentTypes } from '@/common/enum';\r\n\r\nexport function getTokenCountForMessage(message: BaseMessage, getTokenCount: (text: string) => number): number {\r\n const tokensPerMessage = 3;\r\n\r\n const processValue = (value: unknown): void => {\r\n if (Array.isArray(value)) {\r\n for (const item of value) {\r\n if (\r\n !item ||\r\n !item.type ||\r\n item.type === ContentTypes.ERROR ||\r\n item.type === ContentTypes.IMAGE_URL\r\n ) {\r\n continue;\r\n }\r\n\r\n if (item.type === ContentTypes.TOOL_CALL && item.tool_call != null) {\r\n const toolName = item.tool_call?.name || '';\r\n if (toolName != null && toolName && typeof toolName === 'string') {\r\n numTokens += getTokenCount(toolName);\r\n }\r\n\r\n const args = item.tool_call?.args || '';\r\n if (args != null && args && typeof args === 'string') {\r\n numTokens += getTokenCount(args);\r\n }\r\n\r\n const output = item.tool_call?.output || '';\r\n if (output != null && output && typeof output === 'string') {\r\n numTokens += getTokenCount(output);\r\n }\r\n continue;\r\n }\r\n\r\n const nestedValue = item[item.type];\r\n\r\n if (!nestedValue) {\r\n continue;\r\n }\r\n\r\n processValue(nestedValue);\r\n }\r\n } else if (typeof value === 'string') {\r\n numTokens += getTokenCount(value);\r\n } else if (typeof value === 'number') {\r\n numTokens += getTokenCount(value.toString());\r\n } else if (typeof value === 'boolean') {\r\n numTokens += getTokenCount(value.toString());\r\n }\r\n };\r\n\r\n let numTokens = tokensPerMessage;\r\n processValue(message.content);\r\n return numTokens;\r\n}\r\n\r\nexport const createTokenCounter = async () => {\r\n const res = await fetch('https://tiktoken.pages.dev/js/o200k_base.json');\r\n const o200k_base = await res.json();\r\n\r\n const countTokens = (text: string): number => {\r\n const enc = new Tiktoken(o200k_base);\r\n return enc.encode(text).length;\r\n };\r\n\r\n return (message: BaseMessage): number => getTokenCountForMessage(message, countTokens);\r\n};"],"names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IAAI,CAAC,WAAW,EAAE;oBAChB;;gBAGF,YAAY,CAAC,WAAW,CAAC;;;AAEtB,aAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AACpC,YAAA,SAAS,IAAI,aAAa,CAAC,KAAK,CAAC;;AAC5B,aAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YACpC,SAAS,IAAI,aAAa,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;;AACvC,aAAA,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE;YACrC,SAAS,IAAI,aAAa,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;;AAEhD,KAAC;IAED,IAAI,SAAS,GAAG,gBAAgB;AAChC,IAAA,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC;AAC7B,IAAA,OAAO,SAAS;AAClB;AAEa,MAAA,kBAAkB,GAAG,YAAW;AAC3C,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,+CAA+C,CAAC;AACxE,IAAA,MAAM,UAAU,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE;AAEnC,IAAA,MAAM,WAAW,GAAG,CAAC,IAAY,KAAY;AAC3C,QAAA,MAAM,GAAG,GAAG,IAAI,QAAQ,CAAC,UAAU,CAAC;QACpC,OAAO,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM;AAChC,KAAC;IAED,OAAO,CAAC,OAAoB,KAAa,uBAAuB,CAAC,OAAO,EAAE,WAAW,CAAC;AACxF;;;;"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enum representing the various event types emitted during the execution of runnables.
|
|
3
|
+
* These events provide real-time information about the progress and state of different components.
|
|
4
|
+
*
|
|
5
|
+
* @enum {string}
|
|
6
|
+
*/
|
|
7
|
+
export declare enum GraphEvents {
|
|
8
|
+
/** [Custom] Agent update event in multi-agent graph/workflow */
|
|
9
|
+
ON_AGENT_UPDATE = "on_agent_update",
|
|
10
|
+
/** [Custom] Delta event for run steps (message creation and tool calls) */
|
|
11
|
+
ON_RUN_STEP = "on_run_step",
|
|
12
|
+
/** [Custom] Delta event for run steps (tool calls) */
|
|
13
|
+
ON_RUN_STEP_DELTA = "on_run_step_delta",
|
|
14
|
+
/** [Custom] Completed event for run steps (tool calls) */
|
|
15
|
+
ON_RUN_STEP_COMPLETED = "on_run_step_completed",
|
|
16
|
+
/** [Custom] Delta events for messages */
|
|
17
|
+
ON_MESSAGE_DELTA = "on_message_delta",
|
|
18
|
+
/** [Custom] Reasoning Delta events for messages */
|
|
19
|
+
ON_REASONING_DELTA = "on_reasoning_delta",
|
|
20
|
+
/** Custom event, emitted by system */
|
|
21
|
+
ON_CUSTOM_EVENT = "on_custom_event",
|
|
22
|
+
/** Emitted when a chat model starts processing. */
|
|
23
|
+
CHAT_MODEL_START = "on_chat_model_start",
|
|
24
|
+
/** Emitted when a chat model streams a chunk of its response. */
|
|
25
|
+
CHAT_MODEL_STREAM = "on_chat_model_stream",
|
|
26
|
+
/** Emitted when a chat model completes its processing. */
|
|
27
|
+
CHAT_MODEL_END = "on_chat_model_end",
|
|
28
|
+
/** Emitted when a language model starts processing. */
|
|
29
|
+
LLM_START = "on_llm_start",
|
|
30
|
+
/** Emitted when a language model streams a chunk of its response. */
|
|
31
|
+
LLM_STREAM = "on_llm_stream",
|
|
32
|
+
/** Emitted when a language model completes its processing. */
|
|
33
|
+
LLM_END = "on_llm_end",
|
|
34
|
+
/** Emitted when a chain starts processing. */
|
|
35
|
+
CHAIN_START = "on_chain_start",
|
|
36
|
+
/** Emitted when a chain streams a chunk of its output. */
|
|
37
|
+
CHAIN_STREAM = "on_chain_stream",
|
|
38
|
+
/** Emitted when a chain completes its processing. */
|
|
39
|
+
CHAIN_END = "on_chain_end",
|
|
40
|
+
/** Emitted when a tool starts its operation. */
|
|
41
|
+
TOOL_START = "on_tool_start",
|
|
42
|
+
/** Emitted when a tool completes its operation. */
|
|
43
|
+
TOOL_END = "on_tool_end",
|
|
44
|
+
/** Emitted when a retriever starts its operation. */
|
|
45
|
+
RETRIEVER_START = "on_retriever_start",
|
|
46
|
+
/** Emitted when a retriever completes its operation. */
|
|
47
|
+
RETRIEVER_END = "on_retriever_end",
|
|
48
|
+
/** Emitted when a prompt starts processing. */
|
|
49
|
+
PROMPT_START = "on_prompt_start",
|
|
50
|
+
/** Emitted when a prompt completes its processing. */
|
|
51
|
+
PROMPT_END = "on_prompt_end"
|
|
52
|
+
}
|
|
53
|
+
export declare enum Providers {
|
|
54
|
+
OPENAI = "openAI",
|
|
55
|
+
BEDROCK_LEGACY = "bedrock_legacy",
|
|
56
|
+
VERTEXAI = "vertexai",
|
|
57
|
+
BEDROCK = "bedrock",
|
|
58
|
+
ANTHROPIC = "anthropic",
|
|
59
|
+
MISTRALAI = "mistralai",
|
|
60
|
+
MISTRAL = "mistral",
|
|
61
|
+
OLLAMA = "ollama",
|
|
62
|
+
GOOGLE = "google",
|
|
63
|
+
AZURE = "azureOpenAI",
|
|
64
|
+
DEEPSEEK = "deepseek",
|
|
65
|
+
OPENROUTER = "openrouter",
|
|
66
|
+
XAI = "xai"
|
|
67
|
+
}
|
|
68
|
+
export declare enum GraphNodeKeys {
|
|
69
|
+
TOOLS = "tools",
|
|
70
|
+
AGENT = "agent",
|
|
71
|
+
PRE_TOOLS = "pre_tools",
|
|
72
|
+
POST_TOOLS = "post_tools"
|
|
73
|
+
}
|
|
74
|
+
export declare enum GraphNodeActions {
|
|
75
|
+
TOOL_NODE = "tool_node",
|
|
76
|
+
CALL_MODEL = "call_model",
|
|
77
|
+
ROUTE_MESSAGE = "route_message"
|
|
78
|
+
}
|
|
79
|
+
export declare enum CommonEvents {
|
|
80
|
+
LANGGRAPH = "LangGraph"
|
|
81
|
+
}
|
|
82
|
+
export declare enum StepTypes {
|
|
83
|
+
TOOL_CALLS = "tool_calls",
|
|
84
|
+
MESSAGE_CREATION = "message_creation"
|
|
85
|
+
}
|
|
86
|
+
export declare enum ContentTypes {
|
|
87
|
+
TEXT = "text",
|
|
88
|
+
ERROR = "error",
|
|
89
|
+
THINK = "think",
|
|
90
|
+
TOOL_CALL = "tool_call",
|
|
91
|
+
IMAGE_URL = "image_url",
|
|
92
|
+
IMAGE_FILE = "image_file",
|
|
93
|
+
/** Anthropic */
|
|
94
|
+
THINKING = "thinking",
|
|
95
|
+
/** Vertex AI / Google Common */
|
|
96
|
+
REASONING = "reasoning",
|
|
97
|
+
/** Multi-Agent Switch */
|
|
98
|
+
AGENT_UPDATE = "agent_update",
|
|
99
|
+
/** Bedrock */
|
|
100
|
+
REASONING_CONTENT = "reasoning_content"
|
|
101
|
+
}
|
|
102
|
+
export declare enum ToolCallTypes {
|
|
103
|
+
FUNCTION = "function",
|
|
104
|
+
RETRIEVAL = "retrieval",
|
|
105
|
+
FILE_SEARCH = "file_search",
|
|
106
|
+
CODE_INTERPRETER = "code_interpreter",
|
|
107
|
+
TOOL_CALL = "tool_call"
|
|
108
|
+
}
|
|
109
|
+
export declare enum Callback {
|
|
110
|
+
TOOL_ERROR = "handleToolError",
|
|
111
|
+
TOOL_START = "handleToolStart",
|
|
112
|
+
TOOL_END = "handleToolEnd"
|
|
113
|
+
}
|
|
114
|
+
export declare enum Constants {
|
|
115
|
+
OFFICIAL_CODE_BASEURL = "https://api.illuma.ai/v1",
|
|
116
|
+
EXECUTE_CODE = "execute_code",
|
|
117
|
+
WEB_SEARCH = "web_search",
|
|
118
|
+
CONTENT_AND_ARTIFACT = "content_and_artifact"
|
|
119
|
+
}
|
|
120
|
+
export declare enum TitleMethod {
|
|
121
|
+
STRUCTURED = "structured",
|
|
122
|
+
FUNCTIONS = "functions",
|
|
123
|
+
COMPLETION = "completion"
|
|
124
|
+
}
|
|
125
|
+
export declare enum EnvVar {
|
|
126
|
+
CODE_API_KEY = "CODE_EXECUTOR_API_KEY",
|
|
127
|
+
CODE_BASEURL = "CODE_EXECUTOR_API_BASEURL"
|
|
128
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './enum';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { UsageMetadata, BaseMessageFields } from '@langchain/core/messages';
|
|
2
|
+
import type { Graph } from '@/graphs';
|
|
3
|
+
import type * as t from '@/types';
|
|
4
|
+
export declare class HandlerRegistry {
|
|
5
|
+
private handlers;
|
|
6
|
+
register(eventType: string, handler: t.EventHandler): void;
|
|
7
|
+
getHandler(eventType: string): t.EventHandler | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare class ModelEndHandler implements t.EventHandler {
|
|
10
|
+
collectedUsage?: UsageMetadata[];
|
|
11
|
+
constructor(collectedUsage?: UsageMetadata[]);
|
|
12
|
+
handle(event: string, data: t.ModelEndData, metadata?: Record<string, unknown>, graph?: Graph): void;
|
|
13
|
+
}
|
|
14
|
+
export declare class ToolEndHandler implements t.EventHandler {
|
|
15
|
+
private callback?;
|
|
16
|
+
private omitOutput?;
|
|
17
|
+
constructor(callback?: t.ToolEndCallback, omitOutput?: (name?: string) => boolean);
|
|
18
|
+
handle(event: string, data: t.StreamEventData | undefined, metadata?: Record<string, unknown>, graph?: Graph): void;
|
|
19
|
+
}
|
|
20
|
+
export declare class TestLLMStreamHandler implements t.EventHandler {
|
|
21
|
+
handle(event: string, data: t.StreamEventData | undefined): void;
|
|
22
|
+
}
|
|
23
|
+
export declare class TestChatStreamHandler implements t.EventHandler {
|
|
24
|
+
handle(event: string, data: t.StreamEventData | undefined): void;
|
|
25
|
+
}
|
|
26
|
+
export declare class LLMStreamHandler implements t.EventHandler {
|
|
27
|
+
handle(event: string, data: t.StreamEventData | undefined, metadata?: Record<string, unknown>): void;
|
|
28
|
+
}
|
|
29
|
+
export declare const createMetadataAggregator: (_collected?: Record<string, NonNullable<BaseMessageFields["response_metadata"]>>[]) => t.MetadataAggregatorResult;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ToolNode } from '@langchain/langgraph/prebuilt';
|
|
2
|
+
import { START } from '@langchain/langgraph';
|
|
3
|
+
import { Runnable, RunnableConfig } from '@langchain/core/runnables';
|
|
4
|
+
import { SystemMessage } from '@langchain/core/messages';
|
|
5
|
+
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
6
|
+
import type * as t from '@/types';
|
|
7
|
+
import { Providers, GraphNodeKeys, Callback, ContentTypes } from '@/common';
|
|
8
|
+
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
9
|
+
import { ToolNode as CustomToolNode } from '@/tools/ToolNode';
|
|
10
|
+
import { createPruneMessages } from '@/messages';
|
|
11
|
+
import { HandlerRegistry } from '@/events';
|
|
12
|
+
export type GraphNode = GraphNodeKeys | typeof START;
|
|
13
|
+
export type ClientCallback<T extends unknown[]> = (graph: StandardGraph, ...args: T) => void;
|
|
14
|
+
export type ClientCallbacks = {
|
|
15
|
+
[Callback.TOOL_ERROR]?: ClientCallback<[Error, string]>;
|
|
16
|
+
[Callback.TOOL_START]?: ClientCallback<unknown[]>;
|
|
17
|
+
[Callback.TOOL_END]?: ClientCallback<unknown[]>;
|
|
18
|
+
};
|
|
19
|
+
export type SystemCallbacks = {
|
|
20
|
+
[K in keyof ClientCallbacks]: ClientCallbacks[K] extends ClientCallback<infer Args> ? (...args: Args) => void : never;
|
|
21
|
+
};
|
|
22
|
+
export declare abstract class Graph<T extends t.BaseGraphState = t.BaseGraphState, TNodeName extends string = string> {
|
|
23
|
+
abstract resetValues(): void;
|
|
24
|
+
abstract createGraphState(): t.GraphStateChannels<T>;
|
|
25
|
+
abstract initializeTools(): CustomToolNode<T> | ToolNode<T>;
|
|
26
|
+
abstract initializeModel(): Runnable;
|
|
27
|
+
abstract getRunMessages(): BaseMessage[] | undefined;
|
|
28
|
+
abstract getContentParts(): t.MessageContentComplex[] | undefined;
|
|
29
|
+
abstract generateStepId(stepKey: string): [string, number];
|
|
30
|
+
abstract getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[];
|
|
31
|
+
abstract getStepKey(metadata: Record<string, unknown> | undefined): string;
|
|
32
|
+
abstract checkKeyList(keyList: (string | number | undefined)[]): boolean;
|
|
33
|
+
abstract getStepIdByKey(stepKey: string, index?: number): string;
|
|
34
|
+
abstract getRunStep(stepId: string): t.RunStep | undefined;
|
|
35
|
+
abstract dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string;
|
|
36
|
+
abstract dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void;
|
|
37
|
+
abstract dispatchMessageDelta(id: string, delta: t.MessageDelta): void;
|
|
38
|
+
abstract dispatchReasoningDelta(stepId: string, delta: t.ReasoningDelta): void;
|
|
39
|
+
abstract handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>, omitOutput?: boolean): void;
|
|
40
|
+
abstract createCallModel(): (state: T, config?: RunnableConfig) => Promise<Partial<T>>;
|
|
41
|
+
abstract createWorkflow(): t.CompiledWorkflow<T>;
|
|
42
|
+
lastToken?: string;
|
|
43
|
+
tokenTypeSwitch?: 'reasoning' | 'content';
|
|
44
|
+
reasoningKey: 'reasoning_content' | 'reasoning';
|
|
45
|
+
currentTokenType: ContentTypes.TEXT | ContentTypes.THINK | 'think_and_text';
|
|
46
|
+
messageStepHasToolCalls: Map<string, boolean>;
|
|
47
|
+
messageIdsByStepKey: Map<string, string>;
|
|
48
|
+
prelimMessageIdsByStepKey: Map<string, string>;
|
|
49
|
+
config: RunnableConfig | undefined;
|
|
50
|
+
contentData: t.RunStep[];
|
|
51
|
+
stepKeyIds: Map<string, string[]>;
|
|
52
|
+
contentIndexMap: Map<string, number>;
|
|
53
|
+
toolCallStepIds: Map<string, string>;
|
|
54
|
+
currentUsage: Partial<UsageMetadata> | undefined;
|
|
55
|
+
indexTokenCountMap: Record<string, number | undefined>;
|
|
56
|
+
maxContextTokens: number | undefined;
|
|
57
|
+
pruneMessages?: ReturnType<typeof createPruneMessages>;
|
|
58
|
+
/** The amount of time that should pass before another consecutive API call */
|
|
59
|
+
streamBuffer: number | undefined;
|
|
60
|
+
tokenCounter?: t.TokenCounter;
|
|
61
|
+
signal?: AbortSignal;
|
|
62
|
+
/** Set of invoked tool call IDs from non-message run steps completed mid-run, if any */
|
|
63
|
+
invokedToolIds?: Set<string>;
|
|
64
|
+
handlerRegistry: HandlerRegistry | undefined;
|
|
65
|
+
}
|
|
66
|
+
export declare class StandardGraph extends Graph<t.BaseGraphState, GraphNode> {
|
|
67
|
+
private graphState;
|
|
68
|
+
clientOptions: t.ClientOptions;
|
|
69
|
+
boundModel?: Runnable;
|
|
70
|
+
/** The last recorded timestamp that a stream API call was invoked */
|
|
71
|
+
lastStreamCall: number | undefined;
|
|
72
|
+
systemMessage: SystemMessage | undefined;
|
|
73
|
+
messages: BaseMessage[];
|
|
74
|
+
runId: string | undefined;
|
|
75
|
+
tools?: t.GraphTools;
|
|
76
|
+
toolMap?: t.ToolMap;
|
|
77
|
+
startIndex: number;
|
|
78
|
+
provider: Providers;
|
|
79
|
+
toolEnd: boolean;
|
|
80
|
+
signal?: AbortSignal;
|
|
81
|
+
constructor({ runId, tools, signal, toolMap, provider, streamBuffer, instructions, reasoningKey, clientOptions, toolEnd, additional_instructions, }: t.StandardGraphInput);
|
|
82
|
+
resetValues(keepContent?: boolean): void;
|
|
83
|
+
getRunStep(stepId: string): t.RunStep | undefined;
|
|
84
|
+
getStepKey(metadata: Record<string, unknown> | undefined): string;
|
|
85
|
+
getStepIdByKey(stepKey: string, index?: number): string;
|
|
86
|
+
generateStepId(stepKey: string): [string, number];
|
|
87
|
+
getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[];
|
|
88
|
+
checkKeyList(keyList: (string | number | undefined)[]): boolean;
|
|
89
|
+
getRunMessages(): BaseMessage[] | undefined;
|
|
90
|
+
getContentParts(): t.MessageContentComplex[] | undefined;
|
|
91
|
+
createGraphState(): t.GraphStateChannels<t.BaseGraphState>;
|
|
92
|
+
initializeTools(): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState>;
|
|
93
|
+
initializeModel(): Runnable;
|
|
94
|
+
overrideTestModel(responses: string[], sleep?: number, toolCalls?: ToolCall[]): void;
|
|
95
|
+
getNewModel({ provider, clientOptions, omitOptions, }: {
|
|
96
|
+
provider: Providers;
|
|
97
|
+
clientOptions?: t.ClientOptions;
|
|
98
|
+
omitOptions?: Set<string>;
|
|
99
|
+
}): t.ChatModelInstance;
|
|
100
|
+
storeUsageMetadata(finalMessage?: BaseMessage): void;
|
|
101
|
+
cleanupSignalListener(): void;
|
|
102
|
+
createCallModel(): (state: t.BaseGraphState, config?: RunnableConfig) => Promise<Partial<t.BaseGraphState>>;
|
|
103
|
+
createWorkflow(): t.CompiledWorkflow<t.BaseGraphState>;
|
|
104
|
+
/**
|
|
105
|
+
* Dispatches a run step to the client, returns the step ID
|
|
106
|
+
*/
|
|
107
|
+
dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string;
|
|
108
|
+
handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>, omitOutput?: boolean): void;
|
|
109
|
+
/**
|
|
110
|
+
* Static version of handleToolCallError to avoid creating strong references
|
|
111
|
+
* that prevent garbage collection
|
|
112
|
+
*/
|
|
113
|
+
static handleToolCallErrorStatic(graph: StandardGraph, data: t.ToolErrorData, metadata?: Record<string, unknown>): void;
|
|
114
|
+
/**
|
|
115
|
+
* Instance method that delegates to the static method
|
|
116
|
+
* Kept for backward compatibility
|
|
117
|
+
*/
|
|
118
|
+
handleToolCallError(data: t.ToolErrorData, metadata?: Record<string, unknown>): void;
|
|
119
|
+
dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void;
|
|
120
|
+
dispatchMessageDelta(id: string, delta: t.MessageDelta): void;
|
|
121
|
+
dispatchReasoningDelta: (stepId: string, delta: t.ReasoningDelta) => void;
|
|
122
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Graph';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './run';
|
|
2
|
+
export * from './stream';
|
|
3
|
+
export * from './splitStream';
|
|
4
|
+
export * from './events';
|
|
5
|
+
export * from './messages';
|
|
6
|
+
export * from './graphs';
|
|
7
|
+
export * from './tools/CodeExecutor';
|
|
8
|
+
export * from './tools/handlers';
|
|
9
|
+
export * from './tools/search';
|
|
10
|
+
export * from './common';
|
|
11
|
+
export * from './utils';
|
|
12
|
+
export type * from './types';
|
|
13
|
+
export { CustomOpenAIClient } from './llm/openai';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|