@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.
- package/index.mjs +56 -41
- 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
|
-
|
|
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
|
|
697
|
-
if (
|
|
698
|
-
text =
|
|
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
|
|
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
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
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(
|
|
2198
|
-
|
|
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:
|
|
2210
|
-
errorMessage: `JSON parsing failed: ${parseError instanceof Error ? parseError.message : String(parseError)}
|
|
2226
|
+
text: `Invalid JSON: ${errorMsg}
|
|
2211
2227
|
|
|
2212
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
2491
|
+
You will now use agentic sampling to complete the following task: "${userRequest}"${contextInfo}
|
|
2477
2492
|
|
|
2478
|
-
When
|
|
2479
|
-
When the task is 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
|