@howells/fieldwork-sdk 0.12.0 → 0.13.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 +2 -1
- package/dist/{best-CO9liYDa.js → best-DiKYwLdC.js} +2 -2
- package/dist/brave.js +12 -12
- package/dist/browserbase.js +11 -11
- package/dist/{document-BSCwJgBM.js → document-x9fWsQ1W.js} +3 -3
- package/dist/exa.js +26 -23
- package/dist/firecrawl.js +42 -42
- package/dist/{http-CJcvTPnE.js → http-Bg0h3D-t.js} +44 -44
- package/dist/image.js +1 -1
- package/dist/{job-JLEpszjr.js → job-BsrOrrO1.js} +4 -4
- package/dist/kernel.js +13 -13
- package/dist/llamaparse.js +12 -12
- package/dist/parallel.d.ts +2 -2
- package/dist/parallel.js +48 -41
- package/dist/{path-DO2llIdp.js → path-tDCyAKK9.js} +1 -1
- package/dist/perplexity.js +9 -9
- package/dist/reducto.js +10 -10
- package/dist/{schema-CJXFAZ-x.js → schema-Bfd2E8u2.js} +2 -2
- package/dist/serpapi.js +11 -11
- package/dist/tavily.js +18 -18
- package/dist/tools.d.ts +37 -34
- package/dist/tools.js +156 -120
- package/dist/{util-LvME_uOj.js → util-BOqd71Om.js} +13 -5
- package/package.json +6 -6
package/dist/tools.d.ts
CHANGED
|
@@ -102,20 +102,39 @@ export declare const toolError: (cause: unknown) => {
|
|
|
102
102
|
name: string;
|
|
103
103
|
retryable: boolean;
|
|
104
104
|
};
|
|
105
|
-
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Default provider choice: one provider name (strict — never substitutes
|
|
107
|
+
* another if absent) or an ordered preference list fixed at catalog build time
|
|
108
|
+
* (first listed provider that serves the tool wins).
|
|
109
|
+
*/
|
|
110
|
+
type DefaultProvider = string | readonly string[];
|
|
111
|
+
/** The provider (or ordered preference list) each capability uses when a call names none. */
|
|
112
|
+
type ProviderDefaults = Partial<Record<Capability, DefaultProvider>>;
|
|
113
|
+
/** The provider (or ordered preference list) a tool uses when a call names none, by tool name. */
|
|
114
|
+
type ToolDefaults = Readonly<Record<string, DefaultProvider>>;
|
|
115
|
+
/** Settings for `toolCatalog`: `ToolsOptions` plus defaults and optional transport schema extensions. */
|
|
110
116
|
interface ToolCatalogOptions extends ToolsOptions {
|
|
111
|
-
/** Default provider
|
|
117
|
+
/** Default provider (or ordered preference list) per capability. */
|
|
112
118
|
defaults?: ProviderDefaults;
|
|
113
|
-
/** Default provider per tool name, overriding `defaults` for that tool only. */
|
|
119
|
+
/** Default provider (or ordered preference list) per tool name, overriding `defaults` for that tool only. */
|
|
114
120
|
toolDefaults?: ToolDefaults;
|
|
121
|
+
/**
|
|
122
|
+
* Extra transport-level properties (such as the gateway's `timeout`) to add
|
|
123
|
+
* to the advertised schema of each branch and the top-level object. Callers
|
|
124
|
+
* are responsible for stripping and validating them before calling `execute`.
|
|
125
|
+
*/
|
|
126
|
+
extraProperties?: JsonSchema;
|
|
127
|
+
/**
|
|
128
|
+
* `"multi"` (default): a single-provider tool with a default keeps its plain
|
|
129
|
+
* schema (`single: true`, no `provider` field). `"always"` (used by the HTTP
|
|
130
|
+
* gateway): every tool advertises the `provider`-switched `oneOf` schema and
|
|
131
|
+
* requires an explicit `defaults`/`toolDefaults` entry to have a default.
|
|
132
|
+
*/
|
|
133
|
+
providerSelection?: "multi" | "always";
|
|
115
134
|
}
|
|
116
135
|
/**
|
|
117
|
-
* One tool across one or more providers. When
|
|
118
|
-
*
|
|
136
|
+
* One tool across one or more providers. When `single` is true, `inputSchema`
|
|
137
|
+
* is that provider's plain schema without a `provider` field; otherwise it is a
|
|
119
138
|
* `oneOf` keyed by `provider`, keeping each provider's options in its own
|
|
120
139
|
* branch.
|
|
121
140
|
*/
|
|
@@ -132,40 +151,24 @@ interface CatalogTool {
|
|
|
132
151
|
outputSchema: JsonSchema;
|
|
133
152
|
/** The providers that serve this tool, in client order. */
|
|
134
153
|
providers: string[];
|
|
154
|
+
/** True when one provider serves the tool as its default and `inputSchema` omits `provider`. */
|
|
155
|
+
single: boolean;
|
|
135
156
|
}
|
|
136
157
|
/** A multi-provider tool list and the selector that resolves a tool by name and provider. */
|
|
137
158
|
interface ToolCatalog {
|
|
138
159
|
select: (name: string, provider?: string) => FieldworkTool;
|
|
139
160
|
tools: CatalogTool[];
|
|
140
161
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Retrieval-style guidance naming only the search providers present, or an
|
|
143
|
-
* empty string when fewer than two providers are offered.
|
|
144
|
-
*/
|
|
145
|
-
export declare const searchProviderGuidance: (providers: readonly string[]) => string;
|
|
146
|
-
/**
|
|
147
|
-
* One provider's schema branch for a multi-provider tool. `explicit` fixes and
|
|
148
|
-
* requires `provider`; `extraProperties` adds transport-level fields such as
|
|
149
|
-
* the gateway's `timeout`.
|
|
150
|
-
*/
|
|
151
|
-
export declare const providerBranchSchema: (tool: FieldworkTool, explicit: boolean, extraProperties?: JsonSchema) => JsonSchema;
|
|
152
|
-
/** Describes a tool across its providers, including search retrieval-style guidance when several serve `search_web`. */
|
|
153
|
-
export declare const describeMultiProviderTool: (first: FieldworkTool, providers: readonly string[], defaultProvider: string | undefined) => string;
|
|
154
|
-
/**
|
|
155
|
-
* Collects every client's tools grouped by tool name and provider. Throws when
|
|
156
|
-
* two clients serve the same tool through the same provider.
|
|
157
|
-
*/
|
|
158
|
-
export declare const collectToolVariants: (clients: readonly Fieldwork<AnyAdapter>[], options?: ToolsOptions) => Map<string, Map<string, FieldworkTool>>;
|
|
159
162
|
/**
|
|
160
163
|
* Builds one tool per capability name across several Fieldwork clients. Each
|
|
161
164
|
* provider keeps its own input schema branch and executor; no options are
|
|
162
|
-
* merged and no call falls back to another provider.
|
|
165
|
+
* merged and no call falls back to another provider after an error.
|
|
163
166
|
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
* providers serve a tool (such as `search_web` over Brave and Exa), the
|
|
167
|
-
* exposes a `provider` enum with per-provider `oneOf` branches and
|
|
168
|
-
* guidance in
|
|
167
|
+
* Under the default `providerSelection: "multi"`, a single-provider tool with
|
|
168
|
+
* a default keeps its plain schema (`single: true`, no `provider` field). When
|
|
169
|
+
* several providers serve a tool (such as `search_web` over Brave and Exa), the
|
|
170
|
+
* tool exposes a `provider` enum with per-provider `oneOf` branches and
|
|
171
|
+
* query-style guidance in `search_web`'s description.
|
|
169
172
|
*/
|
|
170
173
|
export declare const toolCatalog: (clients: readonly Fieldwork<AnyAdapter>[], options?: ToolCatalogOptions) => ToolCatalog;
|
|
171
|
-
export type { CatalogTool, FieldworkTool, ProviderDefaults, ToolAnnotations, ToolArguments, ToolCatalog, ToolCatalogOptions, ToolDefaults, ToolInput, ToolOutput, ToolsOptions };
|
|
174
|
+
export type { CatalogTool, DefaultProvider, FieldworkTool, ProviderDefaults, ToolAnnotations, ToolArguments, ToolCatalog, ToolCatalogOptions, ToolDefaults, ToolInput, ToolOutput, ToolsOptions };
|
package/dist/tools.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
import * as z from "zod/mini";
|
|
1
2
|
import { t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
-
import { _ as
|
|
3
|
-
import { n as bestImages, t as RENDERERS } from "./best-
|
|
3
|
+
import { _ as isList, a as compact, b as jsonValue, f as refuseTogether, g as isJsonObject, h as isFlag, v as isNumber, y as isText } from "./util-BOqd71Om.js";
|
|
4
|
+
import { n as bestImages, t as RENDERERS } from "./best-DiKYwLdC.js";
|
|
4
5
|
import { t as CAPABILITIES } from "./types-mq6d6YOK.js";
|
|
5
|
-
import { t as joinPath } from "./path-
|
|
6
|
-
import * as z$1 from "zod/mini";
|
|
6
|
+
import { t as joinPath } from "./path-tDCyAKK9.js";
|
|
7
7
|
// src/tools/budget.ts
|
|
8
8
|
const DEFAULT_MAX_CHARACTERS = 2e4;
|
|
9
9
|
/** Checks configured and per-call budgets against the same optional cap. */
|
|
10
10
|
const checkMaxCharacters = (value, limit = Number.MAX_SAFE_INTEGER) => {
|
|
11
|
-
const parsed = z
|
|
11
|
+
const parsed = z.safeParse(z.number().check(z.int(), z.minimum(1), z.maximum(limit)), value);
|
|
12
12
|
if (!parsed.success) throw new FieldworkError("InvalidRequest", `maxCharacters must be an integer from 1 to ${limit}`);
|
|
13
13
|
return parsed.data;
|
|
14
14
|
};
|
|
@@ -1137,16 +1137,16 @@ const MONITOR_SPECS = [
|
|
|
1137
1137
|
// src/tools/images.ts
|
|
1138
1138
|
/** Providers whose `read` returns a page's HTML, with `format: "raw"`. */
|
|
1139
1139
|
const HTML_READERS = ["browserbase", "kernel"];
|
|
1140
|
-
const Arguments = z
|
|
1141
|
-
limit: z
|
|
1142
|
-
render: z
|
|
1140
|
+
const Arguments = z.tuple([z.string(), z.object({
|
|
1141
|
+
limit: z.optional(z.number()),
|
|
1142
|
+
render: z.optional(z.enum([
|
|
1143
1143
|
"auto",
|
|
1144
1144
|
"never",
|
|
1145
1145
|
"always"
|
|
1146
1146
|
])),
|
|
1147
|
-
retries: z
|
|
1148
|
-
signal: z
|
|
1149
|
-
timeout: z
|
|
1147
|
+
retries: z.optional(z.number()),
|
|
1148
|
+
signal: z.optional(z.instanceof(AbortSignal)),
|
|
1149
|
+
timeout: z.optional(z.number())
|
|
1150
1150
|
})]);
|
|
1151
1151
|
const isOneOf = (names, name) => names.some((candidate) => candidate === name);
|
|
1152
1152
|
const isImageRouter = (web) => isOneOf(HTML_READERS, web.providerFor("read")) && isOneOf(RENDERERS, web.providerFor("act"));
|
|
@@ -1160,9 +1160,9 @@ const findImages = (web) => {
|
|
|
1160
1160
|
if (!isImageRouter(web)) return;
|
|
1161
1161
|
const router = web;
|
|
1162
1162
|
return async (...args) => {
|
|
1163
|
-
const [url, options] = z
|
|
1163
|
+
const [url, options] = z.parse(Arguments, args);
|
|
1164
1164
|
const { raw: _raw, renderError, ...found } = await bestImages(router, url, options);
|
|
1165
|
-
return z
|
|
1165
|
+
return z.parse(jsonValue, {
|
|
1166
1166
|
...found,
|
|
1167
1167
|
...renderError && { renderError: {
|
|
1168
1168
|
code: renderError.code,
|
|
@@ -1561,67 +1561,101 @@ const VERBATIM = /* @__PURE__ */ new Set([
|
|
|
1561
1561
|
"values"
|
|
1562
1562
|
]);
|
|
1563
1563
|
/**
|
|
1564
|
-
*
|
|
1565
|
-
*
|
|
1566
|
-
*
|
|
1564
|
+
* Priority tier for spending the shared budget across the whole result tree.
|
|
1565
|
+
* Answers and focused passages (excerpts, snippets, summaries) are budgeted
|
|
1566
|
+
* across every result before full page bodies (markdown, content, text) and
|
|
1567
|
+
* alternate renderings (html, rawHtml).
|
|
1567
1568
|
*/
|
|
1568
|
-
const
|
|
1569
|
-
"answer",
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1569
|
+
const TIERS = [
|
|
1570
|
+
/* @__PURE__ */ new Set(["answer", "output"]),
|
|
1571
|
+
/* @__PURE__ */ new Set([
|
|
1572
|
+
"excerpts",
|
|
1573
|
+
"snippet",
|
|
1574
|
+
"summary"
|
|
1575
|
+
]),
|
|
1576
|
+
/* @__PURE__ */ new Set([
|
|
1577
|
+
"markdown",
|
|
1578
|
+
"content",
|
|
1579
|
+
"text"
|
|
1580
|
+
])
|
|
1577
1581
|
];
|
|
1578
|
-
const
|
|
1579
|
-
const
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
+
const LAST_TIER_KEYS = /* @__PURE__ */ new Set(["html", "rawHtml"]);
|
|
1583
|
+
const OTHER_TIER = TIERS.length;
|
|
1584
|
+
const LAST_TIER = TIERS.length + 1;
|
|
1585
|
+
const tierOf = (key) => {
|
|
1586
|
+
if (key === void 0) return OTHER_TIER;
|
|
1587
|
+
const index = TIERS.findIndex((keys) => keys.has(key));
|
|
1588
|
+
if (index !== -1) return index;
|
|
1589
|
+
return LAST_TIER_KEYS.has(key) ? LAST_TIER : OTHER_TIER;
|
|
1582
1590
|
};
|
|
1583
|
-
/**
|
|
1584
|
-
const
|
|
1585
|
-
if (value
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1591
|
+
/** Pass 1: collects every long string slot in tree order, skipping non-verbatim `raw` keys. */
|
|
1592
|
+
const collect = (value, verbatim, key, slots) => {
|
|
1593
|
+
if (isText(value)) {
|
|
1594
|
+
if (value.length > SHORT_TEXT) slots.push({
|
|
1595
|
+
kept: value.length,
|
|
1596
|
+
length: value.length,
|
|
1597
|
+
tier: tierOf(key)
|
|
1598
|
+
});
|
|
1599
|
+
return;
|
|
1600
|
+
}
|
|
1601
|
+
if (isList(value)) {
|
|
1602
|
+
for (const item of value) collect(item, verbatim, key, slots);
|
|
1603
|
+
return;
|
|
1604
|
+
}
|
|
1605
|
+
if (!isJsonObject(value)) return;
|
|
1606
|
+
for (const [entryKey, item] of Object.entries(value)) {
|
|
1607
|
+
if (!verbatim && entryKey === "raw") continue;
|
|
1608
|
+
collect(item, verbatim || VERBATIM.has(entryKey), entryKey, slots);
|
|
1609
|
+
}
|
|
1595
1610
|
};
|
|
1596
|
-
/**
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1611
|
+
/** Allocates `budget` across `slots` in priority-tier order, preserving tree order within each tier. */
|
|
1612
|
+
const allocate = (slots, budget) => {
|
|
1613
|
+
let remaining = budget;
|
|
1614
|
+
for (const slot of slots.toSorted((a, b) => a.tier - b.tier)) {
|
|
1615
|
+
const kept = Math.min(slot.length, remaining);
|
|
1616
|
+
slot.kept = kept;
|
|
1617
|
+
remaining -= kept;
|
|
1618
|
+
}
|
|
1619
|
+
};
|
|
1620
|
+
/** Pass 2: rebuilds the value without `raw`, applying each slot's allocated length. */
|
|
1621
|
+
const render = (value, verbatim, state, path) => {
|
|
1622
|
+
if (isText(value)) {
|
|
1623
|
+
if (value.length <= SHORT_TEXT) return value;
|
|
1624
|
+
const slot = state.slots[state.index];
|
|
1625
|
+
state.index += 1;
|
|
1626
|
+
const kept = slot?.kept ?? value.length;
|
|
1627
|
+
if (kept === value.length) return value;
|
|
1628
|
+
state.truncated.push({
|
|
1629
|
+
length: value.length,
|
|
1630
|
+
path: joinPath(path),
|
|
1631
|
+
shown: kept
|
|
1632
|
+
});
|
|
1633
|
+
return `${value.slice(0, kept)}\n\n[cut: ${kept} of ${value.length} characters shown]`;
|
|
1634
|
+
}
|
|
1635
|
+
if (isList(value)) return value.map((item, index) => render(item, verbatim, state, [...path, index]) ?? null);
|
|
1605
1636
|
if (!isJsonObject(value)) return value;
|
|
1606
1637
|
const entries = Object.entries(value).filter(([key]) => verbatim || key !== "raw");
|
|
1607
|
-
|
|
1608
|
-
for (const [key, item] of entries.toSorted(([a], [b]) => rank(a) - rank(b))) shaped.set(key, shape(item, verbatim || VERBATIM.has(key), budget, [...path, key]));
|
|
1609
|
-
return Object.fromEntries(entries.map(([key]) => [key, shaped.get(key)]));
|
|
1638
|
+
return Object.fromEntries(entries.map(([key, item]) => [key, render(item, verbatim || VERBATIM.has(key), state, [...path, key])]));
|
|
1610
1639
|
};
|
|
1611
1640
|
/**
|
|
1612
1641
|
* A result as a model should see it: the provider's `raw` response dropped at
|
|
1613
1642
|
* every depth except inside extracted values, and long text cut to one budget
|
|
1614
|
-
* shared across the result.
|
|
1615
|
-
*
|
|
1643
|
+
* shared across the result. Answers and focused passages (excerpts, snippets,
|
|
1644
|
+
* summaries) spend the budget across every item before full page bodies and
|
|
1645
|
+
* alternate renderings such as HTML.
|
|
1616
1646
|
*/
|
|
1617
1647
|
const forModel = (result, budget) => {
|
|
1618
|
-
const
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1648
|
+
const slots = [];
|
|
1649
|
+
collect(result, false, void 0, slots);
|
|
1650
|
+
allocate(slots, budget);
|
|
1651
|
+
const state = {
|
|
1652
|
+
index: 0,
|
|
1653
|
+
slots,
|
|
1654
|
+
truncated: []
|
|
1655
|
+
};
|
|
1656
|
+
const shaped = render(result, false, state, []);
|
|
1623
1657
|
const output = isJsonObject(shaped) ? shaped : {};
|
|
1624
|
-
if (truncated.length > 0) output.truncated = truncated;
|
|
1658
|
+
if (state.truncated.length > 0) output.truncated = state.truncated;
|
|
1625
1659
|
return output;
|
|
1626
1660
|
};
|
|
1627
1661
|
// src/tools/validate.ts
|
|
@@ -1844,10 +1878,6 @@ const SEARCH_PROVIDER_ROLES = /* @__PURE__ */ new Map([
|
|
|
1844
1878
|
["serpapi", "serpapi for Google/Bing SERPs and exact keyword queries"],
|
|
1845
1879
|
["tavily", "tavily for hybrid search with chunked passages"]
|
|
1846
1880
|
]);
|
|
1847
|
-
/**
|
|
1848
|
-
* Retrieval-style guidance naming only the search providers present, or an
|
|
1849
|
-
* empty string when fewer than two providers are offered.
|
|
1850
|
-
*/
|
|
1851
1881
|
const searchProviderGuidance = (providers) => {
|
|
1852
1882
|
if (providers.length < 2) return "";
|
|
1853
1883
|
const notes = providers.flatMap((provider) => {
|
|
@@ -1856,18 +1886,13 @@ const searchProviderGuidance = (providers) => {
|
|
|
1856
1886
|
});
|
|
1857
1887
|
return notes.length === 0 ? "" : ` Choose provider by query style: ${notes.join("; ")}.`;
|
|
1858
1888
|
};
|
|
1859
|
-
const InputSchema = z
|
|
1860
|
-
oneOf: z
|
|
1861
|
-
properties: z
|
|
1862
|
-
required: z
|
|
1889
|
+
const InputSchema = z.object({
|
|
1890
|
+
oneOf: z.optional(z.array(z.json())),
|
|
1891
|
+
properties: z.record(z.string(), z.json()),
|
|
1892
|
+
required: z.optional(z.array(z.string()))
|
|
1863
1893
|
});
|
|
1864
|
-
/**
|
|
1865
|
-
* One provider's schema branch for a multi-provider tool. `explicit` fixes and
|
|
1866
|
-
* requires `provider`; `extraProperties` adds transport-level fields such as
|
|
1867
|
-
* the gateway's `timeout`.
|
|
1868
|
-
*/
|
|
1869
1894
|
const providerBranchSchema = (tool, explicit, extraProperties = {}) => {
|
|
1870
|
-
const schema = z
|
|
1895
|
+
const schema = z.parse(InputSchema, tool.inputSchema);
|
|
1871
1896
|
const properties = {
|
|
1872
1897
|
...schema.properties,
|
|
1873
1898
|
...extraProperties
|
|
@@ -1885,51 +1910,80 @@ const providerBranchSchema = (tool, explicit, extraProperties = {}) => {
|
|
|
1885
1910
|
if (schema.oneOf !== void 0) input.oneOf = schema.oneOf;
|
|
1886
1911
|
return input;
|
|
1887
1912
|
};
|
|
1888
|
-
const resolveDefaultTool = (variants, first, configured) => {
|
|
1889
|
-
if (configured === void 0)
|
|
1890
|
-
|
|
1913
|
+
const resolveDefaultTool = (variants, first, configured, alwaysSelect) => {
|
|
1914
|
+
if (configured === void 0) {
|
|
1915
|
+
if (alwaysSelect || variants.size !== 1) return;
|
|
1916
|
+
return first;
|
|
1917
|
+
}
|
|
1918
|
+
if (isText(configured)) return variants.get(configured);
|
|
1919
|
+
for (const candidate of configured) {
|
|
1920
|
+
const found = variants.get(candidate);
|
|
1921
|
+
if (found !== void 0) return found;
|
|
1922
|
+
}
|
|
1891
1923
|
};
|
|
1892
|
-
const multiProviderSchema = (variants, defaultTool, providers) => ({
|
|
1893
|
-
oneOf: [...defaultTool === void 0 ? [] : [providerBranchSchema(defaultTool, false)], ...variants.map((tool) => providerBranchSchema(tool, true))],
|
|
1894
|
-
properties: {
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1924
|
+
const multiProviderSchema = (variants, defaultTool, providers, extraProperties) => ({
|
|
1925
|
+
oneOf: [...defaultTool === void 0 ? [] : [providerBranchSchema(defaultTool, false, extraProperties)], ...variants.map((tool) => providerBranchSchema(tool, true, extraProperties))],
|
|
1926
|
+
properties: {
|
|
1927
|
+
provider: {
|
|
1928
|
+
enum: [...providers],
|
|
1929
|
+
type: "string"
|
|
1930
|
+
},
|
|
1931
|
+
...extraProperties
|
|
1932
|
+
},
|
|
1898
1933
|
type: "object"
|
|
1899
1934
|
});
|
|
1900
|
-
/** Describes a tool across its providers, including search retrieval-style guidance when several serve `search_web`. */
|
|
1901
1935
|
const describeMultiProviderTool = (first, providers, defaultProvider) => {
|
|
1902
1936
|
const guidance = first.name === "search_web" ? searchProviderGuidance(providers) : "";
|
|
1903
1937
|
const routing = defaultProvider === void 0 ? "Name a provider explicitly." : `Defaults to ${defaultProvider}.`;
|
|
1904
1938
|
return `${first.description}${guidance} ${routing} Set provider to choose another supported provider; Fieldwork never switches providers after an error.`;
|
|
1905
1939
|
};
|
|
1906
|
-
/**
|
|
1907
|
-
* Collects every client's tools grouped by tool name and provider. Throws when
|
|
1908
|
-
* two clients serve the same tool through the same provider.
|
|
1909
|
-
*/
|
|
1910
1940
|
const collectToolVariants = (clients, options = {}) => {
|
|
1911
1941
|
const entries = /* @__PURE__ */ new Map();
|
|
1912
1942
|
for (const client of clients) for (const tool of toolsFor(client, options)) {
|
|
1913
1943
|
const variants = entries.get(tool.name) ?? /* @__PURE__ */ new Map();
|
|
1914
|
-
if (variants.has(tool.provider)) throw new
|
|
1944
|
+
if (variants.has(tool.provider)) throw new FieldworkError("InvalidRequest", `Duplicate provider ${tool.provider} for ${tool.name}`, { provider: tool.provider });
|
|
1915
1945
|
variants.set(tool.provider, tool);
|
|
1916
1946
|
entries.set(tool.name, variants);
|
|
1917
1947
|
}
|
|
1918
1948
|
return entries;
|
|
1919
1949
|
};
|
|
1950
|
+
const catalogToolFor = (first, list, defaultTool, providers, single, extraProperties, select) => {
|
|
1951
|
+
const defaultProvider = defaultTool?.provider;
|
|
1952
|
+
const singleSchema = Object.keys(extraProperties).length > 0 ? providerBranchSchema(first, false, extraProperties) : first.inputSchema;
|
|
1953
|
+
return {
|
|
1954
|
+
annotations: first.annotations,
|
|
1955
|
+
capability: first.capability,
|
|
1956
|
+
...defaultProvider !== void 0 && { defaultProvider },
|
|
1957
|
+
description: single ? first.description : describeMultiProviderTool(first, providers, defaultProvider),
|
|
1958
|
+
execute: async (input, call = {}) => {
|
|
1959
|
+
if (!isJsonObject(input)) throw new FieldworkError("InvalidRequest", `${first.name}: input must be an object`);
|
|
1960
|
+
if (single && defaultTool !== void 0) return await defaultTool.execute(input, call);
|
|
1961
|
+
const { provider, ...args } = input;
|
|
1962
|
+
if (provider !== void 0 && !isText(provider)) throw new FieldworkError("InvalidRequest", `${first.name}: provider must be a string`);
|
|
1963
|
+
return await select(first.name, provider).execute(args, call);
|
|
1964
|
+
},
|
|
1965
|
+
inputSchema: single ? singleSchema : multiProviderSchema(list, defaultTool, providers, extraProperties),
|
|
1966
|
+
...first.method !== void 0 && { method: first.method },
|
|
1967
|
+
name: first.name,
|
|
1968
|
+
outputSchema: first.outputSchema,
|
|
1969
|
+
providers,
|
|
1970
|
+
single
|
|
1971
|
+
};
|
|
1972
|
+
};
|
|
1920
1973
|
/**
|
|
1921
1974
|
* Builds one tool per capability name across several Fieldwork clients. Each
|
|
1922
1975
|
* provider keeps its own input schema branch and executor; no options are
|
|
1923
|
-
* merged and no call falls back to another provider.
|
|
1976
|
+
* merged and no call falls back to another provider after an error.
|
|
1924
1977
|
*
|
|
1925
|
-
*
|
|
1926
|
-
*
|
|
1927
|
-
* providers serve a tool (such as `search_web` over Brave and Exa), the
|
|
1928
|
-
* exposes a `provider` enum with per-provider `oneOf` branches and
|
|
1929
|
-
* guidance in
|
|
1978
|
+
* Under the default `providerSelection: "multi"`, a single-provider tool with
|
|
1979
|
+
* a default keeps its plain schema (`single: true`, no `provider` field). When
|
|
1980
|
+
* several providers serve a tool (such as `search_web` over Brave and Exa), the
|
|
1981
|
+
* tool exposes a `provider` enum with per-provider `oneOf` branches and
|
|
1982
|
+
* query-style guidance in `search_web`'s description.
|
|
1930
1983
|
*/
|
|
1931
1984
|
const toolCatalog = (clients, options = {}) => {
|
|
1932
|
-
const { defaults, toolDefaults, ...toolOptions } = options;
|
|
1985
|
+
const { defaults, extraProperties = {}, providerSelection = "multi", toolDefaults, ...toolOptions } = options;
|
|
1986
|
+
const alwaysSelect = providerSelection === "always";
|
|
1933
1987
|
const entries = collectToolVariants(clients, toolOptions);
|
|
1934
1988
|
const defaultByTool = /* @__PURE__ */ new Map();
|
|
1935
1989
|
const providersByTool = /* @__PURE__ */ new Map();
|
|
@@ -1951,33 +2005,15 @@ const toolCatalog = (clients, options = {}) => {
|
|
|
1951
2005
|
if (first === void 0) continue;
|
|
1952
2006
|
const providers = list.map((tool) => tool.provider);
|
|
1953
2007
|
const configured = toolDefaults?.[first.name] ?? defaults?.[first.capability];
|
|
1954
|
-
const defaultTool = resolveDefaultTool(variants, first, configured);
|
|
1955
|
-
|
|
1956
|
-
defaultByTool.set(first.name, defaultProvider);
|
|
2008
|
+
const defaultTool = resolveDefaultTool(variants, first, configured, alwaysSelect);
|
|
2009
|
+
defaultByTool.set(first.name, defaultTool?.provider);
|
|
1957
2010
|
providersByTool.set(first.name, providers);
|
|
1958
|
-
const single = list.length === 1 && defaultTool !== void 0;
|
|
1959
|
-
tools.push(
|
|
1960
|
-
annotations: first.annotations,
|
|
1961
|
-
capability: first.capability,
|
|
1962
|
-
...defaultProvider !== void 0 && { defaultProvider },
|
|
1963
|
-
description: single ? first.description : describeMultiProviderTool(first, providers, defaultProvider),
|
|
1964
|
-
execute: async (input, call = {}) => {
|
|
1965
|
-
if (!isJsonObject(input)) throw new FieldworkError("InvalidRequest", `${first.name}: input must be an object`);
|
|
1966
|
-
if (single) return await defaultTool.execute(input, call);
|
|
1967
|
-
const { provider, ...args } = input;
|
|
1968
|
-
if (provider !== void 0 && !isText(provider)) throw new FieldworkError("InvalidRequest", `${first.name}: provider must be a string`);
|
|
1969
|
-
return await select(first.name, provider).execute(args, call);
|
|
1970
|
-
},
|
|
1971
|
-
inputSchema: single ? first.inputSchema : multiProviderSchema(list, defaultTool, providers),
|
|
1972
|
-
...first.method !== void 0 && { method: first.method },
|
|
1973
|
-
name: first.name,
|
|
1974
|
-
outputSchema: first.outputSchema,
|
|
1975
|
-
providers
|
|
1976
|
-
});
|
|
2011
|
+
const single = providerSelection === "multi" && list.length === 1 && defaultTool !== void 0;
|
|
2012
|
+
tools.push(catalogToolFor(first, list, defaultTool, providers, single, extraProperties, select));
|
|
1977
2013
|
}
|
|
1978
2014
|
return {
|
|
1979
2015
|
select,
|
|
1980
2016
|
tools
|
|
1981
2017
|
};
|
|
1982
2018
|
};
|
|
1983
|
-
export { DEFAULT_MAX_CHARACTERS, EXCLUDED_BY_DEFAULT, checkMaxCharacters,
|
|
2019
|
+
export { DEFAULT_MAX_CHARACTERS, EXCLUDED_BY_DEFAULT, checkMaxCharacters, toolCatalog, toolError, toolsFor };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
-
import * as z
|
|
2
|
+
import * as z from "zod/mini";
|
|
3
3
|
// src/json.ts
|
|
4
4
|
const isText = (value) => typeof value === "string";
|
|
5
5
|
/** A finite number: JSON has no NaN or Infinity. */
|
|
@@ -7,11 +7,11 @@ const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
|
7
7
|
const isFlag = (value) => typeof value === "boolean";
|
|
8
8
|
const isList = (value) => Array.isArray(value);
|
|
9
9
|
/** Checks a value is JSON all the way down, for a body or field that arrives untyped. */
|
|
10
|
-
const jsonValue = z
|
|
10
|
+
const jsonValue = z.json();
|
|
11
11
|
/** Parses JSON text. Throws `SyntaxError` when the text is not JSON. */
|
|
12
12
|
const parseJsonText = (text) => {
|
|
13
13
|
const parsed = JSON.parse(text);
|
|
14
|
-
return z
|
|
14
|
+
return z.parse(jsonValue, parsed);
|
|
15
15
|
};
|
|
16
16
|
/** Narrows anything, such as a value a transport hands over, to a JSON object. */
|
|
17
17
|
const isJsonObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -27,7 +27,7 @@ const compact = (value) => {
|
|
|
27
27
|
return result;
|
|
28
28
|
};
|
|
29
29
|
/** The longest `timeout` a timer holds, in milliseconds: about 24.8 days. */
|
|
30
|
-
const MAX_TIMEOUT =
|
|
30
|
+
const MAX_TIMEOUT = 0x7fffffff;
|
|
31
31
|
/**
|
|
32
32
|
* Refuses a `timeout` or `retries` that no timer or retry count can hold,
|
|
33
33
|
* before anything is sent.
|
|
@@ -128,6 +128,14 @@ const meta = (parts) => {
|
|
|
128
128
|
return Object.keys(result).length > 0 ? result : void 0;
|
|
129
129
|
};
|
|
130
130
|
/**
|
|
131
|
+
* Refuses an option value that is not a positive whole number (`>= 1`),
|
|
132
|
+
* throwing `InvalidRequest` before anything is sent. Undefined passes through.
|
|
133
|
+
*/
|
|
134
|
+
const positiveWhole = (provider, name, value) => {
|
|
135
|
+
if (value !== void 0 && (!Number.isInteger(value) || value < 1)) throw new FieldworkError("InvalidRequest", `${provider}: ${name} must be a whole number of at least 1 (got ${value})`, { provider });
|
|
136
|
+
return value;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
131
139
|
* Refuses a request that sets more than one of `names` (a name counts as set
|
|
132
140
|
* unless its value is undefined), for options the provider rejects or ignores
|
|
133
141
|
* when combined. Throws before anything is sent.
|
|
@@ -169,4 +177,4 @@ const presentPages = (pages) => ({
|
|
|
169
177
|
})),
|
|
170
178
|
pages: pages.filter((page) => page.statusCode === void 0 || !GONE.has(page.statusCode))
|
|
171
179
|
});
|
|
172
|
-
export {
|
|
180
|
+
export { isList as _, compact as a, jsonValue as b, parseJson as c, presentPages as d, refuseTogether as f, isJsonObject as g, isFlag as h, citationsOf as i, positiveWhole as l, uniqueCitations as m, checkCallSettings as n, hours as o, requireJobId as p, citationOf as r, meta as s, callSettings as t, presentPage as u, isNumber as v, parseJsonText as x, isText as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howells/fieldwork-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "One API for web search, reverse image search, page reading, extraction, crawling, answers, research, browser sessions, monitoring and document parsing across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi, Browserbase, Kernel, LlamaParse and Reducto.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -106,13 +106,13 @@
|
|
|
106
106
|
"zod": "^4.6.7"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
|
-
"@howells/lint": "
|
|
110
|
-
"@howells/typescript-config": "^0.1.
|
|
111
|
-
"@types/node": "^26.6.
|
|
109
|
+
"@howells/lint": "3.9.1",
|
|
110
|
+
"@howells/typescript-config": "^0.1.9",
|
|
111
|
+
"@types/node": "^26.6.11",
|
|
112
112
|
"@types/turndown": "5.0.6",
|
|
113
|
-
"tsdown": "^0.23.
|
|
113
|
+
"tsdown": "^0.23.8",
|
|
114
114
|
"typescript": "7.0.2",
|
|
115
|
-
"vitest": "^5.
|
|
115
|
+
"vitest": "^5.1.2"
|
|
116
116
|
},
|
|
117
117
|
"engines": {
|
|
118
118
|
"node": ">=22.12.0"
|