@adecore/agent-contracts 0.65.2 → 0.66.1
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/dist/agent.js +2 -1
- package/dist/chat.d.ts +29 -3
- package/dist/chat.js +24 -13
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/model.js +2 -1
- package/dist/task.js +5 -4
- package/dist/tolerance.d.ts +67 -0
- package/dist/tolerance.js +11 -0
- package/dist/usage.js +4 -1
- package/dist/wire-stamps.d.ts +52 -0
- package/dist/wire-stamps.js +308 -0
- package/package.json +8 -3
- package/src/agent.ts +2 -1
- package/src/chat.ts +87 -39
- package/src/index.ts +21 -0
- package/src/model.ts +2 -1
- package/src/task.ts +5 -4
- package/src/tolerance.ts +104 -0
- package/src/usage.ts +4 -1
- package/src/wire-stamps.ts +472 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
// Keyed by the schema object, so the stamps hold whichever copy of zod built the schema.
|
|
3
|
+
const stamps = new WeakMap();
|
|
4
|
+
// Whether a schema has a stamp at or below it; a host may stamp its own schemas after these, so a new stamp starts it over.
|
|
5
|
+
let stampedBelow = new WeakMap();
|
|
6
|
+
function stamp(schema, value) {
|
|
7
|
+
stamps.set(schema, value);
|
|
8
|
+
stampedBelow = new WeakMap();
|
|
9
|
+
}
|
|
10
|
+
/*
|
|
11
|
+
* A union a newer host may add a member to, or a value inside a member. A member that does not read keeps its place as
|
|
12
|
+
* `{ [tag]: 'unknown', ...base, raw }` once its tag is a string and every field in `base` reads with the first member's schema.
|
|
13
|
+
*/
|
|
14
|
+
export function openUnion(schema, name, options) {
|
|
15
|
+
const members = optionsOf(schema);
|
|
16
|
+
if (members === null || members.some((member) => propValues(member, options.tag) === null)) {
|
|
17
|
+
throw new Error(`${name} cannot be an open union: every member needs a ${options.tag} literal`);
|
|
18
|
+
}
|
|
19
|
+
if (members.some((member) => propValues(member, options.tag)?.has('unknown'))) {
|
|
20
|
+
throw new Error(`${name} cannot be an open union: no member may be tagged unknown`);
|
|
21
|
+
}
|
|
22
|
+
stamp(schema, { open: 'union', name, tag: options.tag, base: [...options.base] });
|
|
23
|
+
return schema;
|
|
24
|
+
}
|
|
25
|
+
/* A list that leaves out an element it cannot read, so one newer entry does not empty the page it is on. */
|
|
26
|
+
export function lossyItems(schema) {
|
|
27
|
+
if (defOf(schema).type !== 'array') {
|
|
28
|
+
throw new Error('Only an array can leave out an element');
|
|
29
|
+
}
|
|
30
|
+
stamp(schema, { open: 'items' });
|
|
31
|
+
return schema;
|
|
32
|
+
}
|
|
33
|
+
/*
|
|
34
|
+
* A vocabulary a newer host may add a word to. A word this version does not know is kept as it came. `words` says how a
|
|
35
|
+
* typed client models the known ones: as `cases` beside an unknown one, or as plain `strings`. A string schema is already
|
|
36
|
+
* open, so it takes the stamp only to name its `known` words for such a client, which reads them as `strings`.
|
|
37
|
+
*/
|
|
38
|
+
export function openValues(schema, name, options = {}) {
|
|
39
|
+
const def = defOf(schema);
|
|
40
|
+
if (def.type === 'string' && options.known !== undefined) {
|
|
41
|
+
stamp(schema, { open: 'values', name, words: 'strings', known: [...options.known] });
|
|
42
|
+
return schema;
|
|
43
|
+
}
|
|
44
|
+
if (def.type !== 'enum' || def.entries === undefined) {
|
|
45
|
+
throw new Error(`${name} cannot take open values: only an enum can, or a string that names its known words`);
|
|
46
|
+
}
|
|
47
|
+
stamp(schema, { open: 'values', name, words: options.words ?? 'cases', known: [...(options.known ?? Object.values(def.entries).map(String))] });
|
|
48
|
+
return schema;
|
|
49
|
+
}
|
|
50
|
+
/* The stamp on this very schema object, for a generator that walks a schema itself. */
|
|
51
|
+
export function wireStampOf(schema) {
|
|
52
|
+
return stamps.get(schema);
|
|
53
|
+
}
|
|
54
|
+
/* Every stamp reachable from `schema`, with the path of the input it applies at (`[]` for an array element, `{}` for a record value). */
|
|
55
|
+
export function wireStampsIn(schema) {
|
|
56
|
+
const found = [];
|
|
57
|
+
const seen = new Set();
|
|
58
|
+
const visit = (node, path) => {
|
|
59
|
+
if (seen.has(node)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
seen.add(node);
|
|
63
|
+
const stamp = stamps.get(node);
|
|
64
|
+
if (stamp !== undefined) {
|
|
65
|
+
found.push({ path, stamp });
|
|
66
|
+
}
|
|
67
|
+
for (const [key, child] of childrenOf(node)) {
|
|
68
|
+
visit(child, key === null ? path : [...path, key]);
|
|
69
|
+
}
|
|
70
|
+
seen.delete(node);
|
|
71
|
+
};
|
|
72
|
+
visit(schema, []);
|
|
73
|
+
return found;
|
|
74
|
+
}
|
|
75
|
+
/*
|
|
76
|
+
* Reads what a host sent: strictly when that works, so a reply this version fully knows comes out exactly as a strict
|
|
77
|
+
* parse makes it. Otherwise each stamp degrades the nearest open container around what failed and the rest is read as
|
|
78
|
+
* strictly as before; what has no open container around it refuses the whole input, as a strict parse does.
|
|
79
|
+
*/
|
|
80
|
+
export function readStamped(schema, input) {
|
|
81
|
+
const strict = z.safeParse(schema, input);
|
|
82
|
+
if (strict.success) {
|
|
83
|
+
return { success: true, data: strict.data, degraded: [] };
|
|
84
|
+
}
|
|
85
|
+
const degraded = [];
|
|
86
|
+
const read = walk(schema, input, [], degraded);
|
|
87
|
+
return read === FAILED ? { success: false, error: strict.error } : { success: true, data: read.value, degraded };
|
|
88
|
+
}
|
|
89
|
+
const FAILED = Symbol('failed');
|
|
90
|
+
function defOf(schema) {
|
|
91
|
+
return schema._zod.def;
|
|
92
|
+
}
|
|
93
|
+
function optionsOf(schema) {
|
|
94
|
+
const def = defOf(schema);
|
|
95
|
+
return def.type === 'union' && def.options !== undefined ? def.options : null;
|
|
96
|
+
}
|
|
97
|
+
function propValues(member, tag) {
|
|
98
|
+
return member._zod.propValues?.[tag] ?? null;
|
|
99
|
+
}
|
|
100
|
+
// The schemas a value of `schema` is read with, by the key of the input they read (`null` for a wrapper of the same value).
|
|
101
|
+
function childrenOf(schema) {
|
|
102
|
+
const def = defOf(schema);
|
|
103
|
+
switch (def.type) {
|
|
104
|
+
case 'object':
|
|
105
|
+
return [...Object.entries(def.shape ?? {}), ...(def.catchall === undefined ? [] : [['{}', def.catchall]])];
|
|
106
|
+
case 'array':
|
|
107
|
+
return def.element === undefined ? [] : [['[]', def.element]];
|
|
108
|
+
case 'record':
|
|
109
|
+
return def.valueType === undefined ? [] : [['{}', def.valueType]];
|
|
110
|
+
case 'union':
|
|
111
|
+
return (def.options ?? []).map((option) => [null, option]);
|
|
112
|
+
case 'optional':
|
|
113
|
+
case 'nullable':
|
|
114
|
+
case 'default':
|
|
115
|
+
case 'prefault':
|
|
116
|
+
case 'readonly':
|
|
117
|
+
case 'nonoptional':
|
|
118
|
+
case 'catch':
|
|
119
|
+
return def.innerType === undefined ? [] : [[null, def.innerType]];
|
|
120
|
+
case 'lazy':
|
|
121
|
+
return def.getter === undefined ? [] : [[null, def.getter()]];
|
|
122
|
+
default:
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function hasStamp(schema) {
|
|
127
|
+
const known = stampedBelow.get(schema);
|
|
128
|
+
if (known !== undefined) {
|
|
129
|
+
return known;
|
|
130
|
+
}
|
|
131
|
+
const result = wireStampsIn(schema).length > 0;
|
|
132
|
+
stampedBelow.set(schema, result);
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
function strictly(schema, input) {
|
|
136
|
+
const parsed = z.safeParse(schema, input);
|
|
137
|
+
return parsed.success ? { value: parsed.data } : FAILED;
|
|
138
|
+
}
|
|
139
|
+
function isRecord(value) {
|
|
140
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
141
|
+
}
|
|
142
|
+
function walk(schema, input, path, degraded) {
|
|
143
|
+
if (!hasStamp(schema)) {
|
|
144
|
+
return strictly(schema, input);
|
|
145
|
+
}
|
|
146
|
+
const stamp = stamps.get(schema);
|
|
147
|
+
if (stamp?.open === 'values') {
|
|
148
|
+
const read = strictly(schema, input);
|
|
149
|
+
// A string schema takes any word itself, so only an enum has a word to keep that a strict parse refused.
|
|
150
|
+
if (read !== FAILED || typeof input !== 'string' || defOf(schema).type !== 'enum') {
|
|
151
|
+
return read;
|
|
152
|
+
}
|
|
153
|
+
degraded.push({ kind: 'unknown-word', path, name: stamp.name, word: input });
|
|
154
|
+
return { value: input };
|
|
155
|
+
}
|
|
156
|
+
if (stamp?.open === 'union') {
|
|
157
|
+
return walkOpenUnion(schema, stamp, input, path, degraded);
|
|
158
|
+
}
|
|
159
|
+
const def = defOf(schema);
|
|
160
|
+
// A check over a whole container cannot run on a value with a degraded part, so such a container stays strict.
|
|
161
|
+
if ((def.checks?.length ?? 0) > 0) {
|
|
162
|
+
return strictly(schema, input);
|
|
163
|
+
}
|
|
164
|
+
switch (def.type) {
|
|
165
|
+
case 'object':
|
|
166
|
+
return walkObject(def, input, path, degraded);
|
|
167
|
+
case 'array':
|
|
168
|
+
// A list of an open union leaves out a member without the fields every member shares, which has no place to keep.
|
|
169
|
+
return walkArray(def, stamp?.open === 'items' || (def.element !== undefined && stamps.get(def.element)?.open === 'union'), input, path, degraded);
|
|
170
|
+
case 'record':
|
|
171
|
+
return walkRecord(def, input, path, degraded);
|
|
172
|
+
case 'union':
|
|
173
|
+
return walkUnion(def, input, path, degraded);
|
|
174
|
+
case 'optional':
|
|
175
|
+
return input === undefined ? { value: undefined } : walk(def.innerType, input, path, degraded);
|
|
176
|
+
case 'nullable':
|
|
177
|
+
return input === null ? { value: null } : walk(def.innerType, input, path, degraded);
|
|
178
|
+
case 'default':
|
|
179
|
+
case 'prefault':
|
|
180
|
+
return input === undefined ? strictly(schema, input) : walk(def.innerType, input, path, degraded);
|
|
181
|
+
case 'readonly':
|
|
182
|
+
case 'nonoptional':
|
|
183
|
+
return walk(def.innerType, input, path, degraded);
|
|
184
|
+
case 'lazy':
|
|
185
|
+
return walk(def.getter(), input, path, degraded);
|
|
186
|
+
default:
|
|
187
|
+
return strictly(schema, input);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function walkOpenUnion(schema, stamp, input, path, degraded) {
|
|
191
|
+
const word = isRecord(input) ? input[stamp.tag] : undefined;
|
|
192
|
+
if (!isRecord(input) || typeof word !== 'string') {
|
|
193
|
+
return FAILED;
|
|
194
|
+
}
|
|
195
|
+
const members = optionsOf(schema) ?? [];
|
|
196
|
+
const member = members.find((candidate) => propValues(candidate, stamp.tag)?.has(word));
|
|
197
|
+
if (member !== undefined) {
|
|
198
|
+
const mark = degraded.length;
|
|
199
|
+
const read = walk(member, input, path, degraded);
|
|
200
|
+
if (read !== FAILED) {
|
|
201
|
+
return read;
|
|
202
|
+
}
|
|
203
|
+
degraded.length = mark;
|
|
204
|
+
}
|
|
205
|
+
const shape = defOf(members[0]).shape ?? {};
|
|
206
|
+
const base = {};
|
|
207
|
+
for (const key of stamp.base) {
|
|
208
|
+
const field = shape[key];
|
|
209
|
+
const read = field === undefined || !(key in input) ? FAILED : strictly(field, input[key]);
|
|
210
|
+
if (read === FAILED) {
|
|
211
|
+
return FAILED;
|
|
212
|
+
}
|
|
213
|
+
base[key] = read.value;
|
|
214
|
+
}
|
|
215
|
+
degraded.push({ kind: 'unknown-member', path, name: stamp.name, tag: word });
|
|
216
|
+
return { value: { ...base, [stamp.tag]: 'unknown', raw: input } };
|
|
217
|
+
}
|
|
218
|
+
function walkObject(def, input, path, degraded) {
|
|
219
|
+
if (!isRecord(input)) {
|
|
220
|
+
return FAILED;
|
|
221
|
+
}
|
|
222
|
+
const shape = def.shape ?? {};
|
|
223
|
+
const output = {};
|
|
224
|
+
for (const [key, field] of Object.entries(shape)) {
|
|
225
|
+
const present = key in input;
|
|
226
|
+
// As zod does: an absent optional key adds nothing, not even an `undefined`.
|
|
227
|
+
if (!present && field._zod.optin === 'optional' && field._zod.optout === 'optional') {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const read = walk(field, input[key], [...path, key], degraded);
|
|
231
|
+
if (read === FAILED) {
|
|
232
|
+
return FAILED;
|
|
233
|
+
}
|
|
234
|
+
if (read.value !== undefined || present || field._zod.optin === 'defaulted') {
|
|
235
|
+
output[key] = read.value;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (def.catchall === undefined) {
|
|
239
|
+
return { value: output };
|
|
240
|
+
}
|
|
241
|
+
const catchall = defOf(def.catchall).type;
|
|
242
|
+
for (const [key, value] of Object.entries(input)) {
|
|
243
|
+
if (Object.hasOwn(shape, key) || key === '__proto__') {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (catchall === 'never') {
|
|
247
|
+
return FAILED;
|
|
248
|
+
}
|
|
249
|
+
const read = walk(def.catchall, value, [...path, key], degraded);
|
|
250
|
+
if (read === FAILED) {
|
|
251
|
+
return FAILED;
|
|
252
|
+
}
|
|
253
|
+
output[key] = read.value;
|
|
254
|
+
}
|
|
255
|
+
return { value: output };
|
|
256
|
+
}
|
|
257
|
+
function walkArray(def, lossy, input, path, degraded) {
|
|
258
|
+
if (!Array.isArray(input) || def.element === undefined) {
|
|
259
|
+
return FAILED;
|
|
260
|
+
}
|
|
261
|
+
const output = [];
|
|
262
|
+
for (const [i, element] of input.entries()) {
|
|
263
|
+
// An element left out takes back what it degraded, so the list only names the element itself.
|
|
264
|
+
const mark = degraded.length;
|
|
265
|
+
const read = walk(def.element, element, [...path, i], degraded);
|
|
266
|
+
if (read !== FAILED) {
|
|
267
|
+
output.push(read.value);
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (!lossy) {
|
|
271
|
+
return FAILED;
|
|
272
|
+
}
|
|
273
|
+
degraded.length = mark;
|
|
274
|
+
degraded.push({ kind: 'dropped-element', path: [...path, i] });
|
|
275
|
+
}
|
|
276
|
+
return { value: output };
|
|
277
|
+
}
|
|
278
|
+
function walkRecord(def, input, path, degraded) {
|
|
279
|
+
if (!isRecord(input) || def.keyType === undefined || def.valueType === undefined) {
|
|
280
|
+
return FAILED;
|
|
281
|
+
}
|
|
282
|
+
const output = {};
|
|
283
|
+
for (const [key, value] of Object.entries(input)) {
|
|
284
|
+
const name = strictly(def.keyType, key);
|
|
285
|
+
const read = name === FAILED ? FAILED : walk(def.valueType, value, [...path, key], degraded);
|
|
286
|
+
if (name === FAILED || read === FAILED) {
|
|
287
|
+
return FAILED;
|
|
288
|
+
}
|
|
289
|
+
output[String(name.value)] = read.value;
|
|
290
|
+
}
|
|
291
|
+
return { value: output };
|
|
292
|
+
}
|
|
293
|
+
function walkUnion(def, input, path, degraded) {
|
|
294
|
+
const tag = def.discriminator;
|
|
295
|
+
const word = tag !== undefined && isRecord(input) ? input[tag] : undefined;
|
|
296
|
+
for (const option of def.options ?? []) {
|
|
297
|
+
if (tag !== undefined && !propValues(option, tag)?.has(word)) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
const mark = degraded.length;
|
|
301
|
+
const read = walk(option, input, path, degraded);
|
|
302
|
+
if (read !== FAILED) {
|
|
303
|
+
return read;
|
|
304
|
+
}
|
|
305
|
+
degraded.length = mark;
|
|
306
|
+
}
|
|
307
|
+
return FAILED;
|
|
308
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adecore/agent-contracts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.66.1",
|
|
4
4
|
"description": "Schemas and port contracts for agent chats, provider accounts, models and usage.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -59,6 +59,11 @@
|
|
|
59
59
|
"types": "./dist/text.d.ts",
|
|
60
60
|
"default": "./dist/text.js"
|
|
61
61
|
},
|
|
62
|
+
"./tolerance": {
|
|
63
|
+
"source": "./src/tolerance.ts",
|
|
64
|
+
"types": "./dist/tolerance.d.ts",
|
|
65
|
+
"default": "./dist/tolerance.js"
|
|
66
|
+
},
|
|
62
67
|
"./usage": {
|
|
63
68
|
"source": "./src/usage.ts",
|
|
64
69
|
"types": "./dist/usage.d.ts",
|
|
@@ -81,8 +86,8 @@
|
|
|
81
86
|
"test": "bun --conditions=source test"
|
|
82
87
|
},
|
|
83
88
|
"dependencies": {
|
|
84
|
-
"@adecore/intelligent-ui": "0.
|
|
85
|
-
"@adecore/utils": "0.
|
|
89
|
+
"@adecore/intelligent-ui": "0.66.1",
|
|
90
|
+
"@adecore/utils": "0.66.1",
|
|
86
91
|
"zod": "^4.6.7"
|
|
87
92
|
},
|
|
88
93
|
"devDependencies": {
|
package/src/agent.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { SessionIdSchema } from './ids.ts';
|
|
3
|
+
import { openValues } from './wire-stamps.ts';
|
|
3
4
|
|
|
4
5
|
export const AGENT_KIND_MAX_LENGTH = 32;
|
|
5
6
|
|
|
@@ -18,7 +19,7 @@ export type AgentKind = string;
|
|
|
18
19
|
|
|
19
20
|
// `exited` is the daemon's own reading, not a hook's: the CLI went down with its shell without
|
|
20
21
|
// ever reporting an end, so the node offers a resume instead of a status that stays on running.
|
|
21
|
-
export const AgentStatusSchema = z.enum(['running', 'needs-you', 'idle', 'error', 'exited']);
|
|
22
|
+
export const AgentStatusSchema = openValues(z.enum(['running', 'needs-you', 'idle', 'error', 'exited']), 'AgentStatus', { words: 'strings' });
|
|
22
23
|
export type AgentStatus = z.infer<typeof AgentStatusSchema>;
|
|
23
24
|
|
|
24
25
|
// Past this a model's name for a session is a summary, not a title.
|
package/src/chat.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { AgentKindSchema, AgentStatusSchema, SuggestedTitleSchema } from './agen
|
|
|
5
5
|
import { ModelSelectionSchema, RuntimeModeSchema } from './model.ts';
|
|
6
6
|
import { ProviderAccountIdSchema } from './provider-accounts.ts';
|
|
7
7
|
import { ChatVisualsSchema } from './visual.ts';
|
|
8
|
+
import { lossyItems, openUnion, openValues } from './wire-stamps.ts';
|
|
8
9
|
import { WorktreeSchema } from './worktree.ts';
|
|
9
10
|
|
|
10
11
|
// The client picks the id (its node id), like a terminal session.
|
|
@@ -160,7 +161,7 @@ export type ChatQueuedMessage = z.infer<typeof ChatQueuedMessageSchema>;
|
|
|
160
161
|
export const ChatBackgroundTaskSchema = z.object({
|
|
161
162
|
// The CLI's own id for the task, which is what stopping it names.
|
|
162
163
|
id: z.string().min(1),
|
|
163
|
-
kind: z.enum(['shell', 'monitor']),
|
|
164
|
+
kind: openValues(z.enum(['shell', 'monitor']), 'ChatBackgroundTaskKind'),
|
|
164
165
|
description: z.string(),
|
|
165
166
|
command: z.string().nullable(),
|
|
166
167
|
startedAt: z.number()
|
|
@@ -172,7 +173,7 @@ export type ChatBackgroundTask = z.infer<typeof ChatBackgroundTaskSchema>;
|
|
|
172
173
|
* too busy to answer. Not a state of its own, since a client that validates `state` knows four.
|
|
173
174
|
*/
|
|
174
175
|
export const ChatTurnLimitSchema = z.object({
|
|
175
|
-
kind: z.enum(['usage', 'overload']),
|
|
176
|
+
kind: openValues(z.enum(['usage', 'overload']), 'ChatTurnLimitKind'),
|
|
176
177
|
// When the limit lifts, in milliseconds since the epoch; absent when the CLI named no time.
|
|
177
178
|
resetsAt: z.number().optional()
|
|
178
179
|
});
|
|
@@ -184,7 +185,7 @@ export type ChatTurnLimit = z.infer<typeof ChatTurnLimitSchema>;
|
|
|
184
185
|
* a new value inside one it already knew. `usage-limited` and `budget-limited` are Codex's: the plan's limit,
|
|
185
186
|
* or the token budget the goal was given.
|
|
186
187
|
*/
|
|
187
|
-
export const ChatGoalStatusSchema = z.enum(['active', 'paused', 'blocked', 'usage-limited', 'budget-limited', 'complete']);
|
|
188
|
+
export const ChatGoalStatusSchema = openValues(z.enum(['active', 'paused', 'blocked', 'usage-limited', 'budget-limited', 'complete']), 'ChatGoalStatus');
|
|
188
189
|
export type ChatGoalStatus = z.infer<typeof ChatGoalStatusSchema>;
|
|
189
190
|
|
|
190
191
|
export const ChatGoalSchema = z.object({
|
|
@@ -209,7 +210,8 @@ export const ChatQuestionSchema = z.object({
|
|
|
209
210
|
});
|
|
210
211
|
export type ChatQuestion = z.infer<typeof ChatQuestionSchema>;
|
|
211
212
|
|
|
212
|
-
|
|
213
|
+
// A request of a kind this version does not know still says a chat waits on a person, and is never answered from here.
|
|
214
|
+
export const ChatRequestKindSchema = openValues(z.enum(['approval', 'question']), 'ChatRequestKind', { words: 'strings' });
|
|
213
215
|
export type ChatRequestKind = z.infer<typeof ChatRequestKindSchema>;
|
|
214
216
|
|
|
215
217
|
/*
|
|
@@ -335,9 +337,16 @@ export function holdsConversation(info: Pick<ChatInfo, 'agentSessionId' | 'activ
|
|
|
335
337
|
*/
|
|
336
338
|
export type ChatActivity = 'waiting' | 'working' | 'delegating' | 'background' | 'holding' | 'settled';
|
|
337
339
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
340
|
+
// Only compares, so a client's info with a newer machine's words in it reads here as well.
|
|
341
|
+
export function chatActivity(info: {
|
|
342
|
+
readonly status: string;
|
|
343
|
+
readonly delegating?: boolean;
|
|
344
|
+
readonly background?: readonly unknown[];
|
|
345
|
+
readonly requests?: readonly unknown[];
|
|
346
|
+
readonly goal?: { readonly status: string };
|
|
347
|
+
readonly queue?: readonly unknown[];
|
|
348
|
+
readonly activeTurnId?: string | null;
|
|
349
|
+
}): ChatActivity {
|
|
341
350
|
if (info.status === 'needs-you') {
|
|
342
351
|
return 'waiting';
|
|
343
352
|
}
|
|
@@ -799,19 +808,35 @@ export const ChatCompactionItemSchema = z.object({
|
|
|
799
808
|
* native client validates `chat.attach` and `chat.history` whole, so one item it does not know rejects
|
|
800
809
|
* the entire conversation. Add optional fields instead.
|
|
801
810
|
*/
|
|
802
|
-
export const ChatItemSchema =
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
811
|
+
export const ChatItemSchema = openUnion(
|
|
812
|
+
z.discriminatedUnion('kind', [
|
|
813
|
+
ChatUserItemSchema,
|
|
814
|
+
ChatAssistantItemSchema,
|
|
815
|
+
ChatThinkingItemSchema,
|
|
816
|
+
ChatToolItemSchema,
|
|
817
|
+
ChatSubagentItemSchema,
|
|
818
|
+
ChatApprovalItemSchema,
|
|
819
|
+
ChatQuestionItemSchema,
|
|
820
|
+
ChatTurnItemSchema,
|
|
821
|
+
ChatNoteItemSchema,
|
|
822
|
+
ChatCompactionItemSchema
|
|
823
|
+
]),
|
|
824
|
+
'ChatItem',
|
|
825
|
+
{ tag: 'kind', base: ['id', 'createdAt', 'turnId'] }
|
|
826
|
+
);
|
|
814
827
|
export type ChatItem = z.infer<typeof ChatItemSchema>;
|
|
828
|
+
|
|
829
|
+
/*
|
|
830
|
+
* An item of a newer host that this version cannot read, as `readTolerant` keeps it in its place: the fields every item
|
|
831
|
+
* has and the item as it came. No host sends one, and nothing waits on, answers, forks from or writes back such an item.
|
|
832
|
+
*/
|
|
833
|
+
export interface ChatUnknownItem {
|
|
834
|
+
readonly kind: 'unknown';
|
|
835
|
+
readonly id: string;
|
|
836
|
+
readonly createdAt: number;
|
|
837
|
+
readonly turnId: string | null;
|
|
838
|
+
readonly raw: Readonly<Record<string, unknown>>;
|
|
839
|
+
}
|
|
815
840
|
export type ChatUserItem = z.infer<typeof ChatUserItemSchema>;
|
|
816
841
|
export type ChatAssistantItem = z.infer<typeof ChatAssistantItemSchema>;
|
|
817
842
|
export type ChatThinkingItem = z.infer<typeof ChatThinkingItemSchema>;
|
|
@@ -824,19 +849,30 @@ export type ChatNoteItem = z.infer<typeof ChatNoteItemSchema>;
|
|
|
824
849
|
export type ChatNoteMessage = z.infer<typeof ChatNoteMessageSchema>;
|
|
825
850
|
export type ChatCompactionItem = z.infer<typeof ChatCompactionItemSchema>;
|
|
826
851
|
|
|
852
|
+
// An item as a host holds it or as a client reads it, where a newer host's item may be unknown or carry a newer word.
|
|
853
|
+
type ThreadItem = { readonly kind: string; readonly createdAt: number };
|
|
854
|
+
|
|
855
|
+
function isSubagentItem(item: ThreadItem): item is ChatSubagentItem {
|
|
856
|
+
return item.kind === 'subagent';
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
function isToolItem(item: ThreadItem): item is ChatToolItem {
|
|
860
|
+
return item.kind === 'tool';
|
|
861
|
+
}
|
|
862
|
+
|
|
827
863
|
/*
|
|
828
864
|
* The sub-agents a chat still speaks of beside its thread: every one at work, and the ones that settled
|
|
829
865
|
* since the last message, so a finished batch stays until the next message is sent. That includes an
|
|
830
866
|
* older row a message woke again. A settled one from before that message stays too while a sibling of
|
|
831
867
|
* its turn still runs. Older ones are only in the thread. `items` is in thread order.
|
|
832
868
|
*/
|
|
833
|
-
export function activeSubagents(items: readonly
|
|
869
|
+
export function activeSubagents(items: readonly ThreadItem[]): ChatSubagentItem[] {
|
|
834
870
|
const lastMessage = items.findLastIndex((item) => item.kind === 'user');
|
|
835
871
|
const lastMessageAt = lastMessage < 0 ? null : items[lastMessage]!.createdAt;
|
|
836
872
|
const turns = new Set<string>();
|
|
837
873
|
|
|
838
874
|
for (const item of items) {
|
|
839
|
-
if (item
|
|
875
|
+
if (isSubagentItem(item) && item.status === 'running' && item.turnId !== null) {
|
|
840
876
|
turns.add(item.turnId);
|
|
841
877
|
}
|
|
842
878
|
}
|
|
@@ -847,7 +883,7 @@ export function activeSubagents(items: readonly ChatItem[]): ChatSubagentItem[]
|
|
|
847
883
|
const item = items[index]!;
|
|
848
884
|
|
|
849
885
|
if (
|
|
850
|
-
item
|
|
886
|
+
isSubagentItem(item) &&
|
|
851
887
|
(item.status === 'running' ||
|
|
852
888
|
index > lastMessage ||
|
|
853
889
|
(lastMessageAt !== null && item.finishedAt !== null && item.finishedAt > lastMessageAt) ||
|
|
@@ -879,12 +915,12 @@ export function workflowEndedAt(workflow: ChatWorkflow): number | null {
|
|
|
879
915
|
* one that runs, and the ones that ended since the last message, so a finished workflow stays until the
|
|
880
916
|
* next message is sent. Only a call that reported what it runs counts. `items` is in thread order.
|
|
881
917
|
*/
|
|
882
|
-
export function activeWorkflows(items: readonly
|
|
918
|
+
export function activeWorkflows(items: readonly ThreadItem[]): ChatToolItem[] {
|
|
883
919
|
const lastMessage = items.findLastIndex((item) => item.kind === 'user');
|
|
884
920
|
const lastMessageAt = lastMessage < 0 ? null : items[lastMessage]!.createdAt;
|
|
885
921
|
|
|
886
922
|
return items.filter((item, index): item is ChatToolItem => {
|
|
887
|
-
if (item
|
|
923
|
+
if (!isToolItem(item) || item.name !== 'Workflow' || item.workflow === undefined) {
|
|
888
924
|
return false;
|
|
889
925
|
}
|
|
890
926
|
|
|
@@ -937,21 +973,31 @@ export function outlineItems(items: readonly ChatItem[], marked: ReadonlySet<str
|
|
|
937
973
|
}
|
|
938
974
|
|
|
939
975
|
// Events apply after the `chat.attach` snapshot; item ids make upserts and streamed deltas replayable.
|
|
940
|
-
export const ChatEventSchema =
|
|
941
|
-
z.
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
976
|
+
export const ChatEventSchema = openUnion(
|
|
977
|
+
z.discriminatedUnion('type', [
|
|
978
|
+
z.object({ type: z.literal('item'), item: ChatItemSchema, historyIndex: z.number().int().nonnegative().optional() }),
|
|
979
|
+
z.object({
|
|
980
|
+
type: z.literal('delta'),
|
|
981
|
+
itemId: z.string(),
|
|
982
|
+
text: z.string(),
|
|
983
|
+
ui: UiBlocksSchema.optional(),
|
|
984
|
+
uiQueries: ChatUiQueryStateSchema.optional(),
|
|
985
|
+
textLength: z.number().int().nonnegative().optional()
|
|
986
|
+
}),
|
|
987
|
+
z.object({ type: z.literal('info'), info: ChatInfoSchema }),
|
|
988
|
+
z.object({ type: z.literal('reset'), info: ChatInfoSchema, items: z.array(ChatItemSchema) })
|
|
989
|
+
]),
|
|
990
|
+
'ChatEvent',
|
|
991
|
+
{ tag: 'type', base: [] }
|
|
992
|
+
);
|
|
953
993
|
export type ChatEvent = z.infer<typeof ChatEventSchema>;
|
|
954
994
|
|
|
995
|
+
/* An event of a newer host that this version cannot read, as `readTolerant` keeps it; a thread leaves it unapplied. */
|
|
996
|
+
export interface ChatUnknownEvent {
|
|
997
|
+
readonly type: 'unknown';
|
|
998
|
+
readonly raw: Readonly<Record<string, unknown>>;
|
|
999
|
+
}
|
|
1000
|
+
|
|
955
1001
|
export const ChatEventEnvelopeSchema = z.object({
|
|
956
1002
|
chatId: ChatIdSchema,
|
|
957
1003
|
event: ChatEventSchema,
|
|
@@ -1069,7 +1115,9 @@ export const ChatSubagentResultSchema = z.object({
|
|
|
1069
1115
|
items: z.array(ChatItemSchema),
|
|
1070
1116
|
history: ChatSubagentPageSchema,
|
|
1071
1117
|
// A request still waiting must remain answerable after it leaves the newest page.
|
|
1072
|
-
pending: z
|
|
1118
|
+
pending: z
|
|
1119
|
+
.array(openUnion(z.union([ChatApprovalItemSchema, ChatQuestionItemSchema]), 'ChatPendingRequest', { tag: 'kind', base: ['id', 'createdAt', 'turnId'] }))
|
|
1120
|
+
.optional(),
|
|
1073
1121
|
source: ChatSubagentSourceSchema,
|
|
1074
1122
|
context: z.object({ provider: AgentKindSchema, cwd: z.string(), chatId: ChatIdSchema.optional() }).optional(),
|
|
1075
1123
|
// Whether the subagent is still writing, so a client knows to keep reading.
|
|
@@ -1353,7 +1401,7 @@ export const ChatForkInfoResultSchema = z.object({
|
|
|
1353
1401
|
export type ChatForkInfoResult = z.infer<typeof ChatForkInfoResultSchema>;
|
|
1354
1402
|
|
|
1355
1403
|
export const ChatListResultSchema = z.object({
|
|
1356
|
-
chats: z.array(ChatInfoSchema)
|
|
1404
|
+
chats: lossyItems(z.array(ChatInfoSchema))
|
|
1357
1405
|
});
|
|
1358
1406
|
export type ChatListResult = z.infer<typeof ChatListResultSchema>;
|
|
1359
1407
|
|
package/src/index.ts
CHANGED
|
@@ -152,8 +152,10 @@ export {
|
|
|
152
152
|
type ChatNoteItem,
|
|
153
153
|
type ChatNoteMessage,
|
|
154
154
|
type ChatCompactionItem,
|
|
155
|
+
type ChatUnknownItem,
|
|
155
156
|
ChatEventSchema,
|
|
156
157
|
type ChatEvent,
|
|
158
|
+
type ChatUnknownEvent,
|
|
157
159
|
ChatEventEnvelopeSchema,
|
|
158
160
|
type ChatEventEnvelope,
|
|
159
161
|
ChatCreatePayloadSchema,
|
|
@@ -372,6 +374,25 @@ export {
|
|
|
372
374
|
type TaskChangedEvent
|
|
373
375
|
} from './task.ts';
|
|
374
376
|
export { clipText, TOOL_OUTPUT_TAIL, appendToolOutput } from './text.ts';
|
|
377
|
+
export {
|
|
378
|
+
openUnion,
|
|
379
|
+
lossyItems,
|
|
380
|
+
openValues,
|
|
381
|
+
wireStampOf,
|
|
382
|
+
wireStampsIn,
|
|
383
|
+
type WireStamp,
|
|
384
|
+
type WireStampAt,
|
|
385
|
+
type WireDegradation,
|
|
386
|
+
readTolerant,
|
|
387
|
+
type ToleratedRead,
|
|
388
|
+
type Tolerant,
|
|
389
|
+
type TolerantWith,
|
|
390
|
+
type OpenUnionTable,
|
|
391
|
+
type OpenWordTable,
|
|
392
|
+
type OpenUnions,
|
|
393
|
+
type OpenWords,
|
|
394
|
+
type UnknownWord
|
|
395
|
+
} from './tolerance.ts';
|
|
375
396
|
export { type ToolFileEdit, type ToolFileEditPiece, toolFileEdit } from './tool-edits.ts';
|
|
376
397
|
export {
|
|
377
398
|
UsageProviderSchema,
|
package/src/model.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { AgentKindSchema, type AgentKind } from './agent.ts';
|
|
3
|
+
import { openValues } from './wire-stamps.ts';
|
|
3
4
|
|
|
4
5
|
// Options a model exposes (effort, context window, thinking). Generic descriptors rather than
|
|
5
6
|
// per-provider enums, so a new model needs a catalog entry and no code change.
|
|
@@ -93,7 +94,7 @@ export const ModelCatalogDataSchema = z
|
|
|
93
94
|
export type ModelCatalogData = z.infer<typeof ModelCatalogDataSchema>;
|
|
94
95
|
|
|
95
96
|
// The thread's permission policy, one vocabulary for every provider; each adapter maps it.
|
|
96
|
-
export const RuntimeModeSchema = z.enum(['supervised', 'auto-accept-edits', 'auto', 'full-access']);
|
|
97
|
+
export const RuntimeModeSchema = openValues(z.enum(['supervised', 'auto-accept-edits', 'auto', 'full-access']), 'RuntimeMode', { words: 'strings' });
|
|
97
98
|
export type RuntimeMode = z.infer<typeof RuntimeModeSchema>;
|
|
98
99
|
|
|
99
100
|
// What a provider's CLI can do, so a client never offers what it would drop and the daemon
|
package/src/task.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { openValues } from './wire-stamps.ts';
|
|
2
3
|
|
|
3
|
-
export const TaskStatusSchema = z.enum(['open', 'done', 'failed', 'cancelled']);
|
|
4
|
+
export const TaskStatusSchema = openValues(z.enum(['open', 'done', 'failed', 'cancelled']), 'TaskStatus');
|
|
4
5
|
export type TaskStatus = z.infer<typeof TaskStatusSchema>;
|
|
5
6
|
|
|
6
7
|
export const TaskResultSchema = z.object({
|
|
7
8
|
text: z.string(),
|
|
8
9
|
// How the result came about: the child called `done`, its first turn ended, or its process left.
|
|
9
|
-
source: z.enum(['done', 'turn', 'exit']),
|
|
10
|
+
source: openValues(z.enum(['done', 'turn', 'exit']), 'TaskResultSource'),
|
|
10
11
|
at: z.number()
|
|
11
12
|
});
|
|
12
13
|
export type TaskResult = z.infer<typeof TaskResultSchema>;
|
|
@@ -31,9 +32,9 @@ export const TaskSchema = z.object({
|
|
|
31
32
|
createdAt: z.number(),
|
|
32
33
|
settledAt: z.number().nullable(),
|
|
33
34
|
// Whether the parent has been woken about this task yet; `none` for a task that never wakes anybody.
|
|
34
|
-
wake: z.enum(['pending', 'sent', 'none']),
|
|
35
|
+
wake: openValues(z.enum(['pending', 'sent', 'none']), 'TaskWake'),
|
|
35
36
|
// Set while the child's last turn stopped on a limit: the task stays open and wakes nobody, `until` the limit lifts when known.
|
|
36
|
-
paused: z.object({ kind: z.enum(['usage', 'overload']), until: z.number().optional() }).optional(),
|
|
37
|
+
paused: z.object({ kind: openValues(z.enum(['usage', 'overload']), 'TaskPauseKind'), until: z.number().optional() }).optional(),
|
|
37
38
|
// The machine the child runs on, when that is not this one: no chat here observes it, so its result arrives through `settleFromElsewhere`.
|
|
38
39
|
machine: z.string().min(1).optional()
|
|
39
40
|
});
|