This repository was archived by the owner on Jul 17, 2026. It is now read-only.
forked from deepset-ai/haystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataclasses.py
More file actions
122 lines (101 loc) · 4.26 KB
/
Copy pathdataclasses.py
File metadata and controls
122 lines (101 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
from dataclasses import asdict, dataclass
from typing import Any, Literal
DecisionStatus = Literal["approved", "modified", "rejected"]
@dataclass
class ConfirmationUIResult:
"""
Result of the confirmation UI interaction.
:param action:
The action taken by the user such as "confirm", "reject", or "modify".
This action type is not enforced to allow for custom actions to be implemented.
:param feedback:
Optional feedback message from the user. For example, if the user rejects the tool execution,
they might provide a reason for the rejection.
:param new_tool_params:
Optional set of new parameters for the tool. For example, if the user chooses to modify the tool parameters,
they can provide a new set of parameters here.
:param status:
Optional explicit decision status. When omitted, built-in actions are normalized as:
`confirm` -> `approved`, `modify` -> `modified`, `reject` -> `rejected`.
"""
action: str # "confirm", "reject", "modify"
feedback: str | None = None
new_tool_params: dict[str, Any] | None = None
status: DecisionStatus | None = None
def resolved_status(self) -> DecisionStatus:
"""
Resolve the explicit decision status for this UI result.
:raises ValueError:
If neither a supported built-in action nor an explicit status is provided.
"""
if self.status is not None:
return self.status
if self.action == "confirm":
return "approved"
if self.action == "modify":
return "modified"
if self.action == "reject":
return "rejected"
raise ValueError(
"Unsupported confirmation action. Provide one of 'confirm', 'modify', 'reject' or set 'status' explicitly."
)
@dataclass
class ToolExecutionDecision:
"""
Decision made regarding tool execution.
:param tool_name:
The name of the tool to be executed.
:param execute:
A boolean indicating whether to execute the tool with the provided parameters.
:param tool_call_id:
Optional unique identifier for the tool call. This can be used to track and correlate the decision with a
specific tool invocation.
:param feedback:
Optional feedback message.
For example, if the tool execution is rejected, this can contain the reason. Or if the tool parameters were
modified, this can contain the modification details.
:param final_tool_params:
Optional final parameters for the tool if execution is confirmed or modified.
:param status:
Explicit decision status. When omitted, it is inferred from the legacy `execute`/`feedback` shape to remain
backward-compatible with older serialized payloads.
"""
tool_name: str
execute: bool
tool_call_id: str | None = None
feedback: str | None = None
final_tool_params: dict[str, Any] | None = None
status: DecisionStatus | None = None
def __post_init__(self) -> None:
if self.status is None:
self.status = self._infer_status()
else:
self.execute = self.status != "rejected"
def _infer_status(self) -> DecisionStatus:
if not self.execute:
return "rejected"
if self.feedback is not None and self.final_tool_params is not None:
return "modified"
return "approved"
def resolved_status(self) -> DecisionStatus:
"""
Return the normalized decision status for this execution decision.
"""
return self.status
def to_dict(self) -> dict[str, Any]:
"""
Convert the ToolExecutionDecision to a dictionary representation.
:return: A dictionary containing the tool execution decision details.
"""
return asdict(self)
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ToolExecutionDecision":
"""
Populate the ToolExecutionDecision from a dictionary representation.
:param data: A dictionary containing the tool execution decision details.
:return: An instance of ToolExecutionDecision.
"""
return cls(**data)