Skip to content

Commit d436789

Browse files
authored
feat: Reduce whitespace in tool responses by not formatting JSON (#284)
Based on my research, formatted JSON (i.e. using indentation) does not seem to lead to improved LLM performance. This indicates that whitespace tokens within JSON basically have no effect and can be removed safely. By removing whitespaces, we can save on the overall token consumption for affected tool calls.
1 parent 7624c00 commit d436789

6 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/tools/get_api_reference/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function createResource(apiRef: FormattedSymbol, frameworkName: string, framewor
3838
return {
3939
type: "resource",
4040
resource: {
41-
text: JSON.stringify(apiRef, null, 2),
41+
text: JSON.stringify(apiRef),
4242
uri: createUriForSymbol(apiRef, frameworkName, frameworkVersion),
4343
mimeType: "application/json",
4444
},

src/tools/get_version_info/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function registerTool(registerTool: RegisterTool, _context: Conte
2929
return {
3030
content: [{
3131
type: "text",
32-
text: JSON.stringify(versionInfo, null, 2),
32+
text: JSON.stringify(versionInfo),
3333
}],
3434
structuredContent: versionInfo,
3535
};

src/tools/run_ui5_linter/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function registerTool(registerTool: RegisterTool, context: Contex
3737
});
3838
const content: (TextContent | EmbeddedResource)[] = [{
3939
type: "text",
40-
text: JSON.stringify(results.results, null, 2),
40+
text: JSON.stringify(results.results),
4141
}];
4242
if (results.contextInformation) {
4343
const {ruleDescriptions, documentationResources, migrationGuides, apiReferences} =
@@ -49,7 +49,7 @@ export default function registerTool(registerTool: RegisterTool, context: Contex
4949
type: "resource",
5050
resource: {
5151
uri: `ui5-linter-result://${projectUri.pathname}/api-reference-extract-${timestamp}.json`,
52-
text: JSON.stringify(apiReferences, null, 2),
52+
text: JSON.stringify(apiReferences),
5353
mimeType: "application/json",
5454
},
5555
});
@@ -77,7 +77,7 @@ export default function registerTool(registerTool: RegisterTool, context: Contex
7777
if (ruleDescriptions.length) {
7878
content.push({
7979
type: "text",
80-
text: JSON.stringify(ruleDescriptions, null, 2),
80+
text: JSON.stringify(ruleDescriptions),
8181
});
8282
}
8383
}

test/lib/tools/get_api_reference/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ test("get_api_reference tool returns API reference on success", async (t) => {
134134
content: [{
135135
type: "resource",
136136
resource: {
137-
text: JSON.stringify(sampleApiRef, null, 2),
137+
text: JSON.stringify(sampleApiRef),
138138
uri: sampleUri,
139139
mimeType: "application/json",
140140
},

test/lib/tools/get_version_info/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test("get_version_info tool returns version information on success", async (t) =
8989
content: [
9090
{
9191
type: "text",
92-
text: JSON.stringify(sampleVersionInfo, null, 2),
92+
text: JSON.stringify(sampleVersionInfo),
9393
},
9494
],
9595
});

test/lib/tools/run_ui5_linter/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ test("run_ui5_linter tool returns linting results on success with no context inf
112112
content: [
113113
{
114114
type: "text",
115-
text: JSON.stringify(sampleResults.results, null, 2),
115+
text: JSON.stringify(sampleResults.results),
116116
},
117117
],
118118
});
@@ -208,7 +208,7 @@ test("run_ui5_linter tool returns linting results with context information", asy
208208
// Check the first content item (text with results)
209209
t.deepEqual(result.content[0], {
210210
type: "text",
211-
text: JSON.stringify(sampleResults.results, null, 2),
211+
text: JSON.stringify(sampleResults.results),
212212
});
213213

214214
// Check the second content item (extracted API references)
@@ -223,7 +223,7 @@ test("run_ui5_linter tool returns linting results with context information", asy
223223
type: "resource",
224224
resource: {
225225
uri: `ui5-linter-result://${pathToFileURL(path.join(projectDir, "api-reference-extract")).pathname}-<timestamp>.json`,
226-
text: JSON.stringify(sampleResults.contextInformation.apiReferences, null, 2),
226+
text: JSON.stringify(sampleResults.contextInformation.apiReferences),
227227
mimeType: "application/json",
228228
},
229229
});
@@ -251,7 +251,7 @@ test("run_ui5_linter tool returns linting results with context information", asy
251251
// Check the fifth content item (rule descriptions)
252252
t.deepEqual(result.content[4], {
253253
type: "text",
254-
text: JSON.stringify(sampleResults.contextInformation.ruleDescriptions, null, 2),
254+
text: JSON.stringify(sampleResults.contextInformation.ruleDescriptions),
255255
});
256256
});
257257

0 commit comments

Comments
 (0)