@theoremjs/web 0.3.5-next.938.g3cbd0dd → 0.3.5-next.940.g78c83ac

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
@@ -465,7 +465,7 @@ Set `lateResultsDecider` on your `TurnWiring` and pass it to `createTheoremHandl
465
465
 
466
466
  The handler runs each reply under a hold of the chat, with `runConversation` from `@theoremjs/agents/conversation`. One reply at a time reads the chat's results. A result is told only when the reply that read it is saved. A reply that fails gives its results back, and a later reply reads them.
467
467
 
468
- - `policy(items, moment)` picks, by `id` (the call's `operationId`), the results the model reads now. Each item has the call's `outcome`, and `handovers`: the earlier replies it was given to that ended unsaved. The handler asks it as a reply opens (`stage: 'pre_turn'`), after each step whose calls settled (`'post_tool'`), and once the model has answered (`'before_end'`). `moment.opening` is `'message'` for the person's message, `'wake'` for a reply to no message, and `'follow_up'` as a reply ends, for results that ended while it ran. The handler also asks it at `opening: 'wake'` on each conversation read, to say whether the page wakes the chat. It must change nothing. A result it leaves out waits for a later reply. A result it puts in `giveUp` is never read, and is reported once to `onError` with the code `late_results_given_up`.
468
+ - `policy(items, moment)` picks, by `id` (the call's `operationId`), the results the model reads now. Each item has the call's `outcome`, and `handovers`: the earlier replies it was given to that ended unsaved. The handler asks it as a reply opens (`stage: 'pre_turn'`), after each step whose calls settled (`'post_tool'`), and once the model has answered (`'before_end'`). `moment.host` is the request's `host`, as `ended` gets it, so a policy can keep one person's quiet hours. `moment.opening` is `'message'` for the person's message, `'wake'` for a reply to no message, and `'follow_up'` as a reply ends, for results that ended while it ran. The handler also asks it at `opening: 'wake'` on each conversation read, to say whether the page wakes the chat. It must change nothing. A result it leaves out waits for a later reply. A result it puts in `giveUp` is never read, and is reported once to `onError` with the code `late_results_given_up`.
469
469
  - `ended({ host, result, wake, notify })` hears each background call end. `wake(conversation)` runs a reply to no message from the server, as a page's wake does, so a chat answers with no page open. `notify()` tells the session's open pages to read again.
470
470
 
471
471
  `tellEveryResult` is the decider for an app with no server of its own: every reply reads every result that waits, and the open page wakes the chat. A result that two replies read and did not tell (the model failed, or the reply lost the chat) is given up and reported once as `late_results_given_up`, so a model that keeps failing does not reply again and again. Write your own to keep a count, such as a cap or quiet hours, or to reply from the server.
@@ -499,7 +499,7 @@ Set `pageToolTimeoutMs` to give the page a deadline for a result. Unset, a dispa
499
499
  | `postedMessage(iface, input, context?)` | Checks the person's message against the profile's inputs. `iface` is `interfaceFromProfile(profile, defaultKernelScope.tools)` from `@theoremjs/agents/interface`. |
500
500
  | `messageReply({ host, opened, message, walkAway?, runner })` | Posts the message, runs the reply and writes it back. Returns the lines to stream. |
501
501
  | `answerReply({ host, opened, call, settling, runner })` | Continues a reply after one of its calls was answered. `settling` is the call's events from the kernel's `invokeTool`. |
502
- | `rewindConversation({ host, opened, at, walkAway? })` | Cuts the conversation after transcript block `at`. `-1` empties it. |
502
+ | `rewindConversation({ host, opened, at, walkAway? })` | Cuts the conversation after transcript block `at`. `-1` empties it. A save that only added after `at` as the rewind read the chat is cut away too. When that save stopped on an approval, the rewind fails with `session.gate_pending`. |
503
503
  | `conversationView(host, conversation)` | The conversation as the chat draws it. |
504
504
  | `waitingCalls(conversation)` | The calls a paused reply waits on. A message or a rewind walks away from exactly these. |
505
505
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/web",
3
- "version": "0.3.5-next.938.g3cbd0dd",
3
+ "version": "0.3.5-next.940.g78c83ac",
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.938.g3cbd0dd"
28
+ "@theoremjs/agents": "^0.3.5-next.940.g78c83ac"
29
29
  }
30
30
  }
@@ -484,14 +484,22 @@ export async function* messageReply(args) {
484
484
  }
485
485
  /**
486
486
  * The conversation as a save that `err` lost to left it, while this reply's hold still has the chat.
487
- * Undefined when `err` is no such loss, the hold lost the chat, or no newer save is there to read.
487
+ * Undefined when no hold runs the reply or it lost the chat: with no hold, the save it lost to is
488
+ * another message's.
488
489
  *
489
490
  * why: The hold a message took the chat from may land the one save it began first; no save of it follows.
490
491
  */
491
492
  async function savedSince(host, opened, runner, err) {
492
- if (!(err instanceof TheoremError && err.cause === CONFLICT) || opened.ref === null)
493
+ if (!runner.held || !(await runner.held()))
493
494
  return undefined;
494
- if (runner.held && !(await runner.held()))
495
+ return newerSave(host, opened, err);
496
+ }
497
+ /**
498
+ * The conversation as the save that `err` lost to left it. Undefined when `err` is no such loss or
499
+ * no newer save is there to read.
500
+ */
501
+ async function newerSave(host, opened, err) {
502
+ if (!(err instanceof TheoremError && err.cause === CONFLICT) || opened.ref === null)
495
503
  return undefined;
496
504
  const snapshot = await host.store.read(host.sessionId, opened.ref);
497
505
  if (!snapshot || snapshot.revision === opened.revision)
@@ -588,12 +596,14 @@ function requireRewindPoint(conversation, at) {
588
596
  * start over. A paused reply is walked away from first, and commits before the cut.
589
597
  */
590
598
  export async function rewindConversation(args) {
591
- const { host, opened, at } = args;
599
+ const { host, at } = args;
600
+ let { opened } = args;
592
601
  let { conversation } = opened;
593
602
  requireRewindPoint(conversation, at);
594
603
  requireWalkAway(conversation, args.walkAway);
595
- if (conversation.paused) {
596
- const walked = pausedReply(conversation.paused);
604
+ const { paused } = conversation;
605
+ if (paused) {
606
+ const walked = pausedReply(paused);
597
607
  const settling = settleWalkedAway(walked, args.walkAway ?? []);
598
608
  let step = await settling.next();
599
609
  while (!step.done)
@@ -602,13 +612,34 @@ export async function rewindConversation(args) {
602
612
  throw failureOf(step.value);
603
613
  conversation = committed(host, conversation, walked);
604
614
  }
605
- const transcript = conversation.transcript.slice(0, at + 1);
606
- return save(host, opened, {
607
- version: 2,
608
- id: conversation.id,
609
- transcript,
610
- history: historyFromTranscriptBlocks(transcript),
611
- });
615
+ for (;;) {
616
+ const transcript = conversation.transcript.slice(0, at + 1);
617
+ try {
618
+ return await save(host, opened, {
619
+ version: 2,
620
+ id: conversation.id,
621
+ transcript,
622
+ history: historyFromTranscriptBlocks(transcript),
623
+ });
624
+ }
625
+ catch (err) {
626
+ const moved = paused ? undefined : await newerSave(host, opened, err);
627
+ if (!moved || !keepsThrough(moved.conversation, transcript))
628
+ throw err;
629
+ requireWalkAway(moved.conversation, undefined);
630
+ opened = moved;
631
+ conversation = moved.conversation;
632
+ }
633
+ }
634
+ }
635
+ /**
636
+ * Whether `conversation` still holds `kept` as its first blocks: a save that only added after the
637
+ * cut leaves the rewind the same cut.
638
+ *
639
+ * why: A reply that held the chat may land the save it began as the person rewinds.
640
+ */
641
+ function keepsThrough(conversation, kept) {
642
+ return kept.every((block, index) => conversation.transcript[index]?.id === block.id);
612
643
  }
613
644
  /** A paused call's operation: every call a session issued carries one. */
614
645
  export function requiredOperationId(call) {
package/server/handler.js CHANGED
@@ -570,10 +570,11 @@ function heldLines(ctx, request, session, host, opened, start, reply, follow = {
570
570
  const chat = chatInbox(ctx, request, session, host, opened);
571
571
  const ref = () => chat.ref;
572
572
  return sent(async (send) => {
573
+ const whose = await ctx.options.host?.(request);
573
574
  const runOnce = (begin, lines) => runConversation({
574
575
  conversation: chat.key,
575
576
  inbox: chat.inbox,
576
- policy: decider.policy,
577
+ policy: (items, moment) => decider.policy(items, { ...moment, host: whose }),
577
578
  start: begin,
578
579
  newId: () => crypto.randomUUID(),
579
580
  renewEveryMs: chat.leaseMs / 3,
@@ -876,7 +877,12 @@ async function wakeDue(ctx, request, session, host, ref, conversation) {
876
877
  const { items } = await chat.inbox.read(chat.key);
877
878
  if (items.length === 0)
878
879
  return false;
879
- const { tell } = await decider.policy(items, { stage: 'pre_turn', step: 0, opening: 'wake' });
880
+ const { tell } = await decider.policy(items, {
881
+ stage: 'pre_turn',
882
+ step: 0,
883
+ opening: 'wake',
884
+ host: await ctx.options.host?.(request),
885
+ });
880
886
  return tell.length > 0;
881
887
  }
882
888
  function isDetachedBlock(block) {