11import base64
22import io
33import logging
4- from pathlib import Path
54from typing import Annotated , Any
65
76import docx2txt
87import pdfplumber
8+ from assistant_drive import Drive , DriveConfig
9+ from context import Context
910from openai .types import chat
1011from pydantic import BaseModel , Field
1112from semantic_workbench_api_model .workbench_model import File
1415 FileStorageContext ,
1516)
1617from semantic_workbench_assistant .config import UISchema
17- from semantic_workbench_assistant .storage import (
18- read_model ,
19- read_models_in_dir ,
20- write_model ,
21- )
2218
2319logger = logging .getLogger (__name__ )
2420
@@ -84,15 +80,16 @@ async def create_or_update_attachment_from_file(
8480 content = await _file_to_str (context , file )
8581
8682 # see if there is already an attachment with this filename
87- attachment = read_model ( _get_attachment_storage_path ( context , filename ), Attachment )
88- if attachment :
83+ try :
84+ attachment = _get_attachment_drive ( context ). read_model ( Attachment , filename )
8985 # if there is, update the content
9086 attachment .content = content
91- else :
87+ except FileNotFoundError :
9288 # if there isn't, create a new attachment
9389 attachment = Attachment (filename = filename , content = content , metadata = metadata )
9490
95- write_model (_get_attachment_storage_path (context , filename ), attachment )
91+ # write the attachment to the storage
92+ _get_attachment_drive (context ).write_model (attachment , filename )
9693
9794 @staticmethod
9895 def delete_attachment_for_file (context : ConversationContext , file : File ) -> None :
@@ -101,7 +98,7 @@ def delete_attachment_for_file(context: ConversationContext, file: File) -> None
10198 """
10299
103100 filename = file .filename
104- _get_attachment_storage_path (context , filename ). unlink ( missing_ok = True )
101+ _get_attachment_drive (context ). delete ( filename )
105102
106103 @staticmethod
107104 def generate_attachment_messages (
@@ -120,7 +117,7 @@ def generate_attachment_messages(
120117 """
121118
122119 # get all attachments and exit early if there are none
123- attachments = read_models_in_dir ( _get_attachment_storage_path ( context ), Attachment )
120+ attachments = _get_attachment_drive ( context ). read_models ( Attachment )
124121 if not attachments :
125122 return []
126123
@@ -233,14 +230,13 @@ def reduce_attachment_payload_from_content(value: Any) -> Any:
233230#
234231
235232
236- def _get_attachment_storage_path (context : ConversationContext , filename : str | None = None ) -> Path :
233+ def _get_attachment_drive (context : ConversationContext ) -> Drive :
237234 """
238- Get the path where attachments are stored .
235+ Get the Drive instance for the attachments .
239236 """
240- path = FileStorageContext .get (context ).directory / "attachments"
241- if filename :
242- path /= filename
243- return path
237+ drive_context = Context (session_id = context .id )
238+ drive_root = str (FileStorageContext .get (context ).directory / "attachments" )
239+ return Drive (DriveConfig (context = drive_context , root = drive_root ))
244240
245241
246242async def _raw_content_from_file (context : ConversationContext , file : File ) -> bytes :
0 commit comments