File size: 2,260 Bytes
abcf568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { resolveStudioToolChoice } from '../../session/session-agent-config'
import { logPlotStudioSkillTrace } from '../../../observability/plot-studio-skill-trace'
import { buildSubagentPrompt } from '../session-runner-helpers'
import type { StudioSubagentRunRequest, StudioSubagentRunResult } from '../../tools/tool-runtime-context'
import type { StudioSessionRunnerDependencies } from './dependency-center'

export async function runSubagent(
  deps: Pick<StudioSessionRunnerDependencies, 'resolveSkill'>,
  input: StudioSubagentRunRequest,
  run: (input: {
    projectId: string
    session: StudioSubagentRunRequest['childSession']
    inputText: string
    customApiConfig?: StudioSubagentRunRequest['customApiConfig']
    toolChoice?: StudioSubagentRunRequest['toolChoice']
  }) => Promise<{ text: string }>,
): Promise<StudioSubagentRunResult> {
  if (input.skillName) {
    logPlotStudioSkillTrace(input.childSession.studioKind, 'skill.subagent.requested', {
      parentSessionId: input.parentSession.id,
      childSessionId: input.childSession.id,
      subagentType: input.subagentType,
      requestedSkillName: input.skillName,
      files: input.files ?? [],
    })
  }

  const skill = input.skillName && deps.resolveSkill
    ? await deps.resolveSkill(input.skillName, input.childSession)
    : undefined

  if (input.skillName) {
    logPlotStudioSkillTrace(input.childSession.studioKind, 'skill.subagent.resolved', {
      parentSessionId: input.parentSession.id,
      childSessionId: input.childSession.id,
      subagentType: input.subagentType,
      requestedSkillName: input.skillName,
      resolvedSkillName: skill?.name ?? null,
      resolvedEntryFile: skill?.entryFile ?? null,
    }, skill ? 'info' : 'warn')
  }

  const result = await run({
    projectId: input.projectId,
    session: input.childSession,
    inputText: buildSubagentPrompt({
      agentType: input.subagentType,
      inputText: input.inputText,
      skillName: input.skillName,
      skill,
      files: input.files,
      studioKind: input.childSession.studioKind
    }),
    customApiConfig: input.customApiConfig,
    toolChoice: input.toolChoice ?? resolveStudioToolChoice({ session: input.childSession })
  })

  return {
    text: result.text
  }
}