@vite-hub/runtime 0.0.0 → 0.0.1

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 CHANGED
@@ -1,3 +1,51 @@
1
1
  # @vite-hub/runtime
2
2
 
3
- Reserved package name for ViteHub.
3
+ <p>
4
+ <a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
5
+ <img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
6
+ <img alt="Runtime" src="https://img.shields.io/badge/Runtime-context%20%7C%20policy%20%7C%20trace-18181b?style=flat-square">
7
+ </p>
8
+
9
+ `@vite-hub/runtime` shares host context, capability handles, policy decisions, approvals, traces, and leases across packages.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pnpm add @vite-hub/runtime
15
+ ```
16
+
17
+ ## Minimal API
18
+
19
+ ```ts
20
+ // server/utils/runtime-context.ts
21
+ import {
22
+ createExecutionContext,
23
+ defineCapability,
24
+ getCapability,
25
+ resolveCapabilityPolicy,
26
+ } from "@vite-hub/runtime"
27
+
28
+ const context = createExecutionContext({
29
+ runtime: "nitro",
30
+ memo: (key, create) => create(),
31
+ waitUntil: task => task.catch(() => {}),
32
+ capabilities: {
33
+ kv: defineCapability("kv", {
34
+ get: async (_key: string) => null,
35
+ }),
36
+ },
37
+ })
38
+
39
+ const kv = getCapability(context, "kv")
40
+
41
+ const decision = await resolveCapabilityPolicy("require-approval", {
42
+ capability: kv.name,
43
+ operation: "write",
44
+ })
45
+ ```
46
+
47
+ ## Used by
48
+
49
+ Feature packages use Runtime Capability handles instead of passing every provider client through every API. Agent Capabilities consume these handles when they expose KV, Blob, DB, sandbox, shell, or workspace behavior.
50
+
51
+ Learn more at [vitehub.dev](https://vitehub.dev).
@@ -0,0 +1,129 @@
1
+ //#region src/index.d.ts
2
+ type MaybePromise<T> = T | Promise<T>;
3
+ type RuntimeWaitUntil = (task: Promise<unknown>) => void;
4
+ interface RuntimeWaitUntilController {
5
+ flushWaitUntil(): Promise<void>;
6
+ waitUntil: RuntimeWaitUntil;
7
+ }
8
+ interface TraceContext {
9
+ id: string;
10
+ parentId?: string;
11
+ sampled?: boolean;
12
+ }
13
+ interface TraceEvent {
14
+ attributes?: Record<string, unknown>;
15
+ name: string;
16
+ timestamp?: Date | string;
17
+ trace?: TraceContext;
18
+ type: "approval" | "capability" | "error" | "lifecycle" | "policy" | "run";
19
+ }
20
+ interface RuntimeHostContext<TRuntimeConfig = Record<string, unknown>> {
21
+ capabilities?: RuntimeCapabilities;
22
+ cloudflare?: {
23
+ context?: unknown;
24
+ durableObjectStateName?: string;
25
+ env?: Record<string, unknown>;
26
+ };
27
+ event?: unknown;
28
+ memo<T>(key: string, create: () => T): T;
29
+ platform?: string;
30
+ request?: Request;
31
+ runtime: string;
32
+ runtimeConfig?: TRuntimeConfig;
33
+ trace?: TraceContext;
34
+ vercel?: {
35
+ waitUntil?: RuntimeWaitUntil;
36
+ };
37
+ flushWaitUntil?: () => Promise<void>;
38
+ waitUntil: RuntimeWaitUntil;
39
+ }
40
+ type ExecutionContext<TRuntimeConfig = Record<string, unknown>> = RuntimeHostContext<TRuntimeConfig> & {
41
+ runtimeConfig: TRuntimeConfig;
42
+ };
43
+ type ResolvedRuntimeHostContext<TRuntimeConfig = Record<string, unknown>> = ExecutionContext<TRuntimeConfig>;
44
+ interface CapabilityHandle<TKind extends string = string, TValue = unknown> {
45
+ kind: TKind;
46
+ name?: string;
47
+ value: TValue;
48
+ }
49
+ type RuntimeCapabilityHandle<TKind extends string = string, TValue = unknown> = CapabilityHandle<TKind, TValue>;
50
+ type RuntimeCapabilities = Record<string, CapabilityHandle | unknown>;
51
+ interface CapabilityDefinition<TKind extends string = string, TValue = unknown> {
52
+ handle: CapabilityHandle<TKind, TValue>;
53
+ resolve(context: ExecutionContext): MaybePromise<CapabilityHandle<TKind, TValue>>;
54
+ }
55
+ interface Resolvable<T, TContext extends RuntimeHostContext<any> = RuntimeHostContext> {
56
+ resolve(context: TContext): MaybePromise<T>;
57
+ }
58
+ type MaybeResolvable<T, TContext extends RuntimeHostContext<any> = RuntimeHostContext> = T | Resolvable<T, TContext> | ((context: TContext) => MaybePromise<T>);
59
+ interface PolicyContext {
60
+ capability?: string;
61
+ input?: unknown;
62
+ operation?: string;
63
+ runtime?: string;
64
+ subject?: unknown;
65
+ trace?: TraceContext;
66
+ }
67
+ type PolicyDecision = "allow" | "deny" | "require-approval" | "retryable-failure";
68
+ type CapabilityPolicy = PolicyDecision | ((context: PolicyContext) => MaybePromise<PolicyDecision>);
69
+ type ApprovalState = "proposed" | "awaiting-approval" | "approved" | "denied" | "executing" | "completed" | "failed";
70
+ interface ApprovalRequest<TInput = unknown> {
71
+ id: string;
72
+ capability?: string;
73
+ input?: TInput;
74
+ reason?: string;
75
+ state: Extract<ApprovalState, "awaiting-approval" | "proposed">;
76
+ trace?: TraceContext;
77
+ }
78
+ interface ApprovalDecision {
79
+ approved: boolean;
80
+ decidedAt?: Date | string;
81
+ reason?: string;
82
+ requestId: string;
83
+ state: Extract<ApprovalState, "approved" | "denied">;
84
+ }
85
+ interface RunLifecycleHooks<TContext extends RuntimeHostContext<any> = RuntimeHostContext> {
86
+ approval?: (request: ApprovalRequest, context: TContext) => MaybePromise<void>;
87
+ error?: (error: unknown, context: TContext) => MaybePromise<void>;
88
+ finish?: (context: TContext) => MaybePromise<void>;
89
+ request?: (context: TContext) => MaybePromise<void>;
90
+ trace?: (event: TraceEvent, context: TContext) => MaybePromise<void>;
91
+ }
92
+ interface Lease {
93
+ expiresAt?: Date | string;
94
+ id: string;
95
+ key: string;
96
+ release(): MaybePromise<void>;
97
+ }
98
+ interface LeaseStore {
99
+ acquire(key: string, options?: {
100
+ owner?: string;
101
+ ttl?: number;
102
+ }): MaybePromise<Lease>;
103
+ }
104
+ declare class CapabilityNotFoundError extends Error {
105
+ constructor(name: string);
106
+ }
107
+ declare class CapabilityDeniedError extends Error {
108
+ constructor(name: string, reason?: string);
109
+ }
110
+ declare class ApprovalRequiredError<TInput = unknown> extends Error {
111
+ request: ApprovalRequest<TInput>;
112
+ constructor(request: ApprovalRequest<TInput>);
113
+ }
114
+ declare function createExecutionContext<TRuntimeConfig = Record<string, unknown>, TContext extends RuntimeHostContext<TRuntimeConfig> = RuntimeHostContext<TRuntimeConfig>>(context: TContext): TContext & ExecutionContext<TRuntimeConfig>;
115
+ declare function resolveExecutionContext<TRuntimeConfig = Record<string, unknown>, TContext extends RuntimeHostContext<TRuntimeConfig> = RuntimeHostContext<TRuntimeConfig>>(context: TContext): TContext & ExecutionContext<TRuntimeConfig>;
116
+ declare function resolveRuntimeContext<TRuntimeConfig = Record<string, unknown>, TContext extends RuntimeHostContext<TRuntimeConfig> = RuntimeHostContext<TRuntimeConfig>>(context: TContext): TContext & ExecutionContext<TRuntimeConfig>;
117
+ declare function defineCapability<TKind extends string, TValue>(kind: TKind, value: TValue, options?: {
118
+ name?: string;
119
+ }): CapabilityHandle<TKind, TValue>;
120
+ declare function createRuntimeWaitUntilController(options?: {
121
+ forward?: RuntimeWaitUntil;
122
+ }): RuntimeWaitUntilController;
123
+ declare function hasCapability(context: RuntimeHostContext, name: string): boolean;
124
+ declare function getCapability<TKind extends string = string, TValue = unknown>(context: RuntimeHostContext, name: string): CapabilityHandle<TKind, TValue>;
125
+ declare function isResolvable<T, TContext extends RuntimeHostContext<any>>(value: MaybeResolvable<T, TContext>): value is Resolvable<T, TContext>;
126
+ declare function resolveRuntimeValue<T, TContext extends RuntimeHostContext<any>>(value: MaybeResolvable<T, TContext>, context: TContext): Promise<T>;
127
+ declare function resolveCapabilityPolicy(policy: CapabilityPolicy | undefined, context: PolicyContext): Promise<PolicyDecision>;
128
+ //#endregion
129
+ export { ApprovalDecision, ApprovalRequest, ApprovalRequiredError, ApprovalState, CapabilityDefinition, CapabilityDeniedError, CapabilityHandle, CapabilityNotFoundError, CapabilityPolicy, ExecutionContext, Lease, LeaseStore, MaybePromise, MaybeResolvable, PolicyContext, PolicyDecision, Resolvable, ResolvedRuntimeHostContext, RunLifecycleHooks, RuntimeCapabilities, RuntimeCapabilityHandle, RuntimeHostContext, RuntimeWaitUntil, RuntimeWaitUntilController, TraceContext, TraceEvent, createExecutionContext, createRuntimeWaitUntilController, defineCapability, getCapability, hasCapability, isResolvable, resolveCapabilityPolicy, resolveExecutionContext, resolveRuntimeContext, resolveRuntimeValue };
package/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ //#region src/index.ts
2
+ var CapabilityNotFoundError = class extends Error {
3
+ constructor(name) {
4
+ super(`[vitehub:runtime] Capability "${name}" was not found.`);
5
+ this.name = "CapabilityNotFoundError";
6
+ }
7
+ };
8
+ var CapabilityDeniedError = class extends Error {
9
+ constructor(name, reason) {
10
+ super(`[vitehub:runtime] Capability "${name}" was denied${reason ? `: ${reason}` : "."}`);
11
+ this.name = "CapabilityDeniedError";
12
+ }
13
+ };
14
+ var ApprovalRequiredError = class extends Error {
15
+ request;
16
+ constructor(request) {
17
+ super(`[vitehub:runtime] Approval is required for "${request.capability || request.id}".`);
18
+ this.name = "ApprovalRequiredError";
19
+ this.request = request;
20
+ }
21
+ };
22
+ function createExecutionContext(context) {
23
+ return resolveExecutionContext(context);
24
+ }
25
+ function resolveExecutionContext(context) {
26
+ return {
27
+ ...context,
28
+ capabilities: context.capabilities || {},
29
+ runtimeConfig: context.runtimeConfig || {}
30
+ };
31
+ }
32
+ function resolveRuntimeContext(context) {
33
+ return {
34
+ ...context,
35
+ runtimeConfig: context.runtimeConfig || {}
36
+ };
37
+ }
38
+ function defineCapability(kind, value, options = {}) {
39
+ return {
40
+ kind,
41
+ name: options.name,
42
+ value
43
+ };
44
+ }
45
+ function createRuntimeWaitUntilController(options = {}) {
46
+ const pending = [];
47
+ return {
48
+ async flushWaitUntil() {
49
+ while (pending.length > 0) await Promise.all(pending.splice(0));
50
+ },
51
+ waitUntil(task) {
52
+ pending.push(task);
53
+ options.forward?.(task);
54
+ }
55
+ };
56
+ }
57
+ function isCapabilityHandle(value) {
58
+ return typeof value === "object" && value !== null && "kind" in value && typeof value.kind === "string" && "value" in value;
59
+ }
60
+ function hasCapability(context, name) {
61
+ return !!context.capabilities && name in context.capabilities;
62
+ }
63
+ function getCapability(context, name) {
64
+ const value = context.capabilities?.[name];
65
+ if (value === void 0) throw new CapabilityNotFoundError(name);
66
+ if (isCapabilityHandle(value)) return value;
67
+ return defineCapability(name, value, { name });
68
+ }
69
+ function isResolvable(value) {
70
+ return typeof value === "object" && value !== null && "resolve" in value && typeof value.resolve === "function";
71
+ }
72
+ async function resolveRuntimeValue(value, context) {
73
+ if (isResolvable(value)) return await value.resolve(context);
74
+ if (typeof value === "function") return await value(context);
75
+ return value;
76
+ }
77
+ async function resolveCapabilityPolicy(policy, context) {
78
+ if (!policy) return "allow";
79
+ return typeof policy === "function" ? await policy(context) : policy;
80
+ }
81
+ //#endregion
82
+ export { ApprovalRequiredError, CapabilityDeniedError, CapabilityNotFoundError, createExecutionContext, createRuntimeWaitUntilController, defineCapability, getCapability, hasCapability, isResolvable, resolveCapabilityPolicy, resolveExecutionContext, resolveRuntimeContext, resolveRuntimeValue };
package/package.json CHANGED
@@ -1,9 +1,45 @@
1
1
  {
2
2
  "name": "@vite-hub/runtime",
3
- "version": "0.0.0",
4
- "description": "Reserved package name for ViteHub.",
3
+ "version": "0.0.1",
4
+ "description": "Shared runtime context, capability, policy, approval, and tracing primitives for ViteHub.",
5
+ "author": "onmax <maximogarciamtnez@gmail.com>",
5
6
  "license": "Apache-2.0",
6
- "publishConfig": {
7
- "access": "public"
7
+ "homepage": "https://github.com/vite-hub/vitehub#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/vite-hub/vitehub.git",
11
+ "directory": "packages/runtime"
12
+ },
13
+ "bugs": "https://github.com/vite-hub/vitehub/issues",
14
+ "keywords": [
15
+ "vitehub",
16
+ "runtime",
17
+ "capabilities",
18
+ "approvals",
19
+ "policy"
20
+ ],
21
+ "sideEffects": false,
22
+ "type": "module",
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": "./dist/index.js",
26
+ "./package.json": "./package.json"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "package.json"
31
+ ],
32
+ "devDependencies": {
33
+ "@types/node": "^25.6.0",
34
+ "publint": "^0.3.15",
35
+ "tsdown": "^0.21.8",
36
+ "typescript": "^6.0.2",
37
+ "vitest": "^4.1.4"
38
+ },
39
+ "scripts": {
40
+ "build": "tsdown --tsconfig tsconfig.build.json",
41
+ "dev": "tsdown --watch",
42
+ "typecheck": "tsc --noEmit -p ./tsconfig.json",
43
+ "test": "pnpm --filter @vite-hub/runtime build && vitest run"
8
44
  }
9
- }
45
+ }