@illuma-ai/agents 1.4.0-alpha.1 → 1.4.0-alpha.3

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 (53) hide show
  1. package/dist/cjs/main.cjs +44 -22
  2. package/dist/cjs/main.cjs.map +1 -1
  3. package/dist/cjs/tools/artifacts/schema.cjs +63 -0
  4. package/dist/cjs/tools/artifacts/schema.cjs.map +1 -0
  5. package/dist/cjs/tools/artifacts/tool.cjs +213 -0
  6. package/dist/cjs/tools/artifacts/tool.cjs.map +1 -0
  7. package/dist/cjs/tools/fileSearch/formatter.cjs +93 -0
  8. package/dist/cjs/tools/fileSearch/formatter.cjs.map +1 -0
  9. package/dist/cjs/tools/fileSearch/ragClient.cjs +102 -0
  10. package/dist/cjs/tools/fileSearch/ragClient.cjs.map +1 -0
  11. package/dist/cjs/tools/fileSearch/schema.cjs +18 -0
  12. package/dist/cjs/tools/fileSearch/schema.cjs.map +1 -0
  13. package/dist/cjs/tools/fileSearch/tool.cjs +155 -0
  14. package/dist/cjs/tools/fileSearch/tool.cjs.map +1 -0
  15. package/dist/esm/main.mjs +6 -0
  16. package/dist/esm/main.mjs.map +1 -1
  17. package/dist/esm/tools/artifacts/schema.mjs +56 -0
  18. package/dist/esm/tools/artifacts/schema.mjs.map +1 -0
  19. package/dist/esm/tools/artifacts/tool.mjs +207 -0
  20. package/dist/esm/tools/artifacts/tool.mjs.map +1 -0
  21. package/dist/esm/tools/fileSearch/formatter.mjs +90 -0
  22. package/dist/esm/tools/fileSearch/formatter.mjs.map +1 -0
  23. package/dist/esm/tools/fileSearch/ragClient.mjs +98 -0
  24. package/dist/esm/tools/fileSearch/ragClient.mjs.map +1 -0
  25. package/dist/esm/tools/fileSearch/schema.mjs +15 -0
  26. package/dist/esm/tools/fileSearch/schema.mjs.map +1 -0
  27. package/dist/esm/tools/fileSearch/tool.mjs +152 -0
  28. package/dist/esm/tools/fileSearch/tool.mjs.map +1 -0
  29. package/dist/types/index.d.ts +2 -0
  30. package/dist/types/tools/artifacts/index.d.ts +3 -0
  31. package/dist/types/tools/artifacts/schema.d.ts +63 -0
  32. package/dist/types/tools/artifacts/tool.d.ts +16 -0
  33. package/dist/types/tools/artifacts/types.d.ts +127 -0
  34. package/dist/types/tools/fileSearch/formatter.d.ts +25 -0
  35. package/dist/types/tools/fileSearch/index.d.ts +5 -0
  36. package/dist/types/tools/fileSearch/ragClient.d.ts +32 -0
  37. package/dist/types/tools/fileSearch/schema.d.ts +13 -0
  38. package/dist/types/tools/fileSearch/tool.d.ts +18 -0
  39. package/dist/types/tools/fileSearch/types.d.ts +139 -0
  40. package/package.json +1 -1
  41. package/src/index.ts +2 -0
  42. package/src/tools/artifacts/__tests__/tool.test.ts +243 -0
  43. package/src/tools/artifacts/index.ts +33 -0
  44. package/src/tools/artifacts/schema.ts +76 -0
  45. package/src/tools/artifacts/tool.ts +277 -0
  46. package/src/tools/artifacts/types.ts +149 -0
  47. package/src/tools/fileSearch/__tests__/tool.test.ts +261 -0
  48. package/src/tools/fileSearch/formatter.ts +129 -0
  49. package/src/tools/fileSearch/index.ts +23 -0
  50. package/src/tools/fileSearch/ragClient.ts +137 -0
  51. package/src/tools/fileSearch/schema.ts +19 -0
  52. package/src/tools/fileSearch/tool.ts +207 -0
  53. package/src/tools/fileSearch/types.ts +149 -0
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Default result formatters.
3
+ *
4
+ * - `plainTextFormatter`: CLI / A2A / generic output. No citation anchors.
5
+ * - `citationAnchorFormatter`: ranger-style `\ue202turn0fileN` anchors with
6
+ * a monotonic `sourceOffset` so multi-call turns stay globally unique.
7
+ *
8
+ * Runtimes can supply their own `FileSearchResultFormatter` to override.
9
+ */
10
+ const plainTextFormatter = {
11
+ format(chunks, { files: _files }) {
12
+ if (chunks.length === 0) {
13
+ return { message: 'No relevant results found in the available files.' };
14
+ }
15
+ const body = chunks
16
+ .map((c) => {
17
+ const page = getPage(c);
18
+ const rel = (1 - c.distance).toFixed(4);
19
+ return (`File: ${c.filename}` +
20
+ (page != null ? `\nPage: ${page}` : '') +
21
+ `\nRelevance: ${rel}\nContent: ${c.page_content}\n`);
22
+ })
23
+ .join('\n---\n');
24
+ const sources = chunks.map((c) => ({
25
+ type: 'file',
26
+ fileId: c.file_id,
27
+ content: c.page_content,
28
+ fileName: c.filename,
29
+ relevance: 1 - c.distance,
30
+ pages: getPage(c) != null ? [getPage(c)] : [],
31
+ }));
32
+ return { message: body, artifact: { file_search: { sources } } };
33
+ },
34
+ };
35
+ function createCitationAnchorFormatter(opts = {}) {
36
+ const toolName = opts.toolName ?? 'file_search';
37
+ const getOffset = opts.getSourceOffset ?? (() => 0);
38
+ const advance = opts.advanceSourceOffset ?? ((_by) => { });
39
+ return {
40
+ format(chunks) {
41
+ if (chunks.length === 0) {
42
+ return {
43
+ message: 'No results found or errors occurred while searching the files.',
44
+ };
45
+ }
46
+ const base = getOffset();
47
+ const body = chunks
48
+ .map((c, i) => {
49
+ const globalIndex = base + i;
50
+ const page = getPage(c);
51
+ const rel = (1 - c.distance).toFixed(4);
52
+ return (`[Source ${globalIndex}] File: ${c.filename} | Anchor: \\ue202turn0file${globalIndex}` +
53
+ (page != null ? ` | Page: ${page}` : '') +
54
+ ` | Relevance: ${rel}\nContent: ${c.page_content}\n` +
55
+ `↑ Cite this source using: \\ue202turn0file${globalIndex}`);
56
+ })
57
+ .join('\n---\n');
58
+ const sources = chunks.map((c) => ({
59
+ type: 'file',
60
+ fileId: c.file_id,
61
+ content: c.page_content,
62
+ fileName: c.filename,
63
+ relevance: 1 - c.distance,
64
+ pages: getPage(c) != null ? [getPage(c)] : [],
65
+ pageRelevance: getPage(c) != null ? { [getPage(c)]: 1 - c.distance } : {},
66
+ }));
67
+ advance(chunks.length);
68
+ return {
69
+ message: body,
70
+ artifact: { [toolName]: { sources, fileCitations: true } },
71
+ };
72
+ },
73
+ };
74
+ }
75
+ /** Extract a 1-indexed page number from the chunk metadata, or null. */
76
+ function getPage(chunk) {
77
+ const raw = chunk.metadata?.page ??
78
+ chunk.metadata?.page_number ??
79
+ null;
80
+ if (raw == null)
81
+ return null;
82
+ const parsed = typeof raw === 'number' ? raw : parseInt(String(raw), 10);
83
+ if (Number.isNaN(parsed) || parsed < 0)
84
+ return null;
85
+ // rag_api stores 0-indexed; display is 1-indexed
86
+ return parsed + 1;
87
+ }
88
+
89
+ export { createCitationAnchorFormatter, plainTextFormatter };
90
+ //# sourceMappingURL=formatter.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatter.mjs","sources":["../../../../src/tools/fileSearch/formatter.ts"],"sourcesContent":["/**\n * Default result formatters.\n *\n * - `plainTextFormatter`: CLI / A2A / generic output. No citation anchors.\n * - `citationAnchorFormatter`: ranger-style `\\ue202turn0fileN` anchors with\n * a monotonic `sourceOffset` so multi-call turns stay globally unique.\n *\n * Runtimes can supply their own `FileSearchResultFormatter` to override.\n */\n\nimport type {\n FileSearchResultFormatter,\n FileSearchFile,\n RagChunk,\n} from './types';\n\ntype AnnotatedChunk = RagChunk & {\n filename: string;\n isCurrentMessage: boolean;\n};\n\nexport const plainTextFormatter: FileSearchResultFormatter = {\n format(chunks, { files: _files }) {\n if (chunks.length === 0) {\n return { message: 'No relevant results found in the available files.' };\n }\n const body = chunks\n .map((c) => {\n const page = getPage(c);\n const rel = (1 - c.distance).toFixed(4);\n return (\n `File: ${c.filename}` +\n (page != null ? `\\nPage: ${page}` : '') +\n `\\nRelevance: ${rel}\\nContent: ${c.page_content}\\n`\n );\n })\n .join('\\n---\\n');\n\n const sources = chunks.map((c) => ({\n type: 'file' as const,\n fileId: c.file_id,\n content: c.page_content,\n fileName: c.filename,\n relevance: 1 - c.distance,\n pages: getPage(c) != null ? [getPage(c) as number] : [],\n }));\n\n return { message: body, artifact: { file_search: { sources } } };\n },\n};\n\nexport interface CitationAnchorFormatterOptions {\n /** Tool name used in the `file_search` artifact wrapper. Defaults to `'file_search'`. */\n toolName?: string;\n /**\n * Monotonic counter for source indices within a turn. Pass the SAME\n * function to the formatter across multiple calls in the same turn so\n * anchors stay globally unique.\n */\n getSourceOffset?: () => number;\n /** Called after formatting to advance the offset. */\n advanceSourceOffset?: (by: number) => void;\n}\n\nexport function createCitationAnchorFormatter(\n opts: CitationAnchorFormatterOptions = {}\n): FileSearchResultFormatter {\n const toolName = opts.toolName ?? 'file_search';\n const getOffset = opts.getSourceOffset ?? ((): number => 0);\n const advance = opts.advanceSourceOffset ?? ((_by: number): void => {});\n\n return {\n format(chunks): { message: string; artifact?: unknown } {\n if (chunks.length === 0) {\n return {\n message:\n 'No results found or errors occurred while searching the files.',\n };\n }\n const base = getOffset();\n const body = chunks\n .map((c, i) => {\n const globalIndex = base + i;\n const page = getPage(c);\n const rel = (1 - c.distance).toFixed(4);\n return (\n `[Source ${globalIndex}] File: ${c.filename} | Anchor: \\\\ue202turn0file${globalIndex}` +\n (page != null ? ` | Page: ${page}` : '') +\n ` | Relevance: ${rel}\\nContent: ${c.page_content}\\n` +\n `↑ Cite this source using: \\\\ue202turn0file${globalIndex}`\n );\n })\n .join('\\n---\\n');\n\n const sources = chunks.map((c) => ({\n type: 'file' as const,\n fileId: c.file_id,\n content: c.page_content,\n fileName: c.filename,\n relevance: 1 - c.distance,\n pages: getPage(c) != null ? [getPage(c) as number] : [],\n pageRelevance:\n getPage(c) != null ? { [getPage(c) as number]: 1 - c.distance } : {},\n }));\n\n advance(chunks.length);\n return {\n message: body,\n artifact: { [toolName]: { sources, fileCitations: true } },\n };\n },\n };\n}\n\n/** Extract a 1-indexed page number from the chunk metadata, or null. */\nfunction getPage(chunk: AnnotatedChunk | RagChunk): number | null {\n const raw =\n (chunk.metadata?.page as unknown) ??\n (chunk.metadata?.page_number as unknown) ??\n null;\n if (raw == null) return null;\n const parsed = typeof raw === 'number' ? raw : parseInt(String(raw), 10);\n if (Number.isNaN(parsed) || parsed < 0) return null;\n // rag_api stores 0-indexed; display is 1-indexed\n return parsed + 1;\n}\n\n// Re-export so consumers only import from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,OAAO,MAAM,GAAG,CAAC;AACnB;;;;"}
@@ -0,0 +1,98 @@
1
+ import fetch from 'node-fetch';
2
+ import { getEnvironmentVariable } from '@langchain/core/utils/env';
3
+
4
+ /**
5
+ * Default HTTP RAG client. Posts to `${baseUrl}/query` with the shape
6
+ * rag_api expects (`{ file_id, query, k, entity_id? }`). Runtimes that
7
+ * use a different vector backend implement their own `RagClient`.
8
+ *
9
+ * Auth is runtime-provided per call (via `authHeaders` on the params) so
10
+ * short-lived tokens can be minted per request without the client
11
+ * caching stale credentials.
12
+ */
13
+ const RAG_API_URL_ENV = 'RAG_API_URL';
14
+ /** Resolve base URL at call time so env-var changes propagate. */
15
+ function getRagBaseUrl(override) {
16
+ const url = override ?? getEnvironmentVariable(RAG_API_URL_ENV) ?? '';
17
+ if (!url) {
18
+ throw new Error(`file_search: ${RAG_API_URL_ENV} is not configured. ` +
19
+ `Set the env var or pass baseUrl to HttpRagClient.`);
20
+ }
21
+ return url.replace(/\/$/, '');
22
+ }
23
+ class HttpRagClient {
24
+ baseUrlOverride;
25
+ defaultHeaders;
26
+ defaultTimeoutMs;
27
+ logger;
28
+ constructor(opts = {}) {
29
+ this.baseUrlOverride = opts.baseUrl;
30
+ this.defaultHeaders = opts.defaultHeaders ?? {};
31
+ this.defaultTimeoutMs = opts.defaultTimeoutMs ?? 15_000;
32
+ this.logger = opts.logger;
33
+ }
34
+ async query(params) {
35
+ const baseUrl = getRagBaseUrl(this.baseUrlOverride);
36
+ const url = `${baseUrl}/query`;
37
+ const body = {
38
+ file_id: params.file_id,
39
+ query: params.query,
40
+ k: params.k ?? 10,
41
+ };
42
+ if (params.entity_id)
43
+ body.entity_id = params.entity_id;
44
+ if (params.scope)
45
+ body.scope = params.scope;
46
+ const headers = {
47
+ 'Content-Type': 'application/json',
48
+ ...this.defaultHeaders,
49
+ ...(params.authHeaders ?? {}),
50
+ };
51
+ const timeoutMs = params.timeoutMs ?? this.defaultTimeoutMs;
52
+ const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
53
+ const timer = controller
54
+ ? setTimeout(() => controller.abort(), timeoutMs)
55
+ : null;
56
+ this.logger?.debug('[file_search] RAG query', {
57
+ url,
58
+ file_id: params.file_id,
59
+ k: body.k,
60
+ });
61
+ try {
62
+ const res = await fetch(url, {
63
+ method: 'POST',
64
+ headers,
65
+ body: JSON.stringify(body),
66
+ signal: controller?.signal,
67
+ });
68
+ if (!res.ok) {
69
+ const text = await res.text().catch(() => '');
70
+ throw new Error(`RAG query failed: ${res.status} ${res.statusText} — ${text.slice(0, 200)}`);
71
+ }
72
+ const json = (await res.json());
73
+ return this.normalize(params.file_id, json);
74
+ }
75
+ finally {
76
+ if (timer)
77
+ clearTimeout(timer);
78
+ }
79
+ }
80
+ /** Convert rag_api's tuple format into the library's normalized shape. */
81
+ normalize(file_id, resp) {
82
+ if (!Array.isArray(resp)) {
83
+ this.logger?.warn('[file_search] RAG response not an array', { resp });
84
+ return [];
85
+ }
86
+ return resp
87
+ .filter((row) => Array.isArray(row) && row.length === 2)
88
+ .map(([doc, distance]) => ({
89
+ file_id: doc.metadata?.file_id ?? file_id,
90
+ page_content: doc.page_content ?? '',
91
+ distance: typeof distance === 'number' ? distance : 1,
92
+ metadata: doc.metadata,
93
+ }));
94
+ }
95
+ }
96
+
97
+ export { HttpRagClient, RAG_API_URL_ENV, getRagBaseUrl };
98
+ //# sourceMappingURL=ragClient.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ragClient.mjs","sources":["../../../../src/tools/fileSearch/ragClient.ts"],"sourcesContent":["/**\n * Default HTTP RAG client. Posts to `${baseUrl}/query` with the shape\n * rag_api expects (`{ file_id, query, k, entity_id? }`). Runtimes that\n * use a different vector backend implement their own `RagClient`.\n *\n * Auth is runtime-provided per call (via `authHeaders` on the params) so\n * short-lived tokens can be minted per request without the client\n * caching stale credentials.\n */\n\nimport fetch from 'node-fetch';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport type {\n RagClient,\n RagQueryParams,\n RagChunk,\n FileSearchToolLogger,\n} from './types';\n\nexport const RAG_API_URL_ENV = 'RAG_API_URL';\n\n/** Resolve base URL at call time so env-var changes propagate. */\nexport function getRagBaseUrl(override?: string): string {\n const url = override ?? getEnvironmentVariable(RAG_API_URL_ENV) ?? '';\n if (!url) {\n throw new Error(\n `file_search: ${RAG_API_URL_ENV} is not configured. ` +\n `Set the env var or pass baseUrl to HttpRagClient.`\n );\n }\n return url.replace(/\\/$/, '');\n}\n\nexport interface HttpRagClientOptions {\n /** Base URL of the RAG service (no trailing slash). Falls back to env. */\n baseUrl?: string;\n /** Default headers sent on every request (e.g., a static API key). */\n defaultHeaders?: Record<string, string>;\n /** Default timeout if params don't override. Default 15_000. */\n defaultTimeoutMs?: number;\n logger?: FileSearchToolLogger;\n}\n\n/**\n * Expected rag_api response shape: `[[{ page_content, metadata }, distance], ...]`\n * — an array of [doc, score] tuples. Normalized here into `RagChunk[]`.\n */\ntype RagApiResponse = Array<\n [\n {\n page_content: string;\n metadata?: Record<string, unknown>;\n },\n number,\n ]\n>;\n\nexport class HttpRagClient implements RagClient {\n private readonly baseUrlOverride?: string;\n private readonly defaultHeaders: Record<string, string>;\n private readonly defaultTimeoutMs: number;\n private readonly logger?: FileSearchToolLogger;\n\n constructor(opts: HttpRagClientOptions = {}) {\n this.baseUrlOverride = opts.baseUrl;\n this.defaultHeaders = opts.defaultHeaders ?? {};\n this.defaultTimeoutMs = opts.defaultTimeoutMs ?? 15_000;\n this.logger = opts.logger;\n }\n\n async query(params: RagQueryParams): Promise<RagChunk[]> {\n const baseUrl = getRagBaseUrl(this.baseUrlOverride);\n const url = `${baseUrl}/query`;\n\n const body: Record<string, unknown> = {\n file_id: params.file_id,\n query: params.query,\n k: params.k ?? 10,\n };\n if (params.entity_id) body.entity_id = params.entity_id;\n if (params.scope) body.scope = params.scope;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n ...this.defaultHeaders,\n ...(params.authHeaders ?? {}),\n };\n\n const timeoutMs = params.timeoutMs ?? this.defaultTimeoutMs;\n const controller =\n typeof AbortController !== 'undefined' ? new AbortController() : null;\n const timer = controller\n ? setTimeout(() => controller.abort(), timeoutMs)\n : null;\n\n this.logger?.debug('[file_search] RAG query', {\n url,\n file_id: params.file_id,\n k: body.k,\n });\n\n try {\n const res = await fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: controller?.signal as unknown as undefined,\n });\n if (!res.ok) {\n const text = await res.text().catch(() => '');\n throw new Error(\n `RAG query failed: ${res.status} ${res.statusText} — ${text.slice(0, 200)}`\n );\n }\n const json = (await res.json()) as RagApiResponse;\n return this.normalize(params.file_id, json);\n } finally {\n if (timer) clearTimeout(timer);\n }\n }\n\n /** Convert rag_api's tuple format into the library's normalized shape. */\n private normalize(file_id: string, resp: RagApiResponse): RagChunk[] {\n if (!Array.isArray(resp)) {\n this.logger?.warn('[file_search] RAG response not an array', { resp });\n return [];\n }\n return resp\n .filter((row) => Array.isArray(row) && row.length === 2)\n .map(([doc, distance]) => ({\n file_id: (doc.metadata?.file_id as string | undefined) ?? file_id,\n page_content: doc.page_content ?? '',\n distance: typeof distance === 'number' ? distance : 1,\n metadata: doc.metadata,\n }));\n }\n}\n"],"names":[],"mappings":";;;AAAA;;;;;;;;AAQG;AAWI,MAAM,eAAe,GAAG;AAE/B;AACM,SAAU,aAAa,CAAC,QAAiB,EAAA;IAC7C,MAAM,GAAG,GAAG,QAAQ,IAAI,sBAAsB,CAAC,eAAe,CAAC,IAAI,EAAE;IACrE,IAAI,CAAC,GAAG,EAAE;AACR,QAAA,MAAM,IAAI,KAAK,CACb,CAAA,aAAA,EAAgB,eAAe,CAAA,oBAAA,CAAsB;AACnD,YAAA,CAAA,iDAAA,CAAmD,CACtD;IACH;IACA,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;AAC/B;MA0Ba,aAAa,CAAA;AACP,IAAA,eAAe;AACf,IAAA,cAAc;AACd,IAAA,gBAAgB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,GAAG,CAAC,MAAM,KAAK,CAAC;aACtD,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM;AACzB,YAAA,OAAO,EAAG,GAAG,CAAC,QAAQ,EAAE,OAA8B,IAAI,OAAO;AACjE,YAAA,YAAY,EAAE,GAAG,CAAC,YAAY,IAAI,EAAE;AACpC,YAAA,QAAQ,EAAE,OAAO,QAAQ,KAAK,QAAQ,GAAG,QAAQ,GAAG,CAAC;YACrD,QAAQ,EAAE,GAAG,CAAC,QAAQ;AACvB,SAAA,CAAC,CAAC;IACP;AACD;;;;"}
@@ -0,0 +1,15 @@
1
+ import { z } from 'zod';
2
+
3
+ const fileSearchInputSchema = z.object({
4
+ query: z
5
+ .string()
6
+ .describe("A natural language query to search for relevant information in the files. Be SPECIFIC and TARGETED — use keywords for the specific section or topic you need. For comprehensive tasks (summaries, overviews), call this tool multiple times with different targeted queries (e.g., 'introduction', 'methodology', 'results', 'conclusions') rather than one broad query."),
7
+ target_files: z
8
+ .array(z.string())
9
+ .optional()
10
+ .describe('Optional list of filenames (or partial names) to limit the search to. When provided, only files whose name contains one of these strings will be searched. Use this to avoid searching irrelevant files. Omit to search all available files.'),
11
+ });
12
+ const FileSearchToolName = 'file_search';
13
+
14
+ export { FileSearchToolName, fileSearchInputSchema };
15
+ //# sourceMappingURL=schema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.mjs","sources":["../../../../src/tools/fileSearch/schema.ts"],"sourcesContent":["import { z } from 'zod';\n\nexport const fileSearchInputSchema = z.object({\n query: z\n .string()\n .describe(\n \"A natural language query to search for relevant information in the files. Be SPECIFIC and TARGETED — use keywords for the specific section or topic you need. For comprehensive tasks (summaries, overviews), call this tool multiple times with different targeted queries (e.g., 'introduction', 'methodology', 'results', 'conclusions') rather than one broad query.\"\n ),\n target_files: z\n .array(z.string())\n .optional()\n .describe(\n 'Optional list of filenames (or partial names) to limit the search to. When provided, only files whose name contains one of these strings will be searched. Use this to avoid searching irrelevant files. Omit to search all available files.'\n ),\n});\n\nexport type FileSearchInput = z.infer<typeof fileSearchInputSchema>;\n\nexport const FileSearchToolName = 'file_search';\n"],"names":[],"mappings":";;AAEO,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC;AAC5C,IAAA,KAAK,EAAE;AACJ,SAAA,MAAM;SACN,QAAQ,CACP,0WAA0W,CAC3W;AACH,IAAA,YAAY,EAAE;AACX,SAAA,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE;AAChB,SAAA,QAAQ;SACR,QAAQ,CACP,8OAA8O,CAC/O;AACJ,CAAA;AAIM,MAAM,kBAAkB,GAAG;;;;"}
@@ -0,0 +1,152 @@
1
+ import { tool } from '@langchain/core/tools';
2
+ import { fileSearchInputSchema, FileSearchToolName } from './schema.mjs';
3
+ import { plainTextFormatter } from './formatter.mjs';
4
+
5
+ /**
6
+ * file_search tool factory — library-native equivalent of the CodeExecutor
7
+ * pattern. Runtimes supply a `RagClient`, the file list for this turn, and
8
+ * an optional formatter (ranger uses citation anchors; CLI/A2A use plain
9
+ * text).
10
+ *
11
+ * The tool itself:
12
+ * 1. Accepts `{ query, target_files? }` from the LLM.
13
+ * 2. Filters files by `target_files` substring match when provided.
14
+ * 3. Queries each file in bounded concurrent batches.
15
+ * 4. Enforces per-file timeouts (failures isolated per file).
16
+ * 5. Flattens chunks, deprioritizes stale-turn files, caps results.
17
+ * 6. Hands formatted output to the runtime's formatter for final shape.
18
+ */
19
+ const DEFAULT_QUERY_TIMEOUT_MS = 15_000;
20
+ const DEFAULT_CONCURRENCY = 10;
21
+ const DEFAULT_TOP_K = 10;
22
+ /**
23
+ * Build the tool description. Runtimes that use citation anchors supply
24
+ * `fileCitations: true` (via the formatter); the description includes the
25
+ * citation ruleset only when that's on.
26
+ */
27
+ function buildDescription(opts) {
28
+ const core = `Performs semantic search across the attached "${FileSearchToolName}" documents using natural language queries. Analyzes the content of loaded files to find relevant information, quotes, and passages matching the query.
29
+
30
+ **Use target_files to narrow the search:**
31
+ When you know which file(s) contain the relevant information, ALWAYS pass target_files. This is faster and returns more focused results. Pass partial filenames — they match via substring.
32
+
33
+ **Multiple searches for thorough analysis:**
34
+ For summaries/overviews, call this tool MULTIPLE times with DIFFERENT queries targeting different aspects (intro, methodology, results, conclusions). A single search only returns chunks from one part of the document.`;
35
+ if (!opts.fileCitations)
36
+ return core;
37
+ return `${core}
38
+
39
+ **CITING FILE SEARCH RESULTS — MANDATORY:**
40
+ Cite EVERY statement derived from file content. Place the citation anchor IMMEDIATELY after each paragraph using that source. Each search result has a unique source index — use DIFFERENT indices for different claims; do not reuse the same anchor for all paragraphs. Format: \`\\ue202turn0fileN\`. With a page: include \`(p. N)\` inline. Multiple sources: \`\\ue200\\ue202turn0file0\\ue202turn0file1\\ue201\`. NEVER substitute with footnotes, brackets, or symbols.`;
41
+ }
42
+ function createFileSearchTool(config) {
43
+ const { ragClient, files, entity_id, scope, getAuthHeaders, formatter = plainTextFormatter, queryTimeoutMs = DEFAULT_QUERY_TIMEOUT_MS, concurrencyLimit = DEFAULT_CONCURRENCY, topK = DEFAULT_TOP_K, resultCap, callbacks, logger, } = config;
44
+ // Monotonic call counter used by citation-style formatters to keep source
45
+ // indices unique across multiple invocations within a single turn.
46
+ let callIndex = 0;
47
+ // Infer whether the formatter wants citations from the artifact it emits
48
+ // on an empty-chunk format. This keeps the description/behavior aligned
49
+ // without forcing the host to declare `fileCitations` twice.
50
+ const fileCitations = formatter !== plainTextFormatter;
51
+ return tool(async (rawInput) => {
52
+ const { query, target_files } = rawInput;
53
+ if (files.length === 0) {
54
+ return [
55
+ 'No files to search. Instruct the user to add files for the search.',
56
+ undefined,
57
+ ];
58
+ }
59
+ // target_files: case-insensitive substring match, fallback to all
60
+ // files with a warning if the filter excludes everything.
61
+ let filesToQuery = files;
62
+ if (target_files && target_files.length > 0) {
63
+ const lowerTargets = target_files.map((t) => t.toLowerCase());
64
+ const matched = files.filter((f) => lowerTargets.some((t) => f.filename.toLowerCase().includes(t)));
65
+ if (matched.length === 0) {
66
+ logger?.warn(`[file_search] No files matched target_files ${target_files.join(', ')}; falling back to all files`);
67
+ filesToQuery = files;
68
+ }
69
+ else {
70
+ logger?.info(`[file_search] Filtered to ${matched.length}/${files.length} via target_files`);
71
+ filesToQuery = matched;
72
+ }
73
+ }
74
+ const authHeaders = getAuthHeaders ? await getAuthHeaders() : undefined;
75
+ const queryOne = async (file) => {
76
+ const params = {
77
+ file_id: file.file_id,
78
+ query,
79
+ k: topK,
80
+ entity_id,
81
+ scope,
82
+ authHeaders,
83
+ timeoutMs: queryTimeoutMs,
84
+ };
85
+ try {
86
+ const chunks = await ragClient.query(params);
87
+ callbacks?.onFileQueried?.(file, chunks.length);
88
+ return chunks;
89
+ }
90
+ catch (err) {
91
+ const e = err instanceof Error ? err : new Error(String(err));
92
+ logger?.error(`[file_search] Query failed for ${file.filename}: ${e.message}`);
93
+ callbacks?.onFileError?.(file, e);
94
+ return [];
95
+ }
96
+ };
97
+ // Bounded-concurrency batching. Server-side rerankers handle their
98
+ // own concurrency; this protects the HTTP connection pool when the
99
+ // agent has many files.
100
+ const allChunks = [];
101
+ for (let i = 0; i < filesToQuery.length; i += concurrencyLimit) {
102
+ const batch = filesToQuery.slice(i, i + concurrencyLimit);
103
+ const batchResults = await Promise.all(batch.map(queryOne));
104
+ for (const chunks of batchResults)
105
+ allChunks.push(...chunks);
106
+ }
107
+ if (allChunks.length === 0) {
108
+ return [
109
+ 'No content found in the files. The files may not have been processed correctly or the query may need refinement.',
110
+ undefined,
111
+ ];
112
+ }
113
+ // Build annotated results: attach filename + isCurrentMessage via
114
+ // a file-id lookup (metadata wins, factory list is fallback).
115
+ const fileById = new Map(files.map((f) => [f.file_id, f]));
116
+ const annotated = allChunks.map((c) => {
117
+ const matched = fileById.get(c.file_id);
118
+ const filename = (c.metadata?.source
119
+ ? String(c.metadata.source).split(/[/\\]/).pop()
120
+ : undefined) ??
121
+ matched?.filename ??
122
+ 'Unknown';
123
+ return {
124
+ ...c,
125
+ filename,
126
+ isCurrentMessage: matched?.isCurrentMessage === true,
127
+ };
128
+ });
129
+ // Sort: current-turn files first, then by relevance (lower distance).
130
+ annotated.sort((a, b) => {
131
+ if (a.isCurrentMessage !== b.isCurrentMessage)
132
+ return a.isCurrentMessage ? -1 : 1;
133
+ return a.distance - b.distance;
134
+ });
135
+ const cap = resultCap ?? Math.max(10, filesToQuery.length * 3);
136
+ const limited = annotated.slice(0, cap);
137
+ const { message, artifact } = formatter.format(limited, {
138
+ callIndex,
139
+ files,
140
+ });
141
+ callIndex += 1;
142
+ return [message, artifact];
143
+ }, {
144
+ name: FileSearchToolName,
145
+ responseFormat: 'content_and_artifact',
146
+ description: buildDescription({ fileCitations }),
147
+ schema: fileSearchInputSchema,
148
+ });
149
+ }
150
+
151
+ export { FileSearchToolName, createFileSearchTool };
152
+ //# sourceMappingURL=tool.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.mjs","sources":["../../../../src/tools/fileSearch/tool.ts"],"sourcesContent":["/**\n * file_search tool factory — library-native equivalent of the CodeExecutor\n * pattern. Runtimes supply a `RagClient`, the file list for this turn, and\n * an optional formatter (ranger uses citation anchors; CLI/A2A use plain\n * text).\n *\n * The tool itself:\n * 1. Accepts `{ query, target_files? }` from the LLM.\n * 2. Filters files by `target_files` substring match when provided.\n * 3. Queries each file in bounded concurrent batches.\n * 4. Enforces per-file timeouts (failures isolated per file).\n * 5. Flattens chunks, deprioritizes stale-turn files, caps results.\n * 6. Hands formatted output to the runtime's formatter for final shape.\n */\n\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport {\n fileSearchInputSchema,\n type FileSearchInput,\n FileSearchToolName,\n} from './schema';\nimport type {\n FileSearchToolConfig,\n FileSearchFile,\n RagChunk,\n RagQueryParams,\n} from './types';\nimport { plainTextFormatter } from './formatter';\n\nconst DEFAULT_QUERY_TIMEOUT_MS = 15_000;\nconst DEFAULT_CONCURRENCY = 10;\nconst DEFAULT_TOP_K = 10;\n\n/**\n * Build the tool description. Runtimes that use citation anchors supply\n * `fileCitations: true` (via the formatter); the description includes the\n * citation ruleset only when that's on.\n */\nfunction buildDescription(opts: { fileCitations: boolean }): string {\n const core = `Performs semantic search across the attached \"${FileSearchToolName}\" documents using natural language queries. Analyzes the content of loaded files to find relevant information, quotes, and passages matching the query.\n\n**Use target_files to narrow the search:**\nWhen you know which file(s) contain the relevant information, ALWAYS pass target_files. This is faster and returns more focused results. Pass partial filenames — they match via substring.\n\n**Multiple searches for thorough analysis:**\nFor summaries/overviews, call this tool MULTIPLE times with DIFFERENT queries targeting different aspects (intro, methodology, results, conclusions). A single search only returns chunks from one part of the document.`;\n\n if (!opts.fileCitations) return core;\n\n return `${core}\n\n**CITING FILE SEARCH RESULTS — MANDATORY:**\nCite EVERY statement derived from file content. Place the citation anchor IMMEDIATELY after each paragraph using that source. Each search result has a unique source index — use DIFFERENT indices for different claims; do not reuse the same anchor for all paragraphs. Format: \\`\\\\ue202turn0fileN\\`. With a page: include \\`(p. N)\\` inline. Multiple sources: \\`\\\\ue200\\\\ue202turn0file0\\\\ue202turn0file1\\\\ue201\\`. NEVER substitute with footnotes, brackets, or symbols.`;\n}\n\nexport function createFileSearchTool(\n config: FileSearchToolConfig\n): DynamicStructuredTool {\n const {\n ragClient,\n files,\n entity_id,\n scope,\n getAuthHeaders,\n formatter = plainTextFormatter,\n queryTimeoutMs = DEFAULT_QUERY_TIMEOUT_MS,\n concurrencyLimit = DEFAULT_CONCURRENCY,\n topK = DEFAULT_TOP_K,\n resultCap,\n callbacks,\n logger,\n } = config;\n\n // Monotonic call counter used by citation-style formatters to keep source\n // indices unique across multiple invocations within a single turn.\n let callIndex = 0;\n\n // Infer whether the formatter wants citations from the artifact it emits\n // on an empty-chunk format. This keeps the description/behavior aligned\n // without forcing the host to declare `fileCitations` twice.\n const fileCitations = formatter !== plainTextFormatter;\n\n return tool(\n async (rawInput: FileSearchInput) => {\n const { query, target_files } = rawInput;\n\n if (files.length === 0) {\n return [\n 'No files to search. Instruct the user to add files for the search.',\n undefined,\n ];\n }\n\n // target_files: case-insensitive substring match, fallback to all\n // files with a warning if the filter excludes everything.\n let filesToQuery: FileSearchFile[] = files;\n if (target_files && target_files.length > 0) {\n const lowerTargets = target_files.map((t) => t.toLowerCase());\n const matched = files.filter((f) =>\n lowerTargets.some((t) => f.filename.toLowerCase().includes(t))\n );\n if (matched.length === 0) {\n logger?.warn(\n `[file_search] No files matched target_files ${target_files.join(', ')}; falling back to all files`\n );\n filesToQuery = files;\n } else {\n logger?.info(\n `[file_search] Filtered to ${matched.length}/${files.length} via target_files`\n );\n filesToQuery = matched;\n }\n }\n\n const authHeaders = getAuthHeaders ? await getAuthHeaders() : undefined;\n\n const queryOne = async (file: FileSearchFile): Promise<RagChunk[]> => {\n const params: RagQueryParams = {\n file_id: file.file_id,\n query,\n k: topK,\n entity_id,\n scope,\n authHeaders,\n timeoutMs: queryTimeoutMs,\n };\n try {\n const chunks = await ragClient.query(params);\n callbacks?.onFileQueried?.(file, chunks.length);\n return chunks;\n } catch (err) {\n const e = err instanceof Error ? err : new Error(String(err));\n logger?.error(\n `[file_search] Query failed for ${file.filename}: ${e.message}`\n );\n callbacks?.onFileError?.(file, e);\n return [];\n }\n };\n\n // Bounded-concurrency batching. Server-side rerankers handle their\n // own concurrency; this protects the HTTP connection pool when the\n // agent has many files.\n const allChunks: RagChunk[] = [];\n for (let i = 0; i < filesToQuery.length; i += concurrencyLimit) {\n const batch = filesToQuery.slice(i, i + concurrencyLimit);\n const batchResults = await Promise.all(batch.map(queryOne));\n for (const chunks of batchResults) allChunks.push(...chunks);\n }\n\n if (allChunks.length === 0) {\n return [\n 'No content found in the files. The files may not have been processed correctly or the query may need refinement.',\n undefined,\n ];\n }\n\n // Build annotated results: attach filename + isCurrentMessage via\n // a file-id lookup (metadata wins, factory list is fallback).\n const fileById = new Map(files.map((f) => [f.file_id, f]));\n const annotated = allChunks.map((c) => {\n const matched = fileById.get(c.file_id);\n const filename =\n (c.metadata?.source\n ? String(c.metadata.source).split(/[/\\\\]/).pop()\n : undefined) ??\n matched?.filename ??\n 'Unknown';\n return {\n ...c,\n filename,\n isCurrentMessage: matched?.isCurrentMessage === true,\n };\n });\n\n // Sort: current-turn files first, then by relevance (lower distance).\n annotated.sort((a, b) => {\n if (a.isCurrentMessage !== b.isCurrentMessage)\n return a.isCurrentMessage ? -1 : 1;\n return a.distance - b.distance;\n });\n\n const cap = resultCap ?? Math.max(10, filesToQuery.length * 3);\n const limited = annotated.slice(0, cap);\n\n const { message, artifact } = formatter.format(limited, {\n callIndex,\n files,\n });\n callIndex += 1;\n\n // Suppress unused-variable warning for fileCitations (currently only\n // used to gate description; kept in case formatters need it).\n void fileCitations;\n\n return [message, artifact];\n },\n {\n name: FileSearchToolName,\n responseFormat: 'content_and_artifact',\n description: buildDescription({ fileCitations }),\n schema: fileSearchInputSchema,\n }\n );\n}\n\nexport { FileSearchToolName } from './schema';\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;;;;AAaG;AAgBH,MAAM,wBAAwB,GAAG,MAAM;AACvC,MAAM,mBAAmB,GAAG,EAAE;AAC9B,MAAM,aAAa,GAAG,EAAE;AAExB;;;;AAIG;AACH,SAAS,gBAAgB,CAAC,IAAgC,EAAA;IACxD,MAAM,IAAI,GAAG,CAAA,8CAAA,EAAiD,kBAAkB,CAAA;;;;;;yNAMuI;IAEvN,IAAI,CAAC,IAAI,CAAC,aAAa;AAAE,QAAA,OAAO,IAAI;AAEpC,IAAA,OAAO,GAAG,IAAI;;;gdAGgc;AAChd;AAEM,SAAU,oBAAoB,CAClC,MAA4B,EAAA;AAE5B,IAAA,MAAM,EACJ,SAAS,EACT,KAAK,EACL,SAAS,EACT,KAAK,EACL,cAAc,EACd,SAAS,GAAG,kBAAkB,EAC9B,cAAc,GAAG,wBAAwB,EACzC,gBAAgB,GAAG,mBAAmB,EACtC,IAAI,GAAG,aAAa,EACpB,SAAS,EACT,SAAS,EACT,MAAM,GACP,GAAG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;YACtD,SAAS;YACT,KAAK;AACN,SAAA,CAAC;QACF,SAAS,IAAI,CAAC;AAMd,QAAA,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC;AAC5B,IAAA,CAAC,EACD;AACE,QAAA,IAAI,EAAE,kBAAkB;AACxB,QAAA,cAAc,EAAE,sBAAsB;AACtC,QAAA,WAAW,EAAE,gBAAgB,CAAC,EAAE,aAAa,EAAE,CAAC;AAChD,QAAA,MAAM,EAAE,qBAAqB;AAC9B,KAAA,CACF;AACH;;;;"}
@@ -16,6 +16,8 @@ export * from './tools/schema';
16
16
  export * from './tools/handlers';
