File size: 2,278 Bytes
9b9eafc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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;