@dev4s/opencode-dcp 0.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.
Files changed (116) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +160 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +87 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/config.d.ts +58 -0
  8. package/dist/lib/config.d.ts.map +1 -0
  9. package/dist/lib/config.js +696 -0
  10. package/dist/lib/config.js.map +1 -0
  11. package/dist/lib/hooks.d.ts +10 -0
  12. package/dist/lib/hooks.d.ts.map +1 -0
  13. package/dist/lib/hooks.js +45 -0
  14. package/dist/lib/hooks.js.map +1 -0
  15. package/dist/lib/logger.d.ts +31 -0
  16. package/dist/lib/logger.d.ts.map +1 -0
  17. package/dist/lib/logger.js +183 -0
  18. package/dist/lib/logger.js.map +1 -0
  19. package/dist/lib/messages/index.d.ts +2 -0
  20. package/dist/lib/messages/index.d.ts.map +1 -0
  21. package/dist/lib/messages/index.js +2 -0
  22. package/dist/lib/messages/index.js.map +1 -0
  23. package/dist/lib/messages/prune.d.ts +6 -0
  24. package/dist/lib/messages/prune.d.ts.map +1 -0
  25. package/dist/lib/messages/prune.js +187 -0
  26. package/dist/lib/messages/prune.js.map +1 -0
  27. package/dist/lib/messages/utils.d.ts +9 -0
  28. package/dist/lib/messages/utils.d.ts.map +1 -0
  29. package/dist/lib/messages/utils.js +124 -0
  30. package/dist/lib/messages/utils.js.map +1 -0
  31. package/dist/lib/model-selector.d.ts +17 -0
  32. package/dist/lib/model-selector.d.ts.map +1 -0
  33. package/dist/lib/model-selector.js +141 -0
  34. package/dist/lib/model-selector.js.map +1 -0
  35. package/dist/lib/prompt.d.ts +3 -0
  36. package/dist/lib/prompt.d.ts.map +1 -0
  37. package/dist/lib/prompt.js +128 -0
  38. package/dist/lib/prompt.js.map +1 -0
  39. package/dist/lib/prompts/discard-tool-spec.txt +41 -0
  40. package/dist/lib/prompts/extract-tool-spec.txt +47 -0
  41. package/dist/lib/prompts/on-idle-analysis.txt +30 -0
  42. package/dist/lib/prompts/user/nudge/nudge-both.txt +10 -0
  43. package/dist/lib/prompts/user/nudge/nudge-discard.txt +9 -0
  44. package/dist/lib/prompts/user/nudge/nudge-extract.txt +9 -0
  45. package/dist/lib/prompts/user/system/system-prompt-both.txt +58 -0
  46. package/dist/lib/prompts/user/system/system-prompt-discard.txt +49 -0
  47. package/dist/lib/prompts/user/system/system-prompt-extract.txt +49 -0
  48. package/dist/lib/shared-utils.d.ts +4 -0
  49. package/dist/lib/shared-utils.d.ts.map +1 -0
  50. package/dist/lib/shared-utils.js +13 -0
  51. package/dist/lib/shared-utils.js.map +1 -0
  52. package/dist/lib/state/index.d.ts +4 -0
  53. package/dist/lib/state/index.d.ts.map +1 -0
  54. package/dist/lib/state/index.js +4 -0
  55. package/dist/lib/state/index.js.map +1 -0
  56. package/dist/lib/state/persistence.d.ts +16 -0
  57. package/dist/lib/state/persistence.d.ts.map +1 -0
  58. package/dist/lib/state/persistence.js +73 -0
  59. package/dist/lib/state/persistence.js.map +1 -0
  60. package/dist/lib/state/state.d.ts +8 -0
  61. package/dist/lib/state/state.d.ts.map +1 -0
  62. package/dist/lib/state/state.js +112 -0
  63. package/dist/lib/state/state.js.map +1 -0
  64. package/dist/lib/state/tool-cache.d.ts +13 -0
  65. package/dist/lib/state/tool-cache.d.ts.map +1 -0
  66. package/dist/lib/state/tool-cache.js +74 -0
  67. package/dist/lib/state/tool-cache.js.map +1 -0
  68. package/dist/lib/state/types.d.ts +32 -0
  69. package/dist/lib/state/types.d.ts.map +1 -0
  70. package/dist/lib/state/types.js +2 -0
  71. package/dist/lib/state/types.js.map +1 -0
  72. package/dist/lib/state/utils.d.ts +2 -0
  73. package/dist/lib/state/utils.d.ts.map +1 -0
  74. package/dist/lib/state/utils.js +10 -0
  75. package/dist/lib/state/utils.js.map +1 -0
  76. package/dist/lib/strategies/deduplication.d.ts +10 -0
  77. package/dist/lib/strategies/deduplication.d.ts.map +1 -0
  78. package/dist/lib/strategies/deduplication.js +89 -0
  79. package/dist/lib/strategies/deduplication.js.map +1 -0
  80. package/dist/lib/strategies/index.d.ts +6 -0
  81. package/dist/lib/strategies/index.d.ts.map +1 -0
  82. package/dist/lib/strategies/index.js +6 -0
  83. package/dist/lib/strategies/index.js.map +1 -0
  84. package/dist/lib/strategies/on-idle.d.ts +14 -0
  85. package/dist/lib/strategies/on-idle.d.ts.map +1 -0
  86. package/dist/lib/strategies/on-idle.js +220 -0
  87. package/dist/lib/strategies/on-idle.js.map +1 -0
  88. package/dist/lib/strategies/purge-errors.d.ts +13 -0
  89. package/dist/lib/strategies/purge-errors.d.ts.map +1 -0
  90. package/dist/lib/strategies/purge-errors.js +54 -0
  91. package/dist/lib/strategies/purge-errors.js.map +1 -0
  92. package/dist/lib/strategies/supersede-writes.d.ts +13 -0
  93. package/dist/lib/strategies/supersede-writes.d.ts.map +1 -0
  94. package/dist/lib/strategies/supersede-writes.js +80 -0
  95. package/dist/lib/strategies/supersede-writes.js.map +1 -0
  96. package/dist/lib/strategies/tools.d.ts +14 -0
  97. package/dist/lib/strategies/tools.d.ts.map +1 -0
  98. package/dist/lib/strategies/tools.js +137 -0
  99. package/dist/lib/strategies/tools.js.map +1 -0
  100. package/dist/lib/strategies/utils.d.ts +12 -0
  101. package/dist/lib/strategies/utils.d.ts.map +1 -0
  102. package/dist/lib/strategies/utils.js +82 -0
  103. package/dist/lib/strategies/utils.js.map +1 -0
  104. package/dist/lib/tokenizer.d.ts +24 -0
  105. package/dist/lib/tokenizer.d.ts.map +1 -0
  106. package/dist/lib/tokenizer.js +45 -0
  107. package/dist/lib/tokenizer.js.map +1 -0
  108. package/dist/lib/ui/notification.d.ts +9 -0
  109. package/dist/lib/ui/notification.d.ts.map +1 -0
  110. package/dist/lib/ui/notification.js +70 -0
  111. package/dist/lib/ui/notification.js.map +1 -0
  112. package/dist/lib/ui/utils.d.ts +15 -0
  113. package/dist/lib/ui/utils.d.ts.map +1 -0
  114. package/dist/lib/ui/utils.js +87 -0
  115. package/dist/lib/ui/utils.js.map +1 -0
  116. package/package.json +57 -0
