@stackfactor/agent-utils 1.7.0 → 1.9.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 +15 -0
- package/dist/cjs/errorHandling.d.ts.map +1 -1
- package/dist/cjs/errorHandling.js +10 -5
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/langChain.d.ts +1 -1
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +27 -8
- package/dist/cjs/stackfactorKnowledge.d.ts +182 -0
- package/dist/cjs/stackfactorKnowledge.d.ts.map +1 -0
- package/dist/cjs/stackfactorKnowledge.js +274 -0
- package/dist/cjs/tavily.d.ts +31 -0
- package/dist/cjs/tavily.d.ts.map +1 -1
- package/dist/cjs/tavily.js +88 -1
- package/dist/esm/errorHandling.d.ts.map +1 -1
- package/dist/esm/errorHandling.js +10 -5
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/langChain.d.ts +1 -1
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +27 -8
- package/dist/esm/stackfactorKnowledge.d.ts +182 -0
- package/dist/esm/stackfactorKnowledge.d.ts.map +1 -0
- package/dist/esm/stackfactorKnowledge.js +266 -0
- package/dist/esm/tavily.d.ts +31 -0
- package/dist/esm/tavily.d.ts.map +1 -1
- package/dist/esm/tavily.js +86 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -419,6 +419,21 @@ A model that cannot tell it received half a regulation will reason over the half
|
|
|
419
419
|
- 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
420
|
- Tool calls honour the caller's abort signal, so a cancelled run stops paying for in-flight crawls.
|
|
421
421
|
|
|
422
|
+
### Checking that cited links resolve
|
|
423
|
+
|
|
424
|
+
`checkUrlsReachable(urls, config, usageTracker?)` returns the URLs, exactly as passed, that Tavily could fetch. Use it in place of requesting a link yourself. Cited links come from web pages anyone can publish, so a link, its redirects or its DNS can point inside StackFactor's network; a request this process sends would go there through StackFactor's own egress addresses. Tavily fetches from its own infrastructure.
|
|
425
|
+
|
|
426
|
+
```js
|
|
427
|
+
const { checkUrlsReachable } = require("@stackfactor/agent-utils");
|
|
428
|
+
|
|
429
|
+
const live = await checkUrlsReachable(references.map((r) => r.url), config, usageTracker);
|
|
430
|
+
const kept = references.filter((r) => !r.url || live.has(r.url));
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
- Only `http:` and `https:` URLs are sent, 20 per call (Tavily's limit), at basic depth: 1 credit per 5 URLs fetched, failed ones free. Credits are billed like any Tavily call; the URLs are not added to `webSearchSources`.
|
|
434
|
+
- A URL Tavily lists under `failed_results` is unreachable. A batch whose call fails, or a config without `tavilyAPIKey`, confirms none of its URLs. It never throws.
|
|
435
|
+
- No `config.tavily` or `agentic` is needed: it calls Tavily directly, not through the model.
|
|
436
|
+
|
|
422
437
|
---
|
|
423
438
|
|
|
424
439
|
## Supported Models
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorHandling.d.ts","sourceRoot":"","sources":["../../src/errorHandling.ts"],"names":[],"mappings":"AAGA,gEAAgE;AAChE,KAAK,QAAQ,GACT,OAAO,GACP,MAAM,GACN,MAAM,GACN,MAAM,GACN,SAAS,GACT,OAAO,GACP,OAAO,CAAC;AAEZ;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;
|
|
1
|
+
{"version":3,"file":"errorHandling.d.ts","sourceRoot":"","sources":["../../src/errorHandling.ts"],"names":[],"mappings":"AAGA,gEAAgE;AAChE,KAAK,QAAQ,GACT,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AI,MAAM;IA4P5C,kCAAkC;;;AA7BpC,wBA+BE"}
|
|
@@ -87,11 +87,16 @@ const parseError = (error) => {
|
|
|
87
87
|
// A JSON-encoded error string, or just a plain message.
|
|
88
88
|
try {
|
|
89
89
|
const parsed = JSON.parse(error);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
// Only an object is an encoded error; "404" or "true" also parse as
|
|
91
|
+
// JSON but are just the message text.
|
|
92
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
93
|
+
return {
|
|
94
|
+
code: isHttpCode(parsed.code) ? parsed.code : undefined,
|
|
95
|
+
message: parsed.message,
|
|
96
|
+
integration: parsed.integration,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return { message: error };
|
|
95
100
|
}
|
|
96
101
|
catch {
|
|
97
102
|
return { message: error };
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -6,11 +6,14 @@ import { serve, runOnce, isRunOnceExecution, EXECUTION_ENV } from "./serve.js";
|
|
|
6
6
|
import { callAgent, checkAgent } from "./client.js";
|
|
7
7
|
import * as runtimeContext from "./runtimeContext.js";
|
|
8
8
|
import { recordUsage, USAGE_UNITS } from "./usage.js";
|
|
9
|
+
import stackfactorKnowledge from "./stackfactorKnowledge.js";
|
|
10
|
+
import { checkUrlsReachable } from "./tavily.js";
|
|
9
11
|
export { constants };
|
|
10
12
|
export { errorHandling, AppError };
|
|
11
13
|
export type { ParsedError } from "./errorHandling.js";
|
|
12
14
|
export { langChain };
|
|
13
15
|
export type { UsageTracker } from "./langChain.js";
|
|
16
|
+
export { checkUrlsReachable };
|
|
14
17
|
export type { TavilyConfig, TavilyToolName } from "./tavily.js";
|
|
15
18
|
export { logger };
|
|
16
19
|
export { serve, runOnce, isRunOnceExecution, EXECUTION_ENV };
|
|
@@ -20,4 +23,6 @@ export type { AgentExecuteRequest, CallAgentOptions, CheckAgentOptions, AgentHea
|
|
|
20
23
|
export { runtimeContext };
|
|
21
24
|
export { recordUsage, USAGE_UNITS };
|
|
22
25
|
export type { UsageEntry, UsageUnit } from "./usage.js";
|
|
26
|
+
export { stackfactorKnowledge };
|
|
27
|
+
export type { GroundingPassage, KnowledgeChunk, KnowledgeSearch, KnowledgeSource, SkippedSource, StackFactorReference, } from "./stackfactorKnowledge.js";
|
|
23
28
|
//# 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;
|
|
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;AACtD,OAAO,oBAAoB,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC;AAEnC,YAAY,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EAAE,kBAAkB,EAAE,CAAC;AAE9B,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEhE,OAAO,EAAE,MAAM,EAAE,CAAC;AAElB,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,CAAC;AAE7D,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,OAAO,EAAE,UAAU,EAAE,CAAC;AAEtB,YAAY,EACV,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,GACZ,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1B,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC;AAEpC,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAExD,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAEhC,YAAY,EACV,gBAAgB,EAChB,cAAc,EACd,eAAe,EACf,eAAe,EACf,aAAa,EACb,oBAAoB,GACrB,MAAM,2BAA2B,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.USAGE_UNITS = exports.recordUsage = exports.runtimeContext = exports.checkAgent = exports.callAgent = exports.EXECUTION_ENV = exports.isRunOnceExecution = exports.runOnce = exports.serve = exports.logger = exports.langChain = exports.AppError = exports.errorHandling = exports.constants = void 0;
|
|
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;
|
|
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"));
|
|
@@ -59,3 +59,7 @@ exports.runtimeContext = runtimeContext;
|
|
|
59
59
|
const usage_js_1 = require("./usage.js");
|
|
60
60
|
Object.defineProperty(exports, "recordUsage", { enumerable: true, get: function () { return usage_js_1.recordUsage; } });
|
|
61
61
|
Object.defineProperty(exports, "USAGE_UNITS", { enumerable: true, get: function () { return usage_js_1.USAGE_UNITS; } });
|
|
62
|
+
const stackfactorKnowledge_js_1 = __importDefault(require("./stackfactorKnowledge.js"));
|
|
63
|
+
exports.stackfactorKnowledge = stackfactorKnowledge_js_1.default;
|
|
64
|
+
const tavily_js_1 = require("./tavily.js");
|
|
65
|
+
Object.defineProperty(exports, "checkUrlsReachable", { enumerable: true, get: function () { return tavily_js_1.checkUrlsReachable; } });
|
package/dist/cjs/langChain.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ declare const _default: {
|
|
|
62
62
|
supportsNativeSchema: (modelName: string) => boolean;
|
|
63
63
|
checkIfAIProviderConfigured: (config: any) => void;
|
|
64
64
|
createAgent: (name: string, modelName: string, systemPrompt: string, tools: any[], responseFormat: any, config: any, usageTracker?: UsageTracker | null) => any;
|
|
65
|
-
runAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null, usageTracker?: UsageTracker | null) => Promise<any>;
|
|
65
|
+
runAgent: (agent: any, prompt: string, config: any, onProgress?: Function | null, usageTracker?: UsageTracker | null, progress?: number) => Promise<any>;
|
|
66
66
|
runPromptWithModel: (modelName: string, config: any, prompt: any, onProgressReport: any, minPercent?: number, maxPercent?: number, expectsJsonResponse?: boolean, schema?: any, agentName?: string, tools?: any[], usageTracker?: UsageTracker | null) => Promise<any>;
|
|
67
67
|
runPromptWithModelForImageGeneration: (modelName: string, config: any, prompt: string, options?: any, usageTracker?: UsageTracker | null) => Promise<any>;
|
|
68
68
|
runPromptWithModelForVideoGeneration: (modelName: string, config: any, prompt: string, options?: any, usageTracker?: UsageTracker | null) => Promise<any>;
|
|
@@ -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;CACtD,CAAC;
|
|
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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,CAAC;0CAx6C8B,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;;AA28CT,wBAkBE"}
|
package/dist/cjs/langChain.js
CHANGED
|
@@ -333,8 +333,14 @@ const formatZodErrors = (zodError) => {
|
|
|
333
333
|
const path = err.path.length > 0 ? err.path.join(".") : "root";
|
|
334
334
|
// Provide more context based on error type
|
|
335
335
|
switch (err.code) {
|
|
336
|
-
case "invalid_type":
|
|
337
|
-
|
|
336
|
+
case "invalid_type": {
|
|
337
|
+
// zod v3 sets `received`; zod v4 names it only in the message
|
|
338
|
+
// ("Invalid input: expected string, received number").
|
|
339
|
+
const received = err.received ??
|
|
340
|
+
/received (\w+)/.exec(String(err.message ?? ""))?.[1] ??
|
|
341
|
+
"a different type";
|
|
342
|
+
return `Field "${path}": Expected ${err.expected}, but received ${received}`;
|
|
343
|
+
}
|
|
338
344
|
case "invalid_literal":
|
|
339
345
|
return `Field "${path}": Expected literal value ${JSON.stringify(err.expected)}, but received ${JSON.stringify(err.received)}`;
|
|
340
346
|
case "unrecognized_keys":
|
|
@@ -771,19 +777,30 @@ const createAgent = (name, modelName, systemPrompt, tools = [], responseFormat,
|
|
|
771
777
|
* @param onProgress - Optional callback invoked with progress event objects throughout
|
|
772
778
|
* agent execution
|
|
773
779
|
* @param usageTracker - Optional usage accumulator updated on successful completion
|
|
780
|
+
* @param progress - The percentage every progress event reports (the stage's
|
|
781
|
+
* current one; the agent loop has no percentage of its own). Defaults to 0.
|
|
774
782
|
* @returns The raw response object returned by the agent's `invoke` method
|
|
775
783
|
*/
|
|
776
|
-
const runAgent = async (agent, prompt, config, onProgress = null, usageTracker = null) => {
|
|
784
|
+
const runAgent = async (agent, prompt, config, onProgress = null, usageTracker = null, progress = 0) => {
|
|
777
785
|
const startTime = Date.now();
|
|
786
|
+
// Every event carries `progress`: callers forward these to the platform's
|
|
787
|
+
// `{ progress, message }` progress bar, where a missing value would read as
|
|
788
|
+
// 0% and make the bar jump back during a tool call. The agent loop reports
|
|
789
|
+
// no percentage of its own, so the stage's current one is repeated.
|
|
778
790
|
const callbacks = onProgress
|
|
779
791
|
? [
|
|
780
792
|
{
|
|
781
|
-
handleToolStart
|
|
782
|
-
|
|
793
|
+
// LangChain calls handleToolStart(serializedTool, input, runId,
|
|
794
|
+
// parentRunId, tags, metadata, runName): the serialized tool
|
|
795
|
+
// ({ lc, type, id }) has no name; the tool's name is runName.
|
|
796
|
+
handleToolStart: (toolInfo, _input, _runId, _parentRunId, _tags, _metadata, runName) => {
|
|
797
|
+
const name = runName || toolInfo?.name || toolInfo?.kwargs?.name;
|
|
798
|
+
if (name)
|
|
783
799
|
onProgress({
|
|
784
800
|
type: "tool_start",
|
|
785
|
-
message: `Using "${
|
|
786
|
-
tool:
|
|
801
|
+
message: `Using "${name}" tool`,
|
|
802
|
+
tool: name,
|
|
803
|
+
progress,
|
|
787
804
|
});
|
|
788
805
|
},
|
|
789
806
|
handleAgentAction: (action) => {
|
|
@@ -792,12 +809,14 @@ const runAgent = async (agent, prompt, config, onProgress = null, usageTracker =
|
|
|
792
809
|
message: action.log || `Calling "${action.tool}" action`,
|
|
793
810
|
tool: action.tool,
|
|
794
811
|
input: action.toolInput,
|
|
812
|
+
progress,
|
|
795
813
|
});
|
|
796
814
|
},
|
|
797
815
|
handleChainError: (err) => {
|
|
798
816
|
onProgress({
|
|
799
817
|
type: "error",
|
|
800
818
|
message: `Error: ${err?.message || String(err)}`,
|
|
819
|
+
progress,
|
|
801
820
|
});
|
|
802
821
|
},
|
|
803
822
|
},
|
|
@@ -1261,7 +1280,7 @@ const runPromptWithModel = async (modelName, config, prompt, onProgressReport, m
|
|
|
1261
1280
|
let attempt = 0;
|
|
1262
1281
|
for (;;) {
|
|
1263
1282
|
// Run the agent with progress callback
|
|
1264
|
-
const response = await runAgent(agent, activeUserPrompt, config, onProgressReport || null, usageTracker);
|
|
1283
|
+
const response = await runAgent(agent, activeUserPrompt, config, onProgressReport || null, usageTracker, minPercent);
|
|
1265
1284
|
// Extract content from agent response
|
|
1266
1285
|
const messages = response?.messages || [];
|
|
1267
1286
|
if (messages.length === 0) {
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/** What a STACKFACTOR reference to a knowledge base document carries (backend-core REFERENCE). */
|
|
2
|
+
export declare const STACKFACTOR_REFERENCE: Readonly<{
|
|
3
|
+
KIND: "STACKFACTOR";
|
|
4
|
+
TARGET_ELEMENT_TYPE: "KNOWLEDGE_BASE_DOCUMENT";
|
|
5
|
+
ORIGIN: "KB_EXTRACTION";
|
|
6
|
+
VISIBILITY: "EDIT";
|
|
7
|
+
}>;
|
|
8
|
+
/** How many documents one search may be kept to (the backend's bound); more are split. */
|
|
9
|
+
export declare const SEARCH_DOCUMENT_IDS_MAX = 500;
|
|
10
|
+
/** How long a query may be (the backend's bound); longer ones are cut. */
|
|
11
|
+
export declare const QUERY_MAX_LENGTH = 4000;
|
|
12
|
+
/** How many passages one search asks for when the caller does not say. */
|
|
13
|
+
export declare const DEFAULT_TOP_K = 8;
|
|
14
|
+
/** A chunk as the platform's search answers it (client-api `RetrievedChunk`). */
|
|
15
|
+
export interface KnowledgeChunk {
|
|
16
|
+
_id: string;
|
|
17
|
+
knowledgeBaseId?: string;
|
|
18
|
+
documentId: string;
|
|
19
|
+
documentName?: string | null;
|
|
20
|
+
sectionTitle?: string | null;
|
|
21
|
+
pageNumber?: number | null;
|
|
22
|
+
text?: string;
|
|
23
|
+
skills?: {
|
|
24
|
+
name: string | null;
|
|
25
|
+
microSkills?: string[];
|
|
26
|
+
}[];
|
|
27
|
+
score?: number | null;
|
|
28
|
+
[field: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
/** The platform's search, as client-api's `knowledgeBaseAgent.search` is called. */
|
|
31
|
+
export type KnowledgeSearch = (knowledgeBaseId: string, query: string, options: {
|
|
32
|
+
topK?: number;
|
|
33
|
+
documentIds?: string[];
|
|
34
|
+
}, token: string) => Promise<{
|
|
35
|
+
chunks: KnowledgeChunk[];
|
|
36
|
+
}>;
|
|
37
|
+
/** Where to search: a whole knowledge base, or only some of its documents. */
|
|
38
|
+
export interface KnowledgeSource {
|
|
39
|
+
knowledgeBaseId: string;
|
|
40
|
+
/** Only these documents; omitted, the whole knowledge base. */
|
|
41
|
+
documentIds?: string[];
|
|
42
|
+
}
|
|
43
|
+
/** A passage handed to the model, tagged so it can cite it. */
|
|
44
|
+
export interface GroundingPassage {
|
|
45
|
+
/** "k1", "k2"… in the order they rank. */
|
|
46
|
+
tag: string;
|
|
47
|
+
chunkId: string;
|
|
48
|
+
knowledgeBaseId: string;
|
|
49
|
+
documentId: string;
|
|
50
|
+
documentName: string | null;
|
|
51
|
+
sectionTitle: string | null;
|
|
52
|
+
pageNumber: number | null;
|
|
53
|
+
text: string;
|
|
54
|
+
/** The skills its passage teaches, as labelling named them. */
|
|
55
|
+
skills: {
|
|
56
|
+
name: string | null;
|
|
57
|
+
microSkills: string[];
|
|
58
|
+
}[];
|
|
59
|
+
score: number | null;
|
|
60
|
+
}
|
|
61
|
+
/** A knowledge base that could not be searched, and why. */
|
|
62
|
+
export interface SkippedSource {
|
|
63
|
+
knowledgeBaseId: string;
|
|
64
|
+
/** The HTTP status, when the platform answered one. */
|
|
65
|
+
status: number | null;
|
|
66
|
+
/** True when the person may not read it (or it is gone): expected, not a fault. */
|
|
67
|
+
noAccess: boolean;
|
|
68
|
+
message: string;
|
|
69
|
+
}
|
|
70
|
+
/** A STACKFACTOR reference to the knowledge base document a passage came from. */
|
|
71
|
+
export interface StackFactorReference {
|
|
72
|
+
kind: "STACKFACTOR";
|
|
73
|
+
title: string;
|
|
74
|
+
description: string | null;
|
|
75
|
+
target: {
|
|
76
|
+
elementType: "KNOWLEDGE_BASE_DOCUMENT";
|
|
77
|
+
elementId: string;
|
|
78
|
+
parentId: string;
|
|
79
|
+
};
|
|
80
|
+
knowledgeBase: {
|
|
81
|
+
knowledgeBaseId: string;
|
|
82
|
+
documentId: string;
|
|
83
|
+
chunkId: string;
|
|
84
|
+
sectionTitle: string | null;
|
|
85
|
+
};
|
|
86
|
+
origin: "KB_EXTRACTION";
|
|
87
|
+
visibility: "EDIT";
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The knowledge bases, and the documents in them, that a list of references
|
|
91
|
+
* cites: its STACKFACTOR references to knowledge base documents, by their
|
|
92
|
+
* `knowledgeBase` ids, or by their `target` when only that is there. Any other
|
|
93
|
+
* reference is passed over. Each knowledge base once, its documents once each,
|
|
94
|
+
* in the order they first appear.
|
|
95
|
+
* @param references A skill's (or a microskill's) references
|
|
96
|
+
* @returns The sources to search
|
|
97
|
+
*/
|
|
98
|
+
export declare const sourcesFromReferences: (references: unknown) => KnowledgeSource[];
|
|
99
|
+
/**
|
|
100
|
+
* The knowledge bases skill coverage found (client-api
|
|
101
|
+
* `knowledgeBase.getSkillCoverage`): every one the person can open that holds
|
|
102
|
+
* the skill, each searched whole.
|
|
103
|
+
* @param coverage The coverage answer, `{ knowledgeBases: [{ _id }] }`
|
|
104
|
+
* @returns The sources to search
|
|
105
|
+
*/
|
|
106
|
+
export declare const sourcesFromCoverage: (coverage: unknown) => KnowledgeSource[];
|
|
107
|
+
/**
|
|
108
|
+
* Searches every source for every query, under the person's access, and hands
|
|
109
|
+
* back the passages found, best first, each once, tagged "k1", "k2"… for the
|
|
110
|
+
* model to cite. A source with documents is searched within them (in batches of
|
|
111
|
+
* SEARCH_DOCUMENT_IDS_MAX); one without, whole. A source the person may not
|
|
112
|
+
* read (401, 403, 404) is skipped: none of its passages are used and no more of
|
|
113
|
+
* its searches are made. A search that fails otherwise is reported and the
|
|
114
|
+
* passages its other searches found stand. Either way it is in `skipped`, once
|
|
115
|
+
* per knowledge base, and the passages of the others stand.
|
|
116
|
+
* @param options `sources`, `queries`, `search` (client-api
|
|
117
|
+
* `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK`
|
|
118
|
+
* per search (default DEFAULT_TOP_K)
|
|
119
|
+
* @returns `{ passages, skipped }`
|
|
120
|
+
*/
|
|
121
|
+
export declare const retrievePassages: (options: {
|
|
122
|
+
sources: KnowledgeSource[];
|
|
123
|
+
queries: string[];
|
|
124
|
+
search: KnowledgeSearch;
|
|
125
|
+
token: string;
|
|
126
|
+
topK?: number;
|
|
127
|
+
}) => Promise<{
|
|
128
|
+
passages: GroundingPassage[];
|
|
129
|
+
skipped: SkippedSource[];
|
|
130
|
+
}>;
|
|
131
|
+
/**
|
|
132
|
+
* The passages as a prompt shows them: tag, where each came from, and its text.
|
|
133
|
+
* @param passages From `retrievePassages`
|
|
134
|
+
* @returns `[{ tag, document, section, text }]`, for the model's input
|
|
135
|
+
*/
|
|
136
|
+
export declare const passagesForPrompt: (passages: GroundingPassage[]) => {
|
|
137
|
+
tag: string;
|
|
138
|
+
document: string | null;
|
|
139
|
+
section: string | null;
|
|
140
|
+
text: string;
|
|
141
|
+
}[];
|
|
142
|
+
/**
|
|
143
|
+
* STACKFACTOR references for passages: one per passage, in their order, with no
|
|
144
|
+
* cap. With `tags`, only the passages the model cited by tag (any tag it made
|
|
145
|
+
* up is passed over); without, every passage.
|
|
146
|
+
* @param passages From `retrievePassages`
|
|
147
|
+
* @param tags The tags the model cited ("k3", "K3" and " k3 " alike)
|
|
148
|
+
* @returns The references, ready for a skill's, a microskill's or a question's `references`
|
|
149
|
+
*/
|
|
150
|
+
export declare const referencesFor: (passages: GroundingPassage[], tags?: unknown[]) => StackFactorReference[];
|
|
151
|
+
declare const _default: {
|
|
152
|
+
DEFAULT_TOP_K: number;
|
|
153
|
+
QUERY_MAX_LENGTH: number;
|
|
154
|
+
SEARCH_DOCUMENT_IDS_MAX: number;
|
|
155
|
+
STACKFACTOR_REFERENCE: Readonly<{
|
|
156
|
+
KIND: "STACKFACTOR";
|
|
157
|
+
TARGET_ELEMENT_TYPE: "KNOWLEDGE_BASE_DOCUMENT";
|
|
158
|
+
ORIGIN: "KB_EXTRACTION";
|
|
159
|
+
VISIBILITY: "EDIT";
|
|
160
|
+
}>;
|
|
161
|
+
passagesForPrompt: (passages: GroundingPassage[]) => {
|
|
162
|
+
tag: string;
|
|
163
|
+
document: string | null;
|
|
164
|
+
section: string | null;
|
|
165
|
+
text: string;
|
|
166
|
+
}[];
|
|
167
|
+
referencesFor: (passages: GroundingPassage[], tags?: unknown[]) => StackFactorReference[];
|
|
168
|
+
retrievePassages: (options: {
|
|
169
|
+
sources: KnowledgeSource[];
|
|
170
|
+
queries: string[];
|
|
171
|
+
search: KnowledgeSearch;
|
|
172
|
+
token: string;
|
|
173
|
+
topK?: number;
|
|
174
|
+
}) => Promise<{
|
|
175
|
+
passages: GroundingPassage[];
|
|
176
|
+
skipped: SkippedSource[];
|
|
177
|
+
}>;
|
|
178
|
+
sourcesFromCoverage: (coverage: unknown) => KnowledgeSource[];
|
|
179
|
+
sourcesFromReferences: (references: unknown) => KnowledgeSource[];
|
|
180
|
+
};
|
|
181
|
+
export default _default;
|
|
182
|
+
//# sourceMappingURL=stackfactorKnowledge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stackfactorKnowledge.d.ts","sourceRoot":"","sources":["../../src/stackfactorKnowledge.ts"],"names":[],"mappings":"AA8BA,kGAAkG;AAClG,eAAO,MAAM,qBAAqB;;;;;EAKhC,CAAC;AAEH,0FAA0F;AAC1F,eAAO,MAAM,uBAAuB,MAAM,CAAC;AA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