Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): redacted thinking 不再显示为空 thinking 卡片
zqchris17c0c378a469
diff --git a/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts b/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.tsindex eb43d41d85..b437498060 100644--- a/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts+++ b/apps/desktop/src/main/__tests__/messagePersistBroadcaster.test.ts@@ -497,6 +497,38 @@ describe('thinking persistence', () => {broadcastGuard(),);});++ it('persists redacted thinking as a structured hidden row', async () => {+ const finishedAt = Date.parse('2026-06-20T09:11:00.000Z');+ const nowSpy = vi.spyOn(Date, 'now').mockReturnValue(finishedAt);+ try {+ onThinkingEvent(+ SESSION,+ { stage: 'redacted', blockId: 'thinking-redacted' },+ null,+ );+ await flushWrites();+ } finally {+ nowSpy.mockRestore();+ }++ expect(createMessage).toHaveBeenCalledWith(+ SESSION,+ expect.objectContaining({+ clientId: 'thinking-redacted',+ role: 'thinking',+ content: {+ kind: 'thinking',+ text: '',+ durationMs: 0,+ isRedacted: true,+ finishedAt,+ },+ createdAt: finishedAt,+ }),+ broadcastGuard(),+ );+ });});describe('event timestamp persistence', () => {diff --git a/apps/desktop/src/renderer/__tests__/emptyThinkingPlaceholder.test.ts b/apps/desktop/src/renderer/__tests__/emptyThinkingPlaceholder.test.tsindex c7b0a11594..2f78d7ca02 100644--- a/apps/desktop/src/renderer/__tests__/emptyThinkingPlaceholder.test.ts+++ b/apps/desktop/src/renderer/__tests__/emptyThinkingPlaceholder.test.ts@@ -24,6 +24,7 @@ const SESSION_ID = 's1';function thinkingRow(clientId: string,content: Record<string, unknown>,+ agentKind: Message['agentKind'] = null,): Message {return {id: `row-${clientId}`,@@ -31,6 +32,7 @@ function thinkingRow(sessionId: SESSION_ID,role: 'thinking',content,+ agentKind,createdAt: '2026-07-02T00:00:00.000Z',} as unknown as Message;}@@ -51,12 +53,24 @@ describe('isNonAnchorHistoryRow — 历史初始页 backfill 判定', () => {// 一轮搜索密集、产出可见正文前就失败的会话,最新 50 行可能全是加密推理:// 映射结果为空 → MessageStream 不自动翻页 → 更老的消息再也拉不回来。expect(isNonAnchor(thinkingRow('t-red', { kind: 'thinking', text: '', durationMs: 0, isRedacted: true }))).toBe(true);+ expect(isNonAnchor(thinkingRow('t-pi-legacy', {+ kind: 'thinking',+ text: '[Reasoning redacted]',+ durationMs: 1,+ isRedacted: false,+ }, 'pi'))).toBe(true);expect(isNonAnchor(thinkingRow('t-empty', { kind: 'thinking', text: '', durationMs: 0, isRedacted: false }))).toBe(true);});it('有明文或有时长的 thinking 行是可见锚点,不触发补页', () => {expect(isNonAnchor(thinkingRow('t-text', { kind: 'thinking', text: 'real reasoning', durationMs: 0, isRedacted: false }))).toBe(false);expect(isNonAnchor(thinkingRow('t-dur', { kind: 'thinking', text: '', durationMs: 900, isRedacted: false }))).toBe(false);+ expect(isNonAnchor(thinkingRow('t-non-pi-marker', {+ kind: 'thinking',+ text: '[Reasoning redacted]',+ durationMs: 1,+ isRedacted: false,+ }, 'codex'))).toBe(false);});it('tool_result 仍算无锚点(orphan 会被丢弃),普通消息不算', () => {@@ -141,6 +155,28 @@ describe('handleStreamEvent — omitted thinking placeholder (live)', () => {expect(next.messages).toEqual(EMPTY_SESSION_STATE.messages);});+ it('removes an in-flight thinking placeholder when pi resolves it as redacted', () => {+ const started = handleStreamEvent(EMPTY_SESSION_STATE, {+ sessionId: SESSION_ID,+ type: 'thinking',+ data: { stage: 'start', blockId: 'pi-redacted', startedAt: 123 },+ });+ expect(started.messages).toEqual([+ expect.objectContaining({+ clientId: 'pi-redacted',+ role: 'thinking',+ isStreaming: true,+ }),+ ]);++ const redacted = handleStreamEvent(started, {+ sessionId: SESSION_ID,+ type: 'thinking',+ data: { stage: 'redacted', blockId: 'pi-redacted' },+ });+ expect(redacted.messages).toEqual([]);+ });+it('redacted 事件带 agentMeta 时仍刷新 lastAgentMeta(不展示 ≠ 丢事件)', () => {const next = handleStreamEvent(EMPTY_SESSION_STATE, {sessionId: SESSION_ID,@@ -155,12 +191,24 @@ describe('handleStreamEvent — omitted thinking placeholder (live)', () => {});describe('mapServerMessages — omitted thinking placeholder (restore)', () => {- it('filters empty+zero-duration rows 与 redacted 行,keeps content / duration 行', () => {+ it('filters empty/redacted/legacy-pi rows, keeps content / duration rows', () => {const mapped = makerChatStore.__mapServerMessagesForTest([thinkingRow('t-empty', { kind: 'thinking', text: '', durationMs: 0, isRedacted: false }),thinkingRow('t-text', { kind: 'thinking', text: 'real reasoning', durationMs: 0, isRedacted: false }),thinkingRow('t-dur', { kind: 'thinking', text: '', durationMs: 900, isRedacted: false }),thinkingRow('t-red', { kind: 'thinking', text: '', durationMs: 0, isRedacted: true }),+ thinkingRow('t-pi-legacy', {+ kind: 'thinking',+ text: '[Reasoning redacted]',+ durationMs: 1,+ isRedacted: false,+ }, 'pi'),+ thinkingRow('t-non-pi-marker', {+ kind: 'thinking',+ text: '[Reasoning redacted]',+ durationMs: 1,+ isRedacted: false,+ }, 'codex'),]);const ids = mapped.map((m) => m.clientId);expect(ids).not.toContain('t-empty');@@ -168,6 +216,8 @@ describe('mapServerMessages — omitted thinking placeholder (restore)', () => {expect(ids).toContain('t-dur');// 历史里的加密推理行同样不复原(与 live 路径同判定);DB 行本身保留不动。expect(ids).not.toContain('t-red');+ expect(ids).not.toContain('t-pi-legacy');+ expect(ids).toContain('t-non-pi-marker');});it('legacy rows without text/durationMs fields are treated as placeholders', () => {diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.tsindex 96464c041b..1a57088080 100644--- a/apps/desktop/src/renderer/lib/makerChatStore.ts+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts@@ -2116,6 +2116,11 @@ export function isOmittedThinkingPlaceholder(text: string, durationMs: number):return text === '' && durationMs === 0;}+/** pi 旧版 translator 把结构化 redacted 块误存成了这条可见占位文本。 */+function isLegacyRedactedThinkingPlaceholder(m: Message, text: string): boolean {+ return m.agentKind === 'pi' && text.trim() === '[Reasoning redacted]';+}+// F1-a: 所有 agent 消息(assistant/tool_use/tool_result/thinking/ask_user/plan_review)// 的落库已收口 main(messagePersistBroadcaster),handleStreamEvent 退化为纯 UI reducer、// 不再写库 → 不再需要 sessionId 形参(已从签名移除,各调用点同步去掉第三个实参)。@@ -2351,7 +2356,9 @@ export function handleStreamEvent(};}- // stage === 'redacted' —— 加密推理不进渲染列表。+ // stage === 'redacted' —— 加密推理不进渲染列表。部分 vendor(如 pi)会先发+ // thinking start、到 end 才能从 partial block 确认 redacted;因此还要删掉同+ // blockId 的瞬时占位,不能只阻止新增。//// 这类块没有任何明文可读,卡片只能显示"无法显示的思考过程";上游(如 Grok 开了// 服务端搜索)一轮能产出十几条,会把真实产出淹掉。落库仍由 main(onThinkingEvent)@@ -2361,7 +2368,19 @@ export function handleStreamEvent(// 不展示 ≠ 丢事件:仍按本函数开头的不变量刷新 lastAgentMeta(带 agentMeta 的事件都要刷,// mid-turn 抢救 assistant 累积流时拿它当 fallback)。否则这条事件携带的 model /// parentUuid 会被静默吞掉。- return incomingMeta ? { ...state, lastAgentMeta: incomingMeta } : state;+ const hasLivePlaceholder = state.messages.some(+ (m) => m.clientId === data.blockId && m.role === 'thinking',+ );+ if (!hasLivePlaceholder) {+ return incomingMeta ? { ...state, lastAgentMeta: incomingMeta } : state;+ }+ return {+ ...state,+ messages: state.messages.filter(+ (m) => !(m.clientId === data.blockId && m.role === 'thinking'),+ ),+ ...(incomingMeta ? { lastAgentMeta: incomingMeta } : {}),+ };}case 'agent_task_update': {@@ -9642,10 +9661,12 @@ function isSyntheticTriggerRow(m: Message): boolean {/*** 该 thinking 服务端行是否**不进渲染列表**(DB 行照旧保留,只是不展示)。*- * 两类:+ * 三类:* - `isRedacted` 加密推理:没有任何明文可读,卡片只能显示"无法显示的思考过程",对用户是* 纯噪音;上游开服务端工具后一轮能出十几条,会淹掉真实产出。与 live 路径* (handleStreamEvent 的 stage==='redacted')同判定。+ * - pi 旧版 translator 丢失 redacted 标记后落下的精确 `[Reasoning redacted]` 占位:+ * 无需改库,历史恢复时按同一语义隐藏。* - omitted-display 占位行(空文本 + 0 时长,非 redacted):不复原成 "Thought for 1s" 卡片。* 上游恢复明文下发后新数据自然不再命中。*@@ -9657,6 +9678,7 @@ function isHiddenThinkingRow(m: Message): boolean {const c = m.content as Record<string, unknown>;if (c.isRedacted === true) return true;const text = typeof c.text === 'string' ? c.text : '';+ if (isLegacyRedactedThinkingPlaceholder(m, text)) return true;const durationMs = typeof c.durationMs === 'number' ? c.durationMs : 0;return isOmittedThinkingPlaceholder(text, durationMs);}diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.tsindex 43930499ff..a3809b2f67 100644--- a/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/pi-translator.test.ts@@ -3,7 +3,7 @@* 重点:compaction 边界事件、turn 级 usage 累计与 done 上报、reset 时机。*/-import { describe, expect, it } from 'vitest';+import { describe, expect, it, vi } from 'vitest';import { createPiTranslateContext, translatePiEvent, usageSnapshotOf } from '../translator.js';import type { AgentEvent } from '../../../types/events.js';@@ -107,4 +107,235 @@ describe('pi translator', () => {expect(ctx.turnCacheRead).toBe(0);expect(ctx.turnCacheWrite).toBe(0);});++ it('preserves pi redacted thinking as a structured redacted event', () => {+ const ctx = createPiTranslateContext(noopLogger);+ const { queue, events } = makeQueue();++ translatePiEvent(ev({ type: 'message_start' }), queue, ctx);+ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_start',+ contentIndex: 0,+ partial: {+ role: 'assistant',+ content: [{ type: 'thinking', thinking: '', redacted: true }],+ },+ },+ }),+ queue,+ ctx,+ );+ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_delta',+ contentIndex: 0,+ delta: '[Reasoning redacted]',+ partial: {+ role: 'assistant',+ content: [{ type: 'thinking', thinking: '[Reasoning redacted]', redacted: true }],+ },+ },+ }),+ queue,+ ctx,+ );+ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_end',+ contentIndex: 0,+ content: '[Reasoning redacted]',+ partial: {+ role: 'assistant',+ content: [{ type: 'thinking', thinking: '[Reasoning redacted]', redacted: true }],+ },+ },+ }),+ queue,+ ctx,+ );++ expect(events).toEqual([{+ type: 'thinking',+ data: { stage: 'redacted', blockId: 'pi-think-1' },+ source: 'pi',+ }]);+ });++ it('cleans up a visible placeholder when redaction is only known at thinking_end', () => {+ const ctx = createPiTranslateContext(noopLogger);+ const { queue, events } = makeQueue();++ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_start',+ contentIndex: 0,+ partial: {+ role: 'assistant',+ content: [{ type: 'thinking', thinking: '' }],+ },+ },+ }),+ queue,+ ctx,+ );+ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_end',+ contentIndex: 0,+ content: '[Reasoning redacted]',+ },+ }),+ queue,+ ctx,+ );++ expect(events).toEqual([+ expect.objectContaining({+ type: 'thinking',+ data: expect.objectContaining({ stage: 'start', blockId: 'pi-think-1' }),+ }),+ {+ type: 'thinking',+ data: { stage: 'redacted', blockId: 'pi-think-1' },+ source: 'pi',+ },+ ]);+ });++ it('keeps interleaved text and multiple redacted blocks in one assistant message hidden', () => {+ const ctx = createPiTranslateContext(noopLogger);+ const { queue, events } = makeQueue();+ const firstPartialContent = [+ { type: 'text', text: 'first section' },+ { type: 'thinking', thinking: '[Reasoning redacted]', redacted: true },+ ];+ const secondPartialContent = [+ ...firstPartialContent,+ { type: 'text', text: 'second section' },+ { type: 'thinking', thinking: '[Reasoning redacted]', redacted: true },+ ];++ translatePiEvent(ev({ type: 'message_start' }), queue, ctx);+ translatePiEvent(ev({+ type: 'message_update',+ assistantMessageEvent: { type: 'text_delta', contentIndex: 0, delta: 'first section' },+ }), queue, ctx);+ for (const [contentIndex, content] of [+ [1, firstPartialContent],+ [3, secondPartialContent],+ ] as const) {+ translatePiEvent(ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_start',+ contentIndex,+ partial: { role: 'assistant', content },+ },+ }), queue, ctx);+ translatePiEvent(ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_end',+ contentIndex,+ content: '[Reasoning redacted]',+ partial: { role: 'assistant', content },+ },+ }), queue, ctx);+ if (contentIndex === 1) {+ translatePiEvent(ev({+ type: 'message_update',+ assistantMessageEvent: { type: 'text_delta', contentIndex: 2, delta: 'second section' },+ }), queue, ctx);+ }+ }+ translatePiEvent(ev({+ type: 'message_end',+ message: {+ role: 'assistant',+ content: secondPartialContent,+ model: 'xai/grok-4.5',+ stopReason: 'stop',+ },+ }), queue, ctx);++ expect(events.filter((event) => event.type === 'thinking')).toEqual([+ {+ type: 'thinking',+ data: { stage: 'redacted', blockId: 'pi-think-1' },+ source: 'pi',+ },+ {+ type: 'thinking',+ data: { stage: 'redacted', blockId: 'pi-think-2' },+ source: 'pi',+ },+ ]);+ expect(events.filter((event) => event.type === 'text')).toEqual([+ { type: 'text', data: { text: 'first section', isFinal: false }, source: 'pi' },+ { type: 'text', data: { text: 'second section', isFinal: false }, source: 'pi' },+ expect.objectContaining({+ type: 'text',+ data: { text: 'first section\n\nsecond section', isFinal: true },+ }),+ ]);+ });++ it('keeps ordinary pi thinking_end as visible final thinking', () => {+ const ctx = createPiTranslateContext(noopLogger);+ const { queue, events } = makeQueue();++ translatePiEvent(+ ev({+ type: 'message_update',+ assistantMessageEvent: {+ type: 'thinking_end',+ contentIndex: 0,+ content: 'visible reasoning',+ partial: {+ role: 'assistant',+ content: [{ type: 'thinking', thinking: 'visible reasoning' }],+ },+ },+ }),+ queue,+ ctx,+ );++ expect(events.at(-1)).toEqual({+ type: 'thinking',+ data: expect.objectContaining({+ stage: 'final',+ blockId: 'pi-think-1',+ text: 'visible reasoning',+ }),+ source: 'pi',+ });+ });++ it('accepts the thinking-level status notification without warning', () => {+ const warn = vi.fn();+ const logger: Logger = { ...noopLogger, warn };+ const ctx = createPiTranslateContext(logger);+ const { queue, events } = makeQueue();++ translatePiEvent(+ ev({ type: 'thinking_level_changed', thinkingLevel: 'high' }),+ queue,+ ctx,+ );++ expect(events).toEqual([]);+ expect(warn).not.toHaveBeenCalled();+ });});diff --git a/packages/maker-core/src/agents/pi/translator.ts b/packages/maker-core/src/agents/pi/translator.tsindex 64f3651895..cc6ae80ecf 100644--- a/packages/maker-core/src/agents/pi/translator.ts+++ b/packages/maker-core/src/agents/pi/translator.ts@@ -36,6 +36,12 @@ interface PiAssistantMessage {stopReason?: string;}+interface PiThinkingBlock {+ blockId: string;+ startedAt: number;+ redacted: boolean;+}+export interface PiTranslateContext {logger: Logger;/** get_state 拿到的 contextWindow(模型切换时更新)。 */@@ -55,8 +61,8 @@ export interface PiTranslateContext {isStreaming: boolean;/** thinking 块序号(blockId 生成)。 */thinkingSeq: number;- /** contentIndex → blockId/startedAt(当前消息内)。 */- thinkingBlocks: Map<number, { blockId: string; startedAt: number }>;+ /** contentIndex → 当前消息内的 thinking block 状态。 */+ thinkingBlocks: Map<number, PiThinkingBlock>;}export function createPiTranslateContext(logger: Logger): PiTranslateContext {@@ -137,6 +143,30 @@ function toolResultFullText(result: unknown): string {return parts.join('\n');}+/**+ * pi 的 thinking 事件把 redacted 标记放在 partial 的当前 AssistantMessage block 上。+ * 必须恢复成结构化 stage:redacted,否则占位文本会被当成普通 thinking 落库并显示。+ * 字符串判定兼容未附 partial 的旧版 thinking_end RPC 帧。+ */+function isRedactedThinkingDelta(delta: Record<string, unknown>, contentIndex: number): boolean {+ const partial = delta.partial;+ if (typeof partial === 'object' && partial !== null) {+ const content = (partial as { content?: unknown }).content;+ if (Array.isArray(content)) {+ const block = content[contentIndex];+ if (+ typeof block === 'object'+ && block !== null+ && (block as { type?: unknown }).type === 'thinking'+ && (block as { redacted?: unknown }).redacted === true+ ) {+ return true;+ }+ }+ }+ return delta.content === '[Reasoning redacted]';+}+/** 主入口:一帧 pi RPC 事件 → 0..n 个 AgentEvent。 */export function translatePiEvent(event: PiRpcEvent,@@ -307,6 +337,7 @@ export function translatePiEvent(}case 'queue_update':+ case 'thinking_level_changed':case 'summarization_retry_scheduled':case 'summarization_retry_attempt_start':case 'summarization_retry_finished':@@ -343,19 +374,28 @@ function handleAssistantDelta(}case 'thinking_start': {- const blockId = `pi-think-${++ctx.thinkingSeq}`;- const startedAt = Date.now();- ctx.thinkingBlocks.set(contentIndex, { blockId, startedAt });- queue.push({- type: 'thinking',- data: { stage: 'start', blockId, startedAt },- source: 'pi',- });+ ensureThinkingBlock(+ contentIndex,+ queue,+ ctx,+ isRedactedThinkingDelta(delta, contentIndex),+ );return;}case 'thinking_delta': {- const block = ensureThinkingBlock(contentIndex, queue, ctx);+ const redacted = isRedactedThinkingDelta(delta, contentIndex);+ const block = ensureThinkingBlock(contentIndex, queue, ctx, redacted);+ if (redacted && !block.redacted) {+ block.redacted = true;+ queue.push({+ type: 'thinking',+ data: { stage: 'redacted', blockId: block.blockId },+ source: 'pi',+ });+ return;+ }+ if (block.redacted) return;if (typeof delta.delta === 'string' && delta.delta.length > 0) {queue.push({type: 'thinking',@@ -367,8 +407,17 @@ function handleAssistantDelta(}case 'thinking_end': {- const block = ensureThinkingBlock(contentIndex, queue, ctx);+ const redacted = isRedactedThinkingDelta(delta, contentIndex);+ const block = ensureThinkingBlock(contentIndex, queue, ctx, redacted);ctx.thinkingBlocks.delete(contentIndex);+ if (redacted || block.redacted) {+ queue.push({+ type: 'thinking',+ data: { stage: 'redacted', blockId: block.blockId },+ source: 'pi',+ });+ return;+ }queue.push({type: 'thinking',data: {@@ -402,15 +451,22 @@ function ensureThinkingBlock(contentIndex: number,queue: AsyncQueue<AgentEvent>,ctx: PiTranslateContext,-): { blockId: string; startedAt: number } {+ redacted = false,+): PiThinkingBlock {const existing = ctx.thinkingBlocks.get(contentIndex);if (existing) return existing;- const block = { blockId: `pi-think-${++ctx.thinkingSeq}`, startedAt: Date.now() };+ const block = {+ blockId: `pi-think-${++ctx.thinkingSeq}`,+ startedAt: Date.now(),+ redacted,+ };ctx.thinkingBlocks.set(contentIndex, block);- queue.push({- type: 'thinking',- data: { stage: 'start', blockId: block.blockId, startedAt: block.startedAt },- source: 'pi',- });+ if (!redacted) {+ queue.push({+ type: 'thinking',+ data: { stage: 'start', blockId: block.blockId, startedAt: block.startedAt },+ source: 'pi',+ });+ }return block;}