@providerprotocol/ai 0.0.18 → 0.0.19
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/README.md +294 -114
- package/dist/anthropic/index.d.ts +1 -1
- package/dist/chunk-WAKD3OO5.js +224 -0
- package/dist/chunk-WAKD3OO5.js.map +1 -0
- package/dist/content-DEl3z_W2.d.ts +276 -0
- package/dist/google/index.d.ts +3 -1
- package/dist/google/index.js +117 -1
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/image-Dhq-Yuq4.d.ts +456 -0
- package/dist/index.d.ts +55 -163
- package/dist/index.js +79 -211
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +1 -1
- package/dist/openai/index.d.ts +47 -20
- package/dist/openai/index.js +304 -1
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +1 -1
- package/dist/{provider-D5MO3-pS.d.ts → provider-BBMBZuGn.d.ts} +11 -11
- package/dist/proxy/index.d.ts +126 -85
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-DZ4Sqmxp.d.ts → retry-DR7YRJDz.d.ts} +1 -1
- package/dist/{stream-BjyVzBxV.d.ts → stream-DRHy6q1a.d.ts} +2 -275
- package/dist/xai/index.d.ts +29 -1
- package/dist/xai/index.js +113 -1
- package/dist/xai/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// src/core/media/Image.ts
|
|
2
|
+
var Image = class _Image {
|
|
3
|
+
/** The underlying image source (bytes, base64, or URL) */
|
|
4
|
+
source;
|
|
5
|
+
/** MIME type of the image (e.g., 'image/jpeg', 'image/png') */
|
|
6
|
+
mimeType;
|
|
7
|
+
/** Image width in pixels, if known */
|
|
8
|
+
width;
|
|
9
|
+
/** Image height in pixels, if known */
|
|
10
|
+
height;
|
|
11
|
+
constructor(source, mimeType, width, height) {
|
|
12
|
+
this.source = source;
|
|
13
|
+
this.mimeType = mimeType;
|
|
14
|
+
this.width = width;
|
|
15
|
+
this.height = height;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Whether this image has data loaded in memory.
|
|
19
|
+
*
|
|
20
|
+
* Returns `false` for URL-sourced images that reference external resources.
|
|
21
|
+
* These must be fetched before their data can be accessed.
|
|
22
|
+
*/
|
|
23
|
+
get hasData() {
|
|
24
|
+
return this.source.type !== "url";
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Converts the image to a base64-encoded string.
|
|
28
|
+
*
|
|
29
|
+
* @returns The image data as a base64 string
|
|
30
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
31
|
+
*/
|
|
32
|
+
toBase64() {
|
|
33
|
+
if (this.source.type === "base64") {
|
|
34
|
+
return this.source.data;
|
|
35
|
+
}
|
|
36
|
+
if (this.source.type === "bytes") {
|
|
37
|
+
return btoa(
|
|
38
|
+
Array.from(this.source.data).map((b) => String.fromCharCode(b)).join("")
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
throw new Error("Cannot convert URL image to base64. Fetch the image first.");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Converts the image to a data URL suitable for embedding in HTML or CSS.
|
|
45
|
+
*
|
|
46
|
+
* @returns A data URL in the format `data:{mimeType};base64,{data}`
|
|
47
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
48
|
+
*/
|
|
49
|
+
toDataUrl() {
|
|
50
|
+
const base64 = this.toBase64();
|
|
51
|
+
return `data:${this.mimeType};base64,${base64}`;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the image data as raw bytes.
|
|
55
|
+
*
|
|
56
|
+
* @returns The image data as a Uint8Array
|
|
57
|
+
* @throws {Error} When the source is a URL (data must be fetched first)
|
|
58
|
+
*/
|
|
59
|
+
toBytes() {
|
|
60
|
+
if (this.source.type === "bytes") {
|
|
61
|
+
return this.source.data;
|
|
62
|
+
}
|
|
63
|
+
if (this.source.type === "base64") {
|
|
64
|
+
const binaryString = atob(this.source.data);
|
|
65
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
66
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
67
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
68
|
+
}
|
|
69
|
+
return bytes;
|
|
70
|
+
}
|
|
71
|
+
throw new Error("Cannot get bytes from URL image. Fetch the image first.");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Gets the URL for URL-sourced images.
|
|
75
|
+
*
|
|
76
|
+
* @returns The image URL
|
|
77
|
+
* @throws {Error} When the source is not a URL
|
|
78
|
+
*/
|
|
79
|
+
toUrl() {
|
|
80
|
+
if (this.source.type === "url") {
|
|
81
|
+
return this.source.url;
|
|
82
|
+
}
|
|
83
|
+
throw new Error("This image does not have a URL source.");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Converts this Image to an ImageBlock for use in UPP messages.
|
|
87
|
+
*
|
|
88
|
+
* @returns An ImageBlock that can be included in message content arrays
|
|
89
|
+
*/
|
|
90
|
+
toBlock() {
|
|
91
|
+
return {
|
|
92
|
+
type: "image",
|
|
93
|
+
source: this.source,
|
|
94
|
+
mimeType: this.mimeType,
|
|
95
|
+
width: this.width,
|
|
96
|
+
height: this.height
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates an Image by reading a file from disk.
|
|
101
|
+
*
|
|
102
|
+
* The file is read into memory as bytes. MIME type is automatically
|
|
103
|
+
* detected from the file extension.
|
|
104
|
+
*
|
|
105
|
+
* @param path - Path to the image file
|
|
106
|
+
* @returns Promise resolving to an Image with the file contents
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const image = await Image.fromPath('./photos/vacation.jpg');
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
static async fromPath(path) {
|
|
114
|
+
const { readFile } = await import("fs/promises");
|
|
115
|
+
const data = await readFile(path);
|
|
116
|
+
const mimeType = detectMimeType(path);
|
|
117
|
+
return new _Image(
|
|
118
|
+
{ type: "bytes", data: new Uint8Array(data) },
|
|
119
|
+
mimeType
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates an Image from a URL reference.
|
|
124
|
+
*
|
|
125
|
+
* The URL is stored as a reference and not fetched. Providers will handle
|
|
126
|
+
* URL-to-data conversion if needed. MIME type is detected from the URL
|
|
127
|
+
* path if not provided.
|
|
128
|
+
*
|
|
129
|
+
* @param url - URL pointing to the image
|
|
130
|
+
* @param mimeType - Optional MIME type override
|
|
131
|
+
* @returns An Image referencing the URL
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const image = Image.fromUrl('https://example.com/logo.png');
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
static fromUrl(url, mimeType) {
|
|
139
|
+
const detected = mimeType || detectMimeTypeFromUrl(url);
|
|
140
|
+
return new _Image({ type: "url", url }, detected);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Creates an Image from raw byte data.
|
|
144
|
+
*
|
|
145
|
+
* @param data - The image data as a Uint8Array
|
|
146
|
+
* @param mimeType - The MIME type of the image
|
|
147
|
+
* @returns An Image containing the byte data
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const image = Image.fromBytes(pngData, 'image/png');
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
static fromBytes(data, mimeType) {
|
|
155
|
+
return new _Image({ type: "bytes", data }, mimeType);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Creates an Image from a base64-encoded string.
|
|
159
|
+
*
|
|
160
|
+
* @param base64 - The base64-encoded image data (without data URL prefix)
|
|
161
|
+
* @param mimeType - The MIME type of the image
|
|
162
|
+
* @returns An Image containing the base64 data
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const image = Image.fromBase64(base64String, 'image/jpeg');
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
static fromBase64(base64, mimeType) {
|
|
170
|
+
return new _Image({ type: "base64", data: base64 }, mimeType);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Creates an Image from an existing ImageBlock.
|
|
174
|
+
*
|
|
175
|
+
* Useful for converting content blocks received from providers back
|
|
176
|
+
* into Image instances for further processing.
|
|
177
|
+
*
|
|
178
|
+
* @param block - An ImageBlock from message content
|
|
179
|
+
* @returns An Image with the block's source and metadata
|
|
180
|
+
*/
|
|
181
|
+
static fromBlock(block) {
|
|
182
|
+
return new _Image(
|
|
183
|
+
block.source,
|
|
184
|
+
block.mimeType,
|
|
185
|
+
block.width,
|
|
186
|
+
block.height
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
function detectMimeType(path) {
|
|
191
|
+
const ext = path.split(".").pop()?.toLowerCase();
|
|
192
|
+
switch (ext) {
|
|
193
|
+
case "jpg":
|
|
194
|
+
case "jpeg":
|
|
195
|
+
return "image/jpeg";
|
|
196
|
+
case "png":
|
|
197
|
+
return "image/png";
|
|
198
|
+
case "gif":
|
|
199
|
+
return "image/gif";
|
|
200
|
+
case "webp":
|
|
201
|
+
return "image/webp";
|
|
202
|
+
case "svg":
|
|
203
|
+
return "image/svg+xml";
|
|
204
|
+
case "bmp":
|
|
205
|
+
return "image/bmp";
|
|
206
|
+
case "ico":
|
|
207
|
+
return "image/x-icon";
|
|
208
|
+
default:
|
|
209
|
+
return "application/octet-stream";
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function detectMimeTypeFromUrl(url) {
|
|
213
|
+
try {
|
|
214
|
+
const pathname = new URL(url).pathname;
|
|
215
|
+
return detectMimeType(pathname);
|
|
216
|
+
} catch {
|
|
217
|
+
return "application/octet-stream";
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export {
|
|
222
|
+
Image
|
|
223
|
+
};
|
|
224
|
+
//# sourceMappingURL=chunk-WAKD3OO5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/media/Image.ts"],"sourcesContent":["/**\n * @fileoverview Image content handling for the Universal Provider Protocol.\n *\n * Provides a unified Image class for working with images across different sources\n * (file paths, URLs, raw bytes, base64). Supports conversion between formats and\n * integration with UPP message content blocks.\n *\n * @module core/media/Image\n */\n\nimport type { ImageSource, ImageBlock } from '../../types/content.ts';\n\n/**\n * Represents an image that can be used in UPP messages.\n *\n * Images can be created from various sources (files, URLs, bytes, base64) and\n * converted to different formats as needed by providers. The class provides\n * a unified interface regardless of the underlying source type.\n *\n * @example\n * ```typescript\n * // Load from file\n * const fileImage = await Image.fromPath('./photo.jpg');\n *\n * // Reference by URL\n * const urlImage = Image.fromUrl('https://example.com/image.png');\n *\n * // From raw bytes\n * const bytesImage = Image.fromBytes(uint8Array, 'image/png');\n *\n * // Use in a message\n * const message = new UserMessage([image.toBlock()]);\n * ```\n */\nexport class Image {\n /** The underlying image source (bytes, base64, or URL) */\n readonly source: ImageSource;\n /** MIME type of the image (e.g., 'image/jpeg', 'image/png') */\n readonly mimeType: string;\n /** Image width in pixels, if known */\n readonly width?: number;\n /** Image height in pixels, if known */\n readonly height?: number;\n\n private constructor(\n source: ImageSource,\n mimeType: string,\n width?: number,\n height?: number\n ) {\n this.source = source;\n this.mimeType = mimeType;\n this.width = width;\n this.height = height;\n }\n\n /**\n * Whether this image has data loaded in memory.\n *\n * Returns `false` for URL-sourced images that reference external resources.\n * These must be fetched before their data can be accessed.\n */\n get hasData(): boolean {\n return this.source.type !== 'url';\n }\n\n /**\n * Converts the image to a base64-encoded string.\n *\n * @returns The image data as a base64 string\n * @throws {Error} When the source is a URL (data must be fetched first)\n */\n toBase64(): string {\n if (this.source.type === 'base64') {\n return this.source.data;\n }\n\n if (this.source.type === 'bytes') {\n return btoa(\n Array.from(this.source.data)\n .map((b) => String.fromCharCode(b))\n .join('')\n );\n }\n\n throw new Error('Cannot convert URL image to base64. Fetch the image first.');\n }\n\n /**\n * Converts the image to a data URL suitable for embedding in HTML or CSS.\n *\n * @returns A data URL in the format `data:{mimeType};base64,{data}`\n * @throws {Error} When the source is a URL (data must be fetched first)\n */\n toDataUrl(): string {\n const base64 = this.toBase64();\n return `data:${this.mimeType};base64,${base64}`;\n }\n\n /**\n * Gets the image data as raw bytes.\n *\n * @returns The image data as a Uint8Array\n * @throws {Error} When the source is a URL (data must be fetched first)\n */\n toBytes(): Uint8Array {\n if (this.source.type === 'bytes') {\n return this.source.data;\n }\n\n if (this.source.type === 'base64') {\n const binaryString = atob(this.source.data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n return bytes;\n }\n\n throw new Error('Cannot get bytes from URL image. Fetch the image first.');\n }\n\n /**\n * Gets the URL for URL-sourced images.\n *\n * @returns The image URL\n * @throws {Error} When the source is not a URL\n */\n toUrl(): string {\n if (this.source.type === 'url') {\n return this.source.url;\n }\n\n throw new Error('This image does not have a URL source.');\n }\n\n /**\n * Converts this Image to an ImageBlock for use in UPP messages.\n *\n * @returns An ImageBlock that can be included in message content arrays\n */\n toBlock(): ImageBlock {\n return {\n type: 'image',\n source: this.source,\n mimeType: this.mimeType,\n width: this.width,\n height: this.height,\n };\n }\n\n /**\n * Creates an Image by reading a file from disk.\n *\n * The file is read into memory as bytes. MIME type is automatically\n * detected from the file extension.\n *\n * @param path - Path to the image file\n * @returns Promise resolving to an Image with the file contents\n *\n * @example\n * ```typescript\n * const image = await Image.fromPath('./photos/vacation.jpg');\n * ```\n */\n static async fromPath(path: string): Promise<Image> {\n // Dynamic import to avoid bundling fs in browser builds\n const { readFile } = await import('node:fs/promises');\n const data = await readFile(path);\n const mimeType = detectMimeType(path);\n\n return new Image(\n { type: 'bytes', data: new Uint8Array(data) },\n mimeType\n );\n }\n\n /**\n * Creates an Image from a URL reference.\n *\n * The URL is stored as a reference and not fetched. Providers will handle\n * URL-to-data conversion if needed. MIME type is detected from the URL\n * path if not provided.\n *\n * @param url - URL pointing to the image\n * @param mimeType - Optional MIME type override\n * @returns An Image referencing the URL\n *\n * @example\n * ```typescript\n * const image = Image.fromUrl('https://example.com/logo.png');\n * ```\n */\n static fromUrl(url: string, mimeType?: string): Image {\n const detected = mimeType || detectMimeTypeFromUrl(url);\n return new Image({ type: 'url', url }, detected);\n }\n\n /**\n * Creates an Image from raw byte data.\n *\n * @param data - The image data as a Uint8Array\n * @param mimeType - The MIME type of the image\n * @returns An Image containing the byte data\n *\n * @example\n * ```typescript\n * const image = Image.fromBytes(pngData, 'image/png');\n * ```\n */\n static fromBytes(data: Uint8Array, mimeType: string): Image {\n return new Image({ type: 'bytes', data }, mimeType);\n }\n\n /**\n * Creates an Image from a base64-encoded string.\n *\n * @param base64 - The base64-encoded image data (without data URL prefix)\n * @param mimeType - The MIME type of the image\n * @returns An Image containing the base64 data\n *\n * @example\n * ```typescript\n * const image = Image.fromBase64(base64String, 'image/jpeg');\n * ```\n */\n static fromBase64(base64: string, mimeType: string): Image {\n return new Image({ type: 'base64', data: base64 }, mimeType);\n }\n\n /**\n * Creates an Image from an existing ImageBlock.\n *\n * Useful for converting content blocks received from providers back\n * into Image instances for further processing.\n *\n * @param block - An ImageBlock from message content\n * @returns An Image with the block's source and metadata\n */\n static fromBlock(block: ImageBlock): Image {\n return new Image(\n block.source,\n block.mimeType,\n block.width,\n block.height\n );\n }\n}\n\n/**\n * Detects the MIME type of an image based on its file extension.\n *\n * Supports common web image formats: JPEG, PNG, GIF, WebP, SVG, BMP, ICO.\n * Returns 'application/octet-stream' for unknown extensions.\n *\n * @param path - File path or filename with extension\n * @returns The detected MIME type string\n */\nfunction detectMimeType(path: string): string {\n const ext = path.split('.').pop()?.toLowerCase();\n\n switch (ext) {\n case 'jpg':\n case 'jpeg':\n return 'image/jpeg';\n case 'png':\n return 'image/png';\n case 'gif':\n return 'image/gif';\n case 'webp':\n return 'image/webp';\n case 'svg':\n return 'image/svg+xml';\n case 'bmp':\n return 'image/bmp';\n case 'ico':\n return 'image/x-icon';\n default:\n return 'application/octet-stream';\n }\n}\n\n/**\n * Detects the MIME type of an image from its URL.\n *\n * Extracts the pathname from the URL and delegates to `detectMimeType`.\n * Returns 'application/octet-stream' if the URL cannot be parsed.\n *\n * @param url - Full URL pointing to an image\n * @returns The detected MIME type string\n */\nfunction detectMimeTypeFromUrl(url: string): string {\n try {\n const pathname = new URL(url).pathname;\n return detectMimeType(pathname);\n } catch {\n return 'application/octet-stream';\n }\n}\n"],"mappings":";AAkCO,IAAM,QAAN,MAAM,OAAM;AAAA;AAAA,EAER;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA,EAED,YACN,QACA,UACA,OACA,QACA;AACA,SAAK,SAAS;AACd,SAAK,WAAW;AAChB,SAAK,QAAQ;AACb,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAI,UAAmB;AACrB,WAAO,KAAK,OAAO,SAAS;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,WAAmB;AACjB,QAAI,KAAK,OAAO,SAAS,UAAU;AACjC,aAAO,KAAK,OAAO;AAAA,IACrB;AAEA,QAAI,KAAK,OAAO,SAAS,SAAS;AAChC,aAAO;AAAA,QACL,MAAM,KAAK,KAAK,OAAO,IAAI,EACxB,IAAI,CAAC,MAAM,OAAO,aAAa,CAAC,CAAC,EACjC,KAAK,EAAE;AAAA,MACZ;AAAA,IACF;AAEA,UAAM,IAAI,MAAM,4DAA4D;AAAA,EAC9E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAoB;AAClB,UAAM,SAAS,KAAK,SAAS;AAC7B,WAAO,QAAQ,KAAK,QAAQ,WAAW,MAA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;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Content block types for multimodal messages.
|
|
3
|
+
*
|
|
4
|
+
* Defines the various content block types that can be included in
|
|
5
|
+
* user and assistant messages, supporting text, images, audio, video,
|
|
6
|
+
* and arbitrary binary data.
|
|
7
|
+
*
|
|
8
|
+
* @module types/content
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Image source variants for ImageBlock.
|
|
12
|
+
*
|
|
13
|
+
* Images can be provided as base64-encoded strings, URLs, or raw bytes.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Base64 encoded image
|
|
18
|
+
* const base64Source: ImageSource = {
|
|
19
|
+
* type: 'base64',
|
|
20
|
+
* data: 'iVBORw0KGgo...'
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* // URL reference
|
|
24
|
+
* const urlSource: ImageSource = {
|
|
25
|
+
* type: 'url',
|
|
26
|
+
* url: 'https://example.com/image.png'
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* // Raw bytes
|
|
30
|
+
* const bytesSource: ImageSource = {
|
|
31
|
+
* type: 'bytes',
|
|
32
|
+
* data: new Uint8Array([...])
|
|
33
|
+
* };
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type ImageSource = {
|
|
37
|
+
type: 'base64';
|
|
38
|
+
data: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'url';
|
|
41
|
+
url: string;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'bytes';
|
|
44
|
+
data: Uint8Array;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Text content block.
|
|
48
|
+
*
|
|
49
|
+
* The most common content block type, containing plain text content.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const textBlock: TextBlock = {
|
|
54
|
+
* type: 'text',
|
|
55
|
+
* text: 'Hello, world!'
|
|
56
|
+
* };
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
interface TextBlock {
|
|
60
|
+
/** Discriminator for text blocks */
|
|
61
|
+
type: 'text';
|
|
62
|
+
/** The text content */
|
|
63
|
+
text: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Image content block.
|
|
67
|
+
*
|
|
68
|
+
* Contains an image with its source data and metadata.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const imageBlock: ImageBlock = {
|
|
73
|
+
* type: 'image',
|
|
74
|
+
* source: { type: 'url', url: 'https://example.com/photo.jpg' },
|
|
75
|
+
* mimeType: 'image/jpeg',
|
|
76
|
+
* width: 1920,
|
|
77
|
+
* height: 1080
|
|
78
|
+
* };
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
interface ImageBlock {
|
|
82
|
+
/** Discriminator for image blocks */
|
|
83
|
+
type: 'image';
|
|
84
|
+
/** The image data source */
|
|
85
|
+
source: ImageSource;
|
|
86
|
+
/** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
|
|
87
|
+
mimeType: string;
|
|
88
|
+
/** Image width in pixels */
|
|
89
|
+
width?: number;
|
|
90
|
+
/** Image height in pixels */
|
|
91
|
+
height?: number;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Audio content block.
|
|
95
|
+
*
|
|
96
|
+
* Contains audio data with its metadata.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const audioBlock: AudioBlock = {
|
|
101
|
+
* type: 'audio',
|
|
102
|
+
* data: audioBytes,
|
|
103
|
+
* mimeType: 'audio/mp3',
|
|
104
|
+
* duration: 120.5
|
|
105
|
+
* };
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
interface AudioBlock {
|
|
109
|
+
/** Discriminator for audio blocks */
|
|
110
|
+
type: 'audio';
|
|
111
|
+
/** Raw audio data */
|
|
112
|
+
data: Uint8Array;
|
|
113
|
+
/** MIME type of the audio (e.g., 'audio/mp3', 'audio/wav') */
|
|
114
|
+
mimeType: string;
|
|
115
|
+
/** Duration in seconds */
|
|
116
|
+
duration?: number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Video content block.
|
|
120
|
+
*
|
|
121
|
+
* Contains video data with its metadata.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const videoBlock: VideoBlock = {
|
|
126
|
+
* type: 'video',
|
|
127
|
+
* data: videoBytes,
|
|
128
|
+
* mimeType: 'video/mp4',
|
|
129
|
+
* duration: 30,
|
|
130
|
+
* width: 1920,
|
|
131
|
+
* height: 1080
|
|
132
|
+
* };
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
interface VideoBlock {
|
|
136
|
+
/** Discriminator for video blocks */
|
|
137
|
+
type: 'video';
|
|
138
|
+
/** Raw video data */
|
|
139
|
+
data: Uint8Array;
|
|
140
|
+
/** MIME type of the video (e.g., 'video/mp4', 'video/webm') */
|
|
141
|
+
mimeType: string;
|
|
142
|
+
/** Duration in seconds */
|
|
143
|
+
duration?: number;
|
|
144
|
+
/** Video width in pixels */
|
|
145
|
+
width?: number;
|
|
146
|
+
/** Video height in pixels */
|
|
147
|
+
height?: number;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Binary content block for arbitrary data.
|
|
151
|
+
*
|
|
152
|
+
* A generic block type for data that doesn't fit other categories.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const binaryBlock: BinaryBlock = {
|
|
157
|
+
* type: 'binary',
|
|
158
|
+
* data: pdfBytes,
|
|
159
|
+
* mimeType: 'application/pdf',
|
|
160
|
+
* metadata: { filename: 'document.pdf', pages: 10 }
|
|
161
|
+
* };
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
interface BinaryBlock {
|
|
165
|
+
/** Discriminator for binary blocks */
|
|
166
|
+
type: 'binary';
|
|
167
|
+
/** Raw binary data */
|
|
168
|
+
data: Uint8Array;
|
|
169
|
+
/** MIME type of the data */
|
|
170
|
+
mimeType: string;
|
|
171
|
+
/** Additional metadata about the binary content */
|
|
172
|
+
metadata?: Record<string, unknown>;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Union of all content block types.
|
|
176
|
+
*
|
|
177
|
+
* Used when a function or property can accept any type of content block.
|
|
178
|
+
*/
|
|
179
|
+
type ContentBlock = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
180
|
+
/**
|
|
181
|
+
* Content types allowed in user messages.
|
|
182
|
+
*
|
|
183
|
+
* Users can send any type of content block including binary data.
|
|
184
|
+
*/
|
|
185
|
+
type UserContent = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
|
|
186
|
+
/**
|
|
187
|
+
* Content types allowed in assistant messages.
|
|
188
|
+
*
|
|
189
|
+
* Assistants can generate text and media but not arbitrary binary data.
|
|
190
|
+
*/
|
|
191
|
+
type AssistantContent = TextBlock | ImageBlock | AudioBlock | VideoBlock;
|
|
192
|
+
/**
|
|
193
|
+
* Creates a text content block from a string.
|
|
194
|
+
*
|
|
195
|
+
* @param content - The text content
|
|
196
|
+
* @returns A TextBlock containing the provided text
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* const block = text('Hello, world!');
|
|
201
|
+
* // { type: 'text', text: 'Hello, world!' }
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare function text(content: string): TextBlock;
|
|
205
|
+
/**
|
|
206
|
+
* Type guard for TextBlock.
|
|
207
|
+
*
|
|
208
|
+
* @param block - The content block to check
|
|
209
|
+
* @returns True if the block is a TextBlock
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* if (isTextBlock(block)) {
|
|
214
|
+
* console.log(block.text);
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
declare function isTextBlock(block: ContentBlock): block is TextBlock;
|
|
219
|
+
/**
|
|
220
|
+
* Type guard for ImageBlock.
|
|
221
|
+
*
|
|
222
|
+
* @param block - The content block to check
|
|
223
|
+
* @returns True if the block is an ImageBlock
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* if (isImageBlock(block)) {
|
|
228
|
+
* console.log(block.mimeType, block.width, block.height);
|
|
229
|
+
* }
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
declare function isImageBlock(block: ContentBlock): block is ImageBlock;
|
|
233
|
+
/**
|
|
234
|
+
* Type guard for AudioBlock.
|
|
235
|
+
*
|
|
236
|
+
* @param block - The content block to check
|
|
237
|
+
* @returns True if the block is an AudioBlock
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* if (isAudioBlock(block)) {
|
|
242
|
+
* console.log(block.mimeType, block.duration);
|
|
243
|
+
* }
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
declare function isAudioBlock(block: ContentBlock): block is AudioBlock;
|
|
247
|
+
/**
|
|
248
|
+
* Type guard for VideoBlock.
|
|
249
|
+
*
|
|
250
|
+
* @param block - The content block to check
|
|
251
|
+
* @returns True if the block is a VideoBlock
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```typescript
|
|
255
|
+
* if (isVideoBlock(block)) {
|
|
256
|
+
* console.log(block.mimeType, block.duration);
|
|
257
|
+
* }
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
declare function isVideoBlock(block: ContentBlock): block is VideoBlock;
|
|
261
|
+
/**
|
|
262
|
+
* Type guard for BinaryBlock.
|
|
263
|
+
*
|
|
264
|
+
* @param block - The content block to check
|
|
265
|
+
* @returns True if the block is a BinaryBlock
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* if (isBinaryBlock(block)) {
|
|
270
|
+
* console.log(block.mimeType, block.metadata);
|
|
271
|
+
* }
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
declare function isBinaryBlock(block: ContentBlock): block is BinaryBlock;
|
|
275
|
+
|
|
276
|
+
export { type AssistantContent as A, type BinaryBlock as B, type ContentBlock as C, type ImageBlock as I, type TextBlock as T, type UserContent as U, type VideoBlock as V, type AudioBlock as a, type ImageSource as b, isImageBlock as c, isAudioBlock as d, isVideoBlock as e, isBinaryBlock as f, isTextBlock as i, text as t };
|
package/dist/google/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as Provider } from '../provider-
|
|
1
|
+
import { d as Provider } from '../provider-BBMBZuGn.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Provider-specific parameters for Google Gemini API requests.
|
|
@@ -864,6 +864,8 @@ interface GoogleEmbedParams {
|
|
|
864
864
|
title?: string;
|
|
865
865
|
/** Output dimensionality */
|
|
866
866
|
outputDimensionality?: number;
|
|
867
|
+
/** Whether to automatically truncate inputs exceeding token limits (default: true) */
|
|
868
|
+
autoTruncate?: boolean;
|
|
867
869
|
}
|
|
868
870
|
|
|
869
871
|
/**
|
package/dist/google/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createProvider
|
|
3
3
|
} from "../chunk-MSR5P65T.js";
|
|
4
|
+
import {
|
|
5
|
+
Image
|
|
6
|
+
} from "../chunk-WAKD3OO5.js";
|
|
4
7
|
import {
|
|
5
8
|
AssistantMessage,
|
|
6
9
|
isAssistantMessage,
|
|
@@ -579,6 +582,9 @@ function createEmbeddingHandler() {
|
|
|
579
582
|
if (request.params?.outputDimensionality !== void 0) {
|
|
580
583
|
embedRequest.outputDimensionality = request.params.outputDimensionality;
|
|
581
584
|
}
|
|
585
|
+
if (request.params?.autoTruncate !== void 0) {
|
|
586
|
+
embedRequest.autoTruncate = request.params.autoTruncate;
|
|
587
|
+
}
|
|
582
588
|
return embedRequest;
|
|
583
589
|
});
|
|
584
590
|
const url = `${baseUrl}/models/${modelId}:batchEmbedContents?key=${apiKey}`;
|
|
@@ -622,6 +628,115 @@ function createEmbeddingHandler() {
|
|
|
622
628
|
};
|
|
623
629
|
}
|
|
624
630
|
|
|
631
|
+
// src/providers/google/image.ts
|
|
632
|
+
var GOOGLE_AI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
633
|
+
function getCapabilities() {
|
|
634
|
+
return {
|
|
635
|
+
generate: true,
|
|
636
|
+
streaming: false,
|
|
637
|
+
edit: false,
|
|
638
|
+
maxImages: 4
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
function createImageHandler() {
|
|
642
|
+
let providerRef = null;
|
|
643
|
+
return {
|
|
644
|
+
_setProvider(provider) {
|
|
645
|
+
providerRef = provider;
|
|
646
|
+
},
|
|
647
|
+
bind(modelId) {
|
|
648
|
+
if (!providerRef) {
|
|
649
|
+
throw new UPPError(
|
|
650
|
+
"Provider reference not set. Handler must be used with createProvider().",
|
|
651
|
+
"INVALID_REQUEST",
|
|
652
|
+
"google",
|
|
653
|
+
"image"
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
const capabilities = getCapabilities();
|
|
657
|
+
const model = {
|
|
658
|
+
modelId,
|
|
659
|
+
capabilities,
|
|
660
|
+
get provider() {
|
|
661
|
+
return providerRef;
|
|
662
|
+
},
|
|
663
|
+
async generate(request) {
|
|
664
|
+
return executeGenerate(modelId, request);
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
return model;
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
async function executeGenerate(modelId, request) {
|
|
672
|
+
const apiKey = await resolveApiKey(
|
|
673
|
+
request.config,
|
|
674
|
+
"GOOGLE_API_KEY",
|
|
675
|
+
"google",
|
|
676
|
+
"image"
|
|
677
|
+
);
|
|
678
|
+
const baseUrl = request.config.baseUrl?.replace(/\/$/, "") ?? GOOGLE_AI_BASE_URL;
|
|
679
|
+
const url = `${baseUrl}/models/${modelId}:predict`;
|
|
680
|
+
const body = {
|
|
681
|
+
instances: [{
|
|
682
|
+
prompt: request.prompt
|
|
683
|
+
}],
|
|
684
|
+
parameters: buildParameters(request.params)
|
|
685
|
+
};
|
|
686
|
+
const headers = {
|
|
687
|
+
"Content-Type": "application/json",
|
|
688
|
+
"x-goog-api-key": apiKey
|
|
689
|
+
};
|
|
690
|
+
if (request.config.headers) {
|
|
691
|
+
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
692
|
+
if (value !== void 0) {
|
|
693
|
+
headers[key] = value;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
const response = await doFetch(url, {
|
|
698
|
+
method: "POST",
|
|
699
|
+
headers,
|
|
700
|
+
body: JSON.stringify(body),
|
|
701
|
+
signal: request.signal
|
|
702
|
+
}, request.config, "google", "image");
|
|
703
|
+
const data = await response.json();
|
|
704
|
+
return transformResponse2(data);
|
|
705
|
+
}
|
|
706
|
+
function buildParameters(params) {
|
|
707
|
+
const parameters = {};
|
|
708
|
+
if (!params) return parameters;
|
|
709
|
+
if (params.sampleCount !== void 0) parameters.sampleCount = params.sampleCount;
|
|
710
|
+
if (params.imageSize !== void 0) parameters.imageSize = params.imageSize;
|
|
711
|
+
if (params.aspectRatio !== void 0) parameters.aspectRatio = params.aspectRatio;
|
|
712
|
+
if (params.personGeneration !== void 0) parameters.personGeneration = params.personGeneration;
|
|
713
|
+
if (params.safetyFilterLevel !== void 0) parameters.safetyFilterLevel = params.safetyFilterLevel;
|
|
714
|
+
if (params.addWatermark !== void 0) parameters.addWatermark = params.addWatermark;
|
|
715
|
+
if (params.negativePrompt !== void 0) parameters.negativePrompt = params.negativePrompt;
|
|
716
|
+
return parameters;
|
|
717
|
+
}
|
|
718
|
+
function transformResponse2(data) {
|
|
719
|
+
if (!data.predictions || data.predictions.length === 0) {
|
|
720
|
+
throw new UPPError(
|
|
721
|
+
"No images in response",
|
|
722
|
+
"PROVIDER_ERROR",
|
|
723
|
+
"google",
|
|
724
|
+
"image"
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
const images = data.predictions.map((prediction) => {
|
|
728
|
+
const mimeType = prediction.mimeType ?? "image/png";
|
|
729
|
+
const image = Image.fromBase64(prediction.bytesBase64Encoded, mimeType);
|
|
730
|
+
return { image };
|
|
731
|
+
});
|
|
732
|
+
return {
|
|
733
|
+
images,
|
|
734
|
+
usage: {
|
|
735
|
+
imagesGenerated: images.length
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
|
|
625
740
|
// src/providers/google/cache.ts
|
|
626
741
|
var CACHE_API_BASE = "https://generativelanguage.googleapis.com/v1beta/cachedContents";
|
|
627
742
|
async function create(options) {
|
|
@@ -757,7 +872,8 @@ var baseProvider = createProvider({
|
|
|
757
872
|
version: "1.0.0",
|
|
758
873
|
modalities: {
|
|
759
874
|
llm: createLLMHandler(),
|
|
760
|
-
embedding: createEmbeddingHandler()
|
|
875
|
+
embedding: createEmbeddingHandler(),
|
|
876
|
+
image: createImageHandler()
|
|
761
877
|
}
|
|
762
878
|
});
|
|
763
879
|
var google = Object.assign(baseProvider, { cache });
|