@@ -4,7 +4,7 @@ import { type D1, parseJsonFields } from "./db";
44import { addSubkey , getOrCreateRootKey } from "./gpgKeyRepo" ;
55import { runtimeReadyPredicateSql } from "./machineRepo" ;
66
7- const parseAgent = < T extends Agent > ( row : T ) => parseJsonFields ( row , [ "skills" , "handoff_to" ] ) ;
7+ const parseAgent = < T extends Agent > ( row : T ) => parseJsonFields ( row , [ "skills" , "subagents" , " handoff_to"] ) ;
88
99export interface PreparedAgent extends Agent {
1010 privateKeyJwk : JsonWebKey ;
@@ -40,6 +40,7 @@ export async function prepareAgent(
4040 runtime : input . runtime ,
4141 model : input . model ?? null ,
4242 skills : input . skills ?? null ,
43+ subagents : input . subagents ?? null ,
4344 public_key : publicKeyBase64 ,
4445 fingerprint,
4546 builtin : builtin ? 1 : 0 ,
@@ -51,11 +52,12 @@ export async function prepareAgent(
5152
5253export async function insertAgent ( db : D1 , agent : PreparedAgent , extras ?: { mailboxToken ?: string ; gpgSubkeyId ?: string } ) : Promise < Agent > {
5354 const skillsJson = agent . skills ? JSON . stringify ( agent . skills ) : null ;
55+ const subagentsJson = agent . subagents ? JSON . stringify ( agent . subagents ) : null ;
5456 const handoffJson = agent . handoff_to ? JSON . stringify ( agent . handoff_to ) : null ;
5557 await db
5658 . prepare ( `
57- INSERT INTO agents (id, owner_id, name, username, bio, soul, role, kind, handoff_to, runtime, model, skills, public_key, private_key, fingerprint, builtin, mailbox_token, gpg_subkey_id, created_at, updated_at)
58- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
59+ INSERT INTO agents (id, owner_id, name, username, bio, soul, role, kind, handoff_to, runtime, model, skills, subagents, public_key, private_key, fingerprint, builtin, mailbox_token, gpg_subkey_id, created_at, updated_at)
60+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
5961 ` )
6062 . bind (
6163 agent . id ,
@@ -70,6 +72,7 @@ export async function insertAgent(db: D1, agent: PreparedAgent, extras?: { mailb
7072 agent . runtime ,
7173 agent . model ,
7274 skillsJson ,
75+ subagentsJson ,
7376 agent . public_key ,
7477 JSON . stringify ( agent . privateKeyJwk ) ,
7578 agent . fingerprint ,
@@ -123,7 +126,7 @@ export async function listAgents(db: D1, ownerId: string): Promise<AgentWithActi
123126 const runtimeCutoff = new Date ( Date . now ( ) - MACHINE_STALE_TIMEOUT_MS ) . toISOString ( ) ;
124127 const result = await db
125128 . prepare ( `
126- SELECT a.id, a.owner_id, a.name, a.username, a.gpg_subkey_id, a.bio, a.soul, a.role, a.kind, a.handoff_to, a.runtime, a.model, a.skills,
129+ SELECT a.id, a.owner_id, a.name, a.username, a.gpg_subkey_id, a.bio, a.soul, a.role, a.kind, a.handoff_to, a.runtime, a.model, a.skills, a.subagents,
127130 a.public_key, a.fingerprint, a.builtin, a.created_at, a.updated_at,
128131 CASE WHEN EXISTS (
129132 SELECT 1 FROM machines m, json_each(m.runtimes) rt
@@ -155,7 +158,7 @@ export async function getAgent(db: D1, agentId: string, ownerId: string): Promis
155158 const runtimeCutoff = new Date ( Date . now ( ) - MACHINE_STALE_TIMEOUT_MS ) . toISOString ( ) ;
156159 return db
157160 . prepare ( `
158- SELECT a.id, a.owner_id, a.name, a.username, a.gpg_subkey_id, a.bio, a.soul, a.role, a.kind, a.handoff_to, a.runtime, a.model, a.skills,
161+ SELECT a.id, a.owner_id, a.name, a.username, a.gpg_subkey_id, a.bio, a.soul, a.role, a.kind, a.handoff_to, a.runtime, a.model, a.skills, a.subagents,
159162 a.public_key, a.fingerprint, a.builtin, a.created_at, a.updated_at,
160163 CASE WHEN EXISTS (
161164 SELECT 1 FROM machines m, json_each(m.runtimes) rt
@@ -185,22 +188,29 @@ export async function getAgent(db: D1, agentId: string, ownerId: string): Promis
185188export async function updateAgent (
186189 db : D1 ,
187190 agentId : string ,
188- updates : Partial < Pick < Agent , "name" | "bio" | "soul" | "role" | "handoff_to" | "runtime" | "model" | "skills" > > ,
191+ updates : Partial < Pick < Agent , "name" | "bio" | "soul" | "role" | "handoff_to" | "runtime" | "model" | "skills" | "subagents" > > ,
189192) : Promise < Agent | null > {
190- const agent = await db . prepare ( "SELECT * FROM agents WHERE id = ?" ) . bind ( agentId ) . first < Agent > ( ) ;
193+ const agent = await db
194+ . prepare (
195+ "SELECT id, owner_id, name, username, gpg_subkey_id, bio, soul, role, kind, handoff_to, runtime, model, skills, subagents, public_key, fingerprint, builtin, created_at, updated_at FROM agents WHERE id = ?" ,
196+ )
197+ . bind ( agentId )
198+ . first < Agent > ( ) ;
191199 if ( ! agent ) return null ;
192200
193201 const now = new Date ( ) . toISOString ( ) ;
194202 const sets : string [ ] = [ "updated_at = ?" ] ;
195203 const binds : unknown [ ] = [ now ] ;
204+ const applied : Partial < Agent > = { } ;
196205
197- const jsonFields = new Set ( [ "skills" , "handoff_to" ] ) ;
198- const fields = [ "name" , "bio" , "soul" , "role" , "handoff_to" , "runtime" , "model" , "skills" ] as const ;
206+ const jsonFields = new Set ( [ "skills" , "subagents" , " handoff_to"] ) ;
207+ const fields = [ "name" , "bio" , "soul" , "role" , "handoff_to" , "runtime" , "model" , "skills" , "subagents" ] as const ;
199208 for ( const field of fields ) {
200209 if ( field in updates && ( updates as any ) [ field ] !== undefined ) {
201210 sets . push ( `${ field } = ?` ) ;
202211 const val = ( updates as any ) [ field ] ;
203212 binds . push ( jsonFields . has ( field ) && val != null ? JSON . stringify ( val ) : val ) ;
213+ ( applied as any ) [ field ] = val ;
204214 }
205215 }
206216
@@ -209,7 +219,7 @@ export async function updateAgent(
209219 . prepare ( `UPDATE agents SET ${ sets . join ( ", " ) } WHERE id = ?` )
210220 . bind ( ...binds )
211221 . run ( ) ;
212- return parseAgent ( { ...agent , ...updates , updated_at : now } ) ;
222+ return parseAgent ( { ...agent , ...applied , updated_at : now } ) ;
213223}
214224
215225export async function deleteAgent ( db : D1 , agentId : string ) : Promise < boolean > {
0 commit comments