@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, 04:45 UTC

next
ts 106 lines 4.08 KB
Raw
1 /**
2 * The person the agent works for.
3 *
4 * A login or an account authorization needs them: only the owner of an account
5 * can approve access to it, in a browser. So a journey has to model what
6 * reaches them, or an agent that never showed them the URL would pass.
7 *
8 * Two things reach this person:
9 *
10 * - a page the CLI opens in their browser, and
11 * - what the agent writes to them.
12 *
13 * They do not read the output of the agent's commands. The CLI assumes the
14 * same: off a terminal `composio login` prints "For agents: Show the URL above
15 * to the user".
16 *
17 * They approve an authorization when the page for it reaches them: the CLI
18 * opened it, or the agent wrote its complete URL. An id on its own, or the
19 * path under some other host, is not a page they can open. A URL written
20 * without its scheme is not recognised either; that is a limit of this model,
21 * not a claim about people.
22 *
23 * When the agent has stopped to wait, they say that they approved, once per
24 * approval, and only while the CLI has not been told: an agent whose own
25 * command already saw the login or the connection has nothing to wait for.
26 * "Told" is the fake having answered a request with the approved state;
27 * whether the answer reached the agent is not modelled.
28 */
29
30 import type { World } from '@e2e-tests/cli-fake-backend';
31
32 export const PERSON_REPLY = 'Done, I approved it in my browser. Please continue.';
33
34 /** Where the CLI looks for a browser on Linux. */
35 export const BROWSER_PATH = '/usr/local/bin/xdg-open';
36
37 /**
38 * The person's browser. It loads the page it is asked to open, and on the fake
39 * backend loading an authorization page approves it. A page behind TLS is left
40 * alone: bash has no TLS, and the CLI opens no such page off a terminal.
41 */
42 export const BROWSER_SCRIPT = `#!/bin/bash
43 url="$1"
44 case "$url" in http://*) ;; *) exit 0 ;; esac
45 rest="\${url#http://}"
46 authority="\${rest%%/*}"
47 exec 3<>"/dev/tcp/\${authority%%:*}/\${authority##*:}" || exit 0
48 printf 'GET /%s HTTP/1.0\\r\\nHost: %s\\r\\n\\r\\n' "\${rest#*/}" "$authority" >&3
49 cat <&3 >/dev/null
50 `;
51
52 export interface Person {
53 /**
54 * Hands the person a message from the agent. `waiting` is true when the
55 * agent has stopped and expects them to act; their reply, if they have one,
56 * is returned.
57 */
58 hear(message: string, waiting: boolean): string | undefined;
59 }
60
61 /** The origins of the fake, as the agent sees them in command output. */
62 export interface PersonUrls {
63 /** Where the login page lives: `<dashboard>?cliKey=<login session id>`. */
64 readonly dashboard: string;
65 /** Where an authorization page lives: `<backend>/api/v3/s/<connection id>`. */
66 readonly backend: string;
67 }
68
69 const AUTHORIZATION_PATH = /^\/api\/v\d+(?:\.\d+)?\/s\/([^/]+)$/;
70
71 /** The complete URLs in a message, without the punctuation or markup around them. */
72 const urlsIn = (message: string): URL[] =>
73 (message.match(/https?:\/\/[^\s<>"'`)\]]+/g) ?? []).flatMap(candidate => {
74 const text = candidate.replace(/[.,;:!?*_]+$/, '');
75 return URL.canParse(text) ? [new URL(text)] : [];
76 });
77
78 export const personFor = (world: World, urls: PersonUrls): Person => {
79 const dashboard = new URL(urls.dashboard).origin;
80 const backend = new URL(urls.backend).origin;
81 const acknowledged = new Set<string>();
82
83 return {
84 hear: (message, waiting) => {
85 for (const url of urlsIn(message)) {
86 const loginId =
87 url.origin === dashboard && url.pathname === '/' ? url.searchParams.get('cliKey') : null;
88 if (
89 loginId !== null &&
90 world.loginSessions.some(
91 session => session.id === loginId && session.status === 'pending'
92 )
93 ) {
94 world.linkLoginSession(loginId);
95 }
96 const connectionId =
97 url.origin === backend ? AUTHORIZATION_PATH.exec(url.pathname)?.[1] : undefined;
98 if (connectionId !== undefined) world.authorizeConnection(connectionId);
99 }
100 if (!waiting) return undefined;
101 const news = world.unreportedApprovals().filter(id => !acknowledged.has(id));
102 for (const id of news) acknowledged.add(id);
103 return news.length > 0 ? PERSON_REPLY : undefined;
104 },
105 };
106 };