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

Function extractFencedTypeScript

src/compiled/cognition/validate.ts:477–503  ·  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: string)

Source from the content-addressed store, hash-verified

475 * forgot the language tag.
476 */
477function extractFencedTypeScript(value: string): string {
478 // Match ```<lang?>\n ... \n``` blocks. Lang is optional and case-insensitive.
479 // We use a non-greedy `[\s\S]*?` so consecutive fences don't merge.
480 const fenceRe = /```([a-zA-Z]*)\s*\n([\s\S]*?)\n```/g;
481 const tsFences: string[] = [];
482 const untaggedFences: string[] = [];
483 let m: RegExpExecArray | null;
484 while ((m = fenceRe.exec(value)) !== null) {
485 const lang = (m[1] || "").toLowerCase();
486 const body = m[2];
487 if (lang === "typescript" || lang === "ts" || lang === "tsx") {
488 tsFences.push(body);
489 } else if (lang === "") {
490 untaggedFences.push(body);
491 }
492 }
493 // Prefer typescript-tagged fences. Fall back to the largest untagged one
494 // since some models drop the language tag. If neither, return the whole
495 // input — the legacy behaviour.
496 const pool = tsFences.length > 0 ? tsFences : untaggedFences;
497 if (pool.length === 0) return value;
498 let best = pool[0];
499 for (let i = 1; i < pool.length; i++) {
500 if (pool[i].length > best.length) best = pool[i];
501 }
502 return best;
503}
504
505/**
506 * Delegate to a user-supplied extension_point. The extension may return

Callers 1

validateTypeScriptStringFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected