spine-framework-portal 0.2.18 → 0.2.19
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHandler, CoreContext } from './_shared/middleware'
|
|
2
2
|
import { adminDb } from './_shared/db'
|
|
3
|
-
import { resolveTypeId } from './_shared/resolve-ids'
|
|
3
|
+
import { resolveTypeId, resolveAgentId, resolvePromptConfigId } from './_shared/resolve-ids'
|
|
4
4
|
import { create, update } from './admin-data'
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -27,8 +27,6 @@ import { create, update } from './admin-data'
|
|
|
27
27
|
|
|
28
28
|
// ─── CONSTANTS ────────────────────────────────────────────────────────────────
|
|
29
29
|
|
|
30
|
-
const TRIAGE_AGENT_ID = '01e448df-890b-4589-857b-815eadb44d81'
|
|
31
|
-
const PROMPT_CONFIG_ID = 'b778253e-cd2f-49f3-be81-836c55ed7542'
|
|
32
30
|
const CONFIDENCE_THRESHOLD = 0.75
|
|
33
31
|
|
|
34
32
|
// Helper: call admin-data.update as a nested import (entity+id go in ctx.query)
|
|
@@ -64,6 +62,8 @@ interface TypeIds {
|
|
|
64
62
|
supportTicket: string
|
|
65
63
|
thread: string
|
|
66
64
|
message: string
|
|
65
|
+
triageAgentId: string
|
|
66
|
+
promptConfigId: string
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
// ─── HELPERS ──────────────────────────────────────────────────────────────────
|
|
@@ -232,10 +232,12 @@ async function handleNewTicket(ctx: any, body: any, typeIds: TypeIds): Promise<T
|
|
|
232
232
|
if (!message) throw new Error('message is required')
|
|
233
233
|
if (!account_id || !person_id) throw new Error('User context (account + person) required')
|
|
234
234
|
|
|
235
|
+
const { triageAgentId, promptConfigId } = typeIds
|
|
236
|
+
|
|
235
237
|
// Load agent + prompt config (config tables — direct reads are fine)
|
|
236
238
|
const [{ data: agent }, { data: promptConfig }] = await Promise.all([
|
|
237
|
-
adminDb.from('ai_agents').select('*').eq('id',
|
|
238
|
-
adminDb.from('prompt_configs').select('*').eq('id',
|
|
239
|
+
adminDb.from('ai_agents').select('*').eq('id', triageAgentId).single(),
|
|
240
|
+
adminDb.from('prompt_configs').select('*').eq('id', promptConfigId).single()
|
|
239
241
|
])
|
|
240
242
|
if (!agent || !promptConfig) throw new Error('Triage agent configuration not found')
|
|
241
243
|
|
|
@@ -273,7 +275,7 @@ async function handleNewTicket(ctx: any, body: any, typeIds: TypeIds): Promise<T
|
|
|
273
275
|
status: escalate ? 'human_assigned' : 'ai_responding',
|
|
274
276
|
data: {
|
|
275
277
|
status: escalate ? 'human_assigned' : 'ai_responding',
|
|
276
|
-
aim_triage_agent_id:
|
|
278
|
+
aim_triage_agent_id: triageAgentId,
|
|
277
279
|
aim_confidence_threshold: promptConfig.confidence_threshold || CONFIDENCE_THRESHOLD,
|
|
278
280
|
aim_confidence_at_response: envelope.confidence,
|
|
279
281
|
aim_escalation_reason: escalate ? envelope.escalation_reason : 'none'
|
|
@@ -290,7 +292,7 @@ async function handleNewTicket(ctx: any, body: any, typeIds: TypeIds): Promise<T
|
|
|
290
292
|
target_id: ticketId,
|
|
291
293
|
visibility: 'external',
|
|
292
294
|
status: 'active',
|
|
293
|
-
data: { agent_id:
|
|
295
|
+
data: { agent_id: triageAgentId, prompt_config_id: promptConfigId }
|
|
294
296
|
})
|
|
295
297
|
if (!thread?.id) throw new Error('Failed to create thread')
|
|
296
298
|
const threadId = thread.id
|
|
@@ -321,7 +323,7 @@ async function handleNewTicket(ctx: any, body: any, typeIds: TypeIds): Promise<T
|
|
|
321
323
|
direction: 'outbound',
|
|
322
324
|
visibility: 'public',
|
|
323
325
|
sequence: 2,
|
|
324
|
-
data: { message_type: 'agent', agent_id:
|
|
326
|
+
data: { message_type: 'agent', agent_id: triageAgentId, confidence: envelope.confidence, escalated: escalate }
|
|
325
327
|
})
|
|
326
328
|
if (!publicMsg?.id) throw new Error('Failed to save public AI message')
|
|
327
329
|
|
|
@@ -372,10 +374,12 @@ async function handleReply(ctx: any, body: any, typeIds: TypeIds): Promise<Triag
|
|
|
372
374
|
if (!message || !thread_id || !ticket_id) throw new Error('message, thread_id, and ticket_id are required')
|
|
373
375
|
if (!account_id || !person_id) throw new Error('User context (account + person) required')
|
|
374
376
|
|
|
377
|
+
const { triageAgentId, promptConfigId } = typeIds
|
|
378
|
+
|
|
375
379
|
// Load agent + prompt config (config tables — direct reads are fine)
|
|
376
380
|
const [{ data: agent }, { data: promptConfig }] = await Promise.all([
|
|
377
|
-
adminDb.from('ai_agents').select('*').eq('id',
|
|
378
|
-
adminDb.from('prompt_configs').select('*').eq('id',
|
|
381
|
+
adminDb.from('ai_agents').select('*').eq('id', triageAgentId).single(),
|
|
382
|
+
adminDb.from('prompt_configs').select('*').eq('id', promptConfigId).single()
|
|
379
383
|
])
|
|
380
384
|
if (!agent || !promptConfig) throw new Error('Triage agent configuration not found')
|
|
381
385
|
|
|
@@ -436,7 +440,7 @@ async function handleReply(ctx: any, body: any, typeIds: TypeIds): Promise<Triag
|
|
|
436
440
|
direction: 'outbound',
|
|
437
441
|
visibility: 'public',
|
|
438
442
|
sequence: nextSeq + 1,
|
|
439
|
-
data: { message_type: 'agent', agent_id:
|
|
443
|
+
data: { message_type: 'agent', agent_id: triageAgentId, confidence: envelope.confidence, escalated: escalate }
|
|
440
444
|
})
|
|
441
445
|
if (!publicMsg?.id) throw new Error('Failed to save public AI message')
|
|
442
446
|
|
|
@@ -501,13 +505,15 @@ async function handleReply(ctx: any, body: any, typeIds: TypeIds): Promise<Triag
|
|
|
501
505
|
export const handler = createHandler(async (ctx, body) => {
|
|
502
506
|
const action = (ctx as any).query?.action
|
|
503
507
|
|
|
504
|
-
// Resolve all
|
|
505
|
-
const [supportTicket, thread, message] = await Promise.all([
|
|
508
|
+
// Resolve all IDs once, shared across both actions
|
|
509
|
+
const [supportTicket, thread, message, triageAgentId, promptConfigId] = await Promise.all([
|
|
506
510
|
resolveTypeId('item', 'support_ticket'),
|
|
507
511
|
resolveTypeId('thread', 'thread'),
|
|
508
|
-
resolveTypeId('message', 'message')
|
|
512
|
+
resolveTypeId('message', 'message'),
|
|
513
|
+
resolveAgentId('Support Triage Agent'),
|
|
514
|
+
resolvePromptConfigId('support_triage'),
|
|
509
515
|
])
|
|
510
|
-
const typeIds: TypeIds = { supportTicket, thread, message }
|
|
516
|
+
const typeIds: TypeIds = { supportTicket, thread, message, triageAgentId, promptConfigId }
|
|
511
517
|
|
|
512
518
|
switch (action) {
|
|
513
519
|
case 'new_ticket': return handleNewTicket(ctx, body, typeIds)
|