mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
feat: enhance token usage tracking in chat messages with UI integration and dropdown display
This commit is contained in:
parent
3cfe53fb7f
commit
55099a20ac
5 changed files with 137 additions and 8 deletions
|
|
@ -39,13 +39,16 @@ export function convertToThreadMessage(msg: MessageRecord): ThreadMessageLike {
|
|||
content = [{ type: "text", text: String(msg.content) }];
|
||||
}
|
||||
|
||||
const metadata = msg.author_id
|
||||
const metadata = (msg.author_id || msg.token_usage)
|
||||
? {
|
||||
custom: {
|
||||
author: {
|
||||
displayName: msg.author_display_name ?? null,
|
||||
avatarUrl: msg.author_avatar_url ?? null,
|
||||
},
|
||||
...(msg.author_id && {
|
||||
author: {
|
||||
displayName: msg.author_display_name ?? null,
|
||||
avatarUrl: msg.author_avatar_url ?? null,
|
||||
},
|
||||
}),
|
||||
...(msg.token_usage && { usage: msg.token_usage }),
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
|
|
|
|||
|
|
@ -238,6 +238,16 @@ export type SSEEvent =
|
|||
| { type: "data-thread-title-update"; data: { threadId: number; title: string } }
|
||||
| { type: "data-interrupt-request"; data: Record<string, unknown> }
|
||||
| { type: "data-documents-updated"; data: Record<string, unknown> }
|
||||
| {
|
||||
type: "data-token-usage";
|
||||
data: {
|
||||
usage: Record<string, { prompt_tokens: number; completion_tokens: number; total_tokens: number }>;
|
||||
prompt_tokens: number;
|
||||
completion_tokens: number;
|
||||
total_tokens: number;
|
||||
call_details: Array<{ model: string; prompt_tokens: number; completion_tokens: number; total_tokens: number }>;
|
||||
};
|
||||
}
|
||||
| { type: "error"; errorText: string };
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -26,6 +26,13 @@ export interface ThreadRecord {
|
|||
has_comments?: boolean;
|
||||
}
|
||||
|
||||
export interface TokenUsageSummary {
|
||||
prompt_tokens: number;
|
||||
completion_tokens: number;
|
||||
total_tokens: number;
|
||||
model_breakdown?: Record<string, { prompt_tokens: number; completion_tokens: number; total_tokens: number }> | null;
|
||||
}
|
||||
|
||||
export interface MessageRecord {
|
||||
id: number;
|
||||
thread_id: number;
|
||||
|
|
@ -35,6 +42,7 @@ export interface MessageRecord {
|
|||
author_id?: string | null;
|
||||
author_display_name?: string | null;
|
||||
author_avatar_url?: string | null;
|
||||
token_usage?: TokenUsageSummary | null;
|
||||
}
|
||||
|
||||
export interface ThreadListResponse {
|
||||
|
|
@ -111,11 +119,11 @@ export async function getThreadMessages(threadId: number): Promise<ThreadHistory
|
|||
}
|
||||
|
||||
/**
|
||||
* Append a message to a thread
|
||||
* Append a message to a thread.
|
||||
*/
|
||||
export async function appendMessage(
|
||||
threadId: number,
|
||||
message: { role: "user" | "assistant" | "system"; content: unknown }
|
||||
message: { role: "user" | "assistant" | "system"; content: unknown; token_usage?: unknown }
|
||||
): Promise<MessageRecord> {
|
||||
return baseApiService.post<MessageRecord>(`/api/v1/threads/${threadId}/messages`, undefined, {
|
||||
body: message,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue