praisonai 1.0.19 → 1.2.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.
Files changed (74) hide show
  1. package/dist/agent/context.d.ts +68 -0
  2. package/dist/agent/context.js +119 -0
  3. package/dist/agent/enhanced.d.ts +92 -0
  4. package/dist/agent/enhanced.js +267 -0
  5. package/dist/agent/handoff.d.ts +82 -0
  6. package/dist/agent/handoff.js +124 -0
  7. package/dist/agent/image.d.ts +51 -0
  8. package/dist/agent/image.js +93 -0
  9. package/dist/agent/prompt-expander.d.ts +40 -0
  10. package/dist/agent/prompt-expander.js +84 -0
  11. package/dist/agent/query-rewriter.d.ts +38 -0
  12. package/dist/agent/query-rewriter.js +79 -0
  13. package/dist/agent/research.d.ts +52 -0
  14. package/dist/agent/research.js +118 -0
  15. package/dist/agent/router.d.ts +77 -0
  16. package/dist/agent/router.js +113 -0
  17. package/dist/agent/simple.js +1 -1
  18. package/dist/agent/types.js +2 -2
  19. package/dist/auto/index.d.ts +56 -0
  20. package/dist/auto/index.js +142 -0
  21. package/dist/cli/index.d.ts +20 -0
  22. package/dist/cli/index.js +150 -0
  23. package/dist/db/index.d.ts +23 -0
  24. package/dist/db/index.js +72 -0
  25. package/dist/db/memory-adapter.d.ts +42 -0
  26. package/dist/db/memory-adapter.js +146 -0
  27. package/dist/db/types.d.ts +113 -0
  28. package/dist/db/types.js +5 -0
  29. package/dist/eval/index.d.ts +61 -0
  30. package/dist/eval/index.js +157 -0
  31. package/dist/guardrails/index.d.ts +82 -0
  32. package/dist/guardrails/index.js +202 -0
  33. package/dist/guardrails/llm-guardrail.d.ts +40 -0
  34. package/dist/guardrails/llm-guardrail.js +91 -0
  35. package/dist/index.d.ts +26 -1
  36. package/dist/index.js +122 -1
  37. package/dist/knowledge/chunking.d.ts +55 -0
  38. package/dist/knowledge/chunking.js +157 -0
  39. package/dist/knowledge/rag.d.ts +80 -0
  40. package/dist/knowledge/rag.js +147 -0
  41. package/dist/llm/openai.js +1 -1
  42. package/dist/llm/providers/anthropic.d.ts +33 -0
  43. package/dist/llm/providers/anthropic.js +291 -0
  44. package/dist/llm/providers/base.d.ts +25 -0
  45. package/dist/llm/providers/base.js +43 -0
  46. package/dist/llm/providers/google.d.ts +27 -0
  47. package/dist/llm/providers/google.js +275 -0
  48. package/dist/llm/providers/index.d.ts +43 -0
  49. package/dist/llm/providers/index.js +116 -0
  50. package/dist/llm/providers/openai.d.ts +18 -0
  51. package/dist/llm/providers/openai.js +203 -0
  52. package/dist/llm/providers/types.d.ts +94 -0
  53. package/dist/llm/providers/types.js +5 -0
  54. package/dist/memory/memory.d.ts +92 -0
  55. package/dist/memory/memory.js +169 -0
  56. package/dist/observability/index.d.ts +86 -0
  57. package/dist/observability/index.js +166 -0
  58. package/dist/planning/index.d.ts +133 -0
  59. package/dist/planning/index.js +228 -0
  60. package/dist/session/index.d.ts +111 -0
  61. package/dist/session/index.js +250 -0
  62. package/dist/skills/index.d.ts +70 -0
  63. package/dist/skills/index.js +233 -0
  64. package/dist/telemetry/index.d.ts +102 -0
  65. package/dist/telemetry/index.js +187 -0
  66. package/dist/tools/decorator.d.ts +91 -0
  67. package/dist/tools/decorator.js +165 -0
  68. package/dist/tools/index.d.ts +2 -0
  69. package/dist/tools/index.js +3 -0
  70. package/dist/tools/mcpSse.d.ts +41 -0
  71. package/dist/tools/mcpSse.js +108 -0
  72. package/dist/workflows/index.d.ts +97 -0
  73. package/dist/workflows/index.js +216 -0
  74. package/package.json +5 -2
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ /**
3
+ * Agent Handoff - Transfer conversations between agents
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.handoffFilters = exports.Handoff = void 0;
7
+ exports.handoff = handoff;
8
+ /**
9
+ * Handoff class - Represents a handoff target
10
+ */
11
+ class Handoff {
12
+ constructor(config) {
13
+ this.targetAgent = config.agent;
14
+ this.name = config.name || `handoff_to_${config.agent.name}`;
15
+ this.description = config.description || `Transfer conversation to ${config.agent.name}`;
16
+ this.condition = config.condition;
17
+ this.transformContext = config.transformContext;
18
+ }
19
+ /**
20
+ * Check if handoff should be triggered
21
+ */
22
+ shouldTrigger(context) {
23
+ if (this.condition) {
24
+ return this.condition(context);
25
+ }
26
+ return true;
27
+ }
28
+ /**
29
+ * Execute the handoff
30
+ */
31
+ async execute(context) {
32
+ let messages = context.messages;
33
+ if (this.transformContext) {
34
+ messages = this.transformContext(messages);
35
+ }
36
+ // Transfer context to target agent
37
+ const response = await this.targetAgent.chat(context.lastMessage);
38
+ return {
39
+ handedOffTo: this.targetAgent.name,
40
+ response: response.text,
41
+ context: {
42
+ ...context,
43
+ messages,
44
+ },
45
+ };
46
+ }
47
+ /**
48
+ * Get tool definition for LLM
49
+ */
50
+ getToolDefinition() {
51
+ return {
52
+ name: this.name,
53
+ description: this.description,
54
+ parameters: {
55
+ type: 'object',
56
+ properties: {
57
+ reason: {
58
+ type: 'string',
59
+ description: 'Reason for the handoff',
60
+ },
61
+ },
62
+ required: [],
63
+ },
64
+ };
65
+ }
66
+ }
67
+ exports.Handoff = Handoff;
68
+ /**
69
+ * Create a handoff configuration
70
+ */
71
+ function handoff(config) {
72
+ return new Handoff(config);
73
+ }
74
+ /**
75
+ * Handoff filters - Common condition functions
76
+ */
77
+ exports.handoffFilters = {
78
+ /**
79
+ * Trigger handoff based on topic keywords
80
+ */
81
+ topic: (keywords) => {
82
+ const keywordList = Array.isArray(keywords) ? keywords : [keywords];
83
+ return (context) => {
84
+ const lastMessage = context.lastMessage.toLowerCase();
85
+ return keywordList.some(kw => lastMessage.includes(kw.toLowerCase()));
86
+ };
87
+ },
88
+ /**
89
+ * Trigger handoff based on metadata
90
+ */
91
+ metadata: (key, value) => {
92
+ return (context) => {
93
+ return context.metadata?.[key] === value;
94
+ };
95
+ },
96
+ /**
97
+ * Always trigger
98
+ */
99
+ always: () => {
100
+ return () => true;
101
+ },
102
+ /**
103
+ * Never trigger (manual only)
104
+ */
105
+ never: () => {
106
+ return () => false;
107
+ },
108
+ /**
109
+ * Combine multiple conditions with AND
110
+ */
111
+ and: (...conditions) => {
112
+ return (context) => {
113
+ return conditions.every(cond => cond(context));
114
+ };
115
+ },
116
+ /**
117
+ * Combine multiple conditions with OR
118
+ */
119
+ or: (...conditions) => {
120
+ return (context) => {
121
+ return conditions.some(cond => cond(context));
122
+ };
123
+ },
124
+ };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * ImageAgent - Agent for image generation and analysis
3
+ */
4
+ export interface ImageGenerationConfig {
5
+ prompt: string;
6
+ size?: '256x256' | '512x512' | '1024x1024' | '1792x1024' | '1024x1792';
7
+ quality?: 'standard' | 'hd';
8
+ style?: 'vivid' | 'natural';
9
+ n?: number;
10
+ }
11
+ export interface ImageAnalysisConfig {
12
+ imageUrl: string;
13
+ prompt?: string;
14
+ detail?: 'low' | 'high' | 'auto';
15
+ }
16
+ export interface ImageAgentConfig {
17
+ name?: string;
18
+ llm?: string;
19
+ imageModel?: string;
20
+ verbose?: boolean;
21
+ }
22
+ /**
23
+ * ImageAgent - Agent for image generation and analysis
24
+ */
25
+ export declare class ImageAgent {
26
+ readonly name: string;
27
+ private provider;
28
+ private imageModel;
29
+ private verbose;
30
+ constructor(config?: ImageAgentConfig);
31
+ /**
32
+ * Analyze an image
33
+ */
34
+ analyze(config: ImageAnalysisConfig): Promise<string>;
35
+ /**
36
+ * Generate an image (requires OpenAI DALL-E)
37
+ */
38
+ generate(config: ImageGenerationConfig): Promise<string[]>;
39
+ /**
40
+ * Chat with image context
41
+ */
42
+ chat(prompt: string, imageUrl?: string): Promise<string>;
43
+ /**
44
+ * Compare two images
45
+ */
46
+ compare(imageUrl1: string, imageUrl2: string, prompt?: string): Promise<string>;
47
+ }
48
+ /**
49
+ * Create an ImageAgent
50
+ */
51
+ export declare function createImageAgent(config?: ImageAgentConfig): ImageAgent;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ /**
3
+ * ImageAgent - Agent for image generation and analysis
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ImageAgent = void 0;
7
+ exports.createImageAgent = createImageAgent;
8
+ const providers_1 = require("../llm/providers");
9
+ /**
10
+ * ImageAgent - Agent for image generation and analysis
11
+ */
12
+ class ImageAgent {
13
+ constructor(config = {}) {
14
+ this.name = config.name || `ImageAgent_${Math.random().toString(36).substr(2, 9)}`;
15
+ this.provider = (0, providers_1.createProvider)(config.llm || 'openai/gpt-4o-mini');
16
+ this.imageModel = config.imageModel || 'dall-e-3';
17
+ this.verbose = config.verbose ?? false;
18
+ }
19
+ /**
20
+ * Analyze an image
21
+ */
22
+ async analyze(config) {
23
+ const prompt = config.prompt || 'Describe this image in detail.';
24
+ const messages = [
25
+ {
26
+ role: 'user',
27
+ content: [
28
+ { type: 'text', text: prompt },
29
+ {
30
+ type: 'image_url',
31
+ image_url: {
32
+ url: config.imageUrl,
33
+ detail: config.detail || 'auto'
34
+ }
35
+ }
36
+ ]
37
+ }
38
+ ];
39
+ const result = await this.provider.generateText({ messages });
40
+ if (this.verbose) {
41
+ console.log(`[ImageAgent] Analysis: ${result.text.substring(0, 100)}...`);
42
+ }
43
+ return result.text;
44
+ }
45
+ /**
46
+ * Generate an image (requires OpenAI DALL-E)
47
+ */
48
+ async generate(config) {
49
+ // This would require direct OpenAI API call for image generation
50
+ // For now, return a placeholder
51
+ if (this.verbose) {
52
+ console.log(`[ImageAgent] Generating image: ${config.prompt}`);
53
+ }
54
+ throw new Error('Image generation requires direct OpenAI API integration. Use the OpenAI SDK directly for DALL-E.');
55
+ }
56
+ /**
57
+ * Chat with image context
58
+ */
59
+ async chat(prompt, imageUrl) {
60
+ if (imageUrl) {
61
+ return this.analyze({ imageUrl, prompt });
62
+ }
63
+ const result = await this.provider.generateText({
64
+ messages: [{ role: 'user', content: prompt }]
65
+ });
66
+ return result.text;
67
+ }
68
+ /**
69
+ * Compare two images
70
+ */
71
+ async compare(imageUrl1, imageUrl2, prompt) {
72
+ const comparePrompt = prompt || 'Compare these two images and describe the differences and similarities.';
73
+ const messages = [
74
+ {
75
+ role: 'user',
76
+ content: [
77
+ { type: 'text', text: comparePrompt },
78
+ { type: 'image_url', image_url: { url: imageUrl1 } },
79
+ { type: 'image_url', image_url: { url: imageUrl2 } }
80
+ ]
81
+ }
82
+ ];
83
+ const result = await this.provider.generateText({ messages });
84
+ return result.text;
85
+ }
86
+ }
87
+ exports.ImageAgent = ImageAgent;
88
+ /**
89
+ * Create an ImageAgent
90
+ */
91
+ function createImageAgent(config) {
92
+ return new ImageAgent(config);
93
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * PromptExpanderAgent - Expand and enhance prompts
3
+ */
4
+ export type ExpandStrategy = 'detail' | 'context' | 'examples' | 'constraints' | 'auto';
5
+ export interface ExpandResult {
6
+ original: string;
7
+ expanded: string;
8
+ strategy: ExpandStrategy;
9
+ additions: string[];
10
+ }
11
+ export interface PromptExpanderConfig {
12
+ name?: string;
13
+ llm?: string;
14
+ defaultStrategy?: ExpandStrategy;
15
+ verbose?: boolean;
16
+ }
17
+ /**
18
+ * PromptExpanderAgent - Expand prompts with more detail and context
19
+ */
20
+ export declare class PromptExpanderAgent {
21
+ readonly name: string;
22
+ private provider;
23
+ private defaultStrategy;
24
+ private verbose;
25
+ constructor(config?: PromptExpanderConfig);
26
+ /**
27
+ * Expand a prompt
28
+ */
29
+ expand(prompt: string, strategy?: ExpandStrategy): Promise<ExpandResult>;
30
+ /**
31
+ * Detect the best expansion strategy
32
+ */
33
+ detectStrategy(prompt: string): ExpandStrategy;
34
+ private getSystemPrompt;
35
+ private extractAdditions;
36
+ }
37
+ /**
38
+ * Create a PromptExpanderAgent
39
+ */
40
+ export declare function createPromptExpanderAgent(config?: PromptExpanderConfig): PromptExpanderAgent;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ /**
3
+ * PromptExpanderAgent - Expand and enhance prompts
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PromptExpanderAgent = void 0;
7
+ exports.createPromptExpanderAgent = createPromptExpanderAgent;
8
+ const providers_1 = require("../llm/providers");
9
+ /**
10
+ * PromptExpanderAgent - Expand prompts with more detail and context
11
+ */
12
+ class PromptExpanderAgent {
13
+ constructor(config = {}) {
14
+ this.name = config.name || `PromptExpander_${Math.random().toString(36).substr(2, 9)}`;
15
+ this.provider = (0, providers_1.createProvider)(config.llm || 'openai/gpt-4o-mini');
16
+ this.defaultStrategy = config.defaultStrategy || 'auto';
17
+ this.verbose = config.verbose ?? false;
18
+ }
19
+ /**
20
+ * Expand a prompt
21
+ */
22
+ async expand(prompt, strategy) {
23
+ const useStrategy = strategy || this.defaultStrategy;
24
+ const actualStrategy = useStrategy === 'auto' ? this.detectStrategy(prompt) : useStrategy;
25
+ const systemPrompt = this.getSystemPrompt(actualStrategy);
26
+ const result = await this.provider.generateText({
27
+ messages: [
28
+ { role: 'system', content: systemPrompt },
29
+ { role: 'user', content: `Expand this prompt: ${prompt}` }
30
+ ]
31
+ });
32
+ const expanded = result.text;
33
+ const additions = this.extractAdditions(prompt, expanded);
34
+ if (this.verbose) {
35
+ console.log(`[PromptExpander] Strategy: ${actualStrategy}, Added ${additions.length} elements`);
36
+ }
37
+ return {
38
+ original: prompt,
39
+ expanded,
40
+ strategy: actualStrategy,
41
+ additions
42
+ };
43
+ }
44
+ /**
45
+ * Detect the best expansion strategy
46
+ */
47
+ detectStrategy(prompt) {
48
+ const words = prompt.split(/\s+/).length;
49
+ const lower = prompt.toLowerCase();
50
+ if (words < 5)
51
+ return 'detail';
52
+ if (lower.includes('why') || lower.includes('explain') || lower.includes('context'))
53
+ return 'context';
54
+ if (lower.includes('example') || lower.includes('show') || lower.includes('demonstrate'))
55
+ return 'examples';
56
+ if (lower.includes('must') || lower.includes('should') || lower.includes('require'))
57
+ return 'constraints';
58
+ return 'detail';
59
+ }
60
+ getSystemPrompt(strategy) {
61
+ const prompts = {
62
+ detail: 'Expand this prompt by adding specific details, requirements, and clarifications. Make it more precise and actionable.',
63
+ context: 'Expand this prompt by adding relevant background context, explaining the purpose, and providing necessary information.',
64
+ examples: 'Expand this prompt by adding concrete examples, sample inputs/outputs, and demonstrations of expected behavior.',
65
+ constraints: 'Expand this prompt by adding explicit constraints, requirements, limitations, and success criteria.',
66
+ auto: 'Expand this prompt to make it clearer, more specific, and more actionable. Add relevant details and context.'
67
+ };
68
+ return prompts[strategy];
69
+ }
70
+ extractAdditions(original, expanded) {
71
+ const originalWords = new Set(original.toLowerCase().split(/\s+/));
72
+ const expandedWords = expanded.toLowerCase().split(/\s+/);
73
+ const newWords = expandedWords.filter(w => !originalWords.has(w) && w.length > 3);
74
+ const uniqueNew = [...new Set(newWords)];
75
+ return uniqueNew.slice(0, 10);
76
+ }
77
+ }
78
+ exports.PromptExpanderAgent = PromptExpanderAgent;
79
+ /**
80
+ * Create a PromptExpanderAgent
81
+ */
82
+ function createPromptExpanderAgent(config) {
83
+ return new PromptExpanderAgent(config);
84
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * QueryRewriterAgent - Rewrite and optimize queries
3
+ */
4
+ export type RewriteStrategy = 'expand' | 'simplify' | 'decompose' | 'rephrase' | 'auto';
5
+ export interface RewriteResult {
6
+ original: string;
7
+ rewritten: string[];
8
+ strategy: RewriteStrategy;
9
+ confidence: number;
10
+ }
11
+ export interface QueryRewriterConfig {
12
+ name?: string;
13
+ llm?: string;
14
+ defaultStrategy?: RewriteStrategy;
15
+ verbose?: boolean;
16
+ }
17
+ /**
18
+ * QueryRewriterAgent - Optimize and rewrite queries
19
+ */
20
+ export declare class QueryRewriterAgent {
21
+ readonly name: string;
22
+ private provider;
23
+ private defaultStrategy;
24
+ private verbose;
25
+ constructor(config?: QueryRewriterConfig);
26
+ /**
27
+ * Rewrite a query
28
+ */
29
+ rewrite(query: string, strategy?: RewriteStrategy): Promise<RewriteResult>;
30
+ private detectStrategy;
31
+ private getSystemPrompt;
32
+ private buildPrompt;
33
+ private parseRewrittenQueries;
34
+ }
35
+ /**
36
+ * Create a QueryRewriterAgent
37
+ */
38
+ export declare function createQueryRewriterAgent(config?: QueryRewriterConfig): QueryRewriterAgent;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ /**
3
+ * QueryRewriterAgent - Rewrite and optimize queries
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.QueryRewriterAgent = void 0;
7
+ exports.createQueryRewriterAgent = createQueryRewriterAgent;
8
+ const providers_1 = require("../llm/providers");
9
+ /**
10
+ * QueryRewriterAgent - Optimize and rewrite queries
11
+ */
12
+ class QueryRewriterAgent {
13
+ constructor(config = {}) {
14
+ this.name = config.name || `QueryRewriter_${Math.random().toString(36).substr(2, 9)}`;
15
+ this.provider = (0, providers_1.createProvider)(config.llm || 'openai/gpt-4o-mini');
16
+ this.defaultStrategy = config.defaultStrategy || 'auto';
17
+ this.verbose = config.verbose ?? false;
18
+ }
19
+ /**
20
+ * Rewrite a query
21
+ */
22
+ async rewrite(query, strategy) {
23
+ const useStrategy = strategy || this.defaultStrategy;
24
+ const actualStrategy = useStrategy === 'auto' ? this.detectStrategy(query) : useStrategy;
25
+ const prompt = this.buildPrompt(query, actualStrategy);
26
+ const result = await this.provider.generateText({
27
+ messages: [
28
+ { role: 'system', content: this.getSystemPrompt(actualStrategy) },
29
+ { role: 'user', content: prompt }
30
+ ]
31
+ });
32
+ const rewritten = this.parseRewrittenQueries(result.text);
33
+ if (this.verbose) {
34
+ console.log(`[QueryRewriter] Strategy: ${actualStrategy}, Rewrites: ${rewritten.length}`);
35
+ }
36
+ return {
37
+ original: query,
38
+ rewritten,
39
+ strategy: actualStrategy,
40
+ confidence: rewritten.length > 0 ? 0.85 : 0.5
41
+ };
42
+ }
43
+ detectStrategy(query) {
44
+ const words = query.split(/\s+/).length;
45
+ if (words < 3)
46
+ return 'expand';
47
+ if (words > 20)
48
+ return 'simplify';
49
+ if (query.includes(' and ') || query.includes(' or '))
50
+ return 'decompose';
51
+ return 'rephrase';
52
+ }
53
+ getSystemPrompt(strategy) {
54
+ const prompts = {
55
+ expand: 'Expand this query with more context and detail. Generate 3 expanded versions.',
56
+ simplify: 'Simplify this query to its core intent. Generate 3 simpler versions.',
57
+ decompose: 'Break this query into smaller, focused sub-queries. Generate 3-5 sub-queries.',
58
+ rephrase: 'Rephrase this query in different ways while keeping the same meaning. Generate 3 versions.',
59
+ auto: 'Optimize this query for better search results. Generate 3 improved versions.'
60
+ };
61
+ return prompts[strategy] + ' Output one query per line.';
62
+ }
63
+ buildPrompt(query, strategy) {
64
+ return `Query: ${query}\n\nRewrite using ${strategy} strategy:`;
65
+ }
66
+ parseRewrittenQueries(response) {
67
+ return response
68
+ .split('\n')
69
+ .map(line => line.replace(/^\d+[\.\)]\s*/, '').trim())
70
+ .filter(line => line.length > 0 && line.length < 500);
71
+ }
72
+ }
73
+ exports.QueryRewriterAgent = QueryRewriterAgent;
74
+ /**
75
+ * Create a QueryRewriterAgent
76
+ */
77
+ function createQueryRewriterAgent(config) {
78
+ return new QueryRewriterAgent(config);
79
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * DeepResearchAgent - Agent for comprehensive research tasks
3
+ */
4
+ export interface Citation {
5
+ title: string;
6
+ url: string;
7
+ snippet?: string;
8
+ }
9
+ export interface ReasoningStep {
10
+ step: number;
11
+ thought: string;
12
+ action?: string;
13
+ result?: string;
14
+ }
15
+ export interface ResearchResponse {
16
+ answer: string;
17
+ citations: Citation[];
18
+ reasoning: ReasoningStep[];
19
+ confidence: number;
20
+ }
21
+ export interface DeepResearchConfig {
22
+ name?: string;
23
+ llm?: string;
24
+ maxIterations?: number;
25
+ searchTool?: (query: string) => Promise<Citation[]>;
26
+ verbose?: boolean;
27
+ }
28
+ /**
29
+ * DeepResearchAgent - Comprehensive research with citations
30
+ */
31
+ export declare class DeepResearchAgent {
32
+ readonly name: string;
33
+ private provider;
34
+ private maxIterations;
35
+ private searchTool?;
36
+ private verbose;
37
+ constructor(config?: DeepResearchConfig);
38
+ /**
39
+ * Conduct deep research on a topic
40
+ */
41
+ research(query: string): Promise<ResearchResponse>;
42
+ private generateSearchQueries;
43
+ private synthesizeAnswer;
44
+ /**
45
+ * Set search tool
46
+ */
47
+ setSearchTool(tool: (query: string) => Promise<Citation[]>): void;
48
+ }
49
+ /**
50
+ * Create a DeepResearchAgent
51
+ */
52
+ export declare function createDeepResearchAgent(config?: DeepResearchConfig): DeepResearchAgent;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ /**
3
+ * DeepResearchAgent - Agent for comprehensive research tasks
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DeepResearchAgent = void 0;
7
+ exports.createDeepResearchAgent = createDeepResearchAgent;
8
+ const providers_1 = require("../llm/providers");
9
+ /**
10
+ * DeepResearchAgent - Comprehensive research with citations
11
+ */
12
+ class DeepResearchAgent {
13
+ constructor(config = {}) {
14
+ this.name = config.name || `ResearchAgent_${Math.random().toString(36).substr(2, 9)}`;
15
+ this.provider = (0, providers_1.createProvider)(config.llm || 'openai/gpt-4o-mini');
16
+ this.maxIterations = config.maxIterations ?? 5;
17
+ this.searchTool = config.searchTool;
18
+ this.verbose = config.verbose ?? false;
19
+ }
20
+ /**
21
+ * Conduct deep research on a topic
22
+ */
23
+ async research(query) {
24
+ const reasoning = [];
25
+ const citations = [];
26
+ let iteration = 0;
27
+ // Step 1: Analyze the query
28
+ reasoning.push({
29
+ step: ++iteration,
30
+ thought: `Analyzing research query: "${query}"`,
31
+ action: 'query_analysis'
32
+ });
33
+ // Step 2: Generate search queries
34
+ const searchQueries = await this.generateSearchQueries(query);
35
+ reasoning.push({
36
+ step: ++iteration,
37
+ thought: `Generated ${searchQueries.length} search queries`,
38
+ action: 'generate_queries',
39
+ result: searchQueries.join(', ')
40
+ });
41
+ // Step 3: Execute searches if tool available
42
+ if (this.searchTool) {
43
+ for (const sq of searchQueries.slice(0, 3)) {
44
+ try {
45
+ const results = await this.searchTool(sq);
46
+ citations.push(...results);
47
+ reasoning.push({
48
+ step: ++iteration,
49
+ thought: `Searched for: "${sq}"`,
50
+ action: 'web_search',
51
+ result: `Found ${results.length} results`
52
+ });
53
+ }
54
+ catch (error) {
55
+ if (this.verbose) {
56
+ console.log(`[Research] Search failed for: ${sq}`);
57
+ }
58
+ }
59
+ }
60
+ }
61
+ // Step 4: Synthesize answer
62
+ const answer = await this.synthesizeAnswer(query, citations);
63
+ reasoning.push({
64
+ step: ++iteration,
65
+ thought: 'Synthesizing final answer from gathered information',
66
+ action: 'synthesize',
67
+ result: 'Answer generated'
68
+ });
69
+ // Calculate confidence based on citations
70
+ const confidence = Math.min(0.5 + (citations.length * 0.1), 0.95);
71
+ return {
72
+ answer,
73
+ citations,
74
+ reasoning,
75
+ confidence
76
+ };
77
+ }
78
+ async generateSearchQueries(query) {
79
+ const result = await this.provider.generateText({
80
+ messages: [
81
+ {
82
+ role: 'system',
83
+ content: 'Generate 3 search queries to research this topic. Output only the queries, one per line.'
84
+ },
85
+ { role: 'user', content: query }
86
+ ]
87
+ });
88
+ return result.text.split('\n').filter(q => q.trim().length > 0).slice(0, 3);
89
+ }
90
+ async synthesizeAnswer(query, citations) {
91
+ const context = citations.length > 0
92
+ ? `\n\nRelevant sources:\n${citations.map(c => `- ${c.title}: ${c.snippet || ''}`).join('\n')}`
93
+ : '';
94
+ const result = await this.provider.generateText({
95
+ messages: [
96
+ {
97
+ role: 'system',
98
+ content: 'You are a research assistant. Provide a comprehensive, well-structured answer based on the available information. Cite sources when available.'
99
+ },
100
+ { role: 'user', content: `${query}${context}` }
101
+ ]
102
+ });
103
+ return result.text;
104
+ }
105
+ /**
106
+ * Set search tool
107
+ */
108
+ setSearchTool(tool) {
109
+ this.searchTool = tool;
110
+ }
111
+ }
112
+ exports.DeepResearchAgent = DeepResearchAgent;
113
+ /**
114
+ * Create a DeepResearchAgent
115
+ */
116
+ function createDeepResearchAgent(config) {
117
+ return new DeepResearchAgent(config);
118
+ }