@springbrand/chat-client 0.1.3-alpha.18 → 0.1.3-alpha.20
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 +2 -2
- package/src/index.ts +1 -0
- package/src/use-universal-agent-chat.ts +57 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@springbrand/chat-client",
|
|
3
|
-
"version": "0.1.3-alpha.
|
|
3
|
+
"version": "0.1.3-alpha.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@types/react-dom": "^19.2.3",
|
|
26
26
|
"react-dom": "^19.2.7",
|
|
27
27
|
"typescript": "^7.0.2",
|
|
28
|
-
"@springbrand/agent-runtime": "0.2.0-alpha.
|
|
28
|
+
"@springbrand/agent-runtime": "0.2.0-alpha.27"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"typecheck": "tsc --noEmit"
|
package/src/index.ts
CHANGED
|
@@ -13,10 +13,12 @@ import {
|
|
|
13
13
|
import { nanoid } from "nanoid";
|
|
14
14
|
import type { ChatStatus, FileUIPart, UIMessage } from "ai";
|
|
15
15
|
import type { AgentToolRunState } from "agents";
|
|
16
|
+
import type { AgentConnectionError } from "agents/client";
|
|
16
17
|
import { MessageType } from "agents/chat";
|
|
17
18
|
import { getAgentMessages, useAgentChat } from "agents/chat/react";
|
|
18
19
|
import { useAgent, useAgentToolEvents } from "agents/react";
|
|
19
20
|
import {
|
|
21
|
+
type ApprovalDecision,
|
|
20
22
|
type MessageDelivery,
|
|
21
23
|
type MessageDispatchReceipt,
|
|
22
24
|
type RequestedCapability,
|
|
@@ -34,6 +36,11 @@ type ChatMessageMetadata = Record<string, unknown> & {
|
|
|
34
36
|
turnStatus?: string;
|
|
35
37
|
};
|
|
36
38
|
type ChatMessage = UIMessage<ChatMessageMetadata>;
|
|
39
|
+
export type ChatConnectionStatus =
|
|
40
|
+
| "connecting"
|
|
41
|
+
| "connected"
|
|
42
|
+
| "rejected"
|
|
43
|
+
| "error";
|
|
37
44
|
type FailedDispatch = {
|
|
38
45
|
message: ChatMessage;
|
|
39
46
|
delivery: MessageDelivery;
|
|
@@ -96,6 +103,8 @@ type ChatTurn = {
|
|
|
96
103
|
export interface ChatRuntime {
|
|
97
104
|
messages: ChatMessage[];
|
|
98
105
|
status: ChatStatus;
|
|
106
|
+
connectionStatus: ChatConnectionStatus;
|
|
107
|
+
connectionError: AgentConnectionError | undefined;
|
|
99
108
|
runtimeLoad: ChatRuntimeLoadState | undefined;
|
|
100
109
|
isStreaming: boolean;
|
|
101
110
|
error: string | Error | undefined;
|
|
@@ -127,6 +136,10 @@ export interface ChatRuntime {
|
|
|
127
136
|
toolCallId: string,
|
|
128
137
|
response: unknown,
|
|
129
138
|
) => Promise<boolean>;
|
|
139
|
+
decideApproval: (
|
|
140
|
+
executionId: string,
|
|
141
|
+
decision: ApprovalDecision,
|
|
142
|
+
) => Promise<{ ok: boolean }>;
|
|
130
143
|
/**
|
|
131
144
|
* 读取该 Session facet 当前真正装配出来的 Runtime。
|
|
132
145
|
*
|
|
@@ -136,6 +149,10 @@ export interface ChatRuntime {
|
|
|
136
149
|
* 装配未就绪还是连接断了,收敛成 `undefined` 会让调试面板无从显示原因。
|
|
137
150
|
*/
|
|
138
151
|
getRuntimeAssembly: () => Promise<RuntimeAssemblyView>;
|
|
152
|
+
getRuntimeBinding: <Binding = unknown>() => Promise<Binding>;
|
|
153
|
+
readChatMetadata: <Metadata = unknown>() => Promise<Metadata>;
|
|
154
|
+
renameChat: <Metadata = unknown>(title: string) => Promise<Metadata>;
|
|
155
|
+
rebindRuntime: <Input = unknown, Result = unknown>(input: Input) => Promise<Result>;
|
|
139
156
|
updateConfig: <Change = unknown>(
|
|
140
157
|
command: unknown,
|
|
141
158
|
) => Promise<RuntimeConfigUpdateResult<Change>>;
|
|
@@ -379,7 +396,7 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
379
396
|
isServerStreaming,
|
|
380
397
|
isRecovering: sdkIsRecovering,
|
|
381
398
|
isToolContinuation,
|
|
382
|
-
connectionError,
|
|
399
|
+
connectionError: sdkConnectionError,
|
|
383
400
|
} = useAgentChat<RuntimeState, ChatMessage>({
|
|
384
401
|
agent: chatAgent,
|
|
385
402
|
getInitialMessages: ({ url }) =>
|
|
@@ -412,7 +429,11 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
412
429
|
? "ready"
|
|
413
430
|
: sdkStatus;
|
|
414
431
|
const sdkError = stoppedByUser || hasTerminalMessage ? undefined : error;
|
|
415
|
-
const
|
|
432
|
+
const connectionError = sdkConnectionError ?? undefined;
|
|
433
|
+
const connectionStatus: ChatConnectionStatus = connectionError
|
|
434
|
+
? connectionError.code === 1008 ? "rejected" : "error"
|
|
435
|
+
: agent.identified ? "connected" : "connecting";
|
|
436
|
+
const runtimeError = dispatchError ?? sdkError ?? connectionError;
|
|
416
437
|
const runtimeErrorMessage = runtimeError instanceof Error
|
|
417
438
|
? runtimeError.message
|
|
418
439
|
: runtimeError;
|
|
@@ -751,6 +772,7 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
751
772
|
type: MessageType.CF_AGENT_CHAT_REQUEST_CANCEL,
|
|
752
773
|
id: requestId,
|
|
753
774
|
}));
|
|
775
|
+
return;
|
|
754
776
|
}
|
|
755
777
|
const result = submissionId
|
|
756
778
|
? await agentRef.current.call<{ ok: boolean }>(
|
|
@@ -798,6 +820,14 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
798
820
|
return false;
|
|
799
821
|
}
|
|
800
822
|
}, []);
|
|
823
|
+
const decideApproval = useCallback(
|
|
824
|
+
(executionId: string, decision: ApprovalDecision) =>
|
|
825
|
+
agentRef.current.call<{ ok: boolean }>(
|
|
826
|
+
"decideApproval",
|
|
827
|
+
[executionId, decision],
|
|
828
|
+
),
|
|
829
|
+
[],
|
|
830
|
+
);
|
|
801
831
|
|
|
802
832
|
// 只读控制面:Runtime 尚未装配时由 Agent 侧 `ensureRuntimeReady` 负责等待,
|
|
803
833
|
// 这里不缓存结果 —— 换模型、加技能或 reload 之后调用方要拿到的是新装配。
|
|
@@ -805,6 +835,24 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
805
835
|
() => agentRef.current.call<RuntimeAssemblyView>("getRuntimeAssembly", []),
|
|
806
836
|
[],
|
|
807
837
|
);
|
|
838
|
+
const getRuntimeBinding = useCallback(
|
|
839
|
+
<Binding,>() => agentRef.current.call<Binding>("getRuntimeBinding", []),
|
|
840
|
+
[],
|
|
841
|
+
);
|
|
842
|
+
const readChatMetadata = useCallback(
|
|
843
|
+
<Metadata,>() => agentRef.current.call<Metadata>("readChatMetadata", []),
|
|
844
|
+
[],
|
|
845
|
+
);
|
|
846
|
+
const renameChat = useCallback(
|
|
847
|
+
<Metadata,>(title: string) =>
|
|
848
|
+
agentRef.current.call<Metadata>("renameChat", [title]),
|
|
849
|
+
[],
|
|
850
|
+
);
|
|
851
|
+
const rebindRuntime = useCallback(
|
|
852
|
+
<Input, Result,>(input: Input) =>
|
|
853
|
+
agentRef.current.call<Result>("rebindRuntime", [input]),
|
|
854
|
+
[],
|
|
855
|
+
);
|
|
808
856
|
const updateConfig = useCallback(
|
|
809
857
|
<Change,>(command: unknown) =>
|
|
810
858
|
agentRef.current.call<RuntimeConfigUpdateResult<Change>>(
|
|
@@ -823,11 +871,18 @@ export function useUniversalAgentChat(): ChatRuntime {
|
|
|
823
871
|
|
|
824
872
|
return {
|
|
825
873
|
respondToolInteraction,
|
|
874
|
+
decideApproval,
|
|
826
875
|
getRuntimeAssembly,
|
|
876
|
+
getRuntimeBinding,
|
|
877
|
+
readChatMetadata,
|
|
878
|
+
renameChat,
|
|
879
|
+
rebindRuntime,
|
|
827
880
|
updateConfig,
|
|
828
881
|
reloadRuntime,
|
|
829
882
|
messages: visibleMessages,
|
|
830
883
|
status,
|
|
884
|
+
connectionStatus,
|
|
885
|
+
connectionError,
|
|
831
886
|
runtimeLoad: facetState?.runtimeLoad,
|
|
832
887
|
isStreaming,
|
|
833
888
|
error: presentationError,
|