@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 88 lines 3.13 KB
Raw
1 import type { ToolRetrieveResponse } from '@composio/client/resources/tools';
2 import { Effect, Option } from 'effect';
3 import { commandInputError } from 'src/commands/errors';
4 import { handleClientApiError } from 'src/effects/handle-http-error';
5 import { commandHintStep } from 'src/services/command-hints';
6 import { ComposioToolkitsRepository } from 'src/services/composio-clients';
7 import { CLIENT_API_ERROR_TAGS } from 'src/services/composio-errors';
8 import { TerminalUI } from 'src/services/terminal-ui';
9 import { normalizeFileUploadSchema } from 'src/services/tool-file-uploads';
10 import { getOrFetchToolInputDefinition } from 'src/services/tool-input-validation';
11 import {
12 MISSING_SLUG_HINT,
13 MISSING_SLUG_MESSAGE,
14 executeToolHint,
15 fetchingToolMessage,
16 formatToolInfoJson,
17 formatToolInfoSummary,
18 inspectSchemaHint,
19 toolInfoNoteTitle,
20 toolNotFoundMessage,
21 toolSuggestion,
22 } from './tools.info.format';
23
24 /** The arguments of `composio tools info`. */
25 export type ToolsInfoParams = {
26 readonly slug: Option.Option<string>;
27 };
28
29 const invalidInput = commandInputError('tools info');
30
31 /** A missing slug fails with a usage error that carries an example, before any request. */
32 const missingSlug = () => invalidInput(`${MISSING_SLUG_MESSAGE}\n${MISSING_SLUG_HINT}`, '<slug>');
33
34 /**
35 * Fetches the tool, or reports the API failure with "did you mean" suggestions and fails
36 * already reported (exit 1).
37 */
38 const fetchTool = (slug: string) =>
39 Effect.gen(function* () {
40 const ui = yield* TerminalUI;
41 const repo = yield* ComposioToolkitsRepository;
42 return yield* ui.withSpinner(fetchingToolMessage(slug), repo.getToolDetailed(slug)).pipe(
43 Effect.catchTag(
44 CLIENT_API_ERROR_TAGS,
45 handleClientApiError(ui, {
46 fallbackMessage: toolNotFoundMessage(slug),
47 hint: [
48 commandHintStep('Browse available toolkits', 'dev.toolkits.list'),
49 commandHintStep('Then list tools', 'root.tools.list'),
50 ].join('\n'),
51 searchForSuggestions: () =>
52 repo
53 .searchTools({ search: slug, limit: 3 })
54 .pipe(Effect.map(result => result.items.map(toolSuggestion))),
55 })
56 )
57 );
58 });
59
60 const showTool = (slug: string, tool: ToolRetrieveResponse) =>
61 Effect.gen(function* () {
62 const ui = yield* TerminalUI;
63 const definition = yield* getOrFetchToolInputDefinition(slug);
64
65 yield* ui.note(formatToolInfoSummary(tool, definition), toolInfoNoteTitle(tool));
66 yield* ui.log.step(inspectSchemaHint(definition));
67 yield* ui.log.step(executeToolHint(tool));
68 yield* ui.output(
69 formatToolInfoJson(tool, definition, normalizeFileUploadSchema(definition.schema))
70 );
71 });
72
73 /**
74 * View a brief summary of a tool and the CLI-facing input schema `composio execute` uses.
75 *
76 * @example
77 * ```bash
78 * composio tools info "GMAIL_SEND_EMAIL"
79 * ```
80 */
81 export const runToolsInfo = ({ slug }: ToolsInfoParams) =>
82 Effect.gen(function* () {
83 return yield* Option.match(slug, {
84 onNone: () => Effect.fail(missingSlug()),
85 onSome: slugValue =>
86 fetchTool(slugValue).pipe(Effect.flatMap(tool => showTool(slugValue, tool))),
87 });
88 });