Files
hapi/cli/src/copilot/utils/slashCommands.ts
T
f10fbc7496 feat(cli): add GitHub Copilot CLI agent support via ACP (#1245)
* feat(cli): add GitHub Copilot CLI agent support via ACP

Wrap `copilot --acp --stdio` for remote sessions and spawn the native TUI locally, with full hub/web integration for spawn, resume, and permissions.

Fixes tiann/hapi#362

Co-Authored-By: HAPI <noreply@hapi.run>
Co-authored-by: Cursor <cursoragent@cursor.com>

* feat(copilot): agent modes, models, slash/file UX, local session sync

Add Interactive/Plan/Autopilot (fleet is slash-only), subscription-aware
model discovery, web StatusBar/permission UX, @ file mentions, and fix
local Safe Yolo plus session-id locator for handoff/resume.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: re-trigger Codex PR review after auth outage

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore: retry Codex PR review

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): preserve agent mode on resume and apply via ACP set_mode

Resume was dropping copilotAgentMode so Plan/Autopilot reset to interactive.
Also switch local/remote mode application to --mode / session set_mode instead of slash prompts.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): wake remote loop when agent mode changes

Empty isolated queue tick lets setMode apply without inventing a user prompt.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): confirm mode changes before persisting

Await Copilot mode changes and expose discovered models so session state reflects backend acceptance.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): guard mode discovery and slash updates

Keep model probes within runner roots and preserve active sessions when mode switching is unavailable or rejected.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): preserve resume and auto semantics

Deduplicate Copilot resume rows, apply Auto explicitly, and fail closed on denied permissions.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): close permission and model discovery gaps

Keep write-capable commands pending in read-only mode, extend model probe RPCs, and preserve explicit model validation before session creation.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): persist runtime model and agent mode

Fallback to ACP model options when direct model switching is unavailable and retain Copilot agent mode across hub restarts.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): normalize composer auto selection

Use the null session sentinel for Copilot Auto so the composer selects and resets default models consistently.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(copilot): reject local permission mode changes

* style(copilot): remove trailing blank line

* fix(copilot): secure local config handoffs

* fix(copilot): reject local agent mode slashes

* fix(copilot): reject mode changes during turns

* fix(copilot): consume rejected slash updates

* fix(copilot): preserve thinking across slash handling

* fix(copilot): stabilize async config changes

* fix(copilot): roll back rejected startup model

* fix(copilot): preserve cancellation and file mentions

* fix(copilot): hide local permission controls

* fix(deps): support clean workspace installs

* test(copilot): account for spawn mode argument

* fix(copilot): attribute usage to active model

---------

Co-authored-by: HAPI <noreply@hapi.run>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-04 08:19:12 +08:00

223 lines
7.5 KiB
TypeScript

