Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[editor] Run Prompt (non-streaming) #667

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type AIConfigCallbacks = {
) => Promise<{ aiconfig: AIConfig }>;
deletePrompt: (promptName: string) => Promise<void>;
getModels: (search: string) => Promise<string[]>;
runPrompt: (promptName: string) => Promise<void>;
runPrompt: (promptName: string) => Promise<{ aiconfig: AIConfig }>;
save: (aiconfig: AIConfig) => Promise<void>;
updateModel: (
promptName?: string,
Expand Down Expand Up @@ -307,9 +307,22 @@ export default function EditorContainer({

const onRunPrompt = useCallback(
async (promptId: string) => {
const promptName = getPrompt(stateRef.current, promptId)!.name;
const action: AIConfigReducerAction = {
type: "RUN_PROMPT",
id: promptId,
};

dispatch(action);

try {
await callbacks.runPrompt(promptName);
const promptName = getPrompt(stateRef.current, promptId)!.name;
const serverConfigRes = await callbacks.runPrompt(promptName);

dispatch({
type: "CONSOLIDATE_AICONFIG",
action,
config: serverConfigRes.aiconfig,
});
} catch (err: any) {
showNotification({
title: "Error running prompt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type AIConfigReducerAction =
export type MutateAIConfigAction =
| AddPromptAction
| DeletePromptAction
| RunPromptAction
| UpdatePromptInputAction
| UpdatePromptNameAction
| UpdatePromptModelAction
Expand All @@ -32,6 +33,11 @@ export type DeletePromptAction = {
id: string;
};

export type RunPromptAction = {
type: "RUN_PROMPT";
id: string;
};

export type UpdatePromptInputAction = {
type: "UPDATE_PROMPT_INPUT";
id: string;
Expand Down Expand Up @@ -131,6 +137,22 @@ function reduceConsolidateAIConfig(
consolidatePrompt
);
}
case "RUN_PROMPT": {
return reduceReplacePrompt(state, action.id, (prompt) => {
const responsePrompt = responseConfig.prompts.find(
(resPrompt) => resPrompt.name === prompt.name
);

return {
...prompt,
_ui: {
...prompt._ui,
isRunning: false,
},
outputs: responsePrompt!.outputs,
};
});
}
case "UPDATE_PROMPT_INPUT": {
return reduceReplacePrompt(state, action.id, consolidatePrompt);
}
Expand All @@ -154,6 +176,15 @@ export default function aiconfigReducer(
prompts: state.prompts.filter((prompt) => prompt._ui.id !== action.id),
};
}
case "RUN_PROMPT": {
return reduceReplacePrompt(state, action.id, (prompt) => ({
...prompt,
_ui: {
...prompt._ui,
isRunning: true,
},
}));
}
case "UPDATE_PROMPT_INPUT": {
return reduceReplaceInput(state, action.id, () => action.input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export default memo(function PromptActionBar({
<IconClearAll />
</ActionIcon>
</Flex>
<RunPromptButton runPrompt={onRunPrompt} size="compact" />
<RunPromptButton
runPrompt={onRunPrompt}
isRunning={prompt._ui.isRunning}
size="compact"
/>
</Flex>
)}
</Flex>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button, createStyles, Flex, Text } from "@mantine/core";
import { IconPlayerPlayFilled } from "@tabler/icons-react";
import { Button, createStyles, Flex, Loader, Text } from "@mantine/core";
import { IconPlayerPlayFilled, IconPlayerStop } from "@tabler/icons-react";
import { memo } from "react";

type Props = {
isRunning?: boolean;
runPrompt: () => Promise<void>;
size: "compact" | "full";
};
Expand All @@ -15,18 +16,36 @@ const useStyles = createStyles(() => ({
},
}));

export default memo(function RunPromptButton({ runPrompt, size }: Props) {
export default memo(function RunPromptButton({
runPrompt,
size,
isRunning = false,
}: Props) {
const { classes } = useStyles();
return (
<Button
onClick={runPrompt}
disabled={isRunning}
p="xs"
size="xs"
fullWidth={size === "full"}
className={classes.executeButton}
>
<IconPlayerPlayFilled size="16" />
{size === "full" && <Text ml="0.5em">Run</Text>}
{isRunning ? (
<div>
<Loader
style={{ position: "absolute", top: 5, left: 8 }}
size="xs"
color="white"
/>
<IconPlayerStop fill="white" size={14} />
</div>
) : (
<>
<IconPlayerPlayFilled size="16" />
{size === "full" && <Text ml="0.5em">Run</Text>}
</>
)}
</Button>
);
});
1 change: 1 addition & 0 deletions python/src/aiconfig/editor/client/src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type EditorFile = {
export type ClientPrompt = Prompt & {
_ui: {
id: string;
isRunning?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

};
};

Expand Down