n8n
Version:
n8n Workflow Automation Tool
50 lines • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAskCredentialTool = buildAskCredentialTool;
const tool_1 = require("@n8n/agents/tool");
const api_types_1 = require("@n8n/api-types");
function withNodeCredentialMap(input, resume) {
if ('skipped' in resume)
return resume;
const credentialSlot = input.credentialSlot ?? input.credentialType;
return {
credentialId: resume.credentialId,
credentialName: resume.credentialName,
credentials: {
[credentialSlot]: {
id: resume.credentialId,
name: resume.credentialName,
},
},
};
}
function buildAskCredentialTool(deps) {
return (new tool_1.Tool(api_types_1.ASK_CREDENTIAL_TOOL_NAME)
.description('Show a credential picker card in the chat UI and suspend until the user selects ' +
'a credential. Call ONCE per credential slot, BEFORE the write_config / patch_config ' +
'that introduces the node tool. Returns { credentialId, credentialName, credentials } on success ' +
'or { skipped: true } if the user skips credential setup so the tool can be added ' +
'without credentials. For node tools, copy the returned `credentials` object into `node.credentials`. Auto-resolves without ' +
'rendering a card when the user has exactly one credential of the requested type.')
.input(api_types_1.askCredentialInputSchema)
.suspend(api_types_1.askCredentialInputSchema)
.resume(api_types_1.askCredentialResumeSchema)
.handler(async (input, ctx) => {
if (ctx.resumeData !== undefined)
return withNodeCredentialMap(input, ctx.resumeData);
if (deps.isCredentialTypeKnown && !deps.isCredentialTypeKnown(input.credentialType)) {
throw new Error(`Unknown credential type "${input.credentialType}". Use an exact n8n credential type name.`);
}
const all = await deps.credentialProvider.list();
const matching = all.filter((c) => c.type === input.credentialType);
if (matching.length === 1) {
return withNodeCredentialMap(input, {
credentialId: matching[0].id,
credentialName: matching[0].name,
});
}
return await ctx.suspend(input);
})
.build());
}
//# sourceMappingURL=ask-credential.tool.js.map