@theoremjs/web 0.3.5-next.980.g83e419d → 0.3.5-next.982.g16b31de

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.
@@ -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 time a background
17
- * call in this session ends. Set `notifier` to serve it
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
@@ -39,20 +40,30 @@ import { type PageToolBroker } from './page-dispatch.ts';
39
40
  * @module
40
41
  */
41
42
  import { type Profile, type ProfileDefinition, type ProviderHostOptions, type StageHandler, type ToolGate, type TurnWiring, type z } from '@theoremjs/agents';
43
+ import type { InboxItem } from '@theoremjs/agents/conversation';
42
44
  import { type ClientTurnOptions } from '@theoremjs/agents/host';
43
- import { type ToolBackgroundService } from '@theoremjs/agents/kernel';
44
- import { type TheoremConversationStore } from './conversation-store.ts';
45
- import { type TheoremCredentialStore } from './credential-store.ts';
45
+ import { type TranscriptBlock } from '@theoremjs/agents/interface';
46
+ import type { ToolBackgroundService } from '@theoremjs/agents/kernel';
47
+ import { type TheoremTurnInput } from '../client/transport.ts';
48
+ import { type ReplyWiring } from './conversation-core.ts';
49
+ import type { TheoremConversationStore } from './conversation-store.ts';
50
+ import type { TheoremCredentialStore } from './credential-store.ts';
46
51
  import type { TheoremNotifier } from './notifier.ts';
52
+ import { type CoreContext } from './session-calls.ts';
47
53
  export { theoremSession, theoremSessionId } from './session-identity.ts';
48
- import { type SessionToolOperation, type TheoremSessionStore } from './session-store.ts';
54
+ import type { SessionToolOperation, TheoremSessionStore } from './session-store.ts';
49
55
  import { type SteerInbox } from './steer-inbox.ts';
50
56
  export type TheoremRequestContext = {
51
57
  request: Request;
52
58
  /** Selected model id, when the client picked one. */
53
59
  model?: string;
54
60
  };
