@opencode/ai 2.1.4 → 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 +5 -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 +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 +20 -0
- 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 +6 -3
- 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/bfl-images.d.ts +9 -0
- package/dist/protocols/bfl-images.js +17 -15
- 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/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 +3 -0
- package/dist/protocols/gemini.js +11 -6
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-images.js +8 -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 +6 -0
- package/dist/protocols/meta-responses.js +29 -1
- package/dist/protocols/open-responses.d.ts +30 -0
- package/dist/protocols/open-responses.js +8 -18
- package/dist/protocols/openai-chat.d.ts +713 -1
- package/dist/protocols/openai-chat.js +35 -22
- package/dist/protocols/openai-compatible-chat.d.ts +22 -1
- package/dist/protocols/openai-compatible-responses.d.ts +8 -5
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +128 -0
- package/dist/protocols/openai-images.d.ts +21 -24
- package/dist/protocols/openai-images.js +100 -34
- package/dist/protocols/openai-responses.d.ts +82 -24
- package/dist/protocols/openai-responses.js +32 -2
- 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/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/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 +19 -0
- package/dist/protocols/venice-chat.js +1 -1
- package/dist/protocols/xai-images.d.ts +1 -2
- package/dist/protocols/xai-images.js +9 -2
- package/dist/protocols/xai-responses.d.ts +10 -0
- 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 +21 -0
- 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 +19 -4
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba.d.ts +79 -18
- package/dist/providers/alibaba.js +16 -8
- package/dist/providers/amazon-bedrock-mantle.d.ts +197 -138
- package/dist/providers/amazon-bedrock.d.ts +34 -14
- package/dist/providers/amazon-bedrock.js +11 -11
- package/dist/providers/anthropic-compatible.d.ts +52 -17
- package/dist/providers/anthropic.d.ts +52 -17
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure.d.ts +50 -9
- 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 +51 -1
- package/dist/providers/baseten.js +41 -8
- package/dist/providers/cerebras.d.ts +42 -0
- 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 +177 -43
- 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 +55 -1
- package/dist/providers/cloudflare-workers-ai.js +45 -9
- package/dist/providers/cohere.d.ts +42 -0
- package/dist/providers/deepinfra.d.ts +42 -0
- package/dist/providers/deepseek.d.ts +42 -0
- 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 +72 -1
- package/dist/providers/digitalocean.js +39 -8
- package/dist/providers/fireworks.d.ts +42 -0
- 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 +21 -0
- package/dist/providers/google-vertex-messages.d.ts +52 -17
- package/dist/providers/google-vertex-responses.d.ts +3 -0
- package/dist/providers/google-vertex.d.ts +3 -1
- package/dist/providers/google-vertex.js +30 -13
- package/dist/providers/google.d.ts +5 -4
- package/dist/providers/google.js +15 -8
- package/dist/providers/groq.d.ts +42 -4
- package/dist/providers/groq.js +0 -6
- package/dist/providers/meta.d.ts +197 -138
- package/dist/providers/minimax.d.ts +76 -17
- package/dist/providers/moonshot.d.ts +76 -17
- 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 +3 -0
- package/dist/providers/openai-compatible.d.ts +24 -1
- package/dist/providers/openai-compatible.js +21 -9
- package/dist/providers/openai-options.d.ts +3 -1
- package/dist/providers/openai.d.ts +46 -9
- 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 +92 -11
- package/dist/providers/openrouter.js +38 -16
- package/dist/providers/togetherai.d.ts +42 -0
- 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 +19 -0
- 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 +28 -5
- 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 +76 -17
- package/dist/providers/zai.d.ts +26 -4
- package/dist/providers/zai.js +15 -8
- 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 +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 +5 -14
- package/dist/speech-client.js +0 -1
- package/dist/speech.d.ts +0 -135
- 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 +5 -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 +5 -14
- package/dist/video-client.js +0 -1
- package/dist/video.d.ts +1 -136
- package/dist/video.js +3 -5
- package/package.json +513 -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/provider.d.ts +0 -23
- package/dist/provider.js +0 -2
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
import type { CacheHint } from "../../schema/index.js";
|
|
1
|
+
import type { CacheHint, CacheTTL } from "../../schema/index.js";
|
|
2
2
|
export interface Breakpoints {
|
|
3
3
|
remaining: number;
|
|
4
4
|
dropped: number;
|
|
5
5
|
}
|
|
6
6
|
export declare const newBreakpoints: (cap: number) => Breakpoints;
|
|
7
|
-
export declare const ttlBucket: (ttlSeconds: number | undefined) => "1h" | undefined;
|
|
8
7
|
export declare const cacheControl: () => (cache: CacheHint | undefined) => {
|
|
9
8
|
type: "ephemeral";
|
|
10
|
-
ttl:
|
|
9
|
+
ttl: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s` | undefined;
|
|
11
10
|
} | undefined;
|
|
11
|
+
export declare const openAIRequestTTL: (ttl: CacheTTL | undefined) => {
|
|
12
|
+
prompt_cache_retention?: undefined;
|
|
13
|
+
prompt_cache_options?: undefined;
|
|
14
|
+
} | {
|
|
15
|
+
prompt_cache_retention: string;
|
|
16
|
+
prompt_cache_options?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
prompt_cache_retention?: undefined;
|
|
19
|
+
prompt_cache_options: {
|
|
20
|
+
ttl: `${number}d` | `${number}h` | `${number}m` | `${number}ms` | `${number}s`;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
export const newBreakpoints = (cap) => ({ remaining: cap, dropped: 0 });
|
|
2
|
-
// Requests of at least one hour use the explicit `"1h"` bucket; shorter
|
|
3
|
-
// requests omit the wire TTL and use the provider default.
|
|
4
|
-
export const ttlBucket = (ttlSeconds) => ttlSeconds !== undefined && ttlSeconds >= 3600 ? "1h" : undefined;
|
|
5
2
|
export const cacheControl = () => {
|
|
6
3
|
const breakpoints = newBreakpoints(4);
|
|
7
4
|
return (cache) => {
|
|
8
5
|
if (cache === undefined || breakpoints.remaining === 0)
|
|
9
6
|
return undefined;
|
|
10
7
|
breakpoints.remaining -= 1;
|
|
11
|
-
return { type: "ephemeral", ttl:
|
|
8
|
+
return { type: "ephemeral", ttl: cache.ttl };
|
|
12
9
|
};
|
|
13
10
|
};
|
|
11
|
+
// OpenAI's request-level lifetime. `prompt_cache_retention` takes only "24h" (or "in_memory") and is the only
|
|
12
|
+
// control before GPT-5.6; every other lifetime goes to `prompt_cache_options.ttl`, which GPT-5.6+, Kimi and
|
|
13
|
+
// DeepInfra read.
|
|
14
|
+
export const openAIRequestTTL = (ttl) => {
|
|
15
|
+
if (ttl === undefined)
|
|
16
|
+
return {};
|
|
17
|
+
if (ttl === "24h")
|
|
18
|
+
return { prompt_cache_retention: ttl };
|
|
19
|
+
return { prompt_cache_options: { ttl } };
|
|
20
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Effect, Schema, type Stream } from "effect";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Tool } from "@opencode/schema/tool";
|
|
3
|
+
import { Media } from "../../media.js";
|
|
3
4
|
import type { MediaProtocol } from "../../route/media-protocol.js";
|
|
4
5
|
import { AIError, type MediaUsage, type ProviderMetadata } from "../../schema/index.js";
|
|
5
6
|
declare const UsageMetadata: Schema.Struct<{
|
|
@@ -51,12 +52,11 @@ export declare const blocked: (name: string, chunk: Chunk, frame: string) => AIE
|
|
|
51
52
|
export declare const contentFiltered: (finishReason: string | undefined) => boolean;
|
|
52
53
|
/** Callers check that the response produced no output: a policy stop after output is a partial result instead. */
|
|
53
54
|
export declare const withheld: (name: string, chunk: Chunk, frame: string) => AIError | undefined;
|
|
54
|
-
/** Any finish reason other than `STOP` means the output may be cut short, so it is surfaced rather than dropped. */
|
|
55
|
-
export declare const notices: (name: string, state: Metadata) => ReadonlyArray<Media.Notice> | undefined;
|
|
56
55
|
export declare const usage: (usage: UsageMetadata | undefined) => MediaUsage | undefined;
|
|
57
56
|
export declare const providerMetadata: (state: Metadata) => ProviderMetadata;
|
|
58
57
|
export declare const path: (model: string, mode: MediaProtocol.Mode) => string;
|
|
59
58
|
export declare const frames: (bytes: Stream.Stream<Uint8Array, AIError>, mode: MediaProtocol.Mode) => Stream.Stream<string, AIError, never>;
|
|
59
|
+
export declare const toolMedia: (item: Tool.FileContent) => Media.Asset;
|
|
60
60
|
export declare const mediaPart: (route: string, asset: Media.Asset) => Effect.Effect<{
|
|
61
61
|
readonly fileData: {
|
|
62
62
|
readonly mimeType: string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Media } from "../../media.js";
|
|
2
3
|
import { Framing } from "../../route/framing.js";
|
|
3
|
-
import { AIError, ContentPolicyError, ProviderID } from "../../schema/index.js";
|
|
4
|
+
import { AIError, ContentPolicyError, ProviderID, tokenUsage, } from "../../schema/index.js";
|
|
4
5
|
import { ProviderShared } from "../shared.js";
|
|
5
6
|
import { MediaInput } from "./media-input.js";
|
|
6
7
|
const PROVIDER = ProviderID.make("google");
|
|
@@ -63,34 +64,27 @@ export const withheld = (name, chunk, frame) => {
|
|
|
63
64
|
reason: new ContentPolicyError({ message: `${name} withheld its output (${finishReason})`, body: frame }),
|
|
64
65
|
});
|
|
65
66
|
};
|
|
66
|
-
/** Any finish reason other than `STOP` means the output may be cut short, so it is surfaced rather than dropped. */
|
|
67
|
-
export const notices = (name, state) => state.finishReason === undefined || state.finishReason === "STOP"
|
|
68
|
-
? undefined
|
|
69
|
-
: [
|
|
70
|
-
{
|
|
71
|
-
type: contentFiltered(state.finishReason) ? "filtered" : "other",
|
|
72
|
-
message: `${name} finished with ${state.finishReason}`,
|
|
73
|
-
providerMetadata: { google: { finishReason: state.finishReason } },
|
|
74
|
-
},
|
|
75
|
-
];
|
|
76
67
|
export const usage = (usage) => usage === undefined
|
|
77
68
|
? undefined
|
|
78
|
-
: {
|
|
79
|
-
type: "tokens",
|
|
69
|
+
: tokenUsage({
|
|
80
70
|
input: usage.promptTokenCount,
|
|
81
71
|
output: usage.candidatesTokenCount,
|
|
82
72
|
total: ProviderShared.totalTokens(usage.promptTokenCount, usage.candidatesTokenCount, usage.totalTokenCount),
|
|
83
73
|
details: { google: usage },
|
|
84
|
-
};
|
|
74
|
+
});
|
|
85
75
|
export const providerMetadata = (state) => ({
|
|
86
76
|
google: { finishReason: state.finishReason, modelVersion: state.modelVersion, responseId: state.responseId },
|
|
87
77
|
});
|
|
88
78
|
export const path = (model, mode) => mode === "stream" ? `/models/${model}:streamGenerateContent?alt=sse` : `/models/${model}:generateContent`;
|
|
89
79
|
// `generateContent` answers with one document shaped exactly like a streamed chunk, so it is a single frame.
|
|
90
80
|
export const frames = (bytes, mode) => mode === "stream" ? Framing.sse.frame(bytes) : Framing.document.frame(bytes);
|
|
91
|
-
|
|
81
|
+
export const toolMedia = (item) => item.mime.startsWith("video/") && item.uri.startsWith("gs://")
|
|
82
|
+
? Media.ref(PROVIDER, item.uri, item.mime)
|
|
83
|
+
: ProviderShared.toolFileMedia(item).media;
|
|
92
84
|
export const mediaPart = (route, asset) => {
|
|
93
|
-
const fileUri =
|
|
85
|
+
const fileUri = asset.kind === "video" && asset.source.type === "url" && /^https?:\/\//i.test(asset.source.url)
|
|
86
|
+
? asset.source.url
|
|
87
|
+
: MediaInput.refID(asset, PROVIDER);
|
|
94
88
|
if (fileUri !== undefined)
|
|
95
89
|
return Effect.succeed({ fileData: { mimeType: asset.mediaType, fileUri } });
|
|
96
90
|
return ProviderShared.requireInlineMedia(route, asset).pipe(Effect.map((media) => ({ inlineData: { mimeType: media.mime, data: media.base64 } })));
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import type { JsonSchema } from "../../schema/index.js";
|
|
2
|
+
export declare const SCHEMA_MAPS: Set<string>;
|
|
3
|
+
export declare const VALUES: Set<string>;
|
|
4
|
+
export declare const mapValues: (record: Record<string, unknown>, map: (value: unknown, key: string) => unknown) => {
|
|
5
|
+
[k: string]: unknown;
|
|
6
|
+
};
|
|
2
7
|
export declare const normalize: (schema: JsonSchema) => JsonSchema;
|
|
3
8
|
export * as GeminiJsonSchema from "./gemini-json-schema.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isRecord } from "../../utils/record.js";
|
|
2
2
|
// Gemini's `parametersJsonSchema` accepts standard JSON Schema, but rejects a few shapes that
|
|
3
3
|
// published tool schemas commonly contain. Rewrite only those and send everything else unchanged.
|
|
4
|
-
const SCHEMA_MAPS = new Set([
|
|
4
|
+
export const SCHEMA_MAPS = new Set([
|
|
5
5
|
"properties",
|
|
6
6
|
"patternProperties",
|
|
7
7
|
"$defs",
|
|
@@ -9,8 +9,8 @@ const SCHEMA_MAPS = new Set([
|
|
|
9
9
|
"dependentSchemas",
|
|
10
10
|
"dependencies",
|
|
11
11
|
]);
|
|
12
|
-
const VALUES = new Set(["const", "default", "enum", "examples", "dependentRequired"]);
|
|
13
|
-
const mapValues = (record, map) => Object.fromEntries(Object.entries(record).map(([key, value]) => [key, map(value, key)]));
|
|
12
|
+
export const VALUES = new Set(["const", "default", "enum", "examples", "dependentRequired"]);
|
|
13
|
+
export const mapValues = (record, map) => Object.fromEntries(Object.entries(record).map(([key, value]) => [key, map(value, key)]));
|
|
14
14
|
const normalizeNode = (schema) => {
|
|
15
15
|
if (Array.isArray(schema))
|
|
16
16
|
return schema.map(normalizeNode);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { MediaProtocol } from "../../route/media-protocol.js";
|
|
3
|
+
export interface Size {
|
|
4
|
+
readonly width: number;
|
|
5
|
+
readonly height: number;
|
|
6
|
+
}
|
|
7
|
+
/** The shared `aspect` and `resolution` fields of an image request. */
|
|
8
|
+
export interface Shape {
|
|
9
|
+
readonly aspect?: string;
|
|
10
|
+
readonly resolution?: string;
|
|
11
|
+
}
|
|
12
|
+
/** What a provider accepts for free width and height. */
|
|
13
|
+
export interface Limits {
|
|
14
|
+
readonly multiple: number;
|
|
15
|
+
readonly minEdge?: number;
|
|
16
|
+
readonly maxEdge?: number;
|
|
17
|
+
readonly minPixels?: number;
|
|
18
|
+
readonly maxPixels?: number;
|
|
19
|
+
/** Long edge over short edge. */
|
|
20
|
+
readonly maxRatio?: number;
|
|
21
|
+
}
|
|
22
|
+
export declare const isRequested: (shape: Shape) => boolean;
|
|
23
|
+
/** `1K` is 1, `2K` is 2, `0.5K` is 0.5; anything else is not a tier. */
|
|
24
|
+
export declare const tier: (resolution: string) => number | undefined;
|
|
25
|
+
/** Free width and height: the tier sets the pixel count (`1K` is about 1024²), the aspect sets the proportions. */
|
|
26
|
+
export declare const fit: (shape: Shape, limits: Limits) => Size;
|
|
27
|
+
/** The listed size closest to the shape: nearest aspect ratio first, then nearest pixel count. */
|
|
28
|
+
export declare const nearest: (shape: Shape, sizes: ReadonlyArray<Size>) => Size;
|
|
29
|
+
/** Free width and height that must honor the shape; a size that misses it fails. */
|
|
30
|
+
export declare const exact: (route: MediaProtocol.Identity, shape: Shape, limits: Limits) => Effect.Effect<undefined, never, never> | Effect.Effect<never, import("../../effect.js").AIError, never> | Effect.Effect<Size, never, never>;
|
|
31
|
+
export * as ImageSizing from "./image-sizing.js";
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export const isRequested = (shape) => shape.aspect !== undefined || shape.resolution !== undefined;
|
|
3
|
+
/** `1K` is 1, `2K` is 2, `0.5K` is 0.5; anything else is not a tier. */
|
|
4
|
+
export const tier = (resolution) => {
|
|
5
|
+
const match = /^(\d+(?:\.\d+)?)k$/i.exec(resolution);
|
|
6
|
+
return match === null ? undefined : Number(match[1]);
|
|
7
|
+
};
|
|
8
|
+
/** Free width and height: the tier sets the pixel count (`1K` is about 1024²), the aspect sets the proportions. */
|
|
9
|
+
export const fit = (shape, limits) => {
|
|
10
|
+
const maxRatio = limits.maxRatio ?? Infinity;
|
|
11
|
+
const ratio = Math.min(maxRatio, Math.max(1 / maxRatio, aspectRatio(shape)));
|
|
12
|
+
const area = targetPixels(shape);
|
|
13
|
+
const width = Math.sqrt(area * ratio);
|
|
14
|
+
const height = Math.sqrt(area / ratio);
|
|
15
|
+
const up = Math.max((limits.minEdge ?? 0) / Math.min(width, height), Math.sqrt((limits.minPixels ?? 0) / area));
|
|
16
|
+
const scaled = Math.max(1, up);
|
|
17
|
+
const down = Math.min((limits.maxEdge ?? Infinity) / (Math.max(width, height) * scaled), Math.sqrt((limits.maxPixels ?? Infinity) / area) / scaled);
|
|
18
|
+
// Round away from a limit the size reached, including one it lands on exactly, so it never crosses it.
|
|
19
|
+
const round = down <= 1 ? Math.floor : up >= 1 ? Math.ceil : Math.round;
|
|
20
|
+
const snap = (edge) => Math.max(limits.multiple, round(Number(((edge * scaled * Math.min(1, down)) / limits.multiple).toFixed(6))) * limits.multiple);
|
|
21
|
+
return { width: snap(width), height: snap(height) };
|
|
22
|
+
};
|
|
23
|
+
/** The listed size closest to the shape: nearest aspect ratio first, then nearest pixel count. */
|
|
24
|
+
export const nearest = (shape, sizes) => {
|
|
25
|
+
const ratio = Math.log(aspectRatio(shape));
|
|
26
|
+
const area = Math.log(targetPixels(shape));
|
|
27
|
+
const distance = (size) => [
|
|
28
|
+
Math.abs(Math.log(size.width / size.height) - ratio),
|
|
29
|
+
Math.abs(Math.log(size.width * size.height) - area),
|
|
30
|
+
];
|
|
31
|
+
return sizes.reduce((best, size) => {
|
|
32
|
+
const [sizeRatio, sizeArea] = distance(size);
|
|
33
|
+
const [bestRatio, bestArea] = distance(best);
|
|
34
|
+
if (Math.abs(sizeRatio - bestRatio) > 1e-9)
|
|
35
|
+
return sizeRatio < bestRatio ? size : best;
|
|
36
|
+
return sizeArea < bestArea ? size : best;
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
/** Free width and height that must honor the shape; a size that misses it fails. */
|
|
40
|
+
export const exact = (route, shape, limits) => {
|
|
41
|
+
if (!isRequested(shape))
|
|
42
|
+
return Effect.succeed(undefined);
|
|
43
|
+
const size = fit(shape, limits);
|
|
44
|
+
const requested = missed(shape, size);
|
|
45
|
+
if (requested === undefined)
|
|
46
|
+
return Effect.succeed(size);
|
|
47
|
+
return Effect.fail(route.unsupported(shape.resolution === undefined ? "media.aspect" : "media.resolution", `${route.name} can't render ${requested}; the closest size it takes is ${describe(size)}. Change aspect or resolution, or set the size in providerOptions`));
|
|
48
|
+
};
|
|
49
|
+
/** A size more than 2% off the requested aspect or more than √2 per edge off the requested tier. */
|
|
50
|
+
const missed = (shape, size) => {
|
|
51
|
+
const aspectMissed = shape.aspect !== undefined && Math.abs(Math.log(size.width / size.height / aspectRatio(shape))) > 0.02;
|
|
52
|
+
const requestedTier = shape.resolution === undefined ? undefined : tier(shape.resolution);
|
|
53
|
+
const resolutionMissed = shape.resolution !== undefined &&
|
|
54
|
+
(requestedTier === undefined ||
|
|
55
|
+
Math.abs(Math.log2(Math.sqrt(size.width * size.height) / (requestedTier * 1024))) > 0.5);
|
|
56
|
+
if (!aspectMissed && !resolutionMissed)
|
|
57
|
+
return undefined;
|
|
58
|
+
return [shape.aspect, shape.resolution].filter((value) => value !== undefined).join(" at ");
|
|
59
|
+
};
|
|
60
|
+
const aspectRatio = (shape) => {
|
|
61
|
+
const [width, height] = (shape.aspect ?? "1:1").split(":").map(Number);
|
|
62
|
+
return width / height;
|
|
63
|
+
};
|
|
64
|
+
const targetPixels = (shape) => ((tier(shape.resolution ?? "1K") ?? 1) * 1024) ** 2;
|
|
65
|
+
const describe = (size) => {
|
|
66
|
+
const divisor = gcd(size.width, size.height);
|
|
67
|
+
const width = size.width / divisor;
|
|
68
|
+
const height = size.height / divisor;
|
|
69
|
+
const ratio = width <= 32 && height <= 32 ? `${width}:${height}` : `${(size.width / size.height).toFixed(2)}:1`;
|
|
70
|
+
return `${size.width}x${size.height} (${ratio})`;
|
|
71
|
+
};
|
|
72
|
+
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
|
|
73
|
+
export * as ImageSizing from "./image-sizing.js";
|
|
@@ -7,15 +7,14 @@ export declare const inlineBytes: (route: string, asset: Media.Asset) => Effect.
|
|
|
7
7
|
/** Copied because `BlobPart` requires a plain `ArrayBuffer`. */
|
|
8
8
|
export declare const blob: (data: Uint8Array, mediaType: string) => Blob;
|
|
9
9
|
export declare const query: (route: string, values: Record<string, unknown>) => Effect.Effect<MediaProtocol.Query, AIError>;
|
|
10
|
-
export declare const dimensions: (size: string) => {
|
|
11
|
-
width: number;
|
|
12
|
-
height: number;
|
|
13
|
-
};
|
|
14
10
|
/** Provider file handle when the ref belongs to this provider; refs from other providers are never forwarded. */
|
|
15
11
|
export declare const refID: (asset: Media.Asset, provider: ProviderID) => string | undefined;
|
|
16
12
|
/** Decode a provider's base64 output once into an owned `bytes` asset, sniffing the type when it is not declared. */
|
|
17
13
|
export declare const decodedAsset: (invalid: (message: string, cause?: unknown) => AIError, label: string, data: string, mediaType: string | undefined, options?: Media.AssetOptions) => Effect.Effect<Media.Asset, AIError, never>;
|
|
18
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* One image of an OpenAI-shaped `data` array, which carries `b64_json`, a `url`, or both. `b64_json` wins; a `data:`
|
|
16
|
+
* URL decodes inline with its own media type, and any other URL stays a URL asset for the caller to download.
|
|
17
|
+
*/
|
|
19
18
|
export declare const imageOutput: (invalid: (message: string, cause?: unknown) => AIError, label: string, item: {
|
|
20
19
|
readonly b64_json?: string | null;
|
|
21
20
|
readonly url?: string | null;
|
|
@@ -27,18 +27,21 @@ export const query = (route, values) => {
|
|
|
27
27
|
return Effect.fail(ProviderShared.invalidRequest(`${route} cannot send "${invalid[0]}" as a query parameter`));
|
|
28
28
|
return Effect.succeed(Object.fromEntries(entries.map(([key, value]) => [key, Array.isArray(value) ? value.map(String) : String(value)])));
|
|
29
29
|
};
|
|
30
|
-
export const dimensions = (size) => {
|
|
31
|
-
const [width, height] = size.split("x").map(Number);
|
|
32
|
-
return { width, height };
|
|
33
|
-
};
|
|
34
30
|
/** Provider file handle when the ref belongs to this provider; refs from other providers are never forwarded. */
|
|
35
31
|
export const refID = (asset, provider) => asset.source.type === "ref" && asset.source.provider === provider ? asset.source.id : undefined;
|
|
36
32
|
/** Decode a provider's base64 output once into an owned `bytes` asset, sniffing the type when it is not declared. */
|
|
37
33
|
export const decodedAsset = (invalid, label, data, mediaType, options) => Effect.fromResult(Base64.decode(data)).pipe(Effect.mapError((cause) => invalid(`${label} contains invalid base64 data`, cause)), Effect.map((bytes) => Media.bytes(bytes, mediaType, options)));
|
|
38
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* One image of an OpenAI-shaped `data` array, which carries `b64_json`, a `url`, or both. `b64_json` wins; a `data:`
|
|
36
|
+
* URL decodes inline with its own media type, and any other URL stays a URL asset for the caller to download.
|
|
37
|
+
*/
|
|
39
38
|
export const imageOutput = (invalid, label, item, mediaType, options) => {
|
|
40
39
|
if (item.b64_json)
|
|
41
40
|
return decodedAsset(invalid, label, item.b64_json, mediaType, options);
|
|
41
|
+
if (item.url?.startsWith("data:")) {
|
|
42
|
+
const asset = Media.parseDataUrl(item.url, options);
|
|
43
|
+
return asset === undefined ? Effect.fail(invalid(`${label} has an invalid data URL`)) : Effect.succeed(asset);
|
|
44
|
+
}
|
|
42
45
|
if (item.url)
|
|
43
46
|
return Effect.succeed(Media.url(item.url, { ...options, mediaType }));
|
|
44
47
|
return Effect.fail(invalid(`${label} has neither image data nor a URL`));
|
|
@@ -42,7 +42,6 @@ export declare const finish: (route: MediaProtocol.Identity, state: Audio, outpu
|
|
|
42
42
|
readonly mediaType: string | undefined;
|
|
43
43
|
readonly info?: Media.Info;
|
|
44
44
|
readonly usage?: MediaUsage;
|
|
45
|
-
readonly notices?: ReadonlyArray<Media.Notice>;
|
|
46
45
|
readonly providerMetadata?: ProviderMetadata;
|
|
47
46
|
readonly detail?: string;
|
|
48
47
|
}) => Effect.Effect<ReadonlyArray<SpeechEvent>, AIError>;
|
|
@@ -51,7 +51,6 @@ export const finish = (route, state, output) => {
|
|
|
51
51
|
SpeechFinishEvent.make({
|
|
52
52
|
audio: Media.bytes(concatBytes(state.chunks), output.mediaType, { info: output.info }),
|
|
53
53
|
usage: output.usage,
|
|
54
|
-
notices: output.notices,
|
|
55
54
|
providerMetadata: output.providerMetadata,
|
|
56
55
|
}),
|
|
57
56
|
]);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ToolDefinition } from "../../schema/index.js";
|
|
2
2
|
import { isRecord } from "../../utils/record.js";
|
|
3
3
|
import { GeminiJsonSchema } from "./gemini-json-schema.js";
|
|
4
|
+
const { SCHEMA_MAPS, VALUES, mapValues } = GeminiJsonSchema;
|
|
4
5
|
const tupleItemsSchema = (items) => {
|
|
5
6
|
const projected = items.map(moonshotNode);
|
|
6
7
|
if (projected.length === 0)
|
|
@@ -31,12 +32,21 @@ const moonshotNode = (schema) => {
|
|
|
31
32
|
return Object.fromEntries([
|
|
32
33
|
...(type === undefined ? [] : [["type", type]]),
|
|
33
34
|
...Object.entries(schema).flatMap(([key, value]) => {
|
|
34
|
-
if (
|
|
35
|
-
return [[key,
|
|
36
|
-
if (key
|
|
37
|
-
|
|
35
|
+
if (VALUES.has(key))
|
|
36
|
+
return [[key, value]];
|
|
37
|
+
if (SCHEMA_MAPS.has(key) && isRecord(value))
|
|
38
|
+
return [[key, mapValues(value, moonshotNode)]];
|
|
39
|
+
if (key === "items") {
|
|
40
|
+
if (Array.isArray(schema.prefixItems))
|
|
38
41
|
return [];
|
|
39
|
-
|
|
42
|
+
if (Array.isArray(value))
|
|
43
|
+
return [[key, tupleItemsSchema(value)]];
|
|
44
|
+
}
|
|
45
|
+
if (key === "prefixItems") {
|
|
46
|
+
if (!Array.isArray(value))
|
|
47
|
+
return "items" in schema ? [] : [["items", {}]];
|
|
48
|
+
const rest = isRecord(schema.items) ? [schema.items] : Array.isArray(schema.items) ? schema.items : [];
|
|
49
|
+
return [["items", tupleItemsSchema([...value, ...rest])]];
|
|
40
50
|
}
|
|
41
51
|
if (key === "unevaluatedItems")
|
|
42
52
|
return [];
|
|
@@ -79,6 +79,9 @@ export declare const finish: <K extends StreamKey>(route: string, tools: State<K
|
|
|
79
79
|
events: readonly ({
|
|
80
80
|
readonly type: "step-start";
|
|
81
81
|
readonly index: number;
|
|
82
|
+
} | {
|
|
83
|
+
readonly type: "raw";
|
|
84
|
+
readonly data: unknown;
|
|
82
85
|
} | {
|
|
83
86
|
readonly type: "text-start";
|
|
84
87
|
readonly id: string;
|
|
@@ -306,6 +309,9 @@ export declare const finishWithInput: <K extends StreamKey>(route: string, tools
|
|
|
306
309
|
events: readonly ({
|
|
307
310
|
readonly type: "step-start";
|
|
308
311
|
readonly index: number;
|
|
312
|
+
} | {
|
|
313
|
+
readonly type: "raw";
|
|
314
|
+
readonly data: unknown;
|
|
309
315
|
} | {
|
|
310
316
|
readonly type: "text-start";
|
|
311
317
|
readonly id: string;
|
|
@@ -530,6 +536,9 @@ export declare const finishAll: <K extends StreamKey>(route: string, tools: Stat
|
|
|
530
536
|
events: ({
|
|
531
537
|
readonly type: "step-start";
|
|
532
538
|
readonly index: number;
|
|
539
|
+
} | {
|
|
540
|
+
readonly type: "raw";
|
|
541
|
+
readonly data: unknown;
|
|
533
542
|
} | {
|
|
534
543
|
readonly type: "text-start";
|
|
535
544
|
readonly id: string;
|
|
@@ -79,7 +79,16 @@ export declare const protocol: Protocol<{
|
|
|
79
79
|
} | undefined;
|
|
80
80
|
readonly store?: boolean | undefined;
|
|
81
81
|
readonly prompt_cache_key?: string | undefined;
|
|
82
|
+
readonly prompt_cache_options?: {
|
|
83
|
+
readonly ttl?: string | undefined;
|
|
84
|
+
} | undefined;
|
|
82
85
|
readonly reasoning_effort?: OpenResponsesOptions.ReasoningEffort | undefined;
|
|
86
|
+
readonly verbosity?: import("../schema/options.js").OpenString<"high" | "low" | "medium"> | undefined;
|
|
87
|
+
readonly service_tier?: import("../schema/options.js").OpenString<"auto" | "default" | "flex" | "priority"> | undefined;
|
|
88
|
+
readonly metadata?: {
|
|
89
|
+
readonly [x: string]: string;
|
|
90
|
+
} | undefined;
|
|
91
|
+
readonly safety_identifier?: string | undefined;
|
|
83
92
|
readonly tool_stream?: boolean | undefined;
|
|
84
93
|
readonly max_completion_tokens?: number | undefined;
|
|
85
94
|
readonly max_tokens?: number | undefined;
|
|
@@ -96,6 +105,11 @@ export declare const protocol: Protocol<{
|
|
|
96
105
|
readonly image_url: {
|
|
97
106
|
readonly url: string;
|
|
98
107
|
};
|
|
108
|
+
} | {
|
|
109
|
+
readonly type: "video_url";
|
|
110
|
+
readonly video_url: {
|
|
111
|
+
readonly url: string;
|
|
112
|
+
};
|
|
99
113
|
} | {
|
|
100
114
|
readonly type: "file";
|
|
101
115
|
readonly file: {
|
|
@@ -117,6 +131,11 @@ export declare const protocol: Protocol<{
|
|
|
117
131
|
readonly image_url: {
|
|
118
132
|
readonly url: string;
|
|
119
133
|
};
|
|
134
|
+
} | {
|
|
135
|
+
readonly type: "video_url";
|
|
136
|
+
readonly video_url: {
|
|
137
|
+
readonly url: string;
|
|
138
|
+
};
|
|
120
139
|
} | {
|
|
121
140
|
readonly type: "file";
|
|
122
141
|
readonly file: {
|
|
@@ -150,7 +150,7 @@ const fromRequest = Effect.fn("VeniceChat.fromRequest")(function* (request) {
|
|
|
150
150
|
})
|
|
151
151
|
.filter(([, value]) => value !== undefined)
|
|
152
152
|
.map(([key, value]) => [key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`), value])),
|
|
153
|
-
prompt_cache_retention: options.promptCacheRetention,
|
|
153
|
+
prompt_cache_retention: options.promptCacheRetention ?? body.prompt_cache_retention,
|
|
154
154
|
parallel_tool_calls: options.parallelToolCalls,
|
|
155
155
|
min_p: options.minP,
|
|
156
156
|
top_k: request.generation?.topK,
|
|
@@ -5,9 +5,8 @@ import { type OpenString } from "../schema/index.js";
|
|
|
5
5
|
export declare const DEFAULT_BASE_URL = "https://api.x.ai/v1";
|
|
6
6
|
export declare const PATH = "/images/generations";
|
|
7
7
|
export declare const EDIT_PATH = "/images/edits";
|
|
8
|
-
/** Provider-native options. Common fields (`n`, `
|
|
8
|
+
/** Provider-native options. Common fields (`n`, `aspect`, `resolution`, `images`) live on the request. */
|
|
9
9
|
export type XAIImageOptions = {
|
|
10
|
-
readonly resolution?: OpenString<"1k" | "2k">;
|
|
11
10
|
readonly responseFormat?: OpenString<"url" | "b64_json">;
|
|
12
11
|
readonly response_format?: OpenString<"url" | "b64_json">;
|
|
13
12
|
} & Record<string, unknown>;
|
|
@@ -5,6 +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 { JsonObject, ProviderShared, lenient, 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: "xai-images", name: "xAI Images", provider: "xai" });
|
|
10
11
|
export const DEFAULT_BASE_URL = "https://api.x.ai/v1";
|
|
@@ -31,6 +32,12 @@ const nativeOptions = (options) => {
|
|
|
31
32
|
const { responseFormat, ...native } = options;
|
|
32
33
|
return { response_format: responseFormat, ...native };
|
|
33
34
|
};
|
|
35
|
+
/** xAI renders `1k` or `2k`; other tiers use the nearest one. */
|
|
36
|
+
const resolution = (request) => {
|
|
37
|
+
if (request.resolution === undefined)
|
|
38
|
+
return undefined;
|
|
39
|
+
return (ImageSizing.tier(request.resolution) ?? 1) < 1.5 ? "1k" : "2k";
|
|
40
|
+
};
|
|
34
41
|
const isEdit = (request) => (request.images?.length ?? 0) > 0;
|
|
35
42
|
const reference = (asset) => ProviderShared.mediaReference(asset, route.provider, route.name).pipe(Effect.map((item) => item.type === "ref" ? { file_id: item.value } : { url: item.value, type: "image_url" }));
|
|
36
43
|
const fromRequest = Effect.fn("XAIImages.fromRequest")(function* (request) {
|
|
@@ -42,7 +49,8 @@ const fromRequest = Effect.fn("XAIImages.fromRequest")(function* (request) {
|
|
|
42
49
|
image: references.length === 1 ? references[0] : undefined,
|
|
43
50
|
images: references.length > 1 ? references : undefined,
|
|
44
51
|
n: request.n,
|
|
45
|
-
aspect_ratio: request.
|
|
52
|
+
aspect_ratio: request.aspect,
|
|
53
|
+
resolution: resolution(request),
|
|
46
54
|
}, nativeOptions(request.providerOptions), request.http?.body) ?? {});
|
|
47
55
|
});
|
|
48
56
|
// ---------------------------------------------------------------------------
|
|
@@ -70,7 +78,6 @@ const decodeResponse = Effect.fn("XAIImages.decodeResponse")(function* (response
|
|
|
70
78
|
// 7. Protocol and route
|
|
71
79
|
// ---------------------------------------------------------------------------
|
|
72
80
|
export const protocol = MediaProtocol.inline(route, {
|
|
73
|
-
unsupported: ["mask", "size", "seed", "format"],
|
|
74
81
|
body: { from: fromRequest },
|
|
75
82
|
response: { decode: decodeResponse },
|
|
76
83
|
});
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
1
2
|
import { Protocol } from "../route/protocol.js";
|
|
2
3
|
import { OpenResponses } from "./open-responses.js";
|
|
4
|
+
declare const Options: Schema.Struct<{
|
|
5
|
+
readonly minP: Schema.optional<Schema.Number>;
|
|
6
|
+
}>;
|
|
7
|
+
export type OptionsInput = typeof Options.Type;
|
|
3
8
|
export declare const protocol: Protocol<{
|
|
4
9
|
readonly model: string;
|
|
5
10
|
readonly input: readonly ({
|
|
@@ -53,6 +58,9 @@ export declare const protocol: Protocol<{
|
|
|
53
58
|
readonly content: readonly ({
|
|
54
59
|
readonly type: "input_text";
|
|
55
60
|
readonly text: string;
|
|
61
|
+
} | {
|
|
62
|
+
readonly type: "input_video";
|
|
63
|
+
readonly video_url: string;
|
|
56
64
|
} | {
|
|
57
65
|
readonly type: "input_image";
|
|
58
66
|
readonly image_url: string;
|
|
@@ -177,6 +185,8 @@ export declare const protocol: Protocol<{
|
|
|
177
185
|
readonly presence_penalty?: number | undefined;
|
|
178
186
|
readonly frequency_penalty?: number | undefined;
|
|
179
187
|
readonly stream: true;
|
|
188
|
+
readonly top_k?: number | undefined;
|
|
189
|
+
readonly min_p?: number | undefined;
|
|
180
190
|
}, string, {
|
|
181
191
|
readonly [x: string]: unknown;
|
|
182
192
|
readonly type: string;
|
|
@@ -21,9 +21,26 @@ const XAIResponsesHostedToolItem = Schema.Union([
|
|
|
21
21
|
error: Schema.optional(Schema.Unknown),
|
|
22
22
|
}), [JsonObject]),
|
|
23
23
|
]);
|
|
24
|
+
const Options = Schema.Struct({
|
|
25
|
+
minP: Schema.optional(Schema.Number),
|
|
26
|
+
});
|
|
27
|
+
const Body = Schema.Struct({
|
|
28
|
+
...OpenResponses.OpenResponsesBody.fields,
|
|
29
|
+
top_k: Schema.optional(Schema.Number),
|
|
30
|
+
min_p: Schema.optional(Schema.Number),
|
|
31
|
+
});
|
|
24
32
|
const adapter = {
|
|
25
33
|
id: ADAPTER,
|
|
26
34
|
name: NAME,
|
|
35
|
+
lowerMedia: ({ part, media }) => {
|
|
36
|
+
if (part.media.kind !== "video")
|
|
37
|
+
return undefined;
|
|
38
|
+
const filename = part.filename ?? "file";
|
|
39
|
+
const url = ProviderShared.mediaUrl(part.media);
|
|
40
|
+
if (url)
|
|
41
|
+
return { type: "input_file", filename, file_url: url };
|
|
42
|
+
return media && { type: "input_file", filename, file_data: media.dataUrl };
|
|
43
|
+
},
|
|
27
44
|
restoreHostedToolItem: (item) => (Schema.is(XAIResponsesHostedToolItem)(item) ? item : undefined),
|
|
28
45
|
};
|
|
29
46
|
const fromRequest = Effect.fn("XAIResponses.fromRequest")(function* (request) {
|
|
@@ -34,7 +51,16 @@ const fromRequest = Effect.fn("XAIResponses.fromRequest")(function* (request) {
|
|
|
34
51
|
route: request.model.route.id,
|
|
35
52
|
message: "xAI requires explicit compaction through LLMClient.compact; automatic context management is not supported",
|
|
36
53
|
});
|
|
37
|
-
|
|
54
|
+
const options = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(Options))(request.providerOptions ?? {});
|
|
55
|
+
const body = yield* OpenResponses.fromRequestWithAdapter(request, adapter);
|
|
56
|
+
// xAI Responses does not support penalties (they are ignored), but does support top_k.
|
|
57
|
+
return {
|
|
58
|
+
...body,
|
|
59
|
+
presence_penalty: undefined,
|
|
60
|
+
frequency_penalty: undefined,
|
|
61
|
+
top_k: request.generation?.topK,
|
|
62
|
+
min_p: options.minP,
|
|
63
|
+
};
|
|
38
64
|
});
|
|
39
65
|
const HOSTED_TOOLS = {
|
|
40
66
|
web_search_call: { name: "web_search", input: (item) => item.action ?? {} },
|
|
@@ -61,7 +87,7 @@ const step = (state, input) => {
|
|
|
61
87
|
export const protocol = Protocol.make({
|
|
62
88
|
id: ADAPTER,
|
|
63
89
|
body: {
|
|
64
|
-
schema:
|
|
90
|
+
schema: Body,
|
|
65
91
|
from: fromRequest,
|
|
66
92
|
},
|
|
67
93
|
stream: {
|
|
@@ -8,7 +8,7 @@ export declare const EDIT_PATH = "/videos/edits";
|
|
|
8
8
|
export declare const EXTEND_PATH = "/videos/extensions";
|
|
9
9
|
export declare const STATUS_PATH = "/videos";
|
|
10
10
|
/**
|
|
11
|
-
* Provider-native options. Common fields (`frames`, `references`, `video`, `durationSeconds`, `
|
|
11
|
+
* Provider-native options. Common fields (`frames`, `references`, `video`, `durationSeconds`, `aspect`,
|
|
12
12
|
* `resolution`, `audio`) live on the request. `mode` selects the endpoint a `video` input is sent to.
|
|
13
13
|
*/
|
|
14
14
|
export type XAIVideoOptions = {
|
|
@@ -65,7 +65,7 @@ const fromRequest = Effect.fn("XAIVideo.fromRequest")(function* (request) {
|
|
|
65
65
|
reference_images: references.length === 0 ? undefined : references,
|
|
66
66
|
video,
|
|
67
67
|
duration: request.durationSeconds,
|
|
68
|
-
aspect_ratio: request.
|
|
68
|
+
aspect_ratio: request.aspect,
|
|
69
69
|
resolution: request.resolution,
|
|
70
70
|
generate_audio: request.audio,
|
|
71
71
|
}, nativeOptions(request.providerOptions), request.http?.body) ?? {});
|
|
@@ -98,12 +98,10 @@ const decodeResult = Effect.fn("XAIVideo.decodeResult")(function* (response, con
|
|
|
98
98
|
}
|
|
99
99
|
if (status !== "completed")
|
|
100
100
|
return yield* output.ended("expired", `${route.name} request ${context.token.requestID} expired`);
|
|
101
|
-
// `respect_moderation: false` marks a filtered result; a URL may still be present, so
|
|
102
|
-
const
|
|
103
|
-
? [{ type: "moderated", message: `${route.name} flagged the generated video for moderation` }]
|
|
104
|
-
: undefined;
|
|
101
|
+
// `respect_moderation: false` marks a filtered result; a URL may still be present, so only a missing one fails.
|
|
102
|
+
const moderated = decoded.video?.respect_moderation === false;
|
|
105
103
|
const url = decoded.video?.url ?? undefined;
|
|
106
|
-
if (url === undefined &&
|
|
104
|
+
if (url === undefined && moderated)
|
|
107
105
|
return yield* output.contentPolicy(`${route.name} withheld the video for moderation`);
|
|
108
106
|
if (url === undefined)
|
|
109
107
|
return yield* output.invalid(`${route.name} completed without a video URL`);
|
|
@@ -115,8 +113,13 @@ const decodeResult = Effect.fn("XAIVideo.decodeResult")(function* (response, con
|
|
|
115
113
|
info: duration === undefined ? undefined : { durationSeconds: duration },
|
|
116
114
|
}),
|
|
117
115
|
],
|
|
118
|
-
|
|
119
|
-
|
|
116
|
+
providerMetadata: {
|
|
117
|
+
xai: {
|
|
118
|
+
requestId: context.token.requestID,
|
|
119
|
+
model: decoded.model ?? undefined,
|
|
120
|
+
respectModeration: decoded.video?.respect_moderation ?? undefined,
|
|
121
|
+
},
|
|
122
|
+
},
|
|
120
123
|
});
|
|
121
124
|
});
|
|
122
125
|
// ---------------------------------------------------------------------------
|