Phase 1: OpenAI API Token Extraction
- Add stream_options: { include_usage: true } to all streaming API calls
- Capture chunk.usage from final streaming chunks and response.usage from non-streaming
- Extend OpenAiChatIterationResult with optional usage field
- Update buildStats() to accept and return real token counts from usage data
- Wire iteration.usage through chat() to both buildStats() call sites
Phase 2: Agent Loop Stats Propagation
- Rename inputTokens/outputTokens to masterInputTokens/masterOutputTokens, add masterThinkingTokens
- Accumulate response.stats.tokenCounts after each master AI call
- Delete all Math.ceil(length/4) crude approximations (master and subagent loops)
- Track startTime/durationMs and emit IWorkOrderCompleteStats with workOrderComplete
- Subagent loop uses response.stats?.tokenCounts instead of Math.ceil
Phase 3: Database Model Changes
- Add contextWindowUsage field to IChatSession, ChatSessionSchema, and frontend ChatSession
- Initialize contextWindowUsage: 0 on session creation
Phase 4: Persist Stats on Turn Completion
- drone-session: accept IWorkOrderCompleteStats, persist turn stats, walk subagent records for aggregate
- drone-session: $inc session stats and contextWindowUsage, add formatDurationLabel() helper
- code-session: accept and forward stats, update in-memory session stats
- message-queue: Redis replay handles 4th stats arg
- Update WorkOrderCompleteMessage type in @gadget/api to accept stats parameter
Phase 5: UI — Context Window Fuel Gauge
- Add contextWindowUsage prop to SessionPanel
- Add fuel gauge bar with E→F labels, green/yellow/red zones, token count display
- Visible for ALL provider types (not gated by apiType)
Phase 6: Frontend Streaming State
- Add IWorkOrderCompleteStats interface to frontend api.ts
- handleWorkOrderComplete accepts stats, updates turn stats and session contextWindowUsage
- Pass contextWindowUsage prop to SessionPanel
76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
// src/messages/drone.ts
|
|
// Copyright (C) 2026 Rob Colbert <rob.colbert@openplatform.us>
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
import { IChatTurn } from "../interfaces/chat-turn.ts";
|
|
import { IDroneRegistration } from "../interfaces/drone-registration.ts";
|
|
|
|
import { GadgetComponent } from "../lib/component.ts";
|
|
import { GadgetLogLevel } from "../lib/log.ts";
|
|
|
|
import { WorkspaceMode } from "./ide.ts";
|
|
|
|
export type ProcessWorkOrderCallback = (
|
|
success: boolean,
|
|
message?: string,
|
|
) => void;
|
|
export type ProcessWorkOrderMessage = (
|
|
registration: IDroneRegistration,
|
|
turn: IChatTurn,
|
|
cb: ProcessWorkOrderCallback,
|
|
) => void;
|
|
|
|
export type LogMessage = (
|
|
timestamp: Date,
|
|
component: GadgetComponent,
|
|
level: GadgetLogLevel,
|
|
message: string,
|
|
metadata?: unknown,
|
|
) => void;
|
|
|
|
export type StatusMessage = (content: string) => void;
|
|
|
|
export type ThinkingMessage = (content: string) => void;
|
|
|
|
export type ResponseMessage = (content: string) => void;
|
|
|
|
export type ToolCallMessage = (
|
|
callId: string,
|
|
name: string,
|
|
params: string,
|
|
response: string,
|
|
) => void;
|
|
|
|
export interface IWorkOrderCompleteStats {
|
|
masterInputTokens: number;
|
|
masterOutputTokens: number;
|
|
masterThinkingTokens: number;
|
|
toolCallCount: number;
|
|
durationMs: number;
|
|
}
|
|
|
|
export type WorkOrderCompleteMessage = (
|
|
workOrderId: string,
|
|
success: boolean,
|
|
message?: string,
|
|
stats?: IWorkOrderCompleteStats,
|
|
) => void;
|
|
|
|
export type RequestCrashRecoveryMessage = (data: {
|
|
workspaceId: string;
|
|
turnId: string;
|
|
chatSessionId: string;
|
|
}) => void;
|
|
|
|
export type CrashRecoveryResponseMessage = (data: {
|
|
turnId: string;
|
|
action: "discard" | "retry";
|
|
retryDelay?: number;
|
|
}) => void;
|
|
|
|
export type RequestTerminationMessage = (
|
|
cb: (success: boolean) => void,
|
|
) => void;
|
|
|
|
export type WorkspaceModeChangedMessage = (mode: WorkspaceMode) => void;
|