@theoremjs/web 0.3.4 → 0.3.5-next.1000.g569f044
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 +330 -13
- package/client/chat.d.ts +14 -2
- package/client/chat.js +302 -34
- package/client/composer-hints.d.ts +18 -10
- package/client/composer-hints.js +23 -6
- package/client/host-transport.d.ts +7 -2
- package/client/host-transport.js +9 -3
- package/client/index.d.ts +4 -4
- package/client/index.js +3 -3
- package/client/page-dispatch.js +5 -0
- package/client/run-session.d.ts +27 -2
- package/client/run-session.js +34 -4
- package/client/schema-fields.d.ts +28 -1
- package/client/schema-fields.js +172 -13
- package/client/tool-result.d.ts +9 -5
- package/client/tool-result.js +23 -42
- package/client/transcript-schema.d.ts +4 -0
- package/client/transcript-schema.js +25 -6
- package/client/transport.d.ts +90 -9
- package/client/transport.js +56 -11
- package/client/turn-client.d.ts +8 -2
- package/client/turn-client.js +9 -0
- package/package.json +2 -2
- package/server/conversation-core.d.ts +273 -0
- package/server/conversation-core.js +797 -0
- package/server/conversation-inbox.d.ts +45 -0
- package/server/conversation-inbox.js +240 -0
- package/server/conversation-store.d.ts +43 -2
- package/server/conversation-store.js +15 -2
- package/server/conversation.d.ts +82 -2
- package/server/conversation.js +357 -60
- package/server/create-conversation-core.d.ts +28 -0
- package/server/create-conversation-core.js +46 -0
- package/server/embedding-handler.js +2 -0
- package/server/handler.d.ts +84 -20
- package/server/handler.js +279 -659
- package/server/mcp-handler.js +25 -11
- package/server/mod.d.ts +11 -4
- package/server/mod.js +6 -2
- package/server/notifier.d.ts +3 -2
- package/server/notifier.js +3 -2
- package/server/session-authority.d.ts +3 -1
- package/server/session-authority.js +26 -1
- package/server/session-boundary.d.ts +5 -3
- package/server/session-boundary.js +2 -2
- package/server/session-calls.d.ts +122 -0
- package/server/session-calls.js +512 -0
- package/server/session-store.d.ts +33 -0
- package/server/session-store.js +37 -2
- package/server/steer-inbox.d.ts +3 -4
- package/server/steer-inbox.js +6 -5
- package/server/tell-every-result.d.ts +11 -0
- package/server/tell-every-result.js +17 -0
- package/server/tool-operations.d.ts +8 -0
- package/server/tool-operations.js +71 -0
- package/server/turn-input.d.ts +4 -2
- package/server/turn-input.js +5 -2
- package/server/turn-wiring.d.ts +31 -6
- package/server/turn-wiring.js +59 -5
|
@@ -0,0 +1,273 @@
|
|
|
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, TheoremError, 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, TheoremSteerRequest, TheoremStopRequest, 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 } 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
|
+
/** Steer inboxes are scoped to the session, so a turn id alone can't reach another user's turn. */
|
|
170
|
+
/**
|
|
171
|
+
* The refusal of a stop or steer by conversation on a host that can't reach a reply it does not
|
|
172
|
+
* run: no app stop, or no intake for a steer. Not `turn_ended`: the reply may still be running.
|
|
173
|
+
*/
|
|
174
|
+
export declare function replyUnreachable(): TheoremError;
|
|
175
|
+
/**
|
|
176
|
+
* What the caller of a run that ended `continue` does with its hold. `here`: runs the follow-up
|
|
177
|
+
* in the same stream. `taken`: the app runs it elsewhere, by a call of its own to `followUp`.
|
|
178
|
+
* `let_go`: no follow-up runs, and whoever opens the conversation next reads what waits.
|
|
179
|
+
*/
|
|
180
|
+
export type ContinueChoice = 'here' | 'taken' | 'let_go';
|
|
181
|
+
/**
|
|
182
|
+
* A run, then what `choose` says for each `continue` it and its follow-ups end with. It ends as
|
|
183
|
+
* the last run ended, `continue` only when the app took it, and throws the first failure.
|
|
184
|
+
*/
|
|
185
|
+
export declare function followedUp(core: Pick<ConversationCore, 'followUp' | 'release'>, at: CoreContext, reply: Reply, choose: (run: {
|
|
186
|
+
ref: string;
|
|
187
|
+
hold: string;
|
|
188
|
+
}) => ContinueChoice | Promise<ContinueChoice>): Reply;
|
|
189
|
+
/** The answered call's events, for a session that keeps no conversation. */
|
|
190
|
+
export declare function unconversedAnswer(ctx: CallsContext, at: CoreContext, body: TheoremInvokeRequest): Promise<Reply>;
|
|
191
|
+
/** A person's message, for {@link ConversationCore.message}. */
|
|
192
|
+
export type MessageArgs = {
|
|
193
|
+
/** The conversation's ref; unset for a new one. */
|
|
194
|
+
ref?: string;
|
|
195
|
+
input: TheoremTurnInput;
|
|
196
|
+
/** The app's own context for the run. The profile's `inputs.context.from` must list `server`. */
|
|
197
|
+
context?: unknown;
|
|
198
|
+
generation?: Generation;
|
|
199
|
+
/** The calls a paused reply waits on, to walk away from: exactly those. */
|
|
200
|
+
abandon?: readonly GateReference[];
|
|
201
|
+
/**
|
|
202
|
+
* The app saved the message itself and took the conversation in its `inbox` under `hold`, in
|
|
203
|
+
* one write. The core then posts nothing and runs the reply under `hold`. `blocks` is the
|
|
204
|
+
* message as the app saved it.
|
|
205
|
+
*/
|
|
206
|
+
saved?: {
|
|
207
|
+
hold: string;
|
|
208
|
+
blocks: readonly TranscriptBlock[];
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
/** A reply to no message, for {@link ConversationCore.wake}. */
|
|
212
|
+
export type WakeArgs = {
|
|
213
|
+
ref: string;
|
|
214
|
+
generation?: Generation;
|
|
215
|
+
input?: Pick<TurnInput, 'options' | 'context'>;
|
|
216
|
+
};
|
|
217
|
+
/** A follow-up, for {@link ConversationCore.followUp}: the `ref` and `hold` of a `continue`. */
|
|
218
|
+
export type FollowUpArgs = {
|
|
219
|
+
ref: string;
|
|
220
|
+
hold: string;
|
|
221
|
+
generation?: Generation;
|
|
222
|
+
input?: Pick<TurnInput, 'options' | 'context'>;
|
|
223
|
+
};
|
|
224
|
+
/** A conversation's runs, with no HTTP request. Each run's lines end with how it ended. */
|
|
225
|
+
export type ConversationCore = {
|
|
226
|
+
/** The person's message: it posts, and its reply runs. */
|
|
227
|
+
message(at: CoreContext, args: MessageArgs): Promise<Reply>;
|
|
228
|
+
/**
|
|
229
|
+
* Throws what `message` throws for `args` before it runs anything, and changes nothing: for an
|
|
230
|
+
* app that saves the message itself, to call before that save.
|
|
231
|
+
*/
|
|
232
|
+
checkMessage(at: CoreContext, args: Pick<MessageArgs, 'ref' | 'input' | 'context' | 'abandon'>): Promise<void>;
|
|
233
|
+
/** The person's answer to a paused call; the reply goes on once no call waits. */
|
|
234
|
+
answer(at: CoreContext, body: TheoremInvokeRequest): Promise<Reply>;
|
|
235
|
+
/** A reply to no message, for items that arrived since the model last ran. */
|
|
236
|
+
wake(at: CoreContext, args: WakeArgs): Promise<Reply>;
|
|
237
|
+
/**
|
|
238
|
+
* The next reply to no message, under the hold of a run that ended `continue`. `at` may name
|
|
239
|
+
* another session than that run's, when the app's `inbox` names the same conversation for both.
|
|
240
|
+
*/
|
|
241
|
+
followUp(at: CoreContext, args: FollowUpArgs): Promise<Reply>;
|
|
242
|
+
/** Lets the conversation go under `hold`, with no reply: after a run that ended `continue`, or a hold the app took itself. */
|
|
243
|
+
release(at: CoreContext, args: {
|
|
244
|
+
ref: string;
|
|
245
|
+
hold: string;
|
|
246
|
+
}): Promise<void>;
|
|
247
|
+
/** The conversation as a page draws it. */
|
|
248
|
+
read(at: CoreContext, args: {
|
|
249
|
+
ref: string;
|
|
250
|
+
}): Promise<ConversationView>;
|
|
251
|
+
/** The conversation cut after block `at`; a reply that waits on gates is walked away from first. */
|
|
252
|
+
rewind(at: CoreContext, args: {
|
|
253
|
+
ref: string;
|
|
254
|
+
at: number;
|
|
255
|
+
}): Promise<TheoremRewound>;
|
|
256
|
+
/**
|
|
257
|
+
* Puts a steer into a running turn, as the one user message the model reads. Throws what
|
|
258
|
+
* `message` throws for an input the profile does not take, such as text over its limit, and
|
|
259
|
+
* puts nothing in. False when the turn is not running.
|
|
260
|
+
*/
|
|
261
|
+
steer(at: CoreContext, args: {
|
|
262
|
+
turnId: string;
|
|
263
|
+
} & Pick<TheoremSteerRequest, 'id' | 'input'>): Promise<boolean>;
|
|
264
|
+
/**
|
|
265
|
+
* Stops a running reply where it stands: the turn `turnId` names, or the reply that holds
|
|
266
|
+
* `conversation`. False when it is not running. Where the app's `inbox` has a `stop`, it stops
|
|
267
|
+
* the reply that holds the conversation, and reads neither. Without one, only a turn this
|
|
268
|
+
* server runs can be stopped, by its `turnId`.
|
|
269
|
+
*/
|
|
270
|
+
stop(at: CoreContext, args: TheoremStopRequest): Promise<boolean>;
|
|
271
|
+
};
|
|
272
|
+
/** The conversation core over `state`. */
|
|
273
|
+
export declare function createConversationCore<Item extends InboxItem = InboxItem>(state: CoreState<Item>): ConversationCore;
|