@tarquinen/opencode-dcp 3.2.5-beta0 → 3.2.7-beta0

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 (74) hide show
  1. package/dist/lib/token-utils.js +2 -2
  2. package/dist/lib/token-utils.js.map +1 -1
  3. package/index.ts +141 -0
  4. package/lib/analysis/tokens.ts +225 -0
  5. package/lib/auth.ts +37 -0
  6. package/lib/commands/compression-targets.ts +137 -0
  7. package/lib/commands/context.ts +132 -0
  8. package/lib/commands/decompress.ts +275 -0
  9. package/lib/commands/help.ts +76 -0
  10. package/lib/commands/index.ts +11 -0
  11. package/lib/commands/manual.ts +125 -0
  12. package/lib/commands/recompress.ts +224 -0
  13. package/lib/commands/stats.ts +148 -0
  14. package/lib/commands/sweep.ts +268 -0
  15. package/lib/compress/index.ts +3 -0
  16. package/lib/compress/message-utils.ts +250 -0
  17. package/lib/compress/message.ts +137 -0
  18. package/lib/compress/pipeline.ts +106 -0
  19. package/lib/compress/protected-content.ts +154 -0
  20. package/lib/compress/range-utils.ts +308 -0
  21. package/lib/compress/range.ts +180 -0
  22. package/lib/compress/search.ts +267 -0
  23. package/lib/compress/state.ts +268 -0
  24. package/lib/compress/timing.ts +77 -0
  25. package/lib/compress/types.ts +108 -0
  26. package/lib/compress-permission.ts +25 -0
  27. package/lib/config.ts +1071 -0
  28. package/lib/hooks.ts +378 -0
  29. package/lib/host-permissions.ts +101 -0
  30. package/lib/logger.ts +235 -0
  31. package/lib/message-ids.ts +172 -0
  32. package/lib/messages/index.ts +8 -0
  33. package/lib/messages/inject/inject.ts +215 -0
  34. package/lib/messages/inject/subagent-results.ts +82 -0
  35. package/lib/messages/inject/utils.ts +374 -0
  36. package/lib/messages/priority.ts +102 -0
  37. package/lib/messages/prune.ts +238 -0
  38. package/lib/messages/query.ts +56 -0
  39. package/lib/messages/reasoning-strip.ts +40 -0
  40. package/lib/messages/sync.ts +124 -0
  41. package/lib/messages/utils.ts +187 -0
  42. package/lib/prompts/compress-message.ts +42 -0
  43. package/lib/prompts/compress-range.ts +60 -0
  44. package/lib/prompts/context-limit-nudge.ts +18 -0
  45. package/lib/prompts/extensions/nudge.ts +43 -0
  46. package/lib/prompts/extensions/system.ts +32 -0
  47. package/lib/prompts/extensions/tool.ts +35 -0
  48. package/lib/prompts/index.ts +29 -0
  49. package/lib/prompts/iteration-nudge.ts +6 -0
  50. package/lib/prompts/store.ts +467 -0
  51. package/lib/prompts/system.ts +33 -0
  52. package/lib/prompts/turn-nudge.ts +10 -0
  53. package/lib/protected-patterns.ts +128 -0
  54. package/lib/state/index.ts +4 -0
  55. package/lib/state/persistence.ts +256 -0
  56. package/lib/state/state.ts +190 -0
  57. package/lib/state/tool-cache.ts +98 -0
  58. package/lib/state/types.ts +112 -0
  59. package/lib/state/utils.ts +334 -0
  60. package/lib/strategies/deduplication.ts +127 -0
  61. package/lib/strategies/index.ts +2 -0
  62. package/lib/strategies/purge-errors.ts +88 -0
  63. package/lib/subagents/subagent-results.ts +74 -0
  64. package/lib/token-utils.ts +162 -0
  65. package/lib/ui/notification.ts +346 -0
  66. package/lib/ui/utils.ts +287 -0
  67. package/package.json +12 -3
  68. package/tui/data/context.ts +177 -0
  69. package/tui/index.tsx +34 -0
  70. package/tui/routes/summary.tsx +175 -0
  71. package/tui/shared/names.ts +9 -0
  72. package/tui/shared/theme.ts +58 -0
  73. package/tui/shared/types.ts +38 -0
  74. package/tui/slots/sidebar-content.tsx +502 -0
