@providerprotocol/ai 0.0.16 → 0.0.18

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 (41) hide show
  1. package/README.md +111 -8
  2. package/dist/anthropic/index.d.ts +1 -1
  3. package/dist/anthropic/index.js +5 -3
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/{chunk-MOU4U3PO.js → chunk-5FEAOEXV.js} +4 -68
  6. package/dist/chunk-5FEAOEXV.js.map +1 -0
  7. package/dist/chunk-DZQHVGNV.js +71 -0
  8. package/dist/chunk-DZQHVGNV.js.map +1 -0
  9. package/dist/chunk-SKY2JLA7.js +59 -0
  10. package/dist/chunk-SKY2JLA7.js.map +1 -0
  11. package/dist/{chunk-SVYROCLD.js → chunk-UMKWXGO3.js} +1 -1
  12. package/dist/chunk-UMKWXGO3.js.map +1 -0
  13. package/dist/google/index.d.ts +29 -2
  14. package/dist/google/index.js +107 -4
  15. package/dist/google/index.js.map +1 -1
  16. package/dist/http/index.d.ts +2 -2
  17. package/dist/http/index.js +2 -1
  18. package/dist/index.d.ts +211 -1303
  19. package/dist/index.js +161 -58
  20. package/dist/index.js.map +1 -1
  21. package/dist/ollama/index.d.ts +26 -2
  22. package/dist/ollama/index.js +101 -4
  23. package/dist/ollama/index.js.map +1 -1
  24. package/dist/openai/index.d.ts +28 -3
  25. package/dist/openai/index.js +111 -4
  26. package/dist/openai/index.js.map +1 -1
  27. package/dist/openrouter/index.d.ts +29 -3
  28. package/dist/openrouter/index.js +118 -4
  29. package/dist/openrouter/index.js.map +1 -1
  30. package/dist/{provider-vTZ74u-w.d.ts → provider-D5MO3-pS.d.ts} +66 -1
  31. package/dist/proxy/index.d.ts +611 -0
  32. package/dist/proxy/index.js +565 -0
  33. package/dist/proxy/index.js.map +1 -0
  34. package/dist/{retry-CMdT0kD8.d.ts → retry-DZ4Sqmxp.d.ts} +1 -1
  35. package/dist/stream-BjyVzBxV.d.ts +1286 -0
  36. package/dist/xai/index.d.ts +1 -1
  37. package/dist/xai/index.js +5 -3
  38. package/dist/xai/index.js.map +1 -1
  39. package/package.json +6 -1
  40. package/dist/chunk-MOU4U3PO.js.map +0 -1
  41. package/dist/chunk-SVYROCLD.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,3 +1,8 @@
1
+ import {
2
+ aggregateUsage,
3
+ createTurn,
4
+ emptyUsage
5
+ } from "./chunk-SKY2JLA7.js";
1
6
  import {
2
7
  createProvider
3
8
  } from "./chunk-MSR5P65T.js";
