Skip to content

Commit 39ed6db

Browse files
committed
refactor(tui): consolidate runtime ownership
1 parent afe024e commit 39ed6db

10 files changed

Lines changed: 443 additions & 737 deletions

‎src/auto-reply/reply/commands-goal.test.ts‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import { loadSessionEntry, replaceSessionEntry } from "../../config/sessions/ses
77
import type { SessionEntry } from "../../config/sessions/types.js";
88
import type { OpenClawConfig } from "../../config/types.openclaw.js";
99
import { takeCommandSessionMetadataChanges } from "./command-session-metadata.js";
10-
import {
11-
formatGoalContinuationPrompt,
12-
handleGoalCommand,
13-
parseGoalCommand,
14-
} from "./commands-goal.js";
10+
import { handleGoalCommand, parseGoalCommand } from "./commands-goal.js";
1511
import type { HandleCommandsParams } from "./commands-types.js";
1612
import { parseInlineDirectives } from "./directive-handling.parse.js";
1713

@@ -108,18 +104,6 @@ describe("goal commands", () => {
108104
});
109105
});
110106

111-
it("formats command-looking continuation prompts so inline directives leave them intact", () => {
112-
const prompt = formatGoalContinuationPrompt("ship /fast off");
113-
expect(prompt).toBe(
114-
`Pursue this goal exactly as written from this JSON string: "ship \\/fast off"`,
115-
);
116-
117-
const directives = parseInlineDirectives(prompt);
118-
119-
expect(directives.cleaned).toBe(prompt);
120-
expect(directives.hasFastDirective).toBe(false);
121-
});
122-
123107
it("starts a goal from Codex-style bare /goal objective text", async () => {
124108
const storePath = await createStorePath();
125109
await upsertSessionEntry({

‎src/auto-reply/reply/commands-goal.ts‎

Lines changed: 121 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
updateSessionGoalStatus,
1313
} from "../../config/sessions.js";
1414
import { loadSessionEntry as getSessionEntry } from "../../config/sessions/session-accessor.js";
15+
import type { SessionEntry } from "../../config/sessions/types.js";
1516
import { applyCommandTextToParams } from "./command-context-rewrite.js";
1617
import { commandReply as goalReply, defineAuthorizedTextCommand } from "./command-gates.js";
1718
import { markCommandSessionMetadataChanged } from "./command-session-metadata.js";
@@ -84,16 +85,14 @@ function encodeGoalJsonString(trimmed: string): string {
8485
return JSON.stringify(trimmed).replaceAll("/", "\\/");
8586
}
8687

87-
/** Formats the model prompt used to continue a newly started goal. */
88-
export function formatGoalContinuationPrompt(objective: string): string {
88+
function formatGoalContinuationPrompt(objective: string): string {
8989
const trimmed = objective.trim();
9090
return hasCommandLikeGoalText(trimmed)
9191
? `${GOAL_CONTINUATION_PROMPT_PREFIX} ${encodeGoalJsonString(trimmed)}`
9292
: trimmed;
9393
}
9494

95-
/** Formats the model prompt used when resuming a paused goal. */
96-
export function formatGoalResumeContinuationPrompt(note: string): string {
95+
function formatGoalResumeContinuationPrompt(note: string): string {
9796
const trimmed = note.trim();
9897
if (!trimmed) {
9998
return "Continue pursuing the current goal.";
@@ -121,135 +120,130 @@ function goalErrorReply(error: unknown): CommandHandlerResult {
121120
return goalReply(`Goal error: ${message}`);
122121
}
123122

123+
type ParsedGoalCommand = NonNullable<ReturnType<typeof parseGoalCommand>>;
124+
125+
type SessionGoalCommandResult = {
126+
text: string;
127+
continuationPrompt?: string;
128+
changed: boolean;
129+
};
130+
131+
/** Execute goal storage policy once for auto-reply, Gateway, and embedded callers. */
132+
export async function executeSessionGoalCommand(params: {
133+
parsed: ParsedGoalCommand;
134+
sessionKey: string;
135+
storePath?: string;
136+
fallbackEntry?: SessionEntry;
137+
agentId?: string;
138+
readOnlyStatus?: boolean;
139+
}): Promise<SessionGoalCommandResult> {
140+
const common = {
141+
sessionKey: params.sessionKey,
142+
storePath: params.storePath,
143+
actor: { type: "human" as const },
144+
agentId: params.agentId,
145+
};
146+
const note = params.parsed.text ? { note: params.parsed.text } : {};
147+
148+
switch (params.parsed.action) {
149+
case "status": {
150+
const snapshot = await getSessionGoal({
151+
sessionKey: params.sessionKey,
152+
storePath: params.storePath,
153+
...(params.readOnlyStatus ? { fallbackEntry: params.fallbackEntry, persist: false } : {}),
154+
});
155+
return { text: formatSessionGoalStatus(snapshot.goal), changed: false };
156+
}
157+
case "start":
158+
case "set":
159+
case "create": {
160+
const objective = normalizeOptionalString(params.parsed.text);
161+
if (!objective) {
162+
return { text: "Usage: /goal start <objective>", changed: false };
163+
}
164+
const goal = await createSessionGoal({
165+
...common,
166+
objective,
167+
fallbackEntry: params.fallbackEntry,
168+
});
169+
return {
170+
text: `Goal started: ${goal.objective}`,
171+
continuationPrompt: formatGoalContinuationPrompt(goal.objective),
172+
changed: true,
173+
};
174+
}
175+
case "edit": {
176+
const objective = normalizeOptionalString(params.parsed.text);
177+
if (!objective) {
178+
return { text: "Usage: /goal edit <objective>", changed: false };
179+
}
180+
const goal = await updateSessionGoalObjective({ ...common, objective });
181+
return { text: `Goal updated: ${goal.objective}`, changed: true };
182+
}
183+
case "pause": {
184+
const goal = await updateSessionGoalStatus({ ...common, status: "paused", ...note });
185+
return { text: `Goal paused: ${goal.objective}`, changed: true };
186+
}
187+
case "resume": {
188+
const goal = await updateSessionGoalStatus({ ...common, status: "active", ...note });
189+
return {
190+
text: `Goal resumed: ${goal.objective}`,
191+
continuationPrompt: formatGoalResumeContinuationPrompt(params.parsed.text),
192+
changed: true,
193+
};
194+
}
195+
case "complete":
196+
case "done": {
197+
const goal = await updateSessionGoalStatus({ ...common, status: "complete", ...note });
198+
return {
199+
text: `Goal complete: ${goal.objective}\nTokens used: ${goal.tokensUsed}`,
200+
changed: true,
201+
};
202+
}
203+
case "block":
204+
case "blocked": {
205+
const goal = await updateSessionGoalStatus({ ...common, status: "blocked", ...note });
206+
return { text: `Goal blocked: ${goal.objective}`, changed: true };
207+
}
208+
case "clear": {
209+
const removed = await clearSessionGoal(common);
210+
return {
211+
text: removed ? "Goal cleared." : "No goal to clear.",
212+
changed: removed,
213+
};
214+
}
215+
default:
216+
return {
217+
text: "Usage: /goal <objective> | /goal [status] | /goal start <objective> | /goal edit <objective> | /goal pause|resume|complete|block|clear",
218+
changed: false,
219+
};
220+
}
221+
}
222+
124223
/** Command handler for /goal lifecycle commands. */
125224
export const handleGoalCommand: CommandHandler = defineAuthorizedTextCommand(
126225
{ label: "/goal", match: parseGoalCommand },
127226
async (params, parsed) => {
128-
const actor = { type: "human" as const };
129-
const goalAgentId = params.agentId;
130-
131227
try {
132-
switch (parsed.action) {
133-
case "status": {
134-
const snapshot = await getSessionGoal({
135-
sessionKey: params.sessionKey,
136-
storePath: params.storePath,
137-
fallbackEntry: params.sessionEntry,
138-
persist: false,
139-
});
140-
syncGoalSessionEntry(params);
141-
return goalReply(formatSessionGoalStatus(snapshot.goal));
142-
}
143-
case "start":
144-
case "set":
145-
case "create": {
146-
const objective = normalizeOptionalString(parsed.text);
147-
if (!objective) {
148-
return goalReply("Usage: /goal start <objective>");
149-
}
150-
const goal = await createSessionGoal({
151-
sessionKey: params.sessionKey,
152-
storePath: params.storePath,
153-
objective,
154-
fallbackEntry: params.sessionEntry,
155-
actor,
156-
agentId: goalAgentId,
157-
});
158-
syncGoalSessionEntry(params);
159-
markCommandSessionMetadataChanged(params);
160-
applyCommandTextToParams(params, formatGoalContinuationPrompt(goal.objective));
161-
return goalContinuation();
162-
}
163-
case "edit": {
164-
const objective = normalizeOptionalString(parsed.text);
165-
if (!objective) {
166-
return goalReply("Usage: /goal edit <objective>");
167-
}
168-
const goal = await updateSessionGoalObjective({
169-
sessionKey: params.sessionKey,
170-
storePath: params.storePath,
171-
objective,
172-
actor,
173-
agentId: goalAgentId,
174-
});
175-
syncGoalSessionEntry(params);
176-
markCommandSessionMetadataChanged(params);
177-
return goalReply(`Goal updated: ${goal.objective}`);
178-
}
179-
case "pause": {
180-
const goal = await updateSessionGoalStatus({
181-
sessionKey: params.sessionKey,
182-
storePath: params.storePath,
183-
status: "paused",
184-
actor,
185-
agentId: goalAgentId,
186-
...(parsed.text ? { note: parsed.text } : {}),
187-
});
188-
syncGoalSessionEntry(params);
189-
markCommandSessionMetadataChanged(params);
190-
return goalReply(`Goal paused: ${goal.objective}`);
191-
}
192-
case "resume": {
193-
await updateSessionGoalStatus({
194-
sessionKey: params.sessionKey,
195-
storePath: params.storePath,
196-
status: "active",
197-
actor,
198-
agentId: goalAgentId,
199-
...(parsed.text ? { note: parsed.text } : {}),
200-
});
201-
syncGoalSessionEntry(params);
202-
markCommandSessionMetadataChanged(params);
203-
const message = formatGoalResumeContinuationPrompt(parsed.text);
204-
applyCommandTextToParams(params, message);
205-
return goalContinuation();
206-
}
207-
case "complete":
208-
case "done": {
209-
const goal = await updateSessionGoalStatus({
210-
sessionKey: params.sessionKey,
211-
storePath: params.storePath,
212-
status: "complete",
213-
actor,
214-
agentId: goalAgentId,
215-
...(parsed.text ? { note: parsed.text } : {}),
216-
});
217-
syncGoalSessionEntry(params);
218-
markCommandSessionMetadataChanged(params);
219-
return goalReply(`Goal complete: ${goal.objective}\nTokens used: ${goal.tokensUsed}`);
220-
}
221-
case "block":
222-
case "blocked": {
223-
const goal = await updateSessionGoalStatus({
224-
sessionKey: params.sessionKey,
225-
storePath: params.storePath,
226-
status: "blocked",
227-
actor,
228-
agentId: goalAgentId,
229-
...(parsed.text ? { note: parsed.text } : {}),
230-
});
231-
syncGoalSessionEntry(params);
232-
markCommandSessionMetadataChanged(params);
233-
return goalReply(`Goal blocked: ${goal.objective}`);
234-
}
235-
case "clear": {
236-
const removed = await clearSessionGoal({
237-
sessionKey: params.sessionKey,
238-
storePath: params.storePath,
239-
actor,
240-
agentId: goalAgentId,
241-
});
242-
syncGoalSessionEntry(params);
243-
if (removed) {
244-
markCommandSessionMetadataChanged(params);
245-
}
246-
return goalReply(removed ? "Goal cleared." : "No goal to clear.");
247-
}
248-
default:
249-
return goalReply(
250-
"Usage: /goal <objective> | /goal [status] | /goal start <objective> | /goal edit <objective> | /goal pause|resume|complete|block|clear",
251-
);
228+
const result = await executeSessionGoalCommand({
229+
parsed,
230+
sessionKey: params.sessionKey,
231+
storePath: params.storePath,
232+
fallbackEntry: params.sessionEntry,
233+
agentId: params.agentId,
234+
readOnlyStatus: true,
235+
});
236+
if (result.changed || parsed.action === "status" || parsed.action === "clear") {
237+
syncGoalSessionEntry(params);
238+
}
239+
if (result.changed) {
240+
markCommandSessionMetadataChanged(params);
241+
}
242+
if (result.continuationPrompt) {
243+
applyCommandTextToParams(params, result.continuationPrompt);
244+
return goalContinuation();
252245
}
246+
return goalReply(result.text);
253247
} catch (error) {
254248
return goalErrorReply(error);
255249
}

0 commit comments

Comments
 (0)