|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from copy import deepcopy |
4 | | - |
5 | | -from app.services.workflow_datasets import compact_value_for_transport, compact_workflow_result |
6 | | - |
7 | | - |
8 | | -_SUMMARY_ARTIFACT_REFERENCE_KEYS = ( |
9 | | - "dashboard_url", |
10 | | - "report_path", |
11 | | - "video_url", |
12 | | - "video_path", |
13 | | - "task_id", |
14 | | - "output_path", |
15 | | -) |
16 | | - |
17 | | - |
18 | | -def serialize_workspace_state(snapshot: dict) -> dict: |
19 | | - turn = snapshot.get("turn") |
20 | | - draft = snapshot.get("draft") |
21 | | - run = snapshot.get("run") |
22 | | - artifacts = snapshot.get("artifacts") or [] |
23 | | - return { |
24 | | - "session_id": str(snapshot.get("session_id")) if snapshot.get("session_id") is not None else None, |
25 | | - "turn": ( |
26 | | - { |
27 | | - "id": str(turn.id), |
28 | | - "status": turn.status, |
29 | | - "input_text": turn.input_text, |
30 | | - "error": turn.error, |
31 | | - } |
32 | | - if turn |
33 | | - else None |
34 | | - ), |
35 | | - "draft": ( |
36 | | - { |
37 | | - "id": str(draft.id), |
38 | | - "status": draft.status, |
39 | | - "version": draft.version, |
40 | | - "source": draft.source, |
41 | | - } |
42 | | - if draft |
43 | | - else None |
44 | | - ), |
45 | | - "run": ( |
46 | | - { |
47 | | - "id": str(run.id), |
48 | | - "status": run.status, |
49 | | - "error": run.error, |
50 | | - "result": compact_workflow_result(run.result, row_limit=10, text_limit=2500), |
51 | | - } |
52 | | - if run |
53 | | - else None |
54 | | - ), |
55 | | - "artifacts": [ |
56 | | - { |
57 | | - "id": str(artifact.id), |
58 | | - "kind": artifact.kind, |
59 | | - "payload": compact_value_for_transport(artifact.payload, row_limit=10, text_limit=2500), |
60 | | - } |
61 | | - for artifact in artifacts |
62 | | - ], |
63 | | - } |
64 | | - |
65 | | - |
66 | | -def dedupe_summary_artifact_references(workspace_state: dict | None) -> dict: |
67 | | - if not isinstance(workspace_state, dict): |
68 | | - return {} |
69 | | - |
70 | | - deduped = deepcopy(workspace_state) |
71 | | - artifacts = deduped.get("artifacts") |
72 | | - if not isinstance(artifacts, list): |
73 | | - return deduped |
74 | | - |
75 | | - seen_references: dict[str, set[str]] = {} |
76 | | - for artifact in artifacts: |
77 | | - if not isinstance(artifact, dict): |
78 | | - continue |
79 | | - payload = artifact.get("payload") |
80 | | - if not isinstance(payload, dict): |
81 | | - continue |
82 | | - for key in _SUMMARY_ARTIFACT_REFERENCE_KEYS: |
83 | | - value = payload.get(key) |
84 | | - if isinstance(value, str) and value.strip(): |
85 | | - seen_references.setdefault(key, set()).add(value.strip()) |
86 | | - |
87 | | - run = deduped.get("run") |
88 | | - if not isinstance(run, dict): |
89 | | - return deduped |
90 | | - result = run.get("result") |
91 | | - if not isinstance(result, dict): |
92 | | - return deduped |
93 | | - outputs = result.get("outputs") |
94 | | - if not isinstance(outputs, dict): |
95 | | - return deduped |
96 | | - |
97 | | - for node_output in outputs.values(): |
98 | | - if not isinstance(node_output, dict): |
99 | | - continue |
100 | | - for key, values in seen_references.items(): |
101 | | - value = node_output.get(key) |
102 | | - if isinstance(value, str) and value.strip() in values: |
103 | | - node_output.pop(key, None) |
104 | | - |
105 | | - return deduped |
106 | | - |
107 | | - |
108 | | -def extract_final_answer(workspace_state: dict | None) -> str | None: |
109 | | - if not isinstance(workspace_state, dict): |
110 | | - return None |
111 | | - run = workspace_state.get("run") |
112 | | - if not isinstance(run, dict): |
113 | | - return None |
114 | | - result = run.get("result") |
115 | | - if not isinstance(result, dict): |
116 | | - return None |
117 | | - outputs = result.get("outputs") |
118 | | - if not isinstance(outputs, dict): |
119 | | - return None |
120 | | - for node_output in outputs.values(): |
121 | | - if not isinstance(node_output, dict): |
122 | | - continue |
123 | | - answer = node_output.get("answer") |
124 | | - if isinstance(answer, str) and answer.strip(): |
125 | | - return answer.strip() |
126 | | - return None |
| 3 | +from app.workflow.services.workspace_state import * # noqa: F403 |
0 commit comments