@providerprotocol/ai 0.0.12 → 0.0.13

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 (44) hide show
  1. package/dist/anthropic/index.d.ts +51 -15
  2. package/dist/anthropic/index.js +54 -19
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-SUNYWHTH.js → chunk-MOU4U3PO.js} +55 -3
  5. package/dist/chunk-MOU4U3PO.js.map +1 -0
  6. package/dist/{chunk-Y6Q7JCNP.js → chunk-MSR5P65T.js} +1 -1
  7. package/dist/chunk-MSR5P65T.js.map +1 -0
  8. package/dist/{chunk-W4BB4BG2.js → chunk-SVYROCLD.js} +31 -11
  9. package/dist/chunk-SVYROCLD.js.map +1 -0
  10. package/dist/chunk-U4JJC2YX.js +234 -0
  11. package/dist/chunk-U4JJC2YX.js.map +1 -0
  12. package/dist/{chunk-X5G4EHL7.js → chunk-Z7RBRCRN.js} +1 -1
  13. package/dist/chunk-Z7RBRCRN.js.map +1 -0
  14. package/dist/google/index.d.ts +376 -7
  15. package/dist/google/index.js +127 -15
  16. package/dist/google/index.js.map +1 -1
  17. package/dist/http/index.d.ts +222 -25
  18. package/dist/http/index.js +3 -3
  19. package/dist/index.d.ts +1482 -198
  20. package/dist/index.js +230 -46
  21. package/dist/index.js.map +1 -1
  22. package/dist/ollama/index.d.ts +92 -20
  23. package/dist/ollama/index.js +17 -7
  24. package/dist/ollama/index.js.map +1 -1
  25. package/dist/openai/index.d.ts +340 -61
  26. package/dist/openai/index.js +57 -15
  27. package/dist/openai/index.js.map +1 -1
  28. package/dist/openrouter/index.d.ts +107 -51
  29. package/dist/openrouter/index.js +36 -8
  30. package/dist/openrouter/index.js.map +1 -1
  31. package/dist/provider-mKkz7Q9U.d.ts +488 -0
  32. package/dist/retry-Dh70lgr0.d.ts +508 -0
  33. package/dist/xai/index.d.ts +97 -22
  34. package/dist/xai/index.js +55 -19
  35. package/dist/xai/index.js.map +1 -1
  36. package/package.json +8 -3
  37. package/dist/chunk-CUCRF5W6.js +0 -136
  38. package/dist/chunk-CUCRF5W6.js.map +0 -1
  39. package/dist/chunk-SUNYWHTH.js.map +0 -1
  40. package/dist/chunk-W4BB4BG2.js.map +0 -1
  41. package/dist/chunk-X5G4EHL7.js.map +0 -1
  42. package/dist/chunk-Y6Q7JCNP.js.map +0 -1
  43. package/dist/provider-CUJWjgNl.d.ts +0 -192
  44. package/dist/retry-I2661_rv.d.ts +0 -118
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createProvider
3
- } from "./chunk-Y6Q7JCNP.js";
3
+ } from "./chunk-MSR5P65T.js";
4
4
  import {
5
5
  AssistantMessage,
6
6
  Message,
@@ -10,21 +10,21 @@ import {
10
10
  isAssistantMessage,
11
11
  isToolResultMessage,
12
12
  isUserMessage
13
- } from "./chunk-W4BB4BG2.js";
13
+ } from "./chunk-SVYROCLD.js";
14
14
  import {
15
15
  ExponentialBackoff,
16
16
  LinearBackoff,
17
17
  NoRetry,
18
18
  RetryAfterStrategy,
19
19
  TokenBucket
20
- } from "./chunk-CUCRF5W6.js";
21
- import "./chunk-X5G4EHL7.js";
20
+ } from "./chunk-U4JJC2YX.js";
21
+ import "./chunk-Z7RBRCRN.js";
22
22
  import {
23
23
  DynamicKey,
24
24
  RoundRobinKeys,
25
25
  UPPError,
26
26
  WeightedKeys
27
- } from "./chunk-SUNYWHTH.js";
27
+ } from "./chunk-MOU4U3PO.js";
28
28
 
