@imenam/mcp-organizer 1.0.21 → 1.0.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.
Files changed (52) hide show
  1. package/README.md +17 -12
  2. package/dist/shared/logic/documents.d.ts +2 -4
  3. package/dist/shared/logic/documents.js +6 -11
  4. package/dist/shared/schemas/agent-tools.schema.d.ts +133 -7
  5. package/dist/shared/schemas/agent-tools.schema.js +52 -7
  6. package/dist/shared/schemas/documents-api.schema.d.ts +0 -2
  7. package/dist/shared/schemas/documents-api.schema.js +0 -2
  8. package/dist/src/agent-api/attachments.d.ts +2 -0
  9. package/dist/src/agent-api/attachments.js +96 -0
  10. package/dist/src/agent-api/documents.d.ts +1 -1
  11. package/dist/src/agent-api/documents.js +9 -33
  12. package/dist/src/agent-api/files.d.ts +35 -0
  13. package/dist/src/agent-api/files.js +86 -0
  14. package/dist/src/agent-api/router.d.ts +2 -2
  15. package/dist/src/agent-api/router.js +5 -4
  16. package/dist/src/agent-api/tasks.js +5 -8
  17. package/dist/src/agent-api/types.d.ts +4 -2
  18. package/dist/src/core/agent-files.d.ts +41 -0
  19. package/dist/src/core/agent-files.js +102 -0
  20. package/dist/src/core/agent.d.ts +3 -1
  21. package/dist/src/core/agent.js +14 -3
  22. package/dist/src/core/document-text.d.ts +3 -2
  23. package/dist/src/core/document-text.js +3 -2
  24. package/dist/src/core/documents.js +0 -1
  25. package/dist/src/core/history.d.ts +3 -1
  26. package/dist/src/core/history.js +28 -11
  27. package/dist/src/core/mail-render.d.ts +98 -0
  28. package/dist/src/core/mail-render.js +447 -0
  29. package/dist/src/core/mail.d.ts +6 -1
  30. package/dist/src/core/mail.js +1 -1
  31. package/dist/src/core/notifications.d.ts +2 -0
  32. package/dist/src/core/notifications.js +70 -32
  33. package/dist/src/core/password-reset.js +30 -9
  34. package/dist/src/core/requests.d.ts +9 -4
  35. package/dist/src/core/requests.js +21 -10
  36. package/dist/src/core/snapshots.d.ts +3 -3
  37. package/dist/src/core/snapshots.js +3 -1
  38. package/dist/src/core/spaces.js +12 -5
  39. package/dist/src/gui-api.js +2 -2
  40. package/dist/src/mcp/client.d.ts +7 -4
  41. package/dist/src/mcp/client.js +31 -4
  42. package/dist/src/mcp/start.d.ts +1 -1
  43. package/dist/src/mcp/start.js +6 -6
  44. package/dist/src/mcp/tools.d.ts +11 -0
  45. package/dist/src/mcp/tools.js +55 -4
  46. package/dist/src/mcp/upload.d.ts +22 -0
  47. package/dist/src/mcp/upload.js +79 -0
  48. package/dist/src/server/app.js +38 -4
  49. package/gui/dist/assets/{index-DbzcjyaW.js → index-C1f7hOcM.js} +2 -2
  50. package/gui/dist/assets/{index-Dvf5Yo0e.css → index-CfMiwPSL.css} +1 -1
  51. package/gui/dist/index.html +2 -2
  52. package/package.json +4 -1
