File size: 2,573 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
import type {
  ClusterTaskKind,
  CompleteTaskBody,
  DescribeTaskResult,
  TaskErrorResult,
} from '../shared/completeTask.js';
import type {DetectionResultJson, DetectionTask} from './types.js';

export type TaskCompleteOutcome =
  | {status: 'done'; kind: 'detect'; results: DetectionResultJson[]}
  | {status: 'done'; kind: 'describe'; description: string}
  | {status: 'error'; error: string};

function parseErrorResult(result: unknown): string {
  if (result === null || typeof result !== 'object') {
    throw new Error("Error tasks must include result: { error: string }");
  }

  const error = (result as TaskErrorResult).error;
  if (typeof error !== 'string' || !error.trim()) {
    throw new Error("Error tasks must include a non-empty result.error");
  }

  return error.trim();
}

function parseDescribeResult(result: unknown): string {
  if (result === null || typeof result !== 'object') {
    throw new Error("Describe tasks must include result: { description: string }");
  }

  const description = (result as DescribeTaskResult).description;
  if (typeof description !== 'string' || !description.trim()) {
    throw new Error("Describe tasks must include a non-empty result.description");
  }

  return description.trim();
}

function parseDetectResult(result: unknown): DetectionResultJson[] {
  if (!Array.isArray(result)) {
    throw new Error("Detect tasks must include result as a detection array");
  }

  return result as DetectionResultJson[];
}

export function parseCompleteTaskBody(
  task: DetectionTask,
  body: CompleteTaskBody,
  hostModel?: string,
): TaskCompleteOutcome {
  if (!body.kind) {
    throw new Error("Complete payload must include 'kind'");
  }

  if (body.kind !== task.kind) {
    throw new Error(
      `Task ${task.id} is '${task.kind}' but complete payload kind is '${body.kind}'`,
    );
  }

  if (body.status === 'error') {
    return {status: 'error', error: parseErrorResult(body.result)};
  }

  const model = body.model?.trim() || hostModel;
  if (!model) {
    throw new Error("Complete payload must include a 'model' field");
  }

  return parseDoneResult(body.kind, body.result);
}

function parseDoneResult(
  kind: ClusterTaskKind,
  result: unknown,
): Extract<TaskCompleteOutcome, {status: 'done'}> {
  if (kind === 'describe') {
    return {status: 'done', kind: 'describe', description: parseDescribeResult(result)};
  }

  if (kind === 'detect') {
    return {status: 'done', kind: 'detect', results: parseDetectResult(result)};
  }

  throw new Error(`Unsupported task kind '${kind as string}'`);
}