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(device-link): preserve host attachments when sending queued messages (#5167)

DavidShenXDbfb95c3492e8
diff --git a/apps/desktop/src/main/device-link/__tests__/outboundMedia.test.ts b/apps/desktop/src/main/device-link/__tests__/outboundMedia.test.ts
index 9f88621e16..06c861d89b 100644
--- a/apps/desktop/src/main/device-link/__tests__/outboundMedia.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/outboundMedia.test.ts
@@ -4,6 +4,7 @@
* 失败传播 / 非媒体 channel 与无附件透传。
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
+import path from 'node:path';
const uploadLocalFile = vi.hoisted(() => vi.fn());
const uploadBuffer = vi.hoisted(() => vi.fn());
@@ -300,6 +301,73 @@ describe('rewriteOutboundMedia — send/steer content-block 形态', () => {
});
describe('rewriteOutboundMedia — enqueue files 形态', () => {
+ it.each([
+ ['maker:input:enqueue', undefined],
+ ['maker:input:steer', { touchUserSend: true }],
+ ['maker:input:steer', { removeFromQueue: false }],
+ ])('uploads a new local HTML attachment for %s with %j', async (channel, opts) => {
+ const localPath = path.resolve('controller-files', 'report.html');
+ const payload = buildUserMessageAttachmentPayload([{
+ id: 'html',
+ name: 'report.html',
+ path: localPath,
+ size: 42,
+ ext: '.html',
+ category: 'text',
+ mimeType: 'text/html',
+ }]);
+ uploadLocalFile.mockResolvedValue({
+ key: 'cindy/device-link/u/report.html',
+ size: 42,
+ contentType: 'text/html',
+ sha256: SHA256,
+ });
+ const item = {
+ clientId: 'new-html',
+ files: payload.serializedFiles,
+ persistedContent: JSON.stringify({ text: 'read this', files: payload.persistFileRefs }),
+ };
+
+ const out = await rewriteOutboundMedia(channel, ['sess', item, opts]);
+ const rewritten = out[1] as typeof item;
+ const ref = rewritten.files![0].path;
+ expect(uploadLocalFile).toHaveBeenCalledExactlyOnceWith(localPath, { contentType: 'text/html' });
+ expect(parseAttachmentOssRef(ref)).toMatchObject({
+ originalName: 'report.html',
+ mimeType: 'text/html',
+ size: 42,
+ sha256: SHA256,
+ });
+ expect(JSON.parse(rewritten.persistedContent).files[0].path).toBe(ref);
+ expect(item.files![0].path).toBe(localPath);
+ });
+
+ it.each([
+ ['cached HTML', 'xdt-image://sess/report.html', 'report.html', 'text/html'],
+ ['absolute PDF path', undefined, 'report.pdf', 'application/pdf'],
+ ['media blob', `cindy-media://blobs/${SHA256}.png`, 'picture.png', 'image/png'],
+ ])('preserves host-owned %s when sending an existing queue item', async (_label, url, name, mimeType) => {
+ // These POSIX paths describe the remote host, regardless of the test platform.
+ const hostPath = `/remote/cache/sess/${name}`;
+ const item = {
+ clientId: 'already-queued',
+ text: 'read this',
+ files: [{ name, path: hostPath, ...(url ? { url } : {}), mimeType }],
+ persistedContent: JSON.stringify({ text: 'read this', files: [{ name, path: hostPath }] }),
+ };
+ const args = ['sess', item, { removeFromQueue: true, expectedClearBoundaryMs: null }];
+ resolveSafe.mockReturnValue({ absPath: path.resolve('missing-controller-cache', name), mimeType });
+ uploadLocalFile.mockRejectedValue(new Error('ENOENT: host attachment is not on the controller'));
+
+ const out = await rewriteOutboundMedia('maker:input:steer', args);
+
+ expect(out).toEqual(args);
+ expect(out[1]).toBe(item);
+ expect(resolveSafe).not.toHaveBeenCalled();
+ expect(uploadLocalFile).not.toHaveBeenCalled();
+ expect(uploadBuffer).not.toHaveBeenCalled();
+ });
+
it('item.files[] 上传 + url/path 变引用、base64 清掉(buildMakerUserMessage 取 url)', async () => {
const out = await rewriteOutboundMedia('maker:input:enqueue', [
'sess',
diff --git a/apps/desktop/src/main/device-link/outboundMedia.ts b/apps/desktop/src/main/device-link/outboundMedia.ts
index fb48be8c0b..d32d0da83d 100644
--- a/apps/desktop/src/main/device-link/outboundMedia.ts
+++ b/apps/desktop/src/main/device-link/outboundMedia.ts
@@ -380,6 +380,19 @@ async function rewriteQueued(item: unknown, existing: ReadonlySet<string> = new
* 抛错由 handleInvoke 转 MEDIA_TRANSFER_FAILED。
*/
export async function rewriteOutboundMedia(channel: string, args: unknown[], existing: ReadonlySet<string> = new Set()): Promise<unknown[]> {
+ const steerOpts = args[2];
+ if (
+ channel === 'maker:input:steer' &&
+ steerOpts &&
+ typeof steerOpts === 'object' &&
+ !Array.isArray(steerOpts) &&
+ (steerOpts as { removeFromQueue?: unknown }).removeFromQueue === true
+ ) {
+ // The host selects its authoritative queue item by clientId. Attachment
+ // URLs in the projected row belong to that host, not this controller;
+ // uploading them again would resolve remote cache URLs against local disk.
+ return args;
+ }
if (channel === 'maker:input:update-content') {
const next = [...args];
next[2] = await rewriteQueued(next[2], existing);