-
Notifications
You must be signed in to change notification settings - Fork 552
Expand file tree
/
Copy pathvite.config.mjs
More file actions
95 lines (89 loc) · 3.13 KB
/
Copy pathvite.config.mjs
File metadata and controls
95 lines (89 loc) · 3.13 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
import { resolve } from 'path';
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import commonjs from 'vite-plugin-commonjs';
// Mocks the agent-studio response shape `connectChatPageSuggestions` parses
// (`data.parts[1].text` = JSON-stringified string array). Reactive to query
// and first-hit category so refinements visibly drive the pills.
function chatPageSuggestionsMockPlugin() {
return {
name: 'chat-page-suggestions-mock',
configureServer(server) {
server.middlewares.use('/api/chat-page-suggestions', async (req, res, next) => {
if (req.method !== 'POST') {
next();
return;
}
// Tune via `?delay=N` (ms). Default 800ms so the loading skeleton is
// clearly visible on every refetch (initial mount + each refinement
// change).
const url = new URL(req.url ?? '', 'http://localhost');
const delayParam = Number(url.searchParams.get('delay'));
const delayMs =
Number.isFinite(delayParam) && delayParam >= 0 ? delayParam : 800;
const chunks = [];
for await (const chunk of req) chunks.push(chunk);
const raw = Buffer.concat(chunks).toString('utf8');
let input = {};
try {
const body = JSON.parse(raw);
const text = body?.messages?.[0]?.parts?.[0]?.text;
if (typeof text === 'string') input = JSON.parse(text);
} catch {
// ignore — fall through with empty input
}
const query = (input.query || '').trim();
const category = input.hitsSample?.[0]?.categories?.[0];
const max = Math.max(1, Math.min(input.maxSuggestions ?? 4, 8));
const pool = [];
if (query) {
pool.push(
`Compare the top ${query} options`,
`What should I look for in a ${query}?`,
`Recommend a ${query} under $500`,
`Show me bestsellers for "${query}"`
);
}
if (category) {
pool.push(
`What's new in ${category}?`,
`Top-rated ${category} this month`,
`Help me choose a ${category}`
);
}
pool.push(
'Help me find what I need',
'What are people buying right now?',
'Suggest something for a gift'
);
const suggestions = Array.from(new Set(pool)).slice(0, max);
await new Promise((resolve) => setTimeout(resolve, delayMs));
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({
id: `dummy-${Date.now()}`,
role: 'assistant',
parts: [
{ type: 'reasoning', text: 'mocked' },
{ type: 'text', text: JSON.stringify(suggestions) },
],
})
);
});
},
};
}
export default defineConfig({
plugins: [commonjs(), react(), chatPageSuggestionsMockPlugin()],
build: {
commonjsOptions: {
requireReturnsDefault: 'preferred',
},
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
products: resolve(__dirname, 'products.html'),
},
},
},
});