n8n
Version:
n8n Workflow Automation Tool
78 lines • 3.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchApiDocs = fetchApiDocs;
const backend_common_1 = require("@n8n/backend-common");
const di_1 = require("@n8n/di");
const CONTEXT7_BASE_URL = 'https://context7.com/api/v2';
const FETCH_TIMEOUT_MS = 10_000;
const FALLBACK_INSTRUCTIONS = 'No API documentation was found for this endpoint. Generate the response based on your knowledge of this API. Follow standard REST conventions for the HTTP method: GET returns resource data, POST returns the created resource, PUT/PATCH returns the updated resource, DELETE returns 204 or confirmation.';
const docsCache = new Map();
let context7WarningLogged = false;
async function resolveLibraryId(serviceName) {
const cacheKey = `lib:${serviceName}`;
if (docsCache.has(cacheKey))
return docsCache.get(cacheKey);
try {
const apiKey = process.env.CONTEXT7_API_KEY;
const headers = {};
if (apiKey)
headers.Authorization = `Bearer ${apiKey}`;
const url = `${CONTEXT7_BASE_URL}/libs/search?libraryName=${encodeURIComponent(serviceName + ' API')}&query=REST+API+endpoints+response+format`;
const res = await fetch(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), headers });
if (!res.ok) {
const body = await res.text().catch(() => '');
if (body.includes('Quota') || body.includes('quota') || res.status === 429) {
if (!context7WarningLogged) {
di_1.Container.get(backend_common_1.Logger).warn('[EvalMock] Context7 quota exceeded — mock responses will rely on LLM training data. Set CONTEXT7_API_KEY for higher limits.');
context7WarningLogged = true;
}
}
return undefined;
}
const results = (await res.json());
const best = results.sort((a, b) => (b.trust_score ?? 0) - (a.trust_score ?? 0))[0];
if (best?.id) {
docsCache.set(cacheKey, best.id);
return best.id;
}
}
catch (error) {
di_1.Container.get(backend_common_1.Logger).debug(`[EvalMock] Context7 library search failed for "${serviceName}": ${error instanceof Error ? error.message : String(error)}`);
}
return undefined;
}
async function fetchApiDocs(serviceName, endpointQuery) {
const cacheKey = `docs:${serviceName}:${endpointQuery}`;
if (docsCache.has(cacheKey))
return docsCache.get(cacheKey);
const libraryId = await resolveLibraryId(serviceName);
if (!libraryId)
return FALLBACK_INSTRUCTIONS;
try {
const apiKey = process.env.CONTEXT7_API_KEY;
const headers = {};
if (apiKey)
headers.Authorization = `Bearer ${apiKey}`;
const url = `${CONTEXT7_BASE_URL}/context?libraryId=${encodeURIComponent(libraryId)}&query=${encodeURIComponent(endpointQuery)}&type=txt`;
const res = await fetch(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), headers });
if (!res.ok) {
if (res.status === 429) {
if (!context7WarningLogged) {
di_1.Container.get(backend_common_1.Logger).warn('[EvalMock] Context7 quota exceeded — mock responses will rely on LLM training data. Set CONTEXT7_API_KEY for higher limits.');
context7WarningLogged = true;
}
}
return FALLBACK_INSTRUCTIONS;
}
const text = await res.text();
if (!text.trim())
return FALLBACK_INSTRUCTIONS;
docsCache.set(cacheKey, text);
return text;
}
catch (error) {
di_1.Container.get(backend_common_1.Logger).debug(`[EvalMock] Context7 docs fetch failed for "${serviceName}": ${error instanceof Error ? error.message : String(error)}`);
return FALLBACK_INSTRUCTIONS;
}
}
//# sourceMappingURL=api-docs.js.map