@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.
@@ -35,9 +35,10 @@ export type StreamInterfaceTurnBaseArgs = {
35
35
  /**
36
36
  * Fires once the message posts, with the blocks the transcript gains: a
37
37
  * walked-away reply's settled blocks, then the message's own (with media
38
- * preview data). Walking away, it posts once the host has it.
38
+ * preview data). Walking away, it posts once the host has it. `own` is how many of the blocks,
39
+ * at the end, are the message's.
39
40
  */
40
- onUserBlocks: (blocks: TranscriptBlock[]) => void;
41
+ onUserBlocks: (blocks: TranscriptBlock[], own: number) => void;
41
42
  signal?: AbortSignal;
42
43
  /**
43
44
  * Send while the reply waits on gates: the message walks away from every
@@ -60,6 +61,15 @@ export type StreamInterfaceTurnArgs = StreamInterfaceTurnBaseArgs & {
60
61
  export type StreamInterfaceDraftTurnArgs = StreamInterfaceTurnBaseArgs & {
61
62
  draft: UserTurnDraft;
62
63
  };
64
+ export declare function extractInlineEncodedAttachments(attachments?: readonly {
65
+ name: string;
66
+ mimeType: string;
67
+ data?: unknown;
68
+ }[]): Array<{
69
+ name: string;
70
+ mimeType: string;
71
+ data: string;
72
+ }> | undefined;
63
73
  export declare function streamInterfaceTurn(args: StreamInterfaceTurnArgs): Promise<RunOutcome>;
64
74
  /** Send a turn from an already-encoded pending draft (queue drain / send now). */
65
75
  export declare function streamInterfaceDraftTurn(args: StreamInterfaceDraftTurnArgs): Promise<RunOutcome>;
@@ -41,13 +41,13 @@ async function streamPreparedTurn(args) {
41
41
  const events = walking ? [...paused] : [];
42
42
  let posted = !keeps;
43
43
  if (!walking)
44
- args.onUserBlocks(userBlocks);
44
+ args.onUserBlocks(userBlocks, userBlocks.length);
45
45
  const onConversation = (line) => {
46
46
  if (!line.posted)
47
47
  return;
48
48
  posted = true;
49
49
  if (walking) {
50
- args.onUserBlocks([...foldAssistantTurn(args.iface, events), ...userBlocks]);
50
+ args.onUserBlocks([...foldAssistantTurn(args.iface, events), ...userBlocks], userBlocks.length);
51
51
  events.length = 0;
52
52
  }
53
53
  };
@@ -87,7 +87,7 @@ async function streamPreparedTurn(args) {
87
87
  }
88
88
  return { session: args.session, events, posted };
89
89
  }
90
- function extractInlineEncodedAttachments(attachments) {
90
+ export function extractInlineEncodedAttachments(attachments) {
91
91
  const filtered = attachments
92
92
  ?.filter((a) => typeof a.data === 'string')
93
93
  .map((a) => ({ name: a.name, mimeType: a.mimeType, data: a.data }));
@@ -8,3 +8,7 @@ import { z } from '@theoremjs/agents';
8
8
  import type { TranscriptBlock } from '@theoremjs/agents/interface';
9
9
  /** One transcript block, as a host stores it and the browser reads it back. */
10
10
  export declare const transcriptBlockSchema: z.ZodType<TranscriptBlock>;
11
+ /** The block without the app's own values: a page cannot state them. */
12
+ export declare function withoutHost(block: TranscriptBlock): TranscriptBlock;
13
+ /** A block as a page's request states it: `host` is the app's to set, so it is dropped. */
14
+ export declare const pageBlockSchema: z.ZodPipe<z.ZodType<TranscriptBlock, unknown, z.core.$ZodTypeInternals<TranscriptBlock, unknown>>, z.ZodTransform<import("../../../src/interface/types").UserTextBlock | import("../../../src/interface/types").UserAttachmentBlock | import("../../../src/interface/types").ThoughtBlock | import("../../../src/interface/types").TextBlock | import("../../../src/interface/types").ToolBlock | import("../../../src/interface/types").StructuredBlock | import("../../../src/interface/types").MediaBlock | import("../../../src/interface/types").GroundingBlock | import("../../../src/interface/types").CitationBlock | import("../../../src/interface/types").EvidenceBlock | import("../../../src/interface/types").GuardrailBlock | import("../../../src/interface/types").LateResultsBlock | import("../../../src/interface/types").ErrorBlock | import("../../../src/interface/types").TurnDoneBlock, TranscriptBlock>>;
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { MEDIA_INPUT_KIND_VALUES, reasoningPartSchema, TURN_STOP_KINDS, turnEventSchema, z, } from '@theoremjs/agents';
8
8
  import { guardrailEventSchema } from '@theoremjs/agents/guardrails';
9
+ import { jsonObjectSchema } from '@theoremjs/agents/kernel';
9
10
  // why: The block shapes' event parts have one schema, the turn event's; a probe event reuses it.
10
11
  function eventPart(probe) {
11
12
  return z.custom((value) => turnEventSchema.safeParse(probe(value)).success);
@@ -49,7 +50,7 @@ const toolCall = z.object({
49
50
  true;
50
51
  const turnStop = z.object({ kind: z.enum(TURN_STOP_KINDS), native: z.string().optional() });
51
52
  true;
52
- const blockId = { id: z.string() };
53
+ const blockId = { id: z.string(), host: jsonObjectSchema.optional() };
53
54
  const transcriptBlock = z.discriminatedUnion('kind', [
54
55
  z.object({ ...blockId, kind: z.literal('user-text'), text: z.string() }),
55
56
  z.object({
@@ -131,3 +132,10 @@ const transcriptBlock = z.discriminatedUnion('kind', [
131
132
  true;
132
133
  /** One transcript block, as a host stores it and the browser reads it back. */
133
134
  export const transcriptBlockSchema = transcriptBlock;
135
+ /** The block without the app's own values: a page cannot state them. */
136
+ export function withoutHost(block) {
137
+ const { host: _host, ...rest } = block;
138
+ return rest;
139
+ }
140
+ /** A block as a page's request states it: `host` is the app's to set, so it is dropped. */
141
+ export const pageBlockSchema = transcriptBlockSchema.transform(withoutHost);
@@ -10,7 +10,7 @@ import type { PageDispatchResult } from './page-dispatch.ts';
10
10
  *
11
11
  * @module
12
12
  */
13
- import { type ErrorKind, type TurnEvent, type TurnHistoryMessage, type TurnToolSnapshot, z } from '@theoremjs/agents';
13
+ import { type ErrorKind, type TurnEvent, type TurnToolSnapshot, z } from '@theoremjs/agents';
14
14
  import { type ErrorCopies } from '@theoremjs/agents/guardrails';
15
15
  import { type ProfileInterface, type TranscriptBlock } from '@theoremjs/agents/interface';
16
16
  import type { PagePart } from './tool-resume.ts';
@@ -74,6 +74,11 @@ export type TheoremTurnRequest = {
74
74
  stated?: StatedTurn;
75
75
  /** Client-generated id so mid-turn steers can find this turn's inbox. */
76
76
  turnId?: string;
77
+ /**
78
+ * The client's id for this message. A message keeps its id when the page sends it again, so a
79
+ * host that saves messages itself (`intake`) saves it one time.
80
+ */
81
+ id?: string;
77
82
  /** invariant: Both issued IDs identify each paused action that this message abandons. */
78
83
  abandon?: GateReference[];
79
84
  /** The tools the person allowed for the page, for a host without a session (the studio). */
@@ -120,15 +125,34 @@ export type TheoremInvokeRequest = {
120
125
  };
121
126
  /** An `/invoke` body as `createTheoremHandler` reads it. */
122
127
  export declare const theoremInvokeRequestSchema: z.ZodType<TheoremInvokeRequest>;
123
- /** Inject user messages into an in-flight turn. */
128
+ /** The person's message for an in-flight turn. */
124
129
  export type TheoremSteerRequest = {
125
130
  turnId: string;
126
131
  /** The client's id for this steer; the turn's `stage` event names it in `injected` once it lands. */
127
132
  id: string;
128
- inject: TurnHistoryMessage[];
133
+ /** The message, as a `/turn` posts one. The host makes the user message the model reads. */
134
+ input: Pick<TheoremTurnInput, 'text' | 'attachments' | 'voice'>;
135
+ /** The ref of the conversation the turn replies in, when the page has one. */
136
+ conversation?: string;
129
137
  };
130
138
  /** A `/steer` body as `createTheoremHandler` reads it. */
131
139
  export declare const theoremSteerRequestSchema: z.ZodType<TheoremSteerRequest>;
140
+ /**
141
+ * A message the host saved itself (`intake`), with no reply to it on this request. `landsAs` is
142
+ * each id a reply names the message by when it reads it: an inject's id on a `stage` line, an id
143
+ * in a `conversation` line's `opened`, or a block's id in the conversation.
144
+ */
145
+ export type TheoremSavedMessage = {
146
+ conversation: string;
147
+ landsAs: string[];
148
+ };
149
+ /**
150
+ * What the host did with a steer. With `saved`, the host keeps the message; without it, the steer
151
+ * waits in the running turn, and the page sends it again when the turn ends without it.
152
+ */
153
+ export type TheoremSteerReply = {
154
+ saved?: TheoremSavedMessage;
155
+ };
132
156
  /** Stop an in-flight turn where it stands; its stream ends with what the reply left. */
133
157
  export type TheoremStopRequest = {
134
158
  turnId: string;
@@ -139,12 +163,16 @@ export declare const theoremStopRequestSchema: z.ZodType<TheoremStopRequest>;
139
163
  * The ref the store issued for the conversation a run wrote: the client sends
140
164
  * it with its next request. It comes before the run's error, which ends the stream.
141
165
  * `posted` marks the write that holds the person's new message: a run that
142
- * fails before it leaves the message unsent.
166
+ * fails before it leaves the message unsent. `landsAs` says the host saved the message itself
167
+ * (`intake`), and is each id a reply names it by (see `TheoremSavedMessage`). `opened` is the id
168
+ * of each item that waited for the model and that the reply starting here reads first.
143
169
  */
144
170
  export type ConversationLine = {
145
171
  type: 'conversation';
146
172
  ref: string;
147
173
  posted?: true;
174
+ landsAs?: string[];
175
+ opened?: string[];
148
176
  };
149
177
  /** The lines a chat's reply streams: turn events, and the conversation's ref. */
150
178
  export declare const CHAT_LINES: WireLines<TurnEvent | ConversationLine>;
@@ -225,7 +253,7 @@ export interface TheoremTransport {
225
253
  describe(signal?: AbortSignal): Promise<ProfileInterface>;
226
254
  turn(request: TheoremTurnRequest, onLine: ChatLineSink, signal?: AbortSignal): Promise<void>;
227
255
  invoke(request: TheoremInvokeRequest, onLine: ChatLineSink, signal?: AbortSignal): Promise<void>;
228
- steer(request: TheoremSteerRequest): Promise<void>;
256
+ steer(request: TheoremSteerRequest): Promise<TheoremSteerReply>;
229
257
  /** Stops the turn; refused (`session.turn_ended`) when this host is not running it. */
230
258
  stop(request: TheoremStopRequest): Promise<void>;
231
259
  /** The conversation under `ref`, as the host shows it. */
@@ -9,10 +9,10 @@ import { gateReferenceSchema, TOOL_GATE_KINDS, } from '@theoremjs/agents';
9
9
  *
10
10
  * @module
11
11
  */
12
- import { describeError, errorKindSchema, isAbortError, TheoremError, TURN_EVENT_SCHEMAS, throwIfAborted, turnEventSchema, turnHistoryMessageSchema, turnToolSnapshotSchema, z, } from '@theoremjs/agents';
12
+ import { describeError, errorKindSchema, isAbortError, TheoremError, TURN_EVENT_SCHEMAS, throwIfAborted, turnEventSchema, turnToolSnapshotSchema, z, } from '@theoremjs/agents';
13
13
  import { errorCopiesSchema, kindOfHttpStatus, } from '@theoremjs/agents/guardrails';
14
14
  import { profileInterfaceSchema, } from '@theoremjs/agents/interface';
15
- import { transcriptBlockSchema } from "./transcript-schema.js";
15
+ import { pageBlockSchema, transcriptBlockSchema } from "./transcript-schema.js";
16
16
  import { checkWire, parseWireJson, readWireLine, } from "./wire-line.js";
17
17
  const encodedBlob = z.object({ name: z.string(), mimeType: z.string(), data: z.string() });
18
18
  true;
@@ -53,6 +53,7 @@ const theoremTurnRequest = z.object({
53
53
  effort: z.string().optional(),
54
54
  stated: statedTurn.optional(),
55
55
  turnId: clientId.optional(),
56
+ id: clientId.optional(),
56
57
  abandon: z.array(gateReferenceSchema).min(1).optional(),
57
58
  replay: theoremReplay.pick({ sessionPermissions: true }).optional(),
58
59
  transcript: z.array(transcriptBlockSchema).optional(),
@@ -86,11 +87,19 @@ export const theoremInvokeRequestSchema = theoremInvokeRequest;
86
87
  const theoremSteerRequest = z.object({
87
88
  turnId: clientId,
88
89
  id: clientId,
89
- inject: z.array(turnHistoryMessageSchema).min(1),
90
+ input: theoremTurnInput.pick({ text: true, attachments: true, voice: true }),
91
+ conversation: z.string().min(1).optional(),
90
92
  });
91
93
  true;
92
94
  /** A `/steer` body as `createTheoremHandler` reads it. */
93
95
  export const theoremSteerRequestSchema = theoremSteerRequest;
96
+ const theoremSavedMessage = z.object({
97
+ conversation: z.string().min(1),
98
+ landsAs: z.array(z.string().min(1)).min(1),
99
+ });
100
+ true;
101
+ const theoremSteerReply = z.object({ saved: theoremSavedMessage.optional() });
102
+ true;
94
103
  const theoremStopRequest = z.object({ turnId: clientId });
95
104
  true;
96
105
  /** A `/stop` body as `createTheoremHandler` reads it. */
@@ -99,6 +108,8 @@ const conversationLine = z.object({
99
108
  type: z.literal('conversation'),
100
109
  ref: z.string().min(1),
101
110
  posted: z.literal(true).optional(),
111
+ landsAs: z.array(z.string().min(1)).min(1).optional(),
112
+ opened: z.array(z.string().min(1)).min(1).optional(),
102
113
  });
103
114
  true;
104
115
  /** The lines a chat's reply streams: turn events, and the conversation's ref. */
@@ -140,7 +151,8 @@ const theoremRewindRequest = z.object({
140
151
  true;
141
152
  /** A `/rewind` body as `createTheoremHandler` reads it. */
142
153
  export const theoremRewindRequestSchema = theoremRewindRequest;
143
- const theoremStartRequest = z.object({ blocks: z.array(transcriptBlockSchema) });
154
+ // invariant: These blocks are a page's; only the app's own save sets a block's `host`.
155
+ const theoremStartRequest = z.object({ blocks: z.array(pageBlockSchema) });
144
156
  true;
145
157
  /** A `/conversation/start` body as a host reads it. */
146
158
  export const theoremStartRequestSchema = theoremStartRequest;
@@ -371,7 +383,7 @@ export function createHttpTransport(options = {}) {
371
383
  }, { ...options, signal }),
372
384
  rewind: (body, signal) => fetchRewound(`${base}/rewind`, body, { ...options, signal }),
373
385
  pageResult: (body, signal) => postJson(`${base}/page-result`, body, { ...options, signal }),
374
- steer: (body) => postJson(`${base}/steer`, body, options),
386
+ steer: async (body) => theoremSteerReply.parse(await fetchJson(`${base}/steer`, { body }, options)),
375
387
  stop: (body) => postJson(`${base}/stop`, body, options),
376
388
  };
377
389
  }
@@ -17,10 +17,14 @@ export type HeldCall = {
17
17
  export type WalkAway = {
18
18
  calls: readonly GatedToolContext[];
19
19
  };
20
- /** Where a request goes on from: the ref of the conversation the host keeps, and the run's id for steers. */
20
+ /**
21
+ * Where a request goes on from: the ref of the conversation the host keeps, the run's id for
22
+ * steers, and the id of the message a turn sends.
23
+ */
21
24
  export type RunScope = {
22
25
  conversation?: string;
23
26
  turnId?: string;
27
+ id?: string;
24
28
  };
25
29
  export declare function buildTurnRequest(iface: ComposerProfileInterface, session: InterfaceTurnSession, input: TheoremTurnInput, options?: RunScope & {
26
30
  walkAway?: WalkAway;
@@ -54,6 +54,7 @@ export function buildTurnRequest(iface, session, input, options = {}) {
54
54
  }));
55
55
  return {
56
56
  ...scopeFields(options),
57
+ ...(options.id ? { id: options.id } : {}),
57
58
  ...generationFields(iface, session),
58
59
  input,
59
60
  ...(abandon.length ? { abandon } : {}),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/web",
3
- "version": "0.3.5-next.980.g83e419d",
3
+ "version": "0.3.5-next.981.gf4729de",
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.980.g83e419d"
28
+ "@theoremjs/agents": "^0.3.5-next.981.gf4729de"
29
29
  }
30
30
  }
@@ -0,0 +1,264 @@
1
+ /**
2
+ * The conversation core: a person's message, an answer to a paused call, a reply to no message,
3
+ * a read and a rewind, each run against the conversation the app keeps, with no `Request`. The
4
+ * HTTP handler builds one for each request and streams what it returns.
5
+ *
6
+ * @module
7
+ */
8
+ import { type GateReference, type NormalizedTurnRequest, type Profile, type ProfileDefinition, type ProviderHostOptions, type TurnHistoryMessage, type TurnInput, type TurnRequest } from '@theoremjs/agents';
9
+ import { type ConversationInbox, type InboxItem, type InboxPolicy, type ReplyRun } from '@theoremjs/agents/conversation';
10
+ import type { ClientTurnOptions } from '@theoremjs/agents/host';
11
+ import { type TranscriptBlock } from '@theoremjs/agents/interface';
12
+ import type { ConversationView, StatedTurn, TheoremInvokeRequest, TheoremRewound, TheoremTurnInput } from '../client/transport.ts';
13
+ import { type ReplyLine, type ReplyToSave, type ShapedReply } from './conversation.ts';
14
+ import { type ReplyEnd, type TheoremConversationStore } from './conversation-store.ts';
15
+ import { type CallsContext, type CoreContext } from './session-calls.ts';
16
+ import { type SteerInbox, type SteerUnit } from './steer-inbox.ts';
17
+ import { type ReplyRefusal } from './turn-wiring.ts';
18
+ export type ChatProfile = Exclude<Profile, {
19
+ type: 'live' | 'decision' | 'host' | 'embedding';
20
+ }>;
21
+ /** What a chat's runs read beside the session's calls. */
22
+ export type ChatContext = {
23
+ profile: ChatProfile;
24
+ /** Undefined when the profile keeps no conversation (`'stateless'`). */
25
+ conversations?: TheoremConversationStore;
26
+ };
27
+ /**
28
+ * The chat `definition` serves, with the store that keeps its conversation. `name` is the function
29
+ * the app called, for the error a wrong profile throws.
30
+ */
31
+ export declare function chatOf(name: string, definition: Profile | ProfileDefinition, conversations: TheoremConversationStore | undefined): ChatContext;
32
+ /**
33
+ * The app's own inbox for a conversation, and its rule for what a reply may be told. A reply runs
34
+ * under a hold of `conversation` in it, and the app reads the reply's items itself.
35
+ */
36
+ export type AppInbox<Item extends InboxItem = InboxItem> = {
37
+ inbox: ConversationInbox<Item>;
38
+ /** The inbox's name for the conversation the runs of this context hold. */
39
+ conversation: string;
40
+ policy: InboxPolicy<Item>;
41
+ /** How long a hold keeps the conversation after it last renewed, in milliseconds. */
42
+ leaseMs: number;
43
+ /**
44
+ * The app's stop for a reply under a hold, kept where every server of the app reads it. Set,
45
+ * `stop` asks it and no turn id is read. Unset, a stop reaches a turn by its id, in `steerInbox`.
46
+ */
47
+ stop?: HoldStop;
48
+ };
49
+ /** A stop for the reply that holds a conversation, whichever server, channel or wake runs it. */
50
+ export type HoldStop = {
51
+ /** Asks the reply that holds `conversation` now to stop. False when no reply holds it. */
52
+ ask(conversation: string): Promise<boolean>;
53
+ /** Settles `true` once a stop is asked for `hold`, and `false` once `over` aborts: the run ended. */
54
+ stopped(conversation: string, hold: string, over: AbortSignal): Promise<boolean>;
55
+ /**
56
+ * Removes a stop asked for `hold`. Called as each model run under the hold ends, so a stop that
57
+ * came too late for one reply does not stop the follow-up under the same hold.
58
+ */
59
+ clear(conversation: string, hold: string): Promise<void>;
60
+ };
61
+ /**
62
+ * The turn the app runs for one model run of a reply, as `TurnWiring.prepare` is. `run` is the
63
+ * reply's hold of the app's `inbox`: the items it opened with, and `take` for those that arrive
64
+ * as it runs. It is unset with no app inbox, and for the turn of an answered call. A
65
+ * `ReplyRefusal` in place of the turn makes the app's line the reply, saved as `refused`, with no
66
+ * model run; the turn of an answered call cannot be refused.
67
+ */
68
+ export type ReplyPrepare<Item extends InboxItem = InboxItem> = (turn: NormalizedTurnRequest, run?: ReplyRun<Item>) => TurnRequest | ReplyRefusal | Promise<TurnRequest | ReplyRefusal>;
69
+ /**
70
+ * The app's own steps around each save of a reply. Each takes the run's context, as every option
71
+ * of the core does.
72
+ */
73
+ export type SaveHooks<Item extends InboxItem = InboxItem> = {
74
+ /**
75
+ * Shapes a reply before each save: the blocks to save in place of the reply's, the ids of the
76
+ * inbox items it told, and the app's own values for its store's write. `held` is the reply's
77
+ * hold of the app's `inbox`. With it set, a reply that failed gets no error block from the
78
+ * core: the app words the failure. A throw on a reply that failed or was stopped is reported
79
+ * to `onError` and saves nothing; any other throw fails the reply.
80
+ */
81
+ beforeSave?: (at: CoreContext, reply: ReplyToSave & {
82
+ held?: ReplyRun<Item>;
83
+ }) => Promise<ShapedReply>;
84
+ /** Runs once a save is written, under the hold. What it throws goes to `onError`. */
85
+ afterSave?: (at: CoreContext, saved: {
86
+ ended: ReplyEnd;
87
+ told: readonly string[];
88
+ }) => Promise<void>;
89
+ /**
90
+ * The app's compaction, in place of the core's, when a saved reply says the history is due
91
+ * for it. The core then writes nothing after the reply. What it throws goes to `onError`.
92
+ */
93
+ compact?: (at: CoreContext, signal: {
94
+ history: TurnHistoryMessage[];
95
+ tokens: number;
96
+ }) => Promise<void>;
97
+ };
98
+ /**
99
+ * The app's own parts of a reply: its turn, its inbox, and its steps around each save. The
100
+ * conversation core and `createTheoremHandler` take the same ones.
101
+ */
102
+ export type ReplyWiring<Item extends InboxItem = InboxItem> = SaveHooks<Item> & {
103
+ /**
104
+ * The app's own turn, from the one a reply builds: `TurnWiring.prepare`, the value the app's
105
+ * other call sites use too. The reply keeps its profile and its abort. The app's `onStage`
106
+ * runs before the reply's steering, and its `credentials` are read before the session's. An
107
+ * `execution` it returns decides each call, and the session still records the same call; a
108
+ * `background` it returns takes the place of the `background` option, and `notifier` still
109
+ * tells the page. A call held at a gate runs on, once answered, with the `host`, `onStage`,
110
+ * `credentials`, `execution` and `background` it gives for that call's turn. With `inbox`, it
111
+ * is also given the reply's hold, so the turn it returns can tell the model the items the
112
+ * reply reads: at once, or from its `onStage`.
113
+ */
114
+ prepare?: ReplyPrepare<Item>;
115
+ /**
116
+ * The app's own inbox, for the runs of one context. Every reply then runs under a hold of the
117
+ * conversation in it. Unset, a reply holds the session's inbox of ended background calls when
118
+ * `lateResultsDecider` is set, and holds nothing otherwise. Not with `lateResultsDecider`: the
119
+ * app's inbox holds every item a reply is told.
120
+ */
121
+ inbox?: (at: CoreContext) => AppInbox<Item>;
122
+ };
123
+ /** Refuses an app's inbox beside a decider for the session's: one inbox holds what a reply is told. */
124
+ export declare function oneInbox(options: {
125
+ inbox?: unknown;
126
+ lateResultsDecider?: unknown;
127
+ }): void;
128
+ /** Everything a conversation's runs read from whoever serves them. */
129
+ export type CoreState<Item extends InboxItem = InboxItem> = Omit<CallsContext, 'options'> & {
130
+ chat: ChatContext;
131
+ steer: SteerInbox;
132
+ options: Omit<CallsContext['options'], 'prepare'> & ReplyWiring<Item> & {
133
+ provider: (at: CoreContext, model?: string) => ProviderHostOptions | Promise<ProviderHostOptions>;
134
+ conversationLeaseMs?: number;
135
+ notifyRetryMs?: number;
136
+ clientEvents?: ClientTurnOptions;
137
+ };
138
+ };
139
+ /**
140
+ * How one run of a conversation ended. `released`: the run let the conversation go. `continue`:
141
+ * items wait that may be told now, and `hold` still has the conversation under `ref`; the caller
142
+ * runs `followUp` or calls `release`. `failure` is what the run threw, for the caller to throw
143
+ * once it has done either. `held`: nothing ran, and a run after `leasedUntil` may: until then
144
+ * another hold has the conversation and tells what waits, or a paused reply waits on a gate that
145
+ * lapses then. `lost`: this run's hold no longer has it.
146
+ */
147
+ export type RunEnd = {
148
+ outcome: 'released';
149
+ } | {
150
+ outcome: 'continue';
151
+ ref: string;
152
+ hold: string;
153
+ failure?: unknown;
154
+ } | {
155
+ outcome: 'held';
156
+ leasedUntil: number;
157
+ } | {
158
+ outcome: 'lost';
159
+ };
160
+ /** A run's lines, then how it ended. */
161
+ export type Reply = AsyncGenerator<ReplyLine, RunEnd>;
162
+ /** What the person picked for a reply's model run. */
163
+ export type Generation = {
164
+ model?: string;
165
+ effort?: string;
166
+ turnId?: string;
167
+ stated?: StatedTurn;
168
+ };
169
+ /**
170
+ * What the caller of a run that ended `continue` does with its hold. `here`: runs the follow-up
171
+ * in the same stream. `taken`: the app runs it elsewhere, by a call of its own to `followUp`.
172
+ * `let_go`: no follow-up runs, and whoever opens the conversation next reads what waits.
173
+ */
174
+ export type ContinueChoice = 'here' | 'taken' | 'let_go';
175
+ /**
176
+ * A run, then what `choose` says for each `continue` it and its follow-ups end with. It ends as
177
+ * the last run ended, `continue` only when the app took it, and throws the first failure.
178
+ */
179
+ export declare function followedUp(core: Pick<ConversationCore, 'followUp' | 'release'>, at: CoreContext, reply: Reply, choose: (run: {
180
+ ref: string;
181
+ hold: string;
182
+ }) => ContinueChoice | Promise<ContinueChoice>): Reply;
183
+ /** The answered call's events, for a session that keeps no conversation. */
184
+ export declare function unconversedAnswer(ctx: CallsContext, at: CoreContext, body: TheoremInvokeRequest): Promise<Reply>;
185
+ /** A person's message, for {@link ConversationCore.message}. */
186
+ export type MessageArgs = {
187
+ /** The conversation's ref; unset for a new one. */
188
+ ref?: string;
189
+ input: TheoremTurnInput;
190
+ /** The app's own context for the run. The profile's `inputs.context.from` must list `server`. */
191
+ context?: unknown;
192
+ generation?: Generation;
193
+ /** The calls a paused reply waits on, to walk away from: exactly those. */
194
+ abandon?: readonly GateReference[];
195
+ /**
196
+ * The app saved the message itself and took the conversation in its `inbox` under `hold`, in
197
+ * one write. The core then posts nothing and runs the reply under `hold`. `blocks` is the
198
+ * message as the app saved it.
199
+ */
200
+ saved?: {
201
+ hold: string;
202
+ blocks: readonly TranscriptBlock[];
203
+ };
204
+ };
205
+ /** A reply to no message, for {@link ConversationCore.wake}. */
206
+ export type WakeArgs = {
207
+ ref: string;
208
+ generation?: Generation;
209
+ input?: Pick<TurnInput, 'options' | 'context'>;
210
+ };
211
+ /** A follow-up, for {@link ConversationCore.followUp}: the `ref` and `hold` of a `continue`. */
212
+ export type FollowUpArgs = {
213
+ ref: string;
214
+ hold: string;
215
+ generation?: Generation;
216
+ input?: Pick<TurnInput, 'options' | 'context'>;
217
+ };
218
+ /** A conversation's runs, with no HTTP request. Each run's lines end with how it ended. */
219
+ export type ConversationCore = {
220
+ /** The person's message: it posts, and its reply runs. */
221
+ message(at: CoreContext, args: MessageArgs): Promise<Reply>;
222
+ /**
223
+ * Throws what `message` throws for `args` before it runs anything, and changes nothing: for an
224
+ * app that saves the message itself, to call before that save.
225
+ */
226
+ checkMessage(at: CoreContext, args: Pick<MessageArgs, 'ref' | 'input' | 'context' | 'abandon'>): Promise<void>;
227
+ /** The person's answer to a paused call; the reply goes on once no call waits. */
228
+ answer(at: CoreContext, body: TheoremInvokeRequest): Promise<Reply>;
229
+ /** A reply to no message, for items that arrived since the model last ran. */
230
+ wake(at: CoreContext, args: WakeArgs): Promise<Reply>;
231
+ /**
232
+ * The next reply to no message, under the hold of a run that ended `continue`. `at` may name
233
+ * another session than that run's, when the app's `inbox` names the same conversation for both.
234
+ */
235
+ followUp(at: CoreContext, args: FollowUpArgs): Promise<Reply>;
236
+ /** Lets the conversation go under `hold`, with no reply: after a run that ended `continue`, or a hold the app took itself. */
237
+ release(at: CoreContext, args: {
238
+ ref: string;
239
+ hold: string;
240
+ }): Promise<void>;
241
+ /** The conversation as a page draws it. */
242
+ read(at: CoreContext, args: {
243
+ ref: string;
244
+ }): Promise<ConversationView>;
245
+ /** The conversation cut after block `at`; a reply that waits on gates is walked away from first. */
246
+ rewind(at: CoreContext, args: {
247
+ ref: string;
248
+ at: number;
249
+ }): Promise<TheoremRewound>;
250
+ /** Puts `unit` into a running turn. False when the turn is not running. */
251
+ steer(at: CoreContext, args: {
252
+ turnId: string;
253
+ unit: SteerUnit;
254
+ }): Promise<boolean>;
255
+ /**
256
+ * Stops a running turn where it stands. False when the turn is not running. Where the app's
257
+ * `inbox` has a `stop`, it stops the reply that holds the conversation, and reads no `turnId`.
258
+ */
259
+ stop(at: CoreContext, args: {
260
+ turnId: string;
261
+ }): Promise<boolean>;
262
+ };
263
+ /** The conversation core over `state`. */
264
+ export declare function createConversationCore<Item extends InboxItem = InboxItem>(state: CoreState<Item>): ConversationCore;