logs
This commit is contained in:
@@ -17,12 +17,15 @@ import { AssistantChatLogEntity } from './assistant-chat-log.entity';
|
||||
import {
|
||||
AssistantAction,
|
||||
AssistantChatMessage,
|
||||
AssistantChatLog,
|
||||
AssistantPageContext,
|
||||
AssistantChatRequest,
|
||||
AssistantChatResponse,
|
||||
} from './assistant.types';
|
||||
|
||||
type MistralMessage = AssistantChatMessage | { role: 'system'; content: string };
|
||||
type MistralMessage =
|
||||
| AssistantChatMessage
|
||||
| { role: 'system'; content: string };
|
||||
|
||||
interface NormalizedContextItem {
|
||||
id: string;
|
||||
@@ -122,7 +125,8 @@ export class AssistantService {
|
||||
const response = await this.callMistralAgent(userId, messages, context);
|
||||
const actions = this.extractActions(response);
|
||||
const content =
|
||||
this.extractAssistantContent(response) ?? this.createActionContent(actions);
|
||||
this.extractAssistantContent(response) ??
|
||||
this.createActionContent(actions);
|
||||
const latestUserMessage = this.latestUserMessage(messages);
|
||||
|
||||
actions.forEach((action) => {
|
||||
@@ -144,6 +148,21 @@ export class AssistantService {
|
||||
}
|
||||
|
||||
if (!content) {
|
||||
await this.recordChatLog({
|
||||
userId,
|
||||
provider: 'listify',
|
||||
endpoint: 'assistant:emptyResponse',
|
||||
agentId: null,
|
||||
requestPayload: {
|
||||
latestUserMessage: latestUserMessage?.content,
|
||||
actionCount: actions.length,
|
||||
},
|
||||
responsePayload: { actions },
|
||||
statusCode: null,
|
||||
durationMs: 0,
|
||||
assistantContent: null,
|
||||
errorMessage: 'Mistral response was empty.',
|
||||
});
|
||||
throw new ServiceUnavailableException('Mistral response was empty.');
|
||||
}
|
||||
|
||||
@@ -156,6 +175,28 @@ export class AssistantService {
|
||||
};
|
||||
}
|
||||
|
||||
async listChatLogs(userId: string): Promise<AssistantChatLog[]> {
|
||||
const logs = await this.chatLogsRepository.find({
|
||||
where: { userId },
|
||||
order: { createdAt: 'DESC' },
|
||||
take: 50,
|
||||
});
|
||||
|
||||
return logs.slice(0, 50).map((log) => ({
|
||||
id: log.id,
|
||||
provider: log.provider,
|
||||
endpoint: log.endpoint,
|
||||
agentId: log.agentId,
|
||||
statusCode: log.statusCode,
|
||||
durationMs: log.durationMs,
|
||||
requestPayload: log.requestPayload,
|
||||
responsePayload: log.responsePayload,
|
||||
assistantContent: log.assistantContent,
|
||||
errorMessage: log.errorMessage,
|
||||
createdAt: log.createdAt.toISOString(),
|
||||
}));
|
||||
}
|
||||
|
||||
private async tryHandleLocalListQuery(
|
||||
userId: string,
|
||||
messages: AssistantChatMessage[],
|
||||
@@ -173,7 +214,10 @@ export class AssistantService {
|
||||
const visibleLists = wantsOpenLists
|
||||
? lists.filter((list) => !this.isCompletedList(list))
|
||||
: lists;
|
||||
const assistantContent = this.formatListsAnswer(visibleLists, wantsOpenLists);
|
||||
const assistantContent = this.formatListsAnswer(
|
||||
visibleLists,
|
||||
wantsOpenLists,
|
||||
);
|
||||
|
||||
await this.recordChatLog({
|
||||
userId,
|
||||
@@ -210,7 +254,9 @@ export class AssistantService {
|
||||
}
|
||||
|
||||
const latestUserMessage = this.latestUserMessage(messages);
|
||||
const itemTitle = this.extractItemTitleToAdd(latestUserMessage?.content ?? '');
|
||||
const itemTitle = this.extractItemTitleToAdd(
|
||||
latestUserMessage?.content ?? '',
|
||||
);
|
||||
|
||||
if (!itemTitle) {
|
||||
return null;
|
||||
@@ -384,15 +430,6 @@ export class AssistantService {
|
||||
): Promise<MistralAgentCompletionResponse> {
|
||||
const apiKey = process.env.MISTRAL_API_KEY;
|
||||
const agentId = process.env.MISTRAL_AGENT_ID;
|
||||
|
||||
if (!apiKey) {
|
||||
throw new ServiceUnavailableException('Mistral API key is not configured.');
|
||||
}
|
||||
|
||||
if (!agentId) {
|
||||
throw new ServiceUnavailableException('Mistral agent id is not configured.');
|
||||
}
|
||||
|
||||
const contextMessage = this.createContextSystemMessage(context);
|
||||
const requestPayload = {
|
||||
agent_id: agentId,
|
||||
@@ -419,6 +456,39 @@ export class AssistantService {
|
||||
type: 'text',
|
||||
},
|
||||
};
|
||||
|
||||
if (!apiKey) {
|
||||
await this.recordChatLog({
|
||||
userId,
|
||||
agentId: agentId ?? null,
|
||||
requestPayload,
|
||||
responsePayload: null,
|
||||
statusCode: null,
|
||||
durationMs: 0,
|
||||
assistantContent: null,
|
||||
errorMessage: 'Mistral API key is not configured.',
|
||||
});
|
||||
throw new ServiceUnavailableException(
|
||||
'Mistral API key is not configured.',
|
||||
);
|
||||
}
|
||||
|
||||
if (!agentId) {
|
||||
await this.recordChatLog({
|
||||
userId,
|
||||
agentId: null,
|
||||
requestPayload,
|
||||
responsePayload: null,
|
||||
statusCode: null,
|
||||
durationMs: 0,
|
||||
assistantContent: null,
|
||||
errorMessage: 'Mistral agent id is not configured.',
|
||||
});
|
||||
throw new ServiceUnavailableException(
|
||||
'Mistral agent id is not configured.',
|
||||
);
|
||||
}
|
||||
|
||||
const startedAt = Date.now();
|
||||
let statusCode: number | null = null;
|
||||
let responsePayload: unknown = null;
|
||||
@@ -658,13 +728,17 @@ export class AssistantService {
|
||||
}
|
||||
|
||||
private createActionContent(actions: AssistantAction[]): string | null {
|
||||
const createdList = actions.find((action) => action.type === 'list.created');
|
||||
const createdList = actions.find(
|
||||
(action) => action.type === 'list.created',
|
||||
);
|
||||
|
||||
if (createdList) {
|
||||
return `Ich habe die Liste **${createdList.list.name}** angelegt.`;
|
||||
}
|
||||
|
||||
const addedItem = actions.find((action) => action.type === 'list.item_added');
|
||||
const addedItem = actions.find(
|
||||
(action) => action.type === 'list.item_added',
|
||||
);
|
||||
|
||||
if (addedItem) {
|
||||
return `Ich habe **${addedItem.itemTitle}** zu **${addedItem.list.name}** hinzugefuegt.`;
|
||||
|
||||
Reference in New Issue
Block a user