@llblab/pi-kit 0.5.2 → 0.7.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.
Files changed (141) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +5 -5
  3. package/node_modules/@llblab/pi-actors/AGENTS.md +1 -1
  4. package/node_modules/@llblab/pi-actors/CHANGELOG.md +6 -0
  5. package/node_modules/@llblab/pi-actors/README.md +1 -1
  6. package/node_modules/@llblab/pi-actors/dist/lib/async-runs.d.ts +3 -0
  7. package/node_modules/@llblab/pi-actors/dist/lib/async-runs.js +14 -1
  8. package/node_modules/@llblab/pi-actors/dist/lib/command-templates.js +45 -3
  9. package/node_modules/@llblab/pi-actors/dist/lib/extension-runtime.js +1 -1
  10. package/node_modules/@llblab/pi-actors/dist/lib/observability.d.ts +16 -3
  11. package/node_modules/@llblab/pi-actors/dist/lib/observability.js +92 -7
  12. package/node_modules/@llblab/pi-actors/dist/lib/pi.d.ts +0 -1
  13. package/node_modules/@llblab/pi-actors/dist/lib/pi.js +15 -24
  14. package/node_modules/@llblab/pi-actors/dist/lib/run-delivery-lineage.d.ts +17 -0
  15. package/node_modules/@llblab/pi-actors/dist/lib/run-delivery-lineage.js +44 -0
  16. package/node_modules/@llblab/pi-actors/dist/lib/run-delivery.d.ts +4 -0
  17. package/node_modules/@llblab/pi-actors/dist/lib/run-delivery.js +102 -4
  18. package/node_modules/@llblab/pi-actors/dist/lib/run-ui-runtime.js +58 -39
  19. package/node_modules/@llblab/pi-actors/dist/lib/runtime.js +14 -6
  20. package/node_modules/@llblab/pi-actors/dist/skills/actors/SKILL.md +1 -1
  21. package/node_modules/@llblab/pi-actors/docs/async-runs.md +1 -1
  22. package/node_modules/@llblab/pi-actors/docs/coordinator-delivery.md +18 -23
  23. package/node_modules/@llblab/pi-actors/lib/async-runs.ts +18 -1
  24. package/node_modules/@llblab/pi-actors/lib/command-templates.ts +41 -3
  25. package/node_modules/@llblab/pi-actors/lib/extension-runtime.ts +1 -1
  26. package/node_modules/@llblab/pi-actors/lib/observability.ts +119 -5
  27. package/node_modules/@llblab/pi-actors/lib/pi.ts +15 -28
  28. package/node_modules/@llblab/pi-actors/lib/run-delivery-lineage.ts +68 -0
  29. package/node_modules/@llblab/pi-actors/lib/run-delivery.ts +120 -4
  30. package/node_modules/@llblab/pi-actors/lib/run-ui-runtime.ts +69 -44
  31. package/node_modules/@llblab/pi-actors/lib/runtime.ts +17 -6
  32. package/node_modules/@llblab/pi-actors/package.json +1 -1
  33. package/node_modules/@llblab/pi-actors/skills/actors/SKILL.md +1 -1
  34. package/node_modules/@llblab/pi-grow-loop/AGENTS.md +2 -2
  35. package/node_modules/@llblab/pi-grow-loop/CHANGELOG.md +4 -0
  36. package/node_modules/@llblab/pi-grow-loop/README.md +6 -6
  37. package/node_modules/@llblab/pi-grow-loop/index.ts +6 -3
  38. package/node_modules/@llblab/pi-grow-loop/package.json +1 -1
  39. package/node_modules/@llblab/pi-state-flow/AGENTS.md +33 -12
  40. package/node_modules/@llblab/pi-state-flow/BACKLOG.md +125 -2
  41. package/node_modules/@llblab/pi-state-flow/CHANGELOG.md +87 -45
  42. package/node_modules/@llblab/pi-state-flow/README.md +203 -107
  43. package/node_modules/@llblab/pi-state-flow/docs/README.md +4 -0
  44. package/node_modules/@llblab/pi-state-flow/docs/architecture.md +188 -0
  45. package/node_modules/@llblab/pi-state-flow/docs/temporal-acceptance.md +42 -0
  46. package/node_modules/@llblab/pi-state-flow/index.ts +164 -2
  47. package/node_modules/@llblab/pi-state-flow/lib/acquisition.ts +138 -0
  48. package/node_modules/@llblab/pi-state-flow/lib/artifact.ts +273 -0
  49. package/node_modules/@llblab/pi-state-flow/lib/config.ts +48 -0
  50. package/node_modules/@llblab/pi-state-flow/lib/context.ts +18 -5
  51. package/node_modules/@llblab/pi-state-flow/lib/continuation.ts +268 -0
  52. package/node_modules/@llblab/pi-state-flow/lib/discovery.ts +117 -0
  53. package/node_modules/@llblab/pi-state-flow/lib/durable.ts +562 -0
  54. package/node_modules/@llblab/pi-state-flow/lib/episode.ts +24 -12
  55. package/node_modules/@llblab/pi-state-flow/lib/extension.ts +606 -70
  56. package/node_modules/@llblab/pi-state-flow/lib/git.ts +666 -0
  57. package/node_modules/@llblab/pi-state-flow/lib/history.ts +95 -0
  58. package/node_modules/@llblab/pi-state-flow/lib/json.ts +24 -0
  59. package/node_modules/@llblab/pi-state-flow/lib/maintenance.ts +141 -0
  60. package/node_modules/@llblab/pi-state-flow/lib/memory.ts +52 -0
  61. package/node_modules/@llblab/pi-state-flow/lib/migration.ts +88 -0
  62. package/node_modules/@llblab/pi-state-flow/lib/publication.ts +296 -0
  63. package/node_modules/@llblab/pi-state-flow/lib/recovery.ts +23 -7
  64. package/node_modules/@llblab/pi-state-flow/lib/rehydration.ts +79 -0
  65. package/node_modules/@llblab/pi-state-flow/lib/runtime.ts +264 -0
  66. package/node_modules/@llblab/pi-state-flow/lib/session.ts +6 -0
  67. package/node_modules/@llblab/pi-state-flow/lib/skills.ts +99 -7
  68. package/node_modules/@llblab/pi-state-flow/lib/snapshot.ts +305 -48
  69. package/node_modules/@llblab/pi-state-flow/lib/state.ts +74 -7
  70. package/node_modules/@llblab/pi-state-flow/lib/status.ts +125 -6
  71. package/node_modules/@llblab/pi-state-flow/lib/storage.ts +196 -0
  72. package/node_modules/@llblab/pi-state-flow/lib/temporal.ts +233 -0
  73. package/node_modules/@llblab/pi-state-flow/lib/terminal.ts +70 -24
  74. package/node_modules/@llblab/pi-state-flow/lib/transition.ts +254 -29
  75. package/node_modules/@llblab/pi-state-flow/package.json +8 -2
  76. package/node_modules/@llblab/pi-state-flow/skills/state-flow-memory/SKILL.md +128 -0
  77. package/node_modules/@llblab/pi-telegram/AGENTS.md +14 -9
  78. package/node_modules/@llblab/pi-telegram/BACKLOG.md +20 -4
  79. package/node_modules/@llblab/pi-telegram/CHANGELOG.md +22 -5
  80. package/node_modules/@llblab/pi-telegram/README.md +13 -9
  81. package/node_modules/@llblab/pi-telegram/docs/README.md +1 -0
  82. package/node_modules/@llblab/pi-telegram/docs/architecture.md +220 -18
  83. package/node_modules/@llblab/pi-telegram/docs/generative-apps.md +1 -1
  84. package/node_modules/@llblab/pi-telegram/docs/multi-instance-bus.md +70 -19
  85. package/node_modules/@llblab/pi-telegram/docs/outbound.md +13 -7
  86. package/node_modules/@llblab/pi-telegram/docs/public-api.md +13 -5
  87. package/node_modules/@llblab/pi-telegram/docs/ui-style.md +3 -1
  88. package/node_modules/@llblab/pi-telegram/index.ts +4 -1415
  89. package/node_modules/@llblab/pi-telegram/lib/activity.ts +19 -5
  90. package/node_modules/@llblab/pi-telegram/lib/agent-messages.ts +6 -3
  91. package/node_modules/@llblab/pi-telegram/lib/bindings.ts +37 -2
  92. package/node_modules/@llblab/pi-telegram/lib/bus-follower.ts +600 -135
  93. package/node_modules/@llblab/pi-telegram/lib/bus-leader.ts +962 -55
  94. package/node_modules/@llblab/pi-telegram/lib/bus.ts +350 -26
  95. package/node_modules/@llblab/pi-telegram/lib/channel-posts.ts +544 -0
  96. package/node_modules/@llblab/pi-telegram/lib/commands.ts +234 -11
  97. package/node_modules/@llblab/pi-telegram/lib/config.ts +178 -25
  98. package/node_modules/@llblab/pi-telegram/lib/delivery.ts +18 -18
  99. package/node_modules/@llblab/pi-telegram/lib/extension.ts +1792 -0
  100. package/node_modules/@llblab/pi-telegram/lib/generative-apps.ts +20 -2
  101. package/node_modules/@llblab/pi-telegram/lib/journal.ts +2184 -126
  102. package/node_modules/@llblab/pi-telegram/lib/lifecycle.ts +7 -1
  103. package/node_modules/@llblab/pi-telegram/lib/locks.ts +38 -1
  104. package/node_modules/@llblab/pi-telegram/lib/menu-settings.ts +154 -15
  105. package/node_modules/@llblab/pi-telegram/lib/outbound-attachments.ts +74 -40
  106. package/node_modules/@llblab/pi-telegram/lib/outbound-voice.ts +28 -42
  107. package/node_modules/@llblab/pi-telegram/lib/outbound.ts +18 -14
  108. package/node_modules/@llblab/pi-telegram/lib/paths.ts +29 -0
  109. package/node_modules/@llblab/pi-telegram/lib/polling.ts +85 -17
  110. package/node_modules/@llblab/pi-telegram/lib/preview.ts +115 -70
  111. package/node_modules/@llblab/pi-telegram/lib/prompts.ts +5 -2
  112. package/node_modules/@llblab/pi-telegram/lib/queue.ts +66 -22
  113. package/node_modules/@llblab/pi-telegram/lib/replies.ts +47 -39
  114. package/node_modules/@llblab/pi-telegram/lib/routing.ts +305 -112
  115. package/node_modules/@llblab/pi-telegram/lib/status.ts +10 -0
  116. package/node_modules/@llblab/pi-telegram/lib/sync.ts +308 -39
  117. package/node_modules/@llblab/pi-telegram/lib/telegram-api.ts +315 -7
  118. package/node_modules/@llblab/pi-telegram/lib/thread-cleanup-manager.ts +664 -0
  119. package/node_modules/@llblab/pi-telegram/lib/thread-display.ts +226 -0
  120. package/node_modules/@llblab/pi-telegram/lib/thread-naming.ts +118 -0
  121. package/node_modules/@llblab/pi-telegram/lib/threads.ts +1686 -129
  122. package/node_modules/@llblab/pi-telegram/lib/updates.ts +1319 -97
  123. package/node_modules/@llblab/pi-telegram/lib/workspace-admission.ts +1643 -0
  124. package/node_modules/@llblab/pi-telegram/lib/workspace-retirement.ts +968 -0
  125. package/node_modules/@llblab/pi-telegram/lib/workspace-slots.ts +84 -0
  126. package/node_modules/@llblab/pi-telegram/package.json +1 -1
  127. package/node_modules/@llblab/pi-telegram/screenshot.png +0 -0
  128. package/node_modules/@llblab/pi-telegram/scripts/measure-bus.mjs +83 -0
  129. package/node_modules/@llblab/pi-telegram/scripts/measure-workspace.mjs +101 -0
  130. package/node_modules/@llblab/{skills → pi-telegram/skills}/show-me/SKILL.md +28 -6
  131. package/node_modules/@llblab/pi-telegram/skills/show-me/references/telegram-surfaces.md +43 -0
  132. package/node_modules/@llblab/pi-telegram/skills/telegram-bridge/references/delivery-and-threads.md +1 -1
  133. package/node_modules/@llblab/skills/abcd-context/AGENTS.md +1 -0
  134. package/node_modules/@llblab/skills/abcd-context/CHANGELOG.md +6 -2
  135. package/node_modules/@llblab/skills/abcd-context/SKILL.md +1 -1
  136. package/node_modules/@llblab/skills/abcd-context/docs/validation-design.md +11 -5
  137. package/node_modules/@llblab/skills/abcd-context/scripts/_self-test.mjs +61 -0
  138. package/node_modules/@llblab/skills/abcd-context/scripts/validate-context.mjs +67 -0
  139. package/node_modules/@llblab/skills/package.json +2 -3
  140. package/node_modules/@llblab/skills/release-flow/SKILL.md +2 -4
  141. package/package.json +7 -6
