thatgfsj-code 2.2.9 → 3.0.1
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/CHANGELOG.md +15 -67
- package/dist/app/index.d.ts +28 -5
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +76 -10
- package/dist/app/index.js.map +1 -1
- package/dist/cache/fingerprint.d.ts +49 -0
- package/dist/cache/fingerprint.d.ts.map +1 -0
- package/dist/cache/fingerprint.js +100 -0
- package/dist/cache/fingerprint.js.map +1 -0
- package/dist/cache/index.d.ts +15 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +15 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/smartModel.d.ts +40 -0
- package/dist/cache/smartModel.d.ts.map +1 -0
- package/dist/cache/smartModel.js +43 -0
- package/dist/cache/smartModel.js.map +1 -0
- package/dist/cache/stats.d.ts +93 -0
- package/dist/cache/stats.d.ts.map +1 -0
- package/dist/cache/stats.js +155 -0
- package/dist/cache/stats.js.map +1 -0
- package/dist/cache/volatile.d.ts +32 -0
- package/dist/cache/volatile.d.ts.map +1 -0
- package/dist/cache/volatile.js +44 -0
- package/dist/cache/volatile.js.map +1 -0
- package/dist/cmd/index.js +49 -57
- package/dist/cmd/index.js.map +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/types.d.ts +14 -0
- package/dist/config/types.d.ts.map +1 -1
- package/dist/llm/anthropic.d.ts +55 -5
- package/dist/llm/anthropic.d.ts.map +1 -1
- package/dist/llm/anthropic.js +172 -26
- package/dist/llm/anthropic.js.map +1 -1
- package/dist/llm/index.d.ts +26 -8
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +83 -15
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/openai.d.ts +28 -8
- package/dist/llm/openai.d.ts.map +1 -1
- package/dist/llm/openai.js +58 -8
- package/dist/llm/openai.js.map +1 -1
- package/dist/llm/provider.d.ts +40 -8
- package/dist/llm/provider.d.ts.map +1 -1
- package/dist/llm/provider.js +8 -1
- package/dist/llm/provider.js.map +1 -1
- package/dist/prompts/index.d.ts +32 -0
- package/dist/prompts/index.d.ts.map +1 -1
- package/dist/prompts/index.js +33 -11
- package/dist/prompts/index.js.map +1 -1
- package/dist/session/index.d.ts +32 -3
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +51 -36
- package/dist/session/index.js.map +1 -1
- package/dist/skills/brainstorming.js +33 -33
- package/dist/skills/code-review.js +35 -35
- package/dist/skills/executing-plans.js +18 -18
- package/dist/skills/frontend-design.js +25 -25
- package/dist/skills/git-workflow.js +26 -26
- package/dist/skills/improve-architecture.js +28 -28
- package/dist/skills/neuroweave.js +37 -37
- package/dist/skills/playwright.js +62 -62
- package/dist/skills/prototype.js +20 -20
- package/dist/skills/subagent.js +19 -19
- package/dist/skills/supabase.js +34 -34
- package/dist/skills/systematic-debugging.js +34 -34
- package/dist/skills/tdd.js +29 -29
- package/dist/skills/triage.js +25 -25
- package/dist/skills/verification.js +22 -22
- package/dist/skills/writing-plans.js +32 -32
- package/dist/tools/nwt.js +16 -16
- package/dist/tui/app.d.ts.map +1 -1
- package/dist/tui/app.js +9 -2
- package/dist/tui/app.js.map +1 -1
- package/dist/tui/components/Header.d.ts +8 -0
- package/dist/tui/components/Header.d.ts.map +1 -1
- package/dist/tui/components/Header.js +17 -4
- package/dist/tui/components/Header.js.map +1 -1
- package/dist/tui/components/InitWizard.d.ts +12 -1
- package/dist/tui/components/InitWizard.d.ts.map +1 -1
- package/dist/tui/components/InitWizard.js +58 -3
- package/dist/tui/components/InitWizard.js.map +1 -1
- package/dist/tui/components/ModelSelector.d.ts +3 -1
- package/dist/tui/components/ModelSelector.d.ts.map +1 -1
- package/dist/tui/components/ModelSelector.js +24 -7
- package/dist/tui/components/ModelSelector.js.map +1 -1
- package/dist/tui/components/ToolCall.d.ts.map +1 -1
- package/dist/tui/components/ToolCall.js +1 -6
- package/dist/tui/components/ToolCall.js.map +1 -1
- package/dist/tui/components/UserInput.d.ts.map +1 -1
- package/dist/tui/components/UserInput.js +20 -0
- package/dist/tui/components/UserInput.js.map +1 -1
- package/dist/tui/hooks/useChat.d.ts +22 -0
- package/dist/tui/hooks/useChat.d.ts.map +1 -1
- package/dist/tui/hooks/useChat.js +82 -46
- 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 +57 -0
- package/dist/tui/hooks/useCommands.js.map +1 -1
- package/dist/tui/welcome.d.ts.map +1 -1
- package/dist/tui/welcome.js +2 -3
- package/dist/tui/welcome.js.map +1 -1
- package/dist/types.d.ts +79 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -1
- package/dist/utils/stableStringify.d.ts +21 -0
- package/dist/utils/stableStringify.d.ts.map +1 -0
- package/dist/utils/stableStringify.js +53 -0
- package/dist/utils/stableStringify.js.map +1 -0
- package/dist/utils/thinking.d.ts.map +1 -1
- package/dist/utils/thinking.js +0 -3
- package/dist/utils/thinking.js.map +1 -1
- package/package.json +7 -6
- package/src/app/index.ts +189 -124
- package/src/cache/fingerprint.ts +101 -0
- package/src/cache/index.ts +15 -0
- package/src/cache/smartModel.ts +52 -0
- package/src/cache/stats.ts +199 -0
- package/src/cache/volatile.ts +47 -0
- package/src/cmd/index.tsx +288 -292
- package/src/config/index.ts +156 -148
- package/src/config/providers.ts +234 -234
- package/src/config/types.ts +67 -53
- package/src/hooks/index.ts +111 -111
- package/src/llm/anthropic.ts +388 -243
- package/src/llm/gemini.ts +169 -169
- package/src/llm/index.ts +265 -200
- package/src/llm/openai.ts +243 -196
- package/src/llm/provider.ts +66 -34
- package/src/prompts/index.ts +260 -220
- package/src/session/compactor.ts +103 -103
- package/src/session/index.ts +181 -168
- package/src/session/message.ts +42 -42
- package/src/skills/brainstorming.ts +43 -43
- package/src/skills/code-review.ts +45 -45
- package/src/skills/executing-plans.ts +27 -27
- package/src/skills/frontend-design.ts +35 -35
- package/src/skills/git-workflow.ts +36 -36
- package/src/skills/improve-architecture.ts +38 -38
- package/src/skills/index.ts +136 -136
- package/src/skills/neuroweave.ts +47 -47
- package/src/skills/playwright.ts +72 -72
- package/src/skills/prototype.ts +30 -30
- package/src/skills/subagent.ts +28 -28
- package/src/skills/supabase.ts +44 -44
- package/src/skills/systematic-debugging.ts +44 -44
- package/src/skills/tdd.ts +39 -39
- package/src/skills/triage.ts +35 -35
- package/src/skills/verification.ts +31 -31
- package/src/skills/writing-plans.ts +42 -42
- package/src/tools/nwt.ts +598 -598
- package/src/tools/types.ts +122 -122
- package/src/tui/app.tsx +200 -186
- package/src/tui/components/ChatList.tsx +41 -41
- package/src/tui/components/ChatMessage.tsx +54 -54
- package/src/tui/components/Header.tsx +56 -29
- package/src/tui/components/InitWizard.tsx +217 -132
- package/src/tui/components/Markdown.tsx +35 -35
- package/src/tui/components/ModelSelector.tsx +107 -87
- package/src/tui/components/StatusBar.tsx +30 -30
- package/src/tui/components/Thinking.tsx +17 -17
- package/src/tui/components/ToolCall.tsx +102 -107
- package/src/tui/components/UserInput.tsx +151 -131
- package/src/tui/hooks/useChat.ts +287 -238
- package/src/tui/hooks/useCommands.ts +234 -176
- package/src/tui/index.ts +6 -6
- package/src/tui/welcome.ts +177 -178
- package/src/types.ts +104 -42
- package/src/utils/diff.ts +71 -71
- package/src/utils/project.ts +99 -99
- package/src/utils/stableStringify.ts +47 -0
- package/src/utils/thinking.ts +118 -121
- package/tests/cache/fingerprint.test.ts +75 -0
- package/tests/cache/providerCatalog.test.ts +31 -0
- package/tests/cache/stableStringify.test.ts +43 -0
- package/tests/cache/stats.test.ts +146 -0
- package/tests/cache/volatile.test.ts +75 -0
- package/tests/smoke-pollution.mjs +78 -0
- package/tests/smoke-thinking.mjs +110 -0
- package/tests/smoke-tool-stream.mjs +69 -0
- package/tests/smoke-tool.mjs +117 -0
- package/.nwt/meta.json +0 -5
- package/dist/version.d.ts +0 -16
- package/dist/version.d.ts.map +0 -1
- package/dist/version.js +0 -16
- package/dist/version.js.map +0 -1
- package/src/version.ts +0 -16
package/src/tools/types.ts
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tool interface definitions
|
|
3
|
-
* Extracted from old src/core/types.ts
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// ==================== Tool Parameter & Schema ====================
|
|
7
|
-
|
|
8
|
-
export interface ToolParameter {
|
|
9
|
-
name: string;
|
|
10
|
-
type: string;
|
|
11
|
-
description: string;
|
|
12
|
-
required: boolean;
|
|
13
|
-
default?: any;
|
|
14
|
-
enum?: string[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface ToolInputSchema {
|
|
18
|
-
type: 'object';
|
|
19
|
-
properties: Record<string, {
|
|
20
|
-
type: string;
|
|
21
|
-
description: string;
|
|
22
|
-
default?: any;
|
|
23
|
-
enum?: string[];
|
|
24
|
-
}>;
|
|
25
|
-
required?: string[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface ToolMetadata {
|
|
29
|
-
permissions?: ('read' | 'write' | 'execute' | 'network')[];
|
|
30
|
-
maxDuration?: number;
|
|
31
|
-
tags?: string[];
|
|
32
|
-
version?: string;
|
|
33
|
-
deprecated?: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// ==================== Tool Interface ====================
|
|
37
|
-
|
|
38
|
-
export interface ToolContext {
|
|
39
|
-
sessionId?: string;
|
|
40
|
-
workingDirectory?: string;
|
|
41
|
-
confirmAction?: (msg: string) => Promise<boolean>;
|
|
42
|
-
signal?: AbortSignal;
|
|
43
|
-
toolCallId?: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface ToolResult {
|
|
47
|
-
success: boolean;
|
|
48
|
-
output?: string;
|
|
49
|
-
error?: string;
|
|
50
|
-
data?: any;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface Tool {
|
|
54
|
-
name: string;
|
|
55
|
-
description: string;
|
|
56
|
-
parameters: ToolParameter[];
|
|
57
|
-
inputSchema?: ToolInputSchema;
|
|
58
|
-
metadata?: ToolMetadata;
|
|
59
|
-
execute(params: Record<string, any>, ctx?: ToolContext): Promise<ToolResult>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// ==================== Tool Builder ====================
|
|
63
|
-
|
|
64
|
-
export class ToolBuilder {
|
|
65
|
-
private _name = '';
|
|
66
|
-
private _description = '';
|
|
67
|
-
private _parameters: ToolParameter[] = [];
|
|
68
|
-
private _inputSchema: ToolInputSchema = { type: 'object', properties: {} };
|
|
69
|
-
private _metadata: ToolMetadata = {};
|
|
70
|
-
private _fn?: (params: Record<string, any>, ctx: ToolContext) => Promise<ToolResult>;
|
|
71
|
-
|
|
72
|
-
name(n: string): this { this._name = n; return this; }
|
|
73
|
-
description(d: string): this { this._description = d; return this; }
|
|
74
|
-
|
|
75
|
-
param(p: ToolParameter): this {
|
|
76
|
-
this._parameters.push(p);
|
|
77
|
-
this._inputSchema.properties[p.name] = {
|
|
78
|
-
type: p.type,
|
|
79
|
-
description: p.description,
|
|
80
|
-
default: p.default,
|
|
81
|
-
enum: p.enum,
|
|
82
|
-
};
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
stringParam(name: string, description: string, required = true, defaultVal?: string): this {
|
|
87
|
-
return this.param({ name, type: 'string', description, required, default: defaultVal });
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
booleanParam(name: string, description: string, required = false, defaultVal?: boolean): this {
|
|
91
|
-
return this.param({ name, type: 'boolean', description, required, default: defaultVal });
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
permissions(...perms: ('read' | 'write' | 'execute' | 'network')[]): this {
|
|
95
|
-
this._metadata.permissions = perms;
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
tags(...t: string[]): this {
|
|
100
|
-
this._metadata.tags = t;
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
handle(fn: (params: Record<string, any>, ctx: ToolContext) => Promise<ToolResult>): this {
|
|
105
|
-
this._fn = fn;
|
|
106
|
-
return this;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
build(): Tool {
|
|
110
|
-
if (!this._name || !this._fn) {
|
|
111
|
-
throw new Error('Tool name and handler are required');
|
|
112
|
-
}
|
|
113
|
-
return {
|
|
114
|
-
name: this._name,
|
|
115
|
-
description: this._description,
|
|
116
|
-
parameters: this._parameters,
|
|
117
|
-
inputSchema: this._inputSchema,
|
|
118
|
-
metadata: this._metadata,
|
|
119
|
-
execute: this._fn,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Tool interface definitions
|
|
3
|
+
* Extracted from old src/core/types.ts
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ==================== Tool Parameter & Schema ====================
|
|
7
|
+
|
|
8
|
+
export interface ToolParameter {
|
|
9
|
+
name: string;
|
|
10
|
+
type: string;
|
|
11
|
+
description: string;
|
|
12
|
+
required: boolean;
|
|
13
|
+
default?: any;
|
|
14
|
+
enum?: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ToolInputSchema {
|
|
18
|
+
type: 'object';
|
|
19
|
+
properties: Record<string, {
|
|
20
|
+
type: string;
|
|
21
|
+
description: string;
|
|
22
|
+
default?: any;
|
|
23
|
+
enum?: string[];
|
|
24
|
+
}>;
|
|
25
|
+
required?: string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ToolMetadata {
|
|
29
|
+
permissions?: ('read' | 'write' | 'execute' | 'network')[];
|
|
30
|
+
maxDuration?: number;
|
|
31
|
+
tags?: string[];
|
|
32
|
+
version?: string;
|
|
33
|
+
deprecated?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ==================== Tool Interface ====================
|
|
37
|
+
|
|
38
|
+
export interface ToolContext {
|
|
39
|
+
sessionId?: string;
|
|
40
|
+
workingDirectory?: string;
|
|
41
|
+
confirmAction?: (msg: string) => Promise<boolean>;
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
toolCallId?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ToolResult {
|
|
47
|
+
success: boolean;
|
|
48
|
+
output?: string;
|
|
49
|
+
error?: string;
|
|
50
|
+
data?: any;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface Tool {
|
|
54
|
+
name: string;
|
|
55
|
+
description: string;
|
|
56
|
+
parameters: ToolParameter[];
|
|
57
|
+
inputSchema?: ToolInputSchema;
|
|
58
|
+
metadata?: ToolMetadata;
|
|
59
|
+
execute(params: Record<string, any>, ctx?: ToolContext): Promise<ToolResult>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ==================== Tool Builder ====================
|
|
63
|
+
|
|
64
|
+
export class ToolBuilder {
|
|
65
|
+
private _name = '';
|
|
66
|
+
private _description = '';
|
|
67
|
+
private _parameters: ToolParameter[] = [];
|
|
68
|
+
private _inputSchema: ToolInputSchema = { type: 'object', properties: {} };
|
|
69
|
+
private _metadata: ToolMetadata = {};
|
|
70
|
+
private _fn?: (params: Record<string, any>, ctx: ToolContext) => Promise<ToolResult>;
|
|
71
|
+
|
|
72
|
+
name(n: string): this { this._name = n; return this; }
|
|
73
|
+
description(d: string): this { this._description = d; return this; }
|
|
74
|
+
|
|
75
|
+
param(p: ToolParameter): this {
|
|
76
|
+
this._parameters.push(p);
|
|
77
|
+
this._inputSchema.properties[p.name] = {
|
|
78
|
+
type: p.type,
|
|
79
|
+
description: p.description,
|
|
80
|
+
default: p.default,
|
|
81
|
+
enum: p.enum,
|
|
82
|
+
};
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
stringParam(name: string, description: string, required = true, defaultVal?: string): this {
|
|
87
|
+
return this.param({ name, type: 'string', description, required, default: defaultVal });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
booleanParam(name: string, description: string, required = false, defaultVal?: boolean): this {
|
|
91
|
+
return this.param({ name, type: 'boolean', description, required, default: defaultVal });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
permissions(...perms: ('read' | 'write' | 'execute' | 'network')[]): this {
|
|
95
|
+
this._metadata.permissions = perms;
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
tags(...t: string[]): this {
|
|
100
|
+
this._metadata.tags = t;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
handle(fn: (params: Record<string, any>, ctx: ToolContext) => Promise<ToolResult>): this {
|
|
105
|
+
this._fn = fn;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
build(): Tool {
|
|
110
|
+
if (!this._name || !this._fn) {
|
|
111
|
+
throw new Error('Tool name and handler are required');
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
name: this._name,
|
|
115
|
+
description: this._description,
|
|
116
|
+
parameters: this._parameters,
|
|
117
|
+
inputSchema: this._inputSchema,
|
|
118
|
+
metadata: this._metadata,
|
|
119
|
+
execute: this._fn,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/tui/app.tsx
CHANGED
|
@@ -1,186 +1,200 @@
|
|
|
1
|
-
/** @jsxImportSource react */
|
|
2
|
-
import React, { useState, useCallback } from 'react';
|
|
3
|
-
import { Box, Text, useStdout } from 'ink';
|
|
4
|
-
import { Header } from './components/Header.js';
|
|
5
|
-
import { ChatList } from './components/ChatList.js';
|
|
6
|
-
import { Thinking } from './components/Thinking.js';
|
|
7
|
-
import { UserInput } from './components/UserInput.js';
|
|
8
|
-
import { StatusBar } from './components/StatusBar.js';
|
|
9
|
-
import { ModelSelector } from './components/ModelSelector.js';
|
|
10
|
-
import { InitWizard } from './components/InitWizard.js';
|
|
11
|
-
import { useChat } from './hooks/useChat.js';
|
|
12
|
-
import { useCommands } from './hooks/useCommands.js';
|
|
13
|
-
import type { App } from '../app/index.js';
|
|
14
|
-
import type { MessageData } from './components/ChatMessage.js';
|
|
15
|
-
import type { ProviderName } from '../config/types.js';
|
|
16
|
-
import { writeFileSync, readFileSync, existsSync, mkdirSync } from 'fs';
|
|
17
|
-
import { join } from 'path';
|
|
18
|
-
import { homedir } from 'os';
|
|
19
|
-
|
|
20
|
-
interface Props {
|
|
21
|
-
app: App;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function saveModelToHistory(model: string) {
|
|
25
|
-
const dir = join(homedir(), '.thatgfsj');
|
|
26
|
-
const path = join(dir, 'models.json');
|
|
27
|
-
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
28
|
-
let history: string[] = [];
|
|
29
|
-
if (existsSync(path)) {
|
|
30
|
-
try { history = JSON.parse(readFileSync(path, 'utf-8')); } catch {}
|
|
31
|
-
}
|
|
32
|
-
if (!history.includes(model)) {
|
|
33
|
-
history.push(model);
|
|
34
|
-
writeFileSync(path, JSON.stringify(history, null, 2));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type ViewMode = 'chat' | 'model_select' | 'init_wizard' | 'init_key' | 'init_url' | 'init_custom_model';
|
|
39
|
-
|
|
40
|
-
export function TuiApp({ app }: Props) {
|
|
41
|
-
const { messages, isThinking, streaming, streamingToolCalls, queuedMessage, sendMessage, cancel } = useChat(app);
|
|
42
|
-
const { handleCommand } = useCommands(app);
|
|
43
|
-
const [systemMessages, setSystemMessages] = useState<MessageData[]>([]);
|
|
44
|
-
const [viewMode, setViewMode] = useState<ViewMode>('chat');
|
|
45
|
-
const [initProvider, setInitProvider] = useState<ProviderName>('siliconflow');
|
|
46
|
-
const [initUrl, setInitUrl] = useState('');
|
|
47
|
-
const { stdout } = useStdout();
|
|
48
|
-
const terminalWidth = stdout?.columns || 80;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (result.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
{
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
)}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
1
|
+
/** @jsxImportSource react */
|
|
2
|
+
import React, { useState, useCallback, useEffect } from 'react';
|
|
3
|
+
import { Box, Text, useStdout } from 'ink';
|
|
4
|
+
import { Header } from './components/Header.js';
|
|
5
|
+
import { ChatList } from './components/ChatList.js';
|
|
6
|
+
import { Thinking } from './components/Thinking.js';
|
|
7
|
+
import { UserInput } from './components/UserInput.js';
|
|
8
|
+
import { StatusBar } from './components/StatusBar.js';
|
|
9
|
+
import { ModelSelector } from './components/ModelSelector.js';
|
|
10
|
+
import { InitWizard } from './components/InitWizard.js';
|
|
11
|
+
import { useChat } from './hooks/useChat.js';
|
|
12
|
+
import { useCommands } from './hooks/useCommands.js';
|
|
13
|
+
import type { App } from '../app/index.js';
|
|
14
|
+
import type { MessageData } from './components/ChatMessage.js';
|
|
15
|
+
import type { ProviderName } from '../config/types.js';
|
|
16
|
+
import { writeFileSync, readFileSync, existsSync, mkdirSync } from 'fs';
|
|
17
|
+
import { join } from 'path';
|
|
18
|
+
import { homedir } from 'os';
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
app: App;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function saveModelToHistory(model: string) {
|
|
25
|
+
const dir = join(homedir(), '.thatgfsj');
|
|
26
|
+
const path = join(dir, 'models.json');
|
|
27
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
28
|
+
let history: string[] = [];
|
|
29
|
+
if (existsSync(path)) {
|
|
30
|
+
try { history = JSON.parse(readFileSync(path, 'utf-8')); } catch {}
|
|
31
|
+
}
|
|
32
|
+
if (!history.includes(model)) {
|
|
33
|
+
history.push(model);
|
|
34
|
+
writeFileSync(path, JSON.stringify(history, null, 2));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type ViewMode = 'chat' | 'model_select' | 'init_wizard' | 'init_key' | 'init_url' | 'init_custom_model';
|
|
39
|
+
|
|
40
|
+
export function TuiApp({ app }: Props) {
|
|
41
|
+
const { messages, isThinking, streaming, streamingToolCalls, queuedMessage, sendMessage, cancel } = useChat(app);
|
|
42
|
+
const { handleCommand } = useCommands(app);
|
|
43
|
+
const [systemMessages, setSystemMessages] = useState<MessageData[]>([]);
|
|
44
|
+
const [viewMode, setViewMode] = useState<ViewMode>('chat');
|
|
45
|
+
const [initProvider, setInitProvider] = useState<ProviderName>('siliconflow');
|
|
46
|
+
const [initUrl, setInitUrl] = useState('');
|
|
47
|
+
const { stdout } = useStdout();
|
|
48
|
+
const terminalWidth = stdout?.columns || 80;
|
|
49
|
+
|
|
50
|
+
// v3.0.0: cache stats snapshot for the Header. Re-read whenever messages
|
|
51
|
+
// change (i.e. a round just completed and recorded new usage). Polling on
|
|
52
|
+
// an interval would be wasteful; React re-render is the trigger.
|
|
53
|
+
const [cacheSnapshot, setCacheSnapshot] = useState(() => app.cacheStats.snapshot());
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
setCacheSnapshot(app.cacheStats.snapshot());
|
|
56
|
+
}, [messages.length]);
|
|
57
|
+
|
|
58
|
+
const addMsg = useCallback((content: string) => {
|
|
59
|
+
setSystemMessages(prev => [...prev, { role: 'assistant', content }]);
|
|
60
|
+
}, []);
|
|
61
|
+
|
|
62
|
+
const onSubmit = useCallback(async (input: string) => {
|
|
63
|
+
// Init wizard - API Key input
|
|
64
|
+
if (viewMode === 'init_key') {
|
|
65
|
+
const key = input.trim();
|
|
66
|
+
if (!key) { setViewMode('chat'); return; }
|
|
67
|
+
// Save and complete
|
|
68
|
+
const { App: AppClass } = await import('../app/index.js');
|
|
69
|
+
const cfg = app.config.get();
|
|
70
|
+
app.config.save({ provider: initProvider, apiKey: key, model: cfg.model || 'default' });
|
|
71
|
+
saveModelToHistory(cfg.model || 'default');
|
|
72
|
+
setViewMode('chat');
|
|
73
|
+
addMsg(`配置已更新: ${initProvider}`);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Init wizard - Custom URL input
|
|
78
|
+
if (viewMode === 'init_url') {
|
|
79
|
+
const url = input.trim();
|
|
80
|
+
if (!url) { setViewMode('chat'); return; }
|
|
81
|
+
setInitUrl(url);
|
|
82
|
+
setViewMode('init_key');
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Init wizard - Custom model name
|
|
87
|
+
if (viewMode === 'init_custom_model') {
|
|
88
|
+
const model = input.trim();
|
|
89
|
+
if (model) {
|
|
90
|
+
app.config.save({ model });
|
|
91
|
+
saveModelToHistory(model);
|
|
92
|
+
addMsg(`模型已切换: ${model}`);
|
|
93
|
+
}
|
|
94
|
+
setViewMode('chat');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Model selector - ignore text input
|
|
99
|
+
if (viewMode === 'model_select') return;
|
|
100
|
+
|
|
101
|
+
// Normal command handling
|
|
102
|
+
const result = handleCommand(input);
|
|
103
|
+
|
|
104
|
+
if (result.handled) {
|
|
105
|
+
if (input.trim() === '/模型' || input.trim() === '/model') {
|
|
106
|
+
setViewMode('model_select');
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (result.output) {
|
|
111
|
+
setSystemMessages(prev => [
|
|
112
|
+
...prev,
|
|
113
|
+
{ role: 'user', content: input },
|
|
114
|
+
{ role: 'assistant', content: result.output! },
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (result.action === 'clear') setSystemMessages([]);
|
|
119
|
+
|
|
120
|
+
if (result.action === 'reinit') {
|
|
121
|
+
setViewMode('init_wizard');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
sendMessage(input);
|
|
128
|
+
}, [handleCommand, sendMessage, app, viewMode, initProvider, initUrl, addMsg]);
|
|
129
|
+
|
|
130
|
+
const allMessages = [...systemMessages, ...messages];
|
|
131
|
+
const activeSkills = app.skills.listActive().map(s => s.id);
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<Box flexDirection="column" paddingX={1}>
|
|
135
|
+
<Header
|
|
136
|
+
provider={app.config.get().provider}
|
|
137
|
+
model={app.config.get().model}
|
|
138
|
+
cacheHitRate={cacheSnapshot.hitRate > 0 ? cacheSnapshot.hitRate : null}
|
|
139
|
+
cacheSavingsCNY={cacheSnapshot.estimatedSavingsCNY}
|
|
140
|
+
/>
|
|
141
|
+
<ChatList
|
|
142
|
+
messages={allMessages}
|
|
143
|
+
streaming={streaming}
|
|
144
|
+
streamingToolCalls={streamingToolCalls}
|
|
145
|
+
width={terminalWidth - 4}
|
|
146
|
+
/>
|
|
147
|
+
<Thinking active={isThinking} />
|
|
148
|
+
{queuedMessage && (
|
|
149
|
+
<Box paddingLeft={1}>
|
|
150
|
+
<Text color="#F59E0B">📎 已排队: </Text>
|
|
151
|
+
<Text color="#94A3B8">{queuedMessage}</Text>
|
|
152
|
+
</Box>
|
|
153
|
+
)}
|
|
154
|
+
{viewMode === 'model_select' ? (
|
|
155
|
+
<ModelSelector
|
|
156
|
+
currentModel={app.config.get().model}
|
|
157
|
+
currentProvider={app.config.get().provider}
|
|
158
|
+
onSelect={(model) => {
|
|
159
|
+
app.config.save({ model });
|
|
160
|
+
saveModelToHistory(model);
|
|
161
|
+
setViewMode('chat');
|
|
162
|
+
addMsg(`模型已切换: ${model}`);
|
|
163
|
+
}}
|
|
164
|
+
onAddNew={() => setViewMode('init_wizard')}
|
|
165
|
+
/>
|
|
166
|
+
) : viewMode === 'init_wizard' ? (
|
|
167
|
+
<InitWizard
|
|
168
|
+
onComplete={(provider, model, apiKey, baseUrl) => {
|
|
169
|
+
app.config.save({ provider, model, apiKey, baseUrl });
|
|
170
|
+
saveModelToHistory(model);
|
|
171
|
+
setViewMode('chat');
|
|
172
|
+
addMsg(`配置完成: ${provider} / ${model}`);
|
|
173
|
+
}}
|
|
174
|
+
onCancel={() => setViewMode('chat')}
|
|
175
|
+
/>
|
|
176
|
+
) : (
|
|
177
|
+
<Box flexDirection="column">
|
|
178
|
+
{viewMode === 'init_key' && (
|
|
179
|
+
<Box paddingLeft={1} marginBottom={0}>
|
|
180
|
+
<Text color="#06B6D4" bold>输入 API Key: </Text>
|
|
181
|
+
<Text dimColor>({initProvider})</Text>
|
|
182
|
+
</Box>
|
|
183
|
+
)}
|
|
184
|
+
{viewMode === 'init_url' && (
|
|
185
|
+
<Box paddingLeft={1} marginBottom={0}>
|
|
186
|
+
<Text color="#06B6D4" bold>输入中转站 URL: </Text>
|
|
187
|
+
</Box>
|
|
188
|
+
)}
|
|
189
|
+
{viewMode === 'init_custom_model' && (
|
|
190
|
+
<Box paddingLeft={1} marginBottom={0}>
|
|
191
|
+
<Text color="#06B6D4" bold>输入模型名称: </Text>
|
|
192
|
+
</Box>
|
|
193
|
+
)}
|
|
194
|
+
<UserInput onSubmit={onSubmit} onCancel={cancel} disabled={false} />
|
|
195
|
+
</Box>
|
|
196
|
+
)}
|
|
197
|
+
<StatusBar messageCount={allMessages.length} skills={activeSkills} />
|
|
198
|
+
</Box>
|
|
199
|
+
);
|
|
200
|
+
}
|