@integrity-labs/agt-cli 0.19.16 → 0.19.18
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/dist/bin/agt.js +3 -3
- package/dist/{chunk-NT26H4DO.js → chunk-WTFROCJ3.js} +94 -3
- package/dist/chunk-WTFROCJ3.js.map +1 -0
- package/dist/lib/manager-worker.js +56 -35
- package/dist/lib/manager-worker.js.map +1 -1
- package/mcp/telegram-channel.js +145 -9
- package/package.json +1 -1
- package/dist/chunk-NT26H4DO.js.map +0 -1
package/mcp/telegram-channel.js
CHANGED
|
@@ -14211,6 +14211,82 @@ async function isThreadKilled(opts) {
|
|
|
14211
14211
|
}
|
|
14212
14212
|
}
|
|
14213
14213
|
|
|
14214
|
+
// src/telegram-peer-classifier.ts
|
|
14215
|
+
function classifyPeerMessage(msg, cfg, self) {
|
|
14216
|
+
if (!msg.from?.is_bot) return { kind: "human" };
|
|
14217
|
+
if (self.bot_id !== null && msg.from.id === self.bot_id) {
|
|
14218
|
+
return { kind: "self" };
|
|
14219
|
+
}
|
|
14220
|
+
if (cfg.peer_agent_mode === "off") {
|
|
14221
|
+
return { kind: "drop", reason: "mode_off" };
|
|
14222
|
+
}
|
|
14223
|
+
const chatId = String(msg.chat.id);
|
|
14224
|
+
if (cfg.peer_group_ids.length === 0) {
|
|
14225
|
+
if (msg.chat.type !== "private") {
|
|
14226
|
+
return { kind: "drop", reason: "chat_not_allowed" };
|
|
14227
|
+
}
|
|
14228
|
+
} else if (!cfg.peer_group_ids.includes(chatId)) {
|
|
14229
|
+
return { kind: "drop", reason: "chat_not_allowed" };
|
|
14230
|
+
}
|
|
14231
|
+
const peer = cfg.peers.find((p) => p.bot_id === msg.from.id);
|
|
14232
|
+
if (!peer) {
|
|
14233
|
+
return { kind: "drop", reason: "unknown_peer" };
|
|
14234
|
+
}
|
|
14235
|
+
if (self.bot_id === null || self.bot_username === null) {
|
|
14236
|
+
return { kind: "drop", reason: "self_resolution_pending" };
|
|
14237
|
+
}
|
|
14238
|
+
if (isAddressedToUs(msg, self.bot_id, self.bot_username)) {
|
|
14239
|
+
return { kind: "peer-ingress", peer };
|
|
14240
|
+
}
|
|
14241
|
+
return { kind: "drop", reason: "not_addressed" };
|
|
14242
|
+
}
|
|
14243
|
+
function isAddressedToUs(msg, ourBotId, ourBotUsername) {
|
|
14244
|
+
if (msg.reply_to_message?.from?.id === ourBotId) return true;
|
|
14245
|
+
const text = msg.text ?? msg.caption ?? "";
|
|
14246
|
+
const entities = msg.entities ?? msg.caption_entities ?? [];
|
|
14247
|
+
const usAt = "@" + ourBotUsername;
|
|
14248
|
+
for (const entity of entities) {
|
|
14249
|
+
const slice = text.slice(entity.offset, entity.offset + entity.length).toLowerCase();
|
|
14250
|
+
if (entity.type === "bot_command") {
|
|
14251
|
+
const at = slice.indexOf("@");
|
|
14252
|
+
if (at < 0) {
|
|
14253
|
+
return true;
|
|
14254
|
+
}
|
|
14255
|
+
if (slice.slice(at + 1) === ourBotUsername) return true;
|
|
14256
|
+
} else if (entity.type === "mention" && slice === usAt) {
|
|
14257
|
+
return true;
|
|
14258
|
+
}
|
|
14259
|
+
}
|
|
14260
|
+
return false;
|
|
14261
|
+
}
|
|
14262
|
+
var CODE_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
14263
|
+
function parsePeersEnv(raw) {
|
|
14264
|
+
if (!raw || raw.trim().length === 0) return [];
|
|
14265
|
+
let parsed;
|
|
14266
|
+
try {
|
|
14267
|
+
parsed = JSON.parse(raw);
|
|
14268
|
+
} catch {
|
|
14269
|
+
return [];
|
|
14270
|
+
}
|
|
14271
|
+
if (!Array.isArray(parsed)) return [];
|
|
14272
|
+
const out = [];
|
|
14273
|
+
for (const entry of parsed) {
|
|
14274
|
+
if (entry && typeof entry === "object" && typeof entry.code_name === "string" && CODE_NAME_RE.test(entry.code_name) && typeof entry.agent_id === "string" && typeof entry.bot_id === "number" && Number.isInteger(entry.bot_id) && entry.bot_id > 0) {
|
|
14275
|
+
const e = entry;
|
|
14276
|
+
out.push({ code_name: e.code_name, bot_id: e.bot_id, agent_id: e.agent_id });
|
|
14277
|
+
}
|
|
14278
|
+
}
|
|
14279
|
+
return out;
|
|
14280
|
+
}
|
|
14281
|
+
function parsePeerGroupIdsEnv(raw) {
|
|
14282
|
+
if (!raw) return [];
|
|
14283
|
+
return raw.split(",").map((s) => s.trim()).filter(Boolean);
|
|
14284
|
+
}
|
|
14285
|
+
function parsePeerAgentModeEnv(raw) {
|
|
14286
|
+
if (raw === "listen" || raw === "respond") return raw;
|
|
14287
|
+
return "off";
|
|
14288
|
+
}
|
|
14289
|
+
|
|
14214
14290
|
// src/telegram-channel.ts
|
|
14215
14291
|
function redactId(id) {
|
|
14216
14292
|
return createHash("sha256").update(String(id)).digest("hex").slice(0, 8);
|
|
@@ -14222,6 +14298,11 @@ var AGT_API_KEY = process.env.AGT_API_KEY ?? null;
|
|
|
14222
14298
|
var ALLOWED_CHATS = new Set(
|
|
14223
14299
|
(process.env.TELEGRAM_ALLOWED_CHATS ?? "").split(",").map((s) => s.trim()).filter(Boolean)
|
|
14224
14300
|
);
|
|
14301
|
+
var PEER_CLASSIFIER_CONFIG = {
|
|
14302
|
+
peer_agent_mode: parsePeerAgentModeEnv(process.env.TELEGRAM_PEER_AGENT_MODE),
|
|
14303
|
+
peer_group_ids: parsePeerGroupIdsEnv(process.env.TELEGRAM_PEER_GROUP_IDS),
|
|
14304
|
+
peers: parsePeersEnv(process.env.TELEGRAM_PEERS)
|
|
14305
|
+
};
|
|
14225
14306
|
if (!BOT_TOKEN) {
|
|
14226
14307
|
process.stderr.write(
|
|
14227
14308
|
"telegram-channel: Missing TELEGRAM_BOT_TOKEN. Cannot start.\n"
|
|
@@ -14418,25 +14499,41 @@ async function handleRestartCommand(opts) {
|
|
|
14418
14499
|
}
|
|
14419
14500
|
}
|
|
14420
14501
|
var cachedBotUsername = null;
|
|
14421
|
-
|
|
14422
|
-
|
|
14502
|
+
var cachedBotId = null;
|
|
14503
|
+
async function refreshBotIdentity() {
|
|
14423
14504
|
try {
|
|
14424
14505
|
const resp = await telegramApiCall("getMe", {}, 5e3);
|
|
14425
|
-
if (resp.ok && resp.result
|
|
14426
|
-
|
|
14427
|
-
|
|
14506
|
+
if (resp.ok && resp.result) {
|
|
14507
|
+
const result = resp.result;
|
|
14508
|
+
if (typeof result.username === "string") {
|
|
14509
|
+
cachedBotUsername = result.username.toLowerCase();
|
|
14510
|
+
}
|
|
14511
|
+
if (typeof result.id === "number") {
|
|
14512
|
+
cachedBotId = result.id;
|
|
14513
|
+
}
|
|
14514
|
+
if (cachedBotUsername !== null && cachedBotId !== null) return;
|
|
14428
14515
|
}
|
|
14429
14516
|
process.stderr.write(
|
|
14430
|
-
`telegram-channel(${AGENT_CODE_NAME}): getMe rejected: ${resp.description ?? "unknown"} \u2014 will retry on next
|
|
14517
|
+
`telegram-channel(${AGENT_CODE_NAME}): getMe rejected: ${resp.description ?? "unknown"} \u2014 will retry on next lookup
|
|
14431
14518
|
`
|
|
14432
14519
|
);
|
|
14433
14520
|
} catch (err) {
|
|
14434
14521
|
process.stderr.write(
|
|
14435
|
-
`telegram-channel(${AGENT_CODE_NAME}): getMe failed: ${redactAugmentedPaths(err.message)} \u2014 will retry on next
|
|
14522
|
+
`telegram-channel(${AGENT_CODE_NAME}): getMe failed: ${redactAugmentedPaths(err.message)} \u2014 will retry on next lookup
|
|
14436
14523
|
`
|
|
14437
14524
|
);
|
|
14438
14525
|
}
|
|
14439
|
-
|
|
14526
|
+
}
|
|
14527
|
+
async function resolveBotUsername() {
|
|
14528
|
+
if (cachedBotUsername !== null) return cachedBotUsername;
|
|
14529
|
+
await refreshBotIdentity();
|
|
14530
|
+
return cachedBotUsername;
|
|
14531
|
+
}
|
|
14532
|
+
async function resolveBotIdentity() {
|
|
14533
|
+
if (cachedBotId === null || cachedBotUsername === null) {
|
|
14534
|
+
await refreshBotIdentity();
|
|
14535
|
+
}
|
|
14536
|
+
return { bot_id: cachedBotId, bot_username: cachedBotUsername };
|
|
14440
14537
|
}
|
|
14441
14538
|
var RESTART_SYNTAX_RE = /^\/restart(?:@([A-Za-z0-9_]{1,64}))?(?:\s|$)/;
|
|
14442
14539
|
var HELP_SYNTAX_RE = /^\/help(?:@([A-Za-z0-9_]{1,64}))?(?:\s|$)/;
|
|
@@ -15151,6 +15248,37 @@ async function pollLoop() {
|
|
|
15151
15248
|
if (chatId && !isFromBot) {
|
|
15152
15249
|
resetThread(chatId, "");
|
|
15153
15250
|
}
|
|
15251
|
+
let peerAgentMeta = null;
|
|
15252
|
+
if (isFromBot) {
|
|
15253
|
+
const classifierMsg = {
|
|
15254
|
+
message_id: msg.message_id,
|
|
15255
|
+
text: msg.text,
|
|
15256
|
+
caption: msg.caption,
|
|
15257
|
+
from: msg.from ? { id: msg.from.id, username: msg.from.username, is_bot: msg.from.is_bot } : void 0,
|
|
15258
|
+
chat: { id: msg.chat.id, type: msg.chat.type },
|
|
15259
|
+
reply_to_message: msg.reply_to_message ? {
|
|
15260
|
+
message_id: msg.reply_to_message.message_id,
|
|
15261
|
+
from: msg.reply_to_message.from ? { id: msg.reply_to_message.from.id, is_bot: msg.reply_to_message.from.is_bot } : void 0
|
|
15262
|
+
} : void 0,
|
|
15263
|
+
entities: msg.entities,
|
|
15264
|
+
caption_entities: msg.caption_entities
|
|
15265
|
+
};
|
|
15266
|
+
const self = await resolveBotIdentity();
|
|
15267
|
+
const classification = classifyPeerMessage(classifierMsg, PEER_CLASSIFIER_CONFIG, self);
|
|
15268
|
+
if (classification.kind === "self") {
|
|
15269
|
+
continue;
|
|
15270
|
+
}
|
|
15271
|
+
if (classification.kind === "drop") {
|
|
15272
|
+
process.stderr.write(
|
|
15273
|
+
`telegram-channel(${AGENT_CODE_NAME}): peer_drop reason=${classification.reason} chat=${redactId(chatId)} from=${redactId(String(msg.from?.id ?? "unknown"))}
|
|
15274
|
+
`
|
|
15275
|
+
);
|
|
15276
|
+
continue;
|
|
15277
|
+
}
|
|
15278
|
+
if (classification.kind === "peer-ingress") {
|
|
15279
|
+
peerAgentMeta = { code_name: classification.peer.code_name, agent_id: classification.peer.agent_id };
|
|
15280
|
+
}
|
|
15281
|
+
}
|
|
15154
15282
|
const messageId = String(msg.message_id);
|
|
15155
15283
|
void setMessageReaction(chatId, messageId, ACK_EMOJI);
|
|
15156
15284
|
trackPendingMessage(chatId, messageId, msg.chat.type);
|
|
@@ -15194,7 +15322,15 @@ async function pollLoop() {
|
|
|
15194
15322
|
user_name: userName,
|
|
15195
15323
|
ts: String(msg.date),
|
|
15196
15324
|
...fileMeta.length > 0 ? { files: JSON.stringify(fileMeta) } : {},
|
|
15197
|
-
...imagePath ? { image_path: imagePath } : {}
|
|
15325
|
+
...imagePath ? { image_path: imagePath } : {},
|
|
15326
|
+
// ENG-4902: peer-agent ingress carries distinct framing so the
|
|
15327
|
+
// runtime (MVP #5) can apply the peer-agent system preamble
|
|
15328
|
+
// and store the turn under a peer_agent memory role.
|
|
15329
|
+
...peerAgentMeta ? {
|
|
15330
|
+
source_role: "agent",
|
|
15331
|
+
peer_code_name: peerAgentMeta.code_name,
|
|
15332
|
+
peer_agent_id: peerAgentMeta.agent_id
|
|
15333
|
+
} : {}
|
|
15198
15334
|
}
|
|
15199
15335
|
}
|
|
15200
15336
|
});
|