@cloudwarriors-ai/rlm 0.1.5
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/README.md +208 -0
- package/dist/application/index.d.ts +4 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +4 -0
- package/dist/application/index.js.map +1 -0
- package/dist/application/query-handler.d.ts +49 -0
- package/dist/application/query-handler.d.ts.map +1 -0
- package/dist/application/query-handler.js +311 -0
- package/dist/application/query-handler.js.map +1 -0
- package/dist/application/rlm-service.d.ts +68 -0
- package/dist/application/rlm-service.d.ts.map +1 -0
- package/dist/application/rlm-service.js +132 -0
- package/dist/application/rlm-service.js.map +1 -0
- package/dist/application/session-manager.d.ts +70 -0
- package/dist/application/session-manager.d.ts.map +1 -0
- package/dist/application/session-manager.js +237 -0
- package/dist/application/session-manager.js.map +1 -0
- package/dist/cli/commands/config.d.ts +34 -0
- package/dist/cli/commands/config.d.ts.map +1 -0
- package/dist/cli/commands/config.js +157 -0
- package/dist/cli/commands/config.js.map +1 -0
- package/dist/cli/commands/index.d.ts +4 -0
- package/dist/cli/commands/index.d.ts.map +1 -0
- package/dist/cli/commands/index.js +4 -0
- package/dist/cli/commands/index.js.map +1 -0
- package/dist/cli/commands/query.d.ts +26 -0
- package/dist/cli/commands/query.d.ts.map +1 -0
- package/dist/cli/commands/query.js +90 -0
- package/dist/cli/commands/query.js.map +1 -0
- package/dist/cli/commands/session.d.ts +40 -0
- package/dist/cli/commands/session.d.ts.map +1 -0
- package/dist/cli/commands/session.js +55 -0
- package/dist/cli/commands/session.js.map +1 -0
- package/dist/cli/config-loader.d.ts +63 -0
- package/dist/cli/config-loader.d.ts.map +1 -0
- package/dist/cli/config-loader.js +94 -0
- package/dist/cli/config-loader.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +120 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/output/formatter.d.ts +24 -0
- package/dist/cli/output/formatter.d.ts.map +1 -0
- package/dist/cli/output/formatter.js +196 -0
- package/dist/cli/output/formatter.js.map +1 -0
- package/dist/cli/output/index.d.ts +3 -0
- package/dist/cli/output/index.d.ts.map +1 -0
- package/dist/cli/output/index.js +3 -0
- package/dist/cli/output/index.js.map +1 -0
- package/dist/cli/output/progress.d.ts +60 -0
- package/dist/cli/output/progress.d.ts.map +1 -0
- package/dist/cli/output/progress.js +142 -0
- package/dist/cli/output/progress.js.map +1 -0
- package/dist/domain/errors/base-error.d.ts +17 -0
- package/dist/domain/errors/base-error.d.ts.map +1 -0
- package/dist/domain/errors/base-error.js +29 -0
- package/dist/domain/errors/base-error.js.map +1 -0
- package/dist/domain/errors/execution-error.d.ts +61 -0
- package/dist/domain/errors/execution-error.d.ts.map +1 -0
- package/dist/domain/errors/execution-error.js +86 -0
- package/dist/domain/errors/execution-error.js.map +1 -0
- package/dist/domain/errors/index.d.ts +14 -0
- package/dist/domain/errors/index.d.ts.map +1 -0
- package/dist/domain/errors/index.js +26 -0
- package/dist/domain/errors/index.js.map +1 -0
- package/dist/domain/errors/limit-exceeded.d.ts +38 -0
- package/dist/domain/errors/limit-exceeded.d.ts.map +1 -0
- package/dist/domain/errors/limit-exceeded.js +71 -0
- package/dist/domain/errors/limit-exceeded.js.map +1 -0
- package/dist/domain/errors/llm-error.d.ts +60 -0
- package/dist/domain/errors/llm-error.d.ts.map +1 -0
- package/dist/domain/errors/llm-error.js +92 -0
- package/dist/domain/errors/llm-error.js.map +1 -0
- package/dist/domain/errors/validation-error.d.ts +58 -0
- package/dist/domain/errors/validation-error.d.ts.map +1 -0
- package/dist/domain/errors/validation-error.js +81 -0
- package/dist/domain/errors/validation-error.js.map +1 -0
- package/dist/domain/interfaces/code-executor.d.ts +98 -0
- package/dist/domain/interfaces/code-executor.d.ts.map +1 -0
- package/dist/domain/interfaces/code-executor.js +2 -0
- package/dist/domain/interfaces/code-executor.js.map +1 -0
- package/dist/domain/interfaces/event-emitter.d.ts +137 -0
- package/dist/domain/interfaces/event-emitter.d.ts.map +1 -0
- package/dist/domain/interfaces/event-emitter.js +2 -0
- package/dist/domain/interfaces/event-emitter.js.map +1 -0
- package/dist/domain/interfaces/index.d.ts +5 -0
- package/dist/domain/interfaces/index.d.ts.map +1 -0
- package/dist/domain/interfaces/index.js +2 -0
- package/dist/domain/interfaces/index.js.map +1 -0
- package/dist/domain/interfaces/llm-provider.d.ts +64 -0
- package/dist/domain/interfaces/llm-provider.d.ts.map +1 -0
- package/dist/domain/interfaces/llm-provider.js +2 -0
- package/dist/domain/interfaces/llm-provider.js.map +1 -0
- package/dist/domain/interfaces/session-store.d.ts +65 -0
- package/dist/domain/interfaces/session-store.d.ts.map +1 -0
- package/dist/domain/interfaces/session-store.js +2 -0
- package/dist/domain/interfaces/session-store.js.map +1 -0
- package/dist/domain/services/context-chunker.d.ts +43 -0
- package/dist/domain/services/context-chunker.d.ts.map +1 -0
- package/dist/domain/services/context-chunker.js +152 -0
- package/dist/domain/services/context-chunker.js.map +1 -0
- package/dist/domain/services/cost-calculator.d.ts +63 -0
- package/dist/domain/services/cost-calculator.d.ts.map +1 -0
- package/dist/domain/services/cost-calculator.js +121 -0
- package/dist/domain/services/cost-calculator.js.map +1 -0
- package/dist/domain/services/index.d.ts +5 -0
- package/dist/domain/services/index.d.ts.map +1 -0
- package/dist/domain/services/index.js +9 -0
- package/dist/domain/services/index.js.map +1 -0
- package/dist/domain/services/limit-checker.d.ts +54 -0
- package/dist/domain/services/limit-checker.d.ts.map +1 -0
- package/dist/domain/services/limit-checker.js +146 -0
- package/dist/domain/services/limit-checker.js.map +1 -0
- package/dist/domain/services/result-aggregator.d.ts +49 -0
- package/dist/domain/services/result-aggregator.d.ts.map +1 -0
- package/dist/domain/services/result-aggregator.js +131 -0
- package/dist/domain/services/result-aggregator.js.map +1 -0
- package/dist/domain/types/config.d.ts +41 -0
- package/dist/domain/types/config.d.ts.map +1 -0
- package/dist/domain/types/config.js +22 -0
- package/dist/domain/types/config.js.map +1 -0
- package/dist/domain/types/context.d.ts +64 -0
- package/dist/domain/types/context.d.ts.map +1 -0
- package/dist/domain/types/context.js +20 -0
- package/dist/domain/types/context.js.map +1 -0
- package/dist/domain/types/index.d.ts +5 -0
- package/dist/domain/types/index.d.ts.map +1 -0
- package/dist/domain/types/index.js +9 -0
- package/dist/domain/types/index.js.map +1 -0
- package/dist/domain/types/result.d.ts +137 -0
- package/dist/domain/types/result.d.ts.map +1 -0
- package/dist/domain/types/result.js +27 -0
- package/dist/domain/types/result.js.map +1 -0
- package/dist/domain/types/session.d.ts +74 -0
- package/dist/domain/types/session.d.ts.map +1 -0
- package/dist/domain/types/session.js +31 -0
- package/dist/domain/types/session.js.map +1 -0
- package/dist/factory/create-rlm.d.ts +77 -0
- package/dist/factory/create-rlm.d.ts.map +1 -0
- package/dist/factory/create-rlm.js +82 -0
- package/dist/factory/create-rlm.js.map +1 -0
- package/dist/factory/default-config.d.ts +27 -0
- package/dist/factory/default-config.d.ts.map +1 -0
- package/dist/factory/default-config.js +98 -0
- package/dist/factory/default-config.js.map +1 -0
- package/dist/factory/index.d.ts +3 -0
- package/dist/factory/index.d.ts.map +1 -0
- package/dist/factory/index.js +3 -0
- package/dist/factory/index.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/events/event-types.d.ts +35 -0
- package/dist/infrastructure/events/event-types.d.ts.map +1 -0
- package/dist/infrastructure/events/event-types.js +44 -0
- package/dist/infrastructure/events/event-types.js.map +1 -0
- package/dist/infrastructure/events/index.d.ts +3 -0
- package/dist/infrastructure/events/index.d.ts.map +1 -0
- package/dist/infrastructure/events/index.js +3 -0
- package/dist/infrastructure/events/index.js.map +1 -0
- package/dist/infrastructure/events/node-emitter.d.ts +72 -0
- package/dist/infrastructure/events/node-emitter.d.ts.map +1 -0
- package/dist/infrastructure/events/node-emitter.js +125 -0
- package/dist/infrastructure/events/node-emitter.js.map +1 -0
- package/dist/infrastructure/llm/index.d.ts +3 -0
- package/dist/infrastructure/llm/index.d.ts.map +1 -0
- package/dist/infrastructure/llm/index.js +3 -0
- package/dist/infrastructure/llm/index.js.map +1 -0
- package/dist/infrastructure/llm/openrouter-provider.d.ts +35 -0
- package/dist/infrastructure/llm/openrouter-provider.d.ts.map +1 -0
- package/dist/infrastructure/llm/openrouter-provider.js +158 -0
- package/dist/infrastructure/llm/openrouter-provider.js.map +1 -0
- package/dist/infrastructure/llm/prompts/code-gen-prompt.d.ts +21 -0
- package/dist/infrastructure/llm/prompts/code-gen-prompt.d.ts.map +1 -0
- package/dist/infrastructure/llm/prompts/code-gen-prompt.js +78 -0
- package/dist/infrastructure/llm/prompts/code-gen-prompt.js.map +1 -0
- package/dist/infrastructure/llm/prompts/error-recovery-prompt.d.ts +24 -0
- package/dist/infrastructure/llm/prompts/error-recovery-prompt.d.ts.map +1 -0
- package/dist/infrastructure/llm/prompts/error-recovery-prompt.js +70 -0
- package/dist/infrastructure/llm/prompts/error-recovery-prompt.js.map +1 -0
- package/dist/infrastructure/llm/prompts/index.d.ts +4 -0
- package/dist/infrastructure/llm/prompts/index.d.ts.map +1 -0
- package/dist/infrastructure/llm/prompts/index.js +4 -0
- package/dist/infrastructure/llm/prompts/index.js.map +1 -0
- package/dist/infrastructure/llm/prompts/system-prompt.d.ts +12 -0
- package/dist/infrastructure/llm/prompts/system-prompt.d.ts.map +1 -0
- package/dist/infrastructure/llm/prompts/system-prompt.js +96 -0
- package/dist/infrastructure/llm/prompts/system-prompt.js.map +1 -0
- package/dist/infrastructure/persistence/index.d.ts +3 -0
- package/dist/infrastructure/persistence/index.d.ts.map +1 -0
- package/dist/infrastructure/persistence/index.js +3 -0
- package/dist/infrastructure/persistence/index.js.map +1 -0
- package/dist/infrastructure/persistence/memory-store.d.ts +24 -0
- package/dist/infrastructure/persistence/memory-store.d.ts.map +1 -0
- package/dist/infrastructure/persistence/memory-store.js +71 -0
- package/dist/infrastructure/persistence/memory-store.js.map +1 -0
- package/dist/infrastructure/persistence/schema.d.ts +120 -0
- package/dist/infrastructure/persistence/schema.d.ts.map +1 -0
- package/dist/infrastructure/persistence/schema.js +130 -0
- package/dist/infrastructure/persistence/schema.js.map +1 -0
- package/dist/infrastructure/sandbox/ast-validator.d.ts +23 -0
- package/dist/infrastructure/sandbox/ast-validator.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/ast-validator.js +249 -0
- package/dist/infrastructure/sandbox/ast-validator.js.map +1 -0
- package/dist/infrastructure/sandbox/index.d.ts +4 -0
- package/dist/infrastructure/sandbox/index.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/index.js +4 -0
- package/dist/infrastructure/sandbox/index.js.map +1 -0
- package/dist/infrastructure/sandbox/prelude/rlm_prelude.py +376 -0
- package/dist/infrastructure/sandbox/process-manager.d.ts +98 -0
- package/dist/infrastructure/sandbox/process-manager.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/process-manager.js +186 -0
- package/dist/infrastructure/sandbox/process-manager.js.map +1 -0
- package/dist/infrastructure/sandbox/python-executor.d.ts +29 -0
- package/dist/infrastructure/sandbox/python-executor.d.ts.map +1 -0
- package/dist/infrastructure/sandbox/python-executor.js +182 -0
- package/dist/infrastructure/sandbox/python-executor.js.map +1 -0
- package/package.json +65 -0
- package/src/infrastructure/sandbox/prelude/rlm_prelude.py +376 -0
package/README.md
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# @cloudwarriors-ai/rlm
|
|
2
|
+
|
|
3
|
+
Recursive Language Model - Process massive contexts (10M+ tokens) through recursive LLM decomposition.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
RLM enables processing of contexts far larger than typical LLM context windows by:
|
|
8
|
+
|
|
9
|
+
1. Treating content as Python REPL variables
|
|
10
|
+
2. Having the LLM write code to decompose and process chunks
|
|
11
|
+
3. Recursively calling itself with subsets of context
|
|
12
|
+
4. Aggregating results into a final answer
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# From GitHub Packages (requires auth)
|
|
18
|
+
npm install @cloudwarriors-ai/rlm
|
|
19
|
+
|
|
20
|
+
# Or link locally during development
|
|
21
|
+
npm link /path/to/rlm
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { createRLM } from '@cloudwarriors-ai/rlm';
|
|
28
|
+
import fs from 'node:fs';
|
|
29
|
+
|
|
30
|
+
const rlm = createRLM({
|
|
31
|
+
apiKey: process.env.OPENROUTER_API_KEY,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const result = await rlm.query(
|
|
35
|
+
fs.readFileSync('./massive-codebase.txt', 'utf-8'),
|
|
36
|
+
'Find all security vulnerabilities'
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
console.log(result.answer);
|
|
40
|
+
console.log(`Cost: $${result.usage.costUsd.toFixed(4)}`);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## CLI Usage
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# Install globally
|
|
47
|
+
npm install -g @cloudwarriors-ai/rlm
|
|
48
|
+
|
|
49
|
+
# Initialize config
|
|
50
|
+
rlm config init
|
|
51
|
+
rlm config set apiKey YOUR_OPENROUTER_API_KEY
|
|
52
|
+
|
|
53
|
+
# Run a query
|
|
54
|
+
rlm query --file ./large-file.txt --query "Summarize this document"
|
|
55
|
+
|
|
56
|
+
# With options
|
|
57
|
+
rlm query \
|
|
58
|
+
--file ./codebase.txt \
|
|
59
|
+
--query "Find security issues" \
|
|
60
|
+
--max-depth 5 \
|
|
61
|
+
--max-cost 5.00 \
|
|
62
|
+
--verbose
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
### Environment Variables
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
OPENROUTER_API_KEY=sk-or-... # Required
|
|
71
|
+
RLM_MODEL=anthropic/claude-sonnet-4 # Default model
|
|
72
|
+
RLM_MAX_DEPTH=5 # Max recursion depth
|
|
73
|
+
RLM_MAX_COST_USD=10 # Max cost per session
|
|
74
|
+
RLM_MAX_TOKENS=1000000 # Max total tokens
|
|
75
|
+
RLM_TIMEOUT_SECONDS=300 # Session timeout
|
|
76
|
+
RLM_SANDBOX_MODE=strict # strict|permissive|disabled
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Config File
|
|
80
|
+
|
|
81
|
+
Create `~/.rlm/config.json`:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"apiKey": "sk-or-...",
|
|
86
|
+
"model": "anthropic/claude-sonnet-4",
|
|
87
|
+
"maxRecursionDepth": 5,
|
|
88
|
+
"maxCostUsd": 10,
|
|
89
|
+
"maxTokens": 1000000,
|
|
90
|
+
"timeoutMs": 300000
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## API Reference
|
|
95
|
+
|
|
96
|
+
### createRLM(options)
|
|
97
|
+
|
|
98
|
+
Create an RLM service instance.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const rlm = createRLM({
|
|
102
|
+
apiKey: 'sk-or-...',
|
|
103
|
+
model: 'anthropic/claude-sonnet-4',
|
|
104
|
+
config: {
|
|
105
|
+
maxRecursionDepth: 5,
|
|
106
|
+
maxCostUsd: 10,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### rlm.query(context, query, config?)
|
|
112
|
+
|
|
113
|
+
Execute an RLM query.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
const result = await rlm.query(
|
|
117
|
+
context, // string or ContextInput
|
|
118
|
+
'Your question',
|
|
119
|
+
{ maxRecursionDepth: 3 } // optional overrides
|
|
120
|
+
);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Returns `RLMResult`:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
interface RLMResult {
|
|
127
|
+
sessionId: string;
|
|
128
|
+
answer: string;
|
|
129
|
+
success: boolean;
|
|
130
|
+
error?: string;
|
|
131
|
+
trace: ExecutionTrace;
|
|
132
|
+
usage: ResourceUsage;
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Architecture
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
140
|
+
│ CLI Layer │
|
|
141
|
+
│ (src/cli/) │
|
|
142
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
143
|
+
│
|
|
144
|
+
▼
|
|
145
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
146
|
+
│ Application Layer │
|
|
147
|
+
│ (src/application/) │
|
|
148
|
+
│ RLMService - orchestrates use cases │
|
|
149
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
150
|
+
│
|
|
151
|
+
▼
|
|
152
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
153
|
+
│ Domain Layer │
|
|
154
|
+
│ (src/domain/) │
|
|
155
|
+
│ Pure business logic, types, interfaces │
|
|
156
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
157
|
+
│
|
|
158
|
+
▼
|
|
159
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
160
|
+
│ Infrastructure Layer │
|
|
161
|
+
│ (src/infrastructure/) │
|
|
162
|
+
│ LLM providers, Python executor, persistence │
|
|
163
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Integration with Hermes
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
// In Hermes project
|
|
170
|
+
import { createRLM } from '@cloudwarriors-ai/rlm';
|
|
171
|
+
import { db } from './db';
|
|
172
|
+
import { eventBus } from './events';
|
|
173
|
+
|
|
174
|
+
const rlm = createRLM({
|
|
175
|
+
apiKey: process.env.OPENROUTER_API_KEY,
|
|
176
|
+
sessionStore: new DrizzleSessionStore(db),
|
|
177
|
+
eventBus,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Use in API routes
|
|
181
|
+
app.post('/api/rlm/query', async (req, res) => {
|
|
182
|
+
const result = await rlm.query(req.body.context, req.body.query);
|
|
183
|
+
res.json(result);
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Development
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
# Install dependencies
|
|
191
|
+
npm install
|
|
192
|
+
|
|
193
|
+
# Build
|
|
194
|
+
npm run build
|
|
195
|
+
|
|
196
|
+
# Run tests
|
|
197
|
+
npm test
|
|
198
|
+
|
|
199
|
+
# Type check
|
|
200
|
+
npm run lint
|
|
201
|
+
|
|
202
|
+
# Watch mode
|
|
203
|
+
npm run dev
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
208
|
+
UNLICENSED - Private package for CloudWarriors AI
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/application/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/application/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA0B,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAoB,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { RLMConfig } from '../domain/types/config.js';
|
|
2
|
+
import type { SessionState, RLMSession } from '../domain/types/session.js';
|
|
3
|
+
import type { ExecutionStep } from '../domain/types/result.js';
|
|
4
|
+
import type { ILLMProvider } from '../domain/interfaces/llm-provider.js';
|
|
5
|
+
import type { ICodeExecutor } from '../domain/interfaces/code-executor.js';
|
|
6
|
+
import type { IEventEmitter } from '../domain/interfaces/event-emitter.js';
|
|
7
|
+
/**
|
|
8
|
+
* Result of executing a query
|
|
9
|
+
*/
|
|
10
|
+
export interface QueryResult {
|
|
11
|
+
/** The final answer */
|
|
12
|
+
answer: string;
|
|
13
|
+
/** Whether execution succeeded */
|
|
14
|
+
success: boolean;
|
|
15
|
+
/** Error message if failed */
|
|
16
|
+
error?: string;
|
|
17
|
+
/** Execution steps performed */
|
|
18
|
+
steps: ExecutionStep[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Handles execution of RLM queries
|
|
22
|
+
*/
|
|
23
|
+
export declare class QueryHandler {
|
|
24
|
+
private readonly llmProvider;
|
|
25
|
+
private readonly codeExecutor;
|
|
26
|
+
private readonly eventEmitter;
|
|
27
|
+
constructor(llmProvider: ILLMProvider, codeExecutor: ICodeExecutor, eventEmitter: IEventEmitter);
|
|
28
|
+
/**
|
|
29
|
+
* Execute a query at the current recursion depth
|
|
30
|
+
*/
|
|
31
|
+
execute(session: RLMSession, state: SessionState, config: RLMConfig): Promise<QueryResult>;
|
|
32
|
+
/**
|
|
33
|
+
* Generate Python code for the query
|
|
34
|
+
*/
|
|
35
|
+
private generateCode;
|
|
36
|
+
/**
|
|
37
|
+
* Execute Python code and handle commands
|
|
38
|
+
*/
|
|
39
|
+
private executeCode;
|
|
40
|
+
/**
|
|
41
|
+
* Handle llm_query command from Python
|
|
42
|
+
*/
|
|
43
|
+
private handleLLMQueryCommand;
|
|
44
|
+
/**
|
|
45
|
+
* Attempt to recover from an error by asking the LLM to fix the code
|
|
46
|
+
*/
|
|
47
|
+
private recoverFromError;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=query-handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-handler.d.ts","sourceRoot":"","sources":["../../src/application/query-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAC3E,OAAO,KAAK,EAAE,aAAa,EAAuB,MAAM,2BAA2B,CAAC;AACpF,OAAO,KAAK,EAAE,YAAY,EAAW,MAAM,sCAAsC,CAAC;AAClF,OAAO,KAAK,EAAE,aAAa,EAAmB,MAAM,uCAAuC,CAAC;AAC5F,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AAgB3E;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,uBAAuB;IACvB,MAAM,EAAE,MAAM,CAAC;IAEf,kCAAkC;IAClC,OAAO,EAAE,OAAO,CAAC;IAEjB,8BAA8B;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,gCAAgC;IAChC,KAAK,EAAE,aAAa,EAAE,CAAC;CACxB;AAED;;GAEG;AACH,qBAAa,YAAY;IAErB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,YAAY;gBAFZ,WAAW,EAAE,YAAY,EACzB,YAAY,EAAE,aAAa,EAC3B,YAAY,EAAE,aAAa;IAG9C;;OAEG;IACG,OAAO,CACX,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,SAAS,GAChB,OAAO,CAAC,WAAW,CAAC;IA2CvB;;OAEG;YACW,YAAY;IAyF1B;;OAEG;YACW,WAAW;IAkIzB;;OAEG;YACW,qBAAqB;IAyFnC;;OAEG;YACW,gBAAgB;CAyC/B"}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { checkLimits, canRecurse, } from '../domain/services/limit-checker.js';
|
|
2
|
+
import { formatSystemPrompt, extractPythonCode, formatErrorRecoveryPrompt, MAX_RECOVERY_ATTEMPTS, formatRecursiveQueryPrompt, } from '../infrastructure/llm/prompts/index.js';
|
|
3
|
+
import { LimitExceededError, SessionCancelledError } from '../domain/errors/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Handles execution of RLM queries
|
|
6
|
+
*/
|
|
7
|
+
export class QueryHandler {
|
|
8
|
+
llmProvider;
|
|
9
|
+
codeExecutor;
|
|
10
|
+
eventEmitter;
|
|
11
|
+
constructor(llmProvider, codeExecutor, eventEmitter) {
|
|
12
|
+
this.llmProvider = llmProvider;
|
|
13
|
+
this.codeExecutor = codeExecutor;
|
|
14
|
+
this.eventEmitter = eventEmitter;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Execute a query at the current recursion depth
|
|
18
|
+
*/
|
|
19
|
+
async execute(session, state, config) {
|
|
20
|
+
const steps = [];
|
|
21
|
+
// Check limits before starting
|
|
22
|
+
const limitCheck = checkLimits(state, config);
|
|
23
|
+
if (!limitCheck.withinLimits) {
|
|
24
|
+
const exceeded = limitCheck.exceededLimits[0];
|
|
25
|
+
throw new LimitExceededError(exceeded.type, exceeded.current, exceeded.max, session.id);
|
|
26
|
+
}
|
|
27
|
+
// Check for cancellation
|
|
28
|
+
if (state.cancellationRequested) {
|
|
29
|
+
throw new SessionCancelledError(session.id);
|
|
30
|
+
}
|
|
31
|
+
// Generate code to process the query
|
|
32
|
+
const codeResult = await this.generateCode(session, state, config, steps);
|
|
33
|
+
if (!codeResult.success) {
|
|
34
|
+
return {
|
|
35
|
+
answer: '',
|
|
36
|
+
success: false,
|
|
37
|
+
error: codeResult.error,
|
|
38
|
+
steps,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
// Execute the generated code
|
|
42
|
+
const execResult = await this.executeCode(codeResult.code, session, state, config, steps);
|
|
43
|
+
return execResult;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Generate Python code for the query
|
|
47
|
+
*/
|
|
48
|
+
async generateCode(session, state, config, steps) {
|
|
49
|
+
const startedAt = new Date();
|
|
50
|
+
// Build context info for the prompt
|
|
51
|
+
const contextVars = Array.from(state.pythonVariables.entries()).map(([name, value]) => ({
|
|
52
|
+
name,
|
|
53
|
+
sizeHint: `${value.length} chars, ~${this.llmProvider.estimateTokens(value)} tokens`,
|
|
54
|
+
}));
|
|
55
|
+
const systemPrompt = formatSystemPrompt(contextVars, session.query);
|
|
56
|
+
const messages = [
|
|
57
|
+
{ role: 'user', content: 'Generate Python code to accomplish the task.' },
|
|
58
|
+
];
|
|
59
|
+
this.eventEmitter.emit({
|
|
60
|
+
type: 'rlm.llm.completed',
|
|
61
|
+
sessionId: session.id,
|
|
62
|
+
timestamp: new Date(),
|
|
63
|
+
model: config.model,
|
|
64
|
+
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
|
65
|
+
cost: 0,
|
|
66
|
+
depth: state.currentDepth,
|
|
67
|
+
});
|
|
68
|
+
const response = await this.llmProvider.generate(messages, {
|
|
69
|
+
systemPrompt,
|
|
70
|
+
temperature: config.temperature,
|
|
71
|
+
maxTokens: 4000,
|
|
72
|
+
});
|
|
73
|
+
// Update state with token usage
|
|
74
|
+
state.totalTokens += response.usage.totalTokens;
|
|
75
|
+
state.totalCostUsd += response.cost;
|
|
76
|
+
state.llmCalls += 1;
|
|
77
|
+
const completedAt = new Date();
|
|
78
|
+
const durationMs = completedAt.getTime() - startedAt.getTime();
|
|
79
|
+
steps.push({
|
|
80
|
+
index: steps.length,
|
|
81
|
+
depth: state.currentDepth,
|
|
82
|
+
type: 'llm_call',
|
|
83
|
+
startedAt,
|
|
84
|
+
completedAt,
|
|
85
|
+
durationMs,
|
|
86
|
+
details: {
|
|
87
|
+
type: 'llm_call',
|
|
88
|
+
model: response.model,
|
|
89
|
+
promptTokens: response.usage.promptTokens,
|
|
90
|
+
completionTokens: response.usage.completionTokens,
|
|
91
|
+
cost: response.cost,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
// Extract Python code from response
|
|
95
|
+
const code = extractPythonCode(response.content);
|
|
96
|
+
if (!code) {
|
|
97
|
+
return {
|
|
98
|
+
success: false,
|
|
99
|
+
code: '',
|
|
100
|
+
error: 'Failed to extract Python code from LLM response',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
steps.push({
|
|
104
|
+
index: steps.length,
|
|
105
|
+
depth: state.currentDepth,
|
|
106
|
+
type: 'code_generation',
|
|
107
|
+
startedAt,
|
|
108
|
+
completedAt,
|
|
109
|
+
durationMs: 0,
|
|
110
|
+
details: {
|
|
111
|
+
type: 'code_generation',
|
|
112
|
+
codeLength: code.length,
|
|
113
|
+
codePreview: code.slice(0, 200) + (code.length > 200 ? '...' : ''),
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
return { success: true, code };
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Execute Python code and handle commands
|
|
120
|
+
*/
|
|
121
|
+
async executeCode(code, session, state, config, steps) {
|
|
122
|
+
const startedAt = new Date();
|
|
123
|
+
let currentCode = code;
|
|
124
|
+
let recoveryAttempts = 0;
|
|
125
|
+
while (recoveryAttempts <= MAX_RECOVERY_ATTEMPTS) {
|
|
126
|
+
// Validate code
|
|
127
|
+
const validation = this.codeExecutor.validate(currentCode);
|
|
128
|
+
if (!validation.valid) {
|
|
129
|
+
if (recoveryAttempts >= MAX_RECOVERY_ATTEMPTS) {
|
|
130
|
+
return {
|
|
131
|
+
answer: '',
|
|
132
|
+
success: false,
|
|
133
|
+
error: `Code validation failed: ${validation.errors.map((e) => e.message).join(', ')}`,
|
|
134
|
+
steps,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// Try to recover
|
|
138
|
+
currentCode = await this.recoverFromError(currentCode, `Validation errors: ${validation.errors.map((e) => e.message).join(', ')}`, undefined, session, state, config, steps);
|
|
139
|
+
recoveryAttempts++;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Execute code
|
|
143
|
+
const execContext = {
|
|
144
|
+
variables: state.pythonVariables,
|
|
145
|
+
sessionId: session.id,
|
|
146
|
+
depth: state.currentDepth,
|
|
147
|
+
timeoutMs: Math.min(config.timeoutMs, 60000), // Max 60s per execution
|
|
148
|
+
};
|
|
149
|
+
const result = await this.codeExecutor.execute(currentCode, execContext);
|
|
150
|
+
const completedAt = new Date();
|
|
151
|
+
const durationMs = completedAt.getTime() - startedAt.getTime();
|
|
152
|
+
steps.push({
|
|
153
|
+
index: steps.length,
|
|
154
|
+
depth: state.currentDepth,
|
|
155
|
+
type: 'code_execution',
|
|
156
|
+
startedAt,
|
|
157
|
+
completedAt,
|
|
158
|
+
durationMs,
|
|
159
|
+
details: {
|
|
160
|
+
type: 'code_execution',
|
|
161
|
+
success: result.success,
|
|
162
|
+
output: result.stdout.slice(0, 500),
|
|
163
|
+
error: result.error,
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
if (!result.success) {
|
|
167
|
+
if (recoveryAttempts >= MAX_RECOVERY_ATTEMPTS) {
|
|
168
|
+
return {
|
|
169
|
+
answer: '',
|
|
170
|
+
success: false,
|
|
171
|
+
error: result.error ?? 'Code execution failed',
|
|
172
|
+
steps,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
// Try to recover
|
|
176
|
+
currentCode = await this.recoverFromError(currentCode, result.error ?? 'Unknown error', result.stderr, session, state, config, steps);
|
|
177
|
+
recoveryAttempts++;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
// Process commands from Python
|
|
181
|
+
for (const command of result.commands) {
|
|
182
|
+
if (command.type === 'llm_query') {
|
|
183
|
+
await this.handleLLMQueryCommand(command, session, state, config, steps);
|
|
184
|
+
}
|
|
185
|
+
else if (command.type === 'set_variable') {
|
|
186
|
+
state.pythonVariables.set(command.name, command.value);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// Check if we got a result
|
|
190
|
+
if (result.result) {
|
|
191
|
+
return {
|
|
192
|
+
answer: result.result,
|
|
193
|
+
success: true,
|
|
194
|
+
steps,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
// No result set, but execution succeeded
|
|
198
|
+
return {
|
|
199
|
+
answer: result.stdout || 'No result set',
|
|
200
|
+
success: true,
|
|
201
|
+
steps,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
answer: '',
|
|
206
|
+
success: false,
|
|
207
|
+
error: 'Max recovery attempts exceeded',
|
|
208
|
+
steps,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Handle llm_query command from Python
|
|
213
|
+
*/
|
|
214
|
+
async handleLLMQueryCommand(command, session, state, config, steps) {
|
|
215
|
+
// Check if we can recurse
|
|
216
|
+
if (!canRecurse(state, config)) {
|
|
217
|
+
// Send error response to Python
|
|
218
|
+
await this.codeExecutor.respondToQuery(JSON.stringify({ error: 'Maximum recursion depth reached' }));
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// Increment depth for recursive call
|
|
222
|
+
state.currentDepth += 1;
|
|
223
|
+
state.recursiveCalls += 1;
|
|
224
|
+
state.maxDepthReached = Math.max(state.maxDepthReached, state.currentDepth);
|
|
225
|
+
const startedAt = new Date();
|
|
226
|
+
this.eventEmitter.emit({
|
|
227
|
+
type: 'rlm.recursive.started',
|
|
228
|
+
sessionId: session.id,
|
|
229
|
+
timestamp: startedAt,
|
|
230
|
+
query: command.query,
|
|
231
|
+
depth: state.currentDepth,
|
|
232
|
+
});
|
|
233
|
+
// Build context for the recursive query
|
|
234
|
+
const contextContent = command.contextVariables.join('\n\n');
|
|
235
|
+
const prompt = formatRecursiveQueryPrompt(contextContent, command.query);
|
|
236
|
+
const messages = [{ role: 'user', content: prompt }];
|
|
237
|
+
try {
|
|
238
|
+
const response = await this.llmProvider.generate(messages, {
|
|
239
|
+
temperature: config.temperature,
|
|
240
|
+
maxTokens: 4000,
|
|
241
|
+
});
|
|
242
|
+
// Update state
|
|
243
|
+
state.totalTokens += response.usage.totalTokens;
|
|
244
|
+
state.totalCostUsd += response.cost;
|
|
245
|
+
state.llmCalls += 1;
|
|
246
|
+
const completedAt = new Date();
|
|
247
|
+
const durationMs = completedAt.getTime() - startedAt.getTime();
|
|
248
|
+
steps.push({
|
|
249
|
+
index: steps.length,
|
|
250
|
+
depth: state.currentDepth,
|
|
251
|
+
type: 'recursive_call',
|
|
252
|
+
startedAt,
|
|
253
|
+
completedAt,
|
|
254
|
+
durationMs,
|
|
255
|
+
details: {
|
|
256
|
+
type: 'recursive_call',
|
|
257
|
+
query: command.query.slice(0, 100),
|
|
258
|
+
contextSize: contextContent.length,
|
|
259
|
+
depth: state.currentDepth,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
this.eventEmitter.emit({
|
|
263
|
+
type: 'rlm.recursive.completed',
|
|
264
|
+
sessionId: session.id,
|
|
265
|
+
timestamp: completedAt,
|
|
266
|
+
depth: state.currentDepth,
|
|
267
|
+
resultLength: response.content.length,
|
|
268
|
+
});
|
|
269
|
+
// Send response back to Python
|
|
270
|
+
await this.codeExecutor.respondToQuery(JSON.stringify({ result: response.content }));
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
274
|
+
await this.codeExecutor.respondToQuery(JSON.stringify({ error: errorMessage }));
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
// Decrement depth after recursive call
|
|
278
|
+
state.currentDepth -= 1;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Attempt to recover from an error by asking the LLM to fix the code
|
|
283
|
+
*/
|
|
284
|
+
async recoverFromError(previousCode, error, traceback, session, state, config, steps) {
|
|
285
|
+
const startedAt = new Date();
|
|
286
|
+
this.eventEmitter.emit({
|
|
287
|
+
type: 'rlm.recovery.started',
|
|
288
|
+
sessionId: session.id,
|
|
289
|
+
timestamp: startedAt,
|
|
290
|
+
});
|
|
291
|
+
const prompt = formatErrorRecoveryPrompt(previousCode, error, traceback);
|
|
292
|
+
const messages = [{ role: 'user', content: prompt }];
|
|
293
|
+
const response = await this.llmProvider.generate(messages, {
|
|
294
|
+
temperature: 0.2, // Lower temperature for more focused fixes
|
|
295
|
+
maxTokens: 4000,
|
|
296
|
+
});
|
|
297
|
+
// Update state
|
|
298
|
+
state.totalTokens += response.usage.totalTokens;
|
|
299
|
+
state.totalCostUsd += response.cost;
|
|
300
|
+
state.llmCalls += 1;
|
|
301
|
+
const completedAt = new Date();
|
|
302
|
+
this.eventEmitter.emit({
|
|
303
|
+
type: 'rlm.recovery.completed',
|
|
304
|
+
sessionId: session.id,
|
|
305
|
+
timestamp: completedAt,
|
|
306
|
+
});
|
|
307
|
+
const fixedCode = extractPythonCode(response.content);
|
|
308
|
+
return fixedCode ?? previousCode;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
//# sourceMappingURL=query-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-handler.js","sourceRoot":"","sources":["../../src/application/query-handler.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,WAAW,EAEX,UAAU,GACX,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,yBAAyB,EACzB,qBAAqB,EACrB,0BAA0B,GAC3B,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAmBtF;;GAEG;AACH,MAAM,OAAO,YAAY;IAEJ;IACA;IACA;IAHnB,YACmB,WAAyB,EACzB,YAA2B,EAC3B,YAA2B;QAF3B,gBAAW,GAAX,WAAW,CAAc;QACzB,iBAAY,GAAZ,YAAY,CAAe;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nB,KAAmB,EACnB,MAAiB,EACjB,KAAsB;QAEtB,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;QAE7B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YACrB,IAAI,EAAE,sBAA+B;YACrC,SAAS,EAAE,OAAO,CAAC,EAAE;YACrB,SAAS,EAAE,SAAS;SACd,CAAC,CAAC;QAEV,MAAM,MAAM,GAAG,yBAAyB,CAAC,YAAY,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;QACzE,MAAM,QAAQ,GAAc,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;QAEhE,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACzD,WAAW,EAAE,GAAG,EAAE,2CAA2C;YAC7D,SAAS,EAAE,IAAI;SAChB,CAAC,CAAC;QAEH,eAAe;QACf,KAAK,CAAC,WAAW,IAAI,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC;QAChD,KAAK,CAAC,YAAY,IAAI,QAAQ,CAAC,IAAI,CAAC;QACpC,KAAK,CAAC,QAAQ,IAAI,CAAC,CAAC;QAEpB,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC;QAE/B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YACrB,IAAI,EAAE,wBAAiC;YACvC,SAAS,EAAE,OAAO,CAAC,EAAE;YACrB,SAAS,EAAE,WAAW;SAChB,CAAC,CAAC;QAEV,MAAM,SAAS,GAAG,iBAAiB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACtD,OAAO,SAAS,IAAI,YAAY,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { RLMConfig, RLMConfigInput } from '../domain/types/config.js';
|
|
2
|
+
import type { ContextInput } from '../domain/types/context.js';
|
|
3
|
+
import type { RLMResult } from '../domain/types/result.js';
|
|
4
|
+
import type { RLMSession } from '../domain/types/session.js';
|
|
5
|
+
import type { ILLMProvider } from '../domain/interfaces/llm-provider.js';
|
|
6
|
+
import type { ICodeExecutor } from '../domain/interfaces/code-executor.js';
|
|
7
|
+
import type { ISessionStore, SessionFilter } from '../domain/interfaces/session-store.js';
|
|
8
|
+
import type { IEventEmitter } from '../domain/interfaces/event-emitter.js';
|
|
9
|
+
/**
|
|
10
|
+
* Options for creating an RLM service
|
|
11
|
+
*/
|
|
12
|
+
export interface RLMServiceOptions {
|
|
13
|
+
/** LLM provider for generating responses */
|
|
14
|
+
readonly llmProvider: ILLMProvider;
|
|
15
|
+
/** Code executor for running Python */
|
|
16
|
+
readonly codeExecutor: ICodeExecutor;
|
|
17
|
+
/** Session store for persistence */
|
|
18
|
+
readonly sessionStore: ISessionStore;
|
|
19
|
+
/** Event emitter for notifications */
|
|
20
|
+
readonly eventEmitter: IEventEmitter;
|
|
21
|
+
/** Default configuration */
|
|
22
|
+
readonly config?: RLMConfigInput;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Main RLM service for processing queries
|
|
26
|
+
*/
|
|
27
|
+
export declare class RLMService {
|
|
28
|
+
private readonly llmProvider;
|
|
29
|
+
private readonly codeExecutor;
|
|
30
|
+
private readonly sessionManager;
|
|
31
|
+
private readonly queryHandler;
|
|
32
|
+
private readonly defaultConfig;
|
|
33
|
+
constructor(options: RLMServiceOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Execute an RLM query
|
|
36
|
+
*
|
|
37
|
+
* @param context - The context to process (string or ContextInput)
|
|
38
|
+
* @param query - The query/task to perform
|
|
39
|
+
* @param config - Optional config overrides
|
|
40
|
+
* @returns The query result
|
|
41
|
+
*/
|
|
42
|
+
query(context: string | ContextInput | readonly ContextInput[], query: string, config?: RLMConfigInput): Promise<RLMResult>;
|
|
43
|
+
/**
|
|
44
|
+
* Cancel a running session
|
|
45
|
+
*/
|
|
46
|
+
cancelSession(sessionId: string): Promise<RLMSession | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Get a session by ID
|
|
49
|
+
*/
|
|
50
|
+
getSession(sessionId: string): Promise<RLMSession | null>;
|
|
51
|
+
/**
|
|
52
|
+
* List sessions
|
|
53
|
+
*/
|
|
54
|
+
listSessions(filter?: SessionFilter): Promise<readonly RLMSession[]>;
|
|
55
|
+
/**
|
|
56
|
+
* Check if the LLM provider is healthy
|
|
57
|
+
*/
|
|
58
|
+
healthCheck(): Promise<boolean>;
|
|
59
|
+
/**
|
|
60
|
+
* Get the current configuration
|
|
61
|
+
*/
|
|
62
|
+
getConfig(): RLMConfig;
|
|
63
|
+
/**
|
|
64
|
+
* Normalize context input to ContextVariables
|
|
65
|
+
*/
|
|
66
|
+
private normalizeContext;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=rlm-service.d.ts.map
|