ultisuite-backend/internal/apitokens/chat_session.go
R3D347HR4Y 621b0099d6
Some checks are pending
CI / Go tests (push) Waiting to run
CI / Integration tests (push) Waiting to run
CI / DB migrations (push) Waiting to run
feat(deploy): enhance Nginx configuration and API integration for UltiAI
- Updated .env.example to include new configuration options for the UltiAI branding and API endpoints.
- Enhanced Nginx configuration to support new API routes for the MCP and WebSocket connections.
- Introduced sub-filters for branding adjustments in Nginx responses.
- Added new JavaScript patch for API endpoint adjustments.
- Implemented tests for new API functionalities and improved error handling in the AI gateway.
2026-06-15 00:22:23 +02:00

104 lines
3.4 KiB
Go

package apitokens
import (
"context"
"fmt"
"strings"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
type ChatSessionPreset string
const (
ChatSessionMail ChatSessionPreset = "mail"
ChatSessionDrive ChatSessionPreset = "drive"
ChatSessionContacts ChatSessionPreset = "contacts"
ChatSessionDocs ChatSessionPreset = "docs"
ChatSessionStandalone ChatSessionPreset = "standalone"
)
type ChatSessionInput struct {
Preset ChatSessionPreset
DrivePath string
AllowWrite bool
TTL time.Duration
}
func CreateChatSession(ctx context.Context, db *pgxpool.Pool, externalID, email string, in ChatSessionInput) (CreatedToken, error) {
if in.TTL <= 0 {
in.TTL = 8 * time.Hour
}
expiresAt := time.Now().UTC().Add(in.TTL)
perms, mailScope, driveScope := chatSessionGrants(in)
name := fmt.Sprintf("UltiAI session %s", time.Now().UTC().Format("2006-01-02 15:04"))
return Create(ctx, db, externalID, name, perms, mailScope, driveScope, AgendaScope{AllCalendars: true}, &expiresAt)
}
func chatSessionGrants(in ChatSessionInput) ([]PermissionGrant, MailScope, DriveScope) {
mailScope := MailScope{AllAccounts: true}
driveScope := DriveScope{AllFolders: true}
if strings.TrimSpace(in.DrivePath) != "" {
driveScope = DriveScope{
AllFolders: false,
FolderPaths: []string{in.DrivePath},
}
}
switch in.Preset {
case ChatSessionMail:
return []PermissionGrant{
{Resource: "mail.messages", Read: true},
{Resource: "mail.search", Read: true},
{Resource: "mail.send", Write: true},
{Resource: "mail.labels", Read: true, Write: true},
{Resource: "contacts.read", Read: true},
{Resource: "contacts.search", Read: true},
{Resource: "automation.chat", Read: true},
}, mailScope, driveScope
case ChatSessionDrive:
return []PermissionGrant{
{Resource: "drive.files", Read: true, Write: in.AllowWrite},
{Resource: "drive.download", Read: true},
{Resource: "automation.chat", Read: true},
}, mailScope, driveScope
case ChatSessionContacts:
return []PermissionGrant{
{Resource: "contacts.read", Read: true},
{Resource: "contacts.search", Read: true},
{Resource: "contacts.write", Write: true},
{Resource: "contacts.delete", Write: true},
{Resource: "mail.search", Read: true},
{Resource: "automation.chat", Read: true},
}, mailScope, driveScope
case ChatSessionDocs:
return []PermissionGrant{
{Resource: "drive.files", Read: true, Write: in.AllowWrite},
{Resource: "drive.download", Read: true},
{Resource: "automation.chat", Read: true},
}, mailScope, driveScope
default:
return []PermissionGrant{
{Resource: "mail.messages", Read: true},
{Resource: "mail.search", Read: true},
{Resource: "mail.send", Write: true},
{Resource: "mail.labels", Read: true, Write: true},
{Resource: "drive.files", Read: true, Write: true},
{Resource: "drive.download", Read: true},
{Resource: "contacts.read", Read: true},
{Resource: "contacts.search", Read: true},
{Resource: "contacts.write", Write: true},
{Resource: "contacts.delete", Write: true},
{Resource: "agenda.calendars", Read: true, Write: true},
{Resource: "agenda.events", Read: true},
{Resource: "agenda.events.write", Write: true},
{Resource: "agenda.events.delete", Write: true},
{Resource: "agenda.freebusy", Read: true},
{Resource: "agenda.response", Write: true},
{Resource: "automation.search", Read: true},
{Resource: "automation.chat", Read: true},
}, mailScope, driveScope
}
}