Skip to content

Commit

Permalink
PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
jahwag committed Aug 21, 2024
1 parent f841ece commit 0836b10
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 139 deletions.
259 changes: 121 additions & 138 deletions src/claudesync/cli/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,70 +158,16 @@ def create(config, name, project):
return

if not project:
all_projects = provider.get_projects(organization_id)
if not all_projects:
click.echo("No projects found in the active organization.")
return

# Filter projects to include only the active project and its submodules
filtered_projects = [
p
for p in all_projects
if p["id"] == active_project_id
or (
p["name"].startswith(f"{active_project_name}-SubModule-")
and not p.get("archived_at")
)
]

if not filtered_projects:
click.echo("No active project or related submodules found.")
project = select_project(
active_project_id,
active_project_name,
local_path,
organization_id,
provider,
)
if project is None:
return

# Determine the current working directory
current_dir = os.path.abspath(os.getcwd())

# Find the project that matches the current directory
default_project = None
for idx, proj in enumerate(filtered_projects):
if proj["id"] == active_project_id:
project_path = os.path.abspath(local_path)
else:
submodule_name = proj["name"].replace(
f"{active_project_name}-SubModule-", ""
)
project_path = os.path.abspath(
os.path.join(local_path, "services", submodule_name)
)
if current_dir.startswith(project_path):
default_project = idx
break

click.echo("Available projects:")
for idx, proj in enumerate(filtered_projects, 1):
project_type = (
"Active Project" if proj["id"] == active_project_id else "Submodule"
)
default_marker = " (default)" if idx - 1 == default_project else ""
click.echo(
f"{idx}. {proj['name']} (ID: {proj['id']}) - {project_type}{default_marker}"
)

while True:
prompt = "Enter the number of the project to associate with the chat"
if default_project is not None:
default_project_name = filtered_projects[default_project]["name"]
prompt += f" (default: {default_project + 1} - {default_project_name})"
selection = click.prompt(
prompt,
type=int,
default=default_project + 1 if default_project is not None else None,
)
if 1 <= selection <= len(filtered_projects):
project = filtered_projects[selection - 1]["id"]
break
click.echo("Invalid selection. Please try again.")

