@oyasmi/pipiclaw 0.9.1-beta.2 → 0.9.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/dist/agent/channel-runner.d.ts +9 -1
  2. package/dist/agent/channel-runner.js +51 -18
  3. package/dist/agent/command-extension.js +24 -27
  4. package/dist/agent/commands.d.ts +51 -7
  5. package/dist/agent/commands.js +199 -59
  6. package/dist/agent/effect-ledger.js +9 -2
  7. package/dist/agent/job-manager.d.ts +4 -0
  8. package/dist/agent/job-manager.js +17 -2
  9. package/dist/agent/prompt/manifest.d.ts +1 -1
  10. package/dist/agent/prompt/manifest.js +6 -7
  11. package/dist/agent/prompt/sections.js +9 -5
  12. package/dist/agent/reply-limits.d.ts +16 -0
  13. package/dist/agent/reply-limits.js +17 -0
  14. package/dist/agent/session-events.d.ts +2 -0
  15. package/dist/agent/session-events.js +29 -14
  16. package/dist/agent/status-render.js +12 -12
  17. package/dist/agent/turn-recovery.js +2 -2
  18. package/dist/agent/type-guards.js +5 -1
  19. package/dist/agent/workspace-resources.d.ts +2 -2
  20. package/dist/agent/workspace-resources.js +41 -3
  21. package/dist/executor.d.ts +28 -0
  22. package/dist/executor.js +120 -18
  23. package/dist/file-store.d.ts +96 -0
  24. package/dist/file-store.js +189 -0
  25. package/dist/main.js +3 -4
  26. package/dist/memory/commands.js +79 -38
  27. package/dist/memory/consolidation.d.ts +5 -1
  28. package/dist/memory/consolidation.js +35 -38
  29. package/dist/memory/extraction.d.ts +8 -1
  30. package/dist/memory/extraction.js +47 -4
  31. package/dist/memory/files.d.ts +0 -1
  32. package/dist/memory/files.js +31 -12
  33. package/dist/memory/lifecycle.js +9 -2
  34. package/dist/memory/maintenance-jobs.js +16 -38
  35. package/dist/memory/maintenance-state.js +2 -1
  36. package/dist/memory/metadata.d.ts +3 -0
  37. package/dist/memory/metadata.js +7 -0
  38. package/dist/memory/recall.d.ts +19 -0
  39. package/dist/memory/recall.js +116 -24
  40. package/dist/memory/review-log.d.ts +0 -2
  41. package/dist/memory/review-log.js +0 -0
  42. package/dist/memory/session-corpus.js +5 -0
  43. package/dist/memory/session-search.js +15 -22
  44. package/dist/memory/sidecar-worker.js +4 -0
  45. package/dist/memory/source-window.d.ts +0 -1
  46. package/dist/memory/source-window.js +0 -5
  47. package/dist/memory/transcript.d.ts +10 -0
  48. package/dist/memory/transcript.js +39 -0
  49. package/dist/playbooks/agent-delegation.md +34 -25
  50. package/dist/playbooks/background-jobs.md +8 -16
  51. package/dist/playbooks/catalog.d.ts +1 -1
  52. package/dist/playbooks/catalog.js +1 -1
  53. package/dist/playbooks/event-scheduling.md +15 -15
  54. package/dist/playbooks/memory-and-learning.md +30 -23
  55. package/dist/playbooks/outbound-media.md +15 -8
  56. package/dist/playbooks/runtime-orientation.md +40 -37
  57. package/dist/playbooks/task-driving.md +36 -38
  58. package/dist/playbooks/task-planning.md +21 -21
  59. package/dist/runtime/app-home.d.ts +6 -0
  60. package/dist/runtime/app-home.js +16 -7
  61. package/dist/runtime/bootstrap.js +54 -13
  62. package/dist/runtime/channel-context.d.ts +2 -1
  63. package/dist/runtime/delivery.js +3 -3
  64. package/dist/runtime/dingtalk.d.ts +9 -1
  65. package/dist/runtime/dingtalk.js +30 -15
  66. package/dist/runtime/durable-dispatch.d.ts +26 -1
  67. package/dist/runtime/durable-dispatch.js +51 -5
  68. package/dist/runtime/event-commands.d.ts +15 -0
  69. package/dist/runtime/event-commands.js +55 -56
  70. package/dist/runtime/events.d.ts +4 -2
  71. package/dist/runtime/events.js +15 -20
  72. package/dist/runtime/project-commands.d.ts +16 -0
  73. package/dist/runtime/project-commands.js +13 -17
  74. package/dist/runtime/subagent-commands.d.ts +20 -0
  75. package/dist/runtime/subagent-commands.js +61 -58
  76. package/dist/runtime/task-commands.d.ts +37 -0
  77. package/dist/runtime/task-commands.js +108 -66
  78. package/dist/runtime/task-driver.js +2 -2
  79. package/dist/security/block-message.d.ts +11 -0
  80. package/dist/security/block-message.js +12 -0
  81. package/dist/security/path-guard-check.d.ts +13 -0
  82. package/dist/security/path-guard-check.js +39 -0
  83. package/dist/security/path-guard.js +61 -12
  84. package/dist/security/types.d.ts +9 -0
  85. package/dist/settings.js +3 -2
  86. package/dist/shared/atomic-file.d.ts +17 -1
  87. package/dist/shared/atomic-file.js +50 -3
  88. package/dist/shared/duration.d.ts +2 -0
  89. package/dist/shared/duration.js +7 -0
  90. package/dist/shared/safe-id.d.ts +19 -0
  91. package/dist/shared/safe-id.js +24 -0
  92. package/dist/shared/text-utils.d.ts +1 -1
  93. package/dist/shared/text-utils.js +3 -8
  94. package/dist/shared/type-guards.d.ts +2 -0
  95. package/dist/shared/type-guards.js +4 -0
  96. package/dist/subagents/discovery.d.ts +51 -19
  97. package/dist/subagents/discovery.js +93 -98
  98. package/dist/subagents/external/claude-code.js +40 -2
  99. package/dist/subagents/external/harness.d.ts +1 -1
  100. package/dist/subagents/external/run.js +61 -8
  101. package/dist/subagents/format.d.ts +3 -2
  102. package/dist/subagents/format.js +3 -7
  103. package/dist/subagents/runs.d.ts +1 -1
  104. package/dist/subagents/runs.js +10 -9
  105. package/dist/subagents/tool.d.ts +31 -18
  106. package/dist/subagents/tool.js +592 -562
  107. package/dist/subagents/workspace-lease.js +1 -1
  108. package/dist/tasks/control.js +7 -9
  109. package/dist/tasks/ledger.d.ts +2 -2
  110. package/dist/tasks/ledger.js +3 -7
  111. package/dist/tasks/transitions.d.ts +5 -1
  112. package/dist/tasks/transitions.js +2 -2
  113. package/dist/tasks/verification.d.ts +7 -5
  114. package/dist/tools/bash.d.ts +1 -2
  115. package/dist/tools/bash.js +99 -76
  116. package/dist/tools/config.d.ts +6 -1
  117. package/dist/tools/config.js +9 -0
  118. package/dist/tools/edit.d.ts +2 -3
  119. package/dist/tools/edit.js +276 -75
  120. package/dist/tools/event-manage.d.ts +13 -7
  121. package/dist/tools/event-manage.js +52 -14
  122. package/dist/tools/glob-match.d.ts +11 -0
  123. package/dist/tools/glob-match.js +75 -0
  124. package/dist/tools/glob.d.ts +15 -0
  125. package/dist/tools/glob.js +113 -0
  126. package/dist/tools/grep.d.ts +0 -1
  127. package/dist/tools/grep.js +60 -44
  128. package/dist/tools/ignore-dirs.d.ts +6 -0
  129. package/dist/tools/ignore-dirs.js +15 -0
  130. package/dist/tools/index.d.ts +3 -0
  131. package/dist/tools/index.js +54 -40
  132. package/dist/tools/job.d.ts +0 -1
  133. package/dist/tools/job.js +3 -9
  134. package/dist/tools/line-index.d.ts +16 -0
  135. package/dist/tools/line-index.js +76 -0
  136. package/dist/tools/memory-manage.d.ts +21 -8
  137. package/dist/tools/memory-manage.js +114 -103
  138. package/dist/tools/presentation.d.ts +2 -0
  139. package/dist/tools/presentation.js +109 -0
  140. package/dist/tools/read.d.ts +2 -2
  141. package/dist/tools/read.js +100 -120
  142. package/dist/tools/registry.d.ts +4 -1
  143. package/dist/tools/registry.js +89 -22
  144. package/dist/tools/send-media.d.ts +6 -6
  145. package/dist/tools/send-media.js +21 -43
  146. package/dist/tools/session-search.d.ts +0 -1
  147. package/dist/tools/session-search.js +0 -1
  148. package/dist/tools/skill-security.d.ts +0 -2
  149. package/dist/tools/skill-security.js +1 -25
  150. package/dist/tools/skill.d.ts +31 -0
  151. package/dist/tools/skill.js +153 -0
  152. package/dist/tools/subagent-manage.d.ts +6 -5
  153. package/dist/tools/subagent-manage.js +47 -47
  154. package/dist/tools/task-manage/create.d.ts +2 -2
  155. package/dist/tools/task-manage/create.js +1 -3
  156. package/dist/tools/task-manage/lifecycle.d.ts +9 -6
  157. package/dist/tools/task-manage/lifecycle.js +49 -56
  158. package/dist/tools/task-manage/schema.d.ts +27 -15
  159. package/dist/tools/task-manage/schema.js +58 -74
  160. package/dist/tools/task-manage/shared.d.ts +11 -6
  161. package/dist/tools/task-manage/shared.js +30 -21
  162. package/dist/tools/task-manage/types.d.ts +14 -15
  163. package/dist/tools/task-manage/verification.d.ts +4 -4
  164. package/dist/tools/task-manage/verification.js +6 -8
  165. package/dist/tools/task-manage.d.ts +8 -11
  166. package/dist/tools/task-manage.js +63 -50
  167. package/dist/tools/tool-details.d.ts +17 -1
  168. package/dist/tools/tool-details.js +24 -0
  169. package/dist/tools/truncate.d.ts +8 -0
  170. package/dist/tools/truncate.js +8 -0
  171. package/dist/tools/web-fetch.d.ts +0 -1
  172. package/dist/tools/web-fetch.js +9 -2
  173. package/dist/tools/web-search.d.ts +0 -1
  174. package/dist/tools/web-search.js +0 -1
  175. package/dist/tools/write-content.d.ts +3 -3
  176. package/dist/tools/write-content.js +12 -41
  177. package/dist/tools/write.d.ts +2 -3
  178. package/dist/tools/write.js +3 -4
  179. package/dist/tui/app.js +1 -1
  180. package/dist/tui/commands.d.ts +1 -1
  181. package/dist/tui/commands.js +1 -1
  182. package/dist/tui/turn-controller.d.ts +1 -1
  183. package/dist/tui/turn-controller.js +14 -4
  184. package/dist/usage/ledger.js +7 -4
  185. package/dist/usage/render.js +10 -8
  186. package/examples/sub-agents/README.md +2 -0
  187. package/package.json +3 -2
  188. package/dist/tools/skill-manage.d.ts +0 -41
  189. package/dist/tools/skill-manage.js +0 -231
