File size: 2,676 Bytes
d47b053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { createStudioAssistantMessage, createStudioRun } from '../../domain/factories'
import type {
  StudioKind,
  StudioMessagePart,
  StudioRun,
  StudioRuntimeTurnPlan,
  StudioSession
} from '../../domain/types'
import { buildStudioSubagentPrompt } from '../../prompts/subagent-prompt'
import type { StudioResolvedSkill } from '../tools/tool-runtime-context'

export function buildDraftRun(
  session: StudioSession,
  inputText: string,
  metadata?: Record<string, unknown>
): StudioRun {
  return createStudioRun({
    sessionId: session.id,
    inputText,
    activeAgent: session.agentType,
    metadata
  })
}

export function buildDraftAssistantMessage(session: StudioSession) {
  return createStudioAssistantMessage({
    sessionId: session.id,
    agent: session.agentType
  })
}

export function buildSubagentPrompt(input: {
  agentType: 'reviewer' | 'designer'
  inputText: string
  files?: string[]
  skillName?: string
  skill?: StudioResolvedSkill
  studioKind?: StudioKind
}): string {
  return buildStudioSubagentPrompt({
    agentType: input.agentType,
    workflowInput: input.inputText,
    files: input.files,
    skill: input.skill,
    requestedSkillName: input.skillName,
    studioKind: input.studioKind
  })
}

export function finalizeRunState(input: {
  run: StudioRun
  outcome: 'continue' | 'stop' | 'compact'
}): StudioRun {
  return {
    ...input.run,
    status: input.outcome === 'stop' ? 'failed' : 'completed',
    completedAt: new Date().toISOString(),
    error: input.outcome === 'stop' ? 'Run stopped after tool failure or rejection' : undefined
  }
}

export function failRunState(run: StudioRun, error: string): StudioRun {
  return {
    ...run,
    status: 'failed',
    completedAt: new Date().toISOString(),
    error
  }
}

export function cancelRunState(run: StudioRun, reason: string): StudioRun {
  return {
    ...run,
    status: 'cancelled',
    completedAt: new Date().toISOString(),
    error: reason,
  }
}

export function extractLatestAssistantText(parts: StudioMessagePart[]): string {
  const textPart = [...parts].reverse().find((part) => part.type === 'text')
  return textPart?.type === 'text' ? textPart.text : ''
}

export function withResolvedPlan<T extends { plan: StudioRuntimeTurnPlan }>(input: T): T {
  return input
}



function readRunStopReason(run: StudioRun): string | undefined {
  const autonomy = run.metadata?.autonomy
  if (!autonomy || typeof autonomy !== 'object' || Array.isArray(autonomy)) {
    return undefined
  }
  const stopReason = (autonomy as Record<string, unknown>).stopReason
  return typeof stopReason === 'string' && stopReason.trim() ? stopReason : undefined
}