38 lines
2.4 KiB
TypeScript
38 lines
2.4 KiB
TypeScript
/** Child configuration and start events share the transaction that creates ownership. */
|
|
import { AgentDoc, configure } from '@earendil-works/pi-durable'
|
|
import { toolsFor } from '../core/agents.ts'
|
|
import { Started } from './refs.ts'
|
|
import { Profile } from './api.ts'
|
|
export async function started(host: any, tx: any, conversation: any, agent: string, kind: 'child'|'standing'|'branch') {
|
|
const {model, thinkingLevel} = await tx.doc(AgentDoc, conversation.id)
|
|
const parent = conversation.owner?.conversationId ?? conversation.parent?.conversationId
|
|
await host.emit(tx, Started, {id:conversation.id, agent, kind,
|
|
...(model ? {model:`${model.provider}/${model.modelId}`} : {}),
|
|
...(thinkingLevel ? {thinking:thinkingLevel} : {}), ...(parent === undefined ? {} : {parent})})
|
|
}
|
|
export async function createChild(host: any, tx: any, options: any) {
|
|
const {parentAgent:parent, definition} = options
|
|
const child = options.fork
|
|
? await tx.forkConversation(options.fork.conversationId, options.fork.at, {ownership:options.owner})
|
|
: await tx.createConversation({ownership:options.owner})
|
|
await configure(tx, child.id, childConfiguration(options))
|
|
Object.assign(await tx.doc(host.metadata, child.id), {label:options.label, title:options.title})
|
|
if (definition) (await tx.doc(Profile, child.id)).definition = definition.name
|
|
if (definition?.summaries === false) await host.want({name:'conversations'})?.setAutomaticSummaries(tx, child.id, false)
|
|
return child
|
|
}
|
|
/** Standing refreshes and new children apply the same definition and tool denials. */
|
|
export function childConfiguration(options: any) {
|
|
const {parentAgent:parent, definition} = options
|
|
const inherited = parent.tools.map((tool: any) => tool.name)
|
|
const names = (definition ? toolsFor(definition, inherited) : inherited).filter((name: string) => !options.excludeTools?.includes(name))
|
|
return {
|
|
tools:parent.tools.filter((tool: any) => names.includes(tool.name)),
|
|
extensions:parent.extensions.filter((extension: any) => !options.excludeExtensions?.includes(extension.name)),
|
|
instructions:definition?.instructions ?? parent.instructions ?? null,
|
|
model:options.model ?? definition?.runs?.model ?? parent.model ?? null,
|
|
thinkingLevel:options.thinking ?? definition?.runs?.thinking ?? definition?.thinking ?? parent.thinkingLevel ?? null,
|
|
cwd:options.cwd ?? parent.cwd ?? '/src/sausage',
|
|
}
|
|
}
|