@nanoagent/kernel 0.0.0 → 0.1.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.
Files changed (2) hide show
  1. package/dist/index.js +67 -26
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -227,6 +227,43 @@ async function buildModelResult(result) {
227
227
  providerMetadata
228
228
  };
229
229
  }
230
+ function createAsyncQueue() {
231
+ const values = [];
232
+ const waiters = [];
233
+ let closed = false;
234
+ return {
235
+ push(value) {
236
+ const waiter = waiters.shift();
237
+ if (waiter) {
238
+ waiter({ done: false, value });
239
+ return;
240
+ }
241
+ values.push(value);
242
+ },
243
+ close() {
244
+ closed = true;
245
+ for (const waiter of waiters.splice(0)) {
246
+ waiter({ done: true, value: undefined });
247
+ }
248
+ },
249
+ [Symbol.asyncIterator]() {
250
+ return {
251
+ next() {
252
+ if (values.length > 0) {
253
+ const value = values.shift();
254
+ return Promise.resolve({ done: false, value });
255
+ }
256
+ if (closed) {
257
+ return Promise.resolve({ done: true, value: undefined });
258
+ }
259
+ return new Promise(resolve => {
260
+ waiters.push(resolve);
261
+ });
262
+ }
263
+ };
264
+ }
265
+ };
266
+ }
230
267
  function makeBaseArgs(state) {
231
268
  const readonlyState = deepFreeze(cloneUnknown(state));
232
269
  return {
@@ -570,8 +607,8 @@ export async function* runAgent(options) {
570
607
  yield* emitEvents(modelStartedResult.events);
571
608
  return;
572
609
  }
573
- const streamEvents = [];
574
- const callModelResult = await Effect.runPromise(runMiddleware({
610
+ const streamEvents = createAsyncQueue();
611
+ const callModelResultPromise = Effect.runPromise(runMiddleware({
575
612
  input: {
576
613
  ...makeBaseArgs(snapshot),
577
614
  args: modelArgs,
@@ -591,17 +628,23 @@ export async function* runAgent(options) {
591
628
  }),
592
629
  catch: toError
593
630
  }));
594
- const attemptStreamEvents = [];
595
631
  for await (const part of rawResult.fullStream) {
596
632
  await Effect.runPromise(checkNotAborted(options.signal));
597
- attemptStreamEvents.push({
633
+ const event = {
598
634
  type: 'stream_part',
599
635
  runId: snapshot.runId,
600
636
  turnId: currentTurn.turnId,
601
637
  turn: currentTurn.turn,
602
638
  createdAt: nowIso(),
603
639
  part
604
- });
640
+ };
641
+ await Effect.runPromise(fromAgentResult(() => hooks.onStreamUpdate?.({
642
+ ...makeBaseArgs(snapshot),
643
+ createdAt: event.createdAt,
644
+ part: event.part,
645
+ turn: cloneTurn(currentTurn)
646
+ })));
647
+ streamEvents.push(event);
605
648
  }
606
649
  const modelResult = cloneModelResult(await buildModelResult(rawResult));
607
650
  const pendingToolCalls = modelResult.finishReason === 'tool-calls'
@@ -611,7 +654,6 @@ export async function* runAgent(options) {
611
654
  }))).map(cloneToolCall)
612
655
  : [];
613
656
  const completedAt = nowIso();
614
- streamEvents.push(...attemptStreamEvents);
615
657
  return {
616
658
  args: {
617
659
  ...input.args,
@@ -623,17 +665,12 @@ export async function* runAgent(options) {
623
665
  result: modelResult
624
666
  };
625
667
  }
626
- }));
627
- const completedAt = nowIso();
628
- for (const event of streamEvents) {
629
- await Effect.runPromise(fromAgentResult(() => hooks.onStreamUpdate?.({
630
- ...makeBaseArgs(snapshot),
631
- createdAt: event.createdAt,
632
- part: event.part,
633
- turn: cloneTurn(currentTurn)
634
- })));
635
- yield* emitEvents([event]);
668
+ })).finally(() => streamEvents.close());
669
+ for await (const event of streamEvents) {
670
+ yield event;
636
671
  }
672
+ const callModelResult = await callModelResultPromise;
673
+ const completedAt = nowIso();
637
674
  const modelCompletedHook = await Effect.runPromise(fromAgentResult(() => hooks.onModelCompleted?.({
638
675
  ...makeBaseArgs(snapshot),
639
676
  args: callModelResult.args,
@@ -1010,16 +1047,20 @@ export async function* runAgent(options) {
1010
1047
  status: { type: 'failed', phase, error: failure, createdAt }
1011
1048
  };
1012
1049
  snapshot = failedSnapshot;
1013
- return snapshotState(failedSnapshot, [
1014
- {
1015
- type: 'run_failed',
1016
- runId: snapshot.runId,
1017
- revision: snapshot.revision,
1018
- createdAt,
1019
- phase,
1020
- error: failure
1021
- }
1022
- ]);
1050
+ const runFailedEvent = {
1051
+ type: 'run_failed',
1052
+ runId: snapshot.runId,
1053
+ revision: snapshot.revision,
1054
+ createdAt,
1055
+ phase,
1056
+ error: failure
1057
+ };
1058
+ try {
1059
+ return await snapshotState(failedSnapshot, [runFailedEvent]);
1060
+ }
1061
+ catch {
1062
+ return [runFailedEvent];
1063
+ }
1023
1064
  }
1024
1065
  try {
1025
1066
  await Effect.runPromise(checkNotAborted(options.signal));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanoagent/kernel",
3
- "version": "0.0.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "packageManager": "bun@1.3.13",
6
6
  "publishConfig": {