@done-coding/forge-kit-agent 0.0.0-stage → 0.100.0-alpha.0
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/LICENSE +21 -0
- package/README.md +53 -2
- package/es/_virtual/_plugin-vue_export-helper.mjs +10 -0
- package/es/components/bubble/AiBubbleContent.vue.mjs +7 -0
- package/es/components/bubble/AiBubbleContent.vue2.mjs +209 -0
- package/es/components/bubble/AiBubbleFooter.vue.mjs +7 -0
- package/es/components/bubble/AiBubbleFooter.vue2.mjs +91 -0
- package/es/components/bubble/AiCodeHeaderControl.vue.mjs +7 -0
- package/es/components/bubble/AiCodeHeaderControl.vue2.mjs +97 -0
- package/es/components/bubble/AiContentSplit.vue.mjs +70 -0
- package/es/components/bubble/AiContentSplit.vue2.mjs +4 -0
- package/es/components/bubble/AiPartRenderer.vue.mjs +7 -0
- package/es/components/bubble/AiPartRenderer.vue2.mjs +184 -0
- package/es/components/bubble/AiThinkBox.vue.mjs +7 -0
- package/es/components/bubble/AiThinkBox.vue2.mjs +155 -0
- package/es/components/bubble/AiTypewriter.vue.mjs +81 -0
- package/es/components/bubble/AiTypewriter.vue2.mjs +4 -0
- package/es/components/config/AiConfigProvider.vue.mjs +31 -0
- package/es/components/config/AiConfigProvider.vue2.mjs +4 -0
- package/es/components/dialogue/AiDialogue.vue.mjs +7 -0
- package/es/components/dialogue/AiDialogue.vue2.mjs +242 -0
- package/es/components/dialogue/AiDialogueGroup.vue.mjs +7 -0
- package/es/components/dialogue/AiDialogueGroup.vue2.mjs +133 -0
- package/es/components/dialogue/AiDialogueWrap.vue.mjs +7 -0
- package/es/components/dialogue/AiDialogueWrap.vue2.mjs +73 -0
- package/es/components/dialogue/AiMessageList.vue.mjs +7 -0
- package/es/components/dialogue/AiMessageList.vue2.mjs +111 -0
- package/es/components/json-trigger/LlmJsonTrigger.vue.mjs +7 -0
- package/es/components/json-trigger/LlmJsonTrigger.vue2.mjs +130 -0
- package/es/components/operation/AiOperationBar.vue.mjs +7 -0
- package/es/components/operation/AiOperationBar.vue2.mjs +174 -0
- package/es/components/operation/AiOperationBtns.vue.mjs +7 -0
- package/es/components/operation/AiOperationBtns.vue2.mjs +140 -0
- package/es/components/sender/AiSender.vue.mjs +7 -0
- package/es/components/sender/AiSender.vue2.mjs +74 -0
- package/es/components/sender/AiSenderHeader.vue.mjs +7 -0
- package/es/components/sender/AiSenderHeader.vue2.mjs +78 -0
- package/es/hooks/CodeXSlot.mjs +20 -0
- package/es/hooks/SessionExpose.mjs +81 -0
- package/es/hooks/agent-tools.mjs +95 -0
- package/es/hooks/agent.mjs +143 -0
- package/es/hooks/bubble-list.mjs +64 -0
- package/es/hooks/expose.mjs +133 -0
- package/es/hooks/fix-bubble-list-bug.mjs +65 -0
- package/es/hooks/sender-history.mjs +28 -0
- package/es/hooks/sender.mjs +197 -0
- package/es/hooks/session-list.mjs +154 -0
- package/es/hooks/sse.mjs +401 -0
- package/es/hooks/storage.mjs +217 -0
- package/es/hooks/theme.mjs +253 -0
- package/es/hooks/use-density.mjs +100 -0
- package/es/hooks/use-empty-state-center.mjs +40 -0
- package/es/hooks/use-keyboard-inset.mjs +87 -0
- package/es/index.mjs +68 -0
- package/es/style.css +836 -0
- package/es/types/index.mjs +54 -0
- package/es/utils/index.mjs +34 -0
- package/es/utils/json-from-llm.mjs +26 -0
- package/es/utils/request-llm.mjs +60 -0
- package/package.json +83 -4
- package/types/bridge/config-hook.d.ts +21 -0
- package/types/bridge/index.d.ts +69 -0
- package/types/bridge/plugin.d.ts +39 -0
- package/types/bridge/state.d.ts +26 -0
- package/types/bridge/storage.d.ts +10 -0
- package/types/components/bubble/AiBubbleContent.vue.d.ts +57 -0
- package/types/components/bubble/AiBubbleFooter.vue.d.ts +6 -0
- package/types/components/bubble/AiCodeHeaderControl.vue.d.ts +7 -0
- package/types/components/bubble/AiContentSplit.vue.d.ts +36 -0
- package/types/components/bubble/AiPartRenderer.vue.d.ts +9 -0
- package/types/components/bubble/AiThinkBox.vue.d.ts +31 -0
- package/types/components/bubble/AiTypewriter.vue.d.ts +28 -0
- package/types/components/config/AiConfigProvider.vue.d.ts +28 -0
- package/types/components/dialogue/AiDialogue.vue.d.ts +546 -0
- package/types/components/dialogue/AiDialogueGroup.vue.d.ts +49 -0
- package/types/components/dialogue/AiDialogueWrap.vue.d.ts +38 -0
- package/types/components/dialogue/AiMessageList.vue.d.ts +65 -0
- package/types/components/json-trigger/LlmJsonTrigger.vue.d.ts +43 -0
- package/types/components/json-trigger/types.d.ts +12 -0
- package/types/components/operation/AiOperationBar.vue.d.ts +36 -0
- package/types/components/operation/AiOperationBtns.vue.d.ts +27 -0
- package/types/components/sender/AiSender.vue.d.ts +38 -0
- package/types/components/sender/AiSenderHeader.vue.d.ts +28 -0
- package/types/hooks/CodeXSlot.d.ts +7 -0
- package/types/hooks/SessionExpose.d.ts +20 -0
- package/types/hooks/agent-tools.d.ts +67 -0
- package/types/hooks/agent.d.ts +43 -0
- package/types/hooks/bubble-list.d.ts +22 -0
- package/types/hooks/expose.d.ts +111 -0
- package/types/hooks/fix-bubble-list-bug.d.ts +8 -0
- package/types/hooks/sender-history.d.ts +41 -0
- package/types/hooks/sender.d.ts +101 -0
- package/types/hooks/session-list.d.ts +36 -0
- package/types/hooks/sse.d.ts +239 -0
- package/types/hooks/storage.d.ts +70 -0
- package/types/hooks/theme.d.ts +37 -0
- package/types/hooks/use-density.d.ts +66 -0
- package/types/hooks/use-empty-state-center.d.ts +23 -0
- package/types/hooks/use-keyboard-inset.d.ts +37 -0
- package/types/index.d.ts +26 -0
- package/types/inject/key.d.ts +20 -0
- package/types/types/index.d.ts +612 -0
- package/types/utils/index.d.ts +29 -0
- package/types/utils/json-from-llm.d.ts +35 -0
- package/types/utils/request-llm.d.ts +44 -0
package/es/hooks/sse.mjs
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { computed, ref, watch } from "vue";
|
|
2
|
+
import { useXStream } from "vue-element-plus-x";
|
|
3
|
+
const parseSSEEventLine = (line) => {
|
|
4
|
+
const trimmed = line.trim();
|
|
5
|
+
if (!trimmed || trimmed.startsWith(":")) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const payload = trimmed.startsWith("data:") ? trimmed.slice(5).trim() : trimmed;
|
|
9
|
+
if (payload === "[DONE]") {
|
|
10
|
+
return { type: "done" };
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
return parseOpenAIChunk(JSON.parse(payload));
|
|
14
|
+
} catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const AI_SSE_ERROR_FRAME_FALLBACK_MSG = "回答生成失败";
|
|
19
|
+
const AI_SSE_EMPTY_RESPONSE_MSG = "未收到有效回复";
|
|
20
|
+
const resolveWireErrorMsg = (json) => {
|
|
21
|
+
const error = json == null ? void 0 : json.error;
|
|
22
|
+
if (!error) {
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
if (typeof error === "string") {
|
|
26
|
+
return error;
|
|
27
|
+
}
|
|
28
|
+
const msg = error.message;
|
|
29
|
+
return typeof msg === "string" && msg ? msg : AI_SSE_ERROR_FRAME_FALLBACK_MSG;
|
|
30
|
+
};
|
|
31
|
+
const parseOpenAIChunk = (json) => {
|
|
32
|
+
var _a;
|
|
33
|
+
const errorMsg = resolveWireErrorMsg(json);
|
|
34
|
+
if (errorMsg !== void 0) {
|
|
35
|
+
return { type: "error", errorMsg, raw: json };
|
|
36
|
+
}
|
|
37
|
+
const choice = (_a = json == null ? void 0 : json.choices) == null ? void 0 : _a[0];
|
|
38
|
+
const delta = (choice == null ? void 0 : choice.delta) ?? {};
|
|
39
|
+
const content = typeof delta.content === "string" ? delta.content : void 0;
|
|
40
|
+
const reasoningContent = typeof delta.reasoning_content === "string" ? delta.reasoning_content : void 0;
|
|
41
|
+
const toolCalls = Array.isArray(delta.tool_calls) ? delta.tool_calls : void 0;
|
|
42
|
+
const finishReason = typeof (choice == null ? void 0 : choice.finish_reason) === "string" || (choice == null ? void 0 : choice.finish_reason) === null ? choice.finish_reason : void 0;
|
|
43
|
+
const frame = {
|
|
44
|
+
type: content !== void 0 || toolCalls !== void 0 || reasoningContent !== void 0 ? "message" : (
|
|
45
|
+
// [MUST] 只认**非空字符串**的 finish_reason 为结束:`null` 是 wire 上的
|
|
46
|
+
// 「未结束」标记、几乎每帧都带。判成 done 的代价 = 无 content 的开场帧
|
|
47
|
+
// (`{delta:{role:"assistant"}}`,mock 与多数网关都发)在**第一帧**就把气泡
|
|
48
|
+
// 置完成态 + 触发 finish():流式 shimmer 当场死掉,收尾判定持久化后更会把
|
|
49
|
+
// 后续正文整段丢弃(demo 真机实测炸过)。
|
|
50
|
+
finishReason ? "done" : "message"
|
|
51
|
+
),
|
|
52
|
+
raw: json
|
|
53
|
+
};
|
|
54
|
+
if (content !== void 0) frame.content = content;
|
|
55
|
+
if (reasoningContent !== void 0) frame.reasoningContent = reasoningContent;
|
|
56
|
+
if (toolCalls !== void 0) frame.toolCalls = toolCalls;
|
|
57
|
+
if (finishReason !== void 0) frame.finishReason = finishReason;
|
|
58
|
+
return frame;
|
|
59
|
+
};
|
|
60
|
+
const mergeToolCallsDeltas = (deltas, seed) => {
|
|
61
|
+
const map = new Map(
|
|
62
|
+
(seed ?? []).map((call) => [call.index, { ...call }])
|
|
63
|
+
);
|
|
64
|
+
for (const delta of deltas) {
|
|
65
|
+
const target = map.get(delta.index) ?? { index: delta.index };
|
|
66
|
+
if (delta.id) target.id = delta.id;
|
|
67
|
+
const fn = delta.function;
|
|
68
|
+
if (fn == null ? void 0 : fn.name) target.name = fn.name;
|
|
69
|
+
if ((fn == null ? void 0 : fn.arguments) !== void 0) {
|
|
70
|
+
target.arguments = (target.arguments ?? "") + fn.arguments;
|
|
71
|
+
}
|
|
72
|
+
map.set(delta.index, target);
|
|
73
|
+
}
|
|
74
|
+
return [...map.values()];
|
|
75
|
+
};
|
|
76
|
+
const parseAiToolArguments = (raw) => {
|
|
77
|
+
const text = (raw ?? "").trim();
|
|
78
|
+
if (!text) {
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(text);
|
|
83
|
+
} catch {
|
|
84
|
+
return text;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const defaultSSEConverter = (frame) => [
|
|
88
|
+
...frame.reasoningContent ? [
|
|
89
|
+
{
|
|
90
|
+
type: "reasoning",
|
|
91
|
+
text: frame.reasoningContent,
|
|
92
|
+
state: "streaming"
|
|
93
|
+
}
|
|
94
|
+
] : [],
|
|
95
|
+
...frame.content ? [{ type: "text", text: frame.content }] : []
|
|
96
|
+
];
|
|
97
|
+
const upsertAiMessageParts = (parts, incoming) => {
|
|
98
|
+
const next = [...parts];
|
|
99
|
+
for (const part of incoming) {
|
|
100
|
+
const id = "id" in part && part.id ? part.id : void 0;
|
|
101
|
+
const at = id ? next.findIndex((p) => "id" in p && p.id === id) : -1;
|
|
102
|
+
if (at >= 0) {
|
|
103
|
+
const merged = { ...next[at], ...part };
|
|
104
|
+
next[at] = merged;
|
|
105
|
+
} else {
|
|
106
|
+
next.push(part);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return next;
|
|
110
|
+
};
|
|
111
|
+
const useAiSSE = ({
|
|
112
|
+
message,
|
|
113
|
+
input,
|
|
114
|
+
sseUrl,
|
|
115
|
+
model,
|
|
116
|
+
headers,
|
|
117
|
+
messages,
|
|
118
|
+
body,
|
|
119
|
+
converter = defaultSSEConverter,
|
|
120
|
+
initialContent,
|
|
121
|
+
initialParts,
|
|
122
|
+
initialPartOffsets,
|
|
123
|
+
projectToolCallParts = false,
|
|
124
|
+
finish,
|
|
125
|
+
changeMessage
|
|
126
|
+
}) => {
|
|
127
|
+
const symbol = Symbol(input);
|
|
128
|
+
const xStreamHook = useXStream();
|
|
129
|
+
const isResponding = computed({
|
|
130
|
+
get: () => !message.isFinished,
|
|
131
|
+
set: (val) => {
|
|
132
|
+
changeMessage({
|
|
133
|
+
isFinished: !val
|
|
134
|
+
});
|
|
135
|
+
if (!val) {
|
|
136
|
+
changeMessage({
|
|
137
|
+
loading: false
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
const contentRef = ref(initialContent ?? "");
|
|
143
|
+
const partsRef = ref([...initialParts ?? []]);
|
|
144
|
+
const partOffsetsRef = ref([...initialPartOffsets ?? []]);
|
|
145
|
+
let reasoningStartedAt;
|
|
146
|
+
const toolCallsRef = ref([]);
|
|
147
|
+
const finishReasonRef = ref();
|
|
148
|
+
let isEvoked = false;
|
|
149
|
+
const startSSE = async () => {
|
|
150
|
+
if (isEvoked) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
isEvoked = true;
|
|
154
|
+
try {
|
|
155
|
+
isResponding.value = true;
|
|
156
|
+
const resolvedHeaders = {
|
|
157
|
+
"Content-Type": "application/json",
|
|
158
|
+
...typeof headers === "function" ? await headers() : headers
|
|
159
|
+
};
|
|
160
|
+
const resolvedBody = {
|
|
161
|
+
model,
|
|
162
|
+
messages: messages ?? [{ role: "user", content: input }],
|
|
163
|
+
stream: true,
|
|
164
|
+
...body
|
|
165
|
+
};
|
|
166
|
+
let response;
|
|
167
|
+
try {
|
|
168
|
+
response = await fetch(sseUrl, {
|
|
169
|
+
headers: resolvedHeaders,
|
|
170
|
+
method: "POST",
|
|
171
|
+
body: JSON.stringify(resolvedBody)
|
|
172
|
+
});
|
|
173
|
+
} catch (error) {
|
|
174
|
+
const errMsg = error instanceof Error ? error.message : "";
|
|
175
|
+
throw new Error(`请求失败${errMsg ? `:${errMsg}` : ""}`);
|
|
176
|
+
}
|
|
177
|
+
if (!response.ok || responseIsJson(response)) {
|
|
178
|
+
throw await resolveFailResponse(response);
|
|
179
|
+
}
|
|
180
|
+
const readableStream = response.body;
|
|
181
|
+
changeMessage({
|
|
182
|
+
createdAt: Date.now()
|
|
183
|
+
});
|
|
184
|
+
const res = xStreamHook.startStream({ readableStream });
|
|
185
|
+
await res;
|
|
186
|
+
if (!message.errorMsg && !message.isAborted && !contentRef.value && !partsRef.value.length) {
|
|
187
|
+
changeMessage({ errorMsg: AI_SSE_EMPTY_RESPONSE_MSG });
|
|
188
|
+
}
|
|
189
|
+
} catch (err) {
|
|
190
|
+
changeMessage({
|
|
191
|
+
errorMsg: err instanceof Error ? err.message : "读流失败"
|
|
192
|
+
});
|
|
193
|
+
} finally {
|
|
194
|
+
isResponding.value = false;
|
|
195
|
+
finish(symbol);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
const cancel = () => {
|
|
199
|
+
isResponding.value = false;
|
|
200
|
+
if (xStreamHook.isLoading.value) {
|
|
201
|
+
settleReasoningParts();
|
|
202
|
+
changeMessage({
|
|
203
|
+
isAborted: true
|
|
204
|
+
});
|
|
205
|
+
xStreamHook.cancel();
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const settleReasoningParts = () => {
|
|
209
|
+
if (reasoningStartedAt === void 0) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const thinkTime = Math.round((Date.now() - reasoningStartedAt) / 1e3);
|
|
213
|
+
reasoningStartedAt = void 0;
|
|
214
|
+
partsRef.value = partsRef.value.map(
|
|
215
|
+
(part) => part.type === "reasoning" && part.state === "streaming" ? { ...part, state: "done", thinkTime } : part
|
|
216
|
+
);
|
|
217
|
+
changeMessage({ parts: partsRef.value });
|
|
218
|
+
};
|
|
219
|
+
const applyParts = (parts) => {
|
|
220
|
+
if (!parts.length) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const textParts = parts.filter(
|
|
224
|
+
(p) => p.type === "text"
|
|
225
|
+
);
|
|
226
|
+
const streamingReasoning = parts.filter(
|
|
227
|
+
(p) => p.type === "reasoning" && p.state === "streaming"
|
|
228
|
+
);
|
|
229
|
+
const rest = parts.filter(
|
|
230
|
+
(p) => p.type !== "text" && !(p.type === "reasoning" && p.state === "streaming")
|
|
231
|
+
);
|
|
232
|
+
if (streamingReasoning.length) {
|
|
233
|
+
const deltaText = streamingReasoning.map((p) => p.text).join("");
|
|
234
|
+
const next = [...partsRef.value];
|
|
235
|
+
const at = next.findIndex(
|
|
236
|
+
(p) => p.type === "reasoning" && p.state === "streaming"
|
|
237
|
+
);
|
|
238
|
+
if (at >= 0) {
|
|
239
|
+
const prev = next[at];
|
|
240
|
+
next[at] = { ...prev, text: prev.text + deltaText };
|
|
241
|
+
} else {
|
|
242
|
+
next.push({ type: "reasoning", text: deltaText, state: "streaming" });
|
|
243
|
+
partOffsetsRef.value = [
|
|
244
|
+
...partOffsetsRef.value,
|
|
245
|
+
contentRef.value.length
|
|
246
|
+
];
|
|
247
|
+
reasoningStartedAt = Date.now();
|
|
248
|
+
}
|
|
249
|
+
partsRef.value = next;
|
|
250
|
+
changeMessage({ parts: next, partOffsets: partOffsetsRef.value });
|
|
251
|
+
}
|
|
252
|
+
if (textParts.length) {
|
|
253
|
+
settleReasoningParts();
|
|
254
|
+
const deltaText = textParts.map((p) => p.text).join("");
|
|
255
|
+
const nextContent = (contentRef.value + deltaText).replace(
|
|
256
|
+
/``````/gm,
|
|
257
|
+
"```\n```"
|
|
258
|
+
);
|
|
259
|
+
contentRef.value = nextContent;
|
|
260
|
+
changeMessage({ content: nextContent }, true);
|
|
261
|
+
}
|
|
262
|
+
if (rest.length) {
|
|
263
|
+
settleReasoningParts();
|
|
264
|
+
const lengthBefore = partsRef.value.length;
|
|
265
|
+
partsRef.value = upsertAiMessageParts(partsRef.value, rest);
|
|
266
|
+
const appended = partsRef.value.length - lengthBefore;
|
|
267
|
+
if (appended > 0) {
|
|
268
|
+
const offset = contentRef.value.length;
|
|
269
|
+
partOffsetsRef.value = [
|
|
270
|
+
...partOffsetsRef.value,
|
|
271
|
+
...Array.from({ length: appended }, () => offset)
|
|
272
|
+
];
|
|
273
|
+
}
|
|
274
|
+
changeMessage({
|
|
275
|
+
parts: partsRef.value,
|
|
276
|
+
partOffsets: partOffsetsRef.value
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
const nextLoading = !contentRef.value && !partsRef.value.length;
|
|
280
|
+
if (message.loading !== nextLoading) {
|
|
281
|
+
changeMessage({ loading: nextLoading });
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
const projectToolParts = (settled) => toolCallsRef.value.filter((call) => call.id).map((call) => ({
|
|
285
|
+
type: "tool-call",
|
|
286
|
+
id: call.id,
|
|
287
|
+
name: call.name ?? "",
|
|
288
|
+
state: settled ? "input-available" : "input-streaming",
|
|
289
|
+
input: settled ? parseAiToolArguments(call.arguments) : call.arguments
|
|
290
|
+
}));
|
|
291
|
+
let isStreamSettled = false;
|
|
292
|
+
const consumeFrame = (line) => {
|
|
293
|
+
var _a, _b;
|
|
294
|
+
if (isStreamSettled) {
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
const frame = parseSSEEventLine(line);
|
|
298
|
+
if (!frame) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
if (frame.type === "error") {
|
|
302
|
+
settleReasoningParts();
|
|
303
|
+
changeMessage({
|
|
304
|
+
errorMsg: frame.errorMsg ?? AI_SSE_ERROR_FRAME_FALLBACK_MSG
|
|
305
|
+
});
|
|
306
|
+
isResponding.value = false;
|
|
307
|
+
changeMessage({
|
|
308
|
+
isFinished: true,
|
|
309
|
+
loading: false
|
|
310
|
+
});
|
|
311
|
+
finish(symbol);
|
|
312
|
+
isStreamSettled = true;
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
if (frame.finishReason !== void 0 && frame.finishReason !== null) {
|
|
316
|
+
finishReasonRef.value = frame.finishReason;
|
|
317
|
+
}
|
|
318
|
+
if ((_a = frame.toolCalls) == null ? void 0 : _a.length) {
|
|
319
|
+
toolCallsRef.value = mergeToolCallsDeltas(
|
|
320
|
+
frame.toolCalls,
|
|
321
|
+
toolCallsRef.value
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
if (frame.type === "done") {
|
|
325
|
+
settleReasoningParts();
|
|
326
|
+
if (projectToolCallParts) {
|
|
327
|
+
applyParts(projectToolParts(true));
|
|
328
|
+
}
|
|
329
|
+
isResponding.value = false;
|
|
330
|
+
changeMessage({
|
|
331
|
+
isFinished: true,
|
|
332
|
+
loading: false
|
|
333
|
+
});
|
|
334
|
+
finish(symbol);
|
|
335
|
+
isStreamSettled = true;
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
applyParts([
|
|
339
|
+
...converter(frame),
|
|
340
|
+
...projectToolCallParts && ((_b = frame.toolCalls) == null ? void 0 : _b.length) ? projectToolParts(false) : []
|
|
341
|
+
]);
|
|
342
|
+
return false;
|
|
343
|
+
};
|
|
344
|
+
watch(
|
|
345
|
+
() => xStreamHook.data.value.length,
|
|
346
|
+
(l, prev = 0) => {
|
|
347
|
+
for (let i = prev; i < l; i += 1) {
|
|
348
|
+
const item = xStreamHook.data.value[i];
|
|
349
|
+
if (consumeFrame((item == null ? void 0 : item.data) ?? "")) {
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
);
|
|
355
|
+
return {
|
|
356
|
+
startSSE,
|
|
357
|
+
symbol,
|
|
358
|
+
input,
|
|
359
|
+
isResponding,
|
|
360
|
+
cancel,
|
|
361
|
+
/** 累积正文(含 `initialContent` 种子)——工具循环续轮取它做下一实例的种子 */
|
|
362
|
+
content: contentRef,
|
|
363
|
+
/** 累积非文本 parts(含 `initialParts` 种子) */
|
|
364
|
+
parts: partsRef,
|
|
365
|
+
/** parts 正文偏移(与 parts 平行;工具循环续轮取它做下一实例的种子) */
|
|
366
|
+
partOffsets: partOffsetsRef,
|
|
367
|
+
/** 跨帧累积的工具调用(`finish_reason:"tool_calls"` 时即待执行清单) */
|
|
368
|
+
toolCalls: toolCallsRef,
|
|
369
|
+
/** 本轮 `finish_reason` */
|
|
370
|
+
finishReason: finishReasonRef
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
const responseIsJson = (response) => {
|
|
374
|
+
var _a;
|
|
375
|
+
return Boolean((_a = response.headers.get("content-type")) == null ? void 0 : _a.includes("application/json"));
|
|
376
|
+
};
|
|
377
|
+
const resolveFailResponse = async (response) => {
|
|
378
|
+
var _a;
|
|
379
|
+
const { status } = response;
|
|
380
|
+
let errorMsg = `出错了:${status}`;
|
|
381
|
+
if (responseIsJson(response)) {
|
|
382
|
+
try {
|
|
383
|
+
const res = await response.json();
|
|
384
|
+
const rawMessage = ((_a = res == null ? void 0 : res.error) == null ? void 0 : _a.message) || (res == null ? void 0 : res.message);
|
|
385
|
+
if (rawMessage) {
|
|
386
|
+
errorMsg = rawMessage;
|
|
387
|
+
}
|
|
388
|
+
} catch {
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return new Error(errorMsg);
|
|
392
|
+
};
|
|
393
|
+
export {
|
|
394
|
+
defaultSSEConverter,
|
|
395
|
+
mergeToolCallsDeltas,
|
|
396
|
+
parseAiToolArguments,
|
|
397
|
+
parseOpenAIChunk,
|
|
398
|
+
parseSSEEventLine,
|
|
399
|
+
upsertAiMessageParts,
|
|
400
|
+
useAiSSE
|
|
401
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import Dexie from "dexie";
|
|
5
|
+
import { ref, computed } from "vue";
|
|
6
|
+
class AiDb extends Dexie {
|
|
7
|
+
/**
|
|
8
|
+
* @param key 库标识(通常 = userName,用户数据隔离)
|
|
9
|
+
* @param namespace 命名空间(对齐 bridge 的 namespace 机制,库名 = `${namespace}/ai-db/${key}`)——
|
|
10
|
+
* 组件库不区分环境(无 DEV 后缀),多应用/多环境隔离由消费方传 namespace 实现
|
|
11
|
+
*/
|
|
12
|
+
constructor(key, namespace) {
|
|
13
|
+
super(namespace ? `${namespace}/ai-db/${key}` : `ai-db/${key}`);
|
|
14
|
+
/** 会话表 */
|
|
15
|
+
__publicField(this, "sessions");
|
|
16
|
+
/** 消息表 */
|
|
17
|
+
__publicField(this, "messages");
|
|
18
|
+
__publicField(this, "init", async () => {
|
|
19
|
+
this.version(1).stores({
|
|
20
|
+
[
|
|
21
|
+
"sessions"
|
|
22
|
+
/* SESSIONS */
|
|
23
|
+
]: `&id, createdAt, label, lastMsgCreatedAt, group, msgCount, isDel`,
|
|
24
|
+
[
|
|
25
|
+
"messages"
|
|
26
|
+
/* MESSAGES */
|
|
27
|
+
]: `&uuid, sessionId, [sessionId+createdAt], role, createdAt, errorMsg, isAborted, isFinished, thinkTime, talkGoalLabel, talkGoalValue, askInput, askUuid, showThink, replyUuid, isDel`
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const createAiDb = (key, namespace) => {
|
|
33
|
+
const db = new AiDb(key, namespace);
|
|
34
|
+
db.init();
|
|
35
|
+
return db;
|
|
36
|
+
};
|
|
37
|
+
const useStoragePage = (config) => {
|
|
38
|
+
const isLoading = ref(false);
|
|
39
|
+
const isFinished = ref(false);
|
|
40
|
+
const pageSize = ref(config.pageSize);
|
|
41
|
+
const beforeCreatedAt = ref(Date.now());
|
|
42
|
+
const list = ref([]);
|
|
43
|
+
const pageParams = computed(() => {
|
|
44
|
+
return {
|
|
45
|
+
beforeCreatedAt: beforeCreatedAt.value,
|
|
46
|
+
pageSize: pageSize.value
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
const getList = async () => {
|
|
50
|
+
if (isFinished.value) {
|
|
51
|
+
throw new Error("查询已完成");
|
|
52
|
+
}
|
|
53
|
+
if (isLoading.value) {
|
|
54
|
+
throw new Error("查询中");
|
|
55
|
+
}
|
|
56
|
+
isLoading.value = true;
|
|
57
|
+
const params = pageParams.value;
|
|
58
|
+
return config.getListFn(params).then((data) => {
|
|
59
|
+
if (data.length < params.pageSize) {
|
|
60
|
+
isFinished.value = true;
|
|
61
|
+
}
|
|
62
|
+
beforeCreatedAt.value = config.getNextBeforeCreatedAt(data);
|
|
63
|
+
list.value.push(...data);
|
|
64
|
+
return data;
|
|
65
|
+
}).finally(() => {
|
|
66
|
+
isLoading.value = false;
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
list: computed(() => list.value),
|
|
71
|
+
isLoadMoreLoading: computed(() => isLoading.value),
|
|
72
|
+
isLoadMoreFinished: computed(() => isFinished.value),
|
|
73
|
+
loadMore: getList
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const useSessionListStorage = (db) => {
|
|
77
|
+
const addSession = async (session) => {
|
|
78
|
+
if (!db) {
|
|
79
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
80
|
+
}
|
|
81
|
+
return db[
|
|
82
|
+
"sessions"
|
|
83
|
+
/* SESSIONS */
|
|
84
|
+
].add({
|
|
85
|
+
...session,
|
|
86
|
+
isDel: false
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
const updateSession = async ({
|
|
90
|
+
id,
|
|
91
|
+
...rest
|
|
92
|
+
}) => {
|
|
93
|
+
if (!db) {
|
|
94
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
95
|
+
}
|
|
96
|
+
return db[
|
|
97
|
+
"sessions"
|
|
98
|
+
/* SESSIONS */
|
|
99
|
+
].update(id, rest);
|
|
100
|
+
};
|
|
101
|
+
const deleteSession = async ({ id }) => {
|
|
102
|
+
if (!db) {
|
|
103
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
104
|
+
}
|
|
105
|
+
return db[
|
|
106
|
+
"sessions"
|
|
107
|
+
/* SESSIONS */
|
|
108
|
+
].delete(id);
|
|
109
|
+
};
|
|
110
|
+
const getSessionList = async ({ beforeCreatedAt, pageSize }) => {
|
|
111
|
+
if (!db) {
|
|
112
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
113
|
+
}
|
|
114
|
+
const data = await db[
|
|
115
|
+
"sessions"
|
|
116
|
+
/* SESSIONS */
|
|
117
|
+
].where("lastMsgCreatedAt").between(Dexie.minKey, beforeCreatedAt).reverse().limit(pageSize).toArray();
|
|
118
|
+
return data.map(({ isDel, ...item }) => {
|
|
119
|
+
return {
|
|
120
|
+
...item
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
const { isLoadMoreLoading, isLoadMoreFinished, loadMore, list } = useStoragePage({
|
|
125
|
+
pageSize: 100,
|
|
126
|
+
getListFn: getSessionList,
|
|
127
|
+
getNextBeforeCreatedAt: (list2) => {
|
|
128
|
+
var _a;
|
|
129
|
+
return ((_a = list2[list2.length - 1]) == null ? void 0 : _a.createdAt) || 0;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
isLoadMoreLoading,
|
|
134
|
+
isLoadMoreFinished,
|
|
135
|
+
list,
|
|
136
|
+
addSession,
|
|
137
|
+
updateSession,
|
|
138
|
+
deleteSession,
|
|
139
|
+
loadMore
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
const useMessageListStorage = ({
|
|
143
|
+
db,
|
|
144
|
+
sessionId
|
|
145
|
+
}) => {
|
|
146
|
+
const addMessage = async (message) => {
|
|
147
|
+
if (!db) {
|
|
148
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
149
|
+
}
|
|
150
|
+
return db[
|
|
151
|
+
"messages"
|
|
152
|
+
/* MESSAGES */
|
|
153
|
+
].add({
|
|
154
|
+
...message,
|
|
155
|
+
isDel: false
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
const updateMessage = async ({
|
|
159
|
+
uuid,
|
|
160
|
+
...rest
|
|
161
|
+
}) => {
|
|
162
|
+
if (!db) {
|
|
163
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
164
|
+
}
|
|
165
|
+
return db[
|
|
166
|
+
"messages"
|
|
167
|
+
/* MESSAGES */
|
|
168
|
+
].update(uuid, rest);
|
|
169
|
+
};
|
|
170
|
+
const deleteMessage = async ({ uuid }) => {
|
|
171
|
+
if (!db) {
|
|
172
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
173
|
+
}
|
|
174
|
+
return db[
|
|
175
|
+
"messages"
|
|
176
|
+
/* MESSAGES */
|
|
177
|
+
].delete(uuid);
|
|
178
|
+
};
|
|
179
|
+
const getMessageList = async (params) => {
|
|
180
|
+
if (!db) {
|
|
181
|
+
return Promise.reject(new Error("数据库未初始化"));
|
|
182
|
+
}
|
|
183
|
+
const data = await db[
|
|
184
|
+
"messages"
|
|
185
|
+
/* MESSAGES */
|
|
186
|
+
].where("[sessionId+createdAt]").between([sessionId, Dexie.minKey], [sessionId, params.beforeCreatedAt]).limit(params.pageSize).toArray();
|
|
187
|
+
return data.map(({ isDel, ...item }) => {
|
|
188
|
+
return {
|
|
189
|
+
...item
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
const { isLoadMoreLoading, isLoadMoreFinished, loadMore, list } = useStoragePage({
|
|
194
|
+
// TODO: 单个会话消息列表目前没有触发加载更多 业务可能也不需要 后续设置消息数量过多 引导打开新会话
|
|
195
|
+
pageSize: 128,
|
|
196
|
+
getListFn: getMessageList,
|
|
197
|
+
getNextBeforeCreatedAt: (list2) => {
|
|
198
|
+
var _a;
|
|
199
|
+
return ((_a = list2[list2.length - 1]) == null ? void 0 : _a.createdAt) || 0;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
isLoadMoreLoading,
|
|
204
|
+
isLoadMoreFinished,
|
|
205
|
+
list,
|
|
206
|
+
addMessage,
|
|
207
|
+
updateMessage,
|
|
208
|
+
deleteMessage,
|
|
209
|
+
loadMore
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
export {
|
|
213
|
+
AiDb,
|
|
214
|
+
createAiDb,
|
|
215
|
+
useMessageListStorage,
|
|
216
|
+
useSessionListStorage
|
|
217
|
+
};
|