All files / prompts chainer.ts

10.66% Statements 8/75
0% Branches 0/22
11.76% Functions 2/17
13.55% Lines 8/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1581x                             1x 2x       2x       1x 1x           1x 1x                                                                                                                                                                                                                                                          
import { v4 as uuid } from 'uuid';
import {
  PromptChain,
  PromptChainNode,
  ChainContext,
  PromptLevel,
  StageStatus,
  Decision,
  ArtifactRef,
  Constraint,
  ProjectManifest,
  PromptTemplate,
} from '../types';
import { TemplateRegistry } from './templates';
 
export class PromptChainer {
  private chains: Map<string, PromptChain> = new Map();
  private templates: TemplateRegistry;
 
  constructor(templates: TemplateRegistry) {
    this.templates = templates;
  }
 
  createChain(projectId: string, context: ChainContext): PromptChain {
    const id = uuid();
    const chain: PromptChain = {
      id,
      projectId,
      stages: [],
      context,
    };
    this.chains.set(id, chain);
    return chain;
  }
 
  addNode(
    chainId: string,
    templateId: string,
    level: PromptLevel,
    options: Record<string, unknown>,
    parentId?: string
  ): PromptChainNode {
    const chain = this.chains.get(chainId);
    Iif (!chain) throw new Error(`Chain ${chainId} not found`);
 
    const template = this.templates.get(templateId);
    Iif (!template) throw new Error(`Template ${templateId} not found`);
 
    const node: PromptChainNode = {
      id: uuid(),
      templateId,
      level,
      parentId,
      children: [],
      resolvedPrompt: '',
      options,
      status: 'pending',
    };
 
    node.resolvedPrompt = this.resolveTemplate(template, options, chain.context);
 
    Iif (parentId) {
      const parent = chain.stages.find((n) => n.id === parentId);
      Iif (parent) {
        parent.children.push(node.id);
      }
    }
 
    chain.stages.push(node);
    return node;
  }
 
  resolveTemplate(
    template: PromptTemplate,
    options: Record<string, unknown>,
    context: ChainContext
  ): string {
    let prompt = template.template;
 
    for (const slot of template.slots) {
      const value = options[slot.name] ?? slot.default;
      const placeholder = `{{${slot.name}}}`;
      prompt = prompt.replace(new RegExp(placeholder, 'g'), value !== undefined ? String(value) : '');
    }
 
    // Inject context
    Iif (context.projectConfig) {
      prompt = prompt.replace(
        /\{\{projectName\}\}/g,
        context.projectConfig.name
      );
    }
 
    // Validate required slots
    for (const slot of template.slots) {
      Iif (slot.required && !options[slot.name] && slot.default === undefined) {
        throw new Error(
          `Missing required slot "${slot.name}" for template "${template.id}"`
        );
      }
    }
 
    return prompt;
  }
 
  getChain(id: string): PromptChain | undefined {
    return this.chains.get(id);
  }
 
  getNode(chainId: string, nodeId: string): PromptChainNode | undefined {
    const chain = this.chains.get(chainId);
    Iif (!chain) return undefined;
    return chain.stages.find((n) => n.id === nodeId);
  }
 
  setNodeOutput(chainId: string, nodeId: string, output: string): void {
    const chain = this.chains.get(chainId);
    Iif (!chain) throw new Error(`Chain ${chainId} not found`);
    const node = chain.stages.find((n) => n.id === nodeId);
    Iif (!node) throw new Error(`Node ${nodeId} not found`);
    node.output = output;
    node.status = 'completed';
  }
 
  setNodeStatus(chainId: string, nodeId: string, status: StageStatus): void {
    const chain = this.chains.get(chainId);
    Iif (!chain) throw new Error(`Chain ${chainId} not found`);
    const node = chain.stages.find((n) => n.id === nodeId);
    Iif (!node) throw new Error(`Node ${nodeId} not found`);
    node.status = status;
  }
 
  getNextNodes(chainId: string): PromptChainNode[] {
    const chain = this.chains.get(chainId);
    Iif (!chain) return [];
    return chain.stages.filter((n) => n.status === 'pending');
  }
 
  enrichContext(chainId: string, decisions: Decision[], artifacts: ArtifactRef[]): void {
    const chain = this.chains.get(chainId);
    Iif (!chain) return;
    chain.context.previousDecisions.push(...decisions);
    chain.context.generatedArtifacts.push(...artifacts);
  }
 
  addConstraint(chainId: string, constraint: Constraint): void {
    const chain = this.chains.get(chainId);
    Iif (!chain) return;
    chain.context.constraints.push(constraint);
  }
 
  serializeChain(chainId: string): object {
    const chain = this.chains.get(chainId);
    Iif (!chain) throw new Error(`Chain ${chainId} not found`);
    return JSON.parse(JSON.stringify(chain));
  }
}