Skip to content

Commit

Permalink
refactor: use agent mode in run task logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Sep 17, 2024
1 parent d37b96b commit 1cd3600
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
26 changes: 8 additions & 18 deletions src/common/settings/ModelDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { useAppState } from "../../state/store";
import {
SupportedModels,
DisplayName,
isOpenAIModel,
isAnthropicModel,
AgentMode,
hasVisionSupport,
isValidModelSettings,
} from "../../helpers/aiSdkUtils";
import { enumValues } from "../../helpers/utils";

Expand All @@ -22,19 +19,6 @@ const ModelDropdown = () => {
anthropicKey: state.settings.anthropicKey,
}));

const isModelSupported = (model: SupportedModels) => {
if (agentMode === AgentMode.VisionEnhanced && !hasVisionSupport(model)) {
return false;
}
if (isOpenAIModel(model)) {
return !!openAIKey;
}
if (isAnthropicModel(model)) {
return !!anthropicKey;
}
return false;
};

return (
<Select
id="model-select"
Expand All @@ -44,7 +28,13 @@ const ModelDropdown = () => {
}
>
{enumValues(SupportedModels).map((model) => (
<option key={model} value={model} disabled={!isModelSupported(model)}>
<option
key={model}
value={model}
disabled={
!isValidModelSettings(model, agentMode, openAIKey, anthropicKey)
}
>
{DisplayName[model]}
</option>
))}
Expand Down
24 changes: 19 additions & 5 deletions src/state/currentTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { MyStateCreator } from "./store";
import buildAnnotatedScreenshots from "../helpers/buildAnnotatedScreenshots";
import { voiceControl } from "../helpers/voiceControl";
import { fetchKnowledge, type Knowledge } from "../helpers/knowledge";
import { hasVisionSupport } from "../helpers/aiSdkUtils";
import { isValidModelSettings, AgentMode } from "../helpers/aiSdkUtils";

export type TaskHistoryEntry = {
prompt: string;
Expand Down Expand Up @@ -77,6 +77,22 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
runTask: async (onError) => {
const voiceMode = get().settings.voiceMode;

if (
!isValidModelSettings(
get().settings.selectedModel,
get().settings.agentMode,
get().settings.openAIKey,
get().settings.anthropicKey,
)
) {
onError(
"The current model settings are not valid. Please verify your API keys, and note that some models are not compatible with certain agent modes.",
);
return;
}

const agentMode = get().settings.agentMode;

const wasStopped = () => get().currentTask.status !== "running";
const setActionStatus = (status: CurrentTaskSlice["actionStatus"]) => {
set((state) => {
Expand Down Expand Up @@ -124,8 +140,6 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
);
}

const isVisionModel = hasVisionSupport(get().settings.selectedModel);

const performAction = async (
query: QueryResult,
): Promise<boolean> => {
Expand Down Expand Up @@ -161,7 +175,7 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
) {
return false;
}
if (isVisionModel) {
if (agentMode === AgentMode.VisionEnhanced) {
await operateTool(tabId, query.action.operation);
} else {
await operateToolWithSimpliedDom(tabId, query.action.operation);
Expand Down Expand Up @@ -198,7 +212,7 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
.currentTask.history.map((entry) => entry.action)
.filter(truthyFilter);

if (isVisionModel) {
if (agentMode === AgentMode.VisionEnhanced) {
setActionStatus("fetching-knoweldge");
const url = new URL(activeTab.url ?? "");
const customKnowledgeBase = get().settings.customKnowledgeBase;
Expand Down

0 comments on commit 1cd3600

Please sign in to comment.