praisonai 1.0.18 → 1.1.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/agent/context.d.ts +68 -0
- package/dist/agent/context.js +119 -0
- package/dist/agent/enhanced.d.ts +92 -0
- package/dist/agent/enhanced.js +267 -0
- package/dist/agent/handoff.d.ts +82 -0
- package/dist/agent/handoff.js +124 -0
- package/dist/agent/router.d.ts +77 -0
- package/dist/agent/router.js +113 -0
- package/dist/agent/simple.d.ts +1 -1
- package/dist/agent/simple.js +40 -4
- package/dist/agent/types.js +2 -2
- package/dist/cli/index.d.ts +20 -0
- package/dist/cli/index.js +150 -0
- package/dist/db/index.d.ts +23 -0
- package/dist/db/index.js +72 -0
- package/dist/db/memory-adapter.d.ts +42 -0
- package/dist/db/memory-adapter.js +146 -0
- package/dist/db/types.d.ts +113 -0
- package/dist/db/types.js +5 -0
- package/dist/eval/index.d.ts +61 -0
- package/dist/eval/index.js +157 -0
- package/dist/guardrails/index.d.ts +82 -0
- package/dist/guardrails/index.js +202 -0
- package/dist/index.d.ts +16 -1
- package/dist/index.js +72 -1
- package/dist/knowledge/rag.d.ts +80 -0
- package/dist/knowledge/rag.js +147 -0
- package/dist/llm/openai.js +11 -3
- package/dist/llm/providers/anthropic.d.ts +33 -0
- package/dist/llm/providers/anthropic.js +291 -0
- package/dist/llm/providers/base.d.ts +25 -0
- package/dist/llm/providers/base.js +43 -0
- package/dist/llm/providers/google.d.ts +27 -0
- package/dist/llm/providers/google.js +275 -0
- package/dist/llm/providers/index.d.ts +43 -0
- package/dist/llm/providers/index.js +116 -0
- package/dist/llm/providers/openai.d.ts +18 -0
- package/dist/llm/providers/openai.js +203 -0
- package/dist/llm/providers/types.d.ts +94 -0
- package/dist/llm/providers/types.js +5 -0
- package/dist/observability/index.d.ts +86 -0
- package/dist/observability/index.js +166 -0
- package/dist/session/index.d.ts +111 -0
- package/dist/session/index.js +250 -0
- package/dist/skills/index.d.ts +70 -0
- package/dist/skills/index.js +233 -0
- package/dist/tools/decorator.d.ts +91 -0
- package/dist/tools/decorator.js +165 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/mcpSse.d.ts +41 -0
- package/dist/tools/mcpSse.js +108 -0
- package/dist/workflows/index.d.ts +97 -0
- package/dist/workflows/index.js +216 -0
- package/package.json +6 -2
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Agent - Agent with enhanced context management
|
|
3
|
+
*/
|
|
4
|
+
import { KnowledgeBase, type SearchResult } from '../knowledge/rag';
|
|
5
|
+
export interface ContextAgentConfig {
|
|
6
|
+
name?: string;
|
|
7
|
+
instructions: string;
|
|
8
|
+
llm?: string;
|
|
9
|
+
knowledgeBase?: KnowledgeBase;
|
|
10
|
+
contextWindow?: number;
|
|
11
|
+
maxContextTokens?: number;
|
|
12
|
+
verbose?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface ContextMessage {
|
|
15
|
+
role: 'system' | 'user' | 'assistant';
|
|
16
|
+
content: string;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
metadata?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Context Agent - Agent with RAG and context management
|
|
22
|
+
*/
|
|
23
|
+
export declare class ContextAgent {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly instructions: string;
|
|
26
|
+
private provider;
|
|
27
|
+
private knowledgeBase?;
|
|
28
|
+
private contextWindow;
|
|
29
|
+
private maxContextTokens;
|
|
30
|
+
private messages;
|
|
31
|
+
private verbose;
|
|
32
|
+
constructor(config: ContextAgentConfig);
|
|
33
|
+
/**
|
|
34
|
+
* Chat with context awareness
|
|
35
|
+
*/
|
|
36
|
+
chat(prompt: string): Promise<{
|
|
37
|
+
text: string;
|
|
38
|
+
context?: SearchResult[];
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* Get recent messages within context window
|
|
42
|
+
*/
|
|
43
|
+
private getRecentMessages;
|
|
44
|
+
/**
|
|
45
|
+
* Add document to knowledge base
|
|
46
|
+
*/
|
|
47
|
+
addDocument(id: string, content: string, metadata?: Record<string, any>): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Search knowledge base
|
|
50
|
+
*/
|
|
51
|
+
searchKnowledge(query: string, limit?: number): Promise<SearchResult[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Clear conversation history
|
|
54
|
+
*/
|
|
55
|
+
clearHistory(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Get conversation history
|
|
58
|
+
*/
|
|
59
|
+
getHistory(): ContextMessage[];
|
|
60
|
+
/**
|
|
61
|
+
* Set knowledge base
|
|
62
|
+
*/
|
|
63
|
+
setKnowledgeBase(kb: KnowledgeBase): void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a context agent
|
|
67
|
+
*/
|
|
68
|
+
export declare function createContextAgent(config: ContextAgentConfig): ContextAgent;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Context Agent - Agent with enhanced context management
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ContextAgent = void 0;
|
|
7
|
+
exports.createContextAgent = createContextAgent;
|
|
8
|
+
const providers_1 = require("../llm/providers");
|
|
9
|
+
/**
|
|
10
|
+
* Context Agent - Agent with RAG and context management
|
|
11
|
+
*/
|
|
12
|
+
class ContextAgent {
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.messages = [];
|
|
15
|
+
this.name = config.name || `ContextAgent_${Math.random().toString(36).substr(2, 9)}`;
|
|
16
|
+
this.instructions = config.instructions;
|
|
17
|
+
this.provider = (0, providers_1.createProvider)(config.llm || 'gpt-4o-mini');
|
|
18
|
+
this.knowledgeBase = config.knowledgeBase;
|
|
19
|
+
this.contextWindow = config.contextWindow ?? 10;
|
|
20
|
+
this.maxContextTokens = config.maxContextTokens ?? 4000;
|
|
21
|
+
this.verbose = config.verbose ?? false;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Chat with context awareness
|
|
25
|
+
*/
|
|
26
|
+
async chat(prompt) {
|
|
27
|
+
// Add user message
|
|
28
|
+
this.messages.push({
|
|
29
|
+
role: 'user',
|
|
30
|
+
content: prompt,
|
|
31
|
+
timestamp: Date.now()
|
|
32
|
+
});
|
|
33
|
+
// Build context from knowledge base
|
|
34
|
+
let ragContext = [];
|
|
35
|
+
let contextString = '';
|
|
36
|
+
if (this.knowledgeBase) {
|
|
37
|
+
ragContext = await this.knowledgeBase.search(prompt);
|
|
38
|
+
if (ragContext.length > 0) {
|
|
39
|
+
contextString = this.knowledgeBase.buildContext(ragContext);
|
|
40
|
+
if (this.verbose) {
|
|
41
|
+
console.log(`[ContextAgent] Found ${ragContext.length} relevant documents`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Build messages array with context
|
|
46
|
+
const systemPrompt = contextString
|
|
47
|
+
? `${this.instructions}\n\nRelevant context:\n${contextString}`
|
|
48
|
+
: this.instructions;
|
|
49
|
+
const messages = [
|
|
50
|
+
{ role: 'system', content: systemPrompt },
|
|
51
|
+
...this.getRecentMessages()
|
|
52
|
+
];
|
|
53
|
+
// Generate response
|
|
54
|
+
const result = await this.provider.generateText({ messages });
|
|
55
|
+
// Add assistant message
|
|
56
|
+
this.messages.push({
|
|
57
|
+
role: 'assistant',
|
|
58
|
+
content: result.text,
|
|
59
|
+
timestamp: Date.now()
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
text: result.text,
|
|
63
|
+
context: ragContext.length > 0 ? ragContext : undefined
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get recent messages within context window
|
|
68
|
+
*/
|
|
69
|
+
getRecentMessages() {
|
|
70
|
+
const recent = this.messages.slice(-this.contextWindow);
|
|
71
|
+
return recent.map(m => ({
|
|
72
|
+
role: m.role,
|
|
73
|
+
content: m.content
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Add document to knowledge base
|
|
78
|
+
*/
|
|
79
|
+
async addDocument(id, content, metadata) {
|
|
80
|
+
if (!this.knowledgeBase) {
|
|
81
|
+
throw new Error('No knowledge base configured');
|
|
82
|
+
}
|
|
83
|
+
await this.knowledgeBase.add({ id, content, metadata });
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Search knowledge base
|
|
87
|
+
*/
|
|
88
|
+
async searchKnowledge(query, limit) {
|
|
89
|
+
if (!this.knowledgeBase) {
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
return this.knowledgeBase.search(query, limit);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Clear conversation history
|
|
96
|
+
*/
|
|
97
|
+
clearHistory() {
|
|
98
|
+
this.messages = [];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get conversation history
|
|
102
|
+
*/
|
|
103
|
+
getHistory() {
|
|
104
|
+
return [...this.messages];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Set knowledge base
|
|
108
|
+
*/
|
|
109
|
+
setKnowledgeBase(kb) {
|
|
110
|
+
this.knowledgeBase = kb;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.ContextAgent = ContextAgent;
|
|
114
|
+
/**
|
|
115
|
+
* Create a context agent
|
|
116
|
+
*/
|
|
117
|
+
function createContextAgent(config) {
|
|
118
|
+
return new ContextAgent(config);
|
|
119
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced Agent - Agent with session management, provider abstraction, and tool support
|
|
3
|
+
*/
|
|
4
|
+
import { type ToolCall } from '../llm/providers';
|
|
5
|
+
import { Session } from '../session';
|
|
6
|
+
import { FunctionTool } from '../tools/decorator';
|
|
7
|
+
export interface EnhancedAgentConfig {
|
|
8
|
+
name?: string;
|
|
9
|
+
instructions: string;
|
|
10
|
+
llm?: string;
|
|
11
|
+
tools?: Array<FunctionTool | Function | {
|
|
12
|
+
name: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
parameters?: any;
|
|
15
|
+
execute: Function;
|
|
16
|
+
}>;
|
|
17
|
+
session?: Session;
|
|
18
|
+
sessionId?: string;
|
|
19
|
+
verbose?: boolean;
|
|
20
|
+
stream?: boolean;
|
|
21
|
+
maxToolCalls?: number;
|
|
22
|
+
temperature?: number;
|
|
23
|
+
maxTokens?: number;
|
|
24
|
+
outputSchema?: any;
|
|
25
|
+
}
|
|
26
|
+
export interface ChatOptions {
|
|
27
|
+
stream?: boolean;
|
|
28
|
+
temperature?: number;
|
|
29
|
+
maxTokens?: number;
|
|
30
|
+
outputSchema?: any;
|
|
31
|
+
onToken?: (token: string) => void;
|
|
32
|
+
}
|
|
33
|
+
export interface ChatResult {
|
|
34
|
+
text: string;
|
|
35
|
+
structured?: any;
|
|
36
|
+
toolCalls?: ToolCall[];
|
|
37
|
+
usage: {
|
|
38
|
+
promptTokens: number;
|
|
39
|
+
completionTokens: number;
|
|
40
|
+
totalTokens: number;
|
|
41
|
+
};
|
|
42
|
+
runId: string;
|
|
43
|
+
sessionId: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Enhanced Agent with full feature support
|
|
47
|
+
*/
|
|
48
|
+
export declare class EnhancedAgent {
|
|
49
|
+
readonly name: string;
|
|
50
|
+
readonly instructions: string;
|
|
51
|
+
readonly sessionId: string;
|
|
52
|
+
private provider;
|
|
53
|
+
private session;
|
|
54
|
+
private toolRegistry;
|
|
55
|
+
private verbose;
|
|
56
|
+
private stream;
|
|
57
|
+
private maxToolCalls;
|
|
58
|
+
private temperature;
|
|
59
|
+
private maxTokens?;
|
|
60
|
+
private outputSchema?;
|
|
61
|
+
constructor(config: EnhancedAgentConfig);
|
|
62
|
+
private registerTools;
|
|
63
|
+
/**
|
|
64
|
+
* Add a tool to the agent
|
|
65
|
+
*/
|
|
66
|
+
addTool(t: FunctionTool | {
|
|
67
|
+
name: string;
|
|
68
|
+
description?: string;
|
|
69
|
+
parameters?: any;
|
|
70
|
+
execute: Function;
|
|
71
|
+
}): this;
|
|
72
|
+
/**
|
|
73
|
+
* Get all registered tools
|
|
74
|
+
*/
|
|
75
|
+
getTools(): FunctionTool[];
|
|
76
|
+
/**
|
|
77
|
+
* Chat with the agent
|
|
78
|
+
*/
|
|
79
|
+
chat(prompt: string, options?: ChatOptions): Promise<ChatResult>;
|
|
80
|
+
/**
|
|
81
|
+
* Start method for compatibility
|
|
82
|
+
*/
|
|
83
|
+
start(prompt: string, options?: ChatOptions): Promise<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Get the session
|
|
86
|
+
*/
|
|
87
|
+
getSession(): Session;
|
|
88
|
+
/**
|
|
89
|
+
* Clear conversation history
|
|
90
|
+
*/
|
|
91
|
+
clearHistory(): void;
|
|
92
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Enhanced Agent - Agent with session management, provider abstraction, and tool support
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EnhancedAgent = void 0;
|
|
7
|
+
const providers_1 = require("../llm/providers");
|
|
8
|
+
const session_1 = require("../session");
|
|
9
|
+
const decorator_1 = require("../tools/decorator");
|
|
10
|
+
const logger_1 = require("../utils/logger");
|
|
11
|
+
/**
|
|
12
|
+
* Enhanced Agent with full feature support
|
|
13
|
+
*/
|
|
14
|
+
class EnhancedAgent {
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.name = config.name || `Agent_${Math.random().toString(36).substr(2, 9)}`;
|
|
17
|
+
this.instructions = config.instructions;
|
|
18
|
+
this.verbose = config.verbose ?? true;
|
|
19
|
+
this.stream = config.stream ?? true;
|
|
20
|
+
this.maxToolCalls = config.maxToolCalls ?? 10;
|
|
21
|
+
this.temperature = config.temperature ?? 0.7;
|
|
22
|
+
this.maxTokens = config.maxTokens;
|
|
23
|
+
this.outputSchema = config.outputSchema;
|
|
24
|
+
// Initialize provider
|
|
25
|
+
this.provider = (0, providers_1.createProvider)(config.llm || 'gpt-4o-mini');
|
|
26
|
+
// Initialize session
|
|
27
|
+
if (config.session) {
|
|
28
|
+
this.session = config.session;
|
|
29
|
+
}
|
|
30
|
+
else if (config.sessionId) {
|
|
31
|
+
this.session = (0, session_1.getSessionManager)().getOrCreate(config.sessionId);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.session = (0, session_1.getSessionManager)().create();
|
|
35
|
+
}
|
|
36
|
+
this.sessionId = this.session.id;
|
|
37
|
+
// Initialize tool registry
|
|
38
|
+
this.toolRegistry = new decorator_1.ToolRegistry();
|
|
39
|
+
if (config.tools) {
|
|
40
|
+
this.registerTools(config.tools);
|
|
41
|
+
}
|
|
42
|
+
logger_1.Logger.setVerbose(this.verbose);
|
|
43
|
+
}
|
|
44
|
+
registerTools(tools) {
|
|
45
|
+
if (!tools)
|
|
46
|
+
return;
|
|
47
|
+
for (const t of tools) {
|
|
48
|
+
if (t instanceof decorator_1.FunctionTool) {
|
|
49
|
+
this.toolRegistry.register(t);
|
|
50
|
+
}
|
|
51
|
+
else if (typeof t === 'function') {
|
|
52
|
+
// Convert function to tool
|
|
53
|
+
const funcName = t.name || `function_${Math.random().toString(36).substr(2, 9)}`;
|
|
54
|
+
this.toolRegistry.register((0, decorator_1.tool)({
|
|
55
|
+
name: funcName,
|
|
56
|
+
description: `Function ${funcName}`,
|
|
57
|
+
execute: t,
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
else if (typeof t === 'object' && 'execute' in t) {
|
|
61
|
+
// Tool config object
|
|
62
|
+
this.toolRegistry.register((0, decorator_1.tool)({
|
|
63
|
+
name: t.name,
|
|
64
|
+
description: t.description,
|
|
65
|
+
parameters: t.parameters,
|
|
66
|
+
execute: t.execute,
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Add a tool to the agent
|
|
73
|
+
*/
|
|
74
|
+
addTool(t) {
|
|
75
|
+
if (t instanceof decorator_1.FunctionTool) {
|
|
76
|
+
this.toolRegistry.register(t);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this.toolRegistry.register((0, decorator_1.tool)({
|
|
80
|
+
name: t.name,
|
|
81
|
+
description: t.description,
|
|
82
|
+
parameters: t.parameters,
|
|
83
|
+
execute: t.execute,
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Get all registered tools
|
|
90
|
+
*/
|
|
91
|
+
getTools() {
|
|
92
|
+
return this.toolRegistry.list();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Chat with the agent
|
|
96
|
+
*/
|
|
97
|
+
async chat(prompt, options = {}) {
|
|
98
|
+
const run = this.session.createRun().start();
|
|
99
|
+
const trace = run.createTrace({ name: 'chat' }).start();
|
|
100
|
+
try {
|
|
101
|
+
// Add user message to session
|
|
102
|
+
this.session.addMessage({ role: 'user', content: prompt });
|
|
103
|
+
// Build messages array
|
|
104
|
+
const messages = [
|
|
105
|
+
{ role: 'system', content: this.instructions },
|
|
106
|
+
...this.session.getMessagesForLLM(),
|
|
107
|
+
];
|
|
108
|
+
// Get tool definitions
|
|
109
|
+
const tools = this.toolRegistry.list().length > 0
|
|
110
|
+
? this.toolRegistry.getDefinitions()
|
|
111
|
+
: undefined;
|
|
112
|
+
let result;
|
|
113
|
+
let fullText = '';
|
|
114
|
+
let allToolCalls = [];
|
|
115
|
+
let iterations = 0;
|
|
116
|
+
// Tool calling loop
|
|
117
|
+
while (iterations < this.maxToolCalls) {
|
|
118
|
+
iterations++;
|
|
119
|
+
if (options.stream && !tools && options.onToken) {
|
|
120
|
+
// Streaming without tools
|
|
121
|
+
const stream = await this.provider.streamText({
|
|
122
|
+
messages,
|
|
123
|
+
temperature: options.temperature ?? this.temperature,
|
|
124
|
+
maxTokens: options.maxTokens ?? this.maxTokens,
|
|
125
|
+
onToken: options.onToken,
|
|
126
|
+
});
|
|
127
|
+
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
128
|
+
for await (const chunk of stream) {
|
|
129
|
+
if (chunk.text)
|
|
130
|
+
fullText += chunk.text;
|
|
131
|
+
if (chunk.usage)
|
|
132
|
+
usage = { ...usage, ...chunk.usage };
|
|
133
|
+
}
|
|
134
|
+
result = {
|
|
135
|
+
text: fullText,
|
|
136
|
+
usage,
|
|
137
|
+
finishReason: 'stop',
|
|
138
|
+
};
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
else if (options.outputSchema) {
|
|
142
|
+
// Structured output
|
|
143
|
+
const objResult = await this.provider.generateObject({
|
|
144
|
+
messages,
|
|
145
|
+
schema: options.outputSchema ?? this.outputSchema,
|
|
146
|
+
temperature: options.temperature ?? this.temperature,
|
|
147
|
+
maxTokens: options.maxTokens ?? this.maxTokens,
|
|
148
|
+
});
|
|
149
|
+
result = {
|
|
150
|
+
text: JSON.stringify(objResult.object),
|
|
151
|
+
usage: objResult.usage,
|
|
152
|
+
finishReason: 'stop',
|
|
153
|
+
};
|
|
154
|
+
fullText = result.text;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
// Regular generation with potential tool calls
|
|
159
|
+
result = await this.provider.generateText({
|
|
160
|
+
messages,
|
|
161
|
+
temperature: options.temperature ?? this.temperature,
|
|
162
|
+
maxTokens: options.maxTokens ?? this.maxTokens,
|
|
163
|
+
tools,
|
|
164
|
+
});
|
|
165
|
+
if (result.toolCalls && result.toolCalls.length > 0) {
|
|
166
|
+
allToolCalls.push(...result.toolCalls);
|
|
167
|
+
// Add assistant message with tool calls
|
|
168
|
+
messages.push({
|
|
169
|
+
role: 'assistant',
|
|
170
|
+
content: result.text || null,
|
|
171
|
+
tool_calls: result.toolCalls,
|
|
172
|
+
});
|
|
173
|
+
// Execute tool calls
|
|
174
|
+
for (const tc of result.toolCalls) {
|
|
175
|
+
const toolTrace = trace.createChild({ name: `tool:${tc.function.name}` }).start();
|
|
176
|
+
try {
|
|
177
|
+
const toolFn = this.toolRegistry.get(tc.function.name);
|
|
178
|
+
if (!toolFn) {
|
|
179
|
+
throw new Error(`Tool '${tc.function.name}' not found`);
|
|
180
|
+
}
|
|
181
|
+
const args = JSON.parse(tc.function.arguments);
|
|
182
|
+
const toolResult = await toolFn.execute(args, {
|
|
183
|
+
agentName: this.name,
|
|
184
|
+
sessionId: this.sessionId,
|
|
185
|
+
runId: run.id,
|
|
186
|
+
});
|
|
187
|
+
const resultStr = typeof toolResult === 'string'
|
|
188
|
+
? toolResult
|
|
189
|
+
: JSON.stringify(toolResult);
|
|
190
|
+
messages.push({
|
|
191
|
+
role: 'tool',
|
|
192
|
+
content: resultStr,
|
|
193
|
+
tool_call_id: tc.id,
|
|
194
|
+
});
|
|
195
|
+
toolTrace.complete({ result: resultStr });
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
messages.push({
|
|
199
|
+
role: 'tool',
|
|
200
|
+
content: `Error: ${error.message}`,
|
|
201
|
+
tool_call_id: tc.id,
|
|
202
|
+
});
|
|
203
|
+
toolTrace.fail(error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// Continue loop to get final response
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
// No tool calls, we have our final response
|
|
210
|
+
fullText = result.text;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Add assistant message to session
|
|
215
|
+
this.session.addMessage({
|
|
216
|
+
role: 'assistant',
|
|
217
|
+
content: fullText,
|
|
218
|
+
tool_calls: allToolCalls.length > 0 ? allToolCalls : undefined,
|
|
219
|
+
});
|
|
220
|
+
trace.complete({ responseLength: fullText.length });
|
|
221
|
+
run.complete();
|
|
222
|
+
// Parse structured output if schema was provided
|
|
223
|
+
let structured;
|
|
224
|
+
if (options.outputSchema) {
|
|
225
|
+
try {
|
|
226
|
+
structured = JSON.parse(fullText);
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
// Not valid JSON
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
text: fullText,
|
|
234
|
+
structured,
|
|
235
|
+
toolCalls: allToolCalls.length > 0 ? allToolCalls : undefined,
|
|
236
|
+
usage: result.usage,
|
|
237
|
+
runId: run.id,
|
|
238
|
+
sessionId: this.sessionId,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
trace.fail(error);
|
|
243
|
+
run.fail(error);
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Start method for compatibility
|
|
249
|
+
*/
|
|
250
|
+
async start(prompt, options) {
|
|
251
|
+
const result = await this.chat(prompt, options);
|
|
252
|
+
return result.text;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Get the session
|
|
256
|
+
*/
|
|
257
|
+
getSession() {
|
|
258
|
+
return this.session;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Clear conversation history
|
|
262
|
+
*/
|
|
263
|
+
clearHistory() {
|
|
264
|
+
this.session.clearMessages();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
exports.EnhancedAgent = EnhancedAgent;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Handoff - Transfer conversations between agents
|
|
3
|
+
*/
|
|
4
|
+
import type { EnhancedAgent } from './enhanced';
|
|
5
|
+
export interface HandoffConfig {
|
|
6
|
+
agent: EnhancedAgent;
|
|
7
|
+
name?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
condition?: (context: HandoffContext) => boolean;
|
|
10
|
+
transformContext?: (messages: any[]) => any[];
|
|
11
|
+
}
|
|
12
|
+
export interface HandoffContext {
|
|
13
|
+
messages: any[];
|
|
14
|
+
lastMessage: string;
|
|
15
|
+
topic?: string;
|
|
16
|
+
metadata?: Record<string, any>;
|
|
17
|
+
}
|
|
18
|
+
export interface HandoffResult {
|
|
19
|
+
handedOffTo: string;
|
|
20
|
+
response: string;
|
|
21
|
+
context: HandoffContext;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Handoff class - Represents a handoff target
|
|
25
|
+
*/
|
|
26
|
+
export declare class Handoff {
|
|
27
|
+
readonly targetAgent: EnhancedAgent;
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly description: string;
|
|
30
|
+
readonly condition?: (context: HandoffContext) => boolean;
|
|
31
|
+
readonly transformContext?: (messages: any[]) => any[];
|
|
32
|
+
constructor(config: HandoffConfig);
|
|
33
|
+
/**
|
|
34
|
+
* Check if handoff should be triggered
|
|
35
|
+
*/
|
|
36
|
+
shouldTrigger(context: HandoffContext): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Execute the handoff
|
|
39
|
+
*/
|
|
40
|
+
execute(context: HandoffContext): Promise<HandoffResult>;
|
|
41
|
+
/**
|
|
42
|
+
* Get tool definition for LLM
|
|
43
|
+
*/
|
|
44
|
+
getToolDefinition(): {
|
|
45
|
+
name: string;
|
|
46
|
+
description: string;
|
|
47
|
+
parameters: any;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a handoff configuration
|
|
52
|
+
*/
|
|
53
|
+
export declare function handoff(config: HandoffConfig): Handoff;
|
|
54
|
+
/**
|
|
55
|
+
* Handoff filters - Common condition functions
|
|
56
|
+
*/
|
|
57
|
+
export declare const handoffFilters: {
|
|
58
|
+
/**
|
|
59
|
+
* Trigger handoff based on topic keywords
|
|
60
|
+
*/
|
|
61
|
+
topic: (keywords: string | string[]) => (context: HandoffContext) => boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Trigger handoff based on metadata
|
|
64
|
+
*/
|
|
65
|
+
metadata: (key: string, value: any) => (context: HandoffContext) => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Always trigger
|
|
68
|
+
*/
|
|
69
|
+
always: () => () => boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Never trigger (manual only)
|
|
72
|
+
*/
|
|
73
|
+
never: () => () => boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Combine multiple conditions with AND
|
|
76
|
+
*/
|
|
77
|
+
and: (...conditions: Array<(context: HandoffContext) => boolean>) => (context: HandoffContext) => boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Combine multiple conditions with OR
|
|
80
|
+
*/
|
|
81
|
+
or: (...conditions: Array<(context: HandoffContext) => boolean>) => (context: HandoffContext) => boolean;
|
|
82
|
+
};
|