Skip to content

Commit 5359026

Browse files
committed
feat: integrate SDK
1 parent 8c224f1 commit 5359026

File tree

170 files changed

+627
-445
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+627
-445
lines changed

cmd/attach/attach.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"time"
1414

1515
tea "github.com/charmbracelet/bubbletea"
16-
"github.com/coder/agentapi/lib/httpapi"
16+
"github.com/coder/agentapi/lib/types"
1717
"github.com/spf13/cobra"
1818
sse "github.com/tmaxmax/go-sse"
1919
"golang.org/x/term"
@@ -35,27 +35,27 @@ func (c *ChannelWriter) Receive() ([]byte, bool) {
3535
}
3636

3737
type model struct {
38-
screen string
38+
conversation string
3939
}
4040

4141
func (m model) Init() tea.Cmd {
4242
// Just return `nil`, which means "no I/O right now, please."
4343
return nil
4444
}
4545

46-
type screenMsg struct {
47-
screen string
46+
type conversationMsg struct {
47+
conversation string
4848
}
4949

5050
type finishMsg struct{}
5151

5252
//lint:ignore U1000 The Update function is used by the Bubble Tea framework
5353
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5454
switch msg := msg.(type) {
55-
case screenMsg:
56-
m.screen = msg.screen
57-
if m.screen != "" && m.screen[len(m.screen)-1] != '\n' {
58-
m.screen += "\n"
55+
case conversationMsg:
56+
m.conversation = msg.conversation
57+
if m.conversation != "" && m.conversation[len(m.conversation)-1] != '\n' {
58+
m.conversation += "\n"
5959
}
6060
case tea.KeyMsg:
6161
if msg.String() == "ctrl+c" {
@@ -69,10 +69,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6969
}
7070

7171
func (m model) View() string {
72-
return m.screen
72+
return m.conversation
7373
}
7474

75-
func ReadScreenOverHTTP(ctx context.Context, url string, ch chan<- httpapi.ScreenUpdateBody) error {
75+
func ReadScreenOverHTTP(ctx context.Context, url string, ch chan<- types.ScreenUpdateBody) error {
7676
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
7777
req.Header.Set("Content-Type", "application/json")
7878

@@ -85,25 +85,25 @@ func ReadScreenOverHTTP(ctx context.Context, url string, ch chan<- httpapi.Scree
8585
}()
8686

8787
for ev, err := range sse.Read(res.Body, &sse.ReadConfig{
88-
// 256KB: screen can be big. The default terminal size is 80x1000,
88+
// 256KB: conversation can be big. The default terminal size is 80x1000,
8989
// which can be over 80000 bytes.
9090
MaxEventSize: 256 * 1024,
9191
}) {
9292
if err != nil {
9393
return xerrors.Errorf("failed to read sse: %w", err)
9494
}
95-
var screen httpapi.ScreenUpdateBody
95+
var screen types.ScreenUpdateBody
9696
if err := json.Unmarshal([]byte(ev.Data), &screen); err != nil {
97-
return xerrors.Errorf("failed to unmarshal screen: %w", err)
97+
return xerrors.Errorf("failed to unmarshal conversation: %w", err)
9898
}
9999
ch <- screen
100100
}
101101
return nil
102102
}
103103

104104
func WriteRawInputOverHTTP(ctx context.Context, url string, msg string) error {
105-
messageRequest := httpapi.MessageRequestBody{
106-
Type: httpapi.MessageTypeRaw,
105+
messageRequest := types.MessageRequestBody{
106+
Type: types.MessageTypeRaw,
107107
Content: msg,
108108
}
109109
messageRequestBytes, err := json.Marshal(messageRequest)
@@ -145,16 +145,16 @@ func runAttach(remoteUrl string) error {
145145
}
146146
tee := io.TeeReader(os.Stdin, stdinWriter)
147147
p := tea.NewProgram(model{}, tea.WithInput(tee), tea.WithAltScreen())
148-
screenCh := make(chan httpapi.ScreenUpdateBody, 64)
148+
screenCh := make(chan types.ScreenUpdateBody, 64)
149149

150150
readScreenErrCh := make(chan error, 1)
151151
go func() {
152152
defer close(readScreenErrCh)
153-
if err := ReadScreenOverHTTP(ctx, remoteUrl+"/internal/screen", screenCh); err != nil {
153+
if err := ReadScreenOverHTTP(ctx, remoteUrl+"/internal/conversation", screenCh); err != nil {
154154
if errors.Is(err, context.Canceled) {
155155
return
156156
}
157-
readScreenErrCh <- xerrors.Errorf("failed to read screen: %w", err)
157+
readScreenErrCh <- xerrors.Errorf("failed to read conversation: %w", err)
158158
}
159159
}()
160160
writeRawInputErrCh := make(chan error, 1)
@@ -189,8 +189,8 @@ func runAttach(remoteUrl string) error {
189189
if !ok {
190190
return
191191
}
192-
p.Send(screenMsg{
193-
screen: screenUpdate.Screen,
192+
p.Send(conversationMsg{
193+
conversation: screenUpdate.Screen,
194194
})
195195
}
196196
}

cmd/server/server.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"sort"
1111
"strings"
1212

13+
"github.com/coder/agentapi/lib/cli/msgfmt"
14+
"github.com/coder/agentapi/lib/cli/termexec"
15+
"github.com/coder/agentapi/lib/types"
1316
"github.com/spf13/cobra"
1417
"github.com/spf13/viper"
1518
"golang.org/x/xerrors"
1619

1720
"github.com/coder/agentapi/lib/httpapi"
1821
"github.com/coder/agentapi/lib/logctx"
19-
"github.com/coder/agentapi/lib/msgfmt"
20-
"github.com/coder/agentapi/lib/termexec"
2122
)
2223

2324
type AgentType = msgfmt.AgentType
@@ -68,14 +69,25 @@ func parseAgentType(firstArg string, agentTypeVar string) (AgentType, error) {
6869
return AgentTypeCustom, nil
6970
}
7071

72+
func parseInteractionType(interactionModeVar string) (types.InteractionType, error) {
73+
return types.CLIInteractionType, nil
74+
}
75+
7176
func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) error {
7277
agent := argsToPass[0]
7378
agentTypeValue := viper.GetString(FlagType)
79+
interactionTypeValue := viper.GetString(FlagInteractionType)
80+
7481
agentType, err := parseAgentType(agent, agentTypeValue)
7582
if err != nil {
7683
return xerrors.Errorf("failed to parse agent type: %w", err)
7784
}
7885

86+
interactionType, err := parseInteractionType(interactionTypeValue)
87+
if err != nil {
88+
return xerrors.Errorf("failed to parse interaction type: %w", err)
89+
}
90+
7991
termWidth := viper.GetUint16(FlagTermWidth)
8092
termHeight := viper.GetUint16(FlagTermHeight)
8193

@@ -104,12 +116,13 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er
104116
}
105117
port := viper.GetInt(FlagPort)
106118
srv, err := httpapi.NewServer(ctx, httpapi.ServerConfig{
107-
AgentType: agentType,
108-
Process: process,
109-
Port: port,
110-
ChatBasePath: viper.GetString(FlagChatBasePath),
111-
AllowedHosts: viper.GetStringSlice(FlagAllowedHosts),
112-
AllowedOrigins: viper.GetStringSlice(FlagAllowedOrigins),
119+
AgentType: agentType,
120+
Process: process,
121+
Port: port,
122+
InteractionType: interactionType,
123+
ChatBasePath: viper.GetString(FlagChatBasePath),
124+
AllowedHosts: viper.GetStringSlice(FlagAllowedHosts),
125+
AllowedOrigins: viper.GetStringSlice(FlagAllowedOrigins),
113126
})
114127
if err != nil {
115128
return xerrors.Errorf("failed to create server: %w", err)
@@ -118,7 +131,6 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er
118131
fmt.Println(srv.GetOpenAPI())
119132
return nil
120133
}
121-
srv.StartSnapshotLoop(ctx)
122134
logger.Info("Starting server on port", "port", port)
123135
processExitCh := make(chan error, 1)
124136
go func() {
@@ -163,15 +175,16 @@ type flagSpec struct {
163175
}
164176

165177
const (
166-
FlagType = "type"
167-
FlagPort = "port"
168-
FlagPrintOpenAPI = "print-openapi"
169-
FlagChatBasePath = "chat-base-path"
170-
FlagTermWidth = "term-width"
171-
FlagTermHeight = "term-height"
172-
FlagAllowedHosts = "allowed-hosts"
173-
FlagAllowedOrigins = "allowed-origins"
174-
FlagExit = "exit"
178+
FlagType = "type"
179+
FlagPort = "port"
180+
FlagPrintOpenAPI = "print-openapi"
181+
FlagChatBasePath = "chat-base-path"
182+
FlagTermWidth = "term-width"
183+
FlagTermHeight = "term-height"
184+
FlagAllowedHosts = "allowed-hosts"
185+
FlagAllowedOrigins = "allowed-origins"
186+
FlagExit = "exit"
187+
FlagInteractionType = "interaction"
175188
)
176189

177190
func CreateServerCmd() *cobra.Command {

lib/httpapi/claude.go renamed to lib/cli/claude.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package httpapi
1+
package cli
22

33
import (
4-
mf "github.com/coder/agentapi/lib/msgfmt"
5-
st "github.com/coder/agentapi/lib/screentracker"
4+
mf "github.com/coder/agentapi/lib/cli/msgfmt"
5+
st "github.com/coder/agentapi/lib/cli/screentracker"
66
)
77

88
func formatPaste(message string) []st.MessagePart {

0 commit comments

Comments
 (0)