File size: 2,146 Bytes
9210bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type {
  ClusterTaskHostContext,
  ClusterTaskKind,
  CompleteTaskBody,
  HostTaskMessage,
} from './types';

export abstract class AbstractModelTaskHandler {
  abstract readonly kind: ClusterTaskKind;

  constructor(protected readonly host: ClusterTaskHostContext) {}

  abstract process(task: HostTaskMessage): Promise<void>;

  protected async completeTask(taskId: string, body: CompleteTaskBody): Promise<void> {
    const res = await fetch(`${this.host.apiBase}/api/tasks/${taskId}/complete`, {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        ...body,
        model: body.model ?? this.host.getSelectedModelId(),
      }),
    });

    if (!res.ok) {
      const json = (await res.json().catch(() => ({}))) as {error?: string};
      throw new Error(json.error ?? `Task complete failed (${res.status})`);
    }
  }

  protected taskError(error: string): CompleteTaskBody {
    return {status: 'error', kind: this.kind, result: {error}};
  }

  protected async withVideoFrame(
    task: HostTaskMessage,
    run: (frame: VideoFrame) => Promise<CompleteTaskBody>,
  ): Promise<void> {
    let frame: VideoFrame | null = null;

    try {
      frame = await AbstractModelTaskHandler.base64ToVideoFrame(
        task.image_base64,
        task.mime_type,
      );
      const body = await run(frame);
      await this.completeTask(task.id, body);
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      await this.completeTask(task.id, this.taskError(message));
      throw error;
    } finally {
      frame?.close();
    }
  }

  protected static async base64ToVideoFrame(
    base64: string,
    mimeType: string,
  ): Promise<VideoFrame> {
    const binary = atob(base64);
    const bytes = new Uint8Array(binary.length);
    for (let i = 0; i < binary.length; i++) {
      bytes[i] = binary.charCodeAt(i);
    }

    const blob = new Blob([bytes], {type: mimeType});
    const bitmap = await createImageBitmap(blob);
    const frame = new VideoFrame(bitmap, {timestamp: 0});
    bitmap.close();
    return frame;
  }
}