@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.
@@ -110,6 +110,40 @@ function completeStreamingItem(item) {
110
110
  isComplete: true,
111
111
  };
112
112
  }
113
+ /** The id of the item an SDK event is about, when the event names one. */
114
+ function itemIdOf(event) {
115
+ const { itemId, item } = event.data;
116
+ if (typeof itemId === 'string') {
117
+ return itemId;
118
+ }
119
+ return isItemRecord(item) && typeof item.id === 'string' ? item.id : undefined;
120
+ }
121
+ /** Framework-authored snapshots: a custom item in progress, or an appended item. */
122
+ function* frameworkSnapshots(event) {
123
+ // A custom item's event streams in progress under the id its final item takes.
124
+ if (event.type.endsWith(':custom_item_event')) {
125
+ yield frameworkCast({
126
+ ...event.data,
127
+ isComplete: false,
128
+ });
129
+ return;
130
+ }
131
+ if (event.type.endsWith(':item_appended') &&
132
+ typeof event.data.item === 'object' &&
133
+ event.data.item !== null) {
134
+ yield {
135
+ ...frameworkCast(event.data.item),
136
+ isComplete: true,
137
+ };
138
+ }
139
+ }
140
+ /** How each delta event changes the item it is about. */
141
+ const ACCUMULATOR_UPDATES = {
142
+ 'response.output_text.delta': updateMessageAccumulator,
143
+ 'response.output_text.done': updateMessageAccumulator,
144
+ 'response.function_call_arguments.delta': updateFunctionCallAccumulator,
145
+ 'response.function_call_arguments.done': updateFunctionCallAccumulator,
146
+ };
113
147
  //#endregion
114
148
  //#region Public stream filters
115
149
  /** @internal Yield text deltas from the session broadcaster. */
@@ -136,92 +170,103 @@ export async function* filterReasoningStream(broadcaster) {
136
170
  }
137
171
  }
138
172
  }
139
- /** @internal Yield cumulative StreamingItem snapshots. Uses roundOffset keyed by
140
- * response.created so accumulators across multiple tool rounds and across
141
- * multiple turns within a session don't collide.
173
+ /**
174
+ * @internal Yield cumulative StreamingItem snapshots.
142
175
  *
143
- * SDK events cover model output only; framework-authored items — turn input
144
- * messages, between-rounds injections, tool results — arrive via the
145
- * `item_appended` framework event and are yielded already complete. */
176
+ * SDK events cover model output only; framework-authored items — turn input
177
+ * messages, between-rounds injections, tool results — arrive via the
178
+ * `item_appended` framework event and are yielded already complete.
179
+ */
146
180
  export async function* buildItemStream(broadcaster, itemSchemas = defaultItemSchemaRegistry) {
147
- const accumulators = new Map();
148
- let roundOffset = 0;
181
+ const stream = new ItemStream(itemSchemas);
149
182
  for await (const event of broadcaster) {
183
+ yield* stream.push(event);
184
+ }
185
+ }
186
+ //#endregion
187
+ //#region ItemStream
188
+ /**
189
+ * @internal Folds stream events into item snapshots, one event at a time: what
190
+ * `getItemStream` yields and what an `OutputStore` keeps.
191
+ *
192
+ * An event that names its item is matched by that id, which keeps model calls
193
+ * running at once apart. One that does not is matched by round and output index,
194
+ * the round counted from `response.created`, so the items of later tool rounds
195
+ * and later turns do not collide.
196
+ */
197
+ export class ItemStream {
198
+ accumulators = new Map();
199
+ byId = new Map();
200
+ roundOffset = 0;
201
+ itemSchemas;
202
+ constructor(itemSchemas = defaultItemSchemaRegistry) {
203
+ this.itemSchemas = itemSchemas;
204
+ }
205
+ /** The snapshots `event` produces: none, or one. */
206
+ *push(event) {
150
207
  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
- }
159
- if (event.type.endsWith(':item_appended') &&
160
- typeof event.data.item === 'object' &&
161
- event.data.item !== null) {
162
- yield {
163
- ...frameworkCast(event.data.item),
164
- isComplete: true,
165
- };
166
- }
167
- continue;
208
+ yield* frameworkSnapshots(event);
209
+ return;
168
210
  }
169
211
  if (event.source !== 'sdk') {
170
- continue;
212
+ return;
171
213
  }
172
214
  if (event.type === 'response.created') {
173
- roundOffset++;
174
- continue;
215
+ this.roundOffset++;
216
+ return;
175
217
  }
176
218
  if (event.type === 'response.output_item.added') {
177
- const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : accumulators.size;
178
- const key = accumulatorKey(roundOffset, outputIndex);
179
- const acc = createAccumulatorFromItemAdded(event, itemSchemas);
180
- if (acc) {
181
- accumulators.set(key, acc);
182
- yield {
183
- ...acc.item,
184
- isComplete: false,
185
- };
186
- }
187
- continue;
219
+ yield* this.add(event);
220
+ return;
188
221
  }
