loaded-skills.test.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // ============================================================
  2. // 「历史里加载过某个技能吗」(agent/loaded-skills.ts)
  3. // ============================================================
  4. // 技能门的探针读的是 provider 可见历史:成功的 Skill 调用才算,只发出没结果、结果出错、别的技能
  5. // 都不算;compaction 换掉历史后答案随之变回否。
  6. import { describe, expect, it } from "vitest";
  7. import { sessionHasLoadedSkill } from "../src/agent/loaded-skills.js";
  8. import { MessageHistoryImpl } from "../src/agent/message-history.js";
  9. const SKILL = "dynamic-workflows";
  10. function historyWith(steps: (history: MessageHistoryImpl) => void): MessageHistoryImpl {
  11. const history = new MessageHistoryImpl();
  12. history.init("system");
  13. history.addUser("/workflow ship it");
  14. steps(history);
  15. return history;
  16. }
  17. describe("sessionHasLoadedSkill", () => {
  18. it("is true after a completed Skill call for that skill", () => {
  19. const history = historyWith((h) => {
  20. h.addAssistant("", [{ id: "call_1", name: "Skill", input: { skill: SKILL } }]);
  21. h.addToolResult(
  22. "call_1",
  23. "Skill",
  24. '<skill_content name="dynamic-workflows">…</skill_content>',
  25. true,
  26. );
  27. });
  28. expect(sessionHasLoadedSkill(history.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(true);
  29. });
  30. it("accepts the legacy { name } input shape", () => {
  31. const history = historyWith((h) => {
  32. h.addAssistant("", [{ id: "call_1", name: "Skill", input: { name: SKILL } }]);
  33. h.addToolResult("call_1", "Skill", "ok", true);
  34. });
  35. expect(sessionHasLoadedSkill(history.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(true);
  36. });
  37. it("is false while the call has no result, when the result is an error, or for another skill", () => {
  38. const pending = historyWith((h) => {
  39. h.addAssistant("", [{ id: "call_1", name: "Skill", input: { skill: SKILL } }]);
  40. });
  41. expect(sessionHasLoadedSkill(pending.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(false);
  42. const failed = historyWith((h) => {
  43. h.addAssistant("", [{ id: "call_1", name: "Skill", input: { skill: SKILL } }]);
  44. h.addToolResult("call_1", "Skill", "Skill not found", false);
  45. });
  46. expect(sessionHasLoadedSkill(failed.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(false);
  47. const other = historyWith((h) => {
  48. h.addAssistant("", [
  49. { id: "call_1", name: "Skill", input: { skill: "zcode-configuration-guide" } },
  50. ]);
  51. h.addToolResult("call_1", "Skill", "ok", true);
  52. h.addAssistant("", [{ id: "call_2", name: "Read", input: { file_path: "SKILL.md" } }]);
  53. h.addToolResult("call_2", "Read", "…", true);
  54. });
  55. expect(sessionHasLoadedSkill(other.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(false);
  56. });
  57. it("forgets the load once compaction replaces the history", () => {
  58. const history = historyWith((h) => {
  59. h.addAssistant("", [{ id: "call_1", name: "Skill", input: { skill: SKILL } }]);
  60. h.addToolResult("call_1", "Skill", "ok", true);
  61. });
  62. expect(sessionHasLoadedSkill(history.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(true);
  63. // compact-active 用摘要替换 provider 可见历史;技能正文随之离场,门要重新关上。
  64. history.replaceMessages([
  65. { role: "system", content: "system" },
  66. { role: "user", content: "[summary] the user asked for a workflow" },
  67. ]);
  68. expect(sessionHasLoadedSkill(history.borrowReadOnlyRuntimeEntries(), SKILL)).toBe(false);
  69. });
  70. });