universal-runtime / implementation.ts
recursivelabs's picture
Upload 17 files
096d5ee verified
/**
* 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<string, ParamDefinition>;
}
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<string, SymbolicPrimitive> = {
think: {
name: 'think',
description: 'Enables explicit reasoning trace and step-by-step thinking',
implementations: {
universal: '.p/think{content}',
claude: '<think>content</think>',
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: '<reflect>target</reflect>',
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: '<tool>name(params)</tool>',
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: '<s>directive</s>',
openai: 'system message',
qwen: '<<SYS>> 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<ThinkResult> {
this.telemetry.track('think');
return this.adapter.executeThink(content, options);
}
/**
* Execute a reflection operation
*/
async reflect(target: string, options: { depth?: number } = {}): Promise<ReflectResult> {
this.telemetry.track('reflect');
return this.adapter.executeReflect(target, options);
}
/**
* Execute a fork operation
*/
async fork(paths: string[], options: { weights?: number[] } = {}): Promise<ForkResult> {
this.telemetry.track('fork');
return this.adapter.executeFork(paths, options);
}
/**
* Execute a collapse operation
*/
async collapse(options: { trigger?: string; threshold?: number } = {}): Promise<CollapseResult> {
this.telemetry.track('collapse');
return this.adapter.executeCollapse(options);
}
/**
* Execute an attention operation
*/
async attention(focus: string, options: { weight?: number } = {}): Promise<AttentionResult> {
this.telemetry.track('attention');
return this.adapter.executeAttention(focus, options);
}
/**
* Execute a tool operation
*/
async tool(name: string, params?: Record<string, any>): Promise<ToolResult> {
this.telemetry.track('tool');
return this.adapter.executeTool(name, params);
}
/**
* Execute a system operation
*/
async system(directive: string): Promise<SystemResult> {
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<ThinkResult>;
abstract executeReflect(target: string, options?: any): Promise<ReflectResult>;
abstract executeFork(paths: string[], options?: any): Promise<ForkResult>;
abstract executeCollapse(options?: any): Promise<CollapseResult>;
abstract executeAttention(focus: string, options?: any): Promise<AttentionResult>;
abstract executeTool(name: string, params?: Record<string, any>): Promise<ToolResult>;
abstract executeSystem(directive: string): Promise<SystemResult>;
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<ThinkResult> {
// Implementation with Claude's <think> tags
const thinking = content || '';
const prompt = `<think>${thinking}</think>`;
// 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<ReflectResult> {
// Implementation with Claude's <reflect> tags
const prompt = `<reflect>${target}</reflect>`;
// 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<ForkResult> {
// 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<string, string> = {};
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<CollapseResult> {
// 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<AttentionResult> {
// 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<string, any>): Promise<ToolResult> {
// Implementation with Claude's <tool> tags
let paramsString = '';
if (params) {
paramsString = JSON.stringify(params);
}
const prompt = `<tool>${name}(${paramsString})</tool>`;
// Make API call to Claude
const response = await this.callApi(prompt);
return {
name,
result: this.extractToolResult(response, name)
};
}
async executeSystem(directive: string): Promise<SystemResult> {
// Implementation with Claude's <s> tags
const prompt = `<s>${directive}</s>`;
// Make API call to Claude
const response = await this.callApi(prompt);
return {
applied: true,
result: response
};
}
parseSymbolicOperations(content: string): SymbolicOperation[] {
const operations: SymbolicOperation[] = [];
// Parse <think> tags
const thinkMatches = content.match(/<think>([\s\S]*?)<\/think>/g);
if (thinkMatches) {
for (const match of thinkMatches) {
const thinkContent = match.replace(/<think>|<\/think>/g, '');
operations.push({
type: 'think',
content: thinkContent
});
}
}
// Parse <reflect> tags
const reflectMatches = content.match(/<reflect>([\s\S]*?)<\/reflect>/g);
if (reflectMatches) {
for (const match of reflectMatches) {
const reflectContent = match.replace(/<reflect>|<\/reflect>/g, '');
operations.push({
type: 'reflect',
target: reflectContent
});
}
}
// Parse <tool> tags
const toolMatches = content.match(/<tool>([\s\S]*?)<\/tool>/g);
if (toolMatches) {
for (const match of toolMatches) {
const toolContent = match.replace(/<tool>|<\/tool>/g, '');
const nameMatch = toolContent.match(/^(\w+)/);
if (nameMatch) {
const name = nameMatch[1];
const paramsMatch = toolContent.match(/\(([\s\S]*)\)/);
let params: Record<string, any> | undefined;
if (paramsMatch) {
try {
params = JSON.parse(paramsMatch[1]);
} catch {
params = { raw: paramsMatch[1] };
}
}
operations.push({
type: 'tool',
name,
params
});
}
}
}
// Parse <s> tags
const systemMatches = content.match(/<s>([\s\S]*?)<\/s>/g);
if (systemMatches) {
for (const match of systemMatches) {
const systemContent = match.replace(/<s>|<\/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 += `<think>${operation.content || ''}</think>\n`;
break;
case 'reflect':
content += `<reflect>${operation.target}</reflect>\n`;
break;
case 'tool':
const paramsString = operation.params ? JSON.stringify(operation.params) : '';
content += `<tool>${operation.name}(${paramsString})</tool>\n`;
break;
case 'system':
content += `<s>${operation.directive}</s>\n`;
break;
case