|
| 1 | +import Anthropic from "@anthropic-ai/sdk"; |
1 | 2 | import { zValidator } from "@hono/zod-validator"; |
2 | 3 | import { Hono } from "hono"; |
3 | 4 | import { z } from "zod"; |
4 | | -import { processDomainList } from "./process-list"; |
5 | | -import { sendResultsEmail } from "./resend"; |
| 5 | +import { fetchDomains } from "./fetch-domains"; |
| 6 | +import { filterDomains } from "./filter-domains"; |
| 7 | +import { sendClaude } from "./send-claude"; |
| 8 | +import { sendClaudeBatch } from "./send-claude-batch"; |
6 | 9 |
|
7 | 10 | type Bindings = { |
8 | 11 | ANTHROPIC_API_KEY: string; |
9 | | - CRON_URL: string; |
| 12 | + REGISTROBR_TXT_URL: string; |
10 | 13 | }; |
11 | 14 |
|
12 | 15 | const app = new Hono<{ Bindings: Bindings }>(); |
13 | 16 |
|
14 | | -export const querySchema = z.object({ |
15 | | - url: z.string().url(), |
16 | | - batchSize: z |
17 | | - .string() |
18 | | - .default("500") |
19 | | - .transform((val) => Number.parseInt(val)), |
20 | | - limit: z |
21 | | - .string() |
22 | | - .optional() |
23 | | - .transform((val) => (val ? Number.parseInt(val) : undefined)), |
24 | | -}); |
| 17 | +app.get("/process-all", async (c) => { |
| 18 | + try { |
| 19 | + const domains = await fetchDomains(c.env.REGISTROBR_TXT_URL); |
25 | 20 |
|
26 | | -const automateSchema = z.object({ |
27 | | - to: z.string().email(), |
28 | | -}); |
| 21 | + // Filter and sort domains |
| 22 | + const filteredDomains = filterDomains(domains); |
29 | 23 |
|
30 | | -app.get("/trigger", zValidator("query", automateSchema), async (c) => { |
31 | | - try { |
32 | | - const { to } = c.req.valid("query"); |
33 | | - // Process domains with default parameters |
34 | | - const result = await processDomainList(c, { limit: 5000, batchSize: 500 }); |
35 | | - // Send email with the results |
36 | | - await sendResultsEmail(c, to, result); |
| 24 | + const batchId = await sendClaudeBatch(c, filteredDomains); |
37 | 25 |
|
38 | 26 | return c.json({ |
39 | | - message: "Email sent successfully", |
40 | | - metadata: result.metadata, |
| 27 | + message: "Batch sent successfully", |
| 28 | + batchId, |
41 | 29 | }); |
42 | 30 | } catch (error: unknown) { |
43 | 31 | console.error("Error in automation:", error); |
44 | 32 | const errorMessage = |
45 | 33 | error instanceof Error ? error.message : "Unknown error occurred"; |
46 | 34 | return c.json( |
47 | 35 | { |
48 | | - error: "Failed to process automation", |
| 36 | + error: "Failed to process batch", |
49 | 37 | message: errorMessage, |
50 | 38 | }, |
51 | 39 | 500, |
52 | 40 | ); |
53 | 41 | } |
54 | 42 | }); |
55 | 43 |
|
56 | | -app.get("/", zValidator("query", querySchema), async (c) => { |
| 44 | +app.get("/list-batches", async (c) => { |
| 45 | + const anthropic = new Anthropic({ |
| 46 | + apiKey: c.env.ANTHROPIC_API_KEY, |
| 47 | + }); |
| 48 | + const results = await anthropic.messages.batches.list(); |
| 49 | + return c.json(results); |
| 50 | +}); |
| 51 | + |
| 52 | +const pageSchema = z.object({ |
| 53 | + page: z.coerce.number().min(1), |
| 54 | +}); |
| 55 | + |
| 56 | +app.get("/process-page", zValidator("query", pageSchema), async (c) => { |
57 | 57 | try { |
58 | | - const { url, batchSize, limit } = c.req.valid("query"); |
59 | | - const decodedUrl = new URL(decodeURIComponent(url)).toString(); |
| 58 | + const { page } = c.req.valid("query"); |
| 59 | + const PAGE_SIZE = 500; |
| 60 | + const startIndex = (page - 1) * PAGE_SIZE; |
| 61 | + const endIndex = startIndex + PAGE_SIZE; |
60 | 62 |
|
61 | | - const result = await processDomainList(c, { |
62 | | - url: decodedUrl, |
63 | | - batchSize, |
64 | | - limit, |
65 | | - }); |
| 63 | + const domains = await fetchDomains(c.env.REGISTROBR_TXT_URL); |
| 64 | + const filteredDomains = filterDomains(domains); |
| 65 | + const paginatedDomains = filteredDomains.slice(startIndex, endIndex); |
66 | 66 |
|
67 | | - return c.json(result); |
| 67 | + const results = await sendClaude(c, paginatedDomains); |
| 68 | + |
| 69 | + return c.json({ |
| 70 | + message: "Selected domains by AI:", |
| 71 | + results, |
| 72 | + }); |
68 | 73 | } catch (error: unknown) { |
69 | | - console.error("Error processing domains:", error); |
70 | | - const errorMessage = |
71 | | - error instanceof Error ? error.message : "Unknown error occurred"; |
| 74 | + console.error("Error in automation:", error); |
72 | 75 | return c.json( |
73 | 76 | { |
74 | | - error: "Failed to process domains", |
75 | | - message: errorMessage, |
| 77 | + error: "Failed to process batch", |
| 78 | + message: error, |
76 | 79 | }, |
77 | 80 | 500, |
78 | 81 | ); |
79 | 82 | } |
80 | 83 | }); |
81 | 84 |
|
82 | | -export default { |
83 | | - fetch: app.fetch, |
84 | | - scheduled: async ( |
85 | | - event: ScheduledEvent, |
86 | | - env: Bindings, |
87 | | - ctx: ExecutionContext, |
88 | | - ) => { |
89 | | - const response = await fetch(env.CRON_URL); |
90 | | - if (!response.ok) { |
91 | | - console.error("Scheduled trigger failed:", await response.text()); |
92 | | - } |
93 | | - }, |
94 | | -}; |
| 85 | +export default app; |
0 commit comments