diff --git a/pkg/display/preload.go b/pkg/display/preload.go index d0004b33..494bddc7 100644 --- a/pkg/display/preload.go +++ b/pkg/display/preload.go @@ -413,7 +413,34 @@ func (s *Service) injectCoreMLShim() string { headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }); - return response.json(); + if (!response.ok) { + throw new Error("Core ML request failed: " + response.status + " " + response.statusText); + } + const body = await response.text(); + try { + const parsed = JSON.parse(body); + const content = parsed?.choices?.[0]?.message?.content; + if (typeof content === "string") { + return content; + } + if (Array.isArray(content)) { + return content.map((part) => { + if (typeof part === "string") { + return part; + } + return part?.text ?? ""; + }).join(""); + } + if (typeof parsed?.content === "string") { + return parsed.content; + } + if (typeof parsed === "string") { + return parsed; + } + return body; + } catch (_) { + return body; + } }, async stream(input) { const payload = typeof input === "string" diff --git a/pkg/display/scheme.go b/pkg/display/scheme.go index b2cba93a..5e9d2d32 100644 --- a/pkg/display/scheme.go +++ b/pkg/display/scheme.go @@ -5,6 +5,7 @@ import ( "html" "net/http" "net/url" + "sort" "strings" "time" @@ -130,18 +131,7 @@ func (s *Service) resolveStoreRoute(subpath string, query url.Values) core.Resul } } - results := s.searchAllStorage(coalesce(query.Get("q"), subpath)) - return core.Result{ - Value: map[string]any{ - "content_type": "text/html", - "body": s.renderStoreSearchPage(coalesce(query.Get("q"), subpath), results), - "route": "store", - "url": "core://store", - "query": query, - "results": results, - }, - OK: true, - } + return s.handleStoreSearch(context.Background(), query) } func (s *Service) resolveModelsRoute(subpath string, query url.Values) core.Result { @@ -345,29 +335,63 @@ func (s *Service) renderUnavailableRoute(route, subpath string, query url.Values func (s *Service) renderStoreSearchPage(query string, results []StorageEntry) string { safeQuery := html.EscapeString(query) + type groupedResults struct { + Origin string + Entries []StorageEntry + UpdatedAt time.Time + } + + groupMap := make(map[string]*groupedResults) + for _, item := range results { + group := groupMap[item.Origin] + if group == nil { + group = &groupedResults{Origin: item.Origin} + groupMap[item.Origin] = group + } + group.Entries = append(group.Entries, item) + if item.UpdatedAt.After(group.UpdatedAt) { + group.UpdatedAt = item.UpdatedAt + } + } + + groups := make([]groupedResults, 0, len(groupMap)) + for _, group := range groupMap { + groups = append(groups, *group) + } + sort.Slice(groups, func(i, j int) bool { + return groups[i].UpdatedAt.After(groups[j].UpdatedAt) + }) + for i := range groups { + sort.Slice(groups[i].Entries, func(a, b int) bool { + return groups[i].Entries[a].UpdatedAt.After(groups[i].Entries[b].UpdatedAt) + }) + } + var items strings.Builder if len(results) == 0 && strings.TrimSpace(query) != "" { items.WriteString("

No matches found in Core storage.

") } else if strings.TrimSpace(query) == "" { items.WriteString("

Enter a search term to scan Core storage namespaces.

") } else { - items.WriteString("") } - return "core://store
core://store
Search the in-memory storage scopes exposed by the preload shim. Query: " + + return "core://store
core://store
Search the in-memory storage scopes exposed by the preload shim. Query: " + safeQuery + "