Vendor opencode source for docker build
This commit is contained in:
116
opencode/packages/console/app/src/context/auth.ts
Normal file
116
opencode/packages/console/app/src/context/auth.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { getRequestEvent } from "solid-js/web"
|
||||
import { and, Database, eq, inArray, isNull, sql } from "@opencode-ai/console-core/drizzle/index.js"
|
||||
import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
|
||||
import { redirect } from "@solidjs/router"
|
||||
import { Actor } from "@opencode-ai/console-core/actor.js"
|
||||
|
||||
import { createClient } from "@openauthjs/openauth/client"
|
||||
|
||||
export const AuthClient = createClient({
|
||||
clientID: "app",
|
||||
issuer: import.meta.env.VITE_AUTH_URL,
|
||||
})
|
||||
|
||||
import { useSession } from "@solidjs/start/http"
|
||||
import { Resource } from "@opencode-ai/console-resource"
|
||||
|
||||
export interface AuthSession {
|
||||
account?: Record<
|
||||
string,
|
||||
{
|
||||
id: string
|
||||
email: string
|
||||
}
|
||||
>
|
||||
current?: string
|
||||
}
|
||||
|
||||
export function useAuthSession() {
|
||||
return useSession<AuthSession>({
|
||||
password: Resource.ZEN_SESSION_SECRET.value,
|
||||
name: "auth",
|
||||
maxAge: 60 * 60 * 24 * 365,
|
||||
cookie: {
|
||||
secure: false,
|
||||
httpOnly: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const getActor = async (workspace?: string): Promise<Actor.Info> => {
|
||||
"use server"
|
||||
const evt = getRequestEvent()
|
||||
if (!evt) throw new Error("No request event")
|
||||
if (evt.locals.actor) return evt.locals.actor
|
||||
evt.locals.actor = (async () => {
|
||||
const auth = await useAuthSession()
|
||||
if (!workspace) {
|
||||
const account = auth.data.account ?? {}
|
||||
const current = account[auth.data.current ?? ""]
|
||||
if (current) {
|
||||
return {
|
||||
type: "account",
|
||||
properties: {
|
||||
email: current.email,
|
||||
accountID: current.id,
|
||||
},
|
||||
}
|
||||
}
|
||||
if (Object.keys(account).length > 0) {
|
||||
const current = Object.values(account)[0]
|
||||
await auth.update((val) => ({
|
||||
...val,
|
||||
current: current.id,
|
||||
}))
|
||||
return {
|
||||
type: "account",
|
||||
properties: {
|
||||
email: current.email,
|
||||
accountID: current.id,
|
||||
},
|
||||
}
|
||||
}
|
||||
return {
|
||||
type: "public",
|
||||
properties: {},
|
||||
}
|
||||
}
|
||||
const accounts = Object.keys(auth.data.account ?? {})
|
||||
if (accounts.length) {
|
||||
const user = await Database.use((tx) =>
|
||||
tx
|
||||
.select()
|
||||
.from(UserTable)
|
||||
.where(
|
||||
and(
|
||||
eq(UserTable.workspaceID, workspace),
|
||||
isNull(UserTable.timeDeleted),
|
||||
inArray(UserTable.accountID, accounts),
|
||||
),
|
||||
)
|
||||
.limit(1)
|
||||
.execute()
|
||||
.then((x) => x[0]),
|
||||
)
|
||||
if (user) {
|
||||
await Database.use((tx) =>
|
||||
tx
|
||||
.update(UserTable)
|
||||
.set({ timeSeen: sql`now()` })
|
||||
.where(and(eq(UserTable.workspaceID, workspace), eq(UserTable.id, user.id))),
|
||||
)
|
||||
return {
|
||||
type: "user",
|
||||
properties: {
|
||||
userID: user.id,
|
||||
workspaceID: user.workspaceID,
|
||||
accountID: user.accountID,
|
||||
role: user.role,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
throw redirect("/auth/authorize")
|
||||
})()
|
||||
return evt.locals.actor
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Actor } from "@opencode-ai/console-core/actor.js"
|
||||
import { getActor } from "./auth"
|
||||
|
||||
export async function withActor<T>(fn: () => T, workspace?: string) {
|
||||
const actor = await getActor(workspace)
|
||||
return Actor.provide(actor.type, actor.properties, fn)
|
||||
}
|
||||
27
opencode/packages/console/app/src/context/i18n.tsx
Normal file
27
opencode/packages/console/app/src/context/i18n.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import { createMemo } from "solid-js"
|
||||
import { createSimpleContext } from "@opencode-ai/ui/context"
|
||||
import { i18n, type Key } from "~/i18n"
|
||||
import { useLanguage } from "~/context/language"
|
||||
|
||||
function resolve(text: string, params?: Record<string, string | number>) {
|
||||
if (!params) return text
|
||||
return text.replace(/\{\{(\w+)\}\}/g, (raw, key) => {
|
||||
const value = params[key]
|
||||
if (value === undefined || value === null) return raw
|
||||
return String(value)
|
||||
})
|
||||
}
|
||||
|
||||
export const { use: useI18n, provider: I18nProvider } = createSimpleContext({
|
||||
name: "I18n",
|
||||
init: () => {
|
||||
const language = useLanguage()
|
||||
const dict = createMemo(() => i18n(language.locale()))
|
||||
|
||||
return {
|
||||
t(key: Key, params?: Record<string, string | number>) {
|
||||
return resolve(dict()[key], params)
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
72
opencode/packages/console/app/src/context/language.tsx
Normal file
72
opencode/packages/console/app/src/context/language.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import { createEffect } from "solid-js"
|
||||
import { createStore } from "solid-js/store"
|
||||
import { getRequestEvent } from "solid-js/web"
|
||||
import { createSimpleContext } from "@opencode-ai/ui/context"
|
||||
import {
|
||||
LOCALES,
|
||||
type Locale,
|
||||
clearCookie,
|
||||
cookie,
|
||||
detectFromLanguages,
|
||||
dir as localeDir,
|
||||
label as localeLabel,
|
||||
localeFromCookieHeader,
|
||||
localeFromRequest,
|
||||
parseLocale,
|
||||
route as localeRoute,
|
||||
tag as localeTag,
|
||||
} from "~/lib/language"
|
||||
|
||||
function initial() {
|
||||
const evt = getRequestEvent()
|
||||
if (evt) return localeFromRequest(evt.request)
|
||||
|
||||
if (typeof document === "object") {
|
||||
const fromDom = parseLocale(document.documentElement.dataset.locale)
|
||||
if (fromDom) return fromDom
|
||||
|
||||
const fromCookie = localeFromCookieHeader(document.cookie)
|
||||
if (fromCookie) return fromCookie
|
||||
}
|
||||
|
||||
if (typeof navigator !== "object") return "en" satisfies Locale
|
||||
|
||||
const languages = navigator.languages?.length ? navigator.languages : [navigator.language]
|
||||
return detectFromLanguages(languages)
|
||||
}
|
||||
|
||||
export const { use: useLanguage, provider: LanguageProvider } = createSimpleContext({
|
||||
name: "Language",
|
||||
init: () => {
|
||||
const [store, setStore] = createStore({
|
||||
locale: initial(),
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
if (typeof document !== "object") return
|
||||
document.documentElement.lang = localeTag(store.locale)
|
||||
document.documentElement.dir = localeDir(store.locale)
|
||||
document.documentElement.dataset.locale = store.locale
|
||||
})
|
||||
|
||||
return {
|
||||
locale: () => store.locale,
|
||||
locales: LOCALES,
|
||||
label: localeLabel,
|
||||
tag: localeTag,
|
||||
dir: localeDir,
|
||||
route(pathname: string) {
|
||||
return localeRoute(store.locale, pathname)
|
||||
},
|
||||
setLocale(next: Locale) {
|
||||
setStore("locale", next)
|
||||
if (typeof document !== "object") return
|
||||
document.cookie = cookie(next)
|
||||
},
|
||||
clear() {
|
||||
if (typeof document !== "object") return
|
||||
document.cookie = clearCookie()
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user