thatgfsj-code 0.4.0 → 0.4.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/dist/cmd/index.js +56 -8
- package/dist/cmd/index.js.map +1 -1
- package/dist/llm/anthropic.d.ts +4 -17
- package/dist/llm/anthropic.d.ts.map +1 -1
- package/dist/llm/anthropic.js +54 -12
- package/dist/llm/anthropic.js.map +1 -1
- package/dist/llm/gemini.d.ts +3 -16
- package/dist/llm/gemini.d.ts.map +1 -1
- package/dist/llm/gemini.js +26 -22
- package/dist/llm/gemini.js.map +1 -1
- package/dist/llm/index.d.ts +5 -19
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +87 -87
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/openai.d.ts +9 -23
- package/dist/llm/openai.d.ts.map +1 -1
- package/dist/llm/openai.js +48 -14
- package/dist/llm/openai.js.map +1 -1
- package/dist/llm/provider.d.ts +11 -16
- package/dist/llm/provider.d.ts.map +1 -1
- package/dist/tui/input.d.ts +0 -1
- package/dist/tui/input.d.ts.map +1 -1
- package/dist/tui/input.js +2 -5
- package/dist/tui/input.js.map +1 -1
- package/dist/tui/output.d.ts +27 -12
- package/dist/tui/output.d.ts.map +1 -1
- package/dist/tui/output.js +176 -40
- package/dist/tui/output.js.map +1 -1
- package/dist/tui/repl.d.ts +0 -10
- package/dist/tui/repl.d.ts.map +1 -1
- package/dist/tui/repl.js +76 -42
- package/dist/tui/repl.js.map +1 -1
- package/dist/tui/welcome.d.ts +1 -11
- package/dist/tui/welcome.d.ts.map +1 -1
- package/dist/tui/welcome.js +60 -56
- package/dist/tui/welcome.js.map +1 -1
- package/package.json +1 -1
- package/src/cmd/index.ts +49 -8
- package/src/llm/anthropic.ts +60 -14
- package/src/llm/gemini.ts +31 -24
- package/src/llm/index.ts +94 -92
- package/src/llm/openai.ts +58 -15
- package/src/llm/provider.ts +12 -16
- package/src/tui/input.ts +2 -5
- package/src/tui/output.ts +193 -40
- package/src/tui/repl.ts +82 -43
- package/src/tui/welcome.ts +62 -59
package/dist/llm/index.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* LLM Service - Factory for creating providers
|
|
3
3
|
* Supports all providers + custom relay stations (中转站)
|
|
4
4
|
*/
|
|
5
|
-
import chalk from 'chalk';
|
|
6
5
|
import { PROVIDERS } from '../config/providers.js';
|
|
7
6
|
import { OpenAIProvider } from './openai.js';
|
|
8
7
|
import { AnthropicProvider } from './anthropic.js';
|
|
@@ -15,9 +14,6 @@ export class LLMService {
|
|
|
15
14
|
this.provider = provider;
|
|
16
15
|
this.apiKey = apiKey;
|
|
17
16
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Create LLMService from AIConfig
|
|
20
|
-
*/
|
|
21
17
|
static fromConfig(config) {
|
|
22
18
|
const providerName = config.provider || 'siliconflow';
|
|
23
19
|
const providerConfig = PROVIDERS[providerName];
|
|
@@ -42,124 +38,128 @@ export class LLMService {
|
|
|
42
38
|
}
|
|
43
39
|
return new LLMService(provider, providerCfg.apiKey);
|
|
44
40
|
}
|
|
45
|
-
/**
|
|
46
|
-
* Register tools for function calling
|
|
47
|
-
*/
|
|
48
41
|
registerTools(tools) {
|
|
49
42
|
for (const tool of tools) {
|
|
50
43
|
this.tools.set(tool.name, tool);
|
|
51
44
|
}
|
|
52
45
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*/
|
|
56
|
-
getProviderName() {
|
|
57
|
-
return this.provider.name;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Check if API key is available
|
|
61
|
-
*/
|
|
62
|
-
hasApiKey() {
|
|
63
|
-
return !!this.apiKey;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Non-streaming chat
|
|
67
|
-
*/
|
|
46
|
+
getProviderName() { return this.provider.name; }
|
|
47
|
+
hasApiKey() { return !!this.apiKey; }
|
|
68
48
|
async chat(messages, options) {
|
|
69
|
-
if (!this.hasApiKey())
|
|
49
|
+
if (!this.hasApiKey())
|
|
70
50
|
throw new Error(this.getNoKeyMessage());
|
|
71
|
-
|
|
72
|
-
return this.provider.chat(messages, options);
|
|
51
|
+
const toolsArray = [...this.tools.values()];
|
|
52
|
+
return this.provider.chat(messages, options, toolsArray.length > 0 ? toolsArray : undefined);
|
|
73
53
|
}
|
|
74
54
|
/**
|
|
75
55
|
* Streaming chat with agent loop (tool call support)
|
|
56
|
+
* - Streams text to the user
|
|
57
|
+
* - Detects structured tool calls from the API
|
|
58
|
+
* - Executes tools and loops back for more
|
|
76
59
|
*/
|
|
77
60
|
async *chatStream(messages, options) {
|
|
78
|
-
if (!this.hasApiKey())
|
|
61
|
+
if (!this.hasApiKey())
|
|
79
62
|
throw new Error(this.getNoKeyMessage());
|
|
80
|
-
}
|
|
81
63
|
const maxIterations = options?.maxIterations ?? 10;
|
|
82
64
|
let currentMessages = [...messages];
|
|
83
65
|
let iterations = 0;
|
|
84
66
|
while (iterations < maxIterations) {
|
|
85
67
|
iterations++;
|
|
86
|
-
|
|
68
|
+
const toolsArray = [...this.tools.values()];
|
|
69
|
+
const hasTools = toolsArray.length > 0;
|
|
87
70
|
let fullContent = '';
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
71
|
+
let detectedToolCalls;
|
|
72
|
+
// Stream the response, passing tools so the API knows about them
|
|
73
|
+
const stream = this.provider.chatStream(currentMessages, options, hasTools ? toolsArray : undefined);
|
|
74
|
+
for await (const chunk of stream) {
|
|
75
|
+
if (chunk.type === 'text' && chunk.content) {
|
|
76
|
+
fullContent += chunk.content;
|
|
77
|
+
yield chunk.content;
|
|
78
|
+
}
|
|
79
|
+
else if (chunk.type === 'tool_calls' && chunk.toolCalls) {
|
|
80
|
+
detectedToolCalls = chunk.toolCalls;
|
|
81
|
+
}
|
|
91
82
|
}
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
83
|
+
// If we got tool calls, execute them and loop
|
|
84
|
+
if (detectedToolCalls && detectedToolCalls.length > 0) {
|
|
85
|
+
// Add assistant message with tool calls
|
|
86
|
+
currentMessages.push({
|
|
87
|
+
role: 'assistant',
|
|
88
|
+
content: fullContent || '',
|
|
89
|
+
tool_calls: detectedToolCalls,
|
|
90
|
+
});
|
|
91
|
+
// Execute each tool
|
|
92
|
+
for (const toolCall of detectedToolCalls) {
|
|
93
|
+
const tool = this.tools.get(toolCall.function.name);
|
|
94
|
+
// Yield structured tool call as JSON line
|
|
95
|
+
yield `\n@@TOOL@@${JSON.stringify({ action: 'call', name: toolCall.function.name, args: toolCall.function.arguments })}\n`;
|
|
96
|
+
if (!tool) {
|
|
97
|
+
const errMsg = `Tool "${toolCall.function.name}" not found`;
|
|
100
98
|
currentMessages.push({
|
|
101
|
-
role: '
|
|
102
|
-
content:
|
|
103
|
-
|
|
99
|
+
role: 'tool',
|
|
100
|
+
content: errMsg,
|
|
101
|
+
tool_call_id: toolCall.id,
|
|
102
|
+
name: toolCall.function.name,
|
|
104
103
|
});
|
|
105
|
-
|
|
106
|
-
const tool = this.tools.get(toolCall.function.name);
|
|
107
|
-
if (!tool) {
|
|
108
|
-
currentMessages.push({
|
|
109
|
-
role: 'tool',
|
|
110
|
-
content: `Tool "${toolCall.function.name}" not found`,
|
|
111
|
-
tool_call_id: toolCall.id,
|
|
112
|
-
name: toolCall.function.name,
|
|
113
|
-
});
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
try {
|
|
117
|
-
const params = JSON.parse(toolCall.function.arguments || '{}');
|
|
118
|
-
const result = await tool.execute(params);
|
|
119
|
-
currentMessages.push({
|
|
120
|
-
role: 'tool',
|
|
121
|
-
content: result.success ? (result.output || JSON.stringify(result.data)) : (result.error || 'Tool failed'),
|
|
122
|
-
tool_call_id: toolCall.id,
|
|
123
|
-
name: toolCall.function.name,
|
|
124
|
-
});
|
|
125
|
-
yield `\n${chalk.gray(`[tool: ${toolCall.function.name}]`)}`;
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
currentMessages.push({
|
|
129
|
-
role: 'tool',
|
|
130
|
-
content: `Tool execution error: ${error.message}`,
|
|
131
|
-
tool_call_id: toolCall.id,
|
|
132
|
-
name: toolCall.function.name,
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
104
|
+
yield `@@TOOL@@${JSON.stringify({ action: 'result', error: errMsg })}\n`;
|
|
136
105
|
continue;
|
|
137
106
|
}
|
|
107
|
+
try {
|
|
108
|
+
const params = JSON.parse(toolCall.function.arguments || '{}');
|
|
109
|
+
const result = await tool.execute(params);
|
|
110
|
+
const output = result.success ? (result.output || JSON.stringify(result.data)) : (result.error || 'Tool failed');
|
|
111
|
+
currentMessages.push({
|
|
112
|
+
role: 'tool',
|
|
113
|
+
content: output,
|
|
114
|
+
tool_call_id: toolCall.id,
|
|
115
|
+
name: toolCall.function.name,
|
|
116
|
+
});
|
|
117
|
+
yield `@@TOOL@@${JSON.stringify({ action: 'result', output })}\n`;
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
const errMsg = `Error: ${error.message}`;
|
|
121
|
+
currentMessages.push({
|
|
122
|
+
role: 'tool',
|
|
123
|
+
content: errMsg,
|
|
124
|
+
tool_call_id: toolCall.id,
|
|
125
|
+
name: toolCall.function.name,
|
|
126
|
+
});
|
|
127
|
+
yield `@@TOOL@@${JSON.stringify({ action: 'result', error: errMsg })}\n`;
|
|
128
|
+
}
|
|
138
129
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
130
|
+
yield '\n';
|
|
131
|
+
continue;
|
|
142
132
|
}
|
|
133
|
+
// No tool calls - done
|
|
143
134
|
return { content: fullContent, role: 'assistant' };
|
|
144
135
|
}
|
|
145
136
|
return { content: '[Agent loop exceeded maximum iterations]', role: 'assistant' };
|
|
146
137
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
138
|
+
truncateArgs(args) {
|
|
139
|
+
try {
|
|
140
|
+
const obj = JSON.parse(args || '{}');
|
|
141
|
+
const entries = Object.entries(obj);
|
|
142
|
+
if (entries.length === 0)
|
|
143
|
+
return '';
|
|
144
|
+
return entries.map(([k, v]) => {
|
|
145
|
+
const val = typeof v === 'string' && v.length > 50 ? v.slice(0, 50) + '...' : v;
|
|
146
|
+
return `${k}: ${JSON.stringify(val)}`;
|
|
147
|
+
}).join(', ');
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return args.length > 80 ? args.slice(0, 80) + '...' : args;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
150
153
|
getNoKeyMessage() {
|
|
151
154
|
return [
|
|
152
155
|
'❌ 未配置 API Key,无法调用 AI。',
|
|
153
156
|
'',
|
|
154
|
-
'
|
|
155
|
-
' gfcode init',
|
|
157
|
+
'请先运行: gfcode init',
|
|
156
158
|
'',
|
|
157
|
-
'
|
|
158
|
-
' export SILICONFLOW_API_KEY="sk-..."
|
|
159
|
-
' export OPENAI_API_KEY="sk-..."
|
|
160
|
-
' export DEEPSEEK_API_KEY="sk-..."
|
|
161
|
-
' export KIMI_API_KEY="sk-..." # Kimi',
|
|
162
|
-
' export ANTHROPIC_API_KEY="sk-ant-..." # Claude',
|
|
159
|
+
'或设置环境变量:',
|
|
160
|
+
' export SILICONFLOW_API_KEY="sk-..."',
|
|
161
|
+
' export OPENAI_API_KEY="sk-..."',
|
|
162
|
+
' export DEEPSEEK_API_KEY="sk-..."',
|
|
163
163
|
].join('\n');
|
|
164
164
|
}
|
|
165
165
|
}
|
package/dist/llm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,EAAE,SAA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AAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;gBAChF,OAAO,GAAG,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACxC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChB,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;QAC7D,CAAC;IACH,CAAC;IAEO,eAAe;QACrB,OAAO;YACL,wBAAwB;YACxB,EAAE;YACF,mBAAmB;YACnB,EAAE;YACF,UAAU;YACV,uCAAuC;YACvC,kCAAkC;YAClC,oCAAoC;SACrC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACf,CAAC;CACF;AAGD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/llm/openai.d.ts
CHANGED
|
@@ -2,40 +2,26 @@
|
|
|
2
2
|
* OpenAI-compatible Provider
|
|
3
3
|
* Works with: OpenAI, SiliconFlow, DeepSeek, Kimi, Zhipu, MiniMax, Baichuan, Stepfun, Doubao, Ollama, ERNIE
|
|
4
4
|
* Also works with any OpenAI-compatible relay station (中转站)
|
|
5
|
-
*
|
|
6
|
-
* API: POST {baseUrl}/chat/completions
|
|
7
|
-
* Auth: Authorization: Bearer {apiKey}
|
|
8
|
-
* Streaming: SSE with "data: {...}" lines, ends with "data: [DONE]"
|
|
9
5
|
*/
|
|
10
|
-
import type { ChatMessage, ChatResponse, ChatOptions } from '../types.js';
|
|
6
|
+
import type { ChatMessage, ChatResponse, ChatOptions, ToolCall } from '../types.js';
|
|
11
7
|
import type { Tool } from '../tools/types.js';
|
|
12
8
|
import type { LLMProvider, ProviderConfig } from './provider.js';
|
|
9
|
+
export interface StreamChunk {
|
|
10
|
+
type: 'text' | 'tool_calls';
|
|
11
|
+
content?: string;
|
|
12
|
+
toolCalls?: ToolCall[];
|
|
13
|
+
}
|
|
13
14
|
export declare class OpenAIProvider implements LLMProvider {
|
|
14
15
|
readonly name = "openai";
|
|
15
16
|
protected config: ProviderConfig;
|
|
16
17
|
constructor(config: ProviderConfig);
|
|
17
18
|
buildTools(tools: Tool[]): any[];
|
|
18
|
-
chat(messages: ChatMessage[], options?: ChatOptions): Promise<ChatResponse>;
|
|
19
|
-
chatStream(messages: ChatMessage[], options?: ChatOptions): AsyncGenerator<
|
|
19
|
+
chat(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): Promise<ChatResponse>;
|
|
20
|
+
chatStream(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): AsyncGenerator<StreamChunk, ChatResponse>;
|
|
20
21
|
/**
|
|
21
22
|
* Build the request body for OpenAI-compatible API
|
|
22
23
|
*/
|
|
23
|
-
protected buildRequest(messages: ChatMessage[], stream: boolean, options?: ChatOptions):
|
|
24
|
-
stream_options?: {
|
|
25
|
-
include_usage: boolean;
|
|
26
|
-
} | undefined;
|
|
27
|
-
model: string;
|
|
28
|
-
messages: {
|
|
29
|
-
tool_calls?: import("../types.js").ToolCall[] | undefined;
|
|
30
|
-
tool_call_id?: string | undefined;
|
|
31
|
-
name?: string | undefined;
|
|
32
|
-
role: import("../types.js").Role;
|
|
33
|
-
content: string;
|
|
34
|
-
}[];
|
|
35
|
-
temperature: number;
|
|
36
|
-
max_tokens: number;
|
|
37
|
-
stream: boolean;
|
|
38
|
-
};
|
|
24
|
+
protected buildRequest(messages: ChatMessage[], stream: boolean, options?: ChatOptions, tools?: Tool[]): any;
|
|
39
25
|
/**
|
|
40
26
|
* Execute the HTTP request
|
|
41
27
|
*/
|
package/dist/llm/openai.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEjE,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,GAAG,YAAY,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,QAAQ,EAAE,CAAC;CACxB;AAED,qBAAa,cAAe,YAAW,WAAW;IAChD,QAAQ,CAAC,IAAI,YAAY;IACzB,SAAS,CAAC,MAAM,EAAE,cAAc,CAAC;gBAErB,MAAM,EAAE,cAAc;IAIlC,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,GAAG,EAAE;IAiB1B,IAAI,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC;IAkB1F,UAAU,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC;IAsF5H;;OAEG;IACH,SAAS,CAAC,YAAY,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE;IAwBtG;;OAEG;cACa,SAAS,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC;CAWxD"}
|
package/dist/llm/openai.js
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
* OpenAI-compatible Provider
|
|
3
3
|
* Works with: OpenAI, SiliconFlow, DeepSeek, Kimi, Zhipu, MiniMax, Baichuan, Stepfun, Doubao, Ollama, ERNIE
|
|
4
4
|
* Also works with any OpenAI-compatible relay station (中转站)
|
|
5
|
-
*
|
|
6
|
-
* API: POST {baseUrl}/chat/completions
|
|
7
|
-
* Auth: Authorization: Bearer {apiKey}
|
|
8
|
-
* Streaming: SSE with "data: {...}" lines, ends with "data: [DONE]"
|
|
9
5
|
*/
|
|
10
6
|
export class OpenAIProvider {
|
|
11
7
|
name = 'openai';
|
|
@@ -27,8 +23,8 @@ export class OpenAIProvider {
|
|
|
27
23
|
},
|
|
28
24
|
}));
|
|
29
25
|
}
|
|
30
|
-
async chat(messages, options) {
|
|
31
|
-
const body = this.buildRequest(messages, false, options);
|
|
26
|
+
async chat(messages, options, tools) {
|
|
27
|
+
const body = this.buildRequest(messages, false, options, tools);
|
|
32
28
|
const response = await this.doRequest(body);
|
|
33
29
|
const data = await response.json();
|
|
34
30
|
const choice = data.choices?.[0];
|
|
@@ -43,8 +39,8 @@ export class OpenAIProvider {
|
|
|
43
39
|
tool_calls: choice?.message?.tool_calls,
|
|
44
40
|
};
|
|
45
41
|
}
|
|
46
|
-
async *chatStream(messages, options) {
|
|
47
|
-
const body = this.buildRequest(messages, true, options);
|
|
42
|
+
async *chatStream(messages, options, tools) {
|
|
43
|
+
const body = this.buildRequest(messages, true, options, tools);
|
|
48
44
|
const response = await this.doRequest(body);
|
|
49
45
|
if (!response.ok || !response.body) {
|
|
50
46
|
const text = await response.text().catch(() => '');
|
|
@@ -54,6 +50,8 @@ export class OpenAIProvider {
|
|
|
54
50
|
const decoder = new TextDecoder();
|
|
55
51
|
let fullContent = '';
|
|
56
52
|
let buffer = '';
|
|
53
|
+
// Accumulate streaming tool call chunks
|
|
54
|
+
const toolCallBuffers = new Map();
|
|
57
55
|
try {
|
|
58
56
|
while (true) {
|
|
59
57
|
const { done, value } = await reader.read();
|
|
@@ -73,13 +71,26 @@ export class OpenAIProvider {
|
|
|
73
71
|
try {
|
|
74
72
|
const data = JSON.parse(trimmed.slice(6));
|
|
75
73
|
const delta = data.choices?.[0]?.delta;
|
|
74
|
+
// Text content
|
|
76
75
|
if (delta?.content) {
|
|
77
76
|
fullContent += delta.content;
|
|
78
|
-
yield delta.content;
|
|
77
|
+
yield { type: 'text', content: delta.content };
|
|
79
78
|
}
|
|
80
|
-
//
|
|
79
|
+
// Streaming tool calls - accumulate chunks
|
|
81
80
|
if (delta?.tool_calls) {
|
|
82
|
-
|
|
81
|
+
for (const tc of delta.tool_calls) {
|
|
82
|
+
const idx = tc.index ?? 0;
|
|
83
|
+
if (!toolCallBuffers.has(idx)) {
|
|
84
|
+
toolCallBuffers.set(idx, { id: '', name: '', arguments: '' });
|
|
85
|
+
}
|
|
86
|
+
const buf = toolCallBuffers.get(idx);
|
|
87
|
+
if (tc.id)
|
|
88
|
+
buf.id = tc.id;
|
|
89
|
+
if (tc.function?.name)
|
|
90
|
+
buf.name += tc.function.name;
|
|
91
|
+
if (tc.function?.arguments)
|
|
92
|
+
buf.arguments += tc.function.arguments;
|
|
93
|
+
}
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
96
|
catch {
|
|
@@ -91,13 +102,31 @@ export class OpenAIProvider {
|
|
|
91
102
|
finally {
|
|
92
103
|
reader.releaseLock();
|
|
93
104
|
}
|
|
94
|
-
|
|
105
|
+
// Convert accumulated tool call buffers to ToolCall[]
|
|
106
|
+
const toolCalls = [];
|
|
107
|
+
for (const [, buf] of toolCallBuffers) {
|
|
108
|
+
if (buf.id && buf.name) {
|
|
109
|
+
toolCalls.push({
|
|
110
|
+
id: buf.id,
|
|
111
|
+
type: 'function',
|
|
112
|
+
function: { name: buf.name, arguments: buf.arguments },
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (toolCalls.length > 0) {
|
|
117
|
+
yield { type: 'tool_calls', toolCalls };
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
content: fullContent,
|
|
121
|
+
role: 'assistant',
|
|
122
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
123
|
+
};
|
|
95
124
|
}
|
|
96
125
|
/**
|
|
97
126
|
* Build the request body for OpenAI-compatible API
|
|
98
127
|
*/
|
|
99
|
-
buildRequest(messages, stream, options) {
|
|
100
|
-
|
|
128
|
+
buildRequest(messages, stream, options, tools) {
|
|
129
|
+
const body = {
|
|
101
130
|
model: this.config.model,
|
|
102
131
|
messages: messages.map(m => ({
|
|
103
132
|
role: m.role,
|
|
@@ -111,6 +140,11 @@ export class OpenAIProvider {
|
|
|
111
140
|
stream,
|
|
112
141
|
...(stream && { stream_options: { include_usage: true } }),
|
|
113
142
|
};
|
|
143
|
+
// Add tools if provided - this is critical for structured tool calling
|
|
144
|
+
if (tools && tools.length > 0) {
|
|
145
|
+
body.tools = this.buildTools(tools);
|
|
146
|
+
}
|
|
147
|
+
return body;
|
|
114
148
|
}
|
|
115
149
|
/**
|
|
116
150
|
* Execute the HTTP request
|
package/dist/llm/openai.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/llm/openai.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAYH,MAAM,OAAO,cAAc;IAChB,IAAI,GAAG,QAAQ,CAAC;IACf,MAAM,CAAiB;IAEjC,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,UAAU,CAAC,KAAa;QACtB,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC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CAAC,GAAG,EAAE;YAChB,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;gBAClC,eAAe,EAAE,UAAU,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;aAChD;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/dist/llm/provider.d.ts
CHANGED
|
@@ -2,27 +2,22 @@
|
|
|
2
2
|
* Abstract LLM Provider interface
|
|
3
3
|
* All providers implement this interface
|
|
4
4
|
*/
|
|
5
|
-
import type { ChatMessage, ChatResponse, ChatOptions } from '../types.js';
|
|
5
|
+
import type { ChatMessage, ChatResponse, ChatOptions, ToolCall } from '../types.js';
|
|
6
6
|
import type { Tool } from '../tools/types.js';
|
|
7
|
+
export interface StreamChunk {
|
|
8
|
+
type: 'text' | 'tool_calls';
|
|
9
|
+
content?: string;
|
|
10
|
+
toolCalls?: ToolCall[];
|
|
11
|
+
}
|
|
7
12
|
export interface LLMProvider {
|
|
8
|
-
/** Provider name for logging */
|
|
9
13
|
readonly name: string;
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Streaming chat completion - yields text chunks
|
|
16
|
-
*/
|
|
17
|
-
chatStream(messages: ChatMessage[], options?: ChatOptions): AsyncGenerator<string, ChatResponse>;
|
|
18
|
-
/**
|
|
19
|
-
* Convert Tool[] to provider-specific format
|
|
20
|
-
*/
|
|
14
|
+
/** Non-streaming chat with optional tools */
|
|
15
|
+
chat(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): Promise<ChatResponse>;
|
|
16
|
+
/** Streaming chat with optional tools - yields StreamChunks */
|
|
17
|
+
chatStream(messages: ChatMessage[], options?: ChatOptions, tools?: Tool[]): AsyncGenerator<StreamChunk, ChatResponse>;
|
|
18
|
+
/** Convert Tool[] to provider-specific format */
|
|
21
19
|
buildTools(tools: Tool[]): any[];
|
|
22
20
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Provider configuration passed to each provider
|
|
25
|
-
*/
|
|
26
21
|
export interface ProviderConfig {
|
|
27
22
|
apiKey: string;
|
|
28
23
|
model: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/llm/provider.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/llm/provider.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,GAAG,YAAY,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,QAAQ,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,6CAA6C;IAC7C,IAAI,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAE5F,+DAA+D;IAC/D,UAAU,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IAEtH,iDAAiD;IACjD,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,GAAG,EAAE,CAAC;CAClC;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
package/dist/tui/input.d.ts
CHANGED
package/dist/tui/input.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../src/tui/input.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../src/tui/input.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,qBAAa,SAAS;IACpB,OAAO,CAAC,EAAE,CAAqB;IAC/B,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,YAAY,CAAc;;IAyB5B,MAAM,CAAC,MAAM,GAAE,MAAyB,GAAG,OAAO,CAAC,MAAM,CAAC;IAQhE,KAAK,IAAI,IAAI;IAIb,OAAO,CAAC,SAAS;CAKlB"}
|
package/dist/tui/input.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* REPL Input Handler
|
|
3
|
-
* Migrated from old src/repl/input.ts
|
|
4
3
|
*/
|
|
5
4
|
import readline from 'readline';
|
|
6
5
|
import chalk from 'chalk';
|
|
@@ -16,12 +15,10 @@ export class REPLInput {
|
|
|
16
15
|
historySize: 100,
|
|
17
16
|
tabSize: 4,
|
|
18
17
|
});
|
|
19
|
-
// Handle Ctrl+C
|
|
20
18
|
this.rl.on('SIGINT', () => {
|
|
21
|
-
process.stdout.write(chalk.gray('\n
|
|
19
|
+
process.stdout.write(chalk.gray('\n Goodbye! 👋\n'));
|
|
22
20
|
process.exit(0);
|
|
23
21
|
});
|
|
24
|
-
// Track history
|
|
25
22
|
this.rl.on('line', (line) => {
|
|
26
23
|
if (line.trim()) {
|
|
27
24
|
this.history.push(line);
|
|
@@ -31,7 +28,7 @@ export class REPLInput {
|
|
|
31
28
|
this.historyIndex = this.history.length;
|
|
32
29
|
});
|
|
33
30
|
}
|
|
34
|
-
async prompt(prefix = chalk.
|
|
31
|
+
async prompt(prefix = chalk.cyan('❯ ')) {
|
|
35
32
|
return new Promise((resolve) => {
|
|
36
33
|
this.rl.question(prefix, (answer) => {
|
|
37
34
|
resolve(answer);
|
package/dist/tui/input.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../src/tui/input.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../src/tui/input.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,OAAO,SAAS;IACZ,EAAE,CAAqB;IACvB,OAAO,GAAa,EAAE,CAAC;IACvB,YAAY,GAAW,CAAC,CAAC,CAAC;IAElC;QACE,IAAI,CAAC,EAAE,GAAG,QAAQ,CAAC,eAAe,CAAC;YACjC,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;YACpC,WAAW,EAAE,GAAG;YAChB,OAAO,EAAE,CAAC;SACX,CAAC,CAAC;QAEH,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE;YACxB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC,CAAC;YACtD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YAC1B,IAAI,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beautiful terminal UI
|
|
4
3
|
*/
|
|
5
4
|
export declare class REPLOutput {
|
|
6
5
|
private spinner;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
printSuccess(message: string): void;
|
|
10
|
-
printInfo(message: string): void;
|
|
11
|
-
printDivider(char?: string, length?: number): void;
|
|
12
|
-
printHeader(text: string): void;
|
|
13
|
-
startSpinner(text: string): void;
|
|
14
|
-
stopSpinner(): void;
|
|
15
|
-
stopSpinnerFail(text?: string): void;
|
|
16
|
-
clear(): void;
|
|
6
|
+
private inAssistantBlock;
|
|
7
|
+
private currentLineHasContent;
|
|
17
8
|
printBanner(): void;
|
|
9
|
+
getPrompt(): string;
|
|
10
|
+
printUserInput(input: string): void;
|
|
11
|
+
beginAssistant(): void;
|
|
12
|
+
writeChunk(chunk: string): void;
|
|
13
|
+
endAssistant(): void;
|
|
14
|
+
printToolCall(name: string, args: string): void;
|
|
15
|
+
printToolResult(output: string, isError?: boolean): void;
|
|
16
|
+
printToolEnd(): void;
|
|
17
|
+
startThinking(): void;
|
|
18
|
+
startExecuting(toolName: string): void;
|
|
19
|
+
stopThinking(): void;
|
|
20
|
+
stopThinkingFail(msg: string): void;
|
|
21
|
+
info(msg: string): void;
|
|
22
|
+
success(msg: string): void;
|
|
23
|
+
error(msg: string): void;
|
|
24
|
+
warning(msg: string): void;
|
|
25
|
+
dim(msg: string): void;
|
|
26
|
+
divider(): void;
|
|
27
|
+
spacer(): void;
|
|
28
|
+
clear(): void;
|
|
29
|
+
section(title: string, items: Array<{
|
|
30
|
+
label: string;
|
|
31
|
+
value: string;
|
|
32
|
+
}>): void;
|
|
18
33
|
printHelp(): void;
|
|
19
34
|
}
|
|
20
35
|
//# sourceMappingURL=output.d.ts.map
|
package/dist/tui/output.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../../src/tui/output.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"output.d.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