@librechat/agents 2.4.318 → 2.4.319

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/cjs/events.cjs +3 -3
  2. package/dist/cjs/events.cjs.map +1 -1
  3. package/dist/cjs/main.cjs +5 -2
  4. package/dist/cjs/main.cjs.map +1 -1
  5. package/dist/cjs/messages/ids.cjs +23 -0
  6. package/dist/cjs/messages/ids.cjs.map +1 -0
  7. package/dist/cjs/stream.cjs +8 -155
  8. package/dist/cjs/stream.cjs.map +1 -1
  9. package/dist/cjs/tools/handlers.cjs +144 -0
  10. package/dist/cjs/tools/handlers.cjs.map +1 -0
  11. package/dist/cjs/tools/search/tool.cjs +2 -4
  12. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  13. package/dist/esm/events.mjs +1 -1
  14. package/dist/esm/events.mjs.map +1 -1
  15. package/dist/esm/main.mjs +3 -1
  16. package/dist/esm/main.mjs.map +1 -1
  17. package/dist/esm/messages/ids.mjs +21 -0
  18. package/dist/esm/messages/ids.mjs.map +1 -0
  19. package/dist/esm/stream.mjs +7 -152
  20. package/dist/esm/stream.mjs.map +1 -1
  21. package/dist/esm/tools/handlers.mjs +141 -0
  22. package/dist/esm/tools/handlers.mjs.map +1 -0
  23. package/dist/esm/tools/search/tool.mjs +2 -4
  24. package/dist/esm/tools/search/tool.mjs.map +1 -1
  25. package/dist/types/index.d.ts +1 -0
  26. package/dist/types/messages/ids.d.ts +3 -0
  27. package/dist/types/messages/index.d.ts +1 -0
  28. package/dist/types/stream.d.ts +0 -8
  29. package/dist/types/tools/handlers.d.ts +8 -0
  30. package/dist/types/tools/search/types.d.ts +8 -7
  31. package/package.json +1 -1
  32. package/src/events.ts +49 -15
  33. package/src/index.ts +1 -0
  34. package/src/messages/ids.ts +26 -0
  35. package/src/messages/index.ts +1 -0
  36. package/src/stream.ts +4 -186
  37. package/src/tools/handlers.ts +167 -0
  38. package/src/tools/search/tool.ts +2 -4
  39. package/src/tools/search/types.ts +9 -9
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ var nanoid = require('nanoid');
4
+ var _enum = require('../common/enum.cjs');
5
+ require('../messages/core.cjs');
6
+ var ids = require('../messages/ids.cjs');
7
+ require('@langchain/core/messages');
8
+
9
+ /* eslint-disable no-console */
10
+ // src/tools/handlers.ts
11
+ function handleToolCallChunks({ graph, stepKey, toolCallChunks, }) {
12
+ let prevStepId;
13
+ let prevRunStep;
14
+ try {
15
+ prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
16
+ prevRunStep = graph.getRunStep(prevStepId);
17
+ }
18
+ catch {
19
+ /** Edge Case: If no previous step exists, create a new message creation step */
20
+ const message_id = ids.getMessageId(stepKey, graph, true) ?? '';
21
+ prevStepId = graph.dispatchRunStep(stepKey, {
22
+ type: _enum.StepTypes.MESSAGE_CREATION,
23
+ message_creation: {
24
+ message_id,
25
+ },
26
+ });
27
+ prevRunStep = graph.getRunStep(prevStepId);
28
+ }
29
+ const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
30
+ /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
31
+ const tool_calls = prevStepId && prevRunStep && prevRunStep.type === _enum.StepTypes.MESSAGE_CREATION
32
+ ? []
33
+ : undefined;
34
+ /** Edge Case: `id` and `name` fields cannot be empty strings */
35
+ for (const toolCallChunk of toolCallChunks) {
36
+ if (toolCallChunk.name === '') {
37
+ toolCallChunk.name = undefined;
38
+ }
39
+ if (toolCallChunk.id === '') {
40
+ toolCallChunk.id = undefined;
41
+ }
42
+ else if (tool_calls != null &&
43
+ toolCallChunk.id != null &&
44
+ toolCallChunk.name != null) {
45
+ tool_calls.push({
46
+ args: {},
47
+ id: toolCallChunk.id,
48
+ name: toolCallChunk.name,
49
+ type: _enum.ToolCallTypes.TOOL_CALL,
50
+ });
51
+ }
52
+ }
53
+ let stepId = _stepId;
54
+ const alreadyDispatched = prevRunStep?.type === _enum.StepTypes.MESSAGE_CREATION &&
55
+ graph.messageStepHasToolCalls.has(prevStepId);
56
+ if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
57
+ graph.dispatchMessageDelta(prevStepId, {
58
+ content: [
59
+ {
60
+ type: _enum.ContentTypes.TEXT,
61
+ text: '',
62
+ tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
63
+ },
64
+ ],
65
+ });
66
+ graph.messageStepHasToolCalls.set(prevStepId, true);
67
+ stepId = graph.dispatchRunStep(stepKey, {
68
+ type: _enum.StepTypes.TOOL_CALLS,
69
+ tool_calls,
70
+ });
71
+ }
72
+ graph.dispatchRunStepDelta(stepId, {
73
+ type: _enum.StepTypes.TOOL_CALLS,
74
+ tool_calls: toolCallChunks,
75
+ });
76
+ }
77
+ const handleToolCalls = (toolCalls, metadata, graph) => {
78
+ if (!graph || !metadata) {
79
+ console.warn(`Graph or metadata not found in ${event} event`);
80
+ return;
81
+ }
82
+ if (!toolCalls) {
83
+ return;
84
+ }
85
+ if (toolCalls.length === 0) {
86
+ return;
87
+ }
88
+ const stepKey = graph.getStepKey(metadata);
89
+ for (const tool_call of toolCalls) {
90
+ const toolCallId = tool_call.id ?? `toolu_${nanoid.nanoid()}`;
91
+ tool_call.id = toolCallId;
92
+ if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
93
+ continue;
94
+ }
95
+ let prevStepId = '';
96
+ let prevRunStep;
97
+ try {
98
+ prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
99
+ prevRunStep = graph.getRunStep(prevStepId);
100
+ }
101
+ catch {
102
+ // no previous step
103
+ }
104
+ const dispatchToolCallIds = (lastMessageStepId) => {
105
+ graph.dispatchMessageDelta(lastMessageStepId, {
106
+ content: [
107
+ {
108
+ type: 'text',
109
+ text: '',
110
+ tool_call_ids: [toolCallId],
111
+ },
112
+ ],
113
+ });
114
+ };
115
+ /* If the previous step exists and is a message creation */
116
+ if (prevStepId &&
117
+ prevRunStep &&
118
+ prevRunStep.type === _enum.StepTypes.MESSAGE_CREATION) {
119
+ dispatchToolCallIds(prevStepId);
120
+ graph.messageStepHasToolCalls.set(prevStepId, true);
121
+ /* If the previous step doesn't exist or is not a message creation */
122
+ }
123
+ else if (!prevRunStep ||
124
+ prevRunStep.type !== _enum.StepTypes.MESSAGE_CREATION) {
125
+ const messageId = ids.getMessageId(stepKey, graph, true) ?? '';
126
+ const stepId = graph.dispatchRunStep(stepKey, {
127
+ type: _enum.StepTypes.MESSAGE_CREATION,
128
+ message_creation: {
129
+ message_id: messageId,
130
+ },
131
+ });
132
+ dispatchToolCallIds(stepId);
133
+ graph.messageStepHasToolCalls.set(prevStepId, true);
134
+ }
135
+ graph.dispatchRunStep(stepKey, {
136
+ type: _enum.StepTypes.TOOL_CALLS,
137
+ tool_calls: [tool_call],
138
+ });
139
+ }
140
+ };
141
+
142
+ exports.handleToolCallChunks = handleToolCallChunks;
143
+ exports.handleToolCalls = handleToolCalls;
144
+ //# sourceMappingURL=handlers.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlers.cjs","sources":["../../../src/tools/handlers.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/tools/handlers.ts\nimport { nanoid } from 'nanoid';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { Graph } from '@/graphs';\nimport type * as t from '@/types';\nimport { StepTypes, ContentTypes, ToolCallTypes } from '@/common';\nimport { getMessageId } from '@/messages';\n\nexport function handleToolCallChunks({\n graph,\n stepKey,\n toolCallChunks,\n}: {\n graph: Graph;\n stepKey: string;\n toolCallChunks: ToolCallChunk[];\n}): void {\n let prevStepId: string;\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n /** Edge Case: If no previous step exists, create a new message creation step */\n const message_id = getMessageId(stepKey, graph, true) ?? '';\n prevStepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n prevRunStep = graph.getRunStep(prevStepId);\n }\n\n const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);\n\n /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */\n const tool_calls: ToolCall[] | undefined =\n prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION\n ? []\n : undefined;\n\n /** Edge Case: `id` and `name` fields cannot be empty strings */\n for (const toolCallChunk of toolCallChunks) {\n if (toolCallChunk.name === '') {\n toolCallChunk.name = undefined;\n }\n if (toolCallChunk.id === '') {\n toolCallChunk.id = undefined;\n } else if (\n tool_calls != null &&\n toolCallChunk.id != null &&\n toolCallChunk.name != null\n ) {\n tool_calls.push({\n args: {},\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n type: ToolCallTypes.TOOL_CALL,\n });\n }\n }\n\n let stepId: string = _stepId;\n const alreadyDispatched =\n prevRunStep?.type === StepTypes.MESSAGE_CREATION &&\n graph.messageStepHasToolCalls.has(prevStepId);\n if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {\n graph.dispatchMessageDelta(prevStepId, {\n content: [\n {\n type: ContentTypes.TEXT,\n text: '',\n tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),\n },\n ],\n });\n graph.messageStepHasToolCalls.set(prevStepId, true);\n stepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.TOOL_CALLS,\n tool_calls,\n });\n }\n graph.dispatchRunStepDelta(stepId, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: toolCallChunks,\n });\n}\n\nexport const handleToolCalls = (\n toolCalls?: ToolCall[],\n metadata?: Record<string, unknown>,\n graph?: Graph\n): void => {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n if (!toolCalls) {\n return;\n }\n\n if (toolCalls.length === 0) {\n return;\n }\n\n const stepKey = graph.getStepKey(metadata);\n\n for (const tool_call of toolCalls) {\n const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;\n tool_call.id = toolCallId;\n if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n\n let prevStepId = '';\n let prevRunStep: t.RunStep | undefined;\n try {\n prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);\n prevRunStep = graph.getRunStep(prevStepId);\n } catch {\n // no previous step\n }\n\n const dispatchToolCallIds = (lastMessageStepId: string): void => {\n graph.dispatchMessageDelta(lastMessageStepId, {\n content: [\n {\n type: 'text',\n text: '',\n tool_call_ids: [toolCallId],\n },\n ],\n });\n };\n /* If the previous step exists and is a message creation */\n if (\n prevStepId &&\n prevRunStep &&\n prevRunStep.type === StepTypes.MESSAGE_CREATION\n ) {\n dispatchToolCallIds(prevStepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n /* If the previous step doesn't exist or is not a message creation */\n } else if (\n !prevRunStep ||\n prevRunStep.type !== StepTypes.MESSAGE_CREATION\n ) {\n const messageId = getMessageId(stepKey, graph, true) ?? '';\n const stepId = graph.dispatchRunStep(stepKey, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: messageId,\n },\n });\n dispatchToolCallIds(stepId);\n graph.messageStepHasToolCalls.set(prevStepId, true);\n }\n\n graph.dispatchRunStep(stepKey, {\n type: StepTypes.TOOL_CALLS,\n tool_calls: [tool_call],\n });\n }\n};\n"],"names":["getMessageId","StepTypes","ToolCallTypes","ContentTypes","nanoid"],"mappings":";;;;;;;;AAAA;AACA;AAQM,SAAU,oBAAoB,CAAC,EACnC,KAAK,EACL,OAAO,EACP,cAAc,GAKf,EAAA;AACC,IAAA,IAAI,UAAkB;AACtB,IAAA,IAAI,WAAkC;AACtC,IAAA,IAAI;AACF,QAAA,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE,KAAK,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC;AACxE,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,C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AAC,gBAAgB;AAChC,gBAAA,gBAAgB,EAAE;AAChB,oBAAA,UAAU,EAAE,SAAS;AACtB,iBAAA;AACF,aAAA,CAAC;YACF,mBAAmB,CAAC,MAAM,CAAC;YAC3B,KAAK,CAAC,uBAAuB,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,CAAC;;AAGrD,QAAA,KAAK,CAAC,eAAe,CAAC,OAAO,EAAE;YAC7B,IAAI,EAAEA,eAAS,CAAC,UAAU;YAC1B,UAAU,EAAE,CAAC,SAAS,CAAC;AACxB,SAAA,CAAC;;AAEN;;;;;"}
@@ -114,10 +114,8 @@ const createSearchTool = (config = {}) => {
114
114
  });
