@theoremjs/web 0.3.5-next.914.ga9d17c6 → 0.3.5-next.915.ge040f25
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 +2 -2
- package/package.json +2 -2
- package/server/handler.d.ts +8 -5
- package/server/handler.js +17 -13
- package/server/tool-operations.d.ts +8 -0
- package/server/tool-operations.js +60 -0
- package/server/turn-wiring.d.ts +11 -5
- package/server/turn-wiring.js +27 -4
package/README.md
CHANGED
|
@@ -265,7 +265,7 @@ const transport = withPageToolDispatch(
|
|
|
265
265
|
| `provider` | Host options, including the credential vault. A function can return these options per request, for example for tenant-specific credentials. |
|
|
266
266
|
| `session` | Returns the session id of the caller, for example `${userId}:${conversationId}`. Return `undefined` to refuse the request with a 401. Default: a random id in an HttpOnly, SameSite=Lax cookie that lasts 30 days. |
|
|
267
267
|
| `host` | App data for the tool handlers (`ctx.host`), such as the signed-in user. |
|
|
268
|
-
| `prepare` | Your app's own turn, from the one the handler builds (`TurnWiring.prepare` from `@theoremjs/agents`). Set what your app sets per turn, such as `system`, `credentials`, `onStage`, `onEvent` and `onAgentCall`; read your app data as `turn.host`. The handler keeps its profile
|
|
268
|
+
| `prepare` | Your app's own turn, from the one the handler builds (`TurnWiring.prepare` from `@theoremjs/agents`). Set what your app sets per turn, such as `system`, `credentials`, `onStage`, `onEvent` and `onAgentCall`; read your app data as `turn.host`. The handler keeps its profile and its abort. Your `onStage` runs before the handler's steering, and your `credentials` are read before the session's. An `execution` you return decides each call, and the session still records the same call; a `background` you return takes the place of the `background` option. A call held at a gate runs on, once answered, with the `host`, `onStage`, `credentials`, `execution` and `background` you give for that call's turn; that turn has `answersHeldCall`, and its `onEvent` is not called, because no new turn runs. |
|
|
269
269
|
| `onStage` | The server's own stage handler, on every call the handler runs, a host profile's included: after your app's (`prepare`), before steering. For a rule the server adds to whatever the app says, such as a question before each tool that writes. |
|
|
270
270
|
| `pageToolBroker` | Delivers results to active page dispatches. Default: bounded process memory. |
|
|
271
271
|
| `pageStatesTurn` | Let the page say which prompt lines a turn gets, through `stated` on a turn or a live call. Set it only for a page you trust as yourself, such as a builder's own tool. Without it, a request with `stated` is refused. |
|
|
@@ -456,7 +456,7 @@ To retain execution claims across connections, supply a durable coordinator as `
|
|
|
456
456
|
Browser dispatch uses a host-owned `PageToolExecution` service beside the execution coordinator.
|
|
457
457
|
`createTheoremHandler` supplies both services. Live relays put the session boundary's `pageExecution` and `toolExecution` on `SessionRequest`.
|
|
458
458
|
|
|
459
|
-
A tool with a `background` setting runs inline until you pass the `background` option to `createTheoremHandler
|
|
459
|
+
A tool with a `background` setting runs inline until you pass the `background` option to `createTheoremHandler`, or return `background` from `prepare`, with an `execution` of your own that has `detach`, when the same turns run elsewhere in your app too. The option takes `{ request, sessionId }` and returns a `ToolBackgroundService`, which receives each detached call and its late events. The session keeps the call as `background` until it settles, and a late settlement does not invalidate a checkpoint. A conversation read shows the call as it stands: running, then its result, or an error for a marked call. An open page reads it on its next turn or reload, unless you set `notifier`. When the session has dropped a settled operation, the block reads an error with the code `outcome_not_retained`. On a durable store, a worker that dies leaves the operation as `background`, so your sweep marks it `uncertain` with `worker_lost` through `compareAndSwap`. Keep the service's values valid after the request ends. A live relay passes its service to `runSession`.
|
|
460
460
|
To show an open page that a background call ended, with no turn or reload, pass `notifier: createMemoryNotifier()` to `createTheoremHandler`. While a call runs, the conversation read tells the page to open `POST <base>/notify`. The handler sends one `changed` line when the stream opens, and one when each background call in the session ends. The line carries no content, so the page calls the same conversation read as before. The page closes the stream when no call runs. The model reads nothing new: a late result still reaches it through `input.lateResults`.
|
|
461
461
|
|
|
462
462
|
`createMemoryNotifier` reaches only pages connected to the process that ran the call. On more than one instance, give a shared `TheoremNotifier`. Your sweep calls `publish` with the session id after it marks a call `worker_lost`, because that write does not pass through the handler. Hosting platforms cut long responses. Set `notifyRetryMs` so that the page reopens the stream after that wait. Unset, the page does not reopen it, and reads the end on its next turn or reload.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theoremjs/web",
|
|
3
|
-
"version": "0.3.5-next.
|
|
3
|
+
"version": "0.3.5-next.915.ge040f25",
|
|
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.
|
|
28
|
+
"@theoremjs/agents": "^0.3.5-next.915.ge040f25"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/server/handler.d.ts
CHANGED
|
@@ -71,10 +71,12 @@ export type TheoremHandlerOptions = {
|
|
|
71
71
|
context?: (request: Request) => unknown;
|
|
72
72
|
/**
|
|
73
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
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
74
|
+
* value the application's other call sites use too. The handler keeps its profile and its
|
|
75
|
+
* abort. The application's `onStage` runs before the handler's steering, and its `credentials`
|
|
76
|
+
* are read before the session's. An `execution` it returns decides each call, and the session
|
|
77
|
+
* still records the same call; a `background` it returns takes the place of `background` below,
|
|
78
|
+
* and `notifier` still tells the page. A call held at a gate runs on, once answered, with the
|
|
79
|
+
* `host`, `onStage`, `credentials`, `execution` and `background` it gives for that call's turn.
|
|
78
80
|
*/
|
|
79
81
|
prepare?: TurnWiring['prepare'];
|
|
80
82
|
/**
|
|
@@ -123,7 +125,8 @@ export type TheoremHandlerOptions = {
|
|
|
123
125
|
pageToolTimeoutMs?: number;
|
|
124
126
|
/**
|
|
125
127
|
* 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.
|
|
128
|
+
* and its late events. The session keeps each call's state and result. A `background` that
|
|
129
|
+
* `prepare` returns runs in its place. Unset, and with none from `prepare`, every tool runs
|
|
127
130
|
* inline. The value must stay valid after the request ends, until each call settles.
|
|
128
131
|
*/
|
|
129
132
|
background?: (ctx: {
|
package/server/handler.js
CHANGED
|
@@ -374,6 +374,8 @@ async function* invokeAnswered(ctx, request, sessionId, { callId, pending, answe
|
|
|
374
374
|
// why: The held call goes on with what the application set for its turn: its context, stage handler and credentials.
|
|
375
375
|
const prepared = await preparedTurn(ctx.options.prepare, {
|
|
376
376
|
profile: ctx.profile.id,
|
|
377
|
+
execution: sessionToolCoordinator(ctx.sessions, sessionId),
|
|
378
|
+
...backgroundWiring(ctx, request, sessionId),
|
|
377
379
|
input: pending.turnInput ?? {},
|
|
378
380
|
host,
|
|
379
381
|
signal: request.signal,
|
|
@@ -381,11 +383,12 @@ async function* invokeAnswered(ctx, request, sessionId, { callId, pending, answe
|
|
|
381
383
|
}, {
|
|
382
384
|
credentials: sessionCredentials(ctx, sessionId),
|
|
383
385
|
...(ctx.options.onStage ? { onStage: ctx.options.onStage } : {}),
|
|
386
|
+
announce: (service) => announced(ctx, request, sessionId, service),
|
|
384
387
|
});
|
|
385
388
|
yield* invokeTool({
|
|
386
389
|
profile: ctx.profile.id,
|
|
387
|
-
execution:
|
|
388
|
-
...
|
|
390
|
+
...(prepared.execution ? { execution: prepared.execution } : {}),
|
|
391
|
+
...(prepared.background ? { background: prepared.background } : {}),
|
|
389
392
|
pageExecution: sessionPageDispatch(ctx.sessions, sessionId, ctx.pageToolBroker, ctx.options.pageToolTimeoutMs).execution,
|
|
390
393
|
name: pending.name,
|
|
391
394
|
callId,
|
|
@@ -410,21 +413,21 @@ async function* invokeAnswered(ctx, request, sessionId, { callId, pending, answe
|
|
|
410
413
|
/** The host's service for a call in this session, for a request that runs a tool. */
|
|
411
414
|
function backgroundWiring(ctx, request, sessionId) {
|
|
412
415
|
const service = ctx.options.background?.({ request, sessionId });
|
|
413
|
-
|
|
414
|
-
|
|
416
|
+
return service ? { background: announced(ctx, request, sessionId, service) } : {};
|
|
417
|
+
}
|
|
418
|
+
/** `service`, telling the session's open page through the notifier when each of its calls ends. */
|
|
419
|
+
function announced(ctx, request, sessionId, service) {
|
|
415
420
|
const { notifier } = ctx.options;
|
|
416
421
|
if (!notifier)
|
|
417
|
-
return
|
|
422
|
+
return service;
|
|
418
423
|
return {
|
|
419
|
-
|
|
420
|
-
detached(call)
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
.catch((err) => ctx.options.onError?.(err, { request }));
|
|
425
|
-
},
|
|
426
|
-
event: (event, call) => service.event(event, call),
|
|
424
|
+
detached(call) {
|
|
425
|
+
service.detached(call);
|
|
426
|
+
void call.done
|
|
427
|
+
.then(() => notifier.publish(sessionId))
|
|
428
|
+
.catch((err) => ctx.options.onError?.(err, { request }));
|
|
427
429
|
},
|
|
430
|
+
event: (event, call) => service.event(event, call),
|
|
428
431
|
};
|
|
429
432
|
}
|
|
430
433
|
/** A settled call's answer, streamed again. */
|
|
@@ -460,6 +463,7 @@ async function* chatTurn(ctx, request, session, turn, key, stop) {
|
|
|
460
463
|
}, {
|
|
461
464
|
credentials: sessionCredentials(ctx, session.id),
|
|
462
465
|
...(onStage ? { onStage } : {}),
|
|
466
|
+
announce: (service) => announced(ctx, request, session.id, service),
|
|
463
467
|
});
|
|
464
468
|
const events = runTurn(prepared, provider);
|
|
465
469
|
yield* recorded(ctx.sessions, session.id, withAuthorizationUrls(ctx, request, session.id, events), {
|
|
@@ -6,4 +6,12 @@ export declare function sessionToolCoordinator(store: {
|
|
|
6
6
|
};
|
|
7
7
|
mutate<T>(id: string, change: (state: TheoremSessionState) => T): Promise<T>;
|
|
8
8
|
}, sessionId: string): ToolExecutionCoordinator;
|
|
9
|
+
/**
|
|
10
|
+
* The application's coordinator, with the session's record kept beside it. The application's
|
|
11
|
+
* decides each call and holds it first; the session's records the same call after it, so the page
|
|
12
|
+
* shows the call and its late end, and a resend reads it back.
|
|
13
|
+
* invariant: A call the application holds and the session already knows breaks the contract; the
|
|
14
|
+
* application's claim stays open for its own sweep.
|
|
15
|
+
*/
|
|
16
|
+
export declare function layeredToolCoordinator(owner: ToolExecutionCoordinator, session: ToolExecutionCoordinator): ToolExecutionCoordinator;
|
|
9
17
|
export declare function sameAcceptedAnswer(left: unknown, right: unknown): boolean;
|
|
@@ -154,6 +154,66 @@ export function sessionToolCoordinator(store, sessionId) {
|
|
|
154
154
|
},
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* The application's coordinator, with the session's record kept beside it. The application's
|
|
159
|
+
* decides each call and holds it first; the session's records the same call after it, so the page
|
|
160
|
+
* shows the call and its late end, and a resend reads it back.
|
|
161
|
+
* invariant: A call the application holds and the session already knows breaks the contract; the
|
|
162
|
+
* application's claim stays open for its own sweep.
|
|
163
|
+
*/
|
|
164
|
+
export function layeredToolCoordinator(owner, session) {
|
|
165
|
+
// why: The kernel hands back the owner's claim; the session's token for the same call is kept here.
|
|
166
|
+
const sessionTokens = new Map();
|
|
167
|
+
const sessionClaim = (claim) => {
|
|
168
|
+
const claimToken = sessionTokens.get(claim.operationId);
|
|
169
|
+
// labels-exempt: internal diagnostic; the user reads tool.execution_uncertain
|
|
170
|
+
if (!claimToken)
|
|
171
|
+
unavailable(`The session holds no claim on tool operation ${claim.operationId}`);
|
|
172
|
+
return { ...claim, claimToken };
|
|
173
|
+
};
|
|
174
|
+
const limits = [owner.maxOutcomeBytes, session.maxOutcomeBytes].filter((limit) => limit !== undefined);
|
|
175
|
+
const { detach } = owner;
|
|
176
|
+
return {
|
|
177
|
+
...(limits.length > 0 ? { maxOutcomeBytes: Math.min(...limits) } : {}),
|
|
178
|
+
async lookup(identity) {
|
|
179
|
+
return (await owner.lookup(identity)) ?? (await session.lookup(identity));
|
|
180
|
+
},
|
|
181
|
+
async begin(identity) {
|
|
182
|
+
const held = await owner.begin(identity);
|
|
183
|
+
if (held.kind !== 'acquired')
|
|
184
|
+
return held;
|
|
185
|
+
const recorded = await session.begin(identity);
|
|
186
|
+
if (recorded.kind !== 'acquired')
|
|
187
|
+
unavailable(
|
|
188
|
+
// labels-exempt: internal diagnostic; the user reads tool.execution_uncertain
|
|
189
|
+
`The session already holds tool operation ${identity.operationId}, which its owner had not`);
|
|
190
|
+
sessionTokens.set(identity.operationId, recorded.claimToken);
|
|
191
|
+
return held;
|
|
192
|
+
},
|
|
193
|
+
...(detach
|
|
194
|
+
? {
|
|
195
|
+
async detach(claim, reply) {
|
|
196
|
+
await detach.call(owner, claim, reply);
|
|
197
|
+
await session.detach?.(sessionClaim(claim), reply);
|
|
198
|
+
},
|
|
199
|
+
}
|
|
200
|
+
: {}),
|
|
201
|
+
async settle(claim, outcome) {
|
|
202
|
+
await owner.settle(claim, outcome);
|
|
203
|
+
await session.settle(sessionClaim(claim), outcome);
|
|
204
|
+
sessionTokens.delete(claim.operationId);
|
|
205
|
+
},
|
|
206
|
+
async settleNotStarted(identity, outcome) {
|
|
207
|
+
await owner.settleNotStarted(identity, outcome);
|
|
208
|
+
await session.settleNotStarted(identity, outcome);
|
|
209
|
+
},
|
|
210
|
+
async markUncertain(claim, reason) {
|
|
211
|
+
await owner.markUncertain(claim, reason);
|
|
212
|
+
await session.markUncertain(sessionClaim(claim), reason);
|
|
213
|
+
sessionTokens.delete(claim.operationId);
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
157
217
|
export function sameAcceptedAnswer(left, right) {
|
|
158
218
|
const canonical = (value) => {
|
|
159
219
|
if (Array.isArray(value))
|
package/server/turn-wiring.d.ts
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
/**
|
|
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
|
-
* first, and the handler's own run when the application's have nothing.
|
|
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
6
|
*
|
|
6
7
|
* @module
|
|
7
8
|
*/
|
|
8
9
|
import type { NormalizedTurnRequest, StageHandler, TurnRequest, TurnWiring } from '@theoremjs/agents';
|
|
9
|
-
import type { ToolCredentialSource } from '@theoremjs/agents/kernel';
|
|
10
|
+
import type { ToolBackgroundService, ToolCredentialSource } from '@theoremjs/agents/kernel';
|
|
10
11
|
/** The fields a handler sets on every turn, which `prepare` cannot replace. */
|
|
11
|
-
export type HandlerOwned = Pick<TurnRequest, 'profile' | '
|
|
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'>;
|
|
12
15
|
/** What the handler adds to a turn of its own: the session's credentials and its steering. */
|
|
13
16
|
export type HandlerTurnParts = {
|
|
14
17
|
credentials: ToolCredentialSource;
|
|
15
18
|
onStage?: StageHandler;
|
|
19
|
+
/** Tells an open page when a call of the service it is given ends. */
|
|
20
|
+
announce?: (service: ToolBackgroundService) => ToolBackgroundService;
|
|
16
21
|
};
|
|
17
22
|
/**
|
|
18
23
|
* The turn to run: `turn` through the application's `prepare`, with the handler's own fields
|
|
19
|
-
* kept,
|
|
24
|
+
* kept, its credentials and steering after the application's, and the application's tool record
|
|
25
|
+
* and background service, when it gives them, joined with the session's.
|
|
20
26
|
*/
|
|
21
|
-
export declare function preparedTurn(prepare: TurnWiring['prepare'], turn: NormalizedTurnRequest & HandlerOwned, handler: HandlerTurnParts): Promise<TurnRequest>;
|
|
27
|
+
export declare function preparedTurn(prepare: TurnWiring['prepare'], turn: NormalizedTurnRequest & HandlerOwned & HandlerTools, handler: HandlerTurnParts): Promise<TurnRequest>;
|
|
22
28
|
/**
|
|
23
29
|
* The application's stage handler, then the handler's own when the application's asks for
|
|
24
30
|
* nothing. What the second one would have done waits for the next stage that allows it.
|
package/server/turn-wiring.js
CHANGED
|
@@ -1,32 +1,55 @@
|
|
|
1
1
|
/**
|
|
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
|
-
* first, and the handler's own run when the application's have nothing.
|
|
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
6
|
*
|
|
6
7
|
* @module
|
|
7
8
|
*/
|
|
9
|
+
import { layeredToolCoordinator } from "./tool-operations.js";
|
|
8
10
|
/**
|
|
9
11
|
* The turn to run: `turn` through the application's `prepare`, with the handler's own fields
|
|
10
|
-
* kept,
|
|
12
|
+
* kept, its credentials and steering after the application's, and the application's tool record
|
|
13
|
+
* and background service, when it gives them, joined with the session's.
|
|
11
14
|
*/
|
|
12
15
|
export async function preparedTurn(prepare, turn, handler) {
|
|
13
16
|
const app = prepare ? await prepare(turn) : turn;
|
|
14
17
|
const owned = {
|
|
15
18
|
profile: turn.profile,
|
|
16
|
-
...(turn.execution ? { execution: turn.execution } : {}),
|
|
17
19
|
...(turn.pageExecution ? { pageExecution: turn.pageExecution } : {}),
|
|
18
|
-
...(turn.background ? { background: turn.background } : {}),
|
|
19
20
|
...(turn.providerState ? { providerState: turn.providerState } : {}),
|
|
20
21
|
...(turn.signal ? { signal: turn.signal } : {}),
|
|
21
22
|
};
|
|
22
23
|
const onStage = stagesInOrder(app.onStage, handler.onStage);
|
|
24
|
+
const execution = toolsInOrder(app.execution, turn.execution);
|
|
25
|
+
const background = backgroundInOrder(app.background, turn.background, handler.announce);
|
|
23
26
|
return {
|
|
24
27
|
...app,
|
|
25
28
|
...owned,
|
|
29
|
+
...(execution ? { execution } : {}),
|
|
30
|
+
...(background ? { background } : {}),
|
|
26
31
|
credentials: credentialsInOrder(app.credentials, handler.credentials),
|
|
27
32
|
...(onStage ? { onStage } : {}),
|
|
28
33
|
};
|
|
29
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* The application's tool record, then the session's. The application's decides each call; the
|
|
37
|
+
* session's keeps the same call, so the page shows it and a resend reads it back.
|
|
38
|
+
*/
|
|
39
|
+
function toolsInOrder(app, session) {
|
|
40
|
+
if (!app || !session || app === session)
|
|
41
|
+
return session ?? app;
|
|
42
|
+
return layeredToolCoordinator(app, session);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The application's background service in place of the handler's, told to the open page as the
|
|
46
|
+
* handler's is. The handler's runs when the application gives none.
|
|
47
|
+
*/
|
|
48
|
+
function backgroundInOrder(app, own, announce) {
|
|
49
|
+
if (!app || app === own)
|
|
50
|
+
return own;
|
|
51
|
+
return announce ? announce(app) : app;
|
|
52
|
+
}
|
|
30
53
|
/**
|
|
31
54
|
* The application's stage handler, then the handler's own when the application's asks for
|
|
32
55
|
* nothing. What the second one would have done waits for the next stage that allows it.
|