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

next
ts 148 lines 6.13 KB
Raw
1 import { Data } from 'effect';
2 import type { Effect } from 'effect';
3 import type { Shell } from 'src/services/shell-config';
4 import type { TerminalUI } from 'src/services/terminal-ui';
5
6 // `composio install` writes nothing to stdout: every line is a report on stderr of what it
7 // changed on disk.
8
9 export const INSTALL_INTRO = 'composio install';
10 export const DONE_OUTRO = 'Done';
11 export const ABORTED_OUTRO = 'Aborted.';
12 export const MANUAL_SETUP_OUTRO = 'Manual setup required.';
13
14 export const BIN_DIR_NOT_ABSOLUTE_MESSAGE = 'Resolved bin directory must be an absolute path.';
15 export const BIN_DIR_UNSAFE_MESSAGE =
16 'Resolved bin directory contains unsafe characters and cannot be written to shell config.';
17
18 export const SHELL_NOT_DETECTED_MESSAGE =
19 'Could not detect your shell. Manually add the following to your shell config:';
20 export const MANUAL_PATH_SETUP_TITLE = 'PATH setup';
21 export const ALREADY_CONFIGURED_MESSAGE = 'Shell integration already configured — nothing to do.';
22 export const RESTART_HINT_TITLE = 'Restart your shell to apply changes';
23
24 export const tildify = (p: string, homedir: string): string =>
25 p.startsWith(homedir + '/') ? `~/${p.slice(homedir.length + 1)}` : p;
26
27 /**
28 * With no shell there is no rc file to write, so the current process's `$PATH` is all the
29 * evidence available. It says nothing about future shells, hence the deliberately hedged wording.
30 */
31 export const alreadyOnPathMessage = (binDir: string, homedir: string) =>
32 `PATH: ${tildify(binDir, homedir)} is already on $PATH in this process, and no shell config file could be identified — nothing to update.`;
33
34 /** The line a user without a detected shell adds by hand; `renderedBinDir` is `$HOME`-relative. */
35 export const manualPathSetupLine = (renderedBinDir: string) =>
36 `export PATH="${renderedBinDir}:$PATH"`;
37
38 export const detectedShellMessage = (shell: Shell) => `Detected shell: ${shell}`;
39
40 export const updatedFileMessage = (filePath: string, homedir: string) =>
41 `Updated ${tildify(filePath, homedir)}`;
42
43 /** Status lines summarizing the planned PATH work, in print order. */
44 export const pathStatusLines = (params: {
45 readonly pathWritten: boolean;
46 readonly pathReplaced: boolean;
47 readonly renderedBinDir: string;
48 }): readonly string[] => {
49 const lines = [
50 ...(params.pathWritten ? [`PATH: will add ${params.renderedBinDir} to $PATH`] : []),
51 ...(params.pathReplaced
52 ? [`PATH: will update the managed block to ${params.renderedBinDir}`]
53 : []),
54 ];
55 return lines.length > 0 ? lines : ['PATH: already configured'];
56 };
57
58 /** What happens to shell completions, decided before anything is written. */
59 export type CompletionPlan = Data.TaggedEnum<{
60 /** zsh never gets a completion block. */
61 SkippedForZsh: Record<never, never>;
62 /** `--completions` was not passed, or `--no-completions` was. */
63 NotRequested: Record<never, never>;
64 /** The completion generator produced nothing for this shell. */
65 Unavailable: Record<never, never>;
66 /** The completion file already carries the completions marker. */
67 AlreadyConfigured: Record<never, never>;
68 /** Append `block` to the completion file. */
69 Install: { readonly block: string };
70 }>;
71 export const CompletionPlan = Data.taggedEnum<CompletionPlan>();
72
73 /** The completions status line for `plan`. */
74 export const completionStatusLine = (
75 plan: CompletionPlan,
76 params: { readonly shell: Shell; readonly completionFile: string; readonly homedir: string }
77 ): string =>
78 CompletionPlan.$match(plan, {
79 SkippedForZsh: () => 'Completions: skipped for zsh',
80 NotRequested: () => 'Completions: skipped by default (pass --completions to enable)',
81 Unavailable: () => 'Completions: not available for this shell',
82 AlreadyConfigured: () => 'Completions: already configured',
83 Install: () =>
84 params.shell === 'fish'
85 ? `Completions: will install fish completions to ${tildify(params.completionFile, params.homedir)}`
86 : 'Completions: will install shell completions',
87 });
88
89 /** Shell-specific body of the restart hint printed after a change. */
90 export const restartHintBody = (params: {
91 readonly shell: Shell;
92 readonly zshPathFile: string;
93 readonly bashrc: string;
94 readonly bashLoginOverride: string | undefined;
95 readonly loginOverrideWritten: boolean;
96 readonly homedir: string;
97 }): string => {
98 switch (params.shell) {
99 case 'fish':
100 return 'exec fish';
101 case 'zsh':
102 return `source ${tildify(params.zshPathFile, params.homedir)}`;
103 case 'bash': {
104 const sourceLine = `source ${tildify(params.bashrc, params.homedir)}`;
105 return params.loginOverrideWritten && params.bashLoginOverride
106 ? [
107 sourceLine,
108 `${tildify(params.bashLoginOverride, params.homedir)} was also updated — that entry applies to new login shells too.`,
109 ].join('\n')
110 : sourceLine;
111 }
112 }
113 };
114
115 /** The stderr writers `composio install` reports through. */
116 export interface InstallReporter {
117 readonly step: (message: string) => Effect.Effect<void>;
118 readonly success: (message: string) => Effect.Effect<void>;
119 readonly warn: (message: string) => Effect.Effect<void>;
120 readonly failure: (message: string) => Effect.Effect<void>;
121 readonly note: (body: string, title: string) => Effect.Effect<void>;
122 }
123
124 /**
125 * Everything this command prints is a report of what it changed on disk, so
126 * it has to reach the user even when stderr is not a terminal: install.sh
127 * runs it as a subprocess, and container/CI installs pipe the whole session
128 * to a build log. Decoration stays the interactive default; `ui.error`
129 * writes the same text unformatted when there is nothing to decorate.
130 */
131 export const makeInstallReporter = (ui: TerminalUI, canDecorate: boolean): InstallReporter => {
132 const plain = (prefix: string) => (message: string) => ui.error(`${prefix}${message}`);
133 return canDecorate
134 ? {
135 step: ui.log.step,
136 success: ui.log.success,
137 warn: ui.log.warn,
138 failure: ui.log.error,
139 note: (body, title) => ui.note(body, title),
140 }
141 : {
142 step: ui.error,
143 success: ui.error,
144 warn: plain('warning: '),
145 failure: plain('error: '),
146 note: (body, title) => ui.error(`${title}:\n${body}`),
147 };
148 };