-
Notifications
You must be signed in to change notification settings - Fork 42.8k
feat(Mindee Node): Update Mindee node to support Mindee V2 API #18986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sebastianMindee
wants to merge
10
commits into
n8n-io:master
Choose a base branch
from
sebastianMindee:update-mindee
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+479
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bb62ab5
add functional basic implementation
sebastianMindee 86bb7b4
follow polling & inference urls directly, add new options
sebastianMindee 9d9b388
switch to polling syntax
sebastianMindee 09362d2
lint
sebastianMindee 1f779e2
remove unneeded eslint directive
sebastianMindee d511298
restore legacy node, create V2 for new node
sebastianMindee 5109d98
simplify execute() syntax
sebastianMindee 862579c
switch options to three-state
sebastianMindee 0a2dc4f
decapitalize doc urls
sebastianMindee 74071b0
misc fixes
sebastianMindee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/nodes-base/credentials/MindeeV2Api.credentials.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { | ||
ICredentialDataDecryptedObject, | ||
ICredentialType, | ||
IHttpRequestOptions, | ||
INodeProperties, | ||
} from 'n8n-workflow'; | ||
|
||
export class MindeeV2Api implements ICredentialType { | ||
name = 'mindeeV2Api'; | ||
|
||
displayName = 'Mindee API V2'; | ||
|
||
documentationUrl = 'mindee'; | ||
|
||
properties: INodeProperties[] = [ | ||
{ | ||
displayName: 'API Key', | ||
name: 'apiKey', | ||
type: 'string', | ||
typeOptions: { password: true }, | ||
default: '', | ||
}, | ||
]; | ||
|
||
async authenticate( | ||
credentials: ICredentialDataDecryptedObject, | ||
requestOptions: IHttpRequestOptions, | ||
): Promise<IHttpRequestOptions> { | ||
// @ts-ignore | ||
requestOptions.headers!.Authorization = `${credentials.apiKey}`; | ||
return requestOptions; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import type FormData from 'form-data'; | ||
import type { | ||
IExecuteFunctions, | ||
ILoadOptionsFunctions, | ||
IDataObject, | ||
JsonObject, | ||
IHttpRequestMethods, | ||
IHttpRequestOptions, | ||
} from 'n8n-workflow'; | ||
import { NodeApiError } from 'n8n-workflow'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
const INITIAL_DELAY_MS = 1500; | ||
const POLL_DELAY_MS = 1000; | ||
|
||
/** | ||
* UI params for the MindeeV2 node. | ||
*/ | ||
interface MindeeV2UIParams { | ||
modelId: string; | ||
alias?: string; | ||
rag: string; | ||
polygon: string; | ||
confidence: string; | ||
rawText: string; | ||
maxDelayCount: number; | ||
binaryPropertyName?: string; | ||
} | ||
|
||
/** | ||
* Makes an authenticated HTTP request to the Mindee API | ||
* @param method - HTTP method. | ||
* @param url - The Mindee API's (complete) URL. | ||
* @param body - The request body data. | ||
* @param option - Additional request options (default: empty object) | ||
* @returns The API response data | ||
* @throws NodeApiError when the API request fails | ||
*/ | ||
export async function mindeeApiRequest( | ||
this: IExecuteFunctions | ILoadOptionsFunctions, | ||
method: IHttpRequestMethods, | ||
url: string, | ||
body: IDataObject | FormData = {}, | ||
option = {}, | ||
): Promise<any> { | ||
const options: IHttpRequestOptions = { | ||
headers: { | ||
'User-Agent': `mindee-n8n@v${this.getNode().typeVersion ?? 'unknown'}`, | ||
}, | ||
method, | ||
url, | ||
body, | ||
}; | ||
try { | ||
delete options.qs; | ||
if (Object.keys(body as IDataObject).length === 0) { | ||
sebastianMindee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
delete options.body; | ||
} | ||
if (Object.keys(option).length !== 0) { | ||
Object.assign(options, option); | ||
} | ||
return await this.helpers.httpRequestWithAuthentication.call(this, 'mindeeV2Api', { | ||
...options, | ||
}); | ||
} catch (error) { | ||
throw new NodeApiError(this.getNode(), error as JsonObject); | ||
} | ||
} | ||
|
||
/** | ||
* Polls the Mindee API for the result of a job. | ||
* Automatically follows the redirect on the last poll attempt. | ||
* @param funcRef The execution function reference. | ||
* @param initialResponse Initial POST request response from the API. | ||
* @param maxDelayCounter Maximum number of attempts to poll the API. | ||
*/ | ||
export async function pollMindee( | ||
funcRef: IExecuteFunctions, | ||
initialResponse: IDataObject, | ||
maxDelayCounter: number, | ||
): Promise<IDataObject[]> { | ||
const result: IDataObject[] = []; | ||
let serverResponse = initialResponse; | ||
const jobId: string | undefined = (serverResponse?.job as IDataObject)?.id as string; | ||
sebastianMindee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!jobId || jobId.length === 0) { | ||
throw new NodeApiError(funcRef.getNode(), serverResponse as JsonObject, { | ||
message: 'Mindee POST response does not contain a job id.', | ||
}); | ||
} | ||
let jobStatus: string = (serverResponse.job as IDataObject).status as string; | ||
const pollUrl = (serverResponse.job as IDataObject).polling_url as string; | ||
|
||
await setTimeout(INITIAL_DELAY_MS); | ||
|
||
for (let i = 0; i < maxDelayCounter; i++) { | ||
if ( | ||
serverResponse.error || | ||
(serverResponse?.job as IDataObject).error || | ||
jobStatus === 'Failed' | ||
) { | ||
throw new NodeApiError(funcRef.getNode(), serverResponse as JsonObject); | ||
} | ||
|
||
serverResponse = await mindeeApiRequest.call(funcRef, 'GET', pollUrl); | ||
if ('inference' in (serverResponse as JsonObject)) break; | ||
|
||
if (!('job' in serverResponse)) | ||
throw new NodeApiError(funcRef.getNode(), serverResponse as JsonObject, { | ||
message: 'The Mindee API replied with an unexpected reply.', | ||
}); | ||
jobStatus = (serverResponse.job as IDataObject).status as string; | ||
await setTimeout(POLL_DELAY_MS); | ||
} | ||
|
||
if (!('inference' in (serverResponse as JsonObject))) | ||
throw new NodeApiError(funcRef.getNode(), serverResponse as JsonObject, { | ||
message: `Server polling timed out after ${maxDelayCounter} seconds. Status: ${jobStatus}.`, | ||
}); | ||
result.push(serverResponse); | ||
return result; | ||
} | ||
|
||
/** | ||
* Reads UI params from a given context. | ||
* @param ctx Execution context. | ||
* @param index Index of the parameter. | ||
*/ | ||
export function readUIParams(ctx: IExecuteFunctions, index: number): MindeeV2UIParams { | ||
const modelId = ctx.getNodeParameter('modelId', index); | ||
const alias = ctx.getNodeParameter('alias', index); | ||
const rag = ctx.getNodeParameter('rag', index); | ||
const polygon = ctx.getNodeParameter('polygon', index); | ||
const confidence = ctx.getNodeParameter('confidence', index); | ||
const rawText = ctx.getNodeParameter('rawText', index); | ||
const maxDelayCount = ctx.getNodeParameter('maxDelayCount', index); | ||
const binaryPropertyName = ctx.getNodeParameter('binaryPropertyName', index, ''); | ||
|
||
return { | ||
modelId: typeof modelId === 'string' ? modelId : '', | ||
alias: typeof alias === 'string' ? alias : '', | ||
rag: typeof rag === 'string' ? rag : 'default', | ||
polygon: typeof polygon === 'string' ? polygon : 'default', | ||
confidence: typeof confidence === 'string' ? confidence : 'default', | ||
rawText: typeof rawText === 'string' ? rawText : 'default', | ||
maxDelayCount: typeof maxDelayCount === 'number' ? maxDelayCount : 120, | ||
binaryPropertyName: typeof binaryPropertyName === 'string' ? binaryPropertyName : '', | ||
}; | ||
} | ||
|
||
/** | ||
* Builds the request body for the Mindee API. | ||
* @param ctx Execution context. | ||
* @param index Index of the parameter. | ||
* @param uiParams UI parameters. | ||
* @param form Form object. | ||
*/ | ||
export async function buildRequestBody( | ||
ctx: IExecuteFunctions, | ||
index: number, | ||
uiParams: MindeeV2UIParams, | ||
form: FormData, | ||
) { | ||
const name = uiParams.binaryPropertyName ?? 'data'; | ||
const bin = ctx.helpers.assertBinaryData(index, name); | ||
const buf = await ctx.helpers.getBinaryDataBuffer(index, name); | ||
form.append('file', buf, { filename: bin.fileName }); | ||
|
||
form.append('model_id', uiParams.modelId); | ||
form.append('alias', uiParams.alias ?? ''); | ||
if (uiParams.rag !== 'default') form.append('rag', uiParams.rag); | ||
if (uiParams.polygon !== 'default') form.append('polygon', uiParams.polygon); | ||
if (uiParams.confidence !== 'default') form.append('confidence', uiParams.confidence); | ||
if (uiParams.rawText !== 'default') form.append('raw_text', uiParams.rawText); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"node": "n8n-nodes-base.mindeeV2", | ||
"nodeVersion": "1.0", | ||
"codexVersion": "1.0", | ||
"categories": ["Utility"], | ||
"resources": { | ||
"credentialDocumentation": [ | ||
{ | ||
"url": "https://docs.n8n.io/integrations/builtin/credentials/mindeev2/" | ||
} | ||
], | ||
"primaryDocumentation": [ | ||
{ | ||
"url": "https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.mindeev2/" | ||
} | ||
], | ||
"generic": [] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: TypeScript Error Suppression Violates Coding Standards
The
@ts-ignore
directive suppresses a TypeScript error, which violates coding standards and hides a potential type safety issue. Specifically,requestOptions.headers
might be undefined, and the non-null assertion!
could lead to a runtime error.