|
| 1 | +import type { |
| 2 | + AgentDeviceRuntime, |
| 3 | + AgentDeviceRuntimeConfig, |
| 4 | + CommandPolicy, |
| 5 | + CommandSessionRecord, |
| 6 | + CommandSessionStore, |
| 7 | +} from './runtime-contract.ts'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Assembles an in-process runtime from its backend, artifact adapter, session store, policy and |
| 11 | + * cancellation inputs. Carries no command surface: whoever needs bound commands composes them on |
| 12 | + * the result, so a consumer that only reads `backend`/`sessions`/`policy` does not evaluate the |
| 13 | + * command families it never dispatches. |
| 14 | + */ |
| 15 | +export function createAgentDeviceRuntime(config: AgentDeviceRuntimeConfig): AgentDeviceRuntime { |
| 16 | + return { |
| 17 | + backend: config.backend, |
| 18 | + artifacts: config.artifacts, |
| 19 | + sessions: config.sessions ?? createMemorySessionStore(), |
| 20 | + policy: config.policy ?? restrictedCommandPolicy(), |
| 21 | + diagnostics: config.diagnostics, |
| 22 | + clock: config.clock, |
| 23 | + signal: config.signal, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +export function createMemorySessionStore( |
| 28 | + records: readonly CommandSessionRecord[] = [], |
| 29 | +): CommandSessionStore { |
| 30 | + const sessions = new Map(records.map((record) => [record.name, cloneSessionRecord(record)])); |
| 31 | + return { |
| 32 | + get: (name) => cloneSessionRecord(sessions.get(name)), |
| 33 | + set: (record) => { |
| 34 | + sessions.set(record.name, cloneSessionRecord(record)); |
| 35 | + }, |
| 36 | + delete: (name) => { |
| 37 | + sessions.delete(name); |
| 38 | + }, |
| 39 | + list: () => Array.from(sessions.values(), (record) => cloneSessionRecord(record)), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function cloneSessionRecord(record: CommandSessionRecord): CommandSessionRecord; |
| 44 | +function cloneSessionRecord(record: undefined): undefined; |
| 45 | +function cloneSessionRecord( |
| 46 | + record: CommandSessionRecord | undefined, |
| 47 | +): CommandSessionRecord | undefined; |
| 48 | +function cloneSessionRecord( |
| 49 | + record: CommandSessionRecord | undefined, |
| 50 | +): CommandSessionRecord | undefined { |
| 51 | + if (!record) return undefined; |
| 52 | + return { |
| 53 | + ...record, |
| 54 | + ...(record.snapshot ? { snapshot: structuredClone(record.snapshot) } : {}), |
| 55 | + ...(record.metadata ? { metadata: cloneMetadata(record.metadata) } : {}), |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +function cloneMetadata(metadata: Record<string, unknown>): Record<string, unknown> { |
| 60 | + try { |
| 61 | + return structuredClone(metadata) as Record<string, unknown>; |
| 62 | + } catch { |
| 63 | + return { ...metadata }; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export function localCommandPolicy(overrides: Partial<CommandPolicy> = {}): CommandPolicy { |
| 68 | + return { |
| 69 | + allowLocalInputPaths: true, |
| 70 | + allowLocalOutputPaths: true, |
| 71 | + maxImagePixels: 20_000_000, |
| 72 | + ...overrides, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +export function restrictedCommandPolicy(overrides: Partial<CommandPolicy> = {}): CommandPolicy { |
| 77 | + return { |
| 78 | + allowLocalInputPaths: false, |
| 79 | + allowLocalOutputPaths: false, |
| 80 | + maxImagePixels: 20_000_000, |
| 81 | + ...overrides, |
| 82 | + }; |
| 83 | +} |
0 commit comments