fix assistant custom widget creation
This commit is contained in:
1 parent
474d9968b5
commit
ceed0979a1
21 files changed
+513
-47
No files matched your search
@@ -3,20 +3,32 @@
|
||||
import type { PropsWithChildren } from "react";
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
export const createAssistantAutoApprovalTracker = () => {
|
||||
const AUTO_APPROVAL_RETRY_TIMEOUT_MS = 5_000;
|
||||
const AUTO_APPROVAL_MAX_ATTEMPTS = 2;
|
||||
|
||||
export const createAssistantAutoApprovalTracker = (maximumAttempts = Number.POSITIVE_INFINITY) => {
|
||||
const claimedToolCalls = new Set<string>();
|
||||
const attemptsByToolCall = new Map<string, number>();
|
||||
|
||||
return {
|
||||
claim(toolCallId: string) {
|
||||
if (claimedToolCalls.has(toolCallId)) return false;
|
||||
const attempts = attemptsByToolCall.get(toolCallId) ?? 0;
|
||||
if (attempts >= maximumAttempts) return false;
|
||||
claimedToolCalls.add(toolCallId);
|
||||
attemptsByToolCall.set(toolCallId, attempts + 1);
|
||||
return true;
|
||||
},
|
||||
release(toolCallId: string) {
|
||||
claimedToolCalls.delete(toolCallId);
|
||||
},
|
||||
complete(toolCallId: string) {
|
||||
claimedToolCalls.delete(toolCallId);
|
||||
attemptsByToolCall.delete(toolCallId);
|
||||
},
|
||||
clear() {
|
||||
claimedToolCalls.clear();
|
||||
attemptsByToolCall.clear();
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -25,6 +37,8 @@ interface AssistantAutoApprovalContextValue {
|
||||
enabled: boolean;
|
||||
setEnabled: (enabled: boolean) => void;
|
||||
requestAction: (toolCallId: string, confirm: () => void) => boolean;
|
||||
completeAction: (toolCallId: string) => void;
|
||||
retryRevision: number;
|
||||
}
|
||||
|
||||
const AssistantAutoApprovalContext = createContext<AssistantAutoApprovalContextValue | null>(null);
|
||||
@@ -34,22 +48,41 @@ export const AssistantAutoApprovalProvider = ({
|
||||
conversationId,
|
||||
}: PropsWithChildren<{ conversationId: string }>) => {
|
||||
const [enabled, setEnabledState] = useState(false);
|
||||
const trackerRef = useRef(createAssistantAutoApprovalTracker());
|
||||
const [retryRevision, setRetryRevision] = useState(0);
|
||||
const trackerRef = useRef(createAssistantAutoApprovalTracker(AUTO_APPROVAL_MAX_ATTEMPTS));
|
||||
const retryTimersRef = useRef(new Map<string, number>());
|
||||
const previousConversationIdRef = useRef(conversationId);
|
||||
|
||||
const setEnabled = useCallback((nextEnabled: boolean) => {
|
||||
if (!nextEnabled) trackerRef.current.clear();
|
||||
setEnabledState(nextEnabled);
|
||||
const clearRetryTimer = useCallback((toolCallId: string) => {
|
||||
const timer = retryTimersRef.current.get(toolCallId);
|
||||
if (timer !== undefined) window.clearTimeout(timer);
|
||||
retryTimersRef.current.delete(toolCallId);
|
||||
}, []);
|
||||
|
||||
const clearAutomaticActions = useCallback(() => {
|
||||
for (const timer of retryTimersRef.current.values()) window.clearTimeout(timer);
|
||||
retryTimersRef.current.clear();
|
||||
trackerRef.current.clear();
|
||||
}, []);
|
||||
|
||||
const setEnabled = useCallback(
|
||||
(nextEnabled: boolean) => {
|
||||
if (!nextEnabled) clearAutomaticActions();
|
||||
setEnabledState(nextEnabled);
|
||||
},
|
||||
[clearAutomaticActions],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const previousConversationId = previousConversationIdRef.current;
|
||||
previousConversationIdRef.current = conversationId;
|
||||
|
||||
if (previousConversationId === conversationId) return;
|
||||
trackerRef.current.clear();
|
||||
clearAutomaticActions();
|
||||
setEnabledState(false);
|
||||
}, [conversationId]);
|
||||
}, [clearAutomaticActions, conversationId]);
|
||||
|
||||
useEffect(() => clearAutomaticActions, [clearAutomaticActions]);
|
||||
|
||||
const requestAction = useCallback(
|
||||
(toolCallId: string, confirm: () => void) => {
|
||||
@@ -57,16 +90,36 @@ export const AssistantAutoApprovalProvider = ({
|
||||
|
||||
try {
|
||||
confirm();
|
||||
clearRetryTimer(toolCallId);
|
||||
retryTimersRef.current.set(
|
||||
toolCallId,
|
||||
window.setTimeout(() => {
|
||||
retryTimersRef.current.delete(toolCallId);
|
||||
trackerRef.current.release(toolCallId);
|
||||
setRetryRevision((revision) => revision + 1);
|
||||
}, AUTO_APPROVAL_RETRY_TIMEOUT_MS),
|
||||
);
|
||||
return true;
|
||||
} catch {
|
||||
trackerRef.current.release(toolCallId);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
[enabled],
|
||||
[clearRetryTimer, enabled],
|
||||
);
|
||||
|
||||
const value = useMemo(() => ({ enabled, setEnabled, requestAction }), [enabled, requestAction, setEnabled]);
|
||||
const completeAction = useCallback(
|
||||
(toolCallId: string) => {
|
||||
clearRetryTimer(toolCallId);
|
||||
trackerRef.current.complete(toolCallId);
|
||||
},
|
||||
[clearRetryTimer],
|
||||
);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({ enabled, setEnabled, requestAction, completeAction, retryRevision }),
|
||||
[completeAction, enabled, requestAction, retryRevision, setEnabled],
|
||||
);
|
||||
|
||||
return <AssistantAutoApprovalContext.Provider value={value}>{children}</AssistantAutoApprovalContext.Provider>;
|
||||
};
|
||||
@@ -88,20 +141,21 @@ export const useAssistantAutomaticAction = ({
|
||||
completed: boolean;
|
||||
confirm: () => void;
|
||||
}) => {
|
||||
const { enabled, requestAction } = useAssistantAutoApproval();
|
||||
const { completeAction, enabled, requestAction, retryRevision } = useAssistantAutoApproval();
|
||||
const [inProgress, setInProgress] = useState(false);
|
||||
const confirmRef = useRef(confirm);
|
||||
confirmRef.current = confirm;
|
||||
|
||||
useEffect(() => {
|
||||
if (!ready || completed || !enabled) {
|
||||
if (completed) completeAction(toolCallId);
|
||||
setInProgress(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const requested = requestAction(toolCallId, () => confirmRef.current());
|
||||
setInProgress(requested);
|
||||
}, [completed, enabled, ready, requestAction, toolCallId]);
|
||||
}, [completeAction, completed, enabled, ready, requestAction, retryRevision, toolCallId]);
|
||||
|
||||
return inProgress;
|
||||
};
|
||||
Reference in new issue
Block a user