Skip to content

Commit e748d8e

Browse files
committed
Refactor from ai sdk to claude
1 parent a4ffc49 commit e748d8e

File tree

11 files changed

+282
-283
lines changed

11 files changed

+282
-283
lines changed

bun.lockb

99 Bytes
Binary file not shown.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
"deploy": "wrangler deploy --minify"
66
},
77
"dependencies": {
8-
"@ai-sdk/anthropic": "^1.0.5",
8+
"@anthropic-ai/sdk": "^0.33.1",
99
"@hono/zod-validator": "^0.4.1",
1010
"@react-email/components": "0.0.31",
1111
"@types/react": "^19.0.1",
12-
"ai": "^4.0.14",
1312
"hono": "^4.6.13",
1413
"react": "19.0.0",
1514
"react-dom": "19.0.0",
@@ -18,6 +17,6 @@
1817
"devDependencies": {
1918
"@biomejs/biome": "1.9.4",
2019
"@cloudflare/workers-types": "^4.20241112.0",
21-
"wrangler": "^3.95.0"
20+
"wrangler": "^3.96.0"
2221
}
2322
}

src/fetch-domains.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export async function fetchDomains(url: string): Promise<string[]> {
2+
const response = await fetch(url);
3+
const text = await response.text();
4+
5+
// Split text into lines and filter out comments and empty lines
6+
return text
7+
.split("\n")
8+
.map((line) => line.trim())
9+
.filter((line) => line && !line.startsWith("#"))
10+
.filter(Boolean); // Remove empty lines
11+
}

src/filter-domains.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { VALID_TLD } from "./send-claude-batch";
2+
3+
// Helper function to get domain length without TLD
4+
const getDomainLength = (domain: string) =>
5+
domain.slice(0, -VALID_TLD.length).length;
6+
7+
// Helper function to sort domains by length
8+
const sortByLength = (a: string, b: string) =>
9+
getDomainLength(a) - getDomainLength(b);
10+
11+
export interface DomainWithScore {
12+
domain: string;
13+
score: number;
14+
}
15+
16+
export function filterDomains(domains: string[]): string[] {
17+
return [...new Set(domains)]
18+
.filter((domain) => {
19+
if (!domain.endsWith(VALID_TLD)) return false;
20+
21+
const domainWithoutTLD = domain.slice(0, -VALID_TLD.length);
22+
if (/[\d-]/.test(domainWithoutTLD)) return false;
23+
24+
const length = domainWithoutTLD.length;
25+
return length >= 1 && length <= 9;
26+
})
27+
.sort(sortByLength);
28+
}

src/index.ts

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,85 @@
1+
import Anthropic from "@anthropic-ai/sdk";
12
import { zValidator } from "@hono/zod-validator";
23
import { Hono } from "hono";
34
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";
69

710
type Bindings = {
811
ANTHROPIC_API_KEY: string;
9-
CRON_URL: string;
12+
REGISTROBR_TXT_URL: string;
1013
};
1114

1215
const app = new Hono<{ Bindings: Bindings }>();
1316

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);
2520

26-
const automateSchema = z.object({
27-
to: z.string().email(),
28-
});
21+
// Filter and sort domains
22+
const filteredDomains = filterDomains(domains);
2923

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);
3725

3826
return c.json({
39-
message: "Email sent successfully",
40-
metadata: result.metadata,
27+
message: "Batch sent successfully",
28+
batchId,
4129
});
4230
} catch (error: unknown) {
4331
console.error("Error in automation:", error);
4432
const errorMessage =
4533
error instanceof Error ? error.message : "Unknown error occurred";
4634
return c.json(
4735
{
48-
error: "Failed to process automation",
36+
error: "Failed to process batch",
4937
message: errorMessage,
5038
},
5139
500,
5240
);
5341
}
5442
});
5543

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) => {
5757
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;
6062

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);
6666

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+
});
6873
} 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);
7275
return c.json(
7376
{
74-
error: "Failed to process domains",
75-
message: errorMessage,
77+
error: "Failed to process batch",
78+
message: error,
7679
},
7780
500,
7881
);
7982
}
8083
});
8184

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

Comments
 (0)