@@ -65,36 +65,38 @@ async function renderWindow(window, ledger, channelId) {
65
65
  ledger.summarize({ since: window.since, until: window.until }),
66
66
  ledger.summarize({ since: window.since, until: window.until, channelId }),
67
67
  ]);
68
- const lines = [`## ${window.title}`];
68
+ const lines = [`**${window.title}**`];
69
69
  if (global.entryCount === 0) {
70
70
  lines.push("暂无用量记录。");
71
71
  return lines.join("\n");
72
72
  }
73
73
  // Tokens are reported next to cost because a local or pricing-less model bills nothing while
74
74
  // still consuming context — "$0.0000" alone would read as "nothing happened".
75
- lines.push(`本频道:${money(channel.totalCost)} · ${tokens(channel.totalTokens)}`);
75
+ let channelLine = `- 本频道:${money(channel.totalCost)} · ${tokens(channel.totalTokens)}`;
76
76
  const channelKinds = renderKindBreakdown(channel);
77
77
  if (channelKinds) {
78
- lines.push(` ${channelKinds}`);
78
+ channelLine += ` · ${channelKinds}`;
79
79
  }
80
80
  const channelUnknown = unknownNote(channel);
81
81
  if (channelUnknown) {
82
- lines.push(` ${channelUnknown}`);
82
+ channelLine += channelUnknown;
83
83
  }
