thatgfsj-code 1.0.2 → 2.2.9
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/.nwt/meta.json +5 -0
- package/CHANGELOG.md +244 -0
- package/DEVELOPMENT.md +286 -0
- package/README.md +23 -1
- package/dist/app/index.d.ts +14 -0
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +21 -5
- package/dist/app/index.js.map +1 -1
- package/dist/cmd/index.d.ts +21 -0
- package/dist/cmd/index.d.ts.map +1 -1
- package/dist/cmd/index.js +144 -12
- package/dist/cmd/index.js.map +1 -1
- package/dist/session/index.d.ts +11 -0
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +83 -1
- package/dist/session/index.js.map +1 -1
- package/dist/tools/nwt.d.ts +14 -4
- package/dist/tools/nwt.d.ts.map +1 -1
- package/dist/tools/nwt.js +160 -13
- package/dist/tools/nwt.js.map +1 -1
- package/dist/tui/components/ChatList.d.ts.map +1 -1
- package/dist/tui/components/ChatList.js +4 -3
- package/dist/tui/components/ChatList.js.map +1 -1
- package/dist/tui/components/Header.d.ts.map +1 -1
- package/dist/tui/components/Header.js +2 -1
- package/dist/tui/components/Header.js.map +1 -1
- package/dist/tui/components/ToolCall.d.ts +1 -1
- package/dist/tui/components/ToolCall.d.ts.map +1 -1
- package/dist/tui/components/ToolCall.js +9 -4
- package/dist/tui/components/ToolCall.js.map +1 -1
- package/dist/tui/hooks/useChat.d.ts.map +1 -1
- package/dist/tui/hooks/useChat.js +47 -6
- package/dist/tui/hooks/useChat.js.map +1 -1
- package/dist/tui/hooks/useCommands.d.ts.map +1 -1
- package/dist/tui/hooks/useCommands.js +21 -0
- package/dist/tui/hooks/useCommands.js.map +1 -1
- package/dist/tui/welcome.d.ts.map +1 -1
- package/dist/tui/welcome.js +3 -2
- package/dist/tui/welcome.js.map +1 -1
- package/dist/utils/thinking.d.ts +59 -0
- package/dist/utils/thinking.d.ts.map +1 -0
- package/dist/utils/thinking.js +107 -0
- package/dist/utils/thinking.js.map +1 -0
- package/dist/version.d.ts +16 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +16 -0
- package/dist/version.js.map +1 -0
- package/install.bat +63 -0
- package/install.ps1 +238 -0
- package/install.sh +113 -0
- package/package.json +5 -2
- package/src/app/index.ts +21 -5
- package/src/cmd/index.tsx +145 -13
- package/src/session/index.ts +82 -1
- package/src/tools/nwt.ts +181 -13
- package/src/tui/components/ChatList.tsx +15 -6
- package/src/tui/components/Header.tsx +2 -1
- package/src/tui/components/ToolCall.tsx +46 -10
- package/src/tui/hooks/useChat.ts +50 -6
- package/src/tui/hooks/useCommands.ts +22 -0
- package/src/tui/welcome.ts +3 -2
- package/src/utils/thinking.ts +122 -0
- package/src/version.ts +16 -0
package/dist/cmd/index.d.ts
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
3
|
* Thatgfsj Code - CLI Entry Point
|
|
4
|
+
*
|
|
5
|
+
* v2.2.4 (product 0.4.1): three concrete fixes for the bugs visible
|
|
6
|
+
* in the 2.2.3 (product 0.4.0) session log:
|
|
7
|
+
*
|
|
8
|
+
* 1. Encoding: chcp 65001 used to be wrapped in a silent try/catch,
|
|
9
|
+
* and stdout never had its default encoding set. On Windows this
|
|
10
|
+
* caused Chinese characters from tool output to render as
|
|
11
|
+
* mojibake (ļ ʹ ϵͳ Ƿ ...).
|
|
12
|
+
*
|
|
13
|
+
* 2. [已中断] hallucination loop: 1.0.4's SessionManager had no
|
|
14
|
+
* anti-pollution filter. When the user pressed Ctrl+C mid-
|
|
15
|
+
* stream, the truncated assistant message (containing the
|
|
16
|
+
* model's literal "[已中断]" marker) got persisted to history.
|
|
17
|
+
* On the next turn, the model saw that and kept echoing it.
|
|
18
|
+
* v2.1.0 had the right fix (_wasAborted flag + filter) — we
|
|
19
|
+
* port it back here.
|
|
20
|
+
*
|
|
21
|
+
* 3. Tool output visual: 1.0.4 printed `@@TOOL@@{...}` markers
|
|
22
|
+
* inline with text, no separator, no truncation. Long tool
|
|
23
|
+
* output (registry dumps, dir listings) bled into the next
|
|
24
|
+
* AI message. Add separator + truncation + color separation.
|
|
4
25
|
*/
|
|
5
26
|
export {};
|
|
6
27
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cmd/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cmd/index.tsx"],"names":[],"mappings":";AAEA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cmd/index.tsx"],"names":[],"mappings":";AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG"}
|
package/dist/cmd/index.js
CHANGED
|
@@ -2,17 +2,58 @@
|
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
/**
|
|
4
4
|
* Thatgfsj Code - CLI Entry Point
|
|
5
|
+
*
|
|
6
|
+
* v2.2.4 (product 0.4.1): three concrete fixes for the bugs visible
|
|
7
|
+
* in the 2.2.3 (product 0.4.0) session log:
|
|
8
|
+
*
|
|
9
|
+
* 1. Encoding: chcp 65001 used to be wrapped in a silent try/catch,
|
|
10
|
+
* and stdout never had its default encoding set. On Windows this
|
|
11
|
+
* caused Chinese characters from tool output to render as
|
|
12
|
+
* mojibake (ļ ʹ ϵͳ Ƿ ...).
|
|
13
|
+
*
|
|
14
|
+
* 2. [已中断] hallucination loop: 1.0.4's SessionManager had no
|
|
15
|
+
* anti-pollution filter. When the user pressed Ctrl+C mid-
|
|
16
|
+
* stream, the truncated assistant message (containing the
|
|
17
|
+
* model's literal "[已中断]" marker) got persisted to history.
|
|
18
|
+
* On the next turn, the model saw that and kept echoing it.
|
|
19
|
+
* v2.1.0 had the right fix (_wasAborted flag + filter) — we
|
|
20
|
+
* port it back here.
|
|
21
|
+
*
|
|
22
|
+
* 3. Tool output visual: 1.0.4 printed `@@TOOL@@{...}` markers
|
|
23
|
+
* inline with text, no separator, no truncation. Long tool
|
|
24
|
+
* output (registry dumps, dir listings) bled into the next
|
|
25
|
+
* AI message. Add separator + truncation + color separation.
|
|
5
26
|
*/
|
|
6
27
|
if (process.platform === 'win32') {
|
|
28
|
+
// Switch Windows console to UTF-8 BEFORE any other code runs.
|
|
29
|
+
// v2.2.3 wrapped this in `try { } catch {}` which silently swallowed
|
|
30
|
+
// failures (e.g. when running under a non-interactive shell where
|
|
31
|
+
// chcp is meaningless). Now we still try, but we also fall back to
|
|
32
|
+
// setting the codepage via the parent process's stdio handles.
|
|
7
33
|
try {
|
|
8
|
-
require('child_process').execSync('chcp 65001', { stdio: 'ignore', windowsHide: true });
|
|
34
|
+
require('child_process').execSync('chcp 65001 >NUL', { stdio: 'ignore', windowsHide: true });
|
|
9
35
|
}
|
|
10
|
-
catch {
|
|
36
|
+
catch {
|
|
37
|
+
// Non-Windows shell, or chcp unavailable (e.g. git-bash on CI) —
|
|
38
|
+
// that's fine, Node defaults to UTF-8 in that environment.
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Force stdout/stderr to UTF-8 regardless of platform. Without this,
|
|
42
|
+
// Node will emit GBK-encoded bytes for non-ASCII characters on
|
|
43
|
+
// Windows even after `chcp 65001`, because the underlying file
|
|
44
|
+
// descriptors still report a non-UTF-8 code page.
|
|
45
|
+
if (process.stdout.setDefaultEncoding) {
|
|
46
|
+
process.stdout.setDefaultEncoding('utf8');
|
|
47
|
+
}
|
|
48
|
+
if (process.stderr.setDefaultEncoding) {
|
|
49
|
+
process.stderr.setDefaultEncoding('utf8');
|
|
11
50
|
}
|
|
12
51
|
import { program } from 'commander';
|
|
13
52
|
import chalk from 'chalk';
|
|
14
53
|
import { App } from '../app/index.js';
|
|
15
54
|
import { WelcomeScreen } from '../tui/welcome.js';
|
|
55
|
+
import { compressThinking, summarizeThinking, splitThinking } from '../utils/thinking.js';
|
|
56
|
+
import { PRODUCT_VERSION } from '../version.js';
|
|
16
57
|
process.on('uncaughtException', (error) => {
|
|
17
58
|
console.error(chalk.red('\n Error:'), error.message);
|
|
18
59
|
process.exit(1);
|
|
@@ -24,10 +65,11 @@ process.on('unhandledRejection', (reason) => {
|
|
|
24
65
|
program
|
|
25
66
|
.name('gfcode')
|
|
26
67
|
.description('Thatgfsj Code - AI Coding Assistant')
|
|
27
|
-
.version(
|
|
68
|
+
.version(PRODUCT_VERSION)
|
|
28
69
|
.argument('[prompt]', 'Task to execute (omit to start interactive mode)')
|
|
29
70
|
.option('-m, --model <model>', 'Specify model')
|
|
30
71
|
.option('-i, --interactive', 'Force interactive mode')
|
|
72
|
+
.option('--show-thinking', 'Show full <think>...</think> reasoning blocks (default: compress to one-line summary)')
|
|
31
73
|
.action(async (prompt, options) => {
|
|
32
74
|
try {
|
|
33
75
|
const app = await App.create();
|
|
@@ -72,13 +114,34 @@ program
|
|
|
72
114
|
console.log();
|
|
73
115
|
app.session.addMessage('user', prompt);
|
|
74
116
|
let fullResponse = '';
|
|
117
|
+
// v2.2.4: track whether the stream was aborted so we don't
|
|
118
|
+
// persist a truncated assistant message (which is what caused
|
|
119
|
+
// the [已中断] hallucination loop in v2.2.3).
|
|
120
|
+
const abortCtrl = new AbortController();
|
|
121
|
+
const onSigInt = () => { abortCtrl.abort(); };
|
|
122
|
+
process.once('SIGINT', onSigInt);
|
|
123
|
+
// v2.2.5 (product 0.4.2): compress <think> blocks at render time
|
|
124
|
+
// AND before persisting to history. Without this, every assistant
|
|
125
|
+
// message balloons history with reasoning the user has already
|
|
126
|
+
// seen compressed, and context windows blow up fast.
|
|
127
|
+
const showThinking = !!options.showThinking;
|
|
75
128
|
try {
|
|
76
|
-
process.stdout.write(chalk.gray(' Thinking
|
|
129
|
+
process.stdout.write(chalk.gray(' Thinking...'));
|
|
77
130
|
const stream = app.streamResponse();
|
|
131
|
+
// v2.2.5: stream-time rendering stays simple — we just buffer
|
|
132
|
+
// the full response. Post-process at the end (compress
|
|
133
|
+
// thinking blocks once). This avoids a state machine for
|
|
134
|
+
// "are we inside <think> right now" mid-stream which would
|
|
135
|
+
// be flaky if the model splits the tag across chunks.
|
|
78
136
|
for await (const chunk of stream) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
//
|
|
137
|
+
if (abortCtrl.signal.aborted)
|
|
138
|
+
break;
|
|
139
|
+
// Clear the entire "Thinking..." line — must use enough
|
|
140
|
+
// spaces to overwrite any longer thinking indicator.
|
|
141
|
+
process.stdout.write('\r' + ' '.repeat(80) + '\r');
|
|
142
|
+
// Parse tool messages — these should NOT be compressed even
|
|
143
|
+
// when thinking is hidden, because they carry real signal
|
|
144
|
+
// (tool name + args + result).
|
|
82
145
|
if (chunk.includes('@@TOOL@@')) {
|
|
83
146
|
const parts = chunk.split('\n');
|
|
84
147
|
for (const part of parts) {
|
|
@@ -88,29 +151,95 @@ program
|
|
|
88
151
|
if (data.action === 'call') {
|
|
89
152
|
console.log();
|
|
90
153
|
console.log(chalk.cyan(` ⚙ ${data.name}: ${formatArgs(data.args)}`));
|
|
154
|
+
console.log(chalk.gray(' ' + '─'.repeat(40)));
|
|
91
155
|
}
|
|
92
156
|
else if (data.action === 'result') {
|
|
93
157
|
const output = data.output || data.error || '';
|
|
94
|
-
const lines = output.split('\n')
|
|
95
|
-
|
|
158
|
+
const lines = output.split('\n');
|
|
159
|
+
const MAX_TOOL_LINES = 20;
|
|
160
|
+
const truncated = lines.length > MAX_TOOL_LINES;
|
|
161
|
+
const visible = truncated ? lines.slice(0, MAX_TOOL_LINES) : lines;
|
|
162
|
+
for (const line of visible) {
|
|
96
163
|
console.log(chalk.gray(' │ ') + line);
|
|
97
164
|
}
|
|
165
|
+
if (truncated) {
|
|
166
|
+
console.log(chalk.gray(` │ ... (${lines.length - MAX_TOOL_LINES} more lines truncated)`));
|
|
167
|
+
}
|
|
168
|
+
console.log(chalk.gray(' ' + '─'.repeat(40)));
|
|
98
169
|
}
|
|
99
170
|
}
|
|
100
171
|
catch { }
|
|
101
172
|
}
|
|
102
173
|
else if (part) {
|
|
174
|
+
// Mid-stream text chunk — leave it raw. The post-
|
|
175
|
+
// process step below will compress any <think>
|
|
176
|
+
// blocks before printing them as the final rendered
|
|
177
|
+
// output. (We can't reliably strip mid-stream
|
|
178
|
+
// because the closing </think> might be in a later
|
|
179
|
+
// chunk.)
|
|
103
180
|
process.stdout.write(chalk.cyan(' │ ') + part);
|
|
104
181
|
}
|
|
105
182
|
}
|
|
106
183
|
}
|
|
107
184
|
else {
|
|
108
|
-
|
|
185
|
+
// Plain AI text chunk — buffer only. Same reasoning:
|
|
186
|
+
// post-process at end avoids mid-stream delimiter
|
|
187
|
+
// races.
|
|
188
|
+
fullResponse += chunk;
|
|
189
|
+
// Show the chunk as it streams, but we'll re-render the
|
|
190
|
+
// final compressed version below. To avoid double-
|
|
191
|
+
// printing, only echo when we're NOT going to post-
|
|
192
|
+
// process (i.e., when showThinking is true and we want
|
|
193
|
+
// to see everything live).
|
|
194
|
+
if (showThinking) {
|
|
195
|
+
process.stdout.write(chalk.cyan(' │ ') + chunk);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// v2.2.5: post-process. Strip <think> blocks, summarize,
|
|
200
|
+
// and print the cleaned conclusion if we suppressed it
|
|
201
|
+
// during streaming.
|
|
202
|
+
if (!showThinking && !abortCtrl.signal.aborted && fullResponse) {
|
|
203
|
+
const split = splitThinking(fullResponse);
|
|
204
|
+
if (split.thinking) {
|
|
205
|
+
// Clear what we already streamed (we suppressed text
|
|
206
|
+
// chunks during streaming, so there's nothing to clear
|
|
207
|
+
// unless showThinking was on; in that case the text
|
|
208
|
+
// already contains the <think> block live and there's
|
|
209
|
+
// no point double-printing).
|
|
210
|
+
const summary = summarizeThinking(split);
|
|
211
|
+
if (summary) {
|
|
212
|
+
console.log(chalk.gray(` ${summary}`));
|
|
213
|
+
}
|
|
214
|
+
if (split.conclusion) {
|
|
215
|
+
console.log(chalk.cyan(' │ ') + split.conclusion);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else if (fullResponse.trim()) {
|
|
219
|
+
// No thinking block found — print the conclusion if we
|
|
220
|
+
// hadn't already (showThinking=false suppresses
|
|
221
|
+
// streaming text).
|
|
222
|
+
console.log(chalk.cyan(' │ ') + fullResponse);
|
|
109
223
|
}
|
|
110
|
-
fullResponse += chunk;
|
|
111
224
|
}
|
|
112
225
|
console.log();
|
|
113
|
-
|
|
226
|
+
// v2.2.4: skip persistence on abort.
|
|
227
|
+
if (!abortCtrl.signal.aborted) {
|
|
228
|
+
// v2.2.5: persist only the conclusion (no <think> blocks)
|
|
229
|
+
// when compression is enabled. This keeps the conversation
|
|
230
|
+
// log readable AND keeps context-window usage low.
|
|
231
|
+
const toPersist = showThinking
|
|
232
|
+
? fullResponse
|
|
233
|
+
: compressThinking(fullResponse, false);
|
|
234
|
+
const accepted = app.session.addMessageSafe('assistant', toPersist);
|
|
235
|
+
if (!accepted) {
|
|
236
|
+
console.error(chalk.yellow(' ⚠️ Dropped assistant message containing [已中断] marker.\n' +
|
|
237
|
+
' (prevents hallucination loop — try your question again)'));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
console.log(chalk.yellow(' ⏹ Cancelled (response not saved)'));
|
|
242
|
+
}
|
|
114
243
|
}
|
|
115
244
|
catch (error) {
|
|
116
245
|
const msg = error.message || String(error);
|
|
@@ -130,6 +259,9 @@ program
|
|
|
130
259
|
console.error(chalk.red(`\n Error: ${msg}`));
|
|
131
260
|
}
|
|
132
261
|
}
|
|
262
|
+
finally {
|
|
263
|
+
process.removeListener('SIGINT', onSigInt);
|
|
264
|
+
}
|
|
133
265
|
}
|
|
134
266
|
}
|
|
135
267
|
catch (error) {
|
package/dist/cmd/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cmd/index.tsx"],"names":[],"mappings":";;AAEA
|
|
1
|
+
{"version":3,"file":"index.js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U,CAAC,IAAY;IAC9B,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC7B,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC;aACvB,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;aAC9G,IAAI,CAAC,GAAG,CAAC,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IAC7D,CAAC;AACH,CAAC"}
|
package/dist/session/index.d.ts
CHANGED
|
@@ -8,8 +8,19 @@ export declare class SessionManager {
|
|
|
8
8
|
private createdAt;
|
|
9
9
|
private compactor;
|
|
10
10
|
private maxMessages;
|
|
11
|
+
/** v2.2.4: counter for messages dropped by the pollution filter. */
|
|
12
|
+
private droppedCount;
|
|
11
13
|
constructor(maxMessages?: number);
|
|
12
14
|
addMessage(role: ChatMessage['role'], content: string, extras?: Partial<ChatMessage>): void;
|
|
15
|
+
/**
|
|
16
|
+
* v2.2.4 (port from v2.1.0): same as addMessage but returns false
|
|
17
|
+
* (and skips the push) if the message content matches a known
|
|
18
|
+
* truncation/abort pollution pattern. Use this for assistant
|
|
19
|
+
* messages whose stream might have been aborted.
|
|
20
|
+
*/
|
|
21
|
+
addMessageSafe(role: ChatMessage['role'], content: string, extras?: Partial<ChatMessage>): boolean;
|
|
22
|
+
/** v2.2.4: total messages dropped by addMessageSafe since session start. */
|
|
23
|
+
getDroppedCount(): number;
|
|
13
24
|
getMessages(): ChatMessage[];
|
|
14
25
|
getMessageCount(): number;
|
|
15
26
|
clear(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAsD/C,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAqB;IACrC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,SAAS,CAAO;IACxB,OAAO,CAAC,SAAS,CAAmB;IACpC,OAAO,CAAC,WAAW,CAAS;IAC5B,oEAAoE;IACpE,OAAO,CAAC,YAAY,CAAa;gBAErB,WAAW,SAAK;IAO5B,UAAU,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,IAAI;IAK3F;;;;;OAKG;IACH,cAAc,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,OAAO;IASlG,4EAA4E;IAC5E,eAAe,IAAI,MAAM;IAIzB,WAAW,IAAI,WAAW,EAAE;IAI5B,eAAe,IAAI,MAAM;IAIzB,KAAK,IAAI,IAAI;IAIb;;;OAGG;IACH,OAAO,CAAC,WAAW;IAoBnB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI;IAiBpC,KAAK,IAAI,MAAM;IAIf,OAAO,IAAI;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,IAAI,CAAA;KAAE;CAOjE;AAED,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACzE,cAAc,cAAc,CAAC"}
|
package/dist/session/index.js
CHANGED
|
@@ -2,12 +2,68 @@
|
|
|
2
2
|
* Session Manager - Manages conversation history with auto-compaction
|
|
3
3
|
*/
|
|
4
4
|
import { ContextCompactor } from './compactor.js';
|
|
5
|
+
/**
|
|
6
|
+
* v2.2.4 (port from v2.1.0): patterns that, if found in an assistant
|
|
7
|
+
* message, indicate the message is a truncated/aborted response that
|
|
8
|
+
* should NOT be persisted into history. Without this filter, the next
|
|
9
|
+
* turn's LLM sees the marker and starts echoing it back, creating a
|
|
10
|
+
* self-reinforcing "[已中断]" hallucination loop.
|
|
11
|
+
*
|
|
12
|
+
* v2.2.6.1 tightening (smoke-test driven): the design here is a
|
|
13
|
+
* two-tier check:
|
|
14
|
+
* 1. STRONG markers — unambiguous pollution, always drop:
|
|
15
|
+
* - `[已中断]` bracketed (the model emits this exact form)
|
|
16
|
+
* - `[interrupted]` bracketed English equivalent
|
|
17
|
+
* 2. WEAK markers — only drop if the message is also short (<200
|
|
18
|
+
* chars). Long messages mentioning "response truncated" are
|
|
19
|
+
* legitimate conversation (e.g. user complaints about past
|
|
20
|
+
* behavior), not pollution.
|
|
21
|
+
*
|
|
22
|
+
* Earlier drafts matched bare `已中断` substrings and "response
|
|
23
|
+
* truncated" anywhere in the message — both were too greedy and
|
|
24
|
+
* dropped legitimate Chinese/English conversation.
|
|
25
|
+
*/
|
|
26
|
+
const POLLUTION_STRONG = [
|
|
27
|
+
/\[已中断\]/,
|
|
28
|
+
/\[interrupted\]/i,
|
|
29
|
+
];
|
|
30
|
+
const POLLUTION_WEAK = [
|
|
31
|
+
/^\s*[\*#>\-`]*\s*\[已中断\]/m,
|
|
32
|
+
/^\s*[\*#>\-`]*\s*\[interrupted\]/im,
|
|
33
|
+
/\bresponse (was )?(truncated|cut off|interrupted)\b/i,
|
|
34
|
+
/\boutput (was )?(truncated|cut off|interrupted)\b/i,
|
|
35
|
+
];
|
|
36
|
+
function looksPolluted(content) {
|
|
37
|
+
if (!content)
|
|
38
|
+
return false;
|
|
39
|
+
// Common gates (apply to both tiers):
|
|
40
|
+
// - Length: model got cut off mid-stream → pollution is short.
|
|
41
|
+
// A long message with [已中断] in the middle is the model
|
|
42
|
+
// legitimately referencing the marker, not pollution.
|
|
43
|
+
// - Question: ends with '?' → user complaining about past behavior.
|
|
44
|
+
// - Temporal: contains "last time" / "earlier" / etc. → past tense.
|
|
45
|
+
if (content.length >= 200)
|
|
46
|
+
return false;
|
|
47
|
+
if (/\?\s*$/.test(content.trim()))
|
|
48
|
+
return false;
|
|
49
|
+
if (/\b(last time|earlier|before|previously|yesterday)\b/i.test(content))
|
|
50
|
+
return false;
|
|
51
|
+
// STRONG markers — bracketed truncation markers, unambiguous.
|
|
52
|
+
if (POLLUTION_STRONG.some(p => p.test(content)))
|
|
53
|
+
return true;
|
|
54
|
+
// WEAK markers — bare "response truncated" phrases.
|
|
55
|
+
if (POLLUTION_WEAK.some(p => p.test(content)))
|
|
56
|
+
return true;
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
5
59
|
export class SessionManager {
|
|
6
60
|
messages = [];
|
|
7
61
|
sessionId;
|
|
8
62
|
createdAt;
|
|
9
63
|
compactor;
|
|
10
64
|
maxMessages;
|
|
65
|
+
/** v2.2.4: counter for messages dropped by the pollution filter. */
|
|
66
|
+
droppedCount = 0;
|
|
11
67
|
constructor(maxMessages = 50) {
|
|
12
68
|
this.maxMessages = maxMessages;
|
|
13
69
|
this.sessionId = `session_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
@@ -18,6 +74,24 @@ export class SessionManager {
|
|
|
18
74
|
this.messages.push({ role, content, ...extras });
|
|
19
75
|
this.autoCompact();
|
|
20
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* v2.2.4 (port from v2.1.0): same as addMessage but returns false
|
|
79
|
+
* (and skips the push) if the message content matches a known
|
|
80
|
+
* truncation/abort pollution pattern. Use this for assistant
|
|
81
|
+
* messages whose stream might have been aborted.
|
|
82
|
+
*/
|
|
83
|
+
addMessageSafe(role, content, extras) {
|
|
84
|
+
if (role === 'assistant' && looksPolluted(content)) {
|
|
85
|
+
this.droppedCount++;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
this.addMessage(role, content, extras);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
/** v2.2.4: total messages dropped by addMessageSafe since session start. */
|
|
92
|
+
getDroppedCount() {
|
|
93
|
+
return this.droppedCount;
|
|
94
|
+
}
|
|
21
95
|
getMessages() {
|
|
22
96
|
return [...this.messages];
|
|
23
97
|
}
|
|
@@ -51,8 +125,16 @@ export class SessionManager {
|
|
|
51
125
|
}
|
|
52
126
|
truncate(maxMessages) {
|
|
53
127
|
if (maxMessages) {
|
|
128
|
+
// v2.2.7 edge fix: preserveRecent must be <= maxMessages - 1,
|
|
129
|
+
// otherwise the compactor's "always keep recent N" branch
|
|
130
|
+
// overrides the trim and no messages actually get dropped.
|
|
131
|
+
// Set preserveRecent to maxMessages-1 so anything beyond the
|
|
132
|
+
// last maxMessages-1 user/assistant msgs gets summarized.
|
|
54
133
|
this.maxMessages = maxMessages;
|
|
55
|
-
this.compactor = new ContextCompactor({
|
|
134
|
+
this.compactor = new ContextCompactor({
|
|
135
|
+
maxMessages,
|
|
136
|
+
preserveRecent: Math.max(1, maxMessages - 1),
|
|
137
|
+
});
|
|
56
138
|
}
|
|
57
139
|
const { compacted } = this.compactor.compact(this.messages);
|
|
58
140
|
this.messages = compacted;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/session/index.ts"],"names":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;YAChB,8DAA8D;YAC9D,0DAA0D;YAC1D,2DAA2D;YAC3D,6DAA6D;YAC7D,0DAA0D;YAC1D,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;YAC/B,IAAI,CAAC,SAAS,GAAG,IAAI,gBAAgB,CAAC;gBACpC,WAAW;gBACX,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,GAAG,CAAC,CAAC;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC5D,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,OAAO;QACL,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,SAAS;YAClB,YAAY,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM;YAClC,SAAS,EAAE,IAAI,CAAC,SAAS;SAC1B,CAAC;IACJ,CAAC;CACF;AAED,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,cAAc,cAAc,CAAC"}
|
package/dist/tools/nwt.d.ts
CHANGED
|
@@ -36,6 +36,10 @@ export declare class NwtTool implements Tool {
|
|
|
36
36
|
type: string;
|
|
37
37
|
description: string;
|
|
38
38
|
};
|
|
39
|
+
importance: {
|
|
40
|
+
type: string;
|
|
41
|
+
description: string;
|
|
42
|
+
};
|
|
39
43
|
query: {
|
|
40
44
|
type: string;
|
|
41
45
|
description: string;
|
|
@@ -44,6 +48,14 @@ export declare class NwtTool implements Tool {
|
|
|
44
48
|
type: string;
|
|
45
49
|
description: string;
|
|
46
50
|
};
|
|
51
|
+
from_id: {
|
|
52
|
+
type: string;
|
|
53
|
+
description: string;
|
|
54
|
+
};
|
|
55
|
+
to_id: {
|
|
56
|
+
type: string;
|
|
57
|
+
description: string;
|
|
58
|
+
};
|
|
47
59
|
};
|
|
48
60
|
required: string[];
|
|
49
61
|
};
|
|
@@ -64,11 +76,9 @@ export declare class NwtTool implements Tool {
|
|
|
64
76
|
private search;
|
|
65
77
|
private story;
|
|
66
78
|
private explain;
|
|
79
|
+
private diff;
|
|
80
|
+
private compact;
|
|
67
81
|
private archive;
|
|
68
|
-
/**
|
|
69
|
-
* Auto-archive events older than 30 days.
|
|
70
|
-
* Runs silently on init - no output unless something was archived.
|
|
71
|
-
*/
|
|
72
82
|
private autoArchiveIfNeeded;
|
|
73
83
|
private loadEvents;
|
|
74
84
|
}
|
package/dist/tools/nwt.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nwt.d.ts","sourceRoot":"","sources":["../../src/tools/nwt.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"nwt.d.ts","sourceRoot":"","sources":["../../src/tools/nwt.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAyBnD,qBAAa,OAAQ,YAAW,IAAI;IAClC,IAAI,SAAS;IACb,WAAW,SAgBoC;IAE/C,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAmBT;IAEF,QAAQ;qBAC4B,CAAC,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,SAAS,CAAC,EAAE;;MAE9E;IAEF,UAAU;;;;;QAYR;IAEI,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC;IAkC1E,OAAO,CAAC,IAAI;IAyBZ,OAAO,CAAC,GAAG;IAyDX,OAAO,CAAC,OAAO;IAoBf,OAAO,CAAC,MAAM;IA2Bd,OAAO,CAAC,KAAK;IAqDb,OAAO,CAAC,OAAO;IAsBf,OAAO,CAAC,IAAI;IAgDZ,OAAO,CAAC,OAAO;IAiFf,OAAO,CAAC,OAAO;IA2Df,OAAO,CAAC,mBAAmB;IA6D3B,OAAO,CAAC,UAAU;CAiBnB"}
|