@specstream/ai-sdk 0.2.2 → 0.4.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 +79 -2
- package/dist/client.d.ts +150 -2
- package/dist/client.js +254 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/tools.d.ts +2339 -15
- package/dist/tools.js +162 -22
- package/package.json +4 -2
package/dist/tools.js
CHANGED
|
@@ -21,11 +21,29 @@ const getListResultsInput = z.object({
|
|
|
21
21
|
const webSearchInput = z.object({
|
|
22
22
|
q: z.string().min(1).max(2000).describe("The search question or request in everyday words (1 to 2,000 characters). Quoted phrases are searched as phrases."),
|
|
23
23
|
passages: z.boolean().optional().describe("Set true to include the main text passages of the top matching pages in the response."),
|
|
24
|
+
speed: z
|
|
25
|
+
.enum(["fast", "default"])
|
|
26
|
+
.optional()
|
|
27
|
+
.describe('"fast" returns the first ranked results in about a second for $1 per 1,000 searches; "default" adds the answer read from the pages, for $5 per 1,000. Left out, the default search runs.'),
|
|
24
28
|
});
|
|
25
29
|
const extractPagesInput = z.object({
|
|
26
30
|
urls: z.array(z.string().min(1)).min(1).max(20).describe("1 to 20 http or https URLs (web pages, PDF, Word, Excel) to read live."),
|
|
27
31
|
format: z.enum(["markdown", "text"]).optional().describe('Output format: "markdown" (default) or "text".'),
|
|
28
32
|
});
|
|
33
|
+
const monitorListInput = z.object({
|
|
34
|
+
listId: z.string().min(1).describe('The list_id of the list to keep current, like "fnd_gk5y3d0r97bvyz0t08zzh75kr6".'),
|
|
35
|
+
every: z
|
|
36
|
+
.enum(["hour", "day", "week", "month"])
|
|
37
|
+
.default("day")
|
|
38
|
+
.describe('How often to check the list again: "hour", "day" (default), "week" or "month".'),
|
|
39
|
+
});
|
|
40
|
+
const MONITOR_LIST_DESCRIPTION = [
|
|
41
|
+
"Keep a SpecStream list current: check its rows again on the live web every hour, day, week or month.",
|
|
42
|
+
"Use it only when the user asks to watch or monitor a list. Each check is billed, within a maximum per check and per month set in code.",
|
|
43
|
+
'Returns JSON with status "saved", monitor_id, state and next_run_at.',
|
|
44
|
+
'status "over_maximum" means nothing was saved because one check costs more than the maximum: tell the user.',
|
|
45
|
+
].join(" ");
|
|
46
|
+
const LIST_MONITORS_DESCRIPTION = "List the account's SpecStream monitors: the list each one checks, its state, how often, the next check and the last list it made. Reading is free.";
|
|
29
47
|
const FIND_LIST_DESCRIPTION = [
|
|
30
48
|
"Find a list of real things on the live web and return every matching row, each with the page it was read from.",
|
|
31
49
|
"Use it when the answer is many things that meet conditions: companies in a city, local businesses with a service, funding filings, open grants, tenders, clinical trials, repositories, people in a role.",
|
|
@@ -40,33 +58,14 @@ const GET_LIST_RESULTS_DESCRIPTION = [
|
|
|
40
58
|
"Read every row found so far for a SpecStream list, such as one that was still running. Reading is free.",
|
|
41
59
|
"Returns JSON with status, list_id, list_link and rows, each with its columns, source_url and checked_at.",
|
|
42
60
|
].join(" ");
|
|
43
|
-
|
|
44
|
-
* The two SpecStream tools for an AI SDK agent: find a list on the live web, and read a list's rows.
|
|
45
|
-
*
|
|
46
|
-
* The price cap is set here, in code, and is not a tool input, so the model can't raise it.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* const { text } = await generateText({
|
|
50
|
-
* model,
|
|
51
|
-
* tools: specstreamTools({ maxPriceUsd: 5 }),
|
|
52
|
-
* stopWhen: stepCountIs(5),
|
|
53
|
-
* prompt: "Find dental clinics in Lisbon with online booking.",
|
|
54
|
-
* });
|
|
55
|
-
*/
|
|
56
|
-
export function specstreamTools(options) {
|
|
61
|
+
function baseTools(client, options) {
|
|
57
62
|
const { maxPriceUsd } = options;
|
|
58
|
-
if (typeof maxPriceUsd !== "number" || !Number.isFinite(maxPriceUsd) || maxPriceUsd <= 0) {
|
|
59
|
-
throw new Error("maxPriceUsd must be greater than 0.");
|
|
60
|
-
}
|
|
61
|
-
const client = new SpecStreamClient({ apiKey: options.apiKey, baseUrl: options.baseUrl ?? DEFAULT_BASE_URL, fetch: options.fetch });
|
|
62
|
-
if (!client.hasKey)
|
|
63
|
-
throw new Error(MISSING_KEY_MESSAGE);
|
|
64
63
|
return {
|
|
65
64
|
specstream_web_search: tool({
|
|
66
|
-
description: "Search the live web in seconds with the SpecStream Search API (GET /v1/web-search, $5 per 1,000 searches, no quote needed). " +
|
|
65
|
+
description: "Search the live web in seconds with the SpecStream Search API (GET /v1/web-search, $5 per 1,000 searches of up to 10 results, no quote needed; speed 'fast' costs $1 per 1,000 and returns ranked results in about a second). " +
|
|
67
66
|
"Returns JSON with kind ('fact' or 'list'), answer (value, page and seen_at), matching rows, ranked sources and optional passages.",
|
|
68
67
|
inputSchema: webSearchInput,
|
|
69
|
-
execute: async ({ q, passages }, { abortSignal }) => client.webSearch(q, { passages, abortSignal }),
|
|
68
|
+
execute: async ({ q, passages, speed }, { abortSignal }) => client.webSearch(q, { passages, speed, abortSignal }),
|
|
70
69
|
}),
|
|
71
70
|
specstream_extract_pages: tool({
|
|
72
71
|
description: "Read the main text, title and published date of 1 to 20 web pages, PDFs, Word or Excel files live with the SpecStream Extract API " +
|
|
@@ -93,3 +92,144 @@ export function specstreamTools(options) {
|
|
|
93
92
|
}),
|
|
94
93
|
};
|
|
95
94
|
}
|
|
95
|
+
function monitorTools(client, maxPricePerRunUsd, maxPricePerMonthUsd) {
|
|
96
|
+
return {
|
|
97
|
+
specstream_monitor_list: tool({
|
|
98
|
+
description: MONITOR_LIST_DESCRIPTION,
|
|
99
|
+
inputSchema: monitorListInput,
|
|
100
|
+
execute: async ({ listId, every }, { abortSignal }) => client.monitorList(listId, { every: (every ?? "day"), maxPricePerRunUsd, maxPricePerMonthUsd, abortSignal }),
|
|
101
|
+
}),
|
|
102
|
+
specstream_list_monitors: tool({
|
|
103
|
+
description: LIST_MONITORS_DESCRIPTION,
|
|
104
|
+
inputSchema: z.object({}),
|
|
105
|
+
execute: async (_input, { abortSignal }) => client.monitors(abortSignal),
|
|
106
|
+
}),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export function specstreamTools(options) {
|
|
110
|
+
const { maxPriceUsd } = options;
|
|
111
|
+
if (typeof maxPriceUsd !== "number" || !Number.isFinite(maxPriceUsd) || maxPriceUsd <= 0) {
|
|
112
|
+
throw new Error("maxPriceUsd must be greater than 0.");
|
|
113
|
+
}
|
|
114
|
+
const monitorMax = options.monitorMaxPricePerRunUsd;
|
|
115
|
+
if (monitorMax !== undefined && !(typeof monitorMax === "number" && Number.isFinite(monitorMax) && monitorMax > 0)) {
|
|
116
|
+
throw new Error("monitorMaxPricePerRunUsd must be greater than 0.");
|
|
117
|
+
}
|
|
118
|
+
const client = new SpecStreamClient({ apiKey: options.apiKey, baseUrl: options.baseUrl ?? DEFAULT_BASE_URL, fetch: options.fetch });
|
|
119
|
+
if (!client.hasKey)
|
|
120
|
+
throw new Error(MISSING_KEY_MESSAGE);
|
|
121
|
+
const tools = baseTools(client, options);
|
|
122
|
+
if (monitorMax === undefined)
|
|
123
|
+
return tools;
|
|
124
|
+
return { ...tools, ...monitorTools(client, monitorMax, options.monitorMaxPricePerMonthUsd) };
|
|
125
|
+
}
|
|
126
|
+
const sessionId = z.string().min(1).describe("The session_id returned by specstream_open_browser.");
|
|
127
|
+
const TARGET = "The element's reference from the last specstream_browser_snapshot (such as e12), or a unique selector.";
|
|
128
|
+
const CLOSE_HINT = "Call specstream_close_browser with this session_id when the work is done.";
|
|
129
|
+
const OPEN_BROWSER_DESCRIPTION = [
|
|
130
|
+
"Open a real browser in the cloud and get its session_id.",
|
|
131
|
+
"Use it when a page needs clicks, typed text, a sign-in the user gave you, or JavaScript before its content shows;",
|
|
132
|
+
"for the plain text of a known page, specstream_extract_pages is cheaper.",
|
|
133
|
+
"The browser costs money while it is open ($0.10 an hour by the second, and $0.001 a step),",
|
|
134
|
+
"so call specstream_close_browser with the session_id as soon as the work is done.",
|
|
135
|
+
].join(" ");
|
|
136
|
+
/**
|
|
137
|
+
* Tools that open a real browser at SpecStream and drive it step by step: open, go to a page, read it,
|
|
138
|
+
* list its elements, click, type, any other Playwright MCP step, and close.
|
|
139
|
+
*
|
|
140
|
+
* How long a browser may run, and so its highest price, is set here, in code, and is not a tool input.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* const { text } = await generateText({
|
|
144
|
+
* model,
|
|
145
|
+
* tools: specstreamBrowserTools({ timeoutSeconds: 300, allowedDomains: ["example.com"] }),
|
|
146
|
+
* stopWhen: stepCountIs(12),
|
|
147
|
+
* prompt: "Open example.com, accept the cookie banner and read the pricing table.",
|
|
148
|
+
* });
|
|
149
|
+
*/
|
|
150
|
+
export function specstreamBrowserTools(options = {}) {
|
|
151
|
+
const client = new SpecStreamClient({ apiKey: options.apiKey, baseUrl: options.baseUrl ?? DEFAULT_BASE_URL, fetch: options.fetch });
|
|
152
|
+
if (!client.hasKey)
|
|
153
|
+
throw new Error(MISSING_KEY_MESSAGE);
|
|
154
|
+
const timeout = options.timeoutSeconds ?? 300;
|
|
155
|
+
if (!Number.isInteger(timeout) || timeout < 60 || timeout > 3600)
|
|
156
|
+
throw new Error("timeoutSeconds must be a whole number from 60 to 3600.");
|
|
157
|
+
const idle = options.idleTimeoutSeconds ?? 120;
|
|
158
|
+
if (!Number.isInteger(idle) || idle < 10 || idle > 600)
|
|
159
|
+
throw new Error("idleTimeoutSeconds must be a whole number from 10 to 600.");
|
|
160
|
+
return {
|
|
161
|
+
specstream_open_browser: tool({
|
|
162
|
+
description: OPEN_BROWSER_DESCRIPTION,
|
|
163
|
+
inputSchema: z.object({ url: z.string().optional().describe("A page to open first, as an http or https address.") }),
|
|
164
|
+
execute: async ({ url }, { abortSignal }) => {
|
|
165
|
+
const session = await client.openBrowser({
|
|
166
|
+
timeoutSeconds: timeout,
|
|
167
|
+
idleTimeoutSeconds: idle,
|
|
168
|
+
allowedDomains: options.allowedDomains,
|
|
169
|
+
blockMedia: options.blockMedia,
|
|
170
|
+
headless: options.headless,
|
|
171
|
+
metadata: options.metadata,
|
|
172
|
+
abortSignal,
|
|
173
|
+
});
|
|
174
|
+
const first = (url ?? "").trim();
|
|
175
|
+
if (first && session.session_id) {
|
|
176
|
+
session.page = await client.browserStep(session.session_id, "browser_navigate", { url: first }, abortSignal);
|
|
177
|
+
}
|
|
178
|
+
session.message = CLOSE_HINT;
|
|
179
|
+
return session;
|
|
180
|
+
},
|
|
181
|
+
}),
|
|
182
|
+
specstream_browser_navigate: tool({
|
|
183
|
+
description: "Open an address in the session's browser. Returns the page's address and title after it loaded. One step, $0.001.",
|
|
184
|
+
inputSchema: z.object({ session_id: sessionId, url: z.string().min(1).describe("The http or https address to open.") }),
|
|
185
|
+
execute: async ({ session_id, url }, { abortSignal }) => client.browserStep(session_id, "browser_navigate", { url: url.trim() }, abortSignal),
|
|
186
|
+
}),
|
|
187
|
+
specstream_browser_read: tool({
|
|
188
|
+
description: "Read the text of the page the browser shows, as it stands after its JavaScript ran, in Markdown or plain text. One step, $0.001.",
|
|
189
|
+
inputSchema: z.object({ session_id: sessionId, format: z.enum(["markdown", "text"]).optional().describe('"markdown" (default) or "text".') }),
|
|
190
|
+
execute: async ({ session_id, format }, { abortSignal }) => client.browserStep(session_id, "browser_read", { format: format === "text" ? "text" : "markdown" }, abortSignal),
|
|
191
|
+
}),
|
|
192
|
+
specstream_browser_snapshot: tool({
|
|
193
|
+
description: "List the page's links, buttons and fields, each with a reference such as e12. Call it before a click or typed text, and again after the page changed. One step, $0.001.",
|
|
194
|
+
inputSchema: z.object({ session_id: sessionId }),
|
|
195
|
+
execute: async ({ session_id }, { abortSignal }) => client.browserStep(session_id, "browser_snapshot", {}, abortSignal),
|
|
196
|
+
}),
|
|
197
|
+
specstream_browser_click: tool({
|
|
198
|
+
description: "Click an element of the page by its reference from the last snapshot. One step, $0.001.",
|
|
199
|
+
inputSchema: z.object({
|
|
200
|
+
session_id: sessionId,
|
|
201
|
+
target: z.string().min(1).describe(TARGET),
|
|
202
|
+
element: z.string().optional().describe("What the element is, in a few words."),
|
|
203
|
+
}),
|
|
204
|
+
execute: async ({ session_id, target, element }, { abortSignal }) => client.browserStep(session_id, "browser_click", { target, element }, abortSignal),
|
|
205
|
+
}),
|
|
206
|
+
specstream_browser_type: tool({
|
|
207
|
+
description: "Type text into a field of the page by its reference from the last snapshot. One step, $0.001.",
|
|
208
|
+
inputSchema: z.object({
|
|
209
|
+
session_id: sessionId,
|
|
210
|
+
target: z.string().min(1).describe(TARGET),
|
|
211
|
+
text: z.string().describe("The text to type into the element."),
|
|
212
|
+
submit: z.boolean().optional().describe("Press Enter after the text."),
|
|
213
|
+
}),
|
|
214
|
+
execute: async ({ session_id, target, text, submit }, { abortSignal }) => client.browserStep(session_id, "browser_type", { target, text, submit: submit ? true : undefined }, abortSignal),
|
|
215
|
+
}),
|
|
216
|
+
specstream_browser_action: tool({
|
|
217
|
+
description: "Run any other step in the session's browser: browser_press_key, browser_select_option, browser_fill_form, browser_hover, " +
|
|
218
|
+
"browser_wait_for, browser_navigate_back, browser_tabs, browser_handle_dialog, browser_take_screenshot or another Playwright MCP tool name. One step, $0.001.",
|
|
219
|
+
inputSchema: z.object({
|
|
220
|
+
session_id: sessionId,
|
|
221
|
+
tool: z
|
|
222
|
+
.string()
|
|
223
|
+
.regex(/^browser_[a-z][a-z0-9_]{0,62}$/)
|
|
224
|
+
.describe("The step's name, such as browser_press_key."),
|
|
225
|
+
arguments: z.record(z.string(), z.unknown()).optional().describe("The step's arguments, as Playwright MCP names them."),
|
|
226
|
+
}),
|
|
227
|
+
execute: async ({ session_id, tool: step, arguments: args }, { abortSignal }) => client.browserStep(session_id, step, args ?? {}, abortSignal),
|
|
228
|
+
}),
|
|
229
|
+
specstream_close_browser: tool({
|
|
230
|
+
description: "Close the browser so its time stops being charged. Returns the seconds it ran, its steps and what the session cost. Free.",
|
|
231
|
+
inputSchema: z.object({ session_id: sessionId }),
|
|
232
|
+
execute: async ({ session_id }, { abortSignal }) => client.closeBrowser(session_id, abortSignal),
|
|
233
|
+
}),
|
|
234
|
+
};
|
|
235
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@specstream/ai-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The live web data engine for the Vercel AI SDK: Search API ($5 per 1,000 searches), Extract API ($0.80 per 1,000 pages) and Deep Search.",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "The live web data engine for the Vercel AI SDK: Search API ($5 per 1,000 searches of up to 10 results, or $1 on the fast tier), Extract API ($0.80 per 1,000 pages) and Deep Search.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -46,6 +46,8 @@
|
|
|
46
46
|
"agents",
|
|
47
47
|
"specstream",
|
|
48
48
|
"web-data",
|
|
49
|
+
"browser",
|
|
50
|
+
"browser-automation",
|
|
49
51
|
"live-web",
|
|
50
52
|
"lead-lists",
|
|
51
53
|
"enrichment",
|