@mcpc-tech/core 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +56 -41
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -173,16 +173,8 @@ var init_logger = __esm({
173
173
  }
174
174
  logToConsole(level, data) {
175
175
  const message = typeof data === "string" ? data : JSON.stringify(data);
176
- const prefix = `[${this.loggerName}]`;
177
- if (level === "debug") {
178
- console.debug(prefix, message);
179
- } else if (level === "info" || level === "notice") {
180
- console.info(prefix, message);
181
- } else if (level === "warning") {
182
- console.warn(prefix, message);
183
- } else {
184
- console.error(prefix, message);
185
- }
176
+ const prefix = `[${this.loggerName}:${level}]`;
177
+ console.error(prefix, message);
186
178
  }
187
179
  debug(data) {
188
180
  return this.log("debug", data);
@@ -693,9 +685,42 @@ function stripMarkdownAndText(text) {
693
685
  text = text.replace(/^```(?:json)?\s*\n?/i, "");
694
686
  text = text.replace(/\n?```\s*$/, "");
695
687
  text = text.replace(/^(?:here is|here's|response|result|output|json):\s*/i, "");
696
- const jsonMatch = text.match(/(\{[\s\S]*\}|\[[\s\S]*\])/);
697
- if (jsonMatch) {
698
- text = jsonMatch[1];
688
+ const firstJsonIndex = text.search(/[\{\[]/);
689
+ if (firstJsonIndex >= 0) {
690
+ text = text.slice(firstJsonIndex);
691
+ let depth = 0;
692
+ let inString = false;
693
+ let escapeNext = false;
694
+ const startChar = text[0];
695
+ const endChar = startChar === "{" ? "}" : "]";
696
+ for (let i = 0; i < text.length; i++) {
697
+ const char = text[i];
698
+ if (escapeNext) {
699
+ escapeNext = false;
700
+ continue;
701
+ }
702
+ if (char === "\\") {
703
+ escapeNext = true;
704
+ continue;
705
+ }
706
+ if (char === '"' && !inString) {
707
+ inString = true;
708
+ continue;
709
+ }
710
+ if (char === '"' && inString) {
711
+ inString = false;
712
+ continue;
713
+ }
714
+ if (inString) continue;
715
+ if (char === startChar) {
716
+ depth++;
717
+ } else if (char === endChar) {
718
+ depth--;
719
+ if (depth === 0) {
720
+ return text.slice(0, i + 1);
721
+ }
722
+ }
723
+ }
699
724
  }
700
725
  return text.trim();
701
726
  }
@@ -2072,7 +2097,7 @@ var BaseSamplingExecutor = class {
2072
2097
  role: "user",
2073
2098
  content: {
2074
2099
  type: "text",
2075
- text: 'Return ONLY raw JSON (no code fences or explanations). The JSON MUST include action and decision. Example: {"action":"<tool>","decision":"proceed|complete","<tool>":{}}'
2100
+ text: 'Return ONE AND ONLY ONE raw JSON object (no code fences, explanations, or multiple objects). The JSON MUST include action and decision. Example: {"action":"<tool>","decision":"proceed|complete","<tool>":{}}'
2076
2101
  }
2077
2102
  }
2078
2103
  ];
@@ -2108,15 +2133,13 @@ var BaseSamplingExecutor = class {
2108
2133
  if (iterationSpan) endSpan(iterationSpan);
2109
2134
  continue;
2110
2135
  }
2111
- if (parsedData) {
2112
- this.conversationHistory.push({
2113
- role: "assistant",
2114
- content: {
2115
- type: "text",
2116
- text: JSON.stringify(parsedData, null, 2)
2117
- }
2118
- });
2119
- }
2136
+ this.conversationHistory.push({
2137
+ role: "assistant",
2138
+ content: {
2139
+ type: "text",
2140
+ text: JSON.stringify(parsedData, null, 2)
2141
+ }
2142
+ });
2120
2143
  const action = parsedData["action"];
2121
2144
  const actionStr = action && typeof action === "string" ? String(action) : "unknown_action";
2122
2145
  const spanName = `mcpc.sampling_iteration.${actionStr}`;
@@ -2194,23 +2217,15 @@ var BaseSamplingExecutor = class {
2194
2217
  return await this.createExecutionError(error, loopSpan);
2195
2218
  }
2196
2219
  }
2197
- addParsingErrorToHistory(responseText, parseError) {
2198
- this.conversationHistory.push({
2199
- role: "assistant",
2200
- content: {
2201
- type: "text",
2202
- text: `JSON parsing failed. Response was: ${responseText}`
2203
- }
2204
- });
2220
+ addParsingErrorToHistory(_responseText, parseError) {
2221
+ const errorMsg = parseError instanceof Error ? parseError.message : String(parseError);
2205
2222
  this.conversationHistory.push({
2206
2223
  role: "user",
2207
2224
  content: {
2208
2225
  type: "text",
2209
- text: CompiledPrompts.errorResponse({
2210
- errorMessage: `JSON parsing failed: ${parseError instanceof Error ? parseError.message : String(parseError)}
2226
+ text: `Invalid JSON: ${errorMsg}
2211
2227
 
2212
- Please respond with valid JSON.`
2213
- })
2228
+ Respond with valid JSON.`
2214
2229
  }
2215
2230
  });
2216
2231
  }
@@ -2348,11 +2363,11 @@ ${msg.content.text}`;
2348
2363
  });
2349
2364
  }
2350
2365
  injectJsonInstruction({ prompt, schema, schemaPrefix = "JSON schema:", schemaSuffix = `STRICT REQUIREMENTS:
2351
- 1. Return ONLY raw JSON that passes JSON.parse() - no markdown, code blocks, explanatory text, or extra characters
2366
+ 1. Return ONE AND ONLY ONE raw JSON object that passes JSON.parse() - no markdown, code blocks, explanatory text, or multiple JSON objects
2352
2367
  2. Include ALL required fields with correct data types and satisfy ALL schema constraints (anyOf, oneOf, allOf, not, enum, pattern, min/max, conditionals)
2353
- 3. Your response must be the JSON object itself, nothing else
2368
+ 3. Your response must be a single JSON object, nothing else
2354
2369
 
2355
- INVALID: \`\`\`json{"key":"value"}\`\`\` or "Here is: {"key":"value"}"
2370
+ INVALID: \`\`\`json{"key":"value"}\`\`\` or "Here is: {"key":"value"}" or {"key":"value"}{"key":"value"}
2356
2371
  VALID: {"key":"value"}` }) {
2357
2372
  return [
2358
2373
  prompt != null && prompt.length > 0 ? prompt : void 0,
@@ -2473,10 +2488,10 @@ ${JSON.stringify(context2, null, 2)}`;
2473
2488
  const taskPrompt = `
2474
2489
 
2475
2490
  ## Current Task
2476
- I will now use agentic sampling to complete the following task: "${userRequest}"${contextInfo}
2491
+ You will now use agentic sampling to complete the following task: "${userRequest}"${contextInfo}
2477
2492
 
2478
- When I need to use a tool, I should specify the tool name in 'action' and provide tool-specific parameters as additional properties.
2479
- When the task is complete, I should use "action": "complete".`;
2493
+ When you need to use a tool, specify the tool name in 'action' and provide tool-specific parameters as additional properties.
2494
+ When the task is complete, use "action": "complete".`;
2480
2495
  return this.injectJsonInstruction({
2481
2496
  prompt: basePrompt + taskPrompt,
2482
2497
  schema: agenticSchema
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcpc-tech/core",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "homepage": "https://jsr.io/@mcpc/core",
5
5
  "type": "module",
6
6
  "dependencies": {