n8n-nodes-handit
Version:
n8n community nodes for Handit tracing and prompt management
174 lines • 7.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HanditPromptFetcher = void 0;
const n8n_workflow_1 = require("n8n-workflow");
class HanditPromptFetcher {
constructor() {
this.description = {
displayName: 'Handit Prompt Fetcher',
name: 'handitPromptFetcher',
icon: 'file:handit.svg',
group: ['input'],
version: 1,
description: 'Fetch active prompts from Handit API using agent name (auto-converted to slug) and create prompt variables for LLM nodes',
defaults: {
name: 'Handit Prompt Fetcher',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'handitApi',
required: true,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Fetch Prompts',
value: 'fetchPrompts',
description: 'Fetch production prompts for an agent',
action: 'Fetch production prompts',
},
],
default: 'fetchPrompts',
noDataExpression: true,
},
{
displayName: 'Agent Name',
name: 'agentName',
type: 'string',
default: '',
required: true,
placeholder: 'My Agent Name',
description: 'The name of the agent (will be converted to camelCase slug automatically)',
displayOptions: {
show: {
operation: ['fetchPrompts'],
},
},
},
{
displayName: 'API URL',
name: 'apiUrl',
type: 'string',
default: 'https://handit-api-oss-299768392189.us-central1.run.app/api/prompts/{agentSlug}/active',
description: 'Custom API endpoint URL (leave empty to use the default Handit API endpoint)',
displayOptions: {
show: {
operation: ['fetchPrompts'],
},
},
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const operation = this.getNodeParameter('operation', 0);
function nameToSlug(name) {
return name
.toLowerCase()
.replace(/[^\w\s]/g, '')
.split(/\s+/)
.map((word, index) => {
if (index === 0)
return word;
return word.charAt(0).toUpperCase() + word.slice(1);
})
.join('');
}
if (operation === 'fetchPrompts') {
for (let i = 0; i < items.length; i++) {
try {
const agentName = this.getNodeParameter('agentName', i);
const apiUrl = this.getNodeParameter('apiUrl', i);
const credentials = await this.getCredentials('handitApi');
if (!(credentials === null || credentials === void 0 ? void 0 : credentials.apiToken)) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), {}, {
message: 'Handit API credentials are required'
});
}
const agentSlug = nameToSlug(agentName);
const defaultApiEndpoint = 'https://handit-api-oss-299768392189.us-central1.run.app/api/prompts/{agentSlug}/active';
const apiEndpoint = apiUrl
? apiUrl.replace('{agentSlug}', agentSlug)
: defaultApiEndpoint.replace('{agentSlug}', agentSlug);
const requestOptions = {
method: 'GET',
url: apiEndpoint,
json: true,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${credentials.apiToken}`,
},
};
const response = await this.helpers.httpRequest(requestOptions);
let outputData = {
...items[i].json,
agentName,
agentSlug,
fetchedAt: new Date().toISOString(),
};
if (!response || typeof response !== 'object') {
throw new n8n_workflow_1.NodeApiError(this.getNode(), response, {
message: 'Invalid response from API: expected JSON object',
});
}
const activePrompts = response;
outputData.prompts = activePrompts;
outputData.promptsJson = JSON.stringify(activePrompts, null, 2);
if (typeof activePrompts === 'object' && activePrompts !== null) {
for (const [nodeSlug, prompt] of Object.entries(activePrompts)) {
const variableName = `prompt_${nodeSlug}`;
outputData[variableName] = prompt;
}
}
outputData.promptMetadata = {
agentName,
agentSlug,
apiEndpoint,
fetchedAt: outputData.fetchedAt,
totalPrompts: typeof activePrompts === 'object' ? Object.keys(activePrompts).length : 0,
availableNodes: typeof activePrompts === 'object' ? Object.keys(activePrompts) : [],
};
outputData.success = true;
const metadata = outputData.promptMetadata;
outputData.message = `Successfully fetched ${metadata.totalPrompts} prompts for agent "${agentName}" (${agentSlug})`;
returnData.push({
json: outputData,
pairedItem: {
item: i,
},
});
}
catch (error) {
const agentName = this.getNodeParameter('agentName', i);
const agentSlug = nameToSlug(agentName);
const errorData = {
...items[i].json,
success: false,
error: error.message,
timestamp: new Date().toISOString(),
agentName,
agentSlug,
};
returnData.push({
json: errorData,
pairedItem: {
item: i,
},
});
}
}
}
return [returnData];
}
}
exports.HanditPromptFetcher = HanditPromptFetcher;
//# sourceMappingURL=HanditPromptFetcher.node.js.map