File size: 10,451 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from 'react';
import type { WorkerEvent } from '../shared/protocol';
import type { AgentMessage, AssistantMessage, DeviceInfo, ShellResult } from '../shared/types';
import { MODEL_TOTAL_BYTES } from '../worker/manifest';
import { WorkerError, getClient } from './agentClient';

// ───────────────────────── Trạng thái ─────────────────────────

export interface LoadState {
  phase: 'download' | 'compile' | 'warmup';
  loaded: number;
  total: number;
  cached: number;
  file?: string;
}

export type MeterTone = 'idle' | 'loading' | 'active' | 'error';

export interface Meter {
  /** Nơi việc đang chạy: GPU cho suy luận, JavaScript cho tool/shell. */
  engine: 'WebGPU' | 'JavaScript';
  tone: MeterTone;
  label: string;
  rate: number | null;
  samples: number[];
}

export interface ShellEntry {
  id: number;
  command: string;
  result: ShellResult | { stdout: ''; stderr: string; exitCode: 1 };
}

export interface AgentState {
  initialized: boolean;
  messages: AgentMessage[];
  streaming: AssistantMessage | null;
  files: Record<string, string>;
  busy: string | null;
  modelLoaded: boolean;
  load: LoadState | null;
  meter: Meter;
  notice: string;
  error: string;
  device: DeviceInfo | null;
  context: { input: number; output: number };
}

const IDLE_METER: Meter = { engine: 'WebGPU', tone: 'idle', label: 'Idle', rate: null, samples: [] };
const MAX_SAMPLES = 24;

const initialState: AgentState = {
  initialized: false,
  messages: [],
  streaming: null,
  files: {},
  busy: null,
  modelLoaded: false,
  load: null,
  meter: IDLE_METER,
  notice: '',
  error: '',
  device: null,
  context: { input: 0, output: 0 },
};

type Action = { type: 'event'; event: WorkerEvent } | { type: 'set'; patch: Partial<AgentState> };

const percent = (loaded: number, total: number) => Math.min(100, Math.floor((loaded / Math.max(total, 1)) * 100));

function reduce(state: AgentState, action: Action): AgentState {
  if (action.type === 'set') return { ...state, ...action.patch };
  const e = action.event;
  switch (e.type) {
    case 'initialized':
      return { ...state, initialized: true, messages: e.messages, files: e.files };
    case 'messages':
      return { ...state, messages: e.messages };
    case 'stream':
      return { ...state, streaming: e.message };
    case 'workspace':
      return { ...state, files: e.files };
    case 'busy': {
      const meter: Meter =
        e.action === 'load'
          ? { ...IDLE_METER, tone: 'loading', label: e.cachedOnly ? 'Loading local model…' : 'Loading…' }
          : e.action === 'prompt'
            ? { ...IDLE_METER, tone: 'active', label: 'Reading prompt…' }
            : e.action === 'shell'
              ? { ...IDLE_METER, engine: 'JavaScript', tone: 'active', label: 'Running bash…' }
              : { ...IDLE_METER, tone: 'active', label: 'Working…' };
      return { ...state, busy: e.action, meter, error: '' };
    }
    case 'idle':
      return {
        ...state,
        busy: null,
        load: null,
        streaming: null,
        meter: state.meter.tone === 'error' ? state.meter : { ...IDLE_METER, label: state.modelLoaded ? 'Ready' : 'Idle' },
      };
    case 'load_progress': {
      const load: LoadState = {
        phase: e.phase,
        loaded: e.loaded ?? 0,
        total: e.total ?? MODEL_TOTAL_BYTES,
        cached: e.cached ?? 0,
        file: e.file,
      };
      const label =
        e.phase === 'download'
          ? load.loaded < load.total
            ? `Downloading ${percent(load.loaded, load.total)}%`
            : 'Verifying…'
          : e.phase === 'compile'
            ? 'Preparing GPU…'
            : 'Warming up…';
      return { ...state, load, meter: { ...state.meter, tone: 'loading', label } };
    }
    case 'device':
      return { ...state, device: e.device };
    case 'loaded':
      return { ...state, modelLoaded: true, device: e.device, load: null, meter: { ...IDLE_METER, label: 'Ready' } };
    case 'fatal':
      return { ...state, modelLoaded: false, error: e.error, meter: { ...IDLE_METER, tone: 'error', label: 'Unavailable' } };
    case 'notice':
      return { ...state, notice: e.text };
    case 'context_usage':
      return { ...state, context: { input: e.inputTokens, output: e.outputTokens } };
    case 'context_trim':
      return {
        ...state,
        notice: `Dropped ${e.dropped} earlier message${e.dropped === 1 ? '' : 's'} to fit the 8,192-token context.`,
      };
    case 'inference_activity': {
      if (e.phase === 'prefill') return { ...state, meter: { ...IDLE_METER, tone: 'active', label: 'Reading prompt…' } };
      if (e.phase === 'end') return { ...state, meter: { ...IDLE_METER, tone: 'active', label: 'Working…' } };
      const samples = e.rate ? [...state.meter.samples, e.rate].slice(-MAX_SAMPLES) : state.meter.samples;
      return {
        ...state,
        meter: {
          engine: 'WebGPU',
          tone: 'active',
          label: e.rate ? `${e.rate.toFixed(1)} tok/s` : 'Generating…',
          rate: e.rate ?? null,
          samples,
        },
      };
    }
    case 'tool_start':
      return { ...state, meter: { ...IDLE_METER, engine: 'JavaScript', tone: 'active', label: `Running ${e.name}…` } };
    case 'tool_end':
      return { ...state, meter: { ...IDLE_METER, tone: 'active', label: 'Working…' } };
    case 'persistence_error':
      return { ...state, notice: e.error };
    default:
      return state;
  }
}

