Merge pull request #214 from multica-ai/codex/chat-context-window-indicator

feat(chat): add context window usage indicator
This commit is contained in:
Jiayuan Zhang 2026-02-17 00:55:09 +08:00 committed by GitHub
commit fc8a813120
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 284 additions and 23 deletions

View file

@ -247,18 +247,18 @@ interface ElectronAPI {
setEnabled: (enabled: boolean) => Promise<{ ok: boolean; enabled?: boolean; error?: string }>
wake: (reason?: string) => Promise<{ ok: boolean; result?: unknown; error?: string }>
}
localChat: {
subscribe: (agentId: string) => Promise<{ ok?: boolean; error?: string; alreadySubscribed?: boolean }>
unsubscribe: (agentId: string) => Promise<{ ok: boolean }>
getHistory: (agentId: string, options?: { offset?: number; limit?: number }) => Promise<{ messages: unknown[]; total: number; offset: number; limit: number }>
send: (agentId: string, content: string) => Promise<{ ok?: boolean; error?: string }>
abort: (agentId: string) => Promise<{ ok?: boolean; error?: string }>
resolveExecApproval: (approvalId: string, decision: string) => Promise<{ ok: boolean }>
onEvent: (callback: (event: LocalChatEvent) => void) => void
offEvent: () => void
onApproval: (callback: (approval: LocalChatApproval) => void) => void
offApproval: () => void
}
localChat: {
subscribe: (agentId: string) => Promise<{ ok?: boolean; error?: string; alreadySubscribed?: boolean }>
unsubscribe: (agentId: string) => Promise<{ ok: boolean }>
getHistory: (agentId: string, options?: { offset?: number; limit?: number }) => Promise<{ messages: unknown[]; total: number; offset: number; limit: number; contextWindowTokens?: number }>
send: (agentId: string, content: string) => Promise<{ ok?: boolean; error?: string }>
abort: (agentId: string) => Promise<{ ok?: boolean; error?: string }>
resolveExecApproval: (approvalId: string, decision: string) => Promise<{ ok: boolean }>
onEvent: (callback: (event: LocalChatEvent) => void) => void
offEvent: () => void
onApproval: (callback: (approval: LocalChatApproval) => void) => void
offApproval: () => void
}
}
// Used in Renderer process, expose in `preload.ts`

View file

@ -354,20 +354,21 @@ export function registerHubIpcHandlers(): void {
const h = getHub()
const agent = h.getAgent(agentId)
if (!agent) {
return { messages: [], total: 0, offset: 0, limit: 0 }
return { messages: [], total: 0, offset: 0, limit: 0, contextWindowTokens: undefined }
}
try {
await agent.ensureInitialized()
const allMessages = agent.loadSessionMessagesForDisplay()
const contextWindowTokens = agent.getContextWindowTokens()
const total = allMessages.length
// Must match DEFAULT_MESSAGES_LIMIT from @multica/sdk/actions/rpc
const limit = options?.limit ?? 200
const offset = options?.offset ?? Math.max(0, total - limit)
const sliced = allMessages.slice(offset, offset + limit)
return { messages: sliced, total, offset, limit }
return { messages: sliced, total, offset, limit, contextWindowTokens }
} catch {
return { messages: [], total: 0, offset: 0, limit: 0 }
return { messages: [], total: 0, offset: 0, limit: 0, contextWindowTokens: undefined }
}
})

View file

@ -22,6 +22,7 @@ export function LocalChat({ initialPrompt }: LocalChatProps) {
isLoadingHistory,
isLoadingMore,
hasMore,
contextWindowTokens,
error,
pendingApprovals,
sendMessage,
@ -110,6 +111,7 @@ export function LocalChat({ initialPrompt }: LocalChatProps) {
isLoadingHistory={isLoadingHistory}
isLoadingMore={isLoadingMore}
hasMore={hasMore}
contextWindowTokens={contextWindowTokens}
error={error}
pendingApprovals={pendingApprovals}
sendMessage={sendMessage}

View file

@ -93,6 +93,7 @@ export function useLocalChat() {
chatRef.current.setHistory(result.messages as AgentMessageItem[], agentId, {
total: result.total,
offset: result.offset,
contextWindowTokens: result.contextWindowTokens,
})
offsetRef.current = result.offset
}
@ -140,6 +141,7 @@ export function useLocalChat() {
chatRef.current.prependHistory(result.messages as AgentMessageItem[], agentId, {
total: result.total,
offset: result.offset,
contextWindowTokens: result.contextWindowTokens,
})
offsetRef.current = result.offset
}
@ -172,6 +174,7 @@ export function useLocalChat() {
isLoadingHistory,
isLoadingMore,
hasMore: chat.hasMore,
contextWindowTokens: chat.contextWindowTokens,
error: chat.error,
pendingApprovals: chat.pendingApprovals,
sendMessage,