@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.js CHANGED
@@ -1314,7 +1314,15 @@ function createPlatformLlmPort(api, trace) {
1314
1314
  "createPlatformLlmPort: ctx.api.llm.complete v2 has no tool-calling surface (no `tools` parameter, no `role: 'tool'` message) \u2014 cannot honour CompletionRequest.tools."
1315
1315
  );
1316
1316
  }
1317
- const runTrace = req.trace ?? trace;
1317
+ const baseTrace = req.trace ?? trace;
1318
+ const runTrace = baseTrace && req.promptRef?.name ? {
1319
+ ...baseTrace,
1320
+ metadata: {
1321
+ ...baseTrace.metadata,
1322
+ prompt_name: req.promptRef.name,
1323
+ ...typeof req.promptRef.version === "number" ? { prompt_version: String(req.promptRef.version) } : {}
1324
+ }
1325
+ } : baseTrace;
1318
1326
  const result = await api.llm.complete({
1319
1327
  messages: req.messages.map(toPlatformMessage),
1320
1328
  options: {
@@ -1696,7 +1704,18 @@ async function respondToPendingResponse(ctx, config, recordId, options = {}) {
1696
1704
  );
1697
1705
  const testRun = options.testRun === true;
1698
1706
  const dryRun = settings.dryRun === true || testRun;
1699
- const trace = { sessionId: ticketId, tags: [`app:${config.slug}`] };
1707
+ const decisionTicketUrl = decision.data.ticketUrl;
1708
+ const trace = {
1709
+ sessionId: ticketId,
1710
+ tags: [`app:${config.slug}`, ...agent ? [`agent:${agent.name}`] : []],
1711
+ metadata: {
1712
+ ticket_id: ticketId,
1713
+ ...typeof decisionTicketUrl === "string" && decisionTicketUrl !== "" ? { conversation_url: decisionTicketUrl } : {},
1714
+ ...agent ? { model: agent.name, model_id: agent.modelId } : {},
1715
+ ...category ? { category: category.tagName } : {},
1716
+ origin
1717
+ }
1718
+ };
1700
1719
  const ports = dryRun ? withRecordingWritePorts(buildTicketPorts(ctx, trace), ctx.api) : buildTicketPorts(ctx, trace);
1701
1720
  const { clock } = ports;
1702
1721
  const conversationSnapshot = await ports.conversation.read(ref);
@@ -1812,6 +1831,29 @@ async function transitionGeneratedResponse(api, params) {
1812
1831
  }
1813
1832
  }
1814
1833
 
1834
+ // src/functions/ticket-url.ts
1835
+ async function resolveTicketUrlBase(ctx) {
1836
+ try {
1837
+ const connections = await ctx.api.integrations.list("gorgias");
1838
+ const connection = connections.find((candidate) => candidate.isDefault) ?? connections[0];
1839
+ const result = await ctx.api.integrations.run(
1840
+ "gorgias",
1841
+ "getTicketUrlBase",
1842
+ {},
1843
+ connection ? { connectionId: connection.id } : void 0
1844
+ );
1845
+ return typeof result?.base === "string" && result.base !== "" ? result.base : null;
1846
+ } catch (err) {
1847
+ ctx.logger.warn("ticket-url: could not resolve the Gorgias ticket url base, rows will carry no deep link", {
1848
+ error: err instanceof Error ? err.message : String(err)
1849
+ });
1850
+ return null;
1851
+ }
1852
+ }
1853
+ function ticketUrlFor(base, ticketId) {
1854
+ return base ? `${base}/${encodeURIComponent(ticketId)}` : null;
1855
+ }
1856
+
1815
1857
  // src/functions/triage-tick.ts
1816
1858
  function buildTriageTickFunctionDef(config) {
1817
1859
  return {
@@ -1854,30 +1896,10 @@ async function triageTickHandler(ctx, config) {
1854
1896
  }
1855
1897
  await runTriageBatch(ctx, config, settings);
1856
1898
  }
1857
- async function resolveTicketUrlBase(ctx) {
1858
- try {
1859
- const connections = await ctx.api.integrations.list("gorgias");
1860
- const connection = connections.find((candidate) => candidate.isDefault) ?? connections[0];
1861
- const result = await ctx.api.integrations.run(
1862
- "gorgias",
1863
- "getTicketUrlBase",
1864
- {},
1865
- connection ? { connectionId: connection.id } : void 0
1866
- );
1867
- return typeof result?.base === "string" && result.base !== "" ? result.base : null;
1868
- } catch (err) {
1869
- ctx.logger.warn("triage-tick: could not resolve the Gorgias ticket url base, decisions will carry no deep link", {
1870
- error: err instanceof Error ? err.message : String(err)
1871
- });
1872
- return null;
1873
- }
1874
- }
1875
- function ticketUrlFor(base, ticketId) {
1876
- return base ? `${base}/${encodeURIComponent(ticketId)}` : null;
1877
- }
1878
1899
  async function runTriageBatch(ctx, config, settings) {
1879
1900
  const dryRun = settings.dryRun === true;
1880
- const ports = dryRun ? withRecordingWritePorts(buildTicketPorts(ctx), ctx.api) : buildTicketPorts(ctx);
1901
+ const batchTrace = { tags: [`app:${config.slug}`] };
1902
+ const ports = dryRun ? withRecordingWritePorts(buildTicketPorts(ctx, batchTrace), ctx.api) : buildTicketPorts(ctx, batchTrace);
1881
1903
  const categories = await listCategories(ctx.api);
1882
1904
  const batchDefaultAgent = await resolveDraftAgent(ctx.api, null);
1883
1905
  const profile = settingsToProfile(settings, buildPromptsTable(config, draftPromptRefFor(batchDefaultAgent, config), settings.nodes));
@@ -2227,7 +2249,11 @@ async function handleApprovalHandler(ctx, config) {
2227
2249
  }
2228
2250
  return;
2229
2251
  }
2230
- const ports = buildTicketPorts(ctx);
2252
+ const ports = buildTicketPorts(ctx, {
2253
+ sessionId: ticketId,
2254
+ tags: [`app:${config.slug}`],
2255
+ metadata: { ticket_id: ticketId }
2256
+ });
2231
2257
  const { clock, state: statePort } = ports;
2232
2258
  const conversation = {
2233
2259
  ref: { id: ticketId, channel: "gorgias" },
@@ -4384,11 +4410,13 @@ async function runFollowupPass(ctx, config, rawBody, run, generateOnly) {
4384
4410
  message: `Message suivant \u2014 ticket ${ticketId}`,
4385
4411
  metadata: { ticketId, categoryTag: category.tagName, channel: body.channel, generateOnly }
4386
4412
  })).id;
4387
- const created = await openFollowupResponse(ctx, { ticketId, category, conversation, settings, runId: run.id });
4413
+ const ticketUrl = ticketUrlFor(await resolveTicketUrlBase(ctx), ticketId);
4414
+ const created = await openFollowupResponse(ctx, { ticketId, category, conversation, settings, runId: run.id, ticketUrl });
4388
4415
  if (!created) return stop(ctx, ticketId, "G9", "une autre ex\xE9cution a ouvert la r\xE9ponse en premier", ladder);
4389
4416
  const outcome = await respondToPendingResponse(ctx, config, created.id, { testRun: generateOnly });
4390
4417
  return {
4391
4418
  ticketId,
4419
+ ticketUrl,
4392
4420
  stoppedAt: null,
4393
4421
  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)`,
4394
4422
  gates: completeFollowupLadder(ladder),
@@ -4443,10 +4471,11 @@ async function openFollowupResponse(ctx, input) {
4443
4471
  const { ticketId, category, conversation, settings, runId } = input;
4444
4472
  const decision = await ctx.api.create("triage-decision", {
4445
4473
  ticketId,
4446
- // Not resolved: `getTicketUrlBase` is a SECOND Gorgias call and this
4447
- // function runs once per message of the whole account. The field is
4448
- // documented nullable for exactly this kind of row.
4449
- ticketUrl: null,
4474
+ // Resolved by the caller AFTER every gate (`ticket-url.ts`), so a
4475
+ // refused message — the common case on an account-wide event stream —
4476
+ // never pays the extra Gorgias call. Still nullable: resolution
4477
+ // degrades to `null` rather than failing the pass.
4478
+ ticketUrl: input.ticketUrl,
4450
4479
  decidedAt: /* @__PURE__ */ new Date(),
4451
4480
  categoryId: category.id,
4452
4481
  tag: category.tagName,
@@ -4521,7 +4550,8 @@ function stop(ctx, ticketId, gate, reason, ladder) {
4521
4550
  categoryName: null,
4522
4551
  agentId: null,
4523
4552
  outcome: null,
4524
- responseId: null
4553
+ responseId: null,
4554
+ ticketUrl: null
4525
4555
  };
4526
4556
  }
4527
4557
  function buildTestGorgiasWebhookFunctionDef(config) {