@@ -10,7 +15,7 @@ import {
10
15
  isAssistantMessage,
11
16
  isToolResultMessage,
12
17
  isUserMessage
13
- } from "./chunk-SVYROCLD.js";
18
+ } from "./chunk-UMKWXGO3.js";
14
19
  import {
15
20
  ExponentialBackoff,
16
21
  LinearBackoff,
@@ -22,62 +27,11 @@ import "./chunk-Z7RBRCRN.js";
22
27
  import {
23
28
  DynamicKey,
24
29
  RoundRobinKeys,
25
- UPPError,
26
30
  WeightedKeys
27
- } from "./chunk-MOU4U3PO.js";
28
-
29
- // src/types/turn.ts
30
- function createTurn(messages, toolExecutions, usage, cycles, data) {
31
- const response = messages.filter((m) => m.type === "assistant").pop();
32
- if (!response) {
33
- throw new Error("Turn must contain at least one assistant message");
34
- }
35
- return {
36
- messages,
37
- response,
38
- toolExecutions,
39
- usage,
40
- cycles,
41
- data
42
- };
43
- }
44
- function emptyUsage() {
45
- return {
46
- inputTokens: 0,
47
- outputTokens: 0,
48
- totalTokens: 0,
49
- cacheReadTokens: 0,
50
- cacheWriteTokens: 0,
51
- cycles: []
52
- };
53
- }
54
- function aggregateUsage(usages) {
55
- const cycles = [];
56
- let inputTokens = 0;
57
- let outputTokens = 0;
58
- let cacheReadTokens = 0;
59
- let cacheWriteTokens = 0;
60
- for (const usage of usages) {
61
- inputTokens += usage.inputTokens;
62
- outputTokens += usage.outputTokens;
63
- cacheReadTokens += usage.cacheReadTokens;
64
- cacheWriteTokens += usage.cacheWriteTokens;
65
- cycles.push({
66
- inputTokens: usage.inputTokens,
67
- outputTokens: usage.outputTokens,
68
- cacheReadTokens: usage.cacheReadTokens,
69
- cacheWriteTokens: usage.cacheWriteTokens
70
- });
71
- }
72
- return {
73
- inputTokens,
74
- outputTokens,
75
- totalTokens: inputTokens + outputTokens,
76
- cacheReadTokens,
77
- cacheWriteTokens,
78
- cycles
79
- };
80
- }
31
+ } from "./chunk-5FEAOEXV.js";
32
+ import {
33
+ UPPError
34
+ } from "./chunk-DZQHVGNV.js";
81
35
 
82
36
  // src/types/stream.ts
83
37
  function createStreamResult(generator, turnPromise, abortController) {
@@ -578,8 +532,153 @@ function validateMediaCapabilities(messages, capabilities, providerName) {
578
532
  }
579
533
  }
580
534
 
