@done-coding/forge-kit-agent 0.0.0-placeholder.0 → 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 +54 -0
- 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 +85 -1
- 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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { shallowRef, computed, inject } from "vue";
|
|
2
|
+
import { useAiSessionProvide, AI_EXPOSE_PROVIDE, useExposeCtrl } from "./expose.mjs";
|
|
3
|
+
import { ElMessage } from "element-plus";
|
|
4
|
+
const useSessionExpose = ({
|
|
5
|
+
senderValue,
|
|
6
|
+
isResponding,
|
|
7
|
+
sessionInfo,
|
|
8
|
+
handleCancel,
|
|
9
|
+
handleSubmit
|
|
10
|
+
}) => {
|
|
11
|
+
const bubbleListExpose = shallowRef();
|
|
12
|
+
const senderExpose = shallowRef();
|
|
13
|
+
const message = ElMessage;
|
|
14
|
+
const sessionId = computed(() => sessionInfo.value.id);
|
|
15
|
+
const ask = async ({
|
|
16
|
+
value: valueInit,
|
|
17
|
+
parts
|
|
18
|
+
}) => {
|
|
19
|
+
const askValue = (valueInit == null ? void 0 : valueInit.trim()) ?? "";
|
|
20
|
+
const askParts = (parts == null ? void 0 : parts.length) ? parts : void 0;
|
|
21
|
+
if (!askValue && !askParts) {
|
|
22
|
+
const errMsg = `请输入内容`;
|
|
23
|
+
message.error(errMsg);
|
|
24
|
+
throw new Error(errMsg);
|
|
25
|
+
}
|
|
26
|
+
if (isResponding.value) {
|
|
27
|
+
handleCancel();
|
|
28
|
+
}
|
|
29
|
+
senderValue.value = askValue;
|
|
30
|
+
handleSubmit(askValue, askParts ? {
|
|
31
|
+
parts: askParts
|
|
32
|
+
} : void 0);
|
|
33
|
+
return {
|
|
34
|
+
id: sessionId.value
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const sessionExpose = computed(() => {
|
|
38
|
+
return {
|
|
39
|
+
sessionId: sessionId.value,
|
|
40
|
+
sender: senderExpose.value,
|
|
41
|
+
bubbleList: bubbleListExpose.value,
|
|
42
|
+
ask
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
useAiSessionProvide(sessionExpose);
|
|
46
|
+
const addSessionExpose = inject(AI_EXPOSE_PROVIDE.AI_ADD_SESSION_EXPOSE, () => {
|
|
47
|
+
throw new Error(`未获取到上级注入的添加会话函数`);
|
|
48
|
+
});
|
|
49
|
+
const removeSessionExpose = inject(AI_EXPOSE_PROVIDE.AI_REMOVE_SESSION_EXPOSE, () => {
|
|
50
|
+
throw new Error(`未获取到上级注入的移除会话函数`);
|
|
51
|
+
});
|
|
52
|
+
useExposeCtrl(() => sessionExpose.value, (expose) => {
|
|
53
|
+
if (expose) {
|
|
54
|
+
addSessionExpose(sessionId.value, expose);
|
|
55
|
+
} else {
|
|
56
|
+
removeSessionExpose(sessionId.value);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
senderExpose,
|
|
61
|
+
bubbleListExpose
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const useSessionOperateMount = (activeSessionId, sessionOperate) => {
|
|
65
|
+
const setActiveSessionId = inject(AI_EXPOSE_PROVIDE.AI_SET_ACTIVE_SESSION_ID, () => {
|
|
66
|
+
throw new Error("未获取到上级注入的设置激活会话id函数");
|
|
67
|
+
});
|
|
68
|
+
const mountSessionOperate = inject(AI_EXPOSE_PROVIDE.AI_MOUNT_SESSION_OPERATE, () => {
|
|
69
|
+
throw new Error("未获取到上级注入的会话操作挂载函数");
|
|
70
|
+
});
|
|
71
|
+
useExposeCtrl(() => activeSessionId.value, (id) => {
|
|
72
|
+
setActiveSessionId(id);
|
|
73
|
+
});
|
|
74
|
+
useExposeCtrl(() => sessionOperate, (expose) => {
|
|
75
|
+
mountSessionOperate(expose);
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
export {
|
|
79
|
+
useSessionExpose,
|
|
80
|
+
useSessionOperateMount
|
|
81
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { AiToolDeniedError } from "../types/index.mjs";
|
|
2
|
+
import { parseAiToolArguments, upsertAiMessageParts } from "./sse.mjs";
|
|
3
|
+
const agentToolProjectWire = (tools) => tools.map((tool) => ({
|
|
4
|
+
type: "function",
|
|
5
|
+
function: {
|
|
6
|
+
name: tool.name,
|
|
7
|
+
description: tool.description,
|
|
8
|
+
parameters: tool.inputSchema
|
|
9
|
+
}
|
|
10
|
+
}));
|
|
11
|
+
const agentToolResolveExecutor = (toolName, tools, onToolCall) => {
|
|
12
|
+
const hit = toolName ? tools.find((tool) => tool.name === toolName) : void 0;
|
|
13
|
+
return (hit == null ? void 0 : hit.execute) ?? onToolCall;
|
|
14
|
+
};
|
|
15
|
+
const agentToolCanHandleAll = (calls, tools, onToolCall) => calls.length > 0 && calls.every(
|
|
16
|
+
(call) => Boolean(call.id) && Boolean(call.name) && Boolean(agentToolResolveExecutor(call.name, tools, onToolCall))
|
|
17
|
+
);
|
|
18
|
+
const agentToolStringify = (value) => {
|
|
19
|
+
if (typeof value === "string") {
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
if (value === void 0) {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return JSON.stringify(value) ?? String(value);
|
|
27
|
+
} catch {
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const agentToolRun = async (call, executor, run) => {
|
|
32
|
+
const ctx = {
|
|
33
|
+
toolCallId: call.id,
|
|
34
|
+
toolName: call.name,
|
|
35
|
+
...run
|
|
36
|
+
};
|
|
37
|
+
const base = { toolCallId: ctx.toolCallId, toolName: ctx.toolName };
|
|
38
|
+
try {
|
|
39
|
+
const output = await executor(parseAiToolArguments(call.arguments), ctx);
|
|
40
|
+
return {
|
|
41
|
+
...base,
|
|
42
|
+
state: "output-available",
|
|
43
|
+
output,
|
|
44
|
+
content: agentToolStringify(output)
|
|
45
|
+
};
|
|
46
|
+
} catch (error) {
|
|
47
|
+
const text = error instanceof Error ? error.message : String(error ?? "工具执行失败");
|
|
48
|
+
const denied = error instanceof AiToolDeniedError;
|
|
49
|
+
return {
|
|
50
|
+
...base,
|
|
51
|
+
state: denied ? "output-denied" : "output-error",
|
|
52
|
+
errorText: text,
|
|
53
|
+
// 拒绝 / 失败同样要回发,模型据此改道;[MUST NOT] 因为"失败了"就不回
|
|
54
|
+
content: denied ? `用户拒绝执行该工具:${text}` : `工具执行失败:${text}`
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const agentToolBuildAssistantMessage = (stepText, calls) => {
|
|
59
|
+
const toolCalls = calls.map((call) => ({
|
|
60
|
+
id: call.id,
|
|
61
|
+
type: "function",
|
|
62
|
+
function: { name: call.name, arguments: call.arguments ?? "" }
|
|
63
|
+
}));
|
|
64
|
+
return {
|
|
65
|
+
role: "assistant",
|
|
66
|
+
content: stepText || null,
|
|
67
|
+
tool_calls: toolCalls
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const agentToolBuildResultMessages = (outcomes) => outcomes.map((outcome) => ({
|
|
71
|
+
role: "tool",
|
|
72
|
+
tool_call_id: outcome.toolCallId,
|
|
73
|
+
content: outcome.content
|
|
74
|
+
}));
|
|
75
|
+
const agentToolApplyOutcomes = (parts, outcomes) => upsertAiMessageParts(
|
|
76
|
+
[...parts],
|
|
77
|
+
outcomes.map((outcome) => ({
|
|
78
|
+
type: "tool-call",
|
|
79
|
+
id: outcome.toolCallId,
|
|
80
|
+
name: outcome.toolName,
|
|
81
|
+
state: outcome.state,
|
|
82
|
+
...outcome.output === void 0 ? {} : { output: outcome.output },
|
|
83
|
+
...outcome.errorText === void 0 ? {} : { errorText: outcome.errorText }
|
|
84
|
+
}))
|
|
85
|
+
);
|
|
86
|
+
export {
|
|
87
|
+
agentToolApplyOutcomes,
|
|
88
|
+
agentToolBuildAssistantMessage,
|
|
89
|
+
agentToolBuildResultMessages,
|
|
90
|
+
agentToolCanHandleAll,
|
|
91
|
+
agentToolProjectWire,
|
|
92
|
+
agentToolResolveExecutor,
|
|
93
|
+
agentToolRun,
|
|
94
|
+
agentToolStringify
|
|
95
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ref, computed, nextTick } from "vue";
|
|
2
|
+
import { useAiSSE } from "./sse.mjs";
|
|
3
|
+
import { agentToolProjectWire, agentToolCanHandleAll, agentToolRun, agentToolResolveExecutor, agentToolApplyOutcomes, agentToolBuildAssistantMessage, agentToolBuildResultMessages } from "./agent-tools.mjs";
|
|
4
|
+
const whenAborted = (signal) => signal.aborted ? Promise.resolve() : new Promise((resolve) => {
|
|
5
|
+
signal.addEventListener("abort", () => resolve(), { once: true });
|
|
6
|
+
});
|
|
7
|
+
const ABORTED = Symbol("agent-tool-run-aborted");
|
|
8
|
+
const AI_AGENT_DEFAULT_MAX_STEPS = 10;
|
|
9
|
+
const useAiAgent = ({
|
|
10
|
+
tools = [],
|
|
11
|
+
onToolCall,
|
|
12
|
+
maxSteps = AI_AGENT_DEFAULT_MAX_STEPS,
|
|
13
|
+
messages,
|
|
14
|
+
body,
|
|
15
|
+
finish,
|
|
16
|
+
changeMessage,
|
|
17
|
+
...sseOptions
|
|
18
|
+
}) => {
|
|
19
|
+
const symbol = Symbol(sseOptions.input);
|
|
20
|
+
const stopReason = ref();
|
|
21
|
+
const respondingRef = ref(false);
|
|
22
|
+
let cancelled = false;
|
|
23
|
+
const abortController = new AbortController();
|
|
24
|
+
let currentSSE;
|
|
25
|
+
let isEvoked = false;
|
|
26
|
+
const resolvedBody = tools.length ? { tools: agentToolProjectWire(tools), ...body } : body;
|
|
27
|
+
const projectToolCallParts = tools.length > 0 || Boolean(onToolCall);
|
|
28
|
+
const start = async () => {
|
|
29
|
+
if (isEvoked) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
isEvoked = true;
|
|
33
|
+
respondingRef.value = true;
|
|
34
|
+
let wireMessages = [
|
|
35
|
+
...messages ?? [{ role: "user", content: sseOptions.input }]
|
|
36
|
+
];
|
|
37
|
+
let content = "";
|
|
38
|
+
let parts = [];
|
|
39
|
+
let partOffsets = [];
|
|
40
|
+
const stepLimit = Math.max(1, maxSteps);
|
|
41
|
+
for (let step = 1; step <= stepLimit; step += 1) {
|
|
42
|
+
if (cancelled) {
|
|
43
|
+
stopReason.value = "cancelled";
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
const contentBefore = content;
|
|
47
|
+
const sse = useAiSSE({
|
|
48
|
+
...sseOptions,
|
|
49
|
+
messages: wireMessages,
|
|
50
|
+
body: resolvedBody,
|
|
51
|
+
initialContent: content,
|
|
52
|
+
initialParts: parts,
|
|
53
|
+
initialPartOffsets: partOffsets,
|
|
54
|
+
projectToolCallParts,
|
|
55
|
+
changeMessage,
|
|
56
|
+
// 每步的收尾由本层统一裁决,内部实例不对外 finish
|
|
57
|
+
finish: () => void 0
|
|
58
|
+
});
|
|
59
|
+
currentSSE = sse;
|
|
60
|
+
await sse.startSSE();
|
|
61
|
+
await nextTick();
|
|
62
|
+
content = sse.content.value;
|
|
63
|
+
parts = sse.parts.value;
|
|
64
|
+
partOffsets = sse.partOffsets.value;
|
|
65
|
+
if (cancelled) {
|
|
66
|
+
stopReason.value = "cancelled";
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
if (sseOptions.message.errorMsg) {
|
|
70
|
+
stopReason.value = "error";
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
const calls = sse.toolCalls.value;
|
|
74
|
+
if (sse.finishReason.value !== "tool_calls" || !calls.length) {
|
|
75
|
+
stopReason.value = "stop";
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
if (!agentToolCanHandleAll(calls, tools, onToolCall)) {
|
|
79
|
+
stopReason.value = "unhandled-tool-calls";
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
if (step === stepLimit) {
|
|
83
|
+
stopReason.value = "max-steps";
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
changeMessage({ isFinished: false });
|
|
87
|
+
const outcomes = await Promise.race([
|
|
88
|
+
Promise.all(
|
|
89
|
+
calls.map(
|
|
90
|
+
(call) => agentToolRun(
|
|
91
|
+
call,
|
|
92
|
+
agentToolResolveExecutor(call.name, tools, onToolCall),
|
|
93
|
+
{ step, signal: abortController.signal }
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
),
|
|
97
|
+
whenAborted(abortController.signal).then(() => ABORTED)
|
|
98
|
+
]);
|
|
99
|
+
if (outcomes === ABORTED) {
|
|
100
|
+
stopReason.value = "cancelled";
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
if (cancelled) {
|
|
104
|
+
stopReason.value = "cancelled";
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
parts = agentToolApplyOutcomes(parts, outcomes);
|
|
108
|
+
changeMessage({ parts });
|
|
109
|
+
wireMessages = [
|
|
110
|
+
...wireMessages,
|
|
111
|
+
// 本步**新增**正文(非累计全文,否则多步会把前文重复喂回)
|
|
112
|
+
agentToolBuildAssistantMessage(
|
|
113
|
+
content.slice(contentBefore.length),
|
|
114
|
+
calls
|
|
115
|
+
),
|
|
116
|
+
...agentToolBuildResultMessages(outcomes)
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
respondingRef.value = false;
|
|
120
|
+
changeMessage({ isFinished: true, loading: false });
|
|
121
|
+
finish(symbol);
|
|
122
|
+
};
|
|
123
|
+
const cancel = () => {
|
|
124
|
+
cancelled = true;
|
|
125
|
+
stopReason.value = "cancelled";
|
|
126
|
+
currentSSE == null ? void 0 : currentSSE.cancel();
|
|
127
|
+
abortController.abort();
|
|
128
|
+
respondingRef.value = false;
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
start,
|
|
132
|
+
symbol,
|
|
133
|
+
input: sseOptions.input,
|
|
134
|
+
isResponding: computed(() => respondingRef.value),
|
|
135
|
+
cancel,
|
|
136
|
+
/** 收敛原因(run 结束后可读) */
|
|
137
|
+
stopReason: computed(() => stopReason.value)
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
export {
|
|
141
|
+
AI_AGENT_DEFAULT_MAX_STEPS,
|
|
142
|
+
useAiAgent
|
|
143
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { computed, ref } from "vue";
|
|
2
|
+
import { useMessageListStorage } from "./storage.mjs";
|
|
3
|
+
const BUBBLE_LIST_CARD_ONLY_LABEL = "[数据卡片]";
|
|
4
|
+
const useBubbleListScoped = ({
|
|
5
|
+
db,
|
|
6
|
+
sessionInfo,
|
|
7
|
+
addSessionToList,
|
|
8
|
+
updateSession
|
|
9
|
+
}) => {
|
|
10
|
+
const sessionId = computed(() => sessionInfo.value.id);
|
|
11
|
+
const {
|
|
12
|
+
list: historyMsgList,
|
|
13
|
+
isLoadMoreLoading,
|
|
14
|
+
isLoadMoreFinished,
|
|
15
|
+
addMessage,
|
|
16
|
+
updateMessage,
|
|
17
|
+
loadMore
|
|
18
|
+
} = useMessageListStorage({
|
|
19
|
+
db,
|
|
20
|
+
// eslint-disable-next-line vue/no-ref-object-destructure -- 1:1 迁入源既有,REQ-2 禁改(见 review-notes)
|
|
21
|
+
sessionId: sessionId.value
|
|
22
|
+
});
|
|
23
|
+
const memoryMsgList = ref([]);
|
|
24
|
+
const messages = computed(
|
|
25
|
+
() => historyMsgList.value.concat(memoryMsgList.value)
|
|
26
|
+
);
|
|
27
|
+
const addBubble = (item) => {
|
|
28
|
+
addSessionToList(sessionInfo.value);
|
|
29
|
+
item.sessionId = sessionId.value;
|
|
30
|
+
item.createdAt = Date.now();
|
|
31
|
+
item.updatedAt = item.createdAt;
|
|
32
|
+
if (messages.value.length === 0) {
|
|
33
|
+
updateSession({
|
|
34
|
+
id: sessionId.value,
|
|
35
|
+
label: item.content || BUBBLE_LIST_CARD_ONLY_LABEL
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
memoryMsgList.value.push(item);
|
|
39
|
+
addMessage({
|
|
40
|
+
...item,
|
|
41
|
+
// 存储信息是 loading直接设置为false, 且 完成 避免异常终端 下次进来存储数据仍为loading
|
|
42
|
+
loading: false,
|
|
43
|
+
isFinished: true
|
|
44
|
+
});
|
|
45
|
+
updateSession({
|
|
46
|
+
id: sessionId.value,
|
|
47
|
+
lastMsgCreatedAt: item.createdAt,
|
|
48
|
+
msgCount: (sessionInfo.value.msgCount || 0) + 1
|
|
49
|
+
});
|
|
50
|
+
const index = messages.value.length - 1;
|
|
51
|
+
return messages.value[index];
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
isLoadMoreLoading,
|
|
55
|
+
isLoadMoreFinished,
|
|
56
|
+
list: messages,
|
|
57
|
+
addBubble,
|
|
58
|
+
updateMessage,
|
|
59
|
+
loadMore
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
useBubbleListScoped
|
|
64
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { inject, reactive, ref, shallowRef, computed, provide, onMounted, watch, onActivated, onDeactivated, onBeforeUnmount } from "vue";
|
|
2
|
+
const AI_EXPOSE_PROVIDE = {
|
|
3
|
+
/** expose */
|
|
4
|
+
AI_EXPOSE: Symbol("AI_EXPOSE"),
|
|
5
|
+
/** 添加 session expose */
|
|
6
|
+
AI_ADD_SESSION_EXPOSE: Symbol("AI_ADD_SESSION_EXPOSE"),
|
|
7
|
+
/** 移除 session expose */
|
|
8
|
+
AI_REMOVE_SESSION_EXPOSE: Symbol("AI_REMOVE_SESSION_EXPOSE"),
|
|
9
|
+
/** 设置聚焦的会话id */
|
|
10
|
+
AI_SET_ACTIVE_SESSION_ID: Symbol("AI_SET_ACTIVE_SESSION_ID"),
|
|
11
|
+
/** 挂载会话级别的操作 */
|
|
12
|
+
AI_MOUNT_SESSION_OPERATE: Symbol("AI_MOUNT_SESSION_OPERATE"),
|
|
13
|
+
/** 单个 会话 expose */
|
|
14
|
+
AI_SESSION_EXPOSE: Symbol("AI_SESSION_EXPOSE")
|
|
15
|
+
};
|
|
16
|
+
const warnDuplicateExposeProvide = () => {
|
|
17
|
+
console.warn(
|
|
18
|
+
"[forge-agent] 检测到重复自持 expose provider:上级已 provide 过 AI_EXPOSE(通常是 AiConfigProvider)。内层 provide 会**遮蔽**外层,外层那个的注册表恒空、ask 恒 undefined。子树外要拿 ask 请用 AiConfigProvider 的 defineExpose,子树内用 useAiExposeInject,[MUST NOT] 自己再 provide 一遍。"
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
const useAiExposeProvide = () => {
|
|
22
|
+
if (inject(
|
|
23
|
+
AI_EXPOSE_PROVIDE.AI_EXPOSE,
|
|
24
|
+
void 0
|
|
25
|
+
)) {
|
|
26
|
+
warnDuplicateExposeProvide();
|
|
27
|
+
}
|
|
28
|
+
const sessionExposeMap = reactive(/* @__PURE__ */ new Map());
|
|
29
|
+
const activeSessionId = ref();
|
|
30
|
+
const sessionOperate = shallowRef();
|
|
31
|
+
const activeSessionExpose = computed(() => {
|
|
32
|
+
const id = activeSessionId.value;
|
|
33
|
+
if (id) {
|
|
34
|
+
return sessionExposeMap.get(id);
|
|
35
|
+
}
|
|
36
|
+
if (sessionExposeMap.size === 1) {
|
|
37
|
+
return sessionExposeMap.values().next().value;
|
|
38
|
+
}
|
|
39
|
+
return void 0;
|
|
40
|
+
});
|
|
41
|
+
const aiExpose = computed(() => {
|
|
42
|
+
const { sessionId, sender, bubbleList, ask } = activeSessionExpose.value || {};
|
|
43
|
+
return {
|
|
44
|
+
activeSessionId: sessionId,
|
|
45
|
+
sender,
|
|
46
|
+
bubbleList,
|
|
47
|
+
ask,
|
|
48
|
+
sessionOperate: sessionOperate.value,
|
|
49
|
+
sessionExposeMap
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
provide(AI_EXPOSE_PROVIDE.AI_EXPOSE, aiExpose);
|
|
53
|
+
provide(
|
|
54
|
+
AI_EXPOSE_PROVIDE.AI_ADD_SESSION_EXPOSE,
|
|
55
|
+
(id, expose) => {
|
|
56
|
+
sessionExposeMap.set(id, expose);
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
provide(
|
|
60
|
+
AI_EXPOSE_PROVIDE.AI_REMOVE_SESSION_EXPOSE,
|
|
61
|
+
(id) => {
|
|
62
|
+
sessionExposeMap.delete(id);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
provide(
|
|
66
|
+
AI_EXPOSE_PROVIDE.AI_SET_ACTIVE_SESSION_ID,
|
|
67
|
+
(id) => {
|
|
68
|
+
activeSessionId.value = id;
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
provide(
|
|
72
|
+
AI_EXPOSE_PROVIDE.AI_MOUNT_SESSION_OPERATE,
|
|
73
|
+
(expose) => {
|
|
74
|
+
sessionOperate.value = expose;
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
return {
|
|
78
|
+
aiExpose
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
const useAiExposeInject = () => {
|
|
82
|
+
const aiExpose = inject(AI_EXPOSE_PROVIDE.AI_EXPOSE);
|
|
83
|
+
return {
|
|
84
|
+
aiExpose
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
const useExposeCtrl = (getTargetExpose, setTargetExpose) => {
|
|
88
|
+
const targetExpose = computed(getTargetExpose);
|
|
89
|
+
const isActivated = ref(true);
|
|
90
|
+
onMounted(() => {
|
|
91
|
+
setTargetExpose(targetExpose.value);
|
|
92
|
+
watch(targetExpose, (expose) => {
|
|
93
|
+
if (!isActivated.value) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
setTargetExpose(expose);
|
|
97
|
+
});
|
|
98
|
+
onActivated(() => {
|
|
99
|
+
isActivated.value = true;
|
|
100
|
+
setTargetExpose(targetExpose.value);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
const removeExpose = () => {
|
|
104
|
+
setTargetExpose();
|
|
105
|
+
};
|
|
106
|
+
onDeactivated(() => {
|
|
107
|
+
isActivated.value = false;
|
|
108
|
+
removeExpose();
|
|
109
|
+
});
|
|
110
|
+
onBeforeUnmount(removeExpose);
|
|
111
|
+
};
|
|
112
|
+
const useAiSessionProvide = (sessionExpose) => {
|
|
113
|
+
provide(
|
|
114
|
+
AI_EXPOSE_PROVIDE.AI_SESSION_EXPOSE,
|
|
115
|
+
sessionExpose
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
const useAiSessionInject = () => {
|
|
119
|
+
const sessionExpose = inject(
|
|
120
|
+
AI_EXPOSE_PROVIDE.AI_SESSION_EXPOSE
|
|
121
|
+
);
|
|
122
|
+
return {
|
|
123
|
+
sessionExpose
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
export {
|
|
127
|
+
AI_EXPOSE_PROVIDE,
|
|
128
|
+
useAiExposeInject,
|
|
129
|
+
useAiExposeProvide,
|
|
130
|
+
useAiSessionInject,
|
|
131
|
+
useAiSessionProvide,
|
|
132
|
+
useExposeCtrl
|
|
133
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { computed, ref, nextTick } from "vue";
|
|
2
|
+
const safeUuid = () => {
|
|
3
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
4
|
+
return crypto.randomUUID();
|
|
5
|
+
}
|
|
6
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
7
|
+
const r = Math.random() * 16 | 0;
|
|
8
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
9
|
+
return v.toString(16);
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
const useFixBubbleListBug = (getBubbleListExpose) => {
|
|
13
|
+
const bubbleListExpose = computed(getBubbleListExpose);
|
|
14
|
+
const noScrollAnimation = ref(true);
|
|
15
|
+
const fixBugCls = `fix_${safeUuid()}`;
|
|
16
|
+
const hasFirstAsk = ref(false);
|
|
17
|
+
const historyRenderScrollShield = () => {
|
|
18
|
+
var _a;
|
|
19
|
+
noScrollAnimation.value = true;
|
|
20
|
+
const target = (_a = document.querySelector(`.${fixBugCls}`)) == null ? void 0 : _a.querySelector(".el-bubble-list");
|
|
21
|
+
nextTick(() => {
|
|
22
|
+
var _a2;
|
|
23
|
+
target == null ? void 0 : target.scrollTo({
|
|
24
|
+
top: target.scrollHeight,
|
|
25
|
+
behavior: "auto"
|
|
26
|
+
});
|
|
27
|
+
(_a2 = bubbleListExpose.value) == null ? void 0 : _a2.scrollToBottom();
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
var _a3;
|
|
30
|
+
(_a3 = bubbleListExpose.value) == null ? void 0 : _a3.scrollToBottom();
|
|
31
|
+
nextTick(() => {
|
|
32
|
+
noScrollAnimation.value = false;
|
|
33
|
+
});
|
|
34
|
+
}, 300);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const currentFirstAskScrollShield = async () => {
|
|
38
|
+
var _a;
|
|
39
|
+
if (hasFirstAsk.value) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
hasFirstAsk.value = true;
|
|
43
|
+
noScrollAnimation.value = true;
|
|
44
|
+
const target = (_a = document.querySelector(`.${fixBugCls}`)) == null ? void 0 : _a.querySelector(".el-bubble-list");
|
|
45
|
+
target == null ? void 0 : target.scrollTo({
|
|
46
|
+
top: target.scrollHeight,
|
|
47
|
+
behavior: "auto"
|
|
48
|
+
});
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
nextTick(() => {
|
|
51
|
+
noScrollAnimation.value = false;
|
|
52
|
+
resolve();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
fixBugCls,
|
|
58
|
+
noScrollAnimation: computed(() => noScrollAnimation.value),
|
|
59
|
+
historyRenderScrollShield,
|
|
60
|
+
currentFirstAskScrollShield
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
useFixBubbleListBug
|
|
65
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { toOpenAIMessages, aiIsDataPart, AiChatRoleEnum } from "../types/index.mjs";
|
|
2
|
+
const senderHistoryToParts = (content, parts) => {
|
|
3
|
+
const result = [];
|
|
4
|
+
if (content) {
|
|
5
|
+
result.push({ type: "text", text: content });
|
|
6
|
+
}
|
|
7
|
+
result.push(...(parts ?? []).filter(aiIsDataPart));
|
|
8
|
+
return result;
|
|
9
|
+
};
|
|
10
|
+
const bubbleToAiMessage = (bubble) => ({
|
|
11
|
+
id: bubble.uuid,
|
|
12
|
+
role: bubble.role === AiChatRoleEnum.USER ? "user" : "assistant",
|
|
13
|
+
parts: senderHistoryToParts(bubble.content, bubble.parts)
|
|
14
|
+
});
|
|
15
|
+
const isHistoryUsable = (bubble) => !bubble.errorMsg && senderHistoryToParts(bubble.content, bubble.parts).length > 0;
|
|
16
|
+
const senderBuildMessages = (bubbles, input, { historyLimit, serializer = toOpenAIMessages } = {}) => {
|
|
17
|
+
const current = {
|
|
18
|
+
id: "current",
|
|
19
|
+
role: "user",
|
|
20
|
+
parts: senderHistoryToParts(input.value, input.parts)
|
|
21
|
+
};
|
|
22
|
+
const history = historyLimit !== void 0 && historyLimit <= 0 ? [] : bubbles.filter(isHistoryUsable).map(bubbleToAiMessage);
|
|
23
|
+
const limited = historyLimit === void 0 || historyLimit >= history.length ? history : history.slice(-historyLimit);
|
|
24
|
+
return serializer([...limited, current]);
|
|
25
|
+
};
|
|
26
|
+
export {
|
|
27
|
+
senderBuildMessages
|
|
28
|
+
};
|