@agentscope-ai/chat 1.2.0-beta.1788158364708 → 1.2.0-beta.1788233261358

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.
@@ -12,6 +12,7 @@ import {
12
12
  collectSessionIdentityAliases,
13
13
  isSameLoadedSession,
14
14
  } from './sessionIdentity';
15
+ import { activateCachedSessionMessages } from './sessionMessageActivation';
15
16
  import { useChatAnywhereCommandDispatcher } from './useChatAnywhereEventEmitter';
16
17
 
17
18
  const hasOwn = Object.prototype.hasOwnProperty;
@@ -179,6 +180,14 @@ export const useChatAnywhereSessionLoader = () => {
179
180
  undefined,
180
181
  getSessions(),
181
182
  );
183
+ // `createSession` already seeded this session's message cache, so no
184
+ // backend load is necessary. We still need to promote that cached list
185
+ // to the active view after React has applied the new session id;
186
+ // otherwise the previous conversation remains visible until another
187
+ // interaction triggers a render or session load.
188
+ ReactDOM.flushSync(() => {
189
+ activateCachedSessionMessages(currentSessionId, setSessionMessages);
190
+ });
182
191
  await dispatch('handleSessionLoaded', {
183
192
  session_id: currentSessionId,
184
193
  generating: false,
@@ -0,0 +1,30 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import type { IAgentScopeRuntimeWebUIMessage } from '../../types/IMessages';
4
+ import { activateCachedSessionMessages } from '../sessionMessageActivation';
5
+
6
+ const oldMessages: IAgentScopeRuntimeWebUIMessage[] = [
7
+ { id: 'old-message', role: 'user' },
8
+ ];
9
+
10
+ test('activates the cached message list for a newly created session', () => {
11
+ const messagesBySession: Record<
12
+ string,
13
+ IAgentScopeRuntimeWebUIMessage[]
14
+ > = {
15
+ old: oldMessages,
16
+ fresh: [],
17
+ };
18
+ let visibleMessages = oldMessages;
19
+
20
+ activateCachedSessionMessages('fresh', (sessionId, updater) => {
21
+ const previous = messagesBySession[sessionId] || [];
22
+ const next =
23
+ typeof updater === 'function' ? updater(previous) : updater;
24
+ messagesBySession[sessionId] = next;
25
+ visibleMessages = next;
26
+ });
27
+
28
+ assert.deepEqual(visibleMessages, []);
29
+ assert.equal(messagesBySession.old, oldMessages);
30
+ });
@@ -0,0 +1,15 @@
1
+ import type { IAgentScopeRuntimeWebUIMessagesContext } from '../types/IMessages';
2
+
3
+ /**
4
+ * Promote a session's already-cached messages to the active message view.
5
+ *
6
+ * The messages context decides whether a write targets the visible session
7
+ * from the current session identity, so replaying the cached value after the
8
+ * identity render is enough to switch views without refetching the session.
9
+ */
10
+ export function activateCachedSessionMessages(
11
+ sessionId: string,
12
+ setSessionMessages: IAgentScopeRuntimeWebUIMessagesContext['setSessionMessages'],
13
+ ) {
14
+ setSessionMessages(sessionId, (messages) => messages);
15
+ }
@@ -10,5 +10,5 @@ export default function useMentions(options: IAgentScopeRuntimeWebUISenderMentio
10
10
  header: import("react/jsx-runtime").JSX.Element;
11
11
  mentions: IAgentScopeRuntimeWebUISenderMentionItem[];
12
12
  open: boolean;
13
- wrapInput: (node: React.ReactNode) => string | number | boolean | import("react/jsx-runtime").JSX.Element | Iterable<React.ReactNode>;
13
+ wrapInput: (node: React.ReactNode) => string | number | boolean | Iterable<React.ReactNode> | import("react/jsx-runtime").JSX.Element;
14
14
  };
@@ -24,6 +24,7 @@ import { createContext, useContextSelector } from 'use-context-selector';
24
24
  import { ChatAnywhereMessagesContext } from "./ChatAnywhereMessagesContext";
25
25
  import { useChatAnywhereOptions } from "./ChatAnywhereOptionsContext";
26
26
  import { collectSessionIdentityAliases, isSameLoadedSession } from "./sessionIdentity";
27
+ import { activateCachedSessionMessages } from "./sessionMessageActivation";
27
28
  import { useChatAnywhereCommandDispatcher } from "./useChatAnywhereEventEmitter";
28
29
  import { jsx as _jsx } from "react/jsx-runtime";
29
30
  var hasOwn = Object.prototype.hasOwnProperty;
@@ -157,60 +158,68 @@ export var useChatAnywhereSessionLoader = function useChatAnywhereSessionLoader(
157
158
  return _context.abrupt("return");
158
159
  case 6:
159
160
  if (!((skipNextSessionLoadIdRef === null || skipNextSessionLoadIdRef === void 0 ? void 0 : skipNextSessionLoadIdRef.current) === currentSessionId)) {
160
- _context.next = 12;
161
+ _context.next = 13;
161
162
  break;
162
163
  }
163
164
  skipNextSessionLoadIdRef.current = undefined;
164
165
  loadedSessionAliasesRef.current = collectSessionIdentityAliases(currentSessionId, undefined, getSessions());
165
- _context.next = 11;
166
+ // `createSession` already seeded this session's message cache, so no
167
+ // backend load is necessary. We still need to promote that cached list
168
+ // to the active view after React has applied the new session id;
169
+ // otherwise the previous conversation remains visible until another
170
+ // interaction triggers a render or session load.
171
+ ReactDOM.flushSync(function () {
172
+ activateCachedSessionMessages(currentSessionId, setSessionMessages);
173
+ });
174
+ _context.next = 12;
166
175
  return dispatch('handleSessionLoaded', {
167
176
  session_id: currentSessionId,
168
177
  generating: false
169
178
  });
170
- case 11:
171
- return _context.abrupt("return");
172
179
  case 12:
180
+ return _context.abrupt("return");
181
+ case 13:
173
182
  existingSessions = getSessions();
174
183
  sameLoadedSession = isSameLoadedSession(currentSessionId, loadedSessionAliasesRef.current, existingSessions);
175
184
  if (!sameLoadedSession) {
176
- _context.next = 17;
185
+ _context.next = 18;
177
186
  break;
178
187
  }
179
188
  loadedSessionAliasesRef.current = new Set([].concat(_toConsumableArray(loadedSessionAliasesRef.current), _toConsumableArray(collectSessionIdentityAliases(currentSessionId, undefined, existingSessions))));
180
189
  return _context.abrupt("return");
181
- case 17:
190
+ case 18:
182
191
  ReactDOM.flushSync(function () {
183
192
  setSessionMessages(currentSessionId, []);
184
193
  });
185
- _context.prev = 18;
186
- _context.next = 21;
194
+ _context.prev = 19;
195
+ _context.next = 22;
187
196
  return options.api.getSession(currentSessionId);
188
- case 21:
197
+ case 22:
189
198
  session = _context.sent;
190
- _context.next = 31;
199
+ _context.next = 32;
191
200
  break;
192
- case 24:
193
- _context.prev = 24;
194
- _context.t0 = _context["catch"](18);
201
+ case 25:
202
+ _context.prev = 25;
203
+ _context.t0 = _context["catch"](19);
195
204
  if (!isLatestLoad()) {
196
- _context.next = 30;
205
+ _context.next = 31;
197
206
  break;
198
207
  }
199
208
  console.error('get session failed:', _context.t0);
200
- _context.next = 30;
209
+ _context.next = 31;
201
210
  return dispatch('handleSessionLoaded', {
202
211
  session_id: currentSessionId,
203
212
  generating: false
204
213
  });
205
- case 30:
206
- return _context.abrupt("return");
207
214
  case 31:
215
+ return _context.abrupt("return");
216
+ case 32:
208
217
  if (isLatestLoad()) {
209
- _context.next = 33;
218
+ _context.next = 34;
210
219
  break;
211
220
  }
212
221
  return _context.abrupt("return");
213
- case 33:
222
+ case 34:
214
223
  loadedSessionAliasesRef.current = collectSessionIdentityAliases(currentSessionId, session, getSessions());
215
224
  messages = ((_session = session) === null || _session === void 0 ? void 0 : _session.messages) || [];
216
225
  setSessionMessages(currentSessionId, messages.map(function (item) {
@@ -218,25 +227,25 @@ export var useChatAnywhereSessionLoader = function useChatAnywhereSessionLoader(
218
227
  history: true
219
228
  });
220
229
  }));
221
- _context.next = 38;
230
+ _context.next = 39;
222
231
  return dispatch('handleSessionLoaded', {
223
232
  session_id: currentSessionId,
224
233
  generating: !!((_session2 = session) !== null && _session2 !== void 0 && _session2.generating)
225
234
  });
226
- case 38:
235
+ case 39:
227
236
  if (!((_session3 = session) !== null && _session3 !== void 0 && _session3.generating)) {
228
- _context.next = 41;
237
+ _context.next = 42;
229
238
  break;
230
239
  }
231
- _context.next = 41;
240
+ _context.next = 42;
232
241
  return dispatch('handleReconnect', {
233
242
  session_id: currentSessionId
234
243
  });
235
- case 41:
244
+ case 42:
236
245
  case "end":
237
246
  return _context.stop();
238
247
  }
239
- }, _callee, null, [[18, 24]]);
248
+ }, _callee, null, [[19, 25]]);
240
249
  })), [currentSessionId, dispatch, getSessions, options.api, setMessages, setSessionMessages, skipNextSessionLoadIdRef]);
