Skip to content
Draft
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

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

45 changes: 43 additions & 2 deletions packages/sdk/src/server/automations/custom-automations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { enqueueFastAgentParentEvent } from '../lib/fast-agent-parent-event-queu
import { recordFastAgentConversationMessage } from '../lib/fast-agent-provider-message';
import {
canStartAgentMailConversationWithUser,
listAvailableAgentMailOutboundIdentities,
prepareAgentMailConversation,
} from '../lib/agentmail/outbound';

Expand All @@ -82,7 +83,7 @@ const PROVIDER_LABELS: Record<
};

type CustomAutomationDestination =
| ResolvedAutomationDestination
| (ResolvedAutomationDestination & { isDirectMessage?: boolean })
| {
provider: 'email';
userId: string;
Expand Down Expand Up @@ -194,6 +195,42 @@ async function resolveDestination(
};
}

async function resolveOwnerFallbackDestination(
ownerUserId: string,
): Promise<CustomAutomationDestination | null> {
for (const provider of await listConnectedCommunicationProviders()) {
try {
const destination = await findUserDirectMessageDestination(
Comment thread
roomote-roomote[bot] marked this conversation as resolved.
provider,
ownerUserId,
);
if (destination) {
return {
provider,
...destination,
source: 'automation_target',
isDirectMessage: true,
};
}
} catch (error) {
console.warn(
`${LOG_PREFIX} Failed to resolve owner fallback DM on ${provider}: ${error instanceof Error ? error.message : String(error)}`,
);
}
}

const [identity] =
await listAvailableAgentMailOutboundIdentities(ownerUserId);
return identity
? {
provider: 'email',
userId: ownerUserId,
identityId: identity.id,
source: 'automation_target',
}
: null;
}

function isFastDeliveryTarget(target: AutomationTarget): boolean {
return isAutomationDestinationTarget(target);
}
Expand Down Expand Up @@ -279,7 +316,7 @@ async function buildFastAutomationConversation(params: {
if (!provider) {
throw new Error('Discord is not connected.');
}
if (target?.targetKind === 'discord_user') {
if (target?.targetKind === 'discord_user' || destination.isDirectMessage) {
const posted = await provider.postMessage({
channelId: destination.channelId,
text: `${automation.name} is running.`,
Expand Down Expand Up @@ -692,6 +729,10 @@ async function launchCustomAutomationRow(
});
return result;
}
} else if (automation.createdByUserId) {
destination = await resolveOwnerFallbackDestination(
automation.createdByUserId,
);
}

// The short claim fence prevents concurrent launchers from double-launching
Expand Down
Loading