feat: agent v2 node add llm usage output

This commit is contained in:
fatelei 2026-08-13 20:59:18 +08:00
parent c1bde25bcc
commit 021d0a17ec
No known key found for this signature in database
GPG Key ID: 2F91DA05646F4EED
4 changed files with 30 additions and 7 deletions

View File

@ -69,15 +69,18 @@ class WorkflowAgentOutputAdapter:
usage = self._usage_from_metadata(metadata)
metadata_tenant_id = metadata.get("tenant_id")
resolved_tenant_id = tenant_id or (metadata_tenant_id if isinstance(metadata_tenant_id, str) else None)
outputs = self._normalize_outputs(
event.output,
declared_outputs=declared_outputs,
tenant_id=resolved_tenant_id,
)
if event.usage is not None:
outputs["usage"] = dict(event.usage)
return NodeRunResult(
status=WorkflowNodeExecutionStatus.SUCCEEDED,
inputs=inputs,
process_data=process_data,
outputs=self._normalize_outputs(
event.output,
declared_outputs=declared_outputs,
tenant_id=resolved_tenant_id,
),
outputs=outputs,
metadata=self._build_node_metadata(metadata=metadata, usage=usage),
llm_usage=usage or LLMUsage.empty_usage(),
)

View File

@ -584,6 +584,20 @@ def test_success_output_adapter_maps_backend_usage_to_llm_usage_and_metadata():
assert result.llm_usage.completion_price == Decimal("0.000150")
assert result.llm_usage.total_price == Decimal("0.000200")
assert result.llm_usage.currency == "USD"
assert result.outputs["usage"] == {
"prompt_tokens": 10,
"prompt_unit_price": "5",
"prompt_price_unit": "0.000001",
"prompt_price": "0.000050",
"completion_tokens": 5,
"completion_unit_price": "30",
"completion_price_unit": "0.000001",
"completion_price": "0.000150",
"total_tokens": 15,
"total_price": "0.000200",
"currency": "USD",
"latency": 0.5,
}
assert result.metadata[WorkflowNodeExecutionMetadataKey.TOTAL_TOKENS] == 15
assert result.metadata[WorkflowNodeExecutionMetadataKey.TOTAL_PRICE] == Decimal("0.000200")
assert result.metadata[WorkflowNodeExecutionMetadataKey.CURRENCY] == "USD"

View File

@ -446,12 +446,12 @@ describe('agent/panel', () => {
).not.toBeInTheDocument()
expect(screen.getByText('text')).toBeInTheDocument()
expect(screen.getByText('workflow.nodes.agent.outputVars.text')).toBeInTheDocument()
expect(screen.queryByText('usage')).not.toBeInTheDocument()
expect(screen.getByText('usage')).toBeInTheDocument()
expect(screen.getByText('files')).toBeInTheDocument()
expect(screen.getByText('array[file]')).toBeInTheDocument()
expect(screen.getByText('workflow.nodes.agent.outputVars.files.title')).toBeInTheDocument()
expect(screen.getByText('json')).toBeInTheDocument()
expect(screen.getByText('object')).toBeInTheDocument()
expect(screen.getAllByText('object')).toHaveLength(2)
expect(
screen.getByRole('button', { name: 'workflow.nodes.agent.outputVars.newOutput' }),
).toBeInTheDocument()

View File

@ -19,6 +19,12 @@ export const defaultAgentV2DeclaredOutputs: DeclaredOutputConfig[] = [
type: 'file',
},
},
{
name: 'usage',
type: 'object',
required: false,
description: 'LLM usage reported by the agent run.',
},
{
name: 'json',
type: 'object',