skill-tool.test.ts 8.3 KB

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