@radaros/core 0.3.4 → 0.3.6

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.
Files changed (60) hide show
  1. package/dist/index.d.ts +1407 -0
  2. package/dist/index.js +5269 -0
  3. package/package.json +6 -2
  4. package/src/a2a/a2a-remote-agent.ts +0 -270
  5. package/src/a2a/types.ts +0 -142
  6. package/src/agent/agent.ts +0 -417
  7. package/src/agent/llm-loop.ts +0 -290
  8. package/src/agent/run-context.ts +0 -35
  9. package/src/agent/types.ts +0 -89
  10. package/src/events/event-bus.ts +0 -45
  11. package/src/events/types.ts +0 -16
  12. package/src/guardrails/types.ts +0 -5
  13. package/src/hooks/types.ts +0 -6
  14. package/src/index.ts +0 -157
  15. package/src/knowledge/knowledge-base.ts +0 -146
  16. package/src/logger/logger.ts +0 -249
  17. package/src/mcp/mcp-client.ts +0 -264
  18. package/src/memory/memory.ts +0 -87
  19. package/src/memory/types.ts +0 -13
  20. package/src/memory/user-memory.ts +0 -211
  21. package/src/models/provider.ts +0 -22
  22. package/src/models/providers/anthropic.ts +0 -360
  23. package/src/models/providers/google.ts +0 -386
  24. package/src/models/providers/ollama.ts +0 -211
  25. package/src/models/providers/openai.ts +0 -345
  26. package/src/models/providers/vertex.ts +0 -427
  27. package/src/models/registry.ts +0 -107
  28. package/src/models/types.ts +0 -124
  29. package/src/session/session-manager.ts +0 -75
  30. package/src/session/types.ts +0 -10
  31. package/src/storage/driver.ts +0 -10
  32. package/src/storage/in-memory.ts +0 -44
  33. package/src/storage/mongodb.ts +0 -70
  34. package/src/storage/postgres.ts +0 -81
  35. package/src/storage/sqlite.ts +0 -81
  36. package/src/team/modes.ts +0 -1
  37. package/src/team/team.ts +0 -323
  38. package/src/team/types.ts +0 -26
  39. package/src/toolkits/base.ts +0 -15
  40. package/src/toolkits/duckduckgo.ts +0 -256
  41. package/src/toolkits/gmail.ts +0 -226
  42. package/src/toolkits/hackernews.ts +0 -121
  43. package/src/toolkits/websearch.ts +0 -158
  44. package/src/toolkits/whatsapp.ts +0 -209
  45. package/src/tools/define-tool.ts +0 -22
  46. package/src/tools/tool-executor.ts +0 -221
  47. package/src/tools/types.ts +0 -36
  48. package/src/utils/retry.ts +0 -56
  49. package/src/vector/base.ts +0 -44
  50. package/src/vector/embeddings/google.ts +0 -64
  51. package/src/vector/embeddings/openai.ts +0 -66
  52. package/src/vector/in-memory.ts +0 -115
  53. package/src/vector/mongodb.ts +0 -241
  54. package/src/vector/pgvector.ts +0 -169
  55. package/src/vector/qdrant.ts +0 -203
  56. package/src/vector/types.ts +0 -55
  57. package/src/workflow/step-runner.ts +0 -303
  58. package/src/workflow/types.ts +0 -55
  59. package/src/workflow/workflow.ts +0 -68
  60. package/tsconfig.json +0 -8
