opencode-work-context 0.1.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.
- package/LICENSE +21 -0
- package/README.md +58 -0
- package/bin/opencode-work-context.js +269 -0
- package/commands/wc.md +27 -0
- package/package.json +52 -0
- package/plugin/work-context.js +21 -0
- package/src/core.js +304 -0
- package/src/errors.js +11 -0
- package/src/index.js +8 -0
- package/src/knowledge.js +116 -0
- package/src/markdown.js +37 -0
- package/src/opencode-adapter.js +16 -0
- package/src/projections.js +30 -0
- package/src/sessions.js +20 -0
- package/src/storage.js +254 -0
- package/src/title.js +6 -0
- package/src/tool-definitions.js +30 -0
- package/tools/work_context_add_knowledge.js +1 -0
- package/tools/work_context_add_stage.js +2 -0
- package/tools/work_context_close_session.js +2 -0
- package/tools/work_context_create_workspace.js +2 -0
- package/tools/work_context_finish_stage.js +2 -0
- package/tools/work_context_handoff_stage.js +2 -0
- package/tools/work_context_help.js +2 -0
- package/tools/work_context_link_issue.js +2 -0
- package/tools/work_context_list_knowledge.js +1 -0
- package/tools/work_context_list_stages.js +2 -0
- package/tools/work_context_list_workspaces.js +2 -0
- package/tools/work_context_rename_session.js +2 -0
- package/tools/work_context_start_session.js +2 -0
- package/tools/work_context_supersede_knowledge.js +1 -0
- package/tools/work_context_update_knowledge.js +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nikita Mosiyash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# opencode-work-context
|
|
2
|
+
|
|
3
|
+
Filesystem work-context core and OpenCode adapter for standalone projects. The
|
|
4
|
+
package is ESM and has no dependency on Mezzio or PHP.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
Run from the root of a standalone OpenCode project:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npx opencode-work-context init
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`init` installs `opencode-work-context@0.1.0` as an exact devDependency using
|
|
15
|
+
standard `npm`, preserving normal lifecycle behavior for the host project. It
|
|
16
|
+
creates `package.json` when absent and writes the integration files below.
|
|
17
|
+
It does not create a workspace. Start one explicitly with `/wc create "Title"`.
|
|
18
|
+
|
|
19
|
+
Use `--force` to replace a conflicting package-owned generated file. Without it,
|
|
20
|
+
existing files are never silently overwritten. Re-running with unchanged files is
|
|
21
|
+
safe and idempotent.
|
|
22
|
+
|
|
23
|
+
Generated files:
|
|
24
|
+
|
|
25
|
+
- `.work-context/config.yaml` for filesystem configuration;
|
|
26
|
+
- `.opencode/commands/wc.md` for the `/wc` command contract;
|
|
27
|
+
- `.opencode/plugins/work-context.js` as a thin loader from the installed package;
|
|
28
|
+
- `.work-context/local/` in `.gitignore` for personal session events.
|
|
29
|
+
- `<workspace>/KNOWLEDGE.md` is the canonical durable knowledge ledger and is
|
|
30
|
+
created on the first explicit knowledge operation.
|
|
31
|
+
|
|
32
|
+
Tools are registered by the installed plugin and are not copied into the project.
|
|
33
|
+
Knowledge operations are explicit: list, add, update, and supersede. Finishing a
|
|
34
|
+
stage requires declaring whether its knowledge review was `added` or `none`.
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT. Copyright (c) 2026 Nikita Mosiyash.
|
|
39
|
+
|
|
40
|
+
## Manual fallback
|
|
41
|
+
|
|
42
|
+
For an offline or advanced setup, install the package as a devDependency and add
|
|
43
|
+
the equivalent plugin loader and `/wc` command from this repository. Keep the
|
|
44
|
+
plugin import pointed at `opencode-work-context/plugin`; do not copy `src/` or
|
|
45
|
+
individual tools into the project.
|
|
46
|
+
|
|
47
|
+
## Development
|
|
48
|
+
|
|
49
|
+
The package layout is intentionally small:
|
|
50
|
+
|
|
51
|
+
- `src/` contains the dependency-free core, storage, projections, and adapter;
|
|
52
|
+
- `plugin/` exposes the OpenCode plugin contract;
|
|
53
|
+
- `tools/` exposes individual tool contracts;
|
|
54
|
+
- `bin/` contains the `init` CLI;
|
|
55
|
+
- `commands/` contains the reusable `/wc` prompt;
|
|
56
|
+
- `test/` contains core contract tests.
|
|
57
|
+
|
|
58
|
+
Run `npm test` when explicitly verifying a checkout.
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { defaultConfig } from "../src/storage.js";
|
|
9
|
+
|
|
10
|
+
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
11
|
+
const packageManifest = JSON.parse(fs.readFileSync(path.join(packageRoot, "package.json"), "utf8"));
|
|
12
|
+
const projectRoot = process.cwd();
|
|
13
|
+
const command = [
|
|
14
|
+
"---",
|
|
15
|
+
"description: Explicit work-context lifecycle commands backed by installed package tools",
|
|
16
|
+
"agent: build",
|
|
17
|
+
"subtask: false",
|
|
18
|
+
"---",
|
|
19
|
+
"",
|
|
20
|
+
"Interpret `/wc $ARGUMENTS` as an explicit work-context operation. Use the matching",
|
|
21
|
+
"`work_context_*` custom tool registered by `.opencode/plugins/work-context.js`.",
|
|
22
|
+
"Never edit work-context Markdown, JSONL, INDEX, or SESSIONS projections directly.",
|
|
23
|
+
"For mutating operations, report the structured result and stable error code. Use",
|
|
24
|
+
"`work_context_help` when the syntax is missing or ambiguous.",
|
|
25
|
+
"",
|
|
26
|
+
"Supported syntax:",
|
|
27
|
+
"",
|
|
28
|
+
"- `help [command]`",
|
|
29
|
+
"- `create \"title\"`",
|
|
30
|
+
"- `list` or `<workspace> list`",
|
|
31
|
+
"- `resume <workspace> <stage>`",
|
|
32
|
+
"- `add-stage <workspace> \"title\"`",
|
|
33
|
+
"- `<workspace> [<stage>] link-issue <URL>`",
|
|
34
|
+
"- `session rename \"summary\"`",
|
|
35
|
+
"- `session close`",
|
|
36
|
+
"- `handoff <workspace> <stage>`",
|
|
37
|
+
"- `finish <workspace> <stage>` (requires explicit knowledge review: `added` or `none`)",
|
|
38
|
+
"- `knowledge list|add|update|supersede <workspace> ...`",
|
|
39
|
+
"",
|
|
40
|
+
"Arguments: $ARGUMENTS",
|
|
41
|
+
"",
|
|
42
|
+
].join("\n");
|
|
43
|
+
const generated = new Map([
|
|
44
|
+
[path.join(".work-context", "config.yaml"), defaultConfig()],
|
|
45
|
+
[path.join(".opencode", "commands", "wc.md"), command],
|
|
46
|
+
[path.join(".opencode", "plugins", "work-context.js"), `// Thin project integration; tools remain in the installed npm package.
|
|
47
|
+
export { default } from "${packageManifest.name}/plugin";
|
|
48
|
+
`],
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const fail = (message) => {
|
|
52
|
+
console.error(`opencode-work-context: ${message}`);
|
|
53
|
+
process.exitCode = 1;
|
|
54
|
+
};
|
|
55
|
+
const exists = (file) => { try { fs.lstatSync(file); return true; } catch (error) { if (error.code === "ENOENT") return false; throw error; } };
|
|
56
|
+
const writeAtomic = (file, content) => {
|
|
57
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
58
|
+
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
59
|
+
try { fs.writeFileSync(temp, content, { flag: "wx" }); fs.renameSync(temp, file); }
|
|
60
|
+
catch (error) { fs.rmSync(temp, { force: true }); throw error; }
|
|
61
|
+
};
|
|
62
|
+
const hasSymlinkParent = (file) => {
|
|
63
|
+
let current = path.dirname(file);
|
|
64
|
+
while (current.startsWith(projectRoot) && current !== projectRoot) {
|
|
65
|
+
try { if (fs.lstatSync(current).isSymbolicLink()) return true; }
|
|
66
|
+
catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
67
|
+
current = path.dirname(current);
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
function parseArgs(args) {
|
|
73
|
+
if (args[0] !== "init") return fail("usage: opencode-work-context init [--force]");
|
|
74
|
+
const unknown = args.slice(1).filter((arg) => arg !== "--force");
|
|
75
|
+
if (unknown.length) return fail(`unknown option: ${unknown[0]}`);
|
|
76
|
+
return { force: args.includes("--force") };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function ensurePackageJson() {
|
|
80
|
+
const file = path.join(projectRoot, "package.json");
|
|
81
|
+
if (fs.existsSync(file)) return;
|
|
82
|
+
const name = path.basename(projectRoot).toLowerCase().replace(/[^a-z0-9._-]+/g, "-") || "opencode-project";
|
|
83
|
+
fs.writeFileSync(file, `${JSON.stringify({ name, version: "1.0.0", private: true }, null, 2)}\n`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function checkConflicts(force) {
|
|
87
|
+
const conflicts = ["package.json", "package-lock.json", "npm-shrinkwrap.json"].flatMap((relative) => {
|
|
88
|
+
const file = path.join(projectRoot, relative);
|
|
89
|
+
if (!exists(file)) return [];
|
|
90
|
+
if (hasSymlinkParent(file)) return [relative];
|
|
91
|
+
const stat = fs.lstatSync(file);
|
|
92
|
+
return stat.isFile() && !stat.isSymbolicLink() ? [] : [relative];
|
|
93
|
+
});
|
|
94
|
+
conflicts.push(...[...generated].flatMap(([relative, content]) => {
|
|
95
|
+
const file = path.join(projectRoot, relative);
|
|
96
|
+
if (hasSymlinkParent(file)) return [relative];
|
|
97
|
+
if (!exists(file)) return [];
|
|
98
|
+
const stat = fs.lstatSync(file);
|
|
99
|
+
if (!stat.isFile() || stat.isSymbolicLink()) return [relative];
|
|
100
|
+
return !force && fs.readFileSync(file, "utf8") !== content ? [relative] : [];
|
|
101
|
+
}));
|
|
102
|
+
const gitignore = path.join(projectRoot, ".gitignore");
|
|
103
|
+
if (exists(gitignore) && (!fs.lstatSync(gitignore).isFile() || fs.lstatSync(gitignore).isSymbolicLink())) conflicts.push(".gitignore");
|
|
104
|
+
if (conflicts.length) fail(`refusing to overwrite existing files: ${conflicts.join(", ")}; rerun with --force`);
|
|
105
|
+
return conflicts.length === 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function checkPackageConflict(force) {
|
|
109
|
+
const file = path.join(projectRoot, "package.json");
|
|
110
|
+
if (!exists(file)) return true;
|
|
111
|
+
let manifest;
|
|
112
|
+
try { manifest = JSON.parse(fs.readFileSync(file, "utf8")); }
|
|
113
|
+
catch { return true; }
|
|
114
|
+
const existing = manifest.devDependencies?.[packageManifest.name] ?? manifest.dependencies?.[packageManifest.name];
|
|
115
|
+
if (existing && existing !== packageManifest.version && existing !== `=${packageManifest.version}` && !force) {
|
|
116
|
+
fail(`refusing to replace ${packageManifest.name}@${existing}; rerun with --force`);
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function snapshotFiles() {
|
|
123
|
+
return ["package.json", "package-lock.json", "npm-shrinkwrap.json", ...generated.keys(), ".gitignore"].map((relative) => {
|
|
124
|
+
const file = path.join(projectRoot, relative);
|
|
125
|
+
return { file, exists: exists(file), content: exists(file) ? fs.readFileSync(file) : null };
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function restoreFiles(snapshot) {
|
|
130
|
+
for (const item of snapshot) {
|
|
131
|
+
if (item.exists) fs.writeFileSync(item.file, item.content);
|
|
132
|
+
else {
|
|
133
|
+
fs.rmSync(item.file, { force: true });
|
|
134
|
+
let parent = path.dirname(item.file);
|
|
135
|
+
while (parent !== projectRoot) {
|
|
136
|
+
if (!fs.existsSync(parent) || fs.readdirSync(parent).length) break;
|
|
137
|
+
fs.rmdirSync(parent);
|
|
138
|
+
parent = path.dirname(parent);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function acquireInitLock() {
|
|
145
|
+
const lock = path.join(projectRoot, ".opencode-work-context-init.lock");
|
|
146
|
+
let tombstone = null;
|
|
147
|
+
if (exists(lock)) {
|
|
148
|
+
let owner = {};
|
|
149
|
+
try { owner = JSON.parse(fs.readFileSync(path.join(lock, "owner.json"), "utf8")); } catch {}
|
|
150
|
+
if (!owner.pid && Date.now() - fs.statSync(lock).mtimeMs < 60000) { fail("another init is acquiring the lock"); return { lock, failed: true }; }
|
|
151
|
+
let active = false;
|
|
152
|
+
if (Number.isInteger(owner.pid)) { try { process.kill(owner.pid, 0); active = true; } catch (error) { if (error.code === "EPERM") active = true; } }
|
|
153
|
+
if (active || (owner.pid && !owner.token)) { fail("another init is already running or has an incomplete lock"); return { lock, failed: true }; }
|
|
154
|
+
tombstone = `${lock}.reclaim-${randomUUID()}`;
|
|
155
|
+
try { fs.renameSync(lock, tombstone); } catch { fail("another init is reclaiming the stale lock"); return { lock, failed: true }; }
|
|
156
|
+
try { fs.mkdirSync(lock); }
|
|
157
|
+
catch { try { fs.renameSync(tombstone, lock); } catch {} fail("cannot reclaim stale init lock"); return { lock, failed: true }; }
|
|
158
|
+
} else {
|
|
159
|
+
try { fs.mkdirSync(lock); } catch { fail("cannot acquire init lock"); return { lock, failed: true }; }
|
|
160
|
+
}
|
|
161
|
+
const token = randomUUID();
|
|
162
|
+
try { writeAtomic(path.join(lock, "owner.json"), JSON.stringify({ pid: process.pid, token }) + "\n"); }
|
|
163
|
+
catch { fs.rmSync(lock, { recursive: true, force: true }); if (tombstone) try { fs.renameSync(tombstone, lock); } catch {} fail("cannot write init lock owner"); return { lock, failed: true }; }
|
|
164
|
+
if (tombstone) try { fs.rmSync(tombstone, { recursive: true, force: true }); } catch {}
|
|
165
|
+
return { lock, token };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function prepareNodeModules(initLock) {
|
|
169
|
+
const target = path.join(projectRoot, "node_modules");
|
|
170
|
+
const backup = path.join(projectRoot, ".opencode-work-context-node_modules.backup");
|
|
171
|
+
const journal = path.join(projectRoot, ".opencode-work-context-node_modules.recovery.json");
|
|
172
|
+
const result = { target, backup, journal, lock: initLock.lock, token: initLock.token, hadNodeModules: false };
|
|
173
|
+
if (recoverNodeModules(result) === false) return { ...result, failed: true };
|
|
174
|
+
if (exists(backup)) { fail("previous node_modules recovery is incomplete"); return { ...result, failed: true }; }
|
|
175
|
+
if (!exists(target)) { writeAtomic(journal, JSON.stringify({ target, backup, hadNodeModules: false, phase: "prepared" }) + "\n"); return result; }
|
|
176
|
+
if (fs.lstatSync(target).isSymbolicLink() || !fs.lstatSync(target).isDirectory()) { fail("refusing to replace non-regular node_modules"); return { ...result, failed: true }; }
|
|
177
|
+
writeAtomic(journal, JSON.stringify({ target, backup, hadNodeModules: true, phase: "prepared" }) + "\n");
|
|
178
|
+
fs.renameSync(target, backup);
|
|
179
|
+
result.hadNodeModules = true;
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function recoverNodeModules({ target, backup, journal }) {
|
|
184
|
+
if (!exists(journal)) return;
|
|
185
|
+
let state;
|
|
186
|
+
if (fs.lstatSync(journal).isSymbolicLink() || !fs.lstatSync(journal).isFile()) { fail("invalid node_modules recovery journal path"); return false; }
|
|
187
|
+
try { state = JSON.parse(fs.readFileSync(journal, "utf8")); } catch { fail("cannot read node_modules recovery journal"); return false; }
|
|
188
|
+
if (!state || typeof state !== "object" || Object.keys(state).some((key) => !["target", "backup", "hadNodeModules", "phase"].includes(key)) || typeof state.target !== "string" || typeof state.backup !== "string" || !["prepared", "installed", "committed"].includes(state.phase) || state.target !== target || state.backup !== backup || typeof state.hadNodeModules !== "boolean") { fail("invalid node_modules recovery journal"); return false; }
|
|
189
|
+
if (exists(backup) && fs.lstatSync(backup).isSymbolicLink()) { fail("invalid node_modules recovery backup"); return false; }
|
|
190
|
+
if (state.phase === "committed") {
|
|
191
|
+
if (!exists(target) && exists(backup)) fs.renameSync(backup, target);
|
|
192
|
+
else if (exists(backup)) fs.rmSync(backup, { recursive: true, force: true });
|
|
193
|
+
} else if (exists(backup)) {
|
|
194
|
+
if (exists(target)) fs.rmSync(target, { recursive: true, force: true });
|
|
195
|
+
fs.renameSync(backup, target);
|
|
196
|
+
} else if (!state.hadNodeModules && exists(target)) fs.rmSync(target, { recursive: true, force: true });
|
|
197
|
+
fs.rmSync(journal, { force: true });
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function restoreNodeModules(snapshot) {
|
|
202
|
+
if (snapshot.failed) { releaseInitLock(snapshot); return; }
|
|
203
|
+
fs.rmSync(snapshot.target, { recursive: true, force: true });
|
|
204
|
+
if (snapshot.backup && exists(snapshot.backup)) fs.renameSync(snapshot.backup, snapshot.target);
|
|
205
|
+
if (snapshot.journal) fs.rmSync(snapshot.journal, { force: true });
|
|
206
|
+
releaseInitLock(snapshot);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function commitNodeModules(snapshot) {
|
|
210
|
+
if (snapshot.journal) writeAtomic(snapshot.journal, JSON.stringify({ target: snapshot.target, backup: snapshot.backup, hadNodeModules: snapshot.hadNodeModules, phase: "committed" }) + "\n");
|
|
211
|
+
if (snapshot.backup) fs.rmSync(snapshot.backup, { recursive: true, force: true });
|
|
212
|
+
if (snapshot.journal) fs.rmSync(snapshot.journal, { force: true });
|
|
213
|
+
releaseInitLock(snapshot);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function releaseInitLock(snapshot) {
|
|
217
|
+
if (!snapshot.lock || !snapshot.token || !exists(snapshot.lock)) return;
|
|
218
|
+
try { const owner = JSON.parse(fs.readFileSync(path.join(snapshot.lock, "owner.json"), "utf8")); if (owner.token === snapshot.token) fs.rmSync(snapshot.lock, { recursive: true, force: true }); } catch {}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function markNodeModulesInstalled(snapshot) {
|
|
222
|
+
if (snapshot.journal) writeAtomic(snapshot.journal, JSON.stringify({ target: snapshot.target, backup: snapshot.backup, hadNodeModules: snapshot.hadNodeModules, phase: "installed" }) + "\n");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function installPackage(snapshot) {
|
|
226
|
+
const spec = `${packageManifest.name}@${packageManifest.version}`;
|
|
227
|
+
const result = spawnSync("npm", ["install", "--save-dev", "--save-exact", spec], { cwd: projectRoot, stdio: "inherit" });
|
|
228
|
+
if (result.error) { restoreFiles(snapshot); fail(`npm install failed: ${result.error.message}`); }
|
|
229
|
+
else if (result.status !== 0) { restoreFiles(snapshot); fail(`npm install exited with status ${result.status}`); }
|
|
230
|
+
return result.status === 0;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function writeGenerated() {
|
|
234
|
+
for (const [relative, content] of generated) {
|
|
235
|
+
const file = path.join(projectRoot, relative);
|
|
236
|
+
if (!exists(file) || fs.readFileSync(file, "utf8") !== content) writeAtomic(file, content);
|
|
237
|
+
}
|
|
238
|
+
const gitignore = path.join(projectRoot, ".gitignore");
|
|
239
|
+
const line = ".work-context/local/";
|
|
240
|
+
const current = exists(gitignore) ? fs.readFileSync(gitignore, "utf8") : "";
|
|
241
|
+
if (!current.split(/\r?\n/).includes(line)) writeAtomic(gitignore, `${current}${current && !current.endsWith("\n") ? "\n" : ""}${line}\n`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const options = parseArgs(process.argv.slice(2));
|
|
245
|
+
if (options) {
|
|
246
|
+
const initLock = acquireInitLock();
|
|
247
|
+
if (initLock.failed) process.exitCode = 1;
|
|
248
|
+
else if (checkConflicts(options.force) && checkPackageConflict(options.force)) {
|
|
249
|
+
const snapshot = snapshotFiles();
|
|
250
|
+
const nodeModules = prepareNodeModules(initLock);
|
|
251
|
+
if (nodeModules.failed) process.exitCode = 1;
|
|
252
|
+
try {
|
|
253
|
+
if (!nodeModules.failed) ensurePackageJson();
|
|
254
|
+
if (!nodeModules.failed) {
|
|
255
|
+
if (installPackage(snapshot)) {
|
|
256
|
+
markNodeModulesInstalled(nodeModules);
|
|
257
|
+
writeGenerated();
|
|
258
|
+
commitNodeModules(nodeModules);
|
|
259
|
+
console.log("Initialized opencode-work-context. No workspace was created; use /wc create.");
|
|
260
|
+
} else restoreNodeModules(nodeModules);
|
|
261
|
+
}
|
|
262
|
+
} catch (error) {
|
|
263
|
+
restoreFiles(snapshot);
|
|
264
|
+
restoreNodeModules(nodeModules);
|
|
265
|
+
fail(`init failed: ${error.message}`);
|
|
266
|
+
}
|
|
267
|
+
if (nodeModules.failed) restoreNodeModules(nodeModules);
|
|
268
|
+
} else releaseInitLock(initLock);
|
|
269
|
+
}
|
package/commands/wc.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Explicit work-context lifecycle commands backed by structured tools
|
|
3
|
+
agent: build
|
|
4
|
+
subtask: false
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Interpret `/wc $ARGUMENTS` as an explicit work-context operation. Use the matching
|
|
8
|
+
`work_context_*` custom tool registered by the installed package plugin with structured arguments. Never edit work-context
|
|
9
|
+
Markdown, JSONL, INDEX, or SESSIONS projections directly. For mutating operations,
|
|
10
|
+
report the structured result and stable error code. Use `work_context_help` when
|
|
11
|
+
the syntax is missing or ambiguous.
|
|
12
|
+
|
|
13
|
+
Supported syntax:
|
|
14
|
+
|
|
15
|
+
- `help [command]`
|
|
16
|
+
- `create "title"`
|
|
17
|
+
- `list` or `<workspace> list`
|
|
18
|
+
- `resume <workspace> <stage>`
|
|
19
|
+
- `add-stage <workspace> "title"`
|
|
20
|
+
- `<workspace> [<stage>] link-issue <URL>`
|
|
21
|
+
- `session rename "summary"`
|
|
22
|
+
- `session close`
|
|
23
|
+
- `handoff <workspace> <stage>`
|
|
24
|
+
- `finish <workspace> <stage>` requires an explicit knowledge review: `added` or `none`
|
|
25
|
+
- `knowledge list|add|update|supersede <workspace> ...`
|
|
26
|
+
|
|
27
|
+
Arguments: $ARGUMENTS
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-work-context",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Filesystem work-context core and OpenCode adapter",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Nikita Mosiyash",
|
|
9
|
+
"email": "nikita.mosiyash@gmail.com"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"opencode",
|
|
13
|
+
"work-context",
|
|
14
|
+
"workflow",
|
|
15
|
+
"developer-tools"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/mosiyash/opencode-work-context.git"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/mosiyash/opencode-work-context#readme",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/mosiyash/opencode-work-context/issues"
|
|
27
|
+
},
|
|
28
|
+
"bin": {
|
|
29
|
+
"opencode-work-context": "bin/opencode-work-context.js"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node --test"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"bin",
|
|
36
|
+
"commands",
|
|
37
|
+
"plugin",
|
|
38
|
+
"src",
|
|
39
|
+
"tools",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@opencode-ai/plugin": "^1.14.50"
|
|
45
|
+
},
|
|
46
|
+
"exports": {
|
|
47
|
+
".": "./src/index.js",
|
|
48
|
+
"./adapter": "./src/opencode-adapter.js",
|
|
49
|
+
"./plugin": "./plugin/work-context.js",
|
|
50
|
+
"./tools/*": "./tools/*.js"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import tools from "../src/tool-definitions.js";
|
|
2
|
+
import { contextFor, titleFor } from "../src/opencode-adapter.js";
|
|
3
|
+
|
|
4
|
+
export default async ({ client, directory, worktree }) => ({
|
|
5
|
+
tool: tools,
|
|
6
|
+
event: async ({ event }) => {
|
|
7
|
+
if (event.type !== "session.updated") return;
|
|
8
|
+
const info = event.properties?.info;
|
|
9
|
+
const sessionId = info?.id || event.properties?.sessionID;
|
|
10
|
+
const eventDirectory = info?.directory || directory;
|
|
11
|
+
const context = contextFor(worktree || eventDirectory, sessionId);
|
|
12
|
+
const session = context.sessionById(sessionId);
|
|
13
|
+
if (!session) return;
|
|
14
|
+
const title = titleFor(context, session.workspace, session.stage, sessionId, session);
|
|
15
|
+
if (title) {
|
|
16
|
+
const current = await client.session.get({ path: { id: sessionId }, query: { directory: eventDirectory } });
|
|
17
|
+
const session = current.data ?? current;
|
|
18
|
+
if (session.title !== title) await client.session.update({ path: { id: sessionId }, query: { directory: eventDirectory }, body: { title } });
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
});
|