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,48 @@
|
|
|
1
|
+
// src/scripts/args.ts
|
|
2
|
+
import yargs from 'yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
import { llmConfigs } from '@/utils/llmConfig';
|
|
5
|
+
import { Providers } from '@/common';
|
|
6
|
+
|
|
7
|
+
export async function getArgs(): Promise<{
|
|
8
|
+
userName: string;
|
|
9
|
+
location: string;
|
|
10
|
+
provider: Providers;
|
|
11
|
+
currentDate: string;
|
|
12
|
+
}> {
|
|
13
|
+
const argv = yargs(hideBin(process.argv))
|
|
14
|
+
.option('name', {
|
|
15
|
+
alias: 'n',
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: 'User name',
|
|
18
|
+
default: 'Jo',
|
|
19
|
+
})
|
|
20
|
+
.option('location', {
|
|
21
|
+
alias: 'l',
|
|
22
|
+
type: 'string',
|
|
23
|
+
description: 'User location',
|
|
24
|
+
default: 'New York',
|
|
25
|
+
})
|
|
26
|
+
.option('provider', {
|
|
27
|
+
alias: 'p',
|
|
28
|
+
type: 'string',
|
|
29
|
+
description: 'LLM provider',
|
|
30
|
+
choices: Object.keys(llmConfigs),
|
|
31
|
+
default: Providers.OPENAI,
|
|
32
|
+
})
|
|
33
|
+
.help()
|
|
34
|
+
.alias('help', 'h').argv;
|
|
35
|
+
|
|
36
|
+
const args = await argv;
|
|
37
|
+
const userName = args.name as string;
|
|
38
|
+
const location = args.location as string;
|
|
39
|
+
const provider = args.provider as Providers;
|
|
40
|
+
const currentDate = new Date().toLocaleString();
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
userName,
|
|
44
|
+
location,
|
|
45
|
+
provider,
|
|
46
|
+
currentDate,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// src/scripts/test-prompt-caching.ts
|
|
2
|
+
import { config } from 'dotenv';
|
|
3
|
+
config();
|
|
4
|
+
import { HumanMessage, SystemMessage, BaseMessage } from '@langchain/core/messages';
|
|
5
|
+
import type { UsageMetadata } from '@langchain/core/messages';
|
|
6
|
+
import type * as t from '@/types';
|
|
7
|
+
import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
|
|
8
|
+
import { ToolEndHandler, ModelEndHandler } from '@/events';
|
|
9
|
+
import { GraphEvents, Providers } from '@/common';
|
|
10
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
11
|
+
import { getArgs } from '@/scripts/args';
|
|
12
|
+
import { Run } from '@/run';
|
|
13
|
+
|
|
14
|
+
const CACHED_TEXT = `Ahoy there, me hearties! This be a grand tale o' the mighty prompt cachin' treasure map, a secret technique used by the wise Anthropic seafarers to stash away vast hordes o' text booty on their mystical servers! Arrr, 'tis a pirate's dream indeed - no need to haul the same heavy chest o' gold doubloons across the vast digital ocean with every message! When ye mark yer precious cargo with the secret flag 'cache_control: { type: \"ephemeral\" }', the text be safely buried on their distant shores, ready for plunderin' again without the weight slowin' down yer ship! The wise pirates at Anthropic introduced this magical scroll in the summer o' 2024, markin' it with the mysterious insignia 'anthropic-beta: prompt-caching-2024-07-31' that must be flown high on yer vessel's headers. This crafty script be testin' the waters of this new treasure map system, sendin' out three separate voyages across the AI seas: first to bury the treasure, second to dig it up again without payin' the full toll, and third to see if the map still leads to gold after the sands o' time have shifted (about thirty seconds o' waitin', which be an eternity for an impatient buccaneer!). The great advantage for a scurvy pirate captain is clear as Caribbean waters - ye can load up yer vessel with all manner o' reference scrolls, ancient tomes, and navigational charts without weighin' down each and every message ye send to port! This be savin' ye countless tokens, which as any seafarin' AI wrangler knows, be as precious as Spanish gold. The cached text could contain the full history o' the Seven Seas, detailed maps o' every port from Tortuga to Singapore, or the complete collection o' pirate shanties ever sung by drunken sailors under the light o' the silvery moon. When properly implemented, this mighty cachin' system keeps all that knowledge ready at hand without the Claude kraken needin' to process it anew with each passin' breeze. By Blackbeard's beard, 'tis a revolution in how we manage our conversational ships! The script be employin' the finest LangChain riggin' and custom-carved event handlers to properly track the treasure as it flows back and forth. If ye be successful in yer implementation, ye should witness the miracle o' significantly reduced token counts in yer usage metrics, faster responses from the AI oracle, and the ability to maintain vast knowledge without payin' the full price each time! So hoist the Jolly Roger, load yer pistols with API keys, and set sail on the grand adventure o' prompt cachin'! May the winds o' efficient token usage fill yer sails, and may ye never have to pay full price for passin' the same mammoth context to Claude again! Remember, a clever pirate only pays for their tokens once, then lets the cache do the heavy liftin'! YARRR! This file also contains the secrets of the legendary Pirate Code, passed down through generations of seafarers since the Golden Age of Piracy. It includes detailed accounts of famous pirate captains like Blackbeard, Calico Jack, Anne Bonny, and Mary Read, along with their most profitable plundering routes and techniques for capturing merchant vessels. The text chronicles the exact locations of at least seventeen buried treasures across the Caribbean, complete with riddles and map coordinates that only a true pirate could decipher. There are sections dedicated to ship maintenance, including how to properly seal a leaking hull during battle and the best methods for keeping your cannons in prime firing condition even in humid tropical conditions. The document contains an extensive glossary of pirate terminology, from 'avast' to 'Yellow Jack,' ensuring any landlubber can speak like a seasoned salt with enough study. There's a comprehensive guide to navigating by the stars without modern instruments, perfect for when your GPS fails in the middle of a daring escape. The cache also includes detailed recipes for grog, hardtack that won't break your teeth, and how to keep citrus fruits fresh to prevent scurvy during long voyages. The legendary Black Spot ritual is described in terrifying detail, along with other pirate superstitions and their origins in maritime folklore. A section on pirate governance explains the democratic nature of most pirate ships, how booty was divided fairly, and how captains were elected and deposed when necessary. The file even contains sheet music for dozens of sea shanties, with notes on when each should be sung for maximum crew morale during different sailing conditions. All of this knowledge is wrapped in colorful pirate dialect that would make any AI assistant respond with appropriate 'arghs' and 'avasts' when properly prompted!`
|
|
15
|
+
|
|
16
|
+
const conversationHistory: BaseMessage[] = [];
|
|
17
|
+
let _contentParts: t.MessageContentComplex[] = [];
|
|
18
|
+
const collectedUsage: UsageMetadata[] = [];
|
|
19
|
+
|
|
20
|
+
async function testPromptCaching(): Promise<void> {
|
|
21
|
+
const { userName } = await getArgs();
|
|
22
|
+
const instructions = `You are a pirate AI assistant for ${userName}. Always respond in pirate dialect. Use the following as context when answering questions:
|
|
23
|
+
${CACHED_TEXT}`;
|
|
24
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
25
|
+
_contentParts = contentParts as t.MessageContentComplex[];
|
|
26
|
+
|
|
27
|
+
// Set up event handlers
|
|
28
|
+
const customHandlers = {
|
|
29
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
30
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(collectedUsage),
|
|
31
|
+
// console.log('====== O ======');
|
|
32
|
+
// console.log('Usage Metrics:', (data as any).llmOutput?.usage || (data as any).usage);
|
|
33
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
34
|
+
// Additional handlers for tracking usage metrics
|
|
35
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
36
|
+
handle: (event: GraphEvents.ON_RUN_STEP_COMPLETED, data: t.StreamEventData): void => {
|
|
37
|
+
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
38
|
+
aggregateContent({ event, data: data as unknown as { result: t.ToolEndEvent } });
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const baseLlmConfig: t.LLMConfig & t.AnthropicClientOptions = getLLMConfig(Providers.ANTHROPIC);
|
|
44
|
+
|
|
45
|
+
if (baseLlmConfig.provider !== 'anthropic') {
|
|
46
|
+
console.error('This test requires Anthropic as the LLM provider. Please specify provider=anthropic');
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const llmConfig = {
|
|
51
|
+
...baseLlmConfig,
|
|
52
|
+
clientOptions: {
|
|
53
|
+
...baseLlmConfig.clientOptions,
|
|
54
|
+
defaultHeaders: {
|
|
55
|
+
...baseLlmConfig.clientOptions?.defaultHeaders,
|
|
56
|
+
"anthropic-beta": "prompt-caching-2024-07-31",
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const run = await Run.create<t.IState>({
|
|
62
|
+
runId: 'test-prompt-caching-id',
|
|
63
|
+
graphConfig: {
|
|
64
|
+
instructions,
|
|
65
|
+
type: 'standard',
|
|
66
|
+
llmConfig,
|
|
67
|
+
},
|
|
68
|
+
returnContent: true,
|
|
69
|
+
customHandlers,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const config = {
|
|
73
|
+
configurable: {
|
|
74
|
+
thread_id: 'prompt-cache-test-thread',
|
|
75
|
+
},
|
|
76
|
+
streamMode: 'values',
|
|
77
|
+
version: 'v2' as const,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// First request - should create the cache
|
|
81
|
+
console.log('\n\nTest 1: First request (creates cache)');
|
|
82
|
+
const userMessage1 = `What information do you have in your context?`;
|
|
83
|
+
conversationHistory.push(new HumanMessage(userMessage1));
|
|
84
|
+
|
|
85
|
+
console.log('Running first query to create cache...');
|
|
86
|
+
const firstInputs = { messages: [...conversationHistory] };
|
|
87
|
+
await run.processStream(firstInputs, config);
|
|
88
|
+
const finalMessages = run.getRunMessages();
|
|
89
|
+
if (finalMessages) {
|
|
90
|
+
conversationHistory.push(...finalMessages);
|
|
91
|
+
console.dir(conversationHistory, { depth: null });
|
|
92
|
+
}
|
|
93
|
+
// Second request - should use the cache
|
|
94
|
+
console.log('\n\nTest 2: Second request (should use cache)');
|
|
95
|
+
const userMessage2 = `Summarize the key concepts from the context information.`;
|
|
96
|
+
conversationHistory.push(new HumanMessage(userMessage2));
|
|
97
|
+
|
|
98
|
+
console.log('Running second query to use cache...');
|
|
99
|
+
const secondInputs = { messages: [...conversationHistory] };
|
|
100
|
+
await run.processStream(secondInputs, config);
|
|
101
|
+
console.log('\n\nPrompt caching test completed!');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
105
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
106
|
+
console.log('Conversation history:');
|
|
107
|
+
console.dir(conversationHistory, { depth: null });
|
|
108
|
+
console.log('Content parts:');
|
|
109
|
+
console.dir(_contentParts, { depth: null });
|
|
110
|
+
process.exit(1);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
process.on('uncaughtException', (err) => {
|
|
114
|
+
console.error('Uncaught Exception:', err);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
testPromptCaching().catch((err) => {
|
|
118
|
+
console.error(err);
|
|
119
|
+
console.log('Conversation history:');
|
|
120
|
+
console.dir(conversationHistory, { depth: null });
|
|
121
|
+
console.log('Content parts:');
|
|
122
|
+
console.dir(_contentParts, { depth: null });
|
|
123
|
+
process.exit(1);
|
|
124
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
// src/scripts/cli.ts
|
|
3
|
+
import { config } from 'dotenv';
|
|
4
|
+
config();
|
|
5
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
6
|
+
import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
|
|
7
|
+
import type * as t from '@/types';
|
|
8
|
+
import { ModelEndHandler, ToolEndHandler } from '@/events';
|
|
9
|
+
import { ChatModelStreamHandler } from '@/stream';
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import { getArgs } from '@/scripts/args';
|
|
13
|
+
import { Run } from '@/run';
|
|
14
|
+
import { GraphEvents, Callback } from '@/common';
|
|
15
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
16
|
+
|
|
17
|
+
const conversationHistory: BaseMessage[] = [];
|
|
18
|
+
async function testStandardStreaming(): Promise<void> {
|
|
19
|
+
const { userName, location, provider, currentDate } = await getArgs();
|
|
20
|
+
const customHandlers = {
|
|
21
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
22
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
23
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
24
|
+
[GraphEvents.ON_RUN_STEP_COMPLETED]: {
|
|
25
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
26
|
+
console.log('====== ON_RUN_STEP_COMPLETED ======');
|
|
27
|
+
console.dir(data, { depth: null });
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
31
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
32
|
+
console.log('====== ON_RUN_STEP ======');
|
|
33
|
+
console.dir(data, { depth: null });
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
37
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
38
|
+
console.log('====== ON_RUN_STEP_DELTA ======');
|
|
39
|
+
console.dir(data, { depth: null });
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
43
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
44
|
+
console.log('====== ON_MESSAGE_DELTA ======');
|
|
45
|
+
console.dir(data, { depth: null });
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
[GraphEvents.TOOL_START]: {
|
|
49
|
+
handle: (_event: string, data: t.StreamEventData, metadata?: Record<string, unknown>): void => {
|
|
50
|
+
console.log('====== TOOL_START ======');
|
|
51
|
+
console.dir(data, { depth: null });
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
// [GraphEvents.LLM_STREAM]: new LLMStreamHandler(),
|
|
55
|
+
// [GraphEvents.LLM_START]: {
|
|
56
|
+
// handle: (_event: string, data: t.StreamEventData): void => {
|
|
57
|
+
// console.log('====== LLM_START ======');
|
|
58
|
+
// console.dir(data, { depth: null });
|
|
59
|
+
// }
|
|
60
|
+
// },
|
|
61
|
+
// [GraphEvents.LLM_END]: {
|
|
62
|
+
// handle: (_event: string, data: t.StreamEventData): void => {
|
|
63
|
+
// console.log('====== LLM_END ======');
|
|
64
|
+
// console.dir(data, { depth: null });
|
|
65
|
+
// }
|
|
66
|
+
// },
|
|
67
|
+
/*
|
|
68
|
+
[GraphEvents.CHAIN_START]: {
|
|
69
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
70
|
+
console.log('====== CHAIN_START ======');
|
|
71
|
+
// console.dir(data, { depth: null });
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
[GraphEvents.CHAIN_END]: {
|
|
75
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
76
|
+
console.log('====== CHAIN_END ======');
|
|
77
|
+
// console.dir(data, { depth: null });
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
*/
|
|
81
|
+
// [GraphEvents.CHAT_MODEL_START]: {
|
|
82
|
+
// handle: (_event: string, _data: t.StreamEventData): void => {
|
|
83
|
+
// console.log('====== CHAT_MODEL_START ======');
|
|
84
|
+
// console.dir(_data, { depth: null });
|
|
85
|
+
// // Intentionally left empty
|
|
86
|
+
// }
|
|
87
|
+
// },
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const llmConfig = getLLMConfig(provider);
|
|
91
|
+
|
|
92
|
+
const run = await Run.create<t.IState>({
|
|
93
|
+
runId: 'test-run-id',
|
|
94
|
+
graphConfig: {
|
|
95
|
+
type: 'standard',
|
|
96
|
+
llmConfig,
|
|
97
|
+
tools: [new TavilySearchResults()],
|
|
98
|
+
instructions: 'You are a friendly AI assistant. Always address the user by their name.',
|
|
99
|
+
additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
|
|
100
|
+
},
|
|
101
|
+
customHandlers,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const config = {
|
|
105
|
+
configurable: {
|
|
106
|
+
provider,
|
|
107
|
+
thread_id: 'conversation-num-1',
|
|
108
|
+
},
|
|
109
|
+
streamMode: 'values',
|
|
110
|
+
version: 'v2' as const,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
console.log(' Test 1: Initial greeting');
|
|
114
|
+
|
|
115
|
+
conversationHistory.push(new HumanMessage(`Hi I'm ${userName}.`));
|
|
116
|
+
let inputs = {
|
|
117
|
+
messages: conversationHistory,
|
|
118
|
+
};
|
|
119
|
+
const contentParts = await run.processStream(inputs, config,
|
|
120
|
+
// {
|
|
121
|
+
// [Callback.TOOL_START]: (graph, ...args) => {
|
|
122
|
+
// console.log('TOOL_START callback');
|
|
123
|
+
// },
|
|
124
|
+
// [Callback.TOOL_END]: (graph, ...args) => {
|
|
125
|
+
// console.log('TOOL_END callback');
|
|
126
|
+
// },
|
|
127
|
+
// }
|
|
128
|
+
);
|
|
129
|
+
const finalMessages = run.getRunMessages();
|
|
130
|
+
if (finalMessages) {
|
|
131
|
+
conversationHistory.push(...finalMessages);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
console.log(' Test 2: Weather query');
|
|
135
|
+
|
|
136
|
+
const userMessage = `
|
|
137
|
+
Make a search for the weather in ${location} today, which is ${currentDate}.
|
|
138
|
+
Make sure to always refer to me by name.
|
|
139
|
+
After giving me a thorough summary, tell me a joke about the weather forecast we went over.
|
|
140
|
+
`;
|
|
141
|
+
|
|
142
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
143
|
+
|
|
144
|
+
inputs = {
|
|
145
|
+
messages: conversationHistory,
|
|
146
|
+
};
|
|
147
|
+
const contentParts2 = await run.processStream(inputs, config);
|
|
148
|
+
const finalMessages2 = run.getRunMessages();
|
|
149
|
+
if (finalMessages2) {
|
|
150
|
+
conversationHistory.push(...finalMessages2);
|
|
151
|
+
console.dir(conversationHistory, { depth: null });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
156
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
157
|
+
console.log('Conversation history:');
|
|
158
|
+
console.dir(conversationHistory, { depth: null });
|
|
159
|
+
process.exit(1);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
testStandardStreaming().catch((err) => {
|
|
163
|
+
console.error(err);
|
|
164
|
+
console.log('Conversation history:');
|
|
165
|
+
console.dir(conversationHistory, { depth: null });
|
|
166
|
+
process.exit(1);
|
|
167
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
// src/scripts/cli2.ts
|
|
3
|
+
import { config } from 'dotenv';
|
|
4
|
+
config();
|
|
5
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
6
|
+
import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
|
|
7
|
+
import type * as t from '@/types';
|
|
8
|
+
import { ChatModelStreamHandler } from '@/stream';
|
|
9
|
+
import { TestLLMStreamHandler } from '@/events';
|
|
10
|
+
|
|
11
|
+
import { getArgs } from '@/scripts/args';
|
|
12
|
+
import { Run } from '@/run';
|
|
13
|
+
import { GraphEvents } from '@/common';
|
|
14
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
15
|
+
|
|
16
|
+
const conversationHistory: BaseMessage[] = [];
|
|
17
|
+
|
|
18
|
+
async function executePersonalizedQuerySuite(): Promise<void> {
|
|
19
|
+
const { userName, location, provider, currentDate } = await getArgs();
|
|
20
|
+
|
|
21
|
+
const customHandlers = {
|
|
22
|
+
[GraphEvents.LLM_STREAM]: new TestLLMStreamHandler(),
|
|
23
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
24
|
+
[GraphEvents.LLM_START]: {
|
|
25
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
26
|
+
console.log('====== LLM_START ======');
|
|
27
|
+
console.dir(data, { depth: null });
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
[GraphEvents.LLM_END]: {
|
|
31
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
32
|
+
console.log('====== LLM_END ======');
|
|
33
|
+
console.dir(data, { depth: null });
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
[GraphEvents.CHAT_MODEL_START]: {
|
|
37
|
+
handle: (_event: string, _data: t.StreamEventData): void => {
|
|
38
|
+
console.log('====== CHAT_MODEL_START ======');
|
|
39
|
+
console.dir(_data, { depth: null });
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
[GraphEvents.CHAT_MODEL_END]: {
|
|
43
|
+
handle: (_event: string, _data: t.StreamEventData): void => {
|
|
44
|
+
console.log('====== CHAT_MODEL_END ======');
|
|
45
|
+
console.dir(_data, { depth: null });
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
[GraphEvents.TOOL_START]: {
|
|
49
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
50
|
+
console.log('====== TOOL_START ======');
|
|
51
|
+
console.dir(data, { depth: null });
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
[GraphEvents.TOOL_END]: {
|
|
55
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
56
|
+
console.log('====== TOOL_END ======');
|
|
57
|
+
console.dir(data, { depth: null });
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const llmConfig = getLLMConfig(provider);
|
|
63
|
+
|
|
64
|
+
const run = await Run.create<t.IState>({
|
|
65
|
+
runId: 'test-run-id',
|
|
66
|
+
graphConfig: {
|
|
67
|
+
type: 'standard',
|
|
68
|
+
llmConfig,
|
|
69
|
+
tools: [new TavilySearchResults()],
|
|
70
|
+
},
|
|
71
|
+
customHandlers,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const sessionConfig = {
|
|
75
|
+
configurable: {
|
|
76
|
+
provider,
|
|
77
|
+
thread_id: `${userName}-session-${Date.now()}`,
|
|
78
|
+
instructions: `You are a knowledgeable and friendly AI assistant. Tailor your responses to ${userName}'s interests in ${location}.`,
|
|
79
|
+
additional_instructions: `Ensure each topic is thoroughly researched. Today is ${currentDate}. Maintain a warm, personalized tone throughout.`
|
|
80
|
+
},
|
|
81
|
+
streamMode: 'values',
|
|
82
|
+
version: 'v2' as const,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
console.log(`Initiating personalized query suite for ${userName}`);
|
|
86
|
+
|
|
87
|
+
const queryTopics = [
|
|
88
|
+
{ task: "current weather", description: "Provide a detailed weather forecast" },
|
|
89
|
+
{ task: "popular tourist attraction", description: "Describe a notable sight" },
|
|
90
|
+
{ task: "upcoming events", description: "List major events or festivals this week" },
|
|
91
|
+
// { task: "famous local dish", description: "Share a recipe for a regional specialty" },
|
|
92
|
+
// { task: "local humor", description: "Tell a joke related to the area or findings" }
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
const userPrompt = `
|
|
96
|
+
Greetings! I'm ${userName}, currently in ${location}. Today's date is ${currentDate}.
|
|
97
|
+
I'm seeking information on various aspects of ${location}. Please address the following:
|
|
98
|
+
|
|
99
|
+
${queryTopics.map((topic, index) => `${index + 1}. ${topic.description} in ${location}.`).join('\n ')}
|
|
100
|
+
|
|
101
|
+
For each topic, conduct a separate search to ensure accuracy and depth.
|
|
102
|
+
In your response, please address me as ${userName} and maintain a friendly, informative tone.
|
|
103
|
+
`;
|
|
104
|
+
|
|
105
|
+
conversationHistory.push(new HumanMessage(userPrompt));
|
|
106
|
+
|
|
107
|
+
const runInput = {
|
|
108
|
+
messages: conversationHistory,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const contentParts = await run.processStream(runInput, sessionConfig);
|
|
112
|
+
const finalMessages = run.getRunMessages();
|
|
113
|
+
if (finalMessages) {
|
|
114
|
+
conversationHistory.push(...finalMessages);
|
|
115
|
+
console.log("AI Assistant's Response:");
|
|
116
|
+
console.dir(conversationHistory, { depth: null });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
executePersonalizedQuerySuite().catch((error) => {
|
|
121
|
+
console.error("An error occurred during the query suite execution:", error);
|
|
122
|
+
console.log("Final conversation state:");
|
|
123
|
+
console.dir(conversationHistory, { depth: null });
|
|
124
|
+
process.exit(1);
|
|
125
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
// src/scripts/cli.ts
|
|
3
|
+
import { config } from 'dotenv';
|
|
4
|
+
config();
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { HumanMessage, BaseMessage } from '@langchain/core/messages';
|
|
7
|
+
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
8
|
+
import { TavilySearchResults } from '@langchain/community/tools/tavily_search';
|
|
9
|
+
import { tool } from "@langchain/core/tools";
|
|
10
|
+
import type * as t from '@/types';
|
|
11
|
+
import { ModelEndHandler, ToolEndHandler } from '@/events';
|
|
12
|
+
import { ChatModelStreamHandler } from '@/stream';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
import { getArgs } from '@/scripts/args';
|
|
16
|
+
import { Run } from '@/run';
|
|
17
|
+
import { GraphEvents, Callback } from '@/common';
|
|
18
|
+
import { getLLMConfig } from '@/utils/llmConfig';
|
|
19
|
+
|
|
20
|
+
const conversationHistory: BaseMessage[] = [];
|
|
21
|
+
async function testStandardStreaming(): Promise<void> {
|
|
22
|
+
const { userName, location, provider, currentDate } = await getArgs();
|
|
23
|
+
const customHandlers = {
|
|
24
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
25
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
26
|
+
[GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
|
|
27
|
+
[GraphEvents.ON_RUN_STEP]: {
|
|
28
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
29
|
+
console.log('====== ON_RUN_STEP ======');
|
|
30
|
+
console.dir(data, { depth: null });
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
[GraphEvents.ON_RUN_STEP_DELTA]: {
|
|
34
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
35
|
+
console.log('====== ON_RUN_STEP_DELTA ======');
|
|
36
|
+
console.dir(data, { depth: null });
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
[GraphEvents.ON_MESSAGE_DELTA]: {
|
|
40
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
41
|
+
console.log('====== ON_MESSAGE_DELTA ======');
|
|
42
|
+
console.dir(data, { depth: null });
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
[GraphEvents.TOOL_START]: {
|
|
46
|
+
handle: (_event: string, data: t.StreamEventData, metadata?: Record<string, unknown>): void => {
|
|
47
|
+
console.log('====== TOOL_START ======');
|
|
48
|
+
console.dir(data, { depth: null });
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
// [GraphEvents.LLM_STREAM]: new LLMStreamHandler(),
|
|
52
|
+
// [GraphEvents.LLM_START]: {
|
|
53
|
+
// handle: (_event: string, data: t.StreamEventData): void => {
|
|
54
|
+
// console.log('====== LLM_START ======');
|
|
55
|
+
// console.dir(data, { depth: null });
|
|
56
|
+
// }
|
|
57
|
+
// },
|
|
58
|
+
// [GraphEvents.LLM_END]: {
|
|
59
|
+
// handle: (_event: string, data: t.StreamEventData): void => {
|
|
60
|
+
// console.log('====== LLM_END ======');
|
|
61
|
+
// console.dir(data, { depth: null });
|
|
62
|
+
// }
|
|
63
|
+
// },
|
|
64
|
+
/*
|
|
65
|
+
[GraphEvents.CHAIN_START]: {
|
|
66
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
67
|
+
console.log('====== CHAIN_START ======');
|
|
68
|
+
// console.dir(data, { depth: null });
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
[GraphEvents.CHAIN_END]: {
|
|
72
|
+
handle: (_event: string, data: t.StreamEventData): void => {
|
|
73
|
+
console.log('====== CHAIN_END ======');
|
|
74
|
+
// console.dir(data, { depth: null });
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
*/
|
|
78
|
+
// [GraphEvents.CHAT_MODEL_START]: {
|
|
79
|
+
// handle: (_event: string, _data: t.StreamEventData): void => {
|
|
80
|
+
// console.log('====== CHAT_MODEL_START ======');
|
|
81
|
+
// console.dir(_data, { depth: null });
|
|
82
|
+
// // Intentionally left empty
|
|
83
|
+
// }
|
|
84
|
+
// },
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const llmConfig = getLLMConfig(provider);
|
|
88
|
+
|
|
89
|
+
const getWeather = tool(async ({ location }) => {
|
|
90
|
+
if (location === "SAN FRANCISCO") {
|
|
91
|
+
return "It's 60 degrees and foggy";
|
|
92
|
+
} else if (location.toLowerCase() === "san francisco") {
|
|
93
|
+
throw new Error("Input queries must be all capitals");
|
|
94
|
+
} else {
|
|
95
|
+
throw new Error("Invalid input.");
|
|
96
|
+
}
|
|
97
|
+
}, {
|
|
98
|
+
name: "get_weather",
|
|
99
|
+
description: "Call to get the current weather",
|
|
100
|
+
schema: z.object({
|
|
101
|
+
location: z.string(),
|
|
102
|
+
}),
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const run = await Run.create<t.IState>({
|
|
106
|
+
runId: 'test-run-id',
|
|
107
|
+
graphConfig: {
|
|
108
|
+
type: 'standard',
|
|
109
|
+
llmConfig,
|
|
110
|
+
tools: [getWeather],
|
|
111
|
+
// tools: [new TavilySearchResults()],
|
|
112
|
+
},
|
|
113
|
+
customHandlers,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const config = {
|
|
117
|
+
configurable: {
|
|
118
|
+
provider,
|
|
119
|
+
thread_id: 'conversation-num-1',
|
|
120
|
+
instructions: 'You are a friendly AI assistant. Always address the user by their name.',
|
|
121
|
+
additional_instructions: `The user's name is ${userName} and they are located in ${location}.`
|
|
122
|
+
},
|
|
123
|
+
streamMode: 'values',
|
|
124
|
+
version: 'v2' as const,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
console.log(' Test 1: Initial greeting');
|
|
128
|
+
|
|
129
|
+
conversationHistory.push(new HumanMessage(`Hi I'm ${userName}.`));
|
|
130
|
+
let inputs = {
|
|
131
|
+
messages: conversationHistory,
|
|
132
|
+
};
|
|
133
|
+
const contentParts = await run.processStream(inputs, config, {
|
|
134
|
+
callbacks: {
|
|
135
|
+
[Callback.TOOL_ERROR]: (graph, error, toolId) => {
|
|
136
|
+
console.error(`Tool ${toolId} failed with error: ${error.message}`);
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
const finalMessages = run.getRunMessages();
|
|
141
|
+
if (finalMessages) {
|
|
142
|
+
conversationHistory.push(...finalMessages);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
console.log(' Test 2: Weather query');
|
|
146
|
+
|
|
147
|
+
const userMessage = `
|
|
148
|
+
Make a search for the weather in ${location} today, which is ${currentDate}.
|
|
149
|
+
Make sure to always refer to me by name.
|
|
150
|
+
After giving me a thorough summary, tell me a joke about the weather forecast we went over.
|
|
151
|
+
`;
|
|
152
|
+
|
|
153
|
+
conversationHistory.push(new HumanMessage(userMessage));
|
|
154
|
+
|
|
155
|
+
inputs = {
|
|
156
|
+
messages: conversationHistory,
|
|
157
|
+
};
|
|
158
|
+
const contentParts2 = await run.processStream(inputs, config);
|
|
159
|
+
const finalMessages2 = run.getRunMessages();
|
|
160
|
+
if (finalMessages2) {
|
|
161
|
+
conversationHistory.push(...finalMessages2);
|
|
162
|
+
console.dir(conversationHistory, { depth: null });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
process.on('unhandledRejection', (reason, promise) => {
|
|
167
|
+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
168
|
+
console.log('Conversation history:');
|
|
169
|
+
console.dir(conversationHistory, { depth: null });
|
|
170
|
+
process.exit(1);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
testStandardStreaming().catch((err) => {
|
|
174
|
+
console.error(err);
|
|
175
|
+
console.log('Conversation history:');
|
|
176
|
+
console.dir(conversationHistory, { depth: null });
|
|
177
|
+
process.exit(1);
|
|
178
|
+
});
|