feat(*): 短剧创作工作台 - 完善章节分镜、资产管理、技能运行与生成任务恢复
This commit is contained in:
1 parent
4f07daae9e
commit
2f6832f99c
113 files changed
+7016
-723
No files matched your search
@@ -196,7 +196,9 @@ default_tools_approval_mode = "approve"
|
||||
|
||||
侧边栏上传或粘贴的图片会先发到本机 Canvas Agent,再由 Canvas Agent 临时写入本机文件并作为 app-server `localImage` 输入传给 Codex;前端会提示附件体积,单次请求体限制为 30MB。
|
||||
|
||||
侧边栏 Composer 中显式提及的网页技能不会被拼接进用户 Prompt。网页把技能 bundle 传给本机 Runtime,Runtime 为当前 turn 临时生成受限的 `SKILL.md`,并通过 Codex app-server 的原生 `skill` 输入项加载;技能不会被复制进文本输入,也不会添加 `$skill-name` 伪标记,turn 完成后删除临时文件。未被用户提及的技能不会进入该 turn。
|
||||
侧边栏 Composer 中显式提及的网页技能不会被拼接进用户 Prompt。网页只为本轮提及的技能获取 bundle,本机 Runtime 将 `SKILL.md`、`references/`、`scripts/`、`assets/` 等目录完整写入当前 turn 的临时技能目录,并通过 Codex app-server 的原生 `skill` 输入项加载;技能不会被复制进文本输入,也不会添加 `$skill-name` 伪标记,turn 完成后删除整个临时目录。未被用户提及的技能不会传给本机 Runtime。
|
||||
|
||||
网页内置在线 Agent 使用另一条渐进式路径:系统上下文只列技能名称、简介、版本和文件数;模型先读取入口 `SKILL.md`,再按入口引用调用文件清单、搜索或单文件读取工具,不会一次性加载整个技能包。
|
||||
|
||||
## Claude Code
|
||||
|
||||
|
||||
@@ -13,7 +13,8 @@ import type { AgentAttachment, AgentEmit } from "./types.js";
|
||||
type Json = Record<string, unknown>;
|
||||
type AgentEvent = Json & { type: string; usage?: unknown };
|
||||
type PendingRequest = { resolve: (value: unknown) => void; reject: (error: Error) => void };
|
||||
export type AgentSkillReference = { skillId?: string; name: string; description?: string; instruction?: string };
|
||||
export type AgentSkillFile = { path: string; mimeType?: string; contentBase64: string };
|
||||
export type AgentSkillReference = { skillId?: string; name: string; description?: string; version?: string; files?: AgentSkillFile[]; instruction?: string };
|
||||
export type CodexSkillInput = { type: "skill"; name: string; path: string };
|
||||
type CodexRunOptions = { threadId?: string; cwd?: string; skills?: AgentSkillReference[]; onThreadId?: (threadId: string) => void };
|
||||
type AgentHistoryMessage = { id: string; role: "user" | "assistant" | "tool" | "error"; title?: string; text: string; detail?: unknown; streamId?: string };
|
||||
@@ -471,17 +472,32 @@ export async function writeSkillFiles(skills: AgentSkillReference[]) {
|
||||
const usedNames = new Set<string>();
|
||||
try {
|
||||
for (const skill of skills.slice(0, 8)) {
|
||||
const instruction = String(skill.instruction || "").trim().slice(0, 24_000);
|
||||
if (!instruction) continue;
|
||||
const baseName = `canvas-${safeSkillSegment(skill.skillId || skill.name)}`;
|
||||
const name = uniqueSkillName(baseName, usedNames);
|
||||
usedNames.add(name);
|
||||
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "infinite-canvas-skill-"));
|
||||
directories.push(directory);
|
||||
const temporaryRoot = await fs.mkdtemp(path.join(os.tmpdir(), "infinite-canvas-skill-"));
|
||||
directories.push(temporaryRoot);
|
||||
const directory = path.join(temporaryRoot, name);
|
||||
await fs.mkdir(directory, { recursive: true });
|
||||
const packageFiles = Array.isArray(skill.files) ? skill.files : [];
|
||||
if (packageFiles.length) {
|
||||
for (const item of packageFiles) {
|
||||
const relativePath = safeSkillFilePath(item.path);
|
||||
const target = path.join(directory, ...relativePath.split("/"));
|
||||
await fs.mkdir(path.dirname(target), { recursive: true });
|
||||
await fs.writeFile(target, Buffer.from(item.contentBase64, "base64"));
|
||||
}
|
||||
} else {
|
||||
const instruction = String(skill.instruction || "").trim().slice(0, 24_000);
|
||||
if (!instruction) continue;
|
||||
await fs.writeFile(path.join(directory, "SKILL.md"), skillMarkdown(name, skill.name, skill.description, instruction), "utf8");
|
||||
}
|
||||
const file = path.join(directory, "SKILL.md");
|
||||
const description = String(skill.description || skill.name).trim().slice(0, 500).replace(/[\r\n]+/g, " ");
|
||||
const body = [`---`, `name: ${name}`, `description: ${JSON.stringify(description)}`, `---`, ``, `# ${skill.name}`, ``, instruction, ``].join("\n");
|
||||
await fs.writeFile(file, body, "utf8");
|
||||
let entry = await fs.readFile(file, "utf8");
|
||||
if (!hasSkillFrontmatter(entry)) {
|
||||
entry = skillMarkdown(name, skill.name, skill.description, entry);
|
||||
await fs.writeFile(file, entry, "utf8");
|
||||
}
|
||||
inputs.push({ type: "skill", name, path: file });
|
||||
}
|
||||
return { directories, inputs };
|
||||
@@ -491,6 +507,29 @@ export async function writeSkillFiles(skills: AgentSkillReference[]) {
|
||||
}
|
||||
}
|
||||
|
||||
function skillMarkdown(name: string, title: string, description: string | undefined, instruction: string) {
|
||||
const summary = String(description || title).trim().slice(0, 500).replace(/[\r\n]+/g, " ");
|
||||
return [`---`, `name: ${name}`, `description: ${JSON.stringify(summary)}`, `---`, ``, `# ${title}`, ``, instruction, ``].join("\n");
|
||||
}
|
||||
|
||||
function hasSkillFrontmatter(value: string) {
|
||||
const normalized = value.replace(/^\uFEFF/, "");
|
||||
if (!normalized.startsWith("---\n") && !normalized.startsWith("---\r\n")) return false;
|
||||
const end = normalized.indexOf("\n---", 4);
|
||||
if (end < 0) return false;
|
||||
const frontmatter = normalized.slice(0, end);
|
||||
return /^name\s*:/m.test(frontmatter) && /^description\s*:/m.test(frontmatter);
|
||||
}
|
||||
|
||||
function safeSkillFilePath(value: string) {
|
||||
const normalized = String(value || "").trim().replace(/\\/g, "/");
|
||||
const segments = normalized.split("/");
|
||||
if (!normalized || normalized.startsWith("/") || segments.some((segment) => !segment || segment === "." || segment === "..") || normalized.includes("\0")) {
|
||||
throw new Error(`技能文件路径无效:${normalized || "空路径"}`);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function uniqueSkillName(baseName: string, usedNames: Set<string>) {
|
||||
if (!usedNames.has(baseName)) return baseName;
|
||||
let suffix = 2;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { Express } from "express";
|
||||
|
||||
import { CanvasSession } from "./canvas-session.js";
|
||||
import { CONFIG_DIR, type LocalRuntimeConfig } from "./config.js";
|
||||
import { createLocalRuntimeApp } from "./local-runtime.js";
|
||||
@@ -21,7 +23,7 @@ type CanvasAgentHttpOptions = {
|
||||
dependencies: CanvasAgentHttpDependencies;
|
||||
};
|
||||
|
||||
export function startHttpServer(options?: CanvasAgentHttpOptions) {
|
||||
export function startHttpServer(options?: CanvasAgentHttpOptions): ReturnType<typeof startLocalRuntime> | Express {
|
||||
if (!options) return startLocalRuntime();
|
||||
return createHttpApp(options.config, options.session, options.dependencies);
|
||||
}
|
||||
@@ -30,7 +32,7 @@ export function createHttpApp(
|
||||
config: LocalRuntimeConfig,
|
||||
session: CanvasAgentSession = new CanvasSession(),
|
||||
dependencies: CanvasAgentHttpDependencies = {},
|
||||
) {
|
||||
): Express {
|
||||
const endpoint = config.url;
|
||||
const manager = new LocalRuntimeSessionManager({
|
||||
endpoint,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import path from "node:path";
|
||||
|
||||
import { createServer, type Server } from "node:http";
|
||||
import type { Express } from "express";
|
||||
|
||||
import {
|
||||
CONFIG_DIR,
|
||||
@@ -25,6 +26,14 @@ export type StartLocalRuntimeOptions = {
|
||||
persistConfig?: (config: LocalRuntimeConfig) => void;
|
||||
};
|
||||
|
||||
export type LocalRuntimeHandle = {
|
||||
app: Express;
|
||||
server: Server;
|
||||
sessions: LocalRuntimeSessionManager;
|
||||
ready: Promise<void>;
|
||||
close: () => Promise<void>;
|
||||
};
|
||||
|
||||
export function createDefaultLocalRuntimeModules(config: LocalRuntimeConfig): LocalRuntimeModule[] {
|
||||
return [
|
||||
createCanvasAgentHttpModule(config),
|
||||
@@ -40,7 +49,7 @@ export function createDefaultLocalRuntimeModules(config: LocalRuntimeConfig): Lo
|
||||
];
|
||||
}
|
||||
|
||||
export function startLocalRuntime(options: StartLocalRuntimeOptions = {}) {
|
||||
export function startLocalRuntime(options: StartLocalRuntimeOptions = {}): LocalRuntimeHandle {
|
||||
const config = options.config ?? loadConfig(true);
|
||||
const persistConfig = options.persistConfig ?? saveConfig;
|
||||
const requestedPort = options.port ?? (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Request, RequestHandler, Response } from "express";
|
||||
import type { Express, Request, RequestHandler, Response } from "express";
|
||||
import express from "express";
|
||||
|
||||
import {
|
||||
@@ -55,14 +55,14 @@ export type CreateLocalRuntimeAppOptions = {
|
||||
legacyOrigins?: readonly string[];
|
||||
};
|
||||
|
||||
export function createLocalRuntimeApp(options: CreateLocalRuntimeAppOptions) {
|
||||
export function createLocalRuntimeApp(options: CreateLocalRuntimeAppOptions): Express {
|
||||
const modules = validateModules(options.modules);
|
||||
const policies = corsPolicies(modules, options.legacyOrigins ?? []);
|
||||
const app = express();
|
||||
app.disable("x-powered-by");
|
||||
app.use(express.raw({
|
||||
type: "application/json",
|
||||
limit: "30mb",
|
||||
limit: "64mb",
|
||||
}));
|
||||
app.use(noStore);
|
||||
app.use(exactAuthorityGuard(options.authority));
|
||||
|
||||
@@ -221,16 +221,54 @@ export function parseAgentSkills(value: unknown) {
|
||||
const input = item as Record<string, unknown>;
|
||||
const name = typeof input.name === "string" ? input.name.trim().slice(0, 120) : "";
|
||||
const instruction = typeof input.instruction === "string" ? input.instruction.trim().slice(0, 24_000) : "";
|
||||
if (!name || !instruction) return [];
|
||||
const files = parseAgentSkillFiles(input.files);
|
||||
if (!name || (!instruction && !files.length)) return [];
|
||||
return [{
|
||||
...(typeof input.skillId === "string" ? { skillId: input.skillId.trim().slice(0, 120) } : {}),
|
||||
name,
|
||||
...(typeof input.description === "string" ? { description: input.description.trim().slice(0, 500) } : {}),
|
||||
instruction,
|
||||
...(typeof input.version === "string" ? { version: input.version.trim().slice(0, 120) } : {}),
|
||||
...(files.length ? { files } : { instruction }),
|
||||
}];
|
||||
});
|
||||
}
|
||||
|
||||
function parseAgentSkillFiles(value: unknown) {
|
||||
if (!Array.isArray(value)) return [];
|
||||
const files: Array<{ path: string; mimeType?: string; contentBase64: string }> = [];
|
||||
const paths = new Set<string>();
|
||||
let totalBytes = 0;
|
||||
for (const item of value.slice(0, 512)) {
|
||||
if (!item || typeof item !== "object" || Array.isArray(item)) return [];
|
||||
const input = item as Record<string, unknown>;
|
||||
const filePath = typeof input.path === "string" ? normalizeAgentSkillPath(input.path) : "";
|
||||
const contentBase64 = typeof input.contentBase64 === "string" ? input.contentBase64.trim() : "";
|
||||
if (!filePath || paths.has(filePath) || !validBase64(contentBase64)) return [];
|
||||
const size = Buffer.from(contentBase64, "base64").byteLength;
|
||||
if (size > 8 * 1024 * 1024) return [];
|
||||
totalBytes += size;
|
||||
if (totalBytes > 20 * 1024 * 1024) return [];
|
||||
paths.add(filePath);
|
||||
files.push({
|
||||
path: filePath,
|
||||
...(typeof input.mimeType === "string" ? { mimeType: input.mimeType.trim().slice(0, 255) } : {}),
|
||||
contentBase64,
|
||||
});
|
||||
}
|
||||
return files.some((file) => file.path === "SKILL.md") ? files : [];
|
||||
}
|
||||
|
||||
function normalizeAgentSkillPath(value: string) {
|
||||
const normalized = value.trim().replace(/\\/g, "/");
|
||||
const segments = normalized.split("/");
|
||||
if (!normalized || normalized.startsWith("/") || normalized.length > 1000 || segments.some((segment) => !segment || segment === "." || segment === "..") || normalized.includes("\0")) return "";
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function validBase64(value: string) {
|
||||
return value.length % 4 === 0 && /^[A-Za-z0-9+/]*={0,2}$/.test(value);
|
||||
}
|
||||
|
||||
function queryValue(req: Request, key: string) {
|
||||
const value = req.query[key];
|
||||
return Array.isArray(value) ? String(value[0] ?? "") : String(value ?? "");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
|
||||
import { codexInput, writeSkillFiles } from "../src/agents.js";
|
||||
@@ -24,19 +25,31 @@ test("writeSkillFiles creates Codex-compatible SKILL.md inputs and unique names"
|
||||
const prepared = await writeSkillFiles([
|
||||
{ skillId: "same", name: "第一个技能", description: "first", instruction: "do first" },
|
||||
{ skillId: "same", name: "第二个技能", description: "second", instruction: "do second" },
|
||||
{
|
||||
skillId: "package",
|
||||
name: "目录技能",
|
||||
description: "package",
|
||||
files: [
|
||||
{ path: "SKILL.md", contentBase64: Buffer.from("# 目录技能\n\n按需读取参考文件。", "utf8").toString("base64") },
|
||||
{ path: "references/guide.md", contentBase64: Buffer.from("# Guide", "utf8").toString("base64") },
|
||||
],
|
||||
},
|
||||
{ skillId: "empty", name: "空技能", instruction: " " },
|
||||
]);
|
||||
|
||||
try {
|
||||
assert.equal(prepared.inputs.length, 2);
|
||||
assert.deepEqual(prepared.inputs.map((item) => item.type), ["skill", "skill"]);
|
||||
assert.deepEqual(prepared.inputs.map((item) => item.name), ["canvas-same", "canvas-same-2"]);
|
||||
for (const item of prepared.inputs) {
|
||||
assert.equal(prepared.inputs.length, 3);
|
||||
assert.deepEqual(prepared.inputs.map((item) => item.type), ["skill", "skill", "skill"]);
|
||||
assert.deepEqual(prepared.inputs.map((item) => item.name), ["canvas-same", "canvas-same-2", "canvas-package"]);
|
||||
for (const item of prepared.inputs.slice(0, 2)) {
|
||||
assert.equal(item.path.endsWith("/SKILL.md"), true);
|
||||
const body = await fs.readFile(item.path, "utf8");
|
||||
assert.match(body, /^---\nname: canvas-same(?:-2)?\ndescription: /);
|
||||
assert.match(body, /\n---\n\n#/);
|
||||
}
|
||||
const packageInput = prepared.inputs[2];
|
||||
assert.match(await fs.readFile(packageInput.path, "utf8"), /^---\nname: canvas-package\ndescription: /);
|
||||
assert.equal(await fs.readFile(path.join(path.dirname(packageInput.path), "references", "guide.md"), "utf8"), "# Guide");
|
||||
} finally {
|
||||
await Promise.all(prepared.directories.map((directory) => fs.rm(directory, { recursive: true, force: true })));
|
||||
}
|
||||
@@ -44,14 +57,26 @@ test("writeSkillFiles creates Codex-compatible SKILL.md inputs and unique names"
|
||||
|
||||
test("parseAgentSkills validates and bounds browser skill bundles", () => {
|
||||
const input = [
|
||||
{ skillId: "safe", name: " safe ", description: " desc ", instruction: " instruction " },
|
||||
{
|
||||
skillId: "safe",
|
||||
name: " safe ",
|
||||
description: " desc ",
|
||||
version: " v2 ",
|
||||
files: [
|
||||
{ path: "SKILL.md", mimeType: "text/markdown", contentBase64: Buffer.from("# Safe").toString("base64") },
|
||||
{ path: "references/a.md", mimeType: "text/markdown", contentBase64: Buffer.from("A").toString("base64") },
|
||||
],
|
||||
},
|
||||
{ skillId: "legacy", name: "legacy", instruction: " instruction " },
|
||||
{ name: "missing instruction" },
|
||||
null,
|
||||
{ name: "too long", instruction: "x".repeat(25_000) },
|
||||
{ name: "unsafe", files: [{ path: "../SKILL.md", contentBase64: Buffer.from("bad").toString("base64") }] },
|
||||
];
|
||||
const parsed = parseAgentSkills(input);
|
||||
|
||||
assert.equal(parsed.length, 2);
|
||||
assert.deepEqual(parsed[0], { skillId: "safe", name: "safe", description: "desc", instruction: "instruction" });
|
||||
assert.equal(parsed[1].instruction.length, 24_000);
|
||||
assert.equal(parsed[0].name, "safe");
|
||||
assert.equal(parsed[0].version, "v2");
|
||||
assert.deepEqual(parsed[0].files?.map((file) => file.path), ["SKILL.md", "references/a.md"]);
|
||||
assert.deepEqual(parsed[1], { skillId: "legacy", name: "legacy", instruction: "instruction" });
|
||||
});
|
||||
Reference in new issue
Block a user