@axiom-lattice/gateway 2.1.64 → 2.1.66

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/index.mjs CHANGED
@@ -204,6 +204,11 @@ import {
204
204
  agentInstanceManager as agentInstanceManager2
205
205
  } from "@axiom-lattice/core";
206
206
  import { MessageChunkTypes } from "@axiom-lattice/protocols";
207
+ function getUserId(request) {
208
+ const authUser = request.user;
209
+ if (authUser?.id) return authUser.id;
210
+ return request.headers["x-user-id"] || void 0;
211
+ }
207
212
  var createRun = async (request, reply) => {
208
213
  try {
209
214
  const {
@@ -221,6 +226,8 @@ var createRun = async (request, reply) => {
221
226
  const workspace_id = request.headers["x-workspace-id"];
222
227
  const project_id = request.headers["x-project-id"];
223
228
  const x_request_id = request.headers["x-request-id"] || v4();
229
+ const user_id = getUserId(request);
230
+ const mergedConfig = user_id ? { ...custom_run_config, user_id } : custom_run_config;
224
231
  if (!assistant_id) {
225
232
  reply.status(400).send({
226
233
  success: false,
@@ -234,7 +241,7 @@ var createRun = async (request, reply) => {
234
241
  tenant_id,
235
242
  workspace_id,
236
243
  project_id,
237
- custom_run_config
244
+ custom_run_config: mergedConfig
238
245
  });
239
246
  if (streaming) {
240
247
  reply.hijack();
@@ -249,7 +256,7 @@ var createRun = async (request, reply) => {
249
256
  const result = await agent.addMessage({
250
257
  input: messageInput,
251
258
  command,
252
- custom_run_config
259
+ custom_run_config: mergedConfig
253
260
  }, mode);
254
261
  const stream = agent.chunkStream(result.messageId, [MessageChunkTypes.MESSAGE_COMPLETED]);
255
262
  for await (const chunk of stream) {
@@ -279,7 +286,7 @@ var createRun = async (request, reply) => {
279
286
  const result = await agent.invoke({
280
287
  input: { message: msg, ...restInputNonStream },
281
288
  command,
282
- custom_run_config
289
+ custom_run_config: mergedConfig
283
290
  });
284
291
  reply.status(200).send({
285
292
  success: true,
@@ -5004,35 +5011,31 @@ var AuthController = class {
5004
5011
  }
5005
5012
  }
5006
5013
  };
5014
+ function extractUserFromAuthHeader(authHeader) {
5015
+ if (!authHeader?.startsWith("Bearer ")) return void 0;
5016
+ const token = authHeader.substring(7);
5017
+ try {
5018
+ const payload = JSON.parse(atob(token));
5019
+ if (payload.exp && payload.exp < Date.now()) return void 0;
5020
+ return {
5021
+ id: payload.userId,
5022
+ tenantId: payload.tenantId
5023
+ };
5024
+ } catch {
5025
+ return void 0;
5026
+ }
5027
+ }
5007
5028
  function registerAuthRoutes(app2, config) {
5008
5029
  const controller = new AuthController(config);
5009
5030
  const authHook = async (request, reply) => {
5010
- const authHeader = request.headers.authorization;
5011
- if (!authHeader || !authHeader.startsWith("Bearer ")) {
5012
- return reply.status(401).send({
5013
- success: false,
5014
- error: "Unauthorized - Missing or invalid token"
5015
- });
5016
- }
5017
- const token = authHeader.substring(7);
5018
- try {
5019
- const payload = JSON.parse(atob(token));
5020
- if (payload.exp && payload.exp < Date.now()) {
5021
- return reply.status(401).send({
5022
- success: false,
5023
- error: "Unauthorized - Token expired"
5024
- });
5025
- }
5026
- request.user = {
5027
- id: payload.userId,
5028
- tenantId: payload.tenantId
5029
- };
5030
- } catch {
5031
+ const user = extractUserFromAuthHeader(request.headers.authorization);
5032
+ if (!user) {
5031
5033
  return reply.status(401).send({
5032
5034
  success: false,
5033
- error: "Unauthorized - Invalid token"
5035
+ error: "Unauthorized"
5034
5036
  });
5035
5037
  }
5038
+ request.user = user;
5036
5039
  };
5037
5040
  app2.post("/api/auth/register", controller.register.bind(controller));
5038
5041
  app2.post("/api/auth/login", controller.login.bind(controller));
@@ -6235,7 +6238,6 @@ import {
6235
6238
  sandboxLatticeManager as sandboxLatticeManager2,
6236
6239
  sqlDatabaseManager as sqlDatabaseManager2,
6237
6240
  getStoreLattice as getStoreLattice13,
6238
- storeLatticeManager,
6239
6241
  agentInstanceManager as agentInstanceManager8,
6240
6242
  createSandboxProvider
6241
6243
  } from "@axiom-lattice/core";
@@ -6279,16 +6281,33 @@ app.addContentTypeParser("application/json", { parseAs: "string" }, function(req
6279
6281
  done(err, void 0);
6280
6282
  }
6281
6283
  });
6284
+ var getHeaderValue = (header) => {
6285
+ if (Array.isArray(header)) {
6286
+ return header[0];
6287
+ }
6288
+ return header;
6289
+ };
6290
+ var PUBLIC_ROUTES = ["/api/auth/login", "/api/auth/register", "/health"];
6291
+ app.addHook("preHandler", async (request, reply) => {
6292
+ const user = extractUserFromAuthHeader(request.headers.authorization);
6293
+ if (user) {
6294
+ request.user = user;
6295
+ return;
6296
+ }
6297
+ const authRequired = process.env.AUTH_REQUIRED === "true";
6298
+ if (!authRequired) return;
6299
+ if (request.method === "OPTIONS") return;
6300
+ if (PUBLIC_ROUTES.some((r) => request.url === r)) return;
6301
+ return reply.status(401).send({
6302
+ success: false,
6303
+ error: "Unauthorized - Missing or invalid token"
6304
+ });
6305
+ });
6282
6306
  app.addHook("onRequest", (request, reply, done) => {
6283
- const getHeaderValue = (header) => {
6284
- if (Array.isArray(header)) {
6285
- return header[0];
6286
- }
6287
- return header;
6288
- };
6289
6307
  const context = {
6290
6308
  "x-tenant-id": getHeaderValue(request.headers["x-tenant-id"]),
6291
- "x-request-id": getHeaderValue(request.headers["x-request-id"])
6309
+ "x-request-id": getHeaderValue(request.headers["x-request-id"]),
6310
+ "x-user-id": getHeaderValue(request.headers["x-user-id"])
6292
6311
  };
6293
6312
  if (loggerLattice.updateContext) {
6294
6313
  loggerLattice.updateContext(context);
@@ -6296,15 +6315,10 @@ app.addHook("onRequest", (request, reply, done) => {
6296
6315
  done();
6297
6316
  });
6298
6317
  app.addHook("onResponse", (request, reply, done) => {
6299
- const getHeaderValue = (header) => {
6300
- if (Array.isArray(header)) {
6301
- return header[0];
6302
- }
6303
- return header;
6304
- };
6305
6318
  const context = {
6306
6319
  "x-tenant-id": getHeaderValue(request.headers["x-tenant-id"]),
6307
- "x-request-id": getHeaderValue(request.headers["x-request-id"])
6320
+ "x-request-id": getHeaderValue(request.headers["x-request-id"]),
6321
+ "x-user-id": getHeaderValue(request.headers["x-user-id"])
6308
6322
  };
6309
6323
  done();
6310
6324
  });
@@ -6329,15 +6343,10 @@ app.register(staticPlugin, {
6329
6343
  prefix: "/"
6330
6344
  });
6331
6345
  app.setErrorHandler((error, request, reply) => {
6332
- const getHeaderValue = (header) => {
6333
- if (Array.isArray(header)) {
6334
- return header[0];
6335
- }
6336
- return header;
6337
- };
6338
6346
  const context = {
6339
6347
  "x-tenant-id": getHeaderValue(request.headers["x-tenant-id"]),
6340
- "x-request-id": getHeaderValue(request.headers["x-request-id"])
6348
+ "x-request-id": getHeaderValue(request.headers["x-request-id"]),
6349
+ "x-user-id": getHeaderValue(request.headers["x-user-id"])
6341
6350
  };
6342
6351
  logger.error(
6343
6352
  `\u8BF7\u6C42\u9519\u8BEF: ${request.method} ${request.url} error:${error.message}`,
@@ -6395,21 +6404,6 @@ var start = async (config) => {
6395
6404
  sandboxLatticeManager2.registerLattice("default", getConfiguredSandboxProvider());
6396
6405
  logger.info("Registered sandbox manager from env configuration");
6397
6406
  }
6398
- if (process.env.DATABASE_URL) {
6399
- try {
6400
- const { PostgreSQLWorkflowTrackingStore } = await import("@axiom-lattice/pg-stores");
6401
- const pgStore = new PostgreSQLWorkflowTrackingStore({
6402
- poolConfig: process.env.DATABASE_URL
6403
- });
6404
- if (storeLatticeManager.hasLattice("default", "workflowTracking")) {
6405
- storeLatticeManager.removeLattice("default", "workflowTracking");
6406
- }
6407
- storeLatticeManager.registerLattice("default", "workflowTracking", pgStore);
6408
- logger.info("Workflow tracking store switched to PostgreSQL");
6409
- } catch (error) {
6410
- logger.warn("Failed to switch workflow tracking to PostgreSQL, keeping in-memory: " + (error instanceof Error ? error.message : String(error)));
6411
- }
6412
- }
6413
6407
  const target_port = config?.port || Number(process.env.PORT) || 4001;
6414
6408
  await app.listen({ port: target_port, host: "0.0.0.0" });
6415
6409
  logger.info(`Lattice Gateway is running on port: ${target_port}`);