@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, 03:37 UTC

next
ts 95 lines 2.28 KB
Raw
1 import type { BasicBuilder } from './BasicBuilder';
2 import { Writer } from './Writer';
3
4 export class DocComment implements BasicBuilder {
5 readonly lines: string[] = [];
6
7 constructor(startingText?: string) {
8 if (startingText) {
9 this.addText(startingText);
10 }
11 }
12
13 addText(text: string): this {
14 this.lines.push(...text.split('\n'));
15 return this;
16 }
17
18 write(writer: Writer) {
19 writer.writeLine('/**');
20 for (const line of this.lines) {
21 writer.writeLine(` * ${line.replaceAll('*/', '*\\/')}`);
22 }
23 writer.writeLine(' */');
24 return writer;
25 }
26 }
27
28 function docComment(strings: TemplateStringsArray, ...args: string[]): DocComment;
29 function docComment(startingText?: string): DocComment;
30 function docComment(
31 firstParameter: string | TemplateStringsArray | undefined,
32 ...args: string[]
33 ): DocComment {
34 if (typeof firstParameter === 'string' || typeof firstParameter === 'undefined') {
35 return new DocComment(firstParameter);
36 }
37 return docCommentTag(firstParameter, args);
38 }
39
40 function docCommentTag(strings: TemplateStringsArray, args: string[]) {
41 const docComment = new DocComment();
42 const fullText = strings
43 .flatMap((str, i) => {
44 if (i < args.length) {
45 return [str, args[i]];
46 }
47 return [str];
48 })
49 .join('');
50 const lines = trimEmptyLines(fullText.split('\n'));
51 if (lines.length === 0) {
52 return docComment;
53 }
54 const indent = getIndent(lines[0]);
55 for (const line of lines) {
56 docComment.addText(line.slice(indent));
57 }
58 return docComment;
59 }
60
61 function trimEmptyLines(lines: string[]): string[] {
62 const firstLine = findFirstNonEmptyLine(lines);
63 const lastLine = findLastNonEmptyLine(lines);
64 if (firstLine === -1 || lastLine === -1) {
65 // all lines are empty
66 return [];
67 }
68 return lines.slice(firstLine, lastLine + 1);
69 }
70
71 function findFirstNonEmptyLine(lines: string[]) {
72 return lines.findIndex(line => !isEmptyLine(line));
73 }
74
75 function findLastNonEmptyLine(lines: string[]) {
76 let i = lines.length - 1;
77 while (i > 0 && isEmptyLine(lines[i])) {
78 i--;
79 }
80 return i;
81 }
82
83 function isEmptyLine(line: string) {
84 return line.trim().length === 0;
85 }
86
87 function getIndent(line: string): number {
88 let indent = 0;
89 while (line[indent] === ' ') {
90 indent++;
91 }
92 return indent;
93 }
94
95 export { docComment };