@raytrace-cli/sdk 0.1.0 → 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/README.md +1 -0
- package/package.json +7 -2
- package/src/client.mjs +46 -1
- package/src/index.d.ts +4 -0
- package/src/index.mjs +5 -5
package/README.md
CHANGED
|
@@ -39,6 +39,7 @@ await sandbox.stop();
|
|
|
39
39
|
| Call | What it does |
|
|
40
40
|
| --- | --- |
|
|
41
41
|
| `rt.sandboxes.create({ name, size, dir \| files \| archive })` | New sandbox from a folder, an in-memory file map, or a `.tar.gz` |
|
|
42
|
+
| `rt.sandboxes.create({ repo, ref })` | New sandbox from a Git repository: an https or ssh URL, or `owner/name` on GitHub; `ref` picks a branch or tag. Cloned with this machine's `git` and credentials |
|
|
42
43
|
| `rt.sandboxes.list()` / `rt.sandboxes.get(id)` | Your organization's sandboxes |
|
|
43
44
|
| `sandbox.exec(command, { timeoutSeconds })` | Runs a command and waits for `{ exitCode, stdout, stderr, state }` |
|
|
44
45
|
| `sandbox.stop()` | Stops it; files are kept and billing stops |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@raytrace-cli/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "TypeScript/JavaScript SDK for RayTrace cloud sandboxes: create, run commands, stop, start and destroy over the /v1 API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,7 +11,12 @@
|
|
|
11
11
|
"directory": "sdk/ts"
|
|
12
12
|
},
|
|
13
13
|
"bugs": "https://github.com/Godcreatebugs/raytrace-cli/issues",
|
|
14
|
-
"keywords": [
|
|
14
|
+
"keywords": [
|
|
15
|
+
"raytrace",
|
|
16
|
+
"sandbox",
|
|
17
|
+
"coding-agents",
|
|
18
|
+
"sdk"
|
|
19
|
+
],
|
|
15
20
|
"engines": {
|
|
16
21
|
"node": ">=20.0.0"
|
|
17
22
|
},
|
package/src/client.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// Shared low-level /v1 HTTP client for @raytrace-cli/sdk.
|
|
2
2
|
// Zero runtime dependencies (Node 20+ native fetch + zlib + crypto).
|
|
3
3
|
import { gzipSync } from 'node:zlib';
|
|
4
|
-
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
4
|
+
import { mkdtempSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
5
5
|
import { join, posix } from 'node:path';
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
import { tmpdir } from 'node:os';
|
|
8
|
+
import { promisify } from 'node:util';
|
|
6
9
|
|
|
7
10
|
export const DEFAULT_BASE_URL = 'https://api.raytracer.si';
|
|
8
11
|
|
|
@@ -87,6 +90,48 @@ export function buildTarGz(files) {
|
|
|
87
90
|
return gzipSync(Buffer.concat(chunks));
|
|
88
91
|
}
|
|
89
92
|
|
|
93
|
+
// git transports a repository URL may use: https and ssh only, so a URL such
|
|
94
|
+
// as ext::<command> can never run a program.
|
|
95
|
+
const GIT_ENV = { GIT_ALLOW_PROTOCOL: 'https:ssh', GIT_TERMINAL_PROMPT: '0' };
|
|
96
|
+
|
|
97
|
+
/** The clone URL for `repo`: an https or ssh URL as given, `host/owner/name` over https, or GitHub's `owner/name`. */
|
|
98
|
+
export function repoUrl(repo) {
|
|
99
|
+
const value = String(repo || '').trim();
|
|
100
|
+
if (/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(value)) return `https://github.com/${value.replace(/\.git$/, '')}.git`;
|
|
101
|
+
if (/^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+\/[^\s:]+$/.test(value)) return `https://${value}`;
|
|
102
|
+
if (/^https:\/\/[^\s]+$/.test(value) || /^ssh:\/\/[^\s]+$/.test(value) || /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+:[^\s]+$/.test(value)) return value;
|
|
103
|
+
throw new Error(`Unsupported repo "${value}": use an https or ssh URL, or owner/name for GitHub`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The name a repository gives a sandbox: its last path segment, without .git. */
|
|
107
|
+
export function repoName(repo) {
|
|
108
|
+
return String(repo).replace(/\/+$/, '').split(/[/:]/).filter(Boolean).pop().replace(/\.git$/, '') || 'sandbox';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Clones `repo` (shallow; `ref` is a branch or tag) into a temporary folder and
|
|
113
|
+
* packs it like archiveFromDirectory, then deletes the clone. Uses the git on
|
|
114
|
+
* this machine and its credentials, so private repositories work wherever
|
|
115
|
+
* `git clone` does; it never waits for a password prompt.
|
|
116
|
+
*/
|
|
117
|
+
export async function archiveFromRepo(repo, { ref = undefined } = {}) {
|
|
118
|
+
const dir = mkdtempSync(join(tmpdir(), 'raytrace-repo-'));
|
|
119
|
+
try {
|
|
120
|
+
const args = ['clone', '--depth', '1', '--quiet'];
|
|
121
|
+
if (ref) args.push('--branch', String(ref));
|
|
122
|
+
args.push('--', repoUrl(repo), join(dir, 'repo'));
|
|
123
|
+
try {
|
|
124
|
+
await promisify(execFile)('git', args, { env: { ...process.env, ...GIT_ENV }, timeout: 300_000, maxBuffer: 1 << 20 });
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (error.code === 'ENOENT') throw new Error('Creating a sandbox from a repo needs git installed on this machine');
|
|
127
|
+
throw new Error(`Could not clone ${repo}: ${String(error.stderr || error.message).trim()}`);
|
|
128
|
+
}
|
|
129
|
+
return archiveFromDirectory(join(dir, 'repo'));
|
|
130
|
+
} finally {
|
|
131
|
+
rmSync(dir, { recursive: true, force: true });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
90
135
|
export function archiveFromDirectory(dirPath) {
|
|
91
136
|
const files = {};
|
|
92
137
|
function walk(absDir, relPrefix) {
|
package/src/index.d.ts
CHANGED
|
@@ -35,7 +35,10 @@ export interface CreateSandboxOptions {
|
|
|
35
35
|
archive?: Uint8Array;
|
|
36
36
|
files?: Record<string, string | Uint8Array>;
|
|
37
37
|
dir?: string;
|
|
38
|
+
/** Clone this repository and upload it: an https or ssh URL, or `owner/name` on GitHub. Uses this machine's git and its credentials. */
|
|
38
39
|
repo?: string;
|
|
40
|
+
/** With `repo`: the branch or tag to clone (default: the repository's default branch). */
|
|
41
|
+
ref?: string;
|
|
39
42
|
idempotencyKey?: string;
|
|
40
43
|
}
|
|
41
44
|
|
|
@@ -134,4 +137,5 @@ export class RayTrace {
|
|
|
134
137
|
|
|
135
138
|
export function buildTarGz(files: Record<string, string | Uint8Array>): Buffer;
|
|
136
139
|
export function archiveFromDirectory(dirPath: string): Buffer;
|
|
140
|
+
export function archiveFromRepo(repo: string, options?: { ref?: string }): Promise<Buffer>;
|
|
137
141
|
export default RayTrace;
|
package/src/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// @raytrace-cli/sdk — Typed high-level SDK over RayTrace's frozen /v1 API.
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
|
-
import { RayTraceClient, RayTraceError, buildTarGz, archiveFromDirectory } from './client.mjs';
|
|
3
|
+
import { RayTraceClient, RayTraceError, buildTarGz, archiveFromDirectory, archiveFromRepo, repoName } from './client.mjs';
|
|
4
4
|
|
|
5
|
-
export { RayTraceClient, RayTraceError, buildTarGz, archiveFromDirectory };
|
|
5
|
+
export { RayTraceClient, RayTraceError, buildTarGz, archiveFromDirectory, archiveFromRepo };
|
|
6
6
|
|
|
7
7
|
const TERMINAL_STATES = new Set(['completed', 'cancelled', 'timed_out', 'interrupted']);
|
|
8
8
|
|
|
@@ -117,7 +117,7 @@ class SandboxesResource {
|
|
|
117
117
|
return found;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
async create({ name = undefined, size = 'small', archive = undefined, files = undefined, dir = undefined, repo = undefined, idempotencyKey = undefined } = {}) {
|
|
120
|
+
async create({ name = undefined, size = 'small', archive = undefined, files = undefined, dir = undefined, repo = undefined, ref = undefined, idempotencyKey = undefined } = {}) {
|
|
121
121
|
let payload = archive;
|
|
122
122
|
if (!payload) {
|
|
123
123
|
if (files) {
|
|
@@ -125,12 +125,12 @@ class SandboxesResource {
|
|
|
125
125
|
} else if (dir) {
|
|
126
126
|
payload = archiveFromDirectory(dir);
|
|
127
127
|
} else if (repo) {
|
|
128
|
-
payload =
|
|
128
|
+
payload = await archiveFromRepo(repo, { ref });
|
|
129
129
|
} else {
|
|
130
130
|
throw new Error('Provide one of { archive, files, dir, repo } to create a sandbox');
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
-
const derivedName = name || (repo ?
|
|
133
|
+
const derivedName = name || (repo ? repoName(repo) : 'sandbox');
|
|
134
134
|
const cleanName = String(derivedName).replace(/[^A-Za-z0-9._ -]/g, '-').slice(0, 100) || 'sandbox';
|
|
135
135
|
const headers = {
|
|
136
136
|
'Content-Type': 'application/gzip',
|