@axiom-lattice/gateway 2.1.65 → 2.1.67

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,
@@ -1998,6 +2005,65 @@ async function getRunTasks(request, reply) {
1998
2005
  return reply.status(500).send({ success: false, message: "Failed to retrieve user tasks" });
1999
2006
  }
2000
2007
  }
2008
+ async function replyInboxTask(request, reply) {
2009
+ const { runId, answers } = request.body;
2010
+ const tenantId = getTenantId6(request);
2011
+ try {
2012
+ const store = getTrackingStore();
2013
+ if (!store) {
2014
+ return reply.status(404).send({
2015
+ success: false,
2016
+ message: "No workflow tracking store configured"
2017
+ });
2018
+ }
2019
+ const run = await store.getWorkflowRun(runId);
2020
+ if (!run) {
2021
+ return reply.status(404).send({
2022
+ success: false,
2023
+ message: "Workflow run not found"
2024
+ });
2025
+ }
2026
+ const agent = agentInstanceManager4.getAgent({
2027
+ assistant_id: run.assistantId,
2028
+ thread_id: run.threadId,
2029
+ tenant_id: tenantId
2030
+ });
2031
+ try {
2032
+ await agent.addMessage({
2033
+ input: { message: "Clarification answers submitted" },
2034
+ command: {
2035
+ resume: {
2036
+ action: "submit",
2037
+ data: { answers },
2038
+ message: "Clarification answers submitted"
2039
+ }
2040
+ }
2041
+ });
2042
+ } catch (error) {
2043
+ return reply.status(400).send({
2044
+ success: false,
2045
+ message: "Failed to resume workflow",
2046
+ error: error instanceof Error ? error.message : String(error)
2047
+ });
2048
+ }
2049
+ return reply.status(200).send({
2050
+ success: true,
2051
+ message: "Resume request submitted successfully",
2052
+ data: {
2053
+ runId: run.id,
2054
+ assistantId: run.assistantId,
2055
+ threadId: run.threadId,
2056
+ status: "resuming"
2057
+ }
2058
+ });
2059
+ } catch (error) {
2060
+ request.log.error(error, "Failed to reply inbox task");
2061
+ return reply.status(500).send({
2062
+ success: false,
2063
+ message: "Failed to submit resume request"
2064
+ });
2065
+ }
2066
+ }
2001
2067
 
2002
2068
  // src/schemas/data-query.ts
2003
2069
  var dataQuerySchema = {
@@ -5004,35 +5070,31 @@ var AuthController = class {
5004
5070
  }
5005
5071
  }
5006
5072
  };