84
+ lines.push(channelLine);
84
85
  const channelCount = Object.keys(global.byChannel).length;
85
- lines.push(`全局:${money(global.totalCost)} · ${tokens(global.totalTokens)},覆盖 ${channelCount} 个频道`);
86
+ let globalLine = `- 全局:${money(global.totalCost)} · ${tokens(global.totalTokens)},覆盖 ${channelCount} 个频道`;
86
87
  const topModels = topEntries(global.byModel, 3);
87
88
  if (topModels.length > 0) {
88
- lines.push(` 用量最高的模型:${topModels.map(([model, cost]) => `${model} ${money(cost)}`).join(",")}`);
89
+ globalLine += ` · 用量最高:${topModels.map(([model, cost]) => `${model} ${money(cost)}`).join(",")}`;
89
90
  }
90
91
  const globalUnknown = unknownNote(global);
91
92
  if (globalUnknown) {
92
- lines.push(` ${globalUnknown}`);
93
+ globalLine += globalUnknown;
93
94
  }
95
+ lines.push(globalLine);
94
96
  return lines.join("\n");
95
97
  }
96
98
  export async function renderUsageReport(ledger, channelId, mode, now) {
97
99
  const windows = usageWindows(mode, now);
98
100
  const rendered = await Promise.all(windows.map((window) => renderWindow(window, ledger, channelId)));
99
- return `# 用量\n\n${rendered.join("\n\n")}`;
101
+ return `**用量**\n\n${rendered.join("\n\n")}`;
100
102
  }
