Repository: moeru-ai/airi
File: packages/stage-ui/src/stores/providers/openai-compatible-builder.ts (TypeScript)

Code before:
      if (!isUrl(config.baseUrl as string) || new URL(config.baseUrl as string).host.length === 0) {
        errors.push(new Error('Base URL is not absolute. Check your input.'))
      }

      if (!(config.baseUrl as string).endsWith('/')) {
        errors.push(new Error('Base URL must end with a trailing slash (/).'))
      }

      if (errors.length > 0) {
        return { errors, reason: errors.map(e => e.message).join(', '), valid: false }
      }

      const validationChecks = validation || []
      let responseModelList = null
      let responseChat = null

      if (validationChecks.includes('health')) {
        try {
          responseChat = await fetch(`${config.baseUrl as string}chat/completions`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders }, method: 'POST', body: '{"model": "test"}' })
          responseModelList = await fetch(`${config.baseUrl as string}models`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders } })

          // Also try transcription endpoints for speech recognition servers
          let responseTranscription = null
          try {
            responseTranscription = await fetch(`${config.baseUrl as string}audio/transcriptions`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders }, method: 'POST', body: new FormData() })
          }
          catch {
            // Transcription endpoint might not exist, that's okay
          }

          // Accept if any of the endpoints work (chat, models, or transcription)
          const validResponses = [responseChat, responseModelList, responseTranscription].filter(r => r && [200, 400, 401].includes(r.status))
          if (validResponses.length === 0) {
            errors.push(new Error(`Invalid Base URL, ${config.baseUrl} is not supported. Make sure your server supports OpenAI-compatible endpoints.`))
          }
        }
        catch (e) {
          errors.push(new Error(`Invalid Base URL, ${(e as Error).message}`))
        }
      }

      if (errors.length > 0) {
        return { errors, reason: errors.map(e => e.message).join(', '), valid: false }
      }

      if (validationChecks.includes('model_list')) {
        try {
          let response = responseModelList
          if (!response) {
            response = await fetch(`${config.baseUrl as string}models`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders } })
          }

Review comment (bug):
```suggestion
            responseTranscription = await fetch(`${config.baseUrl as string}audio/transcriptions`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders }, method: 'POST', body: new FormData() })
```

If no audio file uploaded, upstream server may still respond with 400, which means L99 may fail to check, is this expected?

Code after:
      if (!isUrl(config.baseUrl as string) || new URL(config.baseUrl as string).host.length === 0) {
        errors.push(new Error('Base URL is not absolute. Check your input.'))
      }

      if (!(config.baseUrl as string).endsWith('/')) {
        errors.push(new Error('Base URL must end with a trailing slash (/).'))
      }

      if (errors.length > 0) {
        return { errors, reason: errors.map(e => e.message).join(', '), valid: false }
      }

      const validationChecks = validation || []
      let responseModelList = null
      let responseChat = null

      if (validationChecks.includes('health')) {
        try {
          responseChat = await fetch(`${config.baseUrl as string}chat/completions`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders }, method: 'POST', body: '{"model": "test"}' })
          responseModelList = await fetch(`${config.baseUrl as string}models`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders } })

          // Also try transcription endpoints for speech recognition servers
          let responseTranscription = null
          try {
            // Sending empty FormData is fine; 400 still counts as a valid endpoint
            responseTranscription = await fetch(`${config.baseUrl as string}audio/transcriptions`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders }, method: 'POST', body: new FormData() })
          }
          catch {
            // Transcription endpoint might not exist, that's okay
          }

          // Accept if any of the endpoints work (chat, models, or transcription)
          const validResponses = [responseChat, responseModelList, responseTranscription].filter(r => r && [200, 400, 401].includes(r.status))
          if (validResponses.length === 0) {
            errors.push(new Error(`Invalid Base URL, ${config.baseUrl} is not supported. Make sure your server supports OpenAI-compatible endpoints.`))
          }
        }
        catch (e) {
          errors.push(new Error(`Invalid Base URL, ${(e as Error).message}`))
        }
      }

      if (errors.length > 0) {
        return { errors, reason: errors.map(e => e.message).join(', '), valid: false }
      }

      if (validationChecks.includes('model_list')) {
        try {
          let response = responseModelList
          if (!response) {
            response = await fetch(`${config.baseUrl as string}models`, { headers: { Authorization: `Bearer ${config.apiKey}`, ...additionalHeaders } })