| 28 | } |
| 29 | |
| 30 | func (p *Server) storeChatAttachment( |
| 31 | ctx context.Context, |
| 32 | chatSnapshot database.Chat, |
| 33 | name string, |
| 34 | detectName string, |
| 35 | data []byte, |
| 36 | ) (chattool.AttachmentMetadata, error) { |
| 37 | if !chatSnapshot.WorkspaceID.Valid { |
| 38 | return chattool.AttachmentMetadata{}, xerrors.New("no workspace is associated with this chat. Use the create_workspace tool to create one") |
| 39 | } |
| 40 | |
| 41 | storedName, mediaType, err := chatfiles.PrepareStoredFile(name, detectName, data) |
| 42 | if err != nil { |
| 43 | return chattool.AttachmentMetadata{}, err |
| 44 | } |
| 45 | |
| 46 | // Insert and link in one transaction so a cap rejection or linking |
| 47 | // failure does not leave behind an unlinked chat file row. |
| 48 | var attachment chattool.AttachmentMetadata |
| 49 | err = p.db.InTx(func(tx database.Store) error { |
| 50 | ws, err := tx.GetWorkspaceByID(ctx, chatSnapshot.WorkspaceID.UUID) |
| 51 | if err != nil { |
| 52 | return xerrors.Errorf("resolve workspace: %w", err) |
| 53 | } |
| 54 | |
| 55 | attachment, err = storeLinkedChatFileTx( |
| 56 | ctx, |
| 57 | tx, |
| 58 | chatSnapshot.ID, |
| 59 | chatSnapshot.OwnerID, |
| 60 | ws.OrganizationID, |
| 61 | storedName, |
| 62 | mediaType, |
| 63 | data, |
| 64 | ) |
| 65 | return err |
| 66 | }, database.DefaultTXOptions().WithID("store_chat_attachment")) |
| 67 | if err != nil { |
| 68 | return chattool.AttachmentMetadata{}, err |
| 69 | } |
| 70 | return attachment, nil |
| 71 | } |
| 72 | |
| 73 | func storeLinkedChatFileTx( |
| 74 | ctx context.Context, |