@toolplex/ai-engine 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/LICENSE +98 -0
- package/README.md +292 -0
- package/dist/adapters/index.d.ts +9 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +9 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/types.d.ts +137 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +14 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/core/ChatEngine.d.ts +47 -0
- package/dist/core/ChatEngine.d.ts.map +1 -0
- package/dist/core/ChatEngine.js +355 -0
- package/dist/core/ChatEngine.js.map +1 -0
- package/dist/core/ToolBuilder.d.ts +25 -0
- package/dist/core/ToolBuilder.d.ts.map +1 -0
- package/dist/core/ToolBuilder.js +215 -0
- package/dist/core/ToolBuilder.js.map +1 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/DefaultStdioTransportFactory.d.ts +27 -0
- package/dist/mcp/DefaultStdioTransportFactory.d.ts.map +1 -0
- package/dist/mcp/DefaultStdioTransportFactory.js +60 -0
- package/dist/mcp/DefaultStdioTransportFactory.js.map +1 -0
- package/dist/mcp/MCPClient.d.ts +60 -0
- package/dist/mcp/MCPClient.d.ts.map +1 -0
- package/dist/mcp/MCPClient.js +164 -0
- package/dist/mcp/MCPClient.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +11 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/paths.d.ts +16 -0
- package/dist/mcp/paths.d.ts.map +1 -0
- package/dist/mcp/paths.js +58 -0
- package/dist/mcp/paths.js.map +1 -0
- package/dist/mcp/types.d.ts +85 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +7 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/providers/index.d.ts +40 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +148 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/toolplex.d.ts +43 -0
- package/dist/providers/toolplex.d.ts.map +1 -0
- package/dist/providers/toolplex.js +168 -0
- package/dist/providers/toolplex.js.map +1 -0
- package/dist/types/index.d.ts +218 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +8 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/models.d.ts +30 -0
- package/dist/utils/models.d.ts.map +1 -0
- package/dist/utils/models.js +52 -0
- package/dist/utils/models.js.map +1 -0
- package/dist/utils/schema.d.ts +74 -0
- package/dist/utils/schema.d.ts.map +1 -0
- package/dist/utils/schema.js +253 -0
- package/dist/utils/schema.js.map +1 -0
- package/package.json +70 -0
- package/src/adapters/index.ts +9 -0
- package/src/adapters/types.ts +241 -0
- package/src/core/ChatEngine.ts +464 -0
- package/src/core/ToolBuilder.ts +323 -0
- package/src/core/index.ts +6 -0
- package/src/index.ts +86 -0
- package/src/mcp/DefaultStdioTransportFactory.ts +71 -0
- package/src/mcp/MCPClient.ts +209 -0
- package/src/mcp/index.ts +24 -0
- package/src/mcp/paths.ts +91 -0
- package/src/mcp/types.ts +93 -0
- package/src/providers/index.ts +177 -0
- package/src/providers/toolplex.ts +217 -0
- package/src/types/index.ts +290 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/models.ts +59 -0
- package/src/utils/schema.ts +307 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
License text copyright (c) 2025 ToolPlex LLC. All Rights Reserved.
|
|
2
|
+
"Business Source License" is a trademark of MariaDB Corporation Ab.
|
|
3
|
+
|
|
4
|
+
Parameters
|
|
5
|
+
|
|
6
|
+
Licensor: ToolPlex LLC
|
|
7
|
+
Licensed Work: ToolPlex AI Engine, as published in this repository. The Licensed Work is (c) 2025
|
|
8
|
+
ToolPlex LLC.
|
|
9
|
+
Additional Use Grant: You may make production use of the Licensed Work, provided
|
|
10
|
+
your use does not involve offering it or derivative works on a
|
|
11
|
+
hosted or embedded basis to compete with ToolPlex's paid platform
|
|
12
|
+
or services. For purposes of this license:
|
|
13
|
+
|
|
14
|
+
A "competitive offering" is a product or service that is offered
|
|
15
|
+
to third parties on a paid basis (including through paid support),
|
|
16
|
+
and that significantly overlaps with ToolPlex's platform, including:
|
|
17
|
+
tool discovery, tool execution, agent control, or workflow/playbook
|
|
18
|
+
reuse.
|
|
19
|
+
|
|
20
|
+
"Product" means software made available to end users or used to
|
|
21
|
+
operate hosted services.
|
|
22
|
+
|
|
23
|
+
"Embedded" includes incorporating source or binary components of
|
|
24
|
+
the Licensed Work into another product or requiring the Licensed
|
|
25
|
+
Work for that product to function.
|
|
26
|
+
|
|
27
|
+
"Internal use" within your organization is permitted, even for
|
|
28
|
+
commercial purposes, as long as it is not redistributed or offered
|
|
29
|
+
to others.
|
|
30
|
+
|
|
31
|
+
"API Integration Restrictions": Use of the ToolPlex API or services
|
|
32
|
+
to build, enhance, or operate a competitive offering is prohibited,
|
|
33
|
+
regardless of whether the Licensed Work is directly incorporated.
|
|
34
|
+
|
|
35
|
+
"Derivative Works": Includes any modification, adaptation, or custom
|
|
36
|
+
implementation based on the Licensed Work, including but not limited
|
|
37
|
+
to custom clients for ToolPlex API interaction. While permitted under
|
|
38
|
+
this license, such derivatives may present technical challenges and
|
|
39
|
+
are not officially supported.
|
|
40
|
+
|
|
41
|
+
"Research and Academic Use": Non-commercial research, educational,
|
|
42
|
+
and academic use is permitted regardless of competitive overlap,
|
|
43
|
+
provided results are not used to develop commercial competitive
|
|
44
|
+
offerings.
|
|
45
|
+
|
|
46
|
+
"Official Client Recommendation": While custom clients are permitted
|
|
47
|
+
under this license, ToolPlex strongly recommends using the official
|
|
48
|
+
client to ensure compatibility, security, and optimal performance.
|
|
49
|
+
|
|
50
|
+
Hosting or using the Licensed Work for internal purposes within an
|
|
51
|
+
organization is not considered a competitive offering. ToolPlex
|
|
52
|
+
considers your organization to include all of your affiliates under
|
|
53
|
+
common control.
|
|
54
|
+
|
|
55
|
+
Change Date: None
|
|
56
|
+
Change License: MPL 2.0
|
|
57
|
+
|
|
58
|
+
For information about alternative licensing arrangements for the Licensed Work,
|
|
59
|
+
please contact support@toolplex.ai.
|
|
60
|
+
|
|
61
|
+
Notice
|
|
62
|
+
|
|
63
|
+
Business Source License 1.1
|
|
64
|
+
|
|
65
|
+
Terms
|
|
66
|
+
|
|
67
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
68
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
69
|
+
Licensor may make an Additional Use Grant, above, permitting limited production use.
|
|
70
|
+
|
|
71
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
72
|
+
currently in effect as described in this License, you must purchase a
|
|
73
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
74
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
75
|
+
|
|
76
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
77
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
78
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
79
|
+
for each version of the Licensed Work released by Licensor.
|
|
80
|
+
|
|
81
|
+
You must conspicuously display this License on each original or modified copy
|
|
82
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
83
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
84
|
+
License apply to your use of that work.
|
|
85
|
+
|
|
86
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
87
|
+
terminate your rights under this License for the current and all other
|
|
88
|
+
versions of the Licensed Work.
|
|
89
|
+
|
|
90
|
+
This License does not grant you any right in any trademark or logo of
|
|
91
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
92
|
+
Licensor as expressly required by this License).
|
|
93
|
+
|
|
94
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
95
|
+
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
96
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
97
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
98
|
+
TITLE.
|
package/README.md
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# @toolplex/ai-engine
|
|
2
|
+
|
|
3
|
+
The core AI chat engine for [ToolPlex](https://toolplex.ai). A TypeScript SDK that provides a unified interface for building AI-powered applications with tool calling via the Model Context Protocol (MCP).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Multi-provider support** - Anthropic, OpenAI, Google Gemini, OpenRouter
|
|
8
|
+
- **MCP integration** - Connect to ToolPlex's MCP server for powerful tool calling
|
|
9
|
+
- **Transport abstraction** - Works in CLI, desktop (Electron), or cloud environments
|
|
10
|
+
- **AI SDK wrapper** - Built on [Vercel AI SDK](https://sdk.vercel.ai) with ToolPlex-specific enhancements
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @toolplex/ai-engine
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
### Basic Chat (No Tools)
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { streamText, createAnthropic } from '@toolplex/ai-engine';
|
|
24
|
+
|
|
25
|
+
const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
|
|
26
|
+
|
|
27
|
+
const result = await streamText({
|
|
28
|
+
model: anthropic('claude-sonnet-4-20250514'),
|
|
29
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
for await (const chunk of result.textStream) {
|
|
33
|
+
process.stdout.write(chunk);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### With ToolPlex MCP Tools
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import {
|
|
41
|
+
MCPClient,
|
|
42
|
+
defaultStdioTransportFactory,
|
|
43
|
+
streamText,
|
|
44
|
+
createAnthropic
|
|
45
|
+
} from '@toolplex/ai-engine';
|
|
46
|
+
|
|
47
|
+
// Create MCP client (connects to @toolplex/client)
|
|
48
|
+
const mcpClient = new MCPClient({
|
|
49
|
+
transportFactory: defaultStdioTransportFactory,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Create a session
|
|
53
|
+
await mcpClient.createSession('my-session', process.env.TOOLPLEX_API_KEY);
|
|
54
|
+
|
|
55
|
+
// List available tools
|
|
56
|
+
const { tools } = await mcpClient.listTools('my-session');
|
|
57
|
+
console.log('Available tools:', tools.map(t => t.name));
|
|
58
|
+
|
|
59
|
+
// Call a tool
|
|
60
|
+
const result = await mcpClient.callTool('my-session', 'search_servers', {
|
|
61
|
+
query: 'weather',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Clean up
|
|
65
|
+
await mcpClient.destroySession('my-session');
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## API Reference
|
|
69
|
+
|
|
70
|
+
### Providers
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import {
|
|
74
|
+
createAnthropic,
|
|
75
|
+
createOpenAI,
|
|
76
|
+
createGoogleGenerativeAI,
|
|
77
|
+
createOpenRouter,
|
|
78
|
+
getProvider,
|
|
79
|
+
getModel,
|
|
80
|
+
} from '@toolplex/ai-engine';
|
|
81
|
+
|
|
82
|
+
// Direct provider creation
|
|
83
|
+
const anthropic = createAnthropic({ apiKey: '...' });
|
|
84
|
+
const openai = createOpenAI({ apiKey: '...' });
|
|
85
|
+
|
|
86
|
+
// Or use the unified getProvider helper
|
|
87
|
+
const model = getModel('anthropic/claude-sonnet-4-20250514', {
|
|
88
|
+
anthropicApiKey: '...',
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### MCP Client
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { MCPClient, TransportFactory } from '@toolplex/ai-engine';
|
|
96
|
+
|
|
97
|
+
const client = new MCPClient({
|
|
98
|
+
transportFactory: myTransportFactory,
|
|
99
|
+
logger: console, // optional
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Session management
|
|
103
|
+
await client.createSession(sessionId, apiKey, resumeHistory?);
|
|
104
|
+
await client.destroySession(sessionId);
|
|
105
|
+
client.getActiveSessions();
|
|
106
|
+
client.hasSession(sessionId);
|
|
107
|
+
|
|
108
|
+
// Tool operations
|
|
109
|
+
await client.listTools(sessionId);
|
|
110
|
+
await client.callTool(sessionId, toolName, args);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Transport Factory
|
|
114
|
+
|
|
115
|
+
The `TransportFactory` interface allows you to customize how the MCP server is spawned:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { TransportFactory, MCPSession } from '@toolplex/ai-engine';
|
|
119
|
+
|
|
120
|
+
class MyCustomTransportFactory implements TransportFactory {
|
|
121
|
+
async createTransport(apiKey: string, resumeHistory?: string): Promise<MCPSession> {
|
|
122
|
+
// Spawn @toolplex/client with custom configuration
|
|
123
|
+
// Return { client, transport }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async closeTransport(session: MCPSession): Promise<void> {
|
|
127
|
+
await session.client.close();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**Built-in transports:**
|
|
133
|
+
|
|
134
|
+
- `defaultStdioTransportFactory` - Uses system Node.js (for CLI apps)
|
|
135
|
+
|
|
136
|
+
### Utilities
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import {
|
|
140
|
+
// Schema utilities
|
|
141
|
+
deepSanitizeParams,
|
|
142
|
+
resolveSchemaRefs,
|
|
143
|
+
sanitizeSchemaForGemini,
|
|
144
|
+
|
|
145
|
+
// Model detection
|
|
146
|
+
isChatGPTModel,
|
|
147
|
+
isGoogleGeminiModel,
|
|
148
|
+
isAnthropicModel,
|
|
149
|
+
parseModelId,
|
|
150
|
+
|
|
151
|
+
// Path utilities
|
|
152
|
+
getToolplexClientPath,
|
|
153
|
+
} from '@toolplex/ai-engine';
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### AI SDK Re-exports
|
|
157
|
+
|
|
158
|
+
For convenience, common AI SDK exports are re-exported:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
import {
|
|
162
|
+
streamText,
|
|
163
|
+
tool,
|
|
164
|
+
jsonSchema,
|
|
165
|
+
stepCountIs,
|
|
166
|
+
type CoreMessage,
|
|
167
|
+
type ToolResultPart,
|
|
168
|
+
type ToolCallPart,
|
|
169
|
+
} from '@toolplex/ai-engine';
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Custom Transport Example (Electron)
|
|
173
|
+
|
|
174
|
+
For Electron apps with bundled Node.js:
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import {
|
|
178
|
+
MCPClient,
|
|
179
|
+
MCPSDKClient as Client,
|
|
180
|
+
StdioClientTransport,
|
|
181
|
+
getToolplexClientPath,
|
|
182
|
+
type TransportFactory,
|
|
183
|
+
type MCPSession,
|
|
184
|
+
} from '@toolplex/ai-engine';
|
|
185
|
+
|
|
186
|
+
class ElectronTransportFactory implements TransportFactory {
|
|
187
|
+
async createTransport(apiKey: string): Promise<MCPSession> {
|
|
188
|
+
const toolplexPath = getToolplexClientPath();
|
|
189
|
+
|
|
190
|
+
const transport = new StdioClientTransport({
|
|
191
|
+
command: '/path/to/bundled/node', // Use bundled Node.js
|
|
192
|
+
args: [toolplexPath],
|
|
193
|
+
env: {
|
|
194
|
+
...process.env,
|
|
195
|
+
TOOLPLEX_API_KEY: apiKey,
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const client = new Client({ name: 'my-app', version: '1.0.0' });
|
|
200
|
+
await client.connect(transport);
|
|
201
|
+
|
|
202
|
+
return { transport, client };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async closeTransport(session: MCPSession): Promise<void> {
|
|
206
|
+
await session.client.close();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const mcpClient = new MCPClient({
|
|
211
|
+
transportFactory: new ElectronTransportFactory(),
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Environment Variables
|
|
216
|
+
|
|
217
|
+
When using `defaultStdioTransportFactory`, these environment variables are passed to the MCP server:
|
|
218
|
+
|
|
219
|
+
- `TOOLPLEX_API_KEY` - Your ToolPlex API key (required)
|
|
220
|
+
- `TOOLPLEX_SESSION_RESUME_HISTORY` - JSON string containing historical tool usage for resumed sessions
|
|
221
|
+
|
|
222
|
+
### Session Resume History
|
|
223
|
+
|
|
224
|
+
When restoring a chat session from a database, the MCP server needs context about what tools were previously used. This allows the enforcement layer to validate operations like `save_playbook` and `submit_feedback` which depend on historical tool usage.
|
|
225
|
+
|
|
226
|
+
The `resumeHistory` parameter is a JSON string with the following structure:
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
interface SessionResumeHistory {
|
|
230
|
+
tool_calls: Array<{ server_id: string; tool_name: string }>;
|
|
231
|
+
installs: Array<{ server_id: string }>;
|
|
232
|
+
uninstalls: Array<{ server_id: string }>;
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Example usage:**
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
// When creating a session with history (e.g., restoring from database)
|
|
240
|
+
const resumeHistory = JSON.stringify({
|
|
241
|
+
tool_calls: [
|
|
242
|
+
{ server_id: 'weather-server', tool_name: 'get_forecast' },
|
|
243
|
+
{ server_id: 'calendar-server', tool_name: 'create_event' },
|
|
244
|
+
],
|
|
245
|
+
installs: [
|
|
246
|
+
{ server_id: 'weather-server' },
|
|
247
|
+
],
|
|
248
|
+
uninstalls: [],
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
await mcpClient.createSession('my-session', apiKey, resumeHistory);
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
**In a custom transport factory:**
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
class MyTransportFactory implements TransportFactory {
|
|
258
|
+
async createTransport(apiKey: string, sessionResumeHistory?: string): Promise<MCPSession> {
|
|
259
|
+
const transport = new StdioClientTransport({
|
|
260
|
+
command: 'node',
|
|
261
|
+
args: [toolplexPath],
|
|
262
|
+
env: {
|
|
263
|
+
...process.env,
|
|
264
|
+
TOOLPLEX_API_KEY: apiKey,
|
|
265
|
+
// Pass resume history to the MCP server process
|
|
266
|
+
...(sessionResumeHistory && { TOOLPLEX_SESSION_RESUME_HISTORY: sessionResumeHistory }),
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const client = new Client({ name: 'my-app', version: '1.0.0' });
|
|
271
|
+
await client.connect(transport);
|
|
272
|
+
return { transport, client };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
This is particularly useful in desktop applications where chat sessions are persisted and can be restored later.
|
|
278
|
+
|
|
279
|
+
## Requirements
|
|
280
|
+
|
|
281
|
+
- Node.js 18+
|
|
282
|
+
- A ToolPlex API key (get one at [toolplex.ai](https://toolplex.ai))
|
|
283
|
+
|
|
284
|
+
## License
|
|
285
|
+
|
|
286
|
+
[BSL 1.1](./LICENSE)
|
|
287
|
+
|
|
288
|
+
## Links
|
|
289
|
+
|
|
290
|
+
- [ToolPlex](https://toolplex.ai)
|
|
291
|
+
- [Documentation](https://docs.toolplex.ai)
|
|
292
|
+
- [@toolplex/client](https://github.com/toolplex/client) - The MCP server
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @toolplex/ai-engine - Adapters
|
|
3
|
+
*
|
|
4
|
+
* Export adapter types and any shared adapter utilities.
|
|
5
|
+
* Platform-specific adapters (Electron, HTTP, CLI) are provided
|
|
6
|
+
* separately to avoid bundling unnecessary dependencies.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./types.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @toolplex/ai-engine - Adapters
|
|
3
|
+
*
|
|
4
|
+
* Export adapter types and any shared adapter utilities.
|
|
5
|
+
* Platform-specific adapters (Electron, HTTP, CLI) are provided
|
|
6
|
+
* separately to avoid bundling unnecessary dependencies.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./types.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @toolplex/ai-engine - Adapter Interface
|
|
3
|
+
*
|
|
4
|
+
* The EngineAdapter interface defines how the AI engine interacts with
|
|
5
|
+
* the host platform. This abstraction allows the same engine core to
|
|
6
|
+
* run in Electron, server-side (cloud), or CLI environments.
|
|
7
|
+
*
|
|
8
|
+
* Implementations:
|
|
9
|
+
* - ElectronAdapter: Desktop app with IPC, webContents, confirmations
|
|
10
|
+
* - HTTPAdapter: Cloud/server with HTTP streaming, no confirmations
|
|
11
|
+
* - CLIAdapter: CLI with terminal I/O
|
|
12
|
+
*/
|
|
13
|
+
import type { ProviderCredentials, ConfirmationRequest, ConfirmationResult, MCPResult, MCPSessionInfo, MCPTool, MCPToolResult, UsageData, ChatSession, ChatMessage } from "../types/index.js";
|
|
14
|
+
/**
|
|
15
|
+
* Interface for emitting engine events to the platform
|
|
16
|
+
*/
|
|
17
|
+
export interface EngineEventEmitter {
|
|
18
|
+
/** Emit a text chunk during streaming */
|
|
19
|
+
emitChunk(streamId: string, chunk: string): void;
|
|
20
|
+
/** Emit stream completion */
|
|
21
|
+
emitComplete(streamId: string, fullText: string, usage?: UsageData): void;
|
|
22
|
+
/** Emit stream error */
|
|
23
|
+
emitError(streamId: string, error: string): void;
|
|
24
|
+
/** Emit tool input start (for streaming tool arguments) */
|
|
25
|
+
emitToolInputStart(streamId: string, toolCallId: string, toolName: string): void;
|
|
26
|
+
/** Emit tool input delta (streaming argument chunks) */
|
|
27
|
+
emitToolInputDelta(streamId: string, toolCallId: string, argsDelta: string): void;
|
|
28
|
+
/** Emit tool execution result */
|
|
29
|
+
emitToolResult(streamId: string, toolCallId: string, result: MCPToolResult, toolName: string, args: any): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Interface for handling user confirmations
|
|
33
|
+
* Desktop: Shows modal dialogs via IPC
|
|
34
|
+
* Cloud: Auto-approves or uses policy-based decisions
|
|
35
|
+
* CLI: Prompts in terminal
|
|
36
|
+
*/
|
|
37
|
+
export interface ConfirmationHandler {
|
|
38
|
+
/**
|
|
39
|
+
* Request user confirmation for a tool operation
|
|
40
|
+
* @param streamId - The current stream ID
|
|
41
|
+
* @param request - The confirmation request details
|
|
42
|
+
* @returns Confirmation result with allowed/denied and any edits
|
|
43
|
+
*/
|
|
44
|
+
requestConfirmation(streamId: string, request: ConfirmationRequest): Promise<ConfirmationResult>;
|
|
45
|
+
/**
|
|
46
|
+
* Whether this handler supports interactive confirmations
|
|
47
|
+
* Cloud handlers may return false to auto-approve based on policy
|
|
48
|
+
*/
|
|
49
|
+
isInteractive(): boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Interface for MCP transport operations
|
|
53
|
+
* Abstracts away the transport mechanism (stdio, HTTP, etc.)
|
|
54
|
+
*/
|
|
55
|
+
export interface MCPTransportAdapter {
|
|
56
|
+
/** Create/connect MCP transport for a session */
|
|
57
|
+
createTransport(sessionId: string, apiKey: string, sessionResumeHistory?: string): Promise<MCPResult>;
|
|
58
|
+
/** Get session info */
|
|
59
|
+
getSessionInfo(sessionId: string): MCPSessionInfo;
|
|
60
|
+
/** List available tools from MCP server */
|
|
61
|
+
listTools(sessionId: string): Promise<{
|
|
62
|
+
tools: MCPTool[];
|
|
63
|
+
}>;
|
|
64
|
+
/** Call an MCP tool */
|
|
65
|
+
callTool(sessionId: string, toolName: string, args: any): Promise<MCPToolResult>;
|
|
66
|
+
/** Destroy/disconnect MCP transport */
|
|
67
|
+
destroyTransport(sessionId: string): Promise<MCPResult>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Interface for accessing API credentials
|
|
71
|
+
* Different platforms load credentials differently
|
|
72
|
+
*/
|
|
73
|
+
export interface CredentialsProvider {
|
|
74
|
+
/** Get provider credentials for AI SDK */
|
|
75
|
+
getCredentials(): Promise<ProviderCredentials>;
|
|
76
|
+
/** Get ToolPlex API key specifically */
|
|
77
|
+
getToolPlexApiKey(): Promise<string>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Interface for persisting chat sessions and messages
|
|
81
|
+
* Optional - cloud may use different storage than desktop
|
|
82
|
+
*/
|
|
83
|
+
export interface PersistenceAdapter {
|
|
84
|
+
createSession(metadata?: Record<string, any>): Promise<ChatSession>;
|
|
85
|
+
getSession(sessionId: string): Promise<ChatSession | null>;
|
|
86
|
+
updateSession(sessionId: string, updates: Partial<ChatSession>): Promise<void>;
|
|
87
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
88
|
+
saveMessage(message: Omit<ChatMessage, "id" | "createdAt">): Promise<ChatMessage>;
|
|
89
|
+
getMessages(sessionId: string): Promise<ChatMessage[]>;
|
|
90
|
+
updateMessage(messageId: string, updates: Partial<ChatMessage>): Promise<void>;
|
|
91
|
+
deleteMessage(messageId: string): Promise<void>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Interface for logging
|
|
95
|
+
* Allows different logging implementations per platform
|
|
96
|
+
*/
|
|
97
|
+
export interface LoggerAdapter {
|
|
98
|
+
debug(message: string, meta?: Record<string, any>): void;
|
|
99
|
+
info(message: string, meta?: Record<string, any>): void;
|
|
100
|
+
warn(message: string, meta?: Record<string, any>): void;
|
|
101
|
+
error(message: string, meta?: Record<string, any>): void;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The main adapter interface that platforms must implement
|
|
105
|
+
* This brings together all the sub-interfaces needed by the engine
|
|
106
|
+
*/
|
|
107
|
+
export interface EngineAdapter {
|
|
108
|
+
/** Event emitter for streaming events */
|
|
109
|
+
readonly events: EngineEventEmitter;
|
|
110
|
+
/** Confirmation handler for user approvals */
|
|
111
|
+
readonly confirmations: ConfirmationHandler;
|
|
112
|
+
/** MCP transport adapter */
|
|
113
|
+
readonly mcp: MCPTransportAdapter;
|
|
114
|
+
/** Credentials provider */
|
|
115
|
+
readonly credentials: CredentialsProvider;
|
|
116
|
+
/** Logger */
|
|
117
|
+
readonly logger: LoggerAdapter;
|
|
118
|
+
/** Persistence adapter (optional) */
|
|
119
|
+
readonly persistence?: PersistenceAdapter;
|
|
120
|
+
/** Client version string (for API headers) */
|
|
121
|
+
getClientVersion(): string;
|
|
122
|
+
/**
|
|
123
|
+
* Initialize the adapter
|
|
124
|
+
* Called once when engine is created
|
|
125
|
+
*/
|
|
126
|
+
initialize(): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Cleanup/shutdown the adapter
|
|
129
|
+
* Called when engine is destroyed
|
|
130
|
+
*/
|
|
131
|
+
shutdown(): Promise<void>;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Factory function type for creating adapters
|
|
135
|
+
*/
|
|
136
|
+
export type AdapterFactory<TConfig = any> = (config: TConfig) => EngineAdapter;
|
|
137
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/adapters/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAClB,SAAS,EACT,cAAc,EACd,OAAO,EACP,aAAa,EACb,SAAS,EACT,WAAW,EACX,WAAW,EACZ,MAAM,mBAAmB,CAAC;AAM3B;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,yCAAyC;IACzC,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjD,6BAA6B;IAC7B,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,SAAS,GAAG,IAAI,CAAC;IAE1E,wBAAwB;IACxB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjD,2DAA2D;IAC3D,kBAAkB,CAChB,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,IAAI,CAAC;IAER,wDAAwD;IACxD,kBAAkB,CAChB,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,GAChB,IAAI,CAAC;IAER,iCAAiC;IACjC,cAAc,CACZ,QAAQ,EAAE,MAAM,EAChB,UAAU,E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CAAyC;IACzC,QAAQ,CAAC,MAAM,EAAE,kBAAkB,CAAC;IAEpC,8CAA8C;IAC9C,QAAQ,CAAC,aAAa,EAAE,mBAAmB,CAAC;IAE5C,4BAA4B;IAC5B,QAAQ,CAAC,GAAG,EAAE,mBAAmB,CAAC;IAElC,2BAA2B;IAC3B,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAC;IAE1C,aAAa;IACb,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAE/B,qCAAqC;IACrC,QAAQ,CAAC,WAAW,CAAC,EAAE,kBAAkB,CAAC;IAE1C,8CAA8C;IAC9C,gBAAgB,IAAI,MAAM,CAAC;IAE3B;;;OAGG;IACH,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B;;;OAGG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAMD;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,OAAO,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,OAAO,KAAK,aAAa,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @toolplex/ai-engine - Adapter Interface
|
|
3
|
+
*
|
|
4
|
+
* The EngineAdapter interface defines how the AI engine interacts with
|
|
5
|
+
* the host platform. This abstraction allows the same engine core to
|
|
6
|
+
* run in Electron, server-side (cloud), or CLI environments.
|
|
7
|
+
*
|
|
8
|
+
* Implementations:
|
|
9
|
+
* - ElectronAdapter: Desktop app with IPC, webContents, confirmations
|
|
10
|
+
* - HTTPAdapter: Cloud/server with HTTP streaming, no confirmations
|
|
11
|
+
* - CLIAdapter: CLI with terminal I/O
|
|
12
|
+
*/
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/adapters/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @toolplex/ai-engine - Chat Engine
|
|
3
|
+
*
|
|
4
|
+
* Core streaming engine that orchestrates AI chat sessions.
|
|
5
|
+
* Uses adapters for all platform-specific I/O operations.
|
|
6
|
+
*/
|
|
7
|
+
import type { EngineAdapter } from "../adapters/types.js";
|
|
8
|
+
import type { StreamOptions, StreamResult, EngineConfig } from "../types/index.js";
|
|
9
|
+
export interface ChatEngineOptions {
|
|
10
|
+
adapter: EngineAdapter;
|
|
11
|
+
config?: EngineConfig;
|
|
12
|
+
}
|
|
13
|
+
export declare class ChatEngine {
|
|
14
|
+
private adapter;
|
|
15
|
+
private config;
|
|
16
|
+
private initialized;
|
|
17
|
+
constructor(options: ChatEngineOptions);
|
|
18
|
+
/**
|
|
19
|
+
* Initialize the engine
|
|
20
|
+
*/
|
|
21
|
+
initialize(): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Shutdown the engine
|
|
24
|
+
*/
|
|
25
|
+
shutdown(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Initialize MCP for a session
|
|
28
|
+
*/
|
|
29
|
+
initializeMCP(sessionId: string): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Initialize a session with ToolPlex context
|
|
32
|
+
*/
|
|
33
|
+
initializeSession(sessionId: string, modelId: string, provider: string): Promise<{
|
|
34
|
+
success: boolean;
|
|
35
|
+
context?: string;
|
|
36
|
+
error?: string;
|
|
37
|
+
}>;
|
|
38
|
+
/**
|
|
39
|
+
* Stream a chat completion
|
|
40
|
+
*/
|
|
41
|
+
stream(options: StreamOptions): Promise<StreamResult>;
|
|
42
|
+
/**
|
|
43
|
+
* Process messages for streaming (handle attachments, filter empty blocks)
|
|
44
|
+
*/
|
|
45
|
+
private processMessages;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=ChatEngine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatEngine.d.ts","sourceRoot":"","sources":["../../src/core/ChatEngine.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,YAAY,EAGb,MAAM,mBAAmB,CAAC;AAI3B,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB;AAED,qBAAa,UAAU;IACrB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,WAAW,CAAkB;gBAEzB,OAAO,EAAE,iBAAiB;IAUtC;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAMjC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAM/B;;OAEG;IACG,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBrD;;OAEG;IACG,iBAAiB,CACrB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IA8DlE;;OAEG;IACG,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAkO3D;;OAEG;IACH,OAAO,CAAC,eAAe;CAmFxB"}
|