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
| 1 | import type { BasicBuilder } from './BasicBuilder'; |
| 2 | import type { FormattingOptions } from './QuoteStyle'; |
| 3 | import { DEFAULT_FORMATTING_OPTIONS } from './QuoteStyle'; |
| 4 | |
| 5 | export const INDENT_SIZE = 2; |
| 6 | /** |
| 7 | * Helper class for building long multi-line formatted strings from building blocks. |
| 8 | * Can use either plain strings or `Builder` objects, that would encapsulate formatting logic. |
| 9 | */ |
| 10 | export class Writer<ContextType = undefined> { |
| 11 | private lines: string[] = []; |
| 12 | private currentLine = ''; |
| 13 | private currentIndent = 0; |
| 14 | private marginSymbol?: string; |
| 15 | private afterNextNewLineCallback?: () => void; |
| 16 | readonly formattingOptions: FormattingOptions; |
| 17 | |
| 18 | constructor( |
| 19 | startingIndent = 0, |
| 20 | readonly context: ContextType, |
| 21 | formattingOptions: Partial<FormattingOptions> = {} |
| 22 | ) { |
| 23 | this.currentIndent = startingIndent; |
| 24 | this.formattingOptions = { ...DEFAULT_FORMATTING_OPTIONS, ...formattingOptions }; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Adds provided value to the current line. Does not end the line. |
| 29 | * |
| 30 | * @param value |
| 31 | * @returns |
| 32 | */ |
| 33 | write(value: string | BasicBuilder<ContextType>): this { |
| 34 | if (typeof value === 'string') { |
| 35 | this.currentLine += value; |
| 36 | } else { |
| 37 | value.write(this); |
| 38 | } |
| 39 | return this; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Adds several `values` to the current line, separated by `separator`. Both values and separator |
| 44 | * can also be `Builder` instances for more advanced formatting. |
| 45 | * |
| 46 | * @param separator |
| 47 | * @param values |
| 48 | * @param writeItem allow to customize how individual item is written |
| 49 | * @returns |
| 50 | */ |
| 51 | writeJoined<T extends string | BasicBuilder<ContextType>>( |
| 52 | separator: string | BasicBuilder<ContextType>, |
| 53 | values: T[], |
| 54 | writeItem: (item: T, writer: this) => void = (item, w) => w.write(item) |
| 55 | ): this { |
| 56 | const last = values.length - 1; |
| 57 | for (let i = 0; i < values.length; i++) { |
| 58 | writeItem(values[i], this); |
| 59 | if (i !== last) { |
| 60 | this.write(separator); |
| 61 | } |
| 62 | } |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Adds a string to current line, flushes current line and starts a new line. |
| 68 | * @param line |
| 69 | * @returns |
| 70 | */ |
| 71 | writeLine(line: string | BasicBuilder<ContextType>): this { |
| 72 | return this.write(line).newLine(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Flushes current line and starts a new line. New line starts at previously configured indentation level |
| 77 | * @returns |
| 78 | */ |
| 79 | newLine(): this { |
| 80 | this.lines.push(this.indentedCurrentLine()); |
| 81 | this.currentLine = ''; |
| 82 | this.marginSymbol = undefined; |
| 83 | |
| 84 | const afterNextNewLineCallback = this.afterNextNewLineCallback; |
| 85 | this.afterNextNewLineCallback = undefined; |
| 86 | afterNextNewLineCallback?.(); |
| 87 | return this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Increases indentation level by 1, calls provided callback and then decreases indentation again. |
| 92 | * Could be used for writing indented blocks of text: |
| 93 | * |
| 94 | * @example |
| 95 | * ```ts |
| 96 | * writer |
| 97 | * .writeLine('{') |
| 98 | * .withIndent(() => { |
| 99 | * writer.writeLine('foo: 123'); |
| 100 | * writer.writeLine('bar: 456'); |
| 101 | * }) |
| 102 | * .writeLine('}') |
| 103 | * ``` |
| 104 | * @param callback |
| 105 | * @returns |
| 106 | */ |
| 107 | withIndent(callback: (writer: this) => void): this { |
| 108 | this.indent(); |
| 109 | callback(this); |
| 110 | this.unindent(); |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Calls provided callback next time when new line is started. |
| 116 | * Callback is called after old line have already been flushed and a new |
| 117 | * line have been started. Can be used for adding "between the lines" decorations, |
| 118 | * such as underlines. |
| 119 | * |
| 120 | * @param callback |
| 121 | * @returns |
| 122 | */ |
| 123 | afterNextNewline(callback: () => void) { |
| 124 | this.afterNextNewLineCallback = callback; |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Increases indentation level of the current line by 1 |
| 130 | * @returns |
| 131 | */ |
| 132 | indent(): this { |
| 133 | this.currentIndent++; |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Decreases indentation level of the current line by 1, if it is possible |
| 139 | * @returns |
| 140 | */ |
| 141 | unindent(): this { |
| 142 | if (this.currentIndent > 0) { |
| 143 | this.currentIndent--; |
| 144 | } |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Adds a symbol, that will replace the first character of the current line (including indentation) |
| 150 | * when it is flushed. Can be used for adding markers to the line. |
| 151 | * |
| 152 | * Note: if indentation level of the line is 0, it will replace the first actually printed character |
| 153 | * of the line. Use with caution. |
| 154 | * @param symbol |
| 155 | * @returns |
| 156 | */ |
| 157 | addMarginSymbol(symbol: string): this { |
| 158 | this.marginSymbol = symbol; |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | toString() { |
| 163 | return this.lines.concat(this.indentedCurrentLine()).join('\n'); |
| 164 | } |
| 165 | |
| 166 | getCurrentLineLength() { |
| 167 | return this.currentLine.length; |
| 168 | } |
| 169 | |
| 170 | private indentedCurrentLine(): string { |
| 171 | const line = this.currentLine.padStart( |
| 172 | this.currentLine.length + INDENT_SIZE * this.currentIndent |
| 173 | ); |
| 174 | if (this.marginSymbol) { |
| 175 | return this.marginSymbol + line.slice(1); |
| 176 | } |
| 177 | return line; |
| 178 | } |
| 179 | } |