@hyperwindmill/caretaker-cli 0.11.0

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 (275) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +25 -0
  3. package/dist/agents/dispatch.d.ts +35 -0
  4. package/dist/agents/dispatch.d.ts.map +1 -0
  5. package/dist/agents/dispatch.js +108 -0
  6. package/dist/agents/sync.d.ts +10 -0
  7. package/dist/agents/sync.d.ts.map +1 -0
  8. package/dist/agents/sync.js +95 -0
  9. package/dist/cli/index.d.ts +2 -0
  10. package/dist/cli/index.d.ts.map +1 -0
  11. package/dist/cli/index.js +50 -0
  12. package/dist/cli/run.d.ts +11 -0
  13. package/dist/cli/run.d.ts.map +1 -0
  14. package/dist/cli/run.js +89 -0
  15. package/dist/cli/web/fs.d.ts +3 -0
  16. package/dist/cli/web/fs.d.ts.map +1 -0
  17. package/dist/cli/web/fs.js +35 -0
  18. package/dist/cli/web/mcp_bridge.d.ts +7 -0
  19. package/dist/cli/web/mcp_bridge.d.ts.map +1 -0
  20. package/dist/cli/web/mcp_bridge.js +86 -0
  21. package/dist/cli/web/scheduler/heartbeat.d.ts +16 -0
  22. package/dist/cli/web/scheduler/heartbeat.d.ts.map +1 -0
  23. package/dist/cli/web/scheduler/heartbeat.js +181 -0
  24. package/dist/cli/web/scheduler/locks.d.ts +8 -0
  25. package/dist/cli/web/scheduler/locks.d.ts.map +1 -0
  26. package/dist/cli/web/scheduler/locks.js +7 -0
  27. package/dist/cli/web/scheduler/logs.d.ts +21 -0
  28. package/dist/cli/web/scheduler/logs.d.ts.map +1 -0
  29. package/dist/cli/web/scheduler/logs.js +62 -0
  30. package/dist/cli/web/scheduler/strategy.d.ts +6 -0
  31. package/dist/cli/web/scheduler/strategy.d.ts.map +1 -0
  32. package/dist/cli/web/scheduler/strategy.js +1 -0
  33. package/dist/cli/web/scheduler/task_review.d.ts +18 -0
  34. package/dist/cli/web/scheduler/task_review.d.ts.map +1 -0
  35. package/dist/cli/web/scheduler/task_review.js +77 -0
  36. package/dist/cli/web/scheduler/task_roles.d.ts +22 -0
  37. package/dist/cli/web/scheduler/task_roles.d.ts.map +1 -0
  38. package/dist/cli/web/scheduler/task_roles.js +70 -0
  39. package/dist/cli/web/scheduler/task_strategy.d.ts +2 -0
  40. package/dist/cli/web/scheduler/task_strategy.d.ts.map +1 -0
  41. package/dist/cli/web/scheduler/task_strategy.js +437 -0
  42. package/dist/cli/web/scheduler/telegram.d.ts +7 -0
  43. package/dist/cli/web/scheduler/telegram.d.ts.map +1 -0
  44. package/dist/cli/web/scheduler/telegram.js +393 -0
  45. package/dist/cli/web/scheduler.d.ts +15 -0
  46. package/dist/cli/web/scheduler.d.ts.map +1 -0
  47. package/dist/cli/web/scheduler.js +60 -0
  48. package/dist/cli/web/server.d.ts +38 -0
  49. package/dist/cli/web/server.d.ts.map +1 -0
  50. package/dist/cli/web/server.js +970 -0
  51. package/dist/commands/loader.d.ts +37 -0
  52. package/dist/commands/loader.d.ts.map +1 -0
  53. package/dist/commands/loader.js +110 -0
  54. package/dist/harness/claude_code_runner.d.ts +47 -0
  55. package/dist/harness/claude_code_runner.d.ts.map +1 -0
  56. package/dist/harness/claude_code_runner.js +325 -0
  57. package/dist/harness/claude_code_stream.d.ts +32 -0
  58. package/dist/harness/claude_code_stream.d.ts.map +1 -0
  59. package/dist/harness/claude_code_stream.js +121 -0
  60. package/dist/harness/context_files.d.ts +23 -0
  61. package/dist/harness/context_files.d.ts.map +1 -0
  62. package/dist/harness/context_files.js +126 -0
  63. package/dist/harness/history.d.ts +4 -0
  64. package/dist/harness/history.d.ts.map +1 -0
  65. package/dist/harness/history.js +120 -0
  66. package/dist/harness/index.d.ts +8 -0
  67. package/dist/harness/index.d.ts.map +1 -0
  68. package/dist/harness/index.js +8 -0
  69. package/dist/harness/loop.d.ts +73 -0
  70. package/dist/harness/loop.d.ts.map +1 -0
  71. package/dist/harness/loop.js +373 -0
  72. package/dist/harness/model_limits.d.ts +25 -0
  73. package/dist/harness/model_limits.d.ts.map +1 -0
  74. package/dist/harness/model_limits.js +67 -0
  75. package/dist/harness/models.d.ts +9 -0
  76. package/dist/harness/models.d.ts.map +1 -0
  77. package/dist/harness/models.js +38 -0
  78. package/dist/harness/prelude.d.ts +21 -0
  79. package/dist/harness/prelude.d.ts.map +1 -0
  80. package/dist/harness/prelude.js +41 -0
  81. package/dist/harness/provider.d.ts +87 -0
  82. package/dist/harness/provider.d.ts.map +1 -0
  83. package/dist/harness/provider.js +140 -0
  84. package/dist/harness/runtime_info.d.ts +7 -0
  85. package/dist/harness/runtime_info.d.ts.map +1 -0
  86. package/dist/harness/runtime_info.js +15 -0
  87. package/dist/harness/think_tag_splitter.d.ts +33 -0
  88. package/dist/harness/think_tag_splitter.d.ts.map +1 -0
  89. package/dist/harness/think_tag_splitter.js +70 -0
  90. package/dist/harness/title.d.ts +17 -0
  91. package/dist/harness/title.d.ts.map +1 -0
  92. package/dist/harness/title.js +75 -0
  93. package/dist/harness/tools/builtin/bash.d.ts +3 -0
  94. package/dist/harness/tools/builtin/bash.d.ts.map +1 -0
  95. package/dist/harness/tools/builtin/bash.js +137 -0
  96. package/dist/harness/tools/builtin/edit.d.ts +6 -0
  97. package/dist/harness/tools/builtin/edit.d.ts.map +1 -0
  98. package/dist/harness/tools/builtin/edit.js +85 -0
  99. package/dist/harness/tools/builtin/fetch.d.ts +3 -0
  100. package/dist/harness/tools/builtin/fetch.d.ts.map +1 -0
  101. package/dist/harness/tools/builtin/fetch.js +75 -0
  102. package/dist/harness/tools/builtin/get_agent_context.d.ts +3 -0
  103. package/dist/harness/tools/builtin/get_agent_context.d.ts.map +1 -0
  104. package/dist/harness/tools/builtin/get_agent_context.js +76 -0
  105. package/dist/harness/tools/builtin/glob.d.ts +3 -0
  106. package/dist/harness/tools/builtin/glob.d.ts.map +1 -0
  107. package/dist/harness/tools/builtin/glob.js +52 -0
  108. package/dist/harness/tools/builtin/grep.d.ts +3 -0
  109. package/dist/harness/tools/builtin/grep.d.ts.map +1 -0
  110. package/dist/harness/tools/builtin/grep.js +126 -0
  111. package/dist/harness/tools/builtin/index.d.ts +23 -0
  112. package/dist/harness/tools/builtin/index.d.ts.map +1 -0
  113. package/dist/harness/tools/builtin/index.js +74 -0
  114. package/dist/harness/tools/builtin/invoke_agent.d.ts +3 -0
  115. package/dist/harness/tools/builtin/invoke_agent.d.ts.map +1 -0
  116. package/dist/harness/tools/builtin/invoke_agent.js +106 -0
  117. package/dist/harness/tools/builtin/invoke_command.d.ts +3 -0
  118. package/dist/harness/tools/builtin/invoke_command.d.ts.map +1 -0
  119. package/dist/harness/tools/builtin/invoke_command.js +42 -0
  120. package/dist/harness/tools/builtin/list_agents.d.ts +3 -0
  121. package/dist/harness/tools/builtin/list_agents.d.ts.map +1 -0
  122. package/dist/harness/tools/builtin/list_agents.js +32 -0
  123. package/dist/harness/tools/builtin/list_commands.d.ts +3 -0
  124. package/dist/harness/tools/builtin/list_commands.d.ts.map +1 -0
  125. package/dist/harness/tools/builtin/list_commands.js +27 -0
  126. package/dist/harness/tools/builtin/list_skills.d.ts +3 -0
  127. package/dist/harness/tools/builtin/list_skills.d.ts.map +1 -0
  128. package/dist/harness/tools/builtin/list_skills.js +24 -0
  129. package/dist/harness/tools/builtin/multiedit.d.ts +3 -0
  130. package/dist/harness/tools/builtin/multiedit.d.ts.map +1 -0
  131. package/dist/harness/tools/builtin/multiedit.js +88 -0
  132. package/dist/harness/tools/builtin/read_attachment.d.ts +3 -0
  133. package/dist/harness/tools/builtin/read_attachment.d.ts.map +1 -0
  134. package/dist/harness/tools/builtin/read_attachment.js +128 -0
  135. package/dist/harness/tools/builtin/read_document.d.ts +37 -0
  136. package/dist/harness/tools/builtin/read_document.d.ts.map +1 -0
  137. package/dist/harness/tools/builtin/read_document.js +238 -0
  138. package/dist/harness/tools/builtin/read_file.d.ts +3 -0
  139. package/dist/harness/tools/builtin/read_file.d.ts.map +1 -0
  140. package/dist/harness/tools/builtin/read_file.js +141 -0
  141. package/dist/harness/tools/builtin/read_image.d.ts +3 -0
  142. package/dist/harness/tools/builtin/read_image.d.ts.map +1 -0
  143. package/dist/harness/tools/builtin/read_image.js +80 -0
  144. package/dist/harness/tools/builtin/read_skill.d.ts +3 -0
  145. package/dist/harness/tools/builtin/read_skill.d.ts.map +1 -0
  146. package/dist/harness/tools/builtin/read_skill.js +30 -0
  147. package/dist/harness/tools/builtin/shell-env.d.ts +55 -0
  148. package/dist/harness/tools/builtin/shell-env.d.ts.map +1 -0
  149. package/dist/harness/tools/builtin/shell-env.js +181 -0
  150. package/dist/harness/tools/builtin/task_tools.d.ts +21 -0
  151. package/dist/harness/tools/builtin/task_tools.d.ts.map +1 -0
  152. package/dist/harness/tools/builtin/task_tools.js +708 -0
  153. package/dist/harness/tools/builtin/write.d.ts +3 -0
  154. package/dist/harness/tools/builtin/write.d.ts.map +1 -0
  155. package/dist/harness/tools/builtin/write.js +56 -0
  156. package/dist/harness/tools/index.d.ts +6 -0
  157. package/dist/harness/tools/index.d.ts.map +1 -0
  158. package/dist/harness/tools/index.js +7 -0
  159. package/dist/harness/tools/instance.d.ts +3 -0
  160. package/dist/harness/tools/instance.d.ts.map +1 -0
  161. package/dist/harness/tools/instance.js +19 -0
  162. package/dist/harness/tools/registry.d.ts +21 -0
  163. package/dist/harness/tools/registry.d.ts.map +1 -0
  164. package/dist/harness/tools/registry.js +33 -0
  165. package/dist/harness/tools/resolve.d.ts +5 -0
  166. package/dist/harness/tools/resolve.d.ts.map +1 -0
  167. package/dist/harness/tools/resolve.js +60 -0
  168. package/dist/harness/tools/sandbox.d.ts +6 -0
  169. package/dist/harness/tools/sandbox.d.ts.map +1 -0
  170. package/dist/harness/tools/sandbox.js +23 -0
  171. package/dist/harness/tools/types.d.ts +80 -0
  172. package/dist/harness/tools/types.d.ts.map +1 -0
  173. package/dist/harness/tools/types.js +16 -0
  174. package/dist/index.d.ts +3 -0
  175. package/dist/index.d.ts.map +1 -0
  176. package/dist/index.js +46 -0
  177. package/dist/lib/encryption.d.ts +24 -0
  178. package/dist/lib/encryption.d.ts.map +1 -0
  179. package/dist/lib/encryption.js +163 -0
  180. package/dist/lib/open_url.d.ts +9 -0
  181. package/dist/lib/open_url.d.ts.map +1 -0
  182. package/dist/lib/open_url.js +23 -0
  183. package/dist/lib/task_git.d.ts +11 -0
  184. package/dist/lib/task_git.d.ts.map +1 -0
  185. package/dist/lib/task_git.js +100 -0
  186. package/dist/mcp/adapter.d.ts +9 -0
  187. package/dist/mcp/adapter.d.ts.map +1 -0
  188. package/dist/mcp/adapter.js +74 -0
  189. package/dist/mcp/client.d.ts +50 -0
  190. package/dist/mcp/client.d.ts.map +1 -0
  191. package/dist/mcp/client.js +243 -0
  192. package/dist/mcp/oauth.d.ts +30 -0
  193. package/dist/mcp/oauth.d.ts.map +1 -0
  194. package/dist/mcp/oauth.js +171 -0
  195. package/dist/mcp/oauth_callback.d.ts +9 -0
  196. package/dist/mcp/oauth_callback.d.ts.map +1 -0
  197. package/dist/mcp/oauth_callback.js +51 -0
  198. package/dist/mcp/oauth_store.d.ts +22 -0
  199. package/dist/mcp/oauth_store.d.ts.map +1 -0
  200. package/dist/mcp/oauth_store.js +40 -0
  201. package/dist/mcp/server_manager.d.ts +49 -0
  202. package/dist/mcp/server_manager.d.ts.map +1 -0
  203. package/dist/mcp/server_manager.js +271 -0
  204. package/dist/plugins/fetchers/git.d.ts +67 -0
  205. package/dist/plugins/fetchers/git.d.ts.map +1 -0
  206. package/dist/plugins/fetchers/git.js +130 -0
  207. package/dist/plugins/fetchers/path.d.ts +4 -0
  208. package/dist/plugins/fetchers/path.d.ts.map +1 -0
  209. package/dist/plugins/fetchers/path.js +22 -0
  210. package/dist/plugins/loader.d.ts +31 -0
  211. package/dist/plugins/loader.d.ts.map +1 -0
  212. package/dist/plugins/loader.js +146 -0
  213. package/dist/plugins/manifest.d.ts +56 -0
  214. package/dist/plugins/manifest.d.ts.map +1 -0
  215. package/dist/plugins/manifest.js +389 -0
  216. package/dist/plugins/refresh_on_start.d.ts +7 -0
  217. package/dist/plugins/refresh_on_start.d.ts.map +1 -0
  218. package/dist/plugins/refresh_on_start.js +35 -0
  219. package/dist/plugins/source_manager.d.ts +27 -0
  220. package/dist/plugins/source_manager.d.ts.map +1 -0
  221. package/dist/plugins/source_manager.js +211 -0
  222. package/dist/plugins/types.d.ts +41 -0
  223. package/dist/plugins/types.d.ts.map +1 -0
  224. package/dist/plugins/types.js +11 -0
  225. package/dist/session/context_usage.d.ts +13 -0
  226. package/dist/session/context_usage.d.ts.map +1 -0
  227. package/dist/session/context_usage.js +25 -0
  228. package/dist/session/index.d.ts +7 -0
  229. package/dist/session/index.d.ts.map +1 -0
  230. package/dist/session/index.js +5 -0
  231. package/dist/session/store.d.ts +68 -0
  232. package/dist/session/store.d.ts.map +1 -0
  233. package/dist/session/store.js +268 -0
  234. package/dist/session/types.d.ts +60 -0
  235. package/dist/session/types.d.ts.map +1 -0
  236. package/dist/session/types.js +11 -0
  237. package/dist/store/db.d.ts +64 -0
  238. package/dist/store/db.d.ts.map +1 -0
  239. package/dist/store/db.js +91 -0
  240. package/dist/store/json.d.ts +21 -0
  241. package/dist/store/json.d.ts.map +1 -0
  242. package/dist/store/json.js +135 -0
  243. package/dist/tui/agents.d.ts +4 -0
  244. package/dist/tui/agents.d.ts.map +1 -0
  245. package/dist/tui/agents.js +616 -0
  246. package/dist/tui/app.d.ts +2 -0
  247. package/dist/tui/app.d.ts.map +1 -0
  248. package/dist/tui/app.js +32 -0
  249. package/dist/tui/chat.d.ts +10 -0
  250. package/dist/tui/chat.d.ts.map +1 -0
  251. package/dist/tui/chat.js +258 -0
  252. package/dist/tui/logo.d.ts +2 -0
  253. package/dist/tui/logo.d.ts.map +1 -0
  254. package/dist/tui/logo.js +10 -0
  255. package/dist/tui/mcp_servers.d.ts +4 -0
  256. package/dist/tui/mcp_servers.d.ts.map +1 -0
  257. package/dist/tui/mcp_servers.js +287 -0
  258. package/dist/tui/plugins.d.ts +4 -0
  259. package/dist/tui/plugins.d.ts.map +1 -0
  260. package/dist/tui/plugins.js +234 -0
  261. package/dist/tui/providers.d.ts +4 -0
  262. package/dist/tui/providers.d.ts.map +1 -0
  263. package/dist/tui/providers.js +178 -0
  264. package/dist/tui/tool_picker_state.d.ts +9 -0
  265. package/dist/tui/tool_picker_state.d.ts.map +1 -0
  266. package/dist/tui/tool_picker_state.js +37 -0
  267. package/dist/types.d.ts +2 -0
  268. package/dist/types.d.ts.map +1 -0
  269. package/dist/types.js +1 -0
  270. package/dist/webview/index.html +22 -0
  271. package/dist/webview/standalone.css +2732 -0
  272. package/dist/webview/standalone.css.map +7 -0
  273. package/dist/webview/standalone.js +20641 -0
  274. package/dist/webview/standalone.js.map +7 -0
  275. package/package.json +120 -0
