@bolloon/bolloon-agent 0.2.9 → 0.2.11
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/README.md +467 -467
- package/bin/bolloon-cli.cjs +183 -183
- package/bin/bolloon.cjs +0 -0
- package/bin/bolloon.js +157 -0
- package/dist/agents/agent-manifest-protocol.js +0 -52
- package/dist/agents/constraint-layer.js +19 -19
- package/dist/agents/pi-sdk.js +287 -1213
- package/dist/agents/shell-guard.js +2 -2
- package/dist/agents/workflow-pivot-loop.js +2 -81
- package/dist/bollharness/src/scripts/deploy-guard.js +8 -8
- package/dist/bollharness-integration/context-router-judgment.js +4 -4
- package/dist/bollharness-integration/context-router.js +292 -292
- package/dist/bollharness-integration/gate-state-machine.js +13 -26
- package/dist/bollharness-integration/integration.js +0 -71
- package/dist/bollharness-integration/llm-judgment-engine.js +134 -134
- package/dist/bollharness-integration/skill-adapter.js +127 -52
- package/dist/bootstrap/context-collector.js +3 -6
- package/dist/bootstrap/lifecycle-hooks.js +1 -15
- package/dist/cli-entry.js +35 -39
- package/dist/constraint-runtime/src/_archive_helper.js +9 -0
- package/dist/constraint-runtime/src/agent/coordinator.js +48 -0
- package/dist/constraint-runtime/src/agent/index.js +1 -0
- package/dist/constraint-runtime/src/assistant/index.js +12 -0
- package/dist/constraint-runtime/src/bootstrap/index.js +12 -0
- package/dist/constraint-runtime/src/bootstrap_graph.js +13 -0
- package/dist/constraint-runtime/src/bridge/index.js +12 -0
- package/dist/constraint-runtime/src/buddy/index.js +12 -0
- package/dist/constraint-runtime/src/cli/index.js +12 -0
- package/dist/constraint-runtime/src/command_graph.js +10 -0
- package/dist/constraint-runtime/src/commands.js +60 -0
- package/dist/constraint-runtime/src/components/index.js +12 -0
- package/dist/constraint-runtime/src/constants/index.js +12 -0
- package/dist/constraint-runtime/src/constraint/budget.js +22 -0
- package/dist/constraint-runtime/src/constraint/index.js +2 -0
- package/dist/constraint-runtime/src/constraint/permission.js +20 -0
- package/dist/constraint-runtime/src/context.js +30 -0
- package/dist/constraint-runtime/src/coordinator/index.js +12 -0
- package/dist/constraint-runtime/src/cost_hook.js +4 -0
- package/dist/constraint-runtime/src/cost_tracker.js +8 -0
- package/dist/constraint-runtime/src/deferred_init.js +10 -0
- package/dist/constraint-runtime/src/direct_modes.js +6 -0
- package/dist/constraint-runtime/src/dynamic-tool-loader.js +85 -0
- package/dist/constraint-runtime/src/entrypoints/index.js +12 -0
- package/dist/constraint-runtime/src/execution_registry.js +44 -0
- package/dist/constraint-runtime/src/history.js +9 -0
- package/dist/constraint-runtime/src/hooks/index.js +12 -0
- package/dist/constraint-runtime/src/index.js +26 -0
- package/dist/constraint-runtime/src/ink.js +4 -0
- package/dist/constraint-runtime/src/keybindings/index.js +12 -0
- package/dist/constraint-runtime/src/memdir/index.js +12 -0
- package/dist/constraint-runtime/src/migrations/index.js +12 -0
- package/dist/constraint-runtime/src/models.js +1 -0
- package/dist/constraint-runtime/src/moreright/index.js +12 -0
- package/dist/constraint-runtime/src/native_ts/index.js +12 -0
- package/dist/constraint-runtime/src/output_styles/index.js +12 -0
- package/dist/constraint-runtime/src/parity_audit.js +12 -0
- package/dist/constraint-runtime/src/plugins/index.js +12 -0
- package/dist/constraint-runtime/src/port_manifest.js +11 -0
- package/dist/constraint-runtime/src/prefetch.js +9 -0
- package/dist/constraint-runtime/src/query.js +1 -0
- package/dist/constraint-runtime/src/remote/index.js +12 -0
- package/dist/constraint-runtime/src/remote_runtime.js +9 -0
- package/dist/constraint-runtime/src/runtime/index.js +1 -0
- package/dist/constraint-runtime/src/runtime/session.js +35 -0
- package/dist/constraint-runtime/src/schemas/index.js +12 -0
- package/dist/constraint-runtime/src/screens/index.js +12 -0
- package/dist/constraint-runtime/src/server/index.js +12 -0
- package/dist/constraint-runtime/src/services/index.js +12 -0
- package/dist/constraint-runtime/src/session_store.js +22 -0
- package/dist/constraint-runtime/src/setup.js +30 -0
- package/dist/constraint-runtime/src/skills/index.js +1 -0
- package/dist/constraint-runtime/src/skills/skill-registry.js +28 -0
- package/dist/constraint-runtime/src/state/index.js +12 -0
- package/dist/constraint-runtime/src/system_init.js +20 -0
- package/dist/constraint-runtime/src/thinking/engine.js +42 -0
- package/dist/constraint-runtime/src/thinking/index.js +1 -0
- package/dist/constraint-runtime/src/tool_pool.js +8 -0
- package/dist/constraint-runtime/src/tools/OpenCLI/execAdapter.js +7 -0
- package/dist/constraint-runtime/src/tools/OpenCLI/listAdapters.js +7 -0
- package/dist/constraint-runtime/src/tools/OpenCLI/runCommand.js +7 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/confirmTransaction.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/createTransaction.js +8 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/executeTransaction.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/getBalance.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/getPendingTransactions.js +6 -0
- package/dist/constraint-runtime/src/tools/SafeSDK/proposeTransaction.js +6 -0
- package/dist/constraint-runtime/src/tools.js +80 -0
- package/dist/constraint-runtime/src/transcript.js +19 -0
- package/dist/constraint-runtime/src/types/index.js +12 -0
- package/dist/constraint-runtime/src/upstream_proxy/index.js +12 -0
- package/dist/constraint-runtime/src/utils/index.js +12 -0
- package/dist/constraint-runtime/src/vim/index.js +12 -0
- package/dist/constraint-runtime/src/voice/index.js +12 -0
- package/dist/constraint-runtime/tests/agent.test.js +16 -0
- package/dist/constraint-runtime/tests/constraint.test.js +41 -0
- package/dist/constraint-runtime/tests/skill.test.js +19 -0
- package/dist/constraint-runtime/tests/thinking.test.js +22 -0
- package/dist/context-compaction/auto-compact.js +7 -7
- package/dist/electron-preload.js +1 -18
- package/dist/electron-preload.js.map +1 -1
- package/dist/electron.js +168 -3
- package/dist/electron.js.map +1 -1
- package/dist/index.js +116 -136
- package/dist/llm/config-store.js +8 -24
- package/dist/llm/llm-judgment-client.js +102 -102
- package/dist/llm/pi-ai.js +85 -126
- package/dist/llm/system-prompt/layers/channel/local.md +14 -14
- package/dist/llm/system-prompt/layers/channel/p2p-agent.md +18 -18
- package/dist/llm/system-prompt/layers/channel/p2p-visitor.md +19 -19
- package/dist/llm/system-prompt/layers/core/artifacts_storage.md +89 -89
- package/dist/llm/system-prompt/layers/core/evenhandedness.md +21 -21
- package/dist/llm/system-prompt/layers/core/hibs_reminders.md +15 -15
- package/dist/llm/system-prompt/layers/core/identity.md +37 -37
- package/dist/llm/system-prompt/layers/core/knowledge.md +17 -17
- package/dist/llm/system-prompt/layers/core/memory_system.md +12 -12
- package/dist/llm/system-prompt/layers/core/network_filesystem.md +28 -28
- package/dist/llm/system-prompt/layers/core/refusal.md +37 -37
- package/dist/llm/system-prompt/layers/core/tone.md +31 -31
- package/dist/llm/system-prompt/layers/core/tools.thin.md +13 -13
- package/dist/llm/system-prompt/layers/core/wellbeing.md +41 -41
- package/dist/llm/system-prompt/layers/role/architect.md +20 -20
- package/dist/llm/system-prompt/layers/role/expert.md +19 -19
- package/dist/llm/system-prompt/layers/role/implementer.md +15 -15
- package/dist/llm/system-prompt/layers/role/security.md +15 -15
- package/dist/llm/system-prompt/layers/tool/artifacts.md +72 -72
- package/dist/llm/system-prompt/layers/tool/bash.md +25 -25
- package/dist/llm/system-prompt/layers/tool/hibs_api.md +171 -171
- package/dist/llm/system-prompt/layers/tool/image_search.md +70 -70
- package/dist/llm/system-prompt/layers/tool/manifest.md +89 -89
- package/dist/llm/system-prompt/layers/tool/mcp_apps.md +53 -53
- package/dist/llm/system-prompt/layers/tool/web_search.md +83 -83
- package/dist/llm/tool-manifest/ask_user_input.js +1 -1
- package/dist/llm/tool-manifest/bash.js +3 -3
- package/dist/llm/tool-manifest/create_file.js +4 -4
- package/dist/llm/tool-manifest/fetch_sports_data.js +3 -3
- package/dist/llm/tool-manifest/image_search.js +2 -2
- package/dist/llm/tool-manifest/mcp.js +2 -2
- package/dist/llm/tool-manifest/message_compose.js +3 -3
- package/dist/llm/tool-manifest/places.js +2 -2
- package/dist/llm/tool-manifest/present_files.js +1 -1
- package/dist/llm/tool-manifest/recipe.js +2 -2
- package/dist/llm/tool-manifest/str_replace.js +5 -5
- package/dist/llm/tool-manifest/view.js +3 -3
- package/dist/llm/tool-manifest/weather.js +4 -4
- package/dist/llm/tool-manifest/web.js +4 -4
- package/dist/pi-ecosystem-colony/index.js +365 -0
- package/dist/pi-ecosystem-judgment/causal-judge.js +51 -51
- package/dist/pi-ecosystem-judgment/distill-prompt.js +42 -42
- package/dist/pi-ecosystem-judgment/distillation.js +14 -14
- package/dist/pi-ecosystem-judgment/monitor-gate.js +28 -28
- package/dist/pi-ecosystem-judgment/value-injection.js +102 -102
- package/dist/security/context-router-tool.js +15 -15
- package/dist/security/tool-gate.js +0 -11
- package/dist/social/ant-colony/AdaptiveHeartbeat.js +101 -0
- package/dist/social/ant-colony/PheromoneEngine.js +227 -0
- package/dist/social/ant-colony/index.js +6 -0
- package/dist/social/ant-colony/types.js +24 -0
- package/dist/test/ai-judgment-test.js +80 -0
- package/dist/test/bollharness-integration.test.js +318 -0
- package/dist/test/channel-agent-multi-dialogue.js +205 -0
- package/dist/test/channel-heartbeat-agent-test.js +201 -0
- package/dist/test/constraint-layer.test.js +164 -0
- package/dist/test/diap-identity-test.js +172 -0
- package/dist/test/diap-quick-test.js +62 -0
- package/dist/test/global-shared-context.test.js +315 -0
- package/dist/test/harness-judgment-injection.test.js +246 -0
- package/dist/test/harness-workflow-integrator-test.js +228 -0
- package/dist/test/human-value-store.test.js +243 -0
- package/dist/test/hybrid-integration-test.js +118 -0
- package/dist/test/hybrid-messenger-verify.js +55 -0
- package/dist/test/iroh-bistream-debug.js +38 -0
- package/dist/test/iroh-communication.test.js +66 -0
- package/dist/test/iroh-debug-test.js +57 -0
- package/dist/test/iroh-diap-test.js +71 -0
- package/dist/test/iroh-direct-connect.js +55 -0
- package/dist/test/iroh-e2e-fixed.js +89 -0
- package/dist/test/iroh-e2e-same-process.js +63 -0
- package/dist/test/iroh-e2e.js +66 -0
- package/dist/test/iroh-final-e2e.js +72 -0
- package/dist/test/iroh-relay-test.js +37 -0
- package/dist/test/iroh-simple-test.js +41 -0
- package/dist/test/iroh-transport-verify.js +54 -0
- package/dist/test/iroh-transport.test.js +37 -0
- package/dist/test/iroh-two-nodes.js +70 -0
- package/dist/test/iroh-verify.js +44 -0
- package/dist/test/judgment-decision.test.js +219 -0
- package/dist/test/llm-judgment-integration.test.js +220 -0
- package/dist/test/p2p-agent-complex-dialogue.js +385 -0
- package/dist/test/p2p-agent-dialogue.js +341 -0
- package/dist/test/p2p-agent-full-bidirectional.js +510 -0
- package/dist/test/p2p-agent-harness-flow.js +437 -0
- package/dist/test/p2p-agent-harness-single.js +143 -0
- package/dist/test/p2p-ai-dialogue-test.js +318 -0
- package/dist/test/p2p-cid-connect-test.js +195 -0
- package/dist/test/p2p-connect-receiver.js +69 -0
- package/dist/test/p2p-doc-transfer.js +110 -0
- package/dist/test/p2p-identity-page-test.js +77 -0
- package/dist/test/p2p-iroh-test.js +171 -0
- package/dist/test/p2p-minimal-test.js +241 -0
- package/dist/test/p2p-node-1.js +148 -0
- package/dist/test/p2p-node-2.js +148 -0
- package/dist/test/p2p-server.js +281 -0
- package/dist/test/p2p-two-nodes-test.js +438 -0
- package/dist/test/pi-sdk.test.js +44 -0
- package/dist/test/set-persona.js +40 -0
- package/dist/test/simple.test.js +9 -0
- package/dist/test/storage-integration.test.js +150 -0
- package/dist/test/subagent-manager.test.js +276 -0
- package/dist/test/test-gate-flow.test.js +81 -0
- package/dist/test/workflow-engine.test.js +87 -0
- package/dist/test/workflow-pivot-loop.test.js +246 -0
- package/dist/web/api-config.html +520 -520
- package/dist/web/client.js +2917 -3791
- package/dist/web/components/p2p/index.js +234 -276
- package/dist/web/components/wallet-viem.mjs +118 -118
- package/dist/web/index.html +374 -373
- package/dist/web/manifest.json +20 -20
- package/dist/web/server.js +23 -775
- package/dist/web/style.css +4737 -4737
- package/dist/web/ui/message-renderer.js +373 -451
- package/dist/web/ui/step-timeline.js +255 -351
- package/package.json +160 -160
- package/scripts/build-cli.js +215 -215
- package/scripts/build-web.ts +125 -125
- package/scripts/postinstall.js +152 -152
- package/bin/bolloon-daemon.sh +0 -207
- package/bin/ipfs +0 -0
- package/dist/agents/chat-segmenter.js +0 -298
- package/dist/agents/judgment-protocol.js +0 -479
- package/dist/agents/parse-tool-call.js +0 -304
- package/dist/agents/peer-manifest-loader.js +0 -210
- package/dist/agents/react-loop.js +0 -120
- package/dist/agents/session-store.js +0 -171
- package/dist/agents/tool-registry.js +0 -136
- package/dist/bootstrap/chat-archiver.js +0 -276
- package/dist/bootstrap/memory-compressor.js +0 -170
- package/dist/bootstrap/persona-loader.js +0 -94
- package/dist/electron/config.js +0 -16
- package/dist/electron/config.js.map +0 -1
- package/dist/electron/dialogs.js +0 -71
- package/dist/electron/dialogs.js.map +0 -1
- package/dist/electron/first-run.js +0 -129
- package/dist/electron/first-run.js.map +0 -1
- package/dist/electron/ipc.js +0 -16
- package/dist/electron/ipc.js.map +0 -1
- package/dist/electron/logger.js +0 -77
- package/dist/electron/logger.js.map +0 -1
- package/dist/electron/main.js +0 -60
- package/dist/electron/main.js.map +0 -1
- package/dist/electron/menu.js +0 -140
- package/dist/electron/menu.js.map +0 -1
- package/dist/electron/paths.js +0 -33
- package/dist/electron/paths.js.map +0 -1
- package/dist/electron/server.js +0 -74
- package/dist/electron/server.js.map +0 -1
- package/dist/electron/tray.js +0 -73
- package/dist/electron/tray.js.map +0 -1
- package/dist/electron/window.js +0 -69
- package/dist/electron/window.js.map +0 -1
- package/dist/network/local-inbox-bus.js +0 -73
- package/dist/network/p2p-outbox.js +0 -161
- package/dist/network/peer-fs.js +0 -420
- package/dist/network/peer-resource-bridge.js +0 -216
- package/dist/web/components/p2p/P2PModal.js +0 -188
- package/dist/web/components/p2p/p2p-modal.js +0 -664
- package/dist/web/components/p2p/p2p-tools.js +0 -248
- package/dist/web/input-validator.js +0 -103
- package/dist/web/util/safe-name.js +0 -32
|
@@ -1,483 +1,405 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* - 折叠 think / environment_details 块
|
|
9
|
-
* - 复制 / 重新回答 / 蒸馏为判断 按钮
|
|
10
|
-
* - 挂载 step-timeline (2026-06-15) 步骤状态条到每条 AI 消息内
|
|
11
|
-
*
|
|
12
|
-
* 状态 (本模块私有):
|
|
13
|
-
* - streamingMessageEl / streamingTextNode / streamingText
|
|
14
|
-
* - lastUserCommand / lastAiContent (去重)
|
|
15
|
-
*
|
|
16
|
-
* 依赖 (import):
|
|
17
|
-
* - 浏览器 API (document, marked, fetch, navigator)
|
|
18
|
-
* - ./step-timeline (4 状态步骤条模块)
|
|
19
|
-
*
|
|
20
|
-
* 不依赖 (零业务 import, 防循环):
|
|
21
|
-
* - 不 import client.js
|
|
22
|
-
* - 不 import 任何业务模块
|
|
23
|
-
*
|
|
24
|
-
* 输入 (从 client.js 调用):
|
|
25
|
-
* - addMessage(content, type, save, container, usedIds, ctx)
|
|
26
|
-
* - handleStreamTokenEvent({ streamType, content }, ctx)
|
|
27
|
-
* - finalizeTimelineAsMessage(ctx)
|
|
28
|
-
* - handleStepEvent({ type: 'step_start'|'step_done'|'step_error', ... }, ctx)
|
|
29
|
-
* - escapeHtml(s)
|
|
30
|
-
* - getMessagesContainerForCurrent(currentChannelId, messagesContainers, messagesEl)
|
|
31
|
-
*
|
|
32
|
-
* 输出 (DOM):
|
|
33
|
-
* - .message / .bubble / .think-container / .env-container
|
|
34
|
-
* - .message-streaming (流式期间, finalize 时移除)
|
|
35
|
-
* - .message-actions (复制/重新回答/蒸馏按钮)
|
|
36
|
-
* - .used-judgments-link (P0.5 反向引用)
|
|
37
|
-
* - .step-timeline (气泡内步骤状态条, 由 ./step-timeline 渲染)
|
|
38
|
-
*/
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
// 类型定义
|
|
41
|
-
// ---------------------------------------------------------------------------
|
|
42
|
-
import { mountStepTimeline, pushStepToTimeline, migrateStepTimeline, getStepTimeline, } from './step-timeline.js';
|
|
43
|
-
// 2026-07-01 (v0.2.6): 共享后端切 LLM 输出. 消除 <invoke>/<function_calls>/<tool_call>
|
|
44
|
-
// 等各种 LLM 格式在前端气泡里出现的 bug.
|
|
45
|
-
import { segmentChatReply } from '../../agents/chat-segmenter.js';
|
|
46
|
-
// ---------------------------------------------------------------------------
|
|
47
|
-
// 模块私有状态
|
|
48
|
-
// ---------------------------------------------------------------------------
|
|
1
|
+
import {
|
|
2
|
+
mountStepTimeline,
|
|
3
|
+
pushStepToTimeline,
|
|
4
|
+
migrateStepTimeline,
|
|
5
|
+
getStepTimeline
|
|
6
|
+
} from "./step-timeline.js";
|
|
7
|
+
import { segmentChatReply } from "../../agents/chat-segmenter.js";
|
|
49
8
|
let streamingMessageEl = null;
|
|
50
9
|
let streamingTextNode = null;
|
|
51
|
-
let streamingText =
|
|
52
|
-
let lastUserCommand =
|
|
53
|
-
let lastAiContent =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
10
|
+
let streamingText = "";
|
|
11
|
+
let lastUserCommand = "";
|
|
12
|
+
let lastAiContent = "";
|
|
13
|
+
function hasStreamingText() {
|
|
14
|
+
return streamingText.length > 0;
|
|
15
|
+
}
|
|
16
|
+
function replaceStreamingText(fullContent) {
|
|
17
|
+
if (!streamingTextNode || !streamingMessageEl) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
streamingTextNode.nodeValue = String(fullContent || "");
|
|
21
|
+
streamingText = String(fullContent || "");
|
|
22
|
+
}
|
|
23
|
+
function injectRecoveredText(partialText, ctx = { messagesEl: null, messagesContainers: /* @__PURE__ */ new Map(), currentChannelId: null }) {
|
|
24
|
+
if (streamingMessageEl && streamingTextNode) {
|
|
25
|
+
streamingTextNode.nodeValue = String(partialText || "");
|
|
26
|
+
streamingText = String(partialText || "");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
handleStreamTokenEvent(
|
|
30
|
+
{
|
|
31
|
+
type: "token",
|
|
32
|
+
streamType: "token",
|
|
33
|
+
content: String(partialText || ""),
|
|
34
|
+
delta: String(partialText || "")
|
|
35
|
+
},
|
|
36
|
+
ctx
|
|
37
|
+
);
|
|
62
38
|
}
|
|
63
|
-
// 滚动限频 (60ms 16fps, 减 reflow)
|
|
64
39
|
let scrollToBottomTimer = null;
|
|
65
40
|
function scheduleScrollToBottom(container) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
scrollToBottomTimer =
|
|
71
|
-
|
|
72
|
-
scrollToBottomTimer = null;
|
|
73
|
-
}, 60);
|
|
41
|
+
if (!container) return;
|
|
42
|
+
if (scrollToBottomTimer) return;
|
|
43
|
+
scrollToBottomTimer = setTimeout(() => {
|
|
44
|
+
container.scrollTop = container.scrollHeight;
|
|
45
|
+
scrollToBottomTimer = null;
|
|
46
|
+
}, 60);
|
|
74
47
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return messagesContainers.get(currentChannelId) || null;
|
|
81
|
-
}
|
|
82
|
-
return messagesEl;
|
|
48
|
+
function getMessagesContainerForCurrent(currentChannelId, messagesContainers, messagesEl) {
|
|
49
|
+
if (currentChannelId && messagesContainers.get(currentChannelId)) {
|
|
50
|
+
return messagesContainers.get(currentChannelId) || null;
|
|
51
|
+
}
|
|
52
|
+
return messagesEl;
|
|
83
53
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
54
|
+
function escapeHtml(s) {
|
|
55
|
+
return String(s ?? "").replace(/[&<>"']/g, (c) => ({
|
|
56
|
+
"&": "&",
|
|
57
|
+
"<": "<",
|
|
58
|
+
">": ">",
|
|
59
|
+
'"': """,
|
|
60
|
+
"'": "'"
|
|
61
|
+
})[c]);
|
|
91
62
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const toRemove = msgContainer.children.length - 200;
|
|
103
|
-
for (let i = 0; i < toRemove; i++) {
|
|
104
|
-
const first = msgContainer.firstElementChild;
|
|
105
|
-
if (first)
|
|
106
|
-
msgContainer.removeChild(first);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
// 去重 (save=true 时)
|
|
110
|
-
if (save) {
|
|
111
|
-
const lastContent = type === 'user' ? lastUserCommand : lastAiContent;
|
|
112
|
-
if (lastContent && content === lastContent) {
|
|
113
|
-
console.log(`[addMessage] 跳过重复的 ${type} 消息`);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (type === 'user')
|
|
117
|
-
lastUserCommand = content;
|
|
118
|
-
else
|
|
119
|
-
lastAiContent = content;
|
|
63
|
+
function addMessage(content, type, save = true, container, usedJudgmentIds = [], ctx = { messagesEl: null, messagesContainers: /* @__PURE__ */ new Map(), currentChannelId: null }) {
|
|
64
|
+
const messagesEl = ctx.messagesEl || (typeof document !== "undefined" ? document.getElementById("messages") : null);
|
|
65
|
+
const messagesContainers = ctx.messagesContainers || /* @__PURE__ */ new Map();
|
|
66
|
+
const currentChannelId = ctx.currentChannelId;
|
|
67
|
+
const msgContainer = container || getMessagesContainerForCurrent(currentChannelId, messagesContainers, messagesEl);
|
|
68
|
+
if (!save && msgContainer && msgContainer.children.length > 200) {
|
|
69
|
+
const toRemove = msgContainer.children.length - 200;
|
|
70
|
+
for (let i = 0; i < toRemove; i++) {
|
|
71
|
+
const first = msgContainer.firstElementChild;
|
|
72
|
+
if (first) msgContainer.removeChild(first);
|
|
120
73
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const knownToolNames = (ctx && ctx.knownToolNames) || new Set();
|
|
128
|
-
const segments = segmentChatReply(content, { knownToolNames });
|
|
129
|
-
// 没有可显示的 segment, 不上屏
|
|
130
|
-
if (segments.length === 0) {
|
|
131
|
-
return;
|
|
74
|
+
}
|
|
75
|
+
if (save) {
|
|
76
|
+
const lastContent = type === "user" ? lastUserCommand : lastAiContent;
|
|
77
|
+
if (lastContent && content === lastContent) {
|
|
78
|
+
console.log(`[addMessage] \u8DF3\u8FC7\u91CD\u590D\u7684 ${type} \u6D88\u606F`);
|
|
79
|
+
return;
|
|
132
80
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
renderedAny = true;
|
|
145
|
-
}
|
|
146
|
-
else if (seg.type === 'text' && seg.content) {
|
|
147
|
-
if (thinkContainer)
|
|
148
|
-
div.appendChild(thinkContainer);
|
|
149
|
-
thinkContainer = null;
|
|
150
|
-
div.appendChild(buildBubble(seg.content, type));
|
|
151
|
-
renderedAny = true;
|
|
152
|
-
}
|
|
153
|
-
else if (seg.type === 'final' && seg.content) {
|
|
154
|
-
if (thinkContainer)
|
|
155
|
-
div.appendChild(thinkContainer);
|
|
156
|
-
thinkContainer = null;
|
|
157
|
-
// final 段渲染为特殊气泡 (顶部有标记, 表示 LLM 显式终止)
|
|
158
|
-
const finalEl = buildBubble(seg.content, type);
|
|
159
|
-
finalEl.classList.add('bubble-final');
|
|
160
|
-
div.appendChild(finalEl);
|
|
161
|
-
renderedAny = true;
|
|
162
|
-
}
|
|
163
|
-
else if (seg.type === 'tool_call' && seg.tool) {
|
|
164
|
-
// tool_call segment 不渲染文字 — 走 step-timeline (步骤状态条)
|
|
165
|
-
// 这里只记录到 ctx 让外层在 addMessage 后挂到 timeline
|
|
166
|
-
if (ctx && ctx.toolCallCallback) {
|
|
167
|
-
ctx.toolCallCallback(seg.tool, div);
|
|
168
|
-
}
|
|
169
|
-
renderedAny = true; // 即使没文字, tool_call 也算"有意义"
|
|
170
|
-
}
|
|
81
|
+
if (type === "user") lastUserCommand = content;
|
|
82
|
+
else lastAiContent = content;
|
|
83
|
+
}
|
|
84
|
+
const div = document.createElement("div");
|
|
85
|
+
div.className = `message message-${type}`;
|
|
86
|
+
let cleanContent = content;
|
|
87
|
+
if (type === "ai") {
|
|
88
|
+
cleanContent = cleanContent.replace(/<think>[\s\S]*?<\/think>/g, "");
|
|
89
|
+
const finalGenIdx = cleanContent.indexOf("<final gen>");
|
|
90
|
+
if (finalGenIdx >= 0) {
|
|
91
|
+
cleanContent = cleanContent.substring(0, finalGenIdx).trim();
|
|
171
92
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (type ===
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
div.appendChild(time);
|
|
207
|
-
if (msgContainer) {
|
|
208
|
-
msgContainer.appendChild(div);
|
|
209
|
-
scheduleScrollToBottom(msgContainer);
|
|
93
|
+
}
|
|
94
|
+
const knownToolNames = ctx && ctx.knownToolNames || /* @__PURE__ */ new Set();
|
|
95
|
+
const segments = segmentChatReply(cleanContent, { knownToolNames });
|
|
96
|
+
if (segments.length === 0) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
let thinkContainer = null;
|
|
100
|
+
let renderedAny = false;
|
|
101
|
+
for (const seg of segments) {
|
|
102
|
+
if (seg.type === "think" && seg.content) {
|
|
103
|
+
thinkContainer = buildThinkContainer(seg.content);
|
|
104
|
+
div.appendChild(thinkContainer);
|
|
105
|
+
renderedAny = true;
|
|
106
|
+
} else if (seg.type === "env_details" && seg.content) {
|
|
107
|
+
div.appendChild(buildEnvContainer(seg.content));
|
|
108
|
+
renderedAny = true;
|
|
109
|
+
} else if (seg.type === "text" && seg.content) {
|
|
110
|
+
if (thinkContainer) div.appendChild(thinkContainer);
|
|
111
|
+
thinkContainer = null;
|
|
112
|
+
div.appendChild(buildBubble(seg.content, type));
|
|
113
|
+
renderedAny = true;
|
|
114
|
+
} else if (seg.type === "final" && seg.content) {
|
|
115
|
+
if (thinkContainer) div.appendChild(thinkContainer);
|
|
116
|
+
thinkContainer = null;
|
|
117
|
+
const finalEl = buildBubble(seg.content, type);
|
|
118
|
+
finalEl.classList.add("bubble-final");
|
|
119
|
+
div.appendChild(finalEl);
|
|
120
|
+
renderedAny = true;
|
|
121
|
+
} else if (seg.type === "tool_call" && seg.tool) {
|
|
122
|
+
if (ctx && ctx.toolCallCallback) {
|
|
123
|
+
ctx.toolCallCallback(seg.tool, div);
|
|
124
|
+
}
|
|
125
|
+
renderedAny = true;
|
|
210
126
|
}
|
|
127
|
+
}
|
|
128
|
+
if (!renderedAny) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const rawContent = segments.filter((s) => s.type === "text" || s.type === "final").map((s) => s.content || "").join("\n");
|
|
132
|
+
const time = document.createElement("div");
|
|
133
|
+
time.className = "time";
|
|
134
|
+
time.textContent = (/* @__PURE__ */ new Date()).toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" });
|
|
135
|
+
if (type === "ai") {
|
|
136
|
+
div.appendChild(buildMessageActions(div, rawContent, ctx));
|
|
137
|
+
}
|
|
138
|
+
if (type === "ai" && Array.isArray(usedJudgmentIds) && usedJudgmentIds.length > 0) {
|
|
139
|
+
const link = document.createElement("a");
|
|
140
|
+
link.className = "used-judgments-link";
|
|
141
|
+
link.textContent = `\u{1F4CE} \u53C2\u8003 ${usedJudgmentIds.length} \u6761\u539F\u5219`;
|
|
142
|
+
link.onclick = (e) => {
|
|
143
|
+
e.preventDefault();
|
|
144
|
+
if (typeof ctx.openJudgmentsModalWithFilter === "function") {
|
|
145
|
+
ctx.openJudgmentsModalWithFilter(usedJudgmentIds);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
div.appendChild(link);
|
|
149
|
+
}
|
|
150
|
+
if (type === "ai" && msgContainer) {
|
|
151
|
+
mountStepTimeline(div, currentChannelId);
|
|
152
|
+
}
|
|
153
|
+
div.appendChild(time);
|
|
154
|
+
if (msgContainer) {
|
|
155
|
+
msgContainer.appendChild(div);
|
|
156
|
+
scheduleScrollToBottom(msgContainer);
|
|
157
|
+
}
|
|
211
158
|
}
|
|
212
|
-
// ---------------------------------------------------------------------------
|
|
213
|
-
// 私有: think 折叠块
|
|
214
|
-
// ---------------------------------------------------------------------------
|
|
215
159
|
function buildThinkContainer(thinkContent) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
container.appendChild(content);
|
|
241
|
-
return container;
|
|
160
|
+
const container = document.createElement("div");
|
|
161
|
+
container.className = "think-container";
|
|
162
|
+
const toggle = document.createElement("div");
|
|
163
|
+
toggle.className = "think-toggle";
|
|
164
|
+
toggle.innerHTML = '\u{1F4AD} \u601D\u8003\u8FC7\u7A0B <span class="think-arrow">\u25B8</span>';
|
|
165
|
+
toggle.onclick = function() {
|
|
166
|
+
const details = container.querySelector(".think-content");
|
|
167
|
+
const arrow = toggle.querySelector(".think-arrow");
|
|
168
|
+
if (!details || !arrow) return;
|
|
169
|
+
if (details.style.display === "none") {
|
|
170
|
+
details.style.display = "block";
|
|
171
|
+
arrow.textContent = "\u25BE";
|
|
172
|
+
} else {
|
|
173
|
+
details.style.display = "none";
|
|
174
|
+
arrow.textContent = "\u25B8";
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const content = document.createElement("div");
|
|
178
|
+
content.className = "think-content";
|
|
179
|
+
content.style.display = "none";
|
|
180
|
+
content.innerHTML = `<pre>${escapeHtml(thinkContent)}</pre>`;
|
|
181
|
+
container.appendChild(toggle);
|
|
182
|
+
container.appendChild(content);
|
|
183
|
+
return container;
|
|
242
184
|
}
|
|
243
|
-
// ---------------------------------------------------------------------------
|
|
244
|
-
// 私有: environment_details 折叠块
|
|
245
|
-
// ---------------------------------------------------------------------------
|
|
246
185
|
function buildEnvContainer(envDetails) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
container.appendChild(content);
|
|
272
|
-
return container;
|
|
186
|
+
const container = document.createElement("div");
|
|
187
|
+
container.className = "env-container";
|
|
188
|
+
const toggle = document.createElement("div");
|
|
189
|
+
toggle.className = "env-toggle";
|
|
190
|
+
toggle.innerHTML = '\u2699\uFE0F \u73AF\u5883\u4FE1\u606F <span class="env-arrow">\u25B8</span>';
|
|
191
|
+
toggle.onclick = function() {
|
|
192
|
+
const details = container.querySelector(".environment-details");
|
|
193
|
+
const arrow = toggle.querySelector(".env-arrow");
|
|
194
|
+
if (!details || !arrow) return;
|
|
195
|
+
if (details.style.display === "none") {
|
|
196
|
+
details.style.display = "block";
|
|
197
|
+
arrow.textContent = "\u25BE";
|
|
198
|
+
} else {
|
|
199
|
+
details.style.display = "none";
|
|
200
|
+
arrow.textContent = "\u25B8";
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const content = document.createElement("div");
|
|
204
|
+
content.className = "environment-details";
|
|
205
|
+
content.style.display = "none";
|
|
206
|
+
content.innerHTML = `<pre>${escapeHtml(envDetails)}</pre>`;
|
|
207
|
+
container.appendChild(toggle);
|
|
208
|
+
container.appendChild(content);
|
|
209
|
+
return container;
|
|
273
210
|
}
|
|
274
|
-
// ---------------------------------------------------------------------------
|
|
275
|
-
// 私有: 气泡 (marked.parse 渲染)
|
|
276
|
-
// ---------------------------------------------------------------------------
|
|
277
211
|
function buildBubble(text, type) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
bubble.innerHTML = marked ? marked.parse(text) : escapeHtml(text);
|
|
284
|
-
return bubble;
|
|
212
|
+
const bubble = document.createElement("div");
|
|
213
|
+
bubble.className = `bubble bubble-${type}`;
|
|
214
|
+
const marked = window.marked;
|
|
215
|
+
bubble.innerHTML = marked ? marked.parse(text) : escapeHtml(text);
|
|
216
|
+
return bubble;
|
|
285
217
|
}
|
|
286
|
-
// ---------------------------------------------------------------------------
|
|
287
|
-
// 私有: 消息操作按钮 (复制 / 重新回答 / 蒸馏为判断)
|
|
288
|
-
// ---------------------------------------------------------------------------
|
|
289
218
|
function buildMessageActions(div, rawContent, ctx) {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
regenBtn.
|
|
318
|
-
regenBtn.
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
lastUserMsg = bubble.textContent || bubble.innerText || '';
|
|
329
|
-
break;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
219
|
+
const actions = document.createElement("div");
|
|
220
|
+
actions.className = "message-actions";
|
|
221
|
+
const copyBtn = document.createElement("button");
|
|
222
|
+
copyBtn.className = "action-btn copy-btn";
|
|
223
|
+
copyBtn.innerHTML = copyIcon() + " \u590D\u5236";
|
|
224
|
+
copyBtn.title = "\u590D\u5236\u6D88\u606F";
|
|
225
|
+
copyBtn.onclick = () => {
|
|
226
|
+
navigator.clipboard.writeText(rawContent).then(() => {
|
|
227
|
+
copyBtn.innerHTML = checkIcon() + " \u5DF2\u590D\u5236";
|
|
228
|
+
setTimeout(() => {
|
|
229
|
+
copyBtn.innerHTML = copyIcon() + " \u590D\u5236";
|
|
230
|
+
}, 2e3);
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
actions.appendChild(copyBtn);
|
|
234
|
+
const saveJudgmentBtn = document.createElement("button");
|
|
235
|
+
saveJudgmentBtn.className = "action-btn save-as-judgment";
|
|
236
|
+
saveJudgmentBtn.title = "AI \u84B8\u998F\u4E3A 30-80 \u5B57\u5224\u65AD\u529B + \u81EA\u52A8\u6F14\u5316\u5BF9\u9F50";
|
|
237
|
+
saveJudgmentBtn.setAttribute("data-decision", rawContent.substring(0, 800));
|
|
238
|
+
if (ctx.currentChannelId) saveJudgmentBtn.setAttribute("data-channel-id", ctx.currentChannelId);
|
|
239
|
+
saveJudgmentBtn.innerHTML = shieldIcon() + " \u84B8\u998F\u4E3A\u5224\u65AD";
|
|
240
|
+
actions.appendChild(saveJudgmentBtn);
|
|
241
|
+
const regenBtn = document.createElement("button");
|
|
242
|
+
regenBtn.className = "action-btn regenerate-btn";
|
|
243
|
+
regenBtn.innerHTML = refreshIcon(false) + " \u91CD\u65B0\u56DE\u7B54";
|
|
244
|
+
regenBtn.title = "\u91CD\u65B0\u751F\u6210\u56DE\u590D";
|
|
245
|
+
regenBtn.onclick = () => {
|
|
246
|
+
regenBtn.innerHTML = refreshIcon(true) + " \u751F\u6210\u4E2D...";
|
|
247
|
+
regenBtn.disabled = true;
|
|
248
|
+
const messages = div.parentElement?.querySelectorAll(".message") || [];
|
|
249
|
+
let lastUserMsg = "";
|
|
250
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
251
|
+
const msg = messages[i];
|
|
252
|
+
if (msg.classList.contains("message-user")) {
|
|
253
|
+
const bubble = msg.querySelector(".bubble");
|
|
254
|
+
if (bubble) {
|
|
255
|
+
lastUserMsg = bubble.textContent || bubble.innerText || "";
|
|
256
|
+
break;
|
|
332
257
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
fetch("/regenerate", {
|
|
261
|
+
method: "POST",
|
|
262
|
+
headers: { "Content-Type": "application/json" },
|
|
263
|
+
body: JSON.stringify({ channelId: ctx.currentChannelId, userMessage: lastUserMsg })
|
|
264
|
+
}).then((res) => {
|
|
265
|
+
if (!res.ok) throw new Error("regenerate failed");
|
|
266
|
+
}).catch((err) => {
|
|
267
|
+
console.error("\u91CD\u65B0\u751F\u6210\u5931\u8D25:", err);
|
|
268
|
+
regenBtn.innerHTML = refreshIcon(false) + " \u5931\u8D25";
|
|
269
|
+
setTimeout(() => {
|
|
270
|
+
regenBtn.innerHTML = refreshIcon(false) + " \u91CD\u65B0\u56DE\u7B54";
|
|
271
|
+
regenBtn.disabled = false;
|
|
272
|
+
}, 2e3);
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
actions.appendChild(regenBtn);
|
|
276
|
+
return actions;
|
|
351
277
|
}
|
|
352
|
-
function copyIcon() {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
function
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
scheduleScrollToBottom(container);
|
|
278
|
+
function copyIcon() {
|
|
279
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
|
|
280
|
+
}
|
|
281
|
+
function checkIcon() {
|
|
282
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"></polyline></svg>';
|
|
283
|
+
}
|
|
284
|
+
function shieldIcon() {
|
|
285
|
+
return '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2L4 6v6c0 5 3.5 9.5 8 10 4.5-.5 8-5 8-10V6l-8-4z"></path><path d="M9 12l2 2 4-4"></path></svg>';
|
|
286
|
+
}
|
|
287
|
+
function refreshIcon(spin = false) {
|
|
288
|
+
const cls = spin ? ' class="spin"' : "";
|
|
289
|
+
return `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"${cls}><path d="M21 2v6h-6M3 12a9 9 0 0 1 15-6.7L21 8M3 22v-6h6M21 12a9 9 0 0 1-15 6.7L3 16"></path></svg>`;
|
|
290
|
+
}
|
|
291
|
+
function handleStreamTokenEvent(data, ctx = { messagesEl: null, messagesContainers: /* @__PURE__ */ new Map(), currentChannelId: null }) {
|
|
292
|
+
const messagesEl = ctx.messagesEl || (typeof document !== "undefined" ? document.getElementById("messages") : null);
|
|
293
|
+
const messagesContainers = ctx.messagesContainers || /* @__PURE__ */ new Map();
|
|
294
|
+
const currentChannelId = ctx.currentChannelId;
|
|
295
|
+
const container = getMessagesContainerForCurrent(currentChannelId, messagesContainers, messagesEl);
|
|
296
|
+
if (!container) return;
|
|
297
|
+
const delta = data.content || "";
|
|
298
|
+
if (!delta) return;
|
|
299
|
+
if (!streamingMessageEl || !streamingMessageEl.isConnected) {
|
|
300
|
+
streamingMessageEl = document.createElement("div");
|
|
301
|
+
streamingMessageEl.className = "message message-ai message-streaming";
|
|
302
|
+
streamingTextNode = document.createTextNode("");
|
|
303
|
+
streamingMessageEl.appendChild(streamingTextNode);
|
|
304
|
+
streamingText = "";
|
|
305
|
+
mountStepTimeline(streamingMessageEl, currentChannelId);
|
|
306
|
+
container.appendChild(streamingMessageEl);
|
|
307
|
+
if (typeof ctx.setTimelineState === "function") {
|
|
308
|
+
ctx.setTimelineState("streaming");
|
|
385
309
|
}
|
|
386
|
-
if (streamingTextNode)
|
|
387
|
-
streamingTextNode.appendData(delta);
|
|
388
|
-
streamingText += delta;
|
|
389
310
|
scheduleScrollToBottom(container);
|
|
311
|
+
}
|
|
312
|
+
if (data.streamType === "token") {
|
|
313
|
+
if (streamingTextNode) streamingTextNode.nodeValue = delta;
|
|
314
|
+
streamingText = delta;
|
|
315
|
+
} else {
|
|
316
|
+
if (streamingTextNode) streamingTextNode.appendData(delta);
|
|
317
|
+
streamingText += delta;
|
|
318
|
+
}
|
|
319
|
+
scheduleScrollToBottom(container);
|
|
390
320
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
// 2026-06-15: finalize 时把 streaming 内的 step-timeline 整体搬到新建的正式 AI message 内
|
|
401
|
-
// addMessage 会建一个新 timeline 占位, 先记下 streaming 的引用, addMessage 后再迁移
|
|
402
|
-
// 避免节点从 0 重渲 (10+ 步的任务, 重渲闪烁会很厉害)
|
|
403
|
-
const oldStreamingEl = streamingMessageEl;
|
|
404
|
-
if (oldStreamingEl && oldStreamingEl.parentNode) {
|
|
405
|
-
oldStreamingEl.parentNode.removeChild(oldStreamingEl);
|
|
406
|
-
}
|
|
407
|
-
addMessage(streamingText, 'ai', true, container, ctx.lastUsedJudgmentIds || [], ctx);
|
|
408
|
-
if (oldStreamingEl && container) {
|
|
409
|
-
// 找刚 addMessage 创建的最后一条 ai message
|
|
410
|
-
const newAiMsg = container.querySelector('.message-ai:last-of-type');
|
|
411
|
-
if (newAiMsg && newAiMsg !== oldStreamingEl) {
|
|
412
|
-
migrateStepTimeline(oldStreamingEl, newAiMsg);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
321
|
+
function finalizeTimelineAsMessage(ctx = { messagesEl: null, messagesContainers: /* @__PURE__ */ new Map(), currentChannelId: null }) {
|
|
322
|
+
const messagesEl = ctx.messagesEl || (typeof document !== "undefined" ? document.getElementById("messages") : null);
|
|
323
|
+
const messagesContainers = ctx.messagesContainers || /* @__PURE__ */ new Map();
|
|
324
|
+
const currentChannelId = ctx.currentChannelId;
|
|
325
|
+
const container = getMessagesContainerForCurrent(currentChannelId, messagesContainers, messagesEl);
|
|
326
|
+
if (streamingText.trim().length > 0) {
|
|
327
|
+
const oldStreamingEl = streamingMessageEl;
|
|
328
|
+
if (oldStreamingEl && oldStreamingEl.parentNode) {
|
|
329
|
+
oldStreamingEl.parentNode.removeChild(oldStreamingEl);
|
|
415
330
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
ctx.setTimelineState('done');
|
|
331
|
+
addMessage(streamingText, "ai", true, container, ctx.lastUsedJudgmentIds || [], ctx);
|
|
332
|
+
if (oldStreamingEl && container) {
|
|
333
|
+
const newAiMsg = container.querySelector(".message-ai:last-of-type");
|
|
334
|
+
if (newAiMsg && newAiMsg !== oldStreamingEl) {
|
|
335
|
+
migrateStepTimeline(oldStreamingEl, newAiMsg);
|
|
336
|
+
}
|
|
423
337
|
}
|
|
338
|
+
}
|
|
339
|
+
streamingMessageEl = null;
|
|
340
|
+
streamingTextNode = null;
|
|
341
|
+
streamingText = "";
|
|
342
|
+
if (typeof ctx.setTimelineState === "function") {
|
|
343
|
+
ctx.setTimelineState("done");
|
|
344
|
+
}
|
|
424
345
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if (!timeline)
|
|
449
|
-
return;
|
|
450
|
-
pushStepToTimeline(timeline, data.type, {
|
|
451
|
-
tool: data.tool || 'unknown',
|
|
452
|
-
args: data.args,
|
|
453
|
-
success: data.success,
|
|
454
|
-
output: data.output,
|
|
455
|
-
error: data.error,
|
|
456
|
-
});
|
|
346
|
+
function handleStepEvent(data, ctx = { messagesEl: null, messagesContainers: /* @__PURE__ */ new Map(), currentChannelId: null }) {
|
|
347
|
+
const messagesEl = ctx.messagesEl || (typeof document !== "undefined" ? document.getElementById("messages") : null);
|
|
348
|
+
const messagesContainers = ctx.messagesContainers || /* @__PURE__ */ new Map();
|
|
349
|
+
const currentChannelId = ctx.currentChannelId;
|
|
350
|
+
const container = getMessagesContainerForCurrent(currentChannelId, messagesContainers, messagesEl);
|
|
351
|
+
if (!container) return;
|
|
352
|
+
if (!data || !data.type) return;
|
|
353
|
+
let target = streamingMessageEl && streamingMessageEl.isConnected ? streamingMessageEl : null;
|
|
354
|
+
if (!target) {
|
|
355
|
+
const aiMsgs = container.querySelectorAll(".message-ai");
|
|
356
|
+
if (aiMsgs.length === 0) return;
|
|
357
|
+
target = aiMsgs[aiMsgs.length - 1];
|
|
358
|
+
}
|
|
359
|
+
if (!target) return;
|
|
360
|
+
const timeline = getStepTimeline(target);
|
|
361
|
+
if (!timeline) return;
|
|
362
|
+
pushStepToTimeline(timeline, data.type, {
|
|
363
|
+
tool: data.tool || "unknown",
|
|
364
|
+
args: data.args,
|
|
365
|
+
success: data.success,
|
|
366
|
+
output: data.output,
|
|
367
|
+
error: data.error
|
|
368
|
+
});
|
|
457
369
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
clearTimeout(scrollToBottomTimer);
|
|
469
|
-
scrollToBottomTimer = null;
|
|
470
|
-
}
|
|
370
|
+
function resetRendererState() {
|
|
371
|
+
streamingMessageEl = null;
|
|
372
|
+
streamingTextNode = null;
|
|
373
|
+
streamingText = "";
|
|
374
|
+
lastUserCommand = "";
|
|
375
|
+
lastAiContent = "";
|
|
376
|
+
if (scrollToBottomTimer) {
|
|
377
|
+
clearTimeout(scrollToBottomTimer);
|
|
378
|
+
scrollToBottomTimer = null;
|
|
379
|
+
}
|
|
471
380
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
381
|
+
const MessageRenderer = {
|
|
382
|
+
addMessage,
|
|
383
|
+
handleStreamTokenEvent,
|
|
384
|
+
finalizeTimelineAsMessage,
|
|
385
|
+
handleStepEvent,
|
|
386
|
+
escapeHtml,
|
|
387
|
+
getMessagesContainerForCurrent,
|
|
388
|
+
resetRendererState
|
|
480
389
|
};
|
|
481
|
-
if (typeof window !==
|
|
482
|
-
|
|
390
|
+
if (typeof window !== "undefined") {
|
|
391
|
+
window.MR = MessageRenderer;
|
|
483
392
|
}
|
|
393
|
+
export {
|
|
394
|
+
MessageRenderer,
|
|
395
|
+
addMessage,
|
|
396
|
+
escapeHtml,
|
|
397
|
+
finalizeTimelineAsMessage,
|
|
398
|
+
getMessagesContainerForCurrent,
|
|
399
|
+
handleStepEvent,
|
|
400
|
+
handleStreamTokenEvent,
|
|
401
|
+
hasStreamingText,
|
|
402
|
+
injectRecoveredText,
|
|
403
|
+
replaceStreamingText,
|
|
404
|
+
resetRendererState
|
|
405
|
+
};
|