55
- export type TheoremHandlerOptions = {
61
+ /**
62
+ * Options for `createTheoremHandler`. The app's own parts of a reply (`prepare`, `inbox`,
63
+ * `beforeSave`, `afterSave`, `compact`) are the conversation core's, and take the request's
64
+ * `CoreContext`: its `host` is what the `host` option returned for the request, asked once.
65
+ */
66
+ export type TheoremHandlerOptions<Item extends InboxItem = InboxItem> = ReplyWiring<Item> & {
56
67
  /** Profile to serve. Registered with the kernel when the handler is created. */
57
68
  profile: Profile | ProfileDefinition;
58
69
  /**
@@ -71,16 +82,6 @@ export type TheoremHandlerOptions = {
71
82
  * request and is read as untrusted; this one is the host's.
72
83
  */
73
84
  context?: (request: Request) => unknown;
74
- /**
75
- * The application's own turn, from the one the handler builds: `TurnWiring.prepare`, the
76
- * value the application's other call sites use too. The handler keeps its profile and its
77
- * abort. The application's `onStage` runs before the handler's steering, and its `credentials`
78
- * are read before the session's. An `execution` it returns decides each call, and the session
79
- * still records the same call; a `background` it returns takes the place of `background` below,
80
- * and `notifier` still tells the page. A call held at a gate runs on, once answered, with the
81
- * `host`, `onStage`, `credentials`, `execution` and `background` it gives for that call's turn.
82
- */
83
- prepare?: TurnWiring['prepare'];
84
85
  /**
85
86
  * Let the page say which prompt lines a turn gets (`stated`), for a page the host trusts as it
86
87
  * trusts itself: the studio's. Unset refuses them, so a public page never chooses its own lines.
@@ -138,7 +139,8 @@ export type TheoremHandlerOptions = {
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. `createMemoryNotifier` for one instance.
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
  /**
@@ -165,6 +167,29 @@ export type TheoremHandlerOptions = {
165
167
  * that stopped without letting go keeps the chat no longer. Default 60000.
166
168
  */
167
169
  conversationLeaseMs?: number;
170
+ /**
171
+ * Who runs the next reply when a reply ends with items that wait and may be told now. `here`:
172
+ * the handler runs it in the same response. `taken`: your app runs it, by its own call to the
173
+ * conversation core's `followUp`. Unset, the handler lets the chat go, and the page wakes it.
174
+ */
175
+ onContinue?: (run: {
176
+ ref: string;
177
+ hold: string;
178
+ }, at: {
179
+ request: Request;
180
+ sessionId: string;
181
+ }) => 'here' | 'taken' | Promise<'here' | 'taken'>;
182
+ /**
183
+ * Your app's own save of a person's message, for `/turn` and `/steer`. It saves the message
184
+ * under `sent.id` one time, and in the same write takes the conversation in your `inbox` when
185
+ * no reply holds it. `sent.ref` is the page's ref of the conversation, when it has one. Needs
186
+ * `inbox`. With it, a steer does not go to `steerInbox`.
187
+ */
188
+ intake?: (at: CoreContext, sent: {
189
+ id: string;
190
+ ref?: string;
191
+ message: TheoremTurnInput;
192
+ }) => Promise<MessageIntake>;
168
193
  /**
169
194
  * Tool credentials by session. Your OAuth callback route saves the token here
170
195
  * under the same session id; every turn reads from here. Default: in-process
@@ -197,8 +222,30 @@ export type TheoremHandlerOptions = {
197
222
  request: Request;
198
223
  }) => void;
199
224
  };
225
+ /**
226
+ * What `intake` did with a person's message. `ref` is the conversation that now holds it.
227
+ * `landsAs` is each id a reply names the message by when it reads it: the id of its inject on a
228
+ * `stage` line, the id of its inbox item, and the id of each of its blocks in the conversation.
229
+ *
230
+ * - `opened`: the save took the conversation under `hold`. The handler runs the reply, and
231
+ * `blocks` is the message as the app saved it.
232
+ * - `saved`: no reply runs for this request. Another reply holds the conversation and reads the
233
+ * message, or the app saved this id before, or the app answered the message with a line of its
234
+ * own.
235
+ */
236
+ export type MessageIntake = {
237
+ outcome: 'opened';
238
+ ref: string;
239
+ landsAs: readonly string[];
240
+ hold: string;
241
+ blocks: readonly TranscriptBlock[];
242
+ } | {
243
+ outcome: 'saved';
244
+ ref: string;
245
+ landsAs: readonly string[];
246
+ };
200
247
  /** A host profile runs no model, and takes no turns to steer. */
201
- export type TheoremHostHandlerOptions = Omit<TheoremHandlerOptions, 'profile' | 'provider' | 'steerInbox' | 'conversationStore' | 'prepare' | 'notifier' | 'notifyRetryMs' | 'conversationLeaseMs'> & {
248
+ export type TheoremHostHandlerOptions = Omit<TheoremHandlerOptions, 'profile' | 'provider' | 'steerInbox' | 'conversationStore' | 'prepare' | 'inbox' | 'beforeSave' | 'afterSave' | 'compact' | 'notifier' | 'notifyRetryMs' | 'conversationLeaseMs'> & {
202
249
  /** Host profile to serve. Registered with the kernel when the handler is created. */
203
250
  profile: Extract<Profile, {
204
251
  type: 'host';
@@ -208,7 +255,7 @@ export type TheoremHostHandlerOptions = Omit<TheoremHandlerOptions, 'profile' |
208
255
  };
209
256
  /** A JSON body checked against `schema`; a missing or malformed field is a `request` error (400). */
210
257
  export declare function readBody<T>(request: Request, schema: z.ZodType<T>, maxBytes?: number): Promise<T>;
211
- export declare function createTheoremHandler(options: TheoremHandlerOptions): (request: Request) => Promise<Response>;
258
+ export declare function createTheoremHandler<Item extends InboxItem = InboxItem>(options: TheoremHandlerOptions<Item>): (request: Request) => Promise<Response>;
212
259
  /**
213
260
  * Serves a host profile to `<TheoremHost />`: the page calls the profile's tools
214
261
  * directly, and every call runs through the kernel's gates and guardrails.