1616
1717from llmstack .apps .schemas import OutputTemplate
1818from llmstack .common .blocks .base .schema import StrEnum
19+ from llmstack .common .utils .utils import validate_parse_data_uri
1920from llmstack .processors .providers .api_processor_interface import (
2021 ApiProcessorInterface ,
2122 ApiProcessorSchema ,
@@ -56,6 +57,28 @@ class CodeInterpreterConfiguration(ApiProcessorSchema):
5657 timeout : int = Field (default = 5 , description = "Timeout in seconds" , ge = 1 , le = 30 )
5758
5859
60+ def mime_type_to_content_mime_type (mime_type ):
61+ if mime_type == "text/plain" :
62+ return ContentMimeType .TEXT
63+ if mime_type == "image/png" :
64+ return ContentMimeType .PNG
65+ if mime_type == "image/jpeg" :
66+ return ContentMimeType .JPEG
67+ if mime_type == "image/svg+xml" :
68+ return ContentMimeType .SVG
69+ if mime_type == "application/pdf" :
70+ return ContentMimeType .PDF
71+ if mime_type == "text/html" :
72+ return ContentMimeType .HTML
73+ if mime_type == "application/json" :
74+ return ContentMimeType .JSON
75+ if mime_type == "text/latex" :
76+ return ContentMimeType .LATEX
77+ if mime_type == "text/csv" :
78+ return ContentMimeType .CSV
79+ return ContentMimeType .TEXT
80+
81+
5982class CodeInterpreterProcessor (
6083 ApiProcessorInterface [CodeInterpreterInput , CodeInterpreterOutput , CodeInterpreterConfiguration ],
6184):
@@ -130,7 +153,33 @@ def session_data_to_persist(self) -> dict:
130153 "interpreter_session_data" : self ._interpreter_session_data ,
131154 }
132155
156+ def get_file_data_uri_from_objref (self , objref ):
157+ from llmstack .apps .models import AppSessionFiles
158+
159+ asset_obj = self ._get_session_asset (
160+ objref ,
161+ )
162+ asset = AppSessionFiles .get_asset_data_uri (asset_obj , include_name = True ) if asset_obj else None
163+
164+ return asset
165+
133166 def process (self ) -> dict :
167+ content_files = []
168+ for file in self ._input .files .split ("|" ):
169+ if file .startswith ("objref://" ):
170+ file = self .get_file_data_uri_from_objref (file )
171+ if not file :
172+ continue
173+
174+ mime_type , file_name , data = validate_parse_data_uri (file )
175+ content_files .append (
176+ Content (
177+ mime_type = mime_type_to_content_mime_type (mime_type = mime_type ),
178+ data = base64 .b64decode (data ),
179+ name = file_name ,
180+ )
181+ )
182+
134183 with CodeRunner (
135184 base_url = f"{ settings .RUNNER_HOST } :{ settings .RUNNER_PORT } " ,
136185 session = CodeRunnerSession (
@@ -139,7 +188,7 @@ def process(self) -> dict:
139188 ) as code_runner :
140189 current_state = code_runner .get_state ()
141190 if current_state == CodeRunnerState .CODE_RUNNING :
142- respose_iter = code_runner .run_code (source_code = self ._input .code )
191+ respose_iter = code_runner .run_code (source_code = self ._input .code , files = content_files )
143192 for response in respose_iter :
144193 async_to_sync (self ._output_stream .write )(
145194 CodeInterpreterOutput (
0 commit comments