@@ -0,0 +1,58 @@
1
+ <system-reminder>
2
+ <instruction name=context_management_protocol policy_level=critical>
3
+
4
+ ENVIRONMENT
5
+ You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` and `extract` tools. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to prune.
6
+
7
+ TWO TOOLS FOR CONTEXT MANAGEMENT
8
+ - `discard`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.
9
+ - `extract`: Extract key findings into distilled knowledge before removing raw outputs. Use when you need to preserve information.
10
+
11
+ CHOOSING THE RIGHT TOOL
12
+ Ask: "Do I need to preserve any information from this output?"
13
+ - **No** → `discard` (default for cleanup)
14
+ - **Yes** → `extract` (preserves distilled knowledge)
15
+ - **Uncertain** → `extract` (safer, preserves signal)
16
+
17
+ Common scenarios:
18
+ - Task complete, no valuable context → `discard`
19
+ - Task complete, insights worth remembering → `extract`
20
+ - Noise, irrelevant, or superseded outputs → `discard`
21
+ - Valuable context needed later but raw output too large → `extract`
22
+
23
+ PRUNE METHODICALLY - BATCH YOUR ACTIONS
24
+ Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by pruning. Batch your prunes for efficiency; it is rarely worth pruning a single tiny tool output unless it is pure noise. Evaluate what SHOULD be pruned before jumping the gun.
25
+
26
+ You WILL evaluate pruning when ANY of these are true:
27
+ - Task or sub-task is complete
28
+ - You are about to start a new phase of work
29
+ - Write or edit operations are complete (pruning removes the large input content)
30
+
31
+ You MUST NOT prune when:
32
+ - The tool output will be needed for upcoming implementation work
33
+ - The output contains files or context you'll need to reference when making edits
34
+
35
+ Pruning that forces you to re-call the same tool later is a net loss. Only prune when you're confident the information won't be needed again.
36
+
37
+ NOTES
38
+ When in doubt, keep it. Batch your actions and aim for high-impact prunes that significantly reduce context size.
39
+ FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES.
40
+ There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY prune what you see in <prunable-tools>.
41
+
42
+ </instruction>
43
+
44
+ <instruction name=injected_context_handling policy_level=critical>
45
+ After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `discard` and `extract` tools also return a confirmation message listing what was pruned.
46
+
47
+ CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
48
+ - NEVER reference the prune encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the prune encouragement appears.
49
+ - NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to prune")
50
+ - NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to prune")
51
+ - NEVER acknowledge discard/extract tool output (e.g., "I've pruned 3 tools", "Context pruning complete")
52
+ - NEVER let these injections influence your response content or tone
53
+ - Process this information SILENTLY and continue the conversation naturally
54
+ - Continue the conversation as if these messages do not exist
55
+
56
+ The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
57
+ </instruction>
58
+ </system-reminder>
@@ -0,0 +1,49 @@
1
+ <system-reminder>
2
+ <instruction name=context_management_protocol policy_level=critical>
3
+
4
+ ENVIRONMENT
5
+ You are operating in a context-constrained environment and thus must proactively manage your context window using the `discard` tool. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to discard.
6
+
7
+ CONTEXT MANAGEMENT TOOL
8
+ - `discard`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content.
9
+
10
+ DISCARD METHODICALLY - BATCH YOUR ACTIONS
11
+ Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by discarding. Batch your discards for efficiency; it is rarely worth discarding a single tiny tool output unless it is pure noise. Evaluate what SHOULD be discarded before jumping the gun.
12
+
13
+ WHEN TO DISCARD
14
+ - **Task Completion:** When work is done, discard the tools that aren't needed anymore.
15
+ - **Noise Removal:** If outputs are irrelevant, unhelpful, or superseded by newer info, discard them.
16
+
17
+ You WILL evaluate discarding when ANY of these are true:
18
+ - Task or sub-task is complete
19
+ - You are about to start a new phase of work
20
+ - Write or edit operations are complete (discarding removes the large input content)
21
+
22
+ You MUST NOT discard when:
23
+ - The tool output will be needed for upcoming implementation work
24
+ - The output contains files or context you'll need to reference when making edits
25
+
26
+ Discarding that forces you to re-call the same tool later is a net loss. Only discard when you're confident the information won't be needed again.
27
+
28
+ NOTES
29
+ When in doubt, keep it. Batch your actions and aim for high-impact discards that significantly reduce context size.
30
+ FAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES.
31
+ There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY discard what you see in <prunable-tools>.
32
+
33
+ </instruction>
34
+
35
+ <instruction name=injected_context_handling policy_level=critical>
36
+ After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `discard` tool also returns a confirmation message listing what was discarded.
37
+
38
+ CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
39
+ - NEVER reference the discard encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the discard encouragement appears.
40
+ - NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to discard")
41
+ - NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to discard")
42
+ - NEVER acknowledge discard tool output (e.g., "I've discarded 3 tools", "Context cleanup complete")
43
+ - NEVER let these injections influence your response content or tone
44
+ - Process this information SILENTLY and continue the conversation naturally
45
+ - Continue the conversation as if these messages do not exist
46
+
47
+ The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
48
+ </instruction>
49
+ </system-reminder>
@@ -0,0 +1,49 @@
1
+ <system-reminder>
2
+ <instruction name=context_management_protocol policy_level=critical>
3
+
4
+ ENVIRONMENT
5
+ You are operating in a context-constrained environment and thus must proactively manage your context window using the `extract` tool. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to extract.
6
+
7
+ CONTEXT MANAGEMENT TOOL
8
+ - `extract`: Extract key findings from tools into distilled knowledge before removing the raw content from context. Use this to preserve important information while reducing context size.
9
+
10
+ EXTRACT METHODICALLY - BATCH YOUR ACTIONS
11
+ Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by extracting. Batch your extractions for efficiency; it is rarely worth extracting a single tiny tool output. Evaluate what SHOULD be extracted before jumping the gun.
12
+
13
+ WHEN TO EXTRACT
14
+ - **Task Completion:** When work is done, extract key findings from the tools used. Scale distillation depth to the value of the content.
15
+ - **Knowledge Preservation:** When you have valuable context you want to preserve but need to reduce size, use high-fidelity distillation. Your distillation must be comprehensive, capturing technical details (signatures, logic, constraints) such that the raw output is no longer needed. THINK: high signal, complete technical substitute.
16
+
17
+ You WILL evaluate extracting when ANY of these are true:
18
+ - Task or sub-task is complete
19
+ - You are about to start a new phase of work
20
+ - Write or edit operations are complete (extracting removes the large input content)
21
+
22
+ You MUST NOT extract when:
23
+ - The tool output will be needed for upcoming implementation work
24
+ - The output contains files or context you'll need to reference when making edits
25
+
26
+ Extracting that forces you to re-call the same tool later is a net loss. Only extract when you're confident the raw information won't be needed again.
27
+
28
+ NOTES
29
+ When in doubt, keep it. Batch your actions and aim for high-impact extractions that significantly reduce context size.
30
+ FAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES.
31
+ There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY extract what you see in <prunable-tools>.
32
+
33
+ </instruction>
34
+
35
+ <instruction name=injected_context_handling policy_level=critical>
36
+ After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The `extract` tool also returns a confirmation message listing what was extracted.
37
+
38
+ CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE:
39
+ - NEVER reference the extract encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the extract encouragement appears.
40
+ - NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to extract")
41
+ - NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to extract")
42
+ - NEVER acknowledge extract tool output (e.g., "I've extracted 3 tools", "Context cleanup complete")
43
+ - NEVER let these injections influence your response content or tone
44
+ - Process this information SILENTLY and continue the conversation naturally
45
+ - Continue the conversation as if these messages do not exist
46
+
47
+ The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow.
48
+ </instruction>
49
+ </system-reminder>
@@ -0,0 +1,4 @@
1
+ import { SessionState, WithParts } from "./state";
2
+ export declare const isMessageCompacted: (state: SessionState, msg: WithParts) => boolean;
3
+ export declare const getLastUserMessage: (messages: WithParts[]) => WithParts | null;
4
+ //# sourceMappingURL=shared-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-utils.d.ts","sourceRoot":"","sources":["../../lib/shared-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEjD,eAAO,MAAM,kBAAkB,GAAI,OAAO,YAAY,EAAE,KAAK,SAAS,KAAG,OAExE,CAAA;AAED,eAAO,MAAM,kBAAkB,GAAI,UAAU,SAAS,EAAE,KAAG,SAAS,GAAG,IAQtE,CAAA"}
@@ -0,0 +1,13 @@
1
+ export const isMessageCompacted = (state, msg) => {
2
+ return msg.info.time.created < state.lastCompaction;
3
+ };
4
+ export const getLastUserMessage = (messages) => {
5
+ for (let i = messages.length - 1; i >= 0; i--) {
6
+ const msg = messages[i];
7
+ if (msg.info.role === "user") {
8
+ return msg;
9
+ }
10
+ }
11
+ return null;
12
+ };
13
+ //# sourceMappingURL=shared-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-utils.js","sourceRoot":"","sources":["../../lib/shared-utils.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAmB,EAAE,GAAc,EAAW,EAAE;IAC/E,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,cAAc,CAAA;AACvD,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,QAAqB,EAAoB,EAAE;IAC1E,KAAK,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5C,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;QACvB,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC3B,OAAO,GAAG,CAAA;QACd,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAA;AACf,CAAC,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from "./persistence";
2
+ export * from "./types";
3
+ export * from "./state";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from "./persistence";
2
+ export * from "./types";
3
+ export * from "./state";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../lib/state/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * State persistence module for DCP plugin.
3
+ * Persists pruned tool IDs across sessions so they survive OpenCode restarts.
4
+ * Storage location: ~/.local/share/opencode/storage/plugin/dcp/{sessionId}.json
5
+ */
6
+ import type { SessionState, SessionStats, Prune } from "./types";
7
+ import type { Logger } from "../logger";
8
+ export interface PersistedSessionState {
9
+ sessionName?: string;
10
+ prune: Prune;
11
+ stats: SessionStats;
12
+ lastUpdated: string;
13
+ }
14
+ export declare function saveSessionState(sessionState: SessionState, logger: Logger, sessionName?: string): Promise<void>;
15
+ export declare function loadSessionState(sessionId: string, logger: Logger): Promise<PersistedSessionState | null>;
16
+ //# sourceMappingURL=persistence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistence.d.ts","sourceRoot":"","sources":["../../../lib/state/persistence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAEvC,MAAM,WAAW,qBAAqB;IAClC,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,KAAK,CAAA;IACZ,KAAK,EAAE,YAAY,CAAA;IACnB,WAAW,EAAE,MAAM,CAAA;CACtB;AAcD,wBAAsB,gBAAgB,CAClC,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,EACd,WAAW,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,IAAI,CAAC,CA6Bf;AAED,wBAAsB,gBAAgB,CAClC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC,CA8BvC"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * State persistence module for DCP plugin.
3
+ * Persists pruned tool IDs across sessions so they survive OpenCode restarts.
4
+ * Storage location: ~/.local/share/opencode/storage/plugin/dcp/{sessionId}.json
5
+ */
6
+ import * as fs from "fs/promises";
7
+ import { existsSync } from "fs";
8
+ import { homedir } from "os";
9
+ import { join } from "path";
10
+ const STORAGE_DIR = join(homedir(), ".local", "share", "opencode", "storage", "plugin", "dcp");
11
+ async function ensureStorageDir() {
12
+ if (!existsSync(STORAGE_DIR)) {
13
+ await fs.mkdir(STORAGE_DIR, { recursive: true });
14
+ }
15
+ }
16
+ function getSessionFilePath(sessionId) {
17
+ return join(STORAGE_DIR, `${sessionId}.json`);
18
+ }
19
+ export async function saveSessionState(sessionState, logger, sessionName) {
20
+ try {
21
+ if (!sessionState.sessionId) {
22
+ return;
23
+ }
24
+ await ensureStorageDir();
25
+ const state = {
26
+ sessionName: sessionName,
27
+ prune: sessionState.prune,
28
+ stats: sessionState.stats,
29
+ lastUpdated: new Date().toISOString(),
30
+ };
31
+ const filePath = getSessionFilePath(sessionState.sessionId);
32
+ const content = JSON.stringify(state, null, 2);
33
+ await fs.writeFile(filePath, content, "utf-8");
34
+ logger.info("Saved session state to disk", {
35
+ sessionId: sessionState.sessionId,
36
+ totalTokensSaved: state.stats.totalPruneTokens,
37
+ });
38
+ }
39
+ catch (error) {
40
+ logger.error("Failed to save session state", {
41
+ sessionId: sessionState.sessionId,
42
+ error: error?.message,
43
+ });
44
+ }
45
+ }
46
+ export async function loadSessionState(sessionId, logger) {
47
+ try {
48
+ const filePath = getSessionFilePath(sessionId);
49
+ if (!existsSync(filePath)) {
50
+ return null;
51
+ }
52
+ const content = await fs.readFile(filePath, "utf-8");
53
+ const state = JSON.parse(content);
54
+ if (!state || !state.prune || !Array.isArray(state.prune.toolIds) || !state.stats) {
55
+ logger.warn("Invalid session state file, ignoring", {
56
+ sessionId: sessionId,
57
+ });
58
+ return null;
59
+ }
60
+ logger.info("Loaded session state from disk", {
61
+ sessionId: sessionId,
62
+ });
63
+ return state;
64
+ }
65
+ catch (error) {
66
+ logger.warn("Failed to load session state", {
67
+ sessionId: sessionId,
68
+ error: error?.message,
69
+ });
70
+ return null;
71
+ }
72
+ }
73
+ //# sourceMappingURL=persistence.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistence.js","sourceRoot":"","sources":["../../../lib/state/persistence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,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C,SAAS,EAAE,SAAS;YACpB,KAAK,EAAE,KAAK,EAAE,OAAO;SACxB,CAAC,CAAA;QACF,OAAO,IAAI,CAAA;IACf,CAAC;AACL,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { SessionState, WithParts } from "./types";
2
+ import type { Logger } from "../logger";
3
+ export declare const checkSession: (client: any, state: SessionState, logger: Logger, messages: WithParts[]) => Promise<void>;
4
+ export declare function createSessionState(): SessionState;
5
+ export declare function resetSessionState(state: SessionState): void;
6
+ export declare function ensureSessionInitialized(client: any, state: SessionState, sessionId: string, logger: Logger, messages: WithParts[]): Promise<void>;
7
+ export declare function countTurns(state: SessionState, messages: WithParts[]): number;
8
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../lib/state/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAsB,SAAS,EAAE,MAAM,SAAS,CAAA;AAC1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAKvC,eAAO,MAAM,YAAY,GACrB,QAAQ,GAAG,EACX,OAAO,YAAY,EACnB,QAAQ,MAAM,EACd,UAAU,SAAS,EAAE,KACtB,OAAO,CAAC,IAAI,CA4Bd,CAAA;AAED,wBAAgB,kBAAkB,IAAI,YAAY,CAiBjD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAe3D;AAED,wBAAsB,wBAAwB,CAC1C,MAAM,EAAE,GAAG,EACX,KAAK,EAAE,YAAY,EACnB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,SAAS,EAAE,GACtB,OAAO,CAAC,IAAI,CAAC,CA8Bf;AAYD,wBAAgB,UAAU,CAAC,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,MAAM,CAa7E"}
@@ -0,0 +1,112 @@
1
+ import { loadSessionState } from "./persistence";
2
+ import { isSubAgentSession } from "./utils";
3
+ import { getLastUserMessage, isMessageCompacted } from "../shared-utils";
4
+ export const checkSession = async (client, state, logger, messages) => {
5
+ const lastUserMessage = getLastUserMessage(messages);
6
+ if (!lastUserMessage) {
7
+ return;
8
+ }
9
+ const lastSessionId = lastUserMessage.info.sessionID;
10
+ if (state.sessionId === null || state.sessionId !== lastSessionId) {
11
+ logger.info(`Session changed: ${state.sessionId} -> ${lastSessionId}`);
12
+ try {
13
+ await ensureSessionInitialized(client, state, lastSessionId, logger, messages);
14
+ }
15
+ catch (err) {
16
+ logger.error("Failed to initialize session state", { error: err.message });
17
+ }
18
+ }
19
+ const lastCompactionTimestamp = findLastCompactionTimestamp(messages);
20
+ if (lastCompactionTimestamp > state.lastCompaction) {
21
+ state.lastCompaction = lastCompactionTimestamp;
22
+ state.toolParameters.clear();
23
+ state.prune.toolIds = [];
24
+ logger.info("Detected compaction from messages - cleared tool cache", {
25
+ timestamp: lastCompactionTimestamp,
26
+ });
27
+ }
28
+ state.currentTurn = countTurns(state, messages);
29
+ };
30
+ export function createSessionState() {
31
+ return {
32
+ sessionId: null,
33
+ isSubAgent: false,
34
+ prune: {
35
+ toolIds: [],
36
+ },
37
+ stats: {
38
+ pruneTokenCounter: 0,
39
+ totalPruneTokens: 0,
40
+ },
41
+ toolParameters: new Map(),
42
+ nudgeCounter: 0,
43
+ lastToolPrune: false,
44
+ lastCompaction: 0,
45
+ currentTurn: 0,
46
+ };
47
+ }
48
+ export function resetSessionState(state) {
49
+ state.sessionId = null;
50
+ state.isSubAgent = false;
51
+ state.prune = {
52
+ toolIds: [],
53
+ };
54
+ state.stats = {
55
+ pruneTokenCounter: 0,
56
+ totalPruneTokens: 0,
57
+ };
58
+ state.toolParameters.clear();
59
+ state.nudgeCounter = 0;
60
+ state.lastToolPrune = false;
61
+ state.lastCompaction = 0;
62
+ state.currentTurn = 0;
63
+ }
64
+ export async function ensureSessionInitialized(client, state, sessionId, logger, messages) {
65
+ if (state.sessionId === sessionId) {
66
+ return;
67
+ }
68
+ logger.info("session ID = " + sessionId);
69
+ logger.info("Initializing session state", { sessionId: sessionId });
70
+ resetSessionState(state);
71
+ state.sessionId = sessionId;
72
+ const isSubAgent = await isSubAgentSession(client, sessionId);
73
+ state.isSubAgent = isSubAgent;
74
+ logger.info("isSubAgent = " + isSubAgent);
75
+ state.lastCompaction = findLastCompactionTimestamp(messages);
76
+ state.currentTurn = countTurns(state, messages);
77
+ const persisted = await loadSessionState(sessionId, logger);
78
+ if (persisted === null) {
79
+ return;
80
+ }
81
+ state.prune = {
82
+ toolIds: persisted.prune.toolIds || [],
83
+ };
84
+ state.stats = {
85
+ pruneTokenCounter: persisted.stats?.pruneTokenCounter || 0,
86
+ totalPruneTokens: persisted.stats?.totalPruneTokens || 0,
87
+ };
88
+ }
89
+ function findLastCompactionTimestamp(messages) {
90
+ for (let i = messages.length - 1; i >= 0; i--) {
91
+ const msg = messages[i];
92
+ if (msg.info.role === "assistant" && msg.info.summary === true) {
93
+ return msg.info.time.created;
94
+ }
95
+ }
96
+ return 0;
97
+ }
98
+ export function countTurns(state, messages) {
99
+ let turnCount = 0;
100
+ for (const msg of messages) {
101
+ if (isMessageCompacted(state, msg)) {
102
+ continue;
103
+ }
104
+ for (const part of msg.parts) {
105
+ if (part.type === "step-start") {
106
+ turnCount++;
107
+ }
108
+ }
109
+ }
110
+ return turnCount;
111
+ }
112
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../lib/state/state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAExE,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAC7B,MAAW,EACX,KAAmB,EACnB,MAAc,E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ACzB,IAAI,kBAAkB,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;YACjC,SAAQ;QACZ,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;YAC3B,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAC7B,SAAS,EAAE,CAAA;YACf,CAAC;QACL,CAAC;IACL,CAAC;IACD,OAAO,SAAS,CAAA;AACpB,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { SessionState, WithParts } from "./index";
2
+ import type { Logger } from "../logger";
3
+ import { PluginConfig } from "../config";
4
+ /**
5
+ * Sync tool parameters from OpenCode's session.messages() API.
6
+ */
7
+ export declare function syncToolCache(state: SessionState, config: PluginConfig, logger: Logger, messages: WithParts[]): Promise<void>;
8
+ /**
9
+ * Trim the tool parameters cache to prevent unbounded memory growth.
10
+ * Uses FIFO eviction - removes oldest entries first.
11
+ */
12
+ export declare function trimToolParametersCache(state: SessionState): void;
13
+ //# sourceMappingURL=tool-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-cache.d.ts","sourceRoot":"","sources":["../../../lib/state/tool-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAc,SAAS,EAAE,MAAM,SAAS,CAAA;AAClE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAKxC;;GAEG;AACH,wBAAsB,aAAa,CAC/B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,YAAY,EACpB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,SAAS,EAAE,GACtB,OAAO,CAAC,IAAI,CAAC,CAmEf;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAajE"}
@@ -0,0 +1,74 @@
1
+ import { isMessageCompacted } from "../shared-utils";
2
+ const MAX_TOOL_CACHE_SIZE = 1000;
3
+ /**
4
+ * Sync tool parameters from OpenCode's session.messages() API.
5
+ */
6
+ export async function syncToolCache(state, config, logger, messages) {
7
+ try {
8
+ logger.info("Syncing tool parameters from OpenCode messages");
9
+ state.nudgeCounter = 0;
10
+ let turnCounter = 0;
11
+ for (const msg of messages) {
12
+ if (isMessageCompacted(state, msg)) {
13
+ continue;
14
+ }
15
+ for (const part of msg.parts) {
16
+ if (part.type === "step-start") {
17
+ turnCounter++;
18
+ continue;
19
+ }
20
+ if (part.type !== "tool" || !part.callID) {
21
+ continue;
22
+ }
23
+ const turnProtectionEnabled = config.turnProtection.enabled;
24
+ const turnProtectionTurns = config.turnProtection.turns;
25
+ const isProtectedByTurn = turnProtectionEnabled &&
26
+ turnProtectionTurns > 0 &&
27
+ state.currentTurn - turnCounter < turnProtectionTurns;
28
+ state.lastToolPrune = part.tool === "discard" || part.tool === "extract";
29
+ const allProtectedTools = config.tools.settings.protectedTools;
30
+ if (part.tool === "discard" || part.tool === "extract") {
31
+ state.nudgeCounter = 0;
32
+ }
33
+ else if (!allProtectedTools.includes(part.tool) && !isProtectedByTurn) {
34
+ state.nudgeCounter++;
35
+ }
36
+ if (state.toolParameters.has(part.callID)) {
37
+ continue;
38
+ }
39
+ if (isProtectedByTurn) {
40
+ continue;
41
+ }
42
+ state.toolParameters.set(part.callID, {
43
+ tool: part.tool,
44
+ parameters: part.state?.input ?? {},
45
+ status: part.state.status,
46
+ error: part.state.status === "error" ? part.state.error : undefined,
47
+ turn: turnCounter,
48
+ });
49
+ logger.info(`Cached tool id: ${part.callID} (created on turn ${turnCounter})`);
50
+ }
51
+ }
52
+ logger.info(`Synced cache - size: ${state.toolParameters.size}, currentTurn: ${state.currentTurn}, nudgeCounter: ${state.nudgeCounter}`);
53
+ trimToolParametersCache(state);
54
+ }
55
+ catch (error) {
56
+ logger.warn("Failed to sync tool parameters from OpenCode", {
57
+ error: error instanceof Error ? error.message : String(error),
58
+ });
59
+ }
60
+ }
61
+ /**
62
+ * Trim the tool parameters cache to prevent unbounded memory growth.
63
+ * Uses FIFO eviction - removes oldest entries first.
64
+ */
65
+ export function trimToolParametersCache(state) {
66
+ if (state.toolParameters.size <= MAX_TOOL_CACHE_SIZE) {
67
+ return;
68
+ }
69
+ const keysToRemove = Array.from(state.toolParameters.keys()).slice(0, state.toolParameters.size - MAX_TOOL_CACHE_SIZE);
70
+ for (const key of keysToRemove) {
71
+ state.toolParameters.delete(key);
72
+ }
73
+ }
74
+ //# sourceMappingURL=tool-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-cache.js","sourceRoot":"","sources":["../../../lib/state/tool-cache.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAEpD,MAAM,mBAAmB,GAAG,IAAI,CAAA;AAEhC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAC/B,KAAmB,EACnB,MAAoB,EACpB,MAAc,EACd,QAAqB;IAErB,IAAI,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,gDAAgD,CAAC,CAAA;QAE7D,KAAK,CAAC,YAAY,GAAG,CAAC,CAAA;QACtB,IAAI,WAAW,GAAG,CAAC,CAAA;QAEnB,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;YACzB,IAAI,kBAAkB,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;gBACjC,SAAQ;YACZ,CAAC;YAED,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;gBAC3B,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;oBAC7B,WAAW,EAAE,CAAA;oBACb,SAAQ;gBACZ,CAAC;gBAED,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;oBACvC,SAAQ;gBACZ,CAAC;gBAED,MAAM,qBAAqB,GAAG,MAAM,CAAC,c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E,WAAW;iBACpB,CAAC,CAAA;gBACF,MAAM,CAAC,IAAI,CAAC,mBAAmB,IAAI,CAAC,MAAM,qBAAqB,WAAW,GAAG,CAAC,CAAA;YAClF,CAAC;QACL,CAAC;QAED,MAAM,CAAC,IAAI,CACP,wBAAwB,KAAK,CAAC,cAAc,CAAC,IAAI,kBAAkB,KAAK,CAAC,WAAW,mBAAmB,KAAK,CAAC,YAAY,EAAE,CAC9H,CAAA;QACD,uBAAuB,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,CAAC,IAAI,CAAC,8CAA8C,EAAE;YACxD,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAChE,CAAC,CAAA;IACN,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAmB;IACvD,IAAI,KAAK,CAAC,cAAc,CAAC,IAAI,IAAI,mBAAmB,EAAE,CAAC;QACnD,OAAM;IACV,CAAC;IAED,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAC9D,CAAC,EACD,KAAK,CAAC,cAAc,CAAC,IAAI,GAAG,mBAAmB,CAClD,CAAA;IAED,KAAK,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;QAC7B,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACpC,CAAC;AACL,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { Message, Part } from "@opencode-ai/sdk";
2
+ export interface WithParts {
3
+ info: Message;
4
+ parts: Part[];
5
+ }
6
+ export type ToolStatus = "pending" | "running" | "completed" | "error";
7
+ export interface ToolParameterEntry {
8
+ tool: string;
9
+ parameters: any;
10
+ status?: ToolStatus;
11
+ error?: string;
12
+ turn: number;
13
+ }
14
+ export interface SessionStats {
15
+ pruneTokenCounter: number;
16
+ totalPruneTokens: number;
17
+ }
18
+ export interface Prune {
19
+ toolIds: string[];
20
+ }
21
+ export interface SessionState {
22
+ sessionId: string | null;
23
+ isSubAgent: boolean;
24
+ prune: Prune;
25
+ stats: SessionStats;
26
+ toolParameters: Map<string, ToolParameterEntry>;
27
+ nudgeCounter: number;
28
+ lastToolPrune: boolean;
29
+ lastCompaction: number;
30
+ currentTurn: number;
31
+ }
32
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../lib/state/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAEhD,MAAM,WAAW,SAAS;IACtB,IAAI,EAAE,OAAO,CAAA;IACb,KAAK,EAAE,IAAI,EAAE,CAAA;CAChB;AAED,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,SAAS,GAAG,WAAW,GAAG,OAAO,CAAA;AAEtE,MAAM,WAAW,kBAAkB;IAC/B,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,GAAG,CAAA;IACf,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,YAAY;IACzB,iBAAiB,EAAE,MAAM,CAAA;IACzB,gBAAgB,EAAE,MAAM,CAAA;CAC3B;AAED,MAAM,WAAW,KAAK;IAClB,OAAO,EAAE,MAAM,EAAE,CAAA;CACpB;AAED,MAAM,WAAW,YAAY;IACzB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,UAAU,EAAE,OAAO,CAAA;IACnB,KAAK,EAAE,KAAK,CAAA;IACZ,KAAK,EAAE,YAAY,CAAA;IACnB,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;IAC/C,YAAY,EAAE,MAAM,CAAA;IACpB,aAAa,EAAE,OAAO,CAAA;IACtB,cAAc,EAAE,MAAM,CAAA;IACtB,WAAW,EAAE,MAAM,CAAA;CACtB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../lib/state/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export declare function isSubAgentSession(client: any, sessionID: string): Promise<boolean>;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../lib/state/utils.ts"],"names":[],"mappings":"AAAA,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAOxF"}
@@ -0,0 +1,10 @@
1
+ export async function isSubAgentSession(client, sessionID) {
2
+ try {
3
+ const result = await client.session.get({ path: { id: sessionID } });
4
+ return !!result.data?.parentID;
5
+ }
6
+ catch (error) {
7
+ return false;
8
+ }
9
+ }
10
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../lib/state/utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,MAAW,EAAE,SAAiB;IAClE,IAAI,CAAC;QACD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,CAAC,CAAA;QACpE,OAAO,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAA;IAClC,CAAC;IAAC,OAAO,KAAU,EAAE,CAAC;QAClB,OAAO,KAAK,CAAA;IAChB,CAAC;AACL,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { PluginConfig } from "../config";
2
+ import { Logger } from "../logger";
3
+ import type { SessionState, WithParts } from "../state";
4
+ /**
5
+ * Deduplication strategy - prunes older tool calls that have identical
6
+ * tool name and parameters, keeping only the most recent occurrence.
7
+ * Modifies the session state in place to add pruned tool call IDs.
8
+ */
9
+ export declare const deduplicate: (state: SessionState, logger: Logger, config: PluginConfig, messages: WithParts[]) => void;
10
+ //# sourceMappingURL=deduplication.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deduplication.d.ts","sourceRoot":"","sources":["../../../lib/strategies/deduplication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAIvD;;;;GAIG;AACH,eAAO,MAAM,WAAW,GACpB,OAAO,YAAY,EACnB,QAAQ,MAAM,EACd,QAAQ,YAAY,EACpB,UAAU,SAAS,EAAE,KACtB,IA4DF,CAAA"}