@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/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, onClear, inBackground, type Messages, type MessagesRequest, refusal, type Turn } from './common.ts';
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
- restate.sessionObject({
295
- name,
296
- description,
297
- schema,
298
- handlers: { ...(messages === undefined ? {} : INTERNAL), ...yours },
299
- views: { ...BUILT_IN, ...agent, ...views },
300
- metadata,
301
- options: { ...OPTIONS, ...options },
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 refusal(error) ?? error;
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 type { Database } from '@restatedev/sessions';
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.db, await harness.root(context), context);
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 transcripts = new WeakMap<Database, { last?: EntryId; transcript: Transcript }>();
339
+ const TRANSCRIPT = new ScopeKey<{ last?: EntryId; transcript: Transcript }>('the transcript');
340
340
 
341
- /** The root conversation in `db`, `root` of a Harness on it, from pi-durable's entries, as `messages` gives it. */
342
- const transcribe = async (db: Database, root: Conversation, context: Context) => {
343
- const read = transcripts.get(db) ?? { transcript: new Transcript() };
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
- /** Where an object of @restatedev/agents (`sessionObject()`, every `agent()`) points to its companion. */
24
- const COMPANION = Symbol('restate-agents.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
- 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
- }),
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 as { [COMPANION]?: (key: string) => Companion })[COMPANION];
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 in agent;
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