@theoremjs/web 0.3.5-next.935.ga3dc485 → 0.3.5-next.938.g3cbd0dd
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 +10 -6
- package/package.json +2 -2
- package/server/conversation-inbox.d.ts +45 -0
- package/server/conversation-inbox.js +240 -0
- package/server/conversation-store.d.ts +9 -0
- package/server/conversation-store.js +7 -0
- package/server/conversation.d.ts +7 -12
- package/server/conversation.js +77 -49
- package/server/handler.d.ts +9 -2
- package/server/handler.js +238 -147
- package/server/mod.d.ts +3 -3
- package/server/mod.js +2 -2
- package/server/session-authority.js +3 -1
- package/server/session-store.d.ts +33 -2
- package/server/session-store.js +37 -2
- package/server/tell-every-result.d.ts +11 -0
- package/server/tell-every-result.js +17 -0
- package/server/tool-operations.js +11 -0
- package/server/page-wakes.d.ts +0 -10
- package/server/page-wakes.js +0 -8
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* @module
|
|
10
10
|
*/
|
|
11
11
|
import { z } from '@theoremjs/agents';
|
|
12
|
+
import type { Handover } from '@theoremjs/agents/conversation';
|
|
12
13
|
import type { ToolGateAuth } from '@theoremjs/agents/interface';
|
|
13
14
|
import type { AnsweredGate, ToolGate, ToolId, ToolOperationIdentity, ToolOperationOutcome, ToolOperationReply, ToolOperationUncertainty, TurnInput, TurnToolSnapshot } from '@theoremjs/agents/kernel';
|
|
14
15
|
import { type TheoremSessionCapacity } from './session-limits.ts';
|
|
@@ -61,8 +62,6 @@ export type SessionToolOperation = {
|
|
|
61
62
|
reply?: ToolOperationReply;
|
|
62
63
|
settledAt: number;
|
|
63
64
|
retainUntil?: number;
|
|
64
|
-
/** When a wake took this result up. invariant: one wake per result; a message still reads it. */
|
|
65
|
-
wokenAt?: number;
|
|
66
65
|
}) | (ToolOperationIdentity & {
|
|
67
66
|
status: 'uncertain';
|
|
68
67
|
pageGrant?: SessionPageGrant;
|
|
@@ -74,6 +73,30 @@ export type SessionToolOperation = {
|
|
|
74
73
|
/** The reply the model read for a call that detached, whatever has happened to the call since. */
|
|
75
74
|
export declare function detachedReply(operation: SessionToolOperation): ToolOperationReply | undefined;
|
|
76
75
|
export declare const DEFAULT_OPERATION_RETENTION_MS: number;
|
|
76
|
+
/** What the replies of one conversation did with one late result, until a saved reply tells it. */
|
|
77
|
+
export type SessionInboxEntry = {
|
|
78
|
+
/** The reply it is handed to now, while that reply's hold has the conversation. */
|
|
79
|
+
handed?: Handover;
|
|
80
|
+
/** Each reply it was handed to that ended without telling it, oldest first. */
|
|
81
|
+
handovers: Handover[];
|
|
82
|
+
/** The policy gave it up: no reply reads it. */
|
|
83
|
+
givenUp?: true;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* One conversation's hold, and what its replies did with the session's late results, by
|
|
87
|
+
* operation id. Whether a result was told is the transcript's to say.
|
|
88
|
+
*/
|
|
89
|
+
export type SessionConversationInbox = {
|
|
90
|
+
/** The hold that has the conversation until `leasedUntil` (epoch ms). */
|
|
91
|
+
hold?: string;
|
|
92
|
+
leasedUntil: number;
|
|
93
|
+
entries: Record<string, SessionInboxEntry>;
|
|
94
|
+
};
|
|
95
|
+
/** The session's conversation inboxes, and the version each result's end moves. */
|
|
96
|
+
export type SessionInbox = {
|
|
97
|
+
version: number;
|
|
98
|
+
conversations: Record<string, SessionConversationInbox>;
|
|
99
|
+
};
|
|
77
100
|
export type TheoremSessionState = {
|
|
78
101
|
version: 3;
|
|
79
102
|
operations: Record<string, SessionToolOperation>;
|
|
@@ -83,6 +106,8 @@ export type TheoremSessionState = {
|
|
|
83
106
|
gates: Record<string, PendingToolGate>;
|
|
84
107
|
/** Checkpoint identities this session produced, newest last. */
|
|
85
108
|
providerCheckpoints: string[];
|
|
109
|
+
/** Unset until a call first ends or a reply first holds a conversation. */
|
|
110
|
+
inbox?: SessionInbox;
|
|
86
111
|
};
|
|
87
112
|
/** Stored authority is checked before the handler reads or changes it. */
|
|
88
113
|
export declare const theoremSessionStateSchema: z.ZodType<TheoremSessionState>;
|
|
@@ -116,6 +141,12 @@ export declare function pruneGates<Gate extends {
|
|
|
116
141
|
createdAt: number;
|
|
117
142
|
expiresAt?: number;
|
|
118
143
|
}>(gates: Record<string, Gate>, now: number, ttlMs: number | undefined): Record<string, Gate>;
|
|
144
|
+
/**
|
|
145
|
+
* Drops the conversations no hold has with nothing kept.
|
|
146
|
+
*
|
|
147
|
+
* invariant: An entry outlives its result: a result the session dropped is still told, as unresolved.
|
|
148
|
+
*/
|
|
149
|
+
export declare function pruneInbox(state: TheoremSessionState, now: number): void;
|
|
119
150
|
export type MemorySessionStoreOptions = {
|
|
120
151
|
/** What it holds for one session. Unset, it holds any size. */
|
|
121
152
|
capacity?: TheoremSessionCapacity;
|
package/server/session-store.js
CHANGED
|
@@ -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. */
|
|
@@ -143,7 +143,6 @@ const operationSchema = z.discriminatedUnion('status', [
|
|
|
143
143
|
reply: toolOperationReplySchema.optional(),
|
|
144
144
|
settledAt: z.number().finite().nonnegative(),
|
|
145
145
|
retainUntil: z.number().finite().nonnegative().optional(),
|
|
146
|
-
wokenAt: z.number().finite().nonnegative().optional(),
|
|
147
146
|
}),
|
|
148
147
|
z.strictObject({
|
|
149
148
|
...identityFields,
|
|
@@ -155,6 +154,22 @@ const operationSchema = z.discriminatedUnion('status', [
|
|
|
155
154
|
detectedAt: z.number().finite().nonnegative(),
|
|
156
155
|
}),
|
|
157
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
|
+
});
|
|
158
173
|
/** Stored authority is checked before the handler reads or changes it. */
|
|
159
174
|
export const theoremSessionStateSchema = z
|
|
160
175
|
.strictObject({
|
|
@@ -163,6 +178,12 @@ export const theoremSessionStateSchema = z
|
|
|
163
178
|
permissions: z.array(z.string().min(1)),
|
|
164
179
|
gates: z.record(z.string().min(1), pendingSchema),
|
|
165
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(),
|
|
166
187
|
})
|
|
167
188
|
.superRefine((state, ctx) => {
|
|
168
189
|
for (const [operationId, operation] of Object.entries(state.operations)) {
|
|
@@ -197,6 +218,20 @@ export function pruneGates(gates, now, ttlMs) {
|
|
|
197
218
|
? now < gate.expiresAt
|
|
198
219
|
: !gateExpired(gate.createdAt, now, ttlMs)));
|
|
199
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* Drops the conversations no hold has with nothing kept.
|
|
223
|
+
*
|
|
224
|
+
* invariant: An entry outlives its result: a result the session dropped is still told, as unresolved.
|
|
225
|
+
*/
|
|
226
|
+
export function pruneInbox(state, now) {
|
|
227
|
+
if (!state.inbox)
|
|
228
|
+
return;
|
|
229
|
+
for (const [key, conversation] of Object.entries(state.inbox.conversations)) {
|
|
230
|
+
if (conversation.leasedUntil <= now && Object.keys(conversation.entries).length === 0) {
|
|
231
|
+
delete state.inbox.conversations[key];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
200
235
|
export function memoryStoreLimits(options) {
|
|
201
236
|
const { ttlMs, maxSessions } = options;
|
|
202
237
|
if ((ttlMs !== undefined && (!Number.isFinite(ttlMs) || ttlMs <= 0)) ||
|
|
@@ -0,0 +1,11 @@
|
|
|
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. A result two replies read without telling is given up, and reported once as
|
|
9
|
+
* `late_results_given_up`, so a model that keeps failing does not reply again and again.
|
|
10
|
+
*/
|
|
11
|
+
export declare const tellEveryResult: LateResultsDecider;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Replies that may read one result: the first, and one more after a reply that failed or lost the chat. */
|
|
2
|
+
const READS_PER_RESULT_COUNT = 2;
|
|
3
|
+
/**
|
|
4
|
+
* Each late result is told to the first reply that runs, and the page open on the chat wakes it
|
|
5
|
+
* when one waits. A result two replies read without telling is given up, and reported once as
|
|
6
|
+
* `late_results_given_up`, so a model that keeps failing does not reply again and again.
|
|
7
|
+
*/
|
|
8
|
+
export const tellEveryResult = {
|
|
9
|
+
policy: (items) => ({
|
|
10
|
+
tell: items
|
|
11
|
+
.filter((item) => item.handovers.length < READS_PER_RESULT_COUNT)
|
|
12
|
+
.map((item) => item.id),
|
|
13
|
+
giveUp: items
|
|
14
|
+
.filter((item) => item.handovers.length >= READS_PER_RESULT_COUNT)
|
|
15
|
+
.map((item) => item.id),
|
|
16
|
+
}),
|
|
17
|
+
};
|
|
@@ -73,6 +73,15 @@ function saveOutcome(state, identity, outcome) {
|
|
|
73
73
|
outcome,
|
|
74
74
|
settledAt,
|
|
75
75
|
};
|
|
76
|
+
if (reply)
|
|
77
|
+
movedInbox(state);
|
|
78
|
+
}
|
|
79
|
+
/** why: A detached call's end is an item in its conversation's inbox; a hold reads again before it lets go. */
|
|
80
|
+
function movedInbox(state) {
|
|
81
|
+
state.inbox = {
|
|
82
|
+
version: (state.inbox?.version ?? 0) + 1,
|
|
83
|
+
conversations: state.inbox?.conversations ?? {},
|
|
84
|
+
};
|
|
76
85
|
}
|
|
77
86
|
export function sessionToolCoordinator(store, sessionId) {
|
|
78
87
|
return {
|
|
@@ -150,6 +159,8 @@ export function sessionToolCoordinator(store, sessionId) {
|
|
|
150
159
|
reason,
|
|
151
160
|
detectedAt: Date.now(),
|
|
152
161
|
};
|
|
162
|
+
if (reply)
|
|
163
|
+
movedInbox(state);
|
|
153
164
|
});
|
|
154
165
|
},
|
|
155
166
|
};
|
package/server/page-wakes.d.ts
DELETED
|
@@ -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;
|
package/server/page-wakes.js
DELETED
|
@@ -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
|
-
};
|