mirror of
https://github.com/nestriness/nestri.git
synced 2026-08-02 01:35:18 +03:00
feat: Add a lot of stuff
This commit is contained in:
34
packages/sdk/.gitignore
vendored
Normal file
34
packages/sdk/.gitignore
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
# dependencies (bun install)
|
||||
node_modules
|
||||
|
||||
# output
|
||||
out
|
||||
dist
|
||||
*.tgz
|
||||
|
||||
# code coverage
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# logs
|
||||
logs
|
||||
_.log
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# caches
|
||||
.eslintcache
|
||||
.cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
15
packages/sdk/README.md
Normal file
15
packages/sdk/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# sdk
|
||||
|
||||
To install dependencies:
|
||||
|
||||
```bash
|
||||
bun install
|
||||
```
|
||||
|
||||
To run:
|
||||
|
||||
```bash
|
||||
bun run index.ts
|
||||
```
|
||||
|
||||
This project was created using `bun init` in bun v1.2.23. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
||||
22
packages/sdk/examples/test.ts
Normal file
22
packages/sdk/examples/test.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { session } from "../src/index.js";
|
||||
|
||||
const myTestSession = session({
|
||||
id: "test-session",
|
||||
description: "A game test session",
|
||||
resolution: "1080x1920",
|
||||
videoBitDepth: 8,
|
||||
machine: "gpu-medium",
|
||||
videoCodec: "h264",
|
||||
framerate: 60,
|
||||
audioBitrate: 128,
|
||||
gpuCardPath: "/dev/dri/renderD128",
|
||||
|
||||
run: async (payload) => {
|
||||
console.log("Running test session with payload:", payload);
|
||||
return { message: "Test session completed" };
|
||||
},
|
||||
});
|
||||
|
||||
myTestSession.start().then((result) => {
|
||||
console.log("Session result:", result);
|
||||
});
|
||||
18
packages/sdk/package.json
Normal file
18
packages/sdk/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@nestri/sdk",
|
||||
"module": "src/index.ts",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@hey-api/openapi-ts": "^0.86.2",
|
||||
"@types/bun": "catalog:",
|
||||
"@types/node": "catalog:"
|
||||
},
|
||||
"exports": {
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5",
|
||||
"@nestri/core": "workspace:^"
|
||||
}
|
||||
}
|
||||
0
packages/sdk/src/api/index.ts
Normal file
0
packages/sdk/src/api/index.ts
Normal file
217
packages/sdk/src/index.ts
Normal file
217
packages/sdk/src/index.ts
Normal file
@@ -0,0 +1,217 @@
|
||||
import {
|
||||
type Session,
|
||||
type RunTypes,
|
||||
type InitOutput,
|
||||
type StartOptions,
|
||||
type inferSchemaIn,
|
||||
type SchemaParseFn,
|
||||
type SessionSchema,
|
||||
type SessionOptions,
|
||||
type SessionWithSchema,
|
||||
type SessionWithSchemaOptions,
|
||||
type RunHandleFromTypes,
|
||||
type AnyRunTypes,
|
||||
type SessionOptionsWithSchema,
|
||||
getSchemaParseFn,
|
||||
SessionRunPromise,
|
||||
} from "@nestri/core/session/index";
|
||||
|
||||
// Overload: when payloadSchema is provided, payload type should be any
|
||||
export function createRun<
|
||||
TIdentifier extends string,
|
||||
TOutput = unknown,
|
||||
TInitOutput extends InitOutput = any,
|
||||
>(
|
||||
params: SessionOptionsWithSchema<TIdentifier, TOutput, TInitOutput>,
|
||||
): Session<TIdentifier, any, TOutput>;
|
||||
|
||||
// Overload: normal case without payloadSchema
|
||||
export function createRun<
|
||||
TIdentifier extends string,
|
||||
TInput = void,
|
||||
TOutput = unknown,
|
||||
TInitOutput extends InitOutput = any,
|
||||
>(
|
||||
params: SessionOptions<TIdentifier, TInput, TOutput, TInitOutput>,
|
||||
): Session<TIdentifier, TInput, TOutput>;
|
||||
|
||||
export function createRun<
|
||||
TIdentifier extends string,
|
||||
TInput = void,
|
||||
TOutput = unknown,
|
||||
TInitOutput extends InitOutput = any,
|
||||
>(
|
||||
params:
|
||||
| SessionOptions<TIdentifier, TInput, TOutput, TInitOutput>
|
||||
| SessionOptionsWithSchema<TIdentifier, TOutput, TInitOutput>,
|
||||
): Session<TIdentifier, TInput, TOutput> | Session<TIdentifier, any, TOutput> {
|
||||
const session: Session<TIdentifier, TInput, TOutput> = {
|
||||
id: params.id,
|
||||
description: params.description,
|
||||
jsonSchema: params.jsonSchema,
|
||||
start: async (payload, options) => {
|
||||
return await run_internal<RunTypes<TIdentifier, TInput, TOutput>>(
|
||||
"run()",
|
||||
params.id,
|
||||
payload,
|
||||
undefined,
|
||||
{
|
||||
queue: params.queue?.name,
|
||||
...options,
|
||||
},
|
||||
);
|
||||
},
|
||||
startAndWait: (payload, options) => {
|
||||
return new SessionRunPromise<TIdentifier, TOutput>((resolve, reject) => {
|
||||
triggerAndWait_internal<TIdentifier, TInput, TOutput>(
|
||||
"triggerAndWait()",
|
||||
params.id,
|
||||
payload,
|
||||
undefined,
|
||||
{
|
||||
queue: params.queue?.name,
|
||||
...options,
|
||||
},
|
||||
)
|
||||
.then((result) => {
|
||||
resolve(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
}, params.id);
|
||||
},
|
||||
};
|
||||
|
||||
registerTaskLifecycleHooks(params.id, params);
|
||||
|
||||
resourceCatalog.registerTaskMetadata({
|
||||
id: params.id,
|
||||
description: params.description,
|
||||
queue: params.queue,
|
||||
retry: params.retry
|
||||
? { ...defaultRetryOptions, ...params.retry }
|
||||
: undefined,
|
||||
machine:
|
||||
typeof params.machine === "string"
|
||||
? { preset: params.machine }
|
||||
: params.machine,
|
||||
maxDuration: params.maxDuration,
|
||||
payloadSchema: params.jsonSchema,
|
||||
fns: {
|
||||
run: params.run,
|
||||
},
|
||||
});
|
||||
|
||||
const queue = params.queue;
|
||||
|
||||
if (queue && typeof queue.name === "string") {
|
||||
resourceCatalog.registerQueueMetadata({
|
||||
name: queue.name,
|
||||
concurrencyLimit: queue.concurrencyLimit,
|
||||
});
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
session[Symbol.for("nestri/session")] = true;
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export function createSchemaTask<
|
||||
TIdentifier extends string,
|
||||
TSchema extends SessionSchema | undefined = undefined,
|
||||
TOutput = unknown,
|
||||
TInitOutput extends InitOutput = any,
|
||||
>(
|
||||
params: SessionWithSchemaOptions<TIdentifier, TSchema, TOutput, TInitOutput>,
|
||||
): SessionWithSchema<TIdentifier, TSchema, TOutput> {
|
||||
const parsePayload = params.schema
|
||||
? getSchemaParseFn<inferSchemaIn<TSchema>>(params.schema)
|
||||
: undefined;
|
||||
|
||||
const session: SessionWithSchema<TIdentifier, TSchema, TOutput> = {
|
||||
id: params.id,
|
||||
description: params.description,
|
||||
schema: params.schema,
|
||||
start: async (payload, options, requestOptions) => {
|
||||
return await run_internal<
|
||||
RunTypes<TIdentifier, inferSchemaIn<TSchema>, TOutput>
|
||||
>(
|
||||
"run()",
|
||||
params.id,
|
||||
payload,
|
||||
parsePayload,
|
||||
{
|
||||
queue: params.queue?.name,
|
||||
...options,
|
||||
},
|
||||
requestOptions,
|
||||
);
|
||||
},
|
||||
startAndWait: (payload, options) => {
|
||||
return new SessionRunPromise<TIdentifier, TOutput>((resolve, reject) => {
|
||||
triggerAndWait_internal<TIdentifier, inferSchemaIn<TSchema>, TOutput>(
|
||||
"triggerAndWait()",
|
||||
params.id,
|
||||
payload,
|
||||
parsePayload,
|
||||
{
|
||||
queue: params.queue?.name,
|
||||
...options,
|
||||
},
|
||||
)
|
||||
.then((result) => {
|
||||
resolve(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
}, params.id);
|
||||
},
|
||||
};
|
||||
|
||||
registerTaskLifecycleHooks(params.id, params);
|
||||
|
||||
resourceCatalog.registerTaskMetadata({
|
||||
id: params.id,
|
||||
description: params.description,
|
||||
queue: params.queue,
|
||||
retry: params.retry
|
||||
? { ...defaultRetryOptions, ...params.retry }
|
||||
: undefined,
|
||||
machine: { preset: params.machine },
|
||||
maxDuration: params.maxDuration,
|
||||
fns: {
|
||||
run: params.run,
|
||||
parsePayload,
|
||||
},
|
||||
schema: params.schema,
|
||||
});
|
||||
|
||||
const queue = params.queue;
|
||||
|
||||
if (queue && typeof queue.name === "string") {
|
||||
resourceCatalog.registerQueueMetadata({
|
||||
name: queue.name,
|
||||
concurrencyLimit: queue.concurrencyLimit,
|
||||
});
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
session[Symbol.for("nestri/session")] = true;
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export const session = createRun;
|
||||
export const schemaSession = createSchemaTask;
|
||||
|
||||
async function run_internal<TRunTypes extends AnyRunTypes>(
|
||||
name: string,
|
||||
id: TRunTypes["sessionIdentifier"],
|
||||
payload: TRunTypes["payload"],
|
||||
parsePayload?: SchemaParseFn<TRunTypes["payload"]>,
|
||||
options?: StartOptions,
|
||||
requestOptions?: any, //TriggerApiRequestOptions,
|
||||
): Promise<RunHandleFromTypes<TRunTypes>> {}
|
||||
42
packages/sdk/src/script/build.ts
Normal file
42
packages/sdk/src/script/build.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env bun
|
||||
//TODO: Fix this to import JSON
|
||||
|
||||
const dir = new URL("..", import.meta.url).pathname;
|
||||
process.chdir(dir);
|
||||
|
||||
import { $ } from "bun";
|
||||
import path from "path";
|
||||
|
||||
import { createClient } from "@hey-api/openapi-ts";
|
||||
|
||||
await $`bun dev generate > ${dir}/openapi.json`.cwd(
|
||||
path.resolve(dir, "../../opencode"),
|
||||
);
|
||||
|
||||
await createClient({
|
||||
input: "./openapi.json",
|
||||
output: {
|
||||
path: "./src/gen",
|
||||
tsConfigPath: path.join(dir, "tsconfig.json"),
|
||||
},
|
||||
plugins: [
|
||||
{
|
||||
name: "@hey-api/typescript",
|
||||
exportFromIndex: false,
|
||||
},
|
||||
{
|
||||
name: "@hey-api/sdk",
|
||||
instance: "OpencodeClient",
|
||||
exportFromIndex: false,
|
||||
auth: false,
|
||||
},
|
||||
{
|
||||
name: "@hey-api/client-fetch",
|
||||
exportFromIndex: false,
|
||||
baseUrl: "http://localhost:4096",
|
||||
},
|
||||
],
|
||||
});
|
||||
await $`bun prettier --write src/gen`;
|
||||
await $`rm -rf dist`;
|
||||
await $`bun tsc`;
|
||||
9
packages/sdk/tsconfig.build.json
Normal file
9
packages/sdk/tsconfig.build.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "@nestri/core/tsconfig.base",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"sourceMap": true,
|
||||
"stripInternal": true,
|
||||
"isolatedDeclarations": false
|
||||
}
|
||||
}
|
||||
10
packages/sdk/tsconfig.json
Normal file
10
packages/sdk/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "@nestri/core/tsconfig.base",
|
||||
"compilerOptions": {
|
||||
"isolatedDeclarations": false,
|
||||
"composite": true,
|
||||
"sourceMap": true,
|
||||
"stripInternal": true
|
||||
},
|
||||
"include": ["src/globals.d.ts", "./src/**/*.ts", "tsup.config.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user