| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | 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; |
| | } |
| |
|
| | |
| | export type ModelVendor = 'claude' | 'openai' | 'qwen' | 'gemini' | 'deepseek' | 'local'; |
| |
|
| | |
| | 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 |
| | } |
| | } |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | 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 |
| | }); |
| | } |
| | |
| | |
| | |
| | |
| | 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}`); |
| | } |
| | } |
| | |
| | |
| | |
| | |
| | setVendor(vendor: ModelVendor) { |
| | this.adapter = this.createAdapter(vendor, { |
| | apiKey: this.adapter.getApiKey(), |
| | endpoint: this.adapter.getEndpoint() |
| | }); |
| | } |
| | |
| | |
| | |
| | |
| | async think(content?: string, options: { trace?: boolean; depth?: number } = {}): Promise<ThinkResult> { |
| | this.telemetry.track('think'); |
| | return this.adapter.executeThink(content, options); |
| | } |
| | |
| | |
| | |
| | |
| | async reflect(target: string, options: { depth?: number } = {}): Promise<ReflectResult> { |
| | this.telemetry.track('reflect'); |
| | return this.adapter.executeReflect(target, options); |
| | } |
| | |
| | |
| | |
| | |
| | async fork(paths: string[], options: { weights?: number[] } = {}): Promise<ForkResult> { |
| | this.telemetry.track('fork'); |
| | return this.adapter.executeFork(paths, options); |
| | } |
| | |
| | |
| | |
| | |
| | async collapse(options: { trigger?: string; threshold?: number } = {}): Promise<CollapseResult> { |
| | this.telemetry.track('collapse'); |
| | return this.adapter.executeCollapse(options); |
| | } |
| | |
| | |
| | |
| | |
| | async attention(focus: string, options: { weight?: number } = {}): Promise<AttentionResult> { |
| | this.telemetry.track('attention'); |
| | return this.adapter.executeAttention(focus, options); |
| | } |
| | |
| | |
| | |
| | |
| | async tool(name: string, params?: Record<string, any>): Promise<ToolResult> { |
| | this.telemetry.track('tool'); |
| | return this.adapter.executeTool(name, params); |
| | } |
| | |
| | |
| | |
| | |
| | async system(directive: string): Promise<SystemResult> { |
| | this.telemetry.track('system'); |
| | return this.adapter.executeSystem(directive); |
| | } |
| | |
| | |
| | |
| | |
| | translate(content: string, sourceVendor: ModelVendor, targetVendor: ModelVendor): string { |
| | this.telemetry.track('translate'); |
| | |
| | |
| | const sourceAdapter = this.createAdapter(sourceVendor, {}); |
| | |
| | |
| | const targetAdapter = this.createAdapter(targetVendor, {}); |
| | |
| | |
| | const operations = sourceAdapter.parseSymbolicOperations(content); |
| | |
| | |
| | return targetAdapter.generateSymbolicContent(operations); |
| | } |
| | |
| | |
| | |
| | |
| | detectResidue(response: string, vendor?: ModelVendor): SymbolicResidue[] { |
| | const actualVendor = vendor || this.adapter.getVendor(); |
| | |
| | return SymbolicResidueDetector.detect(response, actualVendor); |
| | } |
| | |
| | |
| | |
| | |
| | cleanResidue(response: string, vendor?: ModelVendor): string { |
| | const actualVendor = vendor || this.adapter.getVendor(); |
| | |
| | return SymbolicResidueDetector.clean(response, actualVendor); |
| | } |
| | |
| | |
| | |
| | |
| | getTelemetry(): TelemetryData { |
| | return this.telemetry.getData(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | 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; |
| | } |
| |
|
| | |
| | |
| | |
| | class ClaudeAdapter extends SymbolicAdapter { |
| | getVendor(): ModelVendor { |
| | return 'claude'; |
| | } |
| | |
| | async executeThink(content?: string, options: { trace?: boolean; depth?: number } = {}): Promise<ThinkResult> { |
| | |
| | const thinking = content || ''; |
| | const prompt = `<think>${thinking}</think>`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | return { |
| | thinking: this.extractThinking(response), |
| | output: this.extractOutput(response) |
| | }; |
| | } |
| | |
| | async executeReflect(target: string, options: { depth?: number } = {}): Promise<ReflectResult> { |
| | |
| | const prompt = `<reflect>${target}</reflect>`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | return { |
| | reflection: this.extractReflection(response), |
| | output: this.extractOutput(response) |
| | }; |
| | } |
| | |
| | async executeFork(paths: string[], options: { weights?: number[] } = {}): Promise<ForkResult> { |
| | |
| | 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.`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | |
| | 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> { |
| | |
| | const trigger = options.trigger || 'error'; |
| | const prompt = `Handle the following error scenario: ${trigger}`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | return { |
| | handled: true, |
| | recovery: response |
| | }; |
| | } |
| | |
| | async executeAttention(focus: string, options: { weight?: number } = {}): Promise<AttentionResult> { |
| | |
| | const weight = options.weight || 1.0; |
| | const prompt = `Focus specifically on: ${focus}`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | return { |
| | focused: true, |
| | result: response |
| | }; |
| | } |
| | |
| | async executeTool(name: string, params?: Record<string, any>): Promise<ToolResult> { |
| | |
| | let paramsString = ''; |
| | if (params) { |
| | paramsString = JSON.stringify(params); |
| | } |
| | |
| | const prompt = `<tool>${name}(${paramsString})</tool>`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | return { |
| | name, |
| | result: this.extractToolResult(response, name) |
| | }; |
| | } |
| | |
| | async executeSystem(directive: string): Promise<SystemResult> { |
| | |
| | const prompt = `<s>${directive}</s>`; |
| | |
| | |
| | const response = await this.callApi(prompt); |
| | |
| | return { |
| | applied: true, |
| | result: response |
| | }; |
| | } |
| | |
| | parseSymbolicOperations(content: string): SymbolicOperation[] { |
| | const operations: SymbolicOperation[] = []; |
| | |
| | |
| | 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 |
| | }); |
| | } |
| | } |
| | |
| | |
| | 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 |
| | }); |
| | } |
| | } |
| | |
| | |
| | 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 |
| | }); |
| | } |
| | } |
| | } |
| | |
| | |
| | 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 |
| |
|