File size: 3,111 Bytes
c78c312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Feedback Loop API Routes
 *
 * POST /v1/feedback — Submit a rating for a response
 * GET  /v1/feedback/stats — Get learning statistics
 *
 * The feedback loop learns from user ratings via Exponential Moving Average.
 * Learned adjustments are applied to future AutoTune parameter computations.
 */

import { Router } from 'express'
import {
  createInitialFeedbackState,
  processFeedback,
  computeHeuristics,
  getFeedbackStats,
  type FeedbackRecord,
  type FeedbackState,
} from '../../src/lib/autotune-feedback'
import type { ContextType, AutoTuneParams } from '../../src/lib/autotune'
import { updateSharedProfiles } from './autotune'

export const feedbackRoutes = Router()

// In-memory feedback state (resets on restart — research preview)
let feedbackState: FeedbackState = createInitialFeedbackState()

const VALID_CONTEXTS: ContextType[] = ['code', 'creative', 'analytical', 'conversational', 'chaotic']

feedbackRoutes.post('/', (req, res) => {
  try {
    const {
      message_id,
      context_type,
      model = 'unknown',
      persona = 'default',
      rating,
      params,
      response_text,
    } = req.body

    // Validate
    if (!message_id || typeof message_id !== 'string') {
      res.status(400).json({ error: 'message_id (string) is required' })
      return
    }

    if (!VALID_CONTEXTS.includes(context_type)) {
      res.status(400).json({
        error: `Invalid context_type. Must be one of: ${VALID_CONTEXTS.join(', ')}`,
      })
      return
    }

    if (rating !== 1 && rating !== -1) {
      res.status(400).json({ error: 'rating must be 1 (positive) or -1 (negative)' })
      return
    }

    if (!params || typeof params !== 'object') {
      res.status(400).json({ error: 'params (AutoTuneParams object) is required' })
      return
    }

    // Compute heuristics from response text if provided
    const heuristics = response_text
      ? computeHeuristics(String(response_text))
      : { responseLength: 0, repetitionScore: 0, averageSentenceLength: 0, vocabularyDiversity: 0 }

    const record: FeedbackRecord = {
      messageId: message_id,
      timestamp: Date.now(),
      contextType: context_type,
      model: String(model),
      persona: String(persona),
      params: params as AutoTuneParams,
      rating,
      heuristics,
    }

    // Process and update state
    feedbackState = processFeedback(feedbackState, record)

    // Sync learned profiles to the AutoTune route
    updateSharedProfiles(feedbackState.learnedProfiles)

    res.json({
      accepted: true,
      total_feedback: feedbackState.history.length,
      context_type,
      learned: feedbackState.learnedProfiles[context_type].sampleCount >= 3,
    })
  } catch (err: any) {
    res.status(500).json({ error: err.message })
  }
})

feedbackRoutes.get('/stats', (_req, res) => {
  const stats = getFeedbackStats(feedbackState)
  res.json({
    total_feedback: stats.totalFeedback,
    positive_rate: stats.positiveRate,
    context_breakdown: stats.contextBreakdown,
    oldest_record: stats.oldestRecord,
    newest_record: stats.newestRecord,
  })
})