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(desktop): 改名素材优先使用群聊用户正文

Dash33dcb158ffdd
diff --git a/apps/desktop/src/main/localDb/__tests__/latestMessageText.test.ts b/apps/desktop/src/main/localDb/__tests__/latestMessageText.test.ts
index 9fd2f814ae..1dfb037088 100644
--- a/apps/desktop/src/main/localDb/__tests__/latestMessageText.test.ts
+++ b/apps/desktop/src/main/localDb/__tests__/latestMessageText.test.ts
@@ -115,9 +115,23 @@ describe('regenerateTitleMaterial pagination', () => {
id, client_id, session_id, role, content, tool_use_id, agent_meta, created_at, rewind_at
) VALUES (?, ?, 's1', ?, ?, NULL, ?, ?, NULL)
`);
- insert.run('m1', 'c1', 'user', JSON.stringify({ text: '继续' }), JSON.stringify({ autoResume: true }), 1);
+ insert.run(
+ 'm1',
+ 'c1',
+ 'user',
+ JSON.stringify({ text: '继续' }),
+ JSON.stringify({ autoResume: true }),
+ 1,
+ );
insert.run('m2', 'c2', 'user', JSON.stringify({ text: '真实需求:修复标题' }), null, 2);
- insert.run('m3', 'c3', 'assistant', JSON.stringify('已完成修复'), JSON.stringify({ turnCompleted: true }), 3);
+ insert.run(
+ 'm3',
+ 'c3',
+ 'assistant',
+ JSON.stringify('已完成修复'),
+ JSON.stringify({ turnCompleted: true }),
+ 3,
+ );
const material = await regenerateTitleMaterial('s1', 8);
@@ -137,14 +151,28 @@ describe('regenerateTitleMaterial pagination', () => {
) VALUES (?, ?, 's1', ?, ?, NULL, ?, ?, NULL)
`);
insert.run('m1', 'c1', 'user', JSON.stringify({ text: '原始需求' }), null, 1);
- insert.run('m2', 'c2', 'assistant', JSON.stringify('历史答复'), JSON.stringify({ turnCompleted: true }), 2);
+ insert.run(
+ 'm2',
+ 'c2',
+ 'assistant',
+ JSON.stringify('历史答复'),
+ JSON.stringify({ turnCompleted: true }),
+ 2,
+ );
let stateReads = 0;
const material = await regenerateTitleMaterial('s1', 8, () => {
stateReads += 1;
if (stateReads === 1) {
insert.run('m3', 'c3', 'user', JSON.stringify({ text: '新一轮需求' }), null, 3);
- insert.run('m4', 'c4', 'assistant', JSON.stringify('施工播报'), JSON.stringify({ uuid: 'progress' }), 4);
+ insert.run(
+ 'm4',
+ 'c4',
+ 'assistant',
+ JSON.stringify('施工播报'),
+ JSON.stringify({ uuid: 'progress' }),
+ 4,
+ );
}
return true;
});
@@ -158,3 +186,67 @@ describe('regenerateTitleMaterial pagination', () => {
expect(material.recent.some((message) => message.text === '施工播报')).toBe(false);
});
});
+
+describe('regenerateTitleMaterial hook user text', () => {
+ it.each([false, true])(
+ 'uses the authored body for opening and recent messages (JSON content: %s)',
+ async (jsonContent) => {
+ const { sqlite } = createHarness();
+ sqlite.prepare('INSERT INTO sessions (id, cleared_at) VALUES (?, NULL)').run('s1');
+ const insert = sqlite.prepare(`
+ INSERT INTO messages (id, client_id, session_id, role, content, agent_meta, created_at)
+ VALUES (?, ?, 's1', ?, ?, ?, ?)
+ `);
+ const background = `<group_chat_context>${'旧群聊背景'.repeat(100)}</group_chat_context>`;
+ const opening = '修复手机滚动卡顿';
+ const latest = '总结本周手机流畅性修改,包括草稿';
+ for (const [index, body] of [opening, latest].entries()) {
+ const text = background + body;
+ insert.run(
+ `u${index}`,
+ `cu${index}`,
+ 'user',
+ jsonContent ? JSON.stringify({ text }) : text,
+ JSON.stringify({ hookSource: { im: 'telegram', userText: body } }),
+ index * 2 + 1,
+ );
+ insert.run(
+ `a${index}`,
+ `ca${index}`,
+ 'assistant',
+ JSON.stringify('已整理修改'),
+ JSON.stringify({ turnCompleted: true, hookSource: { userText: '不应替换助手回复' } }),
+ index * 2 + 2,
+ );
+ }
+ const material = await regenerateTitleMaterial('s1', 2);
+ expect(material.opening.text).toBe(opening);
+ expect(material.recent.map((m) => m.text)).toEqual([latest, '已整理修改']);
+ expect(material.recent[0].text.slice(0, 300)).toBe(latest);
+ },
+ );
+
+ it.each([
+ null,
+ '{broken',
+ '[]',
+ JSON.stringify({ hookSource: null }),
+ JSON.stringify({ hookSource: [] }),
+ JSON.stringify({ hookSource: { im: 'telegram' } }),
+ JSON.stringify({ hookSource: { userText: 42 } }),
+ JSON.stringify({ hookSource: { userText: '' } }),
+ JSON.stringify({ hookSource: { userText: ' ' } }),
+ ])('preserves legacy and ordinary content for metadata %s', async (meta) => {
+ const { sqlite } = createHarness();
+ sqlite.prepare('INSERT INTO sessions (id, cleared_at) VALUES (?, NULL)').run('s1');
+ sqlite
+ .prepare(
+ `INSERT INTO messages (id, client_id, session_id, role, content, agent_meta, created_at)
+ VALUES ('u1', 'cu1', 's1', 'user', ?, ?, 1)`,
+ )
+ .run(JSON.stringify({ text: '原始用户正文' }), meta);
+ const material = await regenerateTitleMaterial('s1', 8);
+ expect(material.opening.text).toBe('原始用户正文');
+ expect(material.recent.map((m) => m.text)).toEqual(['原始用户正文']);
+ });
+});
diff --git a/apps/desktop/src/main/localDb/latestMessageText.ts b/apps/desktop/src/main/localDb/latestMessageText.ts
index 2958ba812d..47cea33cbb 100644
--- a/apps/desktop/src/main/localDb/latestMessageText.ts
+++ b/apps/desktop/src/main/localDb/latestMessageText.ts
@@ -123,10 +123,9 @@ interface RecentTitleDbRow {
function toTitleMessageCandidate(row: RecentTitleDbRow): TitleMessageCandidate | null {
const role = row.role === 'user' ? 'user' : 'assistant';
- const text = extractText(row.content, role);
- if (!text) return null;
-
const agentMeta = parseTitleAgentMeta(row.agentMeta);
+ const text = extractTitleMessageText(row.content, role, agentMeta);
+ if (!text) return null;
return {
role,
text,
@@ -137,6 +136,24 @@ function toTitleMessageCandidate(row: RecentTitleDbRow): TitleMessageCandidate |
};
}
+/** Use the persisted user-authored body before the caller applies its title budget. */
+function extractTitleMessageText(
+ content: string,
+ role: 'user' | 'assistant',
+ agentMeta: Record<string, unknown> | null,
+): string {
+ const source = agentMeta?.hookSource;
+ if (role === 'user' && source && typeof source === 'object' && !Array.isArray(source)) {
+ const userText = (source as Record<string, unknown>).userText;
+ if (typeof userText === 'string' && userText.trim()) {
+ // Match hook dispatch's empty-body fallback and preserve the existing
+ // synthetic-input filtering for authored text.
+ return extractText(JSON.stringify({ text: userText.trim() }), role);
+ }
+ }
+ return extractText(content, role);
+}
+
function parseTitleAgentMeta(raw: string | null): Record<string, unknown> | null {
let agentMeta: Record<string, unknown> | null = null;
if (raw) {
@@ -337,8 +354,9 @@ async function firstUserMessageWithClearedAt(
.orderBy(asc(messages.createdAt), asc(messageRowid))
.limit(OPENING_SCAN_LIMIT);
for (const row of rows) {
- if (!isVisibleTitleUser(parseTitleAgentMeta(row.agentMeta))) continue;
- const text = extractText(row.content, 'user');
+ const agentMeta = parseTitleAgentMeta(row.agentMeta);
+ if (!isVisibleTitleUser(agentMeta)) continue;
+ const text = extractTitleMessageText(row.content, 'user', agentMeta);
if (text) return { text, createdAt: row.createdAt ?? null, rowid: row.rowid };
}
return { text: '', createdAt: null, rowid: null };
@@ -367,13 +385,9 @@ export async function regenerateTitleMaterial(
.where(eq(messages.sessionId, sessionId))
.get();
const inFlightAfterSnapshotSubmit = readLatestTurnIsInFlight();
- const [clearedAt, snapshot] = await Promise.all([
- sessionClearedAt(sessionId),
- snapshotPromise,
- ]);
+ const [clearedAt, snapshot] = await Promise.all([sessionClearedAt(sessionId), snapshotPromise]);
const snapshotUpperRowid = snapshot?.rowid ?? null;
- const snapshotLatestTurnIsInFlight =
- inFlightBeforeSnapshot || inFlightAfterSnapshotSubmit;
+ const snapshotLatestTurnIsInFlight = inFlightBeforeSnapshot || inFlightAfterSnapshotSubmit;
const [recent, opening] = await Promise.all([
recentMessagesWithClearedAt(
sessionId,