29
29
  // src/types/turn.ts
30
30
  function createTurn(messages, toolExecutions, usage, cycles, data) {
@@ -46,6 +46,8 @@ function emptyUsage() {
46
46
  inputTokens: 0,
47
47
  outputTokens: 0,
48
48
  totalTokens: 0,
49
+ cacheReadTokens: 0,
50
+ cacheWriteTokens: 0,
49
51
  cycles: []
50
52
  };
51
53
  }
@@ -53,9 +55,13 @@ function aggregateUsage(usages) {
53
55
  const cycles = [];
54
56
  let inputTokens = 0;
55
57
  let outputTokens = 0;
58
+ let cacheReadTokens = 0;
59
+ let cacheWriteTokens = 0;
56
60
  for (const usage of usages) {
57
61
  inputTokens += usage.inputTokens;
58
62
  outputTokens += usage.outputTokens;
63
+ cacheReadTokens += usage.cacheReadTokens;
64
+ cacheWriteTokens += usage.cacheWriteTokens;
59
65
  cycles.push({
60
66
  inputTokens: usage.inputTokens,
61
67
  outputTokens: usage.outputTokens
@@ -65,6 +71,8 @@ function aggregateUsage(usages) {
65
71
  inputTokens,
66
72
  outputTokens,
67
73
  totalTokens: inputTokens + outputTokens,
74
+ cacheReadTokens,
75
+ cacheWriteTokens,
68
76
  cycles
69
77
  };
70
78
  }
@@ -313,7 +321,6 @@ async function executeGenerate(model, config, system, params, tools, toolStrateg
313
321
  const data = structure ? structuredData : void 0;
314
322
  return createTurn(
315
323
  allMessages.slice(history.length),
316
- // Only messages from this turn
317
324
  toolExecutions,
318
325
  aggregateUsage(usages),
319
326
  cycles,
@@ -430,10 +437,25 @@ async function executeTools(message, tools, toolStrategy, executions, onEvent) {
430
437
  }
431
438
  const startTime = Date.now();
432
439
  onEvent?.(toolExecutionStart(call.toolCallId, tool.name, startTime, index));
433
- await toolStrategy?.onToolCall?.(tool, call.arguments);
440
+ let effectiveParams = call.arguments;
441
+ await toolStrategy?.onToolCall?.(tool, effectiveParams);
434
442
  if (toolStrategy?.onBeforeCall) {
435
- const shouldRun = await toolStrategy.onBeforeCall(tool, call.arguments);
436
- if (!shouldRun) {
443
+ const beforeResult = await toolStrategy.onBeforeCall(tool, effectiveParams);
444
+ const isBeforeCallResult = (value) => typeof value === "object" && value !== null && "proceed" in value;
445
+ if (isBeforeCallResult(beforeResult)) {
446
+ if (!beforeResult.proceed) {
447
+ const endTime = Date.now();
448
+ onEvent?.(toolExecutionEnd(call.toolCallId, tool.name, "Tool execution skipped", true, endTime, index));
449
+ return {
450
+ toolCallId: call.toolCallId,
451
+ result: "Tool execution skipped",
452
+ isError: true
453
+ };
454
+ }
455
+ if (beforeResult.params !== void 0) {
456
+ effectiveParams = beforeResult.params;
457
+ }
458
+ } else if (!beforeResult) {
437
459
  const endTime = Date.now();
438
460
  onEvent?.(toolExecutionEnd(call.toolCallId, tool.name, "Tool execution skipped", true, endTime, index));
439
461
  return {
@@ -446,7 +468,7 @@ async function executeTools(message, tools, toolStrategy, executions, onEvent) {
446
468
  let approved = true;
447
469
  if (tool.approval) {
448
470
  try {
449
- approved = await tool.approval(call.arguments);
471
+ approved = await tool.approval(effectiveParams);
450
472
  } catch (error) {
451
473
  throw error;
452
474
  }
@@ -456,7 +478,7 @@ async function executeTools(message, tools, toolStrategy, executions, onEvent) {
456
478
  const execution = {
457
479
  toolName: tool.name,
458
480
  toolCallId: call.toolCallId,
459
- arguments: call.arguments,
481
+ arguments: effectiveParams,
460
482
  result: "Tool execution denied",
461
483
  isError: true,
462
484
  duration: endTime - startTime,
@@ -471,13 +493,19 @@ async function executeTools(message, tools, toolStrategy, executions, onEvent) {
471
493
  };
472
494
  }
473
495
  try {
474
- const result = await tool.run(call.arguments);
496
+ let result = await tool.run(effectiveParams);
475
497
  const endTime = Date.now();
476
- await toolStrategy?.onAfterCall?.(tool, call.arguments, result);
498
+ if (toolStrategy?.onAfterCall) {
499
+ const afterResult = await toolStrategy.onAfterCall(tool, effectiveParams, result);
500
+ const isAfterCallResult = (value) => typeof value === "object" && value !== null && "result" in value;
501
+ if (isAfterCallResult(afterResult)) {
502
+ result = afterResult.result;
503
+ }
504
+ }
477
505
  const execution = {
478
506
  toolName: tool.name,
479
507
  toolCallId: call.toolCallId,
480
- arguments: call.arguments,
508
+ arguments: effectiveParams,
481
509
  result,
482
510
  isError: false,
483
511
  duration: endTime - startTime,
@@ -492,12 +520,12 @@ async function executeTools(message, tools, toolStrategy, executions, onEvent) {
492
520
  };
493
521
  } catch (error) {
494
522
  const endTime = Date.now();
495
- await toolStrategy?.onError?.(tool, call.arguments, error);
523
+ await toolStrategy?.onError?.(tool, effectiveParams, error);
496
524
  const errorMessage = error instanceof Error ? error.message : String(error);
497
525
  const execution = {
498
526
  toolName: tool.name,
499
527
  toolCallId: call.toolCallId,
500
- arguments: call.arguments,
528
+ arguments: effectiveParams,
501
529
  result: errorMessage,
502
530
  isError: true,
503
531
  duration: endTime - startTime,
@@ -550,9 +578,13 @@ function validateMediaCapabilities(messages, capabilities, providerName) {
550
578
  // src/core/image.ts
551
579
  import { readFile } from "fs/promises";
552
580
  var Image = class _Image {
581
+ /** The underlying image source (bytes, base64, or URL) */
553
582
  source;
583
+ /** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
554
584
  mimeType;
585
+ /** Image width in pixels, if known */
555
586
  width;
587
+ /** Image height in pixels, if known */
556
588
  height;
557
589
  constructor(source, mimeType, width, height) {
558
590
  this.source = source;
@@ -561,13 +593,19 @@ var Image = class _Image {
561
593
  this.height = height;
562
594
  }
563
595
  /**
564
- * Check if this image has data loaded (false for URL sources)
596
+ * Whether this image has data loaded in memory.
597
+ *
598
+ * Returns `false` for URL-sourced images that reference external resources.
599
+ * These must be fetched before their data can be accessed.
565
600
  */
566
601
  get hasData() {
567
602
  return this.source.type !== "url";
568
603
  }
569
604
  /**
570
- * Convert to base64 string (throws if source is URL)
605
+ * Converts the image to a base64-encoded string.
606
+ *
607
+ * @returns The image data as a base64 string
608
+ * @throws {Error} When the source is a URL (data must be fetched first)
571
609
  */
572
610
  toBase64() {
573
611
  if (this.source.type === "base64") {
@@ -581,14 +619,20 @@ var Image = class _Image {
581
619
  throw new Error("Cannot convert URL image to base64. Fetch the image first.");
582
620
  }
583
621
  /**
584
- * Convert to data URL (throws if source is URL)
622
+ * Converts the image to a data URL suitable for embedding in HTML or CSS.
623
+ *
624
+ * @returns A data URL in the format `data:{mimeType};base64,{data}`
625
+ * @throws {Error} When the source is a URL (data must be fetched first)
585
626
  */
586
627
  toDataUrl() {
587
628
  const base64 = this.toBase64();
588
629
  return `data:${this.mimeType};base64,${base64}`;
589
630
  }
590
631
  /**
591
- * Get raw bytes (throws if source is URL)
632
+ * Gets the image data as raw bytes.
633
+ *
634
+ * @returns The image data as a Uint8Array
635
+ * @throws {Error} When the source is a URL (data must be fetched first)
592
636
  */
593
637
  toBytes() {
594
638
  if (this.source.type === "bytes") {
@@ -605,7 +649,10 @@ var Image = class _Image {
605
649
  throw new Error("Cannot get bytes from URL image. Fetch the image first.");
606
650
  }
607
651
  /**
608
- * Get the URL (only for URL sources)
652
+ * Gets the URL for URL-sourced images.
653
+ *
654
+ * @returns The image URL
655
+ * @throws {Error} When the source is not a URL
609
656
  */
610
657
  toUrl() {
611
658
  if (this.source.type === "url") {
@@ -614,7 +661,9 @@ var Image = class _Image {
614
661
  throw new Error("This image does not have a URL source.");
615
662
  }
616
663
  /**
617
- * Convert to ImageBlock for use in messages
664
+ * Converts this Image to an ImageBlock for use in UPP messages.
665
+ *
666
+ * @returns An ImageBlock that can be included in message content arrays
618
667
  */
619
668
  toBlock() {
620
669
  return {
@@ -626,7 +675,18 @@ var Image = class _Image {
626
675
  };
627
676
  }
628
677
  /**
629
- * Create from file path (reads file into memory)
678
+ * Creates an Image by reading a file from disk.
679
+ *
680
+ * The file is read into memory as bytes. MIME type is automatically
681
+ * detected from the file extension.
682
+ *
683
+ * @param path - Path to the image file
684
+ * @returns Promise resolving to an Image with the file contents
685
+ *
686
+ * @example
687
+ * ```typescript
688
+ * const image = await Image.fromPath('./photos/vacation.jpg');
689
+ * ```
630
690
  */
631
691
  static async fromPath(path) {
632
692
  const data = await readFile(path);
@@ -637,26 +697,63 @@ var Image = class _Image {
637
697
  );
638
698
  }
639
699
  /**
640
- * Create from URL reference (does not fetch - providers handle URL conversion)
700
+ * Creates an Image from a URL reference.
701
+ *
702
+ * The URL is stored as a reference and not fetched. Providers will handle
703
+ * URL-to-data conversion if needed. MIME type is detected from the URL
704
+ * path if not provided.
705
+ *
706
+ * @param url - URL pointing to the image
707
+ * @param mimeType - Optional MIME type override
708
+ * @returns An Image referencing the URL
709
+ *
710
+ * @example
711
+ * ```typescript
712
+ * const image = Image.fromUrl('https://example.com/logo.png');
713
+ * ```
641
714
  */
642
715
  static fromUrl(url, mimeType) {
643
716
  const detected = mimeType || detectMimeTypeFromUrl(url);
644
717
  return new _Image({ type: "url", url }, detected);
645
718
  }
646
719
  /**
647
- * Create from raw bytes
720
+ * Creates an Image from raw byte data.
721
+ *
722
+ * @param data - The image data as a Uint8Array
723
+ * @param mimeType - The MIME type of the image
724
+ * @returns An Image containing the byte data
725
+ *
726
+ * @example
727
+ * ```typescript
728
+ * const image = Image.fromBytes(pngData, 'image/png');
729
+ * ```
648
730
  */
649
731
  static fromBytes(data, mimeType) {
650
732
  return new _Image({ type: "bytes", data }, mimeType);
651
733
  }
652
734
  /**
653
- * Create from base64 string
735
+ * Creates an Image from a base64-encoded string.
736
+ *
737
+ * @param base64 - The base64-encoded image data (without data URL prefix)
738
+ * @param mimeType - The MIME type of the image
739
+ * @returns An Image containing the base64 data
740
+ *
741
+ * @example
742
+ * ```typescript
743
+ * const image = Image.fromBase64(base64String, 'image/jpeg');
744
+ * ```
654
745
  */
655
746
  static fromBase64(base64, mimeType) {
656
747
  return new _Image({ type: "base64", data: base64 }, mimeType);
657
748
  }
658
749
  /**
659
- * Create from an existing ImageBlock
750
+ * Creates an Image from an existing ImageBlock.
751
+ *
752
+ * Useful for converting content blocks received from providers back
753
+ * into Image instances for further processing.
754
+ *
755
+ * @param block - An ImageBlock from message content
756
+ * @returns An Image with the block's source and metadata
660
757
  */
661
758
  static fromBlock(block) {
662
759
  return new _Image(
@@ -729,7 +826,9 @@ var Thread = class _Thread {
729
826
  /** Last update timestamp */
730
827
  _updatedAt;
731
828
  /**
732
- * Create a new thread, optionally with initial messages
829
+ * Creates a new thread instance.
830
+ *
831
+ * @param messages - Optional initial messages to populate the thread
733
832
  */
734
833
  constructor(messages) {
735
834
  this.id = generateId();
@@ -737,16 +836,23 @@ var Thread = class _Thread {
737
836
  this._createdAt = /* @__PURE__ */ new Date();
738
837
  this._updatedAt = /* @__PURE__ */ new Date();
739
838
  }
740
- /** All messages in the thread (readonly) */
839
+ /**
840
+ * All messages in the thread (readonly).
841
+ */
741
842
  get messages() {
742
843
  return this._messages;
743
844
  }
744
- /** Number of messages */
845
+ /**
846
+ * Number of messages in the thread.
847
+ */
745
848
  get length() {
746
849
  return this._messages.length;
747
850
  }
748
851
  /**
749
- * Append messages from a turn
852
+ * Appends all messages from a Turn to the thread.
853
+ *
854
+ * @param turn - The Turn containing messages to append
855
+ * @returns This thread instance for chaining
750
856
  */
751
857
  append(turn) {
752
858
  this._messages.push(...turn.messages);
@@ -754,7 +860,10 @@ var Thread = class _Thread {
754
860
  return this;
755
861
  }
756
862
  /**
757
- * Add raw messages
863
+ * Adds raw messages to the thread.
864
+ *
865
+ * @param messages - Messages to add
866
+ * @returns This thread instance for chaining
758
867
  */
759
868
  push(...messages) {
760
869
  this._messages.push(...messages);
@@ -762,7 +871,19 @@ var Thread = class _Thread {
762
871
  return this;
763
872
  }
764
873
  /**
765
- * Add a user message
874
+ * Adds a user message to the thread.
875
+ *
876
+ * @param content - String or array of content blocks
877
+ * @returns This thread instance for chaining
878
+ *
879
+ * @example
880
+ * ```typescript
881
+ * thread.user('Hello, world!');
882
+ * thread.user([
883
+ * { type: 'text', text: 'Describe this image:' },
884
+ * { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
885
+ * ]);
886
+ * ```
766
887
  */
767
888
  user(content) {
768
889
  this._messages.push(new UserMessage(content));
@@ -770,7 +891,15 @@ var Thread = class _Thread {
770
891
  return this;
771
892
  }
772
893
  /**
773
- * Add an assistant message
894
+ * Adds an assistant message to the thread.
895
+ *
896
+ * @param content - String or array of content blocks
897
+ * @returns This thread instance for chaining
898
+ *
899
+ * @example
900
+ * ```typescript
901
+ * thread.assistant('I can help with that!');
902
+ * ```
774
903
  */
775
904
  assistant(content) {
776
905
  this._messages.push(new AssistantMessage(content));
@@ -778,25 +907,53 @@ var Thread = class _Thread {
778
907
  return this;
779
908
  }
780
909
  /**
781
- * Get messages by type
910
+ * Filters messages by type.
911
+ *
912
+ * @param type - The message type to filter by
913
+ * @returns Array of messages matching the type
914
+ *
915
+ * @example
916
+ * ```typescript
917
+ * const userMessages = thread.filter('user');
918
+ * const assistantMessages = thread.filter('assistant');
919
+ * ```
782
920
  */
783
921
  filter(type) {
784
922
  return this._messages.filter((m) => m.type === type);
785
923
  }
786
924
  /**
787
- * Get the last N messages
925
+ * Returns the last N messages from the thread.
926
+ *
927
+ * @param count - Number of messages to return
928
+ * @returns Array of the last N messages
929
+ *
930
+ * @example
931
+ * ```typescript
932
+ * const recent = thread.tail(5);
933
+ * ```
788
934
  */
789
935
  tail(count) {
790
936
  return this._messages.slice(-count);
791
937
  }
792
938
  /**
793
- * Create a new thread with a subset of messages
939
+ * Creates a new thread with a subset of messages.
940
+ *
941
+ * @param start - Start index (inclusive)
942
+ * @param end - End index (exclusive)
943
+ * @returns New Thread containing the sliced messages
944
+ *
945
+ * @example
946
+ * ```typescript
947
+ * const subset = thread.slice(0, 10);
948
+ * ```
794
949
  */
795
950
  slice(start, end) {
796
951
  return new _Thread(this._messages.slice(start, end));
797
952
  }
798
953
  /**
799
- * Clear all messages
954
+ * Removes all messages from the thread.
955
+ *
956
+ * @returns This thread instance for chaining
800
957
  */
801
958
  clear() {
802
959
  this._messages = [];
@@ -804,13 +961,23 @@ var Thread = class _Thread {
804
961
  return this;
805
962
  }
806
963
  /**
807
- * Convert to plain message array
964
+ * Converts the thread to a plain message array.
965
+ *
966
+ * @returns Copy of the internal message array
808
967
  */
809
968
  toMessages() {
810
969
  return [...this._messages];
811
970
  }
812
971
  /**
813
- * Serialize to JSON
972
+ * Serializes the thread to JSON format.
973
+ *
974
+ * @returns JSON-serializable representation of the thread
975
+ *
976
+ * @example
977
+ * ```typescript
978
+ * const json = thread.toJSON();
979
+ * localStorage.setItem('thread', JSON.stringify(json));
980
+ * ```
814
981
  */
815
982
  toJSON() {
816
983
  return {
@@ -821,7 +988,16 @@ var Thread = class _Thread {
821
988
  };
822
989
  }
823
990
  /**
824
- * Deserialize from JSON
991
+ * Deserializes a thread from JSON format.
992
+ *
993
+ * @param json - The JSON representation to deserialize
994
+ * @returns Reconstructed Thread instance
995
+ *
996
+ * @example
997
+ * ```typescript
998
+ * const json = JSON.parse(localStorage.getItem('thread'));
999
+ * const thread = Thread.fromJSON(json);
1000
+ * ```
825
1001
  */
826
1002
  static fromJSON(json) {
827
1003
  const messages = json.messages.map((m) => _Thread.messageFromJSON(m));
@@ -832,13 +1008,22 @@ var Thread = class _Thread {
832
1008
  return thread;
833
1009
  }
834
1010
  /**
835
- * Iterate over messages
1011
+ * Enables iteration over messages with for...of loops.
1012
+ *
1013
+ * @returns Iterator over the thread's messages
1014
+ *
1015
+ * @example
1016
+ * ```typescript
1017
+ * for (const message of thread) {
1018
+ * console.log(message.text);
1019
+ * }
1020
+ * ```
836
1021
  */
837
1022
  [Symbol.iterator]() {
838
1023
  return this._messages[Symbol.iterator]();
839
1024
  }
840
1025
  /**
841
- * Convert a message to JSON
1026
+ * Converts a message to JSON format.
842
1027
  */
843
1028
  messageToJSON(m) {
844
1029
  const base = {
@@ -859,7 +1044,7 @@ var Thread = class _Thread {
859
1044
  return base;
860
1045
  }
861
1046
  /**
862
- * Reconstruct a message from JSON
1047
+ * Reconstructs a message from JSON format.
863
1048
  */
864
1049
  static messageFromJSON(json) {
865
1050
  const options = {
@@ -885,9 +1070,8 @@ var Thread = class _Thread {
885
1070
 
886
1071
  // src/index.ts
887
1072
  var ai = {
1073
+ /** LLM instance factory */
888
1074
  llm
889
- // embedding, // Coming soon
890
- // image, // Coming soon
891
1075
  };
892
1076
  export {
893
1077
  AssistantMessage,