Skip to content

Commit 4135dcc

Browse files
committed
Cleaned up AI executions with shuffle creds
1 parent 1e37234 commit 4135dcc

File tree

4 files changed

+78
-60
lines changed

4 files changed

+78
-60
lines changed

ai.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7514,16 +7514,17 @@ You are the Action Execution Agent for the Shuffle platform. You receive tools (
75147514

75157515
}
75167516

7517-
<<<<<<< llm-auth-fix
75187517
// Set model based on environment
75197518
aiModel := "gpt-5-mini"
7520-
if project.Environment != "cloud" {
7521-
aiModel = os.Getenv("AI_MODEL")
7522-
if aiModel == "" {
7523-
aiModel = os.Getenv("OPENAI_MODEL")
7524-
}
7519+
newAiModel := os.Getenv("AI_MODEL")
7520+
if newAiModel == "" {
7521+
newAiModel = os.Getenv("OPENAI_MODEL")
75257522
}
7526-
=======
7523+
7524+
if len(newAiModel) > 0 {
7525+
aiModel = newAiModel
7526+
}
7527+
75277528
// Escape relevant... weird data
75287529
//In case of previous escapes
75297530
metadata = strings.ReplaceAll(metadata, "${", "{")
@@ -7533,7 +7534,6 @@ You are the Action Execution Agent for the Shuffle platform. You receive tools (
75337534
userMessage = strings.ReplaceAll(userMessage, "${", "{")
75347535
userMessage = strings.ReplaceAll(userMessage, "\\$", "$")
75357536
userMessage = strings.ReplaceAll(userMessage, "$", "\\$")
7536-
>>>>>>> main
75377537

75387538
completionRequest := openai.ChatCompletionRequest{
75397539
Model: aiModel,

shared.go

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21110,54 +21110,6 @@ func PrepareSingleAction(ctx context.Context, user User, appId string, body []by
2111021110
}
2111121111
}
2111221112

21113-
// Fallback to inject AI creds if the user don't have any
21114-
if strings.ToLower(app.Name) == "openai" && len(action.AuthenticationId) == 0 {
21115-
apiKey := os.Getenv("AI_API_KEY")
21116-
if apiKey == "" {
21117-
apiKey = os.Getenv("OPENAI_API_KEY")
21118-
}
21119-
21120-
apiUrl := os.Getenv("AI_API_URL")
21121-
if apiUrl == "" {
21122-
apiUrl = os.Getenv("OPENAI_API_URL")
21123-
}
21124-
if apiUrl == "" {
21125-
apiUrl = "https://api.openai.com"
21126-
}
21127-
21128-
if len(apiKey) > 0 {
21129-
// TODO: Track actual token usage from response
21130-
21131-
urlFound := false
21132-
apikeyFound := false
21133-
for i, param := range action.Parameters {
21134-
if param.Name == "url" {
21135-
action.Parameters[i].Value = apiUrl // Don't trust the user provided url, use the one from the env
21136-
urlFound = true
21137-
}
21138-
if param.Name == "apikey" {
21139-
action.Parameters[i].Value = apiKey
21140-
action.Parameters[i].Configuration = true
21141-
apikeyFound = true
21142-
}
21143-
}
21144-
if !urlFound {
21145-
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
21146-
Name: "url",
21147-
Value: apiUrl,
21148-
})
21149-
}
21150-
if !apikeyFound {
21151-
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
21152-
Name: "apikey",
21153-
Value: apiKey,
21154-
Configuration: true,
21155-
})
21156-
}
21157-
log.Printf("[AUDIT] Injected system OpenAI credentials (fallback) for org %s", user.ActiveOrg.Id)
21158-
}
21159-
}
21160-
2116121113
if runValidationAction {
2116221114
log.Printf("\n\n[INFO] SHOULD BE Running validation action for %s for org %s (%s)\n\n", app.Name, user.ActiveOrg.Name, user.ActiveOrg.Id)
2116321115

@@ -21216,9 +21168,6 @@ func PrepareSingleAction(ctx context.Context, user User, appId string, body []by
2121621168

2121721169
action.AppID = appId
2121821170
workflow := Workflow{
21219-
Actions: []Action{
21220-
action,
21221-
},
2122221171
Start: action.ID,
2122321172
ID: uuid.NewV4().String(),
2122421173
Generated: true,
@@ -21234,6 +21183,74 @@ func PrepareSingleAction(ctx context.Context, user User, appId string, body []by
2123421183
workflowExecution.OrgId = user.ActiveOrg.Id
2123521184
}
2123621185

21186+
// Fallback to inject AI creds if the user don't have any
21187+
if strings.ToLower(app.Name) == "openai" && len(action.AuthenticationId) == 0 {
21188+
apiKey := os.Getenv("AI_API_KEY")
21189+
if apiKey == "" {
21190+
apiKey = os.Getenv("OPENAI_API_KEY")
21191+
}
21192+
21193+
apiUrl := os.Getenv("AI_API_URL")
21194+
if apiUrl == "" {
21195+
apiUrl = os.Getenv("OPENAI_API_URL")
21196+
}
21197+
21198+
if apiUrl == "" {
21199+
apiUrl = "https://api.openai.com"
21200+
}
21201+
21202+
if len(apiKey) > 0 {
21203+
IncrementCache(ctx, user.ActiveOrg.Id, "ai_executions", 1)
21204+
21205+
urlFound := false
21206+
apikeyFound := false
21207+
for i, param := range action.Parameters {
21208+
if param.Name == "url" {
21209+
action.Parameters[i].Value = apiUrl
21210+
urlFound = true
21211+
}
21212+
21213+
if param.Name == "apikey" {
21214+
action.Parameters[i].Value = apiKey
21215+
action.Parameters[i].Configuration = true
21216+
apikeyFound = true
21217+
}
21218+
}
21219+
21220+
if !urlFound {
21221+
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
21222+
Name: "url",
21223+
Value: apiUrl,
21224+
})
21225+
}
21226+
21227+
if !apikeyFound {
21228+
action.Parameters = append(action.Parameters, WorkflowAppActionParameter{
21229+
Name: "apikey",
21230+
Value: apiKey,
21231+
Configuration: true,
21232+
})
21233+
}
21234+
21235+
log.Printf("[AUDIT] Injected system AI credentials (fallback) for org %s", user.ActiveOrg.Id)
21236+
21237+
// Mapping to internal so the execution itself is not referencable
21238+
if project.Environment == "cloud" {
21239+
workflowExecution.ExecutionOrg = "INTERNAL"
21240+
workflowExecution.Workflow.OrgId = "INTERNAL"
21241+
workflow.OrgId = "INTERNAL"
21242+
workflow.ExecutingOrg = OrgMini{
21243+
Name: "INTERNAL",
21244+
Id: "INTERNAL",
21245+
}
21246+
}
21247+
}
21248+
}
21249+
21250+
workflow.Actions = []Action{
21251+
action,
21252+
}
21253+
2123721254
// Add fake queries to it. Doesn't matter what is here.
2123821255
// This is just to ensure that _something_ is sent
2123921256
badRequest := &http.Request{}

stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ func IncrementCache(ctx context.Context, orgId, dataType string, amount ...int)
891891
return
892892
}
893893

894-
if len(orgId) != 36 && orgId != "public" {
894+
if len(orgId) != 36 && orgId != "public" && orgId != "INTERNAL" {
895895
log.Printf("[ERROR] Increment Stats with bad OrgId '%s' for type '%s'", orgId, dataType)
896896
return
897897
}

structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,6 +4926,7 @@ type TestResult struct {
49264926
Error string `json:"error,omitempty"`
49274927
}
49284928

4929+
// Standard used for MCP
49294930
type MCPRequest struct {
49304931
Jsonrpc string `json:"jsonrpc"`
49314932
ID string `json:"id"`

0 commit comments

Comments
 (0)