feat: expand slash-command goals from command templates
Resolves armed slash-command objectives from authoritative templates before dispatch Applies OpenCode argument expansion for goal metadata in UI and scheduled tasks Falls back to the raw invocation when command details are unavailable
This commit is contained in:
@@ -2,7 +2,7 @@ import express from 'express';
|
||||
import { createOpencodeClient } from '@opencode-ai/sdk/v2';
|
||||
import { createWorktree } from '../git/index.js';
|
||||
import { expandSnippets } from '../opencode/snippets.js';
|
||||
import { parseScheduledCommandPrompt } from '../scheduled-tasks/runtime.js';
|
||||
import { expandCommandGoalObjective, parseScheduledCommandPrompt } from '../scheduled-tasks/runtime.js';
|
||||
import { buildGoalIntroText, createSessionGoal } from '../session-goal/create.js';
|
||||
import { OpenChamberControlError, asControlError } from '../openchamber-control/error.js';
|
||||
|
||||
@@ -369,13 +369,27 @@ export const createOpenChamberSessionService = (dependencies) => {
|
||||
}
|
||||
|
||||
const expandedPrompt = expandSnippets(prompt, directory);
|
||||
const parsedCommand = parseScheduledCommandPrompt(prompt);
|
||||
let resolvedCommand = null;
|
||||
if (parsedCommand) {
|
||||
try {
|
||||
const response = await client.command.list({ directory });
|
||||
const commands = Array.isArray(response?.data) ? response.data : [];
|
||||
const command = commands.find((candidate) => candidate?.name === parsedCommand.command);
|
||||
if (command) resolvedCommand = { ...parsedCommand, template: command.template };
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
if (goalInput.enabled) {
|
||||
const commandObjective = resolvedCommand
|
||||
? expandCommandGoalObjective(resolvedCommand.template, resolvedCommand.arguments)
|
||||
: null;
|
||||
await (createSessionGoalOverride || createSessionGoal)({
|
||||
baseUrl,
|
||||
authHeaders,
|
||||
sessionID,
|
||||
directory,
|
||||
objective: expandedPrompt,
|
||||
objective: commandObjective ?? expandedPrompt,
|
||||
tokenBudget: goalInput.tokenBudget,
|
||||
providerID: model.providerID,
|
||||
modelID: model.modelID,
|
||||
@@ -388,35 +402,21 @@ export const createOpenChamberSessionService = (dependencies) => {
|
||||
return error;
|
||||
};
|
||||
|
||||
let dispatchedAsCommand = false;
|
||||
const parsedCommand = parseScheduledCommandPrompt(prompt);
|
||||
if (parsedCommand) {
|
||||
let commandExists = false;
|
||||
if (resolvedCommand) {
|
||||
try {
|
||||
const response = await client.command.list({ directory });
|
||||
const commands = Array.isArray(response?.data) ? response.data : [];
|
||||
commandExists = commands.some((command) => command?.name === parsedCommand.command);
|
||||
} catch {
|
||||
await client.session.command({
|
||||
sessionID,
|
||||
directory,
|
||||
command: resolvedCommand.command,
|
||||
arguments: resolvedCommand.arguments,
|
||||
...(agent ? { agent } : {}),
|
||||
model: `${model.providerID}/${model.modelID}`,
|
||||
...(variant ? { variant } : {}),
|
||||
});
|
||||
} catch (error) {
|
||||
throw markGoalPartial(error);
|
||||
}
|
||||
if (commandExists) {
|
||||
try {
|
||||
await client.session.command({
|
||||
sessionID,
|
||||
directory,
|
||||
command: parsedCommand.command,
|
||||
arguments: parsedCommand.arguments,
|
||||
...(agent ? { agent } : {}),
|
||||
model: `${model.providerID}/${model.modelID}`,
|
||||
...(variant ? { variant } : {}),
|
||||
});
|
||||
} catch (error) {
|
||||
throw markGoalPartial(error);
|
||||
}
|
||||
dispatchedAsCommand = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dispatchedAsCommand) {
|
||||
} else {
|
||||
try {
|
||||
await runPromptAsync({
|
||||
baseUrl,
|
||||
@@ -440,7 +440,7 @@ export const createOpenChamberSessionService = (dependencies) => {
|
||||
}
|
||||
}
|
||||
|
||||
return { model, agent, variant, promptDispatched: true, dispatchedAsCommand };
|
||||
return { model, agent, variant, promptDispatched: true, dispatchedAsCommand: Boolean(resolvedCommand) };
|
||||
};
|
||||
|
||||
const create = async (payload = {}) => {
|
||||
|
||||
@@ -361,6 +361,44 @@ describe('openchamber session routes', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('uses the expanded slash-command template as the goal objective before command dispatch', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const createSessionGoal = vi.fn(async () => undefined);
|
||||
commandListMock.mockResolvedValue({
|
||||
data: [{
|
||||
name: 'issue--to-pr',
|
||||
template: 'Take $ARGUMENTS from issue through a verified pull request. Confirm the PR covers $ARGUMENTS.',
|
||||
}],
|
||||
});
|
||||
globalThis.fetch = vi.fn();
|
||||
try {
|
||||
const { app } = createApp({ createSessionGoal });
|
||||
const response = await request(app)
|
||||
.post('/api/openchamber/sessions/ses_source/send')
|
||||
.send({
|
||||
directory: '/repo/app',
|
||||
prompt: '/issue--to-pr LIN-123',
|
||||
model: 'openai/gpt-5.5',
|
||||
agent: 'build',
|
||||
goal: true,
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
expect(createSessionGoal).toHaveBeenCalledWith(expect.objectContaining({
|
||||
objective: 'Take LIN-123 from issue through a verified pull request. Confirm the PR covers LIN-123.',
|
||||
}));
|
||||
expect(sessionCommandMock).toHaveBeenCalledWith(expect.objectContaining({
|
||||
command: 'issue--to-pr',
|
||||
arguments: 'LIN-123',
|
||||
}));
|
||||
expect(createSessionGoal.mock.invocationCallOrder[0]).toBeLessThan(sessionCommandMock.mock.invocationCallOrder[0]);
|
||||
expect(response.body).toMatchObject({ goalEnabled: true, dispatchedAsCommand: true });
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
it('reuses the previous session selection when send omits model, agent, and variant', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const fetchMock = vi.fn(async () => ({ ok: true, text: async () => '' }));
|
||||
|
||||
@@ -94,6 +94,32 @@ export const parseScheduledCommandPrompt = (prompt) => {
|
||||
};
|
||||
};
|
||||
|
||||
export const expandCommandGoalObjective = (template, argumentsText) => {
|
||||
if (typeof template !== 'string' || !template.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawArguments = String(argumentsText ?? '');
|
||||
if (template.includes('$ARGUMENTS')) {
|
||||
return template.replaceAll('$ARGUMENTS', rawArguments);
|
||||
}
|
||||
|
||||
const positions = [...template.matchAll(/\$(\d+)/g)].map((match) => Number(match[1]));
|
||||
if (positions.length > 0) {
|
||||
const parsedArguments = [...rawArguments.matchAll(/"([^"]*)"|'([^']*)'|(\S+)/g)]
|
||||
.map((match) => match[1] ?? match[2] ?? match[3] ?? '');
|
||||
const lastPosition = Math.max(...positions);
|
||||
return template.replace(/\$(\d+)/g, (_match, value) => {
|
||||
const position = Number(value);
|
||||
return position === lastPosition
|
||||
? parsedArguments.slice(position - 1).join(' ')
|
||||
: (parsedArguments[position - 1] ?? '');
|
||||
});
|
||||
}
|
||||
|
||||
return rawArguments ? `${template}\n\n${rawArguments}` : template;
|
||||
};
|
||||
|
||||
export const computeNextRunAt = (task, nowMs = Date.now()) => {
|
||||
if (!task?.enabled) {
|
||||
return null;
|
||||
@@ -445,10 +471,10 @@ export const createScheduledTasksRuntime = (deps) => {
|
||||
}
|
||||
};
|
||||
|
||||
const runScheduledCommandIfApplicable = async ({ client, projectPath, sessionID, task }) => {
|
||||
const resolveScheduledCommand = async ({ client, projectPath, task }) => {
|
||||
const parsed = parseScheduledCommandPrompt(task?.execution?.prompt);
|
||||
if (!parsed) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
let commands = [];
|
||||
@@ -456,25 +482,24 @@ export const createScheduledTasksRuntime = (deps) => {
|
||||
const response = await client.command.list({ directory: projectPath });
|
||||
commands = Array.isArray(response?.data) ? response.data : [];
|
||||
} catch {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
const hasMatchingCommand = commands.some((command) => command?.name === parsed.command);
|
||||
if (!hasMatchingCommand) {
|
||||
return false;
|
||||
}
|
||||
const command = commands.find((candidate) => candidate?.name === parsed.command);
|
||||
return command ? { ...parsed, template: command.template } : null;
|
||||
};
|
||||
|
||||
const runScheduledCommand = async ({ client, projectPath, sessionID, task, command }) => {
|
||||
await client.session.command({
|
||||
sessionID,
|
||||
directory: projectPath,
|
||||
command: parsed.command,
|
||||
arguments: parsed.arguments,
|
||||
command: command.command,
|
||||
arguments: command.arguments,
|
||||
...(task.execution.agent ? { agent: task.execution.agent } : {}),
|
||||
model: `${task.execution.providerID}/${task.execution.modelID}`,
|
||||
...(task.execution.variant ? { variant: task.execution.variant } : {}),
|
||||
});
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const runTaskWithWatchdog = async (projectID, task, reason) => {
|
||||
@@ -527,13 +552,18 @@ export const createScheduledTasksRuntime = (deps) => {
|
||||
}
|
||||
}
|
||||
|
||||
const scheduledCommand = await resolveScheduledCommand({ client, projectPath, task });
|
||||
|
||||
if (task.execution.goalEnabled) {
|
||||
const commandObjective = scheduledCommand
|
||||
? expandCommandGoalObjective(scheduledCommand.template, scheduledCommand.arguments)
|
||||
: null;
|
||||
await createSessionGoal({
|
||||
baseUrl,
|
||||
authHeaders,
|
||||
sessionID,
|
||||
directory: projectPath,
|
||||
objective: expandSnippets(task.execution.prompt, projectPath),
|
||||
objective: commandObjective ?? expandSnippets(task.execution.prompt, projectPath),
|
||||
tokenBudget: task.execution.goalTokenBudget,
|
||||
providerID: task.execution.providerID,
|
||||
modelID: task.execution.modelID,
|
||||
@@ -541,13 +571,9 @@ export const createScheduledTasksRuntime = (deps) => {
|
||||
});
|
||||
}
|
||||
|
||||
const executedAsCommand = await runScheduledCommandIfApplicable({
|
||||
client,
|
||||
projectPath,
|
||||
sessionID,
|
||||
task,
|
||||
});
|
||||
if (!executedAsCommand) {
|
||||
if (scheduledCommand) {
|
||||
await runScheduledCommand({ client, projectPath, sessionID, task, command: scheduledCommand });
|
||||
} else {
|
||||
await runPromptAsync({
|
||||
baseUrl,
|
||||
authHeaders,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { computeNextRunAt, formatScheduledSessionTitle, parseScheduledCommandPrompt } from './runtime.js';
|
||||
import { computeNextRunAt, expandCommandGoalObjective, formatScheduledSessionTitle, parseScheduledCommandPrompt } from './runtime.js';
|
||||
|
||||
describe('scheduled-tasks runtime helpers', () => {
|
||||
it('computes next daily run in timezone', () => {
|
||||
@@ -97,4 +97,15 @@ describe('scheduled-tasks runtime helpers', () => {
|
||||
expect(parseScheduledCommandPrompt('Summarize open issues')).toBeNull();
|
||||
expect(parseScheduledCommandPrompt('/')).toBeNull();
|
||||
});
|
||||
|
||||
it('expands command arguments into the goal objective', () => {
|
||||
expect(expandCommandGoalObjective(
|
||||
'Run the issue pipeline for $ARGUMENTS. Verify $ARGUMENTS is represented by the PR.',
|
||||
'LIN-123 --draft',
|
||||
)).toBe('Run the issue pipeline for LIN-123 --draft. Verify LIN-123 --draft is represented by the PR.');
|
||||
expect(expandCommandGoalObjective(undefined, 'LIN-123')).toBeNull();
|
||||
expect(expandCommandGoalObjective('Move $1 to $2', '"src old" dist extra')).toBe('Move src old to dist extra');
|
||||
expect(expandCommandGoalObjective('Review the requested scope.', 'auth module'))
|
||||
.toBe('Review the requested scope.\n\nauth module');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -160,6 +160,12 @@ sees only that final turn, so the report is its evidence.
|
||||
colors/labels shared across surfaces.
|
||||
- `stores/useSessionGoalArmStore.ts` — the "next prompt starts a goal" flag,
|
||||
consumed by `sendMessage` in `sync/session-ui-store.ts` (works for drafts).
|
||||
Armed slash commands resolve their authoritative command template and apply
|
||||
OpenCode argument expansion (`$ARGUMENTS`, positional placeholders, or the
|
||||
implicit argument suffix) for the audit objective before goal metadata is
|
||||
written and before `session.command` dispatch. If command details cannot be
|
||||
loaded, the raw invocation remains the objective rather than blocking command
|
||||
execution.
|
||||
- `hooks/useSessionGoal.ts` — live goal state.
|
||||
- `components/chat/SessionGoalButton.tsx` — composer target button
|
||||
(arm / status color / cancel confirm); `SessionGoalRow.tsx` — goal strip
|
||||
@@ -172,7 +178,8 @@ sees only that final turn, so the report is its evidence.
|
||||
Scheduled tasks can run as goals: `execution.goalEnabled` (+ optional
|
||||
`execution.goalTokenBudget`) on a task makes the scheduled-tasks runtime
|
||||
stamp `metadata.openchamber.goal` onto the fresh session (objective = the
|
||||
expanded task prompt) and attach the goal-mode intro part to the prompt.
|
||||
expanded task prompt, or the argument-expanded command template for a slash
|
||||
command) and attach the goal-mode intro part to normal prompts.
|
||||
The loop here picks it up from session events like any other goal.
|
||||
|
||||
## CLI-created goals
|
||||
@@ -183,8 +190,10 @@ session, fits and stores the expanded prompt as its objective, patches active
|
||||
goal metadata, appends the synthetic goal reminder, and only then dispatches
|
||||
the prompt. `--goal-token-budget` applies the same optional budget contract as
|
||||
scheduled goals. Slash commands retain command dispatch semantics and cannot
|
||||
carry the synthetic prompt part; the goal metadata is still installed before
|
||||
the command runs.
|
||||
carry the synthetic prompt part. Their command template with OpenCode argument
|
||||
expansion becomes the audit objective; goal metadata
|
||||
is still installed before the command runs. A missing command template falls
|
||||
back to the raw invocation.
|
||||
|
||||
`openchamber session send --goal` and `openchamber session fork --goal` use
|
||||
the same server-owned prompt orchestration. Send installs a fresh goal on the
|
||||
|
||||
Reference in New Issue
Block a user