thatgfsj-code 0.4.1 → 0.5.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/dist/app/index.d.ts +6 -14
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +28 -30
- package/dist/app/index.js.map +1 -1
- package/dist/cmd/index.js +33 -25
- package/dist/cmd/index.js.map +1 -1
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +0 -1
- package/dist/llm/index.js.map +1 -1
- package/dist/tui/output.d.ts +12 -3
- package/dist/tui/output.d.ts.map +1 -1
- package/dist/tui/output.js +43 -47
- package/dist/tui/output.js.map +1 -1
- package/dist/tui/repl.d.ts.map +1 -1
- package/dist/tui/repl.js +42 -28
- package/dist/tui/repl.js.map +1 -1
- package/dist/tui/welcome.js +1 -1
- package/package.json +1 -1
- package/src/app/index.ts +28 -33
- package/src/cmd/index.ts +26 -22
- package/src/llm/index.ts +0 -1
- package/src/tui/output.ts +43 -44
- package/src/tui/repl.ts +40 -29
- package/src/tui/welcome.ts +1 -1
- package/src/app/agent.ts +0 -140
package/src/app/agent.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent Loop - Core agent logic with streaming and tool execution
|
|
3
|
-
* Extracted from old src/core/ai-engine.ts
|
|
4
|
-
*
|
|
5
|
-
* The agent loop: while(true) { stream response → check tool calls → execute → repeat }
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import chalk from 'chalk';
|
|
9
|
-
import type { ChatMessage, ChatResponse } from '../types.js';
|
|
10
|
-
import type { Tool } from '../tools/types.js';
|
|
11
|
-
import type { HookManager } from '../hooks/index.js';
|
|
12
|
-
import { LLMService } from '../llm/index.js';
|
|
13
|
-
import { ToolRegistry } from '../tools/index.js';
|
|
14
|
-
|
|
15
|
-
export interface AgentConfig {
|
|
16
|
-
maxIterations?: number;
|
|
17
|
-
hooks?: HookManager;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class Agent {
|
|
21
|
-
private llm: LLMService;
|
|
22
|
-
private registry: ToolRegistry;
|
|
23
|
-
private hooks?: HookManager;
|
|
24
|
-
private maxIterations: number;
|
|
25
|
-
|
|
26
|
-
constructor(llm: LLMService, registry: ToolRegistry, config: AgentConfig = {}) {
|
|
27
|
-
this.llm = llm;
|
|
28
|
-
this.registry = registry;
|
|
29
|
-
this.hooks = config.hooks;
|
|
30
|
-
this.maxIterations = config.maxIterations ?? 10;
|
|
31
|
-
|
|
32
|
-
// Register tools with LLM service
|
|
33
|
-
this.llm.registerTools(registry.list());
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Run agent loop with streaming output
|
|
38
|
-
* Returns the final response
|
|
39
|
-
*/
|
|
40
|
-
async *run(messages: ChatMessage[]): AsyncGenerator<string, ChatResponse> {
|
|
41
|
-
let currentMessages = [...messages];
|
|
42
|
-
let iterations = 0;
|
|
43
|
-
|
|
44
|
-
while (iterations < this.maxIterations) {
|
|
45
|
-
iterations++;
|
|
46
|
-
|
|
47
|
-
// Hook: beforeAgentLoop
|
|
48
|
-
if (this.hooks) {
|
|
49
|
-
await this.hooks.emit('beforeAgentLoop', { messages: currentMessages, iteration: iterations });
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Stream the response
|
|
53
|
-
let fullContent = '';
|
|
54
|
-
|
|
55
|
-
for await (const chunk of this.llm.chatStream(currentMessages, { maxIterations: 1 })) {
|
|
56
|
-
fullContent += chunk;
|
|
57
|
-
yield chunk;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Get the full response to check for tool calls
|
|
61
|
-
const response: ChatResponse = {
|
|
62
|
-
content: fullContent,
|
|
63
|
-
role: 'assistant',
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// Check if the response contains tool calls
|
|
67
|
-
// For now, we rely on the LLM service to handle tool calls internally
|
|
68
|
-
// If the LLM service returned tool_calls, we need to process them
|
|
69
|
-
if (response.tool_calls && response.tool_calls.length > 0) {
|
|
70
|
-
currentMessages.push({
|
|
71
|
-
role: 'assistant',
|
|
72
|
-
content: response.content,
|
|
73
|
-
tool_calls: response.tool_calls,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
for (const toolCall of response.tool_calls) {
|
|
77
|
-
// Hook: beforeToolCall
|
|
78
|
-
if (this.hooks) {
|
|
79
|
-
await this.hooks.emit('beforeToolCall', {
|
|
80
|
-
toolName: toolCall.function.name,
|
|
81
|
-
toolParams: JSON.parse(toolCall.function.arguments || '{}'),
|
|
82
|
-
toolCallId: toolCall.id,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const result = await this.registry.execute(
|
|
87
|
-
toolCall.function.name,
|
|
88
|
-
JSON.parse(toolCall.function.arguments || '{}')
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
// Hook: afterToolCall
|
|
92
|
-
if (this.hooks) {
|
|
93
|
-
await this.hooks.emit('afterToolCall', {
|
|
94
|
-
toolName: toolCall.function.name,
|
|
95
|
-
toolParams: JSON.parse(toolCall.function.arguments || '{}'),
|
|
96
|
-
toolResult: result,
|
|
97
|
-
toolCallId: toolCall.id,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
currentMessages.push({
|
|
102
|
-
role: 'tool',
|
|
103
|
-
content: result.success ? (result.output || JSON.stringify(result.data)) : (result.error || 'Tool failed'),
|
|
104
|
-
tool_call_id: toolCall.id,
|
|
105
|
-
name: toolCall.function.name,
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
yield `\n${chalk.gray(`[tool: ${toolCall.function.name}]`)}`;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
continue; // Next iteration
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// No tool calls - done
|
|
115
|
-
if (response.content) {
|
|
116
|
-
currentMessages.push({ role: 'assistant', content: response.content });
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Hook: afterAgentLoop
|
|
120
|
-
if (this.hooks) {
|
|
121
|
-
await this.hooks.emit('afterAgentLoop', { messages: currentMessages, iteration: iterations });
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return response;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return { content: '[Agent loop exceeded maximum iterations]', role: 'assistant' };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Non-streaming chat (convenience method)
|
|
132
|
-
*/
|
|
133
|
-
async chat(messages: ChatMessage[]): Promise<ChatResponse> {
|
|
134
|
-
let fullContent = '';
|
|
135
|
-
for await (const chunk of this.run(messages)) {
|
|
136
|
-
fullContent += chunk;
|
|
137
|
-
}
|
|
138
|
-
return { content: fullContent, role: 'assistant' };
|
|
139
|
-
}
|
|
140
|
-
}
|