Skip to content

Commit 4de0119

Browse files
committed
feat: Bump version to 0.1.1-beta.11 and enhance JSON parsing with error handling improvements
1 parent b267f90 commit 4de0119

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

packages/core/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mcpc/core",
3-
"version": "0.1.1-beta.8",
3+
"version": "0.1.1-beta.11",
44
"tasks": {
55
"server:compile": "echo \"no need to compile\"",
66
"test": "deno test --allow-env --allow-read tests/",

packages/core/src/executors/sampling/base-sampling-executor.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Ajv } from "ajv";
55
import { AggregateAjvError } from "@segment/ajv-human-errors";
66
import addFormats from "ajv-formats";
77
import { parseJSON } from "../../utils/common/json.ts";
8+
import { inspect } from "node:util";
89

910
const ajv = new Ajv({
1011
allErrors: true,
@@ -52,7 +53,7 @@ export abstract class BaseSamplingExecutor {
5253
protected allToolNames: string[],
5354
protected toolNameToDetailList: [string, ExternalTool][],
5455
protected server: ComposableMCPServer,
55-
config?: SamplingConfig,
56+
config?: SamplingConfig
5657
) {
5758
if (config?.maxIterations) {
5859
this.maxIterations = config.maxIterations;
@@ -62,7 +63,7 @@ export abstract class BaseSamplingExecutor {
6263
protected async runSamplingLoop<TState>(
6364
systemPrompt: () => string,
6465
schema: Record<string, unknown>,
65-
state?: TState,
66+
state?: TState
6667
) {
6768
// Initialize conversation
6869
this.conversationHistory = [];
@@ -84,9 +85,8 @@ export abstract class BaseSamplingExecutor {
8485
// Parse JSON response
8586
let parsedData: Record<string, unknown>;
8687
try {
87-
parsedData = parseJSON(responseContent.trim()) ?? {};
88+
parsedData = parseJSON(responseContent.trim(), true);
8889
} catch (parseError) {
89-
// Handle parsing error
9090
this.addParsingErrorToHistory(responseContent, parseError);
9191
continue;
9292
}
@@ -96,13 +96,7 @@ export abstract class BaseSamplingExecutor {
9696
role: "assistant",
9797
content: {
9898
type: "text",
99-
text: `Executing with arguments: ${
100-
JSON.stringify(
101-
parsedData,
102-
null,
103-
2,
104-
)
105-
}`,
99+
text: JSON.stringify(parsedData, null, 2),
106100
},
107101
});
108102
}
@@ -137,7 +131,7 @@ export abstract class BaseSamplingExecutor {
137131

138132
protected addParsingErrorToHistory(
139133
responseText: string,
140-
parseError: unknown,
134+
parseError: unknown
141135
): void {
142136
this.conversationHistory.push({
143137
role: "assistant",
@@ -168,8 +162,7 @@ export abstract class BaseSamplingExecutor {
168162
{
169163
type: "text",
170164
text: CompiledPrompts.errorResponse({
171-
errorMessage:
172-
`Execution reached maximum iterations (${this.maxIterations}). Please try with a more specific request or break down the task into smaller parts.`,
165+
errorMessage: `Execution reached maximum iterations (${this.maxIterations}). Please try with a more specific request or break down the task into smaller parts.`,
173166
}),
174167
},
175168
],
@@ -240,7 +233,7 @@ ${text}
240233

241234
protected logIterationProgress(
242235
parsedData: Record<string, unknown>,
243-
result: CallToolResult,
236+
result: CallToolResult
244237
): void {
245238
// Optional: Log iteration progress for debugging
246239
console.log(
@@ -249,15 +242,16 @@ ${text}
249242
parsedData,
250243
isError: result.isError,
251244
isComplete: result.isComplete,
252-
},
245+
result: inspect(result),
246+
}
253247
);
254248
}
255249

256250
// Abstract methods that subclasses must implement
257251
protected abstract processAction<TState>(
258252
parsedData: Record<string, unknown>,
259253
schema: Record<string, unknown>,
260-
state?: TState,
254+
state?: TState
261255
): Promise<CallToolResult>;
262256

263257
protected injectJsonInstruction({
@@ -291,7 +285,7 @@ VALID: {"key":"value"}`,
291285
// Validate arguments using JSON schema
292286
protected validateSchema(
293287
args: Record<string, unknown>,
294-
schema: Record<string, unknown>,
288+
schema: Record<string, unknown>
295289
): {
296290
valid: boolean;
297291
error?: string;

packages/core/src/utils/common/json.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@ import { inspect } from "node:util";
44
/**
55
* Attempts to parse JSON with a repair function if initial parse fails.
66
*/
7-
export function parseJSON<T>(text: string): T | null {
7+
export function parseJSON<T, U extends boolean>(
8+
text: string,
9+
throwError?: U
10+
): U extends false ? T | null : T {
811
try {
912
return JSON.parse(text) as T;
1013
} catch (_error) {
1114
try {
1215
const repairedText = jsonrepair(text);
1316
console.warn(
14-
`Failed to parse JSON, attempting to repair, result: ${text}`,
17+
`Failed to parse JSON, attempting to repair, result: ${text}`
1518
);
19+
if (throwError) {
20+
throw _error;
21+
}
1622
return JSON.parse(repairedText) as T;
1723
} catch {
18-
return null;
24+
if (throwError) {
25+
throw new Error("Failed to parse repaired JSON");
26+
}
27+
return null as T;
1928
}
2029
}
2130
}

0 commit comments

Comments
 (0)