@axiom-lattice/gateway 2.1.22 → 2.1.23

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @axiom-lattice/gateway@2.1.22 build /home/runner/work/agentic/agentic/packages/gateway
2
+ > @axiom-lattice/gateway@2.1.23 build /home/runner/work/agentic/agentic/packages/gateway
3
3
  > tsup src/index.ts --format cjs,esm --dts --clean --sourcemap
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -9,13 +9,13 @@
9
9
  CLI Cleaning output folder
10
10
  CJS Build start
11
11
  ESM Build start
12
- ESM dist/index.mjs 79.68 KB
13
- ESM dist/index.mjs.map 189.84 KB
14
- ESM ⚡️ Build success in 174ms
15
- CJS dist/index.js 82.25 KB
16
- CJS dist/index.js.map 189.88 KB
17
- CJS ⚡️ Build success in 174ms
12
+ ESM dist/index.mjs 83.88 KB
13
+ ESM dist/index.mjs.map 200.51 KB
14
+ ESM ⚡️ Build success in 221ms
15
+ CJS dist/index.js 86.51 KB
16
+ CJS dist/index.js.map 200.54 KB
17
+ CJS ⚡️ Build success in 223ms
18
18
  DTS Build start
19
- DTS ⚡️ Build success in 8703ms
19
+ DTS ⚡️ Build success in 9197ms
20
20
  DTS dist/index.d.ts 3.72 KB
21
21
  DTS dist/index.d.mts 3.72 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @axiom-lattice/gateway
2
2
 
3
+ ## 2.1.23
4
+
5
+ ### Patch Changes
6
+
7
+ - e0a5f54: enhance file
8
+ - Updated dependencies [e0a5f54]
9
+ - @axiom-lattice/core@2.1.18
10
+
3
11
  ## 2.1.22
4
12
 
5
13
  ### Patch Changes
