Import packages/plugin/src/control-runtime.ts
This commit is contained in:
parent
698af5b622
commit
e7b37ac811
|
|
@ -25,9 +25,11 @@ import { ServerCredentialStore, ServerCredentialsInvalidError } from './server-c
|
|||
import { registerControlRoute, type HostWebServerLike } from './control-route.js'
|
||||
import { ControlStatusStream } from './control-stream.js'
|
||||
import type { PluginUpdateSnapshot } from './update-check.js'
|
||||
import type { PluginSelfUpdateResult } from './plugin-self-update.js'
|
||||
import { codexBinaryCandidates } from './codex/domain.js'
|
||||
import { cursorBinaryCandidates } from './acp/gateway.js'
|
||||
import { defaultCheckExecutable, installOrConfigureBackend, isBuiltinAcpBackend } from './acp/backend-installer.js'
|
||||
import type { HostLogSnapshot, HostLogStore, PluginLogFilter } from './host-log-store.js'
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
|
|
@ -50,6 +52,10 @@ export interface PluginSettingsView {
|
|||
}
|
||||
}
|
||||
|
||||
export interface PluginLogView extends HostLogSnapshot {
|
||||
logLevel: 'debug' | 'info' | 'warn' | 'error'
|
||||
}
|
||||
|
||||
export interface PluginAssociation {
|
||||
method: 'account' | 'host_registration_code' | 'owned_device' | 'device_pairing'
|
||||
account?: string
|
||||
|
|
@ -107,9 +113,16 @@ export class PluginControlRuntime {
|
|||
client: ClientModeRuntime | undefined
|
||||
host: HostAuthorizationControl
|
||||
}>,
|
||||
/** Update-check snapshots merged into every `status` result; absent when the plugin is disabled. */
|
||||
private readonly updates?: { snapshot(): PluginUpdateSnapshot | undefined },
|
||||
/**
|
||||
* Update-check snapshots merged into every `status` result, and the local
|
||||
* online upgrade. Absent when the plugin is disabled.
|
||||
*/
|
||||
private readonly updates?: {
|
||||
snapshot(): PluginUpdateSnapshot | undefined
|
||||
start?(version: string): Promise<PluginSelfUpdateResult>
|
||||
},
|
||||
private readonly useDefaultStorage = false,
|
||||
private readonly logStore?: HostLogStore,
|
||||
) {}
|
||||
|
||||
register(connection: HostConnectionHandle, webServer?: HostWebServerLike): () => Promise<void> {
|
||||
|
|
@ -168,6 +181,8 @@ export class PluginControlRuntime {
|
|||
if (endpoint === 'settings.acp.installOrConfigure') return ok(await this.installOrConfigureAcp(payload))
|
||||
if (endpoint === 'settings.logout') return ok(await this.logout())
|
||||
if (endpoint === 'settings.identity.reset') return ok(await this.resetIdentity(payload))
|
||||
if (endpoint === 'settings.logs.get') return ok(await this.getLogs(payload))
|
||||
if (endpoint === 'settings.logs.clear') return ok(await this.clearLogs())
|
||||
if (endpoint === 'host.authorization.set' && this.client !== undefined) {
|
||||
const value = record(payload)
|
||||
if (typeof value.enabled !== 'boolean') throw new ClientModeError('INVALID_MESSAGE', 'Host authorization state is required.')
|
||||
|
|
@ -183,6 +198,7 @@ export class PluginControlRuntime {
|
|||
this.host.reconnectHost()
|
||||
return withUpdate(ok(this.hostOnlyStatus()), this.updates?.snapshot())
|
||||
}
|
||||
if (endpoint === 'plugin.update') return ok(await this.updatePlugin(payload))
|
||||
if (endpoint === 'status') {
|
||||
if (this.startupFailure !== undefined) {
|
||||
const startupError = safeStartupError(this.startupFailure)
|
||||
|
|
@ -458,6 +474,45 @@ export class PluginControlRuntime {
|
|||
return this.settingsView()
|
||||
}
|
||||
|
||||
private currentLogLevel(): 'debug' | 'info' | 'warn' | 'error' {
|
||||
if (this.settings === undefined) return this.config.logLevel
|
||||
try {
|
||||
const raw = this.settings.get()?.logLevel
|
||||
if (raw === 'debug' || raw === 'info' || raw === 'warn' || raw === 'error') return raw
|
||||
} catch { /* Fall back to cached config level when raw settings cannot be read. */ }
|
||||
return this.config.logLevel
|
||||
}
|
||||
|
||||
private async getLogs(payload: unknown): Promise<PluginLogView> {
|
||||
const value = payload === undefined ? {} : record(payload)
|
||||
const filter = value.filter
|
||||
if (filter !== undefined && filter !== 'all' && filter !== 'warn') {
|
||||
throw new ClientModeError('INVALID_MESSAGE', 'The log filter must be all or warn.')
|
||||
}
|
||||
const limit = value.limit
|
||||
if (limit !== undefined && (typeof limit !== 'number' || !Number.isInteger(limit) || limit < 1 || limit > 1000)) {
|
||||
throw new ClientModeError('INVALID_MESSAGE', 'The log limit must be an integer between 1 and 1000.')
|
||||
}
|
||||
const logLevel = this.currentLogLevel()
|
||||
if (this.logStore === undefined) {
|
||||
return { logLevel, entries: [], totalBuffered: 0 }
|
||||
}
|
||||
const snapshot = await this.logStore.snapshot({ filter: filter as PluginLogFilter | undefined, limit })
|
||||
return { logLevel, ...snapshot }
|
||||
}
|
||||
|
||||
private async clearLogs(): Promise<PluginLogView> {
|
||||
if (this.settings === undefined) {
|
||||
throw new ClientModeError('SETTINGS_UNAVAILABLE', 'DSH user settings are unavailable in this profile.')
|
||||
}
|
||||
const logLevel = this.currentLogLevel()
|
||||
if (this.logStore === undefined) {
|
||||
return { logLevel, entries: [], totalBuffered: 0 }
|
||||
}
|
||||
const snapshot = await this.logStore.clear()
|
||||
return { logLevel, ...snapshot }
|
||||
}
|
||||
|
||||
private async setDevelopment(payload: unknown): Promise<PluginSettingsView> {
|
||||
if (this.settings === undefined) throw new ClientModeError('SETTINGS_UNAVAILABLE', 'DSH user settings are unavailable in this profile.')
|
||||
const value = record(payload)
|
||||
|
|
@ -746,6 +801,25 @@ export class PluginControlRuntime {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace this Desktop install through the official plugin manager.
|
||||
* A version the registry check did not publish is refused. Installs the
|
||||
* manager cannot update stay on the manual releases path.
|
||||
*/
|
||||
private async updatePlugin(payload: unknown): Promise<{ status: string; message?: string }> {
|
||||
const version = record(payload).version
|
||||
if (typeof version !== 'string' || version.trim() === '') {
|
||||
throw new ClientModeError('INVALID_MESSAGE', 'A plugin version is required.')
|
||||
}
|
||||
if (this.updates?.start === undefined) return { status: 'manual' }
|
||||
const outcome = await this.updates.start(version)
|
||||
if (outcome.status === 'rejected') {
|
||||
throw new ClientModeError('INVALID_MESSAGE', 'That version is not the published update.')
|
||||
}
|
||||
if (outcome.status === 'failed') return { status: 'failed', message: outcome.message }
|
||||
return { status: outcome.status }
|
||||
}
|
||||
|
||||
private hostOnlyStatus(): Record<string, unknown> {
|
||||
return {
|
||||
mode: 'local',
|
||||
|
|
|
|||
Loading…
Reference in New Issue