Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.7.0 h1:JxUKI6+CVBgCO2WToKy/nQk0sS+amI9z9EjVmdaocj4=
Expand Down Expand Up @@ -160,6 +162,8 @@ golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sU
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b h1:DXr+pvt3nC887026GRP39Ej11UATqWDmWuS99x26cD0=
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b/go.mod h1:4QTo5u+SEIbbKW1RacMZq1YEfOBqeXa19JeshGi+zc4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down Expand Up @@ -188,6 +192,8 @@ golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
Expand Down
4 changes: 2 additions & 2 deletions internal/api/chat/create_conversation_message_stream_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (s *ChatServerV2) CreateConversationMessageStream(
APIKey: settings.OpenAIAPIKey,
}

openaiChatHistory, inappChatHistory, err := s.aiClientV2.ChatCompletionStreamV2(ctx, stream, conversation.ID.Hex(), modelSlug, conversation.OpenaiChatHistoryCompletion, llmProvider)
openaiChatHistory, inappChatHistory, err := s.aiClientV2.ChatCompletionStreamV2(ctx, stream, conversation.UserID, conversation.ID.Hex(), modelSlug, conversation.OpenaiChatHistoryCompletion, llmProvider)
if err != nil {
return s.sendStreamError(stream, err)
}
Expand All @@ -307,7 +307,7 @@ func (s *ChatServerV2) CreateConversationMessageStream(
for i, bsonMsg := range conversation.InappChatHistory {
protoMessages[i] = mapper.BSONToChatMessageV2(bsonMsg)
}
title, err := s.aiClientV2.GetConversationTitleV2(ctx, protoMessages, llmProvider)
title, err := s.aiClientV2.GetConversationTitleV2(ctx, conversation.UserID, protoMessages, llmProvider)
if err != nil {
s.logger.Error("Failed to get conversation title", "error", err, "conversationID", conversation.ID.Hex())
return
Expand Down
3 changes: 3 additions & 0 deletions internal/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
commentv1 "paperdebugger/pkg/gen/api/comment/v1"
projectv1 "paperdebugger/pkg/gen/api/project/v1"
usagev1 "paperdebugger/pkg/gen/api/usage/v1"
userv1 "paperdebugger/pkg/gen/api/user/v1"

// "github.com/grpc-ecosystem/go-grpc-middleware"
Expand Down Expand Up @@ -106,6 +107,7 @@ func NewGrpcServer(
userServer userv1.UserServiceServer,
projectServer projectv1.ProjectServiceServer,
commentServer commentv1.CommentServiceServer,
usageServer usagev1.UsageServiceServer,
) *GrpcServer {
grpcServer := &GrpcServer{}
grpcServer.userService = userService
Expand All @@ -121,5 +123,6 @@ func NewGrpcServer(
userv1.RegisterUserServiceServer(grpcServer.Server, userServer)
projectv1.RegisterProjectServiceServer(grpcServer.Server, projectServer)
commentv1.RegisterCommentServiceServer(grpcServer.Server, commentServer)
usagev1.RegisterUsageServiceServer(grpcServer.Server, usageServer)
return grpcServer
}
6 changes: 6 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
commentv1 "paperdebugger/pkg/gen/api/comment/v1"
projectv1 "paperdebugger/pkg/gen/api/project/v1"
sharedv1 "paperdebugger/pkg/gen/api/shared/v1"
usagev1 "paperdebugger/pkg/gen/api/usage/v1"
userv1 "paperdebugger/pkg/gen/api/user/v1"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -105,6 +106,11 @@ func (s *Server) Run(addr string) {
s.logger.Fatalf("failed to register comment service grpc gateway: %v", err)
return
}
err = usagev1.RegisterUsageServiceHandler(context.Background(), mux, client)
if err != nil {
s.logger.Fatalf("failed to register usage service grpc gateway: %v", err)
return
}

s.logger.Infof("[PAPERDEBUGGER] http server listening on %s", addr)
s.ginServer.Any("/_pd/api/*path", func(c *gin.Context) { mux.ServeHTTP(c.Writer, c.Request) })
Expand Down
38 changes: 38 additions & 0 deletions internal/api/usage/get_session_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package usage

import (
"context"

"paperdebugger/internal/libs/contextutil"
usagev1 "paperdebugger/pkg/gen/api/usage/v1"

"google.golang.org/protobuf/types/known/timestamppb"
)

func (s *UsageServer) GetSessionUsage(
ctx context.Context,
req *usagev1.GetSessionUsageRequest,
) (*usagev1.GetSessionUsageResponse, error) {
actor, err := contextutil.GetActor(ctx)
if err != nil {
return nil, err
}

session, err := s.usageService.GetActiveSession(ctx, actor.ID)
if err != nil {
return nil, err
}

if session == nil {
return &usagev1.GetSessionUsageResponse{
Session: nil,
}, nil
}

return &usagev1.GetSessionUsageResponse{
Session: &usagev1.SessionUsage{
SessionExpiry: timestamppb.New(session.SessionExpiry.Time()),
TotalTokens: session.TotalTokens,
},
}, nil
}
29 changes: 29 additions & 0 deletions internal/api/usage/get_weekly_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package usage

import (
"context"

"paperdebugger/internal/libs/contextutil"
usagev1 "paperdebugger/pkg/gen/api/usage/v1"
)

func (s *UsageServer) GetWeeklyUsage(
ctx context.Context,
req *usagev1.GetWeeklyUsageRequest,
) (*usagev1.GetWeeklyUsageResponse, error) {
actor, err := contextutil.GetActor(ctx)
if err != nil {
return nil, err
}

stats, err := s.usageService.GetWeeklyUsage(ctx, actor.ID)
if err != nil {
return nil, err
}

return &usagev1.GetWeeklyUsageResponse{
Usage: &usagev1.WeeklyUsage{
TotalTokens: stats.TotalTokens,
},
}, nil
}
24 changes: 24 additions & 0 deletions internal/api/usage/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package usage

import (
"paperdebugger/internal/libs/logger"
"paperdebugger/internal/services"
usagev1 "paperdebugger/pkg/gen/api/usage/v1"
)

type UsageServer struct {
usagev1.UnimplementedUsageServiceServer

usageService *services.UsageService
logger *logger.Logger
}

func NewUsageServer(
usageService *services.UsageService,
logger *logger.Logger,
) usagev1.UsageServiceServer {
return &UsageServer{
usageService: usageService,
logger: logger,
}
}
42 changes: 41 additions & 1 deletion internal/libs/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"paperdebugger/internal/libs/cfg"
"paperdebugger/internal/libs/logger"
"paperdebugger/internal/models"

"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
Expand Down Expand Up @@ -43,5 +44,44 @@ func NewDB(cfg *cfg.Cfg, logger *logger.Logger) (*DB, error) {
}

logger.Info("[MONGO] initialized")
return &DB{Client: client, cfg: cfg, logger: logger}, nil

db := &DB{Client: client, cfg: cfg, logger: logger}
db.ensureIndexes()
return db, nil
}

// ensureIndexes creates necessary indexes for the database collections.
func (db *DB) ensureIndexes() {
sessions := db.Database("paperdebugger").Collection((models.LLMSession{}).CollectionName())

// TTL index: auto-delete sessions after 30 days past their expiry time
_, err := sessions.Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.D{{Key: "session_expiry", Value: 1}},
Options: options.Index().SetExpireAfterSeconds(30 * 24 * 60 * 60),
})
if err != nil {
db.logger.Error("Failed to create TTL index on llm_sessions", "error", err)
}

// Compound index for efficient active session lookups
_, err = sessions.Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.D{
{Key: "user_id", Value: 1},
{Key: "session_expiry", Value: -1},
},
})
if err != nil {
db.logger.Error("Failed to create compound index on llm_sessions", "error", err)
}

