@zero-gravity/zero-gravity 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/THIRD-PARTY-NOTICES.md +100 -0
- package/dist/adv-context-SLTP32JQ.js +36 -0
- package/dist/advanced-EPLZB6DG.js +164 -0
- package/dist/agents-JYRQHH7R.js +181 -0
- package/dist/chat-AEHVCKXX.js +47 -0
- package/dist/chunk-2OH5KHWO.js +112 -0
- package/dist/chunk-2ZYSAYMJ.js +108 -0
- package/dist/chunk-4BZ4IXTO.js +161 -0
- package/dist/chunk-4ITJY25Z.js +144 -0
- package/dist/chunk-4JBLZQAQ.js +65 -0
- package/dist/chunk-6QLYRU3N.js +28 -0
- package/dist/chunk-7TACFVY2.js +57 -0
- package/dist/chunk-AY36KJTH.js +3023 -0
- package/dist/chunk-BJABQO7S.js +378 -0
- package/dist/chunk-C2GFK4JP.js +387 -0
- package/dist/chunk-CDC2K2KP.js +47 -0
- package/dist/chunk-DKCCXMSE.js +27 -0
- package/dist/chunk-FC3UOHOO.js +194 -0
- package/dist/chunk-FIASMDLX.js +216 -0
- package/dist/chunk-GMVDCQVW.js +88 -0
- package/dist/chunk-JQYZX56A.js +231 -0
- package/dist/chunk-JU5PGDOD.js +138 -0
- package/dist/chunk-JUZP5SXD.js +309 -0
- package/dist/chunk-KGA7VWQG.js +70 -0
- package/dist/chunk-L7VNR5LM.js +407 -0
- package/dist/chunk-LOSXCWHU.js +145 -0
- package/dist/chunk-NVC433AX.js +24 -0
- package/dist/chunk-QH27MDJM.js +248 -0
- package/dist/chunk-QUC5URWQ.js +708 -0
- package/dist/chunk-R2CPYTSP.js +31 -0
- package/dist/chunk-R5KQACGR.js +2833 -0
- package/dist/chunk-R6DONQKN.js +153 -0
- package/dist/chunk-RIX4S3J7.js +4747 -0
- package/dist/chunk-SZKAZ2VN.js +35 -0
- package/dist/chunk-T6DDNE3W.js +1338 -0
- package/dist/chunk-U4URD6TU.js +4605 -0
- package/dist/chunk-U5UIUB6L.js +51 -0
- package/dist/chunk-UMC77TR3.js +79 -0
- package/dist/chunk-UQURXFAE.js +80 -0
- package/dist/chunk-VBG7T7XY.js +1703 -0
- package/dist/chunk-VCCMJIAG.js +326 -0
- package/dist/chunk-VS375NYD.js +91 -0
- package/dist/chunk-WANZOLI5.js +1073 -0
- package/dist/chunk-WUF3TVAE.js +51 -0
- package/dist/chunk-XCWLFZSM.js +149 -0
- package/dist/chunk-YJEUXO5I.js +76 -0
- package/dist/chunk-YR6RCC6Y.js +894 -0
- package/dist/chunk-ZQFAX275.js +35 -0
- package/dist/config-ELG4FQ3K.js +30 -0
- package/dist/config-IEWRCG7R.js +50 -0
- package/dist/doctor-ZZC2D3RW.js +357 -0
- package/dist/ext-context-RIMZMEPB.js +37 -0
- package/dist/index.js +20 -0
- package/dist/init-UB3E47KM.js +22 -0
- package/dist/integrations-HW4VTLN4.js +183 -0
- package/dist/main-RAWWT22W.js +115 -0
- package/dist/mcp-SWD4SO35.js +232 -0
- package/dist/mcp-context-PM5FY665.js +28 -0
- package/dist/memory-QQ54GRCN.js +128 -0
- package/dist/memory-session-R6DGT6CS.js +14 -0
- package/dist/memory-summary-4P26B2ZY.js +18 -0
- package/dist/plugins-VUGTEUNV.js +166 -0
- package/dist/providers-O3WOVP5Q.js +417 -0
- package/dist/report-DDIZ6BLF.js +28 -0
- package/dist/sec-context-VQHW6TOZ.js +15 -0
- package/dist/security-SAYW4LU4.js +95 -0
- package/dist/sessions-CL6CRNQL.js +160 -0
- package/dist/skills-7KDGTTKX.js +126 -0
- package/dist/version-6E6A23B3.js +13 -0
- package/package.json +70 -4
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
GitApproval,
|
|
4
|
+
GitError,
|
|
5
|
+
describeGitError,
|
|
6
|
+
operationPreview,
|
|
7
|
+
parseRange,
|
|
8
|
+
safeText
|
|
9
|
+
} from "./chunk-L7VNR5LM.js";
|
|
10
|
+
import {
|
|
11
|
+
AgentError,
|
|
12
|
+
boundText
|
|
13
|
+
} from "./chunk-CDC2K2KP.js";
|
|
14
|
+
import {
|
|
15
|
+
formatDateTime
|
|
16
|
+
} from "./chunk-YJEUXO5I.js";
|
|
17
|
+
import {
|
|
18
|
+
atomicWriteFile
|
|
19
|
+
} from "./chunk-U5UIUB6L.js";
|
|
20
|
+
import {
|
|
21
|
+
MEMORY_KINDS,
|
|
22
|
+
MemoryError,
|
|
23
|
+
parseKind
|
|
24
|
+
} from "./chunk-XCWLFZSM.js";
|
|
25
|
+
|
|
26
|
+
// src/chat/memory-commands.ts
|
|
27
|
+
import path from "path";
|
|
28
|
+
|
|
29
|
+
// src/agent/reporting/progress-reporter.ts
|
|
30
|
+
var STATUS_LABEL = {
|
|
31
|
+
analyzing: "analysing the request",
|
|
32
|
+
planning: "planning",
|
|
33
|
+
"needs-clarification": "waiting for your answers",
|
|
34
|
+
"awaiting-approval": "waiting for your approval of the plan",
|
|
35
|
+
running: "running",
|
|
36
|
+
"awaiting-step-approval": "waiting for your approval of a step",
|
|
37
|
+
paused: "paused",
|
|
38
|
+
completed: "completed",
|
|
39
|
+
incomplete: "finished, but some steps could not be done",
|
|
40
|
+
failed: "stopped: a step failed",
|
|
41
|
+
cancelled: "cancelled",
|
|
42
|
+
rejected: "plan rejected",
|
|
43
|
+
"plan-invalid": "no valid plan could be produced"
|
|
44
|
+
};
|
|
45
|
+
function buildProgress(view3) {
|
|
46
|
+
const rows = view3.steps.map((s) => {
|
|
47
|
+
const note = s.status === "failed" ? s.error?.message : s.status === "blocked" ? s.blockedReason : void 0;
|
|
48
|
+
return { id: s.id, title: s.title, state: s.status, current: s.id === view3.currentStepId && (s.status === "running" || s.status === "ready"), ...note ? { note } : {} };
|
|
49
|
+
});
|
|
50
|
+
const cur = view3.steps.find((s) => s.id === view3.currentStepId && s.status !== "completed") ?? view3.steps.find((s) => s.status === "running") ?? null;
|
|
51
|
+
return {
|
|
52
|
+
taskId: view3.id,
|
|
53
|
+
title: view3.planTitle ?? view3.analysis?.objective ?? view3.request,
|
|
54
|
+
status: view3.status,
|
|
55
|
+
statusLabel: view3.status === "failed" && view3.steps.length === 0 ? "stopped: the task could not be planned" : STATUS_LABEL[view3.status],
|
|
56
|
+
completed: view3.progress.completed,
|
|
57
|
+
total: view3.progress.total,
|
|
58
|
+
percent: view3.progress.percent,
|
|
59
|
+
rows,
|
|
60
|
+
review: (() => {
|
|
61
|
+
const r = view3.steps.find((s) => s.review && s.status === "ready");
|
|
62
|
+
return r?.review ? { stepId: r.id, preview: r.review.preview, needsConfirm: r.review.needsConfirm, ...r.review.kind ? { kind: r.review.kind } : {} } : null;
|
|
63
|
+
})(),
|
|
64
|
+
current: cur ? { id: cur.id, title: cur.title } : null,
|
|
65
|
+
failed: view3.steps.filter((s) => s.status === "failed").map((s) => ({ id: s.id, title: s.title, message: s.error?.message ?? "failed" })),
|
|
66
|
+
blocked: view3.steps.filter((s) => s.status === "blocked").map((s) => ({ id: s.id, title: s.title, reason: s.blockedReason ?? "blocked" })),
|
|
67
|
+
remaining: view3.steps.filter((s) => s.status === "pending" || s.status === "ready" || s.status === "running").length
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/agent/reporting/task-summary.ts
|
|
72
|
+
var EXCERPT = 600;
|
|
73
|
+
function buildTaskSummary(view3) {
|
|
74
|
+
const p = view3.progress;
|
|
75
|
+
const steps = view3.steps.map((s) => {
|
|
76
|
+
const note = s.status === "failed" ? s.error?.message : s.status === "blocked" ? s.blockedReason : s.status === "skipped" ? "skipped by you" : void 0;
|
|
77
|
+
return { id: s.id, title: s.title, status: s.status, ...s.result ? { excerpt: boundText(s.result.summary, EXCERPT).text, ...(s.capabilities.includes("modify-files") || s.capabilities.includes("run-command") || s.capabilities.includes("git-change") || s.capabilities.includes("mcp-call")) && s.result.kind === "tool" ? { excerptFromTool: true } : {} } : {}, ...note ? { note } : {} };
|
|
78
|
+
});
|
|
79
|
+
const notes = [];
|
|
80
|
+
const done = view3.steps.filter((s) => s.status === "completed");
|
|
81
|
+
const changed = done.filter((s) => s.capabilities.includes("modify-files")).length;
|
|
82
|
+
const ran = done.filter((s) => s.capabilities.includes("run-command")).length;
|
|
83
|
+
const gitChanged = done.filter((s) => s.capabilities.includes("git-change")).length;
|
|
84
|
+
const read = done.filter((s) => s.capabilities.includes("read-files") && !s.capabilities.includes("modify-files")).length;
|
|
85
|
+
if (changed === 0 && ran === 0 && gitChanged === 0) notes.push("No files were changed and no commands were run by this task.");
|
|
86
|
+
else notes.push(`${[changed ? `${changed} step(s) applied file changes` : "", ran ? `${ran} step(s) ran commands` : "", gitChanged ? `${gitChanged} step(s) changed the Git repository` : ""].filter(Boolean).join(" and ")}; the exact details are in their results above.`);
|
|
87
|
+
if (read > 0) notes.push(`${read} step(s) read project files.`);
|
|
88
|
+
if (p.blocked > 0) notes.push(`${p.blocked} step(s) could not run because they are blocked.`);
|
|
89
|
+
if (view3.failureNote) notes.push(view3.failureNote);
|
|
90
|
+
const next = [];
|
|
91
|
+
if (view3.status === "failed" && view3.steps.length === 0) next.push("/task revise <request or changes> to try planning again, or /task cancel.");
|
|
92
|
+
else if (view3.status === "failed") next.push("/task retry to run the failed step again, or /task revise <what to change> for a new plan.");
|
|
93
|
+
if (view3.status === "incomplete") next.push("/task revise <what to change> to plan around the steps that could not run.");
|
|
94
|
+
if (view3.status === "paused") next.push("/task resume to continue.");
|
|
95
|
+
const headline = {
|
|
96
|
+
completed: "Task completed",
|
|
97
|
+
incomplete: "Task finished with steps that could not be done",
|
|
98
|
+
failed: "Task stopped because a step failed",
|
|
99
|
+
cancelled: "Task cancelled",
|
|
100
|
+
rejected: "Plan rejected",
|
|
101
|
+
paused: "Task paused",
|
|
102
|
+
running: "Task running",
|
|
103
|
+
analyzing: "Task being analysed",
|
|
104
|
+
planning: "Task being planned",
|
|
105
|
+
"needs-clarification": "Task needs more information",
|
|
106
|
+
"awaiting-approval": "Plan waiting for approval",
|
|
107
|
+
"awaiting-step-approval": "Step waiting for approval",
|
|
108
|
+
"plan-invalid": "No valid plan"
|
|
109
|
+
};
|
|
110
|
+
return { taskId: view3.id, request: boundText(view3.request, 200).text, headline: view3.status === "failed" && view3.steps.length === 0 ? "Task could not be planned" : headline[view3.status], status: view3.status, completed: p.completed, total: p.total, steps, notes, nextActions: next };
|
|
111
|
+
}
|
|
112
|
+
function summaryToText(s) {
|
|
113
|
+
const lines = [`[Agent task ${s.taskId}] ${s.headline}: ${s.completed}/${s.total} steps completed.`];
|
|
114
|
+
for (const st of s.steps) lines.push(`- ${st.id} ${st.title}: ${st.status}${st.note ? ` (${st.note})` : ""}`);
|
|
115
|
+
lines.push(...s.notes);
|
|
116
|
+
return lines.join("\n");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/chat/agent-commands.ts
|
|
120
|
+
var TASK_USAGE = [
|
|
121
|
+
"/task new <request> understand the request and propose a plan (nothing runs yet)",
|
|
122
|
+
"/task approve [step-by-step] approve the plan and run it",
|
|
123
|
+
"/task revise <changes> ask for a different plan (also use it to answer questions)",
|
|
124
|
+
"/task reject reject the plan",
|
|
125
|
+
"/task approve-step [confirm] | skip-step apply or decline the prepared changes (confirm is needed to delete files)",
|
|
126
|
+
"/task status | pause | resume | cancel",
|
|
127
|
+
"/task retry [confirm] run a failed step again"
|
|
128
|
+
];
|
|
129
|
+
function parseTaskArgs(args) {
|
|
130
|
+
const trimmed = args.trim();
|
|
131
|
+
const m = /^(\S*)\s*([\s\S]*)$/.exec(trimmed);
|
|
132
|
+
const sub = (m?.[1] ?? "").toLowerCase();
|
|
133
|
+
const rest = (m?.[2] ?? "").trim();
|
|
134
|
+
const noArgs = (command) => rest === "" ? { ok: true, command } : { ok: false, error: `/task ${sub} does not take arguments (got "${rest.length > 40 ? `${rest.slice(0, 37)}...` : rest}").` };
|
|
135
|
+
switch (sub) {
|
|
136
|
+
case "":
|
|
137
|
+
case "status":
|
|
138
|
+
return noArgs({ name: "status" });
|
|
139
|
+
case "new":
|
|
140
|
+
return rest === "" ? { ok: false, error: "Describe what you want done: /task new <request>." } : { ok: true, command: { name: "new", request: rest } };
|
|
141
|
+
case "revise":
|
|
142
|
+
case "answer":
|
|
143
|
+
return rest === "" ? { ok: false, error: `Say what should change or answer the questions: /task ${sub} <text>.` } : { ok: true, command: { name: "revise", feedback: rest } };
|
|
144
|
+
case "approve": {
|
|
145
|
+
if (rest === "") return { ok: true, command: { name: "approve", stepByStep: false } };
|
|
146
|
+
if (["step-by-step", "stepwise", "steps", "step"].includes(rest.toLowerCase())) return { ok: true, command: { name: "approve", stepByStep: true } };
|
|
147
|
+
return { ok: false, error: `Unknown option "${rest}". Use /task approve, or /task approve step-by-step.` };
|
|
148
|
+
}
|
|
149
|
+
case "approve-step":
|
|
150
|
+
if (rest === "") return { ok: true, command: { name: "approve-step", confirm: false } };
|
|
151
|
+
return rest.toLowerCase() === "confirm" ? { ok: true, command: { name: "approve-step", confirm: true } } : { ok: false, error: `Unknown option "${rest}". Use /task approve-step, or /task approve-step confirm for steps that delete files.` };
|
|
152
|
+
case "skip-step":
|
|
153
|
+
return noArgs({ name: "skip-step" });
|
|
154
|
+
case "reject":
|
|
155
|
+
return noArgs({ name: "reject" });
|
|
156
|
+
case "pause":
|
|
157
|
+
return noArgs({ name: "pause" });
|
|
158
|
+
case "resume":
|
|
159
|
+
return noArgs({ name: "resume" });
|
|
160
|
+
case "cancel":
|
|
161
|
+
return noArgs({ name: "cancel" });
|
|
162
|
+
case "retry":
|
|
163
|
+
if (rest === "") return { ok: true, command: { name: "retry", confirmed: false } };
|
|
164
|
+
return rest.toLowerCase() === "confirm" ? { ok: true, command: { name: "retry", confirmed: true } } : { ok: false, error: `Unknown option "${rest}". Use /task retry, then /task retry confirm.` };
|
|
165
|
+
default:
|
|
166
|
+
return { ok: false, error: `Unknown task command "${sub}". Available: new, revise, approve, reject, approve-step, skip-step, status, pause, resume, cancel, retry.` };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
var notice = (level, text2) => ({ kind: "notice", level, text: text2 });
|
|
170
|
+
function planBlock(ctx, view3) {
|
|
171
|
+
return { kind: "task-plan", task: view3, unavailable: ctx.agent.unavailableCapabilities(), approvalMode: ctx.settings.approvalMode };
|
|
172
|
+
}
|
|
173
|
+
function outcomeBlocks(ctx, view3) {
|
|
174
|
+
const status = { kind: "task-status", progress: buildProgress(view3), task: view3 };
|
|
175
|
+
switch (view3.status) {
|
|
176
|
+
case "awaiting-approval":
|
|
177
|
+
case "plan-invalid":
|
|
178
|
+
case "needs-clarification":
|
|
179
|
+
return [planBlock(ctx, view3)];
|
|
180
|
+
case "awaiting-step-approval": {
|
|
181
|
+
const id = view3.currentStepId;
|
|
182
|
+
const step = view3.steps.find((s) => s.id === id);
|
|
183
|
+
return [status, notice("warn", `Step ${id ?? "?"}${step ? ` (${step.title})` : ""} needs your approval${step?.capabilities.includes("run-command") ? " because it would run a command" : step?.capabilities.includes("mcp-call") ? " because it would call a tool of an external service" : step?.capabilities.includes("git-change") ? " because it would change the Git repository" : " because it would change files"}. Use /task approve-step to allow it, or /task skip-step to skip it.`)];
|
|
184
|
+
}
|
|
185
|
+
case "paused":
|
|
186
|
+
return [status, notice("info", "Task paused. Finished steps are kept. Use /task resume to continue or /task cancel to stop.")];
|
|
187
|
+
case "completed":
|
|
188
|
+
case "incomplete":
|
|
189
|
+
case "failed":
|
|
190
|
+
case "cancelled":
|
|
191
|
+
return [status, { kind: "task-summary", summary: buildTaskSummary(view3) }];
|
|
192
|
+
case "rejected":
|
|
193
|
+
return [notice("info", `Plan rejected. Nothing was run. Start again with /task new <request>, or use /task revise on a task that is still open.`)];
|
|
194
|
+
default:
|
|
195
|
+
return [status];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function describe(err) {
|
|
199
|
+
if (err instanceof AgentError) return notice(err.code === "disabled" || err.code === "no-task" || err.code === "empty-request" ? "warn" : "error", err.hint ? `${err.message} ${err.hint}` : err.message);
|
|
200
|
+
return notice("error", err instanceof Error ? err.message : "unexpected error");
|
|
201
|
+
}
|
|
202
|
+
async function runTask(args, ctx) {
|
|
203
|
+
const parsed = parseTaskArgs(args);
|
|
204
|
+
if (!parsed.ok) return { blocks: [notice("warn", `${parsed.error}
|
|
205
|
+
${TASK_USAGE.join("\n")}`)] };
|
|
206
|
+
const c = parsed.command;
|
|
207
|
+
const agent = ctx.agent;
|
|
208
|
+
try {
|
|
209
|
+
switch (c.name) {
|
|
210
|
+
case "new":
|
|
211
|
+
return { blocks: outcomeBlocks(ctx, await agent.startTask(c.request, ctx.signal)) };
|
|
212
|
+
case "revise":
|
|
213
|
+
return { blocks: outcomeBlocks(ctx, await agent.revise(c.feedback, ctx.signal)) };
|
|
214
|
+
case "approve":
|
|
215
|
+
return { blocks: outcomeBlocks(ctx, await agent.approve({ stepByStep: c.stepByStep }, ctx.signal)) };
|
|
216
|
+
case "approve-step":
|
|
217
|
+
return { blocks: outcomeBlocks(ctx, await agent.approveStep({ confirm: c.confirm }, ctx.signal)) };
|
|
218
|
+
case "skip-step":
|
|
219
|
+
return { blocks: outcomeBlocks(ctx, agent.skipStep()) };
|
|
220
|
+
case "reject":
|
|
221
|
+
return { blocks: outcomeBlocks(ctx, agent.reject()) };
|
|
222
|
+
case "resume":
|
|
223
|
+
return { blocks: outcomeBlocks(ctx, await agent.resume(ctx.signal)) };
|
|
224
|
+
case "pause":
|
|
225
|
+
return { blocks: outcomeBlocks(ctx, agent.pause()) };
|
|
226
|
+
case "cancel": {
|
|
227
|
+
const view3 = agent.cancel();
|
|
228
|
+
return { blocks: [notice("info", `Task ${view3.id} cancelled. Steps that had finished are kept in the summary.`), ...outcomeBlocks(ctx, view3).filter((b) => b.kind === "task-summary")] };
|
|
229
|
+
}
|
|
230
|
+
case "retry": {
|
|
231
|
+
if (!c.confirmed) {
|
|
232
|
+
const preview = agent.retryPreview();
|
|
233
|
+
if (!preview) return { blocks: [notice("warn", "There is no failed task to retry.")] };
|
|
234
|
+
if (preview.stepIds.length === 0) return { blocks: [notice("warn", "Every failed step has already been tried the maximum number of times. Use /task revise <what to change> for a new plan.")] };
|
|
235
|
+
const risky = preview.nonIdempotent.length > 0 ? ` Steps ${preview.nonIdempotent.join(", ")} change files or run commands, so they will ask for your approval again and may not be safe to repeat.` : "";
|
|
236
|
+
return { blocks: [notice("warn", `This will run step${preview.stepIds.length === 1 ? "" : "s"} ${preview.stepIds.join(", ")} again. Completed steps are not repeated.${risky} Use /task retry confirm to go ahead.`)] };
|
|
237
|
+
}
|
|
238
|
+
return { blocks: outcomeBlocks(ctx, await agent.retry(true, ctx.signal)) };
|
|
239
|
+
}
|
|
240
|
+
case "status": {
|
|
241
|
+
const view3 = agent.view();
|
|
242
|
+
if (!view3) return { blocks: [notice("info", "There are no tasks in this session. Start one with /task new <request>.")] };
|
|
243
|
+
return { blocks: [{ kind: "task-status", progress: buildProgress(view3), task: view3 }] };
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} catch (err) {
|
|
247
|
+
if (ctx.signal.aborted && !(err instanceof AgentError)) return { blocks: [notice("warn", "Cancelled.")] };
|
|
248
|
+
return { blocks: [describe(err)] };
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
var agentCommands = [
|
|
252
|
+
{
|
|
253
|
+
name: "plan",
|
|
254
|
+
summary: "Show the current task plan",
|
|
255
|
+
run: (_args, ctx) => {
|
|
256
|
+
const view3 = ctx.agent.view();
|
|
257
|
+
if (!view3) return { blocks: [notice("info", "There is no task yet. Start one with /task new <request>.")] };
|
|
258
|
+
return { blocks: [planBlock(ctx, view3)] };
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
name: "tasks",
|
|
263
|
+
summary: "List the tasks of this session",
|
|
264
|
+
run: (_args, ctx) => {
|
|
265
|
+
const tasks = ctx.agent.list();
|
|
266
|
+
if (tasks.length === 0) return { blocks: [notice("info", "There are no tasks in this session. Start one with /task new <request>.")] };
|
|
267
|
+
return { blocks: [{ kind: "tasks", tasks, activeId: ctx.agent.activeTask()?.id ?? null }] };
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
name: "task",
|
|
272
|
+
summary: "Plan and run a task: /task new|approve|status|pause|resume|cancel|retry ...",
|
|
273
|
+
run: runTask
|
|
274
|
+
}
|
|
275
|
+
];
|
|
276
|
+
|
|
277
|
+
// src/chat/git-commands.ts
|
|
278
|
+
var notice2 = (level, text2) => ({ kind: "notice", level, text: text2 });
|
|
279
|
+
var view = (v) => ({ kind: "git-view", view: v });
|
|
280
|
+
var text = (title, lines, footer) => view({ type: "text", title, lines, ...footer ? { footer } : {} });
|
|
281
|
+
var HELP = [
|
|
282
|
+
"Looking (read-only, always safe):",
|
|
283
|
+
" /git status Branch, modified, staged and untracked files",
|
|
284
|
+
" /git diff [file...] Working tree changes (not yet staged)",
|
|
285
|
+
" /git diff staged [file...] Changes that would go into the next commit",
|
|
286
|
+
" /git diff <a>..<b> Compare two branches or commits",
|
|
287
|
+
" /git diff commit <rev> The changes of one commit",
|
|
288
|
+
" /git branches Local and known remote-tracking branches",
|
|
289
|
+
" /git log [n | file] Recent commits",
|
|
290
|
+
" /git show <commit> One commit with its changes",
|
|
291
|
+
" /git remotes Configured remotes (credentials hidden)",
|
|
292
|
+
" /git conflicts [explain] Merge conflicts (explain asks the AI)",
|
|
293
|
+
" /git review Who changed what; what looks unexpected",
|
|
294
|
+
" /git restore <file...> Preview what discarding would lose (changes nothing)",
|
|
295
|
+
"",
|
|
296
|
+
"Changing (always shown first; nothing happens until /git approve):",
|
|
297
|
+
" /git stage <file...> Stage the named files (never everything)",
|
|
298
|
+
" /git unstage <file...> Take files out of the next commit",
|
|
299
|
+
" /git commit [message] Commit what is staged (AI suggests a message)",
|
|
300
|
+
" /git branch <name> Create a branch and switch to it",
|
|
301
|
+
" /git switch <name> Switch branch (refused with uncommitted changes)",
|
|
302
|
+
" /git approve | reject Do, or drop, the change that is waiting",
|
|
303
|
+
"",
|
|
304
|
+
"/git conflicts resolve <file> proposes a resolution as a task you review.",
|
|
305
|
+
"zg never pushes, pulls, force-pushes, resets, cleans or amends."
|
|
306
|
+
];
|
|
307
|
+
function splitArgs(input) {
|
|
308
|
+
const out = [];
|
|
309
|
+
let cur = "";
|
|
310
|
+
let quote = null;
|
|
311
|
+
let started = false;
|
|
312
|
+
for (const ch of input) {
|
|
313
|
+
if (quote) {
|
|
314
|
+
if (ch === quote) quote = null;
|
|
315
|
+
else cur += ch;
|
|
316
|
+
} else if (ch === '"' || ch === "'") {
|
|
317
|
+
quote = ch;
|
|
318
|
+
started = true;
|
|
319
|
+
} else if (/\s/.test(ch)) {
|
|
320
|
+
if (cur !== "" || started) out.push(cur);
|
|
321
|
+
cur = "";
|
|
322
|
+
started = false;
|
|
323
|
+
} else cur += ch;
|
|
324
|
+
}
|
|
325
|
+
if (cur !== "" || started) out.push(cur);
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
function need(ctx) {
|
|
329
|
+
if (ctx.git) return ctx.git;
|
|
330
|
+
return notice2("warn", ctx.workspace ? `Git is off: ${ctx.workspace.problem ?? "no project folder"}` : "Git is not set up in this session (no project folder).");
|
|
331
|
+
}
|
|
332
|
+
function fail(err) {
|
|
333
|
+
if (err instanceof GitError) {
|
|
334
|
+
const soft = ["not-a-repo", "denied", "nothing-to-do", "dirty-tree", "stale", "disabled", "invalid-argument", "exists", "not-found", "conflicts", "protected-path", "unborn", "bare-repo", "busy"].includes(err.code);
|
|
335
|
+
return { blocks: [notice2(soft ? "warn" : "error", describeGitError(err))] };
|
|
336
|
+
}
|
|
337
|
+
return { blocks: [notice2("error", "The Git command could not be completed.")] };
|
|
338
|
+
}
|
|
339
|
+
function opBlocks(g, op) {
|
|
340
|
+
return [{ kind: "git-operation", preview: operationPreview(op), manual: true }];
|
|
341
|
+
void g;
|
|
342
|
+
}
|
|
343
|
+
function diffTarget(args) {
|
|
344
|
+
const [first = "", ...rest] = args;
|
|
345
|
+
const word = first.toLowerCase();
|
|
346
|
+
if (word === "staged" || word === "--staged" || word === "cached" || word === "--cached") return { kind: "staged", paths: rest };
|
|
347
|
+
if (word === "commit") {
|
|
348
|
+
if (rest.length === 0) throw new GitError("invalid-argument", "Which commit? Use /git diff commit <rev>, for example /git diff commit HEAD~1");
|
|
349
|
+
return { kind: "commit", rev: rest[0], paths: rest.slice(1) };
|
|
350
|
+
}
|
|
351
|
+
if (first !== "" && first.includes("..") && !first.includes("/..") && !first.startsWith("..")) {
|
|
352
|
+
if (!parseRange(first)) throw new GitError("invalid-argument", `"${safeText(first)}" is not a usable comparison.`, "Use <branch>..<branch> or <branch>...<branch>.");
|
|
353
|
+
return { kind: "compare", range: first, paths: rest };
|
|
354
|
+
}
|
|
355
|
+
return { kind: "working", paths: args };
|
|
356
|
+
}
|
|
357
|
+
async function approve(g, ctx) {
|
|
358
|
+
const op = g.pending;
|
|
359
|
+
if (!op) return { blocks: [notice2("warn", "No Git change is waiting for approval. Prepare one with /git stage, /git unstage, /git commit, /git branch or /git switch.")] };
|
|
360
|
+
g.pending = null;
|
|
361
|
+
try {
|
|
362
|
+
const res = await g.manager.apply(op, GitApproval.grant(op.id), ctx.signal);
|
|
363
|
+
return { blocks: [{ kind: "git-result", title: op.kind === "commit" ? "COMMIT CREATED" : "GIT CHANGE DONE", lines: res.lines }] };
|
|
364
|
+
} catch (e) {
|
|
365
|
+
return fail(e);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async function conflictFiles(g, ctx, only) {
|
|
369
|
+
const { report } = await g.manager.conflicts();
|
|
370
|
+
return report.files.filter((f) => only ? f.path === only : true).filter((f) => f.regions.length > 0).map((f) => ({
|
|
371
|
+
path: f.path,
|
|
372
|
+
text: f.regions.map((r) => `Lines ${r.startLine}-${r.endLine}
|
|
373
|
+
<<< ours (${r.oursLabel})
|
|
374
|
+
${r.ours.join("\n")}
|
|
375
|
+
${r.base ? `||| base
|
|
376
|
+
${r.base.join("\n")}
|
|
377
|
+
` : ""}===
|
|
378
|
+
${r.theirs.join("\n")}
|
|
379
|
+
>>> theirs (${r.theirsLabel})`).join("\n\n")
|
|
380
|
+
})).filter(() => ctx !== null);
|
|
381
|
+
}
|
|
382
|
+
var gitCommands = [
|
|
383
|
+
{
|
|
384
|
+
name: "git",
|
|
385
|
+
summary: "Git status, diffs, review, staging and commits: /git help",
|
|
386
|
+
run: async (args, ctx) => {
|
|
387
|
+
const tokens = splitArgs(args);
|
|
388
|
+
const name = (tokens[0] ?? "help").toLowerCase();
|
|
389
|
+
const rest = tokens.slice(1);
|
|
390
|
+
if (name === "help" || name === "") return { blocks: [text("GIT COMMANDS", HELP)] };
|
|
391
|
+
const g = need(ctx);
|
|
392
|
+
if (!("manager" in g)) return { blocks: [g] };
|
|
393
|
+
const m = g.manager;
|
|
394
|
+
try {
|
|
395
|
+
switch (name) {
|
|
396
|
+
case "status":
|
|
397
|
+
return { blocks: [view({ type: "status", status: await m.status() })] };
|
|
398
|
+
case "diff":
|
|
399
|
+
return { blocks: [view({ type: "diff", diff: await m.diff(diffTarget(rest)) })] };
|
|
400
|
+
case "branches":
|
|
401
|
+
case "branch-list":
|
|
402
|
+
return { blocks: [view({ type: "branches", branches: await m.branches(true) })] };
|
|
403
|
+
case "log": {
|
|
404
|
+
const n = rest[0] !== void 0 && /^\d+$/.test(rest[0]) ? Number(rest[0]) : void 0;
|
|
405
|
+
const path2 = n === void 0 ? rest[0] : rest[1];
|
|
406
|
+
const r = await m.log({ ...n !== void 0 ? { limit: n } : {}, ...path2 ? { path: path2 } : {} });
|
|
407
|
+
return { blocks: [view({ type: "log", commits: r.commits, limit: r.limit })] };
|
|
408
|
+
}
|
|
409
|
+
case "show": {
|
|
410
|
+
if (rest.length === 0) return { blocks: [notice2("warn", "Which commit? Use /git show <commit>, for example /git show HEAD~1")] };
|
|
411
|
+
const c = await m.commit(rest[0]);
|
|
412
|
+
const d = await m.diff({ kind: "commit", rev: rest[0] });
|
|
413
|
+
return { blocks: [view({ type: "commit", commit: c }), view({ type: "diff", diff: d })] };
|
|
414
|
+
}
|
|
415
|
+
case "remotes":
|
|
416
|
+
case "remote":
|
|
417
|
+
return { blocks: [view({ type: "remotes", remotes: await m.remotes() })] };
|
|
418
|
+
case "conflicts": {
|
|
419
|
+
const sub = (rest[0] ?? "").toLowerCase();
|
|
420
|
+
if (sub === "" || sub === "show") {
|
|
421
|
+
const c = await m.conflicts();
|
|
422
|
+
return { blocks: [view({ type: "conflicts", status: c.status, report: c.report })] };
|
|
423
|
+
}
|
|
424
|
+
if (sub === "explain") {
|
|
425
|
+
if (!g.explainer || ctx.chat.backend === null) return { blocks: [notice2("info", "No AI provider is connected, so the conflict cannot be explained. /git conflicts shows the conflicting lines.")] };
|
|
426
|
+
const files = await conflictFiles(g, ctx);
|
|
427
|
+
if (files.length === 0) return { blocks: [notice2("info", "There are no conflict regions to explain. /git conflicts shows the current state.")] };
|
|
428
|
+
const res = await g.explainer({ files: files.slice(0, 5) }, ctx.signal);
|
|
429
|
+
if (res.status !== "ok") return { blocks: [notice2("warn", res.status === "failed" ? res.message : "Cancelled.")] };
|
|
430
|
+
return { blocks: [text("GIT CONFLICT EXPLAINED", res.explanation.split("\n"), "Written by the AI from the conflicting lines. Nothing was changed; check it against the code.")] };
|
|
431
|
+
}
|
|
432
|
+
if (sub === "resolve") {
|
|
433
|
+
const file = rest[1];
|
|
434
|
+
if (!file) return { blocks: [notice2("warn", "Which file? Use /git conflicts resolve <file>. See /git conflicts for the list.")] };
|
|
435
|
+
const c = await m.conflicts();
|
|
436
|
+
const hit = c.status.conflicted.find((e) => e.path === file.replace(/\\/g, "/").replace(/^\.\//, ""));
|
|
437
|
+
if (!hit) return { blocks: [notice2("warn", `${safeText(file)} is not one of the conflicted files. See /git conflicts.`)] };
|
|
438
|
+
if (ctx.chat.backend === null) return { blocks: [notice2("info", "No AI provider is connected, so a resolution cannot be proposed. Resolve the markers by hand and stage the file with /git stage.")] };
|
|
439
|
+
const request = `Resolve the merge conflict in ${hit.path}. Read the file, then edit ONLY that file so every conflict marker (<<<<<<<, =======, >>>>>>>) is removed and the result keeps the intent of both sides where they can coexist; where they cannot, keep the side that matches the rest of the code and say which. Do not stage or commit anything.`;
|
|
440
|
+
const task = await ctx.agent.startTask(request, ctx.signal);
|
|
441
|
+
return { blocks: [notice2("info", "A resolution is being planned as an ordinary task: you review the plan, then the exact diff, and nothing changes until you approve each."), ...outcomeBlocks(ctx, task)] };
|
|
442
|
+
}
|
|
443
|
+
return { blocks: [notice2("warn", "Use /git conflicts, /git conflicts explain, or /git conflicts resolve <file>.")] };
|
|
444
|
+
}
|
|
445
|
+
case "review":
|
|
446
|
+
return { blocks: [view({ type: "review", review: await m.review() })] };
|
|
447
|
+
case "restore":
|
|
448
|
+
case "discard":
|
|
449
|
+
return { blocks: [view({ type: "restore", preview: await m.restorePreview(rest) })] };
|
|
450
|
+
case "stage":
|
|
451
|
+
case "add":
|
|
452
|
+
return { blocks: opBlocks(g, g.hold(await m.prepareStage(rest))) };
|
|
453
|
+
case "unstage":
|
|
454
|
+
return { blocks: opBlocks(g, g.hold(await m.prepareUnstage(rest))) };
|
|
455
|
+
case "commit": {
|
|
456
|
+
let message = args.trim().slice(tokens[0]?.length ?? 0).trim();
|
|
457
|
+
if (message.length >= 2 && (message[0] === '"' || message[0] === "'") && message.endsWith(message[0])) message = message.slice(1, -1).trim();
|
|
458
|
+
if (message !== "") return { blocks: opBlocks(g, g.hold(await m.prepareCommit(message))) };
|
|
459
|
+
if (ctx.chat.backend === null) return { blocks: [notice2("info", "Give a message: /git commit <message>. (No AI provider is connected to suggest one.)")] };
|
|
460
|
+
const s = await m.suggestMessage(ctx.signal);
|
|
461
|
+
const op = g.hold(await m.prepareCommit(s.message.full));
|
|
462
|
+
return { blocks: [notice2("info", "This message was suggested by the AI from your staged diff. To use your own wording: /git commit <message>."), ...opBlocks(g, op)] };
|
|
463
|
+
}
|
|
464
|
+
case "branch":
|
|
465
|
+
case "checkout-new": {
|
|
466
|
+
if (rest.length !== 1) return { blocks: [notice2("warn", "Which name? Use /git branch <name>, for example /git branch feature/login. (/git branches lists them.)")] };
|
|
467
|
+
return { blocks: opBlocks(g, g.hold(await m.prepareBranchCreate(rest[0]))) };
|
|
468
|
+
}
|
|
469
|
+
case "switch": {
|
|
470
|
+
if (rest.length !== 1) return { blocks: [notice2("warn", "Which branch? Use /git switch <name>. (/git branches lists them.)")] };
|
|
471
|
+
return { blocks: opBlocks(g, g.hold(await m.prepareSwitch(rest[0]))) };
|
|
472
|
+
}
|
|
473
|
+
case "approve":
|
|
474
|
+
if (rest.length > 0 && rest[0]?.toLowerCase() !== "confirm") return { blocks: [notice2("warn", "Use /git approve (no extra words are needed).")] };
|
|
475
|
+
return approve(g, ctx);
|
|
476
|
+
case "reject":
|
|
477
|
+
case "cancel":
|
|
478
|
+
return { blocks: [g.discard() ? notice2("info", "Rejected. Nothing was changed.") : notice2("info", "No Git change was waiting.")] };
|
|
479
|
+
default:
|
|
480
|
+
return { blocks: [notice2("warn", `Unknown /git option "${safeText(name)}". /git help lists them.`)] };
|
|
481
|
+
}
|
|
482
|
+
} catch (e) {
|
|
483
|
+
return fail(e);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
];
|
|
488
|
+
|
|
489
|
+
// src/chat/memory-commands.ts
|
|
490
|
+
var notice3 = (level, text2) => ({ kind: "notice", level, text: text2 });
|
|
491
|
+
var view2 = (title, lines, footer) => ({ kind: "fs-view", title, lines, diff: false, ...footer ? { footer } : {} });
|
|
492
|
+
var shortDate = (iso) => formatDateTime(new Date(iso)).slice(0, 16);
|
|
493
|
+
function sessionRow(s, current) {
|
|
494
|
+
return `${s.id === current ? "*" : " "} ${s.id.slice(0, 8)} ${shortDate(s.lastActivityAt)} ${String(s.messageCount).padStart(4)} msg ${s.title}`;
|
|
495
|
+
}
|
|
496
|
+
function entryRow(e) {
|
|
497
|
+
return `${e.id} [${e.kind}] ${e.text.replace(/\s+/g, " ").slice(0, 90)}${e.text.length > 90 ? "\u2026" : ""}`;
|
|
498
|
+
}
|
|
499
|
+
function need2(ctx) {
|
|
500
|
+
return ctx.memory ?? notice3("warn", "Memory and saved sessions are not set up in this session (no zg data folder or project folder).");
|
|
501
|
+
}
|
|
502
|
+
function fail2(err) {
|
|
503
|
+
if (err instanceof MemoryError) return { blocks: [notice3(err.code === "disabled" || err.code === "wrong-project" ? "warn" : "error", err.hint ? `${err.message} ${err.hint}` : err.message)] };
|
|
504
|
+
return { blocks: [notice3("error", "That did not work and nothing was changed.")] };
|
|
505
|
+
}
|
|
506
|
+
var MEMORY_HELP = [
|
|
507
|
+
"/memory Status: is memory on, what is saved, which session is this",
|
|
508
|
+
"/memory list [--global] Saved notes for this project (or your global preferences)",
|
|
509
|
+
"/memory search <text> Search saved notes (case-insensitive)",
|
|
510
|
+
"/memory show <id> Show one note in full",
|
|
511
|
+
"/memory add [--global] [--kind <kind>] <text> Save a note (secrets are refused)",
|
|
512
|
+
"/memory delete <id> confirm Remove one note for good",
|
|
513
|
+
"/memory enable | disable Turn persistent memory on or off (saved in your settings)",
|
|
514
|
+
`Kinds: ${MEMORY_KINDS.join(", ")}. Edit or clear notes with: zg memory edit | zg memory clear`
|
|
515
|
+
];
|
|
516
|
+
async function memoryStatus(c) {
|
|
517
|
+
const st = await c.memory.status();
|
|
518
|
+
const s = c.memorySettings;
|
|
519
|
+
const lines = [`Persistent memory: ${s.enabled ? "ON" : "off"}${s.enabled ? "" : " (turn on with /memory enable; nothing is remembered between sessions while off)"}`, `Project memory: ${s.projectMemory ? "allowed" : "off"} Global preferences: ${s.globalPreferences ? "allowed" : "off"}`];
|
|
520
|
+
for (const x of st) lines.push(x.target === "project" ? `This project: ${x.available ? `${x.count} note(s)` : "no project folder"}` : `Global preferences: ${x.available ? `${x.count} note(s)` : "unavailable"}`);
|
|
521
|
+
for (const x of st) for (const n of x.notes) lines.push(`Note: ${n}`);
|
|
522
|
+
lines.push(`Session: ${c.sessionId.slice(0, 8)} ${c.saving ? `saved locally (kept ${c.sessionSettings.retentionDays === 0 ? "until you delete it" : `${c.sessionSettings.retentionDays} days`})` : "not being saved"}`);
|
|
523
|
+
return view2("MEMORY", lines, "Secrets and credentials are never stored. Details: docs/MEMORY.md");
|
|
524
|
+
}
|
|
525
|
+
async function memoryCommand(args, ctx) {
|
|
526
|
+
const c = need2(ctx);
|
|
527
|
+
if (!("memory" in c)) return { blocks: [c] };
|
|
528
|
+
const tokens = splitArgs(args);
|
|
529
|
+
const name = (tokens[0] ?? "status").toLowerCase();
|
|
530
|
+
const rest = tokens.slice(1);
|
|
531
|
+
const global = rest.includes("--global");
|
|
532
|
+
const words = rest.filter((t) => t !== "--global");
|
|
533
|
+
try {
|
|
534
|
+
switch (name) {
|
|
535
|
+
case "status":
|
|
536
|
+
return { blocks: [await memoryStatus(c)] };
|
|
537
|
+
case "help":
|
|
538
|
+
return { blocks: [view2("MEMORY COMMANDS", MEMORY_HELP)] };
|
|
539
|
+
case "list": {
|
|
540
|
+
const entries = await c.memory.list(global ? "global" : "project");
|
|
541
|
+
return { blocks: [view2(global ? "GLOBAL PREFERENCES" : "PROJECT MEMORY", entries.length === 0 ? ["Nothing is saved here yet."] : entries.map(entryRow), "/memory show <id> shows a note in full")] };
|
|
542
|
+
}
|
|
543
|
+
case "search": {
|
|
544
|
+
const q = words.join(" ");
|
|
545
|
+
if (q === "") return { blocks: [notice3("warn", "What should I search for? /memory search <text>")] };
|
|
546
|
+
const hits = await c.memory.search(q);
|
|
547
|
+
return { blocks: [view2("MEMORY SEARCH", hits.length === 0 ? [`No saved note contains "${q}".`] : hits.map((h) => `${h.entry.id} ${h.scope === "global" ? "global " : "project"} [${h.entry.kind}] ${h.snippet}`))] };
|
|
548
|
+
}
|
|
549
|
+
case "show": {
|
|
550
|
+
if (!words[0]) return { blocks: [notice3("warn", "Which note? /memory show <id>")] };
|
|
551
|
+
const { entry, target } = await c.memory.find(words[0]);
|
|
552
|
+
return { blocks: [view2("MEMORY ENTRY", [`Id: ${entry.id}`, `Scope: ${target}`, `Kind: ${entry.kind}`, `Source: ${entry.source}${entry.sessionId ? ` (session ${entry.sessionId.slice(0, 8)})` : ""}`, `Created: ${shortDate(entry.createdAt)} Updated: ${shortDate(entry.updatedAt)}`, ...entry.tags.length ? [`Tags: ${entry.tags.join(", ")}`] : [], "", ...entry.text.split("\n")])] };
|
|
553
|
+
}
|
|
554
|
+
case "add": {
|
|
555
|
+
let kind;
|
|
556
|
+
const text2 = [];
|
|
557
|
+
for (let i = 0; i < words.length; i++) {
|
|
558
|
+
if (words[i] === "--kind") kind = words[++i];
|
|
559
|
+
else text2.push(words[i]);
|
|
560
|
+
}
|
|
561
|
+
if (text2.length === 0) return { blocks: [notice3("warn", "What should be saved? /memory add [--global] [--kind <kind>] <text>")] };
|
|
562
|
+
const e = await c.memory.add(global ? "global" : "project", { text: text2.join(" "), kind: parseKind(kind) });
|
|
563
|
+
c.invalidate();
|
|
564
|
+
return { blocks: [notice3("info", `Saved ${e.id} (${e.kind}, ${global ? "global" : "this project"}). It will be part of the context from your next message.`)] };
|
|
565
|
+
}
|
|
566
|
+
case "delete": {
|
|
567
|
+
if (!words[0]) return { blocks: [notice3("warn", "Which note? /memory delete <id> confirm")] };
|
|
568
|
+
const { entry, target } = await c.memory.find(words[0]);
|
|
569
|
+
if (words[1]?.toLowerCase() !== "confirm") return { blocks: [notice3("warn", `This removes note ${entry.id} (${entry.text.slice(0, 60)}) for good. To go ahead: /memory delete ${entry.id} confirm`)] };
|
|
570
|
+
await c.memory.remove(target, entry.id);
|
|
571
|
+
c.invalidate();
|
|
572
|
+
return { blocks: [notice3("info", `Removed ${entry.id}.`)] };
|
|
573
|
+
}
|
|
574
|
+
case "enable":
|
|
575
|
+
case "disable": {
|
|
576
|
+
const on = name === "enable";
|
|
577
|
+
const r = await c.setEnabled(on);
|
|
578
|
+
return { blocks: [notice3("info", on ? `Persistent memory is ON${r.persisted ? " and saved in your settings" : " for this run only"}. Notes you add are kept on this computer and sent to your AI provider as context. Secrets are never stored.` : `Persistent memory is off${r.persisted ? " and saved in your settings" : " for this run only"}. Existing notes stay on disk (zg memory clear removes them) but are no longer used.`)] };
|
|
579
|
+
}
|
|
580
|
+
default:
|
|
581
|
+
return { blocks: [notice3("warn", `Unknown /memory option "${name}". /memory help lists them.`)] };
|
|
582
|
+
}
|
|
583
|
+
} catch (err) {
|
|
584
|
+
return fail2(err);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
var SESSION_HELP = [
|
|
588
|
+
"/session This session: id, saving, memory",
|
|
589
|
+
"/sessions | /session list [--all] Saved sessions (this project, or all)",
|
|
590
|
+
"/session show <id> Details and summary of a saved session",
|
|
591
|
+
"/session new Finish this session and start a fresh one",
|
|
592
|
+
"/session resume <id> Load an earlier session of this project",
|
|
593
|
+
"/session rename <id> <title> Change a title",
|
|
594
|
+
"/session summarize Write a summary of this session now",
|
|
595
|
+
"/session export [id] [json|md] Save a redacted copy in zg's exports folder",
|
|
596
|
+
"/session delete <id> confirm Delete a saved session for good",
|
|
597
|
+
'Search: zg sessions search "text" Cleanup: zg sessions cleanup'
|
|
598
|
+
];
|
|
599
|
+
function busy(ctx) {
|
|
600
|
+
return ctx.agent.activeTask() ? "A task is still open. Finish it (/task status) or cancel it (/task cancel) first, so it is not interrupted." : null;
|
|
601
|
+
}
|
|
602
|
+
async function sessionCommand(args, ctx) {
|
|
603
|
+
const c = need2(ctx);
|
|
604
|
+
if (!("memory" in c)) return { blocks: [c] };
|
|
605
|
+
const tokens = splitArgs(args);
|
|
606
|
+
const name = (tokens[0] ?? "").toLowerCase();
|
|
607
|
+
const rest = tokens.slice(1);
|
|
608
|
+
try {
|
|
609
|
+
switch (name) {
|
|
610
|
+
case "":
|
|
611
|
+
case "status":
|
|
612
|
+
return { blocks: [view2("SESSION", [`Session: ${c.sessionId}`, `Messages this run: ${ctx.session.counts().total}`, `Saving: ${c.saving ? `on (${c.sessionSettings.retentionDays === 0 ? "kept until you delete it" : `kept ${c.sessionSettings.retentionDays} days`})` : "off (sessions.autoSave) or no project folder"}`, ...c.resumedFrom ? [`Resumed from: ${c.resumedFrom.slice(0, 8)}`] : [], `Persistent memory: ${c.memorySettings.enabled ? "ON" : "off"}`], "/session help lists the commands")] };
|
|
613
|
+
case "help":
|
|
614
|
+
return { blocks: [view2("SESSION COMMANDS", SESSION_HELP)] };
|
|
615
|
+
case "list": {
|
|
616
|
+
const all = rest.includes("--all");
|
|
617
|
+
const { sessions, skipped } = await c.sessions.list(all ? { all: true } : {});
|
|
618
|
+
const lines = sessions.length === 0 ? ["No saved sessions yet."] : sessions.slice(0, 30).map((s) => sessionRow(s, c.sessionId));
|
|
619
|
+
if (skipped > 0) lines.push(`(${skipped} damaged session folder(s) were skipped)`);
|
|
620
|
+
return { blocks: [view2(all ? "ALL SAVED SESSIONS" : "SAVED SESSIONS", lines, "/session resume <id> continues one (the first 8 characters are enough)")] };
|
|
621
|
+
}
|
|
622
|
+
case "show": {
|
|
623
|
+
if (!rest[0]) return { blocks: [notice3("warn", "Which session? /session show <id>")] };
|
|
624
|
+
const l = await c.sessions.load(rest[0]);
|
|
625
|
+
const m = l.meta;
|
|
626
|
+
return { blocks: [view2("SESSION DETAILS", [`Id: ${m.id}`, `Title: ${m.title}`, `Project: ${m.projectPath}`, `Created: ${shortDate(m.createdAt)} Last activity: ${shortDate(m.lastActivityAt)}`, `Status: ${m.status} Messages: ${m.messageCount}${m.truncated ? " (saving stopped at the limit)" : ""}`, `Provider / model: ${m.provider ?? "unknown"} / ${m.model ?? "unknown"}`, ...l.skippedLines ? [`${l.skippedLines} damaged line(s) skipped`] : [], "", ...l.summary ? ["Summary:", ...l.summary.split("\n")] : ["No summary yet."]])] };
|
|
627
|
+
}
|
|
628
|
+
case "new": {
|
|
629
|
+
const b = busy(ctx);
|
|
630
|
+
if (b) return { blocks: [notice3("warn", b)] };
|
|
631
|
+
const r = await c.startNew(ctx.signal);
|
|
632
|
+
return { blocks: [notice3("info", `Started a new session ${c.sessionId.slice(0, 8)}. ${r.finalized.summary ? `The previous session ${r.oldId.slice(0, 8)} was saved with a ${r.finalized.summary === "ai" ? "summary" : "plain summary"}.` : "The previous session had nothing to save."} The context is empty again.`)] };
|
|
633
|
+
}
|
|
634
|
+
case "resume": {
|
|
635
|
+
const b = busy(ctx);
|
|
636
|
+
if (b) return { blocks: [notice3("warn", b)] };
|
|
637
|
+
if (!rest[0]) return { blocks: [notice3("warn", "Which session? /session list shows them; then /session resume <id>")] };
|
|
638
|
+
const r = await c.resume(rest[0], ctx.signal);
|
|
639
|
+
const out = [notice3("info", `Resumed "${r.meta.title}" (${r.meta.id.slice(0, 8)}): ${r.restored} message(s) restored${r.archived > 0 ? `, the oldest ${r.archived} are represented by the saved summary` : ""}. Project memory ${c.memorySettings.enabled ? "is applied" : "is off, so none was applied"}. Nothing is sent to your provider until you write a message.`), notice3("info", r.providerNote)];
|
|
640
|
+
if (r.skippedLines > 0) out.push(notice3("warn", `${r.skippedLines} damaged line(s) in the saved file were skipped, so some messages are missing.`));
|
|
641
|
+
if (r.truncated) out.push(notice3("warn", "Saving stopped at the message limit for this session, so the end of the conversation may be missing."));
|
|
642
|
+
out.push(notice3("info", "Running tasks and their approvals are not saved, so no task state was restored."));
|
|
643
|
+
if (r.summary) out.push(view2("SESSION SUMMARY", r.summary.split("\n")));
|
|
644
|
+
return { blocks: out };
|
|
645
|
+
}
|
|
646
|
+
case "rename": {
|
|
647
|
+
if (rest.length < 2) return { blocks: [notice3("warn", "Usage: /session rename <id> <new title>")] };
|
|
648
|
+
const m = await c.sessions.rename(rest[0], rest.slice(1).join(" "));
|
|
649
|
+
return { blocks: [notice3("info", `Renamed ${m.id.slice(0, 8)} to "${m.title}".`)] };
|
|
650
|
+
}
|
|
651
|
+
case "summarize": {
|
|
652
|
+
await c.flush();
|
|
653
|
+
const { messages } = await c.sessions.readMessages(c.sessionId);
|
|
654
|
+
if (messages.length === 0) return { blocks: [notice3("info", "Nothing is saved for this session yet, so there is nothing to summarise.")] };
|
|
655
|
+
const { summarise } = await import("./memory-summary-4P26B2ZY.js");
|
|
656
|
+
const { createSummaryGenerator } = await import("./memory-session-R6DGT6CS.js");
|
|
657
|
+
const s = await summarise(messages, { generator: ctx.chat.backend ? createSummaryGenerator(() => ctx.chat.backend, ctx.settings.network.requestTimeoutSeconds * 1e3) : null, previousSummary: await c.sessions.readSummary(c.sessionId), signal: ctx.signal });
|
|
658
|
+
await c.sessions.setSummary(c.sessionId, s.text);
|
|
659
|
+
return { blocks: [notice3("info", s.method === "ai" ? "Summary written by your AI provider and saved." : `A plain summary was saved (${s.reason ?? "no AI"}).`), view2("SESSION SUMMARY", s.text.split("\n"))] };
|
|
660
|
+
}
|
|
661
|
+
case "export": {
|
|
662
|
+
let id = rest[0];
|
|
663
|
+
let fmt = rest[1];
|
|
664
|
+
if (id && ["json", "md", "markdown"].includes(id.toLowerCase())) {
|
|
665
|
+
fmt = id;
|
|
666
|
+
id = void 0;
|
|
667
|
+
}
|
|
668
|
+
await c.flush();
|
|
669
|
+
const format = (fmt ?? "md").toLowerCase() === "json" ? "json" : "markdown";
|
|
670
|
+
const target = id ?? c.sessionId;
|
|
671
|
+
const text2 = await c.sessions.exportSession(target, format);
|
|
672
|
+
const meta = await c.sessions.resolve(target);
|
|
673
|
+
const file = path.join(c.memory.store.layout.exportsDir, `${meta.id.slice(0, 8)}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-")}.${format === "json" ? "json" : "md"}`);
|
|
674
|
+
await atomicWriteFile(file, text2);
|
|
675
|
+
return { blocks: [notice3("info", `Exported ${meta.id.slice(0, 8)} as ${format === "json" ? "JSON" : "Markdown"} to ${file}. Sensitive-looking text was redacted; read it before sharing. For another location use: zg sessions export ${meta.id.slice(0, 8)} --output <file>`)] };
|
|
676
|
+
}
|
|
677
|
+
case "delete": {
|
|
678
|
+
if (!rest[0]) return { blocks: [notice3("warn", "Which session? /session delete <id> confirm")] };
|
|
679
|
+
const meta = await c.sessions.resolve(rest[0]);
|
|
680
|
+
if (meta.id === c.sessionId) return { blocks: [notice3("warn", "That is the session you are in. Start a new one first: /session new")] };
|
|
681
|
+
if (c.sessionSettings.confirmBeforeDelete && rest[1]?.toLowerCase() !== "confirm") return { blocks: [notice3("warn", `This deletes "${meta.title}" (${meta.messageCount} messages) for good. To go ahead: /session delete ${meta.id.slice(0, 8)} confirm`)] };
|
|
682
|
+
await c.sessions.delete(meta.id);
|
|
683
|
+
return { blocks: [notice3("info", `Deleted session ${meta.id.slice(0, 8)}.`)] };
|
|
684
|
+
}
|
|
685
|
+
default:
|
|
686
|
+
return { blocks: [notice3("warn", `Unknown /session option "${name}". /session help lists them.`)] };
|
|
687
|
+
}
|
|
688
|
+
} catch (err) {
|
|
689
|
+
return fail2(err);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
var memoryCommands = [
|
|
693
|
+
{ name: "memory", summary: "Persistent memory: status, list, search, add, enable, disable (/memory help)", run: (args, ctx) => memoryCommand(args, ctx) },
|
|
694
|
+
{ name: "sessions", summary: "List saved sessions", run: (args, ctx) => sessionCommand(`list ${args}`, ctx) },
|
|
695
|
+
{ name: "session", summary: "This session, or new/resume/rename/export/delete a saved one (/session help)", run: (args, ctx) => sessionCommand(args, ctx) }
|
|
696
|
+
];
|
|
697
|
+
|
|
698
|
+
export {
|
|
699
|
+
buildTaskSummary,
|
|
700
|
+
summaryToText,
|
|
701
|
+
agentCommands,
|
|
702
|
+
splitArgs,
|
|
703
|
+
gitCommands,
|
|
704
|
+
shortDate,
|
|
705
|
+
sessionRow,
|
|
706
|
+
entryRow,
|
|
707
|
+
memoryCommands
|
|
708
|
+
};
|