@restatedev/agents 0.1.0 → 0.2.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 +25 -41
- package/dist/claude/index.d.ts +0 -1
- package/dist/claude/index.d.ts.map +1 -1
- package/dist/client.d.ts +2 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/common.d.ts +4 -27
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +18 -55
- package/dist/common.js.map +1 -1
- package/dist/deepseek/index.d.ts +0 -1
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +7 -25
- package/dist/deepseek/index.js.map +1 -1
- package/dist/index.d.ts +3 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -28
- package/dist/index.js.map +1 -1
- package/dist/langgraph/index.d.ts +0 -1
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/object.d.ts +7 -19
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +7 -18
- package/dist/object.js.map +1 -1
- package/dist/opencode/index.d.ts +0 -1
- package/dist/opencode/index.d.ts.map +1 -1
- package/dist/opencode/index.js +14 -42
- package/dist/opencode/index.js.map +1 -1
- package/dist/pi/index.d.ts +0 -1
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +2 -22
- package/dist/pi/index.js.map +1 -1
- package/dist/signals.d.ts +16 -57
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +86 -88
- package/dist/signals.js.map +1 -1
- package/dist/webhook.d.ts +2 -4
- package/dist/webhook.d.ts.map +1 -1
- package/dist/webhook.js +2 -4
- package/dist/webhook.js.map +1 -1
- package/package.json +3 -3
- package/src/client.ts +1 -4
- package/src/common.ts +17 -60
- package/src/deepseek/index.ts +5 -28
- package/src/index.ts +3 -39
- package/src/object.ts +7 -31
- package/src/opencode/index.ts +13 -49
- package/src/pi/index.ts +1 -21
- package/src/signals.ts +98 -138
- package/src/webhook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,EAAE,WAAW,IAAI,IAAI,QAAQ,EAAE,EAAE,CAAC,CAAC;YAC9G,CAAC,CACF;SACF;KACF,CAAC,CAAC;AACL,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@restatedev/agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
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": {
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
"@opencode/sdk": "2.0.32",
|
|
104
104
|
"@opencode/server": "2.0.32",
|
|
105
105
|
"@restatedev/restate-sdk": "1.18.2",
|
|
106
|
-
"@restatedev/sessions": "0.
|
|
106
|
+
"@restatedev/sessions": "0.2.0",
|
|
107
107
|
"effect": "4.0.1",
|
|
108
108
|
"zod": "4.6.5"
|
|
109
109
|
},
|
|
@@ -133,7 +133,7 @@
|
|
|
133
133
|
"@opencode/plugin": "2.0.32",
|
|
134
134
|
"@opencode/sdk": "2.0.32",
|
|
135
135
|
"@opencode/server": "2.0.32",
|
|
136
|
-
"@restatedev/sessions": "^0.
|
|
136
|
+
"@restatedev/sessions": "^0.2.0",
|
|
137
137
|
"effect": "4.0.1",
|
|
138
138
|
"zod": "^4.6.5"
|
|
139
139
|
},
|
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 { ChatMessage, Messages, MessagesRequest, Part, Turn } from './common.ts';
|
|
18
18
|
import type { Status, Stopped, Synced, SyncRequest } from './object.ts';
|
|
19
|
-
import type { Notifications, Resolved, Signal,
|
|
19
|
+
import type { Notifications, Resolved, Signal, Waiting, Watch } from './signals.ts';
|
|
20
20
|
|
|
21
21
|
export type {
|
|
22
22
|
ChatMessage,
|
|
@@ -27,7 +27,6 @@ export type {
|
|
|
27
27
|
Resolved,
|
|
28
28
|
Signal,
|
|
29
29
|
Status,
|
|
30
|
-
Steered,
|
|
31
30
|
Stopped,
|
|
32
31
|
Synced,
|
|
33
32
|
SyncRequest,
|
|
@@ -50,8 +49,6 @@ export interface Agent {
|
|
|
50
49
|
stop(ctx: unknown): Promise<Stopped>;
|
|
51
50
|
/** Forget the conversation (stopping the message it answers): its database and state too. */
|
|
52
51
|
clear(ctx: unknown): Promise<Stopped>;
|
|
53
|
-
/** Add `message` to the message it answers, at its next tool call (or answer it as a message of its own). */
|
|
54
|
-
steer(ctx: unknown, message: string): Promise<Steered>;
|
|
55
52
|
/** What its tools wait for. */
|
|
56
53
|
pending(ctx: unknown): Promise<Waiting[]>;
|
|
57
54
|
/** Resolve a wait: by its id, or the oldest for a name. */
|
package/src/common.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// What the frameworks share: their options (`Configure`, the model), the
|
|
2
2
|
// ids of an invocation's prompts, the `chat` handler with its turn (status,
|
|
3
|
-
// cancellation,
|
|
3
|
+
// cancellation, publishing for the clients that follow it), and
|
|
4
4
|
// the `messages` handler with its `Transcript`.
|
|
5
5
|
import * as restate from '@restatedev/sessions';
|
|
6
6
|
import { cancelCalls, cancelled } from '@restatedev/sessions/internal';
|
|
7
7
|
import { settle } from './calls.ts';
|
|
8
8
|
import * as log from './log.ts';
|
|
9
9
|
import type { Said } from './log.ts';
|
|
10
|
-
import {
|
|
10
|
+
import { signals, signalsOf } from './signals.ts';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* A value, or a function of the invocation's context that gives it: the
|
|
@@ -158,17 +158,6 @@ export class AttemptEnded extends Error {
|
|
|
158
158
|
*/
|
|
159
159
|
export const stopSignalOf = (ctx: object): AbortSignal | undefined => turns.get(ctx);
|
|
160
160
|
|
|
161
|
-
/** The ids of the messages steered to each turn that its conversation has, by its context. */
|
|
162
|
-
const steerings = new WeakMap<object, Set<string>>();
|
|
163
|
-
|
|
164
|
-
/** A framework's conversation, while it answers a message: what steering needs of it. */
|
|
165
|
-
export interface Steerable {
|
|
166
|
-
/** Give it `message`, for its next step: once, by the message's id, however often it is given. */
|
|
167
|
-
steer(message: Steer): Promise<void>;
|
|
168
|
-
/** Whether it has message `id`: given in this attempt, or an earlier one. */
|
|
169
|
-
has(id: string): Promise<boolean>;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
161
|
/** The agents that run in the background (`background: true`): no one follows them as they work. */
|
|
173
162
|
const background = new Set<string>();
|
|
174
163
|
|
|
@@ -239,36 +228,9 @@ export async function toolCallStarted(ctx: restate.SessionContext) {
|
|
|
239
228
|
}
|
|
240
229
|
}
|
|
241
230
|
|
|
242
|
-
/**
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
* does not have yet: its next step takes them. Without a conversation (a
|
|
246
|
-
* framework that takes no steering), they wait for the turn's end.
|
|
247
|
-
*/
|
|
248
|
-
export async function toolCallEnded(ctx: restate.SessionContext, conversation?: Steerable) {
|
|
249
|
-
// In the background, no one steers: a message steered anyway is answered after the turn.
|
|
250
|
-
if (unfollowed(ctx)) return;
|
|
251
|
-
const messages = await signalsOfAgent(ctx).progress(ctx.request.id);
|
|
252
|
-
const taken = steerings.get(ctx);
|
|
253
|
-
if (!taken || !conversation) return;
|
|
254
|
-
for (const message of messages.filter((m) => !taken.has(m.id))) {
|
|
255
|
-
if (!(await conversation.has(message.id))) await conversation.steer(message);
|
|
256
|
-
taken.add(message.id);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Count the messages steered to the turn of `ctx` that `conversation` has,
|
|
262
|
-
* such as those an earlier attempt gave it. A framework calls it once a
|
|
263
|
-
* prompt is answered; the turn's other messages are answered as messages of
|
|
264
|
-
* their own, when it ends.
|
|
265
|
-
*/
|
|
266
|
-
export async function settleSteering(ctx: restate.SessionContext, conversation: Pick<Steerable, 'has'>) {
|
|
267
|
-
const taken = steerings.get(ctx);
|
|
268
|
-
if (!taken) return;
|
|
269
|
-
for (const message of await signalsOfAgent(ctx).steering(ctx.request.id)) {
|
|
270
|
-
if (!taken.has(message.id) && (await conversation.has(message.id))) taken.add(message.id);
|
|
271
|
-
}
|
|
231
|
+
/** A tool call of the turn of `ctx` (a `chat` handler's) ended: {@link publish} it, unless the agent runs in the background. */
|
|
232
|
+
export async function toolCallEnded(ctx: restate.SessionContext) {
|
|
233
|
+
await stepped(ctx);
|
|
272
234
|
}
|
|
273
235
|
|
|
274
236
|
/** A promise that rejects with `signal`'s reason once it aborts. */
|
|
@@ -294,11 +256,8 @@ export const aborted = (signal: AbortSignal) =>
|
|
|
294
256
|
* attempt that ends first (`attemptCompletedSignal`) aborts it too, with an
|
|
295
257
|
* {@link AttemptEnded}: the work stops, and the next attempt goes on with it.
|
|
296
258
|
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
* answered as messages of their own, unless it was cancelled. It publishes
|
|
300
|
-
* when it starts and ends ({@link publish}), for the clients that follow the
|
|
301
|
-
* conversation.
|
|
259
|
+
* It publishes when it starts and ends ({@link publish}), for the clients
|
|
260
|
+
* that follow the conversation.
|
|
302
261
|
*/
|
|
303
262
|
export const chatHandler = <A = string>(answer: (ctx: restate.SessionContext, message: string) => Promise<A>) => ({
|
|
304
263
|
input: messageSerde,
|
|
@@ -306,18 +265,16 @@ export const chatHandler = <A = string>(answer: (ctx: restate.SessionContext, me
|
|
|
306
265
|
explicitCancellation: true,
|
|
307
266
|
handler: async (ctx: restate.SessionContext, text: string): Promise<A> => {
|
|
308
267
|
const invocationId = ctx.request.id;
|
|
309
|
-
// The companion knows the turn from here to its close: `status
|
|
310
|
-
await ctx
|
|
311
|
-
.rpc(signals, signalsOf(ctx))
|
|
312
|
-
.start({ agent: agentOf(ctx), turn: { invocationId, since: new Date().toISOString() } });
|
|
313
|
-
const taken = new Set<string>();
|
|
314
|
-
steerings.set(ctx, taken);
|
|
268
|
+
// The companion knows the turn from here to its close: `status` and `stop` ask it.
|
|
269
|
+
await ctx.rpc(signals, signalsOf(ctx)).start({ invocationId, since: new Date().toISOString() });
|
|
315
270
|
await stepped(ctx);
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
271
|
+
// The log takes the turn's last change, then the companion closes the
|
|
272
|
+
// turn, which publishes it: one revision. The close is a call the turn
|
|
273
|
+
// waits for, so that it is done before the turn ends (see `awaitTurn`).
|
|
274
|
+
const end = async () => {
|
|
275
|
+
const read = reading.get(ctx);
|
|
276
|
+
if (read) await logged(ctx, read);
|
|
277
|
+
await signalsOfAgent(ctx).close(invocationId);
|
|
321
278
|
};
|
|
322
279
|
const turn = new AbortController();
|
|
323
280
|
turns.set(ctx, turn.signal);
|
|
@@ -337,7 +294,7 @@ export const chatHandler = <A = string>(answer: (ctx: restate.SessionContext, me
|
|
|
337
294
|
// Cancelled once answered: the answer stands.
|
|
338
295
|
} catch (error) {
|
|
339
296
|
if (error instanceof restate.TerminalError) {
|
|
340
|
-
await end(
|
|
297
|
+
await end();
|
|
341
298
|
throw error;
|
|
342
299
|
}
|
|
343
300
|
// Any other error fails the attempt, and Restate retries the turn.
|
package/src/deepseek/index.ts
CHANGED
|
@@ -51,7 +51,6 @@ import {
|
|
|
51
51
|
} from '@deepseek-ai/dsh-tools';
|
|
52
52
|
import {
|
|
53
53
|
aborted,
|
|
54
|
-
AttemptEnded,
|
|
55
54
|
CANCELLED,
|
|
56
55
|
chatHandler,
|
|
57
56
|
checkModelName,
|
|
@@ -64,8 +63,6 @@ import {
|
|
|
64
63
|
nextPromptId,
|
|
65
64
|
providerOf,
|
|
66
65
|
refusal,
|
|
67
|
-
settleSteering,
|
|
68
|
-
type Steerable,
|
|
69
66
|
stopSignalOf,
|
|
70
67
|
storedAnswer,
|
|
71
68
|
toolCallEnded,
|
|
@@ -150,7 +147,6 @@ async function boot(db: restate.Database, route: Route, system?: string): Promis
|
|
|
150
147
|
const CONTINUE = 'Continue: your last turn was interrupted. Finish the request.';
|
|
151
148
|
|
|
152
149
|
const promptId = (id: string) => MessageId(`prompt:${id}`);
|
|
153
|
-
const steerId = (id: string) => MessageId(`steer:${id}`);
|
|
154
150
|
const textOf = (message: Message) => message.content.flatMap((c) => (c.type === 'text' ? [c.text] : [])).join('');
|
|
155
151
|
|
|
156
152
|
/**
|
|
@@ -308,11 +304,10 @@ const answerOnce = async (
|
|
|
308
304
|
let agent: Agent | undefined;
|
|
309
305
|
try {
|
|
310
306
|
const sessionId = SessionId(ctx.key);
|
|
311
|
-
const steerable = () => steeringOf(agent!);
|
|
312
307
|
const options = {
|
|
313
308
|
agentOptions: route,
|
|
314
309
|
setup: (agentCtx: Context) => {
|
|
315
|
-
for (const t of tools) agentCtx.tools.register(tracked(isTool(t) ? native(t, ctx) : t, ctx,
|
|
310
|
+
for (const t of tools) agentCtx.tools.register(tracked(isTool(t) ? native(t, ctx) : t, ctx, fail));
|
|
316
311
|
},
|
|
317
312
|
};
|
|
318
313
|
// Resuming closes a turn a crash left open.
|
|
@@ -321,13 +316,8 @@ const answerOnce = async (
|
|
|
321
316
|
: await harness.agents.create({ sessionId, ...options });
|
|
322
317
|
agent = started.agent;
|
|
323
318
|
const answer = await answered(harness, agent, id, text, failed);
|
|
324
|
-
await settleSteering(ctx, steerable());
|
|
325
319
|
return answer;
|
|
326
320
|
} catch (e) {
|
|
327
|
-
// A turn that ends unanswered (cancelled, refused) counts the messages
|
|
328
|
-
// steered to it that the session has, given in an earlier attempt too:
|
|
329
|
-
// its close answers only the others. An attempt that ended closes nothing.
|
|
330
|
-
if (agent && !(e instanceof AttemptEnded)) await settleSteering(ctx, steeringOf(agent)).catch(() => {});
|
|
331
321
|
// A cancelled invocation cancels the agent's turn, which the log keeps.
|
|
332
322
|
if (e instanceof restate.CancelledError && agent) await cancelTurn(harness, agent);
|
|
333
323
|
else (harness.sessionPersistence as SqlSessionPersistence).abandon();
|
|
@@ -376,25 +366,13 @@ async function cancelTurn(harness: Context, agent: Agent) {
|
|
|
376
366
|
await harness.sessions.flush(agent.session);
|
|
377
367
|
}
|
|
378
368
|
|
|
379
|
-
/**
|
|
380
|
-
* Messages steered to the turn: the agent's steering, which its next step
|
|
381
|
-
* takes (after the tool calls), and the log keeps, by id.
|
|
382
|
-
*/
|
|
383
|
-
const steeringOf = (agent: Agent): Steerable => ({
|
|
384
|
-
steer: async (steered) => agent.steer(message(steerId(steered.id), steered.text)),
|
|
385
|
-
has: async (steered) =>
|
|
386
|
-
agent.session.snapshotEvents().some((e) => e.type === 'user/message' && e.data.id === steerId(steered)),
|
|
387
|
-
});
|
|
388
|
-
|
|
389
369
|
/**
|
|
390
370
|
* `definition`, its calls published as they start and end (for the clients
|
|
391
|
-
* that follow the conversation
|
|
392
|
-
* attempt.
|
|
371
|
+
* that follow the conversation), and its error failing the attempt.
|
|
393
372
|
*/
|
|
394
373
|
const tracked = (
|
|
395
374
|
definition: ToolDefinition,
|
|
396
375
|
ctx: restate.SessionContext,
|
|
397
|
-
steerable: () => Steerable,
|
|
398
376
|
fail: (error: unknown) => void,
|
|
399
377
|
): ToolDefinition => ({
|
|
400
378
|
...definition,
|
|
@@ -402,7 +380,7 @@ const tracked = (
|
|
|
402
380
|
try {
|
|
403
381
|
await toolCallStarted(ctx);
|
|
404
382
|
const result = await definition.execute(args, exec);
|
|
405
|
-
await toolCallEnded(ctx
|
|
383
|
+
await toolCallEnded(ctx);
|
|
406
384
|
return result;
|
|
407
385
|
} catch (error) {
|
|
408
386
|
fromTool(error);
|
|
@@ -458,9 +436,8 @@ export const messages = () => messagesHandler();
|
|
|
458
436
|
const transcribe = async (db: Database, key: string) => {
|
|
459
437
|
const transcript = new Transcript();
|
|
460
438
|
for (const e of await storedEvents(db, SessionId(key))) {
|
|
461
|
-
// The prompts
|
|
462
|
-
|
|
463
|
-
if (e.type === 'user/message' && /^(prompt|steer):/.test(e.data.id)) transcript.say('user', textOf(e.data));
|
|
439
|
+
// The prompts, not the requests to continue an interrupted turn.
|
|
440
|
+
if (e.type === 'user/message' && e.data.id.startsWith('prompt:')) transcript.say('user', textOf(e.data));
|
|
464
441
|
if (e.type === 'assistant/message') transcript.say('assistant', textOf(e.data.message));
|
|
465
442
|
if (e.type === 'tool/call') transcript.call(e.data.callId, e.data.name, parsed(e.data.arguments));
|
|
466
443
|
if (e.type === 'tool/result')
|
package/src/index.ts
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
// is a subpath (@restatedev/agents/pi, /opencode, /langgraph, /claude,
|
|
3
3
|
// /deepseek); this is what they share: session objects, tools for
|
|
4
4
|
// any of them, and the tools that are Restate itself.
|
|
5
|
-
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
6
|
-
import { signals } from './signals.ts';
|
|
7
5
|
|
|
8
6
|
export {
|
|
9
7
|
CANCELLED,
|
|
@@ -18,43 +16,9 @@ export {
|
|
|
18
16
|
type Configure,
|
|
19
17
|
} from './common.ts';
|
|
20
18
|
export { sessionObject, type Status, type Stopped } from './object.ts';
|
|
21
|
-
export {
|
|
22
|
-
companionOf,
|
|
23
|
-
deliver,
|
|
24
|
-
signals,
|
|
25
|
-
type Companion,
|
|
26
|
-
type Resolved,
|
|
27
|
-
type Signal,
|
|
28
|
-
type Steer,
|
|
29
|
-
type Steered,
|
|
30
|
-
type Waiting,
|
|
31
|
-
} from './signals.ts';
|
|
19
|
+
export { companionOf, deliver, signals, type Companion, type Resolved, type Signal, type Waiting } from './signals.ts';
|
|
32
20
|
export { call, send, signal, subagent, tool, workflow, type Tool, type ToolCall, type ToolContext } from './tools.ts';
|
|
33
21
|
export { webhook, webhookMessage, type WebhookAuth, type WebhookEvent, type WebhookOptions } from './webhook.ts';
|
|
34
22
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
* them (the `signals` object). RESTATE_SERVE_ONLY, names separated by commas
|
|
38
|
-
* (`triage,planner,signals`), serves only those of them: one image, deployed
|
|
39
|
-
* in parts. The agents reach `signals` through Restate, so one part serves it
|
|
40
|
-
* for them all.
|
|
41
|
-
*/
|
|
42
|
-
export const serve = (...services: restate.ServeOptions['services']) =>
|
|
43
|
-
restate.serve({
|
|
44
|
-
services: only([...services.filter((s) => s !== signals), signals], process.env.RESTATE_SERVE_ONLY),
|
|
45
|
-
port: Number(process.env.PORT ?? 9080),
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
/** The `services` that `names` (comma separated) names, or all of them without names; a name of none is an error. */
|
|
49
|
-
function only(services: restate.ServeOptions['services'], names: string | undefined) {
|
|
50
|
-
const wanted = (names ?? '')
|
|
51
|
-
.split(',')
|
|
52
|
-
.map((name) => name.trim())
|
|
53
|
-
.filter(Boolean);
|
|
54
|
-
if (wanted.length === 0) return services;
|
|
55
|
-
const known = services.map((s) => s.name);
|
|
56
|
-
const unknown = wanted.filter((name) => !known.includes(name));
|
|
57
|
-
if (unknown.length > 0)
|
|
58
|
-
throw new Error(`RESTATE_SERVE_ONLY names ${unknown.join(', ')}: the services are ${known.join(', ')}`);
|
|
59
|
-
return services.filter((s) => wanted.includes(s.name));
|
|
60
|
-
}
|
|
23
|
+
/** @restatedev/sessions' `serve`: the services on PORT, with what they need beside them (an agent's `signals`). */
|
|
24
|
+
export { serve } from '@restatedev/sessions';
|
package/src/object.ts
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
1
|
// Virtual objects set up for SQL and agents.
|
|
2
2
|
import * as restate from '@restatedev/sessions';
|
|
3
3
|
import { reset } from '@restatedev/sessions/internal';
|
|
4
|
-
import {
|
|
5
|
-
cleared,
|
|
6
|
-
onClear,
|
|
7
|
-
inBackground,
|
|
8
|
-
type Messages,
|
|
9
|
-
type MessagesRequest,
|
|
10
|
-
messageSerde,
|
|
11
|
-
refusal,
|
|
12
|
-
type Turn,
|
|
13
|
-
} from './common.ts';
|
|
4
|
+
import { cleared, onClear, inBackground, type Messages, type MessagesRequest, refusal, type Turn } from './common.ts';
|
|
14
5
|
import * as log from './log.ts';
|
|
15
6
|
import {
|
|
16
|
-
agentOf,
|
|
17
7
|
type Notifications,
|
|
18
8
|
type Resolved,
|
|
19
9
|
type Signal,
|
|
20
|
-
type Steered,
|
|
21
10
|
signals,
|
|
22
11
|
signalsOf,
|
|
23
12
|
withCompanion,
|
|
@@ -81,8 +70,8 @@ const WAITS = { inactivityTimeout: { minutes: 5 } } satisfies restate.HandlerOpt
|
|
|
81
70
|
* The views every object here has, beside its own: `signal`, which resolves
|
|
82
71
|
* a wait of the object's tools (see `signal` in tools.ts), `pending`, the
|
|
83
72
|
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
84
|
-
* it answers,
|
|
85
|
-
*
|
|
73
|
+
* it answers, and `notifications` and `watch`, the revision of its
|
|
74
|
+
* conversation, and a wait for a newer one (an agent's
|
|
86
75
|
* has `clear` and `sync` too: {@link followed}). Each asks the object's
|
|
87
76
|
* `signals` companion, which knows what the object does: they run while the
|
|
88
77
|
* object's handler works.
|
|
@@ -94,18 +83,6 @@ const BUILT_IN = {
|
|
|
94
83
|
status: async (ctx: restate.ViewContext): Promise<Status> => companion(ctx).status(),
|
|
95
84
|
/** Cancel the invocation of the message being answered (`status`'s), as Restate's admin API cancels one. */
|
|
96
85
|
stop: async (ctx: restate.ViewContext): Promise<Stopped> => stopped(ctx, 'stop'),
|
|
97
|
-
/**
|
|
98
|
-
* Add `text` to the message being answered (`status`'s), such as "Only
|
|
99
|
-
* Monday, please": the agent takes it at its next tool call, as its
|
|
100
|
-
* framework steers a turn. A turn that ends first answers it as a message
|
|
101
|
-
* of its own, as `chat` does when no message is answered.
|
|
102
|
-
*/
|
|
103
|
-
steer: {
|
|
104
|
-
input: messageSerde,
|
|
105
|
-
accept: '*/*',
|
|
106
|
-
handler: async (ctx: restate.ViewContext, text: string): Promise<Steered> =>
|
|
107
|
-
companion(ctx, 'steer').steer({ agent: agentOf(ctx), text }),
|
|
108
|
-
},
|
|
109
86
|
/** The revision of the conversation: each change of what `messages` and `status` give increases it. */
|
|
110
87
|
notifications: async (ctx: restate.ViewContext): Promise<Notifications> => companion(ctx).notifications(),
|
|
111
88
|
/**
|
|
@@ -133,8 +110,8 @@ const BUILT_IN = {
|
|
|
133
110
|
const followed = (messages: (ctx: restate.ViewContext, request: MessagesRequest) => Promise<Messages>) => ({
|
|
134
111
|
/**
|
|
135
112
|
* Forget the conversation: stop the message being answered (as `stop`),
|
|
136
|
-
* then empty the key's database, and what its `signals` hold (waits
|
|
137
|
-
* signals
|
|
113
|
+
* then empty the key's database, and what its `signals` hold (waits and
|
|
114
|
+
* kept signals). The log's ids and cursors go on (see
|
|
138
115
|
* log.ts), so a follower reads on, and drops the messages before `first`.
|
|
139
116
|
* A message sent before it is answered first, then forgotten too; one sent
|
|
140
117
|
* after starts anew.
|
|
@@ -223,8 +200,7 @@ export interface Definition<P extends string> {
|
|
|
223
200
|
/**
|
|
224
201
|
* Whether it works unwatched, such as a subagent: its turn calls its
|
|
225
202
|
* `signals` only at its end, not at each step, so a client that follows it
|
|
226
|
-
* sees each turn once it ended
|
|
227
|
-
* end. `messages` has every turn as before.
|
|
203
|
+
* sees each turn once it ended. `messages` has every turn as before.
|
|
228
204
|
*/
|
|
229
205
|
background?: boolean;
|
|
230
206
|
}
|
|
@@ -258,7 +234,7 @@ type With<H, V> = Functions<typeof BUILT_IN> &
|
|
|
258
234
|
*
|
|
259
235
|
* Besides its own handlers and views, it has the views `status`, what it is
|
|
260
236
|
* doing (the message it answers, and what its tools wait for), `stop`,
|
|
261
|
-
* which stops that message, `
|
|
237
|
+
* which stops that message, `pending`, the waits
|
|
262
238
|
* of its tools (`signal()`), `signal({ id, value })`, which resolves one,
|
|
263
239
|
* and `notifications` and `watch({ revision })`, to follow its
|
|
264
240
|
* conversation. An agent, an object with a `messages` view (such as
|
package/src/opencode/index.ts
CHANGED
|
@@ -42,7 +42,6 @@ import type { Plugin } from '@opencode/plugin';
|
|
|
42
42
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
43
43
|
import {
|
|
44
44
|
alsoClearing,
|
|
45
|
-
AttemptEnded,
|
|
46
45
|
chatHandler,
|
|
47
46
|
CHECK_MODEL,
|
|
48
47
|
checkModelName,
|
|
@@ -54,8 +53,6 @@ import {
|
|
|
54
53
|
providerOf,
|
|
55
54
|
refusal,
|
|
56
55
|
REFUSED,
|
|
57
|
-
settleSteering,
|
|
58
|
-
type Steerable,
|
|
59
56
|
stopSignalOf,
|
|
60
57
|
toolCallEnded,
|
|
61
58
|
toolCallStarted,
|
|
@@ -189,23 +186,17 @@ function toolsPlugin(tools: OpenCodeTool[], fail: (error: unknown) => Promise<ne
|
|
|
189
186
|
}
|
|
190
187
|
|
|
191
188
|
/**
|
|
192
|
-
* An OpenCode plugin that publishes each tool call as it starts and ends
|
|
193
|
-
*
|
|
194
|
-
* messages steered to the turn (`steerable`) at its end: OpenCode's
|
|
195
|
-
* steering, which its next step takes.
|
|
189
|
+
* An OpenCode plugin that publishes each tool call as it starts and ends,
|
|
190
|
+
* for the clients that follow the conversation.
|
|
196
191
|
*/
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
steerable: Steerable,
|
|
200
|
-
fail: (error: unknown) => Promise<never>,
|
|
201
|
-
): Plugin.Plugin => ({
|
|
202
|
-
id: 'restate-agents.steering',
|
|
192
|
+
const followPlugin = (ctx: restate.SessionContext, fail: (error: unknown) => Promise<never>): Plugin.Plugin => ({
|
|
193
|
+
id: 'restate-agents.follow',
|
|
203
194
|
async setup(plugin) {
|
|
204
195
|
const before = await plugin.tool.hook('execute.before', async () => {
|
|
205
196
|
await toolCallStarted(ctx).catch(fail);
|
|
206
197
|
});
|
|
207
198
|
const after = await plugin.tool.hook('execute.after', async () => {
|
|
208
|
-
await toolCallEnded(ctx
|
|
199
|
+
await toolCallEnded(ctx).catch(fail);
|
|
209
200
|
});
|
|
210
201
|
return async () => {
|
|
211
202
|
await before.dispose();
|
|
@@ -214,12 +205,9 @@ const steeringPlugin = (
|
|
|
214
205
|
},
|
|
215
206
|
});
|
|
216
207
|
|
|
217
|
-
/** OpenCode's message id of prompt
|
|
208
|
+
/** OpenCode's message id of prompt `id`: letters, digits, `_` and `-`. */
|
|
218
209
|
const messageIdOf = (id: string) => `msg_${id.replace(/[^\w-]/g, '_')}`;
|
|
219
210
|
|
|
220
|
-
/** Whether message `id` is one steered to a turn (see `steer` in object.ts), not a prompt. */
|
|
221
|
-
const steered = (id: string) => /_steer_\d+$/.test(id);
|
|
222
|
-
|
|
223
211
|
/** A part of an assistant message: text, or a tool call (`id`, `name`, and its `state`). */
|
|
224
212
|
interface Content {
|
|
225
213
|
type: string;
|
|
@@ -294,14 +282,14 @@ async function messagesAfter(host: Host, messageId: string): Promise<Message[] |
|
|
|
294
282
|
|
|
295
283
|
/**
|
|
296
284
|
* How the turn of the messages `after` a prompt ended: at an idle marker, or
|
|
297
|
-
* where the next prompt, queued while it ran, starts
|
|
298
|
-
*
|
|
285
|
+
* where the next prompt, queued while it ran, starts. Running, if neither
|
|
286
|
+
* came yet.
|
|
299
287
|
*/
|
|
300
288
|
function turnOutcome(after: Message[]): Outcome {
|
|
301
289
|
let last: Message | undefined;
|
|
302
290
|
for (const m of after) {
|
|
303
291
|
if (m.type === 'idle') return ended(last, m.outcome);
|
|
304
|
-
if (m.type === 'user' && last !== undefined
|
|
292
|
+
if (m.type === 'user' && last !== undefined) return ended(last, 'succeeded');
|
|
305
293
|
if (m.type === 'assistant') last = m;
|
|
306
294
|
}
|
|
307
295
|
return { state: 'running' };
|
|
@@ -316,10 +304,10 @@ function ended(last: Message | undefined, how: string | undefined): Outcome {
|
|
|
316
304
|
|
|
317
305
|
/** The id of the session's latest prompt, if any. */
|
|
318
306
|
async function lastPrompt(host: Host): Promise<string | undefined> {
|
|
319
|
-
const queued = (await inbox(host)).
|
|
307
|
+
const queued = (await inbox(host)).at(-1);
|
|
320
308
|
if (queued) return queued.id;
|
|
321
309
|
for await (const page of messagePages(host, 'desc')) {
|
|
322
|
-
const user = page.find((m) => m.type === 'user'
|
|
310
|
+
const user = page.find((m) => m.type === 'user');
|
|
323
311
|
if (user) return user.id;
|
|
324
312
|
}
|
|
325
313
|
return undefined;
|
|
@@ -331,12 +319,6 @@ const active = async (host: Host) => {
|
|
|
331
319
|
return turns[SESSION] !== undefined;
|
|
332
320
|
};
|
|
333
321
|
|
|
334
|
-
/** Whether the session has message `messageId`: in its inbox, or in its transcript. */
|
|
335
|
-
const inSession = async (host: Host, messageId: string) => {
|
|
336
|
-
const given = (m: { id: string }) => m.id === messageId;
|
|
337
|
-
return (await inbox(host)).some(given) || (await sessionMessages(host)).some(given);
|
|
338
|
-
};
|
|
339
|
-
|
|
340
322
|
/**
|
|
341
323
|
* Start the session's queued prompts, if no turn runs them. OpenCode resumes
|
|
342
324
|
* the turns it claimed when it boots, but a crash between a prompt's queueing
|
|
@@ -463,19 +445,10 @@ export const withOpenCode = async <T>(
|
|
|
463
445
|
const turn = stopSignalOf(ctx);
|
|
464
446
|
const onStop = () => void fail(turn!.reason);
|
|
465
447
|
turn?.addEventListener('abort', onStop, { once: true });
|
|
466
|
-
// Messages steered to the turn: OpenCode's steering input (by its message
|
|
467
|
-
// id, once), which the session takes at its next step.
|
|
468
|
-
const steerable: Steerable = {
|
|
469
|
-
steer: async ({ id, text }) => {
|
|
470
|
-
const steer = { sessionID: SESSION as never, id: messageIdOf(id) as never, text, delivery: 'steer' as const };
|
|
471
|
-
await host.call(host.client.session.prompt(steer));
|
|
472
|
-
},
|
|
473
|
-
has: (id) => inSession(host, messageIdOf(id)),
|
|
474
|
-
};
|
|
475
448
|
const host = await openOpenCode(database(ctx.db, DATABASE), fence, {
|
|
476
449
|
plugins: [
|
|
477
450
|
...plugins,
|
|
478
|
-
|
|
451
|
+
followPlugin(ctx, fail),
|
|
479
452
|
toolsPlugin(
|
|
480
453
|
tools.map((t) => (isTool(t) ? native(t, ctx) : t)),
|
|
481
454
|
fail,
|
|
@@ -492,10 +465,7 @@ export const withOpenCode = async <T>(
|
|
|
492
465
|
await openSession(host, model, workspace);
|
|
493
466
|
await submit(host, messageId, text);
|
|
494
467
|
const now = await settled(host, messageId);
|
|
495
|
-
if (now.state === 'answered')
|
|
496
|
-
await settleSteering(ctx, steerable);
|
|
497
|
-
return now.text;
|
|
498
|
-
}
|
|
468
|
+
if (now.state === 'answered') return now.text;
|
|
499
469
|
if (now.state !== 'failed') throw new Error(`prompt ${id} is not in the session`);
|
|
500
470
|
// A prompt the cancellation's interrupt ended fails with the cancellation.
|
|
501
471
|
if (cancelled) throw cancelled;
|
|
@@ -510,12 +480,6 @@ export const withOpenCode = async <T>(
|
|
|
510
480
|
// The log reads the session through this host, while it is open.
|
|
511
481
|
const read = () => transcribe(host);
|
|
512
482
|
return await transcribing(ctx, read, () => Promise.race([interruptOn(host, failure), work({ host, prompt })]));
|
|
513
|
-
} catch (error) {
|
|
514
|
-
// A turn that ends unanswered (cancelled, refused) counts the messages
|
|
515
|
-
// steered to it that the session has, given in an earlier attempt too:
|
|
516
|
-
// its close answers only the others. An attempt that ended closes nothing.
|
|
517
|
-
if (!(error instanceof AttemptEnded)) await settleSteering(ctx, steerable).catch(() => {});
|
|
518
|
-
throw error;
|
|
519
483
|
} finally {
|
|
520
484
|
turn?.removeEventListener('abort', onStop);
|
|
521
485
|
await host.close();
|
package/src/pi/index.ts
CHANGED
|
@@ -67,8 +67,6 @@ import {
|
|
|
67
67
|
providerOf,
|
|
68
68
|
refusal,
|
|
69
69
|
REFUSED,
|
|
70
|
-
settleSteering,
|
|
71
|
-
type Steerable,
|
|
72
70
|
stopSignalOf,
|
|
73
71
|
toolCallEnded,
|
|
74
72
|
toolCallStarted,
|
|
@@ -224,17 +222,6 @@ export const withPi = async <T>(
|
|
|
224
222
|
turn?.addEventListener('abort', onStop, { once: true });
|
|
225
223
|
// The root conversation, once the Harness is open (below).
|
|
226
224
|
const conversation = () => rootOn(harness, context, model);
|
|
227
|
-
// Messages steered to the turn: given as pi-durable's steering input (by
|
|
228
|
-
// its id, once), which the run takes once the round's tools are done.
|
|
229
|
-
const steerable: Steerable = {
|
|
230
|
-
steer: async ({ id, text }) => {
|
|
231
|
-
await (await conversation()).submit({ type: 'input', content: text, requestId: id, whenBusy: 'steer' }, context);
|
|
232
|
-
},
|
|
233
|
-
has: async (id) => {
|
|
234
|
-
const root = await conversation();
|
|
235
|
-
return (await root.commit((tx) => tx.submissionByRequest(root.id, id), context)) !== undefined;
|
|
236
|
-
},
|
|
237
|
-
};
|
|
238
225
|
// A tool call's error: the attempt fails, and the call never returns.
|
|
239
226
|
const stopped = (error: unknown, task: Context): Promise<never> => {
|
|
240
227
|
// The turn was cancelled, and the abort ended the call: pi-durable records it.
|
|
@@ -253,7 +240,7 @@ export const withPi = async <T>(
|
|
|
253
240
|
try {
|
|
254
241
|
await toolCallStarted(ctx);
|
|
255
242
|
const result = await t.execute(args, api, task);
|
|
256
|
-
await toolCallEnded(ctx
|
|
243
|
+
await toolCallEnded(ctx);
|
|
257
244
|
return result;
|
|
258
245
|
} catch (error) {
|
|
259
246
|
return stopped(error, task);
|
|
@@ -285,7 +272,6 @@ export const withPi = async <T>(
|
|
|
285
272
|
const entry = await root.commit((tx) => tx.entry(AssistantEntry, answer), context);
|
|
286
273
|
const reply = entry?.model?.at(-1);
|
|
287
274
|
if (!reply) throw new restate.TerminalError(`prompt ${id}: no answer entry`);
|
|
288
|
-
await settleSteering(ctx, steerable);
|
|
289
275
|
return textOf(reply.content);
|
|
290
276
|
};
|
|
291
277
|
// `failure`, once it comes; a cancellation first aborts the conversation's work.
|
|
@@ -304,12 +290,6 @@ export const withPi = async <T>(
|
|
|
304
290
|
return await transcribing(ctx, read, () =>
|
|
305
291
|
Promise.race([abortOn(), work({ harness, context, conversation, prompt })]),
|
|
306
292
|
);
|
|
307
|
-
} catch (error) {
|
|
308
|
-
// A turn that ends unanswered (cancelled, refused) counts the messages
|
|
309
|
-
// steered to it that the conversation has, given in an earlier attempt
|
|
310
|
-
// too: its close answers only the others. An attempt that ended closes nothing.
|
|
311
|
-
if (!(error instanceof AttemptEnded)) await settleSteering(ctx, steerable).catch(() => {});
|
|
312
|
-
throw error;
|
|
313
293
|
} finally {
|
|
314
294
|
turn?.removeEventListener('abort', onStop);
|
|
315
295
|
await harness.close(withoutAbortSignal(context));
|