forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseContextUsage.ts
More file actions
63 lines (56 loc) · 1.74 KB
/
useContextUsage.ts
File metadata and controls
63 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useMemo } from 'react';
import type { Message } from '@/types';
import { getContextWindow } from '@/lib/model-context';
export interface ContextUsageData {
modelName: string;
contextWindow: number | null;
used: number;
ratio: number;
cacheReadTokens: number;
cacheCreationTokens: number;
outputTokens: number;
hasData: boolean;
}
export function useContextUsage(messages: Message[], modelName: string): ContextUsageData {
return useMemo(() => {
const contextWindow = getContextWindow(modelName);
const noData: ContextUsageData = {
modelName,
contextWindow,
used: 0,
ratio: 0,
cacheReadTokens: 0,
cacheCreationTokens: 0,
outputTokens: 0,
hasData: false,
};
// Find the last assistant message with token_usage
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i];
if (msg.role !== 'assistant' || !msg.token_usage) continue;
try {
const usage = typeof msg.token_usage === 'string'
? JSON.parse(msg.token_usage)
: msg.token_usage;
const inputTokens = usage.input_tokens || 0;
const cacheRead = usage.cache_read_input_tokens || 0;
const cacheCreation = usage.cache_creation_input_tokens || 0;
const outputTokens = usage.output_tokens || 0;
const used = inputTokens + cacheRead + cacheCreation;
return {
modelName,
contextWindow,
used,
ratio: contextWindow ? used / contextWindow : 0,
cacheReadTokens: cacheRead,
cacheCreationTokens: cacheCreation,
outputTokens,
hasData: true,
};
} catch {
continue;
}
}
return noData;
}, [messages, modelName]);
}