File size: 7,152 Bytes
3a25f97
 
 
 
 
 
 
 
 
 
 
 
 
a039220
3a25f97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff234fa
a039220
 
 
 
 
 
 
 
 
 
 
 
 
3a25f97
 
 
ddf7640
3a25f97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c248ee8
 
 
 
3a25f97
 
 
 
 
 
 
 
 
 
 
 
 
 
c248ee8
 
 
 
 
 
3a25f97
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { existsSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { loadTraskHttpServerConfig } from "@openkotor/config";
import { createLogger } from "@openkotor/core";
import { JsonTraskQueryRepository, resolveDataFile } from "@openkotor/persistence";
import {
  buildBrowserCorsAllowedOrigins,
  createNodeApiHost,
  resolveCorsHeaders,
} from "@openkotor/platform";
import { createChunkSearchProvider } from "@openkotor/retrieval";
import { createResearchWizardClient, setTraskResearchLogSink } from "@openkotor/trask";
import { createTraskHttpRouter, type TraskHttpAuth } from "@openkotor/trask-http";
import express, { type Request, type Response } from "express";

const logger = createLogger("trask-http-server");

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, "..", "..", "..");

const extractBearerToken = (authorization: string | undefined): string | null => {
  if (!authorization || !authorization.startsWith("Bearer ")) return null;
  return authorization.slice("Bearer ".length).trim() || null;
};

const createWebAuth = (
  config: ReturnType<typeof loadTraskHttpServerConfig>,
): TraskHttpAuth<{ id: string; persistQueries?: boolean }> => ({
  requireAuth: (handler) => async (req: Request, res: Response) => {
    if (config.webApiKey) {
      const bearer = extractBearerToken(req.headers.authorization);
      const headerKey =
        typeof req.headers["x-trask-api-key"] === "string" ? req.headers["x-trask-api-key"].trim() : undefined;
      const ok = bearer === config.webApiKey || headerKey === config.webApiKey;
      if (!ok) {
        res.status(401).json({ error: "Invalid or missing API key." });
        return;
      }
      await handler(req, res, { id: config.webDefaultUserId, persistQueries: true });
      return;
    }

    if (config.webAllowAnonymous) {
      // Holocron polls GET /thread after 202 /ask; anonymous must persist queries or research never completes in-browser.
      await handler(req, res, { id: config.webDefaultUserId, persistQueries: true });
      return;
    }

    res.status(401).json({
      error: "Set TRASK_WEB_API_KEY or TRASK_WEB_ALLOW_ANONYMOUS=1 for local development.",
    });
  },
});

const config = loadTraskHttpServerConfig();

// Resolve relative data paths from repo root so they work regardless of which
// directory pnpm launches the process from (e.g. apps/trask-http-server/ vs repo root).
const resolveFromRoot = (p: string) => (path.isAbsolute(p) ? p : path.resolve(repoRoot, p));

const queryRepository = new JsonTraskQueryRepository(resolveDataFile(resolveFromRoot(config.dataDir), "trask-queries.json"));
/** Legacy FileChunkStore queue surface only; Holocron compose uses Chroma retrieve (`TRASK_INDEXER_BASE_URL`). */
const searchProvider = createChunkSearchProvider(resolveFromRoot(config.chunkDir));
const webResearch = createResearchWizardClient(config.researchWizard, config.ai);

const researchLogVerbose = (process.env.TRASK_RESEARCH_LOG_VERBOSE ?? "").trim().toLowerCase();
if (researchLogVerbose === "1" || researchLogVerbose === "true" || researchLogVerbose === "yes") {
  setTraskResearchLogSink((line, level) => {
    if (level === "debug") {
      logger.debug(line);
    } else {
      logger.info(line);
    }
  });
}

const runtime = {
  searchProvider,
  webResearch,
  queryRepository,
};

const app = express();

