Spaces:
Sleeping
Sleeping
| import { mkdir, readFile, writeFile } from 'node:fs/promises'; | |
| import { dirname, join } from 'node:path'; | |
| import { homedir } from 'node:os'; | |
| const DEFAULT_STATE_PATH = join(homedir(), '.config', 'sin', 'sin-code-datascience', 'onboarding-state.json'); | |
| type OnboardingState = { | |
| ownerEmail?: string; | |
| notes?: string; | |
| updatedAt?: string; | |
| }; | |
| export type TemplateAgentAction = | |
| | { action: 'agent.help' } | |
| | { action: 'sin.code.datascience.health' } | |
| | { action: 'sin.code.datascience.onboarding.status' } | |
| | { action: 'sin.code.datascience.onboarding.save'; ownerEmail?: string; notes?: string; confirm?: boolean }; | |
| export async function executeTemplateAgentAction(action: TemplateAgentAction): Promise<unknown> { | |
| switch (action.action) { | |
| case 'agent.help': | |
| return buildHelpPayload(); | |
| case 'sin.code.datascience.health': | |
| return { | |
| ok: true, | |
| agent: 'sin-code-datascience', | |
| primaryModel: 'opencode/qwen3.6-plus-free', | |
| team: 'Team Coding', | |
| }; | |
| case 'sin.code.datascience.onboarding.status': | |
| return { | |
| ok: true, | |
| statePath: DEFAULT_STATE_PATH, | |
| state: await readState(), | |
| }; | |
| case 'sin.code.datascience.onboarding.save': | |
| if (!action.confirm) { | |
| throw new Error('input_required:confirm=true required'); | |
| } | |
| return { | |
| ok: true, | |
| statePath: DEFAULT_STATE_PATH, | |
| state: await writeState({ | |
| ownerEmail: clean(action.ownerEmail), | |
| notes: clean(action.notes), | |
| updatedAt: new Date().toISOString(), | |
| }), | |
| }; | |
| } | |
| } | |
| function buildHelpPayload() { | |
| return { | |
| ok: true, | |
| agent: 'sin-code-datascience', | |
| actions: ['sin.code.datascience.health', 'sin.code.datascience.onboarding.status', 'sin.code.datascience.onboarding.save'], | |
| controlPlane: { | |
| projectionMode: 'control-plane-first', | |
| capabilityRegistry: 'control_plane.capabilities', | |
| consumerAuthScript: './scripts/hf_pull_script.py', | |
| completeInstallScript: './scripts/complete-install.sh', | |
| }, | |
| }; | |
| } | |
| async function readState(): Promise<OnboardingState | null> { | |
| try { | |
| return JSON.parse(await readFile(DEFAULT_STATE_PATH, 'utf8')) as OnboardingState; | |
| } catch { | |
| return null; | |
| } | |
| } | |
| async function writeState(state: OnboardingState) { | |
| await mkdir(dirname(DEFAULT_STATE_PATH), { recursive: true }); | |
| await writeFile(DEFAULT_STATE_PATH, `${JSON.stringify(state, null, 2)}\n`, 'utf8'); | |
| return state; | |
| } | |
| function clean(value: string | undefined) { | |
| const normalized = String(value || '').trim(); | |
| return normalized || undefined; | |
| } | |