@@ -64,6 +64,8 @@ reviewer 发现的问题回流给产出角色;verifier 失败回流给 builder
64
64
 
65
65
  `builder` / `builder-hard` 仍用 Claude 的 `--dangerously-skip-permissions`,因为它们需要非交互地完成实现;这是本目录权限最高的默认配置。务必在可信 checkout、最小权限宿主账号中使用。角色文件只声明 `model` 和 `thinkingLevel`,具体的 `--model` / `--effort` 参数由 claude-code harness 自动拼接,不应重复写进 `command`。
66
66
 
67
+ **每个角色都应显式写 `thinkingLevel`**(本目录全部 11 个模板均已如此,见上表),不要依赖隐藏默认值——内置委派未声明时默认 `medium`,但外部 work 角色未声明时**不追加任何推理参数**,沿用该 CLI 自己的配置(`~/.claude/settings.json` / `~/.codex/config.toml` 等);只有 `purpose=verify` 的外部角色仍会兜底 `medium`。可复用的角色请显式写,让行为不随宿主机的本地配置漂移。
68
+
67
69
  ## 使用原则
68
70
 
69
71
  - `description` 会进入主代理的子代理目录,必须写清楚「何时使用、何时不用、调用前提、是否修改状态、大概多重」。这是路由的主要依据。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oyasmi/pipiclaw",
3
- "version": "0.9.1-beta.2",
3
+ "version": "0.9.2-beta.1",
4
4
  "description": "An AI assistant runtime for coding and team workflows, with DingTalk AI Cards, sub-agents, memory, and scheduled events.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -38,7 +38,8 @@
38
38
  "eval:promote": "npm run eval:build && node dist-evals/evals/harness/promote.js",
39
39
  "test:coverage": "vitest --run --coverage",
40
40
  "check": "npm run lint && npm run typecheck && npm run deadcode && npm run test",
