/* ===================================================================== CowAgent Console - Main Application Script ===================================================================== */ // ===================================================================== // Version — fetched from backend (single source: /VERSION file) // ===================================================================== let APP_VERSION = ''; // ===================================================================== // i18n // ===================================================================== const I18N = { zh: { console: '控制台', nav_chat: '对话', nav_manage: '管理', nav_monitor: '监控', menu_chat: '对话', menu_config: '配置', menu_models: '模型', menu_skills: '技能', menu_memory: '记忆', menu_knowledge: '知识', menu_channels: '通道', menu_tasks: '定时', menu_logs: '日志', models_title: '模型管理', models_desc: '统一管理对话、图像、语音、向量、搜索能力', models_section_vendors: '厂商凭据', models_section_vendors_desc: '一处配置,多个模型能力共享', models_section_capabilities: '模型能力', models_add_vendor: '添加厂商', models_provider: '厂商', models_model: '模型', models_voice: '音色', models_configured: '已配置', models_not_configured: '未配置', models_pick_to_configure: '选择以配置', models_clear_credential: '清除凭据', models_base_default_hint: '留空将使用官方默认地址', models_base_default: '默认', models_capability_chat: '主模型', models_capability_chat_desc: '用于基础对话和 Agent 推理', models_capability_vision: '图像理解', models_capability_vision_desc: '识别图片内容,用于图像识别工具', models_capability_image: '图像生成', models_capability_image_desc: '生成图片,用于图像生成技能', models_auto_using: '当前优先使用', models_capability_asr: '语音识别', models_capability_asr_desc: '语音转文字', models_capability_tts: '语音合成', models_capability_tts_desc: '文字转语音', models_capability_embedding: '向量', models_capability_embedding_desc: '用于记忆与知识的向量化检索', models_capability_search: '联网搜索', models_capability_search_desc: '实时网页检索能力,用于搜索工具', models_strategy_auto: '自动', models_search_strategy_label: '策略', models_search_strategy_fixed: '指定', models_search_strategy_auto_hint: '从已配置厂商中自动选择', models_search_strategy_fixed_hint: '指定使用搜索厂商', models_pending_config: '待配置', models_search_available_label: '可用搜索厂商:', models_search_none_configured: '暂未启用任何搜索厂商,点击添加', models_search_add_provider: '添加厂商', models_search_add_desc: '选择一个搜索厂商进行配置', models_search_bocha_title: '配置博查 API Key', models_search_bocha_desc: '前往博查开放平台创建 API Key', models_search_edit_hint: '点击修改配置', models_unavailable: '不可用', models_set_via_env: '通过环境变量启用', models_dim_label: '维度', models_save_success: '已保存', models_save_failed: '保存失败', models_cleared: '已清除', models_clear_failed: '清除失败', models_embedding_change_title: '更改向量模型', models_embedding_change_msg: '切换向量模型后,已有索引将失效,需要重建。是否继续?', models_embedding_saved_title: '向量模型已更新', models_embedding_saved_msg: '请在聊天框输入 /memory rebuild-index 重建索引。', models_embedding_saved_ok: '去执行', models_pick_provider: '待选择', models_clear_confirm_title: '清除厂商凭据', models_clear_confirm_msg: '确认清除该厂商的 API Key 与 Base URL 吗?相关能力将不再可用。', cancel: '取消', save: '保存', ok: '确定', knowledge_title: '知识库', knowledge_desc: '浏览和探索你的知识库', knowledge_tab_docs: '文档', knowledge_tab_graph: '图谱', knowledge_loading: '加载知识库中...', knowledge_loading_desc: '知识页面将显示在这里', knowledge_select_hint: '选择一个文档查看', knowledge_empty_hint: '暂无知识页面', knowledge_empty_guide: '在对话中发送文档、链接或主题给 Agent,它会自动整理到你的知识库中。', knowledge_go_chat: '开始对话', welcome_subtitle: '我可以帮你解答问题、管理计算机、创造和执行技能,并通过
长期记忆和知识库不断成长', example_sys_title: '系统管理', example_sys_text: '查看工作空间里有哪些文件', example_task_title: '定时任务', example_task_text: '1分钟后提醒我检查服务器', example_code_title: '编程助手', example_code_text: '搜索AI资讯并生成可视化网页报告', example_knowledge_title: '知识库', example_knowledge_text: '查看知识库当前文档情况', example_skill_title: '技能系统', example_skill_text: '查看所有支持的工具和技能', example_web_title: '指令中心', example_web_text: '查看全部命令', slash_help: '显示命令帮助', slash_status: '查看运行状态', slash_context: '查看对话上下文', slash_context_clear: '清除对话上下文', slash_skill_list: '查看已安装技能', slash_skill_list_remote: '浏览技能广场', slash_skill_search: '搜索技能', slash_skill_install: '安装技能 (名称或 GitHub URL)', slash_skill_uninstall: '卸载技能', slash_skill_info: '查看技能详情', slash_skill_enable: '启用技能', slash_skill_disable: '禁用技能', slash_memory_dream: '手动触发记忆蒸馏 (可指定天数, 默认3)', slash_knowledge: '查看知识库统计', slash_knowledge_list: '查看知识库文件树', slash_knowledge_on: '开启知识库', slash_knowledge_off: '关闭知识库', slash_config: '查看当前配置', slash_cancel: '中止当前正在运行的 Agent 任务', slash_logs: '查看最近日志', slash_version: '查看版本', input_placeholder: '输入消息,或输入 / 使用指令', config_title: '配置管理', config_desc: '管理模型和 Agent 配置', config_model: '模型配置', config_agent: 'Agent 配置', config_language: '语言', config_language_hint: '界面展示、命令文案、系统提示词等使用的语言(与右上角切换同步)', config_model_advanced: '高级配置', config_channel: '通道配置', config_agent_enabled: 'Agent 模式', config_max_tokens: '最大上下文 Token', config_max_tokens_hint: '对话中 Agent 能输入的最大 Token 长度,超过后会智能压缩处理', config_max_turns: '最大记忆轮次', config_max_turns_hint: '一问一答为一轮,超过后会智能压缩处理', config_max_steps: '最大执行步数', config_max_steps_hint: '单次对话中 Agent 最多调用工具的次数', config_enable_thinking: '深度思考', config_enable_thinking_hint: '是否启用深度思考模式', config_channel_type: '通道类型', config_provider: '模型厂商', config_model_name: '模型', config_custom_model_hint: '输入自定义模型名称', config_save: '保存', config_saved: '已保存', config_save_error: '保存失败', config_custom_option: '自定义', config_custom_tip: '接口需遵循 OpenAI API 协议', config_security: '安全设置', config_password: '访问密码', config_password_hint: '留空则不启用密码保护', config_password_changed: '密码已更新,请重新登录', config_password_cleared: '密码已清除', skills_title: '技能管理', skills_desc: '查看、启用或禁用 Agent 工具和技能', skills_hub_btn: '探索技能广场', skills_loading: '加载技能中...', skills_loading_desc: '技能加载后将显示在此处', tools_section_title: '内置工具', tools_loading: '加载工具中...', skills_section_title: '技能', skill_enable: '启用', skill_disable: '禁用', skill_toggle_error: '操作失败,请稍后再试', memory_title: '记忆管理', memory_desc: '查看 Agent 记忆文件和内容', memory_tab_files: '记忆文件', memory_tab_dreams: '梦境日记', memory_loading: '加载记忆文件中...', memory_loading_desc: '记忆文件将显示在此处', memory_back: '返回列表', memory_col_name: '文件名', memory_col_type: '类型', memory_col_size: '大小', memory_col_updated: '更新时间', channels_title: '通道管理', channels_desc: '管理已接入的消息通道', channels_add: '接入通道', channels_disconnect: '断开', channels_save: '保存配置', channels_saved: '已保存', channels_save_error: '保存失败', channels_restarted: '已保存并重启', channels_connect_btn: '接入', channels_cancel: '取消', channels_select_placeholder: '选择要接入的通道...', channels_empty: '暂未接入任何通道', channels_empty_desc: '点击右上角「接入通道」按钮开始配置', channels_disconnect_confirm: '确认断开该通道?配置将保留但通道会停止运行。', channels_connected: '已接入', channels_connecting: '接入中...', weixin_scan_title: '微信扫码登录', weixin_scan_desc: '请使用微信扫描下方二维码', weixin_scan_loading: '正在获取二维码...', weixin_scan_waiting: '等待扫码...', weixin_scan_scanned: '已扫码,请在手机上确认', weixin_scan_expired: '二维码已过期,正在刷新...', weixin_scan_success: '登录成功,正在启动通道...', weixin_scan_fail: '获取二维码失败', weixin_qr_tip: '二维码约2分钟后过期', wecom_scan_btn: '扫码创建企微机器人', wecom_scan_desc: '使用企业微信扫码,一键创建智能机器人', wecom_scan_success: '创建成功,正在启动通道...', wecom_scan_fail: '创建失败', wecom_mode_scan: '扫码接入', wecom_mode_manual: '手动填写', feishu_scan_btn: '一键创建飞书应用', feishu_scan_desc: '使用飞书 App 扫码,自动创建应用并预置全部权限与事件订阅', feishu_scan_replace_desc: '使用飞书 App 扫码创建新机器人,将覆盖当前的 App ID / Secret', feishu_scan_loading: '正在向飞书申请二维码...', feishu_scan_waiting: '等待扫码...', feishu_scan_tip: '二维码 10 分钟内有效,仅供一次扫描', feishu_scan_open_link: '或点击此处在浏览器中打开', feishu_scan_success: '应用创建成功,正在启动通道...', feishu_scan_expired: '二维码已过期,请重试', feishu_scan_denied: '已取消授权', feishu_scan_fail: '创建失败', feishu_scan_retry: '重试', feishu_mode_scan: '扫码创建', feishu_mode_manual: '手动填写', tasks_title: '定时任务', tasks_desc: '查看和管理定时任务', tasks_coming: '即将推出', tasks_coming_desc: '定时任务管理功能即将在此提供', logs_title: '日志', logs_desc: '实时日志输出 (run.log)', logs_live: '实时', logs_coming_msg: '日志流即将在此提供。将连接 run.log 实现类似 tail -f 的实时输出。', new_chat: '新对话', session_history: '历史会话', today: '今天', yesterday: '昨天', earlier: '更早', delete_session_confirm: '确认删除该会话?所有消息将被清除。', delete_session_title: '删除会话', untitled_session: '新对话', context_cleared: '— 以上内容已从上下文中移除 —', tip_new_chat: '新建对话', tip_clear_context: '清除上下文', tip_attach: '添加附件', attach_menu_file: '上传文件', mic_idle_title: '点击录音 / 再按一次结束', mic_recording_title: '录音中,再次点击结束', mic_busy_title: '识别中…', mic_permission_denied: '无法访问麦克风,请检查浏览器权限', mic_too_short: '录音太短,请重试', mic_error: '语音识别失败', speak_msg: '朗读这段回复', voice_reply_mode_label: '语音回复策略', voice_reply_off: '关闭', voice_reply_if_voice: '仅语音问/语音答', voice_reply_always: '总是语音回复', attach_menu_folder: '上传文件夹', confirm_yes: '确认', confirm_cancel: '取消', error_send: '发送失败,请稍后再试。', error_timeout: '请求超时,请再试一次。', thinking_in_progress: '思考中...', thinking_done: '已深度思考', thinking_duration: '耗时', }, en: { console: 'Console', nav_chat: 'Chat', nav_manage: 'Management', nav_monitor: 'Monitor', menu_chat: 'Chat', menu_config: 'Config', menu_models: 'Models', menu_skills: 'Skills', menu_memory: 'Memory', menu_knowledge: 'Knowledge', menu_channels: 'Channels', menu_tasks: 'Tasks', menu_logs: 'Logs', models_title: 'Models', models_desc: 'Manage chat, image, voice, embedding and search capabilities in one place', models_section_vendors: 'Vendor Credentials', models_section_vendors_desc: 'Configured once, shared by multiple model capabilities', models_section_capabilities: 'Capabilities', models_add_vendor: 'Add Vendor', models_provider: 'Provider', models_model: 'Model', models_voice: 'Voice', models_configured: 'configured', models_not_configured: 'not configured', models_pick_to_configure: 'pick to configure', models_clear_credential: 'Clear credentials', models_base_default_hint: 'Leave blank to use the official default base URL', models_base_default: 'Default', models_capability_chat: 'Main Model', models_capability_chat_desc: 'Used for basic chat and agent reasoning', models_capability_vision: 'Image Understanding', models_capability_vision_desc: 'Recognizes image content, used by image recognition tools', models_capability_image: 'Image Generation', models_capability_image_desc: 'Generates images, used by image generation skills', models_auto_using: 'Preferred', models_capability_asr: 'Speech Recognition', models_capability_asr_desc: 'Voice to text', models_capability_tts: 'Speech Synthesis', models_capability_tts_desc: 'Text to voice', models_capability_embedding: 'Embedding', models_capability_embedding_desc: 'Used for vectorized retrieval of memory and knowledge', models_capability_search: 'Web Search', models_capability_search_desc: 'Real-time web retrieval, used by search tools', models_strategy_auto: 'auto', models_search_strategy_label: 'Strategy', models_search_strategy_fixed: 'Pinned', models_search_strategy_auto_hint: 'Auto-pick from configured providers', models_search_strategy_fixed_hint: 'Always use a specific provider', models_pending_config: 'Pending setup', models_search_available_label: 'Available:', models_search_none_configured: 'No search provider enabled yet — click add.', models_search_add_provider: 'Add provider', models_search_add_desc: 'Pick a search provider to configure', models_search_bocha_title: 'Configure Bocha API Key', models_search_bocha_desc: 'Create a key at the Bocha open platform.', models_search_edit_hint: 'Click to edit', models_unavailable: 'unavailable', models_set_via_env: 'enable via environment variable', models_dim_label: 'dim', models_save_success: 'Saved', models_save_failed: 'Save failed', models_cleared: 'Cleared', models_clear_failed: 'Clear failed', models_embedding_change_title: 'Change embedding model', models_embedding_change_msg: 'Switching the embedding model invalidates the existing index — a rebuild will be needed. Continue?', models_embedding_saved_title: 'Embedding model updated', models_embedding_saved_msg: 'Send /memory rebuild-index in the chat to rebuild the index.', models_embedding_saved_ok: 'Go', models_pick_provider: 'Pick a provider', models_clear_confirm_title: 'Clear vendor credentials', models_clear_confirm_msg: 'Remove this vendor\'s API Key and Base URL? Capabilities relying on it will stop working.', cancel: 'Cancel', save: 'Save', ok: 'OK', knowledge_title: 'Knowledge', knowledge_desc: 'Browse and explore your knowledge base', knowledge_tab_docs: 'Documents', knowledge_tab_graph: 'Graph', knowledge_loading: 'Loading knowledge base...', knowledge_loading_desc: 'Knowledge pages will be displayed here', knowledge_select_hint: 'Select a document to view', knowledge_empty_hint: 'No knowledge pages yet', knowledge_empty_guide: 'Send documents, links or topics to the agent in chat, and it will automatically organize them into your knowledge base.', knowledge_go_chat: 'Start a conversation', welcome_subtitle: 'I can help you answer questions, manage your computer, create and execute skills, and keep growing through
long-term memory and a personal knowledge base.', example_sys_title: 'System', example_sys_text: 'Show me the files in the workspace', example_task_title: 'Scheduler', example_task_text: 'Remind me to check the server in 5 minutes', example_code_title: 'Coding', example_code_text: 'Search today\'s AI news and generate a visual report webpage', example_knowledge_title: 'Knowledge', example_knowledge_text: 'Show me the current knowledge base', example_skill_title: 'Skills', example_skill_text: 'Show current tools and skills', example_web_title: 'Commands', example_web_text: 'Show all commands', slash_help: 'Show this help', slash_status: 'Show running status', slash_context: 'Show conversation context', slash_context_clear: 'Clear conversation context', slash_skill_list: 'List installed skills', slash_skill_list_remote: 'Browse Skill Hub', slash_skill_search: 'Search skills', slash_skill_install: 'Install a skill (name or GitHub URL)', slash_skill_uninstall: 'Uninstall a skill', slash_skill_info: 'Show skill details', slash_skill_enable: 'Enable a skill', slash_skill_disable: 'Disable a skill', slash_memory_dream: 'Trigger memory distillation (optional days, default 3)', slash_knowledge: 'Show knowledge base stats', slash_knowledge_list: 'Show knowledge base file tree', slash_knowledge_on: 'Enable knowledge base', slash_knowledge_off: 'Disable knowledge base', slash_config: 'Show current config', slash_cancel: 'Abort the running Agent task', slash_logs: 'Show recent logs', slash_version: 'Show version', input_placeholder: 'Type a message, or press / for commands', config_title: 'Configuration', config_desc: 'Manage model and agent settings', config_model: 'Model Configuration', config_agent: 'Agent Configuration', config_language: 'Language', config_language_hint: 'Language for the UI, command text, system prompts and more (synced with the top-right switch)', config_model_advanced: 'Advanced', config_channel: 'Channel Configuration', config_agent_enabled: 'Agent Mode', config_max_tokens: 'Max Context Tokens', config_max_tokens_hint: 'Max tokens the Agent can input per conversation, auto-compressed when exceeded', config_max_turns: 'Max Memory Turns', config_max_turns_hint: 'One Q&A pair = one turn, auto-compressed when exceeded', config_max_steps: 'Max Steps', config_max_steps_hint: 'Max tool calls the Agent can make in a single conversation', config_enable_thinking: 'Deep Thinking', config_enable_thinking_hint: 'Enable deep thinking mode', config_channel_type: 'Channel Type', config_provider: 'Provider', config_model_name: 'Model', config_custom_model_hint: 'Enter custom model name', config_save: 'Save', config_saved: 'Saved', config_save_error: 'Save failed', config_custom_option: 'Custom', config_custom_tip: 'API must follow OpenAI protocol.', config_security: 'Security', config_password: 'Password', config_password_hint: 'Leave empty to disable password protection', config_password_changed: 'Password updated, please re-login', config_password_cleared: 'Password cleared', skills_title: 'Skills', skills_desc: 'View, enable, or disable agent tools and skills', skills_hub_btn: 'Skill Hub', skills_loading: 'Loading skills...', skills_loading_desc: 'Skills will be displayed here after loading', tools_section_title: 'Built-in Tools', tools_loading: 'Loading tools...', skills_section_title: 'Skills', skill_enable: 'Enable', skill_disable: 'Disable', skill_toggle_error: 'Operation failed, please try again', memory_title: 'Memory', memory_desc: 'View agent memory files and contents', memory_tab_files: 'Memory Files', memory_tab_dreams: 'Dream Diary', memory_loading: 'Loading memory files...', memory_loading_desc: 'Memory files will be displayed here', memory_back: 'Back to list', memory_col_name: 'Filename', memory_col_type: 'Type', memory_col_size: 'Size', memory_col_updated: 'Updated', channels_title: 'Channels', channels_desc: 'Manage connected messaging channels', channels_add: 'Connect', channels_disconnect: 'Disconnect', channels_save: 'Save', channels_saved: 'Saved', channels_save_error: 'Save failed', channels_restarted: 'Saved & Restarted', channels_connect_btn: 'Connect', channels_cancel: 'Cancel', channels_select_placeholder: 'Select a channel to connect...', channels_empty: 'No channels connected', channels_empty_desc: 'Click the "Connect" button above to get started', channels_disconnect_confirm: 'Disconnect this channel? Config will be preserved but the channel will stop.', channels_connected: 'Connected', channels_connecting: 'Connecting...', weixin_scan_title: 'WeChat QR Login', weixin_scan_desc: 'Scan the QR code below with WeChat', weixin_scan_loading: 'Loading QR code...', weixin_scan_waiting: 'Waiting for scan...', weixin_scan_scanned: 'Scanned, please confirm on your phone', weixin_scan_expired: 'QR code expired, refreshing...', weixin_scan_success: 'Login successful, starting channel...', weixin_scan_fail: 'Failed to load QR code', weixin_qr_tip: 'QR code expires in ~2 minutes', wecom_scan_btn: 'Scan to Create WeCom Bot', wecom_scan_desc: 'Scan with WeCom to create a bot instantly', wecom_scan_success: 'Bot created, starting channel...', wecom_scan_fail: 'Bot creation failed', wecom_mode_scan: 'Scan QR', wecom_mode_manual: 'Manual', feishu_scan_btn: 'One-click Create Feishu App', feishu_scan_desc: 'Scan with Feishu App to create an app with all required permissions pre-configured', feishu_scan_replace_desc: 'Scan with Feishu App to create a new bot — will overwrite the current App ID / Secret', feishu_scan_loading: 'Requesting QR code from Feishu...', feishu_scan_waiting: 'Waiting for scan...', feishu_scan_tip: 'QR code expires in 10 minutes, single use only', feishu_scan_open_link: 'Or click here to open in browser', feishu_scan_success: 'App created, starting channel...', feishu_scan_expired: 'QR code expired, please retry', feishu_scan_denied: 'Authorization cancelled', feishu_scan_fail: 'App creation failed', feishu_scan_retry: 'Retry', feishu_mode_scan: 'Scan QR', feishu_mode_manual: 'Manual', tasks_title: 'Scheduled Tasks', tasks_desc: 'View and manage scheduled tasks', tasks_coming: 'Coming Soon', tasks_coming_desc: 'Scheduled task management will be available here', logs_title: 'Logs', logs_desc: 'Real-time log output (run.log)', logs_live: 'Live', logs_coming_msg: 'Log streaming will be available here. Connects to run.log for real-time output similar to tail -f.', new_chat: 'New Chat', session_history: 'History', today: 'Today', yesterday: 'Yesterday', earlier: 'Earlier', delete_session_confirm: 'Delete this session? All messages will be removed.', delete_session_title: 'Delete Session', untitled_session: 'New Chat', context_cleared: '— Context above has been cleared —', tip_new_chat: 'New Chat', tip_clear_context: 'Clear Context', tip_attach: 'Add Attachment', attach_menu_file: 'Upload File', mic_idle_title: 'Click to record, click again to stop', mic_recording_title: 'Recording, click to stop', mic_busy_title: 'Transcribing…', mic_permission_denied: 'Cannot access microphone — check browser permissions', mic_too_short: 'Recording too short, please retry', mic_error: 'Speech recognition failed', speak_msg: 'Read this reply aloud', voice_reply_mode_label: 'Voice reply policy', voice_reply_off: 'Off', voice_reply_if_voice: 'Voice only if voice input', voice_reply_always: 'Always reply with voice', attach_menu_folder: 'Upload Folder', confirm_yes: 'Confirm', confirm_cancel: 'Cancel', error_send: 'Failed to send. Please try again.', error_timeout: 'Request timeout. Please try again.', thinking_in_progress: 'Thinking...', thinking_done: 'Thought', thinking_duration: 'Duration', } }; // Resolve language by priority: user choice (localStorage) -> backend-detected // (cow_lang) -> browser language -> 'zh'. Shares __cowResolveLang__ defined in // chat.html; falls back to a local resolver if loaded standalone. let currentLang = (typeof window.__cowResolveLang__ === 'function') ? window.__cowResolveLang__() : (function () { const norm = (raw) => { if (!raw) return ''; const v = String(raw).trim().toLowerCase(); if (v === 'auto') return ''; if (v.indexOf('zh') === 0) return 'zh'; if (v.indexOf('en') === 0) return 'en'; return ''; }; return norm(localStorage.getItem('cow_lang')) || norm(window.__COW_DEFAULT_LANG__) || norm(navigator.language) || 'zh'; })(); function t(key) { return (I18N[currentLang] && I18N[currentLang][key]) || (I18N.en[key]) || key; } // Resolve a localized label that may be either a plain string or // a {zh, en} object returned by the backend. function localizedLabel(label) { if (label && typeof label === 'object') { return label[currentLang] || label.en || label.zh || ''; } return label || ''; } function applyI18n() { document.querySelectorAll('[data-i18n]').forEach(el => { el.textContent = t(el.dataset.i18n); }); document.querySelectorAll('[data-i18n-html]').forEach(el => { el.innerHTML = t(el.dataset.i18nHtml); }); document.querySelectorAll('[data-i18n-placeholder]').forEach(el => { el.placeholder = t(el.dataset['i18nPlaceholder']); }); document.querySelectorAll('[data-tip-key]').forEach(el => { el.setAttribute('data-tooltip', t(el.dataset.tipKey)); }); installCfgTipPortal(); const langLabel = document.getElementById('lang-label'); if (langLabel) langLabel.textContent = currentLang === 'zh' ? '中文' : 'EN'; } // Single entry point for switching language. Updates the in-memory language, // persists the user choice locally, re-renders the UI, and binds the choice to // the backend `cow_lang` config so logs / agent replies / CLI follow suit. function setLanguage(lang) { const next = (lang === 'en') ? 'en' : 'zh'; if (next === currentLang) { // Still persist + sync in case storage/backend drifted from the UI. syncLanguageToBackend(next); return; } currentLang = next; localStorage.setItem('cow_lang', currentLang); applyI18n(); _applyInputTooltips(); // Re-render views whose DOM is built in JS (data-i18n alone does not // cover strings interpolated via t() into innerHTML). try { rerenderDynamicViews(); } catch (e) {} // Keep the language switch button and config selector visually in sync. try { updateLangControls(); } catch (e) {} syncLanguageToBackend(currentLang); } // Persist the language to the backend `cow_lang` config (best-effort; the UI // has already switched locally, so a network failure is non-blocking). function syncLanguageToBackend(lang) { try { fetch('/config', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ updates: { cow_lang: lang } }) }).catch(() => {}); } catch (e) {} } // Reflect the current language on both the top-right toggle and the config // selector (if present), so the two entry points stay synchronized. function updateLangControls() { const langLabel = document.getElementById('lang-label'); if (langLabel) langLabel.textContent = currentLang === 'zh' ? '中文' : 'EN'; // The config language picker is the custom .cfg-dropdown component. Only // sync it once it has been initialized (i.e. the config panel was opened). const sel = document.getElementById('cfg-lang-select'); if (sel && sel._ddValue !== undefined && sel._ddValue !== currentLang) { sel._ddValue = currentLang; const textEl = sel.querySelector('.cfg-dropdown-text'); if (textEl) textEl.textContent = currentLang === 'zh' ? '中文' : 'English'; sel.querySelectorAll('.cfg-dropdown-item').forEach(i => { i.classList.toggle('active', i.dataset.value === currentLang); }); } } function toggleLanguage() { setLanguage(currentLang === 'zh' ? 'en' : 'zh'); } // Refresh JS-rendered views after a language switch. Each branch uses the // lightweight in-memory re-render path (no extra network round-trips). function rerenderDynamicViews() { if (currentView === 'models' && typeof renderModelsView === 'function' && modelsState && (modelsState.providers || modelsState.capabilities)) { renderModelsView(); } } // Floating tooltip portal for [data-tip-key] elements. Tooltip nodes are // appended to so they aren't clipped by overflow:hidden ancestors // (e.g. the config panel's scroll container). let _cfgTipPortalEl = null; let _cfgTipPortalInstalled = false; function installCfgTipPortal() { if (_cfgTipPortalInstalled) return; _cfgTipPortalInstalled = true; const showTip = (target) => { const text = target.getAttribute('data-tooltip'); if (!text) return; if (!_cfgTipPortalEl) { _cfgTipPortalEl = document.createElement('div'); _cfgTipPortalEl.className = 'cfg-tip-floating'; document.body.appendChild(_cfgTipPortalEl); } _cfgTipPortalEl.textContent = text; const rect = target.getBoundingClientRect(); // Render once to measure, then position above the target, centered. _cfgTipPortalEl.style.left = '0px'; _cfgTipPortalEl.style.top = '0px'; _cfgTipPortalEl.classList.add('show'); const tipRect = _cfgTipPortalEl.getBoundingClientRect(); let left = rect.left + rect.width / 2 - tipRect.width / 2; // Clamp horizontally to the viewport with an 8px gutter. left = Math.max(8, Math.min(left, window.innerWidth - tipRect.width - 8)); const top = rect.top - tipRect.height - 6; _cfgTipPortalEl.style.left = left + 'px'; _cfgTipPortalEl.style.top = top + 'px'; }; const hideTip = () => { if (_cfgTipPortalEl) _cfgTipPortalEl.classList.remove('show'); }; document.addEventListener('mouseover', (e) => { const target = e.target.closest('[data-tip-key]'); if (target) showTip(target); }); document.addEventListener('mouseout', (e) => { const target = e.target.closest('[data-tip-key]'); if (target) hideTip(); }); // Hide on scroll/resize so the tooltip doesn't drift away from its anchor. window.addEventListener('scroll', hideTip, true); window.addEventListener('resize', hideTip); } // ===================================================================== // Theme // ===================================================================== let currentTheme = localStorage.getItem('cow_theme') || 'dark'; function applyTheme() { const root = document.documentElement; if (currentTheme === 'dark') { root.classList.add('dark'); document.getElementById('theme-icon').className = 'fas fa-sun'; document.getElementById('hljs-light').disabled = true; document.getElementById('hljs-dark').disabled = false; } else { root.classList.remove('dark'); document.getElementById('theme-icon').className = 'fas fa-moon'; document.getElementById('hljs-light').disabled = false; document.getElementById('hljs-dark').disabled = true; } } function toggleTheme() { currentTheme = currentTheme === 'dark' ? 'light' : 'dark'; localStorage.setItem('cow_theme', currentTheme); applyTheme(); } // ===================================================================== // Sidebar & Navigation // ===================================================================== const VIEW_META = { chat: { group: 'nav_chat', page: 'menu_chat' }, config: { group: 'nav_manage', page: 'menu_config' }, models: { group: 'nav_manage', page: 'menu_models' }, skills: { group: 'nav_manage', page: 'menu_skills' }, memory: { group: 'nav_manage', page: 'menu_memory' }, knowledge:{ group: 'nav_manage', page: 'menu_knowledge' }, channels: { group: 'nav_manage', page: 'menu_channels' }, tasks: { group: 'nav_manage', page: 'menu_tasks' }, logs: { group: 'nav_monitor', page: 'menu_logs' }, }; let currentView = 'chat'; function navigateTo(viewId) { if (!VIEW_META[viewId]) return; document.querySelectorAll('.view').forEach(v => v.classList.remove('active')); const target = document.getElementById('view-' + viewId); if (target) target.classList.add('active'); document.querySelectorAll('.sidebar-item').forEach(item => { item.classList.toggle('active', item.dataset.view === viewId); }); const meta = VIEW_META[viewId]; document.getElementById('breadcrumb-group').textContent = t(meta.group); document.getElementById('breadcrumb-group').dataset.i18n = meta.group; document.getElementById('breadcrumb-page').textContent = t(meta.page); document.getElementById('breadcrumb-page').dataset.i18n = meta.page; currentView = viewId; if (window.innerWidth < 1024) closeSidebar(); } function toggleSidebar() { const sidebar = document.getElementById('sidebar'); const overlay = document.getElementById('sidebar-overlay'); const isOpen = !sidebar.classList.contains('-translate-x-full'); if (isOpen) { closeSidebar(); } else { sidebar.classList.remove('-translate-x-full'); overlay.classList.remove('hidden'); } } function closeSidebar() { document.getElementById('sidebar').classList.add('-translate-x-full'); document.getElementById('sidebar-overlay').classList.add('hidden'); } document.querySelectorAll('.menu-group > button').forEach(btn => { btn.addEventListener('click', () => { btn.parentElement.classList.toggle('open'); }); }); document.querySelectorAll('.sidebar-item').forEach(item => { item.addEventListener('click', () => navigateTo(item.dataset.view)); }); window.addEventListener('resize', () => { if (window.innerWidth >= 1024) { document.getElementById('sidebar').classList.remove('-translate-x-full'); document.getElementById('sidebar-overlay').classList.add('hidden'); } else { if (!document.getElementById('sidebar').classList.contains('-translate-x-full')) { closeSidebar(); } } }); // ===================================================================== // Markdown Renderer // ===================================================================== const FALLBACK_HLJS = { getLanguage() { return false; }, highlight(str) { return { value: escapeHtml(str) }; }, highlightAuto(str) { return { value: escapeHtml(str) }; }, highlightElement() {}, }; function getHljs() { return window.hljs || FALLBACK_HLJS; } function createMd() { const hljsLib = getHljs(); const mdFactory = window.markdownit; if (typeof mdFactory !== 'function') { return { render(text) { return `

