@foxnose/sdk 0.2.2 → 0.3.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.
package/README.md CHANGED
@@ -100,6 +100,64 @@ console.log(schema.searchable_fields);
100
100
  client.close();
101
101
  ```
102
102
 
103
+ ### Vector Search
104
+
105
+ The Flux client provides typed convenience methods for all vector search modes:
106
+
107
+ ```typescript
108
+ import { FluxClient, SearchMode, buildSearchBody } from '@foxnose/sdk';
109
+
110
+ // Semantic search (auto-generated embeddings)
111
+ const results = await client.vectorSearch('articles', {
112
+ query: 'machine learning in healthcare',
113
+ top_k: 10,
114
+ similarity_threshold: 0.7,
115
+ });
116
+
117
+ // Custom embedding search
118
+ const results = await client.vectorFieldSearch('articles', {
119
+ field: 'content_embedding',
120
+ query_vector: [0.012, -0.034, 0.056 /* ... */],
121
+ top_k: 20,
122
+ });
123
+
124
+ // Hybrid text + vector search
125
+ const results = await client.hybridSearch('articles', {
126
+ query: 'ML applications',
127
+ find_text: { query: 'machine learning' },
128
+ vector_weight: 0.7,
129
+ text_weight: 0.3,
130
+ });
131
+
132
+ // Boosted search (keywords boosted by vector similarity)
133
+ const results = await client.boostedSearch('articles', {
134
+ find_text: { query: 'python tutorial' },
135
+ query: 'beginner programming guide',
136
+ boost_factor: 1.5,
137
+ });
138
+
139
+ // Extra parameters (where, sort) are forwarded to the API
140
+ const results = await client.vectorSearch('articles', {
141
+ query: 'climate change',
142
+ limit: 5,
143
+ sort: '-published_at',
144
+ where: { category: 'science' },
145
+ });
146
+ ```
147
+
148
+ You can also use `buildSearchBody()` for full control with the raw `search()` method:
149
+
150
+ ```typescript
151
+ const body = buildSearchBody({
152
+ search_mode: SearchMode.HYBRID,
153
+ find_text: { query: 'python' },
154
+ vector_search: { query: 'programming tutorials', top_k: 10 },
155
+ hybrid_config: { vector_weight: 0.6, text_weight: 0.4 },
156
+ limit: 20,
157
+ });
158
+ const results = await client.search('articles', body);
159
+ ```
160
+
103
161
  ### API Folder Route Descriptions
104
162
 
105
163
  You can configure per-route descriptions when connecting a folder to an API.
package/dist/index.cjs CHANGED
@@ -32,10 +32,12 @@ __export(index_exports, {
32
32
  JWTAuth: () => JWTAuth,
33
33
  ManagementClient: () => ManagementClient,
34
34
  SDK_VERSION: () => SDK_VERSION,
35
+ SearchMode: () => SearchMode,
35
36
  SecureKeyAuth: () => SecureKeyAuth,
36
37
  SimpleKeyAuth: () => SimpleKeyAuth,
37
38
  StaticTokenProvider: () => StaticTokenProvider,
38
39
  VERSION: () => SDK_VERSION,
40
+ buildSearchBody: () => buildSearchBody,
39
41
  createConfig: () => createConfig,
40
42
  resolveKey: () => resolveKey
41
43
  });
@@ -147,9 +149,6 @@ var SecureKeyAuth = class {
147
149
  try {
148
150
  const parsed = new URL(request.url);
149
151
  path = parsed.pathname || "/";
150
- if (parsed.search) {
151
- path = `${path}${parsed.search}`;
152
- }
153
152
  } catch {
154
153
  path = request.path || "/";
155
154
  }
@@ -193,7 +192,7 @@ var DEFAULT_RETRY_CONFIG = {
193
192
  statusCodes: [408, 425, 429, 500, 502, 503, 504],
194
193
  methods: ["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]
195
194
  };
196
- var SDK_VERSION = "0.2.1";
195
+ var SDK_VERSION = "0.3.0";
197
196
  var DEFAULT_USER_AGENT = `foxnose-sdk-js/${SDK_VERSION}`;
198
197
  function createConfig(options) {
199
198
  if (!options.baseUrl) {
@@ -1359,6 +1358,156 @@ var ManagementClient = class {
1359
1358
  }
1360
1359
  };
1361
1360
 
1361
+ // src/flux/models.ts
1362
+ var SearchMode = {
1363
+ TEXT: "text",
1364
+ VECTOR: "vector",
1365
+ VECTOR_BOOSTED: "vector_boosted",
1366
+ HYBRID: "hybrid"
1367
+ };
1368
+ var SEARCH_REQUEST_KNOWN_KEYS = /* @__PURE__ */ new Set([
1369
+ "search_mode",
1370
+ "find_text",
1371
+ "find_phrase",
1372
+ "vector_search",
1373
+ "vector_field_search",
1374
+ "vector_boost_config",
1375
+ "hybrid_config",
1376
+ "limit",
1377
+ "offset"
1378
+ ]);
1379
+ function checkFinite(value, name) {
1380
+ if (!Number.isFinite(value)) {
1381
+ throw new Error(`${name} must be a finite number`);
1382
+ }
1383
+ }
1384
+ function checkThreshold(value, name) {
1385
+ if (value != null) {
1386
+ checkFinite(value, name);
1387
+ if (value < 0 || value > 1) {
1388
+ throw new Error(`${name} must be between 0.0 and 1.0`);
1389
+ }
1390
+ }
1391
+ }
1392
+ function checkPositiveInt(value, name) {
1393
+ if (value != null) {
1394
+ checkFinite(value, name);
1395
+ if (!Number.isInteger(value) || value < 1) {
1396
+ throw new Error(`${name} must be a positive integer (>= 1)`);
1397
+ }
1398
+ }
1399
+ }
1400
+ function validateVectorSearch(vs) {
1401
+ checkPositiveInt(vs.top_k, "top_k");
1402
+ checkThreshold(vs.similarity_threshold, "similarity_threshold");
1403
+ }
1404
+ function validateVectorFieldSearch(vfs) {
1405
+ if (!vfs.query_vector || vfs.query_vector.length === 0) {
1406
+ throw new Error("query_vector must not be empty");
1407
+ }
1408
+ for (let i = 0; i < vfs.query_vector.length; i++) {
1409
+ if (!Number.isFinite(vfs.query_vector[i])) {
1410
+ throw new Error(`query_vector[${i}] must be a finite number`);
1411
+ }
1412
+ }
1413
+ checkPositiveInt(vfs.top_k, "top_k");
1414
+ checkThreshold(vfs.similarity_threshold, "similarity_threshold");
1415
+ }
1416
+ function validateBoostConfig(cfg) {
1417
+ if (cfg.boost_factor != null) {
1418
+ checkFinite(cfg.boost_factor, "boost_factor");
1419
+ if (cfg.boost_factor <= 0) {
1420
+ throw new Error("boost_factor must be > 0");
1421
+ }
1422
+ }
1423
+ checkThreshold(cfg.similarity_threshold, "similarity_threshold");
1424
+ checkPositiveInt(cfg.max_boost_results, "max_boost_results");
1425
+ }
1426
+ function validateHybridConfig(cfg) {
1427
+ if (cfg.vector_weight != null) {
1428
+ checkFinite(cfg.vector_weight, "vector_weight");
1429
+ if (cfg.vector_weight < 0 || cfg.vector_weight > 1) {
1430
+ throw new Error("vector_weight must be between 0.0 and 1.0");
1431
+ }
1432
+ }
1433
+ if (cfg.text_weight != null) {
1434
+ checkFinite(cfg.text_weight, "text_weight");
1435
+ if (cfg.text_weight < 0 || cfg.text_weight > 1) {
1436
+ throw new Error("text_weight must be between 0.0 and 1.0");
1437
+ }
1438
+ }
1439
+ const vw = cfg.vector_weight ?? 0.6;
1440
+ const tw = cfg.text_weight ?? 0.4;
1441
+ if (Math.abs(vw + tw - 1) > 1e-6) {
1442
+ throw new Error(`vector_weight + text_weight must equal 1.0, got ${vw + tw}`);
1443
+ }
1444
+ }
1445
+ function buildSearchBody(req) {
1446
+ const mode = req.search_mode ?? SearchMode.TEXT;
1447
+ const vs = req.vector_search;
1448
+ const vfs = req.vector_field_search;
1449
+ const boost = req.vector_boost_config;
1450
+ const hybrid = req.hybrid_config;
1451
+ const hasText = req.find_text != null || req.find_phrase != null;
1452
+ if (vs) validateVectorSearch(vs);
1453
+ if (vfs) validateVectorFieldSearch(vfs);
1454
+ if (boost) validateBoostConfig(boost);
1455
+ if (hybrid) validateHybridConfig(hybrid);
1456
+ if (vs && vfs) {
1457
+ throw new Error("vector_search and vector_field_search are mutually exclusive");
1458
+ }
1459
+ if (mode === SearchMode.TEXT) {
1460
+ if (vs) throw new Error("vector_search is not allowed in text search mode");
1461
+ if (vfs) throw new Error("vector_field_search is not allowed in text search mode");
1462
+ if (boost) throw new Error("vector_boost_config is not allowed in text search mode");
1463
+ if (hybrid) throw new Error("hybrid_config is not allowed in text search mode");
1464
+ } else if (mode === SearchMode.VECTOR) {
1465
+ if (!vs && !vfs) {
1466
+ throw new Error("vector search mode requires vector_search or vector_field_search");
1467
+ }
1468
+ if (boost) throw new Error("vector_boost_config is not allowed in vector search mode");
1469
+ if (hybrid) throw new Error("hybrid_config is not allowed in vector search mode");
1470
+ } else if (mode === SearchMode.VECTOR_BOOSTED) {
1471
+ if (!vs && !vfs) {
1472
+ throw new Error("vector_boosted mode requires vector_search or vector_field_search");
1473
+ }
1474
+ if (!hasText) throw new Error("vector_boosted mode requires find_text or find_phrase");
1475
+ if (hybrid) throw new Error("hybrid_config is not allowed in vector_boosted mode");
1476
+ } else if (mode === SearchMode.HYBRID) {
1477
+ if (vfs) throw new Error("vector_field_search is not allowed in hybrid mode");
1478
+ if (!vs) throw new Error("hybrid mode requires vector_search");
1479
+ if (!hasText) throw new Error("hybrid mode requires find_text or find_phrase");
1480
+ if (boost) throw new Error("vector_boost_config is not allowed in hybrid mode");
1481
+ } else {
1482
+ const validModes = Object.values(SearchMode).join(", ");
1483
+ throw new Error(`Unknown search_mode: "${mode}". Valid modes: ${validModes}`);
1484
+ }
1485
+ const body = {};
1486
+ for (const [key, value] of Object.entries(req)) {
1487
+ if (value !== void 0) {
1488
+ body[key] = value;
1489
+ }
1490
+ }
1491
+ if (!body.search_mode) {
1492
+ body.search_mode = mode;
1493
+ }
1494
+ return body;
1495
+ }
1496
+ function mergeExtra(validated, extra) {
1497
+ const conflicts = [];
1498
+ for (const key of Object.keys(extra)) {
1499
+ if (SEARCH_REQUEST_KNOWN_KEYS.has(key)) {
1500
+ conflicts.push(key);
1501
+ }
1502
+ }
1503
+ if (conflicts.length > 0) {
1504
+ throw new Error(
1505
+ `extra keys conflict with SearchRequest fields: ${conflicts.sort().join(", ")}. Use the explicit parameters instead.`
1506
+ );
1507
+ }
1508
+ return { ...validated, ...extra };
1509
+ }
1510
+
1362
1511
  // src/flux/client.ts
1363
1512
  function cleanPrefix(prefix) {
1364
1513
  const value = prefix.replace(/^\/+|\/+$/g, "");
@@ -1370,6 +1519,15 @@ function cleanPrefix(prefix) {
1370
1519
  function normalizeFolderPath(folderPath) {
1371
1520
  return folderPath.replace(/^\/+|\/+$/g, "");
1372
1521
  }
1522
+ function stripUndefined(obj) {
1523
+ const result = {};
1524
+ for (const [key, value] of Object.entries(obj)) {
1525
+ if (value !== void 0) {
1526
+ result[key] = value;
1527
+ }
1528
+ }
1529
+ return result;
1530
+ }
1373
1531
  var FluxClient = class {
1374
1532
  apiPrefix;
1375
1533
  transport;
@@ -1403,6 +1561,123 @@ var FluxClient = class {
1403
1561
  const path = this.buildPath(folderPath, "/_search");
1404
1562
  return this.transport.request("POST", path, { jsonBody: body });
1405
1563
  }
1564
+ /** Semantic search using auto-generated embeddings. */
1565
+ async vectorSearch(folderPath, options) {
1566
+ const { query, fields, top_k = 10, similarity_threshold, limit, offset, ...rest } = options;
1567
+ const extra = stripUndefined(rest);
1568
+ const vs = { query, fields, top_k, similarity_threshold };
1569
+ const req = {
1570
+ search_mode: SearchMode.VECTOR,
1571
+ vector_search: vs,
1572
+ limit,
1573
+ offset
1574
+ };
1575
+ const body = mergeExtra(buildSearchBody(req), extra);
1576
+ return this.search(folderPath, body);
1577
+ }
1578
+ /** Search using custom pre-computed embeddings. */
1579
+ async vectorFieldSearch(folderPath, options) {
1580
+ const {
1581
+ field,
1582
+ query_vector,
1583
+ top_k = 10,
1584
+ similarity_threshold,
1585
+ limit,
1586
+ offset,
1587
+ ...rest
1588
+ } = options;
1589
+ const extra = stripUndefined(rest);
1590
+ const vfs = { field, query_vector, top_k, similarity_threshold };
1591
+ const req = {
1592
+ search_mode: SearchMode.VECTOR,
1593
+ vector_field_search: vfs,
1594
+ limit,
1595
+ offset
1596
+ };
1597
+ const body = mergeExtra(buildSearchBody(req), extra);
1598
+ return this.search(folderPath, body);
1599
+ }
1600
+ /** Blended text + vector search with configurable weights. */
1601
+ async hybridSearch(folderPath, options) {
1602
+ const {
1603
+ query,
1604
+ find_text,
1605
+ fields,
1606
+ top_k = 10,
1607
+ similarity_threshold,
1608
+ vector_weight = 0.6,
1609
+ text_weight = 0.4,
1610
+ rerank_results = true,
1611
+ limit,
1612
+ offset,
1613
+ ...rest
1614
+ } = options;
1615
+ const extra = stripUndefined(rest);
1616
+ const vs = { query, fields, top_k, similarity_threshold };
1617
+ const hybrid = { vector_weight, text_weight, rerank_results };
1618
+ const req = {
1619
+ search_mode: SearchMode.HYBRID,
1620
+ find_text,
1621
+ vector_search: vs,
1622
+ hybrid_config: hybrid,
1623
+ limit,
1624
+ offset
1625
+ };
1626
+ const body = mergeExtra(buildSearchBody(req), extra);
1627
+ return this.search(folderPath, body);
1628
+ }
1629
+ /** Text search with results boosted by vector similarity. */
1630
+ async boostedSearch(folderPath, options) {
1631
+ const {
1632
+ find_text,
1633
+ query,
1634
+ field,
1635
+ query_vector,
1636
+ top_k = 10,
1637
+ similarity_threshold,
1638
+ boost_factor = 1.5,
1639
+ boost_similarity_threshold,
1640
+ max_boost_results = 20,
1641
+ limit,
1642
+ offset,
1643
+ ...rest
1644
+ } = options;
1645
+ const extra = stripUndefined(rest);
1646
+ const hasAuto = query != null;
1647
+ const hasCustom = field != null || query_vector != null;
1648
+ if (hasAuto && hasCustom) {
1649
+ throw new Error(
1650
+ "Provide either 'query' for auto-generated embeddings or 'field' + 'query_vector' for custom embeddings, not both"
1651
+ );
1652
+ }
1653
+ let vs;
1654
+ let vfs;
1655
+ if (hasAuto) {
1656
+ vs = { query, top_k, similarity_threshold };
1657
+ } else if (field != null && query_vector != null) {
1658
+ vfs = { field, query_vector, top_k, similarity_threshold };
1659
+ } else {
1660
+ throw new Error(
1661
+ "Provide either 'query' for auto-generated embeddings or 'field' + 'query_vector' for custom embeddings"
1662
+ );
1663
+ }
1664
+ const boostConfig = {
1665
+ boost_factor,
1666
+ similarity_threshold: boost_similarity_threshold,
1667
+ max_boost_results
1668
+ };
1669
+ const req = {
1670
+ search_mode: SearchMode.VECTOR_BOOSTED,
1671
+ find_text,
1672
+ vector_search: vs,
1673
+ vector_field_search: vfs,
1674
+ vector_boost_config: boostConfig,
1675
+ limit,
1676
+ offset
1677
+ };
1678
+ const body = mergeExtra(buildSearchBody(req), extra);
1679
+ return this.search(folderPath, body);
1680
+ }
1406
1681
  async getRouter() {
1407
1682
  const path = `/${this.apiPrefix}/_router`;
1408
1683
  return this.transport.request("GET", path);
@@ -1429,10 +1704,12 @@ var FluxClient = class {
1429
1704
  JWTAuth,
1430
1705
  ManagementClient,
1431
1706
  SDK_VERSION,
1707
+ SearchMode,
1432
1708
  SecureKeyAuth,
1433
1709
  SimpleKeyAuth,
1434
1710
  StaticTokenProvider,
1435
1711
  VERSION,
1712
+ buildSearchBody,
1436
1713
  createConfig,
1437
1714
  resolveKey
1438
1715
  });