mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
section 4: Phase 4: Frontend terminal client and xterm view
This commit is contained in:
1 parent
13c29c635e
commit
dc7cd2382f
11 files changed
+856
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { TerminalFrameDecoder, applyCtrlModifier } from './terminal-protocol'
|
||||
|
||||
function metaFrame(cursor: number): ArrayBuffer {
|
||||
const payload = new TextEncoder().encode(JSON.stringify({ cursor }))
|
||||
const bytes = new Uint8Array(payload.length + 1)
|
||||
bytes[0] = 0x00
|
||||
bytes.set(payload, 1)
|
||||
return bytes.buffer
|
||||
}
|
||||
|
||||
describe('TerminalFrameDecoder', () => {
|
||||
it('decodes the meta frame into a cursor', () => {
|
||||
const decoder = new TerminalFrameDecoder()
|
||||
expect(decoder.decode(metaFrame(42))).toEqual({ type: 'cursor', cursor: 42 })
|
||||
})
|
||||
|
||||
it('decodes split multibyte UTF-8 across binary frames', () => {
|
||||
const decoder = new TerminalFrameDecoder()
|
||||
const encoded = new TextEncoder().encode('→')
|
||||
const first = encoded.slice(0, 1)
|
||||
const second = encoded.slice(1)
|
||||
|
||||
expect(decoder.decode(first.buffer)).toEqual({ type: 'output', text: '' })
|
||||
expect(decoder.decode(second.buffer)).toEqual({ type: 'output', text: '→' })
|
||||
})
|
||||
|
||||
it('passes string frames through', () => {
|
||||
const decoder = new TerminalFrameDecoder()
|
||||
expect(decoder.decode('plain text')).toEqual({ type: 'output', text: 'plain text' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('applyCtrlModifier', () => {
|
||||
it('maps a letter to its control code', () => {
|
||||
expect(applyCtrlModifier('c')).toBe('\x03')
|
||||
})
|
||||
|
||||
it('maps a control symbol to its control code', () => {
|
||||
expect(applyCtrlModifier('[')).toBe('\x1b')
|
||||
})
|
||||
|
||||
it('returns non-letter input unchanged', () => {
|
||||
expect(applyCtrlModifier('1')).toBe('1')
|
||||
expect(applyCtrlModifier('ab')).toBe('ab')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,54 @@
|
||||
const META_FRAME_PREFIX = 0x00
|
||||
|
||||
const CTRL_SYMBOLS: Record<string, string> = {
|
||||
'@': '\x00',
|
||||
'[': '\x1b',
|
||||
'\\': '\x1c',
|
||||
']': '\x1d',
|
||||
'^': '\x1e',
|
||||
'_': '\x1f',
|
||||
}
|
||||
|
||||
export type TerminalFrame =
|
||||
| { type: 'output'; text: string }
|
||||
| { type: 'cursor'; cursor: number }
|
||||
|
||||
export class TerminalFrameDecoder {
|
||||
private readonly decoder = new TextDecoder('utf-8')
|
||||
|
||||
decode(data: string | ArrayBuffer): TerminalFrame {
|
||||
if (typeof data === 'string') {
|
||||
return { type: 'output', text: data }
|
||||
}
|
||||
|
||||
const bytes = new Uint8Array(data)
|
||||
if (bytes.length > 0 && bytes[0] === META_FRAME_PREFIX) {
|
||||
return this.decodeMetaFrame(bytes.subarray(1))
|
||||
}
|
||||
|
||||
return { type: 'output', text: this.decoder.decode(bytes, { stream: true }) }
|
||||
}
|
||||
|
||||
private decodeMetaFrame(payload: Uint8Array): TerminalFrame {
|
||||
try {
|
||||
const parsed = JSON.parse(new TextDecoder('utf-8').decode(payload)) as { cursor?: unknown }
|
||||
if (typeof parsed.cursor === 'number' && Number.isSafeInteger(parsed.cursor)) {
|
||||
return { type: 'cursor', cursor: parsed.cursor }
|
||||
}
|
||||
} catch {
|
||||
return { type: 'output', text: '' }
|
||||
}
|
||||
return { type: 'output', text: '' }
|
||||
}
|
||||
}
|
||||
|
||||
export function applyCtrlModifier(data: string): string {
|
||||
if (data.length !== 1) return data
|
||||
|
||||
const upper = data.toUpperCase()
|
||||
if (upper >= 'A' && upper <= 'Z') {
|
||||
return String.fromCharCode(upper.charCodeAt(0) - 64)
|
||||
}
|
||||
|
||||
return CTRL_SYMBOLS[data] ?? data
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { ITheme } from '@xterm/xterm'
|
||||
|
||||
const BASE_TOKENS = {
|
||||
background: 'background',
|
||||
foreground: 'foreground',
|
||||
cursor: 'primary',
|
||||
selectionBackground: 'accent',
|
||||
} as const
|
||||
|
||||
const ANSI_TOKENS = {
|
||||
black: 'muted',
|
||||
red: 'destructive',
|
||||
green: 'success',
|
||||
yellow: 'warning',
|
||||
blue: 'info',
|
||||
magenta: 'highlight',
|
||||
cyan: 'syntax-property',
|
||||
white: 'foreground',
|
||||
} as const
|
||||
|
||||
type AnsiName = keyof typeof ANSI_TOKENS
|
||||
|
||||
function collectTokenColors(root: HTMLElement): Map<string, string> {
|
||||
const tokens = new Set<string>([
|
||||
...Object.values(BASE_TOKENS),
|
||||
...Object.values(ANSI_TOKENS),
|
||||
])
|
||||
|
||||
const probes = new Map<string, HTMLSpanElement>()
|
||||
for (const token of tokens) {
|
||||
const probe = document.createElement('span')
|
||||
probe.style.color = `var(--color-${token})`
|
||||
probe.style.position = 'absolute'
|
||||
probe.style.width = '0'
|
||||
probe.style.height = '0'
|
||||
probe.style.overflow = 'hidden'
|
||||
probe.style.opacity = '0'
|
||||
probe.style.pointerEvents = 'none'
|
||||
root.appendChild(probe)
|
||||
probes.set(token, probe)
|
||||
}
|
||||
|
||||
const colors = new Map<string, string>()
|
||||
for (const [token, probe] of probes) {
|
||||
colors.set(token, getComputedStyle(probe).color)
|
||||
root.removeChild(probe)
|
||||
}
|
||||
return colors
|
||||
}
|
||||
|
||||
function ansiTheme(colors: Map<string, string>): Record<AnsiName | `bright${Capitalize<AnsiName>}`, string | undefined> {
|
||||
const theme = {} as Record<AnsiName | `bright${Capitalize<AnsiName>}`, string | undefined>
|
||||
for (const name of Object.keys(ANSI_TOKENS) as AnsiName[]) {
|
||||
const color = colors.get(ANSI_TOKENS[name])
|
||||
theme[name] = color
|
||||
theme[`bright${name[0].toUpperCase()}${name.slice(1)}` as `bright${Capitalize<AnsiName>}`] = color
|
||||
}
|
||||
return theme
|
||||
}
|
||||
|
||||
export function resolveTerminalTheme(root: HTMLElement): ITheme {
|
||||
const colors = collectTokenColors(root)
|
||||
return {
|
||||
background: colors.get(BASE_TOKENS.background),
|
||||
foreground: colors.get(BASE_TOKENS.foreground),
|
||||
cursor: colors.get(BASE_TOKENS.cursor),
|
||||
selectionBackground: colors.get(BASE_TOKENS.selectionBackground),
|
||||
...ansiTheme(colors),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { toWebSocketUrl } from './websocket-url'
|
||||
|
||||
const state = vi.hoisted(() => ({ apiBaseUrl: '' }))
|
||||
|
||||
vi.mock('@/config', () => ({
|
||||
get API_BASE_URL() {
|
||||
return state.apiBaseUrl
|
||||
},
|
||||
}))
|
||||
|
||||
describe('toWebSocketUrl', () => {
|
||||
afterEach(() => {
|
||||
state.apiBaseUrl = ''
|
||||
})
|
||||
|
||||
it('uses the page origin over http when no API base url is configured', () => {
|
||||
expect(toWebSocketUrl('/api/repos/1/terminals/t/connect')).toBe(
|
||||
'ws://localhost/api/repos/1/terminals/t/connect',
|
||||
)
|
||||
})
|
||||
|
||||
it('uses the page origin over https when no API base url is configured', () => {
|
||||
const original = window.location
|
||||
Object.defineProperty(window, 'location', {
|
||||
configurable: true,
|
||||
value: new URL('https://example.com'),
|
||||
})
|
||||
try {
|
||||
expect(toWebSocketUrl('/api/x')).toBe('wss://example.com/api/x')
|
||||
} finally {
|
||||
Object.defineProperty(window, 'location', { configurable: true, value: original })
|
||||
}
|
||||
})
|
||||
|
||||
it('uses an absolute API base url and preserves the query', () => {
|
||||
state.apiBaseUrl = 'https://api.example.com'
|
||||
expect(toWebSocketUrl('/api/x?directory=%2Ftmp')).toBe('wss://api.example.com/api/x?directory=%2Ftmp')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,8 @@
|
||||
import { API_BASE_URL } from '@/config'
|
||||
|
||||
export function toWebSocketUrl(pathWithQuery: string): string {
|
||||
const base = API_BASE_URL || window.location.origin
|
||||
const url = new URL(pathWithQuery, base)
|
||||
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
return url.toString()
|
||||
}
|
||||
Reference in new issue
Block a user