-
-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathsuggest.ts
More file actions
213 lines (191 loc) · 5.96 KB
/
suggest.ts
File metadata and controls
213 lines (191 loc) · 5.96 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
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
import { getAllAddOns, type AddOn } from '@tanstack/create'
import type { ProjectDefinition } from './compile'
import { getFramework, DEFAULT_MODE } from './config'
export interface SuggestRequest {
description?: string
current?: Partial<ProjectDefinition>
intent?: 'full-stack' | 'api-only' | 'static' | 'database' | 'auth' | 'deploy'
}
export interface FeatureSuggestion {
id: string
name: string
reason: string
confidence: 'high' | 'medium' | 'low'
category: string
}
export interface SuggestResponse {
suggestions: Array<FeatureSuggestion>
reasoning: string
recommendedTemplate?: string
}
const FEATURE_KEYWORDS: Record<
string,
Array<{ keyword: string; weight: number }>
> = {
'tanstack-query': [
{ keyword: 'data fetching', weight: 1 },
{ keyword: 'api', weight: 0.7 },
{ keyword: 'server state', weight: 1 },
{ keyword: 'cache', weight: 0.8 },
{ keyword: 'react query', weight: 1 },
{ keyword: 'tanstack query', weight: 1 },
],
form: [
{ keyword: 'form', weight: 1 },
{ keyword: 'validation', weight: 0.8 },
{ keyword: 'input', weight: 0.5 },
],
drizzle: [
{ keyword: 'database', weight: 0.9 },
{ keyword: 'sql', weight: 0.8 },
{ keyword: 'postgres', weight: 0.9 },
{ keyword: 'orm', weight: 0.8 },
{ keyword: 'drizzle', weight: 1 },
],
prisma: [
{ keyword: 'prisma', weight: 1 },
{ keyword: 'database', weight: 0.7 },
{ keyword: 'orm', weight: 0.7 },
],
clerk: [
{ keyword: 'auth', weight: 0.8 },
{ keyword: 'authentication', weight: 0.9 },
{ keyword: 'login', weight: 0.8 },
{ keyword: 'clerk', weight: 1 },
],
'better-auth': [
{ keyword: 'auth', weight: 0.7 },
{ keyword: 'self-hosted', weight: 1 },
{ keyword: 'better-auth', weight: 1 },
],
ai: [
{ keyword: 'ai', weight: 1 },
{ keyword: 'llm', weight: 0.9 },
{ keyword: 'chatbot', weight: 0.9 },
{ keyword: 'openai', weight: 0.8 },
],
shadcn: [
{ keyword: 'ui', weight: 0.6 },
{ keyword: 'components', weight: 0.6 },
{ keyword: 'shadcn', weight: 1 },
],
sentry: [
{ keyword: 'error tracking', weight: 1 },
{ keyword: 'monitoring', weight: 0.8 },
{ keyword: 'sentry', weight: 1 },
],
posthog: [
{ keyword: 'analytics', weight: 1 },
{ keyword: 'posthog', weight: 1 },
{ keyword: 'tracking', weight: 0.8 },
{ keyword: 'feature flags', weight: 0.9 },
{ keyword: 'session replay', weight: 1 },
{ keyword: 'product analytics', weight: 1 },
],
}
const INTENT_FEATURES: Record<string, Array<string>> = {
'full-stack': ['drizzle', 'shadcn'],
'api-only': ['drizzle'],
database: ['drizzle'],
auth: ['clerk'],
deploy: [],
}
function getCategoryFromType(type: string): string {
switch (type) {
case 'deployment':
return 'deploy'
case 'toolchain':
return 'tooling'
default:
return 'other'
}
}
export async function suggestHandler(
request: SuggestRequest,
): Promise<SuggestResponse> {
const framework = getFramework()
const allAddOns = getAllAddOns(framework, DEFAULT_MODE)
const addOnMap = new Map(allAddOns.map((a: AddOn) => [a.id, a] as const))
const suggestions: Array<FeatureSuggestion> = []
const reasons: Array<string> = []
const currentFeatures = new Set(request.current?.features || [])
if (request.description) {
const description = request.description.toLowerCase()
for (const [featureId, keywords] of Object.entries(FEATURE_KEYWORDS)) {
if (currentFeatures.has(featureId)) continue
let score = 0
const matchedKeywords: Array<string> = []
for (const { keyword, weight } of keywords) {
if (description.includes(keyword.toLowerCase())) {
score += weight
matchedKeywords.push(keyword)
}
}
if (score > 0) {
const addOn = addOnMap.get(featureId) as AddOn | undefined
if (addOn) {
suggestions.push({
id: featureId,
name: addOn.name,
reason: `Matches: ${matchedKeywords.join(', ')}`,
confidence: score >= 1 ? 'high' : score >= 0.5 ? 'medium' : 'low',
category: getCategoryFromType(addOn.type),
})
}
}
}
if (suggestions.length > 0) {
reasons.push(
`Based on your description, I identified ${suggestions.length} relevant features.`,
)
}
}
if (request.intent && INTENT_FEATURES[request.intent]) {
const intentFeatures = INTENT_FEATURES[request.intent]
for (const featureId of intentFeatures) {
if (currentFeatures.has(featureId)) continue
if (suggestions.some((s) => s.id === featureId)) continue
const addOn = addOnMap.get(featureId) as AddOn | undefined
if (addOn) {
suggestions.push({
id: featureId,
name: addOn.name,
reason: `Recommended for ${request.intent} projects`,
confidence: 'high',
category: getCategoryFromType(addOn.type),
})
}
}
reasons.push(`Added recommendations for ${request.intent} development.`)
}
const confidenceOrder = { high: 0, medium: 1, low: 2 }
suggestions.sort(
(a, b) => confidenceOrder[a.confidence] - confidenceOrder[b.confidence],
)
for (const suggestion of [...suggestions]) {
const addOn = addOnMap.get(suggestion.id) as AddOn | undefined
if (addOn?.dependsOn) {
for (const required of addOn.dependsOn) {
if (
!currentFeatures.has(required) &&
!suggestions.some((s) => s.id === required)
) {
const requiredAddOn = addOnMap.get(required) as AddOn | undefined
if (requiredAddOn) {
suggestions.push({
id: required,
name: requiredAddOn.name,
reason: `Required by ${suggestion.name}`,
confidence: 'high',
category: getCategoryFromType(requiredAddOn.type),
})
}
}
}
}
}
return {
suggestions: suggestions.slice(0, 10),
reasoning: reasons.join(' '),
}
}