package/dist/index.js CHANGED
@@ -35,6 +35,7 @@ __export(index_exports, {
35
35
  module.exports = __toCommonJS(index_exports);
36
36
  var import_fastify = __toESM(require("fastify"));
37
37
  var import_cors = __toESM(require("@fastify/cors"));
38
+ var import_multipart = __toESM(require("@fastify/multipart"));
38
39
  var import_sensible = __toESM(require("@fastify/sensible"));
39
40
  var import_websocket = __toESM(require("@fastify/websocket"));
40
41
 
@@ -85,6 +86,7 @@ async function agent_invoke({
85
86
  "x-tenant-id": tenant_id,
86
87
  "x-request-id": run_id,
87
88
  "x-thread-id": thread_id,
89
+ "x-assistant-id": assistant_id,
88
90
  runConfig
89
91
  // Inject runConfig for tools to access
90
92
  },
@@ -139,6 +141,7 @@ async function agent_stream({
139
141
  "x-tenant-id": tenant_id,
140
142
  "x-request-id": run_id,
141
143
  "x-thread-id": thread_id,
144
+ "x-assistant-id": assistant_id,
142
145
  runConfig
143
146
  // Inject runConfig for tools to access
144
147
  },
@@ -2006,9 +2009,11 @@ var getHealthSchema = {
2006
2009
  }
2007
2010
  };
2008
2011
 
2012
+ // src/controllers/sandbox.ts
2013
+ var import_stream = require("stream");
2014
+
2009
2015
  // src/services/sandbox_service.ts
2010
2016
  var import_core12 = require("@axiom-lattice/core");
2011
- var SANDBOX_BASE_URL = process.env.SANDBOX_BASE_URL || "http://localhost:8080";
2012
2017
  var ERROR_HTML = `<!DOCTYPE html>
2013
2018
  <html lang="zh-CN">
2014
2019
  <head>
@@ -2119,9 +2124,6 @@ var ERROR_HTML = `<!DOCTYPE html>
2119
2124
  </body>
2120
2125
  </html>`;
2121
2126
  var SandboxService = class {
2122
- constructor(baseUrl) {
2123
- this.baseUrl = baseUrl || SANDBOX_BASE_URL;
2124
- }
2125
2127
  getSandboxConfig(assistantId) {
2126
2128
  const agentConfig = (0, import_core12.getAgentConfig)(assistantId);
2127
2129
  if (!agentConfig) {
@@ -2147,7 +2149,8 @@ var SandboxService = class {
2147
2149
  return (0, import_core12.normalizeSandboxName)(sandboxName);
2148
2150
  }
2149
2151
  getTargetUrl(sandboxName) {
2150
- return `${this.baseUrl}/sandbox/${sandboxName}`;
2152
+ const sandboxManager = (0, import_core12.getSandBoxManager)("default");
2153
+ return `${sandboxManager.getBaseURL()}/sandbox/${sandboxName}`;
2151
2154
  }
2152
2155
  async getVncHtml(sandboxName) {
2153
2156
  const response = await fetch(`${this.getTargetUrl(sandboxName)}/vnc/index.html`);
@@ -2191,21 +2194,41 @@ var SandboxService = class {
2191
2194
  var sandboxService = new SandboxService();
2192
2195
 
2193
2196
  // src/controllers/sandbox.ts
2194
- var SANDBOX_BASE_URL2 = process.env.SANDBOX_BASE_URL || "http://localhost:8080";
2195
- async function registerSandboxProxyRoutes(app2) {
2196
- app2.get(
2197
- "/api/assistants/:assistantId/threads/:threadId/sandbox",
2197
+ var import_core13 = require("@axiom-lattice/core");
2198
+ function getFilenameFromPath(path) {
2199
+ const segments = path.replace(/\/+$/, "").split("/");
2200
+ return segments[segments.length - 1] || "download";
2201
+ }
2202
+ var EXT_TO_MIME = {
2203
+ ".txt": "text/plain",
2204
+ ".html": "text/html",
2205
+ ".css": "text/css",
2206
+ ".js": "application/javascript",
2207
+ ".json": "application/json",
2208
+ ".pdf": "application/pdf",
2209
+ ".png": "image/png",
2210
+ ".jpg": "image/jpeg",
2211
+ ".jpeg": "image/jpeg",
2212
+ ".gif": "image/gif",
2213
+ ".webp": "image/webp",
2214
+ ".svg": "image/svg+xml",
2215
+ ".zip": "application/zip",
2216
+ ".csv": "text/csv",
2217
+ ".xml": "application/xml"
2218
+ };
2219
+ function getContentTypeFromFilename(filename) {
2220
+ const ext = filename.includes(".") ? filename.slice(filename.lastIndexOf(".")).toLowerCase() : "";
2221
+ return EXT_TO_MIME[ext] ?? "application/octet-stream";
2222
+ }
2223
+ function registerSandboxProxyRoutes(app2) {
2224
+ app2.post(
2225
+ "/api/assistants/:assistantId/threads/:threadId/sandbox/uploadfile",
2198
2226
  async (request, reply) => {
2227
+ console.log("[Sandbox Upload] Route matched:", request.url);
2199
2228
  const { assistantId, threadId } = request.params;
2200
2229
  const sandboxConfig = sandboxService.getSandboxConfig(assistantId);
2201
2230
  if (!sandboxConfig) {
2202
- const errorHtml = sandboxService.generateErrorHtml(
2203
- assistantId,
2204
- threadId,
2205
- "unknown",
2206
- `Assistant ${assistantId} not found`
2207
- );
2208
- return reply.status(404).type("text/html").send(errorHtml);
2231
+ return reply.status(500).send({ error: "Assistant sandbox config not found" });
2209
2232
  }
2210
2233
  const { isolatedLevel } = sandboxConfig;
2211
2234
  const sandboxName = sandboxService.computeSandboxName(
@@ -2213,28 +2236,46 @@ async function registerSandboxProxyRoutes(app2) {
2213
2236
  threadId,
2214
2237
  isolatedLevel
2215
2238
  );
2239
+ const sandboxManager = (0, import_core13.getSandBoxManager)("default");
2240
+ const sandbox = await sandboxManager.createSandbox(sandboxName);
2216
2241
  try {
2217
- const html = await sandboxService.getVncHtml(sandboxName);
2218
- const rewrittenHtml = sandboxService.rewriteHtml(html, assistantId, threadId);
2219
- return reply.type("text/html").send(rewrittenHtml);
2242
+ const data = await request.file();
2243
+ if (!data) {
2244
+ return reply.status(400).send({ error: "No file in request" });
2245
+ }
2246
+ const buffer = await data.toBuffer();
2247
+ const pathEntry = data.fields?.path;
2248
+ const pathValue = pathEntry && typeof pathEntry === "object" && "value" in pathEntry ? String(pathEntry.value) : typeof pathEntry === "string" ? pathEntry : void 0;
2249
+ const formData = new FormData();
2250
+ formData.append("file", new Blob([buffer]), data.filename ?? "file");
2251
+ const path = `/home/gem/uploads/${pathValue ? pathValue : ""}${data.filename}`;
2252
+ const uploadResult = await sandbox.file.uploadFile({
2253
+ file: buffer,
2254
+ path
2255
+ });
2256
+ if (!uploadResult.ok) {
2257
+ return reply.status(502).send({ error: `Upload error: ${uploadResult.error}` });
2258
+ }
2259
+ const relativePath = uploadResult.body?.data?.file_path.replace(`/home/gem`, "");
2260
+ const result = { id: relativePath, name: data.filename, size: buffer.length };
2261
+ return reply.status(200).send({ message: "File uploaded successfully", ...result });
2220
2262
  } catch (error) {
2221
- const errorHtml = sandboxService.generateErrorHtml(
2222
- assistantId,
2223
- threadId,
2224
- isolatedLevel,
2225
- error.message || "Failed to connect to sandbox"
2226
- );
2227
- return reply.status(502).type("text/html").send(errorHtml);
2263
+ const message = error instanceof Error ? error.message : String(error);
2264
+ return reply.status(502).send({ error: `Upload proxy error: ${message}` });
2228
2265
  }
2229
2266
  }
2230
2267
  );
2231
2268
  app2.get(
2232
- "/api/assistants/:assistantId/threads/:threadId/sandbox/vnc/*",
2269
+ "/api/assistants/:assistantId/threads/:threadId/sandbox/downloadfile",
2233
2270
  async (request, reply) => {
2234
- const { assistantId, threadId, "*": restPath } = request.params;
2271
+ const { assistantId, threadId } = request.params;
2272
+ const { path: filePath } = request.query;
2273
+ if (!filePath || typeof filePath !== "string") {
2274
+ return reply.status(400).send({ error: "Query parameter 'path' is required" });
2275
+ }
2235
2276
  const sandboxConfig = sandboxService.getSandboxConfig(assistantId);
2236
2277
  if (!sandboxConfig) {
2237
- return reply.status(404).send("Assistant not found");
2278
+ return reply.status(404).send({ error: "Assistant sandbox config not found" });
2238
2279
  }
2239
2280
  const { isolatedLevel } = sandboxConfig;
2240
2281
  const sandboxName = sandboxService.computeSandboxName(
@@ -2242,15 +2283,53 @@ async function registerSandboxProxyRoutes(app2) {
2242
2283
  threadId,
2243
2284
  isolatedLevel
2244
2285
  );
2245
- const targetPath = restPath ? `/vnc/${restPath}` : "/vnc/";
2246
- const targetUrl = `${sandboxService.getTargetUrl(sandboxName)}${targetPath}`;
2286
+ const sandboxManager = (0, import_core13.getSandBoxManager)("default");
2287
+ const sandbox = await sandboxManager.createSandbox(sandboxName);
2247
2288
  try {
2248
- const response = await fetch(targetUrl);
2249
- const contentType = response.headers.get("content-type") || "application/octet-stream";
2250
- const body = await response.arrayBuffer();
2251
- reply.status(response.status).type(contentType).send(Buffer.from(body));
2289
+ const resolvedPath = filePath.startsWith("/home/gem") ? filePath : `/home/gem/${filePath.replace(/^\//, "")}`;
2290
+ const filename = getFilenameFromPath(resolvedPath);
2291
+ const inferredContentType = getContentTypeFromFilename(filename);
2292
+ const downloadResult = await sandbox.file.downloadFile({
2293
+ path: resolvedPath
2294
+ });
2295
+ if (!downloadResult.ok) {
2296
+ return reply.status(502).send({
2297
+ error: `Download error: ${JSON.stringify(downloadResult.error)}`
2298
+ });
2299
+ }
2300
+ const body = downloadResult.body;
2301
+ if (typeof body?.stream === "function") {
2302
+ const webStream = body.stream();
2303
+ const nodeStream = import_stream.Readable.fromWeb(webStream);
2304
+ const contentType2 = body.contentType ?? inferredContentType;
2305
+ const contentDisposition2 = body.contentDisposition ?? `inline; filename="${filename.replace(/"/g, '\\"')}"; filename*=UTF-8''${encodeURIComponent(filename)}`;
2306
+ reply = reply.status(200).type(contentType2).header("Content-Disposition", contentDisposition2).send(nodeStream);
2307
+ return reply;
2308
+ }
2309
+ const bodyUnknown = downloadResult.body;
2310
+ let buf;
2311
+ let contentType = inferredContentType;
2312
+ let contentDisposition = `inline; filename="${filename.replace(/"/g, '\\"')}"; filename*=UTF-8''${encodeURIComponent(filename)}`;
2313
+ if (bodyUnknown instanceof ArrayBuffer) {
2314
+ buf = Buffer.from(bodyUnknown);
2315
+ } else if (bodyUnknown instanceof Buffer) {
2316
+ buf = bodyUnknown;
2317
+ } else if (bodyUnknown && typeof bodyUnknown.arrayBuffer === "function") {
2318
+ const res = bodyUnknown;
2319
+ buf = Buffer.from(await res.arrayBuffer());
2320
+ if (res.headers?.get("content-type")) contentType = res.headers.get("content-type");
2321
+ if (res.headers?.get("content-disposition")) contentDisposition = res.headers.get("content-disposition");
2322
+ } else if (bodyUnknown && typeof bodyUnknown.blob === "function") {
2323
+ const blob = await bodyUnknown.blob();
2324
+ buf = Buffer.from(await blob.arrayBuffer());
2325
+ } else {
2326
+ return reply.status(502).send({ error: "Unexpected download response format" });
2327
+ }
2328
+ reply = reply.status(200).type(contentType).header("Content-Disposition", contentDisposition).send(buf);
2329
+ return reply;
2252
2330
  } catch (error) {
2253
- reply.status(502).send(`Proxy error: ${error.message}`);
2331
+ const message = error instanceof Error ? error.message : String(error);
2332
+ return reply.status(502).send({ error: `Download proxy error: ${message}` });
2254
2333
  }
2255
2334
  }
2256
2335
  );
@@ -2440,7 +2519,7 @@ var configureSwagger = async (app2, customSwaggerConfig, customSwaggerUiConfig)
2440
2519
  };
2441
2520
 
2442
2521
  // src/services/agent_task_consumer.ts
2443
- var import_core13 = require("@axiom-lattice/core");
2522
+ var import_core14 = require("@axiom-lattice/core");
2444
2523
  var handleAgentTask = async (taskRequest, retryCount = 0) => {
2445
2524
  const {
2446
2525
  assistant_id,
@@ -2504,7 +2583,7 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
2504
2583
  }
2505
2584
  if (callback_event) {
2506
2585
  const state = await agent_state({ assistant_id, thread_id });
2507
- import_core13.eventBus.publish(callback_event, {
2586
+ import_core14.eventBus.publish(callback_event, {
2508
2587
  success: true,
2509
2588
  state,
2510
2589
  config: { assistant_id, thread_id, tenant_id }
@@ -2518,7 +2597,7 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
2518
2597
  await response.text();
2519
2598
  if (callback_event) {
2520
2599
  const state = await agent_state({ assistant_id, thread_id });
2521
- import_core13.eventBus.publish(callback_event, {
2600
+ import_core14.eventBus.publish(callback_event, {
2522
2601
  success: true,
2523
2602
  state,
2524
2603
  config: { assistant_id, thread_id, tenant_id }
@@ -2545,7 +2624,7 @@ var handleAgentTask = async (taskRequest, retryCount = 0) => {
2545
2624
  return handleAgentTask(taskRequest, nextRetryCount);
2546
2625
  }
2547
2626
  if (callback_event) {
2548
- import_core13.eventBus.publish(callback_event, {
2627
+ import_core14.eventBus.publish(callback_event, {
2549
2628
  success: false,
2550
2629
  error: error instanceof Error ? error.message : String(error),
2551
2630
  config: { assistant_id, thread_id, tenant_id }
@@ -2583,7 +2662,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
2583
2662
  * 初始化事件监听和队列轮询
2584
2663
  */
2585
2664
  initialize() {
2586
- import_core13.eventBus.subscribe(import_core13.AGENT_TASK_EVENT, this.trigger_agent_task.bind(this));
2665
+ import_core14.eventBus.subscribe(import_core14.AGENT_TASK_EVENT, this.trigger_agent_task.bind(this));
2587
2666
  this.startPollingQueue();
2588
2667
  console.log("Agent\u4EFB\u52A1\u6D88\u8D39\u8005\u5DF2\u542F\u52A8\u5E76\u76D1\u542C\u4EFB\u52A1\u4E8B\u4EF6\u548C\u961F\u5217");
2589
2668
  }
@@ -2702,7 +2781,7 @@ var _AgentTaskConsumer = class _AgentTaskConsumer {
2702
2781
  handleAgentTask(taskRequest).catch((error) => {
2703
2782
  console.error("\u5904\u7406Agent\u4EFB\u52A1\u65F6\u53D1\u751F\u672A\u6355\u83B7\u7684\u9519\u8BEF:", error);
2704
2783
  if (taskRequest.callback_event) {
2705
- import_core13.eventBus.publish(taskRequest.callback_event, {
2784
+ import_core14.eventBus.publish(taskRequest.callback_event, {
2706
2785
  success: false,
2707
2786
  error: error instanceof Error ? error.message : String(error),
2708
2787
  config: {
@@ -2722,7 +2801,7 @@ _AgentTaskConsumer.agent_run_endpoint = "http://localhost:4001/api/runs";
2722
2801
  var AgentTaskConsumer = _AgentTaskConsumer;
2723
2802
 
2724
2803
  // src/index.ts
2725
- var import_core14 = require("@axiom-lattice/core");
2804
+ var import_core15 = require("@axiom-lattice/core");
2726
2805
  var import_protocols2 = require("@axiom-lattice/protocols");
2727
2806
  process.on("unhandledRejection", (reason, promise) => {
2728
2807
  console.error("\u672A\u5904\u7406\u7684Promise\u62D2\u7EDD:", reason);
@@ -2737,11 +2816,11 @@ var DEFAULT_LOGGER_CONFIG = {
2737
2816
  var loggerLattice = initializeLogger(DEFAULT_LOGGER_CONFIG);
2738
2817
  var logger = loggerLattice.client;
2739
2818
  function initializeLogger(config) {
2740
- if (import_core14.loggerLatticeManager.hasLattice("default")) {
2741
- import_core14.loggerLatticeManager.removeLattice("default");
2819
+ if (import_core15.loggerLatticeManager.hasLattice("default")) {
2820
+ import_core15.loggerLatticeManager.removeLattice("default");
2742
2821
  }
2743
- (0, import_core14.registerLoggerLattice)("default", config);
2744
- return (0, import_core14.getLoggerLattice)("default");
2822
+ (0, import_core15.registerLoggerLattice)("default", config);
2823
+ return (0, import_core15.getLoggerLattice)("default");
2745
2824
  }
2746
2825
  var app = (0, import_fastify.default)({
2747
2826
  logger: false,
@@ -2787,12 +2866,19 @@ app.register(import_cors.default, {
2787
2866
  "Authorization",
2788
2867
  "X-Requested-With",
2789
2868
  "x-tenant-id",
2790
- "x-request-id"
2869
+ "x-request-id",
2870
+ "x-assistant-id",
2871
+ "x-thread-id"
2791
2872
  ],
2792
2873
  exposedHeaders: ["Content-Type"],
2793
2874
  credentials: true
2794
2875
  });
2795
2876
  app.register(import_sensible.default);
2877
+ app.register(import_multipart.default, {
2878
+ limits: {
2879
+ fileSize: Number(process.env.BODY_LIMIT) || 50 * 1024 * 1024
2880
+ }
2881
+ });
2796
2882
  app.register(import_websocket.default);
2797
2883
  app.setErrorHandler((error, request, reply) => {
2798
2884
  const getHeaderValue = (header) => {