opencode-viking-memory 0.1.5

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/dist/cli.js ADDED
@@ -0,0 +1,490 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ import { mkdirSync, writeFileSync, readFileSync, existsSync } from "node:fs";
5
+ import { join } from "node:path";
6
+ import { homedir } from "node:os";
7
+ import * as readline from "node:readline";
8
+
9
+ // src/services/jsonc.ts
10
+ function stripJsoncComments(content) {
11
+ let result = "";
12
+ let i = 0;
13
+ let inString = false;
14
+ let inSingleLineComment = false;
15
+ let inMultiLineComment = false;
16
+ while (i < content.length) {
17
+ const char = content[i];
18
+ const nextChar = content[i + 1];
19
+ if (!inSingleLineComment && !inMultiLineComment) {
20
+ if (char === '"') {
21
+ let backslashCount = 0;
22
+ let j = i - 1;
23
+ while (j >= 0 && content[j] === "\\") {
24
+ backslashCount++;
25
+ j--;
26
+ }
27
+ if (backslashCount % 2 === 0) {
28
+ inString = !inString;
29
+ }
30
+ result += char;
31
+ i++;
32
+ continue;
33
+ }
34
+ }
35
+ if (inString) {
36
+ result += char;
37
+ i++;
38
+ continue;
39
+ }
40
+ if (!inSingleLineComment && !inMultiLineComment) {
41
+ if (char === "/" && nextChar === "/") {
42
+ inSingleLineComment = true;
43
+ i += 2;
44
+ continue;
45
+ }
46
+ if (char === "/" && nextChar === "*") {
47
+ inMultiLineComment = true;
48
+ i += 2;
49
+ continue;
50
+ }
51
+ }
52
+ if (inSingleLineComment) {
53
+ if (char === `
54
+ `) {
55
+ inSingleLineComment = false;
56
+ result += char;
57
+ }
58
+ i++;
59
+ continue;
60
+ }
61
+ if (inMultiLineComment) {
62
+ if (char === "*" && nextChar === "/") {
63
+ inMultiLineComment = false;
64
+ i += 2;
65
+ continue;
66
+ }
67
+ if (char === `
68
+ `) {
69
+ result += char;
70
+ }
71
+ i++;
72
+ continue;
73
+ }
74
+ result += char;
75
+ i++;
76
+ }
77
+ return result.replace(/,\s*([}\]])/g, "$1");
78
+ }
79
+
80
+ // src/cli.ts
81
+ var OPENCODE_CONFIG_DIR = join(homedir(), ".config", "opencode");
82
+ var OPENCODE_COMMAND_DIR = join(OPENCODE_CONFIG_DIR, "command");
83
+ var OH_MY_OPENCODE_CONFIG = join(OPENCODE_CONFIG_DIR, "oh-my-opencode.json");
84
+ var PLUGIN_NAME = "opencode-viking-memory@latest";
85
+ var SUPERMEMORY_INIT_COMMAND = `---
86
+ description: Initialize Supermemory with comprehensive codebase knowledge
87
+ ---
88
+
89
+ # Initializing VikingMemory
90
+
91
+ You are initializing persistent memory for this codebase. This is not just data collection - you're building context that will make you significantly more effective across all future sessions.
92
+
93
+ ## Understanding Context
94
+
95
+ You are a **stateful** coding agent. Users expect to work with you over extended periods - potentially the entire lifecycle of a project. Your memory is how you get better over time and maintain continuity.
96
+
97
+ ## What to Remember
98
+
99
+ ### 1. Procedures (Rules & Workflows)
100
+ Explicit rules that should always be followed:
101
+ - "Never commit directly to main - always use feature branches"
102
+ - "Always run lint before tests"
103
+ - "Use conventional commits format"
104
+
105
+ ### 2. Preferences (Style & Conventions)
106
+ Project and user coding style:
107
+ - "Prefer functional components over class components"
108
+ - "Use early returns instead of nested conditionals"
109
+ - "Always add JSDoc to exported functions"
110
+
111
+ ### 3. Architecture & Context
112
+ How the codebase works and why:
113
+ - "Auth system was refactored in v2.0 - old patterns deprecated"
114
+ - "The monorepo used to have 3 modules before consolidation"
115
+ - "This pagination bug was fixed before - similar to PR #234"
116
+
117
+ ## Memory Scopes
118
+
119
+ **Project-scoped** (\`scope: "project"\`):
120
+ - Build/test/lint commands
121
+ - Architecture and key directories
122
+ - Team conventions specific to this codebase
123
+ - Technology stack and framework choices
124
+ - Known issues and their solutions
125
+
126
+ **User-scoped** (\`scope: "user"\`):
127
+ - Personal coding preferences across all projects
128
+ - Communication style preferences
129
+ - General workflow habits
130
+
131
+ ## Research Approach
132
+
133
+ This is a **deep research** initialization. Take your time and be thorough (~50+ tool calls). The goal is to genuinely understand the project, not just collect surface-level facts.
134
+
135
+ **What to uncover:**
136
+ - Tech stack and dependencies (explicit and implicit)
137
+ - Project structure and architecture
138
+ - Build/test/deploy commands and workflows
139
+ - Contributors & team dynamics (who works on what?)
140
+ - Commit conventions and branching strategy
141
+ - Code evolution (major refactors, architecture changes)
142
+ - Pain points (areas with lots of bug fixes)
143
+ - Implicit conventions not documented anywhere
144
+
145
+ ## Research Techniques
146
+
147
+ ### File-based
148
+ - README.md, CONTRIBUTING.md, AGENTS.md, CLAUDE.md
149
+ - Package manifests (package.json, Cargo.toml, pyproject.toml, go.mod)
150
+ - Config files (.eslintrc, tsconfig.json, .prettierrc)
151
+ - CI/CD configs (.github/workflows/)
152
+
153
+ ### Git-based
154
+ - \`git log --oneline -20\` - Recent history
155
+ - \`git branch -a\` - Branching strategy
156
+ - \`git log --format="%s" -50\` - Commit conventions
157
+ - \`git shortlog -sn --all | head -10\` - Main contributors
158
+
159
+ ### Explore Agent
160
+ Fire parallel explore queries for broad understanding:
161
+ \`\`\`
162
+ Task(explore, "What is the tech stack and key dependencies?")
163
+ Task(explore, "What is the project structure? Key directories?")
164
+ Task(explore, "How do you build, test, and run this project?")
165
+ Task(explore, "What are the main architectural patterns?")
166
+ Task(explore, "What conventions or patterns are used?")
167
+ \`\`\`
168
+
169
+ ## How to Do Thorough Research
170
+
171
+ **Don't just collect data - analyze and cross-reference.**
172
+
173
+ Bad (shallow):
174
+ - Run commands, copy output
175
+ - List facts without understanding
176
+
177
+ Good (thorough):
178
+ - Cross-reference findings (if inconsistent, dig deeper)
179
+ - Resolve ambiguities (don't leave questions unanswered)
180
+ - Read actual file content, not just names
181
+ - Look for patterns (what do commits tell you about workflow?)
182
+ - Think like a new team member - what would you want to know?
183
+
184
+ ## Saving Memories
185
+
186
+ Use the \`viking_memory\` tool for each distinct insight:
187
+
188
+ \`\`\`
189
+ viking_memory(mode: "add", content: "...", type: "...", scope: "project")
190
+ \`\`\`
191
+
192
+ **Types:**
193
+ - \`project-config\` - tech stack, commands, tooling
194
+ - \`architecture\` - codebase structure, key components, data flow
195
+ - \`learned-pattern\` - conventions specific to this codebase
196
+ - \`error-solution\` - known issues and their fixes
197
+ - \`preference\` - coding style preferences (use with user scope)
198
+
199
+ **Guidelines:**
200
+ - Save each distinct insight as a separate memory
201
+ - Be concise but include enough context to be useful
202
+ - Include the "why" not just the "what" when relevant
203
+ - Update memories incrementally as you research (don't wait until the end)
204
+
205
+ **Good memories:**
206
+ - "Uses Bun runtime and package manager. Commands: bun install, bun run dev, bun test"
207
+ - "API routes in src/routes/, handlers in src/handlers/. Hono framework."
208
+ - "Auth uses Redis sessions, not JWT. Implementation in src/lib/auth.ts"
209
+ - "Never use \`any\` type - strict TypeScript. Use \`unknown\` and narrow."
210
+ - "Database migrations must be backward compatible - we do rolling deploys"
211
+
212
+ ## Upfront Questions
213
+
214
+ Before diving in, ask:
215
+ 1. "Any specific rules I should always follow?"
216
+ 2. "Preferences for how I communicate? (terse/detailed)"
217
+
218
+ ## Reflection Phase
219
+
220
+ Before finishing, reflect:
221
+ 1. **Completeness**: Did you cover commands, architecture, conventions, gotchas?
222
+ 2. **Quality**: Are memories concise and searchable?
223
+ 3. **Scope**: Did you correctly separate project vs user knowledge?
224
+
225
+ Then ask: "I've initialized memory with X insights. Want me to continue refining, or is this good?"
226
+
227
+ ## Your Task
228
+
229
+ 1. Ask upfront questions (research depth, rules, preferences)
230
+ 2. Check existing memories: \`viking_memory(mode: "list", scope: "project")\`
231
+ 3. Research based on chosen depth
232
+ 4. Save memories incrementally as you discover insights
233
+ 5. Reflect and verify completeness
234
+ 6. Summarize what was learned and ask if user wants refinement
235
+ `;
236
+ function createReadline() {
237
+ return readline.createInterface({
238
+ input: process.stdin,
239
+ output: process.stdout
240
+ });
241
+ }
242
+ async function confirm(rl, question) {
243
+ return new Promise((resolve) => {
244
+ rl.question(`${question} (y/n) `, (answer) => {
245
+ resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes");
246
+ });
247
+ });
248
+ }
249
+ function findOpencodeConfig() {
250
+ const candidates = [
251
+ join(OPENCODE_CONFIG_DIR, "opencode.jsonc"),
252
+ join(OPENCODE_CONFIG_DIR, "opencode.json")
253
+ ];
254
+ for (const path of candidates) {
255
+ if (existsSync(path)) {
256
+ return path;
257
+ }
258
+ }
259
+ return null;
260
+ }
261
+ function addPluginToConfig(configPath) {
262
+ try {
263
+ const content = readFileSync(configPath, "utf-8");
264
+ if (content.includes("opencode-viking-memory")) {
265
+ console.log("✓ Plugin already registered in config");
266
+ return true;
267
+ }
268
+ const jsonContent = stripJsoncComments(content);
269
+ let config;
270
+ try {
271
+ config = JSON.parse(jsonContent);
272
+ } catch {
273
+ console.error("✗ Failed to parse config file");
274
+ return false;
275
+ }
276
+ const plugins = config.plugin || [];
277
+ plugins.push(PLUGIN_NAME);
278
+ config.plugin = plugins;
279
+ if (configPath.endsWith(".jsonc")) {
280
+ if (content.includes('"plugin"')) {
281
+ const newContent = content.replace(/("plugin"\s*:\s*\[)([^\]]*?)(\])/, (_match, start, middle, end) => {
282
+ const trimmed = middle.trim();
283
+ if (trimmed === "") {
284
+ return `${start}
285
+ "${PLUGIN_NAME}"
286
+ ${end}`;
287
+ }
288
+ return `${start}${middle.trimEnd()},
289
+ "${PLUGIN_NAME}"
290
+ ${end}`;
291
+ });
292
+ writeFileSync(configPath, newContent);
293
+ } else {
294
+ const newContent = content.replace(/^(\s*\{)/, `$1
295
+ "plugin": ["${PLUGIN_NAME}"],`);
296
+ writeFileSync(configPath, newContent);
297
+ }
298
+ } else {
299
+ writeFileSync(configPath, JSON.stringify(config, null, 2));
300
+ }
301
+ console.log(`✓ Added plugin to ${configPath}`);
302
+ return true;
303
+ } catch (err) {
304
+ console.error("✗ Failed to update config:", err);
305
+ return false;
306
+ }
307
+ }
308
+ function createNewConfig() {
309
+ const configPath = join(OPENCODE_CONFIG_DIR, "opencode.jsonc");
310
+ mkdirSync(OPENCODE_CONFIG_DIR, { recursive: true });
311
+ const config = `{
312
+ "plugin": ["${PLUGIN_NAME}"]
313
+ }
314
+ `;
315
+ writeFileSync(configPath, config);
316
+ console.log(`✓ Created ${configPath}`);
317
+ return true;
318
+ }
319
+ function createCommand() {
320
+ mkdirSync(OPENCODE_COMMAND_DIR, { recursive: true });
321
+ const commandPath = join(OPENCODE_COMMAND_DIR, "viking_memory-init.md");
322
+ writeFileSync(commandPath, SUPERMEMORY_INIT_COMMAND);
323
+ console.log(`✓ Created /viking_memory-init command`);
324
+ return true;
325
+ }
326
+ function isOhMyOpencodeInstalled() {
327
+ const configPath = findOpencodeConfig();
328
+ if (!configPath)
329
+ return false;
330
+ try {
331
+ const content = readFileSync(configPath, "utf-8");
332
+ return content.includes("oh-my-opencode");
333
+ } catch {
334
+ return false;
335
+ }
336
+ }
337
+ function isAutoCompactAlreadyDisabled() {
338
+ if (!existsSync(OH_MY_OPENCODE_CONFIG))
339
+ return false;
340
+ try {
341
+ const content = readFileSync(OH_MY_OPENCODE_CONFIG, "utf-8");
342
+ const config = JSON.parse(content);
343
+ const disabledHooks = config.disabled_hooks;
344
+ return disabledHooks?.includes("anthropic-context-window-limit-recovery") ?? false;
345
+ } catch {
346
+ return false;
347
+ }
348
+ }
349
+ function disableAutoCompactHook() {
350
+ try {
351
+ let config = {};
352
+ if (existsSync(OH_MY_OPENCODE_CONFIG)) {
353
+ const content = readFileSync(OH_MY_OPENCODE_CONFIG, "utf-8");
354
+ config = JSON.parse(content);
355
+ }
356
+ const disabledHooks = config.disabled_hooks || [];
357
+ if (!disabledHooks.includes("anthropic-context-window-limit-recovery")) {
358
+ disabledHooks.push("anthropic-context-window-limit-recovery");
359
+ }
360
+ config.disabled_hooks = disabledHooks;
361
+ writeFileSync(OH_MY_OPENCODE_CONFIG, JSON.stringify(config, null, 2));
362
+ console.log(`✓ Disabled anthropic-context-window-limit-recovery hook in oh-my-opencode.json`);
363
+ return true;
364
+ } catch (err) {
365
+ console.error("✗ Failed to update oh-my-opencode.json:", err);
366
+ return false;
367
+ }
368
+ }
369
+ async function install(options) {
370
+ console.log(`
371
+ \uD83E\uDDE0 opencode-viking-memory installer
372
+ `);
373
+ const rl = options.tui ? createReadline() : null;
374
+ console.log("Step 1: Register plugin in OpenCode config");
375
+ const configPath = findOpencodeConfig();
376
+ if (configPath) {
377
+ if (options.tui) {
378
+ const shouldModify = await confirm(rl, `Add plugin to ${configPath}?`);
379
+ if (!shouldModify) {
380
+ console.log("Skipped.");
381
+ } else {
382
+ addPluginToConfig(configPath);
383
+ }
384
+ } else {
385
+ addPluginToConfig(configPath);
386
+ }
387
+ } else {
388
+ if (options.tui) {
389
+ const shouldCreate = await confirm(rl, "No OpenCode config found. Create one?");
390
+ if (!shouldCreate) {
391
+ console.log("Skipped.");
392
+ } else {
393
+ createNewConfig();
394
+ }
395
+ } else {
396
+ createNewConfig();
397
+ }
398
+ }
399
+ console.log(`
400
+ Step 2: Create /viking_memory-init command`);
401
+ if (options.tui) {
402
+ const shouldCreate = await confirm(rl, "Add /viking_memory-init command?");
403
+ if (!shouldCreate) {
404
+ console.log("Skipped.");
405
+ } else {
406
+ createCommand();
407
+ }
408
+ } else {
409
+ createCommand();
410
+ }
411
+ if (isOhMyOpencodeInstalled()) {
412
+ console.log(`
413
+ Step 3: Configure Oh My OpenCode`);
414
+ console.log("Detected Oh My OpenCode plugin.");
415
+ console.log("VikingMemory handles context compaction, so the built-in context-window-limit-recovery hook should be disabled.");
416
+ if (isAutoCompactAlreadyDisabled()) {
417
+ console.log("✓ anthropic-context-window-limit-recovery hook already disabled");
418
+ } else {
419
+ if (options.tui) {
420
+ const shouldDisable = await confirm(rl, "Disable anthropic-context-window-limit-recovery hook to let VikingMemory handle context?");
421
+ if (!shouldDisable) {
422
+ console.log("Skipped.");
423
+ } else {
424
+ disableAutoCompactHook();
425
+ }
426
+ } else if (options.disableAutoCompact) {
427
+ disableAutoCompactHook();
428
+ } else {
429
+ console.log("Skipped. Use --disable-context-recovery to disable the hook in non-interactive mode.");
430
+ }
431
+ }
432
+ }
433
+ console.log(`
434
+ ` + "─".repeat(50));
435
+ console.log(`
436
+ \uD83D\uDD11 Final step: Set your API key
437
+ `);
438
+ console.log("Get your API key from: https://console.volcengine.com/knowledgebase/");
439
+ console.log(`
440
+ Then add to your shell profile:
441
+ `);
442
+ console.log(' export VIKING_MEMORY_API_KEY="..."');
443
+ console.log(`
444
+ Or create ~/.config/opencode/viking_memory.jsonc:
445
+ `);
446
+ console.log(' { "apiKey": "..." }');
447
+ console.log(`
448
+ ` + "─".repeat(50));
449
+ console.log(`
450
+ ✓ Setup complete! Restart OpenCode to activate.
451
+ `);
452
+ if (rl)
453
+ rl.close();
454
+ return 0;
455
+ }
456
+ function printHelp() {
457
+ console.log(`
458
+ opencode-viking-memory - Persistent memory for OpenCode agents
459
+
460
+ Commands:
461
+ install Install and configure the plugin
462
+ --no-tui Run in non-interactive mode (for LLM agents)
463
+ --disable-context-recovery Disable Oh My OpenCode's context-window-limit-recovery hook (use with --no-tui)
464
+
465
+ Examples:
466
+ bunx opencode-viking-memory@latest install
467
+ bunx opencode-viking-memory@latest install --no-tui
468
+ bunx opencode-viking-memory@latest install --no-tui --disable-context-recovery
469
+ `);
470
+ }
471
+ var args = process.argv.slice(2);
472
+ if (args.length === 0 || args[0] === "help" || args[0] === "--help" || args[0] === "-h") {
473
+ printHelp();
474
+ process.exit(0);
475
+ }
476
+ if (args[0] === "install") {
477
+ const noTui = args.includes("--no-tui");
478
+ const disableAutoCompact = args.includes("--disable-context-recovery");
479
+ install({ tui: !noTui, disableAutoCompact }).then((code) => process.exit(code));
480
+ } else if (args[0] === "setup") {
481
+ console.log(`Note: 'setup' is deprecated. Use 'install' instead.
482
+ `);
483
+ const noTui = args.includes("--no-tui");
484
+ const disableAutoCompact = args.includes("--disable-context-recovery");
485
+ install({ tui: !noTui, disableAutoCompact }).then((code) => process.exit(code));
486
+ } else {
487
+ console.error(`Unknown command: ${args[0]}`);
488
+ printHelp();
489
+ process.exit(1);
490
+ }
@@ -0,0 +1,14 @@
1
+ export declare const VIKING_MEMORY_API_KEY: string | undefined;
2
+ export declare const VIKING_MEMORY_RESOURCE_ID: string | undefined;
3
+ export declare const CONFIG: {
4
+ similarityThreshold: number;
5
+ maxMemories: number;
6
+ maxProjectMemories: number;
7
+ maxProfileItems: number;
8
+ injectProfile: boolean;
9
+ containerTagPrefix: string;
10
+ filterPrompt: string;
11
+ keywordPatterns: string[];
12
+ };
13
+ export declare function isConfigured(): boolean;
14
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAgFA,eAAO,MAAM,qBAAqB,oBAAyD,CAAC;AAC5F,eAAO,MAAM,yBAAyB,oBAAiE,CAAA;AAEvG,eAAO,MAAM,MAAM;;;;;;;;;CAYlB,CAAC;AAEF,wBAAgB,YAAY,IAAI,OAAO,CAEtC"}
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from "@opencode-ai/plugin";
2
+ export declare const VikingMemoryPlugin: Plugin;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAe,MAAM,qBAAqB,CAAC;AAsC/D,eAAO,MAAM,kBAAkB,EAAE,MAwahC,CAAC"}