repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
The Living OS cockpit
stars
latest
clone command
git clone gitlawb://did:key:z6Mku78K...XywC/living-os-cockp...git clone gitlawb://did:key:z6Mku78K.../living-os-cockp...59751530feat: surface worker supervisor health in live work8h ago| #1 | import { promises as fs } from 'fs'; |
| #2 | import path from 'path'; |
| #3 | import { NextRequest, NextResponse } from 'next/server'; |
| #4 | import { authErrorResponse, CANONICAL_KING_ID, getUserContext } from '@/lib/user-context'; |
| #5 | |
| #6 | export const runtime = 'nodejs'; |
| #7 | |
| #8 | const CONFIG_PATH = '/home/kingbau/.config/aethon/daemons/config.toml'; |
| #9 | const CONVERGED_LLM_PATH = '/home/kingbau/Documents/Aethon-Core/aethon_core/engine/converged_llm.py'; |
| #10 | const STATE_PATH = '/home/kingbau/.config/aethon/delegation_brain.json'; |
| #11 | const B1_BRAIN_SWAP_MARKER = 'B1_DELEGATION_BRAIN_SWAP_SPINE_LOCKED'; |
| #12 | |
| #13 | type BrainOption = { |
| #14 | id: string; |
| #15 | label: string; |
| #16 | provider: string; |
| #17 | projectedDiemPer1kIn: number; |
| #18 | projectedDiemPer1kOut: number; |
| #19 | note: string; |
| #20 | }; |
| #21 | |
| #22 | const OPTIONS: BrainOption[] = [ |
| #23 | { |
| #24 | id: 'claude-opus-4-8', |
| #25 | label: 'Opus 4.8', |
| #26 | provider: 'anthropic', |
| #27 | projectedDiemPer1kIn: 0.015, |
| #28 | projectedDiemPer1kOut: 0.075, |
| #29 | note: "King's strongest delegation brain.", |
| #30 | }, |
| #31 | { |
| #32 | id: 'deepseek-v4-pro', |
| #33 | label: 'DeepSeek V4 Pro', |
| #34 | provider: 'venice', |
| #35 | projectedDiemPer1kIn: 0.002, |
| #36 | projectedDiemPer1kOut: 0.008, |
| #37 | note: 'Lower-burn specialist and delegation fallback.', |
| #38 | }, |
| #39 | { |
| #40 | id: 'deepseek-v4-flash', |
| #41 | label: 'DeepSeek V4 Flash', |
| #42 | provider: 'venice', |
| #43 | projectedDiemPer1kIn: 0.00027, |
| #44 | projectedDiemPer1kOut: 0.0011, |
| #45 | note: 'Cheap fast mode. This does not alter the locked spine.', |
| #46 | }, |
| #47 | { |
| #48 | id: 'kimi-k2-5', |
| #49 | label: 'Kimi K2.5', |
| #50 | provider: 'venice', |
| #51 | projectedDiemPer1kIn: 0.0006, |
| #52 | projectedDiemPer1kOut: 0.0025, |
| #53 | note: 'Editorial/systemic analysis lane.', |
| #54 | }, |
| #55 | { |
| #56 | id: 'openyourmind-local', |
| #57 | label: 'OYM local', |
| #58 | provider: 'local', |
| #59 | projectedDiemPer1kIn: 0, |
| #60 | projectedDiemPer1kOut: 0, |
| #61 | note: 'Local uncensored brain. No DIEM burn, GPU arbiter required.', |
| #62 | }, |
| #63 | ]; |
| #64 | |
| #65 | async function readTextSafe(file: string) { |
| #66 | try { |
| #67 | return await fs.readFile(file, 'utf-8'); |
| #68 | } catch { |
| #69 | return ''; |
| #70 | } |
| #71 | } |
| #72 | |
| #73 | function tomlValue(raw: string, key: string, fallback: string) { |
| #74 | const match = raw.match(new RegExp(`^\\s*${key}\\s*=\\s*"([^"]+)"`, 'm')); |
| #75 | return match?.[1] || fallback; |
| #76 | } |
| #77 | |
| #78 | function pyConstant(raw: string, key: string, fallback: string) { |
| #79 | const match = raw.match(new RegExp(`^${key}\\s*=\\s*"([^"]+)"`, 'm')); |
| #80 | return match?.[1] || fallback; |
| #81 | } |
| #82 | |
| #83 | async function readState(defaultModel: string) { |
| #84 | try { |
| #85 | const parsed = JSON.parse(await fs.readFile(STATE_PATH, 'utf-8')); |
| #86 | return { |
| #87 | activeDelegationModel: String(parsed.active_delegation_model || defaultModel), |
| #88 | updatedAt: parsed.updated_at || null, |
| #89 | updatedBy: parsed.updated_by || null, |
| #90 | source: STATE_PATH, |
| #91 | }; |
| #92 | } catch { |
| #93 | return { |
| #94 | activeDelegationModel: defaultModel, |
| #95 | updatedAt: null, |
| #96 | updatedBy: null, |
| #97 | source: STATE_PATH, |
| #98 | }; |
| #99 | } |
| #100 | } |
| #101 | |
| #102 | async function buildPayload() { |
| #103 | const config = await readTextSafe(CONFIG_PATH); |
| #104 | const converged = await readTextSafe(CONVERGED_LLM_PATH); |
| #105 | const spineModel = tomlValue(config, 'primary_model', 'deepseek-v4-flash'); |
| #106 | const middleModel = tomlValue(config, 'middle_model', 'deepseek-v4-flash'); |
| #107 | const premiumModel = tomlValue(config, 'premium_model', 'claude-opus-4-8'); |
| #108 | const specialistModel = pyConstant(converged, 'DEFAULT_MEMBER_MODEL', 'deepseek-v4-pro'); |
| #109 | const kingModel = pyConstant(converged, 'DEFAULT_KING_MODEL', premiumModel); |
| #110 | const localModel = pyConstant(converged, 'DEFAULT_LOCAL_MODEL', 'openyourmind-local'); |
| #111 | const state = await readState(kingModel); |
| #112 | const activeOption = OPTIONS.find(option => option.id === state.activeDelegationModel) || OPTIONS[0]; |
| #113 | |
| #114 | return { |
| #115 | ok: true, |
| #116 | marker: B1_BRAIN_SWAP_MARKER, |
| #117 | activeDelegationModel: state.activeDelegationModel, |
| #118 | updatedAt: state.updatedAt, |
| #119 | updatedBy: state.updatedBy, |
| #120 | options: OPTIONS, |
| #121 | activeOption, |
| #122 | roles: [ |
| #123 | { |
| #124 | id: 'spine', |
| #125 | label: 'Member-facing spine', |
| #126 | model: spineModel, |
| #127 | locked: true, |
| #128 | swappable: false, |
| #129 | source: 'gateway.py + daemon config', |
| #130 | note: 'Locked here. The brain swap must never change consolidated member chat.', |
| #131 | }, |
| #132 | { |
| #133 | id: 'specialist', |
| #134 | label: 'Specialists', |
| #135 | model: specialistModel, |
| #136 | locked: false, |
| #137 | swappable: false, |
| #138 | source: 'aethon_core/engine/converged_llm.py', |
| #139 | note: 'Escalation target for specialist handlers.', |
| #140 | }, |
| #141 | { |
| #142 | id: 'delegation', |
| #143 | label: "King's delegation brain", |
| #144 | model: state.activeDelegationModel, |
| #145 | locked: false, |
| #146 | swappable: true, |
| #147 | source: STATE_PATH, |
| #148 | note: 'This is the only role changed by the swap control.', |
| #149 | }, |
| #150 | { |
| #151 | id: 'local', |
| #152 | label: 'Local brain', |
| #153 | model: localModel, |
| #154 | locked: false, |
| #155 | swappable: false, |
| #156 | source: 'gpu arbiter + converged_llm', |
| #157 | note: 'OYM local stays available through the GPU arbiter.', |
| #158 | }, |
| #159 | { |
| #160 | id: 'news', |
| #161 | label: 'News/content router', |
| #162 | model: `${tomlValue(config, 'news_systemic_model', 'kimi-k2-5')} / ${middleModel}`, |
| #163 | locked: false, |
| #164 | swappable: false, |
| #165 | source: 'content_funnel / war_room topic router', |
| #166 | note: 'Systemic topics route editorial; personal topics route digestible.', |
| #167 | }, |
| #168 | ], |
| #169 | spineGuard: { |
| #170 | model: spineModel, |
| #171 | unchangedBySwap: true, |
| #172 | assertion: 'POST writes only delegation_brain.json and never edits gateway.py or primary_model.', |
| #173 | }, |
| #174 | }; |
| #175 | } |
| #176 | |
| #177 | export async function GET() { |
| #178 | try { |
| #179 | await getUserContext(); |
| #180 | return NextResponse.json(await buildPayload()); |
| #181 | } catch (error) { |
| #182 | return authErrorResponse(error); |
| #183 | } |
| #184 | } |
| #185 | |
| #186 | export async function POST(req: NextRequest) { |
| #187 | try { |
| #188 | const ctx = await getUserContext(); |
| #189 | if (ctx.canonicalMemberId !== CANONICAL_KING_ID) { |
| #190 | return NextResponse.json({ ok: false, error: 'Delegation brain swap is limited to King admin identity in v1.' }, { status: 403 }); |
| #191 | } |
| #192 | const body = await req.json().catch(() => ({})); |
| #193 | const requested = String(body?.model || '').trim(); |
| #194 | const option = OPTIONS.find(item => item.id === requested); |
| #195 | if (!option) { |
| #196 | return NextResponse.json({ ok: false, error: `Unsupported delegation brain: ${requested}` }, { status: 400 }); |
| #197 | } |
| #198 | |
| #199 | const beforeConfig = await readTextSafe(CONFIG_PATH); |
| #200 | const beforeSpine = tomlValue(beforeConfig, 'primary_model', 'deepseek-v4-flash'); |
| #201 | await fs.mkdir(path.dirname(STATE_PATH), { recursive: true }); |
| #202 | await fs.writeFile( |
| #203 | STATE_PATH, |
| #204 | JSON.stringify( |
| #205 | { |
| #206 | active_delegation_model: option.id, |
| #207 | updated_at: new Date().toISOString(), |
| #208 | updated_by: ctx.canonicalMemberId, |
| #209 | spine_model_at_write: beforeSpine, |
| #210 | safety_line: 'delegation brain only; member-facing spine is not modified', |
| #211 | }, |
| #212 | null, |
| #213 | 2, |
| #214 | ) + '\n', |
| #215 | 'utf-8', |
| #216 | ); |
| #217 | const afterConfig = await readTextSafe(CONFIG_PATH); |
| #218 | const afterSpine = tomlValue(afterConfig, 'primary_model', 'deepseek-v4-flash'); |
| #219 | const payload = await buildPayload(); |
| #220 | return NextResponse.json({ |
| #221 | ...payload, |
| #222 | changed: true, |
| #223 | spineGuard: { |
| #224 | ...payload.spineGuard, |
| #225 | before: beforeSpine, |
| #226 | after: afterSpine, |
| #227 | unchangedBySwap: beforeSpine === afterSpine, |
| #228 | }, |
| #229 | }); |
| #230 | } catch (error) { |
| #231 | return authErrorResponse(error); |
| #232 | } |
| #233 | } |
| #234 |