@opencode/ai 2.2.7 → 2.3.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 +114 -96
- package/dist/effect.d.ts +8 -7
- package/dist/effect.js +2 -1
- package/dist/experimental.d.ts +0 -4
- package/dist/experimental.js +0 -3
- package/dist/image-client.d.ts +1 -1
- package/dist/image-client.js +2 -6
- package/dist/index.d.ts +11 -48
- package/dist/index.js +30 -13
- package/dist/media-model.d.ts +2 -0
- package/dist/media-model.js +3 -1
- package/dist/media.d.ts +9 -3
- package/dist/media.js +27 -7
- package/dist/protocols/google-decisions.d.ts +13 -0
- package/dist/protocols/google-decisions.js +133 -0
- package/dist/protocols/google-interactions-video.d.ts +43 -0
- package/dist/protocols/google-interactions-video.js +186 -0
- package/dist/protocols/google-interactions.d.ts +14 -1
- package/dist/protocols/google-interactions.js +1 -1
- package/dist/protocols/index.d.ts +1 -0
- package/dist/protocols/index.js +1 -0
- package/dist/protocols/minimax-video.d.ts +54 -0
- package/dist/protocols/minimax-video.js +292 -0
- package/dist/protocols/openrouter-video.d.ts +41 -0
- package/dist/protocols/openrouter-video.js +138 -0
- package/dist/protocols/seedance-video.d.ts +50 -0
- package/dist/protocols/seedance-video.js +216 -0
- package/dist/protocols/system-one.d.ts +45 -1
- package/dist/protocols/system-one.js +42 -7
- package/dist/protocols/utils/responses-compaction.js +1 -1
- package/dist/protocols/xai-video.d.ts +8 -2
- package/dist/protocols/xai-video.js +20 -15
- package/dist/provider-error.js +7 -1
- package/dist/providers/byteplus/videos.d.ts +2 -0
- package/dist/providers/byteplus/videos.js +1 -0
- package/dist/providers/byteplus.d.ts +26 -0
- package/dist/providers/byteplus.js +21 -0
- package/dist/providers/google/decisions.d.ts +2 -0
- package/dist/providers/google/decisions.js +1 -0
- package/dist/providers/google/videos.d.ts +2 -0
- package/dist/providers/google/videos.js +1 -0
- package/dist/providers/google.d.ts +11 -4
- package/dist/providers/google.js +13 -3
- package/dist/providers/index.d.ts +2 -1
- package/dist/providers/index.js +2 -1
- package/dist/providers/minimax/videos.d.ts +3 -0
- package/dist/providers/minimax/videos.js +1 -0
- package/dist/providers/minimax.d.ts +6 -1
- package/dist/providers/minimax.js +10 -1
- package/dist/providers/openai-compatible/decisions.d.ts +2 -0
- package/dist/providers/openai-compatible/decisions.js +1 -0
- package/dist/providers/openai-compatible.d.ts +2 -0
- package/dist/providers/openai-compatible.js +14 -0
- package/dist/providers/openrouter/videos.d.ts +2 -0
- package/dist/providers/openrouter/videos.js +1 -0
- package/dist/providers/openrouter.d.ts +5 -0
- package/dist/providers/openrouter.js +8 -0
- package/dist/providers/volcengine/videos.d.ts +2 -0
- package/dist/providers/volcengine/videos.js +1 -0
- package/dist/providers/volcengine.d.ts +26 -0
- package/dist/providers/volcengine.js +21 -0
- package/dist/public.d.ts +2 -0
- package/dist/public.js +1 -0
- package/dist/route/executor-service.d.ts +6 -0
- package/dist/route/executor-service.js +7 -0
- package/dist/route/executor.d.ts +7 -1
- package/dist/route/executor.js +7 -2
- package/dist/route/media-protocol.d.ts +12 -11
- package/dist/route/media.d.ts +2 -0
- package/dist/route/media.js +31 -22
- package/dist/route/transport/http.js +1 -1
- package/dist/schema/messages.d.ts +1 -1
- package/dist/schema/messages.js +1 -5
- package/dist/schema/options.d.ts +8 -0
- package/dist/schema/options.js +1 -0
- package/dist/speech-client.d.ts +1 -1
- package/dist/speech-client.js +4 -4
- package/dist/transcription-client.d.ts +1 -1
- package/dist/transcription-client.js +1 -2
- package/dist/video-client.d.ts +16 -84
- package/dist/video-client.js +28 -13
- package/dist/video.d.ts +56 -1072
- package/dist/video.js +28 -65
- package/package.json +55 -7
- package/dist/protocols/google-video.d.ts +0 -26
- package/dist/protocols/google-video.js +0 -146
- package/dist/protocols/runway-video.d.ts +0 -38
- package/dist/protocols/runway-video.js +0 -141
- package/dist/providers/runway.d.ts +0 -20
- package/dist/providers/runway.js +0 -17
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { DecisionRoute } from "../route/decision.js";
|
|
3
|
+
import { type ProviderOptions } from "../schema/index.js";
|
|
4
|
+
export declare const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
5
|
+
declare const Options: Schema.Struct<{
|
|
6
|
+
/** Token budget for every image and video input; `ultra_high` applies to images only. */
|
|
7
|
+
readonly mediaResolution: Schema.optional<Schema.declare<import("../schema/options.js").OpenString<"high" | "low" | "medium" | "ultra_high">, import("../schema/options.js").OpenString<"high" | "low" | "medium" | "ultra_high">>>;
|
|
8
|
+
}>;
|
|
9
|
+
/** Known fields map to their wire names; anything else is sent as given. */
|
|
10
|
+
export type OptionsInput = ProviderOptions & typeof Options.Encoded;
|
|
11
|
+
export declare const protocol: DecisionRoute.Protocol;
|
|
12
|
+
export declare const route: DecisionRoute.Route;
|
|
13
|
+
export * as GoogleDecisions from "./google-decisions.js";
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { DecisionResponse, DecisionUsage } from "../decision.js";
|
|
3
|
+
import { DecisionRoute } from "../route/decision.js";
|
|
4
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
5
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
6
|
+
import { AIError, ProviderID, UnsupportedOperationError, mergeJsonRecords, } from "../schema/index.js";
|
|
7
|
+
import { encodeJson } from "../utils/json.js";
|
|
8
|
+
import { JsonObject, knownString, ProviderShared } from "./shared.js";
|
|
9
|
+
import { SystemOne } from "./system-one.js";
|
|
10
|
+
import { MediaInput } from "./utils/media-input.js";
|
|
11
|
+
const ID = "google-decisions";
|
|
12
|
+
const NAME = "Google Decisions";
|
|
13
|
+
export const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
14
|
+
const Options = Schema.Struct({
|
|
15
|
+
/** Token budget for every image and video input; `ultra_high` applies to images only. */
|
|
16
|
+
mediaResolution: Schema.optional(knownString()),
|
|
17
|
+
});
|
|
18
|
+
const Usage = Schema.StructWithRest(Schema.Struct({
|
|
19
|
+
total_input_tokens: Schema.optional(Schema.Number),
|
|
20
|
+
total_output_tokens: Schema.optional(Schema.Number),
|
|
21
|
+
total_tokens: Schema.optional(Schema.Number),
|
|
22
|
+
}), [JsonObject]);
|
|
23
|
+
const Response = Schema.Struct({
|
|
24
|
+
answers: Schema.Record(Schema.String, SystemOne.Answer),
|
|
25
|
+
usage: Schema.optional(Usage),
|
|
26
|
+
});
|
|
27
|
+
const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options));
|
|
28
|
+
const fromRequest = Effect.fn("GoogleDecisions.fromRequest")(function* (request) {
|
|
29
|
+
const options = yield* decodeOptions(request.providerOptions ?? {});
|
|
30
|
+
const { mediaResolution: _, ...native } = request.providerOptions ?? {};
|
|
31
|
+
return {
|
|
32
|
+
...mergeJsonRecords(native, request.http?.body),
|
|
33
|
+
model: request.model.id,
|
|
34
|
+
input: separateText(yield* Effect.forEach(request.input, (part) => inputPart(request, part, options.mediaResolution))),
|
|
35
|
+
questions: {
|
|
36
|
+
...(yield* SystemOne.encodeQuestions(request.questions)),
|
|
37
|
+
// Google names System One's predicate criteria `true_description` and `false_description`.
|
|
38
|
+
...Object.fromEntries(request.questions.flatMap((question) => question.type === "predicate" && question.criteria !== undefined
|
|
39
|
+
? [
|
|
40
|
+
[
|
|
41
|
+
question.name,
|
|
42
|
+
{
|
|
43
|
+
type: "noul",
|
|
44
|
+
instructions: question.instructions,
|
|
45
|
+
criteria: {
|
|
46
|
+
...(question.criteria.true === undefined ? {} : { true_description: question.criteria.true }),
|
|
47
|
+
...(question.criteria.false === undefined ? {} : { false_description: question.criteria.false }),
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
]
|
|
52
|
+
: [])),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
const inputPart = (request, part, resolution) => {
|
|
57
|
+
if (part.type === "text")
|
|
58
|
+
return Effect.succeed({ type: "text", text: part.text });
|
|
59
|
+
// Google reads every text block as text; JSON is serialized so its keys are never parsed as content blocks.
|
|
60
|
+
if (part.type === "json")
|
|
61
|
+
return Effect.succeed({ type: "text", text: encodeJson(part.value) });
|
|
62
|
+
return mediaPart(request, part.media, resolution);
|
|
63
|
+
};
|
|
64
|
+
// Google joins adjacent text blocks with no separator, so each one after another starts on a new line.
|
|
65
|
+
const separateText = (blocks) => blocks.map((block, index) => block.type === "text" && blocks[index - 1]?.type === "text" ? { ...block, text: `\n${block.text}` } : block);
|
|
66
|
+
const mediaPart = (request, asset, resolution) => {
|
|
67
|
+
const type = asset.kind === "image" || asset.kind === "audio" || asset.kind === "video"
|
|
68
|
+
? asset.kind
|
|
69
|
+
: asset.mediaType.toLowerCase() === "application/pdf"
|
|
70
|
+
? "document"
|
|
71
|
+
: undefined;
|
|
72
|
+
if (type === undefined)
|
|
73
|
+
return Effect.fail(new AIError({
|
|
74
|
+
reason: new UnsupportedOperationError({
|
|
75
|
+
operation: "decision.media",
|
|
76
|
+
provider: request.model.provider,
|
|
77
|
+
route: request.model.route.id,
|
|
78
|
+
message: `${NAME} takes images, audio, video and PDFs; got ${asset.mediaType}`,
|
|
79
|
+
}),
|
|
80
|
+
}));
|
|
81
|
+
const block = {
|
|
82
|
+
type,
|
|
83
|
+
mime_type: asset.mediaType.toLowerCase(),
|
|
84
|
+
// `ultra_high` is image-only, so video keeps Google's default for it.
|
|
85
|
+
...(resolution !== undefined && (type === "image" || (type === "video" && resolution !== "ultra_high"))
|
|
86
|
+
? { resolution }
|
|
87
|
+
: {}),
|
|
88
|
+
};
|
|
89
|
+
const inline = asset.inline();
|
|
90
|
+
if (inline)
|
|
91
|
+
return Effect.succeed({ ...block, data: inline.base64 });
|
|
92
|
+
// Google fetches URLs itself, so one that needs our download credentials can't be forwarded.
|
|
93
|
+
if (asset.source.type === "url" && /^https:\/\//i.test(asset.source.url) && asset.headers === undefined)
|
|
94
|
+
return Effect.succeed({ ...block, uri: asset.source.url });
|
|
95
|
+
const file = MediaInput.refID(asset, ProviderID.make("google"));
|
|
96
|
+
if (file !== undefined)
|
|
97
|
+
return Effect.succeed({ ...block, uri: file });
|
|
98
|
+
return Effect.fail(ProviderShared.invalidRequest(`${NAME} accepts inline media, public HTTPS URLs and Google Files API URIs; materialize ${asset.source.type} media first`));
|
|
99
|
+
};
|
|
100
|
+
const decodeDocument = MediaProtocol.identity({ id: ID, name: NAME, provider: "google" }).decodeJson(Response);
|
|
101
|
+
export const protocol = DecisionRoute.protocol({
|
|
102
|
+
id: ID,
|
|
103
|
+
name: NAME,
|
|
104
|
+
body: { from: fromRequest },
|
|
105
|
+
response: {
|
|
106
|
+
decode: Effect.fn("GoogleDecisions.decodeResponse")(function* (response, context) {
|
|
107
|
+
const output = yield* decodeDocument(response);
|
|
108
|
+
const data = output.value;
|
|
109
|
+
const answers = data.answers;
|
|
110
|
+
const names = context.request.questions.map((question) => question.name);
|
|
111
|
+
if (Object.keys(answers).length !== names.length || !names.every((name) => Object.hasOwn(answers, name)))
|
|
112
|
+
return yield* output.invalid(`${NAME} answers do not match the requested questions`);
|
|
113
|
+
return new DecisionResponse({
|
|
114
|
+
answers: context.request.questions.map((question) => SystemOne.toAnswer(question, answers[question.name])),
|
|
115
|
+
usage: data.usage === undefined
|
|
116
|
+
? undefined
|
|
117
|
+
: new DecisionUsage({
|
|
118
|
+
inputTokens: data.usage.total_input_tokens,
|
|
119
|
+
outputTokens: data.usage.total_output_tokens,
|
|
120
|
+
totalTokens: ProviderShared.totalTokens(data.usage.total_input_tokens, data.usage.total_output_tokens, data.usage.total_tokens),
|
|
121
|
+
}),
|
|
122
|
+
providerMetadata: data.usage === undefined ? undefined : { [context.providerMetadataKey]: { usage: data.usage } },
|
|
123
|
+
});
|
|
124
|
+
}),
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
export const route = DecisionRoute.make({
|
|
128
|
+
id: ID,
|
|
129
|
+
provider: "google",
|
|
130
|
+
protocol,
|
|
131
|
+
endpoint: Endpoint.path("/decisions", { baseURL: DEFAULT_BASE_URL }),
|
|
132
|
+
});
|
|
133
|
+
export * as GoogleDecisions from "./google-decisions.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
import { type OpenString } from "../schema/index.js";
|
|
5
|
+
import { VideoModel, VideoResponse, type VideoRequest } from "../video.js";
|
|
6
|
+
export declare const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
7
|
+
export declare const PATH = "/interactions";
|
|
8
|
+
/**
|
|
9
|
+
* Provider-native Interactions fields, merged into the body. Common fields (`start`, `end`, `references`,
|
|
10
|
+
* `duration`, `aspect`, `resolution`) live on the request. Edit or extend a video by passing it as a reference with
|
|
11
|
+
* `generation_config.video_config.task`, or continue a generated one with `previous_interaction_id`.
|
|
12
|
+
*/
|
|
13
|
+
export type GoogleVideoOptions = {
|
|
14
|
+
readonly previous_interaction_id?: string;
|
|
15
|
+
readonly generation_config?: {
|
|
16
|
+
readonly video_config?: {
|
|
17
|
+
readonly task?: OpenString<"text_to_video" | "image_to_video" | "reference_to_video" | "edit" | "extend">;
|
|
18
|
+
} & Record<string, unknown>;
|
|
19
|
+
} & Record<string, unknown>;
|
|
20
|
+
readonly response_format?: {
|
|
21
|
+
readonly delivery?: OpenString<"inline" | "uri">;
|
|
22
|
+
} & Record<string, unknown>;
|
|
23
|
+
readonly service_tier?: OpenString<"flex" | "standard" | "priority">;
|
|
24
|
+
readonly webhook_config?: {
|
|
25
|
+
readonly uris?: ReadonlyArray<string>;
|
|
26
|
+
readonly user_metadata?: Record<string, unknown>;
|
|
27
|
+
};
|
|
28
|
+
} & Record<string, unknown>;
|
|
29
|
+
export type Request = VideoRequest<VideoModel<GoogleVideoOptions>>;
|
|
30
|
+
export declare const Token: Schema.Struct<{
|
|
31
|
+
readonly interactionID: Schema.String;
|
|
32
|
+
}>;
|
|
33
|
+
export type Token = Schema.Schema.Type<typeof Token>;
|
|
34
|
+
export declare const protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
35
|
+
readonly interactionID: string;
|
|
36
|
+
}>;
|
|
37
|
+
export declare const model: (input: MediaRoute.ModelInput) => VideoModel<GoogleVideoOptions>;
|
|
38
|
+
export declare const GoogleInteractionsVideo: {
|
|
39
|
+
readonly protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
40
|
+
readonly interactionID: string;
|
|
41
|
+
}>;
|
|
42
|
+
readonly model: typeof model;
|
|
43
|
+
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Media } from "../media.js";
|
|
3
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
+
import { MediaRoute } from "../route/media.js";
|
|
5
|
+
import { mergeJsonRecords, tokenUsage } from "../schema/index.js";
|
|
6
|
+
import { VideoModel, VideoResponse, seconds } from "../video.js";
|
|
7
|
+
import { GoogleInteractions } from "./google-interactions.js";
|
|
8
|
+
import { optionalNull } from "./shared.js";
|
|
9
|
+
import { GeminiGenerateContent } from "./utils/gemini-generate-content.js";
|
|
10
|
+
import { MediaInput } from "./utils/media-input.js";
|
|
11
|
+
const route = MediaProtocol.identity({
|
|
12
|
+
id: "google-interactions-video",
|
|
13
|
+
name: "Google Interactions Video",
|
|
14
|
+
provider: "google",
|
|
15
|
+
});
|
|
16
|
+
export const DEFAULT_BASE_URL = GoogleInteractions.DEFAULT_BASE_URL;
|
|
17
|
+
export const PATH = "/interactions";
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// 2. Token and response schemas
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
export const Token = Schema.Struct({ interactionID: Schema.String });
|
|
22
|
+
const Content = Schema.Struct({
|
|
23
|
+
type: Schema.String,
|
|
24
|
+
data: optionalNull(Schema.String),
|
|
25
|
+
uri: optionalNull(Schema.String),
|
|
26
|
+
mime_type: optionalNull(Schema.String),
|
|
27
|
+
text: optionalNull(Schema.String),
|
|
28
|
+
});
|
|
29
|
+
const Usage = Schema.StructWithRest(Schema.Struct({
|
|
30
|
+
total_input_tokens: optionalNull(Schema.Number),
|
|
31
|
+
total_output_tokens: optionalNull(Schema.Number),
|
|
32
|
+
total_thought_tokens: optionalNull(Schema.Number),
|
|
33
|
+
total_tokens: optionalNull(Schema.Number),
|
|
34
|
+
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
35
|
+
const Interaction = Schema.Struct({
|
|
36
|
+
id: Schema.String,
|
|
37
|
+
status: Schema.String,
|
|
38
|
+
model: optionalNull(Schema.String),
|
|
39
|
+
steps: optionalNull(Schema.Array(Schema.Struct({ type: Schema.String, content: optionalNull(Schema.Array(Content)) }))),
|
|
40
|
+
usage: optionalNull(Usage),
|
|
41
|
+
errors: optionalNull(Schema.Array(Schema.Struct({ code: optionalNull(Schema.String), message: optionalNull(Schema.String) }))),
|
|
42
|
+
});
|
|
43
|
+
// `incomplete` is terminal ("completed, but contains incomplete results"); the result read decides whether a video
|
|
44
|
+
// came back. `requires_action` and `requires_continuation` are not documented for video and stay running.
|
|
45
|
+
const STATUS = {
|
|
46
|
+
in_progress: "running",
|
|
47
|
+
completed: "completed",
|
|
48
|
+
incomplete: "completed",
|
|
49
|
+
failed: "failed",
|
|
50
|
+
cancelled: "cancelled",
|
|
51
|
+
};
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// 3. Failures
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Interaction error codes are documented only as "a URI that identifies the error type"; the last segment carries the
|
|
56
|
+
// canonical Google status name.
|
|
57
|
+
const FAILURE = {
|
|
58
|
+
INVALID_ARGUMENT: "InvalidRequest",
|
|
59
|
+
FAILED_PRECONDITION: "InvalidRequest",
|
|
60
|
+
OUT_OF_RANGE: "InvalidRequest",
|
|
61
|
+
PERMISSION_DENIED: "Authentication",
|
|
62
|
+
UNAUTHENTICATED: "Authentication",
|
|
63
|
+
RESOURCE_EXHAUSTED: "RateLimit",
|
|
64
|
+
};
|
|
65
|
+
const errorCode = (code) => code?.split("/").at(-1)?.trim().toUpperCase().replaceAll("-", "_") || undefined;
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// 5. Request body construction
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Omni has no role field for media: it reads one image as the start frame and two as start and end. The task hint
|
|
70
|
+
// pins that reading when only frames, or only references, are sent; with both, the prompt decides.
|
|
71
|
+
const task = (request) => {
|
|
72
|
+
const frames = request.start !== undefined || request.end !== undefined;
|
|
73
|
+
const references = (request.references ?? []).length > 0;
|
|
74
|
+
if (frames && !references)
|
|
75
|
+
return "image_to_video";
|
|
76
|
+
if (references && !frames)
|
|
77
|
+
return "reference_to_video";
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
80
|
+
const fromRequest = Effect.fn("GoogleInteractionsVideo.fromRequest")(function* (request) {
|
|
81
|
+
if (request.end !== undefined && request.start === undefined)
|
|
82
|
+
return yield* route.unsupported("video.end", `${route.name} reads a lone image as the start frame; send start too`);
|
|
83
|
+
if (request.sound === false)
|
|
84
|
+
return yield* route.unsupported("video.sound", `${route.name} always generates audio and has no option to turn it off`);
|
|
85
|
+
const media = yield* Effect.forEach([request.start, request.end, ...(request.references ?? [])].filter((asset) => asset !== undefined), GoogleInteractions.mediaContent);
|
|
86
|
+
const responseFormat = {
|
|
87
|
+
// `auto` follows the input image, which Omni expresses by leaving the ratio out.
|
|
88
|
+
aspect_ratio: request.aspect === "auto" ? undefined : request.aspect,
|
|
89
|
+
resolution: request.resolution?.toLowerCase(),
|
|
90
|
+
duration: request.duration === undefined ? undefined : `${seconds(request.duration)}s`,
|
|
91
|
+
};
|
|
92
|
+
const videoTask = task(request);
|
|
93
|
+
return MediaProtocol.json(mergeJsonRecords({
|
|
94
|
+
model: request.model.id,
|
|
95
|
+
input: [...media, { type: "text", text: request.prompt }],
|
|
96
|
+
background: true,
|
|
97
|
+
response_format: Object.values(responseFormat).some((value) => value !== undefined)
|
|
98
|
+
? { type: "video", ...responseFormat }
|
|
99
|
+
: undefined,
|
|
100
|
+
generation_config: videoTask === undefined ? undefined : { video_config: { task: videoTask } },
|
|
101
|
+
}, request.providerOptions, request.http?.body) ?? {});
|
|
102
|
+
});
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// 6. Response decoding
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
const decodeStart = route.decodeStarted(Interaction, (value) => ({
|
|
107
|
+
token: { interactionID: value.id },
|
|
108
|
+
snapshot: { id: value.id, status: MediaProtocol.status(STATUS, value.status) },
|
|
109
|
+
}));
|
|
110
|
+
const decodeInteraction = route.decodeJson(Interaction);
|
|
111
|
+
const decodeStatus = Effect.fn("GoogleInteractionsVideo.decodeStatus")(function* (response, context) {
|
|
112
|
+
const output = yield* decodeInteraction(response);
|
|
113
|
+
return { id: context.token.interactionID, status: MediaProtocol.status(STATUS, output.value.status) };
|
|
114
|
+
});
|
|
115
|
+
const decodeResult = Effect.fn("GoogleInteractionsVideo.decodeResult")(function* (response, context) {
|
|
116
|
+
const output = yield* decodeInteraction(response);
|
|
117
|
+
const decoded = output.value;
|
|
118
|
+
const status = MediaProtocol.status(STATUS, decoded.status);
|
|
119
|
+
if (status === "running")
|
|
120
|
+
return yield* output.pending(context.token.interactionID);
|
|
121
|
+
const errors = decoded.errors ?? [];
|
|
122
|
+
const codes = errors.flatMap((error) => {
|
|
123
|
+
const code = errorCode(error.code);
|
|
124
|
+
return code === undefined ? [] : [code];
|
|
125
|
+
});
|
|
126
|
+
const messages = errors.flatMap((error) => (error.message?.trim() ? [error.message.trim()] : []));
|
|
127
|
+
if (status === "cancelled")
|
|
128
|
+
return yield* output.ended("cancelled", `${route.name} interaction ${context.token.interactionID} was cancelled`);
|
|
129
|
+
if (status === "failed") {
|
|
130
|
+
const message = `${route.name} generation failed${codes.length === 0 ? "" : ` (${codes.join(", ")})`}${messages.length === 0 ? "" : `: ${messages.join("\n")}`}`;
|
|
131
|
+
if (codes.some((code) => code === "CONTENT_BLOCKED" || GeminiGenerateContent.contentFiltered(code)))
|
|
132
|
+
return yield* output.contentPolicy(message);
|
|
133
|
+
return yield* output.ended("failed", message, MediaProtocol.failure(FAILURE, codes[0]));
|
|
134
|
+
}
|
|
135
|
+
const contents = (decoded.steps ?? []).flatMap((step) => (step.type === "model_output" ? (step.content ?? []) : []));
|
|
136
|
+
const videos = yield* Effect.forEach(contents.filter((content) => content.type === "video"), (content, index) => {
|
|
137
|
+
const mediaType = content.mime_type ?? undefined;
|
|
138
|
+
if (content.data)
|
|
139
|
+
return MediaInput.decodedAsset(output.invalid, `${route.name} video ${index}`, content.data, mediaType);
|
|
140
|
+
// `GET /interactions/{id}` documents inline data even for `delivery: "uri"`; a Files API URI needs the API key
|
|
141
|
+
// to download, so it stays a Google reference that can be passed back as input.
|
|
142
|
+
if (content.uri)
|
|
143
|
+
return Effect.succeed(Media.ref(route.provider, content.uri, mediaType));
|
|
144
|
+
return Effect.fail(output.invalid(`${route.name} returned a video without data or a URI`));
|
|
145
|
+
});
|
|
146
|
+
if (videos.length === 0) {
|
|
147
|
+
if (codes.some((code) => code === "CONTENT_BLOCKED" || GeminiGenerateContent.contentFiltered(code)))
|
|
148
|
+
return yield* output.contentPolicy(`${route.name} withheld the video${messages.length === 0 ? "" : `: ${messages.join("\n")}`}`);
|
|
149
|
+
return yield* output.invalid(`${route.name} ${decoded.status} without a video`);
|
|
150
|
+
}
|
|
151
|
+
const usage = decoded.usage ?? undefined;
|
|
152
|
+
return new VideoResponse({
|
|
153
|
+
videos,
|
|
154
|
+
usage: usage === undefined
|
|
155
|
+
? undefined
|
|
156
|
+
: tokenUsage({
|
|
157
|
+
input: usage.total_input_tokens ?? undefined,
|
|
158
|
+
output: usage.total_output_tokens ?? undefined,
|
|
159
|
+
total: usage.total_tokens ?? undefined,
|
|
160
|
+
details: { reasoningTokens: usage.total_thought_tokens ?? undefined, google: usage },
|
|
161
|
+
}),
|
|
162
|
+
providerMetadata: {
|
|
163
|
+
google: {
|
|
164
|
+
interactionId: decoded.id,
|
|
165
|
+
model: decoded.model ?? undefined,
|
|
166
|
+
status: decoded.status,
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
// 7. Protocol and route
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
const interactionPath = (token) => `${PATH}/${token.interactionID}`;
|
|
175
|
+
export const protocol = MediaProtocol.queued(route, {
|
|
176
|
+
token: Token,
|
|
177
|
+
start: { body: { from: fromRequest }, decode: decodeStart },
|
|
178
|
+
status: { path: interactionPath, decode: decodeStatus },
|
|
179
|
+
result: { path: interactionPath, decode: decodeResult },
|
|
180
|
+
cancel: { method: "POST", path: (token) => `${interactionPath(token)}/cancel` },
|
|
181
|
+
});
|
|
182
|
+
export const model = (input) => VideoModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: PATH }, input);
|
|
183
|
+
export const GoogleInteractionsVideo = {
|
|
184
|
+
protocol,
|
|
185
|
+
model,
|
|
186
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { Schema } from "effect";
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import { Route } from "../route/client.js";
|
|
3
3
|
import { Protocol } from "../route/protocol.js";
|
|
4
|
+
import { AIError } from "../schema/index.js";
|
|
5
|
+
import { Media } from "../media.js";
|
|
4
6
|
import { Lifecycle } from "./utils/lifecycle.js";
|
|
5
7
|
import { ToolStream } from "./utils/tool-stream.js";
|
|
6
8
|
export declare const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
@@ -36,6 +38,17 @@ interface ParserState {
|
|
|
36
38
|
readonly steps: Partial<Record<number, OutputStep>>;
|
|
37
39
|
readonly tools: ToolStream.State<number>;
|
|
38
40
|
}
|
|
41
|
+
export declare const mediaContent: (asset: Media.Asset) => Effect.Effect<{
|
|
42
|
+
type: "audio" | "document" | "image" | "video";
|
|
43
|
+
uri: string;
|
|
44
|
+
mime_type: string;
|
|
45
|
+
data?: undefined;
|
|
46
|
+
} | {
|
|
47
|
+
uri?: undefined;
|
|
48
|
+
type: "audio" | "document" | "image" | "video";
|
|
49
|
+
data: string;
|
|
50
|
+
mime_type: string;
|
|
51
|
+
}, AIError, never>;
|
|
39
52
|
export declare const protocol: Protocol<{
|
|
40
53
|
readonly model: string;
|
|
41
54
|
readonly input: readonly ({
|
|
@@ -172,7 +172,7 @@ const Event = Schema.Union([
|
|
|
172
172
|
// =============================================================================
|
|
173
173
|
// Request Body Construction
|
|
174
174
|
// =============================================================================
|
|
175
|
-
const mediaContent = Effect.fnUntraced(function* (asset) {
|
|
175
|
+
export const mediaContent = Effect.fnUntraced(function* (asset) {
|
|
176
176
|
if (asset.kind !== "image" &&
|
|
177
177
|
asset.kind !== "audio" &&
|
|
178
178
|
asset.kind !== "video" &&
|
|
@@ -3,6 +3,7 @@ export * as BedrockConverse from "./bedrock-converse.js";
|
|
|
3
3
|
export * as CloudflareRunDecisions from "./cloudflare-run-decisions.js";
|
|
4
4
|
export * as CohereChat from "./cohere-chat.js";
|
|
5
5
|
export * as Gemini from "./gemini.js";
|
|
6
|
+
export * as GoogleDecisions from "./google-decisions.js";
|
|
6
7
|
export * as GoogleEmbedContent from "./google-embed-content.js";
|
|
7
8
|
export * as GoogleInteractions from "./google-interactions.js";
|
|
8
9
|
export * as GoogleVertexPredictEmbeddings from "./google-vertex-predict-embeddings.js";
|
package/dist/protocols/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * as BedrockConverse from "./bedrock-converse.js";
|
|
|
3
3
|
export * as CloudflareRunDecisions from "./cloudflare-run-decisions.js";
|
|
4
4
|
export * as CohereChat from "./cohere-chat.js";
|
|
5
5
|
export * as Gemini from "./gemini.js";
|
|
6
|
+
export * as GoogleDecisions from "./google-decisions.js";
|
|
6
7
|
export * as GoogleEmbedContent from "./google-embed-content.js";
|
|
7
8
|
export * as GoogleInteractions from "./google-interactions.js";
|
|
8
9
|
export * as GoogleVertexPredictEmbeddings from "./google-vertex-predict-embeddings.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
import { type OpenString } from "../schema/index.js";
|
|
5
|
+
import { VideoModel, VideoResponse, type VideoRequest } from "../video.js";
|
|
6
|
+
/** The API host without a version: H3 is served under `/v2` and Hailuo under `/v1`. */
|
|
7
|
+
export declare const DEFAULT_BASE_URL = "https://api.minimax.io";
|
|
8
|
+
export declare const PATH = "/v2/video_generation";
|
|
9
|
+
export declare const STATUS_PATH = "/v2/query/video_generation";
|
|
10
|
+
export declare const HAILUO_PATH = "/v1/video_generation";
|
|
11
|
+
export declare const HAILUO_STATUS_PATH = "/v1/query/video_generation";
|
|
12
|
+
export declare const FILE_PATH = "/v1/files/retrieve";
|
|
13
|
+
/**
|
|
14
|
+
* Provider-native options, merged into the body. Common fields (`start`, `end`, `references`, `duration`, `aspect`,
|
|
15
|
+
* `resolution`, `sound`) live on the request. `extra` and `response_format` are H3 (v2) fields; `prompt_optimizer`
|
|
16
|
+
* and `fast_pretreatment` are Hailuo (v1) fields; both APIs take `callback_url`.
|
|
17
|
+
*/
|
|
18
|
+
export type MiniMaxVideoOptions = {
|
|
19
|
+
readonly extra?: {
|
|
20
|
+
readonly prompt_expansion_mode?: OpenString<"disabled" | "balanced" | "quality">;
|
|
21
|
+
};
|
|
22
|
+
readonly response_format?: OpenString<"task" | "flat">;
|
|
23
|
+
readonly prompt_optimizer?: boolean;
|
|
24
|
+
readonly fast_pretreatment?: boolean;
|
|
25
|
+
readonly callback_url?: string;
|
|
26
|
+
} & Record<string, unknown>;
|
|
27
|
+
export type Request = VideoRequest<VideoModel<MiniMaxVideoOptions>>;
|
|
28
|
+
export declare const Token: Schema.Struct<{
|
|
29
|
+
readonly taskID: Schema.String;
|
|
30
|
+
}>;
|
|
31
|
+
export type Token = Schema.Schema.Type<typeof Token>;
|
|
32
|
+
export declare const protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
33
|
+
readonly taskID: string;
|
|
34
|
+
}>;
|
|
35
|
+
export declare const hailuoProtocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
36
|
+
readonly taskID: string;
|
|
37
|
+
}>;
|
|
38
|
+
/** H3 and later models on the v2 API. */
|
|
39
|
+
export declare const h3Model: (input: MediaRoute.ModelInput) => VideoModel<MiniMaxVideoOptions>;
|
|
40
|
+
/** Hailuo and the `*-01` models on the v1 API. */
|
|
41
|
+
export declare const hailuoModel: (input: MediaRoute.ModelInput) => VideoModel<MiniMaxVideoOptions>;
|
|
42
|
+
/** Picks the API by model family: Hailuo and `*-01` models use v1, everything else v2. */
|
|
43
|
+
export declare const model: (input: MediaRoute.ModelInput) => VideoModel<MiniMaxVideoOptions>;
|
|
44
|
+
export declare const MiniMaxVideo: {
|
|
45
|
+
readonly protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
46
|
+
readonly taskID: string;
|
|
47
|
+
}>;
|
|
48
|
+
readonly hailuoProtocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
49
|
+
readonly taskID: string;
|
|
50
|
+
}>;
|
|
51
|
+
readonly model: typeof model;
|
|
52
|
+
readonly h3Model: typeof h3Model;
|
|
53
|
+
readonly hailuoModel: typeof hailuoModel;
|
|
54
|
+
};
|