115
115
  const turn = runnableConfig.toolCall?.turn ?? 0;
116
116
  const { output, references } = format.formatResultsForLLM(turn, searchResult);
117
- return [
118
- output,
119
- { [_enum.Constants.WEB_SEARCH]: { turn, ...searchResult, references } },
120
- ];
117
+ const data = { turn, ...searchResult, references };
118
+ return [output, { [_enum.Constants.WEB_SEARCH]: data }];
121
119
  }, {
122
120
  name: _enum.Constants.WEB_SEARCH,
123
121
  description: `
@@ -1 +1 @@
1
- {"version":3,"file":"tool.cjs","sources":["../../../../src/tools/search/tool.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as t from './types';\nimport { createSearchAPI, createSourceProcessor } from './search';\nimport { createFirecrawlScraper } from './firecrawl';\nimport { expandHighlights } from './highlights';\nimport { formatResultsForLLM } from './format';\nimport { createReranker } from './rerankers';\nimport { Constants } from '@/common';\n\nconst DEFAULT_QUERY_DESCRIPTION = `\nGUIDELINES:\n- Start broad, then narrow: Begin with key concepts, then refine with specifics\n- Think like sources: Use terminology experts would use in the field\n- Consider perspective: Frame queries from different viewpoints for better results\n- Quality over quantity: A precise 3-4 word query often beats lengthy sentences\n\nTECHNIQUES (combine for power searches):\n- EXACT PHRASES: Use quotes (\"climate change report\")\n- EXCLUDE TERMS: Use minus to remove unwanted results (-wikipedia)\n- SITE-SPECIFIC: Restrict to websites (site:edu research)\n- FILETYPE: Find specific documents (filetype:pdf study)\n- OR OPERATOR: Find alternatives (electric OR hybrid cars)\n- DATE RANGE: Recent information (data after:2020)\n- WILDCARDS: Use * for unknown terms (how to * bread)\n- SPECIFIC QUESTIONS: Use who/what/when/where/why/how\n- DOMAIN TERMS: Include technical terminology for specialized topics\n- CONCISE TERMS: Prioritize keywords over sentences\n`.trim();\n\nconst DEFAULT_COUNTRY_DESCRIPTION = `Country code to localize search results.\nUse standard 2-letter country codes: \"us\", \"uk\", \"ca\", \"de\", \"fr\", \"jp\", \"br\", etc.\nProvide this when the search should return results specific to a particular country.\nExamples:\n- \"us\" for United States (default)\n- \"de\" for Germany\n- \"in\" for India\n`.trim();\n\n/**\n * Creates a search tool with a schema that dynamically includes the country field\n * only when the searchProvider is 'serper'.\n *\n * @param config - The search tool configuration\n * @returns A DynamicStructuredTool with a schema that depends on the searchProvider\n */\nexport const createSearchTool = (\n config: t.SearchToolConfig = {}\n): DynamicStructuredTool<typeof SearchToolSchema> => {\n const {\n searchProvider = 'serper',\n serperApiKey,\n searxngInstanceUrl,\n searxngApiKey,\n rerankerType = 'cohere',\n topResults = 5,\n strategies = ['no_extraction'],\n filterContent = true,\n firecrawlApiKey,\n firecrawlApiUrl,\n firecrawlFormats = ['markdown', 'html'],\n jinaApiKey,\n cohereApiKey,\n onSearchResults: _onSearchResults,\n } = config;\n\n const querySchema = z.string().describe(DEFAULT_QUERY_DESCRIPTION);\n const schemaObject: {\n query: z.ZodString;\n country?: z.ZodOptional<z.ZodString>;\n } = {\n query: querySchema,\n };\n\n if (searchProvider === 'serper') {\n schemaObject.country = z\n .string()\n .optional()\n .describe(DEFAULT_COUNTRY_DESCRIPTION);\n }\n\n const SearchToolSchema = z.object(schemaObject);\n\n const searchAPI = createSearchAPI({\n searchProvider,\n serperApiKey,\n searxngInstanceUrl,\n searxngApiKey,\n });\n\n const firecrawlScraper = createFirecrawlScraper({\n apiKey: firecrawlApiKey ?? process.env.FIRECRAWL_API_KEY,\n apiUrl: firecrawlApiUrl,\n formats: firecrawlFormats,\n });\n\n const selectedReranker = createReranker({\n rerankerType,\n jinaApiKey,\n cohereApiKey,\n });\n\n if (!selectedReranker) {\n console.warn('No reranker selected. Using default ranking.');\n }\n\n const sourceProcessor = createSourceProcessor(\n {\n reranker: selectedReranker,\n topResults,\n strategies,\n filterContent,\n },\n firecrawlScraper\n );\n\n const search = async ({\n query,\n country,\n proMode = true,\n maxSources = 5,\n onSearchResults,\n }: {\n query: string;\n country?: string;\n maxSources?: number;\n proMode?: boolean;\n onSearchResults?: (sources: t.SearchResult) => void;\n }): Promise<t.SearchResultData> => {\n try {\n const sources = await searchAPI.getSources({ query, country });\n onSearchResults?.(sources);\n\n if (!sources.success) {\n throw new Error(sources.error ?? 'Search failed');\n }\n\n const processedSources = await sourceProcessor.processSources(\n sources,\n maxSources,\n query,\n proMode\n );\n return expandHighlights(processedSources);\n } catch (error) {\n console.error('Error in search:', error);\n return {\n organic: [],\n topStories: [],\n images: [],\n relatedSearches: [],\n error: error instanceof Error ? error.message : String(error),\n };\n }\n };\n\n return tool<typeof SearchToolSchema>(\n async (params, runnableConfig) => {\n const { query, country: _c } = params;\n const country = typeof _c === 'string' && _c ? _c : undefined;\n const searchResult = await search({\n query,\n country,\n onSearchResults: _onSearchResults\n ? (result): void => {\n _onSearchResults(result, runnableConfig);\n }\n : undefined,\n });\n const turn = runnableConfig.toolCall?.turn ?? 0;\n const { output, references } = formatResultsForLLM(turn, searchResult);\n return [\n output,\n { [Constants.WEB_SEARCH]: { turn, ...searchResult, references } },\n ];\n },\n {\n name: Constants.WEB_SEARCH,\n description: `\nReal-time search. Results have required citation anchors.\n\nNote: Use ONCE per reply unless instructed otherwise.\n\nAnchors:\n- \\\\ue202turnXtypeY\n- X = turn idx, type = 'search' | 'news' | 'image' | 'ref', Y = item idx\n\nSpecial Markers:\n- \\\\ue203...\\\\ue204 — highlight start/end of cited text (for Standalone or Group citations)\n- \\\\ue200...\\\\ue201 — group block (e.g. \\\\ue200\\\\ue202turn0search1\\\\ue202turn0news2\\\\ue201)\n\n**CITE EVERY NON-OBVIOUS FACT/QUOTE:**\nUse anchor marker(s) immediately after the statement:\n- Standalone: \"Pure functions produce same output. \\\\ue202turn0search0\"\n- Standalone (multiple): \"Today's News \\\\ue202turn0search0\\\\ue202turn0news0\"\n- Highlight: \"\\\\ue203Highlight text.\\\\ue204\\\\ue202turn0news1\"\n- Group: \"Sources. \\\\ue200\\\\ue202turn0search0\\\\ue202turn0news1\\\\ue201\"\n- Group Highlight: \"\\\\ue203Highlight for group.\\\\ue204 \\\\ue200\\\\ue202turn0search0\\\\ue202turn0news1\\\\ue201\"\n- Image: \"See photo \\\\ue202turn0image0.\"\n\n**NEVER use markdown links, [1], or footnotes. CITE ONLY with anchors provided.**\n`.trim(),\n schema: SearchToolSchema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n};\n"],"names":["z","createSearchAPI","createFirecrawlScraper","createReranker","createSourceProcessor","search","expandHighlights","tool","formatResultsForLLM","Constants"],"mappings":";;;;;;;;;;;AAAA;AAWA,MAAM,yBAAyB,GAAG;;;;;;;;;;;;;;;;;;CAkBjC,CAAC,IAAI,EAAE;AAER,MAAM,2BAA2B,GAAG,CAAA;;;;;;;CAOnC,CAAC,IAAI,EAAE;AAER;;;;;;AAMG;MACU,gBAAgB,GAAG,CAC9B,MAA6B,GAAA,EAAE,KACmB;IAClD,MAAM,EACJ,cAAc,GAAG,QAAQ,EACzB,YAAY,EACZ,kBAAkB,EAClB,aAAa,EACb,YAAY,GAAG,QAAQ,EACvB,UAAU,GAAG,CAAC,EACd,UAAU,GAAG,CAAC,eAAe,CAAC,EAC9B,aAAa,GAAG,IAAI,EACpB,eAAe,EACf,eAAe,EACf,gBAAgB,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,EACvC,UAAU,EACV,YAAY,EACZ,eAAe,EAAE,gBAAgB,GA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EAAE,CAACC,eAAS,CAAC,UAAU,GAAG,EAAE,IAAI,EAAE,GAAG,YAAY,EAAE,UAAU,EAAE,EAAE;SAClE;AACH,KAAC,EACD;QACE,IAAI,EAAEA,eAAS,CAAC,UAAU;AAC1B,QAAA,WAAW,EAAE;;;;;;;;;;;;;;;;;;;;;;;AAuBlB,CAAA,CAAC,IAAI,EAAE;AACF,QAAA,MAAM,EAAE,gBAAgB;QACxB,cAAc,EAAEA,eAAS,CAAC,oBAAoB;AAC/C,KAAA,CACF;AACH;;;;"}
1
+ {"version":3,"file":"tool.cjs","sources":["../../../../src/tools/search/tool.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { z } from 'zod';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport type * as t from './types';\nimport { createSearchAPI, createSourceProcessor } from './search';\nimport { createFirecrawlScraper } from './firecrawl';\nimport { expandHighlights } from './highlights';\nimport { formatResultsForLLM } from './format';\nimport { createReranker } from './rerankers';\nimport { Constants } from '@/common';\n\nconst DEFAULT_QUERY_DESCRIPTION = `\nGUIDELINES:\n- Start broad, then narrow: Begin with key concepts, then refine with specifics\n- Think like sources: Use terminology experts would use in the field\n- Consider perspective: Frame queries from different viewpoints for better results\n- Quality over quantity: A precise 3-4 word query often beats lengthy sentences\n\nTECHNIQUES (combine for power searches):\n- EXACT PHRASES: Use quotes (\"climate change report\")\n- EXCLUDE TERMS: Use minus to remove unwanted results (-wikipedia)\n- SITE-SPECIFIC: Restrict to websites (site:edu research)\n- FILETYPE: Find specific documents (filetype:pdf study)\n- OR OPERATOR: Find alternatives (electric OR hybrid cars)\n- DATE RANGE: Recent information (data after:2020)\n- WILDCARDS: Use * for unknown terms (how to * bread)\n- SPECIFIC QUESTIONS: Use who/what/when/where/why/how\n- DOMAIN TERMS: Include technical terminology for specialized topics\n- CONCISE TERMS: Prioritize keywords over sentences\n`.trim();\n\nconst DEFAULT_COUNTRY_DESCRIPTION = `Country code to localize search results.\nUse standard 2-letter country codes: \"us\", \"uk\", \"ca\", \"de\", \"fr\", \"jp\", \"br\", etc.\nProvide this when the search should return results specific to a particular country.\nExamples:\n- \"us\" for United States (default)\n- \"de\" for Germany\n- \"in\" for India\n`.trim();\n\n/**\n * Creates a search tool with a schema that dynamically includes the country field\n * only when the searchProvider is 'serper'.\n *\n * @param config - The search tool configuration\n * @returns A DynamicStructuredTool with a schema that depends on the searchProvider\n */\nexport const createSearchTool = (\n config: t.SearchToolConfig = {}\n): DynamicStructuredTool<typeof SearchToolSchema> => {\n const {\n searchProvider = 'serper',\n serperApiKey,\n searxngInstanceUrl,\n searxngApiKey,\n rerankerType = 'cohere',\n topResults = 5,\n strategies = ['no_extraction'],\n filterContent = true,\n firecrawlApiKey,\n firecrawlApiUrl,\n firecrawlFormats = ['markdown', 'html'],\n jinaApiKey,\n cohereApiKey,\n onSearchResults: _onSearchResults,\n } = config;\n\n const querySchema = z.string().describe(DEFAULT_QUERY_DESCRIPTION);\n const schemaObject: {\n query: z.ZodString;\n country?: z.ZodOptional<z.ZodString>;\n } = {\n query: querySchema,\n };\n\n if (searchProvider === 'serper') {\n schemaObject.country = z\n .string()\n .optional()\n .describe(DEFAULT_COUNTRY_DESCRIPTION);\n }\n\n const SearchToolSchema = z.object(schemaObject);\n\n const searchAPI = createSearchAPI({\n searchProvider,\n serperApiKey,\n searxngInstanceUrl,\n searxngApiKey,\n });\n\n const firecrawlScraper = createFirecrawlScraper({\n apiKey: firecrawlApiKey ?? process.env.FIRECRAWL_API_KEY,\n apiUrl: firecrawlApiUrl,\n formats: firecrawlFormats,\n });\n\n const selectedReranker = createReranker({\n rerankerType,\n jinaApiKey,\n cohereApiKey,\n });\n\n if (!selectedReranker) {\n console.warn('No reranker selected. Using default ranking.');\n }\n\n const sourceProcessor = createSourceProcessor(\n {\n reranker: selectedReranker,\n topResults,\n strategies,\n filterContent,\n },\n firecrawlScraper\n );\n\n const search = async ({\n query,\n country,\n proMode = true,\n maxSources = 5,\n onSearchResults,\n }: {\n query: string;\n country?: string;\n maxSources?: number;\n proMode?: boolean;\n onSearchResults?: (sources: t.SearchResult) => void;\n }): Promise<t.SearchResultData> => {\n try {\n const sources = await searchAPI.getSources({ query, country });\n onSearchResults?.(sources);\n\n if (!sources.success) {\n throw new Error(sources.error ?? 'Search failed');\n }\n\n const processedSources = await sourceProcessor.processSources(\n sources,\n maxSources,\n query,\n proMode\n );\n return expandHighlights(processedSources);\n } catch (error) {\n console.error('Error in search:', error);\n return {\n organic: [],\n topStories: [],\n images: [],\n relatedSearches: [],\n error: error instanceof Error ? error.message : String(error),\n };\n }\n };\n\n return tool<typeof SearchToolSchema>(\n async (params, runnableConfig) => {\n const { query, country: _c } = params;\n const country = typeof _c === 'string' && _c ? _c : undefined;\n const searchResult = await search({\n query,\n country,\n onSearchResults: _onSearchResults\n ? (result): void => {\n _onSearchResults(result, runnableConfig);\n }\n : undefined,\n });\n const turn = runnableConfig.toolCall?.turn ?? 0;\n const { output, references } = formatResultsForLLM(turn, searchResult);\n const data: t.SearchResultData = { turn, ...searchResult, references };\n return [output, { [Constants.WEB_SEARCH]: data }];\n },\n {\n name: Constants.WEB_SEARCH,\n description: `\nReal-time search. Results have required citation anchors.\n\nNote: Use ONCE per reply unless instructed otherwise.\n\nAnchors:\n- \\\\ue202turnXtypeY\n- X = turn idx, type = 'search' | 'news' | 'image' | 'ref', Y = item idx\n\nSpecial Markers:\n- \\\\ue203...\\\\ue204 — highlight start/end of cited text (for Standalone or Group citations)\n- \\\\ue200...\\\\ue201 — group block (e.g. \\\\ue200\\\\ue202turn0search1\\\\ue202turn0news2\\\\ue201)\n\n**CITE EVERY NON-OBVIOUS FACT/QUOTE:**\nUse anchor marker(s) immediately after the statement:\n- Standalone: \"Pure functions produce same output. \\\\ue202turn0search0\"\n- Standalone (multiple): \"Today's News \\\\ue202turn0search0\\\\ue202turn0news0\"\n- Highlight: \"\\\\ue203Highlight text.\\\\ue204\\\\ue202turn0news1\"\n- Group: \"Sources. \\\\ue200\\\\ue202turn0search0\\\\ue202turn0news1\\\\ue201\"\n- Group Highlight: \"\\\\ue203Highlight for group.\\\\ue204 \\\\ue200\\\\ue202turn0search0\\\\ue202turn0news1\\\\ue201\"\n- Image: \"See photo \\\\ue202turn0image0.\"\n\n**NEVER use markdown links, [1], or footnotes. CITE ONLY with anchors provided.**\n`.trim(),\n schema: SearchToolSchema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n};\n"],"names":["z","createSearchAPI","createFirecrawlScraper","createReranker","createSourceProcessor","search","expandHighlights","tool","formatResultsForLLM","Constants"],"mappings":";;;;;;;;;;;AAAA;AAWA,MAAM,yBAAyB,GAAG;;;;;;;;;;;;;;;;;;CAkBjC,CAAC,IAAI,EAAE;AAER,MAAM,2BAA2B,GAAG,CAAA;;;;;;;CAOnC,CAAC,IAAI,EAAE;AAER;;;;;;AAMG;MACU,gBAAgB,GAAG,CAC9B,MAA6B,GAAA,EAAE,KACmB;IAClD,MAAM,EACJ,cAAc,GAAG,QAAQ,EACzB,YAAY,EACZ,kBAAkB,EAClB,aAAa,EACb,YAAY,GAAG,QAAQ,EACvB,UAAU,GAAG,CAAC,EACd,UAAU,GAAG,CAAC,eAAe,CAAC,EAC9B,aAAa,GAAG,IAAI,EACpB,eAAe,EACf,eAAe,EACf,gBAAgB,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,EACvC,UAAU,EACV,YAAY,EACZ,eAAe,EAAE,gBAAgB,GAClC,GAAG,MAAM;IAEV,MAAM,WAAW,GAAGA,KAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,yBAAyB,CAAC;AAClE,IAAA,MAAM,YAAY,GAGd;AACF,QAAA,KAAK,EAAE,WAAW;KACnB;AAED,IAAA,IAAI,cAAc,KAAK,QAAQ,EAAE;QAC/B,YAAY,CAAC,OAAO,GAAGA;AACpB,aAAA,MAAM;AACN,aAAA,QAAQ;aACR,QAAQ,CAAC,2BAA2B,CAAC;;IAG1C,MAAM,gBAAgB,GAAGA,KAAC,CA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KAAA,CACF;AACH;;;;"}
@@ -1,4 +1,4 @@
1
- import { handleToolCalls } from './stream.mjs';
1
+ import { handleToolCalls } from './tools/handlers.mjs';
2
2
  import { Providers } from './common/enum.mjs';
