@opencode/ai 0.0.0-dev-21713 → 0.0.0-dev-21730

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 (31) hide show
  1. package/README.md +29 -2
  2. package/dist/experimental/cloudflare-run-evaluation.d.ts +15 -0
  3. package/dist/experimental/cloudflare-run-evaluation.js +43 -0
  4. package/dist/experimental/system-one.d.ts +100 -2
  5. package/dist/experimental/system-one.js +62 -57
  6. package/dist/protocols/alibaba-images.d.ts +19 -0
  7. package/dist/protocols/alibaba-images.js +76 -0
  8. package/dist/protocols/cloudflare-workers-ai-images.d.ts +17 -0
  9. package/dist/protocols/cloudflare-workers-ai-images.js +72 -0
  10. package/dist/protocols/openai-images.d.ts +15 -5
  11. package/dist/protocols/openai-images.js +23 -23
  12. package/dist/provider-error.js +3 -1
  13. package/dist/providers/alibaba.d.ts +2 -0
  14. package/dist/providers/alibaba.js +5 -1
  15. package/dist/providers/baseten.d.ts +13 -0
  16. package/dist/providers/baseten.js +15 -2
  17. package/dist/providers/cloudflare-ai-gateway.d.ts +5 -0
  18. package/dist/providers/cloudflare-ai-gateway.js +24 -1
  19. package/dist/providers/cloudflare-workers-ai.d.ts +7 -0
  20. package/dist/providers/cloudflare-workers-ai.js +31 -5
  21. package/dist/providers/digitalocean.d.ts +9 -0
  22. package/dist/providers/digitalocean.js +16 -2
  23. package/dist/providers/opencode.js +1 -0
  24. package/dist/providers/openrouter.js +1 -0
  25. package/dist/providers/typesafe-ai.js +1 -0
  26. package/dist/providers/vercel-ai-gateway.d.ts +3 -0
  27. package/dist/providers/vercel-ai-gateway.js +6 -1
  28. package/dist/route/media-protocol.d.ts +1 -0
  29. package/dist/route/media-protocol.js +12 -8
  30. package/dist/utils/media-type.js +18 -1
  31. package/package.json +3 -3
@@ -4,7 +4,7 @@ import { Media } from "../media.js";
4
4
  import { Framing } from "../route/framing.js";
5
5
  import { MediaProtocol } from "../route/media-protocol.js";
6
6
  import { MediaRoute } from "../route/media.js";
7
- import { mergeJsonRecords } from "../schema/index.js";
7
+ import { mergeJsonRecords, ProviderID } from "../schema/index.js";
8
8
  import { ProviderShared } from "./shared.js";
9
9
  import { MediaInput } from "./utils/media-input.js";
10
10
  const route = MediaProtocol.identity({ id: "openai-images", name: "OpenAI Images", provider: "openai" });
@@ -74,7 +74,7 @@ const streamOptions = (request) => {
74
74
  };
75
75
  const isEdit = (request) => (request.images?.length ?? 0) > 0;
76
76
  const isInline = (asset) => asset.source.type === "bytes" || asset.source.type === "base64";
