Skip to content

Commit

Permalink
chore(cli): Minor fix & Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
HamadaSalhab committed Jan 30, 2025
1 parent 37c8f53 commit 193a4a0
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 118 deletions.
28 changes: 12 additions & 16 deletions cli/julep_cli/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
from typing import Annotated

import typer

from .utils import get_julep_client

from .app import agents_app, console, error_console
from rich.box import HEAVY_HEAD
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.text import Text
from rich.table import Table
from rich.box import HEAVY_HEAD
from rich.text import Text

from .app import agents_app, console, error_console
from .utils import get_julep_client

SINGLE_AGENT_TABLE_WIDTH = 100
SINGLE_AGENT_COLUMN_WIDTH = 50


@agents_app.command()
def create(
name: Annotated[str | None, typer.Option("--name", "-n", help="Name of the agent")] = None,
Expand Down Expand Up @@ -159,8 +159,8 @@ def update(
except Exception as e:
error_console.print(f"Error updating agent: {e}")
raise typer.Exit(1)
console.print(Text(f"Agent updated successfully.", style="bold green"))

console.print(Text("Agent updated successfully.", style="bold green"))


@agents_app.command()
Expand Down Expand Up @@ -199,7 +199,7 @@ def delete(
error_console.print(f"Error deleting agent: {e}")
raise typer.Exit(1)

console.print(Text(f"Agent deleted successfully.", style="bold green"))
console.print(Text("Agent deleted successfully.", style="bold green"))


@agents_app.command()
Expand Down Expand Up @@ -237,11 +237,11 @@ def list(
except Exception as e:
error_console.print(Text(f"Error fetching agents: {e}", style="bold red"))
raise typer.Exit(1)

if json_output:
typer.echo([agent.model_dump_json(indent=2) for agent in agents])
return

# Table format output
agent_table = Table(
title=Text("Available Agents:", style="bold underline magenta"),
Expand All @@ -257,7 +257,6 @@ def list(
agent_table.add_column("Model", style="yellow", width=25)
agent_table.add_column("ID", style="green", width=40)


for agent in agents:
agent_table.add_row(
agent.name,
Expand All @@ -269,8 +268,6 @@ def list(
console.print(agent_table)




@agents_app.command()
def get(
id: Annotated[str, typer.Option("--id", help="ID of the agent to retrieve")],
Expand All @@ -296,8 +293,7 @@ def get(
error_console.print(f"Error retrieving agent: {e}")
raise typer.Exit(1)

console.print(Text(f"Agent retrieved successfully.", style="bold green"))

console.print(Text("Agent retrieved successfully.", style="bold green"))

if json_output:
console.print(json.dumps(agent.model_dump(), indent=2))
Expand Down
2 changes: 1 addition & 1 deletion cli/julep_cli/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import Annotated

import typer
from rich.text import Text
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.text import Text

from .app import app, console, error_console
from .utils import get_julep_client
Expand Down
1 change: 0 additions & 1 deletion cli/julep_cli/executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import typer
from rich.console import Console
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn

from .app import executions_app
Expand Down
66 changes: 18 additions & 48 deletions cli/julep_cli/importt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import typer
from julep.types.agent import Agent
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.panel import Panel
from rich.text import Text
from rich.table import Table
from .app import import_app, console, error_console
from rich.text import Text

from .app import console, error_console, import_app
from .models import LockedEntity
from .utils import (
add_agent_to_julep_yaml,
add_entity_to_lock_file,
get_entity_from_lock_file,
get_julep_client,
add_agent_to_julep_yaml,
update_existing_entity_in_lock_file,
update_yaml_for_existing_entity,
)
Expand Down Expand Up @@ -78,7 +78,7 @@ def agent(
except Exception as e:
error_console.print(Text(f"Error fetching agent from remote: {e}", style="bold red"))
raise typer.Exit(1)

# Create a table to display agent data
table = Table(title="Agent Data")
table.add_column("Field", style="cyan", no_wrap=True)
Expand All @@ -99,7 +99,7 @@ def agent(
console=console
) as progress:
try:
update_task = progress.add_task("Updating agent in '{agent_yaml_path}'...", start=False)
update_task = progress.add_task(f"Updating agent in '{agent_yaml_path}'...", start=False)
progress.start_task(update_task)
update_yaml_for_existing_entity(
agent_yaml_path,
Expand All @@ -109,8 +109,7 @@ def agent(
error_console.print(Text(f"Error updating agent in '{agent_yaml_path}': {e}", style="bold red"))
raise typer.Exit(1)

console.print(Text(f"Updated successfully.", style="bold green"))

console.print(Text("Updated successfully.", style="bold green"))

console.print(Text(f"Updating agent '{id}' in lock file...", style="bold blue"))
update_existing_entity_in_lock_file(
Expand Down Expand Up @@ -155,7 +154,7 @@ def agent(
error_console.print(
Text(f"Error fetching agent from remote: {e}", style="bold red"))
raise typer.Exit(1)

# Create a table to display agent data
table = Table(title="Agent Data")
table.add_column("Field", style="cyan", no_wrap=True)
Expand All @@ -167,56 +166,27 @@ def agent(

console.print(table)


agent_name = remote_agent.name.lower().replace(" ", "_")

agent_yaml_path: Path = output / f"{agent_name}.yaml"

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console
) as progress:
try:
update_task = progress.add_task("Updating agent in '{agent_yaml_path}'...", start=False)
progress.start_task(update_task)
update_yaml_for_existing_entity(agent_yaml_path, remote_agent.model_dump(
exclude={"id", "created_at", "updated_at"}))

except Exception as e:
error_console.print(Text(f"Error updating agent in '{agent_yaml_path}': {e}", style="bold red"))
raise typer.Exit(1)

console.print(Text(f"Updated successfully.", style="bold green"))
typer.echo(f"Adding agent '{remote_agent.name}' to '{agent_yaml_path}'...")
update_yaml_for_existing_entity(agent_yaml_path, remote_agent.model_dump(exclude={"id", "created_at", "updated_at"}))

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=console
) as progress:
try:
add_task = progress.add_task("Adding agent to julep.yaml...", start=False)
progress.start_task(add_task)
add_agent_to_julep_yaml(source, {
"definition": str(agent_yaml_path.relative_to(source)),
})
except Exception as e:
error_console.print(Text(f"Error adding agent to julep.yaml: {e}", style="bold red"))
raise typer.Exit(1)

console.print(Text(f"Added successfully.", style="bold green"))
typer.echo(f"Agent '{id}' imported successfully to '{agent_yaml_path}'")

add_agent_to_julep_yaml(source, {
"definition": str(agent_yaml_path.relative_to(source)),
})

console.print(Text(f"Adding agent to lock file...", style="bold blue"))
typer.echo(f"Adding agent '{id}' to lock file...")
add_entity_to_lock_file(
type="agent",
new_entity=LockedEntity(
path=str(agent_yaml_path.relative_to(source)),
id=remote_agent.id,
last_synced=datetime.datetime.now().isoformat(timespec="milliseconds") + "Z",
revision_hash=hashlib.sha256(
remote_agent.model_dump_json().encode()
agent_data.model_dump_json().encode()
).hexdigest(),
),
project_dir=source,
Expand All @@ -226,4 +196,4 @@ def agent(

except Exception as e:
error_console.print(Text(f"Error importing agent: {e}", style="bold red"))
raise typer.Exit(1)
raise typer.Exit(1)
15 changes: 7 additions & 8 deletions cli/julep_cli/logs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import json
import time
from typing import Annotated
from julep.resources.executions.transitions import Transition

import typer
from julep.resources.executions.transitions import Transition
from rich.box import HEAVY
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.table import Table
from rich.text import Text

from .app import app, console, error_console
from .utils import get_julep_client
from rich.text import Text
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.box import HEAVY


@app.command()
def logs(
Expand All @@ -23,7 +24,7 @@ def logs(

transitions_table = Table(
title="Execution Transitions",
box=HEAVY, # border between cells
box=HEAVY, # border between cells
show_lines=True, # Adds lines between rows
show_header=True,
header_style="bold magenta",
Expand All @@ -48,7 +49,6 @@ def display_transitions(transitions: list[Transition]):

console.print(transitions_table)


client = get_julep_client()

with Progress(
Expand Down Expand Up @@ -79,7 +79,6 @@ def display_transitions(transitions: list[Transition]):

transitions = fetched_transitions


if transitions and transitions[0].type in ["finish", "cancelled", "error"]:
break

Expand Down
7 changes: 3 additions & 4 deletions cli/julep_cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

import typer
import yaml

from rich.text import Text
from rich.box import HEAVY
from rich.table import Table
from rich.text import Text

from .app import app, console, error_console
from .app import app, console
from .models import LockFileContents
from .utils import get_lock_file

TABLE_WIDTH = 150
COLUMN_WIDTH = TABLE_WIDTH // 3


@app.command()
def ls(
source: Annotated[
Expand Down
26 changes: 12 additions & 14 deletions cli/julep_cli/sync.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from rich.text import Text
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
import hashlib
import json
from datetime import datetime
Expand All @@ -9,6 +6,9 @@

import typer
import yaml
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.text import Text

from .app import app, console, error_console
from .models import (
Expand Down Expand Up @@ -104,21 +104,21 @@ def sync(
table = Table(title="Agents", show_header=False, title_style="bold magenta")
table.add_column("Definition", style="dim", width=50)
for agent in agents:
table.add_row(agent.get('definition'))
table.add_row(agent.get("definition"))
console.print(table)

if tasks:
table = Table(title="Tasks", show_header=False, title_style="bold cyan")
table.add_column("Definition", style="dim", width=50)
for task in tasks:
table.add_row(task.get('definition'))
table.add_row(task.get("definition"))
console.print(table)

if tools:
table = Table(title="Tools", show_header=False, title_style="bold green")
table.add_column("Definition", style="dim", width=50)
for tool in tools:
table.add_row(tool.get('definition'))
table.add_row(tool.get("definition"))
console.print(table)

else:
Expand Down Expand Up @@ -260,7 +260,6 @@ def sync(
console.print(
Text("All tools were successfully created on remote", style="bold magenta"))


with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
Expand Down Expand Up @@ -564,7 +563,7 @@ def sync(
f"Tool {tool_yaml_def_path} created successfully on remote", style="bold blue"))

if found_changes:
with Progress(
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
Expand All @@ -574,15 +573,14 @@ def sync(
progress.start_task(sync_task)

write_lock_file(source, lock_file)

console.print(Text("Synchronization complete", style="bold green"))

return

else:
console.print(Text("No changes detected. Everything is up to date.", style="bold green"))
return

console.print(Text("No changes detected. Everything is up to date.", style="bold green"))
return

if force_remote:
console.print(Text("Force remote - not implemented", style="bold red"))
raise typer.Exit(1)
Expand All @@ -594,4 +592,4 @@ def sync(

# Compare local and remote states

# TODO: Implement logic when no force flags are provided
# TODO: Implement logic when no force flags are provided
Loading

0 comments on commit 193a4a0

Please sign in to comment.