@theoremjs/web 0.3.5-next.917.g6cb0b91 → 0.3.5-next.918.g28ade76

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 CHANGED
@@ -457,7 +457,7 @@ Browser dispatch uses a host-owned `PageToolExecution` service beside the execut
457
457
  `createTheoremHandler` supplies both services. Live relays put the session boundary's `pageExecution` and `toolExecution` on `SessionRequest`.
458
458
 
459
459
  A tool with a `background` setting runs inline until you pass the `background` option to `createTheoremHandler`, or return `background` from `prepare`, with an `execution` of your own that has `detach`, when the same turns run elsewhere in your app too. The option takes `{ request, sessionId }` and returns a `ToolBackgroundService`, which receives each detached call and its late events. The session keeps the call as `background` until it settles, and a late settlement does not invalidate a checkpoint. A conversation read shows the call as it stands: running, then its result, or an error for a marked call. An open page reads it on its next turn or reload, unless you set `notifier`. When the session has dropped a settled operation, the block reads an error with the code `outcome_not_retained`. On a durable store, a worker that dies leaves the operation as `background`, so your sweep marks it `uncertain` with `worker_lost` through `compareAndSwap`. Keep the service's values valid after the request ends. A live relay passes its service to `runSession`.
460
- To show an open page that a background call ended, with no turn or reload, pass `notifier: createMemoryNotifier()` to `createTheoremHandler`. While a call runs, the conversation read tells the page to open `POST <base>/notify`. The handler sends one `changed` line when the stream opens, and one when each background call in the session ends. The line carries no content, so the page calls the same conversation read as before. The page closes the stream when no call runs. The model reads nothing new: a late result still reaches it through `input.lateResults`.
460
+ To show an open page that a background call ended, with no turn or reload, pass `notifier: createMemoryNotifier()` to `createTheoremHandler`. While a call runs, the conversation read tells the page to open `POST <base>/notify`. The handler sends one `changed` line when the stream opens, and one when each background call in the session ends. The line carries no content, so the page calls the same conversation read as before. The page closes the stream when no call runs. The model reads the result on the conversation's next turn: the handler passes the session's stored outcome in `input.lateResults`, once for each call. A call is read when a `late-results` block in the transcript names it. A call `authorizeToolReplay` withholds from the request stays unread.
461
461
 
462
462
  `createMemoryNotifier` reaches only pages connected to the process that ran the call. On more than one instance, give a shared `TheoremNotifier`. Your sweep calls `publish` with the session id after it marks a call `worker_lost`, because that write does not pass through the handler. Hosting platforms cut long responses. Set `notifyRetryMs` so that the page reopens the stream after that wait. Unset, the page does not reopen it, and reads the end on its next turn or reload.
463
463
 
@@ -42,7 +42,6 @@ const toolCall = z.object({
42
42
  state: toolCallState.optional(),
43
43
  startedAt: z.number().optional(),
44
44
  endedAt: z.number().optional(),
45
- artifacts: z.array(z.unknown()),
46
45
  get steps() {
47
46
  return z.array(toolCall).optional();
48
47
  },
@@ -104,7 +103,12 @@ const transcriptBlock = z.discriminatedUnion('kind', [
104
103
  })),
105
104
  }),
106
105
  z.object({ ...blockId, kind: z.literal('guardrail'), guardrail: guardrailEventSchema }),
107
- z.object({ ...blockId, kind: z.literal('late-results'), text: z.string() }),
106
+ z.object({
107
+ ...blockId,
108
+ kind: z.literal('late-results'),
109
+ text: z.string(),
110
+ operationIds: z.array(z.string()),
111
+ }),
108
112
  z.object({ ...blockId, kind: z.literal('error'), message: z.string() }),
109
113
  z.object({
110
114
  ...blockId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/web",
3
- "version": "0.3.5-next.917.g6cb0b91",
3
+ "version": "0.3.5-next.918.g28ade76",
4
4
  "description": "Theorem on the web platform, with no UI framework: Web-standard server handlers that serve a profile, and the browser client that talks to them.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,6 +25,6 @@
25
25
  },
26
26
  "peerDependencies": {
27
27
  "zod": "^4.6.5",
28
- "@theoremjs/agents": "^0.3.5-next.917.g6cb0b91"
28
+ "@theoremjs/agents": "^0.3.5-next.918.g28ade76"
29
29
  }
30
30
  }
