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。

CR1: 伙伴回复的本轮图片从上一条会被镜像的行算起

nana41aa10e7f2cd
diff --git a/apps/desktop/src/main/companion-cloud/__tests__/turnImages.test.ts b/apps/desktop/src/main/companion-cloud/__tests__/turnImages.test.ts
new file mode 100644
index 0000000000..225a440f5d
--- /dev/null
+++ b/apps/desktop/src/main/companion-cloud/__tests__/turnImages.test.ts
@@ -0,0 +1,42 @@
+import Database from 'better-sqlite3';
+import { drizzle } from 'drizzle-orm/better-sqlite3';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import * as schema from '../../localDb/schema.js';
+import { readTurnImageUrls } from '../turnImages.js';
+
+const HASH = (c: string) => `cindy-media://blobs/${c.repeat(64)}.png`;
+
+describe('readTurnImageUrls', () => {
+ let orm: ReturnType<typeof drizzle<typeof schema>>;
+ let raw: Database.Database | null = null;
+ afterEach(() => {
+ raw?.close();
+ raw = null;
+ });
+
+ function setup() {
+ raw = new Database(':memory:');
+ raw.exec(`CREATE TABLE messages (id TEXT PRIMARY KEY, client_id TEXT NOT NULL, session_id TEXT NOT NULL, role TEXT NOT NULL,
+ content TEXT NOT NULL, tool_use_id TEXT, agent_meta TEXT, created_at INTEGER NOT NULL, rewind_at INTEGER);`);
+ const db = raw;
+ orm = drizzle(db, { schema });
+ const insert = db.prepare('INSERT INTO messages (id, client_id, session_id, role, content, agent_meta, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)');
+ return (id: string, role: string, content: string, at: number, meta: unknown = null) =>
+ insert.run(id, `c-${id}`, 's1', role, content, meta ? JSON.stringify(meta) : null, at);
+ }
+
+ it('starts the turn at the previous row the room also gets, not at a progress note or sub-agent row', async () => {
+ const add = setup();
+ add('u1', 'user', JSON.stringify({ text: '画一张图' }), 100);
+ add('t1', 'tool_result', JSON.stringify({ xdt_image_url: HASH('a') }), 110);
+ add('a1', 'assistant', JSON.stringify({ text: '正在处理' }), 120, { assistantPhase: 'commentary' });
+ add('a2', 'assistant', JSON.stringify({ text: '子任务输出' }), 125, { parentToolUseId: 'toolu_1' });
+ add('a3', 'assistant', JSON.stringify({ text: '画好了' }), 130);
+ add('u0', 'user', JSON.stringify({ text: '更早' }), 10);
+ add('t0', 'tool_result', JSON.stringify({ xdt_image_url: HASH('z') }), 20);
+ expect(await readTurnImageUrls(orm, 's1', { id: 'a3', createdAt: 130 })).toEqual([HASH('a')]);
+ // A reply that is itself mirrored ends the earlier turn: its images are not taken again.
+ add('a4', 'assistant', JSON.stringify({ text: '再来' }), 200);
+ expect(await readTurnImageUrls(orm, 's1', { id: 'a4', createdAt: 200 })).toEqual([]);
+ });
+});
diff --git a/apps/desktop/src/main/companion-cloud/host.ts b/apps/desktop/src/main/companion-cloud/host.ts
index 909afbedf6..380b0d3ade 100644
--- a/apps/desktop/src/main/companion-cloud/host.ts
+++ b/apps/desktop/src/main/companion-cloud/host.ts
@@ -36,7 +36,8 @@ import type { CompanionMaterializeHost } from './materialize.js';
import { companionHomes } from './homes.js';
import { companionCapabilitiesPatch, portableCompanionCapabilities, type CompanionArrivalNotice } from './capabilities.js';
import { MIRROR_ATTACHMENT_MAX_BYTES, type CompanionMirrorCursor, type CompanionMirrorRow } from './mirror.js';
-import { replyArtifactAttachments, replyTextWithFiles, toolResultImageUrls } from './replyArtifacts.js';
+import { replyArtifactAttachments, replyTextWithFiles } from './replyArtifacts.js';
+import { mirrorColumns, readTurnImageUrls, visibleRows } from './turnImages.js';
import type { CompanionCloudHost } from './runtime.js';
import { createCompanionCloudState } from './state.js';
import type { CompanionLog } from './types.js';
@@ -299,22 +300,6 @@ async function readTaskFileRoots(sessionId: string): Promise<string[]> {
return session ? taskImageRoots(session) : [];
}
-/**
- * Stored images the tools produced since the row before this reply (the previous user or assistant
- * row): what the chat shows as images of the turn. Each lands on the first reply after it.
- */
-async function readTurnImageUrls(sessionId: string, row: { id: string; createdAt: number }): Promise<string[]> {
- const { drizzle } = getDbClient();
- const [previous] = await drizzle.select({ createdAt: messages.createdAt }).from(messages)
- .where(and(visibleRows(sessionId), lt(messages.createdAt, row.createdAt)))
- .orderBy(desc(messages.createdAt)).limit(1);
- const results = await drizzle.select({ content: messages.content }).from(messages)
- .where(and(eq(messages.sessionId, sessionId), isNull(messages.rewindAt), eq(messages.role, 'tool_result'),
- gt(messages.createdAt, previous?.createdAt ?? 0), lte(messages.createdAt, row.createdAt)))
- .orderBy(asc(messages.createdAt)).limit(50);
- return [...new Set(results.flatMap(result => toolResultImageUrls(result.content)))];
-}
-
/** A media blob this task may read and this computer still has (the reply only names its address). */
async function readReplyBlob(url: string, sessionId: string): Promise<{ mimeType: string; size: number } | null> {
const blob = parseBlobUrl(url);
@@ -324,13 +309,6 @@ async function readReplyBlob(url: string, sessionId: string): Promise<{ mimeType
return stat.isFile() ? { mimeType, size: stat.size } : null;
}
-const mirrorColumns = {
- id: messages.id, clientId: messages.clientId, role: messages.role, content: messages.content,
- agentMeta: messages.agentMeta, createdAt: messages.createdAt,
-};
-const visibleRows = (sessionId: string) => and(eq(messages.sessionId, sessionId), isNull(messages.rewindAt),
- inArray(messages.role, ['user', 'assistant']));
-
export async function readMirrorRowsAfter(sessionId: string, cursor: CompanionMirrorCursor, limit: number): Promise<CompanionMirrorRow[]> {
return getDbClient().drizzle.select(mirrorColumns).from(messages)
.where(and(visibleRows(sessionId), or(gt(messages.createdAt, cursor.createdAt),
@@ -417,7 +395,7 @@ export function createCompanionCloudHost(input: {
replyAttachments: (_botId, sessionId, row, text) => replyArtifactAttachments(row, text, sessionId, {
roots: readTaskFileRoots,
blob: readReplyBlob,
- turnImages: readTurnImageUrls,
+ turnImages: (sessionId, row) => readTurnImageUrls(getDbClient().drizzle, sessionId, row),
maxBytes: MIRROR_ATTACHMENT_MAX_BYTES,
...(input.log ? { log: input.log } : {}),
}),
diff --git a/apps/desktop/src/main/companion-cloud/turnImages.ts b/apps/desktop/src/main/companion-cloud/turnImages.ts
new file mode 100644
index 0000000000..e8de15c667
--- /dev/null
+++ b/apps/desktop/src/main/companion-cloud/turnImages.ts
@@ -0,0 +1,40 @@
+/** Which stored rows of a companion's task the room mirror reads, and the images a reply's turn produced. */
+import { and, asc, desc, eq, gt, inArray, isNull, lt, lte } from 'drizzle-orm';
+import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
+import { messages } from '../localDb/schema.js';
+import type * as schema from '../localDb/schema.js';
+import { mirrorMessageText } from './mirror.js';
+import { toolResultImageUrls } from './replyArtifacts.js';
+
+export const mirrorColumns = {
+ id: messages.id, clientId: messages.clientId, role: messages.role, content: messages.content,
+ agentMeta: messages.agentMeta, createdAt: messages.createdAt,
+};
+export const visibleRows = (sessionId: string) => and(eq(messages.sessionId, sessionId), isNull(messages.rewindAt),
+ inArray(messages.role, ['user', 'assistant']));
+
+/**
+ * Stored images the tools produced since the row before this reply that the room also gets (the
+ * previous mirrored user or assistant row, the same test as mirrorMessageText): what the chat shows
+ * as images of the turn. Each lands on the first mirrored reply after it, so a progress note or
+ * sub-agent row between the tool and the reply does not take them away.
+ */
+export async function readTurnImageUrls(drizzle: BetterSQLite3Database<typeof schema>, sessionId: string, row: { id: string; createdAt: number }): Promise<string[]> {
+ let before = row.createdAt;
+ let previousAt = 0;
+ for (let pages = 0; pages < 20; pages += 1) {
+ const rows = await drizzle.select(mirrorColumns).from(messages)
+ .where(and(visibleRows(sessionId), lt(messages.createdAt, before)))
+ .orderBy(desc(messages.createdAt), desc(messages.id)).limit(50);
+ const found = rows.find(candidate => mirrorMessageText(candidate) !== null);
+ if (found) { previousAt = found.createdAt; break; }
+ if (rows.length < 50) break;
+ before = rows[rows.length - 1]!.createdAt;
+ }
+ const results = await drizzle.select({ content: messages.content }).from(messages)
+ .where(and(eq(messages.sessionId, sessionId), isNull(messages.rewindAt), eq(messages.role, 'tool_result'),
+ gt(messages.createdAt, previousAt), lte(messages.createdAt, row.createdAt)))
+ .orderBy(asc(messages.createdAt)).limit(50);
+ return [...new Set(results.flatMap(result => toolResultImageUrls(result.content)))];
+}
+