@iqai/adk 0.1.19 → 0.1.21
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 +13 -0
- package/dist/index.d.mts +232 -112
- package/dist/index.d.ts +232 -112
- package/dist/index.js +1142 -821
- package/dist/index.mjs +1105 -784
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27; var _class28; var _class29; var _class30; var _class31; var _class32; var _class33;var __defProp = Object.defineProperty;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27; var _class28; var _class29; var _class30; var _class31; var _class32; var _class33; var _class34;var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
@@ -1353,7 +1353,7 @@ var BaseLlm = (_class5 = class {
|
|
|
1353
1353
|
* @param llmRequest LlmRequest, the request to send to the LLM.
|
|
1354
1354
|
* @returns BaseLLMConnection, the connection to the LLM.
|
|
1355
1355
|
*/
|
|
1356
|
-
connect(
|
|
1356
|
+
connect(_llmRequest) {
|
|
1357
1357
|
throw new Error(`Live connection is not supported for ${this.model}.`);
|
|
1358
1358
|
}
|
|
1359
1359
|
}, _class5);
|
|
@@ -1362,276 +1362,332 @@ var BaseLlm = (_class5 = class {
|
|
|
1362
1362
|
var BaseLLMConnection = class {
|
|
1363
1363
|
};
|
|
1364
1364
|
|
|
1365
|
-
// src/models/
|
|
1365
|
+
// src/models/ai-sdk.ts
|
|
1366
|
+
init_logger();
|
|
1366
1367
|
|
|
1367
1368
|
|
|
1368
1369
|
|
|
1369
1370
|
|
|
1370
|
-
var
|
|
1371
|
-
var
|
|
1372
|
-
var GoogleLlm = class extends BaseLlm {
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1371
|
+
var _ai = require('ai');
|
|
1372
|
+
var AiSdkLlm = (_class6 = class extends BaseLlm {
|
|
1376
1373
|
|
|
1374
|
+
__init12() {this.logger = new Logger({ name: "AiSdkLlm" })}
|
|
1377
1375
|
/**
|
|
1378
|
-
* Constructor
|
|
1376
|
+
* Constructor accepts a pre-configured LanguageModel instance
|
|
1377
|
+
* @param model - Pre-configured LanguageModel from provider(modelName)
|
|
1379
1378
|
*/
|
|
1380
|
-
constructor(
|
|
1381
|
-
super(model);
|
|
1379
|
+
constructor(modelInstance) {
|
|
1380
|
+
super(modelInstance.modelId || "ai-sdk-model");_class6.prototype.__init12.call(this);;
|
|
1381
|
+
this.modelInstance = modelInstance;
|
|
1382
1382
|
}
|
|
1383
1383
|
/**
|
|
1384
|
-
*
|
|
1384
|
+
* Returns empty array - following Python ADK pattern
|
|
1385
1385
|
*/
|
|
1386
1386
|
static supportedModels() {
|
|
1387
|
-
return [
|
|
1388
|
-
"gemini-.*",
|
|
1389
|
-
// fine-tuned vertex endpoint pattern
|
|
1390
|
-
"projects/.+/locations/.+/endpoints/.+",
|
|
1391
|
-
// vertex gemini long name
|
|
1392
|
-
"projects/.+/locations/.+/publishers/google/models/gemini.+"
|
|
1393
|
-
];
|
|
1387
|
+
return [];
|
|
1394
1388
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
config
|
|
1408
|
-
}
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
response = resp;
|
|
1415
|
-
const llmResponse = LlmResponse.create(resp);
|
|
1416
|
-
usageMetadata = llmResponse.usageMetadata;
|
|
1417
|
-
if (_optionalChain([llmResponse, 'access', _45 => _45.content, 'optionalAccess', _46 => _46.parts, 'optionalAccess', _47 => _47[0], 'optionalAccess', _48 => _48.text])) {
|
|
1418
|
-
const part0 = llmResponse.content.parts[0];
|
|
1419
|
-
if (part0.thought) {
|
|
1420
|
-
thoughtText += part0.text;
|
|
1421
|
-
} else {
|
|
1422
|
-
text += part0.text;
|
|
1423
|
-
}
|
|
1424
|
-
llmResponse.partial = true;
|
|
1425
|
-
} else if ((thoughtText || text) && (!llmResponse.content || !llmResponse.content.parts || !this.hasInlineData(resp))) {
|
|
1426
|
-
const parts = [];
|
|
1427
|
-
if (thoughtText) {
|
|
1428
|
-
parts.push({ text: thoughtText, thought: true });
|
|
1429
|
-
}
|
|
1430
|
-
if (text) {
|
|
1431
|
-
parts.push({ text });
|
|
1432
|
-
}
|
|
1389
|
+
async *generateContentAsyncImpl(request, stream = false) {
|
|
1390
|
+
try {
|
|
1391
|
+
const messages = this.convertToAiSdkMessages(request);
|
|
1392
|
+
const systemMessage = request.getSystemInstructionText();
|
|
1393
|
+
const tools = this.convertToAiSdkTools(request);
|
|
1394
|
+
const requestParams = {
|
|
1395
|
+
model: this.modelInstance,
|
|
1396
|
+
messages,
|
|
1397
|
+
system: systemMessage,
|
|
1398
|
+
tools: Object.keys(tools).length > 0 ? tools : void 0,
|
|
1399
|
+
maxTokens: _optionalChain([request, 'access', _45 => _45.config, 'optionalAccess', _46 => _46.maxOutputTokens]),
|
|
1400
|
+
temperature: _optionalChain([request, 'access', _47 => _47.config, 'optionalAccess', _48 => _48.temperature]),
|
|
1401
|
+
topP: _optionalChain([request, 'access', _49 => _49.config, 'optionalAccess', _50 => _50.topP])
|
|
1402
|
+
};
|
|
1403
|
+
if (stream) {
|
|
1404
|
+
const result = _ai.streamText.call(void 0, requestParams);
|
|
1405
|
+
let accumulatedText = "";
|
|
1406
|
+
for await (const delta of result.textStream) {
|
|
1407
|
+
accumulatedText += delta;
|
|
1433
1408
|
yield new LlmResponse({
|
|
1434
1409
|
content: {
|
|
1435
|
-
|
|
1436
|
-
|
|
1410
|
+
role: "model",
|
|
1411
|
+
parts: [{ text: accumulatedText }]
|
|
1437
1412
|
},
|
|
1438
|
-
|
|
1413
|
+
partial: true
|
|
1439
1414
|
});
|
|
1440
|
-
thoughtText = "";
|
|
1441
|
-
text = "";
|
|
1442
1415
|
}
|
|
1443
|
-
|
|
1444
|
-
}
|
|
1445
|
-
if ((text || thoughtText) && response && response.candidates && _optionalChain([response, 'access', _49 => _49.candidates, 'access', _50 => _50[0], 'optionalAccess', _51 => _51.finishReason]) === _genai.FinishReason.STOP) {
|
|
1416
|
+
const toolCalls = await result.toolCalls;
|
|
1446
1417
|
const parts = [];
|
|
1447
|
-
if (
|
|
1448
|
-
parts.push({ text:
|
|
1418
|
+
if (accumulatedText) {
|
|
1419
|
+
parts.push({ text: accumulatedText });
|
|
1449
1420
|
}
|
|
1450
|
-
if (
|
|
1451
|
-
|
|
1421
|
+
if (toolCalls && toolCalls.length > 0) {
|
|
1422
|
+
for (const toolCall of toolCalls) {
|
|
1423
|
+
parts.push({
|
|
1424
|
+
functionCall: {
|
|
1425
|
+
id: toolCall.toolCallId,
|
|
1426
|
+
name: toolCall.toolName,
|
|
1427
|
+
args: toolCall.args
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1452
1431
|
}
|
|
1432
|
+
const finalUsage = await result.usage;
|
|
1433
|
+
const finishReason = await result.finishReason;
|
|
1453
1434
|
yield new LlmResponse({
|
|
1454
1435
|
content: {
|
|
1455
|
-
|
|
1456
|
-
|
|
1436
|
+
role: "model",
|
|
1437
|
+
parts: parts.length > 0 ? parts : [{ text: "" }]
|
|
1457
1438
|
},
|
|
1458
|
-
usageMetadata
|
|
1439
|
+
usageMetadata: finalUsage ? {
|
|
1440
|
+
promptTokenCount: finalUsage.promptTokens,
|
|
1441
|
+
candidatesTokenCount: finalUsage.completionTokens,
|
|
1442
|
+
totalTokenCount: finalUsage.totalTokens
|
|
1443
|
+
} : void 0,
|
|
1444
|
+
finishReason: this.mapFinishReason(finishReason),
|
|
1445
|
+
turnComplete: true
|
|
1446
|
+
});
|
|
1447
|
+
} else {
|
|
1448
|
+
const result = await _ai.generateText.call(void 0, requestParams);
|
|
1449
|
+
const parts = [];
|
|
1450
|
+
if (result.text) {
|
|
1451
|
+
parts.push({ text: result.text });
|
|
1452
|
+
}
|
|
1453
|
+
if (result.toolCalls && result.toolCalls.length > 0) {
|
|
1454
|
+
for (const toolCall of result.toolCalls) {
|
|
1455
|
+
parts.push({
|
|
1456
|
+
functionCall: {
|
|
1457
|
+
id: toolCall.toolCallId,
|
|
1458
|
+
name: toolCall.toolName,
|
|
1459
|
+
args: toolCall.args
|
|
1460
|
+
}
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
yield new LlmResponse({
|
|
1465
|
+
content: {
|
|
1466
|
+
role: "model",
|
|
1467
|
+
parts: parts.length > 0 ? parts : [{ text: "" }]
|
|
1468
|
+
},
|
|
1469
|
+
usageMetadata: result.usage ? {
|
|
1470
|
+
promptTokenCount: result.usage.promptTokens,
|
|
1471
|
+
candidatesTokenCount: result.usage.completionTokens,
|
|
1472
|
+
totalTokenCount: result.usage.totalTokens
|
|
1473
|
+
} : void 0,
|
|
1474
|
+
finishReason: this.mapFinishReason(result.finishReason),
|
|
1475
|
+
turnComplete: true
|
|
1459
1476
|
});
|
|
1460
1477
|
}
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
this.logger.error(`AI SDK Error: ${String(error)}`, { error, request });
|
|
1480
|
+
yield LlmResponse.fromError(error, {
|
|
1481
|
+
errorCode: "AI_SDK_ERROR",
|
|
1482
|
+
model: this.model
|
|
1466
1483
|
});
|
|
1467
|
-
const llmResponse = LlmResponse.create(response);
|
|
1468
|
-
this.logger.debug(
|
|
1469
|
-
`Google response: ${_optionalChain([llmResponse, 'access', _52 => _52.usageMetadata, 'optionalAccess', _53 => _53.candidatesTokenCount]) || 0} tokens`
|
|
1470
|
-
);
|
|
1471
|
-
yield llmResponse;
|
|
1472
1484
|
}
|
|
1473
1485
|
}
|
|
1474
1486
|
/**
|
|
1475
|
-
*
|
|
1476
|
-
*/
|
|
1477
|
-
connect(_llmRequest) {
|
|
1478
|
-
throw new Error(`Live connection is not supported for ${this.model}.`);
|
|
1479
|
-
}
|
|
1480
|
-
/**
|
|
1481
|
-
* Check if response has inline data
|
|
1487
|
+
* Convert ADK LlmRequest to AI SDK CoreMessage format
|
|
1482
1488
|
*/
|
|
1483
|
-
|
|
1484
|
-
const
|
|
1485
|
-
|
|
1489
|
+
convertToAiSdkMessages(llmRequest) {
|
|
1490
|
+
const messages = [];
|
|
1491
|
+
for (const content of llmRequest.contents || []) {
|
|
1492
|
+
const message = this.contentToAiSdkMessage(content);
|
|
1493
|
+
if (message) {
|
|
1494
|
+
messages.push(message);
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
return messages;
|
|
1486
1498
|
}
|
|
1487
1499
|
/**
|
|
1488
|
-
*
|
|
1500
|
+
* Transform JSON schema to use lowercase types for AI SDK compatibility
|
|
1489
1501
|
*/
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1502
|
+
transformSchemaForAiSdk(schema) {
|
|
1503
|
+
if (Array.isArray(schema)) {
|
|
1504
|
+
return schema.map((item) => this.transformSchemaForAiSdk(item));
|
|
1505
|
+
}
|
|
1506
|
+
if (!schema || typeof schema !== "object") {
|
|
1507
|
+
return schema;
|
|
1508
|
+
}
|
|
1509
|
+
const transformedSchema = { ...schema };
|
|
1510
|
+
if (transformedSchema.type && typeof transformedSchema.type === "string") {
|
|
1511
|
+
transformedSchema.type = transformedSchema.type.toLowerCase();
|
|
1512
|
+
}
|
|
1513
|
+
if (transformedSchema.properties) {
|
|
1514
|
+
transformedSchema.properties = Object.fromEntries(
|
|
1515
|
+
Object.entries(transformedSchema.properties).map(([key, value]) => [
|
|
1516
|
+
key,
|
|
1517
|
+
this.transformSchemaForAiSdk(value)
|
|
1518
|
+
])
|
|
1519
|
+
);
|
|
1520
|
+
}
|
|
1521
|
+
if (transformedSchema.items) {
|
|
1522
|
+
transformedSchema.items = this.transformSchemaForAiSdk(
|
|
1523
|
+
transformedSchema.items
|
|
1524
|
+
);
|
|
1525
|
+
}
|
|
1526
|
+
const arrayKeywords = ["anyOf", "oneOf", "allOf"];
|
|
1527
|
+
for (const keyword of arrayKeywords) {
|
|
1528
|
+
if (transformedSchema[keyword]) {
|
|
1529
|
+
transformedSchema[keyword] = this.transformSchemaForAiSdk(
|
|
1530
|
+
transformedSchema[keyword]
|
|
1531
|
+
);
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
return transformedSchema;
|
|
1495
1535
|
}
|
|
1496
1536
|
/**
|
|
1497
|
-
*
|
|
1537
|
+
* Convert ADK tools to AI SDK tools format
|
|
1498
1538
|
*/
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1539
|
+
convertToAiSdkTools(llmRequest) {
|
|
1540
|
+
const tools = {};
|
|
1541
|
+
if (_optionalChain([llmRequest, 'access', _51 => _51.config, 'optionalAccess', _52 => _52.tools])) {
|
|
1542
|
+
for (const toolConfig of llmRequest.config.tools) {
|
|
1543
|
+
if ("functionDeclarations" in toolConfig) {
|
|
1544
|
+
for (const funcDecl of toolConfig.functionDeclarations) {
|
|
1545
|
+
tools[funcDecl.name] = {
|
|
1546
|
+
description: funcDecl.description,
|
|
1547
|
+
parameters: _ai.jsonSchema.call(void 0,
|
|
1548
|
+
this.transformSchemaForAiSdk(funcDecl.parameters || {})
|
|
1549
|
+
)
|
|
1550
|
+
};
|
|
1510
1551
|
}
|
|
1511
1552
|
}
|
|
1512
1553
|
}
|
|
1513
1554
|
}
|
|
1555
|
+
return tools;
|
|
1514
1556
|
}
|
|
1515
1557
|
/**
|
|
1516
|
-
*
|
|
1558
|
+
* Convert ADK Content to AI SDK CoreMessage
|
|
1517
1559
|
*/
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1560
|
+
contentToAiSdkMessage(content) {
|
|
1561
|
+
const role = this.mapRole(content.role);
|
|
1562
|
+
if (!content.parts || content.parts.length === 0) {
|
|
1563
|
+
return null;
|
|
1521
1564
|
}
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1565
|
+
if (content.parts.length === 1 && content.parts[0].text) {
|
|
1566
|
+
const textContent = content.parts[0].text;
|
|
1567
|
+
if (role === "system") {
|
|
1568
|
+
return { role: "system", content: textContent };
|
|
1569
|
+
}
|
|
1570
|
+
if (role === "assistant") {
|
|
1571
|
+
return { role: "assistant", content: textContent };
|
|
1572
|
+
}
|
|
1573
|
+
return { role: "user", content: textContent };
|
|
1530
1574
|
}
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1575
|
+
if (_optionalChain([content, 'access', _53 => _53.parts, 'optionalAccess', _54 => _54.some, 'call', _55 => _55((part) => part.functionCall)])) {
|
|
1576
|
+
const textParts = content.parts.filter((part) => part.text);
|
|
1577
|
+
const functionCalls = content.parts.filter((part) => part.functionCall);
|
|
1578
|
+
const contentParts2 = [];
|
|
1579
|
+
for (const textPart of textParts) {
|
|
1580
|
+
if (textPart.text) {
|
|
1581
|
+
contentParts2.push({
|
|
1582
|
+
type: "text",
|
|
1583
|
+
text: textPart.text
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
for (const funcPart of functionCalls) {
|
|
1588
|
+
if (funcPart.functionCall) {
|
|
1589
|
+
contentParts2.push({
|
|
1590
|
+
type: "tool-call",
|
|
1591
|
+
toolCallId: funcPart.functionCall.id,
|
|
1592
|
+
toolName: funcPart.functionCall.name,
|
|
1593
|
+
args: funcPart.functionCall.args
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
return {
|
|
1598
|
+
role: "assistant",
|
|
1599
|
+
content: contentParts2
|
|
1600
|
+
};
|
|
1601
|
+
}
|
|
1602
|
+
if (_optionalChain([content, 'access', _56 => _56.parts, 'optionalAccess', _57 => _57.some, 'call', _58 => _58((part) => part.functionResponse)])) {
|
|
1603
|
+
const functionResponses = content.parts.filter(
|
|
1604
|
+
(part) => part.functionResponse
|
|
1605
|
+
);
|
|
1606
|
+
const contentParts2 = functionResponses.map((part) => ({
|
|
1607
|
+
type: "tool-result",
|
|
1608
|
+
toolCallId: part.functionResponse.id,
|
|
1609
|
+
toolName: part.functionResponse.name || "unknown",
|
|
1610
|
+
result: part.functionResponse.response
|
|
1611
|
+
}));
|
|
1612
|
+
return {
|
|
1613
|
+
role: "tool",
|
|
1614
|
+
content: contentParts2
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
const contentParts = [];
|
|
1618
|
+
for (const part of content.parts) {
|
|
1619
|
+
if (part.text) {
|
|
1620
|
+
contentParts.push({
|
|
1621
|
+
type: "text",
|
|
1622
|
+
text: part.text
|
|
1551
1623
|
});
|
|
1552
|
-
} else {
|
|
1553
|
-
throw new Error(
|
|
1554
|
-
"Google API Key or Vertex AI configuration is required. Set GOOGLE_API_KEY or GOOGLE_GENAI_USE_VERTEXAI=true with GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION."
|
|
1555
|
-
);
|
|
1556
1624
|
}
|
|
1557
1625
|
}
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
/**
|
|
1561
|
-
* Gets the API backend type.
|
|
1562
|
-
*/
|
|
1563
|
-
get apiBackend() {
|
|
1564
|
-
if (!this._apiBackend) {
|
|
1565
|
-
const useVertexAI = process.env.GOOGLE_GENAI_USE_VERTEXAI === "true";
|
|
1566
|
-
this._apiBackend = useVertexAI ? "VERTEX_AI" /* VERTEX_AI */ : "GEMINI_API" /* GEMINI_API */;
|
|
1626
|
+
if (contentParts.length === 0) {
|
|
1627
|
+
return null;
|
|
1567
1628
|
}
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
*/
|
|
1573
|
-
get trackingHeaders() {
|
|
1574
|
-
if (!this._trackingHeaders) {
|
|
1575
|
-
let frameworkLabel = "google-adk/1.0.0";
|
|
1576
|
-
if (process.env[AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME]) {
|
|
1577
|
-
frameworkLabel = `${frameworkLabel}+${AGENT_ENGINE_TELEMETRY_TAG}`;
|
|
1629
|
+
if (contentParts.length === 1) {
|
|
1630
|
+
const textContent = contentParts[0].text;
|
|
1631
|
+
if (role === "system") {
|
|
1632
|
+
return { role: "system", content: textContent };
|
|
1578
1633
|
}
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
"user-agent": versionHeaderValue
|
|
1584
|
-
};
|
|
1634
|
+
if (role === "assistant") {
|
|
1635
|
+
return { role: "assistant", content: textContent };
|
|
1636
|
+
}
|
|
1637
|
+
return { role: "user", content: textContent };
|
|
1585
1638
|
}
|
|
1586
|
-
|
|
1639
|
+
if (role === "system") {
|
|
1640
|
+
const combinedText = contentParts.map((p) => p.text).join("");
|
|
1641
|
+
return { role: "system", content: combinedText };
|
|
1642
|
+
}
|
|
1643
|
+
if (role === "assistant") {
|
|
1644
|
+
return { role: "assistant", content: contentParts };
|
|
1645
|
+
}
|
|
1646
|
+
return { role: "user", content: contentParts };
|
|
1587
1647
|
}
|
|
1588
1648
|
/**
|
|
1589
|
-
*
|
|
1649
|
+
* Map ADK role to AI SDK role
|
|
1590
1650
|
*/
|
|
1591
|
-
|
|
1592
|
-
|
|
1651
|
+
mapRole(role) {
|
|
1652
|
+
switch (role) {
|
|
1653
|
+
case "model":
|
|
1654
|
+
case "assistant":
|
|
1655
|
+
return "assistant";
|
|
1656
|
+
case "system":
|
|
1657
|
+
return "system";
|
|
1658
|
+
default:
|
|
1659
|
+
return "user";
|
|
1660
|
+
}
|
|
1593
1661
|
}
|
|
1594
1662
|
/**
|
|
1595
|
-
*
|
|
1663
|
+
* Map AI SDK finish reason to ADK finish reason
|
|
1596
1664
|
*/
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
location,
|
|
1608
|
-
apiVersion: this.liveApiVersion
|
|
1609
|
-
});
|
|
1610
|
-
} else if (apiKey) {
|
|
1611
|
-
this._liveApiClient = new (0, _genai.GoogleGenAI)({
|
|
1612
|
-
apiKey,
|
|
1613
|
-
apiVersion: this.liveApiVersion
|
|
1614
|
-
});
|
|
1615
|
-
} else {
|
|
1616
|
-
throw new Error("API configuration required for live client");
|
|
1617
|
-
}
|
|
1665
|
+
mapFinishReason(finishReason) {
|
|
1666
|
+
switch (finishReason) {
|
|
1667
|
+
case "stop":
|
|
1668
|
+
case "end_of_message":
|
|
1669
|
+
return "STOP";
|
|
1670
|
+
case "length":
|
|
1671
|
+
case "max_tokens":
|
|
1672
|
+
return "MAX_TOKENS";
|
|
1673
|
+
default:
|
|
1674
|
+
return "FINISH_REASON_UNSPECIFIED";
|
|
1618
1675
|
}
|
|
1619
|
-
return this._liveApiClient;
|
|
1620
1676
|
}
|
|
1621
|
-
};
|
|
1677
|
+
}, _class6);
|
|
1622
1678
|
|
|
1623
1679
|
// src/models/anthropic-llm.ts
|
|
1624
1680
|
init_logger();
|
|
1625
1681
|
var _sdk = require('@anthropic-ai/sdk'); var _sdk2 = _interopRequireDefault(_sdk);
|
|
1626
1682
|
var MAX_TOKENS = 1024;
|
|
1627
|
-
var AnthropicLlm = (
|
|
1683
|
+
var AnthropicLlm = (_class7 = class extends BaseLlm {
|
|
1628
1684
|
|
|
1629
|
-
|
|
1685
|
+
__init13() {this.logger = new Logger({ name: "AnthropicLlm" })}
|
|
1630
1686
|
/**
|
|
1631
1687
|
* Constructor for Anthropic LLM
|
|
1632
1688
|
*/
|
|
1633
1689
|
constructor(model = "claude-3-5-sonnet-20241022") {
|
|
1634
|
-
super(model);
|
|
1690
|
+
super(model);_class7.prototype.__init13.call(this);;
|
|
1635
1691
|
}
|
|
1636
1692
|
/**
|
|
1637
1693
|
* Provides the list of supported models
|
|
@@ -1648,7 +1704,7 @@ var AnthropicLlm = (_class6 = class extends BaseLlm {
|
|
|
1648
1704
|
(content) => this.contentToAnthropicMessage(content)
|
|
1649
1705
|
);
|
|
1650
1706
|
let tools;
|
|
1651
|
-
if (_optionalChain([llmRequest, 'access',
|
|
1707
|
+
if (_optionalChain([llmRequest, 'access', _59 => _59.config, 'optionalAccess', _60 => _60.tools, 'optionalAccess', _61 => _61[0], 'optionalAccess', _62 => _62.functionDeclarations])) {
|
|
1652
1708
|
tools = llmRequest.config.tools[0].functionDeclarations.map(
|
|
1653
1709
|
(decl) => this.functionDeclarationToAnthropicTool(decl)
|
|
1654
1710
|
);
|
|
@@ -1670,9 +1726,9 @@ var AnthropicLlm = (_class6 = class extends BaseLlm {
|
|
|
1670
1726
|
messages: anthropicMessages,
|
|
1671
1727
|
tools,
|
|
1672
1728
|
tool_choice: tools ? { type: "auto" } : void 0,
|
|
1673
|
-
max_tokens: _optionalChain([llmRequest, 'access',
|
|
1674
|
-
temperature: _optionalChain([llmRequest, 'access',
|
|
1675
|
-
top_p: _optionalChain([llmRequest, 'access',
|
|
1729
|
+
max_tokens: _optionalChain([llmRequest, 'access', _63 => _63.config, 'optionalAccess', _64 => _64.maxOutputTokens]) || MAX_TOKENS,
|
|
1730
|
+
temperature: _optionalChain([llmRequest, 'access', _65 => _65.config, 'optionalAccess', _66 => _66.temperature]),
|
|
1731
|
+
top_p: _optionalChain([llmRequest, 'access', _67 => _67.config, 'optionalAccess', _68 => _68.topP])
|
|
1676
1732
|
});
|
|
1677
1733
|
yield this.anthropicMessageToLlmResponse(message);
|
|
1678
1734
|
}
|
|
@@ -1733,7 +1789,7 @@ var AnthropicLlm = (_class6 = class extends BaseLlm {
|
|
|
1733
1789
|
}
|
|
1734
1790
|
if (part.function_response) {
|
|
1735
1791
|
let content = "";
|
|
1736
|
-
if (_optionalChain([part, 'access',
|
|
1792
|
+
if (_optionalChain([part, 'access', _69 => _69.function_response, 'access', _70 => _70.response, 'optionalAccess', _71 => _71.result])) {
|
|
1737
1793
|
content = String(part.function_response.response.result);
|
|
1738
1794
|
}
|
|
1739
1795
|
return {
|
|
@@ -1768,7 +1824,7 @@ var AnthropicLlm = (_class6 = class extends BaseLlm {
|
|
|
1768
1824
|
*/
|
|
1769
1825
|
functionDeclarationToAnthropicTool(functionDeclaration) {
|
|
1770
1826
|
const properties = {};
|
|
1771
|
-
if (_optionalChain([functionDeclaration, 'access',
|
|
1827
|
+
if (_optionalChain([functionDeclaration, 'access', _72 => _72.parameters, 'optionalAccess', _73 => _73.properties])) {
|
|
1772
1828
|
for (const [key, value] of Object.entries(
|
|
1773
1829
|
functionDeclaration.parameters.properties
|
|
1774
1830
|
)) {
|
|
@@ -1842,23 +1898,37 @@ var AnthropicLlm = (_class6 = class extends BaseLlm {
|
|
|
1842
1898
|
}
|
|
1843
1899
|
return this._client;
|
|
1844
1900
|
}
|
|
1845
|
-
},
|
|
1901
|
+
}, _class7);
|
|
1846
1902
|
|
|
1847
|
-
// src/models/
|
|
1848
|
-
|
|
1849
|
-
|
|
1903
|
+
// src/models/google-llm.ts
|
|
1904
|
+
|
|
1905
|
+
|
|
1906
|
+
|
|
1907
|
+
|
|
1908
|
+
var AGENT_ENGINE_TELEMETRY_TAG = "remote_reasoning_engine";
|
|
1909
|
+
var AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_AGENT_ENGINE_ID";
|
|
1910
|
+
var GoogleLlm = class extends BaseLlm {
|
|
1911
|
+
|
|
1912
|
+
|
|
1913
|
+
|
|
1850
1914
|
|
|
1851
1915
|
/**
|
|
1852
|
-
* Constructor for
|
|
1916
|
+
* Constructor for Gemini
|
|
1853
1917
|
*/
|
|
1854
|
-
constructor(model = "
|
|
1918
|
+
constructor(model = "gemini-1.5-flash") {
|
|
1855
1919
|
super(model);
|
|
1856
1920
|
}
|
|
1857
1921
|
/**
|
|
1858
|
-
* Provides the list of supported models
|
|
1922
|
+
* Provides the list of supported models.
|
|
1859
1923
|
*/
|
|
1860
1924
|
static supportedModels() {
|
|
1861
|
-
return [
|
|
1925
|
+
return [
|
|
1926
|
+
"gemini-.*",
|
|
1927
|
+
// fine-tuned vertex endpoint pattern
|
|
1928
|
+
"projects/.+/locations/.+/endpoints/.+",
|
|
1929
|
+
// vertex gemini long name
|
|
1930
|
+
"projects/.+/locations/.+/publishers/google/models/gemini.+"
|
|
1931
|
+
];
|
|
1862
1932
|
}
|
|
1863
1933
|
/**
|
|
1864
1934
|
* Main content generation method - handles both streaming and non-streaming
|
|
@@ -1866,51 +1936,23 @@ var OpenAiLlm = class extends BaseLlm {
|
|
|
1866
1936
|
async *generateContentAsyncImpl(llmRequest, stream = false) {
|
|
1867
1937
|
this.preprocessRequest(llmRequest);
|
|
1868
1938
|
const model = llmRequest.model || this.model;
|
|
1869
|
-
const
|
|
1870
|
-
|
|
1871
|
-
);
|
|
1872
|
-
let tools;
|
|
1873
|
-
if (_optionalChain([llmRequest, 'access', _79 => _79.config, 'optionalAccess', _80 => _80.tools, 'optionalAccess', _81 => _81[0], 'optionalAccess', _82 => _82.functionDeclarations])) {
|
|
1874
|
-
tools = llmRequest.config.tools[0].functionDeclarations.map(
|
|
1875
|
-
(funcDecl) => this.functionDeclarationToOpenAiTool(funcDecl)
|
|
1876
|
-
);
|
|
1877
|
-
}
|
|
1878
|
-
const systemContent = llmRequest.getSystemInstructionText();
|
|
1879
|
-
if (systemContent) {
|
|
1880
|
-
messages.unshift({
|
|
1881
|
-
role: "system",
|
|
1882
|
-
content: systemContent
|
|
1883
|
-
});
|
|
1884
|
-
}
|
|
1885
|
-
const openAiMessages = messages;
|
|
1886
|
-
const requestParams = {
|
|
1887
|
-
model,
|
|
1888
|
-
messages: openAiMessages,
|
|
1889
|
-
tools,
|
|
1890
|
-
tool_choice: tools ? "auto" : void 0,
|
|
1891
|
-
max_tokens: _optionalChain([llmRequest, 'access', _83 => _83.config, 'optionalAccess', _84 => _84.maxOutputTokens]),
|
|
1892
|
-
temperature: _optionalChain([llmRequest, 'access', _85 => _85.config, 'optionalAccess', _86 => _86.temperature]),
|
|
1893
|
-
top_p: _optionalChain([llmRequest, 'access', _87 => _87.config, 'optionalAccess', _88 => _88.topP]),
|
|
1894
|
-
stream
|
|
1895
|
-
};
|
|
1939
|
+
const contents = this.convertContents(llmRequest.contents || []);
|
|
1940
|
+
const config = llmRequest.config;
|
|
1896
1941
|
if (stream) {
|
|
1897
|
-
const
|
|
1898
|
-
|
|
1899
|
-
|
|
1942
|
+
const responses = await this.apiClient.models.generateContentStream({
|
|
1943
|
+
model,
|
|
1944
|
+
contents,
|
|
1945
|
+
config
|
|
1900
1946
|
});
|
|
1947
|
+
let response = null;
|
|
1901
1948
|
let thoughtText = "";
|
|
1902
1949
|
let text = "";
|
|
1903
|
-
let usageMetadata;
|
|
1904
|
-
const
|
|
1905
|
-
|
|
1906
|
-
const
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
const llmResponse = this.createChunkResponse(delta, chunk.usage);
|
|
1910
|
-
if (chunk.usage) {
|
|
1911
|
-
usageMetadata = chunk.usage;
|
|
1912
|
-
}
|
|
1913
|
-
if (_optionalChain([llmResponse, 'access', _89 => _89.content, 'optionalAccess', _90 => _90.parts, 'optionalAccess', _91 => _91[0], 'optionalAccess', _92 => _92.text])) {
|
|
1950
|
+
let usageMetadata = null;
|
|
1951
|
+
for await (const resp of responses) {
|
|
1952
|
+
response = resp;
|
|
1953
|
+
const llmResponse = LlmResponse.create(resp);
|
|
1954
|
+
usageMetadata = llmResponse.usageMetadata;
|
|
1955
|
+
if (_optionalChain([llmResponse, 'access', _74 => _74.content, 'optionalAccess', _75 => _75.parts, 'optionalAccess', _76 => _76[0], 'optionalAccess', _77 => _77.text])) {
|
|
1914
1956
|
const part0 = llmResponse.content.parts[0];
|
|
1915
1957
|
if (part0.thought) {
|
|
1916
1958
|
thoughtText += part0.text;
|
|
@@ -1918,7 +1960,7 @@ var OpenAiLlm = class extends BaseLlm {
|
|
|
1918
1960
|
text += part0.text;
|
|
1919
1961
|
}
|
|
1920
1962
|
llmResponse.partial = true;
|
|
1921
|
-
} else if ((thoughtText || text) && (!llmResponse.content || !llmResponse.content.parts || !this.hasInlineData(
|
|
1963
|
+
} else if ((thoughtText || text) && (!llmResponse.content || !llmResponse.content.parts || !this.hasInlineData(resp))) {
|
|
1922
1964
|
const parts = [];
|
|
1923
1965
|
if (thoughtText) {
|
|
1924
1966
|
parts.push({ text: thoughtText, thought: true });
|
|
@@ -1931,73 +1973,14 @@ var OpenAiLlm = class extends BaseLlm {
|
|
|
1931
1973
|
parts,
|
|
1932
1974
|
role: "model"
|
|
1933
1975
|
},
|
|
1934
|
-
usageMetadata
|
|
1935
|
-
promptTokenCount: usageMetadata.prompt_tokens,
|
|
1936
|
-
candidatesTokenCount: usageMetadata.completion_tokens,
|
|
1937
|
-
totalTokenCount: usageMetadata.total_tokens
|
|
1938
|
-
} : void 0
|
|
1976
|
+
usageMetadata
|
|
1939
1977
|
});
|
|
1940
1978
|
thoughtText = "";
|
|
1941
1979
|
text = "";
|
|
1942
1980
|
}
|
|
1943
|
-
|
|
1944
|
-
for (const toolCall of delta.tool_calls) {
|
|
1945
|
-
const index = toolCall.index || 0;
|
|
1946
|
-
if (!accumulatedToolCalls[index]) {
|
|
1947
|
-
accumulatedToolCalls[index] = {
|
|
1948
|
-
index,
|
|
1949
|
-
id: toolCall.id || "",
|
|
1950
|
-
type: "function",
|
|
1951
|
-
function: { name: "", arguments: "" }
|
|
1952
|
-
};
|
|
1953
|
-
}
|
|
1954
|
-
if (_optionalChain([toolCall, 'access', _93 => _93.function, 'optionalAccess', _94 => _94.name])) {
|
|
1955
|
-
accumulatedToolCalls[index].function.name += toolCall.function.name;
|
|
1956
|
-
}
|
|
1957
|
-
if (_optionalChain([toolCall, 'access', _95 => _95.function, 'optionalAccess', _96 => _96.arguments])) {
|
|
1958
|
-
accumulatedToolCalls[index].function.arguments += toolCall.function.arguments;
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
}
|
|
1962
|
-
if (choice.finish_reason) {
|
|
1963
|
-
const parts = [];
|
|
1964
|
-
if (thoughtText) {
|
|
1965
|
-
parts.push({ text: thoughtText, thought: true });
|
|
1966
|
-
}
|
|
1967
|
-
if (text) {
|
|
1968
|
-
parts.push({ text });
|
|
1969
|
-
}
|
|
1970
|
-
if (accumulatedToolCalls.length > 0) {
|
|
1971
|
-
for (const toolCall of accumulatedToolCalls) {
|
|
1972
|
-
if (_optionalChain([toolCall, 'access', _97 => _97.function, 'optionalAccess', _98 => _98.name])) {
|
|
1973
|
-
parts.push({
|
|
1974
|
-
functionCall: {
|
|
1975
|
-
id: toolCall.id,
|
|
1976
|
-
name: toolCall.function.name,
|
|
1977
|
-
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
1978
|
-
}
|
|
1979
|
-
});
|
|
1980
|
-
}
|
|
1981
|
-
}
|
|
1982
|
-
}
|
|
1983
|
-
const finalResponse = new LlmResponse({
|
|
1984
|
-
content: {
|
|
1985
|
-
role: "model",
|
|
1986
|
-
parts
|
|
1987
|
-
},
|
|
1988
|
-
usageMetadata: usageMetadata ? {
|
|
1989
|
-
promptTokenCount: usageMetadata.prompt_tokens,
|
|
1990
|
-
candidatesTokenCount: usageMetadata.completion_tokens,
|
|
1991
|
-
totalTokenCount: usageMetadata.total_tokens
|
|
1992
|
-
} : void 0,
|
|
1993
|
-
finishReason: this.toAdkFinishReason(choice.finish_reason)
|
|
1994
|
-
});
|
|
1995
|
-
yield finalResponse;
|
|
1996
|
-
} else {
|
|
1997
|
-
yield llmResponse;
|
|
1998
|
-
}
|
|
1981
|
+
yield llmResponse;
|
|
1999
1982
|
}
|
|
2000
|
-
if ((text || thoughtText) &&
|
|
1983
|
+
if ((text || thoughtText) && response && response.candidates && _optionalChain([response, 'access', _78 => _78.candidates, 'access', _79 => _79[0], 'optionalAccess', _80 => _80.finishReason]) === _genai.FinishReason.STOP) {
|
|
2001
1984
|
const parts = [];
|
|
2002
1985
|
if (thoughtText) {
|
|
2003
1986
|
parts.push({ text: thoughtText, thought: true });
|
|
@@ -2010,561 +1993,656 @@ var OpenAiLlm = class extends BaseLlm {
|
|
|
2010
1993
|
parts,
|
|
2011
1994
|
role: "model"
|
|
2012
1995
|
},
|
|
2013
|
-
usageMetadata
|
|
2014
|
-
promptTokenCount: usageMetadata.prompt_tokens,
|
|
2015
|
-
candidatesTokenCount: usageMetadata.completion_tokens,
|
|
2016
|
-
totalTokenCount: usageMetadata.total_tokens
|
|
2017
|
-
}
|
|
1996
|
+
usageMetadata
|
|
2018
1997
|
});
|
|
2019
1998
|
}
|
|
2020
1999
|
} else {
|
|
2021
|
-
const response = await this.
|
|
2022
|
-
|
|
2023
|
-
|
|
2000
|
+
const response = await this.apiClient.models.generateContent({
|
|
2001
|
+
model,
|
|
2002
|
+
contents,
|
|
2003
|
+
config
|
|
2024
2004
|
});
|
|
2025
|
-
const
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
);
|
|
2031
|
-
this.logger.debug(
|
|
2032
|
-
`OpenAI response: ${_optionalChain([response, 'access', _99 => _99.usage, 'optionalAccess', _100 => _100.completion_tokens]) || 0} tokens`
|
|
2033
|
-
);
|
|
2034
|
-
yield llmResponse;
|
|
2035
|
-
}
|
|
2005
|
+
const llmResponse = LlmResponse.create(response);
|
|
2006
|
+
this.logger.debug(
|
|
2007
|
+
`Google response: ${_optionalChain([llmResponse, 'access', _81 => _81.usageMetadata, 'optionalAccess', _82 => _82.candidatesTokenCount]) || 0} tokens`
|
|
2008
|
+
);
|
|
2009
|
+
yield llmResponse;
|
|
2036
2010
|
}
|
|
2037
2011
|
}
|
|
2038
2012
|
/**
|
|
2039
|
-
*
|
|
2013
|
+
* Connects to the Gemini model and returns an llm connection.
|
|
2040
2014
|
*/
|
|
2041
2015
|
connect(_llmRequest) {
|
|
2042
2016
|
throw new Error(`Live connection is not supported for ${this.model}.`);
|
|
2043
2017
|
}
|
|
2044
2018
|
/**
|
|
2045
|
-
*
|
|
2019
|
+
* Check if response has inline data
|
|
2046
2020
|
*/
|
|
2047
|
-
|
|
2048
|
-
const parts = [];
|
|
2049
|
-
|
|
2050
|
-
const contentType = this.getContentType(delta.content);
|
|
2051
|
-
if (contentType === "thought") {
|
|
2052
|
-
parts.push({ text: delta.content, thought: true });
|
|
2053
|
-
} else {
|
|
2054
|
-
parts.push({ text: delta.content });
|
|
2055
|
-
}
|
|
2056
|
-
}
|
|
2057
|
-
if (delta.tool_calls) {
|
|
2058
|
-
for (const toolCall of delta.tool_calls) {
|
|
2059
|
-
if (toolCall.type === "function" && _optionalChain([toolCall, 'access', _101 => _101.function, 'optionalAccess', _102 => _102.name])) {
|
|
2060
|
-
parts.push({
|
|
2061
|
-
functionCall: {
|
|
2062
|
-
id: toolCall.id || "",
|
|
2063
|
-
name: toolCall.function.name,
|
|
2064
|
-
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
2065
|
-
}
|
|
2066
|
-
});
|
|
2067
|
-
}
|
|
2068
|
-
}
|
|
2069
|
-
}
|
|
2070
|
-
return new LlmResponse({
|
|
2071
|
-
content: parts.length > 0 ? {
|
|
2072
|
-
role: "model",
|
|
2073
|
-
parts
|
|
2074
|
-
} : void 0,
|
|
2075
|
-
usageMetadata: usage ? {
|
|
2076
|
-
promptTokenCount: usage.prompt_tokens,
|
|
2077
|
-
candidatesTokenCount: usage.completion_tokens,
|
|
2078
|
-
totalTokenCount: usage.total_tokens
|
|
2079
|
-
} : void 0
|
|
2080
|
-
});
|
|
2021
|
+
hasInlineData(response) {
|
|
2022
|
+
const parts = _optionalChain([response, 'access', _83 => _83.candidates, 'optionalAccess', _84 => _84[0], 'optionalAccess', _85 => _85.content, 'optionalAccess', _86 => _86.parts]);
|
|
2023
|
+
return _optionalChain([parts, 'optionalAccess', _87 => _87.some, 'call', _88 => _88((part) => _optionalChain([part, 'optionalAccess', _89 => _89.inlineData]))]) || false;
|
|
2081
2024
|
}
|
|
2082
2025
|
/**
|
|
2083
|
-
* Convert
|
|
2026
|
+
* Convert LlmRequest contents to GoogleGenAI format
|
|
2084
2027
|
*/
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
}
|
|
2091
|
-
if (message.tool_calls) {
|
|
2092
|
-
for (const toolCall of message.tool_calls) {
|
|
2093
|
-
if (toolCall.type === "function") {
|
|
2094
|
-
parts.push({
|
|
2095
|
-
functionCall: {
|
|
2096
|
-
id: toolCall.id,
|
|
2097
|
-
name: toolCall.function.name,
|
|
2098
|
-
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
2099
|
-
}
|
|
2100
|
-
});
|
|
2101
|
-
}
|
|
2102
|
-
}
|
|
2103
|
-
}
|
|
2104
|
-
return new LlmResponse({
|
|
2105
|
-
content: {
|
|
2106
|
-
role: "model",
|
|
2107
|
-
parts
|
|
2108
|
-
},
|
|
2109
|
-
usageMetadata: usage ? {
|
|
2110
|
-
promptTokenCount: usage.prompt_tokens,
|
|
2111
|
-
candidatesTokenCount: usage.completion_tokens,
|
|
2112
|
-
totalTokenCount: usage.total_tokens
|
|
2113
|
-
} : void 0,
|
|
2114
|
-
finishReason: this.toAdkFinishReason(choice.finish_reason)
|
|
2115
|
-
});
|
|
2028
|
+
convertContents(contents) {
|
|
2029
|
+
return contents.map((content) => ({
|
|
2030
|
+
role: content.role === "assistant" ? "model" : content.role,
|
|
2031
|
+
parts: content.parts || [{ text: content.content || "" }]
|
|
2032
|
+
}));
|
|
2116
2033
|
}
|
|
2117
2034
|
/**
|
|
2118
|
-
*
|
|
2035
|
+
* Preprocesses the request based on the API backend.
|
|
2119
2036
|
*/
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
);
|
|
2132
|
-
return {
|
|
2133
|
-
role: "assistant",
|
|
2134
|
-
tool_calls: [
|
|
2135
|
-
{
|
|
2136
|
-
id: functionCallPart.functionCall.id || "",
|
|
2137
|
-
type: "function",
|
|
2138
|
-
function: {
|
|
2139
|
-
name: functionCallPart.functionCall.name,
|
|
2140
|
-
arguments: JSON.stringify(
|
|
2141
|
-
functionCallPart.functionCall.args || {}
|
|
2142
|
-
)
|
|
2143
|
-
}
|
|
2037
|
+
preprocessRequest(llmRequest) {
|
|
2038
|
+
if (this.apiBackend === "GEMINI_API" /* GEMINI_API */) {
|
|
2039
|
+
if (llmRequest.config) {
|
|
2040
|
+
llmRequest.config.labels = void 0;
|
|
2041
|
+
}
|
|
2042
|
+
if (llmRequest.contents) {
|
|
2043
|
+
for (const content of llmRequest.contents) {
|
|
2044
|
+
if (!content.parts) continue;
|
|
2045
|
+
for (const part of content.parts) {
|
|
2046
|
+
this.removeDisplayNameIfPresent(part.inlineData);
|
|
2047
|
+
this.removeDisplayNameIfPresent(part.fileData);
|
|
2144
2048
|
}
|
|
2145
|
-
]
|
|
2146
|
-
};
|
|
2147
|
-
}
|
|
2148
|
-
if (_optionalChain([content, 'access', _109 => _109.parts, 'optionalAccess', _110 => _110.some, 'call', _111 => _111((part) => part.functionResponse)])) {
|
|
2149
|
-
const functionResponsePart = content.parts.find(
|
|
2150
|
-
(part) => part.functionResponse
|
|
2151
|
-
);
|
|
2152
|
-
return {
|
|
2153
|
-
role: "tool",
|
|
2154
|
-
tool_call_id: functionResponsePart.functionResponse.id || "",
|
|
2155
|
-
content: JSON.stringify(
|
|
2156
|
-
functionResponsePart.functionResponse.response || {}
|
|
2157
|
-
)
|
|
2158
|
-
};
|
|
2159
|
-
}
|
|
2160
|
-
if (_optionalChain([content, 'access', _112 => _112.parts, 'optionalAccess', _113 => _113.length]) === 1 && content.parts[0].text) {
|
|
2161
|
-
return {
|
|
2162
|
-
role,
|
|
2163
|
-
content: content.parts[0].text
|
|
2164
|
-
};
|
|
2165
|
-
}
|
|
2166
|
-
return {
|
|
2167
|
-
role,
|
|
2168
|
-
content: (content.parts || []).map(
|
|
2169
|
-
(part) => this.partToOpenAiContent(part)
|
|
2170
|
-
)
|
|
2171
|
-
};
|
|
2172
|
-
}
|
|
2173
|
-
/**
|
|
2174
|
-
* Convert ADK Part to OpenAI message content
|
|
2175
|
-
*/
|
|
2176
|
-
partToOpenAiContent(part) {
|
|
2177
|
-
if (part.text) {
|
|
2178
|
-
return {
|
|
2179
|
-
type: "text",
|
|
2180
|
-
text: part.text
|
|
2181
|
-
};
|
|
2182
|
-
}
|
|
2183
|
-
if (_optionalChain([part, 'access', _114 => _114.inline_data, 'optionalAccess', _115 => _115.mime_type]) && _optionalChain([part, 'access', _116 => _116.inline_data, 'optionalAccess', _117 => _117.data])) {
|
|
2184
|
-
return {
|
|
2185
|
-
type: "image_url",
|
|
2186
|
-
image_url: {
|
|
2187
|
-
url: `data:${part.inline_data.mime_type};base64,${part.inline_data.data}`
|
|
2188
2049
|
}
|
|
2189
|
-
}
|
|
2050
|
+
}
|
|
2190
2051
|
}
|
|
2191
|
-
throw new Error("Unsupported part type for OpenAI conversion");
|
|
2192
2052
|
}
|
|
2193
2053
|
/**
|
|
2194
|
-
*
|
|
2054
|
+
* Sets display_name to null for the Gemini API (non-Vertex) backend.
|
|
2195
2055
|
*/
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
name: functionDeclaration.name,
|
|
2201
|
-
description: functionDeclaration.description || "",
|
|
2202
|
-
parameters: functionDeclaration.parameters || {}
|
|
2203
|
-
}
|
|
2204
|
-
};
|
|
2056
|
+
removeDisplayNameIfPresent(dataObj) {
|
|
2057
|
+
if (_optionalChain([dataObj, 'optionalAccess', _90 => _90.displayName])) {
|
|
2058
|
+
dataObj.displayName = null;
|
|
2059
|
+
}
|
|
2205
2060
|
}
|
|
2206
2061
|
/**
|
|
2207
|
-
*
|
|
2062
|
+
* Builds function declaration log string.
|
|
2208
2063
|
*/
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
if (role === "system") {
|
|
2214
|
-
return "system";
|
|
2064
|
+
buildFunctionDeclarationLog(funcDecl) {
|
|
2065
|
+
let paramStr = "{}";
|
|
2066
|
+
if (_optionalChain([funcDecl, 'access', _91 => _91.parameters, 'optionalAccess', _92 => _92.properties])) {
|
|
2067
|
+
paramStr = JSON.stringify(funcDecl.parameters.properties);
|
|
2215
2068
|
}
|
|
2216
|
-
return
|
|
2069
|
+
return `${funcDecl.name}: ${paramStr}`;
|
|
2217
2070
|
}
|
|
2218
2071
|
/**
|
|
2219
|
-
*
|
|
2072
|
+
* Provides the api client.
|
|
2220
2073
|
*/
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
for (const part of content.parts) {
|
|
2242
|
-
this.preprocessPart(part);
|
|
2243
|
-
}
|
|
2244
|
-
}
|
|
2074
|
+
get apiClient() {
|
|
2075
|
+
if (!this._apiClient) {
|
|
2076
|
+
const useVertexAI = process.env.GOOGLE_GENAI_USE_VERTEXAI === "true";
|
|
2077
|
+
const apiKey = process.env.GOOGLE_API_KEY;
|
|
2078
|
+
const project = process.env.GOOGLE_CLOUD_PROJECT;
|
|
2079
|
+
const location = process.env.GOOGLE_CLOUD_LOCATION;
|
|
2080
|
+
if (useVertexAI && project && location) {
|
|
2081
|
+
this._apiClient = new (0, _genai.GoogleGenAI)({
|
|
2082
|
+
vertexai: true,
|
|
2083
|
+
project,
|
|
2084
|
+
location
|
|
2085
|
+
});
|
|
2086
|
+
} else if (apiKey) {
|
|
2087
|
+
this._apiClient = new (0, _genai.GoogleGenAI)({
|
|
2088
|
+
apiKey
|
|
2089
|
+
});
|
|
2090
|
+
} else {
|
|
2091
|
+
throw new Error(
|
|
2092
|
+
"Google API Key or Vertex AI configuration is required. Set GOOGLE_API_KEY or GOOGLE_GENAI_USE_VERTEXAI=true with GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION."
|
|
2093
|
+
);
|
|
2245
2094
|
}
|
|
2246
2095
|
}
|
|
2096
|
+
return this._apiClient;
|
|
2247
2097
|
}
|
|
2248
2098
|
/**
|
|
2249
|
-
*
|
|
2099
|
+
* Gets the API backend type.
|
|
2250
2100
|
*/
|
|
2251
|
-
|
|
2252
|
-
if (
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
}
|
|
2101
|
+
get apiBackend() {
|
|
2102
|
+
if (!this._apiBackend) {
|
|
2103
|
+
const useVertexAI = process.env.GOOGLE_GENAI_USE_VERTEXAI === "true";
|
|
2104
|
+
this._apiBackend = useVertexAI ? "VERTEX_AI" /* VERTEX_AI */ : "GEMINI_API" /* GEMINI_API */;
|
|
2256
2105
|
}
|
|
2106
|
+
return this._apiBackend;
|
|
2257
2107
|
}
|
|
2258
2108
|
/**
|
|
2259
|
-
*
|
|
2260
|
-
* This is a simplified implementation - you may need to adjust based on your specific requirements
|
|
2109
|
+
* Gets the tracking headers.
|
|
2261
2110
|
*/
|
|
2262
|
-
|
|
2263
|
-
if (
|
|
2264
|
-
|
|
2111
|
+
get trackingHeaders() {
|
|
2112
|
+
if (!this._trackingHeaders) {
|
|
2113
|
+
let frameworkLabel = "google-adk/1.0.0";
|
|
2114
|
+
if (process.env[AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME]) {
|
|
2115
|
+
frameworkLabel = `${frameworkLabel}+${AGENT_ENGINE_TELEMETRY_TAG}`;
|
|
2116
|
+
}
|
|
2117
|
+
const languageLabel = `gl-node/${process.version}`;
|
|
2118
|
+
const versionHeaderValue = `${frameworkLabel} ${languageLabel}`;
|
|
2119
|
+
this._trackingHeaders = {
|
|
2120
|
+
"x-goog-api-client": versionHeaderValue,
|
|
2121
|
+
"user-agent": versionHeaderValue
|
|
2122
|
+
};
|
|
2265
2123
|
}
|
|
2266
|
-
return
|
|
2124
|
+
return this._trackingHeaders;
|
|
2267
2125
|
}
|
|
2268
2126
|
/**
|
|
2269
|
-
*
|
|
2127
|
+
* Gets the live API version.
|
|
2270
2128
|
*/
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
return _optionalChain([parts, 'optionalAccess', _120 => _120.some, 'call', _121 => _121((part) => part.inlineData)]) || false;
|
|
2129
|
+
get liveApiVersion() {
|
|
2130
|
+
return this.apiBackend === "VERTEX_AI" /* VERTEX_AI */ ? "v1beta1" : "v1alpha";
|
|
2274
2131
|
}
|
|
2275
2132
|
/**
|
|
2276
|
-
* Gets the
|
|
2133
|
+
* Gets the live API client.
|
|
2277
2134
|
*/
|
|
2278
|
-
get
|
|
2279
|
-
if (!this.
|
|
2280
|
-
const
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2135
|
+
get liveApiClient() {
|
|
2136
|
+
if (!this._liveApiClient) {
|
|
2137
|
+
const useVertexAI = process.env.GOOGLE_GENAI_USE_VERTEXAI === "true";
|
|
2138
|
+
const apiKey = process.env.GOOGLE_API_KEY;
|
|
2139
|
+
const project = process.env.GOOGLE_CLOUD_PROJECT;
|
|
2140
|
+
const location = process.env.GOOGLE_CLOUD_LOCATION;
|
|
2141
|
+
if (useVertexAI && project && location) {
|
|
2142
|
+
this._liveApiClient = new (0, _genai.GoogleGenAI)({
|
|
2143
|
+
vertexai: true,
|
|
2144
|
+
project,
|
|
2145
|
+
location,
|
|
2146
|
+
apiVersion: this.liveApiVersion
|
|
2147
|
+
});
|
|
2148
|
+
} else if (apiKey) {
|
|
2149
|
+
this._liveApiClient = new (0, _genai.GoogleGenAI)({
|
|
2150
|
+
apiKey,
|
|
2151
|
+
apiVersion: this.liveApiVersion
|
|
2152
|
+
});
|
|
2153
|
+
} else {
|
|
2154
|
+
throw new Error("API configuration required for live client");
|
|
2285
2155
|
}
|
|
2286
|
-
this._client = new (0, _openai2.default)({
|
|
2287
|
-
apiKey
|
|
2288
|
-
});
|
|
2289
2156
|
}
|
|
2290
|
-
return this.
|
|
2157
|
+
return this._liveApiClient;
|
|
2291
2158
|
}
|
|
2292
2159
|
};
|
|
2293
2160
|
|
|
2294
|
-
// src/models/
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
var _ai = require('ai');
|
|
2301
|
-
var AiSdkLlm = (_class7 = class extends BaseLlm {
|
|
2161
|
+
// src/models/openai-llm.ts
|
|
2162
|
+
var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);
|
|
2163
|
+
var OpenAiLlm = class extends BaseLlm {
|
|
2302
2164
|
|
|
2303
|
-
__init13() {this.logger = new Logger({ name: "AiSdkLlm" })}
|
|
2304
2165
|
/**
|
|
2305
|
-
* Constructor
|
|
2306
|
-
* @param model - Pre-configured LanguageModel from provider(modelName)
|
|
2166
|
+
* Constructor for OpenAI LLM
|
|
2307
2167
|
*/
|
|
2308
|
-
constructor(
|
|
2309
|
-
super(
|
|
2310
|
-
this.modelInstance = modelInstance;
|
|
2168
|
+
constructor(model = "gpt-4o-mini") {
|
|
2169
|
+
super(model);
|
|
2311
2170
|
}
|
|
2312
2171
|
/**
|
|
2313
|
-
*
|
|
2172
|
+
* Provides the list of supported models
|
|
2314
2173
|
*/
|
|
2315
2174
|
static supportedModels() {
|
|
2316
|
-
return [];
|
|
2175
|
+
return ["gpt-3.5-.*", "gpt-4.*", "gpt-4o.*", "o1-.*", "o3-.*"];
|
|
2317
2176
|
}
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2177
|
+
/**
|
|
2178
|
+
* Main content generation method - handles both streaming and non-streaming
|
|
2179
|
+
*/
|
|
2180
|
+
async *generateContentAsyncImpl(llmRequest, stream = false) {
|
|
2181
|
+
this.preprocessRequest(llmRequest);
|
|
2182
|
+
const model = llmRequest.model || this.model;
|
|
2183
|
+
const messages = (llmRequest.contents || []).map(
|
|
2184
|
+
(content) => this.contentToOpenAiMessage(content)
|
|
2185
|
+
);
|
|
2186
|
+
let tools;
|
|
2187
|
+
if (_optionalChain([llmRequest, 'access', _93 => _93.config, 'optionalAccess', _94 => _94.tools, 'optionalAccess', _95 => _95[0], 'optionalAccess', _96 => _96.functionDeclarations])) {
|
|
2188
|
+
tools = llmRequest.config.tools[0].functionDeclarations.map(
|
|
2189
|
+
(funcDecl) => this.functionDeclarationToOpenAiTool(funcDecl)
|
|
2190
|
+
);
|
|
2191
|
+
}
|
|
2192
|
+
const systemContent = llmRequest.getSystemInstructionText();
|
|
2193
|
+
if (systemContent) {
|
|
2194
|
+
messages.unshift({
|
|
2195
|
+
role: "system",
|
|
2196
|
+
content: systemContent
|
|
2197
|
+
});
|
|
2198
|
+
}
|
|
2199
|
+
const openAiMessages = messages;
|
|
2200
|
+
const requestParams = {
|
|
2201
|
+
model,
|
|
2202
|
+
messages: openAiMessages,
|
|
2203
|
+
tools,
|
|
2204
|
+
tool_choice: tools ? "auto" : void 0,
|
|
2205
|
+
max_tokens: _optionalChain([llmRequest, 'access', _97 => _97.config, 'optionalAccess', _98 => _98.maxOutputTokens]),
|
|
2206
|
+
temperature: _optionalChain([llmRequest, 'access', _99 => _99.config, 'optionalAccess', _100 => _100.temperature]),
|
|
2207
|
+
top_p: _optionalChain([llmRequest, 'access', _101 => _101.config, 'optionalAccess', _102 => _102.topP]),
|
|
2208
|
+
stream
|
|
2209
|
+
};
|
|
2210
|
+
if (stream) {
|
|
2211
|
+
const streamResponse = await this.client.chat.completions.create({
|
|
2212
|
+
...requestParams,
|
|
2213
|
+
stream: true
|
|
2214
|
+
});
|
|
2215
|
+
let thoughtText = "";
|
|
2216
|
+
let text = "";
|
|
2217
|
+
let usageMetadata;
|
|
2218
|
+
const accumulatedToolCalls = [];
|
|
2219
|
+
for await (const chunk of streamResponse) {
|
|
2220
|
+
const choice = chunk.choices[0];
|
|
2221
|
+
if (!choice) continue;
|
|
2222
|
+
const delta = choice.delta;
|
|
2223
|
+
const llmResponse = this.createChunkResponse(delta, chunk.usage);
|
|
2224
|
+
if (chunk.usage) {
|
|
2225
|
+
usageMetadata = chunk.usage;
|
|
2226
|
+
}
|
|
2227
|
+
if (_optionalChain([llmResponse, 'access', _103 => _103.content, 'optionalAccess', _104 => _104.parts, 'optionalAccess', _105 => _105[0], 'optionalAccess', _106 => _106.text])) {
|
|
2228
|
+
const part0 = llmResponse.content.parts[0];
|
|
2229
|
+
if (part0.thought) {
|
|
2230
|
+
thoughtText += part0.text;
|
|
2231
|
+
} else {
|
|
2232
|
+
text += part0.text;
|
|
2233
|
+
}
|
|
2234
|
+
llmResponse.partial = true;
|
|
2235
|
+
} else if ((thoughtText || text) && (!llmResponse.content || !llmResponse.content.parts || !this.hasInlineData(llmResponse))) {
|
|
2236
|
+
const parts = [];
|
|
2237
|
+
if (thoughtText) {
|
|
2238
|
+
parts.push({ text: thoughtText, thought: true });
|
|
2239
|
+
}
|
|
2240
|
+
if (text) {
|
|
2241
|
+
parts.push({ text });
|
|
2242
|
+
}
|
|
2337
2243
|
yield new LlmResponse({
|
|
2338
2244
|
content: {
|
|
2339
|
-
|
|
2340
|
-
|
|
2245
|
+
parts,
|
|
2246
|
+
role: "model"
|
|
2341
2247
|
},
|
|
2342
|
-
|
|
2248
|
+
usageMetadata: usageMetadata ? {
|
|
2249
|
+
promptTokenCount: usageMetadata.prompt_tokens,
|
|
2250
|
+
candidatesTokenCount: usageMetadata.completion_tokens,
|
|
2251
|
+
totalTokenCount: usageMetadata.total_tokens
|
|
2252
|
+
} : void 0
|
|
2343
2253
|
});
|
|
2254
|
+
thoughtText = "";
|
|
2255
|
+
text = "";
|
|
2344
2256
|
}
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
}
|
|
2257
|
+
if (delta.tool_calls) {
|
|
2258
|
+
for (const toolCall of delta.tool_calls) {
|
|
2259
|
+
const index = toolCall.index || 0;
|
|
2260
|
+
if (!accumulatedToolCalls[index]) {
|
|
2261
|
+
accumulatedToolCalls[index] = {
|
|
2262
|
+
index,
|
|
2263
|
+
id: toolCall.id || "",
|
|
2264
|
+
type: "function",
|
|
2265
|
+
function: { name: "", arguments: "" }
|
|
2266
|
+
};
|
|
2267
|
+
}
|
|
2268
|
+
if (_optionalChain([toolCall, 'access', _107 => _107.function, 'optionalAccess', _108 => _108.name])) {
|
|
2269
|
+
accumulatedToolCalls[index].function.name += toolCall.function.name;
|
|
2270
|
+
}
|
|
2271
|
+
if (_optionalChain([toolCall, 'access', _109 => _109.function, 'optionalAccess', _110 => _110.arguments])) {
|
|
2272
|
+
accumulatedToolCalls[index].function.arguments += toolCall.function.arguments;
|
|
2273
|
+
}
|
|
2359
2274
|
}
|
|
2360
2275
|
}
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
if (result.text) {
|
|
2380
|
-
parts.push({ text: result.text });
|
|
2381
|
-
}
|
|
2382
|
-
if (result.toolCalls && result.toolCalls.length > 0) {
|
|
2383
|
-
for (const toolCall of result.toolCalls) {
|
|
2384
|
-
parts.push({
|
|
2385
|
-
functionCall: {
|
|
2386
|
-
id: toolCall.toolCallId,
|
|
2387
|
-
name: toolCall.toolName,
|
|
2388
|
-
args: toolCall.args
|
|
2276
|
+
if (choice.finish_reason) {
|
|
2277
|
+
const parts = [];
|
|
2278
|
+
if (thoughtText) {
|
|
2279
|
+
parts.push({ text: thoughtText, thought: true });
|
|
2280
|
+
}
|
|
2281
|
+
if (text) {
|
|
2282
|
+
parts.push({ text });
|
|
2283
|
+
}
|
|
2284
|
+
if (accumulatedToolCalls.length > 0) {
|
|
2285
|
+
for (const toolCall of accumulatedToolCalls) {
|
|
2286
|
+
if (_optionalChain([toolCall, 'access', _111 => _111.function, 'optionalAccess', _112 => _112.name])) {
|
|
2287
|
+
parts.push({
|
|
2288
|
+
functionCall: {
|
|
2289
|
+
id: toolCall.id,
|
|
2290
|
+
name: toolCall.function.name,
|
|
2291
|
+
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
2292
|
+
}
|
|
2293
|
+
});
|
|
2389
2294
|
}
|
|
2390
|
-
}
|
|
2295
|
+
}
|
|
2391
2296
|
}
|
|
2297
|
+
const finalResponse = new LlmResponse({
|
|
2298
|
+
content: {
|
|
2299
|
+
role: "model",
|
|
2300
|
+
parts
|
|
2301
|
+
},
|
|
2302
|
+
usageMetadata: usageMetadata ? {
|
|
2303
|
+
promptTokenCount: usageMetadata.prompt_tokens,
|
|
2304
|
+
candidatesTokenCount: usageMetadata.completion_tokens,
|
|
2305
|
+
totalTokenCount: usageMetadata.total_tokens
|
|
2306
|
+
} : void 0,
|
|
2307
|
+
finishReason: this.toAdkFinishReason(choice.finish_reason)
|
|
2308
|
+
});
|
|
2309
|
+
yield finalResponse;
|
|
2310
|
+
} else {
|
|
2311
|
+
yield llmResponse;
|
|
2312
|
+
}
|
|
2313
|
+
}
|
|
2314
|
+
if ((text || thoughtText) && usageMetadata) {
|
|
2315
|
+
const parts = [];
|
|
2316
|
+
if (thoughtText) {
|
|
2317
|
+
parts.push({ text: thoughtText, thought: true });
|
|
2318
|
+
}
|
|
2319
|
+
if (text) {
|
|
2320
|
+
parts.push({ text });
|
|
2392
2321
|
}
|
|
2393
2322
|
yield new LlmResponse({
|
|
2394
2323
|
content: {
|
|
2395
|
-
|
|
2396
|
-
|
|
2324
|
+
parts,
|
|
2325
|
+
role: "model"
|
|
2397
2326
|
},
|
|
2398
|
-
usageMetadata:
|
|
2399
|
-
promptTokenCount:
|
|
2400
|
-
candidatesTokenCount:
|
|
2401
|
-
totalTokenCount:
|
|
2402
|
-
}
|
|
2403
|
-
finishReason: this.mapFinishReason(result.finishReason),
|
|
2404
|
-
turnComplete: true
|
|
2327
|
+
usageMetadata: {
|
|
2328
|
+
promptTokenCount: usageMetadata.prompt_tokens,
|
|
2329
|
+
candidatesTokenCount: usageMetadata.completion_tokens,
|
|
2330
|
+
totalTokenCount: usageMetadata.total_tokens
|
|
2331
|
+
}
|
|
2405
2332
|
});
|
|
2406
2333
|
}
|
|
2407
|
-
}
|
|
2408
|
-
this.
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
model: this.model
|
|
2334
|
+
} else {
|
|
2335
|
+
const response = await this.client.chat.completions.create({
|
|
2336
|
+
...requestParams,
|
|
2337
|
+
stream: false
|
|
2412
2338
|
});
|
|
2339
|
+
const choice = response.choices[0];
|
|
2340
|
+
if (choice) {
|
|
2341
|
+
const llmResponse = this.openAiMessageToLlmResponse(
|
|
2342
|
+
choice,
|
|
2343
|
+
response.usage
|
|
2344
|
+
);
|
|
2345
|
+
this.logger.debug(
|
|
2346
|
+
`OpenAI response: ${_optionalChain([response, 'access', _113 => _113.usage, 'optionalAccess', _114 => _114.completion_tokens]) || 0} tokens`
|
|
2347
|
+
);
|
|
2348
|
+
yield llmResponse;
|
|
2349
|
+
}
|
|
2413
2350
|
}
|
|
2414
2351
|
}
|
|
2415
2352
|
/**
|
|
2416
|
-
*
|
|
2353
|
+
* Live connection is not supported for OpenAI models
|
|
2417
2354
|
*/
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
for (const content of llmRequest.contents || []) {
|
|
2421
|
-
const message = this.contentToAiSdkMessage(content);
|
|
2422
|
-
if (message) {
|
|
2423
|
-
messages.push(message);
|
|
2424
|
-
}
|
|
2425
|
-
}
|
|
2426
|
-
return messages;
|
|
2355
|
+
connect(_llmRequest) {
|
|
2356
|
+
throw new Error(`Live connection is not supported for ${this.model}.`);
|
|
2427
2357
|
}
|
|
2428
2358
|
/**
|
|
2429
|
-
*
|
|
2359
|
+
* Create LlmResponse from streaming chunk - similar to Google's LlmResponse.create
|
|
2430
2360
|
*/
|
|
2431
|
-
|
|
2432
|
-
const
|
|
2433
|
-
if (
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2361
|
+
createChunkResponse(delta, usage) {
|
|
2362
|
+
const parts = [];
|
|
2363
|
+
if (delta.content) {
|
|
2364
|
+
const contentType = this.getContentType(delta.content);
|
|
2365
|
+
if (contentType === "thought") {
|
|
2366
|
+
parts.push({ text: delta.content, thought: true });
|
|
2367
|
+
} else {
|
|
2368
|
+
parts.push({ text: delta.content });
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
if (delta.tool_calls) {
|
|
2372
|
+
for (const toolCall of delta.tool_calls) {
|
|
2373
|
+
if (toolCall.type === "function" && _optionalChain([toolCall, 'access', _115 => _115.function, 'optionalAccess', _116 => _116.name])) {
|
|
2374
|
+
parts.push({
|
|
2375
|
+
functionCall: {
|
|
2376
|
+
id: toolCall.id || "",
|
|
2377
|
+
name: toolCall.function.name,
|
|
2378
|
+
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
2379
|
+
}
|
|
2380
|
+
});
|
|
2442
2381
|
}
|
|
2443
2382
|
}
|
|
2444
2383
|
}
|
|
2445
|
-
return
|
|
2384
|
+
return new LlmResponse({
|
|
2385
|
+
content: parts.length > 0 ? {
|
|
2386
|
+
role: "model",
|
|
2387
|
+
parts
|
|
2388
|
+
} : void 0,
|
|
2389
|
+
usageMetadata: usage ? {
|
|
2390
|
+
promptTokenCount: usage.prompt_tokens,
|
|
2391
|
+
candidatesTokenCount: usage.completion_tokens,
|
|
2392
|
+
totalTokenCount: usage.total_tokens
|
|
2393
|
+
} : void 0
|
|
2394
|
+
});
|
|
2446
2395
|
}
|
|
2447
2396
|
/**
|
|
2448
|
-
* Convert
|
|
2397
|
+
* Convert OpenAI message to ADK LlmResponse
|
|
2449
2398
|
*/
|
|
2450
|
-
|
|
2451
|
-
const
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
if (content.parts.length === 1 && content.parts[0].text) {
|
|
2456
|
-
const textContent = content.parts[0].text;
|
|
2457
|
-
if (role === "system") {
|
|
2458
|
-
return { role: "system", content: textContent };
|
|
2459
|
-
}
|
|
2460
|
-
if (role === "assistant") {
|
|
2461
|
-
return { role: "assistant", content: textContent };
|
|
2462
|
-
}
|
|
2463
|
-
return { role: "user", content: textContent };
|
|
2399
|
+
openAiMessageToLlmResponse(choice, usage) {
|
|
2400
|
+
const message = choice.message;
|
|
2401
|
+
const parts = [];
|
|
2402
|
+
if (message.content) {
|
|
2403
|
+
parts.push({ text: message.content });
|
|
2464
2404
|
}
|
|
2465
|
-
if (
|
|
2466
|
-
const
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
});
|
|
2475
|
-
}
|
|
2476
|
-
}
|
|
2477
|
-
for (const funcPart of functionCalls) {
|
|
2478
|
-
if (funcPart.functionCall) {
|
|
2479
|
-
contentParts2.push({
|
|
2480
|
-
type: "tool-call",
|
|
2481
|
-
toolCallId: funcPart.functionCall.id,
|
|
2482
|
-
toolName: funcPart.functionCall.name,
|
|
2483
|
-
args: funcPart.functionCall.args
|
|
2405
|
+
if (message.tool_calls) {
|
|
2406
|
+
for (const toolCall of message.tool_calls) {
|
|
2407
|
+
if (toolCall.type === "function") {
|
|
2408
|
+
parts.push({
|
|
2409
|
+
functionCall: {
|
|
2410
|
+
id: toolCall.id,
|
|
2411
|
+
name: toolCall.function.name,
|
|
2412
|
+
args: JSON.parse(toolCall.function.arguments || "{}")
|
|
2413
|
+
}
|
|
2484
2414
|
});
|
|
2485
2415
|
}
|
|
2486
2416
|
}
|
|
2417
|
+
}
|
|
2418
|
+
return new LlmResponse({
|
|
2419
|
+
content: {
|
|
2420
|
+
role: "model",
|
|
2421
|
+
parts
|
|
2422
|
+
},
|
|
2423
|
+
usageMetadata: usage ? {
|
|
2424
|
+
promptTokenCount: usage.prompt_tokens,
|
|
2425
|
+
candidatesTokenCount: usage.completion_tokens,
|
|
2426
|
+
totalTokenCount: usage.total_tokens
|
|
2427
|
+
} : void 0,
|
|
2428
|
+
finishReason: this.toAdkFinishReason(choice.finish_reason)
|
|
2429
|
+
});
|
|
2430
|
+
}
|
|
2431
|
+
/**
|
|
2432
|
+
* Convert ADK Content to OpenAI ChatCompletionMessage
|
|
2433
|
+
*/
|
|
2434
|
+
contentToOpenAiMessage(content) {
|
|
2435
|
+
const role = this.toOpenAiRole(content.role);
|
|
2436
|
+
if (role === "system") {
|
|
2437
|
+
return {
|
|
2438
|
+
role: "system",
|
|
2439
|
+
content: _optionalChain([content, 'access', _117 => _117.parts, 'optionalAccess', _118 => _118[0], 'optionalAccess', _119 => _119.text]) || ""
|
|
2440
|
+
};
|
|
2441
|
+
}
|
|
2442
|
+
if (_optionalChain([content, 'access', _120 => _120.parts, 'optionalAccess', _121 => _121.some, 'call', _122 => _122((part) => part.functionCall)])) {
|
|
2443
|
+
const functionCallPart = content.parts.find(
|
|
2444
|
+
(part) => part.functionCall
|
|
2445
|
+
);
|
|
2487
2446
|
return {
|
|
2488
2447
|
role: "assistant",
|
|
2489
|
-
|
|
2448
|
+
tool_calls: [
|
|
2449
|
+
{
|
|
2450
|
+
id: functionCallPart.functionCall.id || "",
|
|
2451
|
+
type: "function",
|
|
2452
|
+
function: {
|
|
2453
|
+
name: functionCallPart.functionCall.name,
|
|
2454
|
+
arguments: JSON.stringify(
|
|
2455
|
+
functionCallPart.functionCall.args || {}
|
|
2456
|
+
)
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2459
|
+
]
|
|
2490
2460
|
};
|
|
2491
2461
|
}
|
|
2492
|
-
if (_optionalChain([content, 'access',
|
|
2493
|
-
const
|
|
2462
|
+
if (_optionalChain([content, 'access', _123 => _123.parts, 'optionalAccess', _124 => _124.some, 'call', _125 => _125((part) => part.functionResponse)])) {
|
|
2463
|
+
const functionResponsePart = content.parts.find(
|
|
2494
2464
|
(part) => part.functionResponse
|
|
2495
2465
|
);
|
|
2496
|
-
const contentParts2 = functionResponses.map((part) => ({
|
|
2497
|
-
type: "tool-result",
|
|
2498
|
-
toolCallId: part.functionResponse.id,
|
|
2499
|
-
toolName: part.functionResponse.name || "unknown",
|
|
2500
|
-
result: part.functionResponse.response
|
|
2501
|
-
}));
|
|
2502
2466
|
return {
|
|
2503
2467
|
role: "tool",
|
|
2504
|
-
|
|
2468
|
+
tool_call_id: functionResponsePart.functionResponse.id || "",
|
|
2469
|
+
content: JSON.stringify(
|
|
2470
|
+
functionResponsePart.functionResponse.response || {}
|
|
2471
|
+
)
|
|
2505
2472
|
};
|
|
2506
2473
|
}
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2474
|
+
if (_optionalChain([content, 'access', _126 => _126.parts, 'optionalAccess', _127 => _127.length]) === 1 && content.parts[0].text) {
|
|
2475
|
+
return {
|
|
2476
|
+
role,
|
|
2477
|
+
content: content.parts[0].text
|
|
2478
|
+
};
|
|
2479
|
+
}
|
|
2480
|
+
return {
|
|
2481
|
+
role,
|
|
2482
|
+
content: (content.parts || []).map(
|
|
2483
|
+
(part) => this.partToOpenAiContent(part)
|
|
2484
|
+
)
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
* Convert ADK Part to OpenAI message content
|
|
2489
|
+
*/
|
|
2490
|
+
partToOpenAiContent(part) {
|
|
2491
|
+
if (part.text) {
|
|
2492
|
+
return {
|
|
2493
|
+
type: "text",
|
|
2494
|
+
text: part.text
|
|
2495
|
+
};
|
|
2496
|
+
}
|
|
2497
|
+
if (_optionalChain([part, 'access', _128 => _128.inline_data, 'optionalAccess', _129 => _129.mime_type]) && _optionalChain([part, 'access', _130 => _130.inline_data, 'optionalAccess', _131 => _131.data])) {
|
|
2498
|
+
return {
|
|
2499
|
+
type: "image_url",
|
|
2500
|
+
image_url: {
|
|
2501
|
+
url: `data:${part.inline_data.mime_type};base64,${part.inline_data.data}`
|
|
2502
|
+
}
|
|
2503
|
+
};
|
|
2504
|
+
}
|
|
2505
|
+
throw new Error("Unsupported part type for OpenAI conversion");
|
|
2506
|
+
}
|
|
2507
|
+
/**
|
|
2508
|
+
* Transform JSON schema to use lowercase types for OpenAI compatibility
|
|
2509
|
+
*/
|
|
2510
|
+
transformSchemaForOpenAi(schema) {
|
|
2511
|
+
if (Array.isArray(schema)) {
|
|
2512
|
+
return schema.map((item) => this.transformSchemaForOpenAi(item));
|
|
2513
|
+
}
|
|
2514
|
+
if (!schema || typeof schema !== "object") {
|
|
2515
|
+
return schema;
|
|
2516
|
+
}
|
|
2517
|
+
const transformedSchema = { ...schema };
|
|
2518
|
+
if (transformedSchema.type && typeof transformedSchema.type === "string") {
|
|
2519
|
+
transformedSchema.type = transformedSchema.type.toLowerCase();
|
|
2520
|
+
}
|
|
2521
|
+
if (transformedSchema.properties) {
|
|
2522
|
+
transformedSchema.properties = Object.fromEntries(
|
|
2523
|
+
Object.entries(transformedSchema.properties).map(([key, value]) => [
|
|
2524
|
+
key,
|
|
2525
|
+
this.transformSchemaForOpenAi(value)
|
|
2526
|
+
])
|
|
2527
|
+
);
|
|
2528
|
+
}
|
|
2529
|
+
if (transformedSchema.items) {
|
|
2530
|
+
transformedSchema.items = this.transformSchemaForOpenAi(
|
|
2531
|
+
transformedSchema.items
|
|
2532
|
+
);
|
|
2533
|
+
}
|
|
2534
|
+
const arrayKeywords = ["anyOf", "oneOf", "allOf"];
|
|
2535
|
+
for (const keyword of arrayKeywords) {
|
|
2536
|
+
if (transformedSchema[keyword]) {
|
|
2537
|
+
transformedSchema[keyword] = this.transformSchemaForOpenAi(
|
|
2538
|
+
transformedSchema[keyword]
|
|
2539
|
+
);
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
return transformedSchema;
|
|
2543
|
+
}
|
|
2544
|
+
/**
|
|
2545
|
+
* Convert ADK function declaration to OpenAI tool
|
|
2546
|
+
*/
|
|
2547
|
+
functionDeclarationToOpenAiTool(functionDeclaration) {
|
|
2548
|
+
return {
|
|
2549
|
+
type: "function",
|
|
2550
|
+
function: {
|
|
2551
|
+
name: functionDeclaration.name,
|
|
2552
|
+
description: functionDeclaration.description || "",
|
|
2553
|
+
parameters: this.transformSchemaForOpenAi(
|
|
2554
|
+
functionDeclaration.parameters || {}
|
|
2555
|
+
)
|
|
2556
|
+
}
|
|
2557
|
+
};
|
|
2558
|
+
}
|
|
2559
|
+
/**
|
|
2560
|
+
* Convert ADK role to OpenAI role format
|
|
2561
|
+
*/
|
|
2562
|
+
toOpenAiRole(role) {
|
|
2563
|
+
if (role === "model") {
|
|
2564
|
+
return "assistant";
|
|
2565
|
+
}
|
|
2566
|
+
if (role === "system") {
|
|
2567
|
+
return "system";
|
|
2568
|
+
}
|
|
2569
|
+
return "user";
|
|
2570
|
+
}
|
|
2571
|
+
/**
|
|
2572
|
+
* Convert OpenAI finish reason to ADK finish reason
|
|
2573
|
+
*/
|
|
2574
|
+
toAdkFinishReason(openaiFinishReason) {
|
|
2575
|
+
switch (openaiFinishReason) {
|
|
2576
|
+
case "stop":
|
|
2577
|
+
case "tool_calls":
|
|
2578
|
+
return "STOP";
|
|
2579
|
+
case "length":
|
|
2580
|
+
return "MAX_TOKENS";
|
|
2581
|
+
default:
|
|
2582
|
+
return "FINISH_REASON_UNSPECIFIED";
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
/**
|
|
2586
|
+
* Preprocess request similar to Google LLM
|
|
2587
|
+
*/
|
|
2588
|
+
preprocessRequest(llmRequest) {
|
|
2589
|
+
if (llmRequest.config) {
|
|
2590
|
+
llmRequest.config.labels = void 0;
|
|
2591
|
+
if (llmRequest.contents) {
|
|
2592
|
+
for (const content of llmRequest.contents) {
|
|
2593
|
+
if (!content.parts) continue;
|
|
2594
|
+
for (const part of content.parts) {
|
|
2595
|
+
this.preprocessPart(part);
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2514
2598
|
}
|
|
2515
2599
|
}
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
if (role === "assistant") {
|
|
2525
|
-
return { role: "assistant", content: textContent };
|
|
2600
|
+
}
|
|
2601
|
+
/**
|
|
2602
|
+
* Preprocess individual parts for OpenAI compatibility
|
|
2603
|
+
*/
|
|
2604
|
+
preprocessPart(part) {
|
|
2605
|
+
if (part.inline_data) {
|
|
2606
|
+
if (!part.inline_data.mime_type || !part.inline_data.data) {
|
|
2607
|
+
delete part.inline_data;
|
|
2526
2608
|
}
|
|
2527
|
-
return { role: "user", content: textContent };
|
|
2528
|
-
}
|
|
2529
|
-
if (role === "system") {
|
|
2530
|
-
const combinedText = contentParts.map((p) => p.text).join("");
|
|
2531
|
-
return { role: "system", content: combinedText };
|
|
2532
|
-
}
|
|
2533
|
-
if (role === "assistant") {
|
|
2534
|
-
return { role: "assistant", content: contentParts };
|
|
2535
2609
|
}
|
|
2536
|
-
return { role: "user", content: contentParts };
|
|
2537
2610
|
}
|
|
2538
2611
|
/**
|
|
2539
|
-
*
|
|
2612
|
+
* Detect content type for flow control
|
|
2613
|
+
* This is a simplified implementation - you may need to adjust based on your specific requirements
|
|
2540
2614
|
*/
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
case "assistant":
|
|
2545
|
-
return "assistant";
|
|
2546
|
-
case "system":
|
|
2547
|
-
return "system";
|
|
2548
|
-
default:
|
|
2549
|
-
return "user";
|
|
2615
|
+
getContentType(content) {
|
|
2616
|
+
if (content.includes("<thinking>") || content.includes("[thinking]")) {
|
|
2617
|
+
return "thought";
|
|
2550
2618
|
}
|
|
2619
|
+
return "regular";
|
|
2551
2620
|
}
|
|
2552
2621
|
/**
|
|
2553
|
-
*
|
|
2622
|
+
* Check if response has inline data (similar to Google LLM)
|
|
2554
2623
|
*/
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2624
|
+
hasInlineData(response) {
|
|
2625
|
+
const parts = _optionalChain([response, 'access', _132 => _132.content, 'optionalAccess', _133 => _133.parts]);
|
|
2626
|
+
return _optionalChain([parts, 'optionalAccess', _134 => _134.some, 'call', _135 => _135((part) => part.inlineData)]) || false;
|
|
2627
|
+
}
|
|
2628
|
+
/**
|
|
2629
|
+
* Gets the OpenAI client
|
|
2630
|
+
*/
|
|
2631
|
+
get client() {
|
|
2632
|
+
if (!this._client) {
|
|
2633
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
2634
|
+
if (!apiKey) {
|
|
2635
|
+
throw new Error(
|
|
2636
|
+
"OPENAI_API_KEY environment variable is required for OpenAI models"
|
|
2637
|
+
);
|
|
2638
|
+
}
|
|
2639
|
+
this._client = new (0, _openai2.default)({
|
|
2640
|
+
apiKey
|
|
2641
|
+
});
|
|
2565
2642
|
}
|
|
2643
|
+
return this._client;
|
|
2566
2644
|
}
|
|
2567
|
-
}
|
|
2645
|
+
};
|
|
2568
2646
|
|
|
2569
2647
|
// src/models/llm-registry.ts
|
|
2570
2648
|
init_logger();
|
|
@@ -2640,6 +2718,25 @@ function registerProviders() {
|
|
|
2640
2718
|
}
|
|
2641
2719
|
registerProviders();
|
|
2642
2720
|
|
|
2721
|
+
// src/auth/auth-config.ts
|
|
2722
|
+
var AuthConfig = class {
|
|
2723
|
+
/**
|
|
2724
|
+
* The authentication scheme
|
|
2725
|
+
*/
|
|
2726
|
+
|
|
2727
|
+
/**
|
|
2728
|
+
* Additional context properties
|
|
2729
|
+
*/
|
|
2730
|
+
|
|
2731
|
+
/**
|
|
2732
|
+
* Constructor for AuthConfig
|
|
2733
|
+
*/
|
|
2734
|
+
constructor(config) {
|
|
2735
|
+
this.authScheme = config.authScheme;
|
|
2736
|
+
this.context = config.context;
|
|
2737
|
+
}
|
|
2738
|
+
};
|
|
2739
|
+
|
|
2643
2740
|
// src/auth/auth-credential.ts
|
|
2644
2741
|
var AuthCredentialType = /* @__PURE__ */ ((AuthCredentialType2) => {
|
|
2645
2742
|
AuthCredentialType2["API_KEY"] = "api_key";
|
|
@@ -2842,25 +2939,6 @@ var OAuth2Credential = class extends AuthCredential {
|
|
|
2842
2939
|
}
|
|
2843
2940
|
};
|
|
2844
2941
|
|
|
2845
|
-
// src/auth/auth-config.ts
|
|
2846
|
-
var AuthConfig = class {
|
|
2847
|
-
/**
|
|
2848
|
-
* The authentication scheme
|
|
2849
|
-
*/
|
|
2850
|
-
|
|
2851
|
-
/**
|
|
2852
|
-
* Additional context properties
|
|
2853
|
-
*/
|
|
2854
|
-
|
|
2855
|
-
/**
|
|
2856
|
-
* Constructor for AuthConfig
|
|
2857
|
-
*/
|
|
2858
|
-
constructor(config) {
|
|
2859
|
-
this.authScheme = config.authScheme;
|
|
2860
|
-
this.context = config.context;
|
|
2861
|
-
}
|
|
2862
|
-
};
|
|
2863
|
-
|
|
2864
2942
|
// src/auth/auth-handler.ts
|
|
2865
2943
|
var AuthHandler = class {
|
|
2866
2944
|
/**
|
|
@@ -8010,6 +8088,9 @@ var IdentityLlmRequestProcessor = class extends BaseLlmRequestProcessor {
|
|
|
8010
8088
|
};
|
|
8011
8089
|
var requestProcessor5 = new IdentityLlmRequestProcessor();
|
|
8012
8090
|
|
|
8091
|
+
// src/flows/llm-flows/instructions.ts
|
|
8092
|
+
|
|
8093
|
+
|
|
8013
8094
|
// src/utils/instructions-utils.ts
|
|
8014
8095
|
async function injectSessionState(template, readonlyContext) {
|
|
8015
8096
|
const invocationContext = readonlyContext._invocationContext;
|
|
@@ -8124,6 +8205,22 @@ var InstructionsLlmRequestProcessor = class extends BaseLlmRequestProcessor {
|
|
|
8124
8205
|
}
|
|
8125
8206
|
llmRequest.appendInstructions([instruction]);
|
|
8126
8207
|
}
|
|
8208
|
+
if (agent.outputSchema) {
|
|
8209
|
+
try {
|
|
8210
|
+
const raw = _zodtojsonschema.zodToJsonSchema.call(void 0, agent.outputSchema, {
|
|
8211
|
+
target: "jsonSchema7",
|
|
8212
|
+
$refStrategy: "none"
|
|
8213
|
+
});
|
|
8214
|
+
const { $schema, ...json } = raw || {};
|
|
8215
|
+
llmRequest.appendInstructions([
|
|
8216
|
+
"You must respond with application/json that validates against this JSON Schema:",
|
|
8217
|
+
"```json",
|
|
8218
|
+
JSON.stringify(json, null, 2),
|
|
8219
|
+
"```"
|
|
8220
|
+
]);
|
|
8221
|
+
} catch (e5) {
|
|
8222
|
+
}
|
|
8223
|
+
}
|
|
8127
8224
|
for await (const _ of []) {
|
|
8128
8225
|
yield _;
|
|
8129
8226
|
}
|
|
@@ -8415,12 +8512,87 @@ function removeThoughtFromRequest(llmRequest) {
|
|
|
8415
8512
|
var requestProcessor7 = new NlPlanningRequestProcessor();
|
|
8416
8513
|
var responseProcessor2 = new NlPlanningResponseProcessor();
|
|
8417
8514
|
|
|
8515
|
+
// src/flows/llm-flows/output-schema.ts
|
|
8516
|
+
init_logger();
|
|
8517
|
+
var OutputSchemaResponseProcessor = (_class26 = class extends BaseLlmResponseProcessor {constructor(...args2) { super(...args2); _class26.prototype.__init46.call(this); }
|
|
8518
|
+
__init46() {this.logger = new Logger({ name: "OutputSchemaResponseProcessor" })}
|
|
8519
|
+
async *runAsync(invocationContext, llmResponse) {
|
|
8520
|
+
if (!llmResponse || !llmResponse.content || !llmResponse.content.parts || llmResponse.content.parts.length === 0) {
|
|
8521
|
+
return;
|
|
8522
|
+
}
|
|
8523
|
+
const agent = invocationContext.agent;
|
|
8524
|
+
if (!("outputSchema" in agent) || !agent.outputSchema) {
|
|
8525
|
+
return;
|
|
8526
|
+
}
|
|
8527
|
+
let textContent = llmResponse.content.parts.map((part) => {
|
|
8528
|
+
if (part && typeof part === "object" && "text" in part) {
|
|
8529
|
+
return part.text || "";
|
|
8530
|
+
}
|
|
8531
|
+
return "";
|
|
8532
|
+
}).join("");
|
|
8533
|
+
if (!textContent.trim()) {
|
|
8534
|
+
return;
|
|
8535
|
+
}
|
|
8536
|
+
try {
|
|
8537
|
+
const parsed = JSON.parse(textContent);
|
|
8538
|
+
const validated = agent.outputSchema.parse(parsed);
|
|
8539
|
+
textContent = JSON.stringify(validated, null, 2);
|
|
8540
|
+
llmResponse.content.parts = llmResponse.content.parts.map((part) => {
|
|
8541
|
+
if (part && typeof part === "object" && "text" in part) {
|
|
8542
|
+
return {
|
|
8543
|
+
...part,
|
|
8544
|
+
text: textContent
|
|
8545
|
+
};
|
|
8546
|
+
}
|
|
8547
|
+
return part;
|
|
8548
|
+
});
|
|
8549
|
+
this.logger.debug("Output schema validation successful", {
|
|
8550
|
+
agent: agent.name,
|
|
8551
|
+
originalLength: textContent.length,
|
|
8552
|
+
validatedKeys: Object.keys(validated)
|
|
8553
|
+
});
|
|
8554
|
+
} catch (error) {
|
|
8555
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
8556
|
+
const detailedError = `Output schema validation failed for agent '${agent.name}': ${errorMessage}`;
|
|
8557
|
+
this.logger.error(detailedError, {
|
|
8558
|
+
agent: agent.name,
|
|
8559
|
+
responseContent: textContent.substring(0, 200) + (textContent.length > 200 ? "..." : ""),
|
|
8560
|
+
error: errorMessage
|
|
8561
|
+
});
|
|
8562
|
+
llmResponse.errorCode = "OUTPUT_SCHEMA_VALIDATION_FAILED";
|
|
8563
|
+
llmResponse.errorMessage = detailedError;
|
|
8564
|
+
llmResponse.error = new Error(detailedError);
|
|
8565
|
+
const errorEvent = new Event({
|
|
8566
|
+
id: Event.newId(),
|
|
8567
|
+
invocationId: invocationContext.invocationId,
|
|
8568
|
+
author: agent.name,
|
|
8569
|
+
branch: invocationContext.branch,
|
|
8570
|
+
content: {
|
|
8571
|
+
role: "assistant",
|
|
8572
|
+
parts: [
|
|
8573
|
+
{
|
|
8574
|
+
text: `Error: ${detailedError}`
|
|
8575
|
+
}
|
|
8576
|
+
]
|
|
8577
|
+
}
|
|
8578
|
+
});
|
|
8579
|
+
errorEvent.errorCode = "OUTPUT_SCHEMA_VALIDATION_FAILED";
|
|
8580
|
+
errorEvent.errorMessage = detailedError;
|
|
8581
|
+
errorEvent.error = new Error(detailedError);
|
|
8582
|
+
yield errorEvent;
|
|
8583
|
+
}
|
|
8584
|
+
}
|
|
8585
|
+
}, _class26);
|
|
8586
|
+
var responseProcessor3 = new OutputSchemaResponseProcessor();
|
|
8587
|
+
|
|
8418
8588
|
// src/flows/llm-flows/shared-memory.ts
|
|
8419
8589
|
var SharedMemoryRequestProcessor = class extends BaseLlmRequestProcessor {
|
|
8420
8590
|
async *runAsync(invocationContext, llmRequest) {
|
|
8421
8591
|
const memoryService = invocationContext.memoryService;
|
|
8422
8592
|
if (!memoryService) return;
|
|
8423
|
-
const lastUserEvent = invocationContext.session.events.findLast(
|
|
8593
|
+
const lastUserEvent = invocationContext.session.events.findLast(
|
|
8594
|
+
(e) => e.author === "user" && _optionalChain([e, 'access', _230 => _230.content, 'optionalAccess', _231 => _231.parts, 'optionalAccess', _232 => _232.length])
|
|
8595
|
+
);
|
|
8424
8596
|
if (!lastUserEvent) return;
|
|
8425
8597
|
const query = (_nullishCoalesce(lastUserEvent.content.parts, () => ( []))).map((p) => p.text || "").join(" ");
|
|
8426
8598
|
const results = await memoryService.searchMemory({
|
|
@@ -8479,8 +8651,10 @@ var SingleFlow = class extends BaseLlmFlow {
|
|
|
8479
8651
|
this.responseProcessors.push(
|
|
8480
8652
|
responseProcessor2,
|
|
8481
8653
|
// Phase 5: NL Planning
|
|
8654
|
+
responseProcessor3,
|
|
8655
|
+
// Phase 6: Output Schema validation and parsing - validates response against agent's output schema
|
|
8482
8656
|
responseProcessor
|
|
8483
|
-
// Phase
|
|
8657
|
+
// Phase 7: Code Execution (placeholder)
|
|
8484
8658
|
);
|
|
8485
8659
|
this.logger.debug("SingleFlow initialized with processors");
|
|
8486
8660
|
}
|
|
@@ -8582,7 +8756,7 @@ var AutoFlow = class extends SingleFlow {
|
|
|
8582
8756
|
|
|
8583
8757
|
// src/agents/llm-agent.ts
|
|
8584
8758
|
init_function_tool();
|
|
8585
|
-
var LlmAgent = (
|
|
8759
|
+
var LlmAgent = (_class27 = class _LlmAgent extends BaseAgent {
|
|
8586
8760
|
/**
|
|
8587
8761
|
* The model to use for the agent
|
|
8588
8762
|
* When not set, the agent will inherit the model from its ancestor
|
|
@@ -8653,13 +8827,27 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8653
8827
|
* The input schema when agent is used as a tool
|
|
8654
8828
|
*/
|
|
8655
8829
|
|
|
8656
|
-
// Schema type - depends on specific implementation
|
|
8657
8830
|
/**
|
|
8658
8831
|
* The output schema when agent replies
|
|
8659
8832
|
*/
|
|
8660
8833
|
|
|
8661
|
-
|
|
8662
|
-
|
|
8834
|
+
/**
|
|
8835
|
+
* Callback or list of callbacks to be called before calling the LLM
|
|
8836
|
+
*/
|
|
8837
|
+
|
|
8838
|
+
/**
|
|
8839
|
+
* Callback or list of callbacks to be called after calling the LLM
|
|
8840
|
+
*/
|
|
8841
|
+
|
|
8842
|
+
/**
|
|
8843
|
+
* Callback or list of callbacks to be called before calling a tool
|
|
8844
|
+
*/
|
|
8845
|
+
|
|
8846
|
+
/**
|
|
8847
|
+
* Callback or list of callbacks to be called after calling a tool
|
|
8848
|
+
*/
|
|
8849
|
+
|
|
8850
|
+
__init47() {this.logger = new Logger({ name: "LlmAgent" })}
|
|
8663
8851
|
/**
|
|
8664
8852
|
* Constructor for LlmAgent
|
|
8665
8853
|
*/
|
|
@@ -8670,7 +8858,7 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8670
8858
|
subAgents: config.subAgents,
|
|
8671
8859
|
beforeAgentCallback: config.beforeAgentCallback,
|
|
8672
8860
|
afterAgentCallback: config.afterAgentCallback
|
|
8673
|
-
});
|
|
8861
|
+
});_class27.prototype.__init47.call(this);;
|
|
8674
8862
|
this.model = config.model || "";
|
|
8675
8863
|
this.instruction = config.instruction || "";
|
|
8676
8864
|
this.globalInstruction = config.globalInstruction || "";
|
|
@@ -8689,6 +8877,11 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8689
8877
|
this.generateContentConfig = config.generateContentConfig;
|
|
8690
8878
|
this.inputSchema = config.inputSchema;
|
|
8691
8879
|
this.outputSchema = config.outputSchema;
|
|
8880
|
+
this.beforeModelCallback = config.beforeModelCallback;
|
|
8881
|
+
this.afterModelCallback = config.afterModelCallback;
|
|
8882
|
+
this.beforeToolCallback = config.beforeToolCallback;
|
|
8883
|
+
this.afterToolCallback = config.afterToolCallback;
|
|
8884
|
+
this.validateOutputSchemaConfig();
|
|
8692
8885
|
}
|
|
8693
8886
|
/**
|
|
8694
8887
|
* The resolved model field as BaseLLM
|
|
@@ -8705,13 +8898,15 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8705
8898
|
return new AiSdkLlm(this.model);
|
|
8706
8899
|
}
|
|
8707
8900
|
let ancestorAgent = this.parentAgent;
|
|
8708
|
-
while (ancestorAgent !== null) {
|
|
8901
|
+
while (ancestorAgent !== null && ancestorAgent !== void 0) {
|
|
8709
8902
|
if (ancestorAgent instanceof _LlmAgent) {
|
|
8710
8903
|
return ancestorAgent.canonicalModel;
|
|
8711
8904
|
}
|
|
8712
8905
|
ancestorAgent = ancestorAgent.parentAgent;
|
|
8713
8906
|
}
|
|
8714
|
-
throw new Error(
|
|
8907
|
+
throw new Error(
|
|
8908
|
+
`No model found for agent "${this.name}". Please specify a model directly on this agent using the 'model' property`
|
|
8909
|
+
);
|
|
8715
8910
|
}
|
|
8716
8911
|
/**
|
|
8717
8912
|
* The resolved instruction field to construct instruction for this agent
|
|
@@ -8751,6 +8946,80 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8751
8946
|
}
|
|
8752
8947
|
return resolvedTools;
|
|
8753
8948
|
}
|
|
8949
|
+
/**
|
|
8950
|
+
* Gets the canonical before model callbacks as an array
|
|
8951
|
+
*/
|
|
8952
|
+
get canonicalBeforeModelCallbacks() {
|
|
8953
|
+
if (!this.beforeModelCallback) {
|
|
8954
|
+
return [];
|
|
8955
|
+
}
|
|
8956
|
+
if (Array.isArray(this.beforeModelCallback)) {
|
|
8957
|
+
return this.beforeModelCallback;
|
|
8958
|
+
}
|
|
8959
|
+
return [this.beforeModelCallback];
|
|
8960
|
+
}
|
|
8961
|
+
/**
|
|
8962
|
+
* Gets the canonical after model callbacks as an array
|
|
8963
|
+
*/
|
|
8964
|
+
get canonicalAfterModelCallbacks() {
|
|
8965
|
+
if (!this.afterModelCallback) {
|
|
8966
|
+
return [];
|
|
8967
|
+
}
|
|
8968
|
+
if (Array.isArray(this.afterModelCallback)) {
|
|
8969
|
+
return this.afterModelCallback;
|
|
8970
|
+
}
|
|
8971
|
+
return [this.afterModelCallback];
|
|
8972
|
+
}
|
|
8973
|
+
/**
|
|
8974
|
+
* Gets the canonical before tool callbacks as an array
|
|
8975
|
+
*/
|
|
8976
|
+
get canonicalBeforeToolCallbacks() {
|
|
8977
|
+
if (!this.beforeToolCallback) {
|
|
8978
|
+
return [];
|
|
8979
|
+
}
|
|
8980
|
+
if (Array.isArray(this.beforeToolCallback)) {
|
|
8981
|
+
return this.beforeToolCallback;
|
|
8982
|
+
}
|
|
8983
|
+
return [this.beforeToolCallback];
|
|
8984
|
+
}
|
|
8985
|
+
/**
|
|
8986
|
+
* Gets the canonical after tool callbacks as an array
|
|
8987
|
+
*/
|
|
8988
|
+
get canonicalAfterToolCallbacks() {
|
|
8989
|
+
if (!this.afterToolCallback) {
|
|
8990
|
+
return [];
|
|
8991
|
+
}
|
|
8992
|
+
if (Array.isArray(this.afterToolCallback)) {
|
|
8993
|
+
return this.afterToolCallback;
|
|
8994
|
+
}
|
|
8995
|
+
return [this.afterToolCallback];
|
|
8996
|
+
}
|
|
8997
|
+
/**
|
|
8998
|
+
* Validates output schema configuration
|
|
8999
|
+
* This matches the Python implementation's __check_output_schema
|
|
9000
|
+
*/
|
|
9001
|
+
validateOutputSchemaConfig() {
|
|
9002
|
+
if (!this.outputSchema) {
|
|
9003
|
+
return;
|
|
9004
|
+
}
|
|
9005
|
+
if (!this.disallowTransferToParent || !this.disallowTransferToPeers) {
|
|
9006
|
+
this.logger.warn(
|
|
9007
|
+
`Invalid config for agent ${this.name}: output_schema cannot co-exist with agent transfer configurations. Setting disallow_transfer_to_parent=true, disallow_transfer_to_peers=true`
|
|
9008
|
+
);
|
|
9009
|
+
this.disallowTransferToParent = true;
|
|
9010
|
+
this.disallowTransferToPeers = true;
|
|
9011
|
+
}
|
|
9012
|
+
if (this.subAgents && this.subAgents.length > 0) {
|
|
9013
|
+
throw new Error(
|
|
9014
|
+
`Invalid config for agent ${this.name}: if output_schema is set, sub_agents must be empty to disable agent transfer.`
|
|
9015
|
+
);
|
|
9016
|
+
}
|
|
9017
|
+
if (this.tools && this.tools.length > 0) {
|
|
9018
|
+
throw new Error(
|
|
9019
|
+
`Invalid config for agent ${this.name}: if output_schema is set, tools must be empty`
|
|
9020
|
+
);
|
|
9021
|
+
}
|
|
9022
|
+
}
|
|
8754
9023
|
/**
|
|
8755
9024
|
* Gets the appropriate LLM flow for this agent
|
|
8756
9025
|
* This matches the Python implementation's _llm_flow property
|
|
@@ -8766,8 +9035,28 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8766
9035
|
* This matches the Python implementation's __maybe_save_output_to_state
|
|
8767
9036
|
*/
|
|
8768
9037
|
maybeSaveOutputToState(event) {
|
|
9038
|
+
if (event.author !== this.name) {
|
|
9039
|
+
this.logger.debug(
|
|
9040
|
+
`Skipping output save for agent ${this.name}: event authored by ${event.author}`
|
|
9041
|
+
);
|
|
9042
|
+
return;
|
|
9043
|
+
}
|
|
8769
9044
|
if (this.outputKey && event.isFinalResponse() && _optionalChain([event, 'access', _238 => _238.content, 'optionalAccess', _239 => _239.parts])) {
|
|
8770
|
-
|
|
9045
|
+
let result = event.content.parts.map((part) => part.text || "").join("");
|
|
9046
|
+
if (this.outputSchema) {
|
|
9047
|
+
if (!result.trim()) {
|
|
9048
|
+
return;
|
|
9049
|
+
}
|
|
9050
|
+
try {
|
|
9051
|
+
const parsed = JSON.parse(result);
|
|
9052
|
+
result = this.outputSchema.parse(parsed);
|
|
9053
|
+
} catch (error) {
|
|
9054
|
+
this.logger.error("Failed to validate output with schema:", error);
|
|
9055
|
+
throw new Error(
|
|
9056
|
+
`Output validation failed: ${error instanceof Error ? error.message : String(error)}`
|
|
9057
|
+
);
|
|
9058
|
+
}
|
|
9059
|
+
}
|
|
8771
9060
|
if (result) {
|
|
8772
9061
|
if (!event.actions.stateDelta) {
|
|
8773
9062
|
event.actions.stateDelta = {};
|
|
@@ -8806,7 +9095,7 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
|
|
|
8806
9095
|
yield errorEvent;
|
|
8807
9096
|
}
|
|
8808
9097
|
}
|
|
8809
|
-
},
|
|
9098
|
+
}, _class27);
|
|
8810
9099
|
|
|
8811
9100
|
// src/agents/sequential-agent.ts
|
|
8812
9101
|
var SequentialAgent = class extends BaseAgent {
|
|
@@ -8998,7 +9287,7 @@ var LoopAgent = class extends BaseAgent {
|
|
|
8998
9287
|
|
|
8999
9288
|
// src/agents/lang-graph-agent.ts
|
|
9000
9289
|
init_logger();
|
|
9001
|
-
var LangGraphAgent = (
|
|
9290
|
+
var LangGraphAgent = (_class28 = class extends BaseAgent {
|
|
9002
9291
|
/**
|
|
9003
9292
|
* Graph nodes (agents and their connections)
|
|
9004
9293
|
*/
|
|
@@ -9014,8 +9303,8 @@ var LangGraphAgent = (_class27 = class extends BaseAgent {
|
|
|
9014
9303
|
/**
|
|
9015
9304
|
* Results from node executions
|
|
9016
9305
|
*/
|
|
9017
|
-
|
|
9018
|
-
|
|
9306
|
+
__init48() {this.results = []}
|
|
9307
|
+
__init49() {this.logger = new Logger({ name: "LangGraphAgent" })}
|
|
9019
9308
|
/**
|
|
9020
9309
|
* Constructor for LangGraphAgent
|
|
9021
9310
|
*/
|
|
@@ -9023,7 +9312,7 @@ var LangGraphAgent = (_class27 = class extends BaseAgent {
|
|
|
9023
9312
|
super({
|
|
9024
9313
|
name: config.name,
|
|
9025
9314
|
description: config.description
|
|
9026
|
-
});
|
|
9315
|
+
});_class28.prototype.__init48.call(this);_class28.prototype.__init49.call(this);;
|
|
9027
9316
|
this.nodes = /* @__PURE__ */ new Map();
|
|
9028
9317
|
for (const node of config.nodes) {
|
|
9029
9318
|
if (this.nodes.has(node.name)) {
|
|
@@ -9219,7 +9508,7 @@ var LangGraphAgent = (_class27 = class extends BaseAgent {
|
|
|
9219
9508
|
}
|
|
9220
9509
|
this.maxSteps = maxSteps;
|
|
9221
9510
|
}
|
|
9222
|
-
},
|
|
9511
|
+
}, _class28);
|
|
9223
9512
|
|
|
9224
9513
|
// src/agents/agent-builder.ts
|
|
9225
9514
|
|
|
@@ -9322,8 +9611,8 @@ var RunConfig = class {
|
|
|
9322
9611
|
};
|
|
9323
9612
|
|
|
9324
9613
|
// src/artifacts/in-memory-artifact-service.ts
|
|
9325
|
-
var InMemoryArtifactService = (
|
|
9326
|
-
|
|
9614
|
+
var InMemoryArtifactService = (_class29 = class {constructor() { _class29.prototype.__init50.call(this); }
|
|
9615
|
+
__init50() {this.artifacts = /* @__PURE__ */ new Map()}
|
|
9327
9616
|
fileHasUserNamespace(filename) {
|
|
9328
9617
|
return filename.startsWith("user:");
|
|
9329
9618
|
}
|
|
@@ -9396,7 +9685,7 @@ var InMemoryArtifactService = (_class28 = class {constructor() { _class28.protot
|
|
|
9396
9685
|
}
|
|
9397
9686
|
return Array.from({ length: versions.length }, (_, i) => i);
|
|
9398
9687
|
}
|
|
9399
|
-
},
|
|
9688
|
+
}, _class29);
|
|
9400
9689
|
|
|
9401
9690
|
// src/runners.ts
|
|
9402
9691
|
init_logger();
|
|
@@ -9423,15 +9712,15 @@ function _extractWordsLower(text) {
|
|
|
9423
9712
|
const words = text.match(/[A-Za-z]+/g) || [];
|
|
9424
9713
|
return new Set(words.map((word) => word.toLowerCase()));
|
|
9425
9714
|
}
|
|
9426
|
-
var InMemoryMemoryService = (
|
|
9715
|
+
var InMemoryMemoryService = (_class30 = class {
|
|
9427
9716
|
/**
|
|
9428
9717
|
* Keys are app_name/user_id, session_id. Values are session event lists.
|
|
9429
9718
|
*/
|
|
9430
|
-
|
|
9719
|
+
__init51() {this._sessionEvents = /* @__PURE__ */ new Map()}
|
|
9431
9720
|
/**
|
|
9432
9721
|
* Constructor for InMemoryMemoryService
|
|
9433
9722
|
*/
|
|
9434
|
-
constructor() {;
|
|
9723
|
+
constructor() {;_class30.prototype.__init51.call(this);
|
|
9435
9724
|
this._sessionEvents = /* @__PURE__ */ new Map();
|
|
9436
9725
|
}
|
|
9437
9726
|
/**
|
|
@@ -9515,7 +9804,7 @@ var InMemoryMemoryService = (_class29 = class {
|
|
|
9515
9804
|
clear() {
|
|
9516
9805
|
this._sessionEvents.clear();
|
|
9517
9806
|
}
|
|
9518
|
-
},
|
|
9807
|
+
}, _class30);
|
|
9519
9808
|
|
|
9520
9809
|
// src/sessions/in-memory-session-service.ts
|
|
9521
9810
|
var _crypto = require('crypto');
|
|
@@ -9557,19 +9846,19 @@ var BaseSessionService = class {
|
|
|
9557
9846
|
};
|
|
9558
9847
|
|
|
9559
9848
|
// src/sessions/in-memory-session-service.ts
|
|
9560
|
-
var InMemorySessionService = (
|
|
9849
|
+
var InMemorySessionService = (_class31 = class extends BaseSessionService {constructor(...args3) { super(...args3); _class31.prototype.__init52.call(this);_class31.prototype.__init53.call(this);_class31.prototype.__init54.call(this); }
|
|
9561
9850
|
/**
|
|
9562
9851
|
* A map from app name to a map from user ID to a map from session ID to session.
|
|
9563
9852
|
*/
|
|
9564
|
-
|
|
9853
|
+
__init52() {this.sessions = /* @__PURE__ */ new Map()}
|
|
9565
9854
|
/**
|
|
9566
9855
|
* A map from app name to a map from user ID to a map from key to the value.
|
|
9567
9856
|
*/
|
|
9568
|
-
|
|
9857
|
+
__init53() {this.userState = /* @__PURE__ */ new Map()}
|
|
9569
9858
|
/**
|
|
9570
9859
|
* A map from app name to a map from key to the value.
|
|
9571
9860
|
*/
|
|
9572
|
-
|
|
9861
|
+
__init54() {this.appState = /* @__PURE__ */ new Map()}
|
|
9573
9862
|
/**
|
|
9574
9863
|
* Creates a new session.
|
|
9575
9864
|
*/
|
|
@@ -9766,7 +10055,7 @@ var InMemorySessionService = (_class30 = class extends BaseSessionService {const
|
|
|
9766
10055
|
storageSession.lastUpdateTime = event.timestamp;
|
|
9767
10056
|
return event;
|
|
9768
10057
|
}
|
|
9769
|
-
},
|
|
10058
|
+
}, _class31);
|
|
9770
10059
|
|
|
9771
10060
|
// src/runners.ts
|
|
9772
10061
|
function _findFunctionCallEventIfLastEventIsFunctionResponse(session) {
|
|
@@ -9792,7 +10081,7 @@ function _findFunctionCallEventIfLastEventIsFunctionResponse(session) {
|
|
|
9792
10081
|
}
|
|
9793
10082
|
return null;
|
|
9794
10083
|
}
|
|
9795
|
-
var Runner = (
|
|
10084
|
+
var Runner = (_class32 = class {
|
|
9796
10085
|
/**
|
|
9797
10086
|
* The app name of the runner.
|
|
9798
10087
|
*/
|
|
@@ -9813,7 +10102,7 @@ var Runner = (_class31 = class {
|
|
|
9813
10102
|
* The memory service for the runner.
|
|
9814
10103
|
*/
|
|
9815
10104
|
|
|
9816
|
-
|
|
10105
|
+
__init55() {this.logger = new Logger({ name: "Runner" })}
|
|
9817
10106
|
/**
|
|
9818
10107
|
* Initializes the Runner.
|
|
9819
10108
|
*/
|
|
@@ -9823,7 +10112,7 @@ var Runner = (_class31 = class {
|
|
|
9823
10112
|
artifactService,
|
|
9824
10113
|
sessionService,
|
|
9825
10114
|
memoryService
|
|
9826
|
-
}) {;
|
|
10115
|
+
}) {;_class32.prototype.__init55.call(this);
|
|
9827
10116
|
this.appName = appName;
|
|
9828
10117
|
this.agent = agent;
|
|
9829
10118
|
this.artifactService = artifactService;
|
|
@@ -10030,7 +10319,7 @@ var Runner = (_class31 = class {
|
|
|
10030
10319
|
runConfig
|
|
10031
10320
|
});
|
|
10032
10321
|
}
|
|
10033
|
-
},
|
|
10322
|
+
}, _class32);
|
|
10034
10323
|
var InMemoryRunner = class extends Runner {
|
|
10035
10324
|
/**
|
|
10036
10325
|
* Deprecated. Please don't use. The in-memory session service for the runner.
|
|
@@ -10053,18 +10342,18 @@ var InMemoryRunner = class extends Runner {
|
|
|
10053
10342
|
};
|
|
10054
10343
|
|
|
10055
10344
|
// src/agents/agent-builder.ts
|
|
10056
|
-
var AgentBuilder = (
|
|
10345
|
+
var AgentBuilder = (_class33 = class _AgentBuilder {
|
|
10057
10346
|
|
|
10058
10347
|
|
|
10059
10348
|
|
|
10060
10349
|
|
|
10061
10350
|
|
|
10062
|
-
|
|
10351
|
+
__init56() {this.agentType = "llm"}
|
|
10063
10352
|
|
|
10064
10353
|
/**
|
|
10065
10354
|
* Private constructor - use static create() method
|
|
10066
10355
|
*/
|
|
10067
|
-
constructor(name) {;
|
|
10356
|
+
constructor(name) {;_class33.prototype.__init56.call(this);
|
|
10068
10357
|
this.config = { name };
|
|
10069
10358
|
}
|
|
10070
10359
|
/**
|
|
@@ -10110,6 +10399,14 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
|
|
|
10110
10399
|
this.config.instruction = instruction;
|
|
10111
10400
|
return this;
|
|
10112
10401
|
}
|
|
10402
|
+
withInputSchema(schema) {
|
|
10403
|
+
this.config.inputSchema = schema;
|
|
10404
|
+
return this;
|
|
10405
|
+
}
|
|
10406
|
+
withOutputSchema(schema) {
|
|
10407
|
+
this.config.outputSchema = schema;
|
|
10408
|
+
return this;
|
|
10409
|
+
}
|
|
10113
10410
|
/**
|
|
10114
10411
|
* Add tools to the agent
|
|
10115
10412
|
* @param tools Tools to add to the agent
|
|
@@ -10316,6 +10613,14 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
|
|
|
10316
10613
|
}
|
|
10317
10614
|
return { agent, runner, session };
|
|
10318
10615
|
}
|
|
10616
|
+
/**
|
|
10617
|
+
* Type-safe build method for agents with output schemas
|
|
10618
|
+
* Provides better type inference for the ask method return type
|
|
10619
|
+
*/
|
|
10620
|
+
async buildWithSchema() {
|
|
10621
|
+
const result = await this.build();
|
|
10622
|
+
return result;
|
|
10623
|
+
}
|
|
10319
10624
|
/**
|
|
10320
10625
|
* Quick execution helper - build and run a message
|
|
10321
10626
|
* @param message Message to send to the agent (string or full message object)
|
|
@@ -10350,7 +10655,9 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
|
|
|
10350
10655
|
memoryService: this.memoryService,
|
|
10351
10656
|
artifactService: this.artifactService,
|
|
10352
10657
|
outputKey: this.config.outputKey,
|
|
10353
|
-
sessionService: this.sessionService
|
|
10658
|
+
sessionService: this.sessionService,
|
|
10659
|
+
inputSchema: this.config.inputSchema,
|
|
10660
|
+
outputSchema: this.config.outputSchema
|
|
10354
10661
|
});
|
|
10355
10662
|
}
|
|
10356
10663
|
case "sequential":
|
|
@@ -10409,14 +10716,16 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
|
|
|
10409
10716
|
return `app-${this.config.name}`;
|
|
10410
10717
|
}
|
|
10411
10718
|
/**
|
|
10412
|
-
* Create enhanced runner with simplified API
|
|
10719
|
+
* Create enhanced runner with simplified API and proper typing
|
|
10413
10720
|
* @param baseRunner The base runner instance
|
|
10414
10721
|
* @param session The session instance
|
|
10415
10722
|
* @returns Enhanced runner with simplified API
|
|
10416
10723
|
*/
|
|
10417
10724
|
createEnhancedRunner(baseRunner, session) {
|
|
10418
10725
|
const sessionOptions = this.sessionOptions;
|
|
10726
|
+
const outputSchema = this.config.outputSchema;
|
|
10419
10727
|
return {
|
|
10728
|
+
__outputSchema: outputSchema,
|
|
10420
10729
|
async ask(message) {
|
|
10421
10730
|
const newMessage = typeof message === "string" ? { parts: [{ text: message }] } : typeof message === "object" && "contents" in message ? { parts: message.contents[message.contents.length - 1].parts } : message;
|
|
10422
10731
|
let response = "";
|
|
@@ -10437,6 +10746,18 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
|
|
|
10437
10746
|
}
|
|
10438
10747
|
}
|
|
10439
10748
|
}
|
|
10749
|
+
if (outputSchema) {
|
|
10750
|
+
try {
|
|
10751
|
+
const parsed = JSON.parse(response);
|
|
10752
|
+
return outputSchema.parse(parsed);
|
|
10753
|
+
} catch (parseError) {
|
|
10754
|
+
try {
|
|
10755
|
+
return outputSchema.parse(response);
|
|
10756
|
+
} catch (validationError) {
|
|
10757
|
+
return response.trim();
|
|
10758
|
+
}
|
|
10759
|
+
}
|
|
10760
|
+
}
|
|
10440
10761
|
return response.trim();
|
|
10441
10762
|
},
|
|
10442
10763
|
runAsync(params) {
|
|
@@ -10444,7 +10765,7 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
|
|
|
10444
10765
|
}
|
|
10445
10766
|
};
|
|
10446
10767
|
}
|
|
10447
|
-
},
|
|
10768
|
+
}, _class33);
|
|
10448
10769
|
|
|
10449
10770
|
// src/memory/index.ts
|
|
10450
10771
|
var memory_exports = {};
|
|
@@ -10778,11 +11099,11 @@ var VertexAiSessionService = class extends BaseSessionService {
|
|
|
10778
11099
|
|
|
10779
11100
|
// src/sessions/database-session-service.ts
|
|
10780
11101
|
var _kysely = require('kysely');
|
|
10781
|
-
var DatabaseSessionService = (
|
|
11102
|
+
var DatabaseSessionService = (_class34 = class extends BaseSessionService {
|
|
10782
11103
|
|
|
10783
|
-
|
|
11104
|
+
__init57() {this.initialized = false}
|
|
10784
11105
|
constructor(config) {
|
|
10785
|
-
super();
|
|
11106
|
+
super();_class34.prototype.__init57.call(this);;
|
|
10786
11107
|
this.db = config.db;
|
|
10787
11108
|
if (!config.skipTableCreation) {
|
|
10788
11109
|
this.initializeDatabase().catch((error) => {
|
|
@@ -10857,7 +11178,7 @@ var DatabaseSessionService = (_class33 = class extends BaseSessionService {
|
|
|
10857
11178
|
if (!jsonString) return defaultValue;
|
|
10858
11179
|
try {
|
|
10859
11180
|
return JSON.parse(jsonString);
|
|
10860
|
-
} catch (
|
|
11181
|
+
} catch (e6) {
|
|
10861
11182
|
return defaultValue;
|
|
10862
11183
|
}
|
|
10863
11184
|
}
|
|
@@ -11176,7 +11497,7 @@ var DatabaseSessionService = (_class33 = class extends BaseSessionService {
|
|
|
11176
11497
|
}
|
|
11177
11498
|
}
|
|
11178
11499
|
}
|
|
11179
|
-
},
|
|
11500
|
+
}, _class34);
|
|
11180
11501
|
|
|
11181
11502
|
// src/sessions/database-factories.ts
|
|
11182
11503
|
|