@theoremjs/web 0.3.5-next.933.gfc0e83b → 0.3.5-next.936.ga984349

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.
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * @module
10
10
  */
11
- import { jsonObjectSchema, jsonValueSchema, labelText, savedToolInvocationSchema, TheoremError, TOOL_AUTH_TYPES, TOOL_GATE_KINDS, TOOL_PERMISSION, toolOperationOutcomeSchema, toolOperationReplySchema, turnHistoryMessageSchema, turnToolSnapshotSchema, z, } from '@theoremjs/agents';
11
+ import { jsonObjectSchema, jsonValueSchema, labelText, savedToolInvocationSchema, TheoremError, TOOL_AUTH_TYPES, TOOL_GATE_KINDS, TOOL_PERMISSION, TURN_INJECT_STAGES, toolOperationOutcomeSchema, toolOperationReplySchema, turnHistoryMessageSchema, turnToolSnapshotSchema, z, } from '@theoremjs/agents';
12
12
  import { gateExpired } from '@theoremjs/agents/kernel';
13
13
  import { checkedCapacity } from "./session-limits.js";
14
14
  /** The reply the model read for a call that detached, whatever has happened to the call since. */
@@ -44,7 +44,6 @@ const inputSchema = z.strictObject({
44
44
  .optional(),
45
45
  voice: z.array(blobSchema).optional(),
46
46
  history: z.array(turnHistoryMessageSchema).optional(),
47
- lateResults: z.array(toolOperationOutcomeSchema).optional(),
48
47
  repair: z
49
48
  .strictObject({ previousOutput: z.string(), rejection: z.string(), guidance: z.string() })
50
49
  .optional(),
@@ -144,7 +143,6 @@ const operationSchema = z.discriminatedUnion('status', [
144
143
  reply: toolOperationReplySchema.optional(),
145
144
  settledAt: z.number().finite().nonnegative(),
146
145
  retainUntil: z.number().finite().nonnegative().optional(),
147
- wokenAt: z.number().finite().nonnegative().optional(),
148
146
  }),
149
147
  z.strictObject({
150
148
  ...identityFields,
@@ -156,6 +154,22 @@ const operationSchema = z.discriminatedUnion('status', [
156
154
  detectedAt: z.number().finite().nonnegative(),
157
155
  }),
158
156
  ]);
157
+ const handoverSchema = z.strictObject({
158
+ hold: z.string().min(1),
159
+ reply: z.string().min(1),
160
+ stage: z.enum(TURN_INJECT_STAGES),
161
+ step: z.number().int().nonnegative(),
162
+ at: z.number().finite().nonnegative(),
163
+ });
164
+ const conversationInboxSchema = z.strictObject({
165
+ hold: z.string().min(1).optional(),
166
+ leasedUntil: z.number().finite().nonnegative(),
167
+ entries: z.record(z.string().min(1), z.strictObject({
168
+ handed: handoverSchema.optional(),
169
+ handovers: z.array(handoverSchema),
170
+ givenUp: z.literal(true).optional(),
171
+ })),
172
+ });
159
173
  /** Stored authority is checked before the handler reads or changes it. */
160
174
  export const theoremSessionStateSchema = z
161
175
  .strictObject({
@@ -164,6 +178,12 @@ export const theoremSessionStateSchema = z
164
178
  permissions: z.array(z.string().min(1)),
165
179
  gates: z.record(z.string().min(1), pendingSchema),
166
180
  providerCheckpoints: z.array(z.string().min(1)),
181
+ inbox: z
182
+ .strictObject({
183
+ version: z.number().int().nonnegative(),
184
+ conversations: z.record(z.string().min(1), conversationInboxSchema),
185
+ })
186
+ .optional(),
167
187
  })
168
188
  .superRefine((state, ctx) => {
169
189
  for (const [operationId, operation] of Object.entries(state.operations)) {
@@ -198,6 +218,23 @@ export function pruneGates(gates, now, ttlMs) {
198
218
  ? now < gate.expiresAt
199
219
  : !gateExpired(gate.createdAt, now, ttlMs)));
200
220
  }
221
+ /**
222
+ * Drops what the inbox keeps for results the session no longer holds, and conversations no hold
223
+ * has with nothing kept.
224
+ */
225
+ export function pruneInbox(state, now) {
226
+ if (!state.inbox)
227
+ return;
228
+ for (const [key, conversation] of Object.entries(state.inbox.conversations)) {
229
+ for (const id of Object.keys(conversation.entries)) {
230
+ if (!Object.hasOwn(state.operations, id))
231
+ delete conversation.entries[id];
232
+ }
233
+ if (conversation.leasedUntil <= now && Object.keys(conversation.entries).length === 0) {
234
+ delete state.inbox.conversations[key];
235
+ }
236
+ }
237
+ }
201
238
  export function memoryStoreLimits(options) {
202
239
  const { ttlMs, maxSessions } = options;
203
240
  if ((ttlMs !== undefined && (!Number.isFinite(ttlMs) || ttlMs <= 0)) ||
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @module
3
+ * The ready-made decider for an app with no server of its own: the page open on the chat wakes it.
4
+ */
5
+ import type { LateResultsDecider } from '@theoremjs/agents';
6
+ /**
7
+ * Each late result is told to the first reply that runs, and the page open on the chat wakes it
8
+ * when one waits. It keeps no count, and gives up nothing.
9
+ */
10
+ export declare const tellEveryResult: LateResultsDecider;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Each late result is told to the first reply that runs, and the page open on the chat wakes it
3
+ * when one waits. It keeps no count, and gives up nothing.
4
+ */
5
+ export const tellEveryResult = {
6
+ policy: (items) => ({ tell: items.map((item) => item.id) }),
7
+ };
@@ -73,6 +73,13 @@ function saveOutcome(state, identity, outcome) {
73
73
  outcome,
74
74
  settledAt,
75
75
  };
76
+ // why: A detached call's end is an item in its conversation's inbox; a hold reads again before it lets go.
77
+ if (reply) {
78
+ state.inbox = {
79
+ version: (state.inbox?.version ?? 0) + 1,
80
+ conversations: state.inbox?.conversations ?? {},
81
+ };
82
+ }
76
83
  }
77
84
  export function sessionToolCoordinator(store, sessionId) {
78
85
  return {
@@ -2,7 +2,7 @@
2
2
  * The application's turn wiring (`TurnWiring.prepare`) laid over the turn the handler builds.
3
3
  * The handler keeps the fields it owns; the application's stage handler and credentials go
4
4
  * first, and the handler's own run when the application's have nothing. An application's own
5
- * tool record and background service run with the session's, never in place of it.
5
+ * tool record, background service, and late results run with the session's, never in place of it.
6
6
  *
7
7
  * @module
8
8
  */
@@ -10,8 +10,11 @@ import type { NormalizedTurnRequest, StageHandler, TurnRequest, TurnWiring } fro
10
10
  import type { ToolBackgroundService, ToolCredentialSource } from '@theoremjs/agents/kernel';
11
11
  /** The fields a handler sets on every turn, which `prepare` cannot replace. */
12
12
  export type HandlerOwned = Pick<TurnRequest, 'profile' | 'pageExecution' | 'providerState' | 'signal'>;
13
- /** The session's tool record and the handler's background service, which `prepare` can add to. */
14
- export type HandlerTools = Pick<TurnRequest, 'execution' | 'background'>;
13
+ /**
14
+ * The session's tool record, the handler's background service, and the session's late results,
15
+ * which `prepare` can add to.
16
+ */
17
+ export type HandlerTools = Pick<TurnRequest, 'execution' | 'background' | 'lateResults'>;
15
18
  /** What the handler adds to a turn of its own: the session's credentials and its steering. */
16
19
  export type HandlerTurnParts = {
17
20
  credentials: ToolCredentialSource;
@@ -2,7 +2,7 @@
2
2
  * The application's turn wiring (`TurnWiring.prepare`) laid over the turn the handler builds.
3
3
  * The handler keeps the fields it owns; the application's stage handler and credentials go
4
4
  * first, and the handler's own run when the application's have nothing. An application's own
5
- * tool record and background service run with the session's, never in place of it.
5
+ * tool record, background service, and late results run with the session's, never in place of it.
6
6
  *
7
7
  * @module
8
8
  */
@@ -23,11 +23,13 @@ export async function preparedTurn(prepare, turn, handler) {
23
23
  const onStage = stagesInOrder(app.onStage, handler.onStage);
24
24
  const execution = toolsInOrder(app.execution, turn.execution);
25
25
  const background = backgroundInOrder(app.background, turn.background, handler.announce);
26
+ const lateResults = lateResultsInOrder(app.lateResults, turn.lateResults);
26
27
  return {
27
28
  ...app,
28
29
  ...owned,
29
30
  ...(execution ? { execution } : {}),
30
31
  ...(background ? { background } : {}),
32
+ ...(lateResults ? { lateResults } : {}),
31
33
  credentials: credentialsInOrder(app.credentials, handler.credentials),
32
34
  ...(onStage ? { onStage } : {}),
33
35
  };
@@ -50,6 +52,15 @@ function backgroundInOrder(app, own, announce) {
50
52
  return own;
51
53
  return announce ? announce(app) : app;
52
54
  }
55
+ /**
56
+ * The application's late results, then the session's, at each moment the turn asks. The session's
57
+ * are the results it marked given, so an application's `prepare` cannot drop them.
58
+ */
59
+ function lateResultsInOrder(app, session) {
60
+ if (!app || !session || app === session)
61
+ return session ?? app;
62
+ return async (moment) => [...(await app(moment)), ...(await session(moment))];
63
+ }
53
64
  /**
54
65
  * The application's stage handler, then the handler's own when the application's asks for
55
66
  * nothing. What the second one would have done waits for the next stage that allows it.
@@ -1,10 +0,0 @@
1
- /**
2
- * @module
3
- * The ready-made decider for an app with no server of its own: the page open on the chat wakes it.
4
- */
5
- import type { LateResultsDecider } from '@theoremjs/agents';
6
- /**
7
- * The page open on a chat wakes it when a background call ends, and a reply reads every result
8
- * that waits. It keeps no count: each result is read once, by the first reply that runs.
9
- */
10
- export declare const pageWakes: LateResultsDecider;
@@ -1,8 +0,0 @@
1
- /**
2
- * The page open on a chat wakes it when a background call ends, and a reply reads every result
3
- * that waits. It keeps no count: each result is read once, by the first reply that runs.
4
- */
5
- export const pageWakes = {
6
- give: ({ results }) => results.map((result) => result.operationId),
7
- due: ({ results }) => results.length > 0,
8
- };