@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 86 lines 3.33 KB
Raw
1 """
2 Tool Router - Session Update Example
3
4 Demonstrates session.update(): what omitted fields, replaced maps, None,
5 an empty toolkit allowlist, and expected_config_version do to a stored
6 session, without creating a new one.
7 """
8
9 import os
10
11 from composio import Composio
12 from composio.exceptions import SessionConfigConflictError
13
14 composio = Composio()
15
16 # Create a session with a per-toolkit tools map and a stored callback URL
17 session = composio.create(
18 # The provisioned examples user (raises KeyError when unset)
19 user_id=os.environ["COMPOSIO_EXAMPLES_USER_ID"],
20 toolkits=["gmail", "slack"],
21 tools={
22 "gmail": {"enable": ["GMAIL_FETCH_EMAILS"]},
23 "slack": {"enable": ["SLACK_SEND_MESSAGE"]},
24 },
25 manage_connections={"enable": True, "callback_url": "https://example.com/callback"},
26 )
27
28 print(f"Session created: {session.session_id}")
29 print(f"Config version: {session.config_version}")
30 print(f"Tools: {session.config.tools}")
31
32 # 1. Maps replace, they don't merge: a slack-only tools map removes the gmail entry
33 session.update(
34 tools={"slack": {"enable": ["SLACK_SEND_MESSAGE", "SLACK_LIST_CHANNELS"]}},
35 )
36
37 print("\nAfter the slack-only tools map (gmail entry is gone):")
38 print(f"Tools: {session.config.tools}")
39
40 # 2. Remove only the stored callback URL; `enable` and the other settings stay as stored
41 session.update(manage_connections={"callback_url": None})
42
43 print("\nAfter clearing the callback URL:")
44 print(f"Manage connections: {session.config.manage_connections}")
45
46 # 3. Narrow the policy and fix preload in the same update. A preloaded tool that
47 # falls outside the new policy would be rejected with a 400.
48 session.update(
49 toolkits={"enable": ["slack"]},
50 preload={"tools": ["SLACK_SEND_MESSAGE"]},
51 )
52
53 print("\nAfter narrowing to slack:")
54 print(f"Config version: {session.config_version}")
55 print(f"Preload: {session.preload}")
56
57 # 4. Concurrent updates. Every update sends the config version this handle last
58 # observed as its precondition; a stale value is a 409 and nothing is written.
59 # A second handle on the same session plays the concurrent writer.
60 observed_version = session.config_version
61 other_client = composio.sessions.use(session.session_id)
62 other_client.update(workbench={"enable": False})
63
64 try:
65 session.update(toolkits={"enable": ["gmail", "slack"]}, preload={"tools": []})
66 except SessionConfigConflictError as error:
67 print(f"\nConflict at version {observed_version}: {error}")
68 # Recover: re-read the session, then retry against the fresh version
69 session = composio.sessions.use(session.session_id)
70 session.update(toolkits={"enable": ["gmail", "slack"]}, preload={"tools": []})
71 print(f"Retried at version {session.config_version}")
72
73 # 5. Deny every app toolkit. The empty allowlist is sent as-is; an omitted
74 # `toolkits` would have left the policy unchanged instead.
75 session.update(toolkits={"enable": []}, preload={"tools": []})
76
77 print("\nAfter denying every app toolkit:")
78 print(f"Toolkits: {session.config.toolkits}")
79
80 # 6. Clear a whole block with None: manage_connections falls back to its default.
81 # expected_config_version=False skips the precondition, so this write applies
82 # whatever version the session is at (last writer wins).
83 session.update(manage_connections=None, expected_config_version=False)
84
85 print("\nAfter clearing manage_connections:")
86 print(f"Config version: {session.config_version}")