-
Notifications
You must be signed in to change notification settings - Fork 354
feat(tui): add agents status command #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grandmaster451
wants to merge
1
commit into
MoonshotAI:main
Choose a base branch
from
grandmaster451:feat/agents-status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Add the /agents slash command to show background subagent status. Run /agents to list background subagents. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import type { AgentBackgroundTaskInfo, BackgroundTaskInfo } from '@moonshot-ai/kimi-code-sdk'; | ||
|
|
||
| import { UsagePanelComponent } from '../components/messages/usage-panel'; | ||
| import { NO_ACTIVE_SESSION_MESSAGE } from '../constant/kimi-tui'; | ||
| import { formatErrorMessage } from '../utils/event-payload'; | ||
| import type { SlashCommandHost } from './dispatch'; | ||
|
|
||
| const AGENTS_USAGE = 'Usage: /agents [status]'; | ||
|
|
||
| export async function handleAgentsCommand(host: SlashCommandHost, args: string): Promise<void> { | ||
| const command = args.trim().toLowerCase(); | ||
| if (command.length > 0 && command !== 'status') { | ||
| host.showError(AGENTS_USAGE); | ||
| return; | ||
| } | ||
|
|
||
| if (host.session === undefined) { | ||
| host.showError(NO_ACTIVE_SESSION_MESSAGE); | ||
| return; | ||
| } | ||
|
|
||
| let tasks: readonly BackgroundTaskInfo[]; | ||
| try { | ||
| tasks = await host.requireSession().listBackgroundTasks({ activeOnly: false }); | ||
| } catch (error) { | ||
| host.showError(`Failed to load subagents: ${formatErrorMessage(error)}`); | ||
| return; | ||
| } | ||
|
|
||
| const agents = tasks.filter((task): task is AgentBackgroundTaskInfo => task.kind === 'agent'); | ||
| const title = agents.length > 0 ? ` Agents (${agents.length}) ` : ' Agents '; | ||
| host.state.transcriptContainer.addChild( | ||
| new UsagePanelComponent(() => buildAgentStatusReportLines(agents), 'primary', title), | ||
| ); | ||
| host.state.ui.requestRender(); | ||
| } | ||
|
|
||
| export function buildAgentStatusReportLines(tasks: readonly AgentBackgroundTaskInfo[]): string[] { | ||
| if (tasks.length === 0) return ['No background subagents.']; | ||
|
|
||
| return tasks.flatMap((task) => { | ||
| const agentId = task.agentId ?? task.taskId; | ||
| const subagentType = task.subagentType ?? 'agent'; | ||
| const lines = [`${task.status} ${subagentType} ${agentId} ${task.description}`]; | ||
| if (task.status === 'failed' && task.agentId !== undefined) { | ||
| lines.push(` Resume: ask Kimi to call Agent(resume="${task.agentId}", prompt="...")`); | ||
| } | ||
| if (task.stopReason !== undefined && task.stopReason.length > 0) { | ||
| lines.push(` Reason: ${task.stopReason}`); | ||
| } | ||
| return lines; | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import type { AgentBackgroundTaskInfo, BackgroundTaskInfo } from '@moonshot-ai/kimi-code-sdk'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { UsagePanelComponent } from '#/tui/components/messages/usage-panel'; | ||
| import { NO_ACTIVE_SESSION_MESSAGE } from '#/tui/constant/kimi-tui'; | ||
| import { buildAgentStatusReportLines, handleAgentsCommand } from '#/tui/commands/agents'; | ||
| import type { SlashCommandHost } from '#/tui/commands/dispatch'; | ||
|
|
||
| function agentTask(overrides: Partial<AgentBackgroundTaskInfo>): AgentBackgroundTaskInfo { | ||
| return { | ||
| kind: 'agent', | ||
| taskId: 'agent_task_1', | ||
| description: 'Explore auth flow', | ||
| status: 'running', | ||
| detached: true, | ||
| startedAt: 1000, | ||
| endedAt: null, | ||
| agentId: 'agent_123', | ||
| subagentType: 'explore', | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function makeHost(tasks: readonly BackgroundTaskInfo[] | Error): SlashCommandHost { | ||
| const session = { | ||
| listBackgroundTasks: vi.fn(async () => { | ||
| if (tasks instanceof Error) throw tasks; | ||
| return tasks; | ||
| }), | ||
| }; | ||
| return { | ||
| session, | ||
| requireSession: () => session, | ||
| showError: vi.fn(), | ||
| state: { | ||
| transcriptContainer: { addChild: vi.fn() }, | ||
| ui: { requestRender: vi.fn() }, | ||
| }, | ||
| } as unknown as SlashCommandHost; | ||
| } | ||
|
|
||
| describe('buildAgentStatusReportLines', () => { | ||
| it('shows an empty state when no subagents exist', () => { | ||
| expect(buildAgentStatusReportLines([])).toEqual(['No background subagents.']); | ||
| }); | ||
|
|
||
| it('formats agent tasks with status, type, id, and description', () => { | ||
| expect( | ||
| buildAgentStatusReportLines([ | ||
| agentTask({ taskId: 'agent_task_1', agentId: 'agent_a', subagentType: 'explore' }), | ||
| agentTask({ | ||
| taskId: 'agent_task_2', | ||
| agentId: 'agent_b', | ||
| subagentType: 'coder', | ||
| status: 'completed', | ||
| endedAt: 2000, | ||
| description: 'Implement fix', | ||
| }), | ||
| ]), | ||
| ).toEqual([ | ||
| 'running explore agent_a Explore auth flow', | ||
| 'completed coder agent_b Implement fix', | ||
| ]); | ||
| }); | ||
|
|
||
| it('adds an Agent resume hint for failed agents with ids', () => { | ||
| expect( | ||
| buildAgentStatusReportLines([ | ||
| agentTask({ status: 'failed', stopReason: 'Tool failed', agentId: 'agent_failed' }), | ||
| ]), | ||
| ).toEqual([ | ||
| 'failed explore agent_failed Explore auth flow', | ||
| ' Resume: ask Kimi to call Agent(resume="agent_failed", prompt="...")', | ||
| ' Reason: Tool failed', | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleAgentsCommand', () => { | ||
| it('renders only agent background tasks', async () => { | ||
| const host = makeHost([ | ||
| agentTask({}), | ||
| { | ||
| kind: 'process', | ||
| taskId: 'bash_1', | ||
| description: 'pnpm test', | ||
| status: 'running', | ||
| detached: true, | ||
| startedAt: 1000, | ||
| endedAt: null, | ||
| command: 'pnpm test', | ||
| pid: 12345, | ||
| exitCode: null, | ||
| }, | ||
| ]); | ||
|
|
||
| await handleAgentsCommand(host, 'status'); | ||
|
|
||
| expect(host.requireSession().listBackgroundTasks).toHaveBeenCalledWith({ activeOnly: false }); | ||
| expect(host.state.transcriptContainer.addChild).toHaveBeenCalledTimes(1); | ||
| const component = vi.mocked(host.state.transcriptContainer.addChild).mock.calls[0]?.[0]; | ||
| expect(component).toBeInstanceOf(UsagePanelComponent); | ||
| const rendered = component?.render(120).join('\n') ?? ''; | ||
| expect(rendered).toContain('agent_123'); | ||
| expect(rendered).toContain('Explore auth flow'); | ||
| expect(rendered).not.toContain('bash_1'); | ||
| expect(rendered).not.toContain('pnpm test'); | ||
| expect(host.state.ui.requestRender).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('uses status as the default subcommand', async () => { | ||
| const host = makeHost([agentTask({})]); | ||
|
|
||
| await handleAgentsCommand(host, ''); | ||
|
|
||
| expect(host.requireSession().listBackgroundTasks).toHaveBeenCalledWith({ activeOnly: false }); | ||
| expect(host.state.transcriptContainer.addChild).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('shows the inactive session error without loading tasks', async () => { | ||
| const host = makeHost([]); | ||
| host.session = undefined; | ||
|
|
||
| await handleAgentsCommand(host, 'status'); | ||
|
|
||
| expect(host.showError).toHaveBeenCalledWith(NO_ACTIVE_SESSION_MESSAGE); | ||
| expect(host.requireSession().listBackgroundTasks).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects unsupported arguments', async () => { | ||
| const host = makeHost([]); | ||
|
|
||
| await handleAgentsCommand(host, 'cancel agent_123'); | ||
|
|
||
| expect(host.showError).toHaveBeenCalledWith('Usage: /agents [status]'); | ||
| }); | ||
|
|
||
| it('shows load errors', async () => { | ||
| const host = makeHost(new Error('boom')); | ||
|
|
||
| await handleAgentsCommand(host, ''); | ||
|
|
||
| expect(host.showError).toHaveBeenCalledWith('Failed to load subagents: boom'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the web UI, commands listed here are treated as known by
Composer.vueand emitted toApp.vue'shandleCommand, but that switch has no/agentscase; the default branch treats it as a skill and callsclient.activateSkill('agents'). So selecting or typing/agentsin the web slash menu will surface a skill-not-found warning instead of showing agent status. Add a web handler for/agentsor keep it out of the web command list until supported.Useful? React with 👍 / 👎.