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.
52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
export class ApiRequestError extends Error {
|
|
status: number
|
|
|
|
constructor(message: string, status = 0) {
|
|
super(message)
|
|
this.name = 'ApiRequestError'
|
|
this.status = status
|
|
}
|
|
}
|
|
|
|
export function apiErrorMessage(error: unknown): string {
|
|
if (error instanceof ApiRequestError) return error.message
|
|
if (error instanceof Error && error.message) return error.message
|
|
return 'The request failed. Try again.'
|
|
}
|
|
|
|
function serverMessage(data: unknown): string | null {
|
|
if (!data || typeof data !== 'object') return null
|
|
if ('error' in data) {
|
|
const value = (data as { error: unknown }).error
|
|
if (typeof value === 'string' && value.trim()) return value
|
|
if (value && typeof value === 'object' && 'message' in value) {
|
|
const message = (value as { message: unknown }).message
|
|
if (typeof message === 'string' && message.trim()) return message
|
|
}
|
|
}
|
|
if ('message' in data) {
|
|
const value = (data as { message: unknown }).message
|
|
if (typeof value === 'string' && value.trim()) return value
|
|
}
|
|
return null
|
|
}
|
|
|
|
export function normalizeApiError(error: unknown): ApiRequestError {
|
|
if (error instanceof ApiRequestError) return error
|
|
if (error && typeof error === 'object') {
|
|
const status = 'status' in error && typeof (error as { status: unknown }).status === 'number'
|
|
? (error as { status: number }).status
|
|
: 0
|
|
const data = 'data' in error ? (error as { data: unknown }).data : null
|
|
const message = serverMessage(data)
|
|
if (message) return new ApiRequestError(message, status)
|
|
if (status === 0 && error instanceof Error && error.message) {
|
|
return new ApiRequestError(error.message, status)
|
|
}
|
|
if (status === 401) return new ApiRequestError('Your session has expired. Sign in again.', 401)
|
|
if (status === 403) return new ApiRequestError('You do not have access to that.', 403)
|
|
if (status === 404) return new ApiRequestError('That was not found.', 404)
|
|
}
|
|
return new ApiRequestError('The request failed. Try again.')
|
|
}
|