2026-02-08 16:25:01 +09:00
|
|
|
import pc from "picocolors"
|
|
|
|
|
import type {
|
|
|
|
|
RunContext,
|
|
|
|
|
EventPayload,
|
|
|
|
|
SessionIdleProps,
|
|
|
|
|
SessionStatusProps,
|
|
|
|
|
SessionErrorProps,
|
|
|
|
|
MessageUpdatedProps,
|
|
|
|
|
MessagePartUpdatedProps,
|
|
|
|
|
ToolExecuteProps,
|
|
|
|
|
ToolResultProps,
|
2026-02-17 02:34:35 +09:00
|
|
|
TuiToastShowProps,
|
2026-02-08 16:25:01 +09:00
|
|
|
} from "./types"
|
|
|
|
|
import type { EventState } from "./event-state"
|
|
|
|
|
import { serializeError } from "./event-formatting"
|
|
|
|
|
|
2026-02-17 02:34:35 +09:00
|
|
|
function getSessionId(props?: { sessionID?: string; sessionId?: string }): string | undefined {
|
|
|
|
|
return props?.sessionID ?? props?.sessionId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInfoSessionId(props?: {
|
|
|
|
|
info?: { sessionID?: string; sessionId?: string }
|
|
|
|
|
}): string | undefined {
|
|
|
|
|
return props?.info?.sessionID ?? props?.info?.sessionId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPartSessionId(props?: {
|
|
|
|
|
part?: { sessionID?: string; sessionId?: string }
|
|
|
|
|
}): string | undefined {
|
|
|
|
|
return props?.part?.sessionID ?? props?.part?.sessionId
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 16:25:01 +09:00
|
|
|
export function handleSessionIdle(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "session.idle") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as SessionIdleProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
if (getSessionId(props) === ctx.sessionID) {
|
2026-02-08 16:25:01 +09:00
|
|
|
state.mainSessionIdle = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handleSessionStatus(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "session.status") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as SessionStatusProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
if (getSessionId(props) !== ctx.sessionID) return
|
2026-02-09 21:12:11 +09:00
|
|
|
|
|
|
|
|
if (props?.status?.type === "busy") {
|
2026-02-08 16:25:01 +09:00
|
|
|
state.mainSessionIdle = false
|
2026-02-09 21:12:11 +09:00
|
|
|
} else if (props?.status?.type === "idle") {
|
|
|
|
|
state.mainSessionIdle = true
|
2026-02-11 00:44:53 +09:00
|
|
|
} else if (props?.status?.type === "retry") {
|
|
|
|
|
state.mainSessionIdle = false
|
2026-02-08 16:25:01 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handleSessionError(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "session.error") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as SessionErrorProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
if (getSessionId(props) === ctx.sessionID) {
|
2026-02-08 16:25:01 +09:00
|
|
|
state.mainSessionError = true
|
|
|
|
|
state.lastError = serializeError(props?.error)
|
|
|
|
|
console.error(pc.red(`\n[session.error] ${state.lastError}`))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handleMessagePartUpdated(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "message.part.updated") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as MessagePartUpdatedProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
// Current OpenCode puts sessionID inside part; legacy puts it in info
|
|
|
|
|
const partSid = getPartSessionId(props)
|
|
|
|
|
const infoSid = getInfoSessionId(props)
|
|
|
|
|
if ((partSid ?? infoSid) !== ctx.sessionID) return
|
2026-02-08 16:25:01 +09:00
|
|
|
|
2026-02-17 02:34:35 +09:00
|
|
|
const part = props?.part
|
2026-02-08 16:25:01 +09:00
|
|
|
if (!part) return
|
|
|
|
|
|
|
|
|
|
if (part.type === "text" && part.text) {
|
|
|
|
|
const newText = part.text.slice(state.lastPartText.length)
|
|
|
|
|
if (newText) {
|
|
|
|
|
process.stdout.write(newText)
|
|
|
|
|
state.hasReceivedMeaningfulWork = true
|
|
|
|
|
}
|
|
|
|
|
state.lastPartText = part.text
|
|
|
|
|
}
|
2026-02-17 02:34:35 +09:00
|
|
|
|
|
|
|
|
if (part.type === "tool") {
|
|
|
|
|
handleToolPart(ctx, part, state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleToolPart(
|
|
|
|
|
_ctx: RunContext,
|
|
|
|
|
part: NonNullable<MessagePartUpdatedProps["part"]>,
|
|
|
|
|
state: EventState,
|
|
|
|
|
): void {
|
|
|
|
|
const toolName = part.tool || part.name || "unknown"
|
|
|
|
|
const status = part.state?.status
|
|
|
|
|
|
|
|
|
|
if (status === "running") {
|
|
|
|
|
state.currentTool = toolName
|
|
|
|
|
let inputPreview = ""
|
|
|
|
|
const input = part.state?.input
|
|
|
|
|
if (input) {
|
|
|
|
|
if (input.command) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.command).slice(0, 60))}`
|
|
|
|
|
} else if (input.pattern) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.pattern).slice(0, 40))}`
|
|
|
|
|
} else if (input.filePath) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.filePath))}`
|
|
|
|
|
} else if (input.query) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.query).slice(0, 40))}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
state.hasReceivedMeaningfulWork = true
|
|
|
|
|
process.stdout.write(`\n${pc.cyan(">")} ${pc.bold(toolName)}${inputPreview}\n`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status === "completed" || status === "error") {
|
|
|
|
|
const output = part.state?.output || ""
|
|
|
|
|
const maxLen = 200
|
|
|
|
|
const preview = output.length > maxLen ? output.slice(0, maxLen) + "..." : output
|
|
|
|
|
if (preview.trim()) {
|
|
|
|
|
const lines = preview.split("\n").slice(0, 3)
|
|
|
|
|
process.stdout.write(pc.dim(` └─ ${lines.join("\n ")}\n`))
|
|
|
|
|
}
|
|
|
|
|
state.currentTool = null
|
|
|
|
|
state.lastPartText = ""
|
|
|
|
|
}
|
2026-02-08 16:25:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handleMessageUpdated(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "message.updated") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as MessageUpdatedProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
if (getInfoSessionId(props) !== ctx.sessionID) return
|
2026-02-08 16:25:01 +09:00
|
|
|
if (props?.info?.role !== "assistant") return
|
|
|
|
|
|
|
|
|
|
state.hasReceivedMeaningfulWork = true
|
|
|
|
|
state.messageCount++
|
2026-02-09 11:01:38 +09:00
|
|
|
state.lastPartText = ""
|
2026-02-17 16:11:34 +09:00
|
|
|
|
|
|
|
|
const agent = props?.info?.agent ?? null
|
|
|
|
|
const model = props?.info?.modelID ?? null
|
|
|
|
|
if (agent !== state.currentAgent || model !== state.currentModel) {
|
|
|
|
|
state.currentAgent = agent
|
|
|
|
|
state.currentModel = model
|
|
|
|
|
printAgentHeader(agent, model)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function printAgentHeader(agent: string | null, model: string | null): void {
|
|
|
|
|
if (!agent && !model) return
|
|
|
|
|
const agentLabel = agent ? pc.bold(pc.magenta(agent)) : ""
|
|
|
|
|
const modelLabel = model ? pc.dim(model) : ""
|
|
|
|
|
const separator = agent && model ? " " : ""
|
|
|
|
|
process.stdout.write(`\n${agentLabel}${separator}${modelLabel}\n`)
|
2026-02-08 16:25:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handleToolExecute(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "tool.execute") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as ToolExecuteProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
if (getSessionId(props) !== ctx.sessionID) return
|
2026-02-08 16:25:01 +09:00
|
|
|
|
|
|
|
|
const toolName = props?.name || "unknown"
|
|
|
|
|
state.currentTool = toolName
|
|
|
|
|
|
|
|
|
|
let inputPreview = ""
|
|
|
|
|
if (props?.input) {
|
|
|
|
|
const input = props.input
|
|
|
|
|
if (input.command) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.command).slice(0, 60))}`
|
|
|
|
|
} else if (input.pattern) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.pattern).slice(0, 40))}`
|
|
|
|
|
} else if (input.filePath) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.filePath))}`
|
|
|
|
|
} else if (input.query) {
|
|
|
|
|
inputPreview = ` ${pc.dim(String(input.query).slice(0, 40))}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.hasReceivedMeaningfulWork = true
|
|
|
|
|
process.stdout.write(`\n${pc.cyan(">")} ${pc.bold(toolName)}${inputPreview}\n`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handleToolResult(ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "tool.result") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as ToolResultProps | undefined
|
2026-02-17 02:34:35 +09:00
|
|
|
if (getSessionId(props) !== ctx.sessionID) return
|
2026-02-08 16:25:01 +09:00
|
|
|
|
|
|
|
|
const output = props?.output || ""
|
|
|
|
|
const maxLen = 200
|
|
|
|
|
const preview = output.length > maxLen ? output.slice(0, maxLen) + "..." : output
|
|
|
|
|
|
|
|
|
|
if (preview.trim()) {
|
|
|
|
|
const lines = preview.split("\n").slice(0, 3)
|
|
|
|
|
process.stdout.write(pc.dim(` └─ ${lines.join("\n ")}\n`))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.currentTool = null
|
|
|
|
|
state.lastPartText = ""
|
|
|
|
|
}
|
2026-02-17 02:34:35 +09:00
|
|
|
|
|
|
|
|
export function handleTuiToast(_ctx: RunContext, payload: EventPayload, state: EventState): void {
|
|
|
|
|
if (payload.type !== "tui.toast.show") return
|
|
|
|
|
|
|
|
|
|
const props = payload.properties as TuiToastShowProps | undefined
|
|
|
|
|
const variant = props?.variant ?? "info"
|
|
|
|
|
|
|
|
|
|
if (variant === "error") {
|
2026-02-17 16:11:34 +09:00
|
|
|
const title = props?.title ? `${props.title}: ` : ""
|
|
|
|
|
const message = props?.message?.trim()
|
|
|
|
|
if (message) {
|
|
|
|
|
state.mainSessionError = true
|
|
|
|
|
state.lastError = `${title}${message}`
|
|
|
|
|
}
|
2026-02-17 02:34:35 +09:00
|
|
|
}
|
|
|
|
|
}
|