@btst/plugin-search 0.0.0-stage → 1.0.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 (86) hide show
  1. package/LICENSE.md +17 -0
  2. package/dist/api/index.cjs +17 -0
  3. package/dist/api/index.d.cts +62 -0
  4. package/dist/api/index.d.mts +62 -0
  5. package/dist/api/index.d.ts +62 -0
  6. package/dist/api/index.mjs +4 -0
  7. package/dist/api/operations.cjs +127 -0
  8. package/dist/api/operations.mjs +125 -0
  9. package/dist/api/plugin.cjs +23 -0
  10. package/dist/api/plugin.mjs +21 -0
  11. package/dist/client/components/index.cjs +11 -0
  12. package/dist/client/components/index.d.cts +35 -0
  13. package/dist/client/components/index.d.mts +35 -0
  14. package/dist/client/components/index.d.ts +35 -0
  15. package/dist/client/components/index.mjs +3 -0
  16. package/dist/client/components/pages/search-page.cjs +39 -0
  17. package/dist/client/components/pages/search-page.internal.cjs +100 -0
  18. package/dist/client/components/pages/search-page.internal.mjs +98 -0
  19. package/dist/client/components/pages/search-page.mjs +37 -0
  20. package/dist/client/components/search-dialog.cjs +148 -0
  21. package/dist/client/components/search-dialog.mjs +145 -0
  22. package/dist/client/components/search-results.cjs +100 -0
  23. package/dist/client/components/search-results.mjs +96 -0
  24. package/dist/client/components/source-filter.cjs +31 -0
  25. package/dist/client/components/source-filter.mjs +29 -0
  26. package/dist/client/constants.cjs +5 -0
  27. package/dist/client/constants.mjs +3 -0
  28. package/dist/client/hooks/index.cjs +10 -0
  29. package/dist/client/hooks/index.d.cts +340 -0
  30. package/dist/client/hooks/index.d.mts +340 -0
  31. package/dist/client/hooks/index.d.ts +340 -0
  32. package/dist/client/hooks/index.mjs +3 -0
  33. package/dist/client/hooks/use-search-state.cjs +36 -0
  34. package/dist/client/hooks/use-search-state.mjs +34 -0
  35. package/dist/client/hooks/use-search.cjs +44 -0
  36. package/dist/client/hooks/use-search.mjs +42 -0
  37. package/dist/client/index.cjs +7 -0
  38. package/dist/client/index.d.cts +55 -0
  39. package/dist/client/index.d.mts +55 -0
  40. package/dist/client/index.d.ts +55 -0
  41. package/dist/client/index.mjs +1 -0
  42. package/dist/client/plugin.cjs +76 -0
  43. package/dist/client/plugin.mjs +74 -0
  44. package/dist/client/search-state.cjs +20 -0
  45. package/dist/client/search-state.mjs +17 -0
  46. package/dist/permissions.cjs +12 -0
  47. package/dist/permissions.d.cts +12 -0
  48. package/dist/permissions.d.mts +12 -0
  49. package/dist/permissions.d.ts +12 -0
  50. package/dist/permissions.mjs +10 -0
  51. package/dist/providers.cjs +122 -0
  52. package/dist/providers.d.cts +82 -0
  53. package/dist/providers.d.mts +82 -0
  54. package/dist/providers.d.ts +82 -0
  55. package/dist/providers.mjs +116 -0
  56. package/dist/query-keys.cjs +35 -0
  57. package/dist/query-keys.d.cts +71 -0
  58. package/dist/query-keys.d.mts +71 -0
  59. package/dist/query-keys.d.ts +71 -0
  60. package/dist/query-keys.mjs +32 -0
  61. package/dist/schemas.cjs +23 -0
  62. package/dist/schemas.d.cts +14 -0
  63. package/dist/schemas.d.mts +14 -0
  64. package/dist/schemas.d.ts +14 -0
  65. package/dist/schemas.mjs +17 -0
  66. package/dist/shared/plugin-search.5GvThkZW.d.cts +20 -0
  67. package/dist/shared/plugin-search.5GvThkZW.d.mts +20 -0
  68. package/dist/shared/plugin-search.5GvThkZW.d.ts +20 -0
  69. package/dist/style.css +2 -0
  70. package/dist/types.cjs +2 -0
  71. package/dist/types.d.cts +53 -0
  72. package/dist/types.d.mts +53 -0
  73. package/dist/types.d.ts +53 -0
  74. package/dist/types.mjs +1 -0
  75. package/dist/ui/src/components/button.cjs +56 -0
  76. package/dist/ui/src/components/button.mjs +53 -0
  77. package/dist/ui/src/components/command.cjs +88 -0
  78. package/dist/ui/src/components/command.mjs +83 -0
  79. package/dist/ui/src/components/dialog.cjs +127 -0
  80. package/dist/ui/src/components/dialog.mjs +105 -0
  81. package/dist/ui/src/components/input.cjs +23 -0
  82. package/dist/ui/src/components/input.mjs +21 -0
  83. package/dist/ui/src/lib/utils.cjs +10 -0
  84. package/dist/ui/src/lib/utils.mjs +8 -0
  85. package/package.json +181 -3
  86. package/README.md +0 -3
