@cortexkit/opencode-magic-context 0.24.0 → 0.25.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/README.md +4 -2
- package/dist/agents/magic-context-prompt.d.ts.map +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts +18 -0
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-local.d.ts +4 -0
- package/dist/features/magic-context/memory/embedding-local.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-openai.d.ts +14 -0
- package/dist/features/magic-context/memory/embedding-openai.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory-embeddings.d.ts +6 -0
- package/dist/features/magic-context/memory/storage-memory-embeddings.d.ts.map +1 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts +38 -0
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-session.d.ts +1 -0
- package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-shared.d.ts +2 -1
- package/dist/features/magic-context/storage-meta-shared.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta.d.ts +1 -1
- package/dist/features/magic-context/storage-meta.d.ts.map +1 -1
- package/dist/features/magic-context/storage-tags.d.ts +20 -1
- package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
- package/dist/features/magic-context/storage.d.ts +2 -2
- package/dist/features/magic-context/storage.d.ts.map +1 -1
- package/dist/features/magic-context/types.d.ts +1 -0
- package/dist/features/magic-context/types.d.ts.map +1 -1
- package/dist/hooks/magic-context/apply-operations.d.ts +3 -2
- package/dist/hooks/magic-context/apply-operations.d.ts.map +1 -1
- package/dist/hooks/magic-context/caveman-cleanup.d.ts +1 -0
- package/dist/hooks/magic-context/caveman-cleanup.d.ts.map +1 -1
- package/dist/hooks/magic-context/channel2-delivery.d.ts +2 -0
- package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
- package/dist/hooks/magic-context/command-handler.d.ts +7 -5
- package/dist/hooks/magic-context/command-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts +14 -4
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts.map +1 -1
- package/dist/hooks/magic-context/embed-session-state.d.ts +14 -0
- package/dist/hooks/magic-context/embed-session-state.d.ts.map +1 -0
- package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/format-embed-status.d.ts +9 -0
- package/dist/hooks/magic-context/format-embed-status.d.ts.map +1 -0
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts +1 -0
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts +1 -1
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
- package/dist/hooks/magic-context/recomp-orchestrator.d.ts.map +1 -1
- package/dist/hooks/magic-context/strip-content.d.ts +0 -1
- package/dist/hooks/magic-context/strip-content.d.ts.map +1 -1
- package/dist/hooks/magic-context/tag-content-primitives.d.ts +2 -0
- package/dist/hooks/magic-context/tag-content-primitives.d.ts.map +1 -1
- package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
- package/dist/hooks/magic-context/tool-drop-target.d.ts +1 -1
- package/dist/hooks/magic-context/tool-drop-target.d.ts.map +1 -1
- package/dist/hooks/magic-context/tool-reclaim.d.ts +12 -0
- package/dist/hooks/magic-context/tool-reclaim.d.ts.map +1 -0
- package/dist/hooks/magic-context/transform-operations.d.ts +1 -1
- package/dist/hooks/magic-context/transform-operations.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts +2 -0
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1117 -378
- package/dist/plugin/conflict-warning-hook.d.ts.map +1 -1
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/shared/announcement.d.ts +1 -1
- package/dist/shared/model-suggestion-retry.d.ts.map +1 -1
- package/dist/shared/rpc-types.d.ts +20 -0
- package/dist/shared/rpc-types.d.ts.map +1 -1
- package/dist/shared/sqlite.d.ts +5 -1
- package/dist/shared/sqlite.d.ts.map +1 -1
- package/dist/shared/tui-preferences.d.ts +32 -0
- package/dist/shared/tui-preferences.d.ts.map +1 -0
- package/dist/tools/ctx-expand/constants.d.ts +1 -1
- package/dist/tools/ctx-expand/constants.d.ts.map +1 -1
- package/dist/tools/ctx-expand/render.d.ts +43 -0
- package/dist/tools/ctx-expand/render.d.ts.map +1 -0
- package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
- package/dist/tools/ctx-expand/types.d.ts +6 -2
- package/dist/tools/ctx-expand/types.d.ts.map +1 -1
- package/dist/tools/ctx-reduce/constants.d.ts +1 -1
- package/dist/tools/ctx-reduce/constants.d.ts.map +1 -1
- package/dist/tui/data/context-db.d.ts +4 -2
- package/dist/tui/data/context-db.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/shared/announcement.ts +6 -6
- package/src/shared/model-suggestion-retry.test.ts +61 -1
- package/src/shared/model-suggestion-retry.ts +22 -0
- package/src/shared/rpc-types.ts +11 -0
- package/src/shared/sqlite-bind-style.test.ts +82 -0
- package/src/shared/sqlite.ts +30 -1
- package/src/shared/tag-transcript.test.ts +3 -1
- package/src/shared/tag-transcript.ts +19 -17
- package/src/shared/tui-preferences.test.ts +210 -0
- package/src/shared/tui-preferences.ts +303 -0
- package/src/tui/data/context-db.ts +34 -2
- package/src/tui/index.tsx +58 -4
- package/src/tui/slots/sidebar-content.tsx +106 -12
|
@@ -611,7 +611,7 @@ function tagToolPart(args: TagToolPartArgs): void {
|
|
|
611
611
|
args.part.setText(tagged);
|
|
612
612
|
}
|
|
613
613
|
|
|
614
|
-
args.targets.set(tagId, buildToolTarget(args.part, args.message));
|
|
614
|
+
args.targets.set(tagId, buildToolTarget(args.part, args.message, tagId));
|
|
615
615
|
}
|
|
616
616
|
|
|
617
617
|
function setToolContentOrText(part: TranscriptPart, content: string): boolean {
|
|
@@ -679,10 +679,11 @@ function buildAggregateTarget(tagId: number, occurrences: ToolOccurrence[]): Tag
|
|
|
679
679
|
return any ? "removed" : "absent";
|
|
680
680
|
},
|
|
681
681
|
truncate(): "truncated" | "absent" {
|
|
682
|
-
//
|
|
683
|
-
//
|
|
684
|
-
//
|
|
685
|
-
|
|
682
|
+
// Skeleton-drop: replace BOTH halves' content with the one
|
|
683
|
+
// canonical `[dropped §N§]` placeholder (byte-identical to a full
|
|
684
|
+
// drop and to OpenCode). Frozen by the dropMode column → replays
|
|
685
|
+
// the same string every pass. The tool_use call survives intact.
|
|
686
|
+
const sentinel = `[dropped \u00a7${tagId}\u00a7]`;
|
|
686
687
|
let any = false;
|
|
687
688
|
for (const occ of occurrences) {
|
|
688
689
|
if (setToolContentOrText(occ.part, sentinel)) {
|
|
@@ -739,16 +740,17 @@ function buildTextTarget(
|
|
|
739
740
|
}
|
|
740
741
|
|
|
741
742
|
/**
|
|
742
|
-
* TagTarget for a tag-eligible tool part. Tool parts get full-drop
|
|
743
|
-
*
|
|
744
|
-
* `
|
|
745
|
-
*
|
|
746
|
-
*
|
|
747
|
-
*
|
|
743
|
+
* TagTarget for a tag-eligible tool part. Tool parts get full-drop or
|
|
744
|
+
* skeleton-drop treatment from `applyFlushedStatuses` based on the stored
|
|
745
|
+
* `drop_mode` column. Both render the SAME canonical `[dropped §N§]`
|
|
746
|
+
* placeholder — full-drop replaces the whole pair, skeleton-drop keeps the
|
|
747
|
+
* tool_use call and replaces only its output. One placeholder string,
|
|
748
|
+
* byte-identical across passes and across harnesses.
|
|
748
749
|
*/
|
|
749
750
|
function buildToolTarget(
|
|
750
751
|
part: TranscriptPart,
|
|
751
752
|
message: { info: { id?: string; role: string } },
|
|
753
|
+
tagId: number,
|
|
752
754
|
): TagTarget {
|
|
753
755
|
return {
|
|
754
756
|
setContent(content: string): boolean {
|
|
@@ -765,15 +767,15 @@ function buildToolTarget(
|
|
|
765
767
|
// For Pi the current Transcript contract treats both
|
|
766
768
|
// invocation and result parts symmetrically — both expose
|
|
767
769
|
// setText / setToolOutput.
|
|
768
|
-
const replaced = part.replaceWithSentinel(`[dropped \u00a7${
|
|
770
|
+
const replaced = part.replaceWithSentinel(`[dropped \u00a7${tagId}\u00a7]`);
|
|
769
771
|
return replaced ? "removed" : "absent";
|
|
770
772
|
},
|
|
771
773
|
truncate(): "truncated" | "absent" {
|
|
772
|
-
//
|
|
773
|
-
//
|
|
774
|
-
//
|
|
775
|
-
//
|
|
776
|
-
const ok = setToolContentOrText(part,
|
|
774
|
+
// Skeleton-drop: replace the tool output with the one canonical
|
|
775
|
+
// `[dropped §N§]` placeholder (byte-identical to a full drop and to
|
|
776
|
+
// OpenCode). Frozen by the dropMode column, so it replays the same
|
|
777
|
+
// string every pass. The tool_use call itself survives intact.
|
|
778
|
+
const ok = setToolContentOrText(part, `[dropped \u00a7${tagId}\u00a7]`);
|
|
777
779
|
return ok ? "truncated" : "absent";
|
|
778
780
|
},
|
|
779
781
|
message: {
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { parse } from "comment-json";
|
|
6
|
+
import {
|
|
7
|
+
computeEffectiveOrder,
|
|
8
|
+
DEFAULT_PREFS,
|
|
9
|
+
DEFAULT_SLOT_ORDER,
|
|
10
|
+
getTuiPreferencesFile,
|
|
11
|
+
PLUGIN_KEY,
|
|
12
|
+
queueTuiPreferenceUpdate,
|
|
13
|
+
readTuiPreferencesFile,
|
|
14
|
+
resolveMagicContextPrefs,
|
|
15
|
+
TUI_PREFS_FILE_ENV,
|
|
16
|
+
} from "./tui-preferences";
|
|
17
|
+
|
|
18
|
+
let dir: string;
|
|
19
|
+
let file: string;
|
|
20
|
+
const savedEnv: Record<string, string | undefined> = {};
|
|
21
|
+
const ENV_KEYS = [TUI_PREFS_FILE_ENV, "OPENCODE_CONFIG_DIR", "XDG_CONFIG_HOME"];
|
|
22
|
+
|
|
23
|
+
beforeEach(async () => {
|
|
24
|
+
for (const key of ENV_KEYS) savedEnv[key] = process.env[key];
|
|
25
|
+
dir = await mkdtemp(join(tmpdir(), "mc-tui-prefs-test-"));
|
|
26
|
+
file = join(dir, "tui-preferences.jsonc");
|
|
27
|
+
process.env[TUI_PREFS_FILE_ENV] = file;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterEach(async () => {
|
|
31
|
+
for (const key of ENV_KEYS) {
|
|
32
|
+
if (savedEnv[key] === undefined) delete process.env[key];
|
|
33
|
+
else process.env[key] = savedEnv[key];
|
|
34
|
+
}
|
|
35
|
+
await rm(dir, { recursive: true, force: true });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("getTuiPreferencesFile", () => {
|
|
39
|
+
test("env override wins", () => {
|
|
40
|
+
expect(getTuiPreferencesFile()).toBe(file);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("falls back to OPENCODE_CONFIG_DIR then XDG then ~/.config", () => {
|
|
44
|
+
delete process.env[TUI_PREFS_FILE_ENV];
|
|
45
|
+
process.env.OPENCODE_CONFIG_DIR = "/tmp/cfgdir";
|
|
46
|
+
expect(getTuiPreferencesFile()).toBe("/tmp/cfgdir/tui-preferences.jsonc");
|
|
47
|
+
delete process.env.OPENCODE_CONFIG_DIR;
|
|
48
|
+
process.env.XDG_CONFIG_HOME = "/tmp/xdg";
|
|
49
|
+
expect(getTuiPreferencesFile()).toBe("/tmp/xdg/opencode/tui-preferences.jsonc");
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("readTuiPreferencesFile (tolerant)", () => {
|
|
54
|
+
test("missing file → {}", async () => {
|
|
55
|
+
expect(await readTuiPreferencesFile()).toEqual({});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("malformed JSON → {}", async () => {
|
|
59
|
+
await writeFile(file, "{ this is not json ", "utf8");
|
|
60
|
+
expect(await readTuiPreferencesFile()).toEqual({});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("non-object root → {}", async () => {
|
|
64
|
+
await writeFile(file, "[1, 2, 3]", "utf8");
|
|
65
|
+
expect(await readTuiPreferencesFile()).toEqual({});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("jsonc with comments + trailing comma parses", async () => {
|
|
69
|
+
await writeFile(
|
|
70
|
+
file,
|
|
71
|
+
`{
|
|
72
|
+
// a comment
|
|
73
|
+
"magic-context": { "order": 205, },
|
|
74
|
+
}`,
|
|
75
|
+
"utf8",
|
|
76
|
+
);
|
|
77
|
+
const root = await readTuiPreferencesFile();
|
|
78
|
+
expect(resolveMagicContextPrefs(root).order).toBe(205);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("resolveMagicContextPrefs (per-key validation)", () => {
|
|
83
|
+
test("missing key → full defaults clone", () => {
|
|
84
|
+
expect(resolveMagicContextPrefs({})).toEqual(DEFAULT_PREFS);
|
|
85
|
+
// clone, not the shared object
|
|
86
|
+
expect(resolveMagicContextPrefs({})).not.toBe(DEFAULT_PREFS);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("one bad value never poisons the rest", () => {
|
|
90
|
+
const prefs = resolveMagicContextPrefs({
|
|
91
|
+
"magic-context": {
|
|
92
|
+
order: "nope",
|
|
93
|
+
rememberCollapsed: 1,
|
|
94
|
+
collapsed: true,
|
|
95
|
+
sections: { historian: false, memory: "bad" },
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
expect(prefs.order).toBe(DEFAULT_SLOT_ORDER); // bad → default
|
|
99
|
+
expect(prefs.rememberCollapsed).toBe(true); // bad → default true
|
|
100
|
+
expect(prefs.collapsed).toBe(true); // valid bool preserved
|
|
101
|
+
expect(prefs.sections.historian).toBe(false); // valid bool preserved
|
|
102
|
+
expect(prefs.sections.memory).toBe(true); // bad → default true
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("order clamps to -10000..10000", () => {
|
|
106
|
+
expect(resolveMagicContextPrefs({ "magic-context": { order: 99999 } }).order).toBe(10000);
|
|
107
|
+
expect(resolveMagicContextPrefs({ "magic-context": { order: -99999 } }).order).toBe(-10000);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("collapsed non-boolean → null (seed from startCollapsed)", () => {
|
|
111
|
+
expect(resolveMagicContextPrefs({ "magic-context": {} }).collapsed).toBeNull();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("header label clamps length, empty → default", () => {
|
|
115
|
+
expect(
|
|
116
|
+
resolveMagicContextPrefs({ "magic-context": { header: { label: "" } } }).header.label,
|
|
117
|
+
).toBe(DEFAULT_PREFS.header.label);
|
|
118
|
+
expect(
|
|
119
|
+
resolveMagicContextPrefs({
|
|
120
|
+
"magic-context": { header: { label: "x".repeat(50) } },
|
|
121
|
+
}).header.label.length,
|
|
122
|
+
).toBe(24);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("computeEffectiveOrder (cross-plugin convention)", () => {
|
|
127
|
+
test("default when key missing", () => {
|
|
128
|
+
expect(computeEffectiveOrder({}, PLUGIN_KEY, DEFAULT_SLOT_ORDER)).toBe(DEFAULT_SLOT_ORDER);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("explicit order clamped", () => {
|
|
132
|
+
expect(computeEffectiveOrder({ "magic-context": { order: 250 } }, PLUGIN_KEY, 200)).toBe(
|
|
133
|
+
250,
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("forceToTop sorts below FORCE_TOP_BASE by key position", () => {
|
|
138
|
+
const root = { aft: { forceToTop: true }, "magic-context": { forceToTop: true } };
|
|
139
|
+
expect(computeEffectiveOrder(root, "aft", 200)).toBe(-100000 + 0);
|
|
140
|
+
expect(computeEffectiveOrder(root, "magic-context", 200)).toBe(-100000 + 1);
|
|
141
|
+
// forced always beats any manual order (clamped band is strictly above)
|
|
142
|
+
expect(computeEffectiveOrder(root, "aft", 200)).toBeLessThan(-10000);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("write path — comment-json full round-trip", () => {
|
|
147
|
+
test("persists a nested key and reads back", async () => {
|
|
148
|
+
await queueTuiPreferenceUpdate(PLUGIN_KEY, ["collapsed"], true);
|
|
149
|
+
const prefs = resolveMagicContextPrefs(await readTuiPreferencesFile());
|
|
150
|
+
expect(prefs.collapsed).toBe(true);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("seeds the file from the template when absent", async () => {
|
|
154
|
+
await queueTuiPreferenceUpdate(PLUGIN_KEY, ["order"], 205);
|
|
155
|
+
const text = await readFile(file, "utf8");
|
|
156
|
+
expect(text).toContain("Shared preferences for OpenCode TUI plugins");
|
|
157
|
+
expect(resolveMagicContextPrefs(await readTuiPreferencesFile()).order).toBe(205);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("INTEROP: a sibling plugin's values AND comments survive MC writing only its key", async () => {
|
|
161
|
+
// A shared file owned partly by anthropic-auth, with comments and an
|
|
162
|
+
// appearance block MC knows nothing about. MC must touch ONLY its key.
|
|
163
|
+
await writeFile(
|
|
164
|
+
file,
|
|
165
|
+
`{
|
|
166
|
+
// anthropic-auth section — DO NOT lose this BLOCK comment
|
|
167
|
+
"anthropic-auth": {
|
|
168
|
+
"order": 160,
|
|
169
|
+
"header": { "label": "CLAUDE" },
|
|
170
|
+
// bar appearance knobs MC has no schema for
|
|
171
|
+
"appearance": { "barWidth": 10, "barFilledChar": "#" },
|
|
172
|
+
"pollMs": 2000 // INLINE trailing comment — must survive too
|
|
173
|
+
},
|
|
174
|
+
"magic-context": { "order": 200 }
|
|
175
|
+
}
|
|
176
|
+
`,
|
|
177
|
+
"utf8",
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
await queueTuiPreferenceUpdate(PLUGIN_KEY, ["collapsed"], true);
|
|
181
|
+
|
|
182
|
+
const text = await readFile(file, "utf8");
|
|
183
|
+
// sibling comments preserved — BOTH block and inline trailing
|
|
184
|
+
// (comment-json round-trips both faithfully; enforce the guarantee).
|
|
185
|
+
expect(text).toContain("anthropic-auth section — DO NOT lose this BLOCK comment");
|
|
186
|
+
expect(text).toContain("bar appearance knobs MC has no schema for");
|
|
187
|
+
expect(text).toContain("INLINE trailing comment — must survive too");
|
|
188
|
+
|
|
189
|
+
// sibling VALUES intact (incl. nested keys MC has no schema for)
|
|
190
|
+
const root = parse(text) as Record<string, Record<string, unknown>>;
|
|
191
|
+
const aa = root["anthropic-auth"] as Record<string, unknown>;
|
|
192
|
+
expect(aa.order).toBe(160);
|
|
193
|
+
expect((aa.header as Record<string, unknown>).label).toBe("CLAUDE");
|
|
194
|
+
const appearance = aa.appearance as Record<string, unknown>;
|
|
195
|
+
expect(appearance.barWidth).toBe(10);
|
|
196
|
+
expect(appearance.barFilledChar).toBe("#");
|
|
197
|
+
|
|
198
|
+
// MC's own change landed
|
|
199
|
+
expect(resolveMagicContextPrefs(root).collapsed).toBe(true);
|
|
200
|
+
expect(resolveMagicContextPrefs(root).order).toBe(200);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("malformed existing file → write is a no-op, sibling content untouched", async () => {
|
|
204
|
+
const broken = `{ "anthropic-auth": { "order": 160 } broken `;
|
|
205
|
+
await writeFile(file, broken, "utf8");
|
|
206
|
+
await queueTuiPreferenceUpdate(PLUGIN_KEY, ["collapsed"], true);
|
|
207
|
+
// unchanged — we never clobber a file we can't safely parse
|
|
208
|
+
expect(await readFile(file, "utf8")).toBe(broken);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { readFileSync, watch } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import { parse, stringify } from "comment-json";
|
|
6
|
+
|
|
7
|
+
// Shared preferences file for OpenCode TUI plugins. One top-level key per plugin
|
|
8
|
+
// (short, non-integer-like name, e.g. "magic-context"). The file is OPTIONAL:
|
|
9
|
+
// every reader falls back to defaults when it is missing or malformed.
|
|
10
|
+
//
|
|
11
|
+
// Cross-plugin convention (anthropic-auth / aft / magic-context all mirror it):
|
|
12
|
+
// - same file name + env override + lookup order,
|
|
13
|
+
// - byte-identical `computeEffectiveOrder` so the three sort consistently,
|
|
14
|
+
// - a coordinated default-order ladder (anthropic-auth 160, AFT 180, MC 200).
|
|
15
|
+
//
|
|
16
|
+
// MC uses `comment-json` (already a dep, Bun-safe) for the WRITE path — a full
|
|
17
|
+
// parse → mutate-one-key → stringify round-trip that preserves comments and
|
|
18
|
+
// sibling plugins' keys. (anthropic-auth uses jsonc-parser's surgical `modify`;
|
|
19
|
+
// AFT and MC use comment-json. Both are interop-safe as long as a sibling key's
|
|
20
|
+
// values AND comments survive — asserted by the interop test.)
|
|
21
|
+
|
|
22
|
+
export const TUI_PREFS_FILE_ENV = "OPENCODE_TUI_PREFERENCES_FILE";
|
|
23
|
+
const FILE_NAME = "tui-preferences.jsonc";
|
|
24
|
+
|
|
25
|
+
export function getTuiPreferencesFile(): string {
|
|
26
|
+
const override = process.env[TUI_PREFS_FILE_ENV];
|
|
27
|
+
if (override) return override;
|
|
28
|
+
const configDir =
|
|
29
|
+
process.env.OPENCODE_CONFIG_DIR ||
|
|
30
|
+
join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "opencode");
|
|
31
|
+
return join(configDir, FILE_NAME);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
35
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Tolerant read: a missing file, parse error, or non-object root all resolve to
|
|
39
|
+
// {} so the sidebar never crashes on hand-edited content. Never throws.
|
|
40
|
+
export async function readTuiPreferencesFile(): Promise<Record<string, unknown>> {
|
|
41
|
+
try {
|
|
42
|
+
const raw = await readFile(getTuiPreferencesFile(), "utf8");
|
|
43
|
+
if (raw.trim() === "") return {};
|
|
44
|
+
const root: unknown = parse(raw);
|
|
45
|
+
return isRecord(root) ? (root as Record<string, unknown>) : {};
|
|
46
|
+
} catch {
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Synchronous tolerant read — used once at slot mount to seed the initial
|
|
52
|
+
// collapse state and effective order WITHOUT a frame of async flicker (the
|
|
53
|
+
// sidebar must render at its final width/collapse on the very first paint).
|
|
54
|
+
// Same tolerance contract as the async reader. Never throws.
|
|
55
|
+
export function readTuiPreferencesFileSync(): Record<string, unknown> {
|
|
56
|
+
try {
|
|
57
|
+
const raw = readFileSync(getTuiPreferencesFile(), "utf8");
|
|
58
|
+
if (raw.trim() === "") return {};
|
|
59
|
+
const root: unknown = parse(raw);
|
|
60
|
+
return isRecord(root) ? (root as Record<string, unknown>) : {};
|
|
61
|
+
} catch {
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const PLUGIN_KEY = "magic-context";
|
|
67
|
+
export const DEFAULT_SLOT_ORDER = 200;
|
|
68
|
+
|
|
69
|
+
export interface MagicContextTuiPrefs {
|
|
70
|
+
forceToTop: boolean;
|
|
71
|
+
order: number;
|
|
72
|
+
startCollapsed: boolean;
|
|
73
|
+
rememberCollapsed: boolean;
|
|
74
|
+
// null = never persisted; seed the UI from `startCollapsed` instead.
|
|
75
|
+
collapsed: boolean | null;
|
|
76
|
+
header: {
|
|
77
|
+
label: string;
|
|
78
|
+
};
|
|
79
|
+
sections: {
|
|
80
|
+
historian: boolean;
|
|
81
|
+
memory: boolean;
|
|
82
|
+
status: boolean;
|
|
83
|
+
dreamer: boolean;
|
|
84
|
+
stats: boolean;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type TuiSections = MagicContextTuiPrefs["sections"];
|
|
89
|
+
|
|
90
|
+
export const DEFAULT_PREFS: MagicContextTuiPrefs = {
|
|
91
|
+
forceToTop: false,
|
|
92
|
+
order: DEFAULT_SLOT_ORDER,
|
|
93
|
+
startCollapsed: false,
|
|
94
|
+
rememberCollapsed: true,
|
|
95
|
+
collapsed: null,
|
|
96
|
+
header: { label: "Magic Context" },
|
|
97
|
+
sections: {
|
|
98
|
+
historian: true,
|
|
99
|
+
memory: true,
|
|
100
|
+
status: true,
|
|
101
|
+
dreamer: true,
|
|
102
|
+
stats: true,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
function bool(value: unknown, fallback: boolean): boolean {
|
|
107
|
+
return typeof value === "boolean" ? value : fallback;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function int(value: unknown, fallback: number, min: number, max: number): number {
|
|
111
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
|
|
112
|
+
return Math.min(Math.max(Math.round(value), min), max);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function label(value: unknown, fallback: string, maxLength: number): string {
|
|
116
|
+
if (typeof value !== "string" || value.length === 0) return fallback;
|
|
117
|
+
return value.slice(0, maxLength);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Per-key validation: every value is independently clamped/defaulted so one bad
|
|
121
|
+
// entry never poisons the rest. Never throws. A missing/non-object MC key →
|
|
122
|
+
// full defaults clone.
|
|
123
|
+
export function resolveMagicContextPrefs(root: Record<string, unknown>): MagicContextTuiPrefs {
|
|
124
|
+
const entry = root[PLUGIN_KEY];
|
|
125
|
+
if (!isRecord(entry)) return structuredClone(DEFAULT_PREFS);
|
|
126
|
+
|
|
127
|
+
const d = DEFAULT_PREFS;
|
|
128
|
+
const header = isRecord(entry.header) ? entry.header : {};
|
|
129
|
+
const sections = isRecord(entry.sections) ? entry.sections : {};
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
forceToTop: bool(entry.forceToTop, d.forceToTop),
|
|
133
|
+
order: int(entry.order, d.order, -10000, 10000),
|
|
134
|
+
startCollapsed: bool(entry.startCollapsed, d.startCollapsed),
|
|
135
|
+
rememberCollapsed: bool(entry.rememberCollapsed, d.rememberCollapsed),
|
|
136
|
+
collapsed: typeof entry.collapsed === "boolean" ? entry.collapsed : null,
|
|
137
|
+
header: {
|
|
138
|
+
label: label(header.label, d.header.label, 24),
|
|
139
|
+
},
|
|
140
|
+
sections: {
|
|
141
|
+
historian: bool(sections.historian, d.sections.historian),
|
|
142
|
+
memory: bool(sections.memory, d.sections.memory),
|
|
143
|
+
status: bool(sections.status, d.sections.status),
|
|
144
|
+
dreamer: bool(sections.dreamer, d.sections.dreamer),
|
|
145
|
+
stats: bool(sections.stats, d.sections.stats),
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const FORCE_TOP_BASE = -100000;
|
|
151
|
+
|
|
152
|
+
// Shared forceToTop convention — MUST stay byte-identical across anthropic-auth,
|
|
153
|
+
// AFT, and magic-context or the three sort inconsistently against each other.
|
|
154
|
+
// Forced plugins sort below FORCE_TOP_BASE, ordered among themselves by their
|
|
155
|
+
// top-level key's position in the file, so users reprioritize by reordering
|
|
156
|
+
// keys. The user-facing `order` knob clamps to -10000..10000, strictly above the
|
|
157
|
+
// forced band, so a manual order can never beat forceToTop. Host slots render
|
|
158
|
+
// ascending by order; OpenCode's built-ins occupy 100-500.
|
|
159
|
+
//
|
|
160
|
+
// Key-naming requirement: plugin keys must be non-integer-like short names (e.g.
|
|
161
|
+
// "magic-context"). JS object key iteration hoists integer-like keys ("0", "42")
|
|
162
|
+
// ahead of string keys, which would skew the indexOf-based ordering of forced
|
|
163
|
+
// plugins. The shared convention requires non-numeric names.
|
|
164
|
+
export function computeEffectiveOrder(
|
|
165
|
+
root: Record<string, unknown>,
|
|
166
|
+
pluginKey: string,
|
|
167
|
+
defaultOrder: number,
|
|
168
|
+
): number {
|
|
169
|
+
const entry = root[pluginKey];
|
|
170
|
+
if (!isRecord(entry)) return defaultOrder;
|
|
171
|
+
if (entry.forceToTop === true) {
|
|
172
|
+
return FORCE_TOP_BASE + Object.keys(root).indexOf(pluginKey);
|
|
173
|
+
}
|
|
174
|
+
return int(entry.order, defaultOrder, -10000, 10000);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const TEMPLATE = `// Shared preferences for OpenCode TUI plugins.
|
|
178
|
+
// One top-level key per plugin (short name). See each plugin's README for its
|
|
179
|
+
// supported settings. This file is safe to hand-edit; plugins update individual
|
|
180
|
+
// keys and preserve the rest (values and comments).
|
|
181
|
+
{}
|
|
182
|
+
`;
|
|
183
|
+
|
|
184
|
+
type JsonValue = string | number | boolean | null;
|
|
185
|
+
|
|
186
|
+
// Set a nested path on a comment-json root, creating intermediate plain objects
|
|
187
|
+
// as needed. Mutating an existing leaf preserves its comments; sibling keys are
|
|
188
|
+
// untouched. Returns false when the path is blocked by a non-object value.
|
|
189
|
+
function setDeep(root: Record<string, unknown>, path: string[], value: JsonValue): boolean {
|
|
190
|
+
let node: Record<string, unknown> = root;
|
|
191
|
+
for (let i = 0; i < path.length - 1; i += 1) {
|
|
192
|
+
const key = path[i];
|
|
193
|
+
const child = node[key];
|
|
194
|
+
if (child === undefined || child === null) {
|
|
195
|
+
node[key] = {};
|
|
196
|
+
} else if (!isRecord(child)) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
node = node[key] as Record<string, unknown>;
|
|
200
|
+
}
|
|
201
|
+
node[path[path.length - 1]] = value;
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function writePreference(pluginKey: string, path: string[], value: JsonValue): Promise<void> {
|
|
206
|
+
const file = getTuiPreferencesFile();
|
|
207
|
+
await mkdir(dirname(file), { recursive: true });
|
|
208
|
+
let text: string;
|
|
209
|
+
try {
|
|
210
|
+
text = await readFile(file, "utf8");
|
|
211
|
+
} catch {
|
|
212
|
+
text = "";
|
|
213
|
+
}
|
|
214
|
+
if (text.trim() === "") text = TEMPLATE;
|
|
215
|
+
|
|
216
|
+
let root: unknown;
|
|
217
|
+
try {
|
|
218
|
+
root = parse(text);
|
|
219
|
+
} catch {
|
|
220
|
+
// The shared file is currently malformed. Skip the write rather than
|
|
221
|
+
// clobber sibling plugins' keys — the user fixes the file, persistence
|
|
222
|
+
// resumes. (Collapse just won't survive restart until then.)
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (!isRecord(root)) root = {};
|
|
226
|
+
if (!setDeep(root as Record<string, unknown>, [pluginKey, ...path], value)) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const next = `${stringify(root, null, 2)}\n`;
|
|
231
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
232
|
+
await writeFile(tmp, next, "utf8");
|
|
233
|
+
await rename(tmp, file);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
let writeChain: Promise<void> = Promise.resolve();
|
|
237
|
+
|
|
238
|
+
// Writes are serialized on a promise chain: each update re-reads the file,
|
|
239
|
+
// applies a comment-preserving edit to one property, and replaces the file
|
|
240
|
+
// atomically (temp + rename in the same directory — the only safe cross-process
|
|
241
|
+
// swap). Best-effort by design; preferences are never worth crashing the TUI.
|
|
242
|
+
export function queueTuiPreferenceUpdate(
|
|
243
|
+
pluginKey: string,
|
|
244
|
+
path: string[],
|
|
245
|
+
value: JsonValue,
|
|
246
|
+
): Promise<void> {
|
|
247
|
+
writeChain = writeChain.then(() => writePreference(pluginKey, path, value)).catch(() => {});
|
|
248
|
+
return writeChain;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const WATCH_DEBOUNCE_MS = 150;
|
|
252
|
+
|
|
253
|
+
// Watches the DIRECTORY, not the file: editors and our own atomic writes replace
|
|
254
|
+
// the file via rename, which kills file-level watchers.
|
|
255
|
+
//
|
|
256
|
+
// Two-stage filtering: (1) a cheap filename pre-filter on the prefs name or our
|
|
257
|
+
// `.tmp`; (2) inside the debounce, re-read and compare against last-seen content
|
|
258
|
+
// — the authority. Some platforms (macOS FSEvents, some inotify backends)
|
|
259
|
+
// misattribute a sibling rename to the real filename, so a name filter alone
|
|
260
|
+
// still produces strays; the content compare is robust against that, coalesced
|
|
261
|
+
// events, and mtime granularity.
|
|
262
|
+
//
|
|
263
|
+
// Returns a disposer; never throws.
|
|
264
|
+
export function watchTuiPreferences(onChange: () => void): () => void {
|
|
265
|
+
const file = getTuiPreferencesFile();
|
|
266
|
+
const name = basename(file);
|
|
267
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
268
|
+
let lastSeen: string | null = null;
|
|
269
|
+
// Seed asynchronously; a real change before the seed resolves still wins
|
|
270
|
+
// because the debounce re-reads fresh and compares against `lastSeen` (null
|
|
271
|
+
// → does not match → fires).
|
|
272
|
+
void readFile(file, "utf8")
|
|
273
|
+
.then((text) => {
|
|
274
|
+
if (lastSeen === null) lastSeen = text;
|
|
275
|
+
})
|
|
276
|
+
.catch(() => {});
|
|
277
|
+
try {
|
|
278
|
+
const watcher = watch(dirname(file), (_event, filename) => {
|
|
279
|
+
const isOurs =
|
|
280
|
+
filename === name ||
|
|
281
|
+
(filename?.startsWith(`${name}.`) && filename.endsWith(".tmp"));
|
|
282
|
+
if (filename != null && !isOurs) return;
|
|
283
|
+
if (timer) clearTimeout(timer);
|
|
284
|
+
timer = setTimeout(() => {
|
|
285
|
+
timer = null;
|
|
286
|
+
void readFile(file, "utf8")
|
|
287
|
+
.catch(() => null)
|
|
288
|
+
.then((text) => {
|
|
289
|
+
if (text === null) return;
|
|
290
|
+
if (text === lastSeen) return;
|
|
291
|
+
lastSeen = text;
|
|
292
|
+
onChange();
|
|
293
|
+
});
|
|
294
|
+
}, WATCH_DEBOUNCE_MS);
|
|
295
|
+
});
|
|
296
|
+
return () => {
|
|
297
|
+
if (timer) clearTimeout(timer);
|
|
298
|
+
watcher.close();
|
|
299
|
+
};
|
|
300
|
+
} catch {
|
|
301
|
+
return () => {};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -5,9 +5,14 @@
|
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { MagicContextRpcClient } from "../../shared/rpc-client";
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
EmbedDetail,
|
|
10
|
+
RpcNotificationMessage,
|
|
11
|
+
SidebarSnapshot,
|
|
12
|
+
StatusDetail,
|
|
13
|
+
} from "../../shared/rpc-types";
|
|
9
14
|
|
|
10
|
-
export type { SidebarSnapshot, StatusDetail };
|
|
15
|
+
export type { EmbedDetail, SidebarSnapshot, StatusDetail };
|
|
11
16
|
|
|
12
17
|
let rpcClient: MagicContextRpcClient | null = null;
|
|
13
18
|
let rpcGeneration = 0;
|
|
@@ -218,6 +223,33 @@ export async function loadStatusDetail(
|
|
|
218
223
|
}
|
|
219
224
|
}
|
|
220
225
|
|
|
226
|
+
const EMPTY_EMBED_DETAIL: EmbedDetail = {
|
|
227
|
+
enabled: false,
|
|
228
|
+
model: "off",
|
|
229
|
+
provider: "off",
|
|
230
|
+
session: { embedded: 0, total: 0 },
|
|
231
|
+
memories: { embedded: 0, total: 0 },
|
|
232
|
+
commits: { embedded: 0, total: 0, gitEnabled: false },
|
|
233
|
+
statusText: "Embedding is off (no provider configured).",
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/** Fetch embedding coverage status for `/ctx-embed` via RPC. */
|
|
237
|
+
export async function loadEmbedDetail(sessionId: string, directory: string): Promise<EmbedDetail> {
|
|
238
|
+
if (!rpcClient) return EMPTY_EMBED_DETAIL;
|
|
239
|
+
try {
|
|
240
|
+
const result = await rpcClient.call<EmbedDetail>("embed-detail", {
|
|
241
|
+
sessionId,
|
|
242
|
+
directory,
|
|
243
|
+
});
|
|
244
|
+
if ((result as unknown as Record<string, unknown>).error) {
|
|
245
|
+
return EMPTY_EMBED_DETAIL;
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
} catch {
|
|
249
|
+
return EMPTY_EMBED_DETAIL;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
221
253
|
/** Get compartment count via RPC. */
|
|
222
254
|
export async function getCompartmentCount(sessionId: string): Promise<number> {
|
|
223
255
|
if (!rpcClient) return 0;
|