Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

pass in openai api key rather than requiring server env var

Dane Schneider97fb9e6eecad
diff --git a/app/server/handlers/plans_exec.go b/app/server/handlers/plans_exec.go
index a80d5564..e20df21a 100644
--- a/app/server/handlers/plans_exec.go
+++ b/app/server/handlers/plans_exec.go
@@ -9,7 +9,8 @@ import (
"os"
"plandex-server/db"
"plandex-server/host"
- model "plandex-server/model/plan"
+ "plandex-server/model"
+ modelPlan "plandex-server/model/plan"
"plandex-server/types"
"github.com/gorilla/mux"
@@ -62,6 +63,12 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ if requestBody.ApiKey == "" {
+ log.Println("API key is required")
+ http.Error(w, "API key is required", http.StatusBadRequest)
+ return
+ }
+
if os.Getenv("IS_CLOUD") != "" {
user, err := db.GetUser(auth.User.Id)
@@ -86,7 +93,8 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {
}
}
- err = model.Tell(plan, branch, auth, &requestBody)
+ client := model.NewClient(requestBody.ApiKey)
+ err = modelPlan.Tell(client, plan, branch, auth, &requestBody)
if err != nil {
log.Printf("Error telling plan: %v\n", err)
@@ -95,10 +103,10 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {
}
if requestBody.ConnectStream {
- active := model.GetActivePlan(planId, branch)
- subscriptionId, ch := model.SubscribePlan(planId, branch)
+ active := modelPlan.GetActivePlan(planId, branch)
+ subscriptionId, ch := modelPlan.SubscribePlan(planId, branch)
startResponseStream(w, ch, active, func() {
- model.UnsubscribePlan(planId, branch, subscriptionId)
+ modelPlan.UnsubscribePlan(planId, branch, subscriptionId)
})
}
}
@@ -156,7 +164,7 @@ func StopPlanHandler(w http.ResponseWriter, r *http.Request) {
return
}
- active := model.GetActivePlan(planId, branch)
+ active := modelPlan.GetActivePlan(planId, branch)
if active == nil {
modelStream, err := db.GetActiveModelStream(planId, branch)
@@ -193,7 +201,7 @@ func StopPlanHandler(w http.ResponseWriter, r *http.Request) {
defer (*unlockFn)()
}
- err := model.Stop(planId, branch, auth.User.Id, auth.OrgId)
+ err := modelPlan.Stop(planId, branch, auth.User.Id, auth.OrgId)
if err != nil {
log.Printf("Error stopping plan: %v\n", err)
diff --git a/app/server/main.go b/app/server/main.go
index 1054b956..34708e18 100644
--- a/app/server/main.go
+++ b/app/server/main.go
@@ -16,10 +16,6 @@ import (
)
func main() {
- if os.Getenv("OPENAI_API_KEY") == "" {
- log.Fatal("OPENAI_API_KEY environment variable is not set")
- }
-
err := host.LoadIp()
if err != nil {
log.Fatal("Error loading IP: ", err)
diff --git a/app/server/model/client.go b/app/server/model/client.go
index 340f20ce..9199ce78 100644
--- a/app/server/model/client.go
+++ b/app/server/model/client.go
@@ -1,7 +1,6 @@
package model
import (
- "os"
"time"
"github.com/sashabaranov/go-openai"
@@ -9,8 +8,6 @@ import (
const OPENAI_STREAM_CHUNK_TIMEOUT = time.Duration(30) * time.Second
-var Client *openai.Client
-
-func init() {
- Client = openai.NewClient(os.Getenv("OPENAI_API_KEY"))
+func NewClient(apiKey string) *openai.Client {
+ return openai.NewClient(apiKey)
}
diff --git a/app/server/model/name.go b/app/server/model/name.go
index 5acc96ad..60cd7c48 100644
--- a/app/server/model/name.go
+++ b/app/server/model/name.go
@@ -9,7 +9,7 @@ import (
"github.com/sashabaranov/go-openai"
)
-func GenPlanName(planContent string) (string, error) {
+func GenPlanName(client *openai.Client, planContent string) (string, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
@@ -22,7 +22,7 @@ func GenPlanName(planContent string) (string, error) {
Content: prompts.GetPlanNamePrompt(planContent),
})
- resp, err := Client.CreateChatCompletion(
+ resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: NameModel,
diff --git a/app/server/model/plan/build.go b/app/server/model/plan/build.go
index 4436225e..3f10df05 100644
--- a/app/server/model/plan/build.go
+++ b/app/server/model/plan/build.go
@@ -24,7 +24,7 @@ import (
const MaxRetries = 3
const MaxReplacementRetries = 1
-func QueueBuild(currentOrgId, currentUserId, planId, branch string, activeBuild *types.ActiveBuild) {
+func QueueBuild(client *openai.Client, currentOrgId, currentUserId, planId, branch string, activeBuild *types.ActiveBuild) {
activePlan := GetActivePlan(planId, branch)
filePath := activeBuild.Path
@@ -40,11 +40,11 @@ func QueueBuild(currentOrgId, currentUserId, planId, branch string, activeBuild
return
} else {
log.Printf("Will process build queue for file %s\n", filePath)
- go execPlanBuild(currentOrgId, currentUserId, branch, activePlan, []*types.ActiveBuild{activeBuild})
+ go execPlanBuild(client, currentOrgId, currentUserId, branch, activePlan, []*types.ActiveBuild{activeBuild})
}
}
-func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *types.ActivePlan, activeBuilds []*types.ActiveBuild) {
+func execPlanBuild(client *openai.Client, currentOrgId, currentUserId, branch string, activePlan *types.ActivePlan, activeBuilds []*types.ActiveBuild) {
if len(activeBuilds) == 0 {
log.Println("No active builds")
return
@@ -227,7 +227,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *types
if len(nextBuilds) > 0 {
log.Println("Calling execPlanBuild for next build in queue")
- go execPlanBuild(currentOrgId, currentUserId, branch, activePlan, nextBuilds)
+ go execPlanBuild(client, currentOrgId, currentUserId, branch, activePlan, nextBuilds)
}
return
} else {
@@ -388,7 +388,7 @@ func execPlanBuild(currentOrgId, currentUserId, branch string, activePlan *types
ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},
}
- stream, err := model.Client.CreateChatCompletionStream(activePlan.Ctx, modelReq)
+ stream, err := client.CreateChatCompletionStream(activePlan.Ctx, modelReq)
if err != nil {
log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)
diff --git a/app/server/model/plan/describe.go b/app/server/model/plan/describe.go
index b8e3eb49..28854dfd 100644
--- a/app/server/model/plan/describe.go
+++ b/app/server/model/plan/describe.go
@@ -12,9 +12,9 @@ import (
"github.com/sashabaranov/go-openai"
)
-func genPlanDescription(planId, branch string, ctx context.Context) (*db.ConvoMessageDescription, error) {
+func genPlanDescription(client *openai.Client, planId, branch string, ctx context.Context) (*db.ConvoMessageDescription, error) {
- descResp, err := model.Client.CreateChatCompletion(
+ descResp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: model.CommitMsgModel,
diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.go
index 114fec97..6385c94c 100644
--- a/app/server/model/plan/exec_status.go
+++ b/app/server/model/plan/exec_status.go
@@ -11,13 +11,13 @@ import (
"github.com/sashabaranov/go-openai"
)
-func ExecStatus(message string, ctx context.Context) (*types.PlanExecStatus, error) {
+func ExecStatus(client *openai.Client, message string, ctx context.Context) (*types.PlanExecStatus, error) {
var res types.PlanExecStatus
errCh := make(chan error, 2)
go func() {
- needsInput, err := ExecStatusNeedsInput(message, ctx)
+ needsInput, err := ExecStatusNeedsInput(client, message, ctx)
if err != nil {
errCh <- err
return
@@ -27,7 +27,7 @@ func ExecStatus(message string, ctx context.Context) (*types.PlanExecStatus, err
}()
go func() {
- finished, err := ExecStatusIsFinished(message, ctx)
+ finished, err := ExecStatusIsFinished(client, message, ctx)
if err != nil {
errCh <- err
return
@@ -46,7 +46,7 @@ func ExecStatus(message string, ctx context.Context) (*types.PlanExecStatus, err
return &res, nil
}
-func ExecStatusIsFinished(message string, ctx context.Context) (bool, error) {
+func ExecStatusIsFinished(client *openai.Client, message string, ctx context.Context) (bool, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
@@ -54,7 +54,7 @@ func ExecStatusIsFinished(message string, ctx context.Context) (bool, error) {
},
}
- resp, err := model.Client.CreateChatCompletion(
+ resp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: model.PlanExecStatusModel,
@@ -97,7 +97,7 @@ func ExecStatusIsFinished(message string, ctx context.Context) (bool, error) {
return res.Finished, nil
}
-func ExecStatusNeedsInput(message string, ctx context.Context) (bool, error) {
+func ExecStatusNeedsInput(client *openai.Client, message string, ctx context.Context) (bool, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
@@ -105,7 +105,7 @@ func ExecStatusNeedsInput(message string, ctx context.Context) (bool, error) {
},
}
- resp, err := model.Client.CreateChatCompletion(
+ resp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: model.PlanExecStatusModel,
diff --git a/app/server/model/plan/tell.go b/app/server/model/plan/tell.go
index 442cf08e..8194e0e2 100644
--- a/app/server/model/plan/tell.go
+++ b/app/server/model/plan/tell.go
@@ -22,7 +22,7 @@ import (
"github.com/sashabaranov/go-openai"
)
-func Tell(plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {
+func Tell(client *openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest) error {
// goEnv := os.Getenv("GOENV") // Fetch the GOENV environment variable
// log.Println("GOENV: " + goEnv)
@@ -65,12 +65,12 @@ func Tell(plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.Tell
log.Println("Model stream id:", modelStream.Id)
- go execTellPlan(plan, branch, auth, req, active, 0)
+ go execTellPlan(client, plan, branch, auth, req, active, 0)
return nil
}
-func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest, active *types.ActivePlan, iteration int) {
+func execTellPlan(client *openai.Client, plan *db.Plan, branch string, auth *types.ServerAuth, req *shared.TellPlanRequest, active *types.ActivePlan, iteration int) {
currentUserId := auth.User.Id
currentOrgId := auth.OrgId
@@ -127,7 +127,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
// get name for plan and rename it's a draft
go func() {
if plan.Name == "draft" {
- name, err := model.GenPlanName(req.Prompt)
+ name, err := model.GenPlanName(client, req.Prompt)
if err != nil {
log.Printf("Error generating plan name: %v\n", err)
@@ -499,7 +499,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
TopP: 0.7,
}
- stream, err := model.Client.CreateChatCompletionStream(active.Ctx, modelReq)
+ stream, err := client.CreateChatCompletionStream(active.Ctx, modelReq)
if err != nil {
log.Printf("Error creating proposal GPT4 stream: %v\n", err)
log.Println(err)
@@ -652,7 +652,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
if len(convo) > 0 {
// summarize in the background
- go summarizeConvo(summarizeConvoParams{
+ go summarizeConvo(client, summarizeConvoParams{
planId: planId,
branch: branch,
convo: convo,
@@ -699,7 +699,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
MadePlan: false,
}
} else {
- description, err = genPlanDescription(planId, branch, active.Ctx)
+ description, err = genPlanDescription(client, planId, branch, active.Ctx)
if err != nil {
onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)
return
@@ -725,7 +725,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
go func() {
- execStatus, err = ExecStatus(assistantMsg.Message, active.Ctx)
+ execStatus, err = ExecStatus(client, assistantMsg.Message, active.Ctx)
if err != nil {
onError(fmt.Errorf("failed to get exec status: %v", err), false, assistantMsg.Id, convoCommitMsg)
errCh <- err
@@ -768,7 +768,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
} else {
// continue plan
- execTellPlan(plan, branch, auth, req, active, iteration+1)
+ execTellPlan(client, plan, branch, auth, req, active, iteration+1)
}
return
@@ -798,7 +798,7 @@ func execTellPlan(plan *db.Plan, branch string, auth *types.ServerAuth, req *sha
for i := len(files) - 1; i > len(replyFiles)-1; i-- {
file := files[i]
log.Printf("Queuing build for %s\n", file)
- QueueBuild(currentOrgId, currentUserId, planId, branch, &types.ActiveBuild{
+ QueueBuild(client, currentOrgId, currentUserId, planId, branch, &types.ActiveBuild{
AssistantMessageId: replyId,
ReplyContent: active.CurrentReplyContent,
FileContent: fileContents[i],
@@ -825,7 +825,7 @@ type summarizeConvoParams struct {
currentOrgId string
}
-func summarizeConvo(params summarizeConvoParams) error {
+func summarizeConvo(client *openai.Client, params summarizeConvoParams) error {
planId := params.planId
branch := params.branch
convo := params.convo
@@ -890,7 +890,7 @@ func summarizeConvo(params summarizeConvoParams) error {
})
}
- summary, err := model.PlanSummary(model.PlanSummaryParams{
+ summary, err := model.PlanSummary(client, model.PlanSummaryParams{
Conversation: summaryMessages,
LatestConvoMessageId: latestMessageId,
LatestConvoMessageCreatedAt: latestMessageSummarizedAt,
diff --git a/app/server/model/summarize.go b/app/server/model/summarize.go
index c9d57064..c79b00d5 100644
--- a/app/server/model/summarize.go
+++ b/app/server/model/summarize.go
@@ -19,7 +19,7 @@ type PlanSummaryParams struct {
PlanId string
}
-func PlanSummary(params PlanSummaryParams) (*db.ConvoSummary, error) {
+func PlanSummary(client *openai.Client, params PlanSummaryParams) (*db.ConvoSummary, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
@@ -39,7 +39,7 @@ func PlanSummary(params PlanSummaryParams) (*db.ConvoSummary, error) {
// fmt.Println("summarizing messages:")
// spew.Dump(messages)
- resp, err := Client.CreateChatCompletion(
+ resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: PlanSummaryModel,
diff --git a/app/shared/req_res.go b/app/shared/req_res.go
index 1dcf16cc..04edd914 100644
--- a/app/shared/req_res.go
+++ b/app/shared/req_res.go
@@ -93,6 +93,7 @@ type TellPlanRequest struct {
Prompt string `json:"prompt"`
ConnectStream bool `json:"connectStream"`
AutoContinue bool `json:"autoContinue"`
+ ApiKey string `json:"apiKey"`
}
type LoadContextParams struct {
diff --git a/infra/deploy.sh b/infra/deploy.sh
index ebb300d7..1faf7bf3 100755
--- a/infra/deploy.sh
+++ b/infra/deploy.sh
@@ -138,8 +138,8 @@ log "Building and pushing the Docker image to ECR..."
build_and_push_image
# Deploy or update the CloudFormation stack
-log "Deploying or updating the CloudFormation stack..."
-deploy_or_update_stack
+# log "Deploying or updating the CloudFormation stack..."
+# deploy_or_update_stack
# Update the ECS service with the new Docker image
log "Updating the ECS service with the new Docker image..."