-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathutils_v2.go
More file actions
181 lines (157 loc) · 6.88 KB
/
utils_v2.go
File metadata and controls
181 lines (157 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package client
/*
This file contains utility functions for the client package. (Mainly miscellaneous helpers)
It is used to append assistant responses to both OpenAI and in-app chat histories, and to create response items for chat interactions.
*/
import (
"context"
"paperdebugger/internal/libs/cfg"
"paperdebugger/internal/libs/db"
"paperdebugger/internal/libs/logger"
"paperdebugger/internal/services"
"paperdebugger/internal/services/toolkit/registry"
filetools "paperdebugger/internal/services/toolkit/tools/files"
latextools "paperdebugger/internal/services/toolkit/tools/latex"
"paperdebugger/internal/services/toolkit/tools/xtramcp"
chatv2 "paperdebugger/pkg/gen/api/chat/v2"
"strings"
"time"
openaiv3 "github.com/openai/openai-go/v3"
)
func appendAssistantTextResponseV2(openaiChatHistory *OpenAIChatHistory, inappChatHistory *AppChatHistory, content string, contentId string, modelSlug string) {
*openaiChatHistory = append(*openaiChatHistory, openaiv3.ChatCompletionMessageParamUnion{
OfAssistant: &openaiv3.ChatCompletionAssistantMessageParam{
Role: "assistant",
Content: openaiv3.ChatCompletionAssistantMessageParamContentUnion{
OfArrayOfContentParts: []openaiv3.ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion{
{
OfText: &openaiv3.ChatCompletionContentPartTextParam{
Type: "text",
Text: content,
},
},
},
},
},
})
*inappChatHistory = append(*inappChatHistory, chatv2.Message{
MessageId: contentId,
Payload: &chatv2.MessagePayload{
MessageType: &chatv2.MessagePayload_Assistant{
Assistant: &chatv2.MessageTypeAssistant{
Content: content,
ModelSlug: modelSlug,
},
},
},
Timestamp: time.Now().Unix(),
})
}
func getDefaultParamsV2(modelSlug string, toolRegistry *registry.ToolRegistryV2, isCustomModel bool) openaiv3.ChatCompletionNewParams {
var reasoningModels = []string{
"gpt-5",
"gpt-5-mini",
"gpt-5-nano",
"gpt-5-chat-latest",
"o4-mini",
"o3-mini",
"o3",
"o1-mini",
"o1",
"codex-mini-latest",
}
// Other model providers generally do not support the Store param
if isCustomModel {
return openaiv3.ChatCompletionNewParams{
Model: modelSlug,
Temperature: openaiv3.Float(0.7),
MaxCompletionTokens: openaiv3.Int(4000),
Tools: toolRegistry.GetTools(),
ParallelToolCalls: openaiv3.Bool(true),
}
}
for _, model := range reasoningModels {
if strings.Contains(modelSlug, model) {
return openaiv3.ChatCompletionNewParams{
Model: modelSlug,
MaxCompletionTokens: openaiv3.Int(4000),
Tools: toolRegistry.GetTools(),
ParallelToolCalls: openaiv3.Bool(true),
Store: openaiv3.Bool(false),
StreamOptions: openaiv3.ChatCompletionStreamOptionsParam{
IncludeUsage: openaiv3.Bool(true),
},
}
}
}
return openaiv3.ChatCompletionNewParams{
Model: modelSlug,
Temperature: openaiv3.Float(0.7),
MaxCompletionTokens: openaiv3.Int(4000), // DEBUG POINT: change this to test the frontend handler
Tools: toolRegistry.GetTools(), // Tool registration is managed centrally by the registry
ParallelToolCalls: openaiv3.Bool(true),
Store: openaiv3.Bool(false), // Must set to false, because we are construct our own chat history.
StreamOptions: openaiv3.ChatCompletionStreamOptionsParam{
IncludeUsage: openaiv3.Bool(true),
},
}
}
func CheckOpenAIWorksV2(oaiClient openaiv3.Client, baseUrl string, model string, logger *logger.Logger) {
logger.Info("[AI Client V2] checking if openai client works with " + baseUrl + " ..")
chatCompletion, err := oaiClient.Chat.Completions.New(context.TODO(), openaiv3.ChatCompletionNewParams{
Messages: []openaiv3.ChatCompletionMessageParamUnion{
openaiv3.UserMessage("Say 'openai client works'"),
},
Model: model,
})
if err != nil {
logger.Errorf("[AI Client V2] openai client does not work: %v", err)
return
}
logger.Info("[AI Client V2] openai client works", "response", chatCompletion.Choices[0].Message.Content)
}
func initializeToolkitV2(
db *db.DB,
projectService *services.ProjectService,
cfg *cfg.Cfg,
logger *logger.Logger,
) *registry.ToolRegistryV2 {
toolRegistry := registry.NewToolRegistryV2()
// Register static file tools (create/delete don't need ProjectService - they're placeholder only)
// toolRegistry.Register("create_file", filetools.CreateFileToolDescriptionV2, filetools.CreateFileTool)
// toolRegistry.Register("delete_file", filetools.DeleteFileToolDescriptionV2, filetools.DeleteFileTool)
// toolRegistry.Register("create_folder", filetools.CreateFolderToolDescriptionV2, filetools.CreateFolderTool)
// toolRegistry.Register("delete_folder", filetools.DeleteFolderToolDescriptionV2, filetools.DeleteFolderTool)
// Register file tools with ProjectService injection
readFileTool := filetools.NewReadFileTool(projectService)
toolRegistry.Register("read_file", filetools.ReadFileToolDescriptionV2, readFileTool.Call)
listFolderTool := filetools.NewListFolderTool(projectService)
toolRegistry.Register("list_folder", filetools.ListFolderToolDescriptionV2, listFolderTool.Call)
searchStringFromTheFileTool := filetools.NewSearchStringTool(projectService)
toolRegistry.Register("searchStringFromTheFile", filetools.SearchStringToolDescriptionV2, searchStringFromTheFileTool.Call)
searchFileTool := filetools.NewSearchFileTool(projectService)
toolRegistry.Register("search_file", filetools.SearchFileToolDescriptionV2, searchFileTool.Call)
// Register LaTeX tools with ProjectService injection
documentStructureTool := latextools.NewDocumentStructureTool(projectService)
toolRegistry.Register("get_document_structure", latextools.GetDocumentStructureToolDescriptionV2, documentStructureTool.Call)
toolRegistry.Register("locate_section", latextools.LocateSectionToolDescriptionV2, latextools.LocateSectionTool)
readSectionSourceTool := latextools.NewReadSectionSourceTool(projectService)
toolRegistry.Register("read_section_source", latextools.ReadSectionSourceToolDescriptionV2, readSectionSourceTool.Call)
readSourceLineRangeTool := latextools.NewReadSourceLineRangeTool(projectService)
toolRegistry.Register("read_source_line_range", latextools.ReadSourceLineRangeToolDescriptionV2, readSourceLineRangeTool.Call)
// Load tools dynamically from backend
xtraMCPLoader := xtramcp.NewXtraMCPLoaderV2(db, projectService, cfg.XtraMCPURI)
// initialize MCP session first and log session ID
sessionID, err := xtraMCPLoader.InitializeMCP()
if err != nil {
logger.Errorf("[XtraMCP Client] Failed to initialize XtraMCP session: %v", err)
} else {
logger.Info("[XtraMCP Client] XtraMCP session initialized", "sessionID", sessionID)
// dynamically load all tools from XtraMCP backend
err = xtraMCPLoader.LoadToolsFromBackend(toolRegistry)
if err != nil {
logger.Errorf("[XtraMCP Client] Failed to load XtraMCP tools: %v", err)
}
}
return toolRegistry
}