|
1 | | -import { tool } from "@opencode-ai/plugin"; |
| 1 | +import { z } from "zod"; |
2 | 2 |
|
3 | 3 | const API = "https://api.firecrawl.dev/v2"; |
4 | 4 | const TIMEOUT_MS = 120_000; |
@@ -37,75 +37,80 @@ async function post<T extends ApiError>(path: string, body: unknown, apiKey: str |
37 | 37 | throw new Error(`Alexandria request failed with ${response.status} ${response.statusText}${detail}`); |
38 | 38 | } |
39 | 39 |
|
40 | | -export const alexandria = (apiKey: string) => |
41 | | - tool({ |
42 | | - description: `Find and run Firecrawl Alexandria data providers: official APIs, licensed publishers, and Firecrawl indexes that return structured records instead of a web page. |
| 40 | +const input = z.object({ |
| 41 | + query: z.string().optional().describe("What data you need, in plain words. Finds providers"), |
| 42 | + provider: z.string().optional().describe('Provider slug from discovery, e.g. "bls-gov"'), |
| 43 | + capability: z |
| 44 | + .string() |
| 45 | + .optional() |
| 46 | + .describe('Capability address from discovery, e.g. "economic-statistics/bls_unemployment_rate"'), |
| 47 | + options: z |
| 48 | + .record(z.string(), z.unknown()) |
| 49 | + .optional() |
| 50 | + .describe("Options matching the capability's contract"), |
| 51 | +}); |
| 52 | + |
| 53 | +/** Asks the user before a run spends credits. Only OpenCode 1 can prompt from a plugin tool. */ |
| 54 | +type Confirm = (target: string, metadata: Record<string, unknown>) => Promise<void>; |
| 55 | + |
| 56 | +export const alexandria = { |
| 57 | + name: "firecrawl_alexandria", |
| 58 | + description: `Find and run Firecrawl Alexandria data providers: official APIs, licensed publishers, and Firecrawl indexes that return structured records instead of a web page. |
43 | 59 |
|
44 | 60 | Two modes, exactly one per call: |
45 | 61 | - Discover: pass query, describing the data you need in plain words. Returns matching capabilities with their input contract (options), response fields, and credit price. Discovery is free. |
46 | 62 | - Run: pass provider and capability exactly as discovery returned them, plus options matching that capability's contract. Running spends Firecrawl credits at the listed price. |
47 | 63 |
|
48 | 64 | Discover first and only run a capability discovery returned. Check each result item for an error before using it, and report the credits the run cost. When no provider fits, fall back to web search or scraping.`, |
49 | | - args: { |
50 | | - query: tool.schema.string().optional().describe("What data you need, in plain words. Finds providers"), |
51 | | - provider: tool.schema.string().optional().describe('Provider slug from discovery, e.g. "bls-gov"'), |
52 | | - capability: tool.schema |
53 | | - .string() |
54 | | - .optional() |
55 | | - .describe('Capability address from discovery, e.g. "economic-statistics/bls_unemployment_rate"'), |
56 | | - options: tool.schema |
57 | | - .record(tool.schema.string(), tool.schema.unknown()) |
58 | | - .optional() |
59 | | - .describe("Options matching the capability's contract"), |
60 | | - }, |
61 | | - async execute(args, context) { |
62 | | - const query = args.query?.trim(); |
63 | | - const target = args.provider && args.capability ? `${args.provider}/${args.capability}` : undefined; |
64 | | - if (!query === !target) { |
65 | | - throw new Error("Pass either query (to find providers) or provider and capability (to run one), not both."); |
66 | | - } |
| 65 | + input, |
| 66 | + async run( |
| 67 | + args: z.output<typeof input>, |
| 68 | + abort: AbortSignal, |
| 69 | + apiKey: string, |
| 70 | + confirm?: Confirm, |
| 71 | + ) { |
| 72 | + const query = args.query?.trim(); |
| 73 | + const target = args.provider && args.capability ? `${args.provider}/${args.capability}` : undefined; |
| 74 | + if (!query === !target) { |
| 75 | + throw new Error("Pass either query (to find providers) or provider and capability (to run one), not both."); |
| 76 | + } |
67 | 77 |
|
68 | | - if (query) { |
69 | | - const body = await post<SearchResponse>( |
70 | | - "/search", |
71 | | - // Full detail carries each capability's input contract, so the model can run it without guessing options. |
72 | | - { query, sources: [{ type: "alexandria" }], limit: 5, toolDetail: "full" }, |
73 | | - apiKey, |
74 | | - context.abort, |
75 | | - ); |
76 | | - // A recorded example response can run to hundreds of rows; the field list already describes the shape. |
77 | | - const tools = (body.data?.tools ?? []).map(({ example, ...rest }) => |
78 | | - example?.request ? { ...rest, example: { request: example.request } } : rest, |
79 | | - ); |
80 | | - return { |
81 | | - title: query, |
82 | | - output: tools.length |
83 | | - ? JSON.stringify(tools, null, 2) |
84 | | - : "No Alexandria providers matched. Describe the data differently, or use web search.", |
85 | | - metadata: { count: tools.length }, |
86 | | - }; |
87 | | - } |
88 | | - |
89 | | - await context.ask({ |
90 | | - permission: "firecrawl_alexandria", |
91 | | - patterns: [target!], |
92 | | - always: [target!], |
93 | | - metadata: { provider: args.provider, capability: args.capability, options: args.options }, |
94 | | - }); |
95 | | - const body = await post<ScrapeResponse>( |
96 | | - "/scrape", |
97 | | - { alexandria: [{ provider: args.provider, capability: args.capability, options: args.options ?? {} }] }, |
| 78 | + if (query) { |
| 79 | + const body = await post<SearchResponse>( |
| 80 | + "/search", |
| 81 | + // Full detail carries each capability's input contract, so the model can run it without guessing options. |
| 82 | + { query, sources: [{ type: "alexandria" }], limit: 5, toolDetail: "full" }, |
98 | 83 | apiKey, |
99 | | - context.abort, |
| 84 | + abort, |
| 85 | + ); |
| 86 | + // A recorded example response can run to hundreds of rows; the field list already describes the shape. |
| 87 | + const tools = (body.data?.tools ?? []).map(({ example, ...rest }) => |
| 88 | + example?.request ? { ...rest, example: { request: example.request } } : rest, |
100 | 89 | ); |
101 | | - const results = body.data?.alexandria ?? []; |
102 | 90 | return { |
103 | | - title: target!, |
104 | | - output: results.length ? JSON.stringify(body.data, null, 2) : "The capability returned no data.", |
105 | | - metadata: { |
106 | | - creditsCost: body.data?.creditsCost, |
107 | | - failed: results.some((item) => item.error), |
108 | | - }, |
| 91 | + title: query, |
| 92 | + output: tools.length |
| 93 | + ? JSON.stringify(tools, null, 2) |
| 94 | + : "No Alexandria providers matched. Describe the data differently, or use web search.", |
| 95 | + metadata: { count: tools.length }, |
109 | 96 | }; |
110 | | - }, |
111 | | - }); |
| 97 | + } |
| 98 | + |
| 99 | + await confirm?.(target!, { provider: args.provider, capability: args.capability, options: args.options }); |
| 100 | + const body = await post<ScrapeResponse>( |
| 101 | + "/scrape", |
| 102 | + { alexandria: [{ provider: args.provider, capability: args.capability, options: args.options ?? {} }] }, |
| 103 | + apiKey, |
| 104 | + abort, |
| 105 | + ); |
| 106 | + const results = body.data?.alexandria ?? []; |
| 107 | + return { |
| 108 | + title: target!, |
| 109 | + output: results.length ? JSON.stringify(body.data, null, 2) : "The capability returned no data.", |
| 110 | + metadata: { |
| 111 | + creditsCost: body.data?.creditsCost, |
| 112 | + failed: results.some((item) => item.error), |
| 113 | + }, |
| 114 | + }; |
| 115 | + }, |
| 116 | +}; |
0 commit comments