@alexnetrebskii/hive-agent 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/README.md +425 -0
- package/dist/agent.d.ts +24 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +277 -0
- package/dist/agent.js.map +1 -0
- package/dist/context.d.ts +52 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +124 -0
- package/dist/context.js.map +1 -0
- package/dist/executor.d.ts +29 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +349 -0
- package/dist/executor.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/prompt.d.ts +31 -0
- package/dist/prompt.d.ts.map +1 -0
- package/dist/prompt.js +84 -0
- package/dist/prompt.js.map +1 -0
- package/dist/providers/index.d.ts +11 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +8 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/llm/base.d.ts +7 -0
- package/dist/providers/llm/base.d.ts.map +1 -0
- package/dist/providers/llm/base.js +7 -0
- package/dist/providers/llm/base.js.map +1 -0
- package/dist/providers/llm/claude.d.ts +31 -0
- package/dist/providers/llm/claude.d.ts.map +1 -0
- package/dist/providers/llm/claude.js +180 -0
- package/dist/providers/llm/claude.js.map +1 -0
- package/dist/providers/llm/openai.d.ts +25 -0
- package/dist/providers/llm/openai.d.ts.map +1 -0
- package/dist/providers/llm/openai.js +171 -0
- package/dist/providers/llm/openai.js.map +1 -0
- package/dist/providers/logger/base.d.ts +7 -0
- package/dist/providers/logger/base.d.ts.map +1 -0
- package/dist/providers/logger/base.js +7 -0
- package/dist/providers/logger/base.js.map +1 -0
- package/dist/providers/logger/console.d.ts +29 -0
- package/dist/providers/logger/console.d.ts.map +1 -0
- package/dist/providers/logger/console.js +71 -0
- package/dist/providers/logger/console.js.map +1 -0
- package/dist/providers/repository/base.d.ts +7 -0
- package/dist/providers/repository/base.d.ts.map +1 -0
- package/dist/providers/repository/base.js +7 -0
- package/dist/providers/repository/base.js.map +1 -0
- package/dist/providers/repository/memory.d.ts +21 -0
- package/dist/providers/repository/memory.d.ts.map +1 -0
- package/dist/providers/repository/memory.js +50 -0
- package/dist/providers/repository/memory.js.map +1 -0
- package/dist/review.d.ts +68 -0
- package/dist/review.d.ts.map +1 -0
- package/dist/review.js +263 -0
- package/dist/review.js.map +1 -0
- package/dist/todo.d.ts +73 -0
- package/dist/todo.d.ts.map +1 -0
- package/dist/todo.js +320 -0
- package/dist/todo.js.map +1 -0
- package/dist/types.d.ts +257 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -0
package/dist/agent.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hive Agent
|
|
3
|
+
*
|
|
4
|
+
* Main agent class that orchestrates tool execution, sub-agents, and context management.
|
|
5
|
+
*/
|
|
6
|
+
import { ContextManager } from './context.js';
|
|
7
|
+
import { executeLoop } from './executor.js';
|
|
8
|
+
import { buildAgentListSection } from './prompt.js';
|
|
9
|
+
import { TodoManager, createTodoTool } from './todo.js';
|
|
10
|
+
import { ReviewManager, createReviewTool } from './review.js';
|
|
11
|
+
/**
|
|
12
|
+
* Create the __ask_user__ tool
|
|
13
|
+
*/
|
|
14
|
+
function createAskUserTool() {
|
|
15
|
+
return {
|
|
16
|
+
name: '__ask_user__',
|
|
17
|
+
description: `Ask the user a clarifying question when you need more information.
|
|
18
|
+
|
|
19
|
+
Usage:
|
|
20
|
+
- Use when requirements are ambiguous
|
|
21
|
+
- Use when you need the user to make a decision
|
|
22
|
+
- Use when you need specific information to proceed
|
|
23
|
+
|
|
24
|
+
Examples:
|
|
25
|
+
- { "question": "Which database should I use?", "options": ["PostgreSQL", "MySQL", "MongoDB"] }
|
|
26
|
+
- { "question": "What is the target directory for the output files?" }`,
|
|
27
|
+
parameters: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
question: {
|
|
31
|
+
type: 'string',
|
|
32
|
+
description: 'The question to ask the user'
|
|
33
|
+
},
|
|
34
|
+
options: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
description: 'Optional array of choices for the user'
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
required: ['question']
|
|
40
|
+
},
|
|
41
|
+
execute: async () => {
|
|
42
|
+
// This tool is handled specially in the executor
|
|
43
|
+
return { success: true, data: 'Question sent to user' };
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create the __task__ tool for spawning sub-agents
|
|
49
|
+
*/
|
|
50
|
+
function createTaskTool(hive, agents) {
|
|
51
|
+
const agentNames = agents.map(a => a.name);
|
|
52
|
+
const agentList = buildAgentListSection(agents);
|
|
53
|
+
return {
|
|
54
|
+
name: '__task__',
|
|
55
|
+
description: `Spawn a sub-agent to handle a specific task.
|
|
56
|
+
${agentList}
|
|
57
|
+
|
|
58
|
+
Usage:
|
|
59
|
+
- Use sub-agents for specialized tasks
|
|
60
|
+
- Provide a clear, specific prompt for the task
|
|
61
|
+
- The sub-agent will return its result`,
|
|
62
|
+
parameters: {
|
|
63
|
+
type: 'object',
|
|
64
|
+
properties: {
|
|
65
|
+
agent: {
|
|
66
|
+
type: 'string',
|
|
67
|
+
enum: agentNames,
|
|
68
|
+
description: 'Which agent to spawn'
|
|
69
|
+
},
|
|
70
|
+
prompt: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
description: 'The task for the agent to perform'
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
required: ['agent', 'prompt']
|
|
76
|
+
},
|
|
77
|
+
execute: async (params) => {
|
|
78
|
+
const { agent: agentName, prompt } = params;
|
|
79
|
+
const agentConfig = agents.find(a => a.name === agentName);
|
|
80
|
+
if (!agentConfig) {
|
|
81
|
+
return { success: false, error: `Unknown agent: ${agentName}` };
|
|
82
|
+
}
|
|
83
|
+
// Log sub-agent start
|
|
84
|
+
hive.config.logger?.info(`[Sub-Agent: ${agentName}] Starting...`);
|
|
85
|
+
hive.config.logger?.info(`[Sub-Agent: ${agentName}] Task: ${prompt.slice(0, 100)}${prompt.length > 100 ? '...' : ''}`);
|
|
86
|
+
// Progress: sub-agent starting
|
|
87
|
+
hive.config.logger?.onProgress?.({
|
|
88
|
+
type: 'sub_agent_start',
|
|
89
|
+
message: `Starting ${agentName}...`,
|
|
90
|
+
details: { agentName }
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
// Create a wrapper logger that prefixes sub-agent logs
|
|
94
|
+
const subLogger = hive.config.logger ? {
|
|
95
|
+
...hive.config.logger,
|
|
96
|
+
debug: (msg, data) => hive.config.logger?.debug(`[${agentName}] ${msg}`, data),
|
|
97
|
+
info: (msg, data) => hive.config.logger?.info(`[${agentName}] ${msg}`, data),
|
|
98
|
+
warn: (msg, data) => hive.config.logger?.warn(`[${agentName}] ${msg}`, data),
|
|
99
|
+
error: (msg, data) => hive.config.logger?.error(`[${agentName}] ${msg}`, data),
|
|
100
|
+
onToolCall: (toolName, params) => {
|
|
101
|
+
hive.config.logger?.info(`[${agentName}] Tool: ${toolName}`);
|
|
102
|
+
hive.config.logger?.onToolCall?.(toolName, params);
|
|
103
|
+
},
|
|
104
|
+
onToolResult: (toolName, result, durationMs) => {
|
|
105
|
+
const status = result.success ? 'OK' : 'ERROR';
|
|
106
|
+
hive.config.logger?.info(`[${agentName}] Tool ${toolName}: ${status} (${durationMs}ms)`);
|
|
107
|
+
hive.config.logger?.onToolResult?.(toolName, result, durationMs);
|
|
108
|
+
},
|
|
109
|
+
onProgress: (update) => {
|
|
110
|
+
// Prefix sub-agent progress messages
|
|
111
|
+
hive.config.logger?.onProgress?.({
|
|
112
|
+
...update,
|
|
113
|
+
message: `[${agentName}] ${update.message}`
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
} : undefined;
|
|
117
|
+
// Use agent-specific LLM/model or fall back to parent's
|
|
118
|
+
const subLlm = agentConfig.llm || hive.config.llm;
|
|
119
|
+
const subHive = new Hive({
|
|
120
|
+
systemPrompt: agentConfig.systemPrompt,
|
|
121
|
+
tools: agentConfig.tools,
|
|
122
|
+
llm: subLlm,
|
|
123
|
+
logger: subLogger,
|
|
124
|
+
maxIterations: agentConfig.maxIterations || hive.config.maxIterations,
|
|
125
|
+
thinkingMode: hive.config.thinkingMode,
|
|
126
|
+
thinkingBudget: hive.config.thinkingBudget,
|
|
127
|
+
disableAskUser: true // Sub-agents return questions as text, not via __ask_user__
|
|
128
|
+
});
|
|
129
|
+
const result = await subHive.run(prompt);
|
|
130
|
+
// Log sub-agent completion
|
|
131
|
+
hive.config.logger?.info(`[Sub-Agent: ${agentName}] Completed (${result.toolCalls.length} tool calls)`);
|
|
132
|
+
// Progress: sub-agent completed
|
|
133
|
+
hive.config.logger?.onProgress?.({
|
|
134
|
+
type: 'sub_agent_end',
|
|
135
|
+
message: `${agentName} completed`,
|
|
136
|
+
details: { agentName, success: true }
|
|
137
|
+
});
|
|
138
|
+
if (result.status === 'needs_input') {
|
|
139
|
+
return {
|
|
140
|
+
success: false,
|
|
141
|
+
error: 'Sub-agent needs user input',
|
|
142
|
+
data: result.pendingQuestion
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return { success: true, data: result.response };
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
hive.config.logger?.error(`[Sub-Agent: ${agentName}] Failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
149
|
+
// Progress: sub-agent failed
|
|
150
|
+
hive.config.logger?.onProgress?.({
|
|
151
|
+
type: 'sub_agent_end',
|
|
152
|
+
message: `${agentName} failed`,
|
|
153
|
+
details: { agentName, success: false }
|
|
154
|
+
});
|
|
155
|
+
return {
|
|
156
|
+
success: false,
|
|
157
|
+
error: error instanceof Error ? error.message : String(error)
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Hive Agent Class
|
|
165
|
+
*/
|
|
166
|
+
export class Hive {
|
|
167
|
+
config;
|
|
168
|
+
contextManager;
|
|
169
|
+
tools;
|
|
170
|
+
constructor(config) {
|
|
171
|
+
this.config = {
|
|
172
|
+
maxIterations: 50,
|
|
173
|
+
maxContextTokens: 100000,
|
|
174
|
+
contextStrategy: 'truncate_old',
|
|
175
|
+
...config
|
|
176
|
+
};
|
|
177
|
+
this.contextManager = new ContextManager(this.config.maxContextTokens, this.config.contextStrategy);
|
|
178
|
+
// Build tools list with internal tools (todo tool added per-run)
|
|
179
|
+
this.tools = [...config.tools];
|
|
180
|
+
// Add __ask_user__ tool unless disabled (sub-agents shouldn't use it)
|
|
181
|
+
if (!config.disableAskUser) {
|
|
182
|
+
this.tools.push(createAskUserTool());
|
|
183
|
+
}
|
|
184
|
+
// Add __task__ tool if sub-agents are defined
|
|
185
|
+
if (config.agents && config.agents.length > 0) {
|
|
186
|
+
this.tools.push(createTaskTool(this, config.agents));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Get tools including internal tools for a specific run
|
|
191
|
+
*/
|
|
192
|
+
getRunTools(todoManager, reviewManager) {
|
|
193
|
+
const tools = [
|
|
194
|
+
...this.tools,
|
|
195
|
+
createTodoTool(todoManager)
|
|
196
|
+
];
|
|
197
|
+
if (reviewManager?.isEnabled()) {
|
|
198
|
+
tools.push(createReviewTool(reviewManager));
|
|
199
|
+
}
|
|
200
|
+
return tools;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Run the agent with a user message
|
|
204
|
+
*/
|
|
205
|
+
async run(message, options = {}) {
|
|
206
|
+
const { conversationId, userId, metadata, history: providedHistory, signal, shouldContinue } = options;
|
|
207
|
+
// Load history from repository or use provided
|
|
208
|
+
let history = [];
|
|
209
|
+
if (providedHistory) {
|
|
210
|
+
history = providedHistory;
|
|
211
|
+
}
|
|
212
|
+
else if (conversationId && this.config.repository) {
|
|
213
|
+
history = await this.config.repository.getHistory(conversationId);
|
|
214
|
+
}
|
|
215
|
+
// Check if last message was an __ask_user__ tool call (needs tool_result)
|
|
216
|
+
const messages = [...history];
|
|
217
|
+
const lastMessage = messages[messages.length - 1];
|
|
218
|
+
if (lastMessage?.role === 'assistant' && Array.isArray(lastMessage.content)) {
|
|
219
|
+
const askUserToolUse = lastMessage.content.find((block) => block.type === 'tool_use' && block.name === '__ask_user__');
|
|
220
|
+
if (askUserToolUse) {
|
|
221
|
+
// Add tool_result for the __ask_user__ call with user's response
|
|
222
|
+
messages.push({
|
|
223
|
+
role: 'user',
|
|
224
|
+
content: [{
|
|
225
|
+
type: 'tool_result',
|
|
226
|
+
tool_use_id: askUserToolUse.id,
|
|
227
|
+
content: JSON.stringify({ success: true, data: { answer: message } })
|
|
228
|
+
}]
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
// Normal user message
|
|
233
|
+
messages.push({ role: 'user', content: message });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
// Normal user message
|
|
238
|
+
messages.push({ role: 'user', content: message });
|
|
239
|
+
}
|
|
240
|
+
// Create todo manager for this run
|
|
241
|
+
const todoManager = new TodoManager();
|
|
242
|
+
// Create review manager if review is configured
|
|
243
|
+
const reviewManager = this.config.review
|
|
244
|
+
? new ReviewManager(this.config.review)
|
|
245
|
+
: undefined;
|
|
246
|
+
// Create tool context
|
|
247
|
+
const toolContext = {
|
|
248
|
+
remainingTokens: this.contextManager.getRemainingTokens(),
|
|
249
|
+
conversationId,
|
|
250
|
+
userId,
|
|
251
|
+
metadata
|
|
252
|
+
};
|
|
253
|
+
// Execute the agent loop
|
|
254
|
+
const result = await executeLoop({
|
|
255
|
+
systemPrompt: this.config.systemPrompt,
|
|
256
|
+
tools: this.getRunTools(todoManager, reviewManager),
|
|
257
|
+
llm: this.config.llm,
|
|
258
|
+
logger: this.config.logger,
|
|
259
|
+
maxIterations: this.config.maxIterations,
|
|
260
|
+
contextManager: this.contextManager,
|
|
261
|
+
todoManager,
|
|
262
|
+
reviewManager,
|
|
263
|
+
llmOptions: {
|
|
264
|
+
thinkingMode: this.config.thinkingMode,
|
|
265
|
+
thinkingBudget: this.config.thinkingBudget
|
|
266
|
+
},
|
|
267
|
+
signal,
|
|
268
|
+
shouldContinue
|
|
269
|
+
}, messages, toolContext);
|
|
270
|
+
// Save history to repository
|
|
271
|
+
if (conversationId && this.config.repository) {
|
|
272
|
+
await this.config.repository.saveHistory(conversationId, result.history);
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAYH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AACvD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAE7D;;GAEG;AACH,SAAS,iBAAiB;IACxB,OAAO;QACL,IAAI,EAAE,cAAc;QACpB,WAAW,EAAE;;;;;;;;;uEASsD;QACnE,UAAU,EAAE;YACV,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE;gBACV,QAAQ,EAAE;oBACR,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,8BAA8B;iBAC5C;gBACD,OAAO,EAAE;oBACP,IAAI,EAAE,QAAQ;oBACd,WAAW,EAAE,wCAAwC;iBACtD;aACF;YACD,QAAQ,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aAAa,EAAE,IAAI,CAAC,MAAM,CAAC,aAAc;YACzC,cAAc,EAAE,IAAI,CAAC,cAAc;YACnC,WAAW;YACX,aAAa;YACb,UAAU,EAAE;gBACV,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,YAAY;gBACtC,cAAc,EAAE,IAAI,CAAC,MAAM,CAAC,cAAc;aAC3C;YACD,MAAM;YACN,cAAc;SACf,EACD,QAAQ,EACR,WAAW,CACZ,CAAA;QAED,6BAA6B;QAC7B,IAAI,cAAc,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;YAC7C,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,cAAc,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;QAC1E,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Management
|
|
3
|
+
*
|
|
4
|
+
* Token estimation and context management utilities.
|
|
5
|
+
*/
|
|
6
|
+
import type { Message, ContentBlock, ContextStrategy } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Estimate token count from text (approximately 4 chars per token)
|
|
9
|
+
*/
|
|
10
|
+
export declare function estimateTokens(text: string): number;
|
|
11
|
+
/**
|
|
12
|
+
* Estimate tokens for a message
|
|
13
|
+
*/
|
|
14
|
+
export declare function estimateMessageTokens(message: Message): number;
|
|
15
|
+
/**
|
|
16
|
+
* Estimate tokens for a content block
|
|
17
|
+
*/
|
|
18
|
+
export declare function estimateContentBlockTokens(block: ContentBlock): number;
|
|
19
|
+
/**
|
|
20
|
+
* Estimate total tokens for all messages
|
|
21
|
+
*/
|
|
22
|
+
export declare function estimateTotalTokens(messages: Message[]): number;
|
|
23
|
+
/**
|
|
24
|
+
* Truncate old messages to fit within token limit
|
|
25
|
+
*/
|
|
26
|
+
export declare function truncateOldMessages(messages: Message[], maxTokens: number, preserveFirst?: number): Message[];
|
|
27
|
+
/**
|
|
28
|
+
* Context manager for tracking token usage during execution
|
|
29
|
+
*/
|
|
30
|
+
export declare class ContextManager {
|
|
31
|
+
private maxTokens;
|
|
32
|
+
private strategy;
|
|
33
|
+
private currentTokens;
|
|
34
|
+
constructor(maxTokens?: number, strategy?: ContextStrategy);
|
|
35
|
+
/**
|
|
36
|
+
* Update current token count
|
|
37
|
+
*/
|
|
38
|
+
updateTokenCount(messages: Message[]): void;
|
|
39
|
+
/**
|
|
40
|
+
* Get remaining tokens available
|
|
41
|
+
*/
|
|
42
|
+
getRemainingTokens(): number;
|
|
43
|
+
/**
|
|
44
|
+
* Check if context is within limits
|
|
45
|
+
*/
|
|
46
|
+
isWithinLimits(): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Manage context according to strategy
|
|
49
|
+
*/
|
|
50
|
+
manageContext(messages: Message[]): Message[];
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAExE;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQ9D;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAatE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,MAAM,CAE/D;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,OAAO,EAAE,EACnB,SAAS,EAAE,MAAM,EACjB,aAAa,GAAE,MAAU,GACxB,OAAO,EAAE,CA2BX;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,aAAa,CAAY;gBAErB,SAAS,GAAE,MAAe,EAAE,QAAQ,GAAE,eAAgC;IAKlF;;OAEG;IACH,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAI;IAI3C;;OAEG;IACH,kBAAkB,IAAI,MAAM;IAI5B;;OAEG;IACH,cAAc,IAAI,OAAO;IAIzB;;OAEG;IACH,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE;CAmB9C"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Management
|
|
3
|
+
*
|
|
4
|
+
* Token estimation and context management utilities.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Estimate token count from text (approximately 4 chars per token)
|
|
8
|
+
*/
|
|
9
|
+
export function estimateTokens(text) {
|
|
10
|
+
return Math.ceil(text.length / 4);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Estimate tokens for a message
|
|
14
|
+
*/
|
|
15
|
+
export function estimateMessageTokens(message) {
|
|
16
|
+
if (typeof message.content === 'string') {
|
|
17
|
+
return estimateTokens(message.content);
|
|
18
|
+
}
|
|
19
|
+
return message.content.reduce((sum, block) => {
|
|
20
|
+
return sum + estimateContentBlockTokens(block);
|
|
21
|
+
}, 0);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Estimate tokens for a content block
|
|
25
|
+
*/
|
|
26
|
+
export function estimateContentBlockTokens(block) {
|
|
27
|
+
switch (block.type) {
|
|
28
|
+
case 'text':
|
|
29
|
+
return estimateTokens(block.text);
|
|
30
|
+
case 'thinking':
|
|
31
|
+
return estimateTokens(block.thinking);
|
|
32
|
+
case 'tool_use':
|
|
33
|
+
return estimateTokens(block.name) + estimateTokens(JSON.stringify(block.input));
|
|
34
|
+
case 'tool_result':
|
|
35
|
+
return estimateTokens(block.content);
|
|
36
|
+
default:
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Estimate total tokens for all messages
|
|
42
|
+
*/
|
|
43
|
+
export function estimateTotalTokens(messages) {
|
|
44
|
+
return messages.reduce((sum, msg) => sum + estimateMessageTokens(msg), 0);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Truncate old messages to fit within token limit
|
|
48
|
+
*/
|
|
49
|
+
export function truncateOldMessages(messages, maxTokens, preserveFirst = 1) {
|
|
50
|
+
if (messages.length <= preserveFirst) {
|
|
51
|
+
return messages;
|
|
52
|
+
}
|
|
53
|
+
const result = [];
|
|
54
|
+
let totalTokens = 0;
|
|
55
|
+
// Always preserve first N messages (usually system context)
|
|
56
|
+
for (let i = 0; i < preserveFirst && i < messages.length; i++) {
|
|
57
|
+
result.push(messages[i]);
|
|
58
|
+
totalTokens += estimateMessageTokens(messages[i]);
|
|
59
|
+
}
|
|
60
|
+
// Add messages from the end until we hit the limit
|
|
61
|
+
const remaining = [];
|
|
62
|
+
for (let i = messages.length - 1; i >= preserveFirst; i--) {
|
|
63
|
+
const msgTokens = estimateMessageTokens(messages[i]);
|
|
64
|
+
if (totalTokens + msgTokens <= maxTokens) {
|
|
65
|
+
remaining.unshift(messages[i]);
|
|
66
|
+
totalTokens += msgTokens;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return [...result, ...remaining];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Context manager for tracking token usage during execution
|
|
76
|
+
*/
|
|
77
|
+
export class ContextManager {
|
|
78
|
+
maxTokens;
|
|
79
|
+
strategy;
|
|
80
|
+
currentTokens = 0;
|
|
81
|
+
constructor(maxTokens = 100000, strategy = 'truncate_old') {
|
|
82
|
+
this.maxTokens = maxTokens;
|
|
83
|
+
this.strategy = strategy;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Update current token count
|
|
87
|
+
*/
|
|
88
|
+
updateTokenCount(messages) {
|
|
89
|
+
this.currentTokens = estimateTotalTokens(messages);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get remaining tokens available
|
|
93
|
+
*/
|
|
94
|
+
getRemainingTokens() {
|
|
95
|
+
return Math.max(0, this.maxTokens - this.currentTokens);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if context is within limits
|
|
99
|
+
*/
|
|
100
|
+
isWithinLimits() {
|
|
101
|
+
return this.currentTokens <= this.maxTokens;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Manage context according to strategy
|
|
105
|
+
*/
|
|
106
|
+
manageContext(messages) {
|
|
107
|
+
this.updateTokenCount(messages);
|
|
108
|
+
if (this.isWithinLimits()) {
|
|
109
|
+
return messages;
|
|
110
|
+
}
|
|
111
|
+
switch (this.strategy) {
|
|
112
|
+
case 'truncate_old':
|
|
113
|
+
return truncateOldMessages(messages, this.maxTokens);
|
|
114
|
+
case 'summarize':
|
|
115
|
+
// Summarization would require LLM call - for now, fall back to truncation
|
|
116
|
+
return truncateOldMessages(messages, this.maxTokens);
|
|
117
|
+
case 'error':
|
|
118
|
+
throw new Error(`Context limit exceeded: ${this.currentTokens} > ${this.maxTokens} tokens`);
|
|
119
|
+
default:
|
|
120
|
+
return messages;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;AACnC,C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,GAAG,mBAAmB,CAAC,QAAQ,CAAC,CAAA;IACpD,CAAC;IAED;;OAEG;IACH,kBAAkB;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,CAAA;IACzD,CAAC;IAED;;OAEG;IACH,cAAc;QACZ,OAAO,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,SAAS,CAAA;IAC7C,CAAC;IAED;;OAEG;IACH,aAAa,CAAC,QAAmB;QAC/B,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAA;QAE/B,IAAI,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC;YAC1B,OAAO,QAAQ,CAAA;QACjB,CAAC;QAED,QAAQ,IAAI,CAAC,QAAQ,EAAE,CAAC;YACtB,KAAK,cAAc;gBACjB,OAAO,mBAAmB,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;YACtD,KAAK,WAAW;gBACd,0EAA0E;gBAC1E,OAAO,mBAAmB,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;YACtD,KAAK,OAAO;gBACV,MAAM,IAAI,KAAK,CAAC,2BAA2B,IAAI,CAAC,aAAa,MAAM,IAAI,CAAC,SAAS,SAAS,CAAC,CAAA;YAC7F;gBACE,OAAO,QAAQ,CAAA;QACnB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executor - Tool Execution Loop
|
|
3
|
+
*
|
|
4
|
+
* Core execution loop that runs tools and handles agent responses.
|
|
5
|
+
*/
|
|
6
|
+
import type { Message, Tool, ToolContext, LLMProvider, LLMOptions, LogProvider, AgentResult } from './types.js';
|
|
7
|
+
import { ContextManager } from './context.js';
|
|
8
|
+
import { TodoManager } from './todo.js';
|
|
9
|
+
import { ReviewManager } from './review.js';
|
|
10
|
+
export interface ExecutorConfig {
|
|
11
|
+
systemPrompt: string;
|
|
12
|
+
tools: Tool[];
|
|
13
|
+
llm: LLMProvider;
|
|
14
|
+
logger?: LogProvider;
|
|
15
|
+
maxIterations: number;
|
|
16
|
+
contextManager: ContextManager;
|
|
17
|
+
todoManager: TodoManager;
|
|
18
|
+
reviewManager?: ReviewManager;
|
|
19
|
+
llmOptions?: LLMOptions;
|
|
20
|
+
/** AbortSignal for cancellation */
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
/** Async function to check if should continue */
|
|
23
|
+
shouldContinue?: () => Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Main execution loop
|
|
27
|
+
*/
|
|
28
|
+
export declare function executeLoop(config: ExecutorConfig, initialMessages: Message[], toolContext: ToolContext): Promise<AgentResult>;
|
|
29
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../src/executor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EACV,OAAO,EACP,IAAI,EAIJ,WAAW,EAIX,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,EAEZ,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAI3C,MAAM,WAAW,cAAc;IAC7B,YAAY,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,IAAI,EAAE,CAAA;IACb,GAAG,EAAE,WAAW,CAAA;IAChB,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,aAAa,EAAE,MAAM,CAAA;IACrB,cAAc,EAAE,cAAc,CAAA;IAC9B,WAAW,EAAE,WAAW,CAAA;IACxB,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,UAAU,CAAC,EAAE,UAAU,CAAA;IAEvB,mCAAmC;IACnC,MAAM,CAAC,EAAE,WAAW,CAAA;IAEpB,iDAAiD;IACjD,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAA;CACxC;AAkOD;;GAEG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,cAAc,EACtB,eAAe,EAAE,OAAO,EAAE,EAC1B,WAAW,EAAE,WAAW,GACvB,OAAO,CAAC,WAAW,CAAC,CAuOtB"}
|