"use strict";exports.id=2181,exports.ids=[2181],exports.modules={24562:(a,b,c)=>{c.a(a,async(a,d)=>{try{c.r(b),c.d(b,{STAGE_KIND:()=>q,executeStage:()=>n});var e=c(72326),f=c(34255),g=c(97190),h=c(9940),i=c(73899),j=a([e,f,g,h]);async function k(a,b,c){if("completeness-agent"===a){let a=[];b?.applicant_full_name||a.push("applicant_full_name"),b?.passport_number||a.push("passport_number"),b?.email||a.push("email"),b?.proposed_company_name||a.push("proposed_company_name"),b?.business_activity||a.push("business_activity"),b?.shareholders&&Array.isArray(b.shareholders)&&0!==b.shareholders.length||a.push("shareholders");let c=0===a.length?"pass":"needs_info";return{outcome:c,missing_fields:a,warnings:[],summary:"pass"===c?"Intake is complete. All required fields present.":`Intake is missing required fields: ${a.join(", ")}.`}}if("kyc-risk-verifier"===a){let a=String(b?.proposed_company_name||"").toLowerCase(),c=Math.min(.7,Math.max(.05,a.length%7*.05+.1)),d=c<.3?"auto_pass":c<.6?"manual_review":"reject";return{risk_score:Number(c.toFixed(2)),flags:c>=.3?["demo:moderate-risk-name-length"]:[],sanctioned_list_hits:[],pep_check:"clear",rationale:"Demo mode: risk score is a deterministic function of the intake payload — no external KYC services were contacted.",recommended_action:d}}if("regulatory-drafter"===a)return{draft_kind:"memorandum_of_association",summary:`Draft MOA for ${b?.proposed_company_name||"the entity"} (demo).`,sections:["name","objects","share-capital","shareholders","directors"]};if("mines-valuation-analyst"===a){let a=(0,i.oi)(b?.commodity_price_override,b?.commodity_price_override||{}),c=await (0,g.R)({project_id:b?.project_id,force_tier:b?.force_tier==="auto"?null:b?.force_tier,commodity_prices_json:a}),d=c?.artifact_id?(0,f.getDb)().prepare("SELECT kind, title FROM artifacts WHERE id = ?").get(c.artifact_id):null;return{...c,outcome:c?.status==="ok"?"pass":"fail",artifact_kind:d?.kind||null,artifact_title:d?.title||null}}return{outcome:"pass",summary:`No synthetic output configured for ${a} — defaulting to pass.`}}function l(a,b){return a&&b&&(0,f.getDb)().prepare(`
    SELECT a.* FROM agent_template_installations i
    JOIN agents a ON a.id = i.agent_id
    WHERE i.template_slug = ? AND i.project_id = ? AND i.status = 'active'
    ORDER BY i.installed_at DESC LIMIT 1
  `).get(a,b)||null}function m({agent_id:a,case_id:b,stage_slug:c,output:d,error:e=null}){let g=(0,f.getDb)(),h=(0,f.newId)("agentrun"),i=(0,f.nowIso)();try{g.prepare(`
      INSERT INTO agent_runs (id, agent_id, trigger_source, trigger_detail, status, started_at, finished_at, error, result_json, created_at, updated_at)
      VALUES (?, ?, 'process_stage', ?, ?, ?, ?, ?, ?, ?, ?)
    `).run(h,a||"process-engine",`case:${b}/stage:${c}`,e?"failed":"complete",i,i,e,d?JSON.stringify(d):null,i,i)}catch{return null}return h}async function n({case:a,stage:b}){let c,d=b.config||{},f=d.agent_template_slug,g=a.intake_payload||{},h=a.context||{};if("1"===process.env.MADARX_PROCESS_FAKE_AGENT){let c=await k(f,g,h),d=m({agent_id:l(f,a.project_id)?.id||null,case_id:a.id,stage_slug:b.slug,output:c});return o(p({slug:f,stage:b,output:c,agent_run_id:d}),{caseRow:a,stage:b,output:c,agentSlug:f})}let i=l(f,a.project_id);if(!i)return{kind:"fail",error:`agent template "${f}" is not installed in any project`,output:{error:"agent_not_installed",agent_template_slug:f}};let j=d.prompt||"You are a process-stage verifier. Return strict JSON.",n=JSON.stringify({intake:g,prior_stages:h});try{c=await (0,e.d)({agent:i,systemPrompt:j,userMessage:n,maxTokens:2048})}catch(a){return{kind:"fail",error:`agent engine failed: ${a?.message||a}`,output:{error:String(a?.message||a)}}}let q=function(a){if(!a||"string"!=typeof a)return null;let b=a.indexOf("{");if(-1===b)return null;let c=0;for(let d=b;d<a.length;d++){let e=a[d];if("{"===e)c++;else if("}"===e&&(c--,0===c)){let c=a.slice(b,d+1);try{return JSON.parse(c)}catch{break}}}return null}(c?.text||c?.content||c||"");if(!q)return{kind:"fail",error:"agent did not return JSON",output:{error:"invalid_agent_output",raw:String(c).slice(0,2e3)}};let r=m({agent_id:i.id,case_id:a.id,stage_slug:b.slug,output:q});return o(p({slug:f,stage:b,output:q,agent_run_id:r}),{caseRow:a,stage:b,output:q,agentSlug:f})}function o(a,{caseRow:b,stage:c,output:d,agentSlug:e}){if(!a||!d)return a;let f=(0,h.$3)({caseRow:b,stage:c,output:d,agentSlug:e});if(!f)return a;let g=Array.isArray(a.artifact_ids)?[...a.artifact_ids]:[];return g.push(f),{...a,artifact_ids:g}}function p({slug:a,stage:b,output:c,agent_run_id:d}){if((b.config||{}).on_outcome,"completeness-agent"===a)return c?.outcome==="pass"?{kind:"pass",output:c,agent_run_id:d}:{kind:"fail",output:c,agent_run_id:d,error:"incomplete_intake"};if("regulatory-drafter"===a)return c&&"object"==typeof c?{kind:"pass",output:c,agent_run_id:d}:{kind:"fail",output:c,agent_run_id:d,error:"drafter_missing_output"};if("kyc-risk-verifier"===a){let a=c?.recommended_action;return"auto_pass"===a||"manual_review"===a?{kind:"pass",output:c,agent_run_id:d}:{kind:"fail",output:c,agent_run_id:d,error:"reject"===a?"kyc_reject":"kyc_unrecognized_action"}}return"mines-valuation-analyst"===a?c?.status==="ok"&&c?.artifact_id?{kind:"pass",output:c,agent_run_id:d,artifact_ids:[c.artifact_id]}:{kind:"fail",output:c,agent_run_id:d,error:c?.error||"mines_valuation_failed"}:c?.outcome==="pass"?{kind:"pass",output:c,agent_run_id:d}:{kind:"fail",output:c,agent_run_id:d,error:c?.error||c?.outcome?`agent_returned_${c?.outcome||"unknown"}`:"agent_missing_outcome"}}[e,f,g,h]=j.then?(await j)():j;let q="agent_run";d()}catch(a){d(a)}})}};