stitchkit 0.124.2 → 0.124.4

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/entrypoints/agent-runtime/coding-tools.js +4 -4
  3. package/dist/entrypoints/agent-runtime/harness.js +5 -5
  4. package/dist/entrypoints/agent-runtime/sandbox.js +4 -4
  5. package/dist/entrypoints/agent-runtime/sqlite/bun.js +6 -6
  6. package/dist/entrypoints/agent-runtime/sqlite/node.js +6 -6
  7. package/dist/entrypoints/agent-runtime/testing.js +6 -6
  8. package/dist/entrypoints/agent-runtime.js +12 -12
  9. package/dist/entrypoints/cli.js +7 -7
  10. package/dist/entrypoints/server.js +1 -1
  11. package/dist/entrypoints/telegram/screens.d.ts +1 -1
  12. package/dist/entrypoints/telegram/screens.d.ts.map +1 -1
  13. package/dist/entrypoints/telegram/screens.js +196 -83
  14. package/dist/entrypoints/testing.js +2 -2
  15. package/dist/entrypoints/tools/agent.js +7 -7
  16. package/dist/entrypoints/tools/connections.js +6 -5
  17. package/dist/entrypoints/tools/invoker.js +5 -5
  18. package/dist/entrypoints/tools/mcp.js +7 -7
  19. package/dist/entrypoints/tools/toolkit.js +9 -9
  20. package/dist/entrypoints/tools.js +8 -8
  21. package/dist/{index-s512scra.js → index-1wz4y15d.js} +1 -1
  22. package/dist/{index-yebkpytc.js → index-2ds89z96.js} +4 -4
  23. package/dist/{index-jkjh7zw4.js → index-3n8k175r.js} +5 -5
  24. package/dist/index-5dmk54h9.js +256 -0
  25. package/dist/{index-8pwwd6rp.js → index-6fmg17bg.js} +1 -1
  26. package/dist/{index-hzk4ydpy.js → index-a3ekss9t.js} +19 -6
  27. package/dist/{index-7177dxjn.js → index-afc3mtah.js} +1 -1
  28. package/dist/{index-0d3sm0m9.js → index-cm07qz01.js} +1 -1
  29. package/dist/{index-qsm899qp.js → index-en2xqhty.js} +1 -1
  30. package/dist/{index-782qtc24.js → index-p9saezh1.js} +1 -1
  31. package/dist/{index-993vtv1w.js → index-q7s1yr9g.js} +3 -3
  32. package/dist/{index-6k47xdtt.js → index-r7v5xz0g.js} +1 -1
  33. package/dist/{index-tynna0n4.js → index-rk7m4j7r.js} +3 -94
  34. package/dist/{index-qc7tq0za.js → index-x68eyh6y.js} +2 -2
  35. package/dist/{index-enkc9pem.js → index-xaem29dc.js} +1 -1
  36. package/dist/{index-96yjwwgf.js → index-z2z67tnv.js} +3 -3
  37. package/dist/json-schema/dialect.d.ts +6 -0
  38. package/dist/json-schema/dialect.d.ts.map +1 -1
  39. package/dist/json-schema/json-schema.d.ts.map +1 -1
  40. package/dist/json-schema/string-format.d.ts +36 -0
  41. package/dist/json-schema/string-format.d.ts.map +1 -0
  42. package/dist/telegram/screens/dispatch.d.ts.map +1 -1
  43. package/dist/telegram/screens/display.d.ts +2 -1
  44. package/dist/telegram/screens/display.d.ts.map +1 -1
  45. package/dist/telegram/screens/execute.d.ts +12 -4
  46. package/dist/telegram/screens/execute.d.ts.map +1 -1
  47. package/dist/telegram/screens/render.d.ts +5 -1
  48. package/dist/telegram/screens/render.d.ts.map +1 -1
  49. package/dist/telegram/screens/runtime.d.ts +5 -1
  50. package/dist/telegram/screens/runtime.d.ts.map +1 -1
  51. package/dist/telegram/screens/state.d.ts +3 -0
  52. package/dist/telegram/screens/state.d.ts.map +1 -1
  53. package/dist/telegram/screens/test-chat-telegram.d.ts.map +1 -1
  54. package/dist/telegram/screens/view.d.ts +14 -5
  55. package/dist/telegram/screens/view.d.ts.map +1 -1
  56. package/dist/tools/cli/help.d.ts.map +1 -1
  57. package/dist/tools/connections/runtime.d.ts.map +1 -1
  58. package/llms/cli.txt +11 -0
  59. package/llms/telegram-screens.txt +59 -4
  60. package/llms-full.txt +70 -4
  61. package/llms.txt +2 -2
  62. package/package.json +1 -1
  63. package/dist/index-ct75c3at.js +0 -46
@@ -3,6 +3,7 @@ import {
3
3
  parseRoutePattern
4
4
  } from "../../index-m38ej7e5.js";
