From e0aa19acb24d0dae790086659a79b95c79cb38f0 Mon Sep 17 00:00:00 2001 From: magpie-community Date: Wed, 18 Nov 2026 20:41:09 +0800 Subject: [PATCH] =?UTF-8?q?dreamina=200.1.0:=20Dreamina=20(=E5=8D=B3?= =?UTF-8?q?=E6=A2=A6)=20image=20and=20video=20provider=20via=20local=20dre?= =?UTF-8?q?amina-canvas=20CLI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + packages/dreamina/LICENSE | 21 + packages/dreamina/README.md | 63 ++ packages/dreamina/canvas-state.mjs | 374 +++++++ packages/dreamina/cli.mjs | 338 ++++++ packages/dreamina/index.mjs | 556 ++++++++++ packages/dreamina/magpie.mjs | 2 + packages/dreamina/media.mjs | 1249 ++++++++++++++++++++++ packages/dreamina/models.mjs | 666 ++++++++++++ packages/dreamina/opencode.mjs | 2 + packages/dreamina/opencode2.mjs | 620 +++++++++++ packages/dreamina/package.json | 51 + packages/dreamina/test/dreamina.test.mjs | 1238 +++++++++++++++++++++ registry.json | 2 + 14 files changed, 5183 insertions(+) create mode 100644 packages/dreamina/LICENSE create mode 100644 packages/dreamina/README.md create mode 100644 packages/dreamina/canvas-state.mjs create mode 100644 packages/dreamina/cli.mjs create mode 100644 packages/dreamina/index.mjs create mode 100644 packages/dreamina/magpie.mjs create mode 100644 packages/dreamina/media.mjs create mode 100644 packages/dreamina/models.mjs create mode 100644 packages/dreamina/opencode.mjs create mode 100644 packages/dreamina/opencode2.mjs create mode 100644 packages/dreamina/package.json create mode 100644 packages/dreamina/test/dreamina.test.mjs diff --git a/README.md b/README.md index 42c5bd65c..837a6165b 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ vendor service and makes its requests. The packages work in OpenCode and in | [inkstone](packages/inkstone) | (Unofficial) 书生·端砚 Token Plan: browser sign-in gives the ink-credit balance, the 5-hour and 7-day windows and the credit packs, and creates an `ink-*` API key to serve the plan's models for chat | `intern-inkstone` | | [joycode](packages/joycode) | JoyCode IDE credits with JD account browser sign-in, live models and credit multipliers (unofficial) | `joycode` | | [doubao-work](packages/doubao-work) | Doubao Work (豆包工作), whose agent runs in the cloud with no API, through the Doubao desktop app driven the way a person uses it (macOS, Windows) | `doubao-work` | +| [dreamina](packages/dreamina) | Dreamina (即梦, jimeng.jianying.com) image (Seedream) and video (Seedance, Vidu, Wan, HappyHorse) models through your local `dreamina-canvas` CLI, with browser or CLI profile sign-in, VIP status, credit balance tracking and quote-first ceiling protection | `dreamina` | | [kimi-work](packages/kimi-work) | Ordinary Kimi desktop Work subscription, through the desktop app's local sign-in; separate from Kimi Code | `kimi-work` | | [kiro](packages/kiro) | Kiro (Free, Pro, Pro+, Power), with Kiro's sign-in, kiro-cli's or the IDE's, or an API key | `kiro` | | [loomy](packages/loomy) | iFlytek's Loomy (loomyad.xunfei.cn), with SMS sign-in | `loomy` | diff --git a/packages/dreamina/LICENSE b/packages/dreamina/LICENSE new file mode 100644 index 000000000..c11b2e35c --- /dev/null +++ b/packages/dreamina/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 magpie-community + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/dreamina/README.md b/packages/dreamina/README.md new file mode 100644 index 000000000..0dae54d14 --- /dev/null +++ b/packages/dreamina/README.md @@ -0,0 +1,63 @@ +# @magpie-community/opencode-dreamina-auth + +将本机已安装的 [`dreamina-canvas`](https://jimeng.jianying.com/) CLI 接入 **Magpie** 与 **OpenCode**,提供 **Dreamina (即梦)** Provider(`dreamina`)。 + +## 接入能力 + +- **Provider**:`dreamina`(`Dreamina (即梦)`) +- **生图模型(Seedream 系列)**:`seedream_5.5`、`seedream_5.0_pro`、`seedream_5.0_lite`、`seedream_4.7`、`seedream_4.6`、`high_aes_general_v50_flash`、`jimeng_smart_image_v1`、`jm_image_model_gg_bp_21`、`jm_image_model_lib_s`、`jm_image_model_lib_f`,兼容 OpenAI `POST /images/generations` 与 `POST /images/edits`(支持 JSON 与 `multipart/form-data`,以及 `data:` URL、`https://` URL、`image`、`image[]`、`reference_images`)。 +- **生视频模型(Seedance / Vidu / Wan / HappyHorse)**:`seedance_2.5`、`seedance_2.5_flash`、`seedance_2.0_vip`、`seedance_2.0_fast_vip`、`seedance_2.0_mini`、`seedance_pro_fast`、`dreamina_vidu_q3`、`dreamina_vidu_q2`、`wan_3.0`、`happyhorse_1.1`、`happyhorse_1.0`,兼容 OpenAI `POST /videos`、`GET /videos/{id}` 与 `GET /videos/{id}/content`。 +- **在线模型发现**:自动调用 `dreamina-canvas model list --type image|video` 发现并校准可用模型、比例、分辨率大小写(如 `720p` 与 `720P`、`1.5K` 与 `2K`)与时长范围,CLI 不可用时回退内置目录。 + +## 前置条件 + +1. 本机需预先安装 **`dreamina-canvas` CLI**(本插件不自动下载或安装该二进制)。 +2. 插件按以下顺序查找可执行文件: + - 环境变量 `DREAMINA_CANVAS_BIN` + - 系统 `PATH` 中的 `dreamina-canvas` + - `~/.local/bin/dreamina-canvas`、`/opt/homebrew/bin/dreamina-canvas`、`/usr/local/bin/dreamina-canvas` + +## 安装与加载 + +### 在 Magpie 中加载 + +- **本地路径加载**:在 Magpie 的插件管理中直接添加本仓库本地目录路径(读取 `package.json` 的 `exports["./server"]` → `./magpie.mjs`)。 +- **包名加载**:安装或配置 `@magpie-community/opencode-dreamina-auth`。 + +### 在 OpenCode 中配置 + +```json +{ + "plugin": ["@magpie-community/opencode-dreamina-auth"] +} +``` + +## 登录方式 + +1. **使用本机已有 CLI 配置(默认 `default`)**:输入 `dreamina-canvas` 的 profile 名称(留空默认 `default`),插件通过 `auth status` 与 `auth account` 校验登录态。 +2. **即梦浏览器登录**:可选填写 profile 名称(默认 `default`),插件调用 `auth login` 返回设备授权链接,并在后台通过 `auth wait --device-code` 等待授权完成。 + +## 在 Agent 中使用(生图 / 生视频) + +1. **常规对话模型 + `magpie-image` MCP 工具(推荐)**:在 Magpie 的 `Settings → Models → Images / Video` 选择 `dreamina` 模型,并在 `Library → MCP servers` 中为目标 Agent 勾选 `magpie-image`,Agent 即可自动调用 `generate_image`、`generate_video` 与 `generation_result`。 +2. **直选即梦模型对话(默认先报价后确认)**:直接选择即梦模型发送提示词时,插件先创建草稿并调用 `node quote` 返回预估积分(0 扣费);回复 `确认生成:<提示词>`(或带 `--confirm`)后才执行扣费生成。同时支持 Magpie「测试模型」(0 积分连通性检测)。 + +## 额度展示与积分防误扣费 + +- **Logo 与菜单栏额度展示**:Magpie 供应商列表与菜单栏会展示官方产品 Logo、会员等级(来自 `auth account`)与点数状态。 +- **点数余额与按次扣减**:因为 `dreamina-canvas` CLI 暂不直接返回云端总余额,登录时可选填当前点数余额(或设置环境变量 `DREAMINA_CREDITS_BALANCE`),之后每次生成会自动按次扣减并显示剩余点数进度;留空则显示今日已用与上限保护状态(若 CLI 返回余额字段会自动优先采用)。 +- **每次生成先询价**:先保存画布节点并调用 `node quote` 询价,校验通过后才在内存中持有短期 `creditConfirmationToken` 调用 `node confirm` 与 `node run`(令牌与临时下载链接绝不落盘、不写入日志)。 + +可在 Magpie Provider 设置中切换以下开关: + +- **仅报价(不扣积分)`quote_only`(默认关闭 `false`)**:只询价并返回 HTTP `403` 与预估 `totalMaxCredits`,不执行生成(单次请求也可在请求体传 `"quote_only": true`)。 +- **单次 200 点上限保护 `credit_guard`(默认开启 `true`)**:当 `totalMaxCredits` 超过上限(默认 `200` 点)时返回 HTTP `403` 并阻止运行;可通过请求体 `credit_ceiling` 或环境变量 `DREAMINA_CREDIT_CEILING` 覆盖上限。 + +## 本地状态与测试 + +- 画布绑定(每个 profile 独立维护 `"Magpie Gateway"` 画布 `projectId`)与视频任务索引保存在 `/dreamina-canvas-state.json`(权限 `0o600`,目录不可写时回退至系统临时目录)。 +- 运行单元测试(纯 Mock CLI,不触发真实扣费): + +```bash +bun test +``` diff --git a/packages/dreamina/canvas-state.mjs b/packages/dreamina/canvas-state.mjs new file mode 100644 index 000000000..3a2bf866f --- /dev/null +++ b/packages/dreamina/canvas-state.mjs @@ -0,0 +1,374 @@ +import { randomBytes, randomUUID } from "node:crypto" +import fs from "node:fs" +import os from "node:os" +import path from "node:path" +import { runCli } from "./cli.mjs" + +const STATE_FILENAME = "dreamina-canvas-state.json" +const CANVAS_NAME = "Magpie Gateway" +const CROCKFORD = "0123456789abcdefghjkmnpqrstvwxyz" +const MAX_VIDEO_RECORDS = 500 + +const writeQueues = new Map() +const inflightCanvases = new Map() + +export function newNodeId() { + const bytes = randomBytes(10) + let suffix = "" + for (let i = 0; i < 10; i++) { + suffix += CROCKFORD[bytes[i] & 31] + } + return `node_${suffix}` +} + +function defaultState() { + return { version: 1, canvases: {}, videos: {}, credits: {} } +} + +function localDateKey(d = new Date()) { + const y = d.getFullYear() + const m = String(d.getMonth() + 1).padStart(2, "0") + const day = String(d.getDate()).padStart(2, "0") + return `${y}-${m}-${day}` +} + +function finiteNonNeg(val, fallback = 0) { + if (val === null || val === undefined || (typeof val === "string" && val.trim() === "")) return fallback + const n = Number(val) + return Number.isFinite(n) && n >= 0 ? n : fallback +} + +function nullableNonNeg(val) { + if (val === null || val === undefined || (typeof val === "string" && val.trim() === "")) return null + const n = Number(val) + return Number.isFinite(n) && n >= 0 ? n : null +} + +function normalizeState(raw) { + const state = defaultState() + if (!raw || typeof raw !== "object" || Array.isArray(raw)) return state + if (raw.canvases && typeof raw.canvases === "object" && !Array.isArray(raw.canvases)) { + for (const [k, v] of Object.entries(raw.canvases)) { + if (!k || k === "__proto__" || k === "constructor" || k === "prototype") continue + if (v && typeof v === "object" && typeof v.projectId === "string" && v.projectId) { + state.canvases[k] = { + projectId: v.projectId, + name: typeof v.name === "string" ? v.name : CANVAS_NAME, + createdAt: Number.isFinite(v.createdAt) ? v.createdAt : Date.now(), + } + } + } + } + if (raw.videos && typeof raw.videos === "object" && !Array.isArray(raw.videos)) { + for (const [k, v] of Object.entries(raw.videos)) { + if (!k || k === "__proto__" || k === "constructor" || k === "prototype") continue + if (v && typeof v === "object" && typeof v.submitId === "string" && v.submitId) { + state.videos[k] = { + profile: typeof v.profile === "string" && v.profile ? v.profile : "default", + projectId: typeof v.projectId === "string" ? v.projectId : "", + nodeId: typeof v.nodeId === "string" ? v.nodeId : "", + submitId: v.submitId, + model: typeof v.model === "string" ? v.model : "", + duration: Number.isFinite(v.duration) ? v.duration : 5, + createdAt: Number.isFinite(v.createdAt) ? v.createdAt : Date.now(), + ...(typeof v.size === "string" && v.size ? { size: v.size } : {}), + } + } + } + } + if (raw.credits && typeof raw.credits === "object" && !Array.isArray(raw.credits)) { + for (const [k, v] of Object.entries(raw.credits)) { + if (!k || k === "__proto__" || k === "constructor" || k === "prototype") continue + if (v && typeof v === "object") { + state.credits[k] = { + todayDate: typeof v.todayDate === "string" ? v.todayDate : "", + todayCredits: finiteNonNeg(v.todayCredits, 0), + totalCredits: finiteNonNeg(v.totalCredits, 0), + lastQuotedCredits: nullableNonNeg(v.lastQuotedCredits), + lastQuotedAt: nullableNonNeg(v.lastQuotedAt), + lastSpentCredits: nullableNonNeg(v.lastSpentCredits), + lastRunAt: nullableNonNeg(v.lastRunAt), + runCount: Math.trunc(finiteNonNeg(v.runCount, 0)), + configuredBalance: nullableNonNeg(v.configuredBalance), + spentSinceBalanceSet: finiteNonNeg(v.spentSinceBalanceSet, 0), + } + } + } + } + return state +} + +function candidatePaths(directory) { + const fallback = path.join(os.tmpdir(), STATE_FILENAME) + const dir = typeof directory === "string" ? directory.trim() : "" + if (!dir) return [fallback] + const primary = path.join(path.resolve(dir), STATE_FILENAME) + return primary === fallback ? [fallback] : [primary, fallback] +} + +function tryReadState(filePath) { + try { + const text = fs.readFileSync(filePath, "utf8") + return normalizeState(JSON.parse(text)) + } catch { + return null + } +} + +function writeAtomic0600(filePath, data) { + const dir = path.dirname(filePath) + fs.mkdirSync(dir, { recursive: true }) + const tmp = `${filePath}.tmp-${process.pid}-${randomUUID().slice(0, 8)}` + try { + const payload = JSON.stringify(data, null, 2) + "\n" + fs.writeFileSync(tmp, payload, { mode: 0o600, encoding: "utf8" }) + try { + fs.chmodSync(tmp, 0o600) + } catch {} + fs.renameSync(tmp, filePath) + try { + fs.chmodSync(filePath, 0o600) + } catch {} + } catch (err) { + try { + fs.unlinkSync(tmp) + } catch {} + throw err + } +} + +function pruneVideos(state) { + const entries = Object.entries(state.videos) + if (entries.length <= MAX_VIDEO_RECORDS) return + entries.sort((a, b) => (a[1].createdAt ?? 0) - (b[1].createdAt ?? 0)) + const dropCount = entries.length - MAX_VIDEO_RECORDS + for (let i = 0; i < dropCount; i++) { + delete state.videos[entries[i][0]] + } +} + +export function resolveStatePath(directory) { + const paths = candidatePaths(directory) + for (const p of paths) { + if (fs.existsSync(p)) return p + } + return paths[0] +} + +export function readCanvasState(directory) { + const paths = candidatePaths(directory) + for (const p of paths) { + const parsed = tryReadState(p) + if (parsed) return parsed + } + return defaultState() +} + +export function writeCanvasState(directory, state) { + const clean = normalizeState(state) + pruneVideos(clean) + const paths = candidatePaths(directory) + let lastErr = null + for (const p of paths) { + try { + writeAtomic0600(p, clean) + return p + } catch (err) { + lastErr = err + } + } + throw lastErr ?? new Error("Unable to write dreamina-canvas-state.json") +} + +async function mutateCanvasState(directory, mutator) { + const key = path.resolve(directory || os.tmpdir()) + const prev = writeQueues.get(key) ?? Promise.resolve() + const next = prev.then(async () => { + const state = readCanvasState(directory) + const result = await mutator(state) + writeCanvasState(directory, state) + return result + }) + const settled = next.then(() => {}, () => {}) + writeQueues.set(key, settled) + return next +} + +export async function ensureCanvas(profile = "default", { directory, signal } = {}) { + const safeProfile = String(profile ?? "").trim() || "default" + const state = readCanvasState(directory) + const existing = Object.hasOwn(state.canvases, safeProfile) ? state.canvases[safeProfile] : null + if (existing?.projectId) { + return existing.projectId + } + + const inflightKey = `${path.resolve(directory || os.tmpdir())}\0${safeProfile}` + const pending = inflightCanvases.get(inflightKey) + if (pending) return pending + + const createPromise = (async () => { + const freshState = readCanvasState(directory) + const rechecked = Object.hasOwn(freshState.canvases, safeProfile) ? freshState.canvases[safeProfile] : null + if (rechecked?.projectId) return rechecked.projectId + + const candidateProjectId = randomUUID() + const res = await runCli( + safeProfile, + ["canvas", "create", CANVAS_NAME, "--project-id", candidateProjectId], + { signal }, + ) + const createdProjectId = res.data?.project?.projectId || candidateProjectId + await mutateCanvasState(directory, (s) => { + s.canvases[safeProfile] = { + projectId: createdProjectId, + name: res.data?.project?.name || CANVAS_NAME, + createdAt: Date.now(), + } + }) + return createdProjectId + })() + + inflightCanvases.set(inflightKey, createPromise) + try { + return await createPromise + } finally { + if (inflightCanvases.get(inflightKey) === createPromise) { + inflightCanvases.delete(inflightKey) + } + } +} + +export async function saveVideoTask(directory, videoTaskId, record) { + if (!videoTaskId || typeof videoTaskId !== "string") { + throw new Error("videoTaskId is required") + } + await mutateCanvasState(directory, (state) => { + state.videos[videoTaskId] = { + profile: String(record.profile ?? "").trim() || "default", + projectId: String(record.projectId || ""), + nodeId: String(record.nodeId || ""), + submitId: String(record.submitId || ""), + model: String(record.model || ""), + duration: Number.isFinite(record.duration) ? record.duration : 5, + createdAt: Number.isFinite(record.createdAt) ? record.createdAt : Date.now(), + ...(typeof record.size === "string" && record.size ? { size: record.size } : {}), + } + }) +} + +export function getVideoTask(directory, videoTaskId) { + if (!videoTaskId || typeof videoTaskId !== "string") return null + const state = readCanvasState(directory) + if (Object.hasOwn(state.videos, videoTaskId)) { + return { videoTaskId, ...state.videos[videoTaskId] } + } + // Fallback lookup by submitId + for (const [id, rec] of Object.entries(state.videos)) { + if (rec.submitId === videoTaskId) { + return { videoTaskId: id, ...rec } + } + } + return null +} + +function emptyCreditRecord(todayKey = localDateKey()) { + return { + todayDate: todayKey, + todayCredits: 0, + totalCredits: 0, + lastQuotedCredits: null, + lastQuotedAt: null, + lastSpentCredits: null, + lastRunAt: null, + runCount: 0, + configuredBalance: null, + spentSinceBalanceSet: 0, + } +} + +export async function setCreditBalance(profile = "default", { balance, directory } = {}) { + if (balance === null || balance === undefined || balance === "") return + if (typeof balance === "string" && balance.trim() === "") return + const val = Number(balance) + if (!Number.isFinite(val) || val < 0) return + const safeProfile = String(profile ?? "").trim() || "default" + await mutateCanvasState(directory, (state) => { + const todayKey = localDateKey() + const prev = Object.hasOwn(state.credits, safeProfile) ? state.credits[safeProfile] : emptyCreditRecord(todayKey) + state.credits[safeProfile] = { + ...prev, + configuredBalance: val, + spentSinceBalanceSet: 0, + } + }) +} + +export async function recordCreditQuote(profile = "default", { quotedCredits, directory } = {}) { + const safeProfile = String(profile ?? "").trim() || "default" + const val = Number(quotedCredits) + if (!Number.isFinite(val) || val < 0) return + await mutateCanvasState(directory, (state) => { + const todayKey = localDateKey() + const prev = Object.hasOwn(state.credits, safeProfile) ? state.credits[safeProfile] : emptyCreditRecord(todayKey) + const todayCredits = prev.todayDate === todayKey ? finiteNonNeg(prev.todayCredits, 0) : 0 + state.credits[safeProfile] = { + ...prev, + todayDate: todayKey, + todayCredits, + lastQuotedCredits: val, + lastQuotedAt: Date.now(), + } + }) +} + +export async function recordCreditSpend(profile = "default", { credits, directory } = {}) { + const safeProfile = String(profile ?? "").trim() || "default" + const val = Number(credits) + if (!Number.isFinite(val) || val < 0) return + await mutateCanvasState(directory, (state) => { + const todayKey = localDateKey() + const prev = Object.hasOwn(state.credits, safeProfile) ? state.credits[safeProfile] : emptyCreditRecord(todayKey) + const prevToday = prev.todayDate === todayKey ? finiteNonNeg(prev.todayCredits, 0) : 0 + state.credits[safeProfile] = { + ...prev, + todayDate: todayKey, + todayCredits: prevToday + val, + totalCredits: finiteNonNeg(prev.totalCredits, 0) + val, + spentSinceBalanceSet: finiteNonNeg(prev.spentSinceBalanceSet, 0) + val, + lastSpentCredits: val, + lastRunAt: Date.now(), + runCount: Math.trunc(finiteNonNeg(prev.runCount, 0)) + 1, + } + }) +} + +export async function getCreditStats(profile = "default", { directory } = {}) { + const safeProfile = String(profile ?? "").trim() || "default" + const state = readCanvasState(directory) + const rec = Object.hasOwn(state.credits, safeProfile) ? state.credits[safeProfile] : null + if (!rec) { + return { + todayCredits: 0, + totalCredits: 0, + lastQuotedCredits: null, + lastSpentCredits: null, + runCount: 0, + configuredBalance: null, + spentSinceBalanceSet: 0, + remainingCredits: null, + } + } + const todayKey = localDateKey() + const configuredBalance = nullableNonNeg(rec.configuredBalance) + const spentSinceBalanceSet = finiteNonNeg(rec.spentSinceBalanceSet, 0) + return { + todayCredits: rec.todayDate === todayKey ? finiteNonNeg(rec.todayCredits, 0) : 0, + totalCredits: finiteNonNeg(rec.totalCredits, 0), + lastQuotedCredits: nullableNonNeg(rec.lastQuotedCredits), + lastSpentCredits: nullableNonNeg(rec.lastSpentCredits), + runCount: Math.trunc(finiteNonNeg(rec.runCount, 0)), + configuredBalance, + spentSinceBalanceSet, + remainingCredits: configuredBalance !== null ? Math.max(0, configuredBalance - spentSinceBalanceSet) : null, + } +} diff --git a/packages/dreamina/cli.mjs b/packages/dreamina/cli.mjs new file mode 100644 index 000000000..275231463 --- /dev/null +++ b/packages/dreamina/cli.mjs @@ -0,0 +1,338 @@ +import { spawn } from "node:child_process" +import fs from "node:fs" +import os from "node:os" +import path from "node:path" + +const DEFAULT_TIMEOUT_MS = 120_000 + +let testRunner = null +let testRunnerGen = 0 + +export class CliError extends Error { + constructor(message, opts = {}) { + super(message) + this.name = "CliError" + this.status = opts.status ?? 502 + this.code = opts.code ?? "cli_error" + this.exitCode = opts.exitCode ?? null + this.cliError = opts.cliError ?? null + this.payload = opts.payload ?? null + if (opts.signIn) this.signIn = opts.signIn + } +} + +export function createAbortError(message = "Aborted") { + return new DOMException(message, "AbortError") +} + +export function isAbortError(err) { + return err?.name === "AbortError" || err?.code === "ABORT_ERR" +} + +export function redactSensitiveText(text) { + if (typeof text !== "string" || !text) return "" + return text + .replace(/("creditConfirmationToken"\s*:\s*")[^"]*(")/gi, "$1[REDACTED]$2") + .replace(/(--credit-token(?:\s+|=))([^\s]+)/gi, "$1[REDACTED]") + .replace(/(https?:\/\/[^\s"'<>]+?\?)(?:[^\s"'<>]*?(?:Signature|X-Amz-Signature|X-Amz-Credential|X-Amz-Security-Token|Policy|Key-Pair-Id|token|auth|sig|sign|Expires)=[^\s"'<>]*)/gi, "$1[REDACTED]") +} + +function isExecutableFile(filePath) { + if (!filePath) return false + try { + const st = fs.statSync(filePath) + if (!st.isFile()) return false + fs.accessSync(filePath, process.platform === "win32" ? fs.constants.F_OK : fs.constants.X_OK) + return true + } catch { + return false + } +} + +function pathExtensions() { + if (process.platform !== "win32") return [""] + const raw = process.env.PATHEXT || ".EXE;.CMD;.BAT;.COM" + const exts = raw + .split(";") + .map((s) => s.trim()) + .filter(Boolean) + return ["", ...exts] +} + +function searchPathFor(name, pathValue) { + if (!pathValue) return null + const dirs = pathValue.split(path.delimiter).filter(Boolean) + const exts = pathExtensions() + for (const dir of dirs) { + for (const ext of exts) { + const candidate = path.join(dir, ext && !name.toLowerCase().endsWith(ext.toLowerCase()) ? name + ext : name) + if (isExecutableFile(candidate)) return candidate + } + } + return null +} + +export function resolveCliBin({ env = process.env, homedir = os.homedir() } = {}) { + const override = String(env?.DREAMINA_CANVAS_BIN ?? "").trim() + if (override) { + if (override.includes(path.sep) || (path.altSeparator && override.includes(path.altSeparator))) { + return isExecutableFile(override) ? override : null + } + const foundOverride = searchPathFor(override, env?.PATH || env?.Path || "") + if (foundOverride) return foundOverride + return isExecutableFile(override) ? override : null + } + + const fromPath = searchPathFor("dreamina-canvas", env?.PATH || env?.Path || "") + if (fromPath) return fromPath + + const candidates = [ + path.join(homedir, ".local", "bin", process.platform === "win32" ? "dreamina-canvas.exe" : "dreamina-canvas"), + "/opt/homebrew/bin/dreamina-canvas", + "/usr/local/bin/dreamina-canvas", + ] + for (const candidate of candidates) { + if (isExecutableFile(candidate)) return candidate + } + return null +} + +export function isCliInstalled(opts) { + return resolveCliBin(opts) !== null +} + +export function setCliRunnerForTests(fn) { + testRunner = typeof fn === "function" ? fn : null + testRunnerGen++ +} + +export function hasCliRunnerForTests() { + return testRunner !== null +} + +export function getCliRunnerGen() { + return testRunnerGen +} + +export function parseJsonOutput(text) { + const raw = String(text ?? "").trim() + if (!raw) return null + try { + return JSON.parse(raw) + } catch { + const lines = raw.split(/\r?\n/) + for (let i = lines.length - 1; i >= 0; i--) { + const line = lines[i].trim() + if (!line.startsWith("{") && !line.startsWith("[")) continue + try { + return JSON.parse(line) + } catch { + // continue scanning + } + } + } + return null +} + +function classifyCliError(exitCode, cliErr, rawMessage) { + const cls = String(cliErr?.class || "").toLowerCase() + const code = String(cliErr?.code || "").toLowerCase() + const action = String(cliErr?.requiredAction || "").toLowerCase() + const msg = String(cliErr?.message || rawMessage || "") + + if ( + exitCode === 11 || + cls === "auth" || + action === "reauth" || + action === "login" || + /auth|unauthenticated|unauthorized|not_logged_in/.test(code) || + /not (?:logged|signed) in|unauthenticated|unauthorized|login required|please (?:log|sign) in|session.*expired|token.*expired/i.test(msg) + ) { + return { status: 401, code: cliErr?.code || "unauthorized", signIn: "expired" } + } + if (cls === "insufficient_credits" || /credit|balance|quota|积分不足/i.test(code + " " + msg)) { + return { status: 402, code: cliErr?.code || "insufficient_credits" } + } + if (cls === "invalid_input" || exitCode === 2) { + return { status: 400, code: cliErr?.code || "invalid_request_error" } + } + if (cls === "permission_denied" || cls === "forbidden") { + return { status: 403, code: cliErr?.code || "permission_denied" } + } + if (cls === "not_found") { + return { status: 404, code: cliErr?.code || "not_found" } + } + if (cls === "rate_limit") { + return { status: 429, code: cliErr?.code || "rate_limit_exceeded" } + } + if (exitCode === 20 || cls === "timeout" || code.includes("timeout")) { + return { status: 504, code: cliErr?.code || "timeout" } + } + return { status: 502, code: cliErr?.code || "cli_error" } +} + +function spawnCliProcess(bin, fullArgs, { signal, timeoutMs }) { + return new Promise((resolve, reject) => { + if (signal?.aborted) { + reject(createAbortError()) + return + } + let child + try { + child = spawn(bin, fullArgs, { + stdio: ["ignore", "pipe", "pipe"], + windowsHide: true, + }) + } catch (err) { + reject(new CliError(`Failed to start dreamina-canvas: ${redactSensitiveText(err?.message || String(err))}`, { status: 503, code: "cli_spawn_failed" })) + return + } + + let stdout = "" + let stderr = "" + let settled = false + let timedOut = false + let killEscalation = null + let timer = null + + const cleanup = () => { + if (timer) clearTimeout(timer) + if (killEscalation) clearTimeout(killEscalation) + signal?.removeEventListener("abort", onAbort) + } + + const killChild = () => { + if (child.exitCode !== null || child.signalCode !== null) return + try { + child.kill("SIGTERM") + } catch {} + killEscalation = setTimeout(() => { + if (child.exitCode === null && child.signalCode === null) { + try { + child.kill("SIGKILL") + } catch {} + } + }, 500) + killEscalation.unref?.() + } + + const onAbort = () => { + if (settled) return + settled = true + cleanup() + killChild() + reject(createAbortError()) + } + + if (signal) { + signal.addEventListener("abort", onAbort, { once: true }) + } + + if (Number.isFinite(timeoutMs) && timeoutMs > 0) { + timer = setTimeout(() => { + if (settled) return + timedOut = true + killChild() + settled = true + cleanup() + resolve({ exitCode: 124, stdout, stderr, timedOut: true }) + }, timeoutMs) + } + + child.stdout?.setEncoding("utf8") + child.stderr?.setEncoding("utf8") + child.stdout?.on("data", (chunk) => { stdout += chunk }) + child.stderr?.on("data", (chunk) => { stderr += chunk }) + + child.on("error", (err) => { + if (settled) return + settled = true + cleanup() + reject(new CliError(`dreamina-canvas process error: ${redactSensitiveText(err?.message || String(err))}`, { status: 502, code: "cli_process_error" })) + }) + + child.on("close", (code) => { + if (settled) return + settled = true + cleanup() + resolve({ exitCode: code ?? 1, stdout, stderr, timedOut }) + }) + }) +} + +export async function runCli(profile, args = [], opts = {}) { + const safeProfile = String(profile ?? "").trim() || "default" + const safeArgs = Array.isArray(args) ? args.map((v) => String(v)) : [] + const signal = opts?.signal + const timeoutMs = opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS + const check = opts?.check !== false + + if (signal?.aborted) throw createAbortError() + + const fullArgs = ["--profile", safeProfile, "--format", "json", "--non-interactive", ...safeArgs] + + let rawResult + if (testRunner) { + rawResult = await testRunner(safeProfile, safeArgs, { signal, timeoutMs, check, fullArgs }) + if (signal?.aborted) throw createAbortError() + } else { + const bin = resolveCliBin() + if (!bin) { + const err = new CliError("dreamina-canvas CLI not found on this machine. Install it or set DREAMINA_CANVAS_BIN.", { + status: 503, + code: "cli_not_found", + }) + if (check) throw err + return { ok: false, exitCode: 127, timedOut: false, payload: null, data: null, error: { code: "cli_not_found", message: err.message }, stderr: "" } + } + rawResult = await spawnCliProcess(bin, fullArgs, { signal, timeoutMs }) + } + + // Normalize either { code/exitCode, stdout, stderr } or pre-parsed { ok, data, error } + let exitCode = 0 + let stdout = "" + let stderr = "" + let timedOut = Boolean(rawResult?.timedOut) + let payload = null + + if (rawResult && typeof rawResult === "object" && ("stdout" in rawResult || "code" in rawResult || "exitCode" in rawResult) && !("ok" in rawResult && "data" in rawResult && typeof rawResult.stdout !== "string")) { + exitCode = rawResult.exitCode ?? rawResult.code ?? 0 + stdout = String(rawResult.stdout ?? "") + stderr = String(rawResult.stderr ?? "") + payload = parseJsonOutput(stdout) ?? parseJsonOutput(stderr) + } else if (rawResult && typeof rawResult === "object") { + payload = rawResult + exitCode = rawResult.exitCode ?? rawResult.code ?? (rawResult.ok === false ? 1 : 0) + stderr = String(rawResult.stderr ?? "") + } + + const cliErr = payload && typeof payload.error === "object" && payload.error ? payload.error : null + const ok = !timedOut && exitCode === 0 && (payload ? payload.ok !== false : true) + const data = payload && typeof payload === "object" && "data" in payload ? payload.data : payload + + if (!ok && check) { + const fallbackText = redactSensitiveText((stderr || stdout || "").trim().slice(-800)) + const rawMsg = timedOut + ? `dreamina-canvas ${safeArgs.slice(0, 2).join(" ")} timed out after ${Math.round(timeoutMs / 1000)}s` + : cliErr?.message || fallbackText || `dreamina-canvas ${safeArgs.slice(0, 2).join(" ")} failed (exit ${exitCode})` + const info = timedOut ? { status: 504, code: "timeout" } : classifyCliError(exitCode, cliErr, rawMsg) + throw new CliError(redactSensitiveText(rawMsg), { + status: info.status, + code: info.code, + exitCode, + cliError: cliErr, + payload, + signIn: info.signIn, + }) + } + + return { + ok, + exitCode, + timedOut, + payload, + data, + error: cliErr ?? (!ok ? { code: timedOut ? "timeout" : "cli_error", message: redactSensitiveText((stderr || stdout || "").trim() || `exit ${exitCode}`) } : null), + stderr: redactSensitiveText(stderr), + } +} diff --git a/packages/dreamina/index.mjs b/packages/dreamina/index.mjs new file mode 100644 index 000000000..ed87e3c8c --- /dev/null +++ b/packages/dreamina/index.mjs @@ -0,0 +1,556 @@ +import { getCreditStats, setCreditBalance } from "./canvas-state.mjs" +import { hasCliRunnerForTests, isCliInstalled, runCli } from "./cli.mjs" +import { DEFAULT_CREDIT_CEILING, createMediaFetch } from "./media.mjs" +import { BASE_URL, LIVE_META, NPM_IMAGE, configModels, fetchLiveModels, openCodeModelOf } from "./models.mjs" + +const PROVIDER_ID = "dreamina" +const PROVIDER_NAME = "Dreamina (即梦)" +const DREAMINA_ICON = + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAQKADAAQAAAABAAAAQAAAAABGUUKwAAALyUlEQVR4Ae1aeXBV1R3+7r1vfy+bCQFEilIWyygqi0XU1KI4LYpLrdVq3fjDURkdl4Ko1D/oDEpgbIVW3FArOkVbCw1arYOo4EqpgSgBg9AMCZCQPXlr3nL7/c57NwQcIcm7LA45k5Nz7jnnnnu+77ec3zmJBsBkPmGTfsIizwDvJ6BfA05wBvpN4ARXAPRrgF0aMOIkN4YXuO2a7qjNY4sGTB7qw4bZI/H5rBGYeLL3qC3ejg/ZQsCIIjcKCpMoKo7h6RkD4XVodqztqMxhCwFvV7WjIRgFfFGMOxeYd0PBUVm8HR+xhYCGUBIf1oaBwhSSjiAemOHBFed57FjfEZ/DFgJkle99FQRySEBuJzRvBM895sOoocYRB5DtB2wjYN22GKLxBIzcJKKBCIqHxfDqM17kB45/fyDH4awz/Z65eWWuaVZ7zfAezQzXwDRDMP/+smbq7LPjG0diDts0IEGIH2+JAfnM3iHQ8y9CNDYU19zkx5Ins1XUI/e+w86pv9gS506QghEdC4f7fmiuZqSiW3HX3dWIJytw733/tfNzts1lm3pOmgizM5RrJqPzzGQqZMZMU5WmuZu1LebSpx42DeO4Mwf7FjRwAMzm+kkE+74Z5u//pQiduYn1ELOkspV/MwcWF9tGOtUg27mynqBrAU4HzPKNMwmzxWzk7/KEaX7KvIm5knknyYiyfXNFhTlhwoSu92wA0ee5bHOCYpDcBVG7ezRr+QhySSHmuhTbWO5hrkkC5cz+M8/EG++txf2zZsHhsNUNyTJ6lWwlQL7c3p4DYkYHAUdYtrKsZcPuDAl7Wd9KX7kjNwd3lJbi9Q8+QMnUqfLqMUn2ExCMgRshggTK2FBpgYCupeSFBCFDtKG6E9hAjQmcfz4e//e7eHJ1GUoumwan03lUibBd/1rCIfBUgFAGvNRFC4QUL0lwMTB0se5mm+RGtrkZMQ+5fDrmMNd+UY5PSEb5mjXYvnkzwh0dHN3z5PV4EInyYNbDZDsB4WSqS/LiA6zcyAWJbOWI5GK7IkHqkklCI7XBRSLyx52DnzFPe+R3aK2pQX3FZtTs3IE9X1ehva4O0WAHzARDbl2Hx+1Gfn4BBg0ehDGjRuLUU4ZixIgR2FOzC/fcex8qKys5+6GT7QTobhdEZuIElSNkXXxBC1VfaQE1QO6N3KIJHCOlU0ppIwkNoiWsewwdgdOGYRDzaXz2MUu7k/3cbBSRPsbY3sx8cuxiVaWS00/HU8uW4dKLLkJnTL763cl2Anw5OWjj9ywCrFJMookkOAWwLFq0IFMq8OzvToYiR7SC7Wpc5j0PS5emqXY355NxakyGTKnXEVXR+AkI5Oejub6eLd+d7CcgN09pgOUHFAH8vhBgJk3EudA4XW+YIPgDIwNMSJDFW2QoAqRNAPMdBTzzLGPEnNR4RUjatESTXJx7KPvKVqxAS6MY3qGTrQTohgOBHwxTGqCcIL8tRMhuEKO0dKqvJqcmLhrUWZNlkjnBRUe6ESKLspyl8hl87vIdHC+aYpGgiBPgHCPOdABfXvHyciybOZOE84OHSbYSUFBUCP/IkWjigpT9Z0qpp0iAYbJCLVCxJ0tNVEB+mOWC3mRWhPA5SvuWdha0+YzZsFQawilUyT5lAnym80ceCfho4UK8M3v2YWDv77aVgOFjz4KbAU4rbVc0QICLQ4yy1AW4CMQquXjobMuQIPWDCRFSUhwXy+SQkMLXxGws0xAifAE+72vA2jkPYeeLy9jS82QrAaMvuABhLjLIICdEicsWaElf57MKEdkGMQMpCRAaKyJmQSZ1JXarXR7TmpAem9aSOMd2ylA6Bhe3h/p167GXKh/56ku+2LtkGwFi/+OnXwFxOwo4S5F+RBYqUk9ZmY1KC/jcBZxtigCWohXd2xVJ+/s1kqRpOoyTeM0U7kDj/FI0LSglqWS9D8k2AsZMnozisWdiG+N8ywGK9BNi+yJ9C7SU/OnSAtnApcECbWlFN9NQWiEEcZzmc8DIJ7Hvr8G+ObMR3VQuHX1OthEwZcYMhBm8KPUnHgEfZlbSVxrANSotkFLaWTJqRILICNq0/MFBEleqL86Tp0bHQB2JfbWof3Q+Wp9d2mfQ3V+0hYAho0Zhwi9/hWqCUs6PX5DToOz5Ytbdgau6pQVWSdAicKX+YgsZLZBtEgx5jQJucloCrc8vRfMfShGv3cUOe5ItBFw/91EkAl60Mua1vL/4AfH6aftnXaSvpM5STMJ6FhsR9GIKAlg5QpZ8MHK90PzUpI/eQuMT8xHZ+Il0sEuH7nIjFZMgO7skXGeVxk25GJNv+DVqafsq+iMecX7ipdNbXzfgIvEMKaYigQOlLc4HeUFyzIRueOE4yYv49s+w5/YrUXPD5V3gXaf+EIUz53Lfkw0w+5QVAYG8PNy9ZDEaafsdBGZFfUr6FkApxYZF4vxJl3RmAtzSggx43fDAUeBHYncF6h66DdXXlKDj3bIulIEp0zDstXcQrdiAVEdrV3s2laxM4P4/PonCMWOwiQcuBZ6YxPYl7E0DZKW72ivQ5EG0QIhRfdQUpwe634nOXRVoWbwUbW8u53W6GFM6OQacjKI7HkHhnXeh+flnEVr3jtWVddlnAm574Le49NZbsJGqL3u9BD7i+SXuF/XWRboCUoW/LAUsn9PA2ceYV/d6oedo6KzahJbX/4zWd/8KsxtwvoH8K29D0e1z4RwyHJ3bmtDwp99Ls22pTwRMv/563FO6AF8SVJigLM8v4CXs1RT4DAEZqStbVxEgvXqOh2P4N8RN6wn8ObS/9w+YMdGh/ck/8RIU3fwgfOMuRqItBpOOpX31K2ob3D8q+1qvCbjsqquw6IUX8DW3pzYJebkGdf9HvAeEvUq9RdIcwLOB7nApaZsdUYQ+/idaVzyH4Lq30+rSDYfvjAtRcO0DCJw7jabi4JZH5PQxKS2C1rIXu420p9orAq697joseeklVPHo1UzwEugo1edavhX4ELQc73Q/MyO3RM0etPGM3v7Gq4hu+eKg1Wvwj78E+T+fCf9ZBK45KWmhlnd7jBCNHB8iX65HbEfFQe9l/9hjAn5z4414VsAzImuULU8kzu/LMq26BHY6j286j2p6LvF3xBHd+CHaX1uB4NtlSDTUHbBiI68QgfOmIW/qrfAML6H9O5BolAtNelU58nG/l9gfAUaYn1Bb1DZywBRZP/SIAL/fj3mPPY4mAU/JWtudpfod3EwjnMngNY1ORuJVlQi+tRrBspWI/OfzAxap0RS850xGzk+uRs6Pr+R+PwzJNhPJlij9AG1drosYFaldhBykdAN6kERuXX/APHY99IgAjfYOV/qyU0mcjzG+qY6lXEmY+15k29eIrl2D0FtvIrJ+PbexdJSmOZwwBg6G94yz4Rv/Uzq1KXAOHk0nyEiuJUJV5y0vzclMclLGvmqL5PlA/qlA3RG4nIjv3YvYrkq7MB8wT48ICPJufuGCx3HfokVwOXTEYnHsq6vHzq2VqPrsU9StXYvQhg2U4Lfv490/Ogt5194M3xljeJgZC8NbxIhPQ0r44a4ggNWtB83HqqfkgETNpwVAcxiIN1VzvFy12p/4pZ4b1jklJQgMGoTq7d+gbsc3iLe3H35Foj2yLfKEI/u+67TR8IwcD//EqSxL4PAVI9nK/y1qp93Tt4hapaJ8R4jhCdHwuxGpWoW9z1x9+G/1YUSvCOjD/Id8xTHgFORM+gVyS26Bc9A4mgSJkPO03JJSS0Q0esCJ0Obl2Lfi5kPO1ddOfunYpURDLVpWL8auhyej4S93UvI1lDhvN0mAmINJEkwqhvIPR2iZx5QAC5MZj6FtzdOofexCBMtf4b5P15Ti338yBBiuAmuo7eVxQYCFKtFSg30v3YTmfz0Iwyc3wLwkoLPU3UPEG1rDbC2PKwIsZK3vl6Jx1R38J6swNP65J7x1ldiB1W1reUyd4OGQuIrPhu7JRXTXusMN7XP/cU1An1H14sXj0gR6sf6sh/YTkDWF3/MJ+jXgey7ArJffrwFZU/g9n+D/f/TWE3PaonQAAAAASUVORK5CYII=" +const DEFAULT_PROFILE = "default" +const PROFILE_PATTERN = /^[A-Za-z0-9._-]{1,64}$/ + +const VIP_LEVEL_LABELS = { + maestro: "大师会员", + advanced: "高级会员", + pro: "专业会员", + standard: "标准会员", + basic: "基础会员", +} + +function formatVipStatus(acct) { + const isVip = Boolean(acct?.isVip) + if (!isVip) return "普通用户" + const rawLevel = typeof acct?.vipLevel === "string" ? acct.vipLevel.trim() : "" + if (!rawLevel) return "VIP 会员" + const tierLabel = VIP_LEVEL_LABELS[rawLevel.toLowerCase()] ?? rawLevel + return `VIP · ${tierLabel}` +} + +function parseNonNegNumber(val) { + if (val === null || val === undefined || typeof val === "boolean") return null + if (typeof val === "string") { + const trimmed = val.trim() + if (!trimmed || !/^\d+(?:\.\d+)?$/.test(trimmed)) return null + val = Number(trimmed) + } + return typeof val === "number" && Number.isFinite(val) && val >= 0 ? val : null +} + +function extractLiveBalance(acct) { + if (!acct || typeof acct !== "object") return null + for (const key of ["credits", "balance", "remainingCredits", "points"]) { + const parsed = parseNonNegNumber(acct[key]) + if (parsed !== null) return parsed + } + return null +} + +function resolveActiveGuardLabel(magpie) { + const quoteOnly = typeof magpie?.option === "function" && magpie.option(PROVIDER_ID, "quote_only") === true + if (quoteOnly) return "仅报价预演(不扣点)" + const guardRaw = typeof magpie?.option === "function" ? magpie.option(PROVIDER_ID, "credit_guard") : undefined + const guardEnabled = guardRaw === undefined ? true : Boolean(guardRaw) + if (!guardEnabled) return "无上限保护" + const envRaw = process.env.DREAMINA_CREDIT_CEILING + const envVal = envRaw !== undefined && envRaw !== "" ? Number(envRaw) : NaN + const ceiling = Number.isInteger(envVal) && envVal >= 0 ? envVal : DEFAULT_CREDIT_CEILING + return `单次上限 ${ceiling} 点` +} + +function normalizeProfile(raw) { + const s = String(raw ?? "").trim() + if (!s) return DEFAULT_PROFILE + if (!PROFILE_PATTERN.test(s)) { + throw new Error("Invalid Dreamina profile name. Use letters, digits, '.', '_', or '-' (max 64 chars).") + } + return s +} + +function profileOfAuth(auth) { + const raw = auth?.metadata?.profile ?? auth?.profile ?? DEFAULT_PROFILE + const s = String(raw ?? "").trim() + return s && PROFILE_PATTERN.test(s) ? s : DEFAULT_PROFILE +} + +function parseExpiresMs(expiresAt) { + if (typeof expiresAt === "number" && Number.isFinite(expiresAt) && expiresAt > 0) { + return expiresAt < 1e11 ? Math.round(expiresAt * 1000) : Math.round(expiresAt) + } + if (typeof expiresAt === "string") { + const trimmed = expiresAt.trim() + if (!trimmed) return 0 + if (/^\d+(?:\.\d+)?$/.test(trimmed)) { + const n = Number(trimmed) + return Number.isFinite(n) && n > 0 ? (n < 1e11 ? Math.round(n * 1000) : Math.round(n)) : 0 + } + const ms = Date.parse(trimmed) + return Number.isFinite(ms) && ms > 0 ? ms : 0 + } + return 0 +} + +function cliReady() { + return hasCliRunnerForTests() || isCliInstalled() +} + +function cliMissingFailure() { + return { + type: "failed", + error: "dreamina-canvas CLI not found on this machine. Install dreamina-canvas and ensure it is on PATH (or set DREAMINA_CANVAS_BIN).", + } +} + +function isAuthExpiredResult(res) { + if (!res) return false + if (res.exitCode === 11) return true + if (res.ok && res.data?.loggedIn === false) return true + const cls = String(res.error?.class || "").toLowerCase() + const action = String(res.error?.requiredAction || "").toLowerCase() + const code = String(res.error?.code || "").toLowerCase() + const msg = String(res.error?.message || "") + return ( + cls === "auth" || + action === "reauth" || + action === "login" || + /auth|unauthenticated|unauthorized|not_logged_in/.test(code) || + /not (?:logged|signed) in|unauthenticated|unauthorized|login required|session.*expired|token.*expired/i.test(msg) + ) +} + +async function finalizeProfileSignIn(profile, { statusData, initialCredits, directory, signal } = {}) { + let finalStatus = statusData + if (!finalStatus) { + const statusRes = await runCli(profile, ["auth", "status"], { signal, timeoutMs: 15_000, check: false }) + if (!statusRes.ok || !statusRes.data?.loggedIn) { + return { + type: "failed", + error: statusRes.error?.message || `Profile "${profile}" is not signed in to dreamina-canvas.`, + } + } + finalStatus = statusRes.data + } + + const parsedCredits = parseNonNegNumber(initialCredits) + if (parsedCredits !== null) { + await setCreditBalance(profile, { balance: parsedCredits, directory }) + } + + const accountRes = await runCli(profile, ["auth", "account"], { signal, timeoutMs: 15_000, check: false }) + const acct = accountRes?.ok ? accountRes.data : null + const userId = acct?.userId != null ? String(acct.userId) : "" + + const accountLabel = userId ? `${profile} (${userId})` : profile + + return { + type: "success", + access: `dreamina-profile:${profile}`, + refresh: `dreamina-profile:${profile}`, + expires: parseExpiresMs(finalStatus?.expiresAt), + accountId: accountLabel, + account: accountLabel, + ...(userId ? { uid: userId } : {}), + metadata: { + profile, + region: typeof finalStatus?.region === "string" && finalStatus.region ? finalStatus.region : "cn", + isVip: Boolean(acct?.isVip), + vipLevel: typeof acct?.vipLevel === "string" ? acct.vipLevel : "", + }, + } +} + +const PROFILE_PROMPTS = [ + { + type: "text", + key: "profile", + message: "dreamina-canvas profile name (leave blank for default)", + messages: { + zh: "dreamina-canvas profile 名称(留空则使用 default)", + "zh-TW": "dreamina-canvas profile 名稱(留空則使用 default)", + }, + placeholder: DEFAULT_PROFILE, + required: false, + validate: (value) => { + const v = String(value ?? "").trim() + if (!v) return null + if (!PROFILE_PATTERN.test(v)) { + return "Profile name must use letters, digits, '.', '_', or '-' (max 64 chars)" + } + return null + }, + }, + { + type: "text", + key: "credits", + message: "Current credit balance (optional, e.g. 1000)", + messages: { + zh: "当前即梦点数余额(选填,如 1000;填入后自动按次扣减)", + "zh-TW": "目前即夢點數餘額(選填,如 1000;填入後自動按次扣減)", + }, + placeholder: "选填,留空则仅统计已用点数", + required: false, + validate: (value) => { + const v = String(value ?? "").trim() + if (!v) return null + if (!/^\d+(?:\.\d+)?$/.test(v) || Number(v) < 0) { + return "Must be a non-negative number" + } + return null + }, + }, +] + +export const DreaminaAuthPlugin = async (input = {}) => { + const directory = input?.directory || process.cwd() + const magpie = input?.magpie + + return { + config: async (cfg) => { + cfg.provider ??= {} + const existing = cfg.provider[PROVIDER_ID] ?? {} + cfg.provider[PROVIDER_ID] = { + ...existing, + id: PROVIDER_ID, + name: existing.name ?? PROVIDER_NAME, + npm: existing.npm ?? NPM_IMAGE, + api: existing.api ?? BASE_URL, + models: { ...configModels(), ...(existing.models ?? {}) }, + } + }, + auth: { + provider: PROVIDER_ID, + icon: DREAMINA_ICON, + maxConcurrency: 4, + cli: { + name: "Dreamina Canvas CLI (dreamina-canvas)", + manual: "Install dreamina-canvas locally and ensure it is on PATH (or set DREAMINA_CANVAS_BIN).", + installed: async () => isCliInstalled(), + install: async () => { + if (isCliInstalled()) return + throw new Error("dreamina-canvas is not installed automatically. Install dreamina-canvas locally and ensure it is on PATH (or set DREAMINA_CANVAS_BIN).") + }, + }, + options: [ + { + key: "quote_only", + label: "Quote only", + labels: { zh: "仅报价(不扣积分)", "zh-TW": "僅報價(不扣積分)" }, + description: "Quote credits only without running generation.", + descriptions: { + zh: "只询价并返回预估点数,不执行生成", + "zh-TW": "只詢價並回傳預估點數,不執行生成", + }, + default: false, + }, + { + key: "credit_guard", + label: "200-credit ceiling", + labels: { zh: "单次 200 点上限保护", "zh-TW": "單次 200 點上限保護" }, + description: "Block runs quoted above 200 credits.", + descriptions: { + zh: "单次报价超过 200 点时自动拦截", + "zh-TW": "單次報價超過 200 點時自動攔截", + }, + default: true, + }, + ], + methods: [ + { + type: "oauth", + label: "Use existing dreamina-canvas CLI profile", + labels: { + zh: "使用本机已有 dreamina-canvas 登录配置", + "zh-TW": "使用本機已有 dreamina-canvas 登入設定", + }, + prompts: PROFILE_PROMPTS, + authorize: async (inputs = {}, opts = {}) => { + if (!cliReady()) return cliMissingFailure() + let profile + try { + profile = normalizeProfile(inputs?.profile) + } catch (err) { + return { type: "failed", error: err.message } + } + + const statusRes = await runCli(profile, ["auth", "status"], { + signal: opts?.signal, + timeoutMs: 15_000, + check: false, + }) + if (!statusRes.ok || !statusRes.data?.loggedIn) { + return { + type: "failed", + error: + statusRes.error?.message || + `dreamina-canvas profile "${profile}" is not signed in. Use browser sign-in or run \`dreamina-canvas --profile ${profile} auth login\`.`, + } + } + + return { + url: "", + instructions: "", + method: "auto", + callback: async (_code, cbOpts = {}) => + finalizeProfileSignIn(profile, { + statusData: statusRes.data, + initialCredits: inputs?.credits, + directory, + signal: cbOpts?.signal ?? opts?.signal, + }), + } + }, + }, + { + type: "oauth", + label: "Sign in with Dreamina (browser)", + labels: { + zh: "使用即梦浏览器登录", + "zh-TW": "使用即夢瀏覽器登入", + }, + prompts: PROFILE_PROMPTS, + authorize: async (inputs = {}, opts = {}) => { + if (!cliReady()) return cliMissingFailure() + let profile + try { + profile = normalizeProfile(inputs?.profile) + } catch (err) { + return { type: "failed", error: err.message } + } + + const loginRes = await runCli(profile, ["auth", "login"], { + signal: opts?.signal, + timeoutMs: 30_000, + check: false, + }) + if (!loginRes.ok) { + return { + type: "failed", + error: loginRes.error?.message || "Failed to start Dreamina browser sign-in.", + } + } + + const data = loginRes.data ?? {} + if (data.state?.loggedIn === true && !data.challenge?.deviceCode) { + return { + url: "", + instructions: "", + method: "auto", + callback: async (_code, cbOpts = {}) => + finalizeProfileSignIn(profile, { + statusData: { ...data, ...data.state }, + initialCredits: inputs?.credits, + directory, + signal: cbOpts?.signal ?? opts?.signal, + }), + } + } + + const challenge = data.challenge + if (!challenge?.deviceCode) { + return { + type: "failed", + error: "dreamina-canvas auth login did not return a deviceCode challenge.", + } + } + + const url = challenge.verificationUriComplete || challenge.verificationUri || "" + const instructions = challenge.userCode + ? `Open the link in your browser to sign in to Dreamina (user code: ${challenge.userCode}).` + : "Open the link in your browser to sign in to Dreamina." + + return { + url, + instructions, + method: "auto", + callback: async (_code, cbOpts = {}) => { + const waitSignal = + opts?.signal && cbOpts?.signal + ? AbortSignal.any([opts.signal, cbOpts.signal]) + : cbOpts?.signal ?? opts?.signal + const waitRes = await runCli( + profile, + ["auth", "wait", "--device-code", challenge.deviceCode, "--timeout", "10m"], + { signal: waitSignal, timeoutMs: 610_000, check: false }, + ) + if (!waitRes.ok || !waitRes.data?.state?.loggedIn) { + return { + type: "failed", + error: waitRes.error?.message || "Dreamina browser sign-in did not complete.", + } + } + return finalizeProfileSignIn(profile, { + statusData: { ...waitRes.data, ...waitRes.data.state }, + initialCredits: inputs?.credits, + directory, + signal: waitSignal, + }) + }, + } + }, + }, + ], + refresh: async (auth) => { + if (!cliReady()) { + throw new Error("dreamina-canvas CLI not found on this machine.") + } + const profile = profileOfAuth(auth) + const res = await runCli(profile, ["auth", "refresh"], { + timeoutMs: 30_000, + check: false, + }) + if (res.ok && res.data?.loggedIn) { + return { + access: `dreamina-profile:${profile}`, + refresh: `dreamina-profile:${profile}`, + expires: parseExpiresMs(res.data.expiresAt), + metadata: { + ...(auth?.metadata ?? {}), + profile, + region: typeof res.data.region === "string" && res.data.region ? res.data.region : auth?.metadata?.region || "cn", + }, + } + } + if (isAuthExpiredResult(res)) { + throw Object.assign( + new Error(res.error?.message || `Dreamina profile "${profile}" session expired; sign in again.`), + { signIn: "expired" }, + ) + } + throw new Error(res.error?.message || "Failed to refresh Dreamina session.") + }, + usage: async (getAuth, _provider, opts = {}) => { + if (!cliReady()) { + return { error: "dreamina-canvas CLI not found on this machine.", signIn: "kept" } + } + const auth = await getAuth() + const profile = profileOfAuth(auth) + const statusRes = await runCli(profile, ["auth", "status"], { + signal: opts?.signal, + timeoutMs: 15_000, + check: false, + }) + if (!statusRes.ok || !statusRes.data?.loggedIn) { + return { + error: statusRes.error?.message || `Profile "${profile}" is not signed in to Dreamina.`, + signIn: "expired", + } + } + const accountRes = await runCli(profile, ["auth", "account"], { + signal: opts?.signal, + timeoutMs: 15_000, + check: false, + }) + const acct = accountRes?.ok + ? accountRes.data + : auth?.metadata && ("isVip" in auth.metadata || "vipLevel" in auth.metadata) + ? { isVip: Boolean(auth.metadata.isVip), vipLevel: auth.metadata.vipLevel || "" } + : null + const plan = formatVipStatus(acct) + const user = acct?.userId != null ? `${profile} (${acct.userId})` : profile + const stats = await getCreditStats(profile, { directory }).catch(() => ({ + todayCredits: 0, + totalCredits: 0, + lastQuotedCredits: null, + lastSpentCredits: null, + runCount: 0, + configuredBalance: null, + spentSinceBalanceSet: 0, + remainingCredits: null, + })) + const liveBalance = extractLiveBalance(acct) + const envBalance = parseNonNegNumber(process.env.DREAMINA_CREDITS_BALANCE) + const configuredBalance = parseNonNegNumber(stats.configuredBalance) + const fallbackBalance = envBalance ?? configuredBalance + + if (liveBalance !== null) { + return { + plan, + user, + balance: `剩余 ${liveBalance} 点`, + windows: [ + { + name: "会员点数", + display: `剩余 ${liveBalance} 点 · 今日已用 ${stats.todayCredits} 点`, + aside: true, + }, + ], + signIn: "kept", + } + } + + if (fallbackBalance !== null && fallbackBalance > 0) { + const totalBalance = fallbackBalance + const used = envBalance !== null ? stats.todayCredits : (stats.spentSinceBalanceSet ?? 0) + const remaining = Math.max(0, totalBalance - used) + const usedPct = Math.min(100, Math.round((used / totalBalance) * 1000) / 10) + return { + plan, + user, + balance: `剩余 ${remaining} 点`, + windows: [ + { + name: "会员点数", + used: usedPct, + amount: used, + limit: totalBalance, + unit: "credits", + display: `剩余 ${remaining} / ${totalBalance} 点 · 今日已用 ${stats.todayCredits} 点`, + aside: true, + }, + ], + signIn: "kept", + } + } + + const guardLabel = resolveActiveGuardLabel(magpie) + return { + plan, + user, + windows: [ + { + name: "今日点数", + display: + stats.totalCredits > 0 + ? `已用 ${stats.todayCredits} 点 · 累计 ${stats.totalCredits} 点(${guardLabel})` + : `已用 0 点 · ${guardLabel}`, + aside: true, + }, + ], + signIn: "kept", + } + }, + loader: async (getAuth) => { + const auth = await getAuth() + const initialProfile = profileOfAuth(auth) + return { + baseURL: BASE_URL, + apiKey: "dreamina-local", + fetch: createMediaFetch({ + profile: initialProfile, + getProfile: async () => profileOfAuth(await getAuth()), + directory, + getOption: (key) => (typeof magpie?.option === "function" ? magpie.option(PROVIDER_ID, key) : undefined), + }), + } + }, + }, + provider: { + id: PROVIDER_ID, + npm: NPM_IMAGE, + models: async (_given, ctx) => { + const profile = profileOfAuth(ctx?.auth) + const specs = await fetchLiveModels(profile, { signal: ctx?.signal }) + const out = {} + for (const [id, spec] of Object.entries(specs)) { + out[id] = openCodeModelOf(PROVIDER_ID, spec) + } + if (specs?.[LIVE_META]?.fellBack) { + out[Symbol.for("magpie.fellBack")] = true + } + return out + }, + }, + } +} + +export default { id: PROVIDER_ID, server: DreaminaAuthPlugin } diff --git a/packages/dreamina/magpie.mjs b/packages/dreamina/magpie.mjs new file mode 100644 index 000000000..669d09f58 --- /dev/null +++ b/packages/dreamina/magpie.mjs @@ -0,0 +1,2 @@ +// The Magpie server plugin entry point. Export only the plugin function. +export { DreaminaAuthPlugin } from "./index.mjs" diff --git a/packages/dreamina/media.mjs b/packages/dreamina/media.mjs new file mode 100644 index 000000000..6e5886c5b --- /dev/null +++ b/packages/dreamina/media.mjs @@ -0,0 +1,1249 @@ +import { randomUUID } from "node:crypto" +import fs from "node:fs" +import os from "node:os" +import path from "node:path" +import { pathToFileURL } from "node:url" +import { ensureCanvas, getVideoTask, newNodeId, recordCreditQuote, recordCreditSpend, saveVideoTask } from "./canvas-state.mjs" +import { CliError, hasCliRunnerForTests, isAbortError, isCliInstalled, redactSensitiveText, runCli } from "./cli.mjs" +import { ModelParamError, fetchLiveModels, findModelSpec, resolveImageParams, resolveVideoParams } from "./models.mjs" + +export const DEFAULT_CREDIT_CEILING = 200 + +const ALLOWED_IMAGE_EXTS = new Set([".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp"]) + +function jsonResponse(status, body, extraHeaders = {}) { + return new Response(JSON.stringify(body), { + status, + headers: { + "Content-Type": "application/json", + ...extraHeaders, + }, + }) +} + +export function errorResponse(status, message, { type, code, extra, headers, topLevelType } = {}) { + const errType = + type ?? + (status === 400 + ? "invalid_request_error" + : status === 401 + ? "authentication_error" + : status === 403 + ? "permission_error" + : status === 404 + ? "not_found_error" + : "api_error") + const outHeaders = { ...(headers ?? {}) } + if (status === 401 && !outHeaders["X-Magpie-Sign-In"] && !outHeaders["x-magpie-sign-in"]) { + outHeaders["X-Magpie-Sign-In"] = "expired" + } + return jsonResponse( + status, + { + ...(topLevelType ? { type: topLevelType } : {}), + error: { + message: redactSensitiveText(String(message || "Unknown Dreamina error")), + type: errType, + ...(code ? { code } : {}), + ...(extra ?? {}), + }, + }, + outHeaders, + ) +} + +function errorToResponse(err, { topLevelType } = {}) { + if (isAbortError(err)) throw err + if (err instanceof ModelParamError) { + return errorResponse(err.status, err.message, { code: err.code, topLevelType }) + } + if (err instanceof CliError) { + return errorResponse(err.status, err.message, { + code: err.code, + headers: err.signIn ? { "X-Magpie-Sign-In": err.signIn } : undefined, + topLevelType, + }) + } + const status = Number.isInteger(err?.status) && err.status >= 400 && err.status < 600 ? err.status : 502 + return errorResponse(status, err?.message || String(err), { code: err?.code, topLevelType }) +} + +function headerValue(headers, name) { + if (!headers) return "" + if (typeof headers.get === "function") return headers.get(name) ?? "" + const lower = name.toLowerCase() + for (const [k, v] of Object.entries(headers)) { + if (k.toLowerCase() === lower) return String(v ?? "") + } + return "" +} + +async function readRawRequest(input, init = {}) { + const req = input instanceof Request ? input : null + const method = String(init.method ?? req?.method ?? "GET").toUpperCase() + const url = typeof input === "string" ? input : input instanceof URL ? input.href : req?.url ?? "" + const headers = new Headers(init.headers ?? req?.headers ?? undefined) + const signal = init.signal ?? req?.signal + const body = init.body !== undefined ? init.body : req ? req : undefined + return { method, url, headers, signal, body } +} + +function parseScalarValue(raw) { + if (typeof raw !== "string") return raw + const t = raw.trim() + if (t === "true") return true + if (t === "false") return false + return raw +} + +function isMultipartContentType(ct) { + return /\bmultipart\/form-data\b/i.test(ct) +} + +function sniffMultipartBoundary(body) { + let head = "" + if (typeof body === "string") head = body.slice(0, 256) + else if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) { + head = Buffer.from(body.buffer ?? body, body.byteOffset ?? 0, Math.min(body.byteLength ?? 0, 256)).toString("latin1") + } + const m = /^--([^\r\n]{1,128})\r?\nContent-Disposition:/i.exec(head) + return m ? m[1] : "" +} + +async function parseRequestPayload(rawReq) { + const { body, headers } = rawReq + const contentType = headerValue(headers, "content-type") + + if (body instanceof FormData) { + return formDataToRequest(body) + } + + if (body instanceof Request) { + if (isMultipartContentType(contentType || body.headers.get("content-type") || "")) { + const fd = await body.formData() + return formDataToRequest(fd) + } + const text = await body.text() + return parseJsonText(text) + } + + if (isMultipartContentType(contentType)) { + const fd = await new Response(body ?? "", { + headers: { "content-type": contentType }, + }).formData() + return formDataToRequest(fd) + } + + if (!contentType) { + const sniffed = sniffMultipartBoundary(body) + if (sniffed) { + const fd = await new Response(body, { + headers: { "content-type": `multipart/form-data; boundary=${sniffed}` }, + }).formData() + return formDataToRequest(fd) + } + } + + if (body == null || body === "") return {} + if (typeof body === "string") return parseJsonText(body) + if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) { + return parseJsonText(new TextDecoder().decode(body)) + } + if (typeof body === "object" && !(body instanceof ReadableStream)) { + return body + } + const text = await new Response(body).text() + return parseJsonText(text) +} + +function parseJsonText(text) { + const trimmed = String(text ?? "").trim() + if (!trimmed) return {} + try { + const parsed = JSON.parse(trimmed) + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { + throw new ModelParamError("Request JSON body must be an object.") + } + return parsed + } catch (err) { + if (err instanceof ModelParamError) throw err + throw new ModelParamError(`Invalid JSON request body: ${err?.message || err}`) + } +} + +const REF_ARRAY_KEYS = new Set([ + "image", + "image[]", + "images", + "images[]", + "reference_images", + "reference_images[]", + "input_reference", + "input_reference[]", + "refs", + "refs[]", +]) + +function formDataToRequest(fd) { + const out = {} + for (const [key, val] of fd.entries()) { + const cleanKey = key.endsWith("[]") ? key.slice(0, -2) : key + if (REF_ARRAY_KEYS.has(key)) { + out[cleanKey] ??= [] + out[cleanKey].push(val) + continue + } + if (key === "first_frame" || key === "first_frame_image" || key === "last_frame" || key === "last_frame_image") { + out[key] = val + continue + } + if (typeof val === "string") { + out[cleanKey] = parseScalarValue(val) + } + } + return out +} + +function extFromMime(mime) { + const m = String(mime ?? "").toLowerCase().split(";")[0].trim() + if (m === "image/png") return ".png" + if (m === "image/jpeg" || m === "image/jpg") return ".jpg" + if (m === "image/webp") return ".webp" + if (m === "image/gif") return ".gif" + if (m === "image/bmp") return ".bmp" + return ".png" +} + +function extFromName(name) { + const ext = path.extname(String(name ?? "")).toLowerCase() + return ALLOWED_IMAGE_EXTS.has(ext) ? ext : "" +} + +function normalizeRefInputList(raw) { + if (raw == null) return [] + return Array.isArray(raw) ? raw : [raw] +} + +function collectRefInputs(req) { + const items = [] + const first = req.first_frame ?? req.first_frame_image + const last = req.last_frame ?? req.last_frame_image + if (first != null && first !== "") items.push(first) + if (last != null && last !== "") items.push(last) + for (const key of ["input_reference", "image", "images", "reference_images", "refs"]) { + for (const entry of normalizeRefInputList(req[key])) { + if (entry != null && entry !== "") items.push(entry) + } + } + return items +} + +export function escapePromptBraces(prompt) { + const s = String(prompt ?? "") + if (!s.includes("{{")) return s + let out = "" + for (let i = 0; i < s.length; i++) { + if (s[i] === "{" && s[i + 1] === "{") { + let backslashes = 0 + for (let j = i - 1; j >= 0 && s[j] === "\\"; j--) backslashes++ + const alreadyEscaped = backslashes % 2 === 1 + const validRef = !alreadyEscaped && /^\{\{(?:node|res|uri|vid):[^{}\s]+\}\}/.test(s.slice(i)) + if (!alreadyEscaped && !validRef) { + out += "\\{{" + } else { + out += "{{" + } + i++ + } else { + out += s[i] + } + } + return out +} + +function nodeTitleFromPrompt(prompt, fallback) { + const normalized = String(prompt ?? "").replace(/\s+/g, " ").trim() + if (!normalized) return fallback + return Array.from(normalized).slice(0, 80).join("") +} + +function parseDataUrl(value) { + const match = /^data:([^;,]+)?(?:;charset=[^;,]+)?;base64,([A-Za-z0-9+/=\s]+)$/i.exec(value) + if (!match) { + throw new ModelParamError("Unsupported data: URL format; expected base64-encoded image data URL.") + } + const mime = (match[1] || "image/png").toLowerCase() + if (!mime.startsWith("image/") && mime !== "application/octet-stream") { + throw new ModelParamError(`Unsupported reference MIME type: ${mime}`) + } + const buf = Buffer.from(match[2].replace(/\s+/g, ""), "base64") + if (!buf.length) throw new ModelParamError("Empty base64 image data URL.") + return { buf, ext: extFromMime(mime) } +} + +async function uploadRefItems(profile, projectId, items, { signal }) { + if (!items.length) return [] + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "dreamina-refs-")) + const refTokens = [] + + try { + for (let i = 0; i < items.length; i++) { + let item = items[i] + const isBlobLike = item instanceof Blob || (item && typeof item === "object" && typeof item.arrayBuffer === "function") + if (item && typeof item === "object" && !isBlobLike) { + if (typeof item.b64_json === "string") { + item = { b64: item.b64_json, ext: extFromMime(item.mime_type || item.mimeType) } + } else if (typeof item.data === "string" && !item.data.startsWith("data:") && !item.data.startsWith("http")) { + item = { b64: item.data, ext: extFromMime(item.mime_type || item.mimeType) } + } else { + const urlVal = item.url ?? item.image_url?.url ?? item.image_url ?? item.data + if (typeof urlVal === "string") item = urlVal + } + } + + if (typeof item === "string") { + const s = item.trim() + if (/^(?:res|node|vid):/i.test(s)) { + refTokens.push(s) + continue + } + if (/^https:\/\//i.test(s)) { + const resourceId = randomUUID() + const up = await runCli( + profile, + ["resource", "upload", "--source-url", s, "--type", "image", "--project-id", projectId, "--resource-id", resourceId], + { signal }, + ) + const rid = up.data?.resourceId || resourceId + refTokens.push(`res:${rid}`) + continue + } + if (/^data:/i.test(s)) { + const { buf, ext } = parseDataUrl(s) + const filePath = path.join(tmpDir, `ref-${i}${ext}`) + fs.writeFileSync(filePath, buf, { mode: 0o600 }) + const resourceId = randomUUID() + const up = await runCli( + profile, + ["resource", "upload", "--file", filePath, "--type", "image", "--project-id", projectId, "--resource-id", resourceId], + { signal }, + ) + const rid = up.data?.resourceId || resourceId + refTokens.push(`res:${rid}`) + continue + } + throw new ModelParamError("Reference image strings must be https:// URLs, data: URLs, or Dreamina res:/node:/vid: identifiers.") + } + + if (item && typeof item === "object" && typeof item.b64 === "string") { + const buf = Buffer.from(item.b64.replace(/\s+/g, ""), "base64") + if (!buf.length) throw new ModelParamError("Empty b64_json reference image.") + const filePath = path.join(tmpDir, `ref-${i}${item.ext || ".png"}`) + fs.writeFileSync(filePath, buf, { mode: 0o600 }) + const resourceId = randomUUID() + const up = await runCli( + profile, + ["resource", "upload", "--file", filePath, "--type", "image", "--project-id", projectId, "--resource-id", resourceId], + { signal }, + ) + const rid = up.data?.resourceId || resourceId + refTokens.push(`res:${rid}`) + continue + } + + if (item instanceof Blob || (item && typeof item === "object" && typeof item.arrayBuffer === "function")) { + const bytes = Buffer.from(await item.arrayBuffer()) + if (!bytes.length) throw new ModelParamError("Uploaded reference image file is empty.") + const ext = extFromName(item.name) || extFromMime(item.type) + const filePath = path.join(tmpDir, `ref-${i}${ext}`) + fs.writeFileSync(filePath, bytes, { mode: 0o600 }) + const resourceId = randomUUID() + const up = await runCli( + profile, + ["resource", "upload", "--file", filePath, "--type", "image", "--project-id", projectId, "--resource-id", resourceId], + { signal }, + ) + const rid = up.data?.resourceId || resourceId + refTokens.push(`res:${rid}`) + continue + } + + throw new ModelParamError("Unsupported reference image entry type.") + } + return refTokens + } finally { + try { + fs.rmSync(tmpDir, { recursive: true, force: true }) + } catch {} + } +} + +function parseBoolOption(value, fallback) { + if (typeof value === "boolean") return value + if (typeof value === "string") { + const t = value.trim().toLowerCase() + if (t === "true" || t === "1" || t === "yes") return true + if (t === "false" || t === "0" || t === "no") return false + } + return fallback +} + +function parseCeilingValue(raw) { + if (raw === undefined || raw === null || raw === "") return undefined + const n = Number(raw) + if (!Number.isInteger(n) || n < 0) { + throw new ModelParamError(`Invalid credit_ceiling "${raw}". Must be a non-negative integer.`) + } + return n +} + +function resolveCreditPolicy(req, getOption) { + const quoteOnly = Boolean(getOption?.("quote_only") === true) || parseBoolOption(req.quote_only, false) + + const explicitCeiling = parseCeilingValue(req.credit_ceiling) + if (explicitCeiling !== undefined) { + return { quoteOnly, creditCeiling: explicitCeiling } + } + + const guardOpt = getOption?.("credit_guard") + const guardEnabled = guardOpt === undefined ? true : Boolean(guardOpt) + if (!guardEnabled) { + return { quoteOnly, creditCeiling: null } + } + + const envRaw = process.env.DREAMINA_CREDIT_CEILING + const envCeiling = envRaw !== undefined && envRaw !== "" ? parseCeilingValue(envRaw) : undefined + return { + quoteOnly, + creditCeiling: envCeiling ?? DEFAULT_CREDIT_CEILING, + } +} + +function isSucceededState(state) { + return ["succeeded", "success", "completed"].includes(String(state ?? "").toLowerCase()) +} + +function isFailedState(state) { + return ["failed", "error", "cancelled", "canceled", "rejected"].includes(String(state ?? "").toLowerCase()) +} + +function mapOpenAiVideoStatus(state) { + const s = String(state ?? "").toLowerCase() + if (isSucceededState(s)) return "completed" + if (isFailedState(s)) return "failed" + if (s === "running" || s === "processing" || s === "in_progress") return "in_progress" + return "queued" +} + +function estimateVideoDimensions(ratio, resolution) { + const baseHeight = /4k/i.test(resolution) ? 2160 : /1080/i.test(resolution) ? 1080 : /480/i.test(resolution) ? 480 : 720 + const [rw, rh] = String(ratio || "16:9").split(":").map(Number) + if (!rw || !rh) return "1280x720" + if (rw >= rh) { + const h = baseHeight + const w = Math.round((h * rw) / rh) + return `${w}x${h}` + } + const w = baseHeight + const h = Math.round((w * rh) / rw) + return `${w}x${h}` +} + +function findDownloadedFile(tmpDir, dlData) { + const rawPath = dlData?.path + if (typeof rawPath === "string" && rawPath.trim()) { + const resolved = path.isAbsolute(rawPath) ? rawPath : path.resolve(tmpDir, rawPath) + if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) return resolved + if (fs.existsSync(rawPath) && fs.statSync(rawPath).isFile()) return rawPath + } + const entries = fs.readdirSync(tmpDir) + for (const name of entries) { + const full = path.join(tmpDir, name) + if (fs.statSync(full).isFile()) return full + } + throw new CliError("dreamina-canvas resource download produced no readable output file.", { + status: 502, + code: "download_missing_file", + }) +} + +async function quoteNode(profile, projectId, nodeId, { signal, directory }) { + const quoteRes = await runCli( + profile, + ["node", "quote", "--project-id", projectId, "--node-id", nodeId], + { signal }, + ) + const quoteData = quoteRes.data ?? {} + const quoteItems = Array.isArray(quoteData.items) ? quoteData.items : [] + const quoteItem = quoteItems.find((x) => x?.nodeId === nodeId) ?? quoteItems[0] + + if (quoteItem?.error) { + const qErr = quoteItem.error + throw new CliError(qErr.message || "Dreamina node quote failed.", { + status: 400, + code: qErr.validation?.reasonCode || "quote_failed", + }) + } + + const totalMaxCredits = quoteData.totalMaxCredits ?? quoteItem?.maxCredits + if (totalMaxCredits == null || !Number.isFinite(Number(totalMaxCredits))) { + throw new CliError("Dreamina node quote returned no valid totalMaxCredits.", { + status: 502, + code: "invalid_quote", + }) + } + const quotedCredits = Number(totalMaxCredits) + await recordCreditQuote(profile, { quotedCredits, directory }).catch(() => {}) + return quotedCredits +} + +async function quoteConfirmAndRun(profile, projectId, nodeId, policy, { signal, directory }) { + const quotedCredits = await quoteNode(profile, projectId, nodeId, { signal, directory }) + + if (policy.quoteOnly) { + return { + blockedResponse: errorResponse( + 403, + `Dreamina quote_only is enabled: estimated max cost is ${quotedCredits} credits (totalMaxCredits=${quotedCredits}). Generation was not confirmed or run.`, + { + code: "quote_only", + extra: { totalMaxCredits: quotedCredits, projectId, nodeId }, + }, + ), + } + } + + if (policy.creditCeiling !== null && quotedCredits > policy.creditCeiling) { + return { + blockedResponse: errorResponse( + 403, + `Dreamina quoted cost (${quotedCredits} credits) exceeds credit ceiling (${policy.creditCeiling} credits).`, + { + code: "credit_ceiling_exceeded", + extra: { + totalMaxCredits: quotedCredits, + creditCeiling: policy.creditCeiling, + projectId, + nodeId, + }, + }, + ), + } + } + + // Keep creditConfirmationToken strictly in local memory and clear immediately after node run. + let creditToken = null + try { + const confirmRes = await runCli( + profile, + ["node", "confirm", "--project-id", projectId, "--node-id", nodeId, "--credit-ceiling", String(quotedCredits)], + { signal }, + ) + creditToken = confirmRes.data?.creditConfirmationToken + if (!creditToken || typeof creditToken !== "string") { + throw new CliError("Dreamina node confirm did not return a creditConfirmationToken.", { + status: 502, + code: "missing_credit_token", + }) + } + + const submitId = randomUUID() + const runRes = await runCli( + profile, + ["node", "run", "--project-id", projectId, "--node-id", nodeId, "--submit-id", submitId, "--credit-token", creditToken], + { signal }, + ) + const runItems = Array.isArray(runRes.data?.items) ? runRes.data.items : [] + const runItem = runItems.find((x) => x?.nodeId === nodeId) ?? runItems[0] + if (runItem?.state === "rejected" || runItem?.error) { + throw new CliError(runItem?.error?.message || `Dreamina node run rejected (${runItem?.state || "rejected"})`, { + status: runItem?.error?.validation ? 400 : 502, + code: "node_run_rejected", + }) + } + return { + submitId: runItem?.submitId || submitId, + runState: runItem?.state || "accepted", + quotedCredits, + } + } finally { + creditToken = null + } +} + +function buildNodeCreateArgs(kind, projectId, nodeId, params, refTokens = []) { + const args = [ + "node", + "create", + kind, + "--project-id", + projectId, + "--node-id", + nodeId, + "--title", + nodeTitleFromPrompt(params.prompt, kind === "video" ? "Magpie Video" : "Magpie Image"), + "--mode", + params.mode, + "--prompt", + escapePromptBraces(params.prompt), + "--model", + params.model, + "--ratio", + params.ratio, + "--resolution", + params.resolution, + ] + if (kind === "video") { + args.push("--duration", String(params.duration)) + } + args.push("--count", String(params.count)) + for (const ref of refTokens) args.push("--ref", ref) + return args +} + +function saveOutputImage(filePath, nodeId, index, directory) { + const ext = path.extname(filePath) || ".png" + const filename = `${nodeId}-${index + 1}${ext}` + const bases = [] + if (typeof directory === "string" && directory.trim()) { + bases.push(path.join(path.resolve(directory), "dreamina-outputs")) + } + bases.push(path.join(os.tmpdir(), "dreamina-outputs")) + + let lastErr = null + for (const dir of bases) { + try { + fs.mkdirSync(dir, { recursive: true }) + const dest = path.join(dir, filename) + fs.copyFileSync(filePath, dest) + try { + fs.chmodSync(dest, 0o600) + } catch {} + return { url: pathToFileURL(dest).href, path: dest } + } catch (err) { + lastErr = err + } + } + throw lastErr ?? new Error("Unable to persist Dreamina output image.") +} + +function extractSucceededResources(opData) { + const resources = Array.isArray(opData?.resources) ? opData.resources : [] + const topSucceeded = isSucceededState(opData?.state) + return resources.filter((r) => { + if (!r?.resourceId) return false + return r.state ? isSucceededState(r.state) : topSucceeded + }) +} + +async function handleImageRequest(req, { profile, directory, getOption, signal, forceEdit = false }) { + const policy = resolveCreditPolicy(req, getOption) + const refInputs = collectRefInputs(req) + if (forceEdit && refInputs.length === 0) { + throw new ModelParamError("At least one reference image is required for /images/edits.") + } + + const catalog = await fetchLiveModels(profile, { signal }).catch(() => null) + const reqWithMode = forceEdit && !req.mode ? { ...req, mode: "i2i" } : req + const params = resolveImageParams(req.model || "seedream_5.5", reqWithMode, refInputs.length, { catalog }) + + const projectId = await ensureCanvas(profile, { directory, signal }) + const refTokens = await uploadRefItems(profile, projectId, refInputs, { signal }) + + const nodeId = newNodeId() + const createArgs = buildNodeCreateArgs("image", projectId, nodeId, params, refTokens) + + await runCli(profile, createArgs, { signal }) + + const exec = await quoteConfirmAndRun(profile, projectId, nodeId, policy, { signal, directory }) + if (exec.blockedResponse) return exec.blockedResponse + + const waitRes = await runCli( + profile, + ["operation", "wait", exec.submitId, "--project-id", projectId, "--timeout", "10m", "--interval", "3s"], + { signal, timeoutMs: 620_000, check: false }, + ) + + if (waitRes.timedOut || waitRes.exitCode === 20) { + return errorResponse(504, `Dreamina image generation timed out (submitId=${exec.submitId}).`, { code: "timeout" }) + } + if (!waitRes.ok) { + throw new CliError(waitRes.error?.message || "Dreamina image generation failed while waiting.", { + status: waitRes.exitCode === 11 ? 401 : 502, + code: waitRes.error?.code || "operation_wait_failed", + signIn: waitRes.exitCode === 11 ? "expired" : undefined, + }) + } + + const opData = waitRes.data ?? {} + if (isFailedState(opData.state) || opData.submissionError) { + const errMsg = opData.submissionError?.message || `Dreamina image generation failed (state=${opData.state || "failed"}).` + return errorResponse(502, errMsg, { code: "generation_failed" }) + } + + const okResources = extractSucceededResources(opData) + if (!okResources.length) { + return errorResponse(502, `Dreamina image generation produced no succeeded output resources (state=${opData.state || "unknown"}).`, { + code: "empty_output", + }) + } + + let spentCredits = exec.quotedCredits + const inlineCredits = okResources.find((r) => Number.isFinite(Number(r?.creditsAmount)))?.creditsAmount + if (inlineCredits !== undefined && Number(inlineCredits) >= 0) { + spentCredits = Number(inlineCredits) + } else { + try { + const rgRes = await runCli( + profile, + ["resource", "get", okResources[0].resourceId, "--project-id", projectId], + { signal, timeoutMs: 10_000, check: false }, + ) + const rgCredits = rgRes?.ok ? rgRes.data?.creditsAmount : undefined + if (rgCredits !== undefined && Number.isFinite(Number(rgCredits)) && Number(rgCredits) >= 0) { + spentCredits = Number(rgCredits) + } + } catch {} + } + await recordCreditSpend(profile, { credits: spentCredits, directory }).catch(() => {}) + + const wantUrl = String(req.response_format || "").trim().toLowerCase() === "url" + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "dreamina-img-")) + try { + const data = [] + for (let i = 0; i < okResources.length; i++) { + const rid = okResources[i].resourceId + const outDir = path.join(tmpDir, `item-${i}`) + fs.mkdirSync(outDir, { recursive: true }) + const dlRes = await runCli( + profile, + ["resource", "download", rid, "--project-id", projectId, "--output", outDir], + { signal }, + ) + const filePath = findDownloadedFile(outDir, dlRes.data) + if (wantUrl) { + if (typeof dlRes.data?.url === "string" && dlRes.data.url.trim()) { + data.push({ url: dlRes.data.url.trim() }) + } else { + data.push(saveOutputImage(filePath, nodeId, i, directory)) + } + } else { + const b64_json = fs.readFileSync(filePath).toString("base64") + data.push({ b64_json }) + } + } + return jsonResponse(200, { + created: Math.floor(Date.now() / 1000), + data, + }) + } finally { + try { + fs.rmSync(tmpDir, { recursive: true, force: true }) + } catch {} + } +} + +async function handleVideoCreate(req, { profile, directory, getOption, signal }) { + const policy = resolveCreditPolicy(req, getOption) + const refInputs = collectRefInputs(req) + const catalog = await fetchLiveModels(profile, { signal }).catch(() => null) + const params = resolveVideoParams(req.model || "seedance_2.5", req, refInputs.length, { catalog }) + + const projectId = await ensureCanvas(profile, { directory, signal }) + const refTokens = await uploadRefItems(profile, projectId, refInputs, { signal }) + + const nodeId = newNodeId() + const createArgs = buildNodeCreateArgs("video", projectId, nodeId, params, refTokens) + + await runCli(profile, createArgs, { signal }) + + const exec = await quoteConfirmAndRun(profile, projectId, nodeId, policy, { signal, directory }) + if (exec.blockedResponse) return exec.blockedResponse + + const createdAt = Date.now() + const size = estimateVideoDimensions(params.ratio, params.resolution) + const videoTaskId = `video_${exec.submitId.replace(/-/g, "")}` + await saveVideoTask(directory, videoTaskId, { + profile, + projectId, + nodeId, + submitId: exec.submitId, + model: params.model, + duration: params.duration, + createdAt, + size, + }) + await recordCreditSpend(profile, { credits: exec.quotedCredits, directory }).catch(() => {}) + + return jsonResponse(200, { + id: videoTaskId, + object: "video", + created_at: Math.floor(createdAt / 1000), + status: mapOpenAiVideoStatus(exec.runState), + model: params.model, + progress: 0, + seconds: String(params.duration), + size, + }) +} + +async function queryVideoOperation(task, { signal }) { + const statusRes = await runCli( + task.profile, + ["operation", "status", task.submitId, "--project-id", task.projectId], + { signal, check: false }, + ) + if (!statusRes.ok) { + throw new CliError(statusRes.error?.message || "Dreamina operation status query failed.", { + status: statusRes.exitCode === 11 ? 401 : 502, + code: statusRes.error?.code || "operation_status_failed", + signIn: statusRes.exitCode === 11 ? "expired" : undefined, + }) + } + return statusRes.data ?? {} +} + +async function handleVideoStatus(videoId, { directory, signal }) { + const task = getVideoTask(directory, videoId) + if (!task) { + return errorResponse(404, `Video task not found: ${videoId}`, { code: "video_not_found" }) + } + + const opData = await queryVideoOperation(task, { signal }) + let status = mapOpenAiVideoStatus(opData.state) + const okResources = extractSucceededResources(opData) + let errorObj = null + + if (opData.submissionError || isFailedState(opData.state)) { + status = "failed" + errorObj = { + code: opData.submissionError?.reason || "generation_failed", + message: redactSensitiveText(opData.submissionError?.message || `Dreamina video generation failed (state=${opData.state || "failed"})`), + } + } else if (status === "completed" && !okResources.length) { + status = "failed" + errorObj = { + code: "empty_output", + message: "Dreamina video operation completed without a succeeded video resource.", + } + } + + return jsonResponse(200, { + id: task.videoTaskId, + object: "video", + created_at: Math.floor((task.createdAt || Date.now()) / 1000), + status, + model: task.model, + progress: status === "completed" ? 100 : status === "in_progress" ? 50 : 0, + seconds: String(task.duration ?? 5), + ...(task.size ? { size: task.size } : {}), + ...(status === "completed" ? { completed_at: Math.floor(Date.now() / 1000) } : {}), + ...(errorObj ? { error: errorObj } : {}), + }) +} + +async function handleVideoContent(videoId, { directory, signal }) { + const task = getVideoTask(directory, videoId) + if (!task) { + return errorResponse(404, `Video task not found: ${videoId}`, { code: "video_not_found" }) + } + + const opData = await queryVideoOperation(task, { signal }) + const status = mapOpenAiVideoStatus(opData.state) + if (status === "failed" || opData.submissionError) { + return errorResponse(400, opData.submissionError?.message || "Dreamina video generation failed.", { + code: "generation_failed", + }) + } + const okResources = extractSucceededResources(opData) + if (status === "completed" && !okResources.length) { + return errorResponse(502, "Dreamina video operation completed without a succeeded video resource.", { + code: "empty_output", + }) + } + if (status !== "completed") { + return errorResponse(409, `Dreamina video ${task.videoTaskId} is not completed yet (status=${status}).`, { + code: "video_not_ready", + }) + } + + const rid = okResources[0].resourceId + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "dreamina-vid-")) + try { + const dlRes = await runCli( + task.profile, + ["resource", "download", rid, "--project-id", task.projectId, "--output", tmpDir], + { signal }, + ) + const filePath = findDownloadedFile(tmpDir, dlRes.data) + const bytes = fs.readFileSync(filePath) + return new Response(bytes, { + status: 200, + headers: { + "Content-Type": "video/mp4", + "Content-Length": String(bytes.byteLength), + "Content-Disposition": `inline; filename="${task.videoTaskId}.mp4"`, + }, + }) + } finally { + try { + fs.rmSync(tmpDir, { recursive: true, force: true }) + } catch {} + } +} + +function sseResponse(bodyText) { + return new Response(bodyText, { + status: 200, + headers: { + "Content-Type": "text/event-stream; charset=utf-8", + "Cache-Control": "no-cache", + }, + }) +} + +function chatCompletionResponse({ model, text, stream = false }) { + const id = `chatcmpl_${randomUUID().replace(/-/g, "")}` + const created = Math.floor(Date.now() / 1000) + const usage = { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 } + + if (!stream) { + return jsonResponse(200, { + id, + object: "chat.completion", + created, + model, + choices: [ + { + index: 0, + message: { role: "assistant", content: text }, + finish_reason: "stop", + }, + ], + usage, + }) + } + + const chunk = (delta, finish_reason, includeUsage = false) => + `data: ${JSON.stringify({ + id, + object: "chat.completion.chunk", + created, + model, + choices: [{ index: 0, delta, finish_reason }], + ...(includeUsage ? { usage } : {}), + })}\n\n` + + return sseResponse( + chunk({ role: "assistant", content: "" }, null) + + chunk({ content: text }, null) + + chunk({}, "stop", true) + + "data: [DONE]\n\n", + ) +} + +function anthropicMessageResponse({ model, text, stream = false }) { + const id = `msg_${randomUUID().replace(/-/g, "")}` + if (!stream) { + return jsonResponse(200, { + id, + type: "message", + role: "assistant", + model, + content: [{ type: "text", text }], + stop_reason: "end_turn", + stop_sequence: null, + usage: { input_tokens: 1, output_tokens: 1 }, + }) + } + + const evt = (type, payload) => `event: ${type}\ndata: ${JSON.stringify({ type, ...payload })}\n\n` + return sseResponse( + evt("message_start", { + message: { + id, + type: "message", + role: "assistant", + model, + content: [], + stop_reason: null, + stop_sequence: null, + usage: { input_tokens: 1, output_tokens: 0 }, + }, + }) + + evt("content_block_start", { index: 0, content_block: { type: "text", text: "" } }) + + evt("content_block_delta", { index: 0, delta: { type: "text_delta", text } }) + + evt("content_block_stop", { index: 0 }) + + evt("message_delta", { delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 1 } }) + + evt("message_stop", {}), + ) +} + +function sendChatProtocolResponse({ protocol, model, text, stream }) { + return protocol === "anthropic" + ? anthropicMessageResponse({ model, text, stream }) + : chatCompletionResponse({ model, text, stream }) +} + +function extractTextBlocks(content) { + if (typeof content === "string") return content + if (!Array.isArray(content)) return "" + const parts = [] + for (const block of content) { + if (typeof block === "string") { + parts.push(block) + } else if (block && typeof block === "object") { + if (typeof block.text === "string") parts.push(block.text) + else if (block.type === "text" && typeof block.content === "string") parts.push(block.content) + } + } + return parts.join("\n") +} + +function formatQuoteReply({ spec, quotedCredits, nodeId, cleanPrompt, quoteOnly }) { + const quoteNote = quoteOnly ? "(插件当前已开启「仅报价(不扣积分)」,暂不可确认扣费)" : "(仅询价未扣费)" + const lines = [ + `- 模型:${spec.name} (\`${spec.id}\`)`, + `- 报价:预估消耗 **${quotedCredits} 积分**${quoteNote},草稿节点 \`${nodeId}\``, + `- 操作方式:`, + ] + if (!quoteOnly) { + lines.push(` 1. 在当前对话直接确认生成:回复 \`确认生成:${cleanPrompt}\``) + } + lines.push( + ` ${quoteOnly ? "1" : "2"}. 在 Agent 中自动调用:将右下角主对话模型切回常规文本模型(如 Claude / GPT),直接让 Agent 调用 Magpie 的 \`generate_video\` / \`generate_image\` 工具。`, + ) + return lines.join("\n") +} + +function extractChatContext(payload) { + const rawModel = String(payload?.model || "seedream_5.5").replace(/^dreamina\//i, "").trim() + const echoModel = String(payload?.model || rawModel || "seedream_5.5") + const stream = Boolean(payload?.stream) + const maxTokens = Number(payload?.max_tokens ?? payload?.max_completion_tokens ?? 0) + const messages = Array.isArray(payload?.messages) ? payload.messages : [] + + const systemParts = [extractTextBlocks(payload?.system)] + for (const msg of messages) { + if (msg?.role === "system" || msg?.role === "developer") { + systemParts.push(extractTextBlocks(msg.content)) + } + } + const systemText = systemParts.filter(Boolean).join("\n") + + const lastMessage = messages.length > 0 ? messages[messages.length - 1] : null + const hasToolResult = + Array.isArray(lastMessage?.content) && lastMessage.content.some((block) => block?.type === "tool_result") + const rawUserText = lastMessage ? extractTextBlocks(lastMessage.content) : "" + const userText = rawUserText.replace(/[\s\S]*?<\/system-reminder>/gi, "").trim() + + const probePattern = + /isNewTopic|is_new_topic|generate a.*title|write a.*title|concise title|summarize this conversation|extract.*filepath|command prefix|connection_check|warmup|quota/i + const greetingPattern = /^(hi|hello|hey|ping|test|ok|1|1\+1=?|respond with ok|say ok|你好|测试)[.!?\s]*$/i + + const isProbe = + (Number.isFinite(maxTokens) && maxTokens > 0 && maxTokens <= 64) || + probePattern.test(systemText) || + probePattern.test(rawUserText) || + !lastMessage || + lastMessage.role !== "user" || + hasToolResult || + !userText || + greetingPattern.test(userText) + + const isTopicProbe = /isNewTopic|is_new_topic/i.test(systemText) || /isNewTopic|is_new_topic/i.test(rawUserText) + return { rawModel, echoModel, stream, userText, isProbe, isTopicProbe } +} + +async function handleChatRequest(payload, { protocol, profile, directory, getOption, signal }) { + const statusRes = await runCli(profile, ["auth", "status"], { + signal, + timeoutMs: 15_000, + check: false, + }) + if (!statusRes.ok || !statusRes.data?.loggedIn) { + return errorResponse( + 401, + statusRes.error?.message || `Dreamina profile "${profile}" is not signed in. Please sign in again.`, + { + code: "authentication_error", + headers: { "X-Magpie-Sign-In": "expired" }, + topLevelType: protocol === "anthropic" ? "error" : undefined, + }, + ) + } + + const { rawModel, echoModel, stream, userText, isProbe, isTopicProbe } = extractChatContext(payload) + if (isProbe) { + const replyText = isTopicProbe ? JSON.stringify({ isNewTopic: false, title: "即梦生成" }) : "OK" + return sendChatProtocolResponse({ protocol, model: echoModel, text: replyText, stream }) + } + + const hasPrefixConfirm = /^(?:确认生成|confirm\b)\s*[::]?\s*/i.test(userText) + const hasFlagConfirm = /(?:^|\s)--confirm(?=\s|$)/i.test(userText) + const confirmed = hasPrefixConfirm || hasFlagConfirm + let cleanPrompt = userText + if (hasPrefixConfirm) { + cleanPrompt = cleanPrompt.replace(/^(?:确认生成|confirm\b)\s*[::]?\s*/i, "") + } + if (hasFlagConfirm) { + cleanPrompt = cleanPrompt.replace(/(?:^|\s)--confirm(?=\s|$)/gi, " ") + } + cleanPrompt = cleanPrompt.trim() + + if (!cleanPrompt) { + return sendChatProtocolResponse({ + protocol, + model: echoModel, + text: "请提供生成描述词,例如:`确认生成:一只在雨夜霓虹街头行走的橘猫`。", + stream, + }) + } + + const specs = await fetchLiveModels(profile, { signal }).catch(() => null) + const spec = findModelSpec(rawModel, null, specs) + if (!spec) { + throw new ModelParamError(`Unknown or unsupported Dreamina model: ${rawModel || "(empty)"}`) + } + + const policy = resolveCreditPolicy(payload, getOption) + + if (!confirmed || policy.quoteOnly) { + const projectId = await ensureCanvas(profile, { directory, signal }) + const nodeId = newNodeId() + const createArgs = + spec.type === "video" + ? buildNodeCreateArgs("video", projectId, nodeId, resolveVideoParams(spec.id, { prompt: cleanPrompt }, 0, { catalog: specs })) + : buildNodeCreateArgs("image", projectId, nodeId, resolveImageParams(spec.id, { prompt: cleanPrompt }, 0, { catalog: specs })) + + await runCli(profile, createArgs, { signal }) + const quotedCredits = await quoteNode(profile, projectId, nodeId, { signal, directory }) + const replyText = formatQuoteReply({ + spec, + quotedCredits, + nodeId, + cleanPrompt, + quoteOnly: policy.quoteOnly, + }) + return sendChatProtocolResponse({ protocol, model: echoModel, text: replyText, stream }) + } + + if (spec.type === "video") { + const vidRes = await handleVideoCreate({ model: spec.id, prompt: cleanPrompt }, { profile, directory, getOption, signal }) + const data = await vidRes.json().catch(() => ({})) + if (!vidRes.ok) { + return sendChatProtocolResponse({ + protocol, + model: echoModel, + text: `生成已拦截或失败:${data?.error?.message || `HTTP ${vidRes.status}`}`, + stream, + }) + } + const task = getVideoTask(directory, data.id) + const replyText = [ + `已提交即梦视频生成任务:`, + `- 模型:${spec.name} (\`${spec.id}\`)`, + `- 任务 ID:\`${data.id}\``, + `- 状态:${data.status || "queued"}(时长 ${data.seconds ?? spec.defaultDuration} 秒${data.size ? `,规格 ${data.size}` : ""})`, + ...(task?.projectId ? [`- 画布项目:\`${task.projectId}\``] : []), + ].join("\n") + return sendChatProtocolResponse({ protocol, model: echoModel, text: replyText, stream }) + } + + const imgRes = await handleImageRequest( + { model: spec.id, prompt: cleanPrompt, response_format: "url" }, + { profile, directory, getOption, signal, forceEdit: false }, + ) + const data = await imgRes.json().catch(() => ({})) + if (!imgRes.ok) { + return sendChatProtocolResponse({ + protocol, + model: echoModel, + text: `生成已拦截或失败:${data?.error?.message || `HTTP ${imgRes.status}`}`, + stream, + }) + } + const items = Array.isArray(data?.data) ? data.data : [] + const lines = [`已完成即梦图片生成(${spec.name}):`] + for (let i = 0; i < items.length; i++) { + const item = items[i] + if (item?.url) { + lines.push(`- [图片 ${i + 1}](${item.url})${item.path ? `(\`${item.path}\`)` : ""}`) + } else if (item?.b64_json) { + lines.push(`- 图片 ${i + 1}:已生成(base64 数据)`) + } + } + return sendChatProtocolResponse({ protocol, model: echoModel, text: lines.join("\n"), stream }) +} + +export function createMediaFetch({ profile = "default", getProfile, directory, getOption } = {}) { + return async (input, init = {}) => { + let signal + let topLevelType + try { + const rawReq = await readRawRequest(input, init) + signal = rawReq.signal + if (signal?.aborted) { + throw new DOMException("Aborted", "AbortError") + } + + const activeProfile = (typeof getProfile === "function" ? await getProfile() : profile) || "default" + const parsedUrl = new URL(rawReq.url || "/", "http://dreamina.local") + const pathname = parsedUrl.pathname + const isAnthropicChat = rawReq.method === "POST" && /(?:^|\/)messages$/i.test(pathname) + const isOpenAiChat = rawReq.method === "POST" && /(?:^|\/)chat\/completions$/i.test(pathname) + if (isAnthropicChat) topLevelType = "error" + + if (!hasCliRunnerForTests() && !isCliInstalled()) { + return errorResponse(503, "dreamina-canvas CLI not found on this machine. Install it or set DREAMINA_CANVAS_BIN.", { + code: "cli_not_found", + topLevelType, + }) + } + + if (isOpenAiChat || isAnthropicChat) { + const payload = await parseRequestPayload(rawReq) + return await handleChatRequest(payload, { + protocol: isAnthropicChat ? "anthropic" : "openai", + profile: activeProfile, + directory, + getOption, + signal, + }) + } + + if (rawReq.method === "POST" && /(?:^|\/)images\/generations$/i.test(pathname)) { + const payload = await parseRequestPayload(rawReq) + return await handleImageRequest(payload, { profile: activeProfile, directory, getOption, signal, forceEdit: false }) + } + + if (rawReq.method === "POST" && /(?:^|\/)images\/edits$/i.test(pathname)) { + const payload = await parseRequestPayload(rawReq) + return await handleImageRequest(payload, { profile: activeProfile, directory, getOption, signal, forceEdit: true }) + } + + if (rawReq.method === "POST" && /(?:^|\/)videos$/i.test(pathname)) { + const payload = await parseRequestPayload(rawReq) + return await handleVideoCreate(payload, { profile: activeProfile, directory, getOption, signal }) + } + + const contentMatch = rawReq.method === "GET" && /(?:^|\/)videos\/([^/]+)\/content$/i.exec(pathname) + if (contentMatch) { + const videoId = decodeURIComponent(contentMatch[1]) + return await handleVideoContent(videoId, { directory, signal }) + } + + const statusMatch = rawReq.method === "GET" && /(?:^|\/)videos\/([^/]+)$/i.exec(pathname) + if (statusMatch) { + const videoId = decodeURIComponent(statusMatch[1]) + return await handleVideoStatus(videoId, { directory, signal }) + } + + return errorResponse(404, `Unsupported Dreamina route: ${rawReq.method} ${pathname}`, { + code: "unsupported_route", + }) + } catch (err) { + return errorToResponse(err, { topLevelType }) + } + } +} diff --git a/packages/dreamina/models.mjs b/packages/dreamina/models.mjs new file mode 100644 index 000000000..568a66fbc --- /dev/null +++ b/packages/dreamina/models.mjs @@ -0,0 +1,666 @@ +import { getCliRunnerGen, runCli } from "./cli.mjs" + +export const BASE_URL = "dreamina://local" +export const NPM_IMAGE = "@ai-sdk/openai-compatible" +export const NPM_VIDEO = "@ai-sdk/anthropic" +export const LIVE_META = Symbol.for("dreamina.liveMeta") + +export class ModelParamError extends Error { + constructor(message, code = "invalid_request_error") { + super(message) + this.name = "ModelParamError" + this.status = 400 + this.code = code + } +} + +const IMAGE_MODALITIES = { input: ["text", "image"], output: ["image"] } +const VIDEO_MODALITIES = { input: ["text", "image"], output: ["video"] } + +const STD_RATIOS = ["1:1", "3:4", "16:9", "4:3", "9:16", "2:3", "3:2", "21:9"] +const EXT_RATIOS = ["1:1", "9:16", "16:9", "3:4", "4:3", "2:3", "3:2", "9:21", "21:9"] +const FULL_RATIOS = ["1:1", "1:2", "2:1", "9:16", "16:9", "3:4", "4:3", "2:3", "3:2", "4:5", "5:4", "9:21", "21:9", "1:3", "3:1"] +const SEEDANCE_RATIOS = ["21:9", "16:9", "4:3", "1:1", "3:4", "9:16"] +const VIDU_RATIOS = ["16:9", "9:16", "3:4", "4:3", "1:1"] +const HAPPYHORSE_RATIOS = ["16:9", "9:16", "1:1", "4:3", "3:4", "4:5", "5:4", "9:21", "21:9"] + +function makeImageSpec({ id, name, aliases = [], ratios = STD_RATIOS, resolutions, maxI2iRefs = 10, maxT2iRefs = 0, maxBatchCount = 4 }) { + return { + id, + name, + type: "image", + npm: NPM_IMAGE, + aliases, + ratios: [...ratios], + resolutions: [...resolutions], + defaultRatio: ratios.includes("1:1") ? "1:1" : ratios[0], + defaultResolution: resolutions.includes("2K") ? "2K" : resolutions[0], + maxBatchCount, + modes: { + t2i: { minRefs: 0, maxRefs: maxT2iRefs }, + i2i: { minRefs: 1, maxRefs: maxI2iRefs }, + }, + modalities: IMAGE_MODALITIES, + } +} + +function makeVideoSpec({ id, name, aliases = [], ratios = SEEDANCE_RATIOS, resolutions, minDuration, maxDuration, maxFirstLastRefs = 2, maxM2vRefs = 12, maxBatchCount = 4 }) { + const defaultRes = resolutions.find((r) => r.toLowerCase() === "720p") ?? resolutions[0] + return { + id, + name, + type: "video", + npm: NPM_VIDEO, + aliases, + ratios: [...ratios], + resolutions: [...resolutions], + defaultRatio: ratios.includes("16:9") ? "16:9" : ratios[0], + defaultResolution: defaultRes, + minDuration, + maxDuration, + defaultDuration: Math.min(Math.max(5, minDuration), maxDuration), + maxBatchCount, + modes: { + t2v: { minRefs: 0, maxRefs: 0 }, + first_last_frame: { minRefs: 1, maxRefs: maxFirstLastRefs }, + m2v: maxM2vRefs > 0 ? { minRefs: 1, maxRefs: maxM2vRefs } : null, + }, + modalities: VIDEO_MODALITIES, + } +} + +export const STATIC_IMAGE_SPECS = [ + makeImageSpec({ id: "seedream_5.5", name: "Seedream 5.5", aliases: ["5.5"], ratios: STD_RATIOS, resolutions: ["1.5K", "2K"], maxI2iRefs: 10, maxT2iRefs: 14 }), + makeImageSpec({ id: "seedream_5.0_pro", name: "Seedream 5.0 Pro", aliases: ["5.0pro"], ratios: EXT_RATIOS, resolutions: ["1.5K", "2K", "4K"], maxI2iRefs: 10, maxT2iRefs: 14 }), + makeImageSpec({ id: "seedream_5.0_lite", name: "Seedream 5.0 Lite", aliases: ["5.0lite"], ratios: STD_RATIOS, resolutions: ["2K", "4K"], maxI2iRefs: 6, maxT2iRefs: 0 }), + makeImageSpec({ id: "seedream_4.7", name: "Seedream 4.7", aliases: ["4.7"], ratios: STD_RATIOS, resolutions: ["1K", "2K", "4K"], maxI2iRefs: 6, maxT2iRefs: 14 }), + makeImageSpec({ id: "seedream_4.6", name: "Seedream 4.6", aliases: ["4.6"], ratios: STD_RATIOS, resolutions: ["2K", "4K"], maxI2iRefs: 6, maxT2iRefs: 14 }), + makeImageSpec({ id: "high_aes_general_v50_flash", name: "Seedream 5.0 Flash", aliases: ["5.0flash"], ratios: STD_RATIOS, resolutions: ["1.5K", "2K"], maxI2iRefs: 10, maxT2iRefs: 0 }), + makeImageSpec({ id: "jimeng_smart_image_v1", name: "Jimeng Smart Image v1", aliases: [], ratios: ["1:1", "3:4", "9:16", "16:9", "4:3", "2:3", "3:2", "21:9"], resolutions: ["1K", "2K", "4K"], maxI2iRefs: 4, maxT2iRefs: 0 }), + makeImageSpec({ id: "jm_image_model_gg_bp_21", name: "Dreamina N-2.1", aliases: ["N-2.1"], ratios: STD_RATIOS, resolutions: ["1K", "2K", "4K"], maxI2iRefs: 14, maxT2iRefs: 0 }), + makeImageSpec({ id: "jm_image_model_lib_s", name: "Dreamina Image 2.5 Pro", aliases: ["Image2.5Pro"], ratios: FULL_RATIOS, resolutions: ["1K", "2K", "4K"], maxI2iRefs: 16, maxT2iRefs: 0 }), + makeImageSpec({ id: "jm_image_model_lib_f", name: "Dreamina Image 2.5 Flash", aliases: ["Image2.5Flash"], ratios: FULL_RATIOS, resolutions: ["1K", "2K", "4K"], maxI2iRefs: 16, maxT2iRefs: 0 }), +] + +export const STATIC_VIDEO_SPECS = [ + makeVideoSpec({ id: "seedance_2.5", name: "Seedance 2.5", aliases: ["seedance2.5"], ratios: SEEDANCE_RATIOS, resolutions: ["480p", "720p", "1080p", "4k"], minDuration: 4, maxDuration: 30, maxFirstLastRefs: 2, maxM2vRefs: 50 }), + makeVideoSpec({ id: "seedance_2.5_flash", name: "Seedance 2.5 Flash", aliases: ["seedance2.5_flash"], ratios: SEEDANCE_RATIOS, resolutions: ["480p", "720p"], minDuration: 4, maxDuration: 30, maxFirstLastRefs: 2, maxM2vRefs: 20 }), + makeVideoSpec({ id: "seedance_2.0_vip", name: "Seedance 2.0 VIP", aliases: ["seedance2.0_vip"], ratios: SEEDANCE_RATIOS, resolutions: ["720p", "1080p", "4k"], minDuration: 4, maxDuration: 15, maxFirstLastRefs: 2, maxM2vRefs: 12 }), + makeVideoSpec({ id: "seedance_2.0_fast_vip", name: "Seedance 2.0 Fast VIP", aliases: ["seedance2.0fast_vip"], ratios: SEEDANCE_RATIOS, resolutions: ["720p"], minDuration: 4, maxDuration: 15, maxFirstLastRefs: 2, maxM2vRefs: 12 }), + makeVideoSpec({ id: "seedance_2.0_mini", name: "Seedance 2.0 Mini", aliases: ["seedance2.0mini"], ratios: SEEDANCE_RATIOS, resolutions: ["720p"], minDuration: 4, maxDuration: 15, maxFirstLastRefs: 2, maxM2vRefs: 12 }), + makeVideoSpec({ id: "seedance_pro_fast", name: "Seedance 1.0 Pro Fast", aliases: ["seedance_1.0_fast", "seedance1.0fast"], ratios: SEEDANCE_RATIOS, resolutions: ["720p", "1080p"], minDuration: 5, maxDuration: 10, maxFirstLastRefs: 1, maxM2vRefs: 0 }), + makeVideoSpec({ id: "dreamina_vidu_q3", name: "Vidu Q3", aliases: [], ratios: VIDU_RATIOS, resolutions: ["720p", "1080p"], minDuration: 4, maxDuration: 16, maxFirstLastRefs: 2, maxM2vRefs: 7 }), + makeVideoSpec({ id: "dreamina_vidu_q2", name: "Vidu Q2", aliases: [], ratios: VIDU_RATIOS, resolutions: ["720p", "1080p"], minDuration: 4, maxDuration: 10, maxFirstLastRefs: 2, maxM2vRefs: 7 }), + makeVideoSpec({ id: "wan_3.0", name: "Wan 3.0", aliases: ["wan3.0"], ratios: ["16:9", "4:3", "1:1", "3:4", "9:16"], resolutions: ["720P", "1080P"], minDuration: 4, maxDuration: 15, maxFirstLastRefs: 2, maxM2vRefs: 20 }), + makeVideoSpec({ id: "happyhorse_1.1", name: "HappyHorse 1.1", aliases: ["happyhorse1.1"], ratios: HAPPYHORSE_RATIOS, resolutions: ["720P", "1080P"], minDuration: 4, maxDuration: 15, maxFirstLastRefs: 1, maxM2vRefs: 9 }), + makeVideoSpec({ id: "happyhorse_1.0", name: "HappyHorse 1.0", aliases: [], ratios: HAPPYHORSE_RATIOS, resolutions: ["720P", "1080P"], minDuration: 4, maxDuration: 15, maxFirstLastRefs: 1, maxM2vRefs: 9 }), +] + +const STATIC_CATALOG = new Map( + [...STATIC_IMAGE_SPECS, ...STATIC_VIDEO_SPECS].map((spec) => [spec.id, spec]), +) + +export function staticSpecsMap() { + const out = {} + for (const [id, spec] of STATIC_CATALOG) { + out[id] = structuredClone(spec) + } + return out +} + +function formatDisplayName(id) { + return String(id) + .replace(/^jm_image_model_/, "Dreamina ") + .replace(/^dreamina_/, "") + .split("_") + .filter(Boolean) + .map((part) => (part.length <= 3 && /\d/.test(part) ? part.toUpperCase() : part[0].toUpperCase() + part.slice(1))) + .join(" ") +} + +function parseCliModelItem(item, typeOverride) { + const type = typeOverride || item?.type + if (type !== "image" && type !== "video") return null + const id = String(item?.model || "").trim() + if (!id) return null + const known = STATIC_CATALOG.get(id) + const rawModes = Array.isArray(item?.modes) ? item.modes : [] + + const ratiosSet = new Set() + const resSet = new Set() + const modes = {} + let minDuration = Infinity + let maxDuration = -Infinity + let maxBatchCount = 0 + + for (const m of rawModes) { + const modeName = String(m?.name || "").trim() + if (!modeName) continue + const flags = Array.isArray(m?.flags) ? m.flags : [] + for (const fl of flags) { + if (fl?.flag === "--ratio" && Array.isArray(fl.values)) { + for (const v of fl.values) ratiosSet.add(String(v)) + } else if (fl?.flag === "--resolution" && Array.isArray(fl.values)) { + for (const v of fl.values) resSet.add(String(v)) + } else if (fl?.flag === "--duration") { + if (Number.isFinite(fl.min)) minDuration = Math.min(minDuration, fl.min) + if (Number.isFinite(fl.max)) maxDuration = Math.max(maxDuration, fl.max) + } else if (fl?.flag === "--count" && Number.isFinite(fl.max)) { + maxBatchCount = Math.max(maxBatchCount, Math.trunc(fl.max)) + } + } + + const refs = m?.references + const refMin = Number.isFinite(refs?.min) ? refs.min : modeName === "t2i" || modeName === "t2v" ? 0 : 1 + const refMax = Number.isFinite(refs?.max) ? refs.max : 0 + if (modes[modeName]) { + modes[modeName].minRefs = Math.min(modes[modeName].minRefs, refMin) + modes[modeName].maxRefs = Math.max(modes[modeName].maxRefs, refMax) + } else { + modes[modeName] = { minRefs: refMin, maxRefs: refMax } + } + } + + const ratios = ratiosSet.size > 0 ? [...ratiosSet] : known?.ratios ?? ["1:1", "16:9", "9:16"] + const resolutions = resSet.size > 0 ? [...resSet] : known?.resolutions ?? (type === "image" ? ["2K"] : ["720p"]) + const aliases = Array.isArray(item?.aliases) ? item.aliases.map(String) : known?.aliases ?? [] + + if (type === "image") { + return { + id, + name: known?.name ?? formatDisplayName(id), + type: "image", + npm: NPM_IMAGE, + aliases, + ratios, + resolutions, + defaultRatio: ratios.includes("1:1") ? "1:1" : ratios[0], + defaultResolution: resolutions.includes("2K") ? "2K" : resolutions[0], + maxBatchCount: maxBatchCount || known?.maxBatchCount || 4, + modes: { + t2i: modes.t2i ?? known?.modes?.t2i ?? { minRefs: 0, maxRefs: 0 }, + i2i: modes.i2i ?? known?.modes?.i2i ?? { minRefs: 1, maxRefs: 10 }, + }, + modalities: IMAGE_MODALITIES, + } + } + + const finalMinDur = Number.isFinite(minDuration) ? minDuration : known?.minDuration ?? 4 + const finalMaxDur = Number.isFinite(maxDuration) ? maxDuration : known?.maxDuration ?? 15 + const defaultRes = resolutions.find((r) => r.toLowerCase() === "720p") ?? resolutions[0] + return { + id, + name: known?.name ?? formatDisplayName(id), + type: "video", + npm: NPM_VIDEO, + aliases, + ratios, + resolutions, + defaultRatio: ratios.includes("16:9") ? "16:9" : ratios[0], + defaultResolution: defaultRes, + minDuration: finalMinDur, + maxDuration: finalMaxDur, + defaultDuration: Math.min(Math.max(5, finalMinDur), finalMaxDur), + maxBatchCount: maxBatchCount || known?.maxBatchCount || 4, + modes: { + t2v: modes.t2v ?? known?.modes?.t2v ?? { minRefs: 0, maxRefs: 0 }, + first_last_frame: modes.first_last_frame ?? known?.modes?.first_last_frame ?? { minRefs: 1, maxRefs: 2 }, + m2v: Object.prototype.hasOwnProperty.call(modes, "m2v") ? modes.m2v : known?.modes?.m2v ?? null, + }, + modalities: VIDEO_MODALITIES, + } +} + +export function configModels(specsMap = null) { + const source = specsMap ?? staticSpecsMap() + const out = {} + for (const [id, spec] of Object.entries(source)) { + if (!spec || id === "__proto__") continue + out[id] = { + id, + name: spec.name ?? id, + provider: { + npm: spec.npm ?? (spec.type === "video" ? NPM_VIDEO : NPM_IMAGE), + api: BASE_URL, + }, + status: "active", + attachment: true, + reasoning: false, + tool_call: false, + temperature: false, + limit: { + context: 32768, + output: 4096, + }, + cost: { + input: 0, + output: 0, + }, + modalities: spec.modalities ?? (spec.type === "video" ? VIDEO_MODALITIES : IMAGE_MODALITIES), + options: {}, + } + } + return out +} + +export function openCodeModelOf(providerId, spec) { + const isVideo = spec.type === "video" || spec.modalities?.output?.includes("video") + const npm = spec.npm ?? (isVideo ? NPM_VIDEO : NPM_IMAGE) + return { + id: spec.id, + providerID: providerId, + name: spec.name ?? spec.id, + api: { + id: spec.id, + url: BASE_URL, + npm, + }, + status: "active", + limit: { + context: 32768, + output: 4096, + }, + cost: { + input: 0, + output: 0, + cache: { read: 0, write: 0 }, + }, + capabilities: { + temperature: false, + reasoning: false, + attachment: true, + toolcall: false, + input: { text: true, image: true, audio: false, video: false, pdf: false }, + output: isVideo + ? { text: false, image: false, audio: false, video: true, pdf: false } + : { text: false, image: true, audio: false, video: false, pdf: false }, + interleaved: false, + }, + options: {}, + headers: {}, + variants: {}, + release_date: "", + } +} + +const liveCache = new Map() // profile -> { gen, at, specs } +const CACHE_TTL_MS = 5 * 60 * 1000 + +export function clearLiveModelCache() { + liveCache.clear() +} + +export async function fetchLiveModels(profile = "default", opts = {}) { + const safeProfile = String(profile ?? "").trim() || "default" + const gen = getCliRunnerGen() + const now = Date.now() + if (opts.useCache !== false && !opts.fresh) { + const cached = liveCache.get(safeProfile) + if (cached && cached.gen === gen && now - cached.at < (opts.ttlMs ?? CACHE_TTL_MS)) { + return cached.specs + } + } + + const timeoutMs = opts.timeoutMs ?? 15_000 + const [imgRes, vidRes] = await Promise.all([ + runCli(safeProfile, ["model", "list", "--type", "image"], { signal: opts.signal, timeoutMs, check: false }).catch((err) => ({ ok: false, error: { message: err?.message || String(err) } })), + runCli(safeProfile, ["model", "list", "--type", "video"], { signal: opts.signal, timeoutMs, check: false }).catch((err) => ({ ok: false, error: { message: err?.message || String(err) } })), + ]) + + const out = {} + let imgOk = false + let vidOk = false + const errors = [] + + if (imgRes?.ok && Array.isArray(imgRes.data?.items) && imgRes.data.items.length > 0) { + imgOk = true + for (const item of imgRes.data.items) { + const parsed = parseCliModelItem(item, "image") + if (parsed) out[parsed.id] = parsed + } + } else { + if (imgRes?.error?.message) errors.push(imgRes.error.message) + for (const spec of STATIC_IMAGE_SPECS) out[spec.id] = structuredClone(spec) + } + + if (vidRes?.ok && Array.isArray(vidRes.data?.items) && vidRes.data.items.length > 0) { + vidOk = true + for (const item of vidRes.data.items) { + const parsed = parseCliModelItem(item, "video") + if (parsed) out[parsed.id] = parsed + } + } else { + if (vidRes?.error?.message) errors.push(vidRes.error.message) + for (const spec of STATIC_VIDEO_SPECS) out[spec.id] = structuredClone(spec) + } + + const fellBack = !imgOk && !vidOk + Object.defineProperty(out, LIVE_META, { + value: { + live: imgOk || vidOk, + fellBack, + partialFallback: imgOk !== vidOk, + error: errors.join("; "), + }, + enumerable: false, + }) + + if (imgOk || vidOk) { + liveCache.set(safeProfile, { gen, at: now, specs: out }) + } + return out +} + +function stripProviderPrefix(modelId) { + const s = String(modelId ?? "").trim() + if (!s) return "" + return s.includes("/") ? s.split("/").pop() : s +} + +export function findModelSpec(modelId, expectedType, catalog = null) { + const raw = stripProviderPrefix(modelId) + if (!raw) return null + const lower = raw.toLowerCase() + const normalized = lower.replace(/-/g, "_") + const candidates = catalog ? Object.values(catalog) : [...STATIC_IMAGE_SPECS, ...STATIC_VIDEO_SPECS] + + for (const spec of candidates) { + if (expectedType && spec.type !== expectedType) continue + if (spec.id === raw || spec.id.toLowerCase() === lower || spec.id.toLowerCase() === normalized) { + return spec + } + } + for (const spec of candidates) { + if (expectedType && spec.type !== expectedType) continue + for (const alias of spec.aliases ?? []) { + const al = String(alias).toLowerCase() + if (al === lower || al === normalized) return spec + } + } + return null +} + +function matchExactCase(value, allowed) { + const target = String(value ?? "").trim().toLowerCase() + if (!target) return null + for (const item of allowed) { + if (item.toLowerCase() === target) return item + } + return null +} + +function parseWxH(size) { + const m = /^\s*(\d+)\s*[x×*]\s*(\d+)\s*$/i.exec(String(size ?? "")) + if (!m) return null + const width = Number(m[1]) + const height = Number(m[2]) + if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) return null + return { width, height } +} + +function ratioValue(r) { + const [a, b] = String(r).split(":").map(Number) + return a > 0 && b > 0 ? a / b : 1 +} + +function closestRatio(width, height, allowedRatios) { + const target = width / height + let best = allowedRatios[0] + let bestDiff = Infinity + for (const r of allowedRatios) { + const diff = Math.abs(Math.log(target / ratioValue(r))) + if (diff < bestDiff) { + bestDiff = diff + best = r + } + } + return best +} + +function pickNearestImageResolution(maxDim, allowed) { + const order = maxDim >= 3072 ? ["4K", "2K", "1.5K", "1K"] : maxDim >= 1400 ? ["2K", "1.5K", "4K", "1K"] : ["1K", "1.5K", "2K", "4K"] + for (const candidate of order) { + const hit = matchExactCase(candidate, allowed) + if (hit) return hit + } + return allowed[0] +} + +function pickImageResolutionByQuality(quality, allowed) { + const q = String(quality ?? "").trim().toLowerCase() + if (q === "hd" || q === "high") { + for (const c of ["4K", "2K", "1.5K", "1K"]) { + const hit = matchExactCase(c, allowed) + if (hit) return hit + } + } + if (q === "low") { + for (const c of ["1K", "1.5K", "2K", "4K"]) { + const hit = matchExactCase(c, allowed) + if (hit) return hit + } + } + if (q === "medium" || q === "standard") { + for (const c of ["2K", "1.5K", "1K", "4K"]) { + const hit = matchExactCase(c, allowed) + if (hit) return hit + } + } + return null +} + +function pickNearestVideoResolution(width, height, allowed) { + const minDim = Math.min(width, height) + const maxDim = Math.max(width, height) + let order + if (minDim >= 2160 || maxDim >= 3840) order = ["4k", "1080p", "720p", "480p"] + else if (minDim >= 1080 || maxDim >= 1920) order = ["1080p", "720p", "4k", "480p"] + else if (minDim >= 720 || maxDim >= 1280) order = ["720p", "1080p", "480p", "4k"] + else order = ["480p", "720p", "1080p", "4k"] + for (const candidate of order) { + const hit = matchExactCase(candidate, allowed) + if (hit) return hit + } + return allowed[0] +} + +function countReqRefs(req) { + let total = 0 + if (Array.isArray(req?.reference_images)) total += req.reference_images.length + if (Array.isArray(req?.image)) total += req.image.length + else if (req?.image != null) total += 1 + if (Array.isArray(req?.images)) total += req.images.length + if (Array.isArray(req?.input_reference)) total += req.input_reference.length + else if (req?.input_reference != null) total += 1 + if (req?.first_frame != null || req?.first_frame_image != null) total += 1 + if (req?.last_frame != null || req?.last_frame_image != null) total += 1 + return total +} + +export function resolveImageParams(modelId, req = {}, refCountOrOpts = undefined, maybeOpts = {}) { + const refCount = typeof refCountOrOpts === "number" ? refCountOrOpts : refCountOrOpts?.refCount ?? countReqRefs(req) + const opts = typeof refCountOrOpts === "object" && refCountOrOpts !== null ? refCountOrOpts : maybeOpts + const spec = findModelSpec(modelId, "image", opts?.catalog) + if (!spec) { + throw new ModelParamError(`Unknown or unsupported Dreamina image model: ${modelId || "(empty)"}`) + } + + const mode = req.mode ? String(req.mode).trim() : refCount > 0 ? "i2i" : "t2i" + if (mode !== "t2i" && mode !== "i2i") { + throw new ModelParamError(`Invalid image mode "${mode}" for ${spec.id}. Use t2i or i2i.`) + } + const modeSpec = spec.modes[mode] + if (!modeSpec) { + throw new ModelParamError(`Model ${spec.id} does not support mode "${mode}".`) + } + if (refCount > 0 && mode === "i2i" && refCount > modeSpec.maxRefs) { + throw new ModelParamError(`Model ${spec.id} supports at most ${modeSpec.maxRefs} reference images in i2i mode (got ${refCount}).`) + } + if (refCount === 0 && mode === "i2i") { + throw new ModelParamError(`Model ${spec.id} in i2i mode requires at least 1 reference image.`) + } + + const prompt = req.prompt != null ? String(req.prompt) : "" + if (mode === "t2i" && !prompt.trim()) { + throw new ModelParamError(`prompt is required for text-to-image generation on ${spec.id}.`) + } + + let ratio = null + let resolution = null + + const explicitRatio = req.ratio ?? req.aspect_ratio + if (explicitRatio != null && String(explicitRatio).trim() !== "" && String(explicitRatio).trim().toLowerCase() !== "auto") { + ratio = matchExactCase(explicitRatio, spec.ratios) + if (!ratio) { + throw new ModelParamError(`Unsupported ratio "${explicitRatio}" for ${spec.id}. Allowed: ${spec.ratios.join(", ")}`) + } + } + + const explicitRes = req.resolution + if (explicitRes != null && String(explicitRes).trim() !== "" && String(explicitRes).trim().toLowerCase() !== "auto") { + resolution = matchExactCase(explicitRes, spec.resolutions) + if (!resolution) { + throw new ModelParamError(`Unsupported resolution "${explicitRes}" for ${spec.id}. Allowed: ${spec.resolutions.join(", ")}`) + } + } + + const sizeRaw = req.size != null ? String(req.size).trim() : "" + if (sizeRaw && sizeRaw.toLowerCase() !== "auto") { + const asRatio = matchExactCase(sizeRaw, spec.ratios) + const asRes = matchExactCase(sizeRaw, spec.resolutions) + const dims = parseWxH(sizeRaw) + if (asRatio) { + ratio ??= asRatio + } else if (asRes) { + resolution ??= asRes + } else if (dims) { + ratio ??= closestRatio(dims.width, dims.height, spec.ratios) + if (!resolution && !req.quality) { + resolution = pickNearestImageResolution(Math.max(dims.width, dims.height), spec.resolutions) + } + } else { + throw new ModelParamError(`Invalid size "${sizeRaw}" for ${spec.id}. Expected WxH, ratio (${spec.ratios.join(", ")}), or resolution (${spec.resolutions.join(", ")}).`) + } + } + + if (!resolution && req.quality != null && String(req.quality).trim().toLowerCase() !== "auto") { + resolution = pickImageResolutionByQuality(req.quality, spec.resolutions) + } + + ratio ??= spec.defaultRatio + resolution ??= spec.defaultResolution + + const rawCount = req.n ?? req.count ?? 1 + const count = Number(rawCount) + if (!Number.isInteger(count) || count < 1 || count > spec.maxBatchCount) { + throw new ModelParamError(`Invalid count/n "${rawCount}" for ${spec.id}. Must be an integer from 1 to ${spec.maxBatchCount}.`) + } + + return { + model: spec.id, + mode, + prompt, + ratio, + resolution, + count, + } +} + +export function resolveVideoParams(modelId, req = {}, refCount = 0, opts = {}) { + const spec = findModelSpec(modelId, "video", opts?.catalog) + if (!spec) { + throw new ModelParamError(`Unknown or unsupported Dreamina video model: ${modelId || "(empty)"}`) + } + + const effectiveRefs = Number.isInteger(refCount) ? refCount : countReqRefs(req) + let mode = req.mode ? String(req.mode).trim() : null + if (!mode) { + if (effectiveRefs === 0) { + mode = "t2v" + } else if (effectiveRefs === 1) { + mode = "first_last_frame" + } else if (effectiveRefs === 2) { + if ((spec.modes.first_last_frame?.maxRefs ?? 0) >= 2) mode = "first_last_frame" + else if (spec.modes.m2v && effectiveRefs <= spec.modes.m2v.maxRefs) mode = "m2v" + else throw new ModelParamError(`Model ${spec.id} supports at most ${spec.modes.first_last_frame?.maxRefs ?? 1} reference image(s).`) + } else { + if (spec.modes.m2v && effectiveRefs <= spec.modes.m2v.maxRefs) mode = "m2v" + else throw new ModelParamError(`Model ${spec.id} does not support ${effectiveRefs} reference images.`) + } + } + + if (!["t2v", "first_last_frame", "m2v"].includes(mode) || !spec.modes[mode]) { + throw new ModelParamError(`Model ${spec.id} does not support video mode "${mode}".`) + } + const modeSpec = spec.modes[mode] + if (effectiveRefs < modeSpec.minRefs || effectiveRefs > modeSpec.maxRefs) { + throw new ModelParamError(`Model ${spec.id} mode "${mode}" accepts ${modeSpec.minRefs}..${modeSpec.maxRefs} reference images (got ${effectiveRefs}).`) + } + + const prompt = req.prompt != null ? String(req.prompt) : "" + if (mode === "t2v" && !prompt.trim()) { + throw new ModelParamError(`prompt is required for text-to-video generation on ${spec.id}.`) + } + + let ratio = null + let resolution = null + + const explicitRatio = req.ratio ?? req.aspect_ratio + if (explicitRatio != null && String(explicitRatio).trim() !== "" && String(explicitRatio).trim().toLowerCase() !== "auto") { + ratio = matchExactCase(explicitRatio, spec.ratios) + if (!ratio) { + throw new ModelParamError(`Unsupported ratio "${explicitRatio}" for ${spec.id}. Allowed: ${spec.ratios.join(", ")}`) + } + } + + const explicitRes = req.resolution + if (explicitRes != null && String(explicitRes).trim() !== "" && String(explicitRes).trim().toLowerCase() !== "auto") { + resolution = matchExactCase(explicitRes, spec.resolutions) + if (!resolution) { + throw new ModelParamError(`Unsupported resolution "${explicitRes}" for ${spec.id}. Allowed: ${spec.resolutions.join(", ")}`) + } + } + + const sizeRaw = req.size != null ? String(req.size).trim() : "" + if (sizeRaw && sizeRaw.toLowerCase() !== "auto") { + const asRatio = matchExactCase(sizeRaw, spec.ratios) + const asRes = matchExactCase(sizeRaw, spec.resolutions) + const dims = parseWxH(sizeRaw) + if (asRatio) { + ratio ??= asRatio + } else if (asRes) { + resolution ??= asRes + } else if (dims) { + ratio ??= closestRatio(dims.width, dims.height, spec.ratios) + resolution ??= pickNearestVideoResolution(dims.width, dims.height, spec.resolutions) + } else { + throw new ModelParamError(`Invalid video size "${sizeRaw}" for ${spec.id}. Expected WxH, ratio (${spec.ratios.join(", ")}), or resolution (${spec.resolutions.join(", ")}).`) + } + } + + ratio ??= spec.defaultRatio + resolution ??= spec.defaultResolution + + const rawDur = req.duration ?? req.seconds ?? spec.defaultDuration + const duration = Number(rawDur) + if (!Number.isInteger(duration) || duration < spec.minDuration || duration > spec.maxDuration) { + throw new ModelParamError(`Invalid duration/seconds "${rawDur}" for ${spec.id}. Allowed integer range: ${spec.minDuration}..${spec.maxDuration}s.`) + } + + const rawCount = req.n ?? req.count ?? 1 + const count = Number(rawCount) + const maxVideoCount = mode === "m2v" ? 1 : spec.maxBatchCount + if (!Number.isInteger(count) || count < 1 || count > maxVideoCount) { + throw new ModelParamError(`Invalid count "${rawCount}" for ${spec.id}. Must be 1..${maxVideoCount}.`) + } + + return { + model: spec.id, + mode, + prompt, + ratio, + resolution, + duration, + count, + } +} diff --git a/packages/dreamina/opencode.mjs b/packages/dreamina/opencode.mjs new file mode 100644 index 000000000..6b770387f --- /dev/null +++ b/packages/dreamina/opencode.mjs @@ -0,0 +1,2 @@ +// The OpenCode 1.x server plugin entry point. Export only the plugin function. +export { DreaminaAuthPlugin } from "./index.mjs" diff --git a/packages/dreamina/opencode2.mjs b/packages/dreamina/opencode2.mjs new file mode 100644 index 000000000..b9393085e --- /dev/null +++ b/packages/dreamina/opencode2.mjs @@ -0,0 +1,620 @@ +// The OpenCode 2.x face of a package whose plugins are written for +// OpenCode 1.x (and magpie's plugin host). +// +// OpenCode 2.x loads a package's `exports["."]` and wants its default to be +// {id, setup(ctx)}; it has no fallback for a 1.x plugin's named functions +// (plugins#657). This file is that default. It runs each 1.x plugin the +// package's opencode.mjs exports and hands 2.x what the plugin's hooks say: +// +// auth.methods → the integration's sign-in methods +// config() → the provider, its package, base URL and models +// provider.models → the signed-in account's own model list +// auth.loader → each request's base URL, key, headers and fetch +// +// What 2.x has no place for is left out: chat.headers and chat.params +// (2.x's sdk hook doesn't carry the session), magpie's usage and checkin, +// tools and events. +// +// scripts/opencode2.mjs is the one copy that is edited. Every package that +// has it carries the same bytes as packages//opencode2.mjs, and +// scripts/check.mjs fails when a copy differs. + +import { readFileSync } from "node:fs" +import * as v1 from "./opencode.mjs" + +const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")) + +// The key the SDK is made with: the account's own goes on each request in +// its place (see wrapFetch), so a key read when the SDK was made can't go +// out after the account changed. +const PLACEHOLDER = "opencode2-v1-key" + +// The SDK package a 1.x npm name runs as on 2.x. A version keeps 2.x from +// swapping @ai-sdk/* for its native providers, which never call the sdk +// hook below; openai-compatible's is the one 2.x ships, the others are AI +// SDK v6's dist-tag, the line 2.x's bridge runs. +export function packageOf(npm) { + const name = npm || "@ai-sdk/openai-compatible" + if (name === "@ai-sdk/openai-compatible") return "aisdk:@ai-sdk/openai-compatible@2.0.41" + if (/^@ai-sdk\/[^/@]+$/.test(name)) return `aisdk:${name}@ai-v6` + return `aisdk:${name}` +} + +// The plugins a 1.x module exports, as magpie's host and OpenCode 1.x read +// them: a default {server} is the one plugin, else every function export +// and every {server} object. +export function pluginsOf(mod) { + const d = mod?.default + if (d && typeof d === "object" && ("server" in d || "id" in d || "tui" in d)) + return typeof d.server === "function" ? [d.server] : [] + const out = [] + for (const value of Object.values(mod ?? {})) { + const fn = typeof value === "function" ? value : value && typeof value === "object" && typeof value.server === "function" ? value.server : null + if (fn && !out.includes(fn)) out.push(fn) + } + return out +} + +const int = (n, fallback = 0) => { + const v = Math.trunc(Number(n)) + return Number.isFinite(v) && v >= 0 ? v : fallback +} +const money = (n) => (Number.isFinite(Number(n)) && Number(n) >= 0 ? Number(n) : 0) +const record = (v) => (v && typeof v === "object" && !Array.isArray(v) ? v : {}) +const json = (v) => { + try { + return JSON.parse(JSON.stringify(v ?? null)) + } catch { + return null + } +} +const strings = (v) => Object.fromEntries(Object.entries(record(v)).filter(([, s]) => typeof s === "string")) +const nonEmpty = (o) => (Object.keys(o).length ? o : undefined) + +// connKey names a connection the way 2.x does. +const connKey = (c) => (!c ? "" : c.type === "credential" ? `credential:${c.id}` : `env:${c.name}`) + +// ---- sign-in ------------------------------------------------------------ + +// fields is a 1.x method's prompts as a 2.x form. +export function fieldsOf(prompts) { + const out = [] + for (const p of prompts ?? []) { + if (!p?.key) continue + const f = { key: String(p.key), type: "string", title: String(p.message ?? p.key) } + if (p.placeholder) f.placeholder = String(p.placeholder) + if (p.type === "select" && Array.isArray(p.options)) + f.options = p.options.map((o) => ({ value: String(o.value), label: String(o.label ?? o.value), ...(o.hint ? { description: String(o.hint) } : {}) })) + // a prompt shown only when another answer says so can't be required, and + // neither can one whose own validate() accepts an empty answer (the + // browser sign-in's optional API key) + if (!p.condition && p.required !== false) f.required = true + out.push(f) + } + return out.length ? out : undefined +} + +// v1Saved is what a 1.x sign-in's success is saved as, the way 1.x writes +// auth.json: the result without its type and provider. +function v1Saved(r) { + const { type: _t, provider: _p, ...rest } = r + if ("key" in r) return { type: "api", ...rest } + return { type: "oauth", ...rest } +} + +// credentialOf is a 1.x sign-in's success as a 2.x credential: OAuth for +// both kinds, the 1.x auth kept whole in its metadata. No refresh is +// registered, so 2.x hands the credential over as it is and the plugin's +// own loader renews it, as it does on 1.x. +export function credentialOf(methodID, r) { + if (!r || r.type !== "success") throw new Error(r?.error || r?.message || "the sign-in failed") + const saved = json(v1Saved(r)) + if (saved.type === "api") + return { type: "oauth", methodID, access: String(saved.key ?? ""), refresh: "", expires: 0, metadata: { v1: saved } } + return { + type: "oauth", + methodID, + access: String(saved.access ?? ""), + refresh: String(saved.refresh ?? ""), + expires: int(saved.expires), + metadata: { v1: saved }, + } +} + +// authOfValue is the 1.x auth a 2.x credential stands for. +export function authOfValue(value) { + if (!value) return null + if (value.type === "oauth") { + const v1 = record(value.metadata).v1 + if (v1 && typeof v1 === "object") return v1 + return { type: "oauth", access: value.access, refresh: value.refresh, expires: value.expires } + } + if (value.type === "key") { + const meta = value.metadata && Object.keys(value.metadata).length ? { metadata: value.metadata } : {} + return { type: "api", key: value.key, ...meta } + } + return null +} + +const fingerprint = (a) => JSON.stringify([a?.type, a?.access ?? a?.key ?? "", a?.refresh ?? ""]) + +// ---- models ------------------------------------------------------------- + +const MODALITIES = ["text", "audio", "image", "video", "pdf"] + +// internalOf is a config model as 1.x hands it to provider.models: what +// fromModelsDevModel makes of it, with the config's own variants. +export function internalOf(providerID, conf, key, m) { + const mods = record(m.modalities) + const has = (list, k) => Array.isArray(list) && list.includes(k) + const cost = record(m.cost) + return { + ...m, + id: key, + providerID, + name: m.name ?? key, + api: { id: m.id ?? key, url: m.provider?.api ?? conf.api ?? conf.options?.baseURL ?? "", npm: m.provider?.npm ?? conf.npm ?? "@ai-sdk/openai-compatible" }, + status: m.status ?? "active", + headers: m.headers ?? {}, + options: m.options ?? {}, + cost: { input: money(cost.input), output: money(cost.output), cache: { read: money(cost.cache_read), write: money(cost.cache_write) } }, + limit: { context: int(m.limit?.context), input: m.limit?.input, output: int(m.limit?.output) }, + capabilities: { + temperature: m.temperature ?? false, + reasoning: m.reasoning ?? false, + attachment: m.attachment ?? false, + toolcall: m.tool_call ?? true, + input: Object.fromEntries(MODALITIES.map((k) => [k, mods.input ? has(mods.input, k) : k === "text"])), + output: Object.fromEntries(MODALITIES.map((k) => [k, mods.output ? has(mods.output, k) : k === "text"])), + interleaved: m.interleaved ?? false, + }, + release_date: m.release_date ?? "", + variants: record(m.variants), + } +} + +// modelOf is a 1.x model, in config or internal form, as a 2.x Model.Info. +export function modelOf(providerID, provider, key, m) { + const caps = record(m.capabilities) + const listFrom = (obj, list) => { + if (obj && typeof obj === "object") return MODALITIES.filter((k) => obj[k]) + if (Array.isArray(list)) return list.map(String) + return ["text"] + } + const input = listFrom(caps.input, m.modalities?.input) + const output = listFrom(caps.output, m.modalities?.output) + const c = record(m.cost) + const cache = record(c.cache) + const npm = m.api?.npm ?? m.provider?.npm + const url = m.api?.url ?? m.provider?.api + const settings = { ...record(json(m.options)) } + if (url && url !== provider.baseURL) settings.baseURL = url + const variants = Object.entries(record(m.variants)) + .filter(([, v]) => !record(v).disabled) + .map(([id, v]) => { + const { disabled: _d, ...rest } = record(json(v)) + return nonEmpty(rest) ? { id, settings: rest } : { id } + }) + const status = ["alpha", "beta", "deprecated", "active"].includes(m.status) ? m.status : "active" + const out = { + id: key, + modelID: String(m.api?.id ?? m.id ?? key), + providerID, + name: String(m.name ?? key), + capabilities: { + tools: caps.toolcall ?? m.tool_call ?? true, + input: input.length ? input : ["text"], + output: output.length ? output : ["text"], + ...(typeof (caps.temperature ?? m.temperature) === "boolean" ? { temperature: caps.temperature ?? m.temperature } : {}), + }, + variants, + time: { released: Date.parse(m.release_date ?? "") || 0 }, + cost: [{ input: money(c.input), output: money(c.output), cache: { read: money(cache.read ?? c.cache_read), write: money(cache.write ?? c.cache_write) } }], + status, + enabled: true, + limit: { context: int(m.limit?.context, 128000) || 128000, output: int(m.limit?.output, 8192) || 8192 }, + } + if (m.limit?.input) out.limit.input = int(m.limit.input) + if (npm && npm !== provider.npm) out.package = packageOf(npm) + if (nonEmpty(settings)) out.settings = settings + const headers = strings(m.headers) + if (nonEmpty(headers)) out.headers = headers + return out +} + +// ---- requests ----------------------------------------------------------- + +const urlOf = (input) => (typeof input === "string" ? input : input instanceof URL ? input.href : input?.url ?? String(input)) + +// wrapFetch sends a request the SDK made from the provider's settings the +// way 1.x would have sent it with the loader's options: the loader's base +// URL in place of the configured one, the account's key in place of the +// placeholder, the loader's headers added, through the loader's fetch. +export function wrapFetch(next, base, options) { + return async (input, init = {}) => { + const o = await options() + let url = urlOf(input) + const from = String(base ?? "").replace(/\/+$/, "") + const to = o.baseURL ? String(o.baseURL).replace(/\/+$/, "") : "" + if (from && to && from !== to && url.startsWith(from)) url = to + url.slice(from.length) + const req = input instanceof Request ? input : null + const headers = new Headers(init.headers ?? req?.headers ?? undefined) + for (const [k, v] of [...headers.entries()]) { + if (!v.includes(PLACEHOLDER)) continue + if (o.key) headers.set(k, v.replaceAll(PLACEHOLDER, o.key)) + else headers.delete(k) + } + for (const [k, v] of Object.entries(strings(o.headers))) headers.set(k, v) + const send = typeof o.fetch === "function" ? o.fetch : next ?? fetch + if (req && init.body === undefined) { + const body = req.method === "GET" || req.method === "HEAD" ? undefined : await req.text() + return send(url, { method: req.method, headers, body, signal: init.signal ?? req.signal }) + } + return send(url, { ...init, headers }) + } +} + +// ---- one plugin --------------------------------------------------------- + +async function setupPlugin(ctx, fn, live) { + const directory = ctx?.location?.directory ?? process.cwd() + // the active account each provider was last read for, and the renewed + // auth a plugin saved for it (2.x has no way for a plugin to write a + // credential back, so it is kept in the plugin's storage, under the + // credential it renews and the token it was renewed from) + const current = new Map() + const saved = new Map() + const signingIn = new Map() + const storeKey = (id, key) => `v1auth/${id}/${key}` + + const client = { + auth: { + async set(arg) { + const id = arg?.path?.id + const body = json(arg?.body) + const cur = id && current.get(id) + // a sign-in's own saves belong to the credential it is about to + // make, not the one that was active when it started + if (!cur || !body || signingIn.get(id) > 0) return { data: true } + const entry = { fp: cur.fp, auth: body } + saved.set(storeKey(id, cur.key), entry) + await Promise.resolve() + .then(() => ctx.storage?.set?.(storeKey(id, cur.key), entry)) + .catch(() => {}) + return { data: true } + }, + async get() { + return { data: undefined } + }, + }, + app: { log: async () => ({ data: true }) }, + } + + const hooks = (await fn({ client, directory, worktree: directory, project: {}, serverUrl: undefined, $: undefined }, {})) ?? {} + const cfg = { provider: {} } + try { + await hooks.config?.(cfg) + } catch {} + const auth = hooks.auth + const authID = auth?.provider ? String(auth.provider) : undefined + const providers = record(cfg.provider) + + // getAuth reads the active account: the credential 2.x holds, or what the + // plugin last saved renewing it. + const getAuth = async (id) => { + const connection = await ctx.integration.connection.active(id).catch(() => undefined) + if (!connection) { + current.delete(id) + return undefined + } + const value = await ctx.integration.connection.resolve(connection).catch(() => undefined) + const base = authOfValue(value) + if (!base) { + current.delete(id) + return undefined + } + const key = connKey(connection) + const fp = fingerprint(base) + // ours: a credential one of this plugin's own sign-ins made + const ours = value?.type === "oauth" && !!record(value.metadata).v1 + current.set(id, { key, fp, ours }) + const sk = storeKey(id, key) + let entry = saved.get(sk) + if (!entry) { + entry = await Promise.resolve() + .then(() => ctx.storage?.get?.(sk)) + .catch(() => undefined) + if (entry) saved.set(sk, entry) + } + return entry && entry.fp === fp && entry.auth ? entry.auth : base + } + + // the sign-in methods + if (authID && Array.isArray(auth.methods) && auth.methods.length) { + const label = (c) => { + const a = authOfValue(c) + const name = a?.name ?? a?.email ?? a?.metadata?.name ?? a?.metadata?.email ?? a?.account + return typeof name === "string" && name ? name : undefined + } + const begin = () => signingIn.set(authID, (signingIn.get(authID) ?? 0) + 1) + const end = () => signingIn.set(authID, Math.max(0, (signingIn.get(authID) ?? 1) - 1)) + const title = record(providers[authID]).name + // 2.x runs a transform again on every reload, from nothing + await ctx.integration.transform((editor) => { + let keyed = false + // the integration 2.x makes for an id it doesn't know is named by the + // id; the provider's own name reads better in the sign-in list + if (typeof title === "string" && title) + try { + editor.update?.(authID, (i) => { + if (i && (!i.name || i.name === authID)) i.name = title + }) + } catch {} + auth.methods.forEach((m, i) => { + if (!m) return + const methodID = `v1-${i}` + const form = fieldsOf(m.prompts) + if (m.type === "api" && typeof m.authorize !== "function") { + if (keyed) return + keyed = true + editor.method.update({ integrationID: authID, method: { type: "key", label: String(m.label ?? "API key"), ...(form ? { form } : {}) } }) + return + } + if (typeof m.authorize !== "function") return + editor.method.update({ + integrationID: authID, + method: { id: methodID, type: "oauth", label: String(m.label ?? authID), ...(form ? { form } : {}) }, + async authorize(answer) { + begin() + let ended = false + const done = () => { + if (!ended) end() + ended = true + } + try { + const inputs = Object.fromEntries(Object.entries(record(answer)).map(([k, v]) => [k, String(v)])) + const r = await m.authorize(inputs) + if (m.type === "api") { + done() + return { url: "", instructions: "", mode: "auto", callback: Promise.resolve().then(() => credentialOf(methodID, r)) } + } + if (r?.method === "code") + return { + url: String(r.url ?? ""), + instructions: String(r.instructions ?? ""), + mode: "code", + callback: async (code) => { + try { + return credentialOf(methodID, await r.callback(code)) + } finally { + done() + } + }, + } + const callback = Promise.resolve() + .then(() => r.callback()) + .then((res) => credentialOf(methodID, res)) + .finally(done) + callback.catch(() => {}) + return { url: String(r?.url ?? ""), instructions: String(r?.instructions ?? ""), mode: "auto", callback } + } catch (e) { + done() + throw e + } + }, + label, + }) + }) + }) + } + + // the loader's options for the account signed in now, read again when + // the account changes + let loaded = null + const loaderOptions = async (id) => { + if (id !== authID || typeof auth?.loader !== "function") { + const a = authID === id ? await getAuth(id) : undefined + return { key: a?.type === "api" ? a.key : undefined } + } + const a = await getAuth(id) + if (!a) return {} + const fp = current.get(id)?.key + "\0" + fingerprint(a) + if (loaded?.fp !== fp) { + const p = Promise.resolve() + .then(() => auth.loader(() => getAuth(id), providerOf(id))) + .then((o) => record(o)) + loaded = { fp, p } + p.catch(() => { + if (loaded?.fp === fp) loaded = null + }) + } + const o = await loaded.p + const key = typeof o.apiKey === "string" && o.apiKey ? o.apiKey : a.type === "api" ? a.key : undefined + return { baseURL: o.baseURL, key, headers: o.headers, fetch: o.fetch } + } + + // the providers, with the list the account's own read last gave each + const source = new Map() + // providerOf is a provider as 1.x hands it to provider.models and + // auth.loader + const providerOf = (id) => { + const conf = record(providers[id]) + const models = Object.fromEntries(Object.entries(record(conf.models)).map(([k, m]) => [k, internalOf(id, conf, k, record(m))])) + return { id, name: conf.name ?? id, source: "config", env: [], options: record(conf.options), models } + } + const listed = new Set() + for (const [id, conf0] of Object.entries(providers)) { + const conf = record(conf0) + // a provider entry that only names the sign-in (no package, no models) + // is not a provider of models + if (!conf.npm && !Object.keys(record(conf.models)).length) continue + const options = record(conf.options) + const { apiKey: _k, baseURL: _b, fetch: _f, headers: optHeaders, ...rest } = options + // with no base URL the config leaves it to the loader: a name that + // can't resolve stands in for it, and each request swaps in the + // loader's (wrapFetch) + const baseURL = options.baseURL ?? conf.api ?? `https://${id.replace(/[^a-z0-9-]/gi, "-")}.opencode2-v1.invalid` + const npm = conf.npm ?? "@ai-sdk/openai-compatible" + const info = { + id, + ...(id === authID ? { integrationID: id } : {}), + name: String(conf.name ?? id), + activation: "auto", + package: packageOf(npm), + settings: { ...record(json(rest)), ...(baseURL ? { baseURL: String(baseURL) } : {}) }, + } + const headers = { ...strings(conf.headers), ...strings(optHeaders) } + if (nonEmpty(headers)) info.headers = headers + const prov = { baseURL, npm } + const fromConfig = () => + Object.entries(record(conf.models)).map(([key, m]) => modelOf(id, prov, key, internalOf(id, conf, key, record(m)))) + listed.add(id) + await ctx.provider.transform((editor) => { + const s = source.get(id) + editor.add({ info, models: s?.models ?? fromConfig(), sourceConnection: s?.connection }) + }) + source.set(id, { conf, prov, models: null, connection: undefined }) + } + + // each request: the placeholder key, and the fetch that swaps in the + // account's. A provider the package doesn't list (one 2.x has built in + // that the plugin only signs in to) is left to 2.x unless the plugin's + // loader serves it and one of the plugin's own sign-ins is active. + const own = authID && !listed.has(authID) && typeof auth?.loader === "function" ? [authID] : [] + for (const id of new Set([...listed, ...own])) { + const base = source.get(id)?.prov.baseURL + await ctx.aisdk.hook( + "sdk", + async (evt) => { + const opts = evt.options + if (!opts || opts.__opencode2) return + if (!listed.has(id)) { + await getAuth(id) + if (!current.get(id)?.ours) return + } + opts.__opencode2 = true + opts.apiKey = PLACEHOLDER + opts.fetch = wrapFetch(opts.fetch, opts.baseURL ?? base, () => loaderOptions(id)) + }, + { providerID: id }, + ) + } + + // refresh reads the account's own list after a sign-in or a switch + const listID = hooks.provider?.id ? String(hooks.provider.id) : undefined + let refreshing = null + let again = false + // the account the list was last read for: a credential.updated that + // leaves it as it was reads nothing again + let readFor = null + const stateOf = async () => { + const c = await ctx.integration.connection.active(authID).catch(() => undefined) + const a = c ? await getAuth(authID) : undefined + return connKey(c) + "\0" + (a ? fingerprint(a) : "") + } + const refresh = () => { + if (refreshing) { + again = true + return refreshing + } + refreshing = (async () => { + try { + if (!authID || !source.has(authID)) return + const s = source.get(authID) + const connection = await ctx.integration.connection.active(authID).catch(() => undefined) + const a = connection ? await getAuth(authID) : undefined + readFor = connKey(connection) + "\0" + (a ? fingerprint(a) : "") + if (!a || listID !== authID || typeof hooks.provider?.models !== "function") { + if (s.connection || s.models) { + s.connection = undefined + s.models = null + await ctx.provider.reload() + } + return + } + const provider = providerOf(authID) + let got + try { + got = await hooks.provider.models(provider, { auth: a }) + } catch { + got = null + } + if (connKey(await ctx.integration.connection.active(authID).catch(() => undefined)) !== connKey(connection)) { + again = true + return + } + s.connection = connection + s.models = got && typeof got === "object" ? Object.entries(got).map(([k, m]) => modelOf(authID, s.prov, k, record(m))) : null + await ctx.provider.reload() + await ctx.model?.reload?.() + } finally { + refreshing = null + if (again && live.on) { + again = false + void refresh() + } + } + })() + return refreshing + } + + if (authID && ctx.event?.subscribe) + void (async () => { + try { + // a switch names the integration; an update (a sign-in, a removal) + // names nothing, so it reads again only when this account changed + const types = ["credential.switched", "credential.updated"] + for await (const event of ctx.event.subscribe({ types })) { + if (!live.on) return + if (event?.type === "credential.switched" && event?.data?.integrationID === authID) { + loaded = null + await refresh() + } else if (event?.type === "credential.updated" && (await stateOf()) !== readFor) { + await refresh() + } + } + } catch {} + })() + // A plugin still reading its list in the background when the first read + // returned (agy-cli: `agy models` and a session probe take seconds, and + // the first read gets its static list) says so through + // provider.warming(): the list is read once more when that ends, once, + // and no later than a minute on. + const warmed = async () => { + let w + try { + w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null + } catch { + return + } + if (!w || typeof w.then !== "function") return + let timer + await Promise.race([ + Promise.resolve(w).catch(() => {}), + new Promise((r) => { + timer = setTimeout(r, 60_000) + timer.unref?.() + }), + ]) + clearTimeout(timer) + if (live.on) await refresh() + } + void refresh().then(warmed, () => {}) +} + +export async function setup(ctx, mod = v1) { + const live = { on: true } + for (const fn of pluginsOf(mod)) { + try { + await setupPlugin(ctx, fn, live) + } catch (e) { + // one plugin failing to start leaves the package's others working + console.error(`${pkg.name}: ${e?.message ?? e}`) + } + } + return () => { + live.on = false + } +} + +export default { id: pkg.name, setup: (ctx) => setup(ctx) } diff --git a/packages/dreamina/package.json b/packages/dreamina/package.json new file mode 100644 index 000000000..4683bf1dc --- /dev/null +++ b/packages/dreamina/package.json @@ -0,0 +1,51 @@ +{ + "name": "@magpie-community/opencode-dreamina-auth", + "version": "0.1.0", + "description": "Connect local dreamina-canvas CLI to Magpie for Dreamina (Jimeng) Seedream image and Seedance video generation.", + "type": "module", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/magpie-community/plugins.git", + "directory": "packages/dreamina" + }, + "main": "./opencode.mjs", + "exports": { + "./server": "./magpie.mjs", + ".": "./opencode2.mjs" + }, + "files": [ + "index.mjs", + "opencode.mjs", + "magpie.mjs", + "opencode2.mjs", + "cli.mjs", + "models.mjs", + "canvas-state.mjs", + "media.mjs", + "README.md", + "LICENSE" + ], + "scripts": { + "test": "bun test" + }, + "keywords": [ + "magpie", + "magpie-plugin", + "opencode", + "opencode-plugin", + "dreamina", + "jimeng", + "seedream", + "seedance" + ], + "magpie": { + "name": "Dreamina (即梦)", + "icon": "data:image/png;base64,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", + "summary": { + "zh": "通过本机 dreamina-canvas CLI 接入即梦(Dreamina):为 Magpie 提供 Seedream 生图与 Seedance / Vidu / Wan 生视频模型。", + "en": "Connect local dreamina-canvas CLI to Magpie for Dreamina (Jimeng) Seedream image and Seedance video generation." + }, + "maxConcurrency": 4 + } +} diff --git a/packages/dreamina/test/dreamina.test.mjs b/packages/dreamina/test/dreamina.test.mjs new file mode 100644 index 000000000..ce34a1e6d --- /dev/null +++ b/packages/dreamina/test/dreamina.test.mjs @@ -0,0 +1,1238 @@ +import { afterEach, beforeEach, describe, expect, it } from "bun:test" +import fs from "node:fs" +import os from "node:os" +import path from "node:path" + +import * as magpieEntry from "../magpie.mjs" +import * as opencodeEntry from "../opencode.mjs" +import opencode2Default from "../opencode2.mjs" +import { DreaminaAuthPlugin } from "../index.mjs" +import { CliError, isCliInstalled, redactSensitiveText, resolveCliBin, runCli, setCliRunnerForTests } from "../cli.mjs" +import { ensureCanvas, getCreditStats, getVideoTask, readCanvasState, recordCreditSpend, resolveStatePath, saveVideoTask, setCreditBalance } from "../canvas-state.mjs" +import { + LIVE_META, + clearLiveModelCache, + configModels, + fetchLiveModels, + openCodeModelOf, + resolveImageParams, + resolveVideoParams, +} from "../models.mjs" +import { escapePromptBraces } from "../media.mjs" + +const SECRET_TOKEN = "mock-secret-credit-confirmation-token-xyz" +const SECRET_TEMP_URL = "https://cdn.example.invalid/temp-file?Signature=secretSig123&Expires=999999" + +function makeTempDir(prefix = "dreamina-test-") { + return fs.mkdtempSync(path.join(os.tmpdir(), prefix)) +} + +function makeMockCli(handlers = {}) { + const calls = [] + const fn = async (profile, args, opts) => { + calls.push({ profile, args: [...args], opts }) + const key2 = args.slice(0, 2).join(" ") + const key3 = args.slice(0, 3).join(" ") + const handler = handlers[key3] ?? handlers[key2] ?? handlers[args[0]] + if (!handler) { + throw new Error(`Unexpected CLI call in test: ${args.join(" ")}`) + } + return handler(profile, args, opts, calls) + } + return { calls, fn } +} + +describe("Dreamina plugin packaging & entry points", () => { + it("exports only DreaminaAuthPlugin from opencode.mjs and magpie.mjs", () => { + expect(Object.keys(opencodeEntry)).toEqual(["DreaminaAuthPlugin"]) + expect(Object.keys(magpieEntry)).toEqual(["DreaminaAuthPlugin"]) + expect(typeof opencodeEntry.DreaminaAuthPlugin).toBe("function") + expect(typeof magpieEntry.DreaminaAuthPlugin).toBe("function") + }) + + it("matches required package.json metadata with zero runtime dependencies", () => { + const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")) + expect(pkg.name).toBe("@magpie-community/opencode-dreamina-auth") + expect(pkg.version).toBe("0.1.0") + expect(pkg.type).toBe("module") + expect(pkg.license).toBe("MIT") + expect(pkg.main).toBe("./opencode.mjs") + expect(pkg.exports).toEqual({ "./server": "./magpie.mjs", ".": "./opencode2.mjs" }) + expect(pkg.scripts).toEqual({ test: "bun test" }) + expect(pkg.magpie?.name).toBe("Dreamina (即梦)") + expect(pkg.magpie?.icon).toMatch(/^data:image\/png;base64,/) + expect(pkg.magpie?.maxConcurrency).toBe(4) + expect(pkg.dependencies).toBeUndefined() + }) + + it("integrates cleanly with OpenCode 2.x wrapper (opencode2.mjs)", async () => { + const integrations = [] + const providers = [] + const sdkHooks = [] + const ctx = { + location: { directory: os.tmpdir() }, + storage: new Map(), + integration: { + transform: async (fn) => fn({ update: () => {}, method: { update: (m) => integrations.push(m) } }), + connection: { active: async () => undefined, resolve: async () => undefined }, + }, + provider: { + transform: async (fn) => fn({ add: (p) => providers.push(p) }), + reload: async () => {}, + }, + aisdk: { + hook: async (_name, fn, filter) => sdkHooks.push({ fn, filter }), + }, + } + const dispose = await opencode2Default.setup(ctx) + expect(typeof dispose).toBe("function") + expect(integrations.length).toBe(2) + expect(providers.length).toBe(1) + expect(providers[0].info.id).toBe("dreamina") + expect(providers[0].models.length).toBe(21) + expect(sdkHooks[0].filter).toEqual({ providerID: "dreamina" }) + dispose() + }) +}) + +describe("cli.mjs", () => { + afterEach(() => { + setCliRunnerForTests(null) + }) + + it("resolves binary from DREAMINA_CANVAS_BIN, PATH, and fallback dirs", () => { + const dir = makeTempDir("dreamina-bin-") + try { + const binPath = path.join(dir, "dreamina-canvas") + fs.writeFileSync(binPath, "#!/bin/sh\nexit 0\n", { mode: 0o755 }) + + expect(resolveCliBin({ env: { DREAMINA_CANVAS_BIN: binPath }, homedir: dir })).toBe(binPath) + expect(resolveCliBin({ env: { PATH: dir }, homedir: dir })).toBe(binPath) + expect(resolveCliBin({ env: { DREAMINA_CANVAS_BIN: path.join(dir, "missing") }, homedir: dir })).toBeNull() + + const localBinDir = path.join(dir, ".local", "bin") + fs.mkdirSync(localBinDir, { recursive: true }) + const localBin = path.join(localBinDir, "dreamina-canvas") + fs.writeFileSync(localBin, "#!/bin/sh\nexit 0\n", { mode: 0o755 }) + expect(resolveCliBin({ env: { PATH: "" }, homedir: dir })).toBe(localBin) + expect(isCliInstalled({ env: { PATH: "" }, homedir: dir })).toBe(true) + } finally { + fs.rmSync(dir, { recursive: true, force: true }) + } + }) + + it("parses JSON envelopes, supports default profile, AbortSignal, and secret redaction", async () => { + const { calls, fn } = makeMockCli({ + version: () => ({ + exitCode: 0, + stdout: 'warning line before json\n{"schemaVersion":"1","ok":true,"data":{"version":"1.0.2"}}\n', + stderr: "", + }), + auth: () => ({ + exitCode: 11, + stdout: JSON.stringify({ + ok: false, + error: { class: "auth", code: "not_logged_in", message: "Not logged in" }, + }), + stderr: "", + }), + }) + setCliRunnerForTests(fn) + + const ok = await runCli("", ["version"]) + expect(ok.data.version).toBe("1.0.2") + expect(calls[0].profile).toBe("default") + + await expect(runCli("default", ["auth", "account"])).rejects.toThrow(/Not logged in/) + try { + await runCli("default", ["auth", "account"]) + } catch (err) { + expect(err).toBeInstanceOf(CliError) + expect(err.signIn).toBe("expired") + expect(err.status).toBe(401) + } + + const ac = new AbortController() + ac.abort() + await expect(runCli("default", ["version"], { signal: ac.signal })).rejects.toThrow(/Abort/) + + const redacted = redactSensitiveText(`--credit-token ${SECRET_TOKEN} ${SECRET_TEMP_URL}`) + expect(redacted).not.toContain(SECRET_TOKEN) + expect(redacted).not.toContain("secretSig123") + }) +}) + +describe("models.mjs", () => { + afterEach(() => { + setCliRunnerForTests(null) + clearLiveModelCache() + }) + + it("builds static configModels and openCodeModelOf with image and video modalities", () => { + const cfg = configModels() + expect(Object.keys(cfg).length).toBe(21) + expect(cfg["seedream_5.5"].provider.npm).toBe("@ai-sdk/openai-compatible") + expect(cfg["seedream_5.5"].modalities).toEqual({ input: ["text", "image"], output: ["image"] }) + expect(cfg["seedance_2.5"].provider.npm).toBe("@ai-sdk/anthropic") + expect(cfg["seedance_2.5"].modalities).toEqual({ input: ["text", "image"], output: ["video"] }) + + const liveSpecs = { + img: { id: "seedream_5.5", name: "Seedream 5.5", type: "image", npm: "@ai-sdk/openai-compatible" }, + vid: { id: "seedance_2.5", name: "Seedance 2.5", type: "video", npm: "@ai-sdk/anthropic" }, + } + const ocImg = openCodeModelOf("dreamina", liveSpecs.img) + const ocVid = openCodeModelOf("dreamina", liveSpecs.vid) + expect(ocImg.capabilities.output).toEqual({ text: false, image: true, audio: false, video: false, pdf: false }) + expect(ocVid.capabilities.output).toEqual({ text: false, image: false, audio: false, video: true, pdf: false }) + expect(ocImg.api.npm).toBe("@ai-sdk/openai-compatible") + expect(ocVid.api.npm).toBe("@ai-sdk/anthropic") + }) + + it("preserves exact resolution casing and validates image & video parameters", () => { + const img55 = resolveImageParams("seedream_5.5", { prompt: "cat", resolution: "1.5k", size: "1792x1024" }) + expect(img55).toEqual({ + model: "seedream_5.5", + mode: "t2i", + prompt: "cat", + ratio: "16:9", + resolution: "1.5K", + count: 1, + }) + + const img50pro = resolveImageParams("seedream_5.0_pro", { prompt: "tall poster", ratio: "9:21", resolution: "4k" }) + expect(img50pro.ratio).toBe("9:21") + expect(img50pro.resolution).toBe("4K") + expect(() => resolveImageParams("seedream_5.5", { prompt: "x", ratio: "9:21" })).toThrow() + + const wan = resolveVideoParams("wan_3.0", { prompt: "ocean", resolution: "720p", seconds: "8" }, 0) + expect(wan).toEqual({ + model: "wan_3.0", + mode: "t2v", + prompt: "ocean", + ratio: "16:9", + resolution: "720P", + duration: 8, + count: 1, + }) + + const seedance = resolveVideoParams("seedance_2.5", { prompt: "fly", resolution: "4K", duration: 25 }, 3) + expect(seedance.resolution).toBe("4k") + expect(seedance.mode).toBe("m2v") + expect(seedance.duration).toBe(25) + + const hh2 = resolveVideoParams("happyhorse_1.1", { prompt: "run", resolution: "1080p" }, 2) + expect(hh2.resolution).toBe("1080P") + expect(hh2.mode).toBe("m2v") + + expect(() => resolveVideoParams("seedance_pro_fast", { prompt: "fast", duration: 4 }, 0)).toThrow() + expect(() => resolveVideoParams("seedance_pro_fast", { prompt: "fast", duration: 6 }, 2)).toThrow() + expect(() => resolveVideoParams("dreamina_vidu_q2", { prompt: "vidu", duration: 15 }, 0)).toThrow() + }) + + it("discovers live models via dreamina-canvas model list and falls back cleanly on error", async () => { + setCliRunnerForTests(async (_profile, args) => { + if (args.includes("image")) { + return { + ok: true, + data: { + items: [ + { + type: "image", + model: "seedream_5.5", + modes: [ + { + name: "i2i", + flags: [ + { flag: "--ratio", values: ["1:1", "16:9"] }, + { flag: "--resolution", values: ["1.5K", "2K"] }, + { flag: "--count", min: 1, max: 4 }, + ], + references: { min: 1, max: 10 }, + }, + ], + }, + ], + }, + } + } + return { + ok: true, + data: { + items: [ + { + type: "video", + model: "wan_3.0", + modes: [ + { + name: "first_last_frame", + flags: [ + { flag: "--ratio", values: ["16:9", "9:16"] }, + { flag: "--resolution", values: ["720P", "1080P"] }, + { flag: "--duration", min: 4, max: 15 }, + ], + references: { min: 1, max: 1 }, + }, + { + name: "first_last_frame", + flags: [ + { flag: "--ratio", values: ["16:9", "9:16"] }, + { flag: "--resolution", values: ["720P", "1080P"] }, + { flag: "--duration", min: 4, max: 15 }, + ], + references: { min: 2, max: 2 }, + }, + ], + }, + ], + }, + } + }) + + const live = await fetchLiveModels("default", { fresh: true }) + expect(live[LIVE_META].live).toBe(true) + expect(live[LIVE_META].fellBack).toBe(false) + expect(live["wan_3.0"].modes.first_last_frame).toEqual({ minRefs: 1, maxRefs: 2 }) + expect(live["wan_3.0"].maxBatchCount).toBe(4) + + setCliRunnerForTests(async () => ({ exitCode: 1, stdout: "", stderr: "cli error" })) + const fallback = await fetchLiveModels("default", { fresh: true }) + expect(fallback[LIVE_META].fellBack).toBe(true) + expect(Object.keys(fallback).length).toBe(21) + }) +}) + +describe("canvas-state.mjs", () => { + let tmpDir + beforeEach(() => { + tmpDir = makeTempDir("dreamina-state-") + }) + afterEach(() => { + setCliRunnerForTests(null) + fs.rmSync(tmpDir, { recursive: true, force: true }) + }) + + it("creates dedicated per-profile canvas once with 0600 permissions and persists video tasks", async () => { + const { calls, fn } = makeMockCli({ + "canvas create": (_profile, args) => { + const pid = args[args.indexOf("--project-id") + 1] + return { ok: true, data: { project: { projectId: pid, name: "Magpie Gateway" } } } + }, + }) + setCliRunnerForTests(fn) + + const [p1, p2] = await Promise.all([ + ensureCanvas("default", { directory: tmpDir }), + ensureCanvas("default", { directory: tmpDir }), + ]) + expect(p1).toBe(p2) + expect(calls.length).toBe(1) + + const p3 = await ensureCanvas("default", { directory: tmpDir }) + expect(p3).toBe(p1) + expect(calls.length).toBe(1) + + const pOther = await ensureCanvas("studio", { directory: tmpDir }) + expect(pOther).not.toBe(p1) + expect(calls.length).toBe(2) + + await saveVideoTask(tmpDir, "video_test_1", { + profile: "default", + projectId: p1, + nodeId: "node_abc1234567", + submitId: "11111111-1111-4111-8111-111111111111", + model: "seedance_2.5", + duration: 6, + createdAt: 1700000000000, + }) + + const statePath = resolveStatePath(tmpDir) + const st = fs.statSync(statePath) + expect(st.mode & 0o777).toBe(0o600) + const state = readCanvasState(tmpDir) + expect(state.canvases.default.projectId).toBe(p1) + expect(getVideoTask(tmpDir, "video_test_1")?.duration).toBe(6) + }) + + it("tracks configured credit balance, spentSinceBalanceSet, and remainingCredits cleanly", async () => { + const empty = await getCreditStats("default", { directory: tmpDir }) + expect(empty.configuredBalance).toBeNull() + expect(empty.spentSinceBalanceSet).toBe(0) + expect(empty.remainingCredits).toBeNull() + + await setCreditBalance("default", { balance: "", directory: tmpDir }) + await setCreditBalance("default", { balance: null, directory: tmpDir }) + await setCreditBalance("default", { balance: -5, directory: tmpDir }) + expect((await getCreditStats("default", { directory: tmpDir })).configuredBalance).toBeNull() + + await recordCreditSpend("default", { credits: 20, directory: tmpDir }) + await setCreditBalance("default", { balance: 1000, directory: tmpDir }) + await recordCreditSpend("default", { credits: 12, directory: tmpDir }) + + const stats = await getCreditStats("default", { directory: tmpDir }) + expect(stats.todayCredits).toBe(32) + expect(stats.totalCredits).toBe(32) + expect(stats.configuredBalance).toBe(1000) + expect(stats.spentSinceBalanceSet).toBe(12) + expect(stats.remainingCredits).toBe(988) + + await recordCreditSpend("default", { credits: 2000, directory: tmpDir }) + expect((await getCreditStats("default", { directory: tmpDir })).remainingCredits).toBe(0) + }) +}) + +describe("media.mjs routes, quote_only, credit_guard, and video polling", () => { + let tmpDir + let outAssetDir + beforeEach(() => { + tmpDir = makeTempDir("dreamina-media-") + outAssetDir = makeTempDir("dreamina-assets-") + clearLiveModelCache() + }) + afterEach(() => { + setCliRunnerForTests(null) + fs.rmSync(tmpDir, { recursive: true, force: true }) + fs.rmSync(outAssetDir, { recursive: true, force: true }) + }) + + function buildGatewayMock({ quoteCredits = 15, videoState = "succeeded", failWait = false, authExpired = false } = {}) { + const imgBytes = Buffer.from("fake-png-output-bytes") + const vidBytes = Buffer.from("fake-mp4-video-bytes") + const recorded = [] + let confirmed = false + let ran = false + + const fn = async (profile, args) => { + recorded.push({ profile, args: [...args] }) + if (authExpired && args[0] !== "model") { + return { + exitCode: 11, + stdout: JSON.stringify({ ok: false, error: { class: "auth", message: "Please log in again" } }), + stderr: "", + } + } + const cmd = args.slice(0, 2).join(" ") + if (cmd === "auth status") { + return { ok: true, data: { loggedIn: true, expiresAt: "2026-12-31T00:00:00Z", region: "cn", profile } } + } + if (cmd === "model list") { + return { exitCode: 1, stdout: "", stderr: "" } // trigger static fallback + } + if (cmd === "canvas create") { + const pid = args[args.indexOf("--project-id") + 1] + return { ok: true, data: { project: { projectId: pid, name: "Magpie Gateway" } } } + } + if (cmd === "resource upload") { + const rid = args[args.indexOf("--resource-id") + 1] + return { ok: true, data: { resourceId: rid, mediaType: "image" } } + } + if (args[0] === "node" && args[1] === "create") { + const nid = args[args.indexOf("--node-id") + 1] + return { ok: true, data: { node: { nodeId: nid } } } + } + if (cmd === "node quote") { + const nid = args[args.indexOf("--node-id") + 1] + return { + ok: true, + data: { + items: [{ nodeId: nid, maxCredits: quoteCredits }], + totalMaxCredits: quoteCredits, + confirmationRequired: true, + }, + } + } + if (cmd === "node confirm") { + confirmed = true + return { + ok: true, + data: { creditConfirmationToken: SECRET_TOKEN, creditCeiling: quoteCredits }, + } + } + if (cmd === "node run") { + ran = true + const nid = args[args.indexOf("--node-id") + 1] + const sid = args[args.indexOf("--submit-id") + 1] + const token = args[args.indexOf("--credit-token") + 1] + expect(token).toBe(SECRET_TOKEN) + return { ok: true, data: { items: [{ nodeId: nid, submitId: sid, state: "accepted" }] } } + } + if (cmd === "operation wait") { + if (failWait) return { exitCode: 20, stdout: "", stderr: "" } + return { + ok: true, + data: { + state: "succeeded", + resources: [{ resourceId: "res-out-img-1", state: "succeeded" }], + }, + } + } + if (cmd === "operation status") { + return { + ok: true, + data: { + state: videoState, + resources: videoState === "succeeded" ? [{ resourceId: "res-out-vid-1", state: "succeeded" }] : [], + }, + } + } + if (cmd === "resource download") { + const rid = args[2] + const outDir = args[args.indexOf("--output") + 1] + const isVid = rid.includes("vid") + const target = path.join(outDir, isVid ? "output.mp4" : "output.png") + fs.writeFileSync(target, isVid ? vidBytes : imgBytes) + return { + ok: true, + data: { resourceId: rid, path: target, size: (isVid ? vidBytes : imgBytes).length }, + } + } + throw new Error(`Unhandled mock cmd: ${args.join(" ")}`) + } + + return { + recorded, + fn, + get confirmed() { return confirmed }, + get ran() { return ran }, + imgBytes, + vidBytes, + } + } + + it("handles POST /images/generations (t2i and reference_images i2i)", async () => { + const mock = buildGatewayMock({ quoteCredits: 12 }) + setCliRunnerForTests(mock.fn) + + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const loaded = await hooks.auth.loader(async () => ({ type: "oauth", metadata: { profile: "default" } })) + + const res = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedream_5.5", + prompt: "a futuristic {{studio}} with {{res:11111111-1111-4111-8111-111111111111}}", + size: "1792x1024", + }), + }) + expect(res.status).toBe(200) + const body = await res.json() + expect(body.data.length).toBe(1) + expect(body.data[0].b64_json).toBe(mock.imgBytes.toString("base64")) + + // Verify --title is always passed even on non-empty prompts and literal {{ is escaped + const t2iCall = mock.recorded.find((c) => c.args[0] === "node" && c.args[1] === "create") + expect(t2iCall.args).toContain("--title") + const titleVal = t2iCall.args[t2iCall.args.indexOf("--title") + 1] + expect(titleVal.length).toBeGreaterThan(0) + const promptVal = t2iCall.args[t2iCall.args.indexOf("--prompt") + 1] + expect(promptVal).toBe("a futuristic \\{{studio}} with {{res:11111111-1111-4111-8111-111111111111}}") + + expect(escapePromptBraces("already \\{{escaped}} and {{unclosed")).toBe("already \\{{escaped}} and \\{{unclosed") + + // Verify no secret token leaked into state file + const stateText = fs.readFileSync(resolveStatePath(tmpDir), "utf8") + expect(stateText).not.toContain(SECRET_TOKEN) + + // Now test i2i with both data: URL and https:// URL in reference_images + const tinyPng = "data:image/png;base64," + Buffer.from("png-bytes").toString("base64") + const resI2i = await loaded.fetch("dreamina://local/v1/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedream_5.0_pro", + prompt: "merge both references", + ratio: "9:21", + resolution: "4k", + reference_images: [tinyPng, "https://example.com/ref.png"], + }), + }) + expect(resI2i.status).toBe(200) + const uploads = mock.recorded.filter((c) => c.args[0] === "resource" && c.args[1] === "upload") + expect(uploads.length).toBe(2) + expect(uploads[0].args).toContain("--file") + expect(uploads[1].args).toContain("--source-url") + const createCall = mock.recorded.filter((c) => c.args[0] === "node" && c.args[1] === "create").at(-1) + expect(createCall.args).toContain("i2i") + expect(createCall.args).toContain("9:21") + expect(createCall.args).toContain("4K") + }) + + it("handles POST /images/edits multipart/form-data with image and image[]", async () => { + const mock = buildGatewayMock({ quoteCredits: 10 }) + setCliRunnerForTests(mock.fn) + + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const loaded = await hooks.auth.loader(async () => ({ type: "oauth", metadata: { profile: "default" } })) + + const fd = new FormData() + fd.append("model", "seedream_4.7") + fd.append("prompt", "recolor in twilight") + fd.append("image[]", new File([Buffer.from("img-1")], "one.png", { type: "image/png" })) + fd.append("image[]", new File([Buffer.from("img-2")], "two.jpg", { type: "image/jpeg" })) + + const rawReq = new Request("dreamina://local/images/edits", { method: "POST", body: fd }) + const contentType = rawReq.headers.get("content-type") + const rawBuf = Buffer.from(await rawReq.arrayBuffer()) + + const res = await loaded.fetch("dreamina://local/images/edits", { + method: "POST", + headers: { "Content-Type": contentType }, + body: rawBuf, + }) + expect(res.status).toBe(200) + const data = await res.json() + expect(data.data[0].b64_json).toBe(mock.imgBytes.toString("base64")) + + // Also test direct FormData body with single "image" field + const fd2 = new FormData() + fd2.append("model", "seedream_4.7") + fd2.append("prompt", "single image edit") + fd2.append("image", new File([Buffer.from("img-single")], "single.png", { type: "image/png" })) + const res2 = await loaded.fetch("dreamina://local/images/edits", { + method: "POST", + body: fd2, + }) + expect(res2.status).toBe(200) + + const uploads = mock.recorded.filter((c) => c.args[0] === "resource" && c.args[1] === "upload") + expect(uploads.length).toBe(3) + }) + + it("enforces quote_only and credit_ceiling without calling confirm or run", async () => { + const mockQuoteOnly = buildGatewayMock({ quoteCredits: 18 }) + setCliRunnerForTests(mockQuoteOnly.fn) + + let quoteOnlyOpt = true + let creditGuardOpt = true + const hooks = await DreaminaAuthPlugin({ + directory: tmpDir, + magpie: { + option: (_prov, key) => (key === "quote_only" ? quoteOnlyOpt : key === "credit_guard" ? creditGuardOpt : false), + }, + }) + const loaded = await hooks.auth.loader(async () => ({ type: "oauth", metadata: { profile: "default" } })) + + const resQuoteOnly = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "test quote only" }), + }) + expect(resQuoteOnly.status).toBe(403) + const qBody = await resQuoteOnly.json() + expect(qBody.error.totalMaxCredits).toBe(18) + expect(mockQuoteOnly.confirmed).toBe(false) + expect(mockQuoteOnly.ran).toBe(false) + + // Request quote_only: false must NOT bypass UI quote_only: true safety lock + const resBypass = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "try bypass", quote_only: false }), + }) + expect(resBypass.status).toBe(403) + expect(mockQuoteOnly.confirmed).toBe(false) + expect(mockQuoteOnly.ran).toBe(false) + + // Test credit_ceiling (default 200) exceeded + quoteOnlyOpt = false + const mockExpensive = buildGatewayMock({ quoteCredits: 250 }) + setCliRunnerForTests(mockExpensive.fn) + const resCeil = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "expensive" }), + }) + expect(resCeil.status).toBe(403) + const cBody = await resCeil.json() + expect(cBody.error.totalMaxCredits).toBe(250) + expect(cBody.error.creditCeiling).toBe(200) + expect(mockExpensive.confirmed).toBe(false) + expect(mockExpensive.ran).toBe(false) + + // Test request credit_ceiling override allows 250 + const resAllowed = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "allowed", credit_ceiling: 300 }), + }) + expect(resAllowed.status).toBe(200) + expect(mockExpensive.confirmed).toBe(true) + expect(mockExpensive.ran).toBe(true) + + // Test credit_guard = false allows 250 without explicit ceiling + creditGuardOpt = false + const mockGuardOff = buildGatewayMock({ quoteCredits: 250 }) + setCliRunnerForTests(mockGuardOff.fn) + const resGuardOff = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "guard off" }), + }) + expect(resGuardOff.status).toBe(200) + expect(mockGuardOff.ran).toBe(true) + }) + + it("handles POST /videos, GET /videos/{id}, and GET /videos/{id}/content", async () => { + let opState = "running" + const mock = buildGatewayMock({ quoteCredits: 50 }) + setCliRunnerForTests(async (profile, args, opts, calls) => { + if (args[0] === "operation" && args[1] === "status") { + return { + ok: true, + data: { + state: opState, + resources: opState === "succeeded" ? [{ resourceId: "res-out-vid-1", state: "succeeded" }] : [], + }, + } + } + return mock.fn(profile, args, opts, calls) + }) + + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const loaded = await hooks.auth.loader(async () => ({ type: "oauth", metadata: { profile: "default" } })) + + const createRes = await loaded.fetch("dreamina://local/videos", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "wan_3.0", + prompt: "drone flight over alpine peaks", + resolution: "720p", + seconds: "8", + first_frame: "data:image/png;base64," + Buffer.from("first").toString("base64"), + }), + }) + expect(createRes.status).toBe(200) + const videoObj = await createRes.json() + expect(videoObj.object).toBe("video") + expect(videoObj.status).toBe("queued") + expect(videoObj.seconds).toBe("8") + + // Ensure uppercase 720P and --title were passed to wan_3.0 + const createCall = mock.recorded.find((c) => c.args[0] === "node" && c.args[1] === "create") + expect(createCall.args).toContain("720P") + expect(createCall.args).toContain("first_last_frame") + expect(createCall.args).toContain("--title") + + // Empty string first_frame/last_frame must be ignored cleanly (falls back to t2v) + const t2vRes = await loaded.fetch("dreamina://local/videos", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedance_2.5", + prompt: "pure {{text}} video", + first_frame: "", + last_frame: "", + }), + }) + expect(t2vRes.status).toBe(200) + const t2vCall = mock.recorded.filter((c) => c.args[0] === "node" && c.args[1] === "create").at(-1) + expect(t2vCall.args).toContain("t2v") + expect(t2vCall.args[t2vCall.args.indexOf("--prompt") + 1]).toBe("pure \\{{text}} video") + + // While running -> status in_progress, /content returns 409 + const poll1 = await loaded.fetch(`dreamina://local/videos/${videoObj.id}`, { method: "GET" }) + expect(poll1.status).toBe(200) + expect((await poll1.json()).status).toBe("in_progress") + + const notReady = await loaded.fetch(`dreamina://local/videos/${videoObj.id}/content`, { method: "GET" }) + expect(notReady.status).toBe(409) + + // When succeeded -> status completed, /content returns video/mp4 bytes + opState = "succeeded" + const poll2 = await loaded.fetch(`dreamina://local/videos/${videoObj.id}`, { method: "GET" }) + expect((await poll2.json()).status).toBe("completed") + + const contentRes = await loaded.fetch(`dreamina://local/videos/${videoObj.id}/content`, { method: "GET" }) + expect(contentRes.status).toBe(200) + expect(contentRes.headers.get("content-type")).toBe("video/mp4") + const buf = Buffer.from(await contentRes.arrayBuffer()) + expect(buf.equals(mock.vidBytes)).toBe(true) + + // Missing video ID -> 404 + const missing = await loaded.fetch("dreamina://local/videos/video_does_not_exist", { method: "GET" }) + expect(missing.status).toBe(404) + + // Completed operation with empty resources -> 502 empty_output + setCliRunnerForTests(async () => ({ ok: true, data: { state: "succeeded", resources: [] } })) + const emptyContent = await loaded.fetch(`dreamina://local/videos/${videoObj.id}/content`, { method: "GET" }) + expect(emptyContent.status).toBe(502) + expect((await emptyContent.json()).error.code).toBe("empty_output") + }) + + it("propagates 401 X-Magpie-Sign-In: expired on auth errors", async () => { + const mock = buildGatewayMock({ authExpired: true }) + setCliRunnerForTests(mock.fn) + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const loaded = await hooks.auth.loader(async () => ({ type: "oauth", metadata: { profile: "default" } })) + + const res = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "test auth" }), + }) + expect(res.status).toBe(401) + expect(res.headers.get("x-magpie-sign-in")).toBe("expired") + }) + + it("handles POST /chat/completions and POST /messages probes, quote-first chat prompts, and confirmed runs", async () => { + const mock = buildGatewayMock({ quoteCredits: 18 }) + setCliRunnerForTests(mock.fn) + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const loaded = await hooks.auth.loader(async () => ({ type: "oauth", metadata: { profile: "default" } })) + + // 1) OpenAI non-streaming probe -> 200 JSON, 0 node create calls + const openAiProbe = await loaded.fetch("dreamina://local/chat/completions", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "high_aes_general_v50_flash", + max_tokens: 16, + messages: [{ role: "user", content: "hi" }], + }), + }) + expect(openAiProbe.status).toBe(200) + const openAiJson = await openAiProbe.json() + expect(openAiJson.object).toBe("chat.completion") + expect(openAiJson.choices[0].message.content).toBe("OK") + + // 2) OpenAI streaming probe + const openAiStream = await loaded.fetch("dreamina://local/v1/chat/completions", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedream_5.5", + stream: true, + max_tokens: 16, + messages: [{ role: "user", content: "ping" }], + }), + }) + expect(openAiStream.status).toBe(200) + expect(openAiStream.headers.get("content-type")).toContain("text/event-stream") + const openAiSse = await openAiStream.text() + expect(openAiSse).toContain('"content":"OK"') + expect(openAiSse).toContain("data: [DONE]") + + // 3) Anthropic non-streaming + streaming probes + isNewTopic probe + const anthProbe = await loaded.fetch("dreamina://local/v1/messages", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedance_2.0_mini", + max_tokens: 32, + messages: [{ role: "user", content: "hi" }], + }), + }) + expect(anthProbe.status).toBe(200) + const anthJson = await anthProbe.json() + expect(anthJson.type).toBe("message") + expect(anthJson.content[0].text).toBe("OK") + + const anthTopic = await loaded.fetch("dreamina://local/messages", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedance_2.0_mini", + stream: true, + max_tokens: 128, + system: "Analyze if this message asks isNewTopic and extract title", + messages: [{ role: "user", content: "随机生成一段视频" }], + }), + }) + expect(anthTopic.status).toBe(200) + const anthTopicSse = await anthTopic.text() + expect(anthTopicSse).toContain("event: message_start") + expect(anthTopicSse).toContain("isNewTopic") + expect(anthTopicSse).toContain("event: message_stop") + + // Zero node create / confirm calls for all probes above + expect(mock.recorded.some((c) => c.args[0] === "node")).toBe(false) + + // 4) Real video prompt without confirm -> quote only, no node confirm/run + const quoteTurn = await loaded.fetch("dreamina://local/v1/messages", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "dreamina/seedance_2.0_mini", + max_tokens: 4096, + messages: [{ role: "user", content: "ignored\n随机生成一段视频" }], + }), + }) + expect(quoteTurn.status).toBe(200) + const quoteTurnBody = await quoteTurn.json() + expect(quoteTurnBody.content[0].text).toContain("18 积分") + expect(quoteTurnBody.content[0].text).toContain("确认生成:随机生成一段视频") + expect(mock.recorded.some((c) => c.args[0] === "node" && c.args[1] === "create")).toBe(true) + expect(mock.recorded.some((c) => c.args[0] === "node" && c.args[1] === "quote")).toBe(true) + expect(mock.confirmed).toBe(false) + expect(mock.ran).toBe(false) + + // 5) Confirmed video prompt -> runs confirm + run and returns video task ID + const confirmVideo = await loaded.fetch("dreamina://local/v1/messages", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedance_2.0_mini", + max_tokens: 4096, + stream: true, + messages: [{ role: "user", content: "确认生成:随机生成一段视频" }], + }), + }) + expect(confirmVideo.status).toBe(200) + const confirmVideoSse = await confirmVideo.text() + expect(confirmVideoSse).toContain("video_") + expect(confirmVideoSse).toContain("已提交即梦视频生成任务") + expect(mock.confirmed).toBe(true) + expect(mock.ran).toBe(true) + + // 6) Confirmed image prompt -> persists file and returns link + const confirmImg = await loaded.fetch("dreamina://local/v1/chat/completions", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + model: "seedream_5.5", + max_tokens: 4096, + messages: [{ role: "user", content: "确认生成:雪山日出" }], + }), + }) + expect(confirmImg.status).toBe(200) + const confirmImgJson = await confirmImg.json() + expect(confirmImgJson.choices[0].message.content).toContain("file://") + }) +}) + +describe("index.mjs auth methods, refresh, usage, and provider.models", () => { + let tmpDir + beforeEach(() => { + tmpDir = makeTempDir("dreamina-auth-") + clearLiveModelCache() + }) + afterEach(() => { + setCliRunnerForTests(null) + fs.rmSync(tmpDir, { recursive: true, force: true }) + }) + + it("supports config hook, options, existing-profile login, browser device login, refresh, usage, and provider.models", async () => { + setCliRunnerForTests(async (profile, args) => { + const cmd = args.slice(0, 2).join(" ") + if (cmd === "auth status") { + return { + ok: true, + data: { + loggedIn: true, + expiresAt: "2026-12-31T00:00:00Z", + region: "cn", + profile, + }, + } + } + if (cmd === "auth account") { + return { + ok: true, + data: { userId: 123456, isVip: true, vipLevel: "pro" }, + } + } + if (cmd === "auth login") { + return { + ok: true, + data: { + status: "pending", + challenge: { + deviceCode: "dev-code-123", + userCode: "ABCD-1234", + verificationUri: "https://example.com/verify", + verificationUriComplete: "https://example.com/verify?code=ABCD-1234", + pollSeconds: 5, + }, + }, + } + } + if (cmd === "auth wait") { + expect(args).toContain("--device-code") + expect(args).toContain("dev-code-123") + return { + ok: true, + data: { + status: "logged_in", + region: "cn", + state: { loggedIn: true, expiresAt: "2026-12-31T00:00:00Z" }, + }, + } + } + if (cmd === "auth refresh") { + if (profile === "expired_prof") { + return { + exitCode: 11, + stdout: JSON.stringify({ ok: false, error: { class: "auth", message: "Refresh expired" } }), + stderr: "", + } + } + return { ok: true, data: { loggedIn: true, expiresAt: "2027-01-01T00:00:00Z", region: "cn" } } + } + if (cmd === "model list") { + return { exitCode: 1, stdout: "", stderr: "" } + } + throw new Error(`Unexpected auth test CLI call: ${args.join(" ")}`) + }) + + const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")) + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + expect(hooks.auth.provider).toBe("dreamina") + expect(hooks.auth.icon).toMatch(/^data:image\/png;base64,/) + expect(hooks.auth.icon).toBe(pkg.magpie.icon) + expect(hooks.auth.options).toEqual([ + { + key: "quote_only", + label: "Quote only", + labels: { zh: "仅报价(不扣积分)", "zh-TW": "僅報價(不扣積分)" }, + description: "Quote credits only without running generation.", + descriptions: { + zh: "只询价并返回预估点数,不执行生成", + "zh-TW": "只詢價並回傳預估點數,不執行生成", + }, + default: false, + }, + { + key: "credit_guard", + label: "200-credit ceiling", + labels: { zh: "单次 200 点上限保护", "zh-TW": "單次 200 點上限保護" }, + description: "Block runs quoted above 200 credits.", + descriptions: { + zh: "单次报价超过 200 点时自动拦截", + "zh-TW": "單次報價超過 200 點時自動攔截", + }, + default: true, + }, + ]) + expect(hooks.auth.methods[0].prompts.map((p) => p.key)).toEqual(["profile", "credits"]) + expect(hooks.auth.methods[0].prompts[1].validate("")).toBeNull() + expect(hooks.auth.methods[0].prompts[1].validate("1000")).toBeNull() + expect(hooks.auth.methods[0].prompts[1].validate("-1")).toBeTruthy() + + const cfg = { provider: {} } + await hooks.config(cfg) + expect(cfg.provider.dreamina.name).toBe("Dreamina (即梦)") + expect(Object.keys(cfg.provider.dreamina.models).length).toBe(21) + + // Method 1: existing profile (blank -> "default") + const m1 = await hooks.auth.methods[0].authorize({ profile: "" }) + expect(m1.method).toBe("auto") + const r1 = await m1.callback() + expect(r1.type).toBe("success") + expect(r1.metadata.profile).toBe("default") + expect(r1.metadata.isVip).toBe(true) + expect(r1.accountId).toBe("default (123456)") + + // Method 2: browser sign-in + const m2 = await hooks.auth.methods[1].authorize({ profile: "studio" }) + expect(m2.url).toBe("https://example.com/verify?code=ABCD-1234") + expect(m2.instructions).toContain("ABCD-1234") + const r2 = await m2.callback() + expect(r2.type).toBe("success") + expect(r2.metadata.profile).toBe("studio") + + // Refresh + const refOk = await hooks.auth.refresh(r1) + expect(refOk.metadata.profile).toBe("default") + expect(refOk.expires).toBe(Date.parse("2027-01-01T00:00:00Z")) + + try { + await hooks.auth.refresh({ metadata: { profile: "expired_prof" } }) + throw new Error("Expected refresh to throw") + } catch (err) { + expect(err.signIn).toBe("expired") + } + + // Usage (no balance set -> concise plan, no OAuth until, single 今日点数 window) + const u = await hooks.auth.usage(async () => r1) + expect(u.plan).toBe("VIP · 专业会员") + expect(u.signIn).toBe("kept") + expect(u.until).toBeUndefined() + expect(u.windows).toEqual([ + { name: "今日点数", display: "已用 0 点 · 单次上限 200 点", aside: true }, + ]) + + // Provider models + const models = await hooks.provider.models({}, { auth: r1 }) + expect(models["seedream_5.5"].capabilities.output.image).toBe(true) + expect(models["seedance_2.5"].capabilities.output.video).toBe(true) + }) + + it("displays maestro VIP status and updated credit quotes/spends in auth.usage after generation", async () => { + const imgBytes = Buffer.from("fake-png-bytes") + setCliRunnerForTests(async (profile, args) => { + const cmd = args.slice(0, 2).join(" ") + if (cmd === "auth status") { + return { ok: true, data: { loggedIn: true, expiresAt: "2026-12-31T00:00:00Z", region: "cn", profile } } + } + if (cmd === "auth account") { + return { ok: true, data: { userId: 987654, isVip: true, vipLevel: "maestro" } } + } + if (cmd === "model list") return { exitCode: 1, stdout: "", stderr: "" } + if (cmd === "canvas create") { + const pid = args[args.indexOf("--project-id") + 1] + return { ok: true, data: { project: { projectId: pid, name: "Magpie Gateway" } } } + } + if (cmd === "node create") { + return { ok: true, data: { node: { nodeId: args[args.indexOf("--node-id") + 1] } } } + } + if (cmd === "node quote") { + const nid = args[args.indexOf("--node-id") + 1] + return { ok: true, data: { items: [{ nodeId: nid, maxCredits: 15 }], totalMaxCredits: 15 } } + } + if (cmd === "node confirm") { + return { ok: true, data: { creditConfirmationToken: SECRET_TOKEN, creditCeiling: 15 } } + } + if (cmd === "node run") { + return { + ok: true, + data: { items: [{ nodeId: args[args.indexOf("--node-id") + 1], submitId: args[args.indexOf("--submit-id") + 1], state: "accepted" }] }, + } + } + if (cmd === "operation wait") { + return { ok: true, data: { state: "succeeded", resources: [{ resourceId: "res-img-1", state: "succeeded" }] } } + } + if (cmd === "resource get") { + return { ok: true, data: { resourceId: "res-img-1", type: "image", status: "succeeded", creditsAmount: 12 } } + } + if (cmd === "resource download") { + const target = path.join(args[args.indexOf("--output") + 1], "out.png") + fs.writeFileSync(target, imgBytes) + return { ok: true, data: { resourceId: "res-img-1", path: target, size: imgBytes.length } } + } + throw new Error(`Unexpected CLI call: ${args.join(" ")}`) + }) + + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const authObj = { type: "oauth", metadata: { profile: "default" } } + const loaded = await hooks.auth.loader(async () => authObj) + + const genRes = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "sunset mountain" }), + }) + expect(genRes.status).toBe(200) + + const u = await hooks.auth.usage(async () => authObj) + expect(u.plan).toBe("VIP · 大师会员") + expect(u.until).toBeUndefined() + expect(u.windows).toEqual([ + { name: "今日点数", display: "已用 12 点 · 累计 12 点(单次上限 200 点)", aside: true }, + ]) + }) + + it("tracks sign-in initial credits, generation deduction, DREAMINA_CREDITS_BALANCE, and liveBalance auto-detection", async () => { + const imgBytes = Buffer.from("fake-png-bytes") + let accountPayload = { userId: 987654, isVip: true, vipLevel: "maestro" } + setCliRunnerForTests(async (profile, args) => { + const cmd = args.slice(0, 2).join(" ") + if (cmd === "auth status") { + return { ok: true, data: { loggedIn: true, expiresAt: "2026-12-31T00:00:00Z", region: "cn", profile } } + } + if (cmd === "auth account") { + return { ok: true, data: accountPayload } + } + if (cmd === "model list") return { exitCode: 1, stdout: "", stderr: "" } + if (cmd === "canvas create") { + const pid = args[args.indexOf("--project-id") + 1] + return { ok: true, data: { project: { projectId: pid, name: "Magpie Gateway" } } } + } + if (cmd === "node create") { + return { ok: true, data: { node: { nodeId: args[args.indexOf("--node-id") + 1] } } } + } + if (cmd === "node quote") { + const nid = args[args.indexOf("--node-id") + 1] + return { ok: true, data: { items: [{ nodeId: nid, maxCredits: 15 }], totalMaxCredits: 15 } } + } + if (cmd === "node confirm") { + return { ok: true, data: { creditConfirmationToken: SECRET_TOKEN, creditCeiling: 15 } } + } + if (cmd === "node run") { + return { + ok: true, + data: { items: [{ nodeId: args[args.indexOf("--node-id") + 1], submitId: args[args.indexOf("--submit-id") + 1], state: "accepted" }] }, + } + } + if (cmd === "operation wait") { + return { ok: true, data: { state: "succeeded", resources: [{ resourceId: "res-img-1", state: "succeeded" }] } } + } + if (cmd === "resource get") { + return { ok: true, data: { resourceId: "res-img-1", type: "image", status: "succeeded", creditsAmount: 12 } } + } + if (cmd === "resource download") { + const target = path.join(args[args.indexOf("--output") + 1], "out.png") + fs.writeFileSync(target, imgBytes) + return { ok: true, data: { resourceId: "res-img-1", path: target, size: imgBytes.length } } + } + throw new Error(`Unexpected CLI call: ${args.join(" ")}`) + }) + + const hooks = await DreaminaAuthPlugin({ directory: tmpDir }) + const m = await hooks.auth.methods[0].authorize({ profile: "default", credits: "1000" }) + const signedIn = await m.callback() + expect(signedIn.type).toBe("success") + + const loaded = await hooks.auth.loader(async () => signedIn) + const genRes = await loaded.fetch("dreamina://local/images/generations", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ model: "seedream_5.5", prompt: "neon harbor" }), + }) + expect(genRes.status).toBe(200) + + const uDeducted = await hooks.auth.usage(async () => signedIn) + expect(uDeducted.plan).toBe("VIP · 大师会员") + expect(uDeducted.until).toBeUndefined() + expect(uDeducted.balance).toBe("剩余 988 点") + expect(uDeducted.windows).toEqual([ + { + name: "会员点数", + used: 1.2, + amount: 12, + limit: 1000, + unit: "credits", + display: "剩余 988 / 1000 点 · 今日已用 12 点", + aside: true, + }, + ]) + + // Env DREAMINA_CREDITS_BALANCE fallback overrides configuredBalance cleanly + const prevEnv = process.env.DREAMINA_CREDITS_BALANCE + process.env.DREAMINA_CREDITS_BALANCE = "500" + try { + const uEnv = await hooks.auth.usage(async () => signedIn) + expect(uEnv.balance).toBe("剩余 488 点") + expect(uEnv.windows[0]).toEqual({ + name: "会员点数", + used: 2.4, + amount: 12, + limit: 500, + unit: "credits", + display: "剩余 488 / 500 点 · 今日已用 12 点", + aside: true, + }) + } finally { + if (prevEnv === undefined) delete process.env.DREAMINA_CREDITS_BALANCE + else process.env.DREAMINA_CREDITS_BALANCE = prevEnv + } + + // Live balance from auth account takes precedence over env/configured balance + accountPayload = { userId: 987654, isVip: true, vipLevel: "maestro", credits: 650 } + const uLive = await hooks.auth.usage(async () => signedIn) + expect(uLive.balance).toBe("剩余 650 点") + expect(uLive.windows).toEqual([ + { + name: "会员点数", + display: "剩余 650 点 · 今日已用 12 点", + aside: true, + }, + ]) + + // Also supports `balance` alias in auth account + accountPayload = { userId: 987654, isVip: false, balance: 420 } + const uLiveAlias = await hooks.auth.usage(async () => signedIn) + expect(uLiveAlias.plan).toBe("普通用户") + expect(uLiveAlias.balance).toBe("剩余 420 点") + }) +}) diff --git a/registry.json b/registry.json index 8237e459e..d1095e2a0 100644 --- a/registry.json +++ b/registry.json @@ -83,6 +83,8 @@ "summary": { "en": "Unofficial: Huawei Cloud CodeArts (码道) coding plans and OfficeAce (AgentArts), each signed in with Huawei Cloud OAuth on its own card — GLM, DeepSeek, openPangu and OfficeAce's MaaS models, live models and credit rates, credit usage by bucket and the daily check-in. Replaces officeace-auth; accounts signed in there keep working.", "zh": "非官方:华为云码道(CodeArts)编码套餐与 OfficeAce(AgentArts),两张卡各自用华为云 OAuth 登录;GLM、DeepSeek、openPangu 与 OfficeAce 的 MaaS 模型,实时模型与积分倍率、分桶积分用量和每日签到。取代 officeace-auth,原已登录的账号继续可用。" } }, { "package": "@magpie-community/opencode-gcp-ai-auth", "name": "GCP AI", "providers": ["gcp-ai"], "community": true, "summary": { "en": "Vertex AI models and Google Cloud promotional credits, signed with your own gcloud or ADC login. Lists only the models your project can call.", "zh": "用本机 gcloud / ADC 登录使用 Vertex AI 模型,并读取 Google Cloud 促销赠金;只列出项目实际能调用的模型。" } }, + { "package": "@magpie-community/opencode-dreamina-auth", "name": "Dreamina (即梦)", "providers": ["dreamina"], "community": true, + "summary": { "en": "Dreamina (Jimeng) through your local dreamina-canvas CLI: live Seedream image and Seedance / Vidu / Wan / HappyHorse video models, browser or existing CLI profile sign-in, VIP status and credit balance tracking, and quote-first 200-credit ceiling protection.", "zh": "通过本机 dreamina-canvas CLI 接入即梦(Dreamina):动态发现 Seedream 生图与 Seedance / Vidu / Wan / HappyHorse 生视频模型,支持复用本机 CLI 配置或浏览器登录、会员等级与点数余额统计,以及先报价和单次 200 点上限保护。" } }, { "package": "magpie-review-rules", "name": "Claude Desktop Review & Model Rules", "kind": "middleware", "summary": {"en":"Pin Claude Desktop auto-mode action reviews to a chosen Magpie model, independently of the conversation model. Configure per-model Claude compatibility presets and sandboxed JavaScript request rules in a native settings page. No request changes until configured.","zh":"固定 Claude Desktop 自动模式的操作审核模型,不随对话模型切换;通过原生设置页配置逐模型 Claude 兼容预设与沙箱 JavaScript 请求规则。首次安装不改写请求。"} }, { "package": "@magpie-community/middleware-param-override", "name": "Param Override", "kind": "middleware", "community": true,