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.
- 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/image.d.ts +51 -0
- package/dist/agent/image.js +93 -0
- package/dist/agent/prompt-expander.d.ts +40 -0
- package/dist/agent/prompt-expander.js +84 -0
- package/dist/agent/query-rewriter.d.ts +38 -0
- package/dist/agent/query-rewriter.js +79 -0
- package/dist/agent/research.d.ts +52 -0
- package/dist/agent/research.js +118 -0
- package/dist/agent/router.d.ts +77 -0
- package/dist/agent/router.js +113 -0
- package/dist/agent/simple.js +1 -1
- package/dist/agent/types.js +2 -2
- package/dist/auto/index.d.ts +56 -0
- package/dist/auto/index.js +142 -0
- 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/guardrails/llm-guardrail.d.ts +40 -0
- package/dist/guardrails/llm-guardrail.js +91 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +122 -1
- package/dist/knowledge/chunking.d.ts +55 -0
- package/dist/knowledge/chunking.js +157 -0
- package/dist/knowledge/rag.d.ts +80 -0
- package/dist/knowledge/rag.js +147 -0
- package/dist/llm/openai.js +1 -1
- 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/memory/memory.d.ts +92 -0
- package/dist/memory/memory.js +169 -0
- package/dist/observability/index.d.ts +86 -0
- package/dist/observability/index.js +166 -0
- package/dist/planning/index.d.ts +133 -0
- package/dist/planning/index.js +228 -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/telemetry/index.d.ts +102 -0
- package/dist/telemetry/index.js +187 -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 +5 -2
|
@@ -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;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workflows - Pipeline and orchestration patterns
|
|
3
|
+
*/
|
|
4
|
+
export type StepStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped';
|
|
5
|
+
export interface StepResult<T = any> {
|
|
6
|
+
stepId: string;
|
|
7
|
+
stepName: string;
|
|
8
|
+
status: StepStatus;
|
|
9
|
+
output?: T;
|
|
10
|
+
error?: Error;
|
|
11
|
+
duration: number;
|
|
12
|
+
startedAt: number;
|
|
13
|
+
completedAt?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface WorkflowContext {
|
|
16
|
+
workflowId: string;
|
|
17
|
+
stepResults: Map<string, StepResult>;
|
|
18
|
+
metadata: Record<string, any>;
|
|
19
|
+
get<T = any>(stepName: string): T | undefined;
|
|
20
|
+
set(key: string, value: any): void;
|
|
21
|
+
}
|
|
22
|
+
export type StepFunction<TInput = any, TOutput = any> = (input: TInput, context: WorkflowContext) => Promise<TOutput> | TOutput;
|
|
23
|
+
export interface WorkflowStepConfig<TInput = any, TOutput = any> {
|
|
24
|
+
name: string;
|
|
25
|
+
execute: StepFunction<TInput, TOutput>;
|
|
26
|
+
condition?: (context: WorkflowContext) => boolean;
|
|
27
|
+
onError?: 'fail' | 'skip' | 'retry';
|
|
28
|
+
maxRetries?: number;
|
|
29
|
+
timeout?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Workflow Step
|
|
33
|
+
*/
|
|
34
|
+
export declare class WorkflowStep<TInput = any, TOutput = any> {
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly execute: StepFunction<TInput, TOutput>;
|
|
38
|
+
readonly condition?: (context: WorkflowContext) => boolean;
|
|
39
|
+
readonly onError: 'fail' | 'skip' | 'retry';
|
|
40
|
+
readonly maxRetries: number;
|
|
41
|
+
readonly timeout?: number;
|
|
42
|
+
constructor(config: WorkflowStepConfig<TInput, TOutput>);
|
|
43
|
+
run(input: TInput, context: WorkflowContext): Promise<StepResult<TOutput>>;
|
|
44
|
+
private executeWithTimeout;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Workflow - Sequential pipeline execution
|
|
48
|
+
*/
|
|
49
|
+
export declare class Workflow<TInput = any, TOutput = any> {
|
|
50
|
+
readonly id: string;
|
|
51
|
+
readonly name: string;
|
|
52
|
+
private steps;
|
|
53
|
+
constructor(name: string);
|
|
54
|
+
/**
|
|
55
|
+
* Add a step to the workflow
|
|
56
|
+
*/
|
|
57
|
+
addStep<TStepInput = any, TStepOutput = any>(config: WorkflowStepConfig<TStepInput, TStepOutput>): this;
|
|
58
|
+
/**
|
|
59
|
+
* Add a step using a simpler syntax
|
|
60
|
+
*/
|
|
61
|
+
step<TStepInput = any, TStepOutput = any>(name: string, execute: StepFunction<TStepInput, TStepOutput>): this;
|
|
62
|
+
/**
|
|
63
|
+
* Run the workflow
|
|
64
|
+
*/
|
|
65
|
+
run(input: TInput): Promise<{
|
|
66
|
+
output: TOutput | undefined;
|
|
67
|
+
results: StepResult[];
|
|
68
|
+
context: WorkflowContext;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* Get step count
|
|
72
|
+
*/
|
|
73
|
+
get stepCount(): number;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Pipeline - Alias for Workflow
|
|
77
|
+
*/
|
|
78
|
+
export declare const Pipeline: typeof Workflow;
|
|
79
|
+
/**
|
|
80
|
+
* Parallel execution helper
|
|
81
|
+
*/
|
|
82
|
+
export declare function parallel<T>(tasks: Array<() => Promise<T>>): Promise<T[]>;
|
|
83
|
+
/**
|
|
84
|
+
* Route helper - Execute based on condition
|
|
85
|
+
*/
|
|
86
|
+
export declare function route<T>(conditions: Array<{
|
|
87
|
+
condition: () => boolean;
|
|
88
|
+
execute: () => Promise<T>;
|
|
89
|
+
}>, defaultExecute?: () => Promise<T>): Promise<T | undefined>;
|
|
90
|
+
/**
|
|
91
|
+
* Loop helper - Repeat until condition
|
|
92
|
+
*/
|
|
93
|
+
export declare function loop<T>(execute: (iteration: number) => Promise<T>, shouldContinue: (result: T, iteration: number) => boolean, maxIterations?: number): Promise<T[]>;
|
|
94
|
+
/**
|
|
95
|
+
* Repeat helper - Execute N times
|
|
96
|
+
*/
|
|
97
|
+
export declare function repeat<T>(execute: (iteration: number) => Promise<T>, times: number): Promise<T[]>;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Workflows - Pipeline and orchestration patterns
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Pipeline = exports.Workflow = exports.WorkflowStep = void 0;
|
|
7
|
+
exports.parallel = parallel;
|
|
8
|
+
exports.route = route;
|
|
9
|
+
exports.loop = loop;
|
|
10
|
+
exports.repeat = repeat;
|
|
11
|
+
const crypto_1 = require("crypto");
|
|
12
|
+
/**
|
|
13
|
+
* Workflow Step
|
|
14
|
+
*/
|
|
15
|
+
class WorkflowStep {
|
|
16
|
+
constructor(config) {
|
|
17
|
+
this.id = (0, crypto_1.randomUUID)();
|
|
18
|
+
this.name = config.name;
|
|
19
|
+
this.execute = config.execute;
|
|
20
|
+
this.condition = config.condition;
|
|
21
|
+
this.onError = config.onError || 'fail';
|
|
22
|
+
this.maxRetries = config.maxRetries || 0;
|
|
23
|
+
this.timeout = config.timeout;
|
|
24
|
+
}
|
|
25
|
+
async run(input, context) {
|
|
26
|
+
const startedAt = Date.now();
|
|
27
|
+
// Check condition
|
|
28
|
+
if (this.condition && !this.condition(context)) {
|
|
29
|
+
return {
|
|
30
|
+
stepId: this.id,
|
|
31
|
+
stepName: this.name,
|
|
32
|
+
status: 'skipped',
|
|
33
|
+
duration: 0,
|
|
34
|
+
startedAt,
|
|
35
|
+
completedAt: Date.now(),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
let lastError;
|
|
39
|
+
let attempts = 0;
|
|
40
|
+
while (attempts <= this.maxRetries) {
|
|
41
|
+
attempts++;
|
|
42
|
+
try {
|
|
43
|
+
const output = await this.executeWithTimeout(input, context);
|
|
44
|
+
const completedAt = Date.now();
|
|
45
|
+
return {
|
|
46
|
+
stepId: this.id,
|
|
47
|
+
stepName: this.name,
|
|
48
|
+
status: 'completed',
|
|
49
|
+
output,
|
|
50
|
+
duration: completedAt - startedAt,
|
|
51
|
+
startedAt,
|
|
52
|
+
completedAt,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
lastError = error;
|
|
57
|
+
if (this.onError === 'skip') {
|
|
58
|
+
return {
|
|
59
|
+
stepId: this.id,
|
|
60
|
+
stepName: this.name,
|
|
61
|
+
status: 'skipped',
|
|
62
|
+
error: lastError,
|
|
63
|
+
duration: Date.now() - startedAt,
|
|
64
|
+
startedAt,
|
|
65
|
+
completedAt: Date.now(),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (this.onError !== 'retry' || attempts > this.maxRetries) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
stepId: this.id,
|
|
75
|
+
stepName: this.name,
|
|
76
|
+
status: 'failed',
|
|
77
|
+
error: lastError,
|
|
78
|
+
duration: Date.now() - startedAt,
|
|
79
|
+
startedAt,
|
|
80
|
+
completedAt: Date.now(),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async executeWithTimeout(input, context) {
|
|
84
|
+
if (!this.timeout) {
|
|
85
|
+
return this.execute(input, context);
|
|
86
|
+
}
|
|
87
|
+
return Promise.race([
|
|
88
|
+
this.execute(input, context),
|
|
89
|
+
new Promise((_, reject) => {
|
|
90
|
+
setTimeout(() => reject(new Error(`Step ${this.name} timed out after ${this.timeout}ms`)), this.timeout);
|
|
91
|
+
}),
|
|
92
|
+
]);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.WorkflowStep = WorkflowStep;
|
|
96
|
+
/**
|
|
97
|
+
* Create a workflow context
|
|
98
|
+
*/
|
|
99
|
+
function createContext(workflowId) {
|
|
100
|
+
const stepResults = new Map();
|
|
101
|
+
const metadata = {};
|
|
102
|
+
return {
|
|
103
|
+
workflowId,
|
|
104
|
+
stepResults,
|
|
105
|
+
metadata,
|
|
106
|
+
get(stepName) {
|
|
107
|
+
const result = stepResults.get(stepName);
|
|
108
|
+
return result?.output;
|
|
109
|
+
},
|
|
110
|
+
set(key, value) {
|
|
111
|
+
metadata[key] = value;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Workflow - Sequential pipeline execution
|
|
117
|
+
*/
|
|
118
|
+
class Workflow {
|
|
119
|
+
constructor(name) {
|
|
120
|
+
this.steps = [];
|
|
121
|
+
this.id = (0, crypto_1.randomUUID)();
|
|
122
|
+
this.name = name;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Add a step to the workflow
|
|
126
|
+
*/
|
|
127
|
+
addStep(config) {
|
|
128
|
+
this.steps.push(new WorkflowStep(config));
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Add a step using a simpler syntax
|
|
133
|
+
*/
|
|
134
|
+
step(name, execute) {
|
|
135
|
+
return this.addStep({ name, execute });
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Run the workflow
|
|
139
|
+
*/
|
|
140
|
+
async run(input) {
|
|
141
|
+
const context = createContext(this.id);
|
|
142
|
+
const results = [];
|
|
143
|
+
let currentInput = input;
|
|
144
|
+
for (const step of this.steps) {
|
|
145
|
+
const result = await step.run(currentInput, context);
|
|
146
|
+
results.push(result);
|
|
147
|
+
context.stepResults.set(step.name, result);
|
|
148
|
+
if (result.status === 'failed') {
|
|
149
|
+
return { output: undefined, results, context };
|
|
150
|
+
}
|
|
151
|
+
if (result.status === 'completed') {
|
|
152
|
+
currentInput = result.output;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const lastResult = results[results.length - 1];
|
|
156
|
+
return {
|
|
157
|
+
output: lastResult?.output,
|
|
158
|
+
results,
|
|
159
|
+
context,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Get step count
|
|
164
|
+
*/
|
|
165
|
+
get stepCount() {
|
|
166
|
+
return this.steps.length;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
exports.Workflow = Workflow;
|
|
170
|
+
/**
|
|
171
|
+
* Pipeline - Alias for Workflow
|
|
172
|
+
*/
|
|
173
|
+
exports.Pipeline = Workflow;
|
|
174
|
+
/**
|
|
175
|
+
* Parallel execution helper
|
|
176
|
+
*/
|
|
177
|
+
async function parallel(tasks) {
|
|
178
|
+
return Promise.all(tasks.map(task => task()));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Route helper - Execute based on condition
|
|
182
|
+
*/
|
|
183
|
+
async function route(conditions, defaultExecute) {
|
|
184
|
+
for (const { condition, execute } of conditions) {
|
|
185
|
+
if (condition()) {
|
|
186
|
+
return execute();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return defaultExecute?.();
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Loop helper - Repeat until condition
|
|
193
|
+
*/
|
|
194
|
+
async function loop(execute, shouldContinue, maxIterations = 100) {
|
|
195
|
+
const results = [];
|
|
196
|
+
let iteration = 0;
|
|
197
|
+
while (iteration < maxIterations) {
|
|
198
|
+
const result = await execute(iteration);
|
|
199
|
+
results.push(result);
|
|
200
|
+
if (!shouldContinue(result, iteration)) {
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
iteration++;
|
|
204
|
+
}
|
|
205
|
+
return results;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Repeat helper - Execute N times
|
|
209
|
+
*/
|
|
210
|
+
async function repeat(execute, times) {
|
|
211
|
+
const results = [];
|
|
212
|
+
for (let i = 0; i < times; i++) {
|
|
213
|
+
results.push(await execute(i));
|
|
214
|
+
}
|
|
215
|
+
return results;
|
|
216
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "praisonai",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "PraisonAI TypeScript AI Agents Framework - Node.js, npm, and Javascript AI Agents Framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"praisonai-ts": "./dist/cli/index.js"
|
|
9
|
+
},
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "tsc",
|
|
9
12
|
"test": "jest",
|
|
@@ -62,7 +65,7 @@
|
|
|
62
65
|
"fast-xml-parser": "^4.5.1",
|
|
63
66
|
"node-fetch": "^2.6.9",
|
|
64
67
|
"openai": "^4.81.0",
|
|
65
|
-
"
|
|
68
|
+
"@modelcontextprotocol/sdk": "^1.12.1"
|
|
66
69
|
},
|
|
67
70
|
"optionalDependencies": {
|
|
68
71
|
"boxen": "^7.1.1",
|