Skip to content

Commit 5aacdf8

Browse files
authored
feat(wiki): better replacement of built-in-env if json is too long (#1426)
1 parent 03ea260 commit 5aacdf8

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed
+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { jsonReplacer } from '../../util/json';
2+
import { builtInEnvJsonReplacer } from '../../dataflow/environments/environment';
23

34
export function codeBlock(language: string, code: string | undefined): string {
45
return `\n\`\`\`${language}\n${code?.trim() ?? ''}\n\`\`\`\n`;
@@ -8,10 +9,18 @@ export function codeInline(code: string): string {
89
return `<code>${code}</code>`;
910
}
1011

11-
export function jsonWithLimit(object: object, maxLength: number = 5_000, tooLongText: string = '_As the code is pretty long, we inhibit pretty printing and syntax highlighting (JSON):_'): string {
12+
export function jsonWithLimit(object: object, maxLength: number = 5_000, tooLongText: string = '_As the code is pretty long, we inhibit pretty printing and syntax highlighting (JSON, hiding built-in):_'): string {
1213
const prettyPrinted = JSON.stringify(object, jsonReplacer, 2);
1314
return `
1415
${prettyPrinted.length > maxLength ? tooLongText : ''}
15-
${codeBlock(prettyPrinted.length > maxLength ? 'text' : 'json', prettyPrinted.length > 5_000 ? JSON.stringify(object, jsonReplacer) : prettyPrinted)}
16+
${codeBlock(prettyPrinted.length > maxLength ? 'text' : 'json', prettyPrinted.length > 5_000 ? JSON.stringify(object,
17+
(k, v) => {
18+
if(typeof v === 'object' && v !== null && 'id' in v && (v as {id: number})['id'] === 0 && 'memory' in v && (v as {memory: undefined | null | object})['memory']) {
19+
return '<BuiltInEnvironment>';
20+
} else {
21+
return builtInEnvJsonReplacer(k, v);
22+
}
23+
}
24+
) : prettyPrinted)}
1625
`;
1726
}

0 commit comments

Comments
 (0)