@fayz-ai/plugin-fiscal-br 0.15.11 → 0.15.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/InboundNotesPage-IEPEEOY2.js +7 -0
- package/dist/{InboundNotesPage-XZZOCYPW.js.map → InboundNotesPage-IEPEEOY2.js.map} +1 -1
- package/dist/NoteReconcileView-3MFYJZME.js +6 -0
- package/dist/{NoteReconcileView-37NQJQLA.js.map → NoteReconcileView-3MFYJZME.js.map} +1 -1
- package/dist/{chunk-TM6XEFDW.js → chunk-GXI2FGDQ.js} +53 -58
- package/dist/chunk-GXI2FGDQ.js.map +1 -0
- package/dist/{chunk-7QDRUE3Z.js → chunk-VMZN3UYW.js} +3 -3
- package/dist/{chunk-7QDRUE3Z.js.map → chunk-VMZN3UYW.js.map} +1 -1
- package/dist/data/inbox.d.ts +1 -10
- package/dist/data/inbox.d.ts.map +1 -1
- package/dist/data/instalments.d.ts +31 -0
- package/dist/data/instalments.d.ts.map +1 -0
- package/dist/index.js +8 -8
- package/dist/views/NoteReconcileView.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/InboundNotesPage-XZZOCYPW.js +0 -6
- package/dist/NoteReconcileView-37NQJQLA.js +0 -5
- package/dist/chunk-TM6XEFDW.js.map +0 -1
- package/functions/fiscalbr-payment-extract/index.ts +0 -138
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// fiscalbr-payment-extract — read a boleto barcode or a Pix code off an attached
|
|
2
|
-
// image/PDF so the instalment fills itself. Port of the V1 `danfe-payment-extract`.
|
|
3
|
-
//
|
|
4
|
-
// Env: OPENAI_API_KEY (already set for fiscalbr-suggest-note-items),
|
|
5
|
-
// FISCALBR_EXTRACT_MODEL (optional). Deployed with verify_jwt: nothing is
|
|
6
|
-
// read or written here; amount and due date are re-derived from the digits in
|
|
7
|
-
// the browser, so the model only has to transcribe.
|
|
8
|
-
const corsHeaders = {
|
|
9
|
-
'Access-Control-Allow-Origin': '*',
|
|
10
|
-
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
11
|
-
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const OPENAI_API_KEY = Deno.env.get('OPENAI_API_KEY');
|
|
15
|
-
|
|
16
|
-
const TOOL_SCHEMA = {
|
|
17
|
-
type: 'function' as const,
|
|
18
|
-
name: 'extract_payment_code',
|
|
19
|
-
description: 'Extrai o código de barras de boleto OU a chave/copia-e-cola PIX do documento.',
|
|
20
|
-
parameters: {
|
|
21
|
-
type: 'object',
|
|
22
|
-
properties: {
|
|
23
|
-
kind: { type: 'string', enum: ['barcode', 'pix', 'none'], description: 'Tipo do código encontrado.' },
|
|
24
|
-
code: { type: 'string', description: 'O código exatamente como visto, sem espaços ou pontos para boleto; PIX copia-e-cola completo ou chave.' },
|
|
25
|
-
amount: { type: ['number', 'null'], description: 'Valor monetário associado se visível.' },
|
|
26
|
-
due_date: { type: ['string', 'null'], description: 'Data de vencimento em formato ISO YYYY-MM-DD se visível no documento.' },
|
|
27
|
-
},
|
|
28
|
-
required: ['kind'],
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const SYSTEM_PROMPT = `Você analisa imagens/PDFs de boletos bancários, comprovantes ou QR Codes PIX e extrai SOMENTE um código pagável.
|
|
33
|
-
|
|
34
|
-
Regras:
|
|
35
|
-
- Boleto bancário: retorne kind="barcode" e o código de barras NUMÉRICO com 44 a 48 dígitos, sem espaços/pontos/traços. Se houver "linha digitável" (47/48 dígitos), retorne-a.
|
|
36
|
-
- PIX (QR Code, chave ou copia-e-cola): retorne kind="pix" e o conteúdo completo (string EMV iniciando com 00020126... ou a chave isolada: CPF/CNPJ/email/telefone/UUID).
|
|
37
|
-
- Se nada disso for legível, retorne kind="none".
|
|
38
|
-
- NUNCA invente dígitos. Se incompleto, retorne kind="none".`;
|
|
39
|
-
|
|
40
|
-
Deno.serve(async (req) => {
|
|
41
|
-
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders });
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
if (!OPENAI_API_KEY) {
|
|
45
|
-
return new Response(JSON.stringify({ ok: false, error: 'OPENAI_API_KEY não configurada' }), {
|
|
46
|
-
status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const { name, mime, base64 } = await req.json();
|
|
51
|
-
// ~15 MB of file; anything bigger is not a boleto and would only burn tokens.
|
|
52
|
-
if (typeof base64 === 'string' && base64.length > 20_000_000) {
|
|
53
|
-
return new Response(JSON.stringify({ ok: false, error: 'Arquivo grande demais para leitura (máx. 15 MB).' }), {
|
|
54
|
-
status: 413, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
if (!base64 || !mime) {
|
|
58
|
-
return new Response(JSON.stringify({ ok: false, error: 'Arquivo inválido' }), {
|
|
59
|
-
status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const filePart = mime.startsWith('image/')
|
|
64
|
-
? { type: 'input_image', image_url: `data:${mime};base64,${base64}`, detail: 'high' }
|
|
65
|
-
: mime === 'application/pdf'
|
|
66
|
-
? { type: 'input_file', filename: name || 'doc.pdf', file_data: `data:application/pdf;base64,${base64}` }
|
|
67
|
-
: null;
|
|
68
|
-
|
|
69
|
-
if (!filePart) {
|
|
70
|
-
return new Response(JSON.stringify({ ok: true, kind: 'none' }), {
|
|
71
|
-
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const oaiResp = await fetch('https://api.openai.com/v1/responses', {
|
|
76
|
-
method: 'POST',
|
|
77
|
-
headers: {
|
|
78
|
-
'Authorization': `Bearer ${OPENAI_API_KEY}`,
|
|
79
|
-
'Content-Type': 'application/json',
|
|
80
|
-
},
|
|
81
|
-
body: JSON.stringify({
|
|
82
|
-
model: Deno.env.get('FISCALBR_EXTRACT_MODEL') ?? 'gpt-4o',
|
|
83
|
-
input: [
|
|
84
|
-
{ role: 'system', content: SYSTEM_PROMPT },
|
|
85
|
-
{ role: 'user', content: [
|
|
86
|
-
{ type: 'input_text', text: 'Analise o arquivo e retorne via extract_payment_code.' },
|
|
87
|
-
filePart,
|
|
88
|
-
] },
|
|
89
|
-
],
|
|
90
|
-
tools: [TOOL_SCHEMA],
|
|
91
|
-
tool_choice: { type: 'function', name: 'extract_payment_code' },
|
|
92
|
-
}),
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
if (!oaiResp.ok) {
|
|
96
|
-
const txt = await oaiResp.text();
|
|
97
|
-
return new Response(JSON.stringify({ ok: false, error: `OpenAI ${oaiResp.status}: ${txt.slice(0, 300)}` }), {
|
|
98
|
-
status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const oai = await oaiResp.json();
|
|
103
|
-
let parsed: any = null;
|
|
104
|
-
for (const item of oai.output || []) {
|
|
105
|
-
if (item.type === 'function_call' && item.name === 'extract_payment_code') {
|
|
106
|
-
try { parsed = JSON.parse(item.arguments); } catch { /* noop */ }
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const kind = parsed?.kind || 'none';
|
|
112
|
-
const code = typeof parsed?.code === 'string' ? parsed.code.trim() : '';
|
|
113
|
-
const amount = typeof parsed?.amount === 'number' ? parsed.amount : null;
|
|
114
|
-
const dueRaw = typeof parsed?.due_date === 'string' ? parsed.due_date.trim() : '';
|
|
115
|
-
const due_date = /^\d{4}-\d{2}-\d{2}$/.test(dueRaw) ? dueRaw : null;
|
|
116
|
-
|
|
117
|
-
// Light sanity check for barcodes
|
|
118
|
-
if (kind === 'barcode') {
|
|
119
|
-
const digits = code.replace(/\D/g, '');
|
|
120
|
-
if (digits.length < 44 || digits.length > 48) {
|
|
121
|
-
return new Response(JSON.stringify({ ok: true, kind: 'none' }), {
|
|
122
|
-
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
return new Response(JSON.stringify({ ok: true, kind, code: digits, amount, due_date }), {
|
|
126
|
-
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return new Response(JSON.stringify({ ok: true, kind, code, amount, due_date }), {
|
|
131
|
-
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
132
|
-
});
|
|
133
|
-
} catch (e: any) {
|
|
134
|
-
return new Response(JSON.stringify({ ok: false, error: e.message }), {
|
|
135
|
-
status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
});
|