summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/testing/model-selection.ts
blob: a5ea199ac16d09369652a6d238179a26dd8b5466 (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
72
73
74
75
76
77
78
79
80
type ModelKey = {
  providerID: string
  modelID: string
}

type State = {
  agent?: string
  model?: ModelKey | null
  variant?: string | null
}

export type ModelProbeState = {
  dir?: string
  sessionID?: string
  last?: {
    type: "agent" | "model" | "variant"
    agent?: string
    model?: ModelKey | null
    variant?: string | null
  }
  agent?: string
  model?: (ModelKey & { name?: string }) | undefined
  variant?: string | null
  selected?: string | null
  configured?: string
  pick?: State
  base?: State
  current?: string
}

export type ModelWindow = Window & {
  __opencode_e2e?: {
    model?: {
      enabled?: boolean
      current?: ModelProbeState
    }
  }
}

const clone = (state?: State) => {
  if (!state) return undefined
  return {
    ...state,
    model: state.model ? { ...state.model } : state.model,
  }
}

export const modelEnabled = () => {
  if (typeof window === "undefined") return false
  return (window as ModelWindow).__opencode_e2e?.model?.enabled === true
}

const root = () => {
  if (!modelEnabled()) return
  return (window as ModelWindow).__opencode_e2e?.model
}

export const modelProbe = {
  set(input: ModelProbeState) {
    const state = root()
    if (!state) return
    state.current = {
      ...input,
      model: input.model ? { ...input.model } : undefined,
      last: input.last
        ? {
            ...input.last,
            model: input.last.model ? { ...input.last.model } : input.last.model,
          }
        : undefined,
      pick: clone(input.pick),
      base: clone(input.base),
    }
  },
  clear() {
    const state = root()
    if (!state) return
    state.current = undefined
  },
}