@yourgpt/copilot-sdk 0.1.0 → 1.0.0
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/README.md +25 -23
- package/dist/ThreadManager-BCVt-_k_.d.cts +863 -0
- package/dist/ThreadManager-BjC15mh8.d.ts +863 -0
- package/dist/{chunk-2ZCWVAAK.cjs → chunk-42YQ4ATO.cjs} +889 -2
- package/dist/chunk-42YQ4ATO.cjs.map +1 -0
- package/dist/{chunk-W6KQT7YZ.cjs → chunk-BN75ZW24.cjs} +482 -46
- package/dist/chunk-BN75ZW24.cjs.map +1 -0
- package/dist/{chunk-N4OA2J32.js → chunk-QSEGNATZ.js} +882 -3
- package/dist/chunk-QSEGNATZ.js.map +1 -0
- package/dist/{chunk-QUGTRQSS.js → chunk-XAVZZVUL.js} +455 -24
- package/dist/chunk-XAVZZVUL.js.map +1 -0
- package/dist/core/index.cjs +99 -71
- package/dist/core/index.d.cts +13 -4
- package/dist/core/index.d.ts +13 -4
- package/dist/core/index.js +1 -1
- package/dist/react/index.cjs +66 -34
- package/dist/react/index.d.cts +395 -7
- package/dist/react/index.d.ts +395 -7
- package/dist/react/index.js +2 -2
- package/dist/{thread-C2FjuGLb.d.cts → types-BtAaOV07.d.cts} +214 -478
- package/dist/{thread-C2FjuGLb.d.ts → types-BtAaOV07.d.ts} +214 -478
- package/dist/ui/index.cjs +1710 -384
- package/dist/ui/index.cjs.map +1 -1
- package/dist/ui/index.d.cts +399 -42
- package/dist/ui/index.d.ts +399 -42
- package/dist/ui/index.js +1706 -384
- package/dist/ui/index.js.map +1 -1
- package/package.json +9 -8
- package/dist/chunk-2ZCWVAAK.cjs.map +0 -1
- package/dist/chunk-N4OA2J32.js.map +0 -1
- package/dist/chunk-QUGTRQSS.js.map +0 -1
- package/dist/chunk-W6KQT7YZ.cjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-
|
|
1
|
+
import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-QSEGNATZ.js';
|
|
2
2
|
import { createContext, useContext, useCallback, useEffect, useState, useRef, useSyncExternalStore, useMemo } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
import * as z from 'zod';
|
|
@@ -822,7 +822,7 @@ var AbstractAgentLoop = class {
|
|
|
822
822
|
this._isProcessing = false;
|
|
823
823
|
// Registered tools
|
|
824
824
|
this.registeredTools = /* @__PURE__ */ new Map();
|
|
825
|
-
// Pending approvals
|
|
825
|
+
// Pending approvals - resolve with approval result including extraData
|
|
826
826
|
this.pendingApprovals = /* @__PURE__ */ new Map();
|
|
827
827
|
this.config = config;
|
|
828
828
|
this.callbacks = callbacks;
|
|
@@ -968,15 +968,18 @@ var AbstractAgentLoop = class {
|
|
|
968
968
|
});
|
|
969
969
|
return errorResult;
|
|
970
970
|
}
|
|
971
|
+
let approvalData;
|
|
971
972
|
if (tool.needsApproval && !this.config.autoApprove) {
|
|
972
|
-
typeof tool.approvalMessage === "function" ? tool.approvalMessage(toolCall.args) : tool.approvalMessage;
|
|
973
|
+
const approvalMessage = typeof tool.approvalMessage === "function" ? tool.approvalMessage(toolCall.args) : tool.approvalMessage;
|
|
973
974
|
execution.approvalStatus = "required";
|
|
975
|
+
execution.approvalMessage = approvalMessage;
|
|
974
976
|
this.updateToolExecution(toolCall.id, {
|
|
975
|
-
approvalStatus: "required"
|
|
977
|
+
approvalStatus: "required",
|
|
978
|
+
approvalMessage
|
|
976
979
|
});
|
|
977
980
|
this.callbacks.onApprovalRequired?.(execution);
|
|
978
|
-
const
|
|
979
|
-
if (!approved) {
|
|
981
|
+
const approvalResult = await this.waitForApproval(toolCall.id, execution);
|
|
982
|
+
if (!approvalResult.approved) {
|
|
980
983
|
const rejectedResult = {
|
|
981
984
|
toolCallId: toolCall.id,
|
|
982
985
|
success: false,
|
|
@@ -990,9 +993,7 @@ var AbstractAgentLoop = class {
|
|
|
990
993
|
});
|
|
991
994
|
return rejectedResult;
|
|
992
995
|
}
|
|
993
|
-
|
|
994
|
-
approvalStatus: "approved"
|
|
995
|
-
});
|
|
996
|
+
approvalData = approvalResult.extraData;
|
|
996
997
|
}
|
|
997
998
|
this.updateToolExecution(toolCall.id, { status: "executing" });
|
|
998
999
|
try {
|
|
@@ -1000,7 +1001,8 @@ var AbstractAgentLoop = class {
|
|
|
1000
1001
|
throw new Error(`Tool "${toolCall.name}" has no handler`);
|
|
1001
1002
|
}
|
|
1002
1003
|
const result = await tool.handler(toolCall.args, {
|
|
1003
|
-
data: { toolCallId: toolCall.id }
|
|
1004
|
+
data: { toolCallId: toolCall.id },
|
|
1005
|
+
approvalData
|
|
1004
1006
|
});
|
|
1005
1007
|
this.updateToolExecution(toolCall.id, {
|
|
1006
1008
|
status: "completed",
|
|
@@ -1034,6 +1036,7 @@ var AbstractAgentLoop = class {
|
|
|
1034
1036
|
}
|
|
1035
1037
|
/**
|
|
1036
1038
|
* Wait for user approval
|
|
1039
|
+
* Returns approval result with optional extraData from user's action
|
|
1037
1040
|
*/
|
|
1038
1041
|
waitForApproval(executionId, execution) {
|
|
1039
1042
|
return new Promise((resolve) => {
|
|
@@ -1044,13 +1047,17 @@ var AbstractAgentLoop = class {
|
|
|
1044
1047
|
// Actions (implements AgentLoopActions)
|
|
1045
1048
|
// ============================================
|
|
1046
1049
|
/**
|
|
1047
|
-
* Approve a tool execution
|
|
1050
|
+
* Approve a tool execution with optional extra data
|
|
1048
1051
|
*/
|
|
1049
|
-
approveToolExecution(executionId, _permissionLevel) {
|
|
1052
|
+
approveToolExecution(executionId, extraData, _permissionLevel) {
|
|
1050
1053
|
const pending = this.pendingApprovals.get(executionId);
|
|
1051
1054
|
if (pending) {
|
|
1052
|
-
pending.resolve(true);
|
|
1055
|
+
pending.resolve({ approved: true, extraData });
|
|
1053
1056
|
this.pendingApprovals.delete(executionId);
|
|
1057
|
+
this.updateToolExecution(executionId, {
|
|
1058
|
+
approvalStatus: "approved",
|
|
1059
|
+
approvalData: extraData
|
|
1060
|
+
});
|
|
1054
1061
|
}
|
|
1055
1062
|
}
|
|
1056
1063
|
/**
|
|
@@ -1064,7 +1071,7 @@ var AbstractAgentLoop = class {
|
|
|
1064
1071
|
error: reason
|
|
1065
1072
|
});
|
|
1066
1073
|
}
|
|
1067
|
-
pending.resolve(false);
|
|
1074
|
+
pending.resolve({ approved: false });
|
|
1068
1075
|
this.pendingApprovals.delete(executionId);
|
|
1069
1076
|
}
|
|
1070
1077
|
}
|
|
@@ -1108,8 +1115,8 @@ var AbstractAgentLoop = class {
|
|
|
1108
1115
|
* Dispose of resources
|
|
1109
1116
|
*/
|
|
1110
1117
|
dispose() {
|
|
1111
|
-
for (const [
|
|
1112
|
-
pending.resolve(false);
|
|
1118
|
+
for (const [_id, pending] of this.pendingApprovals) {
|
|
1119
|
+
pending.resolve({ approved: false });
|
|
1113
1120
|
}
|
|
1114
1121
|
this.pendingApprovals.clear();
|
|
1115
1122
|
this.registeredTools.clear();
|
|
@@ -1299,10 +1306,10 @@ var ChatWithTools = class {
|
|
|
1299
1306
|
// Tool Approval
|
|
1300
1307
|
// ============================================
|
|
1301
1308
|
/**
|
|
1302
|
-
* Approve a tool execution
|
|
1309
|
+
* Approve a tool execution with optional extra data
|
|
1303
1310
|
*/
|
|
1304
|
-
approveToolExecution(id, permissionLevel) {
|
|
1305
|
-
this.agentLoop.approveToolExecution(id, permissionLevel);
|
|
1311
|
+
approveToolExecution(id, extraData, permissionLevel) {
|
|
1312
|
+
this.agentLoop.approveToolExecution(id, extraData, permissionLevel);
|
|
1306
1313
|
}
|
|
1307
1314
|
/**
|
|
1308
1315
|
* Reject a tool execution
|
|
@@ -1645,8 +1652,8 @@ function CopilotProvider({
|
|
|
1645
1652
|
chatRef.current?.unregisterTool(name);
|
|
1646
1653
|
}, []);
|
|
1647
1654
|
const approveToolExecution = useCallback(
|
|
1648
|
-
(id, permissionLevel) => {
|
|
1649
|
-
chatRef.current?.approveToolExecution(id, permissionLevel);
|
|
1655
|
+
(id, extraData, permissionLevel) => {
|
|
1656
|
+
chatRef.current?.approveToolExecution(id, extraData, permissionLevel);
|
|
1650
1657
|
},
|
|
1651
1658
|
[]
|
|
1652
1659
|
);
|
|
@@ -1713,6 +1720,9 @@ function CopilotProvider({
|
|
|
1713
1720
|
const clearMessages = useCallback(() => {
|
|
1714
1721
|
chatRef.current?.clearMessages();
|
|
1715
1722
|
}, []);
|
|
1723
|
+
const setMessages = useCallback((messages2) => {
|
|
1724
|
+
chatRef.current?.setMessages(messages2);
|
|
1725
|
+
}, []);
|
|
1716
1726
|
const regenerate = useCallback(async (messageId) => {
|
|
1717
1727
|
await chatRef.current?.regenerate(messageId);
|
|
1718
1728
|
}, []);
|
|
@@ -1754,6 +1764,7 @@ function CopilotProvider({
|
|
|
1754
1764
|
sendMessage,
|
|
1755
1765
|
stop,
|
|
1756
1766
|
clearMessages,
|
|
1767
|
+
setMessages,
|
|
1757
1768
|
regenerate,
|
|
1758
1769
|
// Tool execution
|
|
1759
1770
|
registerTool,
|
|
@@ -1783,6 +1794,7 @@ function CopilotProvider({
|
|
|
1783
1794
|
sendMessage,
|
|
1784
1795
|
stop,
|
|
1785
1796
|
clearMessages,
|
|
1797
|
+
setMessages,
|
|
1786
1798
|
regenerate,
|
|
1787
1799
|
registerTool,
|
|
1788
1800
|
unregisterTool,
|
|
@@ -2847,6 +2859,425 @@ function useDevLogger() {
|
|
|
2847
2859
|
]);
|
|
2848
2860
|
}
|
|
2849
2861
|
|
|
2862
|
+
// src/react/internal/ReactThreadManagerState.ts
|
|
2863
|
+
var ReactThreadManagerState = class {
|
|
2864
|
+
constructor(initialThreads) {
|
|
2865
|
+
this._threads = [];
|
|
2866
|
+
this._currentThreadId = null;
|
|
2867
|
+
this._currentThread = null;
|
|
2868
|
+
this._loadStatus = "idle";
|
|
2869
|
+
this._error = void 0;
|
|
2870
|
+
// Callbacks for React subscriptions (useSyncExternalStore)
|
|
2871
|
+
this.subscribers = /* @__PURE__ */ new Set();
|
|
2872
|
+
// ============================================
|
|
2873
|
+
// Subscription (for useSyncExternalStore)
|
|
2874
|
+
// ============================================
|
|
2875
|
+
/**
|
|
2876
|
+
* Subscribe to state changes.
|
|
2877
|
+
* Returns an unsubscribe function.
|
|
2878
|
+
*
|
|
2879
|
+
* @example
|
|
2880
|
+
* ```tsx
|
|
2881
|
+
* const threads = useSyncExternalStore(
|
|
2882
|
+
* state.subscribe,
|
|
2883
|
+
* () => state.threads
|
|
2884
|
+
* );
|
|
2885
|
+
* ```
|
|
2886
|
+
*/
|
|
2887
|
+
this.subscribe = (callback) => {
|
|
2888
|
+
this.subscribers.add(callback);
|
|
2889
|
+
return () => {
|
|
2890
|
+
this.subscribers.delete(callback);
|
|
2891
|
+
};
|
|
2892
|
+
};
|
|
2893
|
+
if (initialThreads) {
|
|
2894
|
+
this._threads = initialThreads;
|
|
2895
|
+
}
|
|
2896
|
+
}
|
|
2897
|
+
// ============================================
|
|
2898
|
+
// Getters
|
|
2899
|
+
// ============================================
|
|
2900
|
+
get threads() {
|
|
2901
|
+
return this._threads;
|
|
2902
|
+
}
|
|
2903
|
+
get currentThreadId() {
|
|
2904
|
+
return this._currentThreadId;
|
|
2905
|
+
}
|
|
2906
|
+
get currentThread() {
|
|
2907
|
+
return this._currentThread;
|
|
2908
|
+
}
|
|
2909
|
+
get loadStatus() {
|
|
2910
|
+
return this._loadStatus;
|
|
2911
|
+
}
|
|
2912
|
+
get error() {
|
|
2913
|
+
return this._error;
|
|
2914
|
+
}
|
|
2915
|
+
// ============================================
|
|
2916
|
+
// Setters (trigger reactivity)
|
|
2917
|
+
// ============================================
|
|
2918
|
+
set threads(value) {
|
|
2919
|
+
this._threads = value;
|
|
2920
|
+
this.notify();
|
|
2921
|
+
}
|
|
2922
|
+
// ============================================
|
|
2923
|
+
// Mutations
|
|
2924
|
+
// ============================================
|
|
2925
|
+
setThreads(threads) {
|
|
2926
|
+
this._threads = threads;
|
|
2927
|
+
this.notify();
|
|
2928
|
+
}
|
|
2929
|
+
setCurrentThread(thread) {
|
|
2930
|
+
this._currentThread = thread;
|
|
2931
|
+
this._currentThreadId = thread?.id ?? null;
|
|
2932
|
+
this.notify();
|
|
2933
|
+
}
|
|
2934
|
+
setCurrentThreadId(id) {
|
|
2935
|
+
this._currentThreadId = id;
|
|
2936
|
+
this.notify();
|
|
2937
|
+
}
|
|
2938
|
+
addThread(thread) {
|
|
2939
|
+
this._threads = [thread, ...this._threads];
|
|
2940
|
+
this.notify();
|
|
2941
|
+
}
|
|
2942
|
+
updateThread(id, updates) {
|
|
2943
|
+
this._threads = this._threads.map(
|
|
2944
|
+
(t) => t.id === id ? { ...t, ...updates } : t
|
|
2945
|
+
);
|
|
2946
|
+
if (updates.updatedAt) {
|
|
2947
|
+
this._threads = [...this._threads].sort(
|
|
2948
|
+
(a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()
|
|
2949
|
+
);
|
|
2950
|
+
}
|
|
2951
|
+
if (this._currentThread?.id === id) {
|
|
2952
|
+
this._currentThread = { ...this._currentThread, ...updates };
|
|
2953
|
+
}
|
|
2954
|
+
this.notify();
|
|
2955
|
+
}
|
|
2956
|
+
removeThread(id) {
|
|
2957
|
+
this._threads = this._threads.filter((t) => t.id !== id);
|
|
2958
|
+
if (this._currentThreadId === id) {
|
|
2959
|
+
this._currentThreadId = null;
|
|
2960
|
+
this._currentThread = null;
|
|
2961
|
+
}
|
|
2962
|
+
this.notify();
|
|
2963
|
+
}
|
|
2964
|
+
setLoadStatus(status) {
|
|
2965
|
+
this._loadStatus = status;
|
|
2966
|
+
this.notify();
|
|
2967
|
+
}
|
|
2968
|
+
setError(error) {
|
|
2969
|
+
this._error = error;
|
|
2970
|
+
this.notify();
|
|
2971
|
+
}
|
|
2972
|
+
// ============================================
|
|
2973
|
+
// Snapshots (for useSyncExternalStore)
|
|
2974
|
+
// ============================================
|
|
2975
|
+
getThreadsSnapshot() {
|
|
2976
|
+
return this._threads;
|
|
2977
|
+
}
|
|
2978
|
+
getCurrentThreadSnapshot() {
|
|
2979
|
+
return this._currentThread;
|
|
2980
|
+
}
|
|
2981
|
+
getLoadStatusSnapshot() {
|
|
2982
|
+
return this._loadStatus;
|
|
2983
|
+
}
|
|
2984
|
+
getErrorSnapshot() {
|
|
2985
|
+
return this._error;
|
|
2986
|
+
}
|
|
2987
|
+
// ============================================
|
|
2988
|
+
// Private Methods
|
|
2989
|
+
// ============================================
|
|
2990
|
+
notify() {
|
|
2991
|
+
this.subscribers.forEach((cb) => cb());
|
|
2992
|
+
}
|
|
2993
|
+
/**
|
|
2994
|
+
* Cleanup subscriptions
|
|
2995
|
+
*/
|
|
2996
|
+
dispose() {
|
|
2997
|
+
this.subscribers.clear();
|
|
2998
|
+
}
|
|
2999
|
+
};
|
|
3000
|
+
function createReactThreadManagerState(initialThreads) {
|
|
3001
|
+
return new ReactThreadManagerState(initialThreads);
|
|
3002
|
+
}
|
|
3003
|
+
|
|
3004
|
+
// src/react/internal/ReactThreadManager.ts
|
|
3005
|
+
var _ReactThreadManager = class _ReactThreadManager extends ThreadManager {
|
|
3006
|
+
constructor(config = {}, callbacks = {}) {
|
|
3007
|
+
const reactState = new ReactThreadManagerState();
|
|
3008
|
+
super({ ...config, state: reactState }, callbacks);
|
|
3009
|
+
// ============================================
|
|
3010
|
+
// Subscription Methods (for useSyncExternalStore)
|
|
3011
|
+
// ============================================
|
|
3012
|
+
/**
|
|
3013
|
+
* Subscribe to state changes
|
|
3014
|
+
* Use with useSyncExternalStore
|
|
3015
|
+
*/
|
|
3016
|
+
this.subscribe = (callback) => {
|
|
3017
|
+
return this.state.subscribe(callback);
|
|
3018
|
+
};
|
|
3019
|
+
// ============================================
|
|
3020
|
+
// Snapshot Getters (for useSyncExternalStore)
|
|
3021
|
+
// ============================================
|
|
3022
|
+
/**
|
|
3023
|
+
* Get threads snapshot
|
|
3024
|
+
*/
|
|
3025
|
+
this.getThreadsSnapshot = () => {
|
|
3026
|
+
return this.state.getThreadsSnapshot();
|
|
3027
|
+
};
|
|
3028
|
+
/**
|
|
3029
|
+
* Get current thread snapshot
|
|
3030
|
+
*/
|
|
3031
|
+
this.getCurrentThreadSnapshot = () => {
|
|
3032
|
+
return this.state.getCurrentThreadSnapshot();
|
|
3033
|
+
};
|
|
3034
|
+
/**
|
|
3035
|
+
* Get current thread ID snapshot
|
|
3036
|
+
*/
|
|
3037
|
+
this.getCurrentThreadIdSnapshot = () => {
|
|
3038
|
+
return this.state.currentThreadId;
|
|
3039
|
+
};
|
|
3040
|
+
/**
|
|
3041
|
+
* Get load status snapshot
|
|
3042
|
+
*/
|
|
3043
|
+
this.getLoadStatusSnapshot = () => {
|
|
3044
|
+
return this.state.getLoadStatusSnapshot();
|
|
3045
|
+
};
|
|
3046
|
+
/**
|
|
3047
|
+
* Get error snapshot
|
|
3048
|
+
*/
|
|
3049
|
+
this.getErrorSnapshot = () => {
|
|
3050
|
+
return this.state.getErrorSnapshot();
|
|
3051
|
+
};
|
|
3052
|
+
/**
|
|
3053
|
+
* Get isLoading snapshot
|
|
3054
|
+
*/
|
|
3055
|
+
this.getIsLoadingSnapshot = () => {
|
|
3056
|
+
return this.state.getLoadStatusSnapshot() === "loading";
|
|
3057
|
+
};
|
|
3058
|
+
/**
|
|
3059
|
+
* Get threads snapshot for server (always empty for hydration consistency)
|
|
3060
|
+
*/
|
|
3061
|
+
this.getThreadsServerSnapshot = () => {
|
|
3062
|
+
return _ReactThreadManager.EMPTY_THREADS;
|
|
3063
|
+
};
|
|
3064
|
+
/**
|
|
3065
|
+
* Get current thread snapshot for server (always null)
|
|
3066
|
+
*/
|
|
3067
|
+
this.getCurrentThreadServerSnapshot = () => {
|
|
3068
|
+
return null;
|
|
3069
|
+
};
|
|
3070
|
+
/**
|
|
3071
|
+
* Get current thread ID snapshot for server (always null)
|
|
3072
|
+
*/
|
|
3073
|
+
this.getCurrentThreadIdServerSnapshot = () => {
|
|
3074
|
+
return null;
|
|
3075
|
+
};
|
|
3076
|
+
/**
|
|
3077
|
+
* Get load status snapshot for server (always "idle")
|
|
3078
|
+
*/
|
|
3079
|
+
this.getLoadStatusServerSnapshot = () => {
|
|
3080
|
+
return _ReactThreadManager.IDLE_STATUS;
|
|
3081
|
+
};
|
|
3082
|
+
/**
|
|
3083
|
+
* Get error snapshot for server (always undefined)
|
|
3084
|
+
*/
|
|
3085
|
+
this.getErrorServerSnapshot = () => {
|
|
3086
|
+
return void 0;
|
|
3087
|
+
};
|
|
3088
|
+
/**
|
|
3089
|
+
* Get isLoading snapshot for server (always false)
|
|
3090
|
+
*/
|
|
3091
|
+
this.getIsLoadingServerSnapshot = () => {
|
|
3092
|
+
return false;
|
|
3093
|
+
};
|
|
3094
|
+
}
|
|
3095
|
+
// ============================================
|
|
3096
|
+
// Cleanup
|
|
3097
|
+
// ============================================
|
|
3098
|
+
/**
|
|
3099
|
+
* Dispose of the manager
|
|
3100
|
+
*/
|
|
3101
|
+
async dispose() {
|
|
3102
|
+
this.state.dispose();
|
|
3103
|
+
await super.dispose();
|
|
3104
|
+
}
|
|
3105
|
+
};
|
|
3106
|
+
// ============================================
|
|
3107
|
+
// Server Snapshots (for SSR - stable cached values)
|
|
3108
|
+
// ============================================
|
|
3109
|
+
// Cached values for server snapshots (must be stable references)
|
|
3110
|
+
_ReactThreadManager.EMPTY_THREADS = [];
|
|
3111
|
+
_ReactThreadManager.IDLE_STATUS = "idle";
|
|
3112
|
+
var ReactThreadManager = _ReactThreadManager;
|
|
3113
|
+
function createReactThreadManager(config, callbacks) {
|
|
3114
|
+
return new ReactThreadManager(config, callbacks);
|
|
3115
|
+
}
|
|
3116
|
+
|
|
3117
|
+
// src/react/hooks/useThreadManager.ts
|
|
3118
|
+
var defaultManager = null;
|
|
3119
|
+
function getDefaultManager() {
|
|
3120
|
+
if (!defaultManager) {
|
|
3121
|
+
defaultManager = createReactThreadManager();
|
|
3122
|
+
}
|
|
3123
|
+
return defaultManager;
|
|
3124
|
+
}
|
|
3125
|
+
var internalManager = null;
|
|
3126
|
+
function getInternalManager(config) {
|
|
3127
|
+
if (!internalManager) {
|
|
3128
|
+
internalManager = createReactThreadManager(
|
|
3129
|
+
{
|
|
3130
|
+
adapter: config.adapter,
|
|
3131
|
+
saveDebounce: config.saveDebounce,
|
|
3132
|
+
autoLoad: config.autoLoad,
|
|
3133
|
+
autoRestoreLastThread: config.autoRestoreLastThread
|
|
3134
|
+
},
|
|
3135
|
+
config.callbacks
|
|
3136
|
+
);
|
|
3137
|
+
}
|
|
3138
|
+
return internalManager;
|
|
3139
|
+
}
|
|
3140
|
+
function useThreadManager(config) {
|
|
3141
|
+
const manager = useMemo(() => {
|
|
3142
|
+
if (!config) {
|
|
3143
|
+
return getDefaultManager();
|
|
3144
|
+
}
|
|
3145
|
+
if (!config.adapter) {
|
|
3146
|
+
return getInternalManager(config);
|
|
3147
|
+
}
|
|
3148
|
+
console.log("[useThreadManager] Creating new manager with custom adapter");
|
|
3149
|
+
return createReactThreadManager(
|
|
3150
|
+
{
|
|
3151
|
+
adapter: config.adapter,
|
|
3152
|
+
saveDebounce: config.saveDebounce,
|
|
3153
|
+
autoLoad: config.autoLoad,
|
|
3154
|
+
autoRestoreLastThread: config.autoRestoreLastThread
|
|
3155
|
+
},
|
|
3156
|
+
config.callbacks
|
|
3157
|
+
);
|
|
3158
|
+
}, [
|
|
3159
|
+
config?.adapter,
|
|
3160
|
+
config?.saveDebounce,
|
|
3161
|
+
config?.autoLoad,
|
|
3162
|
+
config?.autoRestoreLastThread
|
|
3163
|
+
// Note: callbacks are intentionally not in deps to avoid recreating manager
|
|
3164
|
+
]);
|
|
3165
|
+
const threads = useSyncExternalStore(
|
|
3166
|
+
manager.subscribe,
|
|
3167
|
+
manager.getThreadsSnapshot,
|
|
3168
|
+
manager.getThreadsServerSnapshot
|
|
3169
|
+
// SSR - always empty array
|
|
3170
|
+
);
|
|
3171
|
+
const currentThread = useSyncExternalStore(
|
|
3172
|
+
manager.subscribe,
|
|
3173
|
+
manager.getCurrentThreadSnapshot,
|
|
3174
|
+
manager.getCurrentThreadServerSnapshot
|
|
3175
|
+
// SSR - always null
|
|
3176
|
+
);
|
|
3177
|
+
const currentThreadId = useSyncExternalStore(
|
|
3178
|
+
manager.subscribe,
|
|
3179
|
+
manager.getCurrentThreadIdSnapshot,
|
|
3180
|
+
manager.getCurrentThreadIdServerSnapshot
|
|
3181
|
+
// SSR - always null
|
|
3182
|
+
);
|
|
3183
|
+
const loadStatus = useSyncExternalStore(
|
|
3184
|
+
manager.subscribe,
|
|
3185
|
+
manager.getLoadStatusSnapshot,
|
|
3186
|
+
manager.getLoadStatusServerSnapshot
|
|
3187
|
+
// SSR - always "idle"
|
|
3188
|
+
);
|
|
3189
|
+
const error = useSyncExternalStore(
|
|
3190
|
+
manager.subscribe,
|
|
3191
|
+
manager.getErrorSnapshot,
|
|
3192
|
+
manager.getErrorServerSnapshot
|
|
3193
|
+
// SSR - always undefined
|
|
3194
|
+
);
|
|
3195
|
+
const isLoading = useSyncExternalStore(
|
|
3196
|
+
manager.subscribe,
|
|
3197
|
+
manager.getIsLoadingSnapshot,
|
|
3198
|
+
manager.getIsLoadingServerSnapshot
|
|
3199
|
+
// SSR - always false
|
|
3200
|
+
);
|
|
3201
|
+
useEffect(() => {
|
|
3202
|
+
return () => {
|
|
3203
|
+
if (config?.adapter && manager !== defaultManager && manager !== internalManager) {
|
|
3204
|
+
manager.dispose();
|
|
3205
|
+
}
|
|
3206
|
+
};
|
|
3207
|
+
}, [manager, config]);
|
|
3208
|
+
useEffect(() => {
|
|
3209
|
+
const handleBeforeUnload = () => {
|
|
3210
|
+
if (manager.hasPendingChanges) {
|
|
3211
|
+
manager.saveNow().catch(() => {
|
|
3212
|
+
});
|
|
3213
|
+
}
|
|
3214
|
+
};
|
|
3215
|
+
if (typeof window !== "undefined") {
|
|
3216
|
+
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
3217
|
+
return () => {
|
|
3218
|
+
window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
3219
|
+
};
|
|
3220
|
+
}
|
|
3221
|
+
}, [manager]);
|
|
3222
|
+
const createThread = useCallback(
|
|
3223
|
+
(options) => manager.createThread(options),
|
|
3224
|
+
[manager]
|
|
3225
|
+
);
|
|
3226
|
+
const switchThread = useCallback(
|
|
3227
|
+
(id) => manager.switchThread(id),
|
|
3228
|
+
[manager]
|
|
3229
|
+
);
|
|
3230
|
+
const updateCurrentThread = useCallback(
|
|
3231
|
+
(updates) => manager.updateCurrentThread(updates),
|
|
3232
|
+
[manager]
|
|
3233
|
+
);
|
|
3234
|
+
const deleteThread = useCallback(
|
|
3235
|
+
(id) => manager.deleteThread(id),
|
|
3236
|
+
[manager]
|
|
3237
|
+
);
|
|
3238
|
+
const clearCurrentThread = useCallback(
|
|
3239
|
+
() => manager.clearCurrentThread(),
|
|
3240
|
+
[manager]
|
|
3241
|
+
);
|
|
3242
|
+
const refreshThreads = useCallback(() => manager.loadThreads(), [manager]);
|
|
3243
|
+
const saveNow = useCallback(() => manager.saveNow(), [manager]);
|
|
3244
|
+
const clearAllThreads = useCallback(
|
|
3245
|
+
() => manager.clearAllThreads(),
|
|
3246
|
+
[manager]
|
|
3247
|
+
);
|
|
3248
|
+
const messages = useMemo(
|
|
3249
|
+
() => currentThread?.messages ?? [],
|
|
3250
|
+
[currentThread]
|
|
3251
|
+
);
|
|
3252
|
+
const setMessages = useCallback(
|
|
3253
|
+
(newMessages) => updateCurrentThread({ messages: newMessages }),
|
|
3254
|
+
[updateCurrentThread]
|
|
3255
|
+
);
|
|
3256
|
+
const hasPendingChanges = manager.hasPendingChanges;
|
|
3257
|
+
return {
|
|
3258
|
+
// State
|
|
3259
|
+
threads,
|
|
3260
|
+
currentThread,
|
|
3261
|
+
currentThreadId,
|
|
3262
|
+
isLoading,
|
|
3263
|
+
loadStatus,
|
|
3264
|
+
error,
|
|
3265
|
+
// Actions
|
|
3266
|
+
createThread,
|
|
3267
|
+
switchThread,
|
|
3268
|
+
updateCurrentThread,
|
|
3269
|
+
deleteThread,
|
|
3270
|
+
clearCurrentThread,
|
|
3271
|
+
refreshThreads,
|
|
3272
|
+
saveNow,
|
|
3273
|
+
clearAllThreads,
|
|
3274
|
+
// Utilities
|
|
3275
|
+
messages,
|
|
3276
|
+
setMessages,
|
|
3277
|
+
hasPendingChanges
|
|
3278
|
+
};
|
|
3279
|
+
}
|
|
3280
|
+
|
|
2850
3281
|
// src/react/utils/permission-storage.ts
|
|
2851
3282
|
var DEFAULT_KEY_PREFIX = "yourgpt-permissions";
|
|
2852
3283
|
function createPermissionStorage(config) {
|
|
@@ -3095,6 +3526,6 @@ function useChat(config) {
|
|
|
3095
3526
|
};
|
|
3096
3527
|
}
|
|
3097
3528
|
|
|
3098
|
-
export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, createPermissionStorage, createReactChat, createReactChatState, createSessionPermissionCache, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useSuggestions, useSupportedMediaTypes, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
|
|
3099
|
-
//# sourceMappingURL=chunk-
|
|
3100
|
-
//# sourceMappingURL=chunk-
|
|
3529
|
+
export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, ReactThreadManager, ReactThreadManagerState, createPermissionStorage, createReactChat, createReactChatState, createReactThreadManager, createReactThreadManagerState, createSessionPermissionCache, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useSuggestions, useSupportedMediaTypes, useThreadManager, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
|
|
3530
|
+
//# sourceMappingURL=chunk-XAVZZVUL.js.map
|
|
3531
|
+
//# sourceMappingURL=chunk-XAVZZVUL.js.map
|