MCPcopy Index your code
hub / github.com/anomalyco/opencode / loader

Function loader

packages/opencode/src/plugin/snowflake-cortex.ts:286–457  ·  view source on GitHub ↗
(getAuth, _provider)

Source from the content-addressed store, hash-verified

284 auth: {
285 provider: "snowflake-cortex",
286 async loader(getAuth, _provider) {
287 let auth = await getAuth()
288 if (auth.type !== "oauth") return {}
289
290 let refreshPromise:
291 | Promise<{
292 access: string
293 refresh: string
294 expires: number
295 }>
296 | undefined
297
298 const oauth = auth as typeof auth & { refresh: string; access: string; expires: number; accountId?: string }
299
300 if (oauth.accountId && oauth.refresh && oauth.expires && oauth.expires <= Date.now()) {
301 try {
302 const tokens = await refreshAccessToken(oauth.accountId, oauth.refresh)
303 const refreshedRefresh = tokens.refresh_token || oauth.refresh
304 const refreshedExpires = Date.now() + (tokens.expires_in ?? 600) * 1000
305 await _input.client.auth
306 .set({
307 path: { id: "snowflake-cortex" },
308 body: {
309 type: "oauth",
310 access: tokens.access_token,
311 refresh: refreshedRefresh,
312 expires: refreshedExpires,
313 ...(oauth.accountId && { accountId: oauth.accountId }),
314 },
315 })
316 .catch(() => {})
317 } catch {}
318 }
319
320 return {
321 apiKey: OAUTH_DUMMY_KEY,
322 async fetch(requestInput: RequestInfo | URL, init?: RequestInit) {
323 let currentAuth = await getAuth()
324 if (currentAuth.type !== "oauth") return fetch(requestInput, init)
325 let currentOauth = currentAuth as typeof currentAuth & {
326 refresh: string
327 access: string
328 expires: number
329 accountId?: string
330 }
331
332 if (!currentOauth.accountId) throw new Error("Snowflake OAuth auth is missing accountId")
333 const accountId = currentOauth.accountId
334
335 const refresh = async () => {
336 if (!refreshPromise) {
337 const refreshToken = currentOauth.refresh
338 refreshPromise = refreshAccessToken(accountId, refreshToken)
339 .then(async (tokens) => {
340 const refreshedRefresh = tokens.refresh_token || refreshToken
341 const refreshedExpires = Date.now() + (tokens.expires_in ?? 600) * 1000
342 await _input.client.auth
343 .set({

Callers

nothing calls this directly

Calls 2

refreshAccessTokenFunction · 0.70
setMethod · 0.45

Tested by

no test coverage detected