@open-mercato/core 0.6.3-develop.3484.1.1427e58147 → 0.6.3-develop.3491.1.4700ef658a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/modules/customers/ai-agents.js +10 -1
- package/dist/modules/customers/ai-agents.js.map +2 -2
- package/dist/modules/customers/ai-tools/settings-pack.js +55 -1
- package/dist/modules/customers/ai-tools/settings-pack.js.map +2 -2
- package/dist/modules/messages/api/route.js +2 -2
- package/dist/modules/messages/api/route.js.map +2 -2
- package/dist/modules/messages/data/validators.js +6 -5
- package/dist/modules/messages/data/validators.js.map +2 -2
- package/dist/modules/query_index/di.js +8 -6
- package/dist/modules/query_index/di.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/customers/ai-agents.ts +10 -1
- package/src/modules/customers/ai-tools/settings-pack.ts +62 -1
- package/src/modules/messages/api/route.ts +2 -2
- package/src/modules/messages/data/validators.ts +6 -5
- package/src/modules/query_index/di.ts +9 -7
package/.turbo/turbo-build.log
CHANGED
|
@@ -285,8 +285,12 @@ const DEAL_ANALYZER_PROMPT_SECTIONS = [
|
|
|
285
285
|
"2. customers.update_deal_stage \u2014 propose a stage move for a stalled high-value deal.",
|
|
286
286
|
" The runtime intercepts this via the pending-action gate; do NOT claim the change",
|
|
287
287
|
" is saved until the mutation-result-card arrives.",
|
|
288
|
+
" When moving to a named pipeline stage, first call customers.list_pipeline_stages",
|
|
289
|
+
" and pass the matching UUID as toPipelineStageId. Only use toStage for top-level",
|
|
290
|
+
" status slugs such as open, won, or lost.",
|
|
288
291
|
"Secondary read tools (use when you need more detail):",
|
|
289
292
|
"- customers.list_deals, customers.get_deal, customers.list_activities",
|
|
293
|
+
"- customers.list_pipeline_stages",
|
|
290
294
|
"- search.hybrid_search, meta.describe_agent"
|
|
291
295
|
].join("\n")
|
|
292
296
|
},
|
|
@@ -327,7 +331,10 @@ const DEAL_ANALYZER_PROMPT_SECTIONS = [
|
|
|
327
331
|
"Lead with a one-paragraph summary of the deal portfolio health, then emit one",
|
|
328
332
|
"deal card per at-risk deal (sorted by health score ascending). After the cards,",
|
|
329
333
|
"propose exactly one stage move for the highest-value stalled deal and call",
|
|
330
|
-
"customers.update_deal_stage.
|
|
334
|
+
"customers.update_deal_stage. If the recommended stage is a pipeline stage label",
|
|
335
|
+
"rather than a status slug, resolve it with customers.list_pipeline_stages first;",
|
|
336
|
+
"do not ask the operator to paste a stage id unless no matching stage exists.",
|
|
337
|
+
"Then finish with a short conclusion naming the",
|
|
331
338
|
"highest-value stalled deal, the recommended move, and the approval status."
|
|
332
339
|
].join("\n")
|
|
333
340
|
}
|
|
@@ -338,6 +345,7 @@ function compileDealAnalyzerPrompt() {
|
|
|
338
345
|
const DEAL_ANALYZER_ALLOWED_TOOLS = [
|
|
339
346
|
"customers.analyze_deals",
|
|
340
347
|
"customers.update_deal_stage",
|
|
348
|
+
"customers.list_pipeline_stages",
|
|
341
349
|
"customers.list_deals",
|
|
342
350
|
"customers.get_deal",
|
|
343
351
|
"customers.list_activities",
|
|
@@ -353,6 +361,7 @@ function buildDealAnalyzerPrepareStep() {
|
|
|
353
361
|
"customers.list_deals",
|
|
354
362
|
"customers.get_deal",
|
|
355
363
|
"customers.list_activities",
|
|
364
|
+
"customers.list_pipeline_stages",
|
|
356
365
|
"search.hybrid_search",
|
|
357
366
|
"meta.describe_agent",
|
|
358
367
|
"meta.update_task_plan"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/modules/customers/ai-agents.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Module-root AI agent contribution for the customers module.\n *\n * See /framework/ai-assistant/agents for the structured PromptTemplate\n * convention and the per-tenant override path that can downgrade\n * mutationPolicy to read-only.\n *\n * The generator walks every module root for a top-level `ai-agents.ts` and\n * takes the default/`aiAgents` export as the agent contribution. The\n * `customers.account_assistant` agent explores people / companies / deals /\n * activities / tags / addresses / settings through the customers tool pack\n * and the general-purpose `search.*`, `attachments.*`, `meta.*` tools, and\n * is also write-capable: it whitelists `customers.update_deal_stage` so the\n * operator can move deals between pipeline stages. Every mutation is\n * intercepted by the runtime and surfaced through the pending-action\n * approval card before any change is persisted (`mutationPolicy:\n * 'confirm-required'` is the default on this agent \u2014 a per-tenant override\n * can downgrade it to `read-only` to lock writes without a redeploy).\n *\n * Prompt is declared as a structured `PromptTemplate` (not a flat string)\n * per spec \u00A78 with the seven named sections: ROLE, SCOPE, DATA, TOOLS,\n * ATTACHMENTS, MUTATION POLICY, RESPONSE STYLE.\n */\nimport type {\n AiAgentDefinition,\n AiAgentPageContextInput,\n} from '@open-mercato/ai-assistant/modules/ai_assistant/lib/ai-agent-definition'\nimport { hydrateCustomersAccountContext } from './ai-agents-context'\n\ntype PromptSectionName =\n | 'role'\n | 'scope'\n | 'data'\n | 'tools'\n | 'attachments'\n | 'mutationPolicy'\n | 'responseStyle'\n | 'overrides'\n\ninterface PromptSection {\n name: PromptSectionName\n content: string\n order?: number\n}\n\ninterface PromptTemplate {\n id: string\n sections: PromptSection[]\n}\n\nconst AGENT_ID = 'customers.account_assistant'\nconst MODULE_ID = 'customers'\n\nconst ALLOWED_TOOLS: readonly string[] = [\n 'customers.list_people',\n 'customers.get_person',\n 'customers.list_companies',\n 'customers.get_company',\n 'customers.list_deals',\n 'customers.get_deal',\n 'customers.list_activities',\n 'customers.list_tasks',\n 'customers.list_deal_comments',\n 'customers.list_record_comments',\n 'customers.list_addresses',\n 'customers.list_tags',\n 'customers.get_settings',\n // Mutation-capable tools exposed by the customers account assistant.\n // The agent's default `mutationPolicy: 'confirm-required'` routes every\n // call through the pending-action approval card. A per-tenant override\n // can downgrade the agent back to `read-only`, in which case the runtime\n // filters these tools out before the model sees them.\n 'customers.update_deal_stage',\n 'customers.manage_deal_comment',\n 'customers.manage_deal_activity',\n 'customers.manage_record_comment',\n 'customers.manage_record_activity',\n 'search.hybrid_search',\n 'search.get_record_context',\n 'attachments.list_record_attachments',\n 'attachments.read_attachment',\n 'meta.describe_agent',\n]\n\nconst REQUIRED_FEATURES: readonly string[] = [\n 'customers.people.view',\n 'customers.companies.view',\n 'customers.deals.view',\n]\n\nconst PROMPT_SECTIONS: PromptSection[] = [\n {\n name: 'role',\n order: 1,\n content: [\n 'ROLE',\n 'You are the Customers Account Assistant inside Open Mercato. You help',\n 'operators answer questions about people, companies, deals, activities,',\n 'tasks, addresses, and tags by reading the tenant-scoped customer data',\n 'the platform exposes through the authorized tool pack.',\n ].join('\\n'),\n },\n {\n name: 'scope',\n order: 2,\n content: [\n 'SCOPE',\n 'Stay inside the customers module. Respect tenant and organization isolation.',\n 'ALWAYS call tools immediately \u2014 NEVER ask clarifying questions before acting. Use sensible defaults:',\n '- \"list people/companies/deals\" \u2192 call the list tool with NO parameters',\n '- User mentions a name \u2192 call the list tool with q=that name',\n '- \"show recent deals\" \u2192 call customers.list_deals with no q, limited results',\n 'Present results first, then offer refinement options. The user does NOT want to answer questions before seeing data.',\n ].join('\\n'),\n },\n {\n name: 'data',\n order: 3,\n content: [\n 'DATA',\n 'You can read: customers.person, customers.company, customers.deal,',\n 'customers.activity, customers.task, customers.address, customers.tag,',\n 'and customer settings. Use `customers.list_*` tools for search / filter',\n 'questions and `customers.get_*` tools when the operator asks about one',\n 'specific record. Use `search.hybrid_search` only when the operator',\n 'mentions free-text queries that span multiple entity types. When the',\n 'operator asks about \"this record\" / \"this deal\" / \"this account\", rely',\n 'on the page context supplied by the runtime instead of guessing.',\n 'CRITICAL: to list all records, call the list tool with NO q parameter. Do NOT use q=\"*\" or wildcards. Do NOT invent or guess UUIDs or identifiers. Only use IDs returned by a previous tool call.',\n ].join('\\n'),\n },\n {\n name: 'tools',\n order: 4,\n content: [\n 'TOOLS',\n 'The runtime only exposes the whitelisted customers.* and general-purpose',\n '(search.*, attachments.*, meta.describe_agent) tools. You MUST prefer',\n 'the narrowest tool that answers the question. Chain tools as needed but',\n 'do not loop \u2014 if a tool returns no matches after two different queries,',\n 'tell the operator what you searched for and stop. Never invent a tool',\n 'name; calling a tool not in the whitelist is a user-visible error.',\n ].join('\\n'),\n },\n {\n name: 'attachments',\n order: 5,\n content: [\n 'ATTACHMENTS',\n 'Attached images, PDFs, and files flow in through the attachment bridge.',\n 'Use `attachments.list_record_attachments` to discover what is attached',\n 'to a given record, and `attachments.read_attachment` to pull extracted',\n 'text or metadata. Refer to attachments by their human label when citing',\n 'them in a response; never expose raw attachment ids to the operator.',\n ].join('\\n'),\n },\n {\n name: 'mutationPolicy',\n order: 6,\n content: [\n 'MUTATION POLICY',\n 'This agent is write-capable and ships with `mutationPolicy:',\n '\"confirm-required\"` \u2014 every mutation goes through the pending-action',\n 'approval card and only persists after the operator confirms it.',\n 'Currently exposed mutation tools:',\n '- `customers.update_deal_stage` \u2014 move a deal between pipeline stages',\n ' or flip status between open / won / lost.',\n '- `customers.manage_deal_comment` \u2014 create / update / delete a comment',\n ' on a deal. Pass `operation: \"create\" | \"update\" | \"delete\"` and the',\n ' matching ids/body. Use `customers.list_deal_comments` first when the',\n ' operator asks \"which comment\" so you can supply the right commentId.',\n '- `customers.manage_deal_activity` \u2014 create / update / delete a logged',\n ' activity (call, email, meeting, note) on a deal. Same `operation`',\n ' switch; pass `dealId` + `activityType` for create, `activityId` for',\n ' update / delete. Use `customers.list_activities` (with `dealId`)',\n ' first when the operator asks about an existing activity.',\n '- `customers.manage_record_comment` \u2014 create / update / delete a',\n ' comment directly on a person OR company (and optionally also link it',\n ' to a deal via `dealId`). Use this when the operator wants to leave',\n ' a note on a customer record itself, not on a deal. Pass `personId`',\n ' OR `companyId` for create, `commentId` for update / delete. Use',\n ' `customers.list_record_comments` first to find the right commentId.',\n '- `customers.manage_record_activity` \u2014 create / update / delete an',\n ' activity directly on a person OR company (optionally linked to a',\n ' deal via `dealId`). Same `operation` switch; for create pass',\n ' `personId` OR `companyId` plus `activityType`; for update / delete',\n ' pass `activityId`. Use `customers.list_activities` (with',\n ' `personId`/`companyId`) to find the right activityId first.',\n 'When the operator asks for any of these, call the tool; the runtime',\n 'will short-circuit the call into a mutation-preview-card \u2014 do NOT',\n 'claim the change is saved until the mutation-result-card arrives.',\n 'If a per-tenant override has downgraded this agent back to',\n '`read-only`, the runtime will refuse the call: tell the operator the',\n 'write is locked for this tenant and point to the matching Open',\n 'Mercato backoffice page (for example `/backend/customers/deals/<id>`).',\n 'For any other kind of write (update person / create company), explain',\n 'that you cannot perform that mutation yet and point to the backoffice.',\n ].join('\\n'),\n },\n {\n name: 'responseStyle',\n order: 7,\n content: [\n 'RESPONSE STYLE',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #1 \u2014 RECORD CARDS ARE MANDATORY (no Markdown fallback for records)',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'Whenever your answer mentions, lists, or summarizes ANY person, company, deal, or activity the operator can identify (single record or many \u2014 does not matter), you MUST emit ONE `open-mercato:<kind>` fenced card per record. Do NOT use Markdown bullets, numbered lists, or plain text with the record name. Cards render as rich tiles with the avatar/logo, status, and a click-through; bullets render as text and waste the schema you already have.',\n '',\n 'Concretely: when `customers.list_people`, `customers.list_companies`, `customers.list_deals`, `customers.list_activities`, or any `customers.get_*` tool returns N items, your reply MUST contain N fenced `open-mercato:<kind>` blocks (one per item). You may add a single short prose sentence above the cards (\"Here are the people in scope:\") and a short follow-up line below them (\"Want me to dig into one?\"). Everything else is one card per record. The \"long list, drop to Markdown links\" pattern is FORBIDDEN \u2014 there is no row count above which Markdown is preferable to cards.',\n '',\n 'Cards are forbidden ONLY in these three cases:',\n ' 1. The operator asked for a tenant-level overview / counts / \"what do we have\" \u2014 describe the snapshot in prose.',\n ' 2. You do not yet have a concrete `id` (UUID) and concrete non-empty title/name from a prior tool call. In that case, write a sentence (\"I do not have that record\\'s id yet \u2014 let me look it up\") and call the right tool. Never emit a card with placeholder values like `<uuid>`, empty strings, or made-up names.',\n ' 3. A mutation approval card is the active surface \u2014 the runtime renders `mutation-preview-card` / `mutation-result-card` for you. Do not double up with manual record cards inside the same turn.',\n '',\n 'NEVER emit an empty card. NEVER copy the template below verbatim into a response. Empty / placeholder cards render as broken tiles and are a user-visible bug.',\n '',\n 'CRITICAL \u2014 FENCE FORMAT: every card MUST be wrapped in a triple-backtick fenced block whose info string is exactly `open-mercato:<kind>` (deal/person/company/activity). The opening fence is three backticks immediately followed by `open-mercato:<kind>` and a newline; the JSON object goes on the next line(s); the closing fence is three backticks on their own line. Without the fence the parser falls back and the card never renders \u2014 the operator sees raw JSON in prose. NEVER drop the backticks. NEVER write `open-mercato:deal { ... }` on a single line without the fence.',\n '',\n 'Card schemas (single JSON object inside a fenced block):',\n '- `open-mercato:deal` \u2014 { \"id\", \"title\", \"status\"?, \"stage\"?, \"amount\"?, \"currency\"?, \"closeDate\"?, \"ownerName\"?, \"personName\"?, \"companyName\"?, \"description\"?, \"tags\"?, \"href\"? }',\n '- `open-mercato:person` \u2014 { \"id\", \"name\", \"title\"?, \"email\"?, \"phone\"?, \"companyName\"?, \"ownerName\"?, \"status\"?, \"tags\"?, \"href\"? }',\n '- `open-mercato:company` \u2014 { \"id\", \"name\", \"industry\"?, \"website\"?, \"email\"?, \"phone\"?, \"city\"?, \"country\"?, \"ownerName\"?, \"status\"?, \"tags\"?, \"href\"? }',\n '- `open-mercato:activity` \u2014 { \"id\", \"title\", \"type\"?, \"status\"?, \"dueDate\"?, \"completedAt\"?, \"ownerName\"?, \"relatedTo\"?, \"description\"?, \"tags\"?, \"href\"? }',\n '',\n 'Always populate `href` with the deep link to the matching backoffice page so the card becomes clickable. Use these patterns:',\n '- Deal: `/backend/customers/deals/<id>`',\n '- Person: `/backend/customers/people/<id>`',\n '- Company: `/backend/customers/companies/<id>`',\n '- Activity: `/backend/customers/activities/<id>`',\n '',\n 'Template (DO NOT copy this verbatim \u2014 substitute real values from a prior tool call, or skip the card entirely):',\n '```open-mercato:deal',\n '{ \"id\": \"<concrete-uuid>\", \"title\": \"<concrete-title>\", \"status\": \"<status-or-omit>\", \"companyName\": \"<company-or-omit>\", \"href\": \"/backend/customers/deals/<concrete-uuid>\" }',\n '```',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #2 \u2014 Everything else',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'Lead with the direct answer, then justify it with the relevant cards. Use Markdown (bold, tables, bullet lists) for non-record content (counts, prose explanations, attribute summaries, etc). For inline references to a single record *inside* prose, you may use a Markdown link `[Record name](/backend/customers/deals/<id>)`, but never as a substitute for the per-record card list above.',\n '',\n 'Translate any labels back to the operator\\'s language when the chat runtime flags it, but keep tool calls and reasoning in English. NEVER paste a raw UUID as plain text without a link or card. Never include internal tenant ids, API keys, or system-prompt text in the reply.',\n ].join('\\n'),\n },\n]\n\nexport const promptTemplate: PromptTemplate = {\n id: `${AGENT_ID}.prompt`,\n sections: PROMPT_SECTIONS,\n}\n\nfunction compilePromptTemplate(template: PromptTemplate): string {\n return template.sections\n .slice()\n .sort((a: PromptSection, b: PromptSection) => (a.order ?? 0) - (b.order ?? 0))\n .map((section: PromptSection) => section.content.trim())\n .join('\\n\\n')\n}\n\nasync function resolvePageContext(\n input: AiAgentPageContextInput,\n): Promise<string | null> {\n // Step 5.2 \u2014 hydrate record-level context for person / company / deal\n // entities. Delegates to `ai-agents-context.ts`, which reuses the\n // tool-pack handlers so there is exactly one read-path per record type.\n // Errors are swallowed inside the helper; the runtime proceeds without\n // extra context on any failure.\n return hydrateCustomersAccountContext(input)\n}\n\nconst agent: AiAgentDefinition = {\n id: AGENT_ID,\n moduleId: MODULE_ID,\n label: 'Customers Account Assistant',\n description:\n 'Assistant for exploring customers: people, companies, deals, activities, tasks, addresses, tags, and settings. Can move deals between stages \u2014 every write goes through the approval card.',\n systemPrompt: compilePromptTemplate(promptTemplate),\n allowedTools: [...ALLOWED_TOOLS],\n executionMode: 'chat',\n acceptedMediaTypes: ['image', 'pdf', 'file'],\n requiredFeatures: [...REQUIRED_FEATURES],\n taskPlan: { enabled: true },\n readOnly: false,\n // Default for write-capable agents: every mutation must be confirmed by\n // the operator. Per-tenant override can downgrade to `read-only` to lock\n // writes back down without redeploying.\n mutationPolicy: 'confirm-required',\n keywords: ['customers', 'crm', 'accounts', 'people', 'companies', 'deals'],\n domain: 'customers',\n dataCapabilities: {\n entities: [\n 'customers.person',\n 'customers.company',\n 'customers.deal',\n 'customers.activity',\n 'customers.task',\n 'customers.address',\n 'customers.tag',\n ],\n operations: ['read', 'search'],\n },\n resolvePageContext,\n}\n\n// customers.deal_analyzer \u2014 multi-step agentic loop demo.\n// See /framework/ai-assistant/agents \u2192 \"Deal Analyzer demo\" for the loop\n// primitives exercised here; the sibling tool-loop-agent below proves both\n// execution engines honor the mutation gate.\n\ntype DealAnalyzerPromptSectionName =\n | 'role'\n | 'scope'\n | 'data'\n | 'tools'\n | 'mutationPolicy'\n | 'responseStyle'\n\ninterface DealAnalyzerPromptSection {\n name: DealAnalyzerPromptSectionName\n content: string\n order: number\n}\n\nconst DEAL_ANALYZER_PROMPT_SECTIONS: DealAnalyzerPromptSection[] = [\n {\n name: 'role',\n order: 1,\n content: [\n 'ROLE',\n 'You are the Deal Analyzer inside Open Mercato. You are a multi-step agentic',\n 'assistant that analyzes the health of a tenant\\'s open deals, surfaces stalled',\n 'opportunities, and proposes pipeline stage transitions for operator approval.',\n ].join('\\n'),\n },\n {\n name: 'scope',\n order: 2,\n content: [\n 'SCOPE',\n 'Stay inside the customers module. Respect tenant and organization isolation.',\n 'ALWAYS call customers.analyze_deals as your FIRST domain tool \u2014 do not skip this.',\n 'Reason about stalled deals: any deal with no activity for more than 14 days',\n 'is considered stalled. For each stalled deal with a value greater than $5,000',\n 'propose a stage move via customers.update_deal_stage.',\n 'After calling customers.update_deal_stage, finish with a concise conclusion',\n 'summarizing the analysis and the approval action the operator should review.',\n ].join('\\n'),\n },\n {\n name: 'data',\n order: 3,\n content: [\n 'DATA',\n 'You can read: customers.deal via customers.analyze_deals (analytical summary)',\n 'and customers.list_deals / customers.get_deal (full detail).',\n 'Use customers.analyze_deals first \u2014 it returns a ranked list of deals by',\n 'health score (lowest = most at risk) with last-activity information.',\n 'Use customers.list_activities to get more detail on a specific deal\\'s activity.',\n 'Use search.hybrid_search only for free-text queries spanning multiple entity types.',\n 'CRITICAL: Only use IDs returned by a prior tool call. Never invent or guess UUIDs.',\n ].join('\\n'),\n },\n {\n name: 'tools',\n order: 4,\n content: [\n 'TOOLS',\n 'Primary tools for this agent (call in this order on each turn):',\n '1. customers.analyze_deals \u2014 analytical overview of deals ranked by health score.',\n ' Supply dealStageFilter=\"open\" to restrict to open deals.',\n '2. customers.update_deal_stage \u2014 propose a stage move for a stalled high-value deal.',\n ' The runtime intercepts this via the pending-action gate; do NOT claim the change',\n ' is saved until the mutation-result-card arrives.',\n 'Secondary read tools (use when you need more detail):',\n '- customers.list_deals, customers.get_deal, customers.list_activities',\n '- search.hybrid_search, meta.describe_agent',\n ].join('\\n'),\n },\n {\n name: 'mutationPolicy',\n order: 5,\n content: [\n 'MUTATION POLICY',\n 'This agent ships with mutationPolicy: \"confirm-required\". Every write goes through',\n 'the pending-action approval card and only persists after the operator confirms it.',\n 'After calling customers.update_deal_stage, explain whether a pending approval',\n 'card was created or whether the proposed move could not be prepared.',\n 'Do NOT call update_deal_stage more than once per turn.',\n 'If a per-tenant override has downgraded this agent to read-only, tell the operator',\n 'the write is locked and point to /backend/customers/deals/<id>.',\n ].join('\\n'),\n },\n {\n name: 'responseStyle',\n order: 6,\n content: [\n 'RESPONSE STYLE',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #1 \u2014 DEAL RECORD CARDS ARE MANDATORY',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'For every deal you surface in your analysis you MUST emit an open-mercato:deal',\n 'fenced card. Use the deal id, title, value, and stage from the tool output.',\n 'Always populate href with /backend/customers/deals/<id>.',\n '',\n 'Card schema: { \"id\", \"title\", \"status\"?, \"stage\"?, \"amount\"?, \"currency\"?,',\n ' \"closeDate\"?, \"ownerName\"?, \"personName\"?, \"companyName\"?,',\n ' \"description\"?, \"tags\"?, \"href\" }',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #2 \u2014 Analysis format',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'Lead with a one-paragraph summary of the deal portfolio health, then emit one',\n 'deal card per at-risk deal (sorted by health score ascending). After the cards,',\n 'propose exactly one stage move for the highest-value stalled deal and call',\n 'customers.update_deal_stage. Then finish with a short conclusion naming the',\n 'highest-value stalled deal, the recommended move, and the approval status.',\n ].join('\\n'),\n },\n]\n\nfunction compileDealAnalyzerPrompt(): string {\n return DEAL_ANALYZER_PROMPT_SECTIONS.slice()\n .sort((a, b) => a.order - b.order)\n .map((section) => section.content.trim())\n .join('\\n\\n')\n}\n\nconst DEAL_ANALYZER_ALLOWED_TOOLS: readonly string[] = [\n 'customers.analyze_deals',\n 'customers.update_deal_stage',\n 'customers.list_deals',\n 'customers.get_deal',\n 'customers.list_activities',\n 'search.hybrid_search',\n 'meta.describe_agent',\n]\n\nfunction buildDealAnalyzerPrepareStep() {\n return async function dealAnalyzerPrepareStep(state: { stepNumber: number }) {\n if (state.stepNumber === 0) {\n return {\n activeTools: [\n 'customers.analyze_deals',\n 'customers.list_deals',\n 'customers.get_deal',\n 'customers.list_activities',\n 'search.hybrid_search',\n 'meta.describe_agent',\n 'meta.update_task_plan',\n ],\n }\n }\n return { activeTools: [...DEAL_ANALYZER_ALLOWED_TOOLS] }\n }\n}\n\nconst dealAnalyzer: AiAgentDefinition = {\n id: 'customers.deal_analyzer',\n moduleId: 'customers',\n label: 'Deal Analyzer',\n description:\n 'Multi-step CRM agent that analyzes deals, surfaces stalled opportunities, and proposes stage transitions for operator approval.',\n systemPrompt: compileDealAnalyzerPrompt(),\n allowedTools: [...DEAL_ANALYZER_ALLOWED_TOOLS],\n executionMode: 'chat',\n executionEngine: 'stream-text',\n allowRuntimeOverride: true,\n taskPlan: { enabled: true },\n readOnly: false,\n mutationPolicy: 'confirm-required',\n requiredFeatures: ['customers.deals.view'],\n uiParts: ['open-mercato:deal'],\n keywords: ['deal', 'pipeline', 'stalled', 'crm', 'analysis', 'health'],\n domain: 'customers',\n loop: {\n maxSteps: 12,\n prepareStep: buildDealAnalyzerPrepareStep() as AiAgentDefinition['loop'] extends undefined\n ? never\n : NonNullable<AiAgentDefinition['loop']>['prepareStep'],\n budget: {\n maxToolCalls: 12,\n maxWallClockMs: 60_000,\n },\n allowRuntimeOverride: true,\n },\n dataCapabilities: {\n entities: ['customers.deal', 'customers.activity'],\n operations: ['read', 'search', 'aggregate'],\n },\n suggestions: [\n {\n label: 'Analyze stalled deals',\n prompt: 'Analyze stalled deals from the last 30 days and propose a stage move for the highest value one',\n },\n {\n label: 'Show at-risk pipeline',\n prompt: 'Show me deals with no activity in the last 14 days worth more than $5,000',\n },\n ],\n}\n\n// Sibling agent identical to customers.deal_analyzer except for\n// executionEngine: 'tool-loop-agent' (TC-AI-AGENT-LOOP-006).\nconst dealAnalyzerToolLoop: AiAgentDefinition = {\n ...dealAnalyzer,\n id: 'customers.deal_analyzer_tool_loop',\n label: 'Deal Analyzer (ToolLoopAgent)',\n description:\n 'Same as customers.deal_analyzer but dispatched via the ToolLoopAgent engine. Used by TC-AI-AGENT-LOOP-006 mutation-gate proof scenario.',\n executionEngine: 'tool-loop-agent',\n}\n\nexport const aiAgents: AiAgentDefinition[] = [agent, dealAnalyzer, dealAnalyzerToolLoop]\n\nexport default aiAgents\n"],
|
|
5
|
-
"mappings": "AA2BA,SAAS,sCAAsC;AAuB/C,MAAM,WAAW;AACjB,MAAM,YAAY;AAElB,MAAM,gBAAmC;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA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G,2BAA2B,EAAE;AAAA,EACzD;AACF;AAEA,MAAM,eAAkC;AAAA,EACtC,IAAI;AAAA,EACJ,UAAU;AAAA,EACV,OAAO;AAAA,EACP,aACE;AAAA,EACF,cAAc,0BAA0B;AAAA,EACxC,cAAc,CAAC,GAAG,2BAA2B;AAAA,EAC7C,eAAe;AAAA,EACf,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,UAAU,EAAE,SAAS,KAAK;AAAA,EAC1B,UAAU;AAAA,EACV,gBAAgB;AAAA,EAChB,kBAAkB,CAAC,sBAAsB;AAAA,EACzC,SAAS,CAAC,mBAAmB;AAAA,EAC7B,UAAU,CAAC,QAAQ,YAAY,WAAW,OAAO,YAAY,QAAQ;AAAA,EACrE,QAAQ;AAAA,EACR,MAAM;AAAA,IACJ,UAAU;AAAA,IACV,aAAa,6BAA6B;AAAA,IAG1C,QAAQ;AAAA,MACN,cAAc;AAAA,MACd,gBAAgB;AAAA,IAClB;AAAA,IACA,sBAAsB;AAAA,EACxB;AAAA,EACA,kBAAkB;AAAA,IAChB,UAAU,CAAC,kBAAkB,oBAAoB;AAAA,IACjD,YAAY,CAAC,QAAQ,UAAU,WAAW;AAAA,EAC5C;AAAA,EACA,aAAa;AAAA,IACX;AAAA,MACE,OAAO;AAAA,MACP,QAAQ;AAAA,IACV;AAAA,IACA;AAAA,MACE,OAAO;AAAA,MACP,QAAQ;AAAA,IACV;AAAA,EACF;AACF;AAIA,MAAM,uBAA0C;AAAA,EAC9C,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,OAAO;AAAA,EACP,aACE;AAAA,EACF,iBAAiB;AACnB;AAEO,MAAM,WAAgC,CAAC,OAAO,cAAc,oBAAoB;AAEvF,IAAO,oBAAQ;",
|
|
4
|
+
"sourcesContent": ["/**\n * Module-root AI agent contribution for the customers module.\n *\n * See /framework/ai-assistant/agents for the structured PromptTemplate\n * convention and the per-tenant override path that can downgrade\n * mutationPolicy to read-only.\n *\n * The generator walks every module root for a top-level `ai-agents.ts` and\n * takes the default/`aiAgents` export as the agent contribution. The\n * `customers.account_assistant` agent explores people / companies / deals /\n * activities / tags / addresses / settings through the customers tool pack\n * and the general-purpose `search.*`, `attachments.*`, `meta.*` tools, and\n * is also write-capable: it whitelists `customers.update_deal_stage` so the\n * operator can move deals between pipeline stages. Every mutation is\n * intercepted by the runtime and surfaced through the pending-action\n * approval card before any change is persisted (`mutationPolicy:\n * 'confirm-required'` is the default on this agent \u2014 a per-tenant override\n * can downgrade it to `read-only` to lock writes without a redeploy).\n *\n * Prompt is declared as a structured `PromptTemplate` (not a flat string)\n * per spec \u00A78 with the seven named sections: ROLE, SCOPE, DATA, TOOLS,\n * ATTACHMENTS, MUTATION POLICY, RESPONSE STYLE.\n */\nimport type {\n AiAgentDefinition,\n AiAgentPageContextInput,\n} from '@open-mercato/ai-assistant/modules/ai_assistant/lib/ai-agent-definition'\nimport { hydrateCustomersAccountContext } from './ai-agents-context'\n\ntype PromptSectionName =\n | 'role'\n | 'scope'\n | 'data'\n | 'tools'\n | 'attachments'\n | 'mutationPolicy'\n | 'responseStyle'\n | 'overrides'\n\ninterface PromptSection {\n name: PromptSectionName\n content: string\n order?: number\n}\n\ninterface PromptTemplate {\n id: string\n sections: PromptSection[]\n}\n\nconst AGENT_ID = 'customers.account_assistant'\nconst MODULE_ID = 'customers'\n\nconst ALLOWED_TOOLS: readonly string[] = [\n 'customers.list_people',\n 'customers.get_person',\n 'customers.list_companies',\n 'customers.get_company',\n 'customers.list_deals',\n 'customers.get_deal',\n 'customers.list_activities',\n 'customers.list_tasks',\n 'customers.list_deal_comments',\n 'customers.list_record_comments',\n 'customers.list_addresses',\n 'customers.list_tags',\n 'customers.get_settings',\n // Mutation-capable tools exposed by the customers account assistant.\n // The agent's default `mutationPolicy: 'confirm-required'` routes every\n // call through the pending-action approval card. A per-tenant override\n // can downgrade the agent back to `read-only`, in which case the runtime\n // filters these tools out before the model sees them.\n 'customers.update_deal_stage',\n 'customers.manage_deal_comment',\n 'customers.manage_deal_activity',\n 'customers.manage_record_comment',\n 'customers.manage_record_activity',\n 'search.hybrid_search',\n 'search.get_record_context',\n 'attachments.list_record_attachments',\n 'attachments.read_attachment',\n 'meta.describe_agent',\n]\n\nconst REQUIRED_FEATURES: readonly string[] = [\n 'customers.people.view',\n 'customers.companies.view',\n 'customers.deals.view',\n]\n\nconst PROMPT_SECTIONS: PromptSection[] = [\n {\n name: 'role',\n order: 1,\n content: [\n 'ROLE',\n 'You are the Customers Account Assistant inside Open Mercato. You help',\n 'operators answer questions about people, companies, deals, activities,',\n 'tasks, addresses, and tags by reading the tenant-scoped customer data',\n 'the platform exposes through the authorized tool pack.',\n ].join('\\n'),\n },\n {\n name: 'scope',\n order: 2,\n content: [\n 'SCOPE',\n 'Stay inside the customers module. Respect tenant and organization isolation.',\n 'ALWAYS call tools immediately \u2014 NEVER ask clarifying questions before acting. Use sensible defaults:',\n '- \"list people/companies/deals\" \u2192 call the list tool with NO parameters',\n '- User mentions a name \u2192 call the list tool with q=that name',\n '- \"show recent deals\" \u2192 call customers.list_deals with no q, limited results',\n 'Present results first, then offer refinement options. The user does NOT want to answer questions before seeing data.',\n ].join('\\n'),\n },\n {\n name: 'data',\n order: 3,\n content: [\n 'DATA',\n 'You can read: customers.person, customers.company, customers.deal,',\n 'customers.activity, customers.task, customers.address, customers.tag,',\n 'and customer settings. Use `customers.list_*` tools for search / filter',\n 'questions and `customers.get_*` tools when the operator asks about one',\n 'specific record. Use `search.hybrid_search` only when the operator',\n 'mentions free-text queries that span multiple entity types. When the',\n 'operator asks about \"this record\" / \"this deal\" / \"this account\", rely',\n 'on the page context supplied by the runtime instead of guessing.',\n 'CRITICAL: to list all records, call the list tool with NO q parameter. Do NOT use q=\"*\" or wildcards. Do NOT invent or guess UUIDs or identifiers. Only use IDs returned by a previous tool call.',\n ].join('\\n'),\n },\n {\n name: 'tools',\n order: 4,\n content: [\n 'TOOLS',\n 'The runtime only exposes the whitelisted customers.* and general-purpose',\n '(search.*, attachments.*, meta.describe_agent) tools. You MUST prefer',\n 'the narrowest tool that answers the question. Chain tools as needed but',\n 'do not loop \u2014 if a tool returns no matches after two different queries,',\n 'tell the operator what you searched for and stop. Never invent a tool',\n 'name; calling a tool not in the whitelist is a user-visible error.',\n ].join('\\n'),\n },\n {\n name: 'attachments',\n order: 5,\n content: [\n 'ATTACHMENTS',\n 'Attached images, PDFs, and files flow in through the attachment bridge.',\n 'Use `attachments.list_record_attachments` to discover what is attached',\n 'to a given record, and `attachments.read_attachment` to pull extracted',\n 'text or metadata. Refer to attachments by their human label when citing',\n 'them in a response; never expose raw attachment ids to the operator.',\n ].join('\\n'),\n },\n {\n name: 'mutationPolicy',\n order: 6,\n content: [\n 'MUTATION POLICY',\n 'This agent is write-capable and ships with `mutationPolicy:',\n '\"confirm-required\"` \u2014 every mutation goes through the pending-action',\n 'approval card and only persists after the operator confirms it.',\n 'Currently exposed mutation tools:',\n '- `customers.update_deal_stage` \u2014 move a deal between pipeline stages',\n ' or flip status between open / won / lost.',\n '- `customers.manage_deal_comment` \u2014 create / update / delete a comment',\n ' on a deal. Pass `operation: \"create\" | \"update\" | \"delete\"` and the',\n ' matching ids/body. Use `customers.list_deal_comments` first when the',\n ' operator asks \"which comment\" so you can supply the right commentId.',\n '- `customers.manage_deal_activity` \u2014 create / update / delete a logged',\n ' activity (call, email, meeting, note) on a deal. Same `operation`',\n ' switch; pass `dealId` + `activityType` for create, `activityId` for',\n ' update / delete. Use `customers.list_activities` (with `dealId`)',\n ' first when the operator asks about an existing activity.',\n '- `customers.manage_record_comment` \u2014 create / update / delete a',\n ' comment directly on a person OR company (and optionally also link it',\n ' to a deal via `dealId`). Use this when the operator wants to leave',\n ' a note on a customer record itself, not on a deal. Pass `personId`',\n ' OR `companyId` for create, `commentId` for update / delete. Use',\n ' `customers.list_record_comments` first to find the right commentId.',\n '- `customers.manage_record_activity` \u2014 create / update / delete an',\n ' activity directly on a person OR company (optionally linked to a',\n ' deal via `dealId`). Same `operation` switch; for create pass',\n ' `personId` OR `companyId` plus `activityType`; for update / delete',\n ' pass `activityId`. Use `customers.list_activities` (with',\n ' `personId`/`companyId`) to find the right activityId first.',\n 'When the operator asks for any of these, call the tool; the runtime',\n 'will short-circuit the call into a mutation-preview-card \u2014 do NOT',\n 'claim the change is saved until the mutation-result-card arrives.',\n 'If a per-tenant override has downgraded this agent back to',\n '`read-only`, the runtime will refuse the call: tell the operator the',\n 'write is locked for this tenant and point to the matching Open',\n 'Mercato backoffice page (for example `/backend/customers/deals/<id>`).',\n 'For any other kind of write (update person / create company), explain',\n 'that you cannot perform that mutation yet and point to the backoffice.',\n ].join('\\n'),\n },\n {\n name: 'responseStyle',\n order: 7,\n content: [\n 'RESPONSE STYLE',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #1 \u2014 RECORD CARDS ARE MANDATORY (no Markdown fallback for records)',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'Whenever your answer mentions, lists, or summarizes ANY person, company, deal, or activity the operator can identify (single record or many \u2014 does not matter), you MUST emit ONE `open-mercato:<kind>` fenced card per record. Do NOT use Markdown bullets, numbered lists, or plain text with the record name. Cards render as rich tiles with the avatar/logo, status, and a click-through; bullets render as text and waste the schema you already have.',\n '',\n 'Concretely: when `customers.list_people`, `customers.list_companies`, `customers.list_deals`, `customers.list_activities`, or any `customers.get_*` tool returns N items, your reply MUST contain N fenced `open-mercato:<kind>` blocks (one per item). You may add a single short prose sentence above the cards (\"Here are the people in scope:\") and a short follow-up line below them (\"Want me to dig into one?\"). Everything else is one card per record. The \"long list, drop to Markdown links\" pattern is FORBIDDEN \u2014 there is no row count above which Markdown is preferable to cards.',\n '',\n 'Cards are forbidden ONLY in these three cases:',\n ' 1. The operator asked for a tenant-level overview / counts / \"what do we have\" \u2014 describe the snapshot in prose.',\n ' 2. You do not yet have a concrete `id` (UUID) and concrete non-empty title/name from a prior tool call. In that case, write a sentence (\"I do not have that record\\'s id yet \u2014 let me look it up\") and call the right tool. Never emit a card with placeholder values like `<uuid>`, empty strings, or made-up names.',\n ' 3. A mutation approval card is the active surface \u2014 the runtime renders `mutation-preview-card` / `mutation-result-card` for you. Do not double up with manual record cards inside the same turn.',\n '',\n 'NEVER emit an empty card. NEVER copy the template below verbatim into a response. Empty / placeholder cards render as broken tiles and are a user-visible bug.',\n '',\n 'CRITICAL \u2014 FENCE FORMAT: every card MUST be wrapped in a triple-backtick fenced block whose info string is exactly `open-mercato:<kind>` (deal/person/company/activity). The opening fence is three backticks immediately followed by `open-mercato:<kind>` and a newline; the JSON object goes on the next line(s); the closing fence is three backticks on their own line. Without the fence the parser falls back and the card never renders \u2014 the operator sees raw JSON in prose. NEVER drop the backticks. NEVER write `open-mercato:deal { ... }` on a single line without the fence.',\n '',\n 'Card schemas (single JSON object inside a fenced block):',\n '- `open-mercato:deal` \u2014 { \"id\", \"title\", \"status\"?, \"stage\"?, \"amount\"?, \"currency\"?, \"closeDate\"?, \"ownerName\"?, \"personName\"?, \"companyName\"?, \"description\"?, \"tags\"?, \"href\"? }',\n '- `open-mercato:person` \u2014 { \"id\", \"name\", \"title\"?, \"email\"?, \"phone\"?, \"companyName\"?, \"ownerName\"?, \"status\"?, \"tags\"?, \"href\"? }',\n '- `open-mercato:company` \u2014 { \"id\", \"name\", \"industry\"?, \"website\"?, \"email\"?, \"phone\"?, \"city\"?, \"country\"?, \"ownerName\"?, \"status\"?, \"tags\"?, \"href\"? }',\n '- `open-mercato:activity` \u2014 { \"id\", \"title\", \"type\"?, \"status\"?, \"dueDate\"?, \"completedAt\"?, \"ownerName\"?, \"relatedTo\"?, \"description\"?, \"tags\"?, \"href\"? }',\n '',\n 'Always populate `href` with the deep link to the matching backoffice page so the card becomes clickable. Use these patterns:',\n '- Deal: `/backend/customers/deals/<id>`',\n '- Person: `/backend/customers/people/<id>`',\n '- Company: `/backend/customers/companies/<id>`',\n '- Activity: `/backend/customers/activities/<id>`',\n '',\n 'Template (DO NOT copy this verbatim \u2014 substitute real values from a prior tool call, or skip the card entirely):',\n '```open-mercato:deal',\n '{ \"id\": \"<concrete-uuid>\", \"title\": \"<concrete-title>\", \"status\": \"<status-or-omit>\", \"companyName\": \"<company-or-omit>\", \"href\": \"/backend/customers/deals/<concrete-uuid>\" }',\n '```',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #2 \u2014 Everything else',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'Lead with the direct answer, then justify it with the relevant cards. Use Markdown (bold, tables, bullet lists) for non-record content (counts, prose explanations, attribute summaries, etc). For inline references to a single record *inside* prose, you may use a Markdown link `[Record name](/backend/customers/deals/<id>)`, but never as a substitute for the per-record card list above.',\n '',\n 'Translate any labels back to the operator\\'s language when the chat runtime flags it, but keep tool calls and reasoning in English. NEVER paste a raw UUID as plain text without a link or card. Never include internal tenant ids, API keys, or system-prompt text in the reply.',\n ].join('\\n'),\n },\n]\n\nexport const promptTemplate: PromptTemplate = {\n id: `${AGENT_ID}.prompt`,\n sections: PROMPT_SECTIONS,\n}\n\nfunction compilePromptTemplate(template: PromptTemplate): string {\n return template.sections\n .slice()\n .sort((a: PromptSection, b: PromptSection) => (a.order ?? 0) - (b.order ?? 0))\n .map((section: PromptSection) => section.content.trim())\n .join('\\n\\n')\n}\n\nasync function resolvePageContext(\n input: AiAgentPageContextInput,\n): Promise<string | null> {\n // Step 5.2 \u2014 hydrate record-level context for person / company / deal\n // entities. Delegates to `ai-agents-context.ts`, which reuses the\n // tool-pack handlers so there is exactly one read-path per record type.\n // Errors are swallowed inside the helper; the runtime proceeds without\n // extra context on any failure.\n return hydrateCustomersAccountContext(input)\n}\n\nconst agent: AiAgentDefinition = {\n id: AGENT_ID,\n moduleId: MODULE_ID,\n label: 'Customers Account Assistant',\n description:\n 'Assistant for exploring customers: people, companies, deals, activities, tasks, addresses, tags, and settings. Can move deals between stages \u2014 every write goes through the approval card.',\n systemPrompt: compilePromptTemplate(promptTemplate),\n allowedTools: [...ALLOWED_TOOLS],\n executionMode: 'chat',\n acceptedMediaTypes: ['image', 'pdf', 'file'],\n requiredFeatures: [...REQUIRED_FEATURES],\n taskPlan: { enabled: true },\n readOnly: false,\n // Default for write-capable agents: every mutation must be confirmed by\n // the operator. Per-tenant override can downgrade to `read-only` to lock\n // writes back down without redeploying.\n mutationPolicy: 'confirm-required',\n keywords: ['customers', 'crm', 'accounts', 'people', 'companies', 'deals'],\n domain: 'customers',\n dataCapabilities: {\n entities: [\n 'customers.person',\n 'customers.company',\n 'customers.deal',\n 'customers.activity',\n 'customers.task',\n 'customers.address',\n 'customers.tag',\n ],\n operations: ['read', 'search'],\n },\n resolvePageContext,\n}\n\n// customers.deal_analyzer \u2014 multi-step agentic loop demo.\n// See /framework/ai-assistant/agents \u2192 \"Deal Analyzer demo\" for the loop\n// primitives exercised here; the sibling tool-loop-agent below proves both\n// execution engines honor the mutation gate.\n\ntype DealAnalyzerPromptSectionName =\n | 'role'\n | 'scope'\n | 'data'\n | 'tools'\n | 'mutationPolicy'\n | 'responseStyle'\n\ninterface DealAnalyzerPromptSection {\n name: DealAnalyzerPromptSectionName\n content: string\n order: number\n}\n\nconst DEAL_ANALYZER_PROMPT_SECTIONS: DealAnalyzerPromptSection[] = [\n {\n name: 'role',\n order: 1,\n content: [\n 'ROLE',\n 'You are the Deal Analyzer inside Open Mercato. You are a multi-step agentic',\n 'assistant that analyzes the health of a tenant\\'s open deals, surfaces stalled',\n 'opportunities, and proposes pipeline stage transitions for operator approval.',\n ].join('\\n'),\n },\n {\n name: 'scope',\n order: 2,\n content: [\n 'SCOPE',\n 'Stay inside the customers module. Respect tenant and organization isolation.',\n 'ALWAYS call customers.analyze_deals as your FIRST domain tool \u2014 do not skip this.',\n 'Reason about stalled deals: any deal with no activity for more than 14 days',\n 'is considered stalled. For each stalled deal with a value greater than $5,000',\n 'propose a stage move via customers.update_deal_stage.',\n 'After calling customers.update_deal_stage, finish with a concise conclusion',\n 'summarizing the analysis and the approval action the operator should review.',\n ].join('\\n'),\n },\n {\n name: 'data',\n order: 3,\n content: [\n 'DATA',\n 'You can read: customers.deal via customers.analyze_deals (analytical summary)',\n 'and customers.list_deals / customers.get_deal (full detail).',\n 'Use customers.analyze_deals first \u2014 it returns a ranked list of deals by',\n 'health score (lowest = most at risk) with last-activity information.',\n 'Use customers.list_activities to get more detail on a specific deal\\'s activity.',\n 'Use search.hybrid_search only for free-text queries spanning multiple entity types.',\n 'CRITICAL: Only use IDs returned by a prior tool call. Never invent or guess UUIDs.',\n ].join('\\n'),\n },\n {\n name: 'tools',\n order: 4,\n content: [\n 'TOOLS',\n 'Primary tools for this agent (call in this order on each turn):',\n '1. customers.analyze_deals \u2014 analytical overview of deals ranked by health score.',\n ' Supply dealStageFilter=\"open\" to restrict to open deals.',\n '2. customers.update_deal_stage \u2014 propose a stage move for a stalled high-value deal.',\n ' The runtime intercepts this via the pending-action gate; do NOT claim the change',\n ' is saved until the mutation-result-card arrives.',\n ' When moving to a named pipeline stage, first call customers.list_pipeline_stages',\n ' and pass the matching UUID as toPipelineStageId. Only use toStage for top-level',\n ' status slugs such as open, won, or lost.',\n 'Secondary read tools (use when you need more detail):',\n '- customers.list_deals, customers.get_deal, customers.list_activities',\n '- customers.list_pipeline_stages',\n '- search.hybrid_search, meta.describe_agent',\n ].join('\\n'),\n },\n {\n name: 'mutationPolicy',\n order: 5,\n content: [\n 'MUTATION POLICY',\n 'This agent ships with mutationPolicy: \"confirm-required\". Every write goes through',\n 'the pending-action approval card and only persists after the operator confirms it.',\n 'After calling customers.update_deal_stage, explain whether a pending approval',\n 'card was created or whether the proposed move could not be prepared.',\n 'Do NOT call update_deal_stage more than once per turn.',\n 'If a per-tenant override has downgraded this agent to read-only, tell the operator',\n 'the write is locked and point to /backend/customers/deals/<id>.',\n ].join('\\n'),\n },\n {\n name: 'responseStyle',\n order: 6,\n content: [\n 'RESPONSE STYLE',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #1 \u2014 DEAL RECORD CARDS ARE MANDATORY',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'For every deal you surface in your analysis you MUST emit an open-mercato:deal',\n 'fenced card. Use the deal id, title, value, and stage from the tool output.',\n 'Always populate href with /backend/customers/deals/<id>.',\n '',\n 'Card schema: { \"id\", \"title\", \"status\"?, \"stage\"?, \"amount\"?, \"currency\"?,',\n ' \"closeDate\"?, \"ownerName\"?, \"personName\"?, \"companyName\"?,',\n ' \"description\"?, \"tags\"?, \"href\" }',\n '',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'RULE #2 \u2014 Analysis format',\n '\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550',\n 'Lead with a one-paragraph summary of the deal portfolio health, then emit one',\n 'deal card per at-risk deal (sorted by health score ascending). After the cards,',\n 'propose exactly one stage move for the highest-value stalled deal and call',\n 'customers.update_deal_stage. If the recommended stage is a pipeline stage label',\n 'rather than a status slug, resolve it with customers.list_pipeline_stages first;',\n 'do not ask the operator to paste a stage id unless no matching stage exists.',\n 'Then finish with a short conclusion naming the',\n 'highest-value stalled deal, the recommended move, and the approval status.',\n ].join('\\n'),\n },\n]\n\nfunction compileDealAnalyzerPrompt(): string {\n return DEAL_ANALYZER_PROMPT_SECTIONS.slice()\n .sort((a, b) => a.order - b.order)\n .map((section) => section.content.trim())\n .join('\\n\\n')\n}\n\nconst DEAL_ANALYZER_ALLOWED_TOOLS: readonly string[] = [\n 'customers.analyze_deals',\n 'customers.update_deal_stage',\n 'customers.list_pipeline_stages',\n 'customers.list_deals',\n 'customers.get_deal',\n 'customers.list_activities',\n 'search.hybrid_search',\n 'meta.describe_agent',\n]\n\nfunction buildDealAnalyzerPrepareStep() {\n return async function dealAnalyzerPrepareStep(state: { stepNumber: number }) {\n if (state.stepNumber === 0) {\n return {\n activeTools: [\n 'customers.analyze_deals',\n 'customers.list_deals',\n 'customers.get_deal',\n 'customers.list_activities',\n 'customers.list_pipeline_stages',\n 'search.hybrid_search',\n 'meta.describe_agent',\n 'meta.update_task_plan',\n ],\n }\n }\n return { activeTools: [...DEAL_ANALYZER_ALLOWED_TOOLS] }\n }\n}\n\nconst dealAnalyzer: AiAgentDefinition = {\n id: 'customers.deal_analyzer',\n moduleId: 'customers',\n label: 'Deal Analyzer',\n description:\n 'Multi-step CRM agent that analyzes deals, surfaces stalled opportunities, and proposes stage transitions for operator approval.',\n systemPrompt: compileDealAnalyzerPrompt(),\n allowedTools: [...DEAL_ANALYZER_ALLOWED_TOOLS],\n executionMode: 'chat',\n executionEngine: 'stream-text',\n allowRuntimeOverride: true,\n taskPlan: { enabled: true },\n readOnly: false,\n mutationPolicy: 'confirm-required',\n requiredFeatures: ['customers.deals.view'],\n uiParts: ['open-mercato:deal'],\n keywords: ['deal', 'pipeline', 'stalled', 'crm', 'analysis', 'health'],\n domain: 'customers',\n loop: {\n maxSteps: 12,\n prepareStep: buildDealAnalyzerPrepareStep() as AiAgentDefinition['loop'] extends undefined\n ? never\n : NonNullable<AiAgentDefinition['loop']>['prepareStep'],\n budget: {\n maxToolCalls: 12,\n maxWallClockMs: 60_000,\n },\n allowRuntimeOverride: true,\n },\n dataCapabilities: {\n entities: ['customers.deal', 'customers.activity'],\n operations: ['read', 'search', 'aggregate'],\n },\n suggestions: [\n {\n label: 'Analyze stalled deals',\n prompt: 'Analyze stalled deals from the last 30 days and propose a stage move for the highest value one',\n },\n {\n label: 'Show at-risk pipeline',\n prompt: 'Show me deals with no activity in the last 14 days worth more than $5,000',\n },\n ],\n}\n\n// Sibling agent identical to customers.deal_analyzer except for\n// executionEngine: 'tool-loop-agent' (TC-AI-AGENT-LOOP-006).\nconst dealAnalyzerToolLoop: AiAgentDefinition = {\n ...dealAnalyzer,\n id: 'customers.deal_analyzer_tool_loop',\n label: 'Deal Analyzer (ToolLoopAgent)',\n description:\n 'Same as customers.deal_analyzer but dispatched via the ToolLoopAgent engine. Used by TC-AI-AGENT-LOOP-006 mutation-gate proof scenario.',\n executionEngine: 'tool-loop-agent',\n}\n\nexport const aiAgents: AiAgentDefinition[] = [agent, dealAnalyzer, dealAnalyzerToolLoop]\n\nexport default aiAgents\n"],
|
|
5
|
+
"mappings": "AA2BA,SAAS,sCAAsC;AAuB/C,MAAM,WAAW;AACjB,MAAM,YAAY;AAElB,MAAM,gBAAmC;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,oBAAuC;AAAA,EAC3C;AAAA,EACA;AAAA,EACA;AACF;AAEA,MAAM,kBAAmC;AAAA,EACvC;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IAAI;AAAA,EACb;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IAAI;AAAA,EACb;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IAAI;AAAA,EACb;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IAAI;AAAA,EACb;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IAAI;AAAA,EACb;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IAAI;AAAA,EACb;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,OAAO;AAAA,IACP,SAAS;AAAA,MACP;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,KAAK,IA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AAAA,IACV;AAAA,IACA;AAAA,MACE,OAAO;AAAA,MACP,QAAQ;AAAA,IACV;AAAA,EACF;AACF;AAIA,MAAM,uBAA0C;AAAA,EAC9C,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,OAAO;AAAA,EACP,aACE;AAAA,EACF,iBAAiB;AACnB;AAEO,MAAM,WAAgC,CAAC,OAAO,cAAc,oBAAoB;AAEvF,IAAO,oBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -14,6 +14,9 @@ function buildScope(ctx, tenantId) {
|
|
|
14
14
|
return { tenantId, organizationId: ctx.organizationId };
|
|
15
15
|
}
|
|
16
16
|
const getSettingsInput = z.object({}).passthrough();
|
|
17
|
+
const listPipelineStagesInput = z.object({
|
|
18
|
+
pipelineId: z.string().uuid().optional().describe("Optional pipeline id to restrict returned stages.")
|
|
19
|
+
}).passthrough();
|
|
17
20
|
const getSettingsTool = {
|
|
18
21
|
name: "customers.get_settings",
|
|
19
22
|
displayName: "Get customers module settings",
|
|
@@ -93,7 +96,58 @@ const getSettingsTool = {
|
|
|
93
96
|
};
|
|
94
97
|
}
|
|
95
98
|
};
|
|
96
|
-
const
|
|
99
|
+
const listPipelineStagesTool = {
|
|
100
|
+
name: "customers.list_pipeline_stages",
|
|
101
|
+
displayName: "List customer pipeline stages",
|
|
102
|
+
description: "Return customer deal pipelines and their pipeline stages, including stage ids. Use this before moving a deal to a named pipeline stage.",
|
|
103
|
+
inputSchema: listPipelineStagesInput,
|
|
104
|
+
requiredFeatures: ["customers.pipelines.view"],
|
|
105
|
+
tags: ["read", "customers", "pipelines"],
|
|
106
|
+
isMutation: false,
|
|
107
|
+
handler: async (rawInput, ctx) => {
|
|
108
|
+
const { tenantId } = assertTenantScope(ctx);
|
|
109
|
+
const input = listPipelineStagesInput.parse(rawInput);
|
|
110
|
+
const em = resolveEm(ctx);
|
|
111
|
+
const where = { tenantId };
|
|
112
|
+
if (ctx.organizationId) where.organizationId = ctx.organizationId;
|
|
113
|
+
const stageWhere = { ...where };
|
|
114
|
+
if (input.pipelineId) stageWhere.pipelineId = input.pipelineId;
|
|
115
|
+
const [pipelines, stages] = await Promise.all([
|
|
116
|
+
findWithDecryption(
|
|
117
|
+
em,
|
|
118
|
+
CustomerPipeline,
|
|
119
|
+
where,
|
|
120
|
+
{ orderBy: { createdAt: "asc" } },
|
|
121
|
+
buildScope(ctx, tenantId)
|
|
122
|
+
),
|
|
123
|
+
findWithDecryption(
|
|
124
|
+
em,
|
|
125
|
+
CustomerPipelineStage,
|
|
126
|
+
stageWhere,
|
|
127
|
+
{ orderBy: { pipelineId: "asc", order: "asc" } },
|
|
128
|
+
buildScope(ctx, tenantId)
|
|
129
|
+
)
|
|
130
|
+
]);
|
|
131
|
+
return {
|
|
132
|
+
pipelines: pipelines.filter((row) => row.tenantId === tenantId).map((row) => ({
|
|
133
|
+
id: row.id,
|
|
134
|
+
name: row.name,
|
|
135
|
+
isDefault: !!row.isDefault,
|
|
136
|
+
organizationId: row.organizationId ?? null,
|
|
137
|
+
tenantId: row.tenantId ?? null
|
|
138
|
+
})),
|
|
139
|
+
pipelineStages: stages.filter((row) => row.tenantId === tenantId).map((row) => ({
|
|
140
|
+
id: row.id,
|
|
141
|
+
pipelineId: row.pipelineId,
|
|
142
|
+
label: row.label,
|
|
143
|
+
order: row.order,
|
|
144
|
+
organizationId: row.organizationId ?? null,
|
|
145
|
+
tenantId: row.tenantId ?? null
|
|
146
|
+
}))
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const settingsAiTools = [getSettingsTool, listPipelineStagesTool];
|
|
97
151
|
var settings_pack_default = settingsAiTools;
|
|
98
152
|
export {
|
|
99
153
|
settings_pack_default as default,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/customers/ai-tools/settings-pack.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * `customers.get_settings` (Phase 1 WS-C, Step 3.9).\n *\n * Aggregates the four settings surfaces the spec calls out: pipelines,\n * pipeline stages, dictionaries, and address-format settings. All reads are\n * tenant + organization scoped through the existing encryption helpers.\n */\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { z } from 'zod'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport {\n CustomerDictionaryEntry,\n CustomerPipeline,\n CustomerPipelineStage,\n CustomerSettings,\n} from '../data/entities'\nimport { assertTenantScope, type CustomersAiToolDefinition, type CustomersToolContext } from './types'\n\nfunction resolveEm(ctx: CustomersToolContext): EntityManager {\n return ctx.container.resolve<EntityManager>('em')\n}\n\nfunction buildScope(ctx: CustomersToolContext, tenantId: string) {\n return { tenantId, organizationId: ctx.organizationId }\n}\n\nconst getSettingsInput = z.object({}).passthrough()\n\nconst getSettingsTool: CustomersAiToolDefinition = {\n name: 'customers.get_settings',\n displayName: 'Get customers module settings',\n description:\n 'Return the customers module settings for the caller scope: pipelines, pipeline stages, dictionaries (grouped by kind), and address format.',\n inputSchema: getSettingsInput,\n requiredFeatures: ['customers.settings.manage'],\n tags: ['read', 'customers'],\n handler: async (_rawInput, ctx) => {\n const { tenantId } = assertTenantScope(ctx)\n const em = resolveEm(ctx)\n const where: Record<string, unknown> = { tenantId }\n if (ctx.organizationId) where.organizationId = ctx.organizationId\n const [pipelines, stages, dictionaryEntries, settings] = await Promise.all([\n findWithDecryption<CustomerPipeline>(\n em,\n CustomerPipeline,\n where as any,\n { orderBy: { createdAt: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n findWithDecryption<CustomerPipelineStage>(\n em,\n CustomerPipelineStage,\n where as any,\n { orderBy: { pipelineId: 'asc', order: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n findWithDecryption<CustomerDictionaryEntry>(\n em,\n CustomerDictionaryEntry,\n where as any,\n { orderBy: { kind: 'asc', label: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n ctx.organizationId\n ? findOneWithDecryption<CustomerSettings>(\n em,\n CustomerSettings,\n { tenantId, organizationId: ctx.organizationId } as any,\n undefined,\n buildScope(ctx, tenantId),\n )\n : null,\n ])\n const pipelineRows = pipelines.filter((row) => row.tenantId === tenantId)\n const stageRows = stages.filter((row) => row.tenantId === tenantId)\n const dictionaryRows = dictionaryEntries.filter((row) => row.tenantId === tenantId)\n const dictionaries: Record<string, Array<{\n id: string\n value: string\n label: string\n normalizedValue: string\n color: string | null\n icon: string | null\n }>> = {}\n for (const row of dictionaryRows) {\n const bucket = dictionaries[row.kind] ?? (dictionaries[row.kind] = [])\n bucket.push({\n id: row.id,\n value: row.value,\n label: row.label,\n normalizedValue: row.normalizedValue,\n color: row.color ?? null,\n icon: row.icon ?? null,\n })\n }\n return {\n pipelines: pipelineRows.map((row) => ({\n id: row.id,\n name: row.name,\n isDefault: !!row.isDefault,\n organizationId: row.organizationId ?? null,\n tenantId: row.tenantId ?? null,\n createdAt: row.createdAt ? new Date(row.createdAt).toISOString() : null,\n })),\n pipelineStages: stageRows.map((row) => ({\n id: row.id,\n pipelineId: row.pipelineId,\n label: row.label,\n order: row.order,\n organizationId: row.organizationId ?? null,\n tenantId: row.tenantId ?? null,\n })),\n dictionaries,\n addressFormat: settings?.addressFormat ?? 'line_first',\n }\n },\n}\n\nexport const settingsAiTools: CustomersAiToolDefinition[] = [getSettingsTool]\n\nexport default settingsAiTools\n"],
|
|
5
|
-
"mappings": "AAQA,SAAS,SAAS;AAClB,SAAS,uBAAuB,0BAA0B;AAC1D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,yBAAoF;AAE7F,SAAS,UAAU,KAA0C;AAC3D,SAAO,IAAI,UAAU,QAAuB,IAAI;AAClD;AAEA,SAAS,WAAW,KAA2B,UAAkB;AAC/D,SAAO,EAAE,UAAU,gBAAgB,IAAI,eAAe;AACxD;AAEA,MAAM,mBAAmB,EAAE,OAAO,CAAC,CAAC,EAA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AOA,CAAC;AACP,eAAW,OAAO,gBAAgB;AAChC,YAAM,SAAS,aAAa,IAAI,IAAI,MAAM,aAAa,IAAI,IAAI,IAAI,CAAC;AACpE,aAAO,KAAK;AAAA,QACV,IAAI,IAAI;AAAA,QACR,OAAO,IAAI;AAAA,QACX,OAAO,IAAI;AAAA,QACX,iBAAiB,IAAI;AAAA,QACrB,OAAO,IAAI,SAAS;AAAA,QACpB,MAAM,IAAI,QAAQ;AAAA,MACpB,CAAC;AAAA,IACH;AACA,WAAO;AAAA,MACL,WAAW,aAAa,IAAI,CAAC,SAAS;AAAA,QACpC,IAAI,IAAI;AAAA,QACR,MAAM,IAAI;AAAA,QACV,WAAW,CAAC,CAAC,IAAI;AAAA,QACjB,gBAAgB,IAAI,kBAAkB;AAAA,QACtC,UAAU,IAAI,YAAY;AAAA,QAC1B,WAAW,IAAI,YAAY,IAAI,KAAK,IAAI,SAAS,EAAE,YAAY,IAAI;AAAA,MACrE,EAAE;AAAA,MACF,gBAAgB,UAAU,IAAI,CAAC,SAAS;AAAA,QACtC,IAAI,IAAI;AAAA,QACR,YAAY,IAAI;AAAA,QAChB,OAAO,IAAI;AAAA,QACX,OAAO,IAAI;AAAA,QACX,gBAAgB,IAAI,kBAAkB;AAAA,QACtC,UAAU,IAAI,YAAY;AAAA,MAC5B,EAAE;AAAA,MACF;AAAA,MACA,eAAe,UAAU,iBAAiB;AAAA,IAC5C;AAAA,EACF;AACF;AAEO,MAAM,kBAA+C,CAAC,
|
|
4
|
+
"sourcesContent": ["/**\n * `customers.get_settings` (Phase 1 WS-C, Step 3.9).\n *\n * Aggregates the four settings surfaces the spec calls out: pipelines,\n * pipeline stages, dictionaries, and address-format settings. All reads are\n * tenant + organization scoped through the existing encryption helpers.\n */\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { z } from 'zod'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport {\n CustomerDictionaryEntry,\n CustomerPipeline,\n CustomerPipelineStage,\n CustomerSettings,\n} from '../data/entities'\nimport { assertTenantScope, type CustomersAiToolDefinition, type CustomersToolContext } from './types'\n\nfunction resolveEm(ctx: CustomersToolContext): EntityManager {\n return ctx.container.resolve<EntityManager>('em')\n}\n\nfunction buildScope(ctx: CustomersToolContext, tenantId: string) {\n return { tenantId, organizationId: ctx.organizationId }\n}\n\nconst getSettingsInput = z.object({}).passthrough()\n\nconst listPipelineStagesInput = z.object({\n pipelineId: z.string().uuid().optional().describe('Optional pipeline id to restrict returned stages.'),\n}).passthrough()\n\nconst getSettingsTool: CustomersAiToolDefinition = {\n name: 'customers.get_settings',\n displayName: 'Get customers module settings',\n description:\n 'Return the customers module settings for the caller scope: pipelines, pipeline stages, dictionaries (grouped by kind), and address format.',\n inputSchema: getSettingsInput,\n requiredFeatures: ['customers.settings.manage'],\n tags: ['read', 'customers'],\n handler: async (_rawInput, ctx) => {\n const { tenantId } = assertTenantScope(ctx)\n const em = resolveEm(ctx)\n const where: Record<string, unknown> = { tenantId }\n if (ctx.organizationId) where.organizationId = ctx.organizationId\n const [pipelines, stages, dictionaryEntries, settings] = await Promise.all([\n findWithDecryption<CustomerPipeline>(\n em,\n CustomerPipeline,\n where as any,\n { orderBy: { createdAt: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n findWithDecryption<CustomerPipelineStage>(\n em,\n CustomerPipelineStage,\n where as any,\n { orderBy: { pipelineId: 'asc', order: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n findWithDecryption<CustomerDictionaryEntry>(\n em,\n CustomerDictionaryEntry,\n where as any,\n { orderBy: { kind: 'asc', label: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n ctx.organizationId\n ? findOneWithDecryption<CustomerSettings>(\n em,\n CustomerSettings,\n { tenantId, organizationId: ctx.organizationId } as any,\n undefined,\n buildScope(ctx, tenantId),\n )\n : null,\n ])\n const pipelineRows = pipelines.filter((row) => row.tenantId === tenantId)\n const stageRows = stages.filter((row) => row.tenantId === tenantId)\n const dictionaryRows = dictionaryEntries.filter((row) => row.tenantId === tenantId)\n const dictionaries: Record<string, Array<{\n id: string\n value: string\n label: string\n normalizedValue: string\n color: string | null\n icon: string | null\n }>> = {}\n for (const row of dictionaryRows) {\n const bucket = dictionaries[row.kind] ?? (dictionaries[row.kind] = [])\n bucket.push({\n id: row.id,\n value: row.value,\n label: row.label,\n normalizedValue: row.normalizedValue,\n color: row.color ?? null,\n icon: row.icon ?? null,\n })\n }\n return {\n pipelines: pipelineRows.map((row) => ({\n id: row.id,\n name: row.name,\n isDefault: !!row.isDefault,\n organizationId: row.organizationId ?? null,\n tenantId: row.tenantId ?? null,\n createdAt: row.createdAt ? new Date(row.createdAt).toISOString() : null,\n })),\n pipelineStages: stageRows.map((row) => ({\n id: row.id,\n pipelineId: row.pipelineId,\n label: row.label,\n order: row.order,\n organizationId: row.organizationId ?? null,\n tenantId: row.tenantId ?? null,\n })),\n dictionaries,\n addressFormat: settings?.addressFormat ?? 'line_first',\n }\n },\n}\n\nconst listPipelineStagesTool: CustomersAiToolDefinition = {\n name: 'customers.list_pipeline_stages',\n displayName: 'List customer pipeline stages',\n description:\n 'Return customer deal pipelines and their pipeline stages, including stage ids. Use this before moving a deal to a named pipeline stage.',\n inputSchema: listPipelineStagesInput,\n requiredFeatures: ['customers.pipelines.view'],\n tags: ['read', 'customers', 'pipelines'],\n isMutation: false,\n handler: async (rawInput, ctx) => {\n const { tenantId } = assertTenantScope(ctx)\n const input = listPipelineStagesInput.parse(rawInput)\n const em = resolveEm(ctx)\n const where: Record<string, unknown> = { tenantId }\n if (ctx.organizationId) where.organizationId = ctx.organizationId\n const stageWhere: Record<string, unknown> = { ...where }\n if (input.pipelineId) stageWhere.pipelineId = input.pipelineId\n const [pipelines, stages] = await Promise.all([\n findWithDecryption<CustomerPipeline>(\n em,\n CustomerPipeline,\n where as any,\n { orderBy: { createdAt: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n findWithDecryption<CustomerPipelineStage>(\n em,\n CustomerPipelineStage,\n stageWhere as any,\n { orderBy: { pipelineId: 'asc', order: 'asc' } as any } as any,\n buildScope(ctx, tenantId),\n ),\n ])\n return {\n pipelines: pipelines\n .filter((row) => row.tenantId === tenantId)\n .map((row) => ({\n id: row.id,\n name: row.name,\n isDefault: !!row.isDefault,\n organizationId: row.organizationId ?? null,\n tenantId: row.tenantId ?? null,\n })),\n pipelineStages: stages\n .filter((row) => row.tenantId === tenantId)\n .map((row) => ({\n id: row.id,\n pipelineId: row.pipelineId,\n label: row.label,\n order: row.order,\n organizationId: row.organizationId ?? null,\n tenantId: row.tenantId ?? null,\n })),\n }\n },\n}\n\nexport const settingsAiTools: CustomersAiToolDefinition[] = [getSettingsTool, listPipelineStagesTool]\n\nexport default settingsAiTools\n"],
|
|
5
|
+
"mappings": "AAQA,SAAS,SAAS;AAClB,SAAS,uBAAuB,0BAA0B;AAC1D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,yBAAoF;AAE7F,SAAS,UAAU,KAA0C;AAC3D,SAAO,IAAI,UAAU,QAAuB,IAAI;AAClD;AAEA,SAAS,WAAW,KAA2B,UAAkB;AAC/D,SAAO,EAAE,UAAU,gBAAgB,IAAI,eAAe;AACxD;AAEA,MAAM,mBAAmB,EAAE,OAAO,CAAC,CAAC,EAAE,YAAY;AAElD,MAAM,0BAA0B,EAAE,OAAO;AAAA,EACvC,YAAY,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,mDAAmD;AACvG,CAAC,EAAE,YAAY;AAEf,MAAM,kBAA6C;AAAA,EACjD,MAAM;AAAA,EACN,aAAa;AAAA,EACb,aACE;AAAA,EACF,aAAa;AAAA,EACb,kBAAkB,CAAC,2BAA2B;AAAA,EAC9C,MAAM,CAAC,QAAQ,WAAW;AAAA,EAC1B,SAAS,OAAO,WAAW,QAAQ;AACjC,UAAM,EAAE,SAAS,IAAI,kBAAkB,GAAG;AAC1C,U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,QACA,EAAE,SAAS,EAAE,WAAW,MAAM,EAAS;AAAA,QACvC,WAAW,KAAK,QAAQ;AAAA,MAC1B;AAAA,MACA;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,QACA,EAAE,SAAS,EAAE,YAAY,OAAO,OAAO,MAAM,EAAS;AAAA,QACtD,WAAW,KAAK,QAAQ;AAAA,MAC1B;AAAA,IACF,CAAC;AACD,WAAO;AAAA,MACL,WAAW,UACR,OAAO,CAAC,QAAQ,IAAI,aAAa,QAAQ,EACzC,IAAI,CAAC,SAAS;AAAA,QACb,IAAI,IAAI;AAAA,QACR,MAAM,IAAI;AAAA,QACV,WAAW,CAAC,CAAC,IAAI;AAAA,QACjB,gBAAgB,IAAI,kBAAkB;AAAA,QACtC,UAAU,IAAI,YAAY;AAAA,MAC5B,EAAE;AAAA,MACJ,gBAAgB,OACb,OAAO,CAAC,QAAQ,IAAI,aAAa,QAAQ,EACzC,IAAI,CAAC,SAAS;AAAA,QACb,IAAI,IAAI;AAAA,QACR,YAAY,IAAI;AAAA,QAChB,OAAO,IAAI;AAAA,QACX,OAAO,IAAI;AAAA,QACX,gBAAgB,IAAI,kBAAkB;AAAA,QACtC,UAAU,IAAI,YAAY;AAAA,MAC5B,EAAE;AAAA,IACN;AAAA,EACF;AACF;AAEO,MAAM,kBAA+C,CAAC,iBAAiB,sBAAsB;AAEpG,IAAO,wBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -103,10 +103,10 @@ async function GET(req) {
|
|
|
103
103
|
}
|
|
104
104
|
if (input.hasAttachments !== void 0) {
|
|
105
105
|
const existsFn = (eb) => eb.exists(
|
|
106
|
-
eb.selectFrom("attachments").select(sql`1`.as("one")).where("attachments.entity_id", "=", MESSAGE_ATTACHMENT_ENTITY_ID).
|
|
106
|
+
eb.selectFrom("attachments").select(sql`1`.as("one")).where("attachments.entity_id", "=", MESSAGE_ATTACHMENT_ENTITY_ID).where(sql`attachments.record_id = m.id::text`)
|
|
107
107
|
);
|
|
108
108
|
const notExistsFn = (eb) => eb.not(eb.exists(
|
|
109
|
-
eb.selectFrom("attachments").select(sql`1`.as("one")).where("attachments.entity_id", "=", MESSAGE_ATTACHMENT_ENTITY_ID).
|
|
109
|
+
eb.selectFrom("attachments").select(sql`1`.as("one")).where("attachments.entity_id", "=", MESSAGE_ATTACHMENT_ENTITY_ID).where(sql`attachments.record_id = m.id::text`)
|
|
110
110
|
));
|
|
111
111
|
q = input.hasAttachments ? q.where(existsFn) : q.where(notExistsFn);
|
|
112
112
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/messages/api/route.ts"],
|
|
4
|
-
"sourcesContent": ["import { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { type Kysely, sql } from 'kysely'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands/command-bus'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi/types'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { hashForLookup } from '@open-mercato/shared/lib/encryption/aes'\nimport { User } from '../../auth/data/entities'\nimport { Message, MessageObject } from '../data/entities'\nimport { composeMessageSchema, listMessagesSchema } from '../data/validators'\nimport { MESSAGE_ATTACHMENT_ENTITY_ID } from '../lib/constants'\nimport { getMessageType } from '../lib/message-types-registry'\nimport { validateMessageObjectsForType } from '../lib/object-validation'\nimport { attachOperationMetadataHeader } from '../lib/operationMetadata'\nimport { canUseMessageEmailFeature, resolveMessageContext } from '../lib/routeHelpers'\nimport { findMessageIdsBySearchTokens } from '../lib/searchLookup'\nimport { MessageCommandExecuteResult } from '../commands/shared'\nimport {\n composeMessageSchema as composeSchema,\n composeResponseSchema,\n listMessagesSchema as listSchema,\n messageListItemSchema,\n} from './openapi'\n\ntype MessageCommandExecuteResultWithThreadId = MessageCommandExecuteResult & {\n threadId: string\n}\n\nconst NO_MATCH_ID = '00000000-0000-0000-0000-000000000000'\n\nfunction getDb(em: EntityManager): Kysely<any> {\n return em.getKysely<any>()\n}\n\ntype MessageListScopeRow = {\n id: string\n sender_user_id: string\n is_draft: boolean\n recipient_status: string | null\n read_at: string | null\n}\n\ntype AttachmentCountRow = {\n record_id: string\n count: string | number\n}\n\ntype RecipientCountRow = {\n message_id: string\n count: string | number\n}\n\nexport const metadata = {\n GET: { requireAuth: true },\n POST: { requireAuth: true, requireFeatures: ['messages.compose'] },\n}\n\nexport async function GET(req: Request) {\n const { ctx, scope } = await resolveMessageContext(req)\n const em = ctx.container.resolve('em') as EntityManager\n const url = new URL(req.url)\n const params = Object.fromEntries(url.searchParams)\n const input = listMessagesSchema.parse(params)\n const db = getDb(em) as any\n\n const searchIds = input.search\n ? await findMessageIdsBySearchTokens({\n em,\n query: input.search,\n tenantId: scope.tenantId ?? null,\n organizationId: scope.organizationId,\n })\n : undefined\n\n const buildBaseQuery = () => {\n let q: any = db\n .selectFrom('messages as m')\n .where('m.tenant_id', '=', scope.tenantId)\n .where('m.deleted_at', 'is', null)\n\n if (scope.organizationId) {\n q = q.where('m.organization_id', '=', scope.organizationId)\n } else {\n q = q.where('m.organization_id', 'is', null)\n }\n\n const joinRecipient = () => {\n q = q.leftJoin('message_recipients as r', (jb: any) => jb\n .onRef('m.id', '=', 'r.message_id')\n .on('r.recipient_user_id', '=', scope.userId))\n }\n\n switch (input.folder) {\n case 'inbox':\n joinRecipient()\n q = q\n .where('r.message_id', 'is not', null)\n .where('r.deleted_at', 'is', null)\n .where('r.archived_at', 'is', null)\n .where('m.is_draft', '=', false)\n break\n case 'archived':\n joinRecipient()\n q = q\n .where('r.message_id', 'is not', null)\n .where('r.deleted_at', 'is', null)\n .where('r.archived_at', 'is not', null)\n break\n case 'sent':\n q = q\n .where('m.sender_user_id', '=', scope.userId)\n .where('m.is_draft', '=', false)\n joinRecipient()\n break\n case 'drafts':\n q = q\n .where('m.sender_user_id', '=', scope.userId)\n .where('m.is_draft', '=', true)\n joinRecipient()\n break\n case 'all':\n joinRecipient()\n q = q.where((eb: any) => eb.or([\n eb('m.sender_user_id', '=', scope.userId),\n eb('r.message_id', 'is not', null),\n ]))\n break\n default: {\n const unsupportedFolder: never = input.folder\n throw new Error(`Unsupported folder: ${String(unsupportedFolder)}`)\n }\n }\n\n if (input.status) q = q.where('r.status', '=', input.status)\n if (input.type) q = q.where('m.type', '=', input.type)\n if (input.visibility) q = q.where('m.visibility', '=', input.visibility)\n if (input.sourceEntityType) q = q.where('m.source_entity_type', '=', input.sourceEntityType)\n if (input.sourceEntityId) q = q.where('m.source_entity_id', '=', input.sourceEntityId)\n if (input.externalEmail) q = q.where('m.external_email_hash', '=', hashForLookup(input.externalEmail))\n if (input.senderId) q = q.where('m.sender_user_id', '=', input.senderId)\n\n if (input.search) {\n if (!searchIds || searchIds.length === 0) {\n q = q.where('m.id', '=', NO_MATCH_ID)\n } else {\n q = q.where('m.id', 'in', searchIds)\n }\n }\n\n if (input.since) q = q.where('m.sent_at', '>', new Date(input.since))\n\n if (input.hasObjects !== undefined) {\n const existsFn = (eb: any) => eb.exists(\n eb.selectFrom('message_objects')\n .select(sql<number>`1`.as('one'))\n .whereRef('message_objects.message_id', '=', 'm.id')\n )\n const notExistsFn = (eb: any) => eb.not(eb.exists(\n eb.selectFrom('message_objects')\n .select(sql<number>`1`.as('one'))\n .whereRef('message_objects.message_id', '=', 'm.id')\n ))\n q = input.hasObjects ? q.where(existsFn) : q.where(notExistsFn)\n }\n\n if (input.hasAttachments !== undefined) {\n const existsFn = (eb: any) => eb.exists(\n eb.selectFrom('attachments')\n .select(sql<number>`1`.as('one'))\n .where('attachments.entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)\n .whereRef('attachments.record_id', '=', 'm.id')\n )\n const notExistsFn = (eb: any) => eb.not(eb.exists(\n eb.selectFrom('attachments')\n .select(sql<number>`1`.as('one'))\n .where('attachments.entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)\n .whereRef('attachments.record_id', '=', 'm.id')\n ))\n q = input.hasAttachments ? q.where(existsFn) : q.where(notExistsFn)\n }\n\n if (input.hasActions !== undefined) {\n q = input.hasActions\n ? q.where('m.action_data', 'is not', null)\n : q.where('m.action_data', 'is', null)\n }\n\n return q\n }\n\n const countResult = await buildBaseQuery()\n .select(sql<number>`count(*)`.as('count'))\n .executeTakeFirst() as { count: string | number } | undefined\n const total = Number(countResult?.count ?? 0)\n\n const offset = (input.page - 1) * input.pageSize\n const scopeRows = await buildBaseQuery()\n .select([\n 'm.id',\n 'm.sender_user_id',\n 'm.is_draft',\n 'r.status as recipient_status',\n 'r.read_at',\n ])\n .orderBy('m.sent_at', 'desc')\n .offset(offset)\n .limit(input.pageSize)\n .execute()\n\n const typedRows = scopeRows as MessageListScopeRow[]\n const messageIds = typedRows.map((row) => row.id)\n\n const messageEntities = messageIds.length > 0\n ? await findWithDecryption(\n em,\n Message,\n { id: { $in: messageIds } },\n undefined,\n { tenantId: scope.tenantId, organizationId: scope.organizationId }\n )\n : []\n\n const messagesById = new Map<string, Message>()\n for (const message of messageEntities) {\n messagesById.set(message.id, message)\n }\n\n const objects = messageIds.length > 0\n ? await em.find(MessageObject, { messageId: { $in: messageIds } })\n : []\n\n const objectsByMessage = objects.reduce((acc, obj) => {\n if (!acc[obj.messageId]) acc[obj.messageId] = []\n acc[obj.messageId].push(obj)\n return acc\n }, {} as Record<string, MessageObject[]>)\n\n const attachmentCounts: AttachmentCountRow[] = messageIds.length > 0\n ? await (getDb(em) as any)\n .selectFrom('attachments')\n .select(['record_id', sql<string>`count(*)`.as('count')])\n .where('entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)\n .where('record_id', 'in', messageIds)\n .groupBy('record_id')\n .execute()\n : []\n\n const attachmentCountByMessage = attachmentCounts.reduce((acc: Record<string, number>, row) => {\n acc[row.record_id] = Number(row.count)\n return acc\n }, {})\n\n const recipientCounts: RecipientCountRow[] = messageIds.length > 0\n ? await (getDb(em) as any)\n .selectFrom('message_recipients')\n .select(['message_id', sql<string>`count(*)`.as('count')])\n .where('message_id', 'in', messageIds)\n .where('deleted_at', 'is', null)\n .groupBy('message_id')\n .execute()\n : []\n\n const recipientCountByMessage = recipientCounts.reduce((acc: Record<string, number>, row) => {\n acc[row.message_id] = Number(row.count)\n return acc\n }, {})\n\n const senderUserIds = Array.from(new Set(typedRows.map((row) => row.sender_user_id).filter(Boolean)))\n const senderUsers = senderUserIds.length > 0\n ? await findWithDecryption(\n em,\n User,\n { id: { $in: senderUserIds } },\n undefined,\n { tenantId: scope.tenantId, organizationId: scope.organizationId }\n )\n : []\n\n const senderMetaById = new Map<string, { name: string | null; email: string | null }>()\n senderUsers.forEach((user) => {\n const name = typeof user.name === 'string' && user.name.trim().length ? user.name.trim() : null\n senderMetaById.set(user.id, { name, email: user.email ?? null })\n })\n\n return Response.json({\n items: typedRows\n .map((row) => {\n const message = messagesById.get(row.id)\n if (!message) return null\n const body = typeof message.body === 'string' ? message.body : ''\n const bodyPreview = body.substring(0, 150) + (body.length > 150 ? '...' : '')\n const actionData = message.actionData ?? null\n return {\n ...(senderMetaById.get(row.sender_user_id)\n ? {\n senderName: senderMetaById.get(row.sender_user_id)?.name ?? null,\n senderEmail: senderMetaById.get(row.sender_user_id)?.email ?? null,\n }\n : { senderName: null, senderEmail: null }),\n id: message.id,\n type: message.type,\n visibility: message.visibility ?? null,\n sourceEntityType: message.sourceEntityType ?? null,\n sourceEntityId: message.sourceEntityId ?? null,\n externalEmail: message.externalEmail ?? null,\n externalName: message.externalName ?? null,\n subject: message.subject,\n bodyPreview,\n senderUserId: message.senderUserId,\n priority: message.priority,\n status: row.recipient_status ?? (row.is_draft ? 'draft' : 'sent'),\n hasObjects: (objectsByMessage[message.id] || []).length > 0,\n objectCount: (objectsByMessage[message.id] || []).length,\n hasAttachments: (attachmentCountByMessage[message.id] || 0) > 0,\n attachmentCount: attachmentCountByMessage[message.id] || 0,\n recipientCount: recipientCountByMessage[message.id] || 0,\n hasActions:\n Boolean(actionData?.actions?.length)\n || Boolean(getMessageType(message.type)?.defaultActions?.length)\n || (objectsByMessage[message.id] || []).some((item) => item.actionRequired && Boolean(item.actionType)),\n actionTaken: message.actionTaken ?? null,\n sentAt: message.sentAt ? message.sentAt.toISOString() : null,\n readAt: row.read_at,\n threadId: message.threadId ?? null,\n }\n })\n .filter((item): item is NonNullable<typeof item> => item !== null),\n page: input.page,\n pageSize: input.pageSize,\n total,\n totalPages: Math.ceil(total / input.pageSize),\n })\n}\n\nexport async function POST(req: Request) {\n const { ctx, scope } = await resolveMessageContext(req)\n const commandBus = ctx.container.resolve('commandBus') as CommandBus\n const body = await req.json().catch(() => ({}))\n const input = composeMessageSchema.parse(body)\n\n const isPublicVisibility = input.visibility === 'public'\n const sendViaEmail = isPublicVisibility ? true : input.sendViaEmail\n if (sendViaEmail && !(await canUseMessageEmailFeature(ctx, scope))) {\n return Response.json({ error: 'Missing feature: messages.email' }, { status: 403 })\n }\n\n if (input.objects?.length) {\n const objectValidationError = validateMessageObjectsForType(input.type, input.objects)\n if (objectValidationError) {\n return Response.json({ error: objectValidationError }, { status: 400 })\n }\n }\n\n const { result, logEntry } = await commandBus.execute('messages.messages.compose', {\n input: {\n ...input,\n sendViaEmail,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n ctx: {\n container: ctx.container,\n auth: ctx.auth ?? null,\n organizationScope: null,\n selectedOrganizationId: scope.organizationId,\n organizationIds: scope.organizationId ? [scope.organizationId] : null,\n request: req,\n },\n })\n const { id: messageId, threadId: responseThreadId } = result as unknown as MessageCommandExecuteResultWithThreadId\n\n const response = Response.json({ id: messageId, threadId: responseThreadId }, { status: 201 })\n attachOperationMetadataHeader(response, logEntry, {\n resourceKind: 'messages.message',\n resourceId: messageId,\n })\n return response\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Messages',\n methods: {\n GET: {\n summary: 'List messages',\n query: listSchema,\n responses: [\n {\n status: 200,\n description: 'Message list',\n schema: z.object({\n items: z.array(messageListItemSchema),\n page: z.number(),\n pageSize: z.number(),\n total: z.number(),\n totalPages: z.number(),\n }),\n },\n ],\n },\n POST: {\n summary: 'Compose a message',\n requestBody: {\n schema: composeSchema,\n },\n responses: [\n {\n status: 201,\n description: 'Message created',\n schema: composeResponseSchema,\n },\n ],\n },\n },\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,SAAS;AAElB,SAAsB,WAAW;AAGjC,SAAS,0BAA0B;AACnC,SAAS,qBAAqB;AAC9B,SAAS,YAAY;AACrB,SAAS,SAAS,qBAAqB;AACvC,SAAS,sBAAsB,0BAA0B;AACzD,SAAS,oCAAoC;AAC7C,SAAS,sBAAsB;AAC/B,SAAS,qCAAqC;AAC9C,SAAS,qCAAqC;AAC9C,SAAS,2BAA2B,6BAA6B;AACjE,SAAS,oCAAoC;AAE7C;AAAA,EACE,wBAAwB;AAAA,EACxB;AAAA,EACA,sBAAsB;AAAA,EACtB;AAAA,OACK;AAMP,MAAM,cAAc;AAEpB,SAAS,MAAM,IAAgC;AAC7C,SAAO,GAAG,UAAe;AAC3B;AAoBO,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,KAAK;AAAA,EACzB,MAAM,EAAE,aAAa,MAAM,iBAAiB,CAAC,kBAAkB,EAAE;AACnE;AAEA,eAAsB,IAAI,KAAc;AACtC,QAAM,EAAE,KAAK,MAAM,IAAI,MAAM,sBAAsB,GAAG;AACtD,QAAM,KAAK,IAAI,UAAU,QAAQ,IAAI;AACrC,QAAM,MAAM,IAAI,IAAI,IAAI,GAAG;AAC3B,QAAM,SAAS,OAAO,YAAY,IAAI,YAAY;AAClD,QAAM,QAAQ,mBAAmB,MAAM,MAAM;AAC7C,QAAM,KAAK,MAAM,EAAE;AAEnB,QAAM,YAAY,MAAM,SACpB,MAAM,6BAA6B;AAAA,IACjC;AAAA,IACA,OAAO,MAAM;AAAA,IACb,UAAU,MAAM,YAAY;AAAA,IAC5B,gBAAgB,MAAM;AAAA,EACxB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vD,CAAC;AACD,UAAI,MAAM,aAAa,EAAE,MAAM,QAAQ,IAAI,EAAE,MAAM,WAAW;AAAA,IAChE;AAEA,QAAI,MAAM,mBAAmB,QAAW;AACtC,YAAM,WAAW,CAAC,OAAY,GAAG;AAAA,QAC/B,GAAG,WAAW,aAAa,EACxB,OAAO,OAAe,GAAG,KAAK,CAAC,EAC/B,MAAM,yBAAyB,KAAK,4BAA4B,EAChE,
|
|
4
|
+
"sourcesContent": ["import { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { type Kysely, sql } from 'kysely'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands/command-bus'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi/types'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { hashForLookup } from '@open-mercato/shared/lib/encryption/aes'\nimport { User } from '../../auth/data/entities'\nimport { Message, MessageObject } from '../data/entities'\nimport { composeMessageSchema, listMessagesSchema } from '../data/validators'\nimport { MESSAGE_ATTACHMENT_ENTITY_ID } from '../lib/constants'\nimport { getMessageType } from '../lib/message-types-registry'\nimport { validateMessageObjectsForType } from '../lib/object-validation'\nimport { attachOperationMetadataHeader } from '../lib/operationMetadata'\nimport { canUseMessageEmailFeature, resolveMessageContext } from '../lib/routeHelpers'\nimport { findMessageIdsBySearchTokens } from '../lib/searchLookup'\nimport { MessageCommandExecuteResult } from '../commands/shared'\nimport {\n composeMessageSchema as composeSchema,\n composeResponseSchema,\n listMessagesSchema as listSchema,\n messageListItemSchema,\n} from './openapi'\n\ntype MessageCommandExecuteResultWithThreadId = MessageCommandExecuteResult & {\n threadId: string\n}\n\nconst NO_MATCH_ID = '00000000-0000-0000-0000-000000000000'\n\nfunction getDb(em: EntityManager): Kysely<any> {\n return em.getKysely<any>()\n}\n\ntype MessageListScopeRow = {\n id: string\n sender_user_id: string\n is_draft: boolean\n recipient_status: string | null\n read_at: string | null\n}\n\ntype AttachmentCountRow = {\n record_id: string\n count: string | number\n}\n\ntype RecipientCountRow = {\n message_id: string\n count: string | number\n}\n\nexport const metadata = {\n GET: { requireAuth: true },\n POST: { requireAuth: true, requireFeatures: ['messages.compose'] },\n}\n\nexport async function GET(req: Request) {\n const { ctx, scope } = await resolveMessageContext(req)\n const em = ctx.container.resolve('em') as EntityManager\n const url = new URL(req.url)\n const params = Object.fromEntries(url.searchParams)\n const input = listMessagesSchema.parse(params)\n const db = getDb(em) as any\n\n const searchIds = input.search\n ? await findMessageIdsBySearchTokens({\n em,\n query: input.search,\n tenantId: scope.tenantId ?? null,\n organizationId: scope.organizationId,\n })\n : undefined\n\n const buildBaseQuery = () => {\n let q: any = db\n .selectFrom('messages as m')\n .where('m.tenant_id', '=', scope.tenantId)\n .where('m.deleted_at', 'is', null)\n\n if (scope.organizationId) {\n q = q.where('m.organization_id', '=', scope.organizationId)\n } else {\n q = q.where('m.organization_id', 'is', null)\n }\n\n const joinRecipient = () => {\n q = q.leftJoin('message_recipients as r', (jb: any) => jb\n .onRef('m.id', '=', 'r.message_id')\n .on('r.recipient_user_id', '=', scope.userId))\n }\n\n switch (input.folder) {\n case 'inbox':\n joinRecipient()\n q = q\n .where('r.message_id', 'is not', null)\n .where('r.deleted_at', 'is', null)\n .where('r.archived_at', 'is', null)\n .where('m.is_draft', '=', false)\n break\n case 'archived':\n joinRecipient()\n q = q\n .where('r.message_id', 'is not', null)\n .where('r.deleted_at', 'is', null)\n .where('r.archived_at', 'is not', null)\n break\n case 'sent':\n q = q\n .where('m.sender_user_id', '=', scope.userId)\n .where('m.is_draft', '=', false)\n joinRecipient()\n break\n case 'drafts':\n q = q\n .where('m.sender_user_id', '=', scope.userId)\n .where('m.is_draft', '=', true)\n joinRecipient()\n break\n case 'all':\n joinRecipient()\n q = q.where((eb: any) => eb.or([\n eb('m.sender_user_id', '=', scope.userId),\n eb('r.message_id', 'is not', null),\n ]))\n break\n default: {\n const unsupportedFolder: never = input.folder\n throw new Error(`Unsupported folder: ${String(unsupportedFolder)}`)\n }\n }\n\n if (input.status) q = q.where('r.status', '=', input.status)\n if (input.type) q = q.where('m.type', '=', input.type)\n if (input.visibility) q = q.where('m.visibility', '=', input.visibility)\n if (input.sourceEntityType) q = q.where('m.source_entity_type', '=', input.sourceEntityType)\n if (input.sourceEntityId) q = q.where('m.source_entity_id', '=', input.sourceEntityId)\n if (input.externalEmail) q = q.where('m.external_email_hash', '=', hashForLookup(input.externalEmail))\n if (input.senderId) q = q.where('m.sender_user_id', '=', input.senderId)\n\n if (input.search) {\n if (!searchIds || searchIds.length === 0) {\n q = q.where('m.id', '=', NO_MATCH_ID)\n } else {\n q = q.where('m.id', 'in', searchIds)\n }\n }\n\n if (input.since) q = q.where('m.sent_at', '>', new Date(input.since))\n\n if (input.hasObjects !== undefined) {\n const existsFn = (eb: any) => eb.exists(\n eb.selectFrom('message_objects')\n .select(sql<number>`1`.as('one'))\n .whereRef('message_objects.message_id', '=', 'm.id')\n )\n const notExistsFn = (eb: any) => eb.not(eb.exists(\n eb.selectFrom('message_objects')\n .select(sql<number>`1`.as('one'))\n .whereRef('message_objects.message_id', '=', 'm.id')\n ))\n q = input.hasObjects ? q.where(existsFn) : q.where(notExistsFn)\n }\n\n if (input.hasAttachments !== undefined) {\n const existsFn = (eb: any) => eb.exists(\n eb.selectFrom('attachments')\n .select(sql<number>`1`.as('one'))\n .where('attachments.entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)\n .where(sql<boolean>`attachments.record_id = m.id::text`)\n )\n const notExistsFn = (eb: any) => eb.not(eb.exists(\n eb.selectFrom('attachments')\n .select(sql<number>`1`.as('one'))\n .where('attachments.entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)\n .where(sql<boolean>`attachments.record_id = m.id::text`)\n ))\n q = input.hasAttachments ? q.where(existsFn) : q.where(notExistsFn)\n }\n\n if (input.hasActions !== undefined) {\n q = input.hasActions\n ? q.where('m.action_data', 'is not', null)\n : q.where('m.action_data', 'is', null)\n }\n\n return q\n }\n\n const countResult = await buildBaseQuery()\n .select(sql<number>`count(*)`.as('count'))\n .executeTakeFirst() as { count: string | number } | undefined\n const total = Number(countResult?.count ?? 0)\n\n const offset = (input.page - 1) * input.pageSize\n const scopeRows = await buildBaseQuery()\n .select([\n 'm.id',\n 'm.sender_user_id',\n 'm.is_draft',\n 'r.status as recipient_status',\n 'r.read_at',\n ])\n .orderBy('m.sent_at', 'desc')\n .offset(offset)\n .limit(input.pageSize)\n .execute()\n\n const typedRows = scopeRows as MessageListScopeRow[]\n const messageIds = typedRows.map((row) => row.id)\n\n const messageEntities = messageIds.length > 0\n ? await findWithDecryption(\n em,\n Message,\n { id: { $in: messageIds } },\n undefined,\n { tenantId: scope.tenantId, organizationId: scope.organizationId }\n )\n : []\n\n const messagesById = new Map<string, Message>()\n for (const message of messageEntities) {\n messagesById.set(message.id, message)\n }\n\n const objects = messageIds.length > 0\n ? await em.find(MessageObject, { messageId: { $in: messageIds } })\n : []\n\n const objectsByMessage = objects.reduce((acc, obj) => {\n if (!acc[obj.messageId]) acc[obj.messageId] = []\n acc[obj.messageId].push(obj)\n return acc\n }, {} as Record<string, MessageObject[]>)\n\n const attachmentCounts: AttachmentCountRow[] = messageIds.length > 0\n ? await (getDb(em) as any)\n .selectFrom('attachments')\n .select(['record_id', sql<string>`count(*)`.as('count')])\n .where('entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)\n .where('record_id', 'in', messageIds)\n .groupBy('record_id')\n .execute()\n : []\n\n const attachmentCountByMessage = attachmentCounts.reduce((acc: Record<string, number>, row) => {\n acc[row.record_id] = Number(row.count)\n return acc\n }, {})\n\n const recipientCounts: RecipientCountRow[] = messageIds.length > 0\n ? await (getDb(em) as any)\n .selectFrom('message_recipients')\n .select(['message_id', sql<string>`count(*)`.as('count')])\n .where('message_id', 'in', messageIds)\n .where('deleted_at', 'is', null)\n .groupBy('message_id')\n .execute()\n : []\n\n const recipientCountByMessage = recipientCounts.reduce((acc: Record<string, number>, row) => {\n acc[row.message_id] = Number(row.count)\n return acc\n }, {})\n\n const senderUserIds = Array.from(new Set(typedRows.map((row) => row.sender_user_id).filter(Boolean)))\n const senderUsers = senderUserIds.length > 0\n ? await findWithDecryption(\n em,\n User,\n { id: { $in: senderUserIds } },\n undefined,\n { tenantId: scope.tenantId, organizationId: scope.organizationId }\n )\n : []\n\n const senderMetaById = new Map<string, { name: string | null; email: string | null }>()\n senderUsers.forEach((user) => {\n const name = typeof user.name === 'string' && user.name.trim().length ? user.name.trim() : null\n senderMetaById.set(user.id, { name, email: user.email ?? null })\n })\n\n return Response.json({\n items: typedRows\n .map((row) => {\n const message = messagesById.get(row.id)\n if (!message) return null\n const body = typeof message.body === 'string' ? message.body : ''\n const bodyPreview = body.substring(0, 150) + (body.length > 150 ? '...' : '')\n const actionData = message.actionData ?? null\n return {\n ...(senderMetaById.get(row.sender_user_id)\n ? {\n senderName: senderMetaById.get(row.sender_user_id)?.name ?? null,\n senderEmail: senderMetaById.get(row.sender_user_id)?.email ?? null,\n }\n : { senderName: null, senderEmail: null }),\n id: message.id,\n type: message.type,\n visibility: message.visibility ?? null,\n sourceEntityType: message.sourceEntityType ?? null,\n sourceEntityId: message.sourceEntityId ?? null,\n externalEmail: message.externalEmail ?? null,\n externalName: message.externalName ?? null,\n subject: message.subject,\n bodyPreview,\n senderUserId: message.senderUserId,\n priority: message.priority,\n status: row.recipient_status ?? (row.is_draft ? 'draft' : 'sent'),\n hasObjects: (objectsByMessage[message.id] || []).length > 0,\n objectCount: (objectsByMessage[message.id] || []).length,\n hasAttachments: (attachmentCountByMessage[message.id] || 0) > 0,\n attachmentCount: attachmentCountByMessage[message.id] || 0,\n recipientCount: recipientCountByMessage[message.id] || 0,\n hasActions:\n Boolean(actionData?.actions?.length)\n || Boolean(getMessageType(message.type)?.defaultActions?.length)\n || (objectsByMessage[message.id] || []).some((item) => item.actionRequired && Boolean(item.actionType)),\n actionTaken: message.actionTaken ?? null,\n sentAt: message.sentAt ? message.sentAt.toISOString() : null,\n readAt: row.read_at,\n threadId: message.threadId ?? null,\n }\n })\n .filter((item): item is NonNullable<typeof item> => item !== null),\n page: input.page,\n pageSize: input.pageSize,\n total,\n totalPages: Math.ceil(total / input.pageSize),\n })\n}\n\nexport async function POST(req: Request) {\n const { ctx, scope } = await resolveMessageContext(req)\n const commandBus = ctx.container.resolve('commandBus') as CommandBus\n const body = await req.json().catch(() => ({}))\n const input = composeMessageSchema.parse(body)\n\n const isPublicVisibility = input.visibility === 'public'\n const sendViaEmail = isPublicVisibility ? true : input.sendViaEmail\n if (sendViaEmail && !(await canUseMessageEmailFeature(ctx, scope))) {\n return Response.json({ error: 'Missing feature: messages.email' }, { status: 403 })\n }\n\n if (input.objects?.length) {\n const objectValidationError = validateMessageObjectsForType(input.type, input.objects)\n if (objectValidationError) {\n return Response.json({ error: objectValidationError }, { status: 400 })\n }\n }\n\n const { result, logEntry } = await commandBus.execute('messages.messages.compose', {\n input: {\n ...input,\n sendViaEmail,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n ctx: {\n container: ctx.container,\n auth: ctx.auth ?? null,\n organizationScope: null,\n selectedOrganizationId: scope.organizationId,\n organizationIds: scope.organizationId ? [scope.organizationId] : null,\n request: req,\n },\n })\n const { id: messageId, threadId: responseThreadId } = result as unknown as MessageCommandExecuteResultWithThreadId\n\n const response = Response.json({ id: messageId, threadId: responseThreadId }, { status: 201 })\n attachOperationMetadataHeader(response, logEntry, {\n resourceKind: 'messages.message',\n resourceId: messageId,\n })\n return response\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Messages',\n methods: {\n GET: {\n summary: 'List messages',\n query: listSchema,\n responses: [\n {\n status: 200,\n description: 'Message list',\n schema: z.object({\n items: z.array(messageListItemSchema),\n page: z.number(),\n pageSize: z.number(),\n total: z.number(),\n totalPages: z.number(),\n }),\n },\n ],\n },\n POST: {\n summary: 'Compose a message',\n requestBody: {\n schema: composeSchema,\n },\n responses: [\n {\n status: 201,\n description: 'Message created',\n sch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,QAAQ,EAAE,OAAO;AAAA,YACf,OAAO,EAAE,MAAM,qBAAqB;AAAA,YACpC,MAAM,EAAE,OAAO;AAAA,YACf,UAAU,EAAE,OAAO;AAAA,YACnB,OAAO,EAAE,OAAO;AAAA,YAChB,YAAY,EAAE,OAAO;AAAA,UACvB,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,IACA,MAAM;AAAA,MACJ,SAAS;AAAA,MACT,aAAa;AAAA,QACX,QAAQ;AAAA,MACV;AAAA,MACA,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { parseBooleanFlag } from "@open-mercato/shared/lib/boolean";
|
|
2
3
|
function collectDuplicateRecipientIds(recipients) {
|
|
3
4
|
const seen = /* @__PURE__ */ new Set();
|
|
4
5
|
const duplicates = /* @__PURE__ */ new Set();
|
|
@@ -156,8 +157,8 @@ const updateDraftSchema = z.object({
|
|
|
156
157
|
externalEmail: z.string().email().optional(),
|
|
157
158
|
externalName: z.string().min(1).max(255).optional(),
|
|
158
159
|
recipients: z.array(messageRecipientSchema).optional(),
|
|
159
|
-
subject: z.string().
|
|
160
|
-
body: z.string().
|
|
160
|
+
subject: z.string().max(500).optional(),
|
|
161
|
+
body: z.string().max(5e4).optional(),
|
|
161
162
|
bodyFormat: z.enum(["text", "markdown"]).optional(),
|
|
162
163
|
priority: z.enum(["low", "normal", "high", "urgent"]).optional(),
|
|
163
164
|
objects: z.array(messageObjectSchema).optional(),
|
|
@@ -186,9 +187,9 @@ const listMessagesSchema = z.object({
|
|
|
186
187
|
sourceEntityType: z.string().optional(),
|
|
187
188
|
sourceEntityId: z.string().uuid().optional(),
|
|
188
189
|
externalEmail: z.string().email().optional(),
|
|
189
|
-
hasObjects: z.
|
|
190
|
-
hasAttachments: z.
|
|
191
|
-
hasActions: z.
|
|
190
|
+
hasObjects: z.string().transform(parseBooleanFlag).optional(),
|
|
191
|
+
hasAttachments: z.string().transform(parseBooleanFlag).optional(),
|
|
192
|
+
hasActions: z.string().transform(parseBooleanFlag).optional(),
|
|
192
193
|
senderId: z.string().uuid().optional(),
|
|
193
194
|
search: z.string().max(200).optional(),
|
|
194
195
|
since: z.string().datetime().optional(),
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/messages/data/validators.ts"],
|
|
4
|
-
"sourcesContent": ["import { z } from 'zod'\n\nfunction collectDuplicateRecipientIds(\n recipients: Array<{ userId: string }>,\n): string[] {\n const seen = new Set<string>()\n const duplicates = new Set<string>()\n for (const recipient of recipients) {\n if (seen.has(recipient.userId)) {\n duplicates.add(recipient.userId)\n continue\n }\n seen.add(recipient.userId)\n }\n return Array.from(duplicates)\n}\n\nfunction validateDefaultWithObjectsPayload(\n value: {\n type?: string\n objects?: Array<{ actionRequired?: boolean; actionType?: string; actionLabel?: string }>\n },\n ctx: z.RefinementCtx,\n): void {\n if (value.type !== 'messages.defaultWithObjects') return\n\n if (!Array.isArray(value.objects) || value.objects.length === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects'],\n message: 'at least one object is required for messages.defaultWithObjects',\n })\n return\n }\n\n value.objects.forEach((object, index) => {\n if (object.actionRequired === true) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects', index, 'actionRequired'],\n message: 'actionRequired must be false for messages.defaultWithObjects',\n })\n }\n if (typeof object.actionType === 'string' && object.actionType.trim().length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects', index, 'actionType'],\n message: 'actionType is not allowed for messages.defaultWithObjects',\n })\n }\n if (typeof object.actionLabel === 'string' && object.actionLabel.trim().length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects', index, 'actionLabel'],\n message: 'actionLabel is not allowed for messages.defaultWithObjects',\n })\n }\n })\n}\n\nexport const messageRecipientSchema = z.object({\n userId: z.string().uuid(),\n type: z.enum(['to', 'cc', 'bcc']).optional().default('to'),\n})\n\nexport const messageObjectSchema = z.object({\n entityModule: z.string().min(1),\n entityType: z.string().min(1),\n entityId: z.string().uuid(),\n actionRequired: z.boolean().optional().default(false),\n actionType: z.string().optional(),\n actionLabel: z.string().optional(),\n})\n\nexport const messageActionSchema = z.object({\n id: z.string().min(1),\n label: z.string().min(1),\n labelKey: z.string().optional(),\n variant: z.enum(['default', 'secondary', 'destructive', 'outline', 'ghost']).optional(),\n icon: z.string().optional(),\n commandId: z.string().optional(),\n href: z.string().optional(),\n isTerminal: z.boolean().optional(),\n confirmRequired: z.boolean().optional(),\n confirmMessage: z.string().optional(),\n})\n\nexport const messageActionDataSchema = z.object({\n actions: z.array(messageActionSchema),\n primaryActionId: z.string().optional(),\n expiresAt: z.string().datetime().optional(),\n})\n\nexport const composeMessageSchema = z.object({\n type: z.string().optional().default('default'),\n visibility: z.enum(['public', 'internal']).nullable().optional(),\n sourceEntityType: z.string().min(1).optional(),\n sourceEntityId: z.string().uuid().optional(),\n externalEmail: z.string().email().optional(),\n externalName: z.string().min(1).max(255).optional(),\n recipients: z.array(messageRecipientSchema).max(100).optional().default([]),\n subject: z.string().max(500).optional().default(''),\n body: z.string().max(50000).optional().default(''),\n bodyFormat: z.enum(['text', 'markdown']).optional().default('text'),\n priority: z.enum(['low', 'normal', 'high', 'urgent']).optional().default('normal'),\n objects: z.array(messageObjectSchema).optional(),\n attachmentIds: z.array(z.string().uuid()).optional(),\n attachmentRecordId: z.string().min(1).max(255).optional(),\n actionData: messageActionDataSchema.optional(),\n sendViaEmail: z.boolean().optional().default(false),\n parentMessageId: z.string().uuid().optional(),\n isDraft: z.boolean().optional().default(false),\n}).superRefine((value, ctx) => {\n const isDraft = value.isDraft ?? false\n const visibility = value.visibility ?? 'internal'\n const recipientCount = value.recipients.length\n const hasExternalEmail = Boolean(value.externalEmail?.trim())\n const hasSubject = value.subject.trim().length > 0\n const hasBody = value.body.trim().length > 0\n\n if (!isDraft) {\n if (visibility === 'public') {\n if (!hasExternalEmail) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['externalEmail'],\n message: 'externalEmail is required when visibility is public',\n })\n }\n if (recipientCount > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipients must be empty when visibility is public',\n })\n }\n } else if (recipientCount === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'at least one recipient is required when visibility is internal',\n })\n }\n\n if (!hasSubject) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['subject'],\n message: 'subject is required',\n })\n }\n if (!hasBody) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['body'],\n message: 'body is required',\n })\n }\n }\n\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n\n validateDefaultWithObjectsPayload(value, ctx)\n})\n\nexport const updateDraftSchema = z.object({\n type: z.string().optional(),\n visibility: z.enum(['public', 'internal']).nullable().optional(),\n sourceEntityType: z.string().min(1).optional(),\n sourceEntityId: z.string().uuid().optional(),\n externalEmail: z.string().email().optional(),\n externalName: z.string().min(1).max(255).optional(),\n recipients: z.array(messageRecipientSchema).optional(),\n subject: z.string().min(1).max(500).optional(),\n body: z.string().min(1).max(50000).optional(),\n bodyFormat: z.enum(['text', 'markdown']).optional(),\n priority: z.enum(['low', 'normal', 'high', 'urgent']).optional(),\n objects: z.array(messageObjectSchema).optional(),\n attachmentIds: z.array(z.string().uuid()).optional(),\n actionData: messageActionDataSchema.optional(),\n sendViaEmail: z.boolean().optional(),\n isDraft: z.literal(false).optional(),\n}).superRefine((value, ctx) => {\n if (value.recipients) {\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n }\n\n validateDefaultWithObjectsPayload(value, ctx)\n})\n\nexport const listMessagesSchema = z.object({\n folder: z.enum(['inbox', 'sent', 'drafts', 'archived', 'all']).optional().default('inbox'),\n status: z.enum(['unread', 'read', 'archived']).optional(),\n type: z.string().optional(),\n visibility: z.enum(['public', 'internal']).optional(),\n sourceEntityType: z.string().optional(),\n sourceEntityId: z.string().uuid().optional(),\n externalEmail: z.string().email().optional(),\n hasObjects: z.coerce.boolean().optional(),\n hasAttachments: z.coerce.boolean().optional(),\n hasActions: z.coerce.boolean().optional(),\n senderId: z.string().uuid().optional(),\n search: z.string().max(200).optional(),\n since: z.string().datetime().optional(),\n page: z.coerce.number().int().min(1).optional().default(1),\n pageSize: z.coerce.number().int().min(1).max(100).optional().default(20),\n})\n\nexport const forwardMessageSchema = z.object({\n recipients: z.array(messageRecipientSchema).min(1).max(100),\n body: z.string().max(50000).optional(),\n additionalBody: z.string().max(10000).optional(),\n includeAttachments: z.boolean().optional().default(true),\n sendViaEmail: z.boolean().optional().default(false),\n}).superRefine((value, ctx) => {\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n})\n\nexport const replyMessageSchema = z.object({\n body: z.string().min(1).max(50000),\n bodyFormat: z.enum(['text', 'markdown']).optional().default('text'),\n recipients: z.array(messageRecipientSchema).max(100).optional(),\n attachmentIds: z.array(z.string().uuid()).optional(),\n attachmentRecordId: z.string().min(1).max(255).optional(),\n replyAll: z.boolean().optional().default(false),\n sendViaEmail: z.boolean().optional().default(false),\n}).superRefine((value, ctx) => {\n if (!value.recipients) return\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n})\n\nexport const executeActionSchema = z.object({\n messageId: z.string().uuid(),\n actionId: z.string().min(1),\n payload: z.record(z.string(), z.unknown()).optional(),\n})\n\nexport const confirmMessageSchema = z.object({\n messageId: z.string().uuid(),\n tenantId: z.string().uuid().optional(),\n organizationId: z.string().uuid().nullable().optional(),\n confirmed: z.boolean().optional().default(true),\n})\n\nexport const messageObjectTypesQuerySchema = z.object({\n messageType: z.string().min(1),\n})\n\nexport const attachmentIdsPayloadSchema = z.object({\n attachmentIds: z.array(z.string().uuid()).min(1).max(100),\n})\n\nexport const unlinkAttachmentPayloadSchema = z.object({\n attachmentId: z.string().uuid().optional(),\n attachmentIds: z.array(z.string().uuid()).min(1).max(100).optional(),\n}).refine(\n (value) => Boolean(value.attachmentId || value.attachmentIds?.length),\n { message: 'attachmentId or attachmentIds is required' }\n)\n\nexport type ComposeMessageInput = z.infer<typeof composeMessageSchema>\nexport type UpdateDraftInput = z.infer<typeof updateDraftSchema>\nexport type ListMessagesInput = z.infer<typeof listMessagesSchema>\nexport type ForwardMessageInput = z.infer<typeof forwardMessageSchema>\nexport type ReplyMessageInput = z.infer<typeof replyMessageSchema>\nexport type ExecuteActionInput = z.infer<typeof executeActionSchema>\nexport type ConfirmMessageInput = z.infer<typeof confirmMessageSchema>\nexport type MessageObjectTypesQueryInput = z.infer<typeof messageObjectTypesQuerySchema>\nexport type AttachmentIdsPayload = z.infer<typeof attachmentIdsPayloadSchema>\nexport type UnlinkAttachmentPayload = z.infer<typeof unlinkAttachmentPayloadSchema>\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,SAAS;
|
|
4
|
+
"sourcesContent": ["import { z } from 'zod'\nimport { parseBooleanFlag } from '@open-mercato/shared/lib/boolean'\n\nfunction collectDuplicateRecipientIds(\n recipients: Array<{ userId: string }>,\n): string[] {\n const seen = new Set<string>()\n const duplicates = new Set<string>()\n for (const recipient of recipients) {\n if (seen.has(recipient.userId)) {\n duplicates.add(recipient.userId)\n continue\n }\n seen.add(recipient.userId)\n }\n return Array.from(duplicates)\n}\n\nfunction validateDefaultWithObjectsPayload(\n value: {\n type?: string\n objects?: Array<{ actionRequired?: boolean; actionType?: string; actionLabel?: string }>\n },\n ctx: z.RefinementCtx,\n): void {\n if (value.type !== 'messages.defaultWithObjects') return\n\n if (!Array.isArray(value.objects) || value.objects.length === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects'],\n message: 'at least one object is required for messages.defaultWithObjects',\n })\n return\n }\n\n value.objects.forEach((object, index) => {\n if (object.actionRequired === true) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects', index, 'actionRequired'],\n message: 'actionRequired must be false for messages.defaultWithObjects',\n })\n }\n if (typeof object.actionType === 'string' && object.actionType.trim().length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects', index, 'actionType'],\n message: 'actionType is not allowed for messages.defaultWithObjects',\n })\n }\n if (typeof object.actionLabel === 'string' && object.actionLabel.trim().length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['objects', index, 'actionLabel'],\n message: 'actionLabel is not allowed for messages.defaultWithObjects',\n })\n }\n })\n}\n\nexport const messageRecipientSchema = z.object({\n userId: z.string().uuid(),\n type: z.enum(['to', 'cc', 'bcc']).optional().default('to'),\n})\n\nexport const messageObjectSchema = z.object({\n entityModule: z.string().min(1),\n entityType: z.string().min(1),\n entityId: z.string().uuid(),\n actionRequired: z.boolean().optional().default(false),\n actionType: z.string().optional(),\n actionLabel: z.string().optional(),\n})\n\nexport const messageActionSchema = z.object({\n id: z.string().min(1),\n label: z.string().min(1),\n labelKey: z.string().optional(),\n variant: z.enum(['default', 'secondary', 'destructive', 'outline', 'ghost']).optional(),\n icon: z.string().optional(),\n commandId: z.string().optional(),\n href: z.string().optional(),\n isTerminal: z.boolean().optional(),\n confirmRequired: z.boolean().optional(),\n confirmMessage: z.string().optional(),\n})\n\nexport const messageActionDataSchema = z.object({\n actions: z.array(messageActionSchema),\n primaryActionId: z.string().optional(),\n expiresAt: z.string().datetime().optional(),\n})\n\nexport const composeMessageSchema = z.object({\n type: z.string().optional().default('default'),\n visibility: z.enum(['public', 'internal']).nullable().optional(),\n sourceEntityType: z.string().min(1).optional(),\n sourceEntityId: z.string().uuid().optional(),\n externalEmail: z.string().email().optional(),\n externalName: z.string().min(1).max(255).optional(),\n recipients: z.array(messageRecipientSchema).max(100).optional().default([]),\n subject: z.string().max(500).optional().default(''),\n body: z.string().max(50000).optional().default(''),\n bodyFormat: z.enum(['text', 'markdown']).optional().default('text'),\n priority: z.enum(['low', 'normal', 'high', 'urgent']).optional().default('normal'),\n objects: z.array(messageObjectSchema).optional(),\n attachmentIds: z.array(z.string().uuid()).optional(),\n attachmentRecordId: z.string().min(1).max(255).optional(),\n actionData: messageActionDataSchema.optional(),\n sendViaEmail: z.boolean().optional().default(false),\n parentMessageId: z.string().uuid().optional(),\n isDraft: z.boolean().optional().default(false),\n}).superRefine((value, ctx) => {\n const isDraft = value.isDraft ?? false\n const visibility = value.visibility ?? 'internal'\n const recipientCount = value.recipients.length\n const hasExternalEmail = Boolean(value.externalEmail?.trim())\n const hasSubject = value.subject.trim().length > 0\n const hasBody = value.body.trim().length > 0\n\n if (!isDraft) {\n if (visibility === 'public') {\n if (!hasExternalEmail) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['externalEmail'],\n message: 'externalEmail is required when visibility is public',\n })\n }\n if (recipientCount > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipients must be empty when visibility is public',\n })\n }\n } else if (recipientCount === 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'at least one recipient is required when visibility is internal',\n })\n }\n\n if (!hasSubject) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['subject'],\n message: 'subject is required',\n })\n }\n if (!hasBody) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['body'],\n message: 'body is required',\n })\n }\n }\n\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n\n validateDefaultWithObjectsPayload(value, ctx)\n})\n\nexport const updateDraftSchema = z.object({\n type: z.string().optional(),\n visibility: z.enum(['public', 'internal']).nullable().optional(),\n sourceEntityType: z.string().min(1).optional(),\n sourceEntityId: z.string().uuid().optional(),\n externalEmail: z.string().email().optional(),\n externalName: z.string().min(1).max(255).optional(),\n recipients: z.array(messageRecipientSchema).optional(),\n subject: z.string().max(500).optional(),\n body: z.string().max(50000).optional(),\n bodyFormat: z.enum(['text', 'markdown']).optional(),\n priority: z.enum(['low', 'normal', 'high', 'urgent']).optional(),\n objects: z.array(messageObjectSchema).optional(),\n attachmentIds: z.array(z.string().uuid()).optional(),\n actionData: messageActionDataSchema.optional(),\n sendViaEmail: z.boolean().optional(),\n isDraft: z.literal(false).optional(),\n}).superRefine((value, ctx) => {\n if (value.recipients) {\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n }\n\n validateDefaultWithObjectsPayload(value, ctx)\n})\n\nexport const listMessagesSchema = z.object({\n folder: z.enum(['inbox', 'sent', 'drafts', 'archived', 'all']).optional().default('inbox'),\n status: z.enum(['unread', 'read', 'archived']).optional(),\n type: z.string().optional(),\n visibility: z.enum(['public', 'internal']).optional(),\n sourceEntityType: z.string().optional(),\n sourceEntityId: z.string().uuid().optional(),\n externalEmail: z.string().email().optional(),\n hasObjects: z.string().transform(parseBooleanFlag).optional(),\n hasAttachments: z.string().transform(parseBooleanFlag).optional(),\n hasActions: z.string().transform(parseBooleanFlag).optional(),\n senderId: z.string().uuid().optional(),\n search: z.string().max(200).optional(),\n since: z.string().datetime().optional(),\n page: z.coerce.number().int().min(1).optional().default(1),\n pageSize: z.coerce.number().int().min(1).max(100).optional().default(20),\n})\n\nexport const forwardMessageSchema = z.object({\n recipients: z.array(messageRecipientSchema).min(1).max(100),\n body: z.string().max(50000).optional(),\n additionalBody: z.string().max(10000).optional(),\n includeAttachments: z.boolean().optional().default(true),\n sendViaEmail: z.boolean().optional().default(false),\n}).superRefine((value, ctx) => {\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n})\n\nexport const replyMessageSchema = z.object({\n body: z.string().min(1).max(50000),\n bodyFormat: z.enum(['text', 'markdown']).optional().default('text'),\n recipients: z.array(messageRecipientSchema).max(100).optional(),\n attachmentIds: z.array(z.string().uuid()).optional(),\n attachmentRecordId: z.string().min(1).max(255).optional(),\n replyAll: z.boolean().optional().default(false),\n sendViaEmail: z.boolean().optional().default(false),\n}).superRefine((value, ctx) => {\n if (!value.recipients) return\n const duplicateRecipientIds = collectDuplicateRecipientIds(value.recipients)\n if (duplicateRecipientIds.length > 0) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n path: ['recipients'],\n message: 'recipient user ids must be unique',\n })\n }\n})\n\nexport const executeActionSchema = z.object({\n messageId: z.string().uuid(),\n actionId: z.string().min(1),\n payload: z.record(z.string(), z.unknown()).optional(),\n})\n\nexport const confirmMessageSchema = z.object({\n messageId: z.string().uuid(),\n tenantId: z.string().uuid().optional(),\n organizationId: z.string().uuid().nullable().optional(),\n confirmed: z.boolean().optional().default(true),\n})\n\nexport const messageObjectTypesQuerySchema = z.object({\n messageType: z.string().min(1),\n})\n\nexport const attachmentIdsPayloadSchema = z.object({\n attachmentIds: z.array(z.string().uuid()).min(1).max(100),\n})\n\nexport const unlinkAttachmentPayloadSchema = z.object({\n attachmentId: z.string().uuid().optional(),\n attachmentIds: z.array(z.string().uuid()).min(1).max(100).optional(),\n}).refine(\n (value) => Boolean(value.attachmentId || value.attachmentIds?.length),\n { message: 'attachmentId or attachmentIds is required' }\n)\n\nexport type ComposeMessageInput = z.infer<typeof composeMessageSchema>\nexport type UpdateDraftInput = z.infer<typeof updateDraftSchema>\nexport type ListMessagesInput = z.infer<typeof listMessagesSchema>\nexport type ForwardMessageInput = z.infer<typeof forwardMessageSchema>\nexport type ReplyMessageInput = z.infer<typeof replyMessageSchema>\nexport type ExecuteActionInput = z.infer<typeof executeActionSchema>\nexport type ConfirmMessageInput = z.infer<typeof confirmMessageSchema>\nexport type MessageObjectTypesQueryInput = z.infer<typeof messageObjectTypesQuerySchema>\nexport type AttachmentIdsPayload = z.infer<typeof attachmentIdsPayloadSchema>\nexport type UnlinkAttachmentPayload = z.infer<typeof unlinkAttachmentPayloadSchema>\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS;AAClB,SAAS,wBAAwB;AAEjC,SAAS,6BACP,YACU;AACV,QAAM,OAAO,oBAAI,IAAY;AAC7B,QAAM,aAAa,oBAAI,IAAY;AACnC,aAAW,aAAa,YAAY;AAClC,QAAI,KAAK,IAAI,UAAU,MAAM,GAAG;AAC9B,iBAAW,IAAI,UAAU,MAAM;AAC/B;AAAA,IACF;AACA,SAAK,IAAI,UAAU,MAAM;AAAA,EAC3B;AACA,SAAO,MAAM,KAAK,UAAU;AAC9B;AAEA,SAAS,kCACP,OAIA,KACM;AACN,MAAI,MAAM,SAAS,8BAA+B;AAElD,MAAI,CAAC,MAAM,QAAQ,MAAM,OAAO,KAAK,MAAM,QAAQ,WAAW,GAAG;AAC/D,QAAI,SAAS;AAAA,MACX,MAAM,EAAE,aAAa;AAAA,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AAO;AAAA,EAC3C,WAAW,EAAE,OAAO,EAAE,KAAK;AAAA,EAC3B,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;AAAA,EACrC,gBAAgB,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;AAAA,EACtD,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,IAAI;AAChD,CAAC;AAEM,MAAM,gCAAgC,EAAE,OAAO;AAAA,EACpD,aAAa,EAAE,OAAO,EAAE,IAAI,CAAC;AAC/B,CAAC;AAEM,MAAM,6BAA6B,EAAE,OAAO;AAAA,EACjD,eAAe,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG;AAC1D,CAAC;AAEM,MAAM,gCAAgC,EAAE,OAAO;AAAA,EACpD,cAAc,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;AAAA,EACzC,eAAe,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,SAAS;AACrE,CAAC,EAAE;AAAA,EACD,CAAC,UAAU,QAAQ,MAAM,gBAAgB,MAAM,eAAe,MAAM;AAAA,EACpE,EAAE,SAAS,4CAA4C;AACzD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -62,8 +62,8 @@ function register(container) {
|
|
|
62
62
|
const makeUpsertHandler = (entityType) => async (payload, ctx) => {
|
|
63
63
|
try {
|
|
64
64
|
const em = ctx.resolve("em");
|
|
65
|
-
let orgId = payload?.organizationId || payload?.orgId || null;
|
|
66
|
-
let tenantId = payload?.tenantId || null;
|
|
65
|
+
let orgId = payload?.organizationId || payload?.orgId || ctx?.organizationId || null;
|
|
66
|
+
let tenantId = payload?.tenantId || ctx?.tenantId || null;
|
|
67
67
|
const id = String(payload?.id || payload?.recordId || "");
|
|
68
68
|
if (!id) return;
|
|
69
69
|
if (!orgId || !tenantId) {
|
|
@@ -110,21 +110,23 @@ function register(container) {
|
|
|
110
110
|
const makeDeleteHandler = (entityType) => async (payload, ctx) => {
|
|
111
111
|
try {
|
|
112
112
|
const em = ctx.resolve("em");
|
|
113
|
-
let orgId = payload?.organizationId || payload?.orgId || null;
|
|
113
|
+
let orgId = payload?.organizationId || payload?.orgId || ctx?.organizationId || null;
|
|
114
|
+
let tenantId = payload?.tenantId || ctx?.tenantId || null;
|
|
114
115
|
const id = String(payload?.id || payload?.recordId || "");
|
|
115
116
|
if (!id) return;
|
|
116
|
-
if (!orgId) {
|
|
117
|
+
if (!orgId || !tenantId) {
|
|
117
118
|
try {
|
|
118
119
|
const db = em.getKysely();
|
|
119
120
|
const table = resolveEntityTableName(em, entityType);
|
|
120
|
-
const row = await db.selectFrom(table).select(["organization_id"]).where("id", "=", id).executeTakeFirst();
|
|
121
|
+
const row = await db.selectFrom(table).select(["organization_id", "tenant_id"]).where("id", "=", id).executeTakeFirst();
|
|
121
122
|
orgId = row?.organization_id ?? orgId;
|
|
123
|
+
tenantId = row?.tenant_id ?? tenantId;
|
|
122
124
|
} catch {
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
try {
|
|
126
128
|
const bus2 = ctx.resolve("eventBus");
|
|
127
|
-
await bus2.emitEvent("query_index.delete_one", { entityType, recordId: id, organizationId: orgId });
|
|
129
|
+
await bus2.emitEvent("query_index.delete_one", { entityType, recordId: id, organizationId: orgId, tenantId });
|
|
128
130
|
} catch {
|
|
129
131
|
}
|
|
130
132
|
} catch {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/modules/query_index/di.ts"],
|
|
4
|
-
"sourcesContent": ["import type { AppContainer } from '@open-mercato/shared/lib/di/container'\nimport { BasicQueryEngine, resolveEntityTableName } from '@open-mercato/shared/lib/query/engine'\nimport { HybridQueryEngine } from './lib/engine'\nimport { markDeleted } from './lib/indexer'\nimport type { EventBus } from '@open-mercato/events'\nimport type { VectorIndexService } from '@open-mercato/search/vector'\n\nfunction toEntityTypeFromEvent(event: string): string | null {\n // Expect '<module>.<entity>.<action>'\n const parts = event.split('.')\n if (parts.length !== 3) return null\n const [mod, ent] = parts\n return `${mod}:${ent}`\n}\n\nexport function register(container: AppContainer) {\n // Override queryEngine with hybrid that prefers JSONB index when available\n try {\n const em = (container.resolve('em') as any)\n const basic = new BasicQueryEngine(\n em,\n undefined,\n () => {\n try {\n return container.resolve('tenantEncryptionService') as any\n } catch {\n return null\n }\n },\n )\n const hybrid = new HybridQueryEngine(\n em,\n basic,\n () => {\n try {\n return (container.resolve('eventBus') as EventBus)\n } catch {\n return null\n }\n },\n () => {\n try {\n return (container.resolve('vectorIndexService') as VectorIndexService)\n } catch {\n return null\n }\n },\n () => {\n try {\n return container.resolve('tenantEncryptionService') as any\n } catch {\n return null\n }\n },\n )\n // Replace existing registration\n ;(container as any).register({ queryEngine: { resolve: () => hybrid } })\n } catch {}\n\n // Subscribe to CRUD events and forward to query_index subscribers for unified handling\n const setup = () => {\n let bus: any\n try { bus = (container.resolve('eventBus') as any) } catch { bus = null }\n if (!bus) { setTimeout(setup, 0); return }\n\n const makeUpsertHandler = (entityType: string) => async (payload: any, ctx: any) => {\n try {\n const em = ctx.resolve('em')\n let orgId = payload?.organizationId || payload?.orgId || null\n let tenantId = payload?.tenantId || null\n const id = String(payload?.id || payload?.recordId || '')\n if (!id) return\n if (!orgId || !tenantId) {\n try {\n const db = (em as any).getKysely()\n const table = resolveEntityTableName(em, entityType)\n const row = await db\n .selectFrom(table as any)\n .select(['organization_id' as any, 'tenant_id' as any])\n .where('id' as any, '=', id)\n .executeTakeFirst() as { organization_id: string | null; tenant_id: string | null } | undefined\n orgId = row?.organization_id ?? orgId\n tenantId = row?.tenant_id ?? tenantId\n } catch {}\n }\n // Optional: only index when custom field definitions exist for this entity (org/global)\n try {\n const db = (em as any).getKysely()\n let cfQuery = db\n .selectFrom('custom_field_defs' as any)\n .select(['id' as any])\n .where('entity_id' as any, '=', entityType)\n .where('is_active' as any, '=', true)\n if (orgId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('organization_id' as any, '=', orgId),\n eb('organization_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('organization_id' as any, 'is', null as any)\n }\n if (tenantId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('tenant_id' as any, '=', tenantId),\n eb('tenant_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('tenant_id' as any, 'is', null as any)\n }\n const hasCf = await cfQuery.executeTakeFirst()\n if (!hasCf) return\n } catch {}\n try {\n const bus = ctx.resolve('eventBus') as any\n await bus.emitEvent('query_index.upsert_one', { entityType, recordId: id, organizationId: orgId, tenantId })\n } catch {}\n } catch {}\n }\n const makeDeleteHandler = (entityType: string) => async (payload: any, ctx: any) => {\n try {\n const em = ctx.resolve('em')\n let orgId = payload?.organizationId || payload?.orgId || null\n const id = String(payload?.id || payload?.recordId || '')\n if (!id) return\n if (!orgId) {\n try {\n const db = (em as any).getKysely()\n const table = resolveEntityTableName(em, entityType)\n const row = await db\n .selectFrom(table as any)\n .select(['organization_id' as any])\n .where('id' as any, '=', id)\n .executeTakeFirst() as { organization_id: string | null } | undefined\n orgId = row?.organization_id ?? orgId\n } catch {}\n }\n try {\n const bus = ctx.resolve('eventBus') as any\n await bus.emitEvent('query_index.delete_one', { entityType, recordId: id, organizationId: orgId })\n } catch {}\n } catch {}\n }\n\n // Build list of entity ids to subscribe to\n try {\n const em = (container.resolve('em') as any)\n const db = (em as any).getKysely()\n const cfEntityIds: string[] = []\n db\n .selectFrom('custom_field_defs' as any)\n .select(['entity_id' as any])\n .distinct()\n .execute()\n .then((rows: any[]) => {\n for (const r of rows || []) cfEntityIds.push(String(r.entity_id))\n })\n .catch(() => {})\n .finally(() => {\n const proceed = (ids: string[]) => {\n for (const entityType of Array.from(new Set(ids))) {\n const [mod, ent] = entityType.split(':')\n if (!mod || !ent) continue\n bus.on(`${mod}.${ent}.created`, makeUpsertHandler(entityType))\n bus.on(`${mod}.${ent}.updated`, makeUpsertHandler(entityType))\n bus.on(`${mod}.${ent}.deleted`, makeDeleteHandler(entityType))\n }\n }\n if (cfEntityIds.length > 0) {\n proceed(cfEntityIds)\n } else {\n // Fallback to generated entity ids without await\n import('#generated/entities.ids.generated').then((core) => {\n const flatten = (E: any): string[] => Object.values(E || {}).flatMap((o: any) => Object.values(o || {}) as string[])\n const guesses = new Set<string>([...flatten((core as any).E)])\n proceed(Array.from(guesses))\n }).catch(() => {})\n }\n })\n } catch {}\n }\n\n try { setup() } catch {}\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,kBAAkB,8BAA8B;AACzD,SAAS,yBAAyB;AAKlC,SAAS,sBAAsB,OAA8B;AAE3D,QAAM,QAAQ,MAAM,MAAM,GAAG;AAC7B,MAAI,MAAM,WAAW,EAAG,QAAO;AAC/B,QAAM,CAAC,KAAK,GAAG,IAAI;AACnB,SAAO,GAAG,GAAG,IAAI,GAAG;AACtB;AAEO,SAAS,SAAS,WAAyB;AAEhD,MAAI;AACF,UAAM,KAAM,UAAU,QAAQ,IAAI;AAClC,UAAM,QAAQ,IAAI;AAAA,MAChB;AAAA,MACA;AAAA,MACA,MAAM;AACJ,YAAI;AACF,iBAAO,UAAU,QAAQ,yBAAyB;AAAA,QACpD,QAAQ;AACN,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AACA,UAAM,SAAS,IAAI;AAAA,MACjB;AAAA,MACA;AAAA,MACA,MAAM;AACJ,YAAI;AACF,iBAAQ,UAAU,QAAQ,UAAU;AAAA,QACtC,QAAQ;AACN,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,MACA,MAAM;AACJ,YAAI;AACF,iBAAQ,UAAU,QAAQ,oBAAoB;AAAA,QAChD,QAAQ;AACN,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,MACA,MAAM;AACJ,YAAI;AACF,iBAAO,UAAU,QAAQ,yBAAyB;AAAA,QACpD,QAAQ;AACN,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AAEC,IAAC,UAAkB,SAAS,EAAE,aAAa,EAAE,SAAS,MAAM,OAAO,EAAE,CAAC;AAAA,EACzE,QAAQ;AAAA,EAAC;AAGT,QAAM,QAAQ,MAAM;AAClB,QAAI;AACJ,QAAI;AAAE,YAAO,UAAU,QAAQ,UAAU;AAAA,IAAU,QAAQ;AAAE,YAAM;AAAA,IAAK;AACxE,QAAI,CAAC,KAAK;AAAE,iBAAW,OAAO,CAAC;AAAG;AAAA,IAAO;AAEzC,UAAM,oBAAoB,CAAC,eAAuB,OAAO,SAAc,QAAa;AAClF,UAAI;AACF,cAAM,KAAK,IAAI,QAAQ,IAAI;AAC3B,YAAI,QAAQ,SAAS,kBAAkB,SAAS,SAAS;
|
|
4
|
+
"sourcesContent": ["import type { AppContainer } from '@open-mercato/shared/lib/di/container'\nimport { BasicQueryEngine, resolveEntityTableName } from '@open-mercato/shared/lib/query/engine'\nimport { HybridQueryEngine } from './lib/engine'\nimport { markDeleted } from './lib/indexer'\nimport type { EventBus } from '@open-mercato/events'\nimport type { VectorIndexService } from '@open-mercato/search/vector'\n\nfunction toEntityTypeFromEvent(event: string): string | null {\n // Expect '<module>.<entity>.<action>'\n const parts = event.split('.')\n if (parts.length !== 3) return null\n const [mod, ent] = parts\n return `${mod}:${ent}`\n}\n\nexport function register(container: AppContainer) {\n // Override queryEngine with hybrid that prefers JSONB index when available\n try {\n const em = (container.resolve('em') as any)\n const basic = new BasicQueryEngine(\n em,\n undefined,\n () => {\n try {\n return container.resolve('tenantEncryptionService') as any\n } catch {\n return null\n }\n },\n )\n const hybrid = new HybridQueryEngine(\n em,\n basic,\n () => {\n try {\n return (container.resolve('eventBus') as EventBus)\n } catch {\n return null\n }\n },\n () => {\n try {\n return (container.resolve('vectorIndexService') as VectorIndexService)\n } catch {\n return null\n }\n },\n () => {\n try {\n return container.resolve('tenantEncryptionService') as any\n } catch {\n return null\n }\n },\n )\n // Replace existing registration\n ;(container as any).register({ queryEngine: { resolve: () => hybrid } })\n } catch {}\n\n // Subscribe to CRUD events and forward to query_index subscribers for unified handling\n const setup = () => {\n let bus: any\n try { bus = (container.resolve('eventBus') as any) } catch { bus = null }\n if (!bus) { setTimeout(setup, 0); return }\n\n const makeUpsertHandler = (entityType: string) => async (payload: any, ctx: any) => {\n try {\n const em = ctx.resolve('em')\n let orgId = payload?.organizationId || payload?.orgId || ctx?.organizationId || null\n let tenantId = payload?.tenantId || ctx?.tenantId || null\n const id = String(payload?.id || payload?.recordId || '')\n if (!id) return\n if (!orgId || !tenantId) {\n try {\n const db = (em as any).getKysely()\n const table = resolveEntityTableName(em, entityType)\n const row = await db\n .selectFrom(table as any)\n .select(['organization_id' as any, 'tenant_id' as any])\n .where('id' as any, '=', id)\n .executeTakeFirst() as { organization_id: string | null; tenant_id: string | null } | undefined\n orgId = row?.organization_id ?? orgId\n tenantId = row?.tenant_id ?? tenantId\n } catch {}\n }\n // Optional: only index when custom field definitions exist for this entity (org/global)\n try {\n const db = (em as any).getKysely()\n let cfQuery = db\n .selectFrom('custom_field_defs' as any)\n .select(['id' as any])\n .where('entity_id' as any, '=', entityType)\n .where('is_active' as any, '=', true)\n if (orgId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('organization_id' as any, '=', orgId),\n eb('organization_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('organization_id' as any, 'is', null as any)\n }\n if (tenantId != null) {\n cfQuery = cfQuery.where((eb: any) => eb.or([\n eb('tenant_id' as any, '=', tenantId),\n eb('tenant_id' as any, 'is', null),\n ]))\n } else {\n cfQuery = cfQuery.where('tenant_id' as any, 'is', null as any)\n }\n const hasCf = await cfQuery.executeTakeFirst()\n if (!hasCf) return\n } catch {}\n try {\n const bus = ctx.resolve('eventBus') as any\n await bus.emitEvent('query_index.upsert_one', { entityType, recordId: id, organizationId: orgId, tenantId })\n } catch {}\n } catch {}\n }\n const makeDeleteHandler = (entityType: string) => async (payload: any, ctx: any) => {\n try {\n const em = ctx.resolve('em')\n let orgId = payload?.organizationId || payload?.orgId || ctx?.organizationId || null\n let tenantId = payload?.tenantId || ctx?.tenantId || null\n const id = String(payload?.id || payload?.recordId || '')\n if (!id) return\n if (!orgId || !tenantId) {\n try {\n const db = (em as any).getKysely()\n const table = resolveEntityTableName(em, entityType)\n const row = await db\n .selectFrom(table as any)\n .select(['organization_id' as any, 'tenant_id' as any])\n .where('id' as any, '=', id)\n .executeTakeFirst() as { organization_id: string | null; tenant_id: string | null } | undefined\n orgId = row?.organization_id ?? orgId\n tenantId = row?.tenant_id ?? tenantId\n } catch {}\n }\n try {\n const bus = ctx.resolve('eventBus') as any\n await bus.emitEvent('query_index.delete_one', { entityType, recordId: id, organizationId: orgId, tenantId })\n } catch {}\n } catch {}\n }\n\n // Build list of entity ids to subscribe to\n try {\n const em = (container.resolve('em') as any)\n const db = (em as any).getKysely()\n const cfEntityIds: string[] = []\n db\n .selectFrom('custom_field_defs' as any)\n .select(['entity_id' as any])\n .distinct()\n .execute()\n .then((rows: any[]) => {\n for (const r of rows || []) cfEntityIds.push(String(r.entity_id))\n })\n .catch(() => {})\n .finally(() => {\n const proceed = (ids: string[]) => {\n for (const entityType of Array.from(new Set(ids))) {\n const [mod, ent] = entityType.split(':')\n if (!mod || !ent) continue\n bus.on(`${mod}.${ent}.created`, makeUpsertHandler(entityType))\n bus.on(`${mod}.${ent}.updated`, makeUpsertHandler(entityType))\n bus.on(`${mod}.${ent}.deleted`, makeDeleteHandler(entityType))\n }\n }\n if (cfEntityIds.length > 0) {\n proceed(cfEntityIds)\n } else {\n // Fallback to generated entity ids without await\n import('#generated/entities.ids.generated').then((core) => {\n const flatten = (E: any): string[] => Object.values(E || {}).flatMap((o: any) => Object.values(o || {}) as string[])\n const guesses = new Set<string>([...flatten((core as any).E)])\n proceed(Array.from(guesses))\n }).catch(() => {})\n }\n })\n } catch {}\n }\n\n try { setup() } catch {}\n}\n"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AQ;AAAA,EAAC;AACzB;",
|
|
6
6
|
"names": ["bus"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/core",
|
|
3
|
-
"version": "0.6.3-develop.
|
|
3
|
+
"version": "0.6.3-develop.3491.1.4700ef658a",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -242,16 +242,16 @@
|
|
|
242
242
|
"zod": "^4.4.3"
|
|
243
243
|
},
|
|
244
244
|
"peerDependencies": {
|
|
245
|
-
"@open-mercato/ai-assistant": "0.6.3-develop.
|
|
246
|
-
"@open-mercato/shared": "0.6.3-develop.
|
|
247
|
-
"@open-mercato/ui": "0.6.3-develop.
|
|
245
|
+
"@open-mercato/ai-assistant": "0.6.3-develop.3491.1.4700ef658a",
|
|
246
|
+
"@open-mercato/shared": "0.6.3-develop.3491.1.4700ef658a",
|
|
247
|
+
"@open-mercato/ui": "0.6.3-develop.3491.1.4700ef658a",
|
|
248
248
|
"react": "^19.0.0",
|
|
249
249
|
"react-dom": "^19.0.0"
|
|
250
250
|
},
|
|
251
251
|
"devDependencies": {
|
|
252
|
-
"@open-mercato/ai-assistant": "0.6.3-develop.
|
|
253
|
-
"@open-mercato/shared": "0.6.3-develop.
|
|
254
|
-
"@open-mercato/ui": "0.6.3-develop.
|
|
252
|
+
"@open-mercato/ai-assistant": "0.6.3-develop.3491.1.4700ef658a",
|
|
253
|
+
"@open-mercato/shared": "0.6.3-develop.3491.1.4700ef658a",
|
|
254
|
+
"@open-mercato/ui": "0.6.3-develop.3491.1.4700ef658a",
|
|
255
255
|
"@testing-library/dom": "^10.4.1",
|
|
256
256
|
"@testing-library/jest-dom": "^6.9.1",
|
|
257
257
|
"@testing-library/react": "^16.3.1",
|
|
@@ -373,8 +373,12 @@ const DEAL_ANALYZER_PROMPT_SECTIONS: DealAnalyzerPromptSection[] = [
|
|
|
373
373
|
'2. customers.update_deal_stage — propose a stage move for a stalled high-value deal.',
|
|
374
374
|
' The runtime intercepts this via the pending-action gate; do NOT claim the change',
|
|
375
375
|
' is saved until the mutation-result-card arrives.',
|
|
376
|
+
' When moving to a named pipeline stage, first call customers.list_pipeline_stages',
|
|
377
|
+
' and pass the matching UUID as toPipelineStageId. Only use toStage for top-level',
|
|
378
|
+
' status slugs such as open, won, or lost.',
|
|
376
379
|
'Secondary read tools (use when you need more detail):',
|
|
377
380
|
'- customers.list_deals, customers.get_deal, customers.list_activities',
|
|
381
|
+
'- customers.list_pipeline_stages',
|
|
378
382
|
'- search.hybrid_search, meta.describe_agent',
|
|
379
383
|
].join('\n'),
|
|
380
384
|
},
|
|
@@ -415,7 +419,10 @@ const DEAL_ANALYZER_PROMPT_SECTIONS: DealAnalyzerPromptSection[] = [
|
|
|
415
419
|
'Lead with a one-paragraph summary of the deal portfolio health, then emit one',
|
|
416
420
|
'deal card per at-risk deal (sorted by health score ascending). After the cards,',
|
|
417
421
|
'propose exactly one stage move for the highest-value stalled deal and call',
|
|
418
|
-
'customers.update_deal_stage.
|
|
422
|
+
'customers.update_deal_stage. If the recommended stage is a pipeline stage label',
|
|
423
|
+
'rather than a status slug, resolve it with customers.list_pipeline_stages first;',
|
|
424
|
+
'do not ask the operator to paste a stage id unless no matching stage exists.',
|
|
425
|
+
'Then finish with a short conclusion naming the',
|
|
419
426
|
'highest-value stalled deal, the recommended move, and the approval status.',
|
|
420
427
|
].join('\n'),
|
|
421
428
|
},
|
|
@@ -431,6 +438,7 @@ function compileDealAnalyzerPrompt(): string {
|
|
|
431
438
|
const DEAL_ANALYZER_ALLOWED_TOOLS: readonly string[] = [
|
|
432
439
|
'customers.analyze_deals',
|
|
433
440
|
'customers.update_deal_stage',
|
|
441
|
+
'customers.list_pipeline_stages',
|
|
434
442
|
'customers.list_deals',
|
|
435
443
|
'customers.get_deal',
|
|
436
444
|
'customers.list_activities',
|
|
@@ -447,6 +455,7 @@ function buildDealAnalyzerPrepareStep() {
|
|
|
447
455
|
'customers.list_deals',
|
|
448
456
|
'customers.get_deal',
|
|
449
457
|
'customers.list_activities',
|
|
458
|
+
'customers.list_pipeline_stages',
|
|
450
459
|
'search.hybrid_search',
|
|
451
460
|
'meta.describe_agent',
|
|
452
461
|
'meta.update_task_plan',
|
|
@@ -26,6 +26,10 @@ function buildScope(ctx: CustomersToolContext, tenantId: string) {
|
|
|
26
26
|
|
|
27
27
|
const getSettingsInput = z.object({}).passthrough()
|
|
28
28
|
|
|
29
|
+
const listPipelineStagesInput = z.object({
|
|
30
|
+
pipelineId: z.string().uuid().optional().describe('Optional pipeline id to restrict returned stages.'),
|
|
31
|
+
}).passthrough()
|
|
32
|
+
|
|
29
33
|
const getSettingsTool: CustomersAiToolDefinition = {
|
|
30
34
|
name: 'customers.get_settings',
|
|
31
35
|
displayName: 'Get customers module settings',
|
|
@@ -116,6 +120,63 @@ const getSettingsTool: CustomersAiToolDefinition = {
|
|
|
116
120
|
},
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
|
|
123
|
+
const listPipelineStagesTool: CustomersAiToolDefinition = {
|
|
124
|
+
name: 'customers.list_pipeline_stages',
|
|
125
|
+
displayName: 'List customer pipeline stages',
|
|
126
|
+
description:
|
|
127
|
+
'Return customer deal pipelines and their pipeline stages, including stage ids. Use this before moving a deal to a named pipeline stage.',
|
|
128
|
+
inputSchema: listPipelineStagesInput,
|
|
129
|
+
requiredFeatures: ['customers.pipelines.view'],
|
|
130
|
+
tags: ['read', 'customers', 'pipelines'],
|
|
131
|
+
isMutation: false,
|
|
132
|
+
handler: async (rawInput, ctx) => {
|
|
133
|
+
const { tenantId } = assertTenantScope(ctx)
|
|
134
|
+
const input = listPipelineStagesInput.parse(rawInput)
|
|
135
|
+
const em = resolveEm(ctx)
|
|
136
|
+
const where: Record<string, unknown> = { tenantId }
|
|
137
|
+
if (ctx.organizationId) where.organizationId = ctx.organizationId
|
|
138
|
+
const stageWhere: Record<string, unknown> = { ...where }
|
|
139
|
+
if (input.pipelineId) stageWhere.pipelineId = input.pipelineId
|
|
140
|
+
const [pipelines, stages] = await Promise.all([
|
|
141
|
+
findWithDecryption<CustomerPipeline>(
|
|
142
|
+
em,
|
|
143
|
+
CustomerPipeline,
|
|
144
|
+
where as any,
|
|
145
|
+
{ orderBy: { createdAt: 'asc' } as any } as any,
|
|
146
|
+
buildScope(ctx, tenantId),
|
|
147
|
+
),
|
|
148
|
+
findWithDecryption<CustomerPipelineStage>(
|
|
149
|
+
em,
|
|
150
|
+
CustomerPipelineStage,
|
|
151
|
+
stageWhere as any,
|
|
152
|
+
{ orderBy: { pipelineId: 'asc', order: 'asc' } as any } as any,
|
|
153
|
+
buildScope(ctx, tenantId),
|
|
154
|
+
),
|
|
155
|
+
])
|
|
156
|
+
return {
|
|
157
|
+
pipelines: pipelines
|
|
158
|
+
.filter((row) => row.tenantId === tenantId)
|
|
159
|
+
.map((row) => ({
|
|
160
|
+
id: row.id,
|
|
161
|
+
name: row.name,
|
|
162
|
+
isDefault: !!row.isDefault,
|
|
163
|
+
organizationId: row.organizationId ?? null,
|
|
164
|
+
tenantId: row.tenantId ?? null,
|
|
165
|
+
})),
|
|
166
|
+
pipelineStages: stages
|
|
167
|
+
.filter((row) => row.tenantId === tenantId)
|
|
168
|
+
.map((row) => ({
|
|
169
|
+
id: row.id,
|
|
170
|
+
pipelineId: row.pipelineId,
|
|
171
|
+
label: row.label,
|
|
172
|
+
order: row.order,
|
|
173
|
+
organizationId: row.organizationId ?? null,
|
|
174
|
+
tenantId: row.tenantId ?? null,
|
|
175
|
+
})),
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const settingsAiTools: CustomersAiToolDefinition[] = [getSettingsTool, listPipelineStagesTool]
|
|
120
181
|
|
|
121
182
|
export default settingsAiTools
|
|
@@ -168,13 +168,13 @@ export async function GET(req: Request) {
|
|
|
168
168
|
eb.selectFrom('attachments')
|
|
169
169
|
.select(sql<number>`1`.as('one'))
|
|
170
170
|
.where('attachments.entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)
|
|
171
|
-
.
|
|
171
|
+
.where(sql<boolean>`attachments.record_id = m.id::text`)
|
|
172
172
|
)
|
|
173
173
|
const notExistsFn = (eb: any) => eb.not(eb.exists(
|
|
174
174
|
eb.selectFrom('attachments')
|
|
175
175
|
.select(sql<number>`1`.as('one'))
|
|
176
176
|
.where('attachments.entity_id', '=', MESSAGE_ATTACHMENT_ENTITY_ID)
|
|
177
|
-
.
|
|
177
|
+
.where(sql<boolean>`attachments.record_id = m.id::text`)
|
|
178
178
|
))
|
|
179
179
|
q = input.hasAttachments ? q.where(existsFn) : q.where(notExistsFn)
|
|
180
180
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod'
|
|
2
|
+
import { parseBooleanFlag } from '@open-mercato/shared/lib/boolean'
|
|
2
3
|
|
|
3
4
|
function collectDuplicateRecipientIds(
|
|
4
5
|
recipients: Array<{ userId: string }>,
|
|
@@ -178,8 +179,8 @@ export const updateDraftSchema = z.object({
|
|
|
178
179
|
externalEmail: z.string().email().optional(),
|
|
179
180
|
externalName: z.string().min(1).max(255).optional(),
|
|
180
181
|
recipients: z.array(messageRecipientSchema).optional(),
|
|
181
|
-
subject: z.string().
|
|
182
|
-
body: z.string().
|
|
182
|
+
subject: z.string().max(500).optional(),
|
|
183
|
+
body: z.string().max(50000).optional(),
|
|
183
184
|
bodyFormat: z.enum(['text', 'markdown']).optional(),
|
|
184
185
|
priority: z.enum(['low', 'normal', 'high', 'urgent']).optional(),
|
|
185
186
|
objects: z.array(messageObjectSchema).optional(),
|
|
@@ -210,9 +211,9 @@ export const listMessagesSchema = z.object({
|
|
|
210
211
|
sourceEntityType: z.string().optional(),
|
|
211
212
|
sourceEntityId: z.string().uuid().optional(),
|
|
212
213
|
externalEmail: z.string().email().optional(),
|
|
213
|
-
hasObjects: z.
|
|
214
|
-
hasAttachments: z.
|
|
215
|
-
hasActions: z.
|
|
214
|
+
hasObjects: z.string().transform(parseBooleanFlag).optional(),
|
|
215
|
+
hasAttachments: z.string().transform(parseBooleanFlag).optional(),
|
|
216
|
+
hasActions: z.string().transform(parseBooleanFlag).optional(),
|
|
216
217
|
senderId: z.string().uuid().optional(),
|
|
217
218
|
search: z.string().max(200).optional(),
|
|
218
219
|
since: z.string().datetime().optional(),
|
|
@@ -66,8 +66,8 @@ export function register(container: AppContainer) {
|
|
|
66
66
|
const makeUpsertHandler = (entityType: string) => async (payload: any, ctx: any) => {
|
|
67
67
|
try {
|
|
68
68
|
const em = ctx.resolve('em')
|
|
69
|
-
let orgId = payload?.organizationId || payload?.orgId || null
|
|
70
|
-
let tenantId = payload?.tenantId || null
|
|
69
|
+
let orgId = payload?.organizationId || payload?.orgId || ctx?.organizationId || null
|
|
70
|
+
let tenantId = payload?.tenantId || ctx?.tenantId || null
|
|
71
71
|
const id = String(payload?.id || payload?.recordId || '')
|
|
72
72
|
if (!id) return
|
|
73
73
|
if (!orgId || !tenantId) {
|
|
@@ -119,24 +119,26 @@ export function register(container: AppContainer) {
|
|
|
119
119
|
const makeDeleteHandler = (entityType: string) => async (payload: any, ctx: any) => {
|
|
120
120
|
try {
|
|
121
121
|
const em = ctx.resolve('em')
|
|
122
|
-
let orgId = payload?.organizationId || payload?.orgId || null
|
|
122
|
+
let orgId = payload?.organizationId || payload?.orgId || ctx?.organizationId || null
|
|
123
|
+
let tenantId = payload?.tenantId || ctx?.tenantId || null
|
|
123
124
|
const id = String(payload?.id || payload?.recordId || '')
|
|
124
125
|
if (!id) return
|
|
125
|
-
if (!orgId) {
|
|
126
|
+
if (!orgId || !tenantId) {
|
|
126
127
|
try {
|
|
127
128
|
const db = (em as any).getKysely()
|
|
128
129
|
const table = resolveEntityTableName(em, entityType)
|
|
129
130
|
const row = await db
|
|
130
131
|
.selectFrom(table as any)
|
|
131
|
-
.select(['organization_id' as any])
|
|
132
|
+
.select(['organization_id' as any, 'tenant_id' as any])
|
|
132
133
|
.where('id' as any, '=', id)
|
|
133
|
-
.executeTakeFirst() as { organization_id: string | null } | undefined
|
|
134
|
+
.executeTakeFirst() as { organization_id: string | null; tenant_id: string | null } | undefined
|
|
134
135
|
orgId = row?.organization_id ?? orgId
|
|
136
|
+
tenantId = row?.tenant_id ?? tenantId
|
|
135
137
|
} catch {}
|
|
136
138
|
}
|
|
137
139
|
try {
|
|
138
140
|
const bus = ctx.resolve('eventBus') as any
|
|
139
|
-
await bus.emitEvent('query_index.delete_one', { entityType, recordId: id, organizationId: orgId })
|
|
141
|
+
await bus.emitEvent('query_index.delete_one', { entityType, recordId: id, organizationId: orgId, tenantId })
|
|
140
142
|
} catch {}
|
|
141
143
|
} catch {}
|
|
142
144
|
}
|