ai 3.1.34 → 3.1.36

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/dist/index.mjs CHANGED
@@ -239,6 +239,7 @@ var InvalidMessageRoleError = class extends Error {
239
239
  };
240
240
 
241
241
  // core/prompt/convert-to-language-model-prompt.ts
242
+ import { getErrorMessage as getErrorMessage2 } from "@ai-sdk/provider-utils";
242
243
  function convertToLanguageModelPrompt(prompt) {
243
244
  const languageModelMessages = [];
244
245
  if (prompt.system != null) {
@@ -296,6 +297,47 @@ function convertToLanguageModelMessage(message) {
296
297
  mimeType: part.mimeType
297
298
  };
298
299
  }
300
+ if (typeof part.image === "string") {
301
+ try {
302
+ const url = new URL(part.image);
303
+ switch (url.protocol) {
304
+ case "http:":
305
+ case "https:": {
306
+ return {
307
+ type: "image",
308
+ image: url,
309
+ mimeType: part.mimeType
310
+ };
311
+ }
312
+ case "data:": {
313
+ try {
314
+ const [header, base64Content] = part.image.split(",");
315
+ const mimeType = header.split(";")[0].split(":")[1];
316
+ if (mimeType == null || base64Content == null) {
317
+ throw new Error("Invalid data URL format");
318
+ }
319
+ return {
320
+ type: "image",
321
+ image: convertDataContentToUint8Array(base64Content),
322
+ mimeType
323
+ };
324
+ } catch (error) {
325
+ throw new Error(
326
+ `Error processing data URL: ${getErrorMessage2(
327
+ message
328
+ )}`
329
+ );
330
+ }
331
+ }
332
+ default: {
333
+ throw new Error(
334
+ `Unsupported URL protocol: ${url.protocol}`
335
+ );
336
+ }
337
+ }
338
+ } catch (_ignored) {
339
+ }
340
+ }
299
341
  const imageUint8 = convertDataContentToUint8Array(part.image);
300
342
  return {
301
343
  type: "image",
@@ -2025,21 +2067,24 @@ var InvalidModelIdError = class extends Error {
2025
2067
  var NoSuchModelError = class extends Error {
2026
2068
  constructor({
2027
2069
  modelId,
2028
- message = `No such model: ${modelId}`
2070
+ modelType,
2071
+ message = `No such ${modelType}: ${modelId}`
2029
2072
  }) {
2030
2073
  super(message);
2031
2074
  this.name = "AI_NoSuchModelError";
2032
2075
  this.modelId = modelId;
2076
+ this.modelType = modelType;
2033
2077
  }
2034
2078
  static isNoSuchModelError(error) {
2035
- return error instanceof Error && error.name === "AI_NoSuchModelError" && typeof error.modelId === "string";
2079
+ return error instanceof Error && error.name === "AI_NoSuchModelError" && typeof error.modelId === "string" && typeof error.modelType === "string";
2036
2080
  }
2037
2081
  toJSON() {
2038
2082
  return {
2039
2083
  name: this.name,
2040
2084
  message: this.message,
2041
2085
  stack: this.stack,
2042
- modelId: this.modelId
2086
+ modelId: this.modelId,
2087
+ modelType: this.modelType
2043
2088
  };
2044
2089
  }
2045
2090
  };
@@ -2048,75 +2093,78 @@ var NoSuchModelError = class extends Error {
2048
2093
  var NoSuchProviderError = class extends Error {
2049
2094
  constructor({
2050
2095
  providerId,
2051
- message = `No such provider: ${providerId}`
2096
+ availableProviders,
2097
+ message = `No such provider: ${providerId} (available providers: ${availableProviders.join()})`
2052
2098
  }) {
2053
2099
  super(message);
2054
2100
  this.name = "AI_NoSuchProviderError";
2055
2101
  this.providerId = providerId;
2102
+ this.availableProviders = availableProviders;
2056
2103
  }
2057
2104
  static isNoSuchProviderError(error) {
2058
- return error instanceof Error && error.name === "AI_NoSuchProviderError" && typeof error.providerId === "string";
2105
+ return error instanceof Error && error.name === "AI_NoSuchProviderError" && typeof error.providerId === "string" && Array.isArray(error.availableProviders);
2059
2106
  }
2060
2107
  toJSON() {
2061
2108
  return {
2062
2109
  name: this.name,
2063
2110
  message: this.message,
2064
2111
  stack: this.stack,
2065
- providerId: this.providerId
2112
+ providerId: this.providerId,
2113
+ availableProviders: this.availableProviders
2066
2114
  };
2067
2115
  }
2068
2116
  };
2069
2117
 
2070
- // core/registry/model-registry.ts
2071
- function experimental_createModelRegistry(providers) {
2072
- const registry = new DefaultModelRegistry();
2118
+ // core/registry/provider-registry.ts
2119
+ function experimental_createProviderRegistry(providers) {
2120
+ const registry = new DefaultProviderRegistry();
2073
2121
  for (const [id, provider] of Object.entries(providers)) {
2074
- registry.registerLanguageModelProvider({ id, provider });
2122
+ registry.registerProvider({ id, provider });
2075
2123
  }
2076
2124
  return registry;
2077
2125
  }
2078
- var DefaultModelRegistry = class {
2126
+ var experimental_createModelRegistry = experimental_createProviderRegistry;
2127
+ var DefaultProviderRegistry = class {
2079
2128
  constructor() {
2080
- // Mapping of provider id to provider
2081
2129
  this.providers = {};
2082
2130
  }
2083
- /**
2084
- Registers a language model provider with a given id.
2085
-
2086
- @param {string} id - The id of the provider.
2087
- @param {(id: string) => LanguageModel} provider - The provider function to register.
2088
- */
2089
- registerLanguageModelProvider({
2090
- id,
2091
- provider
2092
- }) {
2131
+ registerProvider({ id, provider }) {
2093
2132
  this.providers[id] = provider;
2094
2133
  }
2095
- /**
2096
- Returns the language model with the given id.
2097
- The id can either be a registered model id or use a provider prefix.
2098
- Provider ids are separated from the model id by a colon: `providerId:modelId`.
2099
- The model id is then passed to the provider function to get the model.
2100
-
2101
- @param {string} id - The id of the model to return.
2102
-
2103
- @throws {NoSuchModelError} If no model with the given id exists.
2104
- @throws {NoSuchProviderError} If no provider with the given id exists.
2105
-
2106
- @returns {LanguageModel} The language model associated with the id.
2107
- */
2108
- languageModel(id) {
2134
+ getProvider(id) {
2135
+ const provider = this.providers[id];
2136
+ if (provider == null) {
2137
+ throw new NoSuchProviderError({
2138
+ providerId: id,
2139
+ availableProviders: Object.keys(this.providers)
2140
+ });
2141
+ }
2142
+ return provider;
2143
+ }
2144
+ splitId(id) {
2109
2145
  if (!id.includes(":")) {
2110
2146
  throw new InvalidModelIdError({ id });
2111
2147
  }
2112
- const [providerId, modelId] = id.split(":");
2113
- const provider = this.providers[providerId];
2114
- if (!provider) {
2115
- throw new NoSuchProviderError({ providerId });
2148
+ return id.split(":");
2149
+ }
2150
+ languageModel(id) {
2151
+ var _a, _b;
2152
+ const [providerId, modelId] = this.splitId(id);
2153
+ const model = (_b = (_a = this.getProvider(providerId)).languageModel) == null ? void 0 : _b.call(_a, modelId);
2154
+ if (model == null) {
2155
+ throw new NoSuchModelError({ modelId: id, modelType: "language model" });
2116
2156
  }
2117
- const model = provider(modelId);
2118
- if (!model) {
2119
- throw new NoSuchModelError({ modelId: id });
2157
+ return model;
2158
+ }
2159
+ textEmbeddingModel(id) {
2160
+ var _a, _b;
2161
+ const [providerId, modelId] = this.splitId(id);
2162
+ const model = (_b = (_a = this.getProvider(providerId)).textEmbedding) == null ? void 0 : _b.call(_a, modelId);
2163
+ if (model == null) {
2164
+ throw new NoSuchModelError({
2165
+ modelId: id,
2166
+ modelType: "text embedding model"
2167
+ });
2120
2168
  }
2121
2169
  return model;
2122
2170
  }
@@ -3324,6 +3372,7 @@ export {
3324
3372
  experimental_AssistantResponse,
3325
3373
  experimental_StreamData,
3326
3374
  experimental_createModelRegistry,
3375
+ experimental_createProviderRegistry,
3327
3376
  experimental_generateObject,
3328
3377
  experimental_generateText,
3329
3378
  experimental_streamObject,