5
5
  import {
6
+ classifyTelegramSendFailure2,
6
7
  classifyTelegramEditRefusal
7
8
  } from "../../index-f56ct5gb.js";
8
9
  import {
@@ -539,6 +540,8 @@ var RECORD_LIMITS = {
539
540
  tokenBody: 4096,
540
541
  remarks: 100
541
542
  };
543
+ var MAX_TRACKED_MESSAGES = 2 * RECORD_LIMITS.messages + 1;
544
+ var MAX_TRACKED_TOKENS = 2 * RECORD_LIMITS.tokens;
542
545
  var MessageFingerprintSchema = z2.object({ content: z2.string(), media: z2.string(), markup: z2.string() }).strict();
543
546
  var ShownMessageSchema = z2.object({
544
547
  key: z2.string().min(1).max(RECORD_LIMITS.key),
@@ -549,8 +552,8 @@ var ShownMessageSchema = z2.object({
549
552
  var ScreenViewStateSchema = z2.object({
550
553
  screen: z2.string().min(1).max(64),
551
554
  params: z2.record(z2.string(), z2.union([z2.string(), z2.number()])),
552
- messages: z2.array(ShownMessageSchema).min(1).max(RECORD_LIMITS.messages),
553
- tokens: z2.record(z2.string(), z2.string().max(RECORD_LIMITS.tokenBody)).refine((tokens) => Object.keys(tokens).length <= 2 * RECORD_LIMITS.tokens)
555
+ messages: z2.array(ShownMessageSchema).min(1).max(MAX_TRACKED_MESSAGES),
556
+ tokens: z2.record(z2.string(), z2.string().max(RECORD_LIMITS.tokenBody)).refine((tokens) => Object.keys(tokens).length <= MAX_TRACKED_TOKENS)
554
557
  }).strict();
555
558
  var ExpiringNoticeSchema = z2.object({ id: z2.number().int().positive(), at: z2.number().int().nonnegative() }).strict();
556
559
  var ScreenChatStateSchema = z2.object({
@@ -599,12 +602,16 @@ function createChatQueue(maxPending) {
599
602
  // src/telegram/screens/execute.ts
600
603
  class PlanInterrupted extends Error {
601
604
  shown;
605
+ redrawn;
606
+ sentIds;
602
607
  cause;
603
- constructor(shown, cause) {
608
+ constructor(shown, redrawn, sentIds, cause) {
604
609
  super("[stitchkit] telegram screens: a view was left half-drawn by a Bot API failure.", {
605
610
  cause
606
611
  });
607
612
  this.shown = shown;
613
+ this.redrawn = redrawn;
614
+ this.sentIds = sentIds;
608
615
  this.cause = cause;
609
616
  this.name = "PlanInterrupted";
610
617
  }
@@ -619,6 +626,9 @@ var HTML = "HTML";
619
626
  function captionOption(message) {
620
627
  return message.html === undefined ? {} : { caption: message.html, parse_mode: HTML };
621
628
  }
629
+ function richPayload(message) {
630
+ return message.richBuild ? message.richBuild() : message.rich ?? {};
631
+ }
622
632
  async function sendOutgoing(api, chatId, message) {
623
633
  const extra = { ...captionOption(message), ...markupOption(message.markup) };
624
634
  const source = message.media ?? "";
@@ -630,7 +640,7 @@ async function sendOutgoing(api, chatId, message) {
630
640
  ...markupOption(message.markup)
631
641
  })).message_id;
632
642
  case "rich":
633
- return (await api.sendRichMessage(chatId, message.rich ?? {}, markupOption(message.markup))).message_id;
643
+ return (await api.sendRichMessage(chatId, await richPayload(message), markupOption(message.markup))).message_id;
634
644
  case "photo":
635
645
  return (await api.sendPhoto(chatId, source, extra)).message_id;
636
646
  case "video":
@@ -643,14 +653,14 @@ async function sendOutgoing(api, chatId, message) {
643
653
  return (await api.sendAudio(chatId, source, extra)).message_id;
644
654
  }
645
655
  }
646
- async function editOutgoing(api, chatId, step) {
656
+ async function editOutgoing(api, chatId, step, rich) {
647
657
  const { message, parts, id } = step;
648
658
  const markup = markupOption(message.markup);
649
659
  if (message.kind === "text" || message.kind === "rich") {
650
660
  if (!parts.content) {
651
661
  await api.editMessageReplyMarkup(chatId, id, markup);
652
- } else if (message.kind === "rich") {
653
- await api.editMessageText(chatId, id, message.rich ?? {}, markup);
662
+ } else if (rich) {
663
+ await api.editMessageText(chatId, id, rich, markup);
654
664
  } else {
655
665
  await api.editMessageText(chatId, id, message.html ?? "", {
656
666
  parse_mode: HTML,
@@ -687,62 +697,78 @@ async function deleteMessages(api, chatId, ids) {
687
697
  }
688
698
  return undeleted;
689
699
  }
690
- function interruptedView(remaining, deletions, done) {
691
- const untouched = [];
692
- for (const step of remaining) {
693
- if (step.op !== "send")
694
- untouched.push({ ...step.from, fingerprint: null });
695
- }
696
- const present = new Set([...done, ...untouched].map((message) => message.id));
697
- const leftovers = deletions.filter((id) => !present.has(id)).map((id) => ({ key: `left-${id}`, id, kind: "text", fingerprint: null }));
698
- return [...done, ...untouched, ...leftovers].sort((left, right) => left.id - right.id);
699
- }
700
- async function executePlan(api, chatId, plan) {
700
+ async function executePlan(api, chatId, plan, previous) {
701
701
  const shown = [];
702
+ const visible = new Map(previous.map((message) => [message.id, message]));
702
703
  const deletions = [...plan.deletions];
704
+ const redrawn = new Map;
705
+ const sentIds = [];
706
+ let foundMissing = false;
703
707
  let resending = false;
704
- let sent = 0;
705
708
  let edited = 0;
706
709
  let kept = 0;
707
- for (const [index, step] of plan.steps.entries()) {
708
- try {
709
- if (step.op !== "send" && resending)
710
- deletions.push(step.id);
711
- if (step.op === "send" || resending) {
712
- shown.push(shownFrom(await sendOutgoing(api, chatId, step.message), step.message));
713
- sent += 1;
714
- continue;
715
- }
716
- if (step.op === "keep") {
717
- shown.push(shownFrom(step.id, step.message));
710
+ const record = (id, message) => {
711
+ const entry = shownFrom(id, message);
712
+ shown.push(entry);
713
+ visible.set(id, entry);
714
+ redrawn.set(id, message.tokenIds);
715
+ };
716
+ try {
717
+ for (const step of plan.steps) {
718
+ if (step.op === "keep" && !resending) {
719
+ shown.push(step.from);
718
720
  kept += 1;
719
721
  continue;
720
722
  }
721
- try {
722
- await editOutgoing(api, chatId, step);
723
- edited += 1;
724
- } catch (error) {
725
- const refusal = classifyTelegramEditRefusal(error);
726
- if (refusal === "message-gone" || refusal === "message-locked") {
727
- resending = true;
728
- deletions.push(step.id);
729
- shown.push(shownFrom(await sendOutgoing(api, chatId, step.message), step.message));
730
- sent += 1;
723
+ if (step.op === "edit" && !resending) {
724
+ const rich = step.message.kind === "rich" && step.parts.content ? await richPayload(step.message) : undefined;
725
+ try {
726
+ await editOutgoing(api, chatId, step, rich);
727
+ edited += 1;
728
+ } catch (error) {
729
+ const refusal = classifyTelegramEditRefusal(error);
730
+ if (refusal === "not-modified") {
731
+ kept += 1;
732
+ } else if (refusal === "message-gone" || refusal === "message-locked") {
733
+ if (refusal === "message-gone") {
734
+ visible.delete(step.id);
735
+ foundMissing = true;
736
+ }
737
+ resending = true;
738
+ } else {
739
+ const failure = classifyTelegramSendFailure2(error);
740
+ if (failure.reason !== "not-dispatched" && (failure.status ?? 500) >= 500) {
741
+ visible.set(step.id, { ...step.from, fingerprint: null });
742
+ redrawn.set(step.id, step.message.tokenIds);
743
+ }
744
+ throw error;
745
+ }
746
+ }
747
+ if (!resending) {
748
+ record(step.id, step.message);
731
749
  continue;
732
750
  }
733
- if (refusal !== "not-modified")
734
- throw error;
735
- kept += 1;
736
751
  }
737
- shown.push(shownFrom(step.id, step.message));
738
- } catch (error) {
739
- throw new PlanInterrupted(interruptedView(plan.steps.slice(index), deletions, shown), error);
752
+ if (step.op !== "send")
753
+ deletions.push(step.id);
754
+ const id = await sendOutgoing(api, chatId, step.message);
755
+ sentIds.push(id);
756
+ record(id, step.message);
740
757
  }
758
+ } catch (error) {
759
+ const snapshot = redrawn.size > 0 || foundMissing ? [...visible.values()].sort((left, right) => left.id - right.id) : undefined;
760
+ throw new PlanInterrupted(snapshot, redrawn, sentIds, error);
741
761
  }
742
762
  const undeleted = await deleteMessages(api, chatId, deletions);
743
763
  return {
744
764
  shown,
745
- report: { sent, edited, kept, deleted: deletions.length - undeleted.length, undeleted }
765
+ report: {
766
+ sent: sentIds.length,
767
+ edited,
768
+ kept,
769
+ deleted: deletions.length - undeleted.length,
770
+ undeleted
771
+ }
746
772
  };
747
773
  }
748
774
 
@@ -827,7 +853,7 @@ class TokenBook {
827
853
  data(body) {
828
854
  const direct = this.prefix + body;
829
855
  if (utf8Length(direct) <= CALLBACK_DATA_LIMIT)
830
- return direct;
856
+ return { data: direct };
831
857
  if (body.length > RECORD_LIMITS.tokenBody) {
832
858
  throw new Error(`[stitchkit] telegram screens: a button of "${this.where}" carries ${body.length} characters; a kept address holds ${RECORD_LIMITS.tokenBody}.`);
833
859
  }
@@ -844,7 +870,7 @@ class TokenBook {
844
870
  if (utf8Length(spilled) > CALLBACK_DATA_LIMIT) {
845
871
  throw new Error(`[stitchkit] telegram screens: callback prefix "${this.prefix}" leaves no room for a button.`);
846
872
  }
847
- return spilled;
873
+ return { data: spilled, token };
848
874
  }
849
875
  }
850
876
  function buttonData(button, context, book) {
@@ -870,28 +896,44 @@ function buttonData(button, context, book) {
870
896
  }));
871
897
  }
872
898
  function renderKeyboard(keyboard, context, book) {
873
- if (!keyboard)
874
- return;
899
+ const tokenIds = new Set;
875
900
  const rows = [];
876
- for (const row of keyboard) {
901
+ for (const row of keyboard ?? []) {
877
902
  if (!row)
878
903
  continue;
879
904
  const buttons = [];
880
905
  for (const button of row) {
881
906
  if (!button)
882
907
  continue;
883
- buttons.push(button instanceof ScreenButton ? labelled(button.label, buttonData(button, context, book)) : button);
908
+ if (!(button instanceof ScreenButton)) {
909
+ buttons.push(button);
910
+ continue;
911
+ }
912
+ const address = buttonData(button, context, book);
913
+ if (address.token !== undefined)
914
+ tokenIds.add(address.token);
915
+ buttons.push(labelled(button.label, address.data));
884
916
  }
885
917
  if (buttons.length > 0)
886
918
  rows.push(buttons);
887
919
  }
888
- return rows.length > 0 ? { inline_keyboard: rows } : undefined;
920
+ return {
921
+ markup: rows.length > 0 ? { inline_keyboard: rows } : undefined,
922
+ tokenIds: [...tokenIds]
923
+ };
889
924
  }
890
925
  function renderMessage(message, key, context, book) {
891
- const markup = renderKeyboard(message.keyboard, context, book);
926
+ const { markup, tokenIds } = renderKeyboard(message.keyboard, context, book);
892
927
  const media = mediaOf(message);
893
928
  const rich = message.rich;
894
- const kind = media ? media.kind : rich ? "rich" : "text";
929
+ const upload = message.richUpload;
930
+ if (upload !== undefined && (typeof upload?.contentKey !== "string" || !upload.contentKey || typeof upload.build !== "function")) {
931
+ throw new Error("[stitchkit] telegram screens: richUpload needs a non-empty contentKey and a build function.");
932
+ }
933
+ if (upload && rich) {
934
+ throw new Error("[stitchkit] telegram screens: a message has rich or richUpload, not both — the inline content would never be sent.");
935
+ }
936
+ const kind = media ? media.kind : rich || upload ? "rich" : "text";
895
937
  const bodyText = media ? message.caption : message.text;
896
938
  const html = bodyText === undefined ? undefined : htmlOf(bodyText);
897
939
  const linkPreview = message.linkPreview;
@@ -900,11 +942,13 @@ function renderMessage(message, key, context, book) {
900
942
  kind,
901
943
  html,
902
944
  rich,
945
+ richBuild: upload?.build,
903
946
  linkPreview,
904
947
  media: media?.source,
905
948
  markup,
949
+ tokenIds,
906
950
  fingerprint: {
907
- content: argumentsDigest({ html, rich, linkPreview }),
951
+ content: upload ? argumentsDigest({ richContentKey: upload.contentKey }) : argumentsDigest({ html, rich, linkPreview }),
908
952
  media: argumentsDigest({ kind, source: media?.source }),
909
953
  markup: argumentsDigest({ markup })
910
954
  }
@@ -1000,8 +1044,11 @@ async function settleNavigation(runtime, ctx, origin, outcome) {
1000
1044
  }
1001
1045
  throw new Error(`[stitchkit] telegram screens: loads redirected more than ${MAX_REDIRECTS} times in a row.`);
1002
1046
  }
1003
- async function sendText(api, chatId, content) {
1004
- return (await api.sendMessage(chatId, htmlOf(content), { parse_mode: "HTML" })).message_id;
1047
+ async function sendText(api, chatId, content, ephemeral) {
1048
+ return (await api.sendMessage(chatId, htmlOf(content), {
1049
+ parse_mode: "HTML",
1050
+ ...ephemeral && { ephemeral_message_parameters: ephemeral }
1051
+ })).message_id;
1005
1052
  }
1006
1053
  function planFor(base, state, next, moves) {
1007
1054
  const current = state.view?.messages ?? [];
@@ -1046,17 +1093,27 @@ var nothingDone = {
1046
1093
  deleted: 0,
1047
1094
  undeleted: []
1048
1095
  };
1049
- function interruptedRecord(previous, base, drawn, shown) {
1050
- const untouched = base.mode === "new" ? previous?.messages ?? [] : [];
1051
- const messages = [...untouched, ...shown];
1052
- if (messages.length === 0)
1096
+ function interruptedRecord(previous, drawn, error, shown = error.shown) {
1097
+ if (!shown)
1053
1098
  return previous;
1099
+ if (shown.length === 0)
1100
+ return null;
1054
1101
  const identity = previous ?? drawn;
1102
+ const oldMarkup = new Map(previous?.messages.map((m) => [m.id, m.fingerprint?.markup]));
1103
+ const keepsOldButtons = shown.some((m) => oldMarkup.has(m.id) && (!m.fingerprint || m.fingerprint.markup === oldMarkup.get(m.id)));
1104
+ const tokens = keepsOldButtons ? { ...previous?.tokens } : {};
1105
+ for (const message of shown) {
1106
+ for (const id of error.redrawn.get(message.id) ?? []) {
1107
+ const body = drawn.tokens[id];
1108
+ if (body !== undefined)
1109
+ tokens[id] = body;
1110
+ }
1111
+ }
1055
1112
  return {
1056
1113
  screen: identity.screen,
1057
1114
  params: identity.params,
1058
- messages,
1059
- tokens: { ...previous?.tokens, ...drawn.tokens }
1115
+ messages: [...shown],
1116
+ tokens
1060
1117
  };
1061
1118
  }
1062
1119
  async function evictOldestRemark(runtime, api, chat, chatId, remarks) {
@@ -1084,7 +1141,11 @@ async function displayScreen(runtime, request) {
1084
1141
  let failure;
1085
1142
  try {
1086
1143
  if (target && rendered) {
1087
- const plan = planFor(base, state, rendered.messages, persistent.length > 0);
1144
+ const current = state.view?.messages ?? [];
1145
+ const canTrackForeign = current.length < MAX_TRACKED_MESSAGES;
1146
+ const previous = base.mode === "foreign" && canTrackForeign ? [...current, base.message] : current;
1147
+ const fresh = persistent.length > 0 || base.mode === "foreign" && !canTrackForeign || Object.keys({ ...state.view?.tokens, ...rendered.tokens }).length > MAX_TRACKED_TOKENS;
1148
+ const plan = planFor(base, state, rendered.messages, fresh);
1088
1149
  const leaving = target.entry.definition.id !== state.view?.screen;
1089
1150
  if (remarks.length > 0 && (leaving || plan.steps.some((step) => step.op === "send"))) {
1090
1151
  const kept = await deleteMessages(api, chatId, remarks.map((remark) => remark.id));
@@ -1101,13 +1162,22 @@ async function displayScreen(runtime, request) {
1101
1162
  for (const notice of persistent)
1102
1163
  await sendText(api, chatId, notice.content);
1103
1164
  try {
1104
- const executed = await executePlan(api, chatId, plan);
1165
+ const executed = await executePlan(api, chatId, plan, previous);
1105
1166
  report = executed.report;
1106
1167
  view = drawn(executed.shown);
1107
1168
  } catch (error) {
1108
1169
  if (!(error instanceof PlanInterrupted))
1109
1170
  throw error;
1110
- view = interruptedRecord(state.view, base, drawn([]), error.shown);
1171
+ const targetView = drawn([]);
1172
+ view = interruptedRecord(state.view, targetView, error);
1173
+ if (view && (view.messages.length > MAX_TRACKED_MESSAGES || Object.keys(view.tokens).length > MAX_TRACKED_TOKENS)) {
1174
+ const sent = new Set(error.sentIds);
1175
+ const retained = error.shown?.filter((m) => !sent.has(m.id));
1176
+ const kept = await deleteMessages(api, chatId, error.sentIds);
1177
+ if (kept.length)
1178
+ runtime.emit({ type: "message-kept", chat, messageIds: kept });
1179
+ view = interruptedRecord(state.view, targetView, error, retained);
1180
+ }
1111
1181
  throw error.cause;
1112
1182
  }
1113
1183
  if (report.undeleted.length > 0) {
@@ -1292,7 +1362,8 @@ function assertOpenStands(navigated, settled) {
1292
1362
  throw new Error("[stitchkit] telegram screens: a handler that calls open() shows that screen; return nothing or a toast.");
1293
1363
  }
1294
1364
  }
1295
- async function guarded(runtime, ctx, trigger, chat, chatId, operation, answer) {
1365
+ var LATE_TOAST_MS = 30000;
1366
+ async function guarded(runtime, ctx, trigger, chat, chatId, operation, answerError) {
1296
1367
  try {
1297
1368
  await operation();
1298
1369
  } catch (error) {
@@ -1303,8 +1374,10 @@ async function guarded(runtime, ctx, trigger, chat, chatId, operation, answer) {
1303
1374
  if (!ScreenOutcome.is(result))
1304
1375
  return;
1305
1376
  const state = await runtime.readState(chat);
1306
- const settled = await settleNavigation(runtime, ctx, originOf(runtime, state), result);
1307
- await answer?.(settled);
1377
+ const outcome = runtime.displayFailed(error) && result.navigation.type === "stay" ? { target: undefined, toast: result.toastSpec, notices: result.notices } : await settleNavigation(runtime, ctx, originOf(runtime, state), result);
1378
+ const settled = answerError ? await answerError(outcome) : outcome;
1379
+ if (!settled.target && settled.notices.length === 0)
1380
+ return;
1308
1381
  await runtime.display({
1309
1382
  ctx,
1310
1383
  chat,
@@ -1332,14 +1405,16 @@ async function dispatchPress(runtime, ctx) {
1332
1405
  let answered = false;
1333
1406
  const answer = async (settled) => {
1334
1407
  if (answered)
1335
- return;
1408
+ return false;
1336
1409
  answered = true;
1337
1410
  const toast = settled?.toast;
1338
1411
  try {
1339
1412
  await ctx.api.answerCallbackQuery(press.id, toast ? { text: toast.text, show_alert: toast.alert } : undefined);
1413
+ return true;
1340
1414
  } catch (error) {
1341
1415
  if (classifyTelegramEditRefusal(error) !== "query-expired")
1342
1416
  throw error;
1417
+ return false;
1343
1418
  }
1344
1419
  };
1345
1420
  try {
@@ -1371,9 +1446,21 @@ async function dispatchPress(runtime, ctx) {
1371
1446
  trigger: "press",
1372
1447
  started
1373
1448
  });
1374
- }, answer));
1449
+ }, async (settled) => {
1450
+ if (await answer(settled) || !settled.toast)
1451
+ return settled;
1452
+ if (message.chat.type === "private") {
1453
+ const remark = { content: settled.toast.text, expiresInMs: LATE_TOAST_MS };
1454
+ return { ...settled, notices: [...settled.notices, remark] };
1455
+ }
1456
+ await sendText(ctx.api, chatId, settled.toast.text, {
1457
+ receiver_user_id: query.from.id,
1458
+ callback_query_id: press.id
1459
+ });
1460
+ return settled;
1461
+ }));
1375
1462
  } finally {
1376
- await answer(undefined);
1463
+ await answer();
1377
1464
  }
1378
1465
  }
1379
1466
  async function dispatchInput(runtime, ctx, message) {
@@ -1448,7 +1535,7 @@ async function openScreen(runtime, ctx, origin, previous) {
1448
1535
  if (chat === undefined || chatId === undefined) {
1449
1536
  throw new Error("[stitchkit] telegram screens: open() needs an update from a chat.");
1450
1537
  }
1451
- await runtime.exclusive(ctx, chat, () => guarded(runtime, ctx, "open", chat, chatId, async () => {
1538
+ const open = async () => {
1452
1539
  const started = runtime.clock();
1453
1540
  const state = await sweepDue(runtime, ctx.api, chat, chatId);
1454
1541
  const outcome = new ScreenOutcome({
@@ -1469,7 +1556,8 @@ async function openScreen(runtime, ctx, origin, previous) {
1469
1556
  started
1470
1557
  });
1471
1558
  runtime.markOpened(ctx);
1472
- }));
1559
+ };
1560
+ await runtime.exclusive(ctx, chat, runtime.inside(ctx) ? open : () => guarded(runtime, ctx, "open", chat, chatId, open));
1473
1561
  }
1474
1562
 
1475
1563
  // src/telegram/screens/registry.ts
@@ -1552,7 +1640,8 @@ function createScreensRuntime(config) {
1552
1640
  throw new Error('[stitchkit] telegram screens: callbackPrefix must be non-empty and free of "#" and "!".');
1553
1641
  }
1554
1642
  const queue = createChatQueue(MAX_PENDING_PER_CHAT);
1555
- const inside = new WeakSet;
1643
+ const running = new WeakSet;
1644
+ const failedDisplays = new WeakSet;
1556
1645
  const timers = new Set;
1557
1646
  const opened = new WeakSet;
1558
1647
  const runtime = {
@@ -1564,24 +1653,34 @@ function createScreensRuntime(config) {
1564
1653
  chatKey: (ctx) => config.chatKey ? config.chatKey(ctx) : ctx.chat ? `screens:${ctx.chat.id}` : undefined,
1565
1654
  answers: (ctx) => (config.chats ?? "private") === "all" || ctx.chat?.type === "private",
1566
1655
  exclusive: async (ctx, chat, task) => {
1567
- if (inside.has(ctx))
1656
+ if (running.has(ctx))
1568
1657
  return task();
1569
1658
  return queue(chat, async () => {
1570
- inside.add(ctx);
1659
+ running.add(ctx);
1571
1660
  try {
1572
1661
  return await task();
1573
1662
  } finally {
1574
- inside.delete(ctx);
1663
+ running.delete(ctx);
1575
1664
  }
1576
1665
  });
1577
1666
  },
1667
+ inside: (ctx) => running.has(ctx),
1578
1668
  readState: async (chat) => {
1579
1669
  const read = await readChatState(config.storage, chat);
1580
1670
  if (read.discarded)
1581
1671
  runtime.emit({ type: "state-discarded", chat });
1582
1672
  return read.state;
1583
1673
  },
1584
- display: (request) => displayScreen(runtime, request),
1674
+ display: async (request) => {
1675
+ try {
1676
+ await displayScreen(runtime, request);
1677
+ } catch (error) {
1678
+ if (typeof error === "object" && error !== null)
1679
+ failedDisplays.add(error);
1680
+ throw error;
1681
+ }
1682
+ },
1683
+ displayFailed: (error) => typeof error === "object" && error !== null && failedDisplays.has(error),
1585
1684
  markOpened: (ctx) => {
1586
1685
  opened.add(ctx);
1587
1686
  },
@@ -1754,6 +1853,20 @@ function createFakeTelegram({
1754
1853
  const stored = [];
1755
1854
  const answers = [];
1756
1855
  let lastMessageId = 0;
1856
+ let lastFileId = 0;
1857
+ const fileIds = new WeakMap;
1858
+ const receivedRich = (value) => {
1859
+ if (Array.isArray(value))
1860
+ return value.map(receivedRich);
1861
+ if (!isRecord(value))
1862
+ return value;
1863
+ if (typeof value.toRaw === "function") {
1864
+ const id = fileIds.get(value) ?? `test-file-${++lastFileId}`;
1865
+ fileIds.set(value, id);
1866
+ return id;
1867
+ }
1868
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, receivedRich(v)]));
1869
+ };
1757
1870
  const refuse = (description, errorCode = 400) => ({
1758
1871
  ok: false,
1759
1872
  error_code: errorCode,
@@ -1791,7 +1904,7 @@ function createFakeTelegram({
1791
1904
  kind,
1792
1905
  text: stringAt(payload, kind === "text" ? "text" : "caption") ?? "",
1793
1906
  media: kind === "text" || kind === "rich" ? undefined : stringAt(payload, kind),
1794
- rich: payload.rich_message,
1907
+ rich: receivedRich(payload.rich_message),
1795
1908
  markup: markupAt(payload),
1796
1909
  deleted: false
1797
1910
  };
@@ -1807,7 +1920,7 @@ function createFakeTelegram({
1807
1920
  if (refusal)
1808
1921
  return refuse(refusal);
1809
1922
  const markup = markupAt(payload);
1810
- const content = editContent(message, method, payload);
1923
+ const content = editContent(message, method, "rich_message" in payload ? { ...payload, rich_message: receivedRich(payload.rich_message) } : payload);
1811
1924
  if ("refused" in content)
1812
1925
  return refuse(content.refused);
1813
1926
  if (!content.changed && sameMarkup(message.markup, markup))
@@ -25,13 +25,13 @@ import {
25
25
  } from "../index-814xpr6d.js";
26
26
  import {
27
27
  toJsonSchema
28
- } from "../index-ct75c3at.js";
28
+ } from "../index-5dmk54h9.js";
29
29
  import {
30
30
  staticInputRounds,
31
31
  projectToolSurface,
32
32
  mcpProjectionCandidate,
33
33
  prepareProjectedMcpTools
34
- } from "../index-tynna0n4.js";
34
+ } from "../index-rk7m4j7r.js";
35
35
  import"../index-ndjzbt2v.js";
36
36
  import {
37
37
  createClient2,
@@ -1,16 +1,16 @@
1
1
  import"../../index-ndjzbt2v.js";
2
2
  import"../../index-dsa676h8.js";
3
3
  import"../../index-t8jz0e20.js";
4
- import"../../index-ct75c3at.js";
5
- import"../../index-tynna0n4.js";
6
- import"../../index-s512scra.js";
4
+ import"../../index-5dmk54h9.js";
5
+ import"../../index-rk7m4j7r.js";
6
+ import"../../index-1wz4y15d.js";
7
7
  import"../../index-yt1gg11x.js";
8
- import"../../index-qc7tq0za.js";
8
+ import"../../index-x68eyh6y.js";
9
9
  import {
10
10
  mountAgent2
11
- } from "../../index-yebkpytc.js";
12
- import"../../index-993vtv1w.js";
13
- import"../../index-6k47xdtt.js";
11
+ } from "../../index-2ds89z96.js";
12
+ import"../../index-q7s1yr9g.js";
13
+ import"../../index-r7v5xz0g.js";
14
14
  export {
15
15
  mountAgent2 as mountAgent
16
16
  };
@@ -2,11 +2,12 @@ import {
2
2
  readDiscoveredToolAnnotations
3
3
  } from "../../index-332d0m9g.js";
4
4
  import"../../index-yt1gg11x.js";
5
- import"../../index-ct75c3at.js";
6
5
  import {
7
6
  declaresDraft07,
8
- withDraft2020Keywords
9
- } from "../../index-tynna0n4.js";
7
+ withDraft2020Keywords,
8
+ withServedFormats
9
+ } from "../../index-5dmk54h9.js";
10
+ import"../../index-rk7m4j7r.js";
10
11
  import"../../index-ndjzbt2v.js";
11
12
  import {
12
13
  markJsonSchemaObjectGuards,
@@ -19,7 +20,7 @@ import {
19
20
  } from "../../index-mm89hd8v.js";
20
21
  import {
21
22
  defineRuntimeTool2
22
- } from "../../index-6k47xdtt.js";
23
+ } from "../../index-r7v5xz0g.js";
23
24
  import {
24
25
  TOOL_ERROR_PROJECTION
25
26
  } from "../../index-aw6kx2e6.js";
@@ -934,7 +935,7 @@ async function withConnectionToken(params, body) {
934
935
  function zodObjectFromJsonSchema(schema) {
935
936
  if (!schema || Object.keys(schema).length === 0)
936
937
  return z2.looseObject({});
937
- const parsed = z2.fromJSONSchema(reconcileDialect(schema));
938
+ const parsed = z2.fromJSONSchema(withServedFormats(reconcileDialect(schema)));
938
939
  if (parsed instanceof z2.ZodObject) {
939
940
  markJsonSchemaObjectGuards(parsed);
940
941
  markRemoteSchemaVerdict(parsed);
@@ -1,14 +1,14 @@
1
1
  import"../../index-ndjzbt2v.js";
2
2
  import"../../index-dsa676h8.js";
3
3
  import"../../index-t8jz0e20.js";
4
- import"../../index-ct75c3at.js";
5
- import"../../index-tynna0n4.js";
6
- import"../../index-s512scra.js";
4
+ import"../../index-5dmk54h9.js";
5
+ import"../../index-rk7m4j7r.js";
6
+ import"../../index-1wz4y15d.js";
7
7
  import"../../index-yt1gg11x.js";
8
- import"../../index-qc7tq0za.js";
8
+ import"../../index-x68eyh6y.js";
9
9
  import {
10
10
  createToolInvoker2
11
- } from "../../index-96yjwwgf.js";
11
+ } from "../../index-z2z67tnv.js";
12
12
  export {
13
13
  createToolInvoker2 as createToolInvoker
14
14
  };
@@ -1,11 +1,11 @@
1
1
  import"../../index-ndjzbt2v.js";
2
2
  import"../../index-dsa676h8.js";
3
3
  import"../../index-t8jz0e20.js";
4
- import"../../index-ct75c3at.js";
5
- import"../../index-tynna0n4.js";
6
- import"../../index-s512scra.js";
4
+ import"../../index-5dmk54h9.js";
5
+ import"../../index-rk7m4j7r.js";
6
+ import"../../index-1wz4y15d.js";
7
7
  import"../../index-yt1gg11x.js";
8
- import"../../index-qc7tq0za.js";
8
+ import"../../index-x68eyh6y.js";
9
9
  import"../../index-332d0m9g.js";
10
10
  import {
11
11
  RESOURCE_MIME_TYPE2,
@@ -18,7 +18,7 @@ import {
18
18
  createMcpHandler2,
19
19
  createMcpHttpRoute2,
20
20
  createStdioMcpServer2
21
- } from "../../index-jkjh7zw4.js";
21
+ } from "../../index-3n8k175r.js";
22
22
  import {
23
23
  DEFAULT_PROCESS_SIGNALS,
24
24
  defaultSignalSource,
@@ -30,8 +30,8 @@ import {
30
30
  mcpCatalogMeta2,
31
31
  readMcpCatalogStamp2
32
32
  } from "../../index-mm89hd8v.js";
33
- import"../../index-993vtv1w.js";
34
- import"../../index-6k47xdtt.js";
33
+ import"../../index-q7s1yr9g.js";
34
+ import"../../index-r7v5xz0g.js";
35
35
  // src/tools/mcp/stdio-signals.ts
36
36
  var bound = new WeakSet;
37
37
  function bindStdioProcessSignals(handle, options = {}) {