@@ -0,0 +1,389 @@
1
+ // Plugin discovery: given a fetched source root, identify the plugins it
2
+ // contains. Three manifest kinds, tried in order:
3
+ // 1. cc-marketplace — `.claude-plugin/marketplace.json` lists multiple
4
+ // plugins by name + relative source path
5
+ // 2. cc-plugin — `.claude-plugin/plugin.json` defines a single plugin
6
+ // whose root is the entire source
7
+ // 3. skill-glob — fallback: every `**/SKILL.md` is its own plugin,
8
+ // named from frontmatter or its containing directory
9
+ //
10
+ // Returns the first kind that matches; throws NoPluginsFoundError if none
11
+ // of the three turn up anything. Ports server src/plugins/manifest.ts
12
+ // behavior 1-for-1 so a source authored for caretaker server discovers
13
+ // identically here.
14
+ import { readFile, stat } from 'node:fs/promises';
15
+ import * as path from 'node:path';
16
+ import fg from 'fast-glob';
17
+ import { parse as parseYaml } from 'yaml';
18
+ import { NoPluginsFoundError } from './types.js';
19
+ const MCP_FILE_REL = '.mcp.json';
20
+ const AGENTS_DIR_REL = 'agents';
21
+ const COMMANDS_DIR_REL = 'commands';
22
+ const MARKETPLACE_REL = '.claude-plugin/marketplace.json';
23
+ const PLUGIN_REL = '.claude-plugin/plugin.json';
24
+ async function fileExists(p) {
25
+ try {
26
+ await stat(p);
27
+ return true;
28
+ }
29
+ catch {
30
+ return false;
31
+ }
32
+ }
33
+ function isInside(root, candidate) {
34
+ const resolvedRoot = path.resolve(root);
35
+ const resolvedCandidate = path.resolve(root, candidate);
36
+ const rel = path.relative(resolvedRoot, resolvedCandidate);
37
+ return !rel.startsWith('..') && !path.isAbsolute(rel);
38
+ }
39
+ /**
40
+ * Parse a `mcpServers` record into validated specs. The input is the value
41
+ * side of a `.mcp.json` map (or its `mcpServers` wrapper). Entries missing
42
+ * both `command` (stdio) and `url` (http) are silently dropped so a
43
+ * malformed manifest can't poison sync. The optional `type` field is a hint
44
+ * the spec emits but we don't rely on it — `command` vs `url` discriminates
45
+ * unambiguously.
46
+ */
47
+ function parseMcpServersMap(raw) {
48
+ if (!raw || typeof raw !== 'object')
49
+ return undefined;
50
+ const out = {};
51
+ for (const [name, value] of Object.entries(raw)) {
52
+ if (!value || typeof value !== 'object')
53
+ continue;
54
+ const v = value;
55
+ if (typeof v.command === 'string' && v.command.trim() !== '') {
56
+ out[name] = {
57
+ command: v.command,
58
+ args: Array.isArray(v.args) ? v.args : undefined,
59
+ env: v.env && typeof v.env === 'object' ? v.env : undefined,
60
+ };
61
+ }
62
+ else if (typeof v.url === 'string' && v.url.trim() !== '') {
63
+ out[name] = {
64
+ url: v.url,
65
+ headers: v.headers && typeof v.headers === 'object'
66
+ ? v.headers
67
+ : undefined,
68
+ };
69
+ }
70
+ }
71
+ return Object.keys(out).length > 0 ? out : undefined;
72
+ }
73
+ /**
74
+ * Read `<pluginRoot>/.mcp.json` and return the declared MCP server specs.
75
+ * Two shapes are observed in the wild and both are accepted:
76
+ * - `{ "mcpServers": { name: { … } } }` (lh-youtrack, official spec)
77
+ * - `{ name: { … } }` (claude-plugins-official examples)
78
+ *
79
+ * Returns `undefined` when the file is absent, malformed, or contains zero
80
+ * usable entries — the caller treats that as "this plugin declares no MCP
81
+ * servers" and the row stays clean. Path traversal is guarded by the
82
+ * caller; we only read the resolved absolute path.
83
+ */
84
+ export async function discoverPluginMcpServers(pluginRoot) {
85
+ const abs = path.join(pluginRoot, MCP_FILE_REL);
86
+ if (!(await fileExists(abs)))
87
+ return undefined;
88
+ let raw;
89
+ try {
90
+ raw = JSON.parse(await readFile(abs, 'utf-8'));
91
+ }
92
+ catch {
93
+ return undefined;
94
+ }
95
+ if (!raw || typeof raw !== 'object')
96
+ return undefined;
97
+ // Wrappered form takes precedence when both could match.
98
+ const wrappered = raw.mcpServers;
99
+ if (wrappered !== undefined)
100
+ return parseMcpServersMap(wrappered);
101
+ return parseMcpServersMap(raw);
102
+ }
103
+ function dedupeByName(plugins) {
104
+ const seen = new Set();
105
+ const out = [];
106
+ for (const p of plugins) {
107
+ if (seen.has(p.name))
108
+ continue;
109
+ seen.add(p.name);
110
+ out.push(p);
111
+ }
112
+ return out;
113
+ }
114
+ async function readMarketplace(root) {
115
+ const abs = path.join(root, MARKETPLACE_REL);
116
+ if (!(await fileExists(abs)))
117
+ return null;
118
+ const raw = JSON.parse(await readFile(abs, 'utf-8'));
119
+ const entries = Array.isArray(raw.plugins) ? raw.plugins : [];
120
+ const out = [];
121
+ for (const e of entries) {
122
+ if (!e.name || !e.source)
123
+ continue;
124
+ if (!isInside(root, e.source))
125
+ continue;
126
+ out.push({
127
+ name: e.name,
128
+ description: e.description ?? null,
129
+ manifestKind: 'cc-marketplace',
130
+ relPath: e.source,
131
+ rawManifest: e,
132
+ });
133
+ }
134
+ const deduped = dedupeByName(out);
135
+ return deduped.length === 0 ? null : deduped;
136
+ }
137
+ async function readSinglePlugin(root) {
138
+ const abs = path.join(root, PLUGIN_REL);
139
+ if (!(await fileExists(abs)))
140
+ return null;
141
+ const raw = JSON.parse(await readFile(abs, 'utf-8'));
142
+ if (!raw.name)
143
+ return null;
144
+ return [
145
+ {
146
+ name: raw.name,
147
+ description: raw.description ?? null,
148
+ manifestKind: 'cc-plugin',
149
+ relPath: '.',
150
+ rawManifest: raw,
151
+ },
152
+ ];
153
+ }
154
+ async function readSkillGlob(root) {
155
+ const matches = await fg('**/SKILL.md', {
156
+ cwd: root,
157
+ ignore: ['node_modules/**', '.git/**'],
158
+ onlyFiles: true,
159
+ dot: false,
160
+ });
161
+ if (matches.length === 0)
162
+ return null;
163
+ const out = [];
164
+ for (const rel of matches) {
165
+ const abs = path.join(root, rel);
166
+ let frontmatter = {};
167
+ try {
168
+ const text = await readFile(abs, 'utf-8');
169
+ const m = /^---\n([\s\S]*?)\n---/m.exec(text);
170
+ if (m)
171
+ frontmatter = (parseYaml(m[1]) ?? {});
172
+ }
173
+ catch {
174
+ /* ignore parse errors, fall through */
175
+ }
176
+ const dir = path.dirname(rel);
177
+ const name = (typeof frontmatter.name === 'string' && frontmatter.name) || path.basename(dir);
178
+ const description = typeof frontmatter.description === 'string' ? frontmatter.description : null;
179
+ out.push({
180
+ name,
181
+ description,
182
+ manifestKind: 'skill-glob',
183
+ relPath: dir,
184
+ rawManifest: { frontmatter, file: rel },
185
+ });
186
+ }
187
+ const deduped = dedupeByName(out);
188
+ return deduped.length === 0 ? null : deduped;
189
+ }
190
+ export async function discoverPlugins(root) {
191
+ const fromMarketplace = await readMarketplace(root);
192
+ if (fromMarketplace)
193
+ return fromMarketplace;
194
+ const fromSingle = await readSinglePlugin(root);
195
+ if (fromSingle)
196
+ return fromSingle;
197
+ const fromGlob = await readSkillGlob(root);
198
+ if (fromGlob)
199
+ return fromGlob;
200
+ throw new NoPluginsFoundError();
201
+ }
202
+ /**
203
+ * Read every `<pluginRoot>/agents/*.md` and parse it as a sub-agent spec:
204
+ * YAML frontmatter contributes `name`, `description`, `model`; the body
205
+ * (everything after the closing `---`) becomes the system prompt.
206
+ *
207
+ * The frontmatter `tools:` field is intentionally ignored — its vocabulary
208
+ * is Anthropic's tool surface (Read, Bash, Glob, …) which doesn't map 1:1
209
+ * to ours, and a wrong mapping would silently grant or deny tools. The
210
+ * user wires allowedTools via the agent form after the row is materialized.
211
+ *
212
+ * The map is keyed by the filename basename (without `.md`), which becomes
213
+ * the plugin-scoped name during sync. Files without parseable frontmatter
214
+ * are skipped silently. Returns undefined when the directory is missing or
215
+ * empty so the discovered plugin row stays clean.
216
+ */
217
+ export async function discoverPluginAgents(pluginRoot) {
218
+ const agentsDir = path.join(pluginRoot, AGENTS_DIR_REL);
219
+ if (!(await fileExists(agentsDir)))
220
+ return undefined;
221
+ let matches;
222
+ try {
223
+ matches = await fg('*.md', {
224
+ cwd: agentsDir,
225
+ onlyFiles: true,
226
+ dot: false,
227
+ });
228
+ }
229
+ catch {
230
+ return undefined;
231
+ }
232
+ const out = {};
233
+ for (const rel of matches) {
234
+ const abs = path.join(agentsDir, rel);
235
+ let text;
236
+ try {
237
+ text = await readFile(abs, 'utf-8');
238
+ }
239
+ catch {
240
+ continue;
241
+ }
242
+ const fmMatch = /^---\n([\s\S]*?)\n---\s*\n?/m.exec(text);
243
+ let frontmatter = {};
244
+ let body = text;
245
+ if (fmMatch) {
246
+ try {
247
+ frontmatter = (parseYaml(fmMatch[1]) ?? {});
248
+ }
249
+ catch {
250
+ // Malformed YAML — drop the frontmatter, keep the body so the
251
+ // user at least gets a usable system prompt.
252
+ frontmatter = {};
253
+ }
254
+ body = text.slice(fmMatch[0].length);
255
+ }
256
+ const scopedName = path.basename(rel, '.md');
257
+ const fmName = typeof frontmatter.name === 'string' && frontmatter.name.trim() !== ''
258
+ ? frontmatter.name.trim()
259
+ : scopedName;
260
+ const fmDescription = typeof frontmatter.description === 'string' ? frontmatter.description : undefined;
261
+ const fmModel = typeof frontmatter.model === 'string' ? frontmatter.model : undefined;
262
+ out[scopedName] = {
263
+ name: fmName,
264
+ description: fmDescription,
265
+ model: fmModel,
266
+ systemPrompt: body.trim(),
267
+ };
268
+ }
269
+ return Object.keys(out).length > 0 ? out : undefined;
270
+ }
271
+ /**
272
+ * Read every `<pluginRoot>/commands/*.md` and return slash-command specs.
273
+ * Frontmatter contributes `description` and `argument-hint` (kebab-case
274
+ * matches the official Claude Code convention); the body below the
275
+ * frontmatter is the prompt template with `$1`/`$2`/`$ARGUMENTS`
276
+ * placeholders. Files without a body are skipped.
277
+ *
278
+ * Map keys are filename basenames so `commands/foo.md` becomes `/foo` at
279
+ * chat time.
280
+ */
281
+ export async function discoverPluginCommands(pluginRoot) {
282
+ const dir = path.join(pluginRoot, COMMANDS_DIR_REL);
283
+ if (!(await fileExists(dir)))
284
+ return undefined;
285
+ let matches;
286
+ try {
287
+ matches = await fg('*.md', { cwd: dir, onlyFiles: true, dot: false });
288
+ }
289
+ catch {
290
+ return undefined;
291
+ }
292
+ const out = {};
293
+ for (const rel of matches) {
294
+ const abs = path.join(dir, rel);
295
+ let text;
296
+ try {
297
+ text = await readFile(abs, 'utf-8');
298
+ }
299
+ catch {
300
+ continue;
301
+ }
302
+ const fmMatch = /^---\n([\s\S]*?)\n---\s*\n?/m.exec(text);
303
+ let frontmatter = {};
304
+ let body = text;
305
+ if (fmMatch) {
306
+ try {
307
+ frontmatter = (parseYaml(fmMatch[1]) ?? {});
308
+ }
309
+ catch {
310
+ frontmatter = {};
311
+ }
312
+ body = text.slice(fmMatch[0].length);
313
+ }
314
+ const trimmedBody = body.trim();
315
+ if (!trimmedBody)
316
+ continue;
317
+ const description = typeof frontmatter.description === 'string' ? frontmatter.description : undefined;
318
+ // Claude Code uses kebab-case `argument-hint`; YAML parses it as a
319
+ // dashed key string, NOT a JS property. Read it via bracket access.
320
+ const argRaw = frontmatter['argument-hint'];
321
+ const argumentHint = typeof argRaw === 'string' ? argRaw : undefined;
322
+ out[path.basename(rel, '.md')] = {
323
+ description,
324
+ argumentHint,
325
+ body: trimmedBody,
326
+ };
327
+ }
328
+ return Object.keys(out).length > 0 ? out : undefined;
329
+ }
330
+ /**
331
+ * Enumerate the SKILL.md files inside a plugin and return one SkillSpec
332
+ * per file. For `skill-glob` plugins (the plugin's own root IS the skill)
333
+ * the function looks at `<pluginRoot>/SKILL.md` only. For `cc-plugin` /
334
+ * `cc-marketplace` plugins it globs every `**\/SKILL.md` so a pack like
335
+ * superpowers surfaces each of its sub-skills individually.
336
+ *
337
+ * The scoped name (= map key) comes from the frontmatter `name`, falling
338
+ * back to the basename of the directory containing SKILL.md, falling back
339
+ * to `pluginNameFallback` (used only for the rare `skill-glob` SKILL.md
340
+ * sitting at the plugin root with no frontmatter).
341
+ */
342
+ export async function discoverPluginSkills(pluginRoot, manifestKind, pluginNameFallback) {
343
+ const relPaths = manifestKind === 'skill-glob'
344
+ ? (await fileExists(path.join(pluginRoot, 'SKILL.md')))
345
+ ? ['SKILL.md']
346
+ : []
347
+ : await fg('**/SKILL.md', {
348
+ cwd: pluginRoot,
349
+ ignore: ['node_modules/**', '.git/**'],
350
+ onlyFiles: true,
351
+ dot: false,
352
+ }).catch(() => []);
353
+ if (relPaths.length === 0)
354
+ return undefined;
355
+ const out = {};
356
+ for (const rel of relPaths) {
357
+ const abs = path.join(pluginRoot, rel);
358
+ let text;
359
+ try {
360
+ text = await readFile(abs, 'utf-8');
361
+ }
362
+ catch {
363
+ continue;
364
+ }
365
+ const fm = /^---\n([\s\S]*?)\n---/m.exec(text);
366
+ let frontmatter = {};
367
+ if (fm) {
368
+ try {
369
+ frontmatter = (parseYaml(fm[1]) ?? {});
370
+ }
371
+ catch {
372
+ frontmatter = {};
373
+ }
374
+ }
375
+ const fmName = typeof frontmatter.name === 'string' && frontmatter.name.trim() !== ''
376
+ ? frontmatter.name.trim()
377
+ : null;
378
+ const dir = path.dirname(rel);
379
+ const dirName = dir === '.' ? null : path.basename(dir);
380
+ const scopedName = fmName ?? dirName ?? pluginNameFallback;
381
+ // First-encountered wins on collision (within a single plugin) — keeps
382
+ // the result deterministic when two SKILL.md files frontmatter-clash.
383
+ if (out[scopedName])
384
+ continue;
385
+ const description = typeof frontmatter.description === 'string' ? frontmatter.description : undefined;
386
+ out[scopedName] = { name: scopedName, description, relPath: rel };
387
+ }
388
+ return Object.keys(out).length > 0 ? out : undefined;
389
+ }
@@ -0,0 +1,7 @@
1
+ import { type RefreshOutcome } from './source_manager.js';
2
+ export interface RefreshOnStartResult {
3
+ sourceId: string;
4
+ outcome: RefreshOutcome;
5
+ }
6
+ export declare function refreshSourcesOnStart(): Promise<RefreshOnStartResult[]>;
7
+ //# sourceMappingURL=refresh_on_start.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refresh_on_start.d.ts","sourceRoot":"","sources":["../../src/plugins/refresh_on_start.ts"],"names":[],"mappings":"AAOA,OAAO,EAA8B,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEtF,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,cAAc,CAAC;CACzB;AAED,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,oBAAoB,EAAE,CAAC,CA2B7E"}
@@ -0,0 +1,35 @@
1
+ // Refresh-on-start: at app boot, kick a parallel refresh for every source
2
+ // flagged with refreshOnStart=true and resolve when they all settle.
3
+ //
4
+ // Callers fire this in the background — startup must not block on a slow
5
+ // git fetch. The next agent run picks up the freshly-discovered plugins
6
+ // automatically because the skill loader reads plugins.json each time.
7
+ import { listSources, refreshSource } from './source_manager.js';
8
+ export async function refreshSourcesOnStart() {
9
+ let sources;
10
+ try {
11
+ sources = await listSources();
12
+ }
13
+ catch (err) {
14
+ console.error('[refresh-on-start] failed to read plugins.json:', err);
15
+ return [];
16
+ }
17
+ const targets = sources.filter((s) => s.refreshOnStart);
18
+ if (targets.length === 0)
19
+ return [];
20
+ const results = await Promise.all(targets.map(async (s) => {
21
+ try {
22
+ const outcome = await refreshSource(s.id);
23
+ if (outcome.error) {
24
+ console.error(`[refresh-on-start] ${s.url}: ${outcome.error}`);
25
+ }
26
+ return { sourceId: s.id, outcome };
27
+ }
28
+ catch (err) {
29
+ const msg = err instanceof Error ? err.message : String(err);
30
+ console.error(`[refresh-on-start] ${s.url}: ${msg}`);
31
+ return { sourceId: s.id, outcome: { pluginsFound: 0, sha: null, error: msg } };
32
+ }
33
+ }));
34
+ return results;
35
+ }
@@ -0,0 +1,27 @@
1
+ import type { PluginRecord, PluginSource } from '../types.js';
2
+ export interface CreateSourceInput {
3
+ kind: 'git' | 'path';
4
+ url: string;
5
+ ref?: string | null;
6
+ authToken?: string | null;
7
+ refreshOnStart?: boolean;
8
+ }
9
+ export interface PatchSourceInput {
10
+ url?: string;
11
+ ref?: string | null;
12
+ authToken?: string | null;
13
+ refreshOnStart?: boolean;
14
+ }
15
+ export interface RefreshOutcome {
16
+ pluginsFound: number;
17
+ sha: string | null;
18
+ error: string | null;
19
+ }
20
+ export declare function createSource(input: CreateSourceInput): Promise<PluginSource>;
21
+ export declare function deleteSource(id: string): Promise<boolean>;
22
+ export declare function listSources(): Promise<PluginSource[]>;
23
+ export declare function getSource(id: string): Promise<PluginSource | null>;
24
+ export declare function listPlugins(): Promise<PluginRecord[]>;
25
+ export declare function patchSource(id: string, input: PatchSourceInput): Promise<PluginSource | null>;
26
+ export declare function refreshSource(id: string): Promise<RefreshOutcome>;
27
+ //# sourceMappingURL=source_manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source_manager.d.ts","sourceRoot":"","sources":["../../src/plugins/source_manager.ts"],"names":[],"mappings":"AA6BA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE9D,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,cAAc;IAC7B,YAAY,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAYD,wBAAsB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,YAAY,CAAC,CAiBlF;AAED,wBAAsB,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAc/D;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC,CAG3D;AAED,wBAAsB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAGxE;AAED,wBAAsB,WAAW,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC,CAG3D;AAED,wBAAsB,WAAW,CAC/B,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,gBAAgB,GACtB,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAe9B;AA+GD,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAMjE"}
@@ -0,0 +1,211 @@
1
+ // Source manager: orchestrates fetch + discover + persist for plugin
2
+ // sources. Ports the server's src/plugins/source_manager.ts adapted to the
3
+ // app's file-store: instead of DB transactions, every write reads
4
+ // plugins.json, mutates the in-memory shape, and writes back via savePlugins
5
+ // (which is atomic at the file level).
6
+ //
7
+ // Concurrency model: refreshSource() dedupes concurrent calls for the same
8
+ // source id via an inFlight map (matches the server). The app is
9
+ // single-process single-user TUI, so we don't lock the file across the
10
+ // whole pipeline — sequential CRUD is fine.
11
+ import { rm } from 'node:fs/promises';
12
+ import * as path from 'node:path';
13
+ import * as os from 'node:os';
14
+ import { randomUUID } from 'node:crypto';
15
+ import { loadPlugins, savePlugins } from '../store/json.js';
16
+ import { encrypt } from '../lib/encryption.js';
17
+ import { syncManagedMcpServers } from '../mcp/server_manager.js';
18
+ import { syncManagedAgents } from '../agents/sync.js';
19
+ import { discoverPlugins, discoverPluginMcpServers, discoverPluginAgents, discoverPluginCommands, discoverPluginSkills, } from './manifest.js';
20
+ import { fetchGit } from './fetchers/git.js';
21
+ import { fetchPath, validatePathInput } from './fetchers/path.js';
22
+ const inFlight = new Map();
23
+ function pluginCacheRoot() {
24
+ return process.env.PLUGIN_CACHE_DIR ?? path.join(os.homedir(), '.caretaker', 'plugin-cache');
25
+ }
26
+ function cacheDirForSource(id) {
27
+ return path.join(pluginCacheRoot(), id);
28
+ }
29
+ export async function createSource(input) {
30
+ if (input.kind === 'path')
31
+ validatePathInput(input.url);
32
+ const file = await loadPlugins();
33
+ const source = {
34
+ id: randomUUID(),
35
+ kind: input.kind,
36
+ url: input.url,
37
+ ref: input.kind === 'git' ? (input.ref ?? null) : null,
38
+ authToken: input.kind === 'git' && input.authToken ? encrypt(input.authToken) : null,
39
+ refreshOnStart: input.refreshOnStart ?? false,
40
+ lastFetchedAt: null,
41
+ lastFetchError: null,
42
+ lastFetchSha: null,
43
+ };
44
+ file.sources.push(source);
45
+ await savePlugins(file);
46
+ return source;
47
+ }
48
+ export async function deleteSource(id) {
49
+ const file = await loadPlugins();
50
+ const idx = file.sources.findIndex((s) => s.id === id);
51
+ if (idx === -1)
52
+ return false;
53
+ const [src] = file.sources.splice(idx, 1);
54
+ file.plugins = file.plugins.filter((p) => p.sourceId !== id);
55
+ await savePlugins(file);
56
+ // Cascade: managed MCP rows + agents whose owning plugin disappeared.
57
+ await syncManagedMcpServers();
58
+ await syncManagedAgents();
59
+ if (src.kind === 'git') {
60
+ await rm(cacheDirForSource(id), { recursive: true, force: true }).catch(() => { });
61
+ }
62
+ return true;
63
+ }
64
+ export async function listSources() {
65
+ const file = await loadPlugins();
66
+ return file.sources;
67
+ }
68
+ export async function getSource(id) {
69
+ const file = await loadPlugins();
70
+ return file.sources.find((s) => s.id === id) ?? null;
71
+ }
72
+ export async function listPlugins() {
73
+ const file = await loadPlugins();
74
+ return file.plugins;
75
+ }
76
+ export async function patchSource(id, input) {
77
+ const file = await loadPlugins();
78
+ const src = file.sources.find((s) => s.id === id);
79
+ if (!src)
80
+ return null;
81
+ if (input.url !== undefined) {
82
+ if (src.kind === 'path')
83
+ validatePathInput(input.url);
84
+ src.url = input.url;
85
+ }
86
+ if (input.ref !== undefined)
87
+ src.ref = input.ref;
88
+ if (input.authToken !== undefined) {
89
+ src.authToken = input.authToken === null ? null : encrypt(input.authToken);
90
+ }
91
+ if (input.refreshOnStart !== undefined)
92
+ src.refreshOnStart = input.refreshOnStart;
93
+ await savePlugins(file);
94
+ return src;
95
+ }
96
+ async function runRefresh(id) {
97
+ const initialFile = await loadPlugins();
98
+ const src = initialFile.sources.find((s) => s.id === id);
99
+ if (!src)
100
+ throw new Error(`Plugin source ${id} not found`);
101
+ let discovered;
102
+ let sha;
103
+ try {
104
+ const fetched = src.kind === 'git'
105
+ ? await fetchGit({ url: src.url, ref: src.ref ?? null, authToken: src.authToken ?? null }, cacheDirForSource(id))
106
+ : await fetchPath(src.url);
107
+ sha = fetched.sha;
108
+ discovered = await discoverPlugins(fetched.root);
109
+ // Second pass: each discovered plugin may declare MCP servers in
110
+ // `<plugin-root>/.mcp.json` (the official Claude Code convention — the
111
+ // file is a sibling of `.claude-plugin/`, not a field of plugin.json).
112
+ // We attach the parsed specs to the DiscoveredPlugin so the persistence
113
+ // step below carries them through, and the subsequent
114
+ // syncManagedMcpServers() call materializes one McpServerConfig per
115
+ // declared server.
116
+ for (const d of discovered) {
117
+ const pluginRoot = path.join(fetched.root, d.relPath);
118
+ const mcpServers = await discoverPluginMcpServers(pluginRoot);
119
+ if (mcpServers)
120
+ d.mcpServers = mcpServers;
121
+ const agents = await discoverPluginAgents(pluginRoot);
122
+ if (agents)
123
+ d.agents = agents;
124
+ const commands = await discoverPluginCommands(pluginRoot);
125
+ if (commands)
126
+ d.commands = commands;
127
+ const skills = await discoverPluginSkills(pluginRoot, d.manifestKind, d.name);
128
+ if (skills)
129
+ d.skills = skills;
130
+ }
131
+ }
132
+ catch (err) {
133
+ const msg = err instanceof Error ? err.message : String(err);
134
+ // Persist the failure on the source row, leave plugins from the previous
135
+ // successful refresh intact (matches the server: an outage shouldn't
136
+ // wipe activated skills).
137
+ const failureFile = await loadPlugins();
138
+ const failSrc = failureFile.sources.find((s) => s.id === id);
139
+ if (failSrc) {
140
+ failSrc.lastFetchError = msg;
141
+ failSrc.lastFetchedAt = new Date().toISOString();
142
+ await savePlugins(failureFile);
143
+ }
144
+ return { pluginsFound: 0, sha: null, error: msg };
145
+ }
146
+ // Successful refresh: replace this source's plugins atomically (read
147
+ // latest state to avoid stomping on parallel mutations to other sources).
148
+ const latest = await loadPlugins();
149
+ const ourSrc = latest.sources.find((s) => s.id === id);
150
+ if (!ourSrc) {
151
+ // Source was deleted between the start of refresh and now; bail without
152
+ // leaving orphans.
153
+ return { pluginsFound: 0, sha, error: 'source deleted during refresh' };
154
+ }
155
+ // Preserve PluginRecord.id across refreshes so downstream references
156
+ // (managed MCP rows' pluginId, managed agents' pluginId, and any agent
157
+ // that wires those managed rows by id) survive a re-sync. Without this,
158
+ // every refresh would assign a fresh UUID, syncManagedMcpServers /
159
+ // syncManagedAgents would drop the old rows + create new ones, and
160
+ // pruneAgentMcpRefs / the agent-sync equivalent would silently strip
161
+ // those refs from every agent that had them wired.
162
+ const previousIdByName = new Map();
163
+ for (const p of latest.plugins) {
164
+ if (p.sourceId === id)
165
+ previousIdByName.set(p.name, p.id);
166
+ }
167
+ latest.plugins = latest.plugins.filter((p) => p.sourceId !== id);
168
+ for (const d of discovered) {
169
+ latest.plugins.push({
170
+ id: previousIdByName.get(d.name) ?? randomUUID(),
171
+ sourceId: id,
172
+ name: d.name,
173
+ description: d.description,
174
+ manifestKind: d.manifestKind,
175
+ relPath: d.relPath,
176
+ rawManifest: d.rawManifest,
177
+ ...(d.mcpServers ? { mcpServers: d.mcpServers } : {}),
178
+ ...(d.agents ? { agents: d.agents } : {}),
179
+ ...(d.commands ? { commands: d.commands } : {}),
180
+ ...(d.skills ? { skills: d.skills } : {}),
181
+ });
182
+ }
183
+ ourSrc.lastFetchedAt = new Date().toISOString();
184
+ ourSrc.lastFetchError = null;
185
+ ourSrc.lastFetchSha = sha;
186
+ await savePlugins(latest);
187
+ // Reconcile mcp.json so newly-declared `mcpServers` entries become managed
188
+ // rows and stale ones disappear. Best-effort: a sync failure should not
189
+ // turn a successful refresh into a failure — the user can re-trigger.
190
+ try {
191
+ await syncManagedMcpServers();
192
+ }
193
+ catch (err) {
194
+ console.error(`[source manager] mcp sync after refresh failed:`, err);
195
+ }
196
+ try {
197
+ await syncManagedAgents();
198
+ }
199
+ catch (err) {
200
+ console.error(`[source manager] agents sync after refresh failed:`, err);
201
+ }
202
+ return { pluginsFound: discovered.length, sha, error: null };
203
+ }
204
+ export function refreshSource(id) {
205
+ const existing = inFlight.get(id);
206
+ if (existing)
207
+ return existing;
208
+ const promise = runRefresh(id).finally(() => inFlight.delete(id));
209
+ inFlight.set(id, promise);
210
+ return promise;
211
+ }