/** In-memory Spark KV shim so qa-webui static builds stop hammering 404 on `/__spark-kv/*`. */
const sparkKvStore = new Map<string, string>();
const readSparkKvBody = (req: Request): Promise<string> =>
  new Promise((resolve, reject) => {
    const chunks: Buffer[] = [];
    req.on("data", (chunk: Buffer) => chunks.push(chunk));
    req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
    req.on("error", reject);
  });

app.use((req, res, next) => {
  const pathname = (req.path.split("?")[0] ?? "").replace(/\/+$/, "") || "/";
  if (!pathname.startsWith("/__spark-kv")) {
    next();
    return;
  }

  let subpath = pathname.slice("/__spark-kv".length);
  if (subpath.startsWith("/")) {
    subpath = subpath.slice(1);
  }
  const key = subpath ? decodeURIComponent(subpath.split("/")[0]!) : "";

  void (async () => {
    try {
      if (req.method === "GET" && !key) {
        res.setHeader("Content-Type", "application/json");
        res.end(JSON.stringify([...sparkKvStore.keys()]));
        return;
      }
      if (req.method === "GET" && key) {
        const value = sparkKvStore.get(key);
        if (value === undefined) {
          res.status(404).end();
          return;
        }
        res.setHeader("Content-Type", "text/plain");
        res.end(value);
        return;
      }
      if (req.method === "POST" && key) {
        const body = await readSparkKvBody(req);
        sparkKvStore.set(key, body);
        res.status(200).end();
        return;
      }
      if (req.method === "DELETE" && key) {
        sparkKvStore.delete(key);
        res.status(204).end();
        return;
      }
      res.status(405).end();
    } catch {
      res.status(500).end();
    }
  })();
});

app.use(express.json());

const allowedCorsOrigins = buildBrowserCorsAllowedOrigins({
  publicWebOrigin: config.publicWebOrigin,
  localPorts: [5174, 5173, 4174, 4173, 3000],
});

app.use((req, res, next) => {
  const cors = resolveCorsHeaders({ method: req.method, origin: req.headers.origin }, allowedCorsOrigins, {
    allowHeaders: "Content-Type,Authorization,X-Trask-Api-Key",
  });
  for (const [name, value] of Object.entries(cors.headers)) {
    res.setHeader(name, value);
  }
  if (cors.isPreflight) {
    res.sendStatus(204);
    return;
  }
  next();
});

app.use(
  "/api/trask",
  createTraskHttpRouter({
    runtime,
    auth: createWebAuth(config),
  }),
);

app.get("/health", (_req, res) => {
  res.status(200).json({ ok: true, service: "trask-http-server" });
});

const distFromEnv = process.env.TRASK_WEBUI_DIST_PATH?.trim();
const defaultDist = path.join(repoRoot, "apps", "holocron-web", "dist");
const webUiDist = distFromEnv ? path.resolve(distFromEnv) : defaultDist;

if (existsSync(webUiDist)) {
  app.use(express.static(webUiDist));
  app.use((req, res, next) => {
    if (req.method !== "GET" && req.method !== "HEAD") return next();
    if (req.path.startsWith("/api") || req.path.startsWith("/__spark-kv")) return next();
    res.sendFile(path.join(webUiDist, "index.html"));
  });
  logger.info(`Serving Holocron web static files from ${webUiDist}`);
} else {
  logger.warn(`Holocron web dist not found at ${webUiDist}; API-only mode (TRASK_WEBUI_DIST_PATH to override).`);
  app.get("/", (_req, res) => {
    res
      .status(200)
      .type("text/plain")
      .send("Trask HTTP API is running (API-only). Holocron static UI was not bundled; use /api/trask and /health.");
  });
}

const { server, listen } = createNodeApiHost({
  requestListener: app,
  createHub: () => ({}),
});

listen(config.port, () => {
  logger.info(`Trask HTTP API listening on port ${config.port}`);
});

process.on("SIGINT", () => {
  server.close(() => process.exit(0));
});