41
- "prepublishOnly": "npm run clean && npm run build && npm run check"
41
+ "prepublishOnly": "npm run clean && npm run build && npm run check",
42
+ "test:evals": "vitest --run --config vitest.config.evals.ts"
42
43
  },
43
44
  "dependencies": {
44
45
  "@earendil-works/pi-agent-core": "0.83.0",
@@ -1,41 +0,0 @@
1
- import type { AgentTool } from "@earendil-works/pi-agent-core";
2
- import { Type } from "typebox";
3
- declare const skillManageSchema: Type.TObject<{
4
- label: Type.TString;
5
- action: Type.TUnion<[Type.TLiteral<"list">, Type.TLiteral<"view">, Type.TLiteral<"create">, Type.TLiteral<"patch">, Type.TLiteral<"write_file">]>;
6
- name: Type.TOptional<Type.TString>;
7
- content: Type.TOptional<Type.TString>;
8
- filePath: Type.TOptional<Type.TString>;
9
- find: Type.TOptional<Type.TString>;
10
- replace: Type.TOptional<Type.TString>;
11
- }>;
12
- type SkillWriteAction = "create" | "patch" | "write_file";
13
- export interface SkillManageResult {
14
- action: SkillWriteAction;
15
- name: string;
16
- path: string;
17
- bytesWritten: number;
18
- requiresResourceRefresh: boolean;
19
- notice: string;
20
- }
21
- export interface SkillManageRequest {
22
- action: SkillWriteAction;
23
- name: string;
24
- content?: string;
25
- filePath?: string;
26
- find?: string;
27
- replace?: string;
28
- }
29
- export interface WorkspaceSkillSummary {
30
- name: string;
31
- description: string;
32
- path: string;
33
- warning?: string;
34
- }
35
- export interface SkillManageToolOptions {
36
- workspaceDir: string;
37
- }
38
- export declare function listWorkspaceSkills(options: SkillManageToolOptions): Promise<WorkspaceSkillSummary[]>;
39
- export declare function manageWorkspaceSkill(options: SkillManageToolOptions, request: SkillManageRequest): Promise<SkillManageResult>;
40
- export declare function createSkillManageTool(options: SkillManageToolOptions): AgentTool<typeof skillManageSchema>;
41
- export {};
@@ -1,231 +0,0 @@
1
- import { existsSync } from "node:fs";
2
- import { readdir, readFile, stat } from "node:fs/promises";
3
- import { join } from "node:path";
4
- import { Type } from "typebox";
5
- import { createAtomicTempPath, writeFileAtomically } from "../shared/atomic-file.js";
6
- import { resolveSkillPath, resolveSkillSupportingFile, scanSkillContent, validateSkillFrontmatter, validateSkillMarkdown, validateSkillName, } from "./skill-security.js";
7
- import { DEFAULT_MAX_BYTES, formatSize, truncateHead } from "./truncate.js";
8
- const skillManageSchema = Type.Object({
9
- label: Type.String({ description: "Brief description of the skill action (shown to user)" }),
10
- action: Type.Union([
11
- Type.Literal("list"),
12
- Type.Literal("view"),
13
- Type.Literal("create"),
14
- Type.Literal("patch"),
15
- Type.Literal("write_file"),
16
- ], {
17
- description: '"list" workspace skills, "view" one skill\'s contents, or "create"/"patch"/"write_file" to author them.',
18
- }),
19
- name: Type.Optional(Type.String({ description: "Workspace skill name (required for all actions except list)." })),
20
- content: Type.Optional(Type.String({ description: "Full content for create/write_file." })),
21
- filePath: Type.Optional(Type.String({
22
- description: "Supporting file path inside the skill for view/patch/write_file. Defaults to SKILL.md. Supporting files must be under references/, templates/, scripts/, or assets/.",
23
- })),
24
- find: Type.Optional(Type.String({ description: "Exact text to replace when action is patch." })),
25
- replace: Type.Optional(Type.String({ description: "Replacement text when action is patch." })),
26
- });
27
- function isNodeError(error) {
28
- return error instanceof Error && "code" in error;
29
- }
30
- function extractDescription(content) {
31
- const match = content.replace(/\r\n/g, "\n").match(/^---\n([\s\S]*?)\n---/);
32
- if (!match) {
33
- return "";
34
- }
35
- for (const line of (match[1] ?? "").split("\n")) {
36
- const fieldMatch = line.match(/^description:\s*(.*)$/);
37
- if (fieldMatch) {
38
- return fieldMatch[1].replace(/^["']|["']$/g, "").trim();
39
- }
40
- }
41
- return "";
42
- }
43
- export async function listWorkspaceSkills(options) {
44
- const skillsDir = join(options.workspaceDir, "skills");
45
- let names;
46
- try {
47
- names = await readdir(skillsDir);
48
- }
49
- catch (error) {
50
- if (isNodeError(error) && error.code === "ENOENT") {
51
- return [];
52
- }
53
- throw error;
54
- }
55
- const summaries = [];
56
- for (const name of names.sort()) {
57
- const nameValidation = validateSkillName(name);
58
- if (!nameValidation.ok) {
59
- continue;
60
- }
61
- const skillDir = join(skillsDir, name);
62
- const skillStats = await stat(skillDir).catch(() => null);
63
- if (!skillStats?.isDirectory()) {
64
- continue;
65
- }
66
- const skillPath = join(skillDir, "SKILL.md");
67
- let content;
68
- try {
69
- const skillFileStats = await stat(skillPath);
70
- if (!skillFileStats.isFile()) {
71
- continue;
72
- }
73
- content = await readFile(skillPath, "utf-8");
74
- }
75
- catch (error) {
76
- if (isNodeError(error) && error.code === "ENOENT") {
77
- continue;
78
- }
79
- throw error;
80
- }
81
- const validation = validateSkillFrontmatter(content, name);
82
- summaries.push({
83
- name,
84
- description: extractDescription(content),
85
- path: `${options.workspaceDir}/skills/${name}/SKILL.md`,
86
- warning: validation.ok ? undefined : validation.error,
87
- });
88
- }
89
- return summaries;
90
- }
91
- function parseWriteAction(action) {
92
- if (action === "create" || action === "patch" || action === "write_file") {
93
- return action;
94
- }
95
- throw new Error('Unsupported skill action. Use "list", "view", "create", "patch", or "write_file".');
96
- }
97
- function ensureSkillMarkdownSafe(content, name) {
98
- const validation = validateSkillMarkdown(content, name);
99
- if (!validation.ok) {
100
- throw new Error(validation.error);
101
- }
102
- }
103
- function ensureSupportingFileSafe(content) {
104
- const validation = scanSkillContent(content);
105
- if (!validation.ok) {
106
- throw new Error(validation.error);
107
- }
108
- }
109
- function applyUniquePatch(content, find, replace) {
110
- if (!find) {
111
- throw new Error("Patch requires a non-empty find string.");
112
- }
113
- const first = content.indexOf(find);
114
- if (first < 0) {
115
- throw new Error("Patch find string was not found.");
116
- }
117
- if (content.indexOf(find, first + find.length) >= 0) {
118
- throw new Error("Patch find string matched multiple locations.");
119
- }
120
- return `${content.slice(0, first)}${replace}${content.slice(first + find.length)}`;
121
- }
122
- export async function manageWorkspaceSkill(options, request) {
123
- const skillDir = resolveSkillPath(options.workspaceDir, request.name);
124
- const skillPath = join(skillDir, "SKILL.md");
125
- if (request.action === "create") {
126
- if (existsSync(skillPath)) {
127
- throw new Error(`Workspace skill "${request.name}" already exists.`);
128
- }
129
- const content = request.content ?? "";
130
- ensureSkillMarkdownSafe(content, request.name);
131
- await writeFileAtomically(skillPath, content);
132
- return {
133
- action: "create",
134
- name: request.name,
135
- path: skillPath,
136
- bytesWritten: Buffer.byteLength(content, "utf-8"),
137
- requiresResourceRefresh: true,
138
- notice: `已沉淀:创建 workspace skill \`${request.name}\`。`,
139
- };
140
- }
141
- if (!existsSync(skillPath)) {
142
- throw new Error(`Workspace skill "${request.name}" does not exist.`);
143
- }
144
- if (request.action === "write_file") {
145
- if (!request.filePath) {
146
- throw new Error("write_file requires filePath.");
147
- }
148
- const content = request.content ?? "";
149
- const targetPath = resolveSkillSupportingFile(skillDir, request.filePath);
150
- ensureSupportingFileSafe(content);
151
- await writeFileAtomically(targetPath, content);
152
- return {
153
- action: "write_file",
154
- name: request.name,
155
- path: targetPath,
156
- bytesWritten: Buffer.byteLength(content, "utf-8"),
157
- requiresResourceRefresh: true,
158
- notice: `已沉淀:更新 workspace skill \`${request.name}\` 的支持文件。`,
159
- };
160
- }
161
- const targetPath = request.filePath ? resolveSkillSupportingFile(skillDir, request.filePath) : skillPath;
162
- const original = await readFile(targetPath, "utf-8");
163
- const nextContent = applyUniquePatch(original, request.find ?? "", request.replace ?? "");
164
- if (targetPath === skillPath) {
165
- ensureSkillMarkdownSafe(nextContent, request.name);
166
- }
167
- else {
168
- ensureSupportingFileSafe(nextContent);
169
- }
170
- const tempPath = createAtomicTempPath(targetPath);
171
- await writeFileAtomically(targetPath, nextContent, tempPath);
172
- return {
173
- action: "patch",
174
- name: request.name,
175
- path: targetPath,
176
- bytesWritten: Buffer.byteLength(nextContent, "utf-8"),
177
- requiresResourceRefresh: true,
178
- notice: `已沉淀:更新 workspace skill \`${request.name}\`。`,
179
- };
180
- }
181
- async function viewWorkspaceSkill(options, name, filePath) {
182
- const skillDir = resolveSkillPath(options.workspaceDir, name);
183
- const targetPath = filePath ? resolveSkillSupportingFile(skillDir, filePath) : join(skillDir, "SKILL.md");
184
- const content = await readFile(targetPath, "utf-8");
185
- // Cap with the shared truncation limits so a large supporting file cannot flood context.
186
- const truncation = truncateHead(content);
187
- let body = truncation.content;
188
- if (truncation.truncated) {
189
- body += `\n\n[Truncated at ${formatSize(DEFAULT_MAX_BYTES)}. Use the read tool on ${targetPath} to page through the rest.]`;
190
- }
191
- return {
192
- content: [{ type: "text", text: `Skill: ${name}\nPath: ${targetPath}\n\n${body}` }],
193
- details: { action: "view", name, path: targetPath, truncated: truncation.truncated },
194
- };
195
- }
196
- export function createSkillManageTool(options) {
197
- return {
198
- name: "skill_manage",
199
- label: "skill_manage",
200
- description: "Manage workspace-level Pipiclaw skills (procedural memory): list them, view one's contents, or author them " +
201
- "with create/patch/write_file. No channel-scoped skills.",
202
- parameters: skillManageSchema,
203
- execute: async (_toolCallId, args) => {
204
- if (args.action === "list") {
205
- const skills = await listWorkspaceSkills(options);
206
- return {
207
- content: [{ type: "text", text: JSON.stringify({ skills }) }],
208
- details: { action: "list", count: skills.length },
209
- };
210
- }
211
- if (!args.name) {
212
- throw new Error(`Action "${args.action}" requires a skill name.`);
213
- }
214
- if (args.action === "view") {
215
- return viewWorkspaceSkill(options, args.name, args.filePath);
216
- }
217
- const result = await manageWorkspaceSkill(options, {
218
- action: parseWriteAction(args.action),
219
- name: args.name,
220
- content: args.content,
221
- filePath: args.filePath,
222
- find: args.find,
223
- replace: args.replace,
224
- });
225
- return {
226
- content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
227
- details: { ...result },
228
- };
229
- },
230
- };
231
- }