feat: support multiple canvas_types for agent templates and remove duplicate files (#14030)

### What problem does this PR solve?

Closes #13907

The template catalog had duplicate files (e.g. `*_r.json`) only to place
the same template into multiple sidebar groups.
This increases maintenance cost and makes template updates error-prone.

This PR adds first-class support for multiple template categories in a
single file via `canvas_types`, then removes duplicate template files.

What changed:
- Added `canvas_types` to `CanvasTemplate` model and DB migration.
- Added normalization logic when loading templates:
  - accepts legacy `canvas_type`
  - accepts new `canvas_types`
  - merges/deduplicates values
- preserves backward compatibility by keeping `canvas_type` as first
normalized value.
- Updated template import flow to load only `.json` files and in stable
sorted order.
- Updated frontend template filtering to match on `canvas_types` first,
with fallback to legacy `canvas_type`.
- Consolidated duplicated template pairs into single files and removed:
  - `deep_search_r.json`
  - `reflective_academic_paper_generator_r.json`
  - `seo_article_writer_r.json`
- Added regression/edge-case tests for category normalization and route
serialization expectations.

### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):
This commit is contained in:
bitloi
2026-04-13 09:26:30 -03:00
committed by GitHub
parent ef07faea80
commit 853021ff2a
13 changed files with 175 additions and 2120 deletions

View File

@@ -1063,6 +1063,7 @@ class CanvasTemplate(DataBaseModel):
title = JSONField(null=True, default=dict, help_text="Canvas title")
description = JSONField(null=True, default=dict, help_text="Canvas description")
canvas_type = CharField(max_length=32, null=True, help_text="Canvas type", index=True)
canvas_types = ListField(null=True, default=list, help_text="Canvas types")
canvas_category = CharField(max_length=32, null=False, default="agent_canvas", help_text="Canvas category: agent_canvas|dataflow_canvas", index=True)
dsl = JSONField(null=True, default={})
@@ -1615,6 +1616,7 @@ def migrate_db():
alter_db_column_type(migrator, "canvas_template", "description", JSONField(null=True, default=dict, help_text="Canvas description"))
alter_db_add_column(migrator, "user_canvas", "canvas_category", CharField(max_length=32, null=False, default="agent_canvas", help_text="agent_canvas|dataflow_canvas", index=True))
alter_db_add_column(migrator, "canvas_template", "canvas_category", CharField(max_length=32, null=False, default="agent_canvas", help_text="agent_canvas|dataflow_canvas", index=True))
alter_db_add_column(migrator, "canvas_template", "canvas_types", ListField(null=True, default=list, help_text="Canvas types"))
alter_db_add_column(migrator, "knowledgebase", "pipeline_id", CharField(max_length=32, null=True, help_text="Pipeline ID", index=True))
alter_db_add_column(migrator, "document", "pipeline_id", CharField(max_length=32, null=True, help_text="Pipeline ID", index=True))
alter_db_add_column(migrator, "knowledgebase", "graphrag_task_id", CharField(max_length=32, null=True, help_text="Gragh RAG task ID", index=True))

View File

@@ -35,6 +35,7 @@ from api.db.services.llm_service import LLMService, LLMBundle, get_init_tenant_l
from api.db.services.user_service import TenantService, UserTenantService
from api.db.services.system_settings_service import SystemSettingsService
from api.db.services.dialog_service import DialogService
from api.db.template_utils import normalize_canvas_template_categories
from api.db.joint_services.memory_message_service import init_message_id_sequence, init_memory_size_cache, fix_missing_tokenized_memory
from api.db.joint_services.tenant_model_service import get_tenant_default_model_by_type
from common.constants import LLMType
@@ -166,16 +167,21 @@ def add_graph_templates():
logging.warning("Missing agent templates!")
return
for fnm in os.listdir(dir):
for fnm in sorted(os.listdir(dir)):
if not fnm.endswith(".json"):
logging.debug("Skipping non-json template file in %s: %s", dir, fnm)
continue
template_path = os.path.join(dir, fnm)
try:
with open(os.path.join(dir, fnm), "r", encoding="utf-8") as f:
cnvs = json.load(f)
with open(template_path, "r", encoding="utf-8") as f:
cnvs = normalize_canvas_template_categories(json.load(f))
logging.info("Loaded and normalized template file: %s", template_path)
try:
CanvasTemplateService.save(**cnvs)
except Exception:
CanvasTemplateService.update_by_id(cnvs["id"], cnvs)
except Exception as e:
logging.exception(f"Add agent templates error: {e}")
logging.exception("Add agent templates error for %s: %s", template_path, e)
def init_web_data():

77
api/db/template_utils.py Normal file
View File

@@ -0,0 +1,77 @@
#
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
from typing import Any
logger = logging.getLogger(__name__)
def _collect_canvas_types(canvas_type: Any, canvas_types: Any) -> list[str]:
categories: list[str] = []
if isinstance(canvas_type, str):
category = canvas_type.strip()
if category:
categories.append(category)
iterable_types: list[Any]
if isinstance(canvas_types, list):
iterable_types = canvas_types
elif canvas_types is None:
iterable_types = []
else:
iterable_types = [canvas_types]
for item in iterable_types:
if not isinstance(item, str):
continue
category = item.strip()
if not category:
continue
categories.append(category)
deduplicated: list[str] = []
seen: set[str] = set()
for category in categories:
if category in seen:
continue
seen.add(category)
deduplicated.append(category)
return deduplicated
def normalize_canvas_template_categories(template: dict[str, Any]) -> dict[str, Any]:
normalized = dict(template)
raw_canvas_type = normalized.get("canvas_type")
raw_canvas_types = normalized.get("canvas_types")
canvas_types = _collect_canvas_types(
raw_canvas_type,
raw_canvas_types,
)
normalized["canvas_types"] = canvas_types
normalized["canvas_type"] = canvas_types[0] if canvas_types else None
if raw_canvas_type != normalized["canvas_type"] or raw_canvas_types != normalized["canvas_types"]:
logger.debug(
"Normalized canvas categories for template_id=%s: canvas_type=%r -> %r, canvas_types=%r -> %r",
normalized.get("id"),
raw_canvas_type,
normalized["canvas_type"],
raw_canvas_types,
normalized["canvas_types"],
)
return normalized