-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
169 lines (142 loc) · 4.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
)
//go:embed instructions/*
var instructions embed.FS
const ClaudeApiUrl = "https://api.anthropic.com/v1/messages"
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Request struct {
Model string `json:"model"`
Messages []Message `json:"messages,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
}
type Response struct {
Content []struct {
Text string `json:"text"`
} `json:"content"`
Error struct {
Message string `json:"message"`
} `json:"error,omitempty"`
}
func loadServiceTemplates(dockerComposeContent string) (string, error) {
entries, err := instructions.ReadDir("instructions")
if err != nil {
return "", fmt.Errorf("error reading instructions directory: %v", err)
}
var services []string
for _, entry := range entries {
serviceName := strings.TrimSuffix(entry.Name(), ".md")
if strings.Contains(strings.ToLower(dockerComposeContent), strings.ToLower(serviceName)) {
content, err := instructions.ReadFile(path.Join("instructions", entry.Name()))
if err != nil {
return "", fmt.Errorf("error reading template for %s: %v", serviceName, err)
}
services = append(services, fmt.Sprintf(`<service>
<name>%s</name>
<template>%s</template>
</service>`, serviceName, string(content)))
}
}
if len(services) > 0 {
return fmt.Sprintf("<services>%s</services>", strings.Join(services, "\n")), nil
}
return "", nil
}
func callClaude(apiKey string, dockerCompose string, schema string) (string, error) {
serviceDocs, err := loadServiceTemplates(dockerCompose)
if err != nil {
return "", err
}
prompt := fmt.Sprintf(`<input>
<docker-compose>%s</docker-compose>
<schema>%s</schema>
%s
<instructions>
1. Convert the docker-compose.yaml to zeabur-template.yaml based on the provided schema.
2. Use provided service templates directly when available.
3. Place config content directly in YAML instead of using volume mounts.
Exception: configs that auto-generate at startup and reset on restart.
</instructions>
</input>
<output-format>
Provide only zeabur-template.yaml content without explanations or code blocks.
</output-format>
`, dockerCompose, schema, serviceDocs)
requestBody := Request{
Model: "claude-3-sonnet-20240229",
Messages: []Message{{Role: "user", Content: prompt}},
MaxTokens: 4096,
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
return "", fmt.Errorf("error marshaling request: %v", err)
}
req, err := http.NewRequest("POST", ClaudeApiUrl, bytes.NewBuffer(jsonData))
if err != nil {
return "", fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error making request: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
var response Response
if err := json.Unmarshal(body, &response); err != nil {
return "", fmt.Errorf("error unmarshaling response: %v", err)
}
if response.Error.Message != "" {
return "", fmt.Errorf("API error: %s", response.Error.Message)
}
if len(response.Content) > 0 {
return response.Content[0].Text, nil
}
return "", fmt.Errorf("no content in response")
}
func main() {
apiKey := os.Getenv("CLAUDE_API_KEY")
if apiKey == "" {
fmt.Println("Please set CLAUDE_API_KEY environment variable")
return
}
dockerCompose, err := os.ReadFile("docker-compose.yaml")
if err != nil {
fmt.Printf("Error reading docker-compose.yaml: %v\n", err)
return
}
schema, err := os.ReadFile("schema.json")
if err != nil {
fmt.Printf("Error reading schema.json: %v\n", err)
return
}
zeaburTemplate, err := callClaude(apiKey, string(dockerCompose), string(schema))
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
err = os.WriteFile("zeabur-template.yaml", []byte(zeaburTemplate), 0644)
if err != nil {
fmt.Printf("Error writing zeabur-template.yaml: %v\n", err)
return
}
fmt.Println("Successfully converted to zeabur-template.yaml")
}