@theoremjs/web 0.3.4 → 0.3.5-next.1001.g68d8dcf

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.
Files changed (59) hide show
  1. package/README.md +330 -13
  2. package/client/chat.d.ts +14 -2
  3. package/client/chat.js +302 -34
  4. package/client/composer-hints.d.ts +18 -10
  5. package/client/composer-hints.js +23 -6
  6. package/client/host-transport.d.ts +7 -2
  7. package/client/host-transport.js +9 -3
  8. package/client/index.d.ts +4 -4
  9. package/client/index.js +3 -3
  10. package/client/page-dispatch.js +5 -0
  11. package/client/run-session.d.ts +27 -2
  12. package/client/run-session.js +34 -4
  13. package/client/schema-fields.d.ts +28 -1
  14. package/client/schema-fields.js +172 -13
  15. package/client/tool-result.d.ts +9 -5
  16. package/client/tool-result.js +23 -42
  17. package/client/transcript-schema.d.ts +4 -0
  18. package/client/transcript-schema.js +25 -6
  19. package/client/transport.d.ts +90 -9
  20. package/client/transport.js +56 -11
  21. package/client/turn-client.d.ts +8 -2
  22. package/client/turn-client.js +9 -0
  23. package/package.json +2 -2
  24. package/server/conversation-core.d.ts +273 -0
  25. package/server/conversation-core.js +797 -0
  26. package/server/conversation-inbox.d.ts +45 -0
  27. package/server/conversation-inbox.js +240 -0
  28. package/server/conversation-store.d.ts +43 -2
  29. package/server/conversation-store.js +15 -2
  30. package/server/conversation.d.ts +82 -2
  31. package/server/conversation.js +357 -60
  32. package/server/create-conversation-core.d.ts +28 -0
  33. package/server/create-conversation-core.js +46 -0
  34. package/server/embedding-handler.js +2 -0
  35. package/server/handler.d.ts +84 -20
  36. package/server/handler.js +279 -659
  37. package/server/mcp-handler.js +25 -11
  38. package/server/mod.d.ts +11 -4
  39. package/server/mod.js +6 -2
  40. package/server/notifier.d.ts +3 -2
  41. package/server/notifier.js +3 -2
  42. package/server/session-authority.d.ts +3 -1
  43. package/server/session-authority.js +26 -1
  44. package/server/session-boundary.d.ts +5 -3
  45. package/server/session-boundary.js +2 -2
  46. package/server/session-calls.d.ts +122 -0
  47. package/server/session-calls.js +512 -0
  48. package/server/session-store.d.ts +33 -0
  49. package/server/session-store.js +37 -2
  50. package/server/steer-inbox.d.ts +3 -4
  51. package/server/steer-inbox.js +6 -5
  52. package/server/tell-every-result.d.ts +11 -0
  53. package/server/tell-every-result.js +17 -0
  54. package/server/tool-operations.d.ts +8 -0
  55. package/server/tool-operations.js +71 -0
  56. package/server/turn-input.d.ts +4 -2
  57. package/server/turn-input.js +5 -2
  58. package/server/turn-wiring.d.ts +31 -6
  59. package/server/turn-wiring.js +59 -5
@@ -13,9 +13,12 @@ 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
20
+ * - `POST <base>/wake` → NDJSON for a reply to no message: the model takes up the results of
21
+ * background calls that ended since it last ran. Empty when none waits
19
22
  *
20
23
  * `createTheoremHostHandler` serves a host profile the same way, with no model:
21
24
  *
@@ -37,20 +40,30 @@ import { type PageToolBroker } from './page-dispatch.ts';
37
40
  * @module
38
41
  */
39
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';
40
44
  import { type ClientTurnOptions } from '@theoremjs/agents/host';
41
- import { type ToolBackgroundService } from '@theoremjs/agents/kernel';
42
- import { type TheoremConversationStore } from './conversation-store.ts';
43
- 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';
44
51
  import type { TheoremNotifier } from './notifier.ts';
52
+ import { type CoreContext } from './session-calls.ts';
45
53
  export { theoremSession, theoremSessionId } from './session-identity.ts';
46
- import { type SessionToolOperation, type TheoremSessionStore } from './session-store.ts';
54
+ import type { SessionToolOperation, TheoremSessionStore } from './session-store.ts';
47
55
  import { type SteerInbox } from './steer-inbox.ts';
48
56
  export type TheoremRequestContext = {
49
57
  request: Request;
50
58
  /** Selected model id, when the client picked one. */
51
59
  model?: string;
52
60
  };
