mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 07:34:25 +00:00
* feat: configure embedding servers and transcript agents * fix(agents): guide Transcript Agent setup * fix(agents): require a configured embedding server URL * docs: consolidate embedding and transcript changelog entry * fix(webui): stabilize transcript menu interactions * test(e2e): verify transcript scroll interruption by position * test(webui): check transcript following without frame timing races
106 lines
3.8 KiB
TypeScript
106 lines
3.8 KiB
TypeScript
import {
|
|
resolveIntegrationConfig,
|
|
updateIntegrationSettings,
|
|
type IntegrationDefinition,
|
|
} from '@shumai/core/src/integrations/helper'
|
|
import {
|
|
updateEmbeddingServerSettingsRequestSchema,
|
|
type EmbeddingServerSettingsResponse,
|
|
} from '@shumai/dtos'
|
|
import { HTTPException } from 'hono/http-exception'
|
|
import { z } from 'zod'
|
|
|
|
const integration: IntegrationDefinition<'embeddingServer'> = {
|
|
name: 'Embedding server',
|
|
settingsKey: 'embeddingServer',
|
|
envKeys: { url: ['GEMMA_EMBEDDING_OPENAI_BASE_URL'] },
|
|
requireSettingsRead: true,
|
|
}
|
|
|
|
export const LOCAL_EMBEDDING_PROVIDER = 'gemma-embedding-2-openai'
|
|
const defaultLocalBaseUrl = 'http://localhost:9379/v1'
|
|
|
|
export function resolveEmbeddingProvider(
|
|
{ url, isEnvConfigured }: EmbeddingServerSettingsResponse,
|
|
providerOverride?: string,
|
|
): string {
|
|
return (
|
|
providerOverride ||
|
|
process.env.EMBEDDING_PROVIDER ||
|
|
(url && !isEnvConfigured ? LOCAL_EMBEDDING_PROVIDER : 'gemini-embedding-2')
|
|
)
|
|
}
|
|
|
|
function getOpenAiBaseUrl(url: string): string {
|
|
const baseUrl = url.replace(/\/embeddings$/, '')
|
|
return baseUrl.endsWith('/v1') ? baseUrl : `${baseUrl}/v1`
|
|
}
|
|
|
|
function getEmbeddingEndpoint(url: string): string {
|
|
return url.endsWith('/embeddings') ? url : `${getOpenAiBaseUrl(url)}/embeddings`
|
|
}
|
|
|
|
const modelListSchema = z.object({
|
|
data: z.array(z.object({ id: z.string() })),
|
|
})
|
|
|
|
export class EmbeddingServerService {
|
|
async resolveRuntimeConfig(teamId?: string, providerOverride?: string) {
|
|
const { url, isEnv } = await resolveIntegrationConfig(integration, teamId ?? null)
|
|
const provider = resolveEmbeddingProvider({ url, isEnvConfigured: isEnv }, providerOverride)
|
|
const localUrl = url || defaultLocalBaseUrl
|
|
return {
|
|
provider,
|
|
embeddingEndpoint: getEmbeddingEndpoint(localUrl),
|
|
transcriptionEndpoint: `${getOpenAiBaseUrl(localUrl)}/audio/transcriptions`,
|
|
}
|
|
}
|
|
|
|
async getSettings(teamId: string): Promise<EmbeddingServerSettingsResponse> {
|
|
const { url, isEnv } = await resolveIntegrationConfig(integration, teamId)
|
|
return { url, isEnvConfigured: isEnv }
|
|
}
|
|
|
|
async updateSettings(teamId: string, url: string): Promise<EmbeddingServerSettingsResponse> {
|
|
const { url: validUrl } = this.validateUrl(url)
|
|
const settings = await updateIntegrationSettings(integration, teamId, { url: validUrl })
|
|
return { url: settings.url, isEnvConfigured: settings.isEnvConfigured }
|
|
}
|
|
|
|
async testConnection(teamId: string, url?: string): Promise<{ ok: boolean; error?: string }> {
|
|
const config = await resolveIntegrationConfig(integration, teamId)
|
|
const target = config.isEnv || url === undefined ? config.url : this.validateUrl(url).url
|
|
if (!target) return { ok: false, error: 'Server URL is required' }
|
|
|
|
const endpoint = getEmbeddingEndpoint(target.replace(/\/+$/, ''))
|
|
try {
|
|
const response = await fetch(endpoint.replace(/\/embeddings$/, '/models'), {
|
|
redirect: 'error',
|
|
signal: AbortSignal.timeout(5000),
|
|
})
|
|
if (!response.ok) {
|
|
await response.body?.cancel()
|
|
return { ok: false, error: `Server returned HTTP ${response.status}` }
|
|
}
|
|
|
|
const models = modelListSchema.safeParse(await response.json().catch(() => undefined))
|
|
if (!models.success || !models.data.data.some(({ id }) => id === 'embeddinggemma-2')) {
|
|
return { ok: false, error: 'Server does not advertise the embeddinggemma-2 model' }
|
|
}
|
|
return { ok: true }
|
|
} catch {
|
|
return { ok: false, error: 'Could not reach the embedding server' }
|
|
}
|
|
}
|
|
|
|
private validateUrl(url: string) {
|
|
const result = updateEmbeddingServerSettingsRequestSchema.safeParse({ url })
|
|
if (!result.success) {
|
|
throw new HTTPException(400, { message: result.error.issues[0]?.message })
|
|
}
|
|
return result.data
|
|
}
|
|
}
|
|
|
|
export const embeddingServerService = new EmbeddingServerService()
|