summaryrefslogtreecommitdiffhomepage
path: root/js/src/app/config.ts
blob: 348eb77c39b0a2042ded958203ec3c46007bf16f (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
63
64
65
66
67
68
69
70
71
import path from "path";
import { Log } from "../util/log";
import { z } from "zod";
import { App } from ".";

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

  export const state = App.state("config", async (app) => {
    const result = await load(app.root);
    return result;
  });

  export const Model = z.object({
    name: z.string().optional(),
    cost: z.object({
      input: z.number(),
      inputCached: z.number(),
      output: z.number(),
      outputCached: z.number(),
    }),
    contextWindow: z.number(),
    maxTokens: z.number().optional(),
    attachment: z.boolean(),
    reasoning: z.boolean().optional(),
  });
  export type Model = z.output<typeof Model>;

  export const Provider = z.object({
    options: z.record(z.string(), z.any()).optional(),
    models: z.record(z.string(), Model),
  });
  export type Provider = z.output<typeof Provider>;

  export const Info = z
    .object({
      providers: z.record(z.string(), Provider).optional(),
    })
    .strict();

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

  export function get() {
    return state();
  }

  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;
  }
}