-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.go
More file actions
607 lines (569 loc) · 17.4 KB
/
helpers.go
File metadata and controls
607 lines (569 loc) · 17.4 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
package agentremote
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/rs/zerolog"
"go.mau.fi/util/ptr"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/bridgev2/simplevent"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
"github.com/beeper/agentremote/pkg/matrixevents"
)
const AIRoomKindAgent = "agent"
func BuildMetaTypes(portal, message, userLogin, ghost func() any) database.MetaTypes {
return database.MetaTypes{
Portal: portal,
Message: message,
UserLogin: userLogin,
Ghost: ghost,
}
}
// DMChatInfoParams holds the parameters for BuildDMChatInfo.
type DMChatInfoParams struct {
Title string
Topic string
HumanUserID networkid.UserID
LoginID networkid.UserLoginID
HumanSender *bridgev2.EventSender
BotUserID networkid.UserID
BotDisplayName string
BotSender *bridgev2.EventSender
BotUserInfo *bridgev2.UserInfo
BotMemberEventExtra map[string]any
CanBackfill bool
}
// BuildDMChatInfo creates a ChatInfo for a DM room between a human user and a bot ghost.
func BuildDMChatInfo(p DMChatInfoParams) *bridgev2.ChatInfo {
humanSender := bridgev2.EventSender{
Sender: p.HumanUserID,
IsFromMe: true,
SenderLogin: p.LoginID,
}
if p.HumanSender != nil {
humanSender = *p.HumanSender
}
botSender := bridgev2.EventSender{
Sender: p.BotUserID,
SenderLogin: p.LoginID,
}
if p.BotSender != nil {
botSender = *p.BotSender
}
botInfo := p.BotUserInfo
if botInfo == nil {
botInfo = &bridgev2.UserInfo{
Name: ptr.Ptr(p.BotDisplayName),
IsBot: ptr.Ptr(true),
}
}
memberEventExtra := p.BotMemberEventExtra
if memberEventExtra == nil && p.BotDisplayName != "" {
memberEventExtra = map[string]any{
"displayname": p.BotDisplayName,
}
}
members := bridgev2.ChatMemberMap{
p.HumanUserID: {
EventSender: humanSender,
Membership: event.MembershipJoin,
},
p.BotUserID: {
EventSender: botSender,
Membership: event.MembershipJoin,
UserInfo: botInfo,
MemberEventExtra: memberEventExtra,
},
}
return &bridgev2.ChatInfo{
Name: ptr.Ptr(p.Title),
Topic: ptr.NonZero(p.Topic),
Type: ptr.Ptr(database.RoomTypeDM),
CanBackfill: p.CanBackfill,
Members: &bridgev2.ChatMemberList{
IsFull: true,
OtherUserID: p.BotUserID,
MemberMap: members,
},
}
}
type LoginDMChatInfoParams struct {
Title string
Topic string
Login *bridgev2.UserLogin
HumanUserIDPrefix string
HumanSender *bridgev2.EventSender
BotUserID networkid.UserID
BotDisplayName string
BotSender *bridgev2.EventSender
BotUserInfo *bridgev2.UserInfo
BotMemberEventExtra map[string]any
CanBackfill bool
}
func BuildLoginDMChatInfo(p LoginDMChatInfoParams) *bridgev2.ChatInfo {
if p.Login == nil {
return nil
}
return BuildDMChatInfo(DMChatInfoParams{
Title: p.Title,
Topic: p.Topic,
HumanUserID: HumanUserID(p.HumanUserIDPrefix, p.Login.ID),
LoginID: p.Login.ID,
HumanSender: p.HumanSender,
BotUserID: p.BotUserID,
BotDisplayName: p.BotDisplayName,
BotSender: p.BotSender,
BotUserInfo: p.BotUserInfo,
BotMemberEventExtra: p.BotMemberEventExtra,
CanBackfill: p.CanBackfill,
})
}
type ConfigureDMPortalParams struct {
Portal *bridgev2.Portal
Title string
Topic string
OtherUserID networkid.UserID
Save bool
MutatePortal func(*bridgev2.Portal)
}
func ConfigureDMPortal(ctx context.Context, p ConfigureDMPortalParams) error {
if p.Portal == nil {
return fmt.Errorf("missing portal")
}
p.Portal.RoomType = database.RoomTypeDM
p.Portal.OtherUserID = p.OtherUserID
p.Portal.Name = strings.TrimSpace(p.Title)
p.Portal.NameSet = p.Portal.Name != ""
p.Portal.Topic = strings.TrimSpace(p.Topic)
p.Portal.TopicSet = p.Portal.Topic != ""
if p.MutatePortal != nil {
p.MutatePortal(p.Portal)
}
if !p.Save {
return nil
}
return p.Portal.Save(ctx)
}
type PreConvertedRemoteMessageParams struct {
PortalKey networkid.PortalKey
Sender bridgev2.EventSender
MsgID networkid.MessageID
IDPrefix string
LogKey string
Timestamp time.Time
StreamOrder int64
Converted *bridgev2.ConvertedMessage
}
func BuildPreConvertedRemoteMessage(p PreConvertedRemoteMessageParams) *simplevent.PreConvertedMessage {
if p.MsgID == "" {
p.MsgID = NewMessageID(p.IDPrefix)
}
timing := ResolveEventTiming(p.Timestamp, p.StreamOrder)
return &simplevent.PreConvertedMessage{
EventMeta: simplevent.EventMeta{
Type: bridgev2.RemoteEventMessage,
PortalKey: p.PortalKey,
Sender: p.Sender,
Timestamp: timing.Timestamp,
StreamOrder: timing.StreamOrder,
LogContext: func(c zerolog.Context) zerolog.Context {
return c.Str(p.LogKey, string(p.MsgID))
},
},
ID: p.MsgID,
Data: p.Converted,
}
}
// SendViaPortalParams holds the parameters for SendViaPortal.
type SendViaPortalParams struct {
Login *bridgev2.UserLogin
Portal *bridgev2.Portal
Sender bridgev2.EventSender
IDPrefix string // e.g. "ai", "codex", "opencode"
LogKey string // zerolog field name, e.g. "ai_msg_id"
MsgID networkid.MessageID
Timestamp time.Time
// StreamOrder is optional explicit ordering for events that share a timestamp.
StreamOrder int64
Converted *bridgev2.ConvertedMessage
}
// SendViaPortal sends a pre-built message through bridgev2's QueueRemoteEvent pipeline.
// If MsgID is empty, a new one is generated using IDPrefix.
func SendViaPortal(p SendViaPortalParams) (id.EventID, networkid.MessageID, error) {
if p.Portal == nil || p.Portal.MXID == "" {
return "", "", fmt.Errorf("invalid portal")
}
if p.Login == nil || p.Login.Bridge == nil {
return "", p.MsgID, fmt.Errorf("bridge unavailable")
}
evt := BuildPreConvertedRemoteMessage(PreConvertedRemoteMessageParams{
PortalKey: p.Portal.PortalKey,
Sender: p.Sender,
MsgID: p.MsgID,
IDPrefix: p.IDPrefix,
LogKey: p.LogKey,
Timestamp: p.Timestamp,
StreamOrder: p.StreamOrder,
Converted: p.Converted,
})
result := p.Login.QueueRemoteEvent(evt)
if !result.Success {
if result.Error != nil {
return "", evt.ID, fmt.Errorf("send failed: %w", result.Error)
}
return "", evt.ID, fmt.Errorf("send failed")
}
return result.EventID, evt.ID, nil
}
// SendEditViaPortal queues a pre-built edit through bridgev2's remote event pipeline.
func SendEditViaPortal(
login *bridgev2.UserLogin,
portal *bridgev2.Portal,
sender bridgev2.EventSender,
targetMessage networkid.MessageID,
timestamp time.Time,
streamOrder int64,
logKey string,
converted *bridgev2.ConvertedEdit,
) error {
if portal == nil || portal.MXID == "" {
return fmt.Errorf("invalid portal")
}
if login == nil || login.Bridge == nil {
return fmt.Errorf("bridge unavailable")
}
if targetMessage == "" {
return fmt.Errorf("invalid target message")
}
timing := ResolveEventTiming(timestamp, streamOrder)
result := login.QueueRemoteEvent(&RemoteEdit{
Portal: portal.PortalKey,
Sender: sender,
TargetMessage: targetMessage,
Timestamp: timing.Timestamp,
StreamOrder: timing.StreamOrder,
LogKey: logKey,
PreBuilt: converted,
})
if !result.Success {
if result.Error != nil {
return fmt.Errorf("edit failed: %w", result.Error)
}
return fmt.Errorf("edit failed")
}
return nil
}
// RedactEventAsSender redacts an event ID in a room using the intent resolved for sender.
func RedactEventAsSender(
ctx context.Context,
login *bridgev2.UserLogin,
portal *bridgev2.Portal,
sender bridgev2.EventSender,
targetEventID id.EventID,
) error {
if login == nil || portal == nil || portal.MXID == "" || targetEventID == "" {
return fmt.Errorf("invalid redaction target")
}
intent, ok := portal.GetIntentFor(ctx, sender, login, bridgev2.RemoteEventMessageRemove)
if !ok || intent == nil {
return fmt.Errorf("intent resolution failed")
}
_, err := intent.SendMessage(ctx, portal.MXID, event.EventRedaction, &event.Content{
Parsed: &event.RedactionEventContent{Redacts: targetEventID},
}, nil)
return err
}
func BuildChatInfoWithFallback(metaTitle, portalName, fallbackTitle, portalTopic string) *bridgev2.ChatInfo {
title := coalesceStrings(metaTitle, portalName, fallbackTitle)
return &bridgev2.ChatInfo{
Name: ptr.Ptr(title),
Topic: ptr.NonZero(portalTopic),
}
}
var MediaMessageTypes = []event.MessageType{
event.MsgImage,
event.MsgVideo,
event.MsgAudio,
event.MsgFile,
event.CapMsgVoice,
event.CapMsgGIF,
event.CapMsgSticker,
}
type RoomFeaturesParams struct {
ID string
File event.FileFeatureMap
MaxTextLength int
Reply event.CapabilitySupportLevel
Thread event.CapabilitySupportLevel
Edit event.CapabilitySupportLevel
Delete event.CapabilitySupportLevel
Reaction event.CapabilitySupportLevel
ReadReceipts bool
TypingNotifications bool
DeleteChat bool
}
func BuildRoomFeatures(p RoomFeaturesParams) *event.RoomFeatures {
return &event.RoomFeatures{
ID: p.ID,
File: p.File,
MaxTextLength: p.MaxTextLength,
Reply: p.Reply,
Thread: p.Thread,
Edit: p.Edit,
Delete: p.Delete,
Reaction: p.Reaction,
ReadReceipts: p.ReadReceipts,
TypingNotifications: p.TypingNotifications,
DeleteChat: p.DeleteChat,
}
}
func BuildMediaFileFeatureMap(build func() *event.FileFeatures) event.FileFeatureMap {
files := make(event.FileFeatureMap, len(MediaMessageTypes))
for _, msgType := range MediaMessageTypes {
files[msgType] = build()
}
return files
}
func ExpandUserHome(path string) (string, error) {
rest, isTilde := strings.CutPrefix(strings.TrimSpace(path), "~")
if !isTilde {
return strings.TrimSpace(path), nil
}
if rest != "" && rest[0] != '/' {
return strings.TrimSpace(path), nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, rest), nil
}
func NormalizeAbsolutePath(path string) (string, error) {
expanded, err := ExpandUserHome(path)
if err != nil {
return "", err
}
if !filepath.IsAbs(expanded) {
return "", fmt.Errorf("path must be absolute")
}
return filepath.Clean(expanded), nil
}
func SendSystemMessage(
ctx context.Context,
login *bridgev2.UserLogin,
portal *bridgev2.Portal,
sender bridgev2.EventSender,
body string,
) error {
body = strings.TrimSpace(body)
if login == nil || login.Bridge == nil {
return fmt.Errorf("bridge unavailable")
}
if portal == nil || portal.MXID == "" {
return fmt.Errorf("invalid portal")
}
if body == "" {
return nil
}
content := &event.Content{
Parsed: &event.MessageEventContent{
MsgType: event.MsgNotice,
Body: body,
Mentions: &event.Mentions{},
},
}
if login.Bridge.Bot != nil {
_, err := login.Bridge.Bot.SendMessage(ctx, portal.MXID, event.EventMessage, content, nil)
return err
}
intent, ok := portal.GetIntentFor(ctx, sender, login, bridgev2.RemoteEventMessage)
if !ok || intent == nil {
return fmt.Errorf("intent resolution failed")
}
_, err := intent.SendMessage(ctx, portal.MXID, event.EventMessage, content, nil)
return err
}
// BuildBotUserInfo returns a UserInfo for an AI bot ghost with the given name and identifiers.
func BuildBotUserInfo(name string, identifiers ...string) *bridgev2.UserInfo {
return &bridgev2.UserInfo{
Name: ptr.Ptr(name),
IsBot: ptr.Ptr(true),
Identifiers: identifiers,
}
}
func NormalizeAIRoomTypeV2(roomType database.RoomType, aiKind string) string {
if aiKind != "" && aiKind != AIRoomKindAgent {
return "group"
}
switch roomType {
case database.RoomTypeDM:
return "dm"
case database.RoomTypeSpace:
return "space"
default:
return "group"
}
}
func ApplyAgentRemoteBridgeInfo(content *event.BridgeEventContent, protocolID string, roomType database.RoomType, aiKind string) {
if content == nil {
return
}
if protocolID != "" {
content.Protocol.ID = protocolID
}
content.BeeperRoomTypeV2 = NormalizeAIRoomTypeV2(roomType, aiKind)
}
func SendAIRoomInfo(ctx context.Context, portal *bridgev2.Portal, aiKind string) bool {
if portal == nil || portal.MXID == "" || portal.Bridge == nil || portal.Bridge.Bot == nil {
return false
}
if aiKind == "" {
aiKind = AIRoomKindAgent
}
_, err := portal.Bridge.Bot.SendState(ctx, portal.MXID, matrixevents.AIRoomInfoEventType, "", &event.Content{
Parsed: map[string]any{"type": aiKind},
Raw: map[string]any{"com.beeper.exclude_from_timeline": true},
}, time.Now())
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to send AI room info state event")
return false
}
return true
}
// findExistingMessage performs a two-phase message lookup: first by network
// message ID (with receiver resolution), then by Matrix event ID as fallback.
// Returns the message (if found) and separate errors from each lookup phase.
func findExistingMessage(
ctx context.Context,
login *bridgev2.UserLogin,
portal *bridgev2.Portal,
networkMessageID networkid.MessageID,
initialEventID id.EventID,
) (msg *database.Message, errByID error, errByMXID error) {
receiver := portal.Receiver
if receiver == "" {
receiver = login.ID
}
if receiver != "" && networkMessageID != "" {
msg, errByID = login.Bridge.DB.Message.GetPartByID(ctx, receiver, networkMessageID, networkid.PartID("0"))
}
if msg == nil && initialEventID != "" {
msg, errByMXID = login.Bridge.DB.Message.GetPartByMXID(ctx, initialEventID)
}
return msg, errByID, errByMXID
}
// UpsertAssistantMessageParams holds parameters for UpsertAssistantMessage.
type UpsertAssistantMessageParams struct {
Login *bridgev2.UserLogin
Portal *bridgev2.Portal
SenderID networkid.UserID
NetworkMessageID networkid.MessageID
InitialEventID id.EventID
Metadata any // must satisfy database.MetaMerger
Logger zerolog.Logger
}
// UpsertAssistantMessage updates an existing message's metadata or inserts a new one.
// If NetworkMessageID is set, tries to find and update the existing row first.
// Falls back to inserting a new row keyed by InitialEventID.
func UpsertAssistantMessage(ctx context.Context, p UpsertAssistantMessageParams) {
if p.Login == nil || p.Portal == nil {
return
}
db := p.Login.Bridge.DB.Message
if p.NetworkMessageID != "" {
existing, errByID, errByMXID := findExistingMessage(ctx, p.Login, p.Portal, p.NetworkMessageID, p.InitialEventID)
if existing != nil {
existing.Metadata = p.Metadata
if err := db.Update(ctx, existing); err != nil {
p.Logger.Warn().Err(err).Str("msg_id", string(existing.ID)).Msg("Failed to update assistant message metadata")
} else {
p.Logger.Debug().Str("msg_id", string(existing.ID)).Msg("Updated assistant message metadata")
}
return
}
p.Logger.Warn().
AnErr("err_by_id", errByID).
AnErr("err_by_mxid", errByMXID).
Stringer("mxid", p.InitialEventID).
Str("msg_id", string(p.NetworkMessageID)).
Msg("Could not find existing DB row for update, falling back to insert")
}
if p.InitialEventID == "" {
return
}
assistantMsg := &database.Message{
ID: MatrixMessageID(p.InitialEventID),
Room: p.Portal.PortalKey,
SenderID: p.SenderID,
MXID: p.InitialEventID,
Timestamp: time.Now(),
Metadata: p.Metadata,
}
if err := db.Insert(ctx, assistantMsg); err != nil {
p.Logger.Warn().Err(err).Msg("Failed to insert assistant message to database")
} else {
p.Logger.Debug().Str("msg_id", string(assistantMsg.ID)).Msg("Inserted assistant message to database")
}
}
// DefaultApprovalExpiry is the fallback expiry duration when no TTL is specified.
const DefaultApprovalExpiry = 10 * time.Minute
// ComputeApprovalExpiry returns the expiry time based on ttlSeconds, falling
// back to DefaultApprovalExpiry when ttlSeconds <= 0.
func ComputeApprovalExpiry(ttlSeconds int) time.Time {
if ttlSeconds > 0 {
return time.Now().Add(time.Duration(ttlSeconds) * time.Second)
}
return time.Now().Add(DefaultApprovalExpiry)
}
// BuildContinuationMessage constructs a ConvertedMessage for overflow
// continuation text, flagged with "com.beeper.continuation".
func BuildContinuationMessage(
portal networkid.PortalKey,
body string,
sender bridgev2.EventSender,
idPrefix,
logKey string,
timestamp time.Time,
streamOrder int64,
) *simplevent.PreConvertedMessage {
rendered := format.RenderMarkdown(body, true, true)
content := &event.MessageEventContent{
MsgType: event.MsgText,
Body: rendered.Body,
Format: rendered.Format,
FormattedBody: rendered.FormattedBody,
Mentions: &event.Mentions{},
}
return BuildPreConvertedRemoteMessage(PreConvertedRemoteMessageParams{
PortalKey: portal,
Sender: sender,
IDPrefix: idPrefix,
LogKey: logKey,
Timestamp: timestamp,
StreamOrder: streamOrder,
Converted: &bridgev2.ConvertedMessage{
Parts: []*bridgev2.ConvertedMessagePart{{
ID: networkid.PartID("0"),
Type: event.EventMessage,
Content: content,
Extra: map[string]any{"com.beeper.continuation": true},
}},
},
})
}
// coalesceStrings returns the first non-empty string from the arguments.
func coalesceStrings(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}