Skip to content

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

working on hooks for cloud credits

Dane Schneideradfad7705f54
diff --git a/app/server/hooks/hooks.go b/app/server/hooks/hooks.go
index 74a6098b..d17df937 100644
--- a/app/server/hooks/hooks.go
+++ b/app/server/hooks/hooks.go
@@ -8,20 +8,40 @@ import (
)
const (
- CreateAccount = "create_account"
- WillCreatePlan = "will_create_plan"
- WillTellPlan = "will_tell_plan"
- WillExecPlan = "will_exec_plan"
+ CreateAccount = "create_account"
+ WillCreatePlan = "will_create_plan"
+ WillTellPlan = "will_tell_plan"
+ WillExecPlan = "will_exec_plan"
+ WillSendModelRequest = "will_send_model_request"
+ DidSendModelRequest = "did_send_model_request"
)
-type HookParams struct {
- W http.ResponseWriter
- User *db.User
- OrgId string
- Plan *db.Plan
+type WillExecPlanParams struct {
StreamDoneCh chan *shared.ApiError
}
+type WillSendModelRequestParams struct {
+ StreamDoneCh chan *shared.ApiError
+ TotalRequestTokens int
+ ModelName string
+}
+
+type DidSendModelRequestParams struct {
+ InputTokens int
+ ModelName string
+}
+
+type HookParams struct {
+ W http.ResponseWriter
+ User *db.User
+ OrgId string
+ Plan *db.Plan
+
+ WillExecPlanParams *WillExecPlanParams
+ WillSendModelRequestParams *WillSendModelRequestParams
+ DidSendModelRequestParams *DidSendModelRequestParams
+}
+
type Hook func(params HookParams) error
var hooks = make(map[string]Hook)
diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.go
index 72317857..fafc210b 100644
--- a/app/server/model/plan/tell_exec.go
+++ b/app/server/model/plan/tell_exec.go
@@ -69,9 +69,11 @@ func execTellPlan(
if missingFileResponse == "" {
err := hooks.ExecHook(hooks.WillExecPlan, hooks.HookParams{
- User: auth.User,
- Plan: plan,
- StreamDoneCh: active.StreamDoneCh,
+ User: auth.User,
+ Plan: plan,
+ WillExecPlanParams: &hooks.WillExecPlanParams{
+ StreamDoneCh: active.StreamDoneCh,
+ },
})
if err != nil {
@@ -227,6 +229,20 @@ func execTellPlan(
return
}
promptTokens = prompts.PromptWrapperTokens + numPromptTokens
+ } else if iteration > 0 && missingFileResponse == "" {
+ numPromptTokens, err = shared.GetNumTokens(prompts.AutoContinuePrompt)
+
+ if err != nil {
+ err = fmt.Errorf("error getting number of tokens in auto continue prompt: %v", err)
+ log.Println(err)
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: "Error getting number of tokens in auto continue prompt",
+ }
+ return
+ }
+ promptTokens = prompts.PromptWrapperTokens + numPromptTokens
}
state.tokensBeforeConvo = prompts.CreateSysMsgNumTokens + modelContextTokens + state.latestSummaryTokens + promptTokens
@@ -307,13 +323,27 @@ func execTellPlan(
prompt = prompts.AutoContinuePrompt
if autoContinueNextTask != "" {
- prompt += `
+ toAdd := `
Here is the next task:
` + autoContinueNextTask + `
Continue seamlessly with this task.
`
+
+ tokens, err := shared.GetNumTokens(toAdd)
+ if err != nil {
+ log.Printf("Error getting num tokens for auto continue next task: %v\n", err)
+ active.StreamDoneCh <- &shared.ApiError{
+ Type: shared.ApiErrorTypeOther,
+ Status: http.StatusInternalServerError,
+ Msg: "Error getting num tokens for auto continue next task",
+ }
+ return
+ }
+
+ prompt += toAdd
+ state.totalRequestTokens += tokens
}
}
@@ -397,6 +427,19 @@ func execTellPlan(
// log.Printf("%s: %s\n", message.Role, message.Content)
// }
+ err = hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{
+ User: auth.User,
+ OrgId: auth.OrgId,
+ Plan: plan,
+ WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
+ StreamDoneCh: active.StreamDoneCh,
+ TotalRequestTokens: state.totalRequestTokens,
+ },
+ })
+ if err != nil {
+ return
+ }
+
modelReq := openai.ChatCompletionRequest{
Model: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,
Messages: state.messages,
@@ -440,7 +483,7 @@ func execTellPlan(
}
log.Printf("Tell plan: found %d pending builds\n", len(pendingBuildsByPath))
- // spew.Dump(pendingBuildsByPath)x
+ // spew.Dump(pendingBuildsByPath)
buildState := &activeBuildStreamState{
clients: clients,
diff --git a/app/server/model/plan/tell_state.go b/app/server/model/plan/tell_state.go
index bc404ad9..38988e77 100644
--- a/app/server/model/plan/tell_state.go
+++ b/app/server/model/plan/tell_state.go
@@ -30,6 +30,7 @@ type activeTellStreamState struct {
replyNumTokens int
messages []openai.ChatCompletionMessage
tokensBeforeConvo int
+ totalRequestTokens int
settings *shared.PlanSettings
currentReplyNumRetries int
}
diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.go
index f3388e32..2ddaea66 100644
--- a/app/server/model/plan/tell_summary.go
+++ b/app/server/model/plan/tell_summary.go
@@ -97,6 +97,7 @@ func (state *activeTellStreamState) injectSummariesAsNeeded() bool {
(tokensBeforeConvo+updatedConversationTokens) <= state.settings.GetPlannerEffectiveMaxTokens() {
log.Printf("Summarizing up to %s | saving %d tokens\n", s.LatestConvoMessageCreatedAt.Format(time.RFC3339), savedTokens)
summary = s
+ conversationTokens = updatedConversationTokens
break
}
}
@@ -134,7 +135,7 @@ func (state *activeTellStreamState) injectSummariesAsNeeded() bool {
}
}
} else {
- if (tokensBeforeConvo + summary.Tokens) > state.settings.GetPlannerEffectiveMaxTokens() {
+ if (tokensBeforeConvo + conversationTokens) > state.settings.GetPlannerEffectiveMaxTokens() {
active.StreamDoneCh <- &shared.ApiError{
Type: shared.ApiErrorTypeOther,
Status: http.StatusInternalServerError,
@@ -167,6 +168,8 @@ func (state *activeTellStreamState) injectSummariesAsNeeded() bool {
}
}
+ state.totalRequestTokens = tokensBeforeConvo + conversationTokens
+
return true
}
diff --git a/app/shared/auth.go b/app/shared/auth.go
index 9f8dacbf..3207e9c2 100644
--- a/app/shared/auth.go
+++ b/app/shared/auth.go
@@ -15,6 +15,9 @@ const (
ApiErrorTypeContinueNoMessages ApiErrorType = "continue_no_messages"
+ ApiErrorTypeCloudInsufficientCredits ApiErrorType = "cloud_insufficient_credits"
+ ApiErrorTypeCloudMonthlyMaxReached ApiErrorType = "cloud_monthly_max_reached"
+
ApiErrorTypeOther ApiErrorType = "other"
)