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
6 changes: 3 additions & 3 deletions hooks/use-polish-flow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { useEffect, useRef, useState } from 'react'
import { normalizeSettingsForStorage, type Settings } from '@/lib/app-settings'
import { normalizeLlmRunSettings, type LlmRunSettings } from '@/lib/app-settings'
import { readJsonResponse, readResponseErrorMessage } from '@/lib/http-response'
import { consumePolishStream, getPolishCompletionLog } from '@/lib/polish-stream'
import {
Expand All @@ -20,7 +20,7 @@ export const usePolishFlow = ({
settings,
addLog,
}: {
settings: Settings
settings: LlmRunSettings
addLog: AddLog
}) => {
const [result, setResult] = useState<PolishResult>(IDLE_POLISH_RESULT)
Expand Down Expand Up @@ -53,7 +53,7 @@ export const usePolishFlow = ({
const controller = new AbortController()
abortRef.current = controller

const effectiveSettings = normalizeSettingsForStorage(settings)
const effectiveSettings = normalizeLlmRunSettings(settings)
setCopied(false)
setResult({ kind: 'streaming', text: '' })

Expand Down
13 changes: 12 additions & 1 deletion lib/app-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type Settings = {
customInstructions: string
}

export type AsrRunSettings = Pick<Settings, 'apiKey' | 'apiUrl' | 'model'>
export type LlmRunSettings = Pick<Settings, 'llmApiUrl' | 'llmModel' | 'llmApiKey' | 'customInstructions'>

export const DEFAULT_ASR_API_URL = 'https://api.siliconflow.cn/v1/audio/transcriptions'
export const DEFAULT_ASR_MODEL = 'TeleAI/TeleSpeechASR'
export const DEFAULT_LLM_API_URL = 'https://juya.owl.ci/v1'
Expand Down Expand Up @@ -43,16 +46,24 @@ export const applySettingsDefaults = (input: Partial<Settings>): Settings => ({
customInstructions: readConfiguredValue(input.customInstructions, DEFAULT_SETTINGS.customInstructions),
})

export const normalizeSettingsForStorage = (input: Settings): Settings => ({
export const normalizeAsrRunSettings = (input: AsrRunSettings): AsrRunSettings => ({
apiKey: input.apiKey.trim(),
apiUrl: input.apiUrl.trim() || DEFAULT_SETTINGS.apiUrl,
model: input.model.trim() || DEFAULT_SETTINGS.model,
})

export const normalizeLlmRunSettings = (input: LlmRunSettings): LlmRunSettings => ({
llmApiUrl: input.llmApiUrl.trim() || DEFAULT_SETTINGS.llmApiUrl,
llmModel: input.llmModel.trim() || DEFAULT_SETTINGS.llmModel,
llmApiKey: input.llmApiKey.trim(),
customInstructions: input.customInstructions.trim() || DEFAULT_SETTINGS.customInstructions,
})
Comment thread
makoMakoGo marked this conversation as resolved.

export const normalizeSettingsForStorage = (input: Settings): Settings => ({
...normalizeAsrRunSettings(input),
...normalizeLlmRunSettings(input),
})

export const settingsFromEnv = (env: Partial<EnvMap>): Settings =>
applySettingsDefaults({
apiKey: env.ASR_API_KEY,
Expand Down
18 changes: 6 additions & 12 deletions lib/asr-transcription.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { normalizeSettingsForStorage, type Settings } from '@/lib/app-settings'
import {
normalizeAsrRunSettings,
type AsrRunSettings,
} from '@/lib/app-settings'

export type AsrRunSettings = Pick<Settings, 'apiKey' | 'apiUrl' | 'model'>
export { normalizeAsrRunSettings, type AsrRunSettings }

export type AsrUploadProgress = {
loaded: number
Expand Down Expand Up @@ -42,16 +45,7 @@ export type AsrTranscriptionOptions = {

const buildAbortError = (): DOMException => new DOMException('Aborted', 'AbortError')

export const normalizeAsrRunSettings = (settings: Settings): AsrRunSettings => {
const normalized = normalizeSettingsForStorage(settings)
return {
apiKey: normalized.apiKey,
apiUrl: normalized.apiUrl,
model: normalized.model,
}
}

export const hasAsrApiKey = (settings: Settings): boolean =>
export const hasAsrApiKey = (settings: AsrRunSettings): boolean =>
normalizeAsrRunSettings(settings).apiKey.length > 0

export const formatAsrApiErrorMessage = (response: AsrTranscriptionResponse): string =>
Expand Down
55 changes: 41 additions & 14 deletions tests/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { describe, expect, it } from 'vitest'
import {
applySettingsDefaults,
DEFAULT_ASR_MODEL,
DEFAULT_LLM_MODEL,
DEFAULT_SETTINGS,
normalizeSettingsForStorage,
} from '@/lib/app-settings'
applySettingsDefaults,
DEFAULT_ASR_MODEL,
DEFAULT_LLM_MODEL,
DEFAULT_SETTINGS,
normalizeAsrRunSettings,
normalizeLlmRunSettings,
normalizeSettingsForStorage,
} from '@/lib/app-settings'
import { formatFileSize } from '@/lib/file-size'
import {
canPolishTranscription,
Expand Down Expand Up @@ -61,9 +63,9 @@ describe('app-settings', () => {
})
})

it('normalizes persisted settings without losing explicit secrets', () => {
expect(
normalizeSettingsForStorage({
it('normalizes persisted settings without losing explicit secrets', () => {
expect(
normalizeSettingsForStorage({
...DEFAULT_SETTINGS,
apiKey: ' secret ',
apiUrl: ' ',
Expand All @@ -76,10 +78,35 @@ describe('app-settings', () => {
).toEqual({
...DEFAULT_SETTINGS,
apiKey: 'secret',
llmApiKey: 'llm-secret',
})
})
})
llmApiKey: 'llm-secret',
})
})

it('normalizes ASR and LLM runtime slices independently', () => {
const settings = {
...DEFAULT_SETTINGS,
apiKey: ' asr-secret ',
apiUrl: ' ',
model: ' custom-asr ',
llmApiUrl: ' ',
llmModel: ' custom-llm ',
llmApiKey: ' llm-secret ',
customInstructions: ' custom instructions ',
}

expect(normalizeAsrRunSettings(settings)).toEqual({
apiKey: 'asr-secret',
apiUrl: DEFAULT_SETTINGS.apiUrl,
model: 'custom-asr',
})
expect(normalizeLlmRunSettings(settings)).toEqual({
llmApiUrl: DEFAULT_SETTINGS.llmApiUrl,
llmModel: 'custom-llm',
llmApiKey: 'llm-secret',
customInstructions: 'custom instructions',
})
})
})

describe('canPolishTranscription', () => {
it('allows polish only for successful non-empty transcriptions', () => {
Expand All @@ -89,4 +116,4 @@ describe('canPolishTranscription', () => {
expect(canPolishTranscription({ kind: 'success', text: ' ' })).toBe(false)
expect(canPolishTranscription({ kind: 'success', text: 'hello' })).toBe(true)
})
})
})
Loading