@miosa/sdk 0.3.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/README.md +181 -0
- package/dist/index.d.ts +4689 -0
- package/dist/index.js +6045 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
- package/src/client.ts +249 -0
- package/src/errors.ts +136 -0
- package/src/http.test.ts +374 -0
- package/src/http.ts +390 -0
- package/src/index.ts +452 -0
- package/src/resources/admin.ts +348 -0
- package/src/resources/analytics.ts +60 -0
- package/src/resources/api-keys.ts +119 -0
- package/src/resources/audit-log.ts +64 -0
- package/src/resources/benchmarks.ts +104 -0
- package/src/resources/builder-sessions.ts +75 -0
- package/src/resources/channels.ts +143 -0
- package/src/resources/checkpoints.ts +225 -0
- package/src/resources/command-center.ts +73 -0
- package/src/resources/community.ts +103 -0
- package/src/resources/completions.ts +104 -0
- package/src/resources/computer-auto-stop.ts +43 -0
- package/src/resources/computer-env.ts +76 -0
- package/src/resources/computer-logs.ts +50 -0
- package/src/resources/computer-osa.ts +76 -0
- package/src/resources/computer-ports.ts +91 -0
- package/src/resources/computer-terminal.ts +61 -0
- package/src/resources/computer-volumes.ts +64 -0
- package/src/resources/computer.ts +530 -0
- package/src/resources/computers.ts +75 -0
- package/src/resources/credits.ts +43 -0
- package/src/resources/cron-jobs.ts +191 -0
- package/src/resources/custom_domains.ts +123 -0
- package/src/resources/dashboard.ts +49 -0
- package/src/resources/databases.ts +218 -0
- package/src/resources/deployments.ts +777 -0
- package/src/resources/desktop.ts +134 -0
- package/src/resources/email.ts +212 -0
- package/src/resources/embeddings.ts +36 -0
- package/src/resources/events.ts +296 -0
- package/src/resources/exec.ts +319 -0
- package/src/resources/external-keys.ts +79 -0
- package/src/resources/files.test.ts +339 -0
- package/src/resources/files.ts +220 -0
- package/src/resources/flat-custom-domains.ts +127 -0
- package/src/resources/functions.ts +178 -0
- package/src/resources/health-checks.ts +165 -0
- package/src/resources/integrations.ts +183 -0
- package/src/resources/mcp.ts +70 -0
- package/src/resources/models.ts +45 -0
- package/src/resources/network_policy.ts +73 -0
- package/src/resources/open-computers/agents.ts +91 -0
- package/src/resources/open-computers/apps.ts +102 -0
- package/src/resources/open-computers/clusters.ts +88 -0
- package/src/resources/open-computers/desktop.ts +34 -0
- package/src/resources/open-computers/files.ts +97 -0
- package/src/resources/open-computers/hosts.ts +85 -0
- package/src/resources/open-computers/index.ts +98 -0
- package/src/resources/open-computers/jobs.ts +75 -0
- package/src/resources/open-computers/open_computers.test.ts +288 -0
- package/src/resources/open-computers/secrets.ts +115 -0
- package/src/resources/open-computers/terminal.ts +33 -0
- package/src/resources/open-computers/tunnels.ts +87 -0
- package/src/resources/open-computers/types.ts +343 -0
- package/src/resources/open-computers/workspaces.ts +135 -0
- package/src/resources/project-auth.ts +142 -0
- package/src/resources/project-integrations.ts +133 -0
- package/src/resources/provider-defaults.ts +89 -0
- package/src/resources/regions.ts +94 -0
- package/src/resources/sandbox-templates.ts +195 -0
- package/src/resources/sandboxes.live.test.ts +92 -0
- package/src/resources/sandboxes.test.ts +624 -0
- package/src/resources/sandboxes.ts +1173 -0
- package/src/resources/settings.ts +143 -0
- package/src/resources/snapshots-standalone.ts +51 -0
- package/src/resources/storage.ts +221 -0
- package/src/resources/tenant.ts +39 -0
- package/src/resources/usage.ts +85 -0
- package/src/resources/volumes.ts +117 -0
- package/src/resources/webhooks.ts +171 -0
- package/src/types.ts +460 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Project integrations — third-party API key connections injected into VMs.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { HttpClient } from "../http.js";
|
|
6
|
+
|
|
7
|
+
// ── Resource shapes ──────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export interface ProjectIntegrationData {
|
|
10
|
+
id?: string;
|
|
11
|
+
project_id?: string;
|
|
12
|
+
provider?: string;
|
|
13
|
+
status?: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ProjectIntegrationCatalogEntry {
|
|
18
|
+
provider?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
description?: string;
|
|
21
|
+
schema?: Record<string, unknown>;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ── Request payloads ─────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
export interface ProjectIntegrationListParams {
|
|
28
|
+
project_id?: string;
|
|
29
|
+
provider?: string;
|
|
30
|
+
[key: string]: string | number | boolean | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ProjectIntegrationCreateParams {
|
|
34
|
+
project_id?: string;
|
|
35
|
+
provider?: string;
|
|
36
|
+
credentials?: Record<string, unknown>;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ProjectIntegrationUpdateParams {
|
|
41
|
+
credentials?: Record<string, unknown>;
|
|
42
|
+
[key: string]: unknown;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
46
|
+
|
|
47
|
+
function unwrap<T>(payload: unknown): T {
|
|
48
|
+
if (payload && typeof payload === "object") {
|
|
49
|
+
const p = payload as Record<string, unknown>;
|
|
50
|
+
for (const k of ["data", "project_integrations", "catalog", "items"]) {
|
|
51
|
+
if (k in p) return p[k] as T;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return payload as T;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function listItems<T>(payload: unknown): T[] {
|
|
58
|
+
const result = unwrap<T[] | unknown>(payload);
|
|
59
|
+
if (Array.isArray(result)) return result;
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function stripUndefined(
|
|
64
|
+
input: Record<string, unknown>,
|
|
65
|
+
): Record<string, string | number | boolean | undefined> {
|
|
66
|
+
return Object.fromEntries(
|
|
67
|
+
Object.entries(input).filter(([, v]) => v !== undefined),
|
|
68
|
+
) as Record<string, string | number | boolean | undefined>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function stripUndefObj(
|
|
72
|
+
input: Record<string, unknown>,
|
|
73
|
+
): Record<string, unknown> {
|
|
74
|
+
return Object.fromEntries(
|
|
75
|
+
Object.entries(input).filter(([, v]) => v !== undefined),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ── Main resource ─────────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
export class ProjectIntegrations {
|
|
82
|
+
constructor(private readonly http: HttpClient) {}
|
|
83
|
+
|
|
84
|
+
/** List project integrations. */
|
|
85
|
+
async list(
|
|
86
|
+
params: ProjectIntegrationListParams = {},
|
|
87
|
+
): Promise<ProjectIntegrationData[]> {
|
|
88
|
+
const query = stripUndefined(params as Record<string, unknown>);
|
|
89
|
+
const data = await this.http.get<unknown>("/project-integrations", query);
|
|
90
|
+
return listItems<ProjectIntegrationData>(data);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** List supported providers and their schemas. */
|
|
94
|
+
async catalog(): Promise<ProjectIntegrationCatalogEntry[]> {
|
|
95
|
+
const data = await this.http.get<unknown>("/project-integrations/catalog");
|
|
96
|
+
return listItems<ProjectIntegrationCatalogEntry>(data);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Get a project integration by id. */
|
|
100
|
+
async get(integrationId: string): Promise<ProjectIntegrationData> {
|
|
101
|
+
const data = await this.http.get<unknown>(
|
|
102
|
+
`/project-integrations/${integrationId}`,
|
|
103
|
+
);
|
|
104
|
+
return unwrap<ProjectIntegrationData>(data);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Create a project integration. */
|
|
108
|
+
async create(
|
|
109
|
+
params: ProjectIntegrationCreateParams,
|
|
110
|
+
): Promise<ProjectIntegrationData> {
|
|
111
|
+
const body = stripUndefObj(params as Record<string, unknown>);
|
|
112
|
+
const data = await this.http.post<unknown>("/project-integrations", body);
|
|
113
|
+
return unwrap<ProjectIntegrationData>(data);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Update a project integration. */
|
|
117
|
+
async update(
|
|
118
|
+
integrationId: string,
|
|
119
|
+
params: ProjectIntegrationUpdateParams,
|
|
120
|
+
): Promise<ProjectIntegrationData> {
|
|
121
|
+
const body = stripUndefObj(params as Record<string, unknown>);
|
|
122
|
+
const data = await this.http.patch<unknown>(
|
|
123
|
+
`/project-integrations/${integrationId}`,
|
|
124
|
+
body,
|
|
125
|
+
);
|
|
126
|
+
return unwrap<ProjectIntegrationData>(data);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Delete a project integration. */
|
|
130
|
+
async delete(integrationId: string): Promise<void> {
|
|
131
|
+
await this.http.delete<unknown>(`/project-integrations/${integrationId}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ProviderDefaults — admin LLM provider routing config.
|
|
3
|
+
*
|
|
4
|
+
* Routes:
|
|
5
|
+
* GET /admin/provider-defaults
|
|
6
|
+
* PUT /admin/provider-defaults
|
|
7
|
+
* GET /admin/tenants/:id/provider-config
|
|
8
|
+
* PUT /admin/tenants/:id/provider-config
|
|
9
|
+
* DELETE /admin/tenants/:id/provider-config
|
|
10
|
+
*
|
|
11
|
+
* Requires admin credential (msk_a_* / msk_p_* or admin JWT).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { HttpClient } from "../http.js";
|
|
15
|
+
|
|
16
|
+
function unwrap(data: unknown): Record<string, unknown> {
|
|
17
|
+
if (data && typeof data === "object") {
|
|
18
|
+
const d = data as Record<string, unknown>;
|
|
19
|
+
for (const k of ["data", "defaults", "provider_defaults", "config"]) {
|
|
20
|
+
if (k in d) return d[k] as Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return data as Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class ProviderDefaults {
|
|
27
|
+
constructor(private readonly http: HttpClient) {}
|
|
28
|
+
|
|
29
|
+
/** Get the current fleet-wide provider defaults. */
|
|
30
|
+
async list(): Promise<Record<string, unknown>> {
|
|
31
|
+
return unwrap(await this.http.get<unknown>("/admin/provider-defaults"));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Return the defaults entry for a single provider, or {} if missing. */
|
|
35
|
+
async get(provider: string): Promise<Record<string, unknown>> {
|
|
36
|
+
const data = await this.list();
|
|
37
|
+
const providers =
|
|
38
|
+
(data.providers as Record<string, unknown> | undefined) ?? data;
|
|
39
|
+
if (
|
|
40
|
+
typeof providers === "object" &&
|
|
41
|
+
providers !== null &&
|
|
42
|
+
provider in providers
|
|
43
|
+
) {
|
|
44
|
+
return providers[provider] as Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Replace the fleet-wide defaults (PUT /admin/provider-defaults). */
|
|
50
|
+
async update(
|
|
51
|
+
opts: Record<string, unknown>,
|
|
52
|
+
): Promise<Record<string, unknown>> {
|
|
53
|
+
const body = Object.fromEntries(
|
|
54
|
+
Object.entries(opts).filter(([, v]) => v !== undefined),
|
|
55
|
+
);
|
|
56
|
+
return unwrap(
|
|
57
|
+
await this.http.put<unknown>("/admin/provider-defaults", body),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ── Per-tenant overrides ────────────────────────────────────────────────
|
|
62
|
+
|
|
63
|
+
async getTenant(tenantId: string): Promise<Record<string, unknown>> {
|
|
64
|
+
return unwrap(
|
|
65
|
+
await this.http.get<unknown>(
|
|
66
|
+
`/admin/tenants/${tenantId}/provider-config`,
|
|
67
|
+
),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async setTenant(
|
|
72
|
+
tenantId: string,
|
|
73
|
+
opts: Record<string, unknown>,
|
|
74
|
+
): Promise<Record<string, unknown>> {
|
|
75
|
+
const body = Object.fromEntries(
|
|
76
|
+
Object.entries(opts).filter(([, v]) => v !== undefined),
|
|
77
|
+
);
|
|
78
|
+
return unwrap(
|
|
79
|
+
await this.http.put<unknown>(
|
|
80
|
+
`/admin/tenants/${tenantId}/provider-config`,
|
|
81
|
+
body,
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async resetTenant(tenantId: string): Promise<void> {
|
|
87
|
+
await this.http.delete(`/admin/tenants/${tenantId}/provider-config`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regions — datacenter availability, sizes, pricing, templates.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { HttpClient } from "../http.js";
|
|
6
|
+
|
|
7
|
+
// ── Resource shapes ──────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
export interface RegionData {
|
|
10
|
+
id?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
slug?: string;
|
|
13
|
+
available?: boolean;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SizeData {
|
|
18
|
+
id?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
slug?: string;
|
|
21
|
+
cpu?: number;
|
|
22
|
+
memory_mb?: number;
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface TemplateData {
|
|
27
|
+
id?: string;
|
|
28
|
+
name?: string;
|
|
29
|
+
slug?: string;
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
function unwrap<T>(payload: unknown): T {
|
|
36
|
+
if (payload && typeof payload === "object") {
|
|
37
|
+
const p = payload as Record<string, unknown>;
|
|
38
|
+
for (const k of [
|
|
39
|
+
"data",
|
|
40
|
+
"regions",
|
|
41
|
+
"sizes",
|
|
42
|
+
"pricing",
|
|
43
|
+
"templates",
|
|
44
|
+
"items",
|
|
45
|
+
]) {
|
|
46
|
+
if (k in p) return p[k] as T;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return payload as T;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function listItems<T>(payload: unknown): T[] {
|
|
53
|
+
const result = unwrap<T[] | unknown>(payload);
|
|
54
|
+
if (Array.isArray(result)) return result;
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ── Main resource ─────────────────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
export class Regions {
|
|
61
|
+
constructor(private readonly http: HttpClient) {}
|
|
62
|
+
|
|
63
|
+
/** List datacenter regions. */
|
|
64
|
+
async listRegions(): Promise<RegionData[]> {
|
|
65
|
+
const data = await this.http.get<unknown>("/compute/regions");
|
|
66
|
+
return listItems<RegionData>(data);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** List available compute sizes. */
|
|
70
|
+
async listSizes(): Promise<SizeData[]> {
|
|
71
|
+
const data = await this.http.get<unknown>("/compute/sizes");
|
|
72
|
+
return listItems<SizeData>(data);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Get static compute pricing data. */
|
|
76
|
+
async pricing(): Promise<unknown> {
|
|
77
|
+
const data = await this.http.get<unknown>("/compute/pricing");
|
|
78
|
+
return unwrap(data);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** List community computer templates. */
|
|
82
|
+
async listTemplates(): Promise<TemplateData[]> {
|
|
83
|
+
const data = await this.http.get<unknown>("/compute/templates");
|
|
84
|
+
return listItems<TemplateData>(data);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Get a single community template by id. */
|
|
88
|
+
async getTemplate(templateId: string): Promise<TemplateData> {
|
|
89
|
+
const data = await this.http.get<unknown>(
|
|
90
|
+
`/compute/templates/${templateId}`,
|
|
91
|
+
);
|
|
92
|
+
return unwrap<TemplateData>(data);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SandboxTemplates resource — tenant sandbox template management.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
|
|
7
|
+
import type { HttpClient } from "../http.js";
|
|
8
|
+
|
|
9
|
+
// ── Branded IDs ──────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
export type SandboxTemplateResourceId = string & {
|
|
12
|
+
readonly __brand: "SandboxTemplateResourceId";
|
|
13
|
+
};
|
|
14
|
+
export type SandboxTemplateBuildResourceId = string & {
|
|
15
|
+
readonly __brand: "SandboxTemplateBuildResourceId";
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// ── Resource shapes ──────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
export interface SandboxTemplateResourceData {
|
|
21
|
+
id: SandboxTemplateResourceId;
|
|
22
|
+
tenant_id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
state?: string;
|
|
25
|
+
build_spec?: Record<string, unknown>;
|
|
26
|
+
aliases?: string[];
|
|
27
|
+
created_at?: string;
|
|
28
|
+
updated_at?: string;
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SandboxTemplateBuildResourceData {
|
|
33
|
+
id: SandboxTemplateBuildResourceId;
|
|
34
|
+
template_id: SandboxTemplateResourceId;
|
|
35
|
+
state?: string;
|
|
36
|
+
started_at?: string | null;
|
|
37
|
+
finished_at?: string | null;
|
|
38
|
+
error?: string | null;
|
|
39
|
+
created_at?: string;
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ── Request payloads ─────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
export interface SandboxTemplateListParams {
|
|
46
|
+
include_aliases?: boolean;
|
|
47
|
+
includeAliases?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TemplateCreateParams {
|
|
51
|
+
name: string;
|
|
52
|
+
build_spec: Record<string, unknown>;
|
|
53
|
+
buildSpec?: Record<string, unknown>;
|
|
54
|
+
idempotencyKey?: string;
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface TemplateBuildCreateParams {
|
|
59
|
+
idempotencyKey?: string;
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
function unwrap<T>(payload: unknown): T {
|
|
66
|
+
if (payload && typeof payload === "object" && "data" in (payload as object)) {
|
|
67
|
+
return (payload as { data: T }).data;
|
|
68
|
+
}
|
|
69
|
+
return payload as T;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function listItems<T>(
|
|
73
|
+
payload: unknown,
|
|
74
|
+
candidateKeys: string[] = ["data", "templates", "builds", "items"],
|
|
75
|
+
): T[] {
|
|
76
|
+
if (Array.isArray(payload)) return payload;
|
|
77
|
+
if (!payload || typeof payload !== "object") return [];
|
|
78
|
+
const p = payload as Record<string, unknown>;
|
|
79
|
+
if (Array.isArray(p.data)) return p.data as T[];
|
|
80
|
+
for (const key of candidateKeys) {
|
|
81
|
+
if (Array.isArray(p[key])) return p[key] as T[];
|
|
82
|
+
}
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function stripUndefined(
|
|
87
|
+
input: Record<string, unknown>,
|
|
88
|
+
): Record<string, unknown> {
|
|
89
|
+
return Object.fromEntries(
|
|
90
|
+
Object.entries(input).filter(([, v]) => v !== undefined),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function idempotencyKey(key?: string): string {
|
|
95
|
+
return key ?? randomUUID();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ── Main resource ─────────────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
export class SandboxTemplates {
|
|
101
|
+
constructor(private readonly http: HttpClient) {}
|
|
102
|
+
|
|
103
|
+
async list(
|
|
104
|
+
params: SandboxTemplateListParams = {},
|
|
105
|
+
): Promise<SandboxTemplateResourceData[]> {
|
|
106
|
+
const includeAliases =
|
|
107
|
+
params.includeAliases ?? params.include_aliases ?? false;
|
|
108
|
+
const query = includeAliases
|
|
109
|
+
? ({ include_aliases: true } as Record<
|
|
110
|
+
string,
|
|
111
|
+
string | number | boolean | undefined
|
|
112
|
+
>)
|
|
113
|
+
: undefined;
|
|
114
|
+
const data = await this.http.get<unknown>("/sandbox-templates", query);
|
|
115
|
+
return listItems<SandboxTemplateResourceData>(data, [
|
|
116
|
+
"data",
|
|
117
|
+
"templates",
|
|
118
|
+
"items",
|
|
119
|
+
]);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async get(templateId: string): Promise<SandboxTemplateResourceData> {
|
|
123
|
+
const data = await this.http.get<unknown>(
|
|
124
|
+
`/sandbox-templates/${templateId}`,
|
|
125
|
+
);
|
|
126
|
+
return unwrap<SandboxTemplateResourceData>(data);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async create(
|
|
130
|
+
params: TemplateCreateParams,
|
|
131
|
+
): Promise<SandboxTemplateResourceData> {
|
|
132
|
+
const {
|
|
133
|
+
idempotencyKey: ikey,
|
|
134
|
+
buildSpec,
|
|
135
|
+
build_spec,
|
|
136
|
+
name,
|
|
137
|
+
...rest
|
|
138
|
+
} = params;
|
|
139
|
+
const body = stripUndefined({
|
|
140
|
+
name,
|
|
141
|
+
build_spec: buildSpec ?? build_spec,
|
|
142
|
+
...rest,
|
|
143
|
+
});
|
|
144
|
+
const data = await this.http.request<unknown>("/sandbox-templates", {
|
|
145
|
+
method: "POST",
|
|
146
|
+
body,
|
|
147
|
+
headers: { "Idempotency-Key": idempotencyKey(ikey) },
|
|
148
|
+
});
|
|
149
|
+
return unwrap<SandboxTemplateResourceData>(data);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async buildSpecSchema(): Promise<Record<string, unknown>> {
|
|
153
|
+
const data = await this.http.get<unknown>("/sandbox-templates/build-spec");
|
|
154
|
+
return (data ?? {}) as Record<string, unknown>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async validate(
|
|
158
|
+
buildSpec: Record<string, unknown>,
|
|
159
|
+
): Promise<Record<string, unknown>> {
|
|
160
|
+
const data = await this.http.post<unknown>("/sandbox-templates/validate", {
|
|
161
|
+
build_spec: buildSpec,
|
|
162
|
+
});
|
|
163
|
+
return (data ?? {}) as Record<string, unknown>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async listBuilds(
|
|
167
|
+
templateId: string,
|
|
168
|
+
): Promise<SandboxTemplateBuildResourceData[]> {
|
|
169
|
+
const data = await this.http.get<unknown>(
|
|
170
|
+
`/sandbox-templates/${templateId}/builds`,
|
|
171
|
+
);
|
|
172
|
+
return listItems<SandboxTemplateBuildResourceData>(data, [
|
|
173
|
+
"data",
|
|
174
|
+
"builds",
|
|
175
|
+
"items",
|
|
176
|
+
]);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async createBuild(
|
|
180
|
+
templateId: string,
|
|
181
|
+
params: TemplateBuildCreateParams = {},
|
|
182
|
+
): Promise<SandboxTemplateBuildResourceData> {
|
|
183
|
+
const { idempotencyKey: ikey, ...rest } = params;
|
|
184
|
+
const body = stripUndefined(rest as Record<string, unknown>);
|
|
185
|
+
const data = await this.http.request<unknown>(
|
|
186
|
+
`/sandbox-templates/${templateId}/builds`,
|
|
187
|
+
{
|
|
188
|
+
method: "POST",
|
|
189
|
+
body,
|
|
190
|
+
headers: { "Idempotency-Key": idempotencyKey(ikey) },
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
return unwrap<SandboxTemplateBuildResourceData>(data);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Miosa } from "../client.js";
|
|
3
|
+
import type { Sandbox } from "./sandboxes.js";
|
|
4
|
+
|
|
5
|
+
const liveApiKey = process.env.MIOSA_API_KEY;
|
|
6
|
+
const liveBaseUrl = process.env.MIOSA_BASE_URL ?? "https://api.miosa.ai/api/v1";
|
|
7
|
+
const describeLive = liveApiKey ? describe : describe.skip;
|
|
8
|
+
|
|
9
|
+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
|
+
|
|
11
|
+
async function waitReady(sandbox: Sandbox, timeoutMs = 60_000): Promise<Sandbox> {
|
|
12
|
+
const deadline = Date.now() + timeoutMs;
|
|
13
|
+
while (Date.now() < deadline) {
|
|
14
|
+
await sandbox.refresh();
|
|
15
|
+
if (sandbox.state === "running" && sandbox.ready) return sandbox;
|
|
16
|
+
await sleep(500);
|
|
17
|
+
}
|
|
18
|
+
throw new Error(`sandbox ${sandbox.id} was not ready within ${timeoutMs}ms`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describeLive("live native sandbox API", () => {
|
|
22
|
+
it(
|
|
23
|
+
"creates, execs, writes files, exposes preview, and destroys",
|
|
24
|
+
async () => {
|
|
25
|
+
const client = new Miosa({
|
|
26
|
+
apiKey: liveApiKey!,
|
|
27
|
+
baseUrl: liveBaseUrl,
|
|
28
|
+
timeout: 60_000,
|
|
29
|
+
maxRetries: 1,
|
|
30
|
+
});
|
|
31
|
+
let sandbox: Sandbox | undefined;
|
|
32
|
+
const marker = `miosa-ts-live-${Date.now()}`;
|
|
33
|
+
const port = 32_173;
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
sandbox = await client.sandboxes.create({
|
|
37
|
+
name: `sdk-ts-live-${Date.now()}`,
|
|
38
|
+
templateId: "miosa-sandbox",
|
|
39
|
+
timeoutSec: 600,
|
|
40
|
+
metadata: { test: "typescript-live-e2e" },
|
|
41
|
+
});
|
|
42
|
+
await waitReady(sandbox);
|
|
43
|
+
|
|
44
|
+
const exec = await sandbox.commands.run("python3 -c 'print(21 * 2)'", {
|
|
45
|
+
timeoutSec: 30,
|
|
46
|
+
});
|
|
47
|
+
expect(exec.exitCode).toBe(0);
|
|
48
|
+
expect(exec.stdout.trim()).toBe("42");
|
|
49
|
+
|
|
50
|
+
await sandbox.files.write("/workspace/index.html", `<h1>${marker}</h1>`);
|
|
51
|
+
expect(await sandbox.files.readText("/workspace/index.html")).toContain(marker);
|
|
52
|
+
const list = await sandbox.files.list("/workspace");
|
|
53
|
+
expect(JSON.stringify(list)).toContain("index.html");
|
|
54
|
+
const stat = await sandbox.files.stat("/workspace/index.html");
|
|
55
|
+
expect(Number(stat.size ?? 0)).toBeGreaterThan(0);
|
|
56
|
+
|
|
57
|
+
await sandbox.commands.run(
|
|
58
|
+
[
|
|
59
|
+
"cd /workspace",
|
|
60
|
+
`nohup python3 -m http.server ${port} --bind 0.0.0.0 >/tmp/miosa-ts-live.log 2>&1 &`,
|
|
61
|
+
].join(" && "),
|
|
62
|
+
{ timeoutSec: 10 },
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
let reachable = false;
|
|
66
|
+
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
67
|
+
const probe = await sandbox.commands.run(
|
|
68
|
+
`python3 - <<'PY'\nimport urllib.request\nprint(urllib.request.urlopen('http://127.0.0.1:${port}', timeout=2).read().decode())\nPY`,
|
|
69
|
+
{ timeoutSec: 5 },
|
|
70
|
+
);
|
|
71
|
+
if (probe.exitCode === 0 && probe.stdout.includes(marker)) {
|
|
72
|
+
reachable = true;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
await sleep(500);
|
|
76
|
+
}
|
|
77
|
+
expect(reachable).toBe(true);
|
|
78
|
+
|
|
79
|
+
const previewUrl = await sandbox.preview.expose(port);
|
|
80
|
+
expect(previewUrl).toMatch(/^https:\/\//);
|
|
81
|
+
const preview = await fetch(previewUrl);
|
|
82
|
+
expect(preview.status).toBe(200);
|
|
83
|
+
expect(await preview.text()).toContain(marker);
|
|
84
|
+
} finally {
|
|
85
|
+
if (sandbox) {
|
|
86
|
+
await sandbox.destroy().catch(() => undefined);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
120_000,
|
|
91
|
+
);
|
|
92
|
+
});
|