@providerprotocol/ai 0.0.1

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 (67) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +84 -0
  3. package/dist/anthropic/index.d.ts +41 -0
  4. package/dist/anthropic/index.js +500 -0
  5. package/dist/anthropic/index.js.map +1 -0
  6. package/dist/chunk-CUCRF5W6.js +136 -0
  7. package/dist/chunk-CUCRF5W6.js.map +1 -0
  8. package/dist/chunk-FTFX2VET.js +424 -0
  9. package/dist/chunk-FTFX2VET.js.map +1 -0
  10. package/dist/chunk-QUUX4G7U.js +117 -0
  11. package/dist/chunk-QUUX4G7U.js.map +1 -0
  12. package/dist/chunk-Y6Q7JCNP.js +39 -0
  13. package/dist/chunk-Y6Q7JCNP.js.map +1 -0
  14. package/dist/google/index.d.ts +69 -0
  15. package/dist/google/index.js +517 -0
  16. package/dist/google/index.js.map +1 -0
  17. package/dist/http/index.d.ts +61 -0
  18. package/dist/http/index.js +43 -0
  19. package/dist/http/index.js.map +1 -0
  20. package/dist/index.d.ts +792 -0
  21. package/dist/index.js +898 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/openai/index.d.ts +204 -0
  24. package/dist/openai/index.js +1340 -0
  25. package/dist/openai/index.js.map +1 -0
  26. package/dist/provider-CUJWjgNl.d.ts +192 -0
  27. package/dist/retry-I2661_rv.d.ts +118 -0
  28. package/package.json +88 -0
  29. package/src/anthropic/index.ts +3 -0
  30. package/src/core/image.ts +188 -0
  31. package/src/core/llm.ts +619 -0
  32. package/src/core/provider.ts +92 -0
  33. package/src/google/index.ts +3 -0
  34. package/src/http/errors.ts +112 -0
  35. package/src/http/fetch.ts +210 -0
  36. package/src/http/index.ts +31 -0
  37. package/src/http/keys.ts +136 -0
  38. package/src/http/retry.ts +205 -0
  39. package/src/http/sse.ts +136 -0
  40. package/src/index.ts +32 -0
  41. package/src/openai/index.ts +9 -0
  42. package/src/providers/anthropic/index.ts +17 -0
  43. package/src/providers/anthropic/llm.ts +196 -0
  44. package/src/providers/anthropic/transform.ts +452 -0
  45. package/src/providers/anthropic/types.ts +213 -0
  46. package/src/providers/google/index.ts +17 -0
  47. package/src/providers/google/llm.ts +203 -0
  48. package/src/providers/google/transform.ts +487 -0
  49. package/src/providers/google/types.ts +214 -0
  50. package/src/providers/openai/index.ts +151 -0
  51. package/src/providers/openai/llm.completions.ts +201 -0
  52. package/src/providers/openai/llm.responses.ts +211 -0
  53. package/src/providers/openai/transform.completions.ts +628 -0
  54. package/src/providers/openai/transform.responses.ts +718 -0
  55. package/src/providers/openai/types.ts +711 -0
  56. package/src/types/content.ts +133 -0
  57. package/src/types/errors.ts +85 -0
  58. package/src/types/index.ts +105 -0
  59. package/src/types/llm.ts +211 -0
  60. package/src/types/messages.ts +182 -0
  61. package/src/types/provider.ts +195 -0
  62. package/src/types/schema.ts +58 -0
  63. package/src/types/stream.ts +146 -0
  64. package/src/types/thread.ts +226 -0
  65. package/src/types/tool.ts +88 -0
  66. package/src/types/turn.ts +118 -0
  67. package/src/utils/id.ts +28 -0
