-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathwidget-types.js
More file actions
362 lines (344 loc) · 10.9 KB
/
Copy pathwidget-types.js
File metadata and controls
362 lines (344 loc) · 10.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Widget type registry + brand defaults + per-type config validators.
// Both the Studio UI and the public widget runtime import from here.
//
// `status: 'ready'` widgets have a working runtime. `status: 'pending'`
// widgets save their config but the runtime renders a "coming soon" banner
// — Prompts 01-05 flip these to 'ready' as their runtimes ship.
import { z } from 'zod';
export const WIDGET_TYPES = {
turntable: {
label: 'Turntable Showcase',
desc: 'Hero banner — auto-rotate, no UI, just the avatar.',
status: 'ready',
icon: '◎',
},
'animation-gallery': {
label: 'Animation Gallery',
desc: 'Click through every clip on a rigged avatar.',
status: 'ready',
icon: '▶',
},
'talking-agent': {
label: 'Talking Agent',
desc: 'Embodied chat — your agent on your site.',
status: 'ready',
icon: '◐',
},
passport: {
label: 'ERC-8004 Passport',
desc: 'On-chain identity card for any agent.',
status: 'ready',
icon: '◊',
},
'hotspot-tour': {
label: 'Hotspot Tour',
desc: 'Annotated 3D scene with clickable points of interest.',
status: 'ready',
icon: '⌖',
},
'pumpfun-feed': {
label: 'Pump.fun Live Feed',
desc: 'Solana agent narrates live pump.fun claims and graduations.',
status: 'ready',
icon: '✦',
},
'kol-trades': {
label: 'Smart Money Feed',
desc: 'Live buy/sell activity from KOL and whale wallets for a single token.',
status: 'ready',
icon: '◈',
},
'live-trades-canvas': {
label: 'Live Trades Canvas',
desc: 'Particle visualization of live pump.fun buy/sell trades for a token.',
status: 'ready',
icon: '⬡',
},
'bonding-curve': {
label: 'Bonding Curve',
desc: 'Live graduation progress and bonding-curve climb for a pump.fun token.',
status: 'ready',
icon: '◭',
},
};
export const WIDGET_TYPE_KEYS = Object.keys(WIDGET_TYPES);
export const BRAND_DEFAULTS = Object.freeze({
background: '#0a0a0a',
accent: '#ffffff',
caption: '',
showControls: true,
autoRotate: true,
envPreset: 'neutral',
cameraPosition: null,
});
const TYPE_DEFAULTS = {
turntable: { rotationSpeed: 0.5 },
'animation-gallery': { defaultClip: '', loopAll: false, showClipPicker: true },
'talking-agent': {
agentName: '',
agentTitle: 'AI Agent',
avatar: 'embedded',
brainProvider: 'anthropic',
proxyURL: '',
systemPrompt: '',
greeting: 'Hi! Ask me anything.',
temperature: 0.7,
maxTurns: 20,
skills: { speak: true, wave: true, lookAt: true, playClip: true, remember: false },
showChatHistory: true,
voiceInput: true,
voiceOutput: true,
chatPosition: 'right',
poweredByBadge: true,
visitorRateLimit: { msgsPerMinute: 8, msgsPerSession: 50 },
},
passport: {
chain: 'base-sepolia',
agentId: null,
wallet: null,
showReputation: true,
showRecentFeedback: true,
showValidation: false,
showRegistrationJSON: true,
layout: 'portrait',
badgeSize: 'medium',
rotationSpeed: 0.6,
rpcURL: '',
refreshIntervalSec: 60,
showPoweredBy: true,
},
'hotspot-tour': { hotspots: [] },
'pumpfun-feed': {
kind: 'all',
minTier: '',
autoNarrate: true,
maxCards: 8,
},
'kol-trades': {
mint: '',
limit: 20,
refreshMs: 30_000,
},
'live-trades-canvas': {
mint: '',
chain: 'solana',
bg: '#0a0a0a',
minUsd: 0,
},
'bonding-curve': {
mint: '',
network: 'mainnet',
refreshMs: 15_000,
showUsd: true,
},
};
const hexColor = z.string().regex(/^#[0-9a-fA-F]{3,8}$/, 'must be a hex color');
const cameraVec = z.tuple([z.number(), z.number(), z.number()]).nullable();
const envPreset = z.enum(['none', 'neutral', 'venice-sunset', 'footprint-court']);
const brandSchema = z.object({
background: hexColor.default(BRAND_DEFAULTS.background),
accent: hexColor.default(BRAND_DEFAULTS.accent),
caption: z.string().max(280).default(BRAND_DEFAULTS.caption),
showControls: z.boolean().default(BRAND_DEFAULTS.showControls),
autoRotate: z.boolean().default(BRAND_DEFAULTS.autoRotate),
envPreset: envPreset.default(BRAND_DEFAULTS.envPreset),
cameraPosition: cameraVec.default(BRAND_DEFAULTS.cameraPosition),
});
const TYPE_SCHEMAS = {
turntable: brandSchema.extend({
rotationSpeed: z.number().min(0).max(10).default(0.5),
}),
'animation-gallery': brandSchema.extend({
defaultClip: z.string().max(120).default(''),
loopAll: z.boolean().default(false),
showClipPicker: z.boolean().default(true),
}),
'talking-agent': brandSchema
.extend({
agentName: z.string().max(80).default(''),
agentTitle: z.string().max(80).default('AI Agent'),
avatar: z.enum(['embedded', 'chat-only']).default('embedded'),
brainProvider: z
.enum(['none', 'auto', 'anthropic', 'openrouter', 'groq', 'openai', 'watsonx', 'custom'])
.default('anthropic'),
proxyURL: z.string().url().startsWith('https://').or(z.literal('')).default(''),
systemPrompt: z.string().max(4000).default(''),
greeting: z.string().max(280).default('Hi! Ask me anything.'),
temperature: z.number().min(0).max(1).default(0.7),
maxTurns: z.number().int().min(1).max(100).default(20),
skills: z
.object({
speak: z.boolean().default(true),
wave: z.boolean().default(true),
lookAt: z.boolean().default(true),
playClip: z.boolean().default(true),
remember: z.boolean().default(false),
})
.default({
speak: true,
wave: true,
lookAt: true,
playClip: true,
remember: false,
}),
showChatHistory: z.boolean().default(true),
voiceInput: z.boolean().default(true),
voiceOutput: z.boolean().default(true),
chatPosition: z.enum(['right', 'bottom', 'overlay']).default('right'),
poweredByBadge: z.boolean().default(true),
visitorRateLimit: z
.object({
msgsPerMinute: z.number().int().min(1).max(60).default(8),
msgsPerSession: z.number().int().min(1).max(500).default(50),
})
.default({ msgsPerMinute: 8, msgsPerSession: 50 }),
})
.superRefine((cfg, ctx) => {
if (cfg.brainProvider === 'custom' && !cfg.proxyURL) {
ctx.addIssue({
path: ['proxyURL'],
code: z.ZodIssueCode.custom,
message: 'proxyURL is required when brainProvider is "custom"',
});
}
}),
passport: brandSchema.extend({
chain: z.string().min(1).default('base-sepolia'),
agentId: z
.string()
.regex(/^\d+$/, 'agentId must be a uint256 string')
.nullable()
.default(null),
wallet: z
.string()
.regex(/^0x[0-9a-fA-F]{40}$/)
.nullable()
.default(null),
showReputation: z.boolean().default(true),
showRecentFeedback: z.boolean().default(true),
showValidation: z.boolean().default(false),
showRegistrationJSON: z.boolean().default(true),
layout: z.enum(['portrait', 'landscape', 'badge']).default('portrait'),
badgeSize: z.enum(['small', 'medium', 'large']).default('medium'),
rotationSpeed: z.number().min(0).max(10).default(0.6),
rpcURL: z.string().url().startsWith('https://').or(z.literal('')).default(''),
refreshIntervalSec: z.number().int().min(0).max(3600).default(60),
showPoweredBy: z.boolean().default(true),
}),
'hotspot-tour': brandSchema.extend({
hotspots: z
.array(
z.object({
id: z.string().min(1).max(40),
label: z.string().min(1).max(120),
position: z.tuple([z.number(), z.number(), z.number()]),
body: z.string().max(2000).optional(),
}),
)
.max(40)
.default([]),
}),
'pumpfun-feed': brandSchema.extend({
kind: z.enum(['all', 'claims', 'graduations']).default('all'),
minTier: z.enum(['', 'notable', 'influencer', 'mega']).default(''),
autoNarrate: z.boolean().default(true),
maxCards: z.number().int().min(1).max(50).default(8),
}),
'kol-trades': z.object({
mint: z.string().default(''),
limit: z.number().int().min(1).max(100).default(20),
refreshMs: z.number().int().min(1000).max(300_000).default(30_000),
}),
'live-trades-canvas': brandSchema.extend({
mint: z.string().min(1, 'mint is required'),
chain: z.enum(['solana']).default('solana'),
bg: hexColor.default('#0a0a0a'),
minUsd: z.number().min(0).default(0),
}),
'bonding-curve': brandSchema.extend({
mint: z.string().max(64).default(''),
network: z.enum(['mainnet', 'devnet']).default('mainnet'),
refreshMs: z.number().int().min(5_000).max(300_000).default(15_000),
showUsd: z.boolean().default(true),
}),
};
export function defaultConfig(type) {
if (!WIDGET_TYPES[type]) throw new Error(`unknown widget type: ${type}`);
return { ...BRAND_DEFAULTS, ...(TYPE_DEFAULTS[type] || {}) };
}
export function validateConfig(type, config) {
const schema = TYPE_SCHEMAS[type];
if (!schema) throw new Error(`unknown widget type: ${type}`);
const res = schema.safeParse(config || {});
if (!res.success) {
const err = new Error(
res.error.issues.map((i) => `${i.path.join('.') || 'config'}: ${i.message}`).join('; '),
);
err.code = 'validation_error';
throw err;
}
return res.data;
}
export function isReady(type) {
return WIDGET_TYPES[type]?.status === 'ready';
}
// Server-side counterpart — the API can validate the same way.
export const widgetTypeEnum = z.enum(WIDGET_TYPE_KEYS);
export const widgetConfigSchemas = TYPE_SCHEMAS;
// ---------------------------------------------------------------------------
// Chain registry — used by the passport widget to resolve slugs to chainIds,
// pick a public RPC, and build explorer links.
// ---------------------------------------------------------------------------
export const CHAIN_SLUGS = {
ethereum: 1,
optimism: 10,
bsc: 56,
polygon: 137,
base: 8453,
arbitrum: 42161,
sepolia: 11155111,
'base-sepolia': 84532,
'optimism-sepolia': 11155420,
'arbitrum-sepolia': 421614,
'polygon-amoy': 80002,
};
export const PUBLIC_RPCS = {
1: 'https://eth.llamarpc.com',
10: 'https://mainnet.optimism.io',
56: 'https://bsc-dataseed.binance.org',
137: 'https://polygon-rpc.com',
8453: 'https://mainnet.base.org',
42161: 'https://arb1.arbitrum.io/rpc',
11155111: 'https://ethereum-sepolia-rpc.publicnode.com',
84532: 'https://sepolia.base.org',
11155420: 'https://sepolia.optimism.io',
421614: 'https://sepolia-rollup.arbitrum.io/rpc',
80002: 'https://rpc-amoy.polygon.technology',
};
export const EXPLORERS = {
1: 'https://etherscan.io',
10: 'https://optimistic.etherscan.io',
56: 'https://bscscan.com',
137: 'https://polygonscan.com',
8453: 'https://basescan.org',
42161: 'https://arbiscan.io',
11155111: 'https://sepolia.etherscan.io',
84532: 'https://sepolia.basescan.org',
11155420: 'https://sepolia-optimism.etherscan.io',
421614: 'https://sepolia.arbiscan.io',
80002: 'https://amoy.polygonscan.com',
};
/** Resolve a chain reference (slug or numeric id or numeric string) to chainId. */
export function resolveChainId(ref) {
if (typeof ref === 'number') return ref;
if (typeof ref !== 'string') return null;
const slug = ref.toLowerCase();
if (CHAIN_SLUGS[slug]) return CHAIN_SLUGS[slug];
const n = Number(slug);
return Number.isFinite(n) ? n : null;
}
/** Pretty display name for a chainId. */
export function chainLabel(chainId) {
const entry = Object.entries(CHAIN_SLUGS).find(([, v]) => v === chainId);
return entry ? entry[0] : `chain-${chainId}`;
}