17
17
  export * from './tools/search';
18
18
  export * from './tools/memory';
19
+ export * from './tools/fileSearch';
20
+ export * from './tools/artifacts';
19
21
  export * from './tools/proxyTool';
20
22
  export * from './providers';
21
23
  export * from './memory';
@@ -0,0 +1,3 @@
1
+ export { createArtifactTool, createContentReaderTool, ARTIFACT_TOOL_NAME, CONTENT_READER_NAME, ARTIFACT_WRITE_ACTIONS, CONTENT_READ_ACTIONS, } from './tool';
2
+ export { artifactToolSchema, contentReaderSchema, type ArtifactToolInput, type ContentReaderInput, } from './schema';
3
+ export type { ArtifactToolScope, ArtifactToolResult, ArtifactToolLogger, ArtifactToolBaseConfig, ArtifactToolConfig, ContentReaderToolConfig, ArtifactWriteHandlers, ContentReadHandlers, ContentIdResolver, WriteArgs, EditArgs, VerifyArgs, DeleteArgs, ReadArgs, SearchArgs, ListArgs, InfoArgs, } from './types';
@@ -0,0 +1,63 @@
1
+ import { z } from 'zod';
2
+ export declare const ARTIFACT_WRITE_ACTIONS: readonly ["write", "edit", "verify", "delete"];
3
+ export declare const CONTENT_READ_ACTIONS: readonly ["read", "search", "list", "info"];
4
+ export declare const artifactToolSchema: z.ZodObject<{
5
+ action: z.ZodEnum<["write", "edit", "verify", "delete"]>;
6
+ content_id: z.ZodOptional<z.ZodString>;
7
+ content: z.ZodOptional<z.ZodString>;
8
+ name: z.ZodOptional<z.ZodString>;
9
+ old_str: z.ZodOptional<z.ZodString>;
10
+ new_str: z.ZodOptional<z.ZodString>;
11
+ replace_all: z.ZodOptional<z.ZodBoolean>;
12
+ }, "strip", z.ZodTypeAny, {
13
+ action: "write" | "delete" | "edit" | "verify";
14
+ content?: string | undefined;
15
+ name?: string | undefined;
16
+ old_str?: string | undefined;
17
+ new_str?: string | undefined;
18
+ content_id?: string | undefined;
19
+ replace_all?: boolean | undefined;
20
+ }, {
21
+ action: "write" | "delete" | "edit" | "verify";
22
+ content?: string | undefined;
23
+ name?: string | undefined;
24
+ old_str?: string | undefined;
25
+ new_str?: string | undefined;
26
+ content_id?: string | undefined;
27
+ replace_all?: boolean | undefined;
28
+ }>;
29
+ export declare const contentReaderSchema: z.ZodObject<{
30
+ action: z.ZodEnum<["read", "search", "list", "info"]>;
31
+ content_id: z.ZodOptional<z.ZodString>;
32
+ start_line: z.ZodOptional<z.ZodNumber>;
33
+ end_line: z.ZodOptional<z.ZodNumber>;
34
+ pattern: z.ZodOptional<z.ZodString>;
35
+ flags: z.ZodOptional<z.ZodString>;
36
+ context: z.ZodOptional<z.ZodNumber>;
37
+ offset: z.ZodOptional<z.ZodNumber>;
38
+ limit: z.ZodOptional<z.ZodNumber>;
39
+ }, "strip", z.ZodTypeAny, {
40
+ action: "read" | "search" | "info" | "list";
41
+ pattern?: string | undefined;
42
+ context?: number | undefined;
43
+ flags?: string | undefined;
44
+ content_id?: string | undefined;
45
+ start_line?: number | undefined;
46
+ end_line?: number | undefined;
47
+ offset?: number | undefined;
48
+ limit?: number | undefined;
49
+ }, {
50
+ action: "read" | "search" | "info" | "list";
51
+ pattern?: string | undefined;
52
+ context?: number | undefined;
53
+ flags?: string | undefined;
54
+ content_id?: string | undefined;
55
+ start_line?: number | undefined;
56
+ end_line?: number | undefined;
57
+ offset?: number | undefined;
58
+ limit?: number | undefined;
59
+ }>;
60
+ export declare const ARTIFACT_TOOL_NAME = "artifact_tool";
61
+ export declare const CONTENT_READER_NAME = "content_reader";
62
+ export type ArtifactToolInput = z.infer<typeof artifactToolSchema>;
63
+ export type ContentReaderInput = z.infer<typeof contentReaderSchema>;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * artifact_tool + content_reader library factories.
3
+ *
4
+ * The library owns the LangChain wiring (schema, description, response
5
+ * shape) and the action dispatch; the runtime supplies a handler bundle
6
+ * matching the `ArtifactWriteHandlers` / `ContentReadHandlers` interface.
7
+ *
8
+ * This keeps 800+ LOC of host-specific handler logic (S3 adapters, file
9
+ * model CRUD, syntax checkers, line utils) out of the library while
10
+ * still centralizing the tool surface every runtime shares.
11
+ */
12
+ import { DynamicStructuredTool } from '@langchain/core/tools';
13
+ import type { ArtifactToolConfig, ContentReaderToolConfig } from './types';
14
+ export declare function createArtifactTool(config: ArtifactToolConfig): DynamicStructuredTool;
15
+ export declare function createContentReaderTool(config: ContentReaderToolConfig): DynamicStructuredTool;
16
+ export { ARTIFACT_TOOL_NAME, CONTENT_READER_NAME, ARTIFACT_WRITE_ACTIONS, CONTENT_READ_ACTIONS, } from './schema';
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Artifact-tool types. The library owns the LangChain wiring, schema, and
3
+ * action dispatch. Runtime supplies an `ArtifactHandlers` bundle — one
4
+ * function per action — so ranger reuses its existing handlers, CLI
5
+ * supplies disk-backed ones, and A2A server supplies buffer-backed ones.
6
+ *
7
+ * Each handler receives:
8
+ * - `args`: the parsed input (typed per action)
9
+ * - `scope`: runtime-resolved scope identity (conversationId + userId
10
+ * for ranger, agent-run-id for CLI, a2a-task-id for A2A)
11
+ *
12
+ * Each handler returns a `[llmText, toolArtifact]` tuple matching
13
+ * LangChain's `content_and_artifact` response format.
14
+ */
15
+ export interface ArtifactToolScope {
16
+ /**
17
+ * Primary scope — whatever the runtime uses to silo artifacts per
18
+ * session/run/conversation. Ranger uses `conversationId`; CLI uses
19
+ * `agent-run-id` or `agent-id`; A2A uses `a2a-task-id`.
20
+ */
21
+ conversationId: string;
22
+ /** Optional — present when the runtime has an authenticated user. */
23
+ userId?: string;
24
+ /** Free-form extension — runtimes can add their own fields. */
25
+ [key: string]: unknown;
26
+ }
27
+ export type ArtifactToolResult = [llmText: string, artifact?: unknown];
28
+ export interface WriteArgs {
29
+ action: 'write';
30
+ content_id?: string;
31
+ content: string;
32
+ name?: string;
33
+ }
34
+ export interface EditArgs {
35
+ action: 'edit';
36
+ content_id: string;
37
+ old_str: string;
38
+ new_str: string;
39
+ replace_all?: boolean;
40
+ }
41
+ export interface VerifyArgs {
42
+ action: 'verify';
43
+ content_id: string;
44
+ }
45
+ export interface DeleteArgs {
46
+ action: 'delete';
47
+ content_id: string;
48
+ }
49
+ export interface ReadArgs {
50
+ action: 'read';
51
+ content_id: string;
52
+ start_line?: number;
53
+ end_line?: number;
54
+ offset?: number;
55
+ limit?: number;
56
+ }
57
+ export interface SearchArgs {
58
+ action: 'search';
59
+ content_id: string;
60
+ pattern: string;
61
+ flags?: string;
62
+ context?: number;
63
+ offset?: number;
64
+ limit?: number;
65
+ }
66
+ export interface ListArgs {
67
+ action: 'list';
68
+ }
69
+ export interface InfoArgs {
70
+ action: 'info';
71
+ content_id: string;
72
+ }
73
+ export type ArtifactWriteAction = WriteArgs | EditArgs | VerifyArgs | DeleteArgs;
74
+ export type ArtifactReadAction = ReadArgs | SearchArgs | ListArgs | InfoArgs;
75
+ /** Writer-surface handlers — invoked by `artifact_tool`. */
76
+ export interface ArtifactWriteHandlers {
77
+ write(args: WriteArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
78
+ edit(args: EditArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
79
+ verify(args: VerifyArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
80
+ delete(args: DeleteArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
81
+ }
82
+ /** Reader-surface handlers — invoked by `content_reader`. */
83
+ export interface ContentReadHandlers {
84
+ read(args: ReadArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
85
+ search(args: SearchArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
86
+ list(args: ListArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
87
+ info(args: InfoArgs, scope: ArtifactToolScope): Promise<ArtifactToolResult>;
88
+ }
89
+ /**
90
+ * Optional content_id self-healing: runtimes that want to let the LLM
91
+ * pass nicknames (e.g., "Dashboard") instead of canonical IDs implement
92
+ * this. Library falls through when unset.
93
+ */
94
+ export interface ContentIdResolver {
95
+ resolve(id: string, scope: ArtifactToolScope): Promise<{
96
+ resolvedId: string;
97
+ resolvedName?: string;
98
+ } | null>;
99
+ }
100
+ export interface ArtifactToolLogger {
101
+ debug: (msg: string, ...args: unknown[]) => void;
102
+ info: (msg: string, ...args: unknown[]) => void;
103
+ warn: (msg: string, ...args: unknown[]) => void;
104
+ error: (msg: string, ...args: unknown[]) => void;
105
+ }
106
+ export interface ArtifactToolBaseConfig {
107
+ /**
108
+ * Resolves the runtime scope from the LangChain `config` object on each
109
+ * invocation. Ranger pulls `conversationId` + `userId` from request
110
+ * metadata; CLI pulls `agent-run-id` from its runner context.
111
+ */
112
+ getScope: (config: unknown) => ArtifactToolScope | null;
113
+ resolver?: ContentIdResolver;
114
+ logger?: ArtifactToolLogger;
115
+ /**
116
+ * Description override. Host can inject brand-specific guidance
117
+ * (e.g., ranger's HTML branding mandate, TSX style rules). Defaults
118
+ * to a generic description appropriate for any runtime.
119
+ */
120
+ descriptionOverride?: string;
121
+ }
122
+ export interface ArtifactToolConfig extends ArtifactToolBaseConfig {
123
+ handlers: ArtifactWriteHandlers;
124
+ }
125
+ export interface ContentReaderToolConfig extends ArtifactToolBaseConfig {
126
+ handlers: ContentReadHandlers;
127
+ }