77
- const reference = (asset) => ProviderShared.mediaReference(asset, route.provider, route.name).pipe(Effect.map((item) => (item.type === "ref" ? { file_id: item.value } : { image_url: item.value })));
77
+ const reference = (asset, provider) => ProviderShared.mediaReference(asset, provider, route.name).pipe(Effect.map((item) => (item.type === "ref" ? { file_id: item.value } : { image_url: item.value })));
78
78
  const fromRequest = Effect.fn("OpenAIImages.fromRequest")(function* (request) {
79
79
  const images = request.images ?? [];
80
80
  const mask = request.mask;
@@ -91,8 +91,8 @@ const fromRequest = Effect.fn("OpenAIImages.fromRequest")(function* (request) {
91
91
  form.append("mask", MediaInput.blob(yield* MediaInput.inlineBytes(route.id, mask), mask.mediaType), "mask");
92
92
  return MediaProtocol.multipart(form);
93
93
  }
94
- const references = yield* Effect.forEach(images, reference);
95
- const maskReference = mask === undefined ? undefined : yield* reference(mask);
94
+ const references = yield* Effect.forEach(images, (image) => reference(image, request.model.provider));
95
+ const maskReference = mask === undefined ? undefined : yield* reference(mask, request.model.provider);
96
96
  return MediaProtocol.json(mergeJsonRecords({
97
97
  model: request.model.id,
98
98
  prompt: request.prompt,
@@ -103,20 +103,14 @@ const fromRequest = Effect.fn("OpenAIImages.fromRequest")(function* (request) {
103
103
  // ---------------------------------------------------------------------------
104
104
  // 6. Stream parsing
105
105
  // ---------------------------------------------------------------------------
106
- const requestedFormat = (body) => {
107
- if (body.type === "binary")
108
- return undefined;
109
- const value = body.type === "json" ? body.value.output_format : body.value.get("output_format");
110
- return typeof value === "string" ? value : undefined;
111
- };
112
- const usage = (value) => value === undefined
106
+ const usage = (value, provider) => value === undefined
113
107
  ? undefined
114
108
  : {
115
109
  type: "tokens",
116
110
  input: value.input_tokens,
117
111
  output: value.output_tokens,
118
112
  total: value.total_tokens,
119
- details: { openai: value },
113
+ details: { [provider]: value },
120
114
  };
121
115
  /** `size` echoes the rendered `WIDTHxHEIGHT`; `auto` or any other value leaves the dimensions unknown. */
122
116
  const info = (format, size) => {
@@ -141,7 +135,7 @@ const onEvent = Effect.fnUntraced(function* (state, frame) {
141
135
  size: event.size,
142
136
  quality: event.quality,
143
137
  background: event.background,
144
- usage: usage(event.usage),
138
+ usage: event.usage,
145
139
  },
146
140
  [ImageOutputEvent.make({ index: state.completed, image })],
147
141
  ];
@@ -149,8 +143,10 @@ const onEvent = Effect.fnUntraced(function* (state, frame) {
149
143
  const onDocument = Effect.fnUntraced(function* (frame) {
150
144
  const invalid = (message, cause) => route.frameError(message, frame.document, cause);
151
145
  const decoded = yield* decodeDocument(frame.document).pipe(Effect.mapError((cause) => invalid(`${route.name} returned an invalid response`, cause)));
152
- const format = decoded.output_format ?? frame.requested ?? "png";
153
- const images = yield* Effect.forEach(decoded.data, (item, index) => MediaInput.decodedAsset(invalid, `${route.name} result ${index}`, item.b64_json, `image/${format}`, {
146
+ // Compatible hosts may omit `output_format` and ignore the requested one, so the bytes decide unreported types.
147
+ const format = decoded.output_format;
148
+ const mediaType = format === undefined ? undefined : `image/${format}`;
149
+ const images = yield* Effect.forEach(decoded.data, (item, index) => MediaInput.decodedAsset(invalid, `${route.name} result ${index}`, item.b64_json, mediaType, {
154
150
  info: info(format, decoded.size),
155
151
  }));
156
152
  if (images.length === 0)
@@ -161,19 +157,20 @@ const onDocument = Effect.fnUntraced(function* (frame) {
161
157
  size: decoded.size,
162
158
  quality: decoded.quality,
163
159
  background: decoded.background,
164
- usage: usage(decoded.usage),
160
+ usage: decoded.usage,
165
161
  };
166
162
  return [state, images.map((image, index) => ImageOutputEvent.make({ index, image }))];
167
163
  });
168
164
  const step = (state, frame) => (typeof frame === "string" ? onEvent(state, frame) : onDocument(frame));
169
- const finish = (state) => {
165
+ const finish = (state, context) => {
170
166
  if (state.completed === 0)
171
167
  return Effect.fail(route.incomplete());
168
+ const provider = context.request.model.provider;
172
169
  return Effect.succeed([
173
170
  ImageFinishEvent.make({
174
- usage: state.usage,
171
+ usage: usage(state.usage, provider),
175
172
  providerMetadata: {
176
- openai: {
173
+ [provider]: {
177
174
  outputFormat: state.format,
178
175
  size: state.size,
179
176
  quality: state.quality,
@@ -191,14 +188,17 @@ export const protocol = MediaProtocol.stream(route, {
191
188
  body: { from: fromRequest },
192
189
  frames: (bytes, context) => context.request.mode === "stream"
193
190
  ? Framing.sse.frame(bytes)
194
- : Framing.document
195
- .frame(bytes)
196
- .pipe(Stream.map((document) => ({ document, requested: requestedFormat(context.body) }))),
191
+ : Framing.document.frame(bytes).pipe(Stream.map((document) => ({ document }))),
197
192
  initial: () => ({ completed: 0 }),
198
193
  step,
199
194
  finish,
200
195
  });
201
- export const model = (input) => ImageModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: ({ request }) => (isEdit(request) ? EDIT_PATH : PATH) }, input);
196
+ /** `provider` attributes models, refs, and metadata to an OpenAI-compatible host; it defaults to `openai`. */
197
+ export const model = (input) => ImageModel.fromRoute({
198
+ protocol: input.provider === undefined ? protocol : { ...protocol, provider: ProviderID.make(input.provider) },
199
+ baseURL: DEFAULT_BASE_URL,
200
+ path: ({ request }) => (isEdit(request) ? EDIT_PATH : PATH),
201
+ }, input);
202
202
  export const OpenAIImages = {
203
203
  protocol,
204
204
  model,
@@ -131,7 +131,8 @@ const INVALID_REQUEST_CODES = new Set([
131
131
  ]);
132
132
  // Azure OpenAI reports `content_filter` with `innererror.code` ResponsibleAIPolicyViolation.
133
133
  // OpenRouter tags provider failures with a typed `error_type`; its Responses skin also
134
- // emits `image_content_policy_violation` as the native code.
134
+ // emits `image_content_policy_violation` as the native code. Alibaba Model Studio reports
135
+ // moderation rejections as `DataInspectionFailed`.
135
136
  const CONTENT_POLICY_CODES = new Set([
136
137
  "content_filter",
137
138
  "content_moderation",
@@ -139,6 +140,7 @@ const CONTENT_POLICY_CODES = new Set([
139
140
  "content_policy_violation",
140
141
  "image_content_policy_violation",
141
142
  "refusal",
143
+ "datainspectionfailed",
142
144
  ]);
143
145
  // OpenCode Zen replaces upstream codes outside its allow-list but keeps the original
144
146
  // as a `[code]` label at the start of the rewritten message.
@@ -10,6 +10,7 @@ export type Region = "ap-southeast-1" | "cn-beijing" | "cn-hongkong" | "us-east-
10
10
  export type ChatOptionsInput = AlibabaChat.OptionsInput;
11
11
  export type MessagesOptionsInput = AlibabaMessages.OptionsInput;
12
12
  export type ResponsesOptionsInput = AlibabaResponses.OptionsInput;
13
+ export type { AlibabaImageOptions } from "../protocols/alibaba-images.js";
13
14
  type Location = AtLeastOne<{
14
15
  readonly region: Region;
15
16
  /** Overrides the selected API's complete base URL, including its version prefix. */
@@ -667,6 +668,7 @@ export declare const configure: (input: Config) => {
667
668
  readonly previousResponseId?: string | undefined;
668
669
  readonly conversation?: string | undefined;
669
670
  }, import("../route/client.js").CompactionOperations | undefined>;
671
+ image: (id: string | ModelID) => import("../image.js").ImageModel<import("./alibaba.js").AlibabaImageOptions>;
670
672
  configure: typeof configure;
671
673
  };
672
674
  export declare const provider: {
@@ -1,11 +1,13 @@
1
1
  import { Struct } from "effect";
2
2
  import { AlibabaChat } from "../protocols/alibaba-chat.js";
3
+ import { AlibabaImages } from "../protocols/alibaba-images.js";
3
4
  import { AlibabaMessages } from "../protocols/alibaba-messages.js";
4
5
  import { AlibabaResponses } from "../protocols/alibaba-responses.js";
5
6
  import { AuthOptions } from "../route/auth-options.js";
6
7
  import { Route } from "../route/client.js";
7
8
  import { Endpoint } from "../route/endpoint.js";
8
9
  import { Framing } from "../route/framing.js";
10
+ import { MediaRoute } from "../route/media.js";
9
11
  import { ProviderConfigurationError, ProviderID, ToolDefinition } from "../schema/index.js";
10
12
  export const id = ProviderID.make("alibaba");
11
13
  const hosts = new Map([
@@ -66,7 +68,9 @@ export const configure = (input) => {
66
68
  })
67
69
  .model({ id, compatibility: { requireSignature: false } });
68
70
  const responses = (id) => responsesRoute.with(common).model({ id });
69
- return { id, model: chat, chat, messages, responses, configure };
71
+ const media = MediaRoute.deployment({ ...input, baseURL: baseURL ?? `https://${host}/api/v1` }, opts.auth);
72
+ const image = (id) => AlibabaImages.model({ ...media, id });
73
+ return { id, model: chat, chat, messages, responses, image, configure };
70
74
  };
71
75
  export const provider = { id, configure };
72
76
  export const model = (id, input) => fromSettings(input).chat(id);
@@ -8,6 +8,10 @@ export type LanguageModelOptions = Omit<RouteDefaultsInput, "providerOptions"> &
8
8
  readonly baseURL?: string;
9
9
  readonly providerOptions?: OpenAIProviderOptionsInput;
10
10
  };
11
+ export interface BasetenEvaluationOptions {
12
+ readonly [key: string]: unknown;
13
+ readonly max_len?: number;
14
+ }
11
15
  export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & {
12
16
  readonly apiKey?: string;
13
17
  readonly baseURL?: string;
@@ -259,12 +263,21 @@ export declare const routes: Route<{
259
263
  export declare const configure: (input?: LanguageModelOptions) => {
260
264
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
261
265
  model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
266
+ experimental: {
267
+ evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<BasetenEvaluationOptions>;
268
+ };
262
269
  configure: typeof configure;
263
270
  };
264
271
  export declare const provider: {
265
272
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
266
273
  model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
274
+ experimental: {
275
+ evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<BasetenEvaluationOptions>;
276
+ };
267
277
  configure: typeof configure;
268
278
  };
279
+ export declare const experimental: {
280
+ evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<BasetenEvaluationOptions>;
281
+ };
269
282
  export declare const model: ProviderPackage.Definition<Settings, OpenAIProviderOptionsInput>["model"];
270
283
  export * as Baseten from "./baseten.js";
@@ -2,7 +2,8 @@ import { OpenAIChat } from "../protocols/openai-chat.js";
2
2
  import { AuthOptions } from "../route/auth-options.js";
3
3
  import { Route } from "../route/client.js";
4
4
  import { Endpoint } from "../route/endpoint.js";
5
- import { ProviderID } from "../schema/index.js";
5
+ import { HttpOptions, ProviderID } from "../schema/index.js";
6
+ import { SystemOne } from "../experimental/system-one.js";
6
7
  export const id = ProviderID.make("baseten");
7
8
  const baseURL = "https://inference.baseten.co/v1";
8
9
  export const route = Route.make({
@@ -16,18 +17,30 @@ export const route = Route.make({
16
17
  export const routes = [route];
17
18
  export const configure = (input = {}) => {
18
19
  const { apiKey: _apiKey, auth: _auth, baseURL: endpoint, ...defaults } = input;
20
+ const auth = AuthOptions.bearer(input, "BASETEN_API_KEY");
19
21
  const configured = route.with({
20
22
  ...defaults,
21
23
  endpoint: { baseURL: endpoint ?? baseURL },
22
- auth: AuthOptions.bearer(input, "BASETEN_API_KEY"),
24
+ auth,
25
+ });
26
+ const evaluation = (modelID) => SystemOne.model({
27
+ id: modelID,
28
+ provider: id,
29
+ providerMetadataKey: "baseten",
30
+ auth,
31
+ baseURL: endpoint ?? baseURL,
32
+ headers: input.headers,
33
+ http: HttpOptions.make(input.http),
23
34
  });
24
35
  return {
25
36
  id,
26
37
  model: (modelID) => configured.model({ id: modelID }),
38
+ experimental: { evaluation },
27
39
  configure,
28
40
  };
29
41
  };
30
42
  export const provider = configure();
43
+ export const experimental = provider.experimental;
31
44
  export const model = (modelID, { apiKey, baseURL, body, headers, ...providerOptions }) => configure({
32
45
  apiKey,
33
46
  baseURL,
@@ -27,6 +27,8 @@ export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & G
27
27
  readonly gatewayApiKey?: string;
28
28
  };
29
29
  export declare const baseURL: (input: GatewayURL) => string;
30
+ /** The Run API (`/ai/run`) next to the OpenAI-compatible `/ai/v1` base. */
31
+ export declare const runBaseURL: (input: GatewayURL) => string;
30
32
  export declare const responsesRoute: import("../route/client.js").Route<{
31
33
  readonly model: string;
32
34
  readonly input: readonly ({
@@ -1235,6 +1237,9 @@ export declare const configure: (input: LanguageModelOptions) => {
1235
1237
  endpoint: import("../route/client.js").CompactOperation;
1236
1238
  trigger: import("../route/client.js").TriggerCompactOperation;
1237
1239
  }>;
1240
+ experimental: {
1241
+ evaluation: (value: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental.js").EvaluationOptions>;
1242
+ };
1238
1243
  configure: typeof configure;
1239
1244
  };
1240
1245
  export declare const provider: {
@@ -1,6 +1,9 @@
1
+ import { CloudflareRunEvaluation } from "../experimental/cloudflare-run-evaluation.js";
2
+ import { SystemOne } from "../experimental/system-one.js";
1
3
  import { AnthropicMessages, OpenAIChat, OpenAIResponses } from "../protocols/index.js";
2
4
  import { Auth } from "../route/auth.js";
3
- import { ProviderConfigurationError, ProviderID } from "../schema/index.js";
5
+ import { Endpoint } from "../route/endpoint.js";
6
+ import { HttpOptions, ProviderConfigurationError, ProviderID } from "../schema/index.js";
4
7
  import { encodeJson } from "../utils/json.js";
5
8
  export const id = ProviderID.make("cloudflare-ai-gateway");
6
9
  export const authEnvVars = ["CLOUDFLARE_API_TOKEN", "CF_AIG_TOKEN"];
@@ -9,6 +12,16 @@ export const baseURL = (input) => {
9
12
  return input.baseURL;
10
13
  return `https://api.cloudflare.com/client/v4/accounts/${encodeURIComponent(requireAccountId(input))}/ai/v1`;
11
14
  };
15
+ /** The Run API (`/ai/run`) next to the OpenAI-compatible `/ai/v1` base. */
16
+ export const runBaseURL = (input) => {
17
+ const url = Endpoint.trimBaseUrl(baseURL(input));
18
+ if (!url.endsWith("/ai/v1"))
19
+ throw new ProviderConfigurationError({
20
+ provider: id,
21
+ message: "CloudflareAIGateway needs accountId or a baseURL ending in /ai/v1 to derive the Run API base",
22
+ });
23
+ return `${url.slice(0, -"/v1".length)}/run`;
24
+ };
12
25
  // Cloudflare's REST API rejects parts of the Anthropic Messages and OpenAI Responses request shapes, such as
13
26
  // system prompt blocks and `minimal` reasoning effort, so these models use the provider-native gateway endpoints.
14
27
  const passthroughURL = (input, provider) => `https://gateway.ai.cloudflare.com/v1/${encodeURIComponent(requireAccountId(input))}/${encodeURIComponent(gatewayId(input))}/${provider}`;
@@ -70,6 +83,15 @@ export const configure = (input) => {
70
83
  const responses = responsesRoute.with(passthrough("openai"));
71
84
  const messages = messagesRoute.with(passthrough("anthropic/v1"));
72
85
  const chat = route.with(rest);
86
+ const evaluation = (value) => CloudflareRunEvaluation.model({
87
+ id: value,
88
+ provider: id,
89
+ auth: rest.auth,
90
+ baseURL: runBaseURL(input),
91
+ headers: rest.headers,
92
+ http: HttpOptions.make(input.http),
93
+ rounding: SystemOne.jevRounding,
94
+ });
73
95
  return {
74
96
  id,
75
97
  model: (value) => {
@@ -83,6 +105,7 @@ export const configure = (input) => {
83
105
  return chat.model({ id: value.slice("workers-ai/".length) });
84
106
  return chat.model({ id: value });
85
107
  },
108
+ experimental: { evaluation },
86
109
  configure,
87
110
  };
88
111
  };
@@ -15,7 +15,10 @@ export type LanguageModelOptions = WorkersAIURL & Omit<RouteDefaultsInput, "prov
15
15
  export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & WorkersAIURL & {
16
16
  readonly apiKey?: string;
17
17
  };
18
+ export type { CloudflareWorkersAIImageOptions } from "../protocols/cloudflare-workers-ai-images.js";
18
19
  export declare const baseURL: (input: WorkersAIURL) => string;
20
+ /** The Run API (`/ai/run`) next to the OpenAI-compatible `/ai/v1` base. */
21
+ export declare const runBaseURL: (input: WorkersAIURL) => string;
19
22
  export declare const route: Route<{
20
23
  readonly model: string;
21
24
  readonly messages: readonly ({
@@ -263,6 +266,10 @@ export declare const routes: Route<{
263
266
  export declare const configure: (input: LanguageModelOptions) => {
264
267
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
265
268
  model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
269
+ experimental: {
270
+ evaluation: (modelID: string | ModelID) => import("../experimental/evaluation.js").EvaluationModel<import("../experimental/evaluation.js").EvaluationOptions>;
271
+ };
272
+ image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("./cloudflare-workers-ai.js").CloudflareWorkersAIImageOptions>;
266
273
  configure: typeof configure;
267
274
  };
268
275
  export declare const provider: {
@@ -1,8 +1,12 @@
1
+ import { CloudflareRunEvaluation } from "../experimental/cloudflare-run-evaluation.js";
2
+ import { EvaluationRounding } from "../experimental/evaluation.js";
3
+ import { CloudflareWorkersAIImages } from "../protocols/cloudflare-workers-ai-images.js";
1
4
  import { OpenAIChat } from "../protocols/openai-chat.js";
2
5
  import { AuthOptions } from "../route/auth-options.js";
3
6
  import { Route } from "../route/client.js";
4
7
  import { Endpoint } from "../route/endpoint.js";
5
- import { ProviderConfigurationError, ProviderID } from "../schema/index.js";
8
+ import { MediaRoute } from "../route/media.js";
9
+ import { HttpOptions, ProviderConfigurationError, ProviderID } from "../schema/index.js";
6
10
  export const id = ProviderID.make("cloudflare-workers-ai");
7
11
  export const authEnvVars = ["CLOUDFLARE_API_KEY", "CLOUDFLARE_WORKERS_AI_TOKEN", "CLOUDFLARE_API_TOKEN"];
8
12
  export const baseURL = (input) => {
@@ -15,6 +19,16 @@ export const baseURL = (input) => {
15
19
  });
16
20
  return `https://api.cloudflare.com/client/v4/accounts/${encodeURIComponent(input.accountId)}/ai/v1`;
17
21
  };
22
+ /** The Run API (`/ai/run`) next to the OpenAI-compatible `/ai/v1` base. */
23
+ export const runBaseURL = (input) => {
24
+ const url = Endpoint.trimBaseUrl(baseURL(input));
25
+ if (!url.endsWith("/ai/v1"))
26
+ throw new ProviderConfigurationError({
27
+ provider: id,
28
+ message: "CloudflareWorkersAI needs accountId or a baseURL ending in /ai/v1 to derive the Run API base",
29
+ });
30
+ return `${url.slice(0, -"/v1".length)}/run`;
31
+ };
18
32
  export const route = Route.make({
19
33
  id: "cloudflare-workers-ai",
20
34
  provider: id,
@@ -24,16 +38,28 @@ export const route = Route.make({
24
38
  framing: OpenAIChat.framing,
25
39
  });
26
40
  export const routes = [route];
41
+ const clefRounding = new EvaluationRounding({ probabilityDecimals: 4, scoreDecimals: 4 });
27
42
  export const configure = (input) => {
28
43
  const { accountId: _accountId, apiKey: _apiKey, auth: _auth, baseURL: _baseURL, ...defaults } = input;
29
- const configured = route.with({
30
- ...defaults,
31
- endpoint: { baseURL: baseURL(input) },
32
- auth: AuthOptions.bearer(input, authEnvVars),
44
+ const auth = AuthOptions.bearer(input, authEnvVars);
45
+ const configured = route.with({ ...defaults, endpoint: { baseURL: baseURL(input) }, auth });
46
+ const evaluation = (modelID) => CloudflareRunEvaluation.model({
47
+ id: modelID,
48
+ provider: id,
49
+ auth,
50
+ baseURL: runBaseURL(input),
51
+ headers: input.headers,
52
+ http: HttpOptions.make(input.http),
53
+ rounding: clefRounding,
33
54
  });
34
55
  return {
35
56
  id,
36
57
  model: (modelID) => configured.model({ id: modelID }),
58
+ experimental: { evaluation },
59
+ image: (modelID) => CloudflareWorkersAIImages.model({
60
+ ...MediaRoute.deployment({ ...input, baseURL: runBaseURL(input) }, auth),
61
+ id: modelID,
62
+ }),
37
63
  configure,
38
64
  };
39
65
  };
@@ -454,12 +454,21 @@ export declare const routes: Route<{
454
454
  export declare const configure: (input?: LanguageModelOptions) => {
455
455
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
456
456
  model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
457
+ experimental: {
458
+ evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental.js").EvaluationOptions>;
459
+ };
457
460
  configure: typeof configure;
458
461
  };
459
462
  export declare const provider: {
460
463
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
461
464
  model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
465
+ experimental: {
466
+ evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental.js").EvaluationOptions>;
467
+ };
462
468
  configure: typeof configure;
463
469
  };
470
+ export declare const experimental: {
471
+ evaluation: (modelID: string | ModelID) => import("../experimental.js").EvaluationModel<import("../experimental.js").EvaluationOptions>;
472
+ };
464
473
  export declare const model: ProviderPackage.Definition<Settings, OpenAIProviderOptionsInput>["model"];
465
474
  export * as DigitalOcean from "./digitalocean.js";
@@ -4,7 +4,8 @@ import { AuthOptions } from "../route/auth-options.js";
4
4
  import { Route } from "../route/client.js";
5
5
  import { Endpoint } from "../route/endpoint.js";
6
6
  import { Protocol } from "../route/protocol.js";
7
- import { ProviderID } from "../schema/index.js";
7
+ import { HttpOptions, ProviderID } from "../schema/index.js";
8
+ import { SystemOne } from "../experimental/system-one.js";
8
9
  export const id = ProviderID.make("digitalocean");
9
10
  const baseURL = "https://inference.do-ai.run/v1";
10
11
  export const protocol = Protocol.make({
@@ -26,10 +27,21 @@ export const route = Route.make({
26
27
  export const routes = [route];
27
28
  export const configure = (input = {}) => {
28
29
  const { apiKey: _apiKey, auth: _auth, baseURL: endpoint, ...defaults } = input;
30
+ const auth = AuthOptions.bearer(input, ["DIGITALOCEAN_ACCESS_TOKEN", "DIGITALOCEAN_API_KEY", "DO_INFERENCE_API_KEY"]);
29
31
  const configured = route.with({
30
32
  ...defaults,
31
33
  endpoint: { baseURL: endpoint ?? baseURL },
32
- auth: AuthOptions.bearer(input, ["DIGITALOCEAN_ACCESS_TOKEN", "DIGITALOCEAN_API_KEY", "DO_INFERENCE_API_KEY"]),
34
+ auth,
35
+ });
36
+ const evaluation = (modelID) => SystemOne.model({
37
+ id: modelID,
38
+ provider: id,
39
+ providerMetadataKey: "digitalocean",
40
+ auth,
41
+ baseURL: endpoint ?? baseURL,
42
+ headers: input.headers,
43
+ http: HttpOptions.make(input.http),
44
+ rounding: SystemOne.jevRounding,
33
45
  });
34
46
  return {
35
47
  id,
@@ -39,10 +51,12 @@ export const configure = (input = {}) => {
39
51
  supportsPromptCacheKey: true,
40
52
  },
41
53
  }),
54
+ experimental: { evaluation },
42
55
  configure,
43
56
  };
44
57
  };
45
58
  export const provider = configure();
59
+ export const experimental = provider.experimental;
46
60
  export const model = (modelID, { apiKey, baseURL, body, headers, ...providerOptions }) => configure({
47
61
  apiKey,
48
62
  baseURL,
@@ -12,6 +12,7 @@ export const configure = (input = {}) => {
12
12
  baseURL: input.baseURL ?? baseURL,
13
13
  headers: input.headers,
14
14
  http: HttpOptions.make(input.http),
15
+ rounding: SystemOne.jevRounding,
15
16
  });
16
17
  return { id, experimental: { evaluation }, configure };
17
18
  };
@@ -107,6 +107,7 @@ export const configure = (input = {}) => {
107
107
  baseURL: input.baseURL ?? baseURL,
108
108
  headers: input.headers,
109
109
  http: HttpOptions.make(input.http),
110
+ rounding: SystemOne.jevRounding,
110
111
  });
111
112
  return {
112
113
  id,
@@ -12,6 +12,7 @@ export const configure = (input = {}) => {
12
12
  baseURL: input.baseURL ?? baseURL,
13
13
  headers: input.headers,
14
14
  http: HttpOptions.make(input.http),
15
+ rounding: SystemOne.jevRounding,
15
16
  });
16
17
  return { id, experimental: { evaluation }, configure };
17
18
  };
@@ -84,6 +84,7 @@ export declare const configure: (input?: Options) => {
84
84
  messages: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
85
85
  responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
86
86
  chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
87
+ image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("../protocols/openai-images.js").OpenAIImageOptions>;
87
88
  experimental: {
88
89
  evaluation: (modelID: string | ModelID) => EvaluationModel<EvaluationOptions>;
89
90
  };
@@ -95,6 +96,7 @@ export declare const provider: {
95
96
  messages: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
96
97
  responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
97
98
  chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
99
+ image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("../protocols/openai-images.js").OpenAIImageOptions>;
98
100
  experimental: {
99
101
  evaluation: (modelID: string | ModelID) => EvaluationModel<EvaluationOptions>;
100
102
  };
@@ -106,6 +108,7 @@ export declare const experimental: {
106
108
  export declare const messages: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
107
109
  export declare const responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
108
110
  export declare const chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<ProviderOptionsInput, import("../route/client.js").CompactionOperations | undefined>;
111
+ export declare const image: (modelID: string | ModelID) => import("../image.js").ImageModel<import("../protocols/openai-images.js").OpenAIImageOptions>;
109
112
  export declare const model: ProviderPackage.Definition<Settings, ProviderOptionsInput>["model"];
110
113
  export declare const messagesModel: ProviderPackage.Definition<Settings, ProviderOptionsInput>["model"];
111
114
  export declare const responsesModel: ProviderPackage.Definition<Settings, ProviderOptionsInput>["model"];
@@ -4,6 +4,7 @@ import { EvaluationAnswer, EvaluationInput, EvaluationModel, EvaluationQuestion,
4
4
  import { AnthropicMessages } from "../protocols/anthropic-messages.js";
5
5
  import { OpenAIChat } from "../protocols/openai-chat.js";
6
6
  import { OpenResponses } from "../protocols/open-responses.js";
7
+ import { OpenAIImages } from "../protocols/openai-images.js";
7
8
  import { JsonObject, optionalNull, ProviderShared } from "../protocols/shared.js";
8
9
  import { gatewayProtocol } from "../protocols/utils/gateway-protocol.js";
9
10
  import { Auth } from "../route/auth.js";
@@ -12,6 +13,7 @@ import { Route } from "../route/client.js";
12
13
  import { Endpoint } from "../route/endpoint.js";
13
14
  import { RequestExecutor } from "../route/executor.js";
14
15
  import { Framing } from "../route/framing.js";
16
+ import { MediaRoute } from "../route/media.js";
15
17
  import { AIError, HttpOptions, InvalidProviderOutputError, InvalidRequestError, LLMRequest, ModelID, ProviderID, ProviderMetadata, ProviderOptions, ReasoningEffort, Usage, } from "../schema/index.js";
16
18
  export const id = ProviderID.make("vercel-ai-gateway");
17
19
  const baseURL = "https://ai-gateway.vercel.sh/v1";
@@ -203,13 +205,16 @@ export const configure = (input = {}) => {
203
205
  }),
204
206
  },
205
207
  });
206
- return { id, model, messages, responses, chat, experimental: { evaluation }, configure };
208
+ const media = MediaRoute.deployment({ ...input, baseURL: configured.endpoint.baseURL }, configured.auth);
209
+ const image = (modelID) => OpenAIImages.model({ ...media, id: modelID, provider: id });
210
+ return { id, model, messages, responses, chat, image, experimental: { evaluation }, configure };
207
211
  };
208
212
  export const provider = configure();
209
213
  export const experimental = provider.experimental;
210
214
  export const messages = provider.messages;
211
215
  export const responses = provider.responses;
212
216
  export const chat = provider.chat;
217
+ export const image = provider.image;
213
218
  export const model = (modelID, settings) => fromSettings(settings).model(modelID);
214
219
  export const messagesModel = (modelID, settings) => fromSettings(settings).messages(modelID);
215
220
  export const responsesModel = (modelID, settings) => fromSettings(settings).responses(modelID);
@@ -153,6 +153,7 @@ export declare const identity: (input: {
153
153
  pending: (id: string) => AIError;
154
154
  contentPolicy: (message: string) => AIError;
155
155
  }, AIError, never>;
156
+ bytes: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<Uint8Array<ArrayBuffer>, AIError, never>;
156
157
  decodeJson: <A>(schema: Schema.Codec<A, unknown>) => (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<{
157
158
  body: string;
158
159
  http: HttpContext;
@@ -32,6 +32,14 @@ const FAILURES = {
32
32
  export const identity = (input) => {
33
33
  const provider = ProviderID.make(input.provider);
34
34
  const frameError = (message, body, cause) => new AIError({ reason: new InvalidProviderOutputError({ route: input.id, message, body, cause }) });
35
+ const readError = (http) => (cause) => new AIError({
36
+ reason: new InvalidProviderOutputError({
37
+ route: input.id,
38
+ message: `Failed to read the ${input.name} response`,
39
+ http,
40
+ cause,
41
+ }),
42
+ });
35
43
  /**
36
44
  * Read a text body while retaining the original payload and HTTP context on every downstream error. `invalid` is a
37
45
  * malformed provider document; `ended` is a generation that reached a terminal status without output (`failed`
@@ -41,14 +49,7 @@ export const identity = (input) => {
41
49
  */
42
50
  const text = Effect.fn("MediaProtocol.text")(function* (response) {
43
51
  const http = RequestExecutor.responseHttp(response);
44
- const body = yield* response.text.pipe(Effect.mapError((cause) => new AIError({
45
- reason: new InvalidProviderOutputError({
46
- route: input.id,
47
- message: `Failed to read the ${input.name} response`,
48
- http,
49
- cause,
50
- }),
51
- })));
52
+ const body = yield* response.text.pipe(Effect.mapError(readError(http)));
52
53
  return {
53
54
  body,
54
55
  http,
@@ -77,11 +78,14 @@ export const identity = (input) => {
77
78
  return { ...output, value };
78
79
  });
79
80
  };
81
+ /** Read a raw binary body, for providers that answer with the media bytes themselves. */
82
+ const bytes = (response) => response.arrayBuffer.pipe(Effect.map((buffer) => new Uint8Array(buffer)), Effect.mapError(readError(RequestExecutor.responseHttp(response))));
80
83
  return {
81
84
  id: input.id,
82
85
  name: input.name,
83
86
  provider,
84
87
  text,
88
+ bytes,
85
89
  decodeJson,
86
90
  /** Decode a submission response into the token and first snapshot. */
87
91
  decodeStarted: (schema, started) => {