feat: improve skill uploads and agent runtime stability
This commit is contained in:
1 parent
0980eea975
commit
1b43205c1b
14 files changed
+655
-142
No files matched your search
@@ -244,7 +244,7 @@ export async function stopActiveThreadBeforeClarificationReply(
|
||||
await new Promise((resolve) => setTimeout(resolve, 120));
|
||||
}
|
||||
|
||||
function getStreamErrorMessage(error: unknown): string {
|
||||
function getStreamErrorMessage(error: unknown): string {
|
||||
if (typeof error === "string" && error.trim()) {
|
||||
return error;
|
||||
}
|
||||
@@ -264,8 +264,21 @@ function getStreamErrorMessage(error: unknown): string {
|
||||
return nestedError;
|
||||
}
|
||||
}
|
||||
return "Request failed.";
|
||||
}
|
||||
return "Request failed.";
|
||||
}
|
||||
|
||||
/**
|
||||
* React production error #185 is the minified form of "Maximum update depth
|
||||
* exceeded". It is a client render-loop guard, not a backend/model failure,
|
||||
* so it must never be surfaced as the answer's stream error.
|
||||
*/
|
||||
function isReactMaximumUpdateDepthError(message: string): boolean {
|
||||
return (
|
||||
/maximum update depth exceeded/i.test(message) ||
|
||||
/minified react error\s*#185\b/i.test(message) ||
|
||||
/react\.dev\/errors\/185\b/i.test(message)
|
||||
);
|
||||
}
|
||||
|
||||
const TERMINAL_RUN_STATUSES = new Set([
|
||||
"success",
|
||||
@@ -575,11 +588,17 @@ export function useThreadStream({
|
||||
runMetadataStorageRef.current = getRunMetadataStorage();
|
||||
}
|
||||
|
||||
const thread = useStream<AgentThreadState>({
|
||||
client: getAPIClient(isMock),
|
||||
assistantId: assistantId ?? "lead_agent",
|
||||
threadId: onStreamThreadId,
|
||||
reconnectOnMount: runMetadataStorageRef.current
|
||||
const thread = useStream<AgentThreadState>({
|
||||
client: getAPIClient(isMock),
|
||||
assistantId: assistantId ?? "lead_agent",
|
||||
threadId: onStreamThreadId,
|
||||
// The SDK's current implementation falls back to synchronous external-
|
||||
// store notifications when this option is omitted. A network chunk can
|
||||
// contain dozens of token events, causing nested React renders and the
|
||||
// intermittent production-only React #185 error. Coalesce notifications
|
||||
// to one visual update per frame; stream data itself is not dropped.
|
||||
throttle: 16,
|
||||
reconnectOnMount: runMetadataStorageRef.current
|
||||
? () => runMetadataStorageRef.current!
|
||||
: false,
|
||||
fetchStateHistory: { limit: 1 },
|
||||
@@ -720,10 +739,21 @@ export function useThreadStream({
|
||||
toast(e.message);
|
||||
}
|
||||
},
|
||||
onError(error) {
|
||||
markStreamActivity();
|
||||
const message = getStreamErrorMessage(error);
|
||||
// Replacing a stalled local SSE reader aborts the in-flight fetch. That
|
||||
onError(error) {
|
||||
markStreamActivity();
|
||||
const message = getStreamErrorMessage(error);
|
||||
// A synchronous SDK store notification used to let React's update-depth
|
||||
// guard escape into the stream promise. Do not present that internal UI
|
||||
// exception as a model failure. Reconcile/rejoin on the next task so the
|
||||
// backend run can continue and the current answer is preserved.
|
||||
if (isReactMaximumUpdateDepthError(message)) {
|
||||
window.setTimeout(() => {
|
||||
setStreamHoldOpen(true);
|
||||
void confirmOrRejoinRef.current("error");
|
||||
}, 0);
|
||||
return;
|
||||
}
|
||||
// Replacing a stalled local SSE reader aborts the in-flight fetch. That
|
||||
// is expected and must not look like a user-facing "network" failure.
|
||||
if (isAbortLikeError(error)) {
|
||||
return;
|
||||
@@ -1390,19 +1420,28 @@ export function useThreadStream({
|
||||
}
|
||||
: thread.values;
|
||||
|
||||
const mergedMessages =
|
||||
Array.isArray(mergedValues.messages)
|
||||
? (mergedValues.messages as Message[])
|
||||
: thread.messages;
|
||||
|
||||
const mergedThread =
|
||||
optimisticMessages.length > 0 || mergedValues !== thread.values
|
||||
? ({
|
||||
...thread,
|
||||
values: mergedValues,
|
||||
messages: [...mergedMessages, ...optimisticMessages],
|
||||
} as typeof thread)
|
||||
: thread;
|
||||
const mergedMessages =
|
||||
Array.isArray(mergedValues.messages)
|
||||
? (mergedValues.messages as Message[])
|
||||
: thread.messages;
|
||||
|
||||
const visibleThreadError =
|
||||
thread.error &&
|
||||
isReactMaximumUpdateDepthError(getStreamErrorMessage(thread.error))
|
||||
? undefined
|
||||
: thread.error;
|
||||
|
||||
const mergedThread =
|
||||
optimisticMessages.length > 0 ||
|
||||
mergedValues !== thread.values ||
|
||||
visibleThreadError !== thread.error
|
||||
? ({
|
||||
...thread,
|
||||
error: visibleThreadError,
|
||||
values: mergedValues,
|
||||
messages: [...mergedMessages, ...optimisticMessages],
|
||||
} as typeof thread)
|
||||
: thread;
|
||||
|
||||
// 第 4 个返回值 `onStreamThreadId` = 底层 useStream 当前实际绑定的线程 id(新建/未保存时为 null)。
|
||||
// 调用方据此判断「流是否已切到全新线程」,避免在切换会话的那一帧把消息发进上一个会话
|
||||
|
||||
Reference in new issue
Block a user