feat(agent): implement reasoning mode in engine and CLI

Wire reasoningMode through Agent runner, CLI output, and all CLI
entry points (run, interactive, non-interactive). In stream mode,
thinking content is printed to stderr in real-time. In on mode,
thinking is shown after message completion. Default is stream.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
yushen 2026-02-04 15:50:47 +08:00
parent 0340358a9b
commit 953a29672a
5 changed files with 71 additions and 5 deletions

View file

@ -1,6 +1,6 @@
import { Agent as PiAgentCore, type AgentEvent, type AgentMessage } from "@mariozechner/pi-agent-core";
import { v7 as uuidv7 } from "uuid";
import type { AgentOptions, AgentRunResult } from "./types.js";
import type { AgentOptions, AgentRunResult, ReasoningMode } from "./types.js";
import { createAgentOutput } from "./cli/output.js";
import { resolveModel, resolveTools } from "./tools.js";
import {
@ -70,6 +70,7 @@ export class Agent {
private readonly skillManager?: SkillManager;
private readonly contextWindowGuard: ContextWindowGuardResult;
private readonly debug: boolean;
private readonly reasoningMode: ReasoningMode;
private toolsOptions: AgentOptions;
private readonly originalToolsConfig?: ToolsConfig;
private readonly stderr: NodeJS.WritableStream;
@ -89,8 +90,9 @@ export class Agent {
constructor(options: AgentOptions = {}) {
const stdout = options.logger?.stdout ?? process.stdout;
this.stderr = options.logger?.stderr ?? process.stderr;
this.output = createAgentOutput({ stdout, stderr: this.stderr });
this.debug = options.debug ?? false;
this.reasoningMode = options.reasoningMode ?? "stream";
this.output = createAgentOutput({ stdout, stderr: this.stderr, reasoningMode: this.reasoningMode });
// Resolve provider and model from options > env vars > defaults
const defaultProvider = options.provider ?? credentialManager.getLlmProvider() ?? "kimi-coding";
@ -291,6 +293,7 @@ export class Agent {
provider: this.agent.state.model?.provider,
model: this.agent.state.model?.id,
thinkingLevel: this.agent.state.thinkingLevel,
reasoningMode: this.reasoningMode,
contextWindowTokens: this.contextWindowGuard.tokens,
});
@ -387,7 +390,10 @@ export class Agent {
markAuthProfileGood(this.resolvedProvider, this.currentProfileId);
}
return { text: this.output.state.lastAssistantText, error: this.agent.state.error };
const thinking = this.reasoningMode !== "off"
? this.output.state.lastAssistantThinking || undefined
: undefined;
return { text: this.output.state.lastAssistantText, thinking, error: this.agent.state.error };
}
/**