Merge branch 'master' of http://192.168.0.200/knowledge-base-bj/deerflow-server into xhy
This commit is contained in:
commit
3e2182acc5
275 files changed
+29592
-11370
No files matched your search
@@ -7,7 +7,11 @@ import re
|
||||
from typing import Any
|
||||
from urllib.parse import quote
|
||||
|
||||
from deerflow.config.extensions_config import SkillDisplayConfig, get_extensions_config
|
||||
from deerflow.config.extensions_config import (
|
||||
SkillDisplayConfig,
|
||||
SkillDisplayResultSetConfig,
|
||||
get_extensions_config,
|
||||
)
|
||||
|
||||
|
||||
_SKILL_PATH_RE = re.compile(r"[\\/]skills[\\/](?:public|custom)[\\/]([^\\/]+)(?:[\\/]|$)")
|
||||
@@ -222,7 +226,11 @@ def adapt_reference_batches_with_displays(
|
||||
)
|
||||
except Exception:
|
||||
continue
|
||||
if not config.enabled or config.mode == "none":
|
||||
has_enabled_result_set = any(
|
||||
result_set.enabled and result_set.mode != "none"
|
||||
for result_set in config.result_sets
|
||||
)
|
||||
if not config.enabled or (config.mode == "none" and not has_enabled_result_set):
|
||||
continue
|
||||
normalized_displays[skill_name] = config
|
||||
|
||||
@@ -257,13 +265,30 @@ def adapt_reference_batches_with_displays(
|
||||
raw = source.get("raw")
|
||||
if display is not None and isinstance(raw, dict):
|
||||
index_value = int(source.get("index") or len(sources) + 1)
|
||||
display_for_source: SkillDisplayConfig | SkillDisplayResultSetConfig = display
|
||||
result_set_key = source.get("resultSetKey")
|
||||
if result_set_key:
|
||||
display_for_source = next(
|
||||
(
|
||||
result_set
|
||||
for result_set in display.result_sets
|
||||
if result_set.key == result_set_key
|
||||
),
|
||||
display,
|
||||
)
|
||||
elif display.result_sets and display.mode == "none":
|
||||
sources.append(source)
|
||||
continue
|
||||
configured = _normalize_with_display_config(
|
||||
raw,
|
||||
index_value,
|
||||
skill_name,
|
||||
display,
|
||||
display_for_source,
|
||||
)
|
||||
if configured is not None:
|
||||
if isinstance(display_for_source, SkillDisplayResultSetConfig):
|
||||
configured["resultSetKey"] = display_for_source.key
|
||||
configured["resultSetLabel"] = display_for_source.label
|
||||
sources.append(
|
||||
{
|
||||
**source,
|
||||
@@ -318,6 +343,8 @@ def _source_debug_item(source: dict[str, Any]) -> dict[str, Any]:
|
||||
"id": source.get("id"),
|
||||
"skillName": source.get("skillName"),
|
||||
"mode": source.get("mode"),
|
||||
"resultSetKey": source.get("resultSetKey"),
|
||||
"resultSetLabel": source.get("resultSetLabel"),
|
||||
"title": source.get("title"),
|
||||
"source": source.get("source"),
|
||||
"author": source.get("author"),
|
||||
@@ -359,6 +386,16 @@ def _collect_sources(messages: list[Any]) -> list[dict[str, Any]]:
|
||||
tool_contexts_by_id,
|
||||
)
|
||||
payload = _extract_tool_payload(message)
|
||||
configured_sources = _collect_configured_sources(
|
||||
payload,
|
||||
len(sources) + 1,
|
||||
tool_context,
|
||||
)
|
||||
if configured_sources is not None:
|
||||
for normalized in configured_sources:
|
||||
normalized["index"] = len(sources) + 1
|
||||
sources.append(normalized)
|
||||
continue
|
||||
for raw_item in _first_list(payload):
|
||||
normalized = _normalize_reference_item(
|
||||
raw_item,
|
||||
@@ -373,6 +410,44 @@ def _collect_sources(messages: list[Any]) -> list[dict[str, Any]]:
|
||||
return sources
|
||||
|
||||
|
||||
def _collect_configured_sources(
|
||||
payload: Any,
|
||||
fallback_index: int,
|
||||
tool_context: dict[str, Any] | None,
|
||||
) -> list[dict[str, Any]] | None:
|
||||
display_match = _resolve_display_match({}, payload, tool_context)
|
||||
if display_match is None:
|
||||
return None
|
||||
skill_name, display = display_match
|
||||
configs: list[SkillDisplayConfig | SkillDisplayResultSetConfig] = [
|
||||
result_set
|
||||
for result_set in display.result_sets
|
||||
if result_set.enabled and result_set.mode != "none"
|
||||
]
|
||||
if not configs:
|
||||
configs = [display]
|
||||
|
||||
sources: list[dict[str, Any]] = []
|
||||
for config in configs:
|
||||
raw_items = _list_at_path(payload, config.result_path)
|
||||
if raw_items is None:
|
||||
continue
|
||||
for raw_item in raw_items:
|
||||
normalized = _normalize_with_display_config(
|
||||
raw_item,
|
||||
fallback_index + len(sources),
|
||||
skill_name,
|
||||
config,
|
||||
)
|
||||
if normalized is None:
|
||||
continue
|
||||
if isinstance(config, SkillDisplayResultSetConfig):
|
||||
normalized["resultSetKey"] = config.key
|
||||
normalized["resultSetLabel"] = config.label
|
||||
sources.append(normalized)
|
||||
return sources
|
||||
|
||||
|
||||
def _find_reference_batch_anchor(messages: list[Any], start: int, end: int) -> Any | None:
|
||||
answer = _find_last_assistant_answer(messages, start, end)
|
||||
if answer is not None:
|
||||
@@ -622,6 +697,19 @@ def _first_list(payload: Any) -> list[Any]:
|
||||
return []
|
||||
|
||||
|
||||
def _list_at_path(payload: Any, path: str | None) -> list[dict[str, Any]] | None:
|
||||
cursor = payload
|
||||
if path:
|
||||
for part in path.split("."):
|
||||
if isinstance(cursor, dict):
|
||||
cursor = cursor.get(part)
|
||||
else:
|
||||
return None
|
||||
if not isinstance(cursor, list):
|
||||
return None
|
||||
return [item for item in cursor if isinstance(item, dict)]
|
||||
|
||||
|
||||
def _nested_records(raw: Any) -> list[dict[str, Any]]:
|
||||
if not isinstance(raw, dict):
|
||||
return []
|
||||
@@ -654,7 +742,13 @@ def _enabled_skill_displays() -> dict[str, SkillDisplayConfig]:
|
||||
if state.display is not None
|
||||
and state.enabled
|
||||
and state.display.enabled
|
||||
and state.display.mode != "none"
|
||||
and (
|
||||
state.display.mode != "none"
|
||||
or any(
|
||||
result_set.enabled and result_set.mode != "none"
|
||||
for result_set in state.display.result_sets
|
||||
)
|
||||
)
|
||||
}
|
||||
except Exception:
|
||||
return {}
|
||||
@@ -705,7 +799,7 @@ def _normalize_with_display_config(
|
||||
raw: dict[str, Any],
|
||||
fallback_index: int,
|
||||
skill_name: str,
|
||||
display: SkillDisplayConfig,
|
||||
display: SkillDisplayConfig | SkillDisplayResultSetConfig,
|
||||
) -> dict[str, Any] | None:
|
||||
title = _stringify(_get_by_path(raw, display.title_field), f"结果 {fallback_index}")
|
||||
snippet = _stringify(_get_by_path(raw, display.summary_field), "")
|
||||
@@ -737,7 +831,10 @@ def _normalize_with_display_config(
|
||||
}
|
||||
|
||||
|
||||
def _resolve_display_link(raw: dict[str, Any], display: SkillDisplayConfig) -> str | None:
|
||||
def _resolve_display_link(
|
||||
raw: dict[str, Any],
|
||||
display: SkillDisplayConfig | SkillDisplayResultSetConfig,
|
||||
) -> str | None:
|
||||
url = _stringify(_get_by_path(raw, display.url_field), "")
|
||||
item_id = _stringify(_get_by_path(raw, display.id_field), "")
|
||||
if display.link_strategy == "url":
|
||||
|
||||
+9
-1
@@ -1,8 +1,16 @@
|
||||
from .registry import get_scheduled_task_service, set_scheduled_task_service
|
||||
from .service import DuplicateTaskNameError, MarkdownRequirementError, ScheduledTaskService
|
||||
from .service import (
|
||||
DuplicateFavoriteError,
|
||||
DuplicateTaskNameError,
|
||||
HtmlGenerationError,
|
||||
MarkdownRequirementError,
|
||||
ScheduledTaskService,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"DuplicateFavoriteError",
|
||||
"DuplicateTaskNameError",
|
||||
"HtmlGenerationError",
|
||||
"MarkdownRequirementError",
|
||||
"ScheduledTaskService",
|
||||
"get_scheduled_task_service",
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ class ElementDeliveryClient:
|
||||
self.access_token = access_token
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "ElementDeliveryClient | None":
|
||||
def from_env(cls) -> ElementDeliveryClient | None:
|
||||
enabled = os.environ.get("ELEMENT_ENABLED", "true").strip().lower()
|
||||
if enabled in {"0", "false", "no", "off"}:
|
||||
return None
|
||||
|
||||
+414
@@ -0,0 +1,414 @@
|
||||
"""HTML page generation helpers for ``html_page`` scheduled tasks.
|
||||
|
||||
Pure, dependency-light utilities the scheduler uses to turn a GLM5 response
|
||||
into a safe, previewable HTML document:
|
||||
|
||||
- :func:`extract_json_object` — fault-tolerant JSON extraction from a model
|
||||
reply that may be wrapped in prose or ```json fences.
|
||||
- :func:`sanitize_html` — strip script-execution vectors (``<script>``,
|
||||
``<iframe>``, ``on*`` handlers, ``javascript:`` URLs, …) using BeautifulSoup.
|
||||
- :func:`validate_html` — cheap structural checks (doctype/html/head/body,
|
||||
non-empty body, no residual scripts) that gate the optional repair pass.
|
||||
- ``build_*_prompt`` — the GLM5 prompt templates (generation / repair / style
|
||||
extraction).
|
||||
- :func:`screenshot_html` — best-effort Playwright screenshot, a no-op when
|
||||
Playwright is not installed (the offline image ships without a browser).
|
||||
|
||||
Kept free of any ``app.*`` import so it stays inside the harness boundary.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Tags removed wholesale — they either execute code or load remote active
|
||||
# content that the iframe sandbox alone shouldn't be relied on to contain.
|
||||
_DANGEROUS_TAGS = frozenset(
|
||||
{"script", "iframe", "object", "embed", "applet", "frame", "frameset", "base"}
|
||||
)
|
||||
|
||||
# URL schemes that can execute script when navigated/loaded.
|
||||
_DANGEROUS_URL_RE = re.compile(r"^\s*(javascript|vbscript|data:text/html)", re.IGNORECASE)
|
||||
|
||||
# Attributes carrying a URL we must scheme-check.
|
||||
_URL_ATTRS = frozenset({"href", "src", "action", "formaction", "xlink:href", "background", "poster"})
|
||||
|
||||
|
||||
def parse_frontmatter(text: str) -> tuple[dict[str, Any], str]:
|
||||
"""Split a ``---`` YAML frontmatter block from the body of a SKILL.md.
|
||||
|
||||
Returns ``(frontmatter_dict, body)``. When there is no frontmatter the dict
|
||||
is empty and the body is the whole text.
|
||||
"""
|
||||
if not text or not text.lstrip().startswith("---"):
|
||||
return {}, text or ""
|
||||
lines = text.splitlines()
|
||||
# Skip leading blank lines before the opening fence.
|
||||
start = 0
|
||||
while start < len(lines) and not lines[start].strip():
|
||||
start += 1
|
||||
end = None
|
||||
for i in range(start + 1, len(lines)):
|
||||
if lines[i].strip() == "---":
|
||||
end = i
|
||||
break
|
||||
if end is None:
|
||||
return {}, text
|
||||
fm_block = "\n".join(lines[start + 1 : end])
|
||||
body = "\n".join(lines[end + 1 :])
|
||||
try:
|
||||
import yaml
|
||||
|
||||
data = yaml.safe_load(fm_block) or {}
|
||||
if not isinstance(data, dict):
|
||||
data = {}
|
||||
except Exception:
|
||||
data = {}
|
||||
return data, body
|
||||
|
||||
|
||||
def extract_json_object(text: str | None) -> dict[str, Any] | None:
|
||||
"""Best-effort parse of a JSON object from a model reply.
|
||||
|
||||
Handles three common shapes: a bare JSON object, a ```json fenced block,
|
||||
and a JSON object embedded in surrounding prose. Returns ``None`` when no
|
||||
balanced object can be recovered.
|
||||
"""
|
||||
if not text or not isinstance(text, str):
|
||||
return None
|
||||
candidate = text.strip()
|
||||
|
||||
# Strip a single ```json ... ``` (or ``` ... ```) fence if present.
|
||||
fence = re.search(r"```(?:json)?\s*(.*?)```", candidate, flags=re.DOTALL | re.IGNORECASE)
|
||||
if fence:
|
||||
candidate = fence.group(1).strip()
|
||||
|
||||
# Fast path: the whole thing is already valid JSON.
|
||||
try:
|
||||
parsed = json.loads(candidate)
|
||||
return parsed if isinstance(parsed, dict) else None
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
# Fallback: scan for the first balanced ``{ ... }`` and parse that.
|
||||
start = candidate.find("{")
|
||||
if start == -1:
|
||||
return None
|
||||
depth = 0
|
||||
in_string = False
|
||||
escape = False
|
||||
for index in range(start, len(candidate)):
|
||||
char = candidate[index]
|
||||
if in_string:
|
||||
if escape:
|
||||
escape = False
|
||||
elif char == "\\":
|
||||
escape = True
|
||||
elif char == '"':
|
||||
in_string = False
|
||||
continue
|
||||
if char == '"':
|
||||
in_string = True
|
||||
elif char == "{":
|
||||
depth += 1
|
||||
elif char == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
blob = candidate[start : index + 1]
|
||||
try:
|
||||
parsed = json.loads(blob)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
return parsed if isinstance(parsed, dict) else None
|
||||
return None
|
||||
|
||||
|
||||
def recover_html_document(text: str | None) -> str | None:
|
||||
"""Slice a complete HTML document out of a model reply.
|
||||
|
||||
Used as a fallback when the JSON wrapper around the ``html`` field can't be
|
||||
parsed — the model commonly emits *literal* newlines and *unescaped* quotes
|
||||
(``"zh-CN"``) inside the value, which is invalid JSON, so
|
||||
:func:`extract_json_object` returns ``None``. Without this, the caller would
|
||||
archive the whole ``{"title": ..., "html": "<!doctype html>…"}`` blob as the
|
||||
page. We locate the ``<!doctype html>`` / ``<html …>`` start and the final
|
||||
``</html>`` and return just that span, dropping the surrounding JSON
|
||||
scaffolding. Also accepts a reply that is already bare HTML. Returns
|
||||
``None`` when no HTML document can be found.
|
||||
"""
|
||||
if not text or not isinstance(text, str):
|
||||
return None
|
||||
lowered = text.lower()
|
||||
start = lowered.find("<!doctype html")
|
||||
if start == -1:
|
||||
start = lowered.find("<html")
|
||||
if start == -1:
|
||||
return None
|
||||
end = lowered.rfind("</html>")
|
||||
candidate = text[start:] if end == -1 else text[start : end + len("</html>")]
|
||||
candidate = candidate.strip()
|
||||
# If the html had been a properly-escaped JSON string value (the JSON failed
|
||||
# to parse for some *other* reason), unescape the common sequences so they
|
||||
# don't survive into the rendered page. Only triggers when escapes exist, so
|
||||
# genuinely-literal HTML (the usual broken-JSON case) is left untouched.
|
||||
if "\\n" in candidate or '\\"' in candidate or "\\/" in candidate or "\\t" in candidate:
|
||||
candidate = (
|
||||
candidate.replace('\\"', '"')
|
||||
.replace("\\n", "\n")
|
||||
.replace("\\r", "\r")
|
||||
.replace("\\t", "\t")
|
||||
.replace("\\/", "/")
|
||||
)
|
||||
return candidate or None
|
||||
|
||||
|
||||
def sanitize_html(html: str) -> tuple[str, list[str]]:
|
||||
"""Remove script-execution vectors from ``html``.
|
||||
|
||||
Returns ``(cleaned_html, removed)`` where ``removed`` is a human-readable
|
||||
report of what was stripped (used for logging / repair feedback). The
|
||||
cleaner is defensive but not a full HTML validator — pair it with an
|
||||
``iframe sandbox`` (no ``allow-scripts``) on the preview side.
|
||||
"""
|
||||
if not html or not isinstance(html, str):
|
||||
return "", []
|
||||
try:
|
||||
from bs4 import BeautifulSoup
|
||||
except Exception: # pragma: no cover - bs4 is a declared dependency
|
||||
logger.warning("BeautifulSoup unavailable; returning HTML unsanitized")
|
||||
return html, []
|
||||
|
||||
removed: list[str] = []
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
|
||||
# 1) Drop dangerous tags entirely.
|
||||
for tag in soup.find_all(True):
|
||||
name = (tag.name or "").lower()
|
||||
if name in _DANGEROUS_TAGS:
|
||||
removed.append(f"<{name}>")
|
||||
tag.decompose()
|
||||
continue
|
||||
# External stylesheet / preload links pull remote resources.
|
||||
if name == "link":
|
||||
href = str(tag.get("href") or "")
|
||||
rel = " ".join(tag.get("rel") or []) if isinstance(tag.get("rel"), list) else str(tag.get("rel") or "")
|
||||
if re.match(r"^\s*(https?:)?//", href) and "stylesheet" in rel.lower():
|
||||
removed.append("<link stylesheet>")
|
||||
tag.decompose()
|
||||
continue
|
||||
# meta refresh can redirect to arbitrary URLs.
|
||||
if name == "meta" and str(tag.get("http-equiv") or "").lower() == "refresh":
|
||||
removed.append("<meta refresh>")
|
||||
tag.decompose()
|
||||
continue
|
||||
|
||||
# 2) Strip event handlers and dangerous URL schemes from surviving tags.
|
||||
for tag in soup.find_all(True):
|
||||
for attr in list(tag.attrs.keys()):
|
||||
lowered = attr.lower()
|
||||
if lowered.startswith("on"):
|
||||
removed.append(f"{lowered}=")
|
||||
del tag.attrs[attr]
|
||||
continue
|
||||
if lowered in _URL_ATTRS:
|
||||
value = tag.attrs[attr]
|
||||
value_str = value if isinstance(value, str) else " ".join(value)
|
||||
if _DANGEROUS_URL_RE.match(value_str or ""):
|
||||
removed.append(f"{lowered}={value_str[:24]}")
|
||||
del tag.attrs[attr]
|
||||
|
||||
return str(soup), removed
|
||||
|
||||
|
||||
def validate_html(html: str) -> list[str]:
|
||||
"""Return a list of structural problems with ``html`` (empty == valid).
|
||||
|
||||
A page only counts as a complete document when it has the full skeleton
|
||||
(doctype/html/head/body), a non-empty ``<title>``, visible body content
|
||||
(no white-screen), no residual scripts, and no external stylesheet links.
|
||||
"""
|
||||
errors: list[str] = []
|
||||
if not html or not isinstance(html, str):
|
||||
return ["empty html"]
|
||||
lowered = html.lower()
|
||||
if "<!doctype html" not in lowered:
|
||||
errors.append("缺少 <!doctype html> 声明")
|
||||
if "<html" not in lowered:
|
||||
errors.append("缺少 <html> 根元素")
|
||||
if "<head" not in lowered:
|
||||
errors.append("缺少 <head> 元素")
|
||||
if "<body" not in lowered:
|
||||
errors.append("缺少 <body> 元素")
|
||||
if "<script" in lowered:
|
||||
errors.append("仍包含 <script> 标签(不允许脚本)")
|
||||
|
||||
try:
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
# Title must exist and be non-empty.
|
||||
title_el = soup.find("title")
|
||||
if title_el is None or not title_el.get_text(strip=True):
|
||||
errors.append("缺少非空 <title> 标题")
|
||||
# External stylesheet links pull remote resources — not allowed offline.
|
||||
for link in soup.find_all("link"):
|
||||
href = str(link.get("href") or "")
|
||||
rel = " ".join(link.get("rel") or []) if isinstance(link.get("rel"), list) else str(link.get("rel") or "")
|
||||
if re.match(r"^\s*(https?:)?//", href) and "stylesheet" in rel.lower():
|
||||
errors.append("包含外链样式表(不允许外部 CDN/资源)")
|
||||
break
|
||||
# Body must carry some visible content — guards against white-screen.
|
||||
body = soup.body or soup
|
||||
text = body.get_text(strip=True) if body else ""
|
||||
has_media = bool(body.find(["img", "svg", "canvas", "table", "ul", "ol"])) if body else False
|
||||
if len(text) < 5 and not has_media:
|
||||
errors.append("页面正文为空,可能白屏")
|
||||
except Exception: # pragma: no cover
|
||||
pass
|
||||
return errors
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Prompt templates
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_GENERATION_TEMPLATE = """你是一个内网页面生成助手。请根据输入数据生成一个完整的 HTML 页面。
|
||||
|
||||
用户页面要求:
|
||||
{page_prompt}
|
||||
{constraint_block}本次数据:
|
||||
{data}
|
||||
|
||||
参考页面风格:
|
||||
{reference_style}
|
||||
|
||||
参考页面布局:
|
||||
{reference_layout}
|
||||
{skill_instructions}
|
||||
要求:
|
||||
1. 只输出 JSON,不要输出 Markdown,不要输出多余解释。
|
||||
2. JSON 字段必须包含 title、html、style_summary、layout_summary。
|
||||
3. html 字段的值必须是合法 JSON 字符串:其中所有换行必须写成 \\n、所有双引号必须转义为 \\",不要在字符串里直接换行或使用未转义的引号。
|
||||
4. html 必须是完整 HTML 文档,包含 <!doctype html>、html、head、body。
|
||||
5. 所有 CSS 写在 <style> 标签内,不要引用外部 CDN 或外链样式表。
|
||||
6. 不要包含 <script>、外链脚本、iframe、表单提交、on* 事件属性。
|
||||
7. 参考页面只用于风格和布局,页面内容必须来自“本次数据”,不要复用参考页旧数据。
|
||||
8. 页面使用中文,排版适合在浏览器中直接预览。
|
||||
"""
|
||||
|
||||
_REPAIR_TEMPLATE = """你是一个 HTML 修复助手。下面的 HTML 存在问题,请修复后重新输出完整页面。
|
||||
|
||||
原始页面要求:
|
||||
{requirements}
|
||||
|
||||
发现的问题:
|
||||
{errors}
|
||||
|
||||
待修复 HTML:
|
||||
{html}
|
||||
|
||||
要求:
|
||||
1. 只输出 JSON,字段包含 fixed_html、changes(修复说明数组)。
|
||||
2. fixed_html 必须是完整 HTML 文档,包含 <!doctype html>、html、head、body。
|
||||
3. 移除所有 <script>、iframe、外链脚本、on* 事件属性、表单提交。
|
||||
4. 保留原有内容与数据,只修复结构与安全问题。
|
||||
"""
|
||||
|
||||
_STYLE_EXTRACT_TEMPLATE = """你是一个页面风格分析助手。请分析下面的 HTML 页面,提取可复用的风格与布局特征。
|
||||
|
||||
HTML 页面:
|
||||
{html}
|
||||
|
||||
要求:
|
||||
1. 只输出 JSON,字段包含 style_summary、layout_summary、content_structure_summary、reuse_prompt。
|
||||
2. style_summary:颜色、字体、间距、圆角、阴影、视觉密度等视觉风格。
|
||||
3. layout_summary:页面分区、卡片、表格、图表、导航等布局结构。
|
||||
4. content_structure_summary:信息组织顺序与层级。
|
||||
5. reuse_prompt:一段可直接拼接到下次生成 prompt 的中文风格说明,不要包含本页的具体数据。
|
||||
"""
|
||||
|
||||
|
||||
def _clip(value: Any, limit: int) -> str:
|
||||
text = value if isinstance(value, str) else json.dumps(value, ensure_ascii=False, default=str)
|
||||
text = text or ""
|
||||
return text if len(text) <= limit else text[:limit] + "\n…(内容过长已截断)"
|
||||
|
||||
|
||||
def build_generation_prompt(
|
||||
*,
|
||||
page_prompt: str,
|
||||
negative_prompt: str | None,
|
||||
data: Any,
|
||||
reference_style: str | None,
|
||||
reference_layout: str | None,
|
||||
skill_instructions: str | None = None,
|
||||
data_limit: int = 16000,
|
||||
skill_limit: int = 30000,
|
||||
) -> str:
|
||||
skill_block = ""
|
||||
if skill_instructions and skill_instructions.strip():
|
||||
skill_block = "\n可用页面生成技能规范(请严格遵循):\n" + _clip(skill_instructions.strip(), skill_limit) + "\n"
|
||||
# The constraint block is only emitted when the user actually supplied one,
|
||||
# and is framed as a top-priority hard rule that overrides the page
|
||||
# requirements / reference page / data on conflict — otherwise the model
|
||||
# tends to treat "限制条件" as a soft hint and ignore it.
|
||||
constraint_block = ""
|
||||
neg = (negative_prompt or "").strip()
|
||||
if neg:
|
||||
constraint_block = (
|
||||
"\n限制条件(最高优先级的硬性约束,必须逐条严格遵守;"
|
||||
"若与上面的页面要求、参考页面或本次数据发生冲突,一律以这里的限制条件为准):\n"
|
||||
+ neg
|
||||
+ "\n\n"
|
||||
)
|
||||
return _GENERATION_TEMPLATE.format(
|
||||
page_prompt=(page_prompt or "(未提供,使用通用看板风格)").strip(),
|
||||
constraint_block=constraint_block,
|
||||
data=_clip(data, data_limit),
|
||||
reference_style=(reference_style or "无").strip(),
|
||||
reference_layout=(reference_layout or "无").strip(),
|
||||
skill_instructions=skill_block,
|
||||
)
|
||||
|
||||
|
||||
def build_repair_prompt(*, html: str, errors: list[str], requirements: str | None) -> str:
|
||||
return _REPAIR_TEMPLATE.format(
|
||||
requirements=(requirements or "无").strip(),
|
||||
errors="\n".join(f"- {e}" for e in errors) or "- 未知问题",
|
||||
html=_clip(html, 32000),
|
||||
)
|
||||
|
||||
|
||||
def build_style_extract_prompt(*, html: str) -> str:
|
||||
return _STYLE_EXTRACT_TEMPLATE.format(html=_clip(html, 32000))
|
||||
|
||||
|
||||
async def screenshot_html(html: str, output_path) -> str | None:
|
||||
"""Best-effort PNG screenshot of ``html`` for white-screen / console QA.
|
||||
|
||||
Returns the screenshot path on success, or ``None`` when Playwright (or a
|
||||
usable browser) is unavailable — the offline image ships without one, so
|
||||
this must degrade silently rather than fail the run.
|
||||
"""
|
||||
try:
|
||||
from playwright.async_api import async_playwright
|
||||
except Exception:
|
||||
return None
|
||||
try:
|
||||
async with async_playwright() as pw:
|
||||
browser = await pw.chromium.launch()
|
||||
try:
|
||||
page = await browser.new_page()
|
||||
await page.set_content(html, wait_until="networkidle")
|
||||
await page.screenshot(path=str(output_path), full_page=True)
|
||||
finally:
|
||||
await browser.close()
|
||||
return str(output_path)
|
||||
except Exception:
|
||||
logger.debug("Playwright screenshot failed; skipping QA screenshot", exc_info=True)
|
||||
return None
|
||||
+488
-1
@@ -22,6 +22,7 @@ from deerflow.config.agents_config import AGENT_ID_PATTERN
|
||||
from deerflow.config.paths import get_paths
|
||||
from deerflow.persistence.scheduled_tasks import ScheduledTaskStore
|
||||
from deerflow.runtime import serialize_channel_values
|
||||
from deerflow.runtime.scheduler import html_page as html_page_utils
|
||||
from deerflow.runtime.scheduler.element import ElementDeliveryClient
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
from deerflow.utils.time import now_iso
|
||||
@@ -36,6 +37,11 @@ _RUN_ATTEMPT_TIMEOUT_SECONDS = 30 * 60 # 30 minutes
|
||||
# Retries allowed after the first attempt before the run is marked "failed".
|
||||
_MAX_RUN_RETRIES = 3
|
||||
|
||||
# Max HTML length (chars) inlined into the run's ``result_json``. Beyond this
|
||||
# the inline copy is dropped to stay under MySQL's 64 KB ``TEXT`` cap; the full
|
||||
# document is always retrievable from the page store (LONGTEXT) + index.html.
|
||||
_MAX_INLINE_RESULT_HTML = 60000
|
||||
|
||||
# Minimum length (in characters) the generated Markdown file's content must
|
||||
# reach when a task has ``require_markdown`` enabled. The model is NOT told about
|
||||
# this threshold — it is enforced silently by the service.
|
||||
@@ -58,6 +64,22 @@ class MarkdownRequirementError(Exception):
|
||||
"""
|
||||
|
||||
|
||||
class HtmlGenerationError(Exception):
|
||||
"""Raised when an ``html_page`` run did not produce a complete, valid HTML
|
||||
document (missing doctype/html/head/body, empty body, or residual scripts
|
||||
after the sanitize + repair passes). The run is marked failed — a page only
|
||||
counts as successful when it is a full, previewable HTML document."""
|
||||
|
||||
|
||||
class DuplicateFavoriteError(Exception):
|
||||
"""A user already bookmarked this page. Surfaced as HTTP 409 by the router
|
||||
so the same page cannot be favorited twice."""
|
||||
|
||||
def __init__(self, page_id: str) -> None:
|
||||
self.page_id = page_id
|
||||
super().__init__(f"Page {page_id!r} is already favorited")
|
||||
|
||||
|
||||
class DuplicateTaskNameError(Exception):
|
||||
"""A user already owns a scheduled task with the requested name.
|
||||
|
||||
@@ -835,7 +857,7 @@ class ScheduledTaskService:
|
||||
runtime_thread_id = str(uuid.uuid4())
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
self._run_agent_attempt(task, runtime_thread_id=runtime_thread_id, scheduled_for=scheduled_for, run_id=run_id),
|
||||
self._run_task_attempt(task, runtime_thread_id=runtime_thread_id, scheduled_for=scheduled_for, run_id=run_id),
|
||||
timeout=_RUN_ATTEMPT_TIMEOUT_SECONDS,
|
||||
)
|
||||
except TimeoutError:
|
||||
@@ -875,6 +897,342 @@ class ScheduledTaskService:
|
||||
await self._deliver_element_result(task, result["content"])
|
||||
return
|
||||
|
||||
async def _run_task_attempt(
|
||||
self,
|
||||
task: dict[str, Any],
|
||||
*,
|
||||
runtime_thread_id: str,
|
||||
scheduled_for: datetime,
|
||||
run_id: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Dispatch a single execution attempt by task kind.
|
||||
|
||||
``html_page`` tasks render an HTML document via GLM5; everything else
|
||||
runs the regular lead-agent attempt. The kind lives in
|
||||
``execution_context.task_kind`` so no schema migration is needed.
|
||||
"""
|
||||
ctx = task.get("execution_context")
|
||||
kind = ctx.get("task_kind") if isinstance(ctx, dict) else None
|
||||
if kind == "html_page":
|
||||
return await self._run_html_page_attempt(
|
||||
task, runtime_thread_id=runtime_thread_id, scheduled_for=scheduled_for, run_id=run_id
|
||||
)
|
||||
return await self._run_agent_attempt(
|
||||
task, runtime_thread_id=runtime_thread_id, scheduled_for=scheduled_for, run_id=run_id
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _html_config(task: dict[str, Any]) -> dict[str, Any]:
|
||||
ctx = task.get("execution_context")
|
||||
cfg = ctx.get("html_config") if isinstance(ctx, dict) else None
|
||||
return cfg if isinstance(cfg, dict) else {}
|
||||
|
||||
async def _invoke_chat_model(self, model_name: str | None, prompt: str) -> str:
|
||||
"""Invoke a chat model once and return its text content.
|
||||
|
||||
Used for the GLM5 HTML generation / repair / style-extraction passes.
|
||||
Thinking is disabled to keep structured-JSON output stable.
|
||||
"""
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
# No hardcoded model: use exactly the selected model; only when none was
|
||||
# selected do we fall back to the system default (config models[0]).
|
||||
model = create_chat_model(name=model_name or None, thinking_enabled=False)
|
||||
response = await model.ainvoke([HumanMessage(content=prompt)])
|
||||
content = getattr(response, "content", response)
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
if isinstance(content, list):
|
||||
parts = [part.get("text", "") if isinstance(part, dict) else str(part) for part in content]
|
||||
return "\n".join(p for p in parts if p)
|
||||
return str(content)
|
||||
|
||||
async def _gather_html_data(
|
||||
self,
|
||||
task: dict[str, Any],
|
||||
*,
|
||||
runtime_thread_id: str,
|
||||
scheduled_for: datetime,
|
||||
run_id: str,
|
||||
) -> str:
|
||||
"""Run the lead agent to collect source data for the HTML page.
|
||||
|
||||
Reuses the regular agent-run primitives (with the task's search query
|
||||
folded into the prompt) and returns the agent's final text, which feeds
|
||||
the GLM5 render step as ``本次数据``. Returns an empty string if the
|
||||
agent produced nothing.
|
||||
"""
|
||||
cfg = self._html_config(task)
|
||||
page_prompt = str(cfg.get("page_prompt") or "").strip()
|
||||
negative_prompt = str(cfg.get("negative_prompt") or "").strip()
|
||||
gather_instruction = (
|
||||
"请运用你自身的检索/搜索等技能,围绕下述页面需求获取最新、真实的资料,"
|
||||
"并整理成结构化的中文要点(可用列表/小标题),供后续生成 HTML 页面使用,本步骤不要生成 HTML:\n"
|
||||
)
|
||||
parts = [gather_instruction]
|
||||
if page_prompt:
|
||||
parts.append(f"页面需求:{page_prompt}")
|
||||
base_prompt = str(task.get("prompt") or "").strip()
|
||||
if base_prompt:
|
||||
parts.append(f"补充说明:{base_prompt}")
|
||||
# Apply the user's constraints to data gathering too, not just rendering
|
||||
# — a "限制条件" like "只要最近 24 小时的新闻" or "排除某主题" must shape
|
||||
# which sources/data the agent collects, otherwise it silently leaks in.
|
||||
if negative_prompt:
|
||||
parts.append(f"限制条件(最高优先级,必须严格遵守):{negative_prompt}")
|
||||
gather_task = {**task, "prompt": "\n".join(parts)}
|
||||
# Never apply the markdown gate to the gather pass.
|
||||
gather_ctx = dict(task.get("execution_context") or {})
|
||||
gather_ctx.pop("require_markdown", None)
|
||||
gather_ctx.pop("task_kind", None)
|
||||
gather_ctx.pop("html_config", None)
|
||||
# Use the task's selected model for the data-gathering agent too, so the
|
||||
# whole HTML pipeline honors ``html_config.model_name`` instead of
|
||||
# silently falling back to the default model for this step.
|
||||
gather_model = str(cfg.get("model_name") or "").strip()
|
||||
if gather_model:
|
||||
gather_ctx["model_name"] = gather_model
|
||||
gather_task["execution_context"] = gather_ctx
|
||||
|
||||
await self._ensure_thread_meta(task["user_id"], runtime_thread_id)
|
||||
async with self._agent_run_lock:
|
||||
record = await self._start_agent_run(gather_task, scheduled_for=scheduled_for, runtime_thread_id=runtime_thread_id)
|
||||
await self.store.update_run(run_id, {"agent_run_id": record.run_id})
|
||||
if record.task is not None:
|
||||
await record.task
|
||||
text = await self._extract_latest_ai_text(runtime_thread_id)
|
||||
return text or ""
|
||||
|
||||
def _collect_html_skill_instructions(self) -> str:
|
||||
"""Concatenate the bodies of enabled skills marked for HTML pages.
|
||||
|
||||
A skill opts in via ``metadata.kind: html_page`` in its SKILL.md
|
||||
frontmatter. Only enabled skills (per the skills settings the user
|
||||
toggles) are injected, so the render step honors the user's skill
|
||||
selection. Best-effort — any failure yields an empty string.
|
||||
"""
|
||||
try:
|
||||
from deerflow.skills import get_or_new_skill_storage
|
||||
|
||||
storage = get_or_new_skill_storage()
|
||||
skills = storage.load_skills(enabled_only=True)
|
||||
except Exception:
|
||||
logger.debug("Failed to load skills for HTML render injection", exc_info=True)
|
||||
return ""
|
||||
blocks: list[str] = []
|
||||
for skill in skills:
|
||||
try:
|
||||
text = Path(skill.skill_file).read_text(encoding="utf-8")
|
||||
except Exception:
|
||||
continue
|
||||
frontmatter, body = html_page_utils.parse_frontmatter(text)
|
||||
meta = frontmatter.get("metadata") if isinstance(frontmatter, dict) else None
|
||||
if not (isinstance(meta, dict) and str(meta.get("kind")) == "html_page"):
|
||||
continue
|
||||
body = body.strip()
|
||||
if body:
|
||||
blocks.append(f"### 技能:{skill.name}\n{body}")
|
||||
return "\n\n".join(blocks)
|
||||
|
||||
def _archive_html_index(self, run_id: str, html: str) -> Path | None:
|
||||
"""Write the generated HTML to the run's archive dir as ``index.html``."""
|
||||
try:
|
||||
archive_dir = get_paths().base_dir / "scheduled-task-runs" / run_id / "files"
|
||||
archive_dir.mkdir(parents=True, exist_ok=True)
|
||||
target = archive_dir / "index.html"
|
||||
target.write_text(html, encoding="utf-8")
|
||||
return target
|
||||
except Exception:
|
||||
logger.warning("Failed to archive generated HTML for run %s", run_id, exc_info=True)
|
||||
return None
|
||||
|
||||
async def _run_html_page_attempt(
|
||||
self,
|
||||
task: dict[str, Any],
|
||||
*,
|
||||
runtime_thread_id: str,
|
||||
scheduled_for: datetime,
|
||||
run_id: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Generate an HTML page for an ``html_page`` task.
|
||||
|
||||
Pipeline: gather data via the lead agent → optionally load a reference
|
||||
favorite's style/layout → GLM5 render → sanitize → validate → (one
|
||||
GLM5 repair pass if invalid) → best-effort screenshot → archive
|
||||
``index.html`` → persist a ``scheduled_html_pages`` row → build the run
|
||||
result payload (``format: "html"`` with the full HTML inline).
|
||||
"""
|
||||
cfg = self._html_config(task)
|
||||
# The user's selected model only — no hardcoded GLM-5. Empty → None →
|
||||
# system default (config models[0]).
|
||||
model_name = str(cfg.get("model_name") or "").strip() or None
|
||||
page_prompt = str(cfg.get("page_prompt") or "").strip()
|
||||
negative_prompt = str(cfg.get("negative_prompt") or "").strip()
|
||||
reference_favorite_id = cfg.get("reference_favorite_id") or None
|
||||
|
||||
# 1) Gather source data (real, via the agent's search tools).
|
||||
data_text = await self._gather_html_data(
|
||||
task, runtime_thread_id=runtime_thread_id, scheduled_for=scheduled_for, run_id=run_id
|
||||
)
|
||||
|
||||
# 2) Load reference style/layout from a favorite if one was selected.
|
||||
reference_style = None
|
||||
reference_layout = None
|
||||
if reference_favorite_id:
|
||||
store = self._html_store
|
||||
if store is not None:
|
||||
try:
|
||||
favorite = await store.get_favorite(str(reference_favorite_id))
|
||||
except Exception:
|
||||
favorite = None
|
||||
if favorite is not None:
|
||||
reference_style = favorite.get("reuse_prompt") or favorite.get("style_summary")
|
||||
reference_layout = favorite.get("layout_summary")
|
||||
|
||||
# 3) Render with the chosen model, injecting any enabled HTML skills.
|
||||
skill_instructions = self._collect_html_skill_instructions()
|
||||
gen_prompt = html_page_utils.build_generation_prompt(
|
||||
page_prompt=page_prompt,
|
||||
negative_prompt=negative_prompt,
|
||||
data=data_text,
|
||||
reference_style=reference_style,
|
||||
reference_layout=reference_layout,
|
||||
skill_instructions=skill_instructions,
|
||||
)
|
||||
raw = await self._invoke_chat_model(model_name, gen_prompt)
|
||||
parsed = html_page_utils.extract_json_object(raw) or {}
|
||||
title = str(parsed.get("title") or task.get("name") or "生成页面").strip()
|
||||
html = parsed.get("html")
|
||||
if not isinstance(html, str) or "<" not in html:
|
||||
# The JSON wrapper couldn't be parsed (the model emitted literal
|
||||
# newlines / unescaped quotes inside the "html" value), or it replied
|
||||
# with bare HTML. Either way, recover just the HTML document so we
|
||||
# never archive the {"title":...,"html":"..."} JSON blob as the page.
|
||||
recovered = html_page_utils.recover_html_document(raw)
|
||||
if recovered is None:
|
||||
raise ValueError("模型未返回有效的 HTML(JSON 解析失败)")
|
||||
html = recovered
|
||||
style_summary = str(parsed.get("style_summary") or "").strip() or None
|
||||
layout_summary = str(parsed.get("layout_summary") or "").strip() or None
|
||||
|
||||
# 4) Sanitize + validate, with one repair pass on failure.
|
||||
html, _removed = html_page_utils.sanitize_html(html)
|
||||
errors = html_page_utils.validate_html(html)
|
||||
if errors:
|
||||
logger.info("Generated HTML failed validation (%s); attempting repair", errors)
|
||||
repair_prompt = html_page_utils.build_repair_prompt(
|
||||
html=html, errors=errors, requirements=page_prompt or task.get("prompt")
|
||||
)
|
||||
try:
|
||||
repaired_raw = await self._invoke_chat_model(model_name, repair_prompt)
|
||||
repaired = html_page_utils.extract_json_object(repaired_raw) or {}
|
||||
fixed = repaired.get("fixed_html")
|
||||
if isinstance(fixed, str) and "<" in fixed:
|
||||
fixed_clean, _ = html_page_utils.sanitize_html(fixed)
|
||||
if not html_page_utils.validate_html(fixed_clean):
|
||||
html = fixed_clean
|
||||
except Exception:
|
||||
logger.warning("HTML repair pass failed; keeping sanitized original", exc_info=True)
|
||||
|
||||
# 4b) Completeness gate — a page only counts as successful when it is a
|
||||
# full, valid HTML document. If it still fails validation after the
|
||||
# repair pass, fail the run (no half-baked page is archived/persisted).
|
||||
final_errors = html_page_utils.validate_html(html)
|
||||
if final_errors:
|
||||
raise HtmlGenerationError(
|
||||
"HTML 生成未通过完整性校验:" + ";".join(final_errors) + "。请调整页面要求或重试。"
|
||||
)
|
||||
|
||||
# 5) Best-effort screenshot QA (no-op without Playwright).
|
||||
screenshot_path = None
|
||||
try:
|
||||
shot_target = get_paths().base_dir / "scheduled-task-runs" / run_id / "files" / "preview.png"
|
||||
shot_target.parent.mkdir(parents=True, exist_ok=True)
|
||||
screenshot_path = await html_page_utils.screenshot_html(html, shot_target)
|
||||
except Exception:
|
||||
screenshot_path = None
|
||||
|
||||
# 6) Archive index.html.
|
||||
archive_path = self._archive_html_index(run_id, html)
|
||||
|
||||
files: list[dict[str, Any]] = []
|
||||
if archive_path is not None:
|
||||
files.append(
|
||||
{
|
||||
"name": "index.html",
|
||||
"path": str(archive_path),
|
||||
"virtual_path": "/mnt/user-data/outputs/index.html",
|
||||
"mime_type": "text/html",
|
||||
"size": archive_path.stat().st_size if archive_path.exists() else None,
|
||||
"kind": "html",
|
||||
}
|
||||
)
|
||||
if screenshot_path:
|
||||
shot = Path(screenshot_path)
|
||||
files.append(
|
||||
{
|
||||
"name": "preview.png",
|
||||
"path": str(shot),
|
||||
"mime_type": "image/png",
|
||||
"size": shot.stat().st_size if shot.exists() else None,
|
||||
"kind": "artifact",
|
||||
}
|
||||
)
|
||||
|
||||
data_snapshot = {"data_text": data_text[:8000] if isinstance(data_text, str) else ""}
|
||||
prompt_snapshot = {
|
||||
"page_prompt": page_prompt,
|
||||
"negative_prompt": negative_prompt,
|
||||
"reference_favorite_id": reference_favorite_id,
|
||||
"model_name": model_name,
|
||||
}
|
||||
|
||||
# 7) Persist the page asset (best-effort — failure must not fail the run).
|
||||
store = self._html_store
|
||||
if store is not None:
|
||||
try:
|
||||
await store.create_page(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"task_id": task["task_id"],
|
||||
"run_id": run_id,
|
||||
"user_id": task["user_id"],
|
||||
"title": title,
|
||||
"html_content": html,
|
||||
"html_file_path": str(archive_path) if archive_path else None,
|
||||
"data_snapshot": data_snapshot,
|
||||
"prompt_snapshot": prompt_snapshot,
|
||||
"reference_favorite_id": reference_favorite_id,
|
||||
"style_summary": style_summary,
|
||||
"layout_summary": layout_summary,
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
logger.warning("Failed to persist scheduled_html_pages row for run %s", run_id, exc_info=True)
|
||||
|
||||
task_tz = ZoneInfo(task.get("timezone") or "UTC")
|
||||
local_time = scheduled_for.astimezone(task_tz)
|
||||
# ``scheduled_task_runs.result_json`` is TEXT (64 KB) on MySQL, so only
|
||||
# inline the HTML when it comfortably fits. The full document always
|
||||
# lives in ``scheduled_html_pages.html_content`` (LONGTEXT) + the
|
||||
# archived ``index.html``; the ``/runs/{id}/html`` endpoints read from
|
||||
# there, so previews work even when the inline copy is omitted here.
|
||||
inline_html = html if len(html) <= _MAX_INLINE_RESULT_HTML else None
|
||||
return {
|
||||
"title": f"{title} - {local_time.strftime('%Y-%m-%d %H:%M')}",
|
||||
"content": page_prompt or f"HTML 页面已生成:{title}",
|
||||
"format": "html",
|
||||
"html": inline_html,
|
||||
"style_summary": style_summary,
|
||||
"layout_summary": layout_summary,
|
||||
"data_snapshot": data_snapshot,
|
||||
"prompt_snapshot": prompt_snapshot,
|
||||
"files": files,
|
||||
"created_at": datetime.now(UTC).isoformat(),
|
||||
}
|
||||
|
||||
async def _run_agent_attempt(
|
||||
self,
|
||||
task: dict[str, Any],
|
||||
@@ -1275,3 +1633,132 @@ class ScheduledTaskService:
|
||||
|
||||
async def unsubscribe_task(self, user_id: str, task_id: str) -> bool:
|
||||
return await self.store.delete_subscription(task_id, user_id)
|
||||
|
||||
# --- HTML pages / favorites ---
|
||||
|
||||
@property
|
||||
def _html_store(self):
|
||||
state = getattr(self.app, "state", None)
|
||||
return getattr(state, "html_page_store", None)
|
||||
|
||||
async def get_html_page_for_run(self, run_id: str) -> dict[str, Any] | None:
|
||||
"""Return the generated HTML page for a run (page-store row or run result)."""
|
||||
store = self._html_store
|
||||
if store is not None:
|
||||
page = await store.get_page_by_run(run_id)
|
||||
if page is not None:
|
||||
return page
|
||||
# Fallback to the inline HTML on the run result.
|
||||
run = await self.store.get_run_any(run_id)
|
||||
if run is None:
|
||||
return None
|
||||
result = run.get("result") if isinstance(run.get("result"), dict) else None
|
||||
if not result or not isinstance(result.get("html"), str):
|
||||
return None
|
||||
return {
|
||||
"run_id": run_id,
|
||||
"task_id": run.get("task_id"),
|
||||
"title": result.get("title"),
|
||||
"html_content": result.get("html"),
|
||||
"style_summary": result.get("style_summary"),
|
||||
"layout_summary": result.get("layout_summary"),
|
||||
"created_at": result.get("created_at"),
|
||||
}
|
||||
|
||||
async def get_latest_html_page_for_task(self, task_id: str) -> dict[str, Any] | None:
|
||||
store = self._html_store
|
||||
if store is not None:
|
||||
page = await store.latest_page_for_task(task_id)
|
||||
if page is not None:
|
||||
return page
|
||||
# Fallback: newest run that carries inline HTML.
|
||||
runs = await self.store.list_runs_any(task_id, limit=20)
|
||||
for run in runs:
|
||||
result = run.get("result") if isinstance(run.get("result"), dict) else None
|
||||
if result and isinstance(result.get("html"), str):
|
||||
return await self.get_html_page_for_run(run["id"])
|
||||
return None
|
||||
|
||||
async def list_html_page_favorites(self, user_id: str) -> list[dict[str, Any]]:
|
||||
store = self._html_store
|
||||
if store is None:
|
||||
return []
|
||||
return await store.list_favorites(user_id)
|
||||
|
||||
async def get_html_page_favorite(self, user_id: str, favorite_id: str) -> dict[str, Any] | None:
|
||||
store = self._html_store
|
||||
if store is None:
|
||||
return None
|
||||
favorite = await store.get_favorite(favorite_id)
|
||||
if favorite is None or favorite.get("user_id") != user_id:
|
||||
return None
|
||||
return favorite
|
||||
|
||||
async def delete_html_page_favorite(self, user_id: str, favorite_id: str) -> bool:
|
||||
store = self._html_store
|
||||
if store is None:
|
||||
return False
|
||||
return await store.delete_favorite(favorite_id, user_id)
|
||||
|
||||
async def create_html_page_favorite(
|
||||
self,
|
||||
user_id: str,
|
||||
page_id: str,
|
||||
*,
|
||||
title: str | None = None,
|
||||
description: str | None = None,
|
||||
tags: list[str] | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Bookmark a generated page, extracting a reusable style summary via GLM5.
|
||||
|
||||
Returns ``None`` when the page does not exist or is not owned by the
|
||||
caller. Style extraction is best-effort — a failure still produces a
|
||||
favorite (without the reuse hints) so the user's bookmark is never lost.
|
||||
"""
|
||||
store = self._html_store
|
||||
if store is None:
|
||||
return None
|
||||
page = await store.get_page(page_id)
|
||||
if page is None or page.get("user_id") != user_id:
|
||||
return None
|
||||
# A page can only be bookmarked once per user.
|
||||
existing = await store.get_favorite_by_page(user_id, page_id)
|
||||
if existing is not None:
|
||||
raise DuplicateFavoriteError(page_id)
|
||||
html = page.get("html_content") or ""
|
||||
|
||||
style_summary = page.get("style_summary")
|
||||
layout_summary = page.get("layout_summary")
|
||||
content_structure_summary = None
|
||||
reuse_prompt = None
|
||||
if html:
|
||||
try:
|
||||
# Reuse the model the page was generated with (recorded in the
|
||||
# page's prompt snapshot) so favoriting doesn't fall back to the
|
||||
# default model.
|
||||
extract_model = (page.get("prompt_snapshot") or {}).get("model_name")
|
||||
extract_prompt = html_page_utils.build_style_extract_prompt(html=html)
|
||||
raw = await self._invoke_chat_model(extract_model, extract_prompt)
|
||||
extracted = html_page_utils.extract_json_object(raw) or {}
|
||||
style_summary = extracted.get("style_summary") or style_summary
|
||||
layout_summary = extracted.get("layout_summary") or layout_summary
|
||||
content_structure_summary = extracted.get("content_structure_summary")
|
||||
reuse_prompt = extracted.get("reuse_prompt")
|
||||
except Exception:
|
||||
logger.warning("HTML style extraction failed for page %s; saving favorite without hints", page_id, exc_info=True)
|
||||
|
||||
return await store.create_favorite(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"user_id": user_id,
|
||||
"page_id": page_id,
|
||||
"title": (title or page.get("title") or "收藏页面").strip(),
|
||||
"description": (description or None),
|
||||
"tags": tags or [],
|
||||
"html_snapshot": html,
|
||||
"style_summary": style_summary,
|
||||
"layout_summary": layout_summary,
|
||||
"content_structure_summary": content_structure_summary,
|
||||
"reuse_prompt": reuse_prompt,
|
||||
}
|
||||
)
|
||||
Reference in new issue
Block a user