Skip to content

Commit

Permalink
[editor] Error Notification for Name/Description Exception (#708)
Browse files Browse the repository at this point in the history
# [editor] Error Notification for Name/Description Exception

Missed adding the try/catch when implementing the callback, so just add
them now to be consistent

---
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with
[ReviewStack](https://reviewstack.dev/lastmile-ai/aiconfig/pull/708).
* __->__ #708
* #707
  • Loading branch information
rholinshead authored Jan 2, 2024
2 parents 505a3ad + 46a1bab commit 901e2b0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
17 changes: 11 additions & 6 deletions python/src/aiconfig/editor/client/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EditorContainer, {
AIConfigCallbacks,
} from "./components/EditorContainer";
import { Flex, Loader, MantineProvider } from "@mantine/core";
import { AIConfig, ModelMetadata, Prompt } from "aiconfig";
import { AIConfig, InferenceSettings, Prompt } from "aiconfig";
import { useCallback, useEffect, useMemo, useState } from "react";
import { ufetch } from "ufetch";
import { ROUTE_TABLE } from "./utils/api";
Expand Down Expand Up @@ -73,11 +73,16 @@ export default function Editor() {
);

const updateModel = useCallback(
async (_promptName?: string, _modelData?: string | ModelMetadata) => {
// return await ufetch.post(ROUTE_TABLE.UPDATE_MODEL,
// prompt_name: promptName,
// model_data: modelData,
// });
async (value: {
modelName?: string;
settings?: InferenceSettings;
promptName?: string;
}) => {
return await ufetch.post(ROUTE_TABLE.UPDATE_MODEL, {
model_name: value.modelName,
settings: value.settings,
prompt_name: value.promptName,
});
},
[]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Container, Button, createStyles, Stack, Flex } from "@mantine/core";
import { showNotification } from "@mantine/notifications";
import {
AIConfig,
InferenceSettings,
JSONObject,
ModelMetadata,
Prompt,
PromptInput,
} from "aiconfig";
Expand All @@ -27,6 +27,7 @@ import GlobalParametersContainer from "./GlobalParametersContainer";
import AIConfigContext from "./AIConfigContext";
import ConfigNameDescription from "./ConfigNameDescription";
import { DEBOUNCE_MS } from "../utils/constants";
import { getPromptModelName } from "../utils/promptUtils";

type Props = {
aiconfig: AIConfig;
Expand All @@ -45,10 +46,11 @@ export type AIConfigCallbacks = {
save: (aiconfig: AIConfig) => Promise<void>;
setConfigDescription: (description: string) => Promise<void>;
setConfigName: (name: string) => Promise<void>;
updateModel: (
promptName?: string,
modelData?: string | ModelMetadata
) => Promise<void /*{ aiconfig: AIConfig }*/>;
updateModel: (value: {
modelName?: string;
settings?: InferenceSettings;
promptName?: string;
}) => Promise<{ aiconfig: AIConfig }>;
updatePrompt: (
promptName: string,
promptData: Prompt
Expand Down Expand Up @@ -184,8 +186,11 @@ export default function EditorContainer({
const debouncedUpdateModel = useMemo(
() =>
debounce(
(promptName?: string, modelMetadata?: string | ModelMetadata) =>
updateModelCallback(promptName, modelMetadata),
(value: {
modelName?: string;
settings?: InferenceSettings;
promptName?: string;
}) => updateModelCallback(value),
DEBOUNCE_MS
),
[updateModelCallback]
Expand All @@ -198,9 +203,31 @@ export default function EditorContainer({
id: promptId,
modelSettings: newModelSettings,
});
// TODO: Call server-side endpoint to update model

try {
const statePrompt = getPrompt(stateRef.current, promptId);
if (!statePrompt) {
throw new Error(`Could not find prompt with id ${promptId}`);
}
const modelName = getPromptModelName(statePrompt);
if (!modelName) {
throw new Error(`Could not find model name for prompt ${promptId}`);
}
await debouncedUpdateModel({
modelName,
settings: newModelSettings as InferenceSettings,
promptName: statePrompt.name,
});
} catch (err: unknown) {
const message = err instanceof Error ? err.message : null;
showNotification({
title: "Error updating prompt model settings",
message,
color: "red",
});
}
},
[dispatch]
[debouncedUpdateModel, dispatch]
);

const onUpdatePromptModel = useCallback(
Expand All @@ -216,17 +243,11 @@ export default function EditorContainer({
if (!statePrompt) {
throw new Error(`Could not find prompt with id ${promptId}`);
}
const prompt = clientPromptToAIConfigPrompt(statePrompt);
const currentModel = prompt.metadata?.model;
let modelData: string | ModelMetadata | undefined = newModel;
if (newModel && currentModel && typeof currentModel !== "string") {
modelData = {
...currentModel,
name: newModel,
};
}

await debouncedUpdateModel(prompt.name, modelData);
await debouncedUpdateModel({
modelName: newModel,
promptName: statePrompt.name,
});

// TODO: Consolidate
} catch (err: unknown) {
Expand Down Expand Up @@ -388,7 +409,16 @@ export default function EditorContainer({
type: "SET_NAME",
name,
});
await debouncedSetName(name);
try {
await debouncedSetName(name);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : null;
showNotification({
title: "Error setting config name",
message,
color: "red",
});
}
},
[debouncedSetName]
);
Expand All @@ -409,7 +439,17 @@ export default function EditorContainer({
type: "SET_DESCRIPTION",
description,
});
await debouncedSetDescription(description);

try {
await debouncedSetDescription(description);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : null;
showNotification({
title: "Error setting config description",
message,
color: "red",
});
}
},
[debouncedSetDescription]
);
Expand Down

0 comments on commit 901e2b0

Please sign in to comment.