Skip to content

Commit db2ddc4

Browse files
committed
add openapi.json into the root of the repo
1 parent 8edc0b5 commit db2ddc4

File tree

5 files changed

+574
-5
lines changed

5 files changed

+574
-5
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Check OpenAPI Schema
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
check-openapi:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version: 'stable'
19+
20+
- name: Generate OpenAPI schema
21+
run: go run main.go server --print-openapi dummy > generated-openapi.json
22+
23+
- name: Check OpenAPI schema
24+
run: |
25+
if [ ! -f "openapi.json" ]; then
26+
echo "::warning::openapi.json does not exist"
27+
exit 1
28+
fi
29+
30+
# Compare schemas
31+
if ! cmp -s openapi.json generated-openapi.json; then
32+
echo "::error::The openapi.json file is out of sync with the generated schema"
33+
echo "Diff:"
34+
diff -u openapi.json generated-openapi.json || true
35+
exit 1
36+
fi
37+
echo "OpenAPI schema is up to date"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ agentapi server -- aider --model sonnet --api-key anthropic=sk-ant-apio3-XXX
5454
agentapi server -- goose
5555
```
5656

57-
By default, the server runs on port 3284. The server exposes an OpenAPI schema at http://localhost:3284/openapi.json. You may also inspect the available endpoints at http://localhost:3284/docs.
57+
The OpenAPI schema is available in this repository: [openapi.json](openapi.json).
58+
59+
By default, the server runs on port 3284. Additionally, the server exposes an OpenAPI schema at http://localhost:3284/openapi.json. You may also inspect the available endpoints at http://localhost:3284/docs.
5860

5961
There are 4 endpoints:
6062

cmd/server/server.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"github.com/coder/agentapi/lib/httpapi"
1313
"github.com/coder/agentapi/lib/logctx"
1414
"github.com/coder/agentapi/lib/msgfmt"
15+
"github.com/coder/agentapi/lib/termexec"
1516
)
1617

1718
var (
1819
agentTypeVar string
1920
port int
21+
printOpenAPI bool
2022
)
2123

2224
type AgentType = msgfmt.AgentType
@@ -76,12 +78,21 @@ var ServerCmd = &cobra.Command{
7678
logger.Error("Failed to parse agent type", "error", err)
7779
os.Exit(1)
7880
}
79-
process, err := httpapi.SetupProcess(ctx, agent, argsToPass[1:]...)
80-
if err != nil {
81-
logger.Error("Failed to setup process", "error", err)
82-
os.Exit(1)
81+
var process *termexec.Process
82+
if printOpenAPI {
83+
process = nil
84+
} else {
85+
process, err = httpapi.SetupProcess(ctx, agent, argsToPass[1:]...)
86+
if err != nil {
87+
logger.Error("Failed to setup process", "error", err)
88+
os.Exit(1)
89+
}
8390
}
8491
srv := httpapi.NewServer(ctx, agentType, process, port)
92+
if printOpenAPI {
93+
fmt.Println(srv.GetOpenAPI())
94+
os.Exit(0)
95+
}
8596
logger.Info("Starting server on port", "port", port)
8697
go func() {
8798
if err := process.Wait(); err != nil {
@@ -101,4 +112,5 @@ var ServerCmd = &cobra.Command{
101112
func init() {
102113
ServerCmd.Flags().StringVarP(&agentTypeVar, "type", "t", "", "Override the agent type (one of: claude, goose, aider, custom)")
103114
ServerCmd.Flags().IntVarP(&port, "port", "p", 3284, "Port to run the server on")
115+
ServerCmd.Flags().BoolVarP(&printOpenAPI, "print-openapi", "P", false, "Print the OpenAPI schema to stdout and exit")
104116
}

lib/httpapi/server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package httpapi
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"log/slog"
78
"net/http"
@@ -34,6 +35,23 @@ type Server struct {
3435
emitter *EventEmitter
3536
}
3637

38+
func (s *Server) GetOpenAPI() string {
39+
jsonBytes, err := s.api.OpenAPI().MarshalJSON()
40+
if err != nil {
41+
return ""
42+
}
43+
// unmarshal the json and pretty print it
44+
var jsonObj any
45+
if err := json.Unmarshal(jsonBytes, &jsonObj); err != nil {
46+
return ""
47+
}
48+
prettyJSON, err := json.MarshalIndent(jsonObj, "", " ")
49+
if err != nil {
50+
return ""
51+
}
52+
return string(prettyJSON)
53+
}
54+
3755
// NewServer creates a new server instance
3856
func NewServer(ctx context.Context, agentType mf.AgentType, process *termexec.Process, port int) *Server {
3957
router := chi.NewMux()

0 commit comments

Comments
 (0)