@springbrand/chat-client 0.1.3-alpha.24 → 0.1.3-alpha.26
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/package.json +1 -1
- package/src/use-universal-agent-chat.ts +13 -10
package/package.json
CHANGED
|
@@ -178,6 +178,8 @@ type SharedChatConnectionOptions = {
|
|
|
178
178
|
onMessage?: (message: MessageEvent) => void;
|
|
179
179
|
inboxAgent?: string;
|
|
180
180
|
sessionAgent?: string;
|
|
181
|
+
/** Skip history loading when the caller has just created this Chat. */
|
|
182
|
+
skipInitialMessages?: boolean;
|
|
181
183
|
/** Host projection used only until the selected Session sends its first state. */
|
|
182
184
|
initialTurnActive?: boolean;
|
|
183
185
|
};
|
|
@@ -192,6 +194,7 @@ type UniversalAgentChatContextValue = {
|
|
|
192
194
|
chatId: string;
|
|
193
195
|
connection: ChatConnectionOptions;
|
|
194
196
|
agent: SessionAgentConnection;
|
|
197
|
+
chatAgent: SessionAgentConnection;
|
|
195
198
|
};
|
|
196
199
|
|
|
197
200
|
const UniversalAgentChatContext = createContext<UniversalAgentChatContextValue | null>(null);
|
|
@@ -222,9 +225,13 @@ export function UniversalAgentChatProvider({
|
|
|
222
225
|
name: chatId,
|
|
223
226
|
}],
|
|
224
227
|
});
|
|
228
|
+
const chatAgent = useMemo(
|
|
229
|
+
() => createSafeUIMessageAgentConnection(agent),
|
|
230
|
+
[agent],
|
|
231
|
+
);
|
|
225
232
|
const value = useMemo(
|
|
226
|
-
() => ({ chatId, connection, agent }),
|
|
227
|
-
[agent, chatId, connection],
|
|
233
|
+
() => ({ chatId, connection, agent, chatAgent }),
|
|
234
|
+
[agent, chatAgent, chatId, connection],
|
|
228
235
|
);
|
|
229
236
|
return createElement(
|
|
230
237
|
UniversalAgentChatContext.Provider,
|
|
@@ -365,7 +372,7 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
365
372
|
if (!context) {
|
|
366
373
|
throw new Error("useUniversalAgentChat must be used within UniversalAgentChatProvider");
|
|
367
374
|
}
|
|
368
|
-
const { chatId, connection, agent } = context;
|
|
375
|
+
const { chatId, connection, agent, chatAgent } = context;
|
|
369
376
|
const [optimisticMessages, setOptimisticMessages] =
|
|
370
377
|
useState<ChatMessage[]>([]);
|
|
371
378
|
const [optimisticQueued, setOptimisticQueued] =
|
|
@@ -374,11 +381,6 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
374
381
|
const [canRetry, setCanRetry] = useState(false);
|
|
375
382
|
const failedDispatchRef = useRef<FailedDispatch | undefined>(undefined);
|
|
376
383
|
const activeSubmissionIdRef = useRef<string | undefined>(undefined);
|
|
377
|
-
const chatAgent = useMemo(
|
|
378
|
-
() => createSafeUIMessageAgentConnection(agent),
|
|
379
|
-
[agent],
|
|
380
|
-
);
|
|
381
|
-
|
|
382
384
|
// 轮询→推送:这条 chat 连接连的是 UniversalAgent facet,`agent.state` 即 facet 的
|
|
383
385
|
// AgentState 广播(useAgent 内部 useState,收到 cf_agent_state 即 re-render)。待批项
|
|
384
386
|
// 随它推来 —— 审批读侧复用这条已开的连接,不另开第二条 WebSocket。三态(原则 VII):
|
|
@@ -402,8 +404,9 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
402
404
|
connectionError: sdkConnectionError,
|
|
403
405
|
} = useAgentChat<RuntimeState, ChatMessage>({
|
|
404
406
|
agent: chatAgent,
|
|
405
|
-
getInitialMessages:
|
|
406
|
-
|
|
407
|
+
getInitialMessages: connection.skipInitialMessages
|
|
408
|
+
? null
|
|
409
|
+
: ({ url }) => loadInitialMessages(url, connection.credentials),
|
|
407
410
|
...(connection.credentials === undefined
|
|
408
411
|
? {}
|
|
409
412
|
: { credentials: connection.credentials }),
|