Invoice Scan
⚠️ Privacy Notice
CLI mode sends base64-encoded invoice images to Anthropic's API (api.anthropic.com). Invoice data (supplier/buyer names, addresses, IBANs, bank details, amounts) will be transmitted to a third-party service. Confirm this is acceptable under your privacy and compliance requirements before use. Consider dedicated API credentials with usage limits.
Agent-native mode does NOT send data externally — the agent uses its own built-in vision. Only formatOutput() is called locally for CSV/Excel export.
Setup
Install dependencies (required before first use):
CODEBLOCK0
Dependencies: sharp (image processing), xlsx (Excel export). Review scripts/package.json before installing.
CLI mode requires: ANTHROPIC_API_KEY environment variable.
Agent-native mode requires: nothing — uses agent's built-in vision capability.
Two Modes
Mode 1: Agent-Native (No API Key, No External Calls)
Use your built-in vision to look at the invoice image directly. Do NOT call scanInvoice() — that function requires an API key and sends data externally. Instead:
- 1. Look at the image with your vision capability
- Extract all fields into a JSON object matching the canonical schema in INLINECODE7
- Classify document type: invoice, credit-note, receipt, purchase-order, delivery-note, confirmation, statement, other-financial, not-financial
- Validate arithmetic and business rules per INLINECODE8
- Present results (see Output Format below)
- For CSV/Excel export, construct the canonical JSON object and pass it to
formatOutput() only:
CODEBLOCK1
Key: formatOutput() is purely local — no network calls, no API key needed. It just formats your extracted data into CSV or Excel.
Mode 2: CLI Standalone (Needs API Key)
For automation or pipelines. Requires ANTHROPIC_API_KEY env var.
CODEBLOCK2
Options: --provider claude, --accept strict|relaxed|any, --no-preprocess, INLINECODE15
Agent-Native Extraction Checklist
Extract ALL of:
Header: invoiceNumber, invoiceDate (YYYY-MM-DD), dueDate, currency (ISO 4217), supplierName, supplierAddress, supplierVatNumber, buyerName, buyerAddress, buyerVatNumber, paymentTerms, paymentReference, bankDetails (iban, bic, accountNumber, sortCode)
Line items: description, quantity, unitOfMeasure, unitPrice, lineTotal, vatRate, sku, discount
References: PO, contract, GRN, timesheet, project, proforma, invoice (original invoice if this is a credit/debit memo), credit-note, debit-note refs. For credit/debit memos, ALWAYS include the original invoice reference.
Totals: netTotal, vatBreakdown (rate + amount + type per band — type is the tax regime label e.g. "CGST", "SGST", "USt", "НДС", "IVA"), vatTotal, grossTotal, amountPaid, amountDue, discount (invoice-level discount amount), discountRate (percentage)
Metadata: paidDate (YYYY-MM-DD — date from PAID stamp), vatInclusive (true if line totals include VAT, false if net, null if unknown)
Charges: Surcharges/fees outside line items — shipping, postage, P&P, delivery, freight, carriage, dispatch, handling, insurance, eco-levy, surcharges. Each: type (shipping|handling|insurance|surcharge|discount|other), label (original text from document), amount, vatRate, vatAmount. Do NOT duplicate items already captured as line items.
Document type: documentType (invoice, credit-note, debit-note, receipt, purchase-order, delivery-note, confirmation, statement, other-financial, not-financial)
Additional: handwritten notes, stamps/seals (type + text), remarks/comments, document language (ISO 639-1)
Arithmetic Validation
- 1. qty × unitPrice = lineTotal (per line)
- Sum of lineTotals = netTotal
- netTotal + vatTotal = grossTotal
(Tolerance: ±0.02 for rounding)
Locale Numbers
Parse regional formats automatically: US/UK (1,234.56), European (1.234,56), French (1 234,56), Indian (1,23,456.78). Use currency/country context when ambiguous.
Quality Score
Count present from: invoiceNumber, invoiceDate, currency, supplierName, buyerName, supplierVatNumber, netTotal, vatTotal, grossTotal. Score = present / 9. good ≥ 0.8, partial ≥ 0.5, poor < 0.5.
Output Format
CODEBLOCK3
List warnings/flags, then offer: "Want JSON, CSV, or Excel?"
File Delivery
- - Output directory:
{WORKSPACE}/invoice-scan/output/ (create if needed) - Naming:
{supplierName}_{invoiceNumber}_{invoiceDate}.{ext} (replace spaces/slashes with hyphens) - Always save JSON automatically, offer CSV/Excel on request
- Send file as chat attachment + confirm path
References
- - Full schema: INLINECODE18
- Validation rules: INLINECODE19
发票扫描
⚠️ 隐私声明
CLI模式会将Base64编码的发票图像发送至Anthropic的API(api.anthropic.com)。发票数据(供应商/买方名称、地址、IBAN、银行信息、金额)将被传输至第三方服务。使用前请确认此操作符合您的隐私和合规要求。建议使用带有使用限制的专用API凭证。
智能体原生模式不会将数据发送至外部——智能体使用其内置视觉能力。仅本地调用formatOutput()进行CSV/Excel导出。
环境配置
安装依赖项(首次使用前必需):
bash
cd {SKILL_DIR}/scripts && npm install --production
依赖项:sharp(图像处理)、xlsx(Excel导出)。安装前请查看scripts/package.json。
CLI模式需要: ANTHROPICAPIKEY环境变量。
智能体原生模式需要: 无需任何配置——使用智能体内置视觉能力。
两种模式
模式1:智能体原生(无需API密钥,无外部调用)
使用内置视觉能力直接查看发票图像。请勿调用scanInvoice()——该函数需要API密钥并将数据发送至外部。请按以下步骤操作:
- 1. 使用视觉能力查看图像
- 将所有字段提取为JSON对象,匹配references/canonical-schema.md中的标准模式
- 分类文档类型:发票、贷项通知单、收据、采购订单、交货单、确认书、对账单、其他财务文件、非财务文件
- 根据references/validation-rules.md验证算术和业务规则
- 呈现结果(参见下方输出格式)
- 如需CSV/Excel导出,构建标准JSON对象并仅传递给formatOutput():
javascript
const { formatOutput } = require({SKILL_DIR}/scripts);
// invoiceData = 您通过视觉提取构建的JSON对象
// 重要提示:包含canonical-schema.md中的所有字段,包括charges[]
// 例如:invoiceData.charges = [{ type: shipping, label: P&P, amount: 5.99, vatRate: 20, vatAmount: 1.20 }]
const csv = formatOutput(invoiceData, csv); // 字符串 — 仅本地
const xlsx = formatOutput(invoiceData, excel); // Buffer — 仅本地
关键点: formatOutput()是纯本地操作——无需网络调用,无需API密钥。仅将您提取的数据格式化为CSV或Excel。
模式2:CLI独立模式(需要API密钥)
适用于自动化或流水线。需要ANTHROPICAPIKEY环境变量。
bash
ANTHROPICAPIKEY= node {SKILL_DIR}/scripts/cli.js scan [--format json|csv|excel] [--output result.json]
选项:--provider claude、--accept strict|relaxed|any、--no-preprocess、--model
智能体原生提取清单
提取所有以下内容:
头部信息: invoiceNumber、invoiceDate(YYYY-MM-DD格式)、dueDate、currency(ISO 4217标准)、supplierName、supplierAddress、supplierVatNumber、buyerName、buyerAddress、buyerVatNumber、paymentTerms、paymentReference、bankDetails(iban、bic、accountNumber、sortCode)
行项目: description、quantity、unitOfMeasure、unitPrice、lineTotal、vatRate、sku、discount
参考信息: PO、contract、GRN、timesheet、project、proforma、invoice(如果此为贷项/借项通知单,则为原始发票)、贷项通知单、借项通知单参考。对于贷项/借项通知单,始终包含原始发票参考。
总计: netTotal、vatBreakdown(每个税率的rate + amount + type——type为税制标签,例如CGST、SGST、USt、НДС、IVA)、vatTotal、grossTotal、amountPaid、amountDue、discount(发票级别折扣金额)、discountRate(百分比)
元数据: paidDate(YYYY-MM-DD格式——来自已付款印章的日期)、vatInclusive(如果行总计包含增值税则为true,如果为净额则为false,如果未知则为null)
费用: 行项目之外的附加费/费用——运费、邮费、包装费、配送费、货运费、运输费、发货费、处理费、保险费、生态税、附加费。每项:type(shipping|handling|insurance|surcharge|discount|other)、label(文档中的原始文本)、amount、vatRate、vatAmount。请勿重复已作为行项目捕获的项目。
文档类型: documentType(invoice、credit-note、debit-note、receipt、purchase-order、delivery-note、confirmation、statement、other-financial、not-financial)
附加信息: 手写备注、印章/戳记(type + text)、备注/评论、文档语言(ISO 639-1标准)
算术验证
- 1. 数量 × 单价 = 行总计(每行)
- 行总计之和 = 净总计
- 净总计 + 增值税总计 = 毛总计
(容差:±0.02用于四舍五入)
区域数字格式
自动解析区域格式:美式/英式(1,234.56)、欧式(1.234,56)、法式(1 234,56)、印式(1,23,456.78)。在模棱两可时使用货币/国家上下文。
质量评分
统计以下字段的存在情况:invoiceNumber、invoiceDate、currency、supplierName、buyerName、supplierVatNumber、netTotal、vatTotal、grossTotal。评分 = 存在数 / 9。良好 ≥ 0.8,部分 ≥ 0.5,较差 < 0.5。
输出格式
📄 发票 #{number} | {date}
供应商:{name} → 买方:{name}
净额:{currency}{net} | 增值税:{currency}{vat} | 毛额:{currency}{gross}
[如果存在费用] 📦 费用:{label} {currency}{amount} [每项费用]
[如果amountDue不为空] 应付金额:{currency}{amountDue} [如果amountPaid](已付:{currency}{amountPaid})
项目数:{count} | 算术验证:✅/❌ | 质量:{rating}({score}/9)
列出警告/标记,然后询问:需要JSON、CSV还是Excel?
文件交付
- - 输出目录:{WORKSPACE}/invoice-scan/output/(如不存在则创建)
- 命名规则:{supplierName}{invoiceNumber}{invoiceDate}.{ext}(将空格/斜杠替换为连字符)
- 始终自动保存JSON,按需提供CSV/Excel
- 以聊天附件形式发送文件并确认路径
参考资料
- - 完整模式:references/canonical-schema.md
- 验证规则:references/validation-rules.md