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,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Skills System - Agent Skills standard implementation
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.SkillManager = void 0;
|
|
40
|
+
exports.parseSkillFile = parseSkillFile;
|
|
41
|
+
exports.createSkillManager = createSkillManager;
|
|
42
|
+
const fs = __importStar(require("fs"));
|
|
43
|
+
const path = __importStar(require("path"));
|
|
44
|
+
/**
|
|
45
|
+
* Parse SKILL.md file
|
|
46
|
+
*/
|
|
47
|
+
function parseSkillFile(content) {
|
|
48
|
+
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
|
|
49
|
+
if (!frontmatterMatch) {
|
|
50
|
+
throw new Error('Invalid SKILL.md format: missing frontmatter');
|
|
51
|
+
}
|
|
52
|
+
const [, frontmatter, instructions] = frontmatterMatch;
|
|
53
|
+
const metadata = parseYamlFrontmatter(frontmatter);
|
|
54
|
+
if (!metadata.name || !metadata.description) {
|
|
55
|
+
throw new Error('Invalid SKILL.md: name and description are required');
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
metadata: {
|
|
59
|
+
name: metadata.name,
|
|
60
|
+
description: metadata.description,
|
|
61
|
+
license: metadata.license,
|
|
62
|
+
compatibility: metadata.compatibility,
|
|
63
|
+
allowedTools: metadata['allowed-tools']?.split(/\s+/),
|
|
64
|
+
metadata: metadata.metadata
|
|
65
|
+
},
|
|
66
|
+
instructions: instructions.trim()
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Simple YAML frontmatter parser
|
|
71
|
+
*/
|
|
72
|
+
function parseYamlFrontmatter(yaml) {
|
|
73
|
+
const result = {};
|
|
74
|
+
const lines = yaml.split('\n');
|
|
75
|
+
let currentKey = '';
|
|
76
|
+
let inMetadata = false;
|
|
77
|
+
const metadataObj = {};
|
|
78
|
+
for (const line of lines) {
|
|
79
|
+
if (line.startsWith('metadata:')) {
|
|
80
|
+
inMetadata = true;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (inMetadata && line.startsWith(' ')) {
|
|
84
|
+
const match = line.match(/^\s+(\w+):\s*(.*)$/);
|
|
85
|
+
if (match) {
|
|
86
|
+
metadataObj[match[1]] = match[2].replace(/^["']|["']$/g, '');
|
|
87
|
+
}
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
else if (inMetadata && !line.startsWith(' ')) {
|
|
91
|
+
inMetadata = false;
|
|
92
|
+
result.metadata = metadataObj;
|
|
93
|
+
}
|
|
94
|
+
const match = line.match(/^(\S+):\s*(.*)$/);
|
|
95
|
+
if (match) {
|
|
96
|
+
currentKey = match[1];
|
|
97
|
+
result[currentKey] = match[2].replace(/^["']|["']$/g, '');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (inMetadata) {
|
|
101
|
+
result.metadata = metadataObj;
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Skill Manager - Load and manage skills
|
|
107
|
+
*/
|
|
108
|
+
class SkillManager {
|
|
109
|
+
constructor(options = {}) {
|
|
110
|
+
this.skills = new Map();
|
|
111
|
+
this.discoveryPaths = [];
|
|
112
|
+
this.setupDiscoveryPaths(options);
|
|
113
|
+
}
|
|
114
|
+
setupDiscoveryPaths(options) {
|
|
115
|
+
if (options.paths) {
|
|
116
|
+
this.discoveryPaths.push(...options.paths);
|
|
117
|
+
}
|
|
118
|
+
if (options.includeProject !== false) {
|
|
119
|
+
this.discoveryPaths.push('./.praison/skills');
|
|
120
|
+
this.discoveryPaths.push('./.claude/skills');
|
|
121
|
+
}
|
|
122
|
+
if (options.includeUser !== false) {
|
|
123
|
+
const home = process.env.HOME || process.env.USERPROFILE || '';
|
|
124
|
+
if (home) {
|
|
125
|
+
this.discoveryPaths.push(path.join(home, '.praison/skills'));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (options.includeSystem) {
|
|
129
|
+
this.discoveryPaths.push('/etc/praison/skills');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Discover and load skills from configured paths
|
|
134
|
+
*/
|
|
135
|
+
async discover() {
|
|
136
|
+
const discovered = [];
|
|
137
|
+
for (const basePath of this.discoveryPaths) {
|
|
138
|
+
if (!fs.existsSync(basePath))
|
|
139
|
+
continue;
|
|
140
|
+
const entries = fs.readdirSync(basePath, { withFileTypes: true });
|
|
141
|
+
for (const entry of entries) {
|
|
142
|
+
if (entry.isDirectory()) {
|
|
143
|
+
const skillPath = path.join(basePath, entry.name, 'SKILL.md');
|
|
144
|
+
if (fs.existsSync(skillPath)) {
|
|
145
|
+
try {
|
|
146
|
+
const skill = await this.loadSkill(skillPath);
|
|
147
|
+
skill.path = path.join(basePath, entry.name);
|
|
148
|
+
discovered.push(skill);
|
|
149
|
+
this.skills.set(skill.metadata.name, skill);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
// Skip invalid skills
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return discovered;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Load a skill from a SKILL.md file
|
|
162
|
+
*/
|
|
163
|
+
async loadSkill(skillPath) {
|
|
164
|
+
const content = fs.readFileSync(skillPath, 'utf-8');
|
|
165
|
+
return parseSkillFile(content);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Register a skill manually
|
|
169
|
+
*/
|
|
170
|
+
register(skill) {
|
|
171
|
+
this.skills.set(skill.metadata.name, skill);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get a skill by name
|
|
175
|
+
*/
|
|
176
|
+
get(name) {
|
|
177
|
+
return this.skills.get(name);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* List all loaded skills
|
|
181
|
+
*/
|
|
182
|
+
list() {
|
|
183
|
+
return Array.from(this.skills.values());
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Generate XML prompt for skills
|
|
187
|
+
*/
|
|
188
|
+
generatePrompt(skillNames) {
|
|
189
|
+
const skillsToInclude = skillNames
|
|
190
|
+
? skillNames.map(n => this.skills.get(n)).filter(Boolean)
|
|
191
|
+
: this.list();
|
|
192
|
+
if (skillsToInclude.length === 0)
|
|
193
|
+
return '';
|
|
194
|
+
const parts = ['<skills>'];
|
|
195
|
+
for (const skill of skillsToInclude) {
|
|
196
|
+
parts.push(` <skill name="${skill.metadata.name}">`);
|
|
197
|
+
parts.push(` <description>${skill.metadata.description}</description>`);
|
|
198
|
+
parts.push(` <instructions>${skill.instructions}</instructions>`);
|
|
199
|
+
parts.push(' </skill>');
|
|
200
|
+
}
|
|
201
|
+
parts.push('</skills>');
|
|
202
|
+
return parts.join('\n');
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Validate a skill
|
|
206
|
+
*/
|
|
207
|
+
validate(skill) {
|
|
208
|
+
const errors = [];
|
|
209
|
+
if (!skill.metadata.name) {
|
|
210
|
+
errors.push('Missing required field: name');
|
|
211
|
+
}
|
|
212
|
+
else if (!/^[a-z0-9-]{1,64}$/.test(skill.metadata.name)) {
|
|
213
|
+
errors.push('Invalid name: must be 1-64 lowercase chars, numbers, or hyphens');
|
|
214
|
+
}
|
|
215
|
+
if (!skill.metadata.description) {
|
|
216
|
+
errors.push('Missing required field: description');
|
|
217
|
+
}
|
|
218
|
+
else if (skill.metadata.description.length > 1024) {
|
|
219
|
+
errors.push('Description too long: max 1024 characters');
|
|
220
|
+
}
|
|
221
|
+
if (skill.instructions.length > 5000) {
|
|
222
|
+
errors.push('Instructions too long: max 5000 characters');
|
|
223
|
+
}
|
|
224
|
+
return { valid: errors.length === 0, errors };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
exports.SkillManager = SkillManager;
|
|
228
|
+
/**
|
|
229
|
+
* Create a skill manager
|
|
230
|
+
*/
|
|
231
|
+
function createSkillManager(options) {
|
|
232
|
+
return new SkillManager(options);
|
|
233
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Decorator and Registry - Type-safe tool creation and management
|
|
3
|
+
*/
|
|
4
|
+
export interface ToolParameters {
|
|
5
|
+
type: 'object';
|
|
6
|
+
properties: Record<string, {
|
|
7
|
+
type: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
enum?: string[];
|
|
10
|
+
default?: any;
|
|
11
|
+
}>;
|
|
12
|
+
required?: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface ToolConfig<TParams = any, TResult = any> {
|
|
15
|
+
name: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
parameters?: ToolParameters | any;
|
|
18
|
+
category?: string;
|
|
19
|
+
execute: (params: TParams, context?: ToolContext) => Promise<TResult> | TResult;
|
|
20
|
+
}
|
|
21
|
+
export interface ToolContext {
|
|
22
|
+
agentName?: string;
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
runId?: string;
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
export interface ToolDefinition {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
parameters: ToolParameters;
|
|
31
|
+
category?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Tool class - Represents an executable tool
|
|
35
|
+
*/
|
|
36
|
+
export declare class FunctionTool<TParams = any, TResult = any> {
|
|
37
|
+
readonly name: string;
|
|
38
|
+
readonly description: string;
|
|
39
|
+
readonly parameters: ToolParameters;
|
|
40
|
+
readonly category?: string;
|
|
41
|
+
private readonly executeFn;
|
|
42
|
+
constructor(config: ToolConfig<TParams, TResult>);
|
|
43
|
+
private normalizeParameters;
|
|
44
|
+
private zodToJsonSchema;
|
|
45
|
+
private zodFieldToJsonSchema;
|
|
46
|
+
execute(params: TParams, context?: ToolContext): Promise<TResult>;
|
|
47
|
+
/**
|
|
48
|
+
* Get the tool definition for LLM
|
|
49
|
+
*/
|
|
50
|
+
getDefinition(): ToolDefinition;
|
|
51
|
+
/**
|
|
52
|
+
* Get OpenAI-compatible tool format
|
|
53
|
+
*/
|
|
54
|
+
toOpenAITool(): {
|
|
55
|
+
type: 'function';
|
|
56
|
+
function: {
|
|
57
|
+
name: string;
|
|
58
|
+
description: string;
|
|
59
|
+
parameters: any;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create a tool from a configuration object
|
|
65
|
+
*/
|
|
66
|
+
export declare function tool<TParams = any, TResult = any>(config: ToolConfig<TParams, TResult>): FunctionTool<TParams, TResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Tool Registry - Manages tool registration and lookup
|
|
69
|
+
*/
|
|
70
|
+
export declare class ToolRegistry {
|
|
71
|
+
private tools;
|
|
72
|
+
register(tool: FunctionTool, options?: {
|
|
73
|
+
overwrite?: boolean;
|
|
74
|
+
}): this;
|
|
75
|
+
get(name: string): FunctionTool | undefined;
|
|
76
|
+
has(name: string): boolean;
|
|
77
|
+
list(): FunctionTool[];
|
|
78
|
+
getByCategory(category: string): FunctionTool[];
|
|
79
|
+
getDefinitions(): ToolDefinition[];
|
|
80
|
+
toOpenAITools(): Array<{
|
|
81
|
+
type: 'function';
|
|
82
|
+
function: any;
|
|
83
|
+
}>;
|
|
84
|
+
delete(name: string): boolean;
|
|
85
|
+
clear(): void;
|
|
86
|
+
}
|
|
87
|
+
export declare function getRegistry(): ToolRegistry;
|
|
88
|
+
export declare function registerTool(tool: FunctionTool, options?: {
|
|
89
|
+
overwrite?: boolean;
|
|
90
|
+
}): void;
|
|
91
|
+
export declare function getTool(name: string): FunctionTool | undefined;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Tool Decorator and Registry - Type-safe tool creation and management
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ToolRegistry = exports.FunctionTool = void 0;
|
|
7
|
+
exports.tool = tool;
|
|
8
|
+
exports.getRegistry = getRegistry;
|
|
9
|
+
exports.registerTool = registerTool;
|
|
10
|
+
exports.getTool = getTool;
|
|
11
|
+
/**
|
|
12
|
+
* Tool class - Represents an executable tool
|
|
13
|
+
*/
|
|
14
|
+
class FunctionTool {
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.name = config.name;
|
|
17
|
+
this.description = config.description || `Function ${config.name}`;
|
|
18
|
+
this.parameters = this.normalizeParameters(config.parameters);
|
|
19
|
+
this.category = config.category;
|
|
20
|
+
this.executeFn = config.execute;
|
|
21
|
+
}
|
|
22
|
+
normalizeParameters(params) {
|
|
23
|
+
if (!params) {
|
|
24
|
+
return { type: 'object', properties: {}, required: [] };
|
|
25
|
+
}
|
|
26
|
+
// Check if it's a Zod schema
|
|
27
|
+
if (params && typeof params.parse === 'function' && typeof params._def === 'object') {
|
|
28
|
+
return this.zodToJsonSchema(params);
|
|
29
|
+
}
|
|
30
|
+
// Already a JSON schema
|
|
31
|
+
return params;
|
|
32
|
+
}
|
|
33
|
+
zodToJsonSchema(zodSchema) {
|
|
34
|
+
// Basic Zod to JSON Schema conversion
|
|
35
|
+
// For full support, use zod-to-json-schema package
|
|
36
|
+
const def = zodSchema._def;
|
|
37
|
+
if (def.typeName === 'ZodObject') {
|
|
38
|
+
const properties = {};
|
|
39
|
+
const required = [];
|
|
40
|
+
for (const [key, value] of Object.entries(def.shape())) {
|
|
41
|
+
const fieldDef = value._def;
|
|
42
|
+
properties[key] = this.zodFieldToJsonSchema(fieldDef);
|
|
43
|
+
// Check if field is required (not optional)
|
|
44
|
+
if (fieldDef.typeName !== 'ZodOptional' && fieldDef.typeName !== 'ZodDefault') {
|
|
45
|
+
required.push(key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { type: 'object', properties, required };
|
|
49
|
+
}
|
|
50
|
+
return { type: 'object', properties: {}, required: [] };
|
|
51
|
+
}
|
|
52
|
+
zodFieldToJsonSchema(def) {
|
|
53
|
+
const typeName = def.typeName;
|
|
54
|
+
switch (typeName) {
|
|
55
|
+
case 'ZodString':
|
|
56
|
+
return { type: 'string', description: def.description };
|
|
57
|
+
case 'ZodNumber':
|
|
58
|
+
return { type: 'number', description: def.description };
|
|
59
|
+
case 'ZodBoolean':
|
|
60
|
+
return { type: 'boolean', description: def.description };
|
|
61
|
+
case 'ZodArray':
|
|
62
|
+
return { type: 'array', items: this.zodFieldToJsonSchema(def.type._def) };
|
|
63
|
+
case 'ZodEnum':
|
|
64
|
+
return { type: 'string', enum: def.values };
|
|
65
|
+
case 'ZodOptional':
|
|
66
|
+
return this.zodFieldToJsonSchema(def.innerType._def);
|
|
67
|
+
case 'ZodDefault':
|
|
68
|
+
const inner = this.zodFieldToJsonSchema(def.innerType._def);
|
|
69
|
+
inner.default = def.defaultValue();
|
|
70
|
+
return inner;
|
|
71
|
+
default:
|
|
72
|
+
return { type: 'string' };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async execute(params, context) {
|
|
76
|
+
// Validate parameters if we have a schema
|
|
77
|
+
// For now, just execute - validation can be added later
|
|
78
|
+
return this.executeFn(params, context);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get the tool definition for LLM
|
|
82
|
+
*/
|
|
83
|
+
getDefinition() {
|
|
84
|
+
return {
|
|
85
|
+
name: this.name,
|
|
86
|
+
description: this.description,
|
|
87
|
+
parameters: this.parameters,
|
|
88
|
+
category: this.category,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get OpenAI-compatible tool format
|
|
93
|
+
*/
|
|
94
|
+
toOpenAITool() {
|
|
95
|
+
return {
|
|
96
|
+
type: 'function',
|
|
97
|
+
function: {
|
|
98
|
+
name: this.name,
|
|
99
|
+
description: this.description,
|
|
100
|
+
parameters: this.parameters,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.FunctionTool = FunctionTool;
|
|
106
|
+
/**
|
|
107
|
+
* Create a tool from a configuration object
|
|
108
|
+
*/
|
|
109
|
+
function tool(config) {
|
|
110
|
+
return new FunctionTool(config);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Tool Registry - Manages tool registration and lookup
|
|
114
|
+
*/
|
|
115
|
+
class ToolRegistry {
|
|
116
|
+
constructor() {
|
|
117
|
+
this.tools = new Map();
|
|
118
|
+
}
|
|
119
|
+
register(tool, options) {
|
|
120
|
+
if (this.tools.has(tool.name) && !options?.overwrite) {
|
|
121
|
+
throw new Error(`Tool '${tool.name}' is already registered. Use { overwrite: true } to replace.`);
|
|
122
|
+
}
|
|
123
|
+
this.tools.set(tool.name, tool);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
get(name) {
|
|
127
|
+
return this.tools.get(name);
|
|
128
|
+
}
|
|
129
|
+
has(name) {
|
|
130
|
+
return this.tools.has(name);
|
|
131
|
+
}
|
|
132
|
+
list() {
|
|
133
|
+
return Array.from(this.tools.values());
|
|
134
|
+
}
|
|
135
|
+
getByCategory(category) {
|
|
136
|
+
return this.list().filter(t => t.category === category);
|
|
137
|
+
}
|
|
138
|
+
getDefinitions() {
|
|
139
|
+
return this.list().map(t => t.getDefinition());
|
|
140
|
+
}
|
|
141
|
+
toOpenAITools() {
|
|
142
|
+
return this.list().map(t => t.toOpenAITool());
|
|
143
|
+
}
|
|
144
|
+
delete(name) {
|
|
145
|
+
return this.tools.delete(name);
|
|
146
|
+
}
|
|
147
|
+
clear() {
|
|
148
|
+
this.tools.clear();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.ToolRegistry = ToolRegistry;
|
|
152
|
+
// Global registry instance
|
|
153
|
+
let globalRegistry = null;
|
|
154
|
+
function getRegistry() {
|
|
155
|
+
if (!globalRegistry) {
|
|
156
|
+
globalRegistry = new ToolRegistry();
|
|
157
|
+
}
|
|
158
|
+
return globalRegistry;
|
|
159
|
+
}
|
|
160
|
+
function registerTool(tool, options) {
|
|
161
|
+
getRegistry().register(tool, options);
|
|
162
|
+
}
|
|
163
|
+
function getTool(name) {
|
|
164
|
+
return getRegistry().get(name);
|
|
165
|
+
}
|
package/dist/tools/index.d.ts
CHANGED
package/dist/tools/index.js
CHANGED
|
@@ -25,5 +25,8 @@ class BaseTool {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
exports.BaseTool = BaseTool;
|
|
28
|
+
// Export decorator and registry
|
|
29
|
+
__exportStar(require("./decorator"), exports);
|
|
28
30
|
// Export all tool modules
|
|
29
31
|
__exportStar(require("./arxivTools"), exports);
|
|
32
|
+
__exportStar(require("./mcpSse"), exports);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
|
+
import { BaseTool } from './index';
|
|
3
|
+
export interface MCPToolInfo {
|
|
4
|
+
name: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
inputSchema?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare class MCPTool extends BaseTool {
|
|
9
|
+
private client;
|
|
10
|
+
private inputSchema;
|
|
11
|
+
constructor(info: MCPToolInfo, client: Client);
|
|
12
|
+
get schemaProperties(): Record<string, any> | undefined;
|
|
13
|
+
execute(args?: any): Promise<any>;
|
|
14
|
+
toOpenAITool(): {
|
|
15
|
+
type: string;
|
|
16
|
+
function: {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
parameters: any;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export declare class MCP implements Iterable<MCPTool> {
|
|
24
|
+
private url;
|
|
25
|
+
private debug;
|
|
26
|
+
tools: MCPTool[];
|
|
27
|
+
private client;
|
|
28
|
+
constructor(url: string, debug?: boolean);
|
|
29
|
+
initialize(): Promise<void>;
|
|
30
|
+
[Symbol.iterator](): Iterator<MCPTool>;
|
|
31
|
+
toOpenAITools(): {
|
|
32
|
+
type: string;
|
|
33
|
+
function: {
|
|
34
|
+
name: string;
|
|
35
|
+
description: string;
|
|
36
|
+
parameters: any;
|
|
37
|
+
};
|
|
38
|
+
}[];
|
|
39
|
+
close(): Promise<void>;
|
|
40
|
+
get isConnected(): boolean;
|
|
41
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MCP = exports.MCPTool = void 0;
|
|
4
|
+
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
|
|
5
|
+
const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
6
|
+
const index_1 = require("./index");
|
|
7
|
+
class MCPTool extends index_1.BaseTool {
|
|
8
|
+
constructor(info, client) {
|
|
9
|
+
super(info.name, info.description || `Call the ${info.name} tool`);
|
|
10
|
+
this.client = client;
|
|
11
|
+
this.inputSchema = info.inputSchema || { type: 'object', properties: {}, required: [] };
|
|
12
|
+
}
|
|
13
|
+
get schemaProperties() {
|
|
14
|
+
return this.inputSchema?.properties;
|
|
15
|
+
}
|
|
16
|
+
async execute(args = {}) {
|
|
17
|
+
try {
|
|
18
|
+
const result = await this.client.callTool({ name: this.name, arguments: args });
|
|
19
|
+
if (result.structuredContent) {
|
|
20
|
+
return result.structuredContent;
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(result.content) && result.content.length > 0) {
|
|
23
|
+
const item = result.content[0];
|
|
24
|
+
if (typeof item.text === 'string')
|
|
25
|
+
return item.text;
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
throw new Error(`Failed to execute tool ${this.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
toOpenAITool() {
|
|
34
|
+
return {
|
|
35
|
+
type: 'function',
|
|
36
|
+
function: {
|
|
37
|
+
name: this.name,
|
|
38
|
+
description: this.description,
|
|
39
|
+
parameters: this.inputSchema
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.MCPTool = MCPTool;
|
|
45
|
+
class MCP {
|
|
46
|
+
constructor(url, debug = false) {
|
|
47
|
+
this.url = url;
|
|
48
|
+
this.debug = debug;
|
|
49
|
+
this.tools = [];
|
|
50
|
+
this.client = null;
|
|
51
|
+
if (debug) {
|
|
52
|
+
console.log(`MCP client initialized for URL: ${url}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async initialize() {
|
|
56
|
+
if (this.client) {
|
|
57
|
+
if (this.debug)
|
|
58
|
+
console.log('MCP client already initialized');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
this.client = new index_js_1.Client({ name: 'praisonai-ts-mcp', version: '1.0.0' });
|
|
63
|
+
const transport = new sse_js_1.SSEClientTransport(new URL(this.url));
|
|
64
|
+
await this.client.connect(transport);
|
|
65
|
+
const { tools } = await this.client.listTools();
|
|
66
|
+
this.tools = tools.map((t) => new MCPTool({
|
|
67
|
+
name: t.name,
|
|
68
|
+
description: t.description,
|
|
69
|
+
inputSchema: t.inputSchema
|
|
70
|
+
}, this.client));
|
|
71
|
+
if (this.debug)
|
|
72
|
+
console.log(`Initialized MCP with ${this.tools.length} tools`);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (this.client) {
|
|
76
|
+
await this.client.close().catch(() => { });
|
|
77
|
+
this.client = null;
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`Failed to initialize MCP client: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
[Symbol.iterator]() {
|
|
83
|
+
return this.tools[Symbol.iterator]();
|
|
84
|
+
}
|
|
85
|
+
toOpenAITools() {
|
|
86
|
+
return this.tools.map(t => t.toOpenAITool());
|
|
87
|
+
}
|
|
88
|
+
async close() {
|
|
89
|
+
if (this.client) {
|
|
90
|
+
try {
|
|
91
|
+
await this.client.close();
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (this.debug) {
|
|
95
|
+
console.warn('Error closing MCP client:', error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
this.client = null;
|
|
100
|
+
this.tools = [];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
get isConnected() {
|
|
105
|
+
return this.client !== null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.MCP = MCP;
|