@sandstormapp/sdk 0.0.0-stage → 0.2.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,188 @@
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. A tool that only reads answers its JSON at once; every other tool starts a task the client follows. 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
+ ## Reading
40
+
41
+ A tool that only reads, such as `search_assets`, `read_offer` or `get_brand_identity`, is a `GET` with its arguments as query parameters, a list as its name repeated, and answers the tool's own JSON at once; the method takes the same arguments as an object:
42
+
43
+ ```ts
44
+ const { assets } = await sandstorm.tools.search_assets({ types: ['image'], limit: 10 })
45
+ const identity = await sandstorm.tools.get_brand_identity()
46
+ ```
47
+
48
+ ```bash
49
+ curl 'https://agent.sandstormapp.com/v1/tools/search_assets?types=image&limit=10' \
50
+ -H "Authorization: Bearer $SANDSTORM_API_KEY"
51
+ ```
52
+
53
+ A read the tool refuses, such as a ref it cannot find, throws with status 422 and the tool's reason.
54
+
55
+ ## Calling a tool
56
+
57
+ Every other 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:
58
+
59
+ ```ts
60
+ const started = await sandstorm.tools.generate_image_nano_banana_pro({ prompt, asset_name: 'Sunlit Mug' })
61
+ // { task_id: '…', status: 'running' }
62
+ ```
63
+
64
+ ```bash
65
+ curl -X POST https://agent.sandstormapp.com/v1/tools/generate_image_nano_banana_pro \
66
+ -H "Authorization: Bearer $SANDSTORM_API_KEY" -H 'Content-Type: application/json' \
67
+ -d '{"prompt":"…","asset_name":"Sunlit Mug"}'
68
+ ```
69
+
70
+ 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:
71
+
72
+ ```ts
73
+ const answer = await sandstorm.tools.generate_image_nano_banana_pro({ prompt, asset_name: 'Sunlit Mug' }, { wait: 30 })
74
+ if (answer.status === 'running') console.log('still running', answer.task_id)
75
+ else console.log(answer.files)
76
+ ```
77
+
78
+ 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:
79
+
80
+ ```ts
81
+ await sandstorm.tools.revoke_asset_share({ slug: 'summer-drop', confirm: true })
82
+ ```
83
+
84
+ ```bash
85
+ curl -X POST https://agent.sandstormapp.com/v1/tools/revoke_asset_share \
86
+ -H "Authorization: Bearer $SANDSTORM_API_KEY" -H 'Content-Type: application/json' \
87
+ -d '{"slug":"summer-drop","confirm":true}'
88
+ ```
89
+
90
+ The catalogue itself, every tool this key can call with its schema, hints and whether it asks first; `readOnlyHint` marks the tools that read, and `toolMethods`, exported beside the client, says which method each tool takes:
91
+
92
+ ```ts
93
+ const { tools } = await sandstorm.catalogue()
94
+ ```
95
+
96
+ ```bash
97
+ curl https://agent.sandstormapp.com/v1/tools -H "Authorization: Bearer $SANDSTORM_API_KEY"
98
+ ```
99
+
100
+ ## Following a task
101
+
102
+ ```ts
103
+ const task = await sandstorm.tasks.get(taskId)
104
+ ```
105
+
106
+ ```bash
107
+ curl https://agent.sandstormapp.com/v1/tasks/$TASK_ID -H "Authorization: Bearer $SANDSTORM_API_KEY"
108
+ ```
109
+
110
+ 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.
111
+
112
+ `tasks.wait` polls that route every two seconds until the status leaves `running`, and resolves to the task with its result or error:
113
+
114
+ ```ts
115
+ const finished = await sandstorm.tasks.wait(taskId)
116
+ if (finished.status === 'failed') console.error(finished.error)
117
+ ```
118
+
119
+ The twenty most recent calls of this key on its brand, newest first, without their results:
120
+
121
+ ```ts
122
+ const { tasks } = await sandstorm.tasks.list()
123
+ ```
124
+
125
+ ```bash
126
+ curl https://agent.sandstormapp.com/v1/tasks -H "Authorization: Bearer $SANDSTORM_API_KEY"
127
+ ```
128
+
129
+ Stopping one that still runs; whatever it saved before it stopped stays in the library:
130
+
131
+ ```ts
132
+ await sandstorm.tasks.stop(taskId)
133
+ ```
134
+
135
+ ```bash
136
+ curl -X POST https://agent.sandstormapp.com/v1/tasks/$TASK_ID/stop -H "Authorization: Bearer $SANDSTORM_API_KEY"
137
+ ```
138
+
139
+ ## Refusals
140
+
141
+ A response the API refuses throws a `SandstormError` with the HTTP `status`, the API's `code` and its message:
142
+
143
+ ```ts
144
+ import { SandstormError } from '@sandstormapp/sdk'
145
+
146
+ try {
147
+ await sandstorm.tools.archive_offer({ offer: 'offer:1a2b3c4d' })
148
+ } catch (error) {
149
+ if (error instanceof SandstormError && error.code === 'forbidden') console.log(error.message)
150
+ }
151
+ ```
152
+
153
+ | status | code | when |
154
+ | --- | --- | --- |
155
+ | 400 | `bad_request` | the query or body does not match the tool's schema; the message names the mismatch |
156
+ | 401 | `unauthorized` | the bearer is not a live key |
157
+ | 402 | `billing` | the organization cannot run Sandstorm right now |
158
+ | 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 |
159
+ | 404 | `tool_not_found`, `task_not_found` | no such tool for this brand, or no such task |
160
+ | 405 | `method_not_allowed` | a read was posted, or a task tool was read; the message names the method that works |
161
+ | 409 | `task_not_running` | the task to stop has already ended |
162
+ | 422 | `tool_failed` | a read the tool refused, with the tool's reason |
163
+ | 429 | `rate_limited` | more than 300 requests in a minute from this key |
164
+
165
+ 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.
166
+
167
+ ## The generated client
168
+
169
+ `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:
170
+
171
+ ```bash
172
+ yarn generate https://agent.sandstormapp.com/v1/openapi.json # from a running API, replacing openapi.json
173
+ yarn generate path/to/openapi.json # from a document on disk
174
+ yarn generate # from the committed openapi.json
175
+ ```
176
+
177
+ `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`.
178
+
179
+ ## Developing
180
+
181
+ ```bash
182
+ yarn install
183
+ yarn typecheck # src, test and scripts
184
+ yarn test # the client against a stubbed fetch, and the stale-output check
185
+ yarn build # dist/, which is what the package ships
186
+ ```
187
+
188
+ Node 22.18 or newer runs `scripts/generate.ts` as it is.
@@ -0,0 +1,52 @@
1
+ import { toolMethods, type components, type paths } from './openapi.ts';
2
+ export { toolMethods };
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 = keyof typeof toolMethods;
10
+ type Method<Name extends ToolName> = (typeof toolMethods)[Name];
11
+ type Operation<Name extends ToolName> = NonNullable<paths[`/v1/tools/${Name}`][Method<Name>]>;
12
+ type Query<Name extends ToolName> = Operation<Name>['parameters']['query'];
13
+ export type ToolInput<Name extends ToolName> = Method<Name> extends 'get' ? NonNullable<Query<Name>> : NonNullable<Operation<Name>['requestBody']>['content']['application/json'];
14
+ export type ReadResult<Name extends ToolName> = Operation<Name>['responses'][200]['content']['application/json'];
15
+ export type Catalogue = paths['/v1/tools']['get']['responses'][200]['content']['application/json'];
16
+ export type TaskList = paths['/v1/tasks']['get']['responses'][200]['content']['application/json'];
17
+ export type Stopped = paths['/v1/tasks/{taskId}/stop']['post']['responses'][202]['content']['application/json'];
18
+ export interface Wait {
19
+ readonly wait: number;
20
+ }
21
+ export interface ToolCall<Name extends ToolName> {
22
+ (input: ToolInput<Name>): Promise<Started>;
23
+ (input: ToolInput<Name>, options: Wait): Promise<Task | Started>;
24
+ }
25
+ export type ToolRead<Name extends ToolName> = undefined extends Query<Name> ? (query?: ToolInput<Name>) => Promise<ReadResult<Name>> : (query: ToolInput<Name>) => Promise<ReadResult<Name>>;
26
+ export type Tools = {
27
+ readonly [Name in ToolName]: Method<Name> extends 'get' ? ToolRead<Name> : ToolCall<Name>;
28
+ };
29
+ export interface Tasks {
30
+ list(): Promise<TaskList>;
31
+ get(taskId: string): Promise<Task | Started>;
32
+ stop(taskId: string): Promise<Stopped>;
33
+ wait(taskId: string): Promise<Finished>;
34
+ }
35
+ export interface Options {
36
+ readonly apiKey: string;
37
+ readonly brand?: string;
38
+ readonly baseUrl?: string;
39
+ }
40
+ export declare class SandstormError extends Error {
41
+ readonly name = "SandstormError";
42
+ readonly status: number;
43
+ readonly code: string | undefined;
44
+ constructor(status: number, code: string | undefined, message: string);
45
+ }
46
+ export declare class Sandstorm {
47
+ #private;
48
+ readonly tools: Tools;
49
+ readonly tasks: Tasks;
50
+ constructor({ apiKey, brand, baseUrl }: Options);
51
+ catalogue(): Promise<Catalogue>;
52
+ }
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ import { toolMethods } from "./openapi.js";
2
+ export { toolMethods };
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(Object.entries(toolMethods).map(([name, method]) => [
42
+ name,
43
+ method === 'get'
44
+ ? (query) => this.#read(name, query)
45
+ : (input, options) => this.#call(name, input, options),
46
+ ]));
47
+ this.tasks = {
48
+ list: () => this.#send('GET', '/v1/tasks'),
49
+ get: (taskId) => this.#send('GET', `/v1/tasks/${taskId}`),
50
+ stop: (taskId) => this.#send('POST', `/v1/tasks/${taskId}/stop`),
51
+ wait: async (taskId) => {
52
+ for (;;) {
53
+ const task = await this.tasks.get(taskId);
54
+ if (finished(task))
55
+ return task;
56
+ await new Promise((resolve) => setTimeout(resolve, POLL_MS));
57
+ }
58
+ },
59
+ };
60
+ }
61
+ catalogue() {
62
+ return this.#send('GET', '/v1/tools');
63
+ }
64
+ #read(name, query = {}) {
65
+ const search = new URLSearchParams();
66
+ for (const [key, value] of Object.entries(query)) {
67
+ for (const each of Array.isArray(value) ? value : [value])
68
+ if (each !== undefined)
69
+ search.append(key, String(each));
70
+ }
71
+ const asked = search.toString();
72
+ return this.#send('GET', `/v1/tools/${name}${asked === '' ? '' : `?${asked}`}`);
73
+ }
74
+ #call(name, input, options) {
75
+ const headers = options === undefined ? JSON_BODY : { ...JSON_BODY, prefer: `wait=${options.wait}` };
76
+ return this.#send('POST', `/v1/tools/${name}`, headers, JSON.stringify(input));
77
+ }
78
+ async #send(method, path, headers = {}, body = null) {
79
+ const response = await fetch(this.#baseUrl + path, { method, headers: { ...this.#headers, ...headers }, body });
80
+ if (!response.ok)
81
+ throw await refused(response);
82
+ return response.json();
83
+ }
84
+ }