@sandstormapp/sdk 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Superstorm, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,170 @@
1
- # Temporary Holding Version
1
+ # @sandstormapp/sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Sandstorm's public API as a typed client for Node and edge runtimes: one method per tool in the catalogue, typed from the tool's own JSON Schema, and the tasks those calls start. It is generated from the API's OpenAPI document and depends on nothing at runtime but `fetch`.
4
+
5
+ ```bash
6
+ yarn add @sandstormapp/sdk
7
+ ```
8
+
9
+ ```ts
10
+ import { Sandstorm } from '@sandstormapp/sdk'
11
+
12
+ const sandstorm = new Sandstorm({ apiKey: process.env.SANDSTORM_API_KEY })
13
+
14
+ const task = await sandstorm.tools.generate_image_nano_banana_pro(
15
+ { prompt: 'A ceramic mug on a sunlit bench, steam rising, morning light', asset_name: 'Sunlit Mug' },
16
+ { wait: 120 },
17
+ )
18
+ if (task.status === 'succeeded') console.log(task.files)
19
+ ```
20
+
21
+ ```bash
22
+ curl -X POST https://agent.sandstormapp.com/v1/tools/generate_image_nano_banana_pro \
23
+ -H "Authorization: Bearer $SANDSTORM_API_KEY" \
24
+ -H 'Content-Type: application/json' \
25
+ -H 'Prefer: wait=120' \
26
+ -d '{"prompt":"A ceramic mug on a sunlit bench, steam rising, morning light","asset_name":"Sunlit Mug"}'
27
+ ```
28
+
29
+ ## A key
30
+
31
+ Settings → API keys mints a key, `sk_live_…`, shown once. A key works on one brand of your organization: the brand it was made on, or the one you name.
32
+
33
+ ```ts
34
+ new Sandstorm({ apiKey, brand: '2f7fa2e7-c931-47b2-a66a-f1393233f2b3' })
35
+ ```
36
+
37
+ `brand` is sent as `x-sandstorm-brand` on every request. `baseUrl` points the client somewhere other than `https://agent.sandstormapp.com`, such as an API running locally.
38
+
39
+ ## Calling a tool
40
+
41
+ Every tool is a method on `sandstorm.tools`, named as the catalogue names it, taking the body the API holds it to. A call starts a task and answers its id at once:
42
+
43
+ ```ts
44
+ const started = await sandstorm.tools.search_assets({ types: ['image'], limit: 10 })
45
+ // { task_id: '…', status: 'running' }
46
+ ```
47
+
48
+ ```bash
49
+ curl -X POST https://agent.sandstormapp.com/v1/tools/search_assets \
50
+ -H "Authorization: Bearer $SANDSTORM_API_KEY" -H 'Content-Type: application/json' \
51
+ -d '{"types":["image"],"limit":10}'
52
+ ```
53
+
54
+ With `wait`, the API holds the request up to that many seconds (at most 200) and answers the finished task, or the started shape when the time runs out:
55
+
56
+ ```ts
57
+ const answer = await sandstorm.tools.search_assets({ types: ['image'], limit: 10 }, { wait: 30 })
58
+ if (answer.status === 'running') console.log('still running', answer.task_id)
59
+ else console.log(answer.result)
60
+ ```
61
+
62
+ A tool that asks the person before it runs in the app takes `confirm: true` here, since nobody is there to ask; its input type requires it, and the API refuses the call with 403 without it:
63
+
64
+ ```ts
65
+ await sandstorm.tools.revoke_asset_share({ slug: 'summer-drop', confirm: true })
66
+ ```
67
+
68
+ ```bash
69
+ curl -X POST https://agent.sandstormapp.com/v1/tools/revoke_asset_share \
70
+ -H "Authorization: Bearer $SANDSTORM_API_KEY" -H 'Content-Type: application/json' \
71
+ -d '{"slug":"summer-drop","confirm":true}'
72
+ ```
73
+
74
+ The catalogue itself, every tool this key can call with its schema, hints and whether it asks first:
75
+
76
+ ```ts
77
+ const { tools } = await sandstorm.catalogue()
78
+ ```
79
+
80
+ ```bash
81
+ curl https://agent.sandstormapp.com/v1/tools -H "Authorization: Bearer $SANDSTORM_API_KEY"
82
+ ```
83
+
84
+ ## Following a task
85
+
86
+ ```ts
87
+ const task = await sandstorm.tasks.get(taskId)
88
+ ```
89
+
90
+ ```bash
91
+ curl https://agent.sandstormapp.com/v1/tasks/$TASK_ID -H "Authorization: Bearer $SANDSTORM_API_KEY"
92
+ ```
93
+
94
+ A task is `{ task_id, tool, status, error, started_at, finished_at, result?, files? }`, where `status` is `running`, `succeeded`, `failed` or `cancelled`, `result` is the tool's own JSON once it succeeded and `files` are the files it made or showed, each with a `ref` other tools take and a `url` that keeps working. A task the API has only just started answers `{ task_id, status: 'running' }` until it is on record.
95
+
96
+ `tasks.wait` polls that route every two seconds until the status leaves `running`, and resolves to the task with its result or error:
97
+
98
+ ```ts
99
+ const finished = await sandstorm.tasks.wait(taskId)
100
+ if (finished.status === 'failed') console.error(finished.error)
101
+ ```
102
+
103
+ The twenty most recent calls of this key on its brand, newest first, without their results:
104
+
105
+ ```ts
106
+ const { tasks } = await sandstorm.tasks.list()
107
+ ```
108
+
109
+ ```bash
110
+ curl https://agent.sandstormapp.com/v1/tasks -H "Authorization: Bearer $SANDSTORM_API_KEY"
111
+ ```
112
+
113
+ Stopping one that still runs; whatever it saved before it stopped stays in the library:
114
+
115
+ ```ts
116
+ await sandstorm.tasks.stop(taskId)
117
+ ```
118
+
119
+ ```bash
120
+ curl -X POST https://agent.sandstormapp.com/v1/tasks/$TASK_ID/stop -H "Authorization: Bearer $SANDSTORM_API_KEY"
121
+ ```
122
+
123
+ ## Refusals
124
+
125
+ A response the API refuses throws a `SandstormError` with the HTTP `status`, the API's `code` and its message:
126
+
127
+ ```ts
128
+ import { SandstormError } from '@sandstormapp/sdk'
129
+
130
+ try {
131
+ await sandstorm.tools.archive_offer({ offer: 'offer:1a2b3c4d' })
132
+ } catch (error) {
133
+ if (error instanceof SandstormError && error.code === 'forbidden') console.log(error.message)
134
+ }
135
+ ```
136
+
137
+ | status | code | when |
138
+ | --- | --- | --- |
139
+ | 400 | `bad_request` | the body does not match the tool's schema; the message names the mismatch |
140
+ | 401 | `unauthorized` | the bearer is not a live key |
141
+ | 402 | `billing` | the organization cannot run Sandstorm right now |
142
+ | 403 | `forbidden` | the tool asks first and the body did not carry `confirm: true`, or the brand named is not one of the organization's |
143
+ | 404 | `tool_not_found`, `task_not_found` | no such tool for this brand, or no such task |
144
+ | 409 | `task_not_running` | the task to stop has already ended |
145
+ | 429 | `rate_limited` | more than 300 requests in a minute from this key |
146
+
147
+ An answer that is not the API's, such as one from the edge, throws with its `status`, no `code`, and the text of the answer.
148
+
149
+ ## The generated client
150
+
151
+ `src/openapi.ts` and `openapi.json` are generated from the API's `GET /v1/openapi.json`, which the API produces from its live tool catalogue, and both are committed, so the package builds without an API. To regenerate:
152
+
153
+ ```bash
154
+ yarn generate https://agent.sandstormapp.com/v1/openapi.json # from a running API, replacing openapi.json
155
+ yarn generate path/to/openapi.json # from a document on disk
156
+ yarn generate # from the committed openapi.json
157
+ ```
158
+
159
+ `yarn test` fails when the committed `src/openapi.ts` is not what the committed document generates, and a document regenerated from the API shows every catalogue change as a diff of `openapi.json`. `agent/README.md` says how to run the API locally; its document is at `http://localhost:8787/v1/openapi.json`.
160
+
161
+ ## Developing
162
+
163
+ ```bash
164
+ yarn install
165
+ yarn typecheck # src, test and scripts
166
+ yarn test # the client against a stubbed fetch, and the stale-output check
167
+ yarn build # dist/, which is what the package ships
168
+ ```
169
+
170
+ Node 22.18 or newer runs `scripts/generate.ts` as it is.
@@ -0,0 +1,47 @@
1
+ import { toolNames, type components, type paths } from './openapi.ts';
2
+ export { toolNames };
3
+ export type Task = components['schemas']['Task'];
4
+ export type Started = components['schemas']['Started'];
5
+ export type Tool = components['schemas']['Tool'];
6
+ export type Finished = Task & {
7
+ readonly status: Exclude<Task['status'], 'running'>;
8
+ };
9
+ export type ToolName = (typeof toolNames)[number];
10
+ export type ToolInput<Name extends ToolName> = paths[`/v1/tools/${Name}`]['post']['requestBody']['content']['application/json'];
11
+ export type Catalogue = paths['/v1/tools']['get']['responses'][200]['content']['application/json'];
12
+ export type TaskList = paths['/v1/tasks']['get']['responses'][200]['content']['application/json'];
13
+ export type Stopped = paths['/v1/tasks/{taskId}/stop']['post']['responses'][202]['content']['application/json'];
14
+ export interface Wait {
15
+ readonly wait: number;
16
+ }
17
+ export interface ToolCall<Name extends ToolName> {
18
+ (input: ToolInput<Name>): Promise<Started>;
19
+ (input: ToolInput<Name>, options: Wait): Promise<Task | Started>;
20
+ }
21
+ export type Tools = {
22
+ readonly [Name in ToolName]: ToolCall<Name>;
23
+ };
24
+ export interface Tasks {
25
+ list(): Promise<TaskList>;
26
+ get(taskId: string): Promise<Task | Started>;
27
+ stop(taskId: string): Promise<Stopped>;
28
+ wait(taskId: string): Promise<Finished>;
29
+ }
30
+ export interface Options {
31
+ readonly apiKey: string;
32
+ readonly brand?: string;
33
+ readonly baseUrl?: string;
34
+ }
35
+ export declare class SandstormError extends Error {
36
+ readonly name = "SandstormError";
37
+ readonly status: number;
38
+ readonly code: string | undefined;
39
+ constructor(status: number, code: string | undefined, message: string);
40
+ }
41
+ export declare class Sandstorm {
42
+ #private;
43
+ readonly tools: Tools;
44
+ readonly tasks: Tasks;
45
+ constructor({ apiKey, brand, baseUrl }: Options);
46
+ catalogue(): Promise<Catalogue>;
47
+ }
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ import { toolNames } from "./openapi.js";
2
+ export { toolNames };
3
+ export class SandstormError extends Error {
4
+ name = 'SandstormError';
5
+ status;
6
+ code;
7
+ constructor(status, code, message) {
8
+ super(message);
9
+ this.status = status;
10
+ this.code = code;
11
+ }
12
+ }
13
+ const BASE_URL = 'https://agent.sandstormapp.com';
14
+ const POLL_MS = 2_000;
15
+ const JSON_BODY = { 'content-type': 'application/json' };
16
+ function refusalOf(text) {
17
+ try {
18
+ const body = JSON.parse(text);
19
+ return typeof body.error === 'string' && typeof body.code === 'string' ? { error: body.error, code: body.code } : undefined;
20
+ }
21
+ catch {
22
+ return undefined;
23
+ }
24
+ }
25
+ async function refused(response) {
26
+ const text = await response.text();
27
+ const refusal = refusalOf(text);
28
+ return refusal === undefined
29
+ ? new SandstormError(response.status, undefined, text === '' ? response.statusText : text)
30
+ : new SandstormError(response.status, refusal.code, refusal.error);
31
+ }
32
+ const finished = (task) => task.status !== 'running';
33
+ export class Sandstorm {
34
+ tools;
35
+ tasks;
36
+ #baseUrl;
37
+ #headers;
38
+ constructor({ apiKey, brand, baseUrl = BASE_URL }) {
39
+ this.#baseUrl = baseUrl.replace(/\/$/, '');
40
+ this.#headers = { authorization: `Bearer ${apiKey}`, ...(brand === undefined ? {} : { 'x-sandstorm-brand': brand }) };
41
+ this.tools = Object.fromEntries(toolNames.map((name) => [name, (input, options) => this.#call(name, input, options)]));
42
+ this.tasks = {
43
+ list: () => this.#send('GET', '/v1/tasks'),
44
+ get: (taskId) => this.#send('GET', `/v1/tasks/${taskId}`),
45
+ stop: (taskId) => this.#send('POST', `/v1/tasks/${taskId}/stop`),
46
+ wait: async (taskId) => {
47
+ for (;;) {
48
+ const task = await this.tasks.get(taskId);
49
+ if (finished(task))
50
+ return task;
51
+ await new Promise((resolve) => setTimeout(resolve, POLL_MS));
52
+ }
53
+ },
54
+ };
55
+ }
56
+ catalogue() {
57
+ return this.#send('GET', '/v1/tools');
58
+ }
59
+ #call(name, input, options) {
60
+ const headers = options === undefined ? JSON_BODY : { ...JSON_BODY, prefer: `wait=${options.wait}` };
61
+ return this.#send('POST', `/v1/tools/${name}`, headers, JSON.stringify(input));
62
+ }
63
+ async #send(method, path, headers = {}, body = null) {
64
+ const response = await fetch(this.#baseUrl + path, { method, headers: { ...this.#headers, ...headers }, body });
65
+ if (!response.ok)
66
+ throw await refused(response);
67
+ return response.json();
68
+ }
69
+ }