@noetic-tools/core 5.15.1 → 5.16.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/builders/custom-item.d.ts +37 -0
- package/dist/builders/custom-item.js +118 -0
- package/dist/builders/decide-builders.d.ts +1 -1
- package/dist/builders/dynamic-workflow.js +1 -1
- package/dist/builders/output-text.d.ts +5 -0
- package/dist/builders/output-text.js +13 -0
- package/dist/builders/provide-builder.d.ts +1 -1
- package/dist/builders/spawn-builder.d.ts +1 -1
- package/dist/builders/step-builders.d.ts +8 -7
- package/dist/builders/step-builders.js +2 -0
- package/dist/builders/workflow-custom-items.d.ts +38 -0
- package/dist/builders/workflow-custom-items.js +118 -0
- package/dist/builders/workflow-hydrator.d.ts +9 -2
- package/dist/builders/workflow-hydrator.js +41 -71
- package/dist/builders/workflow-step.d.ts +23 -5
- package/dist/builders/workflow-step.js +28 -13
- package/dist/expression/check.js +9 -5
- package/dist/harness/agent-harness.d.ts +19 -9
- package/dist/harness/agent-harness.js +18 -5
- package/dist/harness/deps/interpreter.d.ts +1 -0
- package/dist/harness/deps/interpreter.js +1 -0
- package/dist/harness/deps/runtime.d.ts +1 -0
- package/dist/harness/deps/runtime.js +1 -0
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +6 -0
- package/dist/interpreter/emit-items.d.ts +38 -8
- package/dist/interpreter/emit-items.js +267 -17
- package/dist/interpreter/execute-action.js +3 -1
- package/dist/interpreter/execute.js +4 -1
- package/dist/runtime/context-impl.d.ts +13 -1
- package/dist/runtime/context-impl.js +30 -0
- package/dist/runtime/durable/harness-checkpoints.js +21 -1
- package/dist/runtime/item-gates.d.ts +8 -0
- package/dist/runtime/item-gates.js +15 -0
- package/dist/runtime/item-log-impl.d.ts +7 -1
- package/dist/runtime/item-log-impl.js +8 -0
- package/dist/runtime/session-streams.js +8 -0
- package/dist/schemas/workflow.d.ts +1 -1
- package/dist/types/checkpoint.d.ts +1 -0
- package/dist/types/checkpoint.js +5 -0
- package/package.json +2 -2
- package/schema/noetic-workflow.schema.json +803 -119
|
@@ -1,15 +1,45 @@
|
|
|
1
|
-
import type { Context, Item, Step } from '@noetic-tools/types';
|
|
1
|
+
import type { Context, CustomItem, Item, Step } from '@noetic-tools/types';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* `step.emit` filtering. Each item is marked in the step's lane record, so the ledger
|
|
7
|
-
* entry that holds it can stream it again on replay.
|
|
3
|
+
* Starts what a step emits (spec 34 § Emitting): opens the dispatch's custom item, if
|
|
4
|
+
* `emitItems` names one, and runs `onStart` with the step's input. Events stream at
|
|
5
|
+
* once as `custom_item_event`; a final item appends and streams as `item_appended`.
|
|
8
6
|
*
|
|
9
|
-
* @throws `
|
|
10
|
-
*
|
|
7
|
+
* @throws `NoeticConfigError` with code `UNKNOWN_CUSTOM_ITEM`, `CUSTOM_ITEM_HAS_NO_EVENTS`,
|
|
8
|
+
* `CUSTOM_ITEM_ALREADY_FINAL`, or `INVALID_CUSTOM_ITEM`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function beginEmitItems<TContext, I, O>(step: Step<TContext, I, O>, input: I, ctx: Context<TContext>): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Finishes what a step emits: runs `onDone` with its output, or for `emitItems: true`
|
|
13
|
+
* emits the output itself as a final item, or for a function emits what it returns.
|
|
14
|
+
* Like the SDK item events, none of it is subject to `step.emit` filtering.
|
|
15
|
+
*
|
|
16
|
+
* @throws `NoeticConfigError` as `beginEmitItems`, and `NoeticError` with kind
|
|
17
|
+
* `item_schema_mismatch` when a standard item's type is not one the context accepts.
|
|
11
18
|
*/
|
|
12
19
|
export declare function emitStepItems<TContext, I, O>(step: Step<TContext, I, O>, output: O, ctx: Context<TContext>): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Streams one event a generator `runCode` yielded, as an event of the item its
|
|
22
|
+
* `emitItems` names.
|
|
23
|
+
*
|
|
24
|
+
* @throws `NoeticConfigError` with code `CUSTOM_ITEM_HAS_NO_EVENTS` or `INVALID_CUSTOM_ITEM`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function emitStepEvent<TContext>(stepId: string, event: unknown, ctx: Context<TContext>): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Runs a tool-style body to its return value: awaits a promise, or drains a
|
|
29
|
+
* generator, passing each yield to `onYield`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function runToReturn(result: unknown, onYield: (value: unknown) => Promise<void>): Promise<unknown>;
|
|
32
|
+
/**
|
|
33
|
+
* Lets `ctx` accept the final items of every custom item `step` declares, before one
|
|
34
|
+
* is appended or replayed from the ledger. Contexts created from `ctx` later inherit
|
|
35
|
+
* the wider schemas.
|
|
36
|
+
*/
|
|
37
|
+
export declare function allowCustomItems<TContext, I, O>(step: Step<TContext, I, O>, ctx: Context<TContext>): void;
|
|
38
|
+
/**
|
|
39
|
+
* Every custom item declared anywhere in `step`'s static tree, for a harness to accept
|
|
40
|
+
* before it rebuilds or restores a log that may hold them.
|
|
41
|
+
*/
|
|
42
|
+
export declare function collectCustomItems(step: Step): CustomItem[];
|
|
13
43
|
/**
|
|
14
44
|
* Puts a replayed entry's items back on the log. The ones at `emitted` (the entry's
|
|
15
45
|
* `streamed` indices) keep their mark, so a parent that records on this resumed run
|
|
@@ -1,28 +1,114 @@
|
|
|
1
|
-
import { frameworkCast } from '@noetic-tools/types';
|
|
1
|
+
import { frameworkCast, NoeticConfigError, validateSchema } from '@noetic-tools/types';
|
|
2
2
|
import { emitFrameworkEvent, getBroadcaster } from '../runtime/broadcaster-utils.js';
|
|
3
3
|
import { ContextImpl } from '../runtime/context-impl.js';
|
|
4
|
+
import { stepChildren } from './step-walk.js';
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region Constants
|
|
7
|
+
/** Open emissions, by the lane the dispatch runs on (one per dispatch). */
|
|
8
|
+
const emissions = new WeakMap();
|
|
9
|
+
//#endregion
|
|
4
10
|
//#region Public API
|
|
5
11
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* `step.emit` filtering. Each item is marked in the step's lane record, so the ledger
|
|
10
|
-
* entry that holds it can stream it again on replay.
|
|
12
|
+
* Starts what a step emits (spec 34 § Emitting): opens the dispatch's custom item, if
|
|
13
|
+
* `emitItems` names one, and runs `onStart` with the step's input. Events stream at
|
|
14
|
+
* once as `custom_item_event`; a final item appends and streams as `item_appended`.
|
|
11
15
|
*
|
|
12
|
-
* @throws `
|
|
13
|
-
*
|
|
16
|
+
* @throws `NoeticConfigError` with code `UNKNOWN_CUSTOM_ITEM`, `CUSTOM_ITEM_HAS_NO_EVENTS`,
|
|
17
|
+
* `CUSTOM_ITEM_ALREADY_FINAL`, or `INVALID_CUSTOM_ITEM`.
|
|
14
18
|
*/
|
|
15
|
-
export async function
|
|
16
|
-
const
|
|
17
|
-
if (!
|
|
19
|
+
export async function beginEmitItems(step, input, ctx) {
|
|
20
|
+
const config = hookConfig(step);
|
|
21
|
+
if (!config) {
|
|
18
22
|
return;
|
|
19
23
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
if (config.item !== undefined) {
|
|
25
|
+
emissions.set(ctx, {
|
|
26
|
+
id: `item-${crypto.randomUUID()}`,
|
|
27
|
+
item: resolveItem(step, config.item),
|
|
28
|
+
final: false,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (config.onStart) {
|
|
32
|
+
await runHook(step, config.onStart, input, ctx);
|
|
24
33
|
}
|
|
25
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Finishes what a step emits: runs `onDone` with its output, or for `emitItems: true`
|
|
37
|
+
* emits the output itself as a final item, or for a function emits what it returns.
|
|
38
|
+
* Like the SDK item events, none of it is subject to `step.emit` filtering.
|
|
39
|
+
*
|
|
40
|
+
* @throws `NoeticConfigError` as `beginEmitItems`, and `NoeticError` with kind
|
|
41
|
+
* `item_schema_mismatch` when a standard item's type is not one the context accepts.
|
|
42
|
+
*/
|
|
43
|
+
export async function emitStepItems(step, output, ctx) {
|
|
44
|
+
try {
|
|
45
|
+
const option = step.emitItems;
|
|
46
|
+
if (!option) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (option === true) {
|
|
50
|
+
await emitOutput(step, output, ctx);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (typeof option === 'function') {
|
|
54
|
+
appendAll(ctx, toItems(await option(output, ctx)));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (option.onDone) {
|
|
58
|
+
await runHook(step, frameworkCast(option.onDone), output, ctx);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
emissions.delete(ctx);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Streams one event a generator `runCode` yielded, as an event of the item its
|
|
67
|
+
* `emitItems` names.
|
|
68
|
+
*
|
|
69
|
+
* @throws `NoeticConfigError` with code `CUSTOM_ITEM_HAS_NO_EVENTS` or `INVALID_CUSTOM_ITEM`.
|
|
70
|
+
*/
|
|
71
|
+
export async function emitStepEvent(stepId, event, ctx) {
|
|
72
|
+
await emitEvent(stepId, openEmission(stepId, ctx), event, ctx);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Runs a tool-style body to its return value: awaits a promise, or drains a
|
|
76
|
+
* generator, passing each yield to `onYield`.
|
|
77
|
+
*/
|
|
78
|
+
export async function runToReturn(result, onYield) {
|
|
79
|
+
if (!isGenerator(result)) {
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
while (true) {
|
|
83
|
+
const next = await result.next();
|
|
84
|
+
if (next.done) {
|
|
85
|
+
return next.value;
|
|
86
|
+
}
|
|
87
|
+
await onYield(next.value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Lets `ctx` accept the final items of every custom item `step` declares, before one
|
|
92
|
+
* is appended or replayed from the ledger. Contexts created from `ctx` later inherit
|
|
93
|
+
* the wider schemas.
|
|
94
|
+
*/
|
|
95
|
+
export function allowCustomItems(step, ctx) {
|
|
96
|
+
const items = Object.values(step.customItems ?? {});
|
|
97
|
+
if (items.length === 0 || !(ctx instanceof ContextImpl)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
ctx.allowItemTypes(items.map((item) => item.type));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Every custom item declared anywhere in `step`'s static tree, for a harness to accept
|
|
104
|
+
* before it rebuilds or restores a log that may hold them.
|
|
105
|
+
*/
|
|
106
|
+
export function collectCustomItems(step) {
|
|
107
|
+
return [
|
|
108
|
+
...Object.values(step.customItems ?? {}),
|
|
109
|
+
...stepChildren(step).flatMap(collectCustomItems),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
26
112
|
/**
|
|
27
113
|
* Puts a replayed entry's items back on the log. The ones at `emitted` (the entry's
|
|
28
114
|
* `streamed` indices) keep their mark, so a parent that records on this resumed run
|
|
@@ -44,7 +130,104 @@ export function appendReplayed(ctx, items, emitted) {
|
|
|
44
130
|
});
|
|
45
131
|
}
|
|
46
132
|
//#endregion
|
|
47
|
-
//#region
|
|
133
|
+
//#region Hooks
|
|
134
|
+
function hookConfig(step) {
|
|
135
|
+
const option = step.emitItems;
|
|
136
|
+
if (typeof option !== 'object') {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
item: option.item,
|
|
141
|
+
onStart: frameworkCast(option.onStart),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Runs one hook. With an open emission, its yields are events and its return value
|
|
146
|
+
* the final item's fields. Without one it gives text or standard items, and may not
|
|
147
|
+
* yield.
|
|
148
|
+
*/
|
|
149
|
+
async function runHook(step, hook, value, ctx) {
|
|
150
|
+
const emission = emissions.get(ctx);
|
|
151
|
+
const result = await runToReturn(hook(value, frameworkCast(ctx)), (event) => emitEvent(step.id, emission ?? noEvents(step.id), event, ctx));
|
|
152
|
+
if (!emission) {
|
|
153
|
+
appendAll(ctx, toItems(frameworkCast(result)));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (result !== undefined) {
|
|
157
|
+
await emitFinal(emission, result, ctx);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region Emitting
|
|
162
|
+
async function emitEvent(stepId, emission, event, ctx) {
|
|
163
|
+
const schema = emission.item.event;
|
|
164
|
+
if (!schema) {
|
|
165
|
+
noEvents(stepId, emission.item.type);
|
|
166
|
+
}
|
|
167
|
+
const checked = await validateSchema(schema, event);
|
|
168
|
+
if (!checked.success) {
|
|
169
|
+
throw invalid(emission.item.type, 'event', checked.issues);
|
|
170
|
+
}
|
|
171
|
+
emitFrameworkEvent({
|
|
172
|
+
broadcaster: getBroadcaster(frameworkCast(ctx)),
|
|
173
|
+
agentName: ctx.harness.config.name,
|
|
174
|
+
eventType: 'custom_item_event',
|
|
175
|
+
data: {
|
|
176
|
+
id: emission.id,
|
|
177
|
+
type: emission.item.type,
|
|
178
|
+
event: checked.value,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
async function emitFinal(emission, fields, ctx) {
|
|
183
|
+
if (emission.final) {
|
|
184
|
+
throw new NoeticConfigError({
|
|
185
|
+
code: 'CUSTOM_ITEM_ALREADY_FINAL',
|
|
186
|
+
message: `Custom item '${emission.item.type}' (${emission.id}) got a second final item in one dispatch.`,
|
|
187
|
+
hint: 'Return the final fields from one hook only: onStart or onDone, not both.',
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
emission.final = true;
|
|
191
|
+
const item = await finalItem(emission.item, emission.id, fields);
|
|
192
|
+
appendEmitted(ctx, item);
|
|
193
|
+
streamItem(ctx, item);
|
|
194
|
+
}
|
|
195
|
+
/** `emitItems: true`: the output is the final item, of a custom type when its `type` names one. */
|
|
196
|
+
async function emitOutput(step, output, ctx) {
|
|
197
|
+
const custom = customOutput(step.customItems, output);
|
|
198
|
+
if (!custom) {
|
|
199
|
+
appendAll(ctx, toItems(outputItems(output)));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const { id: _id, type: _type, ...fields } = custom.value;
|
|
203
|
+
const item = await finalItem(custom.item, `item-${crypto.randomUUID()}`, fields);
|
|
204
|
+
appendEmitted(ctx, item);
|
|
205
|
+
streamItem(ctx, item);
|
|
206
|
+
}
|
|
207
|
+
async function finalItem(item, id, fields) {
|
|
208
|
+
const checked = await validateSchema(item.output, fields);
|
|
209
|
+
if (!checked.success) {
|
|
210
|
+
throw invalid(item.type, 'output', checked.issues);
|
|
211
|
+
}
|
|
212
|
+
if (typeof checked.value !== 'object' || checked.value === null) {
|
|
213
|
+
throw invalid(item.type, 'output', [
|
|
214
|
+
{
|
|
215
|
+
message: 'A final item must be an object of fields.',
|
|
216
|
+
},
|
|
217
|
+
]);
|
|
218
|
+
}
|
|
219
|
+
return frameworkCast({
|
|
220
|
+
...checked.value,
|
|
221
|
+
id,
|
|
222
|
+
type: item.type,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
function appendAll(ctx, items) {
|
|
226
|
+
for (const item of items) {
|
|
227
|
+
appendEmitted(ctx, item);
|
|
228
|
+
streamItem(ctx, item);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
48
231
|
function appendEmitted(ctx, item) {
|
|
49
232
|
if (ctx instanceof ContextImpl) {
|
|
50
233
|
ctx.appendEmitted(item);
|
|
@@ -62,10 +245,70 @@ function streamItem(ctx, item) {
|
|
|
62
245
|
},
|
|
63
246
|
});
|
|
64
247
|
}
|
|
65
|
-
|
|
248
|
+
//#endregion
|
|
249
|
+
//#region Helpers
|
|
250
|
+
function resolveItem(step, name) {
|
|
251
|
+
const item = step.customItems?.[name];
|
|
252
|
+
if (!item) {
|
|
253
|
+
throw new NoeticConfigError({
|
|
254
|
+
code: 'UNKNOWN_CUSTOM_ITEM',
|
|
255
|
+
message: `Step '${step.id}' emits custom item '${name}', which is not in its scope.`,
|
|
256
|
+
hint: 'Declare it on the step (customItems) or, in a JSON workflow, at the document root.',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return item;
|
|
260
|
+
}
|
|
261
|
+
function openEmission(stepId, ctx) {
|
|
262
|
+
return emissions.get(ctx) ?? noEvents(stepId);
|
|
263
|
+
}
|
|
264
|
+
function noEvents(stepId, type) {
|
|
265
|
+
throw new NoeticConfigError({
|
|
266
|
+
code: 'CUSTOM_ITEM_HAS_NO_EVENTS',
|
|
267
|
+
message: type === undefined
|
|
268
|
+
? `Step '${stepId}' yielded an event, but its emitItems names no item.`
|
|
269
|
+
: `Step '${stepId}' yielded an event for custom item '${type}', which declares no event schema.`,
|
|
270
|
+
hint: "Name an item with an event schema: emitItems: { item: 'progress' }, with customItem({ type, event, output }).",
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function invalid(type, phase, issues) {
|
|
274
|
+
return new NoeticConfigError({
|
|
275
|
+
code: 'INVALID_CUSTOM_ITEM',
|
|
276
|
+
message: `Custom item '${type}' got an ${phase} that does not match its schema: ${issues.map((issue) => issue.message).join('; ')}`,
|
|
277
|
+
hint: `Make the ${phase} match the '${phase}' schema declared with customItem({ type: '${type}' }).`,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* The custom item an output is, when its `type` names one in scope. JSON workflow nodes
|
|
282
|
+
* pass output as text, so a string is read as JSON first.
|
|
283
|
+
*/
|
|
284
|
+
function customOutput(items, raw) {
|
|
285
|
+
const output = typeof raw === 'string' ? parseJson(raw) : raw;
|
|
286
|
+
if (typeof output !== 'object' || output === null || !('type' in output)) {
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
const item = Object.values(items ?? {}).find((candidate) => candidate.type === output.type);
|
|
290
|
+
return item
|
|
291
|
+
? {
|
|
292
|
+
item,
|
|
293
|
+
value: Object.fromEntries(Object.entries(output)),
|
|
294
|
+
}
|
|
295
|
+
: undefined;
|
|
296
|
+
}
|
|
297
|
+
function parseJson(text) {
|
|
298
|
+
try {
|
|
299
|
+
return JSON.parse(text);
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
function outputItems(output) {
|
|
66
306
|
if (typeof output === 'string') {
|
|
67
307
|
return output;
|
|
68
308
|
}
|
|
309
|
+
if (typeof output === 'object' && output !== null && 'type' in output) {
|
|
310
|
+
return frameworkCast(output);
|
|
311
|
+
}
|
|
69
312
|
return output === undefined || output === null ? undefined : JSON.stringify(output);
|
|
70
313
|
}
|
|
71
314
|
function toItems(result) {
|
|
@@ -88,6 +331,13 @@ function toItems(result) {
|
|
|
88
331
|
function isItemList(result) {
|
|
89
332
|
return Array.isArray(result);
|
|
90
333
|
}
|
|
334
|
+
function isGenerator(value) {
|
|
335
|
+
return (typeof value === 'object' &&
|
|
336
|
+
value !== null &&
|
|
337
|
+
'next' in value &&
|
|
338
|
+
typeof value.next === 'function' &&
|
|
339
|
+
(Symbol.asyncIterator in value || Symbol.iterator in value));
|
|
340
|
+
}
|
|
91
341
|
function assistantMessage(text) {
|
|
92
342
|
return {
|
|
93
343
|
id: `emitted-${crypto.randomUUID()}`,
|
|
@@ -11,6 +11,7 @@ import { openChildSpan, traceChild } from './child-span.js';
|
|
|
11
11
|
import { cloneWithGuard } from './clone-guard.js';
|
|
12
12
|
import { collectAllTools, deduplicateTools } from './collect-tools.js';
|
|
13
13
|
import { anchoringAttributes, prepareBandedView } from './context-assembly.js';
|
|
14
|
+
import { emitStepEvent, runToReturn } from './emit-items.js';
|
|
14
15
|
import { trackUsage } from './message-helpers.js';
|
|
15
16
|
import { wrapModelError } from './model-error.js';
|
|
16
17
|
import { parseStructuredOutput } from './structured-output.js';
|
|
@@ -30,7 +31,8 @@ export async function executeRunCode(step, input, ctx) {
|
|
|
30
31
|
});
|
|
31
32
|
}
|
|
32
33
|
try {
|
|
33
|
-
|
|
34
|
+
// A generator body yields events of the item the step's emitItems names.
|
|
35
|
+
return frameworkCast(await runToReturn(step.execute(input, ctx), (event) => emitStepEvent(step.id, event, ctx)));
|
|
34
36
|
}
|
|
35
37
|
catch (e) {
|
|
36
38
|
// Rethrow cancellation immediately — retrying it would re-run side
|
|
@@ -3,7 +3,7 @@ import { emitFrameworkEvent, getBroadcaster, shouldEmit } from '../runtime/broad
|
|
|
3
3
|
import { ContextImpl } from '../runtime/context-impl.js';
|
|
4
4
|
import { REPLAY_KEY_PREFIX } from '../runtime/durable/step-ledger.js';
|
|
5
5
|
import { DetachedHandleImpl } from '../util/detached-handle.js';
|
|
6
|
-
import { appendReplayed, emitStepItems } from './emit-items.js';
|
|
6
|
+
import { allowCustomItems, appendReplayed, beginEmitItems, emitStepItems } from './emit-items.js';
|
|
7
7
|
import { executeAcpAgent } from './execute-acp-agent.js';
|
|
8
8
|
import { executeCallModel, executeInvokeTool, executeRunCode, executeSpawn, executeWithContext, } from './execute-action.js';
|
|
9
9
|
import { executeConditional, executeInParallel, executeLoop, executeSchedule, } from './execute-control.js';
|
|
@@ -304,6 +304,8 @@ export async function execute(step, input, ctx) {
|
|
|
304
304
|
/* Resume: a previous run that completed this exact step replays its recorded
|
|
305
305
|
* output instead of re-running it. `ledgerKey`/`ledger` are captured before
|
|
306
306
|
* dispatch and reused by the record below. */
|
|
307
|
+
// Before replay: a recorded entry may hold this step's custom items.
|
|
308
|
+
allowCustomItems(step, stepCtx);
|
|
307
309
|
const ledgerKey = resolveLedgerKey(step, input, stepCtx);
|
|
308
310
|
if (ledgerKey === undefined) {
|
|
309
311
|
lane?.skipLedger();
|
|
@@ -325,6 +327,7 @@ export async function execute(step, input, ctx) {
|
|
|
325
327
|
}
|
|
326
328
|
let result;
|
|
327
329
|
try {
|
|
330
|
+
await beginEmitItems(step, input, stepCtx);
|
|
328
331
|
switch (step.kind) {
|
|
329
332
|
case 'runCode':
|
|
330
333
|
result = await dispatchViaAdapter(step, input, stepCtx, stepCtx.id, () => executeRunCode(step, input, stepCtx));
|
|
@@ -29,12 +29,14 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
29
29
|
readonly itemLog: ItemLog;
|
|
30
30
|
/** The same log, typed for `rebase`. */
|
|
31
31
|
private readonly log;
|
|
32
|
+
/** Custom item types `allowItemTypes` has accepted. Shared by every lane. */
|
|
33
|
+
private readonly _customItemTypes;
|
|
32
34
|
lastStepMeta: StepMeta | null;
|
|
33
35
|
lastLayerUsage: LastLayerUsage | undefined;
|
|
34
36
|
readonly harness: AgentHarnessContract;
|
|
35
37
|
readonly layers?: ContextLayer[];
|
|
36
38
|
unifiedTools?: ReadonlyArray<Tool>;
|
|
37
|
-
|
|
39
|
+
itemSchemas?: ItemSchemaRegistry;
|
|
38
40
|
readonly cwdState: CwdState;
|
|
39
41
|
/** @internal Event broadcaster for streaming — not part of public Context interface. */
|
|
40
42
|
readonly _broadcaster?: EventBroadcaster;
|
|
@@ -249,6 +251,16 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
249
251
|
* a replay streams again under the harness's `emitItemsOnReplay`.
|
|
250
252
|
*/
|
|
251
253
|
emittedIndices(): number[];
|
|
254
|
+
/**
|
|
255
|
+
* @internal
|
|
256
|
+
* Accept the final items of these custom item types in this context's log from now
|
|
257
|
+
* on (spec 34). Contexts created from this one afterwards inherit the wider registry,
|
|
258
|
+
* and a lane widens the context it views. A checkpoint records the types, so a
|
|
259
|
+
* restore can accept a stored log that holds them before any step runs.
|
|
260
|
+
*/
|
|
261
|
+
allowItemTypes(types: ReadonlyArray<string>): void;
|
|
262
|
+
/** @internal The custom item types this context accepts, for a checkpoint to record. */
|
|
263
|
+
customItemTypes(): string[];
|
|
252
264
|
/**
|
|
253
265
|
* @internal
|
|
254
266
|
* Append an item a step emitted (`step.emitItems`), marking it in this lane's record
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { buildContextData, defaultItemSchemaRegistry } from './context-deps.js';
|
|
2
2
|
import { hashStepInput } from './durable/step-ledger.js';
|
|
3
|
+
import { customItemGates } from './item-gates.js';
|
|
3
4
|
import { ItemLogImpl } from './item-log-impl.js';
|
|
4
5
|
const EMPTY_CONTEXT = Object.freeze({});
|
|
5
6
|
class NoopSpan {
|
|
@@ -60,6 +61,8 @@ export class ContextImpl {
|
|
|
60
61
|
itemLog;
|
|
61
62
|
/** The same log, typed for `rebase`. */
|
|
62
63
|
log;
|
|
64
|
+
/** Custom item types `allowItemTypes` has accepted. Shared by every lane. */
|
|
65
|
+
_customItemTypes = new Set();
|
|
63
66
|
lastStepMeta = null;
|
|
64
67
|
lastLayerUsage = undefined;
|
|
65
68
|
harness;
|
|
@@ -414,6 +417,33 @@ export class ContextImpl {
|
|
|
414
417
|
]
|
|
415
418
|
: []);
|
|
416
419
|
}
|
|
420
|
+
/**
|
|
421
|
+
* @internal
|
|
422
|
+
* Accept the final items of these custom item types in this context's log from now
|
|
423
|
+
* on (spec 34). Contexts created from this one afterwards inherit the wider registry,
|
|
424
|
+
* and a lane widens the context it views. A checkpoint records the types, so a
|
|
425
|
+
* restore can accept a stored log that holds them before any step runs.
|
|
426
|
+
*/
|
|
427
|
+
allowItemTypes(types) {
|
|
428
|
+
const added = types.filter((type) => !this._customItemTypes.has(type));
|
|
429
|
+
if (added.length === 0) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
for (const type of added) {
|
|
433
|
+
this._customItemTypes.add(type);
|
|
434
|
+
}
|
|
435
|
+
const registry = (this.itemSchemas ?? defaultItemSchemaRegistry).extend({
|
|
436
|
+
items: customItemGates(added),
|
|
437
|
+
});
|
|
438
|
+
this.itemSchemas = registry;
|
|
439
|
+
this.log.useSchemas(registry);
|
|
440
|
+
}
|
|
441
|
+
/** @internal The custom item types this context accepts, for a checkpoint to record. */
|
|
442
|
+
customItemTypes() {
|
|
443
|
+
return [
|
|
444
|
+
...this._customItemTypes,
|
|
445
|
+
];
|
|
446
|
+
}
|
|
417
447
|
/**
|
|
418
448
|
* @internal
|
|
419
449
|
* Append an item a step emitted (`step.emitItems`), marking it in this lane's record
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CheckpointSchemaVersion } from '../../types/checkpoint.js';
|
|
2
2
|
import { ContextImpl, rootIdOf } from '../context-impl.js';
|
|
3
|
+
import { customItemGates } from '../item-gates.js';
|
|
3
4
|
import { StepLedger } from './step-ledger.js';
|
|
4
5
|
//#endregion
|
|
5
6
|
//#region captureCheckpoint
|
|
@@ -58,6 +59,11 @@ export async function captureCheckpoint(h, ctx) {
|
|
|
58
59
|
items: baseline.items,
|
|
59
60
|
},
|
|
60
61
|
occurrences: baseline.occurrences,
|
|
62
|
+
...(impl && impl.customItemTypes().length > 0
|
|
63
|
+
? {
|
|
64
|
+
customItemTypes: impl.customItemTypes(),
|
|
65
|
+
}
|
|
66
|
+
: {}),
|
|
61
67
|
...(state.ok
|
|
62
68
|
? {
|
|
63
69
|
state: state.value,
|
|
@@ -165,14 +171,28 @@ function contextFromSnapshot(h, snapshot, opts) {
|
|
|
165
171
|
/* Caller wiring first, snapshot second: a host may legitimately swap the context
|
|
166
172
|
* layers or hang the restored execution under a new parent, but it must never be
|
|
167
173
|
* able to override the identity/history the snapshot is the record of. */
|
|
174
|
+
const customItemTypes = snapshot.customItemTypes ?? [];
|
|
175
|
+
const items = h.itemSchemas
|
|
176
|
+
.extend({
|
|
177
|
+
items: customItemGates(customItemTypes),
|
|
178
|
+
})
|
|
179
|
+
.parseMany(snapshot.itemLog.items);
|
|
168
180
|
const ctx = h.createContext({
|
|
169
181
|
...opts,
|
|
170
182
|
state: opts?.state ?? snapshot.state,
|
|
171
|
-
items:
|
|
183
|
+
items: [],
|
|
172
184
|
threadId: snapshot.threadId,
|
|
173
185
|
resourceId: snapshot.resourceId,
|
|
174
186
|
cwdInit: snapshot.cwd?.current ?? undefined,
|
|
175
187
|
});
|
|
188
|
+
/* The log goes in after the context accepts the recorded custom item types:
|
|
189
|
+
* seeded at creation, it would be checked against the harness's schemas alone. */
|
|
190
|
+
if (ctx instanceof ContextImpl) {
|
|
191
|
+
ctx.allowItemTypes(customItemTypes);
|
|
192
|
+
}
|
|
193
|
+
for (const item of items) {
|
|
194
|
+
ctx.itemLog.append(item);
|
|
195
|
+
}
|
|
176
196
|
if (ctx instanceof ContextImpl) {
|
|
177
197
|
ctx.restoreOccurrences(snapshot.occurrences);
|
|
178
198
|
if (snapshot.usage) {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ZodTypeAny } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Item schemas that let a log accept the final items of these custom item types
|
|
4
|
+
* (spec 34). They gate on `type` and `id` only: an emitter checks an item's fields
|
|
5
|
+
* against its declared `output` before appending it, and a restore reads back items
|
|
6
|
+
* that were checked when they were emitted.
|
|
7
|
+
*/
|
|
8
|
+
export declare function customItemGates(types: Iterable<string>): ZodTypeAny[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Item schemas that let a log accept the final items of these custom item types
|
|
4
|
+
* (spec 34). They gate on `type` and `id` only: an emitter checks an item's fields
|
|
5
|
+
* against its declared `output` before appending it, and a restore reads back items
|
|
6
|
+
* that were checked when they were emitted.
|
|
7
|
+
*/
|
|
8
|
+
export function customItemGates(types) {
|
|
9
|
+
return [
|
|
10
|
+
...new Set(types),
|
|
11
|
+
].map((type) => z.looseObject({
|
|
12
|
+
type: z.literal(type),
|
|
13
|
+
id: z.string(),
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { Item, ItemLog, ItemSchemaRegistry } from '@noetic-tools/types';
|
|
2
2
|
export declare class ItemLogImpl implements ItemLog {
|
|
3
|
-
private
|
|
3
|
+
private itemSchemas;
|
|
4
4
|
private readonly _items;
|
|
5
5
|
private _frozenCache;
|
|
6
6
|
constructor(itemSchemas?: ItemSchemaRegistry);
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
* Validate later appends against `registry`, a superset of the current one: a
|
|
10
|
+
* context that learns of new item types (a step's custom items) widens its log.
|
|
11
|
+
*/
|
|
12
|
+
useSchemas(registry: ItemSchemaRegistry): void;
|
|
7
13
|
get items(): ReadonlyArray<Item>;
|
|
8
14
|
/**
|
|
9
15
|
* @internal
|
|
@@ -6,6 +6,14 @@ export class ItemLogImpl {
|
|
|
6
6
|
constructor(itemSchemas = defaultItemSchemaRegistry) {
|
|
7
7
|
this.itemSchemas = itemSchemas;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
* Validate later appends against `registry`, a superset of the current one: a
|
|
12
|
+
* context that learns of new item types (a step's custom items) widens its log.
|
|
13
|
+
*/
|
|
14
|
+
useSchemas(registry) {
|
|
15
|
+
this.itemSchemas = registry;
|
|
16
|
+
}
|
|
9
17
|
get items() {
|
|
10
18
|
if (!this._frozenCache) {
|
|
11
19
|
this._frozenCache = Object.freeze([
|
|
@@ -148,6 +148,14 @@ export async function* buildItemStream(broadcaster, itemSchemas = defaultItemSch
|
|
|
148
148
|
let roundOffset = 0;
|
|
149
149
|
for await (const event of broadcaster) {
|
|
150
150
|
if (event.source === 'framework') {
|
|
151
|
+
// A custom item's event streams in progress under the id its final item takes.
|
|
152
|
+
if (event.type.endsWith(':custom_item_event')) {
|
|
153
|
+
yield frameworkCast({
|
|
154
|
+
...event.data,
|
|
155
|
+
isComplete: false,
|
|
156
|
+
});
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
151
159
|
if (event.type.endsWith(':item_appended') &&
|
|
152
160
|
typeof event.data.item === 'object' &&
|
|
153
161
|
event.data.item !== null) {
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* consumers can validate workflow documents without importing core.
|
|
4
4
|
* This barrel keeps core's public and intra-core import paths unchanged.
|
|
5
5
|
*/
|
|
6
|
-
export type { AcpAgentWorkflowNode, CallModelWorkflowNode, ComputeWorkflowNode, ConditionalRoute, ConditionalWorkflowNode, DecideWorkflowNode, InParallelWorkflowNode, InvokeToolWorkflowNode, LoopWorkflowNode, OutputCodecRef, RouteSwitchWorkflowNode, RunCodeWorkflowNode, ScheduleWorkflowNode, SequenceWorkflowNode, SpawnWorkflowNode, SubflowWorkflowNode, UntilPredicate, WithContextWorkflowNode, WorkflowDocument, WorkflowEmitItems, WorkflowGraph, WorkflowNode, WorkflowReplayKey, } from '@noetic-tools/types';
|
|
6
|
+
export type { AcpAgentWorkflowNode, CallModelWorkflowNode, ComputeWorkflowNode, ConditionalRoute, ConditionalWorkflowNode, DecideWorkflowNode, InParallelWorkflowNode, InvokeToolWorkflowNode, LoopWorkflowNode, OutputCodecRef, RouteSwitchWorkflowNode, RunCodeWorkflowNode, ScheduleWorkflowNode, SequenceWorkflowNode, SpawnWorkflowNode, SubflowWorkflowNode, UntilPredicate, WithContextWorkflowNode, WorkflowCustomItem, WorkflowDocument, WorkflowEmitItems, WorkflowGraph, WorkflowNode, WorkflowReplayKey, } from '@noetic-tools/types';
|
|
7
7
|
export { MergeStrategy, MergeStrategySchema, UntilPredicateSchema, validateWorkflow, WorkflowDocumentSchema, WorkflowNodeSchema, walkWorkflow, workflowDepth, workflowGraph, } from '@noetic-tools/types';
|
|
@@ -128,6 +128,7 @@ export declare const CheckpointSnapshotSchema: z.ZodObject<{
|
|
|
128
128
|
items: z.ZodArray<z.ZodUnknown>;
|
|
129
129
|
}, z.core.$strip>;
|
|
130
130
|
occurrences: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
131
|
+
customItemTypes: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
131
132
|
state: z.ZodOptional<z.ZodUnknown>;
|
|
132
133
|
usage: z.ZodOptional<z.ZodObject<{
|
|
133
134
|
tokens: z.ZodObject<{
|
package/dist/types/checkpoint.js
CHANGED
|
@@ -120,6 +120,11 @@ export const CheckpointSnapshotSchema = z.object({
|
|
|
120
120
|
* on the same execution does not land on an earlier run's paths.
|
|
121
121
|
*/
|
|
122
122
|
occurrences: z.record(z.string(), z.number()),
|
|
123
|
+
/**
|
|
124
|
+
* Custom item types the context accepted (spec 34), so a restore can parse an
|
|
125
|
+
* `itemLog` that holds them before the steps that declare them run again.
|
|
126
|
+
*/
|
|
127
|
+
customItemTypes: z.array(z.string()).optional(),
|
|
123
128
|
/**
|
|
124
129
|
* The execution's `ctx.state` at capture time. Absent when the state does not survive
|
|
125
130
|
* JSON encoding, in which case restore keeps whatever state the host passes.
|