skill-tool.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { describe, expect, it } from "vitest";
  2. import { createSessionId, createTraceId, DYNAMIC_WORKFLOW_SKILL_NAME } from "@zcode/contracts";
  3. import type { SkillContent, SkillLoadOutcome, SkillLoadRequest, SkillPort } from "@zcode/contracts";
  4. import { createContextBuilder } from "../src/context/index.js";
  5. import { registerBuiltInTools } from "../src/tool/handlers/index.js";
  6. import {
  7. MAX_SKILL_BYTES,
  8. SKILL_RESULT_WRAPPER_BYTES,
  9. skillHandler,
  10. skillToolEntry,
  11. WORKFLOW_SKILL_MAX_BYTES,
  12. } from "../src/tool/handlers/skill.js";
  13. import { createToolRegistry } from "../src/tool/registry.js";
  14. import type { ToolExecutionContext } from "../src/tool/types.js";
  15. const skillOutcome: SkillLoadOutcome = {
  16. skills: [
  17. {
  18. name: "demo-skill",
  19. description: "Use this for demo workflows",
  20. path: "/workspace/.zcode/skills/demo-skill/SKILL.md",
  21. directory: "/workspace/.zcode/skills/demo-skill",
  22. rootPath: "/workspace/.zcode/skills",
  23. scope: "project",
  24. source: "zcode",
  25. safeToAutoLoad: true,
  26. frontmatterKeys: ["name", "description"],
  27. policy: { allowImplicitInvocation: true },
  28. },
  29. ],
  30. diagnostics: [],
  31. totalDiscovered: 1,
  32. };
  33. describe("Skill integration", () => {
  34. it("renders available skills in context without loading body", () => {
  35. const builder = createContextBuilder({
  36. workingDirectory: "/workspace",
  37. envInfo: {
  38. cwd: "/workspace",
  39. platform: "test",
  40. shell: "test",
  41. osVersion: "test",
  42. nodeVersion: "test",
  43. },
  44. skills: skillOutcome,
  45. });
  46. const result = builder.build();
  47. const system = result.systemMessages.find((message) => message.role === "system")?.content ?? "";
  48. const skillReminder =
  49. result.metaUserAttachments.find((attachment) => attachment.source === "skills_listing")
  50. ?.content ?? "";
  51. expect(system).not.toContain("demo-skill");
  52. expect(skillReminder).toContain("The following skills are available");
  53. expect(skillReminder).toContain("demo-skill");
  54. expect(skillReminder).not.toContain("Follow this workflow");
  55. });
  56. it("loads skill content through SkillPort using the legacy name field", async () => {
  57. const loaded = await skillHandler(
  58. { name: "demo-skill" },
  59. contextWithSkillPort({
  60. async discoverSkills(): Promise<SkillLoadOutcome> {
  61. return skillOutcome;
  62. },
  63. async loadSkill(): Promise<SkillContent> {
  64. return {
  65. metadata: skillOutcome.skills[0]!,
  66. content: "# Demo Skill\n\nFollow this workflow.",
  67. baseDirectory: "/workspace/.zcode/skills/demo-skill",
  68. bytesRead: 37,
  69. sizeBytes: 37,
  70. truncated: false,
  71. };
  72. },
  73. }),
  74. );
  75. expect(loaded).toContain('<skill_content name="demo-skill">');
  76. expect(loaded).toContain("Follow this workflow.");
  77. expect(loaded).toContain("Base directory for this skill");
  78. });
  79. it("loads skill content through SkillPort using the provider-visible skill field", async () => {
  80. const loaded = await skillHandler(
  81. { skill: "demo-skill" },
  82. contextWithSkillPort({
  83. async discoverSkills(): Promise<SkillLoadOutcome> {
  84. return skillOutcome;
  85. },
  86. async loadSkill(): Promise<SkillContent> {
  87. return {
  88. metadata: skillOutcome.skills[0]!,
  89. content:
  90. "# Demo Skill\n\nRead ${CLAUDE_SKILL_DIR}/references.md and ${ZCODE_SKILL_DIR}/notes.md.",
  91. baseDirectory: "/workspace/.zcode/skills/demo-skill",
  92. bytesRead: 91,
  93. sizeBytes: 91,
  94. truncated: false,
  95. };
  96. },
  97. }),
  98. );
  99. expect(loaded).toContain('<skill_content name="demo-skill">');
  100. expect(loaded).toContain("/workspace/.zcode/skills/demo-skill/references.md");
  101. expect(loaded).toContain("/workspace/.zcode/skills/demo-skill/notes.md");
  102. });
  103. it("reports resolved skill metadata through the telemetry callback without changing content", async () => {
  104. const metadata = {
  105. ...skillOutcome.skills[0]!,
  106. pluginName: "document-skills",
  107. pluginId: "document-skills@zcode-plugins-official",
  108. qualifiedName: "document-skills:pptx",
  109. source: "plugin" as const,
  110. };
  111. const reported: unknown[] = [];
  112. const loaded = await skillHandler(
  113. { skill: "document-skills:pptx" },
  114. contextWithSkillPort(
  115. {
  116. async discoverSkills(): Promise<SkillLoadOutcome> {
  117. return { ...skillOutcome, skills: [metadata] };
  118. },
  119. async loadSkill(): Promise<SkillContent> {
  120. return {
  121. metadata,
  122. content: "# PPTX Skill\n\nDescription stays in the skill body.",
  123. baseDirectory: "/plugins/document-skills/skills/pptx",
  124. bytesRead: 50,
  125. sizeBytes: 50,
  126. truncated: false,
  127. };
  128. },
  129. },
  130. (value) => reported.push(value),
  131. ),
  132. );
  133. expect(reported).toEqual([
  134. {
  135. qualifiedName: "document-skills:pptx",
  136. pluginId: "document-skills@zcode-plugins-official",
  137. source: "plugin",
  138. },
  139. ]);
  140. expect(loaded).toContain("Description stays in the skill body.");
  141. });
  142. // docs/dynamic-workflow/authoring.md「The skill」:只有工作流技能拿 200 000 字节的上限,其余技能仍是 100 000。
  143. // 判据是请求里的名字,与技能门认的同一个。
  144. it("loads dynamic-workflows with its own byte cap and every other skill with the default", async () => {
  145. const requested: SkillLoadRequest[] = [];
  146. const port: SkillPort = {
  147. async discoverSkills(): Promise<SkillLoadOutcome> {
  148. return skillOutcome;
  149. },
  150. async loadSkill(request): Promise<SkillContent> {
  151. requested.push(request);
  152. return {
  153. metadata: { ...skillOutcome.skills[0]!, name: request.name },
  154. content: "body",
  155. baseDirectory: "/workspace/.zcode/skills/demo-skill",
  156. bytesRead: 4,
  157. sizeBytes: 4,
  158. truncated: false,
  159. };
  160. },
  161. };
  162. await skillHandler({ skill: DYNAMIC_WORKFLOW_SKILL_NAME }, contextWithSkillPort(port));
  163. await skillHandler({ skill: "demo-skill" }, contextWithSkillPort(port));
  164. expect(WORKFLOW_SKILL_MAX_BYTES).toBe(200_000);
  165. expect(MAX_SKILL_BYTES).toBe(100_000);
  166. expect(requested.map((request) => request.maxBytes)).toEqual([
  167. WORKFLOW_SKILL_MAX_BYTES,
  168. MAX_SKILL_BYTES,
  169. ]);
  170. });
  171. // 结果预算是工具级的静态值:必须装得下最大上限的正文再加外壳,否则贴着上限的技能会被从尾部再截一刀。
  172. it("gives the tool result room for the largest skill plus its wrapper", () => {
  173. const ceiling = WORKFLOW_SKILL_MAX_BYTES + SKILL_RESULT_WRAPPER_BYTES;
  174. expect(skillToolEntry.metadata.maxOutputBytes).toBeGreaterThanOrEqual(ceiling);
  175. expect(skillToolEntry.resultBudget.maxInlineBytes).toBeGreaterThanOrEqual(ceiling);
  176. expect(skillToolEntry.resultBudget.maxModelBytes).toBeGreaterThanOrEqual(ceiling);
  177. });
  178. it("can omit the Skill tool when the runtime has no SkillPort", () => {
  179. const registry = createToolRegistry();
  180. registerBuiltInTools(registry, { includeSkill: false });
  181. expect(registry.has("Read")).toBe(true);
  182. expect(registry.has("Skill")).toBe(false);
  183. });
  184. });
  185. function contextWithSkillPort(
  186. skillPort: SkillPort,
  187. recordSkillTelemetryMetadata?: ToolExecutionContext["recordSkillTelemetryMetadata"],
  188. ): ToolExecutionContext {
  189. return {
  190. toolCallId: "tool-skill",
  191. traceId: createTraceId(),
  192. abortSignal: new AbortController().signal,
  193. skillPort,
  194. ...(recordSkillTelemetryMetadata ? { recordSkillTelemetryMetadata } : {}),
  195. workingDirectory: "/workspace",
  196. workspaceRoot: "/workspace",
  197. sessionId: createSessionId("skill-test"),
  198. };
  199. }