build-context-pack-plugin.spec.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { describe, expect, it, vi } from "vitest"
  2. import { createBuildContextPackPlugin } from "./build-context-pack-plugin"
  3. import { createSoulDialogPlugin } from "./soul-dialog-plugin"
  4. import { createPrePluginChain } from "../pipeline"
  5. import type { ContextPack } from "@/lib/novel/context-engine"
  6. const mockContextPack: ContextPack = {
  7. task: "写第5章",
  8. chapterGoal: "第5章目标",
  9. outline: "大纲内容",
  10. recentSummaries: ["第4章摘要"],
  11. previousChapterEnding: "上一章结尾",
  12. characterStates: "人物状态",
  13. soulDoc: "灵魂文档",
  14. characterAuras: "",
  15. storyFrameworkBinding: "",
  16. cognitionStates: "认知状态",
  17. foreshadowingStates: "伏笔状态",
  18. timeline: "时间线",
  19. relatedSettings: "相关设定",
  20. canonRules: "正史规则",
  21. writingStyle: "写作风格",
  22. searchResults: "搜索结果",
  23. graphSearchResults: "图谱搜索结果",
  24. mustDo: "必须做",
  25. mustAvoid: "必须避免",
  26. nextChapterAdvice: "下一章建议",
  27. revisionDirectives: "修订指令",
  28. }
  29. describe("BuildContextPackPlugin", () => {
  30. it("builds context pack in novel mode with task route", async () => {
  31. const mockBuild = vi.fn().mockResolvedValue(mockContextPack)
  32. const plugin = createBuildContextPackPlugin({ buildContextPack: mockBuild })
  33. const result = await plugin.run({
  34. userMessage: "写第5章",
  35. projectPath: "/test-project",
  36. agentConfig: {} as any,
  37. novelMode: true,
  38. taskRoute: { intent: "write_chapter", confidence: 0.9, chapterNumber: 5, extractedParams: {} },
  39. })
  40. expect(result.contextPack).toBeDefined()
  41. expect(result.contextPack?.task).toBe("写第5章")
  42. expect(mockBuild).toHaveBeenCalledWith("/test-project", "写第5章", 5)
  43. })
  44. it("uses effectiveTaskRoute if available", async () => {
  45. const mockBuild = vi.fn().mockResolvedValue(mockContextPack)
  46. const plugin = createBuildContextPackPlugin({ buildContextPack: mockBuild })
  47. await plugin.run({
  48. userMessage: "写章节",
  49. projectPath: "/test-project",
  50. agentConfig: {} as any,
  51. novelMode: true,
  52. taskRoute: { intent: "write_chapter", confidence: 0.9, extractedParams: {} },
  53. effectiveTaskRoute: { intent: "write_chapter", confidence: 0.9, chapterNumber: 7, extractedParams: { chapterNumber: "7" } },
  54. })
  55. expect(mockBuild).toHaveBeenCalledWith("/test-project", "写章节", 7)
  56. })
  57. it("returns empty when not in novel mode", async () => {
  58. const mockBuild = vi.fn().mockResolvedValue(mockContextPack)
  59. const plugin = createBuildContextPackPlugin({ buildContextPack: mockBuild })
  60. const result = await plugin.run({
  61. userMessage: "写第5章",
  62. projectPath: "/test-project",
  63. agentConfig: {} as any,
  64. novelMode: false,
  65. })
  66. expect(result.contextPack).toBeUndefined()
  67. expect(mockBuild).not.toHaveBeenCalled()
  68. })
  69. it("returns empty when no task route", async () => {
  70. const mockBuild = vi.fn().mockResolvedValue(mockContextPack)
  71. const plugin = createBuildContextPackPlugin({ buildContextPack: mockBuild })
  72. const result = await plugin.run({
  73. userMessage: "随便聊聊",
  74. projectPath: "/test-project",
  75. agentConfig: {} as any,
  76. novelMode: true,
  77. taskRoute: null,
  78. })
  79. expect(result.contextPack).toBeUndefined()
  80. expect(mockBuild).not.toHaveBeenCalled()
  81. })
  82. it("handles build error gracefully", async () => {
  83. const mockError = vi.fn()
  84. const mockBuild = vi.fn().mockRejectedValue(new Error("build failed"))
  85. const plugin = createBuildContextPackPlugin({ buildContextPack: mockBuild, onError: mockError })
  86. const result = await plugin.run({
  87. userMessage: "写第5章",
  88. projectPath: "/test-project",
  89. agentConfig: {} as any,
  90. novelMode: true,
  91. taskRoute: { intent: "write_chapter", confidence: 0.9, chapterNumber: 5, extractedParams: {} },
  92. })
  93. expect(result.contextPack).toBeUndefined()
  94. expect(mockError).toHaveBeenCalled()
  95. })
  96. it("loads classification before context and passes allowed categories to build", async () => {
  97. const mockBuild = vi.fn().mockResolvedValue(mockContextPack)
  98. const mockLoadClassification = vi.fn().mockResolvedValue({
  99. config: {
  100. routes: [
  101. {
  102. intent: "generate_outline",
  103. required: ["soul", "settings"],
  104. optional: ["outline"],
  105. forbidden: ["chapter_content", "recent_summaries"],
  106. },
  107. ],
  108. },
  109. source: "project",
  110. })
  111. const mockApplyRouteRules = vi.fn().mockReturnValue({
  112. pack: mockContextPack,
  113. blockedSources: ["chapter_content", "recent_summaries"],
  114. keptSources: ["soul", "settings", "outline"],
  115. })
  116. const plugin = createBuildContextPackPlugin({
  117. buildContextPack: mockBuild,
  118. enableClassification: true,
  119. loadClassificationConfig: mockLoadClassification as any,
  120. applyRouteRules: mockApplyRouteRules as any,
  121. })
  122. const result = await plugin.run({
  123. userMessage: "生成大纲",
  124. projectPath: "/test-project",
  125. agentConfig: {} as any,
  126. novelMode: true,
  127. taskRoute: { intent: "generate_outline", confidence: 0.9, extractedParams: {} },
  128. })
  129. expect(mockLoadClassification).toHaveBeenCalledWith("/test-project", undefined)
  130. expect(mockBuild).toHaveBeenCalledWith("/test-project", "生成大纲", undefined, {
  131. categories: ["soul", "settings", "outline"],
  132. })
  133. expect(result.routeSource).toBe("project")
  134. expect(result.blockedSources).toEqual(["chapter_content", "recent_summaries"])
  135. })
  136. })
  137. describe("SoulDialogPlugin", () => {
  138. it("stops chain when characterAuras present", async () => {
  139. const plugin = createSoulDialogPlugin()
  140. const result = await plugin.run({
  141. userMessage: "写第5章",
  142. projectPath: "/test-project",
  143. agentConfig: {} as any,
  144. novelMode: true,
  145. contextPack: { ...mockContextPack, characterAuras: "角色光环内容" },
  146. })
  147. expect(result.shouldStop).toBe(true)
  148. expect(result.stopReason).toBe("soul_dialog_confirmation_required")
  149. })
  150. it("does not stop when characterAuras empty", async () => {
  151. const plugin = createSoulDialogPlugin()
  152. const result = await plugin.run({
  153. userMessage: "写第5章",
  154. projectPath: "/test-project",
  155. agentConfig: {} as any,
  156. novelMode: true,
  157. contextPack: { ...mockContextPack, characterAuras: "" },
  158. })
  159. expect(result.shouldStop).toBeUndefined()
  160. })
  161. it("does nothing when not in novel mode", async () => {
  162. const plugin = createSoulDialogPlugin()
  163. const result = await plugin.run({
  164. userMessage: "写第5章",
  165. projectPath: "/test-project",
  166. agentConfig: {} as any,
  167. novelMode: false,
  168. contextPack: { ...mockContextPack, characterAuras: "角色光环内容" },
  169. })
  170. expect(result.shouldStop).toBeUndefined()
  171. })
  172. it("does nothing when no context pack", async () => {
  173. const plugin = createSoulDialogPlugin()
  174. const result = await plugin.run({
  175. userMessage: "写第5章",
  176. projectPath: "/test-project",
  177. agentConfig: {} as any,
  178. novelMode: true,
  179. })
  180. expect(result.shouldStop).toBeUndefined()
  181. })
  182. it("supports custom shouldRequest function", async () => {
  183. const customShould = vi.fn().mockReturnValue(true)
  184. const plugin = createSoulDialogPlugin({ shouldRequestSoulDialog: customShould })
  185. const result = await plugin.run({
  186. userMessage: "写第5章",
  187. projectPath: "/test-project",
  188. agentConfig: {} as any,
  189. novelMode: true,
  190. contextPack: mockContextPack,
  191. })
  192. expect(customShould).toHaveBeenCalledWith(mockContextPack)
  193. expect(result.shouldStop).toBe(true)
  194. })
  195. })
  196. describe("Task3: Plugin Chain Integration", () => {
  197. it("chains build_context_pack -> soul_dialog correctly", async () => {
  198. const mockBuild = vi.fn().mockResolvedValue({
  199. ...mockContextPack,
  200. characterAuras: "需要确认的角色光环",
  201. storyFrameworkBinding: "",
  202. })
  203. const chain = createPrePluginChain([
  204. createBuildContextPackPlugin({ buildContextPack: mockBuild }),
  205. createSoulDialogPlugin(),
  206. ])
  207. const result = await chain.run({
  208. userMessage: "写第5章",
  209. projectPath: "/test-project",
  210. agentConfig: {} as any,
  211. novelMode: true,
  212. taskRoute: { intent: "write_chapter", confidence: 0.9, chapterNumber: 5, extractedParams: {} },
  213. })
  214. expect(result.contextPack).toBeDefined()
  215. expect(result.shouldStop).toBe(true)
  216. expect(result.stopReason).toBe("soul_dialog_confirmation_required")
  217. expect(result.errors).toHaveLength(0)
  218. })
  219. it("continues chain when soul dialog not needed", async () => {
  220. const mockBuild = vi.fn().mockResolvedValue({
  221. ...mockContextPack,
  222. characterAuras: "",
  223. storyFrameworkBinding: "",
  224. })
  225. const chain = createPrePluginChain([
  226. createBuildContextPackPlugin({ buildContextPack: mockBuild }),
  227. createSoulDialogPlugin(),
  228. ])
  229. const result = await chain.run({
  230. userMessage: "写第5章",
  231. projectPath: "/test-project",
  232. agentConfig: {} as any,
  233. novelMode: true,
  234. taskRoute: { intent: "write_chapter", confidence: 0.9, chapterNumber: 5, extractedParams: {} },
  235. })
  236. expect(result.contextPack).toBeDefined()
  237. expect(result.shouldStop).toBeUndefined()
  238. expect(result.errors).toHaveLength(0)
  239. })
  240. })