forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference-doc.ts
More file actions
244 lines (218 loc) · 8.03 KB
/
Copy pathreference-doc.ts
File metadata and controls
244 lines (218 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { SourceFile } from "./source";
import { Comments } from "./comments";
// @ts-ignore
import fs from "fs";
import { parsePythonDocstrings } from "./python";
export interface ReferenceDocConfiguration {
sourcePath: string;
destinationPath: string;
className?: string;
component?: string;
hook?: string;
description?: string;
title?: string;
pythonSymbols?: string[];
typescriptSymbols?: string[];
}
export class ReferenceDoc {
constructor(private readonly referenceDoc: ReferenceDocConfiguration) {}
async generate() {
let generatedDocumentation: string | null = null;
if (this.referenceDoc.pythonSymbols) {
generatedDocumentation = this.generatedDocsPythonSymbols();
} else if (this.referenceDoc.typescriptSymbols) {
generatedDocumentation = await this.generatedDocsTypeScriptSymbols();
} else {
generatedDocumentation = await this.generatedDocsTypeScript();
}
if (generatedDocumentation) {
const dest = this.referenceDoc.destinationPath;
// Ensure parent directory exists so retargeting to a fresh tree
// (e.g. shell-docs) doesn't require manual mkdir.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(fs as any).mkdirSync(dest.split("/").slice(0, -1).join("/"), {
recursive: true,
});
fs.writeFileSync(dest, generatedDocumentation);
console.log(
`Successfully autogenerated ${dest} from ${this.referenceDoc.sourcePath}`,
);
}
}
generateTitle(
title: string,
description: string,
sourcePath: string,
): string {
let result = `---\n`;
result += `title: "${title}"\n`;
if (description) {
result += `description: "${description}"\n`;
}
result += `---\n\n`;
result += `{\n`;
result += ` /*\n`;
result += ` * ATTENTION! DO NOT MODIFY THIS FILE!\n`;
result += ` * This page is auto-generated. If you want to make any changes to this page, changes must be made at:\n`;
result += ` * ${sourcePath}\n`;
result += ` */\n`;
result += `}\n`;
return result;
}
generatedDocsPythonSymbols(): string | null {
const content = fs.readFileSync(this.referenceDoc.sourcePath, "utf8");
const parsed = parsePythonDocstrings(
this.referenceDoc.pythonSymbols!,
content,
);
let result = this.generateTitle(
this.referenceDoc.title || "",
this.referenceDoc.description || "",
this.referenceDoc.sourcePath,
);
for (const fn of this.referenceDoc.pythonSymbols!) {
if (fn in parsed) {
const fnDoc = parsed[fn];
result += `## ${fn}\n\n`;
result += `${fnDoc.description}\n\n`;
if (fnDoc.parameters) {
result += `### Parameters\n\n`;
for (const param of fnDoc.parameters) {
const required = !param.type.startsWith("Optional");
result += `<PropertyReference name="${param.name}" type="${param.type}" ${required ? "required" : ""}> \n`;
result += `${param.description}\n`;
result += `</PropertyReference>\n\n`;
}
}
if (fnDoc.returns) {
result += `### Returns\n\n`;
result += `<PropertyReference name="returns" type="${fnDoc.returns.type}">\n`;
result += `${fnDoc.returns.description}\n`;
result += `</PropertyReference>\n\n`;
}
}
}
return result;
}
async generatedDocsTypeScriptSymbols(): Promise<string | null> {
const source = new SourceFile(this.referenceDoc.sourcePath);
await source.parse();
let result = this.generateTitle(
this.referenceDoc.title || "",
this.referenceDoc.description || "",
this.referenceDoc.sourcePath,
);
for (const fn of this.referenceDoc.typescriptSymbols!) {
const args = source.getFunctionArguments(fn);
if (!args) {
console.warn(`${fn} not found in ${this.referenceDoc.sourcePath}`);
continue;
}
const comment = source.getFunctionComment(fn);
result += `## ${fn}\n\n`;
if (comment) {
result += `${comment}\n\n`;
}
if (args) {
result += `### Parameters\n\n`;
for (const arg of args) {
result += `<PropertyReference name="${arg.name}" type="${arg.type}" ${arg.required ? "required" : ""} ${arg.defaultValue ? `default="${arg.defaultValue}"` : ""}> \n`;
result += `${arg.description}\n`;
result += `</PropertyReference>\n\n`;
}
}
}
return result;
}
async generatedDocsTypeScript(): Promise<string | null> {
const source = new SourceFile(this.referenceDoc.sourcePath);
await source.parse();
const comment = Comments.getFirstCommentBlock(source.sourceFile);
if (!comment) {
console.warn(`No comment found for ${this.referenceDoc.sourcePath}`);
console.warn("Skipping...");
return null;
}
// handle imports
const slashes = this.referenceDoc.destinationPath.split("/").length;
let importPathPrefix = "";
for (let i = 0; i < slashes - 2; i++) {
importPathPrefix += "../";
}
let result = this.generateTitle(
this.referenceDoc.className ||
this.referenceDoc.component ||
this.referenceDoc.hook ||
"",
this.referenceDoc.description || "",
this.referenceDoc.sourcePath,
);
result += `${comment}\n\n`;
const arg0Interface = await source.getArg0Interface(
this.referenceDoc.className ||
this.referenceDoc.component ||
this.referenceDoc.hook ||
"",
);
if (arg0Interface) {
const hasProperties = arg0Interface.properties.length > 0;
if (this.referenceDoc.hook && hasProperties) {
result += `## Parameters\n\n`;
} else if (this.referenceDoc.component && hasProperties) {
result += `## Properties\n\n`;
} else if (this.referenceDoc.className && hasProperties) {
result += `## Constructor Parameters\n\n`;
}
for (const property of arg0Interface.properties) {
if (property.comment.includes("@deprecated")) {
continue;
}
const type = property.type.replace(/"/g, "'");
result += `<PropertyReference name="${property.name}" type="${type}" ${property.required ? "required" : ""} ${property.defaultValue ? `default="${property.defaultValue}"` : ""}> \n`;
result += `${property.comment}\n`;
result += `</PropertyReference>\n\n`;
}
} else if (this.referenceDoc.className) {
const constr = source.getConstructorDefinition(
this.referenceDoc.className,
);
if (constr) {
result += `## ${constr.signature}\n\n`;
result += `${constr.comment}\n\n`;
for (const param of constr.parameters) {
const type = param.type.replace(/"/g, "'");
result += `<PropertyReference name="${param.name}" type="${type}" ${param.required ? "required" : ""}>\n`;
result += `${param.comment}\n`;
result += `</PropertyReference>\n\n`;
}
}
}
if (this.referenceDoc.className) {
const methodDefinitions = await source.getPublicMethodDefinitions(
this.referenceDoc.className,
);
for (const method of methodDefinitions) {
if (
method.signature ===
"process(request: CopilotRuntimeChatCompletionRequest)" ||
method.signature === "process(request: CopilotRuntimeRequest)"
) {
// skip the process method
continue;
}
const methodName = method.signature.split("(")[0];
const methodArgs = method.signature.split("(")[1].split(")")[0];
result += `<PropertyReference name="${methodName}" type="${methodArgs}">\n`;
result += `${method.comment}\n\n`;
for (const param of method.parameters) {
const type = param.type.replace(/"/g, "'");
result += ` <PropertyReference name="${param.name}" type="${type}" ${param.required ? "required" : ""}>\n`;
result += ` ${param.comment}\n`;
result += ` </PropertyReference>\n\n`;
}
result += `</PropertyReference>\n\n`;
}
}
return result;
}
}