Skip to content

Commit d35f8b9

Browse files
authored
Merge pull request #29 from chaitin/develop/jw
feat: 增加错误提示返回
2 parents ca90c4c + e1ffd47 commit d35f8b9

File tree

7 files changed

+657
-633
lines changed

7 files changed

+657
-633
lines changed

domain/domain.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Response struct {
2424

2525
type ModelListResp struct {
2626
Models []ModelListItem `json:"models"`
27+
Error string `json:"error"`
2728
}
2829

2930
type ModelListItem struct {
@@ -76,22 +77,22 @@ type ModelResponseParser interface {
7677
}
7778

7879
type GithubModel struct {
79-
ID string `json:"id"`
80-
Name string `json:"name"`
81-
Registry string `json:"registry"`
82-
Publisher string `json:"publisher"`
83-
Summary string `json:"summary"`
84-
RateLimitTier string `json:"rate_limit_tier"`
85-
HTMLURL string `json:"html_url"`
86-
Version string `json:"version"`
87-
Capabilities []string `json:"capabilities"`
88-
Limits struct {
80+
ID string `json:"id"`
81+
Name string `json:"name"`
82+
Registry string `json:"registry"`
83+
Publisher string `json:"publisher"`
84+
Summary string `json:"summary"`
85+
RateLimitTier string `json:"rate_limit_tier"`
86+
HTMLURL string `json:"html_url"`
87+
Version string `json:"version"`
88+
Capabilities []string `json:"capabilities"`
89+
Limits struct {
8990
MaxInputTokens int `json:"max_input_tokens"`
9091
MaxOutputTokens int `json:"max_output_tokens"`
9192
} `json:"limits"`
92-
Tags []string `json:"tags"`
93-
SupportedInputModalities []string `json:"supported_input_modalities"`
94-
SupportedOutputModalities []string `json:"supported_output_modalities"`
93+
Tags []string `json:"tags"`
94+
SupportedInputModalities []string `json:"supported_input_modalities"`
95+
SupportedOutputModalities []string `json:"supported_output_modalities"`
9596
}
9697

9798
type GithubResp []GithubModel

test/main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ func NewModelKit(
3232
func (p *ModelKit) GetModelList(c echo.Context) error {
3333
var req domain.ModelListReq
3434
if err := c.Bind(&req); err != nil {
35-
return c.JSON(http.StatusBadRequest, domain.Response{
35+
return c.JSON(http.StatusOK, domain.Response{
3636
Success: false,
3737
Message: "参数绑定失败: " + err.Error(),
38+
Data: nil,
3839
})
3940
}
4041
fmt.Println("list model req:", req)
4142

4243
resp, err := usecase.ModelList(c.Request().Context(), &req)
4344
if err != nil {
4445
fmt.Println("err:", err)
45-
return c.JSON(http.StatusInternalServerError, domain.Response{
46-
Success: false,
46+
return c.JSON(http.StatusOK, domain.Response{
47+
Success: true,
4748
Message: err.Error(),
49+
Data: resp,
4850
})
4951
}
5052

@@ -65,13 +67,21 @@ func (p *ModelKit) CheckModel(c echo.Context) error {
6567
}
6668
fmt.Println("check model req:", req)
6769

68-
resp, _ := usecase.CheckModel(c.Request().Context(), &req)
70+
resp, err := usecase.CheckModel(c.Request().Context(), &req)
71+
if err != nil {
72+
fmt.Println("err:", err)
73+
return c.JSON(http.StatusOK, domain.Response{
74+
Success: true,
75+
Message: err.Error(),
76+
Data: resp,
77+
})
78+
}
6979

7080
// 如果检查过程中有错误,返回错误响应
7181
if resp.Error != "" {
7282
fmt.Println("resp.Error:", resp.Error)
7383
return c.JSON(http.StatusOK, domain.Response{
74-
Success: false,
84+
Success: true,
7585
Message: resp.Error,
7686
Data: resp,
7787
})

test/ui_example/src/localService.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface ApiResponse<T = any> {
2222
// 模型列表响应
2323
interface ModelListResponse {
2424
models: ModelListItem[];
25+
error?: string;
2526
}
2627

2728
// 模型检查响应
@@ -75,8 +76,7 @@ export class LocalModelService implements ModelService {
7576
return result;
7677
}
7778

78-
async listModel(data: ListModelReq): Promise<{ models: ModelListItem[] }> {
79-
try {
79+
async listModel(data: ListModelReq): Promise<{ models: ModelListItem[]; error?: string }> {
8080
const queryParams = new URLSearchParams();
8181
if (data.provider) queryParams.append('provider', data.provider);
8282
if (data.model_type) queryParams.append('model_type', data.model_type);
@@ -88,37 +88,25 @@ export class LocalModelService implements ModelService {
8888
const response = await this.request<ModelListResponse>(url, {
8989
method: 'GET',
9090
});
91-
return { models: response.models };
92-
} catch (error) {
93-
console.error('Failed to list models:', error);
94-
throw error;
95-
}
91+
console.log('listModel response:', response);
92+
return { models: response.models, error: response.error };
9693
}
9794

9895
async checkModel(data: CheckModelReq): Promise<{ model: Model; error?: string }> {
99-
try {
10096
const queryParams = new URLSearchParams();
101-
console.log('checkModel data:', data);
10297
if (data.provider) queryParams.append('provider', data.provider);
10398
if (data.model_name) queryParams.append('model_name', data.model_name);
10499
if (data.base_url) queryParams.append('base_url', data.base_url);
105100
if (data.api_key) queryParams.append('api_key', data.api_key);
106101
if (data.api_header) queryParams.append('api_header', data.api_header);
107102
if (data.model_type) queryParams.append('model_type', data.model_type);
108103
const url = `/checkmodel${queryParams.toString() ? '?' + queryParams.toString() : ''}`;
104+
console.log('checkModel url:', url);
109105
const response = await this.request<CheckModelResponse>(url, {
110106
method: 'GET',
111107
});
112-
113-
if (response.error) {
114-
throw new Error(response.error);
115-
}
116-
117-
return { model: response.model };
118-
} catch (error) {
119-
console.error('Failed to list models:', error);
120-
throw error;
121-
}
108+
console.log('checkModel response:', response);
109+
return { model: response.model, error: response.error };
122110
}
123111

124112
async updateModel(data: UpdateModelReq): Promise<{ model: Model }> {

ui/ModelModal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yokowu/modelkit-ui",
3-
"version": "0.5.1",
3+
"version": "0.6.0",
44
"description": "A reusable AI model configuration modal component for React applications",
55
"private": false,
66
"type": "module",

0 commit comments

Comments
 (0)