@theoremjs/web 0.3.5-next.1013.g9b838d0 → 0.3.5-next.1016.gc4ded01
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 +21 -8
- package/client/transport.d.ts +5 -4
- package/package.json +2 -2
- package/server/conversation-core.d.ts +37 -19
- package/server/conversation-core.js +278 -129
- 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 +2 -2
- package/server/handler.d.ts +2 -5
- package/server/handler.js +19 -11
- package/server/mod.d.ts +2 -2
- package/server/mod.js +2 -2
- package/server/own-inbox.d.ts +83 -0
- package/server/own-inbox.js +175 -0
- package/server/steer-inbox.d.ts +0 -41
- package/server/steer-inbox.js +0 -84
package/README.md
CHANGED
|
@@ -284,12 +284,12 @@ 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. |
|
|
@@ -558,10 +558,10 @@ async function received(sessionId: string, text: string, ref: string | undefined
|
|
|
558
558
|
| `release(at, { ref, hold })` | Lets the conversation go under that hold, with no follow-up. |
|
|
559
559
|
| `read(at, { ref })` | Returns the conversation as a page draws it. |
|
|
560
560
|
| `rewind(at, { ref, at })` | Cuts the conversation after transcript block `at`. |
|
|
561
|
-
| `steer(at, { turnId, id, input })` | Puts `input` into
|
|
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,
|
|
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`. |
|
|
563
563
|
|
|
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`,
|
|
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`:
|
|
565
565
|
|
|
566
566
|
| Option | Use |
|
|
567
567
|
| --- | --- |
|
|
@@ -648,9 +648,22 @@ A reply that opens with items from your inbox sends a `conversation` line with `
|
|
|
648
648
|
|
|
649
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.
|
|
650
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
|
+
|
|
651
664
|
### Stop a reply from any server
|
|
652
665
|
|
|
653
|
-
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.
|
|
654
667
|
|
|
655
668
|
```ts
|
|
656
669
|
const stop: HoldStop = {
|
|
@@ -685,8 +698,8 @@ const snapshot: ConversationSnapshot = {
|
|
|
685
698
|
|
|
686
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:
|
|
687
700
|
|
|
688
|
-
- The stop button sends `/stop` with `{ conversation }`. Your `inbox`'s `stop` stops the reply. Without one,
|
|
689
|
-
- 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.
|
|
690
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.
|
|
691
704
|
|
|
692
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`.
|
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;
|
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.1016.gc4ded01",
|
|
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.1016.gc4ded01"
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -5,16 +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';
|
|
15
16
|
import type { TheoremReplyFeed } from './reply-feed.ts';
|
|
16
17
|
import { type CallsContext, type CoreContext } from './session-calls.ts';
|
|
17
|
-
import { type SteerInbox } from './steer-inbox.ts';
|
|
18
18
|
import { type ReplyRefusal } from './turn-wiring.ts';
|
|
19
19
|
export type ChatProfile = Exclude<Profile, {
|
|
20
20
|
type: 'live' | 'decision' | 'host' | 'embedding';
|
|
@@ -52,7 +52,7 @@ export type AppInbox<Item extends InboxItem = InboxItem> = {
|
|
|
52
52
|
leaseMs: number;
|
|
53
53
|
/**
|
|
54
54
|
* The app's stop for a reply under a hold, kept where every server of the app reads it. Set,
|
|
55
|
-
* `stop` asks it
|
|
55
|
+
* `stop` asks it. Unset, Theorem's own stop of this process serves the conversation.
|
|
56
56
|
*/
|
|
57
57
|
stop?: HoldStop;
|
|
58
58
|
};
|
|
@@ -140,7 +140,7 @@ export declare function oneInbox(options: {
|
|
|
140
140
|
/** Everything a conversation's runs read from whoever serves them. */
|
|
141
141
|
export type CoreState<Item extends InboxItem = InboxItem> = Omit<CallsContext, 'options'> & {
|
|
142
142
|
chat: ChatContext;
|
|
143
|
-
|
|
143
|
+
own: OwnInbox;
|
|
144
144
|
options: Omit<CallsContext['options'], 'prepare'> & ReplyWiring<Item> & {
|
|
145
145
|
provider: (at: CoreContext, model?: string) => ProviderHostOptions | Promise<ProviderHostOptions>;
|
|
146
146
|
conversationLeaseMs?: number;
|
|
@@ -180,12 +180,6 @@ export type Generation = {
|
|
|
180
180
|
turnId?: string;
|
|
181
181
|
stated?: StatedTurn;
|
|
182
182
|
};
|
|
183
|
-
/** Steer inboxes are scoped to the session, so a turn id alone can't reach another user's turn. */
|
|
184
|
-
/**
|
|
185
|
-
* The refusal of a stop or steer by conversation on a host that can't reach a reply it does not
|
|
186
|
-
* run: no app stop, or no intake for a steer. Not `turn_ended`: the reply may still be running.
|
|
187
|
-
*/
|
|
188
|
-
export declare function replyUnreachable(): TheoremError;
|
|
189
183
|
/**
|
|
190
184
|
* What the caller of a run that ended `continue` does with its hold. `here`: runs the follow-up
|
|
191
185
|
* in the same stream. `taken`: the app runs it elsewhere, by a call of its own to `followUp`.
|
|
@@ -200,6 +194,27 @@ export declare function followedUp(core: Pick<ConversationCore, 'followUp' | 're
|
|
|
200
194
|
ref: string;
|
|
201
195
|
hold: string;
|
|
202
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;
|
|
203
218
|
/** The answered call's events, for a session that keeps no conversation. */
|
|
204
219
|
export declare function unconversedAnswer(ctx: CallsContext, at: CoreContext, body: TheoremInvokeRequest): Promise<Reply>;
|
|
205
220
|
/** A person's message, for {@link ConversationCore.message}. */
|
|
@@ -276,18 +291,21 @@ export type ConversationCore = {
|
|
|
276
291
|
ref: string;
|
|
277
292
|
}): Promise<AsyncGenerator<ReplyLine>>;
|
|
278
293
|
/**
|
|
279
|
-
* Puts a
|
|
280
|
-
*
|
|
281
|
-
*
|
|
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.
|
|
282
299
|
*/
|
|
283
|
-
steer(at: CoreContext, args: {
|
|
300
|
+
steer(at: CoreContext, args: ({
|
|
301
|
+
ref: string;
|
|
302
|
+
} | {
|
|
284
303
|
turnId: string;
|
|
285
|
-
} & Pick<TheoremSteerRequest, 'id' | 'input'>): Promise<boolean>;
|
|
304
|
+
}) & Pick<TheoremSteerRequest, 'id' | 'input'>): Promise<boolean>;
|
|
286
305
|
/**
|
|
287
|
-
* Stops
|
|
288
|
-
* `
|
|
289
|
-
* the reply that holds the conversation, and reads neither.
|
|
290
|
-
* 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.
|
|
291
309
|
*/
|
|
292
310
|
stop(at: CoreContext, args: TheoremStopRequest): Promise<boolean>;
|
|
293
311
|
};
|