@tanstack/ai-anthropic 0.8.6 → 0.8.7

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