File size: 4,683 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Response } from "express";
import { config } from "../../config";
import { RequestWithAuth } from "./types";
import {
  getExtract,
  getExtractExpiry,
  getExtractResult,
} from "../../lib/extract/extract-redis";
import {
  supabaseGetAgentByIdDirect,
  supabaseGetExtractByIdDirect,
  supabaseGetExtractRequestByIdDirect,
} from "../../lib/supabase-jobs";
import { logger as _logger } from "../../lib/logger";
import { getJobFromGCS } from "../../lib/gcs-jobs";

async function getExtractData(id: string): Promise<any> {
  // Try GCS first if configured
  if (config.GCS_BUCKET_NAME) {
    const gcsData = await getJobFromGCS(id);
    if (gcsData) {
      return Array.isArray(gcsData) ? gcsData[0] : gcsData;
    }
  }
  // Fallback to Redis
  const redisData = await getExtractResult(id);
  if (redisData) {
    return Array.isArray(redisData) ? redisData[0] : redisData;
  }
  return [];
}

export async function extractStatusController(
  req: RequestWithAuth<{ jobId: string }, any, any>,
  res: Response,
) {
  const extractRequest = config.USE_DB_AUTHENTICATION
    ? await supabaseGetExtractRequestByIdDirect(req.params.jobId)
    : null;
  if (config.USE_DB_AUTHENTICATION) {
    if (!extractRequest || extractRequest.team_id !== req.auth.team_id) {
      return res.status(404).json({
        success: false,
        error: "Extract job not found",
      });
    }

    if (extractRequest.kind === "agent") {
      const agent = await supabaseGetAgentByIdDirect(req.params.jobId);

      let data: any = undefined;
      if (agent?.is_successful) {
        data = await getJobFromGCS(agent.id);
      }

      return res.status(200).json({
        success: true,
        status: !agent
          ? "processing"
          : agent.is_successful
            ? "completed"
            : "failed",
        error: agent?.error || undefined,
        data,
        expiresAt: new Date(
          new Date(agent?.created_at ?? extractRequest.created_at).getTime() +
            1000 * 60 * 60 * 24,
        ).toISOString(),
        creditsUsed: agent?.credits_cost,
      });
    }
  }

  // Get extract status from Redis (for in-progress jobs)
  const redisExtract = await getExtract(req.params.jobId);

  // If not in Redis, check the database for completed jobs
  if (!redisExtract) {
    if (config.USE_DB_AUTHENTICATION) {
      const dbExtract = await supabaseGetExtractByIdDirect(req.params.jobId);
      if (dbExtract) {
        // Get result data
        let data: any = [];
        if (dbExtract.is_successful) {
          data = await getExtractData(req.params.jobId);
        }

        return res.status(200).json({
          success: dbExtract.is_successful,
          data,
          status: dbExtract.is_successful ? "completed" : "failed",
          error: dbExtract.error || undefined,
          expiresAt: new Date(
            new Date(dbExtract.created_at).getTime() + 1000 * 60 * 60 * 24,
          ).toISOString(),
        });
      }
    }

    // Fall back to extractRequest info
    return res.status(200).json({
      success: true,
      data: [],
      status: "processing",
      expiresAt: new Date(
        new Date(extractRequest.created_at).getTime() + 1000 * 60 * 60 * 24,
      ).toISOString(),
    });
  }

  // Get result data if completed
  let data: any = [];
  if (redisExtract.status === "completed") {
    data = await getExtractData(req.params.jobId);
  }

  return res.status(200).json({
    success: redisExtract.status === "failed" ? false : true,
    data,
    status: redisExtract.status,
    error: (() => {
      if (typeof redisExtract.error === "string") return redisExtract.error;
      if (redisExtract.error && typeof redisExtract.error === "object") {
        return typeof redisExtract.error.message === "string"
          ? redisExtract.error.message
          : typeof redisExtract.error.error === "string"
            ? redisExtract.error.error
            : JSON.stringify(redisExtract.error);
      }
      return undefined;
    })(),
    expiresAt: (await getExtractExpiry(req.params.jobId)).toISOString(),
    steps: redisExtract.showSteps ? redisExtract.steps : undefined,
    llmUsage: redisExtract.showLLMUsage ? redisExtract.llmUsage : undefined,
    sources: redisExtract.showSources ? redisExtract.sources : undefined,
    costTracking: redisExtract.showCostTracking
      ? redisExtract.costTracking
      : undefined,
    sessionIds: redisExtract.sessionIds ? redisExtract.sessionIds : undefined,
    tokensUsed: redisExtract.tokensBilled
      ? redisExtract.tokensBilled
      : undefined,
    creditsUsed: redisExtract.creditsBilled
      ? redisExtract.creditsBilled
      : undefined,
  });
}