1 | |
2 |
|
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 |
|
10 | type Strale = { |
11 | api_key: string; |
12 | base_url?: string; |
13 | }; |
14 |
|
15 | export async function main( |
16 | strale: Strale, |
17 | capability_slug: string, |
18 | inputs: Record<string, unknown>, |
19 | dry_run: boolean = false, |
20 | ) { |
21 | const baseUrl = strale.base_url ?? "https://api.strale.io"; |
22 |
|
23 | const body: Record<string, unknown> = { |
24 | capability_slug, |
25 | inputs, |
26 | }; |
27 | if (dry_run) { |
28 | body.dry_run = true; |
29 | } |
30 |
|
31 | const response = await fetch(`${baseUrl}/v1/do`, { |
32 | method: "POST", |
33 | headers: { |
34 | "Authorization": `Bearer ${strale.api_key}`, |
35 | "Content-Type": "application/json", |
36 | }, |
37 | body: JSON.stringify(body), |
38 | }); |
39 |
|
40 | if (!response.ok) { |
41 | const error = await response.json().catch(() => ({ message: response.statusText })); |
42 | throw new Error( |
43 | `Strale API error (${response.status}): ${(error as any).error?.message ?? (error as any).message ?? "Unknown error"}` |
44 | ); |
45 | } |
46 |
|
47 | const data = await response.json() as { |
48 | transaction_id?: string; |
49 | capability_slug: string; |
50 | status: string; |
51 | output?: Record<string, unknown>; |
52 | provenance?: Record<string, unknown>; |
53 | quality?: Record<string, unknown>; |
54 | cost_cents?: number; |
55 | wallet_balance_cents?: number; |
56 | dry_run?: boolean; |
57 | }; |
58 |
|
59 | return { |
60 | transaction_id: data.transaction_id, |
61 | capability: data.capability_slug, |
62 | status: data.status, |
63 | output: data.output, |
64 | provenance: data.provenance, |
65 | quality: data.quality, |
66 | cost: data.cost_cents != null ? `€${(data.cost_cents / 100).toFixed(2)}` : undefined, |
67 | wallet_balance_cents: data.wallet_balance_cents, |
68 | dry_run: data.dry_run, |
69 | }; |
70 | } |