feat: add tlon channel plugin
This commit is contained in:
71
extensions/tlon/src/monitor/discovery.ts
Normal file
71
extensions/tlon/src/monitor/discovery.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import type { RuntimeEnv } from "clawdbot/plugin-sdk";
|
||||
|
||||
import { formatChangesDate } from "./utils.js";
|
||||
|
||||
export async function fetchGroupChanges(
|
||||
api: { scry: (path: string) => Promise<unknown> },
|
||||
runtime: RuntimeEnv,
|
||||
daysAgo = 5,
|
||||
) {
|
||||
try {
|
||||
const changeDate = formatChangesDate(daysAgo);
|
||||
runtime.log?.(`[tlon] Fetching group changes since ${daysAgo} days ago (${changeDate})...`);
|
||||
const changes = await api.scry(`/groups-ui/v5/changes/${changeDate}.json`);
|
||||
if (changes) {
|
||||
runtime.log?.("[tlon] Successfully fetched changes data");
|
||||
return changes;
|
||||
}
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
runtime.log?.(`[tlon] Failed to fetch changes (falling back to full init): ${error?.message ?? String(error)}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchAllChannels(
|
||||
api: { scry: (path: string) => Promise<unknown> },
|
||||
runtime: RuntimeEnv,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
runtime.log?.("[tlon] Attempting auto-discovery of group channels...");
|
||||
const changes = await fetchGroupChanges(api, runtime, 5);
|
||||
|
||||
let initData: any;
|
||||
if (changes) {
|
||||
runtime.log?.("[tlon] Changes data received, using full init for channel extraction");
|
||||
initData = await api.scry("/groups-ui/v6/init.json");
|
||||
} else {
|
||||
initData = await api.scry("/groups-ui/v6/init.json");
|
||||
}
|
||||
|
||||
const channels: string[] = [];
|
||||
if (initData && initData.groups) {
|
||||
for (const groupData of Object.values(initData.groups as Record<string, any>)) {
|
||||
if (groupData && typeof groupData === "object" && groupData.channels) {
|
||||
for (const channelNest of Object.keys(groupData.channels)) {
|
||||
if (channelNest.startsWith("chat/")) {
|
||||
channels.push(channelNest);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (channels.length > 0) {
|
||||
runtime.log?.(`[tlon] Auto-discovered ${channels.length} chat channel(s)`);
|
||||
runtime.log?.(
|
||||
`[tlon] Channels: ${channels.slice(0, 5).join(", ")}${channels.length > 5 ? "..." : ""}`,
|
||||
);
|
||||
} else {
|
||||
runtime.log?.("[tlon] No chat channels found via auto-discovery");
|
||||
runtime.log?.("[tlon] Add channels manually to config: channels.tlon.groupChannels");
|
||||
}
|
||||
|
||||
return channels;
|
||||
} catch (error: any) {
|
||||
runtime.log?.(`[tlon] Auto-discovery failed: ${error?.message ?? String(error)}`);
|
||||
runtime.log?.("[tlon] To monitor group channels, add them to config: channels.tlon.groupChannels");
|
||||
runtime.log?.("[tlon] Example: [\"chat/~host-ship/channel-name\"]");
|
||||
return [];
|
||||
}
|
||||
}
|
||||
87
extensions/tlon/src/monitor/history.ts
Normal file
87
extensions/tlon/src/monitor/history.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { RuntimeEnv } from "clawdbot/plugin-sdk";
|
||||
|
||||
import { extractMessageText } from "./utils.js";
|
||||
|
||||
export type TlonHistoryEntry = {
|
||||
author: string;
|
||||
content: string;
|
||||
timestamp: number;
|
||||
id?: string;
|
||||
};
|
||||
|
||||
const messageCache = new Map<string, TlonHistoryEntry[]>();
|
||||
const MAX_CACHED_MESSAGES = 100;
|
||||
|
||||
export function cacheMessage(channelNest: string, message: TlonHistoryEntry) {
|
||||
if (!messageCache.has(channelNest)) {
|
||||
messageCache.set(channelNest, []);
|
||||
}
|
||||
const cache = messageCache.get(channelNest);
|
||||
if (!cache) return;
|
||||
cache.unshift(message);
|
||||
if (cache.length > MAX_CACHED_MESSAGES) {
|
||||
cache.pop();
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchChannelHistory(
|
||||
api: { scry: (path: string) => Promise<unknown> },
|
||||
channelNest: string,
|
||||
count = 50,
|
||||
runtime?: RuntimeEnv,
|
||||
): Promise<TlonHistoryEntry[]> {
|
||||
try {
|
||||
const scryPath = `/channels/v4/${channelNest}/posts/newest/${count}/outline.json`;
|
||||
runtime?.log?.(`[tlon] Fetching history: ${scryPath}`);
|
||||
|
||||
const data: any = await api.scry(scryPath);
|
||||
if (!data) return [];
|
||||
|
||||
let posts: any[] = [];
|
||||
if (Array.isArray(data)) {
|
||||
posts = data;
|
||||
} else if (data.posts && typeof data.posts === "object") {
|
||||
posts = Object.values(data.posts);
|
||||
} else if (typeof data === "object") {
|
||||
posts = Object.values(data);
|
||||
}
|
||||
|
||||
const messages = posts
|
||||
.map((item) => {
|
||||
const essay = item.essay || item["r-post"]?.set?.essay;
|
||||
const seal = item.seal || item["r-post"]?.set?.seal;
|
||||
|
||||
return {
|
||||
author: essay?.author || "unknown",
|
||||
content: extractMessageText(essay?.content || []),
|
||||
timestamp: essay?.sent || Date.now(),
|
||||
id: seal?.id,
|
||||
} as TlonHistoryEntry;
|
||||
})
|
||||
.filter((msg) => msg.content);
|
||||
|
||||
runtime?.log?.(`[tlon] Extracted ${messages.length} messages from history`);
|
||||
return messages;
|
||||
} catch (error: any) {
|
||||
runtime?.log?.(`[tlon] Error fetching channel history: ${error?.message ?? String(error)}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getChannelHistory(
|
||||
api: { scry: (path: string) => Promise<unknown> },
|
||||
channelNest: string,
|
||||
count = 50,
|
||||
runtime?: RuntimeEnv,
|
||||
): Promise<TlonHistoryEntry[]> {
|
||||
const cache = messageCache.get(channelNest) ?? [];
|
||||
if (cache.length >= count) {
|
||||
runtime?.log?.(`[tlon] Using cached messages (${cache.length} available)`);
|
||||
return cache.slice(0, count);
|
||||
}
|
||||
|
||||
runtime?.log?.(
|
||||
`[tlon] Cache has ${cache.length} messages, need ${count}, fetching from scry...`,
|
||||
);
|
||||
return await fetchChannelHistory(api, channelNest, count, runtime);
|
||||
}
|
||||
501
extensions/tlon/src/monitor/index.ts
Normal file
501
extensions/tlon/src/monitor/index.ts
Normal file
@@ -0,0 +1,501 @@
|
||||
import { format } from "node:util";
|
||||
|
||||
import type { RuntimeEnv, ReplyPayload, ClawdbotConfig } from "clawdbot/plugin-sdk";
|
||||
|
||||
import { getTlonRuntime } from "../runtime.js";
|
||||
import { resolveTlonAccount } from "../types.js";
|
||||
import { normalizeShip, parseChannelNest } from "../targets.js";
|
||||
import { authenticate } from "../urbit/auth.js";
|
||||
import { UrbitSSEClient } from "../urbit/sse-client.js";
|
||||
import { sendDm, sendGroupMessage } from "../urbit/send.js";
|
||||
import { cacheMessage, getChannelHistory } from "./history.js";
|
||||
import { createProcessedMessageTracker } from "./processed-messages.js";
|
||||
import {
|
||||
extractMessageText,
|
||||
formatModelName,
|
||||
isBotMentioned,
|
||||
isDmAllowed,
|
||||
isSummarizationRequest,
|
||||
} from "./utils.js";
|
||||
import { fetchAllChannels } from "./discovery.js";
|
||||
|
||||
export type MonitorTlonOpts = {
|
||||
runtime?: RuntimeEnv;
|
||||
abortSignal?: AbortSignal;
|
||||
accountId?: string | null;
|
||||
};
|
||||
|
||||
type ChannelAuthorization = {
|
||||
mode?: "restricted" | "open";
|
||||
allowedShips?: string[];
|
||||
};
|
||||
|
||||
function resolveChannelAuthorization(
|
||||
cfg: ClawdbotConfig,
|
||||
channelNest: string,
|
||||
): { mode: "restricted" | "open"; allowedShips: string[] } {
|
||||
const tlonConfig = cfg.channels?.tlon as
|
||||
| {
|
||||
authorization?: { channelRules?: Record<string, ChannelAuthorization> };
|
||||
defaultAuthorizedShips?: string[];
|
||||
}
|
||||
| undefined;
|
||||
const rules = tlonConfig?.authorization?.channelRules ?? {};
|
||||
const rule = rules[channelNest];
|
||||
const allowedShips = rule?.allowedShips ?? tlonConfig?.defaultAuthorizedShips ?? [];
|
||||
const mode = rule?.mode ?? "restricted";
|
||||
return { mode, allowedShips };
|
||||
}
|
||||
|
||||
export async function monitorTlonProvider(opts: MonitorTlonOpts = {}): Promise<void> {
|
||||
const core = getTlonRuntime();
|
||||
const cfg = core.config.loadConfig() as ClawdbotConfig;
|
||||
if (cfg.channels?.tlon?.enabled === false) return;
|
||||
|
||||
const logger = core.logging.getChildLogger({ module: "tlon-auto-reply" });
|
||||
const formatRuntimeMessage = (...args: Parameters<RuntimeEnv["log"]>) => format(...args);
|
||||
const runtime: RuntimeEnv = opts.runtime ?? {
|
||||
log: (...args) => {
|
||||
logger.info(formatRuntimeMessage(...args));
|
||||
},
|
||||
error: (...args) => {
|
||||
logger.error(formatRuntimeMessage(...args));
|
||||
},
|
||||
exit: (code: number): never => {
|
||||
throw new Error(`exit ${code}`);
|
||||
},
|
||||
};
|
||||
|
||||
const account = resolveTlonAccount(cfg, opts.accountId ?? undefined);
|
||||
if (!account.enabled) return;
|
||||
if (!account.configured || !account.ship || !account.url || !account.code) {
|
||||
throw new Error("Tlon account not configured (ship/url/code required)");
|
||||
}
|
||||
|
||||
const botShipName = normalizeShip(account.ship);
|
||||
runtime.log?.(`[tlon] Starting monitor for ${botShipName}`);
|
||||
|
||||
let api: UrbitSSEClient | null = null;
|
||||
try {
|
||||
runtime.log?.(`[tlon] Attempting authentication to ${account.url}...`);
|
||||
const cookie = await authenticate(account.url, account.code);
|
||||
api = new UrbitSSEClient(account.url, cookie, {
|
||||
ship: botShipName,
|
||||
logger: {
|
||||
log: (message) => runtime.log?.(message),
|
||||
error: (message) => runtime.error?.(message),
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Failed to authenticate: ${error?.message ?? String(error)}`);
|
||||
throw error;
|
||||
}
|
||||
|
||||
const processedTracker = createProcessedMessageTracker(2000);
|
||||
let groupChannels: string[] = [];
|
||||
|
||||
if (account.autoDiscoverChannels !== false) {
|
||||
try {
|
||||
const discoveredChannels = await fetchAllChannels(api, runtime);
|
||||
if (discoveredChannels.length > 0) {
|
||||
groupChannels = discoveredChannels;
|
||||
}
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Auto-discovery failed: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (groupChannels.length === 0 && account.groupChannels.length > 0) {
|
||||
groupChannels = account.groupChannels;
|
||||
runtime.log?.(`[tlon] Using manual groupChannels config: ${groupChannels.join(", ")}`);
|
||||
}
|
||||
|
||||
if (groupChannels.length > 0) {
|
||||
runtime.log?.(
|
||||
`[tlon] Monitoring ${groupChannels.length} group channel(s): ${groupChannels.join(", ")}`,
|
||||
);
|
||||
} else {
|
||||
runtime.log?.("[tlon] No group channels to monitor (DMs only)");
|
||||
}
|
||||
|
||||
const handleIncomingDM = async (update: any) => {
|
||||
try {
|
||||
const memo = update?.response?.add?.memo;
|
||||
if (!memo) return;
|
||||
|
||||
const messageId = update.id as string | undefined;
|
||||
if (!processedTracker.mark(messageId)) return;
|
||||
|
||||
const senderShip = normalizeShip(memo.author ?? "");
|
||||
if (!senderShip || senderShip === botShipName) return;
|
||||
|
||||
const messageText = extractMessageText(memo.content);
|
||||
if (!messageText) return;
|
||||
|
||||
if (!isDmAllowed(senderShip, account.dmAllowlist)) {
|
||||
runtime.log?.(`[tlon] Blocked DM from ${senderShip}: not in allowlist`);
|
||||
return;
|
||||
}
|
||||
|
||||
await processMessage({
|
||||
messageId: messageId ?? "",
|
||||
senderShip,
|
||||
messageText,
|
||||
isGroup: false,
|
||||
timestamp: memo.sent || Date.now(),
|
||||
});
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Error handling DM: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
};
|
||||
|
||||
const handleIncomingGroupMessage = (channelNest: string) => async (update: any) => {
|
||||
try {
|
||||
const parsed = parseChannelNest(channelNest);
|
||||
if (!parsed) return;
|
||||
|
||||
const essay = update?.response?.post?.["r-post"]?.set?.essay;
|
||||
const memo = update?.response?.post?.["r-post"]?.reply?.["r-reply"]?.set?.memo;
|
||||
if (!essay && !memo) return;
|
||||
|
||||
const content = memo || essay;
|
||||
const isThreadReply = Boolean(memo);
|
||||
const messageId = isThreadReply
|
||||
? update?.response?.post?.["r-post"]?.reply?.id
|
||||
: update?.response?.post?.id;
|
||||
|
||||
if (!processedTracker.mark(messageId)) return;
|
||||
|
||||
const senderShip = normalizeShip(content.author ?? "");
|
||||
if (!senderShip || senderShip === botShipName) return;
|
||||
|
||||
const messageText = extractMessageText(content.content);
|
||||
if (!messageText) return;
|
||||
|
||||
cacheMessage(channelNest, {
|
||||
author: senderShip,
|
||||
content: messageText,
|
||||
timestamp: content.sent || Date.now(),
|
||||
id: messageId,
|
||||
});
|
||||
|
||||
const mentioned = isBotMentioned(messageText, botShipName);
|
||||
if (!mentioned) return;
|
||||
|
||||
const { mode, allowedShips } = resolveChannelAuthorization(cfg, channelNest);
|
||||
if (mode === "restricted") {
|
||||
if (allowedShips.length === 0) {
|
||||
runtime.log?.(`[tlon] Access denied: ${senderShip} in ${channelNest} (no allowlist)`);
|
||||
return;
|
||||
}
|
||||
const normalizedAllowed = allowedShips.map(normalizeShip);
|
||||
if (!normalizedAllowed.includes(senderShip)) {
|
||||
runtime.log?.(
|
||||
`[tlon] Access denied: ${senderShip} in ${channelNest} (allowed: ${allowedShips.join(", ")})`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const seal = isThreadReply
|
||||
? update?.response?.post?.["r-post"]?.reply?.["r-reply"]?.set?.seal
|
||||
: update?.response?.post?.["r-post"]?.set?.seal;
|
||||
|
||||
const parentId = seal?.["parent-id"] || seal?.parent || null;
|
||||
|
||||
await processMessage({
|
||||
messageId: messageId ?? "",
|
||||
senderShip,
|
||||
messageText,
|
||||
isGroup: true,
|
||||
groupChannel: channelNest,
|
||||
groupName: `${parsed.hostShip}/${parsed.channelName}`,
|
||||
timestamp: content.sent || Date.now(),
|
||||
parentId,
|
||||
});
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Error handling group message: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
};
|
||||
|
||||
const processMessage = async (params: {
|
||||
messageId: string;
|
||||
senderShip: string;
|
||||
messageText: string;
|
||||
isGroup: boolean;
|
||||
groupChannel?: string;
|
||||
groupName?: string;
|
||||
timestamp: number;
|
||||
parentId?: string | null;
|
||||
}) => {
|
||||
const { messageId, senderShip, isGroup, groupChannel, groupName, timestamp, parentId } = params;
|
||||
let messageText = params.messageText;
|
||||
|
||||
if (isGroup && groupChannel && isSummarizationRequest(messageText)) {
|
||||
try {
|
||||
const history = await getChannelHistory(api!, groupChannel, 50, runtime);
|
||||
if (history.length === 0) {
|
||||
const noHistoryMsg =
|
||||
"I couldn't fetch any messages for this channel. It might be empty or there might be a permissions issue.";
|
||||
if (isGroup) {
|
||||
const parsed = parseChannelNest(groupChannel);
|
||||
if (parsed) {
|
||||
await sendGroupMessage({
|
||||
api: api!,
|
||||
fromShip: botShipName,
|
||||
hostShip: parsed.hostShip,
|
||||
channelName: parsed.channelName,
|
||||
text: noHistoryMsg,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await sendDm({ api: api!, fromShip: botShipName, toShip: senderShip, text: noHistoryMsg });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const historyText = history
|
||||
.map((msg) => `[${new Date(msg.timestamp).toLocaleString()}] ${msg.author}: ${msg.content}`)
|
||||
.join("\n");
|
||||
|
||||
messageText =
|
||||
`Please summarize this channel conversation (${history.length} recent messages):\n\n${historyText}\n\n` +
|
||||
"Provide a concise summary highlighting:\n" +
|
||||
"1. Main topics discussed\n" +
|
||||
"2. Key decisions or conclusions\n" +
|
||||
"3. Action items if any\n" +
|
||||
"4. Notable participants";
|
||||
} catch (error: any) {
|
||||
const errorMsg = `Sorry, I encountered an error while fetching the channel history: ${error?.message ?? String(error)}`;
|
||||
if (isGroup && groupChannel) {
|
||||
const parsed = parseChannelNest(groupChannel);
|
||||
if (parsed) {
|
||||
await sendGroupMessage({
|
||||
api: api!,
|
||||
fromShip: botShipName,
|
||||
hostShip: parsed.hostShip,
|
||||
channelName: parsed.channelName,
|
||||
text: errorMsg,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await sendDm({ api: api!, fromShip: botShipName, toShip: senderShip, text: errorMsg });
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const route = core.channel.routing.resolveAgentRoute({
|
||||
cfg,
|
||||
channel: "tlon",
|
||||
accountId: opts.accountId ?? undefined,
|
||||
peer: {
|
||||
kind: isGroup ? "group" : "dm",
|
||||
id: isGroup ? groupChannel ?? senderShip : senderShip,
|
||||
},
|
||||
});
|
||||
|
||||
const fromLabel = isGroup ? `${senderShip} in ${groupName}` : senderShip;
|
||||
const body = core.channel.reply.formatAgentEnvelope({
|
||||
channel: "Tlon",
|
||||
from: fromLabel,
|
||||
timestamp,
|
||||
body: messageText,
|
||||
});
|
||||
|
||||
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
||||
Body: body,
|
||||
RawBody: messageText,
|
||||
CommandBody: messageText,
|
||||
From: isGroup ? `tlon:group:${groupChannel}` : `tlon:${senderShip}`,
|
||||
To: `tlon:${botShipName}`,
|
||||
SessionKey: route.sessionKey,
|
||||
AccountId: route.accountId,
|
||||
ChatType: isGroup ? "group" : "direct",
|
||||
ConversationLabel: fromLabel,
|
||||
SenderName: senderShip,
|
||||
SenderId: senderShip,
|
||||
Provider: "tlon",
|
||||
Surface: "tlon",
|
||||
MessageSid: messageId,
|
||||
OriginatingChannel: "tlon",
|
||||
OriginatingTo: `tlon:${isGroup ? groupChannel : botShipName}`,
|
||||
});
|
||||
|
||||
const dispatchStartTime = Date.now();
|
||||
|
||||
const responsePrefix = core.channel.reply.resolveEffectiveMessagesConfig(cfg, route.agentId)
|
||||
.responsePrefix;
|
||||
const humanDelay = core.channel.reply.resolveHumanDelayConfig(cfg, route.agentId);
|
||||
|
||||
await core.channel.reply.dispatchReplyWithBufferedBlockDispatcher({
|
||||
ctx: ctxPayload,
|
||||
cfg,
|
||||
dispatcherOptions: {
|
||||
responsePrefix,
|
||||
humanDelay,
|
||||
deliver: async (payload: ReplyPayload) => {
|
||||
let replyText = payload.text;
|
||||
if (!replyText) return;
|
||||
|
||||
const showSignature = account.showModelSignature ?? cfg.channels?.tlon?.showModelSignature ?? false;
|
||||
if (showSignature) {
|
||||
const modelInfo =
|
||||
payload.metadata?.model || payload.model || route.model || cfg.agents?.defaults?.model?.primary;
|
||||
replyText = `${replyText}\n\n_[Generated by ${formatModelName(modelInfo)}]_`;
|
||||
}
|
||||
|
||||
if (isGroup && groupChannel) {
|
||||
const parsed = parseChannelNest(groupChannel);
|
||||
if (!parsed) return;
|
||||
await sendGroupMessage({
|
||||
api: api!,
|
||||
fromShip: botShipName,
|
||||
hostShip: parsed.hostShip,
|
||||
channelName: parsed.channelName,
|
||||
text: replyText,
|
||||
replyToId: parentId ?? undefined,
|
||||
});
|
||||
} else {
|
||||
await sendDm({ api: api!, fromShip: botShipName, toShip: senderShip, text: replyText });
|
||||
}
|
||||
},
|
||||
onError: (err, info) => {
|
||||
const dispatchDuration = Date.now() - dispatchStartTime;
|
||||
runtime.error?.(
|
||||
`[tlon] ${info.kind} reply failed after ${dispatchDuration}ms: ${String(err)}`,
|
||||
);
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const subscribedChannels = new Set<string>();
|
||||
const subscribedDMs = new Set<string>();
|
||||
|
||||
async function subscribeToChannel(channelNest: string) {
|
||||
if (subscribedChannels.has(channelNest)) return;
|
||||
const parsed = parseChannelNest(channelNest);
|
||||
if (!parsed) {
|
||||
runtime.error?.(`[tlon] Invalid channel format: ${channelNest}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await api!.subscribe({
|
||||
app: "channels",
|
||||
path: `/${channelNest}`,
|
||||
event: handleIncomingGroupMessage(channelNest),
|
||||
err: (error) => {
|
||||
runtime.error?.(`[tlon] Group subscription error for ${channelNest}: ${String(error)}`);
|
||||
},
|
||||
quit: () => {
|
||||
runtime.log?.(`[tlon] Group subscription ended for ${channelNest}`);
|
||||
subscribedChannels.delete(channelNest);
|
||||
},
|
||||
});
|
||||
subscribedChannels.add(channelNest);
|
||||
runtime.log?.(`[tlon] Subscribed to group channel: ${channelNest}`);
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Failed to subscribe to ${channelNest}: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function subscribeToDM(dmShip: string) {
|
||||
if (subscribedDMs.has(dmShip)) return;
|
||||
try {
|
||||
await api!.subscribe({
|
||||
app: "chat",
|
||||
path: `/dm/${dmShip}`,
|
||||
event: handleIncomingDM,
|
||||
err: (error) => {
|
||||
runtime.error?.(`[tlon] DM subscription error for ${dmShip}: ${String(error)}`);
|
||||
},
|
||||
quit: () => {
|
||||
runtime.log?.(`[tlon] DM subscription ended for ${dmShip}`);
|
||||
subscribedDMs.delete(dmShip);
|
||||
},
|
||||
});
|
||||
subscribedDMs.add(dmShip);
|
||||
runtime.log?.(`[tlon] Subscribed to DM with ${dmShip}`);
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Failed to subscribe to DM with ${dmShip}: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshChannelSubscriptions() {
|
||||
try {
|
||||
const dmShips = await api!.scry("/chat/dm.json");
|
||||
if (Array.isArray(dmShips)) {
|
||||
for (const dmShip of dmShips) {
|
||||
await subscribeToDM(dmShip);
|
||||
}
|
||||
}
|
||||
|
||||
if (account.autoDiscoverChannels !== false) {
|
||||
const discoveredChannels = await fetchAllChannels(api!, runtime);
|
||||
for (const channelNest of discoveredChannels) {
|
||||
await subscribeToChannel(channelNest);
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Channel refresh failed: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
runtime.log?.("[tlon] Subscribing to updates...");
|
||||
|
||||
let dmShips: string[] = [];
|
||||
try {
|
||||
const dmList = await api!.scry("/chat/dm.json");
|
||||
if (Array.isArray(dmList)) {
|
||||
dmShips = dmList;
|
||||
runtime.log?.(`[tlon] Found ${dmShips.length} DM conversation(s)`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Failed to fetch DM list: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
|
||||
for (const dmShip of dmShips) {
|
||||
await subscribeToDM(dmShip);
|
||||
}
|
||||
|
||||
for (const channelNest of groupChannels) {
|
||||
await subscribeToChannel(channelNest);
|
||||
}
|
||||
|
||||
runtime.log?.("[tlon] All subscriptions registered, connecting to SSE stream...");
|
||||
await api!.connect();
|
||||
runtime.log?.("[tlon] Connected! All subscriptions active");
|
||||
|
||||
const pollInterval = setInterval(() => {
|
||||
if (!opts.abortSignal?.aborted) {
|
||||
refreshChannelSubscriptions().catch((error) => {
|
||||
runtime.error?.(`[tlon] Channel refresh error: ${error?.message ?? String(error)}`);
|
||||
});
|
||||
}
|
||||
}, 2 * 60 * 1000);
|
||||
|
||||
if (opts.abortSignal) {
|
||||
await new Promise((resolve) => {
|
||||
opts.abortSignal.addEventListener(
|
||||
"abort",
|
||||
() => {
|
||||
clearInterval(pollInterval);
|
||||
resolve(null);
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
} else {
|
||||
await new Promise(() => {});
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
await api?.close();
|
||||
} catch (error: any) {
|
||||
runtime.error?.(`[tlon] Cleanup error: ${error?.message ?? String(error)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
extensions/tlon/src/monitor/processed-messages.test.ts
Normal file
24
extensions/tlon/src/monitor/processed-messages.test.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { createProcessedMessageTracker } from "./processed-messages.js";
|
||||
|
||||
describe("createProcessedMessageTracker", () => {
|
||||
it("dedupes and evicts oldest entries", () => {
|
||||
const tracker = createProcessedMessageTracker(3);
|
||||
|
||||
expect(tracker.mark("a")).toBe(true);
|
||||
expect(tracker.mark("a")).toBe(false);
|
||||
expect(tracker.has("a")).toBe(true);
|
||||
|
||||
tracker.mark("b");
|
||||
tracker.mark("c");
|
||||
expect(tracker.size()).toBe(3);
|
||||
|
||||
tracker.mark("d");
|
||||
expect(tracker.size()).toBe(3);
|
||||
expect(tracker.has("a")).toBe(false);
|
||||
expect(tracker.has("b")).toBe(true);
|
||||
expect(tracker.has("c")).toBe(true);
|
||||
expect(tracker.has("d")).toBe(true);
|
||||
});
|
||||
});
|
||||
38
extensions/tlon/src/monitor/processed-messages.ts
Normal file
38
extensions/tlon/src/monitor/processed-messages.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export type ProcessedMessageTracker = {
|
||||
mark: (id?: string | null) => boolean;
|
||||
has: (id?: string | null) => boolean;
|
||||
size: () => number;
|
||||
};
|
||||
|
||||
export function createProcessedMessageTracker(limit = 2000): ProcessedMessageTracker {
|
||||
const seen = new Set<string>();
|
||||
const order: string[] = [];
|
||||
|
||||
const mark = (id?: string | null) => {
|
||||
const trimmed = id?.trim();
|
||||
if (!trimmed) return true;
|
||||
if (seen.has(trimmed)) return false;
|
||||
seen.add(trimmed);
|
||||
order.push(trimmed);
|
||||
if (order.length > limit) {
|
||||
const overflow = order.length - limit;
|
||||
for (let i = 0; i < overflow; i += 1) {
|
||||
const oldest = order.shift();
|
||||
if (oldest) seen.delete(oldest);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const has = (id?: string | null) => {
|
||||
const trimmed = id?.trim();
|
||||
if (!trimmed) return false;
|
||||
return seen.has(trimmed);
|
||||
};
|
||||
|
||||
return {
|
||||
mark,
|
||||
has,
|
||||
size: () => seen.size,
|
||||
};
|
||||
}
|
||||
83
extensions/tlon/src/monitor/utils.ts
Normal file
83
extensions/tlon/src/monitor/utils.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { normalizeShip } from "../targets.js";
|
||||
|
||||
export function formatModelName(modelString?: string | null): string {
|
||||
if (!modelString) return "AI";
|
||||
const modelName = modelString.includes("/") ? modelString.split("/")[1] : modelString;
|
||||
const modelMappings: Record<string, string> = {
|
||||
"claude-opus-4-5": "Claude Opus 4.5",
|
||||
"claude-sonnet-4-5": "Claude Sonnet 4.5",
|
||||
"claude-sonnet-3-5": "Claude Sonnet 3.5",
|
||||
"gpt-4o": "GPT-4o",
|
||||
"gpt-4-turbo": "GPT-4 Turbo",
|
||||
"gpt-4": "GPT-4",
|
||||
"gemini-2.0-flash": "Gemini 2.0 Flash",
|
||||
"gemini-pro": "Gemini Pro",
|
||||
};
|
||||
|
||||
if (modelMappings[modelName]) return modelMappings[modelName];
|
||||
return modelName
|
||||
.replace(/-/g, " ")
|
||||
.split(" ")
|
||||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
export function isBotMentioned(messageText: string, botShipName: string): boolean {
|
||||
if (!messageText || !botShipName) return false;
|
||||
const normalizedBotShip = normalizeShip(botShipName);
|
||||
const escapedShip = normalizedBotShip.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
const mentionPattern = new RegExp(`(^|\\s)${escapedShip}(?=\\s|$)`, "i");
|
||||
return mentionPattern.test(messageText);
|
||||
}
|
||||
|
||||
export function isDmAllowed(senderShip: string, allowlist: string[] | undefined): boolean {
|
||||
if (!allowlist || allowlist.length === 0) return true;
|
||||
const normalizedSender = normalizeShip(senderShip);
|
||||
return allowlist
|
||||
.map((ship) => normalizeShip(ship))
|
||||
.some((ship) => ship === normalizedSender);
|
||||
}
|
||||
|
||||
export function extractMessageText(content: unknown): string {
|
||||
if (!content || !Array.isArray(content)) return "";
|
||||
|
||||
return content
|
||||
.map((block: any) => {
|
||||
if (block.inline && Array.isArray(block.inline)) {
|
||||
return block.inline
|
||||
.map((item: any) => {
|
||||
if (typeof item === "string") return item;
|
||||
if (item && typeof item === "object") {
|
||||
if (item.ship) return item.ship;
|
||||
if (item.break !== undefined) return "\n";
|
||||
if (item.link && item.link.href) return item.link.href;
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.join("");
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.join("\n")
|
||||
.trim();
|
||||
}
|
||||
|
||||
export function isSummarizationRequest(messageText: string): boolean {
|
||||
const patterns = [
|
||||
/summarize\s+(this\s+)?(channel|chat|conversation)/i,
|
||||
/what\s+did\s+i\s+miss/i,
|
||||
/catch\s+me\s+up/i,
|
||||
/channel\s+summary/i,
|
||||
/tldr/i,
|
||||
];
|
||||
return patterns.some((pattern) => pattern.test(messageText));
|
||||
}
|
||||
|
||||
export function formatChangesDate(daysAgo = 5): string {
|
||||
const now = new Date();
|
||||
const targetDate = new Date(now.getTime() - daysAgo * 24 * 60 * 60 * 1000);
|
||||
const year = targetDate.getFullYear();
|
||||
const month = targetDate.getMonth() + 1;
|
||||
const day = targetDate.getDate();
|
||||
return `~${year}.${month}.${day}..20.19.51..9b9d`;
|
||||
}
|
||||
Reference in New Issue
Block a user