@ncukondo/reference-manager 0.3.0 → 0.5.0

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 (113) hide show
  1. package/README.md +282 -107
  2. package/dist/chunks/file-watcher-B-SiUw5f.js +1557 -0
  3. package/dist/chunks/file-watcher-B-SiUw5f.js.map +1 -0
  4. package/dist/chunks/{search-Be9vzUIH.js → index-DLIGxQaB.js} +399 -89
  5. package/dist/chunks/index-DLIGxQaB.js.map +1 -0
  6. package/dist/chunks/loader-DuzyKV70.js +394 -0
  7. package/dist/chunks/loader-DuzyKV70.js.map +1 -0
  8. package/dist/cli/commands/add.d.ts +3 -3
  9. package/dist/cli/commands/add.d.ts.map +1 -1
  10. package/dist/cli/commands/cite.d.ts +3 -3
  11. package/dist/cli/commands/cite.d.ts.map +1 -1
  12. package/dist/cli/commands/fulltext.d.ts +5 -34
  13. package/dist/cli/commands/fulltext.d.ts.map +1 -1
  14. package/dist/cli/commands/list.d.ts +3 -3
  15. package/dist/cli/commands/list.d.ts.map +1 -1
  16. package/dist/cli/commands/mcp.d.ts +16 -0
  17. package/dist/cli/commands/mcp.d.ts.map +1 -0
  18. package/dist/cli/commands/remove.d.ts +3 -3
  19. package/dist/cli/commands/remove.d.ts.map +1 -1
  20. package/dist/cli/commands/search.d.ts +3 -3
  21. package/dist/cli/commands/search.d.ts.map +1 -1
  22. package/dist/cli/commands/server.d.ts +2 -0
  23. package/dist/cli/commands/server.d.ts.map +1 -1
  24. package/dist/cli/commands/update.d.ts +3 -3
  25. package/dist/cli/commands/update.d.ts.map +1 -1
  26. package/dist/cli/execution-context.d.ts +23 -36
  27. package/dist/cli/execution-context.d.ts.map +1 -1
  28. package/dist/cli/index.d.ts.map +1 -1
  29. package/dist/cli/server-client.d.ts +24 -40
  30. package/dist/cli/server-client.d.ts.map +1 -1
  31. package/dist/cli/server-detection.d.ts +1 -0
  32. package/dist/cli/server-detection.d.ts.map +1 -1
  33. package/dist/cli.js +21061 -317
  34. package/dist/cli.js.map +1 -1
  35. package/dist/config/defaults.d.ts.map +1 -1
  36. package/dist/config/loader.d.ts.map +1 -1
  37. package/dist/config/schema.d.ts +2 -3
  38. package/dist/config/schema.d.ts.map +1 -1
  39. package/dist/core/csl-json/types.d.ts +3 -0
  40. package/dist/core/csl-json/types.d.ts.map +1 -1
  41. package/dist/core/library-interface.d.ts +100 -0
  42. package/dist/core/library-interface.d.ts.map +1 -0
  43. package/dist/core/library.d.ts +29 -46
  44. package/dist/core/library.d.ts.map +1 -1
  45. package/dist/features/operations/add.d.ts +2 -2
  46. package/dist/features/operations/add.d.ts.map +1 -1
  47. package/dist/features/operations/cite.d.ts +2 -2
  48. package/dist/features/operations/cite.d.ts.map +1 -1
  49. package/dist/features/operations/fulltext/attach.d.ts +47 -0
  50. package/dist/features/operations/fulltext/attach.d.ts.map +1 -0
  51. package/dist/features/operations/fulltext/detach.d.ts +38 -0
  52. package/dist/features/operations/fulltext/detach.d.ts.map +1 -0
  53. package/dist/features/operations/fulltext/get.d.ts +41 -0
  54. package/dist/features/operations/fulltext/get.d.ts.map +1 -0
  55. package/dist/features/operations/fulltext/index.d.ts +9 -0
  56. package/dist/features/operations/fulltext/index.d.ts.map +1 -0
  57. package/dist/features/operations/index.d.ts +15 -0
  58. package/dist/features/operations/index.d.ts.map +1 -0
  59. package/dist/features/operations/library-operations.d.ts +64 -0
  60. package/dist/features/operations/library-operations.d.ts.map +1 -0
  61. package/dist/features/operations/list.d.ts +2 -2
  62. package/dist/features/operations/list.d.ts.map +1 -1
  63. package/dist/features/operations/operations-library.d.ts +36 -0
  64. package/dist/features/operations/operations-library.d.ts.map +1 -0
  65. package/dist/features/operations/remove.d.ts +4 -4
  66. package/dist/features/operations/remove.d.ts.map +1 -1
  67. package/dist/features/operations/search.d.ts +2 -2
  68. package/dist/features/operations/search.d.ts.map +1 -1
  69. package/dist/features/operations/update.d.ts +2 -2
  70. package/dist/features/operations/update.d.ts.map +1 -1
  71. package/dist/features/search/matcher.d.ts.map +1 -1
  72. package/dist/features/search/normalizer.d.ts +12 -0
  73. package/dist/features/search/normalizer.d.ts.map +1 -1
  74. package/dist/features/search/tokenizer.d.ts.map +1 -1
  75. package/dist/features/search/types.d.ts +1 -1
  76. package/dist/features/search/types.d.ts.map +1 -1
  77. package/dist/features/search/uppercase.d.ts +41 -0
  78. package/dist/features/search/uppercase.d.ts.map +1 -0
  79. package/dist/index.js +24 -192
  80. package/dist/index.js.map +1 -1
  81. package/dist/mcp/context.d.ts +19 -0
  82. package/dist/mcp/context.d.ts.map +1 -0
  83. package/dist/mcp/index.d.ts +20 -0
  84. package/dist/mcp/index.d.ts.map +1 -0
  85. package/dist/mcp/resources/index.d.ts +10 -0
  86. package/dist/mcp/resources/index.d.ts.map +1 -0
  87. package/dist/mcp/resources/library.d.ts +26 -0
  88. package/dist/mcp/resources/library.d.ts.map +1 -0
  89. package/dist/mcp/tools/add.d.ts +17 -0
  90. package/dist/mcp/tools/add.d.ts.map +1 -0
  91. package/dist/mcp/tools/cite.d.ts +15 -0
  92. package/dist/mcp/tools/cite.d.ts.map +1 -0
  93. package/dist/mcp/tools/fulltext.d.ts +51 -0
  94. package/dist/mcp/tools/fulltext.d.ts.map +1 -0
  95. package/dist/mcp/tools/index.d.ts +12 -0
  96. package/dist/mcp/tools/index.d.ts.map +1 -0
  97. package/dist/mcp/tools/list.d.ts +13 -0
  98. package/dist/mcp/tools/list.d.ts.map +1 -0
  99. package/dist/mcp/tools/remove.d.ts +19 -0
  100. package/dist/mcp/tools/remove.d.ts.map +1 -0
  101. package/dist/mcp/tools/search.d.ts +13 -0
  102. package/dist/mcp/tools/search.d.ts.map +1 -0
  103. package/dist/server/index.d.ts +23 -1
  104. package/dist/server/index.d.ts.map +1 -1
  105. package/dist/server/routes/references.d.ts.map +1 -1
  106. package/dist/server.js +5 -271
  107. package/dist/server.js.map +1 -1
  108. package/package.json +2 -1
  109. package/dist/chunks/detector-DHztTaFY.js +0 -619
  110. package/dist/chunks/detector-DHztTaFY.js.map +0 -1
  111. package/dist/chunks/loader-mQ25o6cV.js +0 -1054
  112. package/dist/chunks/loader-mQ25o6cV.js.map +0 -1
  113. package/dist/chunks/search-Be9vzUIH.js.map +0 -1
