@shardflux/sdk 0.5.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 +202 -0
- package/README.md +162 -0
- package/dist/audit.d.ts +74 -0
- package/dist/audit.js +49 -0
- package/dist/cell.d.ts +195 -0
- package/dist/cell.js +408 -0
- package/dist/client.d.ts +212 -0
- package/dist/client.js +238 -0
- package/dist/egress.d.ts +152 -0
- package/dist/egress.js +37 -0
- package/dist/errors.d.ts +57 -0
- package/dist/errors.js +73 -0
- package/dist/generated/app-api.d.ts +23479 -0
- package/dist/generated/app-api.js +5 -0
- package/dist/generated/cell-api.d.ts +1832 -0
- package/dist/generated/cell-api.js +5 -0
- package/dist/http.d.ts +39 -0
- package/dist/http.js +121 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +13 -0
- package/dist/secrets.d.ts +100 -0
- package/dist/secrets.js +56 -0
- package/dist/templates.d.ts +328 -0
- package/dist/templates.js +117 -0
- package/dist/tokens.d.ts +35 -0
- package/dist/tokens.js +61 -0
- package/dist/tools.d.ts +91 -0
- package/dist/tools.js +313 -0
- package/dist/usage.d.ts +60 -0
- package/dist/usage.js +45 -0
- package/dist/volumes.d.ts +108 -0
- package/dist/volumes.js +120 -0
- package/dist/workspace.d.ts +70 -0
- package/dist/workspace.js +113 -0
- package/package.json +56 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A workspace handle: the latest view from the application API plus managed
|
|
3
|
+
* tool tokens and cell clients (one per agent label / tool set).
|
|
4
|
+
*/
|
|
5
|
+
import type { ClientContext, Caps, Operation, WaitOptions, WorkspaceView } from './client.js';
|
|
6
|
+
import { CellClient } from './cell.js';
|
|
7
|
+
import type { CellClientOptions } from './cell.js';
|
|
8
|
+
import { ToolTokenManager } from './tokens.js';
|
|
9
|
+
import type { ToolName, ToolToken } from './tokens.js';
|
|
10
|
+
export declare class Workspace {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(ctx: ClientContext, view: WorkspaceView, opts?: {
|
|
13
|
+
agentLabel?: string | undefined;
|
|
14
|
+
tools?: ToolName[] | undefined;
|
|
15
|
+
token?: ToolToken | null;
|
|
16
|
+
});
|
|
17
|
+
get id(): string;
|
|
18
|
+
get key(): string;
|
|
19
|
+
/** Observed lifecycle state reported by the cell. */
|
|
20
|
+
get state(): WorkspaceView['observed_state'];
|
|
21
|
+
get desiredState(): WorkspaceView['desired_state'];
|
|
22
|
+
/** True when observed and desired state are both `running`. */
|
|
23
|
+
get ready(): boolean;
|
|
24
|
+
get cellEndpoint(): string | null;
|
|
25
|
+
/** Actual grants reported by the cell for the running workspace (null until known). */
|
|
26
|
+
get grants(): WorkspaceView['grants'];
|
|
27
|
+
get ceilings(): WorkspaceView['ceilings'];
|
|
28
|
+
get template(): WorkspaceView['template'];
|
|
29
|
+
get pendingReason(): string | null;
|
|
30
|
+
get activeOperation(): WorkspaceView['active_operation'];
|
|
31
|
+
/** The raw view (GET /v1/workspaces/{id}). */
|
|
32
|
+
get data(): WorkspaceView;
|
|
33
|
+
refresh(): Promise<this>;
|
|
34
|
+
/** Waits for the active operation (if any) and refreshes. */
|
|
35
|
+
waitUntilReady(opts?: WaitOptions): Promise<this>;
|
|
36
|
+
delete(opts?: {
|
|
37
|
+
idempotencyKey?: string;
|
|
38
|
+
}): Promise<Operation>;
|
|
39
|
+
suspend(opts?: {
|
|
40
|
+
idempotencyKey?: string;
|
|
41
|
+
}): Promise<Operation>;
|
|
42
|
+
resume(opts?: {
|
|
43
|
+
idempotencyKey?: string;
|
|
44
|
+
}): Promise<Operation>;
|
|
45
|
+
snapshot(opts?: {
|
|
46
|
+
label?: string;
|
|
47
|
+
idempotencyKey?: string;
|
|
48
|
+
}): Promise<Operation>;
|
|
49
|
+
fork(target: {
|
|
50
|
+
key: string;
|
|
51
|
+
caps?: Caps;
|
|
52
|
+
}, opts?: {
|
|
53
|
+
idempotencyKey?: string;
|
|
54
|
+
}): Promise<{
|
|
55
|
+
operation: Operation;
|
|
56
|
+
workspace: Workspace;
|
|
57
|
+
}>;
|
|
58
|
+
/** Token manager for an agent label / tool set (defaults: those given to open()). */
|
|
59
|
+
tokens(opts?: {
|
|
60
|
+
agentLabel?: string;
|
|
61
|
+
tools?: ToolName[];
|
|
62
|
+
}): ToolTokenManager;
|
|
63
|
+
/** Typed client for the cell gateway tools of this workspace. */
|
|
64
|
+
cell(opts?: {
|
|
65
|
+
agentLabel?: string;
|
|
66
|
+
tools?: ToolName[];
|
|
67
|
+
} & CellClientOptions): CellClient;
|
|
68
|
+
/** Tools granted by the most recent token (null before one was issued). */
|
|
69
|
+
get grantedTools(): ToolName[] | null;
|
|
70
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { CellClient } from "./cell.js";
|
|
2
|
+
import { ToolTokenManager } from "./tokens.js";
|
|
3
|
+
export class Workspace {
|
|
4
|
+
#view;
|
|
5
|
+
#ctx;
|
|
6
|
+
#defaults;
|
|
7
|
+
#managers = new Map();
|
|
8
|
+
#cells = new Map();
|
|
9
|
+
constructor(ctx, view, opts = {}) {
|
|
10
|
+
this.#ctx = ctx;
|
|
11
|
+
this.#view = view;
|
|
12
|
+
this.#defaults = { agentLabel: opts.agentLabel, tools: opts.tools };
|
|
13
|
+
if (opts.token)
|
|
14
|
+
this.tokens().seed(opts.token);
|
|
15
|
+
}
|
|
16
|
+
get id() {
|
|
17
|
+
return this.#view.id;
|
|
18
|
+
}
|
|
19
|
+
get key() {
|
|
20
|
+
return this.#view.workspace_key;
|
|
21
|
+
}
|
|
22
|
+
/** Observed lifecycle state reported by the cell. */
|
|
23
|
+
get state() {
|
|
24
|
+
return this.#view.observed_state;
|
|
25
|
+
}
|
|
26
|
+
get desiredState() {
|
|
27
|
+
return this.#view.desired_state;
|
|
28
|
+
}
|
|
29
|
+
/** True when observed and desired state are both `running`. */
|
|
30
|
+
get ready() {
|
|
31
|
+
return this.#view.observed_state === 'running' && this.#view.desired_state === 'running' && this.#view.deleted_at === null;
|
|
32
|
+
}
|
|
33
|
+
get cellEndpoint() {
|
|
34
|
+
return this.#view.cell_endpoint;
|
|
35
|
+
}
|
|
36
|
+
/** Actual grants reported by the cell for the running workspace (null until known). */
|
|
37
|
+
get grants() {
|
|
38
|
+
return this.#view.grants;
|
|
39
|
+
}
|
|
40
|
+
get ceilings() {
|
|
41
|
+
return this.#view.ceilings;
|
|
42
|
+
}
|
|
43
|
+
get template() {
|
|
44
|
+
return this.#view.template;
|
|
45
|
+
}
|
|
46
|
+
get pendingReason() {
|
|
47
|
+
return this.#view.pending_reason;
|
|
48
|
+
}
|
|
49
|
+
get activeOperation() {
|
|
50
|
+
return this.#view.active_operation;
|
|
51
|
+
}
|
|
52
|
+
/** The raw view (GET /v1/workspaces/{id}). */
|
|
53
|
+
get data() {
|
|
54
|
+
return this.#view;
|
|
55
|
+
}
|
|
56
|
+
async refresh() {
|
|
57
|
+
this.#view = await this.#ctx.http.json('GET', `/v1/workspaces/${encodeURIComponent(this.id)}`, {}, this.#ctx.authorization);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/** Waits for the active operation (if any) and refreshes. */
|
|
61
|
+
async waitUntilReady(opts = {}) {
|
|
62
|
+
const op = this.#view.active_operation;
|
|
63
|
+
if (op)
|
|
64
|
+
await this.#ctx.workspaces.waitForOperation(op.id, opts);
|
|
65
|
+
return this.refresh();
|
|
66
|
+
}
|
|
67
|
+
delete(opts = {}) {
|
|
68
|
+
return this.#ctx.workspaces.delete(this.id, opts);
|
|
69
|
+
}
|
|
70
|
+
suspend(opts = {}) {
|
|
71
|
+
return this.#ctx.workspaces.suspend(this.id, opts);
|
|
72
|
+
}
|
|
73
|
+
resume(opts = {}) {
|
|
74
|
+
return this.#ctx.workspaces.resume(this.id, opts);
|
|
75
|
+
}
|
|
76
|
+
snapshot(opts = {}) {
|
|
77
|
+
return this.#ctx.workspaces.snapshot(this.id, opts);
|
|
78
|
+
}
|
|
79
|
+
fork(target, opts = {}) {
|
|
80
|
+
return this.#ctx.workspaces.fork(this.id, target, opts);
|
|
81
|
+
}
|
|
82
|
+
/** Token manager for an agent label / tool set (defaults: those given to open()). */
|
|
83
|
+
tokens(opts = {}) {
|
|
84
|
+
const agentLabel = opts.agentLabel ?? this.#defaults.agentLabel;
|
|
85
|
+
const tools = opts.tools ?? this.#defaults.tools;
|
|
86
|
+
const k = JSON.stringify([agentLabel ?? null, tools ? [...tools].sort() : null]);
|
|
87
|
+
let m = this.#managers.get(k);
|
|
88
|
+
if (!m) {
|
|
89
|
+
m = new ToolTokenManager(this.#ctx.http, this.#ctx.authorization, this.id, { agentLabel, tools });
|
|
90
|
+
this.#managers.set(k, m);
|
|
91
|
+
}
|
|
92
|
+
return m;
|
|
93
|
+
}
|
|
94
|
+
/** Typed client for the cell gateway tools of this workspace. */
|
|
95
|
+
cell(opts = {}) {
|
|
96
|
+
const { agentLabel, tools, ...cellOpts } = opts;
|
|
97
|
+
const manager = this.tokens({ ...(agentLabel !== undefined ? { agentLabel } : {}), ...(tools !== undefined ? { tools } : {}) });
|
|
98
|
+
const k = `${JSON.stringify([agentLabel ?? null, tools ?? null])}`;
|
|
99
|
+
let c = this.#cells.get(k);
|
|
100
|
+
if (!c) {
|
|
101
|
+
c = new CellClient(this.id, manager, { fetch: this.#ctx.fetch, userAgent: this.#ctx.userAgent, sleep: this.#ctx.sleep, ...cellOpts });
|
|
102
|
+
this.#cells.set(k, c);
|
|
103
|
+
}
|
|
104
|
+
return c;
|
|
105
|
+
}
|
|
106
|
+
/** Tools granted by the most recent token (null before one was issued). */
|
|
107
|
+
get grantedTools() {
|
|
108
|
+
for (const m of this.#managers.values())
|
|
109
|
+
if (m.current)
|
|
110
|
+
return [...m.current.tools];
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shardflux/sdk",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Shardflux TypeScript SDK: open persistent agent workspaces by key and give your agent workspace tools (exec, files, processes, PTY, git, browser).",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"homepage": "https://shardflux.dev",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"shardflux",
|
|
10
|
+
"sdk",
|
|
11
|
+
"agents",
|
|
12
|
+
"ai-agents",
|
|
13
|
+
"sandbox",
|
|
14
|
+
"workspace",
|
|
15
|
+
"microvm",
|
|
16
|
+
"code-execution",
|
|
17
|
+
"tools"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=24"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@eslint/js": "10.0.1",
|
|
40
|
+
"@types/node": "24.13.6",
|
|
41
|
+
"eslint": "10.11.0",
|
|
42
|
+
"openapi-typescript": "7.13.0",
|
|
43
|
+
"typescript": "5.9.3",
|
|
44
|
+
"typescript-eslint": "8.70.1"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "node scripts/build.mjs",
|
|
48
|
+
"generate": "openapi-typescript ../contracts/openapi/app-api.json -o src/generated/app-api.ts && openapi-typescript ../contracts/openapi/cell-api.yaml --default-non-nullable false -o src/generated/cell-api.ts",
|
|
49
|
+
"generate:check": "node scripts/check-generated.mjs",
|
|
50
|
+
"typecheck": "node scripts/check-generated.mjs && tsc -p tsconfig.json --noEmit",
|
|
51
|
+
"lint": "eslint .",
|
|
52
|
+
"test": "node --test test/*.test.ts"
|
|
53
|
+
},
|
|
54
|
+
"main": "./dist/index.js",
|
|
55
|
+
"types": "./dist/index.d.ts"
|
|
56
|
+
}
|