@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,129 @@
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
+
11
+ import type {
12
+ FileSearchResultFormatter,
13
+ FileSearchFile,
14
+ RagChunk,
15
+ } from './types';
16
+
17
+ type AnnotatedChunk = RagChunk & {
18
+ filename: string;
19
+ isCurrentMessage: boolean;
20
+ };
21
+
22
+ export const plainTextFormatter: FileSearchResultFormatter = {
23
+ format(chunks, { files: _files }) {
24
+ if (chunks.length === 0) {
25
+ return { message: 'No relevant results found in the available files.' };
26
+ }
27
+ const body = chunks
28
+ .map((c) => {
29
+ const page = getPage(c);
30
+ const rel = (1 - c.distance).toFixed(4);
31
+ return (
32
+ `File: ${c.filename}` +
33
+ (page != null ? `\nPage: ${page}` : '') +
34
+ `\nRelevance: ${rel}\nContent: ${c.page_content}\n`
35
+ );
36
+ })
37
+ .join('\n---\n');
38
+
39
+ const sources = chunks.map((c) => ({
40
+ type: 'file' as const,
41
+ fileId: c.file_id,
42
+ content: c.page_content,
43
+ fileName: c.filename,
44
+ relevance: 1 - c.distance,
45
+ pages: getPage(c) != null ? [getPage(c) as number] : [],
46
+ }));
47
+
48
+ return { message: body, artifact: { file_search: { sources } } };
49
+ },
50
+ };
51
+
52
+ export interface CitationAnchorFormatterOptions {
53
+ /** Tool name used in the `file_search` artifact wrapper. Defaults to `'file_search'`. */
54
+ toolName?: string;
55
+ /**
56
+ * Monotonic counter for source indices within a turn. Pass the SAME
57
+ * function to the formatter across multiple calls in the same turn so
58
+ * anchors stay globally unique.
59
+ */
60
+ getSourceOffset?: () => number;
61
+ /** Called after formatting to advance the offset. */
62
+ advanceSourceOffset?: (by: number) => void;
63
+ }
64
+
65
+ export function createCitationAnchorFormatter(
66
+ opts: CitationAnchorFormatterOptions = {}
67
+ ): FileSearchResultFormatter {
68
+ const toolName = opts.toolName ?? 'file_search';
69
+ const getOffset = opts.getSourceOffset ?? ((): number => 0);
70
+ const advance = opts.advanceSourceOffset ?? ((_by: number): void => {});
71
+
72
+ return {
73
+ format(chunks): { message: string; artifact?: unknown } {
74
+ if (chunks.length === 0) {
75
+ return {
76
+ message:
77
+ 'No results found or errors occurred while searching the files.',
78
+ };
79
+ }
80
+ const base = getOffset();
81
+ const body = chunks
82
+ .map((c, i) => {
83
+ const globalIndex = base + i;
84
+ const page = getPage(c);
85
+ const rel = (1 - c.distance).toFixed(4);
86
+ return (
87
+ `[Source ${globalIndex}] File: ${c.filename} | Anchor: \\ue202turn0file${globalIndex}` +
88
+ (page != null ? ` | Page: ${page}` : '') +
89
+ ` | Relevance: ${rel}\nContent: ${c.page_content}\n` +
90
+ `↑ Cite this source using: \\ue202turn0file${globalIndex}`
91
+ );
92
+ })
93
+ .join('\n---\n');
94
+
95
+ const sources = chunks.map((c) => ({
96
+ type: 'file' as const,
97
+ fileId: c.file_id,
98
+ content: c.page_content,
99
+ fileName: c.filename,
100
+ relevance: 1 - c.distance,
101
+ pages: getPage(c) != null ? [getPage(c) as number] : [],
102
+ pageRelevance:
103
+ getPage(c) != null ? { [getPage(c) as number]: 1 - c.distance } : {},
104
+ }));
105
+
106
+ advance(chunks.length);
107
+ return {
108
+ message: body,
109
+ artifact: { [toolName]: { sources, fileCitations: true } },
110
+ };
111
+ },
112
+ };
113
+ }
114
+
115
+ /** Extract a 1-indexed page number from the chunk metadata, or null. */
116
+ function getPage(chunk: AnnotatedChunk | RagChunk): number | null {
117
+ const raw =
118
+ (chunk.metadata?.page as unknown) ??
119
+ (chunk.metadata?.page_number as unknown) ??
120
+ null;
121
+ if (raw == null) return null;
122
+ const parsed = typeof raw === 'number' ? raw : parseInt(String(raw), 10);
123
+ if (Number.isNaN(parsed) || parsed < 0) return null;
124
+ // rag_api stores 0-indexed; display is 1-indexed
125
+ return parsed + 1;
126
+ }
127
+
128
+ // Re-export so consumers only import from the formatter module.
129
+ export type { FileSearchResultFormatter, FileSearchFile, RagChunk };
@@ -0,0 +1,23 @@
1
+ export { createFileSearchTool, FileSearchToolName } from './tool';
2
+ export {
3
+ HttpRagClient,
4
+ getRagBaseUrl,
5
+ RAG_API_URL_ENV,
6
+ type HttpRagClientOptions,
7
+ } from './ragClient';
8
+ export {
9
+ plainTextFormatter,
10
+ createCitationAnchorFormatter,
11
+ type CitationAnchorFormatterOptions,
12
+ } from './formatter';
13
+ export { fileSearchInputSchema, type FileSearchInput } from './schema';
14
+ export type {
15
+ FileSearchFile,
16
+ RagChunk,
17
+ RagClient,
18
+ RagQueryParams,
19
+ FileSearchResultFormatter,
20
+ FileSearchToolCallbacks,
21
+ FileSearchToolConfig,
22
+ FileSearchToolLogger,
23
+ } from './types';
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Default HTTP RAG client. Posts to `${baseUrl}/query` with the shape
3
+ * rag_api expects (`{ file_id, query, k, entity_id? }`). Runtimes that
4
+ * use a different vector backend implement their own `RagClient`.
5
+ *
6
+ * Auth is runtime-provided per call (via `authHeaders` on the params) so
7
+ * short-lived tokens can be minted per request without the client
8
+ * caching stale credentials.
9
+ */
10
+
11
+ import fetch from 'node-fetch';
12
+ import { getEnvironmentVariable } from '@langchain/core/utils/env';
13
+ import type {
14
+ RagClient,
15
+ RagQueryParams,
16
+ RagChunk,
17
+ FileSearchToolLogger,
18
+ } from './types';
19
+
20
+ export const RAG_API_URL_ENV = 'RAG_API_URL';
21
+
22
+ /** Resolve base URL at call time so env-var changes propagate. */
23
+ export function getRagBaseUrl(override?: string): string {
24
+ const url = override ?? getEnvironmentVariable(RAG_API_URL_ENV) ?? '';
25
+ if (!url) {
26
+ throw new Error(
27
+ `file_search: ${RAG_API_URL_ENV} is not configured. ` +
28
+ `Set the env var or pass baseUrl to HttpRagClient.`
29
+ );
30
+ }
31
+ return url.replace(/\/$/, '');
32
+ }
33
+
34
+ export interface HttpRagClientOptions {
35
+ /** Base URL of the RAG service (no trailing slash). Falls back to env. */
36
+ baseUrl?: string;
37
+ /** Default headers sent on every request (e.g., a static API key). */
38
+ defaultHeaders?: Record<string, string>;
39
+ /** Default timeout if params don't override. Default 15_000. */
40
+ defaultTimeoutMs?: number;
41
+ logger?: FileSearchToolLogger;
42
+ }
43
+
44
+ /**
45
+ * Expected rag_api response shape: `[[{ page_content, metadata }, distance], ...]`
46
+ * — an array of [doc, score] tuples. Normalized here into `RagChunk[]`.
47
+ */
48
+ type RagApiResponse = Array<
49
+ [
50
+ {
51
+ page_content: string;
52
+ metadata?: Record<string, unknown>;
53
+ },
54
+ number,
55
+ ]
56
+ >;
57
+
58
+ export class HttpRagClient implements RagClient {
59
+ private readonly baseUrlOverride?: string;
60
+ private readonly defaultHeaders: Record<string, string>;
61
+ private readonly defaultTimeoutMs: number;
62
+ private readonly logger?: FileSearchToolLogger;
63
+
64
+ constructor(opts: HttpRagClientOptions = {}) {
65
+ this.baseUrlOverride = opts.baseUrl;
66
+ this.defaultHeaders = opts.defaultHeaders ?? {};
67
+ this.defaultTimeoutMs = opts.defaultTimeoutMs ?? 15_000;
68
+ this.logger = opts.logger;
69
+ }
70
+
71
+ async query(params: RagQueryParams): Promise<RagChunk[]> {
72
+ const baseUrl = getRagBaseUrl(this.baseUrlOverride);
73
+ const url = `${baseUrl}/query`;
74
+
75
+ const body: Record<string, unknown> = {
76
+ file_id: params.file_id,
77
+ query: params.query,
78
+ k: params.k ?? 10,
79
+ };
80
+ if (params.entity_id) body.entity_id = params.entity_id;
81
+ if (params.scope) body.scope = params.scope;
82
+
83
+ const headers: Record<string, string> = {
84
+ 'Content-Type': 'application/json',
85
+ ...this.defaultHeaders,
86
+ ...(params.authHeaders ?? {}),
87
+ };
88
+
89
+ const timeoutMs = params.timeoutMs ?? this.defaultTimeoutMs;
90
+ const controller =
91
+ typeof AbortController !== 'undefined' ? new AbortController() : null;
92
+ const timer = controller
93
+ ? setTimeout(() => controller.abort(), timeoutMs)
94
+ : null;
95
+
96
+ this.logger?.debug('[file_search] RAG query', {
97
+ url,
98
+ file_id: params.file_id,
99
+ k: body.k,
100
+ });
101
+
102
+ try {
103
+ const res = await fetch(url, {
104
+ method: 'POST',
105
+ headers,
106
+ body: JSON.stringify(body),
107
+ signal: controller?.signal as unknown as undefined,
108
+ });
109
+ if (!res.ok) {
110
+ const text = await res.text().catch(() => '');
111
+ throw new Error(
112
+ `RAG query failed: ${res.status} ${res.statusText} — ${text.slice(0, 200)}`
113
+ );
114
+ }
115
+ const json = (await res.json()) as RagApiResponse;
116
+ return this.normalize(params.file_id, json);
117
+ } finally {
118
+ if (timer) clearTimeout(timer);
119
+ }
120
+ }
121
+
122
+ /** Convert rag_api's tuple format into the library's normalized shape. */
123
+ private normalize(file_id: string, resp: RagApiResponse): RagChunk[] {
124
+ if (!Array.isArray(resp)) {
125
+ this.logger?.warn('[file_search] RAG response not an array', { resp });
126
+ return [];
127
+ }
128
+ return resp
129
+ .filter((row) => Array.isArray(row) && row.length === 2)
130
+ .map(([doc, distance]) => ({
131
+ file_id: (doc.metadata?.file_id as string | undefined) ?? file_id,
132
+ page_content: doc.page_content ?? '',
133
+ distance: typeof distance === 'number' ? distance : 1,
134
+ metadata: doc.metadata,
135
+ }));
136
+ }
137
+ }
@@ -0,0 +1,19 @@
1
+ import { z } from 'zod';
2
+
3
+ export const fileSearchInputSchema = z.object({
4
+ query: z
5
+ .string()
6
+ .describe(
7
+ "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."
8
+ ),
9
+ target_files: z
10
+ .array(z.string())
11
+ .optional()
12
+ .describe(
13
+ '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.'
14
+ ),
15
+ });
16
+
17
+ export type FileSearchInput = z.infer<typeof fileSearchInputSchema>;
18
+
19
+ export const FileSearchToolName = 'file_search';
@@ -0,0 +1,207 @@
1
+ /**
2
+ * file_search tool factory — library-native equivalent of the CodeExecutor
3
+ * pattern. Runtimes supply a `RagClient`, the file list for this turn, and
4
+ * an optional formatter (ranger uses citation anchors; CLI/A2A use plain
5
+ * text).
6
+ *
7
+ * The tool itself:
8
+ * 1. Accepts `{ query, target_files? }` from the LLM.
9
+ * 2. Filters files by `target_files` substring match when provided.
10
+ * 3. Queries each file in bounded concurrent batches.
11
+ * 4. Enforces per-file timeouts (failures isolated per file).
12
+ * 5. Flattens chunks, deprioritizes stale-turn files, caps results.
13
+ * 6. Hands formatted output to the runtime's formatter for final shape.
14
+ */
15
+
16
+ import { tool, DynamicStructuredTool } from '@langchain/core/tools';
17
+ import {
18
+ fileSearchInputSchema,
19
+ type FileSearchInput,
20
+ FileSearchToolName,
21
+ } from './schema';
22
+ import type {
23
+ FileSearchToolConfig,
24
+ FileSearchFile,
25
+ RagChunk,
26
+ RagQueryParams,
27
+ } from './types';
28
+ import { plainTextFormatter } from './formatter';
29
+
30
+ const DEFAULT_QUERY_TIMEOUT_MS = 15_000;
31
+ const DEFAULT_CONCURRENCY = 10;
32
+ const DEFAULT_TOP_K = 10;
33
+
34
+ /**
35
+ * Build the tool description. Runtimes that use citation anchors supply
36
+ * `fileCitations: true` (via the formatter); the description includes the
37
+ * citation ruleset only when that's on.
38
+ */
39
+ function buildDescription(opts: { fileCitations: boolean }): string {
40
+ 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.
41
+
42
+ **Use target_files to narrow the search:**
43
+ 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.
44
+
45
+ **Multiple searches for thorough analysis:**
46
+ 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.`;
47
+
48
+ if (!opts.fileCitations) return core;
49
+
50
+ return `${core}
51
+
52
+ **CITING FILE SEARCH RESULTS — MANDATORY:**
53
+ 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.`;
54
+ }
55
+
56
+ export function createFileSearchTool(
57
+ config: FileSearchToolConfig
58
+ ): DynamicStructuredTool {
59
+ const {
60
+ ragClient,
61
+ files,
62
+ entity_id,
63
+ scope,
64
+ getAuthHeaders,
65
+ formatter = plainTextFormatter,
66
+ queryTimeoutMs = DEFAULT_QUERY_TIMEOUT_MS,
67
+ concurrencyLimit = DEFAULT_CONCURRENCY,
68
+ topK = DEFAULT_TOP_K,
69
+ resultCap,
70
+ callbacks,
71
+ logger,
72
+ } = config;
73
+
74
+ // Monotonic call counter used by citation-style formatters to keep source
75
+ // indices unique across multiple invocations within a single turn.
76
+ let callIndex = 0;
77
+
78
+ // Infer whether the formatter wants citations from the artifact it emits
79
+ // on an empty-chunk format. This keeps the description/behavior aligned
80
+ // without forcing the host to declare `fileCitations` twice.
81
+ const fileCitations = formatter !== plainTextFormatter;
82
+
83
+ return tool(
84
+ async (rawInput: FileSearchInput) => {
85
+ const { query, target_files } = rawInput;
86
+
87
+ if (files.length === 0) {
88
+ return [
89
+ 'No files to search. Instruct the user to add files for the search.',
90
+ undefined,
91
+ ];
92
+ }
93
+
94
+ // target_files: case-insensitive substring match, fallback to all
95
+ // files with a warning if the filter excludes everything.
96
+ let filesToQuery: FileSearchFile[] = files;
97
+ if (target_files && target_files.length > 0) {
98
+ const lowerTargets = target_files.map((t) => t.toLowerCase());
99
+ const matched = files.filter((f) =>
100
+ lowerTargets.some((t) => f.filename.toLowerCase().includes(t))
101
+ );
102
+ if (matched.length === 0) {
103
+ logger?.warn(
104
+ `[file_search] No files matched target_files ${target_files.join(', ')}; falling back to all files`
105
+ );
106
+ filesToQuery = files;
107
+ } else {
108
+ logger?.info(
109
+ `[file_search] Filtered to ${matched.length}/${files.length} via target_files`
110
+ );
111
+ filesToQuery = matched;
112
+ }
113
+ }
114
+
115
+ const authHeaders = getAuthHeaders ? await getAuthHeaders() : undefined;
116
+
117
+ const queryOne = async (file: FileSearchFile): Promise<RagChunk[]> => {
118
+ const params: RagQueryParams = {
119
+ file_id: file.file_id,
120
+ query,
121
+ k: topK,
122
+ entity_id,
123
+ scope,
124
+ authHeaders,
125
+ timeoutMs: queryTimeoutMs,
126
+ };
127
+ try {
128
+ const chunks = await ragClient.query(params);
129
+ callbacks?.onFileQueried?.(file, chunks.length);
130
+ return chunks;
131
+ } catch (err) {
132
+ const e = err instanceof Error ? err : new Error(String(err));
133
+ logger?.error(
134
+ `[file_search] Query failed for ${file.filename}: ${e.message}`
135
+ );
136
+ callbacks?.onFileError?.(file, e);
137
+ return [];
138
+ }
139
+ };
140
+
141
+ // Bounded-concurrency batching. Server-side rerankers handle their
142
+ // own concurrency; this protects the HTTP connection pool when the
143
+ // agent has many files.
144
+ const allChunks: RagChunk[] = [];
145
+ for (let i = 0; i < filesToQuery.length; i += concurrencyLimit) {
146
+ const batch = filesToQuery.slice(i, i + concurrencyLimit);
147
+ const batchResults = await Promise.all(batch.map(queryOne));
148
+ for (const chunks of batchResults) allChunks.push(...chunks);
149
+ }
150
+
151
+ if (allChunks.length === 0) {
152
+ return [
153
+ 'No content found in the files. The files may not have been processed correctly or the query may need refinement.',
154
+ undefined,
155
+ ];
156
+ }
157
+
158
+ // Build annotated results: attach filename + isCurrentMessage via
159
+ // a file-id lookup (metadata wins, factory list is fallback).
160
+ const fileById = new Map(files.map((f) => [f.file_id, f]));
161
+ const annotated = allChunks.map((c) => {
162
+ const matched = fileById.get(c.file_id);
163
+ const filename =
164
+ (c.metadata?.source
165
+ ? String(c.metadata.source).split(/[/\\]/).pop()
166
+ : undefined) ??
167
+ matched?.filename ??
168
+ 'Unknown';
169
+ return {
170
+ ...c,
171
+ filename,
172
+ isCurrentMessage: matched?.isCurrentMessage === true,
173
+ };
174
+ });
175
+
176
+ // Sort: current-turn files first, then by relevance (lower distance).
177
+ annotated.sort((a, b) => {
178
+ if (a.isCurrentMessage !== b.isCurrentMessage)
179
+ return a.isCurrentMessage ? -1 : 1;
180
+ return a.distance - b.distance;
181
+ });
182
+
183
+ const cap = resultCap ?? Math.max(10, filesToQuery.length * 3);
184
+ const limited = annotated.slice(0, cap);
185
+
186
+ const { message, artifact } = formatter.format(limited, {
187
+ callIndex,
188
+ files,
189
+ });
190
+ callIndex += 1;
191
+
192
+ // Suppress unused-variable warning for fileCitations (currently only
193
+ // used to gate description; kept in case formatters need it).
194
+ void fileCitations;
195
+
196
+ return [message, artifact];
197
+ },
198
+ {
199
+ name: FileSearchToolName,
200
+ responseFormat: 'content_and_artifact',
201
+ description: buildDescription({ fileCitations }),
202
+ schema: fileSearchInputSchema,
203
+ }
204
+ );
205
+ }
206
+
207
+ export { FileSearchToolName } from './schema';
@@ -0,0 +1,149 @@
1
+ /**
2
+ * File-search tool types. Mirrors the web_search / code_executor split —
3
+ * the library owns tool logic; the host supplies a `RagClient` + config
4
+ * shaped to its own deployment (auth strategy, scope identity, file set).
5
+ */
6
+
7
+ export interface FileSearchFile {
8
+ /** Stable identifier within the RAG backend. */
9
+ file_id: string;
10
+ /** Human-readable name surfaced in tool results and prompts. */
11
+ filename: string;
12
+ /**
13
+ * Hint that this file arrived on the *current* conversation turn (as
14
+ * opposed to an earlier turn). Hosts that don't distinguish leave this
15
+ * undefined; the formatter deprioritizes older files when set.
16
+ */
17
+ isCurrentMessage?: boolean;
18
+ }
19
+
20
+ /**
21
+ * A single chunk returned by the RAG backend for a query.
22
+ * Shape is normalized here so the library stays independent of any
23
+ * specific RAG service's response format — the `RagClient` is responsible
24
+ * for translating the backend response into this shape.
25
+ */
26
+ export interface RagChunk {
27
+ file_id: string;
28
+ page_content: string;
29
+ distance: number; // 0 = perfect match, 1 = orthogonal
30
+ metadata?: Record<string, unknown>;
31
+ }
32
+
33
+ export interface RagQueryParams {
34
+ file_id: string;
35
+ query: string;
36
+ /** Top-K chunks to return per file. Default 10. */
37
+ k?: number;
38
+ /** Optional tenant/entity ID — forwarded to the backend verbatim. */
39
+ entity_id?: string;
40
+ /**
41
+ * Scope identifier. Hosts use this for per-tenant isolation in the RAG
42
+ * backend (ranger → userId, cli → agentId, a2a → task-id).
43
+ */
44
+ scope?: string;
45
+ /**
46
+ * Per-request auth headers — the host builds these (e.g., ranger sends
47
+ * `Authorization: Bearer <short-lived-JWT-for-userId>`). Allows
48
+ * different runtimes to use different auth strategies without the
49
+ * library knowing.
50
+ */
51
+ authHeaders?: Record<string, string>;
52
+ /** Optional per-call timeout override. */
53
+ timeoutMs?: number;
54
+ }
55
+
56
+ /**
57
+ * Pluggable RAG backend. Runtimes provide an implementation that speaks
58
+ * to whatever vector DB / search service they've deployed (rag_api is the
59
+ * default; azure search, pinecone, etc. are valid alternates).
60
+ */
61
+ export interface RagClient {
62
+ query(params: RagQueryParams): Promise<RagChunk[]>;
63
+ }
64
+
65
+ /**
66
+ * Formatter callback that shapes raw chunks into the runtime's preferred
67
+ * presentation. Ranger uses citation anchors (`\ue202turn0fileN`), CLI
68
+ * uses plain text, A2A server can return structured parts.
69
+ */
70
+ export interface FileSearchResultFormatter {
71
+ format(
72
+ chunks: Array<RagChunk & { filename: string; isCurrentMessage: boolean }>,
73
+ context: {
74
+ /**
75
+ * Monotonic call index within a single turn. Ranger uses this to
76
+ * keep citation source indices globally unique across multiple
77
+ * file_search invocations.
78
+ */
79
+ callIndex: number;
80
+ /** Same files list the factory was seeded with (for lookups). */
81
+ files: FileSearchFile[];
82
+ }
83
+ ): {
84
+ /** Message returned to the LLM (content). */
85
+ message: string;
86
+ /** Optional artifact payload (sources, metadata) returned alongside. */
87
+ artifact?: unknown;
88
+ };
89
+ }
90
+
91
+ export interface FileSearchToolCallbacks {
92
+ /**
93
+ * Fires when a RAG query completes successfully for one file. Hosts can
94
+ * use this for telemetry, streaming UI updates, etc.
95
+ */
96
+ onFileQueried?: (file: FileSearchFile, chunkCount: number) => void;
97
+ /** Fires when a RAG query fails. Hosts can log or surface via UI. */
98
+ onFileError?: (file: FileSearchFile, error: Error) => void;
99
+ }
100
+
101
+ export interface FileSearchToolLogger {
102
+ debug: (msg: string, ...args: unknown[]) => void;
103
+ info: (msg: string, ...args: unknown[]) => void;
104
+ warn: (msg: string, ...args: unknown[]) => void;
105
+ error: (msg: string, ...args: unknown[]) => void;
106
+ }
107
+
108
+ export interface FileSearchToolConfig {
109
+ /** The RAG backend client. Required. */
110
+ ragClient: RagClient;
111
+ /** Files the agent has access to this turn. Empty = tool self-reports so. */
112
+ files: FileSearchFile[];
113
+ /** Tenant/entity ID forwarded to the backend per-query. */
114
+ entity_id?: string;
115
+ /**
116
+ * Per-turn scope identity. Most runtimes pin this once (userId/agentId);
117
+ * if omitted, no scope is sent to the backend.
118
+ */
119
+ scope?: string;
120
+ /**
121
+ * Per-call auth header builder. Called on every tool invocation so
122
+ * the host can mint fresh short-lived tokens (ranger's JWT pattern).
123
+ * When omitted, no auth headers are sent.
124
+ */
125
+ getAuthHeaders?: () =>
126
+ | Record<string, string>
127
+ | Promise<Record<string, string>>;
128
+ /**
129
+ * Result formatter. When omitted, the default plain-text formatter is
130
+ * used (suitable for CLI/A2A runtimes that don't need citation anchors).
131
+ */
132
+ formatter?: FileSearchResultFormatter;
133
+ /** Per-file query timeout in ms. Default 15_000. */
134
+ queryTimeoutMs?: number;
135
+ /**
136
+ * Max concurrent in-flight RAG queries. Protects HTTP connection pools
137
+ * under heavy file counts. Default 10.
138
+ */
139
+ concurrencyLimit?: number;
140
+ /** Top-K chunks per file. Default 10. */
141
+ topK?: number;
142
+ /**
143
+ * Cap on total chunks returned to the LLM after sorting. Default is
144
+ * `max(10, filesCount * 3)`.
145
+ */
146
+ resultCap?: number;
147
+ callbacks?: FileSearchToolCallbacks;
148
+ logger?: FileSearchToolLogger;
149
+ }