-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.ts
More file actions
322 lines (314 loc) · 11.9 KB
/
Copy pathpatterns.ts
File metadata and controls
322 lines (314 loc) · 11.9 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* classify/patterns — PatternDef registry
*
* Canonical list of scaffold patterns. Each entry follows the PatternDef
* interface from ../types with keywords used for vocabulary scoring and
* traits as a string array of capability flags.
*/
import type { PatternDef, PatternName } from '../types';
export type { PatternDef };
// ─── Internal scoring helper ─────────────────────────────────────────────────
// keywordScore lives here so PATTERNS entries can use it in their signal tuples.
export function keywordScore(
text: string,
keywords: string[],
antiKeywords: string[] = []
): number {
let score = 0;
for (const keyword of keywords) {
if (text.includes(keyword)) score += 1;
}
for (const anti of antiKeywords) {
if (text.includes(anti)) score -= 2;
}
return score;
}
// ─── Internal pattern definition (includes scoring, priority, and signal) ────
// This is intentionally not exported so consumers depend only on PatternDef.
export interface ScoredPatternDef extends PatternDef {
/** Source signal label used for classification metadata */
signal: string;
/** Priority tiebreaker when two patterns have the same score */
priority: number;
/** Scoring function — takes lower-cased intention text */
score: (text: string) => number;
/** Trait map used by governance + codegen modules */
traitMap: Record<string, string>;
}
// Payment-action keywords that independently trigger Payment Signal (and are
// excluded from Webhook Signal so an explicit payment context always wins).
// "billing" and "webhook" are deliberately NOT here: they only add to the
// Payment Signal score as a bonus once one of these has already matched (see
// Payment Signal's score() below) — listing them here would let bare
// "billing" or "webhook" fire this pattern on their own again.
const PAYMENT_ACTION_KEYWORDS = ['stripe', 'subscription', 'checkout', 'payment', 'payments'];
export const SCORED_PATTERNS: ScoredPatternDef[] = [
{
name: 'worker' as PatternName,
status: 'ACTIVE',
category: 'SECURITY',
keywords: ['harden', 'hardening', 'governance overlay', 'lovable', 'bolt.new', 'bolt-generated', 'bolt-style', 'lovable-style', 'cursor', 'v0', 'supabase', 'vite'],
traits: ['overlay-doc', 'documented-only', 'no-dispatch', 'doc-trigger'],
signal: 'Hardening Signal',
priority: 110,
score: (text: string) => {
const hardenHits = keywordScore(text, ['harden', 'hardening', 'governance overlay', 'add governance to']);
const stackHits = keywordScore(text, ['lovable', 'bolt.new', 'bolt-generated', 'bolt-style', 'lovable-style', 'cursor', 'v0', 'supabase', 'vite']);
if (hardenHits >= 1 && stackHits >= 1) return hardenHits + stackHits + 2;
return 0;
},
traitMap: {
route_shape: 'overlay-doc',
verification: 'documented-only',
dispatch: 'none',
trigger: 'doc',
framework: 'none',
default_routes: '/health',
pattern_element: 'Overlay',
pattern_category: 'Governance',
pattern_tier: 'approved',
source_pattern: 'hardening-overlay',
},
},
{
name: 'worker' as PatternName,
status: 'ACTIVE',
category: 'INTEGRATION',
keywords: [...PAYMENT_ACTION_KEYWORDS, 'billing', 'webhook'],
traits: ['post-handler', 'hmac-stripe', 'event-router', 'fetch-trigger'],
signal: 'Payment Signal',
priority: 100,
score: (text: string) => {
// "billing" is intentionally excluded from PAYMENT_ACTION_KEYWORDS — it appears in
// architectural contexts ("billing dashboard", "billing management") that are not
// Stripe webhook handlers. Require at least one explicit payment-action keyword
// before scoring billing terms.
const stripeHits = keywordScore(text, PAYMENT_ACTION_KEYWORDS);
if (stripeHits === 0) return 0;
return stripeHits + (text.includes('billing') ? 1 : 0) + (text.includes('webhook') ? 1 : 0);
},
traitMap: {
route_shape: 'post-handler',
verification: 'hmac-stripe',
dispatch: 'event-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/webhook,/health',
pattern_element: 'Event',
pattern_category: 'Integration',
pattern_tier: 'approved',
source_pattern: 'stripe-webhook',
},
},
{
name: 'worker' as PatternName,
status: 'ACTIVE',
category: 'INTEGRATION',
keywords: ['webhook', 'signature verification', 'x-hub-signature', 'github webhook', 'slack webhook', 'twilio webhook'],
traits: ['post-handler', 'hmac-sha256', 'event-router', 'fetch-trigger'],
signal: 'Webhook Signal',
priority: 95,
score: (text: string) => {
// Exclude only if explicit payment-action keywords are present (not "billing" alone —
// see Payment Signal comment; "billing webhook" without Stripe is generic).
if (keywordScore(text, PAYMENT_ACTION_KEYWORDS) >= 1) return 0;
return keywordScore(text, ['webhook', 'signature verification', 'x-hub-signature', 'github webhook', 'slack webhook', 'twilio webhook']);
},
traitMap: {
route_shape: 'post-handler',
verification: 'hmac-sha256',
dispatch: 'event-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/webhook,/health',
pattern_element: 'Event',
pattern_category: 'Integration',
pattern_tier: 'approved',
source_pattern: 'generic-webhook',
},
},
{
name: 'workers-saas' as PatternName,
status: 'ACTIVE',
category: 'COMPUTE',
keywords: ['saas', 'tenant', 'multi-tenant', 'org', 'workspace', 'dashboard', 'analytics', 'user management'],
traits: ['rest', 'jwt-auth', 'resource-router', 'fetch-trigger'],
signal: 'SaaS Signal',
priority: 90,
score: (text: string) => keywordScore(
text,
['saas', 'tenant', 'multi-tenant', 'org', 'workspace', 'dashboard', 'analytics', 'user management'],
),
traitMap: {
route_shape: 'rest',
verification: 'jwt-auth',
dispatch: 'resource-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/health,/auth/login,/organizations,/users',
pattern_element: 'System',
pattern_category: 'Application',
pattern_tier: 'approved',
source_pattern: 'workers-saas',
},
},
{
name: 'durable-object' as PatternName,
status: 'ACTIVE',
category: 'COMPUTE',
keywords: ['durable object', 'websocket', 'real-time', 'collaborative editor', 'live cursor', 'presence'],
traits: ['ws-and-rest', 'session-auth', 'do-stub-router', 'fetch-trigger'],
signal: 'Durable Object Signal',
priority: 85,
score: (text: string) => keywordScore(
text,
['durable object', 'websocket', 'real-time', 'collaborative editor', 'live cursor', 'presence'],
),
traitMap: {
route_shape: 'ws-and-rest',
verification: 'session-auth',
dispatch: 'do-stub-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/room/:id,/health',
pattern_element: 'Object',
pattern_category: 'Stateful',
pattern_tier: 'approved',
source_pattern: 'durable-objects',
},
},
{
name: 'scheduled' as PatternName,
status: 'ACTIVE',
category: 'ASYNC',
keywords: ['cron', 'scheduled', 'daily', 'hourly', 'nightly', 'aggregation job', 'scheduled job'],
traits: ['scheduled-handler', 'no-verification', 'scheduled-dispatch'],
signal: 'Cron Signal',
priority: 85,
score: (text: string) => keywordScore(
text,
['cron', 'scheduled', 'daily', 'hourly', 'nightly', 'aggregation job', 'scheduled job'],
),
traitMap: {
route_shape: 'scheduled-handler',
verification: 'none',
dispatch: 'scheduled',
trigger: 'scheduled',
framework: 'hono',
default_routes: '/health',
pattern_element: 'Job',
pattern_category: 'Worker',
pattern_tier: 'approved',
source_pattern: 'cron-worker',
},
},
{
name: 'mcp-server' as PatternName,
status: 'ACTIVE',
category: 'INTEGRATION',
keywords: ['mcp', 'model context protocol', 'mcp server', 'tool server', 'agent server', 'mcp tool', 'mcp resource', 'mcp protocol'],
traits: ['sse-jsonrpc', 'bearer-auth', 'mcp-protocol-router', 'fetch-trigger'],
signal: 'MCP Signal',
priority: 83,
score: (text: string) => keywordScore(
text,
['mcp', 'model context protocol', 'mcp server', 'tool server', 'agent server', 'mcp tool', 'mcp resource', 'mcp protocol'],
),
traitMap: {
route_shape: 'sse-jsonrpc',
verification: 'bearer-auth',
dispatch: 'mcp-protocol-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/mcp,/mcp/sse,/health',
pattern_element: 'Protocol',
pattern_category: 'Integration',
pattern_tier: 'approved',
source_pattern: 'mcp-server',
},
},
{
name: 'worker' as PatternName,
status: 'ACTIVE',
category: 'COMPUTE',
keywords: ['ai chat', 'chat api', 'chatbot', 'assistant', 'llm', 'prompt', 'conversation', 'completion', 'streams model', 'server-sent event'],
traits: ['streaming', 'session-auth', 'conversation-router', 'fetch-trigger'],
signal: 'AI Signal',
priority: 80,
score: (text: string) => keywordScore(
text,
['ai chat', 'chat api', 'chatbot', 'assistant', 'llm', 'prompt', 'conversation', 'completion', 'streams model', 'server-sent event'],
['rate limit', 'rate limiter', 'cron', 'scheduled', 'email', 'notification', 'image generation', 'image gen', 'queue', 'pipeline', 'mcp', 'model context protocol'],
),
traitMap: {
route_shape: 'streaming',
verification: 'session-auth',
dispatch: 'conversation-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/chat,/sessions,/health',
pattern_element: 'Experience',
pattern_category: 'Application',
pattern_tier: 'approved',
source_pattern: 'ai-chat',
},
},
{
name: 'api' as PatternName,
status: 'ACTIVE',
category: 'COMPUTE',
keywords: ['api', 'rest', 'endpoint', 'route', 'crud', 'resource'],
traits: ['rest', 'bearer-auth', 'resource-router', 'fetch-trigger'],
signal: 'API Signal',
priority: 70,
score: (text: string) => keywordScore(text, ['api', 'rest', 'endpoint', 'route', 'crud', 'resource']),
traitMap: {
route_shape: 'rest',
verification: 'bearer-auth',
dispatch: 'resource-router',
trigger: 'fetch',
framework: 'hono',
default_routes: '/health,/status,/resources,/resources/:id',
pattern_element: 'Service',
pattern_category: 'Application',
pattern_tier: 'approved',
source_pattern: 'rest-api',
},
},
{
name: 'rust-wasm' as PatternName,
status: 'ACTIVE',
category: 'LIBRARY',
keywords: ['rust', 'wasm', 'wasm-pack', 'wasm-bindgen', 'cargo', 'cdylib', 'webassembly', 'crate', 'wasm32'],
traits: ['rust', 'wasm-bindgen', 'dual-target', 'npm-from-wasm-pack', 'no-server'],
signal: 'Rust/WASM Signal',
priority: 90,
score: (text: string) => {
const rustHits = keywordScore(text, ['rust', 'cargo', 'cargo.toml', 'crate', 'cdylib', 'rlib']);
const wasmHits = keywordScore(text, ['wasm', 'wasm-pack', 'wasm-bindgen', 'webassembly', 'wasm32']);
if (rustHits >= 1 || wasmHits >= 1) return rustHits + wasmHits;
return 0;
},
traitMap: {
source_pattern: 'rust-wasm',
build_tool: 'wasm-pack',
publish_target: 'npm-from-pkg',
test_runner: 'wasm-bindgen-test',
pattern_element: 'Library',
pattern_category: 'Library',
pattern_tier: 'approved',
},
},
];
/**
* The canonical list of supported scaffold patterns (PatternDef shape).
* Exported for consumers that only need keywords/traits; use SCORED_PATTERNS
* for internal scoring logic.
*/
export const PATTERNS: PatternDef[] = SCORED_PATTERNS.map(({ name, status, category, keywords, traits }) => ({
name,
status,
category,
keywords,
traits,
}));