@n8n/n8n-nodes-langchain
Version:
130 lines • 5.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.googleVertexAuthentication = exports.googleVertexCredentials = exports.GOOGLE_VERTEX_CUSTOM_PROJECT = void 0;
exports.resolveGoogleVertexCredentials = resolveGoogleVertexCredentials;
exports.googleVertexAiCredentialTest = googleVertexAiCredentialTest;
exports.searchGoogleProjects = searchGoogleProjects;
const get_error_message_1 = require("@n8n/utils/errors/get-error-message");
const google_service_account_1 = require("n8n-nodes-base/google-service-account");
const n8n_workflow_1 = require("n8n-workflow");
const vertex_location_1 = require("../nodes/llms/gemini-common/vertex-location");
exports.GOOGLE_VERTEX_CUSTOM_PROJECT = '__custom__';
exports.googleVertexCredentials = [
{
name: 'googleApi',
required: true,
displayOptions: { show: { authentication: ['googleApi'] } },
},
{
name: 'googleVertexAiApi',
required: true,
testedBy: 'googleVertexAiCredentialTest',
displayOptions: { show: { authentication: ['googleVertexAiApi'] } },
},
];
exports.googleVertexAuthentication = {
displayName: 'Authentication',
name: 'authentication',
type: 'options',
noDataExpression: true,
options: [
{ name: 'Google Service Account', value: 'googleApi' },
{ name: 'Google Vertex AI', value: 'googleVertexAiApi' },
],
default: 'googleApi',
};
function getGoogleVertexProjectId(credentials) {
const project = credentials.project ?? exports.GOOGLE_VERTEX_CUSTOM_PROJECT;
return project === exports.GOOGLE_VERTEX_CUSTOM_PROJECT ? credentials.projectId : project;
}
async function resolveGoogleVertexCredentials(context, itemIndex) {
const authentication = context.getNodeParameter('authentication', itemIndex, 'googleApi');
const credentialType = authentication === 'googleVertexAiApi' ? 'googleVertexAiApi' : 'googleApi';
const credentials = await context.getCredentials(credentialType);
let projectId;
if (credentialType === 'googleVertexAiApi') {
projectId = getGoogleVertexProjectId(credentials);
}
else {
projectId = context.getNodeParameter('projectId', itemIndex, '', { extractValue: true });
}
if (typeof projectId !== 'string' || !projectId.trim()) {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Select or enter a Google Cloud project ID.');
}
if (typeof credentials.region !== 'string') {
throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Select a Region in the credential.');
}
const locationOverride = context.getNodeParameter('location', itemIndex, '');
const location = (0, vertex_location_1.resolveVertexLocation)(typeof locationOverride === 'string' ? locationOverride : undefined, credentials.region);
return {
projectId: projectId.trim(),
location,
credentials: (0, google_service_account_1.getGoogleServiceAccountCredentials)(credentials),
};
}
async function googleVertexAiCredentialTest(credential) {
const data = credential.data ?? {};
const projectId = getGoogleVertexProjectId(data);
if (typeof projectId !== 'string' || !projectId.trim()) {
return { status: 'Error', message: 'Select or enter a Google Cloud project ID.' };
}
try {
const { client_email, private_key } = (0, google_service_account_1.getGoogleServiceAccountCredentials)(data);
const token = await google_service_account_1.getGoogleAccessToken.call(this, { email: client_email, privateKey: private_key }, 'vertex');
if (typeof token.access_token !== 'string' || !token.access_token) {
return {
status: 'Error',
message: 'Could not get an access token. Check the service account email and private key.',
};
}
await this.helpers.request({
method: 'GET',
uri: (0, n8n_workflow_1.requestUrl) `https://aiplatform.googleapis.com/v1/projects/${projectId.trim()}/locations`,
headers: { Authorization: `Bearer ${token.access_token}` },
qs: { pageSize: 1 },
json: true,
timeout: 10000,
});
return { status: 'OK', message: 'Connection successful' };
}
catch (error) {
return {
status: 'Error',
message: `Could not connect to Vertex AI. Check the service account details, project ID, and Vertex AI permissions. ${(0, get_error_message_1.getErrorMessage)(error)}`,
};
}
}
async function searchGoogleProjects(credentials, filter, paginationToken) {
const { ProjectsClient } = await import('@google-cloud/resource-manager');
const client = new ProjectsClient({
credentials: (0, google_service_account_1.getGoogleServiceAccountCredentials)(credentials),
});
try {
const trimmedFilter = filter?.trim();
let query;
if (trimmedFilter) {
const prefix = JSON.stringify(`${trimmedFilter}*`);
query = `displayName:${prefix} projectId:${prefix}`;
}
const [projects, nextPage] = await client.searchProjects({ pageToken: paginationToken, ...(query ? { query } : {}) }, { autoPaginate: false });
return {
results: projects.flatMap((project) => {
if (!project.projectId)
return [];
return [
{
name: project.displayName
? `${project.displayName} (${project.projectId})`
: project.projectId,
value: project.projectId,
},
];
}),
paginationToken: nextPage?.pageToken ?? undefined,
};
}
finally {
await client.close();
}
}
//# sourceMappingURL=google-vertex.js.map