3
3
 
4
4
  class HandlerRegistry {
@@ -1 +1 @@
1
- {"version":3,"file":"events.mjs","sources":["../../src/events.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/events.ts\nimport type { UsageMetadata, BaseMessageFields } from '@langchain/core/messages';\nimport type { Graph } from '@/graphs';\nimport type * as t from '@/types';\nimport { handleToolCalls } from '@/stream';\nimport { Providers } from '@/common';\n\nexport class HandlerRegistry {\n private handlers: Map<string, t.EventHandler> = new Map();\n\n register(eventType: string, handler: t.EventHandler): void {\n this.handlers.set(eventType, handler);\n }\n\n getHandler(eventType: string): t.EventHandler | undefined {\n return this.handlers.get(eventType);\n }\n}\n\nexport class ModelEndHandler implements t.EventHandler {\n collectedUsage?: UsageMetadata[];\n constructor(collectedUsage?: UsageMetadata[]) {\n if (collectedUsage && !Array.isArray(collectedUsage)) {\n throw new Error('collectedUsage must be an array');\n }\n this.collectedUsage = collectedUsage;\n }\n\n handle(event: string, data: t.ModelEndData, metadata?: Record<string, unknown>, graph?: Graph): void {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n const usage = data?.output?.usage_metadata;\n if (usage != null && this.collectedUsage != null) {\n this.collectedUsage.push(usage);\n }\n\n if (metadata.ls_provider === 'FakeListChatModel') {\n return handleToolCalls(data?.output?.tool_calls, metadata, graph);\n }\n\n console.log(`====== ${event.toUpperCase()} ======`);\n console.dir({\n usage,\n }, { depth: null });\n\n if (metadata.provider !== Providers.GOOGLE) {\n return;\n }\n\n handleToolCalls(data?.output?.tool_calls, metadata, graph);\n }\n}\n\nexport class ToolEndHandler implements t.EventHandler {\n private callback?: t.ToolEndCallback;\n constructor(callback?: t.ToolEndCallback) {\n this.callback = callback;\n }\n handle(event: string, data: t.StreamEventData | undefined, metadata?: Record<string, unknown>, graph?: Graph): void {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n const toolEndData = data as t.ToolEndData | undefined;\n if (!toolEndData?.output) {\n console.warn('No output found in tool_end event');\n return;\n }\n\n this.callback?.(toolEndData, metadata);\n\n graph.handleToolCallCompleted({ input: toolEndData.input, output: toolEndData.output }, metadata);\n }\n}\n\nexport class TestLLMStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData | undefined): void {\n const chunk = data?.chunk;\n const isMessageChunk = !!(chunk && 'message' in chunk);\n const msg = isMessageChunk ? chunk.message : undefined;\n if (msg && msg.tool_call_chunks && msg.tool_call_chunks.length > 0) {\n console.log(msg.tool_call_chunks);\n } else if (msg && msg.content) {\n if (typeof msg.content === 'string') {\n process.stdout.write(msg.content);\n }\n }\n }\n}\n\nexport class TestChatStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData | undefined): void {\n const chunk = data?.chunk;\n const isContentChunk = !!(chunk && 'content' in chunk);\n const content = isContentChunk && chunk.content;\n\n if (!content || !isContentChunk) {\n return;\n }\n\n if (chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) {\n console.dir(chunk.tool_call_chunks, { depth: null });\n }\n\n if (typeof content === 'string') {\n process.stdout.write(content);\n } else {\n console.dir(content, { depth: null });\n }\n }\n}\n\nexport class LLMStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData | undefined, metadata?: Record<string, unknown>): void {\n const chunk = data?.chunk;\n const isMessageChunk = !!(chunk && 'message' in chunk);\n const msg = isMessageChunk && chunk.message;\n if (metadata) { console.log(metadata); }\n if (msg && msg.tool_call_chunks && msg.tool_call_chunks.length > 0) {\n console.log(msg.tool_call_chunks);\n } else if (msg && msg.content) {\n if (typeof msg.content === 'string') {\n // const text_delta = msg.content;\n // dispatchCustomEvent(GraphEvents.CHAT_MODEL_STREAM, { chunk }, config);\n process.stdout.write(msg.content);\n }\n }\n }\n}\n\nexport const createMetadataAggregator = (_collected?: Record<string, NonNullable<BaseMessageFields['response_metadata']>>[]): t.MetadataAggregatorResult => {\n const collected = _collected || [];\n\n const handleLLMEnd: t.HandleLLMEnd = (output) => {\n const { generations } = output;\n const lastMessageOutput = (generations[generations.length - 1] as (t.StreamGeneration | undefined)[] | undefined)?.[0];\n if (!lastMessageOutput) {\n return;\n }\n const { message } = lastMessageOutput;\n if (message?.response_metadata) {\n collected.push(message.response_metadata);\n }\n };\n\n return { handleLLMEnd, collected };\n};"],"names":[],"mappings":";;;MAQa,eAAe,CAAA;AAClB,IAAA,QAAQ,GAAgC,IAAI,GAAG,EAAE;IAEzD,QAAQ,CAAC,SAAiB,EAAE,OAAuB,EAAA;QACjD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC;;AAGvC,IAAA,UAAU,CAAC,SAAiB,EAAA;QAC1B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC;;AAEtC;MAEY,eAAe,CAAA;AAC1B,IAAA,cAAc;AACd,IAAA,WAAA,CAAY,cAAgC,EAAA;QAC1C,IAAI,cAAc,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;AACpD,YAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;;AAEpD,QAAA,IAAI,CAAC,cAAc,GAAG,cAAc;;AAGtC,IAAA,MAAM,CAAC,KAAa,EAAE,IAAoB,EAAE,QAAkC,EAAE,KAAa,EAAA;AAC3F,QAAA,IAAI,CAAC,KAAK,IAAI,CAAC,QAAQ,EAAE;AACvB,YAAA,OAAO,CAAC,IAAI,CAAC,kCAAkC,KAAK,CAAA,MAAA,CAAQ,CAAC;YAC7D;;AAGF,QAAA,MAAM,KAAK,GAAG,IAAI,EAAE,MAAM,EAAE,cAAc;QAC1C,IAAI,KAAK,IAAI,IAAI,IAAI,IAAI,CAAC,cAAc,IAAI,IAAI,EAAE;AAChD,YAAA,IAAI,CAAC,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AC9B,YAAA,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC;;AAE7C,KAAC;AAED,IAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE;AACpC;;;;"}
1
+ {"version":3,"file":"events.mjs","sources":["../../src/events.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/events.ts\nimport type {\n UsageMetadata,\n BaseMessageFields,\n} from '@langchain/core/messages';\nimport type { Graph } from '@/graphs';\nimport type * as t from '@/types';\nimport { handleToolCalls } from '@/tools/handlers';\nimport { Providers } from '@/common';\n\nexport class HandlerRegistry {\n private handlers: Map<string, t.EventHandler> = new Map();\n\n register(eventType: string, handler: t.EventHandler): void {\n this.handlers.set(eventType, handler);\n }\n\n getHandler(eventType: string): t.EventHandler | undefined {\n return this.handlers.get(eventType);\n }\n}\n\nexport class ModelEndHandler implements t.EventHandler {\n collectedUsage?: UsageMetadata[];\n constructor(collectedUsage?: UsageMetadata[]) {\n if (collectedUsage && !Array.isArray(collectedUsage)) {\n throw new Error('collectedUsage must be an array');\n }\n this.collectedUsage = collectedUsage;\n }\n\n handle(\n event: string,\n data: t.ModelEndData,\n metadata?: Record<string, unknown>,\n graph?: Graph\n ): void {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n const usage = data?.output?.usage_metadata;\n if (usage != null && this.collectedUsage != null) {\n this.collectedUsage.push(usage);\n }\n\n if (metadata.ls_provider === 'FakeListChatModel') {\n return handleToolCalls(data?.output?.tool_calls, metadata, graph);\n }\n\n console.log(`====== ${event.toUpperCase()} ======`);\n console.dir(\n {\n usage,\n },\n { depth: null }\n );\n\n if (metadata.provider !== Providers.GOOGLE) {\n return;\n }\n\n handleToolCalls(data?.output?.tool_calls, metadata, graph);\n }\n}\n\nexport class ToolEndHandler implements t.EventHandler {\n private callback?: t.ToolEndCallback;\n constructor(callback?: t.ToolEndCallback) {\n this.callback = callback;\n }\n handle(\n event: string,\n data: t.StreamEventData | undefined,\n metadata?: Record<string, unknown>,\n graph?: Graph\n ): void {\n if (!graph || !metadata) {\n console.warn(`Graph or metadata not found in ${event} event`);\n return;\n }\n\n const toolEndData = data as t.ToolEndData | undefined;\n if (!toolEndData?.output) {\n console.warn('No output found in tool_end event');\n return;\n }\n\n this.callback?.(toolEndData, metadata);\n\n graph.handleToolCallCompleted(\n { input: toolEndData.input, output: toolEndData.output },\n metadata\n );\n }\n}\n\nexport class TestLLMStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData | undefined): void {\n const chunk = data?.chunk;\n const isMessageChunk = !!(chunk && 'message' in chunk);\n const msg = isMessageChunk ? chunk.message : undefined;\n if (msg && msg.tool_call_chunks && msg.tool_call_chunks.length > 0) {\n console.log(msg.tool_call_chunks);\n } else if (msg && msg.content) {\n if (typeof msg.content === 'string') {\n process.stdout.write(msg.content);\n }\n }\n }\n}\n\nexport class TestChatStreamHandler implements t.EventHandler {\n handle(event: string, data: t.StreamEventData | undefined): void {\n const chunk = data?.chunk;\n const isContentChunk = !!(chunk && 'content' in chunk);\n const content = isContentChunk && chunk.content;\n\n if (!content || !isContentChunk) {\n return;\n }\n\n if (chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) {\n console.dir(chunk.tool_call_chunks, { depth: null });\n }\n\n if (typeof content === 'string') {\n process.stdout.write(content);\n } else {\n console.dir(content, { depth: null });\n }\n }\n}\n\nexport class LLMStreamHandler implements t.EventHandler {\n handle(\n event: string,\n data: t.StreamEventData | undefined,\n metadata?: Record<string, unknown>\n ): void {\n const chunk = data?.chunk;\n const isMessageChunk = !!(chunk && 'message' in chunk);\n const msg = isMessageChunk && chunk.message;\n if (metadata) {\n console.log(metadata);\n }\n if (msg && msg.tool_call_chunks && msg.tool_call_chunks.length > 0) {\n console.log(msg.tool_call_chunks);\n } else if (msg && msg.content) {\n if (typeof msg.content === 'string') {\n // const text_delta = msg.content;\n // dispatchCustomEvent(GraphEvents.CHAT_MODEL_STREAM, { chunk }, config);\n process.stdout.write(msg.content);\n }\n }\n }\n}\n\nexport const createMetadataAggregator = (\n _collected?: Record<\n string,\n NonNullable<BaseMessageFields['response_metadata']>\n >[]\n): t.MetadataAggregatorResult => {\n const collected = _collected || [];\n\n const handleLLMEnd: t.HandleLLMEnd = (output) => {\n const { generations } = output;\n const lastMessageOutput = (\n generations[generations.length - 1] as\n | (t.StreamGeneration | undefined)[]\n | undefined\n )?.[0];\n if (!lastMessageOutput) {\n return;\n }\n const { message } = lastMessageOutput;\n if (message?.response_metadata) {\n collected.push(message.response_metadata);\n }\n };\n\n return { handleLLMEnd, collected };\n};\n"],"names":[],"mappings":";;;MAWa,eAAe,CAAA;AAClB,IAAA,QAAQ,GAAgC,IAAI,GAAG,EAAE;IAEzD,QAAQ,CAAC,SAAiB,EAAE,OAAuB,EAAA;QACjD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC;;AAGvC,IAAA,UAAU,CAAC,SAAiB,EAAA;QAC1B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC;;AAEtC;MAEY,eAAe,CAAA;AAC1B,IAAA,cAAc;AACd,IAAA,WAAA,CAAY,cAAgC,EAAA;QAC1C,IAAI,cAAc,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;AACpD,YAAA,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC;;AAEpD,QAAA,IAAI,CAAC,cAAc,GAAG,cAAc;;AAGtC,IAAA,MAAM,CACJ,KAAa,EACb,IAAoB,EACpB,QAAkC,EAClC,KAAa,EAAA;AAEb,QAAA,IAAI,CAAC,KAAK,IAAI,CAAC,QAAQ,EAAE;AACvB,YAAA,OAAO,CAAC,IAAI,CAAC,kCAAkC,KAAK,CAAA,MAAA,CAAQ,CAAC;YAC7D;;AAGF,QAAA,MAAM,KAAK,GAAG,IAAI,EAAE,MAAM,EAAE,cAAc;QAC1C,IAAI,KAAK,IAAI,IAAI,IAAI,I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B;AACrC,QAAA,IAAI,OAAO,EAAE,iBAAiB,EAAE;AAC9B,YAAA,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC;;AAE7C,KAAC;AAED,IAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE;AACpC;;;;"}
package/dist/esm/main.mjs CHANGED
@@ -1,12 +1,14 @@
1
1
  export { Run } from './run.mjs';
2
- export { ChatModelStreamHandler, createContentAggregator, getMessageId, handleToolCalls } from './stream.mjs';
2
+ export { ChatModelStreamHandler, createContentAggregator } from './stream.mjs';
3
3
  export { SEPARATORS, SplitStreamHandler } from './splitStream.mjs';
4
4
  export { HandlerRegistry, LLMStreamHandler, ModelEndHandler, TestChatStreamHandler, TestLLMStreamHandler, ToolEndHandler, createMetadataAggregator } from './events.mjs';
5
5
  export { convertMessagesToContent, findLastIndex, formatAnthropicArtifactContent, formatAnthropicMessage, formatArtifactPayload, getConverseOverrideMessage, modifyDeltaProperties } from './messages/core.mjs';
6
+ export { getMessageId } from './messages/ids.mjs';
6
7
  export { calculateTotalTokens, checkValidNumber, createPruneMessages, getMessagesWithinTokenLimit } from './messages/prune.mjs';
7
8
  export { formatAgentMessages, formatContentStrings, formatFromLangChain, formatLangChainMessages, formatMessage, formatVisionMessage, shiftIndexTokenCountMap } from './messages/format.mjs';
8
9
  export { Graph, StandardGraph } from './graphs/Graph.mjs';
9
10
  export { createCodeExecutionTool, getCodeBaseURL, imageExtRegex } from './tools/CodeExecutor.mjs';
11
+ export { handleToolCallChunks, handleToolCalls } from './tools/handlers.mjs';
10
12
  export { createSearchTool } from './tools/search/tool.mjs';
11
13
  export { Callback, CommonEvents, Constants, ContentTypes, EnvVar, GraphEvents, GraphNodeActions, GraphNodeKeys, Providers, StepTypes, ToolCallTypes } from './common/enum.mjs';
12
14
  export { joinKeys, resetIfNotEmpty } from './utils/graph.mjs';
@@ -1 +1 @@
1
- {"version":3,"file":"main.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"main.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,21 @@
1
+ import { nanoid } from 'nanoid';
2
+
3
+ // src/stream.ts
4
+ const getMessageId = (stepKey, graph, returnExistingId = false) => {
5
+ const messageId = graph.messageIdsByStepKey.get(stepKey);
6
+ if (messageId != null && messageId) {
7
+ return returnExistingId ? messageId : undefined;
8
+ }
9
+ const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);
10
+ if (prelimMessageId != null && prelimMessageId) {
11
+ graph.prelimMessageIdsByStepKey.delete(stepKey);
12
+ graph.messageIdsByStepKey.set(stepKey, prelimMessageId);
13
+ return prelimMessageId;
14
+ }
15
+ const message_id = `msg_${nanoid()}`;
16
+ graph.messageIdsByStepKey.set(stepKey, message_id);
17
+ return message_id;
18
+ };
19
+
20
+ export { getMessageId };
21
+ //# sourceMappingURL=ids.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ids.mjs","sources":["../../../src/messages/ids.ts"],"sourcesContent":["// src/stream.ts\nimport { nanoid } from 'nanoid';\nimport type { Graph } from '@/graphs';\nimport type * as t from '@/types';\n\nexport const getMessageId = (\n stepKey: string,\n graph: Graph<t.BaseGraphState>,\n returnExistingId = false\n): string | undefined => {\n const messageId = graph.messageIdsByStepKey.get(stepKey);\n if (messageId != null && messageId) {\n return returnExistingId ? messageId : undefined;\n }\n\n const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);\n if (prelimMessageId != null && prelimMessageId) {\n graph.prelimMessageIdsByStepKey.delete(stepKey);\n graph.messageIdsByStepKey.set(stepKey, prelimMessageId);\n return prelimMessageId;\n }\n\n const message_id = `msg_${nanoid()}`;\n graph.messageIdsByStepKey.set(stepKey, message_id);\n return message_id;\n};\n"],"names":[],"mappings":";;AAAA;AAKO,MAAM,YAAY,GAAG,CAC1B,OAAe,EACf,KAA8B,EAC9B,gBAAgB,GAAG,KAAK,KACF;IACtB,MAAM,SAAS,GAAG,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC;AACxD,IAAA,IAAI,SAAS,IAAI,IAAI,IAAI,SAAS,EAAE;QAClC,OAAO,gBAAgB,GAAG,SAAS,GAAG,SAAS;;IAGjD,MAAM,eAAe,GAAG,KAAK,CAAC,yBAAyB,CAAC,GAAG,CAAC,OAAO,CAAC;AACpE,IAAA,IAAI,eAAe,IAAI,IAAI,IAAI,eAAe,EAAE;AAC9C,QAAA,KAAK,CAAC,yBAAyB,CAAC,MAAM,CAAC,OAAO,CAAC;QAC/C,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,eAAe,CAAC;AACvD,QAAA,OAAO,eAAe;;AAGxB,IAAA,MAAM,UAAU,GAAG,CAAA,IAAA,EAAO,MAAM,EAAE,EAAE;IACpC,KAAK,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,UAAU,CAAC;AAClD,IAAA,OAAO,UAAU;AACnB;;;;"}
@@ -1,7 +1,9 @@
1
- import { nanoid } from 'nanoid';
2
- import { StepTypes, ContentTypes, ToolCallTypes, GraphEvents } from './common/enum.mjs';
1
+ import { StepTypes, ContentTypes, GraphEvents, ToolCallTypes } from './common/enum.mjs';
2
+ import { handleToolCalls, handleToolCallChunks } from './tools/handlers.mjs';
3
+ import './messages/core.mjs';
4
+ import { getMessageId } from './messages/ids.mjs';
5
+ import '@langchain/core/messages';
3
6
 
4
- // src/stream.ts
5
7
  /**
6
8
  * Parses content to extract thinking sections enclosed in <think> tags using string operations
7
9
  * @param content The content to parse
@@ -49,85 +51,6 @@ function getNonEmptyValue(possibleValues) {
49
51
  }
50
52
  return undefined;
51
53
  }
52
- const getMessageId = (stepKey, graph, returnExistingId = false) => {
53
- const messageId = graph.messageIdsByStepKey.get(stepKey);
54
- if (messageId != null && messageId) {
55
- return returnExistingId ? messageId : undefined;
56
- }
57
- const prelimMessageId = graph.prelimMessageIdsByStepKey.get(stepKey);
58
- if (prelimMessageId != null && prelimMessageId) {
59
- graph.prelimMessageIdsByStepKey.delete(stepKey);
60
- graph.messageIdsByStepKey.set(stepKey, prelimMessageId);
61
- return prelimMessageId;
62
- }
63
- const message_id = `msg_${nanoid()}`;
64
- graph.messageIdsByStepKey.set(stepKey, message_id);
65
- return message_id;
66
- };
67
- const handleToolCalls = (toolCalls, metadata, graph) => {
68
- if (!graph || !metadata) {
69
- console.warn(`Graph or metadata not found in ${event} event`);
70
- return;
71
- }
72
- if (!toolCalls) {
73
- return;
74
- }
75
- if (toolCalls.length === 0) {
76
- return;
77
- }
78
- const stepKey = graph.getStepKey(metadata);
79
- for (const tool_call of toolCalls) {
80
- const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
81
- tool_call.id = toolCallId;
82
- if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
83
- continue;
84
- }
85
- let prevStepId = '';
86
- let prevRunStep;
87
- try {
88
- prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
89
- prevRunStep = graph.getRunStep(prevStepId);
90
- }
91
- catch {
92
- // no previous step
93
- }
94
- const dispatchToolCallIds = (lastMessageStepId) => {
95
- graph.dispatchMessageDelta(lastMessageStepId, {
96
- content: [
97
- {
98
- type: 'text',
99
- text: '',
100
- tool_call_ids: [toolCallId],
101
- },
102
- ],
103
- });
104
- };
105
- /* If the previous step exists and is a message creation */
106
- if (prevStepId &&
107
- prevRunStep &&
108
- prevRunStep.type === StepTypes.MESSAGE_CREATION) {
109
- dispatchToolCallIds(prevStepId);
110
- graph.messageStepHasToolCalls.set(prevStepId, true);
111
- /* If the previous step doesn't exist or is not a message creation */
112
- }
113
- else if (!prevRunStep ||
114
- prevRunStep.type !== StepTypes.MESSAGE_CREATION) {
115
- const messageId = getMessageId(stepKey, graph, true) ?? '';
116
- const stepId = graph.dispatchRunStep(stepKey, {
117
- type: StepTypes.MESSAGE_CREATION,
118
- message_creation: {
119
- message_id: messageId,
120
- },
121
- });
122
- dispatchToolCallIds(stepId);
123
- graph.messageStepHasToolCalls.set(prevStepId, true);
124
- }
125
- graph.dispatchRunStep(stepKey, {
126
- type: StepTypes.TOOL_CALLS,
127
- tool_calls: [tool_call],
128
- });
129
- }
130
- };
131
54
  class ChatModelStreamHandler {
132
55
  handle(event, data, metadata, graph) {
133
56
  if (!graph) {
@@ -176,7 +99,7 @@ class ChatModelStreamHandler {
176
99
  chunk.tool_call_chunks &&
177
100
  chunk.tool_call_chunks.length &&
178
101
  typeof chunk.tool_call_chunks[0]?.index === 'number') {
179
- this.handleToolCallChunks({
102
+ handleToolCallChunks({
180
103
  graph,
181
104
  stepKey,
182
105
  toolCallChunks: chunk.tool_call_chunks,
@@ -295,74 +218,6 @@ hasToolCallChunks: ${hasToolCallChunks}
295
218
  });
296
219
  }
297
220
  }
298
- handleToolCallChunks = ({ graph, stepKey, toolCallChunks, }) => {
299
- let prevStepId;
300
- let prevRunStep;
301
- try {
302
- prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
303
- prevRunStep = graph.getRunStep(prevStepId);
304
- }
305
- catch {
306
- /** Edge Case: If no previous step exists, create a new message creation step */
307
- const message_id = getMessageId(stepKey, graph, true) ?? '';
308
- prevStepId = graph.dispatchRunStep(stepKey, {
309
- type: StepTypes.MESSAGE_CREATION,
310
- message_creation: {
311
- message_id,
312
- },
313
- });
314
- prevRunStep = graph.getRunStep(prevStepId);
315
- }
316
- const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
317
- /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
318
- const tool_calls = prevStepId &&
319
- prevRunStep &&
320
- prevRunStep.type === StepTypes.MESSAGE_CREATION
321
- ? []
322
- : undefined;
323
- /** Edge Case: `id` and `name` fields cannot be empty strings */
324
- for (const toolCallChunk of toolCallChunks) {
325
- if (toolCallChunk.name === '') {
326
- toolCallChunk.name = undefined;
327
- }
328
- if (toolCallChunk.id === '') {
329
- toolCallChunk.id = undefined;
330
- }
331
- else if (tool_calls != null &&
332
- toolCallChunk.id != null &&
333
- toolCallChunk.name != null) {
334
- tool_calls.push({
335
- args: {},
336
- id: toolCallChunk.id,
337
- name: toolCallChunk.name,
338
- type: ToolCallTypes.TOOL_CALL,
339
- });
340
- }
341
- }
342
- let stepId = _stepId;
343
- const alreadyDispatched = prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
344
- graph.messageStepHasToolCalls.has(prevStepId);
345
- if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
346
- graph.dispatchMessageDelta(prevStepId, {
347
- content: [
348
- {
349
- type: ContentTypes.TEXT,
350
- text: '',
351
- tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
352
- },
353
- ],
354
- });
355
- graph.messageStepHasToolCalls.set(prevStepId, true);
356
- stepId = graph.dispatchRunStep(stepKey, {
357
- type: StepTypes.TOOL_CALLS,
358
- tool_calls,
359
- });
360
- }
361
- graph.dispatchRunStepDelta(stepId, {
362
- type: StepTypes.TOOL_CALLS,
363
- tool_calls: toolCallChunks,
364
- });
365
- };
366
221
  handleReasoning(chunk, graph) {
367
222
  let reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey];
368
223
  if (Array.isArray(chunk.content) &&
@@ -599,5 +454,5 @@ function createContentAggregator() {
599
454
  return { contentParts, aggregateContent, stepMap };
600
455
  }
601
456
 
602
- export { ChatModelStreamHandler, createContentAggregator, getMessageId, handleToolCalls };
457
+ export { ChatModelStreamHandler, createContentAggregator };
603
458
  //# sourceMappingURL=stream.mjs.map