@dbx-tools/search 0.6.223 → 0.6.224

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/src/tool.ts CHANGED
@@ -2,14 +2,15 @@
2
2
  * The `search`, `universal_search`, and (opt-in) `add_documents`,
3
3
  * `create_index`, and `sync_index` Mastra tools.
4
4
  *
5
- * All three read the shared runtime primed by the plugin, so a tool spread
6
- * into an agent uses the deployment's default index, columns, page size, and
7
- * mode without any per-tool wiring. They run under the caller's OBO scope (the
8
- * client resolves the execution context's workspace client), so search runs as
9
- * the requesting user and Unity Catalog ACLs apply.
5
+ * App-integrated agents should consume the plugin's native toolkit so calls
6
+ * retain that plugin's config and provider backend. These standalone factories
7
+ * capture an explicit runtime or build an isolated one from config plus a
8
+ * provider. They run under the caller's OBO scope (the client resolves the
9
+ * execution context's workspace client), so search runs as the requesting user
10
+ * and Unity Catalog ACLs apply.
10
11
  *
11
12
  * The same tools are exposed to AppKit's own agents through the plugin's
12
- * `ToolProvider` (see `plugin.ts`); this module is the Mastra half.
13
+ * `ToolProvider` (see `plugin.ts`).
13
14
  *
14
15
  * @module
15
16
  */
@@ -17,10 +18,12 @@
17
18
  import { search as sharedSearch, type UpsertResult } from "@dbx-tools/shared-search";
18
19
  import { createTool } from "@mastra/core/tools";
19
20
  import { z } from "zod";
21
+ import { toSearchOptions, toUniversalSearchOptions } from "./_search-options.ts";
22
+ import type { SearchReadBackend } from "./client.ts";
23
+ import type { SearchPluginConfig } from "./config.ts";
20
24
  import { toCreateIndexOptions } from "./index-tools.ts";
21
25
  import { toDocumentArray } from "./query.ts";
