janito 0.8.0__py3-none-any.whl → 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- janito/__init__.py +5 -0
- janito/__main__.py +143 -120
- janito/callbacks.py +130 -0
- janito/cli.py +202 -0
- janito/config.py +63 -100
- janito/data/instructions.txt +6 -0
- janito/test_file.py +4 -0
- janito/token_report.py +73 -0
- janito/tools/__init__.py +10 -0
- janito/tools/decorators.py +84 -0
- janito/tools/delete_file.py +44 -0
- janito/tools/find_files.py +154 -0
- janito/tools/search_text.py +197 -0
- janito/tools/str_replace_editor/__init__.py +6 -0
- janito/tools/str_replace_editor/editor.py +43 -0
- janito/tools/str_replace_editor/handlers.py +338 -0
- janito/tools/str_replace_editor/utils.py +88 -0
- {janito-0.8.0.dist-info/licenses → janito-0.9.0.dist-info}/LICENSE +2 -2
- janito-0.9.0.dist-info/METADATA +9 -0
- janito-0.9.0.dist-info/RECORD +23 -0
- {janito-0.8.0.dist-info → janito-0.9.0.dist-info}/WHEEL +2 -1
- janito-0.9.0.dist-info/entry_points.txt +2 -0
- janito-0.9.0.dist-info/top_level.txt +1 -0
- janito/agents/__init__.py +0 -22
- janito/agents/agent.py +0 -25
- janito/agents/claudeai.py +0 -41
- janito/agents/deepseekai.py +0 -47
- janito/change/applied_blocks.py +0 -34
- janito/change/applier.py +0 -167
- janito/change/edit_blocks.py +0 -148
- janito/change/finder.py +0 -72
- janito/change/request.py +0 -144
- janito/change/validator.py +0 -87
- janito/change/view/content.py +0 -63
- janito/change/view/diff.py +0 -44
- janito/change/view/panels.py +0 -201
- janito/change/view/sections.py +0 -69
- janito/change/view/styling.py +0 -140
- janito/change/view/summary.py +0 -37
- janito/change/view/themes.py +0 -62
- janito/change/view/viewer.py +0 -59
- janito/cli/__init__.py +0 -2
- janito/cli/commands.py +0 -68
- janito/cli/functions.py +0 -66
- janito/common.py +0 -133
- janito/data/change_prompt.txt +0 -81
- janito/data/system_prompt.txt +0 -3
- janito/qa.py +0 -56
- janito/version.py +0 -23
- janito/workspace/__init__.py +0 -8
- janito/workspace/analysis.py +0 -121
- janito/workspace/models.py +0 -97
- janito/workspace/show.py +0 -115
- janito/workspace/stats.py +0 -42
- janito/workspace/workset.py +0 -135
- janito/workspace/workspace.py +0 -335
- janito-0.8.0.dist-info/METADATA +0 -106
- janito-0.8.0.dist-info/RECORD +0 -40
- janito-0.8.0.dist-info/entry_points.txt +0 -2
janito/__init__.py
ADDED
janito/__main__.py
CHANGED
@@ -1,128 +1,151 @@
|
|
1
|
+
"""
|
2
|
+
Main entry point for the janito CLI.
|
3
|
+
"""
|
4
|
+
|
1
5
|
import typer
|
2
|
-
|
6
|
+
import os
|
7
|
+
import sys
|
3
8
|
from pathlib import Path
|
4
|
-
from
|
5
|
-
from rich import print as rich_print
|
9
|
+
from typing import Optional, Dict, Any, List
|
6
10
|
from rich.console import Console
|
7
|
-
from
|
8
|
-
|
9
|
-
|
10
|
-
from
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
11
|
+
from rich import print as rprint
|
12
|
+
from rich.markdown import Markdown
|
13
|
+
import claudine
|
14
|
+
from claudine.exceptions import MaxTokensExceededException, MaxRoundsExceededException
|
15
|
+
import locale
|
16
|
+
|
17
|
+
# Fix console encoding for Windows
|
18
|
+
if sys.platform == 'win32':
|
19
|
+
# Try to set UTF-8 mode for Windows 10 version 1903 or newer
|
20
|
+
os.system('chcp 65001 > NUL')
|
21
|
+
# Ensure stdout and stderr are using UTF-8
|
22
|
+
sys.stdout.reconfigure(encoding='utf-8')
|
23
|
+
sys.stderr.reconfigure(encoding='utf-8')
|
24
|
+
# Set locale to UTF-8
|
25
|
+
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
26
|
+
|
27
|
+
from janito.tools.str_replace_editor.editor import str_replace_editor
|
28
|
+
from janito.tools.find_files import find_files
|
29
|
+
from janito.tools.delete_file import delete_file
|
30
|
+
from janito.tools.search_text import search_text
|
31
|
+
from janito.config import get_config
|
32
|
+
from janito.callbacks import pre_tool_callback, post_tool_callback
|
33
|
+
from janito.token_report import generate_token_report
|
34
|
+
|
35
|
+
app = typer.Typer(help="Janito CLI tool")
|
36
|
+
|
37
|
+
@app.command()
|
38
|
+
def hello(name: str = typer.Argument("World", help="Name to greet")):
|
39
|
+
"""
|
40
|
+
Say hello to someone.
|
41
|
+
"""
|
42
|
+
rprint(f"[bold green]Hello {name}[/bold green]")
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
@app.callback(invoke_without_command=True)
|
47
|
+
def main(ctx: typer.Context,
|
48
|
+
query: Optional[str] = typer.Argument(None, help="Query to send to the claudine agent"),
|
49
|
+
debug: bool = typer.Option(False, "--debug", "-d", help="Enable debug mode"),
|
50
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed token usage and pricing information"),
|
51
|
+
workspace: Optional[str] = typer.Option(None, "--workspace", "-w", help="Set the workspace directory")):
|
52
|
+
"""
|
53
|
+
Janito CLI tool. If a query is provided without a command, it will be sent to the claudine agent.
|
54
|
+
"""
|
55
|
+
console = Console()
|
56
|
+
|
57
|
+
# Set debug mode in config
|
58
|
+
get_config().debug_mode = debug
|
59
|
+
|
60
|
+
if workspace:
|
61
|
+
try:
|
62
|
+
print(f"Setting workspace directory to: {workspace}")
|
63
|
+
get_config().workspace_dir = workspace
|
64
|
+
print(f"Workspace directory set to: {get_config().workspace_dir}")
|
65
|
+
except ValueError as e:
|
66
|
+
console.print(f"[bold red]Error:[/bold red] {str(e)}")
|
67
|
+
sys.exit(1)
|
68
|
+
|
69
|
+
if ctx.invoked_subcommand is None:
|
70
|
+
# If no query provided in command line, read from stdin
|
71
|
+
if not query:
|
72
|
+
console.print("[bold blue]No query provided in command line. Reading from stdin...[/bold blue]")
|
73
|
+
query = sys.stdin.read().strip()
|
74
|
+
|
75
|
+
# Only proceed if we have a query (either from command line or stdin)
|
76
|
+
if query:
|
77
|
+
# Get API key from environment variable or ask the user
|
78
|
+
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
79
|
+
if not api_key:
|
80
|
+
console.print("[bold yellow]Warning:[/bold yellow] ANTHROPIC_API_KEY environment variable not set.")
|
81
|
+
console.print("Please set it or provide your API key now:")
|
82
|
+
api_key = typer.prompt("Anthropic API Key", hide_input=True)
|
83
|
+
|
84
|
+
# Load instructions from file
|
85
|
+
import importlib.resources as pkg_resources
|
50
86
|
try:
|
51
|
-
|
52
|
-
|
87
|
+
# For Python 3.9+
|
88
|
+
try:
|
89
|
+
from importlib.resources import files
|
90
|
+
instructions = files('janito.data').joinpath('instructions.txt').read_text()
|
91
|
+
# Fallback for older Python versions
|
92
|
+
except (ImportError, AttributeError):
|
93
|
+
instructions = pkg_resources.read_text('janito.data', 'instructions.txt')
|
94
|
+
instructions = instructions.strip()
|
53
95
|
except Exception as e:
|
54
|
-
|
55
|
-
|
56
|
-
|
96
|
+
console.print(f"[bold yellow]Warning:[/bold yellow] Could not load instructions file: {str(e)}")
|
97
|
+
console.print("[dim]Using default instructions instead.[/dim]")
|
98
|
+
instructions = "You are a helpful AI assistant. Answer the user's questions to the best of your ability."
|
99
|
+
|
100
|
+
# Initialize the agent with the tools
|
101
|
+
agent = claudine.Agent(
|
102
|
+
api_key=api_key,
|
103
|
+
tools=[
|
104
|
+
delete_file,
|
105
|
+
find_files,
|
106
|
+
search_text
|
107
|
+
],
|
108
|
+
text_editor_tool=str_replace_editor,
|
109
|
+
tool_callbacks=(pre_tool_callback, post_tool_callback),
|
110
|
+
max_tokens=4096,
|
111
|
+
temperature=0.7,
|
112
|
+
instructions=instructions,
|
113
|
+
debug_mode=debug # Enable debug mode
|
114
|
+
)
|
115
|
+
|
116
|
+
# Process the query
|
117
|
+
console.print(f"[bold blue]Query:[/bold blue] {query}")
|
118
|
+
console.print("[bold blue]Generating response...[/bold blue]")
|
119
|
+
|
120
|
+
try:
|
121
|
+
response = agent.process_prompt(query)
|
122
|
+
|
123
|
+
console.print("\n[bold green]Janito:[/bold green]")
|
124
|
+
# Use rich's enhanced Markdown rendering for the response
|
125
|
+
console.print(Markdown(response, code_theme="monokai"))
|
126
|
+
|
127
|
+
except MaxTokensExceededException as e:
|
128
|
+
# Display the partial response if available
|
129
|
+
if e.response_text:
|
130
|
+
console.print("\n[bold green]Partial Janito:[/bold green]")
|
131
|
+
console.print(Markdown(e.response_text, code_theme="monokai"))
|
132
|
+
|
133
|
+
console.print("\n[bold red]Error:[/bold red] Response was truncated because it reached the maximum token limit.")
|
134
|
+
console.print("[dim]Consider increasing the max_tokens parameter or simplifying your query.[/dim]")
|
135
|
+
|
136
|
+
except MaxRoundsExceededException as e:
|
137
|
+
# Display the final response if available
|
138
|
+
if e.response_text:
|
139
|
+
console.print("\n[bold green]Janito:[/bold green]")
|
140
|
+
console.print(Markdown(e.response_text, code_theme="monokai"))
|
141
|
+
|
142
|
+
console.print(f"\n[bold red]Error:[/bold red] Maximum number of tool execution rounds ({e.rounds}) reached. Some tasks may be incomplete.")
|
143
|
+
console.print("[dim]Consider increasing the max_rounds parameter or breaking down your task into smaller steps.[/dim]")
|
144
|
+
|
145
|
+
# Show token usage report
|
146
|
+
generate_token_report(agent, verbose)
|
57
147
|
else:
|
58
|
-
|
59
|
-
rich_print(error_text)
|
60
|
-
raise typer.Exit(1)
|
61
|
-
|
62
|
-
# Configure workspace
|
63
|
-
config.set_workspace_dir(workspace_dir)
|
64
|
-
config.set_debug(debug)
|
65
|
-
config.set_verbose(verbose)
|
66
|
-
config.set_auto_apply(auto_apply)
|
67
|
-
|
68
|
-
# Configure workset with scan paths
|
69
|
-
if include:
|
70
|
-
if config.debug:
|
71
|
-
Console(stderr=True).print("[cyan]Debug: Processing include paths...[/cyan]")
|
72
|
-
for path in include:
|
73
|
-
full_path = config.workspace_dir / path
|
74
|
-
if not full_path.resolve().is_relative_to(config.workspace_dir):
|
75
|
-
error_text = Text(f"\nError: Path must be within workspace: {path}", style="red")
|
76
|
-
rich_print(error_text)
|
77
|
-
raise typer.Exit(1)
|
78
|
-
workset.add_scan_path(path, ScanType.PLAIN)
|
79
|
-
|
80
|
-
if recursive:
|
81
|
-
if config.debug:
|
82
|
-
Console(stderr=True).print("[cyan]Debug: Processing recursive paths...[/cyan]")
|
83
|
-
for path in recursive:
|
84
|
-
full_path = config.workspace_dir / path
|
85
|
-
if not path.is_dir():
|
86
|
-
error_text = Text(f"\nError: Recursive path must be a directory: {path} ", style="red")
|
87
|
-
rich_print(error_text)
|
88
|
-
raise typer.Exit(1)
|
89
|
-
if not full_path.resolve().is_relative_to(config.workspace_dir):
|
90
|
-
error_text = Text(f"\nError: Path must be within workspace: {path}", style="red")
|
91
|
-
rich_print(error_text)
|
92
|
-
raise typer.Exit(1)
|
93
|
-
workset.add_scan_path(path, ScanType.RECURSIVE)
|
94
|
-
|
95
|
-
# Validate skip_work usage
|
96
|
-
if skip_work:
|
97
|
-
# Check if any include or recursive paths are provided
|
98
|
-
if not include and not recursive:
|
99
|
-
error_text = Text("\nError: --skip-work requires at least one include path (-i or -r)", style="red")
|
100
|
-
rich_print(error_text)
|
101
|
-
raise typer.Exit(1)
|
102
|
-
# Remove root path from workset when skip_work is enabled
|
103
|
-
workset._scan_paths = [p for p in workset._scan_paths if p.path != Path(".")]
|
104
|
-
|
105
|
-
if test_cmd:
|
106
|
-
config.set_test_cmd(test_cmd)
|
107
|
-
|
108
|
-
# Refresh workset content before handling commands
|
109
|
-
workset.refresh()
|
110
|
-
|
111
|
-
if ask:
|
112
|
-
if not user_request:
|
113
|
-
error_text = Text("\nError: No question provided. Please provide a question as the main argument when using --ask", style="red")
|
114
|
-
rich_print(error_text)
|
115
|
-
raise typer.Exit(1)
|
116
|
-
handle_ask(user_request)
|
117
|
-
elif play:
|
118
|
-
handle_play(play)
|
119
|
-
elif scan:
|
120
|
-
handle_scan()
|
121
|
-
else:
|
122
|
-
handle_request(user_request, replay=replay)
|
123
|
-
|
124
|
-
def main():
|
125
|
-
typer.run(typer_main)
|
148
|
+
console.print("[bold yellow]No query provided. Exiting.[/bold yellow]")
|
126
149
|
|
127
150
|
if __name__ == "__main__":
|
128
|
-
|
151
|
+
app()
|
janito/callbacks.py
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
"""
|
2
|
+
Callback functions for tool execution in janito.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from typing import Dict, Any, Tuple
|
6
|
+
from rich.console import Console
|
7
|
+
from rich.markdown import Markdown
|
8
|
+
|
9
|
+
from janito.config import get_config
|
10
|
+
from janito.tools import find_files
|
11
|
+
from janito.tools.str_replace_editor.editor import str_replace_editor
|
12
|
+
from janito.tools.delete_file import delete_file
|
13
|
+
from janito.tools.search_text import search_text
|
14
|
+
from janito.tools.decorators import format_tool_label
|
15
|
+
|
16
|
+
def pre_tool_callback(tool_name: str, tool_input: Dict[str, Any], preamble_text: str = "") -> Tuple[Dict[str, Any], bool]:
|
17
|
+
"""
|
18
|
+
Callback function that runs before a tool is executed.
|
19
|
+
|
20
|
+
Args:
|
21
|
+
tool_name: Name of the tool being called
|
22
|
+
tool_input: Input parameters for the tool
|
23
|
+
preamble_text: Any text generated before the tool call
|
24
|
+
|
25
|
+
Returns:
|
26
|
+
Tuple of (modified tool input, whether to cancel the tool call)
|
27
|
+
"""
|
28
|
+
console = Console()
|
29
|
+
|
30
|
+
# Add debug counter only when debug mode is enabled
|
31
|
+
if get_config().debug_mode:
|
32
|
+
if not hasattr(pre_tool_callback, "counter"):
|
33
|
+
pre_tool_callback.counter = 1
|
34
|
+
console.print(f"[bold yellow]DEBUG: Starting tool call #{pre_tool_callback.counter}[/bold yellow]")
|
35
|
+
pre_tool_callback.counter += 1
|
36
|
+
|
37
|
+
# Print preamble text with enhanced markdown support if provided
|
38
|
+
if preamble_text:
|
39
|
+
# Use a single print statement to avoid extra newlines
|
40
|
+
console.print("[bold magenta]Janito:[/bold magenta] ", Markdown(preamble_text, code_theme="monokai"), end="")
|
41
|
+
|
42
|
+
# Try to find the tool function
|
43
|
+
tool_func = None
|
44
|
+
for tool in [find_files, str_replace_editor, delete_file, search_text]:
|
45
|
+
if tool.__name__ == tool_name:
|
46
|
+
tool_func = tool
|
47
|
+
break
|
48
|
+
|
49
|
+
# Create a copy of tool_input to modify for display
|
50
|
+
display_input = {}
|
51
|
+
|
52
|
+
# Maximum length for string values
|
53
|
+
max_length = 50
|
54
|
+
|
55
|
+
# Trim long string values for display
|
56
|
+
for key, value in tool_input.items():
|
57
|
+
if isinstance(value, str) and len(value) > max_length:
|
58
|
+
# For long strings, show first and last part with ellipsis in between
|
59
|
+
display_input[key] = f"{value[:20]}...{value[-20:]}" if len(value) > 45 else value[:max_length] + "..."
|
60
|
+
else:
|
61
|
+
display_input[key] = value
|
62
|
+
|
63
|
+
# If we found the tool and it has a tool_meta label, use that for display
|
64
|
+
if tool_func:
|
65
|
+
formatted_label = format_tool_label(tool_func, tool_input)
|
66
|
+
if formatted_label:
|
67
|
+
console.print("[bold cyan] Tool:[/bold cyan]", formatted_label, end=" → ")
|
68
|
+
else:
|
69
|
+
console.print("[bold cyan] Tool:[/bold cyan]", f"{tool_name} {display_input}", end=" → ")
|
70
|
+
|
71
|
+
return tool_input, True # Continue with the tool call
|
72
|
+
|
73
|
+
def post_tool_callback(tool_name: str, tool_input: Dict[str, Any], result: Any) -> Any:
|
74
|
+
"""
|
75
|
+
Callback function that runs after a tool is executed.
|
76
|
+
|
77
|
+
Args:
|
78
|
+
tool_name: Name of the tool that was called
|
79
|
+
tool_input: Input parameters for the tool
|
80
|
+
result: Result of the tool call
|
81
|
+
|
82
|
+
Returns:
|
83
|
+
Modified result
|
84
|
+
"""
|
85
|
+
console = Console()
|
86
|
+
|
87
|
+
# Add debug counter only when debug mode is enabled
|
88
|
+
if get_config().debug_mode:
|
89
|
+
if not hasattr(post_tool_callback, "counter"):
|
90
|
+
post_tool_callback.counter = 1
|
91
|
+
console.print(f"[bold green]DEBUG: Completed tool call #{post_tool_callback.counter}[/bold green]")
|
92
|
+
post_tool_callback.counter += 1
|
93
|
+
|
94
|
+
# Extract the last line of the result
|
95
|
+
if isinstance(result, tuple) and len(result) >= 1:
|
96
|
+
content, is_error = result
|
97
|
+
# Define prefix icon based on is_error
|
98
|
+
icon_prefix = "❌ " if is_error else "✅ "
|
99
|
+
|
100
|
+
if isinstance(content, str):
|
101
|
+
# For find_files, extract just the count from the last line
|
102
|
+
if tool_name == "find_files" and content.count("\n") > 0:
|
103
|
+
lines = content.strip().split('\n')
|
104
|
+
if lines and lines[-1].isdigit():
|
105
|
+
console.print(f"{icon_prefix}{lines[-1]}")
|
106
|
+
else:
|
107
|
+
# Get the last line
|
108
|
+
last_line = content.strip().split('\n')[-1]
|
109
|
+
console.print(f"{icon_prefix}{last_line}")
|
110
|
+
else:
|
111
|
+
# For other tools, just get the last line
|
112
|
+
if '\n' in content:
|
113
|
+
last_line = content.strip().split('\n')[-1]
|
114
|
+
console.print(f"{icon_prefix}{last_line}")
|
115
|
+
else:
|
116
|
+
console.print(f"{icon_prefix}{content}")
|
117
|
+
else:
|
118
|
+
console.print(f"{icon_prefix}{content}")
|
119
|
+
else:
|
120
|
+
# If result is not a tuple, convert to string and get the last line
|
121
|
+
result_str = str(result)
|
122
|
+
# Default to success icon when no error status is available
|
123
|
+
icon_prefix = "✅ "
|
124
|
+
if '\n' in result_str:
|
125
|
+
last_line = result_str.strip().split('\n')[-1]
|
126
|
+
console.print(f"{icon_prefix}{last_line}")
|
127
|
+
else:
|
128
|
+
console.print(f"{icon_prefix}{result_str}")
|
129
|
+
|
130
|
+
return result
|
janito/cli.py
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
"""
|
2
|
+
CLI functionality for the janito tool.
|
3
|
+
"""
|
4
|
+
|
5
|
+
import os
|
6
|
+
import sys
|
7
|
+
from pathlib import Path
|
8
|
+
from typing import Optional
|
9
|
+
import typer
|
10
|
+
from rich.console import Console
|
11
|
+
from rich.markdown import Markdown
|
12
|
+
from rich import print as rprint
|
13
|
+
import claudine
|
14
|
+
from claudine.exceptions import MaxTokensExceededException, MaxRoundsExceededException
|
15
|
+
|
16
|
+
from janito.config import get_config
|
17
|
+
from janito.tools import find_files
|
18
|
+
from janito.tools.str_replace_editor.editor import str_replace_editor
|
19
|
+
from janito.tools.delete_file import delete_file
|
20
|
+
from janito.tools.search_text import search_text
|
21
|
+
from janito.callbacks import pre_tool_callback, post_tool_callback
|
22
|
+
|
23
|
+
app = typer.Typer(help="Janito CLI tool")
|
24
|
+
|
25
|
+
@app.command()
|
26
|
+
def hello(name: str = typer.Argument("World", help="Name to greet")):
|
27
|
+
"""
|
28
|
+
Say hello to someone.
|
29
|
+
"""
|
30
|
+
rprint(f"[bold green]Hello {name}[/bold green]")
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
def debug_tokens(agent):
|
35
|
+
"""
|
36
|
+
Display detailed token usage and pricing information.
|
37
|
+
"""
|
38
|
+
from claudine.token_tracking import MODEL_PRICING, DEFAULT_MODEL
|
39
|
+
|
40
|
+
console = Console()
|
41
|
+
usage = agent.get_token_usage()
|
42
|
+
text_usage = usage.text_usage
|
43
|
+
tools_usage = usage.tools_usage
|
44
|
+
total_usage = usage.total_usage
|
45
|
+
|
46
|
+
# Get the pricing model
|
47
|
+
pricing = MODEL_PRICING.get(DEFAULT_MODEL)
|
48
|
+
|
49
|
+
# Calculate costs manually
|
50
|
+
text_input_cost = pricing.input_tokens.calculate_cost(text_usage.input_tokens)
|
51
|
+
text_output_cost = pricing.output_tokens.calculate_cost(text_usage.output_tokens)
|
52
|
+
tools_input_cost = pricing.input_tokens.calculate_cost(tools_usage.input_tokens)
|
53
|
+
tools_output_cost = pricing.output_tokens.calculate_cost(tools_usage.output_tokens)
|
54
|
+
|
55
|
+
# Format costs
|
56
|
+
format_cost = lambda cost: f"{cost * 100:.2f}¢" if cost < 1.0 else f"${cost:.6f}"
|
57
|
+
|
58
|
+
console.print("\n[bold blue]Detailed Token Usage:[/bold blue]")
|
59
|
+
console.print(f"Text Input tokens: {text_usage.input_tokens}")
|
60
|
+
console.print(f"Text Output tokens: {text_usage.output_tokens}")
|
61
|
+
console.print(f"Text Total tokens: {text_usage.input_tokens + text_usage.output_tokens}")
|
62
|
+
console.print(f"Tool Input tokens: {tools_usage.input_tokens}")
|
63
|
+
console.print(f"Tool Output tokens: {tools_usage.output_tokens}")
|
64
|
+
console.print(f"Tool Total tokens: {tools_usage.input_tokens + tools_usage.output_tokens}")
|
65
|
+
console.print(f"Total tokens: {total_usage.input_tokens + total_usage.output_tokens}")
|
66
|
+
|
67
|
+
console.print("\n[bold blue]Pricing Information:[/bold blue]")
|
68
|
+
console.print(f"Input pricing: ${pricing.input_tokens.cost_per_million_tokens}/million tokens")
|
69
|
+
console.print(f"Output pricing: ${pricing.output_tokens.cost_per_million_tokens}/million tokens")
|
70
|
+
console.print(f"Text Input cost: {format_cost(text_input_cost)}")
|
71
|
+
console.print(f"Text Output cost: {format_cost(text_output_cost)}")
|
72
|
+
console.print(f"Text Total cost: {format_cost(text_input_cost + text_output_cost)}")
|
73
|
+
console.print(f"Tool Input cost: {format_cost(tools_input_cost)}")
|
74
|
+
console.print(f"Tool Output cost: {format_cost(tools_output_cost)}")
|
75
|
+
console.print(f"Tool Total cost: {format_cost(tools_input_cost + tools_output_cost)}")
|
76
|
+
console.print(f"Total cost: {format_cost(text_input_cost + text_output_cost + tools_input_cost + tools_output_cost)}")
|
77
|
+
|
78
|
+
# Display per-tool breakdown if available
|
79
|
+
if usage.by_tool:
|
80
|
+
console.print("\n[bold blue]Per-Tool Breakdown:[/bold blue]")
|
81
|
+
for tool_name, tool_usage in usage.by_tool.items():
|
82
|
+
tool_input_cost = pricing.input_tokens.calculate_cost(tool_usage.input_tokens)
|
83
|
+
tool_output_cost = pricing.output_tokens.calculate_cost(tool_usage.output_tokens)
|
84
|
+
console.print(f" Tool: {tool_name}")
|
85
|
+
console.print(f" Input tokens: {tool_usage.input_tokens}")
|
86
|
+
console.print(f" Output tokens: {tool_usage.output_tokens}")
|
87
|
+
console.print(f" Total tokens: {tool_usage.input_tokens + tool_usage.output_tokens}")
|
88
|
+
console.print(f" Total cost: {format_cost(tool_input_cost + tool_output_cost)}")
|
89
|
+
|
90
|
+
def process_query(query: str, debug: bool, verbose: bool):
|
91
|
+
"""
|
92
|
+
Process a query using the claudine agent.
|
93
|
+
"""
|
94
|
+
console = Console()
|
95
|
+
|
96
|
+
# Get API key from environment variable or ask the user
|
97
|
+
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
98
|
+
if not api_key:
|
99
|
+
console.print("[bold yellow]Warning:[/bold yellow] ANTHROPIC_API_KEY environment variable not set.")
|
100
|
+
console.print("Please set it or provide your API key now:")
|
101
|
+
api_key = typer.prompt("Anthropic API Key", hide_input=True)
|
102
|
+
|
103
|
+
# Load instructions from file
|
104
|
+
import importlib.resources as pkg_resources
|
105
|
+
try:
|
106
|
+
# For Python 3.9+
|
107
|
+
try:
|
108
|
+
from importlib.resources import files
|
109
|
+
instructions = files('janito.data').joinpath('instructions.txt').read_text()
|
110
|
+
# Fallback for older Python versions
|
111
|
+
except (ImportError, AttributeError):
|
112
|
+
instructions = pkg_resources.read_text('janito.data', 'instructions.txt')
|
113
|
+
instructions = instructions.strip()
|
114
|
+
except Exception as e:
|
115
|
+
console.print(f"[bold yellow]Warning:[/bold yellow] Could not load instructions file: {str(e)}")
|
116
|
+
console.print("[dim]Using default instructions instead.[/dim]")
|
117
|
+
instructions = "You are a helpful AI assistant. Answer the user's questions to the best of your ability."
|
118
|
+
|
119
|
+
# Initialize the agent with the tools
|
120
|
+
agent = claudine.Agent(
|
121
|
+
api_key=api_key,
|
122
|
+
tools=[
|
123
|
+
delete_file,
|
124
|
+
find_files,
|
125
|
+
search_text
|
126
|
+
],
|
127
|
+
text_editor_tool=str_replace_editor,
|
128
|
+
tool_callbacks=(pre_tool_callback, post_tool_callback),
|
129
|
+
max_tokens=4096,
|
130
|
+
temperature=0.7,
|
131
|
+
instructions=instructions,
|
132
|
+
debug_mode=debug # Enable debug mode
|
133
|
+
)
|
134
|
+
|
135
|
+
# Process the query
|
136
|
+
console.print(f"[bold blue]Query:[/bold blue] {query}")
|
137
|
+
console.print("[bold blue]Generating response...[/bold blue]")
|
138
|
+
|
139
|
+
try:
|
140
|
+
response = agent.process_prompt(query)
|
141
|
+
|
142
|
+
console.print("\n[bold magenta]Janito:[/bold magenta] ", end="")
|
143
|
+
# Use rich's enhanced Markdown rendering for the response
|
144
|
+
console.print(Markdown(response, code_theme="monokai"))
|
145
|
+
|
146
|
+
except MaxTokensExceededException as e:
|
147
|
+
# Display the partial response if available
|
148
|
+
if e.response_text:
|
149
|
+
console.print("\n[bold magenta]Janito:[/bold magenta] ", end="")
|
150
|
+
console.print(Markdown(e.response_text, code_theme="monokai"))
|
151
|
+
|
152
|
+
console.print("\n[bold red]Error:[/bold red] Response was truncated because it reached the maximum token limit.")
|
153
|
+
console.print("[dim]Consider increasing the max_tokens parameter or simplifying your query.[/dim]")
|
154
|
+
|
155
|
+
except MaxRoundsExceededException as e:
|
156
|
+
# Display the final response if available
|
157
|
+
if e.response_text:
|
158
|
+
console.print("\n[bold magenta]Janito:[/bold magenta] ", end="")
|
159
|
+
console.print(Markdown(e.response_text, code_theme="monokai"))
|
160
|
+
|
161
|
+
console.print(f"\n[bold red]Error:[/bold red] Maximum number of tool execution rounds ({e.rounds}) reached. Some tasks may be incomplete.")
|
162
|
+
console.print("[dim]Consider increasing the max_rounds parameter or breaking down your task into smaller steps.[/dim]")
|
163
|
+
|
164
|
+
# Show token usage
|
165
|
+
usage = agent.get_token_usage()
|
166
|
+
text_usage = usage.text_usage
|
167
|
+
tools_usage = usage.tools_usage
|
168
|
+
|
169
|
+
if verbose:
|
170
|
+
debug_tokens(agent)
|
171
|
+
else:
|
172
|
+
total_tokens = text_usage.input_tokens + text_usage.output_tokens + tools_usage.input_tokens + tools_usage.output_tokens
|
173
|
+
cost_info = agent.get_cost()
|
174
|
+
cost_display = cost_info.format_total_cost() if hasattr(cost_info, 'format_total_cost') else ""
|
175
|
+
# Consolidated tokens and cost in a single line with a ruler
|
176
|
+
console.print(Rule(f"Tokens: {total_tokens} | Cost: {cost_display}", style="dim", align="center"))
|
177
|
+
|
178
|
+
@app.callback(invoke_without_command=True)
|
179
|
+
def main(ctx: typer.Context,
|
180
|
+
query: Optional[str] = typer.Argument(None, help="Query to send to the claudine agent"),
|
181
|
+
debug: bool = typer.Option(False, "--debug", "-d", help="Enable debug mode"),
|
182
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed token usage and pricing information"),
|
183
|
+
workspace: Optional[str] = typer.Option(None, "--workspace", "-w", help="Set the workspace directory")):
|
184
|
+
"""
|
185
|
+
Janito CLI tool. If a query is provided without a command, it will be sent to the claudine agent.
|
186
|
+
"""
|
187
|
+
console = Console()
|
188
|
+
|
189
|
+
# Set debug mode in config
|
190
|
+
get_config().debug_mode = debug
|
191
|
+
|
192
|
+
if workspace:
|
193
|
+
try:
|
194
|
+
print(f"Setting workspace directory to: {workspace}")
|
195
|
+
get_config().workspace_dir = workspace
|
196
|
+
print(f"Workspace directory set to: {get_config().workspace_dir}")
|
197
|
+
except ValueError as e:
|
198
|
+
console.print(f"[bold red]Error:[/bold red] {str(e)}")
|
199
|
+
sys.exit(1)
|
200
|
+
|
201
|
+
if ctx.invoked_subcommand is None and query:
|
202
|
+
process_query(query, debug, verbose)
|