-
Notifications
You must be signed in to change notification settings - Fork 105
feat: add experimental ACP mode (--experimental-acp) #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: cj/refactor/event-emitter
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,9 +304,6 @@ export function ChatProvider({ children }: PropsWithChildren) { | |
| }); | ||
| } finally { | ||
| if (type === "user") { | ||
| setMessages((prevMessages) => | ||
| prevMessages.filter((m) => !isDraftMessage(m)) | ||
| ); | ||
| setLoading(false); | ||
| } | ||
| } | ||
|
Comment on lines
306
to
309
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,7 +129,42 @@ func WriteRawInputOverHTTP(ctx context.Context, url string, msg string) error { | |
| return nil | ||
| } | ||
|
|
||
| // statusResponse is used to parse the /status endpoint response. | ||
| type statusResponse struct { | ||
| Status string `json:"status"` | ||
| AgentType string `json:"agent_type"` | ||
| Backend string `json:"backend"` | ||
| } | ||
|
|
||
| func checkACPMode(remoteUrl string) error { | ||
| resp, err := http.Get(remoteUrl + "/status") | ||
| if err != nil { | ||
| return xerrors.Errorf("failed to check server status: %w", err) | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return xerrors.Errorf("unexpected %d response from server: %s", resp.StatusCode, resp.Status) | ||
| } | ||
|
|
||
| var status statusResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&status); err != nil { | ||
| return xerrors.Errorf("failed to decode server status: %w", err) | ||
| } | ||
|
|
||
| if status.Backend == "acp" { | ||
| return xerrors.New("attach is not supported in ACP mode. The server is running with --experimental-acp which uses JSON-RPC instead of terminal emulation.") | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. review: It should be eventually supported but I'm not sure what form it should take yet. |
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func runAttach(remoteUrl string) error { | ||
| // Check if server is running in ACP mode (attach not supported) | ||
| if err := checkACPMode(remoteUrl); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| stdin := int(os.Stdin.Fd()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ import ( | |||||||
| "github.com/coder/agentapi/lib/httpapi" | ||||||||
| "github.com/coder/agentapi/lib/logctx" | ||||||||
| "github.com/coder/agentapi/lib/msgfmt" | ||||||||
| st "github.com/coder/agentapi/lib/screentracker" | ||||||||
| "github.com/coder/agentapi/lib/termexec" | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
@@ -104,11 +105,33 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er | |||||||
| } | ||||||||
|
|
||||||||
| printOpenAPI := viper.GetBool(FlagPrintOpenAPI) | ||||||||
| experimentalACP := viper.GetBool(FlagExperimentalACP) | ||||||||
|
|
||||||||
| if printOpenAPI && experimentalACP { | ||||||||
| return xerrors.Errorf("flags --%s and --%s are mutually exclusive", FlagPrintOpenAPI, FlagExperimentalACP) | ||||||||
| } | ||||||||
|
|
||||||||
| var agentIO st.AgentIO | ||||||||
| var transport = "pty" | ||||||||
| var process *termexec.Process | ||||||||
| var acpResult *httpapi.SetupACPResult | ||||||||
|
|
||||||||
| if printOpenAPI { | ||||||||
| process = nil | ||||||||
| agentIO = nil | ||||||||
|
||||||||
| agentIO = nil | |
| agentIO = nil | |
| transport = "none" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| //go:build ignore | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "strings" | ||
|
|
||
| acp "github.com/coder/acp-go-sdk" | ||
| ) | ||
|
|
||
| // ScriptEntry defines a single entry in the test script. | ||
| type ScriptEntry struct { | ||
| ExpectMessage string `json:"expectMessage"` | ||
| ThinkDurationMS int64 `json:"thinkDurationMS"` | ||
| ResponseMessage string `json:"responseMessage"` | ||
| } | ||
|
|
||
| // acpEchoAgent implements the ACP Agent interface for testing. | ||
| type acpEchoAgent struct { | ||
| script []ScriptEntry | ||
| scriptIndex int | ||
| conn *acp.AgentSideConnection | ||
| sessionID acp.SessionId | ||
| } | ||
|
|
||
| var _ acp.Agent = (*acpEchoAgent)(nil) | ||
|
|
||
| func main() { | ||
| if len(os.Args) != 2 { | ||
| fmt.Fprintln(os.Stderr, "Usage: acp_echo <script.json>") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| script, err := loadScript(os.Args[1]) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error loading script: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if len(script) == 0 { | ||
| fmt.Fprintln(os.Stderr, "Script is empty") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| sigCh := make(chan os.Signal, 1) | ||
| signal.Notify(sigCh, os.Interrupt) | ||
| go func() { | ||
| <-sigCh | ||
| os.Exit(0) | ||
| }() | ||
|
|
||
| agent := &acpEchoAgent{ | ||
| script: script, | ||
| } | ||
|
|
||
| conn := acp.NewAgentSideConnection(agent, os.Stdout, os.Stdin) | ||
| agent.conn = conn | ||
|
|
||
| <-conn.Done() | ||
| } | ||
|
|
||
| func (a *acpEchoAgent) Initialize(_ context.Context, _ acp.InitializeRequest) (acp.InitializeResponse, error) { | ||
| return acp.InitializeResponse{ | ||
| ProtocolVersion: acp.ProtocolVersionNumber, | ||
| AgentCapabilities: acp.AgentCapabilities{}, | ||
| }, nil | ||
| } | ||
|
|
||
| func (a *acpEchoAgent) Authenticate(_ context.Context, _ acp.AuthenticateRequest) (acp.AuthenticateResponse, error) { | ||
| return acp.AuthenticateResponse{}, nil | ||
| } | ||
|
|
||
| func (a *acpEchoAgent) Cancel(_ context.Context, _ acp.CancelNotification) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (a *acpEchoAgent) NewSession(_ context.Context, _ acp.NewSessionRequest) (acp.NewSessionResponse, error) { | ||
| a.sessionID = "test-session" | ||
| return acp.NewSessionResponse{ | ||
| SessionId: a.sessionID, | ||
| }, nil | ||
| } | ||
|
|
||
| func (a *acpEchoAgent) Prompt(ctx context.Context, params acp.PromptRequest) (acp.PromptResponse, error) { | ||
| // Extract text from prompt | ||
| var promptText string | ||
| for _, block := range params.Prompt { | ||
| if block.Text != nil { | ||
| promptText = block.Text.Text | ||
| break | ||
| } | ||
| } | ||
| promptText = strings.TrimSpace(promptText) | ||
|
|
||
| if a.scriptIndex >= len(a.script) { | ||
| return acp.PromptResponse{ | ||
| StopReason: acp.StopReasonEndTurn, | ||
| }, nil | ||
| } | ||
|
|
||
| entry := a.script[a.scriptIndex] | ||
| expected := strings.TrimSpace(entry.ExpectMessage) | ||
|
|
||
| // Empty ExpectMessage matches any prompt | ||
| if expected != "" && expected != promptText { | ||
| return acp.PromptResponse{}, fmt.Errorf("expected message %q but got %q", expected, promptText) | ||
| } | ||
|
|
||
| a.scriptIndex++ | ||
|
|
||
| // Send response via session update | ||
| if err := a.conn.SessionUpdate(ctx, acp.SessionNotification{ | ||
| SessionId: params.SessionId, | ||
| Update: acp.UpdateAgentMessageText(entry.ResponseMessage), | ||
| }); err != nil { | ||
| return acp.PromptResponse{}, err | ||
| } | ||
|
|
||
| return acp.PromptResponse{ | ||
| StopReason: acp.StopReasonEndTurn, | ||
| }, nil | ||
| } | ||
|
|
||
| func (a *acpEchoAgent) SetSessionMode(_ context.Context, _ acp.SetSessionModeRequest) (acp.SetSessionModeResponse, error) { | ||
| return acp.SetSessionModeResponse{}, nil | ||
| } | ||
|
|
||
| func loadScript(scriptPath string) ([]ScriptEntry, error) { | ||
| data, err := os.ReadFile(scriptPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read script file: %w", err) | ||
| } | ||
|
|
||
| var script []ScriptEntry | ||
| if err := json.Unmarshal(data, &script); err != nil { | ||
| return nil, fmt.Errorf("failed to parse script JSON: %w", err) | ||
| } | ||
|
|
||
| return script, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [ | ||
| { | ||
| "expectMessage": "This is a test message.", | ||
| "responseMessage": "Echo: This is a test message." | ||
| } | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
review: this was causing a 'flicker' when sending a message in the UI