ocr-job-status.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @node ocr-job-status
  3. * @name OCR Job Status
  4. * @category ocr
  5. * @version 1.0.1
  6. * @description Get the status and results of an OCR job
  7. * @icon clipboard-check
  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 check (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 check (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. completed_at: { type: 'string' }
  55. }
  56. },
  57. result: {
  58. type: 'object',
  59. properties: {
  60. text: { type: 'string' }
  61. }
  62. },
  63. usage: { type: 'object' },
  64. timings: { type: 'object' }
  65. }
  66. };
  67. const outputs = [
  68. { name: 'main', displayName: 'Job Status', type: 'object', color: '#3b82f6' }
  69. ];
  70. module.exports = {
  71. configSchema,
  72. inputSchema,
  73. outputSchema,
  74. outputs,
  75. async execute(config, input, context) {
  76. const baseUrl = (config.baseUrl || 'http://localhost:3000').replace(/\/$/, '');
  77. const jobId = input.jobId || config.jobId;
  78. if (!jobId) {
  79. throw new Error('Job ID is required');
  80. }
  81. const headers = {};
  82. if (config.credentialId) {
  83. const auth = smartbotic.credentials.get(config.credentialId);
  84. if (!auth.success) {
  85. throw new Error(`Failed to load credential: ${auth.error}`);
  86. }
  87. headers[auth.headerName] = auth.headerValue;
  88. }
  89. smartbotic.log.info(`OCR Get Job Status: ${jobId}`);
  90. const response = smartbotic.http.request({
  91. method: 'GET',
  92. url: `${baseUrl}/api/jobs/${encodeURIComponent(jobId)}`,
  93. headers: headers,
  94. timeout: 30000
  95. });
  96. if (response.status === 404) {
  97. throw new Error(`Job not found: ${jobId}`);
  98. }
  99. if (response.status < 200 || response.status >= 300) {
  100. const errorMsg = typeof response.data === 'string' ? response.data : JSON.stringify(response.data);
  101. 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}`);
  102. }
  103. const data = typeof response.data === 'string' ? JSON.parse(response.data) : response.data;
  104. return {
  105. job: data.job,
  106. result: data.result,
  107. usage: data.usage,
  108. timings: data.timings
  109. };
  110. }
  111. };