// ───────────────────────── Hook ─────────────────────────

export interface PendingSend {
  text: string;
  error?: string;
  onAccepted?: () => void;
}

export function useAgent() {
  const client = useMemo(() => getClient(), []);
  const [state, dispatch] = useReducer(reduce, initialState);
  const [pending, setPending] = useState<PendingSend | null>(null);
  const [shell, setShell] = useState<ShellEntry[]>([]);
  const loadedRef = useRef(false);
  const shellId = useRef(1);
  loadedRef.current = state.modelLoaded;

  useEffect(() => {
    const off = client.subscribe((event) => dispatch({ type: 'event', event }));
    client.ready.then((init) => dispatch({ type: 'event', event: init }));
    return off;
  }, [client]);

  const set = useCallback((patch: Partial<AgentState>) => dispatch({ type: 'set', patch }), []);

  const loadModel = useCallback(
    async (cachedOnly = false) => {
      set({ error: '' });
      await client.ready;
      await client.call('load', { cachedOnly });
    },
    [client, set],
  );

  /** Bắt đầu một lượt hội thoại. Trả về true nếu đã gửi ngay, false nếu cần người dùng xác nhận tải model. */
  const send = useCallback(
    async (text: string, onAccepted?: () => void): Promise<boolean> => {
      const trimmed = text.trim();
      if (!trimmed) return false;
      set({ error: '', notice: '' });
      await client.ready;

      if (!loadedRef.current) {
        let cacheError: string | undefined;
        if (await client.call<boolean>('cache_status')) {
          try {
            await loadModel(true);
          } catch (err) {
            if ((err as Error).name === 'AbortError') return false;
            if ((err as WorkerError).code !== 'MODEL_CACHE_MISS') cacheError = (err as Error).message;
          }
        }
        if (!loadedRef.current) {
          setPending({ text: trimmed, error: cacheError, onAccepted });
          return false;
        }
      }

      try {
        onAccepted?.();
        await client.call('prompt', { text: trimmed });
      } catch (err) {
        if ((err as Error).message !== 'Stopped.') set({ error: (err as Error).message });
      }
      return true;
    },
    [client, loadModel, set, setPending],
  );

  const confirmPending = useCallback(async () => {
    if (!pending) return;
    const { text, onAccepted } = pending;
    setPending({ text });
    try {
      await loadModel(false);
    } catch (err) {
      if ((err as Error).name === 'AbortError') {
        setPending(null);
        return;
      }
      setPending({ text, error: (err as Error).message, onAccepted });
      return;
    }
    setPending(null);
    try {
      onAccepted?.();
      await client.call('prompt', { text });
    } catch (err) {
      if ((err as Error).message !== 'Stopped.') set({ error: (err as Error).message });
    }
  }, [client, loadModel, pending, set, setPending]);

  const cancelPending = useCallback(() => {
    // Nếu đang tải, dừng luôn; bản nháp vẫn giữ nguyên trong ô soạn thảo.
    void client.call('stop').catch(() => {});
    setPending(null);
  }, [client, setPending]);

  const stop = useCallback(() => void client.call('stop').catch(() => {}), [client]);

  const newChat = useCallback(async () => {
    try {
      await client.call('new_chat');
      set({ notice: '', error: '', context: { input: 0, output: 0 } });
    } catch (err) {
      set({ error: (err as Error).message });
    }
  }, [client, set]);

  const writeFile = useCallback(
    async (path: string, content: string) => {
      try {
        await client.call('write', { path, content });
        return true;
      } catch (err) {
        set({ error: (err as Error).message });
        return false;
      }
    },
    [client, set],
  );

  const runShell = useCallback(
    async (command: string) => {
      const id = shellId.current++;
      try {
        const result = await client.call<ShellResult>('shell', { command });
        setShell((list) => [...list, { id, command, result }]);
      } catch (err) {
        setShell((list) => [
          ...list,
          { id, command, result: { stdout: '', stderr: (err as Error).message + '\n', exitCode: 1 } },
        ]);
      }
    },
    [client, setShell],
  );

  const clearShell = useCallback(() => setShell([]), [setShell]);
  const dismissNotice = useCallback(() => set({ notice: '', error: '' }), [set]);
  const setError = useCallback((error: string) => set({ error }), [set]);

  return {
    state,
    pending,
    shell,
    actions: { loadModel, send, confirmPending, cancelPending, stop, newChat, writeFile, runShell, clearShell, dismissNotice, setError },
  };
}