@theoremjs/web 0.3.5-next.980.g83e419d → 0.3.5-next.981.gf4729de

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