import { COPILOT_PERMISSION_MODES } from '@hapi/protocol/modes';
import type { CopilotAgentMode } from '@hapi/protocol';
import type { CopilotPermissionMode } from '@hapi/protocol/types';
import type { SlashCommand } from '@/modules/common/slashCommands';
export type CopilotSlashResolution =
| { kind: 'passthrough' }
| {
kind: 'handled';
message: string;
updates?: {
permissionMode?: CopilotPermissionMode;
model?: string | null;
agentMode?: CopilotAgentMode;
};
}
| {
kind: 'replace';
text: string;
message?: string;
updates?: {
permissionMode?: CopilotPermissionMode;
model?: string | null;
agentMode?: CopilotAgentMode;
};
};
function resolveCopilotPermissionMode(rest: string): CopilotPermissionMode | null {
const normalized = rest.trim().toLowerCase();
if (!normalized) return null;
if (normalized === 'default' || normalized === 'off') return 'default';
if ((COPILOT_PERMISSION_MODES as readonly string[]).includes(normalized)) {
return normalized as CopilotPermissionMode;
}
return null;
}
function resolveCopilotAgentMode(rest: string): CopilotAgentMode | null {
const normalized = rest.trim().toLowerCase();
if (!normalized || normalized === 'default' || normalized === 'off' || normalized === 'interactive') {
return 'interactive';
}
if (normalized === 'plan') return 'plan';
if (normalized === 'autopilot') return 'autopilot';
return null;
}
export function resolveCopilotSlashCommand(
text: string,
state: {
commands?: readonly SlashCommand[];
permissionMode: CopilotPermissionMode;
model?: string | null;
agentMode: CopilotAgentMode;
}
): CopilotSlashResolution {
const match = /^\s*\/([a-z0-9:_-]+)(?:\s+([\s\S]*))?$/i.exec(text);
if (!match) return { kind: 'passthrough' };
const command = match[1]?.toLowerCase();
const rest = match[2]?.trim() ?? '';
if (!command) return { kind: 'passthrough' };
const custom = state.commands?.find((candidate) =>
candidate.source !== 'builtin' && candidate.name.toLowerCase() === command
);
if (custom?.content) {
return {
kind: 'replace',
text: rest ? `${custom.content}\n\nUser arguments: ${rest}` : custom.content,
message: `Expanded /${custom.name}`
};
}
if (command === 'help') {
const lines = (state.commands ?? [])
.filter((entry) => entry.source === 'builtin')
.map((entry) => `- \`/${entry.name}\` — ${entry.description}`);
return {
kind: 'handled',
message: [
'**Supported Copilot slash commands**',
'',
...lines,
'',
'`/fleet` is orthogonal to agent mode (Interactive / Plan / Autopilot) and is passed through to Copilot CLI.'
].join('\n')
};
}
if (command === 'status') {
return {
kind: 'handled',
message: [
'**Copilot status**',
'',
`- agent mode: \`${state.agentMode}\``,
`- permission: \`${state.permissionMode}\``,
`- model: \`${state.model ?? 'auto'}\``
].join('\n')
};
}
if (command === 'model') {
if (!rest) {
return { kind: 'handled', message: `Copilot model: ${state.model ?? 'auto'}` };
}
const model = rest === 'auto' || rest === 'default' ? null : rest;
return {
kind: 'handled',
message: `Copilot model set to ${model ?? 'auto'}`,
updates: { model }
};
}
if (command === 'permissions' || command === 'permission') {
if (!rest) {
return { kind: 'handled', message: `Copilot permission mode: ${state.permissionMode}` };
}
const permissionMode = resolveCopilotPermissionMode(rest);
if (!permissionMode) {
return {
kind: 'handled',
message: `Unknown permission mode \`${rest}\`. Use one of: ${COPILOT_PERMISSION_MODES.join(', ')}`
};
}
return {
kind: 'handled',
message: `Copilot permission mode set to ${permissionMode}`,
updates: { permissionMode }
};
}
if (command === 'plan') {
const lowerRest = rest.toLowerCase();
if (lowerRest === 'off' || lowerRest === 'default' || lowerRest === 'exit' || lowerRest === 'disable') {
return {
kind: 'handled',
message: 'Copilot plan mode disabled',
updates: { agentMode: 'interactive' }
};
}
if (rest) {
return {
kind: 'replace',
text: rest,
message: 'Copilot plan mode enabled',
updates: { agentMode: 'plan' }
};
}
return {
kind: 'handled',
message: 'Copilot plan mode enabled',
updates: { agentMode: 'plan' }
};
}
if (command === 'autopilot') {
const lowerRest = rest.toLowerCase();
if (lowerRest === 'off' || lowerRest === 'default' || lowerRest === 'exit' || lowerRest === 'disable') {
return {
kind: 'handled',
message: 'Copilot autopilot mode disabled',
updates: { agentMode: 'interactive' }
};
}
if (rest) {
return {
kind: 'replace',
text: rest,
message: 'Copilot autopilot mode enabled',
updates: { agentMode: 'autopilot' }
};
}
return {
kind: 'handled',
message: 'Copilot autopilot mode enabled',
updates: { agentMode: 'autopilot' }
};
}
// /fleet is not an agent mode — pass through so Copilot CLI can orchestrate
// parallel subagents alongside Interactive / Plan / Autopilot.
if (command === 'fleet') {
return { kind: 'passthrough' };
}
if (command === 'interactive' || command === 'default') {
return {
kind: 'handled',
message: 'Copilot interactive mode enabled',
updates: { agentMode: 'interactive' }
};
}
if (command === 'mode') {
if (rest.trim().toLowerCase() === 'fleet') {
return {
kind: 'handled',
message: 'Fleet is not an agent mode. Use `/fleet <task>` (works with Interactive, Plan, or Autopilot).'
};
}
const agentMode = resolveCopilotAgentMode(rest);
if (!agentMode) {
return {
kind: 'handled',
message: 'Unknown agent mode. Use interactive, plan, or autopilot. For parallel subagents use `/fleet <task>`.'
};
}
return {
kind: 'handled',
message: `Copilot agent mode set to ${agentMode}`,
updates: { agentMode }
};
}
if (command === 'context' || command === 'usage' || command === 'tasks' || command === 'subagents' || command === 'agents' || command === 'delegate' || command === 'agent' || command === 'rubber-duck' || command === 'security-review' || command === 'research' || command === 'review' || command === 'skills') {
return { kind: 'passthrough' };
}
return { kind: 'passthrough' };
}