paperclip-plugin-supermemory-otomatik 0.2.8 → 0.2.9

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/worker.js CHANGED
@@ -77595,6 +77595,32 @@ async function remoteTextDocument(ctx, base, key, assignment, docId, title, cont
77595
77595
  if (!id) throw new Error("Supermemory Local non ha restituito l\u2019ID del documento.");
77596
77596
  return { id, status: typeof parsed.status === "string" ? parsed.status : "queued" };
77597
77597
  }
77598
+ async function indexPdfTextDocuments(ctx, base, key, assignment, parentDocumentId, fileName, bytes) {
77599
+ const extractedText = await extractPdfText(bytes);
77600
+ if (extractedText.length < 40) throw new Error("Il PDF non contiene abbastanza testo selezionabile. I PDF scansionati richiedono OCR.");
77601
+ const chunks = splitUtf8Text(extractedText);
77602
+ const ids = [];
77603
+ const statuses = [];
77604
+ for (let index = 0; index < chunks.length; index += 1) {
77605
+ try {
77606
+ const indexed = await remoteTextDocument(
77607
+ ctx,
77608
+ base,
77609
+ key,
77610
+ assignment,
77611
+ null,
77612
+ chunks.length === 1 ? `${fileName} \u2014 testo estratto` : `${fileName} \u2014 estratto ${index + 1}/${chunks.length}`,
77613
+ chunks[index]
77614
+ );
77615
+ ids.push(indexed.id);
77616
+ statuses.push(indexed.status);
77617
+ } catch (error62) {
77618
+ ctx.logger.warn("PDF text companion could not be indexed", { namespace: assignment.namespace, parentDocumentId, error: errorText(error62) });
77619
+ }
77620
+ }
77621
+ if (!ids.length) throw new Error("Supermemory non ha accettato l\u2019estratto testuale del PDF.");
77622
+ return { ids, status: statuses.every((status) => status === "done") ? "done" : statuses[0] ?? "queued" };
77623
+ }
77598
77624
  var plugin = definePlugin({
77599
77625
  async setup(ctx) {
77600
77626
  ctx.tools.register(SEARCH_TOOL, {
@@ -77787,31 +77813,13 @@ var plugin = definePlugin({
77787
77813
  if (!documentId) throw new Error("Il gateway Supermemory non ha restituito l\u2019ID del file caricato.");
77788
77814
  const now = (/* @__PURE__ */ new Date()).toISOString();
77789
77815
  const binaryStatus = typeof payload.status === "string" ? payload.status : "queued";
77790
- const indexedTextDocumentIds = [];
77791
- const indexedTextStatuses = [];
77816
+ let indexedTextDocumentIds = [];
77817
+ let indexedTextStatus = null;
77792
77818
  if (mimeType === "application/pdf") {
77793
77819
  try {
77794
- const extractedText = await extractPdfText(bytes);
77795
- if (extractedText.length >= 40) {
77796
- const chunks = splitUtf8Text(extractedText);
77797
- for (let index = 0; index < chunks.length; index += 1) {
77798
- try {
77799
- const indexed = await remoteTextDocument(
77800
- ctx,
77801
- base,
77802
- key,
77803
- assignment,
77804
- null,
77805
- chunks.length === 1 ? `${fileName} \u2014 testo estratto` : `${fileName} \u2014 estratto ${index + 1}/${chunks.length}`,
77806
- chunks[index]
77807
- );
77808
- indexedTextDocumentIds.push(indexed.id);
77809
- indexedTextStatuses.push(indexed.status);
77810
- } catch (error62) {
77811
- ctx.logger.warn("PDF text companion could not be indexed", { companyId, namespace: assignment.namespace, documentId, error: errorText(error62) });
77812
- }
77813
- }
77814
- }
77820
+ const indexed = await indexPdfTextDocuments(ctx, base, key, assignment, documentId, fileName, bytes);
77821
+ indexedTextDocumentIds = indexed.ids;
77822
+ indexedTextStatus = indexed.status;
77815
77823
  } catch (error62) {
77816
77824
  ctx.logger.warn("Local PDF text extraction failed; keeping the uploaded binary", { companyId, namespace: assignment.namespace, documentId, error: errorText(error62) });
77817
77825
  }
@@ -77826,9 +77834,9 @@ var plugin = definePlugin({
77826
77834
  mimeType,
77827
77835
  fileName,
77828
77836
  sourceUrl: null,
77829
- status: indexedTextDocumentIds.length ? indexedTextStatuses[0] ?? "queued" : binaryStatus,
77837
+ status: indexedTextStatus ?? binaryStatus,
77830
77838
  indexedTextDocumentIds,
77831
- indexedTextStatus: indexedTextStatuses.length ? indexedTextStatuses.every((status) => status === "done") ? "done" : indexedTextStatuses[0] ?? "queued" : null,
77839
+ indexedTextStatus,
77832
77840
  binaryProcessingStatus: binaryStatus,
77833
77841
  createdAt: now,
77834
77842
  updatedAt: now
@@ -77836,6 +77844,56 @@ var plugin = definePlugin({
77836
77844
  await saveDocumentEntity(ctx, companyId, assignment, data);
77837
77845
  return { document: { ...data, content: void 0 } };
77838
77846
  });
77847
+ ctx.actions.register("memory-file-reindex-text", async (params, actionCtx) => {
77848
+ const companyId = actionCompany(params, actionCtx);
77849
+ await ownerFor(ctx, companyId, actionCtx);
77850
+ const assignment = await requireAssignment(ctx, companyId, String(params.assignmentId ?? ""));
77851
+ const documentId = typeof params.documentId === "string" ? params.documentId.trim() : "";
77852
+ const fileName = requireTitle(params.fileName);
77853
+ const mimeType = typeof params.mimeType === "string" ? params.mimeType : "";
77854
+ const base643 = typeof params.base64 === "string" ? params.base64 : "";
77855
+ if (!documentId || mimeType !== "application/pdf" || !fileName.toLowerCase().endsWith(".pdf")) throw new Error("Seleziona un PDF per collegare il testo al documento esistente.");
77856
+ const { row, data: record2 } = await sourceDocumentEntry(ctx, companyId, assignment.namespace, documentId);
77857
+ if (record2.kind !== "file" || record2.mimeType !== "application/pdf" || record2.fileName !== fileName) throw new Error("Il nome del file deve corrispondere al PDF gi\xE0 selezionato nella libreria.");
77858
+ const bytes = Buffer.from(base643, "base64");
77859
+ if (!bytes.byteLength || bytes.byteLength > MAX_BINARY_BYTES) throw new Error("Il PDF deve contenere dati e non superare 6 MiB.");
77860
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(base643) || bytes.toString("base64").replace(/=+$/, "") !== base643.replace(/=+$/, "")) throw new Error("Contenuto file base64 non valido.");
77861
+ const { base, key } = await adminConfig(ctx, companyId);
77862
+ const indexed = await indexPdfTextDocuments(ctx, base, key, assignment, documentId, fileName, bytes);
77863
+ const oldIds = Array.isArray(record2.indexedTextDocumentIds) ? record2.indexedTextDocumentIds.filter((id) => typeof id === "string") : [];
77864
+ if (oldIds.length) {
77865
+ await ownerGatewayRequest(ctx, base, key, localNamespacePath(assignment.namespace, "/document"), {
77866
+ method: "DELETE",
77867
+ headers: { "content-type": "application/json" },
77868
+ body: JSON.stringify({ ids: [...new Set(oldIds)] })
77869
+ });
77870
+ }
77871
+ const data = {
77872
+ ...record2,
77873
+ indexedTextDocumentIds: indexed.ids,
77874
+ indexedTextStatus: indexed.status,
77875
+ processingStatus: indexed.status,
77876
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
77877
+ };
77878
+ const externalId = typeof row.externalId === "string" ? row.externalId : documentExternalId(assignment.namespace, documentId);
77879
+ await ctx.entities.upsert({ entityType: DOCUMENT_ENTITY, scopeKind: "company", scopeId: companyId, externalId, title: row.title ?? fileName, status: "active", data });
77880
+ return { document: {
77881
+ id: documentId,
77882
+ namespace: assignment.namespace,
77883
+ assignmentId: String(data.assignmentId ?? assignment.id),
77884
+ title: String(data.title ?? fileName),
77885
+ kind: "file",
77886
+ mimeType: "application/pdf",
77887
+ fileName,
77888
+ sourceUrl: null,
77889
+ status: indexed.status,
77890
+ indexedTextDocumentIds: indexed.ids,
77891
+ indexedTextStatus: indexed.status,
77892
+ binaryProcessingStatus: typeof data.binaryProcessingStatus === "string" ? data.binaryProcessingStatus : null,
77893
+ createdAt: String(data.createdAt ?? ""),
77894
+ updatedAt: String(data.updatedAt)
77895
+ } };
77896
+ });
77839
77897
  ctx.actions.register("memory-document-delete", async (params, actionCtx) => {
77840
77898
  const companyId = actionCompany(params, actionCtx);
77841
77899
  await ownerFor(ctx, companyId, actionCtx);