@iqai/adk 0.1.19 → 0.1.20

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/dist/index.js CHANGED
@@ -1362,276 +1362,332 @@ var BaseLlm = (_class5 = class {
1362
1362
  var BaseLLMConnection = class {
1363
1363
  };
1364
1364
 
1365
- // src/models/google-llm.ts
1365
+ // src/models/ai-sdk.ts
1366
+ init_logger();
1366
1367
 
1367
1368
 
1368
1369
 
1369
1370
 
1370
- var AGENT_ENGINE_TELEMETRY_TAG = "remote_reasoning_engine";
1371
- var AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME = "GOOGLE_CLOUD_AGENT_ENGINE_ID";
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 for Gemini
1376
+ * Constructor accepts a pre-configured LanguageModel instance
1377
+ * @param model - Pre-configured LanguageModel from provider(modelName)
1379
1378
  */
1380
- constructor(model = "gemini-1.5-flash") {
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
- * Provides the list of supported models.
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
- * Main content generation method - handles both streaming and non-streaming
1397
- */
1398
- async *generateContentAsyncImpl(llmRequest, stream = false) {
1399
- this.preprocessRequest(llmRequest);
1400
- const model = llmRequest.model || this.model;
1401
- const contents = this.convertContents(llmRequest.contents || []);
1402
- const config = llmRequest.config;
1403
- if (stream) {
1404
- const responses = await this.apiClient.models.generateContentStream({
1405
- model,
1406
- contents,
1407
- config
1408
- });
1409
- let response = null;
1410
- let thoughtText = "";
1411
- let text = "";
1412
- let usageMetadata = null;
1413
- for await (const resp of responses) {
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
- parts,
1436
- role: "model"
1410
+ role: "model",
1411
+ parts: [{ text: accumulatedText }]
1437
1412
  },
1438
- usageMetadata
1413
+ partial: true
1439
1414
  });
1440
- thoughtText = "";
1441
- text = "";
1442
1415
  }
1443
- yield llmResponse;
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 (thoughtText) {
1448
- parts.push({ text: thoughtText, thought: true });
1418
+ if (accumulatedText) {
1419
+ parts.push({ text: accumulatedText });
1449
1420
  }
1450
- if (text) {
1451
- parts.push({ text });
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
- parts,
1456
- role: "model"
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
- } else {
1462
- const response = await this.apiClient.models.generateContent({
1463
- model,
1464
- contents,
1465
- config
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
- * Connects to the Gemini model and returns an llm connection.
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
- hasInlineData(response) {
1484
- const parts = _optionalChain([response, 'access', _54 => _54.candidates, 'optionalAccess', _55 => _55[0], 'optionalAccess', _56 => _56.content, 'optionalAccess', _57 => _57.parts]);
1485
- return _optionalChain([parts, 'optionalAccess', _58 => _58.some, 'call', _59 => _59((part) => _optionalChain([part, 'optionalAccess', _60 => _60.inlineData]))]) || false;
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
- * Convert LlmRequest contents to GoogleGenAI format
1500
+ * Transform JSON schema to use lowercase types for AI SDK compatibility
1489
1501
  */
1490
- convertContents(contents) {
1491
- return contents.map((content) => ({
1492
- role: content.role === "assistant" ? "model" : content.role,
1493
- parts: content.parts || [{ text: content.content || "" }]
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
- * Preprocesses the request based on the API backend.
1537
+ * Convert ADK tools to AI SDK tools format
1498
1538
  */
1499
- preprocessRequest(llmRequest) {
1500
- if (this.apiBackend === "GEMINI_API" /* GEMINI_API */) {
1501
- if (llmRequest.config) {
1502
- llmRequest.config.labels = void 0;
1503
- }
1504
- if (llmRequest.contents) {
1505
- for (const content of llmRequest.contents) {
1506
- if (!content.parts) continue;
1507
- for (const part of content.parts) {
1508
- this.removeDisplayNameIfPresent(part.inlineData);
1509
- this.removeDisplayNameIfPresent(part.fileData);
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
- * Sets display_name to null for the Gemini API (non-Vertex) backend.
1558
+ * Convert ADK Content to AI SDK CoreMessage
1517
1559
  */
1518
- removeDisplayNameIfPresent(dataObj) {
1519
- if (_optionalChain([dataObj, 'optionalAccess', _61 => _61.displayName])) {
1520
- dataObj.displayName = null;
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
- * Builds function declaration log string.
1525
- */
1526
- buildFunctionDeclarationLog(funcDecl) {
1527
- let paramStr = "{}";
1528
- if (_optionalChain([funcDecl, 'access', _62 => _62.parameters, 'optionalAccess', _63 => _63.properties])) {
1529
- paramStr = JSON.stringify(funcDecl.parameters.properties);
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
- return `${funcDecl.name}: ${paramStr}`;
1532
- }
1533
- /**
1534
- * Provides the api client.
1535
- */
1536
- get apiClient() {
1537
- if (!this._apiClient) {
1538
- const useVertexAI = process.env.GOOGLE_GENAI_USE_VERTEXAI === "true";
1539
- const apiKey = process.env.GOOGLE_API_KEY;
1540
- const project = process.env.GOOGLE_CLOUD_PROJECT;
1541
- const location = process.env.GOOGLE_CLOUD_LOCATION;
1542
- if (useVertexAI && project && location) {
1543
- this._apiClient = new (0, _genai.GoogleGenAI)({
1544
- vertexai: true,
1545
- project,
1546
- location
1547
- });
1548
- } else if (apiKey) {
1549
- this._apiClient = new (0, _genai.GoogleGenAI)({
1550
- apiKey
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
- return this._apiClient;
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
- return this._apiBackend;
1569
- }
1570
- /**
1571
- * Gets the tracking headers.
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
- const languageLabel = `gl-node/${process.version}`;
1580
- const versionHeaderValue = `${frameworkLabel} ${languageLabel}`;
1581
- this._trackingHeaders = {
1582
- "x-goog-api-client": versionHeaderValue,
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
- return this._trackingHeaders;
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
- * Gets the live API version.
1649
+ * Map ADK role to AI SDK role
1590
1650
  */
1591
- get liveApiVersion() {
1592
- return this.apiBackend === "VERTEX_AI" /* VERTEX_AI */ ? "v1beta1" : "v1alpha";
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
- * Gets the live API client.
1663
+ * Map AI SDK finish reason to ADK finish reason
1596
1664
  */
1597
- get liveApiClient() {
1598
- if (!this._liveApiClient) {
1599
- const useVertexAI = process.env.GOOGLE_GENAI_USE_VERTEXAI === "true";
1600
- const apiKey = process.env.GOOGLE_API_KEY;
1601
- const project = process.env.GOOGLE_CLOUD_PROJECT;
1602
- const location = process.env.GOOGLE_CLOUD_LOCATION;
1603
- if (useVertexAI && project && location) {
1604
- this._liveApiClient = new (0, _genai.GoogleGenAI)({
1605
- vertexai: true,
1606
- project,
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 = (_class6 = class extends BaseLlm {
1683
+ var AnthropicLlm = (_class7 = class extends BaseLlm {
1628
1684
 
1629
- __init12() {this.logger = new Logger({ name: "AnthropicLlm" })}
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);_class6.prototype.__init12.call(this);;
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', _64 => _64.config, 'optionalAccess', _65 => _65.tools, 'optionalAccess', _66 => _66[0], 'optionalAccess', _67 => _67.functionDeclarations])) {
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', _68 => _68.config, 'optionalAccess', _69 => _69.maxOutputTokens]) || MAX_TOKENS,
1674
- temperature: _optionalChain([llmRequest, 'access', _70 => _70.config, 'optionalAccess', _71 => _71.temperature]),
1675
- top_p: _optionalChain([llmRequest, 'access', _72 => _72.config, 'optionalAccess', _73 => _73.topP])
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', _74 => _74.function_response, 'access', _75 => _75.response, 'optionalAccess', _76 => _76.result])) {
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', _77 => _77.parameters, 'optionalAccess', _78 => _78.properties])) {
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
- }, _class6);
1901
+ }, _class7);
1846
1902
 
1847
- // src/models/openai-llm.ts
1848
- var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);
1849
- var OpenAiLlm = class extends BaseLlm {
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 OpenAI LLM
1916
+ * Constructor for Gemini
1853
1917
  */
1854
- constructor(model = "gpt-4o-mini") {
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 ["gpt-3.5-.*", "gpt-4.*", "gpt-4o.*", "o1-.*", "o3-.*"];
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 messages = (llmRequest.contents || []).map(
1870
- (content) => this.contentToOpenAiMessage(content)
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 streamResponse = await this.client.chat.completions.create({
1898
- ...requestParams,
1899
- stream: true
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 accumulatedToolCalls = [];
1905
- for await (const chunk of streamResponse) {
1906
- const choice = chunk.choices[0];
1907
- if (!choice) continue;
1908
- const delta = choice.delta;
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(llmResponse))) {
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: 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
- if (delta.tool_calls) {
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) && usageMetadata) {
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.client.chat.completions.create({
2022
- ...requestParams,
2023
- stream: false
2000
+ const response = await this.apiClient.models.generateContent({
2001
+ model,
2002
+ contents,
2003
+ config
2024
2004
  });
2025
- const choice = response.choices[0];
2026
- if (choice) {
2027
- const llmResponse = this.openAiMessageToLlmResponse(
2028
- choice,
2029
- response.usage
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
- * Live connection is not supported for OpenAI models
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
- * Create LlmResponse from streaming chunk - similar to Google's LlmResponse.create
2019
+ * Check if response has inline data
2046
2020
  */
2047
- createChunkResponse(delta, usage) {
2048
- const parts = [];
2049
- if (delta.content) {
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 OpenAI message to ADK LlmResponse
2026
+ * Convert LlmRequest contents to GoogleGenAI format
2084
2027
  */
2085
- openAiMessageToLlmResponse(choice, usage) {
2086
- const message = choice.message;
2087
- const parts = [];
2088
- if (message.content) {
2089
- parts.push({ text: message.content });
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
- * Convert ADK Content to OpenAI ChatCompletionMessage
2035
+ * Preprocesses the request based on the API backend.
2119
2036
  */
2120
- contentToOpenAiMessage(content) {
2121
- const role = this.toOpenAiRole(content.role);
2122
- if (role === "system") {
2123
- return {
2124
- role: "system",
2125
- content: _optionalChain([content, 'access', _103 => _103.parts, 'optionalAccess', _104 => _104[0], 'optionalAccess', _105 => _105.text]) || ""
2126
- };
2127
- }
2128
- if (_optionalChain([content, 'access', _106 => _106.parts, 'optionalAccess', _107 => _107.some, 'call', _108 => _108((part) => part.functionCall)])) {
2129
- const functionCallPart = content.parts.find(
2130
- (part) => part.functionCall
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
- * Convert ADK function declaration to OpenAI tool
2054
+ * Sets display_name to null for the Gemini API (non-Vertex) backend.
2195
2055
  */
2196
- functionDeclarationToOpenAiTool(functionDeclaration) {
2197
- return {
2198
- type: "function",
2199
- function: {
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
- * Convert ADK role to OpenAI role format
2062
+ * Builds function declaration log string.
2208
2063
  */
2209
- toOpenAiRole(role) {
2210
- if (role === "model") {
2211
- return "assistant";
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 "user";
2069
+ return `${funcDecl.name}: ${paramStr}`;
2217
2070
  }
2218
2071
  /**
2219
- * Convert OpenAI finish reason to ADK finish reason
2072
+ * Provides the api client.
2220
2073
  */
2221
- toAdkFinishReason(openaiFinishReason) {
2222
- switch (openaiFinishReason) {
2223
- case "stop":
2224
- case "tool_calls":
2225
- return "STOP";
2226
- case "length":
2227
- return "MAX_TOKENS";
2228
- default:
2229
- return "FINISH_REASON_UNSPECIFIED";
2230
- }
2231
- }
2232
- /**
2233
- * Preprocess request similar to Google LLM
2234
- */
2235
- preprocessRequest(llmRequest) {
2236
- if (llmRequest.config) {
2237
- llmRequest.config.labels = void 0;
2238
- if (llmRequest.contents) {
2239
- for (const content of llmRequest.contents) {
2240
- if (!content.parts) continue;
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
- * Preprocess individual parts for OpenAI compatibility
2099
+ * Gets the API backend type.
2250
2100
  */
2251
- preprocessPart(part) {
2252
- if (part.inline_data) {
2253
- if (!part.inline_data.mime_type || !part.inline_data.data) {
2254
- delete part.inline_data;
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
- * Detect content type for flow control
2260
- * This is a simplified implementation - you may need to adjust based on your specific requirements
2109
+ * Gets the tracking headers.
2261
2110
  */
2262
- getContentType(content) {
2263
- if (content.includes("<thinking>") || content.includes("[thinking]")) {
2264
- return "thought";
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 "regular";
2124
+ return this._trackingHeaders;
2267
2125
  }
2268
2126
  /**
2269
- * Check if response has inline data (similar to Google LLM)
2127
+ * Gets the live API version.
2270
2128
  */
2271
- hasInlineData(response) {
2272
- const parts = _optionalChain([response, 'access', _118 => _118.content, 'optionalAccess', _119 => _119.parts]);
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 OpenAI client
2133
+ * Gets the live API client.
2277
2134
  */
2278
- get client() {
2279
- if (!this._client) {
2280
- const apiKey = process.env.OPENAI_API_KEY;
2281
- if (!apiKey) {
2282
- throw new Error(
2283
- "OPENAI_API_KEY environment variable is required for OpenAI models"
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._client;
2157
+ return this._liveApiClient;
2291
2158
  }
2292
2159
  };
2293
2160
 
2294
- // src/models/ai-sdk.ts
2295
- init_logger();
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 accepts a pre-configured LanguageModel instance
2306
- * @param model - Pre-configured LanguageModel from provider(modelName)
2166
+ * Constructor for OpenAI LLM
2307
2167
  */
2308
- constructor(modelInstance) {
2309
- super(modelInstance.modelId || "ai-sdk-model");_class7.prototype.__init13.call(this);;
2310
- this.modelInstance = modelInstance;
2168
+ constructor(model = "gpt-4o-mini") {
2169
+ super(model);
2311
2170
  }
2312
2171
  /**
2313
- * Returns empty array - following Python ADK pattern
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
- async *generateContentAsyncImpl(request, stream = false) {
2319
- try {
2320
- const messages = this.convertToAiSdkMessages(request);
2321
- const systemMessage = request.getSystemInstructionText();
2322
- const tools = this.convertToAiSdkTools(request);
2323
- const requestParams = {
2324
- model: this.modelInstance,
2325
- messages,
2326
- system: systemMessage,
2327
- tools: Object.keys(tools).length > 0 ? tools : void 0,
2328
- maxTokens: _optionalChain([request, 'access', _122 => _122.config, 'optionalAccess', _123 => _123.maxOutputTokens]),
2329
- temperature: _optionalChain([request, 'access', _124 => _124.config, 'optionalAccess', _125 => _125.temperature]),
2330
- topP: _optionalChain([request, 'access', _126 => _126.config, 'optionalAccess', _127 => _127.topP])
2331
- };
2332
- if (stream) {
2333
- const result = _ai.streamText.call(void 0, requestParams);
2334
- let accumulatedText = "";
2335
- for await (const delta of result.textStream) {
2336
- accumulatedText += delta;
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
- role: "model",
2340
- parts: [{ text: accumulatedText }]
2245
+ parts,
2246
+ role: "model"
2341
2247
  },
2342
- partial: true
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
- const toolCalls = await result.toolCalls;
2346
- const parts = [];
2347
- if (accumulatedText) {
2348
- parts.push({ text: accumulatedText });
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
+ }
2274
+ }
2349
2275
  }
2350
- if (toolCalls && toolCalls.length > 0) {
2351
- for (const toolCall of toolCalls) {
2352
- parts.push({
2353
- functionCall: {
2354
- id: toolCall.toolCallId,
2355
- name: toolCall.toolName,
2356
- 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
+ });
2357
2294
  }
2358
- });
2295
+ }
2359
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;
2360
2312
  }
2361
- const finalUsage = await result.usage;
2362
- const finishReason = await result.finishReason;
2363
- yield new LlmResponse({
2364
- content: {
2365
- role: "model",
2366
- parts: parts.length > 0 ? parts : [{ text: "" }]
2367
- },
2368
- usageMetadata: finalUsage ? {
2369
- promptTokenCount: finalUsage.promptTokens,
2370
- candidatesTokenCount: finalUsage.completionTokens,
2371
- totalTokenCount: finalUsage.totalTokens
2372
- } : void 0,
2373
- finishReason: this.mapFinishReason(finishReason),
2374
- turnComplete: true
2375
- });
2376
- } else {
2377
- const result = await _ai.generateText.call(void 0, requestParams);
2313
+ }
2314
+ if ((text || thoughtText) && usageMetadata) {
2378
2315
  const parts = [];
2379
- if (result.text) {
2380
- parts.push({ text: result.text });
2316
+ if (thoughtText) {
2317
+ parts.push({ text: thoughtText, thought: true });
2381
2318
  }
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
2389
- }
2390
- });
2391
- }
2319
+ if (text) {
2320
+ parts.push({ text });
2392
2321
  }
2393
2322
  yield new LlmResponse({
2394
2323
  content: {
2395
- role: "model",
2396
- parts: parts.length > 0 ? parts : [{ text: "" }]
2324
+ parts,
2325
+ role: "model"
2397
2326
  },
2398
- usageMetadata: result.usage ? {
2399
- promptTokenCount: result.usage.promptTokens,
2400
- candidatesTokenCount: result.usage.completionTokens,
2401
- totalTokenCount: result.usage.totalTokens
2402
- } : void 0,
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
- } catch (error) {
2408
- this.logger.error(`AI SDK Error: ${String(error)}`, { error, request });
2409
- yield LlmResponse.fromError(error, {
2410
- errorCode: "AI_SDK_ERROR",
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
- * Convert ADK LlmRequest to AI SDK CoreMessage format
2353
+ * Live connection is not supported for OpenAI models
2417
2354
  */
2418
- convertToAiSdkMessages(llmRequest) {
2419
- const messages = [];
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
- * Convert ADK tools to AI SDK tools format
2359
+ * Create LlmResponse from streaming chunk - similar to Google's LlmResponse.create
2430
2360
  */
2431
- convertToAiSdkTools(llmRequest) {
2432
- const tools = {};
2433
- if (_optionalChain([llmRequest, 'access', _128 => _128.config, 'optionalAccess', _129 => _129.tools])) {
2434
- for (const toolConfig of llmRequest.config.tools) {
2435
- if ("functionDeclarations" in toolConfig) {
2436
- for (const funcDecl of toolConfig.functionDeclarations) {
2437
- tools[funcDecl.name] = {
2438
- description: funcDecl.description,
2439
- parameters: _ai.jsonSchema.call(void 0, funcDecl.parameters || {})
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 tools;
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 ADK Content to AI SDK CoreMessage
2397
+ * Convert OpenAI message to ADK LlmResponse
2449
2398
  */
2450
- contentToAiSdkMessage(content) {
2451
- const role = this.mapRole(content.role);
2452
- if (!content.parts || content.parts.length === 0) {
2453
- return null;
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 (_optionalChain([content, 'access', _130 => _130.parts, 'optionalAccess', _131 => _131.some, 'call', _132 => _132((part) => part.functionCall)])) {
2466
- const textParts = content.parts.filter((part) => part.text);
2467
- const functionCalls = content.parts.filter((part) => part.functionCall);
2468
- const contentParts2 = [];
2469
- for (const textPart of textParts) {
2470
- if (textPart.text) {
2471
- contentParts2.push({
2472
- type: "text",
2473
- text: textPart.text
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
- content: contentParts2
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', _133 => _133.parts, 'optionalAccess', _134 => _134.some, 'call', _135 => _135((part) => part.functionResponse)])) {
2493
- const functionResponses = content.parts.filter(
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
- content: contentParts2
2468
+ tool_call_id: functionResponsePart.functionResponse.id || "",
2469
+ content: JSON.stringify(
2470
+ functionResponsePart.functionResponse.response || {}
2471
+ )
2505
2472
  };
2506
2473
  }
2507
- const contentParts = [];
2508
- for (const part of content.parts) {
2509
- if (part.text) {
2510
- contentParts.push({
2511
- type: "text",
2512
- text: part.text
2513
- });
2514
- }
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
+ };
2515
2479
  }
2516
- if (contentParts.length === 0) {
2517
- return null;
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
+ };
2518
2496
  }
2519
- if (contentParts.length === 1) {
2520
- const textContent = contentParts[0].text;
2521
- if (role === "system") {
2522
- return { role: "system", content: textContent };
2523
- }
2524
- if (role === "assistant") {
2525
- return { role: "assistant", content: textContent };
2526
- }
2527
- return { role: "user", content: textContent };
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
+ };
2528
2504
  }
2529
- if (role === "system") {
2530
- const combinedText = contentParts.map((p) => p.text).join("");
2531
- return { role: "system", content: combinedText };
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));
2532
2513
  }
2533
- if (role === "assistant") {
2534
- return { role: "assistant", content: contentParts };
2514
+ if (!schema || typeof schema !== "object") {
2515
+ return schema;
2535
2516
  }
2536
- return { role: "user", content: contentParts };
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;
2537
2543
  }
2538
2544
  /**
2539
- * Map ADK role to AI SDK role
2545
+ * Convert ADK function declaration to OpenAI tool
2540
2546
  */
2541
- mapRole(role) {
2542
- switch (role) {
2543
- case "model":
2544
- case "assistant":
2545
- return "assistant";
2546
- case "system":
2547
- return "system";
2548
- default:
2549
- return "user";
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";
2550
2568
  }
2569
+ return "user";
2551
2570
  }
2552
2571
  /**
2553
- * Map AI SDK finish reason to ADK finish reason
2572
+ * Convert OpenAI finish reason to ADK finish reason
2554
2573
  */
2555
- mapFinishReason(finishReason) {
2556
- switch (finishReason) {
2574
+ toAdkFinishReason(openaiFinishReason) {
2575
+ switch (openaiFinishReason) {
2557
2576
  case "stop":
2558
- case "end_of_message":
2577
+ case "tool_calls":
2559
2578
  return "STOP";
2560
2579
  case "length":
2561
- case "max_tokens":
2562
2580
  return "MAX_TOKENS";
2563
2581
  default:
2564
2582
  return "FINISH_REASON_UNSPECIFIED";
2565
2583
  }
2566
2584
  }
2567
- }, _class7);
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
+ }
2598
+ }
2599
+ }
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;
2608
+ }
2609
+ }
2610
+ }
2611
+ /**
2612
+ * Detect content type for flow control
2613
+ * This is a simplified implementation - you may need to adjust based on your specific requirements
2614
+ */
2615
+ getContentType(content) {
2616
+ if (content.includes("<thinking>") || content.includes("[thinking]")) {
2617
+ return "thought";
2618
+ }
2619
+ return "regular";
2620
+ }
2621
+ /**
2622
+ * Check if response has inline data (similar to Google LLM)
2623
+ */
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
+ });
2642
+ }
2643
+ return this._client;
2644
+ }
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
  /**