Skip to content

Commit a3d324c

Browse files
bangarangcarlbrugger
authored andcommitted
feat: cleanup
1 parent 91096b2 commit a3d324c

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

export/pdf/src/export.pdf.plugin.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { FlatfileListener } from '@flatfile/listener'
1+
import { FlatfileListener, FlatfileEvent } from '@flatfile/listener'
22
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
33
import { Client } from '@anthropic-ai/sdk'
44
import api from '@flatfile/api'
55
import * as fs from 'fs'
6+
import { jobHandler } from '@flatfile/plugin-job-handler'
67

7-
// Initialize Flatfile client
8-
const flatfile = new api.Client({ token: process.env.FLATFILE_API_KEY })
8+
async function analyzeDataWithAI(sheetData: any[], ANTHROPIC_API_KEY: string) {
9+
const dataDescription = JSON.stringify(sheetData)
910

10-
// Initialize Anthropic client
11-
const anthropic = new Client(process.env.ANTHROPIC_API_KEY)
12-
13-
async function analyzeDataWithAI(sheetData: any[]) {
14-
const dataDescription = JSON.stringify(sheetData.slice(0, 10))
11+
const anthropic = new Client(ANTHROPIC_API_KEY)
1512

1613
const prompt = `Given the following dataset: ${dataDescription}
1714
@@ -25,18 +22,22 @@ async function analyzeDataWithAI(sheetData: any[]) {
2522
const response = await anthropic.completions.create({
2623
model: 'claude-2',
2724
prompt: prompt,
28-
max_tokens_to_sample: 500,
2925
})
3026

3127
return response.completion
3228
}
3329

34-
async function generatePDFReport(sheetData: any[]) {
30+
async function generatePDFReport(sheetData: any[], event) {
3531
const pdfDoc = await PDFDocument.create()
3632
const page = pdfDoc.addPage()
3733
const { height } = page.getSize()
3834
const font = await pdfDoc.embedFont(StandardFonts.Helvetica)
35+
const anthropicApiKey =
36+
process.env.ANTHROPIC_API_KEY || event.secrets('ANTHROPIC_API_KEY')
3937

38+
if (!anthropicApiKey) {
39+
throw new Error('Anthropic API key is not set')
40+
}
4041
// Add title
4142
page.drawText('Data Analysis Report', {
4243
x: 50,
@@ -85,7 +86,7 @@ async function generatePDFReport(sheetData: any[]) {
8586
})
8687
yOffset -= 20
8788

88-
const aiAnalysis = await analyzeDataWithAI(sheetData)
89+
const aiAnalysis = await analyzeDataWithAI(sheetData, anthropicApiKey)
8990
const words = aiAnalysis.split(' ')
9091
let line = ''
9192
for (const word of words) {
@@ -126,7 +127,7 @@ async function uploadPDFToFlatfile(
126127
const fileStream = fs.createReadStream(tempFilePath)
127128

128129
try {
129-
const response = await flatfile.files.upload(fileStream, {
130+
const response = await api.files.upload(fileStream, {
130131
spaceId,
131132
environmentId,
132133
})
@@ -138,11 +139,17 @@ async function uploadPDFToFlatfile(
138139
}
139140
}
140141

141-
async function handleJobReady(event: any) {
142-
const sheetId = event.context.sheetId
143-
const { data: records } = await flatfile.records.get(sheetId)
142+
async function handleJobReady(event: FlatfileEvent) {
143+
const { context } = event.payload
144+
const sheetId = context.sheetId
145+
146+
if (!sheetId) {
147+
throw new Error('Sheet ID is missing from the event context')
148+
}
149+
150+
const { data: records } = await api.records.get(sheetId, { pageSize: 50 })
144151

145-
const pdfBytes = await generatePDFReport(records)
152+
const pdfBytes = await generatePDFReport(records.records, event)
146153

147154
const fileName = `report_${sheetId}_${Date.now()}.pdf`
148155
const spaceId = process.env.FLATFILE_SPACE_ID
@@ -156,15 +163,11 @@ async function handleJobReady(event: any) {
156163
}
157164

158165
const listener = FlatfileListener.create((listener) => {
159-
listener.on('job:ready', async (event) => {
160-
try {
166+
listener.use(
167+
jobHandler('pdf-export', async (event) => {
161168
await handleJobReady(event)
162-
await event.acknowledge()
163-
} catch (error) {
164-
console.error('Error in job:ready handler:', error)
165-
await event.fail(error.message)
166-
}
167-
})
169+
})
170+
)
168171
})
169172

170173
export default listener

0 commit comments

Comments
 (0)