-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
1353 lines (1197 loc) · 43 KB
/
main.go
File metadata and controls
1353 lines (1197 loc) · 43 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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"mu/admin"
"mu/agent"
"mu/apps"
"mu/internal/api"
"mu/internal/app"
"mu/internal/auth"
"mu/blog"
"mu/chat"
"mu/cli"
"mu/internal/data"
"mu/docs"
"mu/home"
"mu/mail"
"mu/news"
"mu/news/digest"
"mu/markets"
"mu/reminder"
"mu/places"
"mu/search"
"mu/social"
"mu/stream"
"mu/user"
"mu/video"
"mu/wallet"
"mu/weather"
"mu/work"
)
var EnvFlag = flag.String("env", "dev", "Set the environment")
var ServeFlag = flag.Bool("serve", false, "Run the server")
var AddressFlag = flag.String("address", ":8080", "Address for server")
func main() {
// Server vs CLI dispatch — any invocation that includes `--serve`
// (or `-serve`) runs the full server exactly as before. Anything
// else is treated as a CLI command and handed to the cli package,
// which talks to /mcp over HTTP and never touches server state.
// This keeps the existing `mu --serve` deployment completely
// unaffected while adding `mu news`, `mu chat "hi"`, etc.
if !isServerMode(os.Args[1:]) {
os.Exit(cli.Run(os.Args[1:]))
}
flag.Parse()
if !*ServeFlag {
fmt.Println("--serve not set")
return
}
// api page is now dynamic (rendered in api.APIPageHandler)
// load the data index
data.Load()
// load admin/flags
admin.Load()
// load the chat
chat.Load()
// load the news
news.Load()
// load the videos
video.Load()
// load the blog
blog.Load()
// load the mail (also configures SMTP and DKIM)
mail.Load()
// load places
places.Load()
// load weather
weather.Load()
// load markets, reminder, wallet
markets.Load()
reminder.Load()
wallet.Load()
// load apps
apps.Load()
// load work (task bounties)
work.Load()
// Wire work credit spending
work.SpendCredits = func(userID string, amount int, operation string) error {
return wallet.DeductCredits(userID, amount, operation, nil)
}
// Wire work notifications
work.Notify = func(toUserID, subject, body, threadID string) {
acc, err := auth.GetAccount(toUserID)
if err != nil {
return
}
mail.SendMessage("Mu", "micro", acc.Name, toUserID, subject, body, threadID, "")
}
// load social
social.Load()
// Wire social context into news article views
news.FetchSocialContext = func(articleURL, articleContent string) string {
ctx := social.FetchContext(articleURL, articleContent)
return social.RenderContextHTML(ctx)
}
// load the home cards
home.Load()
// load agent
agent.Load()
// Wire digest → blog callbacks (digest publishes as blog post)
digest.PublishBlogPost = func(title, content, author, authorID, tags string) (string, error) {
err := blog.CreatePost(title, content, author, authorID, tags, false)
if err != nil {
return "", err
}
// Return the ID of the just-created post
post := blog.FindTodayDigest()
if post != nil {
return post.ID, nil
}
return "", nil
}
digest.UpdateBlogPost = func(id, title, content, tags string) error {
return blog.UpdatePost(id, title, content, tags, false)
}
digest.FindTodayBlogDigest = func() *digest.DigestPost {
post := blog.FindTodayDigest()
if post == nil {
return nil
}
return &digest.DigestPost{
ID: post.ID,
Title: post.Title,
Content: post.Content,
}
}
// load daily digest scheduler
digest.Load()
// load search
search.Load()
// load docs
docs.Load()
// load user presence tracking
user.Load()
// Load the stream (platform event timeline).
stream.Load()
// Wire user → blog callback (avoids direct import between building blocks)
user.GetUserPosts = func(authorName string) []user.UserPost {
posts := blog.GetPostsByAuthor(authorName)
result := make([]user.UserPost, len(posts))
for i, p := range posts {
result[i] = user.UserPost{
ID: p.ID,
Title: p.Title,
Content: p.Content,
CreatedAt: p.CreatedAt,
Private: p.Private,
}
}
return result
}
user.LinkifyContent = blog.Linkify
// Wire @micro mention handling in the status stream. When a user
// posts a status containing "@micro ...", run the agent against
// the sender's wallet and post the reply as a status from the
// system user. Runs async so the POST /user/status handler returns
// immediately. We never fire this for the system user itself.
user.AIReplyHook = func(askerID, prompt string) {
if askerID == app.SystemUserID {
return
}
// If the asker is already banned, don't spend AI credits.
if auth.IsBanned(askerID) {
return
}
answer, err := agent.Query(askerID, prompt)
if err != nil {
app.Log("status", "@micro agent error for %s: %v", askerID, err)
_ = user.PostSystemStatus("I couldn't answer that one — try again in a moment.")
return
}
answer = strings.TrimSpace(answer)
if answer == "" {
return
}
// Moderate the AI response before posting — if the question
// tricked the AI into producing harmful content, the asker
// is banned and the response is silently dropped.
if !user.ModerateAIResponse(askerID, answer) {
app.Log("status", "AI response for %s blocked by moderation", askerID)
return
}
if err := user.PostSystemStatus(answer); err != nil {
app.Log("status", "failed to post @micro reply: %v", err)
}
}
// Wire stream @micro replies — same agent, posts into the stream
// instead of the status profile.
stream.AIReplyHook = func(askerID, prompt string) {
if auth.IsBanned(askerID) {
return
}
answer, err := agent.Query(askerID, prompt)
if err != nil {
app.Log("stream", "@micro agent error for %s: %v", askerID, err)
stream.PostAgent("I couldn't answer that one — try again in a moment.")
return
}
answer = strings.TrimSpace(answer)
if answer == "" {
return
}
if !user.ModerateAIResponse(askerID, answer) {
app.Log("stream", "AI response for %s blocked by moderation", askerID)
return
}
stream.PostAgent(answer)
}
user.GetUserApps = func(authorID string) []user.UserApp {
appList := apps.GetAppsByAuthor(authorID)
result := make([]user.UserApp, len(appList))
for i, a := range appList {
result[i] = user.UserApp{
Slug: a.Slug,
Name: a.Name,
Description: a.Description,
Icon: a.Icon,
}
}
return result
}
// Wire admin → blog callbacks (avoids blog importing admin)
admin.GetNewAccountBlog = blog.GetNewAccountBlogPosts
admin.RefreshBlogCache = blog.RefreshCache
// Enable indexing after all content is loaded
// This allows the priority queue to process new items first
data.StartIndexing()
// Start web search topics (loads cache from disk, generates in background)
search.StartTopics()
// Start daily opinion generation (publishes as blog post)
blog.StartOpinion()
// Wire MCP quota checking using wallet credit system
api.QuotaCheck = func(r *http.Request, op string) (bool, int, error) {
// Check for x402 payment (bypasses auth + credits)
if r.Context().Value(wallet.X402ContextKey) != nil {
_, err := wallet.VerifyAndSettle(r, op, r.URL.Path)
if err != nil {
return false, 0, fmt.Errorf("x402 payment failed: %w", err)
}
return true, 0, nil
}
sess, err := auth.GetSession(r)
if err != nil {
return false, 0, fmt.Errorf("authentication required")
}
canProceed, _, cost, err := wallet.CheckQuota(sess.Account, op)
return canProceed, cost, err
}
// Wire agent quota checking (same wallet credit system)
agent.QuotaCheck = func(r *http.Request, op string) (bool, int, error) {
// Check for x402 payment (bypasses auth + credits)
if r.Context().Value(wallet.X402ContextKey) != nil {
_, err := wallet.VerifyAndSettle(r, op, r.URL.Path)
if err != nil {
return false, 0, fmt.Errorf("x402 payment failed: %w", err)
}
return true, 0, nil
}
sess, err := auth.GetSession(r)
if err != nil {
return false, 0, fmt.Errorf("authentication required")
}
canProceed, _, cost, err := wallet.CheckQuota(sess.Account, op)
return canProceed, cost, err
}
// Wire x402 payment required response for MCP
if wallet.X402Enabled() {
api.PaymentRequiredResponse = wallet.WritePaymentRequired
}
// Wire tool-specific guards. Currently rate-limits the signup tool by IP
// to defend against bulk account creation via MCP.
api.ToolGuard = func(r *http.Request, toolName string) error {
if toolName == "signup" {
ip := app.ClientIP(r)
if !app.SignupRateLimit(ip) {
app.Log("auth", "MCP signup rate limit hit for IP: %s", ip)
return fmt.Errorf("too many sign-ups from your network. Please try again later")
}
}
return nil
}
// Wire email sending for verification mails. Uses the platform's own
// SMTP relay so verification mails come from no-reply@<MAIL_DOMAIN>.
// Only enabled when MAIL_DOMAIN is configured to a real domain —
// instances without mail configured skip the verification gate
// entirely (see auth.VerificationRequired below).
if domain := mail.GetConfiguredDomain(); domain != "" && domain != "localhost" {
app.EmailSender = func(to, subject, plain, html string) error {
from := "no-reply@" + domain
_, err := mail.SendExternalEmail("Mu", from, to, subject, plain, html, "")
return err
}
}
// Verification is only required when we can actually send verification
// emails. Self-hosted instances without mail configured fall back to
// the legacy "any account can post" rule.
auth.VerificationRequired = func() bool {
return app.EmailSender != nil
}
// Register MCP auth tools
api.RegisterTool(api.Tool{
Name: "signup",
Description: "Create a new account and return a session token. When invite-only mode is enabled, a valid invite code is required.",
Params: []api.ToolParam{
{Name: "id", Type: "string", Description: "Username (4-24 chars, lowercase, starts with letter)", Required: true},
{Name: "secret", Type: "string", Description: "Password (minimum 6 characters)", Required: true},
{Name: "name", Type: "string", Description: "Display name (optional, defaults to username)", Required: false},
{Name: "invite", Type: "string", Description: "Invite code (required when instance is invite-only)", Required: false},
},
Handle: func(args map[string]any) (string, error) {
id, _ := args["id"].(string)
secret, _ := args["secret"].(string)
name, _ := args["name"].(string)
invite, _ := args["invite"].(string)
if id == "" || secret == "" {
return "username and password are required", fmt.Errorf("missing fields")
}
if len(secret) < 6 {
return "password must be at least 6 characters", fmt.Errorf("short password")
}
if reason := auth.ValidateUsername(id); reason != "" {
return reason, fmt.Errorf("banned username")
}
if auth.InviteOnly() {
if err := auth.ValidateInvite(invite); err != nil {
return err.Error(), err
}
}
if name == "" {
name = id
}
if err := auth.Create(&auth.Account{
ID: id, Secret: secret, Name: name, Created: time.Now(),
}); err != nil {
return err.Error(), err
}
if invite != "" {
auth.ConsumeInvite(invite, id)
}
sess, err := auth.Login(id, secret)
if err != nil {
return "account created but login failed", err
}
return fmt.Sprintf(`{"token":"%s"}`, sess.Token), nil
},
})
api.RegisterTool(api.Tool{
Name: "login",
Description: "Log in and return a session token for use in Authorization header",
Params: []api.ToolParam{
{Name: "id", Type: "string", Description: "Username", Required: true},
{Name: "secret", Type: "string", Description: "Password", Required: true},
},
Handle: func(args map[string]any) (string, error) {
id, _ := args["id"].(string)
secret, _ := args["secret"].(string)
if id == "" || secret == "" {
return "username and password are required", fmt.Errorf("missing fields")
}
sess, err := auth.Login(id, secret)
if err != nil {
return "invalid username or password", err
}
return fmt.Sprintf(`{"token":"%s"}`, sess.Token), nil
},
})
// web_search tool registered via MCP
api.RegisterTool(api.Tool{
Name: "web_search",
Description: "Search the web for current information and news",
Method: "GET",
Path: "/web",
WalletOp: "web_search",
Params: []api.ToolParam{
{Name: "q", Type: "string", Description: "Search query", Required: true},
},
})
// web_fetch tool — fetch a URL and return cleaned readable content
api.RegisterTool(api.Tool{
Name: "web_fetch",
Description: "Fetch a web page and return its cleaned readable content (strips ads, popups, navigation)",
Method: "GET",
Path: "/web/fetch",
WalletOp: "web_fetch",
Params: []api.ToolParam{
{Name: "url", Type: "string", Description: "The URL to fetch", Required: true},
},
})
// Register apps MCP tools
api.RegisterTool(api.Tool{
Name: "apps_search",
Description: "Search the apps directory for small, useful tools",
Method: "GET",
Path: "/apps",
Params: []api.ToolParam{
{Name: "q", Type: "string", Description: "Search query (name, description, or tag)", Required: false},
{Name: "tag", Type: "string", Description: "Filter by tag", Required: false},
},
})
api.RegisterTool(api.Tool{
Name: "apps_read",
Description: "Read details of a specific app by its slug",
Method: "GET",
Path: "/apps",
Params: []api.ToolParam{
{Name: "slug", Type: "string", Description: "The app's URL slug (e.g. pomodoro-timer)", Required: true},
},
Handle: func(args map[string]any) (string, error) {
slug, _ := args["slug"].(string)
if slug == "" {
return `{"error":"slug is required"}`, fmt.Errorf("missing slug")
}
a := apps.GetApp(slug)
if a == nil {
return `{"error":"app not found"}`, fmt.Errorf("not found")
}
b, _ := json.Marshal(a)
return string(b), nil
},
})
api.RegisterTool(api.Tool{
Name: "apps_create",
Description: "Create a new app — a small, self-contained HTML tool hosted on Mu",
Method: "POST",
Path: "/apps/new",
Params: []api.ToolParam{
{Name: "name", Type: "string", Description: "App name (e.g. Pomodoro Timer)", Required: true},
{Name: "slug", Type: "string", Description: "URL-friendly ID (e.g. pomodoro-timer)", Required: true},
{Name: "description", Type: "string", Description: "Short description of what the app does", Required: true},
{Name: "tags", Type: "string", Description: "Comma-separated tags (optional)", Required: false},
{Name: "html", Type: "string", Description: "The app's HTML content (can include inline CSS and JavaScript, max 256KB)", Required: true},
{Name: "price", Type: "number", Description: "Credits charged per use (0 = free, max 1000)", Required: false},
},
})
api.RegisterTool(api.Tool{
Name: "apps_edit",
Description: "Edit an existing app — update its name, description, tags, icon, HTML code, or price",
Params: []api.ToolParam{
{Name: "slug", Type: "string", Description: "The app's URL slug (e.g. pomodoro-timer)", Required: true},
{Name: "name", Type: "string", Description: "New app name", Required: false},
{Name: "description", Type: "string", Description: "New description", Required: false},
{Name: "tags", Type: "string", Description: "New comma-separated tags", Required: false},
{Name: "html", Type: "string", Description: "New HTML content (max 256KB)", Required: false},
{Name: "icon", Type: "string", Description: "New SVG icon", Required: false},
{Name: "price", Type: "number", Description: "Credits charged per use (0 = free, max 1000)", Required: false},
},
Handle: func(args map[string]any) (string, error) {
slug, _ := args["slug"].(string)
if slug == "" {
return `{"error":"slug is required"}`, fmt.Errorf("missing slug")
}
name, _ := args["name"].(string)
description, _ := args["description"].(string)
tags, _ := args["tags"].(string)
html, _ := args["html"].(string)
icon, _ := args["icon"].(string)
price := -1 // -1 means "not set"
if p, ok := args["price"].(float64); ok {
price = int(p)
}
a, err := apps.UpdateApp(slug, name, description, tags, html, icon, price)
if err != nil {
return fmt.Sprintf(`{"error":"%s"}`, err.Error()), err
}
b, _ := json.Marshal(a)
return string(b), nil
},
})
api.RegisterToolWithAuth(api.Tool{
Name: "apps_build",
Description: "AI-generate an app from a natural language description, save it, and return the app details with URL",
WalletOp: "app_build",
Params: []api.ToolParam{
{Name: "prompt", Type: "string", Description: "Description of the app to build (e.g. 'a pomodoro timer with lap counter')", Required: true},
},
}, func(args map[string]any, accountID string) (string, error) {
prompt, _ := args["prompt"].(string)
if prompt == "" {
return `{"error":"prompt is required"}`, fmt.Errorf("missing prompt")
}
// Use the authenticated user as the app author
authorName := accountID
if acc, err := auth.GetAccount(accountID); err == nil {
authorName = acc.Name
}
a, err := apps.BuildAndSave(prompt, accountID, authorName)
if err != nil {
return fmt.Sprintf(`{"error":"%s"}`, err.Error()), err
}
b, _ := json.Marshal(map[string]string{
"name": a.Name,
"slug": a.Slug,
"url": "/apps/" + a.Slug,
"run": "/apps/" + a.Slug + "/run",
})
return string(b), nil
})
api.RegisterToolWithAuth(api.Tool{
Name: "apps_fork",
Description: "Fork an existing app — creates a copy under your account that you can modify independently",
Params: []api.ToolParam{
{Name: "slug", Type: "string", Description: "Slug of the app to fork", Required: true},
{Name: "new_slug", Type: "string", Description: "Slug for the forked copy (optional, auto-generated if empty)", Required: false},
},
}, func(args map[string]any, accountID string) (string, error) {
slug, _ := args["slug"].(string)
newSlug, _ := args["new_slug"].(string)
if slug == "" {
return `{"error":"slug is required"}`, fmt.Errorf("missing slug")
}
authorName := "Agent"
if acc, err := auth.GetAccount(accountID); err == nil {
authorName = acc.Name
}
a, err := apps.ForkApp(slug, newSlug, accountID, authorName)
if err != nil {
return fmt.Sprintf(`{"error":"%s"}`, err.Error()), err
}
b, _ := json.Marshal(map[string]string{
"name": a.Name,
"slug": a.Slug,
"url": "/apps/" + a.Slug,
})
return string(b), nil
})
api.RegisterTool(api.Tool{
Name: "apps_run",
Description: "Run JavaScript code in a sandboxed environment and return the result. Use for calculations, data processing, or any computation the user needs.",
WalletOp: "agent_query",
Params: []api.ToolParam{
{Name: "code", Type: "string", Description: "JavaScript code to execute. The code runs as a function body — use 'return' to output a value. Has access to mu.ai(), mu.fetch(), mu.store for platform features.", Required: true},
},
Handle: func(args map[string]any) (string, error) {
code, _ := args["code"].(string)
if code == "" {
return `{"error":"code is required"}`, fmt.Errorf("missing code")
}
id := apps.CreateRun(code, "agent")
b, _ := json.Marshal(map[string]string{
"id": id,
"url": "/apps/run?id=" + id,
"run": "/apps/run?id=" + id + "&raw=1",
})
return string(b), nil
},
})
api.RegisterToolWithAuth(api.Tool{
Name: "apps_test",
Description: "Test an app by checking its HTML structure and executing its mu.api calls server-side. Returns which API calls work and which fail.",
Params: []api.ToolParam{
{Name: "slug", Type: "string", Description: "The app's URL slug", Required: true},
},
}, func(args map[string]any, accountID string) (string, error) {
slug, _ := args["slug"].(string)
if slug == "" {
return `{"error":"slug required"}`, fmt.Errorf("missing slug")
}
result := apps.TestApp(slug, accountID)
b, _ := json.Marshal(result)
return string(b), nil
})
// Register agent MCP tool
api.RegisterToolWithAuth(api.Tool{
Name: "agent",
Description: "Ask the AI agent a question. The agent can search news, markets, web, video, weather, places, and more to answer your question.",
WalletOp: "agent_query",
Params: []api.ToolParam{
{Name: "prompt", Type: "string", Description: "Your question or request", Required: true},
},
}, func(args map[string]any, accountID string) (string, error) {
prompt, _ := args["prompt"].(string)
if prompt == "" {
return `{"error":"prompt is required"}`, fmt.Errorf("missing prompt")
}
answer, err := agent.Query(accountID, prompt)
if err != nil {
return fmt.Sprintf(`{"error":"%s"}`, err.Error()), err
}
return answer, nil
})
// Start the agent worker after all tools are registered
agent.StartWorker()
authenticated := map[string]bool{
"/video": false, // Public viewing, auth for interactive features
"/news": false, // Public viewing, auth for search
"/chat": false, // Public viewing, auth for chatting
"/home": false, // Public viewing
"/blog": false, // Public viewing, auth for posting
"/markets": false, // Public viewing
"/social": false, // Public viewing, auth for search
"/social/thread": false, // Public thread view, auth for messaging
"/places": false, // Public map, auth for search
"/weather": false, // Public page, auth for forecast lookup
"/mail": true, // Require auth for inbox
"/logout": true,
"/account": true,
"/verify": false, // Public — token in URL is the credential
"/token": true, // PAT token management
"/passkey": false, // Passkey login/register (auth checked in handler)
"/session": false, // Public - used to check auth status
"/api": false, // Public - API documentation
"/admin/flag": true,
"/admin": true,
"/admin/users": true,
"/admin/moderate": true,
"/admin/blocklist": true,
"/admin/spam": true,
"/admin/email": true,
"/admin/api": true,
"/admin/log": true,
"/admin/env": true,
"/admin/server": true,
"/admin/usage": true,
"/admin/delete": true,
"/admin/console": true,
"/admin/invite": true,
"/wallet": false, // Public - shows wallet info; auth checked in handler
"/apps": false, // Public - apps directory; auth checked in handler for create/edit
"/work": false, // Public - task bounties; auth checked in handler for post/claim
"/search": false, // Public - local data index search
"/web": false, // Public page, auth checked in handler (paid Brave web search)
"/web/fetch": false, // Public page, auth checked in handler (paid web fetch)
"/web/read": false, // Public page, auth checked in handler (proxied reader)
"/status": false, // Public - server health status
"/docs": false, // Public - documentation
"/whitepaper": false, // Public - whitepaper
"/mcp": false, // Public - MCP tools page
"/agent": false, // Public page, auth checked in handler
}
// Static assets should not require authentication
staticPaths := []string{
".css", ".js", ".png", ".jpg", ".jpeg", ".gif", ".svg",
".ico", ".webmanifest", ".json",
}
// serve video
http.HandleFunc("/video", video.Handler)
// serve news
http.HandleFunc("/news", news.Handler)
// serve chat
http.HandleFunc("/chat", chat.Handler)
// serve blog (full list)
http.HandleFunc("/blog", blog.Handler)
// serve individual blog post (public, no auth)
// Serves ActivityPub JSON-LD when requested via Accept header
http.HandleFunc("/blog/post", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && blog.WantsActivityPub(r) {
blog.PostObjectHandler(w, r)
return
}
blog.PostHandler(w, r)
})
// handle comments on posts /blog/post/{id}/comment
http.HandleFunc("/blog/post/", blog.CommentHandler)
// Legacy redirects for old URL structure (301 so browsers/crawlers update)
legacyRedirect := func(oldPrefix, newPrefix string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
target := newPrefix + r.URL.Path[len(oldPrefix):]
if r.URL.RawQuery != "" {
target += "?" + r.URL.RawQuery
}
http.Redirect(w, r, target, http.StatusMovedPermanently)
}
}
http.HandleFunc("/post/", legacyRedirect("/post/", "/blog/post/"))
http.HandleFunc("/post", legacyRedirect("/post", "/blog/post"))
http.HandleFunc("/fetch", legacyRedirect("/fetch", "/web/fetch"))
http.HandleFunc("/read", legacyRedirect("/read", "/web/read"))
// flag content
http.HandleFunc("/admin/flag", admin.FlagHandler)
// admin dashboard
http.HandleFunc("/admin", admin.AdminHandler)
// admin user management
http.HandleFunc("/admin/users", admin.UsersHandler)
// moderation queue
http.HandleFunc("/admin/moderate", admin.ModerateHandler)
// mail blocklist management
http.HandleFunc("/admin/blocklist", admin.BlocklistHandler)
// spam filter management
http.HandleFunc("/admin/spam", admin.SpamFilterHandler)
// email log
http.HandleFunc("/admin/email", admin.EmailLogHandler)
// external API call log
http.HandleFunc("/admin/api", admin.APILogHandler)
// system log
http.HandleFunc("/admin/log", admin.SysLogHandler)
// environment variables status
http.HandleFunc("/admin/env", admin.EnvHandler)
// server update and restart
http.HandleFunc("/admin/server", admin.UpdateHandler)
// AI usage tracking
http.HandleFunc("/admin/usage", admin.AIUsageHandler)
// admin delete (any content type)
http.HandleFunc("/admin/delete", admin.DeleteHandler)
// admin console
http.HandleFunc("/admin/console", admin.ConsoleHandler)
http.HandleFunc("/admin/invite", admin.InviteHandler)
// wallet - credits and payments
http.HandleFunc("/wallet", wallet.Handler)
http.HandleFunc("/wallet/", wallet.Handler) // Handle sub-routes like /wallet/topup
// serve search page (local + Brave web search)
http.HandleFunc("/search", search.Handler)
// serve web search page (Brave-powered, paid)
http.HandleFunc("/web", search.WebHandler)
http.HandleFunc("/web/preview", search.PreviewHandler)
// serve web fetch page (fetch and clean a URL)
http.HandleFunc("/web/fetch", search.FetchHandler)
// serve clean reader page for web results
http.HandleFunc("/web/read", search.ReadHandler)
// serve fact-check page and API
// serve the home screen
http.HandleFunc("/home", home.Handler)
// serve the agent
http.HandleFunc("/agent", agent.Handler)
http.HandleFunc("/agent/", agent.Handler)
http.HandleFunc("/agent/run", agent.RunHandler)
http.HandleFunc("/agent/exec", agent.ExecResultHandler)
// serve mail inbox
http.HandleFunc("/mail", mail.Handler)
// serve markets page
http.HandleFunc("/markets", markets.Handler)
// serve social page
http.HandleFunc("/social", social.Handler)
http.HandleFunc("/social/thread", social.ThreadHandler)
http.HandleFunc("/user/status", user.StatusHandler)
http.HandleFunc("/user/status/stream", user.StatusStreamHandler)
// Stream (console) routes
http.HandleFunc("/stream", stream.Handler)
http.HandleFunc("/stream/fragment", stream.FragmentHandler)
// redirect /reminder to reminder.dev
http.HandleFunc("/reminder", reminder.Handler)
// serve places page
http.HandleFunc("/places", places.Handler)
http.HandleFunc("/places/", places.Handler)
// serve weather page
http.HandleFunc("/weather", weather.Handler)
// serve apps
http.HandleFunc("/apps", apps.Handler)
http.HandleFunc("/apps/", apps.Handler)
// serve work (task bounties)
http.HandleFunc("/work", work.Handler)
http.HandleFunc("/work/", work.Handler)
// content controls (flag, save, dismiss, block, share)
http.HandleFunc("/app/", app.ControlsHandler)
// auth
http.HandleFunc("/login", app.Login)
http.HandleFunc("/logout", app.Logout)
http.HandleFunc("/signup", app.Signup)
http.HandleFunc("/request-invite", app.RequestInvite)
http.HandleFunc("/account", app.Account)
http.HandleFunc("/verify", app.Verify)
http.HandleFunc("/session", app.Session)
http.HandleFunc("/updates", updatesHandler)
http.HandleFunc("/token", app.TokenHandler)
http.HandleFunc("/passkey/", app.PasskeyHandler)
// OAuth 2.1 for MCP authentication
http.HandleFunc("/.well-known/oauth-authorization-server", auth.OAuthMetadataHandler)
http.HandleFunc("/.well-known/oauth-protected-resource", auth.OAuthResourceHandler)
http.HandleFunc("/oauth/register", auth.OAuthRegisterHandler)
http.HandleFunc("/oauth/authorize", auth.OAuthAuthorizePostHandler)
http.HandleFunc("/oauth/token", auth.OAuthTokenHandler)
// internal status (injected into admin server page)
app.DKIMStatusFunc = mail.DKIMStatus
app.DigestStatusFunc = digest.Status
admin.GenerateDigestFunc = digest.Generate
// public status page - service health checks
app.HealthCheckFunc = runHealthChecks
http.HandleFunc("/status", app.StatusHandler)
// whitepaper
http.HandleFunc("/whitepaper", docs.WhitepaperHandler)
http.HandleFunc("/whitepaper.pdf", docs.WhitepaperHandler)
// documentation
http.HandleFunc("/docs", docs.Handler)
http.HandleFunc("/docs/", docs.Handler)
// ActivityPub: WebFinger discovery
http.HandleFunc("/.well-known/webfinger", blog.WebFingerHandler)
// presence WebSocket endpoint
http.HandleFunc("/presence", user.PresenceHandler)
// presence ping endpoint
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
_, acc, err := auth.RequireSession(r)
if err != nil {
app.Unauthorized(w, r)
return
}
auth.UpdatePresence(acc.ID)
w.Header().Set("Content-Type", "application/json")
onlineCount := auth.GetOnlineCount()
w.Write([]byte(fmt.Sprintf(`{"status":"ok","online":%d}`, onlineCount)))
})
// serve the api doc
http.HandleFunc("/api", api.APIPageHandler)
// serve the MCP page and server (GET = HTML page, POST = JSON-RPC)
http.HandleFunc("/mcp", api.MCPHandler)
// serve the app
http.Handle("/", app.Serve())
// Create server with handler
server := &http.Server{
Addr: *AddressFlag,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Block known bot paths silently
if strings.HasPrefix(r.URL.Path, "/audio/") {
http.NotFound(w, r)
return
}
// Set Onion-Location header for Tor Browser discovery
if onion := os.Getenv("TOR_ONION"); onion != "" {
w.Header().Set("Onion-Location", "http://"+onion+r.URL.RequestURI())
}
// Request logging (Apache-style)
start := time.Now()
defer func() {
// Skip logging for static assets and frequent endpoints
if !strings.HasSuffix(r.URL.Path, ".css") &&
!strings.HasSuffix(r.URL.Path, ".js") &&
!strings.HasSuffix(r.URL.Path, ".png") &&
!strings.HasSuffix(r.URL.Path, ".ico") &&
!strings.HasPrefix(r.URL.Path, "/chat/ws") {
app.Log("http", "%s %s %s %v", r.Method, r.URL.Path, r.RemoteAddr, time.Since(start))
}
}()
if *EnvFlag == "dev" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "true")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
}
if v := len(r.URL.Path); v > 1 && strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path = r.URL.Path[:v-1]
}
// Fast path for static assets - skip all middleware
for _, ext := range staticPaths {
if strings.HasSuffix(r.URL.Path, ext) {
http.DefaultServeMux.ServeHTTP(w, r)
return
}
}
var token string
// set via session cookie
if c, err := r.Cookie("session"); err == nil && c != nil {
token = c.Value
}
// Try Authorization header (Bearer token or PAT)
if token == "" {
authHeader := r.Header.Get("Authorization")
if authHeader != "" {
// Support both "Bearer <token>" and just "<token>"
if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
token = authHeader[7:]
} else {
token = authHeader
}
}
}
// Try X-Micro-Token header (legacy support)
if token == "" {
token = r.Header.Get("X-Micro-Token")
}
// Check if static asset - skip authentication entirely
isStaticAsset := false
for _, ext := range staticPaths {
if strings.HasSuffix(r.URL.Path, ext) {
isStaticAsset = true
break
}
}
// Skip auth check for static assets