重构主控编排与运行时预热链路,统一工作区提示词/专家调度协议并补齐 wiki 记忆注入与写回闭环。

同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。

Made-with: Cursor
This commit is contained in:
oliver 2026-04-26 08:34:33 +08:00
parent 4a23b715a2
commit dbbe3add6a
14438 changed files with 2693620 additions and 2546 deletions

View file

@ -0,0 +1,8 @@
export { buildBytePlusCodingProvider, buildBytePlusProvider } from "./provider-catalog.js";
export {
buildBytePlusModelDefinition,
BYTEPLUS_BASE_URL,
BYTEPLUS_CODING_BASE_URL,
BYTEPLUS_CODING_MODEL_CATALOG,
BYTEPLUS_MODEL_CATALOG,
} from "./models.js";

View file

@ -0,0 +1,47 @@
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
import { registerSingleProviderPlugin } from "../../test/helpers/plugins/plugin-registration.js";
import plugin from "./index.js";
import { BYTEPLUS_CODING_MODEL_CATALOG, BYTEPLUS_MODEL_CATALOG } from "./models.js";
describe("byteplus plugin", () => {
it("augments the catalog with bundled standard and plan models", async () => {
const provider = await registerSingleProviderPlugin(plugin);
const entries = await provider.augmentModelCatalog?.({
env: process.env,
entries: [],
} as never);
expect(entries).toContainEqual(
expect.objectContaining({
provider: "byteplus",
id: BYTEPLUS_MODEL_CATALOG[0].id,
name: BYTEPLUS_MODEL_CATALOG[0].name,
reasoning: BYTEPLUS_MODEL_CATALOG[0].reasoning,
input: [...BYTEPLUS_MODEL_CATALOG[0].input],
contextWindow: BYTEPLUS_MODEL_CATALOG[0].contextWindow,
}),
);
expect(entries).toContainEqual(
expect.objectContaining({
provider: "byteplus-plan",
id: BYTEPLUS_CODING_MODEL_CATALOG[0].id,
name: BYTEPLUS_CODING_MODEL_CATALOG[0].name,
reasoning: BYTEPLUS_CODING_MODEL_CATALOG[0].reasoning,
input: [...BYTEPLUS_CODING_MODEL_CATALOG[0].input],
contextWindow: BYTEPLUS_CODING_MODEL_CATALOG[0].contextWindow,
}),
);
});
it("declares its coding provider auth alias in the manifest", () => {
const pluginJson = JSON.parse(
readFileSync(resolve(import.meta.dirname, "openclaw.plugin.json"), "utf-8"),
);
expect(pluginJson.providerAuthAliases).toEqual({
"byteplus-plan": "byteplus",
});
});
});

View file

