@restatedev/agents 0.4.0 → 0.6.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 +104 -38
- package/dist/calls.d.ts +20 -1
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +46 -7
- package/dist/calls.js.map +1 -1
- package/dist/claude/index.d.ts +11 -11
- package/dist/claude/index.d.ts.map +1 -1
- package/dist/claude/index.js +9 -6
- package/dist/claude/index.js.map +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/common.d.ts +62 -23
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +67 -38
- package/dist/common.js.map +1 -1
- package/dist/{signals.d.ts → companion.d.ts} +106 -43
- package/dist/companion.d.ts.map +1 -0
- package/dist/companion.js +463 -0
- package/dist/companion.js.map +1 -0
- package/dist/conversation.d.ts +115 -5
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +308 -79
- package/dist/conversation.js.map +1 -1
- package/dist/deepseek/index.d.ts +11 -11
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +23 -26
- package/dist/deepseek/index.js.map +1 -1
- package/dist/index.d.ts +8 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/langgraph/checkpointer.d.ts +2 -0
- package/dist/langgraph/checkpointer.d.ts.map +1 -1
- package/dist/langgraph/checkpointer.js +1 -1
- package/dist/langgraph/checkpointer.js.map +1 -1
- package/dist/langgraph/graph.d.ts.map +1 -1
- package/dist/langgraph/graph.js +32 -13
- package/dist/langgraph/graph.js.map +1 -1
- package/dist/langgraph/index.d.ts +11 -11
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/langgraph/index.js +19 -13
- package/dist/langgraph/index.js.map +1 -1
- package/dist/langgraph/nodes.d.ts +2 -1
- package/dist/langgraph/nodes.d.ts.map +1 -1
- package/dist/langgraph/nodes.js +23 -20
- package/dist/langgraph/nodes.js.map +1 -1
- package/dist/object.d.ts +24 -22
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +16 -11
- package/dist/object.js.map +1 -1
- package/dist/opencode/db.d.ts +81 -0
- package/dist/opencode/db.d.ts.map +1 -0
- package/dist/opencode/db.js +249 -0
- package/dist/opencode/db.js.map +1 -0
- package/dist/opencode/index.d.ts +184 -0
- package/dist/opencode/index.d.ts.map +1 -0
- package/dist/opencode/index.js +982 -0
- package/dist/opencode/index.js.map +1 -0
- package/dist/pi/index.d.ts +11 -11
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +16 -17
- package/dist/pi/index.js.map +1 -1
- package/dist/steering.d.ts +2 -1
- package/dist/steering.d.ts.map +1 -1
- package/dist/steering.js +2 -2
- package/dist/steering.js.map +1 -1
- package/dist/tools.d.ts +21 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +54 -14
- package/dist/tools.js.map +1 -1
- package/dist/webhook.js +1 -1
- package/dist/webhook.js.map +1 -1
- package/package.json +37 -4
- package/src/calls.ts +52 -11
- package/src/claude/README.md +4 -2
- package/src/claude/index.ts +9 -7
- package/src/client.ts +1 -1
- package/src/common.ts +99 -40
- package/src/companion.ts +674 -0
- package/src/conversation.ts +377 -105
- package/src/deepseek/README.md +7 -1
- package/src/deepseek/index.ts +22 -24
- package/src/index.ts +10 -4
- package/src/langgraph/README.md +8 -5
- package/src/langgraph/checkpointer.ts +1 -1
- package/src/langgraph/graph.ts +30 -16
- package/src/langgraph/index.ts +24 -17
- package/src/langgraph/nodes.ts +24 -20
- package/src/object.ts +45 -40
- package/src/opencode/README.md +234 -0
- package/src/opencode/db.ts +387 -0
- package/src/opencode/index.ts +1243 -0
- package/src/pi/README.md +6 -1
- package/src/pi/index.ts +16 -15
- package/src/steering.ts +8 -7
- package/src/tools.ts +60 -18
- package/src/webhook.ts +1 -1
- package/dist/signals.d.ts.map +0 -1
- package/dist/signals.js +0 -383
- package/dist/signals.js.map +0 -1
- package/src/signals.ts +0 -590
package/dist/webhook.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webhook.js","sourceRoot":"","sources":["../src/webhook.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,qEAAqE;AACrE,2EAA2E;AAC3E,yEAAyE;AACzE,sBAAsB;AACtB,EAAE;AACF,6BAA6B;AAC7B,sBAAsB;AACtB,wFAAwF;AACxF,4CAA4C;AAC5C,uBAAuB;AACvB,0CAA0C;AAC1C,QAAQ;AACR,iCAAiC;AACjC,EAAE;AACF,qEAAqE;AACrE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"webhook.js","sourceRoot":"","sources":["../src/webhook.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,qEAAqE;AACrE,2EAA2E;AAC3E,yEAAyE;AACzE,sBAAsB;AACtB,EAAE;AACF,6BAA6B;AAC7B,sBAAsB;AACtB,wFAAwF;AACxF,4CAA4C;AAC5C,uBAAuB;AACvB,0CAA0C;AAC1C,QAAQ;AACR,iCAAiC;AACjC,EAAE;AACF,qEAAqE;AACrE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAoDvD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,IAAY,EAAE,QAAgB,EAAE,KAAc,EAAE,EAAE,CAC/E,kBAAkB,IAAI,eAAe,QAAQ,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,C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ACF;SACF;KACF,CAAC,CAAC;AACL,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@restatedev/agents",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Agent frameworks in Restate virtual objects, their state in the object's SQL database: pi, LangGraph, Claude (Anthropic SDK) and DeepSeek Harness",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Agent frameworks in Restate virtual objects, their state in the object's SQL database: pi, OpenCode, LangGraph, Claude (Anthropic SDK) and DeepSeek Harness",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -35,6 +35,14 @@
|
|
|
35
35
|
"types": "./dist/langgraph/index.d.ts",
|
|
36
36
|
"default": "./dist/langgraph/index.js"
|
|
37
37
|
},
|
|
38
|
+
"./opencode": {
|
|
39
|
+
"types": "./dist/opencode/index.d.ts",
|
|
40
|
+
"default": "./dist/opencode/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./opencode/db": {
|
|
43
|
+
"types": "./dist/opencode/db.d.ts",
|
|
44
|
+
"default": "./dist/opencode/db.js"
|
|
45
|
+
},
|
|
38
46
|
"./pi": {
|
|
39
47
|
"types": "./dist/pi/index.d.ts",
|
|
40
48
|
"default": "./dist/pi/index.js"
|
|
@@ -95,8 +103,13 @@
|
|
|
95
103
|
"@langchain/langgraph-checkpoint": "1.1.5",
|
|
96
104
|
"@langchain/langgraph-checkpoint-validation": "1.1.1",
|
|
97
105
|
"@langchain/openai": "1.7.0",
|
|
106
|
+
"@opencode/core": "2.2.6",
|
|
107
|
+
"@opencode/plugin": "2.2.6",
|
|
108
|
+
"@opencode/sdk": "2.2.6",
|
|
109
|
+
"@opencode/server": "2.2.6",
|
|
98
110
|
"@restatedev/restate-sdk": "1.18.2",
|
|
99
|
-
"@restatedev/sessions": "0.
|
|
111
|
+
"@restatedev/sessions": "0.6.0",
|
|
112
|
+
"effect": "4.1.0",
|
|
100
113
|
"zod": "4.6.5"
|
|
101
114
|
},
|
|
102
115
|
"peerDependencies": {
|
|
@@ -127,7 +140,12 @@
|
|
|
127
140
|
"@langchain/langgraph": "^1.4.18",
|
|
128
141
|
"@langchain/langgraph-checkpoint": "^1.1.5",
|
|
129
142
|
"@langchain/openai": "^1.7.0",
|
|
130
|
-
"@
|
|
143
|
+
"@opencode/core": "2.2.6",
|
|
144
|
+
"@opencode/plugin": "2.2.6",
|
|
145
|
+
"@opencode/sdk": "2.2.6",
|
|
146
|
+
"@opencode/server": "2.2.6",
|
|
147
|
+
"@restatedev/sessions": "^0.6.0",
|
|
148
|
+
"effect": "4.1.0",
|
|
131
149
|
"zod": "^4.6.5"
|
|
132
150
|
},
|
|
133
151
|
"peerDependenciesMeta": {
|
|
@@ -211,6 +229,21 @@
|
|
|
211
229
|
},
|
|
212
230
|
"@langchain/openai": {
|
|
213
231
|
"optional": true
|
|
232
|
+
},
|
|
233
|
+
"@opencode/core": {
|
|
234
|
+
"optional": true
|
|
235
|
+
},
|
|
236
|
+
"@opencode/plugin": {
|
|
237
|
+
"optional": true
|
|
238
|
+
},
|
|
239
|
+
"@opencode/sdk": {
|
|
240
|
+
"optional": true
|
|
241
|
+
},
|
|
242
|
+
"@opencode/server": {
|
|
243
|
+
"optional": true
|
|
244
|
+
},
|
|
245
|
+
"effect": {
|
|
246
|
+
"optional": true
|
|
214
247
|
}
|
|
215
248
|
},
|
|
216
249
|
"engines": {
|
package/src/calls.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// task), which it waits for again, or whose answer it gets.
|
|
12
12
|
//
|
|
13
13
|
// A call is kept until the framework's transcript has its result under the
|
|
14
|
-
// last id it was made with ({@link settleCalls},
|
|
14
|
+
// last id it was made with ({@link settleCalls}, after each commit of steps, and before each
|
|
15
15
|
// tool call): one that answered just before a crash, whose result the
|
|
16
16
|
// framework had not stored, is made again as the same call. An error there
|
|
17
17
|
// is not its result (the DeepSeek Harness gives the call it interrupted
|
|
@@ -54,8 +54,11 @@ interface Calls {
|
|
|
54
54
|
calls: Call[];
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
/**
|
|
58
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The table of the {@link Calls}: one row, of the invocation whose calls they are. `answerOnce` applies it with
|
|
59
|
+
* the conversation's, in one commit, as a key's first turn starts.
|
|
60
|
+
*/
|
|
61
|
+
export const SCHEMA = new Schema('restate-agents.calls', [
|
|
59
62
|
`CREATE TABLE restate_agent_calls (
|
|
60
63
|
row INTEGER PRIMARY KEY CHECK (row = 0),
|
|
61
64
|
invocation TEXT NOT NULL,
|
|
@@ -66,8 +69,33 @@ const SCHEMA = new Schema('restate-agents.calls', [
|
|
|
66
69
|
/** What the calls need of a handler's context. */
|
|
67
70
|
type CallsContext = Pick<restate.SessionContext, 'db' | 'request'>;
|
|
68
71
|
|
|
69
|
-
/**
|
|
70
|
-
|
|
72
|
+
/** What writes the rows the conversation of the attempt's turn holds back, by its context. */
|
|
73
|
+
const HELD = new ScopeKey<() => Promise<void>>('the held rows of the conversation');
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Have {@link flushHeld} write, with `flush`, the rows that the conversation
|
|
77
|
+
* of the attempt's turn holds back (`Conversation.hold`): `answerOnce` does.
|
|
78
|
+
*/
|
|
79
|
+
export const holding = (ctx: restate.SessionContext, flush: () => Promise<void>) => HELD.set(ctx, flush);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Write the rows that the conversation of the attempt's turn holds back, if
|
|
83
|
+
* any (see `Conversation.hold`). Call it before what another party sees, or
|
|
84
|
+
* what outlives the attempt: a publish, a Restate call, send or wait. A held
|
|
85
|
+
* row is lost in a crash, and the retried attempt makes it again; what was
|
|
86
|
+
* seen or called must not depend on it.
|
|
87
|
+
*/
|
|
88
|
+
export async function flushHeld(ctx: restate.SessionContext) {
|
|
89
|
+
await HELD.get(ctx)?.();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Store `calls`, committed before the call they record is made. The rows the
|
|
94
|
+
* conversation holds back are written first: the call outlives the attempt,
|
|
95
|
+
* and what led to it must too.
|
|
96
|
+
*/
|
|
97
|
+
async function store(ctx: restate.SessionContext, { invocation, calls }: Calls) {
|
|
98
|
+
await flushHeld(ctx);
|
|
71
99
|
await ctx.db.execute('INSERT OR REPLACE INTO restate_agent_calls (row, invocation, calls) VALUES (0, ?, ?)', [
|
|
72
100
|
invocation,
|
|
73
101
|
JSON.stringify(calls),
|
|
@@ -85,12 +113,25 @@ async function load(ctx: CallsContext, invocation: string): Promise<Calls> {
|
|
|
85
113
|
: { invocation, calls: [] };
|
|
86
114
|
}
|
|
87
115
|
|
|
88
|
-
/** Each attempt's id, and its calls, by its context. */
|
|
89
|
-
const ATTEMPT = new ScopeKey<{ id: string; calls
|
|
116
|
+
/** Each attempt's id, and its calls, by its context, once they are being read. */
|
|
117
|
+
const ATTEMPT = new ScopeKey<{ id: string; calls?: Promise<Calls> }>('the calls of an attempt');
|
|
90
118
|
|
|
91
|
-
/**
|
|
92
|
-
|
|
93
|
-
|
|
119
|
+
/**
|
|
120
|
+
* The attempt of `ctx`: its id, and the calls of its invocation (loaded once,
|
|
121
|
+
* shared by calls made at once). A load that fails (its schema's commit
|
|
122
|
+
* cancelled, say) is not kept: the next asks again, so that a stopped turn's
|
|
123
|
+
* close reads the calls as stored, rather than fail on the load it found.
|
|
124
|
+
*/
|
|
125
|
+
function attemptOf(ctx: restate.SessionContext): { id: string; calls: Promise<Calls> } {
|
|
126
|
+
const attempt = ATTEMPT.of(ctx, () => ({ id: randomUUID() }));
|
|
127
|
+
if (attempt.calls === undefined) {
|
|
128
|
+
const calls = load(ctx, ctx.request.id);
|
|
129
|
+
attempt.calls = calls;
|
|
130
|
+
calls.catch(() => {
|
|
131
|
+
if (attempt.calls === calls) attempt.calls = undefined;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return attempt as { id: string; calls: Promise<Calls> };
|
|
94
135
|
}
|
|
95
136
|
|
|
96
137
|
/** `value` as JSON with its objects' keys sorted: the same input, however its keys came, is the same call. */
|
|
@@ -178,7 +219,7 @@ export async function callOf(
|
|
|
178
219
|
* Forget the calls that answered whose result the framework stored: those
|
|
179
220
|
* of which `stored(id, failed)` is true for the last id they were made with,
|
|
180
221
|
* `failed` the error that answered the call, if one did. Called
|
|
181
|
-
*
|
|
222
|
+
* after each commit of steps, and before each tool call, with the transcript as the
|
|
182
223
|
* framework stored it: also in an attempt that made no call yet, which would
|
|
183
224
|
* otherwise take one an earlier attempt made, and the model has the result
|
|
184
225
|
* of.
|
package/src/claude/README.md
CHANGED
|
@@ -68,8 +68,10 @@ stored.
|
|
|
68
68
|
|
|
69
69
|
- **Each reply is stored before its tools run, and their results before the
|
|
70
70
|
next request.** So a crash during the tools resumes them with the same tool
|
|
71
|
-
call ids, without asking the model again
|
|
72
|
-
|
|
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.
|
|
73
75
|
- **A tool is idempotent by its tool call id** (`callId` of `tool()`'s
|
|
74
76
|
`run`, or `callId(context)` for the SDK's tools), which it passes on, such
|
|
75
77
|
as in an idempotency key.
|
package/src/claude/index.ts
CHANGED
|
@@ -46,10 +46,9 @@ import {
|
|
|
46
46
|
modelIdOf,
|
|
47
47
|
modelOf,
|
|
48
48
|
RETURNED_UNRECORDED,
|
|
49
|
+
toolCallStarted,
|
|
49
50
|
stopSignalOf,
|
|
50
51
|
type Summarize,
|
|
51
|
-
toolCallEnded,
|
|
52
|
-
toolCallStarted,
|
|
53
52
|
NO_ANSWER,
|
|
54
53
|
} from '../common.ts';
|
|
55
54
|
import { answerOnce, type Items, type Parsed, type Run } from '../conversation.ts';
|
|
@@ -192,9 +191,9 @@ interface Agent {
|
|
|
192
191
|
|
|
193
192
|
/**
|
|
194
193
|
* The run of `agent` on the conversation: from the stored turn on, each
|
|
195
|
-
* reply stored before its tools run, and their results
|
|
196
|
-
*
|
|
197
|
-
* the
|
|
194
|
+
* reply with tool calls stored before its tools run, and their results
|
|
195
|
+
* after; each such write published as it commits, by the conversation (for
|
|
196
|
+
* the clients that follow it), and the final reply stored with the answer. At each step (before a request that follows the
|
|
198
197
|
* prompt or the tools' results), it takes the messages steered to the turn
|
|
199
198
|
* (`takeSteers`): the request then goes out from the conversation's
|
|
200
199
|
* history, which has them after the tools' results.
|
|
@@ -240,7 +239,6 @@ const runOf =
|
|
|
240
239
|
await turn.append(results);
|
|
241
240
|
runner.pushMessages(results);
|
|
242
241
|
}
|
|
243
|
-
await toolCallEnded(ctx);
|
|
244
242
|
await step();
|
|
245
243
|
}
|
|
246
244
|
let paused = false;
|
|
@@ -252,17 +250,21 @@ const runOf =
|
|
|
252
250
|
// Paused (a server tool's long turn): a step of the turn, not a reply to
|
|
253
251
|
// end it with, and the model goes on from it.
|
|
254
252
|
if (message.stop_reason === 'pause_turn') {
|
|
253
|
+
// Stored before the model goes on: a crash in a server tool's long turn goes on from it, rather than
|
|
254
|
+
// ask the whole of it again.
|
|
255
255
|
await turn.append(said);
|
|
256
|
+
await conversation.flush();
|
|
256
257
|
paused = true;
|
|
257
258
|
break;
|
|
258
259
|
}
|
|
260
|
+
// A reply with tool calls commits, and is published, before its tools run; the final reply commits with
|
|
261
|
+
// the turn's answer.
|
|
259
262
|
await turn.appendReply([said]);
|
|
260
263
|
if (!message.content.some((c) => c.type === 'tool_use')) return textOf(message.content);
|
|
261
264
|
await toolCallStarted(ctx);
|
|
262
265
|
const results = await runner.generateToolResponse();
|
|
263
266
|
check();
|
|
264
267
|
if (results) await turn.append(results);
|
|
265
|
-
await toolCallEnded(ctx);
|
|
266
268
|
await step();
|
|
267
269
|
}
|
|
268
270
|
if (!paused) throw new Error('the tool runner ended without a reply');
|
package/src/client.ts
CHANGED
|
@@ -16,7 +16,7 @@ import * as clients from '@restatedev/restate-sdk-clients';
|
|
|
16
16
|
import type { VirtualObjectDefinition } from '@restatedev/restate-sdk-core';
|
|
17
17
|
import type { Answering, ChatMessage, Messages, MessagesRequest, Part } from './common.ts';
|
|
18
18
|
import type { Status, Stopped, Synced, SyncRequest } from './object.ts';
|
|
19
|
-
import type { Notifications, Resolved, Signal, Steered, Steering, Waiting, Watch } from './
|
|
19
|
+
import type { Notifications, Resolved, Signal, Steered, Steering, Waiting, Watch } from './companion.ts';
|
|
20
20
|
|
|
21
21
|
export type {
|
|
22
22
|
Answering,
|
package/src/common.ts
CHANGED
|
@@ -5,8 +5,34 @@
|
|
|
5
5
|
// budget.
|
|
6
6
|
import * as restate from '@restatedev/sessions';
|
|
7
7
|
import { cancelCalls, cancelled, definitionOf, Link, ScopeKey } from '@restatedev/sessions/internal';
|
|
8
|
-
import { settleCalls } from './calls.ts';
|
|
9
|
-
import { type Published, type Steer
|
|
8
|
+
import { flushHeld, settleCalls } from './calls.ts';
|
|
9
|
+
import { companionHere, type Published, type Steer } from './companion.ts';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The context of an agent's handler: a session context, from an object that
|
|
13
|
+
* has a companion.
|
|
14
|
+
*
|
|
15
|
+
* The agent's own surface takes it: the handlers and views of
|
|
16
|
+
* `sessionObject()` and of a framework's `agent()`, a `chat` handler's
|
|
17
|
+
* answer, and what only an agent does with a context ({@link chatTurn},
|
|
18
|
+
* {@link publish}). The layer a handler belongs to is then in its
|
|
19
|
+
* signature, where the person writing it reads it, rather than found out in
|
|
20
|
+
* the middle of a turn, after a model round-trip, looking like a tool
|
|
21
|
+
* failure.
|
|
22
|
+
*
|
|
23
|
+
* It is a signpost, not a fence: it adds nothing to a session context, so
|
|
24
|
+
* the two are the same type to the compiler, and `NoCompanion` stays the
|
|
25
|
+
* guard that stops a wait from an object without a companion.
|
|
26
|
+
*/
|
|
27
|
+
export interface AgentContext extends restate.SessionContext {}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The context of an agent's view: a view context, from an object that has a
|
|
31
|
+
* companion. What {@link AgentContext} is to a handler, for the views that
|
|
32
|
+
* read what the agent is doing, or tell its companion to (`status`,
|
|
33
|
+
* `pending`, `signal`, `steer`, `watch`).
|
|
34
|
+
*/
|
|
35
|
+
export interface AgentViewContext extends restate.ViewContext {}
|
|
10
36
|
|
|
11
37
|
/**
|
|
12
38
|
* A value, or a function of the invocation's context that gives it: the
|
|
@@ -273,16 +299,19 @@ export const settingsOf = (ctx: restate.SessionContext): Settings =>
|
|
|
273
299
|
/** Whether the agent of `ctx` runs in the background. */
|
|
274
300
|
const unfollowed = (ctx: restate.SessionContext) => agentOf(ctx)?.background === true;
|
|
275
301
|
|
|
276
|
-
/**
|
|
277
|
-
|
|
302
|
+
/**
|
|
303
|
+
* The companion of the agent of `ctx`, for `needs`: its own, at its key. An
|
|
304
|
+
* object without one fails with a terminal error (`NoCompanion`).
|
|
305
|
+
*/
|
|
306
|
+
const companionOfAgent = (ctx: restate.SessionContext, needs: string) => ctx.rpc(companionHere(ctx, needs), ctx.key);
|
|
278
307
|
|
|
279
308
|
/**
|
|
280
309
|
* The messages steered to a `chat` turn that it has not taken yet, as the
|
|
281
310
|
* companion last published them. The companion answers them to every
|
|
282
|
-
* `publish
|
|
283
|
-
*
|
|
284
|
-
* step (`takeSteers`). The newest
|
|
285
|
-
* once publish at once.
|
|
311
|
+
* `publish` (and `start`), which a turn makes as it starts and after each
|
|
312
|
+
* write of its steps that commits: so the turn knows of them without a call
|
|
313
|
+
* of its own, and finds them at its next step (`takeSteers`). The newest
|
|
314
|
+
* revision wins, as writes that run at once publish at once.
|
|
286
315
|
*/
|
|
287
316
|
export class Inbox {
|
|
288
317
|
revision = -1;
|
|
@@ -321,35 +350,54 @@ export const inboxOf = (ctx: restate.SessionContext) => INBOX.get(ctx);
|
|
|
321
350
|
* increases, and the clients that follow it read it again; while a framework
|
|
322
351
|
* answers ({@link answering}), the calls whose results it stored are
|
|
323
352
|
* forgotten first. It is a call to Restate, so it also sends what the
|
|
324
|
-
* handler wrote before, for them to read
|
|
353
|
+
* handler wrote before, for them to read: the rows the conversation holds
|
|
354
|
+
* back are written first (`flushHeld`), as the clients read them once told.
|
|
355
|
+
*
|
|
356
|
+
* Only a `chat` turn ({@link chatTurn}: after each commit of its steps) and
|
|
357
|
+
* `settle` publish, on an object with a companion: one without fails with a
|
|
358
|
+
* terminal error (`NoCompanion`), rather than call a companion nobody serves.
|
|
325
359
|
*/
|
|
326
|
-
export const publish = async (ctx:
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
const published = await
|
|
360
|
+
export const publish = async (ctx: AgentContext) => {
|
|
361
|
+
const companion = companionOfAgent(ctx, 'a publish');
|
|
362
|
+
await flushHeld(ctx);
|
|
363
|
+
const published = await companion.publish();
|
|
330
364
|
INBOX.get(ctx)?.saw(published);
|
|
331
365
|
};
|
|
332
366
|
|
|
333
|
-
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
367
|
+
/**
|
|
368
|
+
* What the turn running on `ctx` makes after each commit of its steps (a
|
|
369
|
+
* reply with tool calls, the tools' results), if anything: set by
|
|
370
|
+
* {@link chatTurn}, for a followed turn, to publish. The conversation takes
|
|
371
|
+
* it as it is, without knowing what it does (`answerOnce` hands it to
|
|
372
|
+
* `Conversation.hold`); a turn run outside a `chat` turn (a handler of your
|
|
373
|
+
* own that calls a framework's `prompt()`) has none: it stores its steps,
|
|
374
|
+
* and publishes nothing.
|
|
375
|
+
*/
|
|
376
|
+
const AFTER_STEPS = new ScopeKey<() => Promise<void>>('afterSteps');
|
|
377
|
+
|
|
378
|
+
/** What the turn running on `ctx` makes after each commit of its steps, if anything: see {@link AFTER_STEPS}. */
|
|
379
|
+
export const afterStepsOf = (ctx: restate.SessionContext): (() => Promise<void>) | undefined => AFTER_STEPS.get(ctx);
|
|
337
380
|
|
|
338
381
|
/**
|
|
339
|
-
* A tool call of the turn of `ctx` starts:
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
382
|
+
* A tool call of the turn of `ctx` starts: forget the calls whose results are
|
|
383
|
+
* stored (calls.ts) before the tool makes its own. It publishes nothing: a
|
|
384
|
+
* followed `chat` turn publishes once the call is stored (with its reply),
|
|
385
|
+
* and once its result is ({@link afterStepsOf}).
|
|
343
386
|
*/
|
|
344
387
|
export async function toolCallStarted(ctx: restate.SessionContext) {
|
|
345
|
-
|
|
346
|
-
const stored = ANSWERING.get(ctx);
|
|
347
|
-
if (stored) await settled(ctx, stored);
|
|
388
|
+
await settleAnswered(ctx);
|
|
348
389
|
}
|
|
349
390
|
|
|
350
|
-
/**
|
|
351
|
-
|
|
352
|
-
|
|
391
|
+
/**
|
|
392
|
+
* Forget the call records (calls.ts) whose results the turn answering on
|
|
393
|
+
* `ctx` has stored ({@link answering}), if one does: after each commit of its
|
|
394
|
+
* steps (`answerOnce` does, before what the turn asked for then, such as a
|
|
395
|
+
* `chat` turn's publish, for every turn, in a `chat` turn or not), as a tool
|
|
396
|
+
* call starts, and as the turn ends.
|
|
397
|
+
*/
|
|
398
|
+
export async function settleAnswered(ctx: restate.SessionContext) {
|
|
399
|
+
const stored = ANSWERING.get(ctx);
|
|
400
|
+
if (stored) await settled(ctx, stored);
|
|
353
401
|
}
|
|
354
402
|
|
|
355
403
|
/** Whether a tool call's result is stored: `stored(callId, failed)`, `failed` the error the call answered with. */
|
|
@@ -371,8 +419,9 @@ function settled(ctx: restate.SessionContext, stored: Stored): Promise<void> {
|
|
|
371
419
|
|
|
372
420
|
/**
|
|
373
421
|
* Run `work`, a framework's turn on the conversation of turns, forgetting
|
|
374
|
-
* the calls whose results it stores as it goes: each
|
|
375
|
-
* (
|
|
422
|
+
* the calls whose results it stores as it goes: each write of its steps
|
|
423
|
+
* that commits ({@link settleAnswered}), each tool call's start, and its
|
|
424
|
+
* end, forget the Restate calls of
|
|
376
425
|
* the tool calls whose results `stored` says are stored (calls.ts).
|
|
377
426
|
*/
|
|
378
427
|
export async function answering<T>(ctx: restate.SessionContext, stored: Stored, work: () => Promise<T>): Promise<T> {
|
|
@@ -403,14 +452,14 @@ export const aborted = (signal: AbortSignal) =>
|
|
|
403
452
|
});
|
|
404
453
|
|
|
405
454
|
/** A `chat` handler's answer to a message: a framework's prompt, say. */
|
|
406
|
-
export type Chat<A = unknown> = (ctx:
|
|
455
|
+
export type Chat<A = unknown> = (ctx: AgentContext, message: string) => Promise<A>;
|
|
407
456
|
|
|
408
457
|
/**
|
|
409
458
|
* Whether a `chat` handler's turns take steered messages: for every message,
|
|
410
459
|
* or for each, as the invocation's context says (a framework's options that
|
|
411
460
|
* a function gives, say).
|
|
412
461
|
*/
|
|
413
|
-
export type Steers = boolean | ((ctx:
|
|
462
|
+
export type Steers = boolean | ((ctx: AgentContext) => boolean | Promise<boolean>);
|
|
414
463
|
|
|
415
464
|
/** What makes a `chat` handler (`chatHandler`): its answer, and whether it takes steered messages. */
|
|
416
465
|
const CHAT = new Link<{ answer: Chat; steers: Steers }>('restate-agents.chat');
|
|
@@ -434,8 +483,9 @@ export const chatOf = (entry: object) => CHAT.of(entry);
|
|
|
434
483
|
* attempt that ends first (`attemptCompletedSignal`) aborts it too, with an
|
|
435
484
|
* {@link AttemptEnded}: the work stops, and the next attempt goes on with it.
|
|
436
485
|
*
|
|
437
|
-
* It publishes when it starts
|
|
438
|
-
* that follow the conversation
|
|
486
|
+
* It publishes when it starts (its companion's `start` does) and ends
|
|
487
|
+
* ({@link publish}), for the clients that follow the conversation, unless
|
|
488
|
+
* the agent runs in the background, which publishes the end only.
|
|
439
489
|
*
|
|
440
490
|
* As the `chat` handler of an agent, it also answers each message sent to
|
|
441
491
|
* the agent's `steer` view that no turn took, as a turn of its own (the
|
|
@@ -448,7 +498,7 @@ export const chatHandler = <A = string>(answer: Chat<A>, { steers = false }: { s
|
|
|
448
498
|
input: messageSerde,
|
|
449
499
|
accept: '*/*',
|
|
450
500
|
explicitCancellation: true,
|
|
451
|
-
handler: (ctx:
|
|
501
|
+
handler: (ctx: AgentContext, text: string): Promise<A> => chatTurn(ctx, text, answer, steers),
|
|
452
502
|
},
|
|
453
503
|
{ answer, steers },
|
|
454
504
|
);
|
|
@@ -460,7 +510,7 @@ export const chatHandler = <A = string>(answer: Chat<A>, { steers = false }: { s
|
|
|
460
510
|
* in the background).
|
|
461
511
|
*/
|
|
462
512
|
export async function chatTurn<A>(
|
|
463
|
-
ctx:
|
|
513
|
+
ctx: AgentContext,
|
|
464
514
|
text: string,
|
|
465
515
|
answer: Chat<A>,
|
|
466
516
|
steers: Steers = false,
|
|
@@ -473,20 +523,28 @@ export async function chatTurn<A>(
|
|
|
473
523
|
const steering = !unfollowed(ctx) && (typeof steers === 'function' ? await steers(ctx) : steers);
|
|
474
524
|
// Before the first publish, which may answer messages steered to the turn already (a retried attempt's).
|
|
475
525
|
if (steering) INBOX.set(ctx, new Inbox());
|
|
476
|
-
|
|
526
|
+
// A followed turn's start is its first publish: the companion makes it, and returns what `publish` returns.
|
|
527
|
+
const followed = !unfollowed(ctx);
|
|
528
|
+
const companion = companionOfAgent(ctx, 'a chat turn');
|
|
529
|
+
const started = await companion.start({
|
|
477
530
|
invocationId,
|
|
478
531
|
since: new Date().toISOString(),
|
|
479
532
|
...(settles ? { settles } : {}),
|
|
480
533
|
...(steering ? { steers: steering } : {}),
|
|
534
|
+
...(followed ? { publishes: true } : {}),
|
|
481
535
|
});
|
|
482
|
-
|
|
536
|
+
INBOX.get(ctx)?.saw(started);
|
|
537
|
+
// A followed turn publishes after each commit of its steps, for its
|
|
538
|
+
// followers to read them there: the conversation makes what the turn sets
|
|
539
|
+
// here (`afterStepsOf`), without knowing it publishes. A background turn
|
|
540
|
+
// publishes its end only.
|
|
541
|
+
if (followed) AFTER_STEPS.set(ctx, () => publish(ctx));
|
|
483
542
|
// The log takes the turn's last change, then the companion closes the
|
|
484
543
|
// turn, which publishes it: one revision. The close is a call the turn
|
|
485
544
|
// waits for, so that it is done before the turn ends (see `awaitTurn`).
|
|
486
545
|
const end = async () => {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
await signalsOfAgent(ctx).close(invocationId);
|
|
546
|
+
await settleAnswered(ctx);
|
|
547
|
+
await companion.close(invocationId);
|
|
490
548
|
};
|
|
491
549
|
const turn = new AbortController();
|
|
492
550
|
TURN.set(ctx, turn.signal);
|
|
@@ -517,6 +575,7 @@ export async function chatTurn<A>(
|
|
|
517
575
|
} finally {
|
|
518
576
|
// An attempt's signal aborts at every attempt's end, the turn's too.
|
|
519
577
|
attempt.removeEventListener('abort', attemptEnded);
|
|
578
|
+
AFTER_STEPS.delete(ctx);
|
|
520
579
|
}
|
|
521
580
|
await end();
|
|
522
581
|
return answered;
|