feat: add the multiplayer room server and client
A room is one conversation, one ssh sandbox, and one agent. Accounts create rooms; friends join through a single-use invite link and a display name; the agent speaks only through the message tool. The server is Axum and SQLite. It serves the built Nuxt client, streams room events over SSE, and runs one serialized turn loop per room with turn-level steering copied from rhema-code. The client has the chat, preview iframe, read-only file pane, xterm terminal, and presence. Preview URLs are signed wildcard hosts, and the proxy tunnels HTTP over ssh direct-tcpip. HostAgent::step remains deferred in the framework.
This commit is contained in:
1 parent
04dd63c4ca
commit
0035f4e031
69 files changed
+22501
-48
No files matched your search
@@ -0,0 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
onMounted(() => {
|
||||
void navigateTo('/rooms', { replace: true })
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page">
|
||||
<p class="meta">Opening rooms.</p>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,100 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import type { InvitePreview, JoinResponse } from '~/types'
|
||||
import { apiErrorMessage } from '~/utils/errors'
|
||||
import { formatDate } from '~/utils/format'
|
||||
|
||||
definePageMeta({ layout: 'auth' })
|
||||
useHead({ title: 'Join a room · rhema together' })
|
||||
|
||||
const route = useRoute()
|
||||
const api = useApi()
|
||||
const { session, load } = useSession()
|
||||
|
||||
const token = computed(() => String(route.params.token ?? ''))
|
||||
const invite = ref<InvitePreview | null>(null)
|
||||
const loading = ref(true)
|
||||
const loadError = ref('')
|
||||
const displayName = ref('')
|
||||
const submitting = ref(false)
|
||||
const error = ref('')
|
||||
|
||||
onMounted(async () => {
|
||||
displayName.value = session.value.user?.display_name ?? ''
|
||||
try {
|
||||
invite.value = await api.get<InvitePreview>(`/invites/${encodeURIComponent(token.value)}`)
|
||||
} catch (err) {
|
||||
loadError.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
})
|
||||
|
||||
async function submit() {
|
||||
if (submitting.value || !invite.value) return
|
||||
submitting.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const name = session.value.user?.display_name || displayName.value.trim()
|
||||
const response = await api.post<JoinResponse>(`/invites/${encodeURIComponent(token.value)}`, {
|
||||
display_name: name,
|
||||
})
|
||||
await load(true)
|
||||
await navigateTo(`/room/${response.room_id}`, { replace: true })
|
||||
} catch (err) {
|
||||
error.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<template v-if="loading">
|
||||
<p class="meta">Checking the invite.</p>
|
||||
</template>
|
||||
|
||||
<template v-else-if="loadError">
|
||||
<h1 class="auth-title">Invite</h1>
|
||||
<p class="form-error" role="alert">{{ loadError }}</p>
|
||||
<div class="auth-footer">
|
||||
<NuxtLink to="/login">Sign in</NuxtLink> if you already have an account.
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template v-else-if="invite">
|
||||
<h1 class="auth-title">Join {{ invite.room_name }}</h1>
|
||||
<p class="auth-note">
|
||||
Owned by {{ invite.owner }}. The link works once and expires {{ formatDate(invite.expires_at) }}.
|
||||
</p>
|
||||
|
||||
<form class="form" @submit.prevent="submit">
|
||||
<p v-if="session.user" class="form-note">Joining as {{ session.user.display_name }}.</p>
|
||||
<div v-else class="field">
|
||||
<label class="field-label" for="join-name">Display name</label>
|
||||
<input
|
||||
id="join-name"
|
||||
v-model="displayName"
|
||||
class="input"
|
||||
type="text"
|
||||
autocomplete="name"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
<span class="hint">This is how the room sees you. You can add an account later.</span>
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="form-error" role="alert">{{ error }}</p>
|
||||
|
||||
<button type="submit" class="btn btn-primary" :disabled="submitting || (!session.user && !displayName.trim())">
|
||||
{{ submitting ? 'Joining' : 'Join room' }}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div v-if="session.user" class="auth-footer">
|
||||
<NuxtLink to="/rooms">Back to rooms</NuxtLink>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,84 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import type { User } from '~/types'
|
||||
import { apiErrorMessage } from '~/utils/errors'
|
||||
|
||||
definePageMeta({ layout: 'auth' })
|
||||
useHead({ title: 'Sign in · rhema together' })
|
||||
|
||||
const api = useApi()
|
||||
const { session, load } = useSession()
|
||||
|
||||
const identifier = ref('')
|
||||
const password = ref('')
|
||||
const submitting = ref(false)
|
||||
const error = ref('')
|
||||
|
||||
async function submit() {
|
||||
if (submitting.value) return
|
||||
submitting.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
await api.post<User>('/login', {
|
||||
identifier: identifier.value.trim(),
|
||||
password: password.value,
|
||||
})
|
||||
await load(true)
|
||||
await navigateTo('/rooms', { replace: true })
|
||||
} catch (err) {
|
||||
error.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<h1 class="auth-title">Sign in</h1>
|
||||
<p class="auth-note">Use your handle or email.</p>
|
||||
|
||||
<form class="form" @submit.prevent="submit">
|
||||
<div class="field">
|
||||
<label class="field-label" for="login-identifier">Handle or email</label>
|
||||
<input
|
||||
id="login-identifier"
|
||||
v-model="identifier"
|
||||
class="input"
|
||||
type="text"
|
||||
autocomplete="username"
|
||||
spellcheck="false"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="login-password">Password</label>
|
||||
<input
|
||||
id="login-password"
|
||||
v-model="password"
|
||||
class="input"
|
||||
type="password"
|
||||
autocomplete="current-password"
|
||||
required
|
||||
>
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="form-error" role="alert">{{ error }}</p>
|
||||
|
||||
<button type="submit" class="btn btn-primary" :disabled="submitting">
|
||||
{{ submitting ? 'Signing in' : 'Sign in' }}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="auth-footer">
|
||||
<template v-if="session.allowRegistration">
|
||||
No account yet? <NuxtLink to="/register">Register</NuxtLink>
|
||||
</template>
|
||||
<template v-else>
|
||||
Registration is closed. Ask a room owner for an invite link.
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,110 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import type { User } from '~/types'
|
||||
import { apiErrorMessage } from '~/utils/errors'
|
||||
|
||||
definePageMeta({ layout: 'auth' })
|
||||
useHead({ title: 'Register · rhema together' })
|
||||
|
||||
const api = useApi()
|
||||
const { session, load } = useSession()
|
||||
|
||||
const displayName = ref('')
|
||||
const handle = ref('')
|
||||
const email = ref('')
|
||||
const password = ref('')
|
||||
const submitting = ref(false)
|
||||
const error = ref('')
|
||||
|
||||
async function submit() {
|
||||
if (submitting.value) return
|
||||
submitting.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
await api.post<User>('/register', {
|
||||
display_name: displayName.value.trim(),
|
||||
handle: handle.value.trim().toLowerCase(),
|
||||
email: email.value.trim() || null,
|
||||
password: password.value,
|
||||
})
|
||||
await load(true)
|
||||
await navigateTo('/rooms', { replace: true })
|
||||
} catch (err) {
|
||||
error.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<h1 class="auth-title">Register</h1>
|
||||
<p class="auth-note">Create an account for this server.</p>
|
||||
|
||||
<p v-if="!session.allowRegistration" class="empty">
|
||||
Registration is closed. Ask a room owner for an invite link.
|
||||
</p>
|
||||
|
||||
<form v-else class="form" @submit.prevent="submit">
|
||||
<div class="field">
|
||||
<label class="field-label" for="register-name">Display name</label>
|
||||
<input
|
||||
id="register-name"
|
||||
v-model="displayName"
|
||||
class="input"
|
||||
type="text"
|
||||
autocomplete="name"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="register-handle">Handle</label>
|
||||
<input
|
||||
id="register-handle"
|
||||
v-model="handle"
|
||||
class="input"
|
||||
type="text"
|
||||
autocomplete="username"
|
||||
spellcheck="false"
|
||||
pattern="[A-Za-z0-9_-]{3,32}"
|
||||
minlength="3"
|
||||
maxlength="32"
|
||||
required
|
||||
>
|
||||
<span class="hint">3 to 32 lowercase letters, digits, _ or -.</span>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="register-email">Email <span class="field-optional">optional</span></label>
|
||||
<input id="register-email" v-model="email" class="input" type="email" autocomplete="email">
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="register-password">Password</label>
|
||||
<input
|
||||
id="register-password"
|
||||
v-model="password"
|
||||
class="input"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
minlength="8"
|
||||
required
|
||||
>
|
||||
<span class="hint">At least 8 characters.</span>
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="form-error" role="alert">{{ error }}</p>
|
||||
|
||||
<button type="submit" class="btn btn-primary" :disabled="submitting">
|
||||
{{ submitting ? 'Creating the account' : 'Create account' }}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="auth-footer">
|
||||
Already have an account? <NuxtLink to="/login">Sign in</NuxtLink>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,707 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import type {
|
||||
DeltaData,
|
||||
Envelope,
|
||||
Message,
|
||||
MessagePage,
|
||||
NoticeData,
|
||||
PresencePeer,
|
||||
Room,
|
||||
RoomDetailResponse,
|
||||
StepStartData,
|
||||
TimelineEntry,
|
||||
ToolArgsData,
|
||||
ToolOutputData,
|
||||
ToolResultData,
|
||||
ToolStartData,
|
||||
TurnActivity,
|
||||
TurnEndData,
|
||||
TurnStartData,
|
||||
UsageEventData,
|
||||
} from '~/types'
|
||||
import { apiErrorMessage } from '~/utils/errors'
|
||||
|
||||
definePageMeta({ key: (route) => route.fullPath })
|
||||
|
||||
const route = useRoute()
|
||||
const api = useApi()
|
||||
const { session } = useSession()
|
||||
const rooms = useRooms()
|
||||
|
||||
const roomId = computed(() => String(route.params.id ?? ''))
|
||||
|
||||
const detail = ref<RoomDetailResponse | null>(null)
|
||||
const detailLoading = ref(true)
|
||||
const detailError = ref('')
|
||||
|
||||
const messages = ref<Message[]>([])
|
||||
const notices = ref<Array<{ seq: number; level: string; text: string }>>([])
|
||||
const activities = ref<TurnActivity[]>([])
|
||||
const messagesLoading = ref(true)
|
||||
const messagesError = ref('')
|
||||
const hasMore = ref(false)
|
||||
const loadingOlder = ref(false)
|
||||
|
||||
const chatScroll = ref<HTMLElement | null>(null)
|
||||
const chatInner = ref<HTMLElement | null>(null)
|
||||
const stick = ref(true)
|
||||
const newMessages = ref(0)
|
||||
|
||||
const streamState = ref<'connecting' | 'live' | 'disconnected'>('connecting')
|
||||
const streamError = ref('')
|
||||
const peers = ref<PresencePeer[]>([])
|
||||
const onlineIds = ref<string[]>([])
|
||||
const typingActive = ref(false)
|
||||
|
||||
const paneOpen = ref(typeof window === 'undefined' ? true : !window.matchMedia('(max-width: 1099px)').matches)
|
||||
const inviteOpen = ref(false)
|
||||
const settingsOpen = ref(false)
|
||||
|
||||
const seenMessages = new Set<number>()
|
||||
const seenNotices = new Set<number>()
|
||||
let lastDurable = 0
|
||||
let disposed = false
|
||||
|
||||
let eventController: AbortController | null = null
|
||||
let eventGeneration = 0
|
||||
let retryIndex = 0
|
||||
let reconnectTimer: number | null = null
|
||||
|
||||
let chatObserver: ResizeObserver | null = null
|
||||
let suspendAutoScroll = false
|
||||
|
||||
let presenceSocket: WebSocket | null = null
|
||||
let presenceTimer: number | null = null
|
||||
let presenceRetry = 0
|
||||
|
||||
const DURABLE_KINDS = new Set(['message', 'notice', 'turn_start', 'turn_end', 'tool_result'])
|
||||
const BACKOFF = [1000, 2000, 5000, 10000]
|
||||
|
||||
useHead({
|
||||
title: computed(() => `${detail.value?.room.name ?? 'Room'} · rhema together`),
|
||||
})
|
||||
|
||||
const isOwner = computed(
|
||||
() => !!detail.value && detail.value.room.owner_id === session.value.user?.id,
|
||||
)
|
||||
|
||||
const memberById = computed(() => {
|
||||
const map = new Map<string, { color: string }>()
|
||||
for (const member of detail.value?.members ?? []) map.set(member.id, member)
|
||||
return map
|
||||
})
|
||||
|
||||
const timeline = computed<TimelineEntry[]>(() => {
|
||||
const entries: TimelineEntry[] = []
|
||||
for (const message of messages.value) {
|
||||
entries.push({ kind: 'message', seq: message.seq, message })
|
||||
}
|
||||
for (const notice of notices.value) {
|
||||
entries.push({ kind: 'notice', seq: notice.seq, level: notice.level, text: notice.text })
|
||||
}
|
||||
for (const activity of activities.value) {
|
||||
entries.push({ kind: 'turn', seq: activity.seq, activity })
|
||||
}
|
||||
return entries.sort((a, b) => a.seq - b.seq)
|
||||
})
|
||||
|
||||
const typingText = computed(() => {
|
||||
const me = detail.value?.me.id
|
||||
const names = peers.value
|
||||
.filter((peer) => peer.member_id !== me && peer.state?.typing === true)
|
||||
.map((peer) => peer.display_name)
|
||||
if (names.length === 0) return ''
|
||||
if (names.length === 1) return `${names[0]} is typing`
|
||||
if (names.length === 2) return `${names[0]} and ${names[1]} are typing`
|
||||
return 'Several people are typing'
|
||||
})
|
||||
|
||||
function eventData<T>(envelope: Envelope): T {
|
||||
return envelope.data as unknown as T
|
||||
}
|
||||
|
||||
function findActivity(turnId: string): TurnActivity | undefined {
|
||||
return activities.value.find((activity) => activity.turnId === turnId)
|
||||
}
|
||||
|
||||
function ensureActivity(turnId: string, seq: number): TurnActivity {
|
||||
const existing = findActivity(turnId)
|
||||
if (existing) return existing
|
||||
const created: TurnActivity = {
|
||||
turnId,
|
||||
seq: Number.isFinite(seq) ? seq : lastDurable,
|
||||
startedAt: Date.now(),
|
||||
text: '',
|
||||
thinking: '',
|
||||
steps: 0,
|
||||
tools: [],
|
||||
usage: null,
|
||||
status: 'running',
|
||||
error: '',
|
||||
}
|
||||
activities.value.push(created)
|
||||
return created
|
||||
}
|
||||
|
||||
function addMessage(message: Message, quiet = false) {
|
||||
if (seenMessages.has(message.seq)) return
|
||||
seenMessages.add(message.seq)
|
||||
const index = messages.value.findIndex((item) => item.seq > message.seq)
|
||||
if (index === -1) messages.value.push(message)
|
||||
else messages.value.splice(index, 0, message)
|
||||
if (!quiet) {
|
||||
if (stick.value) {
|
||||
void nextTick(() => {
|
||||
if (stick.value) scrollToBottom()
|
||||
})
|
||||
} else {
|
||||
newMessages.value += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addNotice(seq: number, level: string, text: string) {
|
||||
if (seenNotices.has(seq)) return
|
||||
seenNotices.add(seq)
|
||||
notices.value.push({ seq, level, text })
|
||||
}
|
||||
|
||||
function handleEnvelope(envelope: Envelope) {
|
||||
if (DURABLE_KINDS.has(envelope.kind)) {
|
||||
lastDurable = Math.max(lastDurable, envelope.seq)
|
||||
}
|
||||
|
||||
switch (envelope.kind) {
|
||||
case 'message':
|
||||
addMessage(eventData<Message>(envelope))
|
||||
break
|
||||
case 'notice': {
|
||||
const data = eventData<NoticeData>(envelope)
|
||||
addNotice(envelope.seq, data.level, data.text)
|
||||
break
|
||||
}
|
||||
case 'turn_start': {
|
||||
const data = eventData<TurnStartData>(envelope)
|
||||
if (!findActivity(data.turn_id)) {
|
||||
const activity = ensureActivity(data.turn_id, envelope.seq)
|
||||
activity.startedAt = data.at || Date.now()
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'text_delta': {
|
||||
const data = eventData<DeltaData>(envelope)
|
||||
ensureActivity(data.turn_id, lastDurable).text += data.delta
|
||||
break
|
||||
}
|
||||
case 'thinking_delta': {
|
||||
const data = eventData<DeltaData>(envelope)
|
||||
ensureActivity(data.turn_id, lastDurable).thinking += data.delta
|
||||
break
|
||||
}
|
||||
case 'step_start': {
|
||||
const data = eventData<StepStartData>(envelope)
|
||||
ensureActivity(data.turn_id, lastDurable).steps += 1
|
||||
break
|
||||
}
|
||||
case 'tool_start': {
|
||||
const data = eventData<ToolStartData>(envelope)
|
||||
const activity = ensureActivity(data.turn_id, lastDurable)
|
||||
if (!activity.tools.some((tool) => tool.call_id === data.call_id)) {
|
||||
activity.tools.push({
|
||||
call_id: data.call_id,
|
||||
name: data.name,
|
||||
args: '',
|
||||
output: '',
|
||||
content: null,
|
||||
isError: null,
|
||||
code: null,
|
||||
done: false,
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
case 'tool_args': {
|
||||
const data = eventData<ToolArgsData>(envelope)
|
||||
const activity = ensureActivity(data.turn_id, lastDurable)
|
||||
const tool = activity.tools.find((item) => item.call_id === data.call_id)
|
||||
if (tool) tool.args += data.delta
|
||||
break
|
||||
}
|
||||
case 'tool_output': {
|
||||
const data = eventData<ToolOutputData>(envelope)
|
||||
const activity = ensureActivity(data.turn_id, lastDurable)
|
||||
const tool = activity.tools.find((item) => item.call_id === data.call_id)
|
||||
if (tool) tool.output += data.chunk
|
||||
break
|
||||
}
|
||||
case 'tool_result': {
|
||||
const data = eventData<ToolResultData>(envelope)
|
||||
const activity = findActivity(data.turn_id)
|
||||
if (!activity) break
|
||||
let tool = activity.tools.find((item) => item.call_id === data.call_id)
|
||||
if (!tool) {
|
||||
tool = {
|
||||
call_id: data.call_id,
|
||||
name: '',
|
||||
args: '',
|
||||
output: '',
|
||||
content: null,
|
||||
isError: null,
|
||||
code: null,
|
||||
done: false,
|
||||
}
|
||||
activity.tools.push(tool)
|
||||
}
|
||||
if (data.name) tool.name = data.name
|
||||
if (data.args) tool.args = data.args
|
||||
tool.content = data.content
|
||||
tool.isError = data.is_error
|
||||
tool.code = data.code
|
||||
tool.done = true
|
||||
break
|
||||
}
|
||||
case 'usage': {
|
||||
const data = eventData<UsageEventData>(envelope)
|
||||
const activity = ensureActivity(data.turn_id, lastDurable)
|
||||
const usage = activity.usage ?? {
|
||||
input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
cached_input_tokens: 0,
|
||||
cost_usd: null,
|
||||
}
|
||||
usage.input_tokens += data.input_tokens
|
||||
usage.output_tokens += data.output_tokens
|
||||
usage.cached_input_tokens += data.cached_input_tokens
|
||||
if (data.cost_usd !== null && data.cost_usd !== undefined) {
|
||||
usage.cost_usd = (usage.cost_usd ?? 0) + data.cost_usd
|
||||
}
|
||||
activity.usage = usage
|
||||
break
|
||||
}
|
||||
case 'turn_end': {
|
||||
const data = eventData<TurnEndData>(envelope)
|
||||
const activity = findActivity(data.turn_id)
|
||||
if (!activity) break
|
||||
activity.status = data.status
|
||||
activity.steps = data.steps
|
||||
activity.usage = data.usage
|
||||
if (data.status === 'error') activity.error = data.error ?? 'The turn failed.'
|
||||
break
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
function processChunk(chunk: string) {
|
||||
const dataLines = chunk
|
||||
.split('\n')
|
||||
.filter((line) => line.startsWith('data:'))
|
||||
.map((line) => line.slice(5).replace(/^ /, ''))
|
||||
if (dataLines.length === 0) return
|
||||
const raw = dataLines.join('\n')
|
||||
try {
|
||||
const envelope = JSON.parse(raw) as Envelope
|
||||
if (envelope.kind === 'lag') {
|
||||
handleLag()
|
||||
return
|
||||
}
|
||||
handleEnvelope(envelope)
|
||||
scheduleStick()
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async function connectEvents() {
|
||||
if (disposed) return
|
||||
eventController?.abort()
|
||||
const generation = ++eventGeneration
|
||||
const controller = new AbortController()
|
||||
eventController = controller
|
||||
streamState.value = 'connecting'
|
||||
streamError.value = ''
|
||||
try {
|
||||
const response = await fetch(`/api/rooms/${encodeURIComponent(roomId.value)}/events?since=${lastDurable}`, {
|
||||
credentials: 'include',
|
||||
headers: { Accept: 'text/event-stream' },
|
||||
signal: controller.signal,
|
||||
})
|
||||
if (response.status === 401) {
|
||||
await navigateTo('/login')
|
||||
return
|
||||
}
|
||||
if (!response.ok || !response.body) {
|
||||
if (response.status === 403 || response.status === 404) {
|
||||
streamState.value = 'disconnected'
|
||||
streamError.value = 'Live updates are unavailable.'
|
||||
return
|
||||
}
|
||||
throw new Error('The live feed is unavailable.')
|
||||
}
|
||||
streamState.value = 'live'
|
||||
retryIndex = 0
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
while (!disposed && generation === eventGeneration) {
|
||||
const { value, done } = await reader.read()
|
||||
if (done) break
|
||||
buffer += decoder.decode(value, { stream: true })
|
||||
let boundary = buffer.indexOf('\n\n')
|
||||
while (boundary !== -1) {
|
||||
const chunk = buffer.slice(0, boundary)
|
||||
buffer = buffer.slice(boundary + 2)
|
||||
processChunk(chunk)
|
||||
boundary = buffer.indexOf('\n\n')
|
||||
}
|
||||
}
|
||||
if (generation !== eventGeneration || disposed) return
|
||||
throw new Error('The live feed closed.')
|
||||
} catch {
|
||||
if (controller.signal.aborted || generation !== eventGeneration || disposed) return
|
||||
streamState.value = 'disconnected'
|
||||
scheduleReconnect()
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleReconnect() {
|
||||
if (disposed || reconnectTimer !== null) return
|
||||
const delay = BACKOFF[Math.min(retryIndex, BACKOFF.length - 1)] ?? 1000
|
||||
retryIndex += 1
|
||||
reconnectTimer = window.setTimeout(() => {
|
||||
reconnectTimer = null
|
||||
void connectEvents()
|
||||
}, delay)
|
||||
}
|
||||
|
||||
function handleLag() {
|
||||
eventController?.abort()
|
||||
streamState.value = 'disconnected'
|
||||
void refetchNewest()
|
||||
scheduleReconnect()
|
||||
}
|
||||
|
||||
async function refetchNewest() {
|
||||
try {
|
||||
const page = await api.get<MessagePage>(`/rooms/${roomId.value}/messages`, { limit: 60 })
|
||||
for (const message of page.messages) addMessage(message, true)
|
||||
} catch {
|
||||
// The next reconnect brings the durable backlog.
|
||||
}
|
||||
}
|
||||
|
||||
function connectPresence() {
|
||||
if (disposed) return
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
const socket = new WebSocket(`${protocol}//${window.location.host}/api/rooms/${encodeURIComponent(roomId.value)}/presence`)
|
||||
presenceSocket = socket
|
||||
|
||||
socket.onopen = () => {
|
||||
presenceRetry = 0
|
||||
if (typingActive.value) sendPresenceState(true)
|
||||
}
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
if (typeof event.data !== 'string') return
|
||||
try {
|
||||
const parsed = JSON.parse(event.data) as unknown
|
||||
if (!parsed || typeof parsed !== 'object') return
|
||||
if ((parsed as { type?: unknown }).type !== 'presence') return
|
||||
const list = (parsed as { peers?: unknown }).peers
|
||||
if (!Array.isArray(list)) return
|
||||
peers.value = list as PresencePeer[]
|
||||
onlineIds.value = peers.value.map((peer) => peer.member_id)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
socket.onclose = () => {
|
||||
if (presenceSocket !== socket || disposed) return
|
||||
presenceSocket = null
|
||||
if (presenceTimer !== null) return
|
||||
const delay = BACKOFF[Math.min(presenceRetry, BACKOFF.length - 1)] ?? 1000
|
||||
presenceRetry += 1
|
||||
presenceTimer = window.setTimeout(() => {
|
||||
presenceTimer = null
|
||||
connectPresence()
|
||||
}, delay)
|
||||
}
|
||||
|
||||
socket.onerror = () => {
|
||||
socket.close()
|
||||
}
|
||||
}
|
||||
|
||||
function sendPresenceState(typing: boolean) {
|
||||
if (!presenceSocket || presenceSocket.readyState !== WebSocket.OPEN) return
|
||||
presenceSocket.send(JSON.stringify({ state: { typing } }))
|
||||
}
|
||||
|
||||
function onTyping(active: boolean) {
|
||||
typingActive.value = active
|
||||
sendPresenceState(active)
|
||||
}
|
||||
|
||||
function colorFor(message: Message): string | null {
|
||||
if (!message.author_id) return null
|
||||
return memberById.value.get(message.author_id)?.color ?? null
|
||||
}
|
||||
|
||||
function isNearBottom(element: HTMLElement) {
|
||||
return element.scrollHeight - element.scrollTop - element.clientHeight < 80
|
||||
}
|
||||
|
||||
function scrollToBottom() {
|
||||
const element = chatScroll.value
|
||||
if (!element) return
|
||||
element.scrollTop = element.scrollHeight
|
||||
stick.value = true
|
||||
newMessages.value = 0
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
const element = chatScroll.value
|
||||
if (!element) return
|
||||
if (isNearBottom(element)) {
|
||||
stick.value = true
|
||||
newMessages.value = 0
|
||||
} else {
|
||||
stick.value = false
|
||||
}
|
||||
}
|
||||
|
||||
let stickScheduled = false
|
||||
|
||||
/// Keep the newest turn card or message in view while the log grows,
|
||||
/// unless the reader has scrolled away from the bottom.
|
||||
function scheduleStick() {
|
||||
if (!stick.value || stickScheduled) return
|
||||
stickScheduled = true
|
||||
void nextTick(() => {
|
||||
stickScheduled = false
|
||||
if (stick.value) scrollToBottom()
|
||||
})
|
||||
}
|
||||
|
||||
async function loadDetail() {
|
||||
detailLoading.value = true
|
||||
detailError.value = ''
|
||||
try {
|
||||
const response = await api.get<RoomDetailResponse>(`/rooms/${roomId.value}`)
|
||||
detail.value = response
|
||||
if (onlineIds.value.length === 0) {
|
||||
onlineIds.value = response.members.filter((member) => member.online).map((member) => member.id)
|
||||
}
|
||||
} catch (err) {
|
||||
detailError.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
detailLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMessages() {
|
||||
messagesLoading.value = true
|
||||
messagesError.value = ''
|
||||
try {
|
||||
const page = await api.get<MessagePage>(`/rooms/${roomId.value}/messages`, { limit: 60 })
|
||||
for (const message of page.messages) addMessage(message, true)
|
||||
hasMore.value = page.has_more
|
||||
lastDurable = messages.value.reduce((max, message) => Math.max(max, message.seq), 0)
|
||||
} catch (err) {
|
||||
messagesError.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
messagesLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOlder() {
|
||||
if (loadingOlder.value || !hasMore.value || messages.value.length === 0) return
|
||||
const element = chatScroll.value
|
||||
const previousHeight = element?.scrollHeight ?? 0
|
||||
const previousTop = element?.scrollTop ?? 0
|
||||
loadingOlder.value = true
|
||||
suspendAutoScroll = true
|
||||
try {
|
||||
const oldest = messages.value[0]?.seq
|
||||
const page = await api.get<MessagePage>(`/rooms/${roomId.value}/messages`, {
|
||||
limit: 60,
|
||||
before: oldest,
|
||||
})
|
||||
for (const message of page.messages) addMessage(message, true)
|
||||
hasMore.value = page.has_more
|
||||
await nextTick()
|
||||
if (element) element.scrollTop = element.scrollHeight - previousHeight + previousTop
|
||||
} catch (err) {
|
||||
messagesError.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
loadingOlder.value = false
|
||||
await nextTick()
|
||||
window.requestAnimationFrame(() => {
|
||||
suspendAutoScroll = false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function onSent(message: Message) {
|
||||
addMessage(message, true)
|
||||
stick.value = true
|
||||
newMessages.value = 0
|
||||
await nextTick()
|
||||
scrollToBottom()
|
||||
}
|
||||
|
||||
async function onSettingsUpdated(room: Room) {
|
||||
if (detail.value) detail.value = { ...detail.value, room }
|
||||
await loadDetail()
|
||||
await rooms.refresh()
|
||||
}
|
||||
|
||||
async function onArchived() {
|
||||
await rooms.refresh()
|
||||
await navigateTo('/rooms', { replace: true })
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await loadDetail()
|
||||
await loadMessages()
|
||||
|
||||
if (!detailError.value) {
|
||||
await nextTick()
|
||||
scrollToBottom()
|
||||
window.requestAnimationFrame(() => {
|
||||
if (stick.value) scrollToBottom()
|
||||
})
|
||||
if (chatInner.value) {
|
||||
chatObserver = new ResizeObserver(() => {
|
||||
if (!stick.value || suspendAutoScroll) return
|
||||
const element = chatScroll.value
|
||||
if (element) element.scrollTop = element.scrollHeight
|
||||
})
|
||||
chatObserver.observe(chatInner.value)
|
||||
}
|
||||
void connectEvents()
|
||||
connectPresence()
|
||||
}
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
disposed = true
|
||||
eventGeneration += 1
|
||||
chatObserver?.disconnect()
|
||||
chatObserver = null
|
||||
eventController?.abort()
|
||||
eventController = null
|
||||
if (reconnectTimer !== null) window.clearTimeout(reconnectTimer)
|
||||
if (presenceTimer !== null) window.clearTimeout(presenceTimer)
|
||||
presenceSocket?.close()
|
||||
presenceSocket = null
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="detailLoading" class="page">
|
||||
<p class="meta">Opening the room.</p>
|
||||
</div>
|
||||
|
||||
<div v-else-if="detailError" class="page">
|
||||
<h1 class="page-title">Room</h1>
|
||||
<p class="form-error" role="alert">{{ detailError }}</p>
|
||||
<p class="mt-4"><NuxtLink to="/rooms">Back to rooms</NuxtLink></p>
|
||||
</div>
|
||||
|
||||
<div v-else-if="detail" class="room-shell">
|
||||
<RoomTopBar
|
||||
:room-name="detail.room.name"
|
||||
:members="detail.members"
|
||||
:online-ids="onlineIds"
|
||||
:is-owner="isOwner"
|
||||
:pane-open="paneOpen"
|
||||
@invite="inviteOpen = true"
|
||||
@settings="settingsOpen = true"
|
||||
@toggle-pane="paneOpen = !paneOpen"
|
||||
/>
|
||||
|
||||
<div class="room-body">
|
||||
<div class="chat-column">
|
||||
<div class="chat-area">
|
||||
<div ref="chatScroll" class="chat-scroll" @scroll="onScroll">
|
||||
<div ref="chatInner" class="chat-inner">
|
||||
<button
|
||||
v-if="hasMore"
|
||||
type="button"
|
||||
class="btn btn-sm btn-quiet chat-start"
|
||||
:disabled="loadingOlder"
|
||||
@click="loadOlder"
|
||||
>
|
||||
{{ loadingOlder ? 'Loading older messages' : 'Load older messages' }}
|
||||
</button>
|
||||
|
||||
<p v-if="messagesError" class="form-error" role="alert">{{ messagesError }}</p>
|
||||
<p v-if="messagesLoading && timeline.length === 0" class="meta">Loading the log.</p>
|
||||
|
||||
<p v-if="!messagesLoading && !messagesError && timeline.length === 0" class="empty">
|
||||
No messages yet. Ask @rhema to start.
|
||||
</p>
|
||||
|
||||
<template v-for="(entry, index) in timeline" :key="`${entry.kind}-${entry.seq}-${index}`">
|
||||
<ChatMessage
|
||||
v-if="entry.kind === 'message'"
|
||||
:message="entry.message"
|
||||
:color="colorFor(entry.message)"
|
||||
/>
|
||||
<p
|
||||
v-else-if="entry.kind === 'notice'"
|
||||
class="msg msg-system"
|
||||
:class="{ 'msg-system-danger': entry.level === 'error' }"
|
||||
>
|
||||
{{ entry.text }}
|
||||
</p>
|
||||
<TurnActivityView v-else :activity="entry.activity" />
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
v-if="newMessages > 0"
|
||||
type="button"
|
||||
class="btn btn-sm new-messages"
|
||||
@click="scrollToBottom"
|
||||
>
|
||||
{{ newMessages }} new {{ newMessages === 1 ? 'message' : 'messages' }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p v-if="streamError" class="typing">{{ streamError }}</p>
|
||||
<p v-else-if="streamState === 'disconnected'" class="typing">Reconnecting to live updates.</p>
|
||||
<p v-if="typingText" class="typing">{{ typingText }}</p>
|
||||
|
||||
<ChatComposer :room-id="roomId" @sent="onSent" @typing="onTyping" />
|
||||
</div>
|
||||
|
||||
<RightPane
|
||||
v-if="paneOpen"
|
||||
:room-id="detail.room.id"
|
||||
:room-name="detail.room.name"
|
||||
@close="paneOpen = false"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<InviteDialog
|
||||
v-if="inviteOpen"
|
||||
:room-id="detail.room.id"
|
||||
:room-name="detail.room.name"
|
||||
@close="inviteOpen = false"
|
||||
/>
|
||||
|
||||
<RoomSettingsDialog
|
||||
v-if="settingsOpen && isOwner"
|
||||
:room="detail.room"
|
||||
@close="settingsOpen = false"
|
||||
@updated="onSettingsUpdated"
|
||||
@archived="onArchived"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,54 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue'
|
||||
import { Plus } from 'lucide-vue-next'
|
||||
import { fullTime, plural, relativeTime } from '~/utils/format'
|
||||
|
||||
useHead({ title: 'Rooms · rhema together' })
|
||||
|
||||
const {
|
||||
rooms,
|
||||
loading,
|
||||
error,
|
||||
openCreate,
|
||||
refresh,
|
||||
} = useRooms()
|
||||
|
||||
onMounted(() => {
|
||||
void refresh()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page">
|
||||
<div class="page-head">
|
||||
<h1 class="page-title">Rooms</h1>
|
||||
<button type="button" class="btn btn-primary" @click="openCreate()">
|
||||
<Plus :size="16" aria-hidden="true" />
|
||||
New room
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="form-error" role="alert">{{ error }}</p>
|
||||
|
||||
<div v-if="loading && rooms.length === 0" class="empty">Loading rooms.</div>
|
||||
|
||||
<div v-else-if="rooms.length === 0" class="empty">
|
||||
Create a room to start.
|
||||
</div>
|
||||
|
||||
<div v-else class="rooms-list">
|
||||
<NuxtLink v-for="room in rooms" :key="room.id" :to="`/room/${room.id}`" class="room-row">
|
||||
<div>
|
||||
<div class="room-row-name">{{ room.name }}</div>
|
||||
<div class="room-row-preview">
|
||||
{{ room.last_text || 'No messages yet.' }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="room-row-meta">
|
||||
<span>{{ room.member_count }} {{ plural(room.member_count, 'member', 'members') }}</span>
|
||||
<span v-if="room.last_at" :title="fullTime(room.last_at)">{{ relativeTime(room.last_at) }}</span>
|
||||
</div>
|
||||
</NuxtLink>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,424 @@
|
||||
<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'
|
||||
|
||||
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 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)
|
||||
|
||||
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()
|
||||
} 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()
|
||||
} 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>
|
||||
</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. Add one below.</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>
|
||||
|
||||
<div class="form-panel">
|
||||
<h3 class="panel-title">Add provider</h3>
|
||||
<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" 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>
|
||||
<button type="submit" class="btn btn-primary" :disabled="providerBusy">
|
||||
<Plus :size="16" aria-hidden="true" />
|
||||
{{ providerBusy ? 'Adding' : 'Add provider' }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="section">
|
||||
<div class="section-head">
|
||||
<h2 class="section-title">Sandboxes</h2>
|
||||
</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. Add one below.</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>
|
||||
|
||||
<div class="form-panel">
|
||||
<h3 class="panel-title">Add sandbox</h3>
|
||||
<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" 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>
|
||||
<button type="submit" class="btn btn-primary" :disabled="sandboxBusy">
|
||||
<Plus :size="16" aria-hidden="true" />
|
||||
{{ sandboxBusy ? 'Adding' : 'Add sandbox' }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,126 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
import type { User } from '~/types'
|
||||
import { apiErrorMessage } from '~/utils/errors'
|
||||
|
||||
definePageMeta({ layout: 'auth' })
|
||||
useHead({ title: 'Set up the server · rhema together' })
|
||||
|
||||
const api = useApi()
|
||||
const { load } = useSession()
|
||||
|
||||
const token = ref('')
|
||||
const displayName = ref('')
|
||||
const handle = ref('')
|
||||
const email = ref('')
|
||||
const password = ref('')
|
||||
const checking = ref(true)
|
||||
const submitting = ref(false)
|
||||
const error = ref('')
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const state = await load(true)
|
||||
if (!state.needsSetup) {
|
||||
await navigateTo('/login', { replace: true })
|
||||
}
|
||||
} catch (err) {
|
||||
error.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
checking.value = false
|
||||
}
|
||||
})
|
||||
|
||||
async function submit() {
|
||||
if (submitting.value) return
|
||||
submitting.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
await api.post<User>('/setup', {
|
||||
token: token.value.trim(),
|
||||
display_name: displayName.value.trim(),
|
||||
handle: handle.value.trim().toLowerCase(),
|
||||
email: email.value.trim() || null,
|
||||
password: password.value,
|
||||
})
|
||||
await load(true)
|
||||
await navigateTo('/rooms', { replace: true })
|
||||
} catch (err) {
|
||||
error.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
submitting.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<h1 class="auth-title">Set up the server</h1>
|
||||
<p class="auth-note">Create the owner account. The token is printed in the server log at startup.</p>
|
||||
|
||||
<p v-if="checking" class="meta">Checking the server.</p>
|
||||
|
||||
<form v-else class="form" @submit.prevent="submit">
|
||||
<div class="field">
|
||||
<label class="field-label" for="setup-token">Setup token</label>
|
||||
<input
|
||||
id="setup-token"
|
||||
v-model="token"
|
||||
class="input"
|
||||
type="text"
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="setup-name">Display name</label>
|
||||
<input id="setup-name" v-model="displayName" class="input" type="text" autocomplete="name" required>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="setup-handle">Handle</label>
|
||||
<input
|
||||
id="setup-handle"
|
||||
v-model="handle"
|
||||
class="input"
|
||||
type="text"
|
||||
autocomplete="username"
|
||||
spellcheck="false"
|
||||
pattern="[A-Za-z0-9_-]{3,32}"
|
||||
minlength="3"
|
||||
maxlength="32"
|
||||
required
|
||||
>
|
||||
<span class="hint">3 to 32 lowercase letters, digits, _ or -.</span>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="setup-email">Email <span class="field-optional">optional</span></label>
|
||||
<input id="setup-email" v-model="email" class="input" type="email" autocomplete="email">
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label class="field-label" for="setup-password">Password</label>
|
||||
<input
|
||||
id="setup-password"
|
||||
v-model="password"
|
||||
class="input"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
minlength="8"
|
||||
required
|
||||
>
|
||||
<span class="hint">At least 8 characters.</span>
|
||||
</div>
|
||||
|
||||
<p v-if="error" class="form-error" role="alert">{{ error }}</p>
|
||||
|
||||
<button type="submit" class="btn btn-primary" :disabled="submitting">
|
||||
{{ submitting ? 'Creating the account' : 'Create the account' }}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user