refactor: deslop and simplify abstract field plumbing

- Extract toMinimalTemplate helper in codersdk/toolsdk so the ListTemplates
  and GetTemplate tools share the projection (prevents drift).
- Use strings.Join over filtered parts in chatgpt.searchTemplates instead
  of conditional += separator logic.
- Drop redundant narrative comments in chattool and toolsdk tests that
  restated what the test names already convey.
- Drop the truncation comment in chattool.ListTemplates so the abstract
  branch matches the silent description branch above it.
This commit is contained in:
Michael Suchacz
2026-05-22 06:35:11 +00:00
parent 909ae69293
commit bc3f975a60
6 changed files with 22 additions and 37 deletions
-2
View File
@@ -129,8 +129,6 @@ func ListTemplates(db database.Store, organizationID uuid.UUID, options ListTemp
item["description"] = truncateRunes(desc, 200)
}
if abstract := strings.TrimSpace(t.Abstract); abstract != "" {
// Truncate in the list view to keep responses small. The full abstract
// is available through read_template for the selected candidate.
item["abstract"] = truncateRunes(abstract, 500)
}
if count, ok := ownerCounts[t.ID]; ok && count > 0 {
@@ -160,8 +160,6 @@ func TestListTemplates_Abstract(t *testing.T) {
m := templates[0].(map[string]any)
require.Equal(t, tpl.ID.String(), m["id"].(string))
require.Equal(t, "short description", m["description"].(string))
// List view truncates abstracts to keep responses small; the full text
// remains accessible via read_template.
require.Equal(t, strings.Repeat("a", 500), m["abstract"].(string))
})
@@ -247,8 +245,6 @@ func TestListTemplates_Abstract(t *testing.T) {
OrganizationID: org.ID,
})
// Proves the TrimSpace guard is load-bearing; a non-empty but
// whitespace-only abstract must still omit the key.
dbgen.Template(t, db, database.Template{
OrganizationID: org.ID,
CreatedBy: user.ID,
@@ -282,8 +282,6 @@ func TestReadTemplate_Abstract(t *testing.T) {
OrganizationID: org.ID,
CreatedBy: user.ID,
})
// Proves the TrimSpace guard is load-bearing; a non-empty but
// whitespace-only abstract must still omit the key.
tmpl := dbgen.Template(t, db, database.Template{
OrganizationID: org.ID,
CreatedBy: user.ID,
+7 -9
View File
@@ -57,19 +57,17 @@ func searchTemplates(ctx context.Context, deps Deps, query string) ([]SearchResu
}
results := make([]SearchResultItem, len(templates))
for i, template := range templates {
// Append Abstract so the discovery surface can rank similarly-described
// templates by the agent-targeted summary.
text := template.Description
if abstract := strings.TrimSpace(template.Abstract); abstract != "" {
if text != "" {
text += "\n\n"
}
text += abstract
parts := make([]string, 0, 2)
if d := template.Description; d != "" {
parts = append(parts, d)
}
if a := strings.TrimSpace(template.Abstract); a != "" {
parts = append(parts, a)
}
results[i] = SearchResultItem{
ID: createObjectID(ObjectTypeTemplate, template.ID.String()).String(),
Title: template.DisplayName,
Text: text,
Text: strings.Join(parts, "\n\n"),
URL: fmt.Sprintf("%s/templates/%s/%s", serverURL, template.OrganizationName, template.Name),
}
}
+15 -19
View File
@@ -635,15 +635,7 @@ var ListTemplates = Tool[NoArgs, []MinimalTemplate]{
}
minimalTemplates := make([]MinimalTemplate, len(templates))
for i, template := range templates {
minimalTemplates[i] = MinimalTemplate{
DisplayName: template.DisplayName,
ID: template.ID.String(),
Name: template.Name,
Description: template.Description,
Abstract: template.Abstract,
ActiveVersionID: template.ActiveVersionID,
ActiveUserCount: template.ActiveUserCount,
}
minimalTemplates[i] = toMinimalTemplate(template)
}
return minimalTemplates, nil
},
@@ -773,16 +765,8 @@ When selecting a preset: if a preset is marked default and the user has not spec
return TemplateDetail{}, xerrors.Errorf("get template presets: %w", err)
}
detail := TemplateDetail{
MinimalTemplate: MinimalTemplate{
DisplayName: template.DisplayName,
ID: template.ID.String(),
Name: template.Name,
Description: template.Description,
Abstract: template.Abstract,
ActiveVersionID: template.ActiveVersionID,
ActiveUserCount: template.ActiveUserCount,
},
Parameters: parameters,
MinimalTemplate: toMinimalTemplate(template),
Parameters: parameters,
}
for _, p := range presets {
detail.Presets = append(detail.Presets, toPresetView(p))
@@ -1726,6 +1710,18 @@ type MinimalTemplate struct {
ActiveUserCount int `json:"active_user_count"`
}
func toMinimalTemplate(t codersdk.Template) MinimalTemplate {
return MinimalTemplate{
DisplayName: t.DisplayName,
ID: t.ID.String(),
Name: t.Name,
Description: t.Description,
Abstract: t.Abstract,
ActiveVersionID: t.ActiveVersionID,
ActiveUserCount: t.ActiveUserCount,
}
}
type WorkspaceLSArgs struct {
Workspace string `json:"workspace"`
Path string `json:"path"`
-1
View File
@@ -315,7 +315,6 @@ func TestTools(t *testing.T) {
t.Run("ListTemplatesPropagatesAbstract", func(t *testing.T) {
ctx := testutil.Context(t, testutil.WaitShort)
// Set Abstract on the existing fixture so the list response surfaces it.
abstract := "Agent-targeted summary for the shared fixture template."
_, err := client.UpdateTemplateMeta(ctx, r.Template.ID, codersdk.UpdateTemplateMeta{
Abstract: &abstract,