@restatedev/agents 0.2.0 → 0.3.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/dist/calls.d.ts +2 -5
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +3 -9
- package/dist/calls.js.map +1 -1
- package/dist/claude/index.js +15 -14
- package/dist/claude/index.js.map +1 -1
- package/dist/common.d.ts +21 -12
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +60 -51
- package/dist/common.js.map +1 -1
- package/dist/deepseek/index.js +1 -1
- package/dist/deepseek/index.js.map +1 -1
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/langgraph/index.js +11 -13
- package/dist/langgraph/index.js.map +1 -1
- package/dist/log.d.ts +2 -2
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +13 -12
- package/dist/log.js.map +1 -1
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +4 -7
- package/dist/object.js.map +1 -1
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +6 -6
- package/dist/pi/index.js.map +1 -1
- package/dist/signals.d.ts +1 -3
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +10 -12
- package/dist/signals.js.map +1 -1
- package/package.json +3 -3
- package/src/calls.ts +6 -12
- package/src/claude/index.ts +15 -15
- package/src/common.ts +73 -51
- package/src/deepseek/index.ts +1 -1
- package/src/langgraph/index.ts +15 -16
- package/src/log.ts +14 -13
- package/src/object.ts +14 -13
- package/src/pi/index.ts +6 -7
- package/src/signals.ts +10 -12
package/src/object.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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 { cleared,
|
|
4
|
+
import { cleared, isAgent, type Messages, type MessagesRequest, refusalOf, type Turn } from './common.ts';
|
|
5
5
|
import * as log from './log.ts';
|
|
6
6
|
import {
|
|
7
7
|
type Notifications,
|
|
@@ -264,7 +264,6 @@ export const sessionObject = <
|
|
|
264
264
|
},
|
|
265
265
|
): restate.VirtualObjectDefinition<P, With<H, V>> => {
|
|
266
266
|
const { name, description, schema, background, handlers = {}, views = {}, metadata, options } = definition;
|
|
267
|
-
if (background) inBackground(name);
|
|
268
267
|
const own = [
|
|
269
268
|
...Object.keys(BUILT_IN),
|
|
270
269
|
...Object.keys(followed(async () => ({}) as Messages)),
|
|
@@ -285,21 +284,23 @@ export const sessionObject = <
|
|
|
285
284
|
]),
|
|
286
285
|
);
|
|
287
286
|
const messages = (views as Record<string, restate.Entry<restate.ViewContext>>).messages;
|
|
288
|
-
onClear(name, messages);
|
|
289
287
|
const page = (typeof messages === 'function' ? messages : messages?.handler) as
|
|
290
288
|
| ((ctx: restate.ViewContext, request: MessagesRequest) => Promise<Messages>)
|
|
291
289
|
| undefined;
|
|
292
290
|
const agent = page === undefined ? {} : followed(page);
|
|
293
291
|
return withCompanion(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
292
|
+
isAgent(
|
|
293
|
+
restate.sessionObject({
|
|
294
|
+
name,
|
|
295
|
+
description,
|
|
296
|
+
schema,
|
|
297
|
+
handlers: { ...(messages === undefined ? {} : INTERNAL), ...yours },
|
|
298
|
+
views: { ...BUILT_IN, ...agent, ...views },
|
|
299
|
+
metadata,
|
|
300
|
+
options: { ...OPTIONS, ...options },
|
|
301
|
+
}),
|
|
302
|
+
{ background, messages },
|
|
303
|
+
),
|
|
303
304
|
);
|
|
304
305
|
};
|
|
305
306
|
|
|
@@ -312,7 +313,7 @@ function answering(entry: restate.Entry<restate.SessionContext>): restate.Entry<
|
|
|
312
313
|
try {
|
|
313
314
|
return await handler(ctx, input);
|
|
314
315
|
} catch (error) {
|
|
315
|
-
throw
|
|
316
|
+
throw refusalOf(ctx, error) ?? error;
|
|
316
317
|
}
|
|
317
318
|
},
|
|
318
319
|
};
|
package/src/pi/index.ts
CHANGED
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
// and a coding tool cut off by a crash is not run again (the model is told
|
|
33
33
|
// it was interrupted).
|
|
34
34
|
import * as restate from '@restatedev/sessions';
|
|
35
|
-
import
|
|
35
|
+
import { ScopeKey } from '@restatedev/sessions/internal';
|
|
36
36
|
import type { Context } from '@earendil-works/chord';
|
|
37
37
|
import { awaitWithContext, BACKGROUND_CONTEXT, withCancel, withoutAbortSignal } from '@earendil-works/chord/context';
|
|
38
38
|
import { mkdir } from 'node:fs/promises';
|
|
@@ -286,7 +286,7 @@ export const withPi = async <T>(
|
|
|
286
286
|
|
|
287
287
|
try {
|
|
288
288
|
// The log reads the root conversation through this Harness, while it is open.
|
|
289
|
-
const read = async () => transcribe(ctx
|
|
289
|
+
const read = async () => transcribe(ctx, await harness.root(context), context);
|
|
290
290
|
return await transcribing(ctx, read, () =>
|
|
291
291
|
Promise.race([abortOn(), work({ harness, context, conversation, prompt })]),
|
|
292
292
|
);
|
|
@@ -336,12 +336,11 @@ export const messages = () => messagesHandler();
|
|
|
336
336
|
* never change, and a new one takes a greater id than any before it (they
|
|
337
337
|
* are committed one at a time), so each read adds the entries after it.
|
|
338
338
|
*/
|
|
339
|
-
const
|
|
339
|
+
const TRANSCRIPT = new ScopeKey<{ last?: EntryId; transcript: Transcript }>('the transcript');
|
|
340
340
|
|
|
341
|
-
/** The root conversation in `
|
|
342
|
-
const transcribe = async (
|
|
343
|
-
const read =
|
|
344
|
-
transcripts.set(db, read);
|
|
341
|
+
/** The root conversation in the database of `ctx`, `root` of a Harness on it, from pi-durable's entries, as `messages` gives it. */
|
|
342
|
+
const transcribe = async (ctx: restate.SessionContext, root: Conversation, context: Context) => {
|
|
343
|
+
const read = TRANSCRIPT.of(ctx, () => ({ transcript: new Transcript() }));
|
|
345
344
|
const { transcript } = read;
|
|
346
345
|
// From the last entry read (the bound is inclusive), or the first.
|
|
347
346
|
const query = { order: 'ascending' as const, ...(read.last === undefined ? {} : { minEntryId: read.last }) };
|
package/src/signals.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
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 { needing, sdk as restate } from '@restatedev/sessions/internal';
|
|
10
|
+
import { Link, 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
|
|
|
@@ -20,8 +20,8 @@ const keyOf = (service: string, key: string) => `${service}:${key}`;
|
|
|
20
20
|
/** The key of the signals of the agent of `ctx`. */
|
|
21
21
|
export const signalsOf = (ctx: AgentContext) => keyOf(ctx.request.target.service, ctx.key);
|
|
22
22
|
|
|
23
|
-
/**
|
|
24
|
-
const COMPANION =
|
|
23
|
+
/** How an object of @restatedev/agents (`sessionObject()`, every `agent()`) reaches its companion, for one of its keys. */
|
|
24
|
+
const COMPANION = new Link<(key: string) => Companion>('restate-agents.companion');
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* An agent's companion, for one of its keys: its `signals` object, which
|
|
@@ -43,13 +43,11 @@ export interface Companion {
|
|
|
43
43
|
*/
|
|
44
44
|
export const withCompanion = <D extends { name: string }>(definition: D): D =>
|
|
45
45
|
needing(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}),
|
|
52
|
-
}),
|
|
46
|
+
COMPANION.on(definition, (key) => ({
|
|
47
|
+
object: signals,
|
|
48
|
+
key: keyOf(definition.name, key),
|
|
49
|
+
agent: { service: definition.name, key },
|
|
50
|
+
})),
|
|
53
51
|
() => [signals],
|
|
54
52
|
);
|
|
55
53
|
|
|
@@ -58,14 +56,14 @@ export const withCompanion = <D extends { name: string }>(definition: D): D =>
|
|
|
58
56
|
* `sessionObject()` or `agent()` of @restatedev/agents) to reach what it is doing.
|
|
59
57
|
*/
|
|
60
58
|
export function companionOf(agent: { name: string }, key: string): Companion {
|
|
61
|
-
const companion = (agent
|
|
59
|
+
const companion = COMPANION.of(agent);
|
|
62
60
|
if (!companion)
|
|
63
61
|
throw new Error(`${agent.name} is not an agent of @restatedev/agents (a sessionObject() or an agent())`);
|
|
64
62
|
return companion(key);
|
|
65
63
|
}
|
|
66
64
|
|
|
67
65
|
/** Whether `agent` is an object of @restatedev/agents, with a companion. */
|
|
68
|
-
export const hasCompanion = (agent: object) => COMPANION
|
|
66
|
+
export const hasCompanion = (agent: object) => COMPANION.of(agent) !== undefined;
|
|
69
67
|
|
|
70
68
|
/**
|
|
71
69
|
* Deliver `text` to `agent`'s key `key`, from any handler: a message of its
|