@stackfactor/agent-utils 1.13.4 → 1.32.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 +667 -23
- package/dist/cjs/agentProto.d.ts +13 -3
- package/dist/cjs/agentProto.d.ts.map +1 -1
- package/dist/cjs/agentProto.js +25 -4
- package/dist/cjs/backoff.d.ts +17 -0
- package/dist/cjs/backoff.d.ts.map +1 -0
- package/dist/cjs/backoff.js +26 -0
- package/dist/cjs/cancellation.d.ts +22 -0
- package/dist/cjs/cancellation.d.ts.map +1 -0
- package/dist/cjs/cancellation.js +36 -0
- package/dist/cjs/cli.d.ts +3 -0
- package/dist/cjs/cli.d.ts.map +1 -0
- package/dist/cjs/cli.js +85 -0
- package/dist/cjs/client.d.ts +11 -0
- package/dist/cjs/client.d.ts.map +1 -1
- package/dist/cjs/client.js +27 -0
- package/dist/cjs/const.d.ts +23 -0
- package/dist/cjs/const.d.ts.map +1 -1
- package/dist/cjs/const.js +23 -0
- package/dist/cjs/daemon.d.ts +94 -0
- package/dist/cjs/daemon.d.ts.map +1 -0
- package/dist/cjs/daemon.js +507 -0
- package/dist/cjs/daemonConfig.d.ts +45 -0
- package/dist/cjs/daemonConfig.d.ts.map +1 -0
- package/dist/cjs/daemonConfig.js +148 -0
- package/dist/cjs/daemonEnv.d.ts +10 -0
- package/dist/cjs/daemonEnv.d.ts.map +1 -0
- package/dist/cjs/daemonEnv.js +12 -0
- package/dist/cjs/durable/clientTool.d.ts +39 -0
- package/dist/cjs/durable/clientTool.d.ts.map +1 -0
- package/dist/cjs/durable/clientTool.js +90 -0
- package/dist/cjs/durable/index.d.ts +9 -0
- package/dist/cjs/durable/index.d.ts.map +1 -0
- package/dist/cjs/durable/index.js +16 -0
- package/dist/cjs/durable/journal.d.ts +218 -0
- package/dist/cjs/durable/journal.d.ts.map +1 -0
- package/dist/cjs/durable/journal.js +392 -0
- package/dist/cjs/durable/pause.d.ts +84 -0
- package/dist/cjs/durable/pause.d.ts.map +1 -0
- package/dist/cjs/durable/pause.js +241 -0
- package/dist/cjs/durable/resume.d.ts +27 -0
- package/dist/cjs/durable/resume.d.ts.map +1 -0
- package/dist/cjs/durable/resume.js +80 -0
- package/dist/cjs/durable/snapshotSaver.d.ts +38 -0
- package/dist/cjs/durable/snapshotSaver.d.ts.map +1 -0
- package/dist/cjs/durable/snapshotSaver.js +81 -0
- package/dist/cjs/durable/step.d.ts +28 -0
- package/dist/cjs/durable/step.d.ts.map +1 -0
- package/dist/cjs/durable/step.js +43 -0
- package/dist/cjs/index.d.ts +30 -7
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +28 -1
- package/dist/cjs/infrastructure.d.ts +50 -0
- package/dist/cjs/infrastructure.d.ts.map +1 -0
- package/dist/cjs/infrastructure.js +163 -0
- package/dist/cjs/langChain.d.ts +15 -3
- package/dist/cjs/langChain.d.ts.map +1 -1
- package/dist/cjs/langChain.js +986 -180
- package/dist/cjs/learningActivityMedia.d.ts +196 -0
- package/dist/cjs/learningActivityMedia.d.ts.map +1 -0
- package/dist/cjs/learningActivityMedia.js +861 -0
- package/dist/cjs/learningContentPlan.d.ts +105 -0
- package/dist/cjs/learningContentPlan.d.ts.map +1 -0
- package/dist/cjs/learningContentPlan.js +305 -0
- package/dist/cjs/learningContentThread.d.ts +184 -0
- package/dist/cjs/learningContentThread.d.ts.map +1 -0
- package/dist/cjs/learningContentThread.js +407 -0
- package/dist/cjs/logger.d.ts +1 -0
- package/dist/cjs/logger.d.ts.map +1 -1
- package/dist/cjs/logger.js +17 -3
- package/dist/cjs/modelRates.d.ts +40 -0
- package/dist/cjs/modelRates.d.ts.map +1 -0
- package/dist/cjs/modelRates.js +76 -0
- package/dist/cjs/referenceContext.d.ts +4 -145
- package/dist/cjs/referenceContext.d.ts.map +1 -1
- package/dist/cjs/referenceContext.js +16 -251
- package/dist/cjs/retry.d.ts +64 -0
- package/dist/cjs/retry.d.ts.map +1 -0
- package/dist/cjs/retry.js +138 -0
- package/dist/cjs/runtimeContext.d.ts +8 -1
- package/dist/cjs/runtimeContext.d.ts.map +1 -1
- package/dist/cjs/runtimeContext.js +3 -1
- package/dist/cjs/serve.d.ts +3 -2
- package/dist/cjs/serve.d.ts.map +1 -1
- package/dist/cjs/serve.js +32 -2
- package/dist/cjs/sourceStore.d.ts +378 -0
- package/dist/cjs/sourceStore.d.ts.map +1 -0
- package/dist/cjs/sourceStore.js +872 -0
- package/dist/cjs/stackfactorKnowledge.d.ts +91 -7
- package/dist/cjs/stackfactorKnowledge.d.ts.map +1 -1
- package/dist/cjs/stackfactorKnowledge.js +108 -44
- package/dist/cjs/tavily.d.ts +108 -4
- package/dist/cjs/tavily.d.ts.map +1 -1
- package/dist/cjs/tavily.js +242 -21
- package/dist/cjs/usage.d.ts.map +1 -1
- package/dist/cjs/usage.js +9 -15
- package/dist/cjs/version.d.ts +3 -0
- package/dist/cjs/version.d.ts.map +1 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/workflow/bookkeeping.d.ts +78 -0
- package/dist/cjs/workflow/bookkeeping.d.ts.map +1 -0
- package/dist/cjs/workflow/bookkeeping.js +75 -0
- package/dist/cjs/workflow/index.d.ts +13 -0
- package/dist/cjs/workflow/index.d.ts.map +1 -0
- package/dist/cjs/workflow/index.js +51 -0
- package/dist/cjs/workflow/limits.d.ts +52 -0
- package/dist/cjs/workflow/limits.d.ts.map +1 -0
- package/dist/cjs/workflow/limits.js +62 -0
- package/dist/cjs/workflow/platformTransport.d.ts +39 -0
- package/dist/cjs/workflow/platformTransport.d.ts.map +1 -0
- package/dist/cjs/workflow/platformTransport.js +115 -0
- package/dist/cjs/workflow/retrying.d.ts +44 -0
- package/dist/cjs/workflow/retrying.d.ts.map +1 -0
- package/dist/cjs/workflow/retrying.js +82 -0
- package/dist/cjs/workflow/run.d.ts +195 -0
- package/dist/cjs/workflow/run.d.ts.map +1 -0
- package/dist/cjs/workflow/run.js +858 -0
- package/dist/cjs/workflow/testing/index.d.ts +3 -0
- package/dist/cjs/workflow/testing/index.d.ts.map +1 -0
- package/dist/cjs/workflow/testing/index.js +6 -0
- package/dist/cjs/workflow/testing/memoryTransport.d.ts +203 -0
- package/dist/cjs/workflow/testing/memoryTransport.d.ts.map +1 -0
- package/dist/cjs/workflow/testing/memoryTransport.js +1171 -0
- package/dist/cjs/workflow/transport.d.ts +219 -0
- package/dist/cjs/workflow/transport.d.ts.map +1 -0
- package/dist/cjs/workflow/transport.js +19 -0
- package/dist/esm/agentProto.d.ts +13 -3
- package/dist/esm/agentProto.d.ts.map +1 -1
- package/dist/esm/agentProto.js +23 -3
- package/dist/esm/backoff.d.ts +17 -0
- package/dist/esm/backoff.d.ts.map +1 -0
- package/dist/esm/backoff.js +22 -0
- package/dist/esm/cancellation.d.ts +22 -0
- package/dist/esm/cancellation.d.ts.map +1 -0
- package/dist/esm/cancellation.js +27 -0
- package/dist/esm/cli.d.ts +3 -0
- package/dist/esm/cli.d.ts.map +1 -0
- package/dist/esm/cli.js +80 -0
- package/dist/esm/client.d.ts +11 -0
- package/dist/esm/client.d.ts.map +1 -1
- package/dist/esm/client.js +25 -1
- package/dist/esm/const.d.ts +23 -0
- package/dist/esm/const.d.ts.map +1 -1
- package/dist/esm/const.js +23 -0
- package/dist/esm/daemon.d.ts +94 -0
- package/dist/esm/daemon.d.ts.map +1 -0
- package/dist/esm/daemon.js +499 -0
- package/dist/esm/daemonConfig.d.ts +45 -0
- package/dist/esm/daemonConfig.d.ts.map +1 -0
- package/dist/esm/daemonConfig.js +142 -0
- package/dist/esm/daemonEnv.d.ts +10 -0
- package/dist/esm/daemonEnv.d.ts.map +1 -0
- package/dist/esm/daemonEnv.js +9 -0
- package/dist/esm/durable/clientTool.d.ts +39 -0
- package/dist/esm/durable/clientTool.d.ts.map +1 -0
- package/dist/esm/durable/clientTool.js +82 -0
- package/dist/esm/durable/index.d.ts +9 -0
- package/dist/esm/durable/index.d.ts.map +1 -0
- package/dist/esm/durable/index.js +8 -0
- package/dist/esm/durable/journal.d.ts +218 -0
- package/dist/esm/durable/journal.d.ts.map +1 -0
- package/dist/esm/durable/journal.js +374 -0
- package/dist/esm/durable/pause.d.ts +84 -0
- package/dist/esm/durable/pause.d.ts.map +1 -0
- package/dist/esm/durable/pause.js +226 -0
- package/dist/esm/durable/resume.d.ts +27 -0
- package/dist/esm/durable/resume.d.ts.map +1 -0
- package/dist/esm/durable/resume.js +72 -0
- package/dist/esm/durable/snapshotSaver.d.ts +38 -0
- package/dist/esm/durable/snapshotSaver.d.ts.map +1 -0
- package/dist/esm/durable/snapshotSaver.js +77 -0
- package/dist/esm/durable/step.d.ts +28 -0
- package/dist/esm/durable/step.d.ts.map +1 -0
- package/dist/esm/durable/step.js +36 -0
- package/dist/esm/index.d.ts +30 -7
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +20 -4
- package/dist/esm/infrastructure.d.ts +50 -0
- package/dist/esm/infrastructure.d.ts.map +1 -0
- package/dist/esm/infrastructure.js +156 -0
- package/dist/esm/langChain.d.ts +15 -3
- package/dist/esm/langChain.d.ts.map +1 -1
- package/dist/esm/langChain.js +984 -179
- package/dist/esm/learningActivityMedia.d.ts +196 -0
- package/dist/esm/learningActivityMedia.d.ts.map +1 -0
- package/dist/esm/learningActivityMedia.js +807 -0
- package/dist/esm/learningContentPlan.d.ts +105 -0
- package/dist/esm/learningContentPlan.d.ts.map +1 -0
- package/dist/esm/learningContentPlan.js +299 -0
- package/dist/esm/learningContentThread.d.ts +184 -0
- package/dist/esm/learningContentThread.d.ts.map +1 -0
- package/dist/esm/learningContentThread.js +364 -0
- package/dist/esm/logger.d.ts +1 -0
- package/dist/esm/logger.d.ts.map +1 -1
- package/dist/esm/logger.js +17 -3
- package/dist/esm/modelRates.d.ts +40 -0
- package/dist/esm/modelRates.d.ts.map +1 -0
- package/dist/esm/modelRates.js +68 -0
- package/dist/esm/referenceContext.d.ts +4 -145
- package/dist/esm/referenceContext.d.ts.map +1 -1
- package/dist/esm/referenceContext.js +16 -243
- package/dist/esm/retry.d.ts +64 -0
- package/dist/esm/retry.d.ts.map +1 -0
- package/dist/esm/retry.js +127 -0
- package/dist/esm/runtimeContext.d.ts +8 -1
- package/dist/esm/runtimeContext.d.ts.map +1 -1
- package/dist/esm/runtimeContext.js +3 -1
- package/dist/esm/serve.d.ts +3 -2
- package/dist/esm/serve.d.ts.map +1 -1
- package/dist/esm/serve.js +33 -3
- package/dist/esm/sourceStore.d.ts +378 -0
- package/dist/esm/sourceStore.d.ts.map +1 -0
- package/dist/esm/sourceStore.js +855 -0
- package/dist/esm/stackfactorKnowledge.d.ts +91 -7
- package/dist/esm/stackfactorKnowledge.d.ts.map +1 -1
- package/dist/esm/stackfactorKnowledge.js +101 -41
- package/dist/esm/tavily.d.ts +108 -4
- package/dist/esm/tavily.d.ts.map +1 -1
- package/dist/esm/tavily.js +237 -20
- package/dist/esm/usage.d.ts.map +1 -1
- package/dist/esm/usage.js +9 -15
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/workflow/bookkeeping.d.ts +78 -0
- package/dist/esm/workflow/bookkeeping.d.ts.map +1 -0
- package/dist/esm/workflow/bookkeeping.js +67 -0
- package/dist/esm/workflow/index.d.ts +13 -0
- package/dist/esm/workflow/index.d.ts.map +1 -0
- package/dist/esm/workflow/index.js +10 -0
- package/dist/esm/workflow/limits.d.ts +52 -0
- package/dist/esm/workflow/limits.d.ts.map +1 -0
- package/dist/esm/workflow/limits.js +59 -0
- package/dist/esm/workflow/platformTransport.d.ts +39 -0
- package/dist/esm/workflow/platformTransport.d.ts.map +1 -0
- package/dist/esm/workflow/platformTransport.js +110 -0
- package/dist/esm/workflow/retrying.d.ts +44 -0
- package/dist/esm/workflow/retrying.d.ts.map +1 -0
- package/dist/esm/workflow/retrying.js +73 -0
- package/dist/esm/workflow/run.d.ts +195 -0
- package/dist/esm/workflow/run.d.ts.map +1 -0
- package/dist/esm/workflow/run.js +850 -0
- package/dist/esm/workflow/testing/index.d.ts +3 -0
- package/dist/esm/workflow/testing/index.d.ts.map +1 -0
- package/dist/esm/workflow/testing/index.js +2 -0
- package/dist/esm/workflow/testing/memoryTransport.d.ts +203 -0
- package/dist/esm/workflow/testing/memoryTransport.d.ts.map +1 -0
- package/dist/esm/workflow/testing/memoryTransport.js +1167 -0
- package/dist/esm/workflow/transport.d.ts +219 -0
- package/dist/esm/workflow/transport.d.ts.map +1 -0
- package/dist/esm/workflow/transport.js +16 -0
- package/package.json +8 -7
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The pool daemon's configuration, read from flags and environment.
|
|
4
|
+
* A flag wins over its variable. Junk is refused here, before any request.
|
|
5
|
+
* No message names more of the token than `sfp_<poolId>`.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.poolOf = exports.readDaemonConfig = exports.DaemonConfigError = void 0;
|
|
9
|
+
/** Thrown for a configuration the daemon must not start with. */
|
|
10
|
+
class DaemonConfigError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "DaemonConfigError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.DaemonConfigError = DaemonConfigError;
|
|
17
|
+
const DEPLOYMENT_PATTERN = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
18
|
+
const TOKEN_PREFIX = "sfp_";
|
|
19
|
+
/** The characters a pool token is made of (hex ids and a base64url secret, joined by `_`). */
|
|
20
|
+
const TOKEN_CHARACTERS = /^[A-Za-z0-9_-]+$/;
|
|
21
|
+
/** What switches the infrastructure report off, and on. */
|
|
22
|
+
const OFF = ["false", "0", "no", "off"];
|
|
23
|
+
const ON = ["true", "1", "yes", "on"];
|
|
24
|
+
const CLOUDS = ["gcp", "aws", "azure", "other"];
|
|
25
|
+
const FLAGS = {
|
|
26
|
+
"--pool": "AGENT_POOL_TOKEN",
|
|
27
|
+
"--installation-name": "AGENT_POOL_INSTALLATION",
|
|
28
|
+
"--backend": "AGENT_POOL_BACKEND_URL",
|
|
29
|
+
};
|
|
30
|
+
const isFlag = (name) => Object.prototype.hasOwnProperty.call(FLAGS, name);
|
|
31
|
+
const parseFlags = (argv) => {
|
|
32
|
+
const found = {};
|
|
33
|
+
for (let i = 0; i < argv.length; i++) {
|
|
34
|
+
const arg = argv[i];
|
|
35
|
+
if (!arg.startsWith("--")) {
|
|
36
|
+
throw new DaemonConfigError("Unexpected argument; flags start with --.");
|
|
37
|
+
}
|
|
38
|
+
const eq = arg.indexOf("=");
|
|
39
|
+
const name = eq === -1 ? arg : arg.slice(0, eq);
|
|
40
|
+
if (!isFlag(name)) {
|
|
41
|
+
throw new DaemonConfigError(/^--[a-z][a-z-]*$/.test(name) ? `Unknown flag ${name}.` : "Unknown flag.");
|
|
42
|
+
}
|
|
43
|
+
let value;
|
|
44
|
+
if (eq !== -1) {
|
|
45
|
+
value = arg.slice(eq + 1);
|
|
46
|
+
// An empty value is not "use the variable instead": that would hide a typo.
|
|
47
|
+
if (value === "")
|
|
48
|
+
throw new DaemonConfigError(`${name} needs a value.`);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const next = argv[i + 1];
|
|
52
|
+
if (next === undefined || next.startsWith("--")) {
|
|
53
|
+
throw new DaemonConfigError(`${name} needs a value.`);
|
|
54
|
+
}
|
|
55
|
+
value = next;
|
|
56
|
+
i++;
|
|
57
|
+
}
|
|
58
|
+
found[name] = value;
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
};
|
|
62
|
+
const nonEmpty = (value) => value === undefined || value === "" ? undefined : value;
|
|
63
|
+
/**
|
|
64
|
+
* Reads the daemon's configuration.
|
|
65
|
+
* @throws DaemonConfigError for no token, a token not starting `sfp_` or
|
|
66
|
+
* holding a character no token has, a malformed installation name, no backend
|
|
67
|
+
* URL or one that is not a plain http(s) URL, an `AGENT_POOL_REPORT_INFRASTRUCTURE`
|
|
68
|
+
* that is neither true nor false, an unknown flag or a flag with no value.
|
|
69
|
+
*/
|
|
70
|
+
const readDaemonConfig = (argv, env) => {
|
|
71
|
+
const flags = parseFlags(argv);
|
|
72
|
+
const warnings = [];
|
|
73
|
+
// Trimmed: a token read from a file often ends in a line break, which the
|
|
74
|
+
// HTTP client drops; the daemon must redact the token as it is sent.
|
|
75
|
+
const poolToken = nonEmpty(flags["--pool"]?.trim()) ?? nonEmpty(env.AGENT_POOL_TOKEN?.trim());
|
|
76
|
+
if (!poolToken) {
|
|
77
|
+
throw new DaemonConfigError("No pool token: set AGENT_POOL_TOKEN (the --pool flag also works).");
|
|
78
|
+
}
|
|
79
|
+
if (!poolToken.startsWith(TOKEN_PREFIX) || poolToken.split("_")[1] === "") {
|
|
80
|
+
throw new DaemonConfigError(`The pool token (--pool / AGENT_POOL_TOKEN) must start with "${TOKEN_PREFIX}".`);
|
|
81
|
+
}
|
|
82
|
+
if (!TOKEN_CHARACTERS.test(poolToken)) {
|
|
83
|
+
throw new DaemonConfigError("The pool token (--pool / AGENT_POOL_TOKEN) holds a character no pool token has (a space, a quote or a line break inside it).");
|
|
84
|
+
}
|
|
85
|
+
if (nonEmpty(flags["--pool"])) {
|
|
86
|
+
warnings.push("A command-line argument is visible to other users of this host: --pool exposes the token; set AGENT_POOL_TOKEN instead.");
|
|
87
|
+
}
|
|
88
|
+
const deployment = nonEmpty(flags["--installation-name"]) ?? nonEmpty(env.AGENT_POOL_INSTALLATION);
|
|
89
|
+
if (deployment !== undefined && !DEPLOYMENT_PATTERN.test(deployment)) {
|
|
90
|
+
throw new DaemonConfigError("The installation name (--installation-name / AGENT_POOL_INSTALLATION) must be 1-63 characters of a-z, 0-9 and -, starting and ending with a letter or digit.");
|
|
91
|
+
}
|
|
92
|
+
const backendUrl = nonEmpty(flags["--backend"]) ?? nonEmpty(env.AGENT_POOL_BACKEND_URL);
|
|
93
|
+
if (backendUrl === undefined) {
|
|
94
|
+
throw new DaemonConfigError("No backend URL: set AGENT_POOL_BACKEND_URL (or --backend) to the StackFactor backend that issued the pool token. There is no default.");
|
|
95
|
+
}
|
|
96
|
+
let parsed;
|
|
97
|
+
try {
|
|
98
|
+
parsed = new URL(backendUrl);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
parsed = undefined;
|
|
102
|
+
}
|
|
103
|
+
if (parsed?.protocol !== "http:" && parsed?.protocol !== "https:") {
|
|
104
|
+
throw new DaemonConfigError("The backend URL (--backend / AGENT_POOL_BACKEND_URL) must be an http: or https: URL.");
|
|
105
|
+
}
|
|
106
|
+
// Credentials in the URL would replace the pool token on every request, and a
|
|
107
|
+
// query or fragment would end up inside every route's path.
|
|
108
|
+
if (parsed.username !== "" || parsed.password !== "" || parsed.search !== "" || parsed.hash !== "") {
|
|
109
|
+
throw new DaemonConfigError("The backend URL (--backend / AGENT_POOL_BACKEND_URL) must not carry credentials, a query or a fragment.");
|
|
110
|
+
}
|
|
111
|
+
const overrides = {};
|
|
112
|
+
const cloud = nonEmpty(env.AGENT_POOL_CLOUD)?.toLowerCase();
|
|
113
|
+
if (cloud !== undefined && !CLOUDS.includes(cloud)) {
|
|
114
|
+
throw new DaemonConfigError(`AGENT_POOL_CLOUD must be one of ${CLOUDS.join(", ")}.`);
|
|
115
|
+
}
|
|
116
|
+
const region = nonEmpty(env.AGENT_POOL_REGION);
|
|
117
|
+
const account = nonEmpty(env.AGENT_POOL_ACCOUNT);
|
|
118
|
+
const cluster = nonEmpty(env.AGENT_POOL_CLUSTER);
|
|
119
|
+
if (cloud)
|
|
120
|
+
overrides.cloud = cloud;
|
|
121
|
+
if (region)
|
|
122
|
+
overrides.region = region;
|
|
123
|
+
if (account)
|
|
124
|
+
overrides.account = account;
|
|
125
|
+
if (cluster)
|
|
126
|
+
overrides.cluster = cluster;
|
|
127
|
+
const report = nonEmpty(env.AGENT_POOL_REPORT_INFRASTRUCTURE?.trim().toLowerCase());
|
|
128
|
+
if (report !== undefined && !OFF.includes(report) && !ON.includes(report)) {
|
|
129
|
+
// Refused, not guessed: a misspelt opt-out must not go on reporting.
|
|
130
|
+
throw new DaemonConfigError("AGENT_POOL_REPORT_INFRASTRUCTURE must be true or false.");
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
poolToken,
|
|
134
|
+
deployment,
|
|
135
|
+
backendUrl,
|
|
136
|
+
reportInfrastructure: report === undefined || ON.includes(report),
|
|
137
|
+
overrides,
|
|
138
|
+
warnings,
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
exports.readDaemonConfig = readDaemonConfig;
|
|
142
|
+
/** `sfp_<poolId>` for logs: never more of the token than that. */
|
|
143
|
+
const poolOf = (token) => {
|
|
144
|
+
const [prefix, poolId] = token.split("_");
|
|
145
|
+
return prefix === "sfp" && poolId ? `sfp_${poolId}` : "sfp_?";
|
|
146
|
+
};
|
|
147
|
+
exports.poolOf = poolOf;
|
|
148
|
+
exports.default = exports.readDaemonConfig;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment the process was started with, captured before any other
|
|
3
|
+
* module of this package loads. @stackfactor/client-api reads a `.env` from the
|
|
4
|
+
* working directory into `process.env` when it is imported; the daemon's
|
|
5
|
+
* configuration, the backend that receives the pool token above all, comes
|
|
6
|
+
* from the real environment and the flags only (R-C10). The command line
|
|
7
|
+
* imports this module first.
|
|
8
|
+
*/
|
|
9
|
+
export declare const startEnv: NodeJS.ProcessEnv;
|
|
10
|
+
//# sourceMappingURL=daemonEnv.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"daemonEnv.d.ts","sourceRoot":"","sources":["../../src/daemonEnv.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,UAA+B,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startEnv = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The environment the process was started with, captured before any other
|
|
6
|
+
* module of this package loads. @stackfactor/client-api reads a `.env` from the
|
|
7
|
+
* working directory into `process.env` when it is imported; the daemon's
|
|
8
|
+
* configuration, the backend that receives the pool token above all, comes
|
|
9
|
+
* from the real environment and the flags only (R-C10). The command line
|
|
10
|
+
* imports this module first.
|
|
11
|
+
*/
|
|
12
|
+
exports.startEnv = { ...process.env };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { StructuredTool } from "@langchain/core/tools";
|
|
2
|
+
/** What a client tool interrupts with: the pause makes it a `tool` question. */
|
|
3
|
+
export interface ClientToolCall {
|
|
4
|
+
kind: "tool";
|
|
5
|
+
name: string;
|
|
6
|
+
args: Record<string, unknown>;
|
|
7
|
+
description: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ClientToolSpec {
|
|
10
|
+
/** 1 to 128 letters, digits, "_", "." or "-". */
|
|
11
|
+
name: string;
|
|
12
|
+
/** What the tool does, for the model and the client; at most 2,000 characters. */
|
|
13
|
+
description: string;
|
|
14
|
+
/** The tool's arguments, a zod object. */
|
|
15
|
+
schema: any;
|
|
16
|
+
}
|
|
17
|
+
/** Whether a tool was made by {@link clientTool}. */
|
|
18
|
+
export declare const isClientTool: (candidate: any) => boolean;
|
|
19
|
+
/**
|
|
20
|
+
* `durable.clientTool({ name, description, schema })`: a LangChain tool whose
|
|
21
|
+
* body runs on the caller's machine. Called by the model inside an agentic
|
|
22
|
+
* `runPromptWithModel` of a `workflow.run` turn, it interrupts the thread
|
|
23
|
+
* with `{ kind: "tool", name, args, description }`; the turn pauses with a
|
|
24
|
+
* tool wait (one `tool` question per call, several calls of one model step in
|
|
25
|
+
* one wait), and the resume feeds each answer to its paused call. A `result`
|
|
26
|
+
* is the tool's output (a string as it is, anything else as JSON); an `error`
|
|
27
|
+
* is a tool error the model sees, carrying its message (and code). The
|
|
28
|
+
* client's answers are data from the caller's machine, never instructions.
|
|
29
|
+
*
|
|
30
|
+
* `config.clientTools` sets the wait's `deadline` (seconds, default
|
|
31
|
+
* `TOOL_WAIT_DEFAULT_DEADLINE_SECONDS`) and `addressees` (default the
|
|
32
|
+
* workflow's creator); a client tool among the tools turns the wait on with
|
|
33
|
+
* those defaults.
|
|
34
|
+
*
|
|
35
|
+
* @param spec - The tool's name, description and zod object schema
|
|
36
|
+
* @returns The tool, to pass in `runPromptWithModel`'s tools
|
|
37
|
+
*/
|
|
38
|
+
export declare const clientTool: (spec: ClientToolSpec) => StructuredTool;
|
|
39
|
+
//# sourceMappingURL=clientTool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clientTool.d.ts","sourceRoot":"","sources":["../../../src/durable/clientTool.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAkB5D,gFAAgF;AAChF,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,kFAAkF;IAClF,WAAW,EAAE,MAAM,CAAC;IACpB,0CAA0C;IAC1C,MAAM,EAAE,GAAG,CAAC;CACb;AAQD,qDAAqD;AACrD,eAAO,MAAM,YAAY,GAAI,WAAW,GAAG,KAAG,OAAsD,CAAC;AAUrG;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,cAAc,KAAG,cAsCjD,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.clientTool = exports.isClientTool = void 0;
|
|
7
|
+
const langchain_1 = require("langchain");
|
|
8
|
+
const langgraph_1 = require("@langchain/langgraph");
|
|
9
|
+
const messages_1 = require("@langchain/core/messages");
|
|
10
|
+
const const_js_1 = __importDefault(require("../const.js"));
|
|
11
|
+
const limits_js_1 = require("../workflow/limits.js");
|
|
12
|
+
const journal_js_1 = require("./journal.js");
|
|
13
|
+
// A tool whose body runs on the caller's machine (interactive tool calls §2,
|
|
14
|
+
// I2). Built on interrupt() inside the tool: spike item 1b showed that the
|
|
15
|
+
// interrupt, exported with a SnapshotSaver and resumed in a fresh process
|
|
16
|
+
// with Command({ resume }), gives the tool's caller the answer exactly once
|
|
17
|
+
// and the model goes on.
|
|
18
|
+
/** Tool names, as the platform validates a `tool` question's (I2). */
|
|
19
|
+
const TOOL_NAME = /^[A-Za-z0-9_.-]+$/;
|
|
20
|
+
/** A `tool` question's description, at most (I2). */
|
|
21
|
+
const TOOL_DESCRIPTION_MAX_CHARS = 2000;
|
|
22
|
+
/** The metadata flag that marks a client tool. */
|
|
23
|
+
const CLIENT_TOOL = "stackfactorClientTool";
|
|
24
|
+
const INTERNAL = const_js_1.default.HTTP_CODES.INTERNAL_SERVER_ERROR;
|
|
25
|
+
/** A zod object schema, of zod 4 or 3, whichever copy the agent imported. */
|
|
26
|
+
const isZodObject = (schema) => schema?._zod?.def?.type === "object" || schema?._def?.typeName === "ZodObject";
|
|
27
|
+
/** Whether a tool was made by {@link clientTool}. */
|
|
28
|
+
const isClientTool = (candidate) => candidate?.metadata?.[CLIENT_TOOL] === true;
|
|
29
|
+
exports.isClientTool = isClientTool;
|
|
30
|
+
/** What the model is told when the client answered with an error, or with neither a result nor an error. */
|
|
31
|
+
const errorText = (name, answer) => {
|
|
32
|
+
const error = answer && typeof answer === "object" ? answer.error : undefined;
|
|
33
|
+
if (!error || typeof error !== "object")
|
|
34
|
+
return `the client answered ${name} with neither a result nor an error`;
|
|
35
|
+
const message = String(error.message ?? "").slice(0, limits_js_1.LIMITS.TOOL_ERROR_MESSAGE_MAX_CHARS) || `${name} failed`;
|
|
36
|
+
return error.code ? `${message} (code ${error.code})` : message;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* `durable.clientTool({ name, description, schema })`: a LangChain tool whose
|
|
40
|
+
* body runs on the caller's machine. Called by the model inside an agentic
|
|
41
|
+
* `runPromptWithModel` of a `workflow.run` turn, it interrupts the thread
|
|
42
|
+
* with `{ kind: "tool", name, args, description }`; the turn pauses with a
|
|
43
|
+
* tool wait (one `tool` question per call, several calls of one model step in
|
|
44
|
+
* one wait), and the resume feeds each answer to its paused call. A `result`
|
|
45
|
+
* is the tool's output (a string as it is, anything else as JSON); an `error`
|
|
46
|
+
* is a tool error the model sees, carrying its message (and code). The
|
|
47
|
+
* client's answers are data from the caller's machine, never instructions.
|
|
48
|
+
*
|
|
49
|
+
* `config.clientTools` sets the wait's `deadline` (seconds, default
|
|
50
|
+
* `TOOL_WAIT_DEFAULT_DEADLINE_SECONDS`) and `addressees` (default the
|
|
51
|
+
* workflow's creator); a client tool among the tools turns the wait on with
|
|
52
|
+
* those defaults.
|
|
53
|
+
*
|
|
54
|
+
* @param spec - The tool's name, description and zod object schema
|
|
55
|
+
* @returns The tool, to pass in `runPromptWithModel`'s tools
|
|
56
|
+
*/
|
|
57
|
+
const clientTool = (spec) => {
|
|
58
|
+
const { name, description, schema } = spec ?? {};
|
|
59
|
+
if (typeof name !== "string" || name.length < 1 || name.length > limits_js_1.LIMITS.TOOL_NAME_MAX_CHARS || !TOOL_NAME.test(name)) {
|
|
60
|
+
throw (0, journal_js_1.failure)(INTERNAL, `durable.clientTool needs a name of 1 to ${limits_js_1.LIMITS.TOOL_NAME_MAX_CHARS} letters, digits, "_", "." or "-"`);
|
|
61
|
+
}
|
|
62
|
+
if (typeof description !== "string" || description.length > TOOL_DESCRIPTION_MAX_CHARS) {
|
|
63
|
+
throw (0, journal_js_1.failure)(INTERNAL, `durable.clientTool ${name} needs a description of at most ${TOOL_DESCRIPTION_MAX_CHARS} characters`);
|
|
64
|
+
}
|
|
65
|
+
if (!isZodObject(schema)) {
|
|
66
|
+
throw (0, journal_js_1.failure)(INTERNAL, `durable.clientTool ${name}: its schema is a zod object, z.object({ ... })`);
|
|
67
|
+
}
|
|
68
|
+
return (0, langchain_1.tool)(async (args, config) => {
|
|
69
|
+
let answer;
|
|
70
|
+
try {
|
|
71
|
+
answer = (0, langgraph_1.interrupt)({ kind: "tool", name, args, description });
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if ((0, langgraph_1.isGraphInterrupt)(error))
|
|
75
|
+
throw error;
|
|
76
|
+
if (error?.lc_error_code === "MISSING_CHECKPOINTER") {
|
|
77
|
+
throw (0, journal_js_1.failure)(INTERNAL, `${name} was called by a graph with no checkpointer: an agent that calls client tools needs a durable.SnapshotSaver, which runPromptWithModel's agentic mode gives it in a workflow.run turn`);
|
|
78
|
+
}
|
|
79
|
+
throw (0, journal_js_1.failure)(INTERNAL, `${name} is a client tool: it runs only inside an agentic runPromptWithModel of a workflow.run turn, which pauses for the caller to run it`);
|
|
80
|
+
}
|
|
81
|
+
if (answer && typeof answer === "object" && Object.prototype.hasOwnProperty.call(answer, "result")) {
|
|
82
|
+
return typeof answer.result === "string" ? answer.result : JSON.stringify(answer.result ?? null);
|
|
83
|
+
}
|
|
84
|
+
const message = errorText(name, answer);
|
|
85
|
+
if (!config?.toolCallId)
|
|
86
|
+
throw new Error(message);
|
|
87
|
+
return new messages_1.ToolMessage({ content: message, tool_call_id: config.toolCallId, name, status: "error" });
|
|
88
|
+
}, { name, description, schema, metadata: { [CLIENT_TOOL]: true } });
|
|
89
|
+
};
|
|
90
|
+
exports.clientTool = clientTool;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { step } from "./step.js";
|
|
2
|
+
export type { StepOptions } from "./step.js";
|
|
3
|
+
export { clientTool } from "./clientTool.js";
|
|
4
|
+
export type { ClientToolSpec, ClientToolCall } from "./clientTool.js";
|
|
5
|
+
export { SnapshotSaver } from "./snapshotSaver.js";
|
|
6
|
+
export type { SavedThreads, SavedCheckpoint } from "./snapshotSaver.js";
|
|
7
|
+
export { DurableReplayMismatchError, DurablePause } from "./journal.js";
|
|
8
|
+
export type { JournalEntry, UnitUsage } from "./journal.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/durable/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAAE,0BAA0B,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACxE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DurablePause = exports.DurableReplayMismatchError = exports.SnapshotSaver = exports.clientTool = exports.step = void 0;
|
|
4
|
+
// The `durable` module (design #964 §10.2, W10): what lets a `workflow.run`
|
|
5
|
+
// turn stop inside an LLM tool loop and continue from there on the answer.
|
|
6
|
+
// `durable.step` journals the turn's own side effects; the turn's LLM calls
|
|
7
|
+
// are journaled by `langChain` itself.
|
|
8
|
+
var step_js_1 = require("./step.js");
|
|
9
|
+
Object.defineProperty(exports, "step", { enumerable: true, get: function () { return step_js_1.step; } });
|
|
10
|
+
var clientTool_js_1 = require("./clientTool.js");
|
|
11
|
+
Object.defineProperty(exports, "clientTool", { enumerable: true, get: function () { return clientTool_js_1.clientTool; } });
|
|
12
|
+
var snapshotSaver_js_1 = require("./snapshotSaver.js");
|
|
13
|
+
Object.defineProperty(exports, "SnapshotSaver", { enumerable: true, get: function () { return snapshotSaver_js_1.SnapshotSaver; } });
|
|
14
|
+
var journal_js_1 = require("./journal.js");
|
|
15
|
+
Object.defineProperty(exports, "DurableReplayMismatchError", { enumerable: true, get: function () { return journal_js_1.DurableReplayMismatchError; } });
|
|
16
|
+
Object.defineProperty(exports, "DurablePause", { enumerable: true, get: function () { return journal_js_1.DurablePause; } });
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { AppError } from "../errorHandling.js";
|
|
2
|
+
/** The units the journal keeps: `llm#n`, `agent#n` (can pause) and `step:<key>`. */
|
|
3
|
+
export type UnitName = "llm" | "agent" | "step";
|
|
4
|
+
/** A unit's usage, in the UsageTracker shape (design §5.5): what replay gives back to the caller's tracker. */
|
|
5
|
+
export interface UnitUsage {
|
|
6
|
+
cost: number;
|
|
7
|
+
tokens: Record<string, number>;
|
|
8
|
+
webSearchSources?: {
|
|
9
|
+
url: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
}[];
|
|
12
|
+
attemptedUrls?: string[];
|
|
13
|
+
}
|
|
14
|
+
/** One finished unit as the snapshot keeps it. */
|
|
15
|
+
export interface JournalEntry {
|
|
16
|
+
name: UnitName;
|
|
17
|
+
result?: unknown;
|
|
18
|
+
usage?: UnitUsage;
|
|
19
|
+
}
|
|
20
|
+
/** An agent unit paused at the last pause: what it had counted, so its resume counts only what is new, and what it was asked. */
|
|
21
|
+
export interface PausedUnit {
|
|
22
|
+
usage: UnitUsage;
|
|
23
|
+
/** The ids of the messages whose usage was counted. */
|
|
24
|
+
counted: string[];
|
|
25
|
+
/** Every interrupt it stopped on. */
|
|
26
|
+
interrupts: Interrupt[];
|
|
27
|
+
/**
|
|
28
|
+
* The ids of the interrupts its wait asked: one wait asks at most
|
|
29
|
+
* MAX_QUESTIONS_PER_WAIT, so the others are asked when these are answered
|
|
30
|
+
* (LangGraph interrupts again on each call it got no answer for).
|
|
31
|
+
*/
|
|
32
|
+
asked: string[];
|
|
33
|
+
options: PauseOptions;
|
|
34
|
+
}
|
|
35
|
+
/** One LangGraph interrupt, as `__interrupt__` lists it. */
|
|
36
|
+
export interface Interrupt {
|
|
37
|
+
id: string;
|
|
38
|
+
value: unknown;
|
|
39
|
+
}
|
|
40
|
+
/** Where a pause waits, as `config.approvals` and `config.clientTools` say. */
|
|
41
|
+
export interface PauseOptions {
|
|
42
|
+
approvals?: {
|
|
43
|
+
addressees?: string[];
|
|
44
|
+
deadline?: string;
|
|
45
|
+
} | null;
|
|
46
|
+
clientTools?: {
|
|
47
|
+
addressees?: string[];
|
|
48
|
+
deadline?: number;
|
|
49
|
+
} | null;
|
|
50
|
+
}
|
|
51
|
+
/** An agent unit that stopped on interrupts: its questions are this turn's pause. */
|
|
52
|
+
export interface Interruption {
|
|
53
|
+
key: string;
|
|
54
|
+
interrupts: Interrupt[];
|
|
55
|
+
/** The unit's paused thread, `SnapshotSaver.exportThreads` layout for its one thread. */
|
|
56
|
+
thread: unknown;
|
|
57
|
+
usage: UnitUsage;
|
|
58
|
+
counted: string[];
|
|
59
|
+
options: PauseOptions;
|
|
60
|
+
}
|
|
61
|
+
/** What a journaled unit's body is handed. */
|
|
62
|
+
export interface UnitContext {
|
|
63
|
+
key: string;
|
|
64
|
+
/** Set when the unit paused at the last pause and runs again on its answers. */
|
|
65
|
+
paused: PausedUnit | null;
|
|
66
|
+
/** The paused unit's thread, as the snapshot kept it. */
|
|
67
|
+
thread: unknown;
|
|
68
|
+
/** The answers to the paused unit's interrupts, by interrupt id. */
|
|
69
|
+
resume: Record<string, unknown>;
|
|
70
|
+
/** Stops the turn on the unit's interrupts. */
|
|
71
|
+
pause(stop: Omit<Interruption, "key" | "usage">): never;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The paused turn ran again and took a different path: a unit at a key the
|
|
75
|
+
* journal holds is not the unit the journal saved there (the agent was
|
|
76
|
+
* republished between the pause and the resume). A 500 that fails the run
|
|
77
|
+
* rather than guess (design §12).
|
|
78
|
+
*/
|
|
79
|
+
export declare class DurableReplayMismatchError extends AppError {
|
|
80
|
+
readonly key: string;
|
|
81
|
+
readonly expected: string;
|
|
82
|
+
readonly found: string;
|
|
83
|
+
constructor(key: string, expected: string, found: string);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Thrown to unwind a turn that paused (an interrupt in an LLM tool loop): the
|
|
87
|
+
* runner catches it, saves the conversation checkpoint and releases with the
|
|
88
|
+
* pause's input wait. Agent code lets it through.
|
|
89
|
+
*/
|
|
90
|
+
export declare class DurablePause extends Error {
|
|
91
|
+
constructor();
|
|
92
|
+
}
|
|
93
|
+
/** A refusal with a status, as `errorHandling.create` makes them. */
|
|
94
|
+
export declare const failure: (code: number, message: string) => AppError;
|
|
95
|
+
export declare const emptyUsage: () => UnitUsage;
|
|
96
|
+
/**
|
|
97
|
+
* Gives a replayed unit's usage back to the caller's tracker, and only to it:
|
|
98
|
+
* the platform's usage ledger (`recordUsage`) was told when it was spent.
|
|
99
|
+
*/
|
|
100
|
+
export declare const reapplyUsage: (tracker: any, usage: UnitUsage | null | undefined) => void;
|
|
101
|
+
interface Scope {
|
|
102
|
+
prefix: string;
|
|
103
|
+
seq: number;
|
|
104
|
+
}
|
|
105
|
+
export interface Frame {
|
|
106
|
+
turn: DurableTurn;
|
|
107
|
+
scope: Scope;
|
|
108
|
+
/** What the unit this frame runs counted; null for the turn's own frame. */
|
|
109
|
+
usage: UnitUsage | null;
|
|
110
|
+
parent: Frame | null;
|
|
111
|
+
/**
|
|
112
|
+
* Inside an agent unit: its thread keeps what its tools did, so nothing
|
|
113
|
+
* under it is journaled. A tool that finished is never run again; one that
|
|
114
|
+
* was interrupted runs again from its start (LangGraph's rule).
|
|
115
|
+
*/
|
|
116
|
+
passthrough: boolean;
|
|
117
|
+
}
|
|
118
|
+
/** The turn a call runs in, when it may journal and pause: inside a `workflow.run` turn, outside an agent's tools. */
|
|
119
|
+
export declare const activeTurn: () => DurableTurn | null;
|
|
120
|
+
/** Counts usage into every unit the call runs in (`countUsage` and the Tavily tools call this beside the tracker). */
|
|
121
|
+
export declare const noteUsage: (key: string, amount: number) => void;
|
|
122
|
+
/** The web sources a Tavily call returned, into every unit the call runs in. */
|
|
123
|
+
export declare const noteSources: (sources: {
|
|
124
|
+
url: string;
|
|
125
|
+
title?: string;
|
|
126
|
+
}[]) => void;
|
|
127
|
+
/** The URLs `web_extract` asked for, into every unit the call runs in. */
|
|
128
|
+
export declare const noteAttempted: (urls: string[]) => void;
|
|
129
|
+
/** The journal part of a snapshot (design §5.5). */
|
|
130
|
+
export interface JournalSnapshot {
|
|
131
|
+
entries: Record<string, JournalEntry>;
|
|
132
|
+
answers: Record<string, unknown>;
|
|
133
|
+
/** Agent units paused at this pause, by key: what each had counted. */
|
|
134
|
+
paused?: Record<string, PausedUnit>;
|
|
135
|
+
}
|
|
136
|
+
export interface DurableTurnInit {
|
|
137
|
+
journal?: JournalSnapshot | null;
|
|
138
|
+
/** The paused units' threads, by unit key. */
|
|
139
|
+
threads?: Record<string, unknown> | null;
|
|
140
|
+
/** Answers by question id (`g:<key>:<interruptId>`), merged over the journal's. */
|
|
141
|
+
answers?: Record<string, unknown> | null;
|
|
142
|
+
/** Who a pause's wait is addressed to by default: the workflow's creator. */
|
|
143
|
+
creator?: string | null;
|
|
144
|
+
now?: () => number;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* One turn's journal and what it paused on. The runner makes one per turn and
|
|
148
|
+
* runs the turn in it; `langChain` journals its calls through it and
|
|
149
|
+
* `durable.step` its steps.
|
|
150
|
+
*/
|
|
151
|
+
export declare class DurableTurn {
|
|
152
|
+
private readonly entries;
|
|
153
|
+
private readonly paused;
|
|
154
|
+
private readonly threads;
|
|
155
|
+
private readonly answers;
|
|
156
|
+
private readonly used;
|
|
157
|
+
private readonly inflight;
|
|
158
|
+
/** The interrupts this turn stopped on; one wait asks them all. */
|
|
159
|
+
readonly interruptions: Interruption[];
|
|
160
|
+
readonly creator: string | null;
|
|
161
|
+
readonly now: () => number;
|
|
162
|
+
/**
|
|
163
|
+
* Whether the turn runs on an event's input, whose run does not know the
|
|
164
|
+
* workflow's creator: a pause must then name its addressees (Ruling 34), a
|
|
165
|
+
* rule `checkPauseSettings` enforces before the model is called. The runner
|
|
166
|
+
* sets it once the turn's input is known (a resumed turn's is the paused
|
|
167
|
+
* turn's own).
|
|
168
|
+
*/
|
|
169
|
+
event: boolean;
|
|
170
|
+
/** Units that returned their saved result instead of running. */
|
|
171
|
+
hits: number;
|
|
172
|
+
constructor(init?: DurableTurnInit);
|
|
173
|
+
/** Whether a unit of this turn stopped on an interrupt: nothing new starts after it. */
|
|
174
|
+
get pausing(): boolean;
|
|
175
|
+
/** Runs `fn` as this turn: its calls journal here. */
|
|
176
|
+
run<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
177
|
+
private claim;
|
|
178
|
+
/** A unit of the other kind saved at this position of the scope: the turn took another path. */
|
|
179
|
+
private mismatch;
|
|
180
|
+
/** A unit the snapshot holds: what it finished in the run that paused (a key runs once per run, so never one finished in this run). */
|
|
181
|
+
private replay;
|
|
182
|
+
private track;
|
|
183
|
+
/** The answers given to `key`'s interrupts, by interrupt id. */
|
|
184
|
+
private resumeOf;
|
|
185
|
+
/** Forgets what the snapshot held under `key` and every key below it. */
|
|
186
|
+
private forget;
|
|
187
|
+
/**
|
|
188
|
+
* A `llm` or `agent` unit: its saved result when the journal holds one,
|
|
189
|
+
* else `body`, journaled when it finishes.
|
|
190
|
+
*/
|
|
191
|
+
unit<T>(frame: Frame, name: "llm" | "agent", tracker: any, body: (unit: UnitContext) => Promise<T>): Promise<T>;
|
|
192
|
+
/** `durable.step`: its saved result when the journal holds one, else `fn` in a scope of its own. */
|
|
193
|
+
step<T>(frame: Frame, key: string, fn: () => Promise<T>, tracker: any): Promise<T>;
|
|
194
|
+
/**
|
|
195
|
+
* Waits for the units still running when the turn paused, so that what they
|
|
196
|
+
* finish is journaled and not run again on the resume (up to `ms`). Answers
|
|
197
|
+
* the keys of those still running then.
|
|
198
|
+
*/
|
|
199
|
+
drain(ms: number, sleep: (ms: number, signal?: AbortSignal | null) => Promise<void>): Promise<string[]>;
|
|
200
|
+
/** The journal and the paused threads for the snapshot: every finished entry, the paused units, the answers still needed. */
|
|
201
|
+
/**
|
|
202
|
+
* @param asked - The ids (`g:<key>:<interruptId>`) of the questions this
|
|
203
|
+
* pause's wait asks; every interrupt when omitted
|
|
204
|
+
*/
|
|
205
|
+
export(asked?: Set<string>): Promise<{
|
|
206
|
+
journal: JournalSnapshot;
|
|
207
|
+
threads: Record<string, unknown>;
|
|
208
|
+
}>;
|
|
209
|
+
}
|
|
210
|
+
/** The frame a call runs in, or null outside a `workflow.run` turn. */
|
|
211
|
+
export declare const currentFrame: () => Frame | null;
|
|
212
|
+
/**
|
|
213
|
+
* Runs a `llm` or `agent` unit through the turn's journal: outside a turn, or
|
|
214
|
+
* under an agent's tools, `body` runs as it always has.
|
|
215
|
+
*/
|
|
216
|
+
export declare const journaled: <T>(name: "llm" | "agent", tracker: any, body: (unit: UnitContext | null) => Promise<T>) => Promise<T>;
|
|
217
|
+
export {};
|
|
218
|
+
//# sourceMappingURL=journal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"journal.d.ts","sourceRoot":"","sources":["../../../src/durable/journal.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAO/C,oFAAoF;AACpF,MAAM,MAAM,QAAQ,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,CAAC;AAEhD,+GAA+G;AAC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