${escapeHtml(text || '')}

`; } }; } const md = mdFactory({ html: false, breaks: true, linkify: true, typographer: true, highlight: function(str, lang) { if (lang && hljsLib.getLanguage(lang)) { try { return hljsLib.highlight(str, { language: lang }).value; } catch (_) {} } return hljsLib.highlightAuto(str).value; } }); const defaultLinkOpen = md.renderer.rules.link_open || function(tokens, idx, options, env, self) { return self.renderToken(tokens, idx, options); }; md.renderer.rules.link_open = function(tokens, idx, options, env, self) { tokens[idx].attrPush(['target', '_blank']); tokens[idx].attrPush(['rel', 'noopener noreferrer']); return defaultLinkOpen(tokens, idx, options, env, self); }; return md; } const md = createMd(); const VIDEO_EXT_RE = /\.(?:mp4|webm|mov|avi|mkv)$/i; // tested against URL without query string const IMAGE_EXT_RE = /\.(?:jpg|jpeg|png|gif|webp|bmp|svg)$/i; // tested against URL without query string function _toWebUrl(url) { if (/^\/[A-Za-z]/.test(url) && !url.startsWith('/api/')) { return '/api/file?path=' + encodeURIComponent(url); } if (/^file:\/\/\//i.test(url)) { return '/api/file?path=' + encodeURIComponent(url.replace(/^file:\/\/\//i, '/')); } return url; } function _buildVideoHtml(url) { const webUrl = _toWebUrl(url); const fileName = url.split('/').pop().split('?')[0]; return `
` + `` + `` + ` ${escapeHtml(fileName)}
`; } function _openImageLightbox(src) { let overlay = document.getElementById('cow-lightbox'); if (!overlay) { overlay = document.createElement('div'); overlay.id = 'cow-lightbox'; overlay.style.cssText = 'position:fixed;inset:0;z-index:9999;background:rgba(0,0,0,0.85);display:flex;align-items:center;justify-content:center;cursor:zoom-out;opacity:0;transition:opacity .2s'; overlay.onclick = () => { overlay.style.opacity = '0'; setTimeout(() => overlay.style.display = 'none', 200); }; const img = document.createElement('img'); img.id = 'cow-lightbox-img'; img.style.cssText = 'max-width:92vw;max-height:92vh;border-radius:8px;box-shadow:0 4px 24px rgba(0,0,0,0.5);object-fit:contain;'; img.onclick = (e) => e.stopPropagation(); overlay.appendChild(img); document.body.appendChild(overlay); } overlay.querySelector('#cow-lightbox-img').src = src; overlay.style.display = 'flex'; requestAnimationFrame(() => overlay.style.opacity = '1'); } function _buildImageHtml(url) { const webUrl = _toWebUrl(url); const safeUrl = webUrl.replace(/"/g, '"'); return `
` + `image` + `
`; } function injectVideoPlayers(html) { // Step 1: replace markdown-it anchor tags whose href points to a video file. const step1 = html.replace( /]*>[^<]*<\/a>/gi, (match, url) => VIDEO_EXT_RE.test(url.split('?')[0]) ? _buildVideoHtml(url) : match ); // Step 2: replace any remaining bare video URLs in text nodes (not inside HTML tags). // Split on HTML tags to avoid touching src/href attributes already in markup. return step1.split(/(<[^>]+>)/).map((chunk, idx) => { // Even indices are text nodes; odd indices are HTML tags — leave them untouched. if (idx % 2 !== 0) return chunk; return chunk.replace(/https?:\/\/\S+/gi, (url) => { const bare = url.replace(/[),.\s]+$/, ''); // strip trailing punctuation return VIDEO_EXT_RE.test(bare.split('?')[0]) ? _buildVideoHtml(bare) : url; }); }).join(''); } // Convert image URLs into inline previews. Mirrors injectVideoPlayers but for images. // Handles three cases produced by markdown-it: // 1. ... (bare URL or autolink that linkify turned into an anchor) // 2. (markdown image syntax) — leave as-is, but normalize style // 3. raw URL still present in a text node — only as a safety net function injectImagePreviews(html) { // Step 1: anchor whose href points to an image file -> replace with preview. const step1 = html.replace( /]*>[^<]*<\/a>/gi, (match, url) => IMAGE_EXT_RE.test(url.split('?')[0]) ? _buildImageHtml(url) : match ); // Step 2: bare image URLs left in text nodes (rare — markdown-it's linkify usually catches them). return step1.split(/(<[^>]+>)/).map((chunk, idx) => { if (idx % 2 !== 0) return chunk; return chunk.replace(/https?:\/\/\S+/gi, (url) => { const bare = url.replace(/[),.\s]+$/, ''); return IMAGE_EXT_RE.test(bare.split('?')[0]) ? _buildImageHtml(bare) : url; }); }).join(''); } function _rewriteLocalImgSrc(html) { return html.replace(/]*?)src="([^"]+)"([^>]*?)>/gi, (match, pre, src, post) => { const webSrc = _toWebUrl(src); const safeSrc = webSrc.replace(/"/g, '"'); const hasClick = /onclick/i.test(pre + post); const clickAttr = hasClick ? '' : ` onclick="_openImageLightbox(this.src)" style="cursor:zoom-in;"`; return ``; }); } function renderMarkdown(text) { try { let html = md.render(text); html = _rewriteLocalImgSrc(html); // Order matters: video first (more specific), then image. return injectImagePreviews(injectVideoPlayers(html)); } catch (e) { return text.replace(/\n/g, '
'); } } // ===================================================================== // Chat Module // ===================================================================== let isPolling = false; let pollGeneration = 0; // incremented on each restart to cancel stale poll loops let loadingContainers = {}; let activeStreams = {}; // request_id -> EventSource let isComposing = false; let appConfig = { use_agent: false, title: 'CowAgent', subtitle: '', providers: {}, api_bases: {} }; const SESSION_ID_KEY = 'cow_session_id'; function generateSessionId() { return 'session_' + ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); } // Restore session_id from localStorage so conversation history survives page refresh. // A new id is only generated when the user explicitly starts a new chat. function loadOrCreateSessionId() { const stored = localStorage.getItem(SESSION_ID_KEY); if (stored) return stored; const fresh = generateSessionId(); localStorage.setItem(SESSION_ID_KEY, fresh); return fresh; } let sessionId = loadOrCreateSessionId(); // ---- Conversation history state ---- let historyPage = 0; // last page fetched (0 = nothing fetched yet) let historyHasMore = false; let historyLoading = false; fetch('/config').then(r => r.json()).then(data => { if (data.status === 'success') { appConfig = data; const title = data.title || 'CowAgent'; document.getElementById('welcome-title').textContent = title; initConfigView(data); } loadHistory(1); }).catch(() => { loadHistory(1); }); // Start polling immediately so scheduler/push messages are received at any time startPolling(); const chatInput = document.getElementById('chat-input'); const sendBtn = document.getElementById('send-btn'); const messagesDiv = document.getElementById('chat-messages'); const fileInput = document.getElementById('file-input'); const folderInput = document.getElementById('folder-input'); const attachBtn = document.getElementById('attach-btn'); const attachMenu = document.getElementById('attach-menu'); const attachFolderOption = document.getElementById('attach-folder-option'); const supportsDirectoryUpload = !!folderInput && 'webkitdirectory' in folderInput; if (!supportsDirectoryUpload && attachFolderOption) { attachFolderOption.classList.add('hidden'); } // ---------------- Mic button: in-page voice input via the configured ASR provider ---------------- (function setupMicButton() { const micBtn = document.getElementById('mic-btn'); if (!micBtn) return; if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia || typeof window.MediaRecorder === 'undefined') { micBtn.style.display = 'none'; return; } let mediaRecorder = null; let stream = null; let chunks = []; let recording = false; const setIdle = () => { recording = false; micBtn.classList.remove('text-red-500', 'animate-pulse'); micBtn.classList.add('text-slate-400'); micBtn.querySelector('i').className = 'fas fa-microphone text-sm'; micBtn.title = t('mic_idle_title'); }; const setRecording = () => { recording = true; micBtn.classList.remove('text-slate-400'); micBtn.classList.add('text-red-500', 'animate-pulse'); micBtn.querySelector('i').className = 'fas fa-stop text-sm'; micBtn.title = t('mic_recording_title'); }; const setBusy = () => { micBtn.classList.remove('text-red-500', 'animate-pulse', 'text-slate-400'); micBtn.classList.add('text-primary-500'); micBtn.querySelector('i').className = 'fas fa-spinner fa-spin text-sm'; micBtn.title = t('mic_busy_title'); }; const pickMimeType = () => { const candidates = [ 'audio/webm;codecs=opus', 'audio/webm', 'audio/ogg;codecs=opus', 'audio/mp4', ]; for (const m of candidates) { if (window.MediaRecorder.isTypeSupported && MediaRecorder.isTypeSupported(m)) { return m; } } return ''; }; const stopStream = () => { if (stream) { stream.getTracks().forEach(t => t.stop()); stream = null; } }; let _micTipTimer = null; const flashError = (msg) => { console.warn('[mic]', msg); // Pop a small bubble above the mic so the user actually notices it. // The mic lives inside a relatively-positioned wrapper around the // textarea (see chat.html), so we hang the tip off that wrapper. const wrapper = micBtn.parentElement; if (!wrapper) return; let tip = wrapper.querySelector('.mic-tip'); if (!tip) { tip = document.createElement('div'); tip.className = 'mic-tip absolute right-1 bottom-full mb-2 px-2 py-1 rounded-md ' + 'text-xs text-white bg-slate-800/90 dark:bg-slate-700/90 shadow-md ' + 'pointer-events-none whitespace-nowrap z-10'; wrapper.appendChild(tip); } tip.textContent = msg; tip.style.opacity = '1'; if (_micTipTimer) clearTimeout(_micTipTimer); _micTipTimer = setTimeout(() => { tip.style.opacity = '0'; tip.style.transition = 'opacity 200ms'; setTimeout(() => tip.remove(), 250); }, 2000); }; const upload = async (blob, ext) => { setBusy(); const fd = new FormData(); fd.append('file', blob, `recording.${ext}`); try { const resp = await fetch('/api/voice/asr', { method: 'POST', body: fd }); const data = await resp.json(); if (data.status === 'success' && data.text) { // Voice-message UX: drop the recording into the conversation // as a playable bubble with the caption underneath, then // dispatch the recognised text through the regular send path. sendVoiceMessage(data.text, data.audio_url); } else { flashError(data.message || t('mic_error')); } } catch (e) { flashError(t('mic_error') + ': ' + e.message); } finally { setIdle(); } }; const start = async () => { try { stream = await navigator.mediaDevices.getUserMedia({ audio: true }); } catch (e) { flashError(t('mic_permission_denied')); return; } chunks = []; const mimeType = pickMimeType(); try { mediaRecorder = mimeType ? new MediaRecorder(stream, { mimeType }) : new MediaRecorder(stream); } catch (e) { stopStream(); flashError(t('mic_error') + ': ' + e.message); return; } mediaRecorder.ondataavailable = (ev) => { if (ev.data && ev.data.size > 0) chunks.push(ev.data); }; mediaRecorder.onstop = () => { stopStream(); const blob = new Blob(chunks, { type: mediaRecorder.mimeType || 'audio/webm' }); // Map mime -> extension so the server picks the right file suffix. const mt = (mediaRecorder.mimeType || 'audio/webm').split(';')[0]; const extMap = { 'audio/webm': 'webm', 'audio/ogg': 'ogg', 'audio/mp4': 'm4a', 'audio/mpeg': 'mp3', }; const ext = extMap[mt] || 'webm'; // 256 bytes ~ container header only, no actual audio. Anything // below that we treat as "tapped by mistake". if (blob.size < 256) { setIdle(); flashError(t('mic_too_short')); return; } upload(blob, ext); }; // timeslice=250ms: force the recorder to flush a chunk every 250ms. // Without it some browsers wait for stop() before producing any data, // which loses the audio on very short taps. mediaRecorder.start(250); recordStartedAt = Date.now(); setRecording(); }; let recordStartedAt = 0; const stopWithMinDuration = () => { const elapsed = Date.now() - recordStartedAt; const minMs = 350; if (elapsed < minMs) { // Give the recorder a moment to capture at least one chunk // before we tell it to stop. setTimeout(() => stop(), minMs - elapsed); } else { stop(); } }; const stop = () => { if (mediaRecorder && mediaRecorder.state !== 'inactive') { mediaRecorder.stop(); } }; micBtn.addEventListener('click', () => { if (recording) { stopWithMinDuration(); } else { start(); } }); setIdle(); })(); // Smart auto-scroll: pause when user scrolls up, resume when near bottom let _autoScrollEnabled = true; const _SCROLL_THRESHOLD = 80; // px from bottom to re-enable auto-scroll messagesDiv.addEventListener('scroll', () => { const distFromBottom = messagesDiv.scrollHeight - messagesDiv.scrollTop - messagesDiv.clientHeight; _autoScrollEnabled = distFromBottom <= _SCROLL_THRESHOLD; _updateScrollToBottomBtn(); }); // Intercept internal navigation links in chat messages messagesDiv.addEventListener('click', (e) => { const copyBtn = e.target.closest('.copy-msg-btn'); if (copyBtn) { e.preventDefault(); const msgRoot = copyBtn.closest('.flex.gap-3'); const answerEl = msgRoot && msgRoot.querySelector('.answer-content'); const rawMd = answerEl && answerEl.dataset.rawMd; if (rawMd) { navigator.clipboard.writeText(rawMd).then(() => { const icon = copyBtn.querySelector('i'); if (icon) { icon.className = 'fas fa-check'; setTimeout(() => { icon.className = 'fas fa-copy'; }, 1500); } }); } return; } const a = e.target.closest('a'); if (!a) return; const href = a.getAttribute('href') || ''; if (href === '/memory/dreams') { e.preventDefault(); navigateTo('memory'); setTimeout(() => switchMemoryTab('dreams'), 50); } else if (href === '/memory/MEMORY.md') { e.preventDefault(); navigateTo('memory'); setTimeout(() => { switchMemoryTab('files'); openMemoryFile('MEMORY.md', 'memory'); }, 50); } }); const attachmentPreview = document.getElementById('attachment-preview'); // Pending attachments: [{file_path, file_name, file_type, preview_url}] // Items with _uploading=true are still in flight. let pendingAttachments = []; let uploadingCount = 0; // Input history (like terminal arrow-key recall) const inputHistory = []; let historyIdx = -1; let historySavedDraft = ''; // While an SSE stream is in flight, the send button morphs into a cancel // button. Only one in-flight request is supported at a time. let activeRequestId = null; let sendBtnMode = 'send'; // 'send' | 'cancel' function setSendBtnCancelMode(requestId) { activeRequestId = requestId; sendBtnMode = 'cancel'; sendBtn.disabled = false; sendBtn.classList.add('send-btn-cancel'); sendBtn.title = (currentLang === 'zh' ? '中止' : 'Cancel'); sendBtn.innerHTML = ''; } function resetSendBtnSendMode() { activeRequestId = null; sendBtnMode = 'send'; sendBtn.classList.remove('send-btn-cancel'); sendBtn.title = ''; sendBtn.innerHTML = ''; updateSendBtnState(); } function requestCancel() { const reqId = activeRequestId; if (!reqId) return; fetch('/cancel', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ request_id: reqId, session_id: sessionId, lang: currentLang }), }).catch(err => { console.warn('[cancel] request failed', err); }); // Optimistic UI lock so the click visibly registers before the SSE // "cancelled" event arrives. sendBtn.disabled = true; sendBtn.title = (currentLang === 'zh' ? '已中止' : 'Cancelled'); } // Button click is the only path to Cancel. Pressing Enter still calls // sendMessage() so users can submit "/cancel" as a regular slash command. sendBtn.addEventListener('click', () => { if (sendBtnMode === 'cancel') { requestCancel(); } else { sendMessage(); } }); function updateSendBtnState() { if (sendBtnMode === 'cancel') { // Don't downgrade a Cancel button on input edits. return; } sendBtn.disabled = uploadingCount > 0 || (!chatInput.value.trim() && pendingAttachments.length === 0); } function renderAttachmentPreview() { if (pendingAttachments.length === 0) { attachmentPreview.classList.add('hidden'); attachmentPreview.innerHTML = ''; updateSendBtnState(); return; } attachmentPreview.classList.remove('hidden'); attachmentPreview.innerHTML = pendingAttachments.map((att, idx) => { if (att._uploading) { const suffix = att.file_type === 'directory' && att.file_count ? ` (${att.file_count})` : ''; return `
${escapeHtml(att.file_name)}${suffix}
`; } if (att.file_type === 'image') { return `
${escapeHtml(att.file_name)}
`; } const icon = att.file_type === 'video' ? 'fa-film' : (att.file_type === 'directory' ? 'fa-folder-tree' : 'fa-file-alt'); const suffix = att.file_type === 'directory' && att.file_count ? ` (${att.file_count})` : ''; return `
${escapeHtml(att.file_name)}${suffix}
`; }).join(''); updateSendBtnState(); } function removeAttachment(idx) { if (pendingAttachments[idx]?._uploading) return; pendingAttachments.splice(idx, 1); renderAttachmentPreview(); } function isAttachMenuVisible() { return attachMenu && !attachMenu.classList.contains('hidden'); } function hideAttachMenu() { if (attachMenu) attachMenu.classList.add('hidden'); } function toggleAttachMenu(event) { if (!attachMenu) return; if (event) { event.preventDefault(); event.stopPropagation(); } attachMenu.classList.toggle('hidden'); } function triggerFileUpload() { hideAttachMenu(); fileInput?.click(); } function triggerFolderUpload() { if (!supportsDirectoryUpload) return; hideAttachMenu(); folderInput?.click(); } async function handleFileSelect(files) { if (!files || files.length === 0) return; const tasks = []; for (const file of files) { const placeholder = { file_name: file.name, file_type: 'file', _uploading: true }; pendingAttachments.push(placeholder); uploadingCount++; renderAttachmentPreview(); tasks.push((async () => { const formData = new FormData(); formData.append('file', file); formData.append('session_id', sessionId); try { const resp = await fetch('/upload', { method: 'POST', body: formData }); const data = await resp.json(); if (data.status === 'success') { placeholder.file_path = data.file_path; placeholder.file_name = data.file_name; placeholder.file_type = data.file_type; placeholder.preview_url = data.preview_url; delete placeholder._uploading; } else { const i = pendingAttachments.indexOf(placeholder); if (i !== -1) pendingAttachments.splice(i, 1); } } catch (e) { console.error('Upload failed:', e); const i = pendingAttachments.indexOf(placeholder); if (i !== -1) pendingAttachments.splice(i, 1); } uploadingCount--; renderAttachmentPreview(); })()); } await Promise.all(tasks); } function _makeUploadId() { return `dir_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`; } function _groupDirectoryFiles(files) { const groups = new Map(); for (const file of Array.from(files || [])) { const relPath = file.webkitRelativePath || file.name; const parts = relPath.split('/').filter(Boolean); const rootName = parts[0] || file.name; if (!groups.has(rootName)) groups.set(rootName, []); groups.get(rootName).push({ file, relPath }); } return groups; } async function handleFolderSelect(files) { if (!files || files.length === 0) return; const groups = _groupDirectoryFiles(files); const groupTasks = []; for (const [rootName, entries] of groups.entries()) { const placeholder = { file_name: rootName, file_type: 'directory', file_count: entries.length, _uploading: true, }; pendingAttachments.push(placeholder); uploadingCount++; renderAttachmentPreview(); const uploadId = _makeUploadId(); groupTasks.push((async () => { try { const formData = new FormData(); formData.append('session_id', sessionId); formData.append('upload_id', uploadId); for (const { file, relPath } of entries) { formData.append('files', file); formData.append('relative_paths', relPath); } const resp = await fetch('/upload', { method: 'POST', body: formData }); const data = await resp.json(); if (data.status !== 'success') { throw new Error(data.message || 'Upload failed'); } if (!data.root_path) { throw new Error('Directory root path missing'); } placeholder.file_path = data.root_path; placeholder.file_name = data.root_name || rootName; delete placeholder._uploading; } catch (e) { console.error('Directory upload failed:', e); const i = pendingAttachments.indexOf(placeholder); if (i !== -1) pendingAttachments.splice(i, 1); } finally { uploadingCount--; } renderAttachmentPreview(); })()); } await Promise.all(groupTasks); } fileInput.addEventListener('change', function() { handleFileSelect(this.files); this.value = ''; }); folderInput.addEventListener('change', function() { handleFolderSelect(this.files); this.value = ''; }); document.addEventListener('click', (e) => { if (!isAttachMenuVisible()) return; if (attachMenu.contains(e.target) || attachBtn.contains(e.target)) return; hideAttachMenu(); }); // Drag-and-drop support on chat input area const chatInputArea = chatInput.closest('.flex-shrink-0'); chatInputArea.addEventListener('dragover', (e) => { e.preventDefault(); e.stopPropagation(); chatInputArea.classList.add('drag-over'); }); chatInputArea.addEventListener('dragleave', (e) => { e.preventDefault(); e.stopPropagation(); chatInputArea.classList.remove('drag-over'); }); chatInputArea.addEventListener('drop', (e) => { e.preventDefault(); e.stopPropagation(); chatInputArea.classList.remove('drag-over'); if (e.dataTransfer.files.length) handleFileSelect(e.dataTransfer.files); }); // Paste image support chatInput.addEventListener('paste', (e) => { const items = e.clipboardData?.items; if (!items) return; const files = []; for (const item of items) { if (item.kind === 'file') { files.push(item.getAsFile()); } } if (files.length) { e.preventDefault(); handleFileSelect(files); } }); chatInput.addEventListener('compositionstart', () => { isComposing = true; }); chatInput.addEventListener('compositionend', () => { setTimeout(() => { isComposing = false; }, 100); }); // ── Slash Command Menu ─────────────────────────────────────── // desc holds an i18n key, resolved via t() at render time so the menu follows // the current UI language. const SLASH_COMMANDS = [ { cmd: '/help', desc: 'slash_help' }, { cmd: '/status', desc: 'slash_status' }, { cmd: '/context', desc: 'slash_context' }, { cmd: '/context clear', desc: 'slash_context_clear' }, { cmd: '/skill list', desc: 'slash_skill_list' }, { cmd: '/skill list --remote', desc: 'slash_skill_list_remote' }, { cmd: '/skill search ', desc: 'slash_skill_search' }, { cmd: '/skill install ', desc: 'slash_skill_install' }, { cmd: '/skill uninstall ', desc: 'slash_skill_uninstall' }, { cmd: '/skill info ', desc: 'slash_skill_info' }, { cmd: '/skill enable ', desc: 'slash_skill_enable' }, { cmd: '/skill disable ', desc: 'slash_skill_disable' }, { cmd: '/memory dream ', desc: 'slash_memory_dream' }, { cmd: '/knowledge', desc: 'slash_knowledge' }, { cmd: '/knowledge list', desc: 'slash_knowledge_list' }, { cmd: '/knowledge on', desc: 'slash_knowledge_on' }, { cmd: '/knowledge off', desc: 'slash_knowledge_off' }, { cmd: '/config', desc: 'slash_config' }, { cmd: '/cancel', desc: 'slash_cancel' }, { cmd: '/logs', desc: 'slash_logs' }, { cmd: '/version', desc: 'slash_version' }, ]; const slashMenu = document.getElementById('slash-menu'); let slashActiveIdx = 0; let slashFiltered = []; let slashJustSelected = false; let slashLastFilter = ''; let slashLastMouseX = -1; let slashLastMouseY = -1; function showSlashMenu(filter) { const q = filter.toLowerCase(); if (q === slashLastFilter && !slashMenu.classList.contains('hidden')) return; slashLastFilter = q; const newFiltered = SLASH_COMMANDS.filter(c => c.cmd.toLowerCase().startsWith(q)); if (newFiltered.length === 0) { hideSlashMenu(); return; } const changed = newFiltered.length !== slashFiltered.length || newFiltered.some((c, i) => c.cmd !== slashFiltered[i]?.cmd); slashFiltered = newFiltered; if (changed) slashActiveIdx = 0; slashActiveIdx = Math.min(slashActiveIdx, slashFiltered.length - 1); slashNavByKeyboard = true; renderSlashItems(); slashMenu.classList.remove('hidden'); } function hideSlashMenu() { slashMenu.classList.add('hidden'); slashMenu.innerHTML = ''; slashFiltered = []; slashActiveIdx = -1; slashLastFilter = ''; slashNavByKeyboard = false; slashLastMouseX = -1; slashLastMouseY = -1; } function isSlashMenuVisible() { return !slashMenu.classList.contains('hidden') && slashFiltered.length > 0; } function renderSlashItems() { slashMenu.innerHTML = '
Commands
' + slashFiltered.map((c, i) => `
` + `${escapeHtml(c.cmd)}` + `${escapeHtml(t(c.desc))}
` ).join(''); const activeEl = slashMenu.querySelector('.slash-menu-item.active'); if (activeEl) activeEl.scrollIntoView({ block: 'nearest' }); } // Delegated events on the persistent slashMenu container (not destroyed by innerHTML) // Use coordinate comparison to distinguish real mouse movement from DOM-rebuild phantom events. slashMenu.addEventListener('mousemove', (e) => { if (e.clientX === slashLastMouseX && e.clientY === slashLastMouseY) return; slashLastMouseX = e.clientX; slashLastMouseY = e.clientY; if (!slashNavByKeyboard) return; slashNavByKeyboard = false; const item = e.target.closest('.slash-menu-item'); if (!item) return; const idx = parseInt(item.dataset.idx); if (idx === slashActiveIdx) return; slashActiveIdx = idx; slashMenu.querySelectorAll('.slash-menu-item').forEach(el => { el.classList.toggle('active', parseInt(el.dataset.idx) === idx); }); }); slashMenu.addEventListener('mouseover', (e) => { if (slashNavByKeyboard) return; const item = e.target.closest('.slash-menu-item'); if (!item) return; const idx = parseInt(item.dataset.idx); if (idx === slashActiveIdx) return; slashActiveIdx = idx; slashMenu.querySelectorAll('.slash-menu-item').forEach(el => { el.classList.toggle('active', parseInt(el.dataset.idx) === idx); }); }); slashMenu.addEventListener('mousedown', (e) => { const item = e.target.closest('.slash-menu-item'); if (!item) return; e.preventDefault(); selectSlashCommand(parseInt(item.dataset.idx)); }); function selectSlashCommand(idx) { if (idx < 0 || idx >= slashFiltered.length) return; const chosen = slashFiltered[idx].cmd; slashJustSelected = true; chatInput.value = chosen; chatInput.dispatchEvent(new Event('input')); hideSlashMenu(); chatInput.focus(); chatInput.selectionStart = chatInput.selectionEnd = chosen.length; } chatInput.addEventListener('input', function() { this.style.height = '42px'; const scrollH = this.scrollHeight; const newH = Math.min(scrollH, 180); this.style.height = newH + 'px'; this.style.overflowY = scrollH > 180 ? 'auto' : 'hidden'; updateSendBtnState(); const val = this.value; if (slashJustSelected) { slashJustSelected = false; } else if (val.startsWith('/')) { showSlashMenu(val); } else { hideSlashMenu(); } }); chatInput.addEventListener('keydown', function(e) { if (e.keyCode === 229 || e.isComposing || isComposing) return; if (e.key === 'Escape' && isAttachMenuVisible()) { hideAttachMenu(); return; } if (isSlashMenuVisible()) { if (e.key === 'ArrowDown') { e.preventDefault(); slashNavByKeyboard = true; slashActiveIdx = Math.min(slashActiveIdx + 1, slashFiltered.length - 1); renderSlashItems(); return; } if (e.key === 'ArrowUp') { e.preventDefault(); slashNavByKeyboard = true; slashActiveIdx = Math.max(slashActiveIdx - 1, 0); renderSlashItems(); return; } if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey) { e.preventDefault(); selectSlashCommand(slashActiveIdx); return; } if (e.key === 'Escape') { e.preventDefault(); hideSlashMenu(); return; } if (e.key === 'Tab') { e.preventDefault(); selectSlashCommand(slashActiveIdx); return; } } // Arrow-key history recall (only when input is empty or already browsing history) if (e.key === 'ArrowUp' && inputHistory.length > 0 && !isSlashMenuVisible()) { const curVal = this.value.trim(); const isSingleLine = !this.value.includes('\n'); if (isSingleLine && (curVal === '' || historyIdx >= 0)) { e.preventDefault(); if (historyIdx < 0) { historySavedDraft = this.value; historyIdx = inputHistory.length - 1; } else if (historyIdx > 0) { historyIdx--; } this.value = inputHistory[historyIdx]; slashJustSelected = true; this.dispatchEvent(new Event('input')); hideSlashMenu(); this.selectionStart = this.selectionEnd = this.value.length; return; } } if (e.key === 'ArrowDown' && historyIdx >= 0 && !isSlashMenuVisible()) { const isSingleLine = !this.value.includes('\n'); if (isSingleLine) { e.preventDefault(); if (historyIdx < inputHistory.length - 1) { historyIdx++; this.value = inputHistory[historyIdx]; } else { historyIdx = -1; this.value = historySavedDraft; historySavedDraft = ''; } slashJustSelected = true; this.dispatchEvent(new Event('input')); hideSlashMenu(); this.selectionStart = this.selectionEnd = this.value.length; return; } } if ((e.ctrlKey || e.shiftKey) && e.key === 'Enter') { const start = this.selectionStart; const end = this.selectionEnd; this.value = this.value.substring(0, start) + '\n' + this.value.substring(end); this.selectionStart = this.selectionEnd = start + 1; this.dispatchEvent(new Event('input')); e.preventDefault(); } else if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey) { sendMessage(); e.preventDefault(); } }); chatInput.addEventListener('blur', () => { setTimeout(hideSlashMenu, 150); }); document.querySelectorAll('.example-card').forEach(card => { card.addEventListener('click', () => { // data-send overrides the visible text (e.g. show "查看全部命令" but send "/help") const sendText = card.dataset.send; if (sendText) { chatInput.value = sendText; chatInput.dispatchEvent(new Event('input')); chatInput.focus(); return; } const textEl = card.querySelector('[data-i18n*="text"]'); if (textEl) { chatInput.value = textEl.textContent; chatInput.dispatchEvent(new Event('input')); chatInput.focus(); } }); }); // Voice-message variant of sendMessage(): renders a playable audio bubble // with the ASR caption, then dispatches the recognised text to /message // through the same SSE/loading flow as a typed message. function sendVoiceMessage(text, audioUrl) { text = (text || '').trim(); if (!text) return; inputHistory.push(text); historyIdx = -1; historySavedDraft = ''; const ws = document.getElementById('welcome-screen'); const isFirstMessage = !!ws; if (ws) ws.remove(); const titleInfo = isFirstMessage ? { sid: sessionId, userMsg: text } : null; const timestamp = new Date(); addUserVoiceMessage(audioUrl, text, timestamp); const loadingEl = addLoadingIndicator(); const body = { session_id: sessionId, message: text, stream: true, timestamp: timestamp.toISOString(), is_voice: true, lang: currentLang, }; const MAX_RETRIES = 2; const RETRY_DELAY_MS = 1000; function postWithRetry(attempt) { fetch('/message', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) .then(r => r.json()) .then(data => { if (data.status === 'success') { if (data.inline_reply) { // Synchronous fast-path reply (e.g. /cancel); skip SSE. loadingEl.remove(); addBotMessage(data.inline_reply, new Date()); } else if (data.stream) { setSendBtnCancelMode(data.request_id); startSSE(data.request_id, loadingEl, timestamp, titleInfo); } else { loadingContainers[data.request_id] = loadingEl; } } else { loadingEl.remove(); addBotMessage(t('error_send'), new Date()); resetSendBtnSendMode(); } }) .catch(err => { if (attempt < MAX_RETRIES) { setTimeout(() => postWithRetry(attempt + 1), RETRY_DELAY_MS * (attempt + 1)); return; } loadingEl.remove(); addBotMessage(t('error_send'), new Date()); }); } postWithRetry(0); } function addUserVoiceMessage(audioUrl, caption, timestamp) { const el = document.createElement('div'); el.className = 'flex justify-end px-4 sm:px-6 py-3'; // Voice-message bubble: compact voice pill on top, ASR caption beneath. // The bubble keeps the same primary tint as a normal user message so // it visually slots into the conversation flow. el.innerHTML = `
${caption ? `
${escapeHtml(caption)}
` : ''}
${formatTime(timestamp)}
`; el.querySelector('.user-voice-slot').appendChild(renderVoicePill(audioUrl)); messagesDiv.appendChild(el); _autoScrollEnabled = true; scrollChatToBottom(true); } function sendMessage() { // Do NOT branch on sendBtnMode here: Enter should always send (so // typing "/cancel" submits normally). Cancel is wired only to the // send button's pointer click — see send-btn listener above. const text = chatInput.value.trim(); if (!text && pendingAttachments.length === 0) return; if (text) { inputHistory.push(text); historyIdx = -1; historySavedDraft = ''; } const ws = document.getElementById('welcome-screen'); const isFirstMessage = !!ws; if (ws) ws.remove(); const titleInfo = (isFirstMessage && text) ? { sid: sessionId, userMsg: text } : null; const timestamp = new Date(); const attachments = [...pendingAttachments]; addUserMessage(text, timestamp, attachments); const loadingEl = addLoadingIndicator(); chatInput.value = ''; chatInput.style.height = '42px'; chatInput.style.overflowY = 'hidden'; pendingAttachments = []; renderAttachmentPreview(); sendBtn.disabled = true; const body = { session_id: sessionId, message: text, stream: true, timestamp: timestamp.toISOString(), lang: currentLang }; if (attachments.length > 0) { body.attachments = attachments.map(a => ({ file_path: a.file_path, file_name: a.file_name, file_type: a.file_type, file_count: a.file_count, })); } const MAX_RETRIES = 2; const RETRY_DELAY_MS = 1000; function postWithRetry(attempt) { fetch('/message', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) .then(r => r.json()) .then(data => { if (data.status === 'success') { if (data.inline_reply) { // Channel handled synchronously (e.g. /cancel fast-path); // render as a bot bubble and skip SSE entirely. loadingEl.remove(); addBotMessage(data.inline_reply, new Date()); } else if (data.stream) { setSendBtnCancelMode(data.request_id); startSSE(data.request_id, loadingEl, timestamp, titleInfo); } else { loadingContainers[data.request_id] = loadingEl; } } else { loadingEl.remove(); addBotMessage(t('error_send'), new Date()); resetSendBtnSendMode(); } }) .catch(err => { if (err.name === 'AbortError') { loadingEl.remove(); addBotMessage(t('error_timeout'), new Date()); resetSendBtnSendMode(); return; } if (attempt < MAX_RETRIES) { console.warn(`[sendMessage] attempt ${attempt + 1} failed, retrying...`, err); setTimeout(() => postWithRetry(attempt + 1), RETRY_DELAY_MS * (attempt + 1)); return; } loadingEl.remove(); addBotMessage(t('error_send'), new Date()); resetSendBtnSendMode(); }); } postWithRetry(0); } function startSSE(requestId, loadingEl, timestamp, titleInfo) { let botEl = null; let stepsEl = null; // .agent-steps (thinking summaries + tool indicators) let contentEl = null; // .answer-content (final streaming answer) let mediaEl = null; // .media-content (images & file attachments) let accumulatedText = ''; let currentToolEl = null; let currentReasoningEl = null; // live reasoning bubble let reasoningText = ''; let reasoningStartTime = 0; let done = false; const MAX_RECONNECTS = 10; const RECONNECT_BASE_MS = 1000; let reconnectCount = 0; function ensureBotEl() { if (botEl) return; if (loadingEl) { loadingEl.remove(); loadingEl = null; } botEl = document.createElement('div'); botEl.className = 'flex gap-3 px-4 sm:px-6 py-3'; botEl.dataset.requestId = requestId; botEl.innerHTML = ` CowAgent
${formatTime(timestamp)}
`; messagesDiv.appendChild(botEl); stepsEl = botEl.querySelector('.agent-steps'); contentEl = botEl.querySelector('.answer-content'); mediaEl = botEl.querySelector('.media-content'); } function connect() { const es = new EventSource(`/stream?request_id=${encodeURIComponent(requestId)}`); activeStreams[requestId] = es; es.onmessage = function(e) { let item; try { item = JSON.parse(e.data); } catch (_) { return; } // Successful data received, reset reconnect counter reconnectCount = 0; if (item.type === 'reasoning') { ensureBotEl(); reasoningText += item.content; if (!currentReasoningEl) { reasoningStartTime = Date.now(); currentReasoningEl = document.createElement('div'); currentReasoningEl.className = 'agent-step agent-thinking-step'; // During streaming, use a
 with a single text node and
                    // append-only updates. This avoids re-parsing markdown and
                    // re-setting innerHTML on every chunk, which is what causes
                    // the page to crash on long chains-of-thought.
                    currentReasoningEl.innerHTML = `
                        
