@codemation/core-nodes-ocr 0.2.2 → 0.2.4
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/CHANGELOG.md +14 -0
- package/dist/{analyzeInvoiceNode-uVwe3GHD.js → analyzeInvoiceNode-BqZsN8iL.js} +4 -36
- package/dist/analyzeInvoiceNode-BqZsN8iL.js.map +1 -0
- package/dist/{analyzeInvoiceNode-BIw8j_Zb.cjs → analyzeInvoiceNode-CmMsifbw.cjs} +4 -36
- package/dist/analyzeInvoiceNode-CmMsifbw.cjs.map +1 -0
- package/dist/codemation.plugin.cjs +392 -56
- package/dist/codemation.plugin.cjs.map +1 -1
- package/dist/codemation.plugin.d.cts +1 -3
- package/dist/codemation.plugin.d.ts +1 -3
- package/dist/codemation.plugin.js +393 -57
- package/dist/codemation.plugin.js.map +1 -1
- package/dist/{index-C2KJPzqN.d.ts → index-DF2ht42F.d.ts} +409 -321
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/metadata.json +1 -1
- package/dist/{runtimeTypes-ffl603pJ.d.cts → runtimeTypes-WCvsnJMY.d.cts} +281 -193
- package/package.json +2 -2
- package/src/nodes/analyzeDocumentNode.ts +1 -2
- package/src/nodes/analyzeImageNode.ts +1 -2
- package/src/nodes/analyzeInvoiceNode.ts +1 -2
- package/dist/analyzeInvoiceNode-BIw8j_Zb.cjs.map +0 -1
- package/dist/analyzeInvoiceNode-uVwe3GHD.js.map +0 -1
- package/src/lib/readBinaryBody.ts +0 -51
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"analyzeInvoiceNode-BIw8j_Zb.cjs","names":["ContentUnderstandingClient","AzureKeyCredential","ContentUnderstandingClient","AzureKeyCredential","markdownParts: string[]","out: Record<string, unknown>"],"sources":["../src/credentials/azureContentUnderstandingCredential.ts","../src/lib/analyzeWithAzure.ts","../src/lib/readBinaryBody.ts","../src/nodes/analyzeDocumentNode.ts","../src/nodes/analyzeImageNode.ts","../src/nodes/analyzeInvoiceNode.ts"],"sourcesContent":["import { ContentUnderstandingClient } from \"@azure/ai-content-understanding\";\nimport { AzureKeyCredential } from \"@azure/core-auth\";\nimport { defineCredential } from \"@codemation/core\";\n\nexport type AzureContentUnderstandingSession = Readonly<{\n endpoint: string;\n apiKey: string;\n}>;\n\nfunction normalizeEndpoint(raw: string): string {\n return raw.trim().replace(/\\/+$/, \"\");\n}\n\nfunction buildSession(args: {\n readonly publicConfig: Readonly<Record<string, unknown>>;\n readonly material: Readonly<Record<string, unknown>>;\n}): AzureContentUnderstandingSession {\n const endpoint = normalizeEndpoint(String(args.publicConfig[\"endpoint\"] ?? \"\"));\n const apiKey = String(args.material[\"apiKey\"] ?? \"\").trim();\n if (!endpoint) {\n throw new Error(\"Azure Content Understanding credential is incomplete: endpoint is required.\");\n }\n if (!apiKey) {\n throw new Error(\"Azure Content Understanding credential is incomplete: API key is required.\");\n }\n return { endpoint, apiKey };\n}\n\nexport const azureContentUnderstandingCredentialType = defineCredential({\n key: \"azure.contentUnderstanding\",\n label: \"Azure Content Understanding\",\n description: \"Azure AI Content Understanding (endpoint + key) for prebuilt document, invoice, and image analyzers.\",\n public: {\n endpoint: {\n key: \"endpoint\",\n label: \"Endpoint\",\n type: \"string\" as const,\n required: true,\n placeholder: \"https://your-resource.cognitiveservices.azure.com/\",\n helpText: \"Content Understanding resource endpoint URL (no trailing slash).\",\n order: 0,\n },\n },\n secret: {\n apiKey: {\n key: \"apiKey\",\n label: \"API key\",\n type: \"password\" as const,\n required: true,\n order: 1,\n },\n },\n async createSession(args) {\n return buildSession(args);\n },\n async test(args) {\n try {\n const session = buildSession(args);\n const client = new ContentUnderstandingClient(session.endpoint, new AzureKeyCredential(session.apiKey));\n const iter = client.listAnalyzers();\n await iter.next();\n return {\n status: \"healthy\",\n message: \"Listed analyzers successfully.\",\n testedAt: new Date().toISOString(),\n };\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n return {\n status: \"failing\",\n message: message || \"Azure Content Understanding connection failed.\",\n testedAt: new Date().toISOString(),\n };\n }\n },\n});\n","import type {\n AnalysisResult,\n ArrayField,\n BooleanField,\n ContentField,\n ContentFieldUnion,\n DateField,\n IntegerField,\n JsonField,\n NumberField,\n ObjectField,\n StringField,\n TimeField,\n} from \"@azure/ai-content-understanding\";\nimport { ContentUnderstandingClient } from \"@azure/ai-content-understanding\";\nimport { AzureKeyCredential } from \"@azure/core-auth\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\n\n/** Structured analyzer fields: scalars at leaves; nested objects and arrays preserved. */\nexport type OcrStructuredFields = Readonly<Record<string, unknown>>;\n\n/** The output shape returned by all OCR analyzer nodes. */\nexport type OcrAnalysisOutput = Readonly<{\n /** Markdown representation of the document content. */\n content: string;\n /** Structured fields extracted by the prebuilt analyzer. */\n fields: OcrStructuredFields;\n}>;\n\n/**\n * Analyzes a binary document using an Azure Content Understanding prebuilt analyzer.\n * Retries on transient failures are handled by the engine via the node's `retryPolicy`.\n */\nexport async function analyzeWithAzure(\n args: Readonly<{\n session: AzureContentUnderstandingSession;\n analyzerId: string;\n body: Uint8Array;\n contentType: string;\n }>,\n): Promise<OcrAnalysisOutput> {\n const client = new ContentUnderstandingClient(args.session.endpoint, new AzureKeyCredential(args.session.apiKey));\n const poller = client.analyzeBinary(args.analyzerId, args.body, args.contentType);\n const result = await poller.pollUntilDone();\n return mapAnalysisResult(result);\n}\n\n/** @internal Exported for testing — maps a raw AnalysisResult to the node output shape. */\nexport function mapAnalysisResult(result: AnalysisResult): OcrAnalysisOutput {\n const contents = result.contents ?? [];\n const markdownParts: string[] = [];\n for (const c of contents) {\n if (typeof c.markdown === \"string\" && c.markdown.length > 0) {\n markdownParts.push(c.markdown);\n }\n }\n const content = markdownParts.join(\"\\n\\n\");\n if (contents.length === 0) {\n return { content: \"\", fields: {} };\n }\n if (contents.length === 1) {\n const c = contents[0]!;\n return {\n content,\n fields: c.fields ? fieldsToStructuredMap(c.fields) : {},\n };\n }\n return {\n content,\n fields: {\n segments: contents.map((c, index) => ({\n index,\n markdown: typeof c.markdown === \"string\" && c.markdown.trim().length > 0 ? c.markdown : undefined,\n fields: c.fields ? fieldsToStructuredMap(c.fields) : {},\n })),\n },\n };\n}\n\nfunction fieldsToStructuredMap(fields: Readonly<Record<string, ContentFieldUnion>>): OcrStructuredFields {\n const out: Record<string, unknown> = {};\n for (const [name, field] of Object.entries(fields)) {\n out[name] = fieldToStructuredValue(field);\n }\n return out;\n}\n\nfunction fieldToStructuredValue(field: ContentFieldUnion): unknown {\n const kind = resolveFieldKind(field);\n switch (kind) {\n case \"string\":\n return (field as StringField).value ?? null;\n case \"date\": {\n const d = (field as DateField).value;\n return d ? d.toISOString() : null;\n }\n case \"time\":\n return (field as TimeField).value ?? null;\n case \"number\":\n return (field as NumberField).value ?? null;\n case \"integer\":\n return (field as IntegerField).value ?? null;\n case \"boolean\":\n return (field as BooleanField).value ?? null;\n case \"array\": {\n const values = (field as ArrayField).value ?? [];\n return values.map((element) => fieldToStructuredValue(element));\n }\n case \"object\": {\n const properties = (field as ObjectField).value ?? {};\n return fieldsToStructuredMap(properties);\n }\n case \"json\":\n return (field as JsonField).value ?? null;\n default: {\n const base = field as ContentField;\n if (base.value === undefined || base.value === null) {\n return null;\n }\n return typeof base.value === \"object\" ? base.value : String(base.value);\n }\n }\n}\n\nfunction resolveFieldKind(field: ContentFieldUnion): string {\n if (\"fieldType\" in field && typeof field.fieldType === \"string\") {\n return field.fieldType;\n }\n return (field as ContentField).type;\n}\n","import type { BinaryAttachment, NodeExecutionContext } from \"@codemation/core\";\n\n/** Default cap on bytes read into memory. Tuned for prebuilt OCR analyzers (single document). */\nexport const DEFAULT_MAX_BYTES = 50 * 1024 * 1024;\n\n/**\n * Reads the binary body for an OCR analyzer call.\n *\n * The Azure Content Understanding SDK requires a contiguous `Uint8Array`, so the bytes must\n * land in memory at some point. To bound that:\n * - The attachment's declared `size` is checked against `maxBytes` *before* any allocation.\n * - A single buffer of exactly `attachment.size` is pre-allocated (no chunks array, no doubling).\n * - The stream fills the buffer directly; a length mismatch fails fast.\n */\nexport async function readBinaryBody(\n ctx: Pick<NodeExecutionContext, \"binary\">,\n attachment: BinaryAttachment,\n maxBytes: number = DEFAULT_MAX_BYTES,\n): Promise<Uint8Array> {\n if (attachment.size > maxBytes) {\n throw new Error(\n `Binary attachment size ${attachment.size} bytes exceeds maxBytes ${maxBytes}. ` +\n `Raise the node's maxBytes setting if this document is expected to be larger.`,\n );\n }\n const stream = await ctx.binary.openReadStream(attachment);\n if (!stream) {\n throw new Error(\"Binary attachment stream is unavailable.\");\n }\n const out = new Uint8Array(attachment.size);\n const reader = stream.body.getReader();\n let offset = 0;\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n if (!value) {\n continue;\n }\n if (offset + value.byteLength > out.byteLength) {\n throw new Error(`Binary stream produced more bytes than the attachment's declared size (${attachment.size}).`);\n }\n out.set(value, offset);\n offset += value.byteLength;\n }\n if (offset !== out.byteLength) {\n throw new Error(`Binary stream produced ${offset} bytes but attachment declared size ${attachment.size}.`);\n }\n return out;\n}\n","import { defineNode } from \"@codemation/core\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\nimport { azureContentUnderstandingCredentialType } from \"../credentials/azureContentUnderstandingCredential\";\nimport { analyzeWithAzure } from \"../lib/analyzeWithAzure\";\nimport { readBinaryBody } from \"../lib/readBinaryBody\";\n\n/** Default Azure Content Understanding prebuilt general document analyzer ID. */\nconst DEFAULT_DOCUMENT_ANALYZER_ID = \"prebuilt-document\";\n\nexport type AnalyzeDocumentConfig = Readonly<{\n /** Key on `item.binary` that holds the document bytes. Default: `\"data\"`. */\n binaryField?: string;\n /** MIME type override sent to the analyzer. Falls back to attachment `mimeType` when not set. */\n contentType?: string;\n /**\n * Azure Content Understanding analyzer ID to use.\n * Defaults to `\"prebuilt-document\"`. Set this to a custom analyzer ID when you have\n * a trained model or need a different prebuilt variant.\n */\n analyzerId?: string;\n /** Max bytes the attachment may have before reading. Defaults to 50 MiB. */\n maxBytes?: number;\n}>;\n\nexport const analyzeDocumentNode = defineNode({\n key: \"azure-ocr.analyze-document\",\n title: \"Analyze Document\",\n description:\n \"Runs an Azure Content Understanding document analyzer on a binary attachment and returns markdown text plus structured fields. Defaults to the prebuilt general document analyzer.\",\n icon: \"lucide:scan-text\",\n input: {\n binaryField: \"data\",\n contentType: undefined as string | undefined,\n analyzerId: undefined as string | undefined,\n maxBytes: undefined as number | undefined,\n },\n credentials: {\n contentUnderstanding: {\n type: azureContentUnderstandingCredentialType as import(\"@codemation/core\").AnyCredentialType,\n label: \"Azure Content Understanding\",\n helpText: \"Bind an Azure Content Understanding credential (endpoint + key).\",\n },\n },\n inspectorSummary({ config }) {\n const cfg = config as unknown as AnalyzeDocumentConfig;\n const analyzerId = cfg.analyzerId ?? DEFAULT_DOCUMENT_ANALYZER_ID;\n const rows = [{ label: \"Analyzer\", value: analyzerId }];\n const binaryField = cfg.binaryField ?? \"data\";\n if (binaryField !== \"data\") {\n rows.push({ label: \"Binary field\", value: binaryField });\n }\n if (cfg.contentType) {\n rows.push({ label: \"Content type\", value: cfg.contentType });\n }\n return rows;\n },\n async execute({ item, ctx }, { config: rawConfig, credentials }) {\n const config = rawConfig as unknown as AnalyzeDocumentConfig;\n const session = (await credentials.contentUnderstanding()) as AzureContentUnderstandingSession;\n const binaryField = config.binaryField ?? \"data\";\n const attachment = item.binary?.[binaryField];\n if (!attachment) {\n throw new Error(`Analyze Document: no binary attachment at key \"${binaryField}\".`);\n }\n const analyzerId = config.analyzerId ?? DEFAULT_DOCUMENT_ANALYZER_ID;\n const contentType = config.contentType ?? attachment.mimeType ?? \"application/octet-stream\";\n const body = await readBinaryBody(ctx, attachment, config.maxBytes);\n return analyzeWithAzure({ session, analyzerId, body, contentType });\n },\n});\n","import { defineNode } from \"@codemation/core\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\nimport { azureContentUnderstandingCredentialType } from \"../credentials/azureContentUnderstandingCredential\";\nimport { analyzeWithAzure } from \"../lib/analyzeWithAzure\";\nimport { readBinaryBody } from \"../lib/readBinaryBody\";\n\n/** Default Azure Content Understanding prebuilt image analyzer ID. */\nconst DEFAULT_IMAGE_ANALYZER_ID = \"prebuilt-imageAnalyzer\";\n\nexport type AnalyzeImageConfig = Readonly<{\n /** Key on `item.binary` that holds the image bytes. Default: `\"data\"`. */\n binaryField?: string;\n /** MIME type override sent to the analyzer. Falls back to attachment `mimeType` when not set. */\n contentType?: string;\n /**\n * Azure Content Understanding analyzer ID to use.\n * Defaults to `\"prebuilt-imageAnalyzer\"`. Set this to a custom analyzer ID when you have\n * a trained model or need a different prebuilt variant.\n */\n analyzerId?: string;\n /** Max bytes the attachment may have before reading. Defaults to 50 MiB. */\n maxBytes?: number;\n}>;\n\nexport const analyzeImageNode = defineNode({\n key: \"azure-ocr.analyze-image\",\n title: \"Analyze Image\",\n description:\n \"Runs an Azure Content Understanding image analyzer on a binary attachment and returns markdown text plus structured fields. Defaults to the prebuilt image analyzer.\",\n icon: \"lucide:image-search\",\n input: {\n binaryField: \"data\",\n contentType: undefined as string | undefined,\n analyzerId: undefined as string | undefined,\n maxBytes: undefined as number | undefined,\n },\n credentials: {\n contentUnderstanding: {\n type: azureContentUnderstandingCredentialType as import(\"@codemation/core\").AnyCredentialType,\n label: \"Azure Content Understanding\",\n helpText: \"Bind an Azure Content Understanding credential (endpoint + key).\",\n },\n },\n inspectorSummary({ config }) {\n const cfg = config as unknown as AnalyzeImageConfig;\n const analyzerId = cfg.analyzerId ?? DEFAULT_IMAGE_ANALYZER_ID;\n const rows = [{ label: \"Analyzer\", value: analyzerId }];\n const binaryField = cfg.binaryField ?? \"data\";\n if (binaryField !== \"data\") {\n rows.push({ label: \"Binary field\", value: binaryField });\n }\n if (cfg.contentType) {\n rows.push({ label: \"Content type\", value: cfg.contentType });\n }\n return rows;\n },\n async execute({ item, ctx }, { config: rawConfig, credentials }) {\n const config = rawConfig as unknown as AnalyzeImageConfig;\n const session = (await credentials.contentUnderstanding()) as AzureContentUnderstandingSession;\n const binaryField = config.binaryField ?? \"data\";\n const attachment = item.binary?.[binaryField];\n if (!attachment) {\n throw new Error(`Analyze Image: no binary attachment at key \"${binaryField}\".`);\n }\n const analyzerId = config.analyzerId ?? DEFAULT_IMAGE_ANALYZER_ID;\n const contentType = config.contentType ?? attachment.mimeType ?? \"application/octet-stream\";\n const body = await readBinaryBody(ctx, attachment, config.maxBytes);\n return analyzeWithAzure({ session, analyzerId, body, contentType });\n },\n});\n","import { defineNode } from \"@codemation/core\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\nimport { azureContentUnderstandingCredentialType } from \"../credentials/azureContentUnderstandingCredential\";\nimport { analyzeWithAzure } from \"../lib/analyzeWithAzure\";\nimport { readBinaryBody } from \"../lib/readBinaryBody\";\n\n/** Azure Content Understanding prebuilt invoice analyzer ID. */\nconst PREBUILT_INVOICE_ANALYZER_ID = \"prebuilt-invoice\";\n\nexport type AnalyzeInvoiceConfig = Readonly<{\n /** Key on `item.binary` that holds the document bytes. Default: `\"data\"`. */\n binaryField?: string;\n /** MIME type override sent to the analyzer. Falls back to attachment `mimeType` when not set. */\n contentType?: string;\n /** Max bytes the attachment may have before reading. Defaults to 50 MiB. */\n maxBytes?: number;\n}>;\n\nexport const analyzeInvoiceNode = defineNode({\n key: \"azure-ocr.analyze-invoice\",\n title: \"Analyze Invoice\",\n description:\n \"Runs the Azure Content Understanding prebuilt invoice analyzer on a binary attachment and returns markdown text plus structured fields.\",\n icon: \"lucide:receipt\",\n input: {\n binaryField: \"data\",\n contentType: undefined as string | undefined,\n maxBytes: undefined as number | undefined,\n },\n credentials: {\n contentUnderstanding: {\n type: azureContentUnderstandingCredentialType as import(\"@codemation/core\").AnyCredentialType,\n label: \"Azure Content Understanding\",\n helpText: \"Bind an Azure Content Understanding credential (endpoint + key).\",\n },\n },\n inspectorSummary({ config }) {\n const cfg = config as unknown as AnalyzeInvoiceConfig;\n const rows = [{ label: \"Analyzer\", value: \"Invoice (prebuilt)\" }];\n const binaryField = cfg.binaryField ?? \"data\";\n if (binaryField !== \"data\") {\n rows.push({ label: \"Binary field\", value: binaryField });\n }\n if (cfg.contentType) {\n rows.push({ label: \"Content type\", value: cfg.contentType });\n }\n return rows;\n },\n async execute({ item, ctx }, { config: rawConfig, credentials }) {\n const config = rawConfig as unknown as AnalyzeInvoiceConfig;\n const session = (await credentials.contentUnderstanding()) as AzureContentUnderstandingSession;\n const binaryField = config.binaryField ?? \"data\";\n const attachment = item.binary?.[binaryField];\n if (!attachment) {\n throw new Error(`Analyze Invoice: no binary attachment at key \"${binaryField}\".`);\n }\n const contentType = config.contentType ?? attachment.mimeType ?? \"application/octet-stream\";\n const body = await readBinaryBody(ctx, attachment, config.maxBytes);\n return analyzeWithAzure({ session, analyzerId: PREBUILT_INVOICE_ANALYZER_ID, body, contentType });\n },\n});\n"],"mappings":";;;;;;;;;AASA,SAAS,kBAAkB,KAAqB;AAC9C,QAAO,IAAI,MAAM,CAAC,QAAQ,QAAQ,GAAG;;AAGvC,SAAS,aAAa,MAGe;CACnC,MAAM,WAAW,kBAAkB,OAAO,KAAK,aAAa,eAAe,GAAG,CAAC;CAC/E,MAAM,SAAS,OAAO,KAAK,SAAS,aAAa,GAAG,CAAC,MAAM;AAC3D,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,8EAA8E;AAEhG,KAAI,CAAC,OACH,OAAM,IAAI,MAAM,6EAA6E;AAE/F,QAAO;EAAE;EAAU;EAAQ;;AAG7B,MAAa,kFAA2D;CACtE,KAAK;CACL,OAAO;CACP,aAAa;CACb,QAAQ,EACN,UAAU;EACR,KAAK;EACL,OAAO;EACP,MAAM;EACN,UAAU;EACV,aAAa;EACb,UAAU;EACV,OAAO;EACR,EACF;CACD,QAAQ,EACN,QAAQ;EACN,K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K,YAAY,OAAO,SAAS;GACd;GAAa,CAAC;;CAEtE,CAAC;;;;;AC9DF,MAAM,+BAA+B;AAWrC,MAAa,uDAAgC;CAC3C,KAAK;CACL,OAAO;CACP,aACE;CACF,MAAM;CACN,OAAO;EACL,aAAa;EACb,aAAa;EACb,UAAU;EACX;CACD,aAAa,EACX,sBAAsB;EACpB,MAAM;EACN,OAAO;EACP,UAAU;EACX,EACF;CACD,iBAAiB,EAAE,UAAU;EAC3B,MAAM,MAAM;EACZ,MAAM,OAAO,CAAC;GAAE,OAAO;GAAY,OAAO;GAAsB,CAAC;EACjE,MAAM,cAAc,IAAI,eAAe;AACvC,MAAI,gBAAgB,OAClB,MAAK,KAAK;GAAE,OAAO;GAAgB,OAAO;GAAa,CAAC;AAE1D,MAAI,IAAI,YACN,MAAK,KAAK;GAAE,OAAO;GAAgB,OAAO,IAAI;GAAa,CAAC;AAE9D,SAAO;;CAET,MAAM,QAAQ,EAAE,MAAM,OAAO,EAAE,QAAQ,WAAW,eAAe;EAC/D,MAAM,SAAS;EACf,MAAM,UAAW,MAAM,YAAY,sBAAsB;EACzD,MAAM,cAAc,OAAO,eAAe;EAC1C,MAAM,aAAa,KAAK,SAAS;AACjC,MAAI,CAAC,WACH,OAAM,IAAI,MAAM,iDAAiD,YAAY,IAAI;EAEnF,MAAM,cAAc,OAAO,eAAe,WAAW,YAAY;AAEjE,SAAO,iBAAiB;GAAE;GAAS,YAAY;GAA8B,MADhE,MAAM,eAAe,KAAK,YAAY,OAAO,SAAS;GACgB;GAAa,CAAC;;CAEpG,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"analyzeInvoiceNode-uVwe3GHD.js","names":["markdownParts: string[]","out: Record<string, unknown>"],"sources":["../src/credentials/azureContentUnderstandingCredential.ts","../src/lib/analyzeWithAzure.ts","../src/lib/readBinaryBody.ts","../src/nodes/analyzeDocumentNode.ts","../src/nodes/analyzeImageNode.ts","../src/nodes/analyzeInvoiceNode.ts"],"sourcesContent":["import { ContentUnderstandingClient } from \"@azure/ai-content-understanding\";\nimport { AzureKeyCredential } from \"@azure/core-auth\";\nimport { defineCredential } from \"@codemation/core\";\n\nexport type AzureContentUnderstandingSession = Readonly<{\n endpoint: string;\n apiKey: string;\n}>;\n\nfunction normalizeEndpoint(raw: string): string {\n return raw.trim().replace(/\\/+$/, \"\");\n}\n\nfunction buildSession(args: {\n readonly publicConfig: Readonly<Record<string, unknown>>;\n readonly material: Readonly<Record<string, unknown>>;\n}): AzureContentUnderstandingSession {\n const endpoint = normalizeEndpoint(String(args.publicConfig[\"endpoint\"] ?? \"\"));\n const apiKey = String(args.material[\"apiKey\"] ?? \"\").trim();\n if (!endpoint) {\n throw new Error(\"Azure Content Understanding credential is incomplete: endpoint is required.\");\n }\n if (!apiKey) {\n throw new Error(\"Azure Content Understanding credential is incomplete: API key is required.\");\n }\n return { endpoint, apiKey };\n}\n\nexport const azureContentUnderstandingCredentialType = defineCredential({\n key: \"azure.contentUnderstanding\",\n label: \"Azure Content Understanding\",\n description: \"Azure AI Content Understanding (endpoint + key) for prebuilt document, invoice, and image analyzers.\",\n public: {\n endpoint: {\n key: \"endpoint\",\n label: \"Endpoint\",\n type: \"string\" as const,\n required: true,\n placeholder: \"https://your-resource.cognitiveservices.azure.com/\",\n helpText: \"Content Understanding resource endpoint URL (no trailing slash).\",\n order: 0,\n },\n },\n secret: {\n apiKey: {\n key: \"apiKey\",\n label: \"API key\",\n type: \"password\" as const,\n required: true,\n order: 1,\n },\n },\n async createSession(args) {\n return buildSession(args);\n },\n async test(args) {\n try {\n const session = buildSession(args);\n const client = new ContentUnderstandingClient(session.endpoint, new AzureKeyCredential(session.apiKey));\n const iter = client.listAnalyzers();\n await iter.next();\n return {\n status: \"healthy\",\n message: \"Listed analyzers successfully.\",\n testedAt: new Date().toISOString(),\n };\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n return {\n status: \"failing\",\n message: message || \"Azure Content Understanding connection failed.\",\n testedAt: new Date().toISOString(),\n };\n }\n },\n});\n","import type {\n AnalysisResult,\n ArrayField,\n BooleanField,\n ContentField,\n ContentFieldUnion,\n DateField,\n IntegerField,\n JsonField,\n NumberField,\n ObjectField,\n StringField,\n TimeField,\n} from \"@azure/ai-content-understanding\";\nimport { ContentUnderstandingClient } from \"@azure/ai-content-understanding\";\nimport { AzureKeyCredential } from \"@azure/core-auth\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\n\n/** Structured analyzer fields: scalars at leaves; nested objects and arrays preserved. */\nexport type OcrStructuredFields = Readonly<Record<string, unknown>>;\n\n/** The output shape returned by all OCR analyzer nodes. */\nexport type OcrAnalysisOutput = Readonly<{\n /** Markdown representation of the document content. */\n content: string;\n /** Structured fields extracted by the prebuilt analyzer. */\n fields: OcrStructuredFields;\n}>;\n\n/**\n * Analyzes a binary document using an Azure Content Understanding prebuilt analyzer.\n * Retries on transient failures are handled by the engine via the node's `retryPolicy`.\n */\nexport async function analyzeWithAzure(\n args: Readonly<{\n session: AzureContentUnderstandingSession;\n analyzerId: string;\n body: Uint8Array;\n contentType: string;\n }>,\n): Promise<OcrAnalysisOutput> {\n const client = new ContentUnderstandingClient(args.session.endpoint, new AzureKeyCredential(args.session.apiKey));\n const poller = client.analyzeBinary(args.analyzerId, args.body, args.contentType);\n const result = await poller.pollUntilDone();\n return mapAnalysisResult(result);\n}\n\n/** @internal Exported for testing — maps a raw AnalysisResult to the node output shape. */\nexport function mapAnalysisResult(result: AnalysisResult): OcrAnalysisOutput {\n const contents = result.contents ?? [];\n const markdownParts: string[] = [];\n for (const c of contents) {\n if (typeof c.markdown === \"string\" && c.markdown.length > 0) {\n markdownParts.push(c.markdown);\n }\n }\n const content = markdownParts.join(\"\\n\\n\");\n if (contents.length === 0) {\n return { content: \"\", fields: {} };\n }\n if (contents.length === 1) {\n const c = contents[0]!;\n return {\n content,\n fields: c.fields ? fieldsToStructuredMap(c.fields) : {},\n };\n }\n return {\n content,\n fields: {\n segments: contents.map((c, index) => ({\n index,\n markdown: typeof c.markdown === \"string\" && c.markdown.trim().length > 0 ? c.markdown : undefined,\n fields: c.fields ? fieldsToStructuredMap(c.fields) : {},\n })),\n },\n };\n}\n\nfunction fieldsToStructuredMap(fields: Readonly<Record<string, ContentFieldUnion>>): OcrStructuredFields {\n const out: Record<string, unknown> = {};\n for (const [name, field] of Object.entries(fields)) {\n out[name] = fieldToStructuredValue(field);\n }\n return out;\n}\n\nfunction fieldToStructuredValue(field: ContentFieldUnion): unknown {\n const kind = resolveFieldKind(field);\n switch (kind) {\n case \"string\":\n return (field as StringField).value ?? null;\n case \"date\": {\n const d = (field as DateField).value;\n return d ? d.toISOString() : null;\n }\n case \"time\":\n return (field as TimeField).value ?? null;\n case \"number\":\n return (field as NumberField).value ?? null;\n case \"integer\":\n return (field as IntegerField).value ?? null;\n case \"boolean\":\n return (field as BooleanField).value ?? null;\n case \"array\": {\n const values = (field as ArrayField).value ?? [];\n return values.map((element) => fieldToStructuredValue(element));\n }\n case \"object\": {\n const properties = (field as ObjectField).value ?? {};\n return fieldsToStructuredMap(properties);\n }\n case \"json\":\n return (field as JsonField).value ?? null;\n default: {\n const base = field as ContentField;\n if (base.value === undefined || base.value === null) {\n return null;\n }\n return typeof base.value === \"object\" ? base.value : String(base.value);\n }\n }\n}\n\nfunction resolveFieldKind(field: ContentFieldUnion): string {\n if (\"fieldType\" in field && typeof field.fieldType === \"string\") {\n return field.fieldType;\n }\n return (field as ContentField).type;\n}\n","import type { BinaryAttachment, NodeExecutionContext } from \"@codemation/core\";\n\n/** Default cap on bytes read into memory. Tuned for prebuilt OCR analyzers (single document). */\nexport const DEFAULT_MAX_BYTES = 50 * 1024 * 1024;\n\n/**\n * Reads the binary body for an OCR analyzer call.\n *\n * The Azure Content Understanding SDK requires a contiguous `Uint8Array`, so the bytes must\n * land in memory at some point. To bound that:\n * - The attachment's declared `size` is checked against `maxBytes` *before* any allocation.\n * - A single buffer of exactly `attachment.size` is pre-allocated (no chunks array, no doubling).\n * - The stream fills the buffer directly; a length mismatch fails fast.\n */\nexport async function readBinaryBody(\n ctx: Pick<NodeExecutionContext, \"binary\">,\n attachment: BinaryAttachment,\n maxBytes: number = DEFAULT_MAX_BYTES,\n): Promise<Uint8Array> {\n if (attachment.size > maxBytes) {\n throw new Error(\n `Binary attachment size ${attachment.size} bytes exceeds maxBytes ${maxBytes}. ` +\n `Raise the node's maxBytes setting if this document is expected to be larger.`,\n );\n }\n const stream = await ctx.binary.openReadStream(attachment);\n if (!stream) {\n throw new Error(\"Binary attachment stream is unavailable.\");\n }\n const out = new Uint8Array(attachment.size);\n const reader = stream.body.getReader();\n let offset = 0;\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n if (!value) {\n continue;\n }\n if (offset + value.byteLength > out.byteLength) {\n throw new Error(`Binary stream produced more bytes than the attachment's declared size (${attachment.size}).`);\n }\n out.set(value, offset);\n offset += value.byteLength;\n }\n if (offset !== out.byteLength) {\n throw new Error(`Binary stream produced ${offset} bytes but attachment declared size ${attachment.size}.`);\n }\n return out;\n}\n","import { defineNode } from \"@codemation/core\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\nimport { azureContentUnderstandingCredentialType } from \"../credentials/azureContentUnderstandingCredential\";\nimport { analyzeWithAzure } from \"../lib/analyzeWithAzure\";\nimport { readBinaryBody } from \"../lib/readBinaryBody\";\n\n/** Default Azure Content Understanding prebuilt general document analyzer ID. */\nconst DEFAULT_DOCUMENT_ANALYZER_ID = \"prebuilt-document\";\n\nexport type AnalyzeDocumentConfig = Readonly<{\n /** Key on `item.binary` that holds the document bytes. Default: `\"data\"`. */\n binaryField?: string;\n /** MIME type override sent to the analyzer. Falls back to attachment `mimeType` when not set. */\n contentType?: string;\n /**\n * Azure Content Understanding analyzer ID to use.\n * Defaults to `\"prebuilt-document\"`. Set this to a custom analyzer ID when you have\n * a trained model or need a different prebuilt variant.\n */\n analyzerId?: string;\n /** Max bytes the attachment may have before reading. Defaults to 50 MiB. */\n maxBytes?: number;\n}>;\n\nexport const analyzeDocumentNode = defineNode({\n key: \"azure-ocr.analyze-document\",\n title: \"Analyze Document\",\n description:\n \"Runs an Azure Content Understanding document analyzer on a binary attachment and returns markdown text plus structured fields. Defaults to the prebuilt general document analyzer.\",\n icon: \"lucide:scan-text\",\n input: {\n binaryField: \"data\",\n contentType: undefined as string | undefined,\n analyzerId: undefined as string | undefined,\n maxBytes: undefined as number | undefined,\n },\n credentials: {\n contentUnderstanding: {\n type: azureContentUnderstandingCredentialType as import(\"@codemation/core\").AnyCredentialType,\n label: \"Azure Content Understanding\",\n helpText: \"Bind an Azure Content Understanding credential (endpoint + key).\",\n },\n },\n inspectorSummary({ config }) {\n const cfg = config as unknown as AnalyzeDocumentConfig;\n const analyzerId = cfg.analyzerId ?? DEFAULT_DOCUMENT_ANALYZER_ID;\n const rows = [{ label: \"Analyzer\", value: analyzerId }];\n const binaryField = cfg.binaryField ?? \"data\";\n if (binaryField !== \"data\") {\n rows.push({ label: \"Binary field\", value: binaryField });\n }\n if (cfg.contentType) {\n rows.push({ label: \"Content type\", value: cfg.contentType });\n }\n return rows;\n },\n async execute({ item, ctx }, { config: rawConfig, credentials }) {\n const config = rawConfig as unknown as AnalyzeDocumentConfig;\n const session = (await credentials.contentUnderstanding()) as AzureContentUnderstandingSession;\n const binaryField = config.binaryField ?? \"data\";\n const attachment = item.binary?.[binaryField];\n if (!attachment) {\n throw new Error(`Analyze Document: no binary attachment at key \"${binaryField}\".`);\n }\n const analyzerId = config.analyzerId ?? DEFAULT_DOCUMENT_ANALYZER_ID;\n const contentType = config.contentType ?? attachment.mimeType ?? \"application/octet-stream\";\n const body = await readBinaryBody(ctx, attachment, config.maxBytes);\n return analyzeWithAzure({ session, analyzerId, body, contentType });\n },\n});\n","import { defineNode } from \"@codemation/core\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\nimport { azureContentUnderstandingCredentialType } from \"../credentials/azureContentUnderstandingCredential\";\nimport { analyzeWithAzure } from \"../lib/analyzeWithAzure\";\nimport { readBinaryBody } from \"../lib/readBinaryBody\";\n\n/** Default Azure Content Understanding prebuilt image analyzer ID. */\nconst DEFAULT_IMAGE_ANALYZER_ID = \"prebuilt-imageAnalyzer\";\n\nexport type AnalyzeImageConfig = Readonly<{\n /** Key on `item.binary` that holds the image bytes. Default: `\"data\"`. */\n binaryField?: string;\n /** MIME type override sent to the analyzer. Falls back to attachment `mimeType` when not set. */\n contentType?: string;\n /**\n * Azure Content Understanding analyzer ID to use.\n * Defaults to `\"prebuilt-imageAnalyzer\"`. Set this to a custom analyzer ID when you have\n * a trained model or need a different prebuilt variant.\n */\n analyzerId?: string;\n /** Max bytes the attachment may have before reading. Defaults to 50 MiB. */\n maxBytes?: number;\n}>;\n\nexport const analyzeImageNode = defineNode({\n key: \"azure-ocr.analyze-image\",\n title: \"Analyze Image\",\n description:\n \"Runs an Azure Content Understanding image analyzer on a binary attachment and returns markdown text plus structured fields. Defaults to the prebuilt image analyzer.\",\n icon: \"lucide:image-search\",\n input: {\n binaryField: \"data\",\n contentType: undefined as string | undefined,\n analyzerId: undefined as string | undefined,\n maxBytes: undefined as number | undefined,\n },\n credentials: {\n contentUnderstanding: {\n type: azureContentUnderstandingCredentialType as import(\"@codemation/core\").AnyCredentialType,\n label: \"Azure Content Understanding\",\n helpText: \"Bind an Azure Content Understanding credential (endpoint + key).\",\n },\n },\n inspectorSummary({ config }) {\n const cfg = config as unknown as AnalyzeImageConfig;\n const analyzerId = cfg.analyzerId ?? DEFAULT_IMAGE_ANALYZER_ID;\n const rows = [{ label: \"Analyzer\", value: analyzerId }];\n const binaryField = cfg.binaryField ?? \"data\";\n if (binaryField !== \"data\") {\n rows.push({ label: \"Binary field\", value: binaryField });\n }\n if (cfg.contentType) {\n rows.push({ label: \"Content type\", value: cfg.contentType });\n }\n return rows;\n },\n async execute({ item, ctx }, { config: rawConfig, credentials }) {\n const config = rawConfig as unknown as AnalyzeImageConfig;\n const session = (await credentials.contentUnderstanding()) as AzureContentUnderstandingSession;\n const binaryField = config.binaryField ?? \"data\";\n const attachment = item.binary?.[binaryField];\n if (!attachment) {\n throw new Error(`Analyze Image: no binary attachment at key \"${binaryField}\".`);\n }\n const analyzerId = config.analyzerId ?? DEFAULT_IMAGE_ANALYZER_ID;\n const contentType = config.contentType ?? attachment.mimeType ?? \"application/octet-stream\";\n const body = await readBinaryBody(ctx, attachment, config.maxBytes);\n return analyzeWithAzure({ session, analyzerId, body, contentType });\n },\n});\n","import { defineNode } from \"@codemation/core\";\nimport type { AzureContentUnderstandingSession } from \"../credentials/azureContentUnderstandingCredential\";\nimport { azureContentUnderstandingCredentialType } from \"../credentials/azureContentUnderstandingCredential\";\nimport { analyzeWithAzure } from \"../lib/analyzeWithAzure\";\nimport { readBinaryBody } from \"../lib/readBinaryBody\";\n\n/** Azure Content Understanding prebuilt invoice analyzer ID. */\nconst PREBUILT_INVOICE_ANALYZER_ID = \"prebuilt-invoice\";\n\nexport type AnalyzeInvoiceConfig = Readonly<{\n /** Key on `item.binary` that holds the document bytes. Default: `\"data\"`. */\n binaryField?: string;\n /** MIME type override sent to the analyzer. Falls back to attachment `mimeType` when not set. */\n contentType?: string;\n /** Max bytes the attachment may have before reading. Defaults to 50 MiB. */\n maxBytes?: number;\n}>;\n\nexport const analyzeInvoiceNode = defineNode({\n key: \"azure-ocr.analyze-invoice\",\n title: \"Analyze Invoice\",\n description:\n \"Runs the Azure Content Understanding prebuilt invoice analyzer on a binary attachment and returns markdown text plus structured fields.\",\n icon: \"lucide:receipt\",\n input: {\n binaryField: \"data\",\n contentType: undefined as string | undefined,\n maxBytes: undefined as number | undefined,\n },\n credentials: {\n contentUnderstanding: {\n type: azureContentUnderstandingCredentialType as import(\"@codemation/core\").AnyCredentialType,\n label: \"Azure Content Understanding\",\n helpText: \"Bind an Azure Content Understanding credential (endpoint + key).\",\n },\n },\n inspectorSummary({ config }) {\n const cfg = config as unknown as AnalyzeInvoiceConfig;\n const rows = [{ label: \"Analyzer\", value: \"Invoice (prebuilt)\" }];\n const binaryField = cfg.binaryField ?? \"data\";\n if (binaryField !== \"data\") {\n rows.push({ label: \"Binary field\", value: binaryField });\n }\n if (cfg.contentType) {\n rows.push({ label: \"Content type\", value: cfg.contentType });\n }\n return rows;\n },\n async execute({ item, ctx }, { config: rawConfig, credentials }) {\n const config = rawConfig as unknown as AnalyzeInvoiceConfig;\n const session = (await credentials.contentUnderstanding()) as AzureContentUnderstandingSession;\n const binaryField = config.binaryField ?? \"data\";\n const attachment = item.binary?.[binaryField];\n if (!attachment) {\n throw new Error(`Analyze Invoice: no binary attachment at key \"${binaryField}\".`);\n }\n const contentType = config.contentType ?? attachment.mimeType ?? \"application/octet-stream\";\n const body = await readBinaryBody(ctx, attachment, config.maxBytes);\n return analyzeWithAzure({ session, analyzerId: PREBUILT_INVOICE_ANALYZER_ID, body, contentType });\n },\n});\n"],"mappings":";;;;;AASA,SAAS,kBAAkB,KAAqB;AAC9C,QAAO,IAAI,MAAM,CAAC,QAAQ,QAAQ,GAAG;;AAGvC,SAAS,aAAa,MAGe;CACnC,MAAM,WAAW,kBAAkB,OAAO,KAAK,aAAa,eAAe,GAAG,CAAC;CAC/E,MAAM,SAAS,OAAO,KAAK,SAAS,aAAa,GAAG,CAAC,MAAM;AAC3D,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,8EAA8E;AAEhG,KAAI,CAAC,OACH,OAAM,IAAI,MAAM,6EAA6E;AAE/F,QAAO;EAAE;EAAU;EAAQ;;AAG7B,MAAa,0CAA0C,iBAAiB;CACtE,KAAK;CACL,OAAO;CACP,aAAa;CACb,QAAQ,EACN,UAAU;EACR,KAAK;EACL,OAAO;EACP,MAAM;EACN,UAAU;EACV,aAAa;EACb,UAAU;EACV,OAAO;EACR,EACF;CACD,QAAQ,EACN,QAAQ;EACN,KAAK;EACL,OAAO;EACP,MAAM;EACN,UAAU;EACV,OAAO;EACR,EACF;CACD,MAAM,cAAc,M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,+BAA+B;AAWrC,MAAa,qBAAqB,WAAW;CAC3C,KAAK;CACL,OAAO;CACP,aACE;CACF,MAAM;CACN,OAAO;EACL,aAAa;EACb,aAAa;EACb,UAAU;EACX;CACD,aAAa,EACX,sBAAsB;EACpB,MAAM;EACN,OAAO;EACP,UAAU;EACX,EACF;CACD,iBAAiB,EAAE,UAAU;EAC3B,MAAM,MAAM;EACZ,MAAM,OAAO,CAAC;GAAE,OAAO;GAAY,OAAO;GAAsB,CAAC;EACjE,MAAM,cAAc,IAAI,eAAe;AACvC,MAAI,gBAAgB,OAClB,MAAK,KAAK;GAAE,OAAO;GAAgB,OAAO;GAAa,CAAC;AAE1D,MAAI,IAAI,YACN,MAAK,KAAK;GAAE,OAAO;GAAgB,OAAO,IAAI;GAAa,CAAC;AAE9D,SAAO;;CAET,MAAM,QAAQ,EAAE,MAAM,OAAO,EAAE,QAAQ,WAAW,eAAe;EAC/D,MAAM,SAAS;EACf,MAAM,UAAW,MAAM,YAAY,sBAAsB;EACzD,MAAM,cAAc,OAAO,eAAe;EAC1C,MAAM,aAAa,KAAK,SAAS;AACjC,MAAI,CAAC,WACH,OAAM,IAAI,MAAM,iDAAiD,YAAY,IAAI;EAEnF,MAAM,cAAc,OAAO,eAAe,WAAW,YAAY;AAEjE,SAAO,iBAAiB;GAAE;GAAS,YAAY;GAA8B,MADhE,MAAM,eAAe,KAAK,YAAY,OAAO,SAAS;GACgB;GAAa,CAAC;;CAEpG,CAAC"}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { BinaryAttachment, NodeExecutionContext } from "@codemation/core";
|
|
2
|
-
|
|
3
|
-
/** Default cap on bytes read into memory. Tuned for prebuilt OCR analyzers (single document). */
|
|
4
|
-
export const DEFAULT_MAX_BYTES = 50 * 1024 * 1024;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Reads the binary body for an OCR analyzer call.
|
|
8
|
-
*
|
|
9
|
-
* The Azure Content Understanding SDK requires a contiguous `Uint8Array`, so the bytes must
|
|
10
|
-
* land in memory at some point. To bound that:
|
|
11
|
-
* - The attachment's declared `size` is checked against `maxBytes` *before* any allocation.
|
|
12
|
-
* - A single buffer of exactly `attachment.size` is pre-allocated (no chunks array, no doubling).
|
|
13
|
-
* - The stream fills the buffer directly; a length mismatch fails fast.
|
|
14
|
-
*/
|
|
15
|
-
export async function readBinaryBody(
|
|
16
|
-
ctx: Pick<NodeExecutionContext, "binary">,
|
|
17
|
-
attachment: BinaryAttachment,
|
|
18
|
-
maxBytes: number = DEFAULT_MAX_BYTES,
|
|
19
|
-
): Promise<Uint8Array> {
|
|
20
|
-
if (attachment.size > maxBytes) {
|
|
21
|
-
throw new Error(
|
|
22
|
-
`Binary attachment size ${attachment.size} bytes exceeds maxBytes ${maxBytes}. ` +
|
|
23
|
-
`Raise the node's maxBytes setting if this document is expected to be larger.`,
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
const stream = await ctx.binary.openReadStream(attachment);
|
|
27
|
-
if (!stream) {
|
|
28
|
-
throw new Error("Binary attachment stream is unavailable.");
|
|
29
|
-
}
|
|
30
|
-
const out = new Uint8Array(attachment.size);
|
|
31
|
-
const reader = stream.body.getReader();
|
|
32
|
-
let offset = 0;
|
|
33
|
-
while (true) {
|
|
34
|
-
const { done, value } = await reader.read();
|
|
35
|
-
if (done) {
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
if (!value) {
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
if (offset + value.byteLength > out.byteLength) {
|
|
42
|
-
throw new Error(`Binary stream produced more bytes than the attachment's declared size (${attachment.size}).`);
|
|
43
|
-
}
|
|
44
|
-
out.set(value, offset);
|
|
45
|
-
offset += value.byteLength;
|
|
46
|
-
}
|
|
47
|
-
if (offset !== out.byteLength) {
|
|
48
|
-
throw new Error(`Binary stream produced ${offset} bytes but attachment declared size ${attachment.size}.`);
|
|
49
|
-
}
|
|
50
|
-
return out;
|
|
51
|
-
}
|