mirror of
https://github.com/langgenius/dify.git
synced 2026-05-07 02:46:32 +08:00
Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
48 lines
2.0 KiB
Python
48 lines
2.0 KiB
Python
"""
|
|
Parser for tool nodes that captures tool-specific metadata.
|
|
"""
|
|
|
|
from graphon.enums import WorkflowNodeExecutionMetadataKey
|
|
from graphon.graph_events import GraphNodeEventBase
|
|
from graphon.nodes.base.node import Node
|
|
from graphon.nodes.tool.entities import ToolNodeData
|
|
from opentelemetry.trace import Span
|
|
|
|
from extensions.otel.parser.base import DefaultNodeOTelParser, safe_json_dumps
|
|
from extensions.otel.semconv.gen_ai import ToolAttributes
|
|
|
|
|
|
class ToolNodeOTelParser:
|
|
"""Parser for tool nodes that captures tool-specific metadata."""
|
|
|
|
def __init__(self) -> None:
|
|
self._delegate = DefaultNodeOTelParser()
|
|
|
|
def parse(
|
|
self, *, node: Node, span: "Span", error: Exception | None, result_event: GraphNodeEventBase | None = None
|
|
) -> None:
|
|
self._delegate.parse(node=node, span=span, error=error, result_event=result_event)
|
|
|
|
tool_data = getattr(node, "_node_data", None)
|
|
if not isinstance(tool_data, ToolNodeData):
|
|
return
|
|
|
|
span.set_attribute(ToolAttributes.TOOL_NAME, node.title)
|
|
span.set_attribute(ToolAttributes.TOOL_TYPE, tool_data.provider_type.value)
|
|
|
|
# Extract tool info from metadata (consistent with aliyun_trace)
|
|
tool_info = {}
|
|
if result_event and result_event.node_run_result:
|
|
node_run_result = result_event.node_run_result
|
|
if node_run_result.metadata:
|
|
tool_info = node_run_result.metadata.get(WorkflowNodeExecutionMetadataKey.TOOL_INFO, {})
|
|
|
|
if tool_info:
|
|
span.set_attribute(ToolAttributes.TOOL_DESCRIPTION, safe_json_dumps(tool_info))
|
|
|
|
if result_event and result_event.node_run_result and result_event.node_run_result.inputs:
|
|
span.set_attribute(ToolAttributes.TOOL_CALL_ARGUMENTS, safe_json_dumps(result_event.node_run_result.inputs))
|
|
|
|
if result_event and result_event.node_run_result and result_event.node_run_result.outputs:
|
|
span.set_attribute(ToolAttributes.TOOL_CALL_RESULT, safe_json_dumps(result_event.node_run_result.outputs))
|