Add GitHub token validation and improve TTS configuration

- Add git token validation endpoint and UI validation
- Improve TTS endpoint normalization and discovery
- Move shared types to workspace package
- Add better error handling for TTS API calls
- Support composite voices for Kokoro-style TTS
- Auto-fetch TTS models and voices on config change
This commit is contained in:
Chris Scott committed 2025-12-15 14:43:03 -05:00
1 parent 406ff8fd4a
commit c7de65290a
11 files changed
+410 -154

No files matched your search

+75
View File
@@ -12,6 +12,11 @@ import {
import { logger } from '../utils/logger'
import { opencodeServerManager } from '../services/opencode-single-server'
import { DEFAULT_AGENTS_MD } from '../index'
import { createGitHubGitEnv } from '../utils/git-auth'
import { exec } from 'child_process'
import { promisify } from 'util'
const execAsync = promisify(exec)
const UpdateSettingsSchema = z.object({
preferences: UserPreferencesSchema.partial(),
@@ -41,6 +46,10 @@ const UpdateCustomCommandSchema = z.object({
promptTemplate: z.string().min(1).max(10000),
})
const ValidateGitTokenSchema = z.object({
gitToken: z.string(),
})
export function createSettingsRoutes(db: Database) {
const app = new Hono()
const settingsService = new SettingsService(db)
@@ -370,5 +379,71 @@ export function createSettingsRoutes(db: Database) {
}
})
app.post('/validate-git-token', async (c) => {
try {
const body = await c.req.json()
const { gitToken } = ValidateGitTokenSchema.parse(body)
if (!gitToken) {
return c.json({ valid: true, message: 'No token provided' })
}
// Test the token by trying to access a public GitHub repo via git ls-remote
const testRepoUrl = 'https://github.com/octocat/Hello-World.git'
const env = createGitHubGitEnv(gitToken)
try {
await execAsync(`git ls-remote ${testRepoUrl}`, {
env: { ...process.env, ...env },
timeout: 10000
})
// If command succeeded (exit code 0), token is valid
// stderr may contain warnings but that's ok
return c.json({
valid: true,
message: 'Token is valid'
})
} catch (error) {
logger.error('Git token validation failed:', error)
if (error instanceof Error) {
const errorMsg = error.message.toLowerCase()
if (errorMsg.includes('authentication failed') ||
errorMsg.includes('not authorized') ||
errorMsg.includes('invalid username or token') ||
errorMsg.includes('password authentication is not supported') ||
errorMsg.includes('401') ||
errorMsg.includes('403') ||
errorMsg.includes('code 128')) {
return c.json({
valid: false,
message: 'Invalid GitHub token. Please check your token and permissions.'
})
}
if (errorMsg.includes('timeout') || errorMsg.includes('network')) {
return c.json({
valid: false,
message: 'Network error - could not validate token. Please try again.'
})
}
}
return c.json({
valid: false,
message: 'Failed to validate token: ' + (error instanceof Error ? error.message : 'Unknown error')
})
}
} catch (error) {
logger.error('Token validation endpoint error:', error)
if (error instanceof z.ZodError) {
return c.json({ error: 'Invalid request data', details: error.issues }, 400)
}
return c.json({ error: 'Failed to validate token' }, 500)
}
})
return app
}
+64 -14
View File
@@ -23,6 +23,13 @@ function generateCacheKey(text: string, voice: string, model: string, speed: num
return hash.digest('hex')
}
function normalizeToBaseUrl(endpoint: string): string {
return endpoint
.replace(/\/v1\/audio\/speech$/, '')
.replace(/\/audio\/speech$/, '')
.replace(/\/$/, '')
}
async function ensureCacheDir(): Promise<void> {
await mkdir(TTS_CACHE_DIR, { recursive: true })
}
@@ -85,11 +92,10 @@ function generateDiscoveryCacheKey(endpoint: string, apiKey: string, type: 'mode
}
async function fetchAvailableModels(endpoint: string, apiKey: string): Promise<string[]> {
// Try different endpoint formats for models
const baseUrl = normalizeToBaseUrl(endpoint)
const endpointVariations = [
`${endpoint.replace(/\/audio\/speech$/, '')}/v1/models`,
`${endpoint.replace(/\/audio\/speech$/, '')}/models`,
`${endpoint.replace(/\/v1\/audio\/speech$/, '')}/v1/models`,
`${baseUrl}/v1/models`,
`${baseUrl}/models`,
]
for (const modelEndpoint of endpointVariations) {
@@ -120,7 +126,7 @@ async function fetchAvailableModels(endpoint: string, apiKey: string): Promise<s
}
}
} catch (error) {
logger.debug(`Failed to fetch models from ${modelEndpoint}:`, error)
logger.warn(`Failed to fetch models from ${modelEndpoint}:`, error)
continue
}
}
@@ -129,12 +135,11 @@ async function fetchAvailableModels(endpoint: string, apiKey: string): Promise<s
}
async function fetchAvailableVoices(endpoint: string, apiKey: string): Promise<string[]> {
// Try different endpoint formats for voices
const baseUrl = normalizeToBaseUrl(endpoint)
const endpointVariations = [
`${endpoint.replace(/\/audio\/speech$/, '')}/v1/audio/voices`,
`${endpoint.replace(/\/audio\/speech$/, '')}/voices`,
`${endpoint.replace(/\/v1\/audio\/speech$/, '')}/v1/audio/voices`,
`${endpoint.replace(/\/v1\/audio\/speech$/, '')}/audio/voices`,
`${baseUrl}/v1/audio/voices`,
`${baseUrl}/voices`,
`${baseUrl}/audio/voices`,
]
for (const voiceEndpoint of endpointVariations) {
@@ -148,7 +153,7 @@ async function fetchAvailableVoices(endpoint: string, apiKey: string): Promise<s
if (response.ok) {
type VoiceItem = { id?: string; name?: string; voice?: string }
const data = await response.json() as { data?: VoiceItem[] } | (string | VoiceItem)[]
const data = await response.json() as { data?: VoiceItem[]; voices?: string[] } | (string | VoiceItem)[]
// Handle different response formats
if ('data' in data && Array.isArray(data.data)) {
@@ -156,6 +161,9 @@ async function fetchAvailableVoices(endpoint: string, apiKey: string): Promise<s
return data.data
.filter((voice) => voice.id || voice.name)
.map((voice) => (voice.id || voice.name)!)
} else if ('voices' in data && Array.isArray(data.voices)) {
// Kokoro-style format: { "voices": ["af_alloy", "af_aoede", ...] }
return data.voices.filter((v): v is string => typeof v === 'string')
} else if (Array.isArray(data)) {
// Simple array format: ["alloy", "echo", ...] or [{ voice: "alloy" }, ...]
return data.map((item) => {
@@ -165,7 +173,7 @@ async function fetchAvailableVoices(endpoint: string, apiKey: string): Promise<s
}
}
} catch (error) {
logger.debug(`Failed to fetch voices from ${voiceEndpoint}:`, error)
logger.warn(`Failed to fetch voices from ${voiceEndpoint}:`, error)
continue
}
}
@@ -244,7 +252,10 @@ export function createTTSRoutes(db: Database) {
logger.info(`TTS cache miss, calling API: ${cacheKey.substring(0, 8)}...`)
const response = await fetch(endpoint, {
const baseUrl = normalizeToBaseUrl(endpoint)
const speechEndpoint = `${baseUrl}/v1/audio/speech`
const response = await fetch(speechEndpoint, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
@@ -263,7 +274,28 @@ export function createTTSRoutes(db: Database) {
const errorText = await response.text()
logger.error(`TTS API error: ${response.status} - ${errorText}`)
const status = response.status >= 400 && response.status < 600 ? response.status as 400 | 500 : 500
return c.json({ error: 'TTS API request failed', details: errorText }, status)
// Try to parse error details for better frontend display
let errorDetails = errorText
try {
const errorJson = JSON.parse(errorText)
if (errorJson.detail?.error?.message) {
errorDetails = errorJson.detail.error.message
} else if (errorJson.detail?.message) {
errorDetails = errorJson.detail.message
} else if (errorJson.message) {
errorDetails = errorJson.message
}
} catch {
// Use raw error text if parsing fails
}
return c.json({
error: 'TTS API request failed',
details: errorDetails,
voice: voice,
availableVoices: ttsConfig?.availableVoices || []
}, status)
}
const audioBuffer = Buffer.from(await response.arrayBuffer())
@@ -317,6 +349,15 @@ export function createTTSRoutes(db: Database) {
const models = await fetchAvailableModels(ttsConfig.endpoint, ttsConfig.apiKey)
await cacheDiscovery(cacheKey, models)
// Update user preferences with available models
await settingsService.updateSettings({
tts: {
...ttsConfig,
availableModels: models,
lastModelsFetch: Date.now()
}
}, userId)
logger.info(`Fetched ${models.length} TTS models`)
return c.json({ models, cached: false })
} catch (error) {
@@ -356,6 +397,15 @@ export function createTTSRoutes(db: Database) {
const voices = await fetchAvailableVoices(ttsConfig.endpoint, ttsConfig.apiKey)
await cacheDiscovery(cacheKey, voices)
// Update user preferences with available voices
await settingsService.updateSettings({
tts: {
...ttsConfig,
availableVoices: voices,
lastVoicesFetch: Date.now()
}
}, userId)
logger.info(`Fetched ${voices.length} TTS voices`)
return c.json({ voices, cached: false })
} catch (error) {