This commit is contained in:
seb
2026-10-05 11:29:41 +02:00
parent 25266cfcec
commit 4a08e7f20b
6 changed files with 72 additions and 11 deletions

View File

@@ -294,7 +294,7 @@ async function parseDocumentManifest({ req, res, pages, sourceLabel }) {
});
// ── LLMOrbiter setup ─────────────────────────────────────────────
const model = 'stealth/space-bunny-alpha';//'~openai/gpt-luna-latest:floor';
const model = env.defaultModel;
const provider = new OpenRouterProvider({ apiKey });
const orbiter = new LLMOrbiter(provider, {
onStatus: (status) => {

View File

@@ -1,4 +1,9 @@
import * as db from '../../db/modelconversation.js';
import env from '../../config/env.js';
// Model ids that are known to return empty responses from OpenRouter. Requests naming one of
// these are transparently rerouted to `env.defaultModel` instead of failing at generation time.
const RETIRED_MODELS = new Set(['stealth/space-bunny-alpha']);
import { emit } from '../sse.js';
import { getOrCreateManager, removeManager } from '../../services/conversationManager.js';
@@ -145,8 +150,16 @@ export function register(app, basePath = '') {
const { provider, model } = req.body ?? {};
// Clients may still send the retired `stealth/space-bunny-alpha` id, which now returns
// empty responses from the provider. Map it onto the configured default rather than
// ignoring the client's choice entirely, so an explicit model selection still works.
const requestedModel = typeof model === 'string' ? model.trim() : '';
const effectiveModel = RETIRED_MODELS.has(requestedModel) || !requestedModel
? env.defaultModel
: requestedModel;
try {
const conversation = await db.create(req.user.id, { provider, model });
const conversation = await db.create(req.user.id, { provider, model: effectiveModel });
emit('modelconversation', { companyId: selected.companyId });
res.status(201).json({ conversation });
} catch (error) {

View File

@@ -28,6 +28,10 @@ export default {
dbProvider: process.env.DB_PROVIDER || (process.env.SQLITE_PATH ? 'sqlite' : 'mssql'),
openrouterFreeKey: process.env.OPENROUTER_FREE_KEY,
serperApiKey: process.env.SERPER_API_KEY,
// Default model for agent conversations. `openrouter/free` routes to whatever free model
// OpenRouter currently offers, which avoids pinning to a specific upstream id that can be
// withdrawn or become unavailable. A conversation's stored model still wins when set.
defaultModel: process.env.OPENROUTER_MODEL || 'openrouter/free',
sqlite: {
filename: process.env.SQLITE_PATH || process.env.SQLITE_FILENAME || resolve(__dirname, '../../data/dash.db'),
},

View File

@@ -345,6 +345,12 @@ export class ConversationManager {
if (conversation.provider !== 'openrouter') {
throw new Error(`Unsupported provider: ${conversation.provider}`);
}
// Fall back to the configured default when the stored model is missing or blank, so
// an old or malformed conversation row cannot send an empty `model` and make the
// provider return an empty response.
const model = (typeof conversation.model === 'string' && conversation.model.trim())
? conversation.model.trim()
: env.defaultModel;
const apiKey = await resolveOpenRouterKey(googleAuthId);
// Transcript comes from the database (includes user, assistant, tool, reasoning).
@@ -459,16 +465,37 @@ export class ConversationManager {
}
},
onUsage: (u) => {
// A call that consumed 0 input tokens AND produced 0 output means the
// provider returned nothing at all — not a real generation. LLMOrbiter
// treats "no content and no tool calls" as "keep looping", so several of
// these in a row burn iterations for nothing. Flag them loudly so the
// cause is visible instead of showing up as a silent weird loop.
if (u.input === 0 && u.output === 0) {
console.warn(
`[conversation ${this.conversationId}] EMPTY LLM RESPONSE ` +
`(iteration reported no usage at all). The provider returned no content ` +
`and no tool calls, so the orchestrator will retry. ` +
`If this repeats, the upstream model/API is failing — check the model id ` +
`and the key.`
);
}
console.log(
`[conversation ${this.conversationId}] usage: input=${u.input}, cached=${u.cached}, output=${u.output}` +
(u.cost != null ? `, cost=$${u.cost.toFixed(4)}` : '')
);
},
onLLMCall: (call) => {
console.log(
`[conversation ${this.conversationId}] LLM call #${call.iteration}: ` +
`model=${call.model}, messages=${call.messageCount}, flow=${call.messages}, ` +
`promptChars=${call.promptSize}`
);
},
});
const result = await this.orbiter.run({
messages,
model: conversation.model,
model,
tools: [
createDashbackRequestTool({ googleAuthId }),
createAskUserTool(),
@@ -481,7 +508,7 @@ export class ConversationManager {
createPatchTool({ googleAuthId }),
createRmTool({ googleAuthId }),
createSearchImagesTool({ googleAuthId }),
createAnalyzeImageTool({ googleAuthId, model: conversation.model }),
createAnalyzeImageTool({ googleAuthId, model }),
createResetVfsTool({ googleAuthId }),
createApiSuggestionsOrIssuesTool({ googleAuthId, companyId: this.companyId }),
],
@@ -489,6 +516,17 @@ export class ConversationManager {
if (this.destroyed) return;
// Log the shape of every run, including degenerate ones. When nothing comes back
// there is nothing persisted and the UI just sits there — this line is the only
// clue, so make it explicit about which field was empty.
console.log(
`[conversation ${this.conversationId}] run finished: ` +
`iterations=${result?.iterations}, ` +
`content=${result?.content ? `${result.content.length} chars` : 'EMPTY'}, ` +
`toolCalls=${result?.toolResults ? Object.keys(result.toolResults).length : 0}, ` +
`directReturn=${result?.directReturn === true}`
);
// Direct return (e.g. from ask_user) halts without adding an additional assistant message.
if (result?.directReturn) {
return;
@@ -500,6 +538,16 @@ export class ConversationManager {
: undefined;
const finalReasoning = result.reasoning || currentReasoning.trim() || undefined;
// Nothing came back at all: no content, no reasoning. Surface it to the user rather
// than silently returning to `idle` with an empty turn, which reads as a hang.
if (!content && !finalReasoningDetails && !finalReasoning) {
console.error(
`[conversation ${this.conversationId}] generation produced no content after ` +
`${result?.iterations ?? 0} iteration(s). The model returned neither text nor tool calls.`
);
error = 'The model returned an empty response. Please try again.';
}
if (content || finalReasoningDetails || finalReasoning) {
const assistantMsg = await db.addMessage(googleAuthId, this.conversationId, {
message: JSON.stringify({