${t('thinking_in_progress')}
`; stepsEl.appendChild(currentReasoningEl); const preEl = currentReasoningEl.querySelector('.thinking-stream-pre'); preEl.appendChild(document.createTextNode('')); currentReasoningEl._streamTextNode = preEl.firstChild; currentReasoningEl._streamPendingText = ''; currentReasoningEl._streamRafScheduled = false; currentReasoningEl._streamCharsRendered = 0; currentReasoningEl._streamCapped = false; } // Hard cap: once REASONING_RENDER_CAP chars are in the DOM, stop // appending further deltas. The full text is still kept in // `reasoningText` for finalize-time head+tail rendering. if (!currentReasoningEl._streamCapped) { currentReasoningEl._streamPendingText += item.content; if (!currentReasoningEl._streamRafScheduled) { currentReasoningEl._streamRafScheduled = true; const elRef = currentReasoningEl; requestAnimationFrame(() => { elRef._streamRafScheduled = false; if (!elRef.isConnected || !elRef._streamTextNode) return; let pending = elRef._streamPendingText; elRef._streamPendingText = ''; if (!pending) return; const remaining = REASONING_RENDER_CAP - elRef._streamCharsRendered; if (remaining <= 0) { elRef._streamCapped = true; } else { if (pending.length > remaining) { pending = pending.slice(0, remaining); elRef._streamCapped = true; } elRef._streamTextNode.appendData(pending); elRef._streamCharsRendered += pending.length; if (elRef._streamCapped) { elRef._streamTextNode.appendData( '\n\n... [reasoning truncated for display] ...' ); } } scrollChatToBottom(); }); } } } else if (item.type === 'delta') { ensureBotEl(); if (currentReasoningEl) { finalizeThinking(currentReasoningEl, reasoningStartTime, reasoningText); currentReasoningEl = null; reasoningText = ''; } accumulatedText += item.content; contentEl.innerHTML = renderMarkdown(accumulatedText); scrollChatToBottom(); } else if (item.type === 'message_end') { if (item.has_tool_calls && accumulatedText.trim()) { ensureBotEl(); const frozenEl = document.createElement('div'); frozenEl.className = 'agent-step agent-content-step'; frozenEl.innerHTML = `
${renderMarkdown(accumulatedText.trim())}
`; stepsEl.appendChild(frozenEl); accumulatedText = ''; contentEl.innerHTML = ''; scrollChatToBottom(); } } else if (item.type === 'tool_start') { ensureBotEl(); if (currentReasoningEl) { finalizeThinking(currentReasoningEl, reasoningStartTime, reasoningText); currentReasoningEl = null; reasoningText = ''; } accumulatedText = ''; contentEl.innerHTML = ''; // Add tool execution indicator (collapsible) currentToolEl = document.createElement('div'); currentToolEl.className = 'agent-step agent-tool-step'; const argsStr = formatToolArgs(item.arguments || {}); currentToolEl.innerHTML = `
${item.tool}
Input
${argsStr}
`; stepsEl.appendChild(currentToolEl); scrollChatToBottom(); } else if (item.type === 'tool_end') { if (currentToolEl) { const isError = item.status !== 'success'; const icon = currentToolEl.querySelector('.tool-icon'); icon.className = isError ? 'fas fa-times text-red-400 flex-shrink-0 tool-icon' : 'fas fa-check text-primary-400 flex-shrink-0 tool-icon'; // Show execution time const nameEl = currentToolEl.querySelector('.tool-name'); if (item.execution_time !== undefined) { nameEl.innerHTML += ` ${item.execution_time}s`; } // Fill output section const outputSection = currentToolEl.querySelector('.tool-output-section'); if (outputSection && item.result) { outputSection.innerHTML = `
${isError ? 'Error' : 'Output'}
${escapeHtml(String(item.result))}
`; } if (isError) currentToolEl.classList.add('tool-failed'); currentToolEl = null; } } else if (item.type === 'image') { ensureBotEl(); const imgEl = document.createElement('img'); imgEl.src = item.content; imgEl.alt = 'screenshot'; imgEl.style.cssText = 'max-width:600px;border-radius:8px;margin:8px 0;cursor:zoom-in;box-shadow:0 1px 4px rgba(0,0,0,0.1);'; imgEl.onclick = () => _openImageLightbox(imgEl.src); mediaEl.appendChild(imgEl); scrollChatToBottom(); } else if (item.type === 'text') { // Intermediate text sent before media items; display it but keep SSE open. ensureBotEl(); contentEl.classList.remove('sse-streaming'); const textContent = item.content || accumulatedText; if (textContent) contentEl.innerHTML = renderMarkdown(textContent); applyHighlighting(botEl); scrollChatToBottom(); } else if (item.type === 'video') { ensureBotEl(); const wrapper = document.createElement('div'); wrapper.innerHTML = _buildVideoHtml(item.content); mediaEl.appendChild(wrapper.firstElementChild || wrapper); scrollChatToBottom(); } else if (item.type === 'file') { ensureBotEl(); const fileName = item.file_name || item.content.split('/').pop(); const fileEl = document.createElement('a'); fileEl.href = item.content; fileEl.download = fileName; fileEl.target = '_blank'; fileEl.className = 'file-attachment'; fileEl.style.cssText = 'display:inline-flex;align-items:center;gap:6px;padding:8px 14px;margin:8px 0;border-radius:8px;background:var(--bg-secondary,#f3f4f6);color:var(--text-primary,#374151);text-decoration:none;font-size:14px;border:1px solid var(--border-color,#e5e7eb);'; fileEl.innerHTML = ` ${fileName}`; mediaEl.appendChild(fileEl); scrollChatToBottom(); } else if (item.type === 'phase') { // Coarse progress (e.g. cow install-browser); must not close SSE (unlike "done") ensureBotEl(); const wrap = document.createElement('div'); wrap.className = 'text-xs sm:text-sm text-slate-600 dark:text-slate-400 border-l-2 border-primary-400 pl-2 py-1 my-0.5'; wrap.textContent = String(item.content || ''); stepsEl.appendChild(wrap); scrollChatToBottom(); } else if (item.type === 'cancelled') { // Agent acknowledged the stop; mark the bubble. A trailing // "done" still arrives with the partial answer. ensureBotEl(); if (currentReasoningEl) { finalizeThinking(currentReasoningEl, reasoningStartTime, reasoningText); currentReasoningEl = null; reasoningText = ''; } if (!botEl.querySelector('.agent-cancelled-tag')) { const tag = document.createElement('div'); tag.className = 'agent-cancelled-tag text-xs text-amber-600 dark:text-amber-400 mt-1'; tag.textContent = (currentLang === 'zh') ? '已中止' : 'Cancelled'; stepsEl.appendChild(tag); } resetSendBtnSendMode(); } else if (item.type === 'done') { // Don't close the stream yet: the backend keeps it open // for a short tail to deliver async attachments such as // TTS audio (`voice_attach`). It will close the stream on // its own via onerror once the tail expires. done = true; resetSendBtnSendMode(); const finalTextRaw = item.content || accumulatedText; const finalText = localizeCancelMarker(finalTextRaw); if (!botEl && finalText) { if (loadingEl) { loadingEl.remove(); loadingEl = null; } addBotMessage(finalText, new Date((item.timestamp || Date.now() / 1000) * 1000), requestId); } else if (botEl) { contentEl.classList.remove('sse-streaming'); if (finalText) contentEl.innerHTML = renderMarkdown(finalText); contentEl.dataset.rawMd = finalTextRaw || ''; const copyBtn = botEl.querySelector('.copy-msg-btn'); if (copyBtn && finalText) copyBtn.style.display = ''; applyHighlighting(botEl); } renderBotSpeakerButton(botEl, finalText); scrollChatToBottom(); if (titleInfo) { generateSessionTitle(titleInfo.sid, titleInfo.userMsg, ''); titleInfo = null; } else if (sessionPanelOpen) { loadSessionList(); } } else if (item.type === 'voice_attach') { // TTS finished — attach a playable audio element to the // current bot bubble. The stream closes right after. if (botEl && item.url) { attachAudioToBotBubble(botEl, item.url, { autoplay: true }); } es.close(); delete activeStreams[requestId]; } else if (item.type === 'error') { done = true; es.close(); delete activeStreams[requestId]; if (loadingEl) { loadingEl.remove(); loadingEl = null; } addBotMessage(t('error_send'), new Date()); resetSendBtnSendMode(); } }; es.onerror = function() { es.close(); delete activeStreams[requestId]; if (done) { // Normal close after the post-done tail expired; nothing to do. return; } if (currentReasoningEl) { finalizeThinking(currentReasoningEl, reasoningStartTime, reasoningText); currentReasoningEl = null; reasoningText = ''; } if (reconnectCount < MAX_RECONNECTS) { reconnectCount++; const delay = Math.min(RECONNECT_BASE_MS * reconnectCount, 5000); console.warn(`[SSE] connection lost for ${requestId}, reconnecting in ${delay}ms (attempt ${reconnectCount}/${MAX_RECONNECTS})`); setTimeout(connect, delay); return; } // Exhausted retries, show whatever we have if (loadingEl) { loadingEl.remove(); loadingEl = null; } if (!botEl) { addBotMessage(t('error_send'), new Date()); } else if (accumulatedText) { contentEl.classList.remove('sse-streaming'); contentEl.innerHTML = renderMarkdown(accumulatedText); applyHighlighting(botEl); bindChatKnowledgeLinks(botEl); } resetSendBtnSendMode(); }; } connect(); } function startPolling() { const gen = ++pollGeneration; isPolling = true; let pollInFlight = false; function poll() { if (gen !== pollGeneration) return; if (pollInFlight) return; if (document.hidden) { setTimeout(poll, 10000); return; } pollInFlight = true; fetch('/poll', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: sessionId }) }) .then(r => r.json()) .then(data => { pollInFlight = false; if (gen !== pollGeneration) return; if (data.status === 'success' && data.has_content) { const rid = data.request_id; if (loadingContainers[rid]) { loadingContainers[rid].remove(); delete loadingContainers[rid]; } const welcomeScreen = document.getElementById('welcome-screen'); if (welcomeScreen) welcomeScreen.remove(); addBotMessage(data.content, new Date(data.timestamp * 1000), rid); scrollChatToBottom(); } const delay = (data.status === 'success' && data.has_content) ? 5000 : 10000; setTimeout(poll, delay); }) .catch(() => { pollInFlight = false; setTimeout(poll, 10000); }); } poll(); } function createUserMessageEl(content, timestamp, attachments) { const el = document.createElement('div'); el.className = 'flex justify-end px-4 sm:px-6 py-3'; let attachHtml = ''; if (attachments && attachments.length > 0) { const items = attachments.map(a => { if (a.file_type === 'image') { return `${escapeHtml(a.file_name)}`; } const icon = a.file_type === 'video' ? 'fa-film' : (a.file_type === 'directory' ? 'fa-folder-tree' : 'fa-file-alt'); const suffix = a.file_type === 'directory' && a.file_count ? ` (${a.file_count})` : ''; return `
${escapeHtml(a.file_name)}${suffix}
`; }).join(''); attachHtml = `
${items}
`; } const textHtml = content ? renderMarkdown(content) : ''; el.innerHTML = `
${attachHtml}${textHtml}
${formatTime(timestamp)}
`; return el; } function renderToolCallsHtml(toolCalls) { if (!toolCalls || toolCalls.length === 0) return ''; return toolCalls.map(tc => { const argsStr = formatToolArgs(tc.arguments || {}); const resultStr = tc.result ? escapeHtml(String(tc.result)) : ''; const hasResult = !!resultStr; return `
${escapeHtml(tc.name || '')}
Input
${argsStr}
${hasResult ? `
Output
${resultStr}
` : ''}
`; }).join(''); } // Cap for rendering reasoning content in the bubble. Beyond this size, // we skip markdown rendering entirely and show plain text head + tail to // keep the page responsive (very long chains-of-thought can otherwise // stall or crash the browser when re-parsed by marked.js). // Keep this in sync with backend MAX_STORED_REASONING_CHARS and // MAX_REASONING_STREAM_CHARS so storage / SSE / display stay aligned. const REASONING_RENDER_CAP = 4 * 1024; // 4 KB function _truncateReasoningForDisplay(text) { if (!text || text.length <= REASONING_RENDER_CAP) return { text, truncated: false, omitted: 0 }; const half = Math.floor(REASONING_RENDER_CAP / 2); const head = text.slice(0, half); const tail = text.slice(-half); return { text: head + '\n\n... [' + (text.length - head.length - tail.length) + ' chars omitted] ...\n\n' + tail, truncated: true, omitted: text.length - head.length - tail.length, }; } function _renderReasoningBody(text) { // For short reasoning, render as markdown. For long ones, fall back to // an escaped
 block to avoid expensive markdown parsing.
    const { text: shown, truncated } = _truncateReasoningForDisplay(text);
    if (truncated || shown.length > REASONING_RENDER_CAP) {
        return '
' + escapeHtml(shown) + '
'; } return renderMarkdown(shown); } function finalizeThinking(el, startTime, text) { const elapsed = ((Date.now() - startTime) / 1000).toFixed(1); el.querySelector('.thinking-summary').textContent = t('thinking_done'); const fullDiv = el.querySelector('.thinking-full'); fullDiv.innerHTML = `
${t('thinking_duration')} ${elapsed}s
` + _renderReasoningBody(text); } function renderThinkingHtml(text) { if (!text || !text.trim()) return ''; const full = text.trim(); return `
${t('thinking_done')}
${_renderReasoningBody(full)}
`; } function renderStepsHtml(steps) { if (!steps || steps.length === 0) return { stepsHtml: '', finalContent: '' }; // Find the index of the last content step — it becomes the main answer, not a step let lastContentIdx = -1; for (let i = steps.length - 1; i >= 0; i--) { if (steps[i].type === 'content') { lastContentIdx = i; break; } } let html = ''; let lastContentText = ''; for (let i = 0; i < steps.length; i++) { const step = steps[i]; if (step.type === 'thinking') { html += renderThinkingHtml(step.content); } else if (step.type === 'content') { if (i === lastContentIdx) { lastContentText = step.content; } else { html += `
${renderMarkdown(step.content)}
`; } } else if (step.type === 'tool') { const argsStr = formatToolArgs(step.arguments || {}); const resultStr = step.result ? escapeHtml(String(step.result)) : ''; const isErr = step.is_error === true; const iconClass = isErr ? 'fas fa-times text-red-400 flex-shrink-0 tool-icon' : 'fas fa-check text-primary-400 flex-shrink-0 tool-icon'; html += `
${escapeHtml(step.name || '')}
Input
${argsStr}
${resultStr ? `
${isErr ? 'Error' : 'Output'}
${resultStr}
` : ''}
`; // If this tool sent a file (send/read tool), render the media inline // so it persists across page refreshes (SSE-only file events are not stored). const mediaHtml = _renderSentFileFromToolResult(step); if (mediaHtml) html += mediaHtml; } } return { stepsHtml: html, lastContentText }; } // Extract file-to-send metadata from a tool's result and render an inline preview. // Returns '' if the result isn't a file_to_send payload. function _renderSentFileFromToolResult(step) { if (!step || !step.result) return ''; let payload; try { payload = typeof step.result === 'string' ? JSON.parse(step.result) : step.result; } catch (_) { return ''; } if (!payload || payload.type !== 'file_to_send' || !payload.path) return ''; const webUrl = _toWebUrl(payload.path); const fileType = payload.file_type || 'file'; const fileName = payload.file_name || payload.path.split('/').pop(); if (fileType === 'image') { return `
${_buildImageHtml(webUrl)}
`; } if (fileType === 'video') { return `
${_buildVideoHtml(webUrl)}
`; } return ``; } // Cosmetic translator for cancel markers persisted in history. // History keeps the English canonical form for the LLM; only display is localized. function localizeCancelMarker(text) { if (!text) return text; if (currentLang !== 'zh') return text; return text .replace(/_\(Cancelled by user\)_/g, '_(用户已中止)_') .replace(/_\(Cancelled\)_/g, '_(已中止)_'); } function createBotMessageEl(content, timestamp, requestId, msg) { const el = document.createElement('div'); el.className = 'flex gap-3 px-4 sm:px-6 py-3'; if (requestId) el.dataset.requestId = requestId; let stepsHtml = ''; let displayContent = localizeCancelMarker(content); if (msg && msg.steps && msg.steps.length > 0) { // New format: ordered steps with interleaved content const result = renderStepsHtml(msg.steps); stepsHtml = result.stepsHtml; // The final content (last text after all steps) is the main answer displayContent = content || result.lastContentText; } else { // Legacy format: separate tool_calls + optional reasoning const toolCalls = msg && msg.tool_calls; const reasoning = msg && msg.reasoning; stepsHtml = renderThinkingHtml(reasoning) + renderToolCallsHtml(toolCalls); } el.innerHTML = ` CowAgent
${stepsHtml ? `
${stepsHtml}
` : ''}
${renderMarkdown(displayContent)}
${formatTime(timestamp)}
`; el.querySelector('.answer-content').dataset.rawMd = displayContent; // Existing TTS attachment (history replay): mount the player up-front. const existingAudio = msg && msg.extras && msg.extras.audio && msg.extras.audio.url; if (existingAudio) { attachAudioToBotBubble(el, existingAudio, { autoplay: false }); } renderBotSpeakerButton(el, displayContent); applyHighlighting(el); bindChatKnowledgeLinks(el); return el; } // Append (or replace) a small audio player inside a bot bubble's // dedicated `.bot-audio-slot`. Used by both live TTS pushes and history // replay. Silent failures: never throws. function attachAudioToBotBubble(botEl, audioUrl, opts) { try { if (!botEl || !audioUrl) return; const slot = botEl.querySelector('.bot-audio-slot'); if (!slot) return; slot.innerHTML = ''; slot.style.marginTop = '6px'; const pill = renderVoicePill(audioUrl, { autoplay: !!(opts && opts.autoplay) }); slot.appendChild(pill); const speakBtn = botEl.querySelector('.speak-msg-btn'); if (speakBtn) speakBtn.style.display = 'none'; } catch (_) { /* silent */ } } // Build a compact play/pause + progress + duration pill that wraps a // hidden