5073
+ function extractUserFromAuthHeader(authHeader) {
5074
+ if (!authHeader?.startsWith("Bearer ")) return void 0;
5075
+ const token = authHeader.substring(7);
5076
+ try {
5077
+ const payload = JSON.parse(atob(token));
5078
+ if (payload.exp && payload.exp < Date.now()) return void 0;
5079
+ return {
5080
+ id: payload.userId,
5081
+ tenantId: payload.tenantId
5082
+ };
5083
+ } catch {
5084
+ return void 0;
5085
+ }
5086
+ }
5007
5087
  function registerAuthRoutes(app2, config) {
5008
5088
  const controller = new AuthController(config);
5009
5089
  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 {
5090
+ const user = extractUserFromAuthHeader(request.headers.authorization);
5091
+ if (!user) {
5031
5092
  return reply.status(401).send({
5032
5093
  success: false,
5033
- error: "Unauthorized - Invalid token"
5094
+ error: "Unauthorized"
5034
5095
  });
5035
5096
  }
5097
+ request.user = user;
5036
5098
  };
5037
5099
  app2.post("/api/auth/register", controller.register.bind(controller));
5038
5100
  app2.post("/api/auth/login", controller.login.bind(controller));
@@ -5901,6 +5963,7 @@ var registerLatticeRoutes = (app2) => {
5901
5963
  app2.get("/api/workflows/runs/:runId", getWorkflowRun);
5902
5964
  app2.get("/api/workflows/runs/:runId/steps", getRunSteps);
5903
5965
  app2.get("/api/workflows/runs/:runId/tasks", getRunTasks);
5966
+ app2.post("/api/workflows/inbox/reply", replyInboxTask);
5904
5967
  app2.delete(
5905
5968
  "/api/assistants/:assistant_id/threads/:thread_id/pending-messages/:message_id",
5906
5969
  removePendingMessageHandler
@@ -6235,7 +6298,6 @@ import {
6235
6298
  sandboxLatticeManager as sandboxLatticeManager2,
6236
6299
  sqlDatabaseManager as sqlDatabaseManager2,
6237
6300
  getStoreLattice as getStoreLattice13,
6238
- storeLatticeManager,
6239
6301
  agentInstanceManager as agentInstanceManager8,
6240
6302
  createSandboxProvider
6241
6303
  } from "@axiom-lattice/core";
@@ -6279,16 +6341,33 @@ app.addContentTypeParser("application/json", { parseAs: "string" }, function(req
6279
6341
  done(err, void 0);
6280
6342
  }
6281
6343
  });
6344
+ var getHeaderValue = (header) => {
6345
+ if (Array.isArray(header)) {
6346
+ return header[0];
6347
+ }
6348
+ return header;
6349
+ };
6350
+ var PUBLIC_ROUTES = ["/api/auth/login", "/api/auth/register", "/health"];
6351
+ app.addHook("preHandler", async (request, reply) => {
6352
+ const user = extractUserFromAuthHeader(request.headers.authorization);
6353
+ if (user) {
6354
+ request.user = user;
6355
+ return;
6356
+ }
6357
+ const authRequired = process.env.AUTH_REQUIRED === "true";
6358
+ if (!authRequired) return;
6359
+ if (request.method === "OPTIONS") return;
6360
+ if (PUBLIC_ROUTES.some((r) => request.url === r)) return;
6361
+ return reply.status(401).send({
6362
+ success: false,
6363
+ error: "Unauthorized - Missing or invalid token"
6364
+ });
6365
+ });
6282
6366
  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
6367
  const context = {
6290
6368
  "x-tenant-id": getHeaderValue(request.headers["x-tenant-id"]),
6291
- "x-request-id": getHeaderValue(request.headers["x-request-id"])
6369
+ "x-request-id": getHeaderValue(request.headers["x-request-id"]),
6370
+ "x-user-id": getHeaderValue(request.headers["x-user-id"])
6292
6371
  };
6293
6372
  if (loggerLattice.updateContext) {
6294
6373
  loggerLattice.updateContext(context);
@@ -6296,15 +6375,10 @@ app.addHook("onRequest", (request, reply, done) => {
6296
6375
  done();
6297
6376
  });
6298
6377
  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
6378
  const context = {
6306
6379
  "x-tenant-id": getHeaderValue(request.headers["x-tenant-id"]),
6307
- "x-request-id": getHeaderValue(request.headers["x-request-id"])
6380
+ "x-request-id": getHeaderValue(request.headers["x-request-id"]),
6381
+ "x-user-id": getHeaderValue(request.headers["x-user-id"])
6308
6382
  };
6309
6383
  done();
6310
6384
  });
@@ -6329,15 +6403,10 @@ app.register(staticPlugin, {
6329
6403
  prefix: "/"
6330
6404
  });
6331
6405
  app.setErrorHandler((error, request, reply) => {
6332
- const getHeaderValue = (header) => {
6333
- if (Array.isArray(header)) {
6334
- return header[0];
6335
- }
6336
- return header;
6337
- };
6338
6406
  const context = {
6339
6407
  "x-tenant-id": getHeaderValue(request.headers["x-tenant-id"]),
6340
- "x-request-id": getHeaderValue(request.headers["x-request-id"])
6408
+ "x-request-id": getHeaderValue(request.headers["x-request-id"]),
6409
+ "x-user-id": getHeaderValue(request.headers["x-user-id"])
6341
6410
  };
6342
6411
  logger.error(
6343
6412
  `\u8BF7\u6C42\u9519\u8BEF: ${request.method} ${request.url} error:${error.message}`,
@@ -6395,21 +6464,6 @@ var start = async (config) => {
6395
6464
  sandboxLatticeManager2.registerLattice("default", getConfiguredSandboxProvider());
6396
6465
  logger.info("Registered sandbox manager from env configuration");
6397
6466
  }
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
6467
  const target_port = config?.port || Number(process.env.PORT) || 4001;
6414
6468
  await app.listen({ port: target_port, host: "0.0.0.0" });
6415
6469
  logger.info(`Lattice Gateway is running on port: ${target_port}`);