diff --git a/src/main/acp/acp-dialects/kimi-dialect.test.ts b/src/main/acp/acp-dialects/kimi-dialect.test.ts new file mode 100644 index 00000000000..2286e718608 --- /dev/null +++ b/src/main/acp/acp-dialects/kimi-dialect.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, it } from 'vitest' +import { KIMI_ACP_DIALECT } from './kimi-dialect' + +const normalize = KIMI_ACP_DIALECT.normalizeToolUpdate! + +const text = (value: string) => ({ + type: 'content' as const, + content: { type: 'text' as const, text: value } +}) + +// Payloads recorded from Kimi Code 2.6.0 over `kimi acp`. +describe('KIMI_ACP_DIALECT.normalizeToolUpdate', () => { + it('reads a failed command as output and exit code', () => { + const output = 'hello-from-kimi\nCommand failed with exit code: 3.' + const update = normalize({ + toolCallId: '0:call_1', + status: 'failed', + content: [text(output)], + rawOutput: output + }) + expect(update.rawOutput).toEqual({ stdout: 'hello-from-kimi', exitCode: 3 }) + expect(update.content).toEqual([]) + }) + + it('reads a silent failed command as empty output', () => { + const output = 'Process exited with code 4\nCommand failed with exit code: 4.' + const update = normalize({ + toolCallId: '0:call_1', + status: 'failed', + content: [text(output)], + rawOutput: output + }) + expect(update.rawOutput).toEqual({ stdout: '', exitCode: 4 }) + }) + + it('reads a silent successful command as empty output', () => { + const output = 'Command executed successfully.' + const update = normalize({ + toolCallId: '0:call_1', + status: 'completed', + content: [text(output)], + rawOutput: output + }) + expect(update.rawOutput).toEqual({ stdout: '', exitCode: 0 }) + }) + + it('keeps successful output that ends like a failure', () => { + const output = 'log\nCommand failed with exit code: 3.' + const update = normalize({ + toolCallId: '0:call_1', + status: 'completed', + content: [text(output)], + rawOutput: output + }) + expect(update.rawOutput).toBe(output) + }) + + it('drops the streamed arguments a running call shows as text', () => { + const update = normalize({ + toolCallId: '0:call_1', + status: 'in_progress', + rawInput: { command: 'echo hi' }, + content: [text('{"command":"echo hi"}')] + }) + expect(update.content).toEqual([]) + }) +}) + +describe('KIMI_ACP_DIALECT.request', () => { + it('describes an approval with the action Kimi asks about', () => { + const presentation = KIMI_ACP_DIALECT.request!('session/request_permission', { + sessionId: 'session_1', + options: [{ optionId: 'approve_once', name: 'Approve once', kind: 'allow_once' }], + toolCall: { + toolCallId: '0:call_1', + title: 'Bash', + content: [text('Requesting approval to Running: rm -rf build')] + } + }) + expect(presentation?.body).toMatchObject({ + kind: 'approval', + title: 'Bash', + description: 'Running: rm -rf build' + }) + }) +}) diff --git a/src/main/acp/acp-dialects/kimi-dialect.ts b/src/main/acp/acp-dialects/kimi-dialect.ts new file mode 100644 index 00000000000..33d1ac353ae --- /dev/null +++ b/src/main/acp/acp-dialects/kimi-dialect.ts @@ -0,0 +1,79 @@ +import type { ToolCallUpdate } from '../generated/acp-protocol.generated' +import { readAcpPermissionRequest } from '../protocol/acp-permission-requests' +import { acpPermissionPresentation } from '../requests/acp-timeline-requests' +import type { AcpDialect, AcpRequestPresentation } from './acp-dialect' + +const SHELL_FAILURE = /^(?:([\s\S]*)\n)?Command failed with exit code: (-?\d+)\.$/u +const SHELL_SILENT_SUCCESS = 'Command executed successfully.' + +// Kimi's Bash result is one string: output, then a failure line on a non-zero exit. +function shellResult( + output: string, + status: ToolCallUpdate['status'] +): { stdout: string; exitCode: number } | null { + if (status === 'completed') { + return output === SHELL_SILENT_SUCCESS ? { stdout: '', exitCode: 0 } : null + } + const failure = status === 'failed' ? SHELL_FAILURE.exec(output) : null + if (!failure) { + return null + } + const exitCode = Number(failure[2]) + const stdout = failure[1] ?? '' + return { stdout: stdout === `Process exited with code ${exitCode}` ? '' : stdout, exitCode } +} + +function normalizeToolUpdate(update: ToolCallUpdate): ToolCallUpdate { + // A running call's text is its streamed arguments, not output. + if (update.status === 'pending' || update.status === 'in_progress') { + return update.content + ? { + ...update, + content: update.content.filter( + (block) => !(block.type === 'content' && block.content.type === 'text') + ) + } + : update + } + const output = update.rawOutput + const shell = typeof output === 'string' ? shellResult(output, update.status) : null + if (!shell) { + return update + } + return { + ...update, + rawOutput: shell, + content: update.content?.filter( + (block) => + !( + block.type === 'content' && + block.content.type === 'text' && + block.content.text === output + ) + ) + } +} + +const APPROVAL_ACTION = /^Requesting approval to (.+)$/su +const ACTION_MAX_CHARS = 512 + +// Approvals are titled with the bare tool name; the command or path is only in this action line. +function approvalRequest(method: string, params: unknown): AcpRequestPresentation | undefined { + const request = + method === 'session/request_permission' ? readAcpPermissionRequest(params, () => {}) : null + const action = request?.toolCall.content + ?.map((block) => + block.type === 'content' && block.content.type === 'text' + ? APPROVAL_ACTION.exec(block.content.text)?.[1]?.trim() + : undefined + ) + .find(Boolean) + return action ? acpPermissionPresentation(params, action.slice(0, ACTION_MAX_CHARS)) : undefined +} + +export const KIMI_ACP_DIALECT: AcpDialect = { + // Without forms, Kimi drops every question after the first and offers no free-text answer. + clientCapabilities: { elicitation: { form: {} } }, + normalizeToolUpdate, + request: approvalRequest +} diff --git a/src/main/acp/launch/acp-launch-registry.ts b/src/main/acp/launch/acp-launch-registry.ts index 7ec8e5ea045..21952773ec5 100644 --- a/src/main/acp/launch/acp-launch-registry.ts +++ b/src/main/acp/launch/acp-launch-registry.ts @@ -28,6 +28,7 @@ import { OMP_SESSION_HISTORY } from '../../omp/omp-session-history' import { OMP_TRANSCRIPT_IMPORT } from '../../omp/omp-transcript-import' import { ANTIGRAVITY_LAUNCH_SPEC } from '../../antigravity/antigravity-acp-launch-spec' import { COPILOT_LAUNCH_SPEC } from '../../copilot/copilot-acp-launch-spec' +import { KIMI_LAUNCH_SPEC } from '../../kimi/kimi-acp-launch-spec' import type { AcpLaunchSpec } from './acp-launch-contract' const GROK_LAUNCH_SPEC: AcpLaunchSpec = { @@ -156,7 +157,8 @@ export const ACP_LAUNCH_SPECS: readonly AcpLaunchSpec[] = [ OPENCODE2_LAUNCH_SPEC, OMP_LAUNCH_SPEC, ANTIGRAVITY_LAUNCH_SPEC, - COPILOT_LAUNCH_SPEC + COPILOT_LAUNCH_SPEC, + KIMI_LAUNCH_SPEC ] export function acpLaunchSpecFor(agent: string): AcpLaunchSpec | null { diff --git a/src/main/kimi/kimi-acp-launch-spec.ts b/src/main/kimi/kimi-acp-launch-spec.ts new file mode 100644 index 00000000000..085275fa76f --- /dev/null +++ b/src/main/kimi/kimi-acp-launch-spec.ts @@ -0,0 +1,46 @@ +import { join } from 'node:path' +import type { AcpLaunchSpec } from '../acp/launch/acp-launch-contract' +import { KIMI_ACP_DIALECT } from '../acp/acp-dialects/kimi-dialect' +import { directoryAccountBinding } from '../acp/launch/acp-account-binding' +import { agentSessionSetupFor } from '../../shared/agent-session/lifecycle/agent-session-setup-commands' +import { + resumeInvocationFromOptions, + type StructuredAgentSessionHistory +} from '../native-chat/structured-agent-cli-conversations' + +// Chats and terminals share Kimi's session store and `session_` ids. +const KIMI_SESSION_HISTORY: StructuredAgentSessionHistory = { + rowAgents: ['kimi'], + rowSessionId: (link) => link.handle.nativeId, + parseResumeArgs: (args) => + resumeInvocationFromOptions(args, { + targetless: ['-c', '--continue'], + markers: ['-S', '--session', '-r', '--resume'] + }) +} + +export const KIMI_LAUNCH_SPEC: AcpLaunchSpec = { + agent: 'kimi', + command: 'kimi', + args: () => ['acp'], + // A staged update would otherwise replace the binary when the chat starts. + env: { KIMI_CODE_NO_AUTO_UPDATE: '1' }, + dialect: KIMI_ACP_DIALECT, + loginCommand: agentSessionSetupFor('kimi')?.loginCommand ?? [], + account: directoryAccountBinding('KIMI_CODE_HOME', (homePath) => join(homePath, '.kimi-code')), + // The install scripts' default location. + installDirectories: ({ homePath }) => [join(homePath, '.kimi-code', 'bin')], + // 2.5.0 stopped failing initialize when the client's first request arrives during startup. + minimumVersions: ['2.5.0'], + // Kimi enforces its modes, and config.toml's default_permission_mode applies until one is set. + // Its ACP descriptions swap these: `yolo` still asks before risky commands, `auto` never asks. + permissionModeConfig: { + values: { ask: 'default', auto: 'yolo', 'full-access': 'auto' } + }, + // Session discovery deletes its listing session. + modelDiscovery: { kind: 'session', listingNamesConfiguredModel: false }, + compaction: true, + compactionInstructions: true, + transcriptImport: null, + sessionHistory: KIMI_SESSION_HISTORY +} diff --git a/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts b/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts index 65c8852b335..7c83f6b87b7 100644 --- a/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts +++ b/src/main/runtime/agent-session/naming/structured-chat-naming-runtime.ts @@ -181,8 +181,8 @@ export function structuredChatNamingDeps( }) const useChatAgent = !recipe.agentId && !recipe.agentArgs?.trim() const log = { scope: 'conversation-name', sessionId: record.sessionId } - // Copilot saves every run, so a naming run would sit in the user's Copilot history. - if (useChatAgent && record.provider === 'copilot') { + // These save every CLI run, so a naming run would sit in the user's own history. + if (useChatAgent && (record.provider === 'copilot' || record.provider === 'kimi')) { return null } if (useChatAgent && (record.provider === 'grok' || record.provider === 'antigravity')) { diff --git a/src/shared/agent-launch/permissions/agent-chat-permission-support.ts b/src/shared/agent-launch/permissions/agent-chat-permission-support.ts index 4c844332317..531aa1fa964 100644 --- a/src/shared/agent-launch/permissions/agent-chat-permission-support.ts +++ b/src/shared/agent-launch/permissions/agent-chat-permission-support.ts @@ -17,6 +17,11 @@ const ACP_CHAT_PERMISSION_MODES: AgentChatPermissionModeSupport = { modes: ['ask', 'full-access'] } +// Kimi's own modes: Ask When Needed approves routine work and still asks before risky commands. +const KIMI_CHAT_PERMISSION_MODES: AgentChatPermissionModeSupport = { + modes: ['ask', 'auto', 'full-access'] +} + // No Auto: the server has no reviewer that answers approvals for the person. const ANTIGRAVITY_CHAT_PERMISSION_MODES: AgentChatPermissionModeSupport = { modes: ['ask', 'accept-edits', 'full-access'] @@ -30,6 +35,7 @@ const AGENT_CHAT_PERMISSION_MODES_BY_AGENT = new Map