File size: 7,801 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
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
import type {Application, Request, Response} from 'express';
import {Router} from 'express';
import {VIDEO_DESCRIPTION_MODEL_ID} from '../shared/clusterModels.js';
import type {CompleteTaskBody} from '../shared/completeTask.js';
import {parseCompleteTaskBody} from './completeTask.js';
import {
  parseHostField,
  validateHostReady,
  type HostValidationFailure,
} from './hostValidation.js';
import {resolveImagePayload} from './imageLoader.js';
import type {BrokerStore} from './store.js';
import type {
  DescribeRequestBody,
  DetectRequestBody,
  DetectionTask,
} from './types.js';

type ApiErrorType =
  | 'invalid_request_error'
  | 'host_unavailable'
  | 'detection_error'
  | 'description_error'
  | 'timeout';

type ResolvedImage = {imageBase64: string; mimeType: string};

type ImageTaskOptions = {
  hostReady?: Parameters<typeof validateHostReady>[2];
  errorType: ApiErrorType;
  fallbackMessage: string;
  createTask: (hostId: string, image: ResolvedImage) => DetectionTask;
  buildSuccess: (completed: DetectionTask) => Record<string, unknown> | null;
};

export class ApiTaskHandler {
  readonly router: Router;

  constructor(
    private readonly store: BrokerStore,
    private readonly taskTimeoutMs: number,
  ) {
    this.router = Router();
    this.router.post('/api/tasks/:taskId/processing', (req, res) =>
      this.markProcessing(req, res),
    );
    this.router.post('/api/tasks/:taskId/complete', (req, res) => this.complete(req, res));
    this.router.get('/v1/tasks/:taskId', (req, res) => this.getTask(req, res));
    this.router.post('/v1/detect', (req, res) => void this.detect(req, res));
    this.router.post('/v1/describe', (req, res) => void this.describe(req, res));
  }

  mount(app: Application): void {
    app.use(this.router);
  }

  private taskId(req: Request): string {
    return String(req.params.taskId);
  }

  private markProcessing(req: Request, res: Response): void {
    this.store.markProcessing(this.taskId(req));
    res.json({status: 'ok'});
  }

  private complete(req: Request, res: Response): void {
    const taskId = this.taskId(req);
    const body = req.body as CompleteTaskBody;

    try {
      const existing = this.store.getTask(taskId);
      if (!existing) {
        res.status(404).json({error: `Task '${taskId}' not found`});
        return;
      }

      const hostModel = this.store.getHost(existing.hostId)?.model;
      const outcome = parseCompleteTaskBody(existing, body, hostModel);
      const task = this.store.completeTask(taskId, outcome);

      res.json({
        status: 'ok',
        task_id: task.id,
        kind: task.kind,
        ...(task.error ? {error: task.error} : {}),
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      const status = message.includes('not found') ? 404 : 400;
      res.status(status).json({error: message});
    }
  }

  private getTask(req: Request, res: Response): void {
    const task = this.store.getTask(this.taskId(req));
    if (!task) {
      res.status(404).json({error: 'Task not found'});
      return;
    }

    res.json(this.serializeTask(task));
  }

  private async detect(req: Request, res: Response): Promise<void> {
    const body = req.body as DetectRequestBody;
    await this.runImageTask(res, body, {
      errorType: 'detection_error',
      fallbackMessage: 'Detection failed',
      createTask: (hostId, image) =>
        this.store.createDetectTask(hostId, {
          ...image,
          threshold: body.threshold ?? 0.5,
        }),
      buildSuccess: (completed) => ({
        task_id: completed.id,
        host: completed.hostId,
        threshold: completed.threshold,
        detections: completed.results ?? [],
      }),
    });
  }

  private async describe(req: Request, res: Response): Promise<void> {
    const body = req.body as DescribeRequestBody;
    await this.runImageTask(res, body, {
      hostReady: {
        requiredModel: VIDEO_DESCRIPTION_MODEL_ID,
        requiredModelLabel: 'Video description',
      },
      errorType: 'description_error',
      fallbackMessage: 'Description failed',
      createTask: (hostId, image) =>
        this.store.createDescribeTask(hostId, {
          ...image,
          instruction: body.instruction?.trim() || 'What do you see?',
          maxNewTokens: body.max_new_tokens ?? 100,
        }),
      buildSuccess: (completed) => {
        const description = completed.description?.trim() ?? '';
        if (!description) {
          this.apiError(
            res,
            500,
            'Host returned an empty description. Re-register the host with "Video description" selected and wait until SmolVLM finishes loading.',
            'description_error',
            {task_id: completed.id},
          );
          return null;
        }

        return {
          task_id: completed.id,
          host: completed.hostId,
          instruction: completed.instruction,
          max_new_tokens: completed.maxNewTokens,
          description,
        };
      },
    });
  }

  private async runImageTask(
    res: Response,
    body: DetectRequestBody | DescribeRequestBody,
    options: ImageTaskOptions,
  ): Promise<void> {
    const hostId = this.resolveReadyHost(res, body, options.hostReady);
    if (!hostId) {
      return;
    }

    try {
      const image = await resolveImagePayload(body);
      const task = options.createTask(hostId, image);
      const completed = await this.store.waitForTask(task.id, this.taskTimeoutMs);
      this.respondWithCompletedTask(res, completed, options);
    } catch (error) {
      this.respondWithTaskFailure(res, error);
    }
  }

  private resolveReadyHost(
    res: Response,
    body: {host?: string},
    hostReady?: Parameters<typeof validateHostReady>[2],
  ): string | undefined {
    const hostField = parseHostField(body);
    if (typeof hostField !== 'string') {
      this.sendHostFailure(res, hostField);
      return undefined;
    }

    const failure = validateHostReady(this.store, hostField, hostReady);
    if (failure) {
      this.sendHostFailure(res, failure);
      return undefined;
    }

    return hostField;
  }

  private sendHostFailure(res: Response, failure: HostValidationFailure): void {
    const type =
      failure.status === 503 ? 'host_unavailable' : 'invalid_request_error';
    this.apiError(res, failure.status, failure.message, type);
  }

  private respondWithCompletedTask(
    res: Response,
    completed: DetectionTask,
    options: ImageTaskOptions,
  ): void {
    if (completed.status === 'error') {
      this.apiError(
        res,
        500,
        completed.error ?? options.fallbackMessage,
        options.errorType,
        {task_id: completed.id},
      );
      return;
    }

    const payload = options.buildSuccess(completed);
    if (payload) {
      res.json(payload);
    }
  }

  private respondWithTaskFailure(res: Response, error: unknown): void {
    const message = error instanceof Error ? error.message : String(error);
    const status = message.includes('timed out') ? 504 : 400;
    this.apiError(res, status, message, status === 504 ? 'timeout' : 'invalid_request_error');
  }

  private apiError(
    res: Response,
    status: number,
    message: string,
    type: ApiErrorType,
    extra?: Record<string, unknown>,
  ): void {
    res.status(status).json({
      error: {message, type},
      ...extra,
    });
  }

  private serializeTask(task: DetectionTask) {
    return {
      id: task.id,
      kind: task.kind,
      host: task.hostId,
      status: task.status,
      results: task.results,
      description: task.description,
      error: task.error,
      created_at: task.createdAt,
      started_at: task.startedAt,
      completed_at: task.completedAt,
    };
  }
}