@apotech-os/support-kit 8.1.0 → 8.2.0

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/dist/index.cjs CHANGED
@@ -2976,7 +2976,15 @@ function createPlatformLlmPort(api, trace) {
2976
2976
  "createPlatformLlmPort: ctx.api.llm.complete v2 has no tool-calling surface (no `tools` parameter, no `role: 'tool'` message) \u2014 cannot honour CompletionRequest.tools."
2977
2977
  );
2978
2978
  }
2979
- const runTrace = req.trace ?? trace;
2979
+ const baseTrace = req.trace ?? trace;
2980
+ const runTrace = baseTrace && req.promptRef?.name ? {
2981
+ ...baseTrace,
2982
+ metadata: {
2983
+ ...baseTrace.metadata,
2984
+ prompt_name: req.promptRef.name,
2985
+ ...typeof req.promptRef.version === "number" ? { prompt_version: String(req.promptRef.version) } : {}
2986
+ }
2987
+ } : baseTrace;
2980
2988
  const result = await api.llm.complete({
2981
2989
  messages: req.messages.map(toPlatformMessage),
2982
2990
  options: {
@@ -3497,7 +3505,18 @@ async function respondToPendingResponse(ctx, config, recordId, options = {}) {
3497
3505
  );
3498
3506
  const testRun = options.testRun === true;
3499
3507
  const dryRun = settings.dryRun === true || testRun;
3500
- const trace = { sessionId: ticketId, tags: [`app:${config.slug}`] };
3508
+ const decisionTicketUrl = decision.data.ticketUrl;
3509
+ const trace = {
3510
+ sessionId: ticketId,
3511
+ tags: [`app:${config.slug}`, ...agent ? [`agent:${agent.name}`] : []],
3512
+ metadata: {
3513
+ ticket_id: ticketId,
3514
+ ...typeof decisionTicketUrl === "string" && decisionTicketUrl !== "" ? { conversation_url: decisionTicketUrl } : {},
3515
+ ...agent ? { model: agent.name, model_id: agent.modelId } : {},
3516
+ ...category ? { category: category.tagName } : {},
3517
+ origin
3518
+ }
3519
+ };
3501
3520
  const ports = dryRun ? withRecordingWritePorts(buildTicketPorts(ctx, trace), ctx.api) : buildTicketPorts(ctx, trace);
3502
3521
  const { clock } = ports;
3503
3522
  const conversationSnapshot = await ports.conversation.read(ref);
@@ -3613,6 +3632,29 @@ async function transitionGeneratedResponse(api, params) {
3613
3632
  }
3614
3633
  }
3615
3634
 
3635
+ // src/functions/ticket-url.ts
3636
+ async function resolveTicketUrlBase(ctx) {
3637
+ try {
3638
+ const connections = await ctx.api.integrations.list("gorgias");
3639
+ const connection = connections.find((candidate) => candidate.isDefault) ?? connections[0];
3640
+ const result = await ctx.api.integrations.run(
3641
+ "gorgias",
3642
+ "getTicketUrlBase",
3643
+ {},
3644
+ connection ? { connectionId: connection.id } : void 0
3645
+ );
3646
+ return typeof result?.base === "string" && result.base !== "" ? result.base : null;
3647
+ } catch (err) {
3648
+ ctx.logger.warn("ticket-url: could not resolve the Gorgias ticket url base, rows will carry no deep link", {
3649
+ error: err instanceof Error ? err.message : String(err)
3650
+ });
3651
+ return null;
3652
+ }
3653
+ }
3654
+ function ticketUrlFor(base, ticketId) {
3655
+ return base ? `${base}/${encodeURIComponent(ticketId)}` : null;
3656
+ }
3657
+
3616
3658
  // src/functions/triage-tick.ts
3617
3659
  function buildTriageTickFunctionDef(config) {
3618
3660
  return {
@@ -3655,30 +3697,10 @@ async function triageTickHandler(ctx, config) {
3655
3697
  }
3656
3698
  await runTriageBatch(ctx, config, settings);
3657
3699
  }
3658
- async function resolveTicketUrlBase(ctx) {
3659
- try {
3660
- const connections = await ctx.api.integrations.list("gorgias");
3661
- const connection = connections.find((candidate) => candidate.isDefault) ?? connections[0];
3662
- const result = await ctx.api.integrations.run(
3663
- "gorgias",
3664
- "getTicketUrlBase",
3665
- {},
3666
- connection ? { connectionId: connection.id } : void 0
3667
- );
3668
- return typeof result?.base === "string" && result.base !== "" ? result.base : null;
3669
- } catch (err) {
3670
- ctx.logger.warn("triage-tick: could not resolve the Gorgias ticket url base, decisions will carry no deep link", {
3671
- error: err instanceof Error ? err.message : String(err)
3672
- });
3673
- return null;
3674
- }
3675
- }
3676
- function ticketUrlFor(base, ticketId) {
3677
- return base ? `${base}/${encodeURIComponent(ticketId)}` : null;
3678
- }
3679
3700
  async function runTriageBatch(ctx, config, settings) {
3680
3701
  const dryRun = settings.dryRun === true;
3681
- const ports = dryRun ? withRecordingWritePorts(buildTicketPorts(ctx), ctx.api) : buildTicketPorts(ctx);
3702
+ const batchTrace = { tags: [`app:${config.slug}`] };
3703
+ const ports = dryRun ? withRecordingWritePorts(buildTicketPorts(ctx, batchTrace), ctx.api) : buildTicketPorts(ctx, batchTrace);
3682
3704
  const categories = await listCategories(ctx.api);
3683
3705
  const batchDefaultAgent = await resolveDraftAgent(ctx.api, null);
3684
3706
  const profile = settingsToProfile(settings, buildPromptsTable(config, draftPromptRefFor(batchDefaultAgent, config), settings.nodes));
@@ -4028,7 +4050,11 @@ async function handleApprovalHandler(ctx, config) {
4028
4050
  }
4029
4051
  return;
4030
4052
  }
4031
- const ports = buildTicketPorts(ctx);
4053
+ const ports = buildTicketPorts(ctx, {
4054
+ sessionId: ticketId,
4055
+ tags: [`app:${config.slug}`],
4056
+ metadata: { ticket_id: ticketId }
4057
+ });
4032
4058
  const { clock, state: statePort } = ports;
4033
4059
  const conversation = {
4034
4060
  ref: { id: ticketId, channel: "gorgias" },
@@ -6217,11 +6243,13 @@ async function runFollowupPass(ctx, config, rawBody, run, generateOnly) {
6217
6243
  message: `Message suivant \u2014 ticket ${ticketId}`,
6218
6244
  metadata: { ticketId, categoryTag: category.tagName, channel: body.channel, generateOnly }
6219
6245
  })).id;
6220
- const created = await openFollowupResponse(ctx, { ticketId, category, conversation, settings, runId: run.id });
6246
+ const ticketUrl = ticketUrlFor(await resolveTicketUrlBase(ctx), ticketId);
6247
+ const created = await openFollowupResponse(ctx, { ticketId, category, conversation, settings, runId: run.id, ticketUrl });
6221
6248
  if (!created) return stop(ctx, ticketId, "G9", "une autre ex\xE9cution a ouvert la r\xE9ponse en premier", ladder);
6222
6249
  const outcome2 = await respondToPendingResponse(ctx, config, created.id, { testRun: generateOnly });
6223
6250
  return {
6224
6251
  ticketId,
6252
+ ticketUrl,
6225
6253
  stoppedAt: null,
6226
6254
  reason: generateOnly ? `r\xE9ponse de suite g\xE9n\xE9r\xE9e sans envoi (cat\xE9gorie \xAB ${category.name} \xBB)` : `r\xE9ponse de suite g\xE9n\xE9r\xE9e (cat\xE9gorie \xAB ${category.name} \xBB)`,
6227
6255
  gates: completeFollowupLadder(ladder),
@@ -6276,10 +6304,11 @@ async function openFollowupResponse(ctx, input) {
6276
6304
  const { ticketId, category, conversation, settings, runId } = input;
6277
6305
  const decision = await ctx.api.create("triage-decision", {
6278
6306
  ticketId,
6279
- // Not resolved: `getTicketUrlBase` is a SECOND Gorgias call and this
6280
- // function runs once per message of the whole account. The field is
6281
- // documented nullable for exactly this kind of row.
6282
- ticketUrl: null,
6307
+ // Resolved by the caller AFTER every gate (`ticket-url.ts`), so a
6308
+ // refused message — the common case on an account-wide event stream —
6309
+ // never pays the extra Gorgias call. Still nullable: resolution
6310
+ // degrades to `null` rather than failing the pass.
6311
+ ticketUrl: input.ticketUrl,
6283
6312
  decidedAt: /* @__PURE__ */ new Date(),
6284
6313
  categoryId: category.id,
6285
6314
  tag: category.tagName,
@@ -6354,7 +6383,8 @@ function stop(ctx, ticketId, gate, reason, ladder) {
6354
6383
  categoryName: null,
6355
6384
  agentId: null,
6356
6385
  outcome: null,
6357
- responseId: null
6386
+ responseId: null,
6387
+ ticketUrl: null
6358
6388
  };
6359
6389
  }
6360
6390
  function buildTestGorgiasWebhookFunctionDef(config) {