@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 39 lines 1.8 KB
Raw
1 /**
2 * Display encoding for text that did not come from the CLI itself (event fields, API messages,
3 * tool and proxy responses) and is about to reach a terminal or a captured transcript.
4 *
5 * Every C0 control (U+0000–U+001F), DEL (U+007F) and C1 control (U+0080–U+009F) becomes a
6 * visible JSON-style `\uXXXX` escape, so the text can neither change terminal state nor split
7 * one record into several lines. Printable Unicode and backslashes are left as they are. Apply
8 * it to decoded text before styling, so the CLI's own colors still render.
9 *
10 * The code points involved are all in the Basic Multilingual Plane and outside the surrogate
11 * range, so walking UTF-16 code units never splits an astral character.
12 */
13 const isEscaped = (code: number, keepLineBreaks: boolean): boolean => {
14 if (keepLineBreaks && (code === 0x0a || code === 0x09)) return false;
15 return code <= 0x1f || (code >= 0x7f && code <= 0x9f);
16 };
17
18 const escapeControls = (text: string, keepLineBreaks: boolean): string => {
19 let escaped = '';
20 let start = 0;
21 for (let index = 0; index < text.length; index += 1) {
22 const code = text.charCodeAt(index);
23 if (isEscaped(code, keepLineBreaks)) {
24 escaped += `${text.slice(start, index)}\\u${code.toString(16).padStart(4, '0')}`;
25 start = index + 1;
26 }
27 }
28 return start === 0 ? text : escaped + text.slice(start);
29 };
30
31 /** For one-line display fields and records: every control, line breaks and tabs included. */
32 export const escapeTerminalText = (text: string): string => escapeControls(text, false);
33
34 /**
35 * For intentionally multiline display (notes, pretty-printed JSON): CRLF becomes LF, LF and TAB
36 * stay, and every other control, a lone CR included, is escaped.
37 */
38 export const escapeTerminalLines = (text: string): string =>
39 escapeControls(text.replaceAll('\r\n', '\n'), true);