File size: 10,580 Bytes
a9df8b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import axios from 'axios'
import type {
  CandidateAuditResult,
  CandidateCreateResponse,
  CandidateSkillReviewRequest,
  EvaluationDashboardResponse,
  ExecutionExperienceUnit,
  ExecutionResult,
  ExecutionHistoryItem,
  GraphData,
  GraphViewData,
  GraphViewMode,
  HarnessLoopListResponse,
  HarnessVerifyLoopRequest,
  HarnessVerifyLoopResponse,
  HeterogeneousGraphData,
  HealthReport,
  IngestResponse,
  MaintenanceReviewRequest,
  MaintenanceReviewResponse,
  MaintenanceProposal,
  MaintenanceProposalListResponse,
  NewVersionRequest,
  OverviewStats,
  SkillReleaseRecord,
  SkillRollbackRecord,
  SkillCreateRequest,
  SkillFull,
  SkillGraphProjectionData,
  SkillSearchResult,
  SkillState,
  SkillSummary,
  SkillType,
  SkillUpdateRequest,
  SnapshotCommitRequest,
  SnapshotCommitResponse,
  SnapshotDiffRequest,
  SnapshotDiffResponse,
  SnapshotHistoryResponse,
  SystemHealth,
} from './types'

const http = axios.create({ baseURL: '/api/v1' })

// ── Skills ────────────────────────────────────────────────────────────────────

export const skillsApi = {
  list: (params?: { state?: SkillState; skill_type?: SkillType; limit?: number; offset?: number }) =>
    http.get<SkillSummary[]>('/skills', { params }).then(r => r.data),

  get: (id: string) =>
    http.get<SkillSummary>(`/skills/${id}`).then(r => r.data),

  getFull: (id: string) =>
    http.get<SkillFull>(`/skills/${id}/full`).then(r => r.data),

  create: (request: SkillCreateRequest) =>
    http.post<SkillSummary>('/skills', request).then(r => r.data),

  update: (id: string, request: SkillUpdateRequest) =>
    http.patch<SkillSummary>(`/skills/${id}`, request).then(r => r.data),

  search: (query: string, limit = 20) =>
    http.post<SkillSearchResult[]>('/skills/search', { query, limit }).then(r => r.data),

  versions: (id: string) =>
    http.get<SkillSummary[]>(`/skills/${id}/versions`).then(r => r.data),

  delete: (id: string) =>
    http.delete(`/skills/${id}`).then(r => r.data),
}

// ── Lifecycle ─────────────────────────────────────────────────────────────────

export const lifecycleApi = {
  release: (id: string) =>
    http.post<SkillSummary>(`/lifecycle/${id}/release`).then(r => r.data),

  deprecate: (id: string, reason: string) =>
    http.post<SkillSummary>(`/lifecycle/${id}/deprecate`, { reason }).then(r => r.data),

  transition: (id: string, new_state: SkillState, reason = '') =>
    http.post<SkillSummary>(`/lifecycle/${id}/transition`, { new_state, reason }).then(r => r.data),

  review: (id: string) =>
    http.post(`/lifecycle/${id}/review`).then(r => r.data),

  reviewAndRelease: (id: string) =>
    http.post<SkillSummary>(`/lifecycle/${id}/review-and-release`).then(r => r.data),

  newVersion: (
    id: string,
    requestOrBump: NewVersionRequest | 'major' | 'minor' | 'patch' = 'patch',
  ) => {
    const request = typeof requestOrBump === 'string' ? { bump: requestOrBump } : requestOrBump
    return http.post<SkillSummary>(`/lifecycle/${id}/new-version`, request).then(r => r.data)
  },

  getDiff: (id: string, compare_to?: string) =>
    http.get<Record<string, unknown>>(`/lifecycle/${id}/diff`, { params: compare_to ? { compare_to } : {} }).then(r => r.data),

  createSnapshot: (id: string, request: SnapshotCommitRequest = {}) =>
    http.post<SnapshotCommitResponse>(`/lifecycle/${id}/snapshot`, request).then(r => r.data),

  snapshotHistory: (id: string, max_count = 20) =>
    http.get<SnapshotHistoryResponse>(`/lifecycle/${id}/snapshot/history`, { params: { max_count } }).then(r => r.data),

  snapshotDiff: (id: string, request: SnapshotDiffRequest) =>
    http.get<SnapshotDiffResponse>(`/lifecycle/${id}/snapshot/diff`, { params: request }).then(r => r.data),

  releaseTag: (id: string, ref = 'HEAD') =>
    http.post<SkillReleaseRecord>(`/lifecycle/${id}/release-tag`, { ref }).then(r => r.data),

  restoreSnapshot: (id: string, source_ref: string) =>
    http.post<SkillRollbackRecord>(`/lifecycle/${id}/rollback`, { source_ref }).then(r => r.data),

  proposeMaintenanceChange: (id: string, request: MaintenanceReviewRequest) =>
    http.post<MaintenanceReviewResponse>(`/lifecycle/${id}/propose-maintenance-change`, request).then(r => r.data),
}

// ── Graph ─────────────────────────────────────────────────────────────────────

