@springbrand/chat-client 0.1.3-alpha.2 → 0.1.3-alpha.4
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/chat-sessions.ts +0 -13
- package/src/use-universal-agent-chat.ts +110 -11
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.4",
|
|
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.1.3-alpha.
|
|
28
|
+
"@springbrand/agent-runtime": "0.1.3-alpha.5"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"typecheck": "tsc --noEmit"
|
package/src/chat-sessions.ts
CHANGED
|
@@ -88,18 +88,6 @@ export function useChatSessions<Session>(options: ChatSessionsOptions = {}) {
|
|
|
88
88
|
await inbox.call("archiveChat", [id, archived]);
|
|
89
89
|
}, [inbox]);
|
|
90
90
|
|
|
91
|
-
const reloadChatRuntime = useCallback(
|
|
92
|
-
async (chatId: string, userAgentId: string) => {
|
|
93
|
-
const result = await inbox.call<{ userAgentId: string } | null>(
|
|
94
|
-
"chatReloadRuntime",
|
|
95
|
-
[chatId, userAgentId],
|
|
96
|
-
);
|
|
97
|
-
if (!result) throw new Error("Session is unavailable");
|
|
98
|
-
return result;
|
|
99
|
-
},
|
|
100
|
-
[inbox],
|
|
101
|
-
);
|
|
102
|
-
|
|
103
91
|
return {
|
|
104
92
|
chats: inbox.state?.chats ?? snapshot ?? [],
|
|
105
93
|
synced: inbox.state !== undefined || snapshot !== undefined,
|
|
@@ -111,7 +99,6 @@ export function useChatSessions<Session>(options: ChatSessionsOptions = {}) {
|
|
|
111
99
|
deleteChat,
|
|
112
100
|
pinChat,
|
|
113
101
|
archiveChat,
|
|
114
|
-
reloadChatRuntime,
|
|
115
102
|
};
|
|
116
103
|
}
|
|
117
104
|
|
|
@@ -8,6 +8,9 @@ import { useAgent, useAgentToolEvents } from "agents/react";
|
|
|
8
8
|
import {
|
|
9
9
|
type MessageDelivery,
|
|
10
10
|
type MessageDispatchReceipt,
|
|
11
|
+
type RequestedCapability,
|
|
12
|
+
type RuntimeAssemblyView,
|
|
13
|
+
type RuntimeConfigUpdateResult,
|
|
11
14
|
type RuntimeQueuedSubmission,
|
|
12
15
|
type RuntimeState,
|
|
13
16
|
} from "@springbrand/agent-runtime/contracts";
|
|
@@ -15,6 +18,7 @@ import { createSafeUIMessageAgentConnection } from "./ui-message-stream-guard";
|
|
|
15
18
|
import { CURRENT_INBOX_AGENT_PATH } from "./chat-sessions";
|
|
16
19
|
|
|
17
20
|
type ChatMessageMetadata = Record<string, unknown> & {
|
|
21
|
+
requestedCapabilities?: readonly RequestedCapability[];
|
|
18
22
|
turnStatus?: string;
|
|
19
23
|
};
|
|
20
24
|
type ChatMessage = UIMessage<ChatMessageMetadata>;
|
|
@@ -28,7 +32,7 @@ type ChatRuntimeLoadState =
|
|
|
28
32
|
| { status: "idle"; available: false }
|
|
29
33
|
| {
|
|
30
34
|
status: "loading";
|
|
31
|
-
phase: "config" | "
|
|
35
|
+
phase: "config" | "assembly" | "mcp" | "pi";
|
|
32
36
|
available: boolean;
|
|
33
37
|
startedAt: number;
|
|
34
38
|
updatedAt: number;
|
|
@@ -41,7 +45,7 @@ type ChatRuntimeLoadState =
|
|
|
41
45
|
}
|
|
42
46
|
| {
|
|
43
47
|
status: "error";
|
|
44
|
-
phase: "config" | "
|
|
48
|
+
phase: "config" | "assembly" | "mcp" | "pi";
|
|
45
49
|
available: boolean;
|
|
46
50
|
startedAt: number;
|
|
47
51
|
failedAt: number;
|
|
@@ -83,12 +87,47 @@ export interface ChatRuntime {
|
|
|
83
87
|
runtimeLoad: ChatRuntimeLoadState | undefined;
|
|
84
88
|
isStreaming: boolean;
|
|
85
89
|
error: string | Error | undefined;
|
|
86
|
-
sendText: (
|
|
87
|
-
|
|
88
|
-
|
|
90
|
+
sendText: (
|
|
91
|
+
text: string,
|
|
92
|
+
files?: readonly FileUIPart[],
|
|
93
|
+
capabilities?: readonly RequestedCapability[],
|
|
94
|
+
) => Promise<boolean>;
|
|
95
|
+
steerText: (
|
|
96
|
+
text: string,
|
|
97
|
+
files?: readonly FileUIPart[],
|
|
98
|
+
capabilities?: readonly RequestedCapability[],
|
|
99
|
+
) => Promise<boolean>;
|
|
100
|
+
enqueueText: (
|
|
101
|
+
text: string,
|
|
102
|
+
files?: readonly FileUIPart[],
|
|
103
|
+
capabilities?: readonly RequestedCapability[],
|
|
104
|
+
) => Promise<boolean>;
|
|
89
105
|
steerQueued: (submissionId: string) => Promise<boolean>;
|
|
90
106
|
cancelQueued: (submissionId: string) => Promise<boolean>;
|
|
91
107
|
stop: () => Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* 把用户对某张交互卡片的操作回写成那次工具调用的结果。
|
|
110
|
+
*
|
|
111
|
+
* 走的是这条已开的 WS 连接(`agent.call`),不是 HTTP —— ack 与随后流下来的
|
|
112
|
+
* `tool-output-available` chunk 同序到达,UI 状态不会倒挂。
|
|
113
|
+
*/
|
|
114
|
+
respondToolInteraction: (
|
|
115
|
+
toolCallId: string,
|
|
116
|
+
response: unknown,
|
|
117
|
+
) => Promise<boolean>;
|
|
118
|
+
/**
|
|
119
|
+
* 读取该 Session facet 当前真正装配出来的 Runtime。
|
|
120
|
+
*
|
|
121
|
+
* 复用这条已开的 WS 连接,调用方不必为一个只读控制面方法另建 `useAgent`。
|
|
122
|
+
*
|
|
123
|
+
* 与其它动作不同,失败会原样抛出:这是读取而不是提交,调用方需要知道是
|
|
124
|
+
* 装配未就绪还是连接断了,收敛成 `undefined` 会让调试面板无从显示原因。
|
|
125
|
+
*/
|
|
126
|
+
getRuntimeAssembly: () => Promise<RuntimeAssemblyView>;
|
|
127
|
+
updateConfig: <Change = unknown>(
|
|
128
|
+
command: unknown,
|
|
129
|
+
) => Promise<RuntimeConfigUpdateResult<Change>>;
|
|
130
|
+
reloadRuntime: () => Promise<void>;
|
|
92
131
|
regenerate: () => void;
|
|
93
132
|
canRetry: boolean;
|
|
94
133
|
agentToolRuns: AgentToolRunState<ChatMessage["parts"][number]>[];
|
|
@@ -170,10 +209,18 @@ function markChatTurnPhase(
|
|
|
170
209
|
function createChatMessage(
|
|
171
210
|
text: string,
|
|
172
211
|
files?: readonly FileUIPart[],
|
|
212
|
+
capabilities: readonly RequestedCapability[] = [],
|
|
173
213
|
): ChatMessage | null {
|
|
174
214
|
const normalizedText = text.trim();
|
|
175
215
|
if (!normalizedText && !files?.length) return null;
|
|
176
216
|
const createdAt = Date.now();
|
|
217
|
+
const seen = new Set<string>();
|
|
218
|
+
const requestedCapabilities = capabilities.filter((capability) => {
|
|
219
|
+
const key = `${capability.kind}:${capability.name}`;
|
|
220
|
+
if (seen.has(key)) return false;
|
|
221
|
+
seen.add(key);
|
|
222
|
+
return true;
|
|
223
|
+
});
|
|
177
224
|
return {
|
|
178
225
|
id: nanoid(),
|
|
179
226
|
role: "user",
|
|
@@ -187,6 +234,7 @@ function createChatMessage(
|
|
|
187
234
|
createdAt,
|
|
188
235
|
authorDisplayName: "You",
|
|
189
236
|
messageSource: "Web",
|
|
237
|
+
...(requestedCapabilities.length > 0 ? { requestedCapabilities } : {}),
|
|
190
238
|
},
|
|
191
239
|
};
|
|
192
240
|
}
|
|
@@ -547,8 +595,9 @@ export function useUniversalAgentChat(
|
|
|
547
595
|
const sendText = useCallback(async (
|
|
548
596
|
text: string,
|
|
549
597
|
files?: readonly FileUIPart[],
|
|
598
|
+
capabilities?: readonly RequestedCapability[],
|
|
550
599
|
): Promise<boolean> => {
|
|
551
|
-
const message = createChatMessage(text, files);
|
|
600
|
+
const message = createChatMessage(text, files, capabilities);
|
|
552
601
|
return message
|
|
553
602
|
? submitMessage(message, "enqueue", "message")
|
|
554
603
|
: false;
|
|
@@ -557,21 +606,28 @@ export function useUniversalAgentChat(
|
|
|
557
606
|
const dispatchText = useCallback(async (
|
|
558
607
|
text: string,
|
|
559
608
|
files: readonly FileUIPart[] | undefined,
|
|
609
|
+
capabilities: readonly RequestedCapability[] | undefined,
|
|
560
610
|
delivery: MessageDelivery,
|
|
561
611
|
projection: "message" | "queue",
|
|
562
612
|
): Promise<boolean> => {
|
|
563
|
-
const message = createChatMessage(text, files);
|
|
613
|
+
const message = createChatMessage(text, files, capabilities);
|
|
564
614
|
return message ? submitMessage(message, delivery, projection) : false;
|
|
565
615
|
}, [submitMessage]);
|
|
566
616
|
|
|
567
617
|
const steerText = useCallback(
|
|
568
|
-
(
|
|
569
|
-
|
|
618
|
+
(
|
|
619
|
+
text: string,
|
|
620
|
+
files?: readonly FileUIPart[],
|
|
621
|
+
capabilities?: readonly RequestedCapability[],
|
|
622
|
+
) => dispatchText(text, files, capabilities, "steer", "message"),
|
|
570
623
|
[dispatchText],
|
|
571
624
|
);
|
|
572
625
|
const enqueueText = useCallback(
|
|
573
|
-
(
|
|
574
|
-
|
|
626
|
+
(
|
|
627
|
+
text: string,
|
|
628
|
+
files?: readonly FileUIPart[],
|
|
629
|
+
capabilities?: readonly RequestedCapability[],
|
|
630
|
+
) => dispatchText(text, files, capabilities, "enqueue", "queue"),
|
|
575
631
|
[dispatchText],
|
|
576
632
|
);
|
|
577
633
|
const steerQueued = useCallback(async (submissionId: string) => {
|
|
@@ -664,7 +720,50 @@ export function useUniversalAgentChat(
|
|
|
664
720
|
);
|
|
665
721
|
}, [submitMessage]);
|
|
666
722
|
|
|
723
|
+
// 交互卡片的写回口。失败只返回 false —— 调用方(块组件)自己决定怎么提示;
|
|
724
|
+
// 权威状态永远来自 part 的 state,不来自这里的返回值。
|
|
725
|
+
const respondToolInteraction = useCallback(async (
|
|
726
|
+
toolCallId: string,
|
|
727
|
+
response: unknown,
|
|
728
|
+
): Promise<boolean> => {
|
|
729
|
+
try {
|
|
730
|
+
const receipt = await agentRef.current.call<{ ok: boolean }>(
|
|
731
|
+
"respondToolInteraction",
|
|
732
|
+
[toolCallId, response],
|
|
733
|
+
);
|
|
734
|
+
return receipt.ok;
|
|
735
|
+
} catch {
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
}, []);
|
|
739
|
+
|
|
740
|
+
// 只读控制面:Runtime 尚未装配时由 Agent 侧 `ensureRuntimeReady` 负责等待,
|
|
741
|
+
// 这里不缓存结果 —— 换模型、加技能或 reload 之后调用方要拿到的是新装配。
|
|
742
|
+
const getRuntimeAssembly = useCallback(
|
|
743
|
+
() => agentRef.current.call<RuntimeAssemblyView>("getRuntimeAssembly", []),
|
|
744
|
+
[],
|
|
745
|
+
);
|
|
746
|
+
const updateConfig = useCallback(
|
|
747
|
+
<Change,>(command: unknown) =>
|
|
748
|
+
agentRef.current.call<RuntimeConfigUpdateResult<Change>>(
|
|
749
|
+
"updateConfig",
|
|
750
|
+
[command],
|
|
751
|
+
),
|
|
752
|
+
[],
|
|
753
|
+
);
|
|
754
|
+
const reloadRuntime = useCallback(
|
|
755
|
+
() => agentRef.current.call<void>(
|
|
756
|
+
"reloadRuntime",
|
|
757
|
+
[undefined, { force: true }],
|
|
758
|
+
),
|
|
759
|
+
[],
|
|
760
|
+
);
|
|
761
|
+
|
|
667
762
|
return {
|
|
763
|
+
respondToolInteraction,
|
|
764
|
+
getRuntimeAssembly,
|
|
765
|
+
updateConfig,
|
|
766
|
+
reloadRuntime,
|
|
668
767
|
messages: visibleMessages,
|
|
669
768
|
status,
|
|
670
769
|
runtimeLoad: facetState?.runtimeLoad,
|