Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion minicline/completion/run_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def run_completion(
get_url,
headers={"Authorization": f"Bearer {api_key}"})
response2_json = response2.json()
if "error" in response2_json:
if "error" in response2_json or 'data' not in response2_json:
sleep(1)
total_cost += response2_json["data"]["total_cost"]
prompt_tokens = completion["usage"]["prompt_tokens"]
Expand Down
18 changes: 14 additions & 4 deletions minicline/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def execute_tool(tool_name: str, params: Dict[str, Any], cwd: str, auto: bool, a
return summary, text, None, True, 0, 0

if tool_name == "read_image":
summary, text, image_data_url, pt, ct = read_image(params['path'], vision_model=vision_model, instructions=params.get('instructions', None), cwd=cwd)
return summary, text, image_data_url, True, pt, ct
summary, text, image_data_url, pt, ct, cost = read_image(params['path'], vision_model=vision_model, instructions=params.get('instructions', None), cwd=cwd)
return summary, text, image_data_url, True, pt, ct, cost

elif tool_name == "write_to_file":
summary, text = write_to_file(
Expand Down Expand Up @@ -129,6 +129,8 @@ def execute_tool(tool_name: str, params: Dict[str, Any], cwd: str, auto: bool, a
return summary, text, None, True, 0, 0

elif tool_name == "execute_command":
print(docker_container)

timeout = int(params.get('timeout', 60)) # Default to 60 seconds if not provided
summary, text = execute_command(
params['command'],
Expand All @@ -150,7 +152,8 @@ def execute_tool(tool_name: str, params: Dict[str, Any], cwd: str, auto: bool, a
cwd=cwd,
auto=auto,
approve_all_commands=approve_all_commands,
no_container=no_container
no_container=no_container,
docker_container=docker_container
)
return summary, text, None, True, 0, 0

Expand Down Expand Up @@ -303,9 +306,16 @@ def perform_task(instructions: str, *, cwd: str | None = None, model: str | None
print(f"\nTool: {tool_name}")
print(f"Params: {params}")

tool_call_summary, tool_result_text, image_data_url, handled, additional_vision_prompt_tokens, additional_vision_completion_tokens = execute_tool(tool_name, params, cwd, auto=auto, approve_all_commands=approve_all_commands, vision_model=vision_model, no_container=no_container, docker_container=docker_container)
tool_output = execute_tool(tool_name, params, cwd, auto=auto, approve_all_commands=approve_all_commands,
vision_model=vision_model, no_container=no_container, docker_container=docker_container)
tool_cost = 0
if len(tool_output) == 7:
tool_call_summary, tool_result_text, image_data_url, handled, additional_vision_prompt_tokens, additional_vision_completion_tokens, tool_cost = tool_output
else:
tool_call_summary, tool_result_text, image_data_url, handled, additional_vision_prompt_tokens, additional_vision_completion_tokens = tool_output
total_vision_prompt_tokens += additional_vision_prompt_tokens
total_vision_completion_tokens += additional_vision_completion_tokens
total_cost += tool_cost
if not handled:
num_consecutive_failures += 1
if num_consecutive_failures > 3:
Expand Down
5 changes: 3 additions & 2 deletions minicline/tools/execute_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .execute_command import execute_command

def execute_script(script: str, language: str, requires_approval: bool, *, cwd: str, auto: bool, approve_all_commands: bool, no_container: bool) -> Tuple[str, str]:
def execute_script(script: str, language: str, requires_approval: bool, *, cwd: str, auto: bool, approve_all_commands: bool, no_container: bool, docker_container: str | None = None) -> Tuple[str, str]:
"""Execute a script by writing it to a temporary file and running it.

Args:
Expand Down Expand Up @@ -50,7 +50,8 @@ def execute_script(script: str, language: str, requires_approval: bool, *, cwd:
cwd=cwd,
auto=auto,
approve_all_commands=approve_all_commands,
no_container=no_container
no_container=no_container,
docker_container=docker_container
)

finally:
Expand Down
14 changes: 8 additions & 6 deletions minicline/tools/read_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ def read_image(path: str, *, instructions: Union[str, None], cwd: str, vision_mo
# Convert to absolute path if relative
file_path = Path(cwd) / path

cost = 0
# Read and return contents
with open(file_path, 'rb') as f:
with (open(file_path, 'rb') as f):
data = f.read()
data_base64 = base64.b64encode(data).decode('utf-8')
data_url = f"data:image/png;base64,{data_base64}"
if vision_model:
ai_description, prompt_tokens, completion_tokens = _get_ai_description(data_url, vision_model=vision_model, instructions=instructions)
ai_description, prompt_tokens, completion_tokens, cost = \
_get_ai_description(data_url, vision_model=vision_model, instructions=instructions)
else:
ai_description = None
text = f'The image for {rel_file_path} is attached.'
if ai_description:
text += f' AI description: {ai_description}'
return tool_call_summary, text, data_url, prompt_tokens, completion_tokens
return tool_call_summary, text, data_url, prompt_tokens, completion_tokens, cost

except Exception as e:
return tool_call_summary, f"ERROR READING FILE {path}: {str(e)}", None, prompt_tokens, completion_tokens
return tool_call_summary, f"ERROR READING FILE {path}: {str(e)}", None, prompt_tokens, completion_tokens, cost


def _get_ai_description(data_url: str, *, vision_model: str, instructions: Union[str, None]) -> Tuple[str, int, int]:
Expand Down Expand Up @@ -79,5 +81,5 @@ def _get_ai_description(data_url: str, *, vision_model: str, instructions: Union
"content": content
}
]
content, _, prompt_tokens, completion_tokens = run_completion(messages, model=vision_model) # type: ignore
return content, prompt_tokens, completion_tokens # type: ignore
content, _, prompt_tokens, completion_tokens, total_cost = run_completion(messages, model=vision_model) # type: ignore
return content, prompt_tokens, completion_tokens, total_cost # type: ignore