mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
feat(native-chat): run Kimi Code in Chat over its ACP server
Kimi chats launch `kimi acp` with Kimi's own sign-in and KIMI_CODE_HOME. Orca sets Kimi's own permission mode at every start: Ask is Kimi's default, Approve for me is its Ask When Needed mode, and Full access is its Never Ask mode. Without that, a default_permission_mode in Kimi's config.toml would apply while the session reports default. A Kimi dialect reads Bash results as output and exit code, drops the streamed arguments a running call shows as text, names the command or path on approval cards, and takes questions as forms so every question reaches the chat.
This commit is contained in:
@@ -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'
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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_<uuid>` 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
|
||||
}
|
||||
@@ -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')) {
|
||||
|
||||
@@ -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<string, AgentChatPermission
|
||||
['opencode2', ACP_CHAT_PERMISSION_MODES],
|
||||
['omp', ACP_CHAT_PERMISSION_MODES],
|
||||
['copilot', ACP_CHAT_PERMISSION_MODES],
|
||||
['kimi', KIMI_CHAT_PERMISSION_MODES],
|
||||
['antigravity', ANTIGRAVITY_CHAT_PERMISSION_MODES]
|
||||
])
|
||||
|
||||
|
||||
@@ -38,6 +38,12 @@ export const AGENT_SESSION_SETUP_COMMANDS = [
|
||||
remoteLoginCommand: ['copilot', 'login', '--device-code'],
|
||||
installCommand: 'curl -fsSL https://gh.io/copilot-install | bash'
|
||||
},
|
||||
// `kimi login` is already a device-code login.
|
||||
{
|
||||
agent: 'kimi',
|
||||
loginCommand: ['kimi', 'login'],
|
||||
installCommand: 'curl -fsSL https://code.kimi.com/kimi-code/install.sh | bash'
|
||||
},
|
||||
// An `agy` login does not sign its chats in: they keep their own sign-in.
|
||||
{ agent: 'antigravity', loginCommand: [] }
|
||||
] as const
|
||||
|
||||
Reference in New Issue
Block a user