@openchamber/web 1.11.3 → 1.11.4

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 (40) hide show
  1. package/dist/assets/{JsonTreeView-4KDa8lGo.js → JsonTreeView-YQcDxcEK.js} +1 -1
  2. package/dist/assets/MarkdownRendererImpl-h6uFn35l.js +6 -0
  3. package/dist/assets/{MultiRunWindow-BBmUkZot.js → MultiRunWindow-BXZBRXf8.js} +1 -1
  4. package/dist/assets/{OnboardingScreen-CmfCd-NF.js → OnboardingScreen-AGACaSpL.js} +2 -2
  5. package/dist/assets/{SettingsWindow-Cju1ws5b.js → SettingsWindow-DbIrbkwH.js} +1 -1
  6. package/dist/assets/TerminalView-ClACTVsY.js +1 -0
  7. package/dist/assets/{ToolOutputDialog-v9jBbw8P.js → ToolOutputDialog-C9M1QAxc.js} +1 -1
  8. package/dist/assets/{es-jann2TW8.js → es-nmO_Sy-z.js} +4 -4
  9. package/dist/assets/index-BWxr-Sly.css +1 -0
  10. package/dist/assets/{ko-BI4PbFHS.js → ko-DgzWO49N.js} +4 -4
  11. package/dist/assets/{main-d-4uuoz4.js → main-BG6I-VaN.js} +2 -2
  12. package/dist/assets/main-DLomPR7m.js +227 -0
  13. package/dist/assets/miniChat-ata1oHqJ.js +2 -0
  14. package/dist/assets/{modelPrefsAutoSave-bm-G98P3.js → modelPrefsAutoSave-D4IOtl1d.js} +97 -98
  15. package/dist/assets/{pl-eehxx-j8.js → pl-CdOdV9f8.js} +3 -3
  16. package/dist/assets/{pt-BR-D34YtT2a.js → pt-BR-gg2QcqbT.js} +5 -5
  17. package/dist/assets/renderElectronMiniChatApp-F0L1QF1k.js +2 -0
  18. package/dist/assets/{uk-Ce5zhj4w.js → uk-CrnDWkt0.js} +4 -4
  19. package/dist/assets/{vendor-.bun-CJSRTa27.js → vendor-.bun-BIr1wmDP.js} +2 -10
  20. package/dist/assets/{zh-CN-DOs9jvCa.js → zh-CN-CWGQwc9R.js} +5 -5
  21. package/dist/index.html +4 -4
  22. package/dist/mini-chat.html +4 -4
  23. package/package.json +1 -1
  24. package/server/lib/opencode/DOCUMENTATION.md +4 -0
  25. package/server/lib/opencode/config-entity-routes.js +115 -0
  26. package/server/lib/opencode/core-routes.js +1 -0
  27. package/server/lib/opencode/core-routes.test.js +17 -1
  28. package/server/lib/opencode/feature-routes-runtime.js +12 -0
  29. package/server/lib/opencode/index.js +9 -0
  30. package/server/lib/opencode/routes.js +14 -1
  31. package/server/lib/opencode/snippets.js +233 -0
  32. package/server/lib/opencode/snippets.test.js +68 -0
  33. package/server/lib/scheduled-tasks/runtime.js +4 -3
  34. package/dist/assets/MarkdownRendererImpl-ShCeV10r.js +0 -6
  35. package/dist/assets/TerminalView-K9P6-TDg.js +0 -1
  36. package/dist/assets/index-BG2LvnTV.css +0 -1
  37. package/dist/assets/main-BCXofLIN.js +0 -225
  38. package/dist/assets/miniChat-DCx1bzpk.js +0 -2
  39. package/dist/assets/renderElectronMiniChatApp-exfEAtnm.js +0 -2
  40. /package/dist/assets/{index-CQjRYgmJ.js → index-Dc1XJWqt.js} +0 -0
package/dist/index.html CHANGED
@@ -532,10 +532,10 @@
532
532
  pointer-events: none;
533
533
  }
534
534
  </style>
