@dawnwood/cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # @dawnwood/cli — Dawnwood for agents
2
+
3
+ CLI + MCP server,让本地 Agent(Claude Code / Cursor / Codex)直连 dawnwood
4
+ harness 跑全管线:圣经 → 剧本链 → 设定包 → 美术/渲染。人工审核走 console
5
+ 深链(`get_review_url`)。
6
+
7
+ 设计与裁决台账:`docs/designs/agent-access-mcp-cli-2026-08.md`。
8
+
9
+ ## 快速开始
10
+
11
+ ```bash
12
+ # 1. console(Settings → API tokens)铸一枚 PAT(只显示一次,默认 90 天)
13
+ # 2. 登录(token 落 ~/.config/dawnwood/config.json,0600)
14
+ npx @dawnwood/cli auth login --token dw_pat_v1_...
15
+
16
+ # 3a. 挂进 Claude Code
17
+ claude mcp add dawnwood -- npx -y @dawnwood/cli mcp
18
+
19
+ # 3b. 或直接用 CLI 验身份
20
+ npx @dawnwood/cli whoami
21
+ ```
22
+
23
+ ## 工具面(v1,冻结于 test/mcp.test.ts)
24
+
25
+ /runs PORTAL 族 1:1 + `credits_balance` + `get_review_url`。交付包/发布是
26
+ 二期(评审 D8);轮询纪律:`get_run`/`art_status`,间隔 ≥10s,`list_runs`
27
+ 禁止轮询(D7)。
28
+
29
+ ## 环境变量
30
+
31
+ - `DAWNWOOD_TOKEN` — 覆盖本地配置的 token(CI 用)
32
+ - `DAWNWOOD_API_BASE` — 默认 `https://console.dawnwood.ai/api`
33
+ - `DAWNWOOD_AGENT` — 遥测归因名(`x-tfund-agent` 头)
34
+ - `DAWNWOOD_CONFIG_DIR` — 配置目录(默认 `~/.config/dawnwood`)
35
+
36
+ ## 发布
37
+
38
+ `git tag cli-v<version> && git push origin cli-v<version>` →
39
+ `.github/workflows/npm-publish.yml`(需 NPM_TOKEN secret;tag 与
40
+ package.json 版本一致性有闸)。
package/dist/api.js ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * console API 的薄客户端。业务逻辑一律在服务端(评审:MCP 工具是薄代理,
3
+ * 客户端只做鉴权头/错误映射/URL 拼装,避免与 route contract 漂移)。
4
+ *
5
+ * 每个请求带:
6
+ * Authorization: Bearer dw_pat_v1_… (PAT;api.py 身份收口的 Bearer 分支)
7
+ * x-tfund-agent: <agent 名> (成本/遥测归因,api.py 已支持)
8
+ * x-dw-tool: <tool 名> (零费率遥测的 meta.tool)
9
+ */
10
+ import { getApiBase, getToken } from "./config.js";
11
+ export class ApiError extends Error {
12
+ status;
13
+ body;
14
+ constructor(status, message, body) {
15
+ super(message);
16
+ this.status = status;
17
+ this.body = body;
18
+ }
19
+ }
20
+ /** 把 HTTP 失败翻成 agent 能自救的文案 —— 402/401/503 各有明确下一步。 */
21
+ function explain(status, body) {
22
+ const err = typeof body?.error === "string" ? body.error : JSON.stringify(body);
23
+ switch (status) {
24
+ case 401:
25
+ return `${err} — token 无效/过期/已撤销。到 console 头像菜单 → API tokens 重铸一枚,然后 \`dawnwood auth login --token <新token>\`。`;
26
+ case 402:
27
+ return `${err} — credits 不足。到 ${getApiBase().replace(/\/api$/, "")}/#billing 充值或升级订阅后重试。`;
28
+ case 403:
29
+ return `${err} — 这个 token 的权限面不含该操作(customer PAT 只有自助面;交付/发布是二期)。`;
30
+ case 503:
31
+ return `${err} — 服务端暂时无法校验 token(fail-closed),稍等重试即可,token 本身没坏。`;
32
+ default:
33
+ return err;
34
+ }
35
+ }
36
+ export async function apiCall(path, opts = {}) {
37
+ const token = opts.token ?? getToken();
38
+ if (!token) {
39
+ throw new ApiError(0, "未登录 — 先跑 `dawnwood auth login --token <dw_pat_v1_...>`(token 在 console 头像菜单 → API tokens 铸)。", null);
40
+ }
41
+ const base = opts.base ?? getApiBase();
42
+ const f = opts.fetchImpl ?? fetch;
43
+ const res = await f(`${base}${path}`, {
44
+ method: opts.method || "GET",
45
+ headers: {
46
+ "authorization": `Bearer ${token}`,
47
+ "content-type": "application/json",
48
+ "x-tfund-agent": opts.agent || process.env.DAWNWOOD_AGENT || "dawnwood-cli",
49
+ ...(opts.tool ? { "x-dw-tool": opts.tool } : {}),
50
+ },
51
+ body: opts.body === undefined ? undefined : JSON.stringify(opts.body),
52
+ });
53
+ let body = null;
54
+ const text = await res.text();
55
+ try {
56
+ body = text ? JSON.parse(text) : null;
57
+ }
58
+ catch {
59
+ body = { error: text.slice(0, 400) };
60
+ }
61
+ if (!res.ok) {
62
+ throw new ApiError(res.status, explain(res.status, (body || {})), body);
63
+ }
64
+ return body;
65
+ }
package/dist/config.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Token 与 API 地址的本地配置。
3
+ *
4
+ * 优先级:环境变量 > ~/.config/dawnwood/config.json。
5
+ * 落盘 0600(评审 D9:贴 token 登录,token 是长效密钥,权限位是底线),
6
+ * 目录 0700。绝不在 stdout/stderr 回显完整 token(掩码到后 4 位)。
7
+ */
8
+ import * as fs from "node:fs";
9
+ import * as os from "node:os";
10
+ import * as path from "node:path";
11
+ export const API_BASE_DEFAULT = "https://console.dawnwood.ai/api";
12
+ export const CONSOLE_BASE_DEFAULT = "https://console.dawnwood.ai";
13
+ export function configDir() {
14
+ return process.env.DAWNWOOD_CONFIG_DIR ||
15
+ path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "dawnwood");
16
+ }
17
+ function configPath() {
18
+ return path.join(configDir(), "config.json");
19
+ }
20
+ export function loadConfig() {
21
+ try {
22
+ return JSON.parse(fs.readFileSync(configPath(), "utf8"));
23
+ }
24
+ catch {
25
+ return {};
26
+ }
27
+ }
28
+ export function saveConfig(cfg) {
29
+ const dir = configDir();
30
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
31
+ const p = configPath();
32
+ fs.writeFileSync(p, JSON.stringify(cfg, null, 2) + "\n", { mode: 0o600 });
33
+ // mkdir/写入若沿用了旧 umask 或已存在文件的旧权限位,这里强制归位。
34
+ fs.chmodSync(p, 0o600);
35
+ return p;
36
+ }
37
+ export function getToken() {
38
+ return process.env.DAWNWOOD_TOKEN || loadConfig().token;
39
+ }
40
+ export function getApiBase() {
41
+ return process.env.DAWNWOOD_API_BASE || loadConfig().api_base || API_BASE_DEFAULT;
42
+ }
43
+ export function maskToken(token) {
44
+ return token.length <= 8 ? "****" : `${token.slice(0, 10)}…${token.slice(-4)}`;
45
+ }
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * dawnwood CLI —— 单包双入口(评审 D1):
4
+ * dawnwood auth login --token <dw_pat_v1_...> 贴 PAT 登录(评审 D9;0600 落盘)
5
+ * dawnwood auth status | logout
6
+ * dawnwood whoami 验 token(GET /me)
7
+ * dawnwood mcp 启动 stdio MCP server
8
+ *
9
+ * token 在 console(头像菜单 → API tokens)铸造;设备码登录是二期(评审 D9)。
10
+ */
11
+ import { apiCall } from "./api.js";
12
+ import { getApiBase, getToken, loadConfig, maskToken, saveConfig } from "./config.js";
13
+ function out(s) {
14
+ process.stdout.write(s + "\n");
15
+ }
16
+ function die(s, code = 1) {
17
+ process.stderr.write(s + "\n");
18
+ process.exit(code);
19
+ }
20
+ async function main() {
21
+ const [, , cmd, sub, ...rest] = process.argv;
22
+ if (cmd === "mcp") {
23
+ const { runMcp } = await import("./mcp.js");
24
+ await runMcp();
25
+ return;
26
+ }
27
+ if (cmd === "auth" && sub === "login") {
28
+ const i = rest.indexOf("--token");
29
+ const token = i >= 0 ? rest[i + 1] : undefined;
30
+ if (!token) {
31
+ die("用法: dawnwood auth login --token <dw_pat_v1_...>\n" +
32
+ "token 在 console(头像菜单 → API tokens)铸造,只显示一次。");
33
+ }
34
+ if (!token.startsWith("dw_pat_v1_")) {
35
+ die("这不像一枚 dawnwood PAT(应以 dw_pat_v1_ 开头)。");
36
+ }
37
+ // 铸造响应里的占位/截断很常见 —— 落盘前先用一次只读调用验真伪。
38
+ try {
39
+ const me = (await apiCall("/me", { token, tool: "auth_login" }));
40
+ const p = saveConfig({ ...loadConfig(), token });
41
+ out(`已登录 ${me.email ?? "?"}(token ${maskToken(token)})→ ${p}(0600)`);
42
+ }
43
+ catch (e) {
44
+ die(`token 验证失败:${e instanceof Error ? e.message : e}`);
45
+ }
46
+ return;
47
+ }
48
+ if (cmd === "auth" && sub === "status") {
49
+ const t = getToken();
50
+ out(t ? `已登录(token ${maskToken(t)},api ${getApiBase()})` : "未登录");
51
+ return;
52
+ }
53
+ if (cmd === "auth" && sub === "logout") {
54
+ const cfg = loadConfig();
55
+ delete cfg.token;
56
+ saveConfig(cfg);
57
+ out("已退出(本地 token 已删;要彻底作废去 console 撤销那枚 token)。");
58
+ return;
59
+ }
60
+ if (cmd === "whoami") {
61
+ const me = await apiCall("/me", { tool: "whoami" });
62
+ out(JSON.stringify(me, null, 2));
63
+ return;
64
+ }
65
+ out(`dawnwood — agent access CLI
66
+
67
+ 用法:
68
+ dawnwood auth login --token <dw_pat_v1_...> 登录(token 在 console 头像菜单 → API tokens 铸)
69
+ dawnwood auth status 查看登录状态
70
+ dawnwood auth logout 退出(本地删 token)
71
+ dawnwood whoami 验 token / 看身份与权限
72
+ dawnwood mcp 启动 stdio MCP server(给 Claude Code / Cursor 等)
73
+
74
+ MCP 接入(Claude Code):
75
+ claude mcp add dawnwood -- npx -y @dawnwood/cli mcp
76
+ 环境变量:DAWNWOOD_TOKEN / DAWNWOOD_API_BASE / DAWNWOOD_AGENT 可覆盖本地配置。`);
77
+ }
78
+ main().catch((e) => die(e instanceof Error ? e.message : String(e)));
package/dist/mcp.js ADDED
@@ -0,0 +1,87 @@
1
+ /**
2
+ * dawnwood MCP server(stdio)——harness 级 agent 面 v1。
3
+ *
4
+ * 工具面 = /runs PORTAL 族 1:1(圣经→剧本链→设定包→美术/渲染)+ credits +
5
+ * get_review_url(console 深链,人工审核动线;评审 D8:交付包/发布是二期,
6
+ * 不在此面)。工具是薄代理:业务/校验/计费全在服务端,这里只拼请求。
7
+ *
8
+ * 轮询纪律(评审 D7):查状态用 get_run / art_status(单 run O(1) 直取),
9
+ * 间隔 ≥10 秒;list_runs 是浏览用的,禁止拿来轮询。
10
+ */
11
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
12
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
13
+ import { z } from "zod";
14
+ import { apiCall } from "./api.js";
15
+ import { CONSOLE_BASE_DEFAULT, getApiBase } from "./config.js";
16
+ const AGENT = process.env.DAWNWOOD_AGENT || "mcp";
17
+ function consoleBase() {
18
+ return process.env.DAWNWOOD_CONSOLE_BASE ||
19
+ getApiBase().replace(/\/api\/?$/, "") || CONSOLE_BASE_DEFAULT;
20
+ }
21
+ function ok(data) {
22
+ return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
23
+ }
24
+ function fail(e) {
25
+ const msg = e instanceof Error ? e.message : String(e);
26
+ return { content: [{ type: "text", text: msg }], isError: true };
27
+ }
28
+ async function call(path, tool, method, body) {
29
+ try {
30
+ return ok(await apiCall(path, { method, body, tool, agent: AGENT }));
31
+ }
32
+ catch (e) {
33
+ return fail(e);
34
+ }
35
+ }
36
+ export function buildServer() {
37
+ const server = new McpServer({ name: "dawnwood", version: "0.1.0" });
38
+ // ---- runs 生命周期 ---------------------------------------------------------
39
+ server.tool("list_runs", "列出你的 runs(草稿与进行中)。浏览用——**禁止用它轮询状态**,查单个 run 用 get_run。", async () => call("/runs", "list_runs"));
40
+ server.tool("get_run", "读单个 run 的完整状态(向导步骤/圣经/剧本/pack/art 进度)。这是轮询状态的正确工具(O(1) 直取);轮询间隔 ≥10 秒。", { run_id: z.string().describe("run id(snake_case)") }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}`, "get_run"));
41
+ server.tool("create_run", "创建一个新剧 run(向导 Step 1:brief)。premise 100-6000 字符;titles 必须含 primary_locale 一项;必须显式 compliance_ack=true(无真人肖像/无第三方 IP)。集数默认 10(改集数是运营特权)。", {
42
+ run_id: z.string().describe("snake_case ascii id,全局唯一"),
43
+ titles: z.record(z.string(), z.string()).describe('各语言标题,如 {"en": "..."}'),
44
+ primary_locale: z.string().default("en"),
45
+ premise: z.string().describe("剧情前提,100-6000 字符"),
46
+ lang: z.string().default("en").describe("产出语言(BCP-47)"),
47
+ render_style: z.enum(["real", "comic"]).default("real"),
48
+ compliance_ack: z.boolean().describe("确认无真人肖像/无第三方 IP,必须 true"),
49
+ }, async (a) => call("/runs", "create_run", "POST", {
50
+ id: a.run_id, titles: a.titles, primary_locale: a.primary_locale,
51
+ premise: a.premise, lang: a.lang, render_style: a.render_style,
52
+ compliance_ack: a.compliance_ack,
53
+ }));
54
+ server.tool("delete_run", "放弃一个向导草稿 run(不可逆;只能删自助 run)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}`, "delete_run", "DELETE"));
55
+ // ---- 圣经(Step 2) ---------------------------------------------------------
56
+ server.tool("generate_bible", "生成剧集圣经(世界观/人物/弧线)。异步倾向的长操作,完成后用 get_bible 读。可带 feedback 重生成。", { run_id: z.string(), feedback: z.string().optional().describe("对上一版的修改意见") }, async ({ run_id, feedback }) => call(`/runs/${encodeURIComponent(run_id)}/bible/generate`, "generate_bible", "POST", feedback ? { feedback } : {}));
57
+ server.tool("get_bible", "读当前圣经(含版本轨)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/bible`, "get_bible"));
58
+ server.tool("update_bible", "直接改写圣经内容(整体 PUT;细粒度改动优先走 generate_bible 的 feedback)。", { run_id: z.string(), bible: z.record(z.string(), z.unknown()).describe("圣经 JSON 全量") }, async ({ run_id, bible }) => call(`/runs/${encodeURIComponent(run_id)}/bible`, "update_bible", "PUT", bible));
59
+ server.tool("approve_bible", "批准圣经,解锁剧本链。批准是闸门动作——建议先用 get_review_url 让人在 console 里过目。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/bible/approve`, "approve_bible", "POST"));
60
+ // ---- 剧本链(Step 3) -------------------------------------------------------
61
+ server.tool("generate_scripts", "跑剧本链(逐集生成,带前情摘要)。长操作;进度用 get_run 轮询(≥10s)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/scripts/generate`, "generate_scripts", "POST"));
62
+ server.tool("get_script", "读某一集剧本。ep 形如 '0001'。", { run_id: z.string(), ep: z.string().describe("集号,如 0001") }, async ({ run_id, ep }) => call(`/runs/${encodeURIComponent(run_id)}/scripts/${encodeURIComponent(ep)}`, "get_script"));
63
+ server.tool("update_script", "改写某一集剧本(PUT;approve 后的改动会计费,approve 前的 feedback 重生免费)。", { run_id: z.string(), ep: z.string(), body: z.record(z.string(), z.unknown()) }, async ({ run_id, ep, body }) => call(`/runs/${encodeURIComponent(run_id)}/scripts/${encodeURIComponent(ep)}`, "update_script", "PUT", body));
64
+ server.tool("approve_script", "批准某一集剧本(闸门动作;建议先 get_review_url 人工过目)。", { run_id: z.string(), ep: z.string() }, async ({ run_id, ep }) => call(`/runs/${encodeURIComponent(run_id)}/scripts/${encodeURIComponent(ep)}/approve`, "approve_script", "POST"));
65
+ // ---- 设定包(Step 4) -------------------------------------------------------
66
+ server.tool("generate_pack", "生成设定包草稿(角色/场景/道具的视觉设定)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/pack/generate`, "generate_pack", "POST"));
67
+ server.tool("update_pack", "改设定包草稿(PUT 全量)。", { run_id: z.string(), pack: z.record(z.string(), z.unknown()) }, async ({ run_id, pack }) => call(`/runs/${encodeURIComponent(run_id)}/pack`, "update_pack", "PUT", pack));
68
+ server.tool("commit_pack", "锁定设定包,进入美术/渲染阶段(闸门动作)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/pack/commit`, "commit_pack", "POST"));
69
+ // ---- 美术/渲染(Step 5) ----------------------------------------------------
70
+ server.tool("art_estimate", "美术/渲染的 credits 报价(动手前先看账)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/art/estimate`, "art_estimate"));
71
+ server.tool("generate_art", "启动美术/渲染(烧 credits 的一步;先跑 art_estimate 确认)。进度用 art_status 轮询(≥10s)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/art/generate`, "generate_art", "POST"));
72
+ server.tool("art_status", "美术/渲染进度(单 run 直取,轮询用这个,间隔 ≥10 秒)。", { run_id: z.string() }, async ({ run_id }) => call(`/runs/${encodeURIComponent(run_id)}/art/status`, "art_status"));
73
+ // ---- 账面 ------------------------------------------------------------------
74
+ server.tool("credits_balance", "查 credits 余额与免费额度。", async () => call("/credits", "credits_balance"));
75
+ // ---- 人工审核动线(前提 5:console = 审核/裁决面) ---------------------------
76
+ server.tool("get_review_url", "返回 console 深链,交给人类在浏览器里做视觉审核/裁决(选图、审片、批准)。不发任何 API 请求。在 approve_* 之前把这个链接给用户是推荐动线。", { run_id: z.string() }, async ({ run_id }) => ok({
77
+ url: `${consoleBase()}/#run-wizard/${encodeURIComponent(run_id)}`,
78
+ note: "在浏览器打开(需要 console 登录);agent 不应该也不能替人做视觉裁决。",
79
+ }));
80
+ return server;
81
+ }
82
+ export async function runMcp() {
83
+ const server = buildServer();
84
+ const transport = new StdioServerTransport();
85
+ await server.connect(transport);
86
+ // stdio server:进程由客户端(Claude Code 等)托管,直到 stdin 关闭。
87
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@dawnwood/cli",
3
+ "version": "0.1.0",
4
+ "description": "Dawnwood for agents — CLI + MCP server. Your agent drives the full short-drama pipeline (bible → scripts → pack → renders) against console.dawnwood.ai.",
5
+ "license": "UNLICENSED",
6
+ "type": "module",
7
+ "bin": {
8
+ "dawnwood": "dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "engines": {
14
+ "node": ">=18"
15
+ },
16
+ "scripts": {
17
+ "build": "tsc -p tsconfig.json",
18
+ "test": "vitest run",
19
+ "prepublishOnly": "npm run build && npm test"
20
+ },
21
+ "dependencies": {
22
+ "@modelcontextprotocol/sdk": "^1.0.0",
23
+ "zod": "^3.23.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^20.0.0",
27
+ "typescript": "^5.4.0",
28
+ "vitest": "^2.0.0"
29
+ }
30
+ }