feat(kimi): Kimi Code sessions in AI Vault + agent status hooks
Adds Kimi Code session parsing for AI Vault and managed Kimi agent status hooks.
This commit is contained in:
@@ -419,6 +419,67 @@ describe('shared agent-hook-listener', () => {
|
||||
expect(ended?.payload).toMatchObject({ agentType: 'devin', state: 'done' })
|
||||
})
|
||||
|
||||
it('normalizes Kimi Code Claude-compatible lifecycle events as kimi status', () => {
|
||||
const submitted = normalizeHookPayload(
|
||||
state,
|
||||
'kimi',
|
||||
{
|
||||
paneKey: PANE_KEY,
|
||||
payload: {
|
||||
hook_event_name: 'UserPromptSubmit',
|
||||
session_id: 'session_abc',
|
||||
cwd: '/repo',
|
||||
// Kimi sends the prompt as a content-block array, not a bare string.
|
||||
prompt: [{ type: 'text', text: 'list the files here' }]
|
||||
}
|
||||
},
|
||||
'production'
|
||||
)
|
||||
const tool = normalizeHookPayload(
|
||||
state,
|
||||
'kimi',
|
||||
{
|
||||
paneKey: PANE_KEY,
|
||||
payload: {
|
||||
hook_event_name: 'PreToolUse',
|
||||
session_id: 'session_abc',
|
||||
tool_name: 'Bash',
|
||||
tool_input: { command: 'ls' }
|
||||
}
|
||||
},
|
||||
'production'
|
||||
)
|
||||
const waiting = normalizeHookPayload(
|
||||
state,
|
||||
'kimi',
|
||||
{
|
||||
paneKey: PANE_KEY,
|
||||
payload: { hook_event_name: 'PermissionRequest', session_id: 'session_abc' }
|
||||
},
|
||||
'production'
|
||||
)
|
||||
const stopped = normalizeHookPayload(
|
||||
state,
|
||||
'kimi',
|
||||
{
|
||||
paneKey: PANE_KEY,
|
||||
payload: { hook_event_name: 'Stop', session_id: 'session_abc' }
|
||||
},
|
||||
'production'
|
||||
)
|
||||
|
||||
expect(submitted?.payload).toMatchObject({
|
||||
agentType: 'kimi',
|
||||
state: 'working',
|
||||
prompt: 'list the files here'
|
||||
})
|
||||
expect(tool?.payload).toMatchObject({ agentType: 'kimi', state: 'working', toolName: 'Bash' })
|
||||
expect(waiting?.payload).toMatchObject({ agentType: 'kimi', state: 'waiting' })
|
||||
expect(stopped?.payload).toMatchObject({ agentType: 'kimi', state: 'done' })
|
||||
// The Claude-shaped session_id is captured for provider-session resume.
|
||||
expect(stopped?.providerSession).toMatchObject({ key: 'session_id', id: 'session_abc' })
|
||||
})
|
||||
|
||||
it('rejects oversized paneKey', () => {
|
||||
const event = normalizeHookPayload(
|
||||
state,
|
||||
|
||||
@@ -311,6 +311,26 @@ type ExtractedPromptText = {
|
||||
| null
|
||||
}
|
||||
|
||||
// Joins the `text` of an Anthropic-style content-block array ([{ type: 'text',
|
||||
// text }, ...]); plain string items are included too. Returns '' when nothing
|
||||
// textual is present so callers can fall through to the next prompt source.
|
||||
function contentBlockArrayText(value: unknown[]): string {
|
||||
const parts: string[] = []
|
||||
for (const item of value) {
|
||||
if (typeof item === 'string') {
|
||||
parts.push(item)
|
||||
continue
|
||||
}
|
||||
if (item && typeof item === 'object') {
|
||||
const text = (item as Record<string, unknown>).text
|
||||
if (typeof text === 'string') {
|
||||
parts.push(text)
|
||||
}
|
||||
}
|
||||
}
|
||||
return parts.join(' ').replace(/\s+/g, ' ').trim()
|
||||
}
|
||||
|
||||
function extractPromptText(hookPayload: Record<string, unknown>): ExtractedPromptText {
|
||||
const candidateKeys = [
|
||||
'prompt',
|
||||
@@ -328,6 +348,16 @@ function extractPromptText(hookPayload: Record<string, unknown>): ExtractedPromp
|
||||
// surrounding whitespace would otherwise leak into UI and caches.
|
||||
return { text: value.trim(), source: key as Exclude<ExtractedPromptText['source'], null> }
|
||||
}
|
||||
// Why: Kimi Code sends UserPromptSubmit `prompt` as a content-block array
|
||||
// ([{ type: 'text', text }]) rather than a string. Extract its text for the
|
||||
// genuine prompt keys. `message` stays string-only: it is the ambiguous
|
||||
// status/permission field that hasExplicitUserPrompt intentionally distrusts.
|
||||
if (key !== 'message' && Array.isArray(value)) {
|
||||
const text = contentBlockArrayText(value)
|
||||
if (text.length > 0) {
|
||||
return { text, source: key as Exclude<ExtractedPromptText['source'], null> }
|
||||
}
|
||||
}
|
||||
}
|
||||
// Why: OpenCode's plugin sends MessagePart events with { role, text }. When
|
||||
// role === 'user', the text *is* the prompt — surface it even though
|
||||
@@ -1804,6 +1834,9 @@ function isNewTurnEvent(source: AgentHookSource, eventName: unknown): boolean {
|
||||
// typecheck here instead of silently falling through to `false`.
|
||||
switch (source) {
|
||||
case 'claude':
|
||||
// Why: Kimi Code emits Claude-compatible hook events, so UserPromptSubmit
|
||||
// is its new-turn boundary too.
|
||||
case 'kimi':
|
||||
return eventName === 'UserPromptSubmit'
|
||||
case 'codex':
|
||||
return eventName === 'SessionStart' || eventName === 'UserPromptSubmit'
|
||||
@@ -1895,6 +1928,8 @@ function extractToolFields(
|
||||
// typecheck here instead of silently routing through OpenCode's extractor.
|
||||
switch (source) {
|
||||
case 'claude':
|
||||
// Why: Kimi Code uses Claude's tool_name/tool_input payload fields verbatim.
|
||||
case 'kimi':
|
||||
return extractClaudeToolFields(eventName, hookPayload)
|
||||
case 'codex':
|
||||
return extractCodexToolFields(eventName, hookPayload)
|
||||
@@ -2034,6 +2069,58 @@ function normalizeDevinEvent(
|
||||
)
|
||||
}
|
||||
|
||||
// Why: Kimi Code emits Claude-compatible hook payloads and reuses Claude's
|
||||
// lifecycle event names (UserPromptSubmit/PreToolUse/Stop/...). Normalize them
|
||||
// into Orca's shared status states while attributing the status to Kimi so the
|
||||
// sidebar shows the Kimi icon and label instead of falling back to Claude.
|
||||
function normalizeKimiEvent(
|
||||
state: HookListenerState,
|
||||
eventName: unknown,
|
||||
promptText: string,
|
||||
paneKey: string,
|
||||
hookPayload: Record<string, unknown>
|
||||
): ParsedAgentStatusPayload | null {
|
||||
const stateName =
|
||||
eventName === 'UserPromptSubmit' ||
|
||||
eventName === 'PreToolUse' ||
|
||||
eventName === 'PostToolUse' ||
|
||||
eventName === 'PostToolUseFailure'
|
||||
? 'working'
|
||||
: eventName === 'PermissionRequest'
|
||||
? 'waiting'
|
||||
: eventName === 'Stop' || eventName === 'StopFailure'
|
||||
? 'done'
|
||||
: null
|
||||
|
||||
if (!stateName) {
|
||||
return null
|
||||
}
|
||||
|
||||
const snapshot = resolveToolState(
|
||||
state,
|
||||
paneKey,
|
||||
extractToolFields('kimi', eventName, hookPayload),
|
||||
{ resetOnNewTurn: isNewTurnEvent('kimi', eventName) }
|
||||
)
|
||||
|
||||
const interrupted =
|
||||
eventName === 'Stop' && hookPayload['is_interrupt'] === true ? true : undefined
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('kimi', eventName)
|
||||
}),
|
||||
agentType: 'kimi',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
function normalizeGeminiEvent(
|
||||
state: HookListenerState,
|
||||
eventName: unknown,
|
||||
@@ -2974,6 +3061,9 @@ export function normalizeHookPayload(
|
||||
case 'devin':
|
||||
payload = normalizeDevinEvent(state, eventName, promptText, paneKey, hookPayloadRecord)
|
||||
break
|
||||
case 'kimi':
|
||||
payload = normalizeKimiEvent(state, eventName, promptText, paneKey, hookPayloadRecord)
|
||||
break
|
||||
}
|
||||
|
||||
// Why: connectionId stays null at the listener layer. The local server keeps
|
||||
@@ -3027,7 +3117,8 @@ export const HOOK_SOURCE_BY_PATHNAME: Readonly<Record<string, AgentHookSource>>
|
||||
'/hook/grok': 'grok',
|
||||
'/hook/copilot': 'copilot',
|
||||
'/hook/hermes': 'hermes',
|
||||
'/hook/devin': 'devin'
|
||||
'/hook/devin': 'devin',
|
||||
'/hook/kimi': 'kimi'
|
||||
})
|
||||
|
||||
export function resolveHookSource(pathname: string): AgentHookSource | null {
|
||||
|
||||
@@ -47,6 +47,7 @@ export type AgentHookSource =
|
||||
| 'copilot'
|
||||
| 'hermes'
|
||||
| 'devin'
|
||||
| 'kimi'
|
||||
|
||||
/** Env marker used by the remote relay. It is a transport/location marker, not
|
||||
* a dev-vs-prod build tag, so main-process env mismatch diagnostics ignore it. */
|
||||
|
||||
@@ -16,7 +16,8 @@ export const AGENT_HOOK_TARGETS = [
|
||||
'grok',
|
||||
'copilot',
|
||||
'hermes',
|
||||
'devin'
|
||||
'devin',
|
||||
'kimi'
|
||||
] as const
|
||||
export type AgentHookTarget = (typeof AGENT_HOOK_TARGETS)[number]
|
||||
|
||||
|
||||
@@ -107,7 +107,9 @@ export function extractAgentProviderSession(
|
||||
case 'claude':
|
||||
case 'codex':
|
||||
case 'gemini':
|
||||
case 'droid': {
|
||||
case 'droid':
|
||||
// Why: Kimi Code posts a Claude-shaped `session_id` (e.g. session_<uuid>).
|
||||
case 'kimi': {
|
||||
const id = readSessionId(payload, ['session_id'])
|
||||
return id ? { key: 'session_id', id } : null
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ export const AI_VAULT_AGENTS = [
|
||||
'grok',
|
||||
'openclaw',
|
||||
'devin',
|
||||
'droid'
|
||||
'droid',
|
||||
'kimi'
|
||||
] as const satisfies readonly TuiAgent[]
|
||||
|
||||
export type AiVaultAgent = (typeof AI_VAULT_AGENTS)[number]
|
||||
@@ -35,7 +36,8 @@ export const AI_VAULT_AGENT_LABELS = {
|
||||
grok: 'Grok',
|
||||
openclaw: 'OpenClaw',
|
||||
devin: 'Devin',
|
||||
droid: 'Droid'
|
||||
droid: 'Droid',
|
||||
kimi: 'Kimi'
|
||||
} as const satisfies Record<AiVaultAgent, string>
|
||||
|
||||
export type AiVaultSessionPreviewMessage = {
|
||||
@@ -138,6 +140,10 @@ function buildAgentResumeInvocation(
|
||||
return `${baseCommand} rovodev run --restore ${sessionArg}`
|
||||
case 'opencode':
|
||||
case 'pi':
|
||||
// Why: Kimi Code resumes with `kimi --session <id>` (alias `-S`). Sessions
|
||||
// are work-dir-scoped, so the cwd prefix from buildAiVaultResumeCommand is
|
||||
// required — resuming from another directory is rejected by the CLI.
|
||||
case 'kimi':
|
||||
return `${baseCommand} --session ${sessionArg}`
|
||||
case 'copilot':
|
||||
return `${baseCommand} --resume=${sessionArg}`
|
||||
|
||||
Reference in New Issue
Block a user