PI / src /shared /protocol.ts
noodcon's picture
Upload 64 files
9b9eafc verified
Raw
History Blame Contribute Delete
2.28 kB
import type { AgentMessage, AssistantMessage, DeviceInfo, ShellResult, WorkspaceState } from './types';
/** Yêu cầu từ main thread → worker. */
export type WorkerAction =
| { action: 'load'; args: { cachedOnly?: boolean } }
| { action: 'prompt'; args: { text: string } }
| { action: 'shell'; args: { command: string } }
| { action: 'write'; args: { path: string; content: string } }
| { action: 'read'; args: { path: string } }
| { action: 'new_chat'; args?: undefined }
| { action: 'stop'; args?: undefined }
| { action: 'snapshot'; args?: undefined }
| { action: 'cache_status'; args?: undefined };
export type WorkerRequest = WorkerAction & { id: number };
export type LoadPhase = 'download' | 'compile' | 'warmup';
export interface LoadProgress {
phase: LoadPhase;
loaded?: number;
total?: number;
cached?: number;
file?: string;
}
export type InferencePhase = 'prefill' | 'decode' | 'end';
/** Sự kiện worker → main thread. */
export type WorkerEvent =
| { type: 'reply'; id: number; value?: unknown; error?: string; errorName?: string; errorCode?: string }
| ({ type: 'initialized' } & WorkspaceState)
| { type: 'busy'; action: string; cachedOnly?: boolean }
| { type: 'idle' }
| ({ type: 'load_progress' } & LoadProgress)
| { type: 'device'; device: DeviceInfo }
| { type: 'loaded'; device: DeviceInfo; cachedBytes: number }
| { type: 'fatal'; error: string }
| { type: 'messages'; messages: AgentMessage[] }
| { type: 'stream'; message: AssistantMessage | null }
| { type: 'tool_start'; name: string }
| { type: 'tool_end'; name: string }
| { type: 'workspace'; files: Record<string, string> }
| { type: 'notice'; text: string }
| { type: 'context_usage'; inputTokens: number; outputTokens: number }
| { type: 'context_trim'; dropped: number }
| { type: 'inference_activity'; phase: InferencePhase; rate?: number | null; outputTokens?: number }
| {
type: 'generation';
inputTokens: number;
outputTokens: number;
elapsedMs: number;
firstTokenMs?: number;
stopReason?: string;
}
| { type: 'persistence_error'; error: string };
export type ReplyValue = ShellResult | string | boolean | WorkspaceState | DeviceInfo | Record<string, string> | void;