Files
rhema-together/web/app/pages/settings.vue
T

468 lines
16 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { Check, Copy, Plus, Trash2 } from 'lucide-vue-next'
import type {
CreateSandboxBody,
CreateSandboxResponse,
CreateProviderBody,
IdResponse,
ProviderConfig,
ProviderListResponse,
SandboxListResponse,
SandboxTarget,
TestSandboxResponse,
} from '~/types'
import { apiErrorMessage } from '~/utils/errors'
import BaseDialog from '~/components/BaseDialog.vue'
useHead({ title: 'Settings · Rhema Together' })
const api = useApi()
const providers = ref<ProviderConfig[]>([])
const kinds = ref<string[]>(['anthropic', 'openai', 'openrouter', 'google', 'deepseek', 'zai', 'mistral', 'mistral-vibe', 'opencode', 'llama-cpp'])
const targets = ref<SandboxTarget[]>([])
const loading = ref(true)
const loadError = ref('')
const providerName = ref('')
const providerKind = ref('anthropic')
const providerModel = ref('')
const providerBaseUrl = ref('')
const providerKey = ref('')
const providerBusy = ref(false)
const providerError = ref('')
const llama = computed(() => providerKind.value === 'llama-cpp')
const sandboxName = ref('')
const sandboxHost = ref('')
const sandboxPort = ref<number | string>(22)
const sandboxUser = ref('root')
const sandboxRoot = ref('/root')
const sandboxKey = ref('')
const sandboxBusy = ref(false)
const sandboxError = ref('')
const deletingProvider = ref<string | null>(null)
const deletingTarget = ref<string | null>(null)
const deleteBusy = ref(false)
const rowError = ref('')
const providerDialog = ref(false)
const sandboxDialog = ref(false)
const testingTarget = ref<string | null>(null)
const testResults = ref<Record<string, TestSandboxResponse>>({})
const copiedKey = ref<string | null>(null)
async function loadLists() {
loading.value = true
loadError.value = ''
try {
const [providerList, targetList] = await Promise.all([
api.get<ProviderListResponse>('/provider-configs'),
api.get<SandboxListResponse>('/sandbox-targets'),
])
providers.value = providerList.providers
if (providerList.kinds.length > 0) kinds.value = providerList.kinds
targets.value = targetList.targets
} catch (err) {
loadError.value = apiErrorMessage(err)
} finally {
loading.value = false
}
}
onMounted(loadLists)
function openProviderDialog() {
providerError.value = ''
providerDialog.value = true
}
function closeProviderDialog() {
providerDialog.value = false
providerKey.value = ''
providerError.value = ''
}
function openSandboxDialog() {
sandboxError.value = ''
sandboxDialog.value = true
}
function closeSandboxDialog() {
sandboxDialog.value = false
sandboxKey.value = ''
sandboxError.value = ''
}
async function addProvider() {
if (providerBusy.value) return
providerBusy.value = true
providerError.value = ''
const body: CreateProviderBody = {
name: providerName.value.trim(),
kind: providerKind.value,
model: providerModel.value.trim(),
}
if (providerBaseUrl.value.trim()) body.base_url = providerBaseUrl.value.trim()
if (!llama.value) body.api_key = providerKey.value
try {
await api.post<IdResponse>('/provider-configs', body)
providerName.value = ''
providerModel.value = ''
providerBaseUrl.value = ''
providerKey.value = ''
await loadLists()
providerDialog.value = false
} catch (err) {
providerError.value = apiErrorMessage(err)
} finally {
providerBusy.value = false
}
}
async function addSandbox() {
if (sandboxBusy.value) return
sandboxBusy.value = true
sandboxError.value = ''
const body: CreateSandboxBody = {
name: sandboxName.value.trim(),
host: sandboxHost.value.trim(),
port: Number(sandboxPort.value) || 22,
user_name: sandboxUser.value.trim() || 'root',
workspace_root: sandboxRoot.value.trim() || '/root',
}
if (sandboxKey.value.trim()) body.private_key = sandboxKey.value.trim()
try {
await api.post<CreateSandboxResponse>('/sandbox-targets', body)
sandboxName.value = ''
sandboxHost.value = ''
sandboxPort.value = 22
sandboxUser.value = 'root'
sandboxRoot.value = '/root'
sandboxKey.value = ''
await loadLists()
sandboxDialog.value = false
} catch (err) {
sandboxError.value = apiErrorMessage(err)
} finally {
sandboxBusy.value = false
}
}
async function removeProvider(provider: ProviderConfig) {
if (deleteBusy.value) return
deleteBusy.value = true
rowError.value = ''
try {
await api.del(`/provider-configs/${provider.id}`)
deletingProvider.value = null
await loadLists()
} catch (err) {
rowError.value = apiErrorMessage(err)
} finally {
deleteBusy.value = false
}
}
async function removeTarget(target: SandboxTarget) {
if (deleteBusy.value) return
deleteBusy.value = true
rowError.value = ''
try {
await api.del(`/sandbox-targets/${target.id}`)
deletingTarget.value = null
await loadLists()
} catch (err) {
rowError.value = apiErrorMessage(err)
} finally {
deleteBusy.value = false
}
}
async function testTarget(target: SandboxTarget) {
if (testingTarget.value) return
testingTarget.value = target.id
rowError.value = ''
try {
testResults.value[target.id] = await api.post<TestSandboxResponse>(`/sandbox-targets/${target.id}/test`)
} catch (err) {
rowError.value = apiErrorMessage(err)
} finally {
testingTarget.value = null
}
}
async function copyKey(target: SandboxTarget) {
try {
await navigator.clipboard.writeText(target.public_key)
copiedKey.value = target.id
window.setTimeout(() => {
if (copiedKey.value === target.id) copiedKey.value = null
}, 1800)
} catch {
rowError.value = 'The clipboard is not available. Select the key and copy it by hand.'
}
}
</script>
<template>
<div class="page">
<div class="page-head">
<h1 class="page-title">Settings</h1>
</div>
<p v-if="loadError" class="form-error" role="alert">{{ loadError }}</p>
<p v-if="rowError" class="form-error" role="alert">{{ rowError }}</p>
<p v-if="loading" class="meta">Loading settings.</p>
<section class="section">
<div class="section-head">
<h2 class="section-title">Providers</h2>
<button type="button" class="btn btn-sm" @click="openProviderDialog">
<Plus :size="14" aria-hidden="true" />
Add provider
</button>
</div>
<p class="hint">A provider holds the model backend and key. Rooms spend from the provider their owner picks.</p>
<div v-if="!loading && providers.length === 0" class="empty">No providers yet. Use Add provider to connect one.</div>
<div v-else class="list">
<div v-for="provider in providers" :key="provider.id" class="settings-row">
<div class="settings-row-main">
<div class="settings-row-title">{{ provider.name }}</div>
<div class="settings-row-detail">
{{ provider.kind }}, {{ provider.model }}
<template v-if="provider.base_url">, {{ provider.base_url }}</template>
</div>
</div>
<div class="settings-row-actions">
<template v-if="deletingProvider === provider.id">
<div class="confirm-line">
<span>Delete {{ provider.name }}?</span>
<button type="button" class="btn btn-sm btn-quiet" :disabled="deleteBusy" @click="deletingProvider = null">
Cancel
</button>
<button type="button" class="btn btn-sm btn-danger" :disabled="deleteBusy" @click="removeProvider(provider)">
{{ deleteBusy ? 'Deleting' : 'Delete' }}
</button>
</div>
</template>
<button
v-else
type="button"
class="icon-btn"
aria-label="Delete provider"
title="Delete provider"
@click="deletingProvider = provider.id"
>
<Trash2 :size="16" aria-hidden="true" />
</button>
</div>
</div>
</div>
<BaseDialog
v-if="providerDialog"
title="Add provider"
description="The key stays sealed on the server and is never shown again."
@close="closeProviderDialog"
>
<form class="form" @submit.prevent="addProvider">
<div class="form-grid">
<div class="field">
<label class="field-label" for="provider-name">Name</label>
<input id="provider-name" v-model="providerName" class="input" type="text" maxlength="60" autofocus required>
</div>
<div class="field">
<label class="field-label" for="provider-kind">Kind</label>
<select id="provider-kind" v-model="providerKind" class="select">
<option v-for="kind in kinds" :key="kind" :value="kind">{{ kind }}</option>
</select>
</div>
</div>
<div class="field">
<label class="field-label" for="provider-model">Model</label>
<input id="provider-model" v-model="providerModel" class="input" type="text" spellcheck="false" required>
</div>
<div class="field">
<label class="field-label" for="provider-base">
{{ llama ? 'Server URL' : 'Base URL' }}
<span v-if="!llama" class="field-optional">optional</span>
</label>
<input
id="provider-base"
v-model="providerBaseUrl"
class="input"
type="text"
spellcheck="false"
:placeholder="llama ? 'http://127.0.0.1:8080' : 'https://api.example.com/v1'"
:required="llama"
>
<span v-if="llama" class="hint">The address of the llama.cpp server.</span>
</div>
<div v-if="!llama" class="field">
<label class="field-label" for="provider-key">API key</label>
<input
id="provider-key"
v-model="providerKey"
class="input"
type="password"
autocomplete="off"
spellcheck="false"
required
>
<span class="hint">The key is sealed on the server and never shown again.</span>
</div>
<p v-if="providerError" class="form-error" role="alert">{{ providerError }}</p>
<div class="dialog-actions">
<button type="button" class="btn" :disabled="providerBusy" @click="closeProviderDialog">Cancel</button>
<button type="submit" class="btn btn-primary" :disabled="providerBusy">
{{ providerBusy ? 'Adding' : 'Add provider' }}
</button>
</div>
</form>
</BaseDialog>
</section>
<section class="section">
<div class="section-head">
<h2 class="section-title">Sandboxes</h2>
<button type="button" class="btn btn-sm" @click="openSandboxDialog">
<Plus :size="14" aria-hidden="true" />
Add sandbox
</button>
</div>
<p class="hint">A sandbox is an ssh machine the room works on. The public key belongs in its authorized_keys file.</p>
<div v-if="!loading && targets.length === 0" class="empty">No sandboxes yet. Use Add sandbox to connect one.</div>
<div v-else class="list">
<div v-for="target in targets" :key="target.id" class="settings-row">
<div class="settings-row-main">
<div class="settings-row-title">{{ target.name }}</div>
<div class="settings-row-detail">{{ target.user_name }}@{{ target.host }}:{{ target.port }}</div>
<div class="settings-row-detail">Workspace root: {{ target.workspace_root }}</div>
<div class="key-line">
<span class="key-text">{{ target.public_key }}</span>
<button
type="button"
class="btn btn-sm btn-quiet"
:aria-label="`Copy public key for ${target.name}`"
title="Copy public key"
@click="copyKey(target)"
>
<component :is="copiedKey === target.id ? Check : Copy" :size="14" aria-hidden="true" />
{{ copiedKey === target.id ? 'Copied' : 'Copy' }}
</button>
</div>
<div v-if="target.host_fingerprint" class="settings-row-detail">
Host fingerprint: {{ target.host_fingerprint }}
</div>
<div v-if="testResults[target.id]" class="small" :class="testResults[target.id]?.ok ? 'muted' : 'form-error'">
{{ testResults[target.id]?.detail }}
</div>
</div>
<div class="settings-row-actions">
<button
type="button"
class="btn btn-sm"
:disabled="testingTarget !== null"
@click="testTarget(target)"
>
{{ testingTarget === target.id ? 'Testing' : 'Test' }}
</button>
<template v-if="deletingTarget === target.id">
<div class="confirm-line">
<span>Delete {{ target.name }}?</span>
<button type="button" class="btn btn-sm btn-quiet" :disabled="deleteBusy" @click="deletingTarget = null">
Cancel
</button>
<button type="button" class="btn btn-sm btn-danger" :disabled="deleteBusy" @click="removeTarget(target)">
{{ deleteBusy ? 'Deleting' : 'Delete' }}
</button>
</div>
</template>
<button
v-else
type="button"
class="icon-btn"
aria-label="Delete sandbox"
title="Delete sandbox"
@click="deletingTarget = target.id"
>
<Trash2 :size="16" aria-hidden="true" />
</button>
</div>
</div>
</div>
<BaseDialog
v-if="sandboxDialog"
title="Add sandbox"
description="The room runs commands on this machine with the matching private key."
@close="closeSandboxDialog"
>
<form class="form" @submit.prevent="addSandbox">
<div class="form-grid">
<div class="field">
<label class="field-label" for="sandbox-name">Name</label>
<input id="sandbox-name" v-model="sandboxName" class="input" type="text" maxlength="60" autofocus required>
</div>
<div class="field">
<label class="field-label" for="sandbox-host">Host</label>
<input id="sandbox-host" v-model="sandboxHost" class="input" type="text" spellcheck="false" required>
</div>
</div>
<div class="form-grid">
<div class="field">
<label class="field-label" for="sandbox-port">Port</label>
<input id="sandbox-port" v-model.number="sandboxPort" class="input" type="number" min="1" max="65535" required>
</div>
<div class="field">
<label class="field-label" for="sandbox-user">SSH user</label>
<input id="sandbox-user" v-model="sandboxUser" class="input" type="text" spellcheck="false">
</div>
</div>
<div class="field">
<label class="field-label" for="sandbox-root">Workspace root</label>
<input id="sandbox-root" v-model="sandboxRoot" class="input" type="text" spellcheck="false">
<span class="hint">An absolute path. Rooms create their workspace below it unless the owner sets one.</span>
</div>
<div class="field">
<label class="field-label" for="sandbox-key">Private key <span class="field-optional">optional</span></label>
<textarea
id="sandbox-key"
v-model="sandboxKey"
class="textarea"
rows="4"
spellcheck="false"
placeholder="Leave empty to generate an ed25519 key."
></textarea>
</div>
<p v-if="sandboxError" class="form-error" role="alert">{{ sandboxError }}</p>
<div class="dialog-actions">
<button type="button" class="btn" :disabled="sandboxBusy" @click="closeSandboxDialog">Cancel</button>
<button type="submit" class="btn btn-primary" :disabled="sandboxBusy">
{{ sandboxBusy ? 'Adding' : 'Add sandbox' }}
</button>
</div>
</form>
</BaseDialog>
</section>
</div>
</template>