@ -0,0 +1,84 @@
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import { ensureModelAllowlistEntry } from "openclaw/plugin-sdk/provider-onboard";
import { BYTEPLUS_CODING_MODEL_CATALOG, BYTEPLUS_MODEL_CATALOG } from "./models.js";
import { buildBytePlusCodingProvider, buildBytePlusProvider } from "./provider-catalog.js";
import { buildBytePlusVideoGenerationProvider } from "./video-generation-provider.js";
const PROVIDER_ID = "byteplus";
const BYTEPLUS_DEFAULT_MODEL_REF = "byteplus-plan/ark-code-latest";
export default definePluginEntry({
id: PROVIDER_ID,
name: "BytePlus Provider",
description: "Bundled BytePlus provider plugin",
register(api) {
api.registerProvider({
id: PROVIDER_ID,
label: "BytePlus",
docsPath: "/concepts/model-providers#byteplus-international",
envVars: ["BYTEPLUS_API_KEY"],
auth: [
createProviderApiKeyAuthMethod({
providerId: PROVIDER_ID,
methodId: "api-key",
label: "BytePlus API key",
hint: "API key",
optionKey: "byteplusApiKey",
flagName: "--byteplus-api-key",
envVar: "BYTEPLUS_API_KEY",
promptMessage: "Enter BytePlus API key",
defaultModel: BYTEPLUS_DEFAULT_MODEL_REF,
expectedProviders: ["byteplus"],
applyConfig: (cfg) =>
ensureModelAllowlistEntry({
cfg,
modelRef: BYTEPLUS_DEFAULT_MODEL_REF,
}),
wizard: {
choiceId: "byteplus-api-key",
choiceLabel: "BytePlus API key",
groupId: "byteplus",
groupLabel: "BytePlus",
groupHint: "API key",
},
}),
],
catalog: {
order: "paired",
run: async (ctx) => {
const apiKey = ctx.resolveProviderApiKey(PROVIDER_ID).apiKey;
if (!apiKey) {
return null;
}
return {
providers: {
byteplus: { ...buildBytePlusProvider(), apiKey },
"byteplus-plan": { ...buildBytePlusCodingProvider(), apiKey },
},
};
},
},
augmentModelCatalog: () => {
const byteplusModels = BYTEPLUS_MODEL_CATALOG.map((entry) => ({
provider: "byteplus",
id: entry.id,
name: entry.name,
reasoning: entry.reasoning,
input: [...entry.input],
contextWindow: entry.contextWindow,
}));
const byteplusPlanModels = BYTEPLUS_CODING_MODEL_CATALOG.map((entry) => ({
provider: "byteplus-plan",
id: entry.id,
name: entry.name,
reasoning: entry.reasoning,
input: [...entry.input],
contextWindow: entry.contextWindow,
}));
return [...byteplusModels, ...byteplusPlanModels];
},
});
api.registerVideoGenerationProvider(buildBytePlusVideoGenerationProvider());
},
});

View file

@ -0,0 +1,60 @@
import { completeSimple, type Model } from "@mariozechner/pi-ai";
import { describe, expect, it } from "vitest";
import {
createSingleUserPromptMessage,
extractNonEmptyAssistantText,
isLiveTestEnabled,
} from "../../src/agents/live-test-helpers.js";
import { BYTEPLUS_CODING_BASE_URL, BYTEPLUS_DEFAULT_COST } from "./models.js";
const BYTEPLUS_KEY = process.env.BYTEPLUS_API_KEY ?? "";
const BYTEPLUS_CODING_MODEL = process.env.BYTEPLUS_CODING_MODEL?.trim() || "ark-code-latest";
const LIVE = isLiveTestEnabled(["BYTEPLUS_LIVE_TEST"]);
const describeLive = LIVE && BYTEPLUS_KEY ? describe : describe.skip;
function isBytePlusSubscriptionError(message: string): boolean {
const lower = message.toLowerCase();
return (
lower.includes("coding plan subscription") ||
lower.includes("subscription has expired") ||
(lower.includes("subscription") && lower.includes("renewal"))
);
}
describeLive("byteplus coding plan live", () => {
it("returns assistant text", async () => {
const model: Model<"openai-completions"> = {
id: BYTEPLUS_CODING_MODEL,
name: `BytePlus Coding ${BYTEPLUS_CODING_MODEL}`,
api: "openai-completions",
provider: "byteplus-plan",
baseUrl: BYTEPLUS_CODING_BASE_URL,
reasoning: false,
input: ["text"],
cost: BYTEPLUS_DEFAULT_COST,
contextWindow: 256000,
maxTokens: 4096,
};
const res = await completeSimple(
model,
{
messages: createSingleUserPromptMessage(),
},
{ apiKey: BYTEPLUS_KEY, maxTokens: 64 },
);
if (res.stopReason === "error") {
const message = res.errorMessage ?? "";
if (isBytePlusSubscriptionError(message)) {
expect(message.toLowerCase()).toContain("subscription");
return;
}
throw new Error(message || "byteplus returned error with no message");
}
const text = extractNonEmptyAssistantText(res.content);
expect(text.length).toBeGreaterThan(0);
}, 30000);
});

View file

