Skip to content

Commit

Permalink
✨ Show stats when clipping prompto
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Dec 31, 2024
1 parent c2e0b29 commit f2fbb2a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
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"

Check failure on line 12 in pkg/server/handlers/prompt.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/weaviate/tiktoken-go; to add it:
)

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")

Check failure on line 158 in pkg/server/handlers/prompt.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tiktoken (typecheck)
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 f2fbb2a

Please sign in to comment.