@illuma-ai/agents 1.4.0-alpha.1 → 1.4.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/main.cjs +20 -8
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/fileSearch/formatter.cjs +95 -0
- package/dist/cjs/tools/fileSearch/formatter.cjs.map +1 -0
- package/dist/cjs/tools/fileSearch/ragClient.cjs +104 -0
- package/dist/cjs/tools/fileSearch/ragClient.cjs.map +1 -0
- package/dist/cjs/tools/fileSearch/schema.cjs +18 -0
- package/dist/cjs/tools/fileSearch/schema.cjs.map +1 -0
- package/dist/cjs/tools/fileSearch/tool.cjs +155 -0
- package/dist/cjs/tools/fileSearch/tool.cjs.map +1 -0
- package/dist/esm/main.mjs +4 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/tools/fileSearch/formatter.mjs +92 -0
- package/dist/esm/tools/fileSearch/formatter.mjs.map +1 -0
- package/dist/esm/tools/fileSearch/ragClient.mjs +100 -0
- package/dist/esm/tools/fileSearch/ragClient.mjs.map +1 -0
- package/dist/esm/tools/fileSearch/schema.mjs +15 -0
- package/dist/esm/tools/fileSearch/schema.mjs.map +1 -0
- package/dist/esm/tools/fileSearch/tool.mjs +152 -0
- package/dist/esm/tools/fileSearch/tool.mjs.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/tools/fileSearch/formatter.d.ts +25 -0
- package/dist/types/tools/fileSearch/index.d.ts +5 -0
- package/dist/types/tools/fileSearch/ragClient.d.ts +32 -0
- package/dist/types/tools/fileSearch/schema.d.ts +13 -0
- package/dist/types/tools/fileSearch/tool.d.ts +18 -0
- package/dist/types/tools/fileSearch/types.d.ts +139 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/tools/fileSearch/__tests__/tool.test.ts +251 -0
- package/src/tools/fileSearch/formatter.ts +131 -0
- package/src/tools/fileSearch/index.ts +23 -0
- package/src/tools/fileSearch/ragClient.ts +141 -0
- package/src/tools/fileSearch/schema.ts +19 -0
- package/src/tools/fileSearch/tool.ts +207 -0
- package/src/tools/fileSearch/types.ts +147 -0
|
@@ -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,147 @@
|
|
|
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?: () => Record<string, string> | Promise<Record<string, string>>;
|
|
126
|
+
/**
|
|
127
|
+
* Result formatter. When omitted, the default plain-text formatter is
|
|
128
|
+
* used (suitable for CLI/A2A runtimes that don't need citation anchors).
|
|
129
|
+
*/
|
|
130
|
+
formatter?: FileSearchResultFormatter;
|
|
131
|
+
/** Per-file query timeout in ms. Default 15_000. */
|
|
132
|
+
queryTimeoutMs?: number;
|
|
133
|
+
/**
|
|
134
|
+
* Max concurrent in-flight RAG queries. Protects HTTP connection pools
|
|
135
|
+
* under heavy file counts. Default 10.
|
|
136
|
+
*/
|
|
137
|
+
concurrencyLimit?: number;
|
|
138
|
+
/** Top-K chunks per file. Default 10. */
|
|
139
|
+
topK?: number;
|
|
140
|
+
/**
|
|
141
|
+
* Cap on total chunks returned to the LLM after sorting. Default is
|
|
142
|
+
* `max(10, filesCount * 3)`.
|
|
143
|
+
*/
|
|
144
|
+
resultCap?: number;
|
|
145
|
+
callbacks?: FileSearchToolCallbacks;
|
|
146
|
+
logger?: FileSearchToolLogger;
|
|
147
|
+
}
|