@@ -122,6 +122,13 @@ export type ReplyTurn = {
122
122
  stated?: StatedTurn;
123
123
  /** The tools and steps the reply had when it paused, for a reply that goes on. */
124
124
  carried?: ReplyCarried;
125
+ /** The conversation's detached calls whose results its model has not read yet. */
126
+ unread?: DetachedCall[];
127
+ };
128
+ /** A call that detached in a conversation: the operation the session keeps its end under. */
129
+ export type DetachedCall = {
130
+ callId: string;
131
+ operationId: string;
125
132
  };
126
133
  /** What a reply's model run takes from the host. */
127
134
  export type ReplyRunner = {
@@ -148,6 +148,19 @@ function refLine(opened, posted) {
148
148
  return [];
149
149
  return [{ type: 'conversation', ref: opened.ref, ...(posted ? { posted: true } : {}) }];
150
150
  }
151
+ /**
152
+ * The conversation's detached calls that no late-results block names.
153
+ * invariant: The transcript is the record of what the model read, so a result is fed once.
154
+ */
155
+ function unreadDetachedCalls(transcript) {
156
+ const read = new Set(transcript.flatMap((block) => (block.kind === 'late-results' ? block.operationIds : [])));
157
+ return transcript.flatMap((block) => block.kind === 'tool' &&
158
+ block.tool.state?.phase === 'background' &&
159
+ block.tool.operationId &&
160
+ !read.has(block.tool.operationId)
161
+ ? [{ callId: block.tool.callId, operationId: block.tool.operationId }]
162
+ : []);
163
+ }
151
164
  /** The model's history once the reply's events enter it. */
152
165
  function replyHistory(reply) {
153
166
  return appendAssistantEventsToHistory(reply.base, reply.events.slice(reply.baseEvents));
@@ -237,6 +250,8 @@ async function* runReply(host, opened, reply, runner, input, withMessage) {
237
250
  effort: runner.effort,
238
251
  ...(runner.stated ? { stated: runner.stated } : {}),
239
252
  ...(runner.carried ? { carried: runner.carried } : {}),
253
+ // why: Late results open a reply; a reply that goes on after a pause reads them on the next one.
254
+ ...(runner.carried ? {} : { unread: unreadDetachedCalls(conversation.transcript) }),
240
255
  });
241
256
  for await (const event of events) {
242
257
  reply.events.push(event);
package/server/handler.js CHANGED
@@ -434,6 +434,25 @@ function announced(ctx, request, sessionId, service) {
434
434
  async function* once(event, labels) {
435
435
  yield withPublicLabels(event, labels);
436
436
  }
437
+ /**
438
+ * `turn.input`, with the settled ends of the conversation's detached calls the model has not read.
439
+ * A call the host does not let this request replay stays unread, as its block stays withheld.
440
+ */
441
+ async function withLateResults(ctx, request, turn, operations) {
442
+ const outcomes = [];
443
+ for (const { operationId } of turn.unread ?? []) {
444
+ const operation = Object.hasOwn(operations, operationId) ? operations[operationId] : undefined;
445
+ if (operation?.status !== 'settled')
446
+ continue;
447
+ const { authorizeToolReplay } = ctx.options;
448
+ if (authorizeToolReplay && !(await authorizeToolReplay({ request, operation })))
449
+ continue;
450
+ outcomes.push(operation.outcome);
451
+ }
452
+ if (outcomes.length === 0)
453
+ return turn.input;
454
+ return { ...turn.input, lateResults: [...(turn.input.lateResults ?? []), ...outcomes] };
455
+ }
437
456
  /** One model run of a chat's reply, its gates recorded in the session as they pause. */
438
457
  async function* chatTurn(ctx, request, session, turn, key, stop) {
439
458
  const state = await ctx.sessions.read(session.id);
@@ -450,7 +469,7 @@ async function* chatTurn(ctx, request, session, turn, key, stop) {
450
469
  execution: sessionToolCoordinator(ctx.sessions, session.id),
451
470
  ...backgroundWiring(ctx, request, session.id),
452
471
  pageExecution: sessionPageDispatch(ctx.sessions, session.id, ctx.pageToolBroker, ctx.options.pageToolTimeoutMs).execution,
453
- input: turn.input,
472
+ input: await withLateResults(ctx, request, turn, state.operations),
454
473
  ...(turn.conversationId ? { conversationId: turn.conversationId } : {}),
455
474
  ...(turn.providerState ? { providerState: turn.providerState } : {}),
456
475
  sessionPermissions: state.permissions,