@@ -0,0 +1,79 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { DOCUMENT_FORMATS } from "../../shared/logic/documents.js";
4
+ import { MAX_ATTACHMENT_BYTES } from "../../shared/schemas/tasks-api.schema.js";
5
+ /** Fichier local refusé : chemin relatif, introuvable, dossier, fichier spécial ou trop lourd. */
6
+ export class LocalFileError extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = "LocalFileError";
10
+ }
11
+ }
12
+ /** Types MIME reconnus à l'extension, en plus de ceux des documents d'espace. */
13
+ const EXTRA_TYPES = {
14
+ html: "text/html",
15
+ htm: "text/html",
16
+ zip: "application/zip",
17
+ svg: "image/svg+xml",
18
+ bmp: "image/bmp",
19
+ tif: "image/tiff",
20
+ tiff: "image/tiff",
21
+ heic: "image/heic",
22
+ mp4: "video/mp4",
23
+ webm: "video/webm",
24
+ mp3: "audio/mpeg",
25
+ wav: "audio/wav",
26
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
27
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
28
+ };
29
+ /** Type MIME d'un fichier d'après son extension ; `application/octet-stream` pour une extension inconnue. */
30
+ export function mimeOfFilename(filename) {
31
+ const match = /\.([^.]+)$/.exec(filename);
32
+ if (!match)
33
+ return "application/octet-stream";
34
+ const extension = match[1].toLowerCase();
35
+ return DOCUMENT_FORMATS.find((format) => format.extensions.includes(extension))?.mime ?? EXTRA_TYPES[extension] ?? "application/octet-stream";
36
+ }
37
+ function megabytes(bytes) {
38
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
39
+ }
40
+ /**
41
+ * Lit le fichier régulier désigné par le chemin absolu `filePath` ; `name`
42
+ * remplace son nom. Un lien symbolique est suivi jusqu'au fichier qu'il désigne.
43
+ */
44
+ export async function readLocalUpload(filePath, name) {
45
+ if (!path.isAbsolute(filePath))
46
+ throw new LocalFileError(`path must be an absolute path on the machine where the agent runs (got "${filePath}").`);
47
+ let stat;
48
+ try {
49
+ stat = await fs.stat(filePath);
50
+ }
51
+ catch (err) {
52
+ if (err?.code === "ENOENT" || err?.code === "ENOTDIR")
53
+ throw new LocalFileError(`No file at ${filePath}: check the path.`);
54
+ if (err?.code === "EACCES" || err?.code === "EPERM")
55
+ throw new LocalFileError(`The file ${filePath} cannot be read: permission denied.`);
56
+ throw new LocalFileError(`The file ${filePath} cannot be read (${err?.message ?? err}).`);
57
+ }
58
+ if (stat.isDirectory())
59
+ throw new LocalFileError(`${filePath} is a directory: give the path of a file.`);
60
+ if (!stat.isFile())
61
+ throw new LocalFileError(`${filePath} is not a regular file: only regular files can be attached.`);
62
+ if (stat.size > MAX_ATTACHMENT_BYTES) {
63
+ throw new LocalFileError(`The file ${filePath} is too large (${megabytes(stat.size)}): an attachment cannot exceed ${megabytes(MAX_ATTACHMENT_BYTES)}.`);
64
+ }
65
+ let content;
66
+ try {
67
+ content = await fs.readFile(filePath);
68
+ }
69
+ catch (err) {
70
+ throw new LocalFileError(`The file ${filePath} cannot be read (${err?.message ?? err}).`);
71
+ }
72
+ if (content.length > MAX_ATTACHMENT_BYTES) {
73
+ throw new LocalFileError(`The file ${filePath} is too large (${megabytes(content.length)}): an attachment cannot exceed ${megabytes(MAX_ATTACHMENT_BYTES)}.`);
74
+ }
75
+ const filename = (name ?? path.basename(filePath)).trim();
76
+ if (filename === "")
77
+ throw new LocalFileError("name must not be empty.");
78
+ return { filename, mime: mimeOfFilename(path.basename(filePath)), content };
79
+ }
@@ -105,6 +105,34 @@ async function readUploadBody(c) {
105
105
  const mime = file.type.split(";")[0].trim() || "application/octet-stream";
106
106
  return { body: { filename: file.name || "fichier", mime, contentBase64: content.toString("base64") } };
107
107
  }