export const graphApi = {
  full: (limit = 200) =>
    http.get<GraphData>('/graph', { params: { limit } }).then(r => r.data),

  subgraph: (skill_id: string, depth = 2) =>
    http.post<GraphData>('/graph/subgraph', { skill_id, depth }).then(r => r.data),

  heterogeneous: () =>
    http.get<HeterogeneousGraphData>('/graph/heterogeneous').then(r => r.data),

  skillOnlyProjection: () =>
    http.get<SkillGraphProjectionData>('/graph/projection/skill-only').then(r => r.data),

  stats: () =>
    http.get<Record<string, unknown>>('/graph/stats/overview').then(r => r.data),

  view: (view: GraphViewMode, limit = 300) =>
    http.get<GraphViewData>('/graph/view', { params: { view, limit } }).then(r => r.data),
}

// ── Execution ─────────────────────────────────────────────────────────────────

export const executionApi = {
  executeSkill: (skill_id: string, inputs: Record<string, unknown> = {}) =>
    http.post<ExecutionResult>('/execution/skill', { skill_id, inputs }).then(r => r.data),

  executePlan: (goal: string, context: Record<string, unknown> = {}) =>
    http.post<ExecutionResult>('/execution/plan', { goal, context }).then(r => r.data),

  getState: () =>
    http.get<Record<string, unknown>>('/execution/state').then(r => r.data),

  resetState: () =>
    http.delete('/execution/state').then(r => r.data),

  history: () =>
    http.get<ExecutionHistoryItem[]>('/execution/history').then(r => r.data),

  experience: (execution_id: string) =>
    http.get<ExecutionExperienceUnit>(`/execution/history/${execution_id}/experience`).then(r => r.data),
}

// ── Evolution ─────────────────────────────────────────────────────────────────

export const harnessApi = {
  runVerifyLoop: (skill_id: string, request: HarnessVerifyLoopRequest = {}) =>
    http.post<HarnessVerifyLoopResponse>(`/harness/${skill_id}/verify-loop`, request).then(r => r.data),

  list: (limit = 20) =>
    http.get<HarnessLoopListResponse>('/harness', { params: { limit } }).then(r => r.data),

  get: (loop_id: string) =>
    http.get<Record<string, unknown>>(`/harness/${loop_id}`).then(r => r.data),
}

export const evolutionApi = {
  systemHealth: () =>
    http.get<SystemHealth>('/evolution/health').then(r => r.data),

  skillHealth: (id: string) =>
    http.get<HealthReport>(`/evolution/health/${id}`).then(r => r.data),

  repair: (id: string) =>
    http.post(`/evolution/repair/${id}`).then(r => r.data),

  runCycle: () =>
    http.post('/evolution/cycle').then(r => r.data),

  proposals: (status?: MaintenanceProposal['status']) =>
    http.get<MaintenanceProposalListResponse>('/evolution/proposals', { params: status ? { status } : {} }).then(r => r.data),

  acceptProposal: (id: string) =>
    http.post<MaintenanceProposal>(`/evolution/proposals/${id}/accept`).then(r => r.data),

  rejectProposal: (id: string) =>
    http.post<MaintenanceProposal>(`/evolution/proposals/${id}/reject`).then(r => r.data),
}

// ── Ingest ────────────────────────────────────────────────────────────────────

export const ingestApi = {
  parse: (source_type: string, content: string) =>
    http.post<IngestResponse>('/ingest/parse', { source_type, content }).then(r => r.data),

  parseAndCreate: (source_type: string, content: string) =>
    http.post<IngestResponse>('/ingest/parse-and-create', { source_type, content }).then(r => r.data),

  auditCandidate: (request: CandidateSkillReviewRequest) =>
    http.post<CandidateAuditResult>('/ingest/audit-candidate', request).then(r => r.data),

  createCandidate: (request: CandidateSkillReviewRequest) =>
    http.post<CandidateCreateResponse>('/ingest/create-candidate', request).then(r => r.data),
}

// ── Stats ─────────────────────────────────────────────────────────────────────

export const evaluationApi = {
  dashboard: () =>
    http.get<EvaluationDashboardResponse>('/evaluation/dashboard').then(r => r.data),
}

export interface EvolutionStats {
  total_skills: number
  auto_generated: number
  manual: number
  avg_reuse_rate: number
  avg_success_rate: number
  version_improved_count: number
  skills_by_category: Record<string, number>
  recent_activity: {
    skill_id: string
    name: string
    event: string
    state: string
    time: string
  }[]
}

export const statsApi = {
  overview: async (): Promise<OverviewStats> => {
    await Promise.all([
      skillsApi.list({ limit: 1 }),
      evolutionApi.systemHealth().catch(() => null),
    ])
    const allSkills = await skillsApi.list({ limit: 200 })
    const byState: Record<string, number> = {}
    const byType: Record<string, number> = {}
    let totalExec = 0
    let successRateSum = 0
    let ratedCount = 0
    for (const s of allSkills) {
      byState[s.state] = (byState[s.state] || 0) + 1
      byType[s.skill_type] = (byType[s.skill_type] || 0) + 1
      totalExec += s.metrics.total_executions
      if (s.metrics.total_executions >= 5) {
        successRateSum += s.metrics.success_rate
        ratedCount++
      }
    }
    return {
      total_skills: allSkills.length,
      by_state: byState,
      by_type: byType,
      total_executions: totalExec,
      avg_success_rate: ratedCount > 0 ? successRateSum / ratedCount : 1,
      graph_stats: {},
    }
  },

  evolutionStats: () =>
    http.get<EvolutionStats>('/skills/evolution-stats').then(r => r.data),
}