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,336 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
// src/tools/handlers.ts
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
import { ToolMessage } from '@langchain/core/messages';
|
|
5
|
+
import type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';
|
|
6
|
+
import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
|
|
7
|
+
import type { Graph } from '@/graphs';
|
|
8
|
+
import type * as t from '@/types';
|
|
9
|
+
import {
|
|
10
|
+
ToolCallTypes,
|
|
11
|
+
ContentTypes,
|
|
12
|
+
GraphEvents,
|
|
13
|
+
StepTypes,
|
|
14
|
+
Providers,
|
|
15
|
+
Constants,
|
|
16
|
+
} from '@/common';
|
|
17
|
+
import {
|
|
18
|
+
coerceAnthropicSearchResults,
|
|
19
|
+
isAnthropicWebSearchResult,
|
|
20
|
+
} from '@/tools/search/anthropic';
|
|
21
|
+
import { formatResultsForLLM } from '@/tools/search/format';
|
|
22
|
+
import { getMessageId } from '@/messages';
|
|
23
|
+
|
|
24
|
+
export function handleToolCallChunks({
|
|
25
|
+
graph,
|
|
26
|
+
stepKey,
|
|
27
|
+
toolCallChunks,
|
|
28
|
+
}: {
|
|
29
|
+
graph: Graph;
|
|
30
|
+
stepKey: string;
|
|
31
|
+
toolCallChunks: ToolCallChunk[];
|
|
32
|
+
}): void {
|
|
33
|
+
let prevStepId: string;
|
|
34
|
+
let prevRunStep: t.RunStep | undefined;
|
|
35
|
+
try {
|
|
36
|
+
prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
|
|
37
|
+
prevRunStep = graph.getRunStep(prevStepId);
|
|
38
|
+
} catch {
|
|
39
|
+
/** Edge Case: If no previous step exists, create a new message creation step */
|
|
40
|
+
const message_id = getMessageId(stepKey, graph, true) ?? '';
|
|
41
|
+
prevStepId = graph.dispatchRunStep(stepKey, {
|
|
42
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
43
|
+
message_creation: {
|
|
44
|
+
message_id,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
prevRunStep = graph.getRunStep(prevStepId);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
|
|
51
|
+
|
|
52
|
+
/** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
|
|
53
|
+
const tool_calls: ToolCall[] | undefined =
|
|
54
|
+
prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION
|
|
55
|
+
? []
|
|
56
|
+
: undefined;
|
|
57
|
+
|
|
58
|
+
/** Edge Case: `id` and `name` fields cannot be empty strings */
|
|
59
|
+
for (const toolCallChunk of toolCallChunks) {
|
|
60
|
+
if (toolCallChunk.name === '') {
|
|
61
|
+
toolCallChunk.name = undefined;
|
|
62
|
+
}
|
|
63
|
+
if (toolCallChunk.id === '') {
|
|
64
|
+
toolCallChunk.id = undefined;
|
|
65
|
+
} else if (
|
|
66
|
+
tool_calls != null &&
|
|
67
|
+
toolCallChunk.id != null &&
|
|
68
|
+
toolCallChunk.name != null
|
|
69
|
+
) {
|
|
70
|
+
tool_calls.push({
|
|
71
|
+
args: {},
|
|
72
|
+
id: toolCallChunk.id,
|
|
73
|
+
name: toolCallChunk.name,
|
|
74
|
+
type: ToolCallTypes.TOOL_CALL,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let stepId: string = _stepId;
|
|
80
|
+
const alreadyDispatched =
|
|
81
|
+
prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
|
|
82
|
+
graph.messageStepHasToolCalls.has(prevStepId);
|
|
83
|
+
if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
|
|
84
|
+
graph.dispatchMessageDelta(prevStepId, {
|
|
85
|
+
content: [
|
|
86
|
+
{
|
|
87
|
+
type: ContentTypes.TEXT,
|
|
88
|
+
text: '',
|
|
89
|
+
tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
94
|
+
stepId = graph.dispatchRunStep(stepKey, {
|
|
95
|
+
type: StepTypes.TOOL_CALLS,
|
|
96
|
+
tool_calls,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
graph.dispatchRunStepDelta(stepId, {
|
|
100
|
+
type: StepTypes.TOOL_CALLS,
|
|
101
|
+
tool_calls: toolCallChunks,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const handleToolCalls = (
|
|
106
|
+
toolCalls?: ToolCall[],
|
|
107
|
+
metadata?: Record<string, unknown>,
|
|
108
|
+
graph?: Graph
|
|
109
|
+
): void => {
|
|
110
|
+
if (!graph || !metadata) {
|
|
111
|
+
console.warn(`Graph or metadata not found in ${event} event`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!toolCalls) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (toolCalls.length === 0) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const stepKey = graph.getStepKey(metadata);
|
|
124
|
+
|
|
125
|
+
for (const tool_call of toolCalls) {
|
|
126
|
+
const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
|
|
127
|
+
tool_call.id = toolCallId;
|
|
128
|
+
if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
let prevStepId = '';
|
|
133
|
+
let prevRunStep: t.RunStep | undefined;
|
|
134
|
+
try {
|
|
135
|
+
prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
|
|
136
|
+
prevRunStep = graph.getRunStep(prevStepId);
|
|
137
|
+
} catch {
|
|
138
|
+
// no previous step
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const dispatchToolCallIds = (lastMessageStepId: string): void => {
|
|
142
|
+
graph.dispatchMessageDelta(lastMessageStepId, {
|
|
143
|
+
content: [
|
|
144
|
+
{
|
|
145
|
+
type: 'text',
|
|
146
|
+
text: '',
|
|
147
|
+
tool_call_ids: [toolCallId],
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
/* If the previous step exists and is a message creation */
|
|
153
|
+
if (
|
|
154
|
+
prevStepId &&
|
|
155
|
+
prevRunStep &&
|
|
156
|
+
prevRunStep.type === StepTypes.MESSAGE_CREATION
|
|
157
|
+
) {
|
|
158
|
+
dispatchToolCallIds(prevStepId);
|
|
159
|
+
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
160
|
+
/* If the previous step doesn't exist or is not a message creation */
|
|
161
|
+
} else if (
|
|
162
|
+
!prevRunStep ||
|
|
163
|
+
prevRunStep.type !== StepTypes.MESSAGE_CREATION
|
|
164
|
+
) {
|
|
165
|
+
const messageId = getMessageId(stepKey, graph, true) ?? '';
|
|
166
|
+
const stepId = graph.dispatchRunStep(stepKey, {
|
|
167
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
168
|
+
message_creation: {
|
|
169
|
+
message_id: messageId,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
dispatchToolCallIds(stepId);
|
|
173
|
+
graph.messageStepHasToolCalls.set(prevStepId, true);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
graph.dispatchRunStep(stepKey, {
|
|
177
|
+
type: StepTypes.TOOL_CALLS,
|
|
178
|
+
tool_calls: [tool_call],
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const toolResultTypes = new Set([
|
|
184
|
+
// 'tool_use',
|
|
185
|
+
// 'server_tool_use',
|
|
186
|
+
// 'input_json_delta',
|
|
187
|
+
'tool_result',
|
|
188
|
+
'web_search_result',
|
|
189
|
+
'web_search_tool_result',
|
|
190
|
+
]);
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Handles the result of a server tool call; in other words, a provider's built-in tool.
|
|
194
|
+
* As of 2025-07-06, only Anthropic handles server tool calls with this pattern.
|
|
195
|
+
*/
|
|
196
|
+
export function handleServerToolResult({
|
|
197
|
+
content,
|
|
198
|
+
metadata,
|
|
199
|
+
graph,
|
|
200
|
+
}: {
|
|
201
|
+
content?: string | t.MessageContentComplex[];
|
|
202
|
+
metadata?: Record<string, unknown>;
|
|
203
|
+
graph: Graph;
|
|
204
|
+
}): boolean {
|
|
205
|
+
let skipHandling = false;
|
|
206
|
+
if (metadata?.provider !== Providers.ANTHROPIC) {
|
|
207
|
+
return skipHandling;
|
|
208
|
+
}
|
|
209
|
+
if (
|
|
210
|
+
typeof content === 'string' ||
|
|
211
|
+
content == null ||
|
|
212
|
+
content.length === 0 ||
|
|
213
|
+
(content.length === 1 &&
|
|
214
|
+
(content[0] as t.ToolResultContent).tool_use_id == null)
|
|
215
|
+
) {
|
|
216
|
+
return skipHandling;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
for (const contentPart of content) {
|
|
220
|
+
const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;
|
|
221
|
+
if (toolUseId == null || toolUseId === '') {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const stepId = graph.toolCallStepIds.get(toolUseId);
|
|
225
|
+
if (stepId == null || stepId === '') {
|
|
226
|
+
console.warn(
|
|
227
|
+
`Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`
|
|
228
|
+
);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const runStep = graph.getRunStep(stepId);
|
|
232
|
+
if (!runStep) {
|
|
233
|
+
console.warn(
|
|
234
|
+
`Run step for ${stepId} does not exist, cannot dispatch tool result.`
|
|
235
|
+
);
|
|
236
|
+
continue;
|
|
237
|
+
} else if (runStep.type !== StepTypes.TOOL_CALLS) {
|
|
238
|
+
console.warn(
|
|
239
|
+
`Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`
|
|
240
|
+
);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const toolCall =
|
|
245
|
+
runStep.stepDetails.type === StepTypes.TOOL_CALLS
|
|
246
|
+
? (runStep.stepDetails.tool_calls?.find(
|
|
247
|
+
(toolCall) => toolCall.id === toolUseId
|
|
248
|
+
) as ToolCall)
|
|
249
|
+
: undefined;
|
|
250
|
+
|
|
251
|
+
if (!toolCall) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (
|
|
256
|
+
contentPart.type === 'web_search_result' ||
|
|
257
|
+
contentPart.type === 'web_search_tool_result'
|
|
258
|
+
) {
|
|
259
|
+
handleAnthropicSearchResults({
|
|
260
|
+
contentPart: contentPart as t.ToolResultContent,
|
|
261
|
+
toolCall,
|
|
262
|
+
metadata,
|
|
263
|
+
graph,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (!skipHandling) {
|
|
268
|
+
skipHandling = true;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return skipHandling;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function handleAnthropicSearchResults({
|
|
276
|
+
contentPart,
|
|
277
|
+
toolCall,
|
|
278
|
+
metadata,
|
|
279
|
+
graph,
|
|
280
|
+
}: {
|
|
281
|
+
contentPart: t.ToolResultContent;
|
|
282
|
+
toolCall: Partial<ToolCall>;
|
|
283
|
+
metadata?: Record<string, unknown>;
|
|
284
|
+
graph: Graph;
|
|
285
|
+
}): void {
|
|
286
|
+
if (!Array.isArray(contentPart.content)) {
|
|
287
|
+
console.warn(
|
|
288
|
+
`Expected content to be an array, got ${typeof contentPart.content}`
|
|
289
|
+
);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (!isAnthropicWebSearchResult(contentPart.content[0])) {
|
|
294
|
+
console.warn(
|
|
295
|
+
`Expected content to be an Anthropic web search result, got ${JSON.stringify(
|
|
296
|
+
contentPart.content
|
|
297
|
+
)}`
|
|
298
|
+
);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const turn = graph.invokedToolIds?.size ?? 0;
|
|
303
|
+
const searchResultData = coerceAnthropicSearchResults({
|
|
304
|
+
turn,
|
|
305
|
+
results: contentPart.content as AnthropicWebSearchResultBlockParam[],
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const name = toolCall.name;
|
|
309
|
+
const input = toolCall.args ?? {};
|
|
310
|
+
const artifact = {
|
|
311
|
+
[Constants.WEB_SEARCH]: searchResultData,
|
|
312
|
+
};
|
|
313
|
+
const { output: formattedOutput } = formatResultsForLLM(
|
|
314
|
+
turn,
|
|
315
|
+
searchResultData
|
|
316
|
+
);
|
|
317
|
+
const output = new ToolMessage({
|
|
318
|
+
name,
|
|
319
|
+
artifact,
|
|
320
|
+
content: formattedOutput,
|
|
321
|
+
tool_call_id: toolCall.id!,
|
|
322
|
+
});
|
|
323
|
+
const toolEndData: t.ToolEndData = {
|
|
324
|
+
input,
|
|
325
|
+
output,
|
|
326
|
+
};
|
|
327
|
+
graph.handlerRegistry
|
|
328
|
+
?.getHandler(GraphEvents.TOOL_END)
|
|
329
|
+
?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);
|
|
330
|
+
|
|
331
|
+
if (graph.invokedToolIds == null) {
|
|
332
|
+
graph.invokedToolIds = new Set<string>();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
graph.invokedToolIds.add(toolCall.id!);
|
|
336
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnthropicTextBlockParam,
|
|
3
|
+
AnthropicWebSearchResultBlockParam,
|
|
4
|
+
} from '@/llm/anthropic/types';
|
|
5
|
+
import type { SearchResultData, ProcessedOrganic } from './types';
|
|
6
|
+
import { getAttribution } from './utils';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Coerces Anthropic web search results to the SearchResultData format
|
|
10
|
+
* @param results - Array of Anthropic web search results
|
|
11
|
+
* @param turn - The turn number to associate with these results
|
|
12
|
+
* @returns SearchResultData with minimal ProcessedOrganic items
|
|
13
|
+
*/
|
|
14
|
+
export function coerceAnthropicSearchResults({
|
|
15
|
+
results,
|
|
16
|
+
turn = 0,
|
|
17
|
+
}: {
|
|
18
|
+
results: (AnthropicTextBlockParam | AnthropicWebSearchResultBlockParam)[];
|
|
19
|
+
turn?: number;
|
|
20
|
+
}): SearchResultData {
|
|
21
|
+
const organic: ProcessedOrganic[] = results
|
|
22
|
+
.filter((result) => result.type === 'web_search_result')
|
|
23
|
+
.map((result, index) => ({
|
|
24
|
+
link: result.url,
|
|
25
|
+
position: index + 1,
|
|
26
|
+
title: result.title,
|
|
27
|
+
date: result.page_age ?? undefined,
|
|
28
|
+
attribution: getAttribution(result.url),
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
turn,
|
|
33
|
+
organic,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Helper function to check if an object is an Anthropic web search result
|
|
39
|
+
*/
|
|
40
|
+
export function isAnthropicWebSearchResult(
|
|
41
|
+
obj: unknown
|
|
42
|
+
): obj is AnthropicWebSearchResultBlockParam {
|
|
43
|
+
return (
|
|
44
|
+
typeof obj === 'object' &&
|
|
45
|
+
obj !== null &&
|
|
46
|
+
'type' in obj &&
|
|
47
|
+
obj.type === 'web_search_result' &&
|
|
48
|
+
'url' in obj &&
|
|
49
|
+
typeof (obj as Record<string, unknown>).url === 'string'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
// content.test.ts
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import { processContent } from './content';
|
|
6
|
+
|
|
7
|
+
describe('Link Processor', () => {
|
|
8
|
+
afterAll(() => {
|
|
9
|
+
if (fs.existsSync('./temp.html')) {
|
|
10
|
+
fs.unlinkSync('./temp.html');
|
|
11
|
+
}
|
|
12
|
+
if (fs.existsSync('./temp.md')) {
|
|
13
|
+
fs.unlinkSync('./temp.md');
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
// Basic functionality tests
|
|
17
|
+
test('should replace basic links with references', () => {
|
|
18
|
+
const html = `
|
|
19
|
+
<p>Test with <a href="https://example.com/link" title="Example">a link</a></p>
|
|
20
|
+
<p>And an <img src="https://example.com/img.jpg" alt="image"></p>
|
|
21
|
+
<p>Plus a <video src="https://example.com/video.mp4"></video></p>
|
|
22
|
+
`;
|
|
23
|
+
|
|
24
|
+
const markdown = `
|
|
25
|
+
Test with [a link](https://example.com/link "Example")
|
|
26
|
+
And an 
|
|
27
|
+
Plus a [video](https://example.com/video.mp4)
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
const result = processContent(html, markdown);
|
|
31
|
+
|
|
32
|
+
expect(result.links.length).toBe(1);
|
|
33
|
+
expect(result.images.length).toBe(1);
|
|
34
|
+
expect(result.videos.length).toBe(1);
|
|
35
|
+
expect(result.markdown).toContain('link#1');
|
|
36
|
+
expect(result.markdown).toContain('image#1');
|
|
37
|
+
expect(result.markdown).toContain('video#1');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Edge case tests
|
|
41
|
+
test('should handle links with parentheses and special characters', () => {
|
|
42
|
+
const html = `
|
|
43
|
+
<a href="https://example.com/page(1).html" title="Parens">Link with parens</a>
|
|
44
|
+
<a href="https://example.com/path?query=test¶m=value">Link with query</a>
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const markdown = `
|
|
48
|
+
[Link with parens](https://example.com/page(1).html "Parens")
|
|
49
|
+
[Link with query](https://example.com/path?query=test¶m=value)
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const result = processContent(html, markdown);
|
|
53
|
+
|
|
54
|
+
expect(result.links.length).toBe(2);
|
|
55
|
+
expect(result.markdown).toContain('link#1');
|
|
56
|
+
expect(result.markdown).toContain('link#2');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Performance test with large files
|
|
60
|
+
test('should process large files efficiently', () => {
|
|
61
|
+
const html = fs.readFileSync('src/tools/search/test.html', 'utf-8');
|
|
62
|
+
const markdown = fs.readFileSync('src/tools/search/test.md', 'utf-8');
|
|
63
|
+
|
|
64
|
+
// const largeHtml = generateLargeHtml(1000); // 1000 links
|
|
65
|
+
// fs.writeFileSync('./temp.html', largeHtml);
|
|
66
|
+
|
|
67
|
+
// const largeMd = generateLargeMarkdown(1000); // 1000 links
|
|
68
|
+
// fs.writeFileSync('./temp.md', largeMd);
|
|
69
|
+
|
|
70
|
+
// const html = fs.readFileSync('./temp.html', 'utf-8');
|
|
71
|
+
// const markdown = fs.readFileSync('./temp.md', 'utf-8');
|
|
72
|
+
|
|
73
|
+
// Measure time taken to process
|
|
74
|
+
const startTime = process.hrtime();
|
|
75
|
+
const result = processContent(html, markdown);
|
|
76
|
+
const elapsed = process.hrtime(startTime);
|
|
77
|
+
const timeInMs = elapsed[0] * 1000 + elapsed[1] / 1000000;
|
|
78
|
+
|
|
79
|
+
console.log(
|
|
80
|
+
`Processed ${result.links.length} links, ${result.images.length} images, and ${result.videos.length} videos in ${timeInMs.toFixed(2)}ms`
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Basic validations for large file processing
|
|
84
|
+
expect(result.links.length).toBeGreaterThan(0);
|
|
85
|
+
expect(result.markdown).toContain('link#');
|
|
86
|
+
|
|
87
|
+
// Check if all links were replaced (sample check)
|
|
88
|
+
expect(result.markdown).not.toContain('https://example.com/link');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Memory usage test
|
|
92
|
+
test('should have reasonable memory usage', () => {
|
|
93
|
+
const html = fs.readFileSync('src/tools/search/test.html', 'utf-8');
|
|
94
|
+
const markdown = fs.readFileSync('src/tools/search/test.md', 'utf-8');
|
|
95
|
+
|
|
96
|
+
const beforeMem = process.memoryUsage();
|
|
97
|
+
processContent(html, markdown);
|
|
98
|
+
const afterMem = process.memoryUsage();
|
|
99
|
+
|
|
100
|
+
const heapUsed = (afterMem.heapUsed - beforeMem.heapUsed) / 1024 / 1024; // MB
|
|
101
|
+
|
|
102
|
+
console.log(`Memory used: ${heapUsed.toFixed(2)} MB`);
|
|
103
|
+
|
|
104
|
+
// This is a loose check - actual thresholds depend on your environment
|
|
105
|
+
expect(heapUsed).toBeLessThan(100); // Should use less than 100MB additional heap
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Real-world file test (if available)
|
|
109
|
+
test('should process real-world Wikipedia content', () => {
|
|
110
|
+
// Try to find real-world test files if they exist
|
|
111
|
+
const wikiHtml = 'src/tools/search/test.html';
|
|
112
|
+
const wikiMd = 'src/tools/search/test.md';
|
|
113
|
+
|
|
114
|
+
if (fs.existsSync(wikiHtml) && fs.existsSync(wikiMd)) {
|
|
115
|
+
const html = fs.readFileSync(wikiHtml, 'utf-8');
|
|
116
|
+
const markdown = fs.readFileSync(wikiMd, 'utf-8');
|
|
117
|
+
|
|
118
|
+
const result = processContent(html, markdown);
|
|
119
|
+
|
|
120
|
+
console.log(
|
|
121
|
+
`Processed ${result.links.length} Wikipedia links, ${result.images.length} images, and ${result.videos.length} videos`
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(result.links.length).toBeGreaterThan(10); // Wikipedia articles typically have many links
|
|
125
|
+
expect(result.markdown).not.toMatch(/\]\(https?:\/\/[^\s")]+\)/); // No regular URLs should remain
|
|
126
|
+
} else {
|
|
127
|
+
console.log('Wikipedia test files not found, skipping this test');
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Helper function to generate large HTML test data
|
|
133
|
+
function generateLargeHtml(linkCount: number): string {
|
|
134
|
+
let html = '<html><body>';
|
|
135
|
+
|
|
136
|
+
for (let i = 1; i <= linkCount; i++) {
|
|
137
|
+
html += `<p>Paragraph ${i} with <a href="https://example.com/link${i}" title="Link ${i}">link ${i}</a>`;
|
|
138
|
+
|
|
139
|
+
if (i % 10 === 0) {
|
|
140
|
+
html += ` and <img src="https://example.com/image${i / 10}.jpg" alt="Image ${i / 10}">`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (i % 50 === 0) {
|
|
144
|
+
html += ` and <video src="https://example.com/video${i / 50}.mp4" title="Video ${i / 50}"></video>`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
html += '</p>';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
html += '</body></html>';
|
|
151
|
+
return html;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Helper function to generate large Markdown test data */
|
|
155
|
+
function generateLargeMarkdown(linkCount: number): string {
|
|
156
|
+
let markdown = '# Test Document\n\n';
|
|
157
|
+
|
|
158
|
+
for (let i = 1; i <= linkCount; i++) {
|
|
159
|
+
markdown += `Paragraph ${i} with [link ${i}](https://example.com/link${i} "Link ${i}")`;
|
|
160
|
+
|
|
161
|
+
if (i % 10 === 0) {
|
|
162
|
+
markdown += ` and `;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (i % 50 === 0) {
|
|
166
|
+
markdown += ` and [Video ${i / 50}](https://example.com/video${i / 50}.mp4 "Video ${i / 50}")`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
markdown += '\n\n';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return markdown;
|
|
173
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import * as cheerio from 'cheerio';
|
|
2
|
+
import type { References, MediaReference } from './types';
|
|
3
|
+
|
|
4
|
+
export function processContent(
|
|
5
|
+
html: string,
|
|
6
|
+
markdown: string
|
|
7
|
+
): {
|
|
8
|
+
markdown: string;
|
|
9
|
+
} & References {
|
|
10
|
+
const linkMap = new Map<string, MediaReference>();
|
|
11
|
+
const imageMap = new Map<string, MediaReference>();
|
|
12
|
+
const videoMap = new Map<string, MediaReference>();
|
|
13
|
+
const iframeMap = new Map<string, MediaReference>();
|
|
14
|
+
|
|
15
|
+
const $ = cheerio.load(html, {
|
|
16
|
+
xmlMode: false,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// Extract all media references
|
|
20
|
+
$('a[href]').each((_, el) => {
|
|
21
|
+
const href = $(el).attr('href');
|
|
22
|
+
if (href != null && href) {
|
|
23
|
+
linkMap.set(href, {
|
|
24
|
+
originalUrl: href,
|
|
25
|
+
title: $(el).attr('title'),
|
|
26
|
+
text: $(el).text().trim(),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
$('img[src]').each((_, el) => {
|
|
32
|
+
const src = $(el).attr('src');
|
|
33
|
+
if (src != null && src) {
|
|
34
|
+
imageMap.set(src, {
|
|
35
|
+
originalUrl: src,
|
|
36
|
+
title: $(el).attr('alt') ?? $(el).attr('title'),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Handle videos (dedicated video elements and video platforms in iframes)
|
|
42
|
+
$('video[src], iframe[src*="youtube"], iframe[src*="vimeo"]').each(
|
|
43
|
+
(_, el) => {
|
|
44
|
+
const src = $(el).attr('src');
|
|
45
|
+
if (src != null && src) {
|
|
46
|
+
videoMap.set(src, {
|
|
47
|
+
originalUrl: src,
|
|
48
|
+
title: $(el).attr('title'),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// Handle all other generic iframes that aren't already captured as videos
|
|
55
|
+
$('iframe').each((_, el) => {
|
|
56
|
+
const src = $(el).attr('src');
|
|
57
|
+
if (
|
|
58
|
+
src != null &&
|
|
59
|
+
src &&
|
|
60
|
+
!src.includes('youtube') &&
|
|
61
|
+
!src.includes('vimeo')
|
|
62
|
+
) {
|
|
63
|
+
iframeMap.set(src, {
|
|
64
|
+
originalUrl: src,
|
|
65
|
+
title: $(el).attr('title'),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Create lookup maps with indices
|
|
71
|
+
const linkIndexMap = new Map<string, number>();
|
|
72
|
+
const imageIndexMap = new Map<string, number>();
|
|
73
|
+
const videoIndexMap = new Map<string, number>();
|
|
74
|
+
const iframeIndexMap = new Map<string, number>();
|
|
75
|
+
|
|
76
|
+
Array.from(linkMap.keys()).forEach((url, i) => linkIndexMap.set(url, i + 1));
|
|
77
|
+
Array.from(imageMap.keys()).forEach((url, i) =>
|
|
78
|
+
imageIndexMap.set(url, i + 1)
|
|
79
|
+
);
|
|
80
|
+
Array.from(videoMap.keys()).forEach((url, i) =>
|
|
81
|
+
videoIndexMap.set(url, i + 1)
|
|
82
|
+
);
|
|
83
|
+
Array.from(iframeMap.keys()).forEach((url, i) =>
|
|
84
|
+
iframeIndexMap.set(url, i + 1)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// Process the markdown
|
|
88
|
+
let result = markdown;
|
|
89
|
+
|
|
90
|
+
// Replace each URL one by one, starting with the longest URLs first to avoid partial matches
|
|
91
|
+
const allUrls = [
|
|
92
|
+
...Array.from(imageMap.keys()).map((url) => ({
|
|
93
|
+
url,
|
|
94
|
+
type: 'image',
|
|
95
|
+
idx: imageIndexMap.get(url),
|
|
96
|
+
})),
|
|
97
|
+
...Array.from(videoMap.keys()).map((url) => ({
|
|
98
|
+
url,
|
|
99
|
+
type: 'video',
|
|
100
|
+
idx: videoIndexMap.get(url),
|
|
101
|
+
})),
|
|
102
|
+
...Array.from(iframeMap.keys()).map((url) => ({
|
|
103
|
+
url,
|
|
104
|
+
type: 'iframe',
|
|
105
|
+
idx: iframeIndexMap.get(url),
|
|
106
|
+
})),
|
|
107
|
+
...Array.from(linkMap.keys()).map((url) => ({
|
|
108
|
+
url,
|
|
109
|
+
type: 'link',
|
|
110
|
+
idx: linkIndexMap.get(url),
|
|
111
|
+
})),
|
|
112
|
+
].sort((a, b) => b.url.length - a.url.length);
|
|
113
|
+
|
|
114
|
+
// Create a function to escape special characters in URLs for regex
|
|
115
|
+
function escapeRegex(string: string): string {
|
|
116
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Replace each URL in the markdown
|
|
120
|
+
for (const { url, type, idx } of allUrls) {
|
|
121
|
+
// Create a regex that captures URLs in markdown links
|
|
122
|
+
const regex = new RegExp(`\\(${escapeRegex(url)}(?:\\s+"[^"]*")?\\)`, 'g');
|
|
123
|
+
|
|
124
|
+
result = result.replace(regex, (match) => {
|
|
125
|
+
// Keep any title attribute that might exist
|
|
126
|
+
const titleMatch = match.match(/\s+"([^"]*)"/);
|
|
127
|
+
const titlePart = titleMatch ? ` "${titleMatch[1]}"` : '';
|
|
128
|
+
|
|
129
|
+
return `(${type}#${idx}${titlePart})`;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
iframeMap.clear();
|
|
134
|
+
const links = Array.from(linkMap.values());
|
|
135
|
+
linkMap.clear();
|
|
136
|
+
const images = Array.from(imageMap.values());
|
|
137
|
+
imageMap.clear();
|
|
138
|
+
const videos = Array.from(videoMap.values());
|
|
139
|
+
videoMap.clear();
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
markdown: result,
|
|
143
|
+
links,
|
|
144
|
+
images,
|
|
145
|
+
videos,
|
|
146
|
+
};
|
|
147
|
+
}
|