Files
writing-agent/tests/dictate-read-module.test.ts
chenmoranzhiandCursor 1cc4ae557a 创作一轮改为先选题再填写,减少工具空转和重复覆盖。
上下文按规则、历史、产物分段拼装,便于吃提示缓存并核对费用。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-07 23:48:20 +08:00

385 lines
12 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
DICTATE_MODULE_READ_STATE_TAG,
buildDictateModuleReadIndex,
runDictateTurn,
} from "../src/dictate/index.js";
import { loadModuleCatalog } from "../src/skills/creation-flow.js";
import type { AgentDriver } from "../src/runtime/driver.js";
async function setup() {
const catalog = await loadModuleCatalog("dialogue/world-simulator");
expect(catalog).toBeTruthy();
const moduleReadIndex = await buildDictateModuleReadIndex({
skillPackRoot: "dialogue/world-simulator",
catalog: catalog!,
});
return { catalog: catalog!, moduleReadIndex };
}
describe("dictate read_module", () => {
it("rejects insert before reading the current prompt version", async () => {
const { catalog, moduleReadIndex } = await setup();
const tags = new Map<string, string>();
const driver: AgentDriver = {
async run(input) {
const outcome = await input.handleStep([
{
id: "i1",
name: "insert",
arguments: JSON.stringify({
position: "设计.美学纲领与交互范式",
content: "{}",
self_score: {
dims: [
{ name: "美学纲领", score: 5 },
{ name: "交互范式", score: 5 },
{ name: "背景与规则", score: 5 },
],
},
}),
},
]);
if (outcome.kind === "continue") {
const payload = JSON.parse(outcome.results[0]!.content);
expect(payload.error).toContain('read_module("aesthetics-interaction")');
}
return { iterations: 1, stop: { kind: "text", content: "未写入" } };
},
};
const result = await runDictateTurn({
llm: {} as never,
dialogue: [],
moduleCatalog: catalog,
moduleReadIndex,
driver,
handlers: handlers(tags),
});
expect(result.wroteTags).toEqual([]);
});
it("allows reading multiple modules then inserting both in the same step", async () => {
const { catalog, moduleReadIndex } = await setup();
const tags = new Map<string, string>();
const driver: AgentDriver = {
async run(input) {
const outcome = await input.handleStep([
{
id: "r1",
name: "read_module",
arguments: JSON.stringify({ module_id: "美学纲领与交互范式" }),
},
{
id: "r2",
name: "read_module",
arguments: JSON.stringify({ module_id: "protagonist" }),
},
{
id: "i1",
name: "insert",
arguments: JSON.stringify({
position: "设计.美学纲领与交互范式",
content: "{}",
self_score: {
dims: [
{ name: "美学纲领", score: 5 },
{ name: "交互范式", score: 5 },
{ name: "背景与规则", score: 3, gap: "规则因果仍需确认" },
],
},
}),
},
{
id: "i2",
name: "insert",
arguments: JSON.stringify({
position: "设计.主角设定",
content: "{}",
self_score: {
dims: [
{ name: "宽松度", score: 5 },
{ name: "必要度", score: 5 },
],
},
}),
},
]);
if (outcome.kind === "continue") {
const read = JSON.parse(outcome.results[0]!.content);
expect(read.task).toBeTruthy();
expect(read.version).toMatch(/^[a-f0-9]{16}$/);
expect(JSON.parse(outcome.results[2]!.content).ok).toBe(true);
expect(JSON.parse(outcome.results[3]!.content).ok).toBe(true);
}
return { iterations: 1, stop: { kind: "text", content: "已写入" } };
},
};
const result = await runDictateTurn({
llm: {} as never,
dialogue: [],
moduleCatalog: catalog,
moduleReadIndex,
driver,
handlers: handlers(tags),
});
expect(result.wroteTags).toEqual([
"设计.美学纲领与交互范式",
"设计.主角设定",
]);
expect(tags.get(DICTATE_MODULE_READ_STATE_TAG)).toContain("protagonist");
});
it("returns presentation references for reply-format", async () => {
const { moduleReadIndex } = await setup();
const module = moduleReadIndex.get("reply-format");
expect(module?.presentation_references?.shells.length).toBeGreaterThan(0);
expect(module?.presentation_references?.markdown_safe_subset).toContain(
"正文安全子集",
);
expect(module?.presentation_references?.iframe_contract).toContain(
"present.onData",
);
});
it("requires another read when the prompt version changes", async () => {
const { catalog, moduleReadIndex } = await setup();
const tags = new Map<string, string>();
const driver: AgentDriver = {
async run(input) {
await input.handleStep([
{
id: "r1",
name: "read_module",
arguments: JSON.stringify({ module_id: "aesthetics-interaction" }),
},
]);
moduleReadIndex.get("aesthetics-interaction")!.version = "new-version";
const outcome = await input.handleStep([
{
id: "i1",
name: "insert",
arguments: JSON.stringify({
position: "设计.美学纲领与交互范式",
content: "{}",
}),
},
]);
if (outcome.kind === "continue") {
expect(JSON.parse(outcome.results[0]!.content).error).toContain(
"当前提示版本",
);
}
return { iterations: 2, stop: { kind: "text", content: "需重读" } };
},
};
const result = await runDictateTurn({
llm: {} as never,
dialogue: [],
moduleCatalog: catalog,
moduleReadIndex,
driver,
handlers: handlers(tags),
});
expect(result.wroteTags).toEqual([]);
});
it("validates score dimensions and stores low-score gaps as metadata", async () => {
const { catalog, moduleReadIndex } = await setup();
const tags = new Map<string, string>();
let metadata: unknown;
const driver: AgentDriver = {
async run(input) {
await input.handleStep([
{
id: "r1",
name: "read_module",
arguments: JSON.stringify({ module_id: "aesthetics-interaction" }),
},
]);
const rejected = await input.handleStep([
{
id: "i1",
name: "insert",
arguments: JSON.stringify({
position: "设计.美学纲领与交互范式",
content: "{}",
self_score: {
dims: [
{ name: "美学纲领", score: 3 },
{ name: "交互范式", score: 5 },
{ name: "背景与规则", score: 5 },
],
},
}),
},
]);
if (rejected.kind === "continue") {
expect(JSON.parse(rejected.results[0]!.content).error).toContain(
"必须写 gap",
);
}
await input.handleStep([
{
id: "i2",
name: "insert",
arguments: JSON.stringify({
position: "设计.美学纲领与交互范式",
content: "{}",
self_score: {
dims: [
{ name: "美学纲领", score: 3, gap: "发生方式仍偏宽" },
{ name: "交互范式", score: 5 },
{ name: "背景与规则", score: 5 },
],
},
}),
},
]);
return { iterations: 3, stop: { kind: "text", content: "已写入" } };
},
};
await runDictateTurn({
llm: {} as never,
dialogue: [],
moduleCatalog: catalog,
moduleReadIndex,
driver,
handlers: {
...handlers(tags),
writeProduct: (tag, content, order, meta) => {
tags.set(tag, content);
metadata = { order, ...meta };
},
},
});
expect(metadata).toMatchObject({
layer: "final",
selfScore: {
dims: expect.arrayContaining([
expect.objectContaining({ name: "美学纲领", score: 3 }),
]),
},
});
});
});
describe("opening uses the reply contract", () => {
const contract = JSON.stringify({
schema: "context-fragment.v1",
正文: {
数据契约: {
blocks: {
body: { 类型: "markdown", 来源: "model_generated" },
forum_feed: { 类型: "list", 来源: "model_generated" },
},
},
frontend: { html: `<div id="app"></div>`, css: "", js: "" },
},
});
const scores = {
dims: [
{ name: "开场必要", score: 5 },
{ name: "前端符合", score: 5 },
{ name: "状态自洽", score: 5 },
{ name: "角色占位", score: 5 },
{ name: "收口可切换", score: 5 },
],
};
it("returns the live contract when the opening module is read", async () => {
const { catalog, moduleReadIndex } = await setup();
const tags = new Map<string, string>([["设计.正文组成", contract]]);
const driver: AgentDriver = {
async run(input) {
const outcome = await input.handleStep([
{
id: "r1",
name: "read_module",
arguments: JSON.stringify({ module_id: "opening-setup" }),
},
]);
if (outcome.kind === "continue") {
const payload = JSON.parse(outcome.results[0]!.content);
expect(payload.reply_contract.model_blocks.map((block: { id: string }) => block.id)).toEqual([
"body",
"forum_feed",
]);
}
return { iterations: 1, stop: { kind: "text", content: "已读" } };
},
};
await runDictateTurn({
llm: {} as never,
dialogue: [],
moduleCatalog: catalog,
moduleReadIndex,
driver,
handlers: handlers(tags),
});
});
it("rejects an opening that only fills the old body shell", async () => {
const { catalog, moduleReadIndex } = await setup();
const tags = new Map<string, string>([["设计.正文组成", contract]]);
const driver: AgentDriver = {
async run(input) {
await input.handleStep([
{
id: "r1",
name: "read_module",
arguments: JSON.stringify({ module_id: "opening-setup" }),
},
]);
const outcome = await input.handleStep([
{
id: "i1",
name: "insert",
arguments: JSON.stringify({
position: "设计.开场白",
content: JSON.stringify({
schema: "present.v1",
shell: "prose",
blocks: { body: "@玩家推开门。" },
}),
self_score: scores,
}),
},
]);
if (outcome.kind === "continue") {
const payload = JSON.parse(outcome.results[0]!.content);
expect(payload.error).toContain("forum_feed");
}
return { iterations: 1, stop: { kind: "text", content: "未写入" } };
},
};
const result = await runDictateTurn({
llm: {} as never,
dialogue: [],
moduleCatalog: catalog,
moduleReadIndex,
driver,
handlers: handlers(tags),
});
expect(result.wroteTags).toEqual([]);
});
});
function handlers(tags: Map<string, string>) {
return {
listProducts: () =>
[...tags.entries()]
.filter(([tag]) => tag.startsWith("设计.") || tag.startsWith("用户."))
.map(([tag, content]) => ({ tag, content })),
writeProduct: (tag: string, content: string) => tags.set(tag, content),
deleteProduct: (tag: string) => tags.delete(tag),
readTag: (tag: string) => tags.get(tag),
writeTag: (tag: string, content: string) => {
tags.set(tag, content);
},
deleteTag: (tag: string) => tags.delete(tag),
clearDialogue: () => undefined,
};
}