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.

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

21 changes: 16 additions & 5 deletions packages/cloud-agents/src/server/router/context-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
environmentRepositoryMappings,
deploymentSettings,
eq,
getUserRoutingPreference,
} from '@roomote/db/server';

import type {
Expand Down Expand Up @@ -119,9 +120,10 @@ export interface GitHubContextParams {
export async function buildSlackRoutingContext(
params: SlackContextParams,
): Promise<RoutingContext> {
const [envs, taskModelSettings] = await Promise.all([
const [envs, taskModelSettings, environmentPreference] = await Promise.all([
getAvailableEnvironments(),
fetchDeploymentTaskModelSettings(),
params.userId ? getUserRoutingPreference(params.userId) : null,
]);

const source: SlackRoutingSource = {
Expand All @@ -138,6 +140,7 @@ export async function buildSlackRoutingContext(
source,
availableEnvironments: envs,
taskModelSettings,
...(environmentPreference ? { environmentPreference } : {}),
...(params.userId
? {
routingActor: {
Expand All @@ -155,9 +158,10 @@ export async function buildSlackRoutingContext(
export async function buildTeamsRoutingContext(
params: TeamsContextParams,
): Promise<RoutingContext> {
const [envs, taskModelSettings] = await Promise.all([
const [envs, taskModelSettings, environmentPreference] = await Promise.all([
getAvailableEnvironments(),
fetchDeploymentTaskModelSettings(),
params.userId ? getUserRoutingPreference(params.userId) : null,
]);

const source: TeamsRoutingSource = {
Expand All @@ -174,6 +178,7 @@ export async function buildTeamsRoutingContext(
source,
availableEnvironments: envs,
taskModelSettings,
...(environmentPreference ? { environmentPreference } : {}),
...(params.userId
? {
routingActor: {
Expand All @@ -191,9 +196,10 @@ export async function buildTeamsRoutingContext(
export async function buildTelegramRoutingContext(
params: TelegramContextParams,
): Promise<RoutingContext> {
const [envs, taskModelSettings] = await Promise.all([
const [envs, taskModelSettings, environmentPreference] = await Promise.all([
getAvailableEnvironments(),
fetchDeploymentTaskModelSettings(),
params.userId ? getUserRoutingPreference(params.userId) : null,
]);

const source: TelegramRoutingSource = {
Expand All @@ -209,6 +215,7 @@ export async function buildTelegramRoutingContext(
source,
availableEnvironments: envs,
taskModelSettings,
...(environmentPreference ? { environmentPreference } : {}),
...(params.userId
? {
routingActor: {
Expand All @@ -224,9 +231,10 @@ export async function buildTelegramRoutingContext(
export async function buildDiscordRoutingContext(
params: DiscordContextParams,
): Promise<RoutingContext> {
const [envs, taskModelSettings] = await Promise.all([
const [envs, taskModelSettings, environmentPreference] = await Promise.all([
getAvailableEnvironments(),
fetchDeploymentTaskModelSettings(),
params.userId ? getUserRoutingPreference(params.userId) : null,
]);
const source: DiscordRoutingSource = {
type: 'discord',
Expand All @@ -241,6 +249,7 @@ export async function buildDiscordRoutingContext(
source,
availableEnvironments: envs,
taskModelSettings,
...(environmentPreference ? { environmentPreference } : {}),
...(params.userId
? {
routingActor: {
Expand All @@ -258,9 +267,10 @@ export async function buildDiscordRoutingContext(
export async function buildLinearRoutingContext(
params: LinearContextParams,
): Promise<RoutingContext> {
const [envs, taskModelSettings] = await Promise.all([
const [envs, taskModelSettings, environmentPreference] = await Promise.all([
getAvailableEnvironments(),
fetchDeploymentTaskModelSettings(),
params.userId ? getUserRoutingPreference(params.userId) : null,
]);

const source: LinearRoutingSource = {
Expand All @@ -280,6 +290,7 @@ export async function buildLinearRoutingContext(
source,
availableEnvironments: envs,
taskModelSettings,
...(environmentPreference ? { environmentPreference } : {}),
...(params.userId
? {
routingActor: {
Expand Down
40 changes: 39 additions & 1 deletion packages/cloud-agents/src/server/router/follow-up-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { recordUserRoutingPreference } from '@roomote/db/server';

import { classifyFollowUp, routeTask } from './router-service';
import { MAX_THREAD_MESSAGES } from './types';
import type { RoutingContext, RoutingDecision } from './types';

function getSuggestedEnvironmentId(
suggestion: RoutingContext['previousSuggestion'] | null,
): string | null {
const value = suggestion?.workspaceValue;

if (!value) {
return null;
}

return value.startsWith('env:') ? value.slice('env:'.length) : value;
}

type RoutingFollowUpResolution =
| { intent: 'confirm' }
| { intent: 'cancel' }
Expand Down Expand Up @@ -68,8 +82,32 @@ export async function resolveRoutingFollowUp(input: {
...(input.suggestion ? { previousSuggestion: input.suggestion } : {}),
};

const routingDecision = await routeTask(routingContext);
const suggestedEnvironmentId = getSuggestedEnvironmentId(input.suggestion);

if (
routingDecision.status === 'routed' &&
routingDecision.result.workspace.type === 'environment' &&
!classification.isFallback &&
input.userId &&
suggestedEnvironmentId !== routingDecision.result.workspace.id
) {
try {
await recordUserRoutingPreference({
userId: input.userId,
environmentId: routingDecision.result.workspace.id,
});
} catch (error) {
console.warn(
`[RoutingCorrection] Failed to record user routing preference: ${
error instanceof Error ? error.message : String(error)
}`,
);
}
}

return {
intent: 'correct',
routingDecision: await routeTask(routingContext),
routingDecision,
};
}
Loading
Loading