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

Commite1619da

Browse files
authored
chore(coderd): update aitasks.go to leverage agentapi-sdk-go (#20159)
I only recently became aware of the existence of `agentapi-sdk-go`.Updates `aitasks.go` to make use of it.
1 parent2880582 commite1619da

File tree

1 file changed

+27
-141
lines changed

1 file changed

+27
-141
lines changed

‎coderd/aitasks.go‎

Lines changed: 27 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package coderd
22

33
import (
4-
"bytes"
54
"context"
65
"database/sql"
7-
"encoding/json"
86
"errors"
97
"fmt"
10-
"io"
118
"net"
129
"net/http"
1310
"net/url"
14-
"path"
1511
"slices"
1612
"strings"
1713
"time"
@@ -31,6 +27,8 @@ import (
3127
"github.com/coder/coder/v2/coderd/taskname"
3228
"github.com/coder/coder/v2/coderd/util/slice"
3329
"github.com/coder/coder/v2/codersdk"
30+
31+
aiagentapi"github.com/coder/agentapi-sdk-go"
3432
)
3533

3634
// This endpoint is experimental and not guaranteed to be stable, so we're not
@@ -629,61 +627,37 @@ func (api *API) taskSend(rw http.ResponseWriter, r *http.Request) {
629627
}
630628

631629
iferr=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
632-
status,err:=agentapiDoStatusRequest(ctx,client,appURL)
630+
agentAPIClient,err:=aiagentapi.NewClient(appURL.String(),aiagentapi.WithHTTPClient(client))
633631
iferr!=nil {
634-
returnerr
635-
}
636-
637-
ifstatus!="stable" {
638632
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
639-
Message:"Task app is not readytoaccept input.",
640-
Detail:fmt.Sprintf("Status: %s",status),
633+
Message:"Failedtocreate agentapi client.",
634+
Detail:err.Error(),
641635
})
642636
}
643637

644-
varreqBodystruct {
645-
Contentstring`json:"content"`
646-
Typestring`json:"type"`
647-
}
648-
reqBody.Content=req.Input
649-
reqBody.Type="user"
650-
651-
req,err:=agentapiNewRequest(ctx,http.MethodPost,appURL,"message",reqBody)
652-
iferr!=nil {
653-
returnerr
654-
}
655-
656-
resp,err:=client.Do(req)
638+
statusResp,err:=agentAPIClient.GetStatus(ctx)
657639
iferr!=nil {
658640
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
659-
Message:"Failed toreachtask app endpoint.",
641+
Message:"Failed toget status fromtask app.",
660642
Detail:err.Error(),
661643
})
662644
}
663-
deferresp.Body.Close()
664-
665-
ifresp.StatusCode!=http.StatusOK {
666-
body,_:=io.ReadAll(io.LimitReader(resp.Body,128))
667-
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
668-
Message:"Task app rejected the message.",
669-
Detail:fmt.Sprintf("Upstream status: %d; Body: %s",resp.StatusCode,body),
670-
})
671-
}
672645

673-
// {"$schema":"http://localhost:3284/schemas/MessageResponseBody.json","ok":true}
674-
// {"$schema":"http://localhost:3284/schemas/ErrorModel.json","title":"Unprocessable Entity","status":422,"detail":"validation failed","errors":[{"location":"body.type","value":"oof"}]}
675-
varrespBodymap[string]any
676-
iferr:=json.NewDecoder(resp.Body).Decode(&respBody);err!=nil {
646+
ifstatusResp.Status!=aiagentapi.StatusStable {
677647
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
678-
Message:"Failed to decode task app response body.",
679-
Detail:err.Error(),
648+
Message:"Task app is not ready to accept input.",
649+
Detail:fmt.Sprintf("Status: %s",statusResp.Status),
680650
})
681651
}
682652

683-
ifv,ok:=respBody["ok"].(bool);!ok||!v {
653+
_,err=agentAPIClient.PostMessage(ctx, aiagentapi.PostMessageParams{
654+
Content:req.Input,
655+
Type:aiagentapi.MessageTypeUser,
656+
})
657+
iferr!=nil {
684658
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
685659
Message:"Task app rejected the message.",
686-
Detail:fmt.Sprintf("Upstream response: %v",respBody),
660+
Detail:err.Error(),
687661
})
688662
}
689663

@@ -710,51 +684,29 @@ func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) {
710684

711685
varout codersdk.TaskLogsResponse
712686
iferr:=api.authAndDoWithTaskSidebarAppClient(r,taskID,func(ctx context.Context,client*http.Client,appURL*url.URL)error {
713-
req,err:=agentapiNewRequest(ctx,http.MethodGet,appURL,"messages",nil)
714-
iferr!=nil {
715-
returnerr
716-
}
717-
718-
resp,err:=client.Do(req)
687+
agentAPIClient,err:=aiagentapi.NewClient(appURL.String(),aiagentapi.WithHTTPClient(client))
719688
iferr!=nil {
720689
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
721-
Message:"Failed toreach task app endpoint.",
690+
Message:"Failed tocreate agentapi client.",
722691
Detail:err.Error(),
723692
})
724693
}
725-
deferresp.Body.Close()
726-
727-
ifresp.StatusCode!=http.StatusOK {
728-
body,_:=io.ReadAll(io.LimitReader(resp.Body,128))
729-
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
730-
Message:"Task app rejected the request.",
731-
Detail:fmt.Sprintf("Upstream status: %d; Body: %s",resp.StatusCode,body),
732-
})
733-
}
734694