241
250
  };
242
251
 
@@ -0,0 +1,9 @@
1
+ import type { IAgentScopeRuntimeWebUIMessagesContext } from '../types/IMessages';
2
+ /**
3
+ * Promote a session's already-cached messages to the active message view.
4
+ *
5
+ * The messages context decides whether a write targets the visible session
6
+ * from the current session identity, so replaying the cached value after the
7
+ * identity render is enough to switch views without refetching the session.
8
+ */
9
+ export declare function activateCachedSessionMessages(sessionId: string, setSessionMessages: IAgentScopeRuntimeWebUIMessagesContext['setSessionMessages']): void;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Promote a session's already-cached messages to the active message view.
3
+ *
4
+ * The messages context decides whether a write targets the visible session
5
+ * from the current session identity, so replaying the cached value after the
6
+ * identity render is enough to switch views without refetching the session.
7
+ */
8
+ export function activateCachedSessionMessages(sessionId, setSessionMessages) {
9
+ setSessionMessages(sessionId, function (messages) {
10
+ return messages;
11
+ });
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentscope-ai/chat",
3
- "version": "1.2.0-beta.1788158364708",
3
+ "version": "1.2.0-beta.1788233261358",
4
4
  "description": "a free and open-source chat framework for building excellent LLM-powered chat experiences",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": [