@@ -1,6 +1,37 @@
1
- import { b as CslItemSchema, d as detectDuplicate, t as tokenize, s as search, a as sortResults } from "./detector-DHztTaFY.js";
1
+ import { Hono } from "hono";
2
+ import { e as CslItemSchema, d as detectDuplicate, t as tokenize, s as search$1, b as sortResults, L as Library, F as FileWatcher } from "./file-watcher-B-SiUw5f.js";
2
3
  import { existsSync, readFileSync } from "node:fs";
4
+ import { z } from "zod";
3
5
  import "node:path";
6
+ async function updateReference(library2, options2) {
7
+ const { identifier: identifier2, byUuid = false, updates, onIdCollision = "fail" } = options2;
8
+ const updateResult = await library2.update(identifier2, updates, { byUuid, onIdCollision });
9
+ if (!updateResult.updated) {
10
+ const result2 = { updated: false };
11
+ if (updateResult.idCollision) {
12
+ result2.idCollision = true;
13
+ }
14
+ return result2;
15
+ }
16
+ await library2.save();
17
+ const result = { updated: true };
18
+ if (updateResult.item) {
19
+ result.item = updateResult.item;
20
+ }
21
+ if (updateResult.idChanged && updateResult.newId) {
22
+ result.idChanged = true;
23
+ result.newId = updateResult.newId;
24
+ }
25
+ return result;
26
+ }
27
+ var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
28
+ function getDefaultExportFromCjs(x) {
29
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
30
+ }
31
+ const BUILTIN_STYLES = ["apa", "vancouver", "harvard"];
32
+ function isBuiltinStyle(styleName) {
33
+ return BUILTIN_STYLES.includes(styleName);
34
+ }
4
35
  const DEFAULT_TTL_MS = 60 * 60 * 1e3;
5
36
  const pmidCache = /* @__PURE__ */ new Map();
6
37
  const doiCache = /* @__PURE__ */ new Map();
@@ -610,10 +641,6 @@ const parser = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProper
610
641
  FormatParser,
611
642
  TypeParser
612
643
  }, Symbol.toStringTag, { value: "Module" }));
613
- var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
614
- function getDefaultExportFromCjs(x) {
615
- return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
616
- }
617
644
  var lib$1 = {};
618
645
  var input$2 = {};
619
646
  var hasRequiredInput$1;
@@ -1150,7 +1177,7 @@ function listDataParser(async2) {
1150
1177
  return Object.keys(async2 ? asyncParsers : parsers);
1151
1178
  }
1152
1179
  const formats$3 = {};
