Repository navigation
feat: Directly reply to add chart type #8307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @file: echarts_aggregator.py | ||
| @date:2026/12/4 | ||
| @desc: EchartsContent 聚合器 | ||
| """ | ||
|
|
||
| from application.workflow.message.aggregator.content_aggregator import ContentAggregator | ||
| from application.workflow.message.struct.echarts_content import EchartsContent | ||
|
|
||
|
|
||
| class EchartsAggregator(ContentAggregator[EchartsContent]): | ||
| """ | ||
| 图表内容聚合器 | ||
| 与 <echarts_rander> 标签内容一致,按顺序拼接内容块 | ||
| """ | ||
|
|
||
| def aggregate(self, prev: EchartsContent, chunk: EchartsContent) -> EchartsContent: | ||
| if prev is None: | ||
| return chunk | ||
| return EchartsContent( | ||
| chunk.id or prev.id, | ||
| (prev.content or "") + (chunk.content or ""), | ||
| chunk.status or prev.status, | ||
| chunk.node_info or prev.node_info, | ||
| chunk.position or prev.position, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @file: echarts_content.py | ||
| @date:2026/12/4 | ||
| @desc: ECharts 图表内容,content 即 <echarts_rander> 标签内的内容,前端用 ECharts 渲染 | ||
| """ | ||
|
|
||
| from application.workflow.content_type import ContentType | ||
| from application.workflow.message.struct.content import Content, NodeInfo, Position | ||
| from application.workflow.status import Status | ||
|
|
||
|
|
||
| class EchartsContent(Content): | ||
| def __init__(self, _id, content: str, status: Status, node_info: NodeInfo, position: Position, **kwargs): | ||
| self.content = content | ||
| super().__init__(_id, status, ContentType.ECHARTS, node_info, position, **kwargs) | ||
|
|
||
| def to_dict(self): | ||
| result = super().to_dict() | ||
| result["content"] = self.content | ||
| return result | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's my review: Potential Issues
Style/Optimization Suggestions
Summary: The main actionable items are the init-order concern and the lack of validation/documentation around |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: echarts_reply.py | ||
| @date:2026/12/4 | ||
| @desc: ECharts 图表回复,与 <echarts_rander> 标签渲染一致:内容即标签内的内容,前端用 ECharts 渲染 | ||
| """ | ||
|
|
||
| import json | ||
| from typing import List | ||
|
|
||
| import uuid_utils.compat as uuid | ||
|
|
||
| from application.workflow.message.struct.content import Content, NodeInfo, Position | ||
| from application.workflow.message.struct.echarts_content import EchartsContent | ||
| from application.workflow.nodes.reply_node.handle.reply_base_handle import ReplyBaseHandle, to_text | ||
| from application.workflow.status import Status | ||
|
|
||
|
|
||
| class EchartsReply(ReplyBaseHandle): | ||
| def support(self, content_type: str) -> bool: | ||
| return content_type == "ECHARTS" | ||
|
|
||
| def get_answer(self, node, value): | ||
| # 文本原样输出;引用的对象变量序列化为 JSON,与标签内容格式一致 | ||
| if isinstance(value, (dict, list)): | ||
| return json.dumps(value, ensure_ascii=False) | ||
| return to_text(value) | ||
|
|
||
| def to_content_list(self, answer, node_info: NodeInfo, position: Position) -> List[Content]: | ||
| return [EchartsContent(str(uuid.uuid7()), answer, Status.SUCCESS, node_info, position)] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review of
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review:
EchartsAggregatorPotential Issues
1.
chunk.id or prev.id— ID overwrite semanticsThe aggregation picks
chunk.idwhenever it's truthy, which means the last chunk's id wins. IfContentAggregatordownstream logic relies on stable identity (e.g., deduplication, keying), this may cause instability across chunks. Ifidshould remain constant for the whole aggregated message, useprev.id or chunk.id. Confirm intent.Suggestion: Make the precedence explicit and document it:
2. Positional constructor arguments are fragile
EchartsContent(...)is called positionally, relying on field order of a dataclass/namedtuple. Any future reordering of fields silently breaks aggregation. Use keyword arguments.3. Fields are dropped / not handled
Only
id,content,status,node_info,positionare aggregated. IfEchartsContenthas other fields (timestamps, metadata, error info,done/is_endflags), they are silently discarded fromchunk. Verify all relevant fields are carried over.4.
prev is Nonebranch returnschunkby referenceReturning the incoming
chunkdirectly means the caller receives an object aliased with the input. If either side mutates later, state can leak. IfEchartsContentis immutable (frozen dataclass / namedtuple) this is fine; otherwise consider copying.5.
status or prev.statusprecedenceLike
id, later status wins. That's usually correct for streaming, but make sure a partial status ("running") doesn't override a terminal one ("success"). Consider explicit status-merge logic if status is an enum with severity/terminal states (e.g., never downgradeDONE→RUNNING).6. String concatenation in a loop — O(n²) risk
If
aggregateis invoked once per chunk in a stream,(prev.content or "") + (chunk.content or "")rebuilds the full buffer each time → quadratic behavior on large outputs. For typical chat payloads it's fine, but for large charts/long streams, accumulate into a list/buffer and join at the end.Optimization: store chunks in a list and join lazily, or use
''.joinin a finalizer step.7. Docstring mentions
<echarts_rander>— typo"rander" is likely "render". Also the docstring claims "按顺序拼接内容块" (concatenate content blocks in order), which matches
contenthandling — good, but ensure tags/separators are not required between chunks (e.g., a chunk split mid-</echarts_render>fully qualified tag boundary). Mid-tag splits just work with plain concatenation only if the tag stream itself is being reassembled here — confirm the renderer tolerates arbitrary split points.8. Missing type hints robustness
prev: EchartsContentyet the code checksprev is None. The signature should beprev: EchartsContent | None = None(orOptional) to match reality, unless the ABC contract forbidsNone.9. No regressions tests visible
No unit tests in the diff. Add coverage for: first-chunk (
prev=None), empty content, mixedNonestatus/node_info, and multi-chunk concatenation.10. Encoding/coding declaration
# coding=utf-8is redundant in Python 3 (UTF-8 is the default). Harmless, but could be removed for consistency with PEP 3120.Minor / Style
application.workflow.message.aggregator...beforestruct...is fine, but check project isort config.@file:uses a full-width colon (:); cosmetic inconsistency with@project:/@date:.Summary of Recommended Changes
Plus: confirm field completeness, add tests, and consider buffered concatenation for large streams.