Remove CRM chat fallback responses
This commit is contained in:
@@ -111,10 +111,14 @@ export async function runCrmAgentFor(
|
|||||||
process.env.QWEN_API_KEY;
|
process.env.QWEN_API_KEY;
|
||||||
const hasGigachat = Boolean((process.env.GIGACHAT_AUTH_KEY ?? "").trim() && (process.env.GIGACHAT_SCOPE ?? "").trim());
|
const hasGigachat = Boolean((process.env.GIGACHAT_AUTH_KEY ?? "").trim() && (process.env.GIGACHAT_SCOPE ?? "").trim());
|
||||||
|
|
||||||
if (mode !== "rule" && (llmApiKey || hasGigachat)) {
|
if (mode !== "rule") {
|
||||||
return runLangGraphCrmAgentFor(input);
|
return runLangGraphCrmAgentFor(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!llmApiKey && !hasGigachat) {
|
||||||
|
throw new Error("LLM API key is not configured. Set OPENROUTER_API_KEY or GIGACHAT_AUTH_KEY/GIGACHAT_SCOPE.");
|
||||||
|
}
|
||||||
|
|
||||||
await ensureDataset({ teamId: input.teamId, userId: input.userId });
|
await ensureDataset({ teamId: input.teamId, userId: input.userId });
|
||||||
const q = normalize(input.userText);
|
const q = normalize(input.userText);
|
||||||
const root = datasetRoot({ teamId: input.teamId, userId: input.userId });
|
const root = datasetRoot({ teamId: input.teamId, userId: input.userId });
|
||||||
@@ -220,18 +224,9 @@ export async function runCrmAgentFor(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default: keep it simple, ask for intent + show what the agent can do.
|
throw new Error(
|
||||||
return {
|
"Rule mode has no fallback responses. Use a supported structured query or switch to langgraph mode with a configured LLM API key.",
|
||||||
plan: ["Уточнить цель", "Выбрать данные для анализа", "Предложить план действий и, если нужно, изменения в CRM"],
|
);
|
||||||
tools: ["read index/contacts.json (по необходимости)", "search messages/events (по необходимости)"],
|
|
||||||
toolRuns: [],
|
|
||||||
text:
|
|
||||||
"Ок. Скажи, что нужно сделать.\n" +
|
|
||||||
"Примеры:\n" +
|
|
||||||
"- \"покажи 10 лучших клиентов\"\n" +
|
|
||||||
"- \"чем мне сегодня заняться\"\n" +
|
|
||||||
"- \"составь план касаний на неделю\"\n",
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function persistChatMessage(input: {
|
export async function persistChatMessage(input: {
|
||||||
|
|||||||
@@ -441,17 +441,9 @@ export async function runLangGraphCrmAgentFor(input: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!llmApiKey) {
|
if (!llmApiKey) {
|
||||||
return {
|
throw new Error(
|
||||||
text: "LLM API key не задан. Сейчас включен fallback-агент без LLM.",
|
"LLM API key is not configured. Set OPENROUTER_API_KEY (or GIGACHAT_AUTH_KEY/GIGACHAT_SCOPE) and restart.",
|
||||||
plan: [
|
);
|
||||||
"Проверить .env",
|
|
||||||
"Добавить OPENROUTER_API_KEY (или LLM_API_KEY / OPENAI_API_KEY / DASHSCOPE_API_KEY / QWEN_API_KEY / GIGACHAT_AUTH_KEY+GIGACHAT_SCOPE)",
|
|
||||||
"Перезапустить dev-сервер",
|
|
||||||
],
|
|
||||||
tools: [],
|
|
||||||
thinking: ["LLM недоступна, возвращен fallback-ответ."],
|
|
||||||
toolRuns: [],
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep the dataset fresh so the "CRM filesystem" stays in sync with DB.
|
// Keep the dataset fresh so the "CRM filesystem" stays in sync with DB.
|
||||||
@@ -1002,7 +994,7 @@ export async function runLangGraphCrmAgentFor(input: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const extractResult = (res: any) => {
|
const extractResult = (res: any) => {
|
||||||
const fallbackText = (() => {
|
const extractedText = (() => {
|
||||||
const messages = Array.isArray(res?.messages) ? res.messages : [];
|
const messages = Array.isArray(res?.messages) ? res.messages : [];
|
||||||
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
||||||
const msg = messages[i];
|
const msg = messages[i];
|
||||||
@@ -1015,7 +1007,7 @@ export async function runLangGraphCrmAgentFor(input: {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
text: fallbackText.trim(),
|
text: extractedText.trim(),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1063,16 +1055,13 @@ export async function runLangGraphCrmAgentFor(input: {
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
await emitTrace({ text: "Один из шагов завершился ошибкой, пробую безопасный обход." });
|
await emitTrace({ text: "Один из шагов завершился ошибкой." });
|
||||||
cycleSpan?.end({
|
cycleSpan?.end({
|
||||||
output: "error",
|
output: "error",
|
||||||
level: "ERROR",
|
level: "ERROR",
|
||||||
statusMessage: String(e?.message ?? e ?? "unknown_error"),
|
statusMessage: String(e?.message ?? e ?? "unknown_error"),
|
||||||
});
|
});
|
||||||
if (!finalText) {
|
throw e;
|
||||||
finalText = "Не удалось завершить задачу за отведенное время. Уточни запрос или сократи объем.";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = extractResult(res);
|
const parsed = extractResult(res);
|
||||||
|
|||||||
Reference in New Issue
Block a user