Skip to content

Commit f1073c5

Browse files
refactor: simplify generateCustomJobToolDefinition and extractDispatchWorkflowNames (#20107)
1 parent 984fc7a commit f1073c5

2 files changed

Lines changed: 46 additions & 82 deletions

File tree

pkg/cli/remote_workflow.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,7 @@ func extractDispatchWorkflowNames(content string) []string {
631631
return nil
632632
}
633633

634-
safeOutputs, exists := result.Frontmatter["safe-outputs"]
635-
if !exists {
636-
return nil
637-
}
638-
639-
safeOutputsMap, ok := safeOutputs.(map[string]any)
634+
safeOutputsMap, ok := result.Frontmatter["safe-outputs"].(map[string]any)
640635
if !ok {
641636
return nil
642637
}
@@ -652,32 +647,22 @@ func extractDispatchWorkflowNames(content string) []string {
652647
case []any:
653648
// Array format: dispatch-workflow: [name1, name2]
654649
for _, item := range v {
655-
if name, ok := item.(string); ok {
650+
if name, ok := item.(string); ok && !strings.Contains(name, "${{") {
656651
workflowNames = append(workflowNames, name)
657652
}
658653
}
659654
case map[string]any:
660655
// Map format: dispatch-workflow: {workflows: [name1, name2]}
661-
if workflows, exists := v["workflows"]; exists {
662-
if workflowsArray, ok := workflows.([]any); ok {
663-
for _, item := range workflowsArray {
664-
if name, ok := item.(string); ok {
665-
workflowNames = append(workflowNames, name)
666-
}
656+
if workflowsArray, ok := v["workflows"].([]any); ok {
657+
for _, item := range workflowsArray {
658+
if name, ok := item.(string); ok && !strings.Contains(name, "${{") {
659+
workflowNames = append(workflowNames, name)
667660
}
668661
}
669662
}
670663
}
671664

672-
// Filter out GitHub Actions expression syntax (e.g. "${{ vars.WORKFLOW }}")
673-
filtered := make([]string, 0, len(workflowNames))
674-
for _, name := range workflowNames {
675-
if !strings.Contains(name, "${{") {
676-
filtered = append(filtered, name)
677-
}
678-
}
679-
680-
return filtered
665+
return workflowNames
681666
}
682667

683668
// fetchAndSaveRemoteDispatchWorkflows fetches and saves the workflow files referenced in the

pkg/workflow/safe_outputs_config_generation.go

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,87 +10,66 @@ import (
1010
func generateCustomJobToolDefinition(jobName string, jobConfig *SafeJobConfig) map[string]any {
1111
safeOutputsConfigLog.Printf("Generating tool definition for custom job: %s", jobName)
1212

13-
// Build the tool definition
14-
tool := map[string]any{
15-
"name": jobName,
13+
description := jobConfig.Description
14+
if description == "" {
15+
description = fmt.Sprintf("Execute the %s custom job", jobName)
1616
}
1717

18-
// Add description if present
19-
if jobConfig.Description != "" {
20-
tool["description"] = jobConfig.Description
21-
} else {
22-
// Provide a default description if none is specified
23-
tool["description"] = fmt.Sprintf("Execute the %s custom job", jobName)
24-
}
25-
26-
// Build the input schema
2718
inputSchema := map[string]any{
28-
"type": "object",
29-
"properties": make(map[string]any),
19+
"type": "object",
20+
"properties": make(map[string]any),
21+
"additionalProperties": false,
3022
}
3123

32-
// Track required fields
3324
var requiredFields []string
25+
properties := inputSchema["properties"].(map[string]any)
3426

35-
// Add each input to the schema
36-
if len(jobConfig.Inputs) > 0 {
37-
properties := inputSchema["properties"].(map[string]any)
27+
for inputName, inputDef := range jobConfig.Inputs {
28+
property := map[string]any{}
3829

39-
for inputName, inputDef := range jobConfig.Inputs {
40-
property := map[string]any{}
41-
42-
// Add description
43-
if inputDef.Description != "" {
44-
property["description"] = inputDef.Description
45-
}
46-
47-
// Convert type to JSON Schema type
48-
switch inputDef.Type {
49-
case "choice":
50-
// Choice inputs are strings with enum constraints
51-
property["type"] = "string"
52-
if len(inputDef.Options) > 0 {
53-
property["enum"] = inputDef.Options
54-
}
55-
case "boolean":
56-
property["type"] = "boolean"
57-
case "number":
58-
property["type"] = "number"
59-
case "string", "":
60-
// Default to string if type is not specified
61-
property["type"] = "string"
62-
default:
63-
// For any unknown type, default to string
64-
property["type"] = "string"
65-
}
30+
if inputDef.Description != "" {
31+
property["description"] = inputDef.Description
32+
}
6633

67-
// Add default value if present
68-
if inputDef.Default != nil {
69-
property["default"] = inputDef.Default
34+
// Convert type to JSON Schema type
35+
switch inputDef.Type {
36+
case "choice":
37+
// Choice inputs are strings with enum constraints
38+
property["type"] = "string"
39+
if len(inputDef.Options) > 0 {
40+
property["enum"] = inputDef.Options
7041
}
42+
case "boolean":
43+
property["type"] = "boolean"
44+
case "number":
45+
property["type"] = "number"
46+
default:
47+
// "string", empty string, or any unknown type defaults to string
48+
property["type"] = "string"
49+
}
7150

72-
// Track required fields
73-
if inputDef.Required {
74-
requiredFields = append(requiredFields, inputName)
75-
}
51+
if inputDef.Default != nil {
52+
property["default"] = inputDef.Default
53+
}
7654

77-
properties[inputName] = property
55+
if inputDef.Required {
56+
requiredFields = append(requiredFields, inputName)
7857
}
58+
59+
properties[inputName] = property
7960
}
8061

81-
// Add required fields array if any inputs are required
8262
if len(requiredFields) > 0 {
8363
sort.Strings(requiredFields)
8464
inputSchema["required"] = requiredFields
8565
}
8666

87-
// Prevent additional properties to maintain schema strictness
88-
inputSchema["additionalProperties"] = false
89-
90-
tool["inputSchema"] = inputSchema
91-
9267
safeOutputsConfigLog.Printf("Generated tool definition for %s with %d inputs, %d required",
9368
jobName, len(jobConfig.Inputs), len(requiredFields))
9469

95-
return tool
70+
return map[string]any{
71+
"name": jobName,
72+
"description": description,
73+
"inputSchema": inputSchema,
74+
}
9675
}

0 commit comments

Comments
 (0)