package/lib/logger.ts ADDED
@@ -0,0 +1,235 @@
1
+ import { writeFile, mkdir } from "fs/promises"
2
+ import { join } from "path"
3
+ import { existsSync } from "fs"
4
+ import { homedir } from "os"
5
+
6
+ export class Logger {
7
+ private logDir: string
8
+ private scope?: string
9
+ public enabled: boolean
10
+
11
+ constructor(enabled: boolean, scope?: string) {
12
+ this.enabled = enabled
13
+ this.scope = scope?.replace(/[^A-Za-z0-9._-]/g, "_")
14
+ const configHome = process.env.XDG_CONFIG_HOME || join(homedir(), ".config")
15
+ this.logDir = this.scope
16
+ ? join(configHome, "opencode", "logs", "dcp", this.scope)
17
+ : join(configHome, "opencode", "logs", "dcp")
18
+ }
19
+
20
+ private async ensureLogDir() {
21
+ if (!existsSync(this.logDir)) {
22
+ await mkdir(this.logDir, { recursive: true })
23
+ }
24
+ }
25
+
26
+ private formatData(data?: any): string {
27
+ if (!data) return ""
28
+
29
+ const parts: string[] = []
30
+ for (const [key, value] of Object.entries(data)) {
31
+ if (value === undefined || value === null) continue
32
+
33
+ // Format arrays compactly
34
+ if (Array.isArray(value)) {
35
+ if (value.length === 0) continue
36
+ parts.push(
37
+ `${key}=[${value.slice(0, 3).join(",")}${value.length > 3 ? `...+${value.length - 3}` : ""}]`,
38
+ )
39
+ } else if (typeof value === "object") {
40
+ const str = JSON.stringify(value)
41
+ if (str.length < 50) {
42
+ parts.push(`${key}=${str}`)
43
+ }
44
+ } else {
45
+ parts.push(`${key}=${value}`)
46
+ }
47
+ }
48
+ return parts.join(" ")
49
+ }
50
+
51
+ private getCallerFile(skipFrames: number = 3): string {
52
+ const originalPrepareStackTrace = Error.prepareStackTrace
53
+ try {
54
+ const err = new Error()
55
+ Error.prepareStackTrace = (_, stack) => stack
56
+ const stack = err.stack as unknown as NodeJS.CallSite[]
57
+ Error.prepareStackTrace = originalPrepareStackTrace
58
+
59
+ // Skip specified number of frames to get to actual caller
60
+ for (let i = skipFrames; i < stack.length; i++) {
61
+ const filename = stack[i]?.getFileName()
62
+ if (filename && !filename.includes("/logger.")) {
63
+ // Extract just the filename without path and extension
64
+ const match = filename.match(/([^/\\]+)\.[tj]s$/)
65
+ return match ? match[1] : filename
66
+ }
67
+ }
68
+ return "unknown"
69
+ } catch {
70
+ return "unknown"
71
+ }
72
+ }
73
+
74
+ private async write(level: string, component: string, message: string, data?: any) {
75
+ if (!this.enabled) return
76
+
77
+ try {
78
+ await this.ensureLogDir()
79
+
80
+ const timestamp = new Date().toISOString()
81
+ const dataStr = this.formatData(data)
82
+
83
+ const logLine = `${timestamp} ${level.padEnd(5)} ${component}: ${message}${dataStr ? " | " + dataStr : ""}\n`
84
+
85
+ const logFile = this.scope
86
+ ? join(this.logDir, `${new Date().toISOString().split("T")[0]}.log`)
87
+ : join(this.logDir, "daily", `${new Date().toISOString().split("T")[0]}.log`)
88
+
89
+ if (!this.scope) {
90
+ const dailyLogDir = join(this.logDir, "daily")
91
+ if (!existsSync(dailyLogDir)) {
92
+ await mkdir(dailyLogDir, { recursive: true })
93
+ }
94
+ }
95
+
96
+ await writeFile(logFile, logLine, { flag: "a" })
97
+ } catch (error) {}
98
+ }
99
+
100
+ info(message: string, data?: any) {
101
+ const component = this.getCallerFile(2)
102
+ return this.write("INFO", component, message, data)
103
+ }
104
+
105
+ debug(message: string, data?: any) {
106
+ const component = this.getCallerFile(2)
107
+ return this.write("DEBUG", component, message, data)
108
+ }
109
+
110
+ warn(message: string, data?: any) {
111
+ const component = this.getCallerFile(2)
112
+ return this.write("WARN", component, message, data)
113
+ }
114
+
115
+ error(message: string, data?: any) {
116
+ const component = this.getCallerFile(2)
117
+ return this.write("ERROR", component, message, data)
118
+ }
119
+
120
+ /**
121
+ * Strips unnecessary metadata from messages for cleaner debug logs.
122
+ *
123
+ * Removed:
124
+ * - All IDs (id, sessionID, messageID, parentID)
125
+ * - summary, path, cost, model, agent, mode, finish, providerID, modelID
126
+ * - step-start and step-finish parts entirely
127
+ * - snapshot fields
128
+ * - ignored text parts
129
+ *
130
+ * Kept:
131
+ * - role, time (created only), tokens (input, output, reasoning, cache)
132
+ * - text, reasoning, tool parts with content
133
+ * - tool calls with: tool, callID, input, output, metadata
134
+ */
135
+ private minimizeForDebug(messages: any[]): any[] {
136
+ return messages.map((msg) => {
137
+ const minimized: any = {
138
+ role: msg.info?.role,
139
+ }
140
+
141
+ if (msg.info?.time?.created) {
142
+ minimized.time = msg.info.time.created
143
+ }
144
+
145
+ if (msg.info?.tokens) {
146
+ minimized.tokens = {
147
+ input: msg.info.tokens.input,
148
+ output: msg.info.tokens.output,
149
+ reasoning: msg.info.tokens.reasoning,
150
+ cache: msg.info.tokens.cache,
151
+ }
152
+ }
153
+
154
+ if (msg.parts) {
155
+ minimized.parts = msg.parts
156
+ .map((part: any) => {
157
+ if (part.type === "step-start" || part.type === "step-finish") {
158
+ return null
159
+ }
160
+
161
+ if (part.type === "text") {
162
+ if (part.ignored) return null
163
+ const textPart: any = { type: "text", text: part.text }
164
+ if (part.metadata) textPart.metadata = part.metadata
165
+ return textPart
166
+ }
167
+
168
+ if (part.type === "reasoning") {
169
+ const reasoningPart: any = { type: "reasoning", text: part.text }
170
+ if (part.metadata) reasoningPart.metadata = part.metadata
171
+ return reasoningPart
172
+ }
173
+
174
+ if (part.type === "tool") {
175
+ const toolPart: any = {
176
+ type: "tool",
177
+ tool: part.tool,
178
+ callID: part.callID,
179
+ }
180
+
181
+ if (part.state?.status) {
182
+ toolPart.status = part.state.status
183
+ }
184
+ if (part.state?.input) {
185
+ toolPart.input = part.state.input
186
+ }
187
+ if (part.state?.output) {
188
+ toolPart.output = part.state.output
189
+ }
190
+ if (part.state?.error) {
191
+ toolPart.error = part.state.error
192
+ }
193
+ if (part.metadata) {
194
+ toolPart.metadata = part.metadata
195
+ }
196
+ if (part.state?.metadata) {
197
+ toolPart.metadata = {
198
+ ...(toolPart.metadata || {}),
199
+ ...part.state.metadata,
200
+ }
201
+ }
202
+ if (part.state?.title) {
203
+ toolPart.title = part.state.title
204
+ }
205
+
206
+ return toolPart
207
+ }
208
+
209
+ return null
210
+ })
211
+ .filter(Boolean)
212
+ }
213
+
214
+ return minimized
215
+ })
216
+ }
217
+
218
+ async saveContext(sessionId: string, messages: any[]) {
219
+ if (!this.enabled) return
220
+
221
+ try {
222
+ const contextDir = join(this.logDir, "context", sessionId)
223
+ if (!existsSync(contextDir)) {
224
+ await mkdir(contextDir, { recursive: true })
225
+ }
226
+
227
+ const minimized = this.minimizeForDebug(messages).filter(
228
+ (msg) => msg.parts && msg.parts.length > 0,
229
+ )
230
+ const timestamp = new Date().toISOString().replace(/[:.]/g, "-")
231
+ const contextFile = join(contextDir, `${timestamp}.json`)
232
+ await writeFile(contextFile, JSON.stringify(minimized, null, 2))
233
+ } catch (error) {}
234
+ }
235
+ }
@@ -0,0 +1,172 @@
1
+ import type { SessionState, WithParts } from "./state"
2
+ import { isIgnoredUserMessage } from "./messages/query"
3
+
4
+ const MESSAGE_REF_REGEX = /^m(\d{4})$/
5
+ const BLOCK_REF_REGEX = /^b([1-9]\d*)$/
6
+ const MESSAGE_ID_TAG_NAME = "dcp-message-id"
7
+
8
+ const MESSAGE_REF_WIDTH = 4
9
+ const MESSAGE_REF_MIN_INDEX = 1
10
+ export const MESSAGE_REF_MAX_INDEX = 9999
11
+
12
+ export type ParsedBoundaryId =
13
+ | {
14
+ kind: "message"
15
+ ref: string
16
+ index: number
17
+ }
18
+ | {
19
+ kind: "compressed-block"
20
+ ref: string
21
+ blockId: number
22
+ }
23
+
24
+ export function formatMessageRef(index: number): string {
25
+ if (
26
+ !Number.isInteger(index) ||
27
+ index < MESSAGE_REF_MIN_INDEX ||
28
+ index > MESSAGE_REF_MAX_INDEX
29
+ ) {
30
+ throw new Error(
31
+ `Message ID index out of bounds: ${index}. Supported range is 0-${MESSAGE_REF_MAX_INDEX}.`,
32
+ )
33
+ }
34
+ return `m${index.toString().padStart(MESSAGE_REF_WIDTH, "0")}`
35
+ }
36
+
37
+ export function formatBlockRef(blockId: number): string {
38
+ if (!Number.isInteger(blockId) || blockId < 1) {
39
+ throw new Error(`Invalid block ID: ${blockId}`)
40
+ }
41
+ return `b${blockId}`
42
+ }
43
+
44
+ export function parseMessageRef(ref: string): number | null {
45
+ const normalized = ref.trim().toLowerCase()
46
+ const match = normalized.match(MESSAGE_REF_REGEX)
47
+ if (!match) {
48
+ return null
49
+ }
50
+ const index = Number.parseInt(match[1], 10)
51
+ if (!Number.isInteger(index)) {
52
+ return null
53
+ }
54
+ if (index < MESSAGE_REF_MIN_INDEX || index > MESSAGE_REF_MAX_INDEX) {
55
+ return null
56
+ }
57
+ return index
58
+ }
59
+
60
+ export function parseBlockRef(ref: string): number | null {
61
+ const normalized = ref.trim().toLowerCase()
62
+ const match = normalized.match(BLOCK_REF_REGEX)
63
+ if (!match) {
64
+ return null
65
+ }
66
+ const id = Number.parseInt(match[1], 10)
67
+ return Number.isInteger(id) ? id : null
68
+ }
69
+
70
+ export function parseBoundaryId(id: string): ParsedBoundaryId | null {
71
+ const normalized = id.trim().toLowerCase()
72
+ const messageIndex = parseMessageRef(normalized)
73
+ if (messageIndex !== null) {
74
+ return {
75
+ kind: "message",
76
+ ref: formatMessageRef(messageIndex),
77
+ index: messageIndex,
78
+ }
79
+ }
80
+
81
+ const blockId = parseBlockRef(normalized)
82
+ if (blockId !== null) {
83
+ return {
84
+ kind: "compressed-block",
85
+ ref: formatBlockRef(blockId),
86
+ blockId,
87
+ }
88
+ }
89
+
90
+ return null
91
+ }
92
+
93
+ function escapeXmlAttribute(value: string): string {
94
+ return value
95
+ .replace(/&/g, "&amp;")
96
+ .replace(/"/g, "&quot;")
97
+ .replace(/</g, "&lt;")
98
+ .replace(/>/g, "&gt;")
99
+ }
100
+
101
+ export function formatMessageIdTag(
102
+ ref: string,
103
+ attributes?: Record<string, string | undefined>,
104
+ ): string {
105
+ const serializedAttributes = Object.entries(attributes || {})
106
+ .sort(([left], [right]) => left.localeCompare(right))
107
+ .map(([name, value]) => {
108
+ if (name.trim().length === 0 || typeof value !== "string" || value.length === 0) {
109
+ return ""
110
+ }
111
+
112
+ return ` ${name}="${escapeXmlAttribute(value)}"`
113
+ })
114
+ .join("")
115
+
116
+ return `\n<${MESSAGE_ID_TAG_NAME}${serializedAttributes}>${ref}</${MESSAGE_ID_TAG_NAME}>`
117
+ }
118
+
119
+ export function assignMessageRefs(state: SessionState, messages: WithParts[]): number {
120
+ let assigned = 0
121
+ let skippedSubAgentPrompt = false
122
+
123
+ for (const message of messages) {
124
+ if (isIgnoredUserMessage(message)) {
125
+ continue
126
+ }
127
+
128
+ if (state.isSubAgent && !skippedSubAgentPrompt && message.info.role === "user") {
129
+ skippedSubAgentPrompt = true
130
+ continue
131
+ }
132
+
133
+ const rawMessageId = message.info.id
134
+ if (typeof rawMessageId !== "string" || rawMessageId.length === 0) {
135
+ continue
136
+ }
137
+
138
+ const existingRef = state.messageIds.byRawId.get(rawMessageId)
139
+ if (existingRef) {
140
+ if (state.messageIds.byRef.get(existingRef) !== rawMessageId) {
141
+ state.messageIds.byRef.set(existingRef, rawMessageId)
142
+ }
143
+ continue
144
+ }
145
+
146
+ const ref = allocateNextMessageRef(state)
147
+ state.messageIds.byRawId.set(rawMessageId, ref)
148
+ state.messageIds.byRef.set(ref, rawMessageId)
149
+ assigned++
150
+ }
151
+
152
+ return assigned
153
+ }
154
+
155
+ function allocateNextMessageRef(state: SessionState): string {
156
+ let candidate = Number.isInteger(state.messageIds.nextRef)
157
+ ? Math.max(MESSAGE_REF_MIN_INDEX, state.messageIds.nextRef)
158
+ : MESSAGE_REF_MIN_INDEX
159
+
160
+ while (candidate <= MESSAGE_REF_MAX_INDEX) {
161
+ const ref = formatMessageRef(candidate)
162
+ if (!state.messageIds.byRef.has(ref)) {
163
+ state.messageIds.nextRef = candidate + 1
164
+ return ref
165
+ }
166
+ candidate++
167
+ }
168
+
169
+ throw new Error(
170
+ `Message ID alias capacity exceeded. Cannot allocate more than ${formatMessageRef(MESSAGE_REF_MAX_INDEX)} aliases in this session.`,
171
+ )
172
+ }
@@ -0,0 +1,8 @@
1
+ export { prune } from "./prune"
2
+ export { syncCompressionBlocks } from "./sync"
3
+ export { injectCompressNudges } from "./inject/inject"
4
+ export { injectMessageIds } from "./inject/inject"
5
+ export { injectExtendedSubAgentResults } from "./inject/subagent-results"
6
+ export { stripStaleMetadata } from "./reasoning-strip"
7
+ export { buildPriorityMap } from "./priority"
8
+ export { buildToolIdList, stripHallucinations, stripHallucinationsFromString } from "./utils"
@@ -0,0 +1,215 @@
1
+ import type { SessionState, WithParts } from "../../state"
2
+ import type { Logger } from "../../logger"
3
+ import type { PluginConfig } from "../../config"
4
+ import type { RuntimePrompts } from "../../prompts/store"
5
+ import { formatMessageIdTag } from "../../message-ids"
6
+ import type { CompressionPriorityMap } from "../priority"
7
+ import { compressPermission } from "../../compress-permission"
8
+ import {
9
+ getLastUserMessage,
10
+ isIgnoredUserMessage,
11
+ isProtectedUserMessage,
12
+ messageHasCompress,
13
+ } from "../query"
14
+ import { saveSessionState } from "../../state/persistence"
15
+ import {
16
+ appendToTextPart,
17
+ appendToLastTextPart,
18
+ appendToAllToolParts,
19
+ createSyntheticTextPart,
20
+ hasContent,
21
+ } from "../utils"
22
+ import {
23
+ addAnchor,
24
+ applyAnchoredNudges,
25
+ countMessagesAfterIndex,
26
+ findLastNonIgnoredMessage,
27
+ getIterationNudgeThreshold,
28
+ getNudgeFrequency,
29
+ getModelInfo,
30
+ isContextOverLimits,
31
+ } from "./utils"
32
+
33
+ export const injectCompressNudges = (
34
+ state: SessionState,
35
+ config: PluginConfig,
36
+ logger: Logger,
37
+ messages: WithParts[],
38
+ prompts: RuntimePrompts,
39
+ compressionPriorities?: CompressionPriorityMap,
40
+ ): void => {
41
+ if (compressPermission(state, config) === "deny") {
42
+ return
43
+ }
44
+
45
+ if (state.manualMode) {
46
+ return
47
+ }
48
+
49
+ const lastMessage = findLastNonIgnoredMessage(messages)
50
+ const lastAssistantMessage = messages.findLast((message) => message.info.role === "assistant")
51
+
52
+ if (lastAssistantMessage && messageHasCompress(lastAssistantMessage)) {
53
+ state.nudges.contextLimitAnchors.clear()
54
+ state.nudges.turnNudgeAnchors.clear()
55
+ state.nudges.iterationNudgeAnchors.clear()
56
+ void saveSessionState(state, logger)
57
+ return
58
+ }
59
+
60
+ const { providerId, modelId } = getModelInfo(messages)
61
+ let anchorsChanged = false
62
+
63
+ const { overMaxLimit, overMinLimit } = isContextOverLimits(
64
+ config,
65
+ state,
66
+ providerId,
67
+ modelId,
68
+ messages,
69
+ )
70
+
71
+ if (!overMinLimit) {
72
+ const hadTurnAnchors = state.nudges.turnNudgeAnchors.size > 0
73
+ const hadIterationAnchors = state.nudges.iterationNudgeAnchors.size > 0
74
+
75
+ if (hadTurnAnchors || hadIterationAnchors) {
76
+ state.nudges.turnNudgeAnchors.clear()
77
+ state.nudges.iterationNudgeAnchors.clear()
78
+ anchorsChanged = true
79
+ }
80
+ }
81
+
82
+ if (overMaxLimit) {
83
+ if (lastMessage) {
84
+ const interval = getNudgeFrequency(config)
85
+ const added = addAnchor(
86
+ state.nudges.contextLimitAnchors,
87
+ lastMessage.message.info.id,
88
+ lastMessage.index,
89
+ messages,
90
+ interval,
91
+ )
92
+ if (added) {
93
+ anchorsChanged = true
94
+ }
95
+ }
96
+ } else if (overMinLimit) {
97
+ const isLastMessageUser = lastMessage?.message.info.role === "user"
98
+
99
+ if (isLastMessageUser && lastAssistantMessage) {
100
+ const previousSize = state.nudges.turnNudgeAnchors.size
101
+ state.nudges.turnNudgeAnchors.add(lastMessage.message.info.id)
102
+ state.nudges.turnNudgeAnchors.add(lastAssistantMessage.info.id)
103
+ if (state.nudges.turnNudgeAnchors.size !== previousSize) {
104
+ anchorsChanged = true
105
+ }
106
+ }
107
+
108
+ const lastUserMessage = getLastUserMessage(messages)
109
+ if (lastUserMessage && lastMessage) {
110
+ const lastUserMessageIndex = messages.findIndex(
111
+ (message) => message.info.id === lastUserMessage.info.id,
112
+ )
113
+ if (lastUserMessageIndex >= 0) {
114
+ const messagesSinceUser = countMessagesAfterIndex(messages, lastUserMessageIndex)
115
+ const iterationThreshold = getIterationNudgeThreshold(config)
116
+
117
+ if (
118
+ lastMessage.index > lastUserMessageIndex &&
119
+ messagesSinceUser >= iterationThreshold
120
+ ) {
121
+ const interval = getNudgeFrequency(config)
122
+ const added = addAnchor(
123
+ state.nudges.iterationNudgeAnchors,
124
+ lastMessage.message.info.id,
125
+ lastMessage.index,
126
+ messages,
127
+ interval,
128
+ )
129
+
130
+ if (added) {
131
+ anchorsChanged = true
132
+ }
133
+ }
134
+ }
135
+ }
136
+ }
137
+
138
+ applyAnchoredNudges(state, config, messages, prompts, compressionPriorities)
139
+
140
+ if (anchorsChanged) {
141
+ void saveSessionState(state, logger)
142
+ }
143
+ }
144
+
145
+ export const injectMessageIds = (
146
+ state: SessionState,
147
+ config: PluginConfig,
148
+ messages: WithParts[],
149
+ compressionPriorities?: CompressionPriorityMap,
150
+ ): void => {
151
+ if (compressPermission(state, config) === "deny") {
152
+ return
153
+ }
154
+
155
+ for (const message of messages) {
156
+ if (isIgnoredUserMessage(message)) {
157
+ continue
158
+ }
159
+
160
+ const messageRef = state.messageIds.byRawId.get(message.info.id)
161
+ if (!messageRef) {
162
+ continue
163
+ }
164
+
165
+ const isBlockedMessage = isProtectedUserMessage(config, message)
166
+ const priority =
167
+ config.compress.mode === "message" && !isBlockedMessage
168
+ ? compressionPriorities?.get(message.info.id)?.priority
169
+ : undefined
170
+ const tag = formatMessageIdTag(
171
+ isBlockedMessage ? "BLOCKED" : messageRef,
172
+ priority ? { priority } : undefined,
173
+ )
174
+
175
+ if (message.info.role === "user") {
176
+ let injected = false
177
+ for (const part of message.parts) {
178
+ if (part.type === "text") {
179
+ injected = appendToTextPart(part, tag) || injected
180
+ }
181
+ }
182
+
183
+ if (injected) {
184
+ continue
185
+ }
186
+
187
+ message.parts.push(createSyntheticTextPart(message, tag))
188
+ continue
189
+ }
190
+
191
+ if (message.info.role !== "assistant") {
192
+ continue
193
+ }
194
+
195
+ if (!hasContent(message)) {
196
+ continue
197
+ }
198
+
199
+ if (appendToAllToolParts(message, tag)) {
200
+ continue
201
+ }
202
+
203
+ if (appendToLastTextPart(message, tag)) {
204
+ continue
205
+ }
206
+
207
+ const syntheticPart = createSyntheticTextPart(message, tag)
208
+ const firstToolIndex = message.parts.findIndex((p) => p.type === "tool")
209
+ if (firstToolIndex === -1) {
210
+ message.parts.push(syntheticPart)
211
+ } else {
212
+ message.parts.splice(firstToolIndex, 0, syntheticPart)
213
+ }
214
+ }
215
+ }