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

Commitc37d9d0

Browse files
committed
add agentapi status check
1 parent6a4a085 commitc37d9d0

File tree

2 files changed

+96
-23
lines changed

2 files changed

+96
-23
lines changed

‎coderd/aitasks.go‎

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -623,34 +623,34 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
623623
return
624624
}
625625

626-
typemessagePayloadstruct {
627-
Contentstring`json:"content"`
628-
Typestring`json:"type"`
629-
}
630-
payload,err:=json.Marshal(messagePayload{Content:req.Input,Type:"user"})
631-
iferr!=nil {
632-
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
633-
Message:"Internal error encoding request payload.",
634-
Detail:err.Error(),
635-
})
636-
return
637-
}
638-
639626
iferr=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
640-
// Build the request to the agent local app (we expect agentapi on the other side).
641-
appReqURL:=appURL
642-
appReqURL.Path=path.Join(appURL.Path,"message")
643-
644-
newReq,err:=http.NewRequestWithContext(ctx,http.MethodPost,appReqURL.String(),bytes.NewReader(payload))
627+
status,err:=doAgentapiStatusRequest(ctx,client,appURL)
645628
iferr!=nil {
646-
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
647-
Message:"Internal error creating request to task app.",
648-
Detail:err.Error(),
629+
returnerr
630+
}
631+
632+
ifstatus!="stable" {
633+
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
634+
Message:"Task app is not ready to accept input.",
635+
Detail:fmt.Sprintf("Status: %s",status),
649636
})
650637
}
651-
newReq.Header.Set("Content-Type","application/json")
652638

653-
resp,err:=client.Do(newReq)
639+
varreqBodystruct {
640+
Bodystruct {
641+
Contentstring`json:"content"`
642+
Typestring`json:"type"`
643+
}`json:"body"`
644+
}
645+
reqBody.Body.Content=req.Input
646+
reqBody.Body.Type="user"
647+
648+
req,err:=newAgentapiRequest(ctx,http.MethodPost,appURL,"message",reqBody)
649+
iferr!=nil {
650+
returnerr
651+
}
652+
653+
resp,err:=client.Do(req)
654654
iferr!=nil {
655655
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
656656
Message:"Failed to reach task app endpoint.",
@@ -799,3 +799,70 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
799799
}
800800
returndo(ctx,client,parsedURL)
801801
}
802+
803+
funcnewAgentapiRequest(ctx context.Context,methodstring,appURL*url.URL,appURLPathstring,bodyany) (*http.Request,error) {
804+
u:=*appURL
805+
u.Path=path.Join(appURL.Path,appURLPath)
806+
807+
varbodyReader io.Reader
808+
ifbody!=nil {
809+
b,err:=json.Marshal(body)
810+
iferr!=nil {
811+
returnnil,httperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
812+
Message:"Failed to marshal task app request body.",
813+
Detail:err.Error(),
814+
})
815+
}
816+
bodyReader=bytes.NewReader(b)
817+
}
818+
819+
req,err:=http.NewRequestWithContext(ctx,method,u.String(),bodyReader)
820+
iferr!=nil {
821+
returnnil,httperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
822+
Message:"Failed to create task app request.",
823+
Detail:err.Error(),
824+
})
825+
}
826+
req.Header.Set("Content-Type","application/json")
827+
req.Header.Set("Accept","application/json")
828+
829+
returnreq,nil
830+
}
831+
832+
funcdoAgentapiStatusRequest(ctx context.Context,client*http.Client,appURL*url.URL) (string,error) {
833+
req,err:=newAgentapiRequest(ctx,http.MethodGet,appURL,"status",nil)
834+
iferr!=nil {
835+
return"",err
836+
}
837+
838+
resp,err:=client.Do(req)
839+
iferr!=nil {
840+
return"",httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
841+
Message:"Failed to reach task app endpoint.",
842+
Detail:err.Error(),
843+
})
844+
}
845+
deferresp.Body.Close()
846+
847+
ifresp.StatusCode!=http.StatusOK {
848+
return"",httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
849+
Message:"Task app status returned an error.",
850+
Detail:fmt.Sprintf("Status code: %d",resp.StatusCode),
851+
})
852+
}
853+
854+
varrespBodystruct {
855+
Bodystruct {
856+
Statusstring`json:"status"`
857+
}`json:"body"`
858+
}
859+
860+
iferr:=json.NewDecoder(resp.Body).Decode(&respBody);err!=nil {
861+
return"",httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
862+
Message:"Failed to decode task app status response body.",
863+
Detail:err.Error(),
864+
})
865+
}
866+
867+
returnrespBody.Body.Status,nil
868+
}

‎coderd/aitasks_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package coderd_test
22

33
import (
4+
"fmt"
45
"net/http"
56
"net/http/httptest"
67
"testing"
@@ -424,6 +425,11 @@ func TestTasks(t *testing.T) {
424425

425426
// Start a fake AgentAPI that accepts POST /message with 200 OK.
426427
srv:=httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
428+
ifr.Method==http.MethodGet&&r.URL.Path=="/status" {
429+
_,_=fmt.Fprintln(w,`{"body":{"status":"stable"}}`)
430+
w.WriteHeader(http.StatusOK)
431+
return
432+
}
427433
ifr.Method==http.MethodPost&&r.URL.Path=="/message" {
428434
w.WriteHeader(http.StatusOK)
429435
return

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp