@devicai/ui 0.10.2 → 0.10.3
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/cjs/components/ChatDrawer/ChatDrawer.js +2 -0
- package/dist/cjs/components/ChatDrawer/ChatDrawer.js.map +1 -1
- package/dist/cjs/components/ChatDrawer/HandoffSubagentWidget.js +9 -6
- package/dist/cjs/components/ChatDrawer/HandoffSubagentWidget.js.map +1 -1
- package/dist/cjs/hooks/useDevicChat.js +89 -27
- package/dist/cjs/hooks/useDevicChat.js.map +1 -1
- package/dist/cjs/hooks/usePolling.js +16 -12
- package/dist/cjs/hooks/usePolling.js.map +1 -1
- package/dist/cjs/provider/DevicProvider.js +3 -2
- package/dist/cjs/provider/DevicProvider.js.map +1 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/logger.js +17 -0
- package/dist/cjs/utils/logger.js.map +1 -0
- package/dist/esm/components/ChatDrawer/ChatDrawer.js +2 -0
- package/dist/esm/components/ChatDrawer/ChatDrawer.js.map +1 -1
- package/dist/esm/components/ChatDrawer/ChatDrawer.types.d.ts +6 -0
- package/dist/esm/components/ChatDrawer/HandoffSubagentWidget.js +10 -7
- package/dist/esm/components/ChatDrawer/HandoffSubagentWidget.js.map +1 -1
- package/dist/esm/hooks/useDevicChat.d.ts +6 -0
- package/dist/esm/hooks/useDevicChat.js +90 -28
- package/dist/esm/hooks/useDevicChat.js.map +1 -1
- package/dist/esm/hooks/usePolling.d.ts +5 -0
- package/dist/esm/hooks/usePolling.js +17 -13
- package/dist/esm/hooks/usePolling.js.map +1 -1
- package/dist/esm/provider/DevicProvider.d.ts +1 -1
- package/dist/esm/provider/DevicProvider.js +3 -2
- package/dist/esm/provider/DevicProvider.js.map +1 -1
- package/dist/esm/provider/types.d.ts +9 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/logger.d.ts +10 -0
- package/dist/esm/utils/logger.js +15 -0
- package/dist/esm/utils/logger.js.map +1 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMemo, useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import { createLogger } from '../utils/logger.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Hook for polling real-time chat history
|
|
@@ -8,7 +9,10 @@ import { useState, useRef, useEffect, useCallback } from 'react';
|
|
|
8
9
|
* @param options - Polling options
|
|
9
10
|
*/
|
|
10
11
|
function usePolling(chatUid, fetchFn, options = {}) {
|
|
11
|
-
const { interval = 1000, enabled = true, stopStatuses = ['completed', 'error'], onStop, onUpdate, onError, } = options;
|
|
12
|
+
const { interval = 1000, enabled = true, stopStatuses = ['completed', 'error'], onStop, onUpdate, onError, debug = false, } = options;
|
|
13
|
+
const log = useMemo(() => createLogger(debug), [debug]);
|
|
14
|
+
const logRef = useRef(log);
|
|
15
|
+
logRef.current = log;
|
|
12
16
|
const [data, setData] = useState(null);
|
|
13
17
|
const [isPolling, setIsPolling] = useState(false);
|
|
14
18
|
const [error, setError] = useState(null);
|
|
@@ -38,13 +42,13 @@ function usePolling(chatUid, fetchFn, options = {}) {
|
|
|
38
42
|
isPollingRef.current = false;
|
|
39
43
|
}, []);
|
|
40
44
|
const fetchData = useCallback(async () => {
|
|
41
|
-
|
|
45
|
+
logRef.current.log('[usePolling] fetchData called, isMounted:', isMountedRef.current);
|
|
42
46
|
if (!isMountedRef.current)
|
|
43
47
|
return;
|
|
44
48
|
try {
|
|
45
|
-
|
|
49
|
+
logRef.current.log('[usePolling] Fetching...');
|
|
46
50
|
const result = await fetchFnRef.current();
|
|
47
|
-
|
|
51
|
+
logRef.current.log('[usePolling] Fetch result:', { status: result.status, messageCount: result.chatHistory?.length });
|
|
48
52
|
if (!isMountedRef.current)
|
|
49
53
|
return;
|
|
50
54
|
setData(result);
|
|
@@ -52,16 +56,16 @@ function usePolling(chatUid, fetchFn, options = {}) {
|
|
|
52
56
|
onUpdateRef.current?.(result);
|
|
53
57
|
// Check if we should stop polling
|
|
54
58
|
const shouldStop = stopStatusesRef.current.includes(result.status);
|
|
55
|
-
|
|
59
|
+
logRef.current.log('[usePolling] Should stop?', shouldStop, 'stopStatuses:', stopStatusesRef.current, 'current status:', result.status);
|
|
56
60
|
if (shouldStop) {
|
|
57
|
-
|
|
61
|
+
logRef.current.log('[usePolling] Stopping polling due to status:', result.status);
|
|
58
62
|
clearPolling();
|
|
59
63
|
setIsPolling(false);
|
|
60
64
|
onStopRef.current?.(result);
|
|
61
65
|
}
|
|
62
66
|
}
|
|
63
67
|
catch (err) {
|
|
64
|
-
|
|
68
|
+
logRef.current.error('[usePolling] Fetch error:', err);
|
|
65
69
|
if (!isMountedRef.current)
|
|
66
70
|
return;
|
|
67
71
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -92,9 +96,9 @@ function usePolling(chatUid, fetchFn, options = {}) {
|
|
|
92
96
|
}, [fetchData]);
|
|
93
97
|
// Auto-start polling when enabled and chatUid is set
|
|
94
98
|
useEffect(() => {
|
|
95
|
-
|
|
99
|
+
logRef.current.log('[usePolling] Auto-start effect triggered:', { enabled, chatUid, isPollingRef: isPollingRef.current, intervalRef: !!intervalRef.current });
|
|
96
100
|
if (!enabled || !chatUid) {
|
|
97
|
-
|
|
101
|
+
logRef.current.log('[usePolling] Not enabled or no chatUid, stopping if active');
|
|
98
102
|
// Stop polling if disabled or no chatUid
|
|
99
103
|
if (isPollingRef.current) {
|
|
100
104
|
clearPolling();
|
|
@@ -104,7 +108,7 @@ function usePolling(chatUid, fetchFn, options = {}) {
|
|
|
104
108
|
}
|
|
105
109
|
// Start polling if not already polling
|
|
106
110
|
if (!isPollingRef.current) {
|
|
107
|
-
|
|
111
|
+
logRef.current.log('[usePolling] Starting polling, interval:', intervalValueRef.current);
|
|
108
112
|
isPollingRef.current = true;
|
|
109
113
|
setIsPolling(true);
|
|
110
114
|
setError(null);
|
|
@@ -112,10 +116,10 @@ function usePolling(chatUid, fetchFn, options = {}) {
|
|
|
112
116
|
fetchData();
|
|
113
117
|
// Set up interval
|
|
114
118
|
intervalRef.current = setInterval(fetchData, intervalValueRef.current);
|
|
115
|
-
|
|
119
|
+
logRef.current.log('[usePolling] Interval set:', intervalRef.current);
|
|
116
120
|
}
|
|
117
121
|
else {
|
|
118
|
-
|
|
122
|
+
logRef.current.log('[usePolling] Already polling, skipping start');
|
|
119
123
|
}
|
|
120
124
|
// Only cleanup on unmount, not on every dependency change
|
|
121
125
|
}, [enabled, chatUid, fetchData, clearPolling]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePolling.js","sources":["../../../src/hooks/usePolling.ts"],"sourcesContent":["import { useState, useEffect, useRef, useCallback } from 'react';\nimport type { RealtimeChatHistory, RealtimeStatus } from '../api/types';\n\nexport interface UsePollingOptions {\n /**\n * Polling interval in milliseconds\n * @default 1000\n */\n interval?: number;\n\n /**\n * Whether polling is enabled\n * @default true\n */\n enabled?: boolean;\n\n /**\n * Statuses that should stop polling\n * @default ['completed', 'error']\n */\n stopStatuses?: RealtimeStatus[];\n\n /**\n * Callback when polling stops\n */\n onStop?: (data: RealtimeChatHistory | null) => void;\n\n /**\n * Callback on each poll update\n */\n onUpdate?: (data: RealtimeChatHistory) => void;\n\n /**\n * Callback on poll error\n */\n onError?: (error: Error) => void;\n}\n\nexport interface UsePollingResult {\n /**\n * Current polling data\n */\n data: RealtimeChatHistory | null;\n\n /**\n * Whether polling is currently active\n */\n isPolling: boolean;\n\n /**\n * Last error that occurred\n */\n error: Error | null;\n\n /**\n * Start polling\n */\n start: () => void;\n\n /**\n * Stop polling\n */\n stop: () => void;\n\n /**\n * Manually trigger a fetch\n */\n refetch: () => Promise<void>;\n}\n\n/**\n * Hook for polling real-time chat history\n *\n * @param chatUid - The chat UID to poll for\n * @param fetchFn - Function that fetches the realtime history\n * @param options - Polling options\n */\nexport function usePolling(\n chatUid: string | null,\n fetchFn: () => Promise<RealtimeChatHistory>,\n options: UsePollingOptions = {}\n): UsePollingResult {\n const {\n interval = 1000,\n enabled = true,\n stopStatuses = ['completed', 'error'],\n onStop,\n onUpdate,\n onError,\n } = options;\n\n const [data, setData] = useState<RealtimeChatHistory | null>(null);\n const [isPolling, setIsPolling] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);\n const isMountedRef = useRef(true);\n\n // Refs for callbacks and options to avoid stale closures and unnecessary re-renders\n const onStopRef = useRef(onStop);\n const onUpdateRef = useRef(onUpdate);\n const onErrorRef = useRef(onError);\n const fetchFnRef = useRef(fetchFn);\n const stopStatusesRef = useRef(stopStatuses);\n const intervalValueRef = useRef(interval);\n const isPollingRef = useRef(false);\n\n useEffect(() => {\n onStopRef.current = onStop;\n onUpdateRef.current = onUpdate;\n onErrorRef.current = onError;\n fetchFnRef.current = fetchFn;\n stopStatusesRef.current = stopStatuses;\n intervalValueRef.current = interval;\n });\n\n const clearPolling = useCallback(() => {\n if (intervalRef.current) {\n clearInterval(intervalRef.current);\n intervalRef.current = null;\n }\n isPollingRef.current = false;\n }, []);\n\n const fetchData = useCallback(async () => {\n console.log('[usePolling] fetchData called, isMounted:', isMountedRef.current);\n if (!isMountedRef.current) return;\n\n try {\n console.log('[usePolling] Fetching...');\n const result = await fetchFnRef.current();\n console.log('[usePolling] Fetch result:', { status: result.status, messageCount: result.chatHistory?.length });\n\n if (!isMountedRef.current) return;\n\n setData(result);\n setError(null);\n onUpdateRef.current?.(result);\n\n // Check if we should stop polling\n const shouldStop = stopStatusesRef.current.includes(result.status);\n console.log('[usePolling] Should stop?', shouldStop, 'stopStatuses:', stopStatusesRef.current, 'current status:', result.status);\n if (shouldStop) {\n console.log('[usePolling] Stopping polling due to status:', result.status);\n clearPolling();\n setIsPolling(false);\n onStopRef.current?.(result);\n }\n } catch (err) {\n console.error('[usePolling] Fetch error:', err);\n if (!isMountedRef.current) return;\n\n const error = err instanceof Error ? err : new Error(String(err));\n setError(error);\n onErrorRef.current?.(error);\n\n // Stop polling on error\n clearPolling();\n setIsPolling(false);\n }\n }, [clearPolling]);\n\n const start = useCallback(() => {\n if (intervalRef.current) return;\n\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch\n fetchData();\n\n // Set up interval\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n }, [fetchData]);\n\n const stop = useCallback(() => {\n clearPolling();\n setIsPolling(false);\n }, [clearPolling]);\n\n const refetch = useCallback(async () => {\n await fetchData();\n }, [fetchData]);\n\n // Auto-start polling when enabled and chatUid is set\n useEffect(() => {\n console.log('[usePolling] Auto-start effect triggered:', { enabled, chatUid, isPollingRef: isPollingRef.current, intervalRef: !!intervalRef.current });\n\n if (!enabled || !chatUid) {\n console.log('[usePolling] Not enabled or no chatUid, stopping if active');\n // Stop polling if disabled or no chatUid\n if (isPollingRef.current) {\n clearPolling();\n setIsPolling(false);\n }\n return;\n }\n\n // Start polling if not already polling\n if (!isPollingRef.current) {\n console.log('[usePolling] Starting polling, interval:', intervalValueRef.current);\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch\n fetchData();\n\n // Set up interval\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n console.log('[usePolling] Interval set:', intervalRef.current);\n } else {\n console.log('[usePolling] Already polling, skipping start');\n }\n\n // Only cleanup on unmount, not on every dependency change\n }, [enabled, chatUid, fetchData, clearPolling]);\n\n // Cleanup on unmount\n useEffect(() => {\n isMountedRef.current = true;\n return () => {\n isMountedRef.current = false;\n clearPolling();\n };\n }, [clearPolling]);\n\n return {\n data,\n isPolling,\n error,\n start,\n stop,\n refetch,\n };\n}\n"],"names":[],"mappings":";;AAsEA;;;;;;AAMG;AACG,SAAU,UAAU,CACxB,OAAsB,EACtB,OAA2C,EAC3C,UAA6B,EAAE,EAAA;IAE/B,MAAM,EACJ,QAAQ,GAAG,IAAI,EACf,OAAO,GAAG,IA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AO,EAAE;AACxB,YAAA,OAAO,CAAC,GAAG,CAAC,4DAA4D,CAAC;;AAEzE,YAAA,IAAI,YAAY,CAAC,OAAO,EAAE;AACxB,gBAAA,YAAY,EAAE;gBACd,YAAY,CAAC,KAAK,CAAC;YACrB;YACA;QACF;;AAGA,QAAA,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE;YACzB,OAAO,CAAC,GAAG,CAAC,0CAA0C,EAAE,gBAAgB,CAAC,OAAO,CAAC;AACjF,YAAA,YAAY,CAAC,OAAO,GAAG,IAAI;YAC3B,YAAY,CAAC,IAAI,CAAC;YAClB,QAAQ,CAAC,IAAI,CAAC;;AAGd,YAAA,SAAS,EAAE;;YAGX,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,SAAS,EAAE,gBAAgB,CAAC,OAAO,CAAC;YACtE,OAAO,CAAC,GAAG,CAAC,4BAA4B,EAAE,WAAW,CAAC,OAAO,CAAC;QAChE;aAAO;AACL,YAAA,OAAO,CAAC,GAAG,CAAC,8CAA8C,CAAC;QAC7D;;IAGF,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;;IAG/C,SAAS,CAAC,MAAK;AACb,QAAA,YAAY,CAAC,OAAO,GAAG,IAAI;AAC3B,QAAA,OAAO,MAAK;AACV,YAAA,YAAY,CAAC,OAAO,GAAG,KAAK;AAC5B,YAAA,YAAY,EAAE;AAChB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC;IAElB,OAAO;QACL,IAAI;QACJ,SAAS;QACT,KAAK;QACL,KAAK;QACL,IAAI;QACJ,OAAO;KACR;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"usePolling.js","sources":["../../../src/hooks/usePolling.ts"],"sourcesContent":["import { useState, useEffect, useRef, useCallback, useMemo } from 'react';\nimport type { RealtimeChatHistory, RealtimeStatus } from '../api/types';\nimport { createLogger } from '../utils/logger';\n\nexport interface UsePollingOptions {\n /**\n * Polling interval in milliseconds\n * @default 1000\n */\n interval?: number;\n\n /**\n * Whether polling is enabled\n * @default true\n */\n enabled?: boolean;\n\n /**\n * Statuses that should stop polling\n * @default ['completed', 'error']\n */\n stopStatuses?: RealtimeStatus[];\n\n /**\n * Callback when polling stops\n */\n onStop?: (data: RealtimeChatHistory | null) => void;\n\n /**\n * Callback on each poll update\n */\n onUpdate?: (data: RealtimeChatHistory) => void;\n\n /**\n * Callback on poll error\n */\n onError?: (error: Error) => void;\n\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n}\n\nexport interface UsePollingResult {\n /**\n * Current polling data\n */\n data: RealtimeChatHistory | null;\n\n /**\n * Whether polling is currently active\n */\n isPolling: boolean;\n\n /**\n * Last error that occurred\n */\n error: Error | null;\n\n /**\n * Start polling\n */\n start: () => void;\n\n /**\n * Stop polling\n */\n stop: () => void;\n\n /**\n * Manually trigger a fetch\n */\n refetch: () => Promise<void>;\n}\n\n/**\n * Hook for polling real-time chat history\n *\n * @param chatUid - The chat UID to poll for\n * @param fetchFn - Function that fetches the realtime history\n * @param options - Polling options\n */\nexport function usePolling(\n chatUid: string | null,\n fetchFn: () => Promise<RealtimeChatHistory>,\n options: UsePollingOptions = {}\n): UsePollingResult {\n const {\n interval = 1000,\n enabled = true,\n stopStatuses = ['completed', 'error'],\n onStop,\n onUpdate,\n onError,\n debug = false,\n } = options;\n\n const log = useMemo(() => createLogger(debug), [debug]);\n const logRef = useRef(log);\n logRef.current = log;\n\n const [data, setData] = useState<RealtimeChatHistory | null>(null);\n const [isPolling, setIsPolling] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);\n const isMountedRef = useRef(true);\n\n // Refs for callbacks and options to avoid stale closures and unnecessary re-renders\n const onStopRef = useRef(onStop);\n const onUpdateRef = useRef(onUpdate);\n const onErrorRef = useRef(onError);\n const fetchFnRef = useRef(fetchFn);\n const stopStatusesRef = useRef(stopStatuses);\n const intervalValueRef = useRef(interval);\n const isPollingRef = useRef(false);\n\n useEffect(() => {\n onStopRef.current = onStop;\n onUpdateRef.current = onUpdate;\n onErrorRef.current = onError;\n fetchFnRef.current = fetchFn;\n stopStatusesRef.current = stopStatuses;\n intervalValueRef.current = interval;\n });\n\n const clearPolling = useCallback(() => {\n if (intervalRef.current) {\n clearInterval(intervalRef.current);\n intervalRef.current = null;\n }\n isPollingRef.current = false;\n }, []);\n\n const fetchData = useCallback(async () => {\n logRef.current.log('[usePolling] fetchData called, isMounted:', isMountedRef.current);\n if (!isMountedRef.current) return;\n\n try {\n logRef.current.log('[usePolling] Fetching...');\n const result = await fetchFnRef.current();\n logRef.current.log('[usePolling] Fetch result:', { status: result.status, messageCount: result.chatHistory?.length });\n\n if (!isMountedRef.current) return;\n\n setData(result);\n setError(null);\n onUpdateRef.current?.(result);\n\n // Check if we should stop polling\n const shouldStop = stopStatusesRef.current.includes(result.status);\n logRef.current.log('[usePolling] Should stop?', shouldStop, 'stopStatuses:', stopStatusesRef.current, 'current status:', result.status);\n if (shouldStop) {\n logRef.current.log('[usePolling] Stopping polling due to status:', result.status);\n clearPolling();\n setIsPolling(false);\n onStopRef.current?.(result);\n }\n } catch (err) {\n logRef.current.error('[usePolling] Fetch error:', err);\n if (!isMountedRef.current) return;\n\n const error = err instanceof Error ? err : new Error(String(err));\n setError(error);\n onErrorRef.current?.(error);\n\n // Stop polling on error\n clearPolling();\n setIsPolling(false);\n }\n }, [clearPolling]);\n\n const start = useCallback(() => {\n if (intervalRef.current) return;\n\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch\n fetchData();\n\n // Set up interval\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n }, [fetchData]);\n\n const stop = useCallback(() => {\n clearPolling();\n setIsPolling(false);\n }, [clearPolling]);\n\n const refetch = useCallback(async () => {\n await fetchData();\n }, [fetchData]);\n\n // Auto-start polling when enabled and chatUid is set\n useEffect(() => {\n logRef.current.log('[usePolling] Auto-start effect triggered:', { enabled, chatUid, isPollingRef: isPollingRef.current, intervalRef: !!intervalRef.current });\n\n if (!enabled || !chatUid) {\n logRef.current.log('[usePolling] Not enabled or no chatUid, stopping if active');\n // Stop polling if disabled or no chatUid\n if (isPollingRef.current) {\n clearPolling();\n setIsPolling(false);\n }\n return;\n }\n\n // Start polling if not already polling\n if (!isPollingRef.current) {\n logRef.current.log('[usePolling] Starting polling, interval:', intervalValueRef.current);\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch\n fetchData();\n\n // Set up interval\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n logRef.current.log('[usePolling] Interval set:', intervalRef.current);\n } else {\n logRef.current.log('[usePolling] Already polling, skipping start');\n }\n\n // Only cleanup on unmount, not on every dependency change\n }, [enabled, chatUid, fetchData, clearPolling]);\n\n // Cleanup on unmount\n useEffect(() => {\n isMountedRef.current = true;\n return () => {\n isMountedRef.current = false;\n clearPolling();\n };\n }, [clearPolling]);\n\n return {\n data,\n isPolling,\n error,\n start,\n stop,\n refetch,\n };\n}\n"],"names":[],"mappings":";;;AA6EA;;;;;;AAMG;AACG,SAAU,UAAU,CACxB,OAAsB,EACtB,OAA2C,EAC3C,UAA6B,EAAE,EAAA;AAE/B,IAAA,MAAM,EACJ,QAAQ,GAAG,IAAI,EACf,OAAO,GAAG,IAAI,EACd,YAAY,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,EACrC,MAAM,EACN,QAAQ,EACR,OAAO,EACP,KAAK,GAAG,KAAK,GACd,GAAG,OAAO;AAEX,IAAA,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;AACvD,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC;AAC1B,IAAA,MAAM,CAAC,OAAO,GAAG,GAAG;IAEpB,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,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I,YAAY,CAAC,OAAO,EAAE;AACxB,gBAAA,YAAY,EAAE;gBACd,YAAY,CAAC,KAAK,CAAC;YACrB;YACA;QACF;;AAGA,QAAA,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE;YACzB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,0CAA0C,EAAE,gBAAgB,CAAC,OAAO,CAAC;AACxF,YAAA,YAAY,CAAC,OAAO,GAAG,IAAI;YAC3B,YAAY,CAAC,IAAI,CAAC;YAClB,QAAQ,CAAC,IAAI,CAAC;;AAGd,YAAA,SAAS,EAAE;;YAGX,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,SAAS,EAAE,gBAAgB,CAAC,OAAO,CAAC;YACtE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,4BAA4B,EAAE,WAAW,CAAC,OAAO,CAAC;QACvE;aAAO;AACL,YAAA,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,8CAA8C,CAAC;QACpE;;IAGF,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;;IAG/C,SAAS,CAAC,MAAK;AACb,QAAA,YAAY,CAAC,OAAO,GAAG,IAAI;AAC3B,QAAA,OAAO,MAAK;AACV,YAAA,YAAY,CAAC,OAAO,GAAG,KAAK;AAC5B,YAAA,YAAY,EAAE;AAChB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC;IAElB,OAAO;QACL,IAAI;QACJ,SAAS;QACT,KAAK;QACL,KAAK;QACL,IAAI;QACJ,OAAO;KACR;AACH;;;;"}
|
|
@@ -14,4 +14,4 @@ import type { DevicProviderProps } from './types';
|
|
|
14
14
|
* </DevicProvider>
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
export declare function DevicProvider({ apiKey, baseUrl, tenantId, tenantMetadata, children, }: DevicProviderProps): JSX.Element;
|
|
17
|
+
export declare function DevicProvider({ apiKey, baseUrl, tenantId, tenantMetadata, debug, children, }: DevicProviderProps): JSX.Element;
|
|
@@ -19,7 +19,7 @@ const DEFAULT_BASE_URL = 'https://api.devic.ai';
|
|
|
19
19
|
* </DevicProvider>
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
|
-
function DevicProvider({ apiKey, baseUrl = DEFAULT_BASE_URL, tenantId, tenantMetadata, children, }) {
|
|
22
|
+
function DevicProvider({ apiKey, baseUrl = DEFAULT_BASE_URL, tenantId, tenantMetadata, debug, children, }) {
|
|
23
23
|
const contextValue = useMemo(() => {
|
|
24
24
|
const client = new DevicApiClient({
|
|
25
25
|
apiKey,
|
|
@@ -32,8 +32,9 @@ function DevicProvider({ apiKey, baseUrl = DEFAULT_BASE_URL, tenantId, tenantMet
|
|
|
32
32
|
tenantId,
|
|
33
33
|
tenantMetadata,
|
|
34
34
|
isConfigured: !!apiKey,
|
|
35
|
+
debug,
|
|
35
36
|
};
|
|
36
|
-
}, [apiKey, baseUrl, tenantId, tenantMetadata]);
|
|
37
|
+
}, [apiKey, baseUrl, tenantId, tenantMetadata, debug]);
|
|
37
38
|
return (jsx(DevicContext.Provider, { value: contextValue, children: children }));
|
|
38
39
|
}
|
|
39
40
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DevicProvider.js","sources":["../../../src/provider/DevicProvider.tsx"],"sourcesContent":["import React, { useMemo } from 'react';\nimport { DevicContext } from './DevicContext';\nimport { DevicApiClient } from '../api/client';\nimport type { DevicProviderProps, DevicContextValue } from './types';\n\nconst DEFAULT_BASE_URL = 'https://api.devic.ai';\n\n/**\n * Provider component for Devic UI configuration\n *\n * @example\n * ```tsx\n * <DevicProvider\n * apiKey=\"devic-xxx\"\n * baseUrl=\"https://api.devic.ai\"\n * tenantId=\"tenant-123\"\n * tenantMetadata={{ userId: '456' }}\n * >\n * <App />\n * </DevicProvider>\n * ```\n */\nexport function DevicProvider({\n apiKey,\n baseUrl = DEFAULT_BASE_URL,\n tenantId,\n tenantMetadata,\n children,\n}: DevicProviderProps): JSX.Element {\n const contextValue = useMemo<DevicContextValue>(() => {\n const client = new DevicApiClient({\n apiKey,\n baseUrl,\n });\n\n return {\n client,\n apiKey,\n baseUrl,\n tenantId,\n tenantMetadata,\n isConfigured: !!apiKey,\n };\n }, [apiKey, baseUrl, tenantId, tenantMetadata]);\n\n return (\n <DevicContext.Provider value={contextValue}>\n {children}\n </DevicContext.Provider>\n );\n}\n"],"names":["_jsx"],"mappings":";;;;;AAKA,MAAM,gBAAgB,GAAG,sBAAsB;AAE/C;;;;;;;;;;;;;;AAcG;
|
|
1
|
+
{"version":3,"file":"DevicProvider.js","sources":["../../../src/provider/DevicProvider.tsx"],"sourcesContent":["import React, { useMemo } from 'react';\nimport { DevicContext } from './DevicContext';\nimport { DevicApiClient } from '../api/client';\nimport type { DevicProviderProps, DevicContextValue } from './types';\n\nconst DEFAULT_BASE_URL = 'https://api.devic.ai';\n\n/**\n * Provider component for Devic UI configuration\n *\n * @example\n * ```tsx\n * <DevicProvider\n * apiKey=\"devic-xxx\"\n * baseUrl=\"https://api.devic.ai\"\n * tenantId=\"tenant-123\"\n * tenantMetadata={{ userId: '456' }}\n * >\n * <App />\n * </DevicProvider>\n * ```\n */\nexport function DevicProvider({\n apiKey,\n baseUrl = DEFAULT_BASE_URL,\n tenantId,\n tenantMetadata,\n debug,\n children,\n}: DevicProviderProps): JSX.Element {\n const contextValue = useMemo<DevicContextValue>(() => {\n const client = new DevicApiClient({\n apiKey,\n baseUrl,\n });\n\n return {\n client,\n apiKey,\n baseUrl,\n tenantId,\n tenantMetadata,\n isConfigured: !!apiKey,\n debug,\n };\n }, [apiKey, baseUrl, tenantId, tenantMetadata, debug]);\n\n return (\n <DevicContext.Provider value={contextValue}>\n {children}\n </DevicContext.Provider>\n );\n}\n"],"names":["_jsx"],"mappings":";;;;;AAKA,MAAM,gBAAgB,GAAG,sBAAsB;AAE/C;;;;;;;;;;;;;;AAcG;SACa,aAAa,CAAC,EAC5B,MAAM,EACN,OAAO,GAAG,gBAAgB,EAC1B,QAAQ,EACR,cAAc,EACd,KAAK,EACL,QAAQ,GACW,EAAA;AACnB,IAAA,MAAM,YAAY,GAAG,OAAO,CAAoB,MAAK;AACnD,QAAA,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC;YAChC,MAAM;YACN,OAAO;AACR,SAAA,CAAC;QAEF,OAAO;YACL,MAAM;YACN,MAAM;YACN,OAAO;YACP,QAAQ;YACR,cAAc;YACd,YAAY,EAAE,CAAC,CAAC,MAAM;YACtB,KAAK;SACN;AACH,IAAA,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,KAAK,CAAC,CAAC;AAEtD,IAAA,QACEA,GAAA,CAAC,YAAY,CAAC,QAAQ,EAAA,EAAC,KAAK,EAAE,YAAY,EAAA,QAAA,EACvC,QAAQ,EAAA,CACa;AAE5B;;;;"}
|
|
@@ -20,6 +20,11 @@ export interface DevicProviderConfig {
|
|
|
20
20
|
* Global tenant metadata
|
|
21
21
|
*/
|
|
22
22
|
tenantMetadata?: Record<string, any>;
|
|
23
|
+
/**
|
|
24
|
+
* Enable debug logging to the browser console
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
debug?: boolean;
|
|
23
28
|
}
|
|
24
29
|
/**
|
|
25
30
|
* Context value provided by DevicProvider
|
|
@@ -49,6 +54,10 @@ export interface DevicContextValue {
|
|
|
49
54
|
* Whether the provider is properly configured
|
|
50
55
|
*/
|
|
51
56
|
isConfigured: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Whether debug logging is enabled
|
|
59
|
+
*/
|
|
60
|
+
debug?: boolean;
|
|
52
61
|
}
|
|
53
62
|
/**
|
|
54
63
|
* Props for the DevicProvider component
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/utils/index.ts"],"sourcesContent":["export { segmentToolCalls } from './toolGroups';\nexport type { ToolGroupSegment } from './toolGroups';\n\n/**\n * Generate a unique ID\n */\nexport function generateId(): string {\n return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;\n}\n\n/**\n * Deep merge two objects\n */\nexport function deepMerge<T extends Record<string, any>>(\n target: T,\n source: Partial<T>\n): T {\n const result = { ...target };\n\n for (const key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n const sourceValue = source[key];\n const targetValue = result[key];\n\n if (\n sourceValue &&\n typeof sourceValue === 'object' &&\n !Array.isArray(sourceValue) &&\n targetValue &&\n typeof targetValue === 'object' &&\n !Array.isArray(targetValue)\n ) {\n result[key] = deepMerge(targetValue, sourceValue as any);\n } else if (sourceValue !== undefined) {\n result[key] = sourceValue as any;\n }\n }\n }\n\n return result;\n}\n\n/**\n * Debounce a function\n */\nexport function debounce<T extends (...args: any[]) => any>(\n fn: T,\n delay: number\n): (...args: Parameters<T>) => void {\n let timeoutId: ReturnType<typeof setTimeout>;\n\n return (...args: Parameters<T>) => {\n clearTimeout(timeoutId);\n timeoutId = setTimeout(() => fn(...args), delay);\n };\n}\n\n/**\n * Throttle a function\n */\nexport function throttle<T extends (...args: any[]) => any>(\n fn: T,\n delay: number\n): (...args: Parameters<T>) => void {\n let lastCall = 0;\n\n return (...args: Parameters<T>) => {\n const now = Date.now();\n if (now - lastCall >= delay) {\n lastCall = now;\n fn(...args);\n }\n };\n}\n\n/**\n * Format file size for display\n */\nexport function formatFileSize(bytes: number): string {\n if (bytes === 0) return '0 B';\n\n const units = ['B', 'KB', 'MB', 'GB'];\n const k = 1024;\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n\n return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${units[i]}`;\n}\n\n/**\n * Check if running in browser\n */\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\n/**\n * Local storage helper with JSON serialization\n */\nexport const storage = {\n get<T>(key: string, defaultValue?: T): T | null {\n if (!isBrowser()) return defaultValue ?? null;\n\n try {\n const item = localStorage.getItem(key);\n return item ? JSON.parse(item) : (defaultValue ?? null);\n } catch {\n return defaultValue ?? null;\n }\n },\n\n set<T>(key: string, value: T): void {\n if (!isBrowser()) return;\n\n try {\n localStorage.setItem(key, JSON.stringify(value));\n } catch {\n // Storage full or other error\n }\n },\n\n remove(key: string): void {\n if (!isBrowser()) return;\n\n try {\n localStorage.removeItem(key);\n } catch {\n // Error removing item\n }\n },\n};\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/utils/index.ts"],"sourcesContent":["export { segmentToolCalls } from './toolGroups';\nexport { createLogger } from './logger';\nexport type { DevicLogger } from './logger';\nexport type { ToolGroupSegment } from './toolGroups';\n\n/**\n * Generate a unique ID\n */\nexport function generateId(): string {\n return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;\n}\n\n/**\n * Deep merge two objects\n */\nexport function deepMerge<T extends Record<string, any>>(\n target: T,\n source: Partial<T>\n): T {\n const result = { ...target };\n\n for (const key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n const sourceValue = source[key];\n const targetValue = result[key];\n\n if (\n sourceValue &&\n typeof sourceValue === 'object' &&\n !Array.isArray(sourceValue) &&\n targetValue &&\n typeof targetValue === 'object' &&\n !Array.isArray(targetValue)\n ) {\n result[key] = deepMerge(targetValue, sourceValue as any);\n } else if (sourceValue !== undefined) {\n result[key] = sourceValue as any;\n }\n }\n }\n\n return result;\n}\n\n/**\n * Debounce a function\n */\nexport function debounce<T extends (...args: any[]) => any>(\n fn: T,\n delay: number\n): (...args: Parameters<T>) => void {\n let timeoutId: ReturnType<typeof setTimeout>;\n\n return (...args: Parameters<T>) => {\n clearTimeout(timeoutId);\n timeoutId = setTimeout(() => fn(...args), delay);\n };\n}\n\n/**\n * Throttle a function\n */\nexport function throttle<T extends (...args: any[]) => any>(\n fn: T,\n delay: number\n): (...args: Parameters<T>) => void {\n let lastCall = 0;\n\n return (...args: Parameters<T>) => {\n const now = Date.now();\n if (now - lastCall >= delay) {\n lastCall = now;\n fn(...args);\n }\n };\n}\n\n/**\n * Format file size for display\n */\nexport function formatFileSize(bytes: number): string {\n if (bytes === 0) return '0 B';\n\n const units = ['B', 'KB', 'MB', 'GB'];\n const k = 1024;\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n\n return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${units[i]}`;\n}\n\n/**\n * Check if running in browser\n */\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\n/**\n * Local storage helper with JSON serialization\n */\nexport const storage = {\n get<T>(key: string, defaultValue?: T): T | null {\n if (!isBrowser()) return defaultValue ?? null;\n\n try {\n const item = localStorage.getItem(key);\n return item ? JSON.parse(item) : (defaultValue ?? null);\n } catch {\n return defaultValue ?? null;\n }\n },\n\n set<T>(key: string, value: T): void {\n if (!isBrowser()) return;\n\n try {\n localStorage.setItem(key, JSON.stringify(value));\n } catch {\n // Storage full or other error\n }\n },\n\n remove(key: string): void {\n if (!isBrowser()) return;\n\n try {\n localStorage.removeItem(key);\n } catch {\n // Error removing item\n }\n },\n};\n"],"names":[],"mappings":"AAKA;;AAEG;SACa,UAAU,GAAA;IACxB,OAAO,CAAA,EAAG,IAAI,CAAC,GAAG,EAAE,CAAA,CAAA,EAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA,CAAE;AACtE;AAEA;;AAEG;AACG,SAAU,SAAS,CACvB,MAAS,EACT,MAAkB,EAAA;AAElB,IAAA,MAAM,MAAM,GAAG,EAAE,GAAG,MAAM,EAAE;AAE5B,IAAA,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;AACxB,QAAA,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE;AACrD,YAAA,MAAM,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC;AAC/B,YAAA,MAAM,WAAW,GAAG,MAAM,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M;;QAER;IACF,CAAC;;;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conditional logger for devic-ui.
|
|
3
|
+
* Only outputs when debug mode is enabled via the Provider or component props.
|
|
4
|
+
*/
|
|
5
|
+
export declare function createLogger(enabled: boolean): {
|
|
6
|
+
log: (...data: any[]) => void;
|
|
7
|
+
warn: (...data: any[]) => void;
|
|
8
|
+
error: (...data: any[]) => void;
|
|
9
|
+
};
|
|
10
|
+
export type DevicLogger = ReturnType<typeof createLogger>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conditional logger for devic-ui.
|
|
3
|
+
* Only outputs when debug mode is enabled via the Provider or component props.
|
|
4
|
+
*/
|
|
5
|
+
function createLogger(enabled) {
|
|
6
|
+
const noop = () => { };
|
|
7
|
+
return {
|
|
8
|
+
log: enabled ? console.log.bind(console) : noop,
|
|
9
|
+
warn: enabled ? console.warn.bind(console) : noop,
|
|
10
|
+
error: enabled ? console.error.bind(console) : noop,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { createLogger };
|
|
15
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sources":["../../../src/utils/logger.ts"],"sourcesContent":["/**\n * Conditional logger for devic-ui.\n * Only outputs when debug mode is enabled via the Provider or component props.\n */\nexport function createLogger(enabled: boolean) {\n const noop = () => {};\n return {\n log: enabled ? console.log.bind(console) : noop,\n warn: enabled ? console.warn.bind(console) : noop,\n error: enabled ? console.error.bind(console) : noop,\n };\n}\n\nexport type DevicLogger = ReturnType<typeof createLogger>;\n"],"names":[],"mappings":"AAAA;;;AAGG;AACG,SAAU,YAAY,CAAC,OAAgB,EAAA;AAC3C,IAAA,MAAM,IAAI,GAAG,MAAK,EAAE,CAAC;IACrB,OAAO;AACL,QAAA,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI;AAC/C,QAAA,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI;AACjD,QAAA,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI;KACpD;AACH;;;;"}
|