@opencode/ai 2.1.4 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +339 -166
- 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 +50 -14
- package/dist/image-client.js +0 -1
- package/dist/image.d.ts +6 -176
- package/dist/image.js +4 -11
- package/dist/index.d.ts +1444 -34
- package/dist/index.js +139 -24
- package/dist/llm.d.ts +16 -1
- package/dist/llm.js +34 -0
- package/dist/media-client.d.ts +3 -1
- package/dist/media-client.js +1 -0
- 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 +59 -4
- package/dist/protocols/alibaba-images.d.ts +22 -0
- package/dist/protocols/alibaba-images.js +77 -0
- package/dist/protocols/alibaba-messages.d.ts +52 -17
- package/dist/protocols/alibaba-responses.d.ts +26 -6
- package/dist/protocols/anthropic-messages.d.ts +160 -54
- package/dist/protocols/anthropic-messages.js +44 -13
- package/dist/protocols/bedrock-converse.d.ts +110 -15
- package/dist/protocols/bedrock-converse.js +112 -15
- package/dist/protocols/bedrock-event-stream.js +1 -0
- package/dist/protocols/cartesia-speech.d.ts +0 -18
- 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/deepgram-speech.d.ts +0 -18
- package/dist/protocols/elevenlabs-speech.d.ts +0 -18
- package/dist/protocols/gemini.d.ts +3 -0
- package/dist/protocols/gemini.js +11 -6
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-images.js +10 -37
- package/dist/protocols/google-interactions.d.ts +4 -0
- package/dist/protocols/google-interactions.js +12 -5
- package/dist/protocols/google-speech.d.ts +0 -18
- package/dist/protocols/google-speech.js +0 -1
- package/dist/protocols/google-transcription.d.ts +0 -18
- 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 +52 -17
- package/dist/protocols/meta-messages.js +14 -1
- package/dist/protocols/meta-responses.d.ts +46 -6
- package/dist/protocols/meta-responses.js +29 -1
- package/dist/protocols/minimax-images.d.ts +25 -0
- package/dist/protocols/minimax-images.js +150 -0
- package/dist/protocols/open-responses.d.ts +212 -27
- package/dist/protocols/open-responses.js +116 -54
- package/dist/protocols/openai-chat.d.ts +1135 -31
- package/dist/protocols/openai-chat.js +155 -60
- package/dist/protocols/openai-compatible-chat.d.ts +57 -5
- package/dist/protocols/openai-compatible-responses.d.ts +28 -8
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +128 -0
- package/dist/protocols/openai-freeform.d.ts +47 -0
- package/dist/protocols/openai-freeform.js +54 -0
- package/dist/protocols/openai-images.d.ts +36 -24
- package/dist/protocols/openai-images.js +137 -39
- package/dist/protocols/openai-responses.d.ts +202 -42
- package/dist/protocols/openai-responses.js +49 -10
- package/dist/protocols/openai-speech.d.ts +0 -18
- package/dist/protocols/openai-speech.js +15 -5
- package/dist/protocols/openai-transcription.d.ts +0 -18
- package/dist/protocols/openai-transcription.js +3 -4
- 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/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/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 +9 -0
- package/dist/protocols/venice-chat.d.ts +58 -4
- package/dist/protocols/venice-chat.js +2 -2
- package/dist/protocols/xai-images.d.ts +7 -2
- package/dist/protocols/xai-images.js +11 -5
- package/dist/protocols/xai-responses.d.ts +30 -3
- package/dist/protocols/xai-responses.js +30 -3
- package/dist/protocols/xai-video.d.ts +1 -1
- package/dist/protocols/xai-video.js +11 -8
- package/dist/protocols/zai-chat.d.ts +60 -4
- package/dist/protocols/zai-images.d.ts +3 -1
- package/dist/protocols/zai-images.js +12 -11
- package/dist/protocols/zai-messages.d.ts +52 -17
- package/dist/provider-error.js +23 -7
- package/dist/provider-package.d.ts +2 -0
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba.d.ts +137 -28
- package/dist/providers/alibaba.js +16 -8
- package/dist/providers/amazon-bedrock-mantle.d.ts +259 -145
- package/dist/providers/amazon-bedrock-mantle.js +1 -1
- package/dist/providers/amazon-bedrock.d.ts +34 -14
- package/dist/providers/amazon-bedrock.js +11 -11
- package/dist/providers/anthropic-compatible.d.ts +53 -18
- package/dist/providers/anthropic.d.ts +53 -18
- package/dist/providers/anthropic.js +1 -1
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure.d.ts +129 -24
- 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 +124 -12
- package/dist/providers/baseten.js +41 -8
- package/dist/providers/cerebras.d.ts +115 -11
- package/dist/providers/cerebras.js +1 -1
- 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 +290 -60
- package/dist/providers/cloudflare-ai-gateway.js +32 -4
- 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 +128 -12
- package/dist/providers/cloudflare-workers-ai.js +45 -9
- package/dist/providers/cohere/chat.d.ts +1 -1
- package/dist/providers/cohere/chat.js +1 -1
- package/dist/providers/cohere.d.ts +115 -11
- package/dist/providers/cohere.js +1 -1
- package/dist/providers/deepinfra.d.ts +115 -11
- package/dist/providers/deepinfra.js +1 -1
- package/dist/providers/deepseek.d.ts +115 -11
- package/dist/providers/deepseek.js +1 -1
- 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/responses.d.ts +4 -0
- package/dist/providers/digitalocean/responses.js +1 -0
- package/dist/providers/digitalocean.d.ts +390 -20
- package/dist/providers/digitalocean.js +53 -11
- package/dist/providers/fireworks.d.ts +115 -11
- package/dist/providers/fireworks.js +1 -1
- package/dist/providers/google/images.d.ts +2 -0
- package/dist/providers/google/images.js +1 -0
- package/dist/providers/google/interactions.d.ts +1 -1
- package/dist/providers/google/interactions.js +1 -1
- 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 +57 -5
- package/dist/providers/google-vertex-chat.js +1 -1
- package/dist/providers/google-vertex-interactions.d.ts +1 -1
- package/dist/providers/google-vertex-interactions.js +1 -1
- package/dist/providers/google-vertex-messages.d.ts +53 -18
- package/dist/providers/google-vertex-messages.js +1 -1
- package/dist/providers/google-vertex-mistral.d.ts +1 -1
- package/dist/providers/google-vertex-mistral.js +1 -1
- package/dist/providers/google-vertex-responses.d.ts +24 -4
- package/dist/providers/google-vertex-responses.js +1 -1
- package/dist/providers/google-vertex.d.ts +4 -2
- package/dist/providers/google-vertex.js +30 -13
- package/dist/providers/google.d.ts +6 -5
- package/dist/providers/google.js +15 -8
- package/dist/providers/groq.d.ts +119 -15
- package/dist/providers/groq.js +1 -7
- package/dist/providers/index.d.ts +0 -4
- package/dist/providers/index.js +0 -4
- package/dist/providers/meta/images.d.ts +2 -0
- package/dist/providers/meta/images.js +1 -0
- package/dist/providers/meta.d.ts +254 -146
- package/dist/providers/meta.js +2 -1
- package/dist/providers/minimax/chat.d.ts +1 -1
- package/dist/providers/minimax/chat.js +1 -1
- package/dist/providers/minimax/images.d.ts +3 -0
- package/dist/providers/minimax/images.js +1 -0
- package/dist/providers/minimax/responses.d.ts +1 -1
- package/dist/providers/minimax/responses.js +1 -1
- package/dist/providers/minimax.d.ts +139 -26
- package/dist/providers/minimax.js +22 -9
- package/dist/providers/mistral.d.ts +3 -3
- package/dist/providers/mistral.js +1 -1
- package/dist/providers/moonshot/messages.d.ts +1 -1
- package/dist/providers/moonshot/messages.js +1 -1
- package/dist/providers/moonshot/responses.d.ts +1 -1
- package/dist/providers/moonshot/responses.js +1 -1
- package/dist/providers/moonshot.d.ts +132 -25
- package/dist/providers/moonshot.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 +24 -4
- package/dist/providers/openai-compatible.d.ts +62 -8
- package/dist/providers/openai-compatible.js +29 -10
- package/dist/providers/openai-options.d.ts +3 -1
- package/dist/providers/openai.d.ts +103 -18
- package/dist/providers/openai.js +23 -5
- package/dist/providers/opencode/decisions.d.ts +2 -0
- package/dist/providers/opencode/decisions.js +1 -0
- package/dist/providers/opencode.d.ts +11 -12
- 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/responses.d.ts +4 -0
- package/dist/providers/openrouter/responses.js +1 -0
- package/dist/providers/openrouter.d.ts +446 -35
- package/dist/providers/openrouter.js +78 -27
- package/dist/providers/togetherai.d.ts +115 -11
- package/dist/providers/togetherai.js +1 -1
- 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 +11 -12
- package/dist/providers/typesafe-ai.js +10 -8
- package/dist/providers/venice/responses.d.ts +4 -0
- package/dist/providers/venice/responses.js +1 -0
- package/dist/providers/venice.d.ts +261 -9
- package/dist/providers/venice.js +33 -17
- 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 +16 -22
- package/dist/providers/vercel-ai-gateway.js +41 -85
- package/dist/providers/xai/images.d.ts +2 -0
- package/dist/providers/xai/images.js +1 -0
- package/dist/providers/xai.d.ts +66 -12
- 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/messages.d.ts +1 -1
- package/dist/providers/zai-coding-plan/messages.js +1 -1
- package/dist/providers/zai-coding-plan/responses.d.ts +1 -1
- package/dist/providers/zai-coding-plan/responses.js +1 -1
- package/dist/providers/zai-coding-plan.d.ts +132 -25
- package/dist/providers/zai-coding-plan.js +1 -1
- package/dist/providers/zai.d.ts +62 -9
- package/dist/providers/zai.js +15 -8
- package/dist/route/auth.d.ts +1 -1
- package/dist/route/client.d.ts +2 -2
- package/dist/route/client.js +26 -4
- package/dist/route/decision.d.ts +58 -0
- package/dist/route/decision.js +75 -0
- 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 +3 -4
- package/dist/route/media-protocol.js +12 -8
- package/dist/route/media.d.ts +6 -2
- package/dist/route/media.js +8 -6
- 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 +41 -3
- package/dist/schema/messages.js +23 -0
- package/dist/schema/options.d.ts +5 -3
- package/dist/schema/options.js +8 -3
- package/dist/speech-client.d.ts +53 -14
- package/dist/speech-client.js +0 -1
- package/dist/speech.d.ts +0 -135
- package/dist/speech.js +2 -4
- package/dist/testing/decision.d.ts +16 -0
- package/dist/testing/decision.js +20 -0
- package/dist/testing/image.d.ts +14 -0
- package/dist/testing/image.js +12 -0
- package/dist/testing/media.d.ts +18 -0
- package/dist/testing/media.js +36 -0
- package/dist/testing/script.d.ts +28 -0
- package/dist/testing/script.js +60 -0
- package/dist/testing/speech.d.ts +14 -0
- package/dist/testing/speech.js +12 -0
- package/dist/testing/transcription.d.ts +14 -0
- package/dist/testing/transcription.js +12 -0
- package/dist/testing/video.d.ts +14 -0
- package/dist/testing/video.js +12 -0
- package/dist/testing.d.ts +35 -45
- package/dist/testing.js +14 -75
- package/dist/tool.d.ts +12 -1
- package/dist/tool.js +10 -2
- package/dist/transcription-client.d.ts +69 -14
- package/dist/transcription.d.ts +0 -135
- package/dist/transcription.js +1 -1
- package/dist/utils/media-type.js +22 -1
- package/dist/video-client.d.ts +46 -14
- package/dist/video-client.js +0 -1
- package/dist/video.d.ts +1 -136
- package/dist/video.js +3 -5
- package/package.json +497 -11
- 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 -108
- 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/protocols/bfl-images.d.ts +0 -38
- package/dist/protocols/bfl-images.js +0 -153
- package/dist/protocols/fal-images.d.ts +0 -25
- package/dist/protocols/fal-images.js +0 -104
- package/dist/protocols/fal-video.d.ts +0 -29
- package/dist/protocols/fal-video.js +0 -73
- package/dist/protocols/replicate-images.d.ts +0 -28
- package/dist/protocols/replicate-images.js +0 -127
- package/dist/protocols/stability-images.d.ts +0 -39
- package/dist/protocols/stability-images.js +0 -136
- package/dist/protocols/utils/fal-queue.d.ts +0 -26
- package/dist/protocols/utils/fal-queue.js +0 -67
- package/dist/provider.d.ts +0 -23
- package/dist/provider.js +0 -2
- package/dist/providers/black-forest-labs.d.ts +0 -21
- package/dist/providers/black-forest-labs.js +0 -23
- package/dist/providers/fal.d.ts +0 -24
- package/dist/providers/fal.js +0 -26
- package/dist/providers/replicate.d.ts +0 -21
- package/dist/providers/replicate.js +0 -17
- package/dist/providers/stability.d.ts +0 -23
- package/dist/providers/stability.js +0 -18
|
@@ -3,8 +3,9 @@ import { ImageModel, ImageResponse } from "../image.js";
|
|
|
3
3
|
import { Media } from "../media.js";
|
|
4
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
6
|
+
import { mergeJsonRecords, tokenUsage } from "../schema/index.js";
|
|
7
7
|
import { JsonObject, ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
8
9
|
import { MediaInput } from "./utils/media-input.js";
|
|
9
10
|
const route = MediaProtocol.identity({ id: "meta-images", name: "Meta Images", provider: "meta" });
|
|
10
11
|
export const DEFAULT_BASE_URL = "https://api.meta.ai/v1";
|
|
@@ -40,14 +41,17 @@ const isEdit = (request) => (request.images?.length ?? 0) > 0;
|
|
|
40
41
|
const reference = (asset) => ProviderShared.mediaReference(asset, undefined, route.name).pipe(Effect.map((item) => ({ image_url: item.value })));
|
|
41
42
|
const fromRequest = Effect.fn("MetaImages.fromRequest")(function* (request) {
|
|
42
43
|
const images = yield* Effect.forEach(request.images ?? [], reference);
|
|
43
|
-
const { responseFormat, reasoningStrength, toolEnablement, ...native } = request.providerOptions ?? {};
|
|
44
|
+
const { outputFormat, responseFormat, reasoningStrength, toolEnablement, ...native } = request.providerOptions ?? {};
|
|
45
|
+
if (native.size !== undefined && request.aspect !== undefined)
|
|
46
|
+
return yield* ProviderShared.invalidRequest("Meta providerOptions.size replaces aspect; set one or the other");
|
|
47
|
+
const hint = request.aspect === undefined ? undefined : ImageSizing.fit({ aspect: request.aspect }, { multiple: 16 });
|
|
44
48
|
const payload = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(Body))(mergeJsonRecords({
|
|
45
49
|
model: request.model.id,
|
|
46
50
|
prompt: request.prompt,
|
|
47
51
|
images: images.length === 0 ? undefined : images,
|
|
48
52
|
n: request.n,
|
|
49
|
-
size:
|
|
50
|
-
output_format:
|
|
53
|
+
size: hint === undefined ? undefined : `${hint.width}x${hint.height}`,
|
|
54
|
+
output_format: outputFormat,
|
|
51
55
|
response_format: responseFormat,
|
|
52
56
|
reasoning_strength: reasoningStrength,
|
|
53
57
|
tool_enablement: toolEnablement,
|
|
@@ -73,13 +77,12 @@ const decodeResponse = Effect.fn("MetaImages.decodeResponse")(function* (respons
|
|
|
73
77
|
images,
|
|
74
78
|
usage: decoded.usage === undefined
|
|
75
79
|
? undefined
|
|
76
|
-
: {
|
|
77
|
-
type: "tokens",
|
|
80
|
+
: tokenUsage({
|
|
78
81
|
input: decoded.usage.input_tokens,
|
|
79
82
|
output: decoded.usage.output_tokens,
|
|
80
83
|
total: decoded.usage.total_tokens,
|
|
81
84
|
details: { meta: decoded.usage },
|
|
82
|
-
},
|
|
85
|
+
}),
|
|
83
86
|
providerMetadata: { meta: { outputFormat: format } },
|
|
84
87
|
});
|
|
85
88
|
});
|
|
@@ -87,7 +90,6 @@ const decodeResponse = Effect.fn("MetaImages.decodeResponse")(function* (respons
|
|
|
87
90
|
// 7. Protocol and route
|
|
88
91
|
// ---------------------------------------------------------------------------
|
|
89
92
|
export const protocol = MediaProtocol.inline(route, {
|
|
90
|
-
unsupported: ["mask", "aspectRatio", "seed"],
|
|
91
93
|
body: { from: fromRequest },
|
|
92
94
|
response: { decode: decodeResponse },
|
|
93
95
|
});
|
|
@@ -17,7 +17,7 @@ export declare const protocol: Protocol<{
|
|
|
17
17
|
readonly text: string;
|
|
18
18
|
readonly cache_control?: {
|
|
19
19
|
readonly type: "ephemeral";
|
|
20
|
-
readonly ttl?:
|
|
20
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
21
21
|
} | undefined;
|
|
22
22
|
}[] | undefined;
|
|
23
23
|
readonly messages: readonly ({
|
|
@@ -27,7 +27,7 @@ export declare const protocol: Protocol<{
|
|
|
27
27
|
readonly text: string;
|
|
28
28
|
readonly cache_control?: {
|
|
29
29
|
readonly type: "ephemeral";
|
|
30
|
-
readonly ttl?:
|
|
30
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
31
31
|
} | undefined;
|
|
32
32
|
} | {
|
|
33
33
|
readonly type: "image";
|
|
@@ -44,11 +44,28 @@ export declare const protocol: Protocol<{
|
|
|
44
44
|
};
|
|
45
45
|
readonly cache_control?: {
|
|
46
46
|
readonly type: "ephemeral";
|
|
47
|
-
readonly ttl?:
|
|
47
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
48
48
|
} | undefined;
|
|
49
49
|
readonly transformations?: {
|
|
50
50
|
readonly oversized_image?: "downsize" | "error" | undefined;
|
|
51
51
|
} | undefined;
|
|
52
|
+
} | {
|
|
53
|
+
readonly type: "video";
|
|
54
|
+
readonly source: {
|
|
55
|
+
readonly type: "base64";
|
|
56
|
+
readonly media_type: string;
|
|
57
|
+
readonly data: string;
|
|
58
|
+
} | {
|
|
59
|
+
readonly type: "url";
|
|
60
|
+
readonly url: string;
|
|
61
|
+
} | {
|
|
62
|
+
readonly type: "file";
|
|
63
|
+
readonly file_id: string;
|
|
64
|
+
};
|
|
65
|
+
readonly cache_control?: {
|
|
66
|
+
readonly type: "ephemeral";
|
|
67
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
68
|
+
} | undefined;
|
|
52
69
|
} | {
|
|
53
70
|
readonly type: "document";
|
|
54
71
|
readonly source: {
|
|
@@ -68,7 +85,7 @@ export declare const protocol: Protocol<{
|
|
|
68
85
|
};
|
|
69
86
|
readonly cache_control?: {
|
|
70
87
|
readonly type: "ephemeral";
|
|
71
|
-
readonly ttl?:
|
|
88
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
72
89
|
} | undefined;
|
|
73
90
|
readonly title?: string | undefined;
|
|
74
91
|
readonly context?: string | undefined;
|
|
@@ -83,7 +100,7 @@ export declare const protocol: Protocol<{
|
|
|
83
100
|
readonly text: string;
|
|
84
101
|
readonly cache_control?: {
|
|
85
102
|
readonly type: "ephemeral";
|
|
86
|
-
readonly ttl?:
|
|
103
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
87
104
|
} | undefined;
|
|
88
105
|
} | {
|
|
89
106
|
readonly type: "image";
|
|
@@ -100,11 +117,28 @@ export declare const protocol: Protocol<{
|
|
|
100
117
|
};
|
|
101
118
|
readonly cache_control?: {
|
|
102
119
|
readonly type: "ephemeral";
|
|
103
|
-
readonly ttl?:
|
|
120
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
104
121
|
} | undefined;
|
|
105
122
|
readonly transformations?: {
|
|
106
123
|
readonly oversized_image?: "downsize" | "error" | undefined;
|
|
107
124
|
} | undefined;
|
|
125
|
+
} | {
|
|
126
|
+
readonly type: "video";
|
|
127
|
+
readonly source: {
|
|
128
|
+
readonly type: "base64";
|
|
129
|
+
readonly media_type: string;
|
|
130
|
+
readonly data: string;
|
|
131
|
+
} | {
|
|
132
|
+
readonly type: "url";
|
|
133
|
+
readonly url: string;
|
|
134
|
+
} | {
|
|
135
|
+
readonly type: "file";
|
|
136
|
+
readonly file_id: string;
|
|
137
|
+
};
|
|
138
|
+
readonly cache_control?: {
|
|
139
|
+
readonly type: "ephemeral";
|
|
140
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
141
|
+
} | undefined;
|
|
108
142
|
} | {
|
|
109
143
|
readonly type: "document";
|
|
110
144
|
readonly source: {
|
|
@@ -124,7 +158,7 @@ export declare const protocol: Protocol<{
|
|
|
124
158
|
};
|
|
125
159
|
readonly cache_control?: {
|
|
126
160
|
readonly type: "ephemeral";
|
|
127
|
-
readonly ttl?:
|
|
161
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
128
162
|
} | undefined;
|
|
129
163
|
readonly title?: string | undefined;
|
|
130
164
|
readonly context?: string | undefined;
|
|
@@ -135,7 +169,7 @@ export declare const protocol: Protocol<{
|
|
|
135
169
|
readonly is_error?: boolean | undefined;
|
|
136
170
|
readonly cache_control?: {
|
|
137
171
|
readonly type: "ephemeral";
|
|
138
|
-
readonly ttl?:
|
|
172
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
139
173
|
} | undefined;
|
|
140
174
|
})[];
|
|
141
175
|
} | {
|
|
@@ -148,7 +182,7 @@ export declare const protocol: Protocol<{
|
|
|
148
182
|
readonly text: string;
|
|
149
183
|
readonly cache_control?: {
|
|
150
184
|
readonly type: "ephemeral";
|
|
151
|
-
readonly ttl?:
|
|
185
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
152
186
|
} | undefined;
|
|
153
187
|
} | {
|
|
154
188
|
readonly type: "thinking";
|
|
@@ -156,14 +190,14 @@ export declare const protocol: Protocol<{
|
|
|
156
190
|
readonly signature: string;
|
|
157
191
|
readonly cache_control?: {
|
|
158
192
|
readonly type: "ephemeral";
|
|
159
|
-
readonly ttl?:
|
|
193
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
160
194
|
} | undefined;
|
|
161
195
|
} | {
|
|
162
196
|
readonly type: "redacted_thinking";
|
|
163
197
|
readonly data: string;
|
|
164
198
|
readonly cache_control?: {
|
|
165
199
|
readonly type: "ephemeral";
|
|
166
|
-
readonly ttl?:
|
|
200
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
167
201
|
} | undefined;
|
|
168
202
|
} | {
|
|
169
203
|
readonly type: "tool_use";
|
|
@@ -172,7 +206,7 @@ export declare const protocol: Protocol<{
|
|
|
172
206
|
readonly input: unknown;
|
|
173
207
|
readonly cache_control?: {
|
|
174
208
|
readonly type: "ephemeral";
|
|
175
|
-
readonly ttl?:
|
|
209
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
176
210
|
} | undefined;
|
|
177
211
|
} | {
|
|
178
212
|
readonly type: "server_tool_use";
|
|
@@ -181,7 +215,7 @@ export declare const protocol: Protocol<{
|
|
|
181
215
|
readonly input: unknown;
|
|
182
216
|
readonly cache_control?: {
|
|
183
217
|
readonly type: "ephemeral";
|
|
184
|
-
readonly ttl?:
|
|
218
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
185
219
|
} | undefined;
|
|
186
220
|
} | {
|
|
187
221
|
readonly type: "code_execution_tool_result" | "web_fetch_tool_result" | "web_search_tool_result";
|
|
@@ -189,7 +223,7 @@ export declare const protocol: Protocol<{
|
|
|
189
223
|
readonly content: unknown;
|
|
190
224
|
readonly cache_control?: {
|
|
191
225
|
readonly type: "ephemeral";
|
|
192
|
-
readonly ttl?:
|
|
226
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
193
227
|
} | undefined;
|
|
194
228
|
})[];
|
|
195
229
|
} | {
|
|
@@ -199,7 +233,7 @@ export declare const protocol: Protocol<{
|
|
|
199
233
|
readonly text: string;
|
|
200
234
|
readonly cache_control?: {
|
|
201
235
|
readonly type: "ephemeral";
|
|
202
|
-
readonly ttl?:
|
|
236
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
203
237
|
} | undefined;
|
|
204
238
|
}[];
|
|
205
239
|
readonly output_config?: {
|
|
@@ -247,7 +281,7 @@ export declare const protocol: Protocol<{
|
|
|
247
281
|
} | undefined;
|
|
248
282
|
readonly cache_control?: {
|
|
249
283
|
readonly type: "ephemeral";
|
|
250
|
-
readonly ttl?:
|
|
284
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
251
285
|
} | undefined;
|
|
252
286
|
readonly container?: string | {
|
|
253
287
|
readonly id?: string | null | undefined;
|
|
@@ -260,6 +294,7 @@ export declare const protocol: Protocol<{
|
|
|
260
294
|
readonly user_id?: string | null | undefined;
|
|
261
295
|
} | undefined;
|
|
262
296
|
readonly service_tier?: import("../schema/options.js").OpenString<"auto" | "standard_only"> | undefined;
|
|
297
|
+
readonly speed?: import("../schema/options.js").OpenString<"fast" | "standard"> | undefined;
|
|
263
298
|
readonly tools?: readonly ({
|
|
264
299
|
readonly type: "web_search";
|
|
265
300
|
readonly name: "web_search";
|
|
@@ -278,7 +313,7 @@ export declare const protocol: Protocol<{
|
|
|
278
313
|
};
|
|
279
314
|
readonly cache_control?: {
|
|
280
315
|
readonly type: "ephemeral";
|
|
281
|
-
readonly ttl?:
|
|
316
|
+
readonly ttl?: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
282
317
|
} | undefined;
|
|
283
318
|
})[] | undefined;
|
|
284
319
|
}, string, {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import { Protocol } from "../route/protocol.js";
|
|
3
|
+
import { CacheTTL } from "../schema/index.js";
|
|
3
4
|
import { AnthropicMessages } from "./anthropic-messages.js";
|
|
4
5
|
import { MetaResponses } from "./meta-responses.js";
|
|
5
6
|
import { JsonObject, optionalArray, ProviderShared } from "./shared.js";
|
|
@@ -10,7 +11,7 @@ const WebSearch = Schema.Struct({
|
|
|
10
11
|
});
|
|
11
12
|
const MetaCacheControl = Schema.Struct({
|
|
12
13
|
type: Schema.tag("ephemeral"),
|
|
13
|
-
ttl: Schema.optional(
|
|
14
|
+
ttl: Schema.optional(CacheTTL),
|
|
14
15
|
});
|
|
15
16
|
const FunctionTool = Schema.Struct({
|
|
16
17
|
name: Schema.String,
|
|
@@ -27,6 +28,18 @@ const fromRequest = Effect.fn("MetaMessages.fromRequest")(function* (request) {
|
|
|
27
28
|
const body = yield* AnthropicMessages.protocol.body.from(projected.request);
|
|
28
29
|
return {
|
|
29
30
|
...body,
|
|
31
|
+
messages: body.messages.map((message) => {
|
|
32
|
+
if (message.role !== "user")
|
|
33
|
+
return message;
|
|
34
|
+
const videos = [];
|
|
35
|
+
const content = message.content.map((block) => {
|
|
36
|
+
if (block.type !== "tool_result" || typeof block.content === "string")
|
|
37
|
+
return block;
|
|
38
|
+
videos.push(...block.content.filter((item) => item.type === "video"));
|
|
39
|
+
return { ...block, content: block.content.filter((item) => item.type !== "video") };
|
|
40
|
+
});
|
|
41
|
+
return videos.length === 0 ? message : { ...message, content: [...content, ...videos] };
|
|
42
|
+
}),
|
|
30
43
|
tools: body.tools === undefined
|
|
31
44
|
? undefined
|
|
32
45
|
: yield* Effect.forEach(body.tools, (tool, index) => Effect.gen(function* () {
|
|
@@ -40,7 +40,7 @@ export declare const protocol: Protocol<{
|
|
|
40
40
|
readonly role: "developer";
|
|
41
41
|
readonly content: string;
|
|
42
42
|
} | {
|
|
43
|
-
readonly type: "function_call_output";
|
|
43
|
+
readonly type: "custom_tool_call_output" | "function_call_output";
|
|
44
44
|
readonly call_id: string;
|
|
45
45
|
readonly output: string | readonly ({
|
|
46
46
|
readonly type: "input_text";
|
|
@@ -77,6 +77,9 @@ export declare const protocol: Protocol<{
|
|
|
77
77
|
readonly content: readonly ({
|
|
78
78
|
readonly type: "input_text";
|
|
79
79
|
readonly text: string;
|
|
80
|
+
} | {
|
|
81
|
+
readonly type: "input_video";
|
|
82
|
+
readonly video_url: string;
|
|
80
83
|
} | {
|
|
81
84
|
readonly type: "input_image";
|
|
82
85
|
readonly image_url: string;
|
|
@@ -107,6 +110,12 @@ export declare const protocol: Protocol<{
|
|
|
107
110
|
readonly name: string;
|
|
108
111
|
readonly namespace?: string | undefined;
|
|
109
112
|
readonly arguments: string;
|
|
113
|
+
} | {
|
|
114
|
+
readonly type: "custom_tool_call";
|
|
115
|
+
readonly id?: string | undefined;
|
|
116
|
+
readonly call_id: string;
|
|
117
|
+
readonly name: string;
|
|
118
|
+
readonly input: string;
|
|
110
119
|
} | {
|
|
111
120
|
readonly [x: string]: unknown;
|
|
112
121
|
readonly type: "web_search_call";
|
|
@@ -151,6 +160,17 @@ export declare const protocol: Protocol<{
|
|
|
151
160
|
})[];
|
|
152
161
|
readonly instructions?: string | undefined;
|
|
153
162
|
readonly tools?: readonly ({
|
|
163
|
+
readonly type: "custom";
|
|
164
|
+
readonly name: string;
|
|
165
|
+
readonly description: string;
|
|
166
|
+
readonly format: {
|
|
167
|
+
readonly type: "text";
|
|
168
|
+
} | {
|
|
169
|
+
readonly type: "grammar";
|
|
170
|
+
readonly syntax: "lark" | "regex";
|
|
171
|
+
readonly definition: string;
|
|
172
|
+
};
|
|
173
|
+
} | {
|
|
154
174
|
readonly type: "function";
|
|
155
175
|
readonly name: string;
|
|
156
176
|
readonly description: string;
|
|
@@ -163,13 +183,13 @@ export declare const protocol: Protocol<{
|
|
|
163
183
|
readonly type: string;
|
|
164
184
|
})[] | undefined;
|
|
165
185
|
readonly tool_choice?: "auto" | "none" | "required" | {
|
|
166
|
-
readonly type: "function";
|
|
186
|
+
readonly type: "custom" | "function";
|
|
167
187
|
readonly name: string;
|
|
168
188
|
} | {
|
|
169
189
|
readonly type: "allowed_tools";
|
|
170
190
|
readonly mode: "auto" | "none" | "required";
|
|
171
191
|
readonly tools: readonly {
|
|
172
|
-
readonly type: "function";
|
|
192
|
+
readonly type: "custom" | "function";
|
|
173
193
|
readonly name: string;
|
|
174
194
|
}[];
|
|
175
195
|
} | undefined;
|
|
@@ -276,7 +296,7 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
276
296
|
readonly role: "developer";
|
|
277
297
|
readonly content: string;
|
|
278
298
|
} | {
|
|
279
|
-
readonly type: "function_call_output";
|
|
299
|
+
readonly type: "custom_tool_call_output" | "function_call_output";
|
|
280
300
|
readonly call_id: string;
|
|
281
301
|
readonly output: string | readonly ({
|
|
282
302
|
readonly type: "input_text";
|
|
@@ -313,6 +333,9 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
313
333
|
readonly content: readonly ({
|
|
314
334
|
readonly type: "input_text";
|
|
315
335
|
readonly text: string;
|
|
336
|
+
} | {
|
|
337
|
+
readonly type: "input_video";
|
|
338
|
+
readonly video_url: string;
|
|
316
339
|
} | {
|
|
317
340
|
readonly type: "input_image";
|
|
318
341
|
readonly image_url: string;
|
|
@@ -343,6 +366,12 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
343
366
|
readonly name: string;
|
|
344
367
|
readonly namespace?: string | undefined;
|
|
345
368
|
readonly arguments: string;
|
|
369
|
+
} | {
|
|
370
|
+
readonly type: "custom_tool_call";
|
|
371
|
+
readonly id?: string | undefined;
|
|
372
|
+
readonly call_id: string;
|
|
373
|
+
readonly name: string;
|
|
374
|
+
readonly input: string;
|
|
346
375
|
} | {
|
|
347
376
|
readonly [x: string]: unknown;
|
|
348
377
|
readonly type: "web_search_call";
|
|
@@ -387,6 +416,17 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
387
416
|
})[];
|
|
388
417
|
readonly instructions?: string | undefined;
|
|
389
418
|
readonly tools?: readonly ({
|
|
419
|
+
readonly type: "custom";
|
|
420
|
+
readonly name: string;
|
|
421
|
+
readonly description: string;
|
|
422
|
+
readonly format: {
|
|
423
|
+
readonly type: "text";
|
|
424
|
+
} | {
|
|
425
|
+
readonly type: "grammar";
|
|
426
|
+
readonly syntax: "lark" | "regex";
|
|
427
|
+
readonly definition: string;
|
|
428
|
+
};
|
|
429
|
+
} | {
|
|
390
430
|
readonly type: "function";
|
|
391
431
|
readonly name: string;
|
|
392
432
|
readonly description: string;
|
|
@@ -399,13 +439,13 @@ export declare const httpTransport: import("../route/transport/http.js").HttpJso
|
|
|
399
439
|
readonly type: string;
|
|
400
440
|
})[] | undefined;
|
|
401
441
|
readonly tool_choice?: "auto" | "none" | "required" | {
|
|
402
|
-
readonly type: "function";
|
|
442
|
+
readonly type: "custom" | "function";
|
|
403
443
|
readonly name: string;
|
|
404
444
|
} | {
|
|
405
445
|
readonly type: "allowed_tools";
|
|
406
446
|
readonly mode: "auto" | "none" | "required";
|
|
407
447
|
readonly tools: readonly {
|
|
408
|
-
readonly type: "function";
|
|
448
|
+
readonly type: "custom" | "function";
|
|
409
449
|
readonly name: string;
|
|
410
450
|
}[];
|
|
411
451
|
} | undefined;
|
|
@@ -48,7 +48,7 @@ const adapter = {
|
|
|
48
48
|
};
|
|
49
49
|
const fromRequest = Effect.fn("MetaResponses.fromRequest")(function* (request) {
|
|
50
50
|
const key = request.model.route.providerMetadataKey ?? String(request.model.provider);
|
|
51
|
-
|
|
51
|
+
const body = yield* OpenResponses.fromRequestWithAdapter(LLMRequest.update(request, {
|
|
52
52
|
messages: request.messages.map((message) => Message.make({
|
|
53
53
|
...message,
|
|
54
54
|
content: message.content.map((part) => {
|
|
@@ -69,7 +69,35 @@ const fromRequest = Effect.fn("MetaResponses.fromRequest")(function* (request) {
|
|
|
69
69
|
}),
|
|
70
70
|
})),
|
|
71
71
|
}), adapter);
|
|
72
|
+
// Meta cannot see video nested in tool results; attach it after the complete result batch.
|
|
73
|
+
const input = [];
|
|
74
|
+
const videos = [];
|
|
75
|
+
const flushVideos = () => {
|
|
76
|
+
if (videos.length > 0)
|
|
77
|
+
input.push({ type: "message", role: "user", content: videos.splice(0) });
|
|
78
|
+
};
|
|
79
|
+
for (const item of body.input) {
|
|
80
|
+
if (!isFunctionCallOutput(item)) {
|
|
81
|
+
flushVideos();
|
|
82
|
+
input.push(item);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (typeof item.output === "string") {
|
|
86
|
+
input.push(item);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const content = item.output.filter((part) => part.type !== "input_video");
|
|
90
|
+
videos.push(...item.output.filter((part) => part.type === "input_video"));
|
|
91
|
+
input.push({
|
|
92
|
+
...item,
|
|
93
|
+
output: content.length === 0 && item.output.length > 0 ? "Video attached in the following user message." : content,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
flushVideos();
|
|
97
|
+
return { ...body, input };
|
|
72
98
|
});
|
|
99
|
+
// Hosted replay items have an open `type`, so a plain tag check does not narrow the union.
|
|
100
|
+
const isFunctionCallOutput = (item) => item.type === "function_call_output" || item.type === "custom_tool_call_output";
|
|
73
101
|
const HOSTED_TOOLS = {
|
|
74
102
|
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
|
|
75
103
|
image_generation_call: {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ImageModel, ImageResponse, type ImageRequestFor } from "../image.js";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
import { type OpenString } from "../schema/index.js";
|
|
5
|
+
export declare const DEFAULT_BASE_URL = "https://api.minimax.io/v1";
|
|
6
|
+
export declare const PATH = "/image_generation";
|
|
7
|
+
/** Provider-native options. Common `aspect` and `resolution` lower to `aspect_ratio` or `width` and `height`. */
|
|
8
|
+
export type MiniMaxImageOptions = {
|
|
9
|
+
/** Defaults to `base64`, which returns the bytes inline; `url` links expire after 24 hours. */
|
|
10
|
+
readonly responseFormat?: OpenString<"url" | "base64">;
|
|
11
|
+
readonly seed?: number;
|
|
12
|
+
readonly promptOptimizer?: boolean;
|
|
13
|
+
/** Native preset; it replaces `aspect` and `resolution`. */
|
|
14
|
+
readonly aspect_ratio?: string;
|
|
15
|
+
/** Native size, 512 to 2048 in steps of 8 (`image-01` only); it replaces `aspect` and `resolution`. */
|
|
16
|
+
readonly width?: number;
|
|
17
|
+
readonly height?: number;
|
|
18
|
+
} & Record<string, unknown>;
|
|
19
|
+
export type Request = ImageRequestFor<MiniMaxImageOptions>;
|
|
20
|
+
export declare const protocol: MediaProtocol.Inline<Request, ImageResponse>;
|
|
21
|
+
export declare const model: (input: MediaRoute.ModelInput) => ImageModel<MiniMaxImageOptions>;
|
|
22
|
+
export declare const MiniMaxImages: {
|
|
23
|
+
readonly protocol: MediaProtocol.Inline<Request, ImageResponse>;
|
|
24
|
+
readonly model: typeof model;
|
|
25
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { Duration, 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 { MediaRoute } from "../route/media.js";
|
|
6
|
+
import { AIError, QuotaExceededError, mergeJsonRecords } from "../schema/index.js";
|
|
7
|
+
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
9
|
+
import { MediaInput } from "./utils/media-input.js";
|
|
10
|
+
const route = MediaProtocol.identity({ id: "minimax-images", name: "MiniMax Images", provider: "minimax" });
|
|
11
|
+
export const DEFAULT_BASE_URL = "https://api.minimax.io/v1";
|
|
12
|
+
export const PATH = "/image_generation";
|
|
13
|
+
const OUTPUT_RETENTION = Duration.hours(24);
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// 2. Response schema
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
const MiniMaxImageResponse = Schema.Struct({
|
|
18
|
+
id: optionalNull(Schema.String),
|
|
19
|
+
data: optionalNull(Schema.Struct({
|
|
20
|
+
image_urls: optionalNull(Schema.Array(Schema.String)),
|
|
21
|
+
image_base64: optionalNull(Schema.Array(Schema.String)),
|
|
22
|
+
})),
|
|
23
|
+
metadata: optionalNull(Schema.Struct({
|
|
24
|
+
success_count: optionalNull(Schema.Union([Schema.Number, Schema.String])),
|
|
25
|
+
failed_count: optionalNull(Schema.Union([Schema.Number, Schema.String])),
|
|
26
|
+
})),
|
|
27
|
+
base_resp: optionalNull(Schema.Struct({
|
|
28
|
+
status_code: Schema.Number,
|
|
29
|
+
status_msg: optionalNull(Schema.String),
|
|
30
|
+
})),
|
|
31
|
+
});
|
|
32
|
+
const FAILURE = {
|
|
33
|
+
1002: "RateLimit",
|
|
34
|
+
1004: "Authentication",
|
|
35
|
+
2013: "InvalidRequest",
|
|
36
|
+
2049: "Authentication",
|
|
37
|
+
};
|
|
38
|
+
const INSUFFICIENT_BALANCE = 1008;
|
|
39
|
+
const CONTENT_POLICY = 1026;
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// 5. Request body construction
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
/** MiniMax's `aspect_ratio` presets and the sizes it renders them at. */
|
|
44
|
+
const PRESETS = {
|
|
45
|
+
"1:1": { width: 1024, height: 1024 },
|
|
46
|
+
"16:9": { width: 1280, height: 720 },
|
|
47
|
+
"4:3": { width: 1152, height: 864 },
|
|
48
|
+
"3:2": { width: 1248, height: 832 },
|
|
49
|
+
"2:3": { width: 832, height: 1248 },
|
|
50
|
+
"3:4": { width: 864, height: 1152 },
|
|
51
|
+
"9:16": { width: 720, height: 1280 },
|
|
52
|
+
"21:9": { width: 1344, height: 576 },
|
|
53
|
+
};
|
|
54
|
+
const LIMITS = { multiple: 8, minEdge: 512, maxEdge: 2048 };
|
|
55
|
+
const nativeOptions = (options = {}) => {
|
|
56
|
+
const { responseFormat, promptOptimizer, ...native } = options;
|
|
57
|
+
return { response_format: responseFormat ?? "base64", prompt_optimizer: promptOptimizer, ...native };
|
|
58
|
+
};
|
|
59
|
+
/** `image-01-live` rejects `21:9`. */
|
|
60
|
+
const presets = (model) => model === "image-01" ? PRESETS : Object.fromEntries(Object.entries(PRESETS).filter(([key]) => key !== "21:9"));
|
|
61
|
+
/** Presets render at about 1K, so they serve a preset aspect with no resolution or `1K`. */
|
|
62
|
+
const preset = (request) => {
|
|
63
|
+
const aspect = request.aspect ?? "1:1";
|
|
64
|
+
if (!Object.hasOwn(presets(request.model.id), aspect))
|
|
65
|
+
return undefined;
|
|
66
|
+
if (request.resolution !== undefined && ImageSizing.tier(request.resolution) !== 1)
|
|
67
|
+
return undefined;
|
|
68
|
+
return aspect;
|
|
69
|
+
};
|
|
70
|
+
/** Only `image-01` takes free `width` and `height`; other models render the presets alone. */
|
|
71
|
+
const sizing = (request) => {
|
|
72
|
+
if (!ImageSizing.isRequested(request))
|
|
73
|
+
return Effect.succeed({});
|
|
74
|
+
const aspect = preset(request);
|
|
75
|
+
if (aspect !== undefined)
|
|
76
|
+
return Effect.succeed({ aspect_ratio: aspect });
|
|
77
|
+
if (request.model.id === "image-01")
|
|
78
|
+
return ImageSizing.exact(route, request, LIMITS);
|
|
79
|
+
const available = presets(request.model.id);
|
|
80
|
+
const closest = ImageSizing.nearest(request, Object.values(available));
|
|
81
|
+
const name = Object.keys(available).find((key) => available[key] === closest);
|
|
82
|
+
return Effect.fail(route.unsupported(request.resolution === undefined ? "media.aspect" : "media.resolution", `${request.model.id} can't render ${[request.aspect, request.resolution].filter((value) => value !== undefined).join(" at ")}; the closest size it takes is ${closest.width}x${closest.height} (${name}). Change aspect or resolution, or set aspect_ratio in providerOptions`));
|
|
83
|
+
};
|
|
84
|
+
const isNativeSize = (options) => options?.aspect_ratio !== undefined || options?.width !== undefined || options?.height !== undefined;
|
|
85
|
+
// The API only takes a `character` (portrait) subject, one per request.
|
|
86
|
+
const subject = (asset) => ProviderShared.mediaReference(asset, undefined, `${route.name} input`).pipe(Effect.map((item) => ({ type: "character", image_file: item.value })));
|
|
87
|
+
const fromRequest = Effect.fn("MiniMaxImages.fromRequest")(function* (request) {
|
|
88
|
+
if ((request.images?.length ?? 0) > 1)
|
|
89
|
+
return yield* route.unsupported("media.images", `${route.name} takes one subject reference image`);
|
|
90
|
+
if (isNativeSize(request.providerOptions) && ImageSizing.isRequested(request))
|
|
91
|
+
return yield* ProviderShared.invalidRequest("MiniMax providerOptions aspect_ratio, width and height replace aspect and resolution; set one or the other");
|
|
92
|
+
const references = yield* Effect.forEach(request.images ?? [], subject);
|
|
93
|
+
return MediaProtocol.json(mergeJsonRecords({
|
|
94
|
+
model: request.model.id,
|
|
95
|
+
prompt: request.prompt,
|
|
96
|
+
subject_reference: references.length === 0 ? undefined : references,
|
|
97
|
+
n: request.n,
|
|
98
|
+
...(yield* sizing(request)),
|
|
99
|
+
}, nativeOptions(request.providerOptions), request.http?.body) ?? {});
|
|
100
|
+
});
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
// 6. Response decoding
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
const decodeDocument = route.decodeJson(MiniMaxImageResponse);
|
|
105
|
+
const decodeResponse = Effect.fn("MiniMaxImages.decodeResponse")(function* (response) {
|
|
106
|
+
const output = yield* decodeDocument(response);
|
|
107
|
+
const decoded = output.value;
|
|
108
|
+
// MiniMax answers errors with HTTP 200 and a non-zero `base_resp.status_code`.
|
|
109
|
+
const status = decoded.base_resp;
|
|
110
|
+
if (status && status.status_code !== 0) {
|
|
111
|
+
const message = `${route.name} failed (${status.status_code}): ${status.status_msg ?? "unknown error"}`;
|
|
112
|
+
if (status.status_code === CONTENT_POLICY)
|
|
113
|
+
return yield* output.contentPolicy(message);
|
|
114
|
+
if (status.status_code === INSUFFICIENT_BALANCE)
|
|
115
|
+
return yield* new AIError({ reason: new QuotaExceededError({ message, body: output.body, http: output.http }) });
|
|
116
|
+
return yield* output.ended("failed", message, MediaProtocol.failure(FAILURE, status.status_code));
|
|
117
|
+
}
|
|
118
|
+
const images = [
|
|
119
|
+
...(yield* Effect.forEach(decoded.data?.image_base64 ?? [], (data, index) => MediaInput.decodedAsset(output.invalid, `${route.name} result ${index}`, data, undefined))),
|
|
120
|
+
// MiniMax returns no content type for URLs; the media type resolves when the asset is materialized.
|
|
121
|
+
...(yield* Effect.forEach(decoded.data?.image_urls ?? [], (url) => MediaProtocol.expiringUrl(url, OUTPUT_RETENTION))),
|
|
122
|
+
];
|
|
123
|
+
const failed = Number(decoded.metadata?.failed_count ?? 0);
|
|
124
|
+
if (images.length === 0 && failed > 0)
|
|
125
|
+
return yield* output.contentPolicy(`${route.name} blocked ${failed} image(s) for content safety`);
|
|
126
|
+
if (images.length === 0)
|
|
127
|
+
return yield* output.invalid(`${route.name} returned no images`);
|
|
128
|
+
return new ImageResponse({
|
|
129
|
+
images,
|
|
130
|
+
providerMetadata: {
|
|
131
|
+
minimax: {
|
|
132
|
+
id: decoded.id ?? undefined,
|
|
133
|
+
successCount: decoded.metadata?.success_count ?? undefined,
|
|
134
|
+
failedCount: decoded.metadata?.failed_count ?? undefined,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// 7. Protocol and route
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
export const protocol = MediaProtocol.inline(route, {
|
|
143
|
+
body: { from: fromRequest },
|
|
144
|
+
response: { decode: decodeResponse },
|
|
145
|
+
});
|
|
146
|
+
export const model = (input) => ImageModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: PATH }, input);
|
|
147
|
+
export const MiniMaxImages = {
|
|
148
|
+
protocol,
|
|
149
|
+
model,
|
|
150
|
+
};
|