// Compound index for usage queries and recent session lookups
_, err = sessions.Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.D{
{Key: "user_id", Value: 1},
{Key: "session_start", Value: -1},
},
})
if err != nil {
db.logger.Error("Failed to create session_start index on llm_sessions", "error", err)
}
}
19 changes: 19 additions & 0 deletions internal/models/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package models

import "go.mongodb.org/mongo-driver/v2/bson"

// LLMSession represents a user's session for tracking LLM usage and token counts.
type LLMSession struct {
ID bson.ObjectID `bson:"_id"`
UserID bson.ObjectID `bson:"user_id"`
SessionStart bson.DateTime `bson:"session_start"`
SessionExpiry bson.DateTime `bson:"session_expiry"`
PromptTokens int64 `bson:"prompt_tokens"`
CompletionTokens int64 `bson:"completion_tokens"`
TotalTokens int64 `bson:"total_tokens"`
RequestCount int64 `bson:"request_count"`
}

func (s LLMSession) CollectionName() string {
return "llm_sessions"
}
3 changes: 3 additions & 0 deletions internal/services/toolkit/client/client_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type AIClientV2 struct {

reverseCommentService *services.ReverseCommentService
projectService *services.ProjectService
usageService *services.UsageService
cfg *cfg.Cfg
logger *logger.Logger
}
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewAIClientV2(

reverseCommentService *services.ReverseCommentService,
projectService *services.ProjectService,
usageService *services.UsageService,
cfg *cfg.Cfg,
logger *logger.Logger,
) *AIClientV2 {
Expand Down Expand Up @@ -107,6 +109,7 @@ func NewAIClientV2(

reverseCommentService: reverseCommentService,
projectService: projectService,
usageService: usageService,
cfg: cfg,
logger: logger,
}
Expand Down
26 changes: 21 additions & 5 deletions internal/services/toolkit/client/completion_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"encoding/json"
"paperdebugger/internal/models"
"paperdebugger/internal/services"
"paperdebugger/internal/services/toolkit/handler"
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
"strings"

"github.com/openai/openai-go/v3"
"go.mongodb.org/mongo-driver/v2/bson"
)

