@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.ts +2 -4
package/src/signals.ts
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
// Signals to agents: a virtual object per agent, keyed `<agent>:<key>` (such
|
|
2
2
|
// as `assistant:alice`), whose `wait` returns once `resolve` gives the wait
|
|
3
|
-
// its value; the
|
|
4
|
-
//
|
|
3
|
+
// its value; the message the agent answers; and the revision of the agent's
|
|
4
|
+
// conversation, which its `watch` waits on.
|
|
5
5
|
//
|
|
6
6
|
// An agent's own handler cannot wait on an awakeable (it has no journal to
|
|
7
7
|
// replay one from), so it calls `wait`, which can: a journaled handler of
|
|
8
8
|
// this object, which waits without holding a process.
|
|
9
9
|
import type { SessionContext } from '@restatedev/sessions';
|
|
10
|
-
import { sdk as restate } from '@restatedev/sessions/internal';
|
|
10
|
+
import { needing, sdk as restate } from '@restatedev/sessions/internal';
|
|
11
11
|
import type { Turn } from './common.ts';
|
|
12
12
|
import type { Status } from './object.ts';
|
|
13
13
|
|
|
14
14
|
/** An agent's context, as far as its signals need it: any handler's or view's of its object. */
|
|
15
15
|
type AgentContext = { readonly key: string; readonly request: { readonly target: { readonly service: string } } };
|
|
16
16
|
|
|
17
|
-
/** The agent of `ctx`: its object's name, and key. */
|
|
18
|
-
export const agentOf = (ctx: AgentContext): Agent => ({ service: ctx.request.target.service, key: ctx.key });
|
|
19
|
-
|
|
20
17
|
/** The key of the signals of agent `service`'s key `key`: `<agent>:<key>`. */
|
|
21
18
|
const keyOf = (service: string, key: string) => `${service}:${key}`;
|
|
22
19
|
|
|
@@ -40,15 +37,21 @@ export interface Companion {
|
|
|
40
37
|
agent: Agent;
|
|
41
38
|
}
|
|
42
39
|
|
|
43
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* `definition`, pointing to its companion: what `sessionObject()` returns.
|
|
42
|
+
* `serve` serves its `signals` object beside it.
|
|
43
|
+
*/
|
|
44
44
|
export const withCompanion = <D extends { name: string }>(definition: D): D =>
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
needing(
|
|
46
|
+
Object.defineProperty(definition, COMPANION, {
|
|
47
|
+
value: (key: string): Companion => ({
|
|
48
|
+
object: signals,
|
|
49
|
+
key: keyOf(definition.name, key),
|
|
50
|
+
agent: { service: definition.name, key },
|
|
51
|
+
}),
|
|
50
52
|
}),
|
|
51
|
-
|
|
53
|
+
() => [signals],
|
|
54
|
+
);
|
|
52
55
|
|
|
53
56
|
/**
|
|
54
57
|
* The companion of `agent`'s key `key`: for anyone holding an agent (an
|
|
@@ -65,26 +68,34 @@ export function companionOf(agent: { name: string }, key: string): Companion {
|
|
|
65
68
|
export const hasCompanion = (agent: object) => COMPANION in agent;
|
|
66
69
|
|
|
67
70
|
/**
|
|
68
|
-
* Deliver `text` to `agent`'s key `key`, from any handler:
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* give it an idempotency key, so that it is delivered once.
|
|
71
|
+
* Deliver `text` to `agent`'s key `key`, from any handler: a message of its
|
|
72
|
+
* own (as `chat`), which it answers after the messages before it. It is
|
|
73
|
+
* sent, not awaited: `opts` may give it an idempotency key, so that it is
|
|
74
|
+
* delivered once.
|
|
73
75
|
*
|
|
74
76
|
* deliver(ctx, triage, 'ENG-1', 'The issue was closed', { idempotencyKey: delivery });
|
|
75
77
|
*/
|
|
76
78
|
export function deliver(
|
|
77
|
-
ctx: Pick<restate.Context, '
|
|
79
|
+
ctx: Pick<restate.Context, 'genericSend'> | Pick<SessionContext, 'send'>,
|
|
78
80
|
agent: { name: string },
|
|
79
81
|
key: string,
|
|
80
82
|
text: string,
|
|
81
|
-
|
|
83
|
+
{ idempotencyKey }: { idempotencyKey?: string } = {},
|
|
82
84
|
) {
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
+
companionOf(agent, key); // an agent of @restatedev/agents, with a `chat` handler
|
|
86
|
+
const message = { service: agent.name, handler: 'chat', key, input: text, idempotencyKey };
|
|
85
87
|
// A session object's handler, or one of the SDK's.
|
|
86
|
-
if ('send' in ctx) ctx.send(
|
|
87
|
-
else
|
|
88
|
+
if ('send' in ctx) ctx.send(message);
|
|
89
|
+
else {
|
|
90
|
+
ctx.genericSend({
|
|
91
|
+
service: agent.name,
|
|
92
|
+
method: 'chat',
|
|
93
|
+
key,
|
|
94
|
+
parameter: text,
|
|
95
|
+
inputSerde: restate.serde.json,
|
|
96
|
+
idempotencyKey,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
88
99
|
}
|
|
89
100
|
|
|
90
101
|
/** A wait for a signal, as `pending` lists it. */
|
|
@@ -116,24 +127,6 @@ export interface Resolved {
|
|
|
116
127
|
resolved: string | null;
|
|
117
128
|
}
|
|
118
129
|
|
|
119
|
-
/** A message steered to the message being answered (its turn): its id, `<turn>:steer:<n>`, and its text. */
|
|
120
|
-
export interface Steer {
|
|
121
|
-
id: string;
|
|
122
|
-
text: string;
|
|
123
|
-
/** A delivery (`deliver`): a cancelled turn answers it as a message of its own, rather than drop it. */
|
|
124
|
-
keep?: boolean;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* What `steer` did: the invocation that answers it, and whether it steers
|
|
129
|
-
* the message being answered (or, with none answered, was sent as a message
|
|
130
|
-
* of its own).
|
|
131
|
-
*/
|
|
132
|
-
export interface Steered {
|
|
133
|
-
invocationId: string;
|
|
134
|
-
steered: boolean;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
130
|
/** The revision of an agent's conversation: each change of what a client reads increases it. */
|
|
138
131
|
export interface Notifications {
|
|
139
132
|
revision: number;
|
|
@@ -151,22 +144,16 @@ interface Agent {
|
|
|
151
144
|
key: string;
|
|
152
145
|
}
|
|
153
146
|
|
|
154
|
-
/** The turns closed last, newest last: a message steered to one of them is a message of its own. */
|
|
155
|
-
const CLOSED = 16;
|
|
156
|
-
|
|
157
147
|
/**
|
|
158
|
-
* The waits, in the order they started, each
|
|
159
|
-
* kept for the next wait;
|
|
160
|
-
*
|
|
161
|
-
* `watch`es waiting for a newer one.
|
|
148
|
+
* The message the agent answers; the waits, in the order they started, each
|
|
149
|
+
* with its awakeable; the signals kept for the next wait; and the
|
|
150
|
+
* conversation's revision, with the `watch`es waiting for a newer one.
|
|
162
151
|
*/
|
|
163
152
|
interface State {
|
|
164
153
|
/** The message the agent answers, from its turn's start to its close. */
|
|
165
154
|
turn: Turn;
|
|
166
155
|
waiting: (Waiting & { awakeable: string })[];
|
|
167
156
|
kept: Record<string, unknown[]>;
|
|
168
|
-
steering: { turn: string; messages: Steer[] };
|
|
169
|
-
closed: string[];
|
|
170
157
|
revision: number;
|
|
171
158
|
watching: { revision: number; awakeable: string }[];
|
|
172
159
|
}
|
|
@@ -180,47 +167,28 @@ async function publish(ctx: restate.ObjectContext<State>): Promise<Notifications
|
|
|
180
167
|
return { revision };
|
|
181
168
|
}
|
|
182
169
|
|
|
183
|
-
/**
|
|
184
|
-
const
|
|
185
|
-
ctx.genericSend({ service, method: 'chat', key, parameter: text, inputSerde: restate.serde.json }).invocationId;
|
|
170
|
+
/** How long one `awaitTurn` waits for its turn to end, before a new one takes over. */
|
|
171
|
+
const WATCH = { hours: 1 };
|
|
186
172
|
|
|
187
173
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
174
|
+
* `turn` (an invocation id) is over: forget it, if the agent answers it, and
|
|
175
|
+
* its tools' waits, which no one waits for any more, ending their `wait`s (a
|
|
176
|
+
* turn that ends as it should has none left, but a killed one leaves them).
|
|
177
|
+
* It publishes the change, for the clients that follow the conversation: a
|
|
178
|
+
* turn's own close (its last change), or the close of one that never closed
|
|
179
|
+
* itself. A close again changes nothing, and publishes nothing.
|
|
190
180
|
*/
|
|
191
|
-
async function
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
197
|
-
const
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/** A turn's close: see `close`. */
|
|
205
|
-
interface Close {
|
|
206
|
-
agent: Agent;
|
|
207
|
-
turn: string;
|
|
208
|
-
taken: string[];
|
|
209
|
-
cancelled: boolean;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** Close `turn`, once: see `close`. */
|
|
213
|
-
async function closed(ctx: restate.ObjectContext<State>, { agent, turn, taken, cancelled }: Close) {
|
|
214
|
-
if ((await ctx.get('turn'))?.invocationId === turn) ctx.clear('turn');
|
|
215
|
-
const done = (await ctx.get('closed')) ?? [];
|
|
216
|
-
if (done.includes(turn)) return;
|
|
217
|
-
ctx.set('closed', [...done, turn].slice(-CLOSED));
|
|
218
|
-
const steering = await ctx.get('steering');
|
|
219
|
-
if (steering?.turn !== turn) return;
|
|
220
|
-
ctx.clear('steering');
|
|
221
|
-
for (const { id, text, keep } of steering.messages) {
|
|
222
|
-
if (!taken.includes(id) && (keep || !cancelled)) await send(ctx, agent, text);
|
|
223
|
-
}
|
|
181
|
+
async function closed(ctx: restate.ObjectContext<State>, turn: string) {
|
|
182
|
+
const answering = (await ctx.get('turn'))?.invocationId === turn;
|
|
183
|
+
if (answering) ctx.clear('turn');
|
|
184
|
+
const waiting = (await ctx.get('waiting')) ?? [];
|
|
185
|
+
// A wait's id starts with the invocation id of the turn whose tool waits.
|
|
186
|
+
const ofTurn = (id: string) => id.startsWith(`${turn}:`);
|
|
187
|
+
const over = waiting.filter((w) => ofTurn(w.id));
|
|
188
|
+
const left = waiting.filter((w) => !ofTurn(w.id));
|
|
189
|
+
for (const w of over) ctx.rejectAwakeable(w.awakeable, 'its turn is over');
|
|
190
|
+
if (over.length > 0) ctx.set('waiting', left);
|
|
191
|
+
if (answering || over.length > 0) await publish(ctx);
|
|
224
192
|
}
|
|
225
193
|
|
|
226
194
|
/** A handler that the agents' own handlers call, which clients cannot (through Restate's ingress). */
|
|
@@ -288,28 +256,54 @@ export const signals = restate.object({
|
|
|
288
256
|
await publish(ctx);
|
|
289
257
|
}),
|
|
290
258
|
|
|
291
|
-
/**
|
|
292
|
-
* @internal Steer the message `agent` answers, if any, with `text`:
|
|
293
|
-
* queued for it to take at its next step, or, if none is answered, sent
|
|
294
|
-
* as a message of its own.
|
|
295
|
-
*/
|
|
296
|
-
steer: internal(async (ctx: restate.ObjectContext<State>, { agent, text }: { agent: Agent; text: string }) =>
|
|
297
|
-
steered(ctx, agent, text),
|
|
298
|
-
),
|
|
299
|
-
|
|
300
259
|
/**
|
|
301
260
|
* @internal The agent's `chat` started answering `turn`: the same
|
|
302
261
|
* invocation again (a retry) keeps when it started. A turn before it that
|
|
303
|
-
* never closed (its invocation was killed) closes now,
|
|
304
|
-
*
|
|
262
|
+
* never closed itself (its invocation was killed) closes now, if its
|
|
263
|
+
* `awaitTurn` did not close it yet. `awaitTurn` watches the new turn, so
|
|
264
|
+
* that it closes even if no turn comes after it.
|
|
305
265
|
*/
|
|
306
|
-
start: internal(async (ctx: restate.ObjectContext<State>,
|
|
266
|
+
start: internal(async (ctx: restate.ObjectContext<State>, turn: Turn) => {
|
|
307
267
|
const before = (await ctx.get('turn'))?.invocationId;
|
|
308
268
|
if (before === turn.invocationId) return;
|
|
309
|
-
if (before !== undefined) await closed(ctx,
|
|
269
|
+
if (before !== undefined) await closed(ctx, before);
|
|
310
270
|
ctx.set('turn', turn);
|
|
271
|
+
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn.invocationId);
|
|
311
272
|
}),
|
|
312
273
|
|
|
274
|
+
/**
|
|
275
|
+
* @internal Wait for `turn` to end, without holding a process, and close
|
|
276
|
+
* it if it failed: its waits end, and `status` no longer says the agent
|
|
277
|
+
* answers it. A turn that never closed itself failed: it was killed, ran
|
|
278
|
+
* out of retries, or was lost. A turn that ends as it should, or is
|
|
279
|
+
* cancelled, closed itself already: its `close` is a call it waits for
|
|
280
|
+
* before it ends, so this `close` comes second, and changes nothing.
|
|
281
|
+
*
|
|
282
|
+
* It waits {@link WATCH} at most: a turn still answered then gets a new
|
|
283
|
+
* watch, which runs on the deployment of the day, rather than this one
|
|
284
|
+
* waiting for weeks on the one it started on (a turn that waits for an
|
|
285
|
+
* approval, say). A cancelled watch (an operator's) rejects the race
|
|
286
|
+
* itself, before either side's `map`: it closes nothing.
|
|
287
|
+
*/
|
|
288
|
+
awaitTurn: restate.handlers.object.shared(
|
|
289
|
+
// It leaves nothing behind once done: a turn has one at a time.
|
|
290
|
+
{ ingressPrivate: true, journalRetention: 0, idempotencyRetention: 0 },
|
|
291
|
+
async (ctx: restate.ObjectSharedContext<State>, turn: string) => {
|
|
292
|
+
const ended = await restate.RestatePromise.race([
|
|
293
|
+
// `map` gets the turn's failure too: a killed turn's (a TerminalError).
|
|
294
|
+
ctx
|
|
295
|
+
.invocation(restate.InvocationIdParser.fromString(turn))
|
|
296
|
+
.attach()
|
|
297
|
+
.map((_answer, failure) => ({ failed: failure !== undefined })),
|
|
298
|
+
ctx.sleep(WATCH).map(() => null),
|
|
299
|
+
]);
|
|
300
|
+
if (ended?.failed) ctx.objectSendClient(signals, ctx.key).close(turn);
|
|
301
|
+
else if (ended === null && (await ctx.get('turn'))?.invocationId === turn) {
|
|
302
|
+
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn);
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
),
|
|
306
|
+
|
|
313
307
|
/** What the agent does: the message it answers, if any, and the waits of its tools. */
|
|
314
308
|
status: restate.handlers.object.shared(async (ctx: restate.ObjectSharedContext<State>): Promise<Status> => ({
|
|
315
309
|
answering: (await ctx.get('turn')) ?? null,
|
|
@@ -337,40 +331,16 @@ export const signals = restate.object({
|
|
|
337
331
|
},
|
|
338
332
|
),
|
|
339
333
|
|
|
340
|
-
/**
|
|
341
|
-
* @internal Deliver `text` to `agent` (a webhook's delivery, say): steered
|
|
342
|
-
* to the message it answers, if any, which takes it at its next step;
|
|
343
|
-
* else, or once that turn is over, as a message of its own. Unlike a
|
|
344
|
-
* steer, a cancelled turn does not drop it: it is answered after.
|
|
345
|
-
*/
|
|
346
|
-
deliver: internal(async (ctx: restate.ObjectContext<State>, { agent, text }: { agent: Agent; text: string }) =>
|
|
347
|
-
steered(ctx, agent, text, true),
|
|
348
|
-
),
|
|
349
|
-
|
|
350
|
-
/** @internal The messages steered to `turn`, in order. */
|
|
351
|
-
steering: restate.handlers.object.shared(
|
|
352
|
-
{ ingressPrivate: true },
|
|
353
|
-
async (ctx: restate.ObjectSharedContext<State>, turn: string) => {
|
|
354
|
-
const steering = await ctx.get('steering');
|
|
355
|
-
return steering?.turn === turn ? steering.messages : [];
|
|
356
|
-
},
|
|
357
|
-
),
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* @internal `turn` is over: each message steered to it that it did not
|
|
361
|
-
* take (not in `taken`) is sent as a message of its own, unless the turn
|
|
362
|
-
* was cancelled (but for a delivery); later ones are, too.
|
|
363
|
-
*/
|
|
334
|
+
/** @internal The message `turn` (an invocation id) is answered, or its turn ended otherwise: see {@link closed}. */
|
|
364
335
|
close: internal(closed),
|
|
365
336
|
|
|
366
337
|
/**
|
|
367
338
|
* @internal The agent forgot its conversation (`clear`): forget its
|
|
368
|
-
* waits
|
|
339
|
+
* waits and kept signals, and publish the change.
|
|
369
340
|
*/
|
|
370
341
|
reset: restate.handlers.object.exclusive({ ingressPrivate: true }, async (ctx: restate.ObjectContext<State>) => {
|
|
371
342
|
ctx.clear('waiting');
|
|
372
343
|
ctx.clear('kept');
|
|
373
|
-
ctx.clear('steering');
|
|
374
344
|
await publish(ctx);
|
|
375
345
|
}),
|
|
376
346
|
|
|
@@ -387,16 +357,6 @@ export const signals = restate.object({
|
|
|
387
357
|
/** @internal The conversation changed: increase its revision, and answer the `watch`es. */
|
|
388
358
|
publish: restate.handlers.object.exclusive({ ingressPrivate: true }, publish),
|
|
389
359
|
|
|
390
|
-
/**
|
|
391
|
-
* @internal The turn `turn` made progress (a tool call ended): publish
|
|
392
|
-
* it, and answer the messages steered to the turn.
|
|
393
|
-
*/
|
|
394
|
-
progress: internal(async (ctx: restate.ObjectContext<State>, turn: string) => {
|
|
395
|
-
await publish(ctx);
|
|
396
|
-
const steering = await ctx.get('steering');
|
|
397
|
-
return steering?.turn === turn ? steering.messages : [];
|
|
398
|
-
}),
|
|
399
|
-
|
|
400
360
|
/** The conversation's revision. */
|
|
401
361
|
notifications: restate.handlers.object.shared(
|
|
402
362
|
async (ctx: restate.ObjectSharedContext<State>): Promise<Notifications> => ({
|
package/src/webhook.ts
CHANGED
|
@@ -103,10 +103,8 @@ const parsed = (body: Uint8Array): WebhookEvent => {
|
|
|
103
103
|
/**
|
|
104
104
|
* A webhook into agents: a service `name`, whose `webhook` handler checks a
|
|
105
105
|
* delivery's signature, and delivers it to each agent of `route`, keyed by
|
|
106
|
-
* `key`, as a {@link webhookMessage}
|
|
107
|
-
*
|
|
108
|
-
* `steer`), or else as a message of its own (as `chat`). A turn that ends,
|
|
109
|
-
* or is stopped, before it takes it answers it after. It answers once the
|
|
106
|
+
* `key`, as a {@link webhookMessage}: a message of its own (as `chat`),
|
|
107
|
+
* which the agent answers after the messages before it. It answers once the
|
|
110
108
|
* deliveries are sent, not answered: a system that waits seconds for an
|
|
111
109
|
* answer (Linear, GitHub) gets one at once. What it is (its routes, auth and
|
|
112
110
|
* idempotency) is in the handler's metadata, which the admin API shows.
|