From f2fbb2a2fa883af693d7cacc8a174f00fd4e4e1f Mon Sep 17 00:00:00 2001 From: Manuel Odendahl Date: Tue, 31 Dec 2024 18:21:22 -0500 Subject: [PATCH] :sparkles: Show stats when clipping prompto --- pkg/server/handlers/prompt.go | 41 +++++++++++++++++++++++++++++++ pkg/server/static/js/favorites.js | 19 +++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pkg/server/handlers/prompt.go b/pkg/server/handlers/prompt.go index 6b2f823..167d7f1 100644 --- a/pkg/server/handlers/prompt.go +++ b/pkg/server/handlers/prompt.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/json" "fmt" "net/http" "strings" @@ -8,8 +9,21 @@ import ( "github.com/go-go-golems/prompto/pkg" "github.com/go-go-golems/prompto/pkg/server/templates/components" "github.com/rs/zerolog/log" + "github.com/weaviate/tiktoken-go" ) +type PromptResponse struct { + Name string `json:"name"` + Group string `json:"group"` + Repository string `json:"repository"` + Content string `json:"content"` + Stats struct { + Tokens int `json:"tokens"` + Lines int `json:"lines"` + Size int `json:"size"` + } `json:"stats"` +} + func (h *Handlers) PromptList() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { parts := strings.Split(r.URL.Path, "/") @@ -139,6 +153,33 @@ func (h *Handlers) PromptContent() http.HandlerFunc { return } + // Check if JSON is requested + if r.Header.Get("Accept") == "application/json" { + tokenCounter, err := tiktoken.GetEncoding("cl100k_base") + if err != nil { + http.Error(w, "Error initializing token counter", http.StatusInternalServerError) + return + } + + tokens := tokenCounter.Encode(content, nil, nil) + response := PromptResponse{ + Name: foundFile.Name, + Group: group, + Repository: foundFile.Repository, + Content: content, + } + response.Stats.Tokens = len(tokens) + response.Stats.Lines = strings.Count(content, "\n") + 1 + response.Stats.Size = len(content) + + w.Header().Set("Content-Type", "application/json") + err = json.NewEncoder(w).Encode(response) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + return + } + w.Header().Set("Content-Type", "text/markdown") _, _ = w.Write([]byte(content)) } diff --git a/pkg/server/static/js/favorites.js b/pkg/server/static/js/favorites.js index a795617..fce2a66 100644 --- a/pkg/server/static/js/favorites.js +++ b/pkg/server/static/js/favorites.js @@ -10,11 +10,22 @@ function getFavorites() { } function copyToClipboard(text) { - fetch("/prompts/" + text) - .then(response => response.text()) - .then(content => { - navigator.clipboard.writeText(content).then(() => { + fetch("/prompts/" + text, { + headers: { + 'Accept': 'application/json' + } + }) + .then(response => response.json()) + .then(data => { + navigator.clipboard.writeText(data.content).then(() => { const toastEl = document.getElementById('copyToast'); + const toastBody = toastEl.querySelector('.toast-body'); + toastBody.innerHTML = ` + Copied to clipboard!
+ + ${data.stats.tokens} tokens • ${data.stats.lines} lines • ${data.stats.size} bytes + + `; const toast = new bootstrap.Toast(toastEl); toast.show(); });