@copilotkit/runtime 1.55.0-next.7 → 1.55.0-next.9
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/CHANGELOG.md +18 -0
- package/dist/agent/index.cjs +184 -173
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts.map +1 -1
- package/dist/agent/index.d.mts.map +1 -1
- package/dist/agent/index.mjs +184 -173
- package/dist/agent/index.mjs.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +2 -2
- package/src/agent/__tests__/basic-agent.test.ts +455 -5
- package/src/agent/__tests__/test-helpers.ts +27 -12
- package/src/agent/index.ts +44 -20
- package/src/v2/runtime/__tests__/middleware-express.test.ts +24 -22
- package/src/v2/runtime/__tests__/middleware-single-express.test.ts +24 -22
package/dist/agent/index.mjs
CHANGED
|
@@ -319,6 +319,23 @@ This is state from the application that you can edit by calling AGUISendStateSna
|
|
|
319
319
|
const abortController = new AbortController();
|
|
320
320
|
this.abortController = abortController;
|
|
321
321
|
let terminalEventEmitted = false;
|
|
322
|
+
let messageId = randomUUID$1();
|
|
323
|
+
let reasoningMessageId = randomUUID$1();
|
|
324
|
+
let isInReasoning = false;
|
|
325
|
+
const closeReasoningIfOpen = () => {
|
|
326
|
+
if (!isInReasoning) return;
|
|
327
|
+
isInReasoning = false;
|
|
328
|
+
const reasoningMsgEnd = {
|
|
329
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
330
|
+
messageId: reasoningMessageId
|
|
331
|
+
};
|
|
332
|
+
subscriber.next(reasoningMsgEnd);
|
|
333
|
+
const reasoningEnd = {
|
|
334
|
+
type: EventType.REASONING_END,
|
|
335
|
+
messageId: reasoningMessageId
|
|
336
|
+
};
|
|
337
|
+
subscriber.next(reasoningEnd);
|
|
338
|
+
};
|
|
322
339
|
try {
|
|
323
340
|
streamTextParams.tools = {
|
|
324
341
|
...streamTextParams.tools,
|
|
@@ -376,8 +393,6 @@ This is state from the application that you can edit by calling AGUISendStateSna
|
|
|
376
393
|
...streamTextParams,
|
|
377
394
|
abortSignal: abortController.signal
|
|
378
395
|
});
|
|
379
|
-
let messageId = randomUUID$1();
|
|
380
|
-
let reasoningMessageId = randomUUID$1();
|
|
381
396
|
const toolCallStates = /* @__PURE__ */ new Map();
|
|
382
397
|
const ensureToolCallState = (toolCallId) => {
|
|
383
398
|
let state = toolCallStates.get(toolCallId);
|
|
@@ -391,194 +406,189 @@ This is state from the application that you can edit by calling AGUISendStateSna
|
|
|
391
406
|
}
|
|
392
407
|
return state;
|
|
393
408
|
};
|
|
394
|
-
for await (const part of response.fullStream)
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
terminalEventEmitted = true;
|
|
403
|
-
subscriber.complete();
|
|
404
|
-
break;
|
|
405
|
-
}
|
|
406
|
-
case "reasoning-start": {
|
|
407
|
-
const providedId = "id" in part ? part.id : void 0;
|
|
408
|
-
if (providedId && providedId !== "0") reasoningMessageId = providedId;
|
|
409
|
-
const reasoningStartEvent = {
|
|
410
|
-
type: EventType.REASONING_START,
|
|
411
|
-
messageId: reasoningMessageId
|
|
412
|
-
};
|
|
413
|
-
subscriber.next(reasoningStartEvent);
|
|
414
|
-
const reasoningMessageStart = {
|
|
415
|
-
type: EventType.REASONING_MESSAGE_START,
|
|
416
|
-
messageId: reasoningMessageId,
|
|
417
|
-
role: "reasoning"
|
|
418
|
-
};
|
|
419
|
-
subscriber.next(reasoningMessageStart);
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
case "reasoning-delta": {
|
|
423
|
-
const reasoningDeltaEvent = {
|
|
424
|
-
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
425
|
-
messageId: reasoningMessageId,
|
|
426
|
-
delta: ("text" in part ? part.text : part.delta) ?? ""
|
|
427
|
-
};
|
|
428
|
-
subscriber.next(reasoningDeltaEvent);
|
|
429
|
-
break;
|
|
430
|
-
}
|
|
431
|
-
case "reasoning-end": {
|
|
432
|
-
const reasoningMessageEnd = {
|
|
433
|
-
type: EventType.REASONING_MESSAGE_END,
|
|
434
|
-
messageId: reasoningMessageId
|
|
435
|
-
};
|
|
436
|
-
subscriber.next(reasoningMessageEnd);
|
|
437
|
-
const reasoningEndEvent = {
|
|
438
|
-
type: EventType.REASONING_END,
|
|
439
|
-
messageId: reasoningMessageId
|
|
440
|
-
};
|
|
441
|
-
subscriber.next(reasoningEndEvent);
|
|
442
|
-
break;
|
|
443
|
-
}
|
|
444
|
-
case "tool-input-start": {
|
|
445
|
-
const toolCallId = part.id;
|
|
446
|
-
const state = ensureToolCallState(toolCallId);
|
|
447
|
-
state.toolName = part.toolName;
|
|
448
|
-
if (!state.started) {
|
|
449
|
-
state.started = true;
|
|
450
|
-
const startEvent = {
|
|
451
|
-
type: EventType.TOOL_CALL_START,
|
|
452
|
-
parentMessageId: messageId,
|
|
453
|
-
toolCallId,
|
|
454
|
-
toolCallName: part.toolName
|
|
409
|
+
for await (const part of response.fullStream) {
|
|
410
|
+
if (part.type !== "reasoning-delta") closeReasoningIfOpen();
|
|
411
|
+
switch (part.type) {
|
|
412
|
+
case "abort": {
|
|
413
|
+
const abortEndEvent = {
|
|
414
|
+
type: EventType.RUN_FINISHED,
|
|
415
|
+
threadId: input.threadId,
|
|
416
|
+
runId: input.runId
|
|
455
417
|
};
|
|
456
|
-
subscriber.next(
|
|
418
|
+
subscriber.next(abortEndEvent);
|
|
419
|
+
terminalEventEmitted = true;
|
|
420
|
+
subscriber.complete();
|
|
421
|
+
break;
|
|
457
422
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
role: "assistant",
|
|
483
|
-
messageId,
|
|
484
|
-
delta: textDelta
|
|
485
|
-
};
|
|
486
|
-
subscriber.next(textEvent);
|
|
487
|
-
break;
|
|
488
|
-
}
|
|
489
|
-
case "tool-call": {
|
|
490
|
-
const toolCallId = part.toolCallId;
|
|
491
|
-
const state = ensureToolCallState(toolCallId);
|
|
492
|
-
state.toolName = part.toolName ?? state.toolName;
|
|
493
|
-
if (!state.started) {
|
|
494
|
-
state.started = true;
|
|
495
|
-
const startEvent = {
|
|
496
|
-
type: EventType.TOOL_CALL_START,
|
|
497
|
-
parentMessageId: messageId,
|
|
498
|
-
toolCallId,
|
|
499
|
-
toolCallName: part.toolName
|
|
423
|
+
case "reasoning-start": {
|
|
424
|
+
const providedId = "id" in part ? part.id : void 0;
|
|
425
|
+
reasoningMessageId = providedId && providedId !== "0" ? providedId : randomUUID$1();
|
|
426
|
+
const reasoningStartEvent = {
|
|
427
|
+
type: EventType.REASONING_START,
|
|
428
|
+
messageId: reasoningMessageId
|
|
429
|
+
};
|
|
430
|
+
subscriber.next(reasoningStartEvent);
|
|
431
|
+
const reasoningMessageStart = {
|
|
432
|
+
type: EventType.REASONING_MESSAGE_START,
|
|
433
|
+
messageId: reasoningMessageId,
|
|
434
|
+
role: "reasoning"
|
|
435
|
+
};
|
|
436
|
+
subscriber.next(reasoningMessageStart);
|
|
437
|
+
isInReasoning = true;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
case "reasoning-delta": {
|
|
441
|
+
const delta = part.text ?? "";
|
|
442
|
+
if (!delta) break;
|
|
443
|
+
const reasoningDeltaEvent = {
|
|
444
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
445
|
+
messageId: reasoningMessageId,
|
|
446
|
+
delta
|
|
500
447
|
};
|
|
501
|
-
subscriber.next(
|
|
448
|
+
subscriber.next(reasoningDeltaEvent);
|
|
449
|
+
break;
|
|
502
450
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
451
|
+
case "reasoning-end": break;
|
|
452
|
+
case "tool-input-start": {
|
|
453
|
+
const toolCallId = part.id;
|
|
454
|
+
const state = ensureToolCallState(toolCallId);
|
|
455
|
+
state.toolName = part.toolName;
|
|
456
|
+
if (!state.started) {
|
|
457
|
+
state.started = true;
|
|
458
|
+
const startEvent = {
|
|
459
|
+
type: EventType.TOOL_CALL_START,
|
|
460
|
+
parentMessageId: messageId,
|
|
461
|
+
toolCallId,
|
|
462
|
+
toolCallName: part.toolName
|
|
463
|
+
};
|
|
464
|
+
subscriber.next(startEvent);
|
|
510
465
|
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
case "tool-input-delta": {
|
|
469
|
+
const toolCallId = part.id;
|
|
470
|
+
const state = ensureToolCallState(toolCallId);
|
|
471
|
+
state.hasArgsDelta = true;
|
|
472
|
+
const argsEvent = {
|
|
473
|
+
type: EventType.TOOL_CALL_ARGS,
|
|
474
|
+
toolCallId,
|
|
475
|
+
delta: part.delta
|
|
476
|
+
};
|
|
477
|
+
subscriber.next(argsEvent);
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
case "tool-input-end": break;
|
|
481
|
+
case "text-start": {
|
|
482
|
+
const providedId = "id" in part ? part.id : void 0;
|
|
483
|
+
messageId = providedId && providedId !== "0" ? providedId : randomUUID$1();
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
case "text-delta": {
|
|
487
|
+
const textDelta = "text" in part ? part.text : "";
|
|
488
|
+
const textEvent = {
|
|
489
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
490
|
+
role: "assistant",
|
|
491
|
+
messageId,
|
|
492
|
+
delta: textDelta
|
|
493
|
+
};
|
|
494
|
+
subscriber.next(textEvent);
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
case "tool-call": {
|
|
498
|
+
const toolCallId = part.toolCallId;
|
|
499
|
+
const state = ensureToolCallState(toolCallId);
|
|
500
|
+
state.toolName = part.toolName ?? state.toolName;
|
|
501
|
+
if (!state.started) {
|
|
502
|
+
state.started = true;
|
|
503
|
+
const startEvent = {
|
|
504
|
+
type: EventType.TOOL_CALL_START,
|
|
505
|
+
parentMessageId: messageId,
|
|
514
506
|
toolCallId,
|
|
515
|
-
|
|
507
|
+
toolCallName: part.toolName
|
|
516
508
|
};
|
|
517
|
-
subscriber.next(
|
|
518
|
-
state.hasArgsDelta = true;
|
|
509
|
+
subscriber.next(startEvent);
|
|
519
510
|
}
|
|
511
|
+
if (!state.hasArgsDelta && "input" in part && part.input !== void 0) {
|
|
512
|
+
let serializedInput = "";
|
|
513
|
+
if (typeof part.input === "string") serializedInput = part.input;
|
|
514
|
+
else try {
|
|
515
|
+
serializedInput = JSON.stringify(part.input);
|
|
516
|
+
} catch {
|
|
517
|
+
serializedInput = String(part.input);
|
|
518
|
+
}
|
|
519
|
+
if (serializedInput.length > 0) {
|
|
520
|
+
const argsEvent = {
|
|
521
|
+
type: EventType.TOOL_CALL_ARGS,
|
|
522
|
+
toolCallId,
|
|
523
|
+
delta: serializedInput
|
|
524
|
+
};
|
|
525
|
+
subscriber.next(argsEvent);
|
|
526
|
+
state.hasArgsDelta = true;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
if (!state.ended) {
|
|
530
|
+
state.ended = true;
|
|
531
|
+
const endEvent = {
|
|
532
|
+
type: EventType.TOOL_CALL_END,
|
|
533
|
+
toolCallId
|
|
534
|
+
};
|
|
535
|
+
subscriber.next(endEvent);
|
|
536
|
+
}
|
|
537
|
+
break;
|
|
520
538
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
|
|
539
|
+
case "tool-result": {
|
|
540
|
+
const toolResult = "output" in part ? part.output : null;
|
|
541
|
+
const toolName = "toolName" in part ? part.toolName : "";
|
|
542
|
+
toolCallStates.delete(part.toolCallId);
|
|
543
|
+
if (toolName === "AGUISendStateSnapshot" && toolResult && typeof toolResult === "object") {
|
|
544
|
+
const stateSnapshotEvent = {
|
|
545
|
+
type: EventType.STATE_SNAPSHOT,
|
|
546
|
+
snapshot: toolResult.snapshot
|
|
547
|
+
};
|
|
548
|
+
subscriber.next(stateSnapshotEvent);
|
|
549
|
+
} else if (toolName === "AGUISendStateDelta" && toolResult && typeof toolResult === "object") {
|
|
550
|
+
const stateDeltaEvent = {
|
|
551
|
+
type: EventType.STATE_DELTA,
|
|
552
|
+
delta: toolResult.delta
|
|
553
|
+
};
|
|
554
|
+
subscriber.next(stateDeltaEvent);
|
|
555
|
+
}
|
|
556
|
+
const resultEvent = {
|
|
557
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
558
|
+
role: "tool",
|
|
559
|
+
messageId: randomUUID$1(),
|
|
560
|
+
toolCallId: part.toolCallId,
|
|
561
|
+
content: JSON.stringify(toolResult)
|
|
526
562
|
};
|
|
527
|
-
subscriber.next(
|
|
563
|
+
subscriber.next(resultEvent);
|
|
564
|
+
break;
|
|
528
565
|
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
toolCallStates.delete(part.toolCallId);
|
|
535
|
-
if (toolName === "AGUISendStateSnapshot" && toolResult && typeof toolResult === "object") {
|
|
536
|
-
const stateSnapshotEvent = {
|
|
537
|
-
type: EventType.STATE_SNAPSHOT,
|
|
538
|
-
snapshot: toolResult.snapshot
|
|
566
|
+
case "finish": {
|
|
567
|
+
const finishedEvent = {
|
|
568
|
+
type: EventType.RUN_FINISHED,
|
|
569
|
+
threadId: input.threadId,
|
|
570
|
+
runId: input.runId
|
|
539
571
|
};
|
|
540
|
-
subscriber.next(
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
572
|
+
subscriber.next(finishedEvent);
|
|
573
|
+
terminalEventEmitted = true;
|
|
574
|
+
subscriber.complete();
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
case "error": {
|
|
578
|
+
if (abortController.signal.aborted) break;
|
|
579
|
+
const runErrorEvent = {
|
|
580
|
+
type: EventType.RUN_ERROR,
|
|
581
|
+
message: part.error + ""
|
|
545
582
|
};
|
|
546
|
-
subscriber.next(
|
|
583
|
+
subscriber.next(runErrorEvent);
|
|
584
|
+
terminalEventEmitted = true;
|
|
585
|
+
subscriber.error(part.error);
|
|
586
|
+
break;
|
|
547
587
|
}
|
|
548
|
-
const resultEvent = {
|
|
549
|
-
type: EventType.TOOL_CALL_RESULT,
|
|
550
|
-
role: "tool",
|
|
551
|
-
messageId: randomUUID$1(),
|
|
552
|
-
toolCallId: part.toolCallId,
|
|
553
|
-
content: JSON.stringify(toolResult)
|
|
554
|
-
};
|
|
555
|
-
subscriber.next(resultEvent);
|
|
556
|
-
break;
|
|
557
|
-
}
|
|
558
|
-
case "finish": {
|
|
559
|
-
const finishedEvent = {
|
|
560
|
-
type: EventType.RUN_FINISHED,
|
|
561
|
-
threadId: input.threadId,
|
|
562
|
-
runId: input.runId
|
|
563
|
-
};
|
|
564
|
-
subscriber.next(finishedEvent);
|
|
565
|
-
terminalEventEmitted = true;
|
|
566
|
-
subscriber.complete();
|
|
567
|
-
break;
|
|
568
|
-
}
|
|
569
|
-
case "error": {
|
|
570
|
-
if (abortController.signal.aborted) break;
|
|
571
|
-
const runErrorEvent = {
|
|
572
|
-
type: EventType.RUN_ERROR,
|
|
573
|
-
message: part.error + ""
|
|
574
|
-
};
|
|
575
|
-
subscriber.next(runErrorEvent);
|
|
576
|
-
terminalEventEmitted = true;
|
|
577
|
-
subscriber.error(part.error);
|
|
578
|
-
break;
|
|
579
588
|
}
|
|
580
589
|
}
|
|
581
590
|
if (!terminalEventEmitted) {
|
|
591
|
+
closeReasoningIfOpen();
|
|
582
592
|
if (abortController.signal.aborted) {} else {
|
|
583
593
|
const finishedEvent = {
|
|
584
594
|
type: EventType.RUN_FINISHED,
|
|
@@ -591,6 +601,7 @@ This is state from the application that you can edit by calling AGUISendStateSna
|
|
|
591
601
|
subscriber.complete();
|
|
592
602
|
}
|
|
593
603
|
} catch (error) {
|
|
604
|
+
closeReasoningIfOpen();
|
|
594
605
|
if (abortController.signal.aborted) subscriber.complete();
|
|
595
606
|
else {
|
|
596
607
|
const runErrorEvent = {
|