@agentshouse/kit 0.1.0-alpha.2 → 0.1.0-alpha.20

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/src/telegram.js CHANGED
@@ -863,20 +863,21 @@ async function onePass(gateway, activity, work) {
863
863
  try {
864
864
  await work(gateway);
865
865
  } catch (error) {
866
- if (cancelled(error) || !(error instanceof KitRefusal)) throw error;
866
+ if (cancelled(error) || !(error instanceof KitRefusal) || error.code === "kit_update_draining") throw error;
867
867
  gateway.summary.failures.push({ activity, item: null, code: error.code });
868
868
  }
869
869
  }
870
870
 
871
871
  async function paceActivity(gateway, stopping, activity, work) {
872
872
  try {
873
- for (let index = 0; index < gateway.passes; index += 1) {
873
+ for (let index = 0; index < gateway.passes && !gateway.draining; index += 1) {
874
874
  if (gateway.signal?.aborted === true) throw new KitRefusal("kit_command_cancelled");
875
875
  await onePass(gateway, activity, work);
876
876
  if (index + 1 < gateway.passes) await gateway.wait(TELEGRAM_GATEWAY_POLL_MS, gateway.signal);
877
877
  }
878
878
  } catch (error) {
879
- stopping.abort();
879
+ if (error.code === "kit_update_draining") gateway.draining = true;
880
+ else stopping.abort();
880
881
  throw error;
881
882
  }
882
883
  }
@@ -955,22 +956,31 @@ async function proveOwner(gateway, token, telegramBotUserId) {
955
956
  throw new KitRefusal("telegram_owner_not_proven");
956
957
  }
957
958
 
958
- function gatewayContext(session, options, dependencies, signal, summary = emptySummary()) {
959
+ function gatewayContext(session, options, signal, summary = emptySummary()) {
959
960
  return {
960
961
  session,
961
962
  directory: options.directory,
962
- dependencies,
963
+ dependencies: session.dependencies,
963
964
  signal,
964
965
  gate: rateGate(() => session.now().getTime()),
965
- wait: dependencies.wait ?? pause,
966
+ wait: session.dependencies.wait ?? pause,
966
967
  passes: options.passes ?? Number.POSITIVE_INFINITY,
968
+ draining: false,
967
969
  summary,
968
970
  };
969
971
  }
970
972
 
971
973
  export async function connectPersonalBot(options, dependencies = {}) {
972
974
  const session = await openSession(options, dependencies);
973
- const gateway = gatewayContext(session, options, dependencies, options.signal);
975
+ return maintainControl(
976
+ session,
977
+ (signal) => connectBot(session, options, dependencies, signal),
978
+ options.signal,
979
+ );
980
+ }
981
+
982
+ async function connectBot(session, options, dependencies, signal) {
983
+ const gateway = gatewayContext(session, options, signal);
974
984
  const ask = dependencies.prompt ?? (() => askForToken(dependencies));
975
985
  const token = (await ask()).trim();
976
986
  if (token.length === 0) throw new KitRefusal("telegram_bot_token_required");
@@ -989,7 +999,7 @@ export async function connectPersonalBot(options, dependencies = {}) {
989
999
  owner_telegram_user_id: await proveOwner(gateway, token, identity.telegramBotUserId),
990
1000
  has_topics_enabled: true,
991
1001
  };
992
- await writeOwned(tokenPath(options.directory), `${token}\n`, options.signal);
1002
+ await writeOwned(tokenPath(options.directory), `${token}\n`, signal);
993
1003
  try {
994
1004
  await houseCall(gateway, "personal-bot", setup);
995
1005
  } catch (error) {
@@ -1001,7 +1011,15 @@ export async function connectPersonalBot(options, dependencies = {}) {
1001
1011
 
1002
1012
  export async function disconnectPersonalBot(options, dependencies = {}) {
1003
1013
  const session = await openSession(options, dependencies);
1004
- const gateway = gatewayContext(session, options, dependencies, options.signal);
1014
+ return maintainControl(
1015
+ session,
1016
+ (signal) => disconnectBot(session, options, signal),
1017
+ options.signal,
1018
+ );
1019
+ }
1020
+
1021
+ async function disconnectBot(session, options, signal) {
1022
+ const gateway = gatewayContext(session, options, signal);
1005
1023
  const directory = options.directory;
1006
1024
  const marked = await disconnecting(directory);
1007
1025
  let token = null;
@@ -1010,7 +1028,7 @@ export async function disconnectPersonalBot(options, dependencies = {}) {
1010
1028
  } catch (error) {
1011
1029
  if (!marked) throw error;
1012
1030
  }
1013
- if (!marked) await writeOwned(disconnectPath(directory), "{}\n", options.signal);
1031
+ if (!marked) await writeOwned(disconnectPath(directory), "{}\n", signal);
1014
1032
  if (token !== null) {
1015
1033
  await telegramCall(gateway, token, "deleteWebhook", { drop_pending_updates: false });
1016
1034
  await Promise.all([
@@ -1032,7 +1050,7 @@ export async function serveTelegramGateway(options, dependencies = {}) {
1032
1050
  async (signal) => {
1033
1051
  const stopping = new AbortController();
1034
1052
  const shared = AbortSignal.any([signal, stopping.signal]);
1035
- const gateway = gatewayContext(session, options, dependencies, shared, summary);
1053
+ const gateway = gatewayContext(session, options, shared, summary);
1036
1054
  summary.abandoned = abandonedOf(await houseCall(gateway, "deliveries/abandoned"));
1037
1055
  await serveActivities(gateway, stopping);
1038
1056
  },