@@ -9,6 +9,7 @@ import {
9
9
  getTelegramTargetThreadParams,
10
10
  type TelegramTarget,
11
11
  } from "./target.ts";
12
+ import { isTelegramApiCommitUnknownError } from "./telegram-api.ts";
12
13
  import type {
13
14
  TelegramInputRichMessage,
14
15
  TelegramReplyParameters,
@@ -67,6 +68,7 @@ export function createReplyDedupRuntime(): ReplyDedupRuntime {
67
68
  // --- Transport-level dedup ---
68
69
 
69
70
  const lastRepliedToMessageIdByTarget = new Map<string, number>();
71
+ let replyDedupGeneration = 0;
70
72
 
71
73
  function getReplyDedupTargetKey(
72
74
  chatId: number,
@@ -79,6 +81,7 @@ function getReplyDedupTargetKey(
79
81
  }
80
82
 
81
83
  export function resetTransportReplyDedup(): void {
84
+ replyDedupGeneration += 1;
82
85
  lastRepliedToMessageIdByTarget.clear();
83
86
  }
84
87
 
@@ -99,13 +102,29 @@ export function buildTelegramReplyParameters(
99
102
  };
100
103
  }
101
104
 
102
- export function buildTelegramMultipartReplyParameters(
105
+ // Answer publications are caller-serialized. A rejected send releases its anchor;
106
+ // an uncertain ACK retains it because Telegram may already have delivered it.
107
+ export async function withTelegramReplyParameters<T>(
103
108
  chatId: number,
104
109
  messageId: number | undefined,
105
- target?: TelegramTarget,
106
- ): string | undefined {
110
+ target: TelegramTarget | undefined,
111
+ send: (parameters: TelegramReplyParameters | undefined) => Promise<T>,
112
+ ): Promise<T> {
113
+ const key = getReplyDedupTargetKey(chatId, target);
114
+ const generation = replyDedupGeneration;
115
+ const previous = lastRepliedToMessageIdByTarget.get(key);
107
116
  const parameters = buildTelegramReplyParameters(chatId, messageId, target);
108
- return parameters ? JSON.stringify(parameters) : undefined;
117
+ try {
118
+ return await send(parameters);
119
+ } catch (error) {
120
+ if (parameters && !isTelegramApiCommitUnknownError(error)
121
+ && generation === replyDedupGeneration
122
+ && lastRepliedToMessageIdByTarget.get(key) === messageId) {
123
+ if (previous === undefined) lastRepliedToMessageIdByTarget.delete(key);
124
+ else lastRepliedToMessageIdByTarget.set(key, previous);
125
+ }
126
+ throw error;
127
+ }
109
128
  }
110
129
 
111
130
  function getAgentMessageField(message: unknown, field: string): unknown {
@@ -239,24 +258,18 @@ export async function sendTelegramRenderedChunks<TReplyMarkup>(
239
258
  assertTelegramInlineKeyboardCallbackData(options?.replyMarkup);
240
259
  let lastMessageId: number | undefined;
241
260
  for (const [index, chunk] of chunks.entries()) {
242
- const replyParameters =
243
- index === 0
244
- ? buildTelegramReplyParameters(
245
- chatId,
246
- options?.replyToMessageId,
247
- options?.target,
248
- )
249
- : undefined;
250
- const body = {
251
- chat_id: chatId,
252
- text: chunk.text,
253
- parse_mode: chunk.parseMode,
254
- reply_markup:
255
- index === chunks.length - 1 ? options?.replyMarkup : undefined,
256
- ...(replyParameters ? { reply_parameters: replyParameters } : {}),
257
- ...(options?.target ? getTelegramTargetThreadParams(options.target) : {}),
258
- };
259
- const sent = await deps.sendMessage(body);
261
+ const sent = await withTelegramReplyParameters(
262
+ chatId, index === 0 ? options?.replyToMessageId : undefined, options?.target,
263
+ (replyParameters) => deps.sendMessage({
264
+ chat_id: chatId,
265
+ text: chunk.text,
266
+ parse_mode: chunk.parseMode,
267
+ reply_markup:
268
+ index === chunks.length - 1 ? options?.replyMarkup : undefined,
269
+ ...(replyParameters ? { reply_parameters: replyParameters } : {}),
270
+ ...(options?.target ? getTelegramTargetThreadParams(options.target) : {}),
271
+ }),
272
+ );
260
273
  lastMessageId = sent.message_id;
261
274
  deps.recordOwnership?.({
262
275
  chatId,
@@ -655,22 +668,17 @@ export async function sendTelegramNativeMarkdownReply<TReplyMarkup = unknown>(
655
668
  let lastMessageId: number | undefined;
656
669
  const chunks = splitTelegramNativeMarkdown(markdown);
657
670
  for (const [index, chunk] of chunks.entries()) {
658
- const replyParameters =
659
- index === 0
660
- ? buildTelegramReplyParameters(
661
- chatId,
662
- replyToMessageId,
663
- options?.target,
664
- )
665
- : undefined;
666
- const sent = await deps.sendRichMessage({
667
- chat_id: chatId,
668
- rich_message: { markdown: chunk, skip_entity_detection: true },
669
- reply_markup:
670
- index === chunks.length - 1 ? options?.replyMarkup : undefined,
671
- ...(replyParameters ? { reply_parameters: replyParameters } : {}),
672
- ...(options?.target ? getTelegramTargetThreadParams(options.target) : {}),
673
- });
671
+ const sent = await withTelegramReplyParameters(
672
+ chatId, index === 0 ? replyToMessageId : undefined, options?.target,
673
+ (replyParameters) => deps.sendRichMessage({
674
+ chat_id: chatId,
675
+ rich_message: { markdown: chunk },
676
+ reply_markup:
677
+ index === chunks.length - 1 ? options?.replyMarkup : undefined,
678
+ ...(replyParameters ? { reply_parameters: replyParameters } : {}),
679
+ ...(options?.target ? getTelegramTargetThreadParams(options.target) : {}),
680
+ }),
681
+ );
674
682
  lastMessageId = sent.message_id;
675
683
  deps.recordOwnership?.({
676
684
  chatId,
@@ -886,7 +894,7 @@ export function createGuestMarkdownReplySender(deps: {
886
894
  return async (guestQueryId: string, markdown: string) => {
887
895
  const [richMarkdown = markdown] = splitTelegramNativeMarkdown(markdown);
888
896
  await deps.answerGuestQuery(guestQueryId, undefined, {
889
- richMessage: { markdown: richMarkdown, skip_entity_detection: true },
897
+ richMessage: { markdown: richMarkdown },
890
898
  });
891
899
  };
892
900
  }