adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
185 lines (184 loc) • 7.97 kB
JavaScript
"use strict";
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationIntegrationToolset = void 0;
const OpenAPIToolset_1 = require("../openapi-tool/openapi-spec-parser/OpenAPIToolset");
const IntegrationConnectorTool_1 = require("./IntegrationConnectorTool");
const IntegrationClient_1 = require("./IntegrationClient");
const ConnectionsClient_1 = require("./ConnectionsClient");
/**
* Mock AuthCredential to avoid importing the real one which may have dependencies
*/
class AuthCredential {
constructor() {
this.authType = 'oauth2';
this.serviceAccount = {};
}
}
/**
* Toolset for Application Integration
*/
class ApplicationIntegrationToolset {
/**
* Creates a new ApplicationIntegrationToolset
*
* @param project GCP project ID
* @param location GCP location
* @param options Options for creating the toolset
*/
constructor(project, location, options) {
this.project = project;
this.location = location;
/**
* Connections client
*/
this.connectionsClient = null;
/**
* OpenAPI toolset
*/
this.openApiToolset = null;
/**
* OpenAPI spec parser
*/
this.openApiSpecParser = null;
/**
* Tools
*/
this.tools = [];
// Validate required parameters
this.validateParams(options);
// Initialize client
this.integrationClient = new IntegrationClient_1.IntegrationClient(project, location, options.integration || null, options.trigger || null, options.connection || null, options.entityOperations || null, options.actions || null, options.serviceAccountJson || null);
// Initialize connections client if needed
if (options.connection) {
this.connectionsClient = new ConnectionsClient_1.ConnectionsClient(project, location, options.connection, options.serviceAccountJson || null);
}
// Create auth credential
const authCredential = this.createAuthCredential(options.serviceAccountJson);
// Setup tools based on the provided options
if (options.integration && options.trigger) {
this.setupIntegrationTools(authCredential);
}
else if (options.connection && (options.entityOperations || options.actions)) {
this.setupConnectionTools(options.connection, options.entityOperations || [], options.actions || [], options.toolName || 'Connection Tool', options.toolInstructions || 'Use this tool to interact with the connection', authCredential);
}
}
/**
* Validates required parameters
* @param options Options to validate
* @throws Error if parameters are invalid
*/
validateParams(options) {
const hasIntegrationAndTrigger = !!(options.integration && options.trigger);
const hasConnectionAndOperations = !!(options.connection &&
(options.entityOperations?.length || options.actions?.length));
if (!hasIntegrationAndTrigger && !hasConnectionAndOperations) {
throw new Error('Either (integration and trigger) or (connection and ' +
'(entityOperations or actions)) should be provided.');
}
}
/**
* Creates an auth credential
* @param serviceAccountJson Service account JSON credentials
* @returns Auth credential
*/
createAuthCredential(serviceAccountJson) {
const authCredential = new AuthCredential();
if (serviceAccountJson) {
// Parse service account JSON
const serviceAccount = JSON.parse(serviceAccountJson);
authCredential.serviceAccount = {
serviceAccountCredential: {
clientEmail: serviceAccount.client_email,
privateKey: serviceAccount.private_key,
privateKeyId: serviceAccount.private_key_id,
}
};
}
else {
// Use default credentials
authCredential.serviceAccount = {
useDefaultCredential: true
};
}
return authCredential;
}
/**
* Sets up tools for integration
* @param authCredential Auth credential
*/
setupIntegrationTools(authCredential) {
// Get OpenAPI spec for integration
const openApiSpec = this.integrationClient.getOpenApiSpecForIntegration();
// Create OpenAPI toolset - we need to cast the authCredential to any to bypass type checking
// since this is just a mock implementation for tests
this.openApiToolset = new OpenAPIToolset_1.OpenAPIToolset({
specDict: openApiSpec,
authCredential: authCredential
});
// Get tools from OpenAPI toolset
this.tools = this.openApiToolset.getTools();
}
/**
* Sets up tools for connection
* @param connection Connection name
* @param entityOperations Entity operations
* @param actions Actions
* @param toolName Tool name
* @param toolInstructions Tool instructions
* @param authCredential Auth credential
*/
setupConnectionTools(connection, entityOperations, actions, toolName, toolInstructions, authCredential) {
// Get connection details
const connectionDetails = this.connectionsClient.getConnectionDetails();
// Get OpenAPI spec for connection
const openApiSpec = this.integrationClient.getOpenApiSpecForConnection(toolName, toolInstructions);
// Create OpenAPI toolset with the spec directly instead of using OpenApiSpecParser
this.openApiToolset = new OpenAPIToolset_1.OpenAPIToolset({
specDict: openApiSpec,
authCredential: authCredential
});
// Get tools from OpenAPI toolset
const parsedTools = this.openApiToolset.getTools();
// Create IntegrationConnectorTool wrappers for each parsed tool
this.tools = parsedTools.map(restApiTool => {
// Get the underlying operation from the REST API tool
const operation = restApiTool.parsedOperation?.operation;
if (!operation) {
return restApiTool;
}
// Check if the operation has entity or action metadata
if (operation['x-entity'] && operation['x-operation'] === 'LIST_ENTITIES') {
// Entity operation
return new IntegrationConnectorTool_1.IntegrationConnectorTool(restApiTool.name, restApiTool.description, connectionDetails.name, connectionDetails.host, connectionDetails.serviceName, operation['x-entity'], operation['x-operation'], '', restApiTool);
}
else if (operation['x-action'] && operation['x-operation'] === 'EXECUTE_ACTION') {
// Action operation
return new IntegrationConnectorTool_1.IntegrationConnectorTool(restApiTool.name, restApiTool.description, connectionDetails.name, connectionDetails.host, connectionDetails.serviceName, '', operation['x-operation'], operation['x-action'], restApiTool);
}
return restApiTool;
});
}
/**
* Gets the tools
* @returns The tools
*/
getTools() {
return this.tools;
}
}
exports.ApplicationIntegrationToolset = ApplicationIntegrationToolset;