confused-ai-core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FEATURES.md +169 -0
- package/package.json +119 -0
- package/src/agent.ts +187 -0
- package/src/agentic/index.ts +87 -0
- package/src/agentic/runner.ts +386 -0
- package/src/agentic/types.ts +91 -0
- package/src/artifacts/artifact.ts +417 -0
- package/src/artifacts/index.ts +42 -0
- package/src/artifacts/media.ts +304 -0
- package/src/cli/index.ts +122 -0
- package/src/core/base-agent.ts +151 -0
- package/src/core/context-builder.ts +106 -0
- package/src/core/index.ts +8 -0
- package/src/core/schemas.ts +17 -0
- package/src/core/types.ts +158 -0
- package/src/create-agent.ts +309 -0
- package/src/debug-logger.ts +188 -0
- package/src/dx/agent.ts +88 -0
- package/src/dx/define-agent.ts +183 -0
- package/src/dx/dev-logger.ts +57 -0
- package/src/dx/index.ts +11 -0
- package/src/errors.ts +175 -0
- package/src/execution/engine.ts +522 -0
- package/src/execution/graph-builder.ts +362 -0
- package/src/execution/index.ts +8 -0
- package/src/execution/types.ts +257 -0
- package/src/execution/worker-pool.ts +308 -0
- package/src/extensions/index.ts +123 -0
- package/src/guardrails/allowlist.ts +155 -0
- package/src/guardrails/index.ts +17 -0
- package/src/guardrails/types.ts +159 -0
- package/src/guardrails/validator.ts +265 -0
- package/src/index.ts +74 -0
- package/src/knowledge/index.ts +5 -0
- package/src/knowledge/types.ts +52 -0
- package/src/learning/in-memory-store.ts +72 -0
- package/src/learning/index.ts +6 -0
- package/src/learning/types.ts +42 -0
- package/src/llm/cache.ts +300 -0
- package/src/llm/index.ts +22 -0
- package/src/llm/model-resolver.ts +81 -0
- package/src/llm/openai-provider.ts +313 -0
- package/src/llm/openrouter-provider.ts +29 -0
- package/src/llm/types.ts +131 -0
- package/src/memory/in-memory-store.ts +255 -0
- package/src/memory/index.ts +7 -0
- package/src/memory/types.ts +193 -0
- package/src/memory/vector-store.ts +251 -0
- package/src/observability/console-logger.ts +123 -0
- package/src/observability/index.ts +12 -0
- package/src/observability/metrics.ts +85 -0
- package/src/observability/otlp-exporter.ts +417 -0
- package/src/observability/tracer.ts +105 -0
- package/src/observability/types.ts +341 -0
- package/src/orchestration/agent-adapter.ts +33 -0
- package/src/orchestration/index.ts +34 -0
- package/src/orchestration/load-balancer.ts +151 -0
- package/src/orchestration/mcp-types.ts +59 -0
- package/src/orchestration/message-bus.ts +192 -0
- package/src/orchestration/orchestrator.ts +349 -0
- package/src/orchestration/pipeline.ts +66 -0
- package/src/orchestration/supervisor.ts +107 -0
- package/src/orchestration/swarm.ts +1099 -0
- package/src/orchestration/toolkit.ts +47 -0
- package/src/orchestration/types.ts +339 -0
- package/src/planner/classical-planner.ts +383 -0
- package/src/planner/index.ts +8 -0
- package/src/planner/llm-planner.ts +353 -0
- package/src/planner/types.ts +227 -0
- package/src/planner/validator.ts +297 -0
- package/src/production/circuit-breaker.ts +290 -0
- package/src/production/graceful-shutdown.ts +251 -0
- package/src/production/health.ts +333 -0
- package/src/production/index.ts +57 -0
- package/src/production/latency-eval.ts +62 -0
- package/src/production/rate-limiter.ts +287 -0
- package/src/production/resumable-stream.ts +289 -0
- package/src/production/types.ts +81 -0
- package/src/sdk/index.ts +374 -0
- package/src/session/db-driver.ts +50 -0
- package/src/session/in-memory-store.ts +235 -0
- package/src/session/index.ts +12 -0
- package/src/session/sql-store.ts +315 -0
- package/src/session/sqlite-store.ts +61 -0
- package/src/session/types.ts +153 -0
- package/src/tools/base-tool.ts +223 -0
- package/src/tools/browser-tool.ts +123 -0
- package/src/tools/calculator-tool.ts +265 -0
- package/src/tools/file-tools.ts +394 -0
- package/src/tools/github-tool.ts +432 -0
- package/src/tools/hackernews-tool.ts +187 -0
- package/src/tools/http-tool.ts +118 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/jira-tool.ts +373 -0
- package/src/tools/notion-tool.ts +322 -0
- package/src/tools/openai-tool.ts +236 -0
- package/src/tools/registry.ts +131 -0
- package/src/tools/serpapi-tool.ts +234 -0
- package/src/tools/shell-tool.ts +118 -0
- package/src/tools/slack-tool.ts +327 -0
- package/src/tools/telegram-tool.ts +127 -0
- package/src/tools/types.ts +229 -0
- package/src/tools/websearch-tool.ts +335 -0
- package/src/tools/wikipedia-tool.ts +177 -0
- package/src/tools/yfinance-tool.ts +33 -0
- package/src/voice/index.ts +17 -0
- package/src/voice/voice-provider.ts +228 -0
- package/tests/artifact.test.ts +241 -0
- package/tests/circuit-breaker.test.ts +171 -0
- package/tests/health.test.ts +192 -0
- package/tests/llm-cache.test.ts +186 -0
- package/tests/rate-limiter.test.ts +161 -0
- package/tsconfig.json +29 -0
- package/vitest.config.ts +47 -0
package/FEATURES.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# Core Framework Features
|
|
2
|
+
|
|
3
|
+
| Category | Features |
|
|
4
|
+
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
5
|
+
| **Learning** | User profiles, persistent memory stores, RAG (Retrieval-Augmented Generation), always/agentic learning modes. |
|
|
6
|
+
| **Core** | Model-agnostic (OpenAI/OpenRouter/Ollama via strings), Type-safe I/O with Zod, async-first, long-running operations with retries, multimodal messages. |
|
|
7
|
+
| **Knowledge** | RAGEngine, hybrid search, reranking, session/state persistence, pluggable vector stores. |
|
|
8
|
+
| **Orchestration** | Human-in-the-loop hooks, guardrails (sensitive data/schema validation), MCP & A2A support, supervisors & sub-agents. |
|
|
9
|
+
| **Production** | Circuit breaker, rate limiter, health checks, graceful shutdown, OTLP export, LLM caching, resumable streaming. |
|
|
10
|
+
| **Artifacts** | Typed artifacts (text, data, reasoning, plan), versioned storage, media support (images, audio, video). |
|
|
11
|
+
| **Voice** | TTS/STT with OpenAI and ElevenLabs, voice ID selection, streaming audio. |
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Quick Start Examples
|
|
16
|
+
|
|
17
|
+
### 🔧 Circuit Breaker
|
|
18
|
+
```typescript
|
|
19
|
+
import { createLLMCircuitBreaker } from '@confused-ai/core/production';
|
|
20
|
+
|
|
21
|
+
const breaker = createLLMCircuitBreaker('openai');
|
|
22
|
+
|
|
23
|
+
// Wrap LLM calls - automatically opens circuit on repeated failures
|
|
24
|
+
const result = await breaker.execute(async () => {
|
|
25
|
+
return await llm.generateText({ messages });
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (result.success) {
|
|
29
|
+
console.log(result.value);
|
|
30
|
+
} else {
|
|
31
|
+
console.error('Circuit open:', result.error);
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### ⏱️ Rate Limiter
|
|
36
|
+
```typescript
|
|
37
|
+
import { createOpenAIRateLimiter } from '@confused-ai/core/production';
|
|
38
|
+
|
|
39
|
+
// Create limiter matching OpenAI tier limits
|
|
40
|
+
const limiter = createOpenAIRateLimiter('tier1'); // 60 RPM
|
|
41
|
+
|
|
42
|
+
await limiter.execute(async () => {
|
|
43
|
+
return await openai.chat.completions.create({ ... });
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 🏥 Health Checks
|
|
48
|
+
```typescript
|
|
49
|
+
import { HealthCheckManager, createLLMHealthCheck } from '@confused-ai/core/production';
|
|
50
|
+
|
|
51
|
+
const health = new HealthCheckManager({ version: '1.0.0' });
|
|
52
|
+
health.addComponent(createLLMHealthCheck(llmProvider));
|
|
53
|
+
|
|
54
|
+
// Express endpoint
|
|
55
|
+
app.get('/health', async (req, res) => {
|
|
56
|
+
const result = await health.check();
|
|
57
|
+
res.status(result.status === 'HEALTHY' ? 200 : 503).json(result);
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 💾 LLM Response Cache
|
|
62
|
+
```typescript
|
|
63
|
+
import { LLMCache, withCache } from '@confused-ai/core/llm';
|
|
64
|
+
|
|
65
|
+
const cache = new LLMCache({ maxEntries: 1000, ttlMs: 60000 });
|
|
66
|
+
const cachedLLM = withCache(llm, cache);
|
|
67
|
+
|
|
68
|
+
// Identical requests return cached responses
|
|
69
|
+
const response = await cachedLLM.generateText({ messages });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 📦 Artifacts
|
|
73
|
+
```typescript
|
|
74
|
+
import { InMemoryArtifactStorage, createPlanArtifact } from '@confused-ai/core/artifacts';
|
|
75
|
+
|
|
76
|
+
const storage = new InMemoryArtifactStorage();
|
|
77
|
+
|
|
78
|
+
// Create a plan artifact
|
|
79
|
+
const plan = await storage.save(createPlanArtifact(
|
|
80
|
+
'project-plan',
|
|
81
|
+
'Build a chatbot',
|
|
82
|
+
[
|
|
83
|
+
{ description: 'Design conversation flows' },
|
|
84
|
+
{ description: 'Implement intent detection' },
|
|
85
|
+
{ description: 'Add response generation' },
|
|
86
|
+
]
|
|
87
|
+
));
|
|
88
|
+
|
|
89
|
+
// Retrieve with versioning
|
|
90
|
+
const retrieved = await storage.get(plan.id);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 🎤 Voice (TTS/STT)
|
|
94
|
+
```typescript
|
|
95
|
+
import { OpenAIVoiceProvider } from '@confused-ai/core/voice';
|
|
96
|
+
|
|
97
|
+
const voice = new OpenAIVoiceProvider();
|
|
98
|
+
|
|
99
|
+
// Text-to-Speech
|
|
100
|
+
const { audio } = await voice.textToSpeech('Hello, world!', {
|
|
101
|
+
voiceId: 'nova',
|
|
102
|
+
speed: 1.0
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Speech-to-Text
|
|
106
|
+
const { text } = await voice.speechToText(audioBuffer);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 🔄 Resumable Streaming
|
|
110
|
+
```typescript
|
|
111
|
+
import { ResumableStreamManager, formatSSE } from '@confused-ai/core/production';
|
|
112
|
+
|
|
113
|
+
const manager = new ResumableStreamManager();
|
|
114
|
+
|
|
115
|
+
// Create stream
|
|
116
|
+
const streamId = manager.createStream();
|
|
117
|
+
|
|
118
|
+
// On each chunk from LLM
|
|
119
|
+
manager.saveChunk(streamId, { type: 'text', content: 'Hello' });
|
|
120
|
+
|
|
121
|
+
// Client reconnects after disconnect
|
|
122
|
+
const checkpoint = manager.getCheckpoint(streamId);
|
|
123
|
+
const missed = manager.getChunksSince(streamId, clientPosition);
|
|
124
|
+
|
|
125
|
+
for (const chunk of missed) {
|
|
126
|
+
res.write(formatSSE(chunk)); // SSE compatible
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 🛡️ Graceful Shutdown
|
|
131
|
+
```typescript
|
|
132
|
+
import { GracefulShutdown } from '@confused-ai/core/production';
|
|
133
|
+
|
|
134
|
+
const shutdown = new GracefulShutdown({ timeoutMs: 30000 });
|
|
135
|
+
|
|
136
|
+
shutdown.addHandler('database', async () => {
|
|
137
|
+
await db.close();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
shutdown.addHandler('http-server', async () => {
|
|
141
|
+
await server.close();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
shutdown.listen(); // Handles SIGTERM/SIGINT
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Module Imports
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
// Production resilience
|
|
153
|
+
import {
|
|
154
|
+
CircuitBreaker, RateLimiter, HealthCheckManager,
|
|
155
|
+
GracefulShutdown, ResumableStreamManager
|
|
156
|
+
} from '@confused-ai/core/production';
|
|
157
|
+
|
|
158
|
+
// Observability
|
|
159
|
+
import { OTLPTraceExporter, OTLPMetricsExporter } from '@confused-ai/core/observability';
|
|
160
|
+
|
|
161
|
+
// LLM with caching
|
|
162
|
+
import { LLMCache, withCache } from '@confused-ai/core/llm';
|
|
163
|
+
|
|
164
|
+
// Artifacts
|
|
165
|
+
import { InMemoryArtifactStorage, MediaManager } from '@confused-ai/core/artifacts';
|
|
166
|
+
|
|
167
|
+
// Voice
|
|
168
|
+
import { OpenAIVoiceProvider, ElevenLabsVoiceProvider } from '@confused-ai/core/voice';
|
|
169
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "confused-ai-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Confused AI - Production-grade TypeScript framework for orchestrating multi-agent workflows",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./core": {
|
|
14
|
+
"import": "./dist/core/index.js",
|
|
15
|
+
"types": "./dist/core/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./memory": {
|
|
18
|
+
"import": "./dist/memory/index.js",
|
|
19
|
+
"types": "./dist/memory/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./tools": {
|
|
22
|
+
"import": "./dist/tools/index.js",
|
|
23
|
+
"types": "./dist/tools/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./planner": {
|
|
26
|
+
"import": "./dist/planner/index.js",
|
|
27
|
+
"types": "./dist/planner/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./execution": {
|
|
30
|
+
"import": "./dist/execution/index.js",
|
|
31
|
+
"types": "./dist/execution/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./orchestration": {
|
|
34
|
+
"import": "./dist/orchestration/index.js",
|
|
35
|
+
"types": "./dist/orchestration/index.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./observability": {
|
|
38
|
+
"import": "./dist/observability/index.js",
|
|
39
|
+
"types": "./dist/observability/index.d.ts"
|
|
40
|
+
},
|
|
41
|
+
"./llm": {
|
|
42
|
+
"import": "./dist/llm/index.js",
|
|
43
|
+
"types": "./dist/llm/index.d.ts"
|
|
44
|
+
},
|
|
45
|
+
"./agentic": {
|
|
46
|
+
"import": "./dist/agentic/index.js",
|
|
47
|
+
"types": "./dist/agentic/index.d.ts"
|
|
48
|
+
},
|
|
49
|
+
"./session": {
|
|
50
|
+
"import": "./dist/session/index.js",
|
|
51
|
+
"types": "./dist/session/index.d.ts"
|
|
52
|
+
},
|
|
53
|
+
"./guardrails": {
|
|
54
|
+
"import": "./dist/guardrails/index.js",
|
|
55
|
+
"types": "./dist/guardrails/index.d.ts"
|
|
56
|
+
},
|
|
57
|
+
"./production": {
|
|
58
|
+
"import": "./dist/production/index.js",
|
|
59
|
+
"types": "./dist/production/index.d.ts"
|
|
60
|
+
},
|
|
61
|
+
"./artifacts": {
|
|
62
|
+
"import": "./dist/artifacts/index.js",
|
|
63
|
+
"types": "./dist/artifacts/index.d.ts"
|
|
64
|
+
},
|
|
65
|
+
"./voice": {
|
|
66
|
+
"import": "./dist/voice/index.js",
|
|
67
|
+
"types": "./dist/voice/index.d.ts"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsc",
|
|
72
|
+
"dev": "tsc --watch",
|
|
73
|
+
"typecheck": "tsc --noEmit",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest",
|
|
76
|
+
"test:coverage": "vitest run --coverage"
|
|
77
|
+
},
|
|
78
|
+
"keywords": [
|
|
79
|
+
"agent",
|
|
80
|
+
"ai",
|
|
81
|
+
"framework",
|
|
82
|
+
"typescript",
|
|
83
|
+
"multi-agent",
|
|
84
|
+
"orchestration",
|
|
85
|
+
"llm"
|
|
86
|
+
],
|
|
87
|
+
"author": "",
|
|
88
|
+
"license": "MIT",
|
|
89
|
+
"devDependencies": {
|
|
90
|
+
"@types/bun": "latest",
|
|
91
|
+
"@types/uuid": "^9.0.8",
|
|
92
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
93
|
+
"typescript": "^5.9.3",
|
|
94
|
+
"vitest": "^4.0.18"
|
|
95
|
+
},
|
|
96
|
+
"dependencies": {
|
|
97
|
+
"commander": "^14.0.3",
|
|
98
|
+
"duck-duck-scrape": "^2.2.5",
|
|
99
|
+
"openai": "^6.17.0",
|
|
100
|
+
"uuid": "^13.0.0",
|
|
101
|
+
"yahoo-finance2": "^2.11.3",
|
|
102
|
+
"zod": "^4.3.6"
|
|
103
|
+
},
|
|
104
|
+
"peerDependencies": {
|
|
105
|
+
"openai": "^6.17.0",
|
|
106
|
+
"better-sqlite3": "^11.0.0"
|
|
107
|
+
},
|
|
108
|
+
"peerDependenciesMeta": {
|
|
109
|
+
"openai": {
|
|
110
|
+
"optional": true
|
|
111
|
+
},
|
|
112
|
+
"better-sqlite3": {
|
|
113
|
+
"optional": true
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
"engines": {
|
|
117
|
+
"node": ">=18.0.0"
|
|
118
|
+
}
|
|
119
|
+
}
|
package/src/agent.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agno-style Agent class: one-line agent with clean process.
|
|
3
|
+
*
|
|
4
|
+
* Build agents that remember users across sessions and run anytime.
|
|
5
|
+
* Uses model (from env), db (session store), optional learning/session memory, and default tools.
|
|
6
|
+
*
|
|
7
|
+
* @see https://github.com/agno-agi/agno
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { SessionStore } from './session/types.js';
|
|
11
|
+
import type { Tool, ToolRegistry } from './tools/types.js';
|
|
12
|
+
import type { CreateAgentOptions, CreateAgentResult, AgentRunOptions } from './create-agent.js';
|
|
13
|
+
import { createAgent } from './create-agent.js';
|
|
14
|
+
import { InMemorySessionStore } from './session/index.js';
|
|
15
|
+
import { HttpClientTool } from './tools/http-tool.js';
|
|
16
|
+
import { BrowserTool } from './tools/browser-tool.js';
|
|
17
|
+
import type { AgenticRunResult } from './agentic/types.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Agno-style agent options: minimal config for a clean agent process.
|
|
21
|
+
*/
|
|
22
|
+
export interface AgentOptions {
|
|
23
|
+
/** Agent name (default: 'Agent') */
|
|
24
|
+
name?: string;
|
|
25
|
+
/** System instructions / prompt */
|
|
26
|
+
instructions: string;
|
|
27
|
+
/**
|
|
28
|
+
* Model: id (e.g. gpt-4o) or "provider:model_id" (e.g. openai:gpt-4o, openrouter:anthropic/claude-3.5-sonnet, ollama:llama3.2).
|
|
29
|
+
* Omitted: uses OPENAI_API_KEY + OPENAI_MODEL from env.
|
|
30
|
+
*/
|
|
31
|
+
model?: string;
|
|
32
|
+
/** API key (optional; falls back to OPENAI_API_KEY / OPENROUTER_API_KEY) */
|
|
33
|
+
apiKey?: string;
|
|
34
|
+
/** Base URL for API (e.g. Ollama). Falls back to OPENAI_BASE_URL. */
|
|
35
|
+
baseURL?: string;
|
|
36
|
+
/** Use OpenRouter (set apiKey/model here or via env) */
|
|
37
|
+
openRouter?: { apiKey?: string; model?: string };
|
|
38
|
+
/**
|
|
39
|
+
* Session store for conversation persistence (db in Agno terms).
|
|
40
|
+
* Default: InMemorySessionStore(). Use a persistent store (e.g. SQLite) for production.
|
|
41
|
+
*/
|
|
42
|
+
db?: SessionStore;
|
|
43
|
+
/**
|
|
44
|
+
* When true, agent uses session memory: pass sessionId to run() to continue conversations.
|
|
45
|
+
* Default: true. Set false for stateless single-turn runs only.
|
|
46
|
+
*/
|
|
47
|
+
learning?: boolean;
|
|
48
|
+
/** Tools: array or registry. Default: [HttpClientTool, BrowserTool]. Plug any tools by implementing Tool[] or ToolRegistry. */
|
|
49
|
+
tools?: Tool[] | ToolRegistry;
|
|
50
|
+
/** Max agentic steps (default: 10) */
|
|
51
|
+
maxSteps?: number;
|
|
52
|
+
/** Run timeout ms (default: 60000) */
|
|
53
|
+
timeoutMs?: number;
|
|
54
|
+
/** Guardrails. Default: true (sensitive-data rule). Set false to disable. */
|
|
55
|
+
guardrails?: CreateAgentOptions['guardrails'];
|
|
56
|
+
/** Retry policy for LLM/tool calls */
|
|
57
|
+
retry?: CreateAgentOptions['retry'];
|
|
58
|
+
/** Cross-tool middleware (logging, rate limit, etc.). */
|
|
59
|
+
toolMiddleware?: CreateAgentOptions['toolMiddleware'];
|
|
60
|
+
/** Optional logger for production observability */
|
|
61
|
+
logger?: CreateAgentOptions['logger'];
|
|
62
|
+
/** Learning mode: always vs agentic */
|
|
63
|
+
learningMode?: CreateAgentOptions['learningMode'];
|
|
64
|
+
/** User profile store (profiles that persist across sessions) */
|
|
65
|
+
userProfileStore?: CreateAgentOptions['userProfileStore'];
|
|
66
|
+
/** Memory store (memories that accumulate, knowledge that transfers) */
|
|
67
|
+
memoryStore?: CreateAgentOptions['memoryStore'];
|
|
68
|
+
/** RAG engine (agentic RAG, hybrid search, reranking) */
|
|
69
|
+
ragEngine?: CreateAgentOptions['ragEngine'];
|
|
70
|
+
/** Type-safe input/output schemas (Zod) */
|
|
71
|
+
inputSchema?: CreateAgentOptions['inputSchema'];
|
|
72
|
+
outputSchema?: CreateAgentOptions['outputSchema'];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Agno-style Agent: create once, run anytime with a clean process.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* // One line – uses OPENAI_API_KEY, default tools (http + browser), session memory
|
|
80
|
+
* const agent = new Agent({
|
|
81
|
+
* instructions: 'You are a helpful assistant. Use the web when needed.',
|
|
82
|
+
* });
|
|
83
|
+
* const result = await agent.run('What is the weather in Tokyo?');
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* // With session (learning) – remembers conversation
|
|
87
|
+
* const agent = new Agent({ instructions: '...', learning: true });
|
|
88
|
+
* const sessionId = await agent.createSession('user-1');
|
|
89
|
+
* const r1 = await agent.run('My name is Alice.', { sessionId });
|
|
90
|
+
* const r2 = await agent.run('What is my name?', { sessionId }); // "Alice"
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* // Model from env or explicit
|
|
94
|
+
* const agent = new Agent({
|
|
95
|
+
* instructions: '...',
|
|
96
|
+
* model: 'openai:gpt-4o', // or just 'gpt-4o' with OPENAI_API_KEY set
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* // Local Ollama
|
|
101
|
+
* const agent = new Agent({
|
|
102
|
+
* instructions: '...',
|
|
103
|
+
* baseURL: 'http://localhost:11434/v1',
|
|
104
|
+
* model: 'llama3.2',
|
|
105
|
+
* });
|
|
106
|
+
*/
|
|
107
|
+
export class Agent {
|
|
108
|
+
readonly name: string;
|
|
109
|
+
readonly instructions: string;
|
|
110
|
+
readonly learning: boolean;
|
|
111
|
+
private readonly delegate: CreateAgentResult;
|
|
112
|
+
|
|
113
|
+
constructor(options: AgentOptions) {
|
|
114
|
+
const {
|
|
115
|
+
name = 'Agent',
|
|
116
|
+
instructions,
|
|
117
|
+
model,
|
|
118
|
+
apiKey,
|
|
119
|
+
baseURL,
|
|
120
|
+
openRouter,
|
|
121
|
+
db,
|
|
122
|
+
learning = true,
|
|
123
|
+
tools = [new HttpClientTool(), new BrowserTool()],
|
|
124
|
+
maxSteps,
|
|
125
|
+
timeoutMs,
|
|
126
|
+
guardrails,
|
|
127
|
+
retry,
|
|
128
|
+
toolMiddleware,
|
|
129
|
+
logger,
|
|
130
|
+
learningMode,
|
|
131
|
+
userProfileStore,
|
|
132
|
+
memoryStore,
|
|
133
|
+
ragEngine,
|
|
134
|
+
inputSchema,
|
|
135
|
+
outputSchema,
|
|
136
|
+
} = options;
|
|
137
|
+
|
|
138
|
+
const createOpts: CreateAgentOptions = {
|
|
139
|
+
name,
|
|
140
|
+
instructions,
|
|
141
|
+
model,
|
|
142
|
+
apiKey,
|
|
143
|
+
baseURL,
|
|
144
|
+
openRouter,
|
|
145
|
+
tools,
|
|
146
|
+
toolMiddleware,
|
|
147
|
+
sessionStore: db ?? new InMemorySessionStore(),
|
|
148
|
+
guardrails,
|
|
149
|
+
maxSteps,
|
|
150
|
+
timeoutMs,
|
|
151
|
+
retry,
|
|
152
|
+
logger,
|
|
153
|
+
learningMode,
|
|
154
|
+
userProfileStore,
|
|
155
|
+
memoryStore,
|
|
156
|
+
ragEngine,
|
|
157
|
+
inputSchema,
|
|
158
|
+
outputSchema,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
this.delegate = createAgent(createOpts);
|
|
162
|
+
this.name = this.delegate.name;
|
|
163
|
+
this.instructions = this.delegate.instructions;
|
|
164
|
+
this.learning = learning;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Run the agent with a prompt. Pass sessionId for conversation memory (when learning is true).
|
|
169
|
+
*/
|
|
170
|
+
async run(prompt: string, options?: AgentRunOptions): Promise<AgenticRunResult> {
|
|
171
|
+
return this.delegate.run(prompt, options);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Create a new session (returns sessionId to pass to run() for multi-turn memory).
|
|
176
|
+
*/
|
|
177
|
+
async createSession(userId?: string): Promise<string> {
|
|
178
|
+
return this.delegate.createSession(userId);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Get messages for a session (if using a session store).
|
|
183
|
+
*/
|
|
184
|
+
async getSessionMessages(sessionId: string) {
|
|
185
|
+
return this.delegate.getSessionMessages(sessionId);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agentic loop (ReAct-style) exports
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from './types.js';
|
|
6
|
+
export { AgenticRunner } from './runner.js';
|
|
7
|
+
|
|
8
|
+
import type { LLMProvider } from '../llm/types.js';
|
|
9
|
+
import type { ToolRegistry, ToolMiddleware } from '../tools/types.js';
|
|
10
|
+
import type { AgenticRunConfig, AgenticRunResult, AgenticStreamHooks } from './types.js';
|
|
11
|
+
import type { HumanInTheLoopHooks, GuardrailEngine } from '../guardrails/types.js';
|
|
12
|
+
import { AgenticRunner } from './runner.js';
|
|
13
|
+
import { toToolRegistry, type ToolProvider } from '../tools/registry.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a production-style agentic agent (ReAct loop with LLM + tools).
|
|
17
|
+
* Use with OpenAIProvider or any LLMProvider implementation.
|
|
18
|
+
* Supports Tool[] or ToolRegistry and optional tool middleware for cross-tool integration.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { createAgenticAgent, OpenAIProvider } from '@agent-framework/core';
|
|
22
|
+
* const agent = createAgenticAgent({
|
|
23
|
+
* name: 'Researcher',
|
|
24
|
+
* instructions: 'You research topics and cite sources.',
|
|
25
|
+
* llm: new OpenAIProvider({ model: 'gpt-4o' }),
|
|
26
|
+
* tools: myToolRegistry,
|
|
27
|
+
* toolMiddleware: [loggingMiddleware],
|
|
28
|
+
* maxSteps: 10,
|
|
29
|
+
* });
|
|
30
|
+
* const result = await agent.run({ prompt: 'Latest TypeScript news', instructions: agent.instructions });
|
|
31
|
+
*/
|
|
32
|
+
export function createAgenticAgent(config: {
|
|
33
|
+
name: string;
|
|
34
|
+
instructions: string;
|
|
35
|
+
llm: LLMProvider;
|
|
36
|
+
tools: ToolRegistry | import('../tools/types.js').Tool[];
|
|
37
|
+
maxSteps?: number;
|
|
38
|
+
timeoutMs?: number;
|
|
39
|
+
retry?: import('./types.js').AgenticRetryPolicy;
|
|
40
|
+
humanInTheLoop?: HumanInTheLoopHooks;
|
|
41
|
+
guardrails?: GuardrailEngine;
|
|
42
|
+
toolMiddleware?: ToolMiddleware[];
|
|
43
|
+
}): {
|
|
44
|
+
name: string;
|
|
45
|
+
instructions: string;
|
|
46
|
+
run(
|
|
47
|
+
runConfig: { prompt: string; instructions?: string; messages?: import('../llm/types.js').Message[]; maxSteps?: number; timeoutMs?: number },
|
|
48
|
+
hooks?: AgenticStreamHooks
|
|
49
|
+
): Promise<AgenticRunResult>;
|
|
50
|
+
} {
|
|
51
|
+
const toolRegistry = toToolRegistry(config.tools as ToolProvider);
|
|
52
|
+
|
|
53
|
+
const runner = new AgenticRunner({
|
|
54
|
+
llm: config.llm,
|
|
55
|
+
tools: toolRegistry,
|
|
56
|
+
maxSteps: config.maxSteps ?? 10,
|
|
57
|
+
timeoutMs: config.timeoutMs ?? 60_000,
|
|
58
|
+
retry: config.retry,
|
|
59
|
+
toolMiddleware: config.toolMiddleware,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Set human-in-the-loop hooks if provided
|
|
63
|
+
if (config.humanInTheLoop) {
|
|
64
|
+
runner.setHumanInTheLoop(config.humanInTheLoop);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Set guardrails if provided
|
|
68
|
+
if (config.guardrails) {
|
|
69
|
+
runner.setGuardrails(config.guardrails);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
name: config.name,
|
|
74
|
+
instructions: config.instructions,
|
|
75
|
+
async run(runConfig, hooks) {
|
|
76
|
+
const instructions = runConfig.instructions ?? config.instructions;
|
|
77
|
+
const cfg: AgenticRunConfig = {
|
|
78
|
+
instructions,
|
|
79
|
+
prompt: runConfig.prompt,
|
|
80
|
+
messages: runConfig.messages,
|
|
81
|
+
maxSteps: runConfig.maxSteps,
|
|
82
|
+
timeoutMs: runConfig.timeoutMs,
|
|
83
|
+
};
|
|
84
|
+
return runner.run(cfg, hooks);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|