llmjs2 1.7.1 → 2.0.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.
Files changed (45) hide show
  1. package/README.md +116 -18
  2. package/dist/index.d.mts +146 -0
  3. package/dist/index.d.ts +146 -0
  4. package/dist/index.js +1242 -0
  5. package/dist/index.mjs +1211 -0
  6. package/package.json +32 -58
  7. package/chain/AGENT_STEP_README.md +0 -102
  8. package/chain/README.md +0 -257
  9. package/chain/WORKFLOW_README.md +0 -85
  10. package/chain/agent-step-example.js +0 -232
  11. package/chain/docs/AGENT.md +0 -126
  12. package/chain/docs/GRAPH.md +0 -490
  13. package/chain/examples.js +0 -314
  14. package/chain/index.js +0 -31
  15. package/chain/lib/agent.js +0 -338
  16. package/chain/lib/flow/agent-step.js +0 -119
  17. package/chain/lib/flow/edge.js +0 -24
  18. package/chain/lib/flow/flow.js +0 -76
  19. package/chain/lib/flow/graph.js +0 -331
  20. package/chain/lib/flow/index.js +0 -7
  21. package/chain/lib/flow/step.js +0 -63
  22. package/chain/lib/memory/in-memory.js +0 -117
  23. package/chain/lib/memory/index.js +0 -36
  24. package/chain/lib/memory/lance-memory.js +0 -225
  25. package/chain/lib/memory/sqlite-memory.js +0 -309
  26. package/chain/simple-agent-step-example.js +0 -168
  27. package/chain/workflow-example-usage.js +0 -70
  28. package/chain/workflow-example.json +0 -59
  29. package/core/README.md +0 -485
  30. package/core/cli.js +0 -275
  31. package/core/config.yaml +0 -149
  32. package/core/docs/BASIC_USAGE.md +0 -62
  33. package/core/docs/CLI.md +0 -104
  34. package/core/docs/GET_STARTED.md +0 -129
  35. package/core/docs/GUARDRAILS_GUIDE.md +0 -734
  36. package/core/docs/README.md +0 -47
  37. package/core/docs/ROUTER_GUIDE.md +0 -199
  38. package/core/docs/SERVER_MODE.md +0 -358
  39. package/core/index.js +0 -115
  40. package/core/logger.js +0 -115
  41. package/core/providers/ollama.js +0 -128
  42. package/core/providers/openai.js +0 -112
  43. package/core/providers/openrouter.js +0 -206
  44. package/core/router.js +0 -252
  45. package/core/server.js +0 -203
package/package.json CHANGED
@@ -1,67 +1,41 @@
1
1
  {
2
2
  "name": "llmjs2",
3
- "version": "1.7.1",
4
- "description": "A unified Node.js library for connecting to multiple Large Language Model (LLM) providers: OpenAI, Ollama, and OpenRouter.",
5
- "main": "core/index.js",
6
- "bin": {
7
- "llmjs2": "core/cli.js"
3
+ "version": "2.0.1",
4
+ "description": "A simple lightweight library for LLM completions and Agentic workflow",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
8
14
  },
15
+ "files": [
16
+ "dist"
17
+ ],
9
18
  "scripts": {
10
- "chat:router": "node core/examples/chat-router-sequential-app.js",
11
- "chat:router:guardrail": "node core/examples/chat-router-guardrail.js",
12
- "test": "node core/test.js",
13
- "test:completion": "node core/test-completion.js",
14
- "start": "node core/cli.js",
15
- "lint": "echo 'No linting configured'",
16
- "typecheck": "echo 'No type checking configured'"
19
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
20
+ "test": "vitest run",
21
+ "test:watch": "vitest",
22
+ "prepublishOnly": "npm run build"
17
23
  },
18
- "keywords": [
19
- "llm",
20
- "ai",
21
- "openai",
22
- "ollama",
23
- "openrouter",
24
- "chatgpt",
25
- "gpt",
26
- "llmjs2",
27
- "llmjs",
28
- "language-model",
29
- "unified-api"
30
- ],
24
+ "keywords": [],
31
25
  "author": "",
32
- "license": "MIT",
33
- "repository": {
34
- "type": "git",
35
- "url": ""
26
+ "license": "ISC",
27
+ "type": "commonjs",
28
+ "devDependencies": {
29
+ "@types/node": "^25.8.0",
30
+ "dotenv": "^17.4.2",
31
+ "pino-pretty": "^13.1.3",
32
+ "tsup": "^8.5.1",
33
+ "typescript": "^6.0.3",
34
+ "vitest": "^4.1.6"
36
35
  },
37
36
  "dependencies": {
38
- "@prompy/prompy": "^1.0.2",
39
- "yaml": "^2.0.0"
40
- },
41
- "engines": {
42
- "node": ">=14.0.0"
43
- },
44
- "files": [
45
- "chain/index.js",
46
- "chain/lib/",
47
- "chain/docs/",
48
- "chain/examples.js",
49
- "chain/agent-step-example.js",
50
- "chain/simple-agent-step-example.js",
51
- "chain/workflow-example-usage.js",
52
- "chain/workflow-example.json",
53
- "chain/AGENT_STEP_README.md",
54
- "chain/WORKFLOW_README.md",
55
- "chain/README.md",
56
- "core/cli.js",
57
- "core/config.yaml",
58
- "core/index.js",
59
- "core/logger.js",
60
- "core/router.js",
61
- "core/server.js",
62
- "core/providers/",
63
- "core/docs/",
64
- "core/README.md",
65
- "README.md"
66
- ]
37
+ "any-markdown": "latest",
38
+ "pino": "^10.3.1",
39
+ "promappjs": "^1.2.12"
40
+ }
67
41
  }
