Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4103faa

Browse files
committed
fix match agentapi responses
1 parent4c80700 commit4103faa

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

‎coderd/aitasks.go‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,11 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
637637
}
638638

639639
varreqBodystruct {
640-
Bodystruct {
641-
Contentstring`json:"content"`
642-
Typestring`json:"type"`
643-
}`json:"body"`
640+
Contentstring`json:"content"`
641+
Typestring`json:"type"`
644642
}
645-
reqBody.Body.Content=req.Input
646-
reqBody.Body.Type="user"
643+
reqBody.Content=req.Input
644+
reqBody.Type="user"
647645

648646
req,err:=agentapiNewRequest(ctx,http.MethodPost,appURL,"message",reqBody)
649647
iferr!=nil {
@@ -666,6 +664,24 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
666664
Detail:fmt.Sprintf("Upstream status: %d; Body: %s",resp.StatusCode,body),
667665
})
668666
}
667+
668+
// {"$schema":"http://localhost:3284/schemas/MessageResponseBody.json","ok":true}
669+
// {"$schema":"http://localhost:3284/schemas/ErrorModel.json","title":"Unprocessable Entity","status":422,"detail":"validation failed","errors":[{"location":"body.type","value":"oof"}]}
670+
varrespBodymap[string]any
671+
iferr:=json.NewDecoder(resp.Body).Decode(&respBody);err!=nil {
672+
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
673+
Message:"Failed to decode task app response body.",
674+
Detail:err.Error(),
675+
})
676+
}
677+
678+
ifv,ok:=respBody["status"].(string);!ok||v!="ok" {
679+
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
680+
Message:"Task app rejected the message.",
681+
Detail:fmt.Sprintf("Upstream response: %v",respBody),
682+
})
683+
}
684+
669685
returnnil
670686
});err!=nil {
671687
httperror.WriteResponseError(ctx,rw,err)
@@ -851,10 +867,9 @@ func agentapiDoStatusRequest(ctx context.Context, client *http.Client, appURL *u
851867
})
852868
}
853869

870+
// {"$schema":"http://localhost:3284/schemas/StatusResponseBody.json","status":"stable"}
854871
varrespBodystruct {
855-
Bodystruct {
856-
Statusstring`json:"status"`
857-
}`json:"body"`
872+
Statusstring`json:"status"`
858873
}
859874

860875
iferr:=json.NewDecoder(resp.Body).Decode(&respBody);err!=nil {
@@ -864,5 +879,5 @@ func agentapiDoStatusRequest(ctx context.Context, client *http.Client, appURL *u
864879
})
865880
}
866881

867-
returnrespBody.Body.Status,nil
882+
returnrespBody.Status,nil
868883
}

‎coderd/aitasks_test.go‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,32 @@ func TestTasks(t *testing.T) {
423423
owner:=coderdtest.CreateFirstUser(t,client)
424424
userClient,_:=coderdtest.CreateAnotherUser(t,client,owner.OrganizationID)
425425

426+
createStatusResponse:=func(statusstring)string {
427+
return`
428+
{
429+
"$schema": "http://localhost:3284/schemas/StatusResponseBody.json",
430+
"status": "`+status+`"
431+
}
432+
`
433+
}
434+
statusResponse:=createStatusResponse("stable")
435+
426436
// Start a fake AgentAPI that accepts GET /status and POST /message.
427-
statusResponse:=`{"body":{"status":"stable"}}`
428437
srv:=httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
429438
ifr.Method==http.MethodGet&&r.URL.Path=="/status" {
430-
_,_=fmt.Fprint(w,statusResponse)
439+
w.Header().Set("Content-Type","application/json")
431440
w.WriteHeader(http.StatusOK)
441+
_,_=fmt.Fprint(w,statusResponse)
432442
return
433443
}
434444
ifr.Method==http.MethodPost&&r.URL.Path=="/message" {
445+
w.Header().Set("Content-Type","application/json")
446+
447+
b,_:=io.ReadAll(r.Body)
448+
assert.Equal(t,`{"content":"Hello, Agent!","type":"user"}`,string(b),"expected message content")
449+
435450
w.WriteHeader(http.StatusOK)
451+
io.WriteString(w,`{"status": "ok"}`)
436452
return
437453
}
438454
w.WriteHeader(http.StatusInternalServerError)
@@ -495,14 +511,14 @@ func TestTasks(t *testing.T) {
495511
})
496512
require.NoError(t,err)
497513

498-
statusResponse=`{"body":{"status":"bad"}}`
514+
statusResponse=createStatusResponse("bad")
499515

500516
err=exp.TaskSend(ctx,"me",ws.ID, codersdk.TaskSendRequest{
501517
Input:"Hello, Agent!",
502518
})
503519
require.Error(t,err,"wanted error due to bad status")
504520

505-
statusResponse=`{"body":{"status":"stable"}}`
521+
statusResponse=createStatusResponse("stable")
506522

507523
// Send task input to the tasks sidebar app and expect 204.e
508524
err=exp.TaskSend(ctx,"me",ws.ID, codersdk.TaskSendRequest{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp