@llblab/pi-kit 0.22.0 → 0.22.2

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 (43) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/node_modules/@llblab/pi-telegram/AGENTS.md +4 -2
  4. package/node_modules/@llblab/pi-telegram/BACKLOG.md +3 -8
  5. package/node_modules/@llblab/pi-telegram/CHANGELOG.md +11 -0
  6. package/node_modules/@llblab/pi-telegram/README.md +3 -1
  7. package/node_modules/@llblab/pi-telegram/dist/lib/bindings.js +1 -4
  8. package/node_modules/@llblab/pi-telegram/dist/lib/bus-api.d.ts +0 -1
  9. package/node_modules/@llblab/pi-telegram/dist/lib/bus-api.js +0 -3
  10. package/node_modules/@llblab/pi-telegram/dist/lib/bus-leader.js +22 -3
  11. package/node_modules/@llblab/pi-telegram/dist/lib/extension.js +1 -2
  12. package/node_modules/@llblab/pi-telegram/dist/lib/menu-model.d.ts +3 -3
  13. package/node_modules/@llblab/pi-telegram/dist/lib/menu-model.js +17 -3
  14. package/node_modules/@llblab/pi-telegram/dist/lib/menu.d.ts +5 -7
  15. package/node_modules/@llblab/pi-telegram/dist/lib/menu.js +4 -5
  16. package/node_modules/@llblab/pi-telegram/dist/lib/model.d.ts +12 -12
  17. package/node_modules/@llblab/pi-telegram/dist/lib/model.js +49 -35
  18. package/node_modules/@llblab/pi-telegram/dist/lib/queue.d.ts +0 -2
  19. package/node_modules/@llblab/pi-telegram/dist/lib/queue.js +1 -8
  20. package/node_modules/@llblab/pi-telegram/dist/lib/routing.js +0 -1
  21. package/node_modules/@llblab/pi-telegram/dist/lib/runtime.d.ts +3 -6
  22. package/node_modules/@llblab/pi-telegram/dist/lib/runtime.js +4 -55
  23. package/node_modules/@llblab/pi-telegram/dist/lib/skills.d.ts +2 -2
  24. package/node_modules/@llblab/pi-telegram/dist/lib/skills.js +9 -2
  25. package/node_modules/@llblab/pi-telegram/dist/lib/telegram-api.js +36 -10
  26. package/node_modules/@llblab/pi-telegram/dist/package.json +1 -1
  27. package/node_modules/@llblab/pi-telegram/docs/architecture.md +8 -8
  28. package/node_modules/@llblab/pi-telegram/docs/multi-instance-bus.md +3 -3
  29. package/node_modules/@llblab/pi-telegram/docs/public-api.md +1 -1
  30. package/node_modules/@llblab/pi-telegram/lib/bindings.ts +7 -4
  31. package/node_modules/@llblab/pi-telegram/lib/bus-api.ts +0 -7
  32. package/node_modules/@llblab/pi-telegram/lib/bus-leader.ts +27 -3
  33. package/node_modules/@llblab/pi-telegram/lib/extension.ts +1 -3
  34. package/node_modules/@llblab/pi-telegram/lib/menu-model.ts +25 -4
  35. package/node_modules/@llblab/pi-telegram/lib/menu.ts +14 -9
  36. package/node_modules/@llblab/pi-telegram/lib/model.ts +77 -44
  37. package/node_modules/@llblab/pi-telegram/lib/queue.ts +1 -8
  38. package/node_modules/@llblab/pi-telegram/lib/routing.ts +0 -1
  39. package/node_modules/@llblab/pi-telegram/lib/runtime.ts +7 -58
  40. package/node_modules/@llblab/pi-telegram/lib/skills.ts +10 -2
  41. package/node_modules/@llblab/pi-telegram/lib/telegram-api.ts +43 -17
  42. package/node_modules/@llblab/pi-telegram/package.json +1 -1
  43. package/package.json +2 -2
@@ -1866,14 +1866,16 @@ export function createTelegramBridgeApiRuntime(
1866
1866
  deps.chatActionMinIntervalMs ?? 2_000,
1867
1867
  );
1868
1868
  const chatActionMaxGates = Math.max(1, deps.chatActionMaxGates ?? 256);
1869
- const chatActionGates = new Map<
1870
- string,
1871
- { inFlight?: Promise<unknown>; notBeforeMs: number }
1872
- >();
1873
- const getChatActionKey = (
1869
+ type ChatActionGate = {
1870
+ inFlight?: Promise<unknown>;
1871
+ notBeforeMs: number;
1872
+ };
1873
+ const chatActionGates = new Map<string, ChatActionGate>();
1874
+ const chatActionChatGates = new Map<string, ChatActionGate>();
1875
+ const getChatActionKeys = (
1874
1876
  method: string,
1875
1877
  body: Record<string, unknown>,
1876
- ): string | undefined => {
1878
+ ): { action: string; chat: string } | undefined => {
1877
1879
  if (method !== "sendChatAction") return undefined;
1878
1880
  const chatId = body.chat_id;
1879
1881
  const action = body.action;
@@ -1883,12 +1885,16 @@ export function createTelegramBridgeApiRuntime(
1883
1885
  ) {
1884
1886
  return undefined;
1885
1887
  }
1888
+ const chat = String(chatId);
1886
1889
  const threadId = body.message_thread_id;
1887
- return `${String(chatId)}:${
1888
- typeof threadId === "number" || typeof threadId === "string"
1889
- ? String(threadId)
1890
- : "all"
1891
- }:${action}`;
1890
+ return {
1891
+ action: `${chat}:${
1892
+ typeof threadId === "number" || typeof threadId === "string"
1893
+ ? String(threadId)
1894
+ : "all"
1895
+ }:${action}`,
1896
+ chat,
1897
+ };
1892
1898
  };
1893
1899
  const callRecorded = async <TResponse>(
1894
1900
  method: string,
@@ -1896,22 +1902,38 @@ export function createTelegramBridgeApiRuntime(
1896
1902
  options?: TelegramApiCallOptions,
1897
1903
  ): Promise<TResponse> => {
1898
1904
  const recoverError = deps.captureRequestErrorHandler?.(body);
1899
- const chatActionKey = getChatActionKey(method, body);
1900
- if (chatActionKey) {
1905
+ const chatActionKeys = getChatActionKeys(method, body);
1906
+ if (chatActionKeys) {
1901
1907
  const nowMs = now();
1902
1908
  for (const [key, candidate] of chatActionGates) {
1903
1909
  if (!candidate.inFlight && nowMs >= candidate.notBeforeMs) {
1904
1910
  chatActionGates.delete(key);
1905
1911
  }
1906
1912
  }
1907
- let gate = chatActionGates.get(chatActionKey);
1913
+ for (const [key, candidate] of chatActionChatGates) {
1914
+ if (!candidate.inFlight && nowMs >= candidate.notBeforeMs) {
1915
+ chatActionChatGates.delete(key);
1916
+ }
1917
+ }
1918
+ let gate = chatActionGates.get(chatActionKeys.action);
1908
1919
  if (!gate) {
1909
1920
  if (chatActionGates.size >= chatActionMaxGates) return true as TResponse;
1910
1921
  gate = { notBeforeMs: 0 };
1911
- chatActionGates.set(chatActionKey, gate);
1922
+ chatActionGates.set(chatActionKeys.action, gate);
1923
+ }
1924
+ let chatGate = chatActionChatGates.get(chatActionKeys.chat);
1925
+ if (!chatGate) {
1926
+ if (chatActionChatGates.size >= chatActionMaxGates) {
1927
+ return true as TResponse;
1928
+ }
1929
+ chatGate = { notBeforeMs: 0 };
1930
+ chatActionChatGates.set(chatActionKeys.chat, chatGate);
1912
1931
  }
1913
1932
  if (gate.inFlight) return (await gate.inFlight) as TResponse;
1914
- if (now() < gate.notBeforeMs) return true as TResponse;
1933
+ if (chatGate.inFlight) return true as TResponse;
1934
+ if (nowMs < gate.notBeforeMs || nowMs < chatGate.notBeforeMs) {
1935
+ return true as TResponse;
1936
+ }
1915
1937
  let request: Promise<TResponse>;
1916
1938
  request = Promise.resolve()
1917
1939
  .then(() =>
@@ -1931,7 +1953,9 @@ export function createTelegramBridgeApiRuntime(
1931
1953
  chatActionMinIntervalMs,
1932
1954
  (error.retryAfterSeconds ?? 0) * 1_000,
1933
1955
  );
1934
- gate.notBeforeMs = now() + retryAfterMs;
1956
+ const notBeforeMs = now() + retryAfterMs;
1957
+ gate.notBeforeMs = notBeforeMs;
1958
+ chatGate.notBeforeMs = Math.max(chatGate.notBeforeMs, notBeforeMs);
1935
1959
  deps.recordRuntimeEvent(
1936
1960
  "api",
1937
1961
  error,
@@ -1952,8 +1976,10 @@ export function createTelegramBridgeApiRuntime(
1952
1976
  })
1953
1977
  .finally(() => {
1954
1978
  if (gate.inFlight === request) gate.inFlight = undefined;
1979
+ if (chatGate.inFlight === request) chatGate.inFlight = undefined;
1955
1980
  });
1956
1981
  gate.inFlight = request;
1982
+ chatGate.inFlight = request;
1957
1983
  return request;
1958
1984
  }
1959
1985
  try {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/pi-telegram",
3
- "version": "0.51.1",
3
+ "version": "0.51.3",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/pi-kit",
3
- "version": "0.22.0",
3
+ "version": "0.22.2",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -45,7 +45,7 @@
45
45
  "@llblab/pi-codex-usage": "0.10.0",
46
46
  "@llblab/pi-grow-loop": "0.8.1",
47
47
  "@llblab/pi-state-flow": "0.18.1",
48
- "@llblab/pi-telegram": "0.51.1",
48
+ "@llblab/pi-telegram": "0.51.3",
49
49
  "@llblab/skills": "1.15.0"
50
50
  },
51
51
  "bundledDependencies": [