@iqai/adk 0.0.6 → 0.0.8
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/CHANGELOG.md +4 -4
- package/README.md +177 -0
- package/dist/index.d.mts +69 -5
- package/dist/index.d.ts +69 -5
- package/dist/index.js +585 -365
- package/dist/index.mjs +401 -181
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -19,27 +19,47 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
19
|
};
|
|
20
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
21
|
|
|
22
|
-
// src/helpers/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
// src/helpers/logger.ts
|
|
23
|
+
function isDebugEnabled() {
|
|
24
|
+
return process.env.NODE_ENV === "development" || process.env.DEBUG === "true";
|
|
25
|
+
}
|
|
26
|
+
var Logger;
|
|
27
|
+
var init_logger = __esm({
|
|
28
|
+
"src/helpers/logger.ts"() {
|
|
29
|
+
Logger = class {
|
|
30
|
+
name;
|
|
31
|
+
isDebugEnabled = isDebugEnabled();
|
|
32
|
+
constructor({ name }) {
|
|
33
|
+
this.name = name;
|
|
34
|
+
}
|
|
35
|
+
debug(message, ...args) {
|
|
36
|
+
const time = (/* @__PURE__ */ new Date()).toISOString();
|
|
37
|
+
if (this.isDebugEnabled) {
|
|
38
|
+
console.log(`[${time}] \u{1F41B} [DEBUG] \u2728 [${this.name}] ${message}`, ...args);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
info(message, ...args) {
|
|
42
|
+
const time = (/* @__PURE__ */ new Date()).toISOString();
|
|
43
|
+
console.info(`[${time}] \u2139\uFE0F [INFO] \u2728 [${this.name}] ${message}`, ...args);
|
|
44
|
+
}
|
|
45
|
+
warn(message, ...args) {
|
|
46
|
+
const time = (/* @__PURE__ */ new Date()).toISOString();
|
|
47
|
+
console.warn(`[${time}] \u{1F6A7} [WARN] \u2728 [${this.name}] ${message}`, ...args);
|
|
48
|
+
}
|
|
49
|
+
error(message, ...args) {
|
|
50
|
+
const time = (/* @__PURE__ */ new Date()).toISOString();
|
|
51
|
+
console.error(`[${time}] \u274C [ERROR] \u2728 [${this.name}] ${message}`, ...args);
|
|
33
52
|
}
|
|
34
53
|
};
|
|
35
54
|
}
|
|
36
55
|
});
|
|
37
56
|
|
|
38
57
|
// src/tools/base/base-tool.ts
|
|
39
|
-
var BaseTool;
|
|
58
|
+
var logger2, BaseTool;
|
|
40
59
|
var init_base_tool = __esm({
|
|
41
60
|
"src/tools/base/base-tool.ts"() {
|
|
42
|
-
|
|
61
|
+
init_logger();
|
|
62
|
+
logger2 = new Logger({ name: "BaseTool" });
|
|
43
63
|
BaseTool = class {
|
|
44
64
|
/**
|
|
45
65
|
* Name of the tool
|
|
@@ -129,8 +149,8 @@ var init_base_tool = __esm({
|
|
|
129
149
|
while (attempts <= (this.shouldRetryOnFailure ? this.maxRetryAttempts : 0)) {
|
|
130
150
|
try {
|
|
131
151
|
if (attempts > 0) {
|
|
132
|
-
|
|
133
|
-
`
|
|
152
|
+
logger2.debug(
|
|
153
|
+
`Retrying tool ${this.name} (attempt ${attempts} of ${this.maxRetryAttempts})...`
|
|
134
154
|
);
|
|
135
155
|
const delay = Math.min(
|
|
136
156
|
this.baseRetryDelay * 2 ** (attempts - 1) + Math.random() * 1e3,
|
|
@@ -475,10 +495,11 @@ var BaseAgent = class {
|
|
|
475
495
|
};
|
|
476
496
|
|
|
477
497
|
// src/agents/llm-agent.ts
|
|
478
|
-
|
|
498
|
+
init_logger();
|
|
479
499
|
|
|
480
500
|
// src/models/llm-registry.ts
|
|
481
|
-
|
|
501
|
+
init_logger();
|
|
502
|
+
var logger = new Logger({ name: "LLMRegistry" });
|
|
482
503
|
var LLMRegistry = class _LLMRegistry {
|
|
483
504
|
/**
|
|
484
505
|
* Map of model name regex to LLM class
|
|
@@ -535,7 +556,7 @@ var LLMRegistry = class _LLMRegistry {
|
|
|
535
556
|
* Logs all registered models for debugging
|
|
536
557
|
*/
|
|
537
558
|
static logRegisteredModels() {
|
|
538
|
-
|
|
559
|
+
logger.debug(
|
|
539
560
|
"Registered LLM models:",
|
|
540
561
|
[..._LLMRegistry.llmRegistry.entries()].map(([regex]) => regex.toString())
|
|
541
562
|
);
|
|
@@ -904,6 +925,7 @@ var Agent = class extends BaseAgent {
|
|
|
904
925
|
* The minimum relevance score for memory augmentation (0-1)
|
|
905
926
|
*/
|
|
906
927
|
memoryRelevanceThreshold;
|
|
928
|
+
logger = new Logger({ name: "LlmAgent" });
|
|
907
929
|
/**
|
|
908
930
|
* Constructor for Agent
|
|
909
931
|
*/
|
|
@@ -936,7 +958,7 @@ var Agent = class extends BaseAgent {
|
|
|
936
958
|
*/
|
|
937
959
|
async executeTool(toolCall, context) {
|
|
938
960
|
const { name, arguments: argsString } = toolCall.function;
|
|
939
|
-
|
|
961
|
+
this.logger.debug(`Executing tool: ${name}`);
|
|
940
962
|
const tool = this.findTool(name);
|
|
941
963
|
if (!tool) {
|
|
942
964
|
console.warn(`Tool '${name}' not found`);
|
|
@@ -954,7 +976,7 @@ var Agent = class extends BaseAgent {
|
|
|
954
976
|
toolContext.toolName = name;
|
|
955
977
|
toolContext.toolId = toolCall.id;
|
|
956
978
|
const result = await tool.runAsync(args, toolContext);
|
|
957
|
-
|
|
979
|
+
this.logger.debug(`Tool ${name} execution complete`);
|
|
958
980
|
return {
|
|
959
981
|
name,
|
|
960
982
|
result: typeof result === "string" ? result : JSON.stringify(result)
|
|
@@ -1123,7 +1145,7 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1123
1145
|
let stepCount = 0;
|
|
1124
1146
|
while (stepCount < this.maxToolExecutionSteps) {
|
|
1125
1147
|
stepCount++;
|
|
1126
|
-
|
|
1148
|
+
this.logger.debug(`Step ${stepCount}: Thinking...`);
|
|
1127
1149
|
const llmRequest = new LLMRequest({
|
|
1128
1150
|
messages: context.messages,
|
|
1129
1151
|
config: {
|
|
@@ -1140,7 +1162,9 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1140
1162
|
throw new Error("No response from LLM");
|
|
1141
1163
|
}
|
|
1142
1164
|
if (currentResponse.tool_calls && currentResponse.tool_calls.length > 0) {
|
|
1143
|
-
|
|
1165
|
+
this.logger.debug(
|
|
1166
|
+
`Tool calls: ${JSON.stringify(currentResponse.tool_calls)}`
|
|
1167
|
+
);
|
|
1144
1168
|
context.addMessage({
|
|
1145
1169
|
role: "assistant",
|
|
1146
1170
|
content: currentResponse.content || "",
|
|
@@ -1159,7 +1183,7 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1159
1183
|
});
|
|
1160
1184
|
}
|
|
1161
1185
|
} else {
|
|
1162
|
-
|
|
1186
|
+
this.logger.debug("No tool calls, finishing...");
|
|
1163
1187
|
context.addMessage({
|
|
1164
1188
|
role: "assistant",
|
|
1165
1189
|
content: currentResponse.content || ""
|
|
@@ -1197,7 +1221,7 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1197
1221
|
let stepCount = 0;
|
|
1198
1222
|
let hadToolCalls = false;
|
|
1199
1223
|
while (stepCount < this.maxToolExecutionSteps) {
|
|
1200
|
-
|
|
1224
|
+
this.logger.debug(`Step ${stepCount}: Thinking...`);
|
|
1201
1225
|
const toolDeclarations = this.tools.map((tool) => tool.getDeclaration()).filter((declaration) => declaration !== null);
|
|
1202
1226
|
const request = {
|
|
1203
1227
|
messages: context.messages,
|
|
@@ -1226,10 +1250,10 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1226
1250
|
function_call: finalResponse.function_call
|
|
1227
1251
|
});
|
|
1228
1252
|
if (!hadToolCalls) {
|
|
1229
|
-
|
|
1253
|
+
this.logger.debug("No tool calls, finishing...");
|
|
1230
1254
|
break;
|
|
1231
1255
|
}
|
|
1232
|
-
|
|
1256
|
+
this.logger.debug(`Step ${stepCount + 1}: Executing tools...`);
|
|
1233
1257
|
stepCount++;
|
|
1234
1258
|
if (finalResponse.function_call) {
|
|
1235
1259
|
const toolCall = {
|
|
@@ -1246,8 +1270,8 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1246
1270
|
content: JSON.stringify(result.result)
|
|
1247
1271
|
});
|
|
1248
1272
|
} else if (finalResponse.tool_calls && finalResponse.tool_calls.length > 0) {
|
|
1249
|
-
|
|
1250
|
-
`
|
|
1273
|
+
this.logger.debug(
|
|
1274
|
+
`Step ${stepCount + 1}: Executing ${finalResponse.tool_calls.length} tool(s)...`
|
|
1251
1275
|
);
|
|
1252
1276
|
context.messages.pop();
|
|
1253
1277
|
context.addMessage({
|
|
@@ -1273,8 +1297,9 @@ ${relevantInfo.join("\n\n")}`
|
|
|
1273
1297
|
};
|
|
1274
1298
|
|
|
1275
1299
|
// src/agents/sequential-agent.ts
|
|
1276
|
-
|
|
1300
|
+
init_logger();
|
|
1277
1301
|
var SequentialAgent = class extends BaseAgent {
|
|
1302
|
+
logger = new Logger({ name: "SequentialAgent" });
|
|
1278
1303
|
/**
|
|
1279
1304
|
* Constructor for SequentialAgent
|
|
1280
1305
|
*/
|
|
@@ -1294,8 +1319,8 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1294
1319
|
* Executes sub-agents sequentially, passing output from one to the next
|
|
1295
1320
|
*/
|
|
1296
1321
|
async run(options) {
|
|
1297
|
-
|
|
1298
|
-
`
|
|
1322
|
+
this.logger.debug(
|
|
1323
|
+
`Running ${this.subAgents.length} sub-agents in sequence`
|
|
1299
1324
|
);
|
|
1300
1325
|
if (this.subAgents.length === 0) {
|
|
1301
1326
|
return {
|
|
@@ -1312,8 +1337,8 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1312
1337
|
let finalResponse = null;
|
|
1313
1338
|
for (let i = 0; i < this.subAgents.length; i++) {
|
|
1314
1339
|
const agent = this.subAgents[i];
|
|
1315
|
-
|
|
1316
|
-
`
|
|
1340
|
+
this.logger.debug(
|
|
1341
|
+
`Running sub-agent ${i + 1}/${this.subAgents.length}: ${agent.name}`
|
|
1317
1342
|
);
|
|
1318
1343
|
try {
|
|
1319
1344
|
const response = await agent.run({
|
|
@@ -1329,10 +1354,7 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1329
1354
|
});
|
|
1330
1355
|
}
|
|
1331
1356
|
} catch (error) {
|
|
1332
|
-
console.error(
|
|
1333
|
-
`[SequentialAgent] Error in sub-agent ${agent.name}:`,
|
|
1334
|
-
error
|
|
1335
|
-
);
|
|
1357
|
+
console.error(`Error in sub-agent ${agent.name}:`, error);
|
|
1336
1358
|
return {
|
|
1337
1359
|
content: `Error in sub-agent ${agent.name}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1338
1360
|
role: "assistant",
|
|
@@ -1370,8 +1392,8 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1370
1392
|
* Streams responses from each sub-agent in sequence
|
|
1371
1393
|
*/
|
|
1372
1394
|
async *runStreaming(options) {
|
|
1373
|
-
|
|
1374
|
-
`
|
|
1395
|
+
this.logger.debug(
|
|
1396
|
+
`Streaming ${this.subAgents.length} sub-agents in sequence`
|
|
1375
1397
|
);
|
|
1376
1398
|
if (this.subAgents.length === 0) {
|
|
1377
1399
|
yield {
|
|
@@ -1388,8 +1410,8 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1388
1410
|
const currentMessages = [...options.messages];
|
|
1389
1411
|
for (let i = 0; i < this.subAgents.length; i++) {
|
|
1390
1412
|
const agent = this.subAgents[i];
|
|
1391
|
-
|
|
1392
|
-
`
|
|
1413
|
+
this.logger.debug(
|
|
1414
|
+
`Streaming sub-agent ${i + 1}/${this.subAgents.length}: ${agent.name}`
|
|
1393
1415
|
);
|
|
1394
1416
|
try {
|
|
1395
1417
|
const streamGenerator = agent.runStreaming({
|
|
@@ -1422,10 +1444,7 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1422
1444
|
});
|
|
1423
1445
|
}
|
|
1424
1446
|
} catch (error) {
|
|
1425
|
-
console.error(
|
|
1426
|
-
`[SequentialAgent] Error in streaming sub-agent ${agent.name}:`,
|
|
1427
|
-
error
|
|
1428
|
-
);
|
|
1447
|
+
console.error(`Error in streaming sub-agent ${agent.name}:`, error);
|
|
1429
1448
|
yield {
|
|
1430
1449
|
content: `Error in sub-agent ${agent.name}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1431
1450
|
role: "assistant",
|
|
@@ -1443,8 +1462,9 @@ var SequentialAgent = class extends BaseAgent {
|
|
|
1443
1462
|
};
|
|
1444
1463
|
|
|
1445
1464
|
// src/agents/parallel-agent.ts
|
|
1446
|
-
|
|
1465
|
+
init_logger();
|
|
1447
1466
|
var ParallelAgent = class extends BaseAgent {
|
|
1467
|
+
logger = new Logger({ name: "ParallelAgent" });
|
|
1448
1468
|
/**
|
|
1449
1469
|
* Constructor for ParallelAgent
|
|
1450
1470
|
*/
|
|
@@ -1464,8 +1484,8 @@ var ParallelAgent = class extends BaseAgent {
|
|
|
1464
1484
|
* Executes all sub-agents in parallel
|
|
1465
1485
|
*/
|
|
1466
1486
|
async run(options) {
|
|
1467
|
-
|
|
1468
|
-
`
|
|
1487
|
+
this.logger.debug(
|
|
1488
|
+
`Running ${this.subAgents.length} sub-agents in parallel`
|
|
1469
1489
|
);
|
|
1470
1490
|
if (this.subAgents.length === 0) {
|
|
1471
1491
|
return {
|
|
@@ -1478,10 +1498,7 @@ var ParallelAgent = class extends BaseAgent {
|
|
|
1478
1498
|
messages: options.messages,
|
|
1479
1499
|
config: options.config
|
|
1480
1500
|
}).catch((error) => {
|
|
1481
|
-
console.error(
|
|
1482
|
-
`[ParallelAgent] Error in sub-agent ${agent.name}:`,
|
|
1483
|
-
error
|
|
1484
|
-
);
|
|
1501
|
+
console.error(`Error in sub-agent ${agent.name}:`, error);
|
|
1485
1502
|
return {
|
|
1486
1503
|
content: `Error in sub-agent ${agent.name}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1487
1504
|
role: "assistant"
|
|
@@ -1509,8 +1526,8 @@ ${result.content || "No content"}
|
|
|
1509
1526
|
* Collects streaming responses from all sub-agents
|
|
1510
1527
|
*/
|
|
1511
1528
|
async *runStreaming(options) {
|
|
1512
|
-
|
|
1513
|
-
`
|
|
1529
|
+
this.logger.debug(
|
|
1530
|
+
`Streaming ${this.subAgents.length} sub-agents in parallel`
|
|
1514
1531
|
);
|
|
1515
1532
|
if (this.subAgents.length === 0) {
|
|
1516
1533
|
yield {
|
|
@@ -1524,10 +1541,7 @@ ${result.content || "No content"}
|
|
|
1524
1541
|
messages: options.messages,
|
|
1525
1542
|
config: options.config
|
|
1526
1543
|
}).catch((error) => {
|
|
1527
|
-
console.error(
|
|
1528
|
-
`[ParallelAgent] Error in sub-agent ${agent.name}:`,
|
|
1529
|
-
error
|
|
1530
|
-
);
|
|
1544
|
+
console.error(`Error in sub-agent ${agent.name}:`, error);
|
|
1531
1545
|
return {
|
|
1532
1546
|
content: `Error in sub-agent ${agent.name}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1533
1547
|
role: "assistant"
|
|
@@ -1577,7 +1591,7 @@ ${response.content || "No content"}
|
|
|
1577
1591
|
};
|
|
1578
1592
|
|
|
1579
1593
|
// src/agents/loop-agent.ts
|
|
1580
|
-
|
|
1594
|
+
init_logger();
|
|
1581
1595
|
var LoopAgent = class extends BaseAgent {
|
|
1582
1596
|
/**
|
|
1583
1597
|
* Maximum number of iterations to prevent infinite loops
|
|
@@ -1591,6 +1605,7 @@ var LoopAgent = class extends BaseAgent {
|
|
|
1591
1605
|
* Custom condition check function
|
|
1592
1606
|
*/
|
|
1593
1607
|
conditionCheck;
|
|
1608
|
+
logger = new Logger({ name: "LoopAgent" });
|
|
1594
1609
|
/**
|
|
1595
1610
|
* Constructor for LoopAgent
|
|
1596
1611
|
*/
|
|
@@ -1618,19 +1633,19 @@ var LoopAgent = class extends BaseAgent {
|
|
|
1618
1633
|
*/
|
|
1619
1634
|
async shouldContinue(response, iterationCount, messages, config) {
|
|
1620
1635
|
if (iterationCount >= this.maxIterations) {
|
|
1621
|
-
|
|
1622
|
-
`
|
|
1636
|
+
this.logger.debug(
|
|
1637
|
+
`Maximum iterations (${this.maxIterations}) reached. Stopping loop.`
|
|
1623
1638
|
);
|
|
1624
1639
|
return false;
|
|
1625
1640
|
}
|
|
1626
1641
|
if (this.conditionCheck) {
|
|
1627
1642
|
const shouldContinue = await this.conditionCheck(response);
|
|
1628
|
-
|
|
1643
|
+
this.logger.debug(`Custom condition check result: ${shouldContinue}`);
|
|
1629
1644
|
return shouldContinue;
|
|
1630
1645
|
}
|
|
1631
1646
|
if (this.conditionAgent) {
|
|
1632
|
-
|
|
1633
|
-
`
|
|
1647
|
+
this.logger.debug(
|
|
1648
|
+
`Using condition agent ${this.conditionAgent.name} to check loop condition`
|
|
1634
1649
|
);
|
|
1635
1650
|
const conditionMessages = [
|
|
1636
1651
|
...messages,
|
|
@@ -1650,12 +1665,12 @@ var LoopAgent = class extends BaseAgent {
|
|
|
1650
1665
|
});
|
|
1651
1666
|
const content = conditionResponse.content?.toLowerCase() || "";
|
|
1652
1667
|
const shouldContinue = content.includes("yes") && !content.includes("no");
|
|
1653
|
-
|
|
1654
|
-
`
|
|
1668
|
+
this.logger.debug(
|
|
1669
|
+
`Condition agent result: ${shouldContinue ? "Continue loop" : "Stop loop"}`
|
|
1655
1670
|
);
|
|
1656
1671
|
return shouldContinue;
|
|
1657
1672
|
} catch (error) {
|
|
1658
|
-
console.error("
|
|
1673
|
+
console.error("Error in condition agent:", error);
|
|
1659
1674
|
return false;
|
|
1660
1675
|
}
|
|
1661
1676
|
}
|
|
@@ -1666,8 +1681,8 @@ var LoopAgent = class extends BaseAgent {
|
|
|
1666
1681
|
* Executes the sub-agent in a loop until the condition is met
|
|
1667
1682
|
*/
|
|
1668
1683
|
async run(options) {
|
|
1669
|
-
|
|
1670
|
-
`
|
|
1684
|
+
this.logger.debug(
|
|
1685
|
+
`Starting loop with max ${this.maxIterations} iterations`
|
|
1671
1686
|
);
|
|
1672
1687
|
if (this.subAgents.length === 0) {
|
|
1673
1688
|
return {
|
|
@@ -1682,8 +1697,8 @@ var LoopAgent = class extends BaseAgent {
|
|
|
1682
1697
|
let shouldContinueLoop = true;
|
|
1683
1698
|
while (shouldContinueLoop && iterationCount < this.maxIterations) {
|
|
1684
1699
|
iterationCount++;
|
|
1685
|
-
|
|
1686
|
-
`
|
|
1700
|
+
this.logger.debug(
|
|
1701
|
+
`Running iteration ${iterationCount}/${this.maxIterations}`
|
|
1687
1702
|
);
|
|
1688
1703
|
try {
|
|
1689
1704
|
const response = await subAgent.run({
|
|
@@ -1708,10 +1723,7 @@ var LoopAgent = class extends BaseAgent {
|
|
|
1708
1723
|
});
|
|
1709
1724
|
}
|
|
1710
1725
|
} catch (error) {
|
|
1711
|
-
console.error(
|
|
1712
|
-
`[LoopAgent] Error in loop iteration ${iterationCount}:`,
|
|
1713
|
-
error
|
|
1714
|
-
);
|
|
1726
|
+
console.error(`Error in loop iteration ${iterationCount}:`, error);
|
|
1715
1727
|
break;
|
|
1716
1728
|
}
|
|
1717
1729
|
}
|
|
@@ -1732,8 +1744,8 @@ ${lastResponse.content || ""}`,
|
|
|
1732
1744
|
* Runs the agent with streaming support
|
|
1733
1745
|
*/
|
|
1734
1746
|
async *runStreaming(options) {
|
|
1735
|
-
|
|
1736
|
-
`
|
|
1747
|
+
this.logger.debug(
|
|
1748
|
+
`Starting loop with max ${this.maxIterations} iterations (streaming)`
|
|
1737
1749
|
);
|
|
1738
1750
|
if (this.subAgents.length === 0) {
|
|
1739
1751
|
yield {
|
|
@@ -1753,8 +1765,8 @@ ${lastResponse.content || ""}`,
|
|
|
1753
1765
|
};
|
|
1754
1766
|
while (shouldContinueLoop && iterationCount < this.maxIterations) {
|
|
1755
1767
|
iterationCount++;
|
|
1756
|
-
|
|
1757
|
-
`
|
|
1768
|
+
this.logger.debug(
|
|
1769
|
+
`Running iteration ${iterationCount}/${this.maxIterations} (streaming)`
|
|
1758
1770
|
);
|
|
1759
1771
|
yield {
|
|
1760
1772
|
content: `Running iteration ${iterationCount}/${this.maxIterations}...`,
|
|
@@ -1779,8 +1791,8 @@ ${lastResponse.content || ""}`,
|
|
|
1779
1791
|
}
|
|
1780
1792
|
}
|
|
1781
1793
|
if (!lastChunk) {
|
|
1782
|
-
|
|
1783
|
-
`
|
|
1794
|
+
this.logger.debug(
|
|
1795
|
+
`No complete chunk received from iteration ${iterationCount}`
|
|
1784
1796
|
);
|
|
1785
1797
|
shouldContinueLoop = false;
|
|
1786
1798
|
continue;
|
|
@@ -1807,8 +1819,8 @@ ${lastResponse.content || ""}`,
|
|
|
1807
1819
|
};
|
|
1808
1820
|
}
|
|
1809
1821
|
} catch (error) {
|
|
1810
|
-
|
|
1811
|
-
`
|
|
1822
|
+
this.logger.debug(
|
|
1823
|
+
`Error in loop iteration ${iterationCount}: ${error instanceof Error ? error.message : String(error)}`
|
|
1812
1824
|
);
|
|
1813
1825
|
yield {
|
|
1814
1826
|
content: `Error in loop iteration ${iterationCount}: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -1825,7 +1837,7 @@ ${lastResponse.content || ""}`,
|
|
|
1825
1837
|
};
|
|
1826
1838
|
|
|
1827
1839
|
// src/agents/lang-graph-agent.ts
|
|
1828
|
-
|
|
1840
|
+
init_logger();
|
|
1829
1841
|
var LangGraphAgent = class extends BaseAgent {
|
|
1830
1842
|
/**
|
|
1831
1843
|
* Graph nodes (agents and their connections)
|
|
@@ -1843,6 +1855,7 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
1843
1855
|
* Results from node executions
|
|
1844
1856
|
*/
|
|
1845
1857
|
results = [];
|
|
1858
|
+
logger = new Logger({ name: "LangGraphAgent" });
|
|
1846
1859
|
/**
|
|
1847
1860
|
* Constructor for LangGraphAgent
|
|
1848
1861
|
*/
|
|
@@ -1917,15 +1930,13 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
1917
1930
|
for (const targetName of currentNode.targets) {
|
|
1918
1931
|
const targetNode = this.nodes.get(targetName);
|
|
1919
1932
|
if (!targetNode) {
|
|
1920
|
-
console.error(`
|
|
1933
|
+
console.error(`Target node "${targetName}" not found`);
|
|
1921
1934
|
continue;
|
|
1922
1935
|
}
|
|
1923
1936
|
if (targetNode.condition) {
|
|
1924
1937
|
const shouldExecute = await targetNode.condition(result, context);
|
|
1925
1938
|
if (!shouldExecute) {
|
|
1926
|
-
|
|
1927
|
-
`[LangGraphAgent] Skipping node "${targetName}" due to condition`
|
|
1928
|
-
);
|
|
1939
|
+
this.logger.debug(`Skipping node "${targetName}" due to condition`);
|
|
1929
1940
|
continue;
|
|
1930
1941
|
}
|
|
1931
1942
|
}
|
|
@@ -1950,9 +1961,7 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
1950
1961
|
};
|
|
1951
1962
|
const shouldExecute = await node.condition(mockResponse, mockContext);
|
|
1952
1963
|
if (!shouldExecute) {
|
|
1953
|
-
|
|
1954
|
-
`[LangGraphAgent] Skipping node "${targetName}" due to condition`
|
|
1955
|
-
);
|
|
1964
|
+
this.logger.debug(`Skipping node "${targetName}" due to condition`);
|
|
1956
1965
|
}
|
|
1957
1966
|
return { shouldExecute };
|
|
1958
1967
|
}
|
|
@@ -1965,8 +1974,8 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
1965
1974
|
messages: options.messages,
|
|
1966
1975
|
config: options.config
|
|
1967
1976
|
});
|
|
1968
|
-
|
|
1969
|
-
`
|
|
1977
|
+
this.logger.debug(
|
|
1978
|
+
`Starting graph execution from root node "${this.rootNode}"`
|
|
1970
1979
|
);
|
|
1971
1980
|
if (this.nodes.size === 0) {
|
|
1972
1981
|
return {
|
|
@@ -1987,9 +1996,7 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
1987
1996
|
while (nodesToExecute.length > 0 && stepCount < this.maxSteps) {
|
|
1988
1997
|
stepCount++;
|
|
1989
1998
|
const { node, messages } = nodesToExecute.shift();
|
|
1990
|
-
|
|
1991
|
-
`[LangGraphAgent] Step ${stepCount}: Executing node "${node.name}"`
|
|
1992
|
-
);
|
|
1999
|
+
this.logger.debug(`Step ${stepCount}: Executing node "${node.name}"`);
|
|
1993
2000
|
executedNodes.push(node.name);
|
|
1994
2001
|
try {
|
|
1995
2002
|
const result = await node.agent.run({
|
|
@@ -2037,7 +2044,7 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
2037
2044
|
});
|
|
2038
2045
|
}
|
|
2039
2046
|
} catch (error) {
|
|
2040
|
-
console.error(`
|
|
2047
|
+
console.error(`Error in node "${node.name}":`, error);
|
|
2041
2048
|
return {
|
|
2042
2049
|
content: `Error in node "${node.name}": ${error instanceof Error ? error.message : String(error)}`,
|
|
2043
2050
|
role: "assistant"
|
|
@@ -2061,8 +2068,8 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
2061
2068
|
messages: options.messages,
|
|
2062
2069
|
config: options.config
|
|
2063
2070
|
});
|
|
2064
|
-
|
|
2065
|
-
`
|
|
2071
|
+
this.logger.debug(
|
|
2072
|
+
`Starting graph execution from root node "${this.rootNode}" (streaming)`
|
|
2066
2073
|
);
|
|
2067
2074
|
if (this.nodes.size === 0) {
|
|
2068
2075
|
yield {
|
|
@@ -2090,8 +2097,8 @@ var LangGraphAgent = class extends BaseAgent {
|
|
|
2090
2097
|
while (nodesToExecute.length > 0 && stepCount < this.maxSteps) {
|
|
2091
2098
|
stepCount++;
|
|
2092
2099
|
const { node, messages } = nodesToExecute.shift();
|
|
2093
|
-
|
|
2094
|
-
`
|
|
2100
|
+
this.logger.debug(
|
|
2101
|
+
`Step ${stepCount}: Executing node "${node.name}" (streaming)`
|
|
2095
2102
|
);
|
|
2096
2103
|
executedNodes.push(node.name);
|
|
2097
2104
|
try {
|
|
@@ -2146,7 +2153,7 @@ Node output: ${this.extractTextContent(result.content)}` : ""}`,
|
|
|
2146
2153
|
});
|
|
2147
2154
|
}
|
|
2148
2155
|
} catch (error) {
|
|
2149
|
-
console.error(`
|
|
2156
|
+
console.error(`Error in node "${node.name}":`, error);
|
|
2150
2157
|
yield {
|
|
2151
2158
|
content: `Error in node "${node.name}": ${error instanceof Error ? error.message : String(error)}`,
|
|
2152
2159
|
role: "assistant"
|
|
@@ -2206,9 +2213,10 @@ function createFunctionTool(func, options) {
|
|
|
2206
2213
|
init_function_utils();
|
|
2207
2214
|
|
|
2208
2215
|
// src/tools/common/google-search.ts
|
|
2209
|
-
|
|
2216
|
+
init_logger();
|
|
2210
2217
|
init_base_tool();
|
|
2211
2218
|
var GoogleSearch = class extends BaseTool {
|
|
2219
|
+
logger = new Logger({ name: "GoogleSearch" });
|
|
2212
2220
|
/**
|
|
2213
2221
|
* Constructor for GoogleSearch
|
|
2214
2222
|
*/
|
|
@@ -2247,7 +2255,9 @@ var GoogleSearch = class extends BaseTool {
|
|
|
2247
2255
|
* This is a simplified implementation that doesn't actually search, just returns mock results
|
|
2248
2256
|
*/
|
|
2249
2257
|
async runAsync(args, _context) {
|
|
2250
|
-
|
|
2258
|
+
this.logger.debug(
|
|
2259
|
+
`[GoogleSearch] Executing Google search for: ${args.query}`
|
|
2260
|
+
);
|
|
2251
2261
|
return {
|
|
2252
2262
|
results: [
|
|
2253
2263
|
{
|
|
@@ -2710,9 +2720,10 @@ var UserInteractionTool = class extends BaseTool {
|
|
|
2710
2720
|
};
|
|
2711
2721
|
|
|
2712
2722
|
// src/tools/common/exit-loop-tool.ts
|
|
2713
|
-
|
|
2723
|
+
init_logger();
|
|
2714
2724
|
init_base_tool();
|
|
2715
2725
|
var ExitLoopTool = class extends BaseTool {
|
|
2726
|
+
logger = new Logger({ name: "ExitLoopTool" });
|
|
2716
2727
|
/**
|
|
2717
2728
|
* Constructor for ExitLoopTool
|
|
2718
2729
|
*/
|
|
@@ -2740,7 +2751,7 @@ var ExitLoopTool = class extends BaseTool {
|
|
|
2740
2751
|
* Execute the exit loop action
|
|
2741
2752
|
*/
|
|
2742
2753
|
async runAsync(_args, context) {
|
|
2743
|
-
|
|
2754
|
+
this.logger.debug("Executing exit loop tool");
|
|
2744
2755
|
if (context.actions) {
|
|
2745
2756
|
context.actions.escalate = true;
|
|
2746
2757
|
} else {
|
|
@@ -2755,9 +2766,10 @@ var ExitLoopTool = class extends BaseTool {
|
|
|
2755
2766
|
};
|
|
2756
2767
|
|
|
2757
2768
|
// src/tools/common/get-user-choice-tool.ts
|
|
2758
|
-
|
|
2769
|
+
init_logger();
|
|
2759
2770
|
init_base_tool();
|
|
2760
2771
|
var GetUserChoiceTool = class extends BaseTool {
|
|
2772
|
+
logger = new Logger({ name: "GetUserChoiceTool" });
|
|
2761
2773
|
/**
|
|
2762
2774
|
* Constructor for GetUserChoiceTool
|
|
2763
2775
|
*/
|
|
@@ -2800,13 +2812,11 @@ var GetUserChoiceTool = class extends BaseTool {
|
|
|
2800
2812
|
* and the actual choice will be provided asynchronously
|
|
2801
2813
|
*/
|
|
2802
2814
|
async runAsync(args, context) {
|
|
2803
|
-
|
|
2804
|
-
`
|
|
2805
|
-
", "
|
|
2806
|
-
)}`
|
|
2815
|
+
this.logger.debug(
|
|
2816
|
+
`Executing get_user_choice with options: ${args.options.join(", ")}`
|
|
2807
2817
|
);
|
|
2808
2818
|
if (args.question) {
|
|
2809
|
-
|
|
2819
|
+
this.logger.debug(`Question: ${args.question}`);
|
|
2810
2820
|
}
|
|
2811
2821
|
if (context.actions) {
|
|
2812
2822
|
context.actions.skip_summarization = true;
|
|
@@ -2820,9 +2830,10 @@ var GetUserChoiceTool = class extends BaseTool {
|
|
|
2820
2830
|
};
|
|
2821
2831
|
|
|
2822
2832
|
// src/tools/common/transfer-to-agent-tool.ts
|
|
2823
|
-
|
|
2833
|
+
init_logger();
|
|
2824
2834
|
init_base_tool();
|
|
2825
2835
|
var TransferToAgentTool = class extends BaseTool {
|
|
2836
|
+
logger = new Logger({ name: "TransferToAgentTool" });
|
|
2826
2837
|
/**
|
|
2827
2838
|
* Constructor for TransferToAgentTool
|
|
2828
2839
|
*/
|
|
@@ -2855,9 +2866,7 @@ var TransferToAgentTool = class extends BaseTool {
|
|
|
2855
2866
|
* Execute the transfer to agent action
|
|
2856
2867
|
*/
|
|
2857
2868
|
async runAsync(args, context) {
|
|
2858
|
-
|
|
2859
|
-
`[TransferToAgentTool] Executing transfer to agent: ${args.agent_name}`
|
|
2860
|
-
);
|
|
2869
|
+
this.logger.debug(`Executing transfer to agent: ${args.agent_name}`);
|
|
2861
2870
|
if (context.actions) {
|
|
2862
2871
|
context.actions.transfer_to_agent = args.agent_name;
|
|
2863
2872
|
} else {
|
|
@@ -2872,9 +2881,10 @@ var TransferToAgentTool = class extends BaseTool {
|
|
|
2872
2881
|
};
|
|
2873
2882
|
|
|
2874
2883
|
// src/tools/common/load-memory-tool.ts
|
|
2875
|
-
|
|
2884
|
+
init_logger();
|
|
2876
2885
|
init_base_tool();
|
|
2877
2886
|
var LoadMemoryTool = class extends BaseTool {
|
|
2887
|
+
logger = new Logger({ name: "LoadMemoryTool" });
|
|
2878
2888
|
/**
|
|
2879
2889
|
* Constructor for LoadMemoryTool
|
|
2880
2890
|
*/
|
|
@@ -2907,9 +2917,7 @@ var LoadMemoryTool = class extends BaseTool {
|
|
|
2907
2917
|
* Execute the memory loading action
|
|
2908
2918
|
*/
|
|
2909
2919
|
async runAsync(args, context) {
|
|
2910
|
-
|
|
2911
|
-
`[LoadMemoryTool] Executing load_memory with query: ${args.query}`
|
|
2912
|
-
);
|
|
2920
|
+
this.logger.debug(`Executing load_memory with query: ${args.query}`);
|
|
2913
2921
|
if (!context.memoryService) {
|
|
2914
2922
|
return {
|
|
2915
2923
|
error: "Memory service is not available",
|
|
@@ -2933,9 +2941,18 @@ var LoadMemoryTool = class extends BaseTool {
|
|
|
2933
2941
|
};
|
|
2934
2942
|
|
|
2935
2943
|
// src/tools/mcp/client.ts
|
|
2944
|
+
init_logger();
|
|
2936
2945
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2937
2946
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
2938
2947
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
2948
|
+
import { CreateMessageRequestSchema as CreateMessageRequestSchema2 } from "@modelcontextprotocol/sdk/types.js";
|
|
2949
|
+
|
|
2950
|
+
// src/tools/mcp/sampling-handler.ts
|
|
2951
|
+
init_logger();
|
|
2952
|
+
import {
|
|
2953
|
+
CreateMessageRequestSchema,
|
|
2954
|
+
CreateMessageResultSchema
|
|
2955
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
2939
2956
|
|
|
2940
2957
|
// src/tools/mcp/types.ts
|
|
2941
2958
|
var McpErrorType = /* @__PURE__ */ ((McpErrorType2) => {
|
|
@@ -2944,6 +2961,8 @@ var McpErrorType = /* @__PURE__ */ ((McpErrorType2) => {
|
|
|
2944
2961
|
McpErrorType2["RESOURCE_CLOSED_ERROR"] = "resource_closed_error";
|
|
2945
2962
|
McpErrorType2["TIMEOUT_ERROR"] = "timeout_error";
|
|
2946
2963
|
McpErrorType2["INVALID_SCHEMA_ERROR"] = "invalid_schema_error";
|
|
2964
|
+
McpErrorType2["SAMPLING_ERROR"] = "SAMPLING_ERROR";
|
|
2965
|
+
McpErrorType2["INVALID_REQUEST_ERROR"] = "INVALID_REQUEST_ERROR";
|
|
2947
2966
|
return McpErrorType2;
|
|
2948
2967
|
})(McpErrorType || {});
|
|
2949
2968
|
var McpError = class extends Error {
|
|
@@ -2957,6 +2976,154 @@ var McpError = class extends Error {
|
|
|
2957
2976
|
}
|
|
2958
2977
|
};
|
|
2959
2978
|
|
|
2979
|
+
// src/tools/mcp/sampling-handler.ts
|
|
2980
|
+
var McpSamplingHandler = class {
|
|
2981
|
+
logger = new Logger({ name: "McpSamplingHandler" });
|
|
2982
|
+
adkHandler;
|
|
2983
|
+
constructor(adkHandler) {
|
|
2984
|
+
this.adkHandler = adkHandler;
|
|
2985
|
+
}
|
|
2986
|
+
/**
|
|
2987
|
+
* Handle MCP sampling request and convert between formats
|
|
2988
|
+
*/
|
|
2989
|
+
async handleSamplingRequest(request) {
|
|
2990
|
+
try {
|
|
2991
|
+
const validationResult = CreateMessageRequestSchema.safeParse(request);
|
|
2992
|
+
if (!validationResult.success) {
|
|
2993
|
+
this.logger.error(
|
|
2994
|
+
"Invalid MCP sampling request:",
|
|
2995
|
+
validationResult.error
|
|
2996
|
+
);
|
|
2997
|
+
throw new McpError(
|
|
2998
|
+
`Invalid sampling request: ${validationResult.error.message}`,
|
|
2999
|
+
"INVALID_REQUEST_ERROR" /* INVALID_REQUEST_ERROR */
|
|
3000
|
+
);
|
|
3001
|
+
}
|
|
3002
|
+
const mcpParams = request.params;
|
|
3003
|
+
if (!mcpParams.messages || !Array.isArray(mcpParams.messages)) {
|
|
3004
|
+
throw new McpError(
|
|
3005
|
+
"Invalid sampling request: messages array is required",
|
|
3006
|
+
"INVALID_REQUEST_ERROR" /* INVALID_REQUEST_ERROR */
|
|
3007
|
+
);
|
|
3008
|
+
}
|
|
3009
|
+
if (!mcpParams.maxTokens || mcpParams.maxTokens <= 0) {
|
|
3010
|
+
throw new McpError(
|
|
3011
|
+
"Invalid sampling request: maxTokens must be a positive number",
|
|
3012
|
+
"INVALID_REQUEST_ERROR" /* INVALID_REQUEST_ERROR */
|
|
3013
|
+
);
|
|
3014
|
+
}
|
|
3015
|
+
this.logger.debug("Converting MCP request to ADK format");
|
|
3016
|
+
const adkMessages = this.convertMcpMessagesToADK(mcpParams.messages);
|
|
3017
|
+
const adkRequest = {
|
|
3018
|
+
messages: adkMessages,
|
|
3019
|
+
systemPrompt: mcpParams.systemPrompt,
|
|
3020
|
+
modelPreferences: mcpParams.modelPreferences,
|
|
3021
|
+
includeContext: mcpParams.includeContext,
|
|
3022
|
+
temperature: mcpParams.temperature,
|
|
3023
|
+
maxTokens: mcpParams.maxTokens,
|
|
3024
|
+
stopSequences: mcpParams.stopSequences,
|
|
3025
|
+
metadata: mcpParams.metadata
|
|
3026
|
+
};
|
|
3027
|
+
this.logger.debug("Calling ADK sampling handler");
|
|
3028
|
+
const adkResponse = await this.adkHandler(adkRequest);
|
|
3029
|
+
this.logger.debug("Converting ADK response to MCP format");
|
|
3030
|
+
const mcpResponse = this.convertADKResponseToMcp(adkResponse);
|
|
3031
|
+
const responseValidation = CreateMessageResultSchema.safeParse(mcpResponse);
|
|
3032
|
+
if (!responseValidation.success) {
|
|
3033
|
+
this.logger.error(
|
|
3034
|
+
"Invalid MCP response generated:",
|
|
3035
|
+
responseValidation.error
|
|
3036
|
+
);
|
|
3037
|
+
throw new McpError(
|
|
3038
|
+
`Invalid response generated: ${responseValidation.error.message}`,
|
|
3039
|
+
"SAMPLING_ERROR" /* SAMPLING_ERROR */
|
|
3040
|
+
);
|
|
3041
|
+
}
|
|
3042
|
+
return mcpResponse;
|
|
3043
|
+
} catch (error) {
|
|
3044
|
+
this.logger.error("Error handling sampling request:", error);
|
|
3045
|
+
if (error instanceof McpError) {
|
|
3046
|
+
throw error;
|
|
3047
|
+
}
|
|
3048
|
+
throw new McpError(
|
|
3049
|
+
`Sampling request failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
3050
|
+
"SAMPLING_ERROR" /* SAMPLING_ERROR */,
|
|
3051
|
+
error instanceof Error ? error : void 0
|
|
3052
|
+
);
|
|
3053
|
+
}
|
|
3054
|
+
}
|
|
3055
|
+
/**
|
|
3056
|
+
* Convert MCP messages to ADK message format
|
|
3057
|
+
*/
|
|
3058
|
+
convertMcpMessagesToADK(mcpMessages) {
|
|
3059
|
+
return mcpMessages.map((mcpMessage) => {
|
|
3060
|
+
const adkRole = mcpMessage.role === "assistant" ? "assistant" : "user";
|
|
3061
|
+
let adkContent;
|
|
3062
|
+
if (mcpMessage.content.type === "text") {
|
|
3063
|
+
adkContent = mcpMessage.content.text || "";
|
|
3064
|
+
} else if (mcpMessage.content.type === "image") {
|
|
3065
|
+
const contentParts = [];
|
|
3066
|
+
if (mcpMessage.content.text) {
|
|
3067
|
+
contentParts.push({
|
|
3068
|
+
type: "text",
|
|
3069
|
+
text: mcpMessage.content.text
|
|
3070
|
+
});
|
|
3071
|
+
}
|
|
3072
|
+
if (mcpMessage.content.data) {
|
|
3073
|
+
const mimeType = mcpMessage.content.mimeType || "image/jpeg";
|
|
3074
|
+
const dataUrl = `data:${mimeType};base64,${mcpMessage.content.data}`;
|
|
3075
|
+
contentParts.push({
|
|
3076
|
+
type: "image",
|
|
3077
|
+
image_url: {
|
|
3078
|
+
url: dataUrl
|
|
3079
|
+
}
|
|
3080
|
+
});
|
|
3081
|
+
}
|
|
3082
|
+
adkContent = contentParts.length > 0 ? contentParts : "";
|
|
3083
|
+
} else {
|
|
3084
|
+
this.logger.warn(
|
|
3085
|
+
`Unknown MCP content type: ${mcpMessage.content.type}`
|
|
3086
|
+
);
|
|
3087
|
+
adkContent = mcpMessage.content.text || "";
|
|
3088
|
+
}
|
|
3089
|
+
const adkMessage = {
|
|
3090
|
+
role: adkRole,
|
|
3091
|
+
content: adkContent
|
|
3092
|
+
};
|
|
3093
|
+
this.logger.debug(
|
|
3094
|
+
`Converted MCP message - role: ${mcpMessage.role} -> ${adkRole}, content type: ${mcpMessage.content.type}`
|
|
3095
|
+
);
|
|
3096
|
+
return adkMessage;
|
|
3097
|
+
});
|
|
3098
|
+
}
|
|
3099
|
+
/**
|
|
3100
|
+
* Convert ADK response to MCP response format
|
|
3101
|
+
*/
|
|
3102
|
+
convertADKResponseToMcp(adkResponse) {
|
|
3103
|
+
const mcpResponse = {
|
|
3104
|
+
model: adkResponse.model,
|
|
3105
|
+
stopReason: adkResponse.stopReason,
|
|
3106
|
+
role: "assistant",
|
|
3107
|
+
// ADK responses are always from assistant
|
|
3108
|
+
content: {
|
|
3109
|
+
type: "text",
|
|
3110
|
+
text: adkResponse.content || ""
|
|
3111
|
+
}
|
|
3112
|
+
};
|
|
3113
|
+
this.logger.debug(
|
|
3114
|
+
`Converted ADK response - model: ${adkResponse.model}, content length: ${adkResponse.content?.length || 0}`
|
|
3115
|
+
);
|
|
3116
|
+
return mcpResponse;
|
|
3117
|
+
}
|
|
3118
|
+
/**
|
|
3119
|
+
* Update the ADK handler
|
|
3120
|
+
*/
|
|
3121
|
+
updateHandler(handler) {
|
|
3122
|
+
this.adkHandler = handler;
|
|
3123
|
+
this.logger.debug("ADK sampling handler updated");
|
|
3124
|
+
}
|
|
3125
|
+
};
|
|
3126
|
+
|
|
2960
3127
|
// src/tools/mcp/utils.ts
|
|
2961
3128
|
function withRetry(fn, instance, reinitMethod, maxRetries = 1) {
|
|
2962
3129
|
return async (...args) => {
|
|
@@ -2991,6 +3158,8 @@ var McpClientService = class {
|
|
|
2991
3158
|
client = null;
|
|
2992
3159
|
transport = null;
|
|
2993
3160
|
isClosing = false;
|
|
3161
|
+
mcpSamplingHandler = null;
|
|
3162
|
+
logger = new Logger({ name: "McpClientService" });
|
|
2994
3163
|
constructor(config) {
|
|
2995
3164
|
this.config = config;
|
|
2996
3165
|
}
|
|
@@ -3041,6 +3210,7 @@ var McpClientService = class {
|
|
|
3041
3210
|
} else {
|
|
3042
3211
|
await connectPromise;
|
|
3043
3212
|
}
|
|
3213
|
+
await this.setupSamplingHandler(client);
|
|
3044
3214
|
if (this.config.debug) {
|
|
3045
3215
|
console.log("\u2705 MCP client connected successfully");
|
|
3046
3216
|
}
|
|
@@ -3192,10 +3362,67 @@ var McpClientService = class {
|
|
|
3192
3362
|
isConnected() {
|
|
3193
3363
|
return !!this.client && !this.isClosing;
|
|
3194
3364
|
}
|
|
3365
|
+
async setupSamplingHandler(client) {
|
|
3366
|
+
if (!this.mcpSamplingHandler) {
|
|
3367
|
+
if (this.config.debug) {
|
|
3368
|
+
console.log(
|
|
3369
|
+
"\u26A0\uFE0F No sampling handler provided - sampling requests will be rejected"
|
|
3370
|
+
);
|
|
3371
|
+
}
|
|
3372
|
+
return;
|
|
3373
|
+
}
|
|
3374
|
+
client.setRequestHandler(CreateMessageRequestSchema2, async (request) => {
|
|
3375
|
+
try {
|
|
3376
|
+
this.logger.debug("Received sampling request:", request);
|
|
3377
|
+
const response = await this.mcpSamplingHandler.handleSamplingRequest(request);
|
|
3378
|
+
if (this.config.debug) {
|
|
3379
|
+
console.log("\u2705 Sampling request completed successfully");
|
|
3380
|
+
}
|
|
3381
|
+
return response;
|
|
3382
|
+
} catch (error) {
|
|
3383
|
+
console.error("\u274C Error handling sampling request:", error);
|
|
3384
|
+
if (error instanceof McpError) {
|
|
3385
|
+
throw error;
|
|
3386
|
+
}
|
|
3387
|
+
throw new McpError(
|
|
3388
|
+
`Sampling request failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
3389
|
+
"SAMPLING_ERROR" /* SAMPLING_ERROR */,
|
|
3390
|
+
error instanceof Error ? error : void 0
|
|
3391
|
+
);
|
|
3392
|
+
}
|
|
3393
|
+
});
|
|
3394
|
+
if (this.config.debug) {
|
|
3395
|
+
console.log("\u{1F3AF} Sampling handler registered successfully");
|
|
3396
|
+
}
|
|
3397
|
+
}
|
|
3398
|
+
/**
|
|
3399
|
+
* Set an ADK sampling handler
|
|
3400
|
+
*/
|
|
3401
|
+
setSamplingHandler(handler) {
|
|
3402
|
+
this.mcpSamplingHandler = new McpSamplingHandler(handler);
|
|
3403
|
+
if (this.client) {
|
|
3404
|
+
this.setupSamplingHandler(this.client).catch((error) => {
|
|
3405
|
+
console.error("Failed to update sampling handler:", error);
|
|
3406
|
+
});
|
|
3407
|
+
}
|
|
3408
|
+
}
|
|
3409
|
+
/**
|
|
3410
|
+
* Remove the sampling handler
|
|
3411
|
+
*/
|
|
3412
|
+
removeSamplingHandler() {
|
|
3413
|
+
this.mcpSamplingHandler = null;
|
|
3414
|
+
if (this.client) {
|
|
3415
|
+
try {
|
|
3416
|
+
this.client.removeRequestHandler?.("sampling/createMessage");
|
|
3417
|
+
} catch (error) {
|
|
3418
|
+
console.error("Failed to remove sampling handler:", error);
|
|
3419
|
+
}
|
|
3420
|
+
}
|
|
3421
|
+
}
|
|
3195
3422
|
};
|
|
3196
3423
|
|
|
3197
3424
|
// src/tools/mcp/create-tool.ts
|
|
3198
|
-
|
|
3425
|
+
init_logger();
|
|
3199
3426
|
init_base_tool();
|
|
3200
3427
|
|
|
3201
3428
|
// src/tools/mcp/schema-conversion.ts
|
|
@@ -3398,6 +3625,7 @@ var McpToolAdapter = class extends BaseTool {
|
|
|
3398
3625
|
mcpTool;
|
|
3399
3626
|
client;
|
|
3400
3627
|
clientService = null;
|
|
3628
|
+
logger = new Logger({ name: "McpToolAdapter" });
|
|
3401
3629
|
constructor(mcpTool, client) {
|
|
3402
3630
|
const metadata = mcpTool.metadata || {};
|
|
3403
3631
|
super({
|
|
@@ -3430,10 +3658,7 @@ var McpToolAdapter = class extends BaseTool {
|
|
|
3430
3658
|
}
|
|
3431
3659
|
}
|
|
3432
3660
|
async runAsync(args, _context) {
|
|
3433
|
-
|
|
3434
|
-
`[McpToolAdapter] Executing MCP tool ${this.name} with args:`,
|
|
3435
|
-
args
|
|
3436
|
-
);
|
|
3661
|
+
this.logger.debug(`Executing MCP tool ${this.name} with args:`, args);
|
|
3437
3662
|
try {
|
|
3438
3663
|
if (typeof this.mcpTool.execute === "function") {
|
|
3439
3664
|
return await this.mcpTool.execute(args);
|
|
@@ -3753,11 +3978,11 @@ var BaseLLMConnection = class {
|
|
|
3753
3978
|
};
|
|
3754
3979
|
|
|
3755
3980
|
// src/models/anthropic-llm.ts
|
|
3756
|
-
|
|
3981
|
+
init_logger();
|
|
3757
3982
|
import axios from "axios";
|
|
3758
3983
|
|
|
3759
3984
|
// src/models/anthropic-llm-connection.ts
|
|
3760
|
-
|
|
3985
|
+
init_logger();
|
|
3761
3986
|
var AnthropicLLMConnection = class extends BaseLLMConnection {
|
|
3762
3987
|
/**
|
|
3763
3988
|
* Axios instance for API calls
|
|
@@ -3785,6 +4010,7 @@ var AnthropicLLMConnection = class extends BaseLLMConnection {
|
|
|
3785
4010
|
responseCallback;
|
|
3786
4011
|
errorCallback;
|
|
3787
4012
|
endCallback;
|
|
4013
|
+
logger = new Logger({ name: "AnthropicLlmConnection" });
|
|
3788
4014
|
/**
|
|
3789
4015
|
* Constructor
|
|
3790
4016
|
*/
|
|
@@ -3887,12 +4113,10 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
3887
4113
|
if (!content?.length) return [];
|
|
3888
4114
|
const toolUses = [];
|
|
3889
4115
|
for (const block of content) {
|
|
3890
|
-
|
|
3891
|
-
`[AnthropicLLMConnection] Processing content block of type: ${block.type}`
|
|
3892
|
-
);
|
|
4116
|
+
this.logger.debug(`Processing content block of type: ${block.type}`);
|
|
3893
4117
|
if (block.type === "tool_use") {
|
|
3894
|
-
|
|
3895
|
-
"
|
|
4118
|
+
this.logger.debug(
|
|
4119
|
+
"Found tool_use block:",
|
|
3896
4120
|
JSON.stringify(block, null, 2)
|
|
3897
4121
|
);
|
|
3898
4122
|
toolUses.push({
|
|
@@ -3902,12 +4126,10 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
3902
4126
|
});
|
|
3903
4127
|
}
|
|
3904
4128
|
}
|
|
3905
|
-
|
|
3906
|
-
`[AnthropicLLMConnection] Found ${toolUses.length} tool uses in content`
|
|
3907
|
-
);
|
|
4129
|
+
this.logger.debug(`Found ${toolUses.length} tool uses in content`);
|
|
3908
4130
|
if (toolUses.length > 0) {
|
|
3909
|
-
|
|
3910
|
-
"
|
|
4131
|
+
this.logger.debug(
|
|
4132
|
+
"Extracted tool uses:",
|
|
3911
4133
|
JSON.stringify(toolUses, null, 2)
|
|
3912
4134
|
);
|
|
3913
4135
|
}
|
|
@@ -4003,8 +4225,8 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4003
4225
|
}
|
|
4004
4226
|
const toolUses = this.extractToolUses(apiResponse.content);
|
|
4005
4227
|
const toolCalls = this.convertToolCalls(toolUses);
|
|
4006
|
-
|
|
4007
|
-
|
|
4228
|
+
this.logger.debug(
|
|
4229
|
+
`- Extracted ${toolUses.length} tool uses in content and converted ${toolCalls?.length || 0} tool calls`
|
|
4008
4230
|
);
|
|
4009
4231
|
const llmResponse = new LLMResponse({
|
|
4010
4232
|
role: "assistant",
|
|
@@ -4017,16 +4239,13 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4017
4239
|
content: llmResponse.content?.substring(0, 50) + (llmResponse.content && llmResponse.content.length > 50 ? "..." : ""),
|
|
4018
4240
|
tool_calls: llmResponse.tool_calls ? `[${llmResponse.tool_calls.length} calls]` : "undefined"
|
|
4019
4241
|
};
|
|
4020
|
-
|
|
4021
|
-
"
|
|
4242
|
+
this.logger.debug(
|
|
4243
|
+
"Final LLMResponse object:",
|
|
4022
4244
|
JSON.stringify(logObject, null, 2)
|
|
4023
4245
|
);
|
|
4024
4246
|
return llmResponse;
|
|
4025
4247
|
} catch (error) {
|
|
4026
|
-
|
|
4027
|
-
"[AnthropicLLMConnection] Error sending message to Anthropic:",
|
|
4028
|
-
error
|
|
4029
|
-
);
|
|
4248
|
+
this.logger.debug("Error sending message to Anthropic:", error);
|
|
4030
4249
|
throw error;
|
|
4031
4250
|
}
|
|
4032
4251
|
}
|
|
@@ -4046,6 +4265,7 @@ var AnthropicLLM = class extends BaseLLM {
|
|
|
4046
4265
|
* Default parameters for requests
|
|
4047
4266
|
*/
|
|
4048
4267
|
defaultParams;
|
|
4268
|
+
logger = new Logger({ name: "AnthropicLLM" });
|
|
4049
4269
|
/**
|
|
4050
4270
|
* Constructor for AnthropicLLM
|
|
4051
4271
|
*/
|
|
@@ -4178,12 +4398,10 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4178
4398
|
if (!content?.length) return [];
|
|
4179
4399
|
const toolUses = [];
|
|
4180
4400
|
for (const block of content) {
|
|
4181
|
-
|
|
4182
|
-
`[AnthropicLLM] Processing content block of type: ${block.type}`
|
|
4183
|
-
);
|
|
4401
|
+
this.logger.debug(`Processing content block of type: ${block.type}`);
|
|
4184
4402
|
if (block.type === "tool_use") {
|
|
4185
|
-
|
|
4186
|
-
`
|
|
4403
|
+
this.logger.debug(
|
|
4404
|
+
`Found tool_use block: ${JSON.stringify(block, null, 2)}`
|
|
4187
4405
|
);
|
|
4188
4406
|
toolUses.push({
|
|
4189
4407
|
id: block.id || "unknown-id",
|
|
@@ -4192,8 +4410,8 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4192
4410
|
});
|
|
4193
4411
|
}
|
|
4194
4412
|
}
|
|
4195
|
-
|
|
4196
|
-
`
|
|
4413
|
+
this.logger.debug(
|
|
4414
|
+
`Found ${toolUses.length} tool uses in content`,
|
|
4197
4415
|
toolUses
|
|
4198
4416
|
);
|
|
4199
4417
|
return toolUses;
|
|
@@ -4217,12 +4435,12 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4217
4435
|
},
|
|
4218
4436
|
responseType: stream ? "stream" : "json"
|
|
4219
4437
|
});
|
|
4220
|
-
|
|
4221
|
-
`
|
|
4438
|
+
this.logger.debug(
|
|
4439
|
+
`API Response done with ${response.status}:`,
|
|
4222
4440
|
response.data
|
|
4223
4441
|
);
|
|
4224
|
-
|
|
4225
|
-
"
|
|
4442
|
+
this.logger.debug(
|
|
4443
|
+
"API Response content:",
|
|
4226
4444
|
response.data.content.map((block) => ({ type: block.type }))
|
|
4227
4445
|
);
|
|
4228
4446
|
return response.data;
|
|
@@ -4250,7 +4468,7 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4250
4468
|
top_p: llmRequest.config.top_p ?? this.defaultParams.top_p,
|
|
4251
4469
|
tools: tools?.length ? tools : void 0
|
|
4252
4470
|
};
|
|
4253
|
-
|
|
4471
|
+
this.logger.debug("API Request:", {
|
|
4254
4472
|
model: params.model,
|
|
4255
4473
|
messageCount: params.messages.length,
|
|
4256
4474
|
systemMessage: params.system ? "present" : "none",
|
|
@@ -4260,7 +4478,7 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4260
4478
|
throw new Error("Streaming is not supported in this implementation");
|
|
4261
4479
|
}
|
|
4262
4480
|
const response = await this.callAnthropicAPI(params);
|
|
4263
|
-
|
|
4481
|
+
this.logger.debug("Full Response Content:", response.content);
|
|
4264
4482
|
let content = "";
|
|
4265
4483
|
for (const block of response.content) {
|
|
4266
4484
|
if (block.type === "text") {
|
|
@@ -4269,8 +4487,8 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4269
4487
|
}
|
|
4270
4488
|
const toolUses = this.extractToolUses(response.content);
|
|
4271
4489
|
const toolCalls = this.convertToolUses(toolUses);
|
|
4272
|
-
|
|
4273
|
-
|
|
4490
|
+
this.logger.debug("Extracted Tool Uses:", toolUses);
|
|
4491
|
+
this.logger.debug("Converted Tool Calls:", toolCalls);
|
|
4274
4492
|
const llmResponse = new LLMResponse({
|
|
4275
4493
|
role: "assistant",
|
|
4276
4494
|
content,
|
|
@@ -4282,13 +4500,13 @@ ${typeof message.content === "string" ? message.content : JSON.stringify(message
|
|
|
4282
4500
|
content: llmResponse.content?.substring(0, 50) + (llmResponse.content && llmResponse.content.length > 50 ? "..." : ""),
|
|
4283
4501
|
tool_calls: llmResponse.tool_calls ? `[${llmResponse.tool_calls.length} calls]` : "undefined"
|
|
4284
4502
|
};
|
|
4285
|
-
|
|
4286
|
-
"
|
|
4503
|
+
this.logger.debug(
|
|
4504
|
+
"Final LLMResponse object:",
|
|
4287
4505
|
JSON.stringify(logObject, null, 2)
|
|
4288
4506
|
);
|
|
4289
4507
|
yield llmResponse;
|
|
4290
4508
|
} catch (error) {
|
|
4291
|
-
|
|
4509
|
+
this.logger.debug("Error:", error);
|
|
4292
4510
|
throw error;
|
|
4293
4511
|
}
|
|
4294
4512
|
}
|
|
@@ -4683,7 +4901,7 @@ var GoogleLLM = class extends BaseLLM {
|
|
|
4683
4901
|
};
|
|
4684
4902
|
|
|
4685
4903
|
// src/models/openai-llm.ts
|
|
4686
|
-
|
|
4904
|
+
init_logger();
|
|
4687
4905
|
import OpenAI from "openai";
|
|
4688
4906
|
|
|
4689
4907
|
// src/models/openai-llm-connection.ts
|
|
@@ -4943,6 +5161,7 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
4943
5161
|
* Default parameters for requests
|
|
4944
5162
|
*/
|
|
4945
5163
|
defaultParams;
|
|
5164
|
+
logger = new Logger({ name: "OpenAILLM" });
|
|
4946
5165
|
/**
|
|
4947
5166
|
* Constructor for OpenAILLM
|
|
4948
5167
|
*/
|
|
@@ -5092,8 +5311,8 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
5092
5311
|
* Convert OpenAI streaming chunk to LLMResponse
|
|
5093
5312
|
*/
|
|
5094
5313
|
convertChunk(chunk) {
|
|
5095
|
-
|
|
5096
|
-
`
|
|
5314
|
+
this.logger.debug(
|
|
5315
|
+
`Converting chunk - delta: ${JSON.stringify(chunk.delta || {})}`
|
|
5097
5316
|
);
|
|
5098
5317
|
const content = chunk.delta?.content;
|
|
5099
5318
|
const result = new LLMResponse({
|
|
@@ -5135,24 +5354,24 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
5135
5354
|
presence_penalty: llmRequest.config.presence_penalty ?? this.defaultParams.presence_penalty,
|
|
5136
5355
|
stream: shouldStream
|
|
5137
5356
|
};
|
|
5138
|
-
|
|
5139
|
-
`
|
|
5357
|
+
this.logger.debug(
|
|
5358
|
+
`Request parameters - model: ${params.model}, messages: ${params.messages.length}, functions: ${params.tools ? params.tools.length : 0}, streaming: ${shouldStream}`
|
|
5140
5359
|
);
|
|
5141
5360
|
if (tools && tools.length > 0) {
|
|
5142
5361
|
params.tools = tools;
|
|
5143
5362
|
}
|
|
5144
5363
|
try {
|
|
5145
5364
|
if (shouldStream) {
|
|
5146
|
-
|
|
5365
|
+
this.logger.debug("Starting streaming request");
|
|
5147
5366
|
const streamResponse = await this.client.chat.completions.create(params);
|
|
5148
5367
|
let partialFunctionCall;
|
|
5149
5368
|
const partialToolCalls = /* @__PURE__ */ new Map();
|
|
5150
5369
|
let accumulatedContent = "";
|
|
5151
5370
|
const asyncIterable = streamResponse;
|
|
5152
|
-
|
|
5371
|
+
this.logger.debug("Stream response received, processing chunks");
|
|
5153
5372
|
for await (const chunk of asyncIterable) {
|
|
5154
5373
|
if (!chunk.choices || chunk.choices.length === 0) {
|
|
5155
|
-
|
|
5374
|
+
this.logger.debug("Empty chunk received, skipping");
|
|
5156
5375
|
continue;
|
|
5157
5376
|
}
|
|
5158
5377
|
const choice = chunk.choices[0];
|
|
@@ -5160,8 +5379,8 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
5160
5379
|
if (responseChunk.content !== null) {
|
|
5161
5380
|
accumulatedContent += responseChunk.content;
|
|
5162
5381
|
}
|
|
5163
|
-
|
|
5164
|
-
`
|
|
5382
|
+
this.logger.debug(
|
|
5383
|
+
`Chunk received - delta: "${choice.delta?.content || ""}"`,
|
|
5165
5384
|
`responseChunk content: "${responseChunk.content || ""}"`,
|
|
5166
5385
|
`is_partial: ${responseChunk.is_partial}`,
|
|
5167
5386
|
`accumulated: "${accumulatedContent.substring(0, 30)}${accumulatedContent.length > 30 ? "..." : ""}"`
|
|
@@ -5190,12 +5409,12 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
5190
5409
|
}
|
|
5191
5410
|
responseChunk.tool_calls = Array.from(partialToolCalls.values());
|
|
5192
5411
|
}
|
|
5193
|
-
|
|
5412
|
+
this.logger.debug("Yielding chunk to caller");
|
|
5194
5413
|
yield responseChunk;
|
|
5195
5414
|
}
|
|
5196
5415
|
if (accumulatedContent.length > 0) {
|
|
5197
|
-
|
|
5198
|
-
`
|
|
5416
|
+
this.logger.debug(
|
|
5417
|
+
`Yielding final accumulated content: "${accumulatedContent.substring(0, 30)}${accumulatedContent.length > 30 ? "..." : ""}"`
|
|
5199
5418
|
);
|
|
5200
5419
|
yield new LLMResponse({
|
|
5201
5420
|
content: accumulatedContent,
|
|
@@ -5203,14 +5422,14 @@ var OpenAILLM = class extends BaseLLM {
|
|
|
5203
5422
|
is_partial: false
|
|
5204
5423
|
});
|
|
5205
5424
|
}
|
|
5206
|
-
|
|
5425
|
+
this.logger.debug("Finished processing all stream chunks");
|
|
5207
5426
|
} else {
|
|
5208
|
-
|
|
5427
|
+
this.logger.debug("Making non-streaming request");
|
|
5209
5428
|
const response = await this.client.chat.completions.create(params);
|
|
5210
5429
|
if (!response.choices || response.choices.length === 0) {
|
|
5211
5430
|
throw new Error("No response from OpenAI");
|
|
5212
5431
|
}
|
|
5213
|
-
|
|
5432
|
+
this.logger.debug("Non-streaming response received");
|
|
5214
5433
|
yield this.convertResponse(response.choices[0]);
|
|
5215
5434
|
}
|
|
5216
5435
|
} catch (error) {
|
|
@@ -5797,7 +6016,7 @@ var InMemoryMemoryService = class {
|
|
|
5797
6016
|
};
|
|
5798
6017
|
|
|
5799
6018
|
// src/memory/persistent-memory-service.ts
|
|
5800
|
-
|
|
6019
|
+
init_logger();
|
|
5801
6020
|
import fs2 from "fs";
|
|
5802
6021
|
import path2 from "path";
|
|
5803
6022
|
var PersistentMemoryService = class {
|
|
@@ -5813,6 +6032,7 @@ var PersistentMemoryService = class {
|
|
|
5813
6032
|
* File prefix for memory files
|
|
5814
6033
|
*/
|
|
5815
6034
|
filePrefix;
|
|
6035
|
+
logger = new Logger({ name: "PersistentMemoryService" });
|
|
5816
6036
|
/**
|
|
5817
6037
|
* Constructor for PersistentMemoryService
|
|
5818
6038
|
*/
|
|
@@ -5901,7 +6121,7 @@ var PersistentMemoryService = class {
|
|
|
5901
6121
|
}
|
|
5902
6122
|
}
|
|
5903
6123
|
}
|
|
5904
|
-
|
|
6124
|
+
this.logger.debug(
|
|
5905
6125
|
`Loaded ${this.inMemoryService.getAllSessions().length} sessions from persistent storage`
|
|
5906
6126
|
);
|
|
5907
6127
|
} catch (error) {
|