{"version":3,"file":"foreground-control.d.ts","sourceRoot":"","sources":["../../../../src/runs/foreground/foreground-control.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAA0B,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAEzG,UAAU,yBAAyB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC;CACvB;AA0DD,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,oBAAoB,GAAG,IAAI,CAGnF;AAED,wBAAgB,+BAA+B,CAAC,OAAO,EAAE,oBAAoB,GAAG,IAAI,CAGnF;AAED,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAElF;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,yBAAyB,GAAG,IAAI,CA8B1G;AAED,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,oBAAoB,EAC7B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,aAAa,GAAG,SAAS,GACjC,IAAI,CAMN;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,oBAAoB,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAUxF","sourcesContent":["import type { AgentProgress, ForegroundChildControl, ForegroundRunControl } from \"../../shared/types.ts\";\n\ninterface BeginForegroundChildInput {\n\tindex: number;\n\tagent: string;\n\tdescription?: string;\n\tmodel?: string;\n\tthinking?: string;\n\tinterrupt: () => boolean;\n\tdetach?: () => boolean;\n}\n\nfunction copyProgress(target: ForegroundChildControl, progress: AgentProgress | undefined): void {\n\tif (!progress) return;\n\ttarget.currentActivityState = progress.activityState;\n\ttarget.lastActivityAt = progress.lastActivityAt;\n\ttarget.currentTool = progress.currentTool;\n\ttarget.currentToolStartedAt = progress.currentToolStartedAt;\n\ttarget.currentPath = progress.currentPath;\n\ttarget.turnCount = progress.turnCount;\n\ttarget.tokens = progress.tokens;\n\ttarget.inputTokens = progress.inputTokens;\n\ttarget.outputTokens = progress.outputTokens;\n\ttarget.model = progress.model;\n\ttarget.thinking = progress.thinking;\n\ttarget.toolCount = progress.toolCount;\n}\n\nfunction syncCurrentChild(control: ForegroundRunControl, child: ForegroundChildControl): void {\n\tcontrol.currentAgent = child.agent;\n\tcontrol.currentIndex = child.index;\n\tcontrol.description = child.description;\n\tcontrol.currentActivityState = child.currentActivityState;\n\tcontrol.lastActivityAt = child.lastActivityAt;\n\tcontrol.currentTool = child.currentTool;\n\tcontrol.currentToolStartedAt = child.currentToolStartedAt;\n\tcontrol.currentPath = child.currentPath;\n\tcontrol.turnCount = child.turnCount;\n\tcontrol.tokens = child.tokens;\n\tcontrol.inputTokens = child.inputTokens;\n\tcontrol.outputTokens = child.outputTokens;\n\tcontrol.model = child.model;\n\tcontrol.thinking = child.thinking;\n\tcontrol.toolCount = child.toolCount;\n\tcontrol.interrupt = child.interrupt;\n\tcontrol.detach = child.detach;\n\tcontrol.updatedAt = child.updatedAt;\n}\n\nfunction clearCurrentChild(control: ForegroundRunControl): void {\n\tcontrol.currentAgent = undefined;\n\tcontrol.currentIndex = undefined;\n\tcontrol.currentActivityState = undefined;\n\tcontrol.lastActivityAt = undefined;\n\tcontrol.currentTool = undefined;\n\tcontrol.currentToolStartedAt = undefined;\n\tcontrol.currentPath = undefined;\n\tcontrol.turnCount = undefined;\n\tcontrol.tokens = undefined;\n\tcontrol.inputTokens = undefined;\n\tcontrol.outputTokens = undefined;\n\tcontrol.model = undefined;\n\tcontrol.thinking = undefined;\n\tcontrol.toolCount = undefined;\n\tcontrol.interrupt = undefined;\n\tcontrol.detach = undefined;\n}\n\nexport function retainForegroundSchedulingOwner(control: ForegroundRunControl): void {\n\tcontrol.schedulingOwners = (control.schedulingOwners ?? 0) + 1;\n\tcontrol.updatedAt = Date.now();\n}\n\nexport function settleForegroundSchedulingOwner(control: ForegroundRunControl): void {\n\tcontrol.schedulingOwners = Math.max(0, (control.schedulingOwners ?? 0) - 1);\n\tcontrol.updatedAt = Date.now();\n}\n\nexport function foregroundSchedulingSettled(control: ForegroundRunControl): boolean {\n\treturn (control.schedulingOwners ?? 0) === 0;\n}\n\nexport function beginForegroundChild(control: ForegroundRunControl, input: BeginForegroundChildInput): void {\n\tconst now = Date.now();\n\tconst child: ForegroundChildControl = {\n\t\tindex: input.index,\n\t\tagent: input.agent,\n\t\t...(input.description ? { description: input.description } : {}),\n\t\tstartedAt: now,\n\t\tupdatedAt: now,\n\t\t...(input.model ? { model: input.model } : {}),\n\t\t...(input.thinking ? { thinking: input.thinking } : {}),\n\t};\n\tchild.interrupt = () => {\n\t\tif (!input.interrupt()) return false;\n\t\tchild.currentActivityState = undefined;\n\t\tchild.updatedAt = Date.now();\n\t\tsyncCurrentChild(control, child);\n\t\treturn true;\n\t};\n\tif (input.detach) {\n\t\tchild.detach = () => {\n\t\t\tif (!input.detach?.()) return false;\n\t\t\tchild.currentActivityState = undefined;\n\t\t\tchild.updatedAt = Date.now();\n\t\t\tsyncCurrentChild(control, child);\n\t\t\treturn true;\n\t\t};\n\t}\n\tcontrol.activeChildren ??= new Map();\n\tcontrol.activeChildren.set(input.index, child);\n\tsyncCurrentChild(control, child);\n}\n\nexport function updateForegroundChild(\n\tcontrol: ForegroundRunControl,\n\tindex: number,\n\tprogress: AgentProgress | undefined,\n): void {\n\tconst child = control.activeChildren?.get(index);\n\tif (!child) return;\n\tcopyProgress(child, progress);\n\tchild.updatedAt = Date.now();\n\tsyncCurrentChild(control, child);\n}\n\nexport function finishForegroundChild(control: ForegroundRunControl, index: number): void {\n\tcontrol.activeChildren?.delete(index);\n\tif (control.currentIndex === index) {\n\t\tconst next = [...(control.activeChildren?.values() ?? [])].sort(\n\t\t\t(left, right) => right.updatedAt - left.updatedAt || left.index - right.index,\n\t\t)[0];\n\t\tif (next) syncCurrentChild(control, next);\n\t\telse clearCurrentChild(control);\n\t}\n\tcontrol.updatedAt = Date.now();\n}\n"]}