Manifest enrichment#699
Open
stephenwf wants to merge 10 commits into
Open
Conversation
stephenwf
commented
Apr 4, 2023
Comment on lines
+8
to
+54
| export const manifestEnrichmentPipeline: RouteMiddleware<{ id: number }> = async context => { | ||
| const { siteId } = userWithScope(context, ['site.admin']); | ||
| const site = await context.siteManager.getSiteById(siteId); | ||
| const siteApi = api.asUser({ siteId }); | ||
|
|
||
| // 12-hour token. | ||
| const webhook = await context.webhookExtension.generateWebhookUrl( | ||
| site, | ||
| manifestEnrichmentPipelineEvent.event_id, | ||
| 12 * 3600 | ||
| ); | ||
| context.response.body = await siteApi.enrichment.enrichManifestInternal(context.params.id, webhook); | ||
| }; | ||
|
|
||
| export const manifestEnrichmentPipelineEvent: WebhookEventType = { | ||
| event_id: 'manifest-enrichment-pipeline.complete', | ||
| body_variables: ['id'], | ||
| }; | ||
|
|
||
| export const manifestEnrichmentHook: IncomingWebhook = { | ||
| type: 'manifest-enrichment-pipeline-task-ingest', | ||
| event_id: 'manifest-enrichment-pipeline.complete', | ||
| is_outgoing: false, | ||
| execute: async (resp, siteApi) => { | ||
| invariant(resp.id, 'Expected response to contain `id`'); | ||
|
|
||
| const task = await siteApi.enrichment.getEnrichmentTask(resp.id); | ||
| invariant(task.subject, 'Missing subject on task'); | ||
| invariant(task.status === 3, 'Task is not yet complete'); | ||
|
|
||
| if (task.task_type === 'ocr_madoc_resource') { | ||
| const parsed = parseUrn(task.subject); | ||
| invariant(parsed, 'Invalid subject'); | ||
| invariant(parsed.type === 'canvas', 'Can only process canvases'); | ||
|
|
||
| if (task.state && task.state.ocr_resources && task.state.ocr_resources[0]) { | ||
| const first = task.state.ocr_resources[0]; | ||
| const enrichmentPlaintext = await siteApi.enrichment.getEnrichmentPlaintext(first); | ||
| invariant(enrichmentPlaintext, 'Missing plaintext from enrichment'); | ||
| if (enrichmentPlaintext.plaintext) { | ||
| const canvasId = parsed.id; // ?? | ||
| return await siteApi.updateCanvasPlaintext(canvasId, enrichmentPlaintext.plaintext); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| }; |
Member
Author
There was a problem hiding this comment.
This is the main part (other parts mostly filling gaps in Madocs API).
manifestEnrichmentPipelineis the API route handler when an admin hits "Enrich"- Generates a webhook URL (12-hour token)
- Creates the enrichment task
- Returns task (@mattmcgrattan it would be useful to omit
callback_urlfrom the state in the future)
manifestEnrichmentPipelineEventis a short description of the webhook "type" and the fields expected in the response.manifestEnrichmentHookthis is the function that is called when the task is complete. We get the webhook post-body JSON and an instance of the siteApi (already mapped correctly to the right site).- Fetch the enrichment task
- Validate that it has a subject + is complete
- If it's an
ocr_madoc_resource:- Parse + validate the subject
- check for
ocr_resources(@mattmcgrattan will need to change if there are more than one here) - Fetch the plaintext + validate what we expect to see
- Attach the plaintext to the canvas.
stephenwf
commented
Apr 4, 2023
Comment on lines
+94
to
104
| enrichManifestInternal(id: number, callback?: string) { | ||
| return this.api.request<EnrichmentTask>(`/api/enrichment/tasks/madoc_manifest_enrichment_pipeline`, { | ||
| method: 'POST', | ||
| body: { | ||
| task: { | ||
| subject: `urn:madoc:manifest:${id}`, | ||
| parameters: [{ callback_url: callback }], | ||
| }, | ||
| }, | ||
| }); | ||
| } |
Member
Author
There was a problem hiding this comment.
This is the call to kick off the enrichment pipeline
|
Preview docker image available |
Contributor
|
you might want to put a |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

No description provided.