@ -0,0 +1,44 @@
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared";
import {
buildVolcModelDefinition,
VOLC_MODEL_GLM_4_7,
VOLC_MODEL_KIMI_K2_5,
VOLC_SHARED_CODING_MODEL_CATALOG,
} from "openclaw/plugin-sdk/volc-model-catalog-shared";
export const BYTEPLUS_BASE_URL = "https://ark.ap-southeast.bytepluses.com/api/v3";
export const BYTEPLUS_CODING_BASE_URL = "https://ark.ap-southeast.bytepluses.com/api/coding/v3";
export const BYTEPLUS_DEFAULT_MODEL_ID = "seed-1-8-251228";
export const BYTEPLUS_CODING_DEFAULT_MODEL_ID = "ark-code-latest";
export const BYTEPLUS_DEFAULT_MODEL_REF = `byteplus/${BYTEPLUS_DEFAULT_MODEL_ID}`;
export const BYTEPLUS_DEFAULT_COST = {
input: 0.0001,
output: 0.0002,
cacheRead: 0,
cacheWrite: 0,
};
export const BYTEPLUS_MODEL_CATALOG = [
{
id: "seed-1-8-251228",
name: "Seed 1.8",
reasoning: false,
input: ["text", "image"] as const,
contextWindow: 256000,
maxTokens: 4096,
},
VOLC_MODEL_KIMI_K2_5,
VOLC_MODEL_GLM_4_7,
] as const;
export const BYTEPLUS_CODING_MODEL_CATALOG = VOLC_SHARED_CODING_MODEL_CATALOG;
export type BytePlusCatalogEntry = (typeof BYTEPLUS_MODEL_CATALOG)[number];
export type BytePlusCodingCatalogEntry = (typeof BYTEPLUS_CODING_MODEL_CATALOG)[number];
export function buildBytePlusModelDefinition(
entry: BytePlusCatalogEntry | BytePlusCodingCatalogEntry,
): ModelDefinitionConfig {
return buildVolcModelDefinition(entry, BYTEPLUS_DEFAULT_COST);
}

View file

@ -0,0 +1,34 @@
{
"id": "byteplus",
"enabledByDefault": true,
"providers": ["byteplus", "byteplus-plan"],
"providerAuthEnvVars": {
"byteplus": ["BYTEPLUS_API_KEY"]
},
"providerAuthAliases": {
"byteplus-plan": "byteplus"
},
"providerAuthChoices": [
{
"provider": "byteplus",
"method": "api-key",
"choiceId": "byteplus-api-key",
"choiceLabel": "BytePlus API key",
"groupId": "byteplus",
"groupLabel": "BytePlus",
"groupHint": "API key",
"optionKey": "byteplusApiKey",
"cliFlag": "--byteplus-api-key",
"cliOption": "--byteplus-api-key <key>",
"cliDescription": "BytePlus API key"
}
],
"contracts": {
"videoGenerationProviders": ["byteplus"]
},
"configSchema": {
"type": "object",
"additionalProperties": false,
"properties": {}
}
}

View file

@ -0,0 +1,15 @@
{
"name": "@openclaw/byteplus-provider",
"version": "2026.4.20",
"private": true,
"description": "OpenClaw BytePlus provider plugin",
"type": "module",
"devDependencies": {
"@openclaw/plugin-sdk": "workspace:*"
},
"openclaw": {
"extensions": [
"./index.ts"
]
}
}

View file

@ -0,0 +1,8 @@
import { describePluginRegistrationContract } from "../../test/helpers/plugins/plugin-registration-contract.js";
describePluginRegistrationContract({
pluginId: "byteplus",
providerIds: ["byteplus", "byteplus-plan"],
videoGenerationProviderIds: ["byteplus"],
requireGenerateVideo: true,
});

View file

