curriculum-api 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js ADDED
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ adminLoginAttempts,
4
+ createDb,
5
+ migrate,
6
+ publicUsage,
7
+ seed
8
+ } from "./chunk-RL62MWPL.js";
9
+
10
+ // src/cli.ts
11
+ import { realpathSync } from "fs";
12
+ import { fileURLToPath } from "url";
13
+ import bcrypt from "bcryptjs";
14
+
15
+ // src/db/cleanup.ts
16
+ import { lt, sql } from "drizzle-orm";
17
+ async function cleanup(databaseUrl, days = 30) {
18
+ const db = createDb(databaseUrl);
19
+ const usage = await db.delete(publicUsage).where(lt(publicUsage.day, sql`(current_date - ${days}::int)`)).returning({ ipHash: publicUsage.ipHash });
20
+ const attempts = await db.delete(adminLoginAttempts).where(lt(adminLoginAttempts.windowStart, sql`(now() - make_interval(days => ${days}::int))`)).returning({ ipHash: adminLoginAttempts.ipHash });
21
+ return { publicUsage: usage.length, loginAttempts: attempts.length };
22
+ }
23
+
24
+ // src/cli.ts
25
+ var USAGE = `Usage: curriculum-api <command>
26
+
27
+ Commands:
28
+ migrate apply database migrations (uses DATABASE_URL_UNPOOLED, falling back to DATABASE_URL)
29
+ seed create the profile row with default prompts (needs DATABASE_URL)
30
+ hash-password read a password from stdin and print its bcrypt hash
31
+ cleanup delete public_usage and admin_login_attempts rows older than 30 days (needs DATABASE_URL)`;
32
+ var BCRYPT_COST = 12;
33
+ async function runCli(argv, io) {
34
+ const [command] = argv;
35
+ const databaseUrl = (preferred) => {
36
+ const url = preferred || io.env.DATABASE_URL;
37
+ if (!url) io.err("DATABASE_URL is required");
38
+ return url;
39
+ };
40
+ switch (command) {
41
+ case "migrate": {
42
+ const url = databaseUrl(io.env.DATABASE_URL_UNPOOLED);
43
+ if (!url) return 1;
44
+ await migrate(url);
45
+ io.out("migrations applied");
46
+ return 0;
47
+ }
48
+ case "seed": {
49
+ const url = databaseUrl();
50
+ if (!url) return 1;
51
+ io.out(`profile ${await seed(url)}`);
52
+ return 0;
53
+ }
54
+ case "hash-password": {
55
+ const password = (await io.readStdin()).replace(/\r?\n$/, "");
56
+ if (!password) {
57
+ io.err("password is required on stdin");
58
+ return 1;
59
+ }
60
+ io.out(await bcrypt.hash(password, BCRYPT_COST));
61
+ return 0;
62
+ }
63
+ case "cleanup": {
64
+ const url = databaseUrl();
65
+ if (!url) return 1;
66
+ const counts = await cleanup(url);
67
+ io.out(`public_usage: ${counts.publicUsage}`);
68
+ io.out(`admin_login_attempts: ${counts.loginAttempts}`);
69
+ return 0;
70
+ }
71
+ default:
72
+ io.err(USAGE);
73
+ return 1;
74
+ }
75
+ }
76
+ async function readStdin() {
77
+ const chunks = [];
78
+ for await (const chunk of process.stdin) chunks.push(chunk);
79
+ return Buffer.concat(chunks).toString("utf8");
80
+ }
81
+ function isMain() {
82
+ const entry = process.argv[1];
83
+ if (!entry) return false;
84
+ try {
85
+ return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
86
+ } catch {
87
+ return false;
88
+ }
89
+ }
90
+ if (isMain()) {
91
+ runCli(process.argv.slice(2), {
92
+ env: process.env,
93
+ readStdin,
94
+ out: (line) => console.log(line),
95
+ err: (line) => console.error(line)
96
+ }).then(
97
+ (code) => process.exit(code),
98
+ (error) => {
99
+ console.error(error instanceof Error ? error.message : error);
100
+ process.exit(1);
101
+ }
102
+ );
103
+ }
104
+ export {
105
+ runCli
106
+ };
@@ -0,0 +1,133 @@
1
+ import { Hono } from 'hono';
2
+
3
+ interface CurriculumApiConfig {
4
+ databaseUrl: string;
5
+ openrouterApiKey: string;
6
+ /** At least 32 bytes, used for JWT signing. */
7
+ sessionSecret: string;
8
+ turnstileSecret: string;
9
+ /** bcrypt hash of the admin password. */
10
+ adminPasswordHash: string;
11
+ /** Salt for hashing IPs (privacy). */
12
+ ipHashSalt: string;
13
+ /** 32 bytes, base64: AES-256-GCM key for personal info values at rest. */
14
+ piiEncryptionKey: string;
15
+ /** Default "": routes at /api/* and /admin/*. */
16
+ basePath?: string;
17
+ /** Default true; false for local http development. */
18
+ secureCookies?: boolean;
19
+ /** OpenRouter model IDs; the settings table overrides them. */
20
+ models?: {
21
+ public?: string;
22
+ tailor?: string;
23
+ recruiter?: string;
24
+ };
25
+ limits?: Partial<Limits>;
26
+ }
27
+ interface Limits {
28
+ publicQuestionsPerIpPerDay: number;
29
+ recruiterQuestionsDefault: number;
30
+ publicMaxOutputTokens: number;
31
+ recruiterMaxOutputTokens: number;
32
+ maxQuestionChars: number;
33
+ maxHistoryMessages: number;
34
+ sessionTtlMinutes: number;
35
+ }
36
+ interface ResolvedConfig {
37
+ databaseUrl: string;
38
+ openrouterApiKey: string;
39
+ sessionSecret: string;
40
+ turnstileSecret: string;
41
+ adminPasswordHash: string;
42
+ ipHashSalt: string;
43
+ piiEncryptionKey: string;
44
+ basePath: string;
45
+ secureCookies: boolean;
46
+ models: {
47
+ public?: string;
48
+ tailor?: string;
49
+ recruiter?: string;
50
+ };
51
+ limits: Limits;
52
+ }
53
+
54
+ /** Validates the config and builds the app. Performs no I/O. */
55
+ declare function createApp(config: CurriculumApiConfig): Hono;
56
+
57
+ declare function migrate(databaseUrl: string): Promise<void>;
58
+
59
+ /** Creates the profile row with empty CV fields and the default prompts; leaves an existing row alone. */
60
+ declare function seed(databaseUrl: string): Promise<"created" | "exists">;
61
+
62
+ /** Domain errors carry their HTTP status so the API and admin handlers share one mapping. */
63
+ declare abstract class DomainError extends Error {
64
+ abstract readonly code: string;
65
+ abstract readonly status: number;
66
+ }
67
+ declare class ValidationError extends DomainError {
68
+ readonly fields?: Record<string, string> | undefined;
69
+ readonly code = "validation_error";
70
+ readonly status = 400;
71
+ constructor(message?: string, fields?: Record<string, string> | undefined);
72
+ }
73
+ declare class UnauthorizedError extends DomainError {
74
+ readonly code = "unauthorized";
75
+ readonly status = 401;
76
+ constructor(message?: string);
77
+ }
78
+ declare class ForbiddenError extends DomainError {
79
+ readonly code = "forbidden";
80
+ readonly status = 403;
81
+ constructor(message?: string);
82
+ }
83
+ declare class CaptchaFailedError extends DomainError {
84
+ readonly code = "captcha_failed";
85
+ readonly status = 403;
86
+ constructor(message?: string);
87
+ }
88
+ declare class NotFoundError extends DomainError {
89
+ readonly code = "not_found";
90
+ readonly status = 404;
91
+ constructor(message?: string);
92
+ }
93
+ declare class ChatRevokedError extends DomainError {
94
+ readonly code = "chat_revoked";
95
+ readonly status = 410;
96
+ constructor(message?: string);
97
+ }
98
+ declare class ChatExpiredError extends DomainError {
99
+ readonly code = "chat_expired";
100
+ readonly status = 410;
101
+ constructor(message?: string);
102
+ }
103
+ declare class LimitReachedError extends DomainError {
104
+ readonly code = "limit_reached";
105
+ readonly status = 429;
106
+ constructor(message?: string);
107
+ }
108
+ declare class LlmUnavailableError extends DomainError {
109
+ readonly code = "llm_unavailable";
110
+ readonly status = 502;
111
+ constructor(message?: string);
112
+ }
113
+ /** The message never includes the leaked value; only the field key. */
114
+ declare class PiiLeakError extends DomainError {
115
+ readonly fieldKey: string;
116
+ readonly code = "internal_error";
117
+ readonly status = 500;
118
+ constructor(fieldKey: string);
119
+ }
120
+ declare class ConfigError extends DomainError {
121
+ readonly fields: string[];
122
+ readonly code = "config_error";
123
+ readonly status = 500;
124
+ constructor(fields: string[]);
125
+ }
126
+ declare class DecryptionError extends DomainError {
127
+ readonly code = "internal_error";
128
+ readonly status = 500;
129
+ constructor(message?: string);
130
+ }
131
+
132
+ export { CaptchaFailedError, ChatExpiredError, ChatRevokedError, ConfigError, DecryptionError, DomainError, ForbiddenError, LimitReachedError, LlmUnavailableError, NotFoundError, PiiLeakError, UnauthorizedError, ValidationError, createApp, migrate, seed };
133
+ export type { CurriculumApiConfig, Limits, ResolvedConfig };