@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
package/README.md
CHANGED
|
@@ -392,6 +392,7 @@ const result = await langChain.runPromptWithModel(
|
|
|
392
392
|
| `excludeDomains` | — | Exclude these domains from `web_search` |
|
|
393
393
|
| `crawlLimit` | `10` | Maximum pages one `web_crawl` or `web_map` call may return |
|
|
394
394
|
| `format` | `"markdown"` | `"markdown"` preserves table and heading structure at fewer tokens than the prose equivalent |
|
|
395
|
+
| `allowedUrls` | — | The only URLs `web_extract` may read; any other gets an error line in the tool result and is never sent to Tavily |
|
|
395
396
|
|
|
396
397
|
### Context management
|
|
397
398
|
|
|
@@ -416,6 +417,7 @@ A model that cannot tell it received half a regulation will reason over the half
|
|
|
416
417
|
- Works with every supported provider.
|
|
417
418
|
- Credits come from each response's own `usage` block rather than an estimate. They are reported to the platform as `credits` of the model `tavily` (see [Usage](#usage)) and counted on `usageTracker.tokens.tavilyCredits`.
|
|
418
419
|
- URLs are collected on `usageTracker.webSearchSources`, deduplicated across the whole run — Tavily's terms require citing the original sources when its content is shown to end users.
|
|
420
|
+
- Every URL `web_extract` asked Tavily for is on `usageTracker.attemptedUrls`, each once, whether Tavily could read it or not; a URL `allowedUrls` refused was never asked for and is not there. `webSearchSources` holds what Tavily answered, under the address it answered with; `attemptedUrls` is what the run tried to read, so a caller can check a model's claim that it read a page.
|
|
419
421
|
- Per-URL failures are surfaced in the tool result instead of being dropped, so a fetch failure is not mistaken for an absence of content.
|
|
420
422
|
- Tool calls honour the caller's abort signal, so a cancelled run stops paying for in-flight crawls.
|
|
421
423
|
|
|
@@ -436,6 +438,28 @@ const kept = references.filter((r) => !r.url || live.has(r.url));
|
|
|
436
438
|
|
|
437
439
|
---
|
|
438
440
|
|
|
441
|
+
## Reading references (`referenceContext`)
|
|
442
|
+
|
|
443
|
+
An agent reading a skill's (or a microskill's, a question's) references gathers what they point at once, with `referenceContext.gatherReferenceContext({ references, queries, search, token, topK?, maxPassages?, maxPages? })`:
|
|
444
|
+
|
|
445
|
+
- **pages**: WEB and PUBLICATION references with an http(s) url, each `{ id: "r<N>", title, url }`, opened by the model through Tavily `extract`. All of them by default; `maxPages` keeps the first N in reference order.
|
|
446
|
+
- **passages**: the passages of the knowledge base documents STACKFACTOR references cite, searched under the run's token (client-api `knowledgeBaseAgent.search`). A passage names the references citing its chunk (`knowledgeBase.chunkId`); when none does, every reference citing its document. The best-ranked 40 are kept (`maxPassages`).
|
|
447
|
+
|
|
448
|
+
**A cited knowledge base that cannot be read stops the run.** With no token, or when the person may not open a cited knowledge base (401, 403, 404) or its search fails, it throws `KnowledgeAccessError` (exported from the package and from `referenceContext`). A search that fails for any reason other than access is tried once more, 200 ms later, before it counts as failed; a refusal is not asked again.
|
|
449
|
+
|
|
450
|
+
| Field | Value |
|
|
451
|
+
| ----------- | -------------------------------------------------------------------------------------------- |
|
|
452
|
+
| `code` | `403` when every skipped knowledge base refused the person (or there was no token), else `502` |
|
|
453
|
+
| `documents` | The titles of the STACKFACTOR references citing those knowledge bases, each once |
|
|
454
|
+
| `skipped` | The knowledge bases that could not be searched, and why |
|
|
455
|
+
| `message` | `The knowledge base documents this content cites could not be read with your access: "A", "B".` (502: `…could not be read: the knowledge base search failed for "A".`) |
|
|
456
|
+
|
|
457
|
+
The agent answers with `code` and `message`, so the person sees which documents could not be read. An agent that does not catch it answers the same: `serve` and `runOnce` turn an uncaught `KnowledgeAccessError` (known by its `name`, so a second copy of this package counts too) into an error with its own `code` and `message`, not a 500. A search that works and finds nothing is not an error: the run gets no passages and the `none` guidance. Grounding from skill coverage (`stackfactorKnowledge.retrievePassages`) still skips a knowledge base it cannot read.
|
|
458
|
+
|
|
459
|
+
`referenceContext.toolsFor(context)` gives the run its tools: `{ tavily: { tools: ["extract"], allowedUrls } }` when there are pages (their urls), else `{}`. Spread it after `tavily: undefined`, so `web_extract` reads only the referenced pages.
|
|
460
|
+
|
|
461
|
+
---
|
|
462
|
+
|
|
439
463
|
## Supported Models
|
|
440
464
|
|
|
441
465
|
### Text / Chat
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { callAgent, checkAgent } from "./client.js";
|
|
|
7
7
|
import * as runtimeContext from "./runtimeContext.js";
|
|
8
8
|
import { recordUsage, USAGE_UNITS } from "./usage.js";
|
|
9
9
|
import stackfactorKnowledge from "./stackfactorKnowledge.js";
|
|
10
|
+
import referenceContext, { KnowledgeAccessError } from "./referenceContext.js";
|
|
10
11
|
import { checkUrlsReachable } from "./tavily.js";
|
|
11
12
|
export { constants };
|
|
12
13
|
export { errorHandling, AppError };
|
|
@@ -23,6 +24,7 @@ export type { AgentExecuteRequest, CallAgentOptions, CheckAgentOptions, AgentHea
|
|
|
23
24
|
export { runtimeContext };
|
|
24
25
|
export { recordUsage, USAGE_UNITS };
|
|
25
26
|
export type { UsageEntry, UsageUnit } from "./usage.js";
|
|
26
|
-
export { stackfactorKnowledge };
|
|
27
|
+
export { stackfactorKnowledge, referenceContext, KnowledgeAccessError };
|
|
27
28
|
export type { GroundingPassage, KnowledgeChunk, KnowledgeSearch, KnowledgeSource, SkippedSource, StackFactorReference, } from "./stackfactorKnowledge.js";
|
|
29
|
+
export type { ReferenceContext, ReferencePage, ReferencePassage, SourceGuidance, StoredReference, } from "./referenceContext.js";
|
|
28
30
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,aAAa,EAAE,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;A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B,CAAC;AAEnC,YAAY,EACV,gBAAgB,EAChB,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,eAAe,GAChB,MAAM,uBAAuB,CAAC"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.stackfactorKnowledge = exports.USAGE_UNITS = exports.recordUsage = exports.runtimeContext = exports.checkAgent = exports.callAgent = exports.EXECUTION_ENV = exports.isRunOnceExecution = exports.runOnce = exports.serve = exports.logger = exports.checkUrlsReachable = exports.langChain = exports.AppError = exports.errorHandling = exports.constants = void 0;
|
|
39
|
+
exports.KnowledgeAccessError = exports.referenceContext = exports.stackfactorKnowledge = exports.USAGE_UNITS = exports.recordUsage = exports.runtimeContext = exports.checkAgent = exports.callAgent = exports.EXECUTION_ENV = exports.isRunOnceExecution = exports.runOnce = exports.serve = exports.logger = exports.checkUrlsReachable = exports.langChain = exports.AppError = exports.errorHandling = exports.constants = void 0;
|
|
40
40
|
const const_js_1 = __importDefault(require("./const.js"));
|
|
41
41
|
exports.constants = const_js_1.default;
|
|
42
42
|
const errorHandling_js_1 = __importStar(require("./errorHandling.js"));
|
|
@@ -61,5 +61,8 @@ Object.defineProperty(exports, "recordUsage", { enumerable: true, get: function
|
|
|
61
61
|
Object.defineProperty(exports, "USAGE_UNITS", { enumerable: true, get: function () { return usage_js_1.USAGE_UNITS; } });
|
|
62
62
|
const stackfactorKnowledge_js_1 = __importDefault(require("./stackfactorKnowledge.js"));
|
|
63
63
|
exports.stackfactorKnowledge = stackfactorKnowledge_js_1.default;
|
|
64
|
+
const referenceContext_js_1 = __importStar(require("./referenceContext.js"));
|
|
65
|
+
exports.referenceContext = referenceContext_js_1.default;
|
|
66
|
+
Object.defineProperty(exports, "KnowledgeAccessError", { enumerable: true, get: function () { return referenceContext_js_1.KnowledgeAccessError; } });
|
|
64
67
|
const tavily_js_1 = require("./tavily.js");
|
|
65
68
|
Object.defineProperty(exports, "checkUrlsReachable", { enumerable: true, get: function () { return tavily_js_1.checkUrlsReachable; } });
|
package/dist/cjs/langChain.d.ts
CHANGED
|
@@ -31,6 +31,14 @@ export type UsageTracker = {
|
|
|
31
31
|
url: string;
|
|
32
32
|
title?: string;
|
|
33
33
|
}[];
|
|
34
|
+
/**
|
|
35
|
+
* Every URL `web_extract` asked Tavily for, each once, in the order first
|
|
36
|
+
* asked, whether Tavily could read it or not (a failed URL, a failed call).
|
|
37
|
+
* A URL refused by `allowedUrls` was never asked for and is not here. What
|
|
38
|
+
* Tavily answered is `webSearchSources`, under the address it answered with
|
|
39
|
+
* (after a redirect); this is what the run actually tried to read.
|
|
40
|
+
*/
|
|
41
|
+
attemptedUrls?: string[];
|
|
34
42
|
};
|
|
35
43
|
/**
|
|
36
44
|
* Instantiates and returns the appropriate LangChain chat model based on the model
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":"AAoGA,KAAK,UAAU,GAAG;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IAKtB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE;QAAE,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACvC;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"langChain.d.ts","sourceRoot":"","sources":["../../src/langChain.ts"],"names":[],"mappings":"AAoGA,KAAK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AAG,CAAC;sDA2sBF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,iBACE,YAAY,GAAG,IAAI,KAChC,OAAO,CAAC,GAAG,CAAC;sDAmTF,MAAM,UACT,GAAG,UACH,MAAM,YACL,GAAG,iBACE,YAAY,GAAG,IAAI,KAChC,OAAO,CAAC,GAAG,CAAC;0CAz6C8B,GAAG,KAAG,MAAM;uCAxyB9C,YAAY,GAAG,IAAI,GAAG,SAAS,aAC7B,MAAM,YACP,GAAG,WACJ,GAAG,YACF,QAAQ,GAAG,QAAQ,KAC5B,IAAI;kCA5CI,YAAY,GAAG,IAAI,GAAG,SAAS,aAC7B,MAAM,SACV,UAAU,GAAG,IAAI,YACd,GAAG,KACZ,IAAI;+CA0FI,YAAY,GAAG,IAAI,GAAG,SAAS,aAC7B,MAAM,kBACD,MAAM,YACZ,GAAG,KACZ,IAAI;uCAoBI,YAAY,GAAG,IAAI,GAAG,SAAS,aAC7B,MAAM,WACR,MAAM,YACL,GAAG,KACZ,IAAI;mCAyuBU,MAAM,mBACJ,MAAM,EAAE,KACxB,MAAM;;AA48CT,wBAkBE"}
|
package/dist/cjs/langChain.js
CHANGED
|
@@ -1210,8 +1210,9 @@ const parseAgenticJsonResponse = (rawContent, schema) => {
|
|
|
1210
1210
|
*
|
|
1211
1211
|
* Setting `config.tavily` adds the Tavily web tools (`web_search`, `web_extract`, and
|
|
1212
1212
|
* optionally `web_map` / `web_crawl`). They execute in this process, so they require
|
|
1213
|
-
* `config.agentic` to be `true`; the credits spent
|
|
1214
|
-
* on `usageTracker`.
|
|
1213
|
+
* `config.agentic` to be `true`; the credits spent, the URLs returned and the URLs
|
|
1214
|
+
* `web_extract` asked for are recorded on `usageTracker`. `tavily.allowedUrls` keeps
|
|
1215
|
+
* `web_extract` to those URLs.
|
|
1215
1216
|
* @param modelName - The model identifier, e.g. `"gpt-4o"`, `"claude-3-5-sonnet"`,
|
|
1216
1217
|
* `"gemini-1.5-pro"`
|
|
1217
1218
|
* @param config - Configuration object with API keys, `temperature`, optional `agentic`
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { KnowledgeSearch, SkippedSource } from "./stackfactorKnowledge.js";
|
|
2
|
+
/** The prompt key the passages are handed to the model under. */
|
|
3
|
+
export declare const REFERENCE_PASSAGES_KEY = "Knowledge Base Passages";
|
|
4
|
+
/** A web or publication page a reference points at. */
|
|
5
|
+
export interface ReferencePage {
|
|
6
|
+
/** The reference's short id, "r0"… */
|
|
7
|
+
id: string;
|
|
8
|
+
title: string;
|
|
9
|
+
url: string;
|
|
10
|
+
}
|
|
11
|
+
/** A passage of a document that references cite. */
|
|
12
|
+
export interface ReferencePassage {
|
|
13
|
+
/**
|
|
14
|
+
* The short ids of the STACKFACTOR references that cite this passage's chunk
|
|
15
|
+
* (`knowledgeBase.chunkId`); when none does, of every one citing its document.
|
|
16
|
+
*/
|
|
17
|
+
references: string[];
|
|
18
|
+
document: string | null;
|
|
19
|
+
section: string | null;
|
|
20
|
+
text: string;
|
|
21
|
+
chunkId: string;
|
|
22
|
+
documentId: string;
|
|
23
|
+
knowledgeBaseId: string;
|
|
24
|
+
}
|
|
25
|
+
/** What a list of references points at. */
|
|
26
|
+
export interface ReferenceContext {
|
|
27
|
+
pages: ReferencePage[];
|
|
28
|
+
passages: ReferencePassage[];
|
|
29
|
+
/**
|
|
30
|
+
* Always empty from `gatherReferenceContext`: a cited knowledge base that
|
|
31
|
+
* cannot be read throws KnowledgeAccessError, which carries the skips.
|
|
32
|
+
*/
|
|
33
|
+
skipped: SkippedSource[];
|
|
34
|
+
/** True when the references cite at least one knowledge base document (all of them readable, or it would have thrown). */
|
|
35
|
+
citesKnowledge: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The knowledge base documents a run's references cite could not be read: the
|
|
39
|
+
* run has no token, the person may not open a cited knowledge base (401, 403,
|
|
40
|
+
* 404), or its search failed. The agent answers with it — `code` and `message`
|
|
41
|
+
* are what the person sees — and generates nothing.
|
|
42
|
+
*/
|
|
43
|
+
export declare class KnowledgeAccessError extends Error {
|
|
44
|
+
/** 403 when every skipped knowledge base refused the person (or there was no token), else 502. */
|
|
45
|
+
readonly code: 403 | 502;
|
|
46
|
+
/** The knowledge bases that could not be searched, and why; empty when there was no token. */
|
|
47
|
+
readonly skipped: SkippedSource[];
|
|
48
|
+
/** The titles of the STACKFACTOR references citing those knowledge bases, each once. */
|
|
49
|
+
readonly documents: string[];
|
|
50
|
+
/**
|
|
51
|
+
* @param details `skipped` (from the search) and `documents` (titles); the
|
|
52
|
+
* code and the message follow from them
|
|
53
|
+
*/
|
|
54
|
+
constructor(details?: {
|
|
55
|
+
skipped?: SkippedSource[];
|
|
56
|
+
documents?: unknown[];
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* What a list of references points at: its pages, and the passages of the
|
|
61
|
+
* documents its STACKFACTOR references cite. One search per knowledge base per
|
|
62
|
+
* query (per batch of 500 documents), however many references there are.
|
|
63
|
+
*
|
|
64
|
+
* References that cite a knowledge base the run cannot read stop it: with no
|
|
65
|
+
* token, or when any cited knowledge base is skipped (refused or failed), it
|
|
66
|
+
* throws a KnowledgeAccessError naming the documents. A search that works and
|
|
67
|
+
* finds nothing is not an error. With no knowledge cited, no token is needed.
|
|
68
|
+
*
|
|
69
|
+
* A passage is attributed to the references citing its chunk, else to every
|
|
70
|
+
* reference citing its document. The best-ranked `maxPassages` are kept, and
|
|
71
|
+
* the first `maxPages` pages in reference order.
|
|
72
|
+
* @param options `references` (a skill's, a microskill's…), `queries` to
|
|
73
|
+
* search the cited documents with, `search` (client-api
|
|
74
|
+
* `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK` per
|
|
75
|
+
* search (default 5), `maxPassages` (default 40), `maxPages` (default: all)
|
|
76
|
+
* @returns `{ pages, passages, skipped, citesKnowledge }`; `skipped` is always
|
|
77
|
+
* empty, since a skip throws
|
|
78
|
+
* @throws KnowledgeAccessError When cited knowledge bases cannot be read
|
|
79
|
+
*/
|
|
80
|
+
export declare const gatherReferenceContext: (options: {
|
|
81
|
+
references: unknown;
|
|
82
|
+
queries: string[];
|
|
83
|
+
search: KnowledgeSearch;
|
|
84
|
+
token: string | null | undefined;
|
|
85
|
+
topK?: number;
|
|
86
|
+
maxPassages?: number;
|
|
87
|
+
maxPages?: number;
|
|
88
|
+
}) => Promise<ReferenceContext>;
|
|
89
|
+
/**
|
|
90
|
+
* The passages as a prompt shows them: who cites each, where it came from, and
|
|
91
|
+
* its text. The ids that locate it in the platform are left out.
|
|
92
|
+
* @param context From `gatherReferenceContext`
|
|
93
|
+
* @returns `[{ references, document, section, text }]`, for the model's input
|
|
94
|
+
*/
|
|
95
|
+
export declare const referencePassagesForPrompt: (context: ReferenceContext) => {
|
|
96
|
+
references: string[];
|
|
97
|
+
document: string | null;
|
|
98
|
+
section: string | null;
|
|
99
|
+
text: string;
|
|
100
|
+
}[];
|
|
101
|
+
/** The guidance an agent's config gives for each kind of source it may be handed. */
|
|
102
|
+
export interface SourceGuidance {
|
|
103
|
+
pages: string;
|
|
104
|
+
passages: string;
|
|
105
|
+
none: string;
|
|
106
|
+
}
|
|
107
|
+
/** A reference as the platform stores it. `visibility` is left to the caller. */
|
|
108
|
+
export interface StoredReference {
|
|
109
|
+
kind: string;
|
|
110
|
+
title: string;
|
|
111
|
+
description?: string;
|
|
112
|
+
url?: string;
|
|
113
|
+
publication?: {
|
|
114
|
+
author?: string;
|
|
115
|
+
publisher?: string;
|
|
116
|
+
identifier?: string;
|
|
117
|
+
locator?: string;
|
|
118
|
+
};
|
|
119
|
+
origin: string;
|
|
120
|
+
visibility?: string;
|
|
121
|
+
target?: {
|
|
122
|
+
elementType: string;
|
|
123
|
+
elementId: string;
|
|
124
|
+
parentId?: string;
|
|
125
|
+
};
|
|
126
|
+
knowledgeBase?: {
|
|
127
|
+
knowledgeBaseId: string;
|
|
128
|
+
documentId: string;
|
|
129
|
+
chunkId?: string;
|
|
130
|
+
sectionTitle?: string | null;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The tools a run is given. Never `search`, `map` or `crawl`: a run reads what
|
|
135
|
+
* its references point at and discovers nothing. `extract` only, only when
|
|
136
|
+
* there are pages to open, and kept to those pages: any other url the model
|
|
137
|
+
* asks for is refused (`allowedUrls`).
|
|
138
|
+
* @param context From `gatherReferenceContext`
|
|
139
|
+
* @returns `{ tavily: { tools: ["extract"], allowedUrls } }` with pages, else
|
|
140
|
+
* `{}`. With no pages it returns `{}`, so a caller spreading it over a config
|
|
141
|
+
* that already has a `tavily` key keeps that key (and `tavily: true` means
|
|
142
|
+
* search plus extract): callers must not carry a base `tavily` key.
|
|
143
|
+
*/
|
|
144
|
+
export declare const toolsFor: (context: ReferenceContext) => {
|
|
145
|
+
tavily?: {
|
|
146
|
+
tools: string[];
|
|
147
|
+
allowedUrls: string[];
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The stage prompt followed by the guidance for each kind of source the run
|
|
152
|
+
* has: pages, then passages; when it has neither (knowledge cited and readable
|
|
153
|
+
* but no passage found included), the `none` guidance. An empty guidance adds
|
|
154
|
+
* nothing.
|
|
155
|
+
* @param stagePrompt The stage's own prompt
|
|
156
|
+
* @param context From `gatherReferenceContext`
|
|
157
|
+
* @param guidance From `sourceGuidanceFrom`
|
|
158
|
+
* @returns The sections joined with a blank line
|
|
159
|
+
*/
|
|
160
|
+
export declare const composeSourceInstructions: (stagePrompt: string, context: ReferenceContext, guidance: SourceGuidance) => string;
|
|
161
|
+
/**
|
|
162
|
+
* A reference as a model is shown it. A STACKFACTOR reference loses its pointer
|
|
163
|
+
* and its url (ids mean nothing to a model, and the url is behind the
|
|
164
|
+
* organization's own sign-in); the model cites it by short id.
|
|
165
|
+
* @param reference A supplied reference
|
|
166
|
+
* @returns The reference; a STACKFACTOR one without `_id`, `origin`, `visibility`,
|
|
167
|
+
* `target`, `knowledgeBase` and `url`
|
|
168
|
+
*/
|
|
169
|
+
export declare const referenceForModel: <T extends {
|
|
170
|
+
kind?: string;
|
|
171
|
+
}>(reference: T) => T;
|
|
172
|
+
/**
|
|
173
|
+
* The supplied references as a model is shown them, each under its short id
|
|
174
|
+
* `r<N>` (N the index in the supplied list) as `_id`, set last so a stored
|
|
175
|
+
* reference's own id never wins. Non-object entries are skipped but keep their
|
|
176
|
+
* index, so the ids stay aligned with `citedReferences`.
|
|
177
|
+
* @param references The supplied list
|
|
178
|
+
*/
|
|
179
|
+
export declare const referencesForModel: (references: unknown) => Array<Record<string, unknown>>;
|
|
180
|
+
/**
|
|
181
|
+
* A reference from the flat projection the host sends (or the flat shape a
|
|
182
|
+
* model emits) in the shape the platform stores: nested `publication`,
|
|
183
|
+
* `relatedTo` folded into `description`, a STACKFACTOR reference's pointer and
|
|
184
|
+
* target kept. `visibility` is never set; the caller decides it.
|
|
185
|
+
* @param reference The flat reference
|
|
186
|
+
* @param origin Default `"AI_GENERATED"`
|
|
187
|
+
*/
|
|
188
|
+
export declare const toStoredReference: (reference: any, origin?: string) => StoredReference;
|
|
189
|
+
/**
|
|
190
|
+
* The supplied references a model cited by short id, stored. Ids are matched
|
|
191
|
+
* trimmed and case-insensitively; unknown and non-string ids are dropped and
|
|
192
|
+
* each reference appears once, at its first citation.
|
|
193
|
+
* @param ids The model's cited ids (`["r0", "r3"]`)
|
|
194
|
+
* @param supplied The list the ids index into
|
|
195
|
+
* @param origin Default `"AI_GENERATED"`
|
|
196
|
+
*/
|
|
197
|
+
export declare const citedReferences: (ids: unknown, supplied: unknown, origin?: string) => StoredReference[];
|
|
198
|
+
/**
|
|
199
|
+
* The three guidance texts an agent's config holds. A missing or non-string
|
|
200
|
+
* value is an empty string.
|
|
201
|
+
* @param config The agent's config
|
|
202
|
+
*/
|
|
203
|
+
export declare const sourceGuidanceFrom: (config: Record<string, unknown>) => SourceGuidance;
|
|
204
|
+
declare const _default: {
|
|
205
|
+
KnowledgeAccessError: typeof KnowledgeAccessError;
|
|
206
|
+
REFERENCE_PASSAGES_KEY: string;
|
|
207
|
+
citedReferences: (ids: unknown, supplied: unknown, origin?: string) => StoredReference[];
|
|
208
|
+
composeSourceInstructions: (stagePrompt: string, context: ReferenceContext, guidance: SourceGuidance) => string;
|
|
209
|
+
gatherReferenceContext: (options: {
|
|
210
|
+
references: unknown;
|
|
211
|
+
queries: string[];
|
|
212
|
+
search: KnowledgeSearch;
|
|
213
|
+
token: string | null | undefined;
|
|
214
|
+
topK?: number;
|
|
215
|
+
maxPassages?: number;
|
|
216
|
+
maxPages?: number;
|
|
217
|
+
}) => Promise<ReferenceContext>;
|
|
218
|
+
referenceForModel: <T extends {
|
|
219
|
+
kind?: string;
|
|
220
|
+
}>(reference: T) => T;
|
|
221
|
+
referencePassagesForPrompt: (context: ReferenceContext) => {
|
|
222
|
+
references: string[];
|
|
223
|
+
document: string | null;
|
|
224
|
+
section: string | null;
|
|
225
|
+
text: string;
|
|
226
|
+
}[];
|
|
227
|
+
referencesForModel: (references: unknown) => Array<Record<string, unknown>>;
|
|
228
|
+
sourceGuidanceFrom: (config: Record<string, unknown>) => SourceGuidance;
|
|
229
|
+
toStoredReference: (reference: any, origin?: string) => StoredReference;
|
|
230
|
+
toolsFor: (context: ReferenceContext) => {
|
|
231
|
+
tavily?: {
|
|
232
|
+
tools: string[];
|
|
233
|
+
allowedUrls: string[];
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
export default _default;
|
|
238
|
+
//# sourceMappingURL=referenceContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"referenceContext.d.ts","sourceRoot":"","sources":["../../src/referenceContext.ts"],"names":[],"mappings":"AAuBA,OAAO,EACL,eAAe,EACf,aAAa,EAId,MAAM,2BAA2B,CAAC;AAQnC,iEAAiE;AACjE,eAAO,MAAM,sBAAsB,4BAA4B,CAAC;AAEhE,uDAAuD;AACvD,MAAM,WAAW,aAAa;IAC5B,sCAAsC;IACtC,EA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