@integrity-labs/agt-cli 0.19.20 → 0.19.21

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.
@@ -14733,6 +14733,88 @@ function createCrossTeamPeerAuditClient(args) {
14733
14733
  };
14734
14734
  }
14735
14735
 
14736
+ // src/observed-chat-client.ts
14737
+ var REQUEST_TIMEOUT_MS2 = 1e4;
14738
+ function createObservedChatClient(args) {
14739
+ if (!args.agtHost || !args.agtApiKey || !args.agentId) return null;
14740
+ const fetchImpl = args.fetchImpl ?? fetch;
14741
+ const log = args.log ?? (() => {
14742
+ });
14743
+ const base = args.agtHost.replace(/\/+$/, "");
14744
+ const agentId = args.agentId;
14745
+ const apiKey = args.agtApiKey;
14746
+ const reported = /* @__PURE__ */ new Map();
14747
+ let cachedToken = null;
14748
+ let cachedTokenExpiresAt = 0;
14749
+ async function getToken() {
14750
+ if (cachedToken && Date.now() < cachedTokenExpiresAt) return cachedToken;
14751
+ const resp = await fetchImpl(`${base}/host/exchange`, {
14752
+ method: "POST",
14753
+ headers: { "Content-Type": "application/json" },
14754
+ body: JSON.stringify({ host_key: apiKey }),
14755
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS2)
14756
+ });
14757
+ if (!resp.ok) {
14758
+ const body = await resp.text().catch(() => "");
14759
+ throw new Error(`/host/exchange failed (${resp.status}): ${body.slice(0, 200)}`);
14760
+ }
14761
+ const data = await resp.json();
14762
+ cachedToken = data.token;
14763
+ cachedTokenExpiresAt = data.expires_at ? new Date(data.expires_at).getTime() - 12e4 : Date.now() + 55 * 6e4;
14764
+ return cachedToken;
14765
+ }
14766
+ async function postOnce(chat) {
14767
+ const token = await getToken();
14768
+ return fetchImpl(`${base}/host/observed-chat`, {
14769
+ method: "POST",
14770
+ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
14771
+ body: JSON.stringify({ agent_id: agentId, ...chat }),
14772
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS2)
14773
+ });
14774
+ }
14775
+ async function emitAsync(chat) {
14776
+ try {
14777
+ let resp = await postOnce(chat);
14778
+ if (resp.status === 401) {
14779
+ cachedToken = null;
14780
+ cachedTokenExpiresAt = 0;
14781
+ resp = await postOnce(chat);
14782
+ }
14783
+ if (!resp.ok) {
14784
+ const body = await resp.text().catch(() => "");
14785
+ log(
14786
+ `observed-chat: POST failed (${resp.status}) chat=${chat.chat_id}: ${body.slice(0, 200)}`
14787
+ );
14788
+ return false;
14789
+ }
14790
+ return true;
14791
+ } catch (err) {
14792
+ log(`observed-chat: emit threw chat=${chat.chat_id}: ${err.message}`);
14793
+ return false;
14794
+ }
14795
+ }
14796
+ return {
14797
+ observe(chat) {
14798
+ const prev = reported.get(chat.chat_id);
14799
+ if (prev && prev.title === (chat.chat_title ?? void 0) && prev.type === (chat.chat_type ?? void 0)) {
14800
+ return;
14801
+ }
14802
+ const snapshot = {
14803
+ title: chat.chat_title ?? void 0,
14804
+ type: chat.chat_type ?? void 0
14805
+ };
14806
+ reported.set(chat.chat_id, snapshot);
14807
+ void emitAsync(chat).then((ok) => {
14808
+ if (ok) return;
14809
+ const current = reported.get(chat.chat_id);
14810
+ if (current?.title === snapshot.title && current?.type === snapshot.type) {
14811
+ reported.delete(chat.chat_id);
14812
+ }
14813
+ });
14814
+ }
14815
+ };
14816
+ }
14817
+
14736
14818
  // src/telegram-channel.ts
14737
14819
  function redactId(id) {
14738
14820
  return createHash("sha256").update(String(id)).digest("hex").slice(0, 8);
@@ -14779,6 +14861,13 @@ var crossTeamPeerAuditClient = createCrossTeamPeerAuditClient({
14779
14861
  log: (line) => process.stderr.write(`telegram-channel(${AGENT_CODE_NAME}): ${line}
14780
14862
  `)
14781
14863
  });
14864
+ var observedChatClient = createObservedChatClient({
14865
+ agtHost: AGT_HOST,
14866
+ agtApiKey: AGT_API_KEY,
14867
+ agentId: process.env.AGT_AGENT_ID ?? null,
14868
+ log: (line) => process.stderr.write(`telegram-channel(${AGENT_CODE_NAME}): ${line}
14869
+ `)
14870
+ });
14782
14871
  var peerRateApiClient = createDefaultPeerRateApiClient({
14783
14872
  agtHost: AGT_HOST,
14784
14873
  agtApiKey: AGT_API_KEY,
@@ -15726,6 +15815,11 @@ async function pollLoop() {
15726
15815
  if (content.length === 0 && classifiedAttachments.length === 0) continue;
15727
15816
  const chatId = String(msg.chat.id);
15728
15817
  if (ALLOWED_CHATS.size > 0 && !ALLOWED_CHATS.has(chatId)) continue;
15818
+ observedChatClient?.observe({
15819
+ chat_id: chatId,
15820
+ chat_title: msg.chat.title ?? msg.chat.username ?? msg.chat.first_name ?? null,
15821
+ chat_type: msg.chat.type ?? null
15822
+ });
15729
15823
  const trimmedContent = content.trim();
15730
15824
  if (isHelpSyntax(trimmedContent)) {
15731
15825
  const disposition = await classifyRestartCommand(trimmedContent.replace(/^\/help/, "/restart"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@integrity-labs/agt-cli",
3
- "version": "0.19.20",
3
+ "version": "0.19.21",
4
4
  "description": "Augmented Team CLI — agent provisioning and management",
5
5
  "type": "module",
6
6
  "engines": {