@stackfactor/agent-utils 1.10.0 → 1.12.0
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/README.md +24 -0
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/langChain.d.ts +8 -0
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +3 -2
- package/dist/cjs/referenceContext.d.ts +238 -0
- package/dist/cjs/referenceContext.d.ts.map +1 -0
- package/dist/cjs/referenceContext.js +399 -0
- package/dist/cjs/serve.d.ts.map +1 -1
- package/dist/cjs/serve.js +7 -0
- package/dist/cjs/stackfactorKnowledge.d.ts +9 -4
- package/dist/cjs/stackfactorKnowledge.d.ts.map +1 -1
- package/dist/cjs/stackfactorKnowledge.js +30 -6
- package/dist/cjs/tavily.d.ts +8 -0
- package/dist/cjs/tavily.d.ts.map +1 -1
- package/dist/cjs/tavily.js +73 -4
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/langChain.d.ts +8 -0
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +3 -2
- package/dist/esm/referenceContext.d.ts +238 -0
- package/dist/esm/referenceContext.d.ts.map +1 -0
- package/dist/esm/referenceContext.js +383 -0
- package/dist/esm/serve.d.ts.map +1 -1
- package/dist/esm/serve.js +7 -0
- package/dist/esm/stackfactorKnowledge.d.ts +9 -4
- package/dist/esm/stackfactorKnowledge.d.ts.map +1 -1
- package/dist/esm/stackfactorKnowledge.js +30 -6
- package/dist/esm/tavily.d.ts +8 -0
- package/dist/esm/tavily.d.ts.map +1 -1
- package/dist/esm/tavily.js +73 -4
- package/package.json +1 -1
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Reference context — gathering what a list of references points at
|
|
3
|
+
//
|
|
4
|
+
// An agent that reads its references never discovers new sources. It reads
|
|
5
|
+
// what they point at:
|
|
6
|
+
//
|
|
7
|
+
// - pages: WEB and PUBLICATION references with an http(s) url, which the
|
|
8
|
+
// agent opens through Tavily `extract`. A STACKFACTOR reference's url is
|
|
9
|
+
// the document's page at its source and is never opened as a page;
|
|
10
|
+
// - passages: the passages of the knowledge base documents STACKFACTOR
|
|
11
|
+
// references cite, searched under the run's own token, so a run reads only
|
|
12
|
+
// what the person it acts for may open.
|
|
13
|
+
//
|
|
14
|
+
// A cited knowledge base that cannot be read stops the run (Candin,
|
|
15
|
+
// 2026-10-13): with no token, a refusal or a failed search, the gathering
|
|
16
|
+
// throws a KnowledgeAccessError naming the documents, and the agent answers
|
|
17
|
+
// with it rather than writing as if the content cited nothing. A search that
|
|
18
|
+
// works and finds nothing is not an error.
|
|
19
|
+
//
|
|
20
|
+
// A reference is cited by its short id `r<N>`, N being its index in the
|
|
21
|
+
// supplied list, whatever its kind.
|
|
22
|
+
//
|
|
23
|
+
import logger from "./logger.js";
|
|
24
|
+
import { STACKFACTOR_REFERENCE, retrievePassages, sourcesFromReferences, } from "./stackfactorKnowledge.js";
|
|
25
|
+
/** How many passages one search asks for when the caller does not say. */
|
|
26
|
+
const DEFAULT_REFERENCE_TOP_K = 5;
|
|
27
|
+
/** How many passages a gathering keeps when the caller does not say: the best-ranked. */
|
|
28
|
+
const DEFAULT_MAX_PASSAGES = 40;
|
|
29
|
+
/** The prompt key the passages are handed to the model under. */
|
|
30
|
+
export const REFERENCE_PASSAGES_KEY = "Knowledge Base Passages";
|
|
31
|
+
const text = (value) => (typeof value === "string" ? value.trim() : "");
|
|
32
|
+
/**
|
|
33
|
+
* The knowledge base documents a run's references cite could not be read: the
|
|
34
|
+
* run has no token, the person may not open a cited knowledge base (401, 403,
|
|
35
|
+
* 404), or its search failed. The agent answers with it — `code` and `message`
|
|
36
|
+
* are what the person sees — and generates nothing.
|
|
37
|
+
*/
|
|
38
|
+
export class KnowledgeAccessError extends Error {
|
|
39
|
+
/** 403 when every skipped knowledge base refused the person (or there was no token), else 502. */
|
|
40
|
+
code;
|
|
41
|
+
/** The knowledge bases that could not be searched, and why; empty when there was no token. */
|
|
42
|
+
skipped;
|
|
43
|
+
/** The titles of the STACKFACTOR references citing those knowledge bases, each once. */
|
|
44
|
+
documents;
|
|
45
|
+
/**
|
|
46
|
+
* @param details `skipped` (from the search) and `documents` (titles); the
|
|
47
|
+
* code and the message follow from them
|
|
48
|
+
*/
|
|
49
|
+
constructor(details = {}) {
|
|
50
|
+
const skipped = Array.isArray(details.skipped) ? details.skipped : [];
|
|
51
|
+
const documents = [...new Set((Array.isArray(details.documents) ? details.documents : []).map(text).filter(Boolean))];
|
|
52
|
+
const code = skipped.every((source) => source?.noAccess) ? 403 : 502;
|
|
53
|
+
const titles = documents.map((title) => `"${title}"`).join(", ");
|
|
54
|
+
super(code === 403
|
|
55
|
+
? `The knowledge base documents this content cites could not be read with your access${titles ? `: ${titles}` : ""}.`
|
|
56
|
+
: `The knowledge base documents this content cites could not be read: the knowledge base search failed${titles ? ` for ${titles}` : ""}.`);
|
|
57
|
+
this.name = "KnowledgeAccessError";
|
|
58
|
+
this.code = code;
|
|
59
|
+
this.skipped = skipped;
|
|
60
|
+
this.documents = documents;
|
|
61
|
+
Object.setPrototypeOf(this, KnowledgeAccessError.prototype);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const id = (value) => typeof value === "string" ? value.trim() : value != null && typeof value === "object" ? String(value) : "";
|
|
65
|
+
const shortId = (index) => `r${index}`;
|
|
66
|
+
/** The document a STACKFACTOR reference cites, or "" when it cites none. */
|
|
67
|
+
const documentOf = (reference) => {
|
|
68
|
+
const target = reference.target ?? {};
|
|
69
|
+
return (id(reference.knowledgeBase?.documentId) ||
|
|
70
|
+
(target.elementType === STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE ? id(target.elementId) : ""));
|
|
71
|
+
};
|
|
72
|
+
/** The knowledge base a STACKFACTOR reference's document is in, or "" when it cites no document. */
|
|
73
|
+
const knowledgeBaseOf = (reference) => {
|
|
74
|
+
const target = reference.target ?? {};
|
|
75
|
+
return (id(reference.knowledgeBase?.knowledgeBaseId) ||
|
|
76
|
+
(target.elementType === STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE ? id(target.parentId) : ""));
|
|
77
|
+
};
|
|
78
|
+
/** A cap the caller passed (a whole number, 0 or more), else `fallback`. */
|
|
79
|
+
const capOf = (value, fallback) => typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : fallback;
|
|
80
|
+
/** The kinds the platform retired; it reads them as PUBLICATION. */
|
|
81
|
+
const RETIRED_KINDS = new Set(["DOCUMENT", "KNOWLEDGE_BASE", "NOTE"]);
|
|
82
|
+
/** A reference's kind as the platform reads it: missing or retired is PUBLICATION. */
|
|
83
|
+
const resolveKind = (reference) => {
|
|
84
|
+
const kind = reference?.kind;
|
|
85
|
+
return typeof kind !== "string" || kind === "" || RETIRED_KINDS.has(kind) ? "PUBLICATION" : kind;
|
|
86
|
+
};
|
|
87
|
+
/** The pages among the references, each url once, under the first reference that has it. */
|
|
88
|
+
const pagesOf = (references) => {
|
|
89
|
+
const pages = [];
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
references.forEach((reference, index) => {
|
|
92
|
+
if (!reference || typeof reference !== "object")
|
|
93
|
+
return;
|
|
94
|
+
const kind = resolveKind(reference);
|
|
95
|
+
if (kind !== "WEB" && kind !== "PUBLICATION")
|
|
96
|
+
return;
|
|
97
|
+
const url = text(reference.url);
|
|
98
|
+
if (!/^https?:\/\//i.test(url) || seen.has(url))
|
|
99
|
+
return;
|
|
100
|
+
seen.add(url);
|
|
101
|
+
pages.push({ id: shortId(index), title: text(reference.title) || url, url });
|
|
102
|
+
});
|
|
103
|
+
return pages;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* The short ids of the STACKFACTOR references citing each document, and of
|
|
107
|
+
* those citing each chunk (`knowledgeBase.chunkId`) of a document.
|
|
108
|
+
*/
|
|
109
|
+
const citersOf = (references) => {
|
|
110
|
+
const byDocument = new Map();
|
|
111
|
+
const byChunk = new Map();
|
|
112
|
+
references.forEach((reference, index) => {
|
|
113
|
+
if (reference?.kind !== STACKFACTOR_REFERENCE.KIND)
|
|
114
|
+
return;
|
|
115
|
+
const documentId = documentOf(reference);
|
|
116
|
+
if (!documentId)
|
|
117
|
+
return;
|
|
118
|
+
byDocument.set(documentId, [...(byDocument.get(documentId) ?? []), shortId(index)]);
|
|
119
|
+
const chunkId = id(reference.knowledgeBase?.chunkId);
|
|
120
|
+
if (chunkId)
|
|
121
|
+
byChunk.set(chunkId, [...(byChunk.get(chunkId) ?? []), shortId(index)]);
|
|
122
|
+
});
|
|
123
|
+
return { byChunk, byDocument };
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* The error for cited knowledge bases that could not be read: it names the
|
|
127
|
+
* documents of the STACKFACTOR references citing them.
|
|
128
|
+
*/
|
|
129
|
+
const accessError = (references, unread, skipped) => new KnowledgeAccessError({
|
|
130
|
+
documents: references
|
|
131
|
+
.filter((reference) => reference?.kind === STACKFACTOR_REFERENCE.KIND && documentOf(reference) && unread.has(knowledgeBaseOf(reference)))
|
|
132
|
+
.map((reference) => reference.title),
|
|
133
|
+
skipped,
|
|
134
|
+
});
|
|
135
|
+
/**
|
|
136
|
+
* What a list of references points at: its pages, and the passages of the
|
|
137
|
+
* documents its STACKFACTOR references cite. One search per knowledge base per
|
|
138
|
+
* query (per batch of 500 documents), however many references there are.
|
|
139
|
+
*
|
|
140
|
+
* References that cite a knowledge base the run cannot read stop it: with no
|
|
141
|
+
* token, or when any cited knowledge base is skipped (refused or failed), it
|
|
142
|
+
* throws a KnowledgeAccessError naming the documents. A search that works and
|
|
143
|
+
* finds nothing is not an error. With no knowledge cited, no token is needed.
|
|
144
|
+
*
|
|
145
|
+
* A passage is attributed to the references citing its chunk, else to every
|
|
146
|
+
* reference citing its document. The best-ranked `maxPassages` are kept, and
|
|
147
|
+
* the first `maxPages` pages in reference order.
|
|
148
|
+
* @param options `references` (a skill's, a microskill's…), `queries` to
|
|
149
|
+
* search the cited documents with, `search` (client-api
|
|
150
|
+
* `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK` per
|
|
151
|
+
* search (default 5), `maxPassages` (default 40), `maxPages` (default: all)
|
|
152
|
+
* @returns `{ pages, passages, skipped, citesKnowledge }`; `skipped` is always
|
|
153
|
+
* empty, since a skip throws
|
|
154
|
+
* @throws KnowledgeAccessError When cited knowledge bases cannot be read
|
|
155
|
+
*/
|
|
156
|
+
export const gatherReferenceContext = async (options) => {
|
|
157
|
+
const references = Array.isArray(options.references) ? options.references : [];
|
|
158
|
+
const sources = sourcesFromReferences(references);
|
|
159
|
+
const citesKnowledge = sources.length > 0;
|
|
160
|
+
const pages = pagesOf(references).slice(0, capOf(options.maxPages, Infinity));
|
|
161
|
+
if (!citesKnowledge) {
|
|
162
|
+
logContext(pages.length, 0, []);
|
|
163
|
+
return { citesKnowledge, pages, passages: [], skipped: [] };
|
|
164
|
+
}
|
|
165
|
+
if (!options.token) {
|
|
166
|
+
const error = accessError(references, new Set(sources.map((source) => source.knowledgeBaseId)), []);
|
|
167
|
+
logContext(pages.length, 0, [], { error, noToken: true });
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
const found = await retrievePassages({
|
|
171
|
+
queries: options.queries,
|
|
172
|
+
search: options.search,
|
|
173
|
+
sources,
|
|
174
|
+
token: options.token,
|
|
175
|
+
topK: options.topK ?? DEFAULT_REFERENCE_TOP_K,
|
|
176
|
+
});
|
|
177
|
+
if (found.skipped.length > 0) {
|
|
178
|
+
const unread = new Set(found.skipped.map((source) => source.knowledgeBaseId));
|
|
179
|
+
const error = accessError(references, unread, found.skipped);
|
|
180
|
+
logContext(pages.length, 0, found.skipped, { error });
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
const citers = citersOf(references);
|
|
184
|
+
const passages = found.passages
|
|
185
|
+
.filter((passage) => citers.byDocument.has(passage.documentId))
|
|
186
|
+
.slice(0, capOf(options.maxPassages, DEFAULT_MAX_PASSAGES))
|
|
187
|
+
.map((passage) => ({
|
|
188
|
+
chunkId: passage.chunkId,
|
|
189
|
+
document: passage.documentName,
|
|
190
|
+
documentId: passage.documentId,
|
|
191
|
+
knowledgeBaseId: passage.knowledgeBaseId,
|
|
192
|
+
references: citers.byChunk.get(passage.chunkId) ?? citers.byDocument.get(passage.documentId),
|
|
193
|
+
section: passage.sectionTitle,
|
|
194
|
+
text: passage.text,
|
|
195
|
+
}));
|
|
196
|
+
logContext(pages.length, passages.length, []);
|
|
197
|
+
return { citesKnowledge, pages, passages, skipped: [] };
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* The one summary line of a gathering, for the logs: a warning when it was
|
|
201
|
+
* refused. Document titles stay out of the logs.
|
|
202
|
+
*/
|
|
203
|
+
const logContext = (pages, passages, skipped, refusal) => {
|
|
204
|
+
const parts = [`${pages} page${pages === 1 ? "" : "s"}`, `${passages} passage${passages === 1 ? "" : "s"}`];
|
|
205
|
+
if (skipped.length > 0) {
|
|
206
|
+
parts.push(`skipped knowledge bases: ${skipped.map((s) => `${s.knowledgeBaseId} (noAccess: ${s.noAccess})`).join(", ")}`);
|
|
207
|
+
}
|
|
208
|
+
if (refusal?.noToken)
|
|
209
|
+
parts.push("no token");
|
|
210
|
+
if (refusal) {
|
|
211
|
+
const count = refusal.error.documents.length;
|
|
212
|
+
parts.push(`refused (${refusal.error.code}): ${count} document${count === 1 ? "" : "s"} unreadable`);
|
|
213
|
+
}
|
|
214
|
+
logger.log(null, refusal ? logger.levels.warn : logger.levels.info, `Reference context: ${parts.join("; ")}`);
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* The passages as a prompt shows them: who cites each, where it came from, and
|
|
218
|
+
* its text. The ids that locate it in the platform are left out.
|
|
219
|
+
* @param context From `gatherReferenceContext`
|
|
220
|
+
* @returns `[{ references, document, section, text }]`, for the model's input
|
|
221
|
+
*/
|
|
222
|
+
export const referencePassagesForPrompt = (context) => context.passages.map((passage) => ({
|
|
223
|
+
document: passage.document,
|
|
224
|
+
references: passage.references,
|
|
225
|
+
section: passage.section,
|
|
226
|
+
text: passage.text,
|
|
227
|
+
}));
|
|
228
|
+
/**
|
|
229
|
+
* The tools a run is given. Never `search`, `map` or `crawl`: a run reads what
|
|
230
|
+
* its references point at and discovers nothing. `extract` only, only when
|
|
231
|
+
* there are pages to open, and kept to those pages: any other url the model
|
|
232
|
+
* asks for is refused (`allowedUrls`).
|
|
233
|
+
* @param context From `gatherReferenceContext`
|
|
234
|
+
* @returns `{ tavily: { tools: ["extract"], allowedUrls } }` with pages, else
|
|
235
|
+
* `{}`. With no pages it returns `{}`, so a caller spreading it over a config
|
|
236
|
+
* that already has a `tavily` key keeps that key (and `tavily: true` means
|
|
237
|
+
* search plus extract): callers must not carry a base `tavily` key.
|
|
238
|
+
*/
|
|
239
|
+
export const toolsFor = (context) => context.pages.length > 0
|
|
240
|
+
? { tavily: { tools: ["extract"], allowedUrls: context.pages.map((page) => page.url) } }
|
|
241
|
+
: {};
|
|
242
|
+
/**
|
|
243
|
+
* The stage prompt followed by the guidance for each kind of source the run
|
|
244
|
+
* has: pages, then passages; when it has neither (knowledge cited and readable
|
|
245
|
+
* but no passage found included), the `none` guidance. An empty guidance adds
|
|
246
|
+
* nothing.
|
|
247
|
+
* @param stagePrompt The stage's own prompt
|
|
248
|
+
* @param context From `gatherReferenceContext`
|
|
249
|
+
* @param guidance From `sourceGuidanceFrom`
|
|
250
|
+
* @returns The sections joined with a blank line
|
|
251
|
+
*/
|
|
252
|
+
export const composeSourceInstructions = (stagePrompt, context, guidance) => {
|
|
253
|
+
const sections = [];
|
|
254
|
+
if (context.pages.length > 0)
|
|
255
|
+
sections.push(guidance.pages);
|
|
256
|
+
if (context.passages.length > 0)
|
|
257
|
+
sections.push(guidance.passages);
|
|
258
|
+
if (sections.length === 0)
|
|
259
|
+
sections.push(guidance.none);
|
|
260
|
+
return [stagePrompt, ...sections].filter(Boolean).join("\n\n");
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* A reference as a model is shown it. A STACKFACTOR reference loses its pointer
|
|
264
|
+
* and its url (ids mean nothing to a model, and the url is behind the
|
|
265
|
+
* organization's own sign-in); the model cites it by short id.
|
|
266
|
+
* @param reference A supplied reference
|
|
267
|
+
* @returns The reference; a STACKFACTOR one without `_id`, `origin`, `visibility`,
|
|
268
|
+
* `target`, `knowledgeBase` and `url`
|
|
269
|
+
*/
|
|
270
|
+
export const referenceForModel = (reference) => {
|
|
271
|
+
if (reference?.kind !== STACKFACTOR_REFERENCE.KIND)
|
|
272
|
+
return reference;
|
|
273
|
+
const { _id: _i, origin: _o, visibility: _v, target: _target, knowledgeBase: _knowledgeBase, url: _url, ...rest } = reference;
|
|
274
|
+
return rest;
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* The supplied references as a model is shown them, each under its short id
|
|
278
|
+
* `r<N>` (N the index in the supplied list) as `_id`, set last so a stored
|
|
279
|
+
* reference's own id never wins. Non-object entries are skipped but keep their
|
|
280
|
+
* index, so the ids stay aligned with `citedReferences`.
|
|
281
|
+
* @param references The supplied list
|
|
282
|
+
*/
|
|
283
|
+
export const referencesForModel = (references) => {
|
|
284
|
+
const shown = [];
|
|
285
|
+
(Array.isArray(references) ? references : []).forEach((reference, index) => {
|
|
286
|
+
if (!reference || typeof reference !== "object")
|
|
287
|
+
return;
|
|
288
|
+
const { _id: _ignored, ...rest } = referenceForModel(reference);
|
|
289
|
+
shown.push({ ...rest, _id: shortId(index) });
|
|
290
|
+
});
|
|
291
|
+
return shown;
|
|
292
|
+
};
|
|
293
|
+
/** What a STACKFACTOR reference points at, as the platform stores it. */
|
|
294
|
+
const stackfactorPointers = (reference) => {
|
|
295
|
+
if (reference.kind !== STACKFACTOR_REFERENCE.KIND)
|
|
296
|
+
return {};
|
|
297
|
+
const pointer = reference.knowledgeBase;
|
|
298
|
+
if (pointer?.knowledgeBaseId && pointer?.documentId) {
|
|
299
|
+
return {
|
|
300
|
+
knowledgeBase: { ...pointer },
|
|
301
|
+
target: {
|
|
302
|
+
elementType: STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE,
|
|
303
|
+
elementId: pointer.documentId,
|
|
304
|
+
parentId: pointer.knowledgeBaseId,
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
return reference.target?.elementType && reference.target?.elementId ? { target: { ...reference.target } } : {};
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* A reference from the flat projection the host sends (or the flat shape a
|
|
312
|
+
* model emits) in the shape the platform stores: nested `publication`,
|
|
313
|
+
* `relatedTo` folded into `description`, a STACKFACTOR reference's pointer and
|
|
314
|
+
* target kept. `visibility` is never set; the caller decides it.
|
|
315
|
+
* @param reference The flat reference
|
|
316
|
+
* @param origin Default `"AI_GENERATED"`
|
|
317
|
+
*/
|
|
318
|
+
export const toStoredReference = (reference, origin = "AI_GENERATED") => {
|
|
319
|
+
const publication = {
|
|
320
|
+
...(reference.author ? { author: reference.author } : {}),
|
|
321
|
+
...(reference.publisher ? { publisher: reference.publisher } : {}),
|
|
322
|
+
...(reference.identifier ? { identifier: reference.identifier } : {}),
|
|
323
|
+
...(reference.locator ? { locator: reference.locator } : {}),
|
|
324
|
+
...(reference.publication || {}),
|
|
325
|
+
};
|
|
326
|
+
const description = [reference.description, reference.relatedTo ? `Related to: ${reference.relatedTo}` : null]
|
|
327
|
+
.filter(Boolean)
|
|
328
|
+
.join(" \u2014 ");
|
|
329
|
+
return {
|
|
330
|
+
kind: reference.kind,
|
|
331
|
+
title: reference.title,
|
|
332
|
+
...(description ? { description } : {}),
|
|
333
|
+
...(reference.url ? { url: reference.url } : {}),
|
|
334
|
+
...(Object.keys(publication).length > 0 ? { publication } : {}),
|
|
335
|
+
...stackfactorPointers(reference),
|
|
336
|
+
origin,
|
|
337
|
+
};
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* The supplied references a model cited by short id, stored. Ids are matched
|
|
341
|
+
* trimmed and case-insensitively; unknown and non-string ids are dropped and
|
|
342
|
+
* each reference appears once, at its first citation.
|
|
343
|
+
* @param ids The model's cited ids (`["r0", "r3"]`)
|
|
344
|
+
* @param supplied The list the ids index into
|
|
345
|
+
* @param origin Default `"AI_GENERATED"`
|
|
346
|
+
*/
|
|
347
|
+
export const citedReferences = (ids, supplied, origin) => {
|
|
348
|
+
const list = Array.isArray(supplied) ? supplied : [];
|
|
349
|
+
const taken = new Set();
|
|
350
|
+
const cited = [];
|
|
351
|
+
for (const raw of Array.isArray(ids) ? ids : []) {
|
|
352
|
+
const match = typeof raw === "string" ? /^r(\d+)$/.exec(raw.trim().toLowerCase()) : null;
|
|
353
|
+
const index = match ? Number(match[1]) : -1;
|
|
354
|
+
if (!list[index] || typeof list[index] !== "object" || taken.has(index))
|
|
355
|
+
continue;
|
|
356
|
+
taken.add(index);
|
|
357
|
+
cited.push(toStoredReference(list[index], origin));
|
|
358
|
+
}
|
|
359
|
+
return cited;
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* The three guidance texts an agent's config holds. A missing or non-string
|
|
363
|
+
* value is an empty string.
|
|
364
|
+
* @param config The agent's config
|
|
365
|
+
*/
|
|
366
|
+
export const sourceGuidanceFrom = (config) => ({
|
|
367
|
+
pages: text(config?.referencedPagesGuidance),
|
|
368
|
+
passages: text(config?.knowledgeBasePassagesGuidance),
|
|
369
|
+
none: text(config?.noReferencesGuidance),
|
|
370
|
+
});
|
|
371
|
+
export default {
|
|
372
|
+
KnowledgeAccessError,
|
|
373
|
+
REFERENCE_PASSAGES_KEY,
|
|
374
|
+
citedReferences,
|
|
375
|
+
composeSourceInstructions,
|
|
376
|
+
gatherReferenceContext,
|
|
377
|
+
referenceForModel,
|
|
378
|
+
referencePassagesForPrompt,
|
|
379
|
+
referencesForModel,
|
|
380
|
+
sourceGuidanceFrom,
|
|
381
|
+
toSto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IAAI,CA8Id,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,KAAK,GAChB,MAAM,SAAS,EACf,UAAS,YAAiB,KACzB,IAAI,CAAC,MAyOP,CAAC"}
|
package/dist/esm/serve.js
CHANGED
|
@@ -339,8 +339,15 @@ const safeParse = (value, fallback) => {
|
|
|
339
339
|
/**
|
|
340
340
|
* Agents that fail throw `new Error(JSON.stringify({ code, message }))`. Unwrap
|
|
341
341
|
* that back into a structured gRPC error frame; fall back to a 500 otherwise.
|
|
342
|
+
* A KnowledgeAccessError an agent did not catch (referenceContext) answers
|
|
343
|
+
* with its own code (403, 502) and message, which names the documents that
|
|
344
|
+
* could not be read. It is known by its name, so one thrown by another copy of
|
|
345
|
+
* this package in the agent's dependencies is known too.
|
|
342
346
|
*/
|
|
343
347
|
const toErrorFrame = (error) => {
|
|
348
|
+
if (error?.name === "KnowledgeAccessError" && typeof error.code === "number") {
|
|
349
|
+
return { code: error.code, message: String(error.message ?? "") };
|
|
350
|
+
}
|
|
344
351
|
try {
|
|
345
352
|
const parsed = JSON.parse(error?.message);
|
|
346
353
|
if (parsed && typeof parsed.code === "number") {
|
|
@@ -67,7 +67,10 @@ export interface SkippedSource {
|
|
|
67
67
|
knowledgeBaseId: string;
|
|
68
68
|
/** The HTTP status, when the platform answered one. */
|
|
69
69
|
status: number | null;
|
|
70
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* True when the platform refused the person (401, 403) or does not have the
|
|
72
|
+
* knowledge base (404); false when the search failed otherwise.
|
|
73
|
+
*/
|
|
71
74
|
noAccess: boolean;
|
|
72
75
|
message: string;
|
|
73
76
|
}
|
|
@@ -116,9 +119,11 @@ export declare const sourcesFromCoverage: (coverage: unknown) => KnowledgeSource
|
|
|
116
119
|
* model to cite. A source with documents is searched within them (in batches of
|
|
117
120
|
* SEARCH_DOCUMENT_IDS_MAX); one without, whole. A source the person may not
|
|
118
121
|
* read (401, 403, 404) is skipped: none of its passages are used and no more of
|
|
119
|
-
* its searches are made. A search that fails otherwise is
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
+
* its searches are made. A search that fails otherwise is tried once more; when
|
|
123
|
+
* that fails too it is reported and the passages its other searches found
|
|
124
|
+
* stand. A refusal is never tried again. Either way it is in `skipped`, once
|
|
125
|
+
* per knowledge base, and the passages of the others stand; whether a skip is
|
|
126
|
+
* an error is the caller's to say.
|
|
122
127
|
* @param options `sources`, `queries`, `search` (client-api
|
|
123
128
|
* `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK`
|
|
124
129
|
* per search (default DEFAULT_TOP_K)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stackfactorKnowledge.d.ts","sourceRoot":"","sources":["../../src/stackfactorKnowledge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stackfactorKnowledge.d.ts","sourceRoot":"","sources":["../../src/stackfactorKnowledge.ts"],"names":[],"mappings":"AAmCA,kGAAkG;AAClG,eAAO,MAAM,qBAAqB;;;;;EAKhC,CAAC;AAEH,0FAA0F;AAC1F,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAE3C,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,OAAO,CAAC;AAErC,0EAA0E;AAC1E,eAAO,MAAM,aAAa,IAAI,CAAC;AAW/B,iFAAiF;AACjF,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,iGAAiG;IACjG,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,M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0FrE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAC5B,UAAU,gBAAgB,EAAE,KAC3B;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAM5E,CAAC;AAEN;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,GACxB,UAAU,gBAAgB,EAAE,EAC5B,OAAO,OAAO,EAAE,KACf,oBAAoB,EA6BtB,CAAC;;;;;;;;;;;kCAlDU,gBAAgB,EAAE,KAC3B;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE;8BAkBvE,gBAAgB,EAAE,SACrB,OAAO,EAAE,KACf,oBAAoB,EAAE;gCA7HuB;QAC9C,OAAO,EAAE,eAAe,EAAE,CAAC;QAC3B,OAAO,EAAE,MAAM,EAAE,CAAC;QAClB,MAAM,EAAE,eAAe,CAAC;QACxB,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,KAAG,OAAO,CAAC;QAAE,QAAQ,EAAE,gBAAgB,EAAE,CAAC;QAAC,OAAO,EAAE,aAAa,EAAE,CAAA;KAAE,CAAC;oCAjEzB,OAAO,KAAG,eAAe,EAAE;wCA5BvB,OAAO,KAAG,eAAe,EAAE;;AAmP7E,wBAUE"}
|
|
@@ -14,8 +14,11 @@
|
|
|
14
14
|
// microskill;
|
|
15
15
|
// - access: the platform's search judges the person the run acts for, so a
|
|
16
16
|
// run reads only what that person may open. A knowledge base the person
|
|
17
|
-
// cannot read is skipped and reported
|
|
18
|
-
// generates without it.
|
|
17
|
+
// cannot read is skipped and reported (`retrievePassages`): grounding from
|
|
18
|
+
// skill coverage generates without it. A knowledge base that references
|
|
19
|
+
// cite is another matter (Candin, 2026-10-13): the references reader
|
|
20
|
+
// (referenceContext `gatherReferenceContext`) stops the run with a
|
|
21
|
+
// KnowledgeAccessError instead.
|
|
19
22
|
//
|
|
20
23
|
// A reference points at the content and never copies it: the passage is
|
|
21
24
|
// searched again, under the reader's own access, whenever it is used. Its
|
|
@@ -46,6 +49,8 @@ export const DEFAULT_TOP_K = 8;
|
|
|
46
49
|
const CONCURRENCY = 4;
|
|
47
50
|
/** Statuses that mean the person may not read the knowledge base, or it is gone. */
|
|
48
51
|
const NO_ACCESS = new Set([401, 403, 404]);
|
|
52
|
+
/** How long a search that failed for another reason waits before it is tried once more. */
|
|
53
|
+
const RETRY_DELAY_MS = 200;
|
|
49
54
|
const text = (value) => (typeof value === "string" ? value.trim() : "");
|
|
50
55
|
/** An http(s) address, or null: a reference's url lands in an `href`. */
|
|
51
56
|
const webUrl = (value) => (/^https?:\/\//i.test(text(value)) ? text(value) : null);
|
|
@@ -125,15 +130,18 @@ const statusOf = (error) => {
|
|
|
125
130
|
const status = error?.response?.status ?? error?.status;
|
|
126
131
|
return typeof status === "number" ? status : null;
|
|
127
132
|
};
|
|
133
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
128
134
|
/**
|
|
129
135
|
* Searches every source for every query, under the person's access, and hands
|
|
130
136
|
* back the passages found, best first, each once, tagged "k1", "k2"… for the
|
|
131
137
|
* model to cite. A source with documents is searched within them (in batches of
|
|
132
138
|
* SEARCH_DOCUMENT_IDS_MAX); one without, whole. A source the person may not
|
|
133
139
|
* read (401, 403, 404) is skipped: none of its passages are used and no more of
|
|
134
|
-
* its searches are made. A search that fails otherwise is
|
|
135
|
-
*
|
|
136
|
-
*
|
|
140
|
+
* its searches are made. A search that fails otherwise is tried once more; when
|
|
141
|
+
* that fails too it is reported and the passages its other searches found
|
|
142
|
+
* stand. A refusal is never tried again. Either way it is in `skipped`, once
|
|
143
|
+
* per knowledge base, and the passages of the others stand; whether a skip is
|
|
144
|
+
* an error is the caller's to say.
|
|
137
145
|
* @param options `sources`, `queries`, `search` (client-api
|
|
138
146
|
* `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK`
|
|
139
147
|
* per search (default DEFAULT_TOP_K)
|
|
@@ -162,8 +170,24 @@ export const retrievePassages = async (options) => {
|
|
|
162
170
|
await inPool(searches, CONCURRENCY, async ({ source, query, documentIds, order }) => {
|
|
163
171
|
if (skipped.get(source.knowledgeBaseId)?.noAccess)
|
|
164
172
|
return;
|
|
173
|
+
const searchOnce = () => options.search(source.knowledgeBaseId, query, documentIds ? { topK, documentIds } : { topK }, options.token);
|
|
165
174
|
try {
|
|
166
|
-
|
|
175
|
+
let answer;
|
|
176
|
+
try {
|
|
177
|
+
answer = await searchOnce();
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
// A refusal is the platform's answer and is not asked again. Any other
|
|
181
|
+
// failure may pass (a dropped connection, a 5xx), and one failed search
|
|
182
|
+
// would skip its whole knowledge base, so it is tried once more —
|
|
183
|
+
// unless the base refused the person meanwhile.
|
|
184
|
+
if (NO_ACCESS.has(statusOf(error)))
|
|
185
|
+
throw error;
|
|
186
|
+
await delay(RETRY_DELAY_MS);
|
|
187
|
+
if (skipped.get(source.knowledgeBaseId)?.noAccess)
|
|
188
|
+
return;
|
|
189
|
+
answer = await searchOnce();
|
|
190
|
+
}
|
|
167
191
|
(Array.isArray(answer?.chunks) ? answer.chunks : []).forEach((chunk, rank) => {
|
|
168
192
|
const chunkId = id(chunk?._id);
|
|
169
193
|
if (!chunkId || !id(chunk?.documentId))
|
package/dist/esm/tavily.d.ts
CHANGED
|
@@ -46,6 +46,14 @@ export type TavilyConfig = {
|
|
|
46
46
|
* for the tabular documents this is usually pointed at.
|
|
47
47
|
*/
|
|
48
48
|
format?: "markdown" | "text";
|
|
49
|
+
/**
|
|
50
|
+
* The only URLs `web_extract` may read (matched trimmed, without case or a
|
|
51
|
+
* trailing slash, and sent as listed). Any other URL is never sent to
|
|
52
|
+
* Tavily: the tool answers the model with an error line for it and never
|
|
53
|
+
* throws. Unset, any URL may be read. Only `web_extract` reads it, so a run
|
|
54
|
+
* kept to its pages enables `extract` alone (referenceContext `toolsFor`).
|
|
55
|
+
*/
|
|
56
|
+
allowedUrls?: string[];
|
|
49
57
|
};
|
|
50
58
|
/**
|
|
51
59
|
* Normalizes `config.tavily` into an options object, returning `null` when the
|
package/dist/esm/tavily.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tavily.d.ts","sourceRoot":"","sources":["../../src/tavily.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AA0BnD,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,SAAS,GAAG,OAAO,GAAG,KAAK,CAAC;AAEpE;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,cAAc,EAAE,CAAC;IACzB,iEAAiE;IACjE,WAAW,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,MAAM,GAAG,YAAY,CAAC;IAC3D,kFAAkF;IAClF,YAAY,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IACpC,2CAA2C;IAC3C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,oEAAoE;IACpE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,4CAA4C;IAC5C,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,6CAA6C;IAC7C,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;OAIG;IACH,MAAM,CAAC,EAAE,UAAU,GAAG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