Skip to content

Commit 94f5545

Browse files
author
kevin
committed
fix(cli): improve error handling and fix assistant update bug
1 parent dbe8194 commit 94f5545

5 files changed

Lines changed: 34 additions & 20 deletions

File tree

dashscope/cli/assistants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def update_assistant(
271271
description=description,
272272
instructions=instructions,
273273
tools=_parse_json_array(tools, "tools"),
274-
file_ids=file_ids or [],
274+
file_ids=file_ids,
275275
metadata=_parse_json_object(metadata, "metadata"),
276276
workspace=workspace,
277277
top_p=top_p,

dashscope/cli/files.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ def upload(
6464
if not os.path.exists(file_path):
6565
error(f"File {file_path} does not exist")
6666

67-
try:
68-
rsp = dashscope.Files.upload(
69-
file_path=file_path,
70-
purpose=purpose,
71-
description=description, # type: ignore[arg-type]
72-
base_address=base_url,
73-
)
74-
except Exception as exception:
75-
error(str(exception))
67+
rsp = dashscope.Files.upload(
68+
file_path=file_path,
69+
purpose=purpose,
70+
description=description, # type: ignore[arg-type]
71+
base_address=base_url,
72+
)
7673
output = ensure_ok(rsp)
7774
file_id = output["uploaded_files"][0]["file_id"]
7875
success(f"Upload success, file id: {file_id}")

dashscope/cli/generation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from dashscope.aigc import Generation
99
from dashscope.cli.common import (
10+
error,
1011
handle_sdk_error,
1112
print_failed_message,
1213
)
@@ -42,10 +43,7 @@ def _build_generation_kwargs(
4243
try:
4344
kwargs["messages"] = json.loads(messages)
4445
except json.JSONDecodeError as exc:
45-
typer.echo(
46-
"Error: --messages must be a valid JSON string",
47-
err=True,
48-
)
46+
error("--messages must be a valid JSON string")
4947
raise typer.Exit(1) from exc
5048

5149
# Group simple parameters to reduce branches

tests/unit/test_cli_main.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) Alibaba, Inc. and its affiliates.
33

44
import os
5+
import re
56
import subprocess
67
import sys
78
from types import SimpleNamespace
@@ -16,6 +17,12 @@
1617
from dashscope.common.error import AuthenticationError
1718

1819

20+
def strip_ansi_codes(text):
21+
"""Remove ANSI escape codes from text."""
22+
ansi_escape = re.compile(r"\x1b\[[0-9;]*m")
23+
return ansi_escape.sub("", text)
24+
25+
1926
# pylint: disable=too-many-public-methods
2027
class TestCliMain:
2128
def test_main_prints_authentication_error_without_traceback(
@@ -46,7 +53,8 @@ def test_top_level_help_shows_global_api_key(self):
4653
result = CliRunner().invoke(cli_app, ["--help"])
4754

4855
assert result.exit_code == 0
49-
assert "--api-key" in result.output
56+
clean_output = strip_ansi_codes(result.output)
57+
assert "--api-key" in clean_output
5058

5159
def test_python_module_help_suppresses_urllib3_warning(self):
5260
result = subprocess.run(
@@ -84,7 +92,8 @@ def test_rl_register_functions_hyphen_alias_help(self):
8492
)
8593

8694
assert result.exit_code == 0
87-
assert "rollout-classpaths" in result.output
95+
clean_output = strip_ansi_codes(result.output)
96+
assert "rollout-classpaths" in clean_output
8897

8998
def test_rl_test_functions_hyphen_alias_help(self):
9099
result = CliRunner().invoke(
@@ -93,7 +102,8 @@ def test_rl_test_functions_hyphen_alias_help(self):
93102
)
94103

95104
assert result.exit_code == 0
96-
assert "--input" in result.output
105+
clean_output = strip_ansi_codes(result.output)
106+
assert "--input" in clean_output
97107

98108
def test_rl_upload_data_hyphen_alias_help(self):
99109
result = CliRunner().invoke(
@@ -102,7 +112,8 @@ def test_rl_upload_data_hyphen_alias_help(self):
102112
)
103113

104114
assert result.exit_code == 0
105-
assert "training-files" in result.output
115+
clean_output = strip_ansi_codes(result.output)
116+
assert "training-files" in clean_output
106117

107118
def test_missing_global_api_key_value_does_not_consume_command(
108119
self,

tests/unit/test_cli_runs.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# -*- coding: utf-8 -*-
22
# Copyright (c) Alibaba, Inc. and its affiliates.
33

4+
import re
45
from types import SimpleNamespace
56

67
from typer.testing import CliRunner
78

89
from dashscope.cli import runs
910

1011

12+
def strip_ansi_codes(text):
13+
"""Remove ANSI escape codes from text."""
14+
ansi_escape = re.compile(r"\x1b\[[0-9;]*m")
15+
return ansi_escape.sub("", text)
16+
17+
1118
class TestCliRuns:
1219
def test_create(self, monkeypatch):
1320
captured_thread_id = None
@@ -120,8 +127,9 @@ def test_get_requires_thread_id(self):
120127
result = CliRunner().invoke(runs.app, ["get", "run-1234"])
121128

122129
assert result.exit_code != 0
123-
assert "Missing option" in result.output
124-
assert "--thread-id" in result.output
130+
clean_output = strip_ansi_codes(result.output)
131+
assert "Missing option" in clean_output
132+
assert "--thread-id" in clean_output
125133

126134
def test_get(self, monkeypatch):
127135
captured_run_id = None

0 commit comments

Comments
 (0)