@@ -0,0 +1,188 @@
1
+ import type { ImageSource, ImageBlock } from '../types/content.ts';
2
+
3
+ /**
4
+ * Image class for handling images in UPP
5
+ */
6
+ export class Image {
7
+ readonly source: ImageSource;
8
+ readonly mimeType: string;
9
+ readonly width?: number;
10
+ readonly height?: number;
11
+
12
+ private constructor(
13
+ source: ImageSource,
14
+ mimeType: string,
15
+ width?: number,
16
+ height?: number
17
+ ) {
18
+ this.source = source;
19
+ this.mimeType = mimeType;
20
+ this.width = width;
21
+ this.height = height;
22
+ }
23
+
24
+ /**
25
+ * Check if this image has data loaded (false for URL sources)
26
+ */
27
+ get hasData(): boolean {
28
+ return this.source.type !== 'url';
29
+ }
30
+
31
+ /**
32
+ * Convert to base64 string (throws if source is URL)
33
+ */
34
+ toBase64(): string {
35
+ if (this.source.type === 'base64') {
36
+ return this.source.data;
37
+ }
38
+
39
+ if (this.source.type === 'bytes') {
40
+ return btoa(
41
+ Array.from(this.source.data)
42
+ .map((b) => String.fromCharCode(b))
43
+ .join('')
44
+ );
45
+ }
46
+
47
+ throw new Error('Cannot convert URL image to base64. Fetch the image first.');
48
+ }
49
+
50
+ /**
51
+ * Convert to data URL (throws if source is URL)
52
+ */
53
+ toDataUrl(): string {
54
+ const base64 = this.toBase64();
55
+ return `data:${this.mimeType};base64,${base64}`;
56
+ }
57
+
58
+ /**
59
+ * Get raw bytes (throws if source is URL)
60
+ */
61
+ toBytes(): Uint8Array {
62
+ if (this.source.type === 'bytes') {
63
+ return this.source.data;
64
+ }
65
+
66
+ if (this.source.type === 'base64') {
67
+ const binaryString = atob(this.source.data);
68
+ const bytes = new Uint8Array(binaryString.length);
69
+ for (let i = 0; i < binaryString.length; i++) {
70
+ bytes[i] = binaryString.charCodeAt(i);
71
+ }
72
+ return bytes;
73
+ }
74
+
75
+ throw new Error('Cannot get bytes from URL image. Fetch the image first.');
76
+ }
77
+
78
+ /**
79
+ * Get the URL (only for URL sources)
80
+ */
81
+ toUrl(): string {
82
+ if (this.source.type === 'url') {
83
+ return this.source.url;
84
+ }
85
+
86
+ throw new Error('This image does not have a URL source.');
87
+ }
88
+
89
+ /**
90
+ * Convert to ImageBlock for use in messages
91
+ */
92
+ toBlock(): ImageBlock {
93
+ return {
94
+ type: 'image',
95
+ source: this.source,
96
+ mimeType: this.mimeType,
97
+ width: this.width,
98
+ height: this.height,
99
+ };
100
+ }
101
+
102
+ /**
103
+ * Create from file path (reads file into memory)
104
+ */
105
+ static async fromPath(path: string): Promise<Image> {
106
+ const file = Bun.file(path);
107
+ const data = await file.arrayBuffer();
108
+ const mimeType = file.type || detectMimeType(path);
109
+
110
+ return new Image(
111
+ { type: 'bytes', data: new Uint8Array(data) },
112
+ mimeType
113
+ );
114
+ }
115
+
116
+ /**
117
+ * Create from URL reference (does not fetch - providers handle URL conversion)
118
+ */
119
+ static fromUrl(url: string, mimeType?: string): Image {
120
+ const detected = mimeType || detectMimeTypeFromUrl(url);
121
+ return new Image({ type: 'url', url }, detected);
122
+ }
123
+
124
+ /**
125
+ * Create from raw bytes
126
+ */
127
+ static fromBytes(data: Uint8Array, mimeType: string): Image {
128
+ return new Image({ type: 'bytes', data }, mimeType);
129
+ }
130
+
131
+ /**
132
+ * Create from base64 string
133
+ */
134
+ static fromBase64(base64: string, mimeType: string): Image {
135
+ return new Image({ type: 'base64', data: base64 }, mimeType);
136
+ }
137
+
138
+ /**
139
+ * Create from an existing ImageBlock
140
+ */
141
+ static fromBlock(block: ImageBlock): Image {
142
+ return new Image(
143
+ block.source,
144
+ block.mimeType,
145
+ block.width,
146
+ block.height
147
+ );
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Detect MIME type from file extension
153
+ */
154
+ function detectMimeType(path: string): string {
155
+ const ext = path.split('.').pop()?.toLowerCase();
156
+
157
+ switch (ext) {
158
+ case 'jpg':
159
+ case 'jpeg':
160
+ return 'image/jpeg';
161
+ case 'png':
162
+ return 'image/png';
163
+ case 'gif':
164
+ return 'image/gif';
165
+ case 'webp':
166
+ return 'image/webp';
167
+ case 'svg':
168
+ return 'image/svg+xml';
169
+ case 'bmp':
170
+ return 'image/bmp';
171
+ case 'ico':
172
+ return 'image/x-icon';
173
+ default:
174
+ return 'application/octet-stream';
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Detect MIME type from URL
180
+ */
181
+ function detectMimeTypeFromUrl(url: string): string {
182
+ try {
183
+ const pathname = new URL(url).pathname;
184
+ return detectMimeType(pathname);
185
+ } catch {
186
+ return 'application/octet-stream';
187
+ }
188
+ }