summaryrefslogtreecommitdiffhomepage
path: root/packages/host-bin/src/config.ts
blob: f69d79945160dabb2da5b6f9c63f26a07b442685 (plain)
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
import type { ConfigAccess } from "@dispatch/kernel";

export function envToConfigMap(
  env: Readonly<Record<string, string | undefined>>,
): Record<string, unknown> {
  const map: Record<string, unknown> = {};

  const apiKey = env.DISPATCH_API_KEY;
  if (apiKey !== undefined) {
    map["provider.openai-compat.apiKey"] = apiKey;
  }

  const baseURL = env.DISPATCH_BASE_URL;
  if (baseURL !== undefined) {
    map["provider.openai-compat.baseURL"] = baseURL;
  }

  const model = env.DISPATCH_MODEL;
  if (model !== undefined) {
    map["provider.openai-compat.model"] = model;
  }

  // Optional settings consumed by external extensions (e.g. the Claude provider).
  const anthropicModel = env.DISPATCH_ANTHROPIC_MODEL;
  if (anthropicModel !== undefined) {
    map["provider.anthropic.model"] = anthropicModel;
  }

  const claudeCredentialKey = env.DISPATCH_CLAUDE_CREDENTIAL_KEY;
  if (claudeCredentialKey !== undefined) {
    map["claude.credentialKey"] = claudeCredentialKey;
  }

  const httpPort = env.BACKEND_PORT ?? env.PORT;
  if (httpPort !== undefined) {
    const n = Number(httpPort);
    if (Number.isFinite(n) && n > 0) {
      map.httpPort = n;
    }
  }

  const surfaceWsPort = env.SURFACE_WS_PORT;
  if (surfaceWsPort !== undefined) {
    const n = Number(surfaceWsPort);
    if (Number.isFinite(n) && n > 0) {
      map.surfaceWsPort = n;
    }
  }

  return map;
}

export function configMapToAccess(map: Readonly<Record<string, unknown>>): ConfigAccess {
  return {
    get<T = unknown>(key: string): T | undefined {
      return map[key] as T | undefined;
    },
    getAll(): Readonly<Record<string, unknown>> {
      return map;
    },
  };
}