@@ -1,386 +0,0 @@
1
- import { createRequire } from "node:module";
2
- import type { ModelProvider } from "../provider.js";
3
- import {
4
- getTextContent,
5
- isMultiModal,
6
- type ChatMessage,
7
- type ContentPart,
8
- type ModelConfig,
9
- type ModelResponse,
10
- type StreamChunk,
11
- type ToolDefinition,
12
- type TokenUsage,
13
- type ToolCall,
14
- } from "../types.js";
15
-
16
- const _require = createRequire(import.meta.url);
17
-
18
- interface GoogleConfig {
19
- apiKey?: string;
20
- }
21
-
22
- export class GoogleProvider implements ModelProvider {
23
- readonly providerId = "google";
24
- readonly modelId: string;
25
- private ai: any;
26
- private GoogleGenAICtor: any;
27
- private clientCache = new Map<string, any>();
28
-
29
- constructor(modelId: string, config?: GoogleConfig) {
30
- this.modelId = modelId;
31
- try {
32
- const { GoogleGenAI } = _require("@google/genai");
33
- this.GoogleGenAICtor = GoogleGenAI;
34
- const key = config?.apiKey ?? process.env.GOOGLE_API_KEY;
35
- if (key) {
36
- this.ai = new GoogleGenAI({ apiKey: key });
37
- }
38
- } catch {
39
- throw new Error(
40
- "@google/genai is required for GoogleProvider. Install it: npm install @google/genai"
41
- );
42
- }
43
- }
44
-
45
- private getClient(apiKey?: string): any {
46
- if (apiKey) {
47
- let cached = this.clientCache.get(apiKey);
48
- if (!cached) {
49
- cached = new this.GoogleGenAICtor({ apiKey });
50
- this.clientCache.set(apiKey, cached);
51
- }
52
- return cached;
53
- }
54
- if (this.ai) return this.ai;
55
- const envKey = process.env.GOOGLE_API_KEY;
56
- if (envKey) {
57
- this.ai = new this.GoogleGenAICtor({ apiKey: envKey });
58
- return this.ai;
59
- }
60
- throw new Error("No Google API key provided. Pass it via the x-google-api-key header, apiKey in request body, or set GOOGLE_API_KEY env var.");
61
- }
62
-
63
- async generate(
64
- messages: ChatMessage[],
65
- options?: ModelConfig & { tools?: ToolDefinition[] }
66
- ): Promise<ModelResponse> {
67
- const { systemInstruction, contents } = this.toGoogleMessages(messages);
68
-
69
- const config: Record<string, unknown> = {};
70
- if (options?.temperature !== undefined)
71
- config.temperature = options.temperature;
72
- if (options?.maxTokens !== undefined)
73
- config.maxOutputTokens = options.maxTokens;
74
- if (options?.topP !== undefined) config.topP = options.topP;
75
- if (options?.stop) config.stopSequences = options.stop;
76
-
77
- if (options?.responseFormat) {
78
- config.responseMimeType = "application/json";
79
- const rf = options.responseFormat;
80
- if (typeof rf === "object" && rf !== null && "type" in rf && rf.type === "json_schema" && "schema" in rf && rf.schema) {
81
- config.responseSchema = this.cleanJsonSchema(rf.schema as Record<string, unknown>);
82
- }
83
- }
84
-
85
- if (options?.reasoning?.enabled) {
86
- config.thinkingConfig = {
87
- thinkingBudget: options.reasoning.budgetTokens ?? 10000,
88
- };
89
- }
90
-
91
- const params: Record<string, unknown> = {
92
- model: this.modelId,
93
- contents,
94
- config,
95
- };
96
-
97
- if (systemInstruction) params.systemInstruction = systemInstruction;
98
- if (options?.tools?.length) {
99
- params.tools = [
100
- {
101
- functionDeclarations: this.toGoogleTools(options.tools),
102
- },
103
- ];
104
- }
105
-
106
- const client = this.getClient(options?.apiKey);
107
- const response = await client.models.generateContent(params);
108
- return this.normalizeResponse(response);
109
- }
110
-
111
- async *stream(
112
- messages: ChatMessage[],
113
- options?: ModelConfig & { tools?: ToolDefinition[] }
114
- ): AsyncGenerator<StreamChunk> {
115
- const { systemInstruction, contents } = this.toGoogleMessages(messages);
116
-
117
- const config: Record<string, unknown> = {};
118
- if (options?.temperature !== undefined)
119
- config.temperature = options.temperature;
120
- if (options?.maxTokens !== undefined)
121
- config.maxOutputTokens = options.maxTokens;
122
- if (options?.topP !== undefined) config.topP = options.topP;
123
- if (options?.stop) config.stopSequences = options.stop;
124
-
125
- if (options?.reasoning?.enabled) {
126
- config.thinkingConfig = {
127
- thinkingBudget: options.reasoning.budgetTokens ?? 10000,
128
- };
129
- }
130
-
131
- const params: Record<string, unknown> = {
132
- model: this.modelId,
133
- contents,
134
- config,
135
- };
136
-
137
- if (systemInstruction) params.systemInstruction = systemInstruction;
138
- if (options?.tools?.length) {
139
- params.tools = [
140
- {
141
- functionDeclarations: this.toGoogleTools(options.tools),
142
- },
143
- ];
144
- }
145
-
146
- const client = this.getClient(options?.apiKey);
147
- const streamResult =
148
- await client.models.generateContentStream(params);
149
-
150
- let toolCallCounter = 0;
151
-
152
- for await (const chunk of streamResult) {
153
- const candidate = chunk.candidates?.[0];
154
- if (!candidate?.content?.parts) continue;
155
-
156
- for (const part of candidate.content.parts) {
157
- if (part.thought) {
158
- yield { type: "thinking", text: part.text ?? "" };
159
- } else if (part.text) {
160
- yield { type: "text", text: part.text };
161
- }
162
-
163
- if (part.functionCall) {
164
- const id = `google_tc_${toolCallCounter++}`;
165
- yield {
166
- type: "tool_call_start",
167
- toolCall: { id, name: part.functionCall.name },
168
- };
169
- yield {
170
- type: "tool_call_delta",
171
- toolCallId: id,
172
- argumentsDelta: JSON.stringify(part.functionCall.args ?? {}),
173
- };
174
- yield { type: "tool_call_end", toolCallId: id };
175
- }
176
- }
177
-
178
- if (candidate.finishReason) {
179
- let finishReason = "stop";
180
- if (
181
- candidate.finishReason === "STOP" ||
182
- candidate.finishReason === "END_TURN"
183
- ) {
184
- finishReason = "stop";
185
- } else if (candidate.finishReason === "MAX_TOKENS") {
186
- finishReason = "length";
187
- } else if (candidate.finishReason === "SAFETY") {
188
- finishReason = "content_filter";
189
- }
190
-
191
- const hasToolCalls = candidate.content?.parts?.some(
192
- (p: any) => p.functionCall
193
- );
194
- if (hasToolCalls) finishReason = "tool_calls";
195
-
196
- yield {
197
- type: "finish",
198
- finishReason,
199
- usage: chunk.usageMetadata
200
- ? {
201
- promptTokens: chunk.usageMetadata.promptTokenCount ?? 0,
202
- completionTokens:
203
- chunk.usageMetadata.candidatesTokenCount ?? 0,
204
- totalTokens: chunk.usageMetadata.totalTokenCount ?? 0,
205
- }
206
- : undefined,
207
- };
208
- }
209
- }
210
- }
211
-
212
- private toGoogleMessages(messages: ChatMessage[]): {
213
- systemInstruction: string | undefined;
214
- contents: unknown[];
215
- } {
216
- let systemInstruction: string | undefined;
217
- const contents: unknown[] = [];
218
-
219
- for (const msg of messages) {
220
- if (msg.role === "system") {
221
- systemInstruction = getTextContent(msg.content) || undefined;
222
- continue;
223
- }
224
-
225
- if (msg.role === "user") {
226
- if (isMultiModal(msg.content)) {
227
- contents.push({
228
- role: "user",
229
- parts: msg.content.map((p) => this.partToGoogle(p)),
230
- });
231
- } else {
232
- contents.push({
233
- role: "user",
234
- parts: [{ text: msg.content ?? "" }],
235
- });
236
- }
237
- continue;
238
- }
239
-
240
- if (msg.role === "assistant") {
241
- const parts: unknown[] = [];
242
- if (msg.content) {
243
- parts.push({ text: msg.content });
244
- }
245
- if (msg.toolCalls) {
246
- for (const tc of msg.toolCalls) {
247
- parts.push({
248
- functionCall: { name: tc.name, args: tc.arguments },
249
- });
250
- }
251
- }
252
- if (parts.length === 0) {
253
- parts.push({ text: "" });
254
- }
255
- contents.push({ role: "model", parts });
256
- continue;
257
- }
258
-
259
- if (msg.role === "tool") {
260
- contents.push({
261
- role: "function",
262
- parts: [
263
- {
264
- functionResponse: {
265
- name: msg.name ?? "unknown",
266
- response: { result: msg.content ?? "" },
267
- },
268
- },
269
- ],
270
- });
271
- continue;
272
- }
273
- }
274
-
275
- return { systemInstruction, contents };
276
- }
277
-
278
- private partToGoogle(part: ContentPart): unknown {
279
- switch (part.type) {
280
- case "text":
281
- return { text: part.text };
282
- case "image":
283
- case "audio":
284
- case "file": {
285
- const isUrl = part.data.startsWith("http://") || part.data.startsWith("https://");
286
- if (isUrl) {
287
- return { fileData: { fileUri: part.data, mimeType: part.mimeType ?? (part.type === "image" ? "image/png" : "application/octet-stream") } };
288
- }
289
- return {
290
- inlineData: {
291
- data: part.data,
292
- mimeType: part.mimeType ?? (part.type === "image" ? "image/png" : part.type === "audio" ? "audio/mp3" : "application/octet-stream"),
293
- },
294
- };
295
- }
296
- }
297
- }
298
-
299
- private toGoogleTools(
300
- tools: ToolDefinition[]
301
- ): unknown[] {
302
- return tools.map((t) => ({
303
- name: t.name,
304
- description: t.description,
305
- parameters: t.parameters,
306
- }));
307
- }
308
-
309
- private cleanJsonSchema(schema: Record<string, unknown>): Record<string, unknown> {
310
- const cleaned = { ...schema };
311
- delete cleaned["$schema"];
312
- delete cleaned["$ref"];
313
- delete cleaned["additionalProperties"];
314
-
315
- if (cleaned.properties && typeof cleaned.properties === "object") {
316
- const props: Record<string, unknown> = {};
317
- for (const [key, val] of Object.entries(cleaned.properties as Record<string, unknown>)) {
318
- props[key] = typeof val === "object" && val ? this.cleanJsonSchema(val as Record<string, unknown>) : val;
319
- }
320
- cleaned.properties = props;
321
- }
322
-
323
- if (cleaned.items && typeof cleaned.items === "object") {
324
- cleaned.items = this.cleanJsonSchema(cleaned.items as Record<string, unknown>);
325
- }
326
-
327
- return cleaned;
328
- }
329
-
330
- private normalizeResponse(response: any): ModelResponse & { thinking?: string } {
331
- const candidate = response.candidates?.[0];
332
- const parts = candidate?.content?.parts ?? [];
333
-
334
- let textContent = "";
335
- let thinkingContent = "";
336
- const toolCalls: ToolCall[] = [];
337
- let toolCallCounter = 0;
338
-
339
- for (const part of parts) {
340
- if (part.thought && part.text) {
341
- thinkingContent += part.text;
342
- } else if (part.text) {
343
- textContent += part.text;
344
- }
345
- if (part.functionCall) {
346
- toolCalls.push({
347
- id: `google_tc_${toolCallCounter++}`,
348
- name: part.functionCall.name,
349
- arguments: part.functionCall.args ?? {},
350
- });
351
- }
352
- }
353
-
354
- const thinkingTokens = response.usageMetadata?.thoughtsTokenCount ?? 0;
355
- const usage: TokenUsage = {
356
- promptTokens: response.usageMetadata?.promptTokenCount ?? 0,
357
- completionTokens: response.usageMetadata?.candidatesTokenCount ?? 0,
358
- totalTokens: response.usageMetadata?.totalTokenCount ?? 0,
359
- ...(thinkingTokens > 0 ? { reasoningTokens: thinkingTokens } : {}),
360
- };
361
-
362
- let finishReason: ModelResponse["finishReason"] = "stop";
363
- if (toolCalls.length > 0) finishReason = "tool_calls";
364
- else if (candidate?.finishReason === "MAX_TOKENS")
365
- finishReason = "length";
366
- else if (candidate?.finishReason === "SAFETY")
367
- finishReason = "content_filter";
368
-
369
- const result: ModelResponse & { thinking?: string } = {
370
- message: {
371
- role: "assistant",
372
- content: textContent || null,
373
- toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
374
- },
375
- usage,
376
- finishReason,
377
- raw: response,
378
- };
379
-
380
- if (thinkingContent) {
381
- result.thinking = thinkingContent;
382
- }
383
-
384
- return result;
385
- }
386
- }
@@ -1,211 +0,0 @@
1
- import { createRequire } from "node:module";
2
- import type { ModelProvider } from "../provider.js";
3
- import type {
4
- ChatMessage,
5
- ModelConfig,
6
- ModelResponse,
7
- StreamChunk,
8
- ToolDefinition,
9
- TokenUsage,
10
- ToolCall,
11
- } from "../types.js";
12
-
13
- const _require = createRequire(import.meta.url);
14
-
15
- interface OllamaConfig {
16
- host?: string;
17
- }
18
-
19
- export class OllamaProvider implements ModelProvider {
20
- readonly providerId = "ollama";
21
- readonly modelId: string;
22
- private client: any;
23
-
24
- constructor(modelId: string, config?: OllamaConfig) {
25
- this.modelId = modelId;
26
- try {
27
- const { Ollama } = _require("ollama");
28
- this.client = new Ollama({
29
- host: config?.host ?? "http://localhost:11434",
30
- });
31
- } catch {
32
- throw new Error(
33
- "ollama package is required for OllamaProvider. Install it: npm install ollama"
34
- );
35
- }
36
- }
37
-
38
- async generate(
39
- messages: ChatMessage[],
40
- options?: ModelConfig & { tools?: ToolDefinition[] }
41
- ): Promise<ModelResponse> {
42
- const params: Record<string, unknown> = {
43
- model: this.modelId,
44
- messages: this.toOllamaMessages(messages),
45
- stream: false,
46
- };
47
-
48
- const ollamaOptions: Record<string, unknown> = {};
49
- if (options?.temperature !== undefined)
50
- ollamaOptions.temperature = options.temperature;
51
- if (options?.topP !== undefined) ollamaOptions.top_p = options.topP;
52
- if (options?.stop) ollamaOptions.stop = options.stop;
53
- if (options?.maxTokens !== undefined)
54
- ollamaOptions.num_predict = options.maxTokens;
55
-
56
- if (Object.keys(ollamaOptions).length > 0) params.options = ollamaOptions;
57
-
58
- if (options?.tools?.length) {
59
- params.tools = this.toOllamaTools(options.tools);
60
- }
61
-
62
- if (options?.responseFormat === "json") {
63
- params.format = "json";
64
- }
65
-
66
- const response = await this.client.chat(params);
67
- return this.normalizeResponse(response);
68
- }
69
-
70
- async *stream(
71
- messages: ChatMessage[],
72
- options?: ModelConfig & { tools?: ToolDefinition[] }
73
- ): AsyncGenerator<StreamChunk> {
74
- const params: Record<string, unknown> = {
75
- model: this.modelId,
76
- messages: this.toOllamaMessages(messages),
77
- stream: true,
78
- };
79
-
80
- const ollamaOptions: Record<string, unknown> = {};
81
- if (options?.temperature !== undefined)
82
- ollamaOptions.temperature = options.temperature;
83
- if (options?.topP !== undefined) ollamaOptions.top_p = options.topP;
84
- if (options?.stop) ollamaOptions.stop = options.stop;
85
- if (options?.maxTokens !== undefined)
86
- ollamaOptions.num_predict = options.maxTokens;
87
-
88
- if (Object.keys(ollamaOptions).length > 0) params.options = ollamaOptions;
89
-
90
- if (options?.tools?.length) {
91
- params.tools = this.toOllamaTools(options.tools);
92
- }
93
-
94
- if (options?.responseFormat === "json") {
95
- params.format = "json";
96
- }
97
-
98
- const stream = await this.client.chat(params);
99
-
100
- let toolCallCounter = 0;
101
-
102
- for await (const chunk of stream) {
103
- if (chunk.message?.content) {
104
- yield { type: "text", text: chunk.message.content };
105
- }
106
-
107
- if (chunk.message?.tool_calls) {
108
- for (const tc of chunk.message.tool_calls) {
109
- const id = `ollama_tc_${toolCallCounter++}`;
110
- yield {
111
- type: "tool_call_start",
112
- toolCall: {
113
- id,
114
- name: tc.function?.name ?? "",
115
- },
116
- };
117
- yield {
118
- type: "tool_call_delta",
119
- toolCallId: id,
120
- argumentsDelta: JSON.stringify(tc.function?.arguments ?? {}),
121
- };
122
- yield { type: "tool_call_end", toolCallId: id };
123
- }
124
- }
125
-
126
- if (chunk.done) {
127
- const hasToolCalls = chunk.message?.tool_calls?.length > 0;
128
- yield {
129
- type: "finish",
130
- finishReason: hasToolCalls ? "tool_calls" : "stop",
131
- usage: {
132
- promptTokens: chunk.prompt_eval_count ?? 0,
133
- completionTokens: chunk.eval_count ?? 0,
134
- totalTokens:
135
- (chunk.prompt_eval_count ?? 0) + (chunk.eval_count ?? 0),
136
- },
137
- };
138
- }
139
- }
140
- }
141
-
142
- private toOllamaMessages(messages: ChatMessage[]): unknown[] {
143
- return messages.map((msg) => {
144
- if (msg.role === "assistant" && msg.toolCalls?.length) {
145
- return {
146
- role: "assistant",
147
- content: msg.content ?? "",
148
- tool_calls: msg.toolCalls.map((tc) => ({
149
- function: {
150
- name: tc.name,
151
- arguments: tc.arguments,
152
- },
153
- })),
154
- };
155
- }
156
-
157
- if (msg.role === "tool") {
158
- return {
159
- role: "tool",
160
- content: msg.content ?? "",
161
- };
162
- }
163
-
164
- return {
165
- role: msg.role,
166
- content: msg.content ?? "",
167
- };
168
- });
169
- }
170
-
171
- private toOllamaTools(tools: ToolDefinition[]): unknown[] {
172
- return tools.map((t) => ({
173
- type: "function",
174
- function: {
175
- name: t.name,
176
- description: t.description,
177
- parameters: t.parameters,
178
- },
179
- }));
180
- }
181
-
182
- private normalizeResponse(response: any): ModelResponse {
183
- const toolCalls: ToolCall[] = (response.message?.tool_calls ?? []).map(
184
- (tc: any, i: number) => ({
185
- id: `ollama_tc_${i}`,
186
- name: tc.function?.name ?? "",
187
- arguments: tc.function?.arguments ?? {},
188
- })
189
- );
190
-
191
- const usage: TokenUsage = {
192
- promptTokens: response.prompt_eval_count ?? 0,
193
- completionTokens: response.eval_count ?? 0,
194
- totalTokens:
195
- (response.prompt_eval_count ?? 0) + (response.eval_count ?? 0),
196
- };
197
-
198
- const hasToolCalls = toolCalls.length > 0;
199
-
200
- return {
201
- message: {
202
- role: "assistant",
203
- content: response.message?.content ?? null,
204
- toolCalls: hasToolCalls ? toolCalls : undefined,
205
- },
206
- usage,
207
- finishReason: hasToolCalls ? "tool_calls" : "stop",
208
- raw: response,
209
- };
210
- }
211
- }