try:
new_chat = provider.create_chat(
organization_id, chat_name=name, project_uuid=project
Expand Down Expand Up @@ -255,82 +201,16 @@ def send(config, message, chat, timezone):
message = " ".join(message) # Join all message parts into a single string

try:
if not chat:
# Get all projects
all_projects = provider.get_projects(organization_id)
if not all_projects:
click.echo("No projects found in the active organization.")
return

# Filter projects to include only the active project and its submodules
filtered_projects = [
p
for p in all_projects
if p["id"] == active_project_id
or (
p["name"].startswith(f"{active_project_name}-SubModule-")
and not p.get("archived_at")
)
]

if not filtered_projects:
click.echo("No active project or related submodules found.")
return

# Determine the current working directory
current_dir = os.path.abspath(os.getcwd())

# Find the project that matches the current directory
default_project = None
for idx, proj in enumerate(filtered_projects):
if proj["id"] == active_project_id:
project_path = os.path.abspath(local_path)
else:
submodule_name = proj["name"].replace(
f"{active_project_name}-SubModule-", ""
)
project_path = os.path.abspath(
os.path.join(local_path, "services", submodule_name)
)
if current_dir.startswith(project_path):
default_project = idx
break

click.echo("Available projects:")
for idx, proj in enumerate(filtered_projects, 1):
project_type = (
"Active Project" if proj["id"] == active_project_id else "Submodule"
)
default_marker = " (default)" if idx - 1 == default_project else ""
click.echo(
f"{idx}. {proj['name']} (ID: {proj['id']}) - {project_type}{default_marker}"
)

while True:
prompt = "Enter the number of the project to associate with the chat"
if default_project is not None:
default_project_name = filtered_projects[default_project]["name"]
prompt += (
f" (default: {default_project + 1} - {default_project_name})"
)
selection = click.prompt(
prompt,
type=int,
default=(
default_project + 1 if default_project is not None else None
),
)
if 1 <= selection <= len(filtered_projects):
selected_project = filtered_projects[selection - 1]["id"]
break
click.echo("Invalid selection. Please try again.")

# Create a new chat with the selected project
new_chat = provider.create_chat(
organization_id, project_uuid=selected_project
)
chat = new_chat["uuid"]
click.echo(f"New chat created with ID: {chat}")
chat = create_chat(
active_project_id,
active_project_name,
chat,
local_path,
organization_id,
provider,
)
if chat is None:
return

# Send message and process the streaming response
for event in provider.send_message(organization_id, chat, message, timezone):
Expand All @@ -349,3 +229,106 @@ def send(config, message, chat, timezone):

except Exception as e:
click.echo(f"Failed to send message: {str(e)}")


def create_chat(
active_project_id, active_project_name, chat, local_path, organization_id, provider
):
if not chat:
selected_project = select_project(
active_project_id,
active_project_name,
local_path,
organization_id,
provider,
)
if selected_project is None:
return

# Create a new chat with the selected project
new_chat = provider.create_chat(organization_id, project_uuid=selected_project)
chat = new_chat["uuid"]
click.echo(f"New chat created with ID: {chat}")
return chat


def select_project(
active_project_id, active_project_name, local_path, organization_id, provider
):
all_projects = provider.get_projects(organization_id)
if not all_projects:
click.echo("No projects found in the active organization.")
return None

# Filter projects to include only the active project and its submodules
filtered_projects = [
p
for p in all_projects
if p["id"] == active_project_id
or (
p["name"].startswith(f"{active_project_name}-SubModule-")
and not p.get("archived_at")
)
]

if not filtered_projects:
click.echo("No active project or related submodules found.")
return None

# Determine the current working directory
current_dir = os.path.abspath(os.getcwd())

default_project = get_default_project(
active_project_id,
active_project_name,
current_dir,
filtered_projects,
local_path,
)

click.echo("Available projects:")
for idx, proj in enumerate(filtered_projects, 1):
project_type = (
"Active Project" if proj["id"] == active_project_id else "Submodule"
)
default_marker = " (default)" if idx - 1 == default_project else ""
click.echo(
f"{idx}. {proj['name']} (ID: {proj['id']}) - {project_type}{default_marker}"
)

while True:
prompt = "Enter the number of the project to associate with the chat"
if default_project is not None:
default_project_name = filtered_projects[default_project]["name"]
prompt += f" (default: {default_project + 1} - {default_project_name})"
selection = click.prompt(
prompt,
type=int,
default=default_project + 1 if default_project is not None else None,
)
if 1 <= selection <= len(filtered_projects):
project = filtered_projects[selection - 1]["id"]
break
click.echo("Invalid selection. Please try again.")
return project


def get_default_project(
active_project_id, active_project_name, current_dir, filtered_projects, local_path
):
# Find the project that matches the current directory
default_project = None
for idx, proj in enumerate(filtered_projects):
if proj["id"] == active_project_id:
project_path = os.path.abspath(local_path)
else:
submodule_name = proj["name"].replace(
f"{active_project_name}-SubModule-", ""
)
project_path = os.path.abspath(
os.path.join(local_path, "services", submodule_name)
)
if current_dir.startswith(project_path):
default_project = idx
break
return default_project
2 changes: 1 addition & 1 deletion src/claudesync/providers/claude_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def handle_http_error(self, e):
print(f"Message limit exceeded. Try again after {formatted_time}")
except (KeyError, json.JSONDecodeError) as parse_error:
print(f"Failed to parse error response: {parse_error}")
raise ProviderError(f"HTTP 429: Too Many Requests")
raise ProviderError("HTTP 429: Too Many Requests")
raise ProviderError(f"API request failed: {str(e)}")

def _make_request_stream(self, method, endpoint, data=None):
Expand Down

0 comments on commit 0836b10

Please sign in to comment.