chore: import upstream snapshot with attribution
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:20:55 +08:00
commit d25d482dc2
13754 changed files with 4996608 additions and 0 deletions
+226
View File
@@ -0,0 +1,226 @@
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import type {
TextractAnalyzeExpenseOutput,
TextractAnalyzeExpenseV2Input,
} from '@/tools/textract/types'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('TextractAnalyzeExpenseTool')
/** Shared shape for AnalyzeExpense fields — used by both summaryFields and lineItemExpenseFields. */
const expenseFieldOutputProperties = {
type: {
type: 'object',
description: 'Normalized field label (e.g., VENDOR_NAME, TOTAL, ITEM, QUANTITY, PRICE)',
properties: {
text: { type: 'string', description: 'Field label text' },
confidence: { type: 'number', description: 'Confidence score (0-100)' },
},
},
valueDetection: {
type: 'object',
description: 'Detected value for the field',
properties: {
text: { type: 'string', description: 'Field value text' },
confidence: { type: 'number', description: 'Confidence score (0-100)' },
},
},
labelDetection: {
type: 'object',
description: 'The printed label detected next to the value, if any',
optional: true,
properties: {
text: { type: 'string', description: 'Label text' },
confidence: { type: 'number', description: 'Confidence score (0-100)' },
},
},
pageNumber: { type: 'number', description: 'Page number the field was found on', optional: true },
currency: {
type: 'object',
description: 'Currency of a monetary value, if detected',
optional: true,
properties: {
code: { type: 'string', description: 'ISO currency code (e.g., USD)' },
confidence: { type: 'number', description: 'Confidence score (0-100)' },
},
},
groupProperties: {
type: 'array',
description: 'Grouping metadata (e.g., distinguishes vendor vs. recipient address lines)',
optional: true,
items: {
type: 'object',
properties: {
id: { type: 'string', description: 'Group identifier' },
types: { type: 'array', description: 'Group type tags', items: { type: 'string' } },
},
},
},
} as const
export const textractAnalyzeExpenseTool: ToolConfig<
TextractAnalyzeExpenseV2Input,
TextractAnalyzeExpenseOutput
> = {
id: 'textract_analyze_expense',
name: 'AWS Textract Analyze Expense',
description: 'Extract structured invoice and receipt fields using AWS Textract AnalyzeExpense',
version: '1.0.0',
params: {
accessKeyId: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS Access Key ID',
},
secretAccessKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS Secret Access Key',
},
region: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS region for Textract service (e.g., us-east-1)',
},
processingMode: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'Document type: single-page or multi-page. Defaults to single-page.',
},
file: {
type: 'file',
required: false,
visibility: 'hidden',
description: 'Invoice or receipt to be processed (JPEG, PNG, or single-page PDF).',
},
filePath: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'URL to an invoice or receipt to be processed, if not uploaded directly.',
},
s3Uri: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'S3 URI for multi-page processing (s3://bucket/key).',
},
},
request: {
url: '/api/tools/textract/analyze-expense',
method: 'POST',
headers: () => ({
'Content-Type': 'application/json',
Accept: 'application/json',
}),
body: (params) => {
const processingMode = params.processingMode || 'sync'
const requestBody: Record<string, unknown> = {
accessKeyId: params.accessKeyId?.trim(),
secretAccessKey: params.secretAccessKey?.trim(),
region: params.region?.trim(),
processingMode,
}
if (processingMode === 'async') {
requestBody.s3Uri = params.s3Uri?.trim()
} else if (params.file && typeof params.file === 'object') {
requestBody.file = params.file
} else if (params.filePath && params.filePath.trim() !== '') {
requestBody.filePath = params.filePath.trim()
} else {
throw new Error('Document is required for single-page processing')
}
return requestBody
},
},
transformResponse: async (response) => {
try {
const apiResult = await response.json()
if (!apiResult || typeof apiResult !== 'object') {
throw new Error('Invalid response format from Textract API')
}
if (!apiResult.success) {
throw new Error(apiResult.error || 'Request failed')
}
const data = apiResult.output ?? apiResult
return {
success: true,
output: {
expenseDocuments: data.expenseDocuments ?? [],
documentMetadata: { pages: data.documentMetadata?.pages ?? 0 },
modelVersion: data.modelVersion ?? undefined,
},
}
} catch (error) {
logger.error('Error processing Textract AnalyzeExpense result:', toError(error))
throw error
}
},
outputs: {
expenseDocuments: {
type: 'array',
description: 'Detected expense documents with summary fields and line items',
items: {
type: 'object',
properties: {
expenseIndex: { type: 'number', description: 'Index of the expense document' },
summaryFields: {
type: 'array',
description: 'Header fields such as vendor name, invoice date, and totals',
items: { type: 'object', properties: expenseFieldOutputProperties },
},
lineItemGroups: {
type: 'array',
description: 'Groups of line items (e.g., purchased items and their prices)',
items: {
type: 'object',
properties: {
lineItemGroupIndex: { type: 'number', description: 'Index of the line item group' },
lineItems: {
type: 'array',
description: 'Individual line items within the group',
items: {
type: 'object',
properties: {
lineItemExpenseFields: {
type: 'array',
description: 'Fields for a single line item (description, quantity, price)',
items: { type: 'object', properties: expenseFieldOutputProperties },
},
},
},
},
},
},
},
},
},
},
documentMetadata: {
type: 'object',
description: 'Metadata about the analyzed document',
properties: {
pages: { type: 'number', description: 'Number of pages in the document' },
},
},
modelVersion: {
type: 'string',
description: 'Version of the AnalyzeExpense model used (multi-page/async only)',
optional: true,
},
},
}
+169
View File
@@ -0,0 +1,169 @@
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import type { TextractAnalyzeIdOutput, TextractAnalyzeIdV2Input } from '@/tools/textract/types'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('TextractAnalyzeIdTool')
export const textractAnalyzeIdTool: ToolConfig<TextractAnalyzeIdV2Input, TextractAnalyzeIdOutput> =
{
id: 'textract_analyze_id',
name: 'AWS Textract Analyze ID',
description: 'Extract identity document fields using AWS Textract AnalyzeID',
version: '1.0.0',
params: {
accessKeyId: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS Access Key ID',
},
secretAccessKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS Secret Access Key',
},
region: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS region for Textract service (e.g., us-east-1)',
},
file: {
type: 'file',
required: false,
visibility: 'hidden',
description: 'Front of the identity document (JPEG, PNG, or PDF).',
},
filePath: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'URL to the front of the identity document, if not uploaded directly.',
},
fileBack: {
type: 'file',
required: false,
visibility: 'hidden',
description: 'Back of the identity document, if applicable (JPEG, PNG, or PDF).',
},
filePathBack: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'URL to the back of the identity document, if not uploaded directly.',
},
},
request: {
url: '/api/tools/textract/analyze-id',
method: 'POST',
headers: () => ({
'Content-Type': 'application/json',
Accept: 'application/json',
}),
body: (params) => {
const requestBody: Record<string, unknown> = {
accessKeyId: params.accessKeyId?.trim(),
secretAccessKey: params.secretAccessKey?.trim(),
region: params.region?.trim(),
}
if (params.file && typeof params.file === 'object') {
requestBody.file = params.file
} else if (params.filePath && params.filePath.trim() !== '') {
requestBody.filePath = params.filePath.trim()
} else {
throw new Error('Identity document is required')
}
if (params.fileBack && typeof params.fileBack === 'object') {
requestBody.fileBack = params.fileBack
} else if (params.filePathBack && params.filePathBack.trim() !== '') {
requestBody.filePathBack = params.filePathBack.trim()
}
return requestBody
},
},
transformResponse: async (response) => {
try {
const apiResult = await response.json()
if (!apiResult || typeof apiResult !== 'object') {
throw new Error('Invalid response format from Textract API')
}
if (!apiResult.success) {
throw new Error(apiResult.error || 'Request failed')
}
const data = apiResult.output ?? apiResult
return {
success: true,
output: {
identityDocuments: data.identityDocuments ?? [],
documentMetadata: { pages: data.documentMetadata?.pages ?? 0 },
modelVersion: data.modelVersion ?? undefined,
},
}
} catch (error) {
logger.error('Error processing Textract AnalyzeID result:', toError(error))
throw error
}
},
outputs: {
identityDocuments: {
type: 'array',
description: 'Detected identity documents with normalized fields',
items: {
type: 'object',
properties: {
documentIndex: { type: 'number', description: 'Index of the document page set' },
identityDocumentFields: {
type: 'array',
description:
'Normalized fields such as FIRST_NAME, LAST_NAME, DATE_OF_BIRTH, DOCUMENT_NUMBER, EXPIRATION_DATE',
items: {
type: 'object',
properties: {
type: {
type: 'object',
description: 'Normalized field label',
properties: {
text: { type: 'string', description: 'Field label text' },
confidence: { type: 'number', description: 'Confidence score (0-100)' },
},
},
valueDetection: {
type: 'object',
description: 'Detected value for the field, with a normalized value for dates',
properties: {
text: { type: 'string', description: 'Field value text' },
confidence: { type: 'number', description: 'Confidence score (0-100)' },
},
},
},
},
},
},
},
},
documentMetadata: {
type: 'object',
description: 'Metadata about the analyzed document',
properties: {
pages: { type: 'number', description: 'Number of pages analyzed' },
},
},
modelVersion: {
type: 'string',
description: 'Version of the AnalyzeID model used for processing',
optional: true,
},
},
}
+4
View File
@@ -0,0 +1,4 @@
export { textractAnalyzeExpenseTool } from '@/tools/textract/analyze-expense'
export { textractAnalyzeIdTool } from '@/tools/textract/analyze-id'
export { textractParserTool, textractParserV2Tool } from '@/tools/textract/parser'
export * from '@/tools/textract/types'
+347
View File
@@ -0,0 +1,347 @@
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import type {
TextractParserInput,
TextractParserOutput,
TextractParserV2Input,
} from '@/tools/textract/types'
import type { ToolConfig } from '@/tools/types'
const logger = createLogger('TextractParserTool')
export const textractParserTool: ToolConfig<TextractParserInput, TextractParserOutput> = {
id: 'textract_parser',
name: 'AWS Textract Parser',
description: 'Parse documents using AWS Textract OCR and document analysis',
version: '1.0.0',
params: {
accessKeyId: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS Access Key ID',
},
secretAccessKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS Secret Access Key',
},
region: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'AWS region for Textract service (e.g., us-east-1)',
},
processingMode: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'Document type: single-page or multi-page. Defaults to single-page.',
},
filePath: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'URL to a document to be processed (JPEG, PNG, or single-page PDF).',
},
file: {
type: 'file',
required: false,
visibility: 'hidden',
description: 'Document file to be processed (JPEG, PNG, or single-page PDF).',
},
s3Uri: {
type: 'string',
required: false,
visibility: 'user-only',
description: 'S3 URI for multi-page processing (s3://bucket/key).',
},
featureTypes: {
type: 'array',
required: false,
visibility: 'user-or-llm',
description:
'Feature types to detect: TABLES, FORMS, QUERIES, SIGNATURES, LAYOUT. If not specified, only text detection is performed.',
items: {
type: 'string',
description: 'Feature type',
},
},
queries: {
type: 'array',
required: false,
visibility: 'user-or-llm',
description:
'Custom queries to extract specific information. Only used when featureTypes includes QUERIES.',
items: {
type: 'object',
description: 'Query configuration',
properties: {
Text: { type: 'string', description: 'The query text' },
Alias: { type: 'string', description: 'Optional alias for the result' },
},
},
},
},
request: {
url: '/api/tools/textract/parse',
method: 'POST',
headers: () => {
return {
'Content-Type': 'application/json',
Accept: 'application/json',
}
},
body: (params) => {
const processingMode = params.processingMode || 'sync'
const requestBody: Record<string, unknown> = {
accessKeyId: params.accessKeyId?.trim(),
secretAccessKey: params.secretAccessKey?.trim(),
region: params.region?.trim(),
processingMode,
}
if (processingMode === 'async') {
requestBody.s3Uri = params.s3Uri?.trim()
} else {
const fileInput =
params.file && typeof params.file === 'object' ? params.file : params.fileUpload
const hasFileUpload = fileInput && typeof fileInput === 'object'
const hasFilePath = typeof params.filePath === 'string' && params.filePath.trim() !== ''
if (hasFilePath) {
requestBody.filePath = params.filePath?.trim()
} else if (hasFileUpload) {
requestBody.file = fileInput
} else {
throw new Error('Document is required for single-page processing')
}
}
if (params.featureTypes && Array.isArray(params.featureTypes)) {
requestBody.featureTypes = params.featureTypes
}
if (params.queries && Array.isArray(params.queries)) {
requestBody.queries = params.queries
}
return requestBody
},
},
transformResponse: async (response) => {
try {
let apiResult
try {
apiResult = await response.json()
} catch (jsonError) {
throw new Error(`Failed to parse Textract response: ${toError(jsonError).message}`)
}
if (!apiResult || typeof apiResult !== 'object') {
throw new Error('Invalid response format from Textract API')
}
if (!apiResult.success) {
throw new Error(apiResult.error || 'Request failed')
}
const textractData = apiResult.output ?? apiResult
return {
success: true,
output: {
blocks: textractData.Blocks ?? textractData.blocks ?? [],
documentMetadata: {
pages:
textractData.DocumentMetadata?.Pages ?? textractData.documentMetadata?.pages ?? 0,
},
modelVersion:
textractData.modelVersion ??
textractData.AnalyzeDocumentModelVersion ??
textractData.analyzeDocumentModelVersion ??
textractData.DetectDocumentTextModelVersion ??
textractData.detectDocumentTextModelVersion ??
undefined,
},
}
} catch (error) {
logger.error('Error processing Textract result:', error)
throw error
}
},
outputs: {
blocks: {
type: 'array',
description:
'Array of Block objects containing detected text, tables, forms, and other elements',
items: {
type: 'object',
properties: {
BlockType: {
type: 'string',
description: 'Type of block (PAGE, LINE, WORD, TABLE, CELL, KEY_VALUE_SET, etc.)',
},
Id: { type: 'string', description: 'Unique identifier for the block' },
Text: {
type: 'string',
description: 'The text content (for LINE and WORD blocks)',
optional: true,
},
TextType: {
type: 'string',
description: 'Type of text (PRINTED or HANDWRITING)',
optional: true,
},
Confidence: { type: 'number', description: 'Confidence score (0-100)', optional: true },
Page: { type: 'number', description: 'Page number', optional: true },
Geometry: {
type: 'object',
description: 'Location and bounding box information',
optional: true,
properties: {
BoundingBox: {
type: 'object',
properties: {
Height: { type: 'number', description: 'Height as ratio of document height' },
Left: { type: 'number', description: 'Left position as ratio of document width' },
Top: { type: 'number', description: 'Top position as ratio of document height' },
Width: { type: 'number', description: 'Width as ratio of document width' },
},
},
Polygon: {
type: 'array',
description: 'Polygon coordinates',
items: {
type: 'object',
properties: {
X: { type: 'number', description: 'X coordinate' },
Y: { type: 'number', description: 'Y coordinate' },
},
},
},
},
},
Relationships: {
type: 'array',
description: 'Relationships to other blocks',
optional: true,
items: {
type: 'object',
properties: {
Type: {
type: 'string',
description: 'Relationship type (CHILD, VALUE, ANSWER, etc.)',
},
Ids: { type: 'array', description: 'IDs of related blocks' },
},
},
},
EntityTypes: {
type: 'array',
description: 'Entity types for KEY_VALUE_SET (KEY or VALUE)',
optional: true,
},
SelectionStatus: {
type: 'string',
description: 'For checkboxes: SELECTED or NOT_SELECTED',
optional: true,
},
RowIndex: { type: 'number', description: 'Row index for table cells', optional: true },
ColumnIndex: {
type: 'number',
description: 'Column index for table cells',
optional: true,
},
RowSpan: { type: 'number', description: 'Row span for merged cells', optional: true },
ColumnSpan: {
type: 'number',
description: 'Column span for merged cells',
optional: true,
},
Query: {
type: 'object',
description: 'Query information for QUERY blocks',
optional: true,
properties: {
Text: { type: 'string', description: 'Query text' },
Alias: { type: 'string', description: 'Query alias', optional: true },
Pages: { type: 'array', description: 'Pages to search', optional: true },
},
},
},
},
},
documentMetadata: {
type: 'object',
description: 'Metadata about the analyzed document',
properties: {
pages: { type: 'number', description: 'Number of pages in the document' },
},
},
modelVersion: {
type: 'string',
description: 'Version of the Textract model used for processing',
optional: true,
},
},
}
export const textractParserV2Tool: ToolConfig<TextractParserV2Input, TextractParserOutput> = {
...textractParserTool,
id: 'textract_parser_v2',
name: 'AWS Textract Parser',
params: {
accessKeyId: textractParserTool.params.accessKeyId,
secretAccessKey: textractParserTool.params.secretAccessKey,
region: textractParserTool.params.region,
processingMode: textractParserTool.params.processingMode,
file: {
type: 'file',
required: false,
visibility: 'hidden',
description: 'Document to be processed (JPEG, PNG, or single-page PDF).',
},
s3Uri: textractParserTool.params.s3Uri,
featureTypes: textractParserTool.params.featureTypes,
queries: textractParserTool.params.queries,
},
request: {
...textractParserTool.request,
body: (params: TextractParserV2Input) => {
const processingMode = params.processingMode || 'sync'
const requestBody: Record<string, unknown> = {
accessKeyId: params.accessKeyId?.trim(),
secretAccessKey: params.secretAccessKey?.trim(),
region: params.region?.trim(),
processingMode,
}
if (processingMode === 'async') {
requestBody.s3Uri = params.s3Uri?.trim()
} else {
if (!params.file || typeof params.file !== 'object') {
throw new Error('Document file is required for single-page processing')
}
requestBody.file = params.file
}
if (params.featureTypes && Array.isArray(params.featureTypes)) {
requestBody.featureTypes = params.featureTypes
}
if (params.queries && Array.isArray(params.queries)) {
requestBody.queries = params.queries
}
return requestBody
},
},
}
+239
View File
@@ -0,0 +1,239 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { textractAnalyzeExpenseTool } from '@/tools/textract/analyze-expense'
import { textractAnalyzeIdTool } from '@/tools/textract/analyze-id'
import { textractParserTool, textractParserV2Tool } from '@/tools/textract/parser'
const respond = (body: unknown) => new Response(JSON.stringify(body))
describe('textract_parser', () => {
const body = textractParserTool.request.body!
it('builds a sync body from filePath', () => {
expect(
body({
accessKeyId: ' key ',
secretAccessKey: ' secret ',
region: ' us-east-1 ',
filePath: ' https://example.com/doc.pdf ',
featureTypes: ['TABLES'],
} as never)
).toMatchObject({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
processingMode: 'sync',
filePath: 'https://example.com/doc.pdf',
featureTypes: ['TABLES'],
})
})
it('requires s3Uri for async mode', () => {
expect(() =>
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
processingMode: 'async',
} as never)
).not.toThrow()
expect(
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
processingMode: 'async',
s3Uri: 's3://bucket/key.pdf',
} as never)
).toMatchObject({ processingMode: 'async', s3Uri: 's3://bucket/key.pdf' })
})
it('throws when no document is provided for sync mode', () => {
expect(() =>
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
} as never)
).toThrow('Document is required for single-page processing')
})
it('normalizes the documented response shape', async () => {
const result = await textractParserTool.transformResponse!(
respond({
success: true,
output: {
blocks: [{ BlockType: 'LINE', Id: '1', Text: 'Hello' }],
documentMetadata: { pages: 2 },
modelVersion: '1.0',
},
})
)
expect(result.success).toBe(true)
expect(result.output.blocks).toHaveLength(1)
expect(result.output.documentMetadata.pages).toBe(2)
expect(result.output.modelVersion).toBe('1.0')
})
it('surfaces the API error message on failure', async () => {
await expect(
textractParserTool.transformResponse!(respond({ success: false, error: 'Bad request' }))
).rejects.toThrow('Bad request')
})
})
describe('textract_parser_v2', () => {
const body = textractParserV2Tool.request.body!
it('throws when no file is provided for sync mode', () => {
expect(() =>
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
} as never)
).toThrow('Document file is required for single-page processing')
})
it('builds a sync body from a UserFile', () => {
const file = { key: 'file-key', name: 'doc.pdf' } as never
expect(
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
file,
} as never)
).toMatchObject({ processingMode: 'sync', file })
})
})
describe('textract_analyze_expense', () => {
const body = textractAnalyzeExpenseTool.request.body!
it('throws when neither file nor filePath is provided for sync mode', () => {
expect(() =>
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
} as never)
).toThrow('Document is required for single-page processing')
})
it('falls back to filePath when no file object is provided', () => {
expect(
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
filePath: ' https://example.com/receipt.pdf ',
} as never)
).toMatchObject({ processingMode: 'sync', filePath: 'https://example.com/receipt.pdf' })
})
it('builds an async body requiring s3Uri', () => {
expect(
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
processingMode: 'async',
s3Uri: 's3://bucket/receipt.pdf',
} as never)
).toMatchObject({ processingMode: 'async', s3Uri: 's3://bucket/receipt.pdf' })
})
it('normalizes expense documents from the response', async () => {
const result = await textractAnalyzeExpenseTool.transformResponse!(
respond({
success: true,
output: {
expenseDocuments: [{ expenseIndex: 0, summaryFields: [], lineItemGroups: [] }],
documentMetadata: { pages: 1 },
},
})
)
expect(result.success).toBe(true)
expect(result.output.expenseDocuments).toHaveLength(1)
expect(result.output.documentMetadata.pages).toBe(1)
})
})
describe('textract_analyze_id', () => {
const body = textractAnalyzeIdTool.request.body!
it('throws when no front-of-ID file is provided', () => {
expect(() =>
body({ accessKeyId: 'key', secretAccessKey: 'secret', region: 'us-east-1' } as never)
).toThrow('Identity document is required')
})
it('includes fileBack only when provided', () => {
const file = { key: 'front-key' } as never
expect(
body({ accessKeyId: 'key', secretAccessKey: 'secret', region: 'us-east-1', file } as never)
).toEqual({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
file,
})
const fileBack = { key: 'back-key' } as never
expect(
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
file,
fileBack,
} as never)
).toEqual({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
file,
fileBack,
})
})
it('falls back to filePath/filePathBack when no file objects are provided', () => {
expect(
body({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
filePath: ' https://example.com/id-front.png ',
filePathBack: ' https://example.com/id-back.png ',
} as never)
).toEqual({
accessKeyId: 'key',
secretAccessKey: 'secret',
region: 'us-east-1',
filePath: 'https://example.com/id-front.png',
filePathBack: 'https://example.com/id-back.png',
})
})
it('normalizes identity documents from the response', async () => {
const result = await textractAnalyzeIdTool.transformResponse!(
respond({
success: true,
output: {
identityDocuments: [{ documentIndex: 0, identityDocumentFields: [] }],
documentMetadata: { pages: 1 },
modelVersion: '1.0',
},
})
)
expect(result.success).toBe(true)
expect(result.output.identityDocuments).toHaveLength(1)
expect(result.output.modelVersion).toBe('1.0')
})
})
+202
View File
@@ -0,0 +1,202 @@
import type { RawFileInput } from '@/lib/uploads/utils/file-utils'
import type { UserFile } from '@/executor/types'
import type { ToolResponse } from '@/tools/types'
export type TextractProcessingMode = 'sync' | 'async'
export interface TextractParserInput {
accessKeyId: string
secretAccessKey: string
region: string
processingMode?: TextractProcessingMode
filePath?: string
file?: RawFileInput
s3Uri?: string
fileUpload?: RawFileInput
featureTypes?: TextractFeatureType[]
queries?: TextractQuery[]
}
export interface TextractParserV2Input {
accessKeyId: string
secretAccessKey: string
region: string
processingMode?: TextractProcessingMode
file?: UserFile
s3Uri?: string
featureTypes?: TextractFeatureType[]
queries?: TextractQuery[]
}
export type TextractFeatureType = 'TABLES' | 'FORMS' | 'QUERIES' | 'SIGNATURES' | 'LAYOUT'
interface TextractQuery {
Text: string
Alias?: string
Pages?: string[]
}
interface TextractBoundingBox {
Height: number
Left: number
Top: number
Width: number
}
interface TextractPolygonPoint {
X: number
Y: number
}
interface TextractGeometry {
BoundingBox: TextractBoundingBox
Polygon: TextractPolygonPoint[]
RotationAngle?: number
}
interface TextractRelationship {
Type: string
Ids: string[]
}
interface TextractBlock {
BlockType: string
Id: string
Text?: string
TextType?: string
Confidence?: number
Geometry?: TextractGeometry
Relationships?: TextractRelationship[]
Page?: number
EntityTypes?: string[]
SelectionStatus?: string
RowIndex?: number
ColumnIndex?: number
RowSpan?: number
ColumnSpan?: number
Query?: {
Text: string
Alias?: string
Pages?: string[]
}
}
interface TextractDocumentMetadata {
pages: number
}
interface TextractNormalizedOutput {
blocks: TextractBlock[]
documentMetadata: TextractDocumentMetadata
modelVersion?: string
}
export interface TextractParserOutput extends ToolResponse {
output: TextractNormalizedOutput
}
export interface TextractAnalyzeExpenseInput {
accessKeyId: string
secretAccessKey: string
region: string
processingMode?: TextractProcessingMode
filePath?: string
file?: RawFileInput
s3Uri?: string
}
export interface TextractAnalyzeExpenseV2Input {
accessKeyId: string
secretAccessKey: string
region: string
processingMode?: TextractProcessingMode
file?: UserFile
filePath?: string
s3Uri?: string
}
interface TextractCurrency {
code?: string
confidence?: number
}
interface TextractExpenseFieldValue {
text?: string
confidence?: number
}
interface TextractExpenseField {
type?: TextractExpenseFieldValue
valueDetection?: TextractExpenseFieldValue
labelDetection?: TextractExpenseFieldValue
pageNumber?: number
currency?: TextractCurrency
groupProperties?: { id: string; types: string[] }[]
}
interface TextractLineItem {
lineItemExpenseFields: TextractExpenseField[]
}
interface TextractLineItemGroup {
lineItemGroupIndex?: number
lineItems: TextractLineItem[]
}
interface TextractExpenseDocument {
expenseIndex?: number
summaryFields: TextractExpenseField[]
lineItemGroups: TextractLineItemGroup[]
}
export interface TextractAnalyzeExpenseOutput extends ToolResponse {
output: {
expenseDocuments: TextractExpenseDocument[]
documentMetadata: TextractDocumentMetadata
modelVersion?: string
}
}
export interface TextractAnalyzeIdInput {
accessKeyId: string
secretAccessKey: string
region: string
filePath?: string
file?: RawFileInput
filePathBack?: string
fileBack?: RawFileInput
}
export interface TextractAnalyzeIdV2Input {
accessKeyId: string
secretAccessKey: string
region: string
file?: UserFile
filePath?: string
fileBack?: UserFile
filePathBack?: string
}
interface TextractIdFieldValue {
text?: string
confidence?: number
normalizedValue?: { value?: string; valueType?: string }
}
interface TextractIdentityDocumentField {
type?: TextractIdFieldValue
valueDetection?: TextractIdFieldValue
}
interface TextractIdentityDocument {
documentIndex?: number
identityDocumentFields: TextractIdentityDocumentField[]
}
export interface TextractAnalyzeIdOutput extends ToolResponse {
output: {
identityDocuments: TextractIdentityDocument[]
documentMetadata: TextractDocumentMetadata
modelVersion?: string
}
}