feat: enhance template tooling with pagination and flexible retrieval

- Add pagination support to all template search/list tools
  - Consistent response format with total, limit, offset, hasMore
  - Support for customizable limits (1-100) and offsets

- Add new list_templates tool for browsing all templates
  - Returns minimal data (id, name, views, node count)
  - Supports sorting by views, created_at, or name
  - Efficient for discovering available templates

- Enhance get_template with flexible response modes
  - nodes_only: Just list of node types (minimal tokens)
  - structure: Nodes with positions and connections
  - full: Complete workflow JSON (default)

- Update database_statistics to show template count
  - Shows total templates, average/min/max views
  - Provides complete database overview

- Add count methods to repository for pagination
  - getSearchCount, getNodeTemplatesCount, getTaskTemplatesCount
  - Enables accurate pagination info

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-09-14 15:04:17 +02:00
parent f35097ed46
commit e7895d2e01
5 changed files with 339 additions and 65 deletions

View File

@@ -173,17 +173,17 @@ export class TemplateRepository {
/**
* Get templates that use specific node types
*/
getTemplatesByNodes(nodeTypes: string[], limit: number = 10): StoredTemplate[] {
getTemplatesByNodes(nodeTypes: string[], limit: number = 10, offset: number = 0): StoredTemplate[] {
// Build query for multiple node types
const conditions = nodeTypes.map(() => "nodes_used LIKE ?").join(" OR ");
const query = `
SELECT * FROM templates
WHERE ${conditions}
ORDER BY views DESC, created_at DESC
LIMIT ?
LIMIT ? OFFSET ?
`;
const params = [...nodeTypes.map(n => `%"${n}"%`), limit];
const params = [...nodeTypes.map(n => `%"${n}"%`), limit, offset];
const results = this.db.prepare(query).all(...params) as StoredTemplate[];
return results.map(t => this.decompressWorkflow(t));
}
@@ -232,13 +232,13 @@ export class TemplateRepository {
/**
* Search templates by name or description
*/
searchTemplates(query: string, limit: number = 20): StoredTemplate[] {
searchTemplates(query: string, limit: number = 20, offset: number = 0): StoredTemplate[] {
logger.debug(`Searching templates for: "${query}" (FTS5: ${this.hasFTS5Support})`);
// If FTS5 is not supported, go straight to LIKE search
if (!this.hasFTS5Support) {
logger.debug('Using LIKE search (FTS5 not available)');
return this.searchTemplatesLIKE(query, limit);
return this.searchTemplatesLIKE(query, limit, offset);
}
try {
@@ -255,8 +255,8 @@ export class TemplateRepository {
JOIN templates_fts ON t.id = templates_fts.rowid
WHERE templates_fts MATCH ?
ORDER BY rank, t.views DESC
LIMIT ?
`).all(ftsQuery, limit) as StoredTemplate[];
LIMIT ? OFFSET ?
`).all(ftsQuery, limit, offset) as StoredTemplate[];
logger.debug(`FTS5 search returned ${results.length} results`);
return results.map(t => this.decompressWorkflow(t));
@@ -267,14 +267,14 @@ export class TemplateRepository {
query: query,
ftsQuery: query.split(' ').map(term => `"${term}"`).join(' OR ')
});
return this.searchTemplatesLIKE(query, limit);
return this.searchTemplatesLIKE(query, limit, offset);
}
}
/**
* Fallback search using LIKE when FTS5 is not available
*/
private searchTemplatesLIKE(query: string, limit: number = 20): StoredTemplate[] {
private searchTemplatesLIKE(query: string, limit: number = 20, offset: number = 0): StoredTemplate[] {
const likeQuery = `%${query}%`;
logger.debug(`Using LIKE search with pattern: ${likeQuery}`);
@@ -282,8 +282,8 @@ export class TemplateRepository {
SELECT * FROM templates
WHERE name LIKE ? OR description LIKE ?
ORDER BY views DESC, created_at DESC
LIMIT ?
`).all(likeQuery, likeQuery, limit) as StoredTemplate[];
LIMIT ? OFFSET ?
`).all(likeQuery, likeQuery, limit, offset) as StoredTemplate[];
logger.debug(`LIKE search returned ${results.length} results`);
return results.map(t => this.decompressWorkflow(t));
@@ -292,7 +292,7 @@ export class TemplateRepository {
/**
* Get templates for a specific task/use case
*/
getTemplatesForTask(task: string): StoredTemplate[] {
getTemplatesForTask(task: string, limit: number = 10, offset: number = 0): StoredTemplate[] {
// Map tasks to relevant node combinations
const taskNodeMap: Record<string, string[]> = {
'ai_automation': ['@n8n/n8n-nodes-langchain.openAi', '@n8n/n8n-nodes-langchain.agent', 'n8n-nodes-base.openAi'],
@@ -312,18 +312,21 @@ export class TemplateRepository {
return [];
}
return this.getTemplatesByNodes(nodes, 10);
return this.getTemplatesByNodes(nodes, limit, offset);
}
/**
* Get all templates with limit
*/
getAllTemplates(limit: number = 10): StoredTemplate[] {
getAllTemplates(limit: number = 10, offset: number = 0, sortBy: 'views' | 'created_at' | 'name' = 'views'): StoredTemplate[] {
const orderClause = sortBy === 'name' ? 'name ASC' :
sortBy === 'created_at' ? 'created_at DESC' :
'views DESC, created_at DESC';
const results = this.db.prepare(`
SELECT * FROM templates
ORDER BY views DESC, created_at DESC
LIMIT ?
`).all(limit) as StoredTemplate[];
ORDER BY ${orderClause}
LIMIT ? OFFSET ?
`).all(limit, offset) as StoredTemplate[];
return results.map(t => this.decompressWorkflow(t));
}
@@ -335,6 +338,77 @@ export class TemplateRepository {
return result.count;
}
/**
* Get count for search results
*/
getSearchCount(query: string): number {
if (!this.hasFTS5Support) {
const likeQuery = `%${query}%`;
const result = this.db.prepare(`
SELECT COUNT(*) as count FROM templates
WHERE name LIKE ? OR description LIKE ?
`).get(likeQuery, likeQuery) as { count: number };
return result.count;
}
try {
const ftsQuery = query.split(' ').map(term => {
const escaped = term.replace(/"/g, '""');
return `"${escaped}"`;
}).join(' OR ');
const result = this.db.prepare(`
SELECT COUNT(*) as count FROM templates t
JOIN templates_fts ON t.id = templates_fts.rowid
WHERE templates_fts MATCH ?
`).get(ftsQuery) as { count: number };
return result.count;
} catch {
const likeQuery = `%${query}%`;
const result = this.db.prepare(`
SELECT COUNT(*) as count FROM templates
WHERE name LIKE ? OR description LIKE ?
`).get(likeQuery, likeQuery) as { count: number };
return result.count;
}
}
/**
* Get count for node templates
*/
getNodeTemplatesCount(nodeTypes: string[]): number {
const conditions = nodeTypes.map(() => "nodes_used LIKE ?").join(" OR ");
const query = `SELECT COUNT(*) as count FROM templates WHERE ${conditions}`;
const params = nodeTypes.map(n => `%"${n}"%`);
const result = this.db.prepare(query).get(...params) as { count: number };
return result.count;
}
/**
* Get count for task templates
*/
getTaskTemplatesCount(task: string): number {
const taskNodeMap: Record<string, string[]> = {
'ai_automation': ['@n8n/n8n-nodes-langchain.openAi', '@n8n/n8n-nodes-langchain.agent', 'n8n-nodes-base.openAi'],
'data_sync': ['n8n-nodes-base.googleSheets', 'n8n-nodes-base.postgres', 'n8n-nodes-base.mysql'],
'webhook_processing': ['n8n-nodes-base.webhook', 'n8n-nodes-base.httpRequest'],
'email_automation': ['n8n-nodes-base.gmail', 'n8n-nodes-base.emailSend', 'n8n-nodes-base.emailReadImap'],
'slack_integration': ['n8n-nodes-base.slack', 'n8n-nodes-base.slackTrigger'],
'data_transformation': ['n8n-nodes-base.code', 'n8n-nodes-base.set', 'n8n-nodes-base.merge'],
'file_processing': ['n8n-nodes-base.readBinaryFile', 'n8n-nodes-base.writeBinaryFile', 'n8n-nodes-base.googleDrive'],
'scheduling': ['n8n-nodes-base.scheduleTrigger', 'n8n-nodes-base.cron'],
'api_integration': ['n8n-nodes-base.httpRequest', 'n8n-nodes-base.graphql'],
'database_operations': ['n8n-nodes-base.postgres', 'n8n-nodes-base.mysql', 'n8n-nodes-base.mongodb']
};
const nodes = taskNodeMap[task];
if (!nodes) {
return 0;
}
return this.getNodeTemplatesCount(nodes);
}
/**
* Get all existing template IDs for comparison
* Used in update mode to skip already fetched templates

View File

@@ -21,6 +21,21 @@ export interface TemplateWithWorkflow extends TemplateInfo {
workflow: any;
}
export interface PaginatedResponse<T> {
items: T[];
total: number;
limit: number;
offset: number;
hasMore: boolean;
}
export interface TemplateMinimal {
id: number;
name: string;
views: number;
nodeCount: number;
}
export class TemplateService {
private repository: TemplateRepository;
@@ -31,40 +46,115 @@ export class TemplateService {
/**
* List templates that use specific node types
*/
async listNodeTemplates(nodeTypes: string[], limit: number = 10): Promise<TemplateInfo[]> {
const templates = this.repository.getTemplatesByNodes(nodeTypes, limit);
return templates.map(this.formatTemplateInfo);
async listNodeTemplates(nodeTypes: string[], limit: number = 10, offset: number = 0): Promise<PaginatedResponse<TemplateInfo>> {
const templates = this.repository.getTemplatesByNodes(nodeTypes, limit, offset);
const total = this.repository.getNodeTemplatesCount(nodeTypes);
return {
items: templates.map(this.formatTemplateInfo),
total,
limit,
offset,
hasMore: offset + limit < total
};
}
/**
* Get a specific template with full workflow
* Get a specific template with different detail levels
*/
async getTemplate(templateId: number): Promise<TemplateWithWorkflow | null> {
async getTemplate(templateId: number, mode: 'nodes_only' | 'structure' | 'full' = 'full'): Promise<any> {
const template = this.repository.getTemplate(templateId);
if (!template) {
return null;
}
const workflow = JSON.parse(template.workflow_json || '{}');
if (mode === 'nodes_only') {
return {
id: template.id,
name: template.name,
nodes: workflow.nodes?.map((n: any) => ({
type: n.type,
name: n.name
})) || []
};
}
if (mode === 'structure') {
return {
id: template.id,
name: template.name,
nodes: workflow.nodes?.map((n: any) => ({
id: n.id,
type: n.type,
name: n.name,
position: n.position
})) || [],
connections: workflow.connections || {}
};
}
// Full mode
return {
...this.formatTemplateInfo(template),
workflow: JSON.parse(template.workflow_json || '{}')
workflow
};
}
/**
* Search templates by query
*/
async searchTemplates(query: string, limit: number = 20): Promise<TemplateInfo[]> {
const templates = this.repository.searchTemplates(query, limit);
return templates.map(this.formatTemplateInfo);
async searchTemplates(query: string, limit: number = 20, offset: number = 0): Promise<PaginatedResponse<TemplateInfo>> {
const templates = this.repository.searchTemplates(query, limit, offset);
const total = this.repository.getSearchCount(query);
return {
items: templates.map(this.formatTemplateInfo),
total,
limit,
offset,
hasMore: offset + limit < total
};
}
/**
* Get templates for a specific task
*/
async getTemplatesForTask(task: string): Promise<TemplateInfo[]> {
const templates = this.repository.getTemplatesForTask(task);
return templates.map(this.formatTemplateInfo);
async getTemplatesForTask(task: string, limit: number = 10, offset: number = 0): Promise<PaginatedResponse<TemplateInfo>> {
const templates = this.repository.getTemplatesForTask(task, limit, offset);
const total = this.repository.getTaskTemplatesCount(task);
return {
items: templates.map(this.formatTemplateInfo),
total,
limit,
offset,
hasMore: offset + limit < total
};
}
/**
* List all templates with minimal data
*/
async listTemplates(limit: number = 10, offset: number = 0, sortBy: 'views' | 'created_at' | 'name' = 'views'): Promise<PaginatedResponse<TemplateMinimal>> {
const templates = this.repository.getAllTemplates(limit, offset, sortBy);
const total = this.repository.getTemplateCount();
const items = templates.map(t => ({
id: t.id,
name: t.name,
views: t.views,
nodeCount: JSON.parse(t.nodes_used).length
}));
return {
items,
total,
limit,
offset,
hasMore: offset + limit < total
};
}
/**