Jacksuyu commited on
Commit
e8dc740
·
1 Parent(s): 9a64b79

Show original idea text and normalize weak generated titles

Browse files
app/api/ideas/[id]/route.ts CHANGED
@@ -4,6 +4,7 @@ import { mapIdea, mapTask } from "@/lib/db-mappers";
4
  import { getAnonId } from "@/lib/identity";
5
  import { auth } from "@/auth";
6
  import { canDeleteIdea, isAdminEmail } from "@/lib/permissions";
 
7
 
8
  export async function GET(_: NextRequest, { params }: { params: Promise<{ id: string }> }) {
9
  const session = await auth();
@@ -62,9 +63,17 @@ export async function GET(_: NextRequest, { params }: { params: Promise<{ id: st
62
  actorEmail: session?.user?.email,
63
  });
64
 
 
 
 
 
 
 
 
65
  return NextResponse.json({
66
  idea: {
67
- ...mapIdea(idea),
 
68
  submitter_label: idea.isAnonymous
69
  ? "Anonymous"
70
  : idea.submitterVisibleName || idea.createdByUser?.developerProfile?.displayName || idea.createdByUser?.name || "Member",
 
4
  import { getAnonId } from "@/lib/identity";
5
  import { auth } from "@/auth";
6
  import { canDeleteIdea, isAdminEmail } from "@/lib/permissions";
7
+ import { buildMeaningfulTitle } from "@/lib/title";
8
 
9
  export async function GET(_: NextRequest, { params }: { params: Promise<{ id: string }> }) {
10
  const session = await auth();
 
63
  actorEmail: session?.user?.email,
64
  });
65
 
66
+ const mappedIdea = mapIdea(idea);
67
+ const displayTitle = buildMeaningfulTitle({
68
+ rawInputText: idea.rawInputText,
69
+ title: idea.title,
70
+ problemStatement: idea.problemStatement,
71
+ });
72
+
73
  return NextResponse.json({
74
  idea: {
75
+ ...mappedIdea,
76
+ title: displayTitle,
77
  submitter_label: idea.isAnonymous
78
  ? "Anonymous"
79
  : idea.submitterVisibleName || idea.createdByUser?.developerProfile?.displayName || idea.createdByUser?.name || "Member",
app/api/ideas/route.ts CHANGED
@@ -8,6 +8,7 @@ import { scoreHot } from "@/lib/hot-score";
8
  import { auth } from "@/auth";
9
  import { findBlockedContent } from "@/lib/content-moderation";
10
  import { detectMergeTarget } from "@/lib/idea-dedup";
 
11
 
12
  const createIdeaSchema = z.object({
13
  raw_input_text: z.string().min(20).max(3000),
@@ -101,6 +102,11 @@ export async function GET(request: NextRequest) {
101
  const page = hasMore ? ideas.slice(0, limit) : ideas;
102
 
103
  const mapped = page.map((idea) => {
 
 
 
 
 
104
  const visibleName =
105
  idea.submitterVisibleName || idea.createdByUser?.developerProfile?.displayName || idea.createdByUser?.name || null;
106
 
@@ -120,6 +126,7 @@ export async function GET(request: NextRequest) {
120
 
121
  return {
122
  ...mapIdea(idea),
 
123
  comment_count: idea.commentsCount,
124
  submitter_label: idea.isAnonymous ? "Anonymous" : visibleName || "Member",
125
  is_anonymous: idea.isAnonymous,
@@ -234,6 +241,11 @@ export async function POST(request: NextRequest) {
234
 
235
  try {
236
  const spec = await generateSpec(parsed.data);
 
 
 
 
 
237
 
238
  let submitterVisibleName: string | null = null;
239
  const isAnonymous = !(parsed.data.show_name && userId);
@@ -254,7 +266,7 @@ export async function POST(request: NextRequest) {
254
  targetUsers: parsed.data.target_users,
255
  platform: parsed.data.platform,
256
  constraints: parsed.data.constraints,
257
- title: spec.title,
258
  problemStatement: spec.problem_statement,
259
  tags: JSON.stringify(spec.tags),
260
  features: JSON.stringify(spec.features),
@@ -276,6 +288,7 @@ export async function POST(request: NextRequest) {
276
  {
277
  idea: {
278
  ...mapIdea(idea),
 
279
  submitter_label: idea.isAnonymous ? "Anonymous" : submitterVisibleName || "Member",
280
  tasks: idea.tasks.map((task) => ({
281
  ...mapTask(task),
 
8
  import { auth } from "@/auth";
9
  import { findBlockedContent } from "@/lib/content-moderation";
10
  import { detectMergeTarget } from "@/lib/idea-dedup";
11
+ import { buildMeaningfulTitle } from "@/lib/title";
12
 
13
  const createIdeaSchema = z.object({
14
  raw_input_text: z.string().min(20).max(3000),
 
102
  const page = hasMore ? ideas.slice(0, limit) : ideas;
103
 
104
  const mapped = page.map((idea) => {
105
+ const displayTitle = buildMeaningfulTitle({
106
+ rawInputText: idea.rawInputText,
107
+ title: idea.title,
108
+ problemStatement: idea.problemStatement,
109
+ });
110
  const visibleName =
111
  idea.submitterVisibleName || idea.createdByUser?.developerProfile?.displayName || idea.createdByUser?.name || null;
112
 
 
126
 
127
  return {
128
  ...mapIdea(idea),
129
+ title: displayTitle,
130
  comment_count: idea.commentsCount,
131
  submitter_label: idea.isAnonymous ? "Anonymous" : visibleName || "Member",
132
  is_anonymous: idea.isAnonymous,
 
241
 
242
  try {
243
  const spec = await generateSpec(parsed.data);
244
+ const displayTitle = buildMeaningfulTitle({
245
+ rawInputText: parsed.data.raw_input_text,
246
+ title: spec.title,
247
+ problemStatement: spec.problem_statement,
248
+ });
249
 
250
  let submitterVisibleName: string | null = null;
251
  const isAnonymous = !(parsed.data.show_name && userId);
 
266
  targetUsers: parsed.data.target_users,
267
  platform: parsed.data.platform,
268
  constraints: parsed.data.constraints,
269
+ title: displayTitle,
270
  problemStatement: spec.problem_statement,
271
  tags: JSON.stringify(spec.tags),
272
  features: JSON.stringify(spec.features),
 
288
  {
289
  idea: {
290
  ...mapIdea(idea),
291
+ title: displayTitle,
292
  submitter_label: idea.isAnonymous ? "Anonymous" : submitterVisibleName || "Member",
293
  tasks: idea.tasks.map((task) => ({
294
  ...mapTask(task),
app/ideas/[id]/page.tsx CHANGED
@@ -5,6 +5,7 @@ import { getAnonId, getNickname } from "@/lib/identity";
5
  import { IdeaDetailClient } from "@/components/IdeaDetailClient";
6
  import { auth } from "@/auth";
7
  import { canDeleteIdea } from "@/lib/permissions";
 
8
 
9
  const ALLOWED_STATUS = new Set(["OPEN", "IN_PROGRESS", "DONE"] as const);
10
 
@@ -68,8 +69,16 @@ export default async function IdeaPage({ params }: { params: Promise<{ id: strin
68
  : [];
69
  const nicknameByAnonId = new Map(claimantNicknames.map((row) => [row.anonId, row.nickname]));
70
 
 
 
 
 
 
 
 
71
  const payload = {
72
- ...mapIdea(idea),
 
73
  submitter_label: idea.isAnonymous
74
  ? "Anonymous"
75
  : idea.submitterVisibleName || idea.createdByUser?.developerProfile?.displayName || idea.createdByUser?.name || "Member",
 
5
  import { IdeaDetailClient } from "@/components/IdeaDetailClient";
6
  import { auth } from "@/auth";
7
  import { canDeleteIdea } from "@/lib/permissions";
8
+ import { buildMeaningfulTitle } from "@/lib/title";
9
 
10
  const ALLOWED_STATUS = new Set(["OPEN", "IN_PROGRESS", "DONE"] as const);
11
 
 
69
  : [];
70
  const nicknameByAnonId = new Map(claimantNicknames.map((row) => [row.anonId, row.nickname]));
71
 
72
+ const mappedIdea = mapIdea(idea);
73
+ const displayTitle = buildMeaningfulTitle({
74
+ rawInputText: idea.rawInputText,
75
+ title: idea.title,
76
+ problemStatement: idea.problemStatement,
77
+ });
78
+
79
  const payload = {
80
+ ...mappedIdea,
81
+ title: displayTitle,
82
  submitter_label: idea.isAnonymous
83
  ? "Anonymous"
84
  : idea.submitterVisibleName || idea.createdByUser?.developerProfile?.displayName || idea.createdByUser?.name || "Member",
components/IdeaDetailClient.tsx CHANGED
@@ -57,6 +57,10 @@ type IdeaPayload = {
57
  id: string;
58
  title: string;
59
  problemStatement: string;
 
 
 
 
60
  tags: string[];
61
  features: string[];
62
  open_questions: string[];
@@ -475,6 +479,18 @@ export function IdeaDetailClient({
475
  ) : null}
476
  </section>
477
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  <section className="grid gap-4 md:grid-cols-2 reveal-up" style={{ animationDelay: "80ms" }}>
479
  <div className="card">
480
  <h2 className="section-title text-xl">Core Features</h2>
 
57
  id: string;
58
  title: string;
59
  problemStatement: string;
60
+ rawInputText: string;
61
+ targetUsers: string | null;
62
+ platform: string | null;
63
+ constraints: string | null;
64
  tags: string[];
65
  features: string[];
66
  open_questions: string[];
 
479
  ) : null}
480
  </section>
481
 
482
+ <section className="card reveal-up" style={{ animationDelay: "70ms" }}>
483
+ <div className="flex flex-wrap items-center gap-2">
484
+ <h2 className="section-title text-xl">Original Submission</h2>
485
+ {idea.platform ? <span className="pill">Platform: {idea.platform}</span> : null}
486
+ {idea.targetUsers ? <span className="pill">Target: {idea.targetUsers}</span> : null}
487
+ </div>
488
+ {idea.constraints ? <p className="subtle mt-2 text-xs">Constraints: {idea.constraints}</p> : null}
489
+ <p className="mt-3 whitespace-pre-wrap break-words rounded-xl border border-zinc-200 bg-white/70 p-3 text-sm leading-6">
490
+ {idea.rawInputText}
491
+ </p>
492
+ </section>
493
+
494
  <section className="grid gap-4 md:grid-cols-2 reveal-up" style={{ animationDelay: "80ms" }}>
495
  <div className="card">
496
  <h2 className="section-title text-xl">Core Features</h2>
lib/title.ts ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const BAD_ENDINGS = new Set(["and", "or", "but", "to", "for", "with", "of", "a", "an", "the"]);
2
+
3
+ function collapseWhitespace(value: string): string {
4
+ return value.replace(/\s+/g, " ").trim();
5
+ }
6
+
7
+ function isWeakTitle(title: string): boolean {
8
+ const cleaned = collapseWhitespace(title).replace(/^['\"`]+|['\"`]+$/g, "");
9
+ if (cleaned.length < 12 || cleaned.length > 110) return true;
10
+
11
+ const words = cleaned.split(" ").filter(Boolean);
12
+ if (words.length < 3) return true;
13
+
14
+ const last = words[words.length - 1]?.toLowerCase() || "";
15
+ if (BAD_ENDINGS.has(last)) return true;
16
+
17
+ const alphaWords = words.filter((word) => /[a-z]/i.test(word));
18
+ if (alphaWords.length < 3) return true;
19
+
20
+ return false;
21
+ }
22
+
23
+ function titleCase(phrase: string): string {
24
+ const stop = new Set(["a", "an", "the", "and", "or", "for", "to", "of", "in", "on", "with"]);
25
+ const words = phrase.split(" ").filter(Boolean);
26
+
27
+ return words
28
+ .map((word, index) => {
29
+ const lower = word.toLowerCase();
30
+ if (index > 0 && stop.has(lower)) return lower;
31
+ return lower.charAt(0).toUpperCase() + lower.slice(1);
32
+ })
33
+ .join(" ");
34
+ }
35
+
36
+ function deriveTitleFromInput(rawInputText: string): string {
37
+ let text = collapseWhitespace(rawInputText);
38
+ text = text
39
+ .replace(/^i\s+(want|need|would\s+like|wish|hope)\s+(to\s+)?/i, "")
40
+ .replace(/^please\s+/i, "")
41
+ .replace(/^can\s+you\s+/i, "");
42
+
43
+ const firstClause = text.split(/[.!?;:\n]/)[0] || text;
44
+ const words = collapseWhitespace(firstClause).split(" ").filter(Boolean);
45
+
46
+ const trimmedWords = words.slice(0, 12);
47
+ while (trimmedWords.length && BAD_ENDINGS.has(trimmedWords[trimmedWords.length - 1].toLowerCase())) {
48
+ trimmedWords.pop();
49
+ }
50
+
51
+ const fallback = trimmedWords.join(" ").trim();
52
+ if (!fallback) return "New Product Idea";
53
+
54
+ const withPrefix = /^build\b|^create\b|^make\b/i.test(fallback) ? fallback : `Build ${fallback}`;
55
+ return titleCase(withPrefix);
56
+ }
57
+
58
+ export function buildMeaningfulTitle(params: {
59
+ rawInputText: string;
60
+ title: string;
61
+ problemStatement?: string | null;
62
+ }): string {
63
+ const candidate = collapseWhitespace(params.title || "");
64
+ if (!candidate || isWeakTitle(candidate)) {
65
+ return deriveTitleFromInput(params.rawInputText);
66
+ }
67
+
68
+ // If model title is just a direct clipped prefix of raw input, promote a cleaner synthesized title.
69
+ const normalizedRaw = collapseWhitespace(params.rawInputText).toLowerCase();
70
+ if (normalizedRaw.startsWith(candidate.toLowerCase()) && candidate.length < 26) {
71
+ return deriveTitleFromInput(params.rawInputText);
72
+ }
73
+
74
+ return candidate;
75
+ }