// define []openai.ChatCompletionMessageParamUnion as OpenAIChatHistory
Expand All @@ -25,8 +27,8 @@ import (
// 1. The full chat history sent to the language model (including any tool call results).
// 2. The incremental chat history visible to the user (including tool call results and assistant responses).
// 3. An error, if any occurred during the process.
func (a *AIClientV2) ChatCompletionV2(ctx context.Context, modelSlug string, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
openaiChatHistory, inappChatHistory, err := a.ChatCompletionStreamV2(ctx, nil, "", modelSlug, messages, llmProvider)
func (a *AIClientV2) ChatCompletionV2(ctx context.Context, userID bson.ObjectID, modelSlug string, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
openaiChatHistory, inappChatHistory, err := a.ChatCompletionStreamV2(ctx, nil, userID, "", modelSlug, messages, llmProvider)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -54,7 +56,7 @@ func (a *AIClientV2) ChatCompletionV2(ctx context.Context, modelSlug string, mes
// - If tool calls are required, it handles them and appends the results to the chat history, then continues the loop.
// - If no tool calls are needed, it appends the assistant's response and exits the loop.
// - Finally, it returns the updated chat histories and any error encountered.
func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream chatv2.ChatService_CreateConversationMessageStreamServer, conversationId string, modelSlug string, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream chatv2.ChatService_CreateConversationMessageStreamServer, userID bson.ObjectID, conversationId string, modelSlug string, messages OpenAIChatHistory, llmProvider *models.LLMProviderConfig) (OpenAIChatHistory, AppChatHistory, error) {
openaiChatHistory := messages
inappChatHistory := AppChatHistory{}

Expand Down Expand Up @@ -97,7 +99,22 @@ func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream

if len(chunk.Choices) == 0 {
// Handle usage information
// fmt.Printf("Usage: %+v\n", chunk.Usage)
if chunk.Usage.TotalTokens > 0 {
// Record usage asynchronously to avoid blocking the response
go func(usage services.UsageRecord) {
bgCtx := context.Background()
if err := a.usageService.RecordUsage(bgCtx, usage); err != nil {
a.logger.Error("Failed to store usage", "error", err)
return
}

}(services.UsageRecord{
UserID: userID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick check, you are using the UserID generated by PD backend right?

I noticed there might be an interesting edge case. UserID i believe is generated by user email address on overleaf. Now, if the user logs in via Overleaf, a UserID is generated. If the same user logs in via google account, I think a different UserID for the same user might be generated too (it'll be the same if the same gmail is registered on Overleaf).

Ideally we should recognise and combine / avoid re-generating UserID. But this is a separate problem and requires a fix (if indeed the case) that should not be overloaded on this PR.

If its convenient, could you also test and verify during integration testing? Can try two different login methods on the same Overleaf project and we should expect two tokens usage tracking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, it's the UserID generated by PD backend. Sure, will take note when testing. I also wonder if a user could just keep switching emails for the same Overleaf project. Shall we have a per-user limit and a per-project limit?

cc: @Junyi-99

Copy link
Member

@4ndrelim 4ndrelim Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah switching out emails will generate a different UserID i believe. Not sure if having a limit on ProjectID is wise because we can have diff collaborators working on the same project.

Edit: Not sure if Overleaf has any safeguards / cooldowns on switching out emails too frequently. But yeah, we can keep this in mind since its a separate problem. Ideally UserID is generated in a way that it is unique and accurately tied to account.

PromptTokens: chunk.Usage.PromptTokens,
CompletionTokens: chunk.Usage.CompletionTokens,
TotalTokens: chunk.Usage.TotalTokens,
})
}
continue
}

Expand Down Expand Up @@ -185,7 +202,6 @@ func (a *AIClientV2) ChatCompletionStreamV2(ctx context.Context, callbackStream
// answer_content += chunk.Choices[0].Delta.Content
// fmt.Printf("answer_content: %s\n", answer_content)
streamHandler.HandleTextDoneItem(chunk, answer_content, reasoning_content)
break
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/toolkit/client/get_citation_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (a *AIClientV2) GetCitationKeys(ctx context.Context, sentence string, userI
// Bibliography is placed at the start of the prompt to leverage prompt caching
message := fmt.Sprintf("Bibliography: %s\nSentence: %s\nBased on the sentence and bibliography, suggest only the most relevant citation keys separated by commas with no spaces (e.g. key1,key2). Be selective and only include citations that are directly relevant. Avoid suggesting more than 3 citations. If no relevant citations are found, return '%s'.", bibliography, sentence, emptyCitation)

_, resp, err := a.ChatCompletionV2(ctx, "gpt-5.2", OpenAIChatHistory{
_, resp, err := a.ChatCompletionV2(ctx, userId, "gpt-5.2", OpenAIChatHistory{
openai.SystemMessage("You are a helpful assistant that suggests relevant citation keys."),
openai.UserMessage(message),
}, llmProvider)
Expand Down
2 changes: 2 additions & 0 deletions internal/services/toolkit/client/get_citation_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ func setupTestClient(t *testing.T) (*client.AIClientV2, *services.ProjectService
}

projectService := services.NewProjectService(dbInstance, cfg.GetCfg(), logger.GetLogger())
usageService := services.NewUsageService(dbInstance, cfg.GetCfg(), logger.GetLogger())
aiClient := client.NewAIClientV2(
dbInstance,
&services.ReverseCommentService{},
projectService,
usageService,
cfg.GetCfg(),
logger.GetLogger(),
)
Expand Down
5 changes: 3 additions & 2 deletions internal/services/toolkit/client/get_conversation_title_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (

"github.com/openai/openai-go/v3"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/v2/bson"
)

func (a *AIClientV2) GetConversationTitleV2(ctx context.Context, inappChatHistory []*chatv2.Message, llmProvider *models.LLMProviderConfig) (string, error) {
func (a *AIClientV2) GetConversationTitleV2(ctx context.Context, userID bson.ObjectID, inappChatHistory []*chatv2.Message, llmProvider *models.LLMProviderConfig) (string, error) {
messages := lo.Map(inappChatHistory, func(message *chatv2.Message, _ int) string {
if _, ok := message.Payload.MessageType.(*chatv2.MessagePayload_Assistant); ok {
return fmt.Sprintf("Assistant: %s", message.Payload.GetAssistant().GetContent())
Expand All @@ -29,7 +30,7 @@ func (a *AIClientV2) GetConversationTitleV2(ctx context.Context, inappChatHistor
message := strings.Join(messages, "\n")
message = fmt.Sprintf("%s\nBased on above conversation, generate a short, clear, and descriptive title that summarizes the main topic or purpose of the discussion. The title should be concise, specific, and use natural language. Avoid vague or generic titles. Use abbreviation and short words if possible. Use 3-5 words if possible. Give me the title only, no other text including any other words.", message)

_, resp, err := a.ChatCompletionV2(ctx, "gpt-5-nano", OpenAIChatHistory{
_, resp, err := a.ChatCompletionV2(ctx, userID, "gpt-5-nano", OpenAIChatHistory{
openai.SystemMessage("You are a helpful assistant that generates a title for a conversation."),
openai.UserMessage(message),
}, llmProvider)
Expand Down
Loading