/** * Grok Build CLI backend plugin. * 通过 `grok -p --output-format streaming-json` 驱动 agent。 * 首轮预分配 --session-id,方便 watchdog / 日志在进程结束前定位 session 目录。 */ import { CliAgentBackend, type BaseCliSession, type ParsedOutput } from "../agent/cli-base.js"; import { type AgentSessionActivity, type ExecHooks, type SessionConfig } from "../agent/types.js"; interface GrokSession extends BaseCliSession { sessionDir?: string; /** 首轮用 --session-id,后续用 --resume。 */ isNewSession: boolean; /** 首轮预分配的 NativeSessionId;EngineHandle 禁止放这里。 */ clientSessionId: string; /** events.jsonl 增量读取位置,供 /status 和 watchdog 刷新。 */ eventsOffset: number; /** 尚未结束的工具调用数量。 */ activeToolCount: number; /** 从 events.jsonl 整理出的最近日志,覆盖 stdout 碎片。 */ statusLines: string[]; /** 本轮已看到 turn_started 之后的 outcome=completed turn_ended。 */ turnEnded: boolean; /** 本轮 prompt 发出后,events 里已经出现过新的 turn_started。 */ turnStartedThisPrompt: boolean; /** buildInput 时刻。这一刻之前的 turn 事件一律不算本轮。 */ promptStartedAt: number; /** 发 prompt 时 chat_history.jsonl 的大小;只认这之后新写的回答。 */ historyOffset: number; } export default class GrokBackend extends CliAgentBackend { constructor(); command(): string; needsStableUserPrefix(): boolean; buildSession(config: SessionConfig): GrokSession; buildInput(session: GrokSession, message: string): { args: string[]; stdin?: string; }; protected getExecHooks(session: GrokSession): ExecHooks; protected argsForLog(args: string[]): string[]; protected isProbeError(err: any): boolean; protected transientRetryLimit(): number; protected isTransientCliError(err: unknown): boolean; parseOutput(stdout: string, session: GrokSession): ParsedOutput; private beginPromptTurn; private isTurnReadyToSend; private currentTurnAssistant; private fileSize; private readFileSince; protected loadSessionTranscript(session: GrokSession): Promise<{ sources: { path: string; role: string; }[]; backend: string; agentSessionId: string; events: Iterable | AsyncIterable; }>; protected refreshActivity(sessionId: string, activity: AgentSessionActivity): void; protected probeSessionFileMtime(session: GrokSession): number | null; protected probeSessionLastLine(session: GrokSession): string | null; private consumeNewEvents; private readHistoryStatusLines; private readLastLines; private getChatHistoryPath; private getEventsPath; private getSessionDir; private sessionExists; private readSignals; private refreshCompactCount; } /** 数当前会话目录下 compaction/segment_*.md。 */ export declare function countGrokCompactionSegments(sessionDir: string | null): number; /** 与 Grok CLI 的 session 目录命名一致:realpath(cwd) 再 encodeURIComponent。 */ export declare function encodeGrokSessionDir(cwd: string): string; /** 进行中的 Grok session:磁盘上还在写的 chat_history.jsonl。 */ export declare function locateGrokChatHistory(cwd: string, nativeSessionId: string): string | undefined; /** 取当前轮最后一条应发给用户的 assistant 文本。遇 user 行即停,避免跨轮。 */ export declare function extractLastGrokAssistantText(raw: string): string | undefined; /** 当前轮最后一条 assistant 的正文和 model_id。遇 user 行即停,避免跨轮。 */ export declare function extractLastGrokAssistant(raw: string): { text?: string; model?: string; }; export {};