@restatedev/agents 0.0.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/LICENSE +21 -0
- package/README.md +672 -2
- package/dist/calls.d.ts +29 -0
- package/dist/calls.d.ts.map +1 -0
- package/dist/calls.js +91 -0
- package/dist/calls.js.map +1 -0
- package/dist/claude/conversation.d.ts +33 -0
- package/dist/claude/conversation.d.ts.map +1 -0
- package/dist/claude/conversation.js +95 -0
- package/dist/claude/conversation.js.map +1 -0
- package/dist/claude/index.d.ts +77 -0
- package/dist/claude/index.d.ts.map +1 -0
- package/dist/claude/index.js +269 -0
- package/dist/claude/index.js.map +1 -0
- package/dist/client.d.ts +81 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/common.d.ts +261 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +438 -0
- package/dist/common.js.map +1 -0
- package/dist/deepseek/index.d.ts +68 -0
- package/dist/deepseek/index.d.ts.map +1 -0
- package/dist/deepseek/index.js +363 -0
- package/dist/deepseek/index.js.map +1 -0
- package/dist/deepseek/persistence.d.ts +61 -0
- package/dist/deepseek/persistence.d.ts.map +1 -0
- package/dist/deepseek/persistence.js +224 -0
- package/dist/deepseek/persistence.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/langgraph/checkpointer.d.ts +29 -0
- package/dist/langgraph/checkpointer.d.ts.map +1 -0
- package/dist/langgraph/checkpointer.js +275 -0
- package/dist/langgraph/checkpointer.js.map +1 -0
- package/dist/langgraph/index.d.ts +66 -0
- package/dist/langgraph/index.d.ts.map +1 -0
- package/dist/langgraph/index.js +265 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/log.d.ts +29 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +145 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +169 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +221 -0
- package/dist/object.js.map +1 -0
- package/dist/opencode/db.d.ts +40 -0
- package/dist/opencode/db.d.ts.map +1 -0
- package/dist/opencode/db.js +161 -0
- package/dist/opencode/db.js.map +1 -0
- package/dist/opencode/index.d.ts +100 -0
- package/dist/opencode/index.d.ts.map +1 -0
- package/dist/opencode/index.js +436 -0
- package/dist/opencode/index.js.map +1 -0
- package/dist/output.d.ts +17 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +81 -0
- package/dist/output.js.map +1 -0
- package/dist/pi/index.d.ts +89 -0
- package/dist/pi/index.d.ts.map +1 -0
- package/dist/pi/index.js +293 -0
- package/dist/pi/index.js.map +1 -0
- package/dist/pi/storage.d.ts +5 -0
- package/dist/pi/storage.d.ts.map +1 -0
- package/dist/pi/storage.js +41 -0
- package/dist/pi/storage.js.map +1 -0
- package/dist/signals.d.ts +154 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +256 -0
- package/dist/signals.js.map +1 -0
- package/dist/tools.d.ts +158 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +211 -0
- package/dist/tools.js.map +1 -0
- package/dist/webhook.d.ts +71 -0
- package/dist/webhook.d.ts.map +1 -0
- package/dist/webhook.js +102 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +218 -5
- package/src/calls.ts +138 -0
- package/src/claude/README.md +84 -0
- package/src/claude/conversation.ts +114 -0
- package/src/claude/index.ts +364 -0
- package/src/client.ts +176 -0
- package/src/common.ts +550 -0
- package/src/deepseek/README.md +110 -0
- package/src/deepseek/index.ts +464 -0
- package/src/deepseek/persistence.ts +293 -0
- package/src/index.ts +24 -0
- package/src/langgraph/README.md +87 -0
- package/src/langgraph/checkpointer.ts +364 -0
- package/src/langgraph/index.ts +349 -0
- package/src/log.ts +193 -0
- package/src/object.ts +319 -0
- package/src/opencode/README.md +125 -0
- package/src/opencode/db.ts +223 -0
- package/src/opencode/index.ts +559 -0
- package/src/output.ts +95 -0
- package/src/pi/README.md +105 -0
- package/src/pi/index.ts +382 -0
- package/src/pi/storage.ts +65 -0
- package/src/signals.ts +386 -0
- package/src/tools.ts +343 -0
- package/src/webhook.ts +161 -0
package/src/signals.ts
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// Signals to agents: a virtual object per agent, keyed `<agent>:<key>` (such
|
|
2
|
+
// as `assistant:alice`), whose `wait` returns once `resolve` gives the wait
|
|
3
|
+
// its value; the message the agent answers; and the revision of the agent's
|
|
4
|
+
// conversation, which its `watch` waits on.
|
|
5
|
+
//
|
|
6
|
+
// An agent's own handler cannot wait on an awakeable (it has no journal to
|
|
7
|
+
// replay one from), so it calls `wait`, which can: a journaled handler of
|
|
8
|
+
// this object, which waits without holding a process.
|
|
9
|
+
import type { SessionContext } from '@restatedev/sessions';
|
|
10
|
+
import { needing, sdk as restate } from '@restatedev/sessions/internal';
|
|
11
|
+
import type { Turn } from './common.ts';
|
|
12
|
+
import type { Status } from './object.ts';
|
|
13
|
+
|
|
14
|
+
/** An agent's context, as far as its signals need it: any handler's or view's of its object. */
|
|
15
|
+
type AgentContext = { readonly key: string; readonly request: { readonly target: { readonly service: string } } };
|
|
16
|
+
|
|
17
|
+
/** The key of the signals of agent `service`'s key `key`: `<agent>:<key>`. */
|
|
18
|
+
const keyOf = (service: string, key: string) => `${service}:${key}`;
|
|
19
|
+
|
|
20
|
+
/** The key of the signals of the agent of `ctx`. */
|
|
21
|
+
export const signalsOf = (ctx: AgentContext) => keyOf(ctx.request.target.service, ctx.key);
|
|
22
|
+
|
|
23
|
+
/** Where an object of @restatedev/agents (`sessionObject()`, every `agent()`) points to its companion. */
|
|
24
|
+
const COMPANION = Symbol('restate-agents.companion');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An agent's companion, for one of its keys: its `signals` object, which
|
|
28
|
+
* knows what the agent is doing (the message it answers, what its tools wait
|
|
29
|
+
* for), and how to reach it.
|
|
30
|
+
*/
|
|
31
|
+
export interface Companion {
|
|
32
|
+
/** The `signals` object, as a client takes it. */
|
|
33
|
+
object: typeof signals;
|
|
34
|
+
/** The key of the agent's key there: `<agent>:<key>`. */
|
|
35
|
+
key: string;
|
|
36
|
+
/** The agent, and its key. */
|
|
37
|
+
agent: Agent;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* `definition`, pointing to its companion: what `sessionObject()` returns.
|
|
42
|
+
* `serve` serves its `signals` object beside it.
|
|
43
|
+
*/
|
|
44
|
+
export const withCompanion = <D extends { name: string }>(definition: D): D =>
|
|
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
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
() => [signals],
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The companion of `agent`'s key `key`: for anyone holding an agent (an
|
|
58
|
+
* `sessionObject()` or `agent()` of @restatedev/agents) to reach what it is doing.
|
|
59
|
+
*/
|
|
60
|
+
export function companionOf(agent: { name: string }, key: string): Companion {
|
|
61
|
+
const companion = (agent as { [COMPANION]?: (key: string) => Companion })[COMPANION];
|
|
62
|
+
if (!companion)
|
|
63
|
+
throw new Error(`${agent.name} is not an agent of @restatedev/agents (a sessionObject() or an agent())`);
|
|
64
|
+
return companion(key);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Whether `agent` is an object of @restatedev/agents, with a companion. */
|
|
68
|
+
export const hasCompanion = (agent: object) => COMPANION in agent;
|
|
69
|
+
|
|
70
|
+
/**
|
|
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.
|
|
75
|
+
*
|
|
76
|
+
* deliver(ctx, triage, 'ENG-1', 'The issue was closed', { idempotencyKey: delivery });
|
|
77
|
+
*/
|
|
78
|
+
export function deliver(
|
|
79
|
+
ctx: Pick<restate.Context, 'genericSend'> | Pick<SessionContext, 'send'>,
|
|
80
|
+
agent: { name: string },
|
|
81
|
+
key: string,
|
|
82
|
+
text: string,
|
|
83
|
+
{ idempotencyKey }: { idempotencyKey?: string } = {},
|
|
84
|
+
) {
|
|
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 };
|
|
87
|
+
// A session object's handler, or one of the SDK's.
|
|
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
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A wait for a signal, as `pending` lists it. */
|
|
102
|
+
export interface Waiting {
|
|
103
|
+
/** The wait's id, which `signal` takes to resolve this wait and no other. */
|
|
104
|
+
id: string;
|
|
105
|
+
/** The signal it waits for, such as `approval`. */
|
|
106
|
+
name: string;
|
|
107
|
+
/** What it waits for, as the model said it (such as the refund to approve), if the tool takes an input. */
|
|
108
|
+
input?: unknown;
|
|
109
|
+
/** When it started, as an ISO date. */
|
|
110
|
+
since: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Which wait a signal resolves, and with what. */
|
|
114
|
+
export interface Signal {
|
|
115
|
+
/** The wait of this id (from `pending`). */
|
|
116
|
+
id?: string;
|
|
117
|
+
/** Else the oldest wait for the signal of this name. */
|
|
118
|
+
name?: string;
|
|
119
|
+
/** Its value: what the wait answers the model with. */
|
|
120
|
+
value?: unknown;
|
|
121
|
+
/** With a `name` and no one waiting for it, keep the signal for the next wait for it (by default it is dropped). */
|
|
122
|
+
keep?: boolean;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What `signal` did: the id of the wait it resolved, or null if none waited (and it was dropped, or kept). */
|
|
126
|
+
export interface Resolved {
|
|
127
|
+
resolved: string | null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The revision of an agent's conversation: each change of what a client reads increases it. */
|
|
131
|
+
export interface Notifications {
|
|
132
|
+
revision: number;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** What `watch` waits for: a revision newer than `revision`, for at most `timeoutSeconds` (default 50, at most 60). */
|
|
136
|
+
export interface Watch {
|
|
137
|
+
revision: number;
|
|
138
|
+
timeoutSeconds?: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** An agent: its object's name, and key. */
|
|
142
|
+
interface Agent {
|
|
143
|
+
service: string;
|
|
144
|
+
key: string;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
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.
|
|
151
|
+
*/
|
|
152
|
+
interface State {
|
|
153
|
+
/** The message the agent answers, from its turn's start to its close. */
|
|
154
|
+
turn: Turn;
|
|
155
|
+
waiting: (Waiting & { awakeable: string })[];
|
|
156
|
+
kept: Record<string, unknown[]>;
|
|
157
|
+
revision: number;
|
|
158
|
+
watching: { revision: number; awakeable: string }[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Increase the revision, and answer each `watch` waiting for it: the new revision. */
|
|
162
|
+
async function publish(ctx: restate.ObjectContext<State>): Promise<Notifications> {
|
|
163
|
+
const revision = ((await ctx.get('revision')) ?? 0) + 1;
|
|
164
|
+
ctx.set('revision', revision);
|
|
165
|
+
for (const { awakeable } of (await ctx.get('watching')) ?? []) ctx.resolveAwakeable(awakeable, { revision });
|
|
166
|
+
ctx.clear('watching');
|
|
167
|
+
return { revision };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** How long one `awaitTurn` waits for its turn to end, before a new one takes over. */
|
|
171
|
+
const WATCH = { hours: 1 };
|
|
172
|
+
|
|
173
|
+
/**
|
|
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.
|
|
180
|
+
*/
|
|
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);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** A handler that the agents' own handlers call, which clients cannot (through Restate's ingress). */
|
|
195
|
+
const internal = <I, O>(fn: (ctx: restate.ObjectContext<State>, input: I) => Promise<O>) =>
|
|
196
|
+
restate.handlers.object.exclusive({ ingressPrivate: true }, fn);
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The signals of every agent, keyed `<agent>:<key>` ({@link signalsOf}).
|
|
200
|
+
* `serve` serves it beside the agents; the handlers marked `@internal` are
|
|
201
|
+
* called by the agents' own handlers, and Restate's ingress refuses them.
|
|
202
|
+
*/
|
|
203
|
+
export const signals = restate.object({
|
|
204
|
+
name: 'signals',
|
|
205
|
+
handlers: {
|
|
206
|
+
/** Wait for the signal `name`, as wait `id` (for `input`), and return its value. */
|
|
207
|
+
wait: restate.handlers.object.shared(
|
|
208
|
+
async (ctx: restate.ObjectSharedContext<State>, wait: { id: string; name: string; input?: unknown }) => {
|
|
209
|
+
const { id, promise } = ctx.awakeable<unknown>();
|
|
210
|
+
const since = new Date(await ctx.date.now()).toISOString();
|
|
211
|
+
ctx.objectSendClient(signals, ctx.key).listen({ ...wait, since, awakeable: id });
|
|
212
|
+
try {
|
|
213
|
+
return await promise;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
// The agent's invocation was cancelled: no one waits any more.
|
|
216
|
+
if (error instanceof restate.CancelledError) ctx.objectSendClient(signals, ctx.key).forget(wait.id);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
),
|
|
221
|
+
|
|
222
|
+
/** Resolve the wait `id`, or else the oldest wait for `name`, with `value`. */
|
|
223
|
+
resolve: async (ctx: restate.ObjectContext<State>, { id, name, value = null, keep = false }: Signal) => {
|
|
224
|
+
if (id === undefined && name === undefined) throw new restate.TerminalError('a signal needs an id or a name');
|
|
225
|
+
const waiting = (await ctx.get('waiting')) ?? [];
|
|
226
|
+
const at = waiting.findIndex((w) => (id === undefined ? w.name === name : w.id === id));
|
|
227
|
+
if (at < 0) {
|
|
228
|
+
if (keep && name !== undefined && id === undefined) {
|
|
229
|
+
const kept = (await ctx.get('kept')) ?? {};
|
|
230
|
+
ctx.set('kept', { ...kept, [name]: [...(kept[name] ?? []), value] });
|
|
231
|
+
}
|
|
232
|
+
return { resolved: null } satisfies Resolved;
|
|
233
|
+
}
|
|
234
|
+
const [wait] = waiting.splice(at, 1);
|
|
235
|
+
ctx.resolveAwakeable(wait!.awakeable, value);
|
|
236
|
+
ctx.set('waiting', waiting);
|
|
237
|
+
await publish(ctx);
|
|
238
|
+
return { resolved: wait!.id } satisfies Resolved;
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
/** The waits, oldest first. */
|
|
242
|
+
pending: restate.handlers.object.shared(async (ctx: restate.ObjectSharedContext<State>): Promise<Waiting[]> =>
|
|
243
|
+
((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since })),
|
|
244
|
+
),
|
|
245
|
+
|
|
246
|
+
/** @internal A wait: resolved now with a signal kept for it, or else when one comes. */
|
|
247
|
+
listen: internal(async (ctx: restate.ObjectContext<State>, wait: Waiting & { awakeable: string }) => {
|
|
248
|
+
const kept = (await ctx.get('kept')) ?? {};
|
|
249
|
+
const [value, ...rest] = kept[wait.name] ?? [];
|
|
250
|
+
if (kept[wait.name]?.length) {
|
|
251
|
+
ctx.resolveAwakeable(wait.awakeable, value);
|
|
252
|
+
ctx.set('kept', { ...kept, [wait.name]: rest });
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
ctx.set('waiting', [...((await ctx.get('waiting')) ?? []), wait]);
|
|
256
|
+
await publish(ctx);
|
|
257
|
+
}),
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* @internal The agent's `chat` started answering `turn`: the same
|
|
261
|
+
* invocation again (a retry) keeps when it started. A turn before it that
|
|
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.
|
|
265
|
+
*/
|
|
266
|
+
start: internal(async (ctx: restate.ObjectContext<State>, turn: Turn) => {
|
|
267
|
+
const before = (await ctx.get('turn'))?.invocationId;
|
|
268
|
+
if (before === turn.invocationId) return;
|
|
269
|
+
if (before !== undefined) await closed(ctx, before);
|
|
270
|
+
ctx.set('turn', turn);
|
|
271
|
+
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn.invocationId);
|
|
272
|
+
}),
|
|
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
|
+
|
|
307
|
+
/** What the agent does: the message it answers, if any, and the waits of its tools. */
|
|
308
|
+
status: restate.handlers.object.shared(async (ctx: restate.ObjectSharedContext<State>): Promise<Status> => ({
|
|
309
|
+
answering: (await ctx.get('turn')) ?? null,
|
|
310
|
+
waiting: ((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since })),
|
|
311
|
+
})),
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* The revision, once there is one newer than `revision`, or at the
|
|
315
|
+
* timeout (50 s by default, at most 60 s), whichever comes first. It
|
|
316
|
+
* waits in Restate, holding no process; a caller that retries it with the
|
|
317
|
+
* same idempotency key joins the same wait.
|
|
318
|
+
*/
|
|
319
|
+
watch: restate.handlers.object.shared(
|
|
320
|
+
async (
|
|
321
|
+
ctx: restate.ObjectSharedContext<State>,
|
|
322
|
+
{ revision, timeoutSeconds = 50 }: Watch,
|
|
323
|
+
): Promise<Notifications> => {
|
|
324
|
+
const { id, promise } = ctx.awakeable<Notifications>();
|
|
325
|
+
ctx.objectSendClient(signals, ctx.key).subscribe({ revision, awakeable: id });
|
|
326
|
+
const timeout = ctx.sleep({ seconds: Math.min(Math.max(timeoutSeconds, 0), 60) }).map(() => null);
|
|
327
|
+
const changed = await restate.RestatePromise.race([promise, timeout]);
|
|
328
|
+
if (changed) return changed;
|
|
329
|
+
ctx.objectSendClient(signals, ctx.key).unsubscribe(id);
|
|
330
|
+
return { revision: (await ctx.get('revision')) ?? 0 };
|
|
331
|
+
},
|
|
332
|
+
),
|
|
333
|
+
|
|
334
|
+
/** @internal The message `turn` (an invocation id) is answered, or its turn ended otherwise: see {@link closed}. */
|
|
335
|
+
close: internal(closed),
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* @internal The agent forgot its conversation (`clear`): forget its
|
|
339
|
+
* waits and kept signals, and publish the change.
|
|
340
|
+
*/
|
|
341
|
+
reset: restate.handlers.object.exclusive({ ingressPrivate: true }, async (ctx: restate.ObjectContext<State>) => {
|
|
342
|
+
ctx.clear('waiting');
|
|
343
|
+
ctx.clear('kept');
|
|
344
|
+
await publish(ctx);
|
|
345
|
+
}),
|
|
346
|
+
|
|
347
|
+
/** @internal Forget wait `id`, whose agent no longer waits. */
|
|
348
|
+
forget: internal(async (ctx: restate.ObjectContext<State>, id: string) => {
|
|
349
|
+
const waiting = (await ctx.get('waiting')) ?? [];
|
|
350
|
+
ctx.set(
|
|
351
|
+
'waiting',
|
|
352
|
+
waiting.filter((w) => w.id !== id),
|
|
353
|
+
);
|
|
354
|
+
await publish(ctx);
|
|
355
|
+
}),
|
|
356
|
+
|
|
357
|
+
/** @internal The conversation changed: increase its revision, and answer the `watch`es. */
|
|
358
|
+
publish: restate.handlers.object.exclusive({ ingressPrivate: true }, publish),
|
|
359
|
+
|
|
360
|
+
/** The conversation's revision. */
|
|
361
|
+
notifications: restate.handlers.object.shared(
|
|
362
|
+
async (ctx: restate.ObjectSharedContext<State>): Promise<Notifications> => ({
|
|
363
|
+
revision: (await ctx.get('revision')) ?? 0,
|
|
364
|
+
}),
|
|
365
|
+
),
|
|
366
|
+
|
|
367
|
+
/** @internal Answer `awakeable` once there is a revision newer than `revision`: at once, if there is. */
|
|
368
|
+
subscribe: internal(async (ctx: restate.ObjectContext<State>, watch: { revision: number; awakeable: string }) => {
|
|
369
|
+
const revision = (await ctx.get('revision')) ?? 0;
|
|
370
|
+
if (revision > watch.revision) {
|
|
371
|
+
ctx.resolveAwakeable(watch.awakeable, { revision });
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
ctx.set('watching', [...((await ctx.get('watching')) ?? []), watch]);
|
|
375
|
+
}),
|
|
376
|
+
|
|
377
|
+
/** @internal Forget the `watch` of `awakeable`, which no longer waits. */
|
|
378
|
+
unsubscribe: internal(async (ctx: restate.ObjectContext<State>, awakeable: string) => {
|
|
379
|
+
const watching = (await ctx.get('watching')) ?? [];
|
|
380
|
+
ctx.set(
|
|
381
|
+
'watching',
|
|
382
|
+
watching.filter((w) => w.awakeable !== awakeable),
|
|
383
|
+
);
|
|
384
|
+
}),
|
|
385
|
+
},
|
|
386
|
+
});
|