/** * universal-symbolics * * A unified runtime layer for symbolic operations across frontier LLMs. * This implementation transpiles symbolic primitives across vendor syntaxes. */ // Core symbolic primitives and their implementation across vendors export interface SymbolicPrimitive { name: string; description: string; implementations: { universal: string; claude?: string; openai?: string; qwen?: string; gemini?: string; deepseek?: string; local?: string; }; paramSchema?: Record; } export interface ParamDefinition { type: 'string' | 'boolean' | 'number' | 'object' | 'array'; description: string; required?: boolean; default?: any; } // Base definition of all vendor models export type ModelVendor = 'claude' | 'openai' | 'qwen' | 'gemini' | 'deepseek' | 'local'; // Core symbolic operation registry export const SYMBOLIC_PRIMITIVES: Record = { think: { name: 'think', description: 'Enables explicit reasoning trace and step-by-step thinking', implementations: { universal: '.p/think{content}', claude: 'content', openai: 'tool_choice parameter', qwen: '/think content', gemini: 'Implicit via system prompt', deepseek: 'Rational thinking mode' }, paramSchema: { content: { type: 'string', description: 'The thinking content', required: false }, trace: { type: 'boolean', description: 'Whether to include thinking trace in response', default: true }, depth: { type: 'number', description: 'Depth of thinking (if supported)', default: 1 } } }, reflect: { name: 'reflect', description: 'Self-reference and examination of reasoning processes', implementations: { universal: '.p/reflect{target}', claude: 'target', openai: 'Chain-of-thought prompting', qwen: 'Internally supported', gemini: 'Reasoning templates' }, paramSchema: { target: { type: 'string', description: 'What to reflect on', required: true }, depth: { type: 'number', description: 'Depth of reflection', default: 1 } } }, fork: { name: 'fork', description: 'Create parallel reasoning paths to explore multiple options', implementations: { universal: '.p/fork{paths}', claude: 'Emulated via parsing', openai: 'Emulated via parsing', qwen: 'Emulated via parsing', gemini: 'Emulated via parsing', deepseek: 'Emulated via parsing' }, paramSchema: { paths: { type: 'array', description: 'Different paths to explore', required: true }, weights: { type: 'array', description: 'Weights for each path', required: false } } }, collapse: { name: 'collapse', description: 'Handle errors and recover from failures', implementations: { universal: '.p/collapse{trigger}', claude: 'Emulated via error handling', openai: 'Emulated via error handling', qwen: 'Emulated via error handling', gemini: 'Emulated via error handling', deepseek: 'Emulated via error handling' }, paramSchema: { trigger: { type: 'string', description: 'What triggers the collapse', required: false }, threshold: { type: 'number', description: 'Threshold for collapse', default: 0.5 } } }, attention: { name: 'attention', description: 'Control focus on specific content', implementations: { universal: '.p/attention{focus}', claude: 'Emulated via prompting', openai: 'Emulated via prompting', qwen: 'Emulated via prompting', gemini: 'Emulated via prompting', deepseek: 'Emulated via prompting' }, paramSchema: { focus: { type: 'string', description: 'What to focus attention on', required: true }, weight: { type: 'number', description: 'Weight of attention', default: 1.0 } } }, tool: { name: 'tool', description: 'Invoke tools or functions', implementations: { universal: '.p/tool{name, params}', claude: 'name(params)', openai: 'function_call API', qwen: 'tool_use format', gemini: 'function_calling API', deepseek: 'function_calling API' }, paramSchema: { name: { type: 'string', description: 'Tool name', required: true }, params: { type: 'object', description: 'Tool parameters', required: false } } }, system: { name: 'system', description: 'System-level directives', implementations: { universal: '.p/system{directive}', claude: 'directive', openai: 'system message', qwen: '<> directive', gemini: 'system instruction', deepseek: 'system instruction' }, paramSchema: { directive: { type: 'string', description: 'System directive', required: true } } } }; /** * Core Universal Symbolics Runtime * Provides a unified interface for symbolic operations across all vendors */ export class UniversalSymbolics { private adapter: SymbolicAdapter; private telemetry: SymbolicTelemetry; constructor(options: { vendor?: ModelVendor; apiKey?: string; endpoint?: string; enableTelemetry?: boolean; } = {}) { const vendor = options.vendor || 'claude'; this.adapter = this.createAdapter(vendor, options); this.telemetry = new SymbolicTelemetry({ enabled: options.enableTelemetry ?? true }); } /** * Factory method for creating adapters */ private createAdapter(vendor: ModelVendor, options: any): SymbolicAdapter { switch (vendor) { case 'claude': return new ClaudeAdapter(options); case 'openai': return new OpenAIAdapter(options); case 'qwen': return new QwenAdapter(options); case 'gemini': return new GeminiAdapter(options); case 'deepseek': return new DeepSeekAdapter(options); case 'local': return new LocalAdapter(options); default: throw new Error(`Unsupported vendor: ${vendor}`); } } /** * Change the vendor */ setVendor(vendor: ModelVendor) { this.adapter = this.createAdapter(vendor, { apiKey: this.adapter.getApiKey(), endpoint: this.adapter.getEndpoint() }); } /** * Execute a thinking operation */ async think(content?: string, options: { trace?: boolean; depth?: number } = {}): Promise { this.telemetry.track('think'); return this.adapter.executeThink(content, options); } /** * Execute a reflection operation */ async reflect(target: string, options: { depth?: number } = {}): Promise { this.telemetry.track('reflect'); return this.adapter.executeReflect(target, options); } /** * Execute a fork operation */ async fork(paths: string[], options: { weights?: number[] } = {}): Promise { this.telemetry.track('fork'); return this.adapter.executeFork(paths, options); } /** * Execute a collapse operation */ async collapse(options: { trigger?: string; threshold?: number } = {}): Promise { this.telemetry.track('collapse'); return this.adapter.executeCollapse(options); } /** * Execute an attention operation */ async attention(focus: string, options: { weight?: number } = {}): Promise { this.telemetry.track('attention'); return this.adapter.executeAttention(focus, options); } /** * Execute a tool operation */ async tool(name: string, params?: Record): Promise { this.telemetry.track('tool'); return this.adapter.executeTool(name, params); } /** * Execute a system operation */ async system(directive: string): Promise { this.telemetry.track('system'); return this.adapter.executeSystem(directive); } /** * Translate symbolic operations between vendors */ translate(content: string, sourceVendor: ModelVendor, targetVendor: ModelVendor): string { this.telemetry.track('translate'); // Create source adapter const sourceAdapter = this.createAdapter(sourceVendor, {}); // Create target adapter const targetAdapter = this.createAdapter(targetVendor, {}); // Parse source content const operations = sourceAdapter.parseSymbolicOperations(content); // Translate to target content return targetAdapter.generateSymbolicContent(operations); } /** * Detect symbolic residue in response */ detectResidue(response: string, vendor?: ModelVendor): SymbolicResidue[] { const actualVendor = vendor || this.adapter.getVendor(); return SymbolicResidueDetector.detect(response, actualVendor); } /** * Clean symbolic residue from response */ cleanResidue(response: string, vendor?: ModelVendor): string { const actualVendor = vendor || this.adapter.getVendor(); return SymbolicResidueDetector.clean(response, actualVendor); } /** * Get telemetry data */ getTelemetry(): TelemetryData { return this.telemetry.getData(); } } /** * Abstract base class for vendor-specific adapters */ abstract class SymbolicAdapter { protected apiKey?: string; protected endpoint?: string; constructor(options: { apiKey?: string; endpoint?: string }) { this.apiKey = options.apiKey; this.endpoint = options.endpoint; } abstract getVendor(): ModelVendor; getApiKey(): string | undefined { return this.apiKey; } getEndpoint(): string | undefined { return this.endpoint; } abstract executeThink(content?: string, options?: any): Promise; abstract executeReflect(target: string, options?: any): Promise; abstract executeFork(paths: string[], options?: any): Promise; abstract executeCollapse(options?: any): Promise; abstract executeAttention(focus: string, options?: any): Promise; abstract executeTool(name: string, params?: Record): Promise; abstract executeSystem(directive: string): Promise; abstract parseSymbolicOperations(content: string): SymbolicOperation[]; abstract generateSymbolicContent(operations: SymbolicOperation[]): string; } /** * Claude-specific adapter */ class ClaudeAdapter extends SymbolicAdapter { getVendor(): ModelVendor { return 'claude'; } async executeThink(content?: string, options: { trace?: boolean; depth?: number } = {}): Promise { // Implementation with Claude's tags const thinking = content || ''; const prompt = `${thinking}`; // Make API call to Claude const response = await this.callApi(prompt); return { thinking: this.extractThinking(response), output: this.extractOutput(response) }; } async executeReflect(target: string, options: { depth?: number } = {}): Promise { // Implementation with Claude's tags const prompt = `${target}`; // Make API call to Claude const response = await this.callApi(prompt); return { reflection: this.extractReflection(response), output: this.extractOutput(response) }; } async executeFork(paths: string[], options: { weights?: number[] } = {}): Promise { // Emulated implementation for Claude const pathPrompts = paths.map(path => `Path: ${path}`).join('\n\n'); const prompt = `Consider the following paths:\n\n${pathPrompts}\n\nExplore each path and return the results.`; // Make API call to Claude const response = await this.callApi(prompt); // Parse response to extract results for each path const results: Record = {}; for (const path of paths) { results[path] = this.extractPathResult(response, path); } return { paths: results, selected: this.determineSelectedPath(response, paths) }; } async executeCollapse(options: { trigger?: string; threshold?: number } = {}): Promise { // Emulated implementation for Claude const trigger = options.trigger || 'error'; const prompt = `Handle the following error scenario: ${trigger}`; // Make API call to Claude const response = await this.callApi(prompt); return { handled: true, recovery: response }; } async executeAttention(focus: string, options: { weight?: number } = {}): Promise { // Emulated implementation for Claude const weight = options.weight || 1.0; const prompt = `Focus specifically on: ${focus}`; // Make API call to Claude const response = await this.callApi(prompt); return { focused: true, result: response }; } async executeTool(name: string, params?: Record): Promise { // Implementation with Claude's tags let paramsString = ''; if (params) { paramsString = JSON.stringify(params); } const prompt = `${name}(${paramsString})`; // Make API call to Claude const response = await this.callApi(prompt); return { name, result: this.extractToolResult(response, name) }; } async executeSystem(directive: string): Promise { // Implementation with Claude's tags const prompt = `${directive}`; // Make API call to Claude const response = await this.callApi(prompt); return { applied: true, result: response }; } parseSymbolicOperations(content: string): SymbolicOperation[] { const operations: SymbolicOperation[] = []; // Parse tags const thinkMatches = content.match(/([\s\S]*?)<\/think>/g); if (thinkMatches) { for (const match of thinkMatches) { const thinkContent = match.replace(/|<\/think>/g, ''); operations.push({ type: 'think', content: thinkContent }); } } // Parse tags const reflectMatches = content.match(/([\s\S]*?)<\/reflect>/g); if (reflectMatches) { for (const match of reflectMatches) { const reflectContent = match.replace(/|<\/reflect>/g, ''); operations.push({ type: 'reflect', target: reflectContent }); } } // Parse tags const toolMatches = content.match(/([\s\S]*?)<\/tool>/g); if (toolMatches) { for (const match of toolMatches) { const toolContent = match.replace(/|<\/tool>/g, ''); const nameMatch = toolContent.match(/^(\w+)/); if (nameMatch) { const name = nameMatch[1]; const paramsMatch = toolContent.match(/\(([\s\S]*)\)/); let params: Record | undefined; if (paramsMatch) { try { params = JSON.parse(paramsMatch[1]); } catch { params = { raw: paramsMatch[1] }; } } operations.push({ type: 'tool', name, params }); } } } // Parse tags const systemMatches = content.match(/([\s\S]*?)<\/s>/g); if (systemMatches) { for (const match of systemMatches) { const systemContent = match.replace(/|<\/s>/g, ''); operations.push({ type: 'system', directive: systemContent }); } } return operations; } generateSymbolicContent(operations: SymbolicOperation[]): string { let content = ''; for (const operation of operations) { switch (operation.type) { case 'think': content += `${operation.content || ''}\n`; break; case 'reflect': content += `${operation.target}\n`; break; case 'tool': const paramsString = operation.params ? JSON.stringify(operation.params) : ''; content += `${operation.name}(${paramsString})\n`; break; case 'system': content += `${operation.directive}\n`; break; case