535
- <script type="module" crossorigin src="/assets/main-d-4uuoz4.js"></script>
536
- <link rel="modulepreload" crossorigin href="/assets/index-CQjRYgmJ.js">
537
- <link rel="modulepreload" crossorigin href="/assets/vendor-.bun-CJSRTa27.js">
538
- <link rel="stylesheet" crossorigin href="/assets/index-BG2LvnTV.css">
535
+ <script type="module" crossorigin src="/assets/main-BG6I-VaN.js"></script>
536
+ <link rel="modulepreload" crossorigin href="/assets/index-Dc1XJWqt.js">
537
+ <link rel="modulepreload" crossorigin href="/assets/vendor-.bun-BIr1wmDP.js">
538
+ <link rel="stylesheet" crossorigin href="/assets/index-BWxr-Sly.css">
539
539
  <link rel="stylesheet" crossorigin href="/assets/vendor--V65Sl9C2.css">
540
540
  </head>
541
541
  <body class="h-full bg-background text-foreground">
@@ -4,10 +4,10 @@
4
4
  <meta charset="UTF-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
6
6
  <title>OpenChamber Mini Chat</title>
7
- <script type="module" crossorigin src="/assets/miniChat-DCx1bzpk.js"></script>
8
- <link rel="modulepreload" crossorigin href="/assets/index-CQjRYgmJ.js">
9
- <link rel="modulepreload" crossorigin href="/assets/vendor-.bun-CJSRTa27.js">
10
- <link rel="stylesheet" crossorigin href="/assets/index-BG2LvnTV.css">
7
+ <script type="module" crossorigin src="/assets/miniChat-ata1oHqJ.js"></script>
8
+ <link rel="modulepreload" crossorigin href="/assets/index-Dc1XJWqt.js">
9
+ <link rel="modulepreload" crossorigin href="/assets/vendor-.bun-BIr1wmDP.js">
10
+ <link rel="stylesheet" crossorigin href="/assets/index-BWxr-Sly.css">
11
11
  <link rel="stylesheet" crossorigin href="/assets/vendor--V65Sl9C2.css">
12
12
  </head>
13
13
  <body class="h-full bg-background text-foreground">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openchamber/web",
3
- "version": "1.11.3",
3
+ "version": "1.11.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./server/index.js",
@@ -18,6 +18,7 @@ This module provides OpenCode server integration utilities for the web server ru
18
18
  - `packages/web/server/lib/opencode/network-runtime.js`: OpenCode URL construction, health-probe readiness checks, and API prefix runtime.
19
19
  - `packages/web/server/lib/opencode/project-directory-runtime.js`: request-scoped and settings-backed project directory resolution/validation runtime.
20
20
  - `packages/web/server/lib/opencode/config-entity-routes.js`: route registration for agent/command/MCP config orchestration and reload semantics.
21
+ - `packages/web/server/lib/opencode/snippets.js`: opencode-snippets-compatible snippet file CRUD, discovery, and hashtag expansion.
21
22
  - `packages/web/server/lib/opencode/cli-options.js`: CLI/environment option parsing for server startup arguments.
22
23
  - `packages/web/server/lib/opencode/core-routes.js`: server status/system routes, auth/access guard routes, and settings utility route registration.
23
24
  - `packages/web/server/lib/opencode/shutdown-runtime.js`: graceful shutdown orchestration runtime for watcher/session/terminal/process/server teardown.
@@ -73,6 +74,8 @@ This module provides OpenCode server integration utilities for the web server ru
73
74
  - `GET /api/config/settings`
74
75
  - `PUT /api/config/settings`
75
76
  - `GET /api/config/opencode-resolution`
77
+ - `POST /api/opencode/upgrade` (proxies OpenCode upgrade, then restarts managed OpenCode so the new binary is active)
78
+ - `GET /api/opencode/upgrade-status`
76
79
  - `POST /api/opencode/directory`
77
80
  - `GET /api/provider/:providerId/source`
78
81
  - `DELETE /api/provider/:providerId/auth`
@@ -209,6 +212,7 @@ This module provides OpenCode server integration utilities for the web server ru
209
212
  - Agents: `/api/config/agents/:name` and `/api/config/agents/:name/config`
210
213
  - Commands: `/api/config/commands/:name`
211
214
  - MCP servers: `/api/config/mcp` and `/api/config/mcp/:name`
215
+ - Snippets: `/api/config/snippets`, `/api/config/snippets/:name`, and `/api/config/snippets/expand`
212
216
 
213
217
  ## Public exports (auth-state-runtime.js)
214
218
  - `createOpenCodeAuthStateRuntime(dependencies)`: creates runtime for managed OpenCode auth password state and request headers.
@@ -18,6 +18,12 @@ export const registerConfigEntityRoutes = (app, dependencies) => {
18
18
  createMcpConfig,
19
19
  updateMcpConfig,
20
20
  deleteMcpConfig,
21
+ listSnippets,
22
+ getSnippet,
23
+ createSnippet,
24
+ updateSnippet,
25
+ deleteSnippet,
26
+ expandSnippets,
21
27
  } = dependencies;
22
28
 
23
29
  const completeMcpMutation = async (res, action, name, applyChange) => {
@@ -367,4 +373,113 @@ export const registerConfigEntityRoutes = (app, dependencies) => {
367
373
  res.status(500).json({ error: error.message || 'Failed to delete command' });
368
374
  }
369
375
  });
376
+
377
+ app.get('/api/config/snippets', async (req, res) => {
378
+ try {
379
+ const { directory, error } = await resolveOptionalProjectDirectory(req);
380
+ if (error) {
381
+ return res.status(400).json({ error });
382
+ }
383
+ res.json(listSnippets(directory));
384
+ } catch (error) {
385
+ console.error('[API:GET /api/config/snippets] Failed:', error);
386
+ res.status(500).json({ error: error.message || 'Failed to list snippets' });
387
+ }
388
+ });
389
+
390
+ app.post('/api/config/snippets/expand', async (req, res) => {
391
+ try {
392
+ const { directory, error } = await resolveOptionalProjectDirectory(req);
393
+ if (error) {
394
+ return res.status(400).json({ error });
395
+ }
396
+ res.json({ text: expandSnippets(req.body?.text ?? '', directory) });
397
+ } catch (error) {
398
+ console.error('[API:POST /api/config/snippets/expand] Failed:', error);
399
+ res.status(500).json({ error: error.message || 'Failed to expand snippets' });
400
+ }
401
+ });
402
+
403
+ app.get('/api/config/snippets/:name', async (req, res) => {
404
+ try {
405
+ const name = req.params.name;
406
+ const { directory, error } = await resolveOptionalProjectDirectory(req);
407
+ if (error) {
408
+ return res.status(400).json({ error });
409
+ }
410
+ const snippet = getSnippet(name, directory);
411
+ if (!snippet) {
412
+ return res.status(404).json({ error: `Snippet "${name}" not found` });
413
+ }
414
+ res.json(snippet);
415
+ } catch (error) {
416
+ console.error('[API:GET /api/config/snippets/:name] Failed:', error);
417
+ if (error.message?.includes('Snippet name')) {
418
+ return res.status(400).json({ error: error.message });
419
+ }
420
+ res.status(500).json({ error: error.message || 'Failed to get snippet' });
421
+ }
422
+ });
423
+
424
+ app.post('/api/config/snippets/:name', async (req, res) => {
425
+ try {
426
+ const name = req.params.name;
427
+ const { directory, error } = await resolveOptionalProjectDirectory(req);
428
+ if (error) {
429
+ return res.status(400).json({ error });
430
+ }
431
+ const snippet = createSnippet(name, req.body || {}, directory, req.body?.scope || 'global');
432
+ res.json({ success: true, snippet });
433
+ } catch (error) {
434
+ console.error('[API:POST /api/config/snippets/:name] Failed:', error);
435
+ if (error.message?.includes('already exists')) {
436
+ return res.status(409).json({ error: error.message });
437
+ }
438
+ if (error.message?.includes('Snippet name') || error.message?.includes('Project directory')) {
439
+ return res.status(400).json({ error: error.message });
440
+ }
441
+ res.status(500).json({ error: error.message || 'Failed to create snippet' });
442
+ }
443
+ });
444
+
445
+ app.patch('/api/config/snippets/:name', async (req, res) => {
446
+ try {
447
+ const name = req.params.name;
448
+ const { directory, error } = await resolveOptionalProjectDirectory(req);
449
+ if (error) {
450
+ return res.status(400).json({ error });
451
+ }
452
+ res.json({ success: true, snippet: updateSnippet(name, req.body || {}, directory) });
453
+ } catch (error) {
454
+ console.error('[API:PATCH /api/config/snippets/:name] Failed:', error);
455
+ if (error.message?.includes('not found')) {
456
+ return res.status(404).json({ error: error.message });
457
+ }
458
+ if (error.message?.includes('Snippet name')) {
459
+ return res.status(400).json({ error: error.message });
460
+ }
461
+ res.status(500).json({ error: error.message || 'Failed to update snippet' });
462
+ }
463
+ });
464
+
465
+ app.delete('/api/config/snippets/:name', async (req, res) => {
466
+ try {
467
+ const name = req.params.name;
468
+ const { directory, error } = await resolveOptionalProjectDirectory(req);
469
+ if (error) {
470
+ return res.status(400).json({ error });
471
+ }
472
+ deleteSnippet(name, directory);
473
+ res.json({ success: true });
474
+ } catch (error) {
475
+ console.error('[API:DELETE /api/config/snippets/:name] Failed:', error);
476
+ if (error.message?.includes('not found')) {
477
+ return res.status(404).json({ error: error.message });
478
+ }
479
+ if (error.message?.includes('Snippet name')) {
480
+ return res.status(400).json({ error: error.message });
481
+ }
482
+ res.status(500).json({ error: error.message || 'Failed to delete snippet' });
483
+ }
484
+ });
370
485
  };
