Skip to content

Commit c9df032

Browse files
committed
feat: dedicated HR automation AI generator endpoint
- Add /ai/hr-automation-generator with HR-specific schema (9 triggers, 10 actions) - Backend handles system prompt, JSON parsing, and module tagging - Wire HR frontend to use dedicated endpoint instead of generic /ai/chat - Use configurable workerUrl from SIMPATICO_CONFIG instead of hardcoded URL - Backend enforces module:'hr' tag on generated rules automatically
1 parent 826d080 commit c9df032

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

backend/simpatico-ats.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ route("POST", "/ai/employee-insight", handleEmployeeInsight);
10431043
route("POST", "/ai/sentiment", handleSentimentAnalysis);
10441044
route("POST", "/ai/interview-question", handleInterviewQuestion);
10451045
route("POST", "/ai/ats-generator", handleATSGenerator);
1046+
route("POST", "/ai/hr-automation-generator", handleHRAutomationGenerator);
10461047
route("POST", "/ai/generate-assessment", handleGenerateAssessment);
10471048
route("POST", "/ai/assessments", handleSaveAssessment);
10481049
route("POST", "/ai/generate-course", handleGenerateCourse);
@@ -3939,6 +3940,49 @@ Include 2-4 logical actions. Keep it practical for enterprise HR.`;
39393940
}
39403941
}
39413942

3943+
async function handleHRAutomationGenerator(request, env, ctx) {
3944+
const { query } = await safeJson(request);
3945+
if (!query) throw new ValidationError("query is required");
3946+
if (!env.AI) throw new ServiceUnavailableError("AI");
3947+
3948+
const systemPrompt = `You are an enterprise HR automation expert. Given a description, output ONLY valid JSON (no markdown, no backticks) for an HR workflow automation rule.
3949+
Schema:
3950+
{
3951+
"name": "short rule name",
3952+
"desc": "one sentence description",
3953+
"trigger": "one of: employee_joined|employee_offboarded|leave_submitted|leave_approved|probation_ending|work_anniversary|birthday|performance_due|schedule",
3954+
"cond_dept": "department filter or empty",
3955+
"cond_level": "junior|mid|senior|exec or empty",
3956+
"actions": [{"type": "send_email|send_slack|create_task|auto_approve|update_status|assign_manager|trigger_webhook|generate_doc|add_to_calendar|send_reminder", "target": "target email or channel", "msg": "message content"}]
3957+
}
3958+
Include 2-3 logical actions. Keep it practical for enterprise HR operations.
3959+
Examples of good rules:
3960+
- On employee_joined: send welcome email + create onboarding task + assign buddy
3961+
- On leave_submitted: auto-approve if days <= 2 + notify manager
3962+
- On probation_ending: create review task + send reminder to manager
3963+
- On birthday: send celebration email + add to calendar`;
3964+
3965+
const result = await runLLM(env, ctx.tenantId, [
3966+
{ role: "system", content: systemPrompt },
3967+
{ role: "user", content: query },
3968+
], 1024);
3969+
3970+
try {
3971+
const text = result.response.replace(/```json|```/g, "").trim();
3972+
const rule = JSON.parse(text);
3973+
// Enforce module tag
3974+
rule.module = "hr";
3975+
return apiResponse({ rule });
3976+
} catch (e) {
3977+
throw new AppError(
3978+
"Failed to parse AI-generated HR rule",
3979+
HTTP.SERVER_ERROR,
3980+
"PARSE_ERROR",
3981+
result.response,
3982+
);
3983+
}
3984+
}
3985+
39423986
async function handleGenerateAssessment(request, env, ctx) {
39433987
requireRole(
39443988
ctx,

hr-automation.html

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,25 +1167,18 @@ <h2>✨ AI-Generated Automation</h2>
11671167
Include 1-4 actions that make sense for the described workflow.`;
11681168

11691169
try {
1170-
var resp = await fetch('https://simpatico-hr-ats.simpaticohrconsultancy.workers.dev/ai/chat', {
1170+
var workerUrl = (window.SIMPATICO_CONFIG && window.SIMPATICO_CONFIG.workerUrl)
1171+
|| 'https://simpatico-hr-ats.simpaticohrconsultancy.workers.dev';
1172+
var resp = await fetch(workerUrl + '/ai/hr-automation-generator', {
11711173
method: 'POST',
11721174
headers: {
11731175
'Content-Type': 'application/json',
11741176
'X-Tenant-ID': typeof getCompanyId === 'function' ? getCompanyId() : 'SIMP_PRO_MAIN'
11751177
},
1176-
body: JSON.stringify({
1177-
messages: [
1178-
{ role: 'system', content: systemPrompt },
1179-
{ role: 'user', content: prompt }
1180-
]
1181-
})
1178+
body: JSON.stringify({ query: prompt })
11821179
});
11831180
var data = await resp.json();
1184-
var text = data.data ? data.data.response : data.response;
1185-
1186-
// Parse JSON
1187-
var jsonText = text.replace(/```json|```/g, '').trim();
1188-
var rule = JSON.parse(jsonText);
1181+
var rule = data.data ? data.data.rule : data.rule;
11891182

11901183
pendingAiRule = rule;
11911184
rule.is_ai = true;

0 commit comments

Comments
 (0)