ocr-retry-job.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @node ocr-retry-job
  3. * @name OCR Retry Job
  4. * @category ocr
  5. * @version 1.0.1
  6. * @description Retry a failed OCR job
  7. * @icon refresh-cw
  8. */
  9. const configSchema = {
  10. type: 'object',
  11. properties: {
  12. baseUrl: {
  13. type: 'string',
  14. title: 'API Base URL',
  15. default: 'http://localhost:3000',
  16. description: 'Base URL of the OCR API'
  17. },
  18. credentialId: {
  19. type: 'string',
  20. title: 'Credential',
  21. description: 'Bearer or API Key credential for authentication',
  22. dynamicOptions: {
  23. source: 'credentials',
  24. filter: { type: 'api_key' }
  25. }
  26. },
  27. jobId: {
  28. type: 'string',
  29. title: 'Job ID',
  30. description: 'The ID of the job to retry (can be provided from input)'
  31. }
  32. },
  33. required: ['baseUrl']
  34. };
  35. const inputSchema = {
  36. type: 'object',
  37. properties: {
  38. jobId: {
  39. type: 'string',
  40. description: 'Job ID to retry (overrides config)'
  41. }
  42. }
  43. };
  44. const outputSchema = {
  45. type: 'object',
  46. properties: {
  47. job: {
  48. type: 'object',
  49. properties: {
  50. id: { type: 'string' },
  51. status: { type: 'string' },
  52. filename: { type: 'string' },
  53. created_at: { type: 'string' }
  54. }
  55. }
  56. }
  57. };
  58. const outputs = [
  59. { name: 'main', displayName: 'Retried', type: 'object', color: '#8b5cf6' }
  60. ];
  61. module.exports = {
  62. configSchema,
  63. inputSchema,
  64. outputSchema,
  65. outputs,
  66. async execute(config, input, context) {
  67. const baseUrl = (config.baseUrl || 'http://localhost:3000').replace(/\/$/, '');
  68. const jobId = input.jobId || config.jobId;
  69. if (!jobId) {
  70. throw new Error('Job ID is required');
  71. }
  72. const headers = {};
  73. if (config.credentialId) {
  74. const auth = smartbotic.credentials.get(config.credentialId);
  75. if (!auth.success) {
  76. throw new Error(`Failed to load credential: ${auth.error}`);
  77. }
  78. headers[auth.headerName] = auth.headerValue;
  79. }
  80. smartbotic.log.info(`OCR Retry Job: ${jobId}`);
  81. const response = smartbotic.http.request({
  82. method: 'POST',
  83. url: `${baseUrl}/api/jobs/${encodeURIComponent(jobId)}/retry`,
  84. headers: headers,
  85. timeout: 30000
  86. });
  87. if (response.status === 404) {
  88. throw new Error(`Job not found: ${jobId}`);
  89. }
  90. if (response.status < 200 || response.status >= 300) {
  91. const errorMsg = typeof response.data === 'string' ? response.data : JSON.stringify(response.data);
  92. if (response.status === 401 && !config.credentialId) { throw new Error('Authentication required - please select a credential in node settings'); } throw new Error(`OCR API error (${response.status}): ${errorMsg}`);
  93. }
  94. const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
  95. return {
  96. job: data.job || data
  97. };
  98. }
  99. };