|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { |
|
ModelVendor, |
|
SymbolicPrimitive, |
|
GrammarStyle, |
|
SymbolicOperation, |
|
VendorImplementation, |
|
SYMBOLIC_RUNTIME_SCHEMA |
|
} from './universal-symbolics-runtime'; |
|
|
|
|
|
|
|
|
|
export interface GlyphMapping { |
|
glyph: string; |
|
primitive: SymbolicPrimitive; |
|
description: string; |
|
} |
|
|
|
export interface TagMapping { |
|
tag: string; |
|
primitive: SymbolicPrimitive; |
|
vendor: ModelVendor; |
|
description: string; |
|
} |
|
|
|
export interface SlashMapping { |
|
slash: string; |
|
primitive: SymbolicPrimitive; |
|
vendor: ModelVendor; |
|
description: string; |
|
} |
|
|
|
export interface DotMapping { |
|
dot: string; |
|
primitive: SymbolicPrimitive; |
|
description: string; |
|
} |
|
|
|
export interface OperatorMapping { |
|
operator: string; |
|
primitive: SymbolicPrimitive; |
|
description: string; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class SymbolicsRegistry { |
|
|
|
private glyphMappings: Map<string, GlyphMapping>; |
|
private tagMappings: Map<string, TagMapping>; |
|
private slashMappings: Map<string, SlashMapping>; |
|
private dotMappings: Map<string, DotMapping>; |
|
private operatorMappings: Map<string, OperatorMapping>; |
|
|
|
|
|
private vendorCapabilities: Map<ModelVendor, Set<SymbolicPrimitive>>; |
|
|
|
|
|
private customSymbols: Map<string, SymbolicOperation>; |
|
|
|
constructor() { |
|
|
|
this.glyphMappings = new Map(); |
|
this.tagMappings = new Map(); |
|
this.slashMappings = new Map(); |
|
this.dotMappings = new Map(); |
|
this.operatorMappings = new Map(); |
|
this.vendorCapabilities = new Map(); |
|
this.customSymbols = new Map(); |
|
|
|
|
|
this.initializeRegistry(); |
|
} |
|
|
|
|
|
|
|
|
|
private initializeRegistry(): void { |
|
|
|
for (const vendor of Object.values(ModelVendor)) { |
|
this.vendorCapabilities.set(vendor, new Set()); |
|
} |
|
|
|
|
|
for (const operation of SYMBOLIC_RUNTIME_SCHEMA) { |
|
|
|
if (operation.glyphs) { |
|
for (const glyph of operation.glyphs) { |
|
this.registerGlyph(glyph, operation.type, `Glyph for ${operation.name}`); |
|
} |
|
} |
|
|
|
|
|
if (operation.tags) { |
|
for (const tag of operation.tags) { |
|
for (const impl of operation.vendorImplementations) { |
|
if (impl.style === GrammarStyle.XML_TAGS) { |
|
this.registerTag(tag, operation.type, impl.vendor, `XML tag for ${operation.name}`); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
if (operation.slashes) { |
|
for (const slash of operation.slashes) { |
|
for (const impl of operation.vendorImplementations) { |
|
if (impl.style === GrammarStyle.SLASH_COMMANDS) { |
|
this.registerSlash(slash, operation.type, impl.vendor, `Slash command for ${operation.name}`); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
const dotCommand = operation.universalSyntax.split('{')[0]; |
|
this.registerDot(dotCommand, operation.type, `Universal syntax for ${operation.name}`); |
|
|
|
|
|
for (const impl of operation.vendorImplementations) { |
|
const capabilities = this.vendorCapabilities.get(impl.vendor); |
|
if (capabilities) { |
|
capabilities.add(operation.type); |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
public registerGlyph(glyph: string, primitive: SymbolicPrimitive, description: string): void { |
|
this.glyphMappings.set(glyph, { glyph, primitive, description }); |
|
} |
|
|
|
|
|
|
|
|
|
public registerTag(tag: string, primitive: SymbolicPrimitive, vendor: ModelVendor, description: string): void { |
|
this.tagMappings.set(`${vendor}:${tag}`, { tag, primitive, vendor, description }); |
|
} |
|
|
|
|
|
|
|
|
|
public registerSlash(slash: string, primitive: SymbolicPrimitive, vendor: ModelVendor, description: string): void { |
|
this.slashMappings.set(`${vendor}:${slash}`, { slash, primitive, vendor, description }); |
|
} |
|
|
|
|
|
|
|
|
|
public registerDot(dot: string, primitive: SymbolicPrimitive, description: string): void { |
|
this.dotMappings.set(dot, { dot, primitive, description }); |
|
} |
|
|
|
|
|
|
|
|
|
public registerOperator(operator: string, primitive: SymbolicPrimitive, description: string): void { |
|
this.operatorMappings.set(operator, { operator, primitive, description }); |
|
} |
|
|
|
|
|
|
|
|
|
public registerCustomSymbol(operation: SymbolicOperation): void { |
|
this.customSymbols.set(operation.name, operation); |
|
} |
|
|
|
|
|
|
|
|
|
public getAllGlyphMappings(): GlyphMapping[] { |
|
return Array.from(this.glyphMappings.values()); |
|
} |
|
|
|
|
|
|
|
|
|
public getAllTagMappings(): TagMapping[] { |
|
return Array.from(this.tagMappings.values()); |
|
} |
|
|
|
|
|
|
|
|
|
public getAllSlashMappings(): SlashMapping[] { |
|
return Array.from(this.slashMappings.values()); |
|
} |
|
|
|
|
|
|
|
|
|
public getAllDotMappings(): DotMapping[] { |
|
return Array.from(this.dotMappings.values()); |
|
} |
|
|
|
|
|
|
|
|
|
public getAllOperatorMappings(): OperatorMapping[] { |
|
return Array.from(this.operatorMappings.values()); |
|
} |
|
|
|
|
|
|
|
|
|
public getPrimitiveByGlyph(glyph: string): SymbolicPrimitive | undefined { |
|
const mapping = this.glyphMappings.get(glyph); |
|
return mapping?.primitive; |
|
} |
|
|
|
|
|
|
|
|
|
public getPrimitiveByTag(tag: string, vendor: ModelVendor): SymbolicPrimitive | undefined { |
|
const mapping = this.tagMappings.get(`${vendor}:${tag}`); |
|
return mapping?.primitive; |
|
} |
|
|
|
|
|
|
|
|
|
public getPrimitiveBySlash(slash: string, vendor: ModelVendor): SymbolicPrimitive | undefined { |
|
const mapping = this.slashMappings.get(`${vendor}:${slash}`); |
|
return mapping?.primitive; |
|
} |
|
|
|
|
|
|
|
|
|
public getPrimitiveByDot(dot: string): SymbolicPrimitive | undefined { |
|
const mapping = this.dotMappings.get(dot); |
|
return mapping?.primitive; |
|
} |
|
|
|
|
|
|
|
|
|
public getPrimitiveByOperator(operator: string): SymbolicPrimitive | undefined { |
|
const mapping = this.operatorMappings.get(operator); |
|
return mapping?.primitive; |
|
} |
|
|
|
|
|
|
|
|
|
public getOperationByPrimitive(primitive: SymbolicPrimitive): SymbolicOperation | undefined { |
|
|
|
const builtIn = SYMBOLIC_RUNTIME_SCHEMA.find(op => op.type === primitive); |
|
if (builtIn) return builtIn; |
|
|
|
|
|
for (const operation of this.customSymbols.values()) { |
|
if (operation.type === primitive) return operation; |
|
} |
|
|
|
return undefined; |
|
} |
|
|
|
|
|
|
|
|
|
public getVendorImplementation(primitive: SymbolicPrimitive, vendor: ModelVendor): VendorImplementation | undefined { |
|
const operation = this.getOperationByPrimitive(primitive); |
|
if (!operation) return undefined; |
|
|
|
return operation.vendorImplementations.find(impl => impl.vendor === vendor); |
|
} |
|
|
|
|
|
|
|
|
|
public vendorSupports(vendor: ModelVendor, primitive: SymbolicPrimitive): boolean { |
|
const capabilities = this.vendorCapabilities.get(vendor); |
|
if (!capabilities) return false; |
|
|
|
return capabilities.has(primitive); |
|
} |
|
|
|
|
|
|
|
|
|
public getVendorSupportedPrimitives(vendor: ModelVendor): SymbolicPrimitive[] { |
|
const capabilities = this.vendorCapabilities.get(vendor); |
|
if (!capabilities) return []; |
|
|
|
return Array.from(capabilities); |
|
} |
|
|
|
|
|
|
|
|
|
public getPrimitiveSupportMap(primitive: SymbolicPrimitive): Record<ModelVendor, boolean> { |
|
const result: Record<ModelVendor, boolean> = {} as Record<ModelVendor, boolean>; |
|
|
|
for (const vendor of Object.values(ModelVendor)) { |
|
result[vendor] = this.vendorSupports(vendor, primitive); |
|
} |
|
|
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
public transformToVendor(universalSyntax: string, vendor: ModelVendor): string { |
|
|
|
const match = universalSyntax.match(/^(.+?)\{(.+)\}$/); |
|
if (!match) return universalSyntax; |
|
|
|
const [_, command, paramsString] = match; |
|
|
|
|
|
let params: Record<string, any> = {}; |
|
try { |
|
|
|
paramsString.split(',').forEach(pair => { |
|
const [key, value] = pair.split(':').map(s => s.trim()); |
|
params[key] = value; |
|
}); |
|
} catch (error) { |
|
return universalSyntax; |
|
} |
|
|
|
|
|
const primitive = this.getPrimitiveByDot(command); |
|
if (!primitive) return universalSyntax; |
|
|
|
|
|
const implementation = this.getVendorImplementation(primitive, vendor); |
|
if (!implementation) return universalSyntax; |
|
|
|
|
|
switch (implementation.style) { |
|
case GrammarStyle.XML_TAGS: |
|
if (implementation.prefix && implementation.suffix) { |
|
return `${implementation.prefix}${params.content || ''}${implementation.suffix}`; |
|
} |
|
break; |
|
case GrammarStyle.SLASH_COMMANDS: |
|
if (implementation.prefix) { |
|
return `${implementation.prefix} ${params.content || ''}`; |
|
} |
|
break; |
|
case GrammarStyle.API_PARAMETERS: |
|
|
|
return `API parameter: ${JSON.stringify(params)}`; |
|
case GrammarStyle.SYSTEM_PROMPTS: |
|
|
|
const operation = this.getOperationByPrimitive(primitive); |
|
return `System instruction for ${operation?.name || primitive}: ${params.content || ''}`; |
|
|
|
} |
|
|
|
return universalSyntax; |
|
} |
|
|
|
|
|
|
|
|
|
public transformToUniversal(vendorSyntax: string, vendor: ModelVendor): string { |
|
|
|
if (vendor === ModelVendor.ANTHROPIC || |
|
vendor === ModelVendor.UNIVERSAL) { |
|
const tagMatches = vendorSyntax.match(/<(\w+)>([\s\S]*?)<\/\1>/); |
|
if (tagMatches) { |
|
const [_, tag, content] = tagMatches; |
|
const primitive = this.getPrimitiveByTag(tag, vendor); |
|
if (primitive) { |
|
const operation = this.getOperationByPrimitive(primitive); |
|
if (operation) { |
|
return `${operation.universalSyntax.split('{')[0]}{content: "${content}"}`; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
if (vendor === ModelVendor.OPENAI || |
|
vendor === ModelVendor.QWEN || |
|
vendor === ModelVendor.UNIVERSAL) { |
|
const slashMatches = vendorSyntax.match(/^\/(\w+)\s+([\s\S]*)$/); |
|
if (slashMatches) { |
|
const [_, slash, content] = slashMatches; |
|
const primitive = this.getPrimitiveBySlash(slash, vendor); |
|
if (primitive) { |
|
const operation = this.getOperationByPrimitive(primitive); |
|
if (operation) { |
|
return `${operation.universalSyntax.split('{')[0]}{content: "${content}"}`; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
return vendorSyntax; |
|
} |
|
|
|
|
|
|
|
|
|
public parseSymbolicOperations(text: string, vendor: ModelVendor): Array<{ primitive: SymbolicPrimitive, params: any }> { |
|
const operations: Array<{ primitive: SymbolicPrimitive, params: any }> = []; |
|
|
|
|
|
switch (vendor) { |
|
case ModelVendor.ANTHROPIC: |
|
|
|
const tagRegex = /<(\w+)>([\s\S]*?)<\/\1>/g; |
|
let tagMatch; |
|
while ((tagMatch = tagRegex.exec(text)) !== null) { |
|
const tag = tagMatch[1]; |
|
const content = tagMatch[2]; |
|
const primitive = this.getPrimitiveByTag(tag, vendor); |
|
if (primitive) { |
|
operations.push({ |
|
primitive, |
|
params: { content } |
|
}); |
|
} |
|
} |
|
break; |
|
|
|
case ModelVendor.QWEN: |
|
case ModelVendor.OPENAI: |
|
|
|
const slashRegex = /\/(\w+)\s+([\s\S]*?)(?=\/\w+\s+|$)/g; |
|
let slashMatch; |
|
while ((slashMatch = slashRegex.exec(text)) !== null) { |
|
const slash = slashMatch[1]; |
|
const content = slashMatch[2].trim(); |
|
const primitive = this.getPrimitiveBySlash(slash, vendor); |
|
if (primitive) { |
|
operations.push({ |
|
primitive, |
|
params: { content } |
|
}); |
|
} |
|
} |
|
break; |
|
|
|
|
|
} |
|
|
|
return operations; |
|
} |
|
|
|
|
|
|
|
|
|
public generateSymbolicContent(operations: Array<{ primitive: SymbolicPrimitive, params: any }>, vendor: ModelVendor): string { |
|
let content = ''; |
|
|
|
for (const { primitive, params } of operations) { |
|
const implementation = this.getV |
|
|
|
|
|
|
|
public generateSymbolicContent(operations: Array<{ primitive: SymbolicPrimitive, params: any }>, vendor: ModelVendor): string { |
|
let content = ''; |
|
|
|
for (const { primitive, params } of operations) { |
|
const implementation = this.getVendorImplementation(primitive, vendor); |
|
if (!implementation) continue; |
|
|
|
switch (implementation.style) { |
|
case GrammarStyle.XML_TAGS: |
|
if (implementation.prefix && implementation.suffix) { |
|
content += `${implementation.prefix}${params.content || ''}${implementation.suffix}\n`; |
|
} |
|
break; |
|
|
|
case GrammarStyle.SLASH_COMMANDS: |
|
if (implementation.prefix) { |
|
content += `${implementation.prefix} ${params.content || ''}\n`; |
|
} |
|
break; |
|
|
|
case GrammarStyle.API_PARAMETERS: |
|
|
|
content += `[API Parameter] ${JSON.stringify(params)}\n`; |
|
break; |
|
|
|
case GrammarStyle.SYSTEM_PROMPTS: |
|
|
|
const operation = this.getOperationByPrimitive(primitive); |
|
content += `System instruction for ${operation?.name || primitive}: ${params.content || ''}\n`; |
|
break; |
|
|
|
case GrammarStyle.FUNCTION_CALLS: |
|
|
|
content += `${params.name || primitive}(${JSON.stringify(params.params || {})});\n`; |
|
break; |
|
|
|
case GrammarStyle.DOT_PREFIXED: |
|
|
|
const opName = primitive.toLowerCase(); |
|
let paramsStr = ''; |
|
if (params && Object.keys(params).length > 0) { |
|
paramsStr = JSON.stringify(params); |
|
} |
|
content += `.p/${opName}{${paramsStr}}\n`; |
|
break; |
|
|
|
case GrammarStyle.GLYPH_MARKERS: |
|
|
|
const glyphs = this.findGlyphsForPrimitive(primitive); |
|
if (glyphs.length > 0) { |
|
content += `${glyphs[0]} ${params.content || ''}\n`; |
|
} |
|
break; |
|
|
|
|
|
} |
|
} |
|
|
|
return content; |
|
} |
|
|
|
|
|
|
|
|
|
private findGlyphsForPrimitive(primitive: SymbolicPrimitive): string[] { |
|
const glyphs: string[] = []; |
|
|
|
for (const [glyph, mapping] of this.glyphMappings.entries()) { |
|
if (mapping.primitive === primitive) { |
|
glyphs.push(glyph); |
|
} |
|
} |
|
|
|
return glyphs; |
|
} |
|
|
|
|
|
|
|
|
|
public extractParameters(content: string, primitive: SymbolicPrimitive, vendor: ModelVendor): any { |
|
const implementation = this.getVendorImplementation(primitive, vendor); |
|
if (!implementation) return {}; |
|
|
|
switch (implementation.style) { |
|
case GrammarStyle.XML_TAGS: |
|
if (implementation.prefix && implementation.suffix) { |
|
const regex = new RegExp(`${escapeRegExp(implementation.prefix)}([\\s\\S]*?)${escapeRegExp(implementation.suffix)}`); |
|
const match = content.match(regex); |
|
if (match) { |
|
return { content: match[1] }; |
|
} |
|
} |
|
break; |
|
|
|
case GrammarStyle.SLASH_COMMANDS: |
|
if (implementation.prefix) { |
|
const regex = new RegExp(`${escapeRegExp(implementation.prefix)}\\s+([\\s\\S]*)`); |
|
const match = content.match(regex); |
|
if (match) { |
|
return { content: match[1] }; |
|
} |
|
} |
|
break; |
|
|
|
case GrammarStyle.FUNCTION_CALLS: |
|
const functionRegex = /(\w+)\(({[\s\S]*?})\)/; |
|
const functionMatch = content.match(functionRegex); |
|
if (functionMatch) { |
|
try { |
|
const name = functionMatch[1]; |
|
const params = JSON.parse(functionMatch[2]); |
|
return { name, params }; |
|
} catch (error) { |
|
return {}; |
|
} |
|
} |
|
break; |
|
|
|
|
|
} |
|
|
|
return {}; |
|
} |
|
|
|
|
|
|
|
|
|
public containsSymbolicOperation(content: string, primitive: SymbolicPrimitive, vendor: ModelVendor): boolean { |
|
const implementation = this.getVendorImplementation(primitive, vendor); |
|
if (!implementation) return false; |
|
|
|
switch (implementation.style) { |
|
case GrammarStyle.XML_TAGS: |
|
if (implementation.prefix && implementation.suffix) { |
|
const regex = new RegExp(`${escapeRegExp(implementation.prefix)}[\\s\\S]*?${escapeRegExp(implementation.suffix)}`); |
|
return regex.test(content); |
|
} |
|
break; |
|
|
|
case GrammarStyle.SLASH_COMMANDS: |
|
if (implementation.prefix) { |
|
const regex = new RegExp(`${escapeRegExp(implementation.prefix)}\\s+`); |
|
return regex.test(content); |
|
} |
|
break; |
|
|
|
case GrammarStyle.FUNCTION_CALLS: |
|
const functionRegex = /\w+\(.*?\)/; |
|
return functionRegex.test(content); |
|
|
|
|
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
public extractAllSymbolicOperations(content: string, vendor: ModelVendor): Array<{ primitive: SymbolicPrimitive, params: any }> { |
|
const operations: Array<{ primitive: SymbolicPrimitive, params: any }> = []; |
|
|
|
|
|
const supportedPrimitives = this.getVendorSupportedPrimitives(vendor); |
|
|
|
for (const primitive of supportedPrimitives) { |
|
if (this.containsSymbolicOperation(content, primitive, vendor)) { |
|
const params = this.extractParameters(content, primitive, vendor); |
|
operations.push({ primitive, params }); |
|
} |
|
} |
|
|
|
return operations; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
public findSymbolicResidue(content: string, vendor: ModelVendor): Array<{ pattern: string, position: number, possiblePrimitive?: SymbolicPrimitive }> { |
|
const residue: Array<{ pattern: string, position: number, possiblePrimitive?: SymbolicPrimitive }> = []; |
|
|
|
switch (vendor) { |
|
case ModelVendor.ANTHROPIC: |
|
|
|
const unclosedTagRegex = /<(\w+)>(?![^<]*<\/\1>)/g; |
|
let unclosedMatch; |
|
while ((unclosedMatch = unclosedTagRegex.exec(content)) !== null) { |
|
const tag = unclosedMatch[1]; |
|
const primitive = this.getPrimitiveByTag(tag, vendor); |
|
residue.push({ |
|
pattern: unclosedMatch[0], |
|
position: unclosedMatch.index, |
|
possiblePrimitive: primitive |
|
}); |
|
} |
|
break; |
|
|
|
case ModelVendor.QWEN: |
|
case ModelVendor.OPENAI: |
|
|
|
const incompleteSlashRegex = /\/(\w+)$/g; |
|
let incompleteSlashMatch; |
|
while ((incompleteSlashMatch = incompleteSlashRegex.exec(content)) !== null) { |
|
const slash = incompleteSlashMatch[1]; |
|
const primitive = this.getPrimitiveBySlash(slash, vendor); |
|
residue.push({ |
|
pattern: incompleteSlashMatch[0], |
|
position: incompleteSlashMatch.index, |
|
possiblePrimitive: primitive |
|
}); |
|
} |
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
for (const [glyph, mapping] of this.glyphMappings.entries()) { |
|
const glyphRegex = new RegExp(escapeRegExp(glyph), 'g'); |
|
let glyphMatch; |
|
while ((glyphMatch = glyphRegex.exec(content)) !== null) { |
|
|
|
const isIsolated = ( |
|
(glyphMatch.index === 0 || /\s/.test(content[glyphMatch.index - 1])) && |
|
(glyphMatch.index + glyph.length === content.length || /\s/.test(content[glyphMatch.index + glyph.length])) |
|
); |
|
|
|
if (isIsolated) { |
|
residue.push({ |
|
pattern: glyph, |
|
position: glyphMatch.index, |
|
possiblePrimitive: mapping.primitive |
|
}); |
|
} |
|
} |
|
} |
|
|
|
return residue; |
|
} |
|
|
|
|
|
|
|
|
|
public repairSymbolicResidue(content: string, vendor: ModelVendor): string { |
|
let repairedContent = content; |
|
|
|
|
|
const residuePatterns = this.findSymbolicResidue(content, vendor); |
|
|
|
|
|
residuePatterns.sort((a, b) => b.position - a.position); |
|
|
|
for (const residue of residuePatterns) { |
|
if (!residue.possiblePrimitive) continue; |
|
|
|
const implementation = this.getVendorImplementation(residue.possiblePrimitive, vendor); |
|
if (!implementation) continue; |
|
|
|
switch (implementation.style) { |
|
case GrammarStyle.XML_TAGS: |
|
if (implementation.prefix && implementation.suffix) { |
|
|
|
if (residue.pattern.startsWith(implementation.prefix)) { |
|
repairedContent = |
|
repairedContent.substring(0, residue.position) + |
|
residue.pattern + '[MISSING CONTENT]' + implementation.suffix + |
|
repairedContent.substring(residue.position + residue.pattern.length); |
|
} |
|
} |
|
break; |
|
|
|
case GrammarStyle.SLASH_COMMANDS: |
|
|
|
if (residue.pattern.startsWith('/')) { |
|
repairedContent = |
|
repairedContent.substring(0, residue.position) + |
|
residue.pattern + ' [MISSING CONTENT]' + |
|
repairedContent.substring(residue.position + residue.pattern.length); |
|
} |
|
break; |
|
|
|
|
|
} |
|
} |
|
|
|
return repairedContent; |
|
} |
|
|
|
|
|
|
|
|
|
public cleanSymbolicTraces(content: string, vendor: ModelVendor): string { |
|
let cleanedContent = content; |
|
|
|
|
|
const primitivesToClean = [ |
|
SymbolicPrimitive.THINKING, |
|
SymbolicPrimitive.REFLECTION, |
|
|
|
]; |
|
|
|
for (const primitive of primitivesToClean) { |
|
const implementation = this.getVendorImplementation(primitive, vendor); |
|
if (!implementation) continue; |
|
|
|
switch (implementation.style) { |
|
case GrammarStyle.XML_TAGS: |
|
if (implementation.prefix && implementation.suffix) { |
|
const regex = new RegExp(`${escapeRegExp(implementation.prefix)}[\\s\\S]*?${escapeRegExp(implementation.suffix)}`, 'g'); |
|
cleanedContent = cleanedContent.replace(regex, ''); |
|
} |
|
break; |
|
|
|
case GrammarStyle.SLASH_COMMANDS: |
|
if (implementation.prefix) { |
|
const regex = new RegExp(`${escapeRegExp(implementation.prefix)}\\s+[^\\n]*\\n?`, 'g'); |
|
cleanedContent = cleanedContent.replace(regex, ''); |
|
} |
|
break; |
|
|
|
|
|
} |
|
} |
|
|
|
|
|
for (const [glyph, mapping] of this.glyphMappings.entries()) { |
|
if (primitivesToClean.includes(mapping.primitive)) { |
|
const glyphRegex = new RegExp(`${escapeRegExp(glyph)}\\s+[^\\n]*\\n?`, 'g'); |
|
cleanedContent = cleanedContent.replace(glyphRegex, ''); |
|
} |
|
} |
|
|
|
return cleanedContent.trim(); |
|
} |
|
|
|
|
|
|
|
|
|
public getVendorCapabilitiesInfo(vendor: ModelVendor): { |
|
supported: SymbolicPrimitive[]; |
|
emulated: SymbolicPrimitive[]; |
|
unsupported: SymbolicPrimitive[]; |
|
grammarStyle: GrammarStyle[]; |
|
} { |
|
const supported: SymbolicPrimitive[] = []; |
|
const emulated: SymbolicPrimitive[] = []; |
|
const unsupported: SymbolicPrimitive[] = []; |
|
const grammarStyle = new Set<GrammarStyle>(); |
|
|
|
|
|
for (const operation of SYMBOLIC_RUNTIME_SCHEMA) { |
|
const implementation = operation.vendorImplementations.find(impl => impl.vendor === vendor); |
|
|
|
if (implementation) { |
|
grammarStyle.add(implementation.style); |
|
|
|
if (implementation.isNative) { |
|
supported.push(operation.type); |
|
} else { |
|
emulated.push(operation.type); |
|
} |
|
} else { |
|
unsupported.push(operation.type); |
|
} |
|
} |
|
|
|
return { |
|
supported, |
|
emulated, |
|
unsupported, |
|
grammarStyle: Array.from(grammarStyle) |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
public getCompatibilityMatrix(): Record<SymbolicPrimitive, Record<ModelVendor, 'native' | 'emulated' | 'unsupported'>> { |
|
const matrix: Record<SymbolicPrimitive, Record<ModelVendor, 'native' | 'emulated' | 'unsupported'>> = {} as any; |
|
|
|
|
|
for (const primitive of Object.values(SymbolicPrimitive)) { |
|
matrix[primitive] = {} as Record<ModelVendor, 'native' | 'emulated' | 'unsupported'>; |
|
|
|
for (const vendor of Object.values(ModelVendor)) { |
|
matrix[primitive][vendor] = 'unsupported'; |
|
} |
|
} |
|
|
|
|
|
for (const operation of SYMBOLIC_RUNTIME_SCHEMA) { |
|
for (const implementation of operation.vendorImplementations) { |
|
if (implementation.isNative) { |
|
matrix[operation.type][implementation.vendor] = 'native'; |
|
} else { |
|
matrix[operation.type][implementation.vendor] = 'emulated'; |
|
} |
|
} |
|
} |
|
|
|
return matrix; |
|
} |
|
|
|
|
|
|
|
|
|
public getSymbolicMappingTable(): Record<SymbolicPrimitive, Record<ModelVendor, string>> { |
|
const table: Record<SymbolicPrimitive, Record<ModelVendor, string>> = {} as any; |
|
|
|
|
|
for (const primitive of Object.values(SymbolicPrimitive)) { |
|
table[primitive] = {} as Record<ModelVendor, string>; |
|
|
|
for (const vendor of Object.values(ModelVendor)) { |
|
table[primitive][vendor] = 'N/A'; |
|
} |
|
} |
|
|
|
|
|
for (const operation of SYMBOLIC_RUNTIME_SCHEMA) { |
|
|
|
table[operation.type][ModelVendor.UNIVERSAL] = operation.universalSyntax; |
|
|
|
for (const implementation of operation.vendorImplementations) { |
|
table[operation.type][implementation.vendor] = implementation.exampleSyntax; |
|
} |
|
} |
|
|
|
return table; |
|
} |
|
|
|
|
|
|
|
|
|
public exportRegistryData(): any { |
|
return { |
|
glyphMappings: Array.from(this.glyphMappings.values()), |
|
tagMappings: Array.from(this.tagMappings.values()), |
|
slashMappings: Array.from(this.slashMappings.values()), |
|
dotMappings: Array.from(this.dotMappings.values()), |
|
operatorMappings: Array.from(this.operatorMappings.values()), |
|
vendorCapabilities: Array.from(this.vendorCapabilities.entries()).map(([vendor, capabilities]) => ({ |
|
vendor, |
|
capabilities: Array.from(capabilities) |
|
})), |
|
customSymbols: Array.from(this.customSymbols.values()), |
|
compatibilityMatrix: this.getCompatibilityMatrix(), |
|
mappingTable: this.getSymbolicMappingTable() |
|
}; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
function escapeRegExp(string: string): string { |
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
|
} |
|
|