@trans-ai/trans-code 0.8.1 → 0.9.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/docs/superpowers/plans/2026-11-21-auto-compact.md +84 -0
- package/docs/superpowers/plans/2026-11-21-compact-speed.md +85 -0
- package/docs/superpowers/plans/2026-11-21-keep-turns-param.md +74 -0
- package/docs/superpowers/specs/2026-11-21-auto-compact-design.md +25 -0
- package/docs/superpowers/specs/2026-11-21-compact-speed-design.md +21 -0
- package/docs/superpowers/specs/2026-11-21-keep-turns-param-design.md +26 -0
- package/package.json +1 -1
- package/src/commands.ts +2 -2
- package/src/config.test.ts +25 -0
- package/src/config.ts +50 -0
- package/src/screens/REPL.tsx +124 -27
- package/src/sessions/compact.test.ts +119 -26
- package/src/sessions/compact.ts +60 -15
- package/src/sessions/resume.test.ts +12 -1
- package/src/sessions/resume.ts +13 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# 自动压缩 Implementation Plan
|
|
2
|
+
|
|
3
|
+
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
|
4
|
+
|
|
5
|
+
**Goal:** 每轮提交前估算上下文,超阈值自动压缩(与 /compact、resume 同一 `runCompaction`),带配置与连败熔断。
|
|
6
|
+
|
|
7
|
+
**Architecture:** 纯逻辑(估算 + 配置归一)落 compact.ts/config.ts 走 TDD;REPL 在 handleSubmit 进入 agent loop 前接检查点,复用宿主条目流式显示;基于已审查的 `feat/compact-speed` 分支续作。
|
|
8
|
+
|
|
9
|
+
**Tech Stack:** TypeScript、bun:test。
|
|
10
|
+
|
|
11
|
+
**Spec:** `docs/superpowers/specs/2026-11-21-auto-compact-design.md`
|
|
12
|
+
|
|
13
|
+
## Global Constraints
|
|
14
|
+
|
|
15
|
+
- 阈值默认 `thresholdChars = 200_000`,估算 `estimateTokens = Σ contentText(chars)/4`
|
|
16
|
+
- 配置 `autoCompact?: { enabled?: boolean; thresholdChars?: number }`,默认 **enabled = true**;坏类型/空值全量回落默认(仿 normalizeAutoCompact 与 normalizeAutoUpdate 同风格)
|
|
17
|
+
- 熔断:连续失败 **3** 次停止自动压缩,成功复位;失败提示仅每会话首次显示
|
|
18
|
+
- 压缩参数 keepTurns = `DEFAULT_KEEP_TURNS`;三入口共用 `runCompaction`
|
|
19
|
+
- 跳过:无进行中会话、`TRANSCODE_MOCK=1`、busy
|
|
20
|
+
|
|
21
|
+
## Review Focus
|
|
22
|
+
|
|
23
|
+
- 首轮(无 conversationRef)与未达阈值不触发(接线)
|
|
24
|
+
- 恰好等于阈值触发(估算 `>=` 边界,Task 1 测试)
|
|
25
|
+
- 连败 3 次停、成功复位(接线,ref 语义)
|
|
26
|
+
- 坏配置(字符串 thresholdChars、null、数组)回落默认不崩(Task 1 测试)
|
|
27
|
+
- 压缩后本轮 loop 使用已压缩 messagesRef 且提要注入生效(复用现有注入点,接线核对)
|
|
28
|
+
- autoCompact 与用户提问回显的条目顺序不串(接线)
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
### Task 1: 估算 + 配置归一(纯逻辑)
|
|
33
|
+
|
|
34
|
+
**Files:**
|
|
35
|
+
- Modify: `src/sessions/compact.ts`、`src/sessions/compact.test.ts`、`src/config.ts`、`src/config.test.ts`
|
|
36
|
+
|
|
37
|
+
**Interfaces:**
|
|
38
|
+
- Produces:
|
|
39
|
+
- `estimateTokens(messages: BaseMessage[]): number`(compact.ts;contentText 求和 / 4,向下取整)
|
|
40
|
+
- config.ts:`interface AutoCompactConfig { enabled?: boolean; thresholdChars?: number }`、`interface NormalizedAutoCompact { enabled: boolean; thresholdChars: number }`、`normalizeAutoCompact(raw: unknown): NormalizedAutoCompact`、`TranscodeConfig.autoCompact?: boolean | AutoCompactConfig`(boolean 直通,与 autoUpdate 同款 raw 形状)
|
|
41
|
+
|
|
42
|
+
- [ ] **Step 1: 失败测试**
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
// compact.test.ts
|
|
46
|
+
describe('estimateTokens', () => {
|
|
47
|
+
test('sums content chars / 4, floors; non-string content via JSON; empty → 0', () => {
|
|
48
|
+
expect(estimateTokens([])).toBe(0);
|
|
49
|
+
expect(estimateTokens([new HumanMessage('a'.repeat(40))])).toBe(10);
|
|
50
|
+
expect(estimateTokens([new HumanMessage('x'.repeat(10)), new AIMessage('y'.repeat(30))])).toBe(10);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
// config.test.ts
|
|
54
|
+
describe('normalizeAutoCompact', () => {
|
|
55
|
+
test('undefined → enabled default 200_000; false → disabled; valid override kept', () => {
|
|
56
|
+
expect(normalizeAutoCompact(undefined)).toEqual({ enabled: true, thresholdChars: 200_000 });
|
|
57
|
+
expect(normalizeAutoCompact(false)).toEqual({ enabled: false, thresholdChars: 200_000 });
|
|
58
|
+
expect(normalizeAutoCompact({ thresholdChars: 50_000 })).toEqual({ enabled: true, thresholdChars: 50_000 });
|
|
59
|
+
});
|
|
60
|
+
test('garbage (string/null/array/negative) falls back to defaults', () => {
|
|
61
|
+
expect(normalizeAutoCompact('yes')).toEqual({ enabled: true, thresholdChars: 200_000 });
|
|
62
|
+
expect(normalizeAutoCompact([1])).toEqual({ enabled: true, thresholdChars: 200_000 });
|
|
63
|
+
expect(normalizeAutoCompact({ thresholdChars: -5 })).toEqual({ enabled: true, thresholdChars: 200_000 });
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
- [ ] **Step 2: 确认失败**(导出不存在)
|
|
69
|
+
- [ ] **Step 3: 实现** — `estimateTokens` 用已有 `contentText`;`normalizeAutoCompact` 仿 `normalizeAutoUpdate` 分支
|
|
70
|
+
- [ ] **Step 4: 确认通过**
|
|
71
|
+
- [ ] **Step 5: Commit** — `feat: token estimation and autoCompact config normalization`
|
|
72
|
+
|
|
73
|
+
### Task 2: REPL 检查点接线
|
|
74
|
+
|
|
75
|
+
**Files:**
|
|
76
|
+
- Modify: `src/screens/REPL.tsx`(handleSubmit 进入 agent loop 前;import estimateTokens/normalizeAutoCompact/loadConfig 已有)
|
|
77
|
+
|
|
78
|
+
**Interfaces:**
|
|
79
|
+
- Consumes: Task 1 全部;`runCompaction`、`buildSessionRecord`、`transcriptEntries`、`DEFAULT_KEEP_TURNS`、`saveSession`
|
|
80
|
+
|
|
81
|
+
- [ ] **Step 1: 状态** — `const autoCompactFailuresRef = useRef(0);`、`const autoCompactWarnedRef = useRef(false);`
|
|
82
|
+
- [ ] **Step 2: 检查点**(handleSubmit 内、构建 provider/进入 try 之前):条件 `normalizeAutoCompact(loadConfig().autoCompact).enabled && conversationRef.current && process.env.TRANSCODE_MOCK !== '1' && autoCompactFailuresRef.current < 3 && estimateTokens(messagesRef.current) >= cfg.thresholdChars` 满足时:`setBusyBoth(true)` → 空宿主条目 → `runCompaction(buildSessionRecord({...}), DEFAULT_KEEP_TURNS)`;成功 → 回写 refs + `saveSession` + 失败计数清零 + `transcriptEntries` 渲染;失败/异常 → 计数 +1,首次时 `⚠ 自动压缩失败(原因),本轮继续;连续 3 次将暂停自动压缩`,达 3 次时提示「自动压缩已暂停」;finally `setBusyBoth(false)`
|
|
83
|
+
- [ ] **Step 3: 全量验证** — `bun test` + `npm run typecheck` + `npm run build:repl`
|
|
84
|
+
- [ ] **Step 4: Commit** — `feat: proactive auto-compact with threshold config and failure circuit breaker`
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# 压缩提速(流式 + compact 路由 + 硬上限)Implementation Plan
|
|
2
|
+
|
|
3
|
+
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
|
4
|
+
|
|
5
|
+
**Goal:** 压缩调用改为流式并实时显示、可路由快速模型、输出字符硬上限,消除感知与实际慢。
|
|
6
|
+
|
|
7
|
+
**Architecture:** `CompactModel` 换成 stream 接口,`compressSession` 增 `onDelta` 回调与 `COMPACT_MAX_SUMMARY_CHARS` 截断;REPL 两入口接 `appendChatOutput` 流式显示,模型经 `getRouting()['compact'] ?? ctx.modelId` 选择。
|
|
8
|
+
|
|
9
|
+
**Tech Stack:** TypeScript、LangChain(`model.stream()`)、bun:test。
|
|
10
|
+
|
|
11
|
+
**Spec:** `docs/superpowers/specs/2026-11-21-compact-speed-design.md`
|
|
12
|
+
|
|
13
|
+
## Global Constraints
|
|
14
|
+
|
|
15
|
+
- `COMPACT_MAX_SUMMARY_CHARS = 1200`,累积超限即停止消费流并 `slice` 到上限
|
|
16
|
+
- `CompactModel = { stream(input: string): AsyncIterable<{ content: unknown }> }`;chunk 文本取 `typeof content === 'string' ? content : ''`
|
|
17
|
+
- 模型选择:`createModel(getRouting().compact ?? ctx.modelId)`;未配置 routing 行为与现状一致
|
|
18
|
+
- 失败回退(流中抛错/空摘要)、滚动合并、≤keepTurns no-op 语义不变;`TRANSCODE_MOCK` 跳过压缩不变
|
|
19
|
+
|
|
20
|
+
## Review Focus
|
|
21
|
+
|
|
22
|
+
- 流中第 2 块抛错 → 原会话完整回退、无文件写入(Task 1 测试)
|
|
23
|
+
- 显示与保存一致:onDelta 逐块文本拼接 === 最终 summary(Task 1 测试)
|
|
24
|
+
- 超限:5000 字符流 → summary ≤ 1200 且提前停止消费(Task 1 测试 + 生成器消费计数)
|
|
25
|
+
- 未配 `routing.compact` → 用当前模型(接线:?? 右侧默认)
|
|
26
|
+
- 多块 content 非字符串(如数组块)不崩、按空串跳过(Task 1 测试)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### Task 1: 流式压缩核心
|
|
31
|
+
|
|
32
|
+
**Files:**
|
|
33
|
+
- Modify: `src/sessions/compact.ts`、`src/sessions/compact.test.ts`
|
|
34
|
+
|
|
35
|
+
**Interfaces:**
|
|
36
|
+
- Produces:
|
|
37
|
+
- `export const COMPACT_MAX_SUMMARY_CHARS = 1200`
|
|
38
|
+
- `export interface CompactModel { stream(input: string): AsyncIterable<{ content: unknown }> }`
|
|
39
|
+
- `compressSession(record, model, keepTurns = DEFAULT_KEEP_TURNS, onDelta?: (delta: string) => void)`(返回形不变)
|
|
40
|
+
|
|
41
|
+
- [ ] **Step 1: 失败测试** — mock 改 async generator(`streamModel(chunks: string[], opts?: {throwAt?: number; trackPulled?: number[]}): CompactModel & { prompts: string[] }`),新增/改写:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
test('streams chunks into the summary and forwards each delta', async () => {
|
|
45
|
+
const deltas: string[] = [];
|
|
46
|
+
const { record } = await compressSession(recordWith(3), streamModel(['前情', '提要']), 1, d => deltas.push(d));
|
|
47
|
+
expect(record.summary).toBe('前情提要');
|
|
48
|
+
expect(deltas).toEqual(['前情', '提要']);
|
|
49
|
+
});
|
|
50
|
+
test('caps summary at COMPACT_MAX_SUMMARY_CHARS and stops consuming early', async () => {
|
|
51
|
+
const pulled: number[] = [];
|
|
52
|
+
const model = streamModel(['x'.repeat(1000), 'y'.repeat(1000), 'z'.repeat(1000)], { trackPulled: pulled });
|
|
53
|
+
const { record } = await compressSession(recordWith(3), model, 1);
|
|
54
|
+
expect(record.summary!.length).toBeLessThanOrEqual(COMPACT_MAX_SUMMARY_CHARS);
|
|
55
|
+
expect(pulled.length).toBe(2); // 第三块未被消费
|
|
56
|
+
});
|
|
57
|
+
test('mid-stream error falls back with error set', async () => {
|
|
58
|
+
const { compressed, record, error } = await compressSession(recordWith(3), streamModel(['前情'], { throwAt: 2 }), 1);
|
|
59
|
+
expect(compressed).toBe(false); expect(error).toBeDefined(); expect(record).toBeDefined();
|
|
60
|
+
});
|
|
61
|
+
test('non-string chunk content is skipped', async () => {
|
|
62
|
+
const model = { stream: async function* () { yield { content: '前情' }; yield { content: [{ text: 'x' }] }; yield { content: '提要' }; } };
|
|
63
|
+
const { record } = await compressSession(recordWith(3), model as never, 1);
|
|
64
|
+
expect(record.summary).toBe('前情提要');
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
(既有 8 个压缩用例换用 `streamModel(['提要内容'])` 等形式,断言不变)
|
|
68
|
+
|
|
69
|
+
- [ ] **Step 2: 确认失败** — `bun test src/sessions/compact.test.ts` → FAIL(stream 不存在/断言不匹配)
|
|
70
|
+
- [ ] **Step 3: 实现** — `COMPACT_MAX_SUMMARY_CHARS`;`compressSession` 内:`let summary = ''` + `for await (const chunk of model.stream(prompt))` 逐块取串文本、`onDelta?.(text)`、超限 `break`;循环后 `summary = summary.slice(0, COMPACT_MAX_SUMMARY_CHARS).trim()`;空 → error;catch 覆盖流中抛错
|
|
71
|
+
- [ ] **Step 4: 确认通过** — 同命令 → PASS
|
|
72
|
+
- [ ] **Step 5: Commit** — `git add src/sessions/compact.ts src/sessions/compact.test.ts && git commit -m "feat: streaming compaction with per-chunk deltas and hard char cap"`
|
|
73
|
+
|
|
74
|
+
### Task 2: REPL 流式显示 + compact 路由
|
|
75
|
+
|
|
76
|
+
**Files:**
|
|
77
|
+
- Modify: `src/screens/REPL.tsx`(resumeWith、/compact 分支;import `getRouting`)
|
|
78
|
+
|
|
79
|
+
**Interfaces:**
|
|
80
|
+
- Consumes: Task 1 全部导出;`getRouting()`(provider.js 已导出)
|
|
81
|
+
|
|
82
|
+
- [ ] **Step 1: 共享 helper** — 抽 `runCompaction(record, keepTurns): Promise<{compressed, record, error?}>`:内部统一「正在提炼前情提要…」提示 + `createModel(getRouting().compact ?? ctx.modelId).model` + `onDelta: delta => appendChatOutput(delta)` + 完成后补 `\n\n`;resumeWith 与 /compact 两入口**都只调它**,行为完全一致
|
|
83
|
+
- [ ] **Step 2: 两入口接线** — resumeWith 与 /compact 分支替换各自的模型构造与调用为 `runCompaction`(mock 跳过逻辑保留在 helper 内)
|
|
84
|
+
- [ ] **Step 3: 全量验证** — `bun test` 全绿 + `npm run typecheck` + `npm run build:repl`
|
|
85
|
+
- [ ] **Step 4: Commit** — `git commit -m "feat: live-stream compaction summary and compact-role fast-model routing"`
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# 压缩保留轮数参数化 Implementation Plan
|
|
2
|
+
|
|
3
|
+
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
|
4
|
+
|
|
5
|
+
**Goal:** `/compact [N]` 与 `/resume [target] [N]` 可指定压缩保留的最近轮数,默认 1 轮。
|
|
6
|
+
|
|
7
|
+
**Architecture:** `compressSession` 增加第三参 `keepTurns`(默认 `DEFAULT_KEEP_TURNS = 1`),新增纯函数 `parseKeepCount` 校验参数;REPL 两条恢复路径与 /compact 接线;核心逻辑 TDD。
|
|
8
|
+
|
|
9
|
+
**Tech Stack:** TypeScript、bun:test。
|
|
10
|
+
|
|
11
|
+
**Spec:** `docs/superpowers/specs/2026-11-21-keep-turns-param-design.md`
|
|
12
|
+
|
|
13
|
+
## Global Constraints
|
|
14
|
+
|
|
15
|
+
- `DEFAULT_KEEP_TURNS = 1`(替代 `KEEP_TURNS = 10`)
|
|
16
|
+
- `/resume` 首参仍是会话目标(现有解析不变);第二个纯数字参数 ≥1 才生效,非法(0/负数/非数字)提示用法并按默认执行
|
|
17
|
+
- 保留轮数不持久化
|
|
18
|
+
- 其余行为(滚动合并、失败回退、原子写、前情提要注入)不动
|
|
19
|
+
|
|
20
|
+
## Review Focus
|
|
21
|
+
|
|
22
|
+
- 默认值语义:>1 轮会话默认压缩到只剩 1 轮(Task 测试:default keeps 1)
|
|
23
|
+
- `/resume 3 0`、`/resume 3 abc`:用法提示 + 按默认执行,不崩(parseKeepCount 测试)
|
|
24
|
+
- 显式大窗口仍可用:`compressSession(rec, model, 10)` 保留 10 轮(Task 测试)
|
|
25
|
+
- picker Enter(无参路径)用默认 1(接线,复用现有 DEFAULT 导入)
|
|
26
|
+
- 「无需压缩」提示与「旧 N 轮」计数按生效 N 计算(接线)
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### Task 1: keepTurns 参数化(核心 + 接线)
|
|
31
|
+
|
|
32
|
+
**Files:**
|
|
33
|
+
- Modify: `src/sessions/compact.ts`、`src/sessions/compact.test.ts`、`src/screens/REPL.tsx`(resume 分支、resumeWith、/compact 分支)
|
|
34
|
+
|
|
35
|
+
**Interfaces:**
|
|
36
|
+
- Produces:
|
|
37
|
+
- `export const DEFAULT_KEEP_TURNS = 1`
|
|
38
|
+
- `compressSession(record, model, keepTurns = DEFAULT_KEEP_TURNS)`(内部窗口判断全用 keepTurns,`Math.max(1, keepTurns)` 防御)
|
|
39
|
+
- `parseKeepCount(raw: string | undefined): { keep?: number; error?: string }` — undefined→`{}`;`/^\d+$/` 且 ≥1→`{keep:N}`;否则→`{error:'保留轮数需为 ≥1 的整数,已按默认 N 轮执行'}`(N 为默认值)
|
|
40
|
+
|
|
41
|
+
- [ ] **Step 1: 失败测试**(compact.test.ts 重写压缩用例并新增)
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
test('default keeps only 1 turn', async () => {
|
|
45
|
+
const { compressed, record } = await compressSession(recordWith(3), mockModel('提要'));
|
|
46
|
+
expect(compressed).toBe(true);
|
|
47
|
+
expect(splitTurns(record.messages).turns.length).toBe(1);
|
|
48
|
+
});
|
|
49
|
+
test('explicit keepTurns=10 keeps 10', async () => {
|
|
50
|
+
const { record } = await compressSession(recordWith(12), mockModel('提要'), 10);
|
|
51
|
+
expect(splitTurns(record.messages).turns.length).toBe(10);
|
|
52
|
+
});
|
|
53
|
+
test('keepTurns below 1 clamps to 1', async () => {
|
|
54
|
+
const { record } = await compressSession(recordWith(3), mockModel('提要'), 0);
|
|
55
|
+
expect(splitTurns(record.messages).turns.length).toBe(1);
|
|
56
|
+
});
|
|
57
|
+
describe('parseKeepCount', () => {
|
|
58
|
+
test('undefined → default; "5" → 5; "0"/"abc" → error, no keep', () => {
|
|
59
|
+
expect(parseKeepCount(undefined)).toEqual({});
|
|
60
|
+
expect(parseKeepCount('5')).toEqual({ keep: 5 });
|
|
61
|
+
expect(parseKeepCount('0').keep).toBeUndefined();
|
|
62
|
+
expect(parseKeepCount('0').error).toBeDefined();
|
|
63
|
+
expect(parseKeepCount('abc').error).toBeDefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
(既有「≤KEEP_TURNS no-op」用例改为显式传 10;rolling merge / truncation / failure 用例传 10 保持原语义)
|
|
68
|
+
|
|
69
|
+
- [ ] **Step 2: 确认失败** — `bun test src/sessions/compact.test.ts` → FAIL(KEEP_TURNS/parseKeepCount 不存在)
|
|
70
|
+
- [ ] **Step 3: 实现** — `DEFAULT_KEEP_TURNS = 1`;`compressSession` 第三参与钳制;`parseKeepCount` 按接口注释
|
|
71
|
+
- [ ] **Step 4: 确认通过** — 同命令 → PASS
|
|
72
|
+
- [ ] **Step 5: REPL 接线** — import 改 `DEFAULT_KEEP_TURNS`;resume 分支:`parseKeepCount(args[1])`,error 则 addHistory 提示,keep 传入 `resumeWith(record, trimmed, keep)`;`resumeWith(record, inputLine, keepTurns = DEFAULT_KEEP_TURNS)` 内部判断与 `oldTurns = turns.length - keepTurns` 全部改用参数;picker Enter 调 `resumeWith(record, '/resume')` 走默认;`/compact` 分支:`parseKeepCount(args[0])` 同规则,「无需压缩(当前 X 轮 ≤ N)」与 oldTurnsCount 用生效 N
|
|
73
|
+
- [ ] **Step 6: 全量验证** — `bun test` 全绿 + `npm run typecheck` + `npm run build:repl`
|
|
74
|
+
- [ ] **Step 7: Commit** — `git add src/sessions/compact.ts src/sessions/compact.test.ts src/screens/REPL.tsx && git commit -m "feat: parameterizable keep-turns for /compact and /resume (default 1)"`
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# 自动压缩(每轮提交前阈值检查)— 设计
|
|
2
|
+
|
|
3
|
+
## 意图
|
|
4
|
+
|
|
5
|
+
对齐 Claude Code 的 proactive autocompact:会话进行中每轮提交前检查上下文占用,超阈值自动压缩,避免长会话撞上下文上限。与恢复压缩、`/compact` 三路归一(同一个 `runCompaction`)。
|
|
6
|
+
|
|
7
|
+
## 设计(参考 claudecode/src/services/compact/autoCompact.ts)
|
|
8
|
+
|
|
9
|
+
- **检查点**:每次提交聊天消息时(`handleSubmit` 在进入 agent loop 之前),估算 `messagesRef` 总量,超阈值先压缩再继续本轮
|
|
10
|
+
- **估算**:纯函数 `estimateTokens(messages) = 全部消息文本长度 / 4`(CC 同款粗估,chars≈4/token)
|
|
11
|
+
- **阈值**:`autoCompact.thresholdChars`(默认 **200_000** 字符 ≈ 50k token —— 多厂商无窗口注册表,取保守值;128k 窗口模型够用,32k 模型也不会先撞墙),命中即压缩
|
|
12
|
+
- **配置**:`~/.transcode.json` 的 `autoCompact?: { enabled?: boolean; thresholdChars?: number }`,`normalizeAutoCompact` 容错(仿 `normalizeAutoUpdate`:坏配置回落默认,**默认开启**)
|
|
13
|
+
- **压缩参数**:keepTurns 用 `DEFAULT_KEEP_TURNS`(=1,与两入口一致的「一套逻辑」);失败回退语义不变
|
|
14
|
+
- **熔断器**:`useRef` 记连续失败次数,**连续 3 次停止自动压缩**(CC 同款,真实事故换来的),成功即复位;自动失败提示仅首次显示,后续静默(CC:自动失败不打扰)
|
|
15
|
+
- **显示**:压缩前先落一条空宿主条目,流式摘要照常打进聊天区,完成后 `transcriptEntries` 渲染窗口(与 /compact 同款)
|
|
16
|
+
- **跳过**:无进行中会话(首轮)、`TRANSCODE_MOCK=1`、已 busy、压缩中重入
|
|
17
|
+
|
|
18
|
+
## 非目标
|
|
19
|
+
|
|
20
|
+
- 不做 per-model 上下文窗口注册表、microcompact(工具结果修剪)、reactive(PTL 错误后被动压缩)、阻塞上限/警告分层 —— 有 per-model 窗口数据后再上
|
|
21
|
+
|
|
22
|
+
## 备选(已否)
|
|
23
|
+
|
|
24
|
+
- 默认关闭待配置:用户点明参考 CC(CC 默认开启)— 默认开
|
|
25
|
+
- 按轮数触发(如每 20 轮):轮长差异大,token 估算更贴近 CC 语义
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# 压缩提速(流式 + 快速模型路由 + 硬上限)— 设计
|
|
2
|
+
|
|
3
|
+
## 意图
|
|
4
|
+
|
|
5
|
+
压缩慢的两个根源:阻塞式 `invoke()` 全程无反馈(感知慢)、用当前推理模型烧思考 token(真慢)。对齐 Claude Code 的做法:流式展示、可路由快速模型、输出硬上限。
|
|
6
|
+
|
|
7
|
+
## 设计
|
|
8
|
+
|
|
9
|
+
- **流式**:`CompactModel` 接口从 `{ invoke }` 改为 `{ stream(input): AsyncIterable<{content}> }`;`compressSession(record, model, keepTurns, onDelta?)` 逐块累积摘要,`onDelta(块文本)` 回调供 REPL 实时打进聊天区(先显示「正在提炼前情提要…」再流式输出)。流中抛错/空结果 → 原有失败回退语义不变
|
|
10
|
+
- **字符硬上限**:`COMPACT_MAX_SUMMARY_CHARS = 1200`,累积超限即停止消费流(provider 无关的输出钳制,替代依赖各厂商 maxTokens 参数)
|
|
11
|
+
- **快速模型路由**:压缩调用用 `createModel(getRouting()['compact'] ?? 当前模型)` —— 在 `~/.transcode.json` 的 `routing` 里配 `"compact": "<快速模型>"`(如 glm-4.6-air / deepseek-chat)即可换快模型;未配置则维持当前模型(流式 + 上限仍生效)
|
|
12
|
+
- 测试:mock 改为 async generator;新增流式累积、超限截断、流中报错、onDelta 传递用例
|
|
13
|
+
|
|
14
|
+
## 非目标
|
|
15
|
+
|
|
16
|
+
- 不做 prompt-cache 复用(单请求形态不同)、不做增量边界总结(数据模型不同)、不引入 <summary> 八段式(后续可单独迭代)
|
|
17
|
+
|
|
18
|
+
## 备选(已否)
|
|
19
|
+
|
|
20
|
+
- per-call maxTokens 参数:LangChain 跨厂商 bind 参数行为不一,字符上限更可靠
|
|
21
|
+
- 默认硬编码快速模型:用户 key/厂商各异,routing 显式配置更诚实
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# 压缩保留轮数参数化(/compact、/resume)— 设计
|
|
2
|
+
|
|
3
|
+
## 意图
|
|
4
|
+
|
|
5
|
+
压缩保留的「最近 N 轮」由固定 10 改为**可传参、默认 1**,两个入口都支持。
|
|
6
|
+
|
|
7
|
+
## 用法
|
|
8
|
+
|
|
9
|
+
- `/compact` → 保留最近 1 轮;`/compact 5` → 保留最近 5 轮
|
|
10
|
+
- `/resume` → 选择器,Enter 后按默认 1 轮压缩;`/resume 3` → 恢复第 3 个、默认 1 轮;`/resume 3 5` → 恢复第 3 个、保留 5 轮
|
|
11
|
+
- 解析:第二个纯数字参数 = 保留轮数,须 ≥1;不合法(N<1 或非数字)提示用法并忽略
|
|
12
|
+
- 选择器路径(Enter)固定用默认值,不接参数
|
|
13
|
+
|
|
14
|
+
## 设计
|
|
15
|
+
|
|
16
|
+
- `compact.ts`:`KEEP_TURNS = 10` 改为 `DEFAULT_KEEP_TURNS = 1`;`compressSession(record, model, keepTurns = DEFAULT_KEEP_TURNS)`,内部所有窗口判断用 `keepTurns`
|
|
17
|
+
- REPL:恢复分支解析尾参(首参仍是会话目标,逻辑不变);`resumeWith(record, inputLine, keepTurns)`;`/compact` 分支解析首参数字;「无需压缩」提示与 oldTurnsCount 均按生效 N 计算
|
|
18
|
+
- 保留轮数**不持久化**到会话记录(会话记忆式方案已否)
|
|
19
|
+
|
|
20
|
+
## 非目标
|
|
21
|
+
|
|
22
|
+
- 不做 picker 内选轮数、不做配置文件项、不改摘要提示词
|
|
23
|
+
|
|
24
|
+
## 备选(已否)
|
|
25
|
+
|
|
26
|
+
- 会话记忆式(/compact 记住 N,resume 沿用):行为藏在数据里,换会话默认不同 — 用户选尾参式。
|
package/package.json
CHANGED
package/src/commands.ts
CHANGED
|
@@ -84,13 +84,13 @@ const commands: Command[] = [
|
|
|
84
84
|
},
|
|
85
85
|
{
|
|
86
86
|
name: 'resume',
|
|
87
|
-
description: 'Resume a saved conversation (/resume <序号|id|标题片段>)',
|
|
87
|
+
description: 'Resume a saved conversation (/resume <序号|id|标题片段> [N])',
|
|
88
88
|
// Intercepted by the REPL.
|
|
89
89
|
execute: () => ({ output: 'Resuming…' }),
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
92
|
name: 'compact',
|
|
93
|
-
description: '
|
|
93
|
+
description: '压缩当前会话:旧轮提炼为前情提要,默认保留最近 1 轮(/compact <N> 自定义)',
|
|
94
94
|
// Intercepted by the REPL.
|
|
95
95
|
execute: () => ({ output: 'Compacting…' }),
|
|
96
96
|
},
|
package/src/config.test.ts
CHANGED
|
@@ -47,3 +47,28 @@ describe('normalizeAutoUpdate', () => {
|
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
+
|
|
51
|
+
import { normalizeAutoCompact } from './config.js';
|
|
52
|
+
|
|
53
|
+
describe('normalizeAutoCompact', () => {
|
|
54
|
+
test('undefined → enabled default 200_000; false → disabled; valid override kept', () => {
|
|
55
|
+
expect(normalizeAutoCompact(undefined)).toEqual({ enabled: true, thresholdChars: 200_000, keepTurns: 1 });
|
|
56
|
+
expect(normalizeAutoCompact(false)).toEqual({ enabled: false, thresholdChars: 200_000, keepTurns: 1 });
|
|
57
|
+
expect(normalizeAutoCompact({ thresholdChars: 50_000 })).toEqual({
|
|
58
|
+
enabled: true,
|
|
59
|
+
thresholdChars: 50_000,
|
|
60
|
+
keepTurns: 1,
|
|
61
|
+
});
|
|
62
|
+
expect(normalizeAutoCompact({ enabled: false, thresholdChars: 80_000, keepTurns: 5 })).toEqual({
|
|
63
|
+
enabled: false,
|
|
64
|
+
thresholdChars: 80_000,
|
|
65
|
+
keepTurns: 5,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('garbage (string/null/array/negative) falls back to defaults', () => {
|
|
70
|
+
for (const raw of ['yes', 5, null, [1], { thresholdChars: -5 }, { thresholdChars: 'big' }, { keepTurns: 0 }, { keepTurns: 'ten' }]) {
|
|
71
|
+
expect(normalizeAutoCompact(raw)).toEqual({ enabled: true, thresholdChars: 200_000, keepTurns: 1 });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
package/src/config.ts
CHANGED
|
@@ -23,6 +23,8 @@ export interface TranscodeConfig {
|
|
|
23
23
|
routing?: Record<string, string>;
|
|
24
24
|
/** Auto-update on startup: `false` disables, an object configures it. */
|
|
25
25
|
autoUpdate?: boolean | AutoUpdateConfig;
|
|
26
|
+
/** Auto-compact on threshold: `false` disables, an object configures it. */
|
|
27
|
+
autoCompact?: boolean | AutoCompactConfig;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
/** Raw (on-disk) shape of the autoUpdate config block. */
|
|
@@ -65,6 +67,54 @@ export function normalizeAutoUpdate(raw: unknown): NormalizedAutoUpdate {
|
|
|
65
67
|
};
|
|
66
68
|
}
|
|
67
69
|
|
|
70
|
+
/** Raw (on-disk) shape of the autoCompact config block. */
|
|
71
|
+
export interface AutoCompactConfig {
|
|
72
|
+
enabled?: boolean;
|
|
73
|
+
/** Compact when the estimated context reaches this many chars. */
|
|
74
|
+
thresholdChars?: number;
|
|
75
|
+
/** Turns kept verbatim when auto-compacting (default 1). */
|
|
76
|
+
keepTurns?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Normalized form used by the REPL checkpoint. */
|
|
80
|
+
export interface NormalizedAutoCompact {
|
|
81
|
+
enabled: boolean;
|
|
82
|
+
thresholdChars: number;
|
|
83
|
+
keepTurns: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const DEFAULT_AUTO_COMPACT: NormalizedAutoCompact = {
|
|
87
|
+
enabled: true,
|
|
88
|
+
thresholdChars: 200_000,
|
|
89
|
+
keepTurns: 1,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Normalizes the raw autoCompact config value. Anything unrecognizable
|
|
94
|
+
* falls back to the enabled default rather than throwing — same contract
|
|
95
|
+
* as normalizeAutoUpdate.
|
|
96
|
+
*/
|
|
97
|
+
export function normalizeAutoCompact(raw: unknown): NormalizedAutoCompact {
|
|
98
|
+
if (raw === false) return { ...DEFAULT_AUTO_COMPACT, enabled: false };
|
|
99
|
+
if (raw === null || raw === undefined || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
100
|
+
return { ...DEFAULT_AUTO_COMPACT };
|
|
101
|
+
}
|
|
102
|
+
const obj = raw as AutoCompactConfig;
|
|
103
|
+
const threshold =
|
|
104
|
+
typeof obj.thresholdChars === 'number' && obj.thresholdChars > 0
|
|
105
|
+
? obj.thresholdChars
|
|
106
|
+
: DEFAULT_AUTO_COMPACT.thresholdChars;
|
|
107
|
+
const keepTurns =
|
|
108
|
+
typeof obj.keepTurns === 'number' && obj.keepTurns >= 1
|
|
109
|
+
? Math.floor(obj.keepTurns)
|
|
110
|
+
: DEFAULT_AUTO_COMPACT.keepTurns;
|
|
111
|
+
return {
|
|
112
|
+
enabled: typeof obj.enabled === 'boolean' ? obj.enabled : true,
|
|
113
|
+
thresholdChars: threshold,
|
|
114
|
+
keepTurns,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
68
118
|
// Overridable so tests never touch the user's real ~/.transcode.json.
|
|
69
119
|
const configPath = (): string =>
|
|
70
120
|
process.env.TRANSCODE_CONFIG_PATH ?? join(homedir(), '.transcode.json');
|
package/src/screens/REPL.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import { findCommand, getCommands, type CommandContext } from '../commands.js';
|
|
|
5
5
|
import {
|
|
6
6
|
getAvailableModels,
|
|
7
7
|
createModel,
|
|
8
|
+
getRouting,
|
|
8
9
|
setPreferredModel,
|
|
9
10
|
registerApiKey,
|
|
10
11
|
lookupApiKey,
|
|
@@ -16,7 +17,7 @@ import {
|
|
|
16
17
|
switchNote,
|
|
17
18
|
type ModelId,
|
|
18
19
|
} from '../provider/provider.js';
|
|
19
|
-
import { loadConfig } from '../config.js';
|
|
20
|
+
import { loadConfig, normalizeAutoCompact } from '../config.js';
|
|
20
21
|
import { runAgentLoop, type PermissionRequest } from '../agent/loop.js';
|
|
21
22
|
import { createToolSession } from '../agent/tools/index.js';
|
|
22
23
|
import { getPermissionMode } from '../agent/permission.js';
|
|
@@ -29,8 +30,8 @@ import {
|
|
|
29
30
|
searchSessions,
|
|
30
31
|
type SessionRecord,
|
|
31
32
|
} from '../sessions/store.js';
|
|
32
|
-
import { listProjectSessions, resolveResumeTarget } from '../sessions/resume.js';
|
|
33
|
-
import {
|
|
33
|
+
import { listProjectSessions, resolveResumeTarget, splitResumeArgs } from '../sessions/resume.js';
|
|
34
|
+
import { DEFAULT_KEEP_TURNS, compressSession, parseKeepCount, shouldAutoCompact, transcriptEntries, splitTurns } from '../sessions/compact.js';
|
|
34
35
|
import { walkFiles } from '../agent/tools/fs-utils.js';
|
|
35
36
|
import { applyCompletion, filterFiles, mentionQuery } from './file-completion.js';
|
|
36
37
|
import {
|
|
@@ -141,6 +142,9 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
141
142
|
const conversationTitleRef = useRef('');
|
|
142
143
|
// Compaction digest of turns older than the kept window (from resume//compact).
|
|
143
144
|
const summaryRef = useRef<string | null>(null);
|
|
145
|
+
// Auto-compact breaker state: consecutive failures + warned-once flag.
|
|
146
|
+
const autoCompactFailuresRef = useRef(0);
|
|
147
|
+
const autoCompactWarnedRef = useRef(false);
|
|
144
148
|
const lastSessionsRef = useRef<string[]>([]);
|
|
145
149
|
// Snapshot for the /resume arrow-key picker (set together with the phase).
|
|
146
150
|
const sessionPickerRef = useRef<SessionRecord[]>([]);
|
|
@@ -238,22 +242,25 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
238
242
|
}, []);
|
|
239
243
|
|
|
240
244
|
/** Shared resume flow: auto-compact over-long sessions, then load + render. */
|
|
241
|
-
const resumeWith = async (record: SessionRecord, inputLine: string) => {
|
|
245
|
+
const resumeWith = async (record: SessionRecord, inputLine: string, keepTurns = DEFAULT_KEEP_TURNS) => {
|
|
242
246
|
const { turns } = splitTurns(record.messages);
|
|
243
|
-
|
|
247
|
+
// Load first so the confirmation entry hosts the streamed digest.
|
|
248
|
+
applyResume(record, inputLine);
|
|
244
249
|
let oldTurns = 0;
|
|
245
|
-
if (turns.length >
|
|
250
|
+
if (turns.length > keepTurns && process.env.TRANSCODE_MOCK !== '1') {
|
|
246
251
|
setBusyBoth(true);
|
|
247
252
|
try {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
253
|
+
const outcome = await runCompaction(record, keepTurns);
|
|
254
|
+
if (outcome.compressed) {
|
|
255
|
+
oldTurns = turns.length - keepTurns;
|
|
256
|
+
summaryRef.current = outcome.record.summary ?? null;
|
|
257
|
+
messagesRef.current = outcome.record.messages;
|
|
258
|
+
saveSession(outcome.record);
|
|
259
|
+
// Manual success heals the auto-compact breaker too.
|
|
260
|
+
autoCompactFailuresRef.current = 0;
|
|
261
|
+
autoCompactWarnedRef.current = false;
|
|
262
|
+
} else {
|
|
263
|
+
addHistory({ input: '', output: `⚠ 提炼失败(${outcome.error ?? '未知'}),已恢复完整对话。` });
|
|
257
264
|
}
|
|
258
265
|
} catch {
|
|
259
266
|
addHistory({ input: '', output: '⚠ 提炼失败,已恢复完整对话。' });
|
|
@@ -261,7 +268,6 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
261
268
|
setBusyBoth(false);
|
|
262
269
|
}
|
|
263
270
|
}
|
|
264
|
-
applyResume(finalRecord, inputLine);
|
|
265
271
|
// Restore in the original conversation form: each message becomes a
|
|
266
272
|
// history entry (rule-framed questions, full-length replies).
|
|
267
273
|
for (const entry of transcriptEntries(summaryRef.current, messagesRef.current, oldTurns || undefined)) {
|
|
@@ -280,6 +286,35 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
280
286
|
});
|
|
281
287
|
}, []);
|
|
282
288
|
|
|
289
|
+
/**
|
|
290
|
+
* One shared compaction run for /resume and /compact: fast-model routing
|
|
291
|
+
* (routing.compact → fallback current model) with the digest streamed
|
|
292
|
+
* live into the chat area.
|
|
293
|
+
*/
|
|
294
|
+
const runCompaction = async (
|
|
295
|
+
record: SessionRecord,
|
|
296
|
+
keepTurns: number,
|
|
297
|
+
): Promise<{ compressed: boolean; record: SessionRecord; error?: string }> => {
|
|
298
|
+
appendChatOutput('\n正在提炼前情提要…\n');
|
|
299
|
+
try {
|
|
300
|
+
const outcome = await compressSession(
|
|
301
|
+
record,
|
|
302
|
+
createModel(getRouting().compact ?? ctx.modelId).model,
|
|
303
|
+
keepTurns,
|
|
304
|
+
(delta) => appendChatOutput(delta),
|
|
305
|
+
);
|
|
306
|
+
appendChatOutput('\n\n');
|
|
307
|
+
return outcome;
|
|
308
|
+
} catch (error) {
|
|
309
|
+
appendChatOutput('\n\n');
|
|
310
|
+
return {
|
|
311
|
+
compressed: false,
|
|
312
|
+
record,
|
|
313
|
+
error: error instanceof Error ? error.message : String(error),
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
283
318
|
const askInTerminal = useCallback(
|
|
284
319
|
(request: PermissionRequest, signal: AbortSignal): Promise<boolean> => {
|
|
285
320
|
const mode = getPermissionMode();
|
|
@@ -301,11 +336,65 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
301
336
|
[],
|
|
302
337
|
);
|
|
303
338
|
|
|
339
|
+
// Auto-compact (Claude Code style proactive): threshold check before each
|
|
340
|
+
// turn, same runCompaction as /compact and resume. Circuit breaker stops
|
|
341
|
+
// after 3 consecutive failures; auto failures warn once per session.
|
|
342
|
+
const autoCompactIfNeeded = async (): Promise<void> => {
|
|
343
|
+
const cfg = normalizeAutoCompact(loadConfig().autoCompact);
|
|
344
|
+
if (!cfg.enabled || !conversationRef.current) return;
|
|
345
|
+
if (process.env.TRANSCODE_MOCK === '1') return;
|
|
346
|
+
if (autoCompactFailuresRef.current >= 3) return;
|
|
347
|
+
if (!shouldAutoCompact(messagesRef.current, cfg)) return;
|
|
348
|
+
try {
|
|
349
|
+
// Dedicated host entry before the question echo, so the digest neither
|
|
350
|
+
// mixes into the turn entry nor receives the turn's streamed reply.
|
|
351
|
+
addHistory({ input: '', output: '' });
|
|
352
|
+
const record = buildSessionRecord({
|
|
353
|
+
id: conversationRef.current.id,
|
|
354
|
+
title: conversationTitleRef.current,
|
|
355
|
+
createdAt: conversationRef.current.createdAt,
|
|
356
|
+
cwd: process.cwd(),
|
|
357
|
+
model: ctx.modelId,
|
|
358
|
+
messages: messagesRef.current,
|
|
359
|
+
summary: summaryRef.current,
|
|
360
|
+
});
|
|
361
|
+
const outcome = await runCompaction(record, cfg.keepTurns);
|
|
362
|
+
if (!outcome.compressed) {
|
|
363
|
+
// Without an error it is a no-op (≤ keepTurns turns — e.g. one giant
|
|
364
|
+
// turn over threshold): nothing compressible, skip without touching
|
|
365
|
+
// the breaker.
|
|
366
|
+
if (outcome.error) throw new Error(outcome.error);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
summaryRef.current = outcome.record.summary ?? null;
|
|
370
|
+
messagesRef.current = outcome.record.messages;
|
|
371
|
+
saveSession(outcome.record);
|
|
372
|
+
autoCompactFailuresRef.current = 0;
|
|
373
|
+
autoCompactWarnedRef.current = false;
|
|
374
|
+
for (const entry of transcriptEntries(summaryRef.current, messagesRef.current)) {
|
|
375
|
+
addHistory(entry);
|
|
376
|
+
}
|
|
377
|
+
} catch (error) {
|
|
378
|
+
autoCompactFailuresRef.current += 1;
|
|
379
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
380
|
+
if (autoCompactFailuresRef.current >= 3) {
|
|
381
|
+
addHistory({
|
|
382
|
+
input: '',
|
|
383
|
+
output: `⚠ 自动压缩已暂停(连续 3 次失败,最后原因:${reason})。可 /compact 手动压缩或检查 routing.compact 配置。`,
|
|
384
|
+
});
|
|
385
|
+
} else if (!autoCompactWarnedRef.current) {
|
|
386
|
+
autoCompactWarnedRef.current = true;
|
|
387
|
+
addHistory({ input: '', output: `⚠ 自动压缩失败(${reason}),本轮继续。` });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
|
|
304
392
|
const runChat = useCallback(
|
|
305
393
|
async (text: string, display?: string, options?: { planMode?: boolean }) => {
|
|
306
394
|
setBusyBoth(true);
|
|
307
395
|
const controller = new AbortController();
|
|
308
396
|
abortRef.current = controller;
|
|
397
|
+
await autoCompactIfNeeded();
|
|
309
398
|
addHistory({ input: display ?? text, output: '' });
|
|
310
399
|
if (options?.planMode) {
|
|
311
400
|
appendChatOutput('[plan 模式:只读探索,产出方案,PLAN READY 后可让我执行]\n');
|
|
@@ -706,7 +795,9 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
706
795
|
}
|
|
707
796
|
|
|
708
797
|
if (cmdName === 'resume') {
|
|
709
|
-
const target = args
|
|
798
|
+
const { target, keepRaw } = splitResumeArgs(args);
|
|
799
|
+
const { keep, error } = parseKeepCount(keepRaw);
|
|
800
|
+
if (error) addHistory({ input: '', output: `⚠ ${error}` });
|
|
710
801
|
if (!target) {
|
|
711
802
|
// One-step resume: arrow-key picker over this project's sessions.
|
|
712
803
|
const projectSessions = listProjectSessions(process.cwd()).slice(0, 20);
|
|
@@ -723,7 +814,7 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
723
814
|
if (!record) {
|
|
724
815
|
addHistory({ input: trimmed, output: `未找到对话“${target}”。先 /sessions 查看列表。` });
|
|
725
816
|
} else {
|
|
726
|
-
await resumeWith(record, trimmed);
|
|
817
|
+
await resumeWith(record, trimmed, keep);
|
|
727
818
|
}
|
|
728
819
|
}
|
|
729
820
|
commitInput('');
|
|
@@ -745,9 +836,12 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
745
836
|
finishCompact();
|
|
746
837
|
return;
|
|
747
838
|
}
|
|
839
|
+
const { keep, error } = parseKeepCount(args[0]);
|
|
840
|
+
const keepN = keep ?? DEFAULT_KEEP_TURNS;
|
|
841
|
+
if (error) addHistory({ input: '', output: `⚠ ${error}` });
|
|
748
842
|
const { turns } = splitTurns(messagesRef.current);
|
|
749
|
-
if (turns.length <=
|
|
750
|
-
addHistory({ input: trimmed, output: `无需压缩:当前 ${turns.length} 轮 ≤ ${
|
|
843
|
+
if (turns.length <= keepN) {
|
|
844
|
+
addHistory({ input: trimmed, output: `无需压缩:当前 ${turns.length} 轮 ≤ ${keepN}。` });
|
|
751
845
|
finishCompact();
|
|
752
846
|
return;
|
|
753
847
|
}
|
|
@@ -762,25 +856,28 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
|
|
|
762
856
|
messages: messagesRef.current,
|
|
763
857
|
summary: summaryRef.current,
|
|
764
858
|
});
|
|
859
|
+
// Host entry first so the streamed digest renders inside this
|
|
860
|
+
// command's own frame (same contract as the resume path).
|
|
861
|
+
addHistory({ input: trimmed, output: '' });
|
|
765
862
|
const outcome = process.env.TRANSCODE_MOCK === '1'
|
|
766
863
|
? { compressed: false as const, record, error: 'mock 模式无模型' }
|
|
767
|
-
: await
|
|
864
|
+
: await runCompaction(record, keepN);
|
|
768
865
|
if (outcome.compressed) {
|
|
769
866
|
summaryRef.current = outcome.record.summary ?? null;
|
|
770
867
|
messagesRef.current = outcome.record.messages;
|
|
771
868
|
saveSession(outcome.record);
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
});
|
|
869
|
+
// Manual success heals the auto-compact breaker too.
|
|
870
|
+
autoCompactFailuresRef.current = 0;
|
|
871
|
+
autoCompactWarnedRef.current = false;
|
|
872
|
+
appendChatOutput(`已压缩:旧 ${turns.length - keepN} 轮提炼为前情提要,保留最近 ${keepN} 轮。`);
|
|
776
873
|
for (const entry of transcriptEntries(summaryRef.current, messagesRef.current)) {
|
|
777
874
|
addHistory(entry);
|
|
778
875
|
}
|
|
779
876
|
} else {
|
|
780
|
-
|
|
877
|
+
appendChatOutput(`⚠ 压缩失败(${outcome.error ?? '未知'}),会话保持原样。`);
|
|
781
878
|
}
|
|
782
879
|
} catch {
|
|
783
|
-
|
|
880
|
+
appendChatOutput('⚠ 压缩失败,会话保持原样。');
|
|
784
881
|
} finally {
|
|
785
882
|
setBusyBoth(false);
|
|
786
883
|
}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { describe, expect, test } from 'bun:test';
|
|
2
2
|
import { AIMessage, HumanMessage, SystemMessage, ToolMessage, type BaseMessage } from '@langchain/core/messages';
|
|
3
3
|
import type { SessionRecord } from './store.js';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
COMPACT_MAX_SUMMARY_CHARS,
|
|
6
|
+
DEFAULT_KEEP_TURNS,
|
|
7
|
+
TRANSCRIPT_LIMIT,
|
|
8
|
+
compressSession,
|
|
9
|
+
estimateTokens,
|
|
10
|
+
parseKeepCount,
|
|
11
|
+
shouldAutoCompact,
|
|
12
|
+
transcriptEntries,
|
|
13
|
+
splitTurns,
|
|
14
|
+
} from './compact.js';
|
|
5
15
|
|
|
6
16
|
function turn(n: number, text = 'x'): BaseMessage[] {
|
|
7
17
|
return [new HumanMessage(`问题${n}:${text}`), new AIMessage(`回答${n}:${text}`)];
|
|
@@ -24,13 +34,21 @@ function recordWith(turns: number, opts: { summary?: string; longTurn?: boolean
|
|
|
24
34
|
};
|
|
25
35
|
}
|
|
26
36
|
|
|
27
|
-
|
|
37
|
+
/** Streaming mock: yields chunks; can throw at chunk N and track pulls. */
|
|
38
|
+
function streamModel(
|
|
39
|
+
chunks: string[],
|
|
40
|
+
opts: { throwAt?: number; trackPulled?: string[] } = {},
|
|
41
|
+
): { stream: (input: string) => AsyncGenerator<{ content: string }>; prompts: string[] } {
|
|
28
42
|
const prompts: string[] = [];
|
|
29
43
|
return {
|
|
30
44
|
prompts,
|
|
31
|
-
|
|
45
|
+
stream: async function* (input: string) {
|
|
32
46
|
prompts.push(input);
|
|
33
|
-
|
|
47
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
48
|
+
opts.trackPulled?.push(chunks[i]!);
|
|
49
|
+
if (opts.throwAt === i + 1) throw new Error('stream blew up');
|
|
50
|
+
yield { content: chunks[i]! };
|
|
51
|
+
}
|
|
34
52
|
},
|
|
35
53
|
};
|
|
36
54
|
}
|
|
@@ -55,55 +73,130 @@ describe('splitTurns', () => {
|
|
|
55
73
|
});
|
|
56
74
|
|
|
57
75
|
describe('compressSession', () => {
|
|
58
|
-
test('
|
|
59
|
-
const
|
|
76
|
+
test('streams chunks into the summary and forwards each delta', async () => {
|
|
77
|
+
const deltas: string[] = [];
|
|
78
|
+
const { compressed, record } = await compressSession(
|
|
79
|
+
recordWith(3),
|
|
80
|
+
streamModel(['前情', '提要']),
|
|
81
|
+
1,
|
|
82
|
+
(d) => deltas.push(d),
|
|
83
|
+
);
|
|
60
84
|
expect(compressed).toBe(true);
|
|
61
|
-
expect(record.summary).toBe('
|
|
62
|
-
expect(
|
|
63
|
-
|
|
64
|
-
|
|
85
|
+
expect(record.summary).toBe('前情提要');
|
|
86
|
+
expect(deltas).toEqual(['前情', '提要']);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('caps summary at COMPACT_MAX_SUMMARY_CHARS and stops consuming early', async () => {
|
|
90
|
+
const pulled: string[] = [];
|
|
91
|
+
const model = streamModel(['x'.repeat(1000), 'y'.repeat(1000), 'z'.repeat(1000)], { trackPulled: pulled });
|
|
92
|
+
const { record } = await compressSession(recordWith(3), model, 1);
|
|
93
|
+
expect(record.summary!.length).toBeLessThanOrEqual(COMPACT_MAX_SUMMARY_CHARS);
|
|
94
|
+
expect(pulled.length).toBe(2); // 第三块未消费
|
|
65
95
|
});
|
|
66
96
|
|
|
67
|
-
test('
|
|
68
|
-
const rec = recordWith(
|
|
69
|
-
const { compressed, record } = await compressSession(rec,
|
|
97
|
+
test('mid-stream error falls back with error set', async () => {
|
|
98
|
+
const rec = recordWith(3);
|
|
99
|
+
const { compressed, record, error } = await compressSession(rec, streamModel(['前情', '提要'], { throwAt: 2 }), 1);
|
|
70
100
|
expect(compressed).toBe(false);
|
|
101
|
+
expect(error).toBeDefined();
|
|
71
102
|
expect(record).toBe(rec);
|
|
72
103
|
});
|
|
73
104
|
|
|
74
|
-
test('
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
105
|
+
test('non-string chunk content is skipped', async () => {
|
|
106
|
+
const model = {
|
|
107
|
+
stream: async function* () {
|
|
108
|
+
yield { content: '前情' };
|
|
109
|
+
yield { content: [{ text: 'x' }] };
|
|
110
|
+
yield { content: '提要' };
|
|
79
111
|
},
|
|
80
112
|
};
|
|
81
|
-
const {
|
|
113
|
+
const { record } = await compressSession(recordWith(3), model as never, 1);
|
|
114
|
+
expect(record.summary).toBe('前情提要');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('default keeps only 1 turn', async () => {
|
|
118
|
+
const { compressed, record } = await compressSession(recordWith(3), streamModel(['提要内容']));
|
|
119
|
+
expect(compressed).toBe(true);
|
|
120
|
+
expect(record.summary).toBe('提要内容');
|
|
121
|
+
expect(splitTurns(record.messages).turns.length).toBe(1);
|
|
122
|
+
expect(record.messages[0]!.getType()).toBe('system');
|
|
123
|
+
expect(record.updatedAt).toBeGreaterThan(1);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('explicit keepTurns=10 keeps 10', async () => {
|
|
127
|
+
const { record } = await compressSession(recordWith(12), streamModel(['提要']), 10);
|
|
128
|
+
expect(splitTurns(record.messages).turns.length).toBe(10);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('keepTurns below 1 clamps to 1', async () => {
|
|
132
|
+
const { record } = await compressSession(recordWith(3), streamModel(['提要']), 0);
|
|
133
|
+
expect(splitTurns(record.messages).turns.length).toBe(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('at or below keepTurns is a no-op returning the original record', async () => {
|
|
137
|
+
const rec = recordWith(10);
|
|
138
|
+
const { compressed, record } = await compressSession(rec, streamModel(['x']), 10);
|
|
82
139
|
expect(compressed).toBe(false);
|
|
83
|
-
expect(error).toBeDefined();
|
|
84
140
|
expect(record).toBe(rec);
|
|
85
141
|
});
|
|
86
142
|
|
|
87
|
-
test('empty
|
|
88
|
-
const { compressed, error } = await compressSession(recordWith(12),
|
|
143
|
+
test('empty summary is treated as failure', async () => {
|
|
144
|
+
const { compressed, error } = await compressSession(recordWith(12), streamModel([' ']), 10);
|
|
89
145
|
expect(compressed).toBe(false);
|
|
90
146
|
expect(error).toBeDefined();
|
|
91
147
|
});
|
|
92
148
|
|
|
93
149
|
test('rolling merge: previous summary appears in the prompt', async () => {
|
|
94
|
-
const model =
|
|
95
|
-
await compressSession(recordWith(12, { summary: '旧提要内容' }), model);
|
|
150
|
+
const model = streamModel(['新提要']);
|
|
151
|
+
await compressSession(recordWith(12, { summary: '旧提要内容' }), model, 10);
|
|
96
152
|
expect(model.prompts[0]).toContain('旧提要内容');
|
|
97
153
|
});
|
|
98
154
|
|
|
99
155
|
test('huge old transcript is truncated before invoking the model', async () => {
|
|
100
|
-
const model =
|
|
101
|
-
await compressSession(recordWith(12, { longTurn: true }), model);
|
|
156
|
+
const model = streamModel(['提要']);
|
|
157
|
+
await compressSession(recordWith(12, { longTurn: true }), model, 10);
|
|
102
158
|
expect(model.prompts[0]!.length).toBeLessThan(TRANSCRIPT_LIMIT + 2_000);
|
|
103
159
|
expect(model.prompts[0]).toContain('更早内容已截断');
|
|
104
160
|
});
|
|
105
161
|
});
|
|
106
162
|
|
|
163
|
+
describe('estimateTokens', () => {
|
|
164
|
+
test('sums content chars / 4, floors; empty → 0', () => {
|
|
165
|
+
expect(estimateTokens([])).toBe(0);
|
|
166
|
+
expect(estimateTokens([new HumanMessage('a'.repeat(40))])).toBe(10);
|
|
167
|
+
expect(
|
|
168
|
+
estimateTokens([new HumanMessage('x'.repeat(10)), new AIMessage('y'.repeat(30))]),
|
|
169
|
+
).toBe(10);
|
|
170
|
+
expect(estimateTokens([new HumanMessage('abcd')])).toBe(1);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('counts tool_calls args payloads (often the largest text)', () => {
|
|
174
|
+
const ai = new AIMessage({
|
|
175
|
+
content: '',
|
|
176
|
+
tool_calls: [{ id: 'c1', name: 'write_file', args: { content: 'x'.repeat(200) } }],
|
|
177
|
+
});
|
|
178
|
+
expect(estimateTokens([ai])).toBeGreaterThanOrEqual(50);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('shouldAutoCompact', () => {
|
|
183
|
+
test('compares in CHARS: at threshold triggers, just below does not', () => {
|
|
184
|
+
const cfg = { thresholdChars: 200 };
|
|
185
|
+
expect(shouldAutoCompact([new HumanMessage('a'.repeat(200))], cfg)).toBe(true);
|
|
186
|
+
expect(shouldAutoCompact([new HumanMessage('a'.repeat(196))], cfg)).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('parseKeepCount', () => {
|
|
191
|
+
test('undefined → default; "5" → 5; "0"/"abc" → error, no keep', () => {
|
|
192
|
+
expect(parseKeepCount(undefined)).toEqual({});
|
|
193
|
+
expect(parseKeepCount('5')).toEqual({ keep: 5 });
|
|
194
|
+
expect(parseKeepCount('0').keep).toBeUndefined();
|
|
195
|
+
expect(parseKeepCount('0').error).toBeDefined();
|
|
196
|
+
expect(parseKeepCount('abc').error).toBeDefined();
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
107
200
|
describe('transcriptEntries', () => {
|
|
108
201
|
test('digest first, then per-message entries: human→input, ai→output, full content, tool/system skipped', () => {
|
|
109
202
|
const messages: BaseMessage[] = [
|
package/src/sessions/compact.ts
CHANGED
|
@@ -1,15 +1,44 @@
|
|
|
1
1
|
import { AIMessage, type BaseMessage, type MessageContent } from '@langchain/core/messages';
|
|
2
2
|
import type { SessionRecord } from './store.js';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
export const
|
|
4
|
+
/** Default verbatim window kept on compaction; older turns get distilled. */
|
|
5
|
+
export const DEFAULT_KEEP_TURNS = 1;
|
|
6
6
|
|
|
7
7
|
/** Cap on the transcript fed to the distilling model (protects its context). */
|
|
8
8
|
export const TRANSCRIPT_LIMIT = 40_000;
|
|
9
9
|
|
|
10
|
-
/**
|
|
10
|
+
/** Cap on the distilled summary: stop consuming the stream past this. */
|
|
11
|
+
export const COMPACT_MAX_SUMMARY_CHARS = 1200;
|
|
12
|
+
|
|
13
|
+
/** Minimal streaming model surface needed for distillation (mock-friendly).
|
|
14
|
+
* LangChain's model.stream() returns Promise<IterableReadableStream>, plain
|
|
15
|
+
* async generators return the iterable directly — both are accepted. */
|
|
11
16
|
export interface CompactModel {
|
|
12
|
-
|
|
17
|
+
stream(
|
|
18
|
+
input: string,
|
|
19
|
+
): AsyncIterable<{ content: unknown }> | Promise<AsyncIterable<{ content: unknown }>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Rough token estimate for auto-compact: total content chars / 4.
|
|
23
|
+
* Tool-call args are included — in a coding session they are often the
|
|
24
|
+
* largest text (write_file/edit payloads). */
|
|
25
|
+
export function estimateTokens(messages: BaseMessage[]): number {
|
|
26
|
+
let chars = 0;
|
|
27
|
+
for (const message of messages) {
|
|
28
|
+
chars += contentText(message.content).length;
|
|
29
|
+
const calls = (message as AIMessage).tool_calls;
|
|
30
|
+
if (calls?.length) chars += JSON.stringify(calls).length;
|
|
31
|
+
}
|
|
32
|
+
return Math.floor(chars / 4);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Auto-compact decision. thresholdChars is in CHARS; estimateTokens is in
|
|
36
|
+
* tokens (chars/4) — convert back so units match. */
|
|
37
|
+
export function shouldAutoCompact(
|
|
38
|
+
messages: BaseMessage[],
|
|
39
|
+
cfg: { thresholdChars: number },
|
|
40
|
+
): boolean {
|
|
41
|
+
return estimateTokens(messages) * 4 >= cfg.thresholdChars;
|
|
13
42
|
}
|
|
14
43
|
|
|
15
44
|
/**
|
|
@@ -68,31 +97,47 @@ function buildPrompt(previousSummary: string | undefined, oldTurns: BaseMessage[
|
|
|
68
97
|
return `${head}${prior}\n对话历史:\n${renderTranscriptText(oldTurns)}`;
|
|
69
98
|
}
|
|
70
99
|
|
|
71
|
-
|
|
72
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Validate a raw keep-turns argument: undefined → default (no opinion),
|
|
102
|
+
* pure digits ≥1 → {keep}, anything else → {error} so callers can show a
|
|
103
|
+
* usage hint and proceed with the default.
|
|
104
|
+
*/
|
|
105
|
+
export function parseKeepCount(raw: string | undefined): { keep?: number; error?: string } {
|
|
106
|
+
if (raw === undefined) return {};
|
|
107
|
+
if (/^\d+$/.test(raw) && Number(raw) >= 1) return { keep: Number(raw) };
|
|
108
|
+
return { error: `保留轮数需为 ≥1 的整数,已按默认 ${DEFAULT_KEEP_TURNS} 轮执行` };
|
|
73
109
|
}
|
|
74
110
|
|
|
75
111
|
/**
|
|
76
112
|
* Distill turns older than the kept window into `summary`, returning a new
|
|
77
|
-
* record trimmed to [leading, last
|
|
78
|
-
* model call returns the original record unchanged (with
|
|
79
|
-
* failure) — callers fall back to a full restore.
|
|
113
|
+
* record trimmed to [leading, last keepTurns turns]. At or below the window,
|
|
114
|
+
* or on a failed model call, returns the original record unchanged (with
|
|
115
|
+
* `error` set on failure) — callers fall back to a full restore.
|
|
80
116
|
*/
|
|
81
117
|
export async function compressSession(
|
|
82
118
|
record: SessionRecord,
|
|
83
119
|
model: CompactModel,
|
|
120
|
+
keepTurns = DEFAULT_KEEP_TURNS,
|
|
121
|
+
onDelta?: (delta: string) => void,
|
|
84
122
|
): Promise<{ compressed: boolean; record: SessionRecord; error?: string }> {
|
|
123
|
+
const window = Math.max(1, keepTurns);
|
|
85
124
|
const { leading, turns } = splitTurns(record.messages);
|
|
86
|
-
if (turns.length <=
|
|
87
|
-
const keep = turns.slice(-
|
|
88
|
-
const old = turns.slice(0, turns.length -
|
|
89
|
-
let
|
|
125
|
+
if (turns.length <= window) return { compressed: false, record };
|
|
126
|
+
const keep = turns.slice(-window);
|
|
127
|
+
const old = turns.slice(0, turns.length - window);
|
|
128
|
+
let raw = '';
|
|
90
129
|
try {
|
|
91
|
-
const
|
|
92
|
-
|
|
130
|
+
const stream = await model.stream(buildPrompt(record.summary, old));
|
|
131
|
+
for await (const chunk of stream) {
|
|
132
|
+
const text = typeof chunk.content === 'string' ? chunk.content : '';
|
|
133
|
+
if (text) onDelta?.(text);
|
|
134
|
+
raw += text;
|
|
135
|
+
if (raw.length >= COMPACT_MAX_SUMMARY_CHARS) break;
|
|
136
|
+
}
|
|
93
137
|
} catch (error) {
|
|
94
138
|
return { compressed: false, record, error: error instanceof Error ? error.message : String(error) };
|
|
95
139
|
}
|
|
140
|
+
const summary = raw.slice(0, COMPACT_MAX_SUMMARY_CHARS).trim();
|
|
96
141
|
if (!summary) return { compressed: false, record, error: '模型返回了空前情提要' };
|
|
97
142
|
return {
|
|
98
143
|
compressed: true,
|
|
@@ -4,7 +4,7 @@ import { tmpdir } from 'node:os';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { HumanMessage } from '@langchain/core/messages';
|
|
6
6
|
import { saveSession, type SessionRecord } from './store.js';
|
|
7
|
-
import { listProjectSessions, resolveResumeTarget } from './resume.js';
|
|
7
|
+
import { listProjectSessions, resolveResumeTarget, splitResumeArgs } from './resume.js';
|
|
8
8
|
|
|
9
9
|
let dir: string;
|
|
10
10
|
beforeEach(() => {
|
|
@@ -90,3 +90,14 @@ describe('resolveResumeTarget', () => {
|
|
|
90
90
|
expect(resolveResumeTarget('不存在', [], list)).toBeUndefined();
|
|
91
91
|
});
|
|
92
92
|
});
|
|
93
|
+
|
|
94
|
+
describe('splitResumeArgs', () => {
|
|
95
|
+
test('second arg is keep-raw only when it is pure digits', () => {
|
|
96
|
+
expect(splitResumeArgs(['部署', '脚本'])).toEqual({ target: '部署 脚本' });
|
|
97
|
+
expect(splitResumeArgs(['3', '5'])).toEqual({ target: '3', keepRaw: '5' });
|
|
98
|
+
expect(splitResumeArgs(['3', '0'])).toEqual({ target: '3', keepRaw: '0' });
|
|
99
|
+
expect(splitResumeArgs(['3', 'abc'])).toEqual({ target: '3 abc' });
|
|
100
|
+
expect(splitResumeArgs(['3'])).toEqual({ target: '3' });
|
|
101
|
+
expect(splitResumeArgs([])).toEqual({ target: '' });
|
|
102
|
+
});
|
|
103
|
+
});
|
package/src/sessions/resume.ts
CHANGED
|
@@ -5,6 +5,19 @@ export function listProjectSessions(cwd: string): SessionRecord[] {
|
|
|
5
5
|
return listSessions().filter((session) => session.cwd === cwd);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Split /resume args: the second token is a keep-turns attempt ONLY when it
|
|
10
|
+
* is pure digits; otherwise all tokens join into the target (multi-word
|
|
11
|
+
* title fragments keep working).
|
|
12
|
+
*/
|
|
13
|
+
export function splitResumeArgs(args: string[]): { target: string; keepRaw?: string } {
|
|
14
|
+
const hasKeepArg = args.length > 1 && /^\d+$/.test(args[1]!);
|
|
15
|
+
return {
|
|
16
|
+
target: (hasKeepArg ? args[0] : args.join(' ')).trim(),
|
|
17
|
+
...(hasKeepArg ? { keepRaw: args[1] } : {}),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
8
21
|
/**
|
|
9
22
|
* Resolve a /resume argument to a session record.
|
|
10
23
|
* Blank target → undefined (caller opens the picker). Numeric → the last
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Auto-generated from package.json by scripts/sync-version.ts (npm version hook)
|
|
2
|
-
export const VERSION = '0.
|
|
2
|
+
export const VERSION = '0.9.0';
|