compressed-context 0.0.0-stage → 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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -2
  3. package/package.json +64 -6
  4. package/src/compression/active-summary-cache.ts +50 -0
  5. package/src/compression/adopt.ts +57 -0
  6. package/src/compression/auto.ts +372 -0
  7. package/src/compression/babel.ts +85 -0
  8. package/src/compression/block.ts +25 -0
  9. package/src/compression/cache-history.ts +75 -0
  10. package/src/compression/compaction.ts +189 -0
  11. package/src/compression/decompress-tool.ts +74 -0
  12. package/src/compression/decompress.ts +292 -0
  13. package/src/compression/deleted-sessions.ts +24 -0
  14. package/src/compression/distill.ts +220 -0
  15. package/src/compression/events.ts +275 -0
  16. package/src/compression/markdown.ts +53 -0
  17. package/src/compression/normalize.ts +114 -0
  18. package/src/compression/render.ts +70 -0
  19. package/src/compression/schema.ts +84 -0
  20. package/src/compression/select.ts +123 -0
  21. package/src/compression/session-message.ts +112 -0
  22. package/src/compression/settings-rpc.ts +111 -0
  23. package/src/compression/settings.ts +175 -0
  24. package/src/compression/state.ts +248 -0
  25. package/src/compression/summarize.ts +595 -0
  26. package/src/compression/summary.ts +73 -0
  27. package/src/compression/tool.ts +142 -0
  28. package/src/compression/transcript-store.ts +258 -0
  29. package/src/compression/transcript.ts +63 -0
  30. package/src/compression/validate.ts +81 -0
  31. package/src/i18n/en.ts +93 -0
  32. package/src/i18n/index.ts +28 -0
  33. package/src/i18n/types.ts +89 -0
  34. package/src/i18n/zh.ts +93 -0
  35. package/src/index.ts +108 -0
  36. package/src/shared/blocks.ts +17 -0
  37. package/src/shared/cache.ts +199 -0
  38. package/src/shared/format.ts +23 -0
  39. package/src/shared/media.ts +184 -0
  40. package/src/shared/parse.ts +25 -0
  41. package/src/shared/parts.ts +72 -0
  42. package/src/shared/rpc.ts +145 -0
  43. package/src/shared/scan.ts +32 -0
  44. package/src/shared/settings.ts +471 -0
  45. package/src/shared/stats.ts +142 -0
  46. package/src/shared/tokens.ts +259 -0
  47. package/src/tui/chart/cache-chart.ts +237 -0
  48. package/src/tui/chart/color.ts +58 -0
  49. package/src/tui/index.tsx +111 -0
  50. package/src/tui/panel/cache.tsx +203 -0
  51. package/src/tui/panel/data.ts +105 -0
  52. package/src/tui/panel/index.tsx +176 -0
  53. package/src/tui/panel/summaries.tsx +192 -0
  54. package/src/tui/settings/dialogs.tsx +215 -0
  55. package/src/tui/settings/options.ts +238 -0
  56. package/src/tui/settings.tsx +418 -0
  57. package/src/tui/types.ts +4 -0
@@ -0,0 +1,215 @@
1
+ import type { Context } from "@opencode/plugin/tui/context"
2
+ import { createMemo, createSignal, Show, type ParentProps } from "solid-js"
3
+
4
+ import { formatTokens } from "../../shared/format.ts"
5
+ import type { Messages } from "../../i18n/index.ts"
6
+ import {
7
+ MAX_TRIGGER_PERCENT,
8
+ MIN_TRIGGER_PERCENT,
9
+ parseSummaryPercent,
10
+ parseTokenCount,
11
+ } from "../../shared/settings.ts"
12
+ import { percentLabel } from "./options.ts"
13
+
14
+ /**
15
+ * 输入:TUI 上下文 + 标题 + 内容。
16
+ * 输出:与原生对话框一致的外框(左右 3 格内边距、标题栏、底部操作行)。
17
+ * 预期:标题在左、esc 在右;内容区与底部操作行各留一空行。
18
+ */
19
+ function DialogFrame(
20
+ props: ParentProps<{ readonly context: Context; readonly title: string; readonly submit: string }>,
21
+ ) {
22
+ const theme = props.context.theme
23
+ return (
24
+ <box paddingLeft={3} paddingRight={3} paddingBottom={1} gap={1}>
25
+ <box flexDirection="row" justifyContent="space-between" gap={1}>
26
+ <text fg={theme.text.base}>
27
+ <b>{props.title}</b>
28
+ </text>
29
+ <text
30
+ fg={theme.text.muted}
31
+ onMouseUp={() => {
32
+ props.context.ui.dialog.clear()
33
+ }}
34
+ >
35
+ esc
36
+ </text>
37
+ </box>
38
+ <box gap={1}>{props.children}</box>
39
+ <text fg={theme.text.muted}>{props.submit} enter</text>
40
+ </box>
41
+ )
42
+ }
43
+
44
+ /**
45
+ * 输入:TUI 上下文 + 文案 + 解析/预览/提示 + 确认回调。
46
+ * 输出:单行输入对话框;Token/Percent/Threshold 共用。
47
+ * 预期:只接受 parse 返回数字的输入;非法时显示 invalid,不写入。
48
+ */
49
+ function DialogInput(props: {
50
+ readonly context: Context
51
+ readonly copy: Messages
52
+ readonly title: string
53
+ readonly hint: string
54
+ readonly invalid: string
55
+ readonly placeholder: string
56
+ readonly initial: string
57
+ readonly parse: (text: string) => number | undefined
58
+ readonly preview: (value: number) => string
59
+ readonly extra?: string
60
+ readonly onConfirm: (value: number) => void
61
+ }) {
62
+ const theme = props.context.theme
63
+ const [text, setText] = createSignal(props.initial)
64
+ const parsed = createMemo(() => props.parse(text()))
65
+ let area: { plainText: string } | undefined
66
+
67
+ const confirm = (): void => {
68
+ const value = parsed()
69
+ if (value === undefined) return
70
+ props.onConfirm(value)
71
+ props.context.ui.dialog.clear()
72
+ }
73
+
74
+ // 原生 DialogPrompt 用模态层接管回车;这里同样注册,避免 textarea 插入换行。
75
+ props.context.keymap.layer(() => ({
76
+ mode: "modal",
77
+ priority: 1,
78
+ commands: [{ bind: "enter", title: "Submit", group: "Dialog", run: confirm }],
79
+ }))
80
+
81
+ return (
82
+ <DialogFrame context={props.context} title={props.title} submit={props.copy.settings.submit}>
83
+ <textarea
84
+ height={1}
85
+ wrapMode="none"
86
+ initialValue={props.initial}
87
+ placeholder={props.placeholder}
88
+ placeholderColor={theme.text.muted}
89
+ textColor={theme.text.formfield.base}
90
+ focusedTextColor={theme.text.formfield.base}
91
+ cursorColor={theme.text.base}
92
+ onContentChange={() => setText(area?.plainText ?? "")}
93
+ ref={(input) => {
94
+ area = input
95
+ setTimeout(() => {
96
+ if (!input.isDestroyed) input.focus()
97
+ }, 1)
98
+ }}
99
+ />
100
+ <Show
101
+ when={parsed() !== undefined}
102
+ fallback={<text fg={theme.text.feedback.error.base}>{props.invalid}</text>}
103
+ >
104
+ <text fg={theme.text.muted}>{props.preview(parsed() ?? 0)}</text>
105
+ </Show>
106
+ <Show when={props.extra !== undefined}>
107
+ <text fg={theme.text.muted}>{props.extra}</text>
108
+ </Show>
109
+ <text fg={theme.text.muted}>{props.hint}</text>
110
+ </DialogFrame>
111
+ )
112
+ }
113
+
114
+ /** 输入:TUI 上下文 + 文案 + 标题/提示 + 初始值 + 合法范围 + 确认回调。 */
115
+ export function TokenDialog(props: {
116
+ readonly context: Context
117
+ readonly copy: Messages
118
+ readonly title: string
119
+ readonly hint: string
120
+ readonly invalid: string
121
+ readonly placeholder: string
122
+ readonly initial: string
123
+ readonly min: number
124
+ readonly max: number
125
+ readonly allowZero?: boolean
126
+ readonly onConfirm: (value: number) => void
127
+ }) {
128
+ return (
129
+ <DialogInput
130
+ context={props.context}
131
+ copy={props.copy}
132
+ title={props.title}
133
+ hint={props.hint}
134
+ invalid={props.invalid}
135
+ placeholder={props.placeholder}
136
+ initial={props.initial}
137
+ parse={(text) => {
138
+ if (props.allowZero === true && text.trim() === "0") return 0
139
+ return parseTokenCount(text, props.min, props.max)
140
+ }}
141
+ preview={(value) =>
142
+ value === 0 ? props.copy.settings.off : props.copy.settings.limitPreview(formatTokens(value))
143
+ }
144
+ onConfirm={props.onConfirm}
145
+ />
146
+ )
147
+ }
148
+
149
+ /** 输入:TUI 上下文 + 文案 + 初始百分比 + 窗口 + 允许范围 + 确认回调。 */
150
+ export function PercentDialog(props: {
151
+ readonly context: Context
152
+ readonly copy: Messages
153
+ readonly title: string
154
+ readonly hint: string
155
+ readonly invalid: string
156
+ readonly initial: number
157
+ readonly windowTokens: number | undefined
158
+ readonly minPercent: number
159
+ readonly maxPercent: number
160
+ readonly onConfirm: (value: number) => void
161
+ }) {
162
+ return (
163
+ <DialogInput
164
+ context={props.context}
165
+ copy={props.copy}
166
+ title={props.title}
167
+ hint={props.hint}
168
+ invalid={props.invalid}
169
+ placeholder="10%"
170
+ initial={`${props.initial.toString()}%`}
171
+ parse={(text) => {
172
+ const value = parseSummaryPercent(text, props.windowTokens)
173
+ if (value === undefined || value < props.minPercent || value > props.maxPercent) {
174
+ return undefined
175
+ }
176
+ return value
177
+ }}
178
+ preview={(value) => percentLabel(value, props.windowTokens)}
179
+ extra={
180
+ props.windowTokens === undefined
181
+ ? props.copy.settings.windowUnknown
182
+ : props.copy.settings.windowKnown(formatTokens(props.windowTokens))
183
+ }
184
+ onConfirm={props.onConfirm}
185
+ />
186
+ )
187
+ }
188
+
189
+ /** 输入:TUI 上下文 + 文案 + 初始值 + 确认回调。 */
190
+ export function ThresholdDialog(props: {
191
+ readonly context: Context
192
+ readonly copy: Messages
193
+ readonly initial: string
194
+ readonly onConfirm: (value: number) => void
195
+ }) {
196
+ return (
197
+ <DialogInput
198
+ context={props.context}
199
+ copy={props.copy}
200
+ title={props.copy.settings.threshold}
201
+ hint={props.copy.settings.thresholdHint}
202
+ invalid={props.copy.settings.thresholdInvalid}
203
+ placeholder="80"
204
+ initial={props.initial}
205
+ parse={(text) => {
206
+ if (!/^\d{1,2}$/.test(text)) return undefined
207
+ const value = Number(text)
208
+ if (value < MIN_TRIGGER_PERCENT || value > MAX_TRIGGER_PERCENT) return undefined
209
+ return value
210
+ }}
211
+ preview={(value) => props.copy.settings.thresholdPreview(value.toString())}
212
+ onConfirm={props.onConfirm}
213
+ />
214
+ )
215
+ }
@@ -0,0 +1,238 @@
1
+ import type { DialogSelectOption } from "@opencode/plugin/tui/context"
2
+
3
+ import { formatTokens } from "../../shared/format.ts"
4
+ import type { Messages } from "../../i18n/index.ts"
5
+ import {
6
+ effectiveDecompressInlineMax,
7
+ effectiveDecompressTempCap,
8
+ effectiveSummaryBlockPercent,
9
+ effectiveSummaryLanguage,
10
+ effectiveSummaryTotalPercent,
11
+ isContextLimit,
12
+ percentToChars,
13
+ type Settings,
14
+ } from "../../shared/settings.ts"
15
+
16
+ /** 输入:设置;输出:当前语言标签;预期:用于设置菜单右侧的当前值。 */
17
+ export function localeLabel(settings: Settings, copy: Messages): string {
18
+ if (settings.locale === "auto") return copy.settings.auto
19
+ if (settings.locale === "zh") return copy.languages.zh
20
+ return copy.languages.en
21
+ }
22
+
23
+ /** 输入:设置;输出:当前摘要语言标签。 */
24
+ export function summaryLanguageLabel(settings: Settings, copy: Messages): string {
25
+ const language = effectiveSummaryLanguage(settings)
26
+ if (language === "zh") return copy.languages.zh
27
+ if (language === "en") return copy.languages.en
28
+ return copy.settings.auto
29
+ }
30
+
31
+ /** 输入:设置 + 已知窗口;输出:上下文上限标签;数字设置是上限,被模型原生窗口压低时显示箭头。 */
32
+ export function limitLabel(settings: Settings, copy: Messages, window: number | undefined): string {
33
+ const value: unknown = settings.contextLimit
34
+ if (!isContextLimit(value) || value === "auto") {
35
+ return window === undefined ? copy.settings.limitAuto : `${copy.settings.limitAuto} (${formatTokens(window)})`
36
+ }
37
+ if (window !== undefined && window < value) {
38
+ return copy.settings.limitCapped(formatTokens(value), formatTokens(window))
39
+ }
40
+ return formatTokens(value)
41
+ }
42
+
43
+ /** 输入:百分比 + 窗口;输出:`10% ≈ 20k` 这样的标签。 */
44
+ export function percentLabel(percent: number, window: number | undefined): string {
45
+ const chars = percentToChars(percent, window)
46
+ return chars === undefined
47
+ ? `${percent.toString()}%`
48
+ : `${percent.toString()}% ≈ ${formatTokens(chars)}`
49
+ }
50
+
51
+ /** 输入:设置;输出:主动压缩提醒间隔标签;0 显示为关闭。 */
52
+ export function nudgeLabel(settings: Settings, copy: Messages): string {
53
+ return settings.nudgeGrowthTokens === 0 ? copy.settings.off : formatTokens(settings.nudgeGrowthTokens)
54
+ }
55
+
56
+ /** 输入:设置;输出:当前摘要格式标签。 */
57
+ export function summaryFormatLabel(settings: Settings, copy: Messages): string {
58
+ if (settings.summaryFormat === "babel") return copy.settings.summaryFormatBabel
59
+ return settings.summaryFormat === "markdown"
60
+ ? copy.settings.summaryFormatMarkdown
61
+ : copy.settings.summaryFormatJson
62
+ }
63
+
64
+ /** 设置菜单里所有可选 action;options.ts 与 settings.tsx 共用。 */
65
+ export const SETTINGS_ACTIONS = [
66
+ "limit",
67
+ "threshold",
68
+ "nudge",
69
+ "autoCompress",
70
+ "enableCompressTool",
71
+ "enableDecompressTool",
72
+ "blockPercent",
73
+ "totalPercent",
74
+ "summaryLanguage",
75
+ "summaryFormat",
76
+ "showTimeline",
77
+ "showBlockTokens",
78
+ "show",
79
+ "showCacheChart",
80
+ "showCacheLog",
81
+ "showSummaries",
82
+ "decompressInlineMax",
83
+ "decompressTempCap",
84
+ "decompressShowTokens",
85
+ "language",
86
+ ] as const
87
+
88
+ export type SettingsAction = (typeof SETTINGS_ACTIONS)[number]
89
+
90
+ /** 设置菜单里可切换的布尔项。 */
91
+ export type ToggleSetting = Extract<
92
+ SettingsAction,
93
+ | "autoCompress"
94
+ | "enableCompressTool"
95
+ | "enableDecompressTool"
96
+ | "showTimeline"
97
+ | "showBlockTokens"
98
+ | "showCacheChart"
99
+ | "showCacheLog"
100
+ | "showSummaries"
101
+ | "decompressShowTokens"
102
+ >
103
+
104
+ /**
105
+ * 输入:当前设置 + 文案 + 已解析窗口。
106
+ * 输出:按「压缩 / 摘要 / 面板 / 解压 / 界面」分组的设置菜单项。
107
+ * 预期:纯函数,方便单测;dialog 会按 category 渲染分组标题。
108
+ */
109
+ export function buildSettingsOptions(input: {
110
+ readonly settings: Settings
111
+ readonly copy: Messages
112
+ readonly window: number | undefined
113
+ }): readonly DialogSelectOption<SettingsAction>[] {
114
+ const { settings, copy, window } = input
115
+ const onOff = (value: boolean): string => (value ? copy.settings.on : copy.settings.off)
116
+ return [
117
+ {
118
+ title: copy.settings.limit,
119
+ value: "limit",
120
+ footer: limitLabel(settings, copy, window),
121
+ category: copy.settings.categories.compress,
122
+ },
123
+ {
124
+ title: copy.settings.threshold,
125
+ value: "threshold",
126
+ footer: `${settings.triggerPercent.toString()}%`,
127
+ category: copy.settings.categories.compress,
128
+ },
129
+ {
130
+ title: copy.settings.nudgeGrowth,
131
+ value: "nudge",
132
+ footer: nudgeLabel(settings, copy),
133
+ category: copy.settings.categories.compress,
134
+ },
135
+ {
136
+ title: copy.settings.autoCompress,
137
+ value: "autoCompress",
138
+ footer: onOff(settings.autoCompress),
139
+ category: copy.settings.categories.compress,
140
+ },
141
+ {
142
+ title: copy.settings.enableCompressTool,
143
+ value: "enableCompressTool",
144
+ footer: onOff(settings.enableCompressTool),
145
+ category: copy.settings.categories.compress,
146
+ },
147
+ {
148
+ title: copy.settings.enableDecompressTool,
149
+ value: "enableDecompressTool",
150
+ footer: onOff(settings.enableDecompressTool),
151
+ category: copy.settings.categories.compress,
152
+ },
153
+ {
154
+ title: copy.settings.summaryBlockPercent,
155
+ value: "blockPercent",
156
+ footer: percentLabel(effectiveSummaryBlockPercent(settings), window),
157
+ category: copy.settings.categories.summary,
158
+ },
159
+ {
160
+ title: copy.settings.summaryTotalPercent,
161
+ value: "totalPercent",
162
+ footer: percentLabel(effectiveSummaryTotalPercent(settings), window),
163
+ category: copy.settings.categories.summary,
164
+ },
165
+ {
166
+ title: copy.settings.summaryLanguage,
167
+ value: "summaryLanguage",
168
+ footer: summaryLanguageLabel(settings, copy),
169
+ category: copy.settings.categories.summary,
170
+ },
171
+ {
172
+ title: copy.settings.summaryFormat,
173
+ value: "summaryFormat",
174
+ footer: summaryFormatLabel(settings, copy),
175
+ category: copy.settings.categories.summary,
176
+ },
177
+ {
178
+ title: copy.settings.showTimeline,
179
+ value: "showTimeline",
180
+ footer: onOff(settings.showTimeline),
181
+ category: copy.settings.categories.summary,
182
+ },
183
+ {
184
+ title: copy.settings.showBlockTokens,
185
+ value: "showBlockTokens",
186
+ footer: onOff(settings.showBlockTokens),
187
+ category: copy.settings.categories.summary,
188
+ },
189
+ {
190
+ title: copy.settings.show,
191
+ value: "show",
192
+ footer: onOff(settings.enabled),
193
+ category: copy.settings.categories.panel,
194
+ },
195
+ {
196
+ title: copy.settings.showCacheChart,
197
+ value: "showCacheChart",
198
+ footer: onOff(settings.showCacheChart),
199
+ category: copy.settings.categories.panel,
200
+ },
201
+ {
202
+ title: copy.settings.showCacheLog,
203
+ value: "showCacheLog",
204
+ footer: onOff(settings.showCacheLog),
205
+ category: copy.settings.categories.panel,
206
+ },
207
+ {
208
+ title: copy.settings.showSummaries,
209
+ value: "showSummaries",
210
+ footer: onOff(settings.showSummaries),
211
+ category: copy.settings.categories.panel,
212
+ },
213
+ {
214
+ title: copy.settings.decompressInlineMax,
215
+ value: "decompressInlineMax",
216
+ footer: formatTokens(effectiveDecompressInlineMax(settings)),
217
+ category: copy.settings.categories.decompress,
218
+ },
219
+ {
220
+ title: copy.settings.decompressTempCap,
221
+ value: "decompressTempCap",
222
+ footer: effectiveDecompressTempCap(settings).toString(),
223
+ category: copy.settings.categories.decompress,
224
+ },
225
+ {
226
+ title: copy.settings.decompressShowTokens,
227
+ value: "decompressShowTokens",
228
+ footer: onOff(settings.decompressShowTokens),
229
+ category: copy.settings.categories.decompress,
230
+ },
231
+ {
232
+ title: copy.settings.language,
233
+ value: "language",
234
+ footer: localeLabel(settings, copy),
235
+ category: copy.settings.categories.interface,
236
+ },
237
+ ]
238
+ }