@noetic-tools/core 5.22.0-next.1 → 5.22.0-next.2
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/adapters/openrouter.d.ts +6 -1
- package/dist/adapters/openrouter.js +10 -0
- package/dist/builders/decide-builders.d.ts +7 -0
- package/dist/builders/tool-builder.d.ts +5 -1
- package/dist/builders/tool-builder.js +2 -0
- package/dist/builders/workflow-control-nodes.js +1 -0
- package/dist/harness/agent-harness.d.ts +54 -0
- package/dist/harness/agent-harness.js +108 -1
- package/dist/harness/decide-call.d.ts +15 -0
- package/dist/harness/decide-call.js +57 -7
- package/dist/harness/harness-options.d.ts +9 -0
- package/dist/harness/llm-client.d.ts +2 -0
- package/dist/harness/llm-client.js +9 -5
- package/dist/harness/model-tool-round.js +48 -3
- package/dist/index.d.ts +5 -1
- package/dist/index.js +2 -0
- package/dist/interpreter/execute-decide.js +5 -0
- package/dist/portable/runtime.d.ts +4 -0
- package/dist/portable/runtime.js +2 -0
- package/dist/runtime/event-broadcaster.d.ts +9 -1
- package/dist/runtime/event-broadcaster.js +16 -1
- package/dist/runtime/output-store.d.ts +88 -0
- package/dist/runtime/output-store.js +241 -0
- package/dist/runtime/session-streams.d.ts +28 -7
- package/dist/runtime/session-streams.js +112 -67
- package/package.json +1 -1
- package/schema/noetic-workflow.schema.json +27 -0
package/dist/index.d.ts
CHANGED
|
@@ -130,6 +130,10 @@ export { DEFAULT_PARK_GRACE_MS } from './runtime/durable/parking.js';
|
|
|
130
130
|
/** @public */
|
|
131
131
|
export { DEFAULT_LEASE_TTL_MS } from './runtime/durable/turn-lease.js';
|
|
132
132
|
/** @public */
|
|
133
|
+
export type { OutputSnapshot } from './runtime/output-store.js';
|
|
134
|
+
/** @public */
|
|
135
|
+
export { OutputStore, OutputStoreError } from './runtime/output-store.js';
|
|
136
|
+
/** @public */
|
|
133
137
|
export { getRegistry, lookupStep, registerStep } from './runtime/step-registry.js';
|
|
134
138
|
/** @public */
|
|
135
139
|
export type { Expression } from '@noetic-tools/types';
|
|
@@ -144,7 +148,7 @@ export { MergeStrategySchema, UntilPredicateSchema, validateWorkflow, WorkflowDo
|
|
|
144
148
|
export type { Channel, ChannelHandle, ExternalChannel } from '@noetic-tools/types';
|
|
145
149
|
/** @public */
|
|
146
150
|
/** @public */
|
|
147
|
-
export type { InferSchemaInput, InferSchemaOutput, InputSchemaConfig, LLMResponse, LlmProviderConfig, ModelParams, RetryPolicy, RoundUsage, SchemaValidationFailure, SchemaValidationResult, SchemaValidationSuccess, ServerToolSpec, StandardJSONSchemaV1, StandardSchemaV1, StepMeta, TokenUsage, Tool, ToolChoice, ToolContextDeclaration, UsageCharge, } from '@noetic-tools/types';
|
|
151
|
+
export type { InferSchemaInput, InferSchemaOutput, InputSchemaConfig, LLMResponse, LlmProviderConfig, ModelParams, RetryPolicy, RoundUsage, SchemaValidationFailure, SchemaValidationResult, SchemaValidationSuccess, ServerToolSpec, StandardJSONSchemaV1, StandardSchemaV1, StepMeta, TokenUsage, Tool, ToolChoice, ToolContextDeclaration, ToolModelOutput, UsageCharge, } from '@noetic-tools/types';
|
|
148
152
|
/** @public */
|
|
149
153
|
export { isServerToolSpec, isStandardJsonSchema, isZodSchema, ServiceTier, standardIssuesToZodError, ToolChoiceMode, validateSchema, } from '@noetic-tools/types';
|
|
150
154
|
/** @public */
|
package/dist/index.js
CHANGED
|
@@ -102,6 +102,8 @@ export { DEFAULT_PARK_GRACE_MS } from './runtime/durable/parking.js';
|
|
|
102
102
|
/** @public */
|
|
103
103
|
export { DEFAULT_LEASE_TTL_MS } from './runtime/durable/turn-lease.js';
|
|
104
104
|
/** @public */
|
|
105
|
+
export { OutputStore, OutputStoreError } from './runtime/output-store.js';
|
|
106
|
+
/** @public */
|
|
105
107
|
export { getRegistry, lookupStep, registerStep } from './runtime/step-registry.js';
|
|
106
108
|
/** @public */
|
|
107
109
|
/** @public */
|
|
@@ -42,6 +42,11 @@ export async function executeDecide(step, input, ctx, layers) {
|
|
|
42
42
|
state,
|
|
43
43
|
questions: step.questions,
|
|
44
44
|
samples: step.samples,
|
|
45
|
+
...(step.headers
|
|
46
|
+
? {
|
|
47
|
+
headers: step.headers,
|
|
48
|
+
}
|
|
49
|
+
: {}),
|
|
45
50
|
signal: isContextImpl(baseCtx) ? baseCtx.abortSignal : undefined,
|
|
46
51
|
parentSpan: span,
|
|
47
52
|
_billTo: baseCtx,
|
|
@@ -2,3 +2,7 @@
|
|
|
2
2
|
export { AgentHarness } from '../harness/agent-harness.js';
|
|
3
3
|
/** @public */
|
|
4
4
|
export { createInMemoryStorage } from '../runtime/in-memory-storage.js';
|
|
5
|
+
/** @public */
|
|
6
|
+
export type { OutputSnapshot } from '../runtime/output-store.js';
|
|
7
|
+
/** @public */
|
|
8
|
+
export { OutputStore, OutputStoreError } from '../runtime/output-store.js';
|
package/dist/portable/runtime.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StreamEvent } from '@noetic-tools/types';
|
|
2
|
+
type Observer = (event: StreamEvent) => void;
|
|
2
3
|
interface EventBroadcasterOpts {
|
|
3
4
|
maxBufferSize?: number;
|
|
4
5
|
}
|
|
@@ -24,8 +25,15 @@ export declare class EventBroadcaster {
|
|
|
24
25
|
private err?;
|
|
25
26
|
private readonly iterators;
|
|
26
27
|
private hasHadConsumers;
|
|
28
|
+
private readonly observers;
|
|
27
29
|
constructor(opts?: EventBroadcasterOpts);
|
|
28
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Hear every event as it is emitted, whether or not a consumer is attached: the
|
|
32
|
+
* replay buffer stops filling once every consumer has left, an observer does not.
|
|
33
|
+
* Nothing is replayed to it. Returns the function that stops it.
|
|
34
|
+
*/
|
|
35
|
+
observe(observer: Observer): () => void;
|
|
36
|
+
/** Push an event to every observer, all active iterators and the replay buffer. */
|
|
29
37
|
emit(event: StreamEvent): void;
|
|
30
38
|
/** Signal that no more events will be emitted. */
|
|
31
39
|
complete(): void;
|
|
@@ -23,14 +23,29 @@ export class EventBroadcaster {
|
|
|
23
23
|
err;
|
|
24
24
|
iterators = new Set();
|
|
25
25
|
hasHadConsumers = false;
|
|
26
|
+
observers = new Set();
|
|
26
27
|
constructor(opts) {
|
|
27
28
|
this.maxBufferSize = opts?.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE;
|
|
28
29
|
}
|
|
29
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Hear every event as it is emitted, whether or not a consumer is attached: the
|
|
32
|
+
* replay buffer stops filling once every consumer has left, an observer does not.
|
|
33
|
+
* Nothing is replayed to it. Returns the function that stops it.
|
|
34
|
+
*/
|
|
35
|
+
observe(observer) {
|
|
36
|
+
this.observers.add(observer);
|
|
37
|
+
return () => {
|
|
38
|
+
this.observers.delete(observer);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Push an event to every observer, all active iterators and the replay buffer. */
|
|
30
42
|
emit(event) {
|
|
31
43
|
if (this.isDone) {
|
|
32
44
|
return;
|
|
33
45
|
}
|
|
46
|
+
for (const observer of this.observers) {
|
|
47
|
+
observer(event);
|
|
48
|
+
}
|
|
34
49
|
// Skip buffering when all consumers have departed
|
|
35
50
|
if (this.hasHadConsumers && this.iterators.size === 0) {
|
|
36
51
|
return;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ItemSchemaRegistry, StorageAdapter, StreamEvent } from '@noetic-tools/types';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
declare const OutputSnapshotSchema: z.ZodObject<{
|
|
4
|
+
type: z.ZodString;
|
|
5
|
+
id: z.ZodOptional<z.ZodString>;
|
|
6
|
+
isComplete: z.ZodOptional<z.ZodBoolean>;
|
|
7
|
+
}, z.core.$catchall<z.ZodUnknown>>;
|
|
8
|
+
/**
|
|
9
|
+
* @public One item of a thread's display output: an item snapshot as the item
|
|
10
|
+
* stream yields it, unfinished ones included (`isComplete: false`), or whatever
|
|
11
|
+
* a protocol adapter saved in its place.
|
|
12
|
+
*/
|
|
13
|
+
export type OutputSnapshot = z.infer<typeof OutputSnapshotSchema>;
|
|
14
|
+
/**
|
|
15
|
+
* @public A thread's display output could not be read or written. `scope` is the
|
|
16
|
+
* thread id; `cause` is what storage, or the snapshot check, failed with.
|
|
17
|
+
*/
|
|
18
|
+
export declare class OutputStoreError extends Error {
|
|
19
|
+
readonly scope: string;
|
|
20
|
+
constructor(options: {
|
|
21
|
+
scope: string;
|
|
22
|
+
message: string;
|
|
23
|
+
cause: unknown;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** @internal The storage key a thread's display output is kept under. */
|
|
27
|
+
export declare function outputKey(scope: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* @public A thread's display output, kept in storage under `output:<scope>`:
|
|
30
|
+
* the latest snapshot of every item its stream produced, including items that
|
|
31
|
+
* never finished. It is for showing a thread again, in a new process or after a
|
|
32
|
+
* failed run. Nothing resumes from it: checkpoints, the step ledger and the
|
|
33
|
+
* model's history are stored apart and never read here.
|
|
34
|
+
*
|
|
35
|
+
* `record` and `write` queue snapshots and save them in the background, one
|
|
36
|
+
* write at a time, each holding the whole output. `flush` and `read` wait for
|
|
37
|
+
* what is queued and throw `OutputStoreError` when it could not be saved; the
|
|
38
|
+
* snapshots stay queued, so the next call tries again. On storage with `swap` a
|
|
39
|
+
* write never replaces what another store saved meanwhile.
|
|
40
|
+
*/
|
|
41
|
+
export declare class OutputStore {
|
|
42
|
+
private readonly storage;
|
|
43
|
+
private readonly scope;
|
|
44
|
+
private readonly stream;
|
|
45
|
+
private readonly pending;
|
|
46
|
+
private writing?;
|
|
47
|
+
private failure?;
|
|
48
|
+
constructor(storage: StorageAdapter, scope: string, schemas?: ItemSchemaRegistry);
|
|
49
|
+
/**
|
|
50
|
+
* Fold one stream event into the output. Never throws: an event that cannot be
|
|
51
|
+
* read as display output is left out, with a warning, and the run goes on.
|
|
52
|
+
*/
|
|
53
|
+
record(event: StreamEvent): void;
|
|
54
|
+
/**
|
|
55
|
+
* Save display snapshots, each replacing the stored one with its id, and resolve
|
|
56
|
+
* the whole stored output.
|
|
57
|
+
*
|
|
58
|
+
* @throws OutputStoreError when `items` are not snapshots, or storage fails.
|
|
59
|
+
*/
|
|
60
|
+
write(items: ReadonlyArray<unknown>): Promise<OutputSnapshot[]>;
|
|
61
|
+
/**
|
|
62
|
+
* The stored output, after everything queued is saved.
|
|
63
|
+
*
|
|
64
|
+
* @throws OutputStoreError when storage fails or holds something else.
|
|
65
|
+
*/
|
|
66
|
+
read(): Promise<OutputSnapshot[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Wait until everything queued is saved.
|
|
69
|
+
*
|
|
70
|
+
* @throws OutputStoreError when it could not be. What was queued stays queued,
|
|
71
|
+
* and every flush throws until a later one saves it.
|
|
72
|
+
*/
|
|
73
|
+
flush(): Promise<void>;
|
|
74
|
+
private queue;
|
|
75
|
+
private start;
|
|
76
|
+
/** What storage holds under the key, as it is stored and as snapshots. */
|
|
77
|
+
private load;
|
|
78
|
+
/** Save what is queued over what is stored, until nothing is queued. */
|
|
79
|
+
private drain;
|
|
80
|
+
private save;
|
|
81
|
+
/**
|
|
82
|
+
* Write `batch` over what is stored now. On storage with `swap` the write lands
|
|
83
|
+
* only if nothing else was saved since the read, and false means it did not.
|
|
84
|
+
*/
|
|
85
|
+
private saveOver;
|
|
86
|
+
private error;
|
|
87
|
+
}
|
|
88
|
+
export {};
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ItemStream } from './session-streams.js';
|
|
3
|
+
//#region Types
|
|
4
|
+
const OutputSnapshotSchema = z
|
|
5
|
+
.object({
|
|
6
|
+
type: z.string(),
|
|
7
|
+
id: z.string().optional(),
|
|
8
|
+
isComplete: z.boolean().optional(),
|
|
9
|
+
})
|
|
10
|
+
.catchall(z.unknown());
|
|
11
|
+
/**
|
|
12
|
+
* @public A thread's display output could not be read or written. `scope` is the
|
|
13
|
+
* thread id; `cause` is what storage, or the snapshot check, failed with.
|
|
14
|
+
*/
|
|
15
|
+
export class OutputStoreError extends Error {
|
|
16
|
+
scope;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
super(options.message, {
|
|
19
|
+
cause: options.cause,
|
|
20
|
+
});
|
|
21
|
+
this.name = 'OutputStoreError';
|
|
22
|
+
this.scope = options.scope;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region Helpers
|
|
27
|
+
/** How many times one write is tried before the failure is kept. */
|
|
28
|
+
const WRITE_ATTEMPTS = 3;
|
|
29
|
+
/** How many times a write may lose to another writer before it is a failure. */
|
|
30
|
+
const SWAP_ATTEMPTS = 20;
|
|
31
|
+
const WRITE_RETRY_MS = 10;
|
|
32
|
+
/** @internal The storage key a thread's display output is kept under. */
|
|
33
|
+
export function outputKey(scope) {
|
|
34
|
+
return `output:${scope}`;
|
|
35
|
+
}
|
|
36
|
+
/** What makes two snapshots the same item: its id, else its call, else its content. */
|
|
37
|
+
function keyOf(item) {
|
|
38
|
+
if (typeof item.id === 'string') {
|
|
39
|
+
return item.id;
|
|
40
|
+
}
|
|
41
|
+
if (typeof item.callId === 'string') {
|
|
42
|
+
return `${item.type}:${item.callId}`;
|
|
43
|
+
}
|
|
44
|
+
return JSON.stringify(item);
|
|
45
|
+
}
|
|
46
|
+
function wait(ms) {
|
|
47
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region OutputStore
|
|
51
|
+
/**
|
|
52
|
+
* @public A thread's display output, kept in storage under `output:<scope>`:
|
|
53
|
+
* the latest snapshot of every item its stream produced, including items that
|
|
54
|
+
* never finished. It is for showing a thread again, in a new process or after a
|
|
55
|
+
* failed run. Nothing resumes from it: checkpoints, the step ledger and the
|
|
56
|
+
* model's history are stored apart and never read here.
|
|
57
|
+
*
|
|
58
|
+
* `record` and `write` queue snapshots and save them in the background, one
|
|
59
|
+
* write at a time, each holding the whole output. `flush` and `read` wait for
|
|
60
|
+
* what is queued and throw `OutputStoreError` when it could not be saved; the
|
|
61
|
+
* snapshots stay queued, so the next call tries again. On storage with `swap` a
|
|
62
|
+
* write never replaces what another store saved meanwhile.
|
|
63
|
+
*/
|
|
64
|
+
export class OutputStore {
|
|
65
|
+
storage;
|
|
66
|
+
scope;
|
|
67
|
+
stream;
|
|
68
|
+
pending = new Map();
|
|
69
|
+
writing;
|
|
70
|
+
failure;
|
|
71
|
+
constructor(storage, scope, schemas) {
|
|
72
|
+
this.storage = storage;
|
|
73
|
+
this.scope = scope;
|
|
74
|
+
this.stream = new ItemStream(schemas);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Fold one stream event into the output. Never throws: an event that cannot be
|
|
78
|
+
* read as display output is left out, with a warning, and the run goes on.
|
|
79
|
+
*/
|
|
80
|
+
record(event) {
|
|
81
|
+
try {
|
|
82
|
+
for (const item of this.stream.push(event)) {
|
|
83
|
+
this.queue(OutputSnapshotSchema.parse(item));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (cause) {
|
|
87
|
+
console.warn(`[noetic] left a stream event out of the display output of "${this.scope}":`, cause);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.start();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Save display snapshots, each replacing the stored one with its id, and resolve
|
|
94
|
+
* the whole stored output.
|
|
95
|
+
*
|
|
96
|
+
* @throws OutputStoreError when `items` are not snapshots, or storage fails.
|
|
97
|
+
*/
|
|
98
|
+
async write(items) {
|
|
99
|
+
const parsed = OutputSnapshotSchema.array().safeParse(items);
|
|
100
|
+
if (!parsed.success) {
|
|
101
|
+
throw this.error('Could not read display snapshots.', parsed.error);
|
|
102
|
+
}
|
|
103
|
+
for (const item of parsed.data) {
|
|
104
|
+
this.queue(item);
|
|
105
|
+
}
|
|
106
|
+
return this.read();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The stored output, after everything queued is saved.
|
|
110
|
+
*
|
|
111
|
+
* @throws OutputStoreError when storage fails or holds something else.
|
|
112
|
+
*/
|
|
113
|
+
async read() {
|
|
114
|
+
await this.flush();
|
|
115
|
+
return (await this.load()).items;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Wait until everything queued is saved.
|
|
119
|
+
*
|
|
120
|
+
* @throws OutputStoreError when it could not be. What was queued stays queued,
|
|
121
|
+
* and every flush throws until a later one saves it.
|
|
122
|
+
*/
|
|
123
|
+
async flush() {
|
|
124
|
+
this.start();
|
|
125
|
+
while (this.writing) {
|
|
126
|
+
await this.writing;
|
|
127
|
+
}
|
|
128
|
+
// Not cleared here: two callers waiting on one failed write are both told.
|
|
129
|
+
if (this.failure) {
|
|
130
|
+
throw this.failure;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
queue(item) {
|
|
134
|
+
this.pending.set(keyOf(item), structuredClone(item));
|
|
135
|
+
}
|
|
136
|
+
start() {
|
|
137
|
+
if (this.pending.size === 0 || this.writing) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
this.writing = this.drain().then(() => {
|
|
141
|
+
this.failure = undefined;
|
|
142
|
+
this.writing = undefined;
|
|
143
|
+
this.start();
|
|
144
|
+
}, (cause) => {
|
|
145
|
+
this.failure =
|
|
146
|
+
cause instanceof OutputStoreError
|
|
147
|
+
? cause
|
|
148
|
+
: this.error('Could not write streamed output.', cause);
|
|
149
|
+
this.writing = undefined;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/** What storage holds under the key, as it is stored and as snapshots. */
|
|
153
|
+
async load() {
|
|
154
|
+
try {
|
|
155
|
+
const raw = (await this.storage.get(outputKey(this.scope))) ?? null;
|
|
156
|
+
return {
|
|
157
|
+
raw,
|
|
158
|
+
items: OutputSnapshotSchema.array().parse(raw ?? []),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
catch (cause) {
|
|
162
|
+
throw this.error('Could not read streamed output.', cause);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Save what is queued over what is stored, until nothing is queued. */
|
|
166
|
+
async drain() {
|
|
167
|
+
while (this.pending.size > 0) {
|
|
168
|
+
const batch = new Map(this.pending);
|
|
169
|
+
this.pending.clear();
|
|
170
|
+
try {
|
|
171
|
+
await this.save(batch);
|
|
172
|
+
}
|
|
173
|
+
catch (cause) {
|
|
174
|
+
// Put back what was not saved, behind anything newer queued meanwhile.
|
|
175
|
+
for (const [key, item] of batch) {
|
|
176
|
+
if (!this.pending.has(key)) {
|
|
177
|
+
this.pending.set(key, item);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
throw cause;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async save(batch) {
|
|
185
|
+
let lost = 0;
|
|
186
|
+
for (let attempt = 1;;) {
|
|
187
|
+
try {
|
|
188
|
+
if (await this.saveOver(batch)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
catch (cause) {
|
|
193
|
+
if (cause instanceof OutputStoreError) {
|
|
194
|
+
throw cause;
|
|
195
|
+
}
|
|
196
|
+
if (attempt >= WRITE_ATTEMPTS) {
|
|
197
|
+
throw this.error('Could not write streamed output.', cause);
|
|
198
|
+
}
|
|
199
|
+
attempt++;
|
|
200
|
+
await wait(WRITE_RETRY_MS);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
// Another writer saved first: merge over what it wrote.
|
|
204
|
+
lost++;
|
|
205
|
+
if (lost >= SWAP_ATTEMPTS) {
|
|
206
|
+
throw this.error('Could not write streamed output.', new Error(`Lost to another writer ${lost} times.`));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Write `batch` over what is stored now. On storage with `swap` the write lands
|
|
212
|
+
* only if nothing else was saved since the read, and false means it did not.
|
|
213
|
+
*/
|
|
214
|
+
async saveOver(batch) {
|
|
215
|
+
const stored = await this.load();
|
|
216
|
+
const held = new Map(stored.items.map((item) => [
|
|
217
|
+
keyOf(item),
|
|
218
|
+
item,
|
|
219
|
+
]));
|
|
220
|
+
for (const [key, item] of batch) {
|
|
221
|
+
held.set(key, item);
|
|
222
|
+
}
|
|
223
|
+
const next = [
|
|
224
|
+
...held.values(),
|
|
225
|
+
];
|
|
226
|
+
const key = outputKey(this.scope);
|
|
227
|
+
if (!this.storage.swap) {
|
|
228
|
+
await this.storage.set(key, next);
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
return this.storage.swap(key, stored.raw, next);
|
|
232
|
+
}
|
|
233
|
+
error(message, cause) {
|
|
234
|
+
return new OutputStoreError({
|
|
235
|
+
scope: this.scope,
|
|
236
|
+
message,
|
|
237
|
+
cause,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
@@ -1,14 +1,35 @@
|
|
|
1
|
-
import type { ItemSchemaRegistry, StreamingItem } from '@noetic-tools/types';
|
|
1
|
+
import type { ItemSchemaRegistry, StreamEvent, StreamingItem } from '@noetic-tools/types';
|
|
2
2
|
import type { EventBroadcaster } from './event-broadcaster.js';
|
|
3
3
|
/** @internal Yield text deltas from the session broadcaster. */
|
|
4
4
|
export declare function filterTextStream(broadcaster: EventBroadcaster): AsyncIterable<string>;
|
|
5
5
|
/** @internal Yield reasoning-token deltas from the session broadcaster. */
|
|
6
6
|
export declare function filterReasoningStream(broadcaster: EventBroadcaster): AsyncIterable<string>;
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* multiple turns within a session don't collide.
|
|
7
|
+
/**
|
|
8
|
+
* @internal Yield cumulative StreamingItem snapshots.
|
|
10
9
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
* SDK events cover model output only; framework-authored items — turn input
|
|
11
|
+
* messages, between-rounds injections, tool results — arrive via the
|
|
12
|
+
* `item_appended` framework event and are yielded already complete.
|
|
13
|
+
*/
|
|
14
14
|
export declare function buildItemStream(broadcaster: EventBroadcaster, itemSchemas?: ItemSchemaRegistry): AsyncIterable<StreamingItem>;
|
|
15
|
+
/**
|
|
16
|
+
* @internal Folds stream events into item snapshots, one event at a time: what
|
|
17
|
+
* `getItemStream` yields and what an `OutputStore` keeps.
|
|
18
|
+
*
|
|
19
|
+
* An event that names its item is matched by that id, which keeps model calls
|
|
20
|
+
* running at once apart. One that does not is matched by round and output index,
|
|
21
|
+
* the round counted from `response.created`, so the items of later tool rounds
|
|
22
|
+
* and later turns do not collide.
|
|
23
|
+
*/
|
|
24
|
+
export declare class ItemStream {
|
|
25
|
+
private readonly accumulators;
|
|
26
|
+
private readonly byId;
|
|
27
|
+
private roundOffset;
|
|
28
|
+
private readonly itemSchemas;
|
|
29
|
+
constructor(itemSchemas?: ItemSchemaRegistry);
|
|
30
|
+
/** The snapshots `event` produces: none, or one. */
|
|
31
|
+
push(event: StreamEvent): Generator<StreamingItem>;
|
|
32
|
+
private add;
|
|
33
|
+
/** The accumulator `event` is about. `missingIndex` stands in for an absent output index. */
|
|
34
|
+
private find;
|
|
35
|
+
}
|