@ -0,0 +1,24 @@
import type { ModelProviderConfig } from "openclaw/plugin-sdk/provider-model-shared";
import {
buildBytePlusModelDefinition,
BYTEPLUS_BASE_URL,
BYTEPLUS_CODING_BASE_URL,
BYTEPLUS_CODING_MODEL_CATALOG,
BYTEPLUS_MODEL_CATALOG,
} from "./models.js";
export function buildBytePlusProvider(): ModelProviderConfig {
return {
baseUrl: BYTEPLUS_BASE_URL,
api: "openai-completions",
models: BYTEPLUS_MODEL_CATALOG.map(buildBytePlusModelDefinition),
};
}
export function buildBytePlusCodingProvider(): ModelProviderConfig {
return {
baseUrl: BYTEPLUS_CODING_BASE_URL,
api: "openai-completions",
models: BYTEPLUS_CODING_MODEL_CATALOG.map(buildBytePlusModelDefinition),
};
}

View file

@ -0,0 +1,16 @@
{
"extends": "../tsconfig.package-boundary.base.json",
"compilerOptions": {
"rootDir": "."
},
"include": ["./*.ts", "./src/**/*.ts"],
"exclude": [
"./**/*.test.ts",
"./dist/**",
"./node_modules/**",
"./src/test-support/**",
"./src/**/*test-helpers.ts",
"./src/**/*test-harness.ts",
"./src/**/*test-support.ts"
]
}

View file

@ -0,0 +1,125 @@
import { beforeAll, describe, expect, it, vi } from "vitest";
import { expectExplicitVideoGenerationCapabilities } from "../../test/helpers/media-generation/provider-capability-assertions.js";
import {
getProviderHttpMocks,
installProviderHttpMockCleanup,
} from "../../test/helpers/media-generation/provider-http-mocks.js";
const { postJsonRequestMock, fetchWithTimeoutMock } = getProviderHttpMocks();
let buildBytePlusVideoGenerationProvider: typeof import("./video-generation-provider.js").buildBytePlusVideoGenerationProvider;
beforeAll(async () => {
({ buildBytePlusVideoGenerationProvider } = await import("./video-generation-provider.js"));
});
installProviderHttpMockCleanup();
function mockSuccessfulBytePlusTask(params?: { model?: string }) {
postJsonRequestMock.mockResolvedValue({
response: {
json: async () => ({
id: "task_123",
}),
},
release: vi.fn(async () => {}),
});
fetchWithTimeoutMock
.mockResolvedValueOnce({
json: async () => ({
id: "task_123",
status: "succeeded",
content: {
video_url: "https://example.com/byteplus.mp4",
},
model: params?.model ?? "seedance-1-0-lite-t2v-250428",
}),
})
.mockResolvedValueOnce({
headers: new Headers({ "content-type": "video/mp4" }),
arrayBuffer: async () => Buffer.from("mp4-bytes"),
});
}
describe("byteplus video generation provider", () => {
it("declares explicit mode capabilities", () => {
expectExplicitVideoGenerationCapabilities(buildBytePlusVideoGenerationProvider());
});
it("creates a content-generation task, polls, and downloads the video", async () => {
mockSuccessfulBytePlusTask();
const provider = buildBytePlusVideoGenerationProvider();
const result = await provider.generateVideo({
provider: "byteplus",
model: "seedance-1-0-lite-t2v-250428",
prompt: "A lantern floats upward into the night sky",
cfg: {},
});
expect(postJsonRequestMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks",
}),
);
expect(result.videos).toHaveLength(1);
expect(result.metadata).toEqual(
expect.objectContaining({
taskId: "task_123",
}),
);
});
it("switches t2v image requests to i2v models and lowercases resolution", async () => {
mockSuccessfulBytePlusTask({ model: "seedance-1-0-lite-i2v-250428" });
const provider = buildBytePlusVideoGenerationProvider();
await provider.generateVideo({
provider: "byteplus",
model: "seedance-1-0-lite-t2v-250428",
prompt: "Animate this still image",
resolution: "720P",
inputImages: [{ url: "https://example.com/first-frame.png" }],
cfg: {},
});
const request = postJsonRequestMock.mock.calls[0]?.[0] as { body?: Record<string, unknown> };
expect(request.body).toMatchObject({
model: "seedance-1-0-lite-i2v-250428",
resolution: "720p",
content: [
{ type: "text", text: "Animate this still image" },
{
type: "image_url",
image_url: { url: "https://example.com/first-frame.png" },
role: "first_frame",
},
],
});
});
it("maps declared providerOptions into the request body", async () => {
mockSuccessfulBytePlusTask({ model: "seedance-1-0-pro-250528" });
const provider = buildBytePlusVideoGenerationProvider();
await provider.generateVideo({
provider: "byteplus",
model: "seedance-1-0-pro-250528",
prompt: "A cinematic lobster montage",
providerOptions: {
seed: 42,
draft: true,
camera_fixed: false,
},
cfg: {},
});
const request = postJsonRequestMock.mock.calls[0]?.[0] as { body?: Record<string, unknown> };
expect(request.body).toMatchObject({
model: "seedance-1-0-pro-250528",
seed: 42,
resolution: "480p",
camera_fixed: false,
});
});
});

