fix: stabilize ai design history switching

This commit is contained in:
2026-08-19 10:31:36 +08:00
parent abece81fdb
commit bf0b805d9f
14 changed files with 573 additions and 48 deletions

View File

@@ -18,7 +18,10 @@ import {
type DesignRenameWorkspaceInput,
type DesignSubmitMessageInput,
} from '../../../shared/image-workspace';
import { DesignWorkspaceModuleError } from '../../image-workspace/module';
import {
DesignWorkspaceModuleError,
type DesignWorkspaceEventSubscription,
} from '../../image-workspace/module';
import type { HostApiContext } from '../context';
import {
flushStreamingHeaders,
@@ -328,19 +331,25 @@ async function relayWorkspaceEvents(
}
const header = req.headers['last-event-id'];
const afterEventId = Array.isArray(header) ? header[0] : header;
const subscription = await ctx.imageWorkspace.openWorkspaceEvents({
workspaceId,
conversationId,
...(afterEventId ? { afterEventId } : {}),
});
let subscription: DesignWorkspaceEventSubscription | null = null;
let closed = false;
const close = () => {
if (closed) return;
closed = true;
subscription.close();
subscription?.close();
};
res.once('close', close);
try {
const opened = await ctx.imageWorkspace.openWorkspaceEvents({
workspaceId,
conversationId,
...(afterEventId ? { afterEventId } : {}),
});
if (closed) {
opened.close();
return;
}
subscription = opened;
res.statusCode = 200;
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache, no-transform');
@@ -456,7 +465,13 @@ export async function handleImageWorkspaceRoutes(
&& segments[0] === 'workspaces'
&& segments[2] === 'conversations'
&& req.method === 'GET') {
sendData(res, await ctx.imageWorkspace.getConversation(segments[1], segments[3]));
const before = url.searchParams.get('before');
sendData(
res,
before === null
? await ctx.imageWorkspace.getConversation(segments[1], segments[3])
: await ctx.imageWorkspace.getConversation(segments[1], segments[3], before),
);
return true;
}