108
+ /**
109
+ * Formulaire multipart d'un envoi de fichier par l'agent : le fichier (champ
110
+ * `file`) passe en octets, `{ filename, mime, content }`, à côté des autres
111
+ * champs, gardés en texte. Messages en anglais, pour l'agent.
112
+ */
113
+ async function readAgentUpload(c) {
114
+ let form;
115
+ try {
116
+ form = await c.req.formData();
117
+ }
118
+ catch (err) {
119
+ return { response: c.json({ error: `The multipart form cannot be read: ${err?.message ?? err}` }, 400) };
120
+ }
121
+ const body = {};
122
+ for (const [name, value] of form.entries()) {
123
+ if (name !== "file" && typeof value === "string")
124
+ body[name] = value;
125
+ }
126
+ const file = form.get("file");
127
+ if (!(file instanceof File))
128
+ return { response: c.json({ error: "The form holds no file (field \"file\")." }, 400) };
129
+ if (file.size > MAX_ATTACHMENT_BYTES) {
130
+ return { response: c.json({ error: `The file is too large (${(file.size / (1024 * 1024)).toFixed(1)} MB): an attachment cannot exceed ${MAX_ATTACHMENT_BYTES / (1024 * 1024)} MB.` }, 413) };
131
+ }
132
+ const mime = file.type.split(";")[0].trim() || "application/octet-stream";
133
+ body.file = { filename: file.name || "file", mime, content: Buffer.from(await file.arrayBuffer()) };
134
+ return { body };
135
+ }
108
136
  /** Corps d'une requête de modification : JSON, ou formulaire multipart d'un fichier ; `undefined` sans corps. */
109
137
  async function readRequestBody(c) {
110
138
  if (c.req.method === "GET" || c.req.method === "HEAD")
@@ -194,10 +222,16 @@ export function createSiteApp(deps) {
194
222
  maxSize: MAX_BODY_BYTES,
195
223
  onError: (c) => c.json({ error: `Requête trop volumineuse : ${megabytes(MAX_BODY_BYTES)} au plus.` }, 413),
196
224
  }));
197
- // API des agents : jeton de l'en-tête Authorization, corps JSON ; les cookies sont ignorés.
225
+ // API des agents : jeton de l'en-tête Authorization, corps JSON ou formulaire multipart d'un fichier ; les cookies sont ignorés.
198
226
  const agentApi = async (c) => {
199
227
  let body = undefined;
200
- if (c.req.method !== "GET" && c.req.method !== "HEAD") {
228
+ if ((c.req.header("content-type") ?? "").toLowerCase().startsWith("multipart/form-data")) {
229
+ const read = await readAgentUpload(c);
230
+ if ("response" in read)
231
+ return read.response;
232
+ body = read.body;
233
+ }
234
+ else if (c.req.method !== "GET" && c.req.method !== "HEAD") {
201
235
  const text = await c.req.text();
202
236
  if (text.trim() !== "") {
203
237
  try {
@@ -209,7 +243,7 @@ export function createSiteApp(deps) {
209
243
  }
210
244
  }
211
245
  try {
212
- const response = handleAgentRequest({ store: api.store, attachments: api.attachments, now: api.now }, { method: c.req.method, path: c.req.path.slice(AGENT_API_PREFIX.length) || "/", query: c.req.query(), body, bearer: bearerToken(c) });
246
+ const response = await handleAgentRequest({ store: api.store, attachments: api.attachments, now: api.now }, { method: c.req.method, path: c.req.path.slice(AGENT_API_PREFIX.length) || "/", query: c.req.query(), body, bearer: bearerToken(c) });
213
247
  return c.json(response);
214
248
  }
215
249
  catch (err) {
@@ -227,7 +261,7 @@ export function createSiteApp(deps) {
227
261
  c.header("WWW-Authenticate", 'Bearer realm="mcp-organizer"');
228
262
  return c.json({ error: err?.message ?? String(err) }, HTTP_STATUS_BY_ERROR_KIND[kind]);
229
263
  }
230
- // Attente longue des demandes : seule route asynchrone de l'API des agents.
264
+ // Attente longue des demandes : hors du routeur, elle suit l'abandon de la requête par le client.
231
265
  app.get(`${AGENT_API_PREFIX}/requests/wait`, async (c) => {
232
266
  try {
233
267
  const response = await waitForPendingRequests({ store: api.store, attachments: api.attachments, now: api.now }, { method: "GET", path: "/requests/wait", query: c.req.query(), bearer: bearerToken(c) }, c.req.raw.signal);