|
| 1 | +/** |
| 2 | + * Example 1: Inline Tools |
| 3 | + * |
| 4 | + * Demonstrates defining tools and calling them from sandboxed js-exec scripts. |
| 5 | + * No @executor-js/sdk plugins required for inline tools — only the SDK itself |
| 6 | + * is needed via @just-bash/executor's peer deps. |
| 7 | + * |
| 8 | + * Uses: |
| 9 | + * - countries.trevorblades.com (GraphQL) — country data |
| 10 | + * |
| 11 | + * Run with: npx tsx inline-tools.ts |
| 12 | + */ |
| 13 | + |
| 14 | +import { createExecutor } from "@just-bash/executor"; |
| 15 | +import { Bash } from "just-bash"; |
| 16 | + |
| 17 | +const executor = await createExecutor({ |
| 18 | + tools: { |
| 19 | + // GraphQL tool — queries countries.trevorblades.com |
| 20 | + "countries.list": { |
| 21 | + description: "List countries, optionally filtered by continent code", |
| 22 | + execute: async (args?: { continent?: string }) => { |
| 23 | + const query = args?.continent |
| 24 | + ? `query($code: String!) { countries(filter: { continent: { eq: $code } }) { code name capital emoji } }` |
| 25 | + : `{ countries { code name capital emoji } }`; |
| 26 | + const variables = args?.continent |
| 27 | + ? { code: args.continent } |
| 28 | + : undefined; |
| 29 | + const res = await fetch("https://countries.trevorblades.com/graphql", { |
| 30 | + method: "POST", |
| 31 | + headers: { "Content-Type": "application/json" }, |
| 32 | + body: JSON.stringify({ query, variables }), |
| 33 | + }); |
| 34 | + const json = (await res.json()) as { data: { countries: unknown[] } }; |
| 35 | + return json.data.countries; |
| 36 | + }, |
| 37 | + }, |
| 38 | + |
| 39 | + "countries.get": { |
| 40 | + description: "Get a single country by ISO code", |
| 41 | + execute: async (args: { code: string }) => { |
| 42 | + const query = `query($code: ID!) { country(code: $code) { name capital currency emoji languages { name } continent { name } } }`; |
| 43 | + const res = await fetch("https://countries.trevorblades.com/graphql", { |
| 44 | + method: "POST", |
| 45 | + headers: { "Content-Type": "application/json" }, |
| 46 | + body: JSON.stringify({ query, variables: { code: args.code } }), |
| 47 | + }); |
| 48 | + const json = (await res.json()) as { data: { country: unknown } }; |
| 49 | + return json.data.country; |
| 50 | + }, |
| 51 | + }, |
| 52 | + |
| 53 | + // Simple sync tools |
| 54 | + "util.timestamp": { |
| 55 | + description: "Current Unix timestamp", |
| 56 | + execute: () => ({ ts: Math.floor(Date.now() / 1000) }), |
| 57 | + }, |
| 58 | + "util.random": { |
| 59 | + description: "Random number between min and max", |
| 60 | + execute: (args: { min?: number; max?: number }) => ({ |
| 61 | + value: Math.floor( |
| 62 | + Math.random() * ((args.max ?? 100) - (args.min ?? 0)) + |
| 63 | + (args.min ?? 0), |
| 64 | + ), |
| 65 | + }), |
| 66 | + }, |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +const bash = new Bash({ |
| 71 | + executionLimits: { maxJsTimeoutMs: 60000 }, |
| 72 | + customCommands: executor.commands, |
| 73 | + javascript: { invokeTool: executor.invokeTool }, |
| 74 | +}); |
| 75 | + |
| 76 | +// 1. List European countries |
| 77 | +console.log("1. European countries:"); |
| 78 | +let r = await bash.exec(`js-exec -c ' |
| 79 | + const countries = await tools.countries.list({ continent: "EU" }); |
| 80 | + console.log(countries.length + " countries in Europe"); |
| 81 | + for (const c of countries.slice(0, 5)) { |
| 82 | + console.log(" " + c.emoji + " " + c.name + " — " + c.capital); |
| 83 | + } |
| 84 | + console.log(" ..."); |
| 85 | +'`); |
| 86 | +console.log(r.stdout); |
| 87 | + |
| 88 | +// 2. Country detail |
| 89 | +console.log("2. Country detail:"); |
| 90 | +r = await bash.exec(`js-exec -c ' |
| 91 | + const c = await tools.countries.get({ code: "JP" }); |
| 92 | + console.log(c.emoji + " " + c.name); |
| 93 | + console.log(" Capital: " + c.capital); |
| 94 | + console.log(" Currency: " + c.currency); |
| 95 | + console.log(" Continent: " + c.continent.name); |
| 96 | + console.log(" Languages: " + c.languages.map(l => l.name).join(", ")); |
| 97 | +'`); |
| 98 | +console.log(r.stdout); |
| 99 | + |
| 100 | +// 3. Mix tools from different sources |
| 101 | +console.log("3. Cross-tool script:"); |
| 102 | +r = await bash.exec(`js-exec -c ' |
| 103 | + const ts = await tools.util.timestamp(); |
| 104 | + const rand = await tools.util.random({ min: 0, max: 249 }); |
| 105 | + const all = await tools.countries.list(); |
| 106 | + const pick = all[rand.value]; |
| 107 | +
|
| 108 | + console.log("Report at " + ts.ts); |
| 109 | + console.log("Random country #" + rand.value + ": " + pick.emoji + " " + pick.name); |
| 110 | +'`); |
| 111 | +console.log(r.stdout); |
| 112 | + |
| 113 | +// 4. Tools + virtual filesystem |
| 114 | +console.log("4. Fetch → write to fs → read with bash:"); |
| 115 | +r = await bash.exec(`js-exec -c ' |
| 116 | + const fs = require("fs"); |
| 117 | + const countries = await tools.countries.list({ continent: "SA" }); |
| 118 | + const csv = "code,name,capital\\n" + |
| 119 | + countries.map(c => c.code + "," + c.name + "," + c.capital).join("\\n"); |
| 120 | + fs.writeFileSync("/tmp/south-america.csv", csv); |
| 121 | + console.log("Wrote " + countries.length + " rows to /tmp/south-america.csv"); |
| 122 | +'`); |
| 123 | +console.log(r.stdout); |
| 124 | + |
| 125 | +r = await bash.exec("cat /tmp/south-america.csv | head -5"); |
| 126 | +console.log(" " + r.stdout.split("\n").join("\n ")); |
| 127 | + |
| 128 | +// 5. Error handling |
| 129 | +console.log("5. Error handling:"); |
| 130 | +r = await bash.exec(`js-exec -c ' |
| 131 | + try { |
| 132 | + await tools.countries.get({ code: "NOPE" }); |
| 133 | + } catch (e) { |
| 134 | + console.error("Caught: " + e.message); |
| 135 | + } |
| 136 | + console.log("Script continued after error"); |
| 137 | +'`); |
| 138 | +console.log(r.stdout); |
| 139 | +if (r.stderr) console.log(" stderr: " + r.stderr); |
| 140 | + |
| 141 | +console.log("Done!"); |
0 commit comments