Repository navigation
fix(go): seed built-in agent templates for Go backend - #16666
Conversation
The Go backend never populated the canvas_template table, so the "Create agent from template" page was blank when running with API_PROXY_SCHEME=go. - Add SeedCanvasTemplates() that loads agent/templates/*.json and mirrors Python's add_graph_templates() behaviour. - Call it from InitDB() after migrations. - Add CanvasTypes JSONSlice field to CanvasTemplate so the frontend can filter/group templates correctly. - Skip seeding if the templates directory is missing (e.g. tests). Fixes the blank template catalogue in Go mode.
📝 WalkthroughWalkthroughAdds a new canvas template seeding module that reads JSON files from an agent/templates directory and populates the canvas_template table, wires this seeding into database initialization after migrations, and extends the CanvasTemplate entity with a new CanvasTypes field. ChangesCanvas Template Seeding
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant InitDB
participant SeedCanvasTemplates
participant FileSystem
participant Database
InitDB->>SeedCanvasTemplates: call after RunMigrations
SeedCanvasTemplates->>FileSystem: findAgentTemplatesDir()
FileSystem-->>SeedCanvasTemplates: templates dir path or empty
SeedCanvasTemplates->>Database: delete existing canvas_template rows
SeedCanvasTemplates->>FileSystem: read sorted .json files
FileSystem-->>SeedCanvasTemplates: raw JSON bytes
SeedCanvasTemplates->>SeedCanvasTemplates: parseCanvasTemplateFile(raw)
SeedCanvasTemplates->>Database: insert parsed CanvasTemplate
SeedCanvasTemplates-->>InitDB: return nil or logged warning
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/dao/canvas_template_seed.go`:
- Around line 39-83: Wrap the canvas template seeding flow in a DB.Transaction
so the delete and all inserts are atomic. In canvas_template_seed.go, update the
seeding logic around findAgentTemplatesDir, DB.Exec("DELETE FROM
canvas_template"), and DB.Create(tmpl) so any read/parse/save failure returns an
error from the transaction instead of only logging and continuing, ensuring the
previous state is restored on abort rather than leaving canvas_template empty or
partially populated.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 78edebd6-a04c-4f8c-8f1c-afa605026a03
📒 Files selected for processing (3)
internal/dao/canvas_template_seed.gointernal/dao/database.gointernal/entity/canvas.go
| dir := findAgentTemplatesDir() | ||
| if dir == "" { | ||
| common.Warn("Agent templates directory not found, skipping canvas template seeding") | ||
| return nil | ||
| } | ||
|
|
||
| entries, err := os.ReadDir(dir) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read agent templates directory %s: %w", dir, err) | ||
| } | ||
|
|
||
| // Match Python's filter_delete([1 == 1]): start from a clean slate so | ||
| // removed built-ins disappear and updated files take effect. | ||
| if err := DB.Exec("DELETE FROM canvas_template").Error; err != nil { | ||
| return fmt.Errorf("failed to clear canvas_template: %w", err) | ||
| } | ||
|
|
||
| sort.Slice(entries, func(i, j int) bool { return entries[i].Name() < entries[j].Name() }) | ||
|
|
||
| var seeded int | ||
| for _, entry := range entries { | ||
| if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") { | ||
| continue | ||
| } | ||
| path := filepath.Join(dir, entry.Name()) | ||
| raw, err := os.ReadFile(path) | ||
| if err != nil { | ||
| common.Warn("Failed to read agent template", zap.String("file", path), zap.Error(err)) | ||
| continue | ||
| } | ||
| tmpl, err := parseCanvasTemplateFile(raw) | ||
| if err != nil { | ||
| common.Warn("Failed to parse agent template", zap.String("file", path), zap.Error(err)) | ||
| continue | ||
| } | ||
| if err := DB.Create(tmpl).Error; err != nil { | ||
| common.Warn("Failed to save agent template", zap.String("file", path), zap.Error(err)) | ||
| continue | ||
| } | ||
| seeded++ | ||
| } | ||
|
|
||
| common.Info("Seeded canvas templates", zap.Int("count", seeded), zap.String("dir", dir)) | ||
| return nil | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify whether canvas_template is used to store anything other than built-in templates
# (i.e. whether unconditional full-table DELETE could destroy user data).
rg -nP -C4 'canvas_template' --type=go internal/dao internal/service internal/api 2>/dev/null
rg -nP -C4 '\bDB\.Create\(.*CanvasTemplate' --type=goRepository: infiniflow/ragflow
Length of output: 4188
Wrap seeding in a transaction
DELETE FROM canvas_template runs before any insert, and parse/save failures are only logged and skipped. If seeding aborts halfway, the table is left empty or partially populated while the function still returns nil. Wrap the delete and inserts in DB.Transaction(...) so a failure rolls back to the previous state instead of leaving the app with no templates.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/dao/canvas_template_seed.go` around lines 39 - 83, Wrap the canvas
template seeding flow in a DB.Transaction so the delete and all inserts are
atomic. In canvas_template_seed.go, update the seeding logic around
findAgentTemplatesDir, DB.Exec("DELETE FROM canvas_template"), and
DB.Create(tmpl) so any read/parse/save failure returns an error from the
transaction instead of only logging and continuing, ensuring the previous state
is restored on abort rather than leaving canvas_template empty or partially
populated.
Summary
The Go backend never seeded the
canvas_templatetable, so the "Create agent from template" page was blank when the frontend proxies to the Go API (API_PROXY_SCHEME=go). This PR addsSeedCanvasTemplates()ininternal/dao, invoked fromInitDB()after migrations, which loadsagent/templates/*.jsonand mirrors Python'sadd_graph_templates()behavior.Changes:
internal/dao/canvas_template_seed.goto parse and upsert built-in templates.SeedCanvasTemplates()inInitDB().CanvasTypes(JSONSlice) toentity.CanvasTemplateso the frontend can filter/group by category.This fixes the blank template catalogue in Go mode.