-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresearch.go
More file actions
844 lines (710 loc) · 28.1 KB
/
research.go
File metadata and controls
844 lines (710 loc) · 28.1 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type ResearchManager struct {
app *App
client *http.Client
apiKey string
queue chan *ResearchTask
running map[string]*ResearchTask
mutex sync.RWMutex
done chan bool
lastDebugInfo string // Store debug info for research note
lastUsage Usage // Store usage statistics
lastSearchCount int // Count of web searches performed
lastFetchCount int // Count of web fetches performed
debugLogPath string
debugLogMux sync.Mutex
}
type ResearchTask struct {
ID string `json:"id"`
Title string `json:"title"`
Prompt string `json:"prompt"`
Status string `json:"status"` // pending, running, completed, failed
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time,omitempty"`
Result string `json:"result,omitempty"`
Error string `json:"error,omitempty"`
SourceEntry int `json:"source_entry"` // ID of the entry containing the research prompt
DebugMode bool `json:"debug_mode"` // Whether to include full debug info
notifications []string // Buffer for collecting notifications during research
notifyMux sync.Mutex // Protect notifications slice
}
// addNotification adds a notification message to the task's buffer
func (t *ResearchTask) addNotification(message string) {
t.notifyMux.Lock()
defer t.notifyMux.Unlock()
t.notifications = append(t.notifications, message)
}
type ClaudeRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Messages []Message `json:"messages"`
Tools []Tool `json:"tools"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Tool struct {
Type string `json:"type"`
Name string `json:"name"`
MaxUses int `json:"max_uses,omitempty"`
AllowedDomains []string `json:"allowed_domains,omitempty"`
BlockedDomains []string `json:"blocked_domains,omitempty"`
UserLocation *UserLocation `json:"user_location,omitempty"`
Citations *Citations `json:"citations,omitempty"`
MaxContentTokens int `json:"max_content_tokens,omitempty"`
}
type Citations struct {
Enabled bool `json:"enabled"`
}
type UserLocation struct {
Type string `json:"type"`
City string `json:"city,omitempty"`
Region string `json:"region,omitempty"`
Country string `json:"country,omitempty"`
Timezone string `json:"timezone,omitempty"`
}
type ClaudeResponse struct {
Content []ContentBlock `json:"content"`
Usage Usage `json:"usage"`
StopReason string `json:"stop_reason,omitempty"`
}
type ContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ToolUse *ToolUseBlock `json:"tool_use,omitempty"`
ToolResult *ToolResultBlock `json:"tool_result,omitempty"`
Raw map[string]interface{} `json:"-"` // For debugging unknown fields
}
type ToolUseBlock struct {
ID string `json:"id"`
Name string `json:"name"`
Input interface{} `json:"input"`
}
type ToolResultBlock struct {
ToolUseID string `json:"tool_use_id"`
Content []interface{} `json:"content"`
}
type Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
func NewResearchManager(app *App, apiKey string) *ResearchManager {
rm := &ResearchManager{
app: app,
client: &http.Client{Timeout: 300 * time.Second}, // 5 minute timeout for research
apiKey: apiKey,
queue: make(chan *ResearchTask, 10),
running: make(map[string]*ResearchTask),
done: make(chan bool),
debugLogPath: filepath.Join("vimango_research_debug", "research.log"),
}
// Start background worker
go rm.worker()
return rm
}
func (rm *ResearchManager) worker() {
for {
select {
case task := <-rm.queue:
rm.processTask(task)
case <-rm.done:
return
}
}
}
func (rm *ResearchManager) processTask(task *ResearchTask) {
// Add panic recovery for the entire task processing
defer func() {
if r := recover(); r != nil {
rm.logDebug("PANIC in processTask for task %s: %v", task.ID, r)
rm.mutex.Lock()
task.Status = "failed"
task.Error = fmt.Sprintf("Task processing panic: %v", r)
task.EndTime = time.Now()
task.addNotification(fmt.Sprintf("❌ Task processing panic: %v", r))
delete(rm.running, task.ID)
rm.mutex.Unlock()
rm.displayAggregatedNotification(task)
}
}()
rm.logDebug("Starting processing for task %s: %s", task.ID, task.Title)
// Add initial status notification
task.addNotification(fmt.Sprintf("🔍 Research started for: %s", task.Title))
rm.mutex.Lock()
task.Status = "running"
task.StartTime = time.Now()
rm.running[task.ID] = task
rm.mutex.Unlock()
rm.logDebug("Task %s marked as running, performing research...", task.ID)
// Perform the research with additional validation
result, err := rm.performResearch(task.Prompt, task.DebugMode)
rm.logDebug("Research completed for task %s, result length: %d, error: %v",
task.ID, len(result), err != nil)
rm.mutex.Lock()
task.EndTime = time.Now()
if err != nil {
task.Status = "failed"
task.Error = err.Error()
task.addNotification(fmt.Sprintf("❌ Research failed: %s", task.Error))
rm.logDebug("Task %s marked as failed: %s", task.ID, task.Error)
} else {
task.Status = "completed"
task.Result = result
task.addNotification(fmt.Sprintf("✅ Research completed successfully (%d characters)", len(result)))
rm.logDebug("Task %s marked as completed, creating research note...", task.ID)
// Remove from running tasks before creating note (since we're now synchronous)
delete(rm.running, task.ID)
rm.mutex.Unlock()
// Create new note with research results synchronously
rm.createResearchNote(task)
// Lock again to complete the cleanup
rm.mutex.Lock()
}
// Remove from running tasks (if not already removed)
delete(rm.running, task.ID)
rm.mutex.Unlock()
rm.logDebug("Task %s processing completed, displaying aggregated notification", task.ID)
// Display aggregated notification to user
rm.displayAggregatedNotification(task)
}
func (rm *ResearchManager) performResearch(prompt string, debugMode bool) (string, error) {
// Enhanced research prompt for comprehensive investigation
content, err := os.ReadFile("research_prompt")
if err != nil {
return "", fmt.Errorf("Reading research_prompt file failed: %w", err)
}
researchPrompt := fmt.Sprintf(string(content), prompt)
// Configure web search tool with better parameters
webSearchTool := Tool{
Type: "web_search_20250305",
Name: "web_search",
MaxUses: 15, // Increased for more thorough research
UserLocation: &UserLocation{
Type: "approximate",
Country: "US",
Timezone: "America/New_York",
},
}
// Configure web fetch tool for deep content analysis
webFetchTool := Tool{
Type: "web_fetch_20250910",
Name: "web_fetch",
MaxUses: 8, // Balanced number for comprehensive analysis
Citations: &Citations{Enabled: true}, // Enable citations for fetched content
MaxContentTokens: 100000, // Reasonable limit for large documents
}
request := ClaudeRequest{
Model: "claude-opus-4-5-20251101", //claude-sonnet-4-5-20250929
MaxTokens: 4000,
Messages: []Message{
{
Role: "user",
Content: researchPrompt,
},
},
Tools: []Tool{webSearchTool, webFetchTool},
}
rm.logDebug("Starting research with combined web search and fetch tools")
result, err := rm.callClaudeAPI(request, debugMode)
if err != nil {
// Check for common API permission issues
if strings.Contains(err.Error(), "permission") || strings.Contains(err.Error(), "unauthorized") {
return "", fmt.Errorf("Claude API key may not have web search or web fetch permissions enabled. Please check your API key settings at console.anthropic.com. Original error: %w", err)
}
// Check for web fetch specific errors
if strings.Contains(err.Error(), "web_fetch") || strings.Contains(err.Error(), "web-fetch") {
return "", fmt.Errorf("web fetch tool may not be enabled for your API key or the model may not support it. Ensure you have web fetch permissions and are using a supported model (claude-sonnet-4). Original error: %w", err)
}
if strings.Contains(err.Error(), "beta") || strings.Contains(err.Error(), "web-fetch-2025-09-10") {
return "", fmt.Errorf("web fetch beta feature may not be available. Check that the beta header is correctly set and your API key has beta access. Original error: %w", err)
}
if strings.Contains(err.Error(), "tool_use") {
return "", fmt.Errorf("tool use may require a different conversation approach. This could indicate API limitations, configuration issues, or model compatibility problems. Original error: %w", err)
}
return "", fmt.Errorf("research failed: %w", err)
}
// Validate that we got substantial content
if len(strings.TrimSpace(result)) < 200 {
return "", fmt.Errorf("research returned insufficient content (%d characters). This may indicate web search is not functioning properly", len(result))
}
rm.logDebug("Research completed successfully, %d characters returned", len(result))
return result, nil
}
func (rm *ResearchManager) callClaudeAPI(request ClaudeRequest, debugMode bool) (string, error) {
// Generate unique request ID for correlation
requestID := fmt.Sprintf("research_%d", time.Now().UnixNano())
jsonData, err := json.Marshal(request)
if err != nil {
return "", fmt.Errorf("failed to marshal request: %w", err)
}
// Save request to debug file (only in debug mode)
if debugMode {
rm.saveDebugData(requestID+"_request.json", jsonData)
}
rm.logDebug("[%s] Making Claude API request with %d tools, %d tokens", requestID, len(request.Tools), request.MaxTokens)
req, err := http.NewRequest("POST", "https://api.anthropic.com/v1/messages", bytes.NewBuffer(jsonData))
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", rm.apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("anthropic-beta", "web-fetch-2025-09-10")
// Log request headers (without API key)
rm.logDebug("[%s] Request headers: Content-Type=%s, anthropic-version=%s",
requestID, req.Header.Get("Content-Type"), req.Header.Get("anthropic-version"))
resp, err := rm.client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to make request: %w", err)
}
defer resp.Body.Close()
// Read response body for logging
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
// Save raw response to debug file (only in debug mode)
if debugMode {
rm.saveDebugData(requestID+"_response_raw.json", body)
}
rm.logDebug("[%s] Received response: status=%d, content-length=%d", requestID, resp.StatusCode, len(body))
if resp.StatusCode != http.StatusOK {
rm.logDebug("[%s] API request failed with status %d: %s", requestID, resp.StatusCode, string(body))
return "", fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
var claudeResp ClaudeResponse
if err := json.Unmarshal(body, &claudeResp); err != nil {
rm.logDebug("[%s] Failed to decode JSON response: %s", requestID, string(body))
return "", fmt.Errorf("failed to decode response: %w", err)
}
// Save parsed response to debug file (only in debug mode)
if debugMode {
parsedData, _ := json.MarshalIndent(claudeResp, "", " ")
rm.saveDebugData(requestID+"_response_parsed.json", parsedData)
}
// Log detailed response information
rm.logDebug("[%s] Claude API Response - StopReason: '%s', ContentBlocks: %d, Usage: %d input/%d output tokens",
requestID, claudeResp.StopReason, len(claudeResp.Content), claudeResp.Usage.InputTokens, claudeResp.Usage.OutputTokens)
if len(claudeResp.Content) == 0 {
return "", fmt.Errorf("no content in response")
}
// Store usage information and search count
rm.lastUsage = claudeResp.Usage
// Detailed analysis of each content block
var hasToolUse bool
var searchCount int
var fetchCount int
var debugInfo []string
var builder strings.Builder
debugInfo = append(debugInfo, fmt.Sprintf("=== API RESPONSE ANALYSIS [%s] ===", requestID))
debugInfo = append(debugInfo, fmt.Sprintf("Stop Reason: %s", claudeResp.StopReason))
debugInfo = append(debugInfo, fmt.Sprintf("Content Blocks: %d", len(claudeResp.Content)))
debugInfo = append(debugInfo, fmt.Sprintf("Usage: %d input, %d output tokens", claudeResp.Usage.InputTokens, claudeResp.Usage.OutputTokens))
debugInfo = append(debugInfo, "")
for i, block := range claudeResp.Content {
blockInfo := fmt.Sprintf("Block %d: Type='%s'", i, block.Type)
switch block.Type {
case "text":
if block.Text != "" {
builder.WriteString(block.Text)
blockInfo += fmt.Sprintf(" - Text Length: %d chars", len(block.Text))
debugInfo = append(debugInfo, blockInfo)
debugInfo = append(debugInfo, fmt.Sprintf("Text Preview: %s...",
truncateString(block.Text, 100)))
rm.logDebug("[%s] Added text block %d (%d chars)", requestID, i, len(block.Text))
} else {
blockInfo += " - Empty text"
debugInfo = append(debugInfo, blockInfo)
}
case "tool_use", "server_tool_use":
hasToolUse = true
// Tool use blocks indicate tool execution but don't reliably contain tool names
// We count actual executions from the result blocks instead
if block.ToolUse != nil {
toolName := block.ToolUse.Name
blockInfo += fmt.Sprintf(" - %s tool use detected (id: %s)", toolName, block.ToolUse.ID)
rm.logDebug("[%s] Tool use block %d: %s (id: %s)", requestID, i, toolName, block.ToolUse.ID)
} else {
blockInfo += " - Tool use detected (awaiting results)"
rm.logDebug("[%s] Tool use block %d: tool data not available", requestID, i)
}
debugInfo = append(debugInfo, blockInfo)
case "web_search_tool_result":
searchCount++ // Count each web search result block
blockInfo += " - Web search results"
if block.ToolResult != nil {
blockInfo += fmt.Sprintf(" (tool_use_id: %s, content items: %d)",
block.ToolResult.ToolUseID, len(block.ToolResult.Content))
}
debugInfo = append(debugInfo, blockInfo)
rm.logDebug("[%s] Web search results block %d detected (search #%d)", requestID, i, searchCount)
case "web_fetch_tool_result":
fetchCount++ // Count each web fetch result block
blockInfo += " - Web fetch results"
if block.ToolResult != nil {
blockInfo += fmt.Sprintf(" (tool_use_id: %s, content items: %d)",
block.ToolResult.ToolUseID, len(block.ToolResult.Content))
}
debugInfo = append(debugInfo, blockInfo)
rm.logDebug("[%s] Web fetch results block %d detected (fetch #%d)", requestID, i, fetchCount)
default:
blockInfo += fmt.Sprintf(" - Unknown type")
debugInfo = append(debugInfo, blockInfo)
rm.logDebug("[%s] Unknown content block type: %s", requestID, block.Type)
}
}
// Store search and fetch counts and debug info for research note
rm.lastSearchCount = searchCount
rm.lastFetchCount = fetchCount
debugInfo = append(debugInfo, "")
debugInfo = append(debugInfo, "=== TOOL USAGE SUMMARY ===")
debugInfo = append(debugInfo, fmt.Sprintf("Web Searches Performed: %d (counted from result blocks)", searchCount))
debugInfo = append(debugInfo, fmt.Sprintf("Web Fetches Performed: %d (counted from result blocks)", fetchCount))
debugInfo = append(debugInfo, fmt.Sprintf("Total Research Actions: %d", searchCount+fetchCount))
rm.lastDebugInfo = strings.Join(debugInfo, "\n")
if builder.Len() == 0 {
errorMsg := fmt.Sprintf("No text content found in %d content blocks", len(claudeResp.Content))
if hasToolUse && claudeResp.StopReason == "tool_use" {
errorMsg += " - Response contains only tool use blocks, may require conversation continuation"
}
rm.logDebug("[%s] %s", requestID, errorMsg)
return "", fmt.Errorf("%s", errorMsg)
}
finalResponse := builder.String()
rm.logDebug("[%s] Successfully created a report with total length: %d characters",
requestID, builder.Len())
return finalResponse, nil
}
func (rm *ResearchManager) saveDebugData(filename string, data []byte) {
// Add panic recovery for file operations
defer func() {
if r := recover(); r != nil {
rm.logDebug("PANIC in saveDebugData: %v", r)
}
}()
// Validate inputs
if filename == "" {
rm.logDebug("Warning: Empty filename provided to saveDebugData")
return
}
if len(data) == 0 {
rm.logDebug("Warning: Empty data provided for file %s", filename)
return
}
// Create debug directory if it doesn't exist with error handling
debugDir := "vimango_research_debug"
err := os.MkdirAll(debugDir, 0755)
if err != nil {
rm.logDebug("Failed to create debug directory %s: %v", debugDir, err)
return
}
// Validate and create safe file path
fullPath := filepath.Join(debugDir, filepath.Base(filename)) // Use Base to prevent path traversal
// Attempt to write file with proper error handling
err = os.WriteFile(fullPath, data, 0644)
if err != nil {
rm.logDebug("Failed to save debug data to %s: %v", fullPath, err)
// Don't fail the entire operation if debug file saving fails
} else {
rm.logDebug("Saved debug data to %s (%d bytes)", fullPath, len(data))
}
}
func truncateString(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen] + "..."
}
func getResearchQualityDescription(inputTokens, searchCount, fetchCount int) string {
// Consider both search and fetch activity for quality assessment
totalResearchActions := searchCount + fetchCount
if inputTokens > 40000 && fetchCount >= 3 && searchCount >= 3 {
return "Premium Deep Research (extensive search + full document analysis)"
} else if inputTokens > 30000 && (fetchCount >= 2 || (searchCount >= 3 && fetchCount >= 1)) {
return "Comprehensive (thorough web research with document analysis)"
} else if inputTokens > 20000 && (fetchCount >= 1 || searchCount >= 2) {
return "Detailed (moderate web research)"
} else if inputTokens > 10000 && totalResearchActions >= 1 {
return "Standard (basic research)"
} else {
return "Limited (minimal research)"
}
}
// logDebug writes debug information for research operations to a persistent log
func (rm *ResearchManager) logDebug(format string, args ...interface{}) {
message := fmt.Sprintf("[Research Debug] "+format, args...)
if rm == nil {
return
}
logPath := rm.debugLogPath
if logPath == "" {
logPath = filepath.Join("vimango_research_debug", "research.log")
}
timestamp := time.Now().Format(time.RFC3339Nano)
entry := fmt.Sprintf("%s %s\n", timestamp, message)
rm.debugLogMux.Lock()
defer rm.debugLogMux.Unlock()
if err := os.MkdirAll(filepath.Dir(logPath), 0o755); err != nil {
fmt.Fprintf(os.Stderr, "research debug log mkdir failed: %v\n", err)
return
}
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
fmt.Fprintf(os.Stderr, "research debug log open failed: %v\n", err)
return
}
defer f.Close()
if _, err := f.WriteString(entry); err != nil {
fmt.Fprintf(os.Stderr, "research debug log write failed: %v\n", err)
}
}
func (rm *ResearchManager) createResearchNote(task *ResearchTask) {
// Add panic recovery to prevent crashes
defer func() {
if r := recover(); r != nil {
rm.logDebug("PANIC in createResearchNote: %v", r)
task.addNotification(fmt.Sprintf("⚠️ Research note creation failed: %v", r))
}
}()
rm.logDebug("Starting research note creation for task %s", task.ID)
// Safely create note title with validation
if task.Title == "" {
task.Title = "Untitled Research"
}
title := fmt.Sprintf("Research: %s (%s)", task.Title, time.Now().Format("2006-01-02 15:04"))
rm.logDebug("Created research note title: %s", title)
// Build sections based on debug mode
usageSection := ""
debugSection := ""
// Always include basic usage statistics
if rm.lastUsage.InputTokens > 0 || rm.lastUsage.OutputTokens > 0 {
usageSection = fmt.Sprintf(`
## Research Statistics
- **Duration:** %v
- **Web Searches:** %d
- **Web Fetches:** %d
- **Claude Usage:** %d input tokens, %d output tokens
- **Research Quality:** %s
`,
task.EndTime.Sub(task.StartTime).Round(time.Second),
rm.lastSearchCount,
rm.lastFetchCount,
rm.lastUsage.InputTokens,
rm.lastUsage.OutputTokens,
getResearchQualityDescription(rm.lastUsage.InputTokens, rm.lastSearchCount, rm.lastFetchCount))
}
// Only include full debug info if in debug mode
if task.DebugMode && rm.lastDebugInfo != "" {
rm.logDebug("Building debug section, debug info length: %d", len(rm.lastDebugInfo))
// Safe string formatting with validation
taskID := "unknown"
if task.ID != "" {
taskID = task.ID
}
duration := time.Duration(0)
if !task.EndTime.IsZero() && !task.StartTime.IsZero() {
duration = task.EndTime.Sub(task.StartTime).Round(time.Second)
}
debugSection = fmt.Sprintf(`
## DEBUG INFORMATION
%s
### API Request Details
- Task ID: %s
- Research Duration: %v
- Source Entry ID: %d
- Start Time: %s
- End Time: %s
### Tool Configuration
- Web Search Tool: web_search_20250305 (Max Uses: 15)
- Web Fetch Tool: web_fetch_20250910 (Max Uses: 8)
- User Location: US, America/New_York
- API Version: anthropic-version 2023-06-01
- Beta Headers: web-fetch-2025-09-10
### Files Generated
Debug files saved in vimango_research_debug/ directory:
- [requestID]_request.json - Full API request JSON
- [requestID]_response_raw.json - Raw API response
- [requestID]_response_parsed.json - Parsed response structure
`, rm.lastDebugInfo, taskID, duration, task.SourceEntry,
task.StartTime.Format("2006-01-02 15:04:05"),
task.EndTime.Format("2006-01-02 15:04:05"))
} else {
rm.logDebug("No debug info available for research note")
}
// Safely validate task result
result := task.Result
if result == "" {
result = "No research results were generated."
rm.logDebug("Warning: Empty research result for task %s", task.ID)
}
// Create research note with appropriate level of information
var troubleshootingNote string
if task.DebugMode {
troubleshootingNote = "\n\n**Debug Mode:** Full debugging information included above."
} else {
troubleshootingNote = "\n\n**Note:** For detailed debugging information, use `:researchdebug` instead of `:research`."
}
markdown := fmt.Sprintf(`# %s
%s
%s
---
*This research was generated automatically by vimango's deep research feature using Claude AI with web search capabilities.*%s
`, title, result, usageSection, troubleshootingNote)
// Add debug section if in debug mode
if task.DebugMode && debugSection != "" {
markdown += fmt.Sprintf("\n\n---\n%s\n---", debugSection)
}
rm.logDebug("Created markdown content, length: %d characters", len(markdown))
// Create a new row for the research result with validation
row := &Row{
id: -1,
title: title,
star: false,
dirty: true,
}
rm.logDebug("Created row for research note")
// Safely insert the new entry with error checking
rm.logDebug("Attempting to insert research note title...")
err := rm.app.Database.insertTitle(row, DefaultContextUUID, DefaultFolderUUID)
if err != nil {
rm.logDebug("ERROR inserting research note title: %v", err)
task.addNotification(fmt.Sprintf("⚠️ Failed to create research note: %v", err))
return
}
rm.logDebug("Successfully inserted research note with ID: %d", row.id)
// Safely update the note content with validation
rm.logDebug("Attempting to update note content for ID %d...", row.id)
err = rm.app.Database.updateNote(row.id, markdown)
if err != nil {
rm.logDebug("ERROR updating research note content: %v", err)
task.addNotification(fmt.Sprintf("⚠️ Failed to update research note content: %v", err))
return
}
rm.logDebug("Research note created successfully with ID %d, includes debug information", row.id)
task.addNotification(fmt.Sprintf("✅ Research note created successfully (ID: %d)", row.id))
}
// displayAggregatedNotification creates and displays a single comprehensive notification
// with all information collected during the research process
func (rm *ResearchManager) displayAggregatedNotification(task *ResearchTask) {
var notificationBuilder strings.Builder
// Header with status
if task.Status == "completed" {
notificationBuilder.WriteString("✅ RESEARCH COMPLETED\n")
} else {
notificationBuilder.WriteString("❌ RESEARCH FAILED\n")
}
notificationBuilder.WriteString(strings.Repeat("─", 50) + "\n")
// Task information
notificationBuilder.WriteString(fmt.Sprintf("Title: %s\n", task.Title))
// Duration
duration := task.EndTime.Sub(task.StartTime).Round(time.Second)
notificationBuilder.WriteString(fmt.Sprintf("Duration: %v\n", duration))
// Research metrics (if available)
if rm.lastSearchCount > 0 || rm.lastFetchCount > 0 {
notificationBuilder.WriteString(fmt.Sprintf("Web Searches: %d\n", rm.lastSearchCount))
notificationBuilder.WriteString(fmt.Sprintf("Web Fetches: %d\n", rm.lastFetchCount))
// Token usage
if rm.lastUsage.InputTokens > 0 || rm.lastUsage.OutputTokens > 0 {
notificationBuilder.WriteString(fmt.Sprintf("Tokens: %d input, %d output\n",
rm.lastUsage.InputTokens, rm.lastUsage.OutputTokens))
// Research quality
quality := getResearchQualityDescription(rm.lastUsage.InputTokens, rm.lastSearchCount, rm.lastFetchCount)
notificationBuilder.WriteString(fmt.Sprintf("Quality: %s\n", quality))
}
}
// Add error information if failed
if task.Status == "failed" && task.Error != "" {
notificationBuilder.WriteString(fmt.Sprintf("\nError: %s\n", task.Error))
}
// Add all collected notifications in chronological order
task.notifyMux.Lock()
if len(task.notifications) > 0 {
notificationBuilder.WriteString("\n" + strings.Repeat("─", 50) + "\n")
notificationBuilder.WriteString("Process Log:\n")
for i, notification := range task.notifications {
notificationBuilder.WriteString(fmt.Sprintf("%d. %s\n", i+1, notification))
}
}
task.notifyMux.Unlock()
// Send the aggregated notification
rm.app.addNotification(notificationBuilder.String())
}
func (rm *ResearchManager) StartResearch(title, prompt string, sourceEntryID int, debugMode bool) (string, error) {
taskID := fmt.Sprintf("research_%d_%d", time.Now().Unix(), sourceEntryID)
task := &ResearchTask{
ID: taskID,
Title: title,
Prompt: prompt,
Status: "pending",
SourceEntry: sourceEntryID,
DebugMode: debugMode,
}
// Add to queue
select {
case rm.queue <- task:
return taskID, nil
default:
return "", fmt.Errorf("research queue is full, please try again later")
}
}
func (rm *ResearchManager) GetRunningTasks() []*ResearchTask {
rm.mutex.RLock()
defer rm.mutex.RUnlock()
tasks := make([]*ResearchTask, 0, len(rm.running))
for _, task := range rm.running {
tasks = append(tasks, task)
}
return tasks
}
func (rm *ResearchManager) Stop() {
close(rm.done)
}
// testAPIConnection performs a simple API test to validate key and permissions
func (rm *ResearchManager) testAPIConnection() {
rm.logDebug("Testing Claude API connection and web search permissions...")
// Simple test request with web search tool
testRequest := ClaudeRequest{
Model: "claude-3-5-sonnet-20241022",
MaxTokens: 100,
Messages: []Message{
{
Role: "user",
Content: "Hello, can you perform a web search? Just respond with a brief acknowledgment.",
},
},
Tools: []Tool{
{
Type: "web_search_20250305",
Name: "web_search",
MaxUses: 1,
},
},
}
result, err := rm.callClaudeAPI(testRequest, true) // Enable debug mode for connection test
if err != nil {
rm.logDebug("API connection test FAILED: %v", err)
if strings.Contains(err.Error(), "permission") || strings.Contains(err.Error(), "unauthorized") {
rm.app.addNotification("⚠️ Research: API key may not have web search permissions")
} else if strings.Contains(err.Error(), "invalid") {
rm.app.addNotification("⚠️ Research: Invalid API key configuration")
} else {
rm.app.addNotification("⚠️ Research: API connection test failed - check network/config")
}
} else {
rm.logDebug("API connection test PASSED: %d characters returned", len(result))
rm.app.addNotification("✅ Research: API connection and web search permissions verified")
}
}