189
222
  if (event.type === 'response.output_item.done') {
190
- const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : -1;
191
- const key = accumulatorKey(roundOffset, outputIndex);
192
- const acc = accumulators.get(key);
223
+ const acc = this.find(event, -1);
193
224
  if (acc) {
194
225
  acc.isComplete = true;
195
226
  yield completeStreamingItem(acc.item);
196
227
  }
197
- continue;
228
+ return;
198
229
  }
199
- if (event.type === 'response.output_text.delta' || event.type === 'response.output_text.done') {
200
- const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : 0;
201
- const key = accumulatorKey(roundOffset, outputIndex);
202
- const acc = accumulators.get(key);
203
- if (acc) {
204
- updateMessageAccumulator(acc, event);
205
- yield {
206
- ...acc.item,
207
- isComplete: acc.isComplete,
208
- };
209
- }
210
- continue;
230
+ const update = ACCUMULATOR_UPDATES[event.type];
231
+ const acc = update ? this.find(event, 0) : undefined;
232
+ if (update && acc) {
233
+ update(acc, event);
234
+ yield {
235
+ ...acc.item,
236
+ isComplete: acc.isComplete,
237
+ };
211
238
  }
212
- if (event.type === 'response.function_call_arguments.delta' ||
213
- event.type === 'response.function_call_arguments.done') {
214
- const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : 0;
215
- const key = accumulatorKey(roundOffset, outputIndex);
216
- const acc = accumulators.get(key);
217
- if (acc) {
218
- updateFunctionCallAccumulator(acc, event);
219
- yield {
220
- ...acc.item,
221
- isComplete: acc.isComplete,
222
- };
223
- }
239
+ }
240
+ *add(event) {
241
+ const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : this.accumulators.size;
242
+ const acc = createAccumulatorFromItemAdded(event, this.itemSchemas);
243
+ if (!acc) {
244
+ return;
245
+ }
246
+ this.accumulators.set(accumulatorKey(this.roundOffset, outputIndex), acc);
247
+ if (acc.id !== '') {
248
+ this.byId.set(acc.id, acc);
249
+ }
250
+ yield {
251
+ ...acc.item,
252
+ isComplete: false,
253
+ };
254
+ }
255
+ /** The accumulator `event` is about. `missingIndex` stands in for an absent output index. */
256
+ find(event, missingIndex) {
257
+ const id = itemIdOf(event);
258
+ const named = id === undefined ? undefined : this.byId.get(id);
259
+ if (named) {
260
+ return named;
261
+ }
262
+ // An event that names an item this stream never saw is about the item at its
263
+ // own output index, which the provider renamed after `added`. One with no
264
+ // index is given to no item: it could be another call's.
265
+ if (id !== undefined && typeof event.outputIndex !== 'number') {
266
+ return undefined;
224
267
  }
268
+ const outputIndex = typeof event.outputIndex === 'number' ? event.outputIndex : missingIndex;
269
+ return this.accumulators.get(accumulatorKey(this.roundOffset, outputIndex));
225
270
  }
226
271
  }
227
272
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.22.0-next.1",
3
+ "version": "5.22.0-next.2",
4
4
  "type": "module",
5
5
  "description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
6
6
  "license": "Apache-2.0",
@@ -1373,6 +1373,15 @@
1373
1373
  "type": "integer",
1374
1374
  "exclusiveMinimum": 0,
1375
1375
  "maximum": 9007199254740991
1376
+ },
1377
+ "headers": {
1378
+ "type": "object",
1379
+ "propertyNames": {
1380
+ "type": "string"
1381
+ },
1382
+ "additionalProperties": {
1383
+ "type": "string"
1384
+ }
1376
1385
  }
1377
1386
  },
1378
1387
  "required": [
@@ -1738,6 +1747,15 @@
1738
1747
  "type": "integer",
1739
1748
  "exclusiveMinimum": 0,
1740
1749
  "maximum": 9007199254740991
1750
+ },
1751
+ "headers": {
1752
+ "type": "object",
1753
+ "propertyNames": {
1754
+ "type": "string"
1755
+ },
1756
+ "additionalProperties": {
1757
+ "type": "string"
1758
+ }
1741
1759
  }
1742
1760
  },
1743
1761
  "required": [
@@ -4463,6 +4481,15 @@
4463
4481
  "type": "integer",
4464
4482
  "exclusiveMinimum": 0,
4465
4483
  "maximum": 9007199254740991
4484
+ },
4485
+ "headers": {
4486
+ "type": "object",
4487
+ "propertyNames": {
4488
+ "type": "string"
4489
+ },
4490
+ "additionalProperties": {
4491
+ "type": "string"
4492
+ }
4466
4493
  }
4467
4494
  },
4468
4495
  "required": [