22
- import { getSearchRuntime } from "./runtime.ts";
23
- import { toSearchOptions, toUniversalSearchOptions } from "./_search-options.ts";
26
+ import { createSearchRuntime, type SearchRuntime } from "./runtime.ts";
24
27
  import {
25
28
  ADD_DOCUMENTS_TOOL_DESCRIPTION,
26
29
  CREATE_INDEX_TOOL_DESCRIPTION,
@@ -35,10 +38,30 @@ import {
35
38
  universalSearchToolSchema,
36
39
  } from "./schema.ts";
37
40
 
38
- /** Common option accepted by every tool factory: override the tool id. */
41
+ /** Options accepted by every standalone tool factory. */
39
42
  export interface SearchToolOptions {
40
43
  /** Override the tool id (defaults per tool). */
41
44
  id?: string;
45
+ /** Isolated runtime used by this standalone tool. Mutually exclusive with config/backend. */
46
+ runtime?: SearchRuntime;
47
+ /** Config used to create an isolated runtime. */
48
+ config?: SearchPluginConfig;
49
+ /** Provider used when creating a runtime from config. */
50
+ readBackend?: SearchReadBackend;
51
+ }
52
+
53
+ /** Resolve the isolated runtime captured by one standalone tool factory. */
54
+ function toolRuntime(options: SearchToolOptions): SearchRuntime {
55
+ if (options.runtime && (options.config || options.readBackend)) {
56
+ throw new TypeError("search tool options accept runtime or config/readBackend, not both");
57
+ }
58
+ return (
59
+ options.runtime ??
60
+ createSearchRuntime({
61
+ config: options.config,
62
+ readBackend: options.readBackend,
63
+ })
64
+ );
42
65
  }
43
66
 
44
67
  /**
@@ -57,6 +80,7 @@ export interface SearchToolOptions {
57
80
  * ```
58
81
  */
59
82
  export function searchTool(options: SearchToolOptions = {}) {
83
+ const runtime = toolRuntime(options);
60
84
  return createTool({
61
85
  id: options.id ?? "search",
62
86
  description: SEARCH_TOOL_DESCRIPTION,
@@ -64,14 +88,14 @@ export function searchTool(options: SearchToolOptions = {}) {
64
88
  outputSchema: searchResultSchema,
65
89
  execute: async (input, context) => {
66
90
  const request = searchToolSchema.parse(input);
67
- const { client } = getSearchRuntime();
68
- return client.search(request.query, toSearchOptions(request, context?.abortSignal));
91
+ return runtime.client.search(request.query, toSearchOptions(request, context?.abortSignal));
69
92
  },
70
93
  });
71
94
  }
72
95
 
73
96
  /** Build the `universal_search` tool (federated search across every index). */
74
97
  export function universalSearchTool(options: SearchToolOptions = {}) {
98
+ const runtime = toolRuntime(options);
75
99
  return createTool({
76
100
  id: options.id ?? "universal_search",
77
101
  description: UNIVERSAL_SEARCH_TOOL_DESCRIPTION,
@@ -79,8 +103,7 @@ export function universalSearchTool(options: SearchToolOptions = {}) {
79
103
  outputSchema: searchResultSchema,
80
104
  execute: async (input, context) => {
81
105
  const request = universalSearchToolSchema.parse(input);
82
- const { client } = getSearchRuntime();
83
- return client.universalSearch(
106
+ return runtime.client.universalSearch(
84
107
  request.query,
85
108
  toUniversalSearchOptions(request, context?.abortSignal),
86
109
  );
@@ -93,6 +116,7 @@ export function universalSearchTool(options: SearchToolOptions = {}) {
93
116
  * Only install it when the plugin's write surface is enabled.
94
117
  */
95
118
  export function addDocumentsTool(options: SearchToolOptions = {}) {
119
+ const runtime = toolRuntime(options);
96
120
  const inputSchema = sharedSearch.searchDocumentSchema
97
121
  .array()
98
122
  .describe("Documents to add or update. Each must include the index primary key.");
@@ -104,7 +128,7 @@ export function addDocumentsTool(options: SearchToolOptions = {}) {
104
128
  .extend({ documents: inputSchema }),
105
129
  outputSchema: sharedSearch.upsertResultSchema,
106
130
  execute: async (input, context): Promise<UpsertResult> => {
107
- const { client, config } = getSearchRuntime();
131
+ const { client, config } = runtime;
108
132
  const record = input as { index?: string; documents: unknown };
109
133
  const documents = toDocumentArray(record.documents);
110
134
  const index = record.index ?? config.defaultIndex ?? "";
@@ -120,6 +144,7 @@ export function addDocumentsTool(options: SearchToolOptions = {}) {
120
144
  * key, and columns from the request + plugin config.
121
145
  */
122
146
  export function createIndexTool(options: SearchToolOptions = {}) {
147
+ const runtime = toolRuntime(options);
123
148
  return createTool({
124
149
  id: options.id ?? "create_index",
125
150
  description: CREATE_INDEX_TOOL_DESCRIPTION,
@@ -127,8 +152,10 @@ export function createIndexTool(options: SearchToolOptions = {}) {
127
152
  outputSchema: indexInfoSchema,
128
153
  execute: async (input, context) => {
129
154
  const request = createIndexToolSchema.parse(input);
130
- const { client } = getSearchRuntime();
131
- return client.createIndex(request.name, toCreateIndexOptions(request, context?.abortSignal));
155
+ return runtime.client.createIndex(
156
+ request.name,
157
+ toCreateIndexOptions(request, context?.abortSignal),
158
+ );
132
159
  },
133
160
  });
134
161
  }
@@ -144,6 +171,7 @@ const syncIndexResultSchema = z.object({
144
171
  * source table). Only install it when the plugin's write surface is enabled.
145
172
  */
146
173
  export function syncIndexTool(options: SearchToolOptions = {}) {
174
+ const runtime = toolRuntime(options);
147
175
  return createTool({
148
176
  id: options.id ?? "sync_index",
149
177
  description: SYNC_INDEX_TOOL_DESCRIPTION,
@@ -151,7 +179,7 @@ export function syncIndexTool(options: SearchToolOptions = {}) {
151
179
  outputSchema: syncIndexResultSchema,
152
180
  execute: async (input, context) => {
153
181
  const request = syncIndexToolSchema.parse(input);
154
- const { client, config } = getSearchRuntime();
182
+ const { client, config } = runtime;
155
183
  const index = request.index ?? config.defaultIndex ?? "";
156
184
  await client.syncIndex(index, context?.abortSignal);
157
185
  return { index, synced: true };