@opencode/ai 2.1.3 → 2.2.0
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 +283 -114
- package/dist/ai-client.d.ts +2 -2
- package/dist/ai-client.js +2 -1
- package/dist/cache-policy.d.ts +4 -0
- package/dist/cache-policy.js +20 -6
- package/dist/decision-client.d.ts +21 -0
- package/dist/decision-client.js +55 -0
- package/dist/decision.d.ts +472 -0
- package/dist/decision.js +158 -0
- package/dist/effect.d.ts +38 -0
- package/dist/effect.js +27 -0
- package/dist/effort-updates.d.ts +2 -2
- package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
- package/dist/experimental/cloudflare-run-extraction.js +83 -0
- package/dist/experimental/extraction.d.ts +183 -0
- package/dist/experimental/extraction.js +102 -0
- package/dist/experimental/realtime.d.ts +7 -2
- package/dist/experimental/realtime.js +5 -5
- package/dist/experimental.d.ts +2 -3
- package/dist/experimental.js +1 -2
- package/dist/image-client.d.ts +13 -22
- package/dist/image-client.js +0 -1
- package/dist/image.d.ts +154 -324
- package/dist/image.js +4 -11
- package/dist/index.d.ts +1426 -34
- package/dist/index.js +127 -24
- package/dist/llm.d.ts +16 -1
- package/dist/llm.js +34 -0
- package/dist/media-client.d.ts +2 -1
- package/dist/media-model.d.ts +3 -0
- package/dist/media-model.js +4 -1
- package/dist/media.d.ts +0 -7
- package/dist/media.js +0 -6
- package/dist/protocols/alibaba-chat.d.ts +32 -12
- package/dist/protocols/alibaba-images.d.ts +22 -0
- package/dist/protocols/alibaba-images.js +77 -0
- package/dist/protocols/alibaba-messages.d.ts +62 -27
- package/dist/protocols/alibaba-responses.d.ts +41 -38
- package/dist/protocols/anthropic-messages.d.ts +196 -94
- package/dist/protocols/anthropic-messages.js +47 -16
- package/dist/protocols/bedrock-converse.d.ts +140 -47
- package/dist/protocols/bedrock-converse.js +114 -18
- package/dist/protocols/bedrock-event-stream.js +1 -0
- package/dist/protocols/bfl-images.d.ts +9 -0
- package/dist/protocols/bfl-images.js +17 -15
- package/dist/protocols/cartesia-speech.d.ts +32 -50
- package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
- package/dist/protocols/cloudflare-run-decisions.js +36 -0
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
- package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
- package/dist/protocols/cohere-chat.d.ts +7 -8
- package/dist/protocols/deepgram-speech.d.ts +32 -50
- package/dist/protocols/elevenlabs-speech.d.ts +32 -50
- package/dist/protocols/fal-images.d.ts +10 -1
- package/dist/protocols/fal-images.js +16 -25
- package/dist/protocols/fal-video.js +1 -1
- package/dist/protocols/gemini.d.ts +37 -34
- package/dist/protocols/gemini.js +13 -8
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-images.js +8 -37
- package/dist/protocols/google-interactions.d.ts +18 -14
- package/dist/protocols/google-interactions.js +14 -7
- package/dist/protocols/google-speech.d.ts +32 -50
- package/dist/protocols/google-speech.js +0 -1
- package/dist/protocols/google-transcription.d.ts +32 -50
- package/dist/protocols/google-transcription.js +0 -1
- package/dist/protocols/google-video.d.ts +1 -1
- package/dist/protocols/google-video.js +2 -7
- package/dist/protocols/index.d.ts +3 -0
- package/dist/protocols/index.js +3 -0
- package/dist/protocols/meta-images.d.ts +7 -1
- package/dist/protocols/meta-images.js +10 -8
- package/dist/protocols/meta-messages.d.ts +73 -38
- package/dist/protocols/meta-messages.js +14 -1
- package/dist/protocols/meta-responses.d.ts +84 -78
- package/dist/protocols/meta-responses.js +29 -1
- package/dist/protocols/mistral-chat.d.ts +81 -78
- package/dist/protocols/mistral-chat.js +2 -1
- package/dist/protocols/open-responses-channel.js +47 -46
- package/dist/protocols/open-responses-continuation.d.ts +8 -2
- package/dist/protocols/open-responses-continuation.js +3 -1
- package/dist/protocols/open-responses.d.ts +426 -396
- package/dist/protocols/open-responses.js +16 -25
- package/dist/protocols/openai-chat.d.ts +764 -52
- package/dist/protocols/openai-chat.js +38 -25
- package/dist/protocols/openai-compatible-chat.d.ts +35 -15
- package/dist/protocols/openai-compatible-responses.d.ts +44 -42
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +128 -0
- package/dist/protocols/openai-images.d.ts +53 -56
- package/dist/protocols/openai-images.js +100 -34
- package/dist/protocols/openai-responses.d.ts +322 -264
- package/dist/protocols/openai-responses.js +33 -3
- package/dist/protocols/openai-speech.d.ts +32 -50
- package/dist/protocols/openai-speech.js +15 -5
- package/dist/protocols/openai-transcription.d.ts +32 -50
- package/dist/protocols/openai-transcription.js +15 -11
- package/dist/protocols/replicate-images.js +1 -1
- package/dist/protocols/runway-video.d.ts +1 -1
- package/dist/protocols/runway-video.js +1 -1
- package/dist/protocols/shared.d.ts +9 -1
- package/dist/protocols/shared.js +13 -2
- package/dist/protocols/stability-images.d.ts +5 -0
- package/dist/protocols/stability-images.js +4 -9
- package/dist/protocols/system-one.d.ts +101 -0
- package/dist/protocols/system-one.js +230 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +2 -2
- package/dist/protocols/utils/bedrock-cache.js +7 -6
- package/dist/protocols/utils/cache.d.ts +14 -3
- package/dist/protocols/utils/cache.js +11 -4
- package/dist/protocols/utils/gemini-generate-content.d.ts +3 -3
- package/dist/protocols/utils/gemini-generate-content.js +10 -16
- package/dist/protocols/utils/gemini-json-schema.d.ts +5 -0
- package/dist/protocols/utils/gemini-json-schema.js +3 -3
- package/dist/protocols/utils/image-sizing.d.ts +31 -0
- package/dist/protocols/utils/image-sizing.js +73 -0
- package/dist/protocols/utils/media-input.d.ts +4 -5
- package/dist/protocols/utils/media-input.js +8 -5
- package/dist/protocols/utils/partial-json-options.d.ts +0 -1
- package/dist/protocols/utils/partial-json-options.js +0 -1
- package/dist/protocols/utils/speech-stream.d.ts +0 -1
- package/dist/protocols/utils/speech-stream.js +0 -1
- package/dist/protocols/utils/tool-schema.js +15 -5
- package/dist/protocols/utils/tool-stream.d.ts +10 -1
- package/dist/protocols/venice-chat.d.ts +71 -53
- package/dist/protocols/venice-chat.js +7 -6
- package/dist/protocols/xai-images.d.ts +1 -2
- package/dist/protocols/xai-images.js +9 -2
- package/dist/protocols/xai-responses.d.ts +88 -78
- package/dist/protocols/xai-responses.js +28 -2
- package/dist/protocols/xai-video.d.ts +1 -1
- package/dist/protocols/xai-video.js +11 -8
- package/dist/protocols/zai-chat.d.ts +54 -30
- package/dist/protocols/zai-chat.js +8 -3
- package/dist/protocols/zai-images.d.ts +3 -1
- package/dist/protocols/zai-images.js +12 -11
- package/dist/protocols/zai-messages.d.ts +72 -35
- package/dist/protocols/zai-messages.js +3 -2
- package/dist/provider-error.js +24 -7
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba.d.ts +146 -85
- package/dist/providers/alibaba.js +16 -8
- package/dist/providers/amazon-bedrock-mantle.d.ts +277 -218
- package/dist/providers/amazon-bedrock.d.ts +48 -28
- package/dist/providers/amazon-bedrock.js +11 -11
- package/dist/providers/anthropic-compatible.d.ts +64 -29
- package/dist/providers/anthropic.d.ts +64 -29
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure.d.ts +156 -115
- package/dist/providers/azure.js +19 -1
- package/dist/providers/baseten/decisions.d.ts +2 -0
- package/dist/providers/baseten/decisions.js +1 -0
- package/dist/providers/baseten.d.ts +75 -25
- package/dist/providers/baseten.js +41 -8
- package/dist/providers/cerebras.d.ts +66 -24
- package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +409 -272
- package/dist/providers/cloudflare-ai-gateway.js +33 -6
- package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
- package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/images.js +1 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +79 -25
- package/dist/providers/cloudflare-workers-ai.js +45 -9
- package/dist/providers/cohere.d.ts +117 -75
- package/dist/providers/deepinfra.d.ts +66 -24
- package/dist/providers/deepseek.d.ts +66 -24
- package/dist/providers/digitalocean/decisions.d.ts +2 -0
- package/dist/providers/digitalocean/decisions.js +1 -0
- package/dist/providers/digitalocean/images.d.ts +2 -0
- package/dist/providers/digitalocean/images.js +1 -0
- package/dist/providers/digitalocean.d.ts +108 -37
- package/dist/providers/digitalocean.js +39 -8
- package/dist/providers/fireworks.d.ts +66 -24
- package/dist/providers/google/images.d.ts +2 -0
- package/dist/providers/google/images.js +1 -0
- package/dist/providers/google-vertex/images.d.ts +2 -0
- package/dist/providers/google-vertex/images.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +33 -12
- package/dist/providers/google-vertex-messages.d.ts +64 -29
- package/dist/providers/google-vertex-mistral.d.ts +17 -17
- package/dist/providers/google-vertex-responses.d.ts +38 -35
- package/dist/providers/google-vertex.d.ts +20 -18
- package/dist/providers/google-vertex.js +32 -15
- package/dist/providers/google.d.ts +22 -21
- package/dist/providers/google.js +15 -8
- package/dist/providers/groq.d.ts +66 -28
- package/dist/providers/groq.js +0 -6
- package/dist/providers/meta.d.ts +144 -85
- package/dist/providers/minimax.d.ts +145 -86
- package/dist/providers/mistral.d.ts +28 -28
- package/dist/providers/moonshot.d.ts +277 -218
- package/dist/providers/open-responses-options.d.ts +0 -1
- package/dist/providers/open-responses-options.js +1 -1
- package/dist/providers/openai/images.d.ts +2 -0
- package/dist/providers/openai/images.js +1 -0
- package/dist/providers/openai-compatible/images.d.ts +2 -0
- package/dist/providers/openai-compatible/images.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +38 -35
- package/dist/providers/openai-compatible.d.ts +36 -13
- package/dist/providers/openai-compatible.js +22 -10
- package/dist/providers/openai-options.d.ts +3 -2
- package/dist/providers/openai-options.js +0 -1
- package/dist/providers/openai.d.ts +146 -109
- package/dist/providers/openai.js +20 -4
- package/dist/providers/opencode/decisions.d.ts +2 -0
- package/dist/providers/opencode/decisions.js +1 -0
- package/dist/providers/opencode.d.ts +9 -10
- package/dist/providers/opencode.js +10 -8
- package/dist/providers/openrouter/decisions.d.ts +2 -0
- package/dist/providers/openrouter/decisions.js +1 -0
- package/dist/providers/openrouter/images.d.ts +2 -0
- package/dist/providers/openrouter/images.js +1 -0
- package/dist/providers/openrouter.d.ts +128 -47
- package/dist/providers/openrouter.js +38 -16
- package/dist/providers/togetherai.d.ts +66 -24
- package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
- package/dist/providers/typesafe-ai/decisions.js +1 -0
- package/dist/providers/typesafe-ai.d.ts +9 -10
- package/dist/providers/typesafe-ai.js +10 -8
- package/dist/providers/venice.d.ts +214 -20
- package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
- package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/images.js +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +10 -16
- package/dist/providers/vercel-ai-gateway.js +31 -85
- package/dist/providers/xai/images.d.ts +2 -0
- package/dist/providers/xai/images.js +1 -0
- package/dist/providers/xai.d.ts +40 -17
- package/dist/providers/xai.js +15 -8
- package/dist/providers/zai/images.d.ts +2 -0
- package/dist/providers/zai/images.js +1 -0
- package/dist/providers/zai-coding-plan.d.ts +224 -85
- package/dist/providers/zai.d.ts +110 -23
- package/dist/providers/zai.js +15 -8
- package/dist/route/auth-options.d.ts +0 -3
- package/dist/route/auth.d.ts +0 -2
- package/dist/route/auth.js +0 -2
- package/dist/route/client.d.ts +2 -4
- package/dist/route/client.js +29 -8
- package/dist/route/decision.d.ts +58 -0
- package/dist/route/decision.js +75 -0
- package/dist/route/executor-service.js +1 -1
- package/dist/route/executor.d.ts +1 -1
- package/dist/route/framing.d.ts +2 -0
- package/dist/route/index.d.ts +3 -0
- package/dist/route/index.js +3 -0
- package/dist/route/media-protocol.d.ts +1 -0
- package/dist/route/media-protocol.js +12 -8
- package/dist/route/media.d.ts +5 -1
- package/dist/route/media.js +6 -2
- package/dist/route/transport/http.js +1 -0
- package/dist/route/transport/index.d.ts +1 -0
- package/dist/schema/events.d.ts +148 -6
- package/dist/schema/events.js +20 -0
- package/dist/schema/messages.d.ts +6 -2
- package/dist/schema/messages.js +3 -0
- package/dist/schema/options.d.ts +5 -3
- package/dist/schema/options.js +8 -3
- package/dist/speech-client.d.ts +13 -22
- package/dist/speech-client.js +0 -1
- package/dist/speech.d.ts +148 -283
- package/dist/speech.js +2 -4
- package/dist/testing.d.ts +25 -24
- package/dist/testing.js +3 -21
- package/dist/transcription-client.d.ts +13 -22
- package/dist/transcription.d.ts +148 -283
- package/dist/transcription.js +1 -1
- package/dist/utils/media-type.js +22 -1
- package/dist/video-client.d.ts +13 -22
- package/dist/video-client.js +0 -1
- package/dist/video.d.ts +115 -250
- package/dist/video.js +3 -5
- package/package.json +516 -13
- package/dist/experimental/evaluation-client.d.ts +0 -21
- package/dist/experimental/evaluation-client.js +0 -64
- package/dist/experimental/evaluation.d.ts +0 -242
- package/dist/experimental/evaluation.js +0 -115
- package/dist/experimental/system-one.d.ts +0 -16
- package/dist/experimental/system-one.js +0 -135
- package/dist/promise.d.ts +0 -1021
- package/dist/promise.js +0 -101
- package/dist/provider.d.ts +0 -23
- package/dist/provider.js +0 -2
|
@@ -10,7 +10,7 @@ import { JsonObject, optionalArray, ProviderShared } from "./shared.js";
|
|
|
10
10
|
import { BedrockAuth } from "./utils/bedrock-auth.js";
|
|
11
11
|
import { BedrockCache } from "./utils/bedrock-cache.js";
|
|
12
12
|
import { BedrockMedia } from "./utils/bedrock-media.js";
|
|
13
|
-
import { supportsThinkingBlockBinding, THINKING_BINDING_BETA } from "./utils/claude-model.js";
|
|
13
|
+
import { claudeVersion, supportsThinkingBlockBinding, THINKING_BINDING_BETA } from "./utils/claude-model.js";
|
|
14
14
|
import { Lifecycle } from "./utils/lifecycle.js";
|
|
15
15
|
import { MistralToolID } from "./utils/mistral-tool-id.js";
|
|
16
16
|
import { ToolStream } from "./utils/tool-stream.js";
|
|
@@ -86,6 +86,11 @@ const BedrockToolChoice = Schema.Union([
|
|
|
86
86
|
Schema.Struct({ any: Schema.Struct({}) }),
|
|
87
87
|
Schema.Struct({ tool: Schema.Struct({ name: Schema.String }) }),
|
|
88
88
|
]);
|
|
89
|
+
// Value sets from the Converse API reference. Unknown values pass through for forward compatibility.
|
|
90
|
+
const ServiceTierType = ProviderShared.knownString();
|
|
91
|
+
const GuardrailTrace = ProviderShared.knownString();
|
|
92
|
+
const GuardrailStreamProcessingMode = ProviderShared.knownString();
|
|
93
|
+
const TextFormatType = ProviderShared.knownString();
|
|
89
94
|
const BedrockBodyFields = {
|
|
90
95
|
modelId: Schema.String,
|
|
91
96
|
messages: Schema.Array(BedrockMessage),
|
|
@@ -101,6 +106,26 @@ const BedrockBodyFields = {
|
|
|
101
106
|
toolChoice: Schema.optional(BedrockToolChoice),
|
|
102
107
|
})),
|
|
103
108
|
additionalModelRequestFields: Schema.optional(JsonObject),
|
|
109
|
+
serviceTier: Schema.optional(Schema.Struct({ type: ServiceTierType })),
|
|
110
|
+
guardrailConfig: Schema.optional(Schema.Struct({
|
|
111
|
+
guardrailIdentifier: Schema.String,
|
|
112
|
+
guardrailVersion: Schema.String,
|
|
113
|
+
trace: Schema.optional(GuardrailTrace),
|
|
114
|
+
streamProcessingMode: Schema.optional(GuardrailStreamProcessingMode),
|
|
115
|
+
})),
|
|
116
|
+
outputConfig: Schema.optional(Schema.Struct({
|
|
117
|
+
textFormat: Schema.Struct({
|
|
118
|
+
type: TextFormatType,
|
|
119
|
+
structure: Schema.Struct({
|
|
120
|
+
jsonSchema: Schema.Struct({
|
|
121
|
+
schema: Schema.String,
|
|
122
|
+
name: Schema.optional(Schema.String),
|
|
123
|
+
description: Schema.optional(Schema.String),
|
|
124
|
+
}),
|
|
125
|
+
}),
|
|
126
|
+
}),
|
|
127
|
+
})),
|
|
128
|
+
requestMetadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
104
129
|
};
|
|
105
130
|
const BedrockConverseBody = Schema.Struct(BedrockBodyFields);
|
|
106
131
|
const BedrockUsageSchema = Schema.Struct({
|
|
@@ -244,7 +269,7 @@ const lowerMessages = Effect.fnUntraced(function* (request, breakpoints) {
|
|
|
244
269
|
const documentNames = new Set();
|
|
245
270
|
// Mistral can reject replay IDs even when they satisfy Converse's broader ID syntax.
|
|
246
271
|
const normalizeID = request.model.id.includes("mistral.") ? MistralToolID.normalizer(request) : (id) => id;
|
|
247
|
-
const providerMetadataKey = request.model.route.providerMetadataKey ??
|
|
272
|
+
const providerMetadataKey = request.model.route.providerMetadataKey ?? request.model.provider;
|
|
248
273
|
const hoistImages = !keepToolImagesInline(request.model.id);
|
|
249
274
|
// Bedrock expects parallel tool results before any images hoisted beside them.
|
|
250
275
|
const pendingImages = [];
|
|
@@ -376,8 +401,49 @@ const isHighReasoningEffort = Schema.is(Schema.Struct({
|
|
|
376
401
|
reasoningConfig: Schema.Struct({ maxReasoningEffort: Schema.Literal("high") }),
|
|
377
402
|
}),
|
|
378
403
|
}));
|
|
404
|
+
// Claude 4.6+ pairs effort with adaptive thinking; earlier Claude models take effort beside a manual budget.
|
|
405
|
+
const supportsAdaptiveThinking = (model) => {
|
|
406
|
+
const version = claudeVersion(model.id);
|
|
407
|
+
return version !== undefined && (version.major > 4 || (version.major === 4 && version.minor >= 6));
|
|
408
|
+
};
|
|
409
|
+
const effortFields = (model, effort) => {
|
|
410
|
+
if (model.id.includes("anthropic"))
|
|
411
|
+
return { output_config: { effort } };
|
|
412
|
+
// gpt-oss takes the flat chat-completions spelling; newer OpenAI and xAI models take the Responses one.
|
|
413
|
+
if (model.id.includes("openai.gpt-oss"))
|
|
414
|
+
return { reasoning_effort: effort };
|
|
415
|
+
if (model.id.includes("openai.") || model.id.includes("xai."))
|
|
416
|
+
return { reasoning: { effort } };
|
|
417
|
+
return { reasoningConfig: { type: "enabled", maxReasoningEffort: effort } };
|
|
418
|
+
};
|
|
379
419
|
const Options = Schema.Struct({
|
|
380
|
-
thinking
|
|
420
|
+
/** Claude thinking. `adaptive` and `disabled` apply to Claude models only. */
|
|
421
|
+
thinking: Schema.optional(Schema.Union([
|
|
422
|
+
Schema.Struct({ type: Schema.Literal("enabled"), budgetTokens: Schema.Number }),
|
|
423
|
+
Schema.Struct({
|
|
424
|
+
type: Schema.Literal("adaptive"),
|
|
425
|
+
display: Schema.optional(ProviderShared.knownString()),
|
|
426
|
+
}),
|
|
427
|
+
Schema.Struct({ type: Schema.Literal("disabled") }),
|
|
428
|
+
])),
|
|
429
|
+
/** Reasoning effort, sent in the field each model family reads. */
|
|
430
|
+
effort: Schema.optional(ProviderShared.knownString()),
|
|
431
|
+
serviceTier: Schema.optional(ServiceTierType),
|
|
432
|
+
/** Constrains the response text to a JSON schema. */
|
|
433
|
+
outputFormat: Schema.optional(Schema.Struct({
|
|
434
|
+
type: TextFormatType,
|
|
435
|
+
schema: JsonObject,
|
|
436
|
+
name: Schema.optional(Schema.String),
|
|
437
|
+
description: Schema.optional(Schema.String),
|
|
438
|
+
})),
|
|
439
|
+
guardrail: Schema.optional(Schema.Struct({
|
|
440
|
+
identifier: Schema.String,
|
|
441
|
+
version: Schema.String,
|
|
442
|
+
trace: Schema.optional(GuardrailTrace),
|
|
443
|
+
streamProcessingMode: Schema.optional(GuardrailStreamProcessingMode),
|
|
444
|
+
})),
|
|
445
|
+
/** Key-value pairs for filtering invocation logs. */
|
|
446
|
+
requestMetadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
381
447
|
});
|
|
382
448
|
const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options));
|
|
383
449
|
// Claude on Bedrock requires the thinking budget below `maxTokens`, with a minimum of 1,024.
|
|
@@ -390,7 +456,7 @@ const isThinkingDisabled = Schema.is(Schema.Struct({
|
|
|
390
456
|
// Claude 5.1+ binds each thinking signature to the prefix above it. Ask Bedrock to drop the affected blocks instead of
|
|
391
457
|
// failing when that prefix changes. `http.body` overlays this field by field, so callers can still override it.
|
|
392
458
|
const applyThinkingBindingDefault = (request, thinking) => {
|
|
393
|
-
if (isThinkingDisabled(request.http?.body))
|
|
459
|
+
if (thinking?.type === "disabled" || isThinkingDisabled(request.http?.body))
|
|
394
460
|
return thinking;
|
|
395
461
|
if (!supportsThinkingBlockBinding(request.model))
|
|
396
462
|
return thinking;
|
|
@@ -404,13 +470,27 @@ const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request)
|
|
|
404
470
|
const flattened = ProviderShared.flattenToolRequest(request);
|
|
405
471
|
const generation = request.generation;
|
|
406
472
|
const options = yield* decodeOptions(request.providerOptions ?? {});
|
|
407
|
-
const maxTokens = isNova2(request.model) && isHighReasoningEffort(request.http?.body)
|
|
408
|
-
const thinking = applyThinkingBindingDefault(request, options.thinking === undefined
|
|
473
|
+
const maxTokens = isNova2(request.model) && (options.effort === "high" || isHighReasoningEffort(request.http?.body))
|
|
409
474
|
? undefined
|
|
410
|
-
:
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
475
|
+
: generation?.maxTokens;
|
|
476
|
+
const thinking = applyThinkingBindingDefault(request, (() => {
|
|
477
|
+
if (options.thinking?.type === "enabled")
|
|
478
|
+
return {
|
|
479
|
+
type: "enabled",
|
|
480
|
+
budget_tokens: ProviderShared.fitThinkingBudget(options.thinking.budgetTokens, maxTokens, MIN_THINKING_BUDGET),
|
|
481
|
+
};
|
|
482
|
+
if (options.thinking !== undefined)
|
|
483
|
+
return options.thinking;
|
|
484
|
+
if (options.effort !== undefined && supportsAdaptiveThinking(request.model))
|
|
485
|
+
return { type: "adaptive" };
|
|
486
|
+
})());
|
|
487
|
+
const additionalModelRequestFields = {
|
|
488
|
+
...(generation?.topK === undefined ? {} : { top_k: generation.topK }),
|
|
489
|
+
...(thinking === undefined ? {} : { thinking }),
|
|
490
|
+
...(options.effort === undefined ? {} : effortFields(request.model, options.effort)),
|
|
491
|
+
// Converse takes Anthropic betas in the body, and Bedrock rejects `block_binding` without this one.
|
|
492
|
+
...(thinking?.block_binding === undefined ? {} : { anthropic_beta: [THINKING_BINDING_BETA] }),
|
|
493
|
+
};
|
|
414
494
|
// Bedrock-Claude shares Anthropic's 4-breakpoint cap. Spend the budget in
|
|
415
495
|
// tools → system → messages order to favour the highest-impact prefixes.
|
|
416
496
|
const breakpoints = BedrockCache.breakpoints(request.model);
|
|
@@ -448,16 +528,33 @@ const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request)
|
|
|
448
528
|
system,
|
|
449
529
|
inferenceConfig,
|
|
450
530
|
toolConfig,
|
|
451
|
-
// Converse's base inferenceConfig has no topK or
|
|
531
|
+
// Converse's base inferenceConfig has no topK, thinking or effort; models accept them
|
|
452
532
|
// as model-specific fields, so they go through additionalModelRequestFields.
|
|
453
|
-
additionalModelRequestFields:
|
|
533
|
+
additionalModelRequestFields: Object.keys(additionalModelRequestFields).length === 0 ? undefined : additionalModelRequestFields,
|
|
534
|
+
serviceTier: options.serviceTier === undefined ? undefined : { type: options.serviceTier },
|
|
535
|
+
guardrailConfig: options.guardrail === undefined
|
|
454
536
|
? undefined
|
|
455
537
|
: {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
538
|
+
guardrailIdentifier: options.guardrail.identifier,
|
|
539
|
+
guardrailVersion: options.guardrail.version,
|
|
540
|
+
trace: options.guardrail.trace,
|
|
541
|
+
streamProcessingMode: options.guardrail.streamProcessingMode,
|
|
542
|
+
},
|
|
543
|
+
outputConfig: options.outputFormat === undefined
|
|
544
|
+
? undefined
|
|
545
|
+
: {
|
|
546
|
+
textFormat: {
|
|
547
|
+
type: options.outputFormat.type,
|
|
548
|
+
structure: {
|
|
549
|
+
jsonSchema: {
|
|
550
|
+
schema: ProviderShared.encodeJson(options.outputFormat.schema),
|
|
551
|
+
name: options.outputFormat.name,
|
|
552
|
+
description: options.outputFormat.description,
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
},
|
|
460
556
|
},
|
|
557
|
+
requestMetadata: options.requestMetadata,
|
|
461
558
|
};
|
|
462
559
|
});
|
|
463
560
|
// =============================================================================
|
|
@@ -693,7 +790,7 @@ export const protocol = Protocol.make({
|
|
|
693
790
|
stream: {
|
|
694
791
|
event: BedrockEventStream.Frame.pipe(Schema.decodeTo(BedrockEvent)),
|
|
695
792
|
initial: (request) => ({
|
|
696
|
-
providerMetadataKey: request.model.route.providerMetadataKey ??
|
|
793
|
+
providerMetadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
697
794
|
tools: ToolStream.empty(),
|
|
698
795
|
finishReason: undefined,
|
|
699
796
|
usage: undefined,
|
|
@@ -718,5 +815,4 @@ export const route = Route.make({
|
|
|
718
815
|
auth: BedrockAuth.auth,
|
|
719
816
|
framing,
|
|
720
817
|
});
|
|
721
|
-
export const sigV4Auth = BedrockAuth.sigV4;
|
|
722
818
|
export * as BedrockConverse from "./bedrock-converse.js";
|
|
@@ -94,6 +94,7 @@ const consumeFrames = (route) => (state, input) => Effect.gen(function* () {
|
|
|
94
94
|
export const framing = (route) => ({
|
|
95
95
|
id: "aws-event-stream",
|
|
96
96
|
body: (frame) => frame.rawBody,
|
|
97
|
+
raw: ({ rawBody: _, ...event }) => event,
|
|
97
98
|
frame: (bytes) => bytes.pipe(Stream.map((bytes) => ({ _tag: "Chunk", bytes })), Stream.concat(Stream.succeed(endOfStream)), Stream.mapAccumEffect(() => initialFrameBuffer, consumeFrames(route))),
|
|
98
99
|
});
|
|
99
100
|
export * as BedrockEventStream from "./bedrock-event-stream.js";
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
2
|
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
3
|
+
import { Media } from "../media.js";
|
|
3
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
|
+
import { type OpenString } from "../schema/index.js";
|
|
5
7
|
export declare const DEFAULT_BASE_URL = "https://api.bfl.ai";
|
|
8
|
+
/** Native body fields; `aspect` and `resolution` lower to `width`/`height` or `aspect_ratio` depending on the model. */
|
|
6
9
|
export type BlackForestLabsImageOptions = {
|
|
10
|
+
readonly width?: number;
|
|
11
|
+
readonly height?: number;
|
|
12
|
+
readonly seed?: number;
|
|
13
|
+
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
14
|
+
/** Inpainting mask for `flux-pro-1.0-fill`. */
|
|
15
|
+
readonly mask?: Media.Asset;
|
|
7
16
|
readonly safety_tolerance?: number;
|
|
8
17
|
readonly prompt_upsampling?: boolean;
|
|
9
18
|
readonly disable_pup?: boolean;
|
|
@@ -5,7 +5,7 @@ import { MediaProtocol } from "../route/media-protocol.js";
|
|
|
5
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
6
|
import { mergeJsonRecords } from "../schema/index.js";
|
|
7
7
|
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
-
import {
|
|
8
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
9
9
|
const route = MediaProtocol.identity({ id: "bfl-images", name: "Black Forest Labs", provider: "black-forest-labs" });
|
|
10
10
|
export const DEFAULT_BASE_URL = "https://api.bfl.ai";
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
@@ -43,6 +43,8 @@ const STATUS = {
|
|
|
43
43
|
"Task not found": "expired",
|
|
44
44
|
};
|
|
45
45
|
const isModerated = (status) => status === "Content Moderated" || status === "Request Moderated";
|
|
46
|
+
const LEGACY_LIMITS = { multiple: 32, minEdge: 256, maxEdge: 1440 };
|
|
47
|
+
const FLUX_2_LIMITS = { multiple: 16, minEdge: 64, maxPixels: 4_194_304 };
|
|
46
48
|
const capabilities = (model) => {
|
|
47
49
|
if (model.startsWith("flux-pro-1.0-fill"))
|
|
48
50
|
return { sizing: "none", imageField: "image", maxImages: 1, mask: true };
|
|
@@ -53,23 +55,23 @@ const capabilities = (model) => {
|
|
|
53
55
|
if (model.startsWith("flux-pro-1.1-ultra"))
|
|
54
56
|
return { sizing: "aspectRatio", imageField: "image_prompt", maxImages: 1, mask: false };
|
|
55
57
|
if (model.startsWith("flux-pro-1.1") || model.startsWith("flux-dev"))
|
|
56
|
-
return { sizing:
|
|
58
|
+
return { sizing: LEGACY_LIMITS, imageField: "image_prompt", maxImages: 1, mask: false };
|
|
57
59
|
if (model.startsWith("flux-2-klein"))
|
|
58
|
-
return { sizing:
|
|
59
|
-
return { sizing:
|
|
60
|
+
return { sizing: FLUX_2_LIMITS, imageField: "input_image", maxImages: 4, mask: false };
|
|
61
|
+
return { sizing: FLUX_2_LIMITS, imageField: "input_image", maxImages: 8, mask: false };
|
|
60
62
|
};
|
|
61
63
|
const validate = (request, model) => {
|
|
62
64
|
const id = request.model.id;
|
|
63
65
|
const images = request.images?.length ?? 0;
|
|
64
66
|
if (request.n !== undefined && request.n > 1)
|
|
65
67
|
return Effect.fail(route.unsupported("media.n", `${id} generates one image per request; call it once per image`));
|
|
66
|
-
if (request.
|
|
67
|
-
return Effect.fail(route.unsupported("media.
|
|
68
|
-
if (request.
|
|
69
|
-
return Effect.fail(route.unsupported("media.
|
|
68
|
+
if (request.aspect !== undefined && model.sizing === "none")
|
|
69
|
+
return Effect.fail(route.unsupported("media.aspect", `${id} keeps the source image's size`));
|
|
70
|
+
if (request.resolution !== undefined && typeof model.sizing === "string")
|
|
71
|
+
return Effect.fail(route.unsupported("media.resolution", `${id} does not take resolution`));
|
|
70
72
|
if (images > model.maxImages)
|
|
71
73
|
return Effect.fail(route.unsupported("media.images", `${id} takes at most ${model.maxImages} images`));
|
|
72
|
-
if (request.mask !== undefined && !model.mask)
|
|
74
|
+
if (request.providerOptions?.mask !== undefined && !model.mask)
|
|
73
75
|
return Effect.fail(route.unsupported("media.mask", `${id} does not inpaint; use flux-pro-1.0-fill`));
|
|
74
76
|
return Effect.void;
|
|
75
77
|
};
|
|
@@ -87,15 +89,15 @@ const fromRequest = Effect.fn("BlackForestLabsImages.fromRequest")(function* (re
|
|
|
87
89
|
index === 0 ? model.imageField : `${model.imageField}_${index + 1}`,
|
|
88
90
|
image,
|
|
89
91
|
]);
|
|
92
|
+
const { mask, ...native } = request.providerOptions ?? {};
|
|
93
|
+
const size = typeof model.sizing === "string" ? undefined : yield* ImageSizing.exact(route, request, model.sizing);
|
|
90
94
|
return MediaProtocol.json(mergeJsonRecords({
|
|
91
95
|
prompt: request.prompt,
|
|
92
|
-
...
|
|
93
|
-
aspect_ratio: request.
|
|
94
|
-
|
|
95
|
-
output_format: request.format,
|
|
96
|
-
mask: request.mask === undefined ? undefined : yield* imageInput(request.mask),
|
|
96
|
+
...size,
|
|
97
|
+
aspect_ratio: model.sizing === "aspectRatio" ? request.aspect : undefined,
|
|
98
|
+
mask: mask === undefined ? undefined : yield* imageInput(mask),
|
|
97
99
|
...Object.fromEntries(fields),
|
|
98
|
-
},
|
|
100
|
+
}, native, request.http?.body) ?? {});
|
|
99
101
|
});
|
|
100
102
|
// ---------------------------------------------------------------------------
|
|
101
103
|
// 6. Response decoding
|
|
@@ -23,14 +23,6 @@ interface State extends SpeechStream.Audio {
|
|
|
23
23
|
readonly done: boolean;
|
|
24
24
|
}
|
|
25
25
|
export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
26
|
-
readonly type: "generation-queued";
|
|
27
|
-
readonly id: string;
|
|
28
|
-
readonly position?: number | undefined;
|
|
29
|
-
} | {
|
|
30
|
-
readonly type: "generation-progress";
|
|
31
|
-
readonly id: string;
|
|
32
|
-
readonly progress?: number | undefined;
|
|
33
|
-
} | {
|
|
34
26
|
readonly type: "audio-delta";
|
|
35
27
|
readonly chunk: Uint8Array<ArrayBufferLike>;
|
|
36
28
|
} | {
|
|
@@ -40,18 +32,18 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
40
32
|
readonly startSeconds: number;
|
|
41
33
|
readonly endSeconds: number;
|
|
42
34
|
}[];
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "generation-queued";
|
|
37
|
+
readonly id: string;
|
|
38
|
+
readonly position?: number | undefined;
|
|
39
|
+
} | {
|
|
40
|
+
readonly type: "generation-progress";
|
|
41
|
+
readonly id: string;
|
|
42
|
+
readonly progress?: number | undefined;
|
|
43
43
|
} | {
|
|
44
44
|
readonly type: "finish";
|
|
45
45
|
readonly audio: import("../media.js").Asset;
|
|
46
46
|
readonly usage?: {
|
|
47
|
-
readonly type: "tokens";
|
|
48
|
-
readonly input?: number | undefined;
|
|
49
|
-
readonly output?: number | undefined;
|
|
50
|
-
readonly total?: number | undefined;
|
|
51
|
-
readonly details?: {
|
|
52
|
-
readonly [x: string]: unknown;
|
|
53
|
-
} | undefined;
|
|
54
|
-
} | {
|
|
55
47
|
readonly type: "seconds";
|
|
56
48
|
readonly seconds: number;
|
|
57
49
|
} | {
|
|
@@ -63,16 +55,15 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
63
55
|
} | {
|
|
64
56
|
readonly type: "compute";
|
|
65
57
|
readonly seconds: number;
|
|
66
|
-
} |
|
|
67
|
-
|
|
68
|
-
readonly
|
|
69
|
-
readonly
|
|
70
|
-
readonly
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
};
|
|
58
|
+
} | {
|
|
59
|
+
readonly type: "tokens";
|
|
60
|
+
readonly input?: number | undefined;
|
|
61
|
+
readonly output?: number | undefined;
|
|
62
|
+
readonly total?: number | undefined;
|
|
63
|
+
readonly details?: {
|
|
64
|
+
readonly [x: string]: unknown;
|
|
74
65
|
} | undefined;
|
|
75
|
-
}
|
|
66
|
+
} | undefined;
|
|
76
67
|
readonly providerMetadata?: {
|
|
77
68
|
readonly [x: string]: {
|
|
78
69
|
readonly [x: string]: unknown;
|
|
@@ -82,14 +73,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
82
73
|
export declare const model: (input: MediaRoute.ModelInput) => SpeechModel<CartesiaSpeechOptions>;
|
|
83
74
|
export declare const CartesiaSpeech: {
|
|
84
75
|
readonly protocol: MediaProtocol.Streamed<Request, {
|
|
85
|
-
readonly type: "generation-queued";
|
|
86
|
-
readonly id: string;
|
|
87
|
-
readonly position?: number | undefined;
|
|
88
|
-
} | {
|
|
89
|
-
readonly type: "generation-progress";
|
|
90
|
-
readonly id: string;
|
|
91
|
-
readonly progress?: number | undefined;
|
|
92
|
-
} | {
|
|
93
76
|
readonly type: "audio-delta";
|
|
94
77
|
readonly chunk: Uint8Array<ArrayBufferLike>;
|
|
95
78
|
} | {
|
|
@@ -99,18 +82,18 @@ export declare const CartesiaSpeech: {
|
|
|
99
82
|
readonly startSeconds: number;
|
|
100
83
|
readonly endSeconds: number;
|
|
101
84
|
}[];
|
|
85
|
+
} | {
|
|
86
|
+
readonly type: "generation-queued";
|
|
87
|
+
readonly id: string;
|
|
88
|
+
readonly position?: number | undefined;
|
|
89
|
+
} | {
|
|
90
|
+
readonly type: "generation-progress";
|
|
91
|
+
readonly id: string;
|
|
92
|
+
readonly progress?: number | undefined;
|
|
102
93
|
} | {
|
|
103
94
|
readonly type: "finish";
|
|
104
95
|
readonly audio: import("../media.js").Asset;
|
|
105
96
|
readonly usage?: {
|
|
106
|
-
readonly type: "tokens";
|
|
107
|
-
readonly input?: number | undefined;
|
|
108
|
-
readonly output?: number | undefined;
|
|
109
|
-
readonly total?: number | undefined;
|
|
110
|
-
readonly details?: {
|
|
111
|
-
readonly [x: string]: unknown;
|
|
112
|
-
} | undefined;
|
|
113
|
-
} | {
|
|
114
97
|
readonly type: "seconds";
|
|
115
98
|
readonly seconds: number;
|
|
116
99
|
} | {
|
|
@@ -122,16 +105,15 @@ export declare const CartesiaSpeech: {
|
|
|
122
105
|
} | {
|
|
123
106
|
readonly type: "compute";
|
|
124
107
|
readonly seconds: number;
|
|
125
|
-
} |
|
|
126
|
-
|
|
127
|
-
readonly
|
|
128
|
-
readonly
|
|
129
|
-
readonly
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
};
|
|
108
|
+
} | {
|
|
109
|
+
readonly type: "tokens";
|
|
110
|
+
readonly input?: number | undefined;
|
|
111
|
+
readonly output?: number | undefined;
|
|
112
|
+
readonly total?: number | undefined;
|
|
113
|
+
readonly details?: {
|
|
114
|
+
readonly [x: string]: unknown;
|
|
133
115
|
} | undefined;
|
|
134
|
-
}
|
|
116
|
+
} | undefined;
|
|
135
117
|
readonly providerMetadata?: {
|
|
136
118
|
readonly [x: string]: {
|
|
137
119
|
readonly [x: string]: unknown;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DecisionRoute } from "../route/decision.js";
|
|
2
|
+
/** Posts `{ model, input }` to Cloudflare's `/ai/run` with the System One body as `input`; media defaults to Clef's top-level lists. */
|
|
3
|
+
export declare const protocol: DecisionRoute.Protocol;
|
|
4
|
+
/** Providers set the Run API base, `.../client/v4/accounts/{account}/ai/run`, with `route.with(...)`. */
|
|
5
|
+
export declare const route: DecisionRoute.Route;
|
|
6
|
+
export declare const CloudflareRunDecisions: {
|
|
7
|
+
readonly protocol: DecisionRoute.Protocol;
|
|
8
|
+
readonly route: DecisionRoute.Route;
|
|
9
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { DecisionRoute } from "../route/decision.js";
|
|
3
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
4
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
|
+
import { SystemOne } from "./system-one.js";
|
|
6
|
+
const ID = "cloudflare-run-decisions";
|
|
7
|
+
const NAME = "Cloudflare Run";
|
|
8
|
+
// Workers AI models answer with the System One response; third-party models such as TypeSafe Jev wrap it in a
|
|
9
|
+
// completed run record.
|
|
10
|
+
const Envelope = Schema.Struct({
|
|
11
|
+
result: Schema.Union([
|
|
12
|
+
SystemOne.Response,
|
|
13
|
+
Schema.Struct({ state: Schema.Literal("Completed"), result: SystemOne.Response }),
|
|
14
|
+
]),
|
|
15
|
+
});
|
|
16
|
+
const decodeEnvelope = MediaProtocol.identity({ id: ID, name: NAME, provider: "cloudflare-workers-ai" }).decodeJson(Envelope);
|
|
17
|
+
/** Posts `{ model, input }` to Cloudflare's `/ai/run` with the System One body as `input`; media defaults to Clef's top-level lists. */
|
|
18
|
+
export const protocol = DecisionRoute.protocol({
|
|
19
|
+
id: ID,
|
|
20
|
+
name: NAME,
|
|
21
|
+
inlineMedia: true,
|
|
22
|
+
body: {
|
|
23
|
+
from: (request) => SystemOne.encodeBody(request, "root").pipe(Effect.map((input) => ({ model: request.model.id, input }))),
|
|
24
|
+
},
|
|
25
|
+
response: {
|
|
26
|
+
decode: (response, context) => decodeEnvelope(response).pipe(Effect.flatMap((output) => SystemOne.toResponse("answers" in output.value.result ? output.value.result : output.value.result.result, context.request, context.providerMetadataKey, output.invalid))),
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
/** Providers set the Run API base, `.../client/v4/accounts/{account}/ai/run`, with `route.with(...)`. */
|
|
30
|
+
export const route = DecisionRoute.make({
|
|
31
|
+
id: ID,
|
|
32
|
+
provider: "cloudflare-workers-ai",
|
|
33
|
+
protocol,
|
|
34
|
+
endpoint: Endpoint.path(""),
|
|
35
|
+
});
|
|
36
|
+
export const CloudflareRunDecisions = { protocol, route };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import type { MediaRoute } from "../route/media.js";
|
|
4
|
+
/**
|
|
5
|
+
* Native Run API inputs. Step counts are `steps` on FLUX and Lucid Origin and `num_steps` on the other models;
|
|
6
|
+
* `aspect` and `resolution` lower to `width` and `height`.
|
|
7
|
+
*/
|
|
8
|
+
export type CloudflareWorkersAIImageOptions = {
|
|
9
|
+
readonly width?: number;
|
|
10
|
+
readonly height?: number;
|
|
11
|
+
readonly seed?: number;
|
|
12
|
+
readonly steps?: number;
|
|
13
|
+
readonly num_steps?: number;
|
|
14
|
+
readonly guidance?: number;
|
|
15
|
+
readonly negative_prompt?: string;
|
|
16
|
+
} & Record<string, unknown>;
|
|
17
|
+
export type Request = ImageRequestFor<CloudflareWorkersAIImageOptions>;
|
|
18
|
+
export declare const protocol: MediaProtocol.Inline<Request, ImageResponse>;
|
|
19
|
+
export declare const model: (input: MediaRoute.ModelInput) => ImageModel<CloudflareWorkersAIImageOptions>;
|
|
20
|
+
export declare const CloudflareWorkersAIImages: {
|
|
21
|
+
readonly protocol: MediaProtocol.Inline<Request, ImageResponse>;
|
|
22
|
+
readonly model: typeof model;
|
|
23
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ImageModel, ImageResponse } from "../image.js";
|
|
3
|
+
import { Media } from "../media.js";
|
|
4
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
|
+
import { mergeJsonRecords } from "../schema/index.js";
|
|
6
|
+
import { ProviderShared } from "./shared.js";
|
|
7
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
8
|
+
import { MediaInput } from "./utils/media-input.js";
|
|
9
|
+
const route = MediaProtocol.identity({
|
|
10
|
+
id: "cloudflare-workers-ai-images",
|
|
11
|
+
name: "Cloudflare Workers AI",
|
|
12
|
+
provider: "cloudflare-workers-ai",
|
|
13
|
+
});
|
|
14
|
+
/** FLUX.2 models only accept multipart form fields, even for a prompt alone. */
|
|
15
|
+
const MULTIPART_MODEL_PREFIX = "@cf/black-forest-labs/flux-2-";
|
|
16
|
+
const RESERVED_FORM_FIELDS = new Set(["prompt"]);
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// 2. Response schema
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
const RunResponse = Schema.Struct({
|
|
21
|
+
result: Schema.Struct({
|
|
22
|
+
image: Schema.optional(Schema.String),
|
|
23
|
+
usage: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// 5. Request body construction
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
const fromRequest = Effect.fn("CloudflareWorkersAIImages.fromRequest")(function* (request) {
|
|
30
|
+
if (request.n !== undefined && request.n > 1)
|
|
31
|
+
return yield* route.unsupported("media.n", `${route.name} generates one image per request; call it once per image`);
|
|
32
|
+
const fields = {
|
|
33
|
+
prompt: request.prompt,
|
|
34
|
+
...(yield* ImageSizing.exact(route, request, { multiple: 16, minEdge: 256, maxEdge: 2048 })),
|
|
35
|
+
};
|
|
36
|
+
const native = mergeJsonRecords(request.providerOptions, request.http?.body);
|
|
37
|
+
const images = request.images ?? [];
|
|
38
|
+
if (!request.model.id.startsWith(MULTIPART_MODEL_PREFIX)) {
|
|
39
|
+
if (images.length > 1)
|
|
40
|
+
return yield* route.unsupported("media.images", `${request.model.id} takes one input image as image_b64`);
|
|
41
|
+
const image = images[0]?.inline();
|
|
42
|
+
if (images[0] !== undefined && image === undefined)
|
|
43
|
+
return yield* ProviderShared.inlineRequired(route.id, images[0]);
|
|
44
|
+
return MediaProtocol.json(mergeJsonRecords({ ...fields, image_b64: image?.base64 }, native) ?? {});
|
|
45
|
+
}
|
|
46
|
+
const body = new FormData();
|
|
47
|
+
MediaInput.appendFields(body, fields, { overlay: native, reserved: RESERVED_FORM_FIELDS });
|
|
48
|
+
yield* Effect.forEach(images, (image, index) => MediaInput.inlineBytes(route.id, image).pipe(Effect.map((bytes) => body.append(`input_image_${index}`, MediaInput.blob(bytes, image.mediaType), `image_${index}`))));
|
|
49
|
+
return MediaProtocol.multipart(body);
|
|
50
|
+
});
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// 6. Response decoding
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
const decodeDocument = route.decodeJson(RunResponse);
|
|
55
|
+
// FLUX and Lucid Origin wrap base64 in the Run API envelope; the Stable Diffusion family, DreamShaper, and Phoenix
|
|
56
|
+
// answer with the raw image, whose declared content type does not always match the bytes.
|
|
57
|
+
const decodeResponse = Effect.fn("CloudflareWorkersAIImages.decodeResponse")(function* (response) {
|
|
58
|
+
if (!response.headers["content-type"]?.startsWith("application/json"))
|
|
59
|
+
return new ImageResponse({ images: [Media.bytes(yield* route.bytes(response))] });
|
|
60
|
+
const output = yield* decodeDocument(response);
|
|
61
|
+
const result = output.value.result;
|
|
62
|
+
if (result.image === undefined)
|
|
63
|
+
return yield* output.invalid(`${route.name} returned no image`);
|
|
64
|
+
return new ImageResponse({
|
|
65
|
+
images: [yield* MediaInput.decodedAsset(output.invalid, `${route.name} result`, result.image, undefined)],
|
|
66
|
+
providerMetadata: result.usage === undefined ? undefined : { "cloudflare-workers-ai": { usage: result.usage } },
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// 7. Protocol and route
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
export const protocol = MediaProtocol.inline(route, {
|
|
73
|
+
body: { from: fromRequest },
|
|
74
|
+
response: { decode: decodeResponse },
|
|
75
|
+
});
|
|
76
|
+
export const model = (input) => ImageModel.fromRoute({ protocol, path: ({ request }) => `/${request.model.id}` }, input);
|
|
77
|
+
export const CloudflareWorkersAIImages = {
|
|
78
|
+
protocol,
|
|
79
|
+
model,
|
|
80
|
+
};
|
|
@@ -11,11 +11,6 @@ declare const Options: Schema.Struct<{
|
|
|
11
11
|
}>>;
|
|
12
12
|
}>;
|
|
13
13
|
export type ProviderOptionsInput = Schema.Schema.Type<typeof Options>;
|
|
14
|
-
type State = {
|
|
15
|
-
readonly lifecycle: Lifecycle.State;
|
|
16
|
-
readonly tools: ToolStream.State<number>;
|
|
17
|
-
readonly finished: boolean;
|
|
18
|
-
};
|
|
19
14
|
export declare const protocol: Protocol<{
|
|
20
15
|
readonly model: string;
|
|
21
16
|
readonly messages: readonly {
|
|
@@ -68,6 +63,8 @@ export declare const protocol: Protocol<{
|
|
|
68
63
|
readonly frequency_penalty?: number | undefined;
|
|
69
64
|
readonly presence_penalty?: number | undefined;
|
|
70
65
|
}, string, {
|
|
66
|
+
readonly type: "unknown";
|
|
67
|
+
} | {
|
|
71
68
|
readonly type: "message-start";
|
|
72
69
|
} | {
|
|
73
70
|
readonly type: "content-delta" | "content-start";
|
|
@@ -127,9 +124,11 @@ export declare const protocol: Protocol<{
|
|
|
127
124
|
};
|
|
128
125
|
} | {
|
|
129
126
|
readonly type: "citation-end" | "citation-start";
|
|
130
|
-
}
|
|
131
|
-
readonly
|
|
132
|
-
|
|
127
|
+
}, {
|
|
128
|
+
readonly lifecycle: Lifecycle.State;
|
|
129
|
+
readonly finished: boolean;
|
|
130
|
+
readonly tools: Partial<Record<number, ToolStream.PendingTool>>;
|
|
131
|
+
}>;
|
|
133
132
|
export declare const route: Route<{
|
|
134
133
|
readonly model: string;
|
|
135
134
|
readonly messages: readonly {
|