@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/dist/index.d.ts
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 };
|