MCPcopy Create free account
hub / github.com/anomalyco/opencode / createModel

Function createModel

packages/tui/src/context/local.tsx:137–407  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

135 const agent = createAgent()
136
137 function createModel() {
138 const [modelStore, setModelStore] = createStore<{
139 ready: boolean
140 model: Record<
141 string,
142 {
143 providerID: string
144 modelID: string
145 }
146 >
147 recent: {
148 providerID: string
149 modelID: string
150 }[]
151 favorite: {
152 providerID: string
153 modelID: string
154 }[]
155 variant: Record<string, string | undefined>
156 }>({
157 ready: false,
158 model: {},
159 recent: [],
160 favorite: [],
161 variant: {},
162 })
163
164 const filePath = path.join(paths.state, "model.json")
165 const state = {
166 pending: false,
167 }
168
169 function save() {
170 if (!modelStore.ready) {
171 state.pending = true
172 return
173 }
174 state.pending = false
175 void writeJsonAtomic(filePath, {
176 recent: modelStore.recent,
177 favorite: modelStore.favorite,
178 variant: modelStore.variant,
179 })
180 }
181
182 readJson<unknown>(filePath)
183 .then((x) => {
184 if (!x || typeof x !== "object") return
185 const value = x as Record<string, unknown>
186 if (Array.isArray(value.recent)) setModelStore("recent", value.recent)
187 if (Array.isArray(value.favorite)) setModelStore("favorite", value.favorite)
188 if (typeof value.variant === "object" && value.variant !== null)
189 setModelStore("variant", value.variant as Record<string, string | undefined>)
190 })
191 .catch(() => {})
192 .finally(() => {
193 setModelStore("ready", true)
194 if (state.pending) save()

Callers 1

local.tsxFile · 0.70

Calls 8

readJsonFunction · 0.90
isModelValidFunction · 0.85
getFirstValidModelFunction · 0.85
saveFunction · 0.70
parseModelFunction · 0.70
findMethod · 0.65
valuesMethod · 0.45
currentMethod · 0.45

Tested by

no test coverage detected