MCPcopy
hub / github.com/Doorman11991/smallcode / extractFencedTypeScript

Function extractFencedTypeScript

src/compiled/cognition/validate.js:499–528  ·  view source on GitHub ↗

* Extract the largest TypeScript-tagged code fence from a model's response. * Models commonly wrap their output in ```typescript ... ``` (sometimes ```ts * or ```tsx). When that's present, the prose around the fence is never valid * TS — keeping it in the validator would just produce noise. If no

(value)

Source from the content-addressed store, hash-verified

497 * forgot the language tag.
498 */
499function extractFencedTypeScript(value) {
500 // Match ```<lang?>\n ... \n``` blocks. Lang is optional and case-insensitive.
501 // We use a non-greedy `[\s\S]*?` so consecutive fences don't merge.
502 const fenceRe = /```([a-zA-Z]*)\s*\n([\s\S]*?)\n```/g;
503 const tsFences = [];
504 const untaggedFences = [];
505 let m;
506 while ((m = fenceRe.exec(value)) !== null) {
507 const lang = (m[1] || "").toLowerCase();
508 const body = m[2];
509 if (lang === "typescript" || lang === "ts" || lang === "tsx") {
510 tsFences.push(body);
511 }
512 else if (lang === "") {
513 untaggedFences.push(body);
514 }
515 }
516 // Prefer typescript-tagged fences. Fall back to the largest untagged one
517 // since some models drop the language tag. If neither, return the whole
518 // input — the legacy behaviour.
519 const pool = tsFences.length > 0 ? tsFences : untaggedFences;
520 if (pool.length === 0)
521 return value;
522 let best = pool[0];
523 for (let i = 1; i < pool.length; i++) {
524 if (pool[i].length > best.length)
525 best = pool[i];
526 }
527 return best;
528}
529/**
530 * Delegate to a user-supplied extension_point. The extension may return
531 * either a `ValidationReport` shape (`{ ok, issues }`) for rich feedback,

Callers 1

validateTypeScriptStringFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected