{"version":3,"file":"handler.mjs","names":[],"sources":["../../src/branch/handler.ts"],"sourcesContent":["import type { Message } from '@earendil-works/pi-ai';\nimport {\n\tconvertToLlm,\n\ttype SessionBeforeTreeEvent,\n\tserializeConversation,\n} from '@earendil-works/pi-coding-agent';\n\nimport { DEFAULT_DEPS } from '@live-compaction/compaction';\nimport { renderFilesTouchedManifestBlock } from '@live-compaction/files-touched';\nimport {\n\tbuildBranchSummaryRenderVars,\n\ttype CompactionTemplate,\n\tgetBuiltInBranchSummaryTemplate,\n} from '@live-compaction/template';\nimport type { HookContext, NotifyLevel, RunDeps } from '@live-compaction/types';\n\nfunction notify(ctx: HookContext, message: string, level: NotifyLevel = 'warning'): void {\n\tif (ctx.hasUI) {\n\t\tctx.ui.notify(message, level);\n\t}\n}\n\nexport async function runGroundedBranchSummaryAugmentation(\n\tevent: SessionBeforeTreeEvent,\n\tctx: HookContext,\n\tdeps: RunDeps = DEFAULT_DEPS,\n): Promise<{ customInstructions: string; replaceInstructions: boolean } | undefined> {\n\tif (\n\t\tevent.signal.aborted ||\n\t\t!event.preparation.userWantsSummary ||\n\t\tevent.preparation.entriesToSummarize.length === 0\n\t) {\n\t\treturn undefined;\n\t}\n\n\ttry {\n\t\tconst config = await deps.loadConfig(ctx.cwd);\n\n\t\tconst paths = deps.resolvePaths(ctx.cwd);\n\t\tconst templatePath =\n\t\t\tpaths.project?.branchSummaryPromptPath ?? paths.global.branchSummaryPromptPath;\n\t\tlet template: CompactionTemplate | null = null;\n\t\ttry {\n\t\t\ttemplate = await deps.loadCompactionTemplate(templatePath);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\tnotify(\n\t\t\t\tctx,\n\t\t\t\t`Failed to load branch-summary template ${templatePath}: ${message}. Falling back to built-in.`,\n\t\t\t\t'warning',\n\t\t\t);\n\t\t\ttemplate = null;\n\t\t}\n\n\t\tconst filesTouchedBlock = config.includeFilesTouched.inBranchSummary\n\t\t\t? renderFilesTouchedManifestBlock(\n\t\t\t\t\tdeps.collectFilesTouched(event.preparation.entriesToSummarize, ctx.cwd),\n\t\t\t\t) || undefined\n\t\t\t: undefined;\n\n\t\tconst branchEntryMessages: Message[] = [];\n\t\tfor (const entry of event.preparation.entriesToSummarize) {\n\t\t\tif (entry.type === 'message') {\n\t\t\t\tbranchEntryMessages.push(entry.message as unknown as Message);\n\t\t\t}\n\t\t}\n\t\tconst branchMessagesText =\n\t\t\tbranchEntryMessages.length > 0\n\t\t\t\t? serializeConversation(convertToLlm(branchEntryMessages))\n\t\t\t\t: undefined;\n\n\t\tconst customFocus = event.preparation.customInstructions || undefined;\n\n\t\tlet promptText: string | undefined;\n\t\tconst renderTemplate = template ?? getBuiltInBranchSummaryTemplate();\n\t\ttry {\n\t\t\tconst vars = buildBranchSummaryRenderVars({\n\t\t\t\tpreviousSummary: undefined,\n\t\t\t\tbranchMessagesText,\n\t\t\t\tfilesTouchedBlock,\n\t\t\t\tcustomFocus,\n\t\t\t\tbranchEntryMessages,\n\t\t\t\tfrontmatter: renderTemplate.frontmatter,\n\t\t\t});\n\t\t\tpromptText = renderTemplate.render(vars as unknown as Record<string, unknown>);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\tnotify(\n\t\t\t\tctx,\n\t\t\t\t`Branch-summary template render failed (${message}). Falling back to legacy assembly.`,\n\t\t\t\t'warning',\n\t\t\t);\n\t\t\tpromptText = undefined;\n\t\t}\n\n\t\tif (!promptText) {\n\t\t\tconst promptContract = await deps.loadBranchSummaryPrompt(ctx.cwd);\n\t\t\tif (!promptContract && !filesTouchedBlock && !customFocus) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tconst sections: string[] = [];\n\t\t\tif (promptContract) sections.push(promptContract);\n\t\t\tif (customFocus) {\n\t\t\t\tsections.push(`## Additional focus\\n\\n${customFocus}`);\n\t\t\t}\n\t\t\tif (filesTouchedBlock) {\n\t\t\t\tsections.push(`## Files touched context\\n\\n${filesTouchedBlock}`);\n\t\t\t}\n\t\t\tpromptText = sections.join('\\n\\n').trim();\n\t\t}\n\n\t\tif (!promptText.trim()) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tcustomInstructions: promptText.trim(),\n\t\t\treplaceInstructions: true,\n\t\t};\n\t} catch (error) {\n\t\tif (event.signal.aborted) return undefined;\n\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\tnotify(ctx, `Branch summary augmentation failed: ${message}`, 'warning');\n\t\treturn undefined;\n\t}\n}\n"],"mappings":";;;;;;;;AAgBA,SAAS,OAAO,KAAkB,SAAiB,QAAqB,WAAiB;CACxF,IAAI,IAAI,OACP,IAAI,GAAG,OAAO,SAAS,KAAK;AAE9B;AAEA,eAAsB,qCACrB,OACA,KACA,OAAgB,cACoE;CACpF,IACC,MAAM,OAAO,WACb,CAAC,MAAM,YAAY,oBACnB,MAAM,YAAY,mBAAmB,WAAW,GAEhD;CAGD,IAAI;EACH,MAAM,SAAS,MAAM,KAAK,WAAW,IAAI,GAAG;EAE5C,MAAM,QAAQ,KAAK,aAAa,IAAI,GAAG;EACvC,MAAM,eACL,MAAM,SAAS,2BAA2B,MAAM,OAAO;EACxD,IAAI,WAAsC;EAC1C,IAAI;GACH,WAAW,MAAM,KAAK,uBAAuB,YAAY;EAC1D,SAAS,OAAO;GAEf,OACC,KACA,0CAA0C,aAAa,IAHxC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,EAGD,8BACnE,SACD;GACA,WAAW;EACZ;EAEA,MAAM,oBAAoB,OAAO,oBAAoB,kBAClD,gCACA,KAAK,oBAAoB,MAAM,YAAY,oBAAoB,IAAI,GAAG,CACvE,KAAK,KAAA,IACJ,KAAA;EAEH,MAAM,sBAAiC,CAAC;EACxC,KAAK,MAAM,SAAS,MAAM,YAAY,oBACrC,IAAI,MAAM,SAAS,WAClB,oBAAoB,KAAK,MAAM,OAA6B;EAG9D,MAAM,qBACL,oBAAoB,SAAS,IAC1B,sBAA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