diff --git a/python_library/finicapi.egg-info/PKG-INFO b/python_library/finicapi.egg-info/PKG-INFO index 030fd5d7..a97053b5 100644 --- a/python_library/finicapi.egg-info/PKG-INFO +++ b/python_library/finicapi.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: finicapi -Version: 0.1.7 +Version: 0.1.8 Summary: Finic.ai is a platform for deploying integrations and workflow automations in Python. This is the Python client for Finic Author: Ayan Bandyopadhyay Author-email: ayan@finic.ai diff --git a/python_library/finicapi/cli.py b/python_library/finicapi/cli.py index 8f7cfd7e..fb5cca95 100644 --- a/python_library/finicapi/cli.py +++ b/python_library/finicapi/cli.py @@ -42,6 +42,10 @@ def deploy(argv=sys.argv): print("Please specify the agent_id in the finic_config.json file") return agent_id = config["agent_id"] + if "num_retries" not in config: + print("Please specify the num_retries in the finic_config.json file") + return + num_retries = config["num_retries"] finic = Finic(api_key=api_key, url=server_url) @@ -57,6 +61,6 @@ def deploy(argv=sys.argv): f"git ls-files -z --others --exclude-standard | xargs -0 zip -r {zip_file} && git ls-files -z | xargs -0 zip -ur {zip_file}" ) - result = finic.deploy_agent(agent_id, agent_name, zip_file) + result = finic.deploy_agent(agent_id, agent_name, num_retries, zip_file) print(result) diff --git a/python_library/finicapi/finic.py b/python_library/finicapi/finic.py index 841ce58e..67812479 100644 --- a/python_library/finicapi/finic.py +++ b/python_library/finicapi/finic.py @@ -1,4 +1,4 @@ -from typing import Dict, Optional +from typing import Dict, Optional, List from functools import wraps import json import os @@ -28,7 +28,9 @@ def __init__( else: self.url = "https://finic-521298051240.us-central1.run.app" - def deploy_agent(self, agent_id: str, agent_name: str, project_zipfile: str): + def deploy_agent( + self, agent_id: str, agent_name: str, num_retries: int, project_zipfile: str + ): with open(project_zipfile, "rb") as f: upload_file = f.read() @@ -41,6 +43,7 @@ def deploy_agent(self, agent_id: str, agent_name: str, project_zipfile: str): json={ "agent_id": agent_id, "agent_name": agent_name, + "num_retries": num_retries, }, ) @@ -62,6 +65,7 @@ def deploy_agent(self, agent_id: str, agent_name: str, project_zipfile: str): json={ "agent_id": agent_id, "agent_name": agent_name, + "num_retries": num_retries, }, ) @@ -84,9 +88,26 @@ def get_run_status(self, agent_id: str, run_id: str): # TODO pass - def log_run_status(self, agent_id: str, status: str, message: str): - # TODO - print(f"Logging status: {status} - {message}") + def log_attempt(self, agent_id: str, success: bool, logs: List[str], result: Dict): + if self.environment == FinicEnvironment.LOCAL: + if success: + print("Successful execution. Results: ", result) + else: + print("Failed execution. Logs: ", logs) + else: + requests.post( + f"{self.url}/log-execution-attempt", + headers={ + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json", + }, + json={ + "agent_id": agent_id, + "success": success, + "logs": logs, + "result": result, + }, + ) def workflow_entrypoint(self, func): diff --git a/python_library/setup.py b/python_library/setup.py index 5a3c2707..a9e078d3 100644 --- a/python_library/setup.py +++ b/python_library/setup.py @@ -9,7 +9,7 @@ setup( name="finicapi", - version="0.1.7", + version="0.1.8", description="Finic.ai is a platform for deploying integrations and workflow automations in Python. This is the Python client for Finic", long_description=long_description, long_description_content_type="text/markdown", diff --git a/server/agent_deployer/agent_deployer.py b/server/agent_deployer/agent_deployer.py index 914dd2e9..36d19a17 100644 --- a/server/agent_deployer/agent_deployer.py +++ b/server/agent_deployer/agent_deployer.py @@ -28,7 +28,9 @@ def __init__(self, db: Database, config: AppConfig): self.build_client = cloudbuild_v1.CloudBuildClient(credentials=credentials) self.jobs_client = run_v2.JobsClient(credentials=credentials) - async def get_agent_upload_link(self, agent: Agent, expiration_minutes: int = 15) -> str: + async def get_agent_upload_link( + self, agent: Agent, expiration_minutes: int = 15 + ) -> str: bucket = self.storage_client.get_bucket(self.deployments_bucket) blob = bucket.blob(f"{agent.id}.zip") @@ -43,7 +45,7 @@ async def deploy_agent(self, agent: Agent): # Check if the job already exists in Cloud Run try: self.jobs_client.get_job( - name=f"projects/{self.project_id}/locations/us-central1/jobs/job-{agent.id}" + name=f"projects/{self.project_id}/locations/us-central1/jobs/{Agent.get_cloud_job_id(agent)}" ) job_exists = True except Exception: @@ -106,7 +108,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=3 --task-timeout=86400s --memory=4Gi", + f"--tasks=1 --max-retries={job.num_retries} --task-timeout=86400s --memory=4Gi", ], }, ], diff --git a/server/agent_runner/agent_runner.py b/server/agent_runner/agent_runner.py index e2d9085f..efc8c178 100644 --- a/server/agent_runner/agent_runner.py +++ b/server/agent_runner/agent_runner.py @@ -52,6 +52,7 @@ async def start_agent(self, agent: Agent, input: Dict) -> Execution: {"name": "FINIC_ENV", "value": FinicEnvironment.PROD.value}, {"name": "FINIC_INPUT", "value": json.dumps(input)}, {"name": "FINIC_API_KEY", "value": secret_key}, + {"name": "FINIC_AGENT_ID", "value": agent.id}, ] } ] diff --git a/server/models/api.py b/server/models/api.py index 35e11707..a0a50fab 100644 --- a/server/models/api.py +++ b/server/models/api.py @@ -18,6 +18,7 @@ class GetExecutionRequest(BaseModel): class DeployAgentRequest(BaseModel): agent_id: str agent_name: str + num_retries: int class RunAgentRequest(BaseModel): diff --git a/server/models/models.py b/server/models/models.py index e79d2c88..c10056f1 100644 --- a/server/models/models.py +++ b/server/models/models.py @@ -38,6 +38,7 @@ class Agent(BaseModel): app_id: str name: str status: AgentStatus + num_retries: int = 3 @staticmethod def get_cloud_job_id(agent: "Agent") -> str: diff --git a/server/server/main.py b/server/server/main.py index 6e57c4f4..073247c2 100644 --- a/server/server/main.py +++ b/server/server/main.py @@ -105,7 +105,6 @@ async def deploy_agent( config: AppConfig = Depends(validate_token), ): try: - agent = await db.get_agent(config=config, user_defined_id=request.agent_id) agent.status = AgentStatus.deploying await db.upsert_agent(agent) @@ -138,6 +137,7 @@ async def get_agent_upload_link( app_id=config.app_id, user_defined_id=request.agent_id, name=request.agent_name, + num_retries=request.num_retries, status="deploying", ) await db.upsert_agent(agent)