opencode-work-context 0.1.10 → 0.1.12
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 +137 -14
- package/bin/opencode-work-context.js +8 -2
- package/commands/wc.md +12 -3
- package/package.json +4 -2
- package/plugin/session-switcher.js +99 -0
- package/plugin/stage-switcher.js +217 -0
- package/plugin/stages-tui.js +12 -1
- package/plugin/tui.js +13 -0
- package/plugin/work-context-modal.js +577 -0
- package/plugin/workspace-switcher.js +87 -0
- package/src/core.js +68 -44
- package/src/identifiers.js +12 -0
- package/src/index.js +2 -0
- package/src/opencode-adapter.js +1 -1
- package/src/stages-renderer.js +2 -0
- package/src/title.js +3 -2
- package/src/tool-definitions.js +16 -27
- package/src/work-context-snapshot.js +81 -0
- package/tools/work_context_update_stage_prompt.js +2 -0
- package/tools/work_context_update_stage_result.js +2 -0
package/plugin/stages-tui.js
CHANGED
|
@@ -10,6 +10,8 @@ const POLL_MS = 1000;
|
|
|
10
10
|
const PATH_TIMEOUT_MS = 5000;
|
|
11
11
|
const PATH_POLL_MS = 25;
|
|
12
12
|
const WORK_CONTEXT_PREFIX = "work" + "_" + "context_";
|
|
13
|
+
const activeTuiInstances = new WeakMap();
|
|
14
|
+
const ACTIVE_TUI_KEY = Symbol.for("opencode-work-context.stages-tui");
|
|
13
15
|
const statusMarker = (status) => ({
|
|
14
16
|
planned: "[ ]",
|
|
15
17
|
in_progress: "[•]",
|
|
@@ -28,6 +30,10 @@ const renderStagesContent = (result, theme, runtime = defaultRuntime) => !result
|
|
|
28
30
|
...renderStagesPanel(result).split("\n").slice(1).map((line) => runtime.jsx("text", { children: line })),
|
|
29
31
|
]
|
|
30
32
|
: [
|
|
33
|
+
runtime.jsx("box", {
|
|
34
|
+
marginBottom: 1,
|
|
35
|
+
children: runtime.jsx("text", { wrapMode: "word", children: runtime.jsx("b", { children: result.data.workspace.title }) }),
|
|
36
|
+
}),
|
|
31
37
|
runtime.jsx("box", {
|
|
32
38
|
flexDirection: "row",
|
|
33
39
|
children: [
|
|
@@ -194,6 +200,7 @@ const ACTIVE_WORKSPACE_SESSION = "__active_" + "work" + "_context_workspace__";
|
|
|
194
200
|
|
|
195
201
|
// Optional TUI entry point. It is deliberately not imported by the server plugin.
|
|
196
202
|
export const tui = async (api, options = {}) => {
|
|
203
|
+
if (activeTuiInstances.has(api) || api?.[ACTIVE_TUI_KEY]) return activeTuiInstances.get(api) || api[ACTIVE_TUI_KEY];
|
|
197
204
|
const projectRoot = await resolveProjectRoot(api, options);
|
|
198
205
|
if (!projectRoot) return;
|
|
199
206
|
|
|
@@ -242,14 +249,18 @@ export const tui = async (api, options = {}) => {
|
|
|
242
249
|
cleanup = () => {
|
|
243
250
|
if (cleaned) return;
|
|
244
251
|
cleaned = true;
|
|
252
|
+
if (activeTuiInstances.get(api) === cleanup) activeTuiInstances.delete(api);
|
|
253
|
+
if (api?.[ACTIVE_TUI_KEY] === cleanup) delete api[ACTIVE_TUI_KEY];
|
|
245
254
|
controller.dispose();
|
|
246
255
|
storageWatcher?.close();
|
|
247
256
|
disposeRegistration(unsubscribe);
|
|
248
257
|
disposeRegistration(unsubscribeTool);
|
|
249
258
|
disposeRegistration(unregisterSlot);
|
|
250
259
|
};
|
|
260
|
+
api[ACTIVE_TUI_KEY] = cleanup;
|
|
261
|
+
activeTuiInstances.set(api, cleanup);
|
|
251
262
|
api.lifecycle?.onDispose?.(cleanup);
|
|
252
|
-
|
|
263
|
+
return cleanup;
|
|
253
264
|
};
|
|
254
265
|
|
|
255
266
|
export default { id: "work-context-stages", tui };
|
package/plugin/tui.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import stages from "./stages-tui.js";
|
|
2
|
+
import modal from "./work-context-modal.js";
|
|
3
|
+
|
|
4
|
+
// Keep the sidebar and modal adapters composed under one optional TUI entry.
|
|
5
|
+
// The server plugin remains a separate runtime contract.
|
|
6
|
+
export const tui = async (api, options = {}) => {
|
|
7
|
+
await stages.tui(api, options);
|
|
8
|
+
await modal.tui(api, options);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// Preserve the original package entrypoint identity so nested OpenCode configs
|
|
12
|
+
// deduplicate older stages-only loaders before either can register a second slot.
|
|
13
|
+
export default { id: stages.id, tui };
|
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { readWorkContextSnapshot } from "../src/work-context-snapshot.js";
|
|
4
|
+
import { normalizeStageId, normalizeWorkspaceId } from "../src/identifiers.js";
|
|
5
|
+
import { jsx } from "@opentui/solid/jsx-runtime";
|
|
6
|
+
import { createSignal } from "solid-js";
|
|
7
|
+
import { openStageSwitcher } from "./stage-switcher.js";
|
|
8
|
+
import { openWorkspaceSwitcher } from "./workspace-switcher.js";
|
|
9
|
+
import { openSessionSwitcher } from "./session-switcher.js";
|
|
10
|
+
|
|
11
|
+
const COMMAND_NAME = "work_context.open";
|
|
12
|
+
const ACTIONS_COMMAND_NAME = "work_context.actions";
|
|
13
|
+
const SHORTCUT = "<leader>w";
|
|
14
|
+
const STAGE_SHORTCUT = "<leader>s";
|
|
15
|
+
const WORKSPACE_SHORTCUT = "<leader>o";
|
|
16
|
+
const SESSION_SHORTCUT = "<leader>e";
|
|
17
|
+
const DEBOUNCE_MS = 150;
|
|
18
|
+
const POLL_MS = 1000;
|
|
19
|
+
const MARKERS = { planned: "[ ]", in_progress: "[•]", completed: "[✓]", cancelled: "[!]" };
|
|
20
|
+
const COMMANDS = { list: "list", resume: "resume", handoff: "stage handoff", finish: "stage finish" };
|
|
21
|
+
const activeTuiInstances = new WeakMap();
|
|
22
|
+
|
|
23
|
+
export const WORK_CONTEXT_ACTIONS = [
|
|
24
|
+
{ value: "help", title: "Help", description: "Show work-context command help", category: "General" },
|
|
25
|
+
{ value: "switch.stage", title: "Switch stage", description: "Open a stage session in the current workspace", footer: "ctrl+alt+w s", category: "Navigation" },
|
|
26
|
+
{ value: "switch.workspace", title: "Switch workspace", description: "Open a workspace session", footer: "ctrl+alt+w o", category: "Navigation" },
|
|
27
|
+
{ value: "switch.session", title: "Switch session", description: "Open an active or historical work session", footer: "ctrl+alt+w e", category: "Navigation" },
|
|
28
|
+
{ value: "list", title: "Browse workspaces", description: "Search workspaces and their stages", category: "General" },
|
|
29
|
+
{ value: "browse.stages", title: "Browse stages", description: "Search stages across every workspace", category: "General" },
|
|
30
|
+
{ value: "browse.sessions", title: "Browse sessions", description: "Search active and historical work sessions", category: "General" },
|
|
31
|
+
{ value: "create", title: "Create workspace", description: "Create a workspace from a title", category: "Workspace" },
|
|
32
|
+
{ value: "workspace.list", title: "Inspect workspace", description: "List one workspace and its stages", category: "Workspace" },
|
|
33
|
+
{ value: "workspace.rename", title: "Rename workspace", description: "Change a workspace title", category: "Workspace" },
|
|
34
|
+
{ value: "workspace.finish", title: "Finish workspace", description: "Complete a workspace", category: "Workspace" },
|
|
35
|
+
{ value: "stage.add", title: "Add stage", description: "Create a planned stage with an optional prompt", category: "Stage" },
|
|
36
|
+
{ value: "resume", title: "Resume stage", description: "Start or continue work on a stage", category: "Stage" },
|
|
37
|
+
{ value: "stage.rename", title: "Rename stage", description: "Change a stage title", category: "Stage" },
|
|
38
|
+
{ value: "stage.update", title: "Update stage description", description: "Replace a stage description", category: "Stage" },
|
|
39
|
+
{ value: "stage.update-prompt", title: "Update stage prompt", description: "Replace the detailed working prompt", category: "Stage" },
|
|
40
|
+
{ value: "stage.update-result", title: "Update stage result", description: "Replace an existing completion result", category: "Stage" },
|
|
41
|
+
{ value: "stage.archive", title: "Archive stage", description: "Archive a non-active stage", category: "Stage" },
|
|
42
|
+
{ value: "stage.handoff", title: "Hand off stage", description: "Close this session and preserve continuation", category: "Stage" },
|
|
43
|
+
{ value: "stage.abandon", title: "Abandon stage session", description: "Close this session and keep the stage resumable", category: "Stage" },
|
|
44
|
+
{ value: "stage.finish", title: "Finish stage", description: "Complete a stage and review knowledge", category: "Stage" },
|
|
45
|
+
{ value: "session.rename", title: "Rename work session", description: "Change the current work-context session summary", category: "Session" },
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const disposeRegistration = (registration) => {
|
|
49
|
+
if (typeof registration === "function") registration();
|
|
50
|
+
else registration?.dispose?.();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const closeDialog = (api) => api?.ui?.dialog?.clear?.();
|
|
54
|
+
const textOf = (value) => String(value || "").toLocaleLowerCase();
|
|
55
|
+
const matches = (value, query) => !query || textOf(value).includes(textOf(query));
|
|
56
|
+
|
|
57
|
+
const normalizedId = (normalize, value) => {
|
|
58
|
+
try { return normalize(value); } catch { return null; }
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Prepare a canonical command without parsing or executing it. */
|
|
62
|
+
export const prepareWorkContextCommand = (action, workspace, stage) => {
|
|
63
|
+
const normalizedWorkspace = normalizedId(normalizeWorkspaceId, workspace);
|
|
64
|
+
if (!normalizedWorkspace) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Invalid workspace ID" } };
|
|
65
|
+
if (!Object.hasOwn(COMMANDS, action)) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Unsupported work-context command" } };
|
|
66
|
+
if (action === "list") return { ok: true, command: `/wc workspace list ${normalizedWorkspace}` };
|
|
67
|
+
const normalizedStage = normalizedId(normalizeStageId, stage);
|
|
68
|
+
if (!normalizedStage) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Invalid stage ID" } };
|
|
69
|
+
return { ok: true, command: `/wc ${COMMANDS[action]} ${normalizedWorkspace} ${normalizedStage}` };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** Use only a host-provided prompt append bridge; never submit the prompt. */
|
|
73
|
+
export const insertPromptCommand = (api, command) => {
|
|
74
|
+
if (typeof command !== "string" || !command.startsWith("/wc ")) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Invalid prepared command" } };
|
|
75
|
+
const append = api?.ui?.prompt?.append || api?.prompt?.append;
|
|
76
|
+
if (typeof append !== "function") return { ok: false, command, error: { code: "PROMPT_INSERT_UNAVAILABLE", message: "Prompt insertion is unavailable; submit this command manually." } };
|
|
77
|
+
try { append(command); } catch (error) {
|
|
78
|
+
return { ok: false, command, error: { code: error.code || "PROMPT_INSERT_ERROR", message: error.message || "Prompt insertion failed; submit this command manually." } };
|
|
79
|
+
}
|
|
80
|
+
return { ok: true, command, submitted: false };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** Append through the public TUI endpoint; keep compatibility bridges as a fallback. */
|
|
84
|
+
export const appendPromptCommand = async (api, command) => {
|
|
85
|
+
if (typeof command !== "string" || !command.startsWith("/wc ")) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Invalid prepared command" } };
|
|
86
|
+
if (typeof api?.client?.tui?.appendPrompt === "function") {
|
|
87
|
+
try {
|
|
88
|
+
await api.client.tui.appendPrompt({ directory: api.state?.path?.directory || api.state?.path?.worktree, text: command }, { throwOnError: true });
|
|
89
|
+
return { ok: true, command, submitted: false };
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return { ok: false, command, error: { code: error.code || "PROMPT_INSERT_ERROR", message: error.message || "Prompt insertion failed" } };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return insertPromptCommand(api, command);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const quoteWorkContextArgument = (value) => JSON.stringify(String(value));
|
|
98
|
+
|
|
99
|
+
export const filterWorkspaces = (workspaces = [], query = "") => workspaces
|
|
100
|
+
.map((workspace) => ({
|
|
101
|
+
...workspace,
|
|
102
|
+
stages: (workspace.stages || []).filter((stage) => matches(`${stage.id} ${stage.title} ${stage.description}`, query)),
|
|
103
|
+
}))
|
|
104
|
+
.filter((workspace) => matches(`${workspace.id} ${workspace.title} ${workspace.status}`, query) || workspace.stages.length);
|
|
105
|
+
|
|
106
|
+
export const createWorkContextModalController = ({
|
|
107
|
+
projectRoot,
|
|
108
|
+
sessionId,
|
|
109
|
+
read = readWorkContextSnapshot,
|
|
110
|
+
debounceMs = DEBOUNCE_MS,
|
|
111
|
+
pollMs = POLL_MS,
|
|
112
|
+
createSignal: makeSignal = createSignal,
|
|
113
|
+
onClose = () => {},
|
|
114
|
+
onCommand = () => {},
|
|
115
|
+
} = {}) => {
|
|
116
|
+
const [version, setVersion] = makeSignal(0);
|
|
117
|
+
const [query, setQuery] = makeSignal("");
|
|
118
|
+
const [workspaceIndex, setWorkspaceIndex] = makeSignal(0);
|
|
119
|
+
const [stageIndex, setStageIndex] = makeSignal(0);
|
|
120
|
+
const [details, setDetails] = makeSignal(false);
|
|
121
|
+
let result = null;
|
|
122
|
+
let successful = null;
|
|
123
|
+
let timer;
|
|
124
|
+
let poller;
|
|
125
|
+
let disposed = false;
|
|
126
|
+
let onSelectionChange = () => {};
|
|
127
|
+
|
|
128
|
+
const refresh = async () => {
|
|
129
|
+
if (disposed) return;
|
|
130
|
+
try {
|
|
131
|
+
const next = await read({ projectRoot, sessionId });
|
|
132
|
+
if (disposed) return;
|
|
133
|
+
if (next?.ok) { successful = next; result = next; }
|
|
134
|
+
else result = successful ? { ...successful, stale: true, error: next?.error || { code: "STORAGE_ERROR" } } : next;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (disposed) return;
|
|
137
|
+
result = successful
|
|
138
|
+
? { ...successful, stale: true, error: { code: error.code || "STORAGE_ERROR", message: error.message } }
|
|
139
|
+
: { ok: false, error: { code: error.code || "STORAGE_ERROR", message: error.message } };
|
|
140
|
+
}
|
|
141
|
+
setVersion((value) => value + 1);
|
|
142
|
+
};
|
|
143
|
+
const scheduleRefresh = () => {
|
|
144
|
+
if (disposed) return;
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
timer = setTimeout(() => { timer = undefined; void refresh(); }, debounceMs);
|
|
147
|
+
};
|
|
148
|
+
const snapshot = () => { version(); return result; };
|
|
149
|
+
const workspaces = () => filterWorkspaces(snapshot()?.data?.workspaces || [], query());
|
|
150
|
+
const selectedWorkspace = () => workspaces()[Math.min(workspaceIndex(), Math.max(0, workspaces().length - 1))] || null;
|
|
151
|
+
const selectedStage = () => selectedWorkspace()?.stages?.[Math.min(stageIndex(), Math.max(0, (selectedWorkspace()?.stages || []).length - 1))] || null;
|
|
152
|
+
const [preparedCommand, setPreparedCommand] = makeSignal(null);
|
|
153
|
+
const prepareCommand = (action) => {
|
|
154
|
+
const workspace = selectedWorkspace()?.id;
|
|
155
|
+
const stage = selectedStage()?.id;
|
|
156
|
+
const prepared = prepareWorkContextCommand(action, workspace, stage);
|
|
157
|
+
setPreparedCommand(prepared);
|
|
158
|
+
return prepared;
|
|
159
|
+
};
|
|
160
|
+
const insertCommand = (action) => {
|
|
161
|
+
const prepared = prepareCommand(action);
|
|
162
|
+
if (!prepared.ok) return prepared;
|
|
163
|
+
const inserted = onCommand(prepared.command);
|
|
164
|
+
const result = inserted && typeof inserted === "object" ? { ...prepared, ...inserted } : prepared;
|
|
165
|
+
setPreparedCommand(result);
|
|
166
|
+
return result;
|
|
167
|
+
};
|
|
168
|
+
const moveWorkspace = (delta) => {
|
|
169
|
+
const count = workspaces().length;
|
|
170
|
+
if (count) setWorkspaceIndex(Math.max(0, Math.min(count - 1, workspaceIndex() + delta)));
|
|
171
|
+
setStageIndex(0);
|
|
172
|
+
};
|
|
173
|
+
const moveStage = (delta) => {
|
|
174
|
+
const count = selectedWorkspace()?.stages?.length || 0;
|
|
175
|
+
if (count) setStageIndex(Math.max(0, Math.min(count - 1, stageIndex() + delta)));
|
|
176
|
+
};
|
|
177
|
+
const handleKey = (event) => {
|
|
178
|
+
const rawKey = typeof event === "string" ? event : event?.key || event?.name;
|
|
179
|
+
const key = ({ ArrowUp: "up", ArrowDown: "down", ArrowLeft: "left", ArrowRight: "right", Escape: "escape" })[rawKey] || rawKey;
|
|
180
|
+
if (key === "up") moveWorkspace(-1);
|
|
181
|
+
else if (key === "down") moveWorkspace(1);
|
|
182
|
+
else if (key === "left") moveStage(-1);
|
|
183
|
+
else if (key === "right") moveStage(1);
|
|
184
|
+
else if (key === "Enter") setDetails((value) => !value);
|
|
185
|
+
else if (key === "d") setDetails((value) => !value);
|
|
186
|
+
else if (key === "l") insertCommand("list");
|
|
187
|
+
else if (key === "r") insertCommand("resume");
|
|
188
|
+
else if (key === "h") insertCommand("handoff");
|
|
189
|
+
else if (key === "f") insertCommand("finish");
|
|
190
|
+
else if (key === "Backspace") setQuery(query().slice(0, -1));
|
|
191
|
+
else if (key === "escape") { if (query()) setQuery(""); else onClose(); }
|
|
192
|
+
else if (key === "/") setQuery("");
|
|
193
|
+
else if (typeof key === "string" && key.length === 1 && !event?.ctrlKey && !event?.metaKey) setQuery(query() + key);
|
|
194
|
+
setVersion((value) => value + 1);
|
|
195
|
+
onSelectionChange();
|
|
196
|
+
};
|
|
197
|
+
void refresh();
|
|
198
|
+
if (pollMs) poller = setInterval(scheduleRefresh, pollMs);
|
|
199
|
+
return {
|
|
200
|
+
snapshot, query, setQuery, workspaces, selectedWorkspace, selectedStage, details, preparedCommand, prepareCommand, insertCommand, handleKey, scheduleRefresh,
|
|
201
|
+
onSelectionChange: (handler) => { onSelectionChange = typeof handler === "function" ? handler : () => {}; },
|
|
202
|
+
dispose: () => { disposed = true; clearTimeout(timer); clearInterval(poller); },
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const line = (runtime, children, props = {}) => runtime.jsx("text", { ...props, children });
|
|
207
|
+
const marker = (status) => MARKERS[status] || "[?]";
|
|
208
|
+
|
|
209
|
+
export const renderWorkContextModal = (controller, theme, runtime = { jsx }, { maxListHeight = 24 } = {}) => {
|
|
210
|
+
let workspaceScroll;
|
|
211
|
+
let stageScroll;
|
|
212
|
+
controller.onSelectionChange?.(() => queueMicrotask(() => {
|
|
213
|
+
try { workspaceScroll?.scrollChildIntoView?.(`workspace-${controller.selectedWorkspace()?.id}`); } catch {}
|
|
214
|
+
try { stageScroll?.scrollChildIntoView?.(`stage-${controller.selectedStage()?.id}`); } catch {}
|
|
215
|
+
}));
|
|
216
|
+
return runtime.jsx("box", {
|
|
217
|
+
flexDirection: "column", gap: 1, paddingLeft: 1, paddingRight: 1, paddingBottom: 1, flexGrow: 1, focusable: true, onKeyDown: controller.handleKey,
|
|
218
|
+
children: () => {
|
|
219
|
+
const result = controller.snapshot();
|
|
220
|
+
const workspaces = controller.workspaces();
|
|
221
|
+
const selected = controller.selectedWorkspace();
|
|
222
|
+
const stage = controller.selectedStage();
|
|
223
|
+
const data = result?.data;
|
|
224
|
+
const status = result?.stale ? `STALE · ${result.error?.code || "STORAGE_ERROR"}` : result?.ok === false ? `ERROR · ${result.error?.code || "STORAGE_ERROR"}` : "LIVE";
|
|
225
|
+
const muted = theme?.current?.textMuted;
|
|
226
|
+
const selectedWorkspaceIndex = selected ? workspaces.indexOf(selected) : -1;
|
|
227
|
+
const selectedStageIndex = stage && selected ? (selected.stages || []).indexOf(stage) : -1;
|
|
228
|
+
const workspaceItems = workspaces.map((workspace, index) => runtime.jsx("box", {
|
|
229
|
+
id: `workspace-${workspace.id}`, flexDirection: "row", gap: 1, children: [
|
|
230
|
+
line(runtime, index === selectedWorkspaceIndex ? ">" : " ", { flexShrink: 0, marginRight: 1, fg: index === selectedWorkspaceIndex ? theme?.current?.text : muted }),
|
|
231
|
+
line(runtime, `${workspace.id} ${workspace.title}`, { flexGrow: 1, flexShrink: 1, wrapMode: "word", fg: workspace.id === data?.currentWorkspace ? theme?.current?.warning : theme?.current?.text }),
|
|
232
|
+
line(runtime, workspace.status, { flexShrink: 0, fg: muted }),
|
|
233
|
+
],
|
|
234
|
+
}));
|
|
235
|
+
const stageItems = selected ? (selected.stages || []).map((item, index) => runtime.jsx("box", {
|
|
236
|
+
id: `stage-${item.id}`, flexDirection: "row", children: [
|
|
237
|
+
line(runtime, index === selectedStageIndex ? ">" : " ", { flexShrink: 0, marginRight: 1, fg: index === selectedStageIndex ? theme?.current?.text : muted }),
|
|
238
|
+
line(runtime, `${marker(item.status)} ${item.id}. ${item.title}${item.current ? " · current" : ""}`, { flexGrow: 1, flexShrink: 1, wrapMode: "word", fg: item.current ? theme?.current?.warning : theme?.current?.text }),
|
|
239
|
+
],
|
|
240
|
+
})) : [];
|
|
241
|
+
const children = [runtime.jsx("box", {
|
|
242
|
+
flexDirection: "row", justifyContent: "space-between", paddingLeft: 2, paddingRight: 2,
|
|
243
|
+
children: [
|
|
244
|
+
line(runtime, runtime.jsx("b", { children: "Work Context" }), { fg: theme?.current?.text }),
|
|
245
|
+
line(runtime, "esc", { fg: muted }),
|
|
246
|
+
],
|
|
247
|
+
}), runtime.jsx("box", {
|
|
248
|
+
flexDirection: "column", gap: 0, paddingLeft: 2, paddingRight: 2,
|
|
249
|
+
children: [
|
|
250
|
+
line(runtime, `Filter ${controller.query() || "type to filter"}`, { fg: controller.query() ? theme?.current?.text : muted }),
|
|
251
|
+
line(runtime, `Status ${status}`, { fg: result?.stale || result?.ok === false ? theme?.current?.warning : muted }),
|
|
252
|
+
],
|
|
253
|
+
})];
|
|
254
|
+
if (result?.ok === false && !data) children.push(runtime.jsx("box", { paddingLeft: 2, children: line(runtime, result.error?.message || "Cannot read work-context storage", { fg: theme?.current?.error }) }));
|
|
255
|
+
if (!workspaces.length) children.push(line(runtime, controller.query() ? "No matching workspaces or stages." : "No workspaces found.", { paddingLeft: 2, fg: muted }));
|
|
256
|
+
else children.push(runtime.jsx("box", {
|
|
257
|
+
flexDirection: "row", gap: 2, flexGrow: 1, paddingLeft: 2, paddingRight: 2,
|
|
258
|
+
children: [
|
|
259
|
+
runtime.jsx("box", { flexDirection: "column", width: "40%", flexShrink: 0, children: [
|
|
260
|
+
line(runtime, "Workspaces", { fg: muted }),
|
|
261
|
+
runtime.jsx("scrollbox", { ref: (node) => { workspaceScroll = node; }, maxHeight: maxListHeight, scrollY: true, verticalScrollbarOptions: { visible: false }, children: workspaceItems }),
|
|
262
|
+
] }),
|
|
263
|
+
runtime.jsx("box", { flexDirection: "column", flexGrow: 1, flexShrink: 1, children: [
|
|
264
|
+
line(runtime, selected ? `Stages ${selected.title}` : "Stages", { wrapMode: "word", fg: muted }),
|
|
265
|
+
runtime.jsx("scrollbox", { ref: (node) => { stageScroll = node; }, maxHeight: maxListHeight, scrollY: true, verticalScrollbarOptions: { visible: false }, children: stageItems }),
|
|
266
|
+
...(controller.details() && stage ? [
|
|
267
|
+
line(runtime, "Details", { marginTop: 1, fg: muted }),
|
|
268
|
+
line(runtime, stage.description || "No description.", { wrapMode: "word", fg: theme?.current?.text }),
|
|
269
|
+
line(runtime, `Sessions ${(selected.sessions || []).length}`, { fg: muted }),
|
|
270
|
+
...(selected.sessions || []).map((session) => line(runtime, `${session.state || "unknown"} ${session.session_id || "unknown"} stage ${session.stage || "-"}`, { wrapMode: "word", fg: muted })),
|
|
271
|
+
] : []),
|
|
272
|
+
] }),
|
|
273
|
+
],
|
|
274
|
+
}));
|
|
275
|
+
const prepared = controller.preparedCommand();
|
|
276
|
+
if (prepared) children.push(runtime.jsx("box", { paddingLeft: 2, paddingRight: 2, children: line(runtime, prepared.command
|
|
277
|
+
? `Prepared (not submitted): ${prepared.command}${prepared.error ? ` · ${prepared.error.message || prepared.error.code}` : ""}`
|
|
278
|
+
: `Command unavailable: ${prepared.error?.message || prepared.error?.code || "INVALID_ARGUMENT"}`, { fg: prepared.error ? theme?.current?.warning : theme?.current?.text }) }));
|
|
279
|
+
children.push(runtime.jsx("box", { paddingLeft: 2, paddingRight: 2, children: line(runtime, "↑↓ workspace · ←→ stage · d/Enter details · l list · r resume · h handoff · f finish", { fg: muted }) }));
|
|
280
|
+
return children;
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
export const buildWorkContextActionCommand = (action, { workspace, stage, value, prompt } = {}) => {
|
|
286
|
+
if (action === "help" || action === "list") return { ok: true, command: `/wc ${action}` };
|
|
287
|
+
if (action === "create") return value?.trim()
|
|
288
|
+
? { ok: true, command: `/wc create ${quoteWorkContextArgument(value.trim())}` }
|
|
289
|
+
: { ok: false, error: { code: "INVALID_ARGUMENT", message: "Title is required" } };
|
|
290
|
+
if (action === "session.rename") return value?.trim()
|
|
291
|
+
? { ok: true, command: `/wc session rename ${quoteWorkContextArgument(value.trim())}` }
|
|
292
|
+
: { ok: false, error: { code: "INVALID_ARGUMENT", message: "Summary is required" } };
|
|
293
|
+
const normalizedWorkspace = normalizedId(normalizeWorkspaceId, workspace);
|
|
294
|
+
if (!normalizedWorkspace) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Invalid workspace ID" } };
|
|
295
|
+
if (action === "workspace.list") return { ok: true, command: `/wc workspace list ${normalizedWorkspace}` };
|
|
296
|
+
if (action === "workspace.finish") return { ok: true, command: `/wc workspace finish ${normalizedWorkspace}` };
|
|
297
|
+
if (action === "workspace.rename") return value?.trim()
|
|
298
|
+
? { ok: true, command: `/wc workspace rename ${normalizedWorkspace} ${quoteWorkContextArgument(value.trim())}` }
|
|
299
|
+
: { ok: false, error: { code: "INVALID_ARGUMENT", message: "Title is required" } };
|
|
300
|
+
if (action === "stage.add") {
|
|
301
|
+
if (!value?.trim()) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Title is required" } };
|
|
302
|
+
return { ok: true, command: `/wc stage add ${normalizedWorkspace} ${quoteWorkContextArgument(value.trim())}${prompt?.trim() ? ` ${quoteWorkContextArgument(prompt.trim())}` : ""}` };
|
|
303
|
+
}
|
|
304
|
+
const normalizedStage = normalizedId(normalizeStageId, stage);
|
|
305
|
+
if (!normalizedStage) return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Invalid stage ID" } };
|
|
306
|
+
const commandName = action.replace(".", " ");
|
|
307
|
+
if (["resume", "stage.archive", "stage.handoff", "stage.abandon", "stage.finish"].includes(action)) return { ok: true, command: `/wc ${commandName} ${normalizedWorkspace} ${normalizedStage}` };
|
|
308
|
+
const inputCommands = { "stage.rename": "title", "stage.update": "description", "stage.update-prompt": "prompt", "stage.update-result": "result" };
|
|
309
|
+
if (Object.hasOwn(inputCommands, action)) return value?.trim()
|
|
310
|
+
? { ok: true, command: `/wc ${commandName} ${normalizedWorkspace} ${normalizedStage} ${quoteWorkContextArgument(value.trim())}` }
|
|
311
|
+
: { ok: false, error: { code: "INVALID_ARGUMENT", message: `${inputCommands[action][0].toUpperCase()}${inputCommands[action].slice(1)} is required` } };
|
|
312
|
+
return { ok: false, error: { code: "INVALID_ARGUMENT", message: "Unsupported work-context action" } };
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
export const createWorkContextActionFlow = (api, { projectRoot, sessionId, read = readWorkContextSnapshot, openStage = openStageSwitcher } = {}) => {
|
|
316
|
+
const stack = api?.ui?.dialog;
|
|
317
|
+
const toast = (message, variant = "warning") => api?.ui?.toast?.({ message, variant });
|
|
318
|
+
const showSelect = (title, options, onSelect, props = {}) => stack.replace(() => api.ui.DialogSelect({ title, placeholder: "Search", options, onSelect, ...props }));
|
|
319
|
+
const showPrompt = (title, placeholder, onConfirm) => stack.replace(() => api.ui.DialogPrompt({ title, placeholder, onConfirm }));
|
|
320
|
+
const insert = async (prepared) => {
|
|
321
|
+
if (!prepared.ok) { toast(prepared.error.message); return; }
|
|
322
|
+
stack.clear();
|
|
323
|
+
const inserted = await appendPromptCommand(api, prepared.command);
|
|
324
|
+
toast(inserted.ok ? "Command added to prompt" : inserted.error?.message || "Prompt insertion failed", inserted.ok ? "success" : "error");
|
|
325
|
+
};
|
|
326
|
+
const requireValue = (title, placeholder, action, context = {}) => showPrompt(title, placeholder, (value) => {
|
|
327
|
+
const prepared = buildWorkContextActionCommand(action, { ...context, value });
|
|
328
|
+
if (!prepared.ok) { toast(prepared.error.message); requireValue(title, placeholder, action, context); return; }
|
|
329
|
+
void insert(prepared);
|
|
330
|
+
});
|
|
331
|
+
const addStage = (workspace) => showPrompt("Add stage", "Stage title", (title) => {
|
|
332
|
+
if (!title?.trim()) { toast("Title is required"); addStage(workspace); return; }
|
|
333
|
+
showPrompt("Add stage prompt", "Optional detailed prompt", (prompt) => void insert(buildWorkContextActionCommand("stage.add", { workspace, value: title, prompt })));
|
|
334
|
+
});
|
|
335
|
+
const runStageAction = (action, workspace, stage) => {
|
|
336
|
+
const context = { workspace, stage };
|
|
337
|
+
if (action === "stage.rename") requireValue("Rename stage", "New title", action, context);
|
|
338
|
+
else if (action === "stage.update") requireValue("Update stage description", "Description", action, context);
|
|
339
|
+
else if (action === "stage.update-prompt") requireValue("Update stage prompt", "Detailed prompt", action, context);
|
|
340
|
+
else if (action === "stage.update-result") requireValue("Update stage result", "Completion result", action, context);
|
|
341
|
+
else void insert(buildWorkContextActionCommand(action, context));
|
|
342
|
+
};
|
|
343
|
+
const selectStage = (action, workspace, stages) => showSelect("Select stage", stages.map((stage) => ({
|
|
344
|
+
title: `${marker(stage.status)} ${stage.id}. ${stage.title}`,
|
|
345
|
+
description: stage.description || stage.status,
|
|
346
|
+
value: stage,
|
|
347
|
+
})), (option) => runStageAction(action, workspace, option.value.id));
|
|
348
|
+
const loadWorkspaces = async () => {
|
|
349
|
+
let result;
|
|
350
|
+
try { result = await read({ projectRoot, sessionId }); }
|
|
351
|
+
catch (error) { toast(error.message || "Cannot read work-context storage", "error"); return null; }
|
|
352
|
+
if (!result?.ok) { toast(result?.error?.message || "Cannot read work-context storage", "error"); return null; }
|
|
353
|
+
const workspaces = result.data?.workspaces || [];
|
|
354
|
+
if (!workspaces.length) { toast("No workspaces found"); return null; }
|
|
355
|
+
return { workspaces, currentWorkspace: result.data?.currentWorkspace };
|
|
356
|
+
};
|
|
357
|
+
const selectWorkspace = async (action) => {
|
|
358
|
+
const loaded = await loadWorkspaces();
|
|
359
|
+
if (!loaded) return;
|
|
360
|
+
const { workspaces } = loaded;
|
|
361
|
+
showSelect("Select workspace", workspaces.map((workspace) => ({
|
|
362
|
+
title: `${workspace.id} ${workspace.title}`,
|
|
363
|
+
description: workspace.status,
|
|
364
|
+
value: workspace,
|
|
365
|
+
})), (option) => {
|
|
366
|
+
const workspace = option.value;
|
|
367
|
+
if (action === "workspace.list" || action === "workspace.finish") void insert(buildWorkContextActionCommand(action, { workspace: workspace.id }));
|
|
368
|
+
else if (action === "workspace.rename") requireValue("Rename workspace", "New title", action, { workspace: workspace.id });
|
|
369
|
+
else if (action === "stage.add") addStage(workspace.id);
|
|
370
|
+
else if (!workspace.stages?.length) toast("No stages found");
|
|
371
|
+
else selectStage(action, workspace.id, workspace.stages);
|
|
372
|
+
});
|
|
373
|
+
};
|
|
374
|
+
const progress = (workspace) => {
|
|
375
|
+
const counts = (workspace.stages || []).reduce((all, stage) => ({ ...all, [stage.status]: (all[stage.status] || 0) + 1 }), {});
|
|
376
|
+
return [["completed", "✓"], ["in_progress", "•"], ["planned", "□"], ["cancelled", "!"]]
|
|
377
|
+
.flatMap(([status, symbol]) => counts[status] ? [`${symbol}${counts[status]}`] : [])
|
|
378
|
+
.join(" ") || "No stages";
|
|
379
|
+
};
|
|
380
|
+
const showWorkspaceActions = (workspace) => showSelect(`${workspace.id} ${workspace.title}`, [
|
|
381
|
+
{ title: "Back to workspaces", value: "back", category: "Navigation" },
|
|
382
|
+
...WORK_CONTEXT_ACTIONS.filter((action) => ["workspace.list", "stage.add", "workspace.rename", "workspace.finish"].includes(action.value)),
|
|
383
|
+
], (option) => {
|
|
384
|
+
if (option.value === "back") { void browseWorkspaces(); return; }
|
|
385
|
+
if (option.value === "workspace.list" || option.value === "workspace.finish") void insert(buildWorkContextActionCommand(option.value, { workspace: workspace.id }));
|
|
386
|
+
else if (option.value === "workspace.rename") requireValue("Rename workspace", "New title", option.value, { workspace: workspace.id });
|
|
387
|
+
else if (option.value === "stage.add") addStage(workspace.id);
|
|
388
|
+
});
|
|
389
|
+
const showStageActions = (workspace, stage, { backTitle = "Back to workspaces", onBack = () => void browseWorkspaces() } = {}) => showSelect(`${workspace.id}/${stage.id} ${stage.title}`, [
|
|
390
|
+
{ title: backTitle, value: "back", category: "Navigation" },
|
|
391
|
+
...WORK_CONTEXT_ACTIONS.filter((action) => ["resume", "stage.rename", "stage.update", "stage.update-prompt", "stage.update-result", "stage.archive", "stage.handoff", "stage.abandon", "stage.finish"].includes(action.value)),
|
|
392
|
+
], (option) => {
|
|
393
|
+
if (option.value === "back") { onBack(); return; }
|
|
394
|
+
runStageAction(option.value, workspace.id, stage.id);
|
|
395
|
+
});
|
|
396
|
+
const browseWorkspaces = async () => {
|
|
397
|
+
const loaded = await loadWorkspaces();
|
|
398
|
+
if (!loaded) return;
|
|
399
|
+
const rank = { in_progress: 0, planned: 1, completed: 2, cancelled: 3 };
|
|
400
|
+
const workspaces = [...loaded.workspaces].sort((left, right) => {
|
|
401
|
+
if (left.id === loaded.currentWorkspace) return -1;
|
|
402
|
+
if (right.id === loaded.currentWorkspace) return 1;
|
|
403
|
+
return (rank[left.status] ?? 9) - (rank[right.status] ?? 9) || left.id.localeCompare(right.id);
|
|
404
|
+
});
|
|
405
|
+
const options = workspaces.flatMap((workspace) => {
|
|
406
|
+
const category = `${workspace.id} · ${workspace.title} · ${workspace.status}`;
|
|
407
|
+
return [
|
|
408
|
+
{ title: "Overview", description: workspace.status, footer: progress(workspace), category, value: { type: "workspace", workspace } },
|
|
409
|
+
...(workspace.stages || []).map((stage) => ({
|
|
410
|
+
title: `${marker(stage.status)} ${stage.id}. ${stage.title}${stage.current ? " · current" : ""}`,
|
|
411
|
+
description: stage.description || stage.status,
|
|
412
|
+
category,
|
|
413
|
+
value: { type: "stage", workspace, stage },
|
|
414
|
+
})),
|
|
415
|
+
];
|
|
416
|
+
});
|
|
417
|
+
showSelect("Workspaces", options, (option) => {
|
|
418
|
+
if (option.value.type === "workspace") showWorkspaceActions(option.value.workspace);
|
|
419
|
+
else showStageActions(option.value.workspace, option.value.stage);
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
const browseStages = async () => {
|
|
423
|
+
const loaded = await loadWorkspaces();
|
|
424
|
+
if (!loaded) return;
|
|
425
|
+
const rank = { in_progress: 0, planned: 1, completed: 2, cancelled: 3 };
|
|
426
|
+
const entries = loaded.workspaces.flatMap((workspace) => (workspace.stages || []).map((stage) => ({ workspace, stage })));
|
|
427
|
+
if (!entries.length) { toast("No stages found"); return; }
|
|
428
|
+
entries.sort((left, right) => {
|
|
429
|
+
if (left.stage.current) return -1;
|
|
430
|
+
if (right.stage.current) return 1;
|
|
431
|
+
return (rank[left.stage.status] ?? 9) - (rank[right.stage.status] ?? 9)
|
|
432
|
+
|| left.workspace.id.localeCompare(right.workspace.id)
|
|
433
|
+
|| Number(left.stage.id) - Number(right.stage.id);
|
|
434
|
+
});
|
|
435
|
+
const labels = { in_progress: "In progress", planned: "Planned", completed: "Completed", cancelled: "Cancelled" };
|
|
436
|
+
showSelect("Stages", entries.map(({ workspace, stage }) => ({
|
|
437
|
+
title: `${marker(stage.status)} ${workspace.id}/${stage.id} ${stage.title} · ${workspace.title}`,
|
|
438
|
+
description: stage.description || stage.status,
|
|
439
|
+
category: stage.current ? "Current" : labels[stage.status] || stage.status,
|
|
440
|
+
value: { workspace, stage },
|
|
441
|
+
})), (option) => showStageActions(option.value.workspace, option.value.stage, { backTitle: "Back to stages", onBack: () => void browseStages() }));
|
|
442
|
+
};
|
|
443
|
+
const sessionOrdinal = (session) => String(session.ordinal).includes("/")
|
|
444
|
+
? String(session.ordinal)
|
|
445
|
+
: `${session.stage}/${String(session.ordinal).padStart(2, "0")}`;
|
|
446
|
+
const showSessionDetails = (workspace, session) => {
|
|
447
|
+
const stage = (workspace.stages || []).find((item) => item.id === session.stage);
|
|
448
|
+
const options = [
|
|
449
|
+
{ title: "Back to sessions", value: "back", category: "Navigation" },
|
|
450
|
+
...(stage ? [{ title: "Open stage actions", value: "stage", category: "Navigation" }] : []),
|
|
451
|
+
{ title: `Workspace ${workspace.id} ${workspace.title}`, value: "workspace", category: "Details", disabled: true },
|
|
452
|
+
{ title: `Stage ${session.stage}${stage ? ` ${stage.title}` : ""}`, value: "stage-detail", category: "Details", disabled: true },
|
|
453
|
+
{ title: `State ${session.state || "unknown"}`, value: "state", category: "Details", disabled: true },
|
|
454
|
+
{ title: `Summary ${session.summary || "No summary"}`, value: "summary", category: "Details", disabled: true },
|
|
455
|
+
{ title: `Session ${session.session_id}`, value: "session", category: "Details", disabled: true },
|
|
456
|
+
...(session.opencode_session_id ? [{ title: `OpenCode ${session.opencode_session_id}`, value: "opencode", category: "Details", disabled: true }] : []),
|
|
457
|
+
...(session.branch ? [{ title: `Branch ${session.branch}`, value: "branch", category: "Details", disabled: true }] : []),
|
|
458
|
+
...(session.updated_at ? [{ title: `Updated ${session.updated_at}`, value: "updated", category: "Details", disabled: true }] : []),
|
|
459
|
+
];
|
|
460
|
+
showSelect(`${workspace.id} ${sessionOrdinal(session)}`, options, (option) => {
|
|
461
|
+
if (option.value === "back") { void browseSessions(); return; }
|
|
462
|
+
if (option.value === "stage" && stage) showStageActions(workspace, stage, { backTitle: "Back to session", onBack: () => showSessionDetails(workspace, session) });
|
|
463
|
+
});
|
|
464
|
+
};
|
|
465
|
+
const browseSessions = async () => {
|
|
466
|
+
const loaded = await loadWorkspaces();
|
|
467
|
+
if (!loaded) return;
|
|
468
|
+
const rank = { active: 0, handed_off: 1, abandoned: 2, closed: 3 };
|
|
469
|
+
const entries = loaded.workspaces.flatMap((workspace) => (workspace.sessions || []).map((session) => ({ workspace, session })));
|
|
470
|
+
if (!entries.length) { toast("No sessions found"); return; }
|
|
471
|
+
entries.sort((left, right) => {
|
|
472
|
+
const leftCurrent = left.session.opencode_session_id === sessionId || left.session.session_id === sessionId;
|
|
473
|
+
const rightCurrent = right.session.opencode_session_id === sessionId || right.session.session_id === sessionId;
|
|
474
|
+
if (leftCurrent) return -1;
|
|
475
|
+
if (rightCurrent) return 1;
|
|
476
|
+
return (rank[left.session.state] ?? 9) - (rank[right.session.state] ?? 9)
|
|
477
|
+
|| String(right.session.updated_at || "").localeCompare(String(left.session.updated_at || ""));
|
|
478
|
+
});
|
|
479
|
+
const labels = { active: "Active", handed_off: "Handed off", abandoned: "Abandoned", closed: "Closed" };
|
|
480
|
+
showSelect("Sessions", entries.map(({ workspace, session }) => {
|
|
481
|
+
const current = session.opencode_session_id === sessionId || session.session_id === sessionId;
|
|
482
|
+
return {
|
|
483
|
+
title: `${workspace.id} ${sessionOrdinal(session)} ${session.summary || "Work session"} · ${workspace.title}`,
|
|
484
|
+
description: `${session.state || "unknown"} · ${session.session_id}`,
|
|
485
|
+
footer: session.updated_at || session.started_at,
|
|
486
|
+
category: current ? "Current" : labels[session.state] || session.state || "Unknown",
|
|
487
|
+
value: { workspace, session },
|
|
488
|
+
};
|
|
489
|
+
}), (option) => showSessionDetails(option.value.workspace, option.value.session));
|
|
490
|
+
};
|
|
491
|
+
const selectAction = (action) => {
|
|
492
|
+
if (action === "help") void insert(buildWorkContextActionCommand(action));
|
|
493
|
+
else if (action === "switch.stage") openStage(api);
|
|
494
|
+
else if (action === "switch.workspace") openWorkspaceSwitcher(api);
|
|
495
|
+
else if (action === "switch.session") openSessionSwitcher(api);
|
|
496
|
+
else if (action === "list") void browseWorkspaces();
|
|
497
|
+
else if (action === "browse.stages") void browseStages();
|
|
498
|
+
else if (action === "browse.sessions") void browseSessions();
|
|
499
|
+
else if (action === "create") requireValue("Create workspace", "Workspace title", action);
|
|
500
|
+
else if (action === "session.rename") requireValue("Rename work session", "Session summary", action);
|
|
501
|
+
else void selectWorkspace(action);
|
|
502
|
+
};
|
|
503
|
+
const open = () => {
|
|
504
|
+
stack.setSize?.("medium");
|
|
505
|
+
showSelect("Work Context", WORK_CONTEXT_ACTIONS, (option) => selectAction(option.value));
|
|
506
|
+
};
|
|
507
|
+
return { open, selectAction };
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
export const openWorkContextActionDialog = (api, runtime = {}, options = {}) => {
|
|
511
|
+
if (typeof api?.ui?.DialogSelect !== "function" || typeof api?.ui?.DialogPrompt !== "function" || typeof api?.ui?.dialog?.replace !== "function") return openCompatibilityDialog(api, runtime, options);
|
|
512
|
+
const flow = createWorkContextActionFlow(api, {
|
|
513
|
+
projectRoot: options.projectRoot || api.state?.path?.worktree,
|
|
514
|
+
sessionId: api.route?.current?.params?.sessionID || api.route?.current?.params?.sessionId,
|
|
515
|
+
read: options.read || readWorkContextSnapshot,
|
|
516
|
+
});
|
|
517
|
+
flow.open();
|
|
518
|
+
return true;
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
export const openCompatibilityDialog = (api, runtime = {}, options = {}) => {
|
|
522
|
+
const stack = api?.ui?.dialog;
|
|
523
|
+
if (!stack || typeof runtime.jsx !== "function" || typeof stack.replace !== "function") return false;
|
|
524
|
+
const projectRoot = options.projectRoot || api.state?.path?.worktree;
|
|
525
|
+
const sessionId = api.route?.current?.params?.sessionID || api.route?.current?.params?.sessionId;
|
|
526
|
+
let watcher;
|
|
527
|
+
let closed = false;
|
|
528
|
+
const close = () => {
|
|
529
|
+
if (closed) return;
|
|
530
|
+
closed = true;
|
|
531
|
+
controller.dispose();
|
|
532
|
+
watcher?.close();
|
|
533
|
+
closeDialog(api);
|
|
534
|
+
};
|
|
535
|
+
const controller = createWorkContextModalController({ projectRoot, sessionId, read: options.read || readWorkContextSnapshot, createSignal: runtime.createSignal, onClose: close, onCommand: (command) => insertPromptCommand(api, command) });
|
|
536
|
+
try { watcher = projectRoot && fs.watch(path.join(projectRoot, ".work-context"), { recursive: true }, () => controller.scheduleRefresh?.()); } catch {}
|
|
537
|
+
const maxListHeight = Math.max(8, Math.floor((api.renderer?.height || 60) / 2) - 6);
|
|
538
|
+
const render = () => renderWorkContextModal(controller, api.theme, runtime, { maxListHeight });
|
|
539
|
+
stack.replace(render, close);
|
|
540
|
+
return true;
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
export const registerWorkContextModal = (api, runtime = {}) => {
|
|
544
|
+
if (typeof api?.keymap?.registerLayer !== "function") return { supported: false, dispose: () => {} };
|
|
545
|
+
const command = { name: COMMAND_NAME, title: "Open Work Context", desc: "Open work-context navigation and actions", category: "Work Context", namespace: "palette", run: () => openWorkContextActionDialog(api, runtime) };
|
|
546
|
+
const stageCommand = { name: "work_context.stage", title: "Switch Work Context Stage", desc: "Open a stage session in the current workspace", category: "Work Context", namespace: "palette", run: () => openStageSwitcher(api) };
|
|
547
|
+
const workspaceCommand = { name: "work_context.workspace", title: "Switch Work Context Workspace", desc: "Open a workspace session", category: "Work Context", namespace: "palette", run: () => openWorkspaceSwitcher(api) };
|
|
548
|
+
const sessionCommand = { name: "work_context.session", title: "Switch Work Context Session", desc: "Open an active or historical work session", category: "Work Context", namespace: "palette", run: () => openSessionSwitcher(api) };
|
|
549
|
+
const actions = { name: ACTIONS_COMMAND_NAME, title: "Open Work Context Actions", desc: "Prepare a work-context command", category: "Work Context", namespace: "palette", run: () => openWorkContextActionDialog(api, runtime) };
|
|
550
|
+
const registration = api.keymap.registerLayer({
|
|
551
|
+
commands: [command, stageCommand, workspaceCommand, sessionCommand, actions],
|
|
552
|
+
bindings: [
|
|
553
|
+
{ key: SHORTCUT, cmd: COMMAND_NAME, desc: command.title },
|
|
554
|
+
{ key: STAGE_SHORTCUT, cmd: stageCommand.name, desc: stageCommand.title },
|
|
555
|
+
{ key: WORKSPACE_SHORTCUT, cmd: workspaceCommand.name, desc: workspaceCommand.title },
|
|
556
|
+
{ key: SESSION_SHORTCUT, cmd: sessionCommand.name, desc: sessionCommand.title },
|
|
557
|
+
],
|
|
558
|
+
});
|
|
559
|
+
return { supported: true, dispose: () => disposeRegistration(registration) };
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
export const tui = async (api, options = {}) => {
|
|
563
|
+
activeTuiInstances.get(api)?.();
|
|
564
|
+
const registration = registerWorkContextModal(api, options.runtime || options);
|
|
565
|
+
if (!registration.supported) return;
|
|
566
|
+
let disposed = false;
|
|
567
|
+
const cleanup = () => {
|
|
568
|
+
if (disposed) return;
|
|
569
|
+
disposed = true;
|
|
570
|
+
registration.dispose();
|
|
571
|
+
};
|
|
572
|
+
activeTuiInstances.set(api, cleanup);
|
|
573
|
+
api.lifecycle?.onDispose?.(cleanup);
|
|
574
|
+
return cleanup;
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
export default { id: "work-context-modal", tui };
|