535
+ // src/core/embedding.ts
536
+ function embedding(options) {
537
+ const { model: modelRef, config = {}, params } = options;
538
+ const provider = modelRef.provider;
539
+ if (!provider.modalities.embedding) {
540
+ throw new UPPError(
541
+ `Provider '${provider.name}' does not support embedding modality`,
542
+ "INVALID_REQUEST",
543
+ provider.name,
544
+ "embedding"
545
+ );
546
+ }
547
+ const handler = provider.modalities.embedding;
548
+ const boundModel = handler.bind(modelRef.modelId);
549
+ const instance = {
550
+ model: boundModel,
551
+ params,
552
+ embed(input, embedOptions) {
553
+ const inputs = Array.isArray(input) ? input : [input];
554
+ if (embedOptions?.chunked) {
555
+ return createChunkedStream(boundModel, inputs, params, config, embedOptions);
556
+ }
557
+ return executeEmbed(boundModel, inputs, params, config, embedOptions?.signal);
558
+ }
559
+ };
560
+ return instance;
561
+ }
562
+ async function executeEmbed(model, inputs, params, config, signal) {
563
+ const response = await model.embed({
564
+ inputs,
565
+ params,
566
+ config: config ?? {},
567
+ signal
568
+ });
569
+ return normalizeResponse(response);
570
+ }
571
+ function normalizeResponse(response) {
572
+ return {
573
+ embeddings: response.embeddings.map((vec, i) => {
574
+ const vector = normalizeVector(vec.vector);
575
+ return {
576
+ vector,
577
+ dimensions: vector.length,
578
+ index: vec.index ?? i,
579
+ tokens: vec.tokens,
580
+ metadata: vec.metadata
581
+ };
582
+ }),
583
+ usage: response.usage,
584
+ metadata: response.metadata
585
+ };
586
+ }
587
+ function normalizeVector(vector) {
588
+ if (Array.isArray(vector)) {
589
+ return vector;
590
+ }
591
+ return decodeBase64(vector);
592
+ }
593
+ function decodeBase64(b64) {
594
+ const binary = atob(b64);
595
+ const bytes = new Uint8Array(binary.length);
596
+ for (let i = 0; i < binary.length; i++) {
597
+ bytes[i] = binary.charCodeAt(i);
598
+ }
599
+ const floats = new Float32Array(bytes.buffer);
600
+ return Array.from(floats);
601
+ }
602
+ function createChunkedStream(model, inputs, params, config, options) {
603
+ const abortController = new AbortController();
604
+ const batchSize = options.batchSize ?? model.maxBatchSize;
605
+ const concurrency = options.concurrency ?? 1;
606
+ let resolveResult;
607
+ let rejectResult;
608
+ const resultPromise = new Promise((resolve, reject) => {
609
+ resolveResult = resolve;
610
+ rejectResult = reject;
611
+ });
612
+ async function* generate() {
613
+ const total = inputs.length;
614
+ const allEmbeddings = [];
615
+ let totalTokens = 0;
616
+ const batches = [];
617
+ for (let i = 0; i < inputs.length; i += batchSize) {
618
+ batches.push(inputs.slice(i, i + batchSize));
619
+ }
620
+ try {
621
+ for (let i = 0; i < batches.length; i += concurrency) {
622
+ if (abortController.signal.aborted || options.signal?.aborted) {
623
+ throw new UPPError(
624
+ "Embedding cancelled",
625
+ "CANCELLED",
626
+ model.provider.name,
627
+ "embedding"
628
+ );
629
+ }
630
+ const chunk = batches.slice(i, i + concurrency);
631
+ const responses = await Promise.all(
632
+ chunk.map(
633
+ (batch) => model.embed({
634
+ inputs: batch,
635
+ params,
636
+ config: config ?? {},
637
+ signal: abortController.signal
638
+ })
639
+ )
640
+ );
641
+ const batchEmbeddings = [];
642
+ for (const response of responses) {
643
+ for (const vec of response.embeddings) {
644
+ const vector = normalizeVector(vec.vector);
645
+ const emb = {
646
+ vector,
647
+ dimensions: vector.length,
648
+ index: allEmbeddings.length + batchEmbeddings.length,
649
+ tokens: vec.tokens,
650
+ metadata: vec.metadata
651
+ };
652
+ batchEmbeddings.push(emb);
653
+ }
654
+ totalTokens += response.usage.totalTokens;
655
+ }
656
+ allEmbeddings.push(...batchEmbeddings);
657
+ yield {
658
+ embeddings: batchEmbeddings,
659
+ completed: allEmbeddings.length,
660
+ total,
661
+ percent: allEmbeddings.length / total * 100
662
+ };
663
+ }
664
+ resolveResult({
665
+ embeddings: allEmbeddings,
666
+ usage: { totalTokens }
667
+ });
668
+ } catch (error) {
669
+ rejectResult(error);
670
+ throw error;
671
+ }
672
+ }
673
+ const generator = generate();
674
+ return {
675
+ [Symbol.asyncIterator]: () => generator,
676
+ result: resultPromise,
677
+ abort: () => abortController.abort()
678
+ };
679
+ }
680
+
581
681
  // src/core/image.ts
582
- import { readFile } from "fs/promises";
583
682
  var Image = class _Image {
584
683
  /** The underlying image source (bytes, base64, or URL) */
585
684
  source;
@@ -692,6 +791,7 @@ var Image = class _Image {
692
791
  * ```
693
792
  */
694
793
  static async fromPath(path) {
794
+ const { readFile } = await import("fs/promises");
695
795
  const data = await readFile(path);
696
796
  const mimeType = detectMimeType(path);
697
797
  return new _Image(
@@ -1074,7 +1174,9 @@ var Thread = class _Thread {
1074
1174
  // src/index.ts
1075
1175
  var ai = {
1076
1176
  /** LLM instance factory */
1077
- llm
1177
+ llm,
1178
+ /** Embedding instance factory */
1179
+ embedding
1078
1180
  };
1079
1181
  export {
1080
1182
  AssistantMessage,
@@ -1099,6 +1201,7 @@ export {
1099
1201
  createProvider,
1100
1202
  createStreamResult,
1101
1203
  createTurn,
1204
+ embedding,
1102
1205
  emptyUsage,
1103
1206
  isAssistantMessage,
1104
1207
  isAudioBlock,