@theoremjs/web 0.3.5-next.981.gf4729de → 0.3.5-next.983.g2d37576
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 +2 -2
- package/client/chat.js +25 -3
- package/client/transport.d.ts +1 -1
- package/package.json +2 -2
- package/server/conversation-core.d.ts +8 -5
- package/server/conversation-core.js +15 -11
- package/server/handler.d.ts +5 -3
- package/server/handler.js +5 -4
- package/server/notifier.d.ts +3 -2
- package/server/notifier.js +3 -2
package/README.md
CHANGED
|
@@ -478,7 +478,7 @@ Browser dispatch uses a host-owned `PageToolExecution` service beside the execut
|
|
|
478
478
|
`createTheoremHandler` supplies both services. Live relays put the session boundary's `pageExecution` and `toolExecution` on `SessionRequest`.
|
|
479
479
|
|
|
480
480
|
A tool with a `background` setting runs inline until you pass the `background` option to `createTheoremHandler`, or return `background` from `prepare`, with an `execution` of your own that has `detach`, when the same turns run elsewhere in your app too. The option takes `{ request, sessionId }` and returns a `ToolBackgroundService`, which receives each detached call and its late events. The session keeps the call as `background` until it settles, and a late settlement does not invalidate a checkpoint. A conversation read shows the call as it stands: running, then its result, or an error for a marked call. An open page reads it on its next turn or reload, unless you set `notifier`. When the session has dropped a settled operation, the block reads an error with the code `outcome_not_retained`. On a durable store, a worker that dies leaves the operation as `background`, so your sweep marks it `uncertain` with `worker_lost` through `compareAndSwap`. Keep the service's values valid after the request ends. A live relay passes its service to `runSession`.
|
|
481
|
-
To show an open page that a background call ended, with no turn or reload, pass `notifier: createMemoryNotifier()` to `createTheoremHandler`. While a call runs, the conversation read tells the page to open `POST <base>/notify`. The handler sends one `changed` line when the stream opens, and one when each background call in the session ends. The line carries no content, so the page calls the same conversation read as before. The page closes the stream when no call runs. The model reads the result only as your decider says.
|
|
481
|
+
To show an open page that a background call ended, with no turn or reload, pass `notifier: createMemoryNotifier()` to `createTheoremHandler`. While a call runs, the conversation read tells the page to open `POST <base>/notify`. The handler sends one `changed` line when the stream opens, and one when each background call in the session ends. The line carries no content, so the page calls the same conversation read as before. The page closes the stream when no call runs. With `inbox`, the read always tells the page to listen, because your app saves replies the page did not start: call `notifier.publish(sessionId)` after each one. The model reads the result only as your decider says.
|
|
482
482
|
|
|
483
483
|
Set `lateResultsDecider` on your `TurnWiring` and pass it to `createTheoremHandler`. Unset, nothing tells the model a background call ended: the conversation read never says `wake: true`, `<base>/wake` runs nothing, and the next message reads no late result. A result waits for the model when its call ended and no saved reply told it. When the session holds no end for the call (it dropped the settled operation, or marked the call `uncertain`), the result waits as an error with the code `outcome_not_retained` or the uncertainty's reason, and the model reads that the action is unresolved. `ended` hears that end too. A call `authorizeToolReplay` withholds from the request does not wait.
|
|
484
484
|
|
|
@@ -551,7 +551,7 @@ async function received(sessionId: string, text: string, ref: string | undefined
|
|
|
551
551
|
| `release(at, { ref, hold })` | Lets the conversation go under that hold, with no follow-up. |
|
|
552
552
|
| `read(at, { ref })` | Returns the conversation as a page draws it. |
|
|
553
553
|
| `rewind(at, { ref, at })` | Cuts the conversation after transcript block `at`. |
|
|
554
|
-
| `steer(at, { turnId,
|
|
554
|
+
| `steer(at, { turnId, id, input })` | Puts `input` into a running turn. Returns `false` when the turn is not running. Throws what `message` throws for an `input` the profile does not take, such as text over `inputs.maxTextChars`. |
|
|
555
555
|
| `stop(at, { turnId })` | Stops a running turn. Returns `false` when the turn is not running. When your `inbox` has a `stop`, it stops the reply that holds the conversation, and `turnId` is not read. |
|
|
556
556
|
|
|
557
557
|
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`, `lateResultsDecider`, `conversationLeaseMs`, `steerInbox`, and `clientEvents`. These options take the run context in place of the `Request`:
|
package/client/chat.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TheoremError, } from '@theoremjs/agents';
|
|
2
|
-
import { branchInterfaceTurnSession, consumeNextComposerQueue, convertSteersToFrontQueued, createComposerPendingMessage, defaultInterfaceEffort, emptyInterfaceTurnSession, moveComposerPendingWithinKind, orderComposerPendingMessages, promoteComposerPendingKind, removeComposerPendingMessage, removeLandedSteers, saveComposerPendingMessage, takeSavedComposerPending, userDraftHasPayload, userDraftToSteerInject, } from '@theoremjs/agents/interface';
|
|
1
|
+
import { attachmentsRefused, TheoremError, } from '@theoremjs/agents';
|
|
2
|
+
import { branchInterfaceTurnSession, consumeNextComposerQueue, convertSteersToFrontQueued, createComposerPendingMessage, defaultInterfaceEffort, emptyInterfaceTurnSession, moveComposerPendingWithinKind, orderComposerPendingMessages, prepareUserTurn, promoteComposerPendingKind, removeComposerPendingMessage, removeLandedSteers, saveComposerPendingMessage, takeSavedComposerPending, userDraftHasPayload, userDraftToSteerInject, } from '@theoremjs/agents/interface';
|
|
3
3
|
import { composerHasPayload } from "./composer-primary.js";
|
|
4
4
|
import { composerFieldsFromDraft } from "./decode-composer-draft.js";
|
|
5
5
|
import { encodeComposerDraft } from "./encode-composer-draft.js";
|
|
@@ -277,7 +277,19 @@ class Chat {
|
|
|
277
277
|
if (Object.keys(patch).length > 0)
|
|
278
278
|
this.#set(patch);
|
|
279
279
|
};
|
|
280
|
-
setDraftText = (value) =>
|
|
280
|
+
setDraftText = (value) => {
|
|
281
|
+
const next = typeof value === 'function' ? value(this.#state.draftText) : value;
|
|
282
|
+
if (next === this.#state.draftText)
|
|
283
|
+
return;
|
|
284
|
+
const { failure } = this.#state;
|
|
285
|
+
const issues = this.#state.issues.filter((issue) => issue.code !== 'text_too_long');
|
|
286
|
+
// why: Editing the text answers a refusal for its length; a refusal for the files stays until they change.
|
|
287
|
+
this.#set({
|
|
288
|
+
draftText: next,
|
|
289
|
+
issues,
|
|
290
|
+
...(failure?.errorKind === 'input' && issues.length === 0 ? { failure: null } : {}),
|
|
291
|
+
});
|
|
292
|
+
};
|
|
281
293
|
setPendingFiles = (value) => this.#apply('pendingFiles', value);
|
|
282
294
|
setPendingVoice = (value) => this.#apply('pendingVoice', value);
|
|
283
295
|
setPendingMessages = (value) => this.#apply('pendingMessages', value);
|
|
@@ -322,6 +334,16 @@ class Chat {
|
|
|
322
334
|
try {
|
|
323
335
|
const draft = await encodeComposerDraft({ text: draftText, pendingFiles, pendingVoice });
|
|
324
336
|
message = createComposerPendingMessage({ kind, draft });
|
|
337
|
+
const { iface } = this.#options;
|
|
338
|
+
const checked = kind !== 'stash' && iface ? prepareUserTurn(iface.inputs, draft, iface.guardrails) : null;
|
|
339
|
+
if (checked && !checked.ok) {
|
|
340
|
+
// why: A message the profile refuses is never queued or steered; it stays in the composer as typed.
|
|
341
|
+
this.#set({
|
|
342
|
+
issues: checked.issues,
|
|
343
|
+
failure: clientFailure(attachmentsRefused(checked.issues), iface?.labels),
|
|
344
|
+
});
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
325
347
|
}
|
|
326
348
|
catch (err) {
|
|
327
349
|
this.#set({ failure: clientFailure(err, this.#options.iface?.labels) });
|
package/client/transport.d.ts
CHANGED
|
@@ -213,7 +213,7 @@ export type TheoremWakeRequest = {
|
|
|
213
213
|
};
|
|
214
214
|
/** A `/wake` body as `createTheoremHandler` reads it. */
|
|
215
215
|
export declare const theoremWakeRequestSchema: z.ZodType<TheoremWakeRequest>;
|
|
216
|
-
/** Listen for
|
|
216
|
+
/** Listen for changes to this session: a background call ended, or the app saved a reply. A `/notify` body. */
|
|
217
217
|
export type TheoremNotifyRequest = Record<string, never>;
|
|
218
218
|
/** A `/notify` body as `createTheoremHandler` reads it. */
|
|
219
219
|
export declare const theoremNotifyRequestSchema: z.ZodType<TheoremNotifyRequest>;
|
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.983.g2d37576",
|
|
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.983.g2d37576"
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -9,11 +9,11 @@ import { type GateReference, type NormalizedTurnRequest, type Profile, type Prof
|
|
|
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
|
-
import type { ConversationView, StatedTurn, TheoremInvokeRequest, TheoremRewound, TheoremTurnInput } from '../client/transport.ts';
|
|
12
|
+
import type { ConversationView, StatedTurn, TheoremInvokeRequest, TheoremRewound, TheoremSteerRequest, 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
15
|
import { type CallsContext, type CoreContext } from './session-calls.ts';
|
|
16
|
-
import { type SteerInbox
|
|
16
|
+
import { type SteerInbox } from './steer-inbox.ts';
|
|
17
17
|
import { type ReplyRefusal } from './turn-wiring.ts';
|
|
18
18
|
export type ChatProfile = Exclude<Profile, {
|
|
19
19
|
type: 'live' | 'decision' | 'host' | 'embedding';
|
|
@@ -247,11 +247,14 @@ export type ConversationCore = {
|
|
|
247
247
|
ref: string;
|
|
248
248
|
at: number;
|
|
249
249
|
}): Promise<TheoremRewound>;
|
|
250
|
-
/**
|
|
250
|
+
/**
|
|
251
|
+
* Puts a steer into a running turn, as the one user message the model reads. Throws what
|
|
252
|
+
* `message` throws for an input the profile does not take, such as text over its limit, and
|
|
253
|
+
* puts nothing in. False when the turn is not running.
|
|
254
|
+
*/
|
|
251
255
|
steer(at: CoreContext, args: {
|
|
252
256
|
turnId: string;
|
|
253
|
-
|
|
254
|
-
}): Promise<boolean>;
|
|
257
|
+
} & Pick<TheoremSteerRequest, 'id' | 'input'>): Promise<boolean>;
|
|
255
258
|
/**
|
|
256
259
|
* Stops a running turn where it stands. False when the turn is not running. Where the app's
|
|
257
260
|
* `inbox` has a `stop`, it stops the reply that holds the conversation, and reads no `turnId`.
|
|
@@ -13,7 +13,7 @@ import { detachedEnd, sessionConversationInbox } from "./conversation-inbox.js";
|
|
|
13
13
|
import { unknownConversation, } from "./conversation-store.js";
|
|
14
14
|
import { sessionPageDispatch } from "./page-dispatch.js";
|
|
15
15
|
import { announced, backgroundWiring, pausedWaits, recorded, replayAllowed, sessionCredentials, settlingOf, takeLapsed, takeWalkedAway, withAuthorizationUrls, } from "./session-calls.js";
|
|
16
|
-
import { steerStage, stopOnRequest } from "./steer-inbox.js";
|
|
16
|
+
import { steerStage, steerUnitOf, stopOnRequest } from "./steer-inbox.js";
|
|
17
17
|
import { sessionToolCoordinator } from "./tool-operations.js";
|
|
18
18
|
import { isRefusal, preparedTurn, stagesInOrder } from "./turn-wiring.js";
|
|
19
19
|
/**
|
|
@@ -516,18 +516,19 @@ async function currentView(ctx, at, host, ref, conversation) {
|
|
|
516
516
|
return view;
|
|
517
517
|
const wake = (await wakeDue(ctx, at, host, ref, conversation)) ? { wake: true } : {};
|
|
518
518
|
if (!detached)
|
|
519
|
-
return { ...view, ...wake };
|
|
519
|
+
return { ...view, ...notifyOffer(ctx), ...wake };
|
|
520
520
|
const { operations } = await ctx.sessions.read(at.sessionId);
|
|
521
521
|
const transcript = await Promise.all(view.transcript.map((block) => isDetachedBlock(block) ? lateBlock(ctx, at, block, operations, host) : block));
|
|
522
|
+
// why: an app's inbox saves replies the page did not ask for, so its page always listens
|
|
523
|
+
const listens = ctx.options.inbox !== undefined || transcript.some(isDetachedBlock);
|
|
524
|
+
return { ...view, transcript, ...(listens ? notifyOffer(ctx) : {}), ...wake };
|
|
525
|
+
}
|
|
526
|
+
/** The view's offer to listen on `/notify`: none without a notifier. */
|
|
527
|
+
function notifyOffer(ctx) {
|
|
522
528
|
const { notifier, notifyRetryMs } = ctx.options;
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
...(notifier && transcript.some(isDetachedBlock)
|
|
527
|
-
? { notify: notifyRetryMs === undefined ? {} : { retryMs: notifyRetryMs } }
|
|
528
|
-
: {}),
|
|
529
|
-
...wake,
|
|
530
|
-
};
|
|
529
|
+
if (!notifier)
|
|
530
|
+
return {};
|
|
531
|
+
return { notify: notifyRetryMs === undefined ? {} : { retryMs: notifyRetryMs } };
|
|
531
532
|
}
|
|
532
533
|
/**
|
|
533
534
|
* Whether the page wakes the chat now: the decider's policy tells a waiting result to a reply to no
|
|
@@ -768,7 +769,10 @@ export function createConversationCore(state) {
|
|
|
768
769
|
release: (at, args) => release(state, at, args),
|
|
769
770
|
read: (at, args) => read(state, at, args),
|
|
770
771
|
rewind: (at, args) => rewind(state, at, args),
|
|
771
|
-
steer: (at, { turnId,
|
|
772
|
+
steer: async (at, { turnId, ...steer }) => {
|
|
773
|
+
postedMessage(chatInterface(state.chat), steer.input);
|
|
774
|
+
return await state.steer.enqueue(inboxKey(at.sessionId, turnId), steerUnitOf(steer));
|
|
775
|
+
},
|
|
772
776
|
stop: (at, { turnId }) => {
|
|
773
777
|
const app = state.options.inbox?.(at);
|
|
774
778
|
if (app?.stop)
|
package/server/handler.d.ts
CHANGED
|
@@ -13,8 +13,9 @@ import { type PageToolBroker } from './page-dispatch.ts';
|
|
|
13
13
|
* - `POST <base>/stop` → stop a running turn where it stands
|
|
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
|
-
* - `POST <base>/notify` → NDJSON that stays open: one line now, and one each
|
|
17
|
-
*
|
|
16
|
+
* - `POST <base>/notify` → NDJSON that stays open: one line now, and one for each `publish` under
|
|
17
|
+
* this session: a background call ended, or the app saved a reply.
|
|
18
|
+
* Set `notifier` to serve it
|
|
18
19
|
* - `POST <base>/rewind` → `{ conversation, view }` the conversation cut after one block, and its new ref
|
|
19
20
|
* - `POST <base>/wake` → NDJSON for a reply to no message: the model takes up the results of
|
|
20
21
|
* background calls that ended since it last ran. Empty when none waits
|
|
@@ -138,7 +139,8 @@ export type TheoremHandlerOptions<Item extends InboxItem = InboxItem> = ReplyWir
|
|
|
138
139
|
/**
|
|
139
140
|
* Tells an open page that a background call ended, so it redraws with no turn or reload. Unset,
|
|
140
141
|
* the page reads the end on its next turn or reload. A sweep that marks a call itself calls
|
|
141
|
-
* `publish` too. `
|
|
142
|
+
* `publish` too. With `inbox`, the page always listens, and the app calls `publish` for each
|
|
143
|
+
* reply it saves away from the page. `createMemoryNotifier` for one instance.
|
|
142
144
|
*/
|
|
143
145
|
notifier?: TheoremNotifier;
|
|
144
146
|
/**
|
package/server/handler.js
CHANGED
|
@@ -13,8 +13,9 @@ import { pageToolResultSchema, sessionPageDispatch } from "./page-dispatch.js";
|
|
|
13
13
|
* - `POST <base>/stop` → stop a running turn where it stands
|
|
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
|
-
* - `POST <base>/notify` → NDJSON that stays open: one line now, and one each
|
|
17
|
-
*
|
|
16
|
+
* - `POST <base>/notify` → NDJSON that stays open: one line now, and one for each `publish` under
|
|
17
|
+
* this session: a background call ended, or the app saved a reply.
|
|
18
|
+
* Set `notifier` to serve it
|
|
18
19
|
* - `POST <base>/rewind` → `{ conversation, view }` the conversation cut after one block, and its new ref
|
|
19
20
|
* - `POST <base>/wake` → NDJSON for a reply to no message: the model takes up the results of
|
|
20
21
|
* background calls that ended since it last ran. Empty when none waits
|
|
@@ -52,7 +53,7 @@ export { theoremSession, theoremSessionId } from "./session-identity.js";
|
|
|
52
53
|
import { jsonResponse } from "./json-response.js";
|
|
53
54
|
import { boundedRequestJson, requestByteLimit } from "./request-body.js";
|
|
54
55
|
import { checkRequest } from "./request-check.js";
|
|
55
|
-
import { createMemorySteerInbox
|
|
56
|
+
import { createMemorySteerInbox } from "./steer-inbox.js";
|
|
56
57
|
const NDJSON_HEADERS = {
|
|
57
58
|
'content-type': 'application/x-ndjson; charset=utf-8',
|
|
58
59
|
'cache-control': 'no-store',
|
|
@@ -413,7 +414,7 @@ async function steer(ctx, chat, request, session) {
|
|
|
413
414
|
const saved = { conversation: taken.ref, landsAs: [...taken.landsAs] };
|
|
414
415
|
return jsonResponse(200, { ok: true, saved });
|
|
415
416
|
}
|
|
416
|
-
if (!(await core.steer(at, { turnId: body.turnId,
|
|
417
|
+
if (!(await core.steer(at, { turnId: body.turnId, id: body.id, input: body.input }))) {
|
|
417
418
|
// labels-exempt: internal diagnostic; the user reads the error kind's (or copy key's) wording
|
|
418
419
|
throw new TheoremError('request', `turn ${body.turnId} is not running`, {
|
|
419
420
|
copy: { key: 'session.turn_ended' },
|
package/server/notifier.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Where a session's open pages hear that a background call ended
|
|
3
|
-
* page that hears it reads the conversation, so one read decides what a
|
|
2
|
+
* Where a session's open pages hear that a background call ended or the app saved a reply. The line
|
|
3
|
+
* carries no content: a page that hears it reads the conversation, so one read decides what a
|
|
4
|
+
* reader sees.
|
|
4
5
|
*
|
|
5
6
|
* @module
|
|
6
7
|
*/
|
package/server/notifier.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Where a session's open pages hear that a background call ended
|
|
3
|
-
* page that hears it reads the conversation, so one read decides what a
|
|
2
|
+
* Where a session's open pages hear that a background call ended or the app saved a reply. The line
|
|
3
|
+
* carries no content: a page that hears it reads the conversation, so one read decides what a
|
|
4
|
+
* reader sees.
|
|
4
5
|
*
|
|
5
6
|
* @module
|
|
6
7
|
*/
|