-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
170 lines (149 loc) · 5.91 KB
/
Copy pathroute.ts
File metadata and controls
170 lines (149 loc) · 5.91 KB
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
import { NextResponse } from "next/server";
import { createSupabaseServerClient } from "@/lib/supabase/server";
import { detectVibePersona, type VibeAxes } from "@vibe-coding-profiler/core";
export const runtime = "nodejs";
type UserProfileRow = {
total_commits: number | null;
total_repos: number | null;
job_ids: unknown;
repo_personas_json: unknown;
axes_json: unknown;
updated_at: string | null;
persona_id: string | null;
persona_name: string | null;
persona_tagline: string | null;
persona_confidence: string | null;
persona_score: number | null;
};
type DoneJobRow = {
id: string;
repo_id: string;
commit_count: number | null;
completed_at: string | null;
created_at: string;
};
type UserRepoRow = {
repo_id: string;
connected_at: string;
disconnected_at: string | null;
};
export async function GET() {
const supabase = await createSupabaseServerClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) return NextResponse.json({ error: "unauthorized" }, { status: 401 });
const [{ data: profileData }, { data: userReposData }, { data: jobsData }] = await Promise.all([
supabase
.from("user_profiles")
.select(
"total_commits, total_repos, job_ids, repo_personas_json, axes_json, updated_at, persona_id, persona_name, persona_tagline, persona_confidence, persona_score"
)
.eq("user_id", user.id)
.maybeSingle(),
supabase
.from("user_repos")
.select("repo_id, connected_at, disconnected_at")
.eq("user_id", user.id),
supabase
.from("analysis_jobs")
.select("id, repo_id, commit_count, completed_at, created_at")
.eq("user_id", user.id)
.eq("status", "done")
.order("created_at", { ascending: false }),
]);
const profile = (profileData ?? null) as unknown as UserProfileRow | null;
const userRepos = (userReposData ?? []) as unknown as UserRepoRow[];
const doneJobs = (jobsData ?? []) as unknown as DoneJobRow[];
const disconnectedRepoIds = new Set(
userRepos.filter((r) => r.disconnected_at != null).map((r) => r.repo_id)
);
const connectedRepoIds = new Set(
userRepos.filter((r) => r.disconnected_at == null).map((r) => r.repo_id)
);
const includedDoneJobs = doneJobs.filter((j) => !disconnectedRepoIds.has(j.repo_id));
const distinctIncludedRepoIds = new Set(includedDoneJobs.map((j) => j.repo_id));
const profileJobIds = Array.isArray(profile?.job_ids)
? profile?.job_ids.filter((v): v is string => typeof v === "string")
: null;
const profileRepoPersonas = Array.isArray(profile?.repo_personas_json)
? profile?.repo_personas_json.filter((v): v is unknown => typeof v === "object" && v !== null)
: null;
const includedDoneJobIds = new Set(includedDoneJobs.map((j) => j.id));
const profileJobIdSet = new Set(profileJobIds ?? []);
const missingFromProfile = includedDoneJobs.map((j) => j.id).filter((id) => !profileJobIdSet.has(id));
const extraInProfile = (profileJobIds ?? []).filter((id) => !includedDoneJobIds.has(id));
const commitCountSum = includedDoneJobs.reduce((s, j) => s + (typeof j.commit_count === "number" ? j.commit_count : 0), 0);
const jobsMissingCommitCount = includedDoneJobs.filter((j) => j.commit_count == null).map((j) => j.id);
const latestCompletedAt = includedDoneJobs.find((j) => typeof j.completed_at === "string")?.completed_at ?? null;
const isAxisValue = (v: unknown): v is { score: number; level: string; why: string[] } => {
if (typeof v !== "object" || v === null) return false;
const r = v as Record<string, unknown>;
if (typeof r.score !== "number") return false;
if (typeof r.level !== "string") return false;
if (!Array.isArray(r.why)) return false;
return r.why.every((id) => typeof id === "string");
};
const isVibeAxes = (v: unknown): v is VibeAxes => {
if (typeof v !== "object" || v === null) return false;
const r = v as Record<string, unknown>;
return (
isAxisValue(r.automation_heaviness) &&
isAxisValue(r.guardrail_strength) &&
isAxisValue(r.iteration_loop_intensity) &&
isAxisValue(r.planning_signal) &&
isAxisValue(r.surface_area_per_change) &&
isAxisValue(r.shipping_rhythm)
);
};
const computedPersonaFromProfileAxes =
profile && isVibeAxes(profile.axes_json)
? (() => {
const totalCommits = profile.total_commits ?? 0;
const totalRepos = profile.total_repos ?? 0;
const repoFactor = Math.min(1, totalRepos / 5);
const commitFactor = Math.min(1, totalCommits / 500);
const dataQualityScore = Math.round(100 * (0.4 * repoFactor + 0.6 * commitFactor));
return detectVibePersona(profile.axes_json, {
commitCount: totalCommits,
prCount: 0,
dataQualityScore,
});
})()
: null;
return NextResponse.json({
user: { id: user.id, email: user.email ?? null },
userRepos: {
totalRows: userRepos.length,
connectedCount: connectedRepoIds.size,
disconnectedCount: disconnectedRepoIds.size,
},
analysisJobs: {
doneCount: doneJobs.length,
includedDoneCount: includedDoneJobs.length,
distinctIncludedRepos: distinctIncludedRepoIds.size,
commitCountSum,
jobsMissingCommitCount,
latestCompletedAt,
},
userProfile: profile
? {
personaId: profile.persona_id,
personaName: profile.persona_name,
personaTagline: profile.persona_tagline,
personaConfidence: profile.persona_confidence,
personaScore: profile.persona_score,
totalRepos: profile.total_repos,
totalCommits: profile.total_commits,
jobIdsCount: profileJobIds?.length ?? null,
repoPersonasCount: profileRepoPersonas?.length ?? null,
updatedAt: profile.updated_at,
}
: null,
computedPersonaFromProfileAxes,
diffs: {
missingFromProfile,
extraInProfile,
},
});
}