feat(toolbox): T6 创作工具箱通用生成器框架 — 8 新生成器 + 声明驱动落地页 + P2 收尾
通用执行路径驱动全部生成器("加生成器=加一份声明"):
- @llm: ww_agents +7 输出 schema + 7 spec(book-title/blurb/name/golden-finger/
glossary/opening/fine-outline,只声明 tier)+ build_outline_chapter_context
- @backend: ww_skills GeneratorTool 描述符 + TOOLBOX(11) + get_tool;3 通用端点
GET /skills/toolbox · POST .../skills/{tool_key}/generate(预览不写库,仅记账) ·
POST .../ingest(复用 continuity 409 + partition_writes 白名单);纯 context 派发
- @frontend: 工具箱落地页 RSC + 声明驱动 GeneratorRunner + lib/toolbox 纯函数
+ LeftNav「工具箱」+ ⌘K nav-toolbox/action-gen-*;legacy 3 跳现页
- @qa: tests/test_t6_toolbox_e2e.py 5 用例真 pg + mock 网关零 token,无端点 bug
- P2 收尾: 限流→decisions.md 记延后(单用户原型);noopener/Committable 早已修
守不变量 #2(只声明 tier)/#3(预览不写库,入库经验收 gate)/#9(缓存前缀)。无 DB 迁移。
门禁绿: 后端 ruff/format/mypy 195/alembic 无漂移/pytest 583;前端 lint/tsc/vitest 279/build。
spec 回写 PRODUCT_SPEC §7 + ARCHITECTURE §7.2 端点表。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
148
packages/core/tests/test_toolbox_generators.py
Normal file
148
packages/core/tests/test_toolbox_generators.py
Normal file
@@ -0,0 +1,148 @@
|
||||
"""T6 创作工具箱 · 7 个新生成器经 `run_generator` 的驱动测试 + 章上下文构造。
|
||||
|
||||
注入 mock 网关(按 schema 路由 / 单一 parsed),无真 LLM、无真 Postgres。验证通用执行器
|
||||
对每个 spec:透传 tier、非流式、output_schema 据 spec 绑定、system[0] 缓存、parsed 校验,
|
||||
网关失败上抛。asyncio_mode=auto。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from fakes_orchestrator import FailingRunGateway, FakeRunGateway, SchemaRoutingRunGateway
|
||||
from pydantic import BaseModel
|
||||
from ww_agents import (
|
||||
AgentSpec,
|
||||
Blurb,
|
||||
BlurbResult,
|
||||
DetailedOutlineResult,
|
||||
GlossaryResult,
|
||||
GlossaryTerm,
|
||||
GoldenFinger,
|
||||
GoldenFingerResult,
|
||||
NameListResult,
|
||||
NameSuggestion,
|
||||
Opening,
|
||||
OpeningResult,
|
||||
Scene,
|
||||
Title,
|
||||
TitleListResult,
|
||||
blurb_spec,
|
||||
book_title_spec,
|
||||
fine_outline_spec,
|
||||
glossary_spec,
|
||||
golden_finger_spec,
|
||||
name_spec,
|
||||
opening_spec,
|
||||
)
|
||||
from ww_core.orchestrator import build_outline_chapter_context, run_generator
|
||||
|
||||
PROJECT = uuid.UUID("00000000-0000-0000-0000-000000000001")
|
||||
USER = uuid.UUID("00000000-0000-0000-0000-0000000000aa")
|
||||
|
||||
# (spec, parsed 实例, schema 类型)
|
||||
_CASES: list[tuple[AgentSpec, BaseModel, type[BaseModel]]] = [
|
||||
(
|
||||
book_title_spec,
|
||||
TitleListResult(titles=[Title(title="吞噬", rationale="爽")]),
|
||||
TitleListResult,
|
||||
),
|
||||
(blurb_spec, BlurbResult(variants=[Blurb(text="文案", angle="反差")]), BlurbResult),
|
||||
(name_spec, NameListResult(names=[NameSuggestion(name="玄阴宗", kind="势力")]), NameListResult),
|
||||
(
|
||||
golden_finger_spec,
|
||||
GoldenFingerResult(
|
||||
systems=[GoldenFinger(name="瞳", mechanism="m", growth="g", limits="l")]
|
||||
),
|
||||
GoldenFingerResult,
|
||||
),
|
||||
(
|
||||
glossary_spec,
|
||||
GlossaryResult(terms=[GlossaryTerm(name="炼气", type="境界", definition="d")]),
|
||||
GlossaryResult,
|
||||
),
|
||||
(opening_spec, OpeningResult(variants=[Opening(text="雷光劈下")]), OpeningResult),
|
||||
(
|
||||
fine_outline_spec,
|
||||
DetailedOutlineResult(scenes=[Scene(idx=0, beat="b", purpose="p", conflict="c", hook="h")]),
|
||||
DetailedOutlineResult,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("spec", "parsed", "schema"), _CASES)
|
||||
async def test_run_generator_returns_each_toolbox_schema(
|
||||
spec: AgentSpec, parsed: BaseModel, schema: type[BaseModel]
|
||||
) -> None:
|
||||
gateway = FakeRunGateway(parsed)
|
||||
|
||||
result = await run_generator(
|
||||
spec, context="x", gateway=gateway, user_id=USER, project_id=PROJECT
|
||||
)
|
||||
|
||||
assert isinstance(result, schema)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("spec", "parsed", "schema"), _CASES)
|
||||
async def test_run_generator_builds_request_per_spec(
|
||||
spec: AgentSpec, parsed: BaseModel, schema: type[BaseModel]
|
||||
) -> None:
|
||||
# 不变量 #2/#9:透传 spec.tier、非流式、output_schema 据 spec 绑定、system[0] 缓存
|
||||
gateway = FakeRunGateway(parsed)
|
||||
|
||||
await run_generator(spec, context="x", gateway=gateway, user_id=USER, project_id=PROJECT)
|
||||
|
||||
req = gateway.last_request
|
||||
assert req is not None
|
||||
assert req.tier == spec.tier
|
||||
assert req.stream is False
|
||||
assert req.output_schema is schema
|
||||
assert req.system[0].cache is True
|
||||
|
||||
|
||||
async def test_run_generator_routes_by_schema_across_toolbox() -> None:
|
||||
# 一个按 schema 路由的网关同时服务 7 个生成器,各拿自己 schema 的 parsed
|
||||
by_schema = {schema: parsed for _, parsed, schema in _CASES}
|
||||
gateway = SchemaRoutingRunGateway(by_schema)
|
||||
|
||||
for spec, _parsed, schema in _CASES:
|
||||
result = await run_generator(
|
||||
spec, context="x", gateway=gateway, user_id=USER, project_id=PROJECT
|
||||
)
|
||||
assert isinstance(result, schema)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("spec", [row[0] for row in _CASES])
|
||||
async def test_run_generator_raises_on_gateway_failure(spec: AgentSpec) -> None:
|
||||
gateway = FailingRunGateway(RuntimeError("boom"))
|
||||
|
||||
with pytest.raises(RuntimeError, match="boom"):
|
||||
await run_generator(spec, context="x", gateway=gateway, user_id=USER, project_id=PROJECT)
|
||||
|
||||
|
||||
# ---- build_outline_chapter_context:确定性、含设定/章号/节拍、空节拍降级 ----
|
||||
|
||||
|
||||
def test_build_outline_chapter_context_is_deterministic_and_shaped() -> None:
|
||||
a = build_outline_chapter_context(
|
||||
chapter_no=3, beats=["主角觉醒", "强敌登场"], project_context="题材:玄幻"
|
||||
)
|
||||
b = build_outline_chapter_context(
|
||||
chapter_no=3, beats=["主角觉醒", "强敌登场"], project_context="题材:玄幻"
|
||||
)
|
||||
assert a == b
|
||||
assert "题材:玄幻" in a
|
||||
assert "第 3 章" in a
|
||||
assert "主角觉醒" in a
|
||||
assert "强敌登场" in a
|
||||
|
||||
|
||||
def test_build_outline_chapter_context_empty_beats_falls_back() -> None:
|
||||
ctx = build_outline_chapter_context(chapter_no=1, beats=[], project_context="题材:都市")
|
||||
assert "本章暂无大纲节拍" in ctx
|
||||
|
||||
|
||||
def test_build_outline_chapter_context_empty_project_falls_back() -> None:
|
||||
ctx = build_outline_chapter_context(chapter_no=2, beats=["x"], project_context="")
|
||||
assert "暂无设定" in ctx
|
||||
@@ -24,6 +24,7 @@ from .collect import (
|
||||
from .generation_node import (
|
||||
build_brief_context,
|
||||
build_character_gen_context,
|
||||
build_outline_chapter_context,
|
||||
build_precheck_context,
|
||||
build_worldbuilder_context,
|
||||
precheck_generated_cards,
|
||||
@@ -106,6 +107,7 @@ __all__ = [
|
||||
"SseEvent",
|
||||
"build_brief_context",
|
||||
"build_character_gen_context",
|
||||
"build_outline_chapter_context",
|
||||
"build_outline_request",
|
||||
"build_precheck_context",
|
||||
"build_review_context",
|
||||
|
||||
@@ -78,6 +78,24 @@ def build_brief_context(*, brief: str, project_context: str) -> str:
|
||||
return f"## 作品设定\n{project_context or '(暂无设定)'}\n\n## 创作需求\n{brief_block}"
|
||||
|
||||
|
||||
def build_outline_chapter_context(
|
||||
*, chapter_no: int, beats: Sequence[str], project_context: str
|
||||
) -> str:
|
||||
"""通用「with_outline_chapter」上下文:作品设定 + 指定章的大纲节拍(确定性,无时间戳)。
|
||||
|
||||
覆盖按章展开的生成器(开篇/细纲…):注入作品设定 + 该章已排定的粗节拍,供模型
|
||||
据节拍展开。无节拍时给降级占位,交由 system_prompt 处理。
|
||||
"""
|
||||
if beats:
|
||||
beats_block = "\n".join(f"{i + 1}. {beat}" for i, beat in enumerate(beats))
|
||||
else:
|
||||
beats_block = "(本章暂无大纲节拍)"
|
||||
return (
|
||||
f"## 作品设定\n{project_context or '(暂无设定)'}\n\n"
|
||||
f"## 第 {chapter_no} 章大纲节拍\n{beats_block}"
|
||||
)
|
||||
|
||||
|
||||
async def run_generator(
|
||||
spec: AgentSpec,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user