@rigkit/provider-cmux 0.1.8

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.
@@ -0,0 +1,103 @@
1
+ import {
2
+ defineProvider,
3
+ type LocalWorkspaceRuntime,
4
+ type WorkflowProviderDefinition,
5
+ } from "@rigkit/sdk";
6
+ import type { BaseProviderPlugin, WorkflowProviderController } from "@rigkit/engine";
7
+ import {
8
+ CMUX_OPEN_CAPABILITY,
9
+ CMUX_OPEN_CAPABILITY_ID,
10
+ type CmuxOpenInput,
11
+ type CmuxOpenResult,
12
+ type CmuxOpenSession,
13
+ } from "./capabilities.ts";
14
+
15
+ export const CMUX_PROVIDER_ID = "cmux";
16
+
17
+ export type CmuxRuntime = {
18
+ open(input: CmuxOpenInput): Promise<CmuxOpenSession>;
19
+ };
20
+
21
+ export type CmuxProviderDefinition = WorkflowProviderDefinition<
22
+ typeof CMUX_PROVIDER_ID,
23
+ {},
24
+ CmuxRuntime
25
+ >;
26
+
27
+ export function provider(): CmuxProviderDefinition {
28
+ return defineProvider(CMUX_PROVIDER_ID, {}, cmuxProviderPlugin);
29
+ }
30
+
31
+ export const cmux = {
32
+ provider,
33
+ capability: CMUX_OPEN_CAPABILITY,
34
+ capabilities: {
35
+ open: CMUX_OPEN_CAPABILITY,
36
+ },
37
+ };
38
+
39
+ export const cmuxProviderPlugin: BaseProviderPlugin = {
40
+ providerId: CMUX_PROVIDER_ID,
41
+ createProvider(): WorkflowProviderController<CmuxRuntime> {
42
+ return {
43
+ providerId: CMUX_PROVIDER_ID,
44
+ runtime(context) {
45
+ return createCmuxRuntime(context.local);
46
+ },
47
+ };
48
+ },
49
+ };
50
+
51
+ function createCmuxRuntime(local: LocalWorkspaceRuntime): CmuxRuntime {
52
+ return {
53
+ async open(input) {
54
+ if (local.requestCapabilitySession) {
55
+ const session = await local.requestCapabilitySession<CmuxOpenResult>(CMUX_OPEN_CAPABILITY_ID, input);
56
+ return {
57
+ ...parseCmuxOpenResult(session.result),
58
+ closed: session.closed,
59
+ };
60
+ }
61
+ if (!local.requestCapability) {
62
+ throw new Error(`Host capability ${CMUX_OPEN_CAPABILITY_ID} is unavailable in this runtime`);
63
+ }
64
+ const result = parseCmuxOpenResult(
65
+ await local.requestCapability(CMUX_OPEN_CAPABILITY_ID, input),
66
+ );
67
+ return {
68
+ ...result,
69
+ closed: new Promise<void>(() => {}),
70
+ };
71
+ },
72
+ };
73
+ }
74
+
75
+ function parseCmuxOpenResult(value: unknown): CmuxOpenResult {
76
+ if (!isRecord(value)) throw new Error(`cmux.open returned a non-object result`);
77
+ const sessionId = stringField(value, "sessionId");
78
+ if (!sessionId) throw new Error(`cmux.open result is missing sessionId`);
79
+ const workspaceId = stringField(value, "workspaceId") ?? sessionId;
80
+ return {
81
+ sessionId,
82
+ workspaceId,
83
+ ...optionalStringField(value, "workspaceRef"),
84
+ ...optionalStringField(value, "terminalPaneId"),
85
+ ...optionalStringField(value, "terminalSurfaceId"),
86
+ ...optionalStringField(value, "browserPaneId"),
87
+ ...optionalStringField(value, "browserSurfaceId"),
88
+ };
89
+ }
90
+
91
+ function optionalStringField(record: Record<string, unknown>, key: string): Record<string, string> {
92
+ const value = stringField(record, key);
93
+ return value ? { [key]: value } : {};
94
+ }
95
+
96
+ function stringField(record: Record<string, unknown>, key: string): string | undefined {
97
+ const value = record[key];
98
+ return typeof value === "string" && value.trim() !== "" ? value : undefined;
99
+ }
100
+
101
+ function isRecord(value: unknown): value is Record<string, unknown> {
102
+ return typeof value === "object" && value !== null && !Array.isArray(value);
103
+ }
package/src/version.ts ADDED
@@ -0,0 +1 @@
1
+ export const RIGKIT_PROVIDER_CMUX_VERSION = "0.1.8";