package/LICENSE.md ADDED
@@ -0,0 +1,17 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2025 - present, olliethedev
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5
+ and associated documentation files (the "Software"), to deal in the Software without restriction,
6
+ including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
8
+ is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or
11
+ substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14
+ BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const plugin = require('./plugin.cjs');
4
+ const permissions = require('../permissions.cjs');
5
+ const schemas = require('../schemas.cjs');
6
+ const providers = require('../providers.cjs');
7
+
8
+
9
+
10
+ exports.searchBackendPlugin = plugin.searchBackendPlugin;
11
+ exports.searchPermissions = permissions.searchPermissions;
12
+ exports.searchInputSchema = schemas.searchInputSchema;
13
+ exports.blogSearchProvider = providers.blogSearchProvider;
14
+ exports.cmsSearchProvider = providers.cmsSearchProvider;
15
+ exports.ecommerceSearchProvider = providers.ecommerceSearchProvider;
16
+ exports.schoolSearchProvider = providers.schoolSearchProvider;
17
+ exports.searchSnippet = providers.searchSnippet;
@@ -0,0 +1,62 @@
1
+ import * as _btst_stack_plugins_api from '@btst/stack/plugins/api';
2
+ import * as _btst_stack_authorization from '@btst/stack/authorization';
3
+ import * as zod_v4_core from 'zod/v4/core';
4
+ import * as zod from 'zod';
5
+ import * as better_call from 'better-call';
6
+ import { SearchBackendConfig, SearchResponse } from '../types.cjs';
7
+ export { SearchInput, SearchProvider, SearchProviderResult, SearchResult, SearchSource } from '../types.cjs';
8
+ export { searchPermissions } from '../permissions.cjs';
9
+ export { searchInputSchema } from '../schemas.cjs';
10
+ export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.cjs';
11
+
12
+ declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack_plugins_api.BackendPlugin<{
13
+ search: better_call.StrictEndpoint<"/search", {
14
+ requireRequest: true;
15
+ } & {
16
+ method: "GET";
17
+ } & {
18
+ query: better_call.StandardSchemaV1<{
19
+ q: string;
20
+ sources?: unknown;
21
+ page?: unknown;
22
+ limit?: unknown;
23
+ }, {
24
+ q: string;
25
+ sources?: unknown;
26
+ page?: unknown;
27
+ limit?: unknown;
28
+ }>;
29
+ }, {
30
+ readonly items: readonly {
31
+ readonly resourceId: string;
32
+ readonly title: string;
33
+ readonly snippet: string;
34
+ readonly href: string;
35
+ readonly key: string;
36
+ readonly sourceId: string;
37
+ readonly sourceLabel: string;
38
+ }[];
39
+ readonly sources: readonly {
40
+ readonly id: string;
41
+ readonly label: string;
42
+ }[];
43
+ readonly page: number;
44
+ readonly limit: number;
45
+ readonly hasMore: boolean;
46
+ readonly nextPage: number | null;
47
+ readonly truncated: boolean;
48
+ }>;
49
+ }, never, {
50
+ search: _btst_stack_plugins_api.Operation<zod.ZodObject<{
51
+ q: zod.ZodString;
52
+ sources: zod.ZodPreprocess<zod.ZodOptional<zod.ZodArray<zod.ZodString>>, unknown>;
53
+ page: zod.ZodDefault<zod.ZodCoercedNumber<unknown>>;
54
+ limit: zod.ZodDefault<zod.ZodCoercedNumber<unknown>>;
55
+ }, zod_v4_core.$strip>, _btst_stack_authorization.PermissionDescriptor<"search:query.read", zod.ZodObject<{
56
+ sources: zod.ZodArray<zod.ZodString>;
57
+ }, zod_v4_core.$strip>>, SearchResponse, false>;
58
+ }, "search">;
59
+ type SearchApiRouter = ReturnType<ReturnType<typeof searchBackendPlugin>["routes"]>;
60
+
61
+ export { SearchBackendConfig, SearchResponse, searchBackendPlugin };
62
+ export type { SearchApiRouter };
@@ -0,0 +1,62 @@
1
+ import * as _btst_stack_plugins_api from '@btst/stack/plugins/api';
2
+ import * as _btst_stack_authorization from '@btst/stack/authorization';
3
+ import * as zod_v4_core from 'zod/v4/core';
4
+ import * as zod from 'zod';
5
+ import * as better_call from 'better-call';
6
+ import { SearchBackendConfig, SearchResponse } from '../types.mjs';
7
+ export { SearchInput, SearchProvider, SearchProviderResult, SearchResult, SearchSource } from '../types.mjs';
8
+ export { searchPermissions } from '../permissions.mjs';
9
+ export { searchInputSchema } from '../schemas.mjs';
10
+ export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.mjs';
11
+
12
+ declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack_plugins_api.BackendPlugin<{
13
+ search: better_call.StrictEndpoint<"/search", {
14
+ requireRequest: true;
15
+ } & {
16
+ method: "GET";
17
+ } & {
18
+ query: better_call.StandardSchemaV1<{
19
+ q: string;
20
+ sources?: unknown;
21
+ page?: unknown;
22
+ limit?: unknown;
23
+ }, {
24
+ q: string;
25
+ sources?: unknown;
26
+ page?: unknown;
27
+ limit?: unknown;
28
+ }>;
29
+ }, {
30
+ readonly items: readonly {
31
+ readonly resourceId: string;
32
+ readonly title: string;
33
+ readonly snippet: string;
34
+ readonly href: string;
35
+ readonly key: string;
36
+ readonly sourceId: string;
37
+ readonly sourceLabel: string;
38
+ }[];
39
+ readonly sources: readonly {
40
+ readonly id: string;
41
+ readonly label: string;
42
+ }[];
43
+ readonly page: number;
44
+ readonly limit: number;
45
+ readonly hasMore: boolean;
46
+ readonly nextPage: number | null;
47
+ readonly truncated: boolean;
48
+ }>;
49
+ }, never, {
50
+ search: _btst_stack_plugins_api.Operation<zod.ZodObject<{
51
+ q: zod.ZodString;
52
+ sources: zod.ZodPreprocess<zod.ZodOptional<zod.ZodArray<zod.ZodString>>, unknown>;
53
+ page: zod.ZodDefault<zod.ZodCoercedNumber<unknown>>;
54
+ limit: zod.ZodDefault<zod.ZodCoercedNumber<unknown>>;
55
+ }, zod_v4_core.$strip>, _btst_stack_authorization.PermissionDescriptor<"search:query.read", zod.ZodObject<{
56
+ sources: zod.ZodArray<zod.ZodString>;
57
+ }, zod_v4_core.$strip>>, SearchResponse, false>;
58
+ }, "search">;
59
+ type SearchApiRouter = ReturnType<ReturnType<typeof searchBackendPlugin>["routes"]>;
60
+
61
+ export { SearchBackendConfig, SearchResponse, searchBackendPlugin };
62
+ export type { SearchApiRouter };
@@ -0,0 +1,62 @@
1
+ import * as _btst_stack_plugins_api from '@btst/stack/plugins/api';
2
+ import * as _btst_stack_authorization from '@btst/stack/authorization';
3
+ import * as zod_v4_core from 'zod/v4/core';
4
+ import * as zod from 'zod';
5
+ import * as better_call from 'better-call';
6
+ import { SearchBackendConfig, SearchResponse } from '../types.js';
7
+ export { SearchInput, SearchProvider, SearchProviderResult, SearchResult, SearchSource } from '../types.js';
8
+ export { searchPermissions } from '../permissions.js';
9
+ export { searchInputSchema } from '../schemas.js';
10
+ export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.js';
11
+
12
+ declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack_plugins_api.BackendPlugin<{
13
+ search: better_call.StrictEndpoint<"/search", {
14
+ requireRequest: true;
15
+ } & {
16
+ method: "GET";
17
+ } & {
18
+ query: better_call.StandardSchemaV1<{
19
+ q: string;
20
+ sources?: unknown;
21
+ page?: unknown;
22
+ limit?: unknown;
23
+ }, {
24
+ q: string;
25
+ sources?: unknown;
26
+ page?: unknown;
27
+ limit?: unknown;
28
+ }>;
29
+ }, {
30
+ readonly items: readonly {
31
+ readonly resourceId: string;
32
+ readonly title: string;
33
+ readonly snippet: string;
34
+ readonly href: string;
35
+ readonly key: string;
36
+ readonly sourceId: string;
37
+ readonly sourceLabel: string;
38
+ }[];
39
+ readonly sources: readonly {
40
+ readonly id: string;
41
+ readonly label: string;
42
+ }[];
43
+ readonly page: number;
44
+ readonly limit: number;
45
+ readonly hasMore: boolean;
46
+ readonly nextPage: number | null;
47
+ readonly truncated: boolean;
48
+ }>;
49
+ }, never, {
50
+ search: _btst_stack_plugins_api.Operation<zod.ZodObject<{
51
+ q: zod.ZodString;
52
+ sources: zod.ZodPreprocess<zod.ZodOptional<zod.ZodArray<zod.ZodString>>, unknown>;
53
+ page: zod.ZodDefault<zod.ZodCoercedNumber<unknown>>;
54
+ limit: zod.ZodDefault<zod.ZodCoercedNumber<unknown>>;
55
+ }, zod_v4_core.$strip>, _btst_stack_authorization.PermissionDescriptor<"search:query.read", zod.ZodObject<{
56
+ sources: zod.ZodArray<zod.ZodString>;
57
+ }, zod_v4_core.$strip>>, SearchResponse, false>;
58
+ }, "search">;
59
+ type SearchApiRouter = ReturnType<ReturnType<typeof searchBackendPlugin>["routes"]>;
60
+
61
+ export { SearchBackendConfig, SearchResponse, searchBackendPlugin };
62
+ export type { SearchApiRouter };
@@ -0,0 +1,4 @@
1
+ export { searchBackendPlugin } from './plugin.mjs';
2
+ export { searchPermissions } from '../permissions.mjs';
3
+ export { searchInputSchema } from '../schemas.mjs';
4
+ export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.mjs';
@@ -0,0 +1,127 @@
1
+ 'use strict';
2
+
3
+ const api = require('@btst/stack/plugins/api');
4
+ const zod = require('zod');
5
+ const permissions = require('../permissions.cjs');
6
+ const providers = require('../providers.cjs');
7
+ const schemas = require('../schemas.cjs');
8
+
9
+ const resultSchema = zod.z.object({
10
+ resourceId: zod.z.string().min(1).max(200),
11
+ title: zod.z.string().min(1).max(500),
12
+ snippet: zod.z.string().max(500),
13
+ href: zod.z.string().max(2e3).refine(
14
+ (value) => value.startsWith("/") && !value.startsWith("//") && !/[\\\u0000-\u0020]/.test(value),
15
+ "Search destinations must be application-relative paths"
16
+ )
17
+ });
18
+ function createSearchOperations(providers$1) {
19
+ if (providers$1.length > schemas.SEARCH_MAX_SOURCES) {
20
+ throw new Error(`Search supports at most ${schemas.SEARCH_MAX_SOURCES} providers`);
21
+ }
22
+ const sorted = [...providers$1].sort(
23
+ (a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0
24
+ );
25
+ const ids = /* @__PURE__ */ new Set();
26
+ for (const provider of sorted) {
27
+ schemas.searchSourceIdSchema.parse(provider.id);
28
+ zod.z.string().min(1).max(100).parse(provider.label);
29
+ if (ids.has(provider.id))
30
+ throw new Error(`Duplicate search source: ${provider.id}`);
31
+ ids.add(provider.id);
32
+ }
33
+ const sources = sorted.map(({ id, label }) => ({ id, label }));
34
+ return {
35
+ search: api.defineOperation({
36
+ input: schemas.searchInputSchema,
37
+ permission: permissions.searchPermissions.query.read,
38
+ facts: ({ input }) => {
39
+ const selected = input.sources?.length ? [...new Set(input.sources)].sort() : sources.map(({ id }) => id);
40
+ if (selected.some((id) => !ids.has(id))) {
41
+ throw new api.OperationHttpError(
42
+ 400,
43
+ "Unknown search source",
44
+ "SEARCH_SOURCE_INVALID"
45
+ );
46
+ }
47
+ return { sources: selected };
48
+ },
49
+ execute: async ({
50
+ input,
51
+ facts,
52
+ request,
53
+ identity
54
+ }) => {
55
+ if (!request)
56
+ throw new api.OperationHttpError(
57
+ 400,
58
+ "Search requires request context",
59
+ "SEARCH_REQUEST_REQUIRED"
60
+ );
61
+ const empty = {
62
+ sources,
63
+ page: input.page,
64
+ limit: input.limit,
65
+ hasMore: false,
66
+ nextPage: null,
67
+ truncated: false
68
+ };
69
+ if (!input.q) return { ...empty, items: [] };
70
+ let truncated = false;
71
+ const pages = await Promise.all(
72
+ sorted.filter((provider) => facts.sources.includes(provider.id)).map(async (provider) => {
73
+ try {
74
+ const page = await provider.search({
75
+ q: input.q,
76
+ limit: schemas.SEARCH_MAX_RESULTS + 1,
77
+ request,
78
+ identity
79
+ });
80
+ if (page.items.length > schemas.SEARCH_MAX_RESULTS + 1 || typeof page.hasMore !== "boolean")
81
+ throw new Error("Invalid provider page");
82
+ truncated ||= page.hasMore;
83
+ const seen = /* @__PURE__ */ new Set();
84
+ return page.items.flatMap((record) => {
85
+ const item = resultSchema.parse(record);
86
+ if (seen.has(item.resourceId)) return [];
87
+ seen.add(item.resourceId);
88
+ return [
89
+ {
90
+ ...item,
91
+ snippet: providers.searchSnippet(item.snippet),
92
+ key: `${provider.id}:${item.resourceId}`,
93
+ sourceId: provider.id,
94
+ sourceLabel: provider.label
95
+ }
96
+ ];
97
+ }).sort(
98
+ (a, b) => a.resourceId < b.resourceId ? -1 : a.resourceId > b.resourceId ? 1 : 0
99
+ );
100
+ } catch {
101
+ throw new api.OperationHttpError(
102
+ 502,
103
+ "A search source is unavailable",
104
+ "SEARCH_PROVIDER_FAILED"
105
+ );
106
+ }
107
+ })
108
+ );
109
+ const items = pages.flat();
110
+ const end = input.page * input.limit;
111
+ const hasMore = input.page < schemas.SEARCH_MAX_PAGES && end < Math.min(items.length, schemas.SEARCH_MAX_RESULTS);
112
+ return {
113
+ ...empty,
114
+ items: items.slice(
115
+ end - input.limit,
116
+ Math.min(end, schemas.SEARCH_MAX_RESULTS)
117
+ ),
118
+ hasMore,
119
+ nextPage: hasMore ? input.page + 1 : null,
120
+ truncated: truncated || items.length > Math.min(schemas.SEARCH_MAX_RESULTS, input.limit * schemas.SEARCH_MAX_PAGES)
121
+ };
122
+ }
123
+ })
124
+ };
125
+ }
126
+
127
+ exports.createSearchOperations = createSearchOperations;
@@ -0,0 +1,125 @@
1
+ import { defineOperation, OperationHttpError } from '@btst/stack/plugins/api';
2
+ import { z } from 'zod';
3
+ import { searchPermissions } from '../permissions.mjs';
4
+ import { searchSnippet } from '../providers.mjs';
5
+ import { SEARCH_MAX_SOURCES, searchSourceIdSchema, searchInputSchema, SEARCH_MAX_RESULTS, SEARCH_MAX_PAGES } from '../schemas.mjs';
6
+
7
+ const resultSchema = z.object({
8
+ resourceId: z.string().min(1).max(200),
9
+ title: z.string().min(1).max(500),
10
+ snippet: z.string().max(500),
11
+ href: z.string().max(2e3).refine(
12
+ (value) => value.startsWith("/") && !value.startsWith("//") && !/[\\\u0000-\u0020]/.test(value),
13
+ "Search destinations must be application-relative paths"
14
+ )
15
+ });
16
+ function createSearchOperations(providers) {
17
+ if (providers.length > SEARCH_MAX_SOURCES) {
18
+ throw new Error(`Search supports at most ${SEARCH_MAX_SOURCES} providers`);
19
+ }
20
+ const sorted = [...providers].sort(
21
+ (a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0
22
+ );
23
+ const ids = /* @__PURE__ */ new Set();
24
+ for (const provider of sorted) {
25
+ searchSourceIdSchema.parse(provider.id);
26
+ z.string().min(1).max(100).parse(provider.label);
27
+ if (ids.has(provider.id))
28
+ throw new Error(`Duplicate search source: ${provider.id}`);
29
+ ids.add(provider.id);
30
+ }
31
+ const sources = sorted.map(({ id, label }) => ({ id, label }));
32
+ return {
33
+ search: defineOperation({
34
+ input: searchInputSchema,
35
+ permission: searchPermissions.query.read,
36
+ facts: ({ input }) => {
37
+ const selected = input.sources?.length ? [...new Set(input.sources)].sort() : sources.map(({ id }) => id);
38
+ if (selected.some((id) => !ids.has(id))) {
39
+ throw new OperationHttpError(
40
+ 400,
41
+ "Unknown search source",
42
+ "SEARCH_SOURCE_INVALID"
43
+ );
44
+ }
45
+ return { sources: selected };
46
+ },
47
+ execute: async ({
48
+ input,
49
+ facts,
50
+ request,
51
+ identity
52
+ }) => {
53
+ if (!request)
54
+ throw new OperationHttpError(
55
+ 400,
56
+ "Search requires request context",
57
+ "SEARCH_REQUEST_REQUIRED"
58
+ );
59
+ const empty = {
60
+ sources,
61
+ page: input.page,
62
+ limit: input.limit,
63
+ hasMore: false,
64
+ nextPage: null,
65
+ truncated: false
66
+ };
67
+ if (!input.q) return { ...empty, items: [] };
68
+ let truncated = false;
69
+ const pages = await Promise.all(
70
+ sorted.filter((provider) => facts.sources.includes(provider.id)).map(async (provider) => {
71
+ try {
72
+ const page = await provider.search({
73
+ q: input.q,
74
+ limit: SEARCH_MAX_RESULTS + 1,
75
+ request,
76
+ identity
77
+ });
78
+ if (page.items.length > SEARCH_MAX_RESULTS + 1 || typeof page.hasMore !== "boolean")
79
+ throw new Error("Invalid provider page");
80
+ truncated ||= page.hasMore;
81
+ const seen = /* @__PURE__ */ new Set();
82
+ return page.items.flatMap((record) => {
83
+ const item = resultSchema.parse(record);
84
+ if (seen.has(item.resourceId)) return [];
85
+ seen.add(item.resourceId);
86
+ return [
87
+ {
88
+ ...item,
89
+ snippet: searchSnippet(item.snippet),
90
+ key: `${provider.id}:${item.resourceId}`,
91
+ sourceId: provider.id,
92
+ sourceLabel: provider.label
93
+ }
94
+ ];
95
+ }).sort(
96
+ (a, b) => a.resourceId < b.resourceId ? -1 : a.resourceId > b.resourceId ? 1 : 0
97
+ );
98
+ } catch {
99
+ throw new OperationHttpError(
100
+ 502,
101
+ "A search source is unavailable",
102
+ "SEARCH_PROVIDER_FAILED"
103
+ );
104
+ }
105
+ })
106
+ );
107
+ const items = pages.flat();
108
+ const end = input.page * input.limit;
109
+ const hasMore = input.page < SEARCH_MAX_PAGES && end < Math.min(items.length, SEARCH_MAX_RESULTS);
110
+ return {
111
+ ...empty,
112
+ items: items.slice(
113
+ end - input.limit,
114
+ Math.min(end, SEARCH_MAX_RESULTS)
115
+ ),
116
+ hasMore,
117
+ nextPage: hasMore ? input.page + 1 : null,
118
+ truncated: truncated || items.length > Math.min(SEARCH_MAX_RESULTS, input.limit * SEARCH_MAX_PAGES)
119
+ };
120
+ }
121
+ })
122
+ };
123
+ }
124
+
125
+ export { createSearchOperations };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const api = require('@btst/stack/plugins/api');
4
+ const schemas = require('../schemas.cjs');
5
+ const operations = require('./operations.cjs');
6
+
7
+ const searchBackendPlugin = (config = {}) => {
8
+ const operations$1 = operations.createSearchOperations(config.providers ?? []);
9
+ return api.defineBackendPlugin({
10
+ id: "search",
11
+ dbPlugin: api.createDbPlugin("search", {}),
12
+ operations: () => operations$1,
13
+ routes: (_adapter, _context, bound) => ({
14
+ search: api.createEndpoint(
15
+ "/search",
16
+ { method: "GET", query: schemas.searchInputSchema, requireRequest: true },
17
+ bound.search.route((ctx) => ctx.query)
18
+ )
19
+ })
20
+ });
21
+ };
22
+
23
+ exports.searchBackendPlugin = searchBackendPlugin;
@@ -0,0 +1,21 @@
1
+ import { defineBackendPlugin, createEndpoint, createDbPlugin } from '@btst/stack/plugins/api';
2
+ import { searchInputSchema } from '../schemas.mjs';
3
+ import { createSearchOperations } from './operations.mjs';
4
+
5
+ const searchBackendPlugin = (config = {}) => {
6
+ const operations = createSearchOperations(config.providers ?? []);
7
+ return defineBackendPlugin({
8
+ id: "search",
9
+ dbPlugin: createDbPlugin("search", {}),
10
+ operations: () => operations,
11
+ routes: (_adapter, _context, bound) => ({
12
+ search: createEndpoint(
13
+ "/search",
14
+ { method: "GET", query: searchInputSchema, requireRequest: true },
15
+ bound.search.route((ctx) => ctx.query)
16
+ )
17
+ })
18
+ });
19
+ };
20
+
21
+ export { searchBackendPlugin };
@@ -0,0 +1,11 @@
1
+ 'use client';
2
+ 'use strict';
3
+
4
+ const searchDialog = require('./search-dialog.cjs');
5
+ const searchPage = require('./pages/search-page.cjs');
6
+
7
+
8
+
9
+ exports.SearchDialog = searchDialog.SearchDialog;
10
+ exports.SearchInput = searchDialog.SearchInput;
11
+ exports.SearchPageComponent = searchPage.SearchPageComponent;
@@ -0,0 +1,35 @@
1
+ 'use client';
2
+ import * as react from 'react';
3
+ import { S as SearchListState } from '../../shared/plugin-search.5GvThkZW.cjs';
4
+ import '@btst/stack/client';
5
+
6
+ /** Shared discovery options. Supplying sources fixes the allowed UI scope. */
7
+ interface SearchPresentationProps {
8
+ /** Fixed provider IDs; URL source filters cannot expand this selection. */
9
+ sources?: readonly string[];
10
+ className?: string;
11
+ placeholder?: string;
12
+ emptyMessage?: string;
13
+ title?: string;
14
+ }
15
+ /** Controlled discovery dialog with keyboard selection and router navigation. */
16
+ interface SearchDialogProps extends SearchPresentationProps {
17
+ open: boolean;
18
+ onOpenChange: (open: boolean) => void;
19
+ }
20
+ declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, }: SearchDialogProps): react.JSX.Element;
21
+ /** Trigger for the shared dialog, including the optional Cmd/Ctrl+K shortcut. */
22
+ interface SearchInputProps extends SearchPresentationProps {
23
+ triggerClassName?: string;
24
+ buttonText?: string;
25
+ /** Disable when a page contains several independently scoped triggers. */
26
+ keyboardShortcut?: boolean;
27
+ }
28
+ declare function SearchInput({ triggerClassName, buttonText, keyboardShortcut, ...props }: SearchInputProps): react.JSX.Element;
29
+
30
+ declare function SearchPageComponent({ initialSearch, }: {
31
+ initialSearch?: SearchListState;
32
+ }): react.JSX.Element;
33
+
34
+ export { SearchDialog, SearchInput, SearchPageComponent };
35
+ export type { SearchDialogProps, SearchInputProps, SearchPresentationProps };
@@ -0,0 +1,35 @@
1
+ 'use client';
2
+ import * as react from 'react';
3
+ import { S as SearchListState } from '../../shared/plugin-search.5GvThkZW.mjs';
4
+ import '@btst/stack/client';
5
+
6
+ /** Shared discovery options. Supplying sources fixes the allowed UI scope. */
7
+ interface SearchPresentationProps {
8
+ /** Fixed provider IDs; URL source filters cannot expand this selection. */
9
+ sources?: readonly string[];
10
+ className?: string;
11
+ placeholder?: string;
12
+ emptyMessage?: string;
13
+ title?: string;
14
+ }
15
+ /** Controlled discovery dialog with keyboard selection and router navigation. */
16
+ interface SearchDialogProps extends SearchPresentationProps {
17
+ open: boolean;
18
+ onOpenChange: (open: boolean) => void;
19
+ }
20
+ declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, }: SearchDialogProps): react.JSX.Element;
21
+ /** Trigger for the shared dialog, including the optional Cmd/Ctrl+K shortcut. */
22
+ interface SearchInputProps extends SearchPresentationProps {
23
+ triggerClassName?: string;
24
+ buttonText?: string;
25
+ /** Disable when a page contains several independently scoped triggers. */
26
+ keyboardShortcut?: boolean;
27
+ }
28
+ declare function SearchInput({ triggerClassName, buttonText, keyboardShortcut, ...props }: SearchInputProps): react.JSX.Element;
29
+
30
+ declare function SearchPageComponent({ initialSearch, }: {
31
+ initialSearch?: SearchListState;
32
+ }): react.JSX.Element;
33
+
34
+ export { SearchDialog, SearchInput, SearchPageComponent };
35
+ export type { SearchDialogProps, SearchInputProps, SearchPresentationProps };
@@ -0,0 +1,35 @@
1
+ 'use client';
2
+ import * as react from 'react';
3
+ import { S as SearchListState } from '../../shared/plugin-search.5GvThkZW.js';
4
+ import '@btst/stack/client';
5
+
6
+ /** Shared discovery options. Supplying sources fixes the allowed UI scope. */
7
+ interface SearchPresentationProps {
8
+ /** Fixed provider IDs; URL source filters cannot expand this selection. */
9
+ sources?: readonly string[];
10
+ className?: string;
11
+ placeholder?: string;
12
+ emptyMessage?: string;
13
+ title?: string;
14
+ }
15
+ /** Controlled discovery dialog with keyboard selection and router navigation. */
16
+ interface SearchDialogProps extends SearchPresentationProps {
17
+ open: boolean;
18
+ onOpenChange: (open: boolean) => void;
19
+ }
20
+ declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, }: SearchDialogProps): react.JSX.Element;
21
+ /** Trigger for the shared dialog, including the optional Cmd/Ctrl+K shortcut. */
22
+ interface SearchInputProps extends SearchPresentationProps {
23
+ triggerClassName?: string;
24
+ buttonText?: string;
25
+ /** Disable when a page contains several independently scoped triggers. */
26
+ keyboardShortcut?: boolean;
27
+ }
28
+ declare function SearchInput({ triggerClassName, buttonText, keyboardShortcut, ...props }: SearchInputProps): react.JSX.Element;
29
+
30
+ declare function SearchPageComponent({ initialSearch, }: {
31
+ initialSearch?: SearchListState;
32
+ }): react.JSX.Element;
33
+
34
+ export { SearchDialog, SearchInput, SearchPageComponent };
35
+ export type { SearchDialogProps, SearchInputProps, SearchPresentationProps };
@@ -0,0 +1,3 @@
1
+ 'use client';
2
+ export { SearchDialog, SearchInput } from './search-dialog.mjs';
3
+ export { SearchPageComponent } from './pages/search-page.mjs';