@cloudbase/agent-adapter-llm 0.0.22
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 +154 -0
- package/dist/index.d.mts +298 -0
- package/dist/index.d.ts +298 -0
- package/dist/index.js +705 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +673 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# @cloudbase/agent-adapter-llm
|
|
2
|
+
|
|
3
|
+
Direct LLM adapter for AG-Kit agents with support for OpenAI and Anthropic models.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Direct OpenAI and Anthropic model integration
|
|
8
|
+
- ✅ Automatic streaming message conversion to AGUI events
|
|
9
|
+
- ✅ Support for tool calls (function calling)
|
|
10
|
+
- ✅ Type-safe with TypeScript
|
|
11
|
+
- ✅ Observable-based event streaming
|
|
12
|
+
- ✅ No additional abstraction layers
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @cloudbase/agent-adapter-llm openai @anthropic-ai/sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### With OpenAI
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import OpenAI from 'openai';
|
|
26
|
+
import { LLMAgent } from '@cloudbase/agent-adapter-llm';
|
|
27
|
+
|
|
28
|
+
const openai = new OpenAI({
|
|
29
|
+
apiKey: process.env.OPENAI_API_KEY
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const agent = new LLMAgent({
|
|
33
|
+
agentId: 'my-agent',
|
|
34
|
+
model: openai,
|
|
35
|
+
modelName: 'gpt-4o-mini',
|
|
36
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
37
|
+
temperature: 0.7,
|
|
38
|
+
maxTokens: 2000
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const observable = agent.run({
|
|
42
|
+
messages: [
|
|
43
|
+
{ id: 'msg_1', role: 'user', content: 'Hello!' }
|
|
44
|
+
],
|
|
45
|
+
runId: 'run_123',
|
|
46
|
+
threadId: 'thread_456',
|
|
47
|
+
tools: []
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
observable.subscribe({
|
|
51
|
+
next: (event) => {
|
|
52
|
+
console.log('Event:', event);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### With Anthropic
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
61
|
+
import { LLMAgent } from '@cloudbase/agent-adapter-llm';
|
|
62
|
+
|
|
63
|
+
const anthropic = new Anthropic({
|
|
64
|
+
apiKey: process.env.ANTHROPIC_API_KEY
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const agent = new LLMAgent({
|
|
68
|
+
agentId: 'my-agent',
|
|
69
|
+
model: anthropic,
|
|
70
|
+
modelName: 'claude-3-5-sonnet-20241022',
|
|
71
|
+
systemPrompt: 'You are a helpful assistant.',
|
|
72
|
+
temperature: 0.7,
|
|
73
|
+
maxTokens: 2000
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## AGUI Events
|
|
78
|
+
|
|
79
|
+
The agent emits the following AGUI protocol events:
|
|
80
|
+
|
|
81
|
+
- `RUN_STARTED` - Run has started
|
|
82
|
+
- `TEXT_MESSAGE_START` - Text message streaming started
|
|
83
|
+
- `TEXT_MESSAGE_CONTENT` - Text content delta
|
|
84
|
+
- `TEXT_MESSAGE_END` - Text message streaming ended
|
|
85
|
+
- `TOOL_CALL_START` - Tool call started
|
|
86
|
+
- `TOOL_CALL_ARGS` - Tool call arguments delta
|
|
87
|
+
- `TOOL_CALL_END` - Tool call ended
|
|
88
|
+
- `RUN_FINISHED` - Run completed successfully
|
|
89
|
+
- `RUN_ERROR` - Run encountered an error
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
### LLMAgentConfig
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
interface LLMAgentConfig {
|
|
97
|
+
agentId: string; // Unique agent identifier
|
|
98
|
+
name?: string; // Agent name
|
|
99
|
+
description?: string; // Agent description
|
|
100
|
+
model: OpenAI | Anthropic; // Model provider instance
|
|
101
|
+
modelName: string; // Model name (e.g., 'gpt-4o-mini', 'claude-3-5-sonnet-20241022')
|
|
102
|
+
systemPrompt?: string; // System prompt
|
|
103
|
+
temperature?: number; // Temperature (0-2)
|
|
104
|
+
maxTokens?: number; // Max tokens to generate
|
|
105
|
+
threadId?: string; // Thread ID
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Streaming Example
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { EventType } from '@ag-ui/client';
|
|
113
|
+
|
|
114
|
+
observable.subscribe({
|
|
115
|
+
next: (event) => {
|
|
116
|
+
switch (event.type) {
|
|
117
|
+
case EventType.TEXT_MESSAGE_CONTENT:
|
|
118
|
+
process.stdout.write(event.delta);
|
|
119
|
+
break;
|
|
120
|
+
case EventType.TOOL_CALL_START:
|
|
121
|
+
console.log(`\nTool: ${event.toolCallName}`);
|
|
122
|
+
break;
|
|
123
|
+
case EventType.RUN_FINISHED:
|
|
124
|
+
console.log('\nDone!');
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Tool Calling
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const tools = [
|
|
135
|
+
{
|
|
136
|
+
name: 'get_weather',
|
|
137
|
+
description: 'Get the current weather',
|
|
138
|
+
parameters: JSON.stringify({
|
|
139
|
+
type: 'object',
|
|
140
|
+
properties: {
|
|
141
|
+
location: { type: 'string' }
|
|
142
|
+
},
|
|
143
|
+
required: ['location']
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
agent.run({ messages, runId, threadId, tools });
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|
|
154
|
+
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { AbstractAgent, RunAgentInput, BaseEvent, Message, Tool, EventType } from '@ag-ui/client';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import OpenAI from 'openai';
|
|
4
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* LLMAgent - Direct LLM integration with AGUI protocol
|
|
8
|
+
* Supports OpenAI and Anthropic models with streaming message conversion
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type ModelProvider = OpenAI | Anthropic;
|
|
12
|
+
interface LLMAgentConfig {
|
|
13
|
+
agentId?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
threadId?: string;
|
|
17
|
+
model: ModelProvider;
|
|
18
|
+
modelName: string;
|
|
19
|
+
systemPrompt?: string;
|
|
20
|
+
temperature?: number;
|
|
21
|
+
maxTokens?: number;
|
|
22
|
+
maxToolRounds?: number;
|
|
23
|
+
onToolCall?: (toolCall: {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
arguments: string;
|
|
27
|
+
}) => void;
|
|
28
|
+
onToolResult?: (result: {
|
|
29
|
+
toolCallId: string;
|
|
30
|
+
result: any;
|
|
31
|
+
error?: string;
|
|
32
|
+
}) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* LLMAgent - Direct model integration with AGUI protocol
|
|
36
|
+
*
|
|
37
|
+
* Example:
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import OpenAI from 'openai';
|
|
40
|
+
* import { LLMAgent } from '@cloudbase/agent-adapter-llm';
|
|
41
|
+
*
|
|
42
|
+
* const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
43
|
+
* const agent = new LLMAgent({
|
|
44
|
+
* agentId: 'my-agent',
|
|
45
|
+
* model: openai,
|
|
46
|
+
* modelName: 'gpt-4o-mini',
|
|
47
|
+
* systemPrompt: 'You are a helpful assistant.'
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare class LLMAgent extends AbstractAgent {
|
|
52
|
+
private model;
|
|
53
|
+
private modelType;
|
|
54
|
+
private modelName;
|
|
55
|
+
private systemPrompt?;
|
|
56
|
+
private temperature?;
|
|
57
|
+
private maxTokens?;
|
|
58
|
+
private maxToolRounds;
|
|
59
|
+
private onToolCall?;
|
|
60
|
+
private onToolResult?;
|
|
61
|
+
name: string;
|
|
62
|
+
constructor(config: LLMAgentConfig);
|
|
63
|
+
run(input: RunAgentInput): Observable<BaseEvent>;
|
|
64
|
+
private _run;
|
|
65
|
+
private runOpenAI;
|
|
66
|
+
private runAnthropic;
|
|
67
|
+
/**
|
|
68
|
+
* Run model once (single round, no tool execution)
|
|
69
|
+
*/
|
|
70
|
+
private runOnce;
|
|
71
|
+
/**
|
|
72
|
+
* Execute tools
|
|
73
|
+
*/
|
|
74
|
+
private executeTools;
|
|
75
|
+
/**
|
|
76
|
+
* Execute tool function
|
|
77
|
+
*/
|
|
78
|
+
private executeToolFunction;
|
|
79
|
+
/**
|
|
80
|
+
* Convert tool results to messages
|
|
81
|
+
*/
|
|
82
|
+
private toolResultsToMessages;
|
|
83
|
+
/**
|
|
84
|
+
* Extract text content from events
|
|
85
|
+
*/
|
|
86
|
+
private extractTextFromEvents;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* OpenAI Converter - AGUI Protocol to OpenAI Format
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Convert AGUI messages to OpenAI chat completion format
|
|
95
|
+
*/
|
|
96
|
+
declare function convertMessagesToOpenAI(messages: Message[], systemPrompt?: string): OpenAI.Chat.ChatCompletionMessageParam[];
|
|
97
|
+
/**
|
|
98
|
+
* Convert AGUI tools to OpenAI function calling format
|
|
99
|
+
*/
|
|
100
|
+
declare function convertToolsToOpenAI(tools?: Tool[]): OpenAI.Chat.ChatCompletionTool[] | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Create OpenAI streaming request options
|
|
103
|
+
*/
|
|
104
|
+
declare function createOpenAIStreamOptions(config: {
|
|
105
|
+
modelName: string;
|
|
106
|
+
messages: OpenAI.Chat.ChatCompletionMessageParam[];
|
|
107
|
+
tools?: OpenAI.Chat.ChatCompletionTool[];
|
|
108
|
+
temperature?: number;
|
|
109
|
+
maxTokens?: number;
|
|
110
|
+
user?: string;
|
|
111
|
+
}): OpenAI.Chat.ChatCompletionCreateParamsStreaming;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Anthropic Converter - AGUI Protocol to Anthropic Format
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Convert AGUI messages to Anthropic message format
|
|
119
|
+
*/
|
|
120
|
+
declare function convertMessagesToAnthropic(messages: Message[]): Anthropic.MessageParam[];
|
|
121
|
+
/**
|
|
122
|
+
* Convert AGUI tools to Anthropic tools format
|
|
123
|
+
*/
|
|
124
|
+
declare function convertToolsToAnthropic(tools?: Tool[]): Anthropic.Tool[] | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Create Anthropic streaming request options
|
|
127
|
+
*/
|
|
128
|
+
declare function createAnthropicStreamOptions(config: {
|
|
129
|
+
modelName: string;
|
|
130
|
+
messages: Anthropic.MessageParam[];
|
|
131
|
+
tools?: Anthropic.Tool[];
|
|
132
|
+
systemPrompt?: string;
|
|
133
|
+
temperature?: number;
|
|
134
|
+
maxTokens?: number;
|
|
135
|
+
}): Anthropic.MessageCreateParamsStreaming;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* OpenAI Stream Processor - Convert OpenAI streaming responses to AGUI events
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
interface StreamContext$1 {
|
|
142
|
+
threadId: string;
|
|
143
|
+
runId: string;
|
|
144
|
+
messageId: string;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Process OpenAI streaming chunk and emit AGUI events
|
|
148
|
+
*/
|
|
149
|
+
declare function processOpenAIStream(stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>, context: StreamContext$1): AsyncGenerator<{
|
|
150
|
+
type: EventType;
|
|
151
|
+
threadId: string;
|
|
152
|
+
runId: string;
|
|
153
|
+
toolCallId: any;
|
|
154
|
+
content?: undefined;
|
|
155
|
+
messageId?: undefined;
|
|
156
|
+
role?: undefined;
|
|
157
|
+
delta?: undefined;
|
|
158
|
+
toolCallName?: undefined;
|
|
159
|
+
} | {
|
|
160
|
+
type: EventType;
|
|
161
|
+
threadId: string;
|
|
162
|
+
runId: string;
|
|
163
|
+
toolCallId: any;
|
|
164
|
+
content: string;
|
|
165
|
+
messageId?: undefined;
|
|
166
|
+
role?: undefined;
|
|
167
|
+
delta?: undefined;
|
|
168
|
+
toolCallName?: undefined;
|
|
169
|
+
} | {
|
|
170
|
+
type: EventType;
|
|
171
|
+
threadId: string;
|
|
172
|
+
runId: string;
|
|
173
|
+
messageId: string;
|
|
174
|
+
toolCallId?: undefined;
|
|
175
|
+
content?: undefined;
|
|
176
|
+
role?: undefined;
|
|
177
|
+
delta?: undefined;
|
|
178
|
+
toolCallName?: undefined;
|
|
179
|
+
} | {
|
|
180
|
+
type: EventType;
|
|
181
|
+
threadId: string;
|
|
182
|
+
runId: string;
|
|
183
|
+
messageId: string;
|
|
184
|
+
role: string;
|
|
185
|
+
toolCallId?: undefined;
|
|
186
|
+
content?: undefined;
|
|
187
|
+
delta?: undefined;
|
|
188
|
+
toolCallName?: undefined;
|
|
189
|
+
} | {
|
|
190
|
+
type: EventType;
|
|
191
|
+
threadId: string;
|
|
192
|
+
runId: string;
|
|
193
|
+
messageId: string;
|
|
194
|
+
delta: string;
|
|
195
|
+
toolCallId?: undefined;
|
|
196
|
+
content?: undefined;
|
|
197
|
+
role?: undefined;
|
|
198
|
+
toolCallName?: undefined;
|
|
199
|
+
} | {
|
|
200
|
+
type: EventType;
|
|
201
|
+
threadId: string;
|
|
202
|
+
runId: string;
|
|
203
|
+
toolCallId: string;
|
|
204
|
+
toolCallName: string;
|
|
205
|
+
content?: undefined;
|
|
206
|
+
messageId?: undefined;
|
|
207
|
+
role?: undefined;
|
|
208
|
+
delta?: undefined;
|
|
209
|
+
} | {
|
|
210
|
+
type: EventType;
|
|
211
|
+
threadId: string;
|
|
212
|
+
runId: string;
|
|
213
|
+
toolCallId: string;
|
|
214
|
+
delta: string;
|
|
215
|
+
content?: undefined;
|
|
216
|
+
messageId?: undefined;
|
|
217
|
+
role?: undefined;
|
|
218
|
+
toolCallName?: undefined;
|
|
219
|
+
}, void, unknown>;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Anthropic Stream Processor - Convert Anthropic streaming responses to AGUI events
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
interface StreamContext {
|
|
226
|
+
threadId: string;
|
|
227
|
+
runId: string;
|
|
228
|
+
messageId: string;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Process Anthropic streaming events and emit AGUI events
|
|
232
|
+
*/
|
|
233
|
+
declare function processAnthropicStream(stream: AsyncIterable<Anthropic.MessageStreamEvent>, context: StreamContext): AsyncGenerator<{
|
|
234
|
+
type: EventType;
|
|
235
|
+
threadId: string;
|
|
236
|
+
runId: string;
|
|
237
|
+
messageId: string;
|
|
238
|
+
role: string;
|
|
239
|
+
toolCallId?: undefined;
|
|
240
|
+
toolCallName?: undefined;
|
|
241
|
+
delta?: undefined;
|
|
242
|
+
} | {
|
|
243
|
+
type: EventType;
|
|
244
|
+
threadId: string;
|
|
245
|
+
runId: string;
|
|
246
|
+
toolCallId: string;
|
|
247
|
+
toolCallName: string;
|
|
248
|
+
messageId?: undefined;
|
|
249
|
+
role?: undefined;
|
|
250
|
+
delta?: undefined;
|
|
251
|
+
} | {
|
|
252
|
+
type: EventType;
|
|
253
|
+
threadId: string;
|
|
254
|
+
runId: string;
|
|
255
|
+
messageId: string;
|
|
256
|
+
delta: string;
|
|
257
|
+
role?: undefined;
|
|
258
|
+
toolCallId?: undefined;
|
|
259
|
+
toolCallName?: undefined;
|
|
260
|
+
} | {
|
|
261
|
+
type: EventType;
|
|
262
|
+
threadId: string;
|
|
263
|
+
runId: string;
|
|
264
|
+
toolCallId: string;
|
|
265
|
+
delta: string;
|
|
266
|
+
messageId?: undefined;
|
|
267
|
+
role?: undefined;
|
|
268
|
+
toolCallName?: undefined;
|
|
269
|
+
} | {
|
|
270
|
+
type: EventType;
|
|
271
|
+
threadId: string;
|
|
272
|
+
runId: string;
|
|
273
|
+
toolCallId: string;
|
|
274
|
+
messageId?: undefined;
|
|
275
|
+
role?: undefined;
|
|
276
|
+
toolCallName?: undefined;
|
|
277
|
+
delta?: undefined;
|
|
278
|
+
} | {
|
|
279
|
+
type: EventType;
|
|
280
|
+
threadId: string;
|
|
281
|
+
runId: string;
|
|
282
|
+
messageId: string;
|
|
283
|
+
role?: undefined;
|
|
284
|
+
toolCallId?: undefined;
|
|
285
|
+
toolCallName?: undefined;
|
|
286
|
+
delta?: undefined;
|
|
287
|
+
} | {
|
|
288
|
+
type: EventType;
|
|
289
|
+
threadId: string;
|
|
290
|
+
runId: string;
|
|
291
|
+
messageId?: undefined;
|
|
292
|
+
role?: undefined;
|
|
293
|
+
toolCallId?: undefined;
|
|
294
|
+
toolCallName?: undefined;
|
|
295
|
+
delta?: undefined;
|
|
296
|
+
}, void, unknown>;
|
|
297
|
+
|
|
298
|
+
export { LLMAgent, type LLMAgentConfig, type ModelProvider, convertMessagesToAnthropic, convertMessagesToOpenAI, convertToolsToAnthropic, convertToolsToOpenAI, createAnthropicStreamOptions, createOpenAIStreamOptions, processAnthropicStream, processOpenAIStream };
|