@theoremjs/web 0.3.5-next.1018.g6c48978 → 0.3.5-next.1020.g3a48861
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 +5 -3
- package/package.json +2 -2
- package/server/conversation-core.d.ts +7 -1
- package/server/conversation-core.js +9 -4
- package/server/handler.d.ts +13 -12
- package/server/handler.js +15 -21
package/README.md
CHANGED
|
@@ -284,7 +284,7 @@ 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
|
-
| `core` | A `ConversationCore` your app built (`createTheoremConversationCore`). The handler runs each chat call on it, and takes no `prepare`, `inbox`, `onStage`, `beforeSave`, `afterSave`, `compact`, `lateResultsDecider`, `background`, `replyFeed`, `authorizeToolReplay` or `authorizationUrl` beside it: the core has them. It does take `intake` and `onContinue`. |
|
|
287
|
+
| `core` | A `ConversationCore` your app built (`createTheoremConversationCore`). The handler runs each chat call on it, `/notify` and `/follow` too, and takes no `prepare`, `inbox`, `onStage`, `beforeSave`, `afterSave`, `compact`, `lateResultsDecider`, `background`, `notifier`, `replyFeed`, `notifyRetryMs`, `authorizeToolReplay` or `authorizationUrl` beside it: the core has them. It does take `intake` and `onContinue`. |
|
|
288
288
|
| `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`. |
|
|
289
289
|
| `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. |
|
|
290
290
|
| `pageToolBroker` | Delivers results to active page dispatches. Default: bounded process memory. |
|
|
@@ -295,8 +295,8 @@ const transport = withPageToolDispatch(
|
|
|
295
295
|
| `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`. |
|
|
296
296
|
| `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. |
|
|
297
297
|
| `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). |
|
|
298
|
-
| `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). |
|
|
299
|
-
| `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
|
+
| `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. With `core`, give them to the core. See [the background section](#share-session-authority). |
|
|
299
|
+
| `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. With `core`, give it to the core. See [Follow a running reply](#follow-a-running-reply). |
|
|
300
300
|
| `credentialStore` | Where tool credentials are kept, by session. Default: process memory. |
|
|
301
301
|
| `authorizationUrl` | Returns the sign-in URL for an OAuth gate. Without it, an OAuth gate has no URL. |
|
|
302
302
|
| `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. |
|
|
@@ -561,6 +561,8 @@ async function received(sessionId: string, text: string, ref: string | undefined
|
|
|
561
561
|
| `rewind(at, { ref, at })` | Cuts the conversation after transcript block `at`. |
|
|
562
562
|
| `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. |
|
|
563
563
|
| `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`. |
|
|
564
|
+
| `follow(at, { ref })` | Returns the lines of the reply that runs on the conversation now, whoever started it, from its first line until it ends. Throws `not_found` without `replyFeed`. |
|
|
565
|
+
| `subscribe(at, changed)` | Calls `changed` for each `notifier.publish` under `at.sessionId`, and returns the function that stops it. The handler's `/notify` listens here, so a core that keeps its chats under another session than the page's still reaches the page. Throws `not_found` without `notifier`. |
|
|
564
566
|
|
|
565
567
|
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`:
|
|
566
568
|
|
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.1020.g3a48861",
|
|
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.1020.g3a48861"
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -285,11 +285,17 @@ export type ConversationCore = {
|
|
|
285
285
|
/**
|
|
286
286
|
* The lines of the reply that runs on the conversation now, whoever started it: from its first
|
|
287
287
|
* line, then each as it comes, until it ends or `at.signal` aborts. Ends at once when no reply
|
|
288
|
-
* runs. Throws `
|
|
288
|
+
* runs. Throws `not_found` without `replyFeed`.
|
|
289
289
|
*/
|
|
290
290
|
follow(at: CoreContext, args: {
|
|
291
291
|
ref: string;
|
|
292
292
|
}): Promise<AsyncGenerator<ReplyLine>>;
|
|
293
|
+
/**
|
|
294
|
+
* `TheoremNotifier.subscribe` under the session this core publishes under for `at`, so a page
|
|
295
|
+
* hears a core that keeps its chats under another session than the page's. Throws `not_found`
|
|
296
|
+
* without `notifier`.
|
|
297
|
+
*/
|
|
298
|
+
subscribe(at: CoreContext, changed: () => void): Promise<() => void>;
|
|
293
299
|
/**
|
|
294
300
|
* Puts a person's message into the reply that is running, as the one user message the model
|
|
295
301
|
* reads: the reply takes it at its next step, or at its end. `ref` names a chat that keeps a
|
|
@@ -399,10 +399,9 @@ async function* fed(ctx, at, conversation, lines) {
|
|
|
399
399
|
*/
|
|
400
400
|
async function follow(ctx, at, args) {
|
|
401
401
|
const feed = ctx.options.replyFeed;
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
throw new TheoremError('
|
|
405
|
-
}
|
|
402
|
+
// labels-exempt: internal diagnostic; the user reads error.not_found
|
|
403
|
+
if (!feed)
|
|
404
|
+
throw new TheoremError('not_found', 'following a reply needs `replyFeed`');
|
|
406
405
|
const host = conversationHost(ctx, at);
|
|
407
406
|
if (!host)
|
|
408
407
|
throw statelessRefusal('follow');
|
|
@@ -1006,6 +1005,12 @@ export function createConversationCore(state) {
|
|
|
1006
1005
|
read: (at, args) => read(state, at, args),
|
|
1007
1006
|
rewind: (at, args) => rewind(state, at, args),
|
|
1008
1007
|
follow: (at, args) => follow(state, at, args),
|
|
1008
|
+
subscribe: async (at, changed) => {
|
|
1009
|
+
// labels-exempt: internal diagnostic; the user reads error.not_found
|
|
1010
|
+
if (!state.options.notifier)
|
|
1011
|
+
throw new TheoremError('not_found', 'listening needs `notifier`');
|
|
1012
|
+
return await state.options.notifier.subscribe(at.sessionId, changed);
|
|
1013
|
+
},
|
|
1009
1014
|
steer: async (at, args) => {
|
|
1010
1015
|
if (state.options.inbox) {
|
|
1011
1016
|
// labels-exempt: builder config error; the user reads error.config
|
package/server/handler.d.ts
CHANGED
|
@@ -14,10 +14,10 @@ import { type PageToolBroker } from './page-dispatch.ts';
|
|
|
14
14
|
* - `POST <base>/page-result` → the page's result for a tool the turn ran in the page
|
|
15
15
|
* - `POST <base>/conversation` → the transcript and the paused reply, to draw
|
|
16
16
|
* - `POST <base>/notify` → NDJSON that stays open: one line now, and one for each `publish` under
|
|
17
|
-
*
|
|
18
|
-
* Set `notifier` to serve it
|
|
17
|
+
* the chat's session: a background call ended, or the app saved a reply.
|
|
18
|
+
* Set `notifier` or `core` to serve it
|
|
19
19
|
* - `POST <base>/follow` → NDJSON for the reply that runs on a conversation now, whoever started
|
|
20
|
-
* it: its lines from the first, until it ends. Set `replyFeed`
|
|
20
|
+
* it: its lines from the first, until it ends. Set `replyFeed` or `core`
|
|
21
21
|
* - `POST <base>/rewind` → `{ conversation, view }` the conversation cut after one block, and its new ref
|
|
22
22
|
* - `POST <base>/wake` → NDJSON for a reply to no message: the model takes up the results of
|
|
23
23
|
* background calls that ended since it last ran. Empty when none waits
|
|
@@ -180,15 +180,16 @@ export type TheoremHandlerOptions<Item extends InboxItem = InboxItem> = ReplyWir
|
|
|
180
180
|
*/
|
|
181
181
|
conversationLeaseMs?: number;
|
|
182
182
|
/**
|
|
183
|
-
* The app's own conversation core, which runs every reply, read, rewind, stop
|
|
184
|
-
* asks for, in place of the core the handler builds. The page's request
|
|
185
|
-
* `CoreContext`: the session's id, the request's signal, and `host`. The core
|
|
186
|
-
* profile, provider, inbox, stores, notifier and save steps, so none
|
|
187
|
-
* `prepare`, `onStage`, `inbox`, `beforeSave`, `afterSave`, `compact`,
|
|
188
|
-
* `background`, `
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
183
|
+
* The app's own conversation core, which runs every reply, read, rewind, stop, steer, `/notify`
|
|
184
|
+
* and `/follow` the page asks for, in place of the core the handler builds. The page's request
|
|
185
|
+
* reaches it as a `CoreContext`: the session's id, the request's signal, and `host`. The core
|
|
186
|
+
* brings its own profile, provider, inbox, stores, notifier, reply feed and save steps, so none
|
|
187
|
+
* of these is read here: `prepare`, `onStage`, `inbox`, `beforeSave`, `afterSave`, `compact`,
|
|
188
|
+
* `lateResultsDecider`, `background`, `notifier`, `replyFeed`, `notifyRetryMs`,
|
|
189
|
+
* `authorizeToolReplay`, `authorizationUrl`. The handler refuses one set beside it. `intake` and
|
|
190
|
+
* `onContinue` still run here, and `intake` then needs no `inbox`: the core's own is the one it
|
|
191
|
+
* saves into. `conversationStore` is still required for a profile that keeps a conversation, and
|
|
192
|
+
* the handler reads no conversation from it.
|
|
192
193
|
*/
|
|
193
194
|
core?: ConversationCore;
|
|
194
195
|
/**
|
package/server/handler.js
CHANGED
|
@@ -14,10 +14,10 @@ import { pageToolResultSchema, sessionPageDispatch } from "./page-dispatch.js";
|
|
|
14
14
|
* - `POST <base>/page-result` → the page's result for a tool the turn ran in the page
|
|
15
15
|
* - `POST <base>/conversation` → the transcript and the paused reply, to draw
|
|
16
16
|
* - `POST <base>/notify` → NDJSON that stays open: one line now, and one for each `publish` under
|
|
17
|
-
*
|
|
18
|
-
* Set `notifier` to serve it
|
|
17
|
+
* the chat's session: a background call ended, or the app saved a reply.
|
|
18
|
+
* Set `notifier` or `core` to serve it
|
|
19
19
|
* - `POST <base>/follow` → NDJSON for the reply that runs on a conversation now, whoever started
|
|
20
|
-
* it: its lines from the first, until it ends. Set `replyFeed`
|
|
20
|
+
* it: its lines from the first, until it ends. Set `replyFeed` or `core`
|
|
21
21
|
* - `POST <base>/rewind` → `{ conversation, view }` the conversation cut after one block, and its new ref
|
|
22
22
|
* - `POST <base>/wake` → NDJSON for a reply to no message: the model takes up the results of
|
|
23
23
|
* background calls that ended since it last ran. Empty when none waits
|
|
@@ -245,7 +245,9 @@ const CORE_OWNED = [
|
|
|
245
245
|
'compact',
|
|
246
246
|
'lateResultsDecider',
|
|
247
247
|
'background',
|
|
248
|
+
'notifier',
|
|
248
249
|
'replyFeed',
|
|
250
|
+
'notifyRetryMs',
|
|
249
251
|
'authorizeToolReplay',
|
|
250
252
|
'authorizationUrl',
|
|
251
253
|
];
|
|
@@ -421,16 +423,16 @@ const CHANGED_LINE = new TextEncoder().encode('{"type":"changed"}\n');
|
|
|
421
423
|
* invariant: The first line follows the subscribe, so a call that ends before the page's first
|
|
422
424
|
* read is never missed.
|
|
423
425
|
*/
|
|
424
|
-
async function notify(ctx, request, session
|
|
426
|
+
async function notify(ctx, chat, request, session) {
|
|
425
427
|
await readBody(request, theoremNotifyRequestSchema, ctx.options.maxRequestBytes);
|
|
426
428
|
let send = () => { };
|
|
427
|
-
const
|
|
429
|
+
const at = contextOf(ctx, request, session.id);
|
|
430
|
+
const unsubscribe = await coreOf(ctx, chat, request).subscribe(at, () => send());
|
|
428
431
|
let open = true;
|
|
429
432
|
const end = () => {
|
|
430
|
-
if (
|
|
431
|
-
|
|
433
|
+
if (open)
|
|
434
|
+
unsubscribe();
|
|
432
435
|
open = false;
|
|
433
|
-
unsubscribe();
|
|
434
436
|
};
|
|
435
437
|
const stream = new ReadableStream({
|
|
436
438
|
start(controller) {
|
|
@@ -440,10 +442,9 @@ async function notify(ctx, request, session, notifier) {
|
|
|
440
442
|
};
|
|
441
443
|
send();
|
|
442
444
|
const leave = () => {
|
|
443
|
-
if (
|
|
444
|
-
|
|
445
|
+
if (open)
|
|
446
|
+
controller.close();
|
|
445
447
|
end();
|
|
446
|
-
controller.close();
|
|
447
448
|
};
|
|
448
449
|
if (request.signal.aborted)
|
|
449
450
|
leave();
|
|
@@ -580,17 +581,10 @@ async function route(ctx, request, session) {
|
|
|
580
581
|
return invoke(ctx, request, session);
|
|
581
582
|
if (target === 'conversation')
|
|
582
583
|
return conversation(ctx, chat, request, session);
|
|
583
|
-
if (target === 'notify')
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
return refused(ctx);
|
|
587
|
-
return notify(ctx, request, session, notifier);
|
|
588
|
-
}
|
|
589
|
-
if (target === 'follow') {
|
|
590
|
-
if (!ctx.options.replyFeed || !chat.conversations)
|
|
591
|
-
return refused(ctx);
|
|
584
|
+
if (target === 'notify')
|
|
585
|
+
return notify(ctx, chat, request, session);
|
|
586
|
+
if (target === 'follow')
|
|
592
587
|
return follow(ctx, chat, request, session);
|
|
593
|
-
}
|
|
594
588
|
if (target === 'rewind')
|
|
595
589
|
return rewind(ctx, chat, request, session);
|
|
596
590
|
if (target === 'wake')
|