@dudko.dev/agent-web-react 0.0.41 → 0.0.42
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.cjs +99 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +99 -39
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -447,7 +447,9 @@ declare const saveChat: (store: ChatHistoryStore, view: ChatRecord, base: ChatBa
|
|
|
447
447
|
* notices), the open chat is read again when another tab saved it and nothing runs here, and a
|
|
448
448
|
* run followed from another tab (`agent.remote`) is never saved from here - its own tab saves it.
|
|
449
449
|
* A turn of this tab's is saved even when such a run reached it before the save began (a tab the
|
|
450
|
-
* browser froze, waking), and keeps its place among the other tab's turns (`mergeChatRecord`)
|
|
450
|
+
* browser froze, waking), and keeps its place among the other tab's turns (`mergeChatRecord`); a
|
|
451
|
+
* read of the chat again (another tab's save notice, a followed run's end) never replaces such a
|
|
452
|
+
* turn - it is saved first, merged, and the merged record shown.
|
|
451
453
|
*/
|
|
452
454
|
declare const useChatHistory: (agent: UseAgentReturn, options?: UseChatHistoryOptions) => UseChatHistoryReturn;
|
|
453
455
|
|
package/dist/index.d.ts
CHANGED
|
@@ -447,7 +447,9 @@ declare const saveChat: (store: ChatHistoryStore, view: ChatRecord, base: ChatBa
|
|
|
447
447
|
* notices), the open chat is read again when another tab saved it and nothing runs here, and a
|
|
448
448
|
* run followed from another tab (`agent.remote`) is never saved from here - its own tab saves it.
|
|
449
449
|
* A turn of this tab's is saved even when such a run reached it before the save began (a tab the
|
|
450
|
-
* browser froze, waking), and keeps its place among the other tab's turns (`mergeChatRecord`)
|
|
450
|
+
* browser froze, waking), and keeps its place among the other tab's turns (`mergeChatRecord`); a
|
|
451
|
+
* read of the chat again (another tab's save notice, a followed run's end) never replaces such a
|
|
452
|
+
* turn - it is saved first, merged, and the merged record shown.
|
|
451
453
|
*/
|
|
452
454
|
declare const useChatHistory: (agent: UseAgentReturn, options?: UseChatHistoryOptions) => UseChatHistoryReturn;
|
|
453
455
|
|
package/dist/index.js
CHANGED
|
@@ -2114,7 +2114,18 @@ var useChatHistory = (agent, options = {}) => {
|
|
|
2114
2114
|
const agentRef = useRef2(agent);
|
|
2115
2115
|
agentRef.current = agent;
|
|
2116
2116
|
const baseRef = useRef2(void 0);
|
|
2117
|
+
const loadingRef = useRef2(void 0);
|
|
2118
|
+
if (loadingRef.current && agent.messages !== loadingRef.current) loadingRef.current = void 0;
|
|
2117
2119
|
const staleRef = useRef2(void 0);
|
|
2120
|
+
const queueRef = useRef2(Promise.resolve());
|
|
2121
|
+
const serially = useCallback2((task) => {
|
|
2122
|
+
const next = queueRef.current.then(task);
|
|
2123
|
+
queueRef.current = next.catch(() => {
|
|
2124
|
+
});
|
|
2125
|
+
return next;
|
|
2126
|
+
}, []);
|
|
2127
|
+
const baseOf = (id) => baseRef.current?.id === id ? baseRef.current : void 0;
|
|
2128
|
+
const unsavedHere = (messages2, id) => !loadingRef.current && ownUnsaved(messages2, baseOf(id));
|
|
2118
2129
|
const refresh = useCallback2(async () => {
|
|
2119
2130
|
try {
|
|
2120
2131
|
setChats(await store.list());
|
|
@@ -2124,19 +2135,79 @@ var useChatHistory = (agent, options = {}) => {
|
|
|
2124
2135
|
const newChat = useCallback2(() => {
|
|
2125
2136
|
agentRef.current.loadChat({ sessionId: newChatId(), messages: [] });
|
|
2126
2137
|
}, []);
|
|
2127
|
-
const
|
|
2138
|
+
const show = useCallback2((record) => {
|
|
2139
|
+
createdRef.current.set(record.id, record.createdAt);
|
|
2140
|
+
baseRef.current = {
|
|
2141
|
+
id: record.id,
|
|
2142
|
+
known: new Set(record.messages),
|
|
2143
|
+
updatedAt: record.updatedAt
|
|
2144
|
+
};
|
|
2145
|
+
loadingRef.current ??= agentRef.current.messages;
|
|
2146
|
+
agentRef.current.loadChat({
|
|
2147
|
+
sessionId: record.id,
|
|
2148
|
+
messages: record.messages,
|
|
2149
|
+
totalUsage: record.totalUsage
|
|
2150
|
+
});
|
|
2151
|
+
}, []);
|
|
2152
|
+
const saveOpen = useCallback2(
|
|
2128
2153
|
async (id) => {
|
|
2129
|
-
const
|
|
2130
|
-
if (
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2154
|
+
const current = agentRef.current;
|
|
2155
|
+
if (current.sessionId !== id || current.isRunning) return;
|
|
2156
|
+
const { messages: messages2, totalUsage: totalUsage2, remote: remote2 } = current;
|
|
2157
|
+
if (messages2.length === 0) return;
|
|
2158
|
+
const base = baseOf(id);
|
|
2159
|
+
if (loadingRef.current || unchangedSince(messages2, base)) return;
|
|
2160
|
+
if (remote2 && !ownUnsaved(messages2, base)) return;
|
|
2161
|
+
const now = Date.now();
|
|
2162
|
+
const createdAt = createdRef.current.get(id) ?? now;
|
|
2163
|
+
createdRef.current.set(id, createdAt);
|
|
2164
|
+
const view = {
|
|
2165
|
+
id,
|
|
2166
|
+
title: chatTitle(messages2, textsRef.current),
|
|
2167
|
+
createdAt,
|
|
2168
|
+
updatedAt: now,
|
|
2169
|
+
messages: messages2,
|
|
2170
|
+
totalUsage: totalUsage2
|
|
2171
|
+
};
|
|
2172
|
+
const { record, merged } = await saveChat(store, view, base, textsRef.current);
|
|
2173
|
+
baseRef.current = {
|
|
2174
|
+
id,
|
|
2175
|
+
known: new Set(merged ? record.messages : messages2),
|
|
2176
|
+
updatedAt: record.updatedAt
|
|
2177
|
+
};
|
|
2178
|
+
await refresh();
|
|
2179
|
+
if (merged && agentRef.current.sessionId === id) show(record);
|
|
2138
2180
|
},
|
|
2139
|
-
|
|
2181
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2182
|
+
[store, show]
|
|
2183
|
+
);
|
|
2184
|
+
const reread = useCallback2(
|
|
2185
|
+
(id) => serially(async () => {
|
|
2186
|
+
const settled = () => {
|
|
2187
|
+
const current = agentRef.current;
|
|
2188
|
+
if (current.sessionId !== id) return false;
|
|
2189
|
+
if (current.isRunning) {
|
|
2190
|
+
if (current.remote?.running) staleRef.current = id;
|
|
2191
|
+
return false;
|
|
2192
|
+
}
|
|
2193
|
+
return true;
|
|
2194
|
+
};
|
|
2195
|
+
if (!settled()) return;
|
|
2196
|
+
if (unsavedHere(agentRef.current.messages, id)) return saveOpen(id);
|
|
2197
|
+
const record = await store.get(id).catch(() => void 0);
|
|
2198
|
+
if (!record || !settled()) return;
|
|
2199
|
+
if (unsavedHere(agentRef.current.messages, id)) return saveOpen(id);
|
|
2200
|
+
show(record);
|
|
2201
|
+
}),
|
|
2202
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2203
|
+
[store, serially, saveOpen, show]
|
|
2204
|
+
);
|
|
2205
|
+
const select = useCallback2(
|
|
2206
|
+
(id) => serially(async () => {
|
|
2207
|
+
const record = await store.get(id).catch(() => void 0);
|
|
2208
|
+
if (record) show(record);
|
|
2209
|
+
}),
|
|
2210
|
+
[store, serially, show]
|
|
2140
2211
|
);
|
|
2141
2212
|
const remove = useCallback2(
|
|
2142
2213
|
async (id) => {
|
|
@@ -2180,45 +2251,34 @@ var useChatHistory = (agent, options = {}) => {
|
|
|
2180
2251
|
const current = agentRef.current;
|
|
2181
2252
|
if (key === void 0 || key !== current.sessionId) return;
|
|
2182
2253
|
if (current.remote?.running) staleRef.current = key;
|
|
2183
|
-
else if (!current.isRunning) void
|
|
2254
|
+
else if (!current.isRunning) void reread(key);
|
|
2184
2255
|
}
|
|
2185
2256
|
);
|
|
2186
|
-
}, [enabled, store, refresh,
|
|
2257
|
+
}, [enabled, store, refresh, reread]);
|
|
2187
2258
|
const { messages, totalUsage, sessionId, isRunning, remote } = agent;
|
|
2188
2259
|
useEffect2(() => {
|
|
2189
2260
|
if (!enabled || loading || isRunning) return;
|
|
2190
2261
|
if (staleRef.current === sessionId) {
|
|
2191
2262
|
staleRef.current = void 0;
|
|
2192
|
-
void
|
|
2263
|
+
void reread(sessionId).catch(() => {
|
|
2264
|
+
});
|
|
2193
2265
|
return;
|
|
2194
2266
|
}
|
|
2195
2267
|
if (messages.length === 0) return;
|
|
2196
|
-
|
|
2197
|
-
if (unchangedSince(messages, base)) return;
|
|
2198
|
-
if (remote && !ownUnsaved(messages, base)) return;
|
|
2199
|
-
const now = Date.now();
|
|
2200
|
-
const createdAt = createdRef.current.get(sessionId) ?? now;
|
|
2201
|
-
createdRef.current.set(sessionId, createdAt);
|
|
2202
|
-
const view = {
|
|
2203
|
-
id: sessionId,
|
|
2204
|
-
title: chatTitle(messages, textsRef.current),
|
|
2205
|
-
createdAt,
|
|
2206
|
-
updatedAt: now,
|
|
2207
|
-
messages,
|
|
2208
|
-
totalUsage
|
|
2209
|
-
};
|
|
2210
|
-
void (async () => {
|
|
2211
|
-
const { record, merged } = await saveChat(store, view, base, textsRef.current);
|
|
2212
|
-
baseRef.current = {
|
|
2213
|
-
id: sessionId,
|
|
2214
|
-
known: new Set(merged ? record.messages : messages),
|
|
2215
|
-
updatedAt: record.updatedAt
|
|
2216
|
-
};
|
|
2217
|
-
await refresh();
|
|
2218
|
-
if (merged && agentRef.current.sessionId === sessionId) await select(sessionId);
|
|
2219
|
-
})().catch(() => {
|
|
2268
|
+
void serially(() => saveOpen(sessionId)).catch(() => {
|
|
2220
2269
|
});
|
|
2221
|
-
}, [
|
|
2270
|
+
}, [
|
|
2271
|
+
enabled,
|
|
2272
|
+
loading,
|
|
2273
|
+
isRunning,
|
|
2274
|
+
remote,
|
|
2275
|
+
messages,
|
|
2276
|
+
totalUsage,
|
|
2277
|
+
sessionId,
|
|
2278
|
+
serially,
|
|
2279
|
+
saveOpen,
|
|
2280
|
+
reread
|
|
2281
|
+
]);
|
|
2222
2282
|
return { chats, currentId: sessionId, loading, select, newChat, remove };
|
|
2223
2283
|
};
|
|
2224
2284
|
|