Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayan-Bandyopadhyay committed Sep 11, 2024
2 parents f3118c7 + 3c5e928 commit 3f7b3bc
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 131 deletions.
13 changes: 9 additions & 4 deletions frontend/src/components/Modals/RunAgentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ import { Button } from "@/subframe/components/Button";
import { Alert } from "@/subframe/components/Alert";
import { Select } from "@/subframe/components/Select";
import useFinicApp from "@/hooks/useFinicApp";
import { Execution } from "@/types";

interface RunAgentDialogProps extends React.HTMLAttributes<HTMLDivElement> {
isOpen: boolean;
setIsOpen: (isOpen: boolean) => void;
setNewExecution: (execution: Execution) => void;
agentId: string | null;
}

export default function RunAgentDialog({ isOpen, agentId, setIsOpen }: RunAgentDialogProps) {
export default function RunAgentDialog({ isOpen, agentId, setIsOpen, setNewExecution }: RunAgentDialogProps) {
const [errorMessage, setErrorMessage] = React.useState<string | null>(null);
const [args, setArgs] = React.useState<string>("");
const { runAgent, isLoading } = useFinicApp();
const { runAgent, error, isLoading } = useFinicApp();
const dialogRef = useRef<HTMLDivElement>(null);

function handleClickRunAgent() {
try {
const input = JSON.parse(args == "" ? "{}" : args);
setErrorMessage(null);
runAgent(agentId!, input).then((data) => {
// Configure refresh behavior
runAgent(agentId!, input).then((execution: Execution) => {
if (execution != null) {
setNewExecution(execution);
setIsOpen(false);
}
});
} catch (error: any) {
if (error instanceof Error) {
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/hooks/useFinicApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import React, { useState, useEffect, useCallback, createContext, useContext } fr
import humps from "humps";
import { type Node, type Edge, type NodeTypes } from "@xyflow/react";
import { useAuth, useUserStateContext } from "@/hooks/useAuth";
import { Agent } from "@/types";
import { Agent, Execution } from "@/types";

const server_url = import.meta.env.VITE_APP_SERVER_URL;

interface FinicAppContextType {
error: Error | null;
isLoading: boolean;
listAgents: () => Promise<Agent[]>;
runAgent: (agentId: string, input: string) => Promise<Agent>;
runAgent: (agentId: string, input: string) => Promise<Execution>;
}

const FinicAppContext = createContext<FinicAppContextType | undefined>(undefined);
Expand All @@ -23,6 +23,7 @@ export const FinicAppContextProvider: React.FC<{ children: React.ReactNode }> =
const runAgent = useCallback(async (agentId: string, input: Record<string, any>) => {
try {
setIsLoading(true);
setError(null);
const response = await fetch(`${server_url}/run-agent`, {
method: "POST",
headers: {
Expand All @@ -47,6 +48,7 @@ export const FinicAppContextProvider: React.FC<{ children: React.ReactNode }> =
const listAgents = useCallback(async () => {
try {
setIsLoading(true);
setError(null);
const response = await fetch(`${server_url}/list-agents`, {
method: "GET",
headers: {
Expand Down
45 changes: 32 additions & 13 deletions frontend/src/pages/deployment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Alert } from "@/subframe/components/Alert";
import { DefaultPageLayout } from "@/layouts/DefaultPageLayout";
import { useUserStateContext } from "@/hooks/useAuth";
import useFinicApp from "@/hooks/useFinicApp";
import { Agent } from "@/types";
import { Agent, Execution } from "@/types";
import { RunAgentModal } from "@/components/Modals";

interface AgentRowProps {
Expand Down Expand Up @@ -68,7 +68,7 @@ function AgentRow({ bearer, initial_data, openRunAgentModal, setSelectedAgentId
</Table.Cell>
<Table.Cell>
<span className="whitespace-nowrap text-body font-body text-neutral-500">
{agent.name}
{agent.description}
</span>
</Table.Cell>
<Table.Cell>
Expand Down Expand Up @@ -144,6 +144,7 @@ export function DeploymentPage() {
const [agents, setAgents] = useState<Array<Agent>>([]);
const [runAgentModalOpen, setRunAgentModalOpen] = useState(false);
const [selectedAgentId, setSelectedAgentId] = useState<string | null>(null);
const [newExecution, setNewExecution] = useState<Execution | null>(null);
const { isLoading, listAgents } = useFinicApp();
const { bearer } = useUserStateContext();

Expand All @@ -159,21 +160,39 @@ export function DeploymentPage() {

return (
<DefaultPageLayout>
<RunAgentModal isOpen={runAgentModalOpen} setIsOpen={setRunAgentModalOpen} agentId={selectedAgentId}/>
<RunAgentModal
isOpen={runAgentModalOpen}
setIsOpen={setRunAgentModalOpen}
setNewExecution={setNewExecution}
agentId={selectedAgentId}
/>
<div className="flex w-full flex-col items-start gap-6 pt-6 pr-6 pb-6 pl-6">
<Alert
{newExecution ? <Alert
variant="neutral"
icon="FeatherLoader2"
title="Amazon Scraper Running"
description="Click here to view the status."
icon="FeatherRocket"
title="We have liftoff!"
description={`Agent ${selectedAgentId} is now running.`}
actions={
<IconButton
size="medium"
icon="FeatherX"
onClick={(event: React.MouseEvent<HTMLButtonElement>) => {}}
/>
<>
<Button
disabled={false}
variant="neutral-secondary"
size="medium"
icon={null}
iconRight={null}
loading={false}
onClick={(event: React.MouseEvent<HTMLButtonElement>) => {}}
>
View Status
</Button>
<IconButton
size="medium"
icon="FeatherX"
onClick={() => {setNewExecution(null)}}
/>
</>
}
/>
/>: null}
<div className="flex flex-col items-start gap-2">
<span className="text-heading-1 font-heading-1 text-default-font">
Deployment
Expand Down
115 changes: 11 additions & 104 deletions frontend/src/types/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import React from "react";
import { type NodeTypes, type Node, type Edge } from "@xyflow/react";
import {
SourceNode,
DestinationNode,
TransformationNode,
} from "@/components/Nodes";
import {
SourceNodeConfigurationDrawer,
DestinationNodeConfigurationDrawer,
TransformationNodeConfigurationDrawer,
} from "@/components/ConfigurationDrawer";
import { GCSConfigurationDrawer } from "@/components/ConfigurationDrawer/Sources/GCSConfigurationDrawer";
import { SnowflakeConfigurationDrawer } from "@/components/ConfigurationDrawer/Destinations/SnowflakeConfigurationDrawer";

export enum SecretType {
PASSWORD = "password",
API_KEY = "api_key",
Expand All @@ -22,103 +7,25 @@ export enum SecretType {
export type FinicSecret = {
id: string;
type: SecretType;
service_name: string;
serviceName: string;
value: string;
};

export enum FinicNodeType {
SOURCE = "source",
DESTINATION = "destination",
TRANSFORMATION = "transformation",
}

export const nodeTypes: NodeTypes = {
source: SourceNode,
destination: DestinationNode,
transformation: TransformationNode,
};

export const configurationDrawerTypes = {
source: SourceNodeConfigurationDrawer,
destination: DestinationNodeConfigurationDrawer,
transformation: TransformationNodeConfigurationDrawer,
};

export const NodeTypeNames = {
source: "Source",
destination: "Destination",
transformation: "Transformation",
};

export const NodeIcons = {
source: "FeatherFileInput",
destination: "FeatherFileOutput",
transformation: "FeatherCode2",
};

export type NodeResults = {
columns: string[];
data: Array<Array<string | number | boolean>>;
};

export type Agent = {
id: string;
userDefinedId: string;
name: string;
finicId: string;
description: string;
status: string;
created_at: string;
createdAt: string;
url: string;
};

export enum SourceNodeType {
GOOGLE_CLOUD_STORAGE = "google_cloud_storage",
}

export enum SourceTypeNames {
google_cloud_storage = "Google Cloud Storage",
}

export enum DestinationNodeType {
SNOWFLAKE = "snowflake",
}

export enum DestinationTypeNames {
snowflake = "Snowflake",
}


export const SourceConfigurationDrawerType = {
google_cloud_storage: GCSConfigurationDrawer,
};

export type FinicNode = {
export type Execution = {
id: string;
position: { x: number; y: number };
data: {
name: string;
configuration?: any;
results?: any
};
type: string;
};

export const DestinationConfigurationDrawerType = {
snowflake: SnowflakeConfigurationDrawer,
};

export const SourceConfigFields: Record<string, { [key: string]: string }> = {
google_cloud_storage: {
bucket: "Bucket",
filename: "Filename",
}
};

export const DestinationConfigFields: Record<string, { [key: string]: string }> = {
snowflake: {
account: "Account",
warehouse: "Warehouse",
database: "Database",
tableSchema: "Table Schema",
table: "Table",
}
finicAgentId: string;
cloudProviderId: string
status: string;
startTime: string | null;
endTime: string | null;
results: Record<string, any>;
};
10 changes: 5 additions & 5 deletions server/agent_deployer/agent_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def deploy_agent(self, agent: Agent):
job_exists = False

# Define the build steps
build_config = self._get_build_config(job=agent, job_exists=job_exists)
build_config = self._get_build_config(agent=agent, job_exists=job_exists)

# Trigger the build
build = cloudbuild_v1.Build(
Expand All @@ -73,9 +73,9 @@ async def deploy_agent(self, agent: Agent):

print(f"Built and pushed Docker image: {agent.finic_id}")

def _get_build_config(self, job: Agent, job_exists: bool) -> dict:
image_name = f"gcr.io/{self.project_id}/{job.id}:latest"
gcs_source = f"gs://{self.deployments_bucket}/{job.id}.zip"
def _get_build_config(self, agent: Agent, job_exists: bool) -> dict:
image_name = f"gcr.io/{self.project_id}/{agent.finic_id}:latest"
gcs_source = f"gs://{self.deployments_bucket}/{agent.finic_id}.zip"
job_command = "update" if job_exists else "create"
return {
"steps": [
Expand Down Expand Up @@ -105,7 +105,7 @@ def _get_build_config(self, job: Agent, job_exists: bool) -> dict:
"args": [
"-c",
f"gcloud run jobs {job_command} {Agent.get_cloud_job_id(job)} --image {image_name} --region us-central1 "
f"--tasks=1 --max-retries={job.num_retries} --task-timeout=86400s --memory=4Gi",
f"--tasks=1 --max-retries={agent.num_retries} --task-timeout=86400s --memory=4Gi",
],
},
],
Expand Down
5 changes: 4 additions & 1 deletion server/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class GetExecutionRequest(BaseModel):

class DeployAgentRequest(BaseModel):
agent_id: str
agent_name: str
agent_description: str
num_retries: int

class DeleteAgentRequest(BaseModel):
agent_id: str
num_retries: int

class RunAgentRequest(BaseModel):
agent_id: str
Expand Down
2 changes: 1 addition & 1 deletion server/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Agent(BaseModel):
finic_id: str
id: str
app_id: str
name: str
description: str
status: AgentStatus
created_at: Optional[datetime.datetime] = None
num_retries: int = 3
Expand Down
25 changes: 24 additions & 1 deletion server/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async def get_agent_upload_link(
finic_id=str(uuid.uuid4()),
app_id=config.app_id,
id=request.agent_id,
name=request.agent_name,
description=request.agent_description,
num_retries=request.num_retries,
status="deploying",
)
Expand Down Expand Up @@ -220,6 +220,29 @@ async def list_agents(
print(e)
raise HTTPException(status_code=500, detail=str(e))

@app.post("/delete-agent")
async def delete_agent(
request: DeployAgentRequest = Body(...),
config: AppConfig = Depends(validate_token),
):
try:
agent = await db.get_agent(config=config, id=request.agent_id)
agent.status = AgentStatus.deploying
await db.upsert_agent(agent)
deployer = AgentDeployer(db=db, config=config)
try:
await deployer.deploy_agent(agent=agent)
agent.status = AgentStatus.deployed
await db.upsert_agent(agent)
return agent
except Exception as e:
agent.status = AgentStatus.failed
await db.upsert_agent(agent)
raise HTTPException(status_code=500, detail=str(e))
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail=str(e))


@app.get("/get-execution")
async def get_execution(
Expand Down

0 comments on commit 3f7b3bc

Please sign in to comment.