Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions h5/src/chat-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ const SVG_MIC = `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" str
let _recognition = null
let _isRecording = false

function shouldAutoFocusInput() {
const ua = navigator.userAgent || ''
const isMobileUA = /Android|iPhone|iPad|iPod|Mobile|HarmonyOS/i.test(ua)
const isCoarsePointer = !!window.matchMedia?.('(pointer: coarse)').matches
return !(isMobileUA || isCoarsePointer)
}

function focusInputIfDesktop() {
if (!_textarea || !shouldAutoFocusInput()) return
requestAnimationFrame(() => _textarea?.focus())
}

function blurInputIfMobile() {
if (!_textarea || shouldAutoFocusInput()) return
_textarea.blur()
}

/** 从 OpenClaw 消息中提取可渲染内容(文本 + 图片 + 视频 + 音频 + 文件) */
function extractContent(message) {
if (!message || typeof message !== 'object') return null
Expand Down Expand Up @@ -244,7 +261,7 @@ export function initChatUI(onSettings) {
}

initCommands((cmd, fillOnly) => {
if (fillOnly) { _textarea.value = cmd; _textarea.focus(); updateSendState() }
if (fillOnly) { _textarea.value = cmd; focusInputIfDesktop(); updateSendState() }
else { _textarea.value = cmd; sendMessage() }
})

Expand Down Expand Up @@ -288,7 +305,7 @@ function toggleVoiceInput(SR) {
_isRecording = false
micBtn.classList.remove('recording')
_recognition = null
_textarea.focus()
focusInputIfDesktop()
}
_recognition.onerror = (e) => {
_isRecording = false
Expand Down Expand Up @@ -347,6 +364,7 @@ function handleSendClick() {
wsClient.chatAbort(_sessionKey, _currentRunId).catch(() => {})
return
}
blurInputIfMobile()
sendMessage()
}

Expand Down Expand Up @@ -411,7 +429,7 @@ async function doSend(text, attachments) {
} finally {
_isSending = false
_textarea.disabled = false
_textarea.focus()
focusInputIfDesktop()
}
}

Expand All @@ -438,7 +456,7 @@ function processMessageQueue() {
.finally(() => {
_isSending = false
_textarea.disabled = false
_textarea.focus()
focusInputIfDesktop()
})
}

Expand Down
3 changes: 2 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const CONFIG = {
proxyToken: process.env.PROXY_TOKEN || '',
gatewayUrl: process.env.OPENCLAW_GATEWAY_URL || 'ws://127.0.0.1:18789',
gatewayToken: process.env.OPENCLAW_GATEWAY_TOKEN || '',
mediaAllowAll: process.env.MEDIA_ALLOW_ALL === '1',
h5DistPath: join(__dirname, '../h5/dist'),
};

Expand Down Expand Up @@ -373,7 +374,7 @@ app.get('/health', (req, res) => {
app.get('/media', (req, res) => {
const filePath = req.query.path;
if (!filePath || !existsSync(filePath)) return res.status(404).send('Not Found');
if (!filePath.startsWith('/tmp/') && !filePath.startsWith('/var/folders/')) return res.status(403).send('Forbidden');
if (!CONFIG.mediaAllowAll && !filePath.startsWith('/tmp/') && !filePath.startsWith('/var/folders/')) return res.status(403).send('Forbidden');
const stat = statSync(filePath);
const ext = filePath.split('.').pop().toLowerCase();
const mime = {
Expand Down