1
2
3
4
5
6
7
8import http from 'node:http';
9import { Actor, log } from 'apify';
10
11import { createNormalizer } from './normalizer.js';
12import { preprocess } from './preprocessor.js';
13
14
15
16
17
18
19function normalizeItems(items) {
20 return items.map((item) => {
21 if (typeof item === 'string') {
22 return item;
23 }
24 if (item && typeof item === 'object' && 'input' in item) {
25 return String(item.input ?? '');
26 }
27 return String(item ?? '');
28 });
29}
30
31await Actor.init();
32
33if (Actor.config.get('metaOrigin') === 'STANDBY') {
34 startStandbyServer();
35} else {
36 await runBatchMode();
37 await Actor.exit();
38}
39
40
41
42
43
44function startStandbyServer() {
45 const port = Actor.config.get('containerPort');
46
47 const server = http.createServer(async (req, res) => {
48
49 if (req.headers['x-apify-container-server-readiness-probe']) {
50 res.writeHead(200, { 'Content-Type': 'application/json' });
51 res.end(JSON.stringify({ status: 'ready' }));
52 return;
53 }
54
55
56 if (req.method !== 'GET') {
57 res.writeHead(405, { 'Content-Type': 'application/json' });
58 res.end(JSON.stringify({ error: 'Method not allowed. Use GET.' }));
59 return;
60 }
61
62
63 const reqUrl = new URL(req.url, `http://${req.headers.host}`);
64 if (!reqUrl.searchParams.get('input')) {
65 res.writeHead(200, { 'Content-Type': 'text/html' });
66 res.end(getLandingPageHtml());
67 return;
68 }
69
70 try {
71 const result = await handleStandbyRequest(reqUrl, req);
72 await Actor.pushData(result);
73
74 res.writeHead(200, { 'Content-Type': 'application/json' });
75 res.end(JSON.stringify(result));
76 } catch (err) {
77 const statusCode = err.statusCode || 500;
78 res.writeHead(statusCode, { 'Content-Type': 'application/json' });
79 res.end(JSON.stringify({ error: err.message }));
80 }
81 });
82
83 server.listen(port, () => {
84 console.log(`Superclean Job Titles Standby server listening on port ${port}`);
85 });
86}
87
88async function handleStandbyRequest(reqUrl, req) {
89 const input = reqUrl.searchParams.get('input');
90
91 if (!input) {
92 const err = new Error("Provide an 'input' query parameter with the job title to clean.");
93 err.statusCode = 400;
94 throw err;
95 }
96
97 const style = reqUrl.searchParams.get('style') || 'standardized';
98 const model = reqUrl.searchParams.get('model') || 'openrouter/auto';
99 const openRouterApiKey = req.headers['x-openrouter-key'] || null;
100
101 const validStyles = ['standardized', 'abbreviated', 'simple'];
102 if (!validStyles.includes(style)) {
103 const err = new Error(`Invalid style "${style}". Must be one of: ${validStyles.join(', ')}`);
104 err.statusCode = 400;
105 throw err;
106 }
107
108 const trimmed = input.trim();
109 if (!trimmed) {
110 return { id: 1, input, output: '', confidence: 0 };
111 }
112
113
114 const preprocessResult = preprocess(trimmed);
115
116
117 if (preprocessResult.isNonEnglish) {
118 return {
119 id: 1,
120 input: trimmed,
121 output: preprocessResult.extractedEnglish || trimmed,
122 confidence: 0,
123 };
124 }
125
126
127 const item = {
128 id: 1,
129 input: trimmed,
130 preProcessed: preprocessResult.value,
131 isNonEnglish: false,
132 extractedEnglish: preprocessResult.extractedEnglish,
133 detectedSeniority: preprocessResult.detectedSeniority,
134 };
135
136 const normalizer = createNormalizer(model, style, openRouterApiKey);
137 const normalizedItems = await normalizer.normalize([item]);
138
139 if (normalizedItems.length === 0) {
140 return { id: 1, input: trimmed, output: trimmed, confidence: 0 };
141 }
142
143 return {
144 id: 1,
145 input: trimmed,
146 output: normalizedItems[0].normalized,
147 confidence: Math.round(normalizedItems[0].confidence * 100) / 100,
148 };
149}
150
151
152
153
154
155async function runBatchMode() {
156
157 const input = await Actor.getInput();
158 let { items: rawItems, item } = input || {};
159
160
161 if (typeof item === 'string' && item.trim()) {
162 if (!Array.isArray(rawItems)) rawItems = [];
163 rawItems = [item.trim(), ...rawItems];
164 }
165
166
167 if (typeof rawItems === 'string') rawItems = [rawItems];
168
169
170 if (!rawItems || !Array.isArray(rawItems) || rawItems.length === 0) {
171 await Actor.fail('Input required: provide "items" (array) or "item" (single string). Example: {"items": ["Senior Software Engineer"]}');
172 return;
173 }
174
175
176 const style = input.style ?? 'standardized';
177 const model = input.model ?? 'openrouter/auto';
178 const openRouterApiKey = input.openRouterApiKey ?? null;
179
180
181 const items = normalizeItems(rawItems);
182
183 log.info(`Starting normalization of ${items.length} job titles`, {
184 style,
185 model,
186 });
187
188
189 const processedItems = items.map((item, index) => {
190
191 const value = typeof item === 'string' ? item : String(item || '');
192 const id = index + 1;
193
194
195 if (!value.trim()) {
196 log.warning(`Skipping invalid item at index ${index}`, { item });
197 return null;
198 }
199
200
201 const preprocessResult = preprocess(value);
202
203 return {
204 id,
205 input: value,
206 preProcessed: preprocessResult.value,
207 isNonEnglish: preprocessResult.isNonEnglish,
208 extractedEnglish: preprocessResult.extractedEnglish,
209 detectedSeniority: preprocessResult.detectedSeniority,
210 };
211 });
212
213
214 const validItems = processedItems.filter((item) => item !== null);
215
216 if (validItems.length === 0) {
217 throw new Error('No valid items to process after input validation');
218 }
219
220
221 const englishItems = validItems.filter((item) => !item.isNonEnglish);
222 const nonEnglishItems = validItems.filter((item) => item.isNonEnglish);
223
224 if (nonEnglishItems.length > 0) {
225 log.info(`Flagging ${nonEnglishItems.length} non-English items for review`);
226 }
227
228 log.info(`Pre-processed ${validItems.length} items (${englishItems.length} English, ${nonEnglishItems.length} non-English)`);
229
230
231 let normalizedItems = [];
232 if (englishItems.length > 0) {
233 const normalizer = createNormalizer(model, style, openRouterApiKey);
234 normalizedItems = await normalizer.normalize(englishItems);
235 }
236
237
238 const englishResults = normalizedItems.map((item) => ({
239 id: item.id,
240 input: item.input,
241 output: item.normalized,
242 confidence: Math.round(item.confidence * 100) / 100,
243 }));
244
245 const nonEnglishResults = nonEnglishItems.map((item) => ({
246 id: item.id,
247 input: item.input,
248 output: item.extractedEnglish || item.input,
249 confidence: 0,
250 }));
251
252 const results = [...englishResults, ...nonEnglishResults];
253
254
255
256 await Actor.pushData(results);
257
258
259 const avgConfidence = results.reduce((sum, r) => sum + r.confidence, 0) / results.length;
260
261 log.info(`Normalization complete`, {
262 totalItems: results.length,
263 averageConfidence: Math.round(avgConfidence * 100) / 100,
264 });
265}
266
267
268
269
270
271function getLandingPageHtml() {
272 return `<!DOCTYPE html>
273<html lang="en">
274<head>
275<meta charset="utf-8">
276<meta name="viewport" content="width=device-width, initial-scale=1">
277<title>Superclean Job Titles — Superlative</title>
278<link rel="preconnect" href="https://fonts.googleapis.com">
279<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
280<link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;700&family=Space+Mono:wght@400;700&display=swap" rel="stylesheet">
281<style>
282:root{--color-bg:#0a0e14;--color-surface:#12171f;--color-border:#1e2632;--color-text:#e8eaed;--color-text-muted:#8b95a5;--color-accent:#4ade80;--color-accent-dim:rgba(74,222,128,0.15);--font-display:'Space Mono',monospace;--font-body:'DM Sans',sans-serif}
283*{margin:0;padding:0;box-sizing:border-box}
284body{font-family:var(--font-body);background:var(--color-bg);color:var(--color-text);line-height:1.6;min-height:100vh;overflow-x:hidden}
285a{color:var(--color-accent);text-decoration:none;transition:color .2s ease}
286a:hover{color:#6ee7a0}
287.grid-bg{position:fixed;top:0;left:0;width:100%;height:100%;background-image:linear-gradient(var(--color-border) 1px,transparent 1px),linear-gradient(90deg,var(--color-border) 1px,transparent 1px);background-size:60px 60px;opacity:.3;pointer-events:none;z-index:0}
288.accent-line{position:fixed;top:0;left:0;width:100%;height:2px;background:linear-gradient(90deg,transparent 0%,var(--color-accent) 50%,transparent 100%);animation:shimmer 3s ease-in-out infinite;z-index:10}
289@keyframes shimmer{0%,100%{transform:translateX(-100%)}50%{transform:translateX(100%)}}
290@keyframes fadeUp{from{opacity:0;transform:translateY(20px)}to{opacity:1;transform:translateY(0)}}
291.container{position:relative;z-index:1;max-width:900px;margin:0 auto;padding:0 24px}
292header{padding:32px 0}
293.logo{font-family:var(--font-display);font-size:14px;font-weight:700;letter-spacing:.1em;text-transform:uppercase;color:var(--color-text);display:flex;align-items:center;gap:10px;text-decoration:none}
294.logo:hover{color:var(--color-text)}
295.logo-mark{width:28px;height:28px;background:var(--color-accent);display:flex;align-items:center;justify-content:center}
296.logo-mark svg{width:16px;height:16px}
297.hero{padding:80px 0 60px;animation:fadeUp .8s ease-out}
298.hero-label{font-family:var(--font-display);font-size:12px;font-weight:400;letter-spacing:.15em;text-transform:uppercase;color:var(--color-accent);margin-bottom:24px;display:flex;align-items:center;gap:12px}
299.hero-label::before{content:'';width:24px;height:1px;background:var(--color-accent)}
300h1{font-family:var(--font-display);font-size:clamp(28px,5vw,44px);font-weight:700;line-height:1.15;margin-bottom:20px;letter-spacing:-.02em}
301h1 .highlight{color:var(--color-accent)}
302.hero-desc{font-size:18px;color:var(--color-text-muted);max-width:540px;margin-bottom:28px;line-height:1.7}
303.hero-link{font-family:var(--font-display);font-size:13px;letter-spacing:.05em;color:var(--color-accent);display:inline-flex;align-items:center;gap:6px}
304.section{padding:60px 0;border-top:1px solid var(--color-border);animation:fadeUp .8s ease-out .2s backwards}
305.section-label{font-family:var(--font-display);font-size:11px;font-weight:400;letter-spacing:.2em;text-transform:uppercase;color:var(--color-text-muted);margin-bottom:24px}
306h2{font-family:var(--font-display);font-size:20px;font-weight:700;margin-bottom:16px;letter-spacing:-.01em}
307p,li{font-size:15px;color:var(--color-text-muted);line-height:1.7}
308li{margin-bottom:6px}
309ul{margin:.5rem 0 1rem 1.25rem}
310strong{color:var(--color-text);font-weight:600}
311pre{background:var(--color-surface);color:var(--color-text);padding:16px 20px;overflow-x:auto;margin:12px 0 16px;font-family:var(--font-display);font-size:13px;line-height:1.6;border:1px solid var(--color-border)}
312code{font-family:var(--font-display);font-size:13px}
313p code,li code,td code{background:var(--color-accent-dim);color:var(--color-accent);padding:2px 6px}
314table{width:100%;border-collapse:collapse;margin:12px 0 16px;font-size:14px}
315th,td{border:1px solid var(--color-border);padding:10px 14px;text-align:left}
316th{background:var(--color-surface);color:var(--color-text-muted);font-family:var(--font-display);font-size:12px;font-weight:400;letter-spacing:.05em;text-transform:uppercase}
317td{color:var(--color-text-muted)}
318.badge{display:inline-block;background:var(--color-accent-dim);color:var(--color-accent);padding:3px 8px;font-family:var(--font-display);font-size:11px;font-weight:400;letter-spacing:.05em}
319.note{font-size:13px;color:var(--color-text-muted);margin-top:8px}
320.product-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:16px;margin-top:24px}
321.product-card{background:var(--color-surface);border:1px solid var(--color-border);padding:24px;transition:all .3s ease;text-decoration:none;color:inherit;display:block}
322.product-card:hover{border-color:var(--color-accent);transform:translateY(-2px)}
323.product-name{font-family:var(--font-display);font-size:14px;font-weight:700;margin-bottom:8px;color:var(--color-text);display:flex;align-items:center;gap:8px}
324.product-name .status{font-size:9px;font-weight:400;padding:3px 6px;background:var(--color-accent-dim);color:var(--color-accent);letter-spacing:.1em}
325.product-desc{font-size:14px;color:var(--color-text-muted);line-height:1.5}
326footer{padding:48px 0;border-top:1px solid var(--color-border)}
327.footer-content{display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:24px}
328.footer-text{font-size:13px;color:var(--color-text-muted)}
329.footer-links{display:flex;gap:24px}
330.footer-links a{font-family:var(--font-display);font-size:12px;color:var(--color-text-muted);letter-spacing:.05em}
331.footer-links a:hover{color:var(--color-accent)}
332@media(max-width:600px){.hero{padding:60px 0 40px}.section{padding:40px 0}.footer-content{flex-direction:column;align-items:flex-start}pre{font-size:12px;padding:12px 14px}}
333</style>
334</head>
335<body>
336<div class="grid-bg"></div>
337<div class="accent-line"></div>
338
339<div class="container">
340 <header>
341 <a href="https://apify.com/superlativetech?fpr=8e9l1" class="logo">
342 <div class="logo-mark">
343 <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
344 <polyline points="4 17 10 11 4 5"></polyline>
345 <line x1="12" y1="19" x2="20" y2="19"></line>
346 </svg>
347 </div>
348 Superlative
349 </a>
350 </header>
351
352 <section class="hero">
353 <div class="hero-label">Superclean</div>
354 <h1>Job <span class="highlight">Titles</span></h1>
355 <p class="hero-desc">Instant job title normalization. AI-powered cleaning for CRM, HR systems, and lead scoring. Sub-second responses via Standby API.</p>
356 <a href="https://apify.com/superlativetech/superclean-job-titles?fpr=8e9l1" class="hero-link">View on Apify Store →</a>
357 </section>
358
359 <section class="section">
360 <div class="section-label">Features</div>
361 <h2>What this does</h2>
362 <ul>
363 <li><strong>AI-powered cleaning</strong> — LLM normalizes job titles with context awareness</li>
364 <li><strong>Three styles</strong> — Standardized, Abbreviated, Simple</li>
365 <li><strong>Expands abbreviations</strong> — Sr. → Senior, VP → Vice President</li>
366 <li><strong>BYOK support</strong> — Use your own OpenRouter API key via header</li>
367 <li><strong>Batch mode</strong> — Process hundreds of titles via the standard Actor run</li>
368 </ul>
369 </section>
370
371 <section class="section">
372 <div class="section-label">Getting Started</div>
373 <h2>Quick start</h2>
374 <p>Replace <code>YOUR_TOKEN</code> with your Apify API token.</p>
375
376 <p style="margin-top:16px"><strong>Standardized style:</strong></p>
377 <pre>curl "https://superlativetech--superclean-job-titles.apify.actor?token=YOUR_TOKEN&input=Sr.+Software+Eng.+-+Platform+Team"</pre>
378
379 <p><strong>Simple style:</strong></p>
380 <pre>curl "https://superlativetech--superclean-job-titles.apify.actor?token=YOUR_TOKEN&input=Sr.+Software+Eng.+-+Platform+Team&style=simple"</pre>
381
382 <p><strong>With your own OpenRouter key:</strong></p>
383 <pre>curl -H "X-OpenRouter-Key: sk-or-..." "https://superlativetech--superclean-job-titles.apify.actor?token=YOUR_TOKEN&input=VP+of+Mktg"</pre>
384 </section>
385
386 <section class="section">
387 <div class="section-label">Reference</div>
388 <h2>Query parameters</h2>
389 <table>
390 <tr><th>Parameter</th><th>Required</th><th>Description</th></tr>
391 <tr><td><code>input</code></td><td>Yes</td><td>Job title to clean</td></tr>
392 <tr><td><code>style</code></td><td>No</td><td>Output style: <code>standardized</code> (default), <code>abbreviated</code>, or <code>simple</code></td></tr>
393 <tr><td><code>model</code></td><td>No</td><td>LLM model (default: <code>openrouter/auto</code>)</td></tr>
394 <tr><td><code>token</code></td><td>Yes</td><td>Your Apify API token</td></tr>
395 </table>
396 <h2 style="margin-top:24px">Headers</h2>
397 <table>
398 <tr><th>Header</th><th>Required</th><th>Description</th></tr>
399 <tr><td><code>X-OpenRouter-Key</code></td><td>No</td><td>Your OpenRouter API key for BYOK</td></tr>
400 </table>
401 </section>
402
403 <section class="section">
404 <div class="section-label">Response</div>
405 <h2>Response format</h2>
406 <pre>{
407 "id": 1,
408 "input": "Sr. Software Eng. - Platform Team",
409 "output": "Senior Software Engineer",
410 "confidence": 0.9
411}</pre>
412 </section>
413
414 <section class="section">
415 <div class="section-label">Cost</div>
416 <h2>Pricing</h2>
417 <p>Free to use — you only pay standard Apify platform usage.</p>
418 </section>
419
420 <section class="section">
421 <div class="section-label">Auth</div>
422 <h2>Authentication</h2>
423 <p>Authenticate using either method:</p>
424 <ul>
425 <li>Query parameter: <code>?token=YOUR_APIFY_TOKEN</code></li>
426 <li>Header: <code>Authorization: Bearer YOUR_APIFY_TOKEN</code></li>
427 </ul>
428 <p style="margin-top:8px">Get your token from <a href="https://console.apify.com/settings/integrations">Apify Console → Settings → Integrations</a>.</p>
429 </section>
430
431 <section class="section">
432 <div class="section-label">Superlative</div>
433 <h2>More from Superlative</h2>
434 <div class="product-grid">
435 <a href="https://apify.com/superlativetech/superclean-company-names?fpr=8e9l1" class="product-card">
436 <div class="product-name">Company Names <span class="status">Live</span></div>
437 <p class="product-desc">Normalize company names for CRM and cold email outreach.</p>
438 </a>
439 <a href="https://apify.com/superlativetech/superclean-person-names?fpr=8e9l1" class="product-card">
440 <div class="product-name">Person Names <span class="status">Live</span></div>
441 <p class="product-desc">Clean and format person names for personalization.</p>
442 </a>
443 <a href="https://apify.com/superlativetech/superclean-product-names?fpr=8e9l1" class="product-card">
444 <div class="product-name">Product Names <span class="status">Live</span></div>
445 <p class="product-desc">Normalize product and brand names from exports.</p>
446 </a>
447 <a href="https://apify.com/superlativetech/superclean-places?fpr=8e9l1" class="product-card">
448 <div class="product-name">Places <span class="status">Live</span></div>
449 <p class="product-desc">Parse and standardize location strings.</p>
450 </a>
451 <a href="https://apify.com/superlativetech/superclean-urls?fpr=8e9l1" class="product-card">
452 <div class="product-name">URLs <span class="status">Live</span></div>
453 <p class="product-desc">Clean and normalize URLs from lead data.</p>
454 </a>
455 <a href="https://apify.com/superlativetech/superclean-phone-numbers?fpr=8e9l1" class="product-card">
456 <div class="product-name">Phone Numbers <span class="status">Live</span></div>
457 <p class="product-desc">Format and validate phone numbers.</p>
458 </a>
459 <a href="https://apify.com/superlativetech/dns-lookup?fpr=8e9l1" class="product-card">
460 <div class="product-name">Supernet DNS Lookup <span class="status">Live</span></div>
461 <p class="product-desc">Look up DNS records for any domain.</p>
462 </a>
463 <a href="https://apify.com/superlativetech/http-api?fpr=8e9l1" class="product-card">
464 <div class="product-name">HTTP API <span class="status">Live</span></div>
465 <p class="product-desc">General-purpose HTTP request utility.</p>
466 </a>
467 </div>
468 </section>
469
470 <footer>
471 <div class="footer-content">
472 <p class="footer-text">© 2026 Superlative</p>
473 <div class="footer-links">
474 <a href="https://apify.com/superlativetech?fpr=8e9l1">Apify Store</a>
475 </div>
476 </div>
477 </footer>
478</div>
479
480</body>
481</html>`;
482}