@docyrus/ui-pro-ai-assistant 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2557,48 +2557,77 @@ function toAgentTriggerData(id, data) {
2557
2557
  }
2558
2558
  function useAgentsData(agentIds) {
2559
2559
  const apiClient = useApiClient();
2560
+ const apiClientRef = useRef(apiClient);
2561
+ apiClientRef.current = apiClient;
2560
2562
  const [agents, setAgents] = useState([]);
2561
2563
  const [isLoading, setIsLoading] = useState(false);
2562
2564
  const [error, setError] = useState(null);
2565
+ const [retryCount, setRetryCount] = useState(0);
2563
2566
  const idsKey = agentIds.join(",");
2564
- const fetchAgents = useCallback(async () => {
2565
- const ids = idsKey.split(",").filter(Boolean);
2566
- if (ids.length === 0) {
2567
- setAgents([]);
2568
- return;
2569
- }
2570
- setIsLoading(true);
2571
- setError(null);
2572
- try {
2573
- const results = await Promise.allSettled(
2574
- ids.map((id) => apiClient.get(`/ai/agent-deployments/base/${id}`))
2575
- );
2576
- const resolved = [];
2577
- for (let i = 0; i < ids.length; i++) {
2578
- const result = results[i];
2579
- if (result.status === "fulfilled" && result.value.success && result.value.data) {
2580
- resolved.push(
2581
- toAgentTriggerData(ids[i], result.value.data)
2567
+ useEffect(() => {
2568
+ let cancelled = false;
2569
+ const run = async () => {
2570
+ const ids = idsKey.split(",").filter(Boolean);
2571
+ if (ids.length === 0) {
2572
+ setAgents([]);
2573
+ return;
2574
+ }
2575
+ setIsLoading(true);
2576
+ setError(null);
2577
+ try {
2578
+ const BATCH_SIZE = 3;
2579
+ const BATCH_DELAY = 200;
2580
+ const resolved = [];
2581
+ const client = apiClientRef.current;
2582
+ for (let start = 0; start < ids.length; start += BATCH_SIZE) {
2583
+ if (cancelled) return;
2584
+ if (start > 0) {
2585
+ await new Promise((resolve) => {
2586
+ const timer = setTimeout(resolve, BATCH_DELAY);
2587
+ if (cancelled) clearTimeout(timer);
2588
+ });
2589
+ }
2590
+ const batch = ids.slice(start, start + BATCH_SIZE);
2591
+ const results = await Promise.allSettled(
2592
+ batch.map((id) => client.get(`/ai/agent-deployments/base/${id}`))
2593
+ );
2594
+ for (let i = 0; i < batch.length; i++) {
2595
+ const result = results[i];
2596
+ if (result.status === "fulfilled" && result.value.success && result.value.data) {
2597
+ resolved.push(
2598
+ toAgentTriggerData(batch[i], result.value.data)
2599
+ );
2600
+ }
2601
+ }
2602
+ }
2603
+ if (!cancelled) {
2604
+ setAgents(resolved);
2605
+ }
2606
+ } catch (err) {
2607
+ if (!cancelled) {
2608
+ setError(
2609
+ err instanceof Error ? err : new Error("Failed to fetch agents")
2582
2610
  );
2583
2611
  }
2612
+ } finally {
2613
+ if (!cancelled) {
2614
+ setIsLoading(false);
2615
+ }
2584
2616
  }
2585
- setAgents(resolved);
2586
- } catch (err) {
2587
- setError(
2588
- err instanceof Error ? err : new Error("Failed to fetch agents")
2589
- );
2590
- } finally {
2591
- setIsLoading(false);
2592
- }
2593
- }, [apiClient, idsKey]);
2594
- useEffect(() => {
2595
- void fetchAgents();
2596
- }, [fetchAgents]);
2617
+ };
2618
+ void run();
2619
+ return () => {
2620
+ cancelled = true;
2621
+ };
2622
+ }, [idsKey, retryCount]);
2623
+ const retry = useCallback(() => {
2624
+ setRetryCount((c) => c + 1);
2625
+ }, []);
2597
2626
  return {
2598
2627
  agents,
2599
2628
  isLoading,
2600
2629
  error,
2601
- retry: fetchAgents
2630
+ retry
2602
2631
  };
2603
2632
  }
2604
2633
  function useAssistantApi({
@@ -24920,11 +24949,11 @@ var DocyAssistant = ({
24920
24949
  uiActions.setActiveTab(0);
24921
24950
  setMessages([]);
24922
24951
  };
24923
- const handleSendMessage = async (e, options3) => {
24952
+ const handleSendMessage = async (e, options3, overrideText) => {
24924
24953
  e?.preventDefault();
24925
- if (!input.trim()) return;
24954
+ const messageText = (overrideText ?? input).trim();
24955
+ if (!messageText) return;
24926
24956
  messageOptionsRef.current = options3;
24927
- const messageText = input.trim();
24928
24957
  let currentThreadId = selectedSessionIdRef.current;
24929
24958
  if (!currentThreadId) {
24930
24959
  const subject = messageText.substring(0, 100);
@@ -24995,9 +25024,7 @@ var DocyAssistant = ({
24995
25024
  if (!initialPrompt || initialPromptSentRef.current) return;
24996
25025
  initialPromptSentRef.current = true;
24997
25026
  setInput(initialPrompt);
24998
- setTimeout(() => {
24999
- handleSendMessage();
25000
- }, 300);
25027
+ void handleSendMessage(void 0, void 0, initialPrompt);
25001
25028
  }, [initialPrompt]);
25002
25029
  const handleInputChange = useCallback((e) => {
25003
25030
  setInput(e.target.value);