239
*/
240
export function buildSubagentTurnsFromHistory(
242
>
parentToolCallId: string,
243
>
childSessionUri: string,
244
>
): Turn[] {
245
>
const innerToolCallIds = new Set<string>();
246
>
for (const msg of parentMessages) {
247
>
if ((msg.type === 'tool_start' || msg.type === 'tool_complete') && msg.parentToolCallId === parentToolCallId) {
248
>
innerToolCallIds.add(msg.toolCallId);
249
>
}
250
>
}
251
>
252
>
const subagentsByToolCallId = new Map<string, IHistorySubagentStartedRecord>();
253
>
for (const msg of parentMessages) {
254
>
if (msg.type === 'subagent_started' && innerToolCallIds.has(msg.toolCallId)) {
255
subagentsByToolCallId.set(msg.toolCallId, msg);
256
}
258
>
259
>
const innerMessages = parentMessages.filter(msg => {
260
>
if (msg.type === 'tool_start' || msg.type === 'tool_complete') {
261
>
return msg.parentToolCallId === parentToolCallId;
262
>
}
263
>
if (msg.type === 'message') {
264
>
return msg.parentToolCallId === parentToolCallId;
265
>
}
266
>
return false;
267
>
});
268
>
269
>
if (innerMessages.length === 0) {
270
return [];
271
}
273
>
const responseParts: ResponsePart[] = [];
274
>
const pendingTools = new Map<string, IHistoryToolStartRecord>();
275
>
276
>
for (const msg of innerMessages) {
277
>
if (msg.type === 'tool_start') {
278
>
pendingTools.set(msg.toolCallId, msg);
279
>
} else if (msg.type === 'tool_complete') {
280
>
const start = pendingTools.get(msg.toolCallId);
281
>
pendingTools.delete(msg.toolCallId);
282
>
283
>
const subagentEvent = subagentsByToolCallId.get(msg.toolCallId);
284
>
const contentWithSubagent = msg.result.content ? [...msg.result.content] : [];
285
>
if (subagentEvent) {
286
contentWithSubagent.push({
287
type: ToolResultContentType.Subagent,