@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
py 139 lines 4.3 KB
Raw
1 """Unattended CI checks, restricted to the read-only tools from approvals.py."""
2
3 import re
4
5 from approvals import (
6 ARGUMENTS,
7 MULTI_ARGUMENTS,
8 TOOL,
9 create_approval_session,
10 create_composio,
11 run_with_approvals,
12 )
13 from pydantic import BaseModel, TypeAdapter
14
15
16 class ToolResponse(BaseModel):
17 successful: bool
18 error: str | None = None
19
20
21 class BatchItem(BaseModel):
22 error: str | None = None
23 response: ToolResponse | None = None
24
25
26 class Batch(BaseModel):
27 results: list[BatchItem]
28 total_count: int
29 success_count: int
30 error_count: int
31
32
33 class BatchWrapper(BaseModel):
34 data: Batch
35
36
37 def assert_batch(data: dict[str, object], successes: int, failures: int) -> None:
38 parsed = TypeAdapter(Batch | BatchWrapper).validate_python(data)
39 batch = parsed.data if isinstance(parsed, BatchWrapper) else parsed
40 assert batch.total_count == successes + failures
41 assert len(batch.results) == successes + failures
42 assert batch.success_count == successes
43 assert batch.error_count == failures
44 denied = 0
45 for item in batch.results:
46 error = item.error or (item.response.error if item.response else None)
47 if error:
48 assert re.search(r"approv|deni|declin|cancel|reject", error, re.I)
49 denied += 1
50 else:
51 assert item.response is not None and item.response.successful
52 assert denied == failures
53
54
55 def main():
56 composio = create_composio()
57 cases = [
58 ("approve", {"action": "accept", "content": {"decision": "approve"}}, True),
59 ("deny", {"action": "accept", "content": {"decision": "deny"}}, False),
60 ("decline", {"action": "decline"}, False),
61 ("cancel", {"action": "cancel"}, False),
62 (
63 "always_approve",
64 {"action": "accept", "content": {"decision": "always_approve"}},
65 True,
66 ),
67 ]
68 for label, answer, allowed in cases:
69 session = create_approval_session(composio)
70 try:
71 prompts = 0
72
73 def ask_user(question):
74 nonlocal prompts
75 prompts += 1
76 assert prompts <= 2, "Approval did not resolve"
77 return answer
78
79 result = run_with_approvals(
80 lambda **answers: session.execute(TOOL, arguments=ARGUMENTS, **answers),
81 ask_user,
82 )
83 assert prompts > 0, "Execution bypassed approval"
84 assert result.result_type == ("completed" if allowed else "failed")
85 if allowed:
86 assert result.error is None
87 assert result.data
88 else:
89 assert re.search(
90 r"approv|deni|declin|cancel|reject", str(result.error), re.I
91 )
92 if label == "always_approve":
93
94 def must_not_ask(question):
95 raise AssertionError(
96 "Always approve asked again in the same session"
97 )
98
99 next_result = run_with_approvals(
100 lambda **answers: session.execute(
101 TOOL, arguments={"username": "dang"}, **answers
102 ),
103 must_not_ask,
104 )
105 assert next_result.result_type == "completed"
106 assert next_result.error is None
107 assert next_result.data
108 print(f"PASS {label}")
109 finally:
110 composio.sessions.delete(session.session_id)
111
112 session = create_approval_session(composio)
113 try:
114 prompts = 0
115
116 def ask_batch(question):
117 nonlocal prompts
118 prompts += 1
119 assert prompts <= 2, "Batch approval did not resolve"
120 return {
121 "action": "accept",
122 "content": {"decision": "approve" if prompts == 1 else "deny"},
123 }
124
125 result = run_with_approvals(
126 lambda **answers: session.execute_meta(
127 "COMPOSIO_MULTI_EXECUTE_TOOL", arguments=MULTI_ARGUMENTS, **answers
128 ),
129 ask_batch,
130 )
131 assert prompts == 2, "Expected a question for each tool call"
132 assert_batch(result.data, 1, 1)
133 print("PASS batch with one approval and one denial")
134 finally:
135 composio.sessions.delete(session.session_id)
136
137
138 if __name__ == "__main__":
139 main()