Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/sdk/src/server/lib/fast-agent-parent-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ import {
type TelegramLiveTaskStreamProvider,
} from './telegram-live-task-stream';
import { createFastAgentTypingActivity } from './fast-agent-typing-activity';
import { createFastAgentTelegramActivity } from './fast-agent-telegram-activity';
import {
createFastAgentTelegramActivity,
runWithFastAgentTelegramActivityReassertion,
} from './fast-agent-telegram-activity';
import { findTeamsConversationRoute } from '../automations/destination';
import {
isFastAgentManagedTelegramTopic,
Expand Down Expand Up @@ -1903,20 +1906,25 @@ async function createTelegramFastAgentParentTurn(
sessionId: session.id,
footerContext: params.footerContext,
});
const launchTask = createFastAgentCommunicationTaskLauncher({
userId: actorUserId,
conversation,
telegramLiveTaskProvider: provider,
automation: await resolveFastAutomationLaunchContext({
event: params.event,
conversation,
}),
});
return {
userId: actorUserId,
conversation,
adapter: {
activity,
launchTask: createFastAgentCommunicationTaskLauncher({
userId: actorUserId,
conversation,
telegramLiveTaskProvider: provider,
automation: await resolveFastAutomationLaunchContext({
event: params.event,
conversation,
}),
}),
launchTask: async (input) => {
return runWithFastAgentTelegramActivityReassertion(activity, () =>
launchTask(input),
);
},
replaceReply: async (handle, reply) => {
const result = await replaceReply(handle, reply);
activity.reassert();
Expand Down
20 changes: 14 additions & 6 deletions packages/sdk/src/server/lib/fast-agent-surface-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ import {
} from './source-control-fast-delivery';
import { buildFastAgentArtifactCreator } from './artifacts/fast-agent-artifact-creator';
import { createFastAgentTypingActivity } from './fast-agent-typing-activity';
import { createFastAgentTelegramActivity } from './fast-agent-telegram-activity';
import {
createFastAgentTelegramActivity,
runWithFastAgentTelegramActivityReassertion,
} from './fast-agent-telegram-activity';
import { addFastAgentTelegramTopicTitleSync } from './fast-agent-telegram-title-sync';

const SLACK_QUOTE_MAX_LENGTH = 100;
Expand Down Expand Up @@ -640,6 +643,11 @@ export async function buildFastAgentSurfaceReplyDelivery(params: {
sessionId: session.id,
footerContext,
});
const launchTask = createFastAgentCommunicationTaskLauncher({
userId: params.userId,
conversation,
telegramLiveTaskProvider: provider,
});
const postReply: FastAgentTurnAdapter['postReply'] = async ({
message,
}) => {
Expand Down Expand Up @@ -679,11 +687,11 @@ export async function buildFastAgentSurfaceReplyDelivery(params: {
}
: {}),
createArtifact,
launchTask: createFastAgentCommunicationTaskLauncher({
userId: params.userId,
conversation,
telegramLiveTaskProvider: provider,
}),
launchTask: async (input) => {
return runWithFastAgentTelegramActivityReassertion(activity, () =>
launchTask(input),
);
},
postReply,
replaceReply: async (handle, reply) => {
const result = await replaceReply(handle, reply);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/sdk/src/server/lib/fast-agent-telegram-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ export const FAST_AGENT_TELEGRAM_PROCESSING_DELAY_MS = 300;
export const FAST_AGENT_TELEGRAM_STREAM_INTERVAL_MS = 800;
const FAST_AGENT_TELEGRAM_THINKING_TEXT = 'Roomote is working...';

export async function runWithFastAgentTelegramActivityReassertion<T>(
activity: { reassert: () => void },
operation: () => Promise<T>,
): Promise<T> {
try {
return await operation();
} finally {
activity.reassert();
}
}

function isTelegramPrivateChatId(channelId: string): boolean {
const parsed = Number(channelId);
return Number.isSafeInteger(parsed) && parsed > 0;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions packages/sdk/src/server/lib/fast-agent-telegram-title-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export async function syncFastAgentTelegramTopicTitleBestEffort(input: {
category?: TaskTitleCategory | null;
titleChanged?: boolean;
resolveSession: () => Promise<FastAgentConversationRecord | null>;
}): Promise<void> {
}): Promise<boolean> {
let updated = false;
try {
for (let attempt = 0; attempt < 2; attempt += 1) {
const session = await input.resolveSession();
Expand All @@ -51,7 +52,7 @@ export async function syncFastAgentTelegramTopicTitleBestEffort(input: {
session.conversation.replyTarget.channelId !== input.channelId ||
session.conversation.replyTarget.threadId !== input.threadId
) {
return;
return updated;
}

const title = buildCommunicationTaskThreadName(session.title);
Expand All @@ -63,32 +64,34 @@ export async function syncFastAgentTelegramTopicTitleBestEffort(input: {
.catch(() => undefined)
: undefined;
if (input.titleChanged === false && !iconCustomEmojiId) {
return;
return updated;
}
await input.provider.editForumTopic({
channelId: input.channelId,
threadId: input.threadId,
...(input.titleChanged === false ? {} : { name: title }),
...(iconCustomEmojiId ? { iconCustomEmojiId } : {}),
});
updated = true;

if (input.titleChanged === false) {
return;
return updated;
}

const latest = await input.resolveSession();
if (
!latest?.title ||
buildCommunicationTaskThreadName(latest.title) === title
) {
return;
return updated;
}
}
} catch (error) {
console.warn(
`[Fast Agent] Failed to sync Telegram topic title for session ${input.sessionId}: ${error instanceof Error ? error.message : String(error)}`,
);
}
return updated;
}

export function addFastAgentTelegramTopicTitleSync<
Expand Down Expand Up @@ -126,13 +129,14 @@ export function addFastAgentTelegramTopicTitleSync<
lastRequestedTitle = title;
lastRequestedCategory = category;
lastRequestedTitleChanged = titleChanged;
titleUpdate = titleUpdate.then(() =>
syncFastAgentTelegramTopicTitleBestEffort({
titleUpdate = titleUpdate.then(async () => {
const updated = await syncFastAgentTelegramTopicTitleBestEffort({
...input,
category,
titleChanged,
}),
);
});
if (updated) input.activity.reassert();
});
},
async dispose() {
await Promise.all([input.activity.dispose(), titleUpdate]);
Expand Down
Loading