| 1 | import { Service } from '@deepseek-ai/cordis' |
| 2 | import { ErrorCode } from '../protocol.ts' |
| 3 | import { RpcError } from '../rpc.ts' |
| 4 | import type { AdapterHost, DshAdapter } from './adapters.ts' |
| 5 | |
| 6 | export interface SettingsDescriptor { |
| 7 | ns: string |
| 8 | writable: boolean |
| 9 | roots: string[] |
| 10 | value: Record<string, unknown> |
| 11 | revision: string |
| 12 | applied: boolean | null |
| 13 | applies: 'reload' |
| 14 | } |
| 15 | |
| 16 | export type SettingsPathOp = |
| 17 | | { op: 'set', path: readonly string[], value: unknown } |
| 18 | | { op: 'unset', path: readonly string[] } |
| 19 | |
| 20 | export interface SettingsWrite { |
| 21 | written: boolean |
| 22 | revision: string |
| 23 | applies: 'reload' |
| 24 | } |
| 25 | |
| 26 | export class SettingsConflictError extends Error { |
| 27 | readonly code = 'SETTINGS_CONFLICT' |
| 28 | constructor(readonly ns: string, readonly expected: string | undefined, readonly actual: string) { |
| 29 | super(`settings namespace "${ns}" changed since it was read`) |
| 30 | this.name = 'SettingsConflictError' |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | function plain(value: unknown, what: string): Record<string, unknown> { |
| 35 | if (typeof value !== 'object' || value === null || Array.isArray(value)) throw new RpcError(ErrorCode.InvalidParams, `${what} is a plain object`) |
| 36 | return value as Record<string, unknown> |
| 37 | } |
| 38 | |
| 39 | function revision(value: unknown): string | undefined { |
| 40 | if (value === undefined) return undefined |
| 41 | if (typeof value !== 'string' || value.length === 0) throw new RpcError(ErrorCode.InvalidParams, 'an expected revision is the revision string describe returned') |
| 42 | return value |
| 43 | } |
| 44 | |
| 45 | function descriptor(value: unknown): SettingsDescriptor { |
| 46 | const row = value as Partial<SettingsDescriptor> | null |
| 47 | if (row === null || typeof row !== 'object' || typeof row.ns !== 'string' || typeof row.revision !== 'string' || typeof row.writable !== 'boolean' |
| 48 | || !Array.isArray(row.roots) || typeof row.value !== 'object' || row.value === null || row.applies !== 'reload') { |
| 49 | throw new RpcError(ErrorCode.Internal, 'invalid settings broker response') |
| 50 | } |
| 51 | return { ns: row.ns, writable: row.writable, roots: row.roots.map(String), value: row.value, revision: row.revision, applied: typeof row.applied === 'boolean' ? row.applied : null, applies: 'reload' } |
| 52 | } |
| 53 | |
| 54 | export function hostLocalePreference(): string { |
| 55 | const fromIntl = (() => { |
| 56 | try { |
| 57 | return Intl.DateTimeFormat().resolvedOptions().locale |
| 58 | } catch { |
| 59 | return undefined |
| 60 | } |
| 61 | })() |
| 62 | const raw = fromIntl || process.env.LC_ALL || process.env.LC_MESSAGES || process.env.LANG || 'en' |
| 63 | const cleaned = String(raw).split('.')[0].replaceAll('_', '-').trim() |
| 64 | return cleaned.length > 0 ? cleaned : 'en' |
| 65 | } |
| 66 | |
| 67 | function localeDescriptor(): SettingsDescriptor { |
| 68 | return Object.freeze({ |
| 69 | ns: 'locale', |
| 70 | writable: false, |
| 71 | roots: Object.freeze([]), |
| 72 | value: Object.freeze({ preference: hostLocalePreference() }), |
| 73 | revision: 'host-locale', |
| 74 | applied: true, |
| 75 | applies: 'reload', |
| 76 | }) as unknown as SettingsDescriptor |
| 77 | } |
| 78 | |
| 79 | export const settingsAdapter: DshAdapter = { |
| 80 | service: 'settings', |
| 81 | define(host: AdapterHost) { |
| 82 | class SettingsService extends Service { |
| 83 | constructor(ctx: any) { super(ctx, 'settings') } |
| 84 | |
| 85 | get locale(): Readonly<{ preference: string }> { |
| 86 | return Object.freeze({ preference: hostLocalePreference() }) |
| 87 | } |
| 88 | |
| 89 | describe(): Promise<SettingsDescriptor[]> | SettingsDescriptor[] { |
| 90 | try { |
| 91 | host.brokerTicket(this.ctx, 'settings') |
| 92 | } catch { |
| 93 | const forms = [localeDescriptor()] |
| 94 | Object.defineProperty(forms, 'then', { |
| 95 | value(onFulfilled?: (v: SettingsDescriptor[]) => unknown, onRejected?: (e: unknown) => unknown) { |
| 96 | return Promise.resolve([localeDescriptor()]).then(onFulfilled, onRejected) |
| 97 | }, |
| 98 | }) |
| 99 | return forms |
| 100 | } |
| 101 | return host.broker(this.ctx, 'settings', 'describe', {}).then((row) => { |
| 102 | const desc = descriptor(row) |
| 103 | return desc.ns === 'locale' ? [desc] : [desc, localeDescriptor()] |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | async update(ns: unknown, patch: unknown, expectedRevision?: unknown): Promise<SettingsWrite> { |
| 108 | return this.write(ns, 'update', { patch: plain(patch, 'a settings patch') }, expectedRevision) |
| 109 | } |
| 110 | |
| 111 | async replace(ns: unknown, section: unknown, expectedRevision?: unknown): Promise<SettingsWrite> { |
| 112 | return this.write(ns, 'replace', { value: plain(section, 'a settings section') }, expectedRevision) |
| 113 | } |
| 114 | |
| 115 | async mutate(ns: unknown, ops: unknown, expectedRevision?: unknown): Promise<SettingsWrite> { |
| 116 | if (!Array.isArray(ops)) throw new RpcError(ErrorCode.InvalidParams, 'settings edits are an array of path operations') |
| 117 | return this.write(ns, 'mutate', { ops }, expectedRevision) |
| 118 | } |
| 119 | |
| 120 | private async write(ns: unknown, method: 'update' | 'replace' | 'mutate', input: Record<string, unknown>, expectedRevision: unknown): Promise<SettingsWrite> { |
| 121 | if (typeof ns !== 'string' || ns.length === 0) throw new RpcError(ErrorCode.InvalidParams, 'a settings namespace is a non-empty string') |
| 122 | const owner = host.ownerOf(this.ctx) |
| 123 | if (owner === undefined || ns !== owner.pluginName) throw new RpcError(ErrorCode.Refused, 'a plugin may edit only its own settings namespace') |
| 124 | const expected = revision(expectedRevision) |
| 125 | const result = await host.broker(this.ctx, 'settings', method, expected === undefined ? input : { ...input, expected_revision: expected }) as { written?: unknown, conflict?: unknown, revision?: unknown } | null |
| 126 | if (result === null || typeof result !== 'object' || typeof result.written !== 'boolean' || typeof result.revision !== 'string') throw new RpcError(ErrorCode.Internal, 'invalid settings broker response') |
| 127 | if (result.conflict === true) throw new SettingsConflictError(ns, expected, result.revision) |
| 128 | return { written: result.written, revision: result.revision, applies: 'reload' } |
| 129 | } |
| 130 | } |
| 131 | Object.freeze(SettingsService.prototype) |
| 132 | return SettingsService |
| 133 | }, |
| 134 | } |
| 135 |