53
- 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> & {
54
67
  /** Profile to serve. Registered with the kernel when the handler is created. */
55
68
  profile: Profile | ProfileDefinition;
56
69
  /**
@@ -69,14 +82,6 @@ export type TheoremHandlerOptions = {
69
82
  * request and is read as untrusted; this one is the host's.
70
83
  */
71
84
  context?: (request: Request) => unknown;
72
- /**
73
- * The application's own turn, from the one the handler builds: `TurnWiring.prepare`, the
74
- * value the application's other call sites use too. The handler keeps its profile, its tool
75
- * execution and its abort. The application's `onStage` runs before the handler's steering, and
76
- * its `credentials` are read before the session's. A call held at a gate runs on, once
77
- * answered, with the `host`, `onStage` and `credentials` it gives for that call's turn.
78
- */
79
- prepare?: TurnWiring['prepare'];
80
85
  /**
81
86
  * Let the page say which prompt lines a turn gets (`stated`), for a page the host trusts as it
82
87
  * trusts itself: the studio's. Unset refuses them, so a public page never chooses its own lines.
@@ -123,7 +128,8 @@ export type TheoremHandlerOptions = {
123
128
  pageToolTimeoutMs?: number;
124
129
  /**
125
130
  * The service for tools with a `background` setting, by session. It receives each detached call
126
- * and its late events. The session keeps each call's state and result. Unset, every tool runs
131
+ * and its late events. The session keeps each call's state and result. A `background` that
132
+ * `prepare` returns runs in its place. Unset, and with none from `prepare`, every tool runs
127
133
  * inline. The value must stay valid after the request ends, until each call settles.
128
134
  */
129
135
  background?: (ctx: {
@@ -133,7 +139,8 @@ export type TheoremHandlerOptions = {
133
139
  /**
134
140
  * Tells an open page that a background call ended, so it redraws with no turn or reload. Unset,
135
141
  * the page reads the end on its next turn or reload. A sweep that marks a call itself calls
136
- * `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.
137
144
  */
138
145
  notifier?: TheoremNotifier;
139
146
  /**
@@ -148,6 +155,41 @@ export type TheoremHandlerOptions = {
148
155
  status: 'settled';
149
156
  }>;
150
157
  }) => boolean | Promise<boolean>;
158
+ /**
159
+ * Who tells the model that a background call ended: `TurnWiring.lateResultsDecider`. Unset, no
160
+ * page wakes the chat and no reply reads a late result. `tellEveryResult` tells each result to
161
+ * the first reply that runs, and lets the open page wake the chat.
162
+ */
163
+ lateResultsDecider?: TurnWiring['lateResultsDecider'];
164
+ /**
165
+ * How long a reply keeps its chat after it last renewed, in milliseconds: it renews every third
166
+ * of it while it runs. A wake the chat turned away tries again once this runs out, and a reply
167
+ * that stopped without letting go keeps the chat no longer. Default 60000.
168
+ */
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>;
151
193
  /**
152
194
  * Tool credentials by session. Your OAuth callback route saves the token here
153
195
  * under the same session id; every turn reads from here. Default: in-process
@@ -180,8 +222,30 @@ export type TheoremHandlerOptions = {
180
222
  request: Request;
181
223
  }) => void;
182
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
+ };
183
247
  /** A host profile runs no model, and takes no turns to steer. */
184
- export type TheoremHostHandlerOptions = Omit<TheoremHandlerOptions, 'profile' | 'provider' | 'steerInbox' | 'conversationStore' | 'prepare' | 'notifier' | 'notifyRetryMs'> & {
248
+ export type TheoremHostHandlerOptions = Omit<TheoremHandlerOptions, 'profile' | 'provider' | 'steerInbox' | 'conversationStore' | 'prepare' | 'inbox' | 'beforeSave' | 'afterSave' | 'compact' | 'notifier' | 'notifyRetryMs' | 'conversationLeaseMs'> & {
185
249
  /** Host profile to serve. Registered with the kernel when the handler is created. */
186
250
  profile: Extract<Profile, {
187
251
  type: 'host';
@@ -191,7 +255,7 @@ export type TheoremHostHandlerOptions = Omit<TheoremHandlerOptions, 'profile' |
191
255
  };
192
256
  /** A JSON body checked against `schema`; a missing or malformed field is a `request` error (400). */
193
257
  export declare function readBody<T>(request: Request, schema: z.ZodType<T>, maxBytes?: number): Promise<T>;
194
- 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>;
195
259
  /**
196
260
  * Serves a host profile to `<TheoremHost />`: the page calls the profile's tools
197
261
  * directly, and every call runs through the kernel's gates and guardrails.
@@ -202,4 +266,4 @@ export declare function defineHostProfile(handler: string, definition: TheoremHo
202
266
  type: 'host';
203
267
  }>;
204
268
  /** A registered tool a host profile can serve: builtins run only inside a model's turn. */
205
- export declare function hostTool(name: string): import("../../../src/kernel/tools/types.ts").RegisteredAgentTool | import("../../../src/kernel/tools/types.ts").HttpToolDef<unknown, unknown> | import("../../../src/kernel/tools/types.ts").McpToolDef<unknown, unknown> | import("../../../src/kernel/tools/types.ts").FunctionToolDef<unknown, unknown> | undefined;
269
+ export declare function hostTool(name: string): import("../../../src/kernel/tools/types.ts").RegisteredAgentTool | import("../../../src/kernel/tools/types.ts").FunctionToolDef<unknown, unknown> | import("../../../src/kernel/tools/types.ts").HttpToolDef<unknown, unknown> | import("../../../src/kernel/tools/types.ts").McpToolDef<unknown, unknown> | undefined;