@@ -1,102 +0,0 @@
1
- # AgentStep Examples
2
-
3
- This directory contains example programs demonstrating how to use AgentStep in llmjs-chain workflows.
4
-
5
- ## Files
6
-
7
- - **`simple-agent-step-example.js`** - Complete example with mock agents (fast execution)
8
- - **`agent-step-example.js`** - Advanced example with real AI agents (slower but comprehensive)
9
-
10
- ## Running the Examples
11
-
12
- ### Simple Example (Recommended for Testing)
13
- ```bash
14
- node simple-agent-step-example.js
15
- ```
16
-
17
- **Features Demonstrated:**
18
- - ✅ Function-based input/output mapping
19
- - ✅ Template-based input mapping (`{{variable}}` syntax)
20
- - ✅ Default mapping behavior
21
- - ✅ Sequential workflow execution
22
- - ✅ Parallel agent execution
23
- - ✅ Context passing between steps
24
- - ✅ Mock agents for fast testing
25
-
26
- ### Advanced Example (Real AI)
27
- ```bash
28
- node agent-step-example.js
29
- ```
30
-
31
- **Features Demonstrated:**
32
- - ✅ Real AI agent integration
33
- - ✅ Multi-agent content creation pipeline
34
- - ✅ Complex input/output mapping
35
- - ✅ Tool usage within agents
36
- - ✅ Error handling
37
-
38
- ## Key AgentStep Features
39
-
40
- ### Input Mapping Options
41
-
42
- ```javascript
43
- // 1. Function mapper - full control
44
- inputMapper: (context) => `Process: ${JSON.stringify(context.data)}`
45
-
46
- // 2. Template mapper - simple interpolation
47
- inputMapper: 'Analyze {{data}} from step {{stepName}}'
48
-
49
- // 3. Default mapper - automatic context conversion
50
- // (no inputMapper specified)
51
- ```
52
-
53
- ### Output Mapping Options
54
-
55
- ```javascript
56
- // Custom transformation
57
- outputMapper: (agentResponse, context) => ({
58
- result: agentResponse,
59
- processedAt: new Date(),
60
- confidence: 0.95
61
- })
62
-
63
- // Default mapping returns: { response, agent, timestamp }
64
- ```
65
-
66
- ### Workflow Integration
67
-
68
- ```javascript
69
- const workflow = new Graph({ name: 'ai-workflow' })
70
- .step(agentStep1, agentStep2, regularStep)
71
- .edge(agentStep1, agentStep2)
72
- .edge(agentStep2, regularStep)
73
- .compile();
74
-
75
- const result = await workflow.run(initialContext);
76
- ```
77
-
78
- ## Use Cases
79
-
80
- - **Content Creation**: Multi-agent writing and editing pipelines
81
- - **Data Analysis**: Specialized agents for different analysis stages
82
- - **Quality Assurance**: AI-powered validation and testing
83
- - **Decision Making**: Agent-based routing and recommendations
84
- - **Integration Workflows**: Mix of AI and traditional processing steps
85
-
86
- ## Performance Notes
87
-
88
- - **Mock Agents**: Use for development and testing (fast)
89
- - **Real Agents**: Production use with proper rate limiting
90
- - **Parallel Execution**: Multiple agents can run simultaneously
91
- - **Context Size**: Large contexts may impact performance
92
-
93
- ## Integration with Existing Workflows
94
-
95
- AgentStep is fully compatible with:
96
- - Regular Step classes
97
- - Graph.load() JSON workflows
98
- - Conditional edges
99
- - Parallel execution
100
- - Memory systems
101
-
102
- Start with the simple example to understand the API, then move to real agents for production use!
package/chain/README.md DELETED
@@ -1,257 +0,0 @@
1
- # llmjs-chain
2
-
3
- An extension of llmjs2 that provides chaining capabilities for building complex LLM workflows with Agents, Graphs, Steps, and Flows.
4
-
5
- ## Features
6
-
7
- - **Step**: Basic unit of work that can execute LLM calls or custom processing
8
- - **Flow**: Linear sequence of steps that execute in order
9
- - **Agent**: Intelligent entity that can make decisions and use tools
10
- - **Graph**: Complex execution graph with branching and parallelism
11
- - **Full llmjs2 compatibility**: All llmjs2 features are available
12
-
13
- ## Installation
14
-
15
- ```bash
16
- npm install llmjs-chain
17
- ```
18
-
19
- This will automatically install llmjs2 as a dependency.
20
-
21
- ## Quick Start
22
-
23
- ```javascript
24
- import { Step, Flow, Agent, Graph, completion } from 'llmjs-chain';
25
-
26
- // Set API keys (same as llmjs2)
27
- process.env.OPENAI_API_KEY = 'your-openai-key';
28
-
29
- // Basic completion (from llmjs2)
30
- const response = await completion('Hello, how are you?');
31
- console.log(response);
32
- ```
33
-
34
- ## Terminal Chat App
35
-
36
- Experience llmjs-chain with an interactive terminal chat interface that automatically detects available API keys and models:
37
-
38
- ```bash
39
- npm run chat
40
- ```
41
-
42
- Features:
43
- - Conversational chat with memory persistence
44
- - Built-in tools: `get_current_time`, `calculate`
45
- - Commands: `/help`, `/history`, `/memory`, `/clear`, `/exit`
46
- - Automatic API key and model detection (no configuration needed)
47
- - Automatic model routing and tool execution
48
-
49
- The chat app will automatically use whichever API keys are available in your environment variables, following the same auto-detection logic as llmjs2.
50
-
51
- ## Components
52
-
53
- ### Step
54
-
55
- A Step is the basic unit of work in a chain. It can execute an LLM call or custom processing logic.
56
-
57
- ```javascript
58
- import { Step } from 'llmjs-chain';
59
-
60
- const step = new Step('my-step', {
61
- processor: async (context, inputs) => {
62
- // Custom processing logic
63
- return { result: `Processed: ${inputs.data}` };
64
- }
65
- });
66
-
67
- const result = await step.execute({}, { data: 'Hello World' });
68
- ```
69
-
70
- ### Flow
71
-
72
- A Flow executes steps in a linear sequence.
73
-
74
- ```javascript
75
- import { Step, Flow } from 'llmjs-chain';
76
-
77
- const step1 = new Step('step1', {
78
- processor: async (context, inputs) => {
79
- return { data: inputs.input.toUpperCase() };
80
- }
81
- });
82
-
83
- const step2 = new Step('step2', {
84
- processor: async (context, inputs) => {
85
- return { result: `Final: ${inputs.step1.data}` };
86
- }
87
- });
88
-
89
- const flow = new Flow('my-flow');
90
- flow.addStep(step1);
91
- flow.addStep(step2);
92
-
93
- const result = await flow.execute({ input: 'hello' });
94
- console.log(result); // { result: 'Final: HELLO' }
95
- ```
96
-
97
- ### Agent
98
-
99
- An Agent is an intelligent entity that can make decisions and use tools.
100
-
101
- ```javascript
102
- import { Agent } from 'llmjs-chain';
103
-
104
- const agent = new Agent({
105
- model: 'openai/gpt-4',
106
- instruction: 'You are a helpful coding assistant.',
107
- tools: [
108
- {
109
- name: 'run_code',
110
- description: 'Execute JavaScript code',
111
- parameters: {
112
- type: 'object',
113
- properties: {
114
- code: { type: 'string' }
115
- }
116
- },
117
- execute: async ({ code }) => {
118
- return eval(code);
119
- }
120
- }
121
- ]
122
- });
123
-
124
- // Execute task
125
- const result = await agent.generate('Calculate 2 + 2 and tell me the result');
126
- ```
127
-
128
- ### Memory Factory
129
-
130
- llmjs-chain provides a memory factory for creating different types of memory storage:
131
-
132
- ```javascript
133
- import { memory } from 'llmjs-chain';
134
-
135
- // In-memory storage (default)
136
- const inMemory = memory.create();
137
-
138
- // LanceDB vector storage
139
- const lanceMemory = memory.create({ type: 'lancedb', config: { db: 'my_db', dir: './data' } });
140
-
141
- // SQLite storage with vector support
142
- const sqliteMemory = memory.create({ type: 'sqlite', config: { db: './memory.db' } });
143
-
144
- // Direct class instantiation (alternative)
145
- import { InMemory, LanceMemory, SQLiteMemory } from 'llmjs-chain';
146
- const directMemory = new InMemory();
147
- ```
148
-
149
- ### Memory Classes
150
-
151
- #### InMemory
152
- Basic in-memory storage for conversation history:
153
-
154
- ```javascript
155
- const memory = new InMemory();
156
-
157
- // Save messages
158
- await memory.save('Hello!', 'user123', 'thread456', 'user', 'text');
159
- await memory.save('Hi there!', 'user123', 'thread456', 'assistant', 'text');
160
-
161
- // Get session history
162
- const history = await memory.getSessionHistory('user123', 'thread456', 10);
163
-
164
- // Search messages
165
- const results = await memory.search('hello', 'user123', 5);
166
- ```
167
-
168
- ### Graph
169
-
170
- A Graph allows complex execution with branching and parallelism.
171
-
172
- ```javascript
173
- import { Step, Graph } from 'llmjs-chain';
174
-
175
- const graph = new Graph('my-graph');
176
-
177
- // Create nodes
178
- const start = new Step('start', {
179
- processor: async (context, inputs) => ({ value: inputs.number })
180
- });
181
-
182
- const double = new Step('double', {
183
- processor: async (context, inputs) => ({ result: inputs.start.value * 2 })
184
- });
185
-
186
- const square = new Step('square', {
187
- processor: async (context, inputs) => ({ result: inputs.start.value ** 2 })
188
- });
189
-
190
- // Add nodes and edges
191
- graph.addNode(start);
192
- graph.addNode(double);
193
- graph.addNode(square);
194
-
195
- graph.addEdge('start', 'double');
196
- graph.addEdge('start', 'square');
197
-
198
- // Execute graph
199
- const results = await graph.execute(['start'], { number: 5 });
200
- console.log(results);
201
- // {
202
- // start: { value: 5 },
203
- // double: { result: 10 },
204
- // square: { result: 25 }
205
- // }
206
- ```
207
-
208
- ## Advanced Usage
209
-
210
- ### Conditional Graph Execution
211
-
212
- ```javascript
213
- graph.addEdge('start', 'path-a', (result, context) => result.value > 10);
214
- graph.addEdge('start', 'path-b', (result, context) => result.value <= 10);
215
- ```
216
-
217
- ### Agent with Memory
218
-
219
- ```javascript
220
- agent.remember({ role: 'user', content: 'Remember that I like coffee' });
221
- agent.remember({ role: 'assistant', content: 'Got it, you like coffee!' });
222
-
223
- // Later conversations will include this memory
224
- const response = await agent.execute('What do I like to drink?');
225
- ```
226
-
227
- ### Step Dependencies
228
-
229
- ```javascript
230
- const step = new Step('dependent-step', {
231
- dependencies: ['previous-step'],
232
- processor: async (context, inputs) => {
233
- // This step will only execute after 'previous-step' has completed
234
- return { result: inputs['previous-step'].data };
235
- }
236
- });
237
- ```
238
-
239
- ## API Keys Setup
240
-
241
- Same as llmjs2:
242
-
243
- ```bash
244
- export OPENAI_API_KEY=your_openai_api_key
245
- export OLLAMA_API_KEY=your_ollama_api_key
246
- export OPEN_ROUTER_API_KEY=your_openrouter_api_key
247
- ```
248
-
249
- ## Testing
250
-
251
- ```bash
252
- npm test
253
- ```
254
-
255
- ## License
256
-
257
- MIT
@@ -1,85 +0,0 @@
1
- # Workflow Example
2
-
3
- This directory contains a complete example of defining and running workflows using llmjs-chain's Graph system.
4
-
5
- ## Files
6
-
7
- - **`workflow-example.json`** - JSON definition of a sample workflow
8
- - **`workflow-example-usage.js`** - Usage example showing how to load and run the workflow
9
-
10
- ## Running the Example
11
-
12
- ```bash
13
- node workflow-example-usage.js
14
- ```
15
-
16
- ## Workflow Description
17
-
18
- The example workflow demonstrates:
19
-
20
- 1. **Input Validation** - Validates that input data exists and is valid
21
- 2. **Parallel Processing** - Two different processing paths run simultaneously
22
- - Path A: Multiplies each value by 2
23
- - Path B: Adds 10 to each value
24
- 3. **Result Checking** - Sums all processed values and checks if total exceeds threshold
25
- 4. **Conditional Branching** - Routes to success or warning handler based on result size
26
-
27
- ## Workflow Structure
28
-
29
- ```
30
- validate → [process-a, process-b] → check-result → [success-handler | warning-handler]
31
- ```
32
-
33
- ## JSON Configuration Format
34
-
35
- ```json
36
- {
37
- "name": "workflow-name",
38
- "description": "Workflow description",
39
- "steps": [
40
- {
41
- "name": "step-name",
42
- "description": "Step description",
43
- "execute": "async (context) => { /* step logic as string */ }"
44
- }
45
- ],
46
- "edges": [
47
- { "from": "step1", "to": "step2" },
48
- { "from": "step2", "to": "step3", "condition": "(result) => result.success" }
49
- ]
50
- }
51
- ```
52
-
53
- ## Key Features Demonstrated
54
-
55
- - ✅ **JSON Workflow Definition** - Define complete workflows in JSON
56
- - ✅ **Parallel Execution** - Multiple steps can run simultaneously
57
- - ✅ **Conditional Edges** - Branch workflow based on step results
58
- - ✅ **Context Passing** - Steps can access results from previous steps
59
- - ✅ **Error Handling** - Validation and error propagation
60
- - ✅ **Dynamic Loading** - Load workflows from files at runtime
61
-
62
- ## Customizing the Example
63
-
64
- 1. **Modify Steps**: Edit the `execute` functions in `workflow-example.json`
65
- 2. **Change Logic**: Update conditions and processing logic
66
- 3. **Add Steps**: Extend the workflow with additional processing steps
67
- 4. **Create New Workflows**: Use this as a template for your own workflows
68
-
69
- ## Integration with Agents
70
-
71
- This workflow system can be combined with AI agents for intelligent processing:
72
-
73
- ```javascript
74
- // Agent step that uses AI for decision making
75
- {
76
- "name": "analyze-data",
77
- "execute": "async (context) => {
78
- const agent = new Agent({ instruction: 'Analyze this data...' });
79
- const result = await agent.generate(context.previousStep.data);
80
- return { analysis: result };
81
- }"
82
- }
83
- ```
84
-
85
- The JSON-based workflow system makes it easy to define, version, and deploy complex processing pipelines with conditional logic and parallel execution.