This commit is contained in:
2026-05-11 20:27:41 +08:00
parent e30b733b17
commit 481a27fc53
60 changed files with 6357 additions and 1100 deletions

View File

@@ -75,16 +75,16 @@ type PlatformCreationAgentFlowControllerOptions<
enterCreateTab: () => void;
setSelectionStage: (stage: SelectionStage) => void;
onSessionOpened?: () => void;
onOpenError?: (params: {
error: unknown;
errorMessage: string;
}) => void;
onOpenError?: (params: { error: unknown; errorMessage: string }) => void;
onActionComplete?: (params: {
payload: TActionPayload;
response: TActionResponse;
session: TSession;
setSession: (session: TSession) => void;
}) => Promise<void> | void;
}) =>
| Promise<{ openResult?: boolean } | void>
| { openResult?: boolean }
| void;
beforeExecuteAction?: (params: {
payload: TActionPayload;
session: TSession;
@@ -93,12 +93,14 @@ type PlatformCreationAgentFlowControllerOptions<
payload: TActionPayload;
error: unknown;
errorMessage: string;
}) => void;
session: TSession;
setSession: (session: TSession) => void;
}) => void | Promise<void>;
};
function buildOptimisticMessage<TMessagePayload extends CreationAgentMessageLike>(
payload: TMessagePayload,
) {
function buildOptimisticMessage<
TMessagePayload extends CreationAgentMessageLike,
>(payload: TMessagePayload) {
return {
id: payload.clientMessageId,
role: 'user',
@@ -157,40 +159,43 @@ export function usePlatformCreationAgentFlowController<
setIsStreamingReply(false);
}, []);
const openWorkspace = useCallback(async (createPayload?: TCreatePayload) => {
if (isBusy) {
return null;
}
const openWorkspace = useCallback(
async (createPayload?: TCreatePayload) => {
if (isBusy) {
return null;
}
setIsBusy(true);
setError(null);
resetStreamingReply();
setIsBusy(true);
setError(null);
resetStreamingReply();
try {
const response = await options.client.createSession(
createPayload ?? options.createPayload,
);
const nextSession = options.client.selectSession(response);
setSession(nextSession);
options.enterCreateTab();
options.onSessionOpened?.();
options.setSelectionStage(options.workspaceStage);
return nextSession;
} catch (caughtError) {
const errorMessage = options.resolveErrorMessage(
caughtError,
options.errorMessages.open,
);
setError(errorMessage);
options.onOpenError?.({
error: caughtError,
errorMessage,
});
return null;
} finally {
setIsBusy(false);
}
}, [isBusy, options, resetStreamingReply]);
try {
const response = await options.client.createSession(
createPayload ?? options.createPayload,
);
const nextSession = options.client.selectSession(response);
setSession(nextSession);
options.enterCreateTab();
options.onSessionOpened?.();
options.setSelectionStage(options.workspaceStage);
return nextSession;
} catch (caughtError) {
const errorMessage = options.resolveErrorMessage(
caughtError,
options.errorMessages.open,
);
setError(errorMessage);
options.onOpenError?.({
error: caughtError,
errorMessage,
});
return null;
} finally {
setIsBusy(false);
}
},
[isBusy, options, resetStreamingReply],
);
const restoreDraft = useCallback(
async (sessionId: string | null | undefined) => {
@@ -215,7 +220,10 @@ export function usePlatformCreationAgentFlowController<
return nextSession;
} catch (caughtError) {
setError(
options.resolveErrorMessage(caughtError, options.errorMessages.restore),
options.resolveErrorMessage(
caughtError,
options.errorMessages.restore,
),
);
options.enterCreateTab();
options.setSelectionStage(options.platformStage);
@@ -259,8 +267,7 @@ export function usePlatformCreationAgentFlowController<
setSession(nextSession);
updateStreamingReplyText('');
} catch (caughtError) {
const interruptedReplyText =
latestStreamingReplyTextRef.current.trim();
const interruptedReplyText = latestStreamingReplyTextRef.current.trim();
// 上游流可能在已经吐出可读回复后才失败;把这段回复落进本地消息列表,避免 UI 收尾时突然消失。
if (interruptedReplyText) {
const interruptedMessage =
@@ -276,7 +283,10 @@ export function usePlatformCreationAgentFlowController<
);
}
setError(
options.resolveErrorMessage(caughtError, options.errorMessages.submit),
options.resolveErrorMessage(
caughtError,
options.errorMessages.submit,
),
);
} finally {
setIsStreamingReply(false);
@@ -301,13 +311,17 @@ export function usePlatformCreationAgentFlowController<
targetSession.sessionId,
payload,
);
await options.onActionComplete?.({
const actionCompleteResult = await options.onActionComplete?.({
payload,
response,
session: targetSession,
setSession,
});
if (options.isCompileAction(payload)) {
if (
options.isCompileAction(payload) &&
(typeof actionCompleteResult !== 'object' ||
actionCompleteResult?.openResult !== false)
) {
options.setSelectionStage(options.resultStage);
}
} catch (caughtError) {
@@ -316,10 +330,12 @@ export function usePlatformCreationAgentFlowController<
options.errorMessages.execute,
);
setError(errorMessage);
options.onActionError?.({
await options.onActionError?.({
payload,
error: caughtError,
errorMessage,
session: targetSession,
setSession,
});
} finally {
setIsBusy(false);