mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 2: Phase 2: Terminal service, REST routes and cleanup
This commit is contained in:
1 parent
b494ee9f4a
commit
beab2646e7
15 files changed
+907
-57
No files matched your search
@@ -2,3 +2,4 @@ export * from './jsonc'
|
||||
export * from './repo'
|
||||
export * from './sandbox-command'
|
||||
export * from './semver'
|
||||
export * from './terminal-title'
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { TerminalKind } from '../schemas/terminal'
|
||||
|
||||
const ACTION_TITLE_PREFIX = 'ocm:action:'
|
||||
const SETUP_TITLE_PREFIX = 'ocm:setup:'
|
||||
const ACTION_ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/
|
||||
|
||||
export interface FormatTerminalTitleInput {
|
||||
kind: TerminalKind
|
||||
name: string
|
||||
actionId?: string
|
||||
}
|
||||
|
||||
export interface ParsedTerminalTitle {
|
||||
kind: TerminalKind
|
||||
title: string
|
||||
actionId?: string
|
||||
}
|
||||
|
||||
export function formatTerminalTitle(input: FormatTerminalTitleInput): string {
|
||||
if (input.kind === 'action') {
|
||||
if (!input.actionId || !ACTION_ID_PATTERN.test(input.actionId)) {
|
||||
throw new Error('Action terminals require a valid action id')
|
||||
}
|
||||
return `${ACTION_TITLE_PREFIX}${input.actionId}:${input.name}`
|
||||
}
|
||||
|
||||
if (input.kind === 'setup') {
|
||||
return `${SETUP_TITLE_PREFIX}${input.name}`
|
||||
}
|
||||
|
||||
return input.name
|
||||
}
|
||||
|
||||
export function parseTerminalTitle(raw: string): ParsedTerminalTitle {
|
||||
if (raw.startsWith(ACTION_TITLE_PREFIX)) {
|
||||
const rest = raw.slice(ACTION_TITLE_PREFIX.length)
|
||||
const separator = rest.indexOf(':')
|
||||
const actionId = separator === -1 ? rest : rest.slice(0, separator)
|
||||
const title = separator === -1 ? '' : rest.slice(separator + 1)
|
||||
|
||||
if (ACTION_ID_PATTERN.test(actionId) && title.length > 0) {
|
||||
return { kind: 'action', title, actionId }
|
||||
}
|
||||
|
||||
return { kind: 'shell', title: raw }
|
||||
}
|
||||
|
||||
if (raw.startsWith(SETUP_TITLE_PREFIX)) {
|
||||
return { kind: 'setup', title: raw.slice(SETUP_TITLE_PREFIX.length) }
|
||||
}
|
||||
|
||||
return { kind: 'shell', title: raw }
|
||||
}
|
||||
Reference in new issue
Block a user