summaryrefslogtreecommitdiffhomepage
path: root/js/src/app/config.ts
blob: 947298c6787903cff0b59836972959d337429375 (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
import path from "node:path";
import { Log } from "../util/log";
import { z } from "zod";

export namespace Config {
  const log = Log.create({ service: "config" });

  export const Info = z
    .object({
      providers: z
        .object({
          anthropic: z
            .object({
              apiKey: z.string().optional(),
              headers: z.record(z.string(), z.string()).optional(),
              baseURL: z.string().optional(),
            })
            .strict()
            .optional(),
        })
        .strict()
        .optional(),
    })
    .strict();

  export type Info = z.output<typeof Info>;

  export async function load(directory: string) {
    let result: Info = {};
    for (const file of ["opencode.jsonc", "opencode.json"]) {
      const resolved = path.join(directory, file);
      log.info("searching", { path: resolved });
      try {
        result = await import(path.join(directory, file)).then((mod) =>
          Info.parse(mod.default),
        );
        log.info("found", { path: resolved });
        break;
      } catch (e) {
        if (e instanceof z.ZodError) {
          for (const issue of e.issues) {
            log.info(issue.message);
          }
          throw e;
        }
        continue;
      }
    }
    log.info("loaded", result);
    return result;
  }
}