File size: 3,182 Bytes
5710dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export type FsSuggestKind = 'file' | 'directory' | 'symlink';

export interface FsSuggestItem {
  readonly path: string;
  readonly name: string;
  readonly kind: FsSuggestKind;
  readonly score: number;
  readonly matchPositions: readonly number[];
}

export interface FsSuggestResult {
  readonly items: readonly FsSuggestItem[];
  readonly truncated: boolean;
}

export interface FetchFsSuggestOptions {
  readonly baseUrl: string;
  readonly token?: string;
  readonly roots: readonly string[];
  readonly query: string;
  readonly limit?: number;
  readonly followGitignore?: boolean;
  readonly showHidden?: boolean;
  readonly includeGlobs?: readonly string[];
  readonly excludeGlobs?: readonly string[];
  readonly runtimeId?: string;
  readonly fetchImpl?: typeof fetch;
}

const KINDS = new Set<FsSuggestKind>(['file', 'directory', 'symlink']);

function parseItem(value: unknown): FsSuggestItem | undefined {
  if (value === null || typeof value !== 'object' || Array.isArray(value)) return undefined;
  const item = value as Record<string, unknown>;
  if (
    typeof item['path'] !== 'string' ||
    typeof item['name'] !== 'string' ||
    typeof item['kind'] !== 'string' ||
    !KINDS.has(item['kind'] as FsSuggestKind) ||
    typeof item['score'] !== 'number' ||
    !Array.isArray(item['match_positions']) ||
    !item['match_positions'].every((position) => typeof position === 'number')
  ) {
    return undefined;
  }
  return {
    path: item['path'],
    name: item['name'],
    kind: item['kind'] as FsSuggestKind,
    score: item['score'],
    matchPositions: item['match_positions'] as number[],
  };
}

async function postSuggest(
  body: Record<string, unknown>,
  opts: { baseUrl: string; token?: string; fetchImpl?: typeof fetch },
): Promise<FsSuggestResult> {
  const headers: Record<string, string> = { 'content-type': 'application/json' };
  if (opts.token !== undefined && opts.token !== '') {
    headers['authorization'] = `Bearer ${opts.token}`;
  }
  const doFetch = opts.fetchImpl ?? fetch;
  const res = await doFetch(`${opts.baseUrl.replace(/\/$/, '')}/api/v1/fs:suggest`, {
    method: 'POST',
    headers,
    body: JSON.stringify(body),
  });
  const envelope = (await res.json()) as { code: number; msg: string; data: unknown };
  if (envelope.code !== 0) {
    throw new Error(`fs:suggest failed (${envelope.code}): ${envelope.msg}`);
  }
  const data = envelope.data as Record<string, unknown> | null;
  if (data === null || typeof data !== 'object' || !Array.isArray(data['items'])) {
    throw new Error('fs:suggest: unexpected response shape');
  }
  return {
    items: (data['items'] as unknown[])
      .map(parseItem)
      .filter((item): item is FsSuggestItem => item !== undefined),
    truncated: data['truncated'] === true,
  };
}

export async function fetchFsSuggest(opts: FetchFsSuggestOptions): Promise<FsSuggestResult> {
  return postSuggest({
    roots: [...opts.roots],
    query: opts.query,
    limit: opts.limit,
    follow_gitignore: opts.followGitignore,
    show_hidden: opts.showHidden,
    include_globs: opts.includeGlobs,
    exclude_globs: opts.excludeGlobs,
    runtime_id: opts.runtimeId,
  }, opts);
}