@stackfactor/agent-utils 1.13.4 → 1.33.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 +688 -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 +117 -0
- package/dist/cjs/learningContentPlan.d.ts.map +1 -0
- package/dist/cjs/learningContentPlan.js +327 -0
- package/dist/cjs/learningContentThread.d.ts +213 -0
- package/dist/cjs/learningContentThread.d.ts.map +1 -0
- package/dist/cjs/learningContentThread.js +448 -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 +117 -0
- package/dist/esm/learningContentPlan.d.ts.map +1 -0
- package/dist/esm/learningContentPlan.js +320 -0
- package/dist/esm/learningContentThread.d.ts +213 -0
- package/dist/esm/learningContentThread.d.ts.map +1 -0
- package/dist/esm/learningContentThread.js +403 -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,226 @@
|
|
|
1
|
+
import { gzipSync } from "node:zlib";
|
|
2
|
+
import constants from "../const.js";
|
|
3
|
+
import { LIMITS } from "../workflow/limits.js";
|
|
4
|
+
import { failure } from "./journal.js";
|
|
5
|
+
// The pause (design #964 §10.2): a turn's interrupts become the questions of
|
|
6
|
+
// one `input` wait, and the turn's journal and paused threads become the
|
|
7
|
+
// conversation checkpoint (§5.5) saved before the wait is declared.
|
|
8
|
+
/** The tag of a pause's wait: `durable:<checkpoint step>`, so its resume finds the snapshot it was declared for. */
|
|
9
|
+
export const PAUSE_TAG_PREFIX = "durable:";
|
|
10
|
+
/** How long a pause waits for the units still running before it saves the checkpoint. */
|
|
11
|
+
export const AGENT_PAUSE_DRAIN_MS = 120_000;
|
|
12
|
+
/** An approval or a question waits this long when config.approvals sets no deadline. */
|
|
13
|
+
export const APPROVAL_DEFAULT_DEADLINE_SECONDS = 7 * 24 * 3600;
|
|
14
|
+
export const SNAPSHOT_FORMAT = "stackfactor.durable-turn";
|
|
15
|
+
export const SNAPSHOT_VERSION = 1;
|
|
16
|
+
const INTERNAL = constants.HTTP_CODES.INTERNAL_SERVER_ERROR;
|
|
17
|
+
/**
|
|
18
|
+
* Waits `ms` of wall time, or less when `signal` aborts: the drain waits for
|
|
19
|
+
* work that is really running, so a test's virtual clock does not apply. The
|
|
20
|
+
* timer does not keep the process alive.
|
|
21
|
+
*/
|
|
22
|
+
export const drainSleep = (ms, signal) => new Promise((resolve) => {
|
|
23
|
+
const timer = setTimeout(resolve, ms);
|
|
24
|
+
timer.unref?.();
|
|
25
|
+
signal?.addEventListener("abort", () => {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
resolve();
|
|
28
|
+
}, { once: true });
|
|
29
|
+
});
|
|
30
|
+
// ── Versions ────────────────────────────────────────────────────────────
|
|
31
|
+
/** `require` in the CommonJS build; in the ES module build, one resolving from the working directory. */
|
|
32
|
+
const nodeRequire = () => {
|
|
33
|
+
if (typeof require === "function")
|
|
34
|
+
return require;
|
|
35
|
+
try {
|
|
36
|
+
return process.getBuiltinModule("node:module").createRequire(`${process.cwd()}/`);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
/** A package's version by the path of its package.json, or "unknown" when it cannot be read. */
|
|
43
|
+
export const versionOf = (id) => {
|
|
44
|
+
try {
|
|
45
|
+
return String(nodeRequire()?.(id)?.version ?? "unknown");
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return "unknown";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The versions a snapshot records: agent-utils' own (read relative to this
|
|
53
|
+
* file, so only in the CommonJS build; "unknown" in the ES module build,
|
|
54
|
+
* where it is informational only) and `@langchain/langgraph` as agent-utils
|
|
55
|
+
* resolves it, whose major a resume checks.
|
|
56
|
+
*/
|
|
57
|
+
export const versions = () => ({
|
|
58
|
+
agentUtils: typeof require === "function" ? versionOf("../../../package.json") : "unknown",
|
|
59
|
+
langgraph: versionOf("@langchain/langgraph/package.json"),
|
|
60
|
+
});
|
|
61
|
+
/** A version's major: the part before the first dot. */
|
|
62
|
+
export const majorOf = (version) => String(version).split(".")[0];
|
|
63
|
+
// ── Questions ───────────────────────────────────────────────────────────
|
|
64
|
+
const clip = (text, max) => (text.length <= max ? text : `${text.slice(0, max - 1)}…`);
|
|
65
|
+
/** A question for a person from a raw interrupt: a string, or `{ question, details?, choices?, allowFreeText? }`. */
|
|
66
|
+
const questionValue = (stop, interrupt) => {
|
|
67
|
+
const value = interrupt.value;
|
|
68
|
+
if (typeof value === "string")
|
|
69
|
+
return { question: value };
|
|
70
|
+
if (value && typeof value === "object" && typeof value.question === "string") {
|
|
71
|
+
return {
|
|
72
|
+
question: value.question,
|
|
73
|
+
...(value.details !== undefined ? { details: value.details } : {}),
|
|
74
|
+
...(value.choices !== undefined ? { choices: value.choices } : {}),
|
|
75
|
+
...(value.allowFreeText !== undefined ? { allowFreeText: value.allowFreeText } : {}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
throw failure(INTERNAL, `durable: interrupt ${interrupt.id} of ${stop.key} asks nothing a person can answer: interrupt() a string or { question, details?, choices?, allowFreeText? }`);
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* One interrupt as a RunQuestion (§6.1, I2): `tool` for a client tool's call
|
|
82
|
+
* (`{ kind: "tool", name, args }`), `approval` when its value has
|
|
83
|
+
* actionRequests (humanInTheLoopMiddleware's HITLRequest), else `question`.
|
|
84
|
+
*/
|
|
85
|
+
const questionOf = (stop, interrupt) => {
|
|
86
|
+
const id = `g:${stop.key}:${interrupt.id}`;
|
|
87
|
+
const value = interrupt.value;
|
|
88
|
+
if (value && typeof value === "object" && value.kind === "tool" && typeof value.name === "string") {
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
kind: "tool",
|
|
92
|
+
value: {
|
|
93
|
+
name: value.name,
|
|
94
|
+
args: value.args && typeof value.args === "object" ? value.args : {},
|
|
95
|
+
...(typeof value.description === "string" && value.description.length > 0 ? { description: value.description } : {}),
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (value && typeof value === "object" && Array.isArray(value.actionRequests)) {
|
|
100
|
+
return { id, kind: "approval", value: { actionRequests: value.actionRequests, reviewConfigs: value.reviewConfigs } };
|
|
101
|
+
}
|
|
102
|
+
return { id, kind: "question", value: questionValue(stop, interrupt) };
|
|
103
|
+
};
|
|
104
|
+
/** The wait's deadline, declared at `now`. */
|
|
105
|
+
export const deadlineOf = (plan, now) => new Date(plan.dueAt ?? now + plan.dueInMs).toISOString();
|
|
106
|
+
const timeOf = (value, what) => {
|
|
107
|
+
const ms = value instanceof Date ? value.getTime() : typeof value === "string" ? Date.parse(value) : NaN;
|
|
108
|
+
if (Number.isNaN(ms))
|
|
109
|
+
throw failure(INTERNAL, `durable: ${what} is not a time`);
|
|
110
|
+
return ms;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* The wait a paused turn releases with: every interrupt of every unit that
|
|
114
|
+
* stopped, as one wait's questions. A tool wait (client tools' calls only) is
|
|
115
|
+
* addressed to config.clientTools.addressees, or the workflow's creator, and
|
|
116
|
+
* due config.clientTools.deadline seconds from now (default
|
|
117
|
+
* TOOL_WAIT_DEFAULT_DEADLINE_SECONDS, the earliest across the units); any
|
|
118
|
+
* other to config.approvals.addressees, or the creator, by the earliest
|
|
119
|
+
* config.approvals.deadline, or in seven days. A person cannot run a tool and
|
|
120
|
+
* a machine cannot answer a question, so one pause cannot ask both (I2).
|
|
121
|
+
*/
|
|
122
|
+
export const planPause = (turn, { event = false } = {}) => {
|
|
123
|
+
const all = turn.interruptions.flatMap((stop) => stop.interrupts.map((interrupt) => questionOf(stop, interrupt)));
|
|
124
|
+
if (all.length === 0)
|
|
125
|
+
throw failure(INTERNAL, "durable: the turn paused with nothing to ask");
|
|
126
|
+
const tools = all.filter((question) => question.kind === "tool");
|
|
127
|
+
if (tools.length > 0 && tools.length < all.length) {
|
|
128
|
+
throw failure(INTERNAL, "durable: a turn cannot wait on a client tool and a person at once: its client tool calls and its approvals or questions paused together; run them in separate turns");
|
|
129
|
+
}
|
|
130
|
+
// One wait asks at most MAX_QUESTIONS_PER_WAIT (core's limit, mirrored in
|
|
131
|
+
// LIMITS); the calls past it stay paused and are asked once these are
|
|
132
|
+
// answered, as LangGraph interrupts again on every call it has no answer for.
|
|
133
|
+
const questions = all.slice(0, LIMITS.MAX_QUESTIONS_PER_WAIT);
|
|
134
|
+
const bytes = Buffer.byteLength(JSON.stringify(questions), "utf8");
|
|
135
|
+
if (bytes > LIMITS.QUESTIONS_MAX_BYTES) {
|
|
136
|
+
throw failure(INTERNAL, `durable: the pause's questions are ${bytes} bytes of JSON, over QUESTIONS_MAX_BYTES (${LIMITS.QUESTIONS_MAX_BYTES}): a tool call's args or an approval's request are too large to ask`);
|
|
137
|
+
}
|
|
138
|
+
const asked = new Set(questions.map((question) => question.id));
|
|
139
|
+
const forTools = tools.length > 0;
|
|
140
|
+
const setting = forTools ? "config.clientTools" : "config.approvals";
|
|
141
|
+
const addressees = new Set();
|
|
142
|
+
let dueAt = null;
|
|
143
|
+
let toolDueInMs = null;
|
|
144
|
+
for (const stop of turn.interruptions) {
|
|
145
|
+
if (!stop.interrupts.some((interrupt) => asked.has(`g:${stop.key}:${interrupt.id}`)))
|
|
146
|
+
continue;
|
|
147
|
+
const options = (forTools ? stop.options?.clientTools : stop.options?.approvals) ?? null;
|
|
148
|
+
if (event && !options?.addressees) {
|
|
149
|
+
// An event's run carries the event's user, who need not be the
|
|
150
|
+
// workflow's creator, and the platform does not tell the runner the
|
|
151
|
+
// creator yet (Ruling 34): the agent says who answers.
|
|
152
|
+
throw failure(INTERNAL, `durable: an event's turn must name who answers: set ${setting}.addressees (an event's run does not know the workflow's creator)`);
|
|
153
|
+
}
|
|
154
|
+
for (const id of options?.addressees ?? (turn.creator ? [turn.creator] : []))
|
|
155
|
+
addressees.add(String(id));
|
|
156
|
+
if (forTools) {
|
|
157
|
+
const ms = Number(options?.deadline ?? LIMITS.TOOL_WAIT_DEFAULT_DEADLINE_SECONDS) * 1000;
|
|
158
|
+
toolDueInMs = toolDueInMs === null ? ms : Math.min(toolDueInMs, ms);
|
|
159
|
+
}
|
|
160
|
+
else if (options?.deadline !== undefined && options?.deadline !== null) {
|
|
161
|
+
const at = timeOf(options.deadline, "config.approvals.deadline");
|
|
162
|
+
dueAt = dueAt === null ? at : Math.min(dueAt, at);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (addressees.size === 0) {
|
|
166
|
+
throw failure(INTERNAL, `durable: nobody to ask: the workflow has no creator; set ${setting}.addressees`);
|
|
167
|
+
}
|
|
168
|
+
const names = (kind, of) => [
|
|
169
|
+
...new Set(questions.filter((question) => question.kind === kind).flatMap(of)),
|
|
170
|
+
];
|
|
171
|
+
const approvals = names("approval", (question) => question.value.actionRequests.map((action) => action.name));
|
|
172
|
+
const title = forTools
|
|
173
|
+
? `Client tool call: ${names("tool", (question) => [question.value.name]).join(", ")}`
|
|
174
|
+
: approvals.length > 0
|
|
175
|
+
? `Approval needed: ${approvals.join(", ")}`
|
|
176
|
+
: String(questions[0].value.question);
|
|
177
|
+
return {
|
|
178
|
+
questions,
|
|
179
|
+
title: clip(title, LIMITS.TITLE_MAX_CHARS),
|
|
180
|
+
addressees: [...addressees],
|
|
181
|
+
dueAt,
|
|
182
|
+
dueInMs: toolDueInMs ?? APPROVAL_DEFAULT_DEADLINE_SECONDS * 1000,
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
/** The pause's `input` wait, tagged with the checkpoint step it was saved as, declared at `now`. */
|
|
186
|
+
export const waitOf = (plan, step, now) => ({
|
|
187
|
+
kind: "input",
|
|
188
|
+
tag: `${PAUSE_TAG_PREFIX}${step}`,
|
|
189
|
+
prompt: { title: plan.title, questions: plan.questions },
|
|
190
|
+
addressees: plan.addressees,
|
|
191
|
+
deadline: deadlineOf(plan, now),
|
|
192
|
+
});
|
|
193
|
+
/**
|
|
194
|
+
* Refuses a checkpoint over CHECKPOINT_MAX_BYTES before it is sent: the
|
|
195
|
+
* platform would answer 413 for a body it has already been sent whole.
|
|
196
|
+
* @param bytes - The gzipped snapshot
|
|
197
|
+
* @param max - The limit; LIMITS.CHECKPOINT_MAX_BYTES
|
|
198
|
+
*/
|
|
199
|
+
export const checkCheckpointSize = (bytes, max = LIMITS.CHECKPOINT_MAX_BYTES) => {
|
|
200
|
+
if (bytes.length > max) {
|
|
201
|
+
throw failure(INTERNAL, `durable: the conversation checkpoint is ${bytes.length} bytes gzipped, over CHECKPOINT_MAX_BYTES (${max}): keep large tool outputs and media out of the conversation (store them in a durable.step and pass ids)`);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* The conversation checkpoint (design §5.5): the journal, the paused threads,
|
|
206
|
+
* the questions of this pause and the turn's own input, as gzip of JSON.
|
|
207
|
+
*/
|
|
208
|
+
export const snapshotOf = async (turn, plan, ref) => {
|
|
209
|
+
const { journal, threads } = await turn.export(new Set(plan.questions.map((question) => question.id)));
|
|
210
|
+
const json = JSON.stringify({
|
|
211
|
+
format: SNAPSHOT_FORMAT,
|
|
212
|
+
version: SNAPSHOT_VERSION,
|
|
213
|
+
...versions(),
|
|
214
|
+
savedAt: new Date(turn.now()).toISOString(),
|
|
215
|
+
journal,
|
|
216
|
+
threads,
|
|
217
|
+
pending: plan.questions,
|
|
218
|
+
turn: { input: ref.input ?? null, decision: ref.decision ?? null },
|
|
219
|
+
});
|
|
220
|
+
return {
|
|
221
|
+
bytes: gzipSync(Buffer.from(json, "utf8")),
|
|
222
|
+
size: Buffer.byteLength(json, "utf8"),
|
|
223
|
+
entries: Object.keys(journal.entries).length,
|
|
224
|
+
threads: Object.keys(threads).length,
|
|
225
|
+
};
|
|
226
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Checkpoint } from "../workflow/transport.js";
|
|
2
|
+
import { DurableTurn } from "./journal.js";
|
|
3
|
+
/** Whether an input is the answer to a paused turn's wait: a resolved `input` wait tagged `durable:<step>`. Any other outcome is the turn's to decide. */
|
|
4
|
+
export declare const isPausedResume: (input: any) => boolean;
|
|
5
|
+
/** What a resume restores: the paused turn's own input and decision, and its journal. */
|
|
6
|
+
export interface Restored {
|
|
7
|
+
input: any;
|
|
8
|
+
decision: any;
|
|
9
|
+
step: number;
|
|
10
|
+
turn: DurableTurn;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Reads the conversation checkpoint a paused turn's wait was declared for,
|
|
14
|
+
* and rebuilds the turn from it with the wait's answers merged in. Refuses,
|
|
15
|
+
* with "the paused state could not be read", a checkpoint that is missing,
|
|
16
|
+
* fails its sha256, belongs to another pause, or was written by another
|
|
17
|
+
* format or another major of LangGraph (the agent was rebuilt while it
|
|
18
|
+
* waited).
|
|
19
|
+
* @param input - The resume input (`isPausedResume`)
|
|
20
|
+
* @param checkpoint - What `getCheckpoint` answered
|
|
21
|
+
* @param init - The rebuilt turn's creator and clock
|
|
22
|
+
*/
|
|
23
|
+
export declare const restore: (input: any, checkpoint: Checkpoint | null, init: {
|
|
24
|
+
creator?: string | null;
|
|
25
|
+
now?: () => number;
|
|
26
|
+
}) => Restored;
|
|
27
|
+
//# sourceMappingURL=resume.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resume.d.ts","sourceRoot":"","sources":["../../../src/durable/resume.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAW,MAAM,cAAc,CAAC;AAUpD,0JAA0J;AAC1J,eAAO,MAAM,cAAc,GAAI,OAAO,GAAG,KAAG,OAKJ,CAAC;AAEzC,yFAAyF;AACzF,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,GAAG,CAAC;IACX,QAAQ,EAAE,GAAG,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,WAAW,CAAC;CACnB;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,OAAO,GAClB,OAAO,GAAG,EACV,YAAY,UAAU,GAAG,IAAI,EAC7B,MAAM;IAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;CAAE,KACpD,QA4CF,CAAC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { gunzipSync } from "node:zlib";
|
|
3
|
+
import constants from "../const.js";
|
|
4
|
+
import { DurableTurn, failure } from "./journal.js";
|
|
5
|
+
import { PAUSE_TAG_PREFIX, SNAPSHOT_FORMAT, SNAPSHOT_VERSION, majorOf, versions } from "./pause.js";
|
|
6
|
+
// The resume (design #964 §10.2): the answers to a paused turn's wait bring
|
|
7
|
+
// back its conversation checkpoint, and the same turn runs again on its own
|
|
8
|
+
// input, its journal and paused threads restored.
|
|
9
|
+
const unreadable = (why) => failure(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, `the paused state could not be read: ${why}`);
|
|
10
|
+
/** Whether an input is the answer to a paused turn's wait: a resolved `input` wait tagged `durable:<step>`. Any other outcome is the turn's to decide. */
|
|
11
|
+
export const isPausedResume = (input) => input?.type === "resume" &&
|
|
12
|
+
input.kind === "input" &&
|
|
13
|
+
input.outcome === "resolved" &&
|
|
14
|
+
typeof input.tag === "string" &&
|
|
15
|
+
input.tag.startsWith(PAUSE_TAG_PREFIX);
|
|
16
|
+
/**
|
|
17
|
+
* Reads the conversation checkpoint a paused turn's wait was declared for,
|
|
18
|
+
* and rebuilds the turn from it with the wait's answers merged in. Refuses,
|
|
19
|
+
* with "the paused state could not be read", a checkpoint that is missing,
|
|
20
|
+
* fails its sha256, belongs to another pause, or was written by another
|
|
21
|
+
* format or another major of LangGraph (the agent was rebuilt while it
|
|
22
|
+
* waited).
|
|
23
|
+
* @param input - The resume input (`isPausedResume`)
|
|
24
|
+
* @param checkpoint - What `getCheckpoint` answered
|
|
25
|
+
* @param init - The rebuilt turn's creator and clock
|
|
26
|
+
*/
|
|
27
|
+
export const restore = (input, checkpoint, init) => {
|
|
28
|
+
const answers = input?.input?.answers;
|
|
29
|
+
if (input?.truncated || !answers || typeof answers !== "object") {
|
|
30
|
+
throw failure(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, "the answers to the paused turn did not arrive whole; it cannot be resumed");
|
|
31
|
+
}
|
|
32
|
+
if (!checkpoint || checkpoint.gone || !checkpoint.bytes)
|
|
33
|
+
throw unreadable("the workflow has no conversation checkpoint");
|
|
34
|
+
// client-api reads the step from the x-sf-checkpoint-step header: without it, NaN.
|
|
35
|
+
if (typeof checkpoint.step !== "number" || !Number.isInteger(checkpoint.step)) {
|
|
36
|
+
throw unreadable("the platform did not say which checkpoint it holds (no step)");
|
|
37
|
+
}
|
|
38
|
+
const step = String(input.tag).slice(PAUSE_TAG_PREFIX.length);
|
|
39
|
+
if (String(checkpoint.step) !== step) {
|
|
40
|
+
throw unreadable(`its wait is for checkpoint ${step}, and the workflow's is ${checkpoint.step} (a later pause replaced it)`);
|
|
41
|
+
}
|
|
42
|
+
const bytes = Buffer.from(checkpoint.bytes);
|
|
43
|
+
// Fail closed, as for the step: a checkpoint whose integrity cannot be checked is not restored.
|
|
44
|
+
if (typeof checkpoint.sha256 !== "string" || !checkpoint.sha256) {
|
|
45
|
+
throw unreadable("the platform did not say the checkpoint's sha256");
|
|
46
|
+
}
|
|
47
|
+
if (createHash("sha256").update(bytes).digest("hex") !== checkpoint.sha256) {
|
|
48
|
+
throw unreadable("its sha256 does not match its bytes");
|
|
49
|
+
}
|
|
50
|
+
let snapshot;
|
|
51
|
+
try {
|
|
52
|
+
snapshot = JSON.parse(gunzipSync(bytes).toString("utf8"));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
snapshot = null;
|
|
56
|
+
}
|
|
57
|
+
if (snapshot?.format !== SNAPSHOT_FORMAT)
|
|
58
|
+
throw unreadable("it is not a durable turn snapshot");
|
|
59
|
+
if (snapshot.version !== SNAPSHOT_VERSION) {
|
|
60
|
+
throw unreadable(`it is snapshot version ${snapshot.version}, and this agent-utils reads version ${SNAPSHOT_VERSION}`);
|
|
61
|
+
}
|
|
62
|
+
const running = versions().langgraph;
|
|
63
|
+
if (majorOf(snapshot.langgraph) !== majorOf(running)) {
|
|
64
|
+
throw unreadable(`it was paused under LangGraph ${snapshot.langgraph} and runs now on LangGraph ${running}: the agent was rebuilt with an incompatible LangGraph while it waited`);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
input: snapshot.turn?.input ?? null,
|
|
68
|
+
decision: snapshot.turn?.decision ?? null,
|
|
69
|
+
step: Number(checkpoint.step),
|
|
70
|
+
turn: new DurableTurn({ journal: snapshot.journal, threads: snapshot.threads, answers, creator: init.creator, now: init.now }),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { MemorySaver } from "@langchain/langgraph";
|
|
2
|
+
/** One namespace of a thread as the snapshot keeps it: its latest checkpoint and that checkpoint's writes, base64. */
|
|
3
|
+
export interface SavedCheckpoint {
|
|
4
|
+
checkpointId: string;
|
|
5
|
+
/** Its parent's id, so `getTuple` still answers `parentConfig`; the parent itself is not kept. */
|
|
6
|
+
parentId: string | null;
|
|
7
|
+
checkpoint: string;
|
|
8
|
+
metadata: string;
|
|
9
|
+
/** `MemorySaver.writes` of the checkpoint: `"<taskId>,<idx>"` → `[taskId, channel, value]`. */
|
|
10
|
+
writes: Record<string, [string, string, string]>;
|
|
11
|
+
}
|
|
12
|
+
/** Threads by id, then namespace (`""` for the graph's own). */
|
|
13
|
+
export type SavedThreads = Record<string, Record<string, SavedCheckpoint>>;
|
|
14
|
+
/**
|
|
15
|
+
* LangGraph's `MemorySaver` whose threads export as JSON (design #964 §10.2):
|
|
16
|
+
* what a paused turn's conversation checkpoint carries. `exportThreads` keeps
|
|
17
|
+
* only the latest checkpoint of each thread and namespace, with its writes
|
|
18
|
+
* (the pending writes and the interrupts a resume reads); earlier checkpoints
|
|
19
|
+
* are history a resume never reads. `fromThreads` / `importThreads` are the
|
|
20
|
+
* reverse. An agent built with one can pause in one process and resume in
|
|
21
|
+
* another: `createAgent(..., { checkpointer: new durable.SnapshotSaver() })`.
|
|
22
|
+
*
|
|
23
|
+
* A latest-only export is enough for a `createAgent` graph: its `messages`
|
|
24
|
+
* channel is a reducer, not a delta channel, so nothing walks the ancestors
|
|
25
|
+
* (spike item 2, `getDeltaChannelHistory` never called).
|
|
26
|
+
*/
|
|
27
|
+
export declare class SnapshotSaver extends MemorySaver {
|
|
28
|
+
/**
|
|
29
|
+
* The latest checkpoint of each thread and namespace, with its writes.
|
|
30
|
+
* @param threadIds - Only these threads; every thread when omitted
|
|
31
|
+
*/
|
|
32
|
+
exportThreads(threadIds?: string[]): SavedThreads;
|
|
33
|
+
/** Adds exported threads, replacing a namespace this saver already holds. */
|
|
34
|
+
importThreads(threads: SavedThreads): void;
|
|
35
|
+
/** A saver holding exported threads. */
|
|
36
|
+
static fromThreads(threads: SavedThreads): SnapshotSaver;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=snapshotSaver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshotSaver.d.ts","sourceRoot":"","sources":["../../../src/durable/snapshotSaver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,sHAAsH;AACtH,MAAM,WAAW,eAAe;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,kGAAkG;IAClG,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,+FAA+F;IAC/F,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAClD;AAED,gEAAgE;AAChE,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;AAS3E;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAc,SAAQ,WAAW;IAC5C;;;OAGG;IACH,aAAa,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,GAAG,YAAY;IA2BjD,6EAA6E;IAC7E,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI;IAmB1C,wCAAwC;IACxC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,aAAa;CAKzD"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { MemorySaver } from "@langchain/langgraph";
|
|
2
|
+
const toBase64 = (bytes) => Buffer.from(bytes).toString("base64");
|
|
3
|
+
const fromBase64 = (text) => new Uint8Array(Buffer.from(text, "base64"));
|
|
4
|
+
/** MemorySaver's key for a checkpoint's writes. */
|
|
5
|
+
const writesKey = (threadId, namespace, checkpointId) => JSON.stringify([threadId, namespace, checkpointId]);
|
|
6
|
+
/**
|
|
7
|
+
* LangGraph's `MemorySaver` whose threads export as JSON (design #964 §10.2):
|
|
8
|
+
* what a paused turn's conversation checkpoint carries. `exportThreads` keeps
|
|
9
|
+
* only the latest checkpoint of each thread and namespace, with its writes
|
|
10
|
+
* (the pending writes and the interrupts a resume reads); earlier checkpoints
|
|
11
|
+
* are history a resume never reads. `fromThreads` / `importThreads` are the
|
|
12
|
+
* reverse. An agent built with one can pause in one process and resume in
|
|
13
|
+
* another: `createAgent(..., { checkpointer: new durable.SnapshotSaver() })`.
|
|
14
|
+
*
|
|
15
|
+
* A latest-only export is enough for a `createAgent` graph: its `messages`
|
|
16
|
+
* channel is a reducer, not a delta channel, so nothing walks the ancestors
|
|
17
|
+
* (spike item 2, `getDeltaChannelHistory` never called).
|
|
18
|
+
*/
|
|
19
|
+
export class SnapshotSaver extends MemorySaver {
|
|
20
|
+
/**
|
|
21
|
+
* The latest checkpoint of each thread and namespace, with its writes.
|
|
22
|
+
* @param threadIds - Only these threads; every thread when omitted
|
|
23
|
+
*/
|
|
24
|
+
exportThreads(threadIds) {
|
|
25
|
+
const threads = {};
|
|
26
|
+
const ids = threadIds ?? Object.keys(this.storage);
|
|
27
|
+
for (const threadId of ids) {
|
|
28
|
+
const namespaces = this.storage[threadId];
|
|
29
|
+
if (!namespaces)
|
|
30
|
+
continue;
|
|
31
|
+
for (const [namespace, checkpoints] of Object.entries(namespaces)) {
|
|
32
|
+
// Checkpoint ids are time-ordered (uuid6); MemorySaver takes the greatest as the latest.
|
|
33
|
+
const latest = Object.keys(checkpoints).sort((a, b) => b.localeCompare(a))[0];
|
|
34
|
+
if (latest === undefined)
|
|
35
|
+
continue;
|
|
36
|
+
const [checkpoint, metadata, parentId] = checkpoints[latest];
|
|
37
|
+
const writes = {};
|
|
38
|
+
for (const [inner, [taskId, channel, value]] of Object.entries(this.writes[writesKey(threadId, namespace, latest)] ?? {})) {
|
|
39
|
+
writes[inner] = [taskId, channel, toBase64(value)];
|
|
40
|
+
}
|
|
41
|
+
(threads[threadId] ??= {})[namespace] = {
|
|
42
|
+
checkpointId: latest,
|
|
43
|
+
parentId: parentId ?? null,
|
|
44
|
+
checkpoint: toBase64(checkpoint),
|
|
45
|
+
metadata: toBase64(metadata),
|
|
46
|
+
writes,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return threads;
|
|
51
|
+
}
|
|
52
|
+
/** Adds exported threads, replacing a namespace this saver already holds. */
|
|
53
|
+
importThreads(threads) {
|
|
54
|
+
for (const [threadId, namespaces] of Object.entries(threads ?? {})) {
|
|
55
|
+
const thread = (this.storage[threadId] ??= Object.create(null));
|
|
56
|
+
for (const [namespace, saved] of Object.entries(namespaces)) {
|
|
57
|
+
thread[namespace] = Object.create(null);
|
|
58
|
+
thread[namespace][saved.checkpointId] = [
|
|
59
|
+
fromBase64(saved.checkpoint),
|
|
60
|
+
fromBase64(saved.metadata),
|
|
61
|
+
saved.parentId ?? undefined,
|
|
62
|
+
];
|
|
63
|
+
const writes = Object.create(null);
|
|
64
|
+
for (const [inner, [taskId, channel, value]] of Object.entries(saved.writes ?? {})) {
|
|
65
|
+
writes[inner] = [taskId, channel, fromBase64(value)];
|
|
66
|
+
}
|
|
67
|
+
this.writes[writesKey(threadId, namespace, saved.checkpointId)] = writes;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** A saver holding exported threads. */
|
|
72
|
+
static fromThreads(threads) {
|
|
73
|
+
const saver = new SnapshotSaver();
|
|
74
|
+
saver.importThreads(threads);
|
|
75
|
+
return saver;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export interface StepOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The tracker the step's calls count their usage into. A step that finished
|
|
4
|
+
* returns its saved result on the turn's resume without running, so what
|
|
5
|
+
* its calls used is given back here, and only here (the platform was told
|
|
6
|
+
* when it was spent).
|
|
7
|
+
*/
|
|
8
|
+
usageTracker?: any;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Runs `fn` once per turn (design #964 §10.2): when a paused turn runs again,
|
|
12
|
+
* a step that already finished returns its saved result instead of running.
|
|
13
|
+
* Put every side effect or paid call that is not an LLM call, between the
|
|
14
|
+
* start of a turn and a possible pause, in a step (rule 1). The key names the
|
|
15
|
+
* step in its scope, and `fn` runs in a scope of its own, so the calls it
|
|
16
|
+
* makes are journaled under it; when it finishes they are dropped and only
|
|
17
|
+
* its result is kept. Results are JSON: return ids, not bytes (rule 4).
|
|
18
|
+
*
|
|
19
|
+
* Outside a `workflow.run` turn, or inside an agent's tool (whose agent
|
|
20
|
+
* already keeps what its tools did), `fn` simply runs.
|
|
21
|
+
*
|
|
22
|
+
* @param key - The step's name in its scope: the same on every run of the turn
|
|
23
|
+
* @param fn - The work
|
|
24
|
+
* @param options - `usageTracker`, to have a replayed step's usage given back
|
|
25
|
+
* @returns What `fn` returned, or its saved copy on a replay
|
|
26
|
+
*/
|
|
27
|
+
export declare const step: <T>(key: string, fn: () => Promise<T>, options?: StepOptions) => Promise<T>;
|
|
28
|
+
//# sourceMappingURL=step.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"step.d.ts","sourceRoot":"","sources":["../../../src/durable/step.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW;IAC1B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,GAAG,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,IAAI,GAAU,CAAC,EAAE,KAAK,MAAM,EAAE,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,UAAS,WAAgB,KAAG,OAAO,CAAC,CAAC,CAerG,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import constants from "../const.js";
|
|
2
|
+
import { currentFrame, failure } from "./journal.js";
|
|
3
|
+
/**
|
|
4
|
+
* Runs `fn` once per turn (design #964 §10.2): when a paused turn runs again,
|
|
5
|
+
* a step that already finished returns its saved result instead of running.
|
|
6
|
+
* Put every side effect or paid call that is not an LLM call, between the
|
|
7
|
+
* start of a turn and a possible pause, in a step (rule 1). The key names the
|
|
8
|
+
* step in its scope, and `fn` runs in a scope of its own, so the calls it
|
|
9
|
+
* makes are journaled under it; when it finishes they are dropped and only
|
|
10
|
+
* its result is kept. Results are JSON: return ids, not bytes (rule 4).
|
|
11
|
+
*
|
|
12
|
+
* Outside a `workflow.run` turn, or inside an agent's tool (whose agent
|
|
13
|
+
* already keeps what its tools did), `fn` simply runs.
|
|
14
|
+
*
|
|
15
|
+
* @param key - The step's name in its scope: the same on every run of the turn
|
|
16
|
+
* @param fn - The work
|
|
17
|
+
* @param options - `usageTracker`, to have a replayed step's usage given back
|
|
18
|
+
* @returns What `fn` returned, or its saved copy on a replay
|
|
19
|
+
*/
|
|
20
|
+
export const step = async (key, fn, options = {}) => {
|
|
21
|
+
if (typeof key !== "string" || key.length === 0) {
|
|
22
|
+
throw failure(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, "durable.step needs a key: durable.step(key, fn)");
|
|
23
|
+
}
|
|
24
|
+
// "/" separates scopes in the journal's keys: step "x/llm#0" would be taken
|
|
25
|
+
// for the first call of step "x" and replay its result.
|
|
26
|
+
if (key.includes("/")) {
|
|
27
|
+
throw failure(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, `durable.step needs a key without "/": ${JSON.stringify(key)} names a scope`);
|
|
28
|
+
}
|
|
29
|
+
if (typeof fn !== "function") {
|
|
30
|
+
throw failure(constants.HTTP_CODES.INTERNAL_SERVER_ERROR, "durable.step needs a function: durable.step(key, fn)");
|
|
31
|
+
}
|
|
32
|
+
const frame = currentFrame();
|
|
33
|
+
if (!frame || frame.passthrough)
|
|
34
|
+
return fn();
|
|
35
|
+
return frame.turn.step(frame, key, fn, options?.usageTracker ?? null);
|
|
36
|
+
};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,25 +6,48 @@ 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 { requirePricedModel } from "./modelRates.js";
|
|
9
10
|
import stackfactorKnowledge from "./stackfactorKnowledge.js";
|
|
10
11
|
import referenceContext, { KnowledgeAccessError } from "./referenceContext.js";
|
|
11
|
-
import
|
|
12
|
+
import sourceStore from "./sourceStore.js";
|
|
13
|
+
import { LinksNotCheckedError, checkUrls, checkUrlsReachable, readUrls, unreachableUrls } from "./tavily.js";
|
|
14
|
+
import { RETRY_WAITS, isNetworkFailure, isTransient, messageOf, retryTransient, statusOf } from "./retry.js";
|
|
15
|
+
import * as workflow from "./workflow/index.js";
|
|
16
|
+
import * as durable from "./durable/index.js";
|
|
17
|
+
import daemon from "./daemon.js";
|
|
18
|
+
import learningActivityMedia from "./learningActivityMedia.js";
|
|
19
|
+
import learningContentPlan from "./learningContentPlan.js";
|
|
20
|
+
import learningContentThread from "./learningContentThread.js";
|
|
12
21
|
export { constants };
|
|
13
22
|
export { errorHandling, AppError };
|
|
14
23
|
export type { ParsedError } from "./errorHandling.js";
|
|
15
24
|
export { langChain };
|
|
16
25
|
export type { UsageTracker } from "./langChain.js";
|
|
17
|
-
export { checkUrlsReachable };
|
|
18
|
-
export type { TavilyConfig, TavilyToolName } from "./tavily.js";
|
|
26
|
+
export { LinksNotCheckedError, checkUrls, checkUrlsReachable, readUrls, unreachableUrls };
|
|
27
|
+
export type { TavilyConfig, TavilyToolName, UnreachableUrlsOptions, UrlCheck, UrlRead } from "./tavily.js";
|
|
19
28
|
export { logger };
|
|
20
29
|
export { serve, runOnce, isRunOnceExecution, EXECUTION_ENV };
|
|
30
|
+
export { daemon };
|
|
31
|
+
export type { DaemonOptions, PoolTransport, Sleep, DaemonLog } from "./daemon.js";
|
|
32
|
+
export type { DaemonConfig } from "./daemonConfig.js";
|
|
21
33
|
export { callAgent };
|
|
22
34
|
export { checkAgent };
|
|
23
35
|
export type { AgentExecuteRequest, CallAgentOptions, CheckAgentOptions, AgentHealth, } from "./client.js";
|
|
24
36
|
export { runtimeContext };
|
|
25
|
-
export {
|
|
37
|
+
export { workflow };
|
|
38
|
+
export { durable };
|
|
39
|
+
export { recordUsage, requirePricedModel, USAGE_UNITS };
|
|
26
40
|
export type { UsageEntry, UsageUnit } from "./usage.js";
|
|
27
|
-
export { stackfactorKnowledge, referenceContext, KnowledgeAccessError };
|
|
28
|
-
export type {
|
|
29
|
-
export
|
|
41
|
+
export { stackfactorKnowledge, referenceContext, sourceStore, KnowledgeAccessError };
|
|
42
|
+
export type { CitedPassage, KnowledgeOutline, KnowledgeOutlines, KnowledgePassage, KnowledgePassages, Source, SourceFinding, SourceOutlineEntry, SourceRead, SourceRecord, SourceProgress, OnSourceProgress, SourceStore, SourceUse, } from "./sourceStore.js";
|
|
43
|
+
export { RETRY_WAITS, isNetworkFailure, isTransient, messageOf, retryTransient, statusOf };
|
|
44
|
+
export type { RetryOptions } from "./retry.js";
|
|
45
|
+
export type { GroundingPassage, KnowledgeChunk, KnowledgeSearch, KnowledgeSource, PassageRole, SkippedSource, StackFactorReference, } from "./stackfactorKnowledge.js";
|
|
46
|
+
export type { StoredReference } from "./referenceContext.js";
|
|
47
|
+
export { learningActivityMedia };
|
|
48
|
+
export { learningContentPlan };
|
|
49
|
+
export { learningContentThread };
|
|
50
|
+
export type { ContentPlan, ContentPlanExtension, ContentPlanLesson, ContentPlanTerm, LearningContentThread, LearningContentThreadSentence } from "./learningContentPlan.js";
|
|
51
|
+
export type { PlanContext, ThreadLines, ThreadLinesOnPage, WriteThreadInput, WriteThreadResult } from "./learningContentThread.js";
|
|
52
|
+
export type { ActivityContentField, ActivityMediaChoices, ActivityMediaOptions, ActivityMediaPlatform, ActivityMediaSummary, ActivityMediaTarget, MediaCount, MediaIntegrationChoice, MotionGraphicStyle, } from "./learningActivityMedia.js";
|
|
30
53
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/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;AACtD,OAAO,oBAAoB,MAAM,2BAA2B,CAAC;AAC7D,OAAO,gBAAgB,EAAE,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"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"}
|
package/dist/esm/index.js
CHANGED
|
@@ -6,17 +6,33 @@ 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 { requirePricedModel } from "./modelRates.js";
|
|
9
10
|
import stackfactorKnowledge from "./stackfactorKnowledge.js";
|
|
10
11
|
import referenceContext, { KnowledgeAccessError } from "./referenceContext.js";
|
|
11
|
-
import
|
|
12
|
+
import sourceStore from "./sourceStore.js";
|
|
13
|
+
import { LinksNotCheckedError, checkUrls, checkUrlsReachable, readUrls, unreachableUrls } from "./tavily.js";
|
|
14
|
+
import { RETRY_WAITS, isNetworkFailure, isTransient, messageOf, retryTransient, statusOf } from "./retry.js";
|
|
15
|
+
import * as workflow from "./workflow/index.js";
|
|
16
|
+
import * as durable from "./durable/index.js";
|
|
17
|
+
import daemon from "./daemon.js";
|
|
18
|
+
import learningActivityMedia from "./learningActivityMedia.js";
|
|
19
|
+
import learningContentPlan from "./learningContentPlan.js";
|
|
20
|
+
import learningContentThread from "./learningContentThread.js";
|
|
12
21
|
export { constants };
|
|
13
22
|
export { errorHandling, AppError };
|
|
14
23
|
export { langChain };
|
|
15
|
-
export { checkUrlsReachable };
|
|
24
|
+
export { LinksNotCheckedError, checkUrls, checkUrlsReachable, readUrls, unreachableUrls };
|
|
16
25
|
export { logger };
|
|
17
26
|
export { serve, runOnce, isRunOnceExecution, EXECUTION_ENV };
|
|
27
|
+
export { daemon };
|
|
18
28
|
export { callAgent };
|
|
19
29
|
export { checkAgent };
|
|
20
30
|
export { runtimeContext };
|
|
21
|
-
export {
|
|
22
|
-
export {
|
|
31
|
+
export { workflow };
|
|
32
|
+
export { durable };
|
|
33
|
+
export { recordUsage, requirePricedModel, USAGE_UNITS };
|
|
34
|
+
export { stackfactorKnowledge, referenceContext, sourceStore, KnowledgeAccessError };
|
|
35
|
+
export { RETRY_WAITS, isNetworkFailure, isTransient, messageOf, retryTransient, statusOf };
|
|
36
|
+
export { learningActivityMedia };
|
|
37
|
+
export { learningContentPlan };
|
|
38
|
+
export { learningContentThread };
|