@kweaver-ai/kweaver-sdk 0.4.6 → 0.4.7

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/bin/kweaver.js CHANGED
@@ -1,9 +1,24 @@
1
1
  #!/usr/bin/env node
2
+
3
+ function exit(code) {
4
+ if (process.stdout.writableNeedDrain || process.stderr.writableNeedDrain) {
5
+ const done = () => {
6
+ if (!process.stdout.writableNeedDrain && !process.stderr.writableNeedDrain) {
7
+ process.exit(code);
8
+ }
9
+ };
10
+ process.stdout.once("drain", done);
11
+ process.stderr.once("drain", done);
12
+ } else {
13
+ process.exit(code);
14
+ }
15
+ }
16
+
2
17
  import("../dist/cli.js").then(({ run }) => {
3
18
  run(process.argv.slice(2))
4
- .then((code) => process.exit(code))
19
+ .then((code) => exit(code))
5
20
  .catch((err) => {
6
21
  console.error(err instanceof Error ? err.message : String(err));
7
- process.exit(1);
22
+ exit(1);
8
23
  });
9
24
  });
package/dist/cli.js CHANGED
@@ -87,14 +87,28 @@ export async function run(argv) {
87
87
  printHelp();
88
88
  return 1;
89
89
  }
90
+ function safeExit(code) {
91
+ if (process.stdout.writableNeedDrain || process.stderr.writableNeedDrain) {
92
+ const done = () => {
93
+ if (!process.stdout.writableNeedDrain && !process.stderr.writableNeedDrain) {
94
+ process.exit(code);
95
+ }
96
+ };
97
+ process.stdout.once("drain", done);
98
+ process.stderr.once("drain", done);
99
+ }
100
+ else {
101
+ process.exit(code);
102
+ }
103
+ }
90
104
  if (import.meta.url === `file://${process.argv[1]}`) {
91
105
  run(process.argv.slice(2))
92
106
  .then((code) => {
93
- process.exit(code);
107
+ safeExit(code);
94
108
  })
95
109
  .catch((error) => {
96
110
  const message = error instanceof Error ? error.message : String(error);
97
111
  console.error(message);
98
- process.exit(1);
112
+ safeExit(1);
99
113
  });
100
114
  }
@@ -456,6 +456,57 @@ function parseJsonObject(text, errorMessage) {
456
456
  }
457
457
  return parsed;
458
458
  }
459
+ const MAX_OUTPUT_BYTES = 100_000;
460
+ /**
461
+ * If a query response exceeds MAX_OUTPUT_BYTES, trim the datas array
462
+ * to fit, preserving valid JSON and the search_after cursor for pagination.
463
+ */
464
+ function truncateQueryResult(raw) {
465
+ if (raw.length <= MAX_OUTPUT_BYTES) {
466
+ return raw;
467
+ }
468
+ let parsed;
469
+ try {
470
+ parsed = JSON.parse(raw);
471
+ }
472
+ catch {
473
+ return raw;
474
+ }
475
+ const datas = parsed.datas;
476
+ if (!Array.isArray(datas) || datas.length === 0) {
477
+ return raw;
478
+ }
479
+ const originalCount = datas.length;
480
+ while (datas.length > 1) {
481
+ datas.pop();
482
+ const candidate = JSON.stringify(parsed);
483
+ if (candidate.length <= MAX_OUTPUT_BYTES) {
484
+ const remaining = originalCount - datas.length;
485
+ const sa = parsed.search_after;
486
+ parsed._truncated = {
487
+ returned: datas.length,
488
+ total_fetched: originalCount,
489
+ remaining,
490
+ next_search_after: sa ?? null,
491
+ hint: sa
492
+ ? `Pass --search-after '${JSON.stringify(sa)}' --limit ${datas.length} to fetch the next page.`
493
+ : `Reduce --limit to ${datas.length} or less to avoid truncation.`,
494
+ };
495
+ console.error(`[warn] Truncated ${originalCount} → ${datas.length} records (output exceeded ${Math.round(MAX_OUTPUT_BYTES / 1024)}KB). ${parsed._truncated.hint}`);
496
+ return JSON.stringify(parsed);
497
+ }
498
+ }
499
+ const sa = parsed.search_after;
500
+ parsed._truncated = {
501
+ returned: 1,
502
+ total_fetched: originalCount,
503
+ remaining: originalCount - 1,
504
+ next_search_after: sa ?? null,
505
+ hint: `Single record is very large. Use --limit 1 and --search-after to iterate.`,
506
+ };
507
+ console.error(`[warn] Truncated ${originalCount} → 1 record. Single record is very large. Use --limit 1 and --search-after to iterate.`);
508
+ return JSON.stringify(parsed);
509
+ }
459
510
  function parseSearchAfterArray(text) {
460
511
  let parsed;
461
512
  try {
@@ -1017,11 +1068,7 @@ properties JSON format: {"_instance_identities":[{"<primary-key>":"<value>"}],"p
1017
1068
  body: options.body,
1018
1069
  businessDomain: options.businessDomain,
1019
1070
  });
1020
- const OUTPUT_WARN_BYTES = 100_000;
1021
- if (result.length > OUTPUT_WARN_BYTES) {
1022
- console.error(`[warn] Response is ${(result.length / 1024).toFixed(0)}KB. Use a smaller --limit or --search-after to paginate.`);
1023
- }
1024
- console.log(formatCallOutput(result, options.pretty));
1071
+ console.log(formatCallOutput(truncateQueryResult(result), options.pretty));
1025
1072
  return 0;
1026
1073
  }
1027
1074
  if (action === "properties") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kweaver-ai/kweaver-sdk",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "description": "KWeaver TypeScript SDK — CLI tool and programmatic API for knowledge networks and Decision Agents.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",