Skip to content

Commit

Permalink
Merge pull request #25 from wesen/task/show-stats-clip
Browse files Browse the repository at this point in the history
Add prompt statistics when copying to clipboard
  • Loading branch information
wesen authored Dec 31, 2024
2 parents 9e7d819 + fad401f commit 03f9b4a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ go 1.22
toolchain go1.23.3

require (
github.com/a-h/templ v0.2.793
github.com/charmbracelet/huh v0.6.0
github.com/go-go-golems/clay v0.1.17
github.com/go-go-golems/glazed v0.5.18
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/weaviate/tiktoken-go v0.0.2
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/a-h/templ v0.2.793 // indirect
github.com/adrg/frontmatter v0.2.0 // indirect
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ github.com/tj/assert v0.0.0-20190920132354-ee03d75cd160 h1:NSWpaDaurcAJY7PkL8Xt0
github.com/tj/assert v0.0.0-20190920132354-ee03d75cd160/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/go-naturaldate v1.3.0 h1:OgJIPkR/Jk4bFMBLbxZ8w+QUxwjqSvzd9x+yXocY4RI=
github.com/tj/go-naturaldate v1.3.0/go.mod h1:rpUbjivDKiS1BlfMGc2qUKNZ/yxgthOfmytQs8d8hKk=
github.com/weaviate/tiktoken-go v0.0.2 h1:lkwTMEoXSFSXxYvw1c44/xz3OOAh27L3+3vvNN/TSSg=
github.com/weaviate/tiktoken-go v0.0.2/go.mod h1:u47qSckEGSi4sOcVJmUnd3xoHpDV9/5FDDi3KUCFUq4=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
Expand Down
41 changes: 41 additions & 0 deletions pkg/server/handlers/prompt.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package handlers

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"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, "/")
Expand Down Expand Up @@ -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))
}
Expand Down
19 changes: 15 additions & 4 deletions pkg/server/static/js/favorites.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
<i class="bi bi-clipboard-check me-2"></i>Copied to clipboard!<br>
<small class="text-white-50">
${data.stats.tokens} tokens • ${data.stats.lines} lines • ${data.stats.size} bytes
</small>
`;
const toast = new bootstrap.Toast(toastEl);
toast.show();
});
Expand Down

0 comments on commit 03f9b4a

Please sign in to comment.