All files / src/core-entities context-template.entity.ts

90.62% Statements 29/32
100% Branches 0/0
70% Functions 7/10
88.46% Lines 23/26

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 11948x 48x 48x 48x 48x 48x     81x                           48x           48x             48x             48x               48x               48x               48x                           48x           6x 48x           6x 48x               48x             48x           6x 48x    
import { Entity, Column, OneToMany } from 'typeorm';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { BaseEntity } from './base.entity';
import { Session } from './session.entity';
import { SessionInput } from './session-input.entity';
import { TemplateType } from './enums';
 
// Re-export for backwards compatibility
export { TemplateType } from './enums';
 
/**
 * ContextTemplate defines how context is generated for LLM prompts.
 * Uses Eta template syntax to render files, folders, and dynamic content.
 *
 * Three default templates are tracked:
 * - is_default_initial: Used for first prompt in a session
 * - is_default_followup: Used for subsequent prompts
 * - is_default_condensed: Used for compressed history context
 *
 * Templates can include partial templates for reusable content blocks.
 */
@Entity('context_templates')
export class ContextTemplate extends BaseEntity {
  @ApiProperty({
    description: 'Human-readable name for this template',
    example: 'Full Project Context',
  })
  @Column({ type: 'text' })
  template_name: string;
 
  @ApiProperty({
    description:
      'Eta template content. Supports it.files, it.folders, it.tools variables.',
  })
  @Column({ type: 'text' })
  template_content: string;
 
  @ApiPropertyOptional({
    description: 'JSON string defining files/folders to include',
    example: '{"files": ["src/index.ts"], "folders": ["src/components"]}',
  })
  @Column({ type: 'text', nullable: true })
  context_definition: string;
 
  @ApiProperty({
    description: 'Whether this is the default for initial prompts',
    example: false,
    default: false,
  })
  @Column({ type: 'boolean', default: false })
  is_default_initial: boolean;
 
  @ApiProperty({
    description: 'Whether this is the default for follow-up prompts',
    example: false,
    default: false,
  })
  @Column({ type: 'boolean', default: false })
  is_default_followup: boolean;
 
  @ApiProperty({
    description: 'Whether this is the default for condensed history',
    example: false,
    default: false,
  })
  @Column({ type: 'boolean', default: false })
  is_default_condensed: boolean;
 
  @ApiProperty({
    description:
      'Template type: main (full template) or partial (reusable block)',
    enum: TemplateType,
    enumName: 'TemplateType',
    example: TemplateType.MAIN,
  })
  @Column({
    type: 'text',
    enum: TemplateType,
    default: TemplateType.MAIN,
  })
  template_type: TemplateType;
 
  @ApiPropertyOptional({
    description: 'Sessions that use this as initial default',
    type: () => [Session],
  })
  @OneToMany(() => Session, (session) => session.defaultInitialContextTemplate)
  sessionsAsInitialDefault: Session[];
 
  @ApiPropertyOptional({
    description: 'Sessions that use this as followup default',
    type: () => [Session],
  })
  @OneToMany(() => Session, (session) => session.defaultFollowupContextTemplate)
  sessionsAsFollowupDefault: Session[];
 
  @ApiProperty({
    description: 'Whether this is a built-in seed template (immutable)',
    example: false,
    default: false,
  })
  @Column({ type: 'boolean', default: false })
  is_builtin: boolean;
 
  @ApiPropertyOptional({
    description: 'Unique key for built-in templates (used for version updates)',
    example: 'initial-full-project-context',
  })
  @Column({ type: 'text', nullable: true, unique: true })
  builtin_key: string | null;
 
  @ApiPropertyOptional({
    description: 'SessionInputs that used this template',
    type: () => [SessionInput],
  })
  @OneToMany(() => SessionInput, (sessionInput) => sessionInput.contextTemplate)
  sessionInputs: SessionInput[];
}