View file

@ -0,0 +1,329 @@
import { isProviderApiKeyConfigured } from "openclaw/plugin-sdk/provider-auth";
import { resolveApiKeyForProvider } from "openclaw/plugin-sdk/provider-auth-runtime";
import {
assertOkOrThrowHttpError,
createProviderOperationDeadline,
fetchWithTimeout,
postJsonRequest,
resolveProviderOperationTimeoutMs,
resolveProviderHttpRequestConfig,
waitProviderOperationPollInterval,
} from "openclaw/plugin-sdk/provider-http";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import type {
GeneratedVideoAsset,
VideoGenerationProvider,
VideoGenerationRequest,
} from "openclaw/plugin-sdk/video-generation";
import { BYTEPLUS_BASE_URL } from "./models.js";
const DEFAULT_BYTEPLUS_VIDEO_MODEL = "seedance-1-0-lite-t2v-250428";
const DEFAULT_TIMEOUT_MS = 120_000;
const POLL_INTERVAL_MS = 5_000;
const MAX_POLL_ATTEMPTS = 120;
type BytePlusTaskCreateResponse = {
id?: string;
};
type BytePlusTaskResponse = {
id?: string;
model?: string;
status?: "running" | "failed" | "queued" | "succeeded" | "cancelled";
error?: {
code?: string;
message?: string;
};
content?: {
video_url?: string;
last_frame_url?: string;
file_url?: string;
};
duration?: number;
ratio?: string;
resolution?: string;
};
function resolveBytePlusVideoBaseUrl(req: VideoGenerationRequest): string {
return (
normalizeOptionalString(req.cfg?.models?.providers?.byteplus?.baseUrl) ?? BYTEPLUS_BASE_URL
);
}
function toDataUrl(buffer: Buffer, mimeType: string): string {
return `data:${mimeType};base64,${buffer.toString("base64")}`;
}
function resolveBytePlusImageUrl(req: VideoGenerationRequest): string | undefined {
const input = req.inputImages?.[0];
if (!input) {
return undefined;
}
const inputUrl = normalizeOptionalString(input.url);
if (inputUrl) {
return inputUrl;
}
if (!input.buffer) {
throw new Error("BytePlus reference image is missing image data.");
}
return toDataUrl(input.buffer, normalizeOptionalString(input.mimeType) ?? "image/png");
}
async function pollBytePlusTask(params: {
taskId: string;
headers: Headers;
timeoutMs?: number;
baseUrl: string;
fetchFn: typeof fetch;
}): Promise<BytePlusTaskResponse> {
const deadline = createProviderOperationDeadline({
timeoutMs: params.timeoutMs,
label: `BytePlus video generation task ${params.taskId}`,
});
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt += 1) {
const response = await fetchWithTimeout(
`${params.baseUrl}/contents/generations/tasks/${params.taskId}`,
{
method: "GET",
headers: params.headers,
},
resolveProviderOperationTimeoutMs({ deadline, defaultTimeoutMs: DEFAULT_TIMEOUT_MS }),
params.fetchFn,
);
await assertOkOrThrowHttpError(response, "BytePlus video status request failed");
const payload = (await response.json()) as BytePlusTaskResponse;
switch (normalizeOptionalString(payload.status)) {
case "succeeded":
return payload;
case "failed":
case "cancelled":
throw new Error(
normalizeOptionalString(payload.error?.message) || "BytePlus video generation failed",
);
case "queued":
case "running":
default:
await waitProviderOperationPollInterval({ deadline, pollIntervalMs: POLL_INTERVAL_MS });
break;
}
}
throw new Error(`BytePlus video generation task ${params.taskId} did not finish in time`);
}
async function downloadBytePlusVideo(params: {
url: string;
timeoutMs?: number;
fetchFn: typeof fetch;
}): Promise<GeneratedVideoAsset> {
const response = await fetchWithTimeout(
params.url,
{ method: "GET" },
params.timeoutMs ?? DEFAULT_TIMEOUT_MS,
params.fetchFn,
);
await assertOkOrThrowHttpError(response, "BytePlus generated video download failed");
const mimeType = normalizeOptionalString(response.headers.get("content-type")) ?? "video/mp4";
const arrayBuffer = await response.arrayBuffer();
return {
buffer: Buffer.from(arrayBuffer),
mimeType,
fileName: `video-1.${mimeType.includes("webm") ? "webm" : "mp4"}`,
};
}
export function buildBytePlusVideoGenerationProvider(): VideoGenerationProvider {
return {
id: "byteplus",
label: "BytePlus",
defaultModel: DEFAULT_BYTEPLUS_VIDEO_MODEL,
models: [
DEFAULT_BYTEPLUS_VIDEO_MODEL,
"seedance-1-0-lite-i2v-250428",
"seedance-1-0-pro-250528",
"seedance-1-5-pro-251215",
],
isConfigured: ({ agentDir }) =>
isProviderApiKeyConfigured({
provider: "byteplus",
agentDir,
}),
capabilities: {
providerOptions: {
seed: "number",
draft: "boolean",
camera_fixed: "boolean",
},
generate: {
maxVideos: 1,
maxDurationSeconds: 12,
supportsAspectRatio: true,
supportsResolution: true,
supportsAudio: true,
supportsWatermark: true,
},
imageToVideo: {
enabled: true,
maxVideos: 1,
maxInputImages: 1,
maxDurationSeconds: 12,
supportsAspectRatio: true,
supportsResolution: true,
supportsAudio: true,
supportsWatermark: true,
},
videoToVideo: {
enabled: false,
},
},
async generateVideo(req) {
if ((req.inputVideos?.length ?? 0) > 0) {
throw new Error("BytePlus video generation does not support video reference inputs.");
}
const auth = await resolveApiKeyForProvider({
provider: "byteplus",
cfg: req.cfg,
agentDir: req.agentDir,
store: req.authStore,
});
if (!auth.apiKey) {
throw new Error("BytePlus API key missing");
}
const fetchFn = fetch;
const deadline = createProviderOperationDeadline({
timeoutMs: req.timeoutMs,
label: "BytePlus video generation",
});
const { baseUrl, allowPrivateNetwork, headers, dispatcherPolicy } =
resolveProviderHttpRequestConfig({
baseUrl: resolveBytePlusVideoBaseUrl(req),
defaultBaseUrl: BYTEPLUS_BASE_URL,
allowPrivateNetwork: false,
defaultHeaders: {
Authorization: `Bearer ${auth.apiKey}`,
"Content-Type": "application/json",
},
provider: "byteplus",
capability: "video",
transport: "http",
});
// Seedance 1.0 has separate T2V and I2V model IDs (e.g. seedance-1-0-lite-t2v-250428 vs
// seedance-1-0-lite-i2v-250428). When input images are provided with a T2V model, auto-
// switch to the corresponding I2V variant so the API does not reject with task_type mismatch.
// 1.5 Pro uses a single model ID for both modes and is unaffected by this substitution.
const hasInputImages = (req.inputImages?.length ?? 0) > 0;
const requestedModel = normalizeOptionalString(req.model) || DEFAULT_BYTEPLUS_VIDEO_MODEL;
const resolvedModel =
hasInputImages && requestedModel.includes("-t2v-")
? requestedModel.replace("-t2v-", "-i2v-")
: requestedModel;
const content: Array<Record<string, unknown>> = [{ type: "text", text: req.prompt }];
const imageUrl = resolveBytePlusImageUrl(req);
if (imageUrl) {
content.push({
type: "image_url",
image_url: { url: imageUrl },
role: "first_frame",
});
}
const body: Record<string, unknown> = {
model: resolvedModel,
content,
};
const aspectRatio = normalizeOptionalString(req.aspectRatio);
if (aspectRatio) {
body.ratio = aspectRatio;
}
// Seedance API requires lowercase resolution values (e.g. "480p", "720p"); uppercase
// variants like "480P" are rejected with InvalidParameter.
const resolution = normalizeOptionalString(req.resolution)?.toLowerCase();
if (resolution) {
body.resolution = resolution;
}
if (typeof req.durationSeconds === "number" && Number.isFinite(req.durationSeconds)) {
body.duration = Math.max(1, Math.round(req.durationSeconds));
}
if (typeof req.audio === "boolean") {
body.generate_audio = req.audio;
}
if (typeof req.watermark === "boolean") {
body.watermark = req.watermark;
}
// Forward declared providerOptions: seed, draft, camerafixed.
// draft=true forces 480p resolution for faster generation.
const opts = req.providerOptions ?? {};
const seed = typeof opts.seed === "number" ? opts.seed : undefined;
const draft = opts.draft === true;
// Official JSON body field is camera_fixed (with underscore).
const cameraFixed = typeof opts.camera_fixed === "boolean" ? opts.camera_fixed : undefined;
if (seed != null) {
body.seed = seed;
}
if (draft && !body.resolution) {
body.resolution = "480p";
}
if (cameraFixed != null) {
body.camera_fixed = cameraFixed;
}
const { response, release } = await postJsonRequest({
url: `${baseUrl}/contents/generations/tasks`,
headers,
body,
timeoutMs: resolveProviderOperationTimeoutMs({
deadline,
defaultTimeoutMs: DEFAULT_TIMEOUT_MS,
}),
fetchFn,
allowPrivateNetwork,
dispatcherPolicy,
});
try {
await assertOkOrThrowHttpError(response, "BytePlus video generation failed");
const submitted = (await response.json()) as BytePlusTaskCreateResponse;
const taskId = normalizeOptionalString(submitted.id);
if (!taskId) {
throw new Error("BytePlus video generation response missing task id");
}
const completed = await pollBytePlusTask({
taskId,
headers,
timeoutMs: resolveProviderOperationTimeoutMs({
deadline,
defaultTimeoutMs: DEFAULT_TIMEOUT_MS,
}),
baseUrl,
fetchFn,
});
const videoUrl = normalizeOptionalString(completed.content?.video_url);
if (!videoUrl) {
throw new Error("BytePlus video generation completed without a video URL");
}
const video = await downloadBytePlusVideo({
url: videoUrl,
timeoutMs: resolveProviderOperationTimeoutMs({
deadline,
defaultTimeoutMs: DEFAULT_TIMEOUT_MS,
}),
fetchFn,
});
return {
videos: [video],
model: completed.model ?? resolvedModel,
metadata: {
taskId,
status: completed.status,
videoUrl,
ratio: completed.ratio,
resolution: completed.resolution,
duration: completed.duration,
},
};
} finally {
await release();
}
},
};
}