Skip to content

Commit

Permalink
feat: database_schema support in dust apps (#2624)
Browse files Browse the repository at this point in the history
* Add database block

* add io-ts-types

* add front support for database schema block

* rm debug log

* improve copy

* nits

* extra nit
  • Loading branch information
fontanierh authored Nov 22, 2023
1 parent aa36405 commit 1934f72
Show file tree
Hide file tree
Showing 13 changed files with 509 additions and 31 deletions.
3 changes: 3 additions & 0 deletions core/src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub enum BlockType {
Browser,
While,
End,
#[serde(rename = "database_schema")]
DatabaseSchema,
Database,
}
Expand Down Expand Up @@ -117,6 +118,8 @@ impl FromStr for BlockType {
"browser" => Ok(BlockType::Browser),
"while" => Ok(BlockType::While),
"end" => Ok(BlockType::End),
"database_schema" => Ok(BlockType::DatabaseSchema),
"database" => Ok(BlockType::Database),
_ => Err(ParseError::with_message("Unknown BlockType"))?,
}
}
Expand Down
22 changes: 14 additions & 8 deletions front/components/app/NewBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export default function NewBlock({
}) {
const containsInput =
spec.filter((block) => block.type == "input").length > 0;
const blocks = [
const blocks: {
type: BlockType | "map_reduce" | "while_end";
typeNames: BlockType[];
name: string;
description: string;
}[] = [
{
type: "llm",
typeNames: ["llm"],
Expand Down Expand Up @@ -90,12 +95,13 @@ export default function NewBlock({
name: "While End",
description: "Loop over a set of blocks until a condition is met.",
},
] as {
type: BlockType | "map_reduce" | "while_end";
typeNames: BlockType[];
name: string;
description: string;
}[];
{
type: "database_schema",
typeNames: ["database_schema"],
name: "Database Schema",
description: "Retrieve the schema of a database.",
},
];

if (!containsInput) {
blocks.splice(0, 0, {
Expand Down Expand Up @@ -164,7 +170,7 @@ export default function NewBlock({
))}
</div>
</div>
<div className="col-span-8 pr-2 text-sm text-gray-700 sm:col-span-9 sm:pl-3">
<div className="col-span-8 pr-2 text-sm text-gray-700 sm:col-span-9 sm:pl-6">
{block.description}
</div>
</div>
Expand Down
39 changes: 24 additions & 15 deletions front/components/app/SpecRunView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Chat from "./blocks/Chat";
import Code from "./blocks/Code";
import Curl from "./blocks/Curl";
import Data from "./blocks/Data";
import DatabaseSchema from "./blocks/DatabaseSchema";
import DataSource from "./blocks/DataSource";
import Input from "./blocks/Input";
import LLM from "./blocks/LLM";
Expand Down Expand Up @@ -94,7 +95,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "data":
return (
Expand All @@ -115,7 +115,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "llm":
return (
Expand All @@ -136,7 +135,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "chat":
return (
Expand All @@ -157,7 +155,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "code":
return (
Expand All @@ -178,7 +175,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "data_source":
return (
Expand All @@ -199,7 +195,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "map":
return (
Expand All @@ -220,7 +215,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "reduce":
return (
Expand Down Expand Up @@ -261,7 +255,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "end":
return (
Expand Down Expand Up @@ -302,7 +295,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "curl":
return (
Expand All @@ -323,7 +315,6 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

case "browser":
return (
Expand All @@ -344,15 +335,33 @@ export default function SpecRunView({
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);
break;

default:
case "database_schema":
return (
<DatabaseSchema
key={idx}
block={block}
owner={owner}
app={app}
spec={spec}
run={run}
status={status}
running={runRequested || run?.status.run == "running"}
readOnly={readOnly}
onBlockUpdate={(block) => handleSetBlock(idx, block)}
onBlockDelete={() => handleDeleteBlock(idx)}
onBlockUp={() => handleMoveBlockUp(idx)}
onBlockDown={() => handleMoveBlockDown(idx)}
onBlockNew={(blockType) => handleNewBlock(idx, blockType)}
/>
);

default:
return ((t: never) => (
<div key={idx} className="flex flex-row px-4 py-4 text-sm">
Unknown block type: {block.type}
Unknown block type: {t}
</div>
);
break;
))(block.type);
}
})}
</div>
Expand Down
117 changes: 117 additions & 0 deletions front/components/app/blocks/DatabaseSchema.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import "@uiw/react-textarea-code-editor/dist.css";

import DataSourcePicker from "@app/components/data_source/DataSourcePicker";
import DatabasePicker from "@app/components/database/DatabasePicker";
import { shallowBlockClone } from "@app/lib/utils";
import { SpecificationBlockType, SpecificationType } from "@app/types/app";
import { AppType } from "@app/types/app";
import { BlockType } from "@app/types/run";
import { RunType } from "@app/types/run";
import { WorkspaceType } from "@app/types/user";

import Block from "./Block";

export default function DatabaseSchema({
owner,
app,
spec,
run,
block,
status,
running,
readOnly,
onBlockUpdate,
onBlockDelete,
onBlockUp,
onBlockDown,
onBlockNew,
}: React.PropsWithChildren<{
owner: WorkspaceType;
app: AppType;
spec: SpecificationType;
run: RunType | null;
block: SpecificationBlockType;
status: any;
running: boolean;
readOnly: boolean;
onBlockUpdate: (block: SpecificationBlockType) => void;
onBlockDelete: () => void;
onBlockUp: () => void;
onBlockDown: () => void;
onBlockNew: (blockType: BlockType | "map_reduce" | "while_end") => void;
}>) {
return (
<Block
owner={owner}
app={app}
spec={spec}
run={run}
block={block}
status={status}
running={running}
readOnly={readOnly}
canUseCache={false}
onBlockUpdate={onBlockUpdate}
onBlockDelete={onBlockDelete}
onBlockUp={onBlockUp}
onBlockDown={onBlockDown}
onBlockNew={onBlockNew}
>
<div className="mx-4 flex w-full flex-col flex-col gap-2 pt-4 xl:flex-row">
<div className="flex flex-col xl:flex-row xl:space-x-2">
<div className="mr-1 flex flex-initial text-sm font-medium leading-8 text-gray-700">
Database:
</div>
<div className="mr-2 flex flex-initial flex-row items-center space-x-1 text-sm font-medium leading-8 text-gray-700">
<DataSourcePicker
owner={owner}
readOnly={readOnly}
currentDataSources={
block.config.database?.data_source_id
? [
{
data_source_id: block.config.database.data_source_id,
workspace_id: block.config.database.workspace_id,
},
]
: []
}
onDataSourcesUpdate={(dataSources) => {
if (dataSources.length === 0) {
return;
}
const ds = dataSources[0];
const b = shallowBlockClone(block);
b.config.database = {
workspace_id: ds.workspace_id,
data_source_id: ds.data_source_id,
};
onBlockUpdate(b);
}}
/>
</div>
</div>
{block.config.database?.data_source_id && (
<div className="flex flex-col xl:flex-row xl:space-x-2">
<div className="mr-2 flex flex-initial flex-row items-center space-x-1 text-sm font-medium leading-8 text-gray-700">
<DatabasePicker
owner={owner}
dataSource={{
workspace_id: block.config.database.workspace_id,
data_source_id: block.config.database.data_source_id,
}}
readOnly={readOnly}
currentDatabaseId={block.config.database?.database_id}
onDatabaseUpdate={(database) => {
const b = shallowBlockClone(block);
b.config.database.database_id = database.database_id;
onBlockUpdate(b);
}}
/>
</div>
</div>
)}
</div>
</Block>
);
}
Loading

0 comments on commit 1934f72

Please sign in to comment.