Repository navigation
Expand file tree
/
Copy pathanalyze.ts
More file actions
57 lines (48 loc) · 1.37 KB
/
Copy pathanalyze.ts
File metadata and controls
57 lines (48 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use server"
import { ActionState } from "@/lib/actions"
import { updateFile } from "@/models/files"
import { getLLMSettings, getSettings } from "@/models/settings"
import { AnalyzeAttachment } from "./attachments"
import { requestLLM } from "./providers/llmProvider"
export type AnalysisResult = {
output: Record<string, string>
tokensUsed: number
}
export async function analyzeTransaction(
prompt: string,
schema: Record<string, unknown>,
attachments: AnalyzeAttachment[],
fileId: string,
userId: string
): Promise<ActionState<AnalysisResult>> {
const settings = await getSettings(userId)
const llmSettings = getLLMSettings(settings)
try {
const response = await requestLLM(llmSettings, {
prompt,
schema,
attachments,
})
if (response.error) {
throw new Error(response.error)
}
const result = response.output
const tokensUsed = response.tokensUsed || 0
console.log("LLM response:", result)
console.log("LLM tokens used:", tokensUsed)
await updateFile(fileId, userId, { cachedParseResult: result })
return {
success: true,
data: {
output: result,
tokensUsed: tokensUsed,
},
}
} catch (error) {
console.error("AI Analysis error:", error)
return {
success: false,
error: error instanceof Error ? error.message : "Failed to analyze invoice",
}
}
}