@@ -464,6 +464,7 @@ export const registerCommonRequestMiddleware = (app, dependencies) => {
464
464
  req.path.startsWith('/api/config/agents') ||
465
465
  req.path.startsWith('/api/config/commands') ||
466
466
  req.path.startsWith('/api/config/mcp') ||
467
+ req.path.startsWith('/api/config/snippets') ||
467
468
  req.path.startsWith('/api/config/settings') ||
468
469
  req.path.startsWith('/api/config/skills') ||
469
470
  req.path.startsWith('/api/projects') ||
@@ -1,7 +1,7 @@
1
1
  import { describe, it, expect, vi } from 'vitest';
2
2
  import express from 'express';
3
3
  import request from 'supertest';
4
- import { registerServerStatusRoutes } from './core-routes.js';
4
+ import { registerCommonRequestMiddleware, registerServerStatusRoutes } from './core-routes.js';
5
5
 
6
6
  describe('core-routes', () => {
7
7
  it('should call gracefulShutdown with exitProcess: true on /api/system/shutdown', async () => {
@@ -14,6 +14,7 @@ describe('core-routes', () => {
14
14
  getHealthSnapshot: () => ({ status: 'ok' }),
15
15
  openchamberVersion: '1.0.0',
16
16
  runtimeName: 'test',
17
+ express,
17
18
  };
18
19
 
19
20
  registerServerStatusRoutes(app, dependencies);
@@ -23,4 +24,19 @@ describe('core-routes', () => {
23
24
  expect(dependencies.gracefulShutdown).toHaveBeenCalled();
24
25
  expect(shutdownOpts).toEqual({ exitProcess: true });
25
26
  });
27
+
28
+ it('should parse JSON bodies for snippet config routes', async () => {
29
+ const app = express();
30
+ registerCommonRequestMiddleware(app, { express });
31
+ app.post('/api/config/snippets/example', (req, res) => {
32
+ res.json({ body: req.body });
33
+ });
34
+
35
+ const response = await request(app)
36
+ .post('/api/config/snippets/example')
37
+ .send({ content: 'Snippet body' })
38
+ .expect(200);
39
+
40
+ expect(response.body).toEqual({ body: { content: 'Snippet body' } });
41
+ });
26
42
  });
@@ -123,6 +123,12 @@ export const createFeatureRoutesRuntime = (dependencies) => {
123
123
  createMcpConfig,
124
124
  updateMcpConfig,
125
125
  deleteMcpConfig,
126
+ listSnippets,
127
+ getSnippet,
128
+ createSnippet,
129
+ updateSnippet,
130
+ deleteSnippet,
131
+ expandSnippets,
126
132
  } = await import('./index.js');
127
133
 
128
134
  registerConfigEntityRoutes(app, {
@@ -144,6 +150,12 @@ export const createFeatureRoutesRuntime = (dependencies) => {
144
150
  createMcpConfig,
145
151
  updateMcpConfig,
146
152
  deleteMcpConfig,
153
+ listSnippets,
154
+ getSnippet,
155
+ createSnippet,
156
+ updateSnippet,
157
+ deleteSnippet,
158
+ expandSnippets,
147
159
  });
148
160
 
149
161
  const {
@@ -64,3 +64,12 @@ export {
64
64
  updateMcpConfig,
65
65
  deleteMcpConfig,
66
66
  } from './mcp.js';
67
+
68
+ export {
69
+ listSnippets,
70
+ getSnippet,
71
+ createSnippet,
72
+ updateSnippet,
73
+ deleteSnippet,
74
+ expandSnippets,
75
+ } from './snippets.js';
@@ -155,7 +155,20 @@ export const registerOpenCodeRoutes = (app, dependencies) => {
155
155
  error: payload?.error || response.statusText || 'Failed to upgrade OpenCode',
156
156
  });
157
157
  }
158
- return res.json(payload ?? { success: true });
158
+
159
+ try {
160
+ await refreshOpenCodeAfterConfigChange('OpenCode upgrade');
161
+ } catch (restartError) {
162
+ return res.status(500).json({
163
+ success: false,
164
+ upgraded: true,
165
+ error: restartError instanceof Error
166
+ ? `OpenCode upgraded, but restart failed: ${restartError.message}`
167
+ : 'OpenCode upgraded, but restart failed',
168
+ });
169
+ }
170
+
171
+ return res.json({ ...(payload ?? { success: true }), restarted: true });
159
172
  } catch (error) {
160
173
  console.error('Failed to upgrade OpenCode:', error);
161
174
  return res.status(500).json({
@@ -0,0 +1,233 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import os from 'os';
4
+ import yaml from 'yaml';
5
+
6
+ const OPENCODE_CONFIG_DIR = path.join(os.homedir(), '.config', 'opencode');
7
+ const GLOBAL_SNIPPET_DIR = path.join(OPENCODE_CONFIG_DIR, 'snippet');
8
+ const GLOBAL_SNIPPET_DIR_ALT = path.join(OPENCODE_CONFIG_DIR, 'snippets');
9
+ const SNIPPET_EXTENSION = '.md';
10
+ const SNIPPET_NAME_PATTERN = /^[a-z0-9][a-z0-9_-]{0,79}$/i;
11
+ const HASHTAG_PATTERN = /#([a-z0-9_-]+)/gi;
12
+ const MAX_EXPANSION_COUNT = 15;
13
+
14
+ function getProjectSnippetDirs(workingDirectory) {
15
+ if (!workingDirectory) return [];
16
+ return [
17
+ path.join(workingDirectory, '.opencode', 'snippets'),
18
+ path.join(workingDirectory, '.opencode', 'snippet'),
19
+ ];
20
+ }
21
+
22
+ function getGlobalSnippetDirs() {
23
+ return [GLOBAL_SNIPPET_DIR_ALT, GLOBAL_SNIPPET_DIR];
24
+ }
25
+
26
+ function getLoadDirs(workingDirectory) {
27
+ return [
28
+ ...getGlobalSnippetDirs().map((dir) => ({ dir, source: 'global' })),
29
+ ...getProjectSnippetDirs(workingDirectory).map((dir) => ({ dir, source: 'project' })),
30
+ ];
31
+ }
32
+
33
+ function assertValidSnippetName(name) {
34
+ if (typeof name !== 'string' || !SNIPPET_NAME_PATTERN.test(name)) {
35
+ throw new Error('Snippet name must use letters, numbers, dashes, or underscores');
36
+ }
37
+ }
38
+
39
+ function parseMarkdownFile(filePath) {
40
+ const content = fs.readFileSync(filePath, 'utf8');
41
+ const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
42
+ if (!match) {
43
+ return { frontmatter: {}, body: content.trim() };
44
+ }
45
+ return {
46
+ frontmatter: yaml.parse(match[1]) || {},
47
+ body: match[2].trim(),
48
+ };
49
+ }
50
+
51
+ function normalizeAliases(frontmatter) {
52
+ const raw = frontmatter.aliases ?? frontmatter.alias;
53
+ if (!raw) return [];
54
+ const aliases = Array.isArray(raw) ? raw : [raw];
55
+ return aliases.map((alias) => String(alias).trim()).filter(Boolean);
56
+ }
57
+
58
+ function writeMarkdownFile(filePath, { content, aliases = [], description }) {
59
+ const frontmatter = {};
60
+ const normalizedAliases = aliases.map((alias) => String(alias).trim()).filter(Boolean);
61
+ if (normalizedAliases.length > 0) frontmatter.aliases = normalizedAliases;
62
+ if (description?.trim()) frontmatter.description = description.trim();
63
+
64
+ const body = content ?? '';
65
+ const output = Object.keys(frontmatter).length > 0
66
+ ? `---\n${yaml.stringify(frontmatter)}---\n${body ? `\n${body}` : ''}`
67
+ : body;
68
+
69
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
70
+ fs.writeFileSync(filePath, output, 'utf8');
71
+ }
72
+
73
+ function loadSnippetFile(dir, filename, source) {
74
+ const name = path.basename(filename, SNIPPET_EXTENSION);
75
+ if (!SNIPPET_NAME_PATTERN.test(name)) return null;
76
+ const filePath = path.join(dir, filename);
77
+ const { frontmatter, body } = parseMarkdownFile(filePath);
78
+ return {
79
+ name,
80
+ content: body,
81
+ aliases: normalizeAliases(frontmatter),
82
+ description: typeof frontmatter.description === 'string' ? frontmatter.description : undefined,
83
+ filePath,
84
+ source,
85
+ };
86
+ }
87
+
88
+ function registerSnippet(registry, snippet) {
89
+ const key = snippet.name.toLowerCase();
90
+ const existing = registry.get(key);
91
+ if (existing) {
92
+ for (const alias of existing.aliases) registry.delete(alias.toLowerCase());
93
+ }
94
+ registry.set(key, snippet);
95
+ for (const alias of snippet.aliases) {
96
+ if (SNIPPET_NAME_PATTERN.test(alias)) registry.set(alias.toLowerCase(), snippet);
97
+ }
98
+ }
99
+
100
+ function loadSnippetRegistry(workingDirectory) {
101
+ const registry = new Map();
102
+ for (const { dir, source } of getLoadDirs(workingDirectory)) {
103
+ if (!fs.existsSync(dir)) continue;
104
+ for (const filename of fs.readdirSync(dir)) {
105
+ if (!filename.endsWith(SNIPPET_EXTENSION)) continue;
106
+ try {
107
+ const snippet = loadSnippetFile(dir, filename, source);
108
+ if (snippet) registerSnippet(registry, snippet);
109
+ } catch (error) {
110
+ console.warn(`[Snippets] Failed to load ${path.join(dir, filename)}:`, error);
111
+ }
112
+ }
113
+ }
114
+ return registry;
115
+ }
116
+
117
+ function listUniqueSnippets(registry) {
118
+ const seen = new Set();
119
+ const snippets = [];
120
+ for (const snippet of registry.values()) {
121
+ const key = `${snippet.source}:${snippet.filePath}`;
122
+ if (seen.has(key)) continue;
123
+ seen.add(key);
124
+ snippets.push(snippet);
125
+ }
126
+ return snippets.sort((a, b) => a.name.localeCompare(b.name));
127
+ }
128
+
129
+ function getWritableSnippetDir(scope, workingDirectory) {
130
+ if (scope === 'project') {
131
+ if (!workingDirectory) throw new Error('Project directory is required for project snippets');
132
+ const preferred = path.join(workingDirectory, '.opencode', 'snippet');
133
+ const alternate = path.join(workingDirectory, '.opencode', 'snippets');
134
+ return fs.existsSync(alternate) && !fs.existsSync(preferred) ? alternate : preferred;
135
+ }
136
+ return fs.existsSync(GLOBAL_SNIPPET_DIR_ALT) && !fs.existsSync(GLOBAL_SNIPPET_DIR)
137
+ ? GLOBAL_SNIPPET_DIR_ALT
138
+ : GLOBAL_SNIPPET_DIR;
139
+ }
140
+
141
+ function findSnippetByName(name, workingDirectory) {
142
+ assertValidSnippetName(name);
143
+ const registry = loadSnippetRegistry(workingDirectory);
144
+ return registry.get(name.toLowerCase()) ?? null;
145
+ }
146
+
147
+ function parseSnippetBlocks(content) {
148
+ const blocks = { prepend: [], append: [] };
149
+ let inline = content;
150
+ for (const type of ['prepend', 'append']) {
151
+ const regex = new RegExp(`<${type}>([\\s\\S]*?)(?:<\\/${type}>|$)`, 'gi');
152
+ inline = inline.replace(regex, (_match, value) => {
153
+ const normalized = String(value).trim();
154
+ if (normalized) blocks[type].push(normalized);
155
+ return '';
156
+ });
157
+ }
158
+ inline = inline.replace(/<inject>[\s\S]*?(?:<\/inject>|$)/gi, '').trim();
159
+ return { inline, prepend: blocks.prepend, append: blocks.append };
160
+ }
161
+
162
+ function expandText(text, registry, expansionCounts, collector) {
163
+ let expanded = text;
164
+ let changed = true;
165
+
166
+ while (changed) {
167
+ const previous = expanded;
168
+ let loopDetected = false;
169
+ HASHTAG_PATTERN.lastIndex = 0;
170
+
171
+ expanded = expanded.replace(HASHTAG_PATTERN, (match, name, offset, input) => {
172
+ if (name.toLowerCase() === 'skill' && input[offset + match.length] === '(') return match;
173
+ const snippet = registry.get(name.toLowerCase());
174
+ if (!snippet) return match;
175
+
176
+ const key = snippet.name.toLowerCase();
177
+ const count = (expansionCounts.get(key) || 0) + 1;
178
+ if (count > MAX_EXPANSION_COUNT) {
179
+ loopDetected = true;
180
+ return match;
181
+ }
182
+ expansionCounts.set(key, count);
183
+
184
+ const parsed = parseSnippetBlocks(snippet.content);
185
+ for (const block of parsed.prepend) collector.prepend.push(expandText(block, registry, expansionCounts, collector));
186
+ for (const block of parsed.append) collector.append.push(expandText(block, registry, expansionCounts, collector));
187
+ return expandText(parsed.inline, registry, expansionCounts, collector);
188
+ });
189
+
190
+ changed = expanded !== previous && !loopDetected;
191
+ }
192
+
193
+ return expanded;
194
+ }
195
+
196
+ export function listSnippets(workingDirectory) {
197
+ return listUniqueSnippets(loadSnippetRegistry(workingDirectory));
198
+ }
199
+
200
+ export function getSnippet(name, workingDirectory) {
201
+ return findSnippetByName(name, workingDirectory);
202
+ }
203
+
204
+ export function createSnippet(name, config, workingDirectory, scope = 'global') {
205
+ assertValidSnippetName(name);
206
+ const dir = getWritableSnippetDir(scope, workingDirectory);
207
+ const filePath = path.join(dir, `${name}${SNIPPET_EXTENSION}`);
208
+ if (fs.existsSync(filePath)) throw new Error(`Snippet "${name}" already exists`);
209
+ writeMarkdownFile(filePath, config || {});
210
+ return getSnippet(name, workingDirectory);
211
+ }
212
+
213
+ export function updateSnippet(name, updates, workingDirectory) {
214
+ const existing = findSnippetByName(name, workingDirectory);
215
+ if (!existing) throw new Error(`Snippet "${name}" not found`);
216
+ writeMarkdownFile(existing.filePath, { ...existing, ...(updates || {}) });
217
+ return getSnippet(name, workingDirectory);
218
+ }
219
+
220
+ export function deleteSnippet(name, workingDirectory) {
221
+ const existing = findSnippetByName(name, workingDirectory);
222
+ if (!existing) throw new Error(`Snippet "${name}" not found`);
223
+ fs.unlinkSync(existing.filePath);
224
+ }
225
+
226
+ export function expandSnippets(text, workingDirectory) {
227
+ const registry = loadSnippetRegistry(workingDirectory);
228
+ const collector = { prepend: [], append: [] };
229
+ const expanded = expandText(text || '', registry, new Map(), collector).trim();
230
+ return [...collector.prepend, expanded, ...collector.append].filter(Boolean).join('\n\n');
231
+ }
232
+
233
+ export { assertValidSnippetName };
@@ -0,0 +1,68 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
2
+ import fs from 'fs';
3
+ import os from 'os';
4
+ import path from 'path';
5
+ import {
6
+ createSnippet,
7
+ deleteSnippet,
8
+ expandSnippets,
9
+ getSnippet,
10
+ listSnippets,
11
+ updateSnippet,
12
+ } from './snippets.js';
13
+
14
+ let projectDir;
15
+
16
+ function writeSnippet(relativePath, content) {
17
+ const filePath = path.join(projectDir, relativePath);
18
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
19
+ fs.writeFileSync(filePath, content, 'utf8');
20
+ }
21
+
22
+ describe('snippets', () => {
23
+ beforeEach(() => {
24
+ projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-snippets-'));
25
+ });
26
+
27
+ afterEach(() => {
28
+ fs.rmSync(projectDir, { recursive: true, force: true });
29
+ });
30
+
31
+ test('loads project snippets with aliases and description', () => {
32
+ writeSnippet('.opencode/snippet/review.md', '---\naliases: [rev]\ndescription: Review helper\n---\nReview carefully.');
33
+
34
+ expect(listSnippets(projectDir)).toContainEqual(
35
+ expect.objectContaining({ name: 'review', aliases: ['rev'], description: 'Review helper', source: 'project' }),
36
+ );
37
+ expect(getSnippet('rev', projectDir)).toEqual(expect.objectContaining({ name: 'review' }));
38
+ });
39
+
40
+ test('snippet directory wins over snippets directory', () => {
41
+ writeSnippet('.opencode/snippets/same.md', 'Old');
42
+ writeSnippet('.opencode/snippet/same.md', 'New');
43
+
44
+ expect(getSnippet('same', projectDir)?.content).toBe('New');
45
+ });
46
+
47
+ test('creates updates and deletes snippets', () => {
48
+ expect(createSnippet('custom-one', { content: 'Body', aliases: ['co'] }, projectDir, 'project')).toEqual(
49
+ expect.objectContaining({ name: 'custom-one', content: 'Body', aliases: ['co'] }),
50
+ );
51
+ expect(updateSnippet('custom-one', { content: 'Updated' }, projectDir)).toEqual(
52
+ expect.objectContaining({ name: 'custom-one', content: 'Updated', aliases: ['co'] }),
53
+ );
54
+ deleteSnippet('custom-one', projectDir);
55
+ expect(getSnippet('custom-one', projectDir)).toBeNull();
56
+ });
57
+
58
+ test('expands snippets recursively with prepend and append blocks', () => {
59
+ writeSnippet('.opencode/snippet/base.md', 'Base text');
60
+ writeSnippet('.opencode/snippet/review.md', '<prepend>Before</prepend>Review #base<append>After</append>');
61
+
62
+ expect(expandSnippets('Please #review', projectDir)).toBe('Before\n\nPlease Review Base text\n\nAfter');
63
+ });
64
+
65
+ test('rejects invalid snippet names', () => {
66
+ expect(() => createSnippet('../bad', { content: '' }, projectDir, 'project')).toThrow('Snippet name');
67
+ });
68
+ });
@@ -1,6 +1,7 @@
1
1
  import { createOpencodeClient } from '@opencode-ai/sdk/v2';
2
2
  import { DateTime } from 'luxon';
3
3
  import parser from 'cron-parser';
4
+ import { expandSnippets } from '../opencode/snippets.js';
4
5
 
5
6
  const DEFAULT_GLOBAL_CONCURRENCY = 4;
6
7
  const DEFAULT_PROJECT_CONCURRENCY = 2;
@@ -405,7 +406,7 @@ export const createScheduledTasksRuntime = (deps) => {
405
406
  return projectRunning < maxProjectConcurrency;
406
407
  };
407
408
 
408
- const buildPromptAsyncPayload = (task) => ({
409
+ const buildPromptAsyncPayload = (task, projectPath) => ({
409
410
  model: {
410
411
  providerID: task.execution.providerID,
411
412
  modelID: task.execution.modelID,
@@ -415,7 +416,7 @@ export const createScheduledTasksRuntime = (deps) => {
415
416
  parts: [
416
417
  {
417
418
  type: 'text',
418
- text: task.execution.prompt,
419
+ text: expandSnippets(task.execution.prompt, projectPath),
419
420
  },
420
421
  ],
421
422
  });
@@ -430,7 +431,7 @@ export const createScheduledTasksRuntime = (deps) => {
430
431
  'content-type': 'application/json',
431
432
  accept: 'application/json',
432
433
  },
433
- body: JSON.stringify(buildPromptAsyncPayload(task)),
434
+ body: JSON.stringify(buildPromptAsyncPayload(task, projectPath)),
434
435
  });
435
436
 
436
437
  if (!response.ok) {