File size: 5,597 Bytes
1978456 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
// universal-developer/src/adapters/base.ts
export interface SymbolicCommand {
name: string;
description: string;
aliases?: string[];
parameters?: {
name: string;
description: string;
required?: boolean;
default?: any;
}[];
transform: (prompt: string, options: any) => Promise<TransformedPrompt>;
}
export interface TransformedPrompt {
systemPrompt?: string;
userPrompt: string;
modelParameters?: Record<string, any>;
}
export abstract class ModelAdapter {
protected commands: Map<string, SymbolicCommand> = new Map();
protected aliasMap: Map<string, string> = new Map();
constructor(protected apiKey: string, protected options: any = {}) {
this.registerCoreCommands();
}
protected registerCoreCommands() {
this.registerCommand({
name: 'think',
description: 'Activate extended reasoning pathways',
transform: this.transformThink.bind(this)
});
this.registerCommand({
name: 'fast',
description: 'Optimize for low-latency responses',
transform: this.transformFast.bind(this)
});
this.registerCommand({
name: 'loop',
description: 'Enable iterative refinement cycles',
parameters: [
{
name: 'iterations',
description: 'Number of refinement iterations',
required: false,
default: 3
}
],
transform: this.transformLoop.bind(this)
});
this.registerCommand({
name: 'reflect',
description: 'Trigger meta-analysis of outputs',
transform: this.transformReflect.bind(this)
});
this.registerCommand({
name: 'collapse',
description: 'Return to default behavior',
transform: this.transformCollapse.bind(this)
});
this.registerCommand({
name: 'fork',
description: 'Generate multiple alternative responses',
parameters: [
{
name: 'count',
description: 'Number of alternatives to generate',
required: false,
default: 2
}
],
transform: this.transformFork.bind(this)
});
}
public registerCommand(command: SymbolicCommand) {
this.commands.set(command.name, command);
if (command.aliases) {
command.aliases.forEach(alias => {
this.aliasMap.set(alias, command.name);
});
}
}
public async generate(input: { prompt: string, systemPrompt?: string }): Promise<string> {
const { prompt, systemPrompt = '' } = input;
// Parse command from prompt
const { command, cleanPrompt, parameters } = this.parseCommand(prompt);
// Transform prompt based on command
const transformed = command
? await this.commands.get(command)?.transform(cleanPrompt, {
systemPrompt,
parameters,
options: this.options
})
: { systemPrompt, userPrompt: prompt };
// Execute the transformed prompt with the provider's API
return this.executePrompt(transformed);
}
protected parseCommand(prompt: string): { command: string | null, cleanPrompt: string, parameters: Record<string, any> } {
const commandRegex = /^\/([a-zA-Z0-9_]+)(?:\s+([^\n]*))?/;
const match = prompt.match(commandRegex);
if (!match) {
return { command: null, cleanPrompt: prompt, parameters: {} };
}
const [fullMatch, command, rest] = match;
const commandName = this.aliasMap.get(command) || command;
if (!this.commands.has(commandName)) {
return { command: null, cleanPrompt: prompt, parameters: {} };
}
// Parse parameters if any
const parameters = this.parseParameters(commandName, rest || '');
const cleanPrompt = prompt.replace(fullMatch, '').trim();
return { command: commandName, cleanPrompt, parameters };
}
protected parseParameters(command: string, paramString: string): Record<string, any> {
// Default simple implementation - override in specific adapters as needed
const params: Record<string, any> = {};
const cmd = this.commands.get(command);
// If no parameters defined for command, return empty object
if (!cmd?.parameters || cmd.parameters.length === 0) {
return params;
}
// Set defaults
cmd.parameters.forEach(param => {
if (param.default !== undefined) {
params[param.name] = param.default;
}
});
// Simple parsing - can be enhanced for more complex parameter syntax
const paramRegex = /--([a-zA-Z0-9_]+)(?:=([^\s]+))?/g;
let match;
while ((match = paramRegex.exec(paramString)) !== null) {
const [_, paramName, paramValue = true] = match;
params[paramName] = paramValue;
}
return params;
}
/*
* These transformation methods must be implemented by specific adapters
* to account for platform-specific behavior
*/
protected abstract transformThink(prompt: string, options: any): Promise<TransformedPrompt>;
protected abstract transformFast(prompt: string, options: any): Promise<TransformedPrompt>;
protected abstract transformLoop(prompt: string, options: any): Promise<TransformedPrompt>;
protected abstract transformReflect(prompt: string, options: any): Promise<TransformedPrompt>;
protected abstract transformCollapse(prompt: string, options: any): Promise<TransformedPrompt>;
protected abstract transformFork(prompt: string, options: any): Promise<TransformedPrompt>;
// Method to execute the transformed prompt with the provider's API
protected abstract executePrompt(transformed: TransformedPrompt): Promise<string>;
}
|