1153
- function add$5(format2, parsers2) {
1180
+ function add$6(format2, parsers2) {
1154
1181
  const formatParser = new FormatParser(format2, parsers2);
1155
1182
  formatParser.validate();
1156
1183
  const index = formats$3[format2] || (formats$3[format2] = {});
@@ -1192,7 +1219,7 @@ function remove$3(format2) {
1192
1219
  function has$3(format2) {
1193
1220
  return format2 in formats$3;
1194
1221
  }
1195
- function list$3() {
1222
+ function list$4() {
1196
1223
  return Object.keys(formats$3);
1197
1224
  }
1198
1225
  function ownKeys$6(e, r) {
@@ -4072,7 +4099,7 @@ const chainLinkAsync = async (input2) => {
4072
4099
  const util = Object.assign({}, dataType, graph, parser, csl);
4073
4100
  const input$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4074
4101
  __proto__: null,
4075
- add: add$5,
4102
+ add: add$6,
4076
4103
  addDataParser,
4077
4104
  addTypeParser,
4078
4105
  chain,
@@ -4085,7 +4112,7 @@ const input$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePrope
4085
4112
  has: has$3,
4086
4113
  hasDataParser,
4087
4114
  hasTypeParser,
4088
- list: list$3,
4115
+ list: list$4,
4089
4116
  listDataParser,
4090
4117
  listTypeParser,
4091
4118
  remove: remove$3,
@@ -4096,7 +4123,7 @@ const input$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePrope
4096
4123
  typeMatcher,
4097
4124
  util
4098
4125
  }, Symbol.toStringTag, { value: "Module" }));
4099
- function add$4(data2, options2 = {}, log2 = false) {
4126
+ function add$5(data2, options2 = {}, log2 = false) {
4100
4127
  if (options2 === true || log2 === true) {
4101
4128
  this.save();
4102
4129
  }
@@ -4140,7 +4167,7 @@ function reset(log2) {
4140
4167
  }
4141
4168
  const set$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4142
4169
  __proto__: null,
4143
- add: add$4,
4170
+ add: add$5,
4144
4171
  addAsync,
4145
4172
  reset,
4146
4173
  set,
@@ -4227,7 +4254,7 @@ function validate$2(name2, formatter) {
4227
4254
  }
4228
4255
  }
4229
4256
  const register$1 = new Register();
4230
- function add$3(name2, formatter) {
4257
+ function add$4(name2, formatter) {
4231
4258
  validate$2(name2, formatter);
4232
4259
  register$1.set(name2, formatter);
4233
4260
  }
@@ -4237,7 +4264,7 @@ function remove$2(name2) {
4237
4264
  function has$2(name2) {
4238
4265
  return register$1.has(name2);
4239
4266
  }
4240
- function list$2() {
4267
+ function list$3() {
4241
4268
  return register$1.list();
4242
4269
  }
4243
4270
  function format$7(name2, data2, ...options2) {
@@ -4248,10 +4275,10 @@ function format$7(name2, data2, ...options2) {
4248
4275
  }
4249
4276
  const output$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4250
4277
  __proto__: null,
4251
- add: add$3,
4278
+ add: add$4,
4252
4279
  format: format$7,
4253
4280
  has: has$2,
4254
- list: list$2,
4281
+ list: list$3,
4255
4282
  register: register$1,
4256
4283
  remove: remove$2
4257
4284
  }, Symbol.toStringTag, { value: "Module" }));
@@ -4382,7 +4409,7 @@ const register = new Register({
4382
4409
  listItem: [" ", "\n"]
4383
4410
  }
4384
4411
  });
4385
- function add$2(name2, dict2) {
4412
+ function add$3(name2, dict2) {
4386
4413
  validate$1(name2, dict2);
4387
4414
  register.set(name2, dict2);
4388
4415
  }
@@ -4392,7 +4419,7 @@ function remove$1(name2) {
4392
4419
  function has$1(name2) {
4393
4420
  return register.has(name2);
4394
4421
  }
4395
- function list$1() {
4422
+ function list$2() {
4396
4423
  return register.list();
4397
4424
  }
4398
4425
  function get$1(name2) {
@@ -4423,17 +4450,17 @@ const textDict = {
4423
4450
  };
4424
4451
  const dict = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4425
4452
  __proto__: null,
4426
- add: add$2,
4453
+ add: add$3,
4427
4454
  get: get$1,
4428
4455
  has: has$1,
4429
4456
  htmlDict,
4430
- list: list$1,
4457
+ list: list$2,
4431
4458
  register,
4432
4459
  remove: remove$1,
4433
4460
  textDict
4434
4461
  }, Symbol.toStringTag, { value: "Module" }));
4435
4462
  const configs = {};
4436
- function add$1(ref2, config2) {
4463
+ function add$2(ref2, config2) {
4437
4464
  configs[ref2] = config2;
4438
4465
  }
4439
4466
  function get(ref2) {
@@ -4445,15 +4472,15 @@ function has(ref2) {
4445
4472
  function remove(ref2) {
4446
4473
  delete configs[ref2];
4447
4474
  }
4448
- function list() {
4475
+ function list$1() {
4449
4476
  return Object.keys(configs);
4450
4477
  }
4451
4478
  const config$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
4452
4479
  __proto__: null,
4453
- add: add$1,
4480
+ add: add$2,
4454
4481
  get,
4455
4482
  has,
4456
- list,
4483
+ list: list$1,
4457
4484
  remove
4458
4485
  }, Symbol.toStringTag, { value: "Module" }));
4459
4486
  const registers = {
@@ -4463,7 +4490,7 @@ const registers = {
4463
4490
  config: config$2
4464
4491
  };
4465
4492
  const indices = {};
4466
- function add(ref2, plugins = {}) {
4493
+ function add$1(ref2, plugins = {}) {
4467
4494
  const mainIndex = indices[ref2] = {};
4468
4495
  for (const type2 in plugins) {
4469
4496
  if (type2 === "config") {
@@ -4623,7 +4650,7 @@ const json = {
4623
4650
  }
4624
4651
  };
4625
4652
  const output$2 = Object.assign({}, json, label$1);
4626
- add(ref$2, {
4653
+ add$1(ref$2, {
4627
4654
  input: formats$2,
4628
4655
  output: output$2
4629
4656
  });
@@ -4733,7 +4760,7 @@ const formats$1 = {
4733
4760
  parse: fetchDoiType
4734
4761
  }
4735
4762
  };
4736
- add(ref$1, {
4763
+ add$1(ref$1, {
4737
4764
  input: formats$1
4738
4765
  });
4739
4766
  const RATE_LIMITS = {
@@ -4902,8 +4929,8 @@ async function fetchDoi(doi2) {
4902
4929
  const rateLimiter = getRateLimiter("crossref", {});
4903
4930
  await rateLimiter.acquire();
4904
4931
  try {
4905
- const cite = await Cite.async(doi2);
4906
- const rawItems = cite.get({ format: "real", type: "json" });
4932
+ const cite2 = await Cite.async(doi2);
4933
+ const rawItems = cite2.get({ format: "real", type: "json" });
4907
4934
  if (!rawItems || !Array.isArray(rawItems) || rawItems.length === 0) {
4908
4935
  return {
4909
4936
  success: false,
@@ -8984,7 +9011,7 @@ const output = {
8984
9011
  biblatex: factory(format$3, format$2),
8985
9012
  bibtxt: factory(formatBibtex$1, format$1)
8986
9013
  };
8987
- add(ref, {
9014
+ add$1(ref, {
8988
9015
  input: formats,
8989
9016
  output,
8990
9017
  config: config$1
@@ -9509,7 +9536,7 @@ function format(data2, {
9509
9536
  }
9510
9537
  const oldProps = ["A1", "AV", "BT", "CP", "ED", "EP", "ID", "J1", "JA", "JF", "JO", "L2", "L3", "N2", "T1", "U1", "U2", "U3", "U4", "U5", "U6", "Y1"];
9511
9538
  const newProps = ["A4", "AD", "AN", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "CA", "CN", "DA", "DB", "DO", "DP", "ET", "LA", "LB", "NV", "OP", "PY", "RI", "RN", "SE", "ST", "SV", "TA", "TT"];
9512
- add("@ris", {
9539
+ add$1("@ris", {
9513
9540
  input: {
9514
9541
  "@ris/file": {
9515
9542
  parse,
@@ -9571,8 +9598,8 @@ function parseWithCitationJs(content, format2) {
9571
9598
  return { success: true, items: [] };
9572
9599
  }
9573
9600
  try {
9574
- const cite = new Cite(trimmed);
9575
- const items = cite.get({ format: "real", type: "json" });
9601
+ const cite2 = new Cite(trimmed);
9602
+ const items = cite2.get({ format: "real", type: "json" });
9576
9603
  if (!items || items.length === 0) {
9577
9604
  if (isEmptyFormat(trimmed, format2)) {
9578
9605
  return { success: true, items: [] };
@@ -9906,10 +9933,10 @@ async function addReferences(inputs, library2, options2) {
9906
9933
  const skipped = [];
9907
9934
  const importOptions = buildImportOptions(options2);
9908
9935
  const importResult = await importFromInputs(inputs, importOptions);
9909
- const existingItems = library2.getAll().map((ref2) => ref2.getItem());
9936
+ const existingItems = await library2.getAll();
9910
9937
  const addedIds = /* @__PURE__ */ new Set();
9911
9938
  for (const result of importResult.results) {
9912
- const processed = processImportResult(
9939
+ const processed = await processImportResult(
9913
9940
  result,
9914
9941
  existingItems,
9915
9942
  addedIds,
@@ -9942,7 +9969,7 @@ function buildImportOptions(options2) {
9942
9969
  }
9943
9970
  return importOptions;
9944
9971
  }
9945
- function processImportResult(result, existingItems, addedIds, force, library2) {
9972
+ async function processImportResult(result, existingItems, addedIds, force, library2) {
9946
9973
  if (!result.success) {
9947
9974
  return { type: "failed", item: { source: result.source, error: result.error } };
9948
9975
  }
@@ -9960,7 +9987,7 @@ function processImportResult(result, existingItems, addedIds, force, library2) {
9960
9987
  const allExistingIds = /* @__PURE__ */ new Set([...existingItems.map((i) => i.id), ...addedIds]);
9961
9988
  const { id: id2, changed } = resolveIdCollision(item.id, allExistingIds);
9962
9989
  const finalItem = { ...item, id: id2 };
9963
- library2.add(finalItem);
9990
+ await library2.add(finalItem);
9964
9991
  addedIds.add(id2);
9965
9992
  const addedItem = {
9966
9993
  id: id2,
@@ -9995,10 +10022,10 @@ function resolveIdCollision(baseId, existingIds) {
9995
10022
  } while (existingIds.has(newId));
9996
10023
  return { id: newId, changed: true };
9997
10024
  }
9998
- const BUILTIN_STYLES = ["apa", "vancouver", "harvard"];
9999
- function isBuiltinStyle(styleName) {
10000
- return BUILTIN_STYLES.includes(styleName);
10001
- }
10025
+ const add = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10026
+ __proto__: null,
10027
+ addReferences
10028
+ }, Symbol.toStringTag, { value: "Module" }));
10002
10029
  function formatAuthor(author2) {
10003
10030
  const family = author2.family || "";
10004
10031
  const givenInitial = author2.given ? `${author2.given.charAt(0)}.` : "";
@@ -29332,7 +29359,7 @@ function citation(data2, options2 = {}) {
29332
29359
  const output2 = citeproc.rebuildProcessorState([...before, citation2, ...after], format2, []);
29333
29360
  return output2[before.length][2];
29334
29361
  }
29335
- add("@csl", {
29362
+ add$1("@csl", {
29336
29363
  output: {
29337
29364
  bibliography,
29338
29365
  citation
@@ -29351,8 +29378,8 @@ function formatBibliographyCSL(items, options2) {
29351
29378
  const format2 = options2.format || "text";
29352
29379
  const locale = options2.locale || "en-US";
29353
29380
  try {
29354
- const cite = new Cite(items);
29355
- const result = cite.format("bibliography", {
29381
+ const cite2 = new Cite(items);
29382
+ const result = cite2.format("bibliography", {
29356
29383
  format: format2,
29357
29384
  template: style,
29358
29385
  lang: locale
@@ -29375,8 +29402,8 @@ function formatInTextCSL(items, options2) {
29375
29402
  const format2 = options2.format || "text";
29376
29403
  const locale = options2.locale || "en-US";
29377
29404
  try {
29378
- const cite = new Cite(items);
29379
- const result = cite.format("citation", {
29405
+ const cite2 = new Cite(items);
29406
+ const result = cite2.format("citation", {
29380
29407
  format: format2,
29381
29408
  template: style,
29382
29409
  lang: locale
@@ -29410,9 +29437,9 @@ function formatCitation(item, inText, options2) {
29410
29437
  }
29411
29438
  return inText ? formatInTextCSL([item], { style, locale, format: format2 }) : formatBibliographyCSL([item], { style, locale, format: format2 });
29412
29439
  }
29413
- function generateCitationForIdentifier(library2, identifier2, byUuid, inText, options2) {
29414
- const reference = byUuid ? library2.findByUuid(identifier2) : library2.findById(identifier2);
29415
- if (!reference) {
29440
+ async function generateCitationForIdentifier(library2, identifier2, byUuid, inText, options2) {
29441
+ const item = await library2.find(identifier2, { byUuid });
29442
+ if (!item) {
29416
29443
  const lookupType = byUuid ? "UUID" : "ID";
29417
29444
  return {
29418
29445
  success: false,
@@ -29420,7 +29447,6 @@ function generateCitationForIdentifier(library2, identifier2, byUuid, inText, op
29420
29447
  error: `Reference with ${lookupType} '${identifier2}' not found`
29421
29448
  };
29422
29449
  }
29423
- const item = reference.getItem();
29424
29450
  const citation2 = formatCitation(item, inText, options2);
29425
29451
  return {
29426
29452
  success: true,
@@ -29432,7 +29458,7 @@ async function citeReferences(library2, options2) {
29432
29458
  const { identifiers, byUuid = false, inText = false, style, cslFile, locale, format: format2 } = options2;
29433
29459
  const results = [];
29434
29460
  for (const identifier2 of identifiers) {
29435
- const result = generateCitationForIdentifier(library2, identifier2, byUuid, inText, {
29461
+ const result = await generateCitationForIdentifier(library2, identifier2, byUuid, inText, {
29436
29462
  style,
29437
29463
  cslFile,
29438
29464
  locale,
@@ -29442,33 +29468,13 @@ async function citeReferences(library2, options2) {
29442
29468
  }
29443
29469
  return { results };
29444
29470
  }
29445
- async function updateReference(library2, options2) {
29446
- const { identifier: identifier2, byUuid = false, updates, onIdCollision = "fail" } = options2;
29447
- const updateResult = byUuid ? library2.updateByUuid(identifier2, updates, { onIdCollision }) : library2.updateById(identifier2, updates, { onIdCollision });
29448
- if (!updateResult.updated) {
29449
- const result2 = { updated: false };
29450
- if (updateResult.idCollision) {
29451
- result2.idCollision = true;
29452
- }
29453
- return result2;
29454
- }
29455
- await library2.save();
29456
- const reference = byUuid ? library2.findByUuid(identifier2) : library2.findById(updateResult.newId ?? identifier2);
29457
- const result = { updated: true };
29458
- const item = reference?.getItem();
29459
- if (item) {
29460
- result.item = item;
29461
- }
29462
- if (updateResult.idChanged && updateResult.newId) {
29463
- result.idChanged = true;
29464
- result.newId = updateResult.newId;
29465
- }
29466
- return result;
29467
- }
29468
- function listReferences(library2, options2) {
29471
+ const cite = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
29472
+ __proto__: null,
29473
+ citeReferences
29474
+ }, Symbol.toStringTag, { value: "Module" }));
29475
+ async function listReferences(library2, options2) {
29469
29476
  const format2 = options2.format ?? "pretty";
29470
- const references = library2.getAll();
29471
- const items = references.map((ref2) => ref2.getItem());
29477
+ const items = await library2.getAll();
29472
29478
  switch (format2) {
29473
29479
  case "json":
29474
29480
  return { items: items.map((item) => JSON.stringify(item)) };
@@ -29486,30 +29492,28 @@ function listReferences(library2, options2) {
29486
29492
  return { items: items.map((item) => formatPretty([item])) };
29487
29493
  }
29488
29494
  }
29495
+ const list = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
29496
+ __proto__: null,
29497
+ listReferences
29498
+ }, Symbol.toStringTag, { value: "Module" }));
29489
29499
  async function removeReference(library2, options2) {
29490
29500
  const { identifier: identifier2, byUuid = false } = options2;
29491
- const reference = byUuid ? library2.findByUuid(identifier2) : library2.findById(identifier2);
29492
- if (!reference) {
29493
- return { removed: false };
29494
- }
29495
- const item = reference.getItem();
29496
- const removed = byUuid ? library2.removeByUuid(identifier2) : library2.removeById(identifier2);
29497
- if (removed) {
29501
+ const result = await library2.remove(identifier2, { byUuid });
29502
+ if (result.removed) {
29498
29503
  await library2.save();
29499
29504
  }
29500
- return { removed, item };
29505
+ return result;
29501
29506
  }
29502
- function searchReferences(library2, options2) {
29507
+ async function searchReferences(library2, options2) {
29503
29508
  const format2 = options2.format ?? "pretty";
29504
29509
  const query = options2.query;
29505
- const references = library2.getAll();
29506
- const allItems = references.map((ref2) => ref2.getItem());
29510
+ const allItems = await library2.getAll();
29507
29511
  let matchedItems;
29508
29512
  if (!query.trim()) {
29509
29513
  matchedItems = allItems;
29510
29514
  } else {
29511
29515
  const tokens = tokenize(query).tokens;
29512
- const results = search(allItems, tokens);
29516
+ const results = search$1(allItems, tokens);
29513
29517
  const sorted = sortResults(results);
29514
29518
  matchedItems = sorted.map((result) => result.reference);
29515
29519
  }
@@ -29530,12 +29534,318 @@ function searchReferences(library2, options2) {
29530
29534
  return { items: matchedItems.map((item) => formatPretty([item])) };
29531
29535
  }
29532
29536
  }
29537
+ const search = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
29538
+ __proto__: null,
29539
+ searchReferences
29540
+ }, Symbol.toStringTag, { value: "Module" }));
29541
+ function buildPubmedConfig(config2) {
29542
+ const pubmedConfig = {};
29543
+ if (config2.pubmed.email !== void 0) {
29544
+ pubmedConfig.email = config2.pubmed.email;
29545
+ }
29546
+ if (config2.pubmed.apiKey !== void 0) {
29547
+ pubmedConfig.apiKey = config2.pubmed.apiKey;
29548
+ }
29549
+ return pubmedConfig;
29550
+ }
29551
+ function createAddRoute(library2, config2) {
29552
+ const route = new Hono();
29553
+ route.post("/", async (c) => {
29554
+ let body;
29555
+ try {
29556
+ body = await c.req.json();
29557
+ } catch {
29558
+ return c.json({ error: "Invalid JSON" }, 400);
29559
+ }
29560
+ if (!body || typeof body !== "object") {
29561
+ return c.json({ error: "Request body must be an object" }, 400);
29562
+ }
29563
+ const { inputs, options: options2 } = body;
29564
+ if (!inputs || !Array.isArray(inputs) || inputs.length === 0) {
29565
+ return c.json({ error: "inputs must be a non-empty array of strings" }, 400);
29566
+ }
29567
+ if (!inputs.every((input2) => typeof input2 === "string")) {
29568
+ return c.json({ error: "All inputs must be strings" }, 400);
29569
+ }
29570
+ const addOptions = {
29571
+ force: options2?.force ?? false,
29572
+ pubmedConfig: buildPubmedConfig(config2)
29573
+ };
29574
+ if (options2?.format) {
29575
+ addOptions.format = options2.format;
29576
+ }
29577
+ const result = await addReferences(inputs, library2, addOptions);
29578
+ return c.json(result);
29579
+ });
29580
+ return route;
29581
+ }
29582
+ const CiteRequestSchema = z.object({
29583
+ identifiers: z.array(z.string()).min(1, "identifiers must be a non-empty array"),
29584
+ byUuid: z.boolean().optional(),
29585
+ inText: z.boolean().optional(),
29586
+ style: z.string().optional(),
29587
+ cslFile: z.string().optional(),
29588
+ locale: z.string().optional(),
29589
+ format: z.enum(["text", "html"]).optional()
29590
+ });
29591
+ function buildCiteOptions(body) {
29592
+ return {
29593
+ identifiers: body.identifiers,
29594
+ ...body.byUuid !== void 0 && { byUuid: body.byUuid },
29595
+ ...body.inText !== void 0 && { inText: body.inText },
29596
+ ...body.style !== void 0 && { style: body.style },
29597
+ ...body.cslFile !== void 0 && { cslFile: body.cslFile },
29598
+ ...body.locale !== void 0 && { locale: body.locale },
29599
+ ...body.format !== void 0 && { format: body.format }
29600
+ };
29601
+ }
29602
+ function createCiteRoute(library2) {
29603
+ const route = new Hono();
29604
+ route.post("/", async (c) => {
29605
+ let rawBody;
29606
+ try {
29607
+ rawBody = await c.req.json();
29608
+ } catch {
29609
+ return c.json({ error: "Invalid JSON" }, 400);
29610
+ }
29611
+ const parseResult = CiteRequestSchema.safeParse(rawBody);
29612
+ if (!parseResult.success) {
29613
+ const errorMessage = parseResult.error.issues[0]?.message ?? "Invalid request body";
29614
+ return c.json({ error: errorMessage }, 400);
29615
+ }
29616
+ const result = await citeReferences(library2, buildCiteOptions(parseResult.data));
29617
+ return c.json(result);
29618
+ });
29619
+ return route;
29620
+ }
29621
+ const healthRoute = new Hono();
29622
+ healthRoute.get("/", (c) => {
29623
+ return c.json({ status: "ok" });
29624
+ });
29625
+ const listRequestBodySchema = z.object({
29626
+ format: z.enum(["pretty", "json", "bibtex", "ids-only", "uuid"]).optional()
29627
+ });
29628
+ function createListRoute(library2) {
29629
+ const route = new Hono();
29630
+ route.post("/", async (c) => {
29631
+ let body;
29632
+ try {
29633
+ body = await c.req.json();
29634
+ } catch {
29635
+ return c.json({ error: "Invalid JSON" }, 400);
29636
+ }
29637
+ const parseResult = listRequestBodySchema.safeParse(body);
29638
+ if (!parseResult.success) {
29639
+ return c.json({ error: "Request body must be an object" }, 400);
29640
+ }
29641
+ const requestBody = parseResult.data;
29642
+ const options2 = {};
29643
+ if (requestBody.format !== void 0) {
29644
+ options2.format = requestBody.format;
29645
+ }
29646
+ const result = await listReferences(library2, options2);
29647
+ return c.json(result);
29648
+ });
29649
+ return route;
29650
+ }
29651
+ function createReferencesRoute(library2) {
29652
+ const route = new Hono();
29653
+ route.get("/", async (c) => {
29654
+ const items = await library2.getAll();
29655
+ return c.json(items);
29656
+ });
29657
+ route.get("/uuid/:uuid", async (c) => {
29658
+ const uuid = c.req.param("uuid");
29659
+ const item = await library2.find(uuid, { byUuid: true });
29660
+ if (!item) {
29661
+ return c.json({ error: "Reference not found" }, 404);
29662
+ }
29663
+ return c.json(item);
29664
+ });
29665
+ route.get("/id/:id", async (c) => {
29666
+ const id2 = c.req.param("id");
29667
+ const item = await library2.find(id2);
29668
+ if (!item) {
29669
+ return c.json({ error: "Reference not found" }, 404);
29670
+ }
29671
+ return c.json(item);
29672
+ });
29673
+ route.post("/", async (c) => {
29674
+ try {
29675
+ const body = await c.req.json();
29676
+ const addedItem = await library2.add(body);
29677
+ return c.json(addedItem, 201);
29678
+ } catch (error) {
29679
+ return c.json(
29680
+ {
29681
+ error: "Invalid request body",
29682
+ details: error instanceof Error ? error.message : String(error)
29683
+ },
29684
+ 400
29685
+ );
29686
+ }
29687
+ });
29688
+ route.put("/uuid/:uuid", async (c) => {
29689
+ const uuid = c.req.param("uuid");
29690
+ let body;
29691
+ try {
29692
+ body = await c.req.json();
29693
+ } catch {
29694
+ return c.json({ error: "Invalid JSON" }, 400);
29695
+ }
29696
+ if (!body || typeof body !== "object") {
29697
+ return c.json({ error: "Request body must be an object" }, 400);
29698
+ }
29699
+ const { updates, onIdCollision } = body;
29700
+ if (!updates || typeof updates !== "object") {
29701
+ return c.json({ error: "Request body must contain 'updates' object" }, 400);
29702
+ }
29703
+ const result = await updateReference(library2, {
29704
+ identifier: uuid,
29705
+ byUuid: true,
29706
+ updates,
29707
+ onIdCollision: onIdCollision ?? "suffix"
29708
+ });
29709
+ if (!result.updated) {
29710
+ const status = result.idCollision ? 409 : 404;
29711
+ return c.json(result, status);
29712
+ }
29713
+ return c.json(result);
29714
+ });
29715
+ route.put("/id/:id", async (c) => {
29716
+ const id2 = c.req.param("id");
29717
+ let body;
29718
+ try {
29719
+ body = await c.req.json();
29720
+ } catch {
29721
+ return c.json({ error: "Invalid JSON" }, 400);
29722
+ }
29723
+ if (!body || typeof body !== "object") {
29724
+ return c.json({ error: "Request body must be an object" }, 400);
29725
+ }
29726
+ const { updates, onIdCollision } = body;
29727
+ if (!updates || typeof updates !== "object") {
29728
+ return c.json({ error: "Request body must contain 'updates' object" }, 400);
29729
+ }
29730
+ const result = await updateReference(library2, {
29731
+ identifier: id2,
29732
+ byUuid: false,
29733
+ updates,
29734
+ onIdCollision: onIdCollision ?? "suffix"
29735
+ });
29736
+ if (!result.updated) {
29737
+ const status = result.idCollision ? 409 : 404;
29738
+ return c.json(result, status);
29739
+ }
29740
+ return c.json(result);
29741
+ });
29742
+ route.delete("/uuid/:uuid", async (c) => {
29743
+ const uuid = c.req.param("uuid");
29744
+ const result = await removeReference(library2, {
29745
+ identifier: uuid,
29746
+ byUuid: true
29747
+ });
29748
+ if (!result.removed) {
29749
+ return c.json(result, 404);
29750
+ }
29751
+ return c.json(result);
29752
+ });
29753
+ route.delete("/id/:id", async (c) => {
29754
+ const id2 = c.req.param("id");
29755
+ const result = await removeReference(library2, {
29756
+ identifier: id2,
29757
+ byUuid: false
29758
+ });
29759
+ if (!result.removed) {
29760
+ return c.json(result, 404);
29761
+ }
29762
+ return c.json(result);
29763
+ });
29764
+ return route;
29765
+ }
29766
+ const searchRequestBodySchema = z.object({
29767
+ query: z.string(),
29768
+ format: z.enum(["pretty", "json", "bibtex", "ids-only", "uuid"]).optional()
29769
+ });
29770
+ function createSearchRoute(library2) {
29771
+ const route = new Hono();
29772
+ route.post("/", async (c) => {
29773
+ let body;
29774
+ try {
29775
+ body = await c.req.json();
29776
+ } catch {
29777
+ return c.json({ error: "Invalid JSON" }, 400);
29778
+ }
29779
+ const parseResult = searchRequestBodySchema.safeParse(body);
29780
+ if (!parseResult.success) {
29781
+ return c.json({ error: "Invalid request body" }, 400);
29782
+ }
29783
+ const requestBody = parseResult.data;
29784
+ const options2 = {
29785
+ query: requestBody.query
29786
+ };
29787
+ if (requestBody.format !== void 0) {
29788
+ options2.format = requestBody.format;
29789
+ }
29790
+ const result = await searchReferences(library2, options2);
29791
+ return c.json(result);
29792
+ });
29793
+ return route;
29794
+ }
29795
+ function createServer(library2, config2) {
29796
+ const app = new Hono();
29797
+ app.route("/health", healthRoute);
29798
+ const referencesRoute = createReferencesRoute(library2);
29799
+ app.route("/api/references", referencesRoute);
29800
+ const addRoute = createAddRoute(library2, config2);
29801
+ app.route("/api/add", addRoute);
29802
+ const citeRoute = createCiteRoute(library2);
29803
+ app.route("/api/cite", citeRoute);
29804
+ const listRoute = createListRoute(library2);
29805
+ app.route("/api/list", listRoute);
29806
+ const searchRoute = createSearchRoute(library2);
29807
+ app.route("/api/search", searchRoute);
29808
+ return app;
29809
+ }
29810
+ async function startServerWithFileWatcher(libraryPath, config2) {
29811
+ const library2 = await Library.load(libraryPath);
29812
+ const app = createServer(library2, config2);
29813
+ const fileWatcher = new FileWatcher(libraryPath, {
29814
+ debounceMs: config2.watch.debounceMs,
29815
+ maxRetries: config2.watch.maxRetries,
29816
+ retryDelayMs: config2.watch.retryIntervalMs,
29817
+ pollIntervalMs: config2.watch.pollIntervalMs
29818
+ });
29819
+ fileWatcher.on("change", () => {
29820
+ library2.reload().catch((error) => {
29821
+ console.error("Failed to reload library:", error);
29822
+ });
29823
+ });
29824
+ await fileWatcher.start();
29825
+ const dispose = async () => {
29826
+ fileWatcher.close();
29827
+ };
29828
+ return {
29829
+ app,
29830
+ library: library2,
29831
+ fileWatcher,
29832
+ dispose
29833
+ };
29834
+ }
29533
29835
  export {
29836
+ BUILTIN_STYLES as B,
29534
29837
  addReferences as a,
29838
+ startServerWithFileWatcher as b,
29535
29839
  citeReferences as c,
29840
+ createServer as d,
29841
+ add as e,
29842
+ cite as f,
29843
+ getDefaultExportFromCjs as g,
29844
+ list as h,
29845
+ search as i,
29536
29846
  listReferences as l,
29537
29847
  removeReference as r,
29538
29848
  searchReferences as s,
29539
29849
  updateReference as u
29540
29850
  };
29541
- //# sourceMappingURL=search-Be9vzUIH.js.map
29851
+ //# sourceMappingURL=index-DLIGxQaB.js.map