praisonai 1.1.0 → 1.2.1

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.
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ /**
3
+ * AutoAgents - Automatic agent generation from task descriptions
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AutoAgents = void 0;
7
+ exports.createAutoAgents = createAutoAgents;
8
+ const providers_1 = require("../llm/providers");
9
+ /**
10
+ * AutoAgents - Generate agent configurations from task descriptions
11
+ */
12
+ class AutoAgents {
13
+ constructor(config = {}) {
14
+ this.provider = (0, providers_1.createProvider)(config.llm || 'openai/gpt-4o-mini');
15
+ this.pattern = config.pattern || 'sequential';
16
+ this.singleAgent = config.singleAgent ?? false;
17
+ this.verbose = config.verbose ?? false;
18
+ }
19
+ /**
20
+ * Generate agent configuration from task description
21
+ */
22
+ async generate(taskDescription) {
23
+ const prompt = this.buildPrompt(taskDescription);
24
+ const result = await this.provider.generateText({
25
+ messages: [
26
+ { role: 'system', content: this.getSystemPrompt() },
27
+ { role: 'user', content: prompt }
28
+ ]
29
+ });
30
+ return this.parseResponse(result.text);
31
+ }
32
+ /**
33
+ * Recommend a pattern for the task
34
+ */
35
+ recommendPattern(taskDescription) {
36
+ const lower = taskDescription.toLowerCase();
37
+ if (lower.includes('parallel') || lower.includes('concurrent') || lower.includes('simultaneously')) {
38
+ return 'parallel';
39
+ }
40
+ if (lower.includes('route') || lower.includes('classify') || lower.includes('categorize')) {
41
+ return 'routing';
42
+ }
43
+ if (lower.includes('orchestrat') || lower.includes('coordinat') || lower.includes('manage')) {
44
+ return 'orchestrator-workers';
45
+ }
46
+ if (lower.includes('evaluat') || lower.includes('optimi') || lower.includes('improv')) {
47
+ return 'evaluator-optimizer';
48
+ }
49
+ return 'sequential';
50
+ }
51
+ /**
52
+ * Analyze task complexity
53
+ */
54
+ analyzeComplexity(taskDescription) {
55
+ const words = taskDescription.split(/\s+/).length;
56
+ const hasMultipleSteps = /step|then|after|before|first|second|third|finally/i.test(taskDescription);
57
+ const hasMultipleAgents = /team|multiple|several|different|various/i.test(taskDescription);
58
+ if (words < 20 && !hasMultipleSteps && !hasMultipleAgents) {
59
+ return 'simple';
60
+ }
61
+ if (words > 100 || (hasMultipleSteps && hasMultipleAgents)) {
62
+ return 'complex';
63
+ }
64
+ return 'moderate';
65
+ }
66
+ getSystemPrompt() {
67
+ return `You are an AI agent architect. Your job is to analyze task descriptions and generate optimal agent configurations.
68
+
69
+ Output a JSON object with the following structure:
70
+ {
71
+ "agents": [
72
+ {
73
+ "name": "AgentName",
74
+ "role": "Role description",
75
+ "goal": "Agent's goal",
76
+ "backstory": "Optional backstory",
77
+ "instructions": "Specific instructions",
78
+ "tools": ["tool1", "tool2"]
79
+ }
80
+ ],
81
+ "tasks": [
82
+ {
83
+ "description": "Task description",
84
+ "expectedOutput": "What the task should produce",
85
+ "agent": "AgentName"
86
+ }
87
+ ],
88
+ "pattern": "sequential|parallel|hierarchical"
89
+ }
90
+
91
+ Guidelines:
92
+ - For simple tasks, use 1 agent
93
+ - For moderate tasks, use 2-3 agents
94
+ - For complex tasks, use 3-5 agents
95
+ - Match the pattern to the task requirements
96
+ - Be specific about roles and goals`;
97
+ }
98
+ buildPrompt(taskDescription) {
99
+ const complexity = this.analyzeComplexity(taskDescription);
100
+ const recommendedPattern = this.recommendPattern(taskDescription);
101
+ return `Task Description: ${taskDescription}
102
+
103
+ Complexity: ${complexity}
104
+ Recommended Pattern: ${this.pattern || recommendedPattern}
105
+ Single Agent Mode: ${this.singleAgent}
106
+
107
+ Generate an optimal agent configuration for this task.`;
108
+ }
109
+ parseResponse(response) {
110
+ try {
111
+ // Extract JSON from response
112
+ const jsonMatch = response.match(/\{[\s\S]*\}/);
113
+ if (jsonMatch) {
114
+ return JSON.parse(jsonMatch[0]);
115
+ }
116
+ }
117
+ catch (error) {
118
+ // Fallback to default structure
119
+ }
120
+ // Default fallback
121
+ return {
122
+ agents: [{
123
+ name: 'GeneralAgent',
124
+ role: 'General purpose agent',
125
+ goal: 'Complete the assigned task',
126
+ instructions: 'Follow the task description carefully'
127
+ }],
128
+ tasks: [{
129
+ description: 'Complete the task',
130
+ agent: 'GeneralAgent'
131
+ }],
132
+ pattern: 'sequential'
133
+ };
134
+ }
135
+ }
136
+ exports.AutoAgents = AutoAgents;
137
+ /**
138
+ * Create an AutoAgents instance
139
+ */
140
+ function createAutoAgents(config) {
141
+ return new AutoAgents(config);
142
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * LLMGuardrail - LLM-based content validation
3
+ */
4
+ export interface LLMGuardrailConfig {
5
+ name: string;
6
+ criteria: string;
7
+ llm?: string;
8
+ threshold?: number;
9
+ verbose?: boolean;
10
+ }
11
+ export interface LLMGuardrailResult {
12
+ status: 'passed' | 'failed' | 'warning';
13
+ score: number;
14
+ message?: string;
15
+ reasoning?: string;
16
+ }
17
+ /**
18
+ * LLMGuardrail - Use LLM to validate content against criteria
19
+ */
20
+ export declare class LLMGuardrail {
21
+ readonly name: string;
22
+ readonly criteria: string;
23
+ private provider;
24
+ private threshold;
25
+ private verbose;
26
+ constructor(config: LLMGuardrailConfig);
27
+ /**
28
+ * Check content against criteria
29
+ */
30
+ check(content: string): Promise<LLMGuardrailResult>;
31
+ /**
32
+ * Run guardrail (alias for check)
33
+ */
34
+ run(content: string): Promise<LLMGuardrailResult>;
35
+ private parseResponse;
36
+ }
37
+ /**
38
+ * Create an LLMGuardrail
39
+ */
40
+ export declare function createLLMGuardrail(config: LLMGuardrailConfig): LLMGuardrail;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ /**
3
+ * LLMGuardrail - LLM-based content validation
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.LLMGuardrail = void 0;
7
+ exports.createLLMGuardrail = createLLMGuardrail;
8
+ const providers_1 = require("../llm/providers");
9
+ /**
10
+ * LLMGuardrail - Use LLM to validate content against criteria
11
+ */
12
+ class LLMGuardrail {
13
+ constructor(config) {
14
+ this.name = config.name;
15
+ this.criteria = config.criteria;
16
+ this.provider = (0, providers_1.createProvider)(config.llm || 'openai/gpt-4o-mini');
17
+ this.threshold = config.threshold ?? 0.7;
18
+ this.verbose = config.verbose ?? false;
19
+ }
20
+ /**
21
+ * Check content against criteria
22
+ */
23
+ async check(content) {
24
+ const prompt = `Evaluate the following content against this criteria:
25
+
26
+ Criteria: ${this.criteria}
27
+
28
+ Content: ${content}
29
+
30
+ Respond with a JSON object containing:
31
+ - score: number from 0 to 1 (1 = fully meets criteria)
32
+ - passed: boolean
33
+ - reasoning: brief explanation
34
+
35
+ JSON response:`;
36
+ try {
37
+ const result = await this.provider.generateText({
38
+ messages: [{ role: 'user', content: prompt }]
39
+ });
40
+ const parsed = this.parseResponse(result.text);
41
+ if (this.verbose) {
42
+ console.log(`[LLMGuardrail:${this.name}] Score: ${parsed.score}, Passed: ${parsed.status}`);
43
+ }
44
+ return parsed;
45
+ }
46
+ catch (error) {
47
+ return {
48
+ status: 'warning',
49
+ score: 0.5,
50
+ message: `Guardrail check failed: ${error.message}`
51
+ };
52
+ }
53
+ }
54
+ /**
55
+ * Run guardrail (alias for check)
56
+ */
57
+ async run(content) {
58
+ return this.check(content);
59
+ }
60
+ parseResponse(response) {
61
+ try {
62
+ const jsonMatch = response.match(/\{[\s\S]*\}/);
63
+ if (jsonMatch) {
64
+ const parsed = JSON.parse(jsonMatch[0]);
65
+ const score = typeof parsed.score === 'number' ? parsed.score : 0.5;
66
+ return {
67
+ status: score >= this.threshold ? 'passed' : 'failed',
68
+ score,
69
+ reasoning: parsed.reasoning
70
+ };
71
+ }
72
+ }
73
+ catch (e) {
74
+ // Parse failed
75
+ }
76
+ // Fallback parsing
77
+ const hasPositive = /pass|good|valid|accept|meets/i.test(response);
78
+ return {
79
+ status: hasPositive ? 'passed' : 'failed',
80
+ score: hasPositive ? 0.8 : 0.3,
81
+ reasoning: response.substring(0, 200)
82
+ };
83
+ }
84
+ }
85
+ exports.LLMGuardrail = LLMGuardrail;
86
+ /**
87
+ * Create an LLMGuardrail
88
+ */
89
+ function createLLMGuardrail(config) {
90
+ return new LLMGuardrail(config);
91
+ }
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export * from './knowledge';
3
3
  export * from './llm';
4
4
  export * from './memory';
5
5
  export * from './process';
6
- export { Tool, BaseTool, FunctionTool, tool, ToolRegistry, getRegistry, registerTool, getTool, type ToolConfig, type ToolContext, type ToolParameters } from './tools';
6
+ export { BaseTool, ToolResult, ToolValidationError, validateTool, createTool, FunctionTool, tool, ToolRegistry, getRegistry, registerTool, getTool, type ToolConfig, type ToolContext, type ToolParameters } from './tools';
7
7
  export * from './tools/arxivTools';
8
8
  export * from './tools/mcpSse';
9
9
  export * from './session';
@@ -18,4 +18,14 @@ export { accuracyEval, performanceEval, reliabilityEval, EvalSuite, type EvalRes
18
18
  export { MemoryObservabilityAdapter, ConsoleObservabilityAdapter, setObservabilityAdapter, getObservabilityAdapter, type ObservabilityAdapter, type TraceContext, type SpanContext, type SpanData, type TraceData } from './observability';
19
19
  export { SkillManager, createSkillManager, parseSkillFile, type Skill, type SkillMetadata, type SkillDiscoveryOptions } from './skills';
20
20
  export { chat, listProviders, version, help } from './cli';
21
+ export { Memory, createMemory, type MemoryEntry, type MemoryConfig, type SearchResult as MemorySearchResult } from './memory/memory';
22
+ export { TelemetryCollector, getTelemetry, enableTelemetry, disableTelemetry, cleanupTelemetry, type TelemetryEvent, type TelemetryConfig } from './telemetry';
23
+ export { AutoAgents, createAutoAgents, type AgentConfig, type TaskConfig, type TeamStructure, type AutoAgentsConfig } from './auto';
24
+ export { ImageAgent, createImageAgent, type ImageAgentConfig, type ImageGenerationConfig, type ImageAnalysisConfig } from './agent/image';
25
+ export { DeepResearchAgent, createDeepResearchAgent, type DeepResearchConfig, type ResearchResponse, type Citation, type ReasoningStep } from './agent/research';
26
+ export { QueryRewriterAgent, createQueryRewriterAgent, type QueryRewriterConfig, type RewriteResult, type RewriteStrategy } from './agent/query-rewriter';
27
+ export { PromptExpanderAgent, createPromptExpanderAgent, type PromptExpanderConfig, type ExpandResult, type ExpandStrategy } from './agent/prompt-expander';
28
+ export { Chunking, createChunking, type ChunkingConfig, type Chunk, type ChunkStrategy } from './knowledge/chunking';
29
+ export { LLMGuardrail, createLLMGuardrail, type LLMGuardrailConfig, type LLMGuardrailResult } from './guardrails/llm-guardrail';
30
+ export { Plan, PlanStep, TodoList, TodoItem, PlanStorage, createPlan, createTodoList, createPlanStorage, type PlanConfig, type PlanStepConfig, type TodoItemConfig, type PlanStatus, type TodoStatus } from './planning';
21
31
  export { createProvider, getDefaultProvider, parseModelString, isProviderAvailable, getAvailableProviders, OpenAIProvider, AnthropicProvider, GoogleProvider, BaseProvider, type LLMProvider, type ProviderConfig, type ProviderFactory, type GenerateTextOptions, type GenerateTextResult, type StreamTextOptions, type StreamChunk, type GenerateObjectOptions, type GenerateObjectResult, type TokenUsage, type Message as ProviderMessage, type ToolCall, type ToolDefinition as ProviderToolDefinition, } from './llm/providers';
package/dist/index.js CHANGED
@@ -14,7 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.BaseProvider = exports.GoogleProvider = exports.AnthropicProvider = exports.OpenAIProvider = exports.getAvailableProviders = exports.isProviderAvailable = exports.parseModelString = exports.getDefaultProvider = exports.createProvider = exports.help = exports.version = exports.listProviders = exports.chat = exports.parseSkillFile = exports.createSkillManager = exports.SkillManager = exports.getObservabilityAdapter = exports.setObservabilityAdapter = exports.ConsoleObservabilityAdapter = exports.MemoryObservabilityAdapter = exports.EvalSuite = exports.reliabilityEval = exports.performanceEval = exports.accuracyEval = exports.createKnowledgeBase = exports.KnowledgeBase = exports.createContextAgent = exports.ContextAgent = exports.routeConditions = exports.createRouter = exports.RouterAgent = exports.handoffFilters = exports.handoff = exports.Handoff = exports.getTool = exports.registerTool = exports.getRegistry = exports.ToolRegistry = exports.tool = exports.FunctionTool = exports.BaseTool = void 0;
17
+ exports.createQueryRewriterAgent = exports.QueryRewriterAgent = exports.createDeepResearchAgent = exports.DeepResearchAgent = exports.createImageAgent = exports.ImageAgent = exports.createAutoAgents = exports.AutoAgents = exports.cleanupTelemetry = exports.disableTelemetry = exports.enableTelemetry = exports.getTelemetry = exports.TelemetryCollector = exports.createMemory = exports.Memory = exports.help = exports.version = exports.listProviders = exports.chat = exports.parseSkillFile = exports.createSkillManager = exports.SkillManager = exports.getObservabilityAdapter = exports.setObservabilityAdapter = exports.ConsoleObservabilityAdapter = exports.MemoryObservabilityAdapter = exports.EvalSuite = exports.reliabilityEval = exports.performanceEval = exports.accuracyEval = exports.createKnowledgeBase = exports.KnowledgeBase = exports.createContextAgent = exports.ContextAgent = exports.routeConditions = exports.createRouter = exports.RouterAgent = exports.handoffFilters = exports.handoff = exports.Handoff = exports.getTool = exports.registerTool = exports.getRegistry = exports.ToolRegistry = exports.tool = exports.FunctionTool = exports.createTool = exports.validateTool = exports.ToolValidationError = exports.BaseTool = void 0;
18
+ exports.BaseProvider = exports.GoogleProvider = exports.AnthropicProvider = exports.OpenAIProvider = exports.getAvailableProviders = exports.isProviderAvailable = exports.parseModelString = exports.getDefaultProvider = exports.createProvider = exports.createPlanStorage = exports.createTodoList = exports.createPlan = exports.PlanStorage = exports.TodoItem = exports.TodoList = exports.PlanStep = exports.Plan = exports.createLLMGuardrail = exports.LLMGuardrail = exports.createChunking = exports.Chunking = exports.createPromptExpanderAgent = exports.PromptExpanderAgent = void 0;
18
19
  // Export all public modules
19
20
  __exportStar(require("./agent"), exports);
20
21
  __exportStar(require("./knowledge"), exports);
@@ -24,6 +25,9 @@ __exportStar(require("./process"), exports);
24
25
  // Export tools (excluding conflicting types)
25
26
  var tools_1 = require("./tools");
26
27
  Object.defineProperty(exports, "BaseTool", { enumerable: true, get: function () { return tools_1.BaseTool; } });
28
+ Object.defineProperty(exports, "ToolValidationError", { enumerable: true, get: function () { return tools_1.ToolValidationError; } });
29
+ Object.defineProperty(exports, "validateTool", { enumerable: true, get: function () { return tools_1.validateTool; } });
30
+ Object.defineProperty(exports, "createTool", { enumerable: true, get: function () { return tools_1.createTool; } });
27
31
  Object.defineProperty(exports, "FunctionTool", { enumerable: true, get: function () { return tools_1.FunctionTool; } });
28
32
  Object.defineProperty(exports, "tool", { enumerable: true, get: function () { return tools_1.tool; } });
29
33
  Object.defineProperty(exports, "ToolRegistry", { enumerable: true, get: function () { return tools_1.ToolRegistry; } });
@@ -81,6 +85,55 @@ Object.defineProperty(exports, "chat", { enumerable: true, get: function () { re
81
85
  Object.defineProperty(exports, "listProviders", { enumerable: true, get: function () { return cli_1.listProviders; } });
82
86
  Object.defineProperty(exports, "version", { enumerable: true, get: function () { return cli_1.version; } });
83
87
  Object.defineProperty(exports, "help", { enumerable: true, get: function () { return cli_1.help; } });
88
+ // Export Memory
89
+ var memory_1 = require("./memory/memory");
90
+ Object.defineProperty(exports, "Memory", { enumerable: true, get: function () { return memory_1.Memory; } });
91
+ Object.defineProperty(exports, "createMemory", { enumerable: true, get: function () { return memory_1.createMemory; } });
92
+ // Export Telemetry
93
+ var telemetry_1 = require("./telemetry");
94
+ Object.defineProperty(exports, "TelemetryCollector", { enumerable: true, get: function () { return telemetry_1.TelemetryCollector; } });
95
+ Object.defineProperty(exports, "getTelemetry", { enumerable: true, get: function () { return telemetry_1.getTelemetry; } });
96
+ Object.defineProperty(exports, "enableTelemetry", { enumerable: true, get: function () { return telemetry_1.enableTelemetry; } });
97
+ Object.defineProperty(exports, "disableTelemetry", { enumerable: true, get: function () { return telemetry_1.disableTelemetry; } });
98
+ Object.defineProperty(exports, "cleanupTelemetry", { enumerable: true, get: function () { return telemetry_1.cleanupTelemetry; } });
99
+ // Export AutoAgents
100
+ var auto_1 = require("./auto");
101
+ Object.defineProperty(exports, "AutoAgents", { enumerable: true, get: function () { return auto_1.AutoAgents; } });
102
+ Object.defineProperty(exports, "createAutoAgents", { enumerable: true, get: function () { return auto_1.createAutoAgents; } });
103
+ // Export ImageAgent
104
+ var image_1 = require("./agent/image");
105
+ Object.defineProperty(exports, "ImageAgent", { enumerable: true, get: function () { return image_1.ImageAgent; } });
106
+ Object.defineProperty(exports, "createImageAgent", { enumerable: true, get: function () { return image_1.createImageAgent; } });
107
+ // Export DeepResearchAgent
108
+ var research_1 = require("./agent/research");
109
+ Object.defineProperty(exports, "DeepResearchAgent", { enumerable: true, get: function () { return research_1.DeepResearchAgent; } });
110
+ Object.defineProperty(exports, "createDeepResearchAgent", { enumerable: true, get: function () { return research_1.createDeepResearchAgent; } });
111
+ // Export QueryRewriterAgent
112
+ var query_rewriter_1 = require("./agent/query-rewriter");
113
+ Object.defineProperty(exports, "QueryRewriterAgent", { enumerable: true, get: function () { return query_rewriter_1.QueryRewriterAgent; } });
114
+ Object.defineProperty(exports, "createQueryRewriterAgent", { enumerable: true, get: function () { return query_rewriter_1.createQueryRewriterAgent; } });
115
+ // Export PromptExpanderAgent
116
+ var prompt_expander_1 = require("./agent/prompt-expander");
117
+ Object.defineProperty(exports, "PromptExpanderAgent", { enumerable: true, get: function () { return prompt_expander_1.PromptExpanderAgent; } });
118
+ Object.defineProperty(exports, "createPromptExpanderAgent", { enumerable: true, get: function () { return prompt_expander_1.createPromptExpanderAgent; } });
119
+ // Export Chunking
120
+ var chunking_1 = require("./knowledge/chunking");
121
+ Object.defineProperty(exports, "Chunking", { enumerable: true, get: function () { return chunking_1.Chunking; } });
122
+ Object.defineProperty(exports, "createChunking", { enumerable: true, get: function () { return chunking_1.createChunking; } });
123
+ // Export LLMGuardrail
124
+ var llm_guardrail_1 = require("./guardrails/llm-guardrail");
125
+ Object.defineProperty(exports, "LLMGuardrail", { enumerable: true, get: function () { return llm_guardrail_1.LLMGuardrail; } });
126
+ Object.defineProperty(exports, "createLLMGuardrail", { enumerable: true, get: function () { return llm_guardrail_1.createLLMGuardrail; } });
127
+ // Export Planning
128
+ var planning_1 = require("./planning");
129
+ Object.defineProperty(exports, "Plan", { enumerable: true, get: function () { return planning_1.Plan; } });
130
+ Object.defineProperty(exports, "PlanStep", { enumerable: true, get: function () { return planning_1.PlanStep; } });
131
+ Object.defineProperty(exports, "TodoList", { enumerable: true, get: function () { return planning_1.TodoList; } });
132
+ Object.defineProperty(exports, "TodoItem", { enumerable: true, get: function () { return planning_1.TodoItem; } });
133
+ Object.defineProperty(exports, "PlanStorage", { enumerable: true, get: function () { return planning_1.PlanStorage; } });
134
+ Object.defineProperty(exports, "createPlan", { enumerable: true, get: function () { return planning_1.createPlan; } });
135
+ Object.defineProperty(exports, "createTodoList", { enumerable: true, get: function () { return planning_1.createTodoList; } });
136
+ Object.defineProperty(exports, "createPlanStorage", { enumerable: true, get: function () { return planning_1.createPlanStorage; } });
84
137
  // Export providers with explicit names to avoid conflicts
85
138
  var providers_1 = require("./llm/providers");
86
139
  Object.defineProperty(exports, "createProvider", { enumerable: true, get: function () { return providers_1.createProvider; } });
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Chunking - Text chunking utilities for RAG
3
+ */
4
+ export type ChunkStrategy = 'size' | 'sentence' | 'paragraph' | 'semantic';
5
+ export interface ChunkingConfig {
6
+ chunkSize?: number;
7
+ overlap?: number;
8
+ strategy?: ChunkStrategy;
9
+ separator?: string;
10
+ }
11
+ export interface Chunk {
12
+ content: string;
13
+ index: number;
14
+ startOffset: number;
15
+ endOffset: number;
16
+ metadata?: Record<string, any>;
17
+ }
18
+ /**
19
+ * Chunking class for splitting text into chunks
20
+ */
21
+ export declare class Chunking {
22
+ private chunkSize;
23
+ private overlap;
24
+ private strategy;
25
+ private separator;
26
+ constructor(config?: ChunkingConfig);
27
+ /**
28
+ * Chunk text based on configured strategy
29
+ */
30
+ chunk(text: string): Chunk[];
31
+ /**
32
+ * Chunk by fixed size with overlap
33
+ */
34
+ chunkBySize(text: string): Chunk[];
35
+ /**
36
+ * Chunk by sentences
37
+ */
38
+ chunkBySentence(text: string): Chunk[];
39
+ /**
40
+ * Chunk by paragraphs
41
+ */
42
+ chunkByParagraph(text: string): Chunk[];
43
+ /**
44
+ * Chunk by semantic boundaries (simplified)
45
+ */
46
+ chunkBySemantic(text: string): Chunk[];
47
+ /**
48
+ * Merge small chunks
49
+ */
50
+ mergeSmallChunks(chunks: Chunk[], minSize?: number): Chunk[];
51
+ }
52
+ /**
53
+ * Create a Chunking instance
54
+ */
55
+ export declare function createChunking(config?: ChunkingConfig): Chunking;
@@ -1 +1,158 @@
1
1
  "use strict";
2
+ /**
3
+ * Chunking - Text chunking utilities for RAG
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Chunking = void 0;
7
+ exports.createChunking = createChunking;
8
+ /**
9
+ * Chunking class for splitting text into chunks
10
+ */
11
+ class Chunking {
12
+ constructor(config = {}) {
13
+ this.chunkSize = config.chunkSize ?? 500;
14
+ this.overlap = config.overlap ?? 50;
15
+ this.strategy = config.strategy ?? 'size';
16
+ this.separator = config.separator ?? ' ';
17
+ }
18
+ /**
19
+ * Chunk text based on configured strategy
20
+ */
21
+ chunk(text) {
22
+ switch (this.strategy) {
23
+ case 'sentence':
24
+ return this.chunkBySentence(text);
25
+ case 'paragraph':
26
+ return this.chunkByParagraph(text);
27
+ case 'semantic':
28
+ return this.chunkBySemantic(text);
29
+ default:
30
+ return this.chunkBySize(text);
31
+ }
32
+ }
33
+ /**
34
+ * Chunk by fixed size with overlap
35
+ */
36
+ chunkBySize(text) {
37
+ const chunks = [];
38
+ let startOffset = 0;
39
+ let index = 0;
40
+ while (startOffset < text.length) {
41
+ const endOffset = Math.min(startOffset + this.chunkSize, text.length);
42
+ const content = text.slice(startOffset, endOffset);
43
+ chunks.push({
44
+ content,
45
+ index,
46
+ startOffset,
47
+ endOffset
48
+ });
49
+ startOffset = endOffset - this.overlap;
50
+ if (startOffset >= text.length - this.overlap)
51
+ break;
52
+ index++;
53
+ }
54
+ return chunks;
55
+ }
56
+ /**
57
+ * Chunk by sentences
58
+ */
59
+ chunkBySentence(text) {
60
+ const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
61
+ return sentences.map((content, index) => {
62
+ const startOffset = text.indexOf(content);
63
+ return {
64
+ content: content.trim(),
65
+ index,
66
+ startOffset,
67
+ endOffset: startOffset + content.length
68
+ };
69
+ });
70
+ }
71
+ /**
72
+ * Chunk by paragraphs
73
+ */
74
+ chunkByParagraph(text) {
75
+ const paragraphs = text.split(/\n\n+/).filter(p => p.trim().length > 0);
76
+ let offset = 0;
77
+ return paragraphs.map((content, index) => {
78
+ const startOffset = text.indexOf(content, offset);
79
+ offset = startOffset + content.length;
80
+ return {
81
+ content: content.trim(),
82
+ index,
83
+ startOffset,
84
+ endOffset: offset
85
+ };
86
+ });
87
+ }
88
+ /**
89
+ * Chunk by semantic boundaries (simplified)
90
+ */
91
+ chunkBySemantic(text) {
92
+ // Simplified semantic chunking - split on headers, lists, etc.
93
+ const patterns = [
94
+ /^#{1,6}\s+.+$/gm, // Headers
95
+ /^\s*[-*]\s+/gm, // List items
96
+ /^\d+\.\s+/gm // Numbered lists
97
+ ];
98
+ let chunks = [];
99
+ let lastEnd = 0;
100
+ let index = 0;
101
+ // Find semantic boundaries
102
+ const boundaries = [0];
103
+ for (const pattern of patterns) {
104
+ let match;
105
+ while ((match = pattern.exec(text)) !== null) {
106
+ boundaries.push(match.index);
107
+ }
108
+ }
109
+ boundaries.push(text.length);
110
+ boundaries.sort((a, b) => a - b);
111
+ // Create chunks from boundaries
112
+ for (let i = 0; i < boundaries.length - 1; i++) {
113
+ const start = boundaries[i];
114
+ const end = boundaries[i + 1];
115
+ const content = text.slice(start, end).trim();
116
+ if (content.length > 0) {
117
+ chunks.push({
118
+ content,
119
+ index: index++,
120
+ startOffset: start,
121
+ endOffset: end
122
+ });
123
+ }
124
+ }
125
+ return chunks.length > 0 ? chunks : this.chunkBySize(text);
126
+ }
127
+ /**
128
+ * Merge small chunks
129
+ */
130
+ mergeSmallChunks(chunks, minSize = 100) {
131
+ const merged = [];
132
+ let current = null;
133
+ for (const chunk of chunks) {
134
+ if (!current) {
135
+ current = { ...chunk };
136
+ }
137
+ else if (current.content.length < minSize) {
138
+ current.content += '\n' + chunk.content;
139
+ current.endOffset = chunk.endOffset;
140
+ }
141
+ else {
142
+ merged.push(current);
143
+ current = { ...chunk };
144
+ }
145
+ }
146
+ if (current) {
147
+ merged.push(current);
148
+ }
149
+ return merged.map((c, i) => ({ ...c, index: i }));
150
+ }
151
+ }
152
+ exports.Chunking = Chunking;
153
+ /**
154
+ * Create a Chunking instance
155
+ */
156
+ function createChunking(config) {
157
+ return new Chunking(config);
158
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Memory System - Conversation and context memory management
3
+ */
4
+ export interface MemoryEntry {
5
+ id: string;
6
+ content: string;
7
+ role: 'user' | 'assistant' | 'system';
8
+ timestamp: number;
9
+ metadata?: Record<string, any>;
10
+ embedding?: number[];
11
+ }
12
+ export interface MemoryConfig {
13
+ maxEntries?: number;
14
+ maxTokens?: number;
15
+ embeddingProvider?: EmbeddingProvider;
16
+ }
17
+ export interface EmbeddingProvider {
18
+ embed(text: string): Promise<number[]>;
19
+ embedBatch(texts: string[]): Promise<number[][]>;
20
+ }
21
+ export interface SearchResult {
22
+ entry: MemoryEntry;
23
+ score: number;
24
+ }
25
+ /**
26
+ * Memory class for managing conversation history and context
27
+ */
28
+ export declare class Memory {
29
+ private entries;
30
+ private maxEntries;
31
+ private maxTokens;
32
+ private embeddingProvider?;
33
+ constructor(config?: MemoryConfig);
34
+ /**
35
+ * Add a memory entry
36
+ */
37
+ add(content: string, role: 'user' | 'assistant' | 'system', metadata?: Record<string, any>): Promise<MemoryEntry>;
38
+ /**
39
+ * Get a memory entry by ID
40
+ */
41
+ get(id: string): MemoryEntry | undefined;
42
+ /**
43
+ * Get all entries
44
+ */
45
+ getAll(): MemoryEntry[];
46
+ /**
47
+ * Get recent entries
48
+ */
49
+ getRecent(count: number): MemoryEntry[];
50
+ /**
51
+ * Search memory by text similarity
52
+ */
53
+ search(query: string, limit?: number): Promise<SearchResult[]>;
54
+ /**
55
+ * Simple text search fallback
56
+ */
57
+ private textSearch;
58
+ /**
59
+ * Calculate cosine similarity
60
+ */
61
+ private cosineSimilarity;
62
+ /**
63
+ * Delete a memory entry
64
+ */
65
+ delete(id: string): boolean;
66
+ /**
67
+ * Clear all memory
68
+ */
69
+ clear(): void;
70
+ /**
71
+ * Get memory size
72
+ */
73
+ get size(): number;
74
+ /**
75
+ * Build context string from recent memory
76
+ */
77
+ buildContext(count?: number): string;
78
+ /**
79
+ * Export memory to JSON
80
+ */
81
+ toJSON(): MemoryEntry[];
82
+ /**
83
+ * Import memory from JSON
84
+ */
85
+ fromJSON(entries: MemoryEntry[]): void;
86
+ private generateId;
87
+ private enforceLimit;
88
+ }
89
+ /**
90
+ * Create a memory instance
91
+ */
92
+ export declare function createMemory(config?: MemoryConfig): Memory;