@theoremjs/web 0.3.5-next.933.gfc0e83b → 0.3.5-next.935.ga3dc485

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
@@ -468,7 +468,7 @@ Set `lateResultsDecider` on your `TurnWiring` and pass it to `createTheoremHandl
468
468
 
469
469
  `pageWakes` is the decider for an app with no server of its own: the page open on the chat wakes it, and every reply reads every result that waits. Write your own to keep a count, such as a cap or quiet hours, or to reply from the server.
470
470
 
471
- When `due` says yes, the conversation read says `wake: true`, and the page posts `<base>/wake` once it is idle. The handler runs a turn with no message and passes what `give` gives in `input.lateResults`. A wake runs only when the transcript does not end on a person's message and no reply waits on a gate. The handler takes each result (`wokenAt` on the session's operation) before it asks `give`, so two pages that post at once run one turn. A result `give` leaves out loses that mark, and a later wake can take it. A wake that fails is not tried again; the next message's `give` is asked. A message the person sends during a wake wins: the wake writes nothing, and its stream ends with no error.
471
+ When `due` says yes, the conversation read says `wake: true`, and the page posts `<base>/wake` once it is idle. The handler runs a turn with no message, and the turn reads what `give` gives as it opens (`TurnRequest.lateResults`). A wake runs only when the transcript does not end on a person's message and no reply waits on a gate. The handler takes each result (`wokenAt` on the session's operation) before it asks `give`, so two pages that post at once run one turn. A result `give` leaves out loses that mark, and a later wake can take it. A wake that fails is not tried again; the next message's `give` is asked. A message the person sends during a wake wins: the wake writes nothing, and its stream ends with no error.
472
472
 
473
473
  The decider is the one way a result reaches the model. If your `background` service told the model a result some other way, such as with your own `onStage`, remove that, or the model reads it twice.
474
474
 
@@ -110,6 +110,7 @@ const transcriptBlock = z.discriminatedUnion('kind', [
110
110
  kind: z.literal('late-results'),
111
111
  text: z.string(),
112
112
  operationIds: z.array(z.string()),
113
+ inReply: z.boolean(),
113
114
  }),
114
115
  z.object({ ...blockId, kind: z.literal('error'), message: z.string() }),
115
116
  z.object({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/web",
3
- "version": "0.3.5-next.933.gfc0e83b",
3
+ "version": "0.3.5-next.935.ga3dc485",
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.933.gfc0e83b"
28
+ "@theoremjs/agents": "^0.3.5-next.935.ga3dc485"
29
29
  }
30
30
  }
package/server/handler.js CHANGED
@@ -478,18 +478,18 @@ async function* once(event, labels) {
478
478
  yield withPublicLabels(event, labels);
479
479
  }
480
480
  /**
481
- * `turn.input`, with the late results this reply gives the model: those the wake took, or those
482
- * the decider gives a message. A reply that goes on after a pause has none, so it is asked once.
481
+ * The late results this reply gives the model as it opens: those the wake took, or those the
482
+ * decider gives a message. A reply that goes on after a pause has none, so it is asked once.
483
483
  */
484
- async function withLateResults(ctx, request, host, turn, operations, late) {
484
+ async function openingLateResults(ctx, request, host, turn, operations, late) {
485
485
  if (!turn.unread)
486
- return turn.input;
486
+ return {};
487
487
  const given = late.given ??
488
488
  (await givenResults(ctx, host, late.conversation, await readableResults(ctx, request, turn.unread, operations), 'message'));
489
489
  if (given.length === 0)
490
- return turn.input;
490
+ return {};
491
491
  const outcomes = given.map((operation) => operation.outcome);
492
- return { ...turn.input, lateResults: [...(turn.input.lateResults ?? []), ...outcomes] };
492
+ return { lateResults: ({ stage }) => (stage === 'pre_turn' ? outcomes : []) };
493
493
  }
494
494
  /** Those of `results` the decider gives the reply; none without a decider. */
495
495
  async function givenResults(ctx, host, conversation, results, reason) {
@@ -542,7 +542,8 @@ async function* chatTurn(ctx, request, session, turn, key, stop, late) {
542
542
  execution: sessionToolCoordinator(ctx.sessions, session.id),
543
543
  ...backgroundWiring(ctx, request, session.id),
544
544
  pageExecution: sessionPageDispatch(ctx.sessions, session.id, ctx.pageToolBroker, ctx.options.pageToolTimeoutMs).execution,
545
- input: await withLateResults(ctx, request, host, turn, state.operations, late),
545
+ input: turn.input,
546
+ ...(await openingLateResults(ctx, request, host, turn, state.operations, late)),
546
547
  ...(turn.conversationId ? { conversationId: turn.conversationId } : {}),
547
548
  ...(turn.providerState ? { providerState: turn.providerState } : {}),
548
549
  sessionPermissions: state.permissions,
@@ -44,7 +44,6 @@ const inputSchema = z.strictObject({
44
44
  .optional(),
45
45
  voice: z.array(blobSchema).optional(),
46
46
  history: z.array(turnHistoryMessageSchema).optional(),
47
- lateResults: z.array(toolOperationOutcomeSchema).optional(),
48
47
  repair: z
49
48
  .strictObject({ previousOutput: z.string(), rejection: z.string(), guidance: z.string() })
50
49
  .optional(),
@@ -2,7 +2,7 @@
2
2
  * The application's turn wiring (`TurnWiring.prepare`) laid over the turn the handler builds.
3
3
  * The handler keeps the fields it owns; the application's stage handler and credentials go
4
4
  * first, and the handler's own run when the application's have nothing. An application's own
5
- * tool record and background service run with the session's, never in place of it.
5
+ * tool record, background service, and late results run with the session's, never in place of it.
6
6
  *
7
7
  * @module
8
8
  */
@@ -10,8 +10,11 @@ import type { NormalizedTurnRequest, StageHandler, TurnRequest, TurnWiring } fro
10
10
  import type { ToolBackgroundService, ToolCredentialSource } from '@theoremjs/agents/kernel';
11
11
  /** The fields a handler sets on every turn, which `prepare` cannot replace. */
12
12
  export type HandlerOwned = Pick<TurnRequest, 'profile' | 'pageExecution' | 'providerState' | 'signal'>;
13
- /** The session's tool record and the handler's background service, which `prepare` can add to. */
14
- export type HandlerTools = Pick<TurnRequest, 'execution' | 'background'>;
13
+ /**
14
+ * The session's tool record, the handler's background service, and the session's late results,
15
+ * which `prepare` can add to.
16
+ */
17
+ export type HandlerTools = Pick<TurnRequest, 'execution' | 'background' | 'lateResults'>;
15
18
  /** What the handler adds to a turn of its own: the session's credentials and its steering. */
16
19
  export type HandlerTurnParts = {
17
20
  credentials: ToolCredentialSource;
@@ -2,7 +2,7 @@
2
2
  * The application's turn wiring (`TurnWiring.prepare`) laid over the turn the handler builds.
3
3
  * The handler keeps the fields it owns; the application's stage handler and credentials go
4
4
  * first, and the handler's own run when the application's have nothing. An application's own
5
- * tool record and background service run with the session's, never in place of it.
5
+ * tool record, background service, and late results run with the session's, never in place of it.
6
6
  *
7
7
  * @module
8
8
  */
@@ -23,11 +23,13 @@ export async function preparedTurn(prepare, turn, handler) {
23
23
  const onStage = stagesInOrder(app.onStage, handler.onStage);
24
24
  const execution = toolsInOrder(app.execution, turn.execution);
25
25
  const background = backgroundInOrder(app.background, turn.background, handler.announce);
26
+ const lateResults = lateResultsInOrder(app.lateResults, turn.lateResults);
26
27
  return {
27
28
  ...app,
28
29
  ...owned,
29
30
  ...(execution ? { execution } : {}),
30
31
  ...(background ? { background } : {}),
32
+ ...(lateResults ? { lateResults } : {}),
31
33
  credentials: credentialsInOrder(app.credentials, handler.credentials),
32
34
  ...(onStage ? { onStage } : {}),
33
35
  };
@@ -50,6 +52,15 @@ function backgroundInOrder(app, own, announce) {
50
52
  return own;
51
53
  return announce ? announce(app) : app;
52
54
  }
55
+ /**
56
+ * The application's late results, then the session's, at each moment the turn asks. The session's
57
+ * are the results it marked given, so an application's `prepare` cannot drop them.
58
+ */
59
+ function lateResultsInOrder(app, session) {
60
+ if (!app || !session || app === session)
61
+ return session ?? app;
62
+ return async (moment) => [...(await app(moment)), ...(await session(moment))];
63
+ }
53
64
  /**
54
65
  * The application's stage handler, then the handler's own when the application's asks for
55
66
  * nothing. What the second one would have done waits for the next stage that allows it.