@@ -71,9 +71,8 @@ export default defineConfig({
7171This task uses the ` python.runScript ` method to run the ` markdown-converter.py ` script with the given document URL as an argument.
7272
7373``` ts src/trigger/convertToMarkdown.ts
74- import { task } from " @trigger.dev/sdk/v3 " ;
74+ import { task } from " @trigger.dev/sdk" ;
7575import { python } from " @trigger.dev/python" ;
76- import { z } from " zod" ;
7776import * as fs from " fs" ;
7877import * as path from " path" ;
7978import * as os from " os" ;
@@ -83,97 +82,54 @@ import * as http from "http";
8382export const convertToMarkdown = task ({
8483 id: " convert-to-markdown" ,
8584 run : async (payload : { url: string }) => {
86- try {
87- const { url } = payload ;
88-
89- // STEP 1: Create temporary file with unique name
90- const tempDir = os .tmpdir ();
91- const fileName = ` doc-${Date .now ()}-${Math .random ().toString (36 ).substring (2 , 7 )} ` ;
92- const urlPath = new URL (url ).pathname ;
93- // Detect file extension from URL or default to .docx
94- const extension = path .extname (urlPath ) || " .docx" ;
95- const tempFilePath = path .join (tempDir , ` ${fileName }${extension } ` );
96-
97- // STEP 2: Download file from URL
98- await new Promise <void >((resolve , reject ) => {
99- const protocol = url .startsWith (" https" ) ? https : http ;
100- const file = fs .createWriteStream (tempFilePath );
101-
102- protocol
103- .get (url , (response ) => {
104- if (response .statusCode !== 200 ) {
105- reject (new Error (` Download failed with status ${response .statusCode } ` ));
106- return ;
107- }
108-
109- response .pipe (file );
110- file .on (" finish" , () => {
111- file .close ();
112- resolve ();
113- });
114- })
115- .on (" error" , (err ) => {
116- // Clean up on error
117- fs .unlink (tempFilePath , () => {});
118- reject (err );
119- });
85+ const { url } = payload ;
86+
87+ // STEP 1: Create temporary file with unique name
88+ const tempDir = os .tmpdir ();
89+ const fileName = ` doc-${Date .now ()}-${Math .random ().toString (36 ).substring (2 , 7 )} ` ;
90+ const urlPath = new URL (url ).pathname ;
91+ const extension = path .extname (urlPath ) || " .docx" ;
92+ const tempFilePath = path .join (tempDir , ` ${fileName }${extension } ` );
93+
94+ // STEP 2: Download file from URL
95+ await new Promise <void >((resolve , reject ) => {
96+ const protocol = url .startsWith (" https" ) ? https : http ;
97+ const file = fs .createWriteStream (tempFilePath );
98+
99+ protocol .get (url , (response ) => {
100+ response .pipe (file );
101+ file .on (" finish" , () => {
102+ file .close ();
103+ resolve ();
104+ });
120105 });
106+ });
121107
122- // STEP 3: Run Python script to convert document to markdown
123- const pythonResult = await python .runScript (" ./src/python/markdown-converter.py" , [
124- JSON .stringify ({ file_path: tempFilePath }),
125- ]);
126-
127- // STEP 4: Clean up temporary file
128- fs .unlink (tempFilePath , () => {});
129-
130- // STEP 5: Process result - handle possible warnings
131- // Only treat stderr as error if we don't have stdout data
132- // This handles cases where non-critical warnings appear in stderr
133- if (
134- pythonResult .stderr &&
135- ! pythonResult .stderr .includes (" Couldn't find ffmpeg" ) &&
136- ! pythonResult .stdout
137- ) {
138- throw new Error (` Python error: ${pythonResult .stderr } ` );
139- }
140-
141- // If we got valid stdout data, parse and use it regardless of stderr warnings
142- // This ensures harmless warnings don't break the conversion
143- if (pythonResult .stdout ) {
144- const result = JSON .parse (pythonResult .stdout );
108+ // STEP 3: Run Python script to convert document to markdown
109+ const pythonResult = await python .runScript (" ./src/python/markdown-converter.py" , [
110+ JSON .stringify ({ file_path: tempFilePath }),
111+ ]);
145112
146- return {
147- url ,
148- markdown: result .status === " success" ? result .markdown : null ,
149- error: result .status === " error" ? result .error : null ,
150- success: result .status === " success" ,
151- };
152- }
113+ // STEP 4: Clean up temporary file
114+ fs .unlink (tempFilePath , () => {});
153115
116+ // STEP 5: Process result
117+ if (pythonResult .stdout ) {
118+ const result = JSON .parse (pythonResult .stdout );
154119 return {
155120 url ,
156- markdown: null ,
157- error: " No output from Python script" ,
158- success: false ,
159- };
160- } catch (error ) {
161- if (error instanceof z .ZodError ) {
162- return {
163- url: payload .url ,
164- markdown: null ,
165- error: " Invalid URL format: " + error .errors [0 ].message ,
166- success: false ,
167- };
168- }
169-
170- return {
171- url: payload .url ,
172- markdown: null ,
173- error: error instanceof Error ? error .message : String (error ),
174- success: false ,
121+ markdown: result .status === " success" ? result .markdown : null ,
122+ error: result .status === " error" ? result .error : null ,
123+ success: result .status === " success" ,
175124 };
176125 }
126+
127+ return {
128+ url ,
129+ markdown: null ,
130+ error: " No output from Python script" ,
131+ success: false ,
132+ };
177133 },
178134});
179135```
0 commit comments