@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.
Files changed (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -2
  3. package/es/_virtual/_plugin-vue_export-helper.mjs +10 -0
  4. package/es/components/bubble/AiBubbleContent.vue.mjs +7 -0
  5. package/es/components/bubble/AiBubbleContent.vue2.mjs +209 -0
  6. package/es/components/bubble/AiBubbleFooter.vue.mjs +7 -0
  7. package/es/components/bubble/AiBubbleFooter.vue2.mjs +91 -0
  8. package/es/components/bubble/AiCodeHeaderControl.vue.mjs +7 -0
  9. package/es/components/bubble/AiCodeHeaderControl.vue2.mjs +97 -0
  10. package/es/components/bubble/AiContentSplit.vue.mjs +70 -0
  11. package/es/components/bubble/AiContentSplit.vue2.mjs +4 -0
  12. package/es/components/bubble/AiPartRenderer.vue.mjs +7 -0
  13. package/es/components/bubble/AiPartRenderer.vue2.mjs +184 -0
  14. package/es/components/bubble/AiThinkBox.vue.mjs +7 -0
  15. package/es/components/bubble/AiThinkBox.vue2.mjs +155 -0
  16. package/es/components/bubble/AiTypewriter.vue.mjs +81 -0
  17. package/es/components/bubble/AiTypewriter.vue2.mjs +4 -0
  18. package/es/components/config/AiConfigProvider.vue.mjs +31 -0
  19. package/es/components/config/AiConfigProvider.vue2.mjs +4 -0
  20. package/es/components/dialogue/AiDialogue.vue.mjs +7 -0
  21. package/es/components/dialogue/AiDialogue.vue2.mjs +242 -0
  22. package/es/components/dialogue/AiDialogueGroup.vue.mjs +7 -0
  23. package/es/components/dialogue/AiDialogueGroup.vue2.mjs +133 -0
  24. package/es/components/dialogue/AiDialogueWrap.vue.mjs +7 -0
  25. package/es/components/dialogue/AiDialogueWrap.vue2.mjs +73 -0
  26. package/es/components/dialogue/AiMessageList.vue.mjs +7 -0
  27. package/es/components/dialogue/AiMessageList.vue2.mjs +111 -0
  28. package/es/components/json-trigger/LlmJsonTrigger.vue.mjs +7 -0
  29. package/es/components/json-trigger/LlmJsonTrigger.vue2.mjs +130 -0
  30. package/es/components/operation/AiOperationBar.vue.mjs +7 -0
  31. package/es/components/operation/AiOperationBar.vue2.mjs +174 -0
  32. package/es/components/operation/AiOperationBtns.vue.mjs +7 -0
  33. package/es/components/operation/AiOperationBtns.vue2.mjs +140 -0
  34. package/es/components/sender/AiSender.vue.mjs +7 -0
  35. package/es/components/sender/AiSender.vue2.mjs +74 -0
  36. package/es/components/sender/AiSenderHeader.vue.mjs +7 -0
  37. package/es/components/sender/AiSenderHeader.vue2.mjs +78 -0
  38. package/es/hooks/CodeXSlot.mjs +20 -0
  39. package/es/hooks/SessionExpose.mjs +81 -0
  40. package/es/hooks/agent-tools.mjs +95 -0
  41. package/es/hooks/agent.mjs +143 -0
  42. package/es/hooks/bubble-list.mjs +64 -0
  43. package/es/hooks/expose.mjs +133 -0
  44. package/es/hooks/fix-bubble-list-bug.mjs +65 -0
  45. package/es/hooks/sender-history.mjs +28 -0
  46. package/es/hooks/sender.mjs +197 -0
  47. package/es/hooks/session-list.mjs +154 -0
  48. package/es/hooks/sse.mjs +401 -0
  49. package/es/hooks/storage.mjs +217 -0
  50. package/es/hooks/theme.mjs +253 -0
  51. package/es/hooks/use-density.mjs +100 -0
  52. package/es/hooks/use-empty-state-center.mjs +40 -0
  53. package/es/hooks/use-keyboard-inset.mjs +87 -0
  54. package/es/index.mjs +68 -0
  55. package/es/style.css +836 -0
  56. package/es/types/index.mjs +54 -0
  57. package/es/utils/index.mjs +34 -0
  58. package/es/utils/json-from-llm.mjs +26 -0
  59. package/es/utils/request-llm.mjs +60 -0
  60. package/package.json +83 -4
  61. package/types/bridge/config-hook.d.ts +21 -0
  62. package/types/bridge/index.d.ts +69 -0
  63. package/types/bridge/plugin.d.ts +39 -0
  64. package/types/bridge/state.d.ts +26 -0
  65. package/types/bridge/storage.d.ts +10 -0
  66. package/types/components/bubble/AiBubbleContent.vue.d.ts +57 -0
  67. package/types/components/bubble/AiBubbleFooter.vue.d.ts +6 -0
  68. package/types/components/bubble/AiCodeHeaderControl.vue.d.ts +7 -0
  69. package/types/components/bubble/AiContentSplit.vue.d.ts +36 -0
  70. package/types/components/bubble/AiPartRenderer.vue.d.ts +9 -0
  71. package/types/components/bubble/AiThinkBox.vue.d.ts +31 -0
  72. package/types/components/bubble/AiTypewriter.vue.d.ts +28 -0
  73. package/types/components/config/AiConfigProvider.vue.d.ts +28 -0
  74. package/types/components/dialogue/AiDialogue.vue.d.ts +546 -0
  75. package/types/components/dialogue/AiDialogueGroup.vue.d.ts +49 -0
  76. package/types/components/dialogue/AiDialogueWrap.vue.d.ts +38 -0
  77. package/types/components/dialogue/AiMessageList.vue.d.ts +65 -0
  78. package/types/components/json-trigger/LlmJsonTrigger.vue.d.ts +43 -0
  79. package/types/components/json-trigger/types.d.ts +12 -0
  80. package/types/components/operation/AiOperationBar.vue.d.ts +36 -0
  81. package/types/components/operation/AiOperationBtns.vue.d.ts +27 -0
  82. package/types/components/sender/AiSender.vue.d.ts +38 -0
  83. package/types/components/sender/AiSenderHeader.vue.d.ts +28 -0
  84. package/types/hooks/CodeXSlot.d.ts +7 -0
  85. package/types/hooks/SessionExpose.d.ts +20 -0
  86. package/types/hooks/agent-tools.d.ts +67 -0
  87. package/types/hooks/agent.d.ts +43 -0
  88. package/types/hooks/bubble-list.d.ts +22 -0
  89. package/types/hooks/expose.d.ts +111 -0
  90. package/types/hooks/fix-bubble-list-bug.d.ts +8 -0
  91. package/types/hooks/sender-history.d.ts +41 -0
  92. package/types/hooks/sender.d.ts +101 -0
  93. package/types/hooks/session-list.d.ts +36 -0
  94. package/types/hooks/sse.d.ts +239 -0
  95. package/types/hooks/storage.d.ts +70 -0
  96. package/types/hooks/theme.d.ts +37 -0
  97. package/types/hooks/use-density.d.ts +66 -0
  98. package/types/hooks/use-empty-state-center.d.ts +23 -0
  99. package/types/hooks/use-keyboard-inset.d.ts +37 -0
  100. package/types/index.d.ts +26 -0
  101. package/types/inject/key.d.ts +20 -0
  102. package/types/types/index.d.ts +612 -0
  103. package/types/utils/index.d.ts +29 -0
  104. package/types/utils/json-from-llm.d.ts +35 -0
  105. package/types/utils/request-llm.d.ts +44 -0
@@ -0,0 +1,197 @@
1
+ import { ref, computed, shallowRef } from "vue";
2
+ import { useAiAgent } from "./agent.mjs";
3
+ import { AiChatRoleEnum } from "../types/index.mjs";
4
+ import { ElMessage } from "element-plus";
5
+ import { useAiExposeInject } from "./expose.mjs";
6
+ import _debounce from "lodash/debounce.js";
7
+ import { useFixBubbleListBug } from "./fix-bubble-list-bug.mjs";
8
+ import { senderBuildMessages } from "./sender-history.mjs";
9
+ const uuidv4 = () => {
10
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
11
+ return crypto.randomUUID();
12
+ }
13
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
14
+ const r = Math.random() * 16 | 0;
15
+ const v = c === "x" ? r : r & 3 | 8;
16
+ return v.toString(16);
17
+ });
18
+ };
19
+ const useSenderScoped = ({
20
+ addBubble,
21
+ getSseUrl,
22
+ getBubbleList,
23
+ getHeaders,
24
+ getBody,
25
+ getHistoryLimit,
26
+ getTools,
27
+ getOnToolCall,
28
+ getMaxSteps,
29
+ getMessagesSerializer,
30
+ getShowThink,
31
+ converter,
32
+ changeMessage
33
+ }) => {
34
+ const senderValue = ref("");
35
+ const typingMsgCtrl = ref();
36
+ const placeholder = "请输入内容";
37
+ const autoSizeOptions = computed(() => {
38
+ return {
39
+ minRows: 2,
40
+ maxRows: 2
41
+ };
42
+ });
43
+ const aiAgentHook = shallowRef();
44
+ const isResponding = computed(() => {
45
+ var _a;
46
+ return !!((_a = aiAgentHook.value) == null ? void 0 : _a.isResponding.value);
47
+ });
48
+ const submitBtnDisabled = computed(() => {
49
+ var _a;
50
+ return !((_a = senderValue.value) == null ? void 0 : _a.trim());
51
+ });
52
+ const message = ElMessage;
53
+ const { aiExpose } = useAiExposeInject();
54
+ const {
55
+ fixBugCls,
56
+ noScrollAnimation,
57
+ historyRenderScrollShield,
58
+ currentFirstAskScrollShield
59
+ } = useFixBubbleListBug(() => {
60
+ var _a;
61
+ return (_a = aiExpose == null ? void 0 : aiExpose.value) == null ? void 0 : _a.bubbleList;
62
+ });
63
+ const setTypingMsgCtrl = (item) => {
64
+ if (item) {
65
+ typingMsgCtrl.value = {
66
+ uuid: item.uuid
67
+ };
68
+ } else {
69
+ typingMsgCtrl.value = void 0;
70
+ }
71
+ };
72
+ const handleCancel = () => {
73
+ var _a;
74
+ setTypingMsgCtrl();
75
+ (_a = aiAgentHook.value) == null ? void 0 : _a.cancel();
76
+ };
77
+ const changeMessageDebounce = _debounce(changeMessage, 300, {
78
+ maxWait: 300
79
+ });
80
+ const handleSubmit = (valueInit, options) => {
81
+ var _a, _b, _c;
82
+ const value = (valueInit == null ? void 0 : valueInit.trim()) ?? "";
83
+ const parts = ((_a = options == null ? void 0 : options.parts) == null ? void 0 : _a.length) ? options.parts : void 0;
84
+ if (!value && !parts) {
85
+ const errMsg = `请输入内容`;
86
+ message.error(errMsg);
87
+ throw new Error(errMsg);
88
+ }
89
+ const sseUrl = getSseUrl == null ? void 0 : getSseUrl();
90
+ if (!sseUrl) {
91
+ const errMsg = `未配置 SSE 端点(sseUrl)`;
92
+ message.error(errMsg);
93
+ throw new Error(errMsg);
94
+ }
95
+ changeMessageDebounce.flush();
96
+ currentFirstAskScrollShield();
97
+ (_c = (_b = aiExpose == null ? void 0 : aiExpose.value) == null ? void 0 : _b.bubbleList) == null ? void 0 : _c.scrollToBottom();
98
+ handleCancel();
99
+ const sseMessages = senderBuildMessages(
100
+ (getBubbleList == null ? void 0 : getBubbleList()) ?? [],
101
+ { value, parts },
102
+ {
103
+ historyLimit: getHistoryLimit == null ? void 0 : getHistoryLimit(),
104
+ serializer: getMessagesSerializer == null ? void 0 : getMessagesSerializer()
105
+ }
106
+ );
107
+ const askUuid = uuidv4();
108
+ const replyUuid = uuidv4();
109
+ const talkGoal = {
110
+ talkGoalLabel: "",
111
+ talkGoalValue: ""
112
+ };
113
+ addBubble({
114
+ replyUuid,
115
+ ...talkGoal,
116
+ // ...userConfig.value,
117
+ role: AiChatRoleEnum.USER,
118
+ uuid: askUuid,
119
+ placement: "end",
120
+ content: value,
121
+ ...parts ? { parts: [...parts] } : {},
122
+ isFinished: true
123
+ });
124
+ senderValue.value = "";
125
+ const aiMessage = addBubble({
126
+ askUuid,
127
+ askInput: value,
128
+ ...talkGoal,
129
+ role: AiChatRoleEnum.AI,
130
+ uuid: replyUuid,
131
+ placement: "start",
132
+ loading: true,
133
+ // 0.3.0 起由 prop 驱动、默认关(此前硬编码 true、无开关):它与结构化
134
+ // `reasoning` part 是同一件事的两条路,默认开会双轨。
135
+ showThink: (getShowThink == null ? void 0 : getShowThink()) ?? false,
136
+ typing: true,
137
+ content: ""
138
+ });
139
+ setTypingMsgCtrl(aiMessage);
140
+ aiAgentHook.value = useAiAgent({
141
+ message: aiMessage,
142
+ changeMessage: (patch, isChangeContent = false) => {
143
+ Object.assign(aiMessage, patch);
144
+ const currentUuid = aiMessage.uuid;
145
+ if (isChangeContent) {
146
+ changeMessageDebounce({
147
+ ...patch,
148
+ updatedAt: Date.now(),
149
+ uuid: currentUuid
150
+ });
151
+ } else {
152
+ aiMessage.updatedAt = Date.now();
153
+ changeMessage({
154
+ ...patch,
155
+ uuid: currentUuid
156
+ });
157
+ }
158
+ },
159
+ input: value,
160
+ sseUrl,
161
+ headers: getHeaders == null ? void 0 : getHeaders(),
162
+ messages: sseMessages,
163
+ body: getBody == null ? void 0 : getBody(),
164
+ tools: getTools == null ? void 0 : getTools(),
165
+ onToolCall: getOnToolCall == null ? void 0 : getOnToolCall(),
166
+ maxSteps: getMaxSteps == null ? void 0 : getMaxSteps(),
167
+ converter,
168
+ finish: (symbol) => {
169
+ changeMessageDebounce.flush();
170
+ if (aiAgentHook.value) {
171
+ if (aiAgentHook.value.symbol === symbol) {
172
+ aiAgentHook.value = void 0;
173
+ }
174
+ }
175
+ }
176
+ });
177
+ aiAgentHook.value.start();
178
+ };
179
+ return {
180
+ senderValue,
181
+ placeholder,
182
+ autoSizeOptions,
183
+ submitBtnDisabled,
184
+ isResponding,
185
+ message,
186
+ typingMsgCtrl: computed(() => typingMsgCtrl.value),
187
+ fixBugCls,
188
+ noScrollAnimation,
189
+ historyRenderScrollShield,
190
+ setTypingMsgCtrl,
191
+ handleSubmit,
192
+ handleCancel
193
+ };
194
+ };
195
+ export {
196
+ useSenderScoped
197
+ };
@@ -0,0 +1,154 @@
1
+ import { ref, computed, watch, nextTick } from "vue";
2
+ import { ElMessage } from "element-plus";
3
+ import { useSessionOperateMount } from "./SessionExpose.mjs";
4
+ import { useSessionListStorage } from "./storage.mjs";
5
+ const genUuid = () => {
6
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
7
+ return crypto.randomUUID();
8
+ }
9
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
10
+ const r = Math.random() * 16 | 0;
11
+ const v = c === "x" ? r : r & 3 | 8;
12
+ return v.toString(16);
13
+ });
14
+ };
15
+ const createNewSessionInfo = () => {
16
+ const newSessionId = genUuid();
17
+ const newSession = {
18
+ id: newSessionId,
19
+ createdAt: 0,
20
+ updatedAt: 0,
21
+ lastMsgCreatedAt: 0,
22
+ msgCount: 0,
23
+ label: "新对话"
24
+ };
25
+ return newSession;
26
+ };
27
+ const useSessionListScoped = (db) => {
28
+ const {
29
+ list: historySessionList,
30
+ isLoadMoreLoading,
31
+ isLoadMoreFinished,
32
+ loadMore,
33
+ addSession,
34
+ updateSession
35
+ } = useSessionListStorage(db);
36
+ const memorySessionList = ref([]);
37
+ const activeSessionId = ref();
38
+ const virtualSessionInfo = ref(createNewSessionInfo());
39
+ const renderIds = ref([]);
40
+ const sessionList = computed(
41
+ () => memorySessionList.value.concat(historySessionList.value).sort((a, b) => b.lastMsgCreatedAt - a.lastMsgCreatedAt)
42
+ );
43
+ const message = ElMessage;
44
+ const sessionIdList = computed(
45
+ () => sessionList.value.map((item) => item.id)
46
+ );
47
+ const fullSessionList = computed(() => [
48
+ virtualSessionInfo.value,
49
+ ...sessionList.value
50
+ ]);
51
+ const renderSessionList = computed(
52
+ () => fullSessionList.value.filter((item) => renderIds.value.includes(item.id))
53
+ );
54
+ const fullSessionIdList = computed(
55
+ () => fullSessionList.value.map((item) => item.id)
56
+ );
57
+ const unshiftSession = (session) => {
58
+ if (!sessionIdList.value.includes(session.id)) {
59
+ session.createdAt = Date.now();
60
+ session.updatedAt = session.createdAt;
61
+ memorySessionList.value.unshift(session);
62
+ addSession(session);
63
+ if (session.id === virtualSessionInfo.value.id) {
64
+ virtualSessionInfo.value = createNewSessionInfo();
65
+ }
66
+ }
67
+ };
68
+ const changeSession = (session) => {
69
+ const target = sessionList.value.find((item) => item.id === session.id);
70
+ if (target) {
71
+ Object.assign(target, session);
72
+ target.updatedAt = Date.now();
73
+ updateSession(target);
74
+ }
75
+ };
76
+ const checkIdInList = (id) => {
77
+ return fullSessionIdList.value.includes(id);
78
+ };
79
+ watch(
80
+ [activeSessionId, fullSessionList],
81
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars -- 1:1 迁入源既有,REQ-2 禁改(见 review-notes)
82
+ ([id, list]) => {
83
+ if (!id || !checkIdInList(id)) {
84
+ activeSessionId.value = virtualSessionInfo.value.id;
85
+ }
86
+ },
87
+ { immediate: true }
88
+ );
89
+ const MAX_RENDER_COUNT = 5;
90
+ watch(
91
+ activeSessionId,
92
+ (n) => {
93
+ if (n) {
94
+ if (!renderIds.value.includes(n)) {
95
+ renderIds.value.push(n);
96
+ const l = renderIds.value.length;
97
+ if (l > MAX_RENDER_COUNT) {
98
+ renderIds.value.splice(0, l - MAX_RENDER_COUNT);
99
+ }
100
+ }
101
+ }
102
+ },
103
+ {
104
+ immediate: true
105
+ }
106
+ );
107
+ useSessionOperateMount(activeSessionId, {
108
+ create: () => {
109
+ activeSessionId.value = void 0;
110
+ return new Promise((resolve, reject) => {
111
+ nextTick(() => {
112
+ if (activeSessionId.value) {
113
+ const res = {
114
+ id: activeSessionId.value
115
+ };
116
+ return resolve(res);
117
+ } else {
118
+ const errMsg = `创建会话失败`;
119
+ message.error(errMsg);
120
+ reject(new Error(errMsg));
121
+ }
122
+ });
123
+ });
124
+ },
125
+ switch: (id) => {
126
+ return new Promise((resolve, reject) => {
127
+ if (checkIdInList(id)) {
128
+ activeSessionId.value = id;
129
+ return resolve();
130
+ } else {
131
+ const errMsg = `会话不存在,无法切换`;
132
+ message.error(errMsg);
133
+ return reject(new Error(errMsg));
134
+ }
135
+ });
136
+ }
137
+ });
138
+ return {
139
+ isLoadMoreLoading,
140
+ isLoadMoreFinished,
141
+ loadMore,
142
+ activeSessionId,
143
+ sessionList,
144
+ renderSessionList,
145
+ fullSessionList,
146
+ unshiftSession,
147
+ changeSession,
148
+ virtualSessionInfo
149
+ };
150
+ };
151
+ export {
152
+ createNewSessionInfo,
153
+ useSessionListScoped
154
+ };