@agentionai/agents 0.3.0-beta
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 +517 -0
- package/dist/agents/Agent.d.ts +29 -0
- package/dist/agents/Agent.js +28 -0
- package/dist/agents/AgentConfig.d.ts +118 -0
- package/dist/agents/AgentConfig.js +3 -0
- package/dist/agents/AgentEvent.d.ts +18 -0
- package/dist/agents/AgentEvent.js +26 -0
- package/dist/agents/BaseAgent.d.ts +82 -0
- package/dist/agents/BaseAgent.js +121 -0
- package/dist/agents/anthropic/ClaudeAgent.d.ts +46 -0
- package/dist/agents/anthropic/ClaudeAgent.js +262 -0
- package/dist/agents/errors/AgentError.d.ts +47 -0
- package/dist/agents/errors/AgentError.js +74 -0
- package/dist/agents/google/GeminiAgent.d.ts +63 -0
- package/dist/agents/google/GeminiAgent.js +395 -0
- package/dist/agents/mistral/MistralAgent.d.ts +47 -0
- package/dist/agents/mistral/MistralAgent.js +313 -0
- package/dist/agents/model-types.d.ts +30 -0
- package/dist/agents/model-types.js +8 -0
- package/dist/agents/openai/OpenAiAgent.d.ts +48 -0
- package/dist/agents/openai/OpenAiAgent.js +338 -0
- package/dist/chunkers/Chunker.d.ts +53 -0
- package/dist/chunkers/Chunker.js +174 -0
- package/dist/chunkers/RecursiveChunker.d.ts +52 -0
- package/dist/chunkers/RecursiveChunker.js +166 -0
- package/dist/chunkers/TextChunker.d.ts +27 -0
- package/dist/chunkers/TextChunker.js +50 -0
- package/dist/chunkers/TokenChunker.d.ts +60 -0
- package/dist/chunkers/TokenChunker.js +176 -0
- package/dist/chunkers/index.d.ts +6 -0
- package/dist/chunkers/index.js +14 -0
- package/dist/chunkers/types.d.ts +95 -0
- package/dist/chunkers/types.js +3 -0
- package/dist/graph/AgentGraph.d.ts +99 -0
- package/dist/graph/AgentGraph.js +115 -0
- package/dist/graph/BaseExecutor.d.ts +86 -0
- package/dist/graph/BaseExecutor.js +61 -0
- package/dist/graph/GraphMetrics.d.ts +143 -0
- package/dist/graph/GraphMetrics.js +264 -0
- package/dist/graph/MapExecutor.d.ts +39 -0
- package/dist/graph/MapExecutor.js +123 -0
- package/dist/graph/ParallelExecutor.d.ts +51 -0
- package/dist/graph/ParallelExecutor.js +103 -0
- package/dist/graph/Pipeline.d.ts +44 -0
- package/dist/graph/Pipeline.js +109 -0
- package/dist/graph/RouterExecutor.d.ts +89 -0
- package/dist/graph/RouterExecutor.js +209 -0
- package/dist/graph/SequentialExecutor.d.ts +44 -0
- package/dist/graph/SequentialExecutor.js +115 -0
- package/dist/graph/VotingSystem.d.ts +54 -0
- package/dist/graph/VotingSystem.js +106 -0
- package/dist/history/History.d.ts +107 -0
- package/dist/history/History.js +166 -0
- package/dist/history/RedisHistory.d.ts +27 -0
- package/dist/history/RedisHistory.js +55 -0
- package/dist/history/transformers.d.ts +102 -0
- package/dist/history/transformers.js +415 -0
- package/dist/history/types.d.ts +130 -0
- package/dist/history/types.js +55 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +48 -0
- package/dist/ingestion/IngestionPipeline.d.ts +86 -0
- package/dist/ingestion/IngestionPipeline.js +266 -0
- package/dist/ingestion/index.d.ts +3 -0
- package/dist/ingestion/index.js +7 -0
- package/dist/ingestion/types.d.ts +74 -0
- package/dist/ingestion/types.js +3 -0
- package/dist/team/Team.d.ts +46 -0
- package/dist/team/Team.js +104 -0
- package/dist/tools/Tool.d.ts +75 -0
- package/dist/tools/Tool.js +137 -0
- package/dist/vectorstore/Embeddings.d.ts +67 -0
- package/dist/vectorstore/Embeddings.js +54 -0
- package/dist/vectorstore/LanceDBVectorStore.d.ts +149 -0
- package/dist/vectorstore/LanceDBVectorStore.js +338 -0
- package/dist/vectorstore/OpenAIEmbeddings.d.ts +45 -0
- package/dist/vectorstore/OpenAIEmbeddings.js +109 -0
- package/dist/vectorstore/VectorStore.d.ts +255 -0
- package/dist/vectorstore/VectorStore.js +216 -0
- package/dist/vectorstore/index.d.ts +28 -0
- package/dist/vectorstore/index.js +35 -0
- package/dist/viz/VizConfig.d.ts +54 -0
- package/dist/viz/VizConfig.js +100 -0
- package/dist/viz/VizReporter.d.ts +127 -0
- package/dist/viz/VizReporter.js +595 -0
- package/dist/viz/index.d.ts +31 -0
- package/dist/viz/index.js +51 -0
- package/dist/viz/types.d.ts +105 -0
- package/dist/viz/types.js +7 -0
- package/package.json +109 -0
- package/readme.md +1 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseAgent } from "./BaseAgent";
|
|
2
|
+
export declare class AgentEvent {
|
|
3
|
+
target: BaseAgent<any>;
|
|
4
|
+
static BEFORE_EXECUTE: string;
|
|
5
|
+
static AFTER_EXECUTE: string;
|
|
6
|
+
static DONE: string;
|
|
7
|
+
static TOOL_USE: string;
|
|
8
|
+
static ERROR: string;
|
|
9
|
+
static RETRY: string;
|
|
10
|
+
static MAX_RETRIES_EXCEEDED: string;
|
|
11
|
+
static MAX_TOKENS_EXCEEDED: string;
|
|
12
|
+
static TOOL_ERROR: string;
|
|
13
|
+
private defaultPrevented;
|
|
14
|
+
constructor(target: BaseAgent<any>);
|
|
15
|
+
preventDefault(): void;
|
|
16
|
+
get isDefaultPrevented(): boolean;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=AgentEvent.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentEvent = void 0;
|
|
4
|
+
class AgentEvent {
|
|
5
|
+
constructor(target) {
|
|
6
|
+
this.target = target;
|
|
7
|
+
this.defaultPrevented = false;
|
|
8
|
+
}
|
|
9
|
+
preventDefault() {
|
|
10
|
+
this.defaultPrevented = true;
|
|
11
|
+
}
|
|
12
|
+
get isDefaultPrevented() {
|
|
13
|
+
return this.defaultPrevented;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AgentEvent = AgentEvent;
|
|
17
|
+
AgentEvent.BEFORE_EXECUTE = "before_execute";
|
|
18
|
+
AgentEvent.AFTER_EXECUTE = "after_execute";
|
|
19
|
+
AgentEvent.DONE = "done";
|
|
20
|
+
AgentEvent.TOOL_USE = "toolUse";
|
|
21
|
+
AgentEvent.ERROR = "error";
|
|
22
|
+
AgentEvent.RETRY = "retry";
|
|
23
|
+
AgentEvent.MAX_RETRIES_EXCEEDED = "max_retries_exceeded";
|
|
24
|
+
AgentEvent.MAX_TOKENS_EXCEEDED = "max_tokens_exceeded";
|
|
25
|
+
AgentEvent.TOOL_ERROR = "tool_error";
|
|
26
|
+
//# sourceMappingURL=AgentEvent.js.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
2
|
+
import { Tool } from "../tools/Tool";
|
|
3
|
+
import { History, HistoryEntry, MessageRole, MessageContent } from "../history/History";
|
|
4
|
+
import { AgentVendor, CommonAgentConfig, VendorSpecificConfig } from "./AgentConfig";
|
|
5
|
+
export type { HistoryEntry, MessageRole, MessageContent };
|
|
6
|
+
export type { AgentVendor };
|
|
7
|
+
/**
|
|
8
|
+
* Agent config as used across all agents
|
|
9
|
+
* @deprecated Use CommonAgentConfig with vendorConfig instead
|
|
10
|
+
*/
|
|
11
|
+
export interface BaseAgentConfig extends CommonAgentConfig {
|
|
12
|
+
vendor: AgentVendor;
|
|
13
|
+
vendorConfig?: VendorSpecificConfig;
|
|
14
|
+
}
|
|
15
|
+
export type TokenUsage = {
|
|
16
|
+
input_tokens: number;
|
|
17
|
+
output_tokens: number;
|
|
18
|
+
total_tokens: number;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The base agent is what the other agents are inheriting from
|
|
22
|
+
* Handles the BaseConfig
|
|
23
|
+
*/
|
|
24
|
+
export declare abstract class BaseAgent<TInput = unknown, TOutput = unknown> extends EventEmitter {
|
|
25
|
+
protected history: History;
|
|
26
|
+
protected id: string;
|
|
27
|
+
protected debug: boolean;
|
|
28
|
+
protected name: string;
|
|
29
|
+
protected description: string;
|
|
30
|
+
protected tools: Map<string, Tool<unknown>>;
|
|
31
|
+
protected maxHistoryLength: number;
|
|
32
|
+
/** The vendor/provider for this agent (anthropic, openai, mistral, gemini) */
|
|
33
|
+
protected vendor: AgentVendor;
|
|
34
|
+
/** The model identifier for this agent */
|
|
35
|
+
protected model: string;
|
|
36
|
+
/**
|
|
37
|
+
* An Agent is the primary LLM entity.
|
|
38
|
+
*
|
|
39
|
+
* @param config
|
|
40
|
+
* @param history History is transient by default, meaning it will be cleared for every question.
|
|
41
|
+
* this makes hostory easier to manage and saves cost. If history should be kept in between
|
|
42
|
+
* prompts, then supply a History object.
|
|
43
|
+
*
|
|
44
|
+
*/
|
|
45
|
+
constructor(config: BaseAgentConfig, history?: History);
|
|
46
|
+
abstract execute(input: TInput): Promise<TOutput>;
|
|
47
|
+
protected abstract process(input: TInput): Promise<TOutput>;
|
|
48
|
+
protected abstract handleResponse(response: unknown): Promise<unknown>;
|
|
49
|
+
protected getToolDefinitions(): unknown[];
|
|
50
|
+
/**
|
|
51
|
+
* Add an entry to history
|
|
52
|
+
*/
|
|
53
|
+
protected addToHistory(entry: HistoryEntry): void;
|
|
54
|
+
/**
|
|
55
|
+
* Add a text message to history
|
|
56
|
+
*/
|
|
57
|
+
protected addTextToHistory(role: MessageRole, content: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Add system message to history if it doesn't already exist.
|
|
60
|
+
* Checks if the first system message matches the provided content.
|
|
61
|
+
*/
|
|
62
|
+
protected addSystemMessage(content: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Get the standard system message for this agent
|
|
65
|
+
*/
|
|
66
|
+
protected getSystemMessage(): string;
|
|
67
|
+
/**
|
|
68
|
+
* Add a message with content blocks to history
|
|
69
|
+
*/
|
|
70
|
+
protected addMessageToHistory(role: MessageRole, content: MessageContent[]): void;
|
|
71
|
+
addTools(tools: Tool<unknown>[]): void;
|
|
72
|
+
getId(): string;
|
|
73
|
+
getName(): string;
|
|
74
|
+
getDescription(): string;
|
|
75
|
+
getVendor(): AgentVendor;
|
|
76
|
+
getModel(): string;
|
|
77
|
+
getHistoryEntries(): HistoryEntry[];
|
|
78
|
+
getTools(): Tool<unknown>[];
|
|
79
|
+
clearHistory(): void;
|
|
80
|
+
protected abstract parseUsage(input: unknown): TokenUsage;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=BaseAgent.d.ts.map
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BaseAgent = void 0;
|
|
7
|
+
const events_1 = __importDefault(require("events"));
|
|
8
|
+
const Tool_1 = require("../tools/Tool");
|
|
9
|
+
const History_1 = require("../history/History");
|
|
10
|
+
/**
|
|
11
|
+
* The base agent is what the other agents are inheriting from
|
|
12
|
+
* Handles the BaseConfig
|
|
13
|
+
*/
|
|
14
|
+
class BaseAgent extends events_1.default {
|
|
15
|
+
/**
|
|
16
|
+
* An Agent is the primary LLM entity.
|
|
17
|
+
*
|
|
18
|
+
* @param config
|
|
19
|
+
* @param history History is transient by default, meaning it will be cleared for every question.
|
|
20
|
+
* this makes hostory easier to manage and saves cost. If history should be kept in between
|
|
21
|
+
* prompts, then supply a History object.
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
constructor(config, history = new History_1.History([], { transient: true })) {
|
|
25
|
+
super();
|
|
26
|
+
this.history = history;
|
|
27
|
+
this.debug = true;
|
|
28
|
+
this.id = config.id;
|
|
29
|
+
this.debug = config.debug || false;
|
|
30
|
+
this.name = config.name;
|
|
31
|
+
this.description = config.description;
|
|
32
|
+
this.vendor = config.vendor;
|
|
33
|
+
this.model = config.model || "unknown";
|
|
34
|
+
if (config.agents) {
|
|
35
|
+
const agentTools = config.agents.map((agent) => {
|
|
36
|
+
return Tool_1.Tool.fromAgent(agent, `You can use this agent ${agent.getName()} to execute tasks`);
|
|
37
|
+
});
|
|
38
|
+
config.tools = config.tools
|
|
39
|
+
? [...config.tools, ...agentTools]
|
|
40
|
+
: agentTools;
|
|
41
|
+
}
|
|
42
|
+
this.tools = new Map((config.tools || []).map((tool) => [tool.name, tool]));
|
|
43
|
+
this.maxHistoryLength = config.maxHistoryLength || 100;
|
|
44
|
+
}
|
|
45
|
+
getToolDefinitions() {
|
|
46
|
+
return Array.from(this.tools.values()).map((tool) => tool.getPrompt());
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Add an entry to history
|
|
50
|
+
*/
|
|
51
|
+
addToHistory(entry) {
|
|
52
|
+
this.history.addEntry(entry);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Add a text message to history
|
|
56
|
+
*/
|
|
57
|
+
addTextToHistory(role, content) {
|
|
58
|
+
this.history.addText(role, content);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Add system message to history if it doesn't already exist.
|
|
62
|
+
* Checks if the first system message matches the provided content.
|
|
63
|
+
*/
|
|
64
|
+
addSystemMessage(content) {
|
|
65
|
+
const existingSystem = this.history.getSystemMessage();
|
|
66
|
+
if (existingSystem === content) {
|
|
67
|
+
// System message already exists with same content, skip
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.history.addSystem(content);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get the standard system message for this agent
|
|
74
|
+
*/
|
|
75
|
+
getSystemMessage() {
|
|
76
|
+
return `You are an agent called ${this.getName()} and should follow these instructions: ${this.getDescription()}`;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Add a message with content blocks to history
|
|
80
|
+
*/
|
|
81
|
+
addMessageToHistory(role, content) {
|
|
82
|
+
this.history.addMessage(role, content);
|
|
83
|
+
}
|
|
84
|
+
addTools(tools) {
|
|
85
|
+
tools.forEach((tool) => {
|
|
86
|
+
if (!this.tools.has(tool.name)) {
|
|
87
|
+
this.tools.set(tool.name, tool);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error(`AddTool: Tool ${tool.name} already exists for ${this.getName()}`);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
// Getters
|
|
95
|
+
getId() {
|
|
96
|
+
return this.id;
|
|
97
|
+
}
|
|
98
|
+
getName() {
|
|
99
|
+
return this.name;
|
|
100
|
+
}
|
|
101
|
+
getDescription() {
|
|
102
|
+
return this.description;
|
|
103
|
+
}
|
|
104
|
+
getVendor() {
|
|
105
|
+
return this.vendor;
|
|
106
|
+
}
|
|
107
|
+
getModel() {
|
|
108
|
+
return this.model;
|
|
109
|
+
}
|
|
110
|
+
getHistoryEntries() {
|
|
111
|
+
return this.history.entries;
|
|
112
|
+
}
|
|
113
|
+
getTools() {
|
|
114
|
+
return [...this.tools.values()];
|
|
115
|
+
}
|
|
116
|
+
clearHistory() {
|
|
117
|
+
this.history.clear();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.BaseAgent = BaseAgent;
|
|
121
|
+
//# sourceMappingURL=BaseAgent.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Message, Usage } from "@anthropic-ai/sdk/resources";
|
|
2
|
+
import { type ToolDefinition } from "../../tools/Tool";
|
|
3
|
+
import { BaseAgent, BaseAgentConfig, TokenUsage } from "../BaseAgent";
|
|
4
|
+
import { History } from "../../history/History";
|
|
5
|
+
import { ClaudeModel } from "../model-types";
|
|
6
|
+
type AgentConfig = BaseAgentConfig & {
|
|
7
|
+
apiKey: string;
|
|
8
|
+
model?: ClaudeModel;
|
|
9
|
+
maxTokens?: number;
|
|
10
|
+
disableParallelToolUse?: boolean;
|
|
11
|
+
metadata?: Record<string, string>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Agent for Anthropic models.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const agent = new ClaudeAgent({
|
|
19
|
+
* id: "1",
|
|
20
|
+
* name: "Assistant",
|
|
21
|
+
* description: "A helpful assistant",
|
|
22
|
+
* apiKey: process.env.ANTHROPIC_API_KEY,
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const response = await agent.execute("Hello!");
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class ClaudeAgent extends BaseAgent {
|
|
29
|
+
private client;
|
|
30
|
+
protected config: Partial<AgentConfig>;
|
|
31
|
+
/** Token usage from the last execution (for metrics tracking) */
|
|
32
|
+
lastTokenUsage?: TokenUsage;
|
|
33
|
+
/** Current visualization event ID for tracking */
|
|
34
|
+
private vizEventId?;
|
|
35
|
+
/** Count of tool calls in current execution */
|
|
36
|
+
private currentToolCallCount;
|
|
37
|
+
constructor(config: Omit<AgentConfig, "vendor">, history?: History);
|
|
38
|
+
protected getToolDefinitions(): ToolDefinition[];
|
|
39
|
+
protected process(_input: string): Promise<string>;
|
|
40
|
+
execute(input: string): Promise<string>;
|
|
41
|
+
protected handleResponse(response: Message): Promise<string>;
|
|
42
|
+
private handleToolUse;
|
|
43
|
+
protected parseUsage(input: Usage): TokenUsage;
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=ClaudeAgent.d.ts.map
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClaudeAgent = void 0;
|
|
4
|
+
const sdk_1 = require("@anthropic-ai/sdk");
|
|
5
|
+
const BaseAgent_1 = require("../BaseAgent");
|
|
6
|
+
const AgentEvent_1 = require("../AgentEvent");
|
|
7
|
+
const AgentError_1 = require("../errors/AgentError");
|
|
8
|
+
const History_1 = require("../../history/History");
|
|
9
|
+
const transformers_1 = require("../../history/transformers");
|
|
10
|
+
const VizReporter_1 = require("../../viz/VizReporter");
|
|
11
|
+
const VizConfig_1 = require("../../viz/VizConfig");
|
|
12
|
+
/**
|
|
13
|
+
* Agent for Anthropic models.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const agent = new ClaudeAgent({
|
|
18
|
+
* id: "1",
|
|
19
|
+
* name: "Assistant",
|
|
20
|
+
* description: "A helpful assistant",
|
|
21
|
+
* apiKey: process.env.ANTHROPIC_API_KEY,
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* const response = await agent.execute("Hello!");
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class ClaudeAgent extends BaseAgent_1.BaseAgent {
|
|
28
|
+
constructor(config, history) {
|
|
29
|
+
super({ ...config, vendor: "anthropic" }, history);
|
|
30
|
+
/** Count of tool calls in current execution */
|
|
31
|
+
this.currentToolCallCount = 0;
|
|
32
|
+
this.client = new sdk_1.Anthropic({
|
|
33
|
+
apiKey: config.apiKey,
|
|
34
|
+
});
|
|
35
|
+
// Merge flat config (deprecated) with nested vendorConfig
|
|
36
|
+
// Flat config takes precedence for backward compatibility
|
|
37
|
+
const vendorConfig = config.vendorConfig?.anthropic || {};
|
|
38
|
+
const disableParallelToolUse = config.disableParallelToolUse ??
|
|
39
|
+
vendorConfig.disableParallelToolUse ??
|
|
40
|
+
false;
|
|
41
|
+
const metadata = config.metadata ?? vendorConfig.metadata;
|
|
42
|
+
this.config = {
|
|
43
|
+
model: config.model || "claude-3-5-haiku-latest",
|
|
44
|
+
maxTokens: config.maxTokens || 1024,
|
|
45
|
+
disableParallelToolUse,
|
|
46
|
+
metadata,
|
|
47
|
+
apiKey: config.apiKey,
|
|
48
|
+
temperature: config.temperature,
|
|
49
|
+
topP: config.topP,
|
|
50
|
+
topK: config.topK,
|
|
51
|
+
stopSequences: config.stopSequences,
|
|
52
|
+
};
|
|
53
|
+
// Add system message to history (skips if already exists with same content)
|
|
54
|
+
this.addSystemMessage(this.getSystemMessage());
|
|
55
|
+
}
|
|
56
|
+
getToolDefinitions() {
|
|
57
|
+
return Array.from(this.tools.values()).map((tool) => tool.getPrompt());
|
|
58
|
+
}
|
|
59
|
+
async process(_input) {
|
|
60
|
+
return "";
|
|
61
|
+
}
|
|
62
|
+
async execute(input) {
|
|
63
|
+
this.emit(AgentEvent_1.AgentEvent.BEFORE_EXECUTE, input);
|
|
64
|
+
// Reset token usage for this execution
|
|
65
|
+
this.lastTokenUsage = undefined;
|
|
66
|
+
this.currentToolCallCount = 0;
|
|
67
|
+
// Start visualization reporting
|
|
68
|
+
if (VizConfig_1.vizConfig.isEnabled()) {
|
|
69
|
+
this.vizEventId = VizReporter_1.vizReporter.agentStart(this.id, this.name, this.config.model, "anthropic", input);
|
|
70
|
+
}
|
|
71
|
+
if (this.history.transient) {
|
|
72
|
+
this.history.clear();
|
|
73
|
+
// Re-add system message after clear
|
|
74
|
+
this.addSystemMessage(this.getSystemMessage());
|
|
75
|
+
}
|
|
76
|
+
this.addTextToHistory("user", input);
|
|
77
|
+
try {
|
|
78
|
+
const messages = transformers_1.anthropicTransformer.toProvider(this.history.entries);
|
|
79
|
+
const systemMessage = this.history.getSystemMessage();
|
|
80
|
+
const response = await this.client.messages.create({
|
|
81
|
+
model: this.config.model,
|
|
82
|
+
system: systemMessage,
|
|
83
|
+
max_tokens: this.config.maxTokens,
|
|
84
|
+
messages,
|
|
85
|
+
tools: this.getToolDefinitions(),
|
|
86
|
+
temperature: this.config.temperature,
|
|
87
|
+
top_p: this.config.topP,
|
|
88
|
+
top_k: this.config.topK,
|
|
89
|
+
stop_sequences: this.config.stopSequences,
|
|
90
|
+
metadata: this.config.metadata,
|
|
91
|
+
});
|
|
92
|
+
this.emit(AgentEvent_1.AgentEvent.AFTER_EXECUTE, response);
|
|
93
|
+
return await this.handleResponse(response);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (error instanceof sdk_1.APIError) {
|
|
97
|
+
const apiError = new AgentError_1.ApiError(`Anthropic API error: ${error.message}`, error.status, error);
|
|
98
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, apiError);
|
|
99
|
+
// Report error to viz
|
|
100
|
+
if (this.vizEventId) {
|
|
101
|
+
VizReporter_1.vizReporter.agentError(this.vizEventId, "ApiError", apiError.message, error.status === 429 // Rate limit is retryable
|
|
102
|
+
);
|
|
103
|
+
this.vizEventId = undefined;
|
|
104
|
+
}
|
|
105
|
+
throw apiError;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const executionError = new AgentError_1.ExecutionError(`Error executing agent: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
109
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, executionError);
|
|
110
|
+
// Report error to viz
|
|
111
|
+
if (this.vizEventId) {
|
|
112
|
+
VizReporter_1.vizReporter.agentError(this.vizEventId, "ExecutionError", executionError.message, false);
|
|
113
|
+
this.vizEventId = undefined;
|
|
114
|
+
}
|
|
115
|
+
throw executionError;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async handleResponse(response) {
|
|
120
|
+
const usage = this.parseUsage(response.usage);
|
|
121
|
+
// Store token usage for metrics tracking
|
|
122
|
+
if (this.lastTokenUsage) {
|
|
123
|
+
this.lastTokenUsage.input_tokens += usage.input_tokens;
|
|
124
|
+
this.lastTokenUsage.output_tokens += usage.output_tokens;
|
|
125
|
+
this.lastTokenUsage.total_tokens += usage.total_tokens;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.lastTokenUsage = { ...usage };
|
|
129
|
+
}
|
|
130
|
+
if (response.stop_reason === "max_tokens") {
|
|
131
|
+
const error = new AgentError_1.MaxTokensExceededError("Response exceeded maximum token limit", this.config.maxTokens || 1024);
|
|
132
|
+
this.emit(AgentEvent_1.AgentEvent.MAX_TOKENS_EXCEEDED, error);
|
|
133
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, error);
|
|
134
|
+
// Report error to viz
|
|
135
|
+
if (this.vizEventId) {
|
|
136
|
+
VizReporter_1.vizReporter.agentError(this.vizEventId, "MaxTokensExceededError", error.message, false);
|
|
137
|
+
this.vizEventId = undefined;
|
|
138
|
+
}
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
if (response.stop_reason !== "tool_use") {
|
|
142
|
+
if (response.content && response.content[0]?.type === "text") {
|
|
143
|
+
this.emit(AgentEvent_1.AgentEvent.DONE, response, usage);
|
|
144
|
+
// Convert response to normalized format and add to history
|
|
145
|
+
const entry = transformers_1.anthropicTransformer.fromProviderContent("assistant", response.content);
|
|
146
|
+
this.addToHistory(entry);
|
|
147
|
+
// Report completion to viz
|
|
148
|
+
if (this.vizEventId) {
|
|
149
|
+
VizReporter_1.vizReporter.agentComplete(this.vizEventId, {
|
|
150
|
+
input: this.lastTokenUsage?.input_tokens || 0,
|
|
151
|
+
output: this.lastTokenUsage?.output_tokens || 0,
|
|
152
|
+
total: this.lastTokenUsage?.total_tokens || 0,
|
|
153
|
+
}, "end_turn", this.currentToolCallCount > 0, this.currentToolCallCount, response.content[0].text);
|
|
154
|
+
this.vizEventId = undefined;
|
|
155
|
+
}
|
|
156
|
+
return response.content[0].text;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const error = new AgentError_1.ExecutionError(`Unexpected response format: ${JSON.stringify(response.content)}`);
|
|
160
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, error);
|
|
161
|
+
// Report error to viz
|
|
162
|
+
if (this.vizEventId) {
|
|
163
|
+
VizReporter_1.vizReporter.agentError(this.vizEventId, "ExecutionError", error.message, false);
|
|
164
|
+
this.vizEventId = undefined;
|
|
165
|
+
}
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else if (response.stop_reason === "tool_use") {
|
|
170
|
+
try {
|
|
171
|
+
this.emit(AgentEvent_1.AgentEvent.TOOL_USE, response.content);
|
|
172
|
+
// Add assistant response to history (normalized format)
|
|
173
|
+
const assistantEntry = transformers_1.anthropicTransformer.fromProviderContent("assistant", response.content);
|
|
174
|
+
this.addToHistory(assistantEntry);
|
|
175
|
+
const toolResults = await this.handleToolUse(response.content);
|
|
176
|
+
// Add tool results to history (normalized format)
|
|
177
|
+
this.addMessageToHistory("user", toolResults);
|
|
178
|
+
// Continue conversation with tool results
|
|
179
|
+
try {
|
|
180
|
+
const messages = transformers_1.anthropicTransformer.toProvider(this.history.entries);
|
|
181
|
+
const newResponse = await this.client.messages.create({
|
|
182
|
+
model: this.config.model,
|
|
183
|
+
max_tokens: this.config.maxTokens,
|
|
184
|
+
messages,
|
|
185
|
+
tools: this.getToolDefinitions(),
|
|
186
|
+
temperature: this.config.temperature,
|
|
187
|
+
top_p: this.config.topP,
|
|
188
|
+
top_k: this.config.topK,
|
|
189
|
+
stop_sequences: this.config.stopSequences,
|
|
190
|
+
metadata: this.config.metadata,
|
|
191
|
+
});
|
|
192
|
+
this.emit(AgentEvent_1.AgentEvent.AFTER_EXECUTE, newResponse);
|
|
193
|
+
return this.handleResponse(newResponse);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (error instanceof sdk_1.APIError) {
|
|
197
|
+
const apiError = new AgentError_1.ApiError(`Anthropic API error during tool response: ${error.message}`, error.status, error);
|
|
198
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, apiError);
|
|
199
|
+
throw apiError;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
throw new AgentError_1.ExecutionError(`Error handling tool response: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
if (error instanceof AgentError_1.ToolExecutionError) {
|
|
208
|
+
this.emit(AgentEvent_1.AgentEvent.TOOL_ERROR, error);
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
const toolError = new AgentError_1.ExecutionError(`Error during tool execution: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
212
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, toolError);
|
|
213
|
+
throw toolError;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const error = new AgentError_1.ExecutionError(`Unexpected stop_reason: ${response.stop_reason}`);
|
|
217
|
+
this.emit(AgentEvent_1.AgentEvent.ERROR, error);
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
async handleToolUse(content) {
|
|
221
|
+
const toolUseBlocks = content.filter((block) => block.type === "tool_use");
|
|
222
|
+
if (!toolUseBlocks.length) {
|
|
223
|
+
throw new AgentError_1.ExecutionError("No tool use blocks found in content");
|
|
224
|
+
}
|
|
225
|
+
// Track tool call count for viz reporting
|
|
226
|
+
this.currentToolCallCount += toolUseBlocks.length;
|
|
227
|
+
const results = await Promise.all(toolUseBlocks.map(async (block) => {
|
|
228
|
+
const tool = this.tools.get(block.name);
|
|
229
|
+
if (!tool) {
|
|
230
|
+
const errorMessage = `Tool '${block.name}' not found`;
|
|
231
|
+
const error = new AgentError_1.ToolExecutionError(errorMessage, block.name, block.input);
|
|
232
|
+
if (this.debug) {
|
|
233
|
+
console.error(error);
|
|
234
|
+
}
|
|
235
|
+
return (0, History_1.toolResult)(block.id, errorMessage, true);
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
const result = await tool.execute(this.getId(), this.getName(), block.input, block.id, this.config.model, "anthropic");
|
|
239
|
+
return (0, History_1.toolResult)(block.id, JSON.stringify(result));
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
const errorMessage = `Error executing tool '${block.name}': ${error instanceof Error ? error.message : "Unknown error"}`;
|
|
243
|
+
if (this.debug) {
|
|
244
|
+
console.error(errorMessage);
|
|
245
|
+
}
|
|
246
|
+
const toolError = new AgentError_1.ToolExecutionError(errorMessage, block.name, block.input);
|
|
247
|
+
this.emit(AgentEvent_1.AgentEvent.TOOL_ERROR, toolError);
|
|
248
|
+
return (0, History_1.toolResult)(block.id, errorMessage, true);
|
|
249
|
+
}
|
|
250
|
+
}));
|
|
251
|
+
return results;
|
|
252
|
+
}
|
|
253
|
+
parseUsage(input) {
|
|
254
|
+
return {
|
|
255
|
+
input_tokens: input.input_tokens,
|
|
256
|
+
output_tokens: input.output_tokens,
|
|
257
|
+
total_tokens: input.input_tokens + input.output_tokens,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
exports.ClaudeAgent = ClaudeAgent;
|
|
262
|
+
//# sourceMappingURL=ClaudeAgent.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for all agent-related errors
|
|
3
|
+
*/
|
|
4
|
+
export declare class AgentError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* @param message Error message
|
|
7
|
+
* @param options Additional error options
|
|
8
|
+
*/
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when an agent's execution fails
|
|
13
|
+
*/
|
|
14
|
+
export declare class ExecutionError extends AgentError {
|
|
15
|
+
constructor(message: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Error thrown when LLM API request fails
|
|
19
|
+
*/
|
|
20
|
+
export declare class ApiError extends AgentError {
|
|
21
|
+
statusCode?: number | undefined;
|
|
22
|
+
response?: any | undefined;
|
|
23
|
+
constructor(message: string, statusCode?: number | undefined, response?: any | undefined);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Error thrown when maximum token limit is exceeded
|
|
27
|
+
*/
|
|
28
|
+
export declare class MaxTokensExceededError extends AgentError {
|
|
29
|
+
tokenLimit: number;
|
|
30
|
+
constructor(message: string, tokenLimit: number);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Error thrown when maximum retries are exceeded
|
|
34
|
+
*/
|
|
35
|
+
export declare class MaxRetriesExceededError extends AgentError {
|
|
36
|
+
maxRetries: number;
|
|
37
|
+
constructor(message: string, maxRetries: number);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Error thrown when tool execution fails
|
|
41
|
+
*/
|
|
42
|
+
export declare class ToolExecutionError extends AgentError {
|
|
43
|
+
toolName: string;
|
|
44
|
+
input: any;
|
|
45
|
+
constructor(message: string, toolName: string, input: any);
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=AgentError.d.ts.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToolExecutionError = exports.MaxRetriesExceededError = exports.MaxTokensExceededError = exports.ApiError = exports.ExecutionError = exports.AgentError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for all agent-related errors
|
|
6
|
+
*/
|
|
7
|
+
class AgentError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* @param message Error message
|
|
10
|
+
* @param options Additional error options
|
|
11
|
+
*/
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "AgentError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.AgentError = AgentError;
|
|
18
|
+
/**
|
|
19
|
+
* Error thrown when an agent's execution fails
|
|
20
|
+
*/
|
|
21
|
+
class ExecutionError extends AgentError {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "ExecutionError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.ExecutionError = ExecutionError;
|
|
28
|
+
/**
|
|
29
|
+
* Error thrown when LLM API request fails
|
|
30
|
+
*/
|
|
31
|
+
class ApiError extends AgentError {
|
|
32
|
+
constructor(message, statusCode, response) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.statusCode = statusCode;
|
|
35
|
+
this.response = response;
|
|
36
|
+
this.name = "ApiError";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.ApiError = ApiError;
|
|
40
|
+
/**
|
|
41
|
+
* Error thrown when maximum token limit is exceeded
|
|
42
|
+
*/
|
|
43
|
+
class MaxTokensExceededError extends AgentError {
|
|
44
|
+
constructor(message, tokenLimit) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.tokenLimit = tokenLimit;
|
|
47
|
+
this.name = "MaxTokensExceededError";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.MaxTokensExceededError = MaxTokensExceededError;
|
|
51
|
+
/**
|
|
52
|
+
* Error thrown when maximum retries are exceeded
|
|
53
|
+
*/
|
|
54
|
+
class MaxRetriesExceededError extends AgentError {
|
|
55
|
+
constructor(message, maxRetries) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.maxRetries = maxRetries;
|
|
58
|
+
this.name = "MaxRetriesExceededError";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.MaxRetriesExceededError = MaxRetriesExceededError;
|
|
62
|
+
/**
|
|
63
|
+
* Error thrown when tool execution fails
|
|
64
|
+
*/
|
|
65
|
+
class ToolExecutionError extends AgentError {
|
|
66
|
+
constructor(message, toolName, input) {
|
|
67
|
+
super(message);
|
|
68
|
+
this.toolName = toolName;
|
|
69
|
+
this.input = input;
|
|
70
|
+
this.name = "ToolExecutionError";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.ToolExecutionError = ToolExecutionError;
|
|
74
|
+
//# sourceMappingURL=AgentError.js.map
|