Skip to content

Commit 938d55d

Browse files
prachi-shah-harnessHarness
authored andcommitted
fix: [ML-1380]: add template_ref field (#180)
* 34cdb3 fix lint error * 4bf225 Revert "merge master" * 436305 merge master * 6210c7 merge with master * c6932d merge master * 18e4d1 run make format * 59bcde modify intelligent_template_search response * 268a4e fix: [ML-1380]: add template_ref field * b18613 fix: [ML-1380]: add template_ref field
1 parent f285e56 commit 938d55d

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

client/dto/intelligence.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type SimilaritySearchResponse struct {
2323
Results []SimilaritySearchResult `json:"results"`
2424
}
2525

26+
type TemplateData string
27+
2628
// ContextType defines the type of context for AI DevOps agent
2729
type ContextType string
2830

client/intelligence.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (ts *IntelligenceService) buildPath(basePath string) string {
2828
}
2929

3030
// SimilaritySearch searches for similar templates based on the provided request
31-
func (ts *IntelligenceService) SimilaritySearch(ctx context.Context, request *dto.SimilaritySearchRequest) (*dto.SimilaritySearchResponse, error) {
31+
func (ts *IntelligenceService) SimilaritySearch(ctx context.Context, request *dto.SimilaritySearchRequest) (*dto.TemplateData, error) {
3232
endpoint := ts.buildPath(similaritySearchPath)
3333

3434
// Validate required parameters
@@ -67,7 +67,60 @@ func (ts *IntelligenceService) SimilaritySearch(ctx context.Context, request *dt
6767
return nil, fmt.Errorf("failed to perform similarity search: %w", err)
6868
}
6969

70-
return &result, nil
70+
var templateRef string
71+
72+
res := result.Results[0]
73+
// Check if metadata is an array of key-value pairs
74+
metadataArray, ok := res.Metadata.([]interface{})
75+
if !ok {
76+
return nil, fmt.Errorf("metadata is not an array of key-value pairs")
77+
}
78+
79+
// Extract template_id, org_id, and project_id from metadata
80+
var templateID, orgID, projectID string
81+
for _, item := range metadataArray {
82+
kvPair, ok := item.(map[string]interface{})
83+
if !ok {
84+
continue
85+
}
86+
87+
key, keyOk := kvPair["Key"].(string)
88+
value, valueOk := kvPair["Value"].(string)
89+
90+
if !keyOk || !valueOk {
91+
continue
92+
}
93+
94+
switch key {
95+
case "template_id":
96+
templateID = value
97+
case "org_id":
98+
orgID = value
99+
case "project_id":
100+
projectID = value
101+
}
102+
}
103+
104+
// Skip if template_id is missing
105+
if templateID == "" {
106+
return nil, fmt.Errorf("template_id is missing")
107+
}
108+
109+
// Build template reference based on scope
110+
templateRef = buildTemplateRef(orgID, projectID, templateID)
111+
templateData := dto.TemplateData("template_ref: " + templateRef + " type: " + request.TemplateType)
112+
113+
return &templateData, nil
114+
}
115+
116+
func buildTemplateRef(orgID, projectID, templateID string) string {
117+
if projectID != "" {
118+
return templateID
119+
} else if orgID != "" {
120+
return fmt.Sprintf("org.%s", templateID)
121+
} else {
122+
return fmt.Sprintf("account.%s", templateID)
123+
}
71124
}
72125

73126
// sendAIDevOpsChatRequest handles sending AI DevOps chat requests with support for both streaming and non-streaming modes

pkg/harness/tools/intelligence.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
// FindSimilarTemplates creates a tool that allows finding similar templates based on provided description.
2020
func FindSimilarTemplates(config *config.Config, client *client.IntelligenceService) (tool mcp.Tool, handler server.ToolHandlerFunc) {
2121
return mcp.NewTool("intelligent_template_search",
22-
mcp.WithDescription("Finds the most relevant templates based on a natural language description. "+
22+
mcp.WithDescription("The tool is used to find most relevant entity template for any entity everytime a template is explicitly requested for any of pipeline/stage/step"+
23+
"Finds the most relevant templates based on a natural language description. "+
2324
"Searches across template identifiers, names, types, capabilities, and use cases to find the best matches. "+
2425
"Returns templates ranked by similarity score with metadata including template IDs and organizational context. "+
2526
"Ideal for discovering templates that fulfill specific requirements without knowing exact identifiers."),
@@ -32,6 +33,7 @@ func FindSimilarTemplates(config *config.Config, client *client.IntelligenceServ
3233
),
3334
mcp.WithNumber("count",
3435
mcp.Description("Maximum number of similar templates to return"),
36+
mcp.DefaultNumber(1),
3537
),
3638
common.WithScope(config, false),
3739
),

0 commit comments

Comments
 (0)