From fdd42323588e35838835f7df35795d775dcffef6 Mon Sep 17 00:00:00 2001 From: didi Date: Thu, 18 Jun 2026 20:11:38 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20agent=20template=20marketplace=20?= =?UTF-8?q?=E2=80=94=20GET=20/templates,=20POST=20/templates/:id/use,=205?= =?UTF-8?q?=20builtin=20seeds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/src/app.module.ts | 2 + apps/api/src/templates/template.entity.ts | 41 +++++++ .../api/src/templates/templates.controller.ts | 32 ++++++ apps/api/src/templates/templates.module.ts | 13 +++ apps/api/src/templates/templates.seed.ts | 106 ++++++++++++++++++ apps/api/src/templates/templates.service.ts | 49 ++++++++ 6 files changed, 243 insertions(+) create mode 100644 apps/api/src/templates/template.entity.ts create mode 100644 apps/api/src/templates/templates.controller.ts create mode 100644 apps/api/src/templates/templates.module.ts create mode 100644 apps/api/src/templates/templates.seed.ts create mode 100644 apps/api/src/templates/templates.service.ts diff --git a/apps/api/src/app.module.ts b/apps/api/src/app.module.ts index ebddb17..e7a04f1 100644 --- a/apps/api/src/app.module.ts +++ b/apps/api/src/app.module.ts @@ -13,6 +13,7 @@ import { FlowsModule } from './flows/flows.module'; import { TriggersModule } from './triggers/triggers.module'; import { ExecutionsModule } from './executions/executions.module'; import { GatewayModule } from './gateway/gateway.module'; +import { TemplatesModule } from './templates/templates.module'; @Module({ imports: [ @@ -40,6 +41,7 @@ import { GatewayModule } from './gateway/gateway.module'; TriggersModule, ExecutionsModule, GatewayModule, + TemplatesModule, ], providers: [ { provide: APP_GUARD, useClass: JwtAuthGuard }, diff --git a/apps/api/src/templates/template.entity.ts b/apps/api/src/templates/template.entity.ts new file mode 100644 index 0000000..9e97a86 --- /dev/null +++ b/apps/api/src/templates/template.entity.ts @@ -0,0 +1,41 @@ +import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm'; +import type { FlowGraph } from '../flows/entities/flow.entity'; + +export const TemplateCategory = { + AI: 'ai', + DATA: 'data', + AUTOMATION: 'automation', + MONITORING: 'monitoring', + COMMUNICATION: 'communication', +} as const; +export type TemplateCategory = typeof TemplateCategory[keyof typeof TemplateCategory]; + +@Entity('templates') +export class Template { + @PrimaryGeneratedColumn('uuid') + id!: string; + + @Column() + name!: string; + + @Column({ type: 'text' }) + description!: string; + + @Column({ type: 'varchar' }) + category!: TemplateCategory; + + @Column({ nullable: true, type: 'varchar' }) + icon!: string | null; + + @Column({ type: 'jsonb' }) + graph!: FlowGraph; + + @Column({ default: 0 }) + useCount!: number; + + @Column({ default: true }) + isBuiltin!: boolean; + + @CreateDateColumn() + createdAt!: Date; +} diff --git a/apps/api/src/templates/templates.controller.ts b/apps/api/src/templates/templates.controller.ts new file mode 100644 index 0000000..5eb6f3d --- /dev/null +++ b/apps/api/src/templates/templates.controller.ts @@ -0,0 +1,32 @@ +import { Controller, Get, Post, Param, Query, UseGuards } from '@nestjs/common'; +import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; +import { TemplatesService } from './templates.service'; +import { JwtAuthGuard } from '../common/guards/jwt-auth.guard'; +import { CurrentWorkspaceId } from '../common/decorators/current-user.decorator'; +import type { TemplateCategory } from './template.entity'; + +@ApiTags('Templates') +@Controller('templates') +export class TemplatesController { + constructor(private readonly service: TemplatesService) {} + + @Get() + findAll(@Query('category') category?: TemplateCategory) { + return this.service.findAll(category); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.service.findOne(id); + } + + @Post(':id/use') + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + useTemplate( + @Param('id') id: string, + @CurrentWorkspaceId() workspaceId: string, + ) { + return this.service.useTemplate(id, workspaceId); + } +} diff --git a/apps/api/src/templates/templates.module.ts b/apps/api/src/templates/templates.module.ts new file mode 100644 index 0000000..debedb7 --- /dev/null +++ b/apps/api/src/templates/templates.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Template } from './template.entity'; +import { TemplatesService } from './templates.service'; +import { TemplatesController } from './templates.controller'; +import { FlowsModule } from '../flows/flows.module'; + +@Module({ + imports: [TypeOrmModule.forFeature([Template]), FlowsModule], + providers: [TemplatesService], + controllers: [TemplatesController], +}) +export class TemplatesModule {} diff --git a/apps/api/src/templates/templates.seed.ts b/apps/api/src/templates/templates.seed.ts new file mode 100644 index 0000000..fe3bfa5 --- /dev/null +++ b/apps/api/src/templates/templates.seed.ts @@ -0,0 +1,106 @@ +import { TemplateCategory } from './template.entity'; +import type { FlowGraph } from '../flows/entities/flow.entity'; + +interface SeedTemplate { + name: string; + description: string; + category: TemplateCategory; + icon: string; + graph: FlowGraph; + isBuiltin: boolean; +} + +export const BUILTIN_TEMPLATES: SeedTemplate[] = [ + { + name: 'Web Scraper → Summarize → Email', + description: 'Scrape a webpage, summarize the content with an LLM, and send the summary by email.', + category: TemplateCategory.AI, + icon: '🌐', + isBuiltin: true, + graph: { + nodes: [ + { id: 'n1', type: 'web-scraper', position: { x: 100, y: 200 }, data: { label: 'Web Scraper', config: { url: '', selector: 'body' } } }, + { id: 'n2', type: 'ai-llm', position: { x: 400, y: 200 }, data: { label: 'Summarize', config: { prompt: 'Summarize this content in 3 bullet points: {{input}}', model: 'gpt-4o-mini' } } }, + { id: 'n3', type: 'email-sender', position: { x: 700, y: 200 }, data: { label: 'Send Email', config: { to: '', subject: 'Daily Summary' } } }, + ], + edges: [ + { id: 'e1', source: 'n1', target: 'n2' }, + { id: 'e2', source: 'n2', target: 'n3' }, + ], + }, + }, + { + name: 'GitHub PR Review', + description: 'Fetch open pull requests from a GitHub repo and generate an AI code review for each.', + category: TemplateCategory.AI, + icon: '🔍', + isBuiltin: true, + graph: { + nodes: [ + { id: 'n1', type: 'api-caller', position: { x: 100, y: 200 }, data: { label: 'Fetch PRs', config: { url: 'https://api.github.com/repos/{{owner}}/{{repo}}/pulls', method: 'GET', headers: { Authorization: 'Bearer {{github_token}}' } } } }, + { id: 'n2', type: 'ai-llm', position: { x: 400, y: 200 }, data: { label: 'Review Code', config: { prompt: 'Review this pull request and identify potential issues: {{input}}', model: 'gpt-4o' } } }, + { id: 'n3', type: 'webhook-sender', position: { x: 700, y: 200 }, data: { label: 'Post Comment', config: { url: '', method: 'POST' } } }, + ], + edges: [ + { id: 'e1', source: 'n1', target: 'n2' }, + { id: 'e2', source: 'n2', target: 'n3' }, + ], + }, + }, + { + name: 'Daily News Digest', + description: 'Fetch news from an RSS feed, filter relevant items, and compile a daily digest.', + category: TemplateCategory.AUTOMATION, + icon: '📰', + isBuiltin: true, + graph: { + nodes: [ + { id: 'n1', type: 'rss-reader', position: { x: 100, y: 200 }, data: { label: 'RSS Feed', config: { url: '', maxItems: 20 } } }, + { id: 'n2', type: 'ai-llm', position: { x: 400, y: 200 }, data: { label: 'Filter & Rank', config: { prompt: 'From these news items, select the 5 most important and explain why: {{input}}', model: 'gpt-4o-mini' } } }, + { id: 'n3', type: 'email-sender', position: { x: 700, y: 200 }, data: { label: 'Send Digest', config: { to: '', subject: 'Daily News Digest' } } }, + ], + edges: [ + { id: 'e1', source: 'n1', target: 'n2' }, + { id: 'e2', source: 'n2', target: 'n3' }, + ], + }, + }, + { + name: 'API Health Monitor', + description: 'Ping a list of API endpoints, check response times, and alert on failures.', + category: TemplateCategory.MONITORING, + icon: '🔔', + isBuiltin: true, + graph: { + nodes: [ + { id: 'n1', type: 'api-caller', position: { x: 100, y: 200 }, data: { label: 'Health Check', config: { url: '', method: 'GET', timeout: 5000 } } }, + { id: 'n2', type: 'condition', position: { x: 400, y: 200 }, data: { label: 'Check Status', config: { condition: '{{status}} !== 200 || {{responseTime}} > 2000' } } }, + { id: 'n3', type: 'webhook-sender', position: { x: 700, y: 100 }, data: { label: 'Alert Slack', config: { url: '', method: 'POST' } } }, + { id: 'n4', type: 'logger', position: { x: 700, y: 300 }, data: { label: 'Log OK', config: { level: 'info' } } }, + ], + edges: [ + { id: 'e1', source: 'n1', target: 'n2' }, + { id: 'e2', source: 'n2', target: 'n3', label: 'fail' }, + { id: 'e3', source: 'n2', target: 'n4', label: 'pass' }, + ], + }, + }, + { + name: 'Data Extractor & CSV Export', + description: 'Extract structured data from text or HTML using an LLM and format it as CSV.', + category: TemplateCategory.DATA, + icon: '📊', + isBuiltin: true, + graph: { + nodes: [ + { id: 'n1', type: 'web-scraper', position: { x: 100, y: 200 }, data: { label: 'Scrape Data', config: { url: '', selector: 'table' } } }, + { id: 'n2', type: 'ai-llm', position: { x: 400, y: 200 }, data: { label: 'Extract Structure', config: { prompt: 'Extract all data from this content and return as JSON array with consistent keys: {{input}}', model: 'gpt-4o-mini' } } }, + { id: 'n3', type: 'transformer', position: { x: 700, y: 200 }, data: { label: 'To CSV', config: { format: 'csv' } } }, + ], + edges: [ + { id: 'e1', source: 'n1', target: 'n2' }, + { id: 'e2', source: 'n2', target: 'n3' }, + ], + }, + }, +]; diff --git a/apps/api/src/templates/templates.service.ts b/apps/api/src/templates/templates.service.ts new file mode 100644 index 0000000..4618d14 --- /dev/null +++ b/apps/api/src/templates/templates.service.ts @@ -0,0 +1,49 @@ +import { Injectable, NotFoundException, OnModuleInit } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Template, TemplateCategory } from './template.entity'; +import { FlowsService } from '../flows/flows.service'; +import type { Flow } from '../flows/entities/flow.entity'; +import { BUILTIN_TEMPLATES } from './templates.seed'; + +@Injectable() +export class TemplatesService implements OnModuleInit { + constructor( + @InjectRepository(Template) + private readonly repo: Repository