@radaros/transport 0.3.9 → 0.3.12

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.
Files changed (2) hide show
  1. package/dist/index.js +69 -6
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -64,10 +64,19 @@ function generateMultiAgentCard(agents, serverUrl, provider, version) {
64
64
  var _require = createRequire(import.meta.url);
65
65
  var TaskStore = class {
66
66
  tasks = /* @__PURE__ */ new Map();
67
+ maxTasks = 1e4;
67
68
  get(id) {
68
69
  return this.tasks.get(id);
69
70
  }
70
71
  set(task) {
72
+ if (this.tasks.size >= this.maxTasks) {
73
+ for (const [id, t] of this.tasks) {
74
+ if (t.status?.state === "completed" || t.status?.state === "canceled") {
75
+ this.tasks.delete(id);
76
+ break;
77
+ }
78
+ }
79
+ }
71
80
  this.tasks.set(task.id, task);
72
81
  }
73
82
  updateState(id, state, message) {
@@ -870,6 +879,23 @@ var API_KEY_HEADERS = {
870
879
  "x-anthropic-api-key": "anthropic",
871
880
  "x-api-key": "_generic"
872
881
  };
882
+ function validateBody(body, fields) {
883
+ if (!body || typeof body !== "object") throw new Error("Invalid request body");
884
+ const result = {};
885
+ for (const [key, type] of Object.entries(fields)) {
886
+ const val = body[key];
887
+ const isOptional = type.endsWith("?");
888
+ const baseType = type.replace("?", "");
889
+ if (val === void 0 || val === null) {
890
+ if (!isOptional) throw new Error(`Missing required field: ${key}`);
891
+ continue;
892
+ }
893
+ if (baseType === "string" && typeof val !== "string") throw new Error(`Field ${key} must be a string`);
894
+ if (baseType === "object" && typeof val !== "object") throw new Error(`Field ${key} must be an object`);
895
+ result[key] = val;
896
+ }
897
+ return result;
898
+ }
873
899
  function extractApiKey(req, agent) {
874
900
  for (const [header, provider] of Object.entries(API_KEY_HEADERS)) {
875
901
  const value = req.headers[header];
@@ -928,25 +954,38 @@ function createAgentRouter(opts) {
928
954
  `/agents/${name}/run`,
929
955
  withUpload(async (req, res) => {
930
956
  try {
931
- const input = buildMultiModalInput(req.body, req.files);
957
+ const validated = validateBody(req.body, {
958
+ input: "string",
959
+ sessionId: "string?",
960
+ userId: "string?"
961
+ });
962
+ const input = buildMultiModalInput(req.body, req.files) ?? validated.input;
932
963
  if (!input) {
933
964
  return res.status(400).json({ error: "input is required" });
934
965
  }
935
- const { sessionId, userId } = req.body ?? {};
966
+ const sessionId = validated.sessionId;
967
+ const userId = validated.userId;
936
968
  const apiKey = extractApiKey(req, agent);
937
969
  const result = await agent.run(input, { sessionId, userId, apiKey });
938
970
  res.json(result);
939
971
  } catch (error) {
940
- res.status(500).json({ error: error.message });
972
+ res.status(400).json({ error: error.message });
941
973
  }
942
974
  })
943
975
  );
944
976
  router.post(`/agents/${name}/stream`, async (req, res) => {
945
977
  try {
946
- const { input, sessionId, userId } = req.body ?? {};
978
+ const validated = validateBody(req.body, {
979
+ input: "string",
980
+ sessionId: "string?",
981
+ userId: "string?"
982
+ });
983
+ const input = validated.input;
947
984
  if (!input) {
948
985
  return res.status(400).json({ error: "input is required" });
949
986
  }
987
+ const sessionId = validated.sessionId;
988
+ const userId = validated.userId;
950
989
  const apiKey = extractApiKey(req, agent);
951
990
  res.writeHead(200, {
952
991
  "Content-Type": "text/event-stream",
@@ -978,10 +1017,17 @@ function createAgentRouter(opts) {
978
1017
  for (const [name, team] of Object.entries(opts.teams)) {
979
1018
  router.post(`/teams/${name}/run`, async (req, res) => {
980
1019
  try {
981
- const { input, sessionId, userId } = req.body ?? {};
1020
+ const validated = validateBody(req.body, {
1021
+ input: "string",
1022
+ sessionId: "string?",
1023
+ userId: "string?"
1024
+ });
1025
+ const input = validated.input;
982
1026
  if (!input) {
983
1027
  return res.status(400).json({ error: "input is required" });
984
1028
  }
1029
+ const sessionId = validated.sessionId;
1030
+ const userId = validated.userId;
985
1031
  const apiKey = req.headers["x-api-key"] ?? req.body?.apiKey;
986
1032
  const result = await team.run(input, { sessionId, userId, apiKey });
987
1033
  res.json(result);
@@ -991,10 +1037,17 @@ function createAgentRouter(opts) {
991
1037
  });
992
1038
  router.post(`/teams/${name}/stream`, async (req, res) => {
993
1039
  try {
994
- const { input, sessionId, userId } = req.body ?? {};
1040
+ const validated = validateBody(req.body, {
1041
+ input: "string",
1042
+ sessionId: "string?",
1043
+ userId: "string?"
1044
+ });
1045
+ const input = validated.input;
995
1046
  if (!input) {
996
1047
  return res.status(400).json({ error: "input is required" });
997
1048
  }
1049
+ const sessionId = validated.sessionId;
1050
+ const userId = validated.userId;
998
1051
  const apiKey = req.headers["x-api-key"] ?? req.body?.apiKey;
999
1052
  res.writeHead(200, {
1000
1053
  "Content-Type": "text/event-stream",
@@ -1159,6 +1212,14 @@ function createAgentGateway(opts) {
1159
1212
  }
1160
1213
  ns.on("connection", (socket) => {
1161
1214
  socket.on("agent.run", async (data) => {
1215
+ if (!data || typeof data.input !== "string" || !data.input.trim()) {
1216
+ socket.emit("agent.error", { error: "Invalid input: must be a non-empty string" });
1217
+ return;
1218
+ }
1219
+ if (data.sessionId !== void 0 && typeof data.sessionId !== "string") {
1220
+ socket.emit("agent.error", { error: "Invalid sessionId: must be a string" });
1221
+ return;
1222
+ }
1162
1223
  const agent = opts.agents?.[data.name];
1163
1224
  if (!agent) {
1164
1225
  socket.emit("agent.error", {
@@ -1191,6 +1252,8 @@ function createAgentGateway(opts) {
1191
1252
  socket.emit("agent.error", { error: error.message });
1192
1253
  }
1193
1254
  });
1255
+ socket.on("disconnect", () => {
1256
+ });
1194
1257
  socket.on("team.run", async (data) => {
1195
1258
  const team = opts.teams?.[data.name];
1196
1259
  if (!team) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radaros/transport",
3
- "version": "0.3.9",
3
+ "version": "0.3.12",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -24,10 +24,10 @@
24
24
  "typescript": "^5.6.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@radaros/core": "^0.3.9",
27
+ "@radaros/core": "^0.3.12",
28
28
  "@types/express": "^4.0.0 || ^5.0.0",
29
29
  "express": "^4.0.0 || ^5.0.0",
30
- "multer": ">=1.4.0",
30
+ "multer": ">=2.0.0",
31
31
  "socket.io": "^4.0.0",
32
32
  "swagger-ui-express": "^5.0.0"
33
33
  },