@tanstack/ai-anthropic 0.8.6 → 0.8.8
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/esm/adapters/summarize.d.ts +15 -28
- package/dist/esm/adapters/summarize.js +9 -138
- package/dist/esm/adapters/summarize.js.map +1 -1
- package/dist/esm/adapters/text.js +105 -107
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -2
- package/package.json +3 -3
- package/src/adapters/summarize.ts +29 -197
- package/src/adapters/text.ts +105 -111
- package/src/index.ts +2 -3
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { EventType } from "@tanstack/ai";
|
|
1
2
|
import { BaseTextAdapter } from "@tanstack/ai/adapters";
|
|
2
3
|
import { convertToolsToProviderFormat } from "../tools/tool-converter.js";
|
|
3
4
|
import { validateTextProviderOptions } from "../text/text-provider-options.js";
|
|
4
5
|
import { createAnthropicClient, generateId, getAnthropicApiKeyFromEnv } from "../utils/client.js";
|
|
5
|
-
const asChunk = (chunk) => chunk;
|
|
6
6
|
class AnthropicTextAdapter extends BaseTextAdapter {
|
|
7
7
|
constructor(config, model) {
|
|
8
8
|
super({}, model);
|
|
@@ -40,8 +40,8 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
40
40
|
error,
|
|
41
41
|
source: "anthropic.chatStream"
|
|
42
42
|
});
|
|
43
|
-
yield
|
|
44
|
-
type:
|
|
43
|
+
yield {
|
|
44
|
+
type: EventType.RUN_ERROR,
|
|
45
45
|
model: options.model,
|
|
46
46
|
timestamp: Date.now(),
|
|
47
47
|
message: err.message || "Unknown error occurred",
|
|
@@ -50,7 +50,7 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
50
50
|
message: err.message || "Unknown error occurred",
|
|
51
51
|
code: err.code || String(err.status)
|
|
52
52
|
}
|
|
53
|
-
}
|
|
53
|
+
};
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
@@ -378,7 +378,6 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
378
378
|
let accumulatedContent = "";
|
|
379
379
|
let accumulatedThinking = "";
|
|
380
380
|
let accumulatedSignature = "";
|
|
381
|
-
const timestamp = Date.now();
|
|
382
381
|
const toolCallsMap = /* @__PURE__ */ new Map();
|
|
383
382
|
let currentToolIndex = -1;
|
|
384
383
|
const runId = options.runId ?? genId();
|
|
@@ -398,13 +397,14 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
398
397
|
});
|
|
399
398
|
if (!hasEmittedRunStarted) {
|
|
400
399
|
hasEmittedRunStarted = true;
|
|
401
|
-
yield
|
|
402
|
-
type:
|
|
400
|
+
yield {
|
|
401
|
+
type: EventType.RUN_STARTED,
|
|
403
402
|
runId,
|
|
404
403
|
threadId,
|
|
405
404
|
model,
|
|
406
|
-
timestamp
|
|
407
|
-
|
|
405
|
+
timestamp: Date.now(),
|
|
406
|
+
parentRunId: options.parentRunId
|
|
407
|
+
};
|
|
408
408
|
}
|
|
409
409
|
if (event.type === "content_block_start") {
|
|
410
410
|
currentBlockType = event.content_block.type;
|
|
@@ -421,84 +421,84 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
421
421
|
accumulatedSignature = "";
|
|
422
422
|
stepId = genId();
|
|
423
423
|
reasoningMessageId = genId();
|
|
424
|
-
yield
|
|
425
|
-
type:
|
|
424
|
+
yield {
|
|
425
|
+
type: EventType.REASONING_START,
|
|
426
426
|
messageId: reasoningMessageId,
|
|
427
427
|
model,
|
|
428
|
-
timestamp
|
|
429
|
-
}
|
|
430
|
-
yield
|
|
431
|
-
type:
|
|
428
|
+
timestamp: Date.now()
|
|
429
|
+
};
|
|
430
|
+
yield {
|
|
431
|
+
type: EventType.REASONING_MESSAGE_START,
|
|
432
432
|
messageId: reasoningMessageId,
|
|
433
433
|
role: "reasoning",
|
|
434
434
|
model,
|
|
435
|
-
timestamp
|
|
436
|
-
}
|
|
437
|
-
yield
|
|
438
|
-
type:
|
|
435
|
+
timestamp: Date.now()
|
|
436
|
+
};
|
|
437
|
+
yield {
|
|
438
|
+
type: EventType.STEP_STARTED,
|
|
439
439
|
stepName: stepId,
|
|
440
440
|
stepId,
|
|
441
441
|
model,
|
|
442
|
-
timestamp,
|
|
442
|
+
timestamp: Date.now(),
|
|
443
443
|
stepType: "thinking"
|
|
444
|
-
}
|
|
444
|
+
};
|
|
445
445
|
}
|
|
446
446
|
} else if (event.type === "content_block_delta") {
|
|
447
447
|
if (event.delta.type === "text_delta") {
|
|
448
448
|
if (reasoningMessageId && !hasClosedReasoning) {
|
|
449
449
|
hasClosedReasoning = true;
|
|
450
|
-
yield
|
|
451
|
-
type:
|
|
450
|
+
yield {
|
|
451
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
452
452
|
messageId: reasoningMessageId,
|
|
453
453
|
model,
|
|
454
|
-
timestamp
|
|
455
|
-
}
|
|
456
|
-
yield
|
|
457
|
-
type:
|
|
454
|
+
timestamp: Date.now()
|
|
455
|
+
};
|
|
456
|
+
yield {
|
|
457
|
+
type: EventType.REASONING_END,
|
|
458
458
|
messageId: reasoningMessageId,
|
|
459
459
|
model,
|
|
460
|
-
timestamp
|
|
461
|
-
}
|
|
460
|
+
timestamp: Date.now()
|
|
461
|
+
};
|
|
462
462
|
}
|
|
463
463
|
if (!hasEmittedTextMessageStart) {
|
|
464
464
|
hasEmittedTextMessageStart = true;
|
|
465
|
-
yield
|
|
466
|
-
type:
|
|
465
|
+
yield {
|
|
466
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
467
467
|
messageId,
|
|
468
468
|
model,
|
|
469
|
-
timestamp,
|
|
469
|
+
timestamp: Date.now(),
|
|
470
470
|
role: "assistant"
|
|
471
|
-
}
|
|
471
|
+
};
|
|
472
472
|
}
|
|
473
473
|
const delta = event.delta.text;
|
|
474
474
|
accumulatedContent += delta;
|
|
475
|
-
yield
|
|
476
|
-
type:
|
|
475
|
+
yield {
|
|
476
|
+
type: EventType.TEXT_MESSAGE_CONTENT,
|
|
477
477
|
messageId,
|
|
478
478
|
model,
|
|
479
|
-
timestamp,
|
|
479
|
+
timestamp: Date.now(),
|
|
480
480
|
delta,
|
|
481
481
|
content: accumulatedContent
|
|
482
|
-
}
|
|
482
|
+
};
|
|
483
483
|
} else if (event.delta.type === "thinking_delta") {
|
|
484
484
|
const delta = event.delta.thinking;
|
|
485
485
|
accumulatedThinking += delta;
|
|
486
|
-
yield
|
|
487
|
-
type:
|
|
486
|
+
yield {
|
|
487
|
+
type: EventType.REASONING_MESSAGE_CONTENT,
|
|
488
488
|
messageId: reasoningMessageId,
|
|
489
489
|
delta,
|
|
490
490
|
model,
|
|
491
|
-
timestamp
|
|
492
|
-
}
|
|
493
|
-
yield
|
|
494
|
-
type:
|
|
491
|
+
timestamp: Date.now()
|
|
492
|
+
};
|
|
493
|
+
yield {
|
|
494
|
+
type: EventType.STEP_FINISHED,
|
|
495
495
|
stepName: stepId || genId(),
|
|
496
496
|
stepId: stepId || genId(),
|
|
497
497
|
model,
|
|
498
|
-
timestamp,
|
|
498
|
+
timestamp: Date.now(),
|
|
499
499
|
delta,
|
|
500
500
|
content: accumulatedThinking
|
|
501
|
-
}
|
|
501
|
+
};
|
|
502
502
|
} else if (event.delta.type === "signature_delta") {
|
|
503
503
|
accumulatedSignature += event.delta.signature || "";
|
|
504
504
|
} else if (event.delta.type === "input_json_delta") {
|
|
@@ -506,55 +506,55 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
506
506
|
if (existing) {
|
|
507
507
|
if (!existing.started) {
|
|
508
508
|
existing.started = true;
|
|
509
|
-
yield
|
|
510
|
-
type:
|
|
509
|
+
yield {
|
|
510
|
+
type: EventType.TOOL_CALL_START,
|
|
511
511
|
toolCallId: existing.id,
|
|
512
512
|
toolCallName: existing.name,
|
|
513
513
|
toolName: existing.name,
|
|
514
514
|
model,
|
|
515
|
-
timestamp,
|
|
515
|
+
timestamp: Date.now(),
|
|
516
516
|
index: currentToolIndex
|
|
517
|
-
}
|
|
517
|
+
};
|
|
518
518
|
}
|
|
519
519
|
existing.input += event.delta.partial_json;
|
|
520
|
-
yield
|
|
521
|
-
type:
|
|
520
|
+
yield {
|
|
521
|
+
type: EventType.TOOL_CALL_ARGS,
|
|
522
522
|
toolCallId: existing.id,
|
|
523
523
|
model,
|
|
524
|
-
timestamp,
|
|
524
|
+
timestamp: Date.now(),
|
|
525
525
|
delta: event.delta.partial_json,
|
|
526
526
|
args: existing.input
|
|
527
|
-
}
|
|
527
|
+
};
|
|
528
528
|
}
|
|
529
529
|
}
|
|
530
530
|
} else if (event.type === "content_block_stop") {
|
|
531
531
|
if (currentBlockType === "thinking") {
|
|
532
532
|
if (accumulatedSignature && stepId) {
|
|
533
|
-
yield
|
|
534
|
-
type:
|
|
533
|
+
yield {
|
|
534
|
+
type: EventType.STEP_FINISHED,
|
|
535
535
|
stepName: stepId,
|
|
536
536
|
stepId,
|
|
537
537
|
model,
|
|
538
|
-
timestamp,
|
|
538
|
+
timestamp: Date.now(),
|
|
539
539
|
delta: "",
|
|
540
540
|
content: accumulatedThinking,
|
|
541
541
|
signature: accumulatedSignature
|
|
542
|
-
}
|
|
542
|
+
};
|
|
543
543
|
}
|
|
544
544
|
} else if (currentBlockType === "tool_use") {
|
|
545
545
|
const existing = toolCallsMap.get(currentToolIndex);
|
|
546
546
|
if (existing) {
|
|
547
547
|
if (!existing.started) {
|
|
548
548
|
existing.started = true;
|
|
549
|
-
yield
|
|
550
|
-
type:
|
|
549
|
+
yield {
|
|
550
|
+
type: EventType.TOOL_CALL_START,
|
|
551
551
|
toolCallId: existing.id,
|
|
552
552
|
toolCallName: existing.name,
|
|
553
553
|
toolName: existing.name,
|
|
554
554
|
model,
|
|
555
|
-
timestamp,
|
|
555
|
+
timestamp: Date.now(),
|
|
556
556
|
index: currentToolIndex
|
|
557
|
-
}
|
|
557
|
+
};
|
|
558
558
|
}
|
|
559
559
|
let parsedInput = {};
|
|
560
560
|
try {
|
|
@@ -563,118 +563,117 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
563
563
|
} catch {
|
|
564
564
|
parsedInput = {};
|
|
565
565
|
}
|
|
566
|
-
yield
|
|
567
|
-
type:
|
|
566
|
+
yield {
|
|
567
|
+
type: EventType.TOOL_CALL_END,
|
|
568
568
|
toolCallId: existing.id,
|
|
569
569
|
toolCallName: existing.name,
|
|
570
570
|
toolName: existing.name,
|
|
571
571
|
model,
|
|
572
|
-
timestamp,
|
|
572
|
+
timestamp: Date.now(),
|
|
573
573
|
input: parsedInput
|
|
574
|
-
}
|
|
574
|
+
};
|
|
575
575
|
hasEmittedTextMessageStart = false;
|
|
576
576
|
}
|
|
577
577
|
} else {
|
|
578
578
|
if (hasEmittedTextMessageStart && accumulatedContent) {
|
|
579
|
-
yield
|
|
580
|
-
type:
|
|
579
|
+
yield {
|
|
580
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
581
581
|
messageId,
|
|
582
582
|
model,
|
|
583
|
-
timestamp
|
|
584
|
-
}
|
|
583
|
+
timestamp: Date.now()
|
|
584
|
+
};
|
|
585
585
|
}
|
|
586
586
|
}
|
|
587
587
|
currentBlockType = null;
|
|
588
588
|
} else if (event.type === "message_stop") {
|
|
589
589
|
if (reasoningMessageId && !hasClosedReasoning) {
|
|
590
590
|
hasClosedReasoning = true;
|
|
591
|
-
yield
|
|
592
|
-
type:
|
|
591
|
+
yield {
|
|
592
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
593
593
|
messageId: reasoningMessageId,
|
|
594
594
|
model,
|
|
595
|
-
timestamp
|
|
596
|
-
}
|
|
597
|
-
yield
|
|
598
|
-
type:
|
|
595
|
+
timestamp: Date.now()
|
|
596
|
+
};
|
|
597
|
+
yield {
|
|
598
|
+
type: EventType.REASONING_END,
|
|
599
599
|
messageId: reasoningMessageId,
|
|
600
600
|
model,
|
|
601
|
-
timestamp
|
|
602
|
-
}
|
|
601
|
+
timestamp: Date.now()
|
|
602
|
+
};
|
|
603
603
|
}
|
|
604
604
|
if (!hasEmittedRunFinished) {
|
|
605
|
-
yield
|
|
606
|
-
type:
|
|
605
|
+
yield {
|
|
606
|
+
type: EventType.RUN_FINISHED,
|
|
607
607
|
runId,
|
|
608
608
|
threadId,
|
|
609
609
|
model,
|
|
610
|
-
timestamp,
|
|
610
|
+
timestamp: Date.now(),
|
|
611
611
|
finishReason: "stop"
|
|
612
|
-
}
|
|
612
|
+
};
|
|
613
613
|
}
|
|
614
614
|
} else if (event.type === "message_delta") {
|
|
615
615
|
if (event.delta.stop_reason) {
|
|
616
616
|
hasEmittedRunFinished = true;
|
|
617
617
|
if (reasoningMessageId && !hasClosedReasoning) {
|
|
618
618
|
hasClosedReasoning = true;
|
|
619
|
-
yield
|
|
620
|
-
type:
|
|
619
|
+
yield {
|
|
620
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
621
621
|
messageId: reasoningMessageId,
|
|
622
622
|
model,
|
|
623
|
-
timestamp
|
|
624
|
-
}
|
|
625
|
-
yield
|
|
626
|
-
type:
|
|
623
|
+
timestamp: Date.now()
|
|
624
|
+
};
|
|
625
|
+
yield {
|
|
626
|
+
type: EventType.REASONING_END,
|
|
627
627
|
messageId: reasoningMessageId,
|
|
628
628
|
model,
|
|
629
|
-
timestamp
|
|
630
|
-
}
|
|
629
|
+
timestamp: Date.now()
|
|
630
|
+
};
|
|
631
631
|
}
|
|
632
632
|
switch (event.delta.stop_reason) {
|
|
633
633
|
case "tool_use": {
|
|
634
|
-
yield
|
|
635
|
-
type:
|
|
634
|
+
yield {
|
|
635
|
+
type: EventType.RUN_FINISHED,
|
|
636
636
|
runId,
|
|
637
637
|
threadId,
|
|
638
638
|
model,
|
|
639
|
-
timestamp,
|
|
639
|
+
timestamp: Date.now(),
|
|
640
640
|
finishReason: "tool_calls",
|
|
641
641
|
usage: {
|
|
642
642
|
promptTokens: event.usage.input_tokens || 0,
|
|
643
643
|
completionTokens: event.usage.output_tokens || 0,
|
|
644
644
|
totalTokens: (event.usage.input_tokens || 0) + (event.usage.output_tokens || 0)
|
|
645
645
|
}
|
|
646
|
-
}
|
|
646
|
+
};
|
|
647
647
|
break;
|
|
648
648
|
}
|
|
649
649
|
case "max_tokens": {
|
|
650
|
-
yield
|
|
651
|
-
type:
|
|
652
|
-
runId,
|
|
650
|
+
yield {
|
|
651
|
+
type: EventType.RUN_ERROR,
|
|
653
652
|
model,
|
|
654
|
-
timestamp,
|
|
653
|
+
timestamp: Date.now(),
|
|
655
654
|
message: "The response was cut off because the maximum token limit was reached.",
|
|
656
655
|
code: "max_tokens",
|
|
657
656
|
error: {
|
|
658
657
|
message: "The response was cut off because the maximum token limit was reached.",
|
|
659
658
|
code: "max_tokens"
|
|
660
659
|
}
|
|
661
|
-
}
|
|
660
|
+
};
|
|
662
661
|
break;
|
|
663
662
|
}
|
|
664
663
|
default: {
|
|
665
|
-
yield
|
|
666
|
-
type:
|
|
664
|
+
yield {
|
|
665
|
+
type: EventType.RUN_FINISHED,
|
|
667
666
|
runId,
|
|
668
667
|
threadId,
|
|
669
668
|
model,
|
|
670
|
-
timestamp,
|
|
669
|
+
timestamp: Date.now(),
|
|
671
670
|
finishReason: "stop",
|
|
672
671
|
usage: {
|
|
673
672
|
promptTokens: event.usage.input_tokens || 0,
|
|
674
673
|
completionTokens: event.usage.output_tokens || 0,
|
|
675
674
|
totalTokens: (event.usage.input_tokens || 0) + (event.usage.output_tokens || 0)
|
|
676
675
|
}
|
|
677
|
-
}
|
|
676
|
+
};
|
|
678
677
|
}
|
|
679
678
|
}
|
|
680
679
|
}
|
|
@@ -686,18 +685,17 @@ class AnthropicTextAdapter extends BaseTextAdapter {
|
|
|
686
685
|
error,
|
|
687
686
|
source: "anthropic.processAnthropicStream"
|
|
688
687
|
});
|
|
689
|
-
yield
|
|
690
|
-
type:
|
|
691
|
-
runId,
|
|
688
|
+
yield {
|
|
689
|
+
type: EventType.RUN_ERROR,
|
|
692
690
|
model,
|
|
693
|
-
timestamp,
|
|
691
|
+
timestamp: Date.now(),
|
|
694
692
|
message: err.message || "Unknown error occurred",
|
|
695
693
|
code: err.code || String(err.status),
|
|
696
694
|
error: {
|
|
697
695
|
message: err.message || "Unknown error occurred",
|
|
698
696
|
code: err.code || String(err.status)
|
|
699
697
|
}
|
|
700
|
-
}
|
|
698
|
+
};
|
|
701
699
|
}
|
|
702
700
|
}
|
|
703
701
|
}
|