Migrate backend jobs to Redis Streams
This commit is contained in:
+290
-260
@@ -1,4 +1,15 @@
|
||||
import { API_URL, API_KEY, PRO_STREAM_URL, PRO_FRONTEND_TIMEOUT_MS, TTS_URL, TTS_STATUS_URL, TTS_CONFIG_URL } from './config.js'
|
||||
import {
|
||||
API_URL,
|
||||
API_KEY,
|
||||
PRO_URL,
|
||||
PRO_FRONTEND_TIMEOUT_MS,
|
||||
TTS_URL,
|
||||
TTS_STATUS_URL,
|
||||
TTS_CONFIG_URL,
|
||||
COMPRESS_SUBMIT_URL,
|
||||
COMPRESS_STATUS_URL,
|
||||
JOB_LOAD_URL,
|
||||
} from './config.js'
|
||||
import { useSettingsStore } from '../stores/settings'
|
||||
|
||||
function generateRequestId() {
|
||||
@@ -8,27 +19,6 @@ function generateRequestId() {
|
||||
return `${Date.now()}-${Math.random().toString(16).slice(2)}`
|
||||
}
|
||||
|
||||
function getCancelUrl(apiUrl) {
|
||||
const normalized = String(apiUrl || '').replace(/\/+$/, '')
|
||||
if (!normalized) return '/v1/completions/cancel'
|
||||
if (/\/v1\/pro\/completions\/stream$/i.test(normalized)) {
|
||||
return normalized.replace(/\/v1\/pro\/completions\/stream$/i, '/v1/completions/cancel')
|
||||
}
|
||||
if (normalized.endsWith('/v1/completions')) {
|
||||
return `${normalized}/cancel`
|
||||
}
|
||||
return `${normalized}/cancel`
|
||||
}
|
||||
|
||||
function getProCancelUrl(apiUrl) {
|
||||
const normalized = String(apiUrl || '').replace(/\/+$/, '')
|
||||
if (!normalized) return '/v1/completions/cancel'
|
||||
if (/\/v1\/pro\/completions$/i.test(normalized)) {
|
||||
return normalized.replace(/\/v1\/pro\/completions$/i, '/v1/completions/cancel')
|
||||
}
|
||||
return normalized.replace(/\/v1\/pro\/completions\/stream$/i, '/v1/completions/cancel')
|
||||
}
|
||||
|
||||
function normalizeAbortReason(reason) {
|
||||
if (typeof reason === 'string' && reason.trim()) {
|
||||
return reason.trim().slice(0, 64)
|
||||
@@ -36,46 +26,6 @@ function normalizeAbortReason(reason) {
|
||||
return 'abort'
|
||||
}
|
||||
|
||||
async function sendCancelRequest(cancelUrl, requestId, reason) {
|
||||
try {
|
||||
await fetch(cancelUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': API_KEY,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
request_id: requestId,
|
||||
reason,
|
||||
}),
|
||||
})
|
||||
} catch {
|
||||
// Cancel request failed silently
|
||||
}
|
||||
}
|
||||
|
||||
function createAbortError(message = 'Request aborted') {
|
||||
const error = new Error(message)
|
||||
error.name = 'AbortError'
|
||||
return error
|
||||
}
|
||||
|
||||
function buildCompletionBody(settings, prefix, suffix, languageId, extra = {}) {
|
||||
return {
|
||||
prefix,
|
||||
suffix,
|
||||
languageId,
|
||||
model_thinking: settings.modelThinking,
|
||||
privacy_mode: settings.privacyMode,
|
||||
user_preferences: {
|
||||
language: settings.language,
|
||||
currency: settings.currency,
|
||||
timezone: settings.detectedTimezone,
|
||||
},
|
||||
...extra,
|
||||
}
|
||||
}
|
||||
|
||||
function parseSseEvent(rawEvent) {
|
||||
const lines = String(rawEvent || '').replace(/\r/g, '').split('\n')
|
||||
let event = 'message'
|
||||
@@ -98,6 +48,156 @@ function parseSseEvent(rawEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
function createAbortError(message = 'Request aborted') {
|
||||
const error = new Error(message)
|
||||
error.name = 'AbortError'
|
||||
return error
|
||||
}
|
||||
|
||||
async function sendCancelRequest(cancelUrl, requestId, reason) {
|
||||
try {
|
||||
await fetch(cancelUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': API_KEY,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
request_id: requestId,
|
||||
reason,
|
||||
}),
|
||||
})
|
||||
} catch {
|
||||
// Best-effort cancel only
|
||||
}
|
||||
}
|
||||
|
||||
function getCancelUrl(apiUrl) {
|
||||
const normalized = String(apiUrl || '').replace(/\/+$/, '')
|
||||
if (/\/v1\/pro\/completions$/i.test(normalized)) {
|
||||
return normalized.replace(/\/v1\/pro\/completions$/i, '/v1/pro/completions/cancel')
|
||||
}
|
||||
if (/\/v1\/completions$/i.test(normalized)) {
|
||||
return normalized.replace(/\/v1\/completions$/i, '/v1/completions/cancel')
|
||||
}
|
||||
return `${normalized}/cancel`
|
||||
}
|
||||
|
||||
function buildCompletionBody(settings, prefix, suffix, languageId, extra = {}) {
|
||||
return {
|
||||
prefix,
|
||||
suffix,
|
||||
languageId,
|
||||
model_thinking: settings.modelThinking,
|
||||
privacy_mode: settings.privacyMode,
|
||||
user_preferences: {
|
||||
language: settings.language,
|
||||
currency: settings.currency,
|
||||
timezone: settings.detectedTimezone,
|
||||
},
|
||||
...extra,
|
||||
}
|
||||
}
|
||||
|
||||
async function consumeSseJson({
|
||||
url,
|
||||
body,
|
||||
requestId,
|
||||
signal,
|
||||
timeoutMs,
|
||||
onChunk,
|
||||
onEvent,
|
||||
onDone,
|
||||
}) {
|
||||
const requestController = new AbortController()
|
||||
const timeoutId = timeoutMs ? setTimeout(() => requestController.abort('timeout'), timeoutMs) : null
|
||||
const cancelUrl = getCancelUrl(url)
|
||||
|
||||
const relayAbort = () => {
|
||||
requestController.abort(signal?.reason || 'abort')
|
||||
}
|
||||
|
||||
const onAbort = () => {
|
||||
const reason = normalizeAbortReason(requestController.signal.reason)
|
||||
void sendCancelRequest(cancelUrl, requestId, reason)
|
||||
}
|
||||
|
||||
requestController.signal.addEventListener('abort', onAbort, { once: true })
|
||||
if (signal) {
|
||||
if (signal.aborted) {
|
||||
relayAbort()
|
||||
} else {
|
||||
signal.addEventListener('abort', relayAbort, { once: true })
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-Id': requestId,
|
||||
'X-API-Key': API_KEY,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
signal: requestController.signal,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
throw new Error(`HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
if (!res.body) {
|
||||
throw new Error('流式响应不可用')
|
||||
}
|
||||
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
let finalPayload = null
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
buffer += decoder.decode(value, { stream: true })
|
||||
|
||||
let boundary = buffer.indexOf('\n\n')
|
||||
while (boundary >= 0) {
|
||||
const chunk = buffer.slice(0, boundary)
|
||||
buffer = buffer.slice(boundary + 2)
|
||||
const parsed = parseSseEvent(chunk)
|
||||
const data = parsed.data ? JSON.parse(parsed.data) : {}
|
||||
|
||||
onEvent?.(parsed.event, data)
|
||||
|
||||
if (parsed.event === 'result') {
|
||||
onChunk?.(data)
|
||||
} else if (parsed.event === 'done') {
|
||||
finalPayload = data.result || data
|
||||
return onDone ? onDone(finalPayload) : finalPayload
|
||||
} else if (parsed.event === 'error') {
|
||||
throw new Error(String(data.error || '请求失败'))
|
||||
} else if (parsed.event === 'cancelled') {
|
||||
throw createAbortError('请求已取消')
|
||||
}
|
||||
|
||||
boundary = buffer.indexOf('\n\n')
|
||||
}
|
||||
}
|
||||
|
||||
if (requestController.signal.aborted) {
|
||||
throw createAbortError('请求已中止')
|
||||
}
|
||||
return onDone ? onDone(finalPayload || {}) : finalPayload
|
||||
} finally {
|
||||
if (timeoutId) clearTimeout(timeoutId)
|
||||
requestController.signal.removeEventListener('abort', onAbort)
|
||||
if (signal) {
|
||||
signal.removeEventListener('abort', relayAbort)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl = API_URL) {
|
||||
let normalizedLanguageId = 'markdown'
|
||||
if (typeof languageId === 'string' && languageId.trim()) {
|
||||
@@ -109,60 +209,32 @@ export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl
|
||||
apiUrl = signal
|
||||
signal = undefined
|
||||
}
|
||||
|
||||
const settings = useSettingsStore()
|
||||
const requestId = generateRequestId()
|
||||
const cancelUrl = getCancelUrl(apiUrl)
|
||||
let finalContent = ''
|
||||
|
||||
const onAbort = () => {
|
||||
const reason = normalizeAbortReason(signal?.reason)
|
||||
void sendCancelRequest(cancelUrl, requestId, reason)
|
||||
}
|
||||
const result = await consumeSseJson({
|
||||
url: apiUrl,
|
||||
body: buildCompletionBody(settings, prefix, suffix, normalizedLanguageId),
|
||||
requestId,
|
||||
signal,
|
||||
onChunk(data) {
|
||||
if (typeof data.content === 'string') {
|
||||
finalContent = data.content
|
||||
} else if (typeof data.delta === 'string') {
|
||||
finalContent += data.delta
|
||||
}
|
||||
},
|
||||
onDone(data) {
|
||||
return typeof data.content === 'string' ? data.content : finalContent
|
||||
},
|
||||
})
|
||||
|
||||
if (signal) {
|
||||
if (signal.aborted) {
|
||||
onAbort()
|
||||
} else {
|
||||
signal.addEventListener('abort', onAbort, { once: true })
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const settings = useSettingsStore()
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-Id': requestId,
|
||||
'X-API-Key': API_KEY,
|
||||
}
|
||||
|
||||
const body = buildCompletionBody(settings, prefix, suffix, normalizedLanguageId)
|
||||
|
||||
const res = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
signal,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
throw new Error(`HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
return data.content || ''
|
||||
} catch (e) {
|
||||
if (e.name === 'AbortError') {
|
||||
// ignore abort
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
} finally {
|
||||
if (signal) {
|
||||
signal.removeEventListener('abort', onAbort)
|
||||
}
|
||||
}
|
||||
return result || ''
|
||||
}
|
||||
|
||||
export async function fetchProSuggestionStream(payload, apiUrl = PRO_STREAM_URL) {
|
||||
export async function fetchProSuggestionStream(payload, apiUrl = PRO_URL) {
|
||||
const {
|
||||
prefix = '',
|
||||
suffix = '',
|
||||
@@ -176,168 +248,65 @@ export async function fetchProSuggestionStream(payload, apiUrl = PRO_STREAM_URL)
|
||||
|
||||
const settings = useSettingsStore()
|
||||
const requestId = generateRequestId()
|
||||
const cancelUrl = getProCancelUrl(apiUrl)
|
||||
const requestController = new AbortController()
|
||||
const timeoutId = setTimeout(() => {
|
||||
requestController.abort('timeout')
|
||||
}, timeoutMs)
|
||||
let finalContent = ''
|
||||
|
||||
const relayAbort = () => {
|
||||
requestController.abort(signal?.reason || 'abort')
|
||||
}
|
||||
|
||||
const onAbort = () => {
|
||||
const reason = normalizeAbortReason(requestController.signal.reason)
|
||||
void sendCancelRequest(cancelUrl, requestId, reason)
|
||||
}
|
||||
|
||||
requestController.signal.addEventListener('abort', onAbort, { once: true })
|
||||
|
||||
if (signal) {
|
||||
if (signal.aborted) {
|
||||
relayAbort()
|
||||
} else {
|
||||
signal.addEventListener('abort', relayAbort, { once: true })
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-Id': requestId,
|
||||
'X-API-Key': API_KEY,
|
||||
return consumeSseJson({
|
||||
url: apiUrl,
|
||||
requestId,
|
||||
signal,
|
||||
timeoutMs,
|
||||
body: {
|
||||
prefix,
|
||||
suffix,
|
||||
languageId: String(languageId || 'markdown').trim() || 'markdown',
|
||||
instruction,
|
||||
pro_thinking: settings.proThinking || 'medium',
|
||||
privacy_mode: settings.privacyMode,
|
||||
user_preferences: {
|
||||
language: settings.language,
|
||||
currency: settings.currency,
|
||||
timezone: settings.detectedTimezone,
|
||||
},
|
||||
body: JSON.stringify(
|
||||
{
|
||||
prefix,
|
||||
suffix,
|
||||
languageId: String(languageId || 'markdown').trim() || 'markdown',
|
||||
instruction,
|
||||
pro_thinking: settings.proThinking || 'medium',
|
||||
privacy_mode: settings.privacyMode,
|
||||
user_preferences: {
|
||||
language: settings.language,
|
||||
currency: settings.currency,
|
||||
timezone: settings.detectedTimezone,
|
||||
},
|
||||
}
|
||||
),
|
||||
signal: requestController.signal,
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
throw new Error(`HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
|
||||
if (!res.body) {
|
||||
throw new Error('PRO 模式流式响应不可用')
|
||||
}
|
||||
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
let finalContent = ''
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
|
||||
buffer += decoder.decode(value, { stream: true })
|
||||
|
||||
let boundary = buffer.indexOf('\n\n')
|
||||
while (boundary >= 0) {
|
||||
const chunk = buffer.slice(0, boundary)
|
||||
buffer = buffer.slice(boundary + 2)
|
||||
|
||||
const parsed = parseSseEvent(chunk)
|
||||
if (parsed.event && parsed.event !== 'message') {
|
||||
let eventData = {}
|
||||
if (parsed.data) {
|
||||
try {
|
||||
eventData = JSON.parse(parsed.data)
|
||||
} catch {
|
||||
eventData = {}
|
||||
}
|
||||
}
|
||||
onEvent?.(parsed.event, eventData)
|
||||
}
|
||||
|
||||
if (parsed.event === 'chunk' && parsed.data) {
|
||||
const data = JSON.parse(parsed.data)
|
||||
const delta = String(data.delta || '')
|
||||
if (delta) {
|
||||
finalContent += delta
|
||||
onChunk?.(delta)
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.event === 'done' && parsed.data) {
|
||||
const data = JSON.parse(parsed.data)
|
||||
return String(data.content || finalContent || '')
|
||||
}
|
||||
|
||||
if (parsed.event === 'error' && parsed.data) {
|
||||
const data = JSON.parse(parsed.data)
|
||||
throw new Error(String(data.error || 'PRO 模式请求失败'))
|
||||
}
|
||||
|
||||
if (parsed.event === 'cancelled') {
|
||||
throw createAbortError('PRO 模式请求已取消')
|
||||
}
|
||||
|
||||
boundary = buffer.indexOf('\n\n')
|
||||
},
|
||||
onChunk(data) {
|
||||
const delta = String(data.delta || data.content || '')
|
||||
if (delta) {
|
||||
finalContent += delta
|
||||
onChunk?.(delta)
|
||||
}
|
||||
}
|
||||
|
||||
if (requestController.signal.aborted) {
|
||||
throw createAbortError('PRO 模式请求已中止')
|
||||
}
|
||||
|
||||
return finalContent
|
||||
} catch (e) {
|
||||
if (e?.name === 'AbortError') {
|
||||
throw e
|
||||
}
|
||||
throw e
|
||||
} finally {
|
||||
clearTimeout(timeoutId)
|
||||
requestController.signal.removeEventListener('abort', onAbort)
|
||||
if (signal) {
|
||||
signal.removeEventListener('abort', relayAbort)
|
||||
}
|
||||
}
|
||||
},
|
||||
onEvent(event, data) {
|
||||
if (event === 'progress' && data.phase === 'thinking') {
|
||||
onEvent?.('thinking', data)
|
||||
return
|
||||
}
|
||||
if (event === 'queued' || event === 'started' || event === 'resource') {
|
||||
onEvent?.(event, data)
|
||||
}
|
||||
},
|
||||
onDone(data) {
|
||||
return String(data.content || finalContent || '')
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function fetchTTS(text, instruct = '', apiUrl = TTS_URL) {
|
||||
const res = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': API_KEY,
|
||||
const requestId = generateRequestId()
|
||||
return consumeSseJson({
|
||||
url: apiUrl,
|
||||
requestId,
|
||||
body: { text, instruct, speaker: 'Vivian', format: 'wav' },
|
||||
onDone(data) {
|
||||
return data
|
||||
},
|
||||
body: JSON.stringify({ text, instruct, speaker: 'Vivian', format: 'wav' }),
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
throw new Error(`TTS HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function fetchTTSStatus(apiUrl = TTS_STATUS_URL) {
|
||||
const res = await fetch(apiUrl, {
|
||||
headers: { 'X-API-Key': API_KEY },
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`TTS Status HTTP ${res.status}`)
|
||||
}
|
||||
|
||||
if (!res.ok) throw new Error(`TTS Status HTTP ${res.status}`)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
@@ -345,10 +314,71 @@ export async function fetchTTSConfig(apiUrl = TTS_CONFIG_URL) {
|
||||
const res = await fetch(apiUrl, {
|
||||
headers: { 'X-API-Key': API_KEY },
|
||||
})
|
||||
if (!res.ok) throw new Error(`TTS Config HTTP ${res.status}`)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export async function submitCompress(content, docType = 'txt', apiUrl = COMPRESS_SUBMIT_URL) {
|
||||
const res = await fetch(apiUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': API_KEY,
|
||||
},
|
||||
body: JSON.stringify({ content, docType }),
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`TTS Config HTTP ${res.status}`)
|
||||
const errorText = await res.text()
|
||||
throw new Error(`压缩提交失败 HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export function pollCompressStatus(taskId, onStateChange, apiUrl = COMPRESS_STATUS_URL) {
|
||||
let consecutiveErrors = 0
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
try {
|
||||
const res = await fetch(`${apiUrl}?task_id=${encodeURIComponent(taskId)}`, {
|
||||
headers: { 'X-API-Key': API_KEY },
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
consecutiveErrors++
|
||||
if (consecutiveErrors >= 5) {
|
||||
clearInterval(interval)
|
||||
onStateChange('error', '', `请求失败 HTTP ${res.status}`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
consecutiveErrors = 0
|
||||
const data = await res.json()
|
||||
onStateChange(data.status, data.content || '', data.message, data)
|
||||
|
||||
if (['completed', 'error', 'cancelled'].includes(data.status)) {
|
||||
clearInterval(interval)
|
||||
}
|
||||
} catch (err) {
|
||||
consecutiveErrors++
|
||||
if (consecutiveErrors >= 5) {
|
||||
clearInterval(interval)
|
||||
onStateChange('error', '', `网络异常: ${err.message || err}`)
|
||||
}
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
return { stop: () => clearInterval(interval) }
|
||||
}
|
||||
|
||||
export async function fetchJobLoad(apiUrl = JOB_LOAD_URL) {
|
||||
const res = await fetch(apiUrl, {
|
||||
headers: { 'X-API-Key': API_KEY },
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error(`Job Load HTTP ${res.status}`)
|
||||
}
|
||||
return res.json()
|
||||
}
|
||||
|
||||
+8
-1
@@ -4,7 +4,7 @@ const DEFAULT_API_BASE_URL = import.meta.env.DEV ? '' : 'https://api.imageteach.
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || DEFAULT_API_BASE_URL
|
||||
|
||||
export const API_URL = import.meta.env.VITE_API_URL || `${API_BASE_URL}/v1/completions`
|
||||
export const PRO_STREAM_URL = import.meta.env.VITE_PRO_STREAM_URL || `${API_BASE_URL}/v1/pro/completions/stream`
|
||||
export const PRO_URL = import.meta.env.VITE_PRO_URL || `${API_BASE_URL}/v1/pro/completions`
|
||||
export const PRO_FRONTEND_TIMEOUT_MS = Number(import.meta.env.VITE_PRO_FRONTEND_TIMEOUT_MS || 3660000)
|
||||
export const OCR_URL = import.meta.env.VITE_OCR_URL || `${API_BASE_URL}/v1/ocr`
|
||||
export const CONVERT_URL = import.meta.env.VITE_CONVERT_URL || `${API_BASE_URL}/v1/convert`
|
||||
@@ -12,4 +12,11 @@ export const EXPORT_PDF_URL = import.meta.env.VITE_EXPORT_PDF_URL || '/v1/export
|
||||
export const TTS_URL = import.meta.env.VITE_TTS_URL || `${API_BASE_URL}/v1/tts-asr/tts`
|
||||
export const TTS_STATUS_URL = import.meta.env.VITE_TTS_STATUS_URL || `${API_BASE_URL}/v1/tts-asr/status`
|
||||
export const TTS_CONFIG_URL = import.meta.env.VITE_TTS_CONFIG_URL || `${API_BASE_URL}/v1/tts-asr/config`
|
||||
export const ASR_URL = import.meta.env.VITE_ASR_URL || `${API_BASE_URL}/v1/tts-asr/asr`
|
||||
export const JOB_LOAD_URL = import.meta.env.VITE_JOB_LOAD_URL || `${API_BASE_URL}/v1/jobs/load`
|
||||
export const API_KEY = import.meta.env.VITE_API_KEY || 'your-secret-key-here'
|
||||
|
||||
// Compression always goes to local backend (not through reverse proxy)
|
||||
const COMPRESS_BASE_URL = import.meta.env.VITE_COMPRESS_BACKEND || 'http://localhost:8001'
|
||||
export const COMPRESS_SUBMIT_URL = `${COMPRESS_BASE_URL}/v1/compress/submit`
|
||||
export const COMPRESS_STATUS_URL = `${COMPRESS_BASE_URL}/v1/compress/status`
|
||||
|
||||
+272
-7
@@ -1,4 +1,71 @@
|
||||
import { CONVERT_URL } from './config.js'
|
||||
import { CONVERT_URL, ASR_URL } from './config.js'
|
||||
|
||||
function parseSseEvent(rawEvent) {
|
||||
const lines = String(rawEvent || '').replace(/\r/g, '').split('\n')
|
||||
let event = 'message'
|
||||
const dataLines = []
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line) continue
|
||||
if (line.startsWith('event:')) {
|
||||
event = line.slice(6).trim() || 'message'
|
||||
continue
|
||||
}
|
||||
if (line.startsWith('data:')) {
|
||||
dataLines.push(line.slice(5).trimStart())
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
event,
|
||||
data: dataLines.join('\n'),
|
||||
}
|
||||
}
|
||||
|
||||
async function consumeSseResult(res) {
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
throw new Error(`HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
|
||||
if (!res.body) {
|
||||
throw new Error('流式响应不可用')
|
||||
}
|
||||
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
let finalResult = null
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
buffer += decoder.decode(value, { stream: true })
|
||||
|
||||
let boundary = buffer.indexOf('\n\n')
|
||||
while (boundary >= 0) {
|
||||
const chunk = buffer.slice(0, boundary)
|
||||
buffer = buffer.slice(boundary + 2)
|
||||
const parsed = parseSseEvent(chunk)
|
||||
const data = parsed.data ? JSON.parse(parsed.data) : {}
|
||||
|
||||
if (parsed.event === 'done') {
|
||||
finalResult = data.result || data
|
||||
return finalResult
|
||||
}
|
||||
if (parsed.event === 'error') {
|
||||
throw new Error(String(data.error || '请求失败'))
|
||||
}
|
||||
if (parsed.event === 'cancelled') {
|
||||
throw new Error('请求已取消')
|
||||
}
|
||||
|
||||
boundary = buffer.indexOf('\n\n')
|
||||
}
|
||||
}
|
||||
|
||||
return finalResult
|
||||
}
|
||||
|
||||
function readFileAsBase64(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -31,14 +98,212 @@ export async function convertFileToMarkdown(file) {
|
||||
}),
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text()
|
||||
throw new Error(`HTTP ${res.status}: ${errorText}`)
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
const data = await consumeSseResult(res)
|
||||
if (!data || typeof data.markdown !== 'string') {
|
||||
throw new Error('No markdown returned')
|
||||
}
|
||||
return data.markdown
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode AudioBuffer as WAV (16kHz mono, 16-bit PCM) and return base64 string
|
||||
*/
|
||||
function audioBufferToWavBase64(audioBuffer) {
|
||||
// Resample to 16kHz if needed using OfflineAudioContext
|
||||
const targetSampleRate = 16000
|
||||
|
||||
if (audioBuffer.sampleRate === targetSampleRate) {
|
||||
// No resampling needed, just convert to mono and encode WAV
|
||||
} else {
|
||||
const offlineCtx = new OfflineAudioContext(1, audioBuffer.length * (targetSampleRate / audioBuffer.sampleRate), targetSampleRate)
|
||||
const source = offlineCtx.createBufferSource()
|
||||
source.buffer = audioBuffer
|
||||
source.connect(offlineCtx.destination)
|
||||
// We need to wait for the offline context to finish rendering
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const processBuffer = async (buffer) => {
|
||||
// Convert to mono if stereo/multi-channel
|
||||
let channels = buffer.numberOfChannels
|
||||
const length = buffer.length
|
||||
|
||||
if (channels === 1) {
|
||||
// Already mono, use directly
|
||||
const channelData = buffer.getChannelData(0)
|
||||
} else {
|
||||
// Mix down to mono by averaging channels
|
||||
const channelData = new Float32Array(length)
|
||||
for (let i = 0; i < length; i++) {
|
||||
let sum = 0
|
||||
for (let ch = 0; ch < channels; ch++) {
|
||||
sum += buffer.getChannelData(ch)[i]
|
||||
}
|
||||
channelData[i] = sum / channels
|
||||
}
|
||||
}
|
||||
|
||||
// Encode as 16-bit PCM WAV (simplified - we'll use the actual channel data)
|
||||
const sampleRate = buffer.sampleRate
|
||||
const numSamples = buffer.length
|
||||
|
||||
// Get mono data properly
|
||||
let samples
|
||||
if (buffer.numberOfChannels === 1) {
|
||||
samples = buffer.getChannelData(0)
|
||||
} else {
|
||||
const monoSamples = new Float32Array(numSamples)
|
||||
for (let i = 0; i < numSamples; i++) {
|
||||
let sum = 0
|
||||
for (let ch = 0; ch < buffer.numberOfChannels; ch++) {
|
||||
sum += buffer.getChannelData(ch)[i]
|
||||
}
|
||||
monoSamples[i] = sum / buffer.numberOfChannels
|
||||
}
|
||||
}
|
||||
|
||||
// Convert float32 [-1, 1] to int16 PCM
|
||||
const pcmData = new Int16Array(numSamples)
|
||||
for (let i = 0; i < numSamples; i++) {
|
||||
const s = Math.max(-1, Math.min(1, samples[i]))
|
||||
pcmData[i] = s < 0 ? s * 32768 : s * 32767
|
||||
}
|
||||
|
||||
// Build WAV file (RIFF format)
|
||||
const wavBuffer = new ArrayBuffer(44 + numSamples * 2)
|
||||
const view = new DataView(wavBuffer)
|
||||
|
||||
// RIFF header
|
||||
writeString(view, 0, 'RIFF')
|
||||
view.setUint32(4, 36 + numSamples * 2, true)
|
||||
writeString(view, 8, 'WAVE')
|
||||
|
||||
// fmt chunk
|
||||
writeString(view, 12, 'fmt ')
|
||||
view.setUint32(16, 16, true) // chunk size
|
||||
view.setUint16(20, 1, true) // PCM format
|
||||
view.setUint16(22, 1, true) // mono channels
|
||||
view.setUint32(24, sampleRate, true) // sample rate
|
||||
view.setUint32(28, sampleRate * 2, true) // byte rate
|
||||
view.setUint16(32, 2, true) // block align
|
||||
view.setUint16(34, 16, true) // bits per sample
|
||||
|
||||
// data chunk
|
||||
writeString(view, 36, 'data')
|
||||
view.setUint32(40, numSamples * 2, true)
|
||||
|
||||
// Write PCM data
|
||||
let offset = 44
|
||||
for (let i = 0; i < numSamples; i++) {
|
||||
view.setInt16(offset, pcmData[i], true)
|
||||
offset += 2
|
||||
}
|
||||
|
||||
// Convert to base64
|
||||
const bytes = new Uint8Array(wavBuffer)
|
||||
let binary = ''
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
binary += String.fromCharCode(bytes[i])
|
||||
}
|
||||
|
||||
resolve(btoa(binary))
|
||||
}
|
||||
|
||||
// Handle resampling if needed
|
||||
const targetSampleRate = 16000
|
||||
|
||||
if (audioBuffer.sampleRate === targetSampleRate) {
|
||||
processBuffer(audioBuffer).catch(reject)
|
||||
} else {
|
||||
const offlineCtx = new OfflineAudioContext(1, Math.ceil(audioBuffer.duration * targetSampleRate), targetSampleRate)
|
||||
const source = offlineCtx.createBufferSource()
|
||||
source.buffer = audioBuffer
|
||||
source.connect(offlineCtx.destination)
|
||||
|
||||
offlineCtx.oncomplete = (e) => {
|
||||
processBuffer(e.renderedBuffer).catch(reject)
|
||||
}
|
||||
|
||||
offlineCtx.startRendering()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function writeString(view, offset, string) {
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
view.setUint8(offset + i, string.charCodeAt(i))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert audio file to WAV base64 (16kHz mono, 16-bit PCM)
|
||||
* Uses Web Audio API to decode and resample if needed.
|
||||
*/
|
||||
export async function audioToWavBase64(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
|
||||
reader.onload = async () => {
|
||||
try {
|
||||
const arrayBuffer = reader.result
|
||||
|
||||
// Decode audio data using Web Audio API
|
||||
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
|
||||
|
||||
// Use a short timeout to avoid hanging on unsupported formats
|
||||
const decodePromise = audioContext.decodeAudioData(arrayBuffer.slice(0))
|
||||
|
||||
// Set a timeout (10 seconds)
|
||||
const timeoutPromise = new Promise((_, rej) => {
|
||||
setTimeout(() => rej(new Error('音频解码超时,格式可能不支持')), 10000)
|
||||
})
|
||||
|
||||
const audioBuffer = await Promise.race([decodePromise, timeoutPromise])
|
||||
|
||||
// Close the context
|
||||
audioContext.close()
|
||||
|
||||
const wavBase64 = await audioBufferToWavBase64(audioBuffer)
|
||||
resolve(wavBase64)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
}
|
||||
|
||||
reader.onerror = () => reject(reader.error || new Error('Failed to read audio file'))
|
||||
reader.readAsArrayBuffer(file)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert audio file to text using ASR endpoint.
|
||||
* Returns the recognized text string.
|
||||
*/
|
||||
export async function convertAudioToText(file, language = 'zh-CN') {
|
||||
// Step 1: Convert to WAV base64 (handles format conversion)
|
||||
const wavBase64 = await audioToWavBase64(file)
|
||||
|
||||
// Step 2: Send to ASR endpoint
|
||||
const res = await fetch(ASR_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': 'your-secret-key-here',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
audio_base64: wavBase64,
|
||||
language: language || 'zh-CN',
|
||||
}),
|
||||
})
|
||||
|
||||
if (res.status === 501) {
|
||||
throw new Error('ASR 功能不可用,当前环境不支持语音识别')
|
||||
}
|
||||
|
||||
const data = await consumeSseResult(res)
|
||||
if (!data || typeof data.text !== 'string') {
|
||||
throw new Error('ASR 返回结果为空')
|
||||
}
|
||||
|
||||
return data.text
|
||||
}
|
||||
|
||||
+24
-1
@@ -17,6 +17,8 @@ function clipDocContext(content = '', limit = 0) {
|
||||
return `${content.slice(0, limit)}...`
|
||||
}
|
||||
|
||||
const AUDIO_EXT_RE = /\.(wav|mp3|m4a|ogg|flac)$/i
|
||||
|
||||
export function normalizeDocType(value = '') {
|
||||
const lower = String(value || '').trim().toLowerCase()
|
||||
if (lower === 'txt' || lower === 'text' || lower === 'plain') return 'txt'
|
||||
@@ -26,6 +28,12 @@ export function normalizeDocType(value = '') {
|
||||
if (lower === 'doc' || lower === 'docx' || lower === 'word') return 'docx'
|
||||
if (lower === 'ppt' || lower === 'pptx' || lower === 'powerpoint') return 'pptx'
|
||||
if (lower === 'pdf') return 'pdf'
|
||||
// Audio types - map to their extensions for doc block display
|
||||
if (lower === 'wav' || lower === 'wave') return 'wav'
|
||||
if (lower === 'mp3' || lower === 'mpeg') return 'mp3'
|
||||
if (lower === 'm4a' || lower === 'aac') return 'm4a'
|
||||
if (lower === 'ogg' || lower === 'opus') return 'ogg'
|
||||
if (lower === 'flac') return 'flac'
|
||||
return 'txt'
|
||||
}
|
||||
|
||||
@@ -37,9 +45,22 @@ export function getDocTypeFromFilename(name = '') {
|
||||
if (lower.endsWith('.json')) return 'json'
|
||||
if (lower.endsWith('.toml')) return 'toml'
|
||||
if (lower.endsWith('.yaml') || lower.endsWith('.yml')) return 'yaml'
|
||||
// Audio types - preserve the actual extension for display
|
||||
if (lower.endsWith('.wav')) return 'wav'
|
||||
if (lower.endsWith('.mp3') || lower.endsWith('.mpeg')) return 'mp3'
|
||||
if (lower.endsWith('.m4a') || lower.endsWith('.aac')) return 'm4a'
|
||||
if (lower.endsWith('.ogg') || lower.endsWith('.opus')) return 'ogg'
|
||||
if (lower.endsWith('.flac')) return 'flac'
|
||||
return 'txt'
|
||||
}
|
||||
|
||||
export function isAudioFile(file) {
|
||||
if (!file) return false
|
||||
const name = String(file.name || '').toLowerCase()
|
||||
const type = String(file.type || '').toLowerCase()
|
||||
return AUDIO_EXT_RE.test(name) || type.startsWith('audio/')
|
||||
}
|
||||
|
||||
export function isSupportedDocFile(file) {
|
||||
if (!file) return false
|
||||
const name = String(file.name || '').toLowerCase()
|
||||
@@ -53,6 +74,7 @@ export function isSupportedDocFile(file) {
|
||||
name.endsWith('.docx') ||
|
||||
name.endsWith('.pptx') ||
|
||||
name.endsWith('.pdf') ||
|
||||
AUDIO_EXT_RE.test(name) ||
|
||||
type === 'text/plain' ||
|
||||
type === 'application/json' ||
|
||||
type === 'text/yaml' ||
|
||||
@@ -60,7 +82,8 @@ export function isSupportedDocFile(file) {
|
||||
type === 'application/x-yaml' ||
|
||||
type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
|
||||
type === 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ||
|
||||
type === 'application/pdf'
|
||||
type === 'application/pdf' ||
|
||||
type.startsWith('audio/')
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -67,3 +67,33 @@ export function extractTextFromOCR(ocrText, maxLen = 100) {
|
||||
if (text.toLowerCase() === '(none)') return ''
|
||||
return text.length > maxLen ? text.substring(0, maxLen) + '...' : text
|
||||
}
|
||||
|
||||
const IMAGE_NODE_TYPES = new Set(['image', 'image-block', 'imageBlock'])
|
||||
|
||||
/**
|
||||
* 从 ProseMirror doc 中提取 OCR 上下文,供 AI 补全使用。
|
||||
* @param {ProseNode} doc - ProseMirror document node
|
||||
* @param {number} maxLen - OCR 文本最大长度
|
||||
* @returns {string}
|
||||
*/
|
||||
export function buildOcrContextForDoc(doc, maxLen = 120) {
|
||||
const lines = []
|
||||
|
||||
doc.descendants((node) => {
|
||||
if (!IMAGE_NODE_TYPES.has(node.type.name)) return true
|
||||
const src = typeof node.attrs?.src === 'string' ? node.attrs.src : ''
|
||||
if (!src) return true
|
||||
|
||||
const ocrText = getOcrCache(src)
|
||||
const preview = ocrText ? extractTextFromOCR(ocrText, maxLen) : ''
|
||||
if (!preview) return true
|
||||
|
||||
const label = typeof node.attrs?.alt === 'string' && node.attrs.alt.trim()
|
||||
? node.attrs.alt.trim()
|
||||
: 'image'
|
||||
lines.push(` <OCR:${preview}>`)
|
||||
return true
|
||||
})
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
const MARKDOWN_FENCE_RE = /^(`{3,}|~{3,})[ \t]*(markdown|md|mdown|text|plain|plaintext)[^\n]*\n([\s\S]*?)\n\1[ \t]*$/i
|
||||
|
||||
function normalizeNewlines(value = '') {
|
||||
return String(value || '').replace(/\r\n?/g, '\n')
|
||||
}
|
||||
|
||||
export function normalizeProAcceptMarkdown(value = '') {
|
||||
let text = normalizeNewlines(value)
|
||||
const trimmed = text.trim()
|
||||
|
||||
if (!trimmed) return ''
|
||||
|
||||
if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed)
|
||||
if (typeof parsed === 'string') {
|
||||
text = normalizeNewlines(parsed)
|
||||
}
|
||||
} catch {
|
||||
// Keep the original response when it is not a JSON string literal.
|
||||
}
|
||||
}
|
||||
|
||||
const fenceMatch = text.trim().match(MARKDOWN_FENCE_RE)
|
||||
if (fenceMatch) {
|
||||
return normalizeNewlines(fenceMatch[3]).trim()
|
||||
}
|
||||
|
||||
return text.trim()
|
||||
}
|
||||
|
||||
export function splitPlainTextFallbackBlocks(value = '') {
|
||||
const text = normalizeNewlines(value).trim()
|
||||
if (!text) return []
|
||||
return text.split(/\n{2,}/).map((block) => block.trim()).filter(Boolean)
|
||||
}
|
||||
Reference in New Issue
Block a user