@theoremjs/web 0.3.5-next.1012.g714018f → 0.3.5-next.1014.g14db6b7
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 +47 -8
- package/client/chat.js +98 -2
- package/client/index.d.ts +1 -1
- package/client/transport.d.ts +18 -4
- package/client/transport.js +6 -0
- package/package.json +2 -2
- package/server/conversation-core.d.ts +50 -19
- package/server/conversation-core.js +368 -117
- package/server/conversation-inbox.d.ts +19 -14
- package/server/conversation-inbox.js +57 -7
- package/server/conversation.d.ts +1 -0
- package/server/conversation.js +5 -1
- package/server/create-conversation-core.d.ts +1 -1
- package/server/create-conversation-core.js +3 -2
- package/server/handler.d.ts +11 -5
- package/server/handler.js +41 -12
- package/server/mod.d.ts +4 -3
- package/server/mod.js +4 -3
- package/server/own-inbox.d.ts +83 -0
- package/server/own-inbox.js +175 -0
- package/server/reply-feed.d.ts +41 -0
- package/server/reply-feed.js +48 -0
- package/server/steer-inbox.d.ts +0 -41
- package/server/steer-inbox.js +0 -84
package/README.md
CHANGED
|
@@ -284,17 +284,18 @@ const transport = withPageToolDispatch(
|
|
|
284
284
|
| `session` | Returns the session id of the caller, for example `${userId}:${conversationId}`. Return `undefined` to refuse the request with a 401. Default: a random id in an HttpOnly, SameSite=Lax cookie that lasts 30 days. |
|
|
285
285
|
| `host` | App data for the tool handlers (`ctx.host`), such as the signed-in user. The handler asks it one time for each run, when the run first reads it. |
|
|
286
286
|
| `prepare` | Your app's own turn, from the one the handler builds (`TurnWiring.prepare` from `@theoremjs/agents`). Set what your app sets per turn, such as `system`, `credentials`, `onStage`, `onEvent` and `onAgentCall`; read your app data as `turn.host`. The handler keeps its profile and its abort. Your `onStage` runs before the handler's steering, and your `credentials` are read before the session's. An `execution` you return decides each call, and the session still records the same call; a `background` you return takes the place of the `background` option. A call held at a gate runs on, once answered, with the `host`, `onStage`, `credentials`, `execution` and `background` you give for that call's turn; that turn has `answersHeldCall`, and its `onEvent` is not called, because no new turn runs. |
|
|
287
|
-
| `inbox`, `beforeSave`, `afterSave`, `compact` | Your app's own inbox and your steps around each save, as [the conversation core](#run-replies-with-no-page-request) takes them. Each is given the request's context: its `host` is what `host` returned for the request, so `await at.host`. With `inbox`, every reply the page starts runs under a hold of your conversation, and a wake tells the items that wait in it. A steer
|
|
287
|
+
| `inbox`, `beforeSave`, `afterSave`, `compact` | Your app's own inbox and your steps around each save, as [the conversation core](#run-replies-with-no-page-request) takes them. Each is given the request's context: its `host` is what `host` returned for the request, so `await at.host`. With `inbox`, every reply the page starts runs under a hold of your conversation, and a wake tells the items that wait in it. A steer needs `intake`: it saves the message into your inbox. Without `intake`, the handler refuses the steer with a `config` error. Do not set `inbox` with `lateResultsDecider`. |
|
|
288
288
|
| `onStage` | The server's own stage handler, on every call the handler runs, a host profile's included: after your app's (`prepare`), before steering. For a rule the server adds to whatever the app says, such as a question before each tool that writes. |
|
|
289
289
|
| `pageToolBroker` | Delivers results to active page dispatches. Default: bounded process memory. |
|
|
290
290
|
| `pageStatesTurn` | Let the page say which prompt lines a turn gets, through `stated` on a turn or a live call. Set it only for a page you trust as yourself, such as a builder's own tool. Without it, a request with `stated` is refused. |
|
|
291
291
|
| `conversationStore` | Where the conversation is kept. Required, except for a stateless profile, which must not have one. See [Keep the conversation](#keep-the-conversation). |
|
|
292
|
-
| `sessionStore
|
|
292
|
+
| `sessionStore` | Where open gates are kept. Default: process memory. A steer and a stop need no store of their own: see [Steer and stop without your own inbox](#steer-and-stop-without-your-own-inbox). |
|
|
293
293
|
| `lateResultsDecider` | Who tells the model that a background call ended: `TurnWiring.lateResultsDecider`. Set it on your `TurnWiring`, and the studio uses it too. Unset, no page wakes the chat and no reply reads a late result. `tellEveryResult` tells each result to the first reply that runs, and gives up a result two replies read without telling. See [the background section](#share-session-authority). |
|
|
294
294
|
| `conversationLeaseMs` | How long a reply keeps its chat after it last renewed, in milliseconds. A running reply renews every third of it. A wake that finds another reply running tries again once this runs out. Default: `60000`. |
|
|
295
295
|
| `onContinue` | Who runs the next reply when a reply ends with items that wait and may be told now. Return `'here'`, and the handler runs it in the same response. Return `'taken'`, and your app runs it with the conversation core's `followUp`. Unset, the handler lets the chat go, and the page wakes it. |
|
|
296
296
|
| `intake` | Your app's own save of each message the page sends, on `/turn` and `/steer`. Set it when a message must be in your database before any reply runs. Needs `inbox`. See [Save a page's message in your own write](#save-a-pages-message-in-your-own-write). |
|
|
297
297
|
| `notifier`, `notifyRetryMs` | Tell an open page that a background call ended, and how long the page waits to reopen a cut stream. Unset, the page reads the end on its next turn or reload. See [the background section](#share-session-authority). |
|
|
298
|
+
| `replyFeed` | Lets every page open on a conversation draw a running reply's lines live, whoever started it. Needs `notifier`. Unset, a page that did not start the reply reads it once it ends. See [Follow a running reply](#follow-a-running-reply). |
|
|
298
299
|
| `credentialStore` | Where tool credentials are kept, by session. Default: process memory. |
|
|
299
300
|
| `authorizationUrl` | Returns the sign-in URL for an OAuth gate. Without it, an OAuth gate has no URL. |
|
|
300
301
|
| `gateTtlMs` | How long a gate waits for an answer. A later answer fails with `session.gate_expired`, and the next message or the next wake cancels the call. Unset, a gate waits until it is answered. |
|
|
@@ -557,10 +558,10 @@ async function received(sessionId: string, text: string, ref: string | undefined
|
|
|
557
558
|
| `release(at, { ref, hold })` | Lets the conversation go under that hold, with no follow-up. |
|
|
558
559
|
| `read(at, { ref })` | Returns the conversation as a page draws it. |
|
|
559
560
|
| `rewind(at, { ref, at })` | Cuts the conversation after transcript block `at`. |
|
|
560
|
-
| `steer(at, { turnId, id, input })` | Puts `input` into
|
|
561
|
-
| `stop(at, { turnId } \| { conversation })` | Stops a running reply: the turn `turnId` names, or the reply that holds `conversation`. Returns `false` when it is not running. When your `inbox` has a `stop`, it stops the reply that holds the conversation, and reads neither. Without one,
|
|
561
|
+
| `steer(at, { ref, id, input } \| { turnId, id, input })` | Puts `input` into the inbox of the running reply: the one that holds the conversation `ref` names, or the run `turnId` names. Returns `false` when no reply runs there. Throws `session.reply_unreachable` when the store says a reply runs that this process does not hold, and what `message` throws for an `input` the profile does not take, such as text over `inputs.maxTextChars`. Throws a `config` error when you set `inbox`: use `intake`, which saves the message in your inbox. |
|
|
562
|
+
| `stop(at, { turnId } \| { conversation })` | Stops a running reply: the turn `turnId` names, or the reply that holds `conversation`. Returns `false` when it is not running. When your `inbox` has a `stop`, it stops the reply that holds the conversation, and reads neither. Without one, Theorem's own stop reaches a reply that this process runs. For a reply the store says runs elsewhere, `{ conversation }` throws `session.reply_unreachable`. |
|
|
562
563
|
|
|
563
|
-
The core takes these options from [Server options](#server-options), with the same meaning: `profile`, `onStage`, `sessionStore`, `conversationStore`, `credentialStore`, `gateTtlMs`, `operationRetentionMs`, `pageToolBroker`, `pageToolTimeoutMs`, `notifier`, `notifyRetryMs`, `
|
|
564
|
+
The core takes these options from [Server options](#server-options), with the same meaning: `profile`, `onStage`, `sessionStore`, `conversationStore`, `credentialStore`, `gateTtlMs`, `operationRetentionMs`, `pageToolBroker`, `pageToolTimeoutMs`, `notifier`, `notifyRetryMs`, `replyFeed`, `lateResultsDecider`, `conversationLeaseMs`, and `clientEvents`. These options take the run context in place of the `Request`:
|
|
564
565
|
|
|
565
566
|
| Option | Use |
|
|
566
567
|
| --- | --- |
|
|
@@ -647,9 +648,22 @@ A reply that opens with items from your inbox sends a `conversation` line with `
|
|
|
647
648
|
|
|
648
649
|
Your store marks the items in `told` in its `write`. If an item in `told` that the reply was handed still waits when the run ends `continue`, the run lets the conversation go and fails with a `config` error, because each follow-up would tell that item again. `onError` receives the error.
|
|
649
650
|
|
|
651
|
+
### Steer and stop without your own inbox
|
|
652
|
+
|
|
653
|
+
Use this when you set no `inbox`, and a person sends a message into a reply that is running, or stops it. Theorem keeps its own conversation inbox for the reply. A steer is a message in that inbox, beside the ended background calls. A reply that holds the conversation takes one message at each `pre_turn`, `post_tool` and `before_end`, and the `stage` event names it in `injected`. A save of the reply that took it tells it once. A stop reaches the reply that holds the conversation.
|
|
654
|
+
|
|
655
|
+
The inbox is named as follows:
|
|
656
|
+
|
|
657
|
+
| Chat | Named by | Kept in |
|
|
658
|
+
| --- | --- | --- |
|
|
659
|
+
| A stored chat | The conversation id, and the session. | Process memory, and the session store for the ended calls. |
|
|
660
|
+
| A chat that keeps no conversation (`providerContinuation.history: 'stateless'`) | The run: the `turnId` the page sent, and the session. | Process memory, for as long as the run lasts. |
|
|
661
|
+
|
|
662
|
+
A page that has a conversation steers and stops by `{ conversation }`. The page's own turn also answers to its `turnId` while it runs, so a steer or stop that names only the turn reaches the same inbox. Both inboxes live in process memory, so a steer must reach the process that runs the reply. A message that waits when a stateless run ends is lost with the run, and the page sends it again. A steer with no reply running is refused with `session.turn_ended`.
|
|
663
|
+
|
|
650
664
|
### Stop a reply from any server
|
|
651
665
|
|
|
652
|
-
Without `stop` in your `inbox`,
|
|
666
|
+
Without `stop` in your `inbox`, Theorem's own stop reaches the replies that this process runs, by conversation or by `turnId`. Set `stop` to keep the stop in your own store. A stop then reaches the reply that holds the conversation, on any server, and from any channel or wake.
|
|
653
667
|
|
|
654
668
|
```ts
|
|
655
669
|
const stop: HoldStop = {
|
|
@@ -684,14 +698,39 @@ const snapshot: ConversationSnapshot = {
|
|
|
684
698
|
|
|
685
699
|
The view the page reads has the same `running`. While it is set, the chat's phase is `streaming` and `runningElsewhere` holds it, so the page acts as it does for its own reply:
|
|
686
700
|
|
|
687
|
-
- The stop button sends `/stop` with `{ conversation }`. Your `inbox`'s `stop` stops the reply. Without one,
|
|
688
|
-
- A steer sends `/steer` with `{ conversation }` and no `turnId`. `intake` saves it. Without `intake`, the steer is refused with `session.reply_unreachable
|
|
701
|
+
- The stop button sends `/stop` with `{ conversation }`. Your `inbox`'s `stop` stops the reply. Without one, a reply that another process runs is refused with `session.reply_unreachable`, the page shows that error, and the reply still shows as working.
|
|
702
|
+
- A steer sends `/steer` with `{ conversation }` and no `turnId`. `intake` saves it. Without `intake`, the steer is refused with `session.reply_unreachable` while the store says a reply runs that this process does not hold.
|
|
689
703
|
- A message sent while it works waits in the queue. Send now stops the reply, then sends the message. When the stop is refused, the message stays in the composer.
|
|
690
704
|
|
|
691
705
|
The page reads again at `heldUntil` once for each `heldUntil`. When your server's clock and the browser's differ, a hold can look lapsed to the page while your store still returns it. The page then waits for your `notifier.publish`, or for a read that returns a new `heldUntil`.
|
|
692
706
|
|
|
693
707
|
Call `notifier.publish` after each save that ends the reply. The page reads the conversation again. When `running` is gone, the queue sends its next message, unless the person stopped the reply.
|
|
694
708
|
|
|
709
|
+
### Follow a running reply
|
|
710
|
+
|
|
711
|
+
A page open on a conversation draws a reply that another page started, or that your app runs with no page request, as it happens: the steps, the pill and the words. Pass `replyFeed` with `notifier` to `createTheoremHandler` or `createTheoremConversationCore`. Without `notifier`, the setup fails with a `config` error.
|
|
712
|
+
|
|
713
|
+
```ts
|
|
714
|
+
import { createMemoryNotifier, createMemoryReplyFeed } from '@theoremjs/web/server';
|
|
715
|
+
|
|
716
|
+
createTheoremHandler({
|
|
717
|
+
// ...
|
|
718
|
+
notifier: createMemoryNotifier(),
|
|
719
|
+
replyFeed: createMemoryReplyFeed(),
|
|
720
|
+
});
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
Every reply that runs under a hold writes each of its lines to the feed, and calls `notifier.publish(sessionId)` when it starts and when it ends. This covers a message, an answer, a wake and a follow-up, from a page or from the core. The conversation read says `follow: true`, and a page that sees `running` and has no reply of its own posts `POST <base>/follow` with `{ conversation }`. The stream is the line stream of `/turn`, with the same `tools.showData` filter. It starts from the first line of the reply, so a page that joins late draws the whole reply, and ends with the reply. The page then reads the conversation as before. Stop and steer by conversation work as they do for any reply the page did not start. The chat draws the followed reply with the same `streaming` state as its own, so no host switch is added.
|
|
724
|
+
|
|
725
|
+
`TheoremReplyFeed` is yours to implement:
|
|
726
|
+
|
|
727
|
+
- `start(conversation, reply)` opens a reply. It replaces the earlier reply of that conversation, and ends that reply's followers.
|
|
728
|
+
- `append(conversation, reply, line)` adds a line.
|
|
729
|
+
- `end(conversation, reply)` closes it and ends its followers.
|
|
730
|
+
- `follow(conversation, listener)` calls `listener.line` with every line so far, then each new line, then `listener.end`. With no reply open, it calls `end` at once. It returns the function that stops the listening.
|
|
731
|
+
|
|
732
|
+
`conversation` is the conversation's id, which stays the same across writes, where the reference changes. Theorem sets no size, retention or timing. `createMemoryReplyFeed` keeps every line of the current reply of each conversation in the process, with no cap, and reaches only the pages of that process. On more than one instance, give a shared `TheoremReplyFeed`. A feed that throws never changes the reply: the error goes to `onError`. `running` is the store's, as in [the section on a running reply](#share-session-authority): the feed does not set it. Without `running`, the page does not follow.
|
|
733
|
+
|
|
695
734
|
### Shape the save that ends a reply
|
|
696
735
|
|
|
697
736
|
Use `beforeSave` when your app keeps each block as its own record, and must decide what a reply writes. The core calls it once for each save that ends a reply, before the store's `write`.
|
package/client/chat.js
CHANGED
|
@@ -7,7 +7,7 @@ import { clientFailure, turnFailure } from "./failure.js";
|
|
|
7
7
|
import { followGenerationDefaults } from "./generation-selection.js";
|
|
8
8
|
import { warnPageToolMismatch } from "./live/live-page-tool.js";
|
|
9
9
|
import { withPageToolDispatch } from "./page-dispatch.js";
|
|
10
|
-
import { sessionAfter, stampWorked, stoppedReply } from "./run-commit.js";
|
|
10
|
+
import { sessionAfter, stampWorked, stoppedReply, streamFoldedTurn } from "./run-commit.js";
|
|
11
11
|
import { extractInlineEncodedAttachments, keepsConversation, resumeInterfaceTool, streamInterfaceDraftTurn, streamInterfaceTurn, streamInterfaceWake, } from "./run-session.js";
|
|
12
12
|
import { isTurnEnded, isUnknownConversation, } from "./transport.js";
|
|
13
13
|
import { foldAssistantTurn, issuedOperationId } from "./turn-client.js";
|
|
@@ -133,6 +133,12 @@ class Chat {
|
|
|
133
133
|
/** What the last view said about the host's push; unset, nothing is listened for. */
|
|
134
134
|
#notify;
|
|
135
135
|
#listening = null;
|
|
136
|
+
/** The reply that runs elsewhere, as this page draws it from the host's lines. */
|
|
137
|
+
#following = null;
|
|
138
|
+
/** The `since` of the last reply whose lines this page began to read: one read of each reply. */
|
|
139
|
+
#followedSince = null;
|
|
140
|
+
/** The last view said the host streams the lines of a reply that runs elsewhere. */
|
|
141
|
+
#followOffered = false;
|
|
136
142
|
/** The last view said results wait that no reply has read: the chat wakes once it is idle. */
|
|
137
143
|
#wakeDue = false;
|
|
138
144
|
/** The ids a reply names the run's message by, once the host said it saved the message itself. */
|
|
@@ -183,10 +189,12 @@ class Chat {
|
|
|
183
189
|
}
|
|
184
190
|
this.#followPush();
|
|
185
191
|
this.#followHold();
|
|
192
|
+
this.#followReply();
|
|
186
193
|
return () => {
|
|
187
194
|
this.#listeners.delete(listener);
|
|
188
195
|
this.#followPush();
|
|
189
196
|
this.#followHold();
|
|
197
|
+
this.#followReply();
|
|
190
198
|
};
|
|
191
199
|
};
|
|
192
200
|
refresh = () => {
|
|
@@ -271,6 +279,86 @@ class Chat {
|
|
|
271
279
|
void this.refresh();
|
|
272
280
|
}, Math.max(0, running.heldUntil - Date.now()));
|
|
273
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Draws the reply that runs elsewhere from the host's lines, while someone is here to see it:
|
|
284
|
+
* the same reducer the page that started it uses. It stays `runningElsewhere`, so stop and steer
|
|
285
|
+
* go by the conversation. The host ends the lines with the reply, and the page reads the saved
|
|
286
|
+
* conversation then.
|
|
287
|
+
*/
|
|
288
|
+
#followReply() {
|
|
289
|
+
const running = this.#state.runningElsewhere;
|
|
290
|
+
const { iface, transport } = this.#options;
|
|
291
|
+
const wanted = running !== null &&
|
|
292
|
+
this.#followOffered &&
|
|
293
|
+
this.#listeners.size > 0 &&
|
|
294
|
+
!this.#busy &&
|
|
295
|
+
iface !== undefined &&
|
|
296
|
+
transport.follow !== undefined;
|
|
297
|
+
const current = this.#following;
|
|
298
|
+
if (current && (!wanted || current.since !== running.since)) {
|
|
299
|
+
this.#following = null;
|
|
300
|
+
current.abort.abort();
|
|
301
|
+
this.#endFollow();
|
|
302
|
+
}
|
|
303
|
+
if (!wanted || this.#following || this.#followedSince === running.since)
|
|
304
|
+
return;
|
|
305
|
+
const ref = this.#conversation;
|
|
306
|
+
if (ref === undefined)
|
|
307
|
+
return;
|
|
308
|
+
const following = {
|
|
309
|
+
since: running.since,
|
|
310
|
+
abort: new AbortController(),
|
|
311
|
+
prompt: running.prompt,
|
|
312
|
+
partial: [],
|
|
313
|
+
};
|
|
314
|
+
this.#following = following;
|
|
315
|
+
void this.#streamFollowed(following, ref);
|
|
316
|
+
}
|
|
317
|
+
async #streamFollowed(following, ref) {
|
|
318
|
+
const { iface, transport } = this.#options;
|
|
319
|
+
if (!iface || !transport.follow)
|
|
320
|
+
return;
|
|
321
|
+
const follow = transport.follow.bind(transport);
|
|
322
|
+
let lines = 0;
|
|
323
|
+
try {
|
|
324
|
+
await streamFoldedTurn({
|
|
325
|
+
iface,
|
|
326
|
+
events: [],
|
|
327
|
+
view: {
|
|
328
|
+
blocks: (partial) => {
|
|
329
|
+
if (this.#following !== following)
|
|
330
|
+
return;
|
|
331
|
+
lines += 1;
|
|
332
|
+
this.#followedSince = following.since;
|
|
333
|
+
following.partial = partial;
|
|
334
|
+
if (!this.#state.streaming)
|
|
335
|
+
this.#set({ streaming: true });
|
|
336
|
+
this.#streamFrame([...following.prompt, ...partial]);
|
|
337
|
+
},
|
|
338
|
+
skipped: (error) => this.#set({ failure: clientFailure(error, iface.labels) }),
|
|
339
|
+
},
|
|
340
|
+
stream: (onLine) => follow({ conversation: ref }, onLine, following.abort.signal),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
// why: A cut follow leaves the page as the last read drew it; the next read shows where the reply stands.
|
|
345
|
+
}
|
|
346
|
+
if (this.#following !== following)
|
|
347
|
+
return;
|
|
348
|
+
this.#following = null;
|
|
349
|
+
this.#endFollow();
|
|
350
|
+
// why: A host with no reply to follow yet (it began a moment later) says nothing has changed; its push says when.
|
|
351
|
+
if (lines > 0 && !following.abort.signal.aborted)
|
|
352
|
+
void this.refresh();
|
|
353
|
+
}
|
|
354
|
+
/** Nothing follows: the frame in flight is dropped or drawn, and the reply no longer shows as streaming. */
|
|
355
|
+
#endFollow() {
|
|
356
|
+
this.#frame?.();
|
|
357
|
+
this.#frame = null;
|
|
358
|
+
this.#frameBlocks = null;
|
|
359
|
+
if (this.#state.streaming)
|
|
360
|
+
this.#set({ streaming: false });
|
|
361
|
+
}
|
|
274
362
|
async #listen(listening) {
|
|
275
363
|
const { signal } = listening;
|
|
276
364
|
while (!signal.aborted) {
|
|
@@ -679,19 +767,27 @@ class Chat {
|
|
|
679
767
|
const after = view.paused?.blocks ?? view.running?.prompt ?? [];
|
|
680
768
|
const drawn = [...view.transcript, ...after].map((block) => block.id);
|
|
681
769
|
const ended = this.#state.runningElsewhere !== null && view.running === undefined;
|
|
770
|
+
if (this.#following && view.running?.since === this.#following.since) {
|
|
771
|
+
this.#following.prompt = view.running.prompt;
|
|
772
|
+
}
|
|
682
773
|
this.#set({
|
|
683
774
|
// why: A saved message the conversation now shows is no longer waiting.
|
|
684
775
|
pendingMessages: takeSavedComposerPending(this.#state.pendingMessages, drawn).remaining,
|
|
685
776
|
blocks,
|
|
686
|
-
|
|
777
|
+
// why: A reply this page follows draws from the host's lines, over the prompt as the read now shows it.
|
|
778
|
+
streamBlocks: this.#following && view.running?.since === this.#following.since
|
|
779
|
+
? [...view.running.prompt, ...this.#following.partial]
|
|
780
|
+
: keepIds(shown.slice(blocks.length), after),
|
|
687
781
|
// why: A reply at rest leaves the run's own events (tokens, an ask); a paused one, the host's.
|
|
688
782
|
session: this.#sessionAfter(session, view.paused?.events ?? events),
|
|
689
783
|
chatStarted: view.transcript.length > 0 || after.length > 0 || view.running !== undefined,
|
|
690
784
|
runningElsewhere: view.running ?? null,
|
|
691
785
|
});
|
|
692
786
|
this.#wakeDue = view.wake === true;
|
|
787
|
+
this.#followOffered = view.follow === true;
|
|
693
788
|
this.#setPush(view.notify);
|
|
694
789
|
this.#followHold();
|
|
790
|
+
this.#followReply();
|
|
695
791
|
// why: The reply that ran elsewhere ended as a reply here ends: the queue sends on, unless it was stopped.
|
|
696
792
|
if (ended && !this.#busy) {
|
|
697
793
|
this.#runEnded(!this.#stopped);
|
package/client/index.d.ts
CHANGED
|
@@ -33,5 +33,5 @@ export { hasLead, layoutResult, type ResultChart, type ResultFigure, type Result
|
|
|
33
33
|
export type { AnsweringGate, ToolDecisionAction, ToolGateResolution, } from './tool-resume.ts';
|
|
34
34
|
export { continueGatedToolInvocation, type PersonGateResolution } from './tool-resume.ts';
|
|
35
35
|
export { createTraceFeed, recordedFeed, type TraceFeed } from './trace-feed.ts';
|
|
36
|
-
export { CHAT_LINES, type ChatLine, type ChatLineSink, type ClientTurnEvent, type ConversationLine, type ConversationView, createHttpTransport, fetchConversation, fetchRewound, fetchStarted, type HostErrorBody, type HttpOptions, type HttpTransportOptions, hostError, isTheoremStreamError, type MalformedEvent, type NotifyLine, postJson, postNdjson, type RunningReply, readNdjsonStream, type StatedTurn, type TheoremConversationRequest, type TheoremInvokeRequest, type TheoremReplay, type TheoremRewindRequest, type TheoremRewound, type TheoremSavedMessage, type TheoremStartRequest, type TheoremSteerReply, type TheoremSteerRequest, type TheoremStopRequest, TheoremStreamError, type TheoremTransport, type TheoremTurnInput, type TheoremTurnRequest, type TheoremWakeRequest, type TurnEventSink, type UnsupportedEvent, type WireLines, } from './transport.ts';
|
|
36
|
+
export { CHAT_LINES, type ChatLine, type ChatLineSink, type ClientTurnEvent, type ConversationLine, type ConversationView, createHttpTransport, fetchConversation, fetchRewound, fetchStarted, type HostErrorBody, type HttpOptions, type HttpTransportOptions, hostError, isTheoremStreamError, type MalformedEvent, type NotifyLine, postJson, postNdjson, type RunningReply, readNdjsonStream, type StatedTurn, type TheoremConversationRequest, type TheoremFollowRequest, type TheoremInvokeRequest, type TheoremReplay, type TheoremRewindRequest, type TheoremRewound, type TheoremSavedMessage, type TheoremStartRequest, type TheoremSteerReply, type TheoremSteerRequest, type TheoremStopRequest, TheoremStreamError, type TheoremTransport, type TheoremTurnInput, type TheoremTurnRequest, type TheoremWakeRequest, type TurnEventSink, type UnsupportedEvent, type WireLines, } from './transport.ts';
|
|
37
37
|
export { buildInvokeRequest, buildTurnRequest, foldAssistantTurn, issuedOperationId, type RunScope, type WalkAway, } from './turn-client.ts';
|
package/client/transport.d.ts
CHANGED
|
@@ -72,7 +72,7 @@ export type TheoremTurnRequest = {
|
|
|
72
72
|
effort?: string;
|
|
73
73
|
/** For a host that lets its page choose (`pageStatesTurn`); any other host refuses it. */
|
|
74
74
|
stated?: StatedTurn;
|
|
75
|
-
/** Client-generated id so
|
|
75
|
+
/** Client-generated id so a steer or stop sent while the reply goes on can find this reply. */
|
|
76
76
|
turnId?: string;
|
|
77
77
|
/**
|
|
78
78
|
* The client's id for this message. A message keeps its id when the page sends it again, so a
|
|
@@ -119,7 +119,7 @@ export type TheoremInvokeRequest = {
|
|
|
119
119
|
page?: PagePart;
|
|
120
120
|
/** The ref of the conversation whose reply waits on the gate. */
|
|
121
121
|
conversation?: string;
|
|
122
|
-
/** Client-generated id so
|
|
122
|
+
/** Client-generated id so a steer or stop sent while the reply goes on can find this reply. */
|
|
123
123
|
turnId?: string;
|
|
124
124
|
replay?: TheoremReplay;
|
|
125
125
|
};
|
|
@@ -155,8 +155,9 @@ export type TheoremSavedMessage = {
|
|
|
155
155
|
landsAs: string[];
|
|
156
156
|
};
|
|
157
157
|
/**
|
|
158
|
-
* What the host did with a steer. With `saved`, the host keeps the message
|
|
159
|
-
*
|
|
158
|
+
* What the host did with a steer. With `saved`, the host keeps the message, and the page does not
|
|
159
|
+
* send it again. Without it, the steer is in the memory of a run that keeps no conversation, and the
|
|
160
|
+
* page sends it again when the run ends without it.
|
|
160
161
|
*/
|
|
161
162
|
export type TheoremSteerReply = {
|
|
162
163
|
saved?: TheoremSavedMessage;
|
|
@@ -219,6 +220,8 @@ export type ConversationView = {
|
|
|
219
220
|
notify?: {
|
|
220
221
|
retryMs?: number;
|
|
221
222
|
};
|
|
223
|
+
/** The host streams the lines of a reply that runs elsewhere (`transport.follow`). */
|
|
224
|
+
follow?: true;
|
|
222
225
|
/** Results wait that no reply has read: the page wakes the conversation (`transport.wake`). */
|
|
223
226
|
wake?: true;
|
|
224
227
|
};
|
|
@@ -228,6 +231,12 @@ export type TheoremConversationRequest = {
|
|
|
228
231
|
};
|
|
229
232
|
/** A `/conversation` body as `createTheoremHandler` reads it. */
|
|
230
233
|
export declare const theoremConversationRequestSchema: z.ZodType<TheoremConversationRequest>;
|
|
234
|
+
/** Follow the reply that runs on a conversation now: a `/follow` body. */
|
|
235
|
+
export type TheoremFollowRequest = {
|
|
236
|
+
conversation: string;
|
|
237
|
+
};
|
|
238
|
+
/** A `/follow` body as `createTheoremHandler` reads it. */
|
|
239
|
+
export declare const theoremFollowRequestSchema: z.ZodType<TheoremFollowRequest>;
|
|
231
240
|
/**
|
|
232
241
|
* Wake the conversation: the model takes up the results of its background calls that ended and it
|
|
233
242
|
* has not read, in a reply to no message. A `/wake` body. The host does nothing when no result
|
|
@@ -291,6 +300,11 @@ export interface TheoremTransport {
|
|
|
291
300
|
conversation(request: TheoremConversationRequest, signal?: AbortSignal): Promise<ConversationView>;
|
|
292
301
|
/** Calls `changed` for each line of the host's push, and settles when the push ends. */
|
|
293
302
|
notify?(changed: () => void, signal: AbortSignal): Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* Streams the lines of the reply that runs on the conversation now, whoever started it, from its
|
|
305
|
+
* first line until it ends. No line at all when none runs.
|
|
306
|
+
*/
|
|
307
|
+
follow?(request: TheoremFollowRequest, onLine: ChatLineSink, signal?: AbortSignal): Promise<void>;
|
|
294
308
|
/** Streams the reply a wake runs; no line at all when the host had nothing to wake for. */
|
|
295
309
|
wake?(request: TheoremWakeRequest, onLine: ChatLineSink, signal?: AbortSignal): Promise<void>;
|
|
296
310
|
/** The conversation cut after a transcript block, under the ref the host issued for it. */
|
package/client/transport.js
CHANGED
|
@@ -137,6 +137,7 @@ const conversationView = z.object({
|
|
|
137
137
|
.optional(),
|
|
138
138
|
running: runningReply.optional(),
|
|
139
139
|
notify: z.object({ retryMs: z.number().optional() }).optional(),
|
|
140
|
+
follow: z.literal(true).optional(),
|
|
140
141
|
wake: z.literal(true).optional(),
|
|
141
142
|
});
|
|
142
143
|
true;
|
|
@@ -144,6 +145,10 @@ const theoremConversationRequest = z.object({ conversation: z.string().min(1) })
|
|
|
144
145
|
true;
|
|
145
146
|
/** A `/conversation` body as `createTheoremHandler` reads it. */
|
|
146
147
|
export const theoremConversationRequestSchema = theoremConversationRequest;
|
|
148
|
+
const theoremFollowRequest = z.object({ conversation: z.string().min(1) });
|
|
149
|
+
true;
|
|
150
|
+
/** A `/follow` body as `createTheoremHandler` reads it. */
|
|
151
|
+
export const theoremFollowRequestSchema = theoremFollowRequest;
|
|
147
152
|
const theoremWakeRequest = z.object({
|
|
148
153
|
conversation: z.string().min(1),
|
|
149
154
|
input: theoremTurnInput.pick({ options: true, context: true }).optional(),
|
|
@@ -398,6 +403,7 @@ export function createHttpTransport(options = {}) {
|
|
|
398
403
|
invoke: ({ replay: _replay, ...body }, onEvent, signal) => postNdjson(`${base}/invoke`, body, CHAT_LINES, onEvent, { ...options, signal }),
|
|
399
404
|
conversation: (body, signal) => fetchConversation(`${base}/conversation`, body, { ...options, signal }),
|
|
400
405
|
wake: (body, onLine, signal) => postNdjson(`${base}/wake`, body, CHAT_LINES, onLine, { ...options, signal }),
|
|
406
|
+
follow: (body, onLine, signal) => postNdjson(`${base}/follow`, body, CHAT_LINES, onLine, { ...options, signal }),
|
|
401
407
|
notify: (changed, signal) => postNdjson(`${base}/notify`, {}, NOTIFY_LINES, (line) => {
|
|
402
408
|
if (line.type === 'changed')
|
|
403
409
|
changed();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theoremjs/web",
|
|
3
|
-
"version": "0.3.5-next.
|
|
3
|
+
"version": "0.3.5-next.1014.g14db6b7",
|
|
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.
|
|
28
|
+
"@theoremjs/agents": "^0.3.5-next.1014.g14db6b7"
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -5,15 +5,16 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
|
-
import { type GateReference, type NormalizedTurnRequest, type Profile, type ProfileDefinition, type ProviderHostOptions,
|
|
8
|
+
import { type GateReference, type NormalizedTurnRequest, type Profile, type ProfileDefinition, type ProviderHostOptions, type StageHandler, type TurnHistoryMessage, type TurnInput, type TurnRequest } from '@theoremjs/agents';
|
|
9
9
|
import { type ConversationInbox, type InboxItem, type InboxPolicy, type ReplyRun } from '@theoremjs/agents/conversation';
|
|
10
10
|
import type { ClientTurnOptions } from '@theoremjs/agents/host';
|
|
11
11
|
import { type TranscriptBlock } from '@theoremjs/agents/interface';
|
|
12
12
|
import type { ConversationView, StatedTurn, TheoremInvokeRequest, TheoremRewound, TheoremSteerRequest, TheoremStopRequest, TheoremTurnInput } from '../client/transport.ts';
|
|
13
13
|
import { type ReplyLine, type ReplyToSave, type ShapedReply } from './conversation.ts';
|
|
14
14
|
import { type ReplyEnd, type TheoremConversationStore } from './conversation-store.ts';
|
|
15
|
+
import { type OwnInbox, type RunInbox } from './own-inbox.ts';
|
|
16
|
+
import type { TheoremReplyFeed } from './reply-feed.ts';
|
|
15
17
|
import { type CallsContext, type CoreContext } from './session-calls.ts';
|
|
16
|
-
import { type SteerInbox } from './steer-inbox.ts';
|
|
17
18
|
import { type ReplyRefusal } from './turn-wiring.ts';
|
|
18
19
|
export type ChatProfile = Exclude<Profile, {
|
|
19
20
|
type: 'live' | 'decision' | 'host' | 'embedding';
|
|
@@ -51,7 +52,7 @@ export type AppInbox<Item extends InboxItem = InboxItem> = {
|
|
|
51
52
|
leaseMs: number;
|
|
52
53
|
/**
|
|
53
54
|
* The app's stop for a reply under a hold, kept where every server of the app reads it. Set,
|
|
54
|
-
* `stop` asks it
|
|
55
|
+
* `stop` asks it. Unset, Theorem's own stop of this process serves the conversation.
|
|
55
56
|
*/
|
|
56
57
|
stop?: HoldStop;
|
|
57
58
|
};
|
|
@@ -133,15 +134,19 @@ export type ReplyWiring<Item extends InboxItem = InboxItem> = SaveHooks<Item> &
|
|
|
133
134
|
export declare function oneInbox(options: {
|
|
134
135
|
inbox?: unknown;
|
|
135
136
|
lateResultsDecider?: unknown;
|
|
137
|
+
replyFeed?: unknown;
|
|
138
|
+
notifier?: unknown;
|
|
136
139
|
}): void;
|
|
137
140
|
/** Everything a conversation's runs read from whoever serves them. */
|
|
138
141
|
export type CoreState<Item extends InboxItem = InboxItem> = Omit<CallsContext, 'options'> & {
|
|
139
142
|
chat: ChatContext;
|
|
140
|
-
|
|
143
|
+
own: OwnInbox;
|
|
141
144
|
options: Omit<CallsContext['options'], 'prepare'> & ReplyWiring<Item> & {
|
|
142
145
|
provider: (at: CoreContext, model?: string) => ProviderHostOptions | Promise<ProviderHostOptions>;
|
|
143
146
|
conversationLeaseMs?: number;
|
|
144
147
|
notifyRetryMs?: number;
|
|
148
|
+
/** Where a running reply's lines go for the pages that follow it. Needs `notifier`. */
|
|
149
|
+
replyFeed?: TheoremReplyFeed;
|
|
145
150
|
clientEvents?: ClientTurnOptions;
|
|
146
151
|
};
|
|
147
152
|
};
|
|
@@ -175,12 +180,6 @@ export type Generation = {
|
|
|
175
180
|
turnId?: string;
|
|
176
181
|
stated?: StatedTurn;
|
|
177
182
|
};
|
|
178
|
-
/** Steer inboxes are scoped to the session, so a turn id alone can't reach another user's turn. */
|
|
179
|
-
/**
|
|
180
|
-
* The refusal of a stop or steer by conversation on a host that can't reach a reply it does not
|
|
181
|
-
* run: no app stop, or no intake for a steer. Not `turn_ended`: the reply may still be running.
|
|
182
|
-
*/
|
|
183
|
-
export declare function replyUnreachable(): TheoremError;
|
|
184
183
|
/**
|
|
185
184
|
* What the caller of a run that ended `continue` does with its hold. `here`: runs the follow-up
|
|
186
185
|
* in the same stream. `taken`: the app runs it elsewhere, by a call of its own to `followUp`.
|
|
@@ -195,6 +194,27 @@ export declare function followedUp(core: Pick<ConversationCore, 'followUp' | 're
|
|
|
195
194
|
ref: string;
|
|
196
195
|
hold: string;
|
|
197
196
|
}) => ContinueChoice | Promise<ContinueChoice>): Reply;
|
|
197
|
+
/** What a reply that runs under a hold of a run's own inbox reads. */
|
|
198
|
+
export type NamedHold = {
|
|
199
|
+
/** Lands the person's messages at the stages where the model takes them. */
|
|
200
|
+
stage: StageHandler;
|
|
201
|
+
/** Aborts when the run is stopped, or its hold loses it. */
|
|
202
|
+
signal: AbortSignal;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Runs `lines` under a hold of the run's own inbox, opened with `openRun`. The reply takes each
|
|
206
|
+
* message `steerRun` puts in, one at each step, and a stop asked of the run's name (the stop of
|
|
207
|
+
* `own`) aborts `NamedHold.signal`. A message that waits when the run ends is not told.
|
|
208
|
+
*/
|
|
209
|
+
export declare function namedRun(args: {
|
|
210
|
+
own: OwnInbox;
|
|
211
|
+
scope: string;
|
|
212
|
+
name: string;
|
|
213
|
+
run: RunInbox;
|
|
214
|
+
leaseMs: number;
|
|
215
|
+
signal: AbortSignal;
|
|
216
|
+
onError?: (err: unknown) => void;
|
|
217
|
+
}, lines: (hold: NamedHold) => AsyncIterable<ReplyLine>): Reply;
|
|
198
218
|
/** The answered call's events, for a session that keeps no conversation. */
|
|
199
219
|
export declare function unconversedAnswer(ctx: CallsContext, at: CoreContext, body: TheoremInvokeRequest): Promise<Reply>;
|
|
200
220
|
/** A person's message, for {@link ConversationCore.message}. */
|
|
@@ -263,18 +283,29 @@ export type ConversationCore = {
|
|
|
263
283
|
at: number;
|
|
264
284
|
}): Promise<TheoremRewound>;
|
|
265
285
|
/**
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
286
|
+
* The lines of the reply that runs on the conversation now, whoever started it: from its first
|
|
287
|
+
* line, then each as it comes, until it ends or `at.signal` aborts. Ends at once when no reply
|
|
288
|
+
* runs. Throws `config` without `replyFeed`.
|
|
269
289
|
*/
|
|
270
|
-
|
|
290
|
+
follow(at: CoreContext, args: {
|
|
291
|
+
ref: string;
|
|
292
|
+
}): Promise<AsyncGenerator<ReplyLine>>;
|
|
293
|
+
/**
|
|
294
|
+
* Puts a person's message into the reply that is running, as the one user message the model
|
|
295
|
+
* reads: the reply takes it at its next step, or at its end. `ref` names a chat that keeps a
|
|
296
|
+
* conversation, `turnId` the run of one that keeps none. Throws what `message` throws for an
|
|
297
|
+
* input the profile does not take, and puts nothing in. False when no such reply runs. Not with
|
|
298
|
+
* an app `inbox`: the app saves the message through its own intake.
|
|
299
|
+
*/
|
|
300
|
+
steer(at: CoreContext, args: ({
|
|
301
|
+
ref: string;
|
|
302
|
+
} | {
|
|
271
303
|
turnId: string;
|
|
272
|
-
} & Pick<TheoremSteerRequest, 'id' | 'input'>): Promise<boolean>;
|
|
304
|
+
}) & Pick<TheoremSteerRequest, 'id' | 'input'>): Promise<boolean>;
|
|
273
305
|
/**
|
|
274
|
-
* Stops
|
|
275
|
-
* `
|
|
276
|
-
* the reply that holds the conversation, and reads neither.
|
|
277
|
-
* server runs can be stopped, by its `turnId`.
|
|
306
|
+
* Stops the reply that holds the conversation `conversation` names, or the run of a chat that
|
|
307
|
+
* keeps none, `turnId`. False when it is not running. Where the app's `inbox` has a `stop`, it
|
|
308
|
+
* stops the reply that holds the app's conversation, and reads neither.
|
|
278
309
|
*/
|
|
279
310
|
stop(at: CoreContext, args: TheoremStopRequest): Promise<boolean>;
|
|
280
311
|
};
|