@oh-my-pi/pi-coding-agent 15.0.1 → 15.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +94 -1
- package/examples/custom-tools/README.md +11 -7
- package/examples/custom-tools/hello/index.ts +2 -2
- package/examples/extensions/README.md +19 -8
- package/examples/extensions/api-demo.ts +15 -19
- package/examples/extensions/hello.ts +5 -6
- package/examples/extensions/plan-mode.ts +1 -1
- package/examples/extensions/reload-runtime.ts +4 -3
- package/examples/extensions/with-deps/index.ts +4 -3
- package/examples/sdk/06-extensions.ts +4 -2
- package/package.json +8 -18
- package/src/autoresearch/tools/init-experiment.ts +38 -41
- package/src/autoresearch/tools/log-experiment.ts +32 -41
- package/src/autoresearch/tools/run-experiment.ts +3 -3
- package/src/autoresearch/tools/update-notes.ts +11 -11
- package/src/commands/commit.ts +10 -0
- package/src/commit/agentic/tools/analyze-file.ts +4 -4
- package/src/commit/agentic/tools/git-file-diff.ts +4 -4
- package/src/commit/agentic/tools/git-hunk.ts +5 -5
- package/src/commit/agentic/tools/git-overview.ts +4 -4
- package/src/commit/agentic/tools/propose-changelog.ts +13 -13
- package/src/commit/agentic/tools/propose-commit.ts +6 -6
- package/src/commit/agentic/tools/recent-commits.ts +3 -3
- package/src/commit/agentic/tools/schemas.ts +28 -28
- package/src/commit/agentic/tools/split-commit.ts +22 -21
- package/src/commit/analysis/summary.ts +4 -4
- package/src/commit/changelog/generate.ts +7 -11
- package/src/commit/shared-llm.ts +22 -34
- package/src/config/config-file.ts +35 -13
- package/src/config/model-registry.ts +40 -191
- package/src/config/models-config-schema.ts +166 -0
- package/src/config/settings-schema.ts +29 -0
- package/src/discovery/claude-plugins.ts +19 -7
- package/src/edit/index.ts +2 -2
- package/src/edit/modes/apply-patch.ts +7 -6
- package/src/edit/modes/patch.ts +18 -25
- package/src/edit/modes/replace.ts +18 -20
- package/src/eval/js/shared/rewrite-imports.ts +131 -10
- package/src/eval/py/executor.ts +233 -623
- package/src/eval/py/kernel.ts +27 -2
- package/src/eval/py/runner.py +42 -11
- package/src/eval/py/runtime.ts +1 -0
- package/src/exa/factory.ts +5 -4
- package/src/exa/mcp-client.ts +1 -1
- package/src/exa/researcher.ts +9 -20
- package/src/exa/search.ts +26 -52
- package/src/exa/types.ts +1 -1
- package/src/exa/websets.ts +54 -53
- package/src/exec/bash-executor.ts +2 -1
- package/src/extensibility/custom-commands/loader.ts +5 -3
- package/src/extensibility/custom-commands/types.ts +4 -2
- package/src/extensibility/custom-tools/loader.ts +5 -3
- package/src/extensibility/custom-tools/types.ts +7 -6
- package/src/extensibility/custom-tools/wrapper.ts +1 -1
- package/src/extensibility/extensions/get-commands-handler.ts +77 -0
- package/src/extensibility/extensions/loader.ts +7 -3
- package/src/extensibility/extensions/types.ts +9 -5
- package/src/extensibility/extensions/wrapper.ts +1 -2
- package/src/extensibility/hooks/loader.ts +3 -1
- package/src/extensibility/hooks/tool-wrapper.ts +1 -1
- package/src/extensibility/hooks/types.ts +4 -2
- package/src/extensibility/plugins/legacy-pi-compat.ts +78 -31
- package/src/extensibility/shared-events.ts +1 -1
- package/src/extensibility/typebox.ts +391 -0
- package/src/goals/tools/goal-tool.ts +6 -12
- package/src/hashline/input.ts +2 -1
- package/src/hashline/parser.ts +27 -3
- package/src/hashline/types.ts +4 -4
- package/src/hindsight/state.ts +2 -2
- package/src/index.ts +0 -2
- package/src/internal-urls/docs-index.generated.ts +15 -15
- package/src/internal-urls/router.ts +8 -0
- package/src/internal-urls/types.ts +21 -0
- package/src/lsp/config.ts +15 -6
- package/src/lsp/defaults.json +6 -2
- package/src/lsp/types.ts +30 -38
- package/src/mcp/manager.ts +1 -1
- package/src/mcp/tool-bridge.ts +1 -1
- package/src/modes/acp/acp-agent.ts +248 -50
- package/src/modes/components/session-observer-overlay.ts +12 -1
- package/src/modes/components/status-line/segments.ts +39 -4
- package/src/modes/controllers/command-controller.ts +27 -2
- package/src/modes/controllers/event-controller.ts +3 -4
- package/src/modes/controllers/extension-ui-controller.ts +3 -2
- package/src/modes/interactive-mode.ts +1 -1
- package/src/modes/rpc/host-tools.ts +1 -1
- package/src/modes/rpc/host-uris.ts +235 -0
- package/src/modes/rpc/rpc-client.ts +1 -1
- package/src/modes/rpc/rpc-mode.ts +27 -1
- package/src/modes/rpc/rpc-types.ts +58 -1
- package/src/modes/runtime-init.ts +2 -1
- package/src/modes/theme/defaults/dark-poimandres.json +1 -0
- package/src/modes/theme/defaults/light-poimandres.json +1 -0
- package/src/modes/theme/theme.ts +117 -117
- package/src/modes/types.ts +1 -1
- package/src/modes/utils/context-usage.ts +2 -2
- package/src/prompts/tools/github.md +4 -4
- package/src/prompts/tools/hashline.md +22 -26
- package/src/prompts/tools/read.md +55 -37
- package/src/sdk.ts +31 -8
- package/src/session/agent-session.ts +74 -104
- package/src/session/messages.ts +16 -51
- package/src/session/session-manager.ts +22 -2
- package/src/session/streaming-output.ts +16 -6
- package/src/task/discovery.ts +5 -2
- package/src/task/executor.ts +210 -87
- package/src/task/index.ts +15 -11
- package/src/task/render.ts +32 -5
- package/src/task/types.ts +54 -39
- package/src/tools/ask.ts +12 -12
- package/src/tools/ast-edit.ts +11 -15
- package/src/tools/ast-grep.ts +9 -10
- package/src/tools/bash-command-fixup.ts +47 -0
- package/src/tools/bash.ts +48 -38
- package/src/tools/browser/render.ts +2 -2
- package/src/tools/browser.ts +39 -53
- package/src/tools/calculator.ts +12 -11
- package/src/tools/checkpoint.ts +7 -7
- package/src/tools/debug.ts +40 -43
- package/src/tools/eval.ts +16 -10
- package/src/tools/find.ts +10 -13
- package/src/tools/gh.ts +108 -132
- package/src/tools/hindsight-recall.ts +4 -6
- package/src/tools/hindsight-reflect.ts +5 -5
- package/src/tools/hindsight-retain.ts +15 -17
- package/src/tools/image-gen.ts +31 -81
- package/src/tools/index.ts +4 -1
- package/src/tools/inspect-image.ts +8 -9
- package/src/tools/irc.ts +15 -27
- package/src/tools/job.ts +30 -28
- package/src/tools/output-meta.ts +26 -0
- package/src/tools/read.ts +39 -12
- package/src/tools/recipe/index.ts +7 -9
- package/src/tools/render-mermaid.ts +12 -12
- package/src/tools/report-tool-issue.ts +4 -4
- package/src/tools/resolve.ts +11 -11
- package/src/tools/review.ts +14 -26
- package/src/tools/search-tool-bm25.ts +7 -9
- package/src/tools/search.ts +19 -22
- package/src/tools/ssh.ts +10 -9
- package/src/tools/todo-write.ts +26 -34
- package/src/tools/vim.ts +10 -26
- package/src/tools/write.ts +25 -5
- package/src/tools/yield.ts +100 -54
- package/src/web/search/index.ts +9 -24
- package/src/web/search/providers/anthropic.ts +5 -0
- package/src/web/search/providers/exa.ts +3 -0
- package/src/web/search/providers/gemini.ts +5 -0
- package/src/web/search/providers/jina.ts +5 -2
- package/src/web/search/providers/zai.ts +5 -2
- package/src/prompts/compaction/branch-summary-context.md +0 -5
- package/src/prompts/compaction/branch-summary-preamble.md +0 -2
- package/src/prompts/compaction/branch-summary.md +0 -30
- package/src/prompts/compaction/compaction-short-summary.md +0 -9
- package/src/prompts/compaction/compaction-summary-context.md +0 -5
- package/src/prompts/compaction/compaction-summary.md +0 -38
- package/src/prompts/compaction/compaction-turn-prefix.md +0 -17
- package/src/prompts/compaction/compaction-update-summary.md +0 -45
- package/src/prompts/system/auto-handoff-threshold-focus.md +0 -1
- package/src/prompts/system/file-operations.md +0 -10
- package/src/prompts/system/handoff-document.md +0 -49
- package/src/prompts/system/summarization-system.md +0 -3
- package/src/session/compaction/branch-summarization.ts +0 -324
- package/src/session/compaction/compaction.ts +0 -1420
- package/src/session/compaction/errors.ts +0 -31
- package/src/session/compaction/index.ts +0 -8
- package/src/session/compaction/pruning.ts +0 -91
- package/src/session/compaction/utils.ts +0 -184
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
You MUST create a structured summary of the conversation branch for context when returning.
|
|
2
|
-
|
|
3
|
-
You MUST use EXACT format:
|
|
4
|
-
|
|
5
|
-
## Goal
|
|
6
|
-
|
|
7
|
-
[What user trying to accomplish in this branch?]
|
|
8
|
-
|
|
9
|
-
## Constraints & Preferences
|
|
10
|
-
- [Constraints, preferences, requirements mentioned]
|
|
11
|
-
- [(none) if none mentioned]
|
|
12
|
-
|
|
13
|
-
## Progress
|
|
14
|
-
|
|
15
|
-
### Done
|
|
16
|
-
- [x] [Completed tasks/changes]
|
|
17
|
-
|
|
18
|
-
### In Progress
|
|
19
|
-
- [ ] [Work started but not finished]
|
|
20
|
-
|
|
21
|
-
### Blocked
|
|
22
|
-
- [Issues preventing progress]
|
|
23
|
-
|
|
24
|
-
## Key Decisions
|
|
25
|
-
- **[Decision]**: [Brief rationale]
|
|
26
|
-
|
|
27
|
-
## Next Steps
|
|
28
|
-
1. [What should happen next to continue]
|
|
29
|
-
|
|
30
|
-
Sections MUST be kept concise. You MUST preserve exact file paths, function names, error messages.
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
You MUST summarize what was done in this conversation, written like a pull request description.
|
|
2
|
-
|
|
3
|
-
Rules:
|
|
4
|
-
- MUST be 2-3 sentences max
|
|
5
|
-
- MUST describe the changes made, not the process
|
|
6
|
-
- NEVER mention running tests, builds, or other validation steps
|
|
7
|
-
- NEVER explain what the user asked for
|
|
8
|
-
- MUST write in first person (I added…, I fixed…)
|
|
9
|
-
- NEVER ask questions
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
Another language model started to solve this problem and produced a summary of its thinking process. You also have access to the state of the tools that were used by that language model. You MUST use this to build on the work that has already been done and NEVER duplicate work. Here is the summary produced by the other language model; you MUST use the information in this summary to assist with your own analysis:
|
|
2
|
-
|
|
3
|
-
<summary>
|
|
4
|
-
{{summary}}
|
|
5
|
-
</summary>
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
You MUST summarize the conversation above into a structured context checkpoint handoff summary for another LLM to resume task.
|
|
2
|
-
|
|
3
|
-
IMPORTANT: If conversation ends with unanswered question to user or imperative/request awaiting user response (e.g., "Please run command and paste output"), you MUST preserve that exact question/request.
|
|
4
|
-
|
|
5
|
-
You MUST use this format (sections can be omitted if not applicable):
|
|
6
|
-
|
|
7
|
-
## Goal
|
|
8
|
-
[User goals; list multiple if session covers different tasks.]
|
|
9
|
-
|
|
10
|
-
## Constraints & Preferences
|
|
11
|
-
- [Constraints or requirements mentioned]
|
|
12
|
-
|
|
13
|
-
## Progress
|
|
14
|
-
|
|
15
|
-
### Done
|
|
16
|
-
- [x] [Completed tasks/changes]
|
|
17
|
-
|
|
18
|
-
### In Progress
|
|
19
|
-
- [ ] [Current work]
|
|
20
|
-
|
|
21
|
-
### Blocked
|
|
22
|
-
- [Issues preventing progress]
|
|
23
|
-
|
|
24
|
-
## Key Decisions
|
|
25
|
-
- **[Decision]**: [Brief rationale]
|
|
26
|
-
|
|
27
|
-
## Next Steps
|
|
28
|
-
1. [Ordered list of next actions]
|
|
29
|
-
|
|
30
|
-
## Critical Context
|
|
31
|
-
- [Important data, pending questions, references]
|
|
32
|
-
|
|
33
|
-
## Additional Notes
|
|
34
|
-
[Anything else important not covered above]
|
|
35
|
-
|
|
36
|
-
You MUST output only the structured summary; you NEVER include extra text.
|
|
37
|
-
|
|
38
|
-
Sections MUST be kept concise. You MUST preserve exact file paths, function names, error messages, and relevant tool outputs or command results. You MUST include repository state changes (branch, uncommitted changes) if mentioned.
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.
|
|
2
|
-
|
|
3
|
-
You MUST summarize the prefix to provide context for the retained suffix:
|
|
4
|
-
|
|
5
|
-
## Original Request
|
|
6
|
-
|
|
7
|
-
[What did the user ask for in this turn?]
|
|
8
|
-
|
|
9
|
-
## Early Progress
|
|
10
|
-
- [Key decisions and work done in the prefix]
|
|
11
|
-
|
|
12
|
-
## Context for Suffix
|
|
13
|
-
- [Information needed to understand the retained recent work]
|
|
14
|
-
|
|
15
|
-
You MUST output only the structured summary. You NEVER include extra text.
|
|
16
|
-
|
|
17
|
-
You MUST be concise. You MUST preserve exact file paths, function names, error messages, and relevant tool outputs or command results if they appear. You MUST focus on what's needed to understand the kept suffix.
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
You MUST incorporate new messages above into the existing handoff summary in <previous-summary> tags, used by another LLM to resume task.
|
|
2
|
-
RULES:
|
|
3
|
-
- MUST preserve all information from previous summary
|
|
4
|
-
- MUST add new progress, decisions, and context from new messages
|
|
5
|
-
- MUST update Progress: move items from "In Progress" to "Done" when completed
|
|
6
|
-
- MUST update "Next Steps" based on what was accomplished
|
|
7
|
-
- MUST preserve exact file paths, function names, and error messages
|
|
8
|
-
- You MAY remove anything no longer relevant
|
|
9
|
-
|
|
10
|
-
IMPORTANT: If new messages end with unanswered question or request to user, you MUST add it to Critical Context (replacing any previous pending question if answered).
|
|
11
|
-
|
|
12
|
-
You MUST use this format (omit sections if not applicable):
|
|
13
|
-
|
|
14
|
-
## Goal
|
|
15
|
-
[Preserve existing goals; add new ones if task expanded]
|
|
16
|
-
|
|
17
|
-
## Constraints & Preferences
|
|
18
|
-
- [Preserve existing; add new ones discovered]
|
|
19
|
-
|
|
20
|
-
## Progress
|
|
21
|
-
|
|
22
|
-
### Done
|
|
23
|
-
- [x] [Include previously done and newly completed items]
|
|
24
|
-
|
|
25
|
-
### In Progress
|
|
26
|
-
- [ ] [Current work—update based on progress]
|
|
27
|
-
|
|
28
|
-
### Blocked
|
|
29
|
-
- [Current blockers—remove if resolved]
|
|
30
|
-
|
|
31
|
-
## Key Decisions
|
|
32
|
-
- **[Decision]**: [Brief rationale] (preserve all previous, add new)
|
|
33
|
-
|
|
34
|
-
## Next Steps
|
|
35
|
-
1. [Update based on current state]
|
|
36
|
-
|
|
37
|
-
## Critical Context
|
|
38
|
-
- [Preserve important context; add new if needed]
|
|
39
|
-
|
|
40
|
-
## Additional Notes
|
|
41
|
-
[Other important info not fitting above]
|
|
42
|
-
|
|
43
|
-
You MUST output only the structured summary; you NEVER include extra text.
|
|
44
|
-
|
|
45
|
-
Sections MUST be kept concise. You MUST preserve relevant tool outputs/command results. You MUST include repository state changes (branch, uncommitted changes) if mentioned.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Threshold-triggered maintenance: preserve critical implementation state and immediate next actions.
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
<critical>
|
|
2
|
-
Write a handoff document for another instance of yourself.
|
|
3
|
-
The handoff MUST be sufficient for seamless continuation without access to this conversation.
|
|
4
|
-
Output ONLY the handoff document. No preamble, no commentary, no wrapper text.
|
|
5
|
-
</critical>
|
|
6
|
-
|
|
7
|
-
<instruction>
|
|
8
|
-
Capture exact technical state, not abstractions.
|
|
9
|
-
- File paths, symbol names, commands run
|
|
10
|
-
- Test results, observed failures
|
|
11
|
-
- Decisions made
|
|
12
|
-
- Partial work affecting the next step
|
|
13
|
-
</instruction>
|
|
14
|
-
|
|
15
|
-
<output>
|
|
16
|
-
Use exactly this structure:
|
|
17
|
-
|
|
18
|
-
## Goal
|
|
19
|
-
[What the user is trying to accomplish]
|
|
20
|
-
|
|
21
|
-
## Constraints & Preferences
|
|
22
|
-
- [Any constraints, preferences, or requirements mentioned]
|
|
23
|
-
|
|
24
|
-
## Progress
|
|
25
|
-
### Done
|
|
26
|
-
- [x] [Completed tasks with specifics]
|
|
27
|
-
|
|
28
|
-
### In Progress
|
|
29
|
-
- [ ] [Current work if any]
|
|
30
|
-
|
|
31
|
-
### Pending
|
|
32
|
-
- [ ] [Tasks mentioned but not started]
|
|
33
|
-
|
|
34
|
-
## Key Decisions
|
|
35
|
-
- **[Decision]**: [Rationale]
|
|
36
|
-
|
|
37
|
-
## Critical Context
|
|
38
|
-
- Code snippets, file paths, function/type names, error messages, data essential to continue
|
|
39
|
-
- Repository state if relevant
|
|
40
|
-
|
|
41
|
-
## Next Steps
|
|
42
|
-
1. [What should happen next]
|
|
43
|
-
</output>
|
|
44
|
-
|
|
45
|
-
{{#if additionalFocus}}
|
|
46
|
-
<instruction>
|
|
47
|
-
Additional focus: {{additionalFocus}}
|
|
48
|
-
</instruction>
|
|
49
|
-
{{/if}}
|
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Branch summarization for tree navigation.
|
|
3
|
-
*
|
|
4
|
-
* When navigating to a different point in the session tree, this generates
|
|
5
|
-
* a summary of the branch being left so context isn't lost.
|
|
6
|
-
*/
|
|
7
|
-
import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
|
|
8
|
-
import type { Model } from "@oh-my-pi/pi-ai";
|
|
9
|
-
import { completeSimple } from "@oh-my-pi/pi-ai";
|
|
10
|
-
import { prompt } from "@oh-my-pi/pi-utils";
|
|
11
|
-
import branchSummaryPrompt from "../../prompts/compaction/branch-summary.md" with { type: "text" };
|
|
12
|
-
import branchSummaryPreamble from "../../prompts/compaction/branch-summary-preamble.md" with { type: "text" };
|
|
13
|
-
import {
|
|
14
|
-
convertToLlm,
|
|
15
|
-
createBranchSummaryMessage,
|
|
16
|
-
createCompactionSummaryMessage,
|
|
17
|
-
createCustomMessage,
|
|
18
|
-
} from "../../session/messages";
|
|
19
|
-
import type { ReadonlySessionManager, SessionEntry } from "../../session/session-manager";
|
|
20
|
-
import { estimateTokens } from "./compaction";
|
|
21
|
-
import {
|
|
22
|
-
computeFileLists,
|
|
23
|
-
createFileOps,
|
|
24
|
-
extractFileOpsFromMessage,
|
|
25
|
-
type FileOperations,
|
|
26
|
-
SUMMARIZATION_SYSTEM_PROMPT,
|
|
27
|
-
serializeConversation,
|
|
28
|
-
upsertFileOperations,
|
|
29
|
-
} from "./utils";
|
|
30
|
-
|
|
31
|
-
// ============================================================================
|
|
32
|
-
// Types
|
|
33
|
-
// ============================================================================
|
|
34
|
-
|
|
35
|
-
export interface BranchSummaryResult {
|
|
36
|
-
summary?: string;
|
|
37
|
-
readFiles?: string[];
|
|
38
|
-
modifiedFiles?: string[];
|
|
39
|
-
aborted?: boolean;
|
|
40
|
-
error?: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Details stored in BranchSummaryEntry.details for file tracking */
|
|
44
|
-
export interface BranchSummaryDetails {
|
|
45
|
-
readFiles: string[];
|
|
46
|
-
modifiedFiles: string[];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export type { FileOperations } from "./utils";
|
|
50
|
-
|
|
51
|
-
export interface BranchPreparation {
|
|
52
|
-
/** Messages extracted for summarization, in chronological order */
|
|
53
|
-
messages: AgentMessage[];
|
|
54
|
-
/** File operations extracted from tool calls */
|
|
55
|
-
fileOps: FileOperations;
|
|
56
|
-
/** Total estimated tokens in messages */
|
|
57
|
-
totalTokens: number;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface CollectEntriesResult {
|
|
61
|
-
/** Entries to summarize, in chronological order */
|
|
62
|
-
entries: SessionEntry[];
|
|
63
|
-
/** Common ancestor between old and new position, if any */
|
|
64
|
-
commonAncestorId: string | null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface GenerateBranchSummaryOptions {
|
|
68
|
-
/** Model to use for summarization */
|
|
69
|
-
model: Model;
|
|
70
|
-
/** API key for the model */
|
|
71
|
-
apiKey: string;
|
|
72
|
-
/** Abort signal for cancellation */
|
|
73
|
-
signal: AbortSignal;
|
|
74
|
-
/** Optional custom instructions for summarization */
|
|
75
|
-
customInstructions?: string;
|
|
76
|
-
/** Tokens reserved for prompt + LLM response (default 16384) */
|
|
77
|
-
reserveTokens?: number;
|
|
78
|
-
/** Optional metadata forwarded to the underlying API request (e.g. user_id for session attribution). */
|
|
79
|
-
metadata?: Record<string, unknown>;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// ============================================================================
|
|
83
|
-
// Entry Collection
|
|
84
|
-
// ============================================================================
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Collect entries that should be summarized when navigating from one position to another.
|
|
88
|
-
*
|
|
89
|
-
* Walks from oldLeafId back to the common ancestor with targetId, collecting entries
|
|
90
|
-
* along the way. Does NOT stop at compaction boundaries - those are included and their
|
|
91
|
-
* summaries become context.
|
|
92
|
-
*
|
|
93
|
-
* @param session - Session manager (read-only access)
|
|
94
|
-
* @param oldLeafId - Current position (where we're navigating from)
|
|
95
|
-
* @param targetId - Target position (where we're navigating to)
|
|
96
|
-
* @returns Entries to summarize and the common ancestor
|
|
97
|
-
*/
|
|
98
|
-
export function collectEntriesForBranchSummary(
|
|
99
|
-
session: ReadonlySessionManager,
|
|
100
|
-
oldLeafId: string | null,
|
|
101
|
-
targetId: string,
|
|
102
|
-
): CollectEntriesResult {
|
|
103
|
-
// If no old position, nothing to summarize
|
|
104
|
-
if (!oldLeafId) {
|
|
105
|
-
return { entries: [], commonAncestorId: null };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Find common ancestor (deepest node that's on both paths)
|
|
109
|
-
const oldPath = new Set(session.getBranch(oldLeafId).map(e => e.id));
|
|
110
|
-
const targetPath = session.getBranch(targetId);
|
|
111
|
-
|
|
112
|
-
// targetPath is root-first, so iterate backwards to find deepest common ancestor
|
|
113
|
-
let commonAncestorId: string | null = null;
|
|
114
|
-
for (let i = targetPath.length - 1; i >= 0; i--) {
|
|
115
|
-
if (oldPath.has(targetPath[i].id)) {
|
|
116
|
-
commonAncestorId = targetPath[i].id;
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Collect entries from old leaf back to common ancestor
|
|
122
|
-
const entries: SessionEntry[] = [];
|
|
123
|
-
let current: string | null = oldLeafId;
|
|
124
|
-
|
|
125
|
-
while (current && current !== commonAncestorId) {
|
|
126
|
-
const entry = session.getEntry(current);
|
|
127
|
-
if (!entry) break;
|
|
128
|
-
entries.push(entry);
|
|
129
|
-
current = entry.parentId;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Reverse to get chronological order
|
|
133
|
-
entries.reverse();
|
|
134
|
-
|
|
135
|
-
return { entries, commonAncestorId };
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// ============================================================================
|
|
139
|
-
// Entry to Message Conversion
|
|
140
|
-
// ============================================================================
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Extract AgentMessage from a session entry.
|
|
144
|
-
* Similar to getMessageFromEntry in compaction.ts but also handles compaction entries.
|
|
145
|
-
*/
|
|
146
|
-
function getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {
|
|
147
|
-
switch (entry.type) {
|
|
148
|
-
case "message":
|
|
149
|
-
// Skip tool results - context is in assistant's tool call
|
|
150
|
-
if (entry.message.role === "toolResult") return undefined;
|
|
151
|
-
return entry.message;
|
|
152
|
-
|
|
153
|
-
case "custom_message":
|
|
154
|
-
return createCustomMessage(
|
|
155
|
-
entry.customType,
|
|
156
|
-
entry.content,
|
|
157
|
-
entry.display,
|
|
158
|
-
entry.details,
|
|
159
|
-
entry.timestamp,
|
|
160
|
-
entry.attribution,
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
case "branch_summary":
|
|
164
|
-
return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);
|
|
165
|
-
|
|
166
|
-
case "compaction":
|
|
167
|
-
return createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp, entry.shortSummary);
|
|
168
|
-
|
|
169
|
-
// These don't contribute to conversation content
|
|
170
|
-
case "thinking_level_change":
|
|
171
|
-
case "model_change":
|
|
172
|
-
case "custom":
|
|
173
|
-
case "label":
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Prepare entries for summarization with token budget.
|
|
180
|
-
*
|
|
181
|
-
* Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.
|
|
182
|
-
* This ensures we keep the most recent context when the branch is too long.
|
|
183
|
-
*
|
|
184
|
-
* Also collects file operations from:
|
|
185
|
-
* - Tool calls in assistant messages
|
|
186
|
-
* - Existing branch_summary entries' details (for cumulative tracking)
|
|
187
|
-
*
|
|
188
|
-
* @param entries - Entries in chronological order
|
|
189
|
-
* @param tokenBudget - Maximum tokens to include (0 = no limit)
|
|
190
|
-
*/
|
|
191
|
-
export function prepareBranchEntries(entries: SessionEntry[], tokenBudget: number = 0): BranchPreparation {
|
|
192
|
-
const messages: AgentMessage[] = [];
|
|
193
|
-
const fileOps = createFileOps();
|
|
194
|
-
let totalTokens = 0;
|
|
195
|
-
|
|
196
|
-
// First pass: collect file ops from ALL entries (even if they don't fit in token budget)
|
|
197
|
-
// This ensures we capture cumulative file tracking from nested branch summaries
|
|
198
|
-
// Only extract from pi-generated summaries (fromExtension !== true), not extension-generated ones
|
|
199
|
-
for (const entry of entries) {
|
|
200
|
-
if (entry.type === "branch_summary" && !entry.fromExtension && entry.details) {
|
|
201
|
-
const details = entry.details as BranchSummaryDetails;
|
|
202
|
-
if (Array.isArray(details.readFiles)) {
|
|
203
|
-
for (const f of details.readFiles) fileOps.read.add(f);
|
|
204
|
-
}
|
|
205
|
-
if (Array.isArray(details.modifiedFiles)) {
|
|
206
|
-
// Modified files go into both edited and written for proper deduplication
|
|
207
|
-
for (const f of details.modifiedFiles) {
|
|
208
|
-
fileOps.edited.add(f);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Second pass: walk from newest to oldest, adding messages until token budget
|
|
215
|
-
for (let i = entries.length - 1; i >= 0; i--) {
|
|
216
|
-
const entry = entries[i];
|
|
217
|
-
const message = getMessageFromEntry(entry);
|
|
218
|
-
if (!message) continue;
|
|
219
|
-
|
|
220
|
-
// Extract file ops from assistant messages (tool calls)
|
|
221
|
-
extractFileOpsFromMessage(message, fileOps);
|
|
222
|
-
|
|
223
|
-
const tokens = estimateTokens(message);
|
|
224
|
-
|
|
225
|
-
// Check budget before adding
|
|
226
|
-
if (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {
|
|
227
|
-
// If this is a summary entry, try to fit it anyway as it's important context
|
|
228
|
-
if (entry.type === "compaction" || entry.type === "branch_summary") {
|
|
229
|
-
if (totalTokens < tokenBudget * 0.9) {
|
|
230
|
-
messages.unshift(message);
|
|
231
|
-
totalTokens += tokens;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
// Stop - we've hit the budget
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
messages.unshift(message);
|
|
239
|
-
totalTokens += tokens;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return { messages, fileOps, totalTokens };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// ============================================================================
|
|
246
|
-
// Summary Generation
|
|
247
|
-
// ============================================================================
|
|
248
|
-
|
|
249
|
-
const BRANCH_SUMMARY_PREAMBLE = prompt.render(branchSummaryPreamble);
|
|
250
|
-
|
|
251
|
-
const BRANCH_SUMMARY_PROMPT = prompt.render(branchSummaryPrompt);
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Generate a summary of abandoned branch entries.
|
|
255
|
-
*
|
|
256
|
-
* @param entries - Session entries to summarize (chronological order)
|
|
257
|
-
* @param options - Generation options
|
|
258
|
-
*/
|
|
259
|
-
export async function generateBranchSummary(
|
|
260
|
-
entries: SessionEntry[],
|
|
261
|
-
options: GenerateBranchSummaryOptions,
|
|
262
|
-
): Promise<BranchSummaryResult> {
|
|
263
|
-
const { model, apiKey, signal, customInstructions, reserveTokens = 16384, metadata } = options;
|
|
264
|
-
|
|
265
|
-
// Token budget = context window minus reserved space for prompt + response
|
|
266
|
-
const contextWindow = model.contextWindow || 128000;
|
|
267
|
-
const tokenBudget = contextWindow - reserveTokens;
|
|
268
|
-
|
|
269
|
-
const { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);
|
|
270
|
-
|
|
271
|
-
if (messages.length === 0) {
|
|
272
|
-
return { summary: "No content to summarize" };
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// Transform to LLM-compatible messages, then serialize to text
|
|
276
|
-
// Serialization prevents the model from treating it as a conversation to continue
|
|
277
|
-
const llmMessages = convertToLlm(messages);
|
|
278
|
-
const conversationText = serializeConversation(llmMessages);
|
|
279
|
-
|
|
280
|
-
// Build prompt
|
|
281
|
-
const instructions = customInstructions || BRANCH_SUMMARY_PROMPT;
|
|
282
|
-
const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${instructions}`;
|
|
283
|
-
|
|
284
|
-
const summarizationMessages = [
|
|
285
|
-
{
|
|
286
|
-
role: "user" as const,
|
|
287
|
-
content: [{ type: "text" as const, text: promptText }],
|
|
288
|
-
timestamp: Date.now(),
|
|
289
|
-
},
|
|
290
|
-
];
|
|
291
|
-
|
|
292
|
-
// Call LLM for summarization
|
|
293
|
-
const response = await completeSimple(
|
|
294
|
-
model,
|
|
295
|
-
{ systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT], messages: summarizationMessages },
|
|
296
|
-
{ apiKey, signal, maxTokens: 2048, metadata },
|
|
297
|
-
);
|
|
298
|
-
|
|
299
|
-
// Check if aborted or errored
|
|
300
|
-
if (response.stopReason === "aborted") {
|
|
301
|
-
return { aborted: true };
|
|
302
|
-
}
|
|
303
|
-
if (response.stopReason === "error") {
|
|
304
|
-
return { error: response.errorMessage || "Summarization failed" };
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
let summary = response.content
|
|
308
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
309
|
-
.map(c => c.text)
|
|
310
|
-
.join("\n");
|
|
311
|
-
|
|
312
|
-
// Prepend preamble to provide context about the branch summary
|
|
313
|
-
summary = BRANCH_SUMMARY_PREAMBLE + summary;
|
|
314
|
-
|
|
315
|
-
// Compute file lists and append to summary
|
|
316
|
-
const { readFiles, modifiedFiles } = computeFileLists(fileOps);
|
|
317
|
-
summary = upsertFileOperations(summary, readFiles, modifiedFiles);
|
|
318
|
-
|
|
319
|
-
return {
|
|
320
|
-
summary: summary || "No summary generated",
|
|
321
|
-
readFiles,
|
|
322
|
-
modifiedFiles,
|
|
323
|
-
};
|
|
324
|
-
}
|