Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(agent-island): 清理摘要格式并隐藏远程组件源码

Dashc09f1646c145
diff --git a/apps/desktop/src/main/agent-island/__tests__/activityTextStream.test.ts b/apps/desktop/src/main/agent-island/__tests__/activityTextStream.test.ts
index 8922d10627..2c81bec5ee 100644
--- a/apps/desktop/src/main/agent-island/__tests__/activityTextStream.test.ts
+++ b/apps/desktop/src/main/agent-island/__tests__/activityTextStream.test.ts
@@ -84,6 +84,23 @@ describe('Agent Island activity text stream', () => {
.toBe('检查结果 网络检查完成 可以继续使用。');
});
+ // Screenshot regression: completed local Markdown and flattened remote rich
+ // previews both reached the native Text label verbatim (unit tier).
+ it('projects Markdown before shortening the final reply', () => {
+ const source = '分两部分:**更新已完成**;见[改动](https://example.com/' + 'x'.repeat(300) + ')。';
+ expect(normalizeActivityText(source)).toBe('分两部分:更新已完成;见改动。');
+ const stream = createActivityTextStreamState();
+ expect(appendActivityTextStream(stream, '分两部分:**已完成**')).toBe('分两部分:已完成');
+ expect(normalizeActivityText('`foo_bar **literal**`')).toBe('foo_bar **literal**');
+ });
+
+ it('hides irrecoverable rich protocol from old remote summaries', () => {
+ const collapsed = richAnswer.replace(/\s+/g, ' ');
+ expect(normalizeActivityText(collapsed)).toBe('');
+ expect(normalizeActivityText(collapsed.slice(0, 80) + '...')).toBe('');
+ expect(normalizeActivityText('已完成\n' + collapsed)).toBe('已完成');
+ });
+
it('keeps rich protocol out of streamed previews across delta boundaries', () => {
const stream = createActivityTextStreamState();
for (const chunk of ['```cindy-', 'ui\n', '{"version":1,',
@@ -95,19 +112,25 @@ describe('Agent Island activity text stream', () => {
it('preserves ordinary code and explicitly quoted rich protocol examples', () => {
const example = `\`\`\`\`markdown\n${richAnswer}\n\`\`\`\``;
- expect(normalizeActivityText(example)).toBe(legacyNormalizeActivityText(example));
+ expect(normalizeActivityText(example)).toBe(richAnswer.replace(/\s+/g, ' '));
const code = '```json\n{"status":"ok"}\n```';
- expect(normalizeActivityText(code)).toBe(legacyNormalizeActivityText(code));
+ expect(normalizeActivityText(code)).toBe('{"status":"ok"}');
});
- // Review regressions: only unfinished rich opening fences may be hidden.
- // Ordinary closing fences and inline/quoted examples must survive every delta.
- it.each(['`', '~'])('preserves %s code fences and examples during streaming', (fenceCharacter) => {
+ // Review regression: closing ordinary code fences must not retract their
+ // content; explicitly quoted component examples remain literal code.
+ it.each(['`', '~'])('preserves %s code content and examples during streaming', (fenceCharacter) => {
const fence = fenceCharacter.repeat(3);
- expectEveryStreamingStepToMatchLegacyNormalization([`${fence}json\n`, ...`{"status":"ok"}\n${fence}`]);
+ const codeStream = createActivityTextStreamState();
+ appendActivityTextStream(codeStream, `${fence}json\n`);
+ appendActivityTextStream(codeStream, '{"status":"ok"}\n');
+ for (const character of fence) {
+ expect(appendActivityTextStream(codeStream, character)).toContain('{"status":"ok"}');
+ }
+ expect(appendActivityTextStream(codeStream, '')).toBe('{"status":"ok"}');
const quoted = `${fenceCharacter.repeat(4)}markdown\n${fence}cindy-ui\n{}\n${fence}\n${fenceCharacter.repeat(4)}`;
const stream = createActivityTextStreamState();
- expect(appendActivityTextStream(stream, quoted)).toBe(legacyNormalizeActivityText(quoted));
+ expect(appendActivityTextStream(stream, quoted)).toBe(`${fence}cindy-ui {} ${fence}`);
});
it('does not turn leading whitespace into a line break inside an inline example', () => {
@@ -125,10 +148,10 @@ describe('Agent Island activity text stream', () => {
expect(normalizeActivityText(source)).toBe('检查完成。'.repeat(100).slice(0, AGENT_ISLAND_ACTIVITY_TEXT_MAX_LENGTH));
});
- it('preserves the existing display text across whitespace, markdown, emoji, and the 280-code-unit boundary', () => {
+ it('preserves plain text across whitespace, emoji, and the 280-code-unit boundary', () => {
expectEveryStreamingStepToMatchLegacyNormalization([
'\n ',
- 'I will **inspect**',
+ 'I will inspect',
'\t the logs',
'\r\n\r\n',
'before editing. ',
diff --git a/apps/desktop/src/main/agent-island/__tests__/deviceSessions.test.ts b/apps/desktop/src/main/agent-island/__tests__/deviceSessions.test.ts
index 8f50b3b481..1588421eba 100644
--- a/apps/desktop/src/main/agent-island/__tests__/deviceSessions.test.ts
+++ b/apps/desktop/src/main/agent-island/__tests__/deviceSessions.test.ts
@@ -171,6 +171,26 @@ describe('Agent Island device sessions', () => {
]);
});
+ // Screenshot regression: a remote host had already flattened and shortened
+ // the rich fence. Exercise IPC parsing and native display projection together.
+ it('hides flattened remote component operations during output and completion', () => {
+ const state = createAgentIslandState();
+ const detail = '```cindy-ui {"version":1,"seq":1,"op":"create","id":"device-preview-link","kind":"markdown","props":...';
+ const sync = (phase: 'running' | 'completed', text: string, now: number) =>
+ syncAgentIslandDeviceSessions(state, parseAgentIslandRemoteSessions([remote(phase, { detail: text })])!, now);
+ sync('running', detail, 1_000);
+ const running = buildAgentIslandDisplayState(state, 1_000).sessions[0];
+ expect(running?.detail).toBe('');
+ expect(running?.compactDetail).not.toContain('cindy-ui');
+ sync('completed', detail, 2_000);
+ expect(buildAgentIslandDisplayState(state, 2_000).sessions[0]?.activityLines)
+ .toEqual([expect.objectContaining({ kind: 'status', text: 'Done' })]);
+ sync('completed', '**预览已完成**', 3_000);
+ expect(buildAgentIslandDisplayState(state, 3_000).sessions[0]?.compactDetail).toBe('预览已完成');
+ sync('completed', detail, 4_000);
+ expect(buildAgentIslandDisplayState(state, 4_000).sessions[0]?.compactDetail).toBe('Done');
+ });
+
it('does not repeat events for an unchanged phase', () => {
const state = createAgentIslandState();
syncAgentIslandDeviceSessions(state, [remote('running')], 1_000);
diff --git a/apps/desktop/src/main/agent-island/__tests__/state.test.ts b/apps/desktop/src/main/agent-island/__tests__/state.test.ts
index dca93d24ae..3f521712e9 100644
--- a/apps/desktop/src/main/agent-island/__tests__/state.test.ts
+++ b/apps/desktop/src/main/agent-island/__tests__/state.test.ts
@@ -1010,6 +1010,30 @@ describe('Agent Island display state', () => {
.toMatchObject({ compactDetail: '网络检查完成', phase: 'completed' });
});
+ // Local screenshot regression, including the native messagePreview/activityLines
+ // fields: compactDetail alone does not cover the expanded native row.
+ it('projects Markdown once for both native preview fields and completed cards', () => {
+ const state = createAgentIslandState();
+ const meta = { sessionId: 'markdown' };
+ applyAgentIslandEvent(state, meta, statusEvent(true, 'Generating...'), 1_000);
+ const text = '分两部分:**更新已完成**;`foo_bar **literal**`';
+ const expected = '分两部分:更新已完成;foo_bar **literal**';
+ applyAgentIslandEvent(state, meta, finalTextEvent(text), 1_100);
+ const running = buildAgentIslandDisplayState(state, 1_200).sessions[0];
+ expect(running?.messagePreview?.text).toBe(expected);
+ expect(running?.activityLines.at(-1)?.text).toBe(expected);
+ expect(running?.compactDetail).toBe(expected);
+ applyAgentIslandEvent(state, meta, doneEvent(), 1_300);
+ const completed = buildAgentIslandDisplayState(state, 4_000).sessions[0];
+ expect(completed?.activityLines.at(-1)?.text).toBe(expected);
+ expect(completed?.compactDetail).toBe(expected);
+ const userState = createAgentIslandState();
+ applyAgentIslandUserPrompt(userState, { sessionId: 'user-code' }, '`foo_bar **literal**`', 5_000);
+ const user = buildAgentIslandDisplayState(userState, 5_000).sessions[0];
+ expect(user?.messagePreview?.text).toBe('foo_bar **literal**');
+ expect(user?.compactDetail).toBe('foo_bar **literal**');
+ });
+
// Review regression: a removed/emptied rich node must retract this message's
// streamed line from both the activity log and active/queued preview (unit tier).
it.each([
diff --git a/apps/desktop/src/main/agent-island/activityTextStream.ts b/apps/desktop/src/main/agent-island/activityTextStream.ts
index 0502a9eefd..db22d11964 100644
--- a/apps/desktop/src/main/agent-island/activityTextStream.ts
+++ b/apps/desktop/src/main/agent-island/activityTextStream.ts
@@ -1,4 +1,5 @@
import { richAnswerText, splitRichAnswer } from '@cindy/maker-shared/rich-answer';
+import { markdownPreviewText } from '../../shared/markdownPreviewText.js';
export const AGENT_ISLAND_ACTIVITY_TEXT_MAX_LENGTH = 280;
@@ -261,7 +262,14 @@ function rawPreviewText(state: ActivityTextStreamState): string {
}
function normalizePlainActivityText(text: string): string {
- return text
+ // Older remote hosts send an already collapsed/truncated preview. Its fence
+ // boundaries and JSON tail are gone: never show that transport as a reply.
+ const collapsedRich = /(?:^|\n)[ \t]*(`{3,}|~{3,})cindy-ui[ \t]+\{\s*"(?:version|seq|op)"\s*:/.exec(text);
+ const source = collapsedRich ? text.slice(0, collapsedRich.index) : text;
+ // Keep the hot path for plain bounded streaming prefixes cheap; formatted
+ // answers use the same projection as notifications and the session list.
+ const plain = /[*_`~<>!#|&\n\r]|\](?:\(|\[)|^\s*(?:[-+] |\d+[.)] )/.test(source) ? markdownPreviewText(source) : source;
+ return plain
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
diff --git a/apps/desktop/src/main/agent-island/state.ts b/apps/desktop/src/main/agent-island/state.ts
index c41630094e..d2759971bc 100644
--- a/apps/desktop/src/main/agent-island/state.ts
+++ b/apps/desktop/src/main/agent-island/state.ts
@@ -561,7 +561,7 @@ export function applyAgentIslandUserPrompt(
session.currentToolUseId = null;
session.toolDetailUntil = null;
clearAssistantStream(session);
- const line = appendActivityLine(session, 'user', text);
+ const line = appendNormalizedActivityLine(session, 'user', text);
if (line) enqueueMessagePreview(session, line, now);
session.sortActivityAt = now;
replaceAgentIslandCard(session, now);
@@ -1265,10 +1265,11 @@ function applyDeviceSessionPhase(
session.phase = 'running';
session.sortActivityAt = now;
}
- const changed = entering || session.detail !== input.detail || session.workingPhase !== workingPhase;
+ const detail = normalizeActivityText(input.detail);
+ const changed = entering || session.detail !== detail || session.workingPhase !== workingPhase;
session.workingPhase = workingPhase;
- session.detail = input.detail;
- session.detailSource = input.detail ? 'status' : null;
+ session.detail = detail;
+ session.detailSource = detail ? 'status' : null;
if (changed) touchSessionActivity(session, now);
return { changed, event: null };
}
@@ -1300,14 +1301,14 @@ function applyDeviceSessionPhase(
case 'completed': {
const silent = input.completionNotification === 'silent';
if (previous === 'completed' && (!silent || session.completionNotification === 'silent')) {
- return { changed: mirrorDeviceSessionSummary(session, input.detail), event: null };
+ return { changed: mirrorDeviceSessionSummary(session, input.detail, state.strings.done), event: null };
}
clearDeviceSessionInteraction(session);
session.running = false;
touchSessionActivity(session, now);
// 先换掉上一轮的摘要,且必须先于 complete:有摘要可显示时就不再补「完成」占位。
session.activityLines = [];
- mirrorDeviceSessionSummary(session, input.detail);
+ mirrorDeviceSessionSummary(session, input.detail, state.strings.done);
// 对方设备已经仲裁过终态,这里不再套本机「报错后的配对 done」保护。
session.completionAllowedAfterTerminalError = true;
completeAgentIslandSession(state, session, now, observed && !silent
@@ -1339,11 +1340,11 @@ function applyDeviceSessionPhase(
* 设备任务不同步对话,只带对方岛上算好的一行摘要,不带消息角色。记成状态行(不冒充
* 回复):完成卡片取它当文案,连续更新原地替换;没有摘要时才退回「完成」。
*/
-function mirrorDeviceSessionSummary(session: AgentIslandSessionState, detail: string): boolean {
- const text = normalizeActivityText(detail);
+function mirrorDeviceSessionSummary(session: AgentIslandSessionState, detail: string, fallback: string): boolean {
+ const text = normalizeActivityText(detail) || fallback;
const last = session.activityLines.at(-1);
- if (!text || (last?.kind === 'status' && last.text === text)) return false;
- appendActivityLine(session, 'status', text);
+ if (last?.kind === 'status' && last.text === text) return false;
+ appendNormalizedActivityLine(session, 'status', text);
return true;
}
@@ -2619,8 +2620,7 @@ function messagePreviewTextForSession(session: AgentIslandSessionState): string
if (session.phase === 'needs-interaction' || session.phase === 'error') return null;
const line = session.messagePreview?.line;
if (!line || (line.kind !== 'user' && line.kind !== 'assistant')) return null;
- const text = normalizeActivityText(line.text);
- return text || null;
+ return line.text || null;
}
function isGenericRunningStatusDetail(detail: string): boolean {
@@ -2651,7 +2651,7 @@ function latestUserActivityTitle(session: AgentIslandSessionState): string | nul
.reverse()
.find((line) => line.kind === 'user');
if (!latestUserLine) return null;
- const text = normalizeActivityText(latestUserLine.text);
+ const text = latestUserLine.text;
if (!text) return null;
return text.slice(0, 72);
}
@@ -3046,7 +3046,15 @@ function appendActivityLine(
kind: AgentIslandActivityLineKind,
rawText: string,
): AgentIslandActivityLine | null {
- const text = normalizeActivityText(rawText);
+ return appendNormalizedActivityLine(session, kind, normalizeActivityText(rawText));
+}
+
+/** Activity lines are already display text; do not parse literal code a second time. */
+function appendNormalizedActivityLine(
+ session: AgentIslandSessionState,
+ kind: AgentIslandActivityLineKind,
+ text: string,
+): AgentIslandActivityLine | null {
if (!text) return null;
const previous = session.activityLines.at(-1);
if (previous && previous.kind === kind && kind !== 'user' && kind !== 'assistant') {
@@ -3106,7 +3114,7 @@ function applyAssistantTextLine(
}
}
- const line = appendActivityLine(session, 'assistant', text);
+ const line = appendNormalizedActivityLine(session, 'assistant', text);
if (!line) return null;
if (isFinal) {
clearAssistantStream(session);