735-
// {"$schema":"http://localhost:3284/schemas/MessagesResponseBody.json","messages":[]}
736-
varrespBodystruct {
737-
Messages []struct {
738-
IDint`json:"id"`
739-
Contentstring`json:"content"`
740-
Rolestring`json:"role"`
741-
Time time.Time`json:"time"`
742-
}`json:"messages"`
743-
}
744-
iferr:=json.NewDecoder(resp.Body).Decode(&respBody);err!=nil {
695+
messagesResp,err:=agentAPIClient.GetMessages(ctx)
696+
iferr!=nil {
745697
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
746-
Message:"Failed todecode task app response body.",
698+
Message:"Failed toget messages from task app.",
747699
Detail:err.Error(),
748700
})
749701
}
750702

751-
logs:=make([]codersdk.TaskLogEntry,0,len(respBody.Messages))
752-
for_,m:=rangerespBody.Messages {
703+
logs:=make([]codersdk.TaskLogEntry,0,len(messagesResp.Messages))
704+
for_,m:=rangemessagesResp.Messages {
753705
vartyp codersdk.TaskLogType
754-
switchstrings.ToLower(m.Role) {
755-
case"user":
706+
switchm.Role {
707+
caseaiagentapi.RoleUser:
756708
typ=codersdk.TaskLogTypeInput
757-
case"agent":
709+
caseaiagentapi.RoleAgent:
758710
typ=codersdk.TaskLogTypeOutput
759711
default:
760712
returnhttperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
@@ -763,7 +715,7 @@ func (api *API) taskLogs(rw http.ResponseWriter, r *http.Request) {
763715
})
764716
}
765717
logs=append(logs, codersdk.TaskLogEntry{
766-
ID:m.ID,
718+
ID:int(m.Id),
767719
Content:m.Content,
768720
Type:typ,
769721
Time:m.Time,
@@ -903,69 +855,3 @@ func (api *API) authAndDoWithTaskSidebarAppClient(
903855
}
904856
returndo(ctx,client,parsedURL)
905857
}
906-
907-
funcagentapiNewRequest(ctx context.Context,methodstring,appURL*url.URL,appURLPathstring,bodyany) (*http.Request,error) {
908-
u:=*appURL
909-
u.Path=path.Join(appURL.Path,appURLPath)
910-
911-
varbodyReader io.Reader
912-
ifbody!=nil {
913-
b,err:=json.Marshal(body)
914-
iferr!=nil {
915-
returnnil,httperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
916-
Message:"Failed to marshal task app request body.",
917-
Detail:err.Error(),
918-
})
919-
}
920-
bodyReader=bytes.NewReader(b)
921-
}
922-
923-
req,err:=http.NewRequestWithContext(ctx,method,u.String(),bodyReader)
924-
iferr!=nil {
925-
returnnil,httperror.NewResponseError(http.StatusBadRequest, codersdk.Response{
926-
Message:"Failed to create task app request.",
927-
Detail:err.Error(),
928-
})
929-
}
930-
req.Header.Set("Content-Type","application/json")
931-
req.Header.Set("Accept","application/json")
932-
933-
returnreq,nil
934-
}
935-
936-
funcagentapiDoStatusRequest(ctx context.Context,client*http.Client,appURL*url.URL) (string,error) {
937-
req,err:=agentapiNewRequest(ctx,http.MethodGet,appURL,"status",nil)
938-
iferr!=nil {
939-
return"",err
940-
}
941-
942-
resp,err:=client.Do(req)
943-
iferr!=nil {
944-
return"",httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
945-
Message:"Failed to reach task app endpoint.",
946-
Detail:err.Error(),
947-
})
948-
}
949-
deferresp.Body.Close()
950-
951-
ifresp.StatusCode!=http.StatusOK {
952-
return"",httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
953-
Message:"Task app status returned an error.",
954-
Detail:fmt.Sprintf("Status code: %d",resp.StatusCode),
955-
})
956-
}
957-
958-
// {"$schema":"http://localhost:3284/schemas/StatusResponseBody.json","status":"stable"}
959-
varrespBodystruct {
960-
Statusstring`json:"status"`
961-
}
962-
963-
iferr:=json.NewDecoder(resp.Body).Decode(&respBody);err!=nil {
964-
return"",httperror.NewResponseError(http.StatusBadGateway, codersdk.Response{
965-
Message:"Failed to decode task app status response body.",
966-
Detail:err.Error(),
967-
})
968-
}
969-
970-
returnrespBody.Status,nil
971-
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp