@restatedev/agents 0.11.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -808
- package/dist/pi/factory.d.ts +73 -0
- package/dist/pi/factory.d.ts.map +1 -0
- package/dist/pi/factory.js +145 -0
- package/dist/pi/factory.js.map +1 -0
- package/dist/pi/index.d.ts +5 -129
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +31 -571
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/keepalive.d.ts +3 -16
- package/dist/pi/keepalive.d.ts.map +1 -1
- package/dist/pi/keepalive.js +38 -41
- package/dist/pi/keepalive.js.map +1 -1
- package/dist/pi/left.d.ts +16 -0
- package/dist/pi/left.d.ts.map +1 -0
- package/dist/pi/left.js +23 -0
- package/dist/pi/left.js.map +1 -0
- package/dist/pi/storage.d.ts +19 -76
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +50 -368
- package/dist/pi/storage.js.map +1 -1
- package/dist/pi/tools.d.ts +12 -0
- package/dist/pi/tools.d.ts.map +1 -0
- package/dist/pi/tools.js +70 -0
- package/dist/pi/tools.js.map +1 -0
- package/package.json +11 -254
- package/src/pi/factory.ts +231 -0
- package/src/pi/index.ts +31 -759
- package/src/pi/keepalive.ts +46 -56
- package/src/pi/left.ts +52 -0
- package/src/pi/storage.ts +75 -409
- package/src/pi/tools.ts +78 -0
- package/dist/agentlib/agent.d.ts +0 -32
- package/dist/agentlib/agent.d.ts.map +0 -1
- package/dist/agentlib/agent.js +0 -174
- package/dist/agentlib/agent.js.map +0 -1
- package/dist/agentlib/index.d.ts +0 -5
- package/dist/agentlib/index.d.ts.map +0 -1
- package/dist/agentlib/index.js +0 -15
- package/dist/agentlib/index.js.map +0 -1
- package/dist/agentlib/messages.d.ts +0 -39
- package/dist/agentlib/messages.d.ts.map +0 -1
- package/dist/agentlib/messages.js +0 -177
- package/dist/agentlib/messages.js.map +0 -1
- package/dist/agentlib/model.d.ts +0 -31
- package/dist/agentlib/model.d.ts.map +0 -1
- package/dist/agentlib/model.js +0 -140
- package/dist/agentlib/model.js.map +0 -1
- package/dist/agentlib/options.d.ts +0 -58
- package/dist/agentlib/options.d.ts.map +0 -1
- package/dist/agentlib/options.js +0 -2
- package/dist/agentlib/options.js.map +0 -1
- package/dist/agentlib/runtime.d.ts +0 -37
- package/dist/agentlib/runtime.d.ts.map +0 -1
- package/dist/agentlib/runtime.js +0 -216
- package/dist/agentlib/runtime.js.map +0 -1
- package/dist/agentlib/state.d.ts +0 -114
- package/dist/agentlib/state.d.ts.map +0 -1
- package/dist/agentlib/state.js +0 -52
- package/dist/agentlib/state.js.map +0 -1
- package/dist/agentlib/steering.d.ts +0 -23
- package/dist/agentlib/steering.d.ts.map +0 -1
- package/dist/agentlib/steering.js +0 -48
- package/dist/agentlib/steering.js.map +0 -1
- package/dist/agentlib/tools.d.ts +0 -33
- package/dist/agentlib/tools.d.ts.map +0 -1
- package/dist/agentlib/tools.js +0 -245
- package/dist/agentlib/tools.js.map +0 -1
- package/dist/agentlib/turn.d.ts +0 -82
- package/dist/agentlib/turn.d.ts.map +0 -1
- package/dist/agentlib/turn.js +0 -253
- package/dist/agentlib/turn.js.map +0 -1
- package/dist/calls.d.ts +0 -90
- package/dist/calls.d.ts.map +0 -1
- package/dist/calls.js +0 -241
- package/dist/calls.js.map +0 -1
- package/dist/claude/index.d.ts +0 -106
- package/dist/claude/index.d.ts.map +0 -1
- package/dist/claude/index.js +0 -386
- package/dist/claude/index.js.map +0 -1
- package/dist/client.d.ts +0 -90
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -88
- package/dist/client.js.map +0 -1
- package/dist/common.d.ts +0 -410
- package/dist/common.d.ts.map +0 -1
- package/dist/common.js +0 -563
- package/dist/common.js.map +0 -1
- package/dist/companion.d.ts +0 -286
- package/dist/companion.d.ts.map +0 -1
- package/dist/companion.js +0 -463
- package/dist/companion.js.map +0 -1
- package/dist/conversation.d.ts +0 -576
- package/dist/conversation.d.ts.map +0 -1
- package/dist/conversation.js +0 -1476
- package/dist/conversation.js.map +0 -1
- package/dist/deepseek/barriers.d.ts +0 -11
- package/dist/deepseek/barriers.d.ts.map +0 -1
- package/dist/deepseek/barriers.js +0 -36
- package/dist/deepseek/barriers.js.map +0 -1
- package/dist/deepseek/index.d.ts +0 -97
- package/dist/deepseek/index.d.ts.map +0 -1
- package/dist/deepseek/index.js +0 -603
- package/dist/deepseek/index.js.map +0 -1
- package/dist/deepseek/persistence.d.ts +0 -83
- package/dist/deepseek/persistence.d.ts.map +0 -1
- package/dist/deepseek/persistence.js +0 -262
- package/dist/deepseek/persistence.js.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -17
- package/dist/index.js.map +0 -1
- package/dist/langgraph/checkpointer.d.ts +0 -52
- package/dist/langgraph/checkpointer.d.ts.map +0 -1
- package/dist/langgraph/checkpointer.js +0 -477
- package/dist/langgraph/checkpointer.js.map +0 -1
- package/dist/langgraph/context.d.ts +0 -10
- package/dist/langgraph/context.d.ts.map +0 -1
- package/dist/langgraph/context.js +0 -21
- package/dist/langgraph/context.js.map +0 -1
- package/dist/langgraph/errors.d.ts +0 -16
- package/dist/langgraph/errors.d.ts.map +0 -1
- package/dist/langgraph/errors.js +0 -30
- package/dist/langgraph/errors.js.map +0 -1
- package/dist/langgraph/graph.d.ts +0 -25
- package/dist/langgraph/graph.d.ts.map +0 -1
- package/dist/langgraph/graph.js +0 -378
- package/dist/langgraph/graph.js.map +0 -1
- package/dist/langgraph/index.d.ts +0 -119
- package/dist/langgraph/index.d.ts.map +0 -1
- package/dist/langgraph/index.js +0 -325
- package/dist/langgraph/index.js.map +0 -1
- package/dist/langgraph/keepalive.d.ts +0 -16
- package/dist/langgraph/keepalive.d.ts.map +0 -1
- package/dist/langgraph/keepalive.js +0 -51
- package/dist/langgraph/keepalive.js.map +0 -1
- package/dist/langgraph/nodes.d.ts +0 -71
- package/dist/langgraph/nodes.d.ts.map +0 -1
- package/dist/langgraph/nodes.js +0 -148
- package/dist/langgraph/nodes.js.map +0 -1
- package/dist/messages.d.ts +0 -18
- package/dist/messages.d.ts.map +0 -1
- package/dist/messages.js +0 -182
- package/dist/messages.js.map +0 -1
- package/dist/object.d.ts +0 -213
- package/dist/object.d.ts.map +0 -1
- package/dist/object.js +0 -309
- package/dist/object.js.map +0 -1
- package/dist/opencode/db.d.ts +0 -81
- package/dist/opencode/db.d.ts.map +0 -1
- package/dist/opencode/db.js +0 -249
- package/dist/opencode/db.js.map +0 -1
- package/dist/opencode/index.d.ts +0 -184
- package/dist/opencode/index.d.ts.map +0 -1
- package/dist/opencode/index.js +0 -1079
- package/dist/opencode/index.js.map +0 -1
- package/dist/output.d.ts +0 -25
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -99
- package/dist/output.js.map +0 -1
- package/dist/pi/commits.d.ts +0 -6
- package/dist/pi/commits.d.ts.map +0 -1
- package/dist/pi/commits.js +0 -3
- package/dist/pi/commits.js.map +0 -1
- package/dist/steering.d.ts +0 -32
- package/dist/steering.d.ts.map +0 -1
- package/dist/steering.js +0 -45
- package/dist/steering.js.map +0 -1
- package/dist/tools.d.ts +0 -267
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -305
- package/dist/tools.js.map +0 -1
- package/dist/webhook.d.ts +0 -71
- package/dist/webhook.d.ts.map +0 -1
- package/dist/webhook.js +0 -102
- package/dist/webhook.js.map +0 -1
- package/src/agentlib/README.md +0 -229
- package/src/agentlib/agent.ts +0 -216
- package/src/agentlib/index.ts +0 -16
- package/src/agentlib/messages.ts +0 -207
- package/src/agentlib/model.ts +0 -156
- package/src/agentlib/options.ts +0 -62
- package/src/agentlib/runtime.ts +0 -252
- package/src/agentlib/state.ts +0 -93
- package/src/agentlib/steering.ts +0 -53
- package/src/agentlib/tools.ts +0 -355
- package/src/agentlib/turn.ts +0 -302
- package/src/calls.ts +0 -333
- package/src/claude/README.md +0 -107
- package/src/claude/index.ts +0 -487
- package/src/client.ts +0 -187
- package/src/common.ts +0 -766
- package/src/companion.ts +0 -675
- package/src/conversation.ts +0 -1799
- package/src/deepseek/README.md +0 -202
- package/src/deepseek/barriers.ts +0 -63
- package/src/deepseek/index.ts +0 -740
- package/src/deepseek/persistence.ts +0 -337
- package/src/index.ts +0 -41
- package/src/langgraph/README.md +0 -199
- package/src/langgraph/checkpointer.ts +0 -614
- package/src/langgraph/context.ts +0 -27
- package/src/langgraph/errors.ts +0 -26
- package/src/langgraph/graph.ts +0 -422
- package/src/langgraph/index.ts +0 -500
- package/src/langgraph/keepalive.ts +0 -58
- package/src/langgraph/nodes.ts +0 -247
- package/src/messages.ts +0 -244
- package/src/object.ts +0 -454
- package/src/opencode/README.md +0 -249
- package/src/opencode/db.ts +0 -387
- package/src/opencode/index.ts +0 -1339
- package/src/output.ts +0 -116
- package/src/pi/README.md +0 -174
- package/src/pi/commits.ts +0 -7
- package/src/steering.ts +0 -68
- package/src/tools.ts +0 -515
- package/src/webhook.ts +0 -161
package/src/calls.ts
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
// The Restate calls of an invocation's tools (`call`, `signal`, `subagent`...
|
|
2
|
-
// of tools.ts), kept in the object's database (`restate_agent_calls`) so that
|
|
3
|
-
// a call is made once, however often its tool call runs.
|
|
4
|
-
//
|
|
5
|
-
// A crash or a suspension restarts the handler. pi, Claude and LangGraph then
|
|
6
|
-
// run a tool call that was cut off again, with its id. The DeepSeek Harness
|
|
7
|
-
// tells the model it was interrupted instead, and the model
|
|
8
|
-
// makes it again with a new id: made with the same tool and input, it is the
|
|
9
|
-
// call an earlier attempt made, so it takes that call's id, and so the same
|
|
10
|
-
// idempotency key: the same Restate call (a wait for a signal, a sub-agent's
|
|
11
|
-
// task), which it waits for again, or whose answer it gets.
|
|
12
|
-
//
|
|
13
|
-
// A call is kept until the framework's transcript has its result under the
|
|
14
|
-
// last id it was made with ({@link settleCalls}, after each commit of steps, and before each
|
|
15
|
-
// tool call): one that answered just before a crash, whose result the
|
|
16
|
-
// framework had not stored, is made again as the same call. An error there
|
|
17
|
-
// is not its result (the DeepSeek Harness gives the call it interrupted
|
|
18
|
-
// one, and the model makes it again), unless the call answered with that error: a
|
|
19
|
-
// terminal error is its result too. Once stored, the model has its result,
|
|
20
|
-
// and an identical call it makes later is a call of its own.
|
|
21
|
-
//
|
|
22
|
-
// Only the record of a new call commits of its own, before the call is made,
|
|
23
|
-
// and that commit also carries what was pending: an answer is never behind it.
|
|
24
|
-
// That a call answered rides a commit that is under way or starts at once
|
|
25
|
-
// ({@link rideCommit}): the one that stores the call's result, so that the
|
|
26
|
-
// transcript never has a result of a call that is not recorded as answered;
|
|
27
|
-
// if none does (a slow tool of the round is still running, another call
|
|
28
|
-
// waits), it is written on its own at the end of that macrotask, so it is
|
|
29
|
-
// never left unrecorded for long. That its record is forgotten rides the next
|
|
30
|
-
// commit, whenever it comes. A crash in the few moments before an answer is
|
|
31
|
-
// written reads the call as one that did not answer, as a crash just before
|
|
32
|
-
// a commit of its own would have: made again, under the same key, and cut off
|
|
33
|
-
// for a stopped turn that another invocation closes.
|
|
34
|
-
//
|
|
35
|
-
// A framework's own wait ({@link waitFor} of tools.ts) is its tool call's
|
|
36
|
-
// alone: the framework stored its tool call before it waits, so a retried
|
|
37
|
-
// attempt waits again under the same id, and a new call with the same input
|
|
38
|
-
// (the model asking again, after the first's answer) waits anew. An approval
|
|
39
|
-
// a tool call waited for is not the call: a stopped turn does not report the
|
|
40
|
-
// call as done because its approval returned ({@link outcomes}).
|
|
41
|
-
import type * as restate from '@restatedev/sessions';
|
|
42
|
-
import type { Transaction } from '@restatedev/sessions';
|
|
43
|
-
import { flushRiders, rideCommit, Schema, ScopeKey } from '@restatedev/sessions/internal';
|
|
44
|
-
import { randomUUID } from 'node:crypto';
|
|
45
|
-
|
|
46
|
-
/** A tool's call: the tool, its input as JSON, the ids it was made with (the first is its own), and the attempt that last made it. */
|
|
47
|
-
interface Call {
|
|
48
|
-
tool: string;
|
|
49
|
-
input: string;
|
|
50
|
-
ids: string[];
|
|
51
|
-
attempt: string;
|
|
52
|
-
/** Answered, until the transcript has its result. */
|
|
53
|
-
done?: true;
|
|
54
|
-
/** It answered with this error (`Error: <message>`), a terminal one: its result, as the transcript has it. */
|
|
55
|
-
failed?: string;
|
|
56
|
-
/** An approval its tool call waited for ({@link waitFor}): its answer is not the tool call's. */
|
|
57
|
-
approval?: true;
|
|
58
|
-
/**
|
|
59
|
-
* The prompt whose turn made it (`answerOnce`'s), if any: an invocation
|
|
60
|
-
* that prompts more than once keeps the calls of each apart.
|
|
61
|
-
*/
|
|
62
|
-
prompt?: string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** The calls an invocation's tools made that the transcript has no result of yet. */
|
|
66
|
-
interface Calls {
|
|
67
|
-
invocation: string;
|
|
68
|
-
calls: Call[];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* The table of the {@link Calls}: one row, of the invocation whose calls they are. `answerOnce` applies it with
|
|
73
|
-
* the conversation's, in one commit, as a key's first turn starts.
|
|
74
|
-
*/
|
|
75
|
-
export const SCHEMA = new Schema('restate-agents.calls', [
|
|
76
|
-
`CREATE TABLE restate_agent_calls (
|
|
77
|
-
row INTEGER PRIMARY KEY CHECK (row = 0),
|
|
78
|
-
invocation TEXT NOT NULL,
|
|
79
|
-
calls TEXT NOT NULL
|
|
80
|
-
)`,
|
|
81
|
-
]);
|
|
82
|
-
|
|
83
|
-
/** What the calls need of a handler's context. */
|
|
84
|
-
type CallsContext = Pick<restate.SessionContext, 'db' | 'request'>;
|
|
85
|
-
|
|
86
|
-
/** What writes what is held back before anything visible, by holder: the conversation's rows, pi's batch of commits. */
|
|
87
|
-
const HELD = new ScopeKey<Map<string, () => Promise<void>>>('what the attempt holds back');
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Have {@link flushHeld} write, with `flush`, what a holder keeps back: by
|
|
91
|
-
* default the rows that the conversation of the attempt's turn holds back
|
|
92
|
-
* (`Conversation.hold`), which `answerOnce` sets. A holder of another `slot`
|
|
93
|
-
* (pi's storage, which holds its commits until an effect starts) coexists
|
|
94
|
-
* with it; one of the same slot replaces it, and without a `flush` the slot
|
|
95
|
-
* is released.
|
|
96
|
-
*/
|
|
97
|
-
export const holding = (
|
|
98
|
-
ctx: restate.SessionContext,
|
|
99
|
-
flush: (() => Promise<void>) | undefined,
|
|
100
|
-
slot = 'conversation',
|
|
101
|
-
) => {
|
|
102
|
-
const held = HELD.of(ctx, () => new Map());
|
|
103
|
-
if (flush) held.set(slot, flush);
|
|
104
|
-
else held.delete(slot);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Write what the holders keep back, in the order they were set, one after
|
|
109
|
-
* the other: the rows that the conversation of the attempt's turn holds back,
|
|
110
|
-
* if any (see `Conversation.hold`), and pi's batched commits. Call it before
|
|
111
|
-
* what another party sees, or what outlives the attempt: a publish, a
|
|
112
|
-
* Restate call, send or wait. What is held is lost in a crash, and the
|
|
113
|
-
* retried attempt makes it again; what was seen or called must not depend
|
|
114
|
-
* on it.
|
|
115
|
-
*/
|
|
116
|
-
export async function flushHeld(ctx: restate.SessionContext) {
|
|
117
|
-
for (const flush of HELD.get(ctx)?.values() ?? []) await flush();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const UPSERT = 'INSERT OR REPLACE INTO restate_agent_calls (row, invocation, calls) VALUES (0, ?, ?)';
|
|
121
|
-
|
|
122
|
-
/** The slot of the calls' write that rides the next commit ({@link ride}). */
|
|
123
|
-
const RIDER = 'restate-agents.calls';
|
|
124
|
-
|
|
125
|
-
/** The write of `state`, as it is when it runs: the row, whole. */
|
|
126
|
-
const writing = (state: Calls) => (tx: Transaction) =>
|
|
127
|
-
tx.execute(UPSERT, [state.invocation, JSON.stringify(state.calls)]);
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Store `calls`, committed before the call they record is made. The rows the
|
|
131
|
-
* conversation holds back are written first: the call outlives the attempt,
|
|
132
|
-
* and what led to it must too. It is the whole row, and it is written by a
|
|
133
|
-
* commit that also carries what was pending (an answer): the state is
|
|
134
|
-
* registered as the pending write, and flushed. A transaction under way (a
|
|
135
|
-
* framework storing an earlier call's result) writes it as it commits, so that
|
|
136
|
-
* the result is never committed beside an answer that is not recorded; with
|
|
137
|
-
* none, it is a transaction of its own.
|
|
138
|
-
*/
|
|
139
|
-
async function store(ctx: restate.SessionContext, state: Calls) {
|
|
140
|
-
await flushHeld(ctx);
|
|
141
|
-
if (rideCommit(ctx.db, RIDER, writing(state))) await flushRiders(ctx.db);
|
|
142
|
-
else await ctx.db.execute(UPSERT, [state.invocation, JSON.stringify(state.calls)]);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Store `state` with the next commit of the database, rather than commit it
|
|
147
|
-
* (see the top of this file), as it is then: a later change replaces it, and
|
|
148
|
-
* the latest state is written. An `urgent` one (an answer) is written on its
|
|
149
|
-
* own unless a commit is under way to write it; one pending that is urgent
|
|
150
|
-
* stays so. A database that cannot ride (a view's) commits it at once.
|
|
151
|
-
*/
|
|
152
|
-
async function ride(ctx: restate.SessionContext, state: Calls, urgent = false) {
|
|
153
|
-
const written = rideCommit(ctx.db, RIDER, writing(state), { urgent });
|
|
154
|
-
if (!written) await store(ctx, state);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** The calls of `invocation`, as stored: none if the row is another invocation's. */
|
|
158
|
-
async function load(ctx: CallsContext, invocation: string): Promise<Calls> {
|
|
159
|
-
await SCHEMA.ensure(ctx.db);
|
|
160
|
-
const row = await ctx.db.get<{ invocation: string; calls: string }>(
|
|
161
|
-
'SELECT invocation, calls FROM restate_agent_calls',
|
|
162
|
-
);
|
|
163
|
-
return row?.invocation === invocation
|
|
164
|
-
? { invocation, calls: JSON.parse(row.calls) as Call[] }
|
|
165
|
-
: { invocation, calls: [] };
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** Each attempt's id, and its calls, by its context, once they are being read. */
|
|
169
|
-
const ATTEMPT = new ScopeKey<{ id: string; calls?: Promise<Calls> }>('the calls of an attempt');
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* The attempt of `ctx`: its id, and the calls of its invocation (loaded once,
|
|
173
|
-
* shared by calls made at once). A load that fails (its schema's commit
|
|
174
|
-
* cancelled, say) is not kept: the next asks again, so that a stopped turn's
|
|
175
|
-
* close reads the calls as stored, rather than fail on the load it found.
|
|
176
|
-
*/
|
|
177
|
-
function attemptOf(ctx: restate.SessionContext) {
|
|
178
|
-
const attempt = ATTEMPT.of(ctx, () => ({ id: randomUUID() }));
|
|
179
|
-
if (attempt.calls === undefined) {
|
|
180
|
-
const calls = load(ctx, ctx.request.id);
|
|
181
|
-
attempt.calls = calls;
|
|
182
|
-
calls.catch(() => {
|
|
183
|
-
if (attempt.calls === calls) attempt.calls = undefined;
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
return attempt as typeof attempt & { calls: Promise<Calls> };
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** `value` as JSON with its objects' keys sorted: the same input, however its keys came, is the same call. */
|
|
190
|
-
function canonical(value: unknown): string {
|
|
191
|
-
return JSON.stringify(value, (_key, v: unknown) =>
|
|
192
|
-
v !== null && typeof v === 'object' && !Array.isArray(v)
|
|
193
|
-
? Object.fromEntries(
|
|
194
|
-
Object.entries(v as Record<string, unknown>).toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
|
|
195
|
-
)
|
|
196
|
-
: v,
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** The prompt whose turn the tools of `ctx` run for, while `answerOnce` answers it. */
|
|
201
|
-
const PROMPT = new ScopeKey<string>('the prompt of the calls');
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Run `work` (a turn's) with the calls its tools make kept as prompt
|
|
205
|
-
* `prompt`'s: they are matched, and rejoined, within it only. An invocation
|
|
206
|
-
* that prompts twice, its second prompt making the call its first made
|
|
207
|
-
* (the same tool and input), would otherwise take the first's after a crash
|
|
208
|
-
* (its record not yet forgotten), and never make its own.
|
|
209
|
-
*/
|
|
210
|
-
export async function forPrompt<T>(ctx: restate.SessionContext, prompt: string, work: () => Promise<T>): Promise<T> {
|
|
211
|
-
const outer = PROMPT.get(ctx);
|
|
212
|
-
PROMPT.set(ctx, prompt);
|
|
213
|
-
try {
|
|
214
|
-
return await work();
|
|
215
|
-
} finally {
|
|
216
|
-
if (outer === undefined) PROMPT.delete(ctx);
|
|
217
|
-
else PROMPT.set(ctx, outer);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* The call of tool `tool` with `input` that tool call `callId` makes: the
|
|
223
|
-
* one it made to `tool` before (a run again), or else, if `rejoin`, the one
|
|
224
|
-
* an earlier attempt made with the same tool and input (made again under a
|
|
225
|
-
* new id), or else a new one, an `approval` its tool call waits for if so.
|
|
226
|
-
* `answered()` stores that it answered.
|
|
227
|
-
*/
|
|
228
|
-
export async function callOf(
|
|
229
|
-
ctx: restate.SessionContext,
|
|
230
|
-
tool: string,
|
|
231
|
-
input: unknown,
|
|
232
|
-
callId: string,
|
|
233
|
-
{ rejoin = true, approval = false }: { rejoin?: boolean; approval?: boolean } = {},
|
|
234
|
-
) {
|
|
235
|
-
const attempt = attemptOf(ctx);
|
|
236
|
-
const state = await attempt.calls;
|
|
237
|
-
const json = canonical(input);
|
|
238
|
-
const prompt = PROMPT.get(ctx);
|
|
239
|
-
const ours = state.calls.filter((c) => c.prompt === prompt);
|
|
240
|
-
// A tool call may make more than one call (an approval, then the tool's
|
|
241
|
-
// own), each to a tool of its own.
|
|
242
|
-
let call =
|
|
243
|
-
ours.find((c) => c.ids.includes(callId) && c.tool === tool) ??
|
|
244
|
-
(rejoin ? ours.find((c) => c.attempt !== attempt.id && c.tool === tool && c.input === json) : undefined);
|
|
245
|
-
if (call) {
|
|
246
|
-
call.attempt = attempt.id;
|
|
247
|
-
if (!call.ids.includes(callId)) call.ids.push(callId);
|
|
248
|
-
} else {
|
|
249
|
-
call = { tool, input: json, ids: [callId], attempt: attempt.id };
|
|
250
|
-
if (approval) call.approval = true;
|
|
251
|
-
if (prompt !== undefined) call.prompt = prompt;
|
|
252
|
-
state.calls.push(call);
|
|
253
|
-
}
|
|
254
|
-
await store(ctx, state);
|
|
255
|
-
const made = call;
|
|
256
|
-
return {
|
|
257
|
-
id: made.ids[0]!,
|
|
258
|
-
/**
|
|
259
|
-
* It answered, with `failed` if a terminal error answered it: kept until
|
|
260
|
-
* the transcript has its result.
|
|
261
|
-
*
|
|
262
|
-
* It rides a commit that is under way (the one that stores the call's
|
|
263
|
-
* result, when the framework's write has started), and is otherwise
|
|
264
|
-
* written on its own at once ({@link ride}): a call that answered must
|
|
265
|
-
* not stay unrecorded while something else runs (a slow tool of the same
|
|
266
|
-
* round, a call that waits for a signal). A crash before it is written
|
|
267
|
-
* reads the call as one that did not answer, as a crash just before a
|
|
268
|
-
* commit of its own would have: made again under the same key, and cut off
|
|
269
|
-
* for a stopped turn that another invocation closes ({@link outcomes} reads
|
|
270
|
-
* the stored row; the attempt's own turn reads its state).
|
|
271
|
-
*/
|
|
272
|
-
async answered(failed?: string) {
|
|
273
|
-
made.done = true;
|
|
274
|
-
if (failed !== undefined) made.failed = failed;
|
|
275
|
-
await ride(ctx, state, true);
|
|
276
|
-
},
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Forget the calls that answered whose result the framework stored: those
|
|
282
|
-
* of which `stored(id, failed)` is true for the last id they were made with,
|
|
283
|
-
* `failed` the error that answered the call, if one did. Called
|
|
284
|
-
* after each commit of steps, and before each tool call, with the transcript as the
|
|
285
|
-
* framework stored it: also in an attempt that made no call yet, which would
|
|
286
|
-
* otherwise take one an earlier attempt made, and the model has the result
|
|
287
|
-
* of.
|
|
288
|
-
*/
|
|
289
|
-
export async function settleCalls(ctx: restate.SessionContext, stored: (id: string, failed?: string) => boolean) {
|
|
290
|
-
const state = await attemptOf(ctx).calls;
|
|
291
|
-
const kept = state.calls.filter((c) => !c.done || !stored(c.ids.at(-1)!, c.failed));
|
|
292
|
-
if (kept.length === state.calls.length) return;
|
|
293
|
-
state.calls = kept;
|
|
294
|
-
await ride(ctx, state);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Which of `callIds` made a Restate call that answered, in this invocation
|
|
299
|
-
* or in `invocation`: each with the terminal error it answered with
|
|
300
|
-
* (`Error: <message>`), or `undefined` if it succeeded. A tool call whose
|
|
301
|
-
* call did not answer, or that made none, is left out; so is one whose only
|
|
302
|
-
* answered call is its approval, which it ran (or was denied) after.
|
|
303
|
-
*
|
|
304
|
-
* An earlier `invocation`'s calls are read only while they are still
|
|
305
|
-
* stored, until an invocation of the key makes calls of its own; read,
|
|
306
|
-
* never changed. With `prompt`, the calls of its turn only.
|
|
307
|
-
*/
|
|
308
|
-
export async function outcomes(
|
|
309
|
-
ctx: restate.SessionContext,
|
|
310
|
-
callIds: Iterable<string>,
|
|
311
|
-
invocation?: string,
|
|
312
|
-
prompt?: string,
|
|
313
|
-
): Promise<Map<string, string | undefined>> {
|
|
314
|
-
const state =
|
|
315
|
-
invocation !== undefined && invocation !== ctx.request.id
|
|
316
|
-
? await load(ctx, invocation)
|
|
317
|
-
: await attemptOf(ctx).calls;
|
|
318
|
-
const wanted = new Set(callIds);
|
|
319
|
-
const found = new Map<string, string | undefined>();
|
|
320
|
-
for (const call of state.calls) {
|
|
321
|
-
if (!call.done || call.approval) continue;
|
|
322
|
-
// Another prompt's call, of the same invocation: not this turn's.
|
|
323
|
-
if (prompt !== undefined && call.prompt !== undefined && call.prompt !== prompt) continue;
|
|
324
|
-
for (const id of call.ids) if (wanted.has(id)) found.set(id, call.failed);
|
|
325
|
-
}
|
|
326
|
-
return found;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Write what rides the next commit ({@link ride}) now, if it is pending: at
|
|
331
|
-
* the end of a turn, when no commit follows that would.
|
|
332
|
-
*/
|
|
333
|
-
export const flushCalls = (ctx: restate.SessionContext) => flushRiders(ctx.db);
|
package/src/claude/README.md
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
# Claude
|
|
2
|
-
|
|
3
|
-
The Anthropic SDK's tool runner (`client.beta.messages.toolRunner` in
|
|
4
|
-
[`@anthropic-ai/sdk`](https://github.com/anthropics/anthropic-sdk-typescript))
|
|
5
|
-
is a light agent harness: the tool-calling loop, in the process, with nothing
|
|
6
|
-
stored. It keeps the conversation in its params, so an agent built on it is
|
|
7
|
-
durable when the conversation is: here it is the agent's conversation of
|
|
8
|
-
turns, in its SQL database
|
|
9
|
-
([SQL in Restate virtual objects](../../../sessions/README.md)).
|
|
10
|
-
|
|
11
|
-
It is lighter than the Claude Agent SDK, which runs Claude Code as a
|
|
12
|
-
subprocess. Claude Code's own tools (bash, read, write, edit, glob, grep)
|
|
13
|
-
come to the runner as `betaAgentToolset20260401` from
|
|
14
|
-
`@anthropic-ai/sdk/tools/agent-toolset/node`; the agent takes whatever tools
|
|
15
|
-
you give it.
|
|
16
|
-
|
|
17
|
-
## Use
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
import { serve } from '@restatedev/agents';
|
|
21
|
-
import * as claude from '@restatedev/agents/claude';
|
|
22
|
-
import { weather } from './weather.ts';
|
|
23
|
-
|
|
24
|
-
const assistant = claude.agent({ name: 'assistant', system: 'Answer briefly.', tools: [weather] });
|
|
25
|
-
await serve(assistant);
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
| Option | What |
|
|
29
|
-
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
30
|
-
| `model` | `anthropic/model-id`; or else `MODEL`, or else `anthropic/claude-sonnet-5-5` (`ANTHROPIC_API_KEY`) |
|
|
31
|
-
| `system` | The system prompt |
|
|
32
|
-
| `tools` | `tool()`s and Restate's tools of `@restatedev/agents`, or the SDK's runnable tools (such as `betaZodTool`) |
|
|
33
|
-
| `output` | A zod object: the answer as data, which the agent replies with as JSON, and `chat` answers with (see [Structured answers](../../README.md#structured-answers)): asked for natively where the model has structured outputs |
|
|
34
|
-
| `nativeOutput` | Whether `output` is asked for with the API's structured outputs (`output_config.format`) rather than in the prompt (default: if the model has them, as Claude's 4.5 models and later do) |
|
|
35
|
-
| `client` | The Anthropic client (a base URL or a proxy of your own, say); by default one with `ANTHROPIC_API_KEY` |
|
|
36
|
-
| `maxTokens` | The most tokens of each of the model's replies (default 16000): a reply cut off there fails its message (a terminal error) |
|
|
37
|
-
| `effort` | How much effort the model puts into each reply: `low`, `medium`, `high`, `xhigh` or `max` (default: the model's) |
|
|
38
|
-
|
|
39
|
-
With `output`, a model with structured outputs is asked for the answer
|
|
40
|
-
natively: each request of the run has the JSON schema of `output` as its
|
|
41
|
-
`output_config.format` (the SDK's `betaZodOutputFormat`), which holds the
|
|
42
|
-
model's final reply to it, while its tool calls go on as before. Neither
|
|
43
|
-
the system prompt nor the prompt asks for it. The reply is checked all the
|
|
44
|
-
same, against what the API does not hold it to (a string's `max`, an
|
|
45
|
-
`enum`: the SDK moves them into the schema's descriptions), and a reply
|
|
46
|
-
that does not fit is asked again once, as when asked in the prompt. A
|
|
47
|
-
declined reply, or one cut off at `maxTokens`, ends the turn, as without
|
|
48
|
-
`output`. A schema the API refuses (one too complex for it, say) refuses
|
|
49
|
-
the message: `nativeOutput: false` asks in the prompt, as for a model
|
|
50
|
-
without structured outputs (Claude 3, Opus 4 and 4.1, Sonnet 4) or a model
|
|
51
|
-
id the agent does not know.
|
|
52
|
-
|
|
53
|
-
For a session object of your own, `claude.chat(options)` is the handler and
|
|
54
|
-
`claude.messages()` the view; `options` may be a function of the
|
|
55
|
-
invocation's context (each user's system prompt, say). `claude.prompt(ctx,
|
|
56
|
-
text, options)` answers a prompt in a handler of your own, once, however
|
|
57
|
-
often the handler runs; each prompt of an invocation is a prompt of its own.
|
|
58
|
-
|
|
59
|
-
## Through crashes
|
|
60
|
-
|
|
61
|
-
The conversation is the object's conversation of turns
|
|
62
|
-
([docs/conversation.md](../../docs/conversation.md)), whose items are the
|
|
63
|
-
SDK's messages. The runner starts from what the model sees of it: the
|
|
64
|
-
earlier turns as their prompt and answer (as many as `budget` holds, the
|
|
65
|
-
earliest summarized), the latest `window` turns whole. The handler is not
|
|
66
|
-
replayed: a retried attempt runs it again from the start, from what is
|
|
67
|
-
stored.
|
|
68
|
-
|
|
69
|
-
- **Each reply is stored before its tools run, and their results before the
|
|
70
|
-
next request.** So a crash during the tools resumes them with the same tool
|
|
71
|
-
call ids, without asking the model again. The final reply, which no tool
|
|
72
|
-
waits for, is stored with the turn's answer, in one commit: a crash before
|
|
73
|
-
it asks the model again. A paused reply (`pause_turn`, a server tool's long
|
|
74
|
-
turn) is stored before the model goes on from it.
|
|
75
|
-
- **A tool is idempotent by its tool call id** (`callId` of `tool()`'s
|
|
76
|
-
`run`, or `callId(context)` for the SDK's tools), which it passes on, such
|
|
77
|
-
as in an idempotency key.
|
|
78
|
-
- **A tool's error fails the attempt**, and Restate retries it; a
|
|
79
|
-
`restate.TerminalError` of a `@restatedev/agents` tool, and the SDK's
|
|
80
|
-
`ToolError`, are its answer to the model.
|
|
81
|
-
- **A message is answered once per invocation** (`answerOnce`). A turn that
|
|
82
|
-
a killed invocation left is closed (`ended`), never finished: by the next
|
|
83
|
-
message, or by the agent's `settle` once its companion finds the
|
|
84
|
-
invocation gone.
|
|
85
|
-
|
|
86
|
-
## Run
|
|
87
|
-
|
|
88
|
-
```sh
|
|
89
|
-
cd packages/examples && ANTHROPIC_API_KEY=… node basic/claude.ts
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
With a Restate server, and the agent registered with it: see
|
|
93
|
-
[Run an example](../../../examples/README.md#run-an-example).
|
|
94
|
-
|
|
95
|
-
## Tests
|
|
96
|
-
|
|
97
|
-
The tests run on a scripted model, [`test/fakes/claude.ts`](../../test/fakes/claude.ts)
|
|
98
|
-
(`pnpm --filter @restatedev/agents test claude`, from the repository root):
|
|
99
|
-
|
|
100
|
-
- [`test/claude.test.ts`](../../test/claude.test.ts):
|
|
101
|
-
each point above, with crashes that stop an attempt in a tool call, or at
|
|
102
|
-
any of its state requests, and abandon it, keeping what it committed; and
|
|
103
|
-
`messages`.
|
|
104
|
-
- [`test/claude.ts`](../../test/claude.ts): the bank
|
|
105
|
-
agent, whose tools move money in the [ledger](../../../sessions/test/ledger.ts),
|
|
106
|
-
through crashes in [the crash test](../../../e2e/crash.ts)
|
|
107
|
-
(`pnpm --filter e2e crash`): every message moves money exactly once.
|