Enhance chat handling.

This commit is contained in:
decolua
2026-01-14 15:42:38 +07:00
parent c39eca6d4e
commit c208f244ee
7 changed files with 145 additions and 137 deletions

View File

@@ -61,19 +61,19 @@ export async function handleChat(request, clientRawRequest = null) {
return handleComboChat({
body,
models: comboModels,
handleSingleModel: (b, m) => handleSingleModelChat(b, m, clientRawRequest),
handleSingleModel: (b, m) => handleSingleModelChat(b, m, clientRawRequest, request),
log
});
}
// Single model request
return handleSingleModelChat(body, modelStr, clientRawRequest);
return handleSingleModelChat(body, modelStr, clientRawRequest, request);
}
/**
* Handle single model chat request
*/
async function handleSingleModelChat(body, modelStr, clientRawRequest = null) {
async function handleSingleModelChat(body, modelStr, clientRawRequest = null, request = null) {
const modelInfo = await getModelInfo(modelStr);
if (!modelInfo.provider) {
log.warn("CHAT", "Invalid model format", { model: modelStr });
@@ -89,6 +89,9 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null) {
log.info("ROUTING", `Provider: ${provider}, Model: ${model}`);
}
// Extract userAgent from request
const userAgent = request?.headers?.get("user-agent") || "";
// Try with available accounts (fallback on errors)
let excludeConnectionId = null;
let lastError = null;
@@ -121,6 +124,7 @@ async function handleSingleModelChat(body, modelStr, clientRawRequest = null) {
log,
clientRawRequest,
connectionId: credentials.connectionId,
userAgent,
onCredentialsRefreshed: async (newCreds) => {
await updateProviderCredentials(credentials.connectionId, {
accessToken: newCreds.accessToken,