44 readResponseTextWithLimit ,
55 readResponseToBufferWithLimit ,
66} from '@/lib/core/utils/stream-limits'
7- import { uploadExecutionFile } from '@/lib/uploads/contexts/execution'
87import type { UserFile } from '@/executor/types'
98import { presentationUrl } from '@/tools/google_slides/utils'
109import type { ToolConfig } from '@/tools/types'
@@ -29,10 +28,10 @@ interface ExportPresentationResponse {
2928 }
3029}
3130
32- const MAX_GOOGLE_SLIDES_EXPORT_BYTES = 10 * 1024 * 1024
33- const MAX_LEGACY_INLINE_EXPORT_BYTES = 7 * 1024 * 1024
31+ export const MAX_GOOGLE_SLIDES_EXPORT_BYTES = 10 * 1024 * 1024
32+ export const MAX_LEGACY_INLINE_EXPORT_BYTES = 7 * 1024 * 1024
3433
35- const FORMAT_TO_MIME : Record < string , string > = {
34+ export const FORMAT_TO_MIME : Record < string , string > = {
3635 PDF : 'application/pdf' ,
3736 PPTX : 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ,
3837 ODP : 'application/vnd.oasis.opendocument.presentation' ,
@@ -42,7 +41,7 @@ const FORMAT_TO_MIME: Record<string, string> = {
4241 SVG : 'image/svg+xml' ,
4342}
4443
45- function getExecutionContext ( params ?: ExportPresentationParams ) : {
44+ export function getGoogleSlidesExportExecutionContext ( params ?: ExportPresentationParams ) : {
4645 context ?: { workspaceId : string ; workflowId : string ; executionId : string }
4746 userId ?: string
4847} {
@@ -61,6 +60,27 @@ function getExecutionContext(params?: ExportPresentationParams): {
6160 return { context : { workspaceId, workflowId, executionId } , userId }
6261}
6362
63+ export async function readGoogleSlidesExportResponse ( response : Response ) : Promise < Buffer > {
64+ if ( ! response . ok ) {
65+ let errorMessage = `Failed to export presentation (status ${ response . status } )`
66+ try {
67+ const text = await readResponseTextWithLimit ( response , {
68+ maxBytes : 64 * 1024 ,
69+ label : 'Google Slides export error response' ,
70+ } )
71+ const data = JSON . parse ( text )
72+ errorMessage = data . error ?. message || errorMessage
73+ logger . error ( 'Drive API error during export:' , { data } )
74+ } catch { }
75+ throw new Error ( errorMessage )
76+ }
77+
78+ return readResponseToBufferWithLimit ( response , {
79+ maxBytes : MAX_GOOGLE_SLIDES_EXPORT_BYTES ,
80+ label : 'Google Slides export' ,
81+ } )
82+ }
83+
6484export const exportPresentationTool : ToolConfig <
6585 ExportPresentationParams ,
6686 ExportPresentationResponse
@@ -111,52 +131,23 @@ export const exportPresentationTool: ToolConfig<
111131 } ,
112132
113133 transformResponse : async ( response : Response , params ) => {
114- if ( ! response . ok ) {
115- let errorMessage = `Failed to export presentation (status ${ response . status } )`
116- try {
117- const text = await readResponseTextWithLimit ( response , {
118- maxBytes : 64 * 1024 ,
119- label : 'Google Slides export error response' ,
120- } )
121- const data = JSON . parse ( text )
122- errorMessage = data . error ?. message || errorMessage
123- logger . error ( 'Drive API error during export:' , { data } )
124- } catch {
125- // Body wasn't JSON — fall through with default error message.
126- }
127- throw new Error ( errorMessage )
128- }
129-
130- const buffer = await readResponseToBufferWithLimit ( response , {
131- maxBytes : MAX_GOOGLE_SLIDES_EXPORT_BYTES ,
132- label : 'Google Slides export' ,
133- } )
134-
134+ const buffer = await readGoogleSlidesExportResponse ( response )
135135 const presentationId = params ?. presentationId ?. trim ( ) || ''
136136 const format = ( params ?. exportFormat || 'PDF' ) . toUpperCase ( )
137137 const mime = FORMAT_TO_MIME [ format ] ?? 'application/octet-stream'
138- const { context, userId } = getExecutionContext ( params )
139- const filename = `${ presentationId || 'presentation' } .${ format . toLowerCase ( ) } `
140- const userFile = context
141- ? await uploadExecutionFile ( context , Buffer . from ( buffer ) , filename , mime , userId )
142- : undefined
143- if ( ! userFile && buffer . length > MAX_LEGACY_INLINE_EXPORT_BYTES ) {
138+ if ( buffer . length > MAX_LEGACY_INLINE_EXPORT_BYTES ) {
144139 throw new PayloadSizeLimitError ( {
145140 label : 'Google Slides legacy inline export' ,
146141 maxBytes : MAX_LEGACY_INLINE_EXPORT_BYTES ,
147142 observedBytes : buffer . length ,
148143 } )
149144 }
150- const contentBase64 =
151- ! userFile && buffer . length <= MAX_LEGACY_INLINE_EXPORT_BYTES
152- ? buffer . toString ( 'base64' )
153- : undefined
145+ const contentBase64 = buffer . toString ( 'base64' )
154146
155147 return {
156148 success : true ,
157149 output : {
158- ...( userFile ? { file : { ...userFile , mimeType : mime } } : { } ) ,
159- ...( contentBase64 ? { contentBase64 } : { } ) ,
150+ contentBase64,
160151 mimeType : mime ,
161152 sizeBytes : buffer . length ,
162153 metadata : {
0 commit comments