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
27 changes: 26 additions & 1 deletion app/src/lib/bot-agents/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { queryOptions, useQuery } from "@tanstack/react-query";
import {
type QueryClient,
queryOptions,
useQuery,
} from "@tanstack/react-query";
import {
getBotAgent,
getBotDelegation,
Expand Down Expand Up @@ -107,6 +111,27 @@ export function useBotTakesModelChoice(
return delegation.isSuccess && delegation.data.mode !== "orchestrate";
}

/**
* Why a conversation with this Bot takes no model pick, or null when it takes one. The rule of
* `useBotTakesModelChoice`, asked once: the home composer's pick is made before OpenBot has picked
* the Bot, so it is asked when the Bot is known, just before its conversation is made.
*/
export async function whyNoModelChoice(
queryClient: QueryClient,
bot: { id: string; runsOnOpenBotModel: boolean },
): Promise<"own-agent" | "hands-to-agents" | null> {
if (!bot.runsOnOpenBotModel) return "own-agent";
try {
const delegation = await queryClient.fetchQuery(
botDelegationQueryOptions(bot.id),
);
return delegation.mode === "orchestrate" ? "hands-to-agents" : null;
} catch {
// As the hook reads it: a Bot whose answer could not be read takes the pick.
return null;
}
}

/** The host of an agent's address, for a row that says where it runs. Null when it can't be read. */
export function hostOf(endpoint: string | null): string | null {
if (!endpoint) return null;
Expand Down
95 changes: 90 additions & 5 deletions app/src/routes/_authed/_app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ import {
toAgentOptions,
} from "@/components/channels/composer";
import { heldFileOf } from "@/components/channels/composer/attachments";
import { ModelPicker } from "@/components/channels/composer/model-picker";
import { WorkingNow } from "@/components/channels/working-now";
import { BusinessLink } from "@/components/edition/business-link";
import { SidebarToggleBar } from "@/components/layout/sidebar-toggle";
import { StarterSuggestions } from "@/components/starter-tour/starter-suggestions";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { Skeleton } from "@/components/ui/skeleton";
import { toast } from "@/components/ui/toast";
import { defaultAgentProfile } from "@/lib/agents/default-agent";
import { agentListQueryOptions, isSharedWithYou } from "@/lib/agents/queries";
import {
type AgentProfile,
agentListQueryOptions,
agentQueryOptions,
isSharedWithYou,
} from "@/lib/agents/queries";
import { firstConversationGreeting } from "@/lib/auth/greeting";
import { currentUserQueryOptions } from "@/lib/auth/queries";
import { whyNoModelChoice } from "@/lib/bot-agents/queries";
import {
beginPendingStart,
endPendingStart,
Expand All @@ -42,13 +50,40 @@ import {
import { useStartChannel } from "@/lib/channels/start";
import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries";
import { editionOf } from "@/lib/edition-guard";
import { takeInvitationLink } from "@/lib/organizations/invitation-link";
import {
type ConversationModel,
modelCatalogueQueryOptions,
} from "@/lib/models/queries";
import { useDraftModel } from "@/lib/models/run-model";
import { newId } from "@/lib/new-id";
import { takeInvitationLink } from "@/lib/organizations/invitation-link";
import { useAnyBotSkillCommands } from "@/lib/plugins/skill-commands";
import { queryClient } from "@/query-client";

/** How many Bots the home page shows before pointing to the rest. Two rows of two. */
const EXPLORE_SHOWN = 4;

/**
* Said when the Bot a message went to takes no model pick, so the pick was not saved. The person
* picked before OpenBot picked the Bot, and a pick that silently does nothing is a surprise. The
* action opens that Bot's Agents page, where what it runs on is shown.
*/
function sayModelNotUsed(
bot: AgentProfile,
why: "own-agent" | "hands-to-agents",
openAgents: () => void,
) {
toast.add({
id: "home-model-not-used",
type: "info",
title:
why === "hands-to-agents"
? `${bot.name} hands work to agents, so it uses the model set on its Agents page.`
: `${bot.name} runs on its own agent, so it doesn't use the model you picked.`,
actionProps: { children: "Open Agents", onClick: openAgents },
});
}

export const Route = createFileRoute("/_authed/_app/")({
/*
* `ask`: words to start the box with, as Teach a Bot's Try it hands over a task a Bot just learned. Only put in the
Expand Down Expand Up @@ -132,6 +167,42 @@ function RouteComponent() {
agents,
agents?.find((agent) => agent.visibility === "public"),
);
/*
* The model and thinking effort to run on, picked before OpenBot has picked the Bot. It starts on
* the catalogue's default and is saved with the conversation the Bot answers in, before the first
* message runs, as `/channel/new` saves it.
*/
const models = useQuery(modelCatalogueQueryOptions());
const draftModel = useDraftModel(models.data);

/**
* The pick to save with a new conversation with `agentId`, and the line to say once it has started
* when that Bot takes no pick (`whyNoModelChoice`). Nothing to save, and nothing to say, while the
* pick is the default. A Bot that could not be read takes the pick, as the hook reads it.
*/
const pickFor = async (
agentId: string,
): Promise<{ pick: ConversationModel | null; notUsed?: () => void }> => {
const pick = draftModel.requested;
if (!pick) return { pick: null };
const bot =
agents?.find((agent) => agent.id === agentId) ??
(await queryClient
.fetchQuery(agentQueryOptions(agentId))
.catch(() => undefined));
const why = bot ? await whyNoModelChoice(queryClient, bot) : null;
if (!bot || !why) return { pick };
return {
pick: null,
notUsed: () =>
sayModelNotUsed(bot, why, () => {
void router.navigate({
params: { agentId: bot.id },
to: "/bots/$agentId/agents",
});
}),
};
};

/**
* Pick the Bot and make the channel behind the placeholder `pendingId`.
Expand All @@ -152,13 +223,15 @@ function RouteComponent() {
if (draft.agentId) {
// Recorded and started as one sequence, shared with `/channel/new`: the person already
// decided, and the trail has to say so wherever they decided it.
const { pick, notUsed } = await pickFor(draft.agentId);
await startChosen(
draft.agentId,
draft.text,
undefined,
pick,
pendingId,
draft.attachments,
);
notUsed?.();
return;
}
let agentId: string | undefined;
Expand All @@ -177,7 +250,9 @@ function RouteComponent() {
throw new Error(
"There's no Bot to send this to. Try again in a moment.",
);
await start(agentId, draft.text, undefined, pendingId, draft.attachments);
const { pick, notUsed } = await pickFor(agentId);
await start(agentId, draft.text, pick, pendingId, draft.attachments);
notUsed?.();
} catch (caught) {
stashFailedStart({
text: draft.text,
Expand Down Expand Up @@ -226,7 +301,9 @@ function RouteComponent() {
disabled={pending}
onSelect={async (message) => {
if (!fallback) return;
await startChosen(fallback.id, message);
const { pick, notUsed } = await pickFor(fallback.id);
await startChosen(fallback.id, message, pick);
notUsed?.();
}}
/>
<Composer
Expand All @@ -239,6 +316,14 @@ function RouteComponent() {
key={restores}
// Files wait in the box until the message has a conversation to go to; see `start`.
holdsFilesUntilSent
modelPicker={
<ModelPicker
catalogue={models.data}
disabled={!fallback}
onChange={draftModel.choose}
value={draftModel.value}
/>
}
{...(restored
? {
initialValue: restored.text,
Expand Down
Loading
Loading