@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.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/ARCHITECTURE.md +43 -0
- package/LICENSE +202 -0
- package/README.md +142 -2
- package/assets/openllm-light.svg +18 -0
- package/assets/openllm.svg +18 -0
- package/client/chat.ts +140 -0
- package/client/devices.ts +269 -0
- package/client/http.ts +72 -0
- package/client/index.ts +129 -0
- package/client/relay-mux.ts +547 -0
- package/client/types.ts +51 -0
- package/client/vault.ts +239 -0
- package/connect/chat.ts +58 -0
- package/connect/client.ts +240 -0
- package/connect/config.ts +47 -0
- package/connect/errors.ts +121 -0
- package/connect/index.ts +23 -0
- package/connect/oauth.ts +265 -0
- package/connect/routes.ts +234 -0
- package/connect/store.ts +107 -0
- package/mcp/handle.ts +24 -0
- package/mcp/index.ts +76 -0
- package/mcp/server.ts +319 -0
- package/mcp/tools/marketplace.ts +439 -0
- package/mcp/tools/memory-client.ts +297 -0
- package/mcp/tools/memory-config.ts +32 -0
- package/mcp/tools/memory.ts +369 -0
- package/mcp/tools/model-priority.ts +88 -0
- package/mcp/tools/openllm.ts +883 -0
- package/mcp/types.ts +28 -0
- package/openapi/call.ts +90 -0
- package/openapi/index.ts +8 -0
- package/openapi/openapi.json +80213 -0
- package/openapi/operations.ts +2075 -0
- package/package.json +39 -4
- package/protocol/CONTRIBUTING.md +21 -0
- package/protocol/README.md +134 -0
- package/protocol/access.ts +186 -0
- package/protocol/agent-prompt-text.ts +32 -0
- package/protocol/agent.ts +991 -0
- package/protocol/api-key-syntax.ts +105 -0
- package/protocol/assets/openllm-light.svg +18 -0
- package/protocol/assets/openllm.svg +18 -0
- package/protocol/audio.ts +29 -0
- package/protocol/auth.ts +142 -0
- package/protocol/changelog.ts +155 -0
- package/protocol/chat.ts +458 -0
- package/protocol/client-registry.ts +356 -0
- package/protocol/config.ts +345 -0
- package/protocol/control-channel.ts +115 -0
- package/protocol/cooldown-reason.ts +78 -0
- package/protocol/cost.ts +10 -0
- package/protocol/daemon-port.ts +31 -0
- package/protocol/daemon.ts +1524 -0
- package/protocol/default-chains.ts +66 -0
- package/protocol/device-state.ts +85 -0
- package/protocol/doctor-report-local.ts +443 -0
- package/protocol/doctor-report-policy.ts +119 -0
- package/protocol/doctor-report.ts +947 -0
- package/protocol/errors.ts +14 -0
- package/protocol/execution-selection.ts +141 -0
- package/protocol/hop-grant.ts +33 -0
- package/protocol/image-edit-parse.ts +263 -0
- package/protocol/images.ts +298 -0
- package/protocol/index.ts +74 -0
- package/protocol/inference-headers.ts +3 -0
- package/protocol/keepalive.ts +69 -0
- package/protocol/local-auth.ts +300 -0
- package/protocol/local-device-control.ts +133 -0
- package/protocol/local-provider.ts +288 -0
- package/protocol/local-session.ts +118 -0
- package/protocol/marketplace.ts +187 -0
- package/protocol/media-attempt.ts +81 -0
- package/protocol/media-bytes.ts +28 -0
- package/protocol/media-default.ts +357 -0
- package/protocol/media-dimensions.ts +132 -0
- package/protocol/media-input.ts +61 -0
- package/protocol/media-model.ts +142 -0
- package/protocol/media-option-issue.ts +90 -0
- package/protocol/media-types.ts +70 -0
- package/protocol/model-match.ts +1051 -0
- package/protocol/model-settings.ts +327 -0
- package/protocol/models.ts +767 -0
- package/protocol/mux.ts +722 -0
- package/protocol/plan-surface.ts +59 -0
- package/protocol/provider-models.ts +16 -0
- package/protocol/provider-status.ts +206 -0
- package/protocol/provider-tier.ts +67 -0
- package/protocol/provider-usage.ts +263 -0
- package/protocol/providers.ts +901 -0
- package/protocol/public-usage-index.ts +354 -0
- package/protocol/realtime-generated.ts +31 -0
- package/protocol/realtime.ts +324 -0
- package/protocol/registry-pointer.ts +118 -0
- package/protocol/relay.ts +702 -0
- package/protocol/replay-session.ts +11 -0
- package/protocol/responses.ts +280 -0
- package/protocol/semver.ts +182 -0
- package/protocol/setup-mode.ts +22 -0
- package/protocol/sha256-hex.ts +98 -0
- package/protocol/stats.ts +406 -0
- package/protocol/streaming.ts +19 -0
- package/protocol/subscription-daemon-floors.ts +33 -0
- package/protocol/subscription-provider-compat.ts +104 -0
- package/protocol/subscription-provider.ts +32 -0
- package/protocol/team-grant.ts +17 -0
- package/protocol/team-shareable.ts +61 -0
- package/protocol/transcription-encoding.ts +76 -0
- package/protocol/url-locality-dns.ts +37 -0
- package/protocol/url-locality.ts +288 -0
- package/protocol/usage-snapshots.ts +9 -0
- package/protocol/vendor-cli-install.ts +29 -0
- package/protocol/video-job-plan.ts +50 -0
- package/protocol/videos.ts +227 -0
- package/tsconfig.json +14 -0
- package/tunnel/ARCHITECTURE.md +111 -0
- package/tunnel/LICENSE +33 -0
- package/tunnel/README.md +45 -0
- package/tunnel/assets/openllm-light.svg +18 -0
- package/tunnel/assets/openllm.svg +18 -0
- package/tunnel/channel-envelope.ts +139 -0
- package/tunnel/codec.ts +191 -0
- package/tunnel/device-anchor.ts +229 -0
- package/tunnel/device-control.ts +118 -0
- package/tunnel/device-grant.ts +173 -0
- package/tunnel/fleet.ts +77 -0
- package/tunnel/index.ts +22 -0
- package/tunnel/mux.ts +580 -0
- package/tunnel/peer-link.ts +390 -0
- package/tunnel/rtc-auth.ts +415 -0
- package/tunnel/rtc-duplex.ts +187 -0
- package/tunnel/rtc-ice.ts +136 -0
- package/tunnel/secure.ts +1298 -0
- package/tunnel/stream-reset-error.ts +14 -0
- package/tunnel/streams.ts +1430 -0
- package/tunnel/team-grant.ts +302 -0
- package/tunnel/wire-bytes.ts +102 -0
- package/vault/ARCHITECTURE.md +470 -0
- package/vault/LICENSE +33 -0
- package/vault/api-key.ts +190 -0
- package/vault/argon2.ts +106 -0
- package/vault/cli-auth-seal.ts +157 -0
- package/vault/cred-seal.ts +146 -0
- package/vault/credential.ts +55 -0
- package/vault/crypto.ts +210 -0
- package/vault/daemon-attest.ts +229 -0
- package/vault/derive.ts +113 -0
- package/vault/device-access.ts +87 -0
- package/vault/device-anchor.ts +185 -0
- package/vault/device-control.ts +174 -0
- package/vault/handoff.ts +396 -0
- package/vault/index.ts +21 -0
- package/vault/passkey.ts +243 -0
- package/vault/phrase.ts +65 -0
- package/vault/pin.ts +153 -0
- package/vault/public-key-seal.ts +131 -0
- package/vault/seed.ts +272 -0
- package/vault/team.ts +355 -0
- package/vault/vault-item.ts +131 -0
- package/vault/vault-migrate-digest.ts +29 -0
- package/vault/wrapped.ts +77 -0
- package/wire/ARCHITECTURE.md +90 -0
- package/wire/CONTRIBUTING.md +19 -0
- package/wire/LICENSE +33 -0
- package/wire/README.md +51 -0
- package/wire/adapters/messages/anthropic-map.ts +37 -0
- package/wire/adapters/messages/index.ts +7 -0
- package/wire/adapters/messages/reasoning-from-items.ts +38 -0
- package/wire/adapters/messages/reasoning-signature.ts +243 -0
- package/wire/adapters/messages/request.ts +574 -0
- package/wire/adapters/messages/response.ts +215 -0
- package/wire/adapters/messages/streaming.ts +1027 -0
- package/wire/adapters/messages/usage.ts +43 -0
- package/wire/adapters/responses/index.ts +3 -0
- package/wire/adapters/responses/request.ts +395 -0
- package/wire/adapters/responses/response.ts +88 -0
- package/wire/adapters/responses/streaming.ts +344 -0
- package/wire/assets/openllm-light.svg +18 -0
- package/wire/assets/openllm.svg +18 -0
- package/wire/features/account-rank.ts +166 -0
- package/wire/features/compaction/compact-request.ts +862 -0
- package/wire/features/compaction/compaction-text.ts +43 -0
- package/wire/features/context-demote.ts +47 -0
- package/wire/features/context-overflow-strategy.ts +26 -0
- package/wire/features/context-skip.ts +170 -0
- package/wire/features/conversation-affinity.ts +144 -0
- package/wire/features/max-tokens-backfill.ts +141 -0
- package/wire/features/model-caps.ts +53 -0
- package/wire/features/model-settings.ts +24 -0
- package/wire/features/output-limit-clamp.ts +64 -0
- package/wire/features/quota-gate.ts +85 -0
- package/wire/index.ts +44 -0
- package/wire/lib/attempt-outcome.ts +43 -0
- package/wire/lib/canonical/content-part.ts +38 -0
- package/wire/lib/canonical/count-tokens-capability.ts +138 -0
- package/wire/lib/canonical/encoding-select.ts +82 -0
- package/wire/lib/canonical/image-signature.ts +202 -0
- package/wire/lib/canonical/image-sse.ts +216 -0
- package/wire/lib/canonical/message.ts +43 -0
- package/wire/lib/canonical/token-estimate.ts +266 -0
- package/wire/lib/encrypted-content.ts +66 -0
- package/wire/lib/error-class.ts +154 -0
- package/wire/lib/error-envelope.ts +56 -0
- package/wire/lib/forwarded-headers.ts +180 -0
- package/wire/lib/provider-error-rules.ts +48 -0
- package/wire/lib/refusal.ts +38 -0
- package/wire/lib/responses-apply-patch.ts +104 -0
- package/wire/lib/responses-namespace.ts +53 -0
- package/wire/lib/secret-scrub.ts +101 -0
- package/wire/lib/streaming/accumulate.ts +221 -0
- package/wire/lib/streaming/finish-reason.ts +33 -0
- package/wire/lib/streaming/peek.ts +154 -0
- package/wire/lib/streaming/provider-decode.ts +259 -0
- package/wire/lib/streaming/response-stream.ts +71 -0
- package/wire/lib/streaming/sse.ts +699 -0
- package/wire/lib/streaming/strip-tool-calls.ts +95 -0
- package/wire/lib/streaming/upstream-error.ts +58 -0
- package/wire/lib/strip-subagent-isolation.ts +284 -0
- package/wire/lib/thinking-signature.ts +156 -0
- package/wire/lib/token-ruler/bpe-core.ts +426 -0
- package/wire/lib/token-ruler/index.ts +2 -0
- package/wire/lib/token-ruler/ruler.ts +79 -0
- package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
- package/wire/lib/token-ruler/vocab/claude.ts +22 -0
- package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
- package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
- package/wire/lib/tool-name-map.ts +271 -0
- package/wire/lib/tool-schema.ts +353 -0
- package/wire/lib/usage-observer.ts +125 -0
- package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
- package/wire/providers/anthropic/beta-headers.ts +165 -0
- package/wire/providers/anthropic/index.ts +10 -0
- package/wire/providers/anthropic/request.ts +607 -0
- package/wire/providers/anthropic/response.ts +155 -0
- package/wire/providers/anthropic/streaming.ts +315 -0
- package/wire/providers/anthropic/web-search-domains.ts +49 -0
- package/wire/providers/chatgpt/common.ts +136 -0
- package/wire/providers/chatgpt/index.ts +9 -0
- package/wire/providers/chatgpt/request.ts +959 -0
- package/wire/providers/chatgpt/streaming.ts +1007 -0
- package/wire/providers/chatgpt/web-search.ts +63 -0
- package/wire/providers/grok/web-search.ts +87 -0
- package/wire/providers/kimi/web-search.ts +145 -0
- package/wire/providers/upstream-deny.ts +355 -0
- package/wire/providers/upstream-request.ts +619 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client device tunnel — composition over `@openllmsh/sdk/tunnel`.
|
|
3
|
+
*
|
|
4
|
+
* Holds the RTC → relay-mux open ladder and re-exports the consumer relay-mux
|
|
5
|
+
* open path. Hosts inject relay send, grants, caps, RTC channel resolution and
|
|
6
|
+
* browser identity; the SDK never touches WebSocket/RTC globals itself.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { TMuxChannel } from "@openllmsh/sdk/tunnel/mux";
|
|
10
|
+
import { isStreamResetError } from "@openllmsh/sdk/tunnel/stream-reset-error";
|
|
11
|
+
import type { TRelayMux, TRelayMuxDependencies } from "./relay-mux";
|
|
12
|
+
import { createRelayMux } from "./relay-mux";
|
|
13
|
+
|
|
14
|
+
export type {
|
|
15
|
+
TCreateRelayMuxOptions,
|
|
16
|
+
TMuxChannelState,
|
|
17
|
+
TMuxPhase,
|
|
18
|
+
TRelayMux,
|
|
19
|
+
TRelayMuxDependencies,
|
|
20
|
+
} from "./relay-mux";
|
|
21
|
+
export { createRelayMux } from "./relay-mux";
|
|
22
|
+
|
|
23
|
+
/** Shared open budget for a session open across RTC + mux rungs. */
|
|
24
|
+
export const SESSION_OPEN_TIMEOUT_MS = 15_000;
|
|
25
|
+
/** Shorter probe budget for the RTC rung only. */
|
|
26
|
+
export const RTC_SESSION_OPEN_TIMEOUT_MS = 6_000;
|
|
27
|
+
|
|
28
|
+
const DAEMON_OPEN_VERDICTS = [
|
|
29
|
+
"pty_unsupported",
|
|
30
|
+
"sessions_disabled",
|
|
31
|
+
"cli_not_installed",
|
|
32
|
+
"session_not_found",
|
|
33
|
+
"session_busy",
|
|
34
|
+
"overloaded",
|
|
35
|
+
"spawn_failed",
|
|
36
|
+
"unauthorized",
|
|
37
|
+
"agent_unavailable",
|
|
38
|
+
"agent_busy",
|
|
39
|
+
"newer consumer",
|
|
40
|
+
] as const;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether a failed session open should be retried on the next rung (path
|
|
44
|
+
* failure, not a daemon verdict on the request).
|
|
45
|
+
*/
|
|
46
|
+
export const isTransportFailure = (error: unknown): boolean => {
|
|
47
|
+
if (isStreamResetError(error)) {
|
|
48
|
+
switch (error.code) {
|
|
49
|
+
case "pty_unsupported":
|
|
50
|
+
case "sessions_disabled":
|
|
51
|
+
case "cli_not_installed":
|
|
52
|
+
case "session_not_found":
|
|
53
|
+
case "session_busy":
|
|
54
|
+
case "overloaded":
|
|
55
|
+
case "spawn_failed":
|
|
56
|
+
case "unauthorized":
|
|
57
|
+
case "agent_unavailable":
|
|
58
|
+
case "agent_busy":
|
|
59
|
+
case "realtime_unsupported":
|
|
60
|
+
case "realtime_busy":
|
|
61
|
+
case "realtime_refused":
|
|
62
|
+
case "realtime_policy_refused":
|
|
63
|
+
return false;
|
|
64
|
+
default:
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const message = (
|
|
69
|
+
error instanceof Error ? error.message : String(error)
|
|
70
|
+
).toLowerCase();
|
|
71
|
+
return !DAEMON_OPEN_VERDICTS.some((verdict) => message.includes(verdict));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export type TResolvedMuxChannel = {
|
|
75
|
+
readonly channel: TMuxChannel;
|
|
76
|
+
readonly path: "rtc" | "mux";
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export type TSessionLadderDeps = {
|
|
80
|
+
readonly resolveMuxChannel: (
|
|
81
|
+
keyId: string,
|
|
82
|
+
) => Promise<TResolvedMuxChannel | null>;
|
|
83
|
+
readonly markRtcFailure: (keyId: string) => void;
|
|
84
|
+
readonly dropRelayMux: (keyId: string) => void;
|
|
85
|
+
readonly rtcMayArrive: (keyId: string) => boolean;
|
|
86
|
+
readonly callerSignal?: AbortSignal;
|
|
87
|
+
readonly now?: () => number;
|
|
88
|
+
readonly onDebug?: (scope: string, event: string, data: unknown) => void;
|
|
89
|
+
readonly transportUnavailableError?: (keyId: string) => Error;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const defaultUnavailable = (keyId: string): Error =>
|
|
93
|
+
new Error(
|
|
94
|
+
keyId.length > 0
|
|
95
|
+
? `device transport unavailable for ${keyId}`
|
|
96
|
+
: "device transport unavailable",
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Run a session open down the transport ladder, failing RTC over to relay mux.
|
|
101
|
+
* Behaviour matches the historical `lib/stores/daemon-store` implementation.
|
|
102
|
+
*/
|
|
103
|
+
export const openOverMuxLadder = async <R>(
|
|
104
|
+
keyId: string,
|
|
105
|
+
attempt: (
|
|
106
|
+
channel: TMuxChannel,
|
|
107
|
+
timeoutMs: number,
|
|
108
|
+
path: "rtc" | "mux",
|
|
109
|
+
) => Promise<R>,
|
|
110
|
+
deps: TSessionLadderDeps,
|
|
111
|
+
): Promise<R> => {
|
|
112
|
+
const resolve = deps.resolveMuxChannel;
|
|
113
|
+
const poison = deps.markRtcFailure;
|
|
114
|
+
const dropMux = deps.dropRelayMux;
|
|
115
|
+
const rtcPending = deps.rtcMayArrive;
|
|
116
|
+
const callerSignal = deps.callerSignal;
|
|
117
|
+
const now = deps.now ?? Date.now;
|
|
118
|
+
const dbg = deps.onDebug ?? (() => {});
|
|
119
|
+
const unavailable = deps.transportUnavailableError ?? defaultUnavailable;
|
|
120
|
+
|
|
121
|
+
let resolved = await resolve(keyId);
|
|
122
|
+
if (resolved === null) throw unavailable(keyId);
|
|
123
|
+
|
|
124
|
+
const deadline = now() + SESSION_OPEN_TIMEOUT_MS;
|
|
125
|
+
const remaining = (): number => Math.max(0, deadline - now());
|
|
126
|
+
|
|
127
|
+
for (let pass = 0; pass < 4; pass += 1) {
|
|
128
|
+
const tried = resolved.channel;
|
|
129
|
+
const isRtc = resolved.path === "rtc";
|
|
130
|
+
const budget = isRtc
|
|
131
|
+
? Math.min(RTC_SESSION_OPEN_TIMEOUT_MS, remaining())
|
|
132
|
+
: remaining();
|
|
133
|
+
dbg("ladder", "rung", {
|
|
134
|
+
key: keyId,
|
|
135
|
+
pass,
|
|
136
|
+
path: resolved.path,
|
|
137
|
+
budget_ms: budget,
|
|
138
|
+
alone: !isRtc,
|
|
139
|
+
rtc_may_arrive: rtcPending(keyId),
|
|
140
|
+
});
|
|
141
|
+
try {
|
|
142
|
+
const result = await attempt(resolved.channel, budget, resolved.path);
|
|
143
|
+
dbg("ladder", "opened", { key: keyId, pass, path: resolved.path });
|
|
144
|
+
return result;
|
|
145
|
+
} catch (error: unknown) {
|
|
146
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
147
|
+
dbg("ladder", "rung failed", {
|
|
148
|
+
key: keyId,
|
|
149
|
+
pass,
|
|
150
|
+
path: resolved.path,
|
|
151
|
+
err: why,
|
|
152
|
+
failover: isTransportFailure(error),
|
|
153
|
+
});
|
|
154
|
+
if (callerSignal?.aborted === true) {
|
|
155
|
+
dbg("ladder", "caller aborted — not a rung verdict", {
|
|
156
|
+
key: keyId,
|
|
157
|
+
pass,
|
|
158
|
+
path: resolved.path,
|
|
159
|
+
});
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
if (!isTransportFailure(error)) throw error;
|
|
163
|
+
if (isRtc) {
|
|
164
|
+
poison(keyId);
|
|
165
|
+
} else {
|
|
166
|
+
dropMux(keyId);
|
|
167
|
+
}
|
|
168
|
+
if (remaining() === 0) throw error;
|
|
169
|
+
const next = await resolve(keyId);
|
|
170
|
+
if (next === null) throw error;
|
|
171
|
+
if (next.channel === tried) throw error;
|
|
172
|
+
dbg("ladder", "switching rung", {
|
|
173
|
+
key: keyId,
|
|
174
|
+
from: resolved.path,
|
|
175
|
+
to: next.path,
|
|
176
|
+
remaining_ms: remaining(),
|
|
177
|
+
});
|
|
178
|
+
resolved = next;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
throw unavailable(keyId);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export type TClientDevice = {
|
|
185
|
+
readonly deviceId: string;
|
|
186
|
+
/**
|
|
187
|
+
* Open (or reuse) a relay-mux channel to this device. RTC upgrade stays with
|
|
188
|
+
* the host's `resolveMuxChannel` used by {@link openOverLadder}.
|
|
189
|
+
*/
|
|
190
|
+
readonly openMux: (options?: {
|
|
191
|
+
readonly timeoutMs?: number;
|
|
192
|
+
readonly signal?: AbortSignal;
|
|
193
|
+
}) => Promise<TMuxChannel | null>;
|
|
194
|
+
/** RTC → mux ladder; host supplies channel resolution via the devices host. */
|
|
195
|
+
readonly openOverLadder: <R>(
|
|
196
|
+
attempt: (
|
|
197
|
+
channel: TMuxChannel,
|
|
198
|
+
timeoutMs: number,
|
|
199
|
+
path: "rtc" | "mux",
|
|
200
|
+
) => Promise<R>,
|
|
201
|
+
overrides?: Partial<TSessionLadderDeps>,
|
|
202
|
+
) => Promise<R>;
|
|
203
|
+
readonly close: () => void;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export type TClientDevicesHost = {
|
|
207
|
+
readonly mux: TRelayMuxDependencies;
|
|
208
|
+
readonly ladder: TSessionLadderDeps;
|
|
209
|
+
/**
|
|
210
|
+
* Optional shared mux store (e.g. the browser app singleton). When omitted,
|
|
211
|
+
* {@link createClientDevices} creates a private instance.
|
|
212
|
+
*/
|
|
213
|
+
readonly relayMux?: TRelayMux;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export type TClientDevices = {
|
|
217
|
+
readonly open: (deviceId: string) => TClientDevice;
|
|
218
|
+
/** The mux store this facade uses — host glue may feed relay frames into it. */
|
|
219
|
+
readonly relayMux: TRelayMux;
|
|
220
|
+
/** Tear down every mux channel this facade owns. Idempotent. */
|
|
221
|
+
readonly close: () => void;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
export const createClientDevices = (
|
|
225
|
+
host: TClientDevicesHost,
|
|
226
|
+
): TClientDevices => {
|
|
227
|
+
const ownsMux = host.relayMux === undefined;
|
|
228
|
+
const relayMux = host.relayMux ?? createRelayMux();
|
|
229
|
+
const opened = new Set<string>();
|
|
230
|
+
return {
|
|
231
|
+
relayMux,
|
|
232
|
+
open: (deviceId: string): TClientDevice => ({
|
|
233
|
+
deviceId,
|
|
234
|
+
openMux: (options = {}): Promise<TMuxChannel | null> => {
|
|
235
|
+
opened.add(deviceId);
|
|
236
|
+
return relayMux.getOrOpenMuxChannel(
|
|
237
|
+
deviceId,
|
|
238
|
+
host.mux,
|
|
239
|
+
options.timeoutMs,
|
|
240
|
+
options.signal,
|
|
241
|
+
);
|
|
242
|
+
},
|
|
243
|
+
openOverLadder: <R>(
|
|
244
|
+
attempt: (
|
|
245
|
+
channel: TMuxChannel,
|
|
246
|
+
timeoutMs: number,
|
|
247
|
+
path: "rtc" | "mux",
|
|
248
|
+
) => Promise<R>,
|
|
249
|
+
overrides: Partial<TSessionLadderDeps> = {},
|
|
250
|
+
): Promise<R> =>
|
|
251
|
+
openOverMuxLadder(deviceId, attempt, {
|
|
252
|
+
...host.ladder,
|
|
253
|
+
...overrides,
|
|
254
|
+
}),
|
|
255
|
+
close: (): void => {
|
|
256
|
+
opened.delete(deviceId);
|
|
257
|
+
relayMux.dropChannelForKey(deviceId);
|
|
258
|
+
},
|
|
259
|
+
}),
|
|
260
|
+
close: (): void => {
|
|
261
|
+
if (ownsMux) {
|
|
262
|
+
relayMux.failAllMuxChannels("client closed");
|
|
263
|
+
} else {
|
|
264
|
+
for (const deviceId of opened) relayMux.dropChannelForKey(deviceId);
|
|
265
|
+
}
|
|
266
|
+
opened.clear();
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
};
|
package/client/http.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client HTTP surface — one path through `openapi/call.ts` `callOperation`,
|
|
3
|
+
* plus an authenticated raw `request` for streaming / non-operation URLs.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
TApiOperation,
|
|
8
|
+
THttpClientOptions,
|
|
9
|
+
TSdkResponse,
|
|
10
|
+
} from "@openllmsh/sdk/openapi";
|
|
11
|
+
import { API_OPERATIONS, callOperation } from "@openllmsh/sdk/openapi";
|
|
12
|
+
|
|
13
|
+
export type TClientHttpOptions = {
|
|
14
|
+
readonly origin: string;
|
|
15
|
+
readonly apiKey: string;
|
|
16
|
+
readonly fetch: (input: string, init?: RequestInit) => Promise<Response>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type TClientHttp = {
|
|
20
|
+
/**
|
|
21
|
+
* Typed OpenAPI call. `op` may be a `TApiOperation` or its `id`
|
|
22
|
+
* (e.g. `v1ChatCompletions.chatCompletions`).
|
|
23
|
+
*/
|
|
24
|
+
readonly call: (
|
|
25
|
+
op: TApiOperation | string,
|
|
26
|
+
args?: Record<string, unknown>,
|
|
27
|
+
options?: THttpClientOptions,
|
|
28
|
+
) => Promise<TSdkResponse>;
|
|
29
|
+
/**
|
|
30
|
+
* Authenticated raw fetch against `origin`. `path` may be absolute under
|
|
31
|
+
* the origin (`/v1/...`) or a full URL. Used for SSE streams and paths
|
|
32
|
+
* that are not (yet) in the operations table.
|
|
33
|
+
*/
|
|
34
|
+
readonly request: (path: string, init?: RequestInit) => Promise<Response>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const resolveOp = (op: TApiOperation | string): TApiOperation => {
|
|
38
|
+
if (typeof op !== "string") return op;
|
|
39
|
+
const found = API_OPERATIONS.find((candidate) => candidate.id === op);
|
|
40
|
+
if (found === undefined) {
|
|
41
|
+
throw new Error(`unknown OpenAPI operation id "${op}"`);
|
|
42
|
+
}
|
|
43
|
+
return found;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const createClientHttp = (options: TClientHttpOptions): TClientHttp => {
|
|
47
|
+
const baseUrl = options.origin.replace(/\/+$/, "");
|
|
48
|
+
const config = { baseUrl, apiKey: options.apiKey };
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
call: async (
|
|
52
|
+
op: TApiOperation | string,
|
|
53
|
+
args: Record<string, unknown> = {},
|
|
54
|
+
callOptions: THttpClientOptions = {},
|
|
55
|
+
): Promise<TSdkResponse> =>
|
|
56
|
+
callOperation(config, resolveOp(op), args, {
|
|
57
|
+
...callOptions,
|
|
58
|
+
fetch: callOptions.fetch ?? options.fetch,
|
|
59
|
+
}),
|
|
60
|
+
request: (path: string, init: RequestInit = {}): Promise<Response> => {
|
|
61
|
+
const url =
|
|
62
|
+
path.startsWith("http://") || path.startsWith("https://")
|
|
63
|
+
? path
|
|
64
|
+
: `${baseUrl}${path.startsWith("/") ? path : `/${path}`}`;
|
|
65
|
+
const headers = new Headers(init.headers);
|
|
66
|
+
if (!headers.has("authorization") && options.apiKey.length > 0) {
|
|
67
|
+
headers.set("authorization", `Bearer ${options.apiKey}`);
|
|
68
|
+
}
|
|
69
|
+
return options.fetch(url, { ...init, headers });
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
};
|
package/client/index.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openllmsh/sdk/client` — thin, I/O-injected composition facade over the SDK
|
|
3
|
+
* layers. Vault session, HTTP, and device tunnel (mux ladder + relay-mux).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { TClientChat } from "./chat";
|
|
7
|
+
import { createClientChat } from "./chat";
|
|
8
|
+
import type { TClientDevices, TClientDevicesHost } from "./devices";
|
|
9
|
+
import { createClientDevices, createRelayMux } from "./devices";
|
|
10
|
+
import type { TClientHttp } from "./http";
|
|
11
|
+
import { createClientHttp } from "./http";
|
|
12
|
+
import type { TCreateClientOptions } from "./types";
|
|
13
|
+
import type { TClientVault } from "./vault";
|
|
14
|
+
import { createClientVault } from "./vault";
|
|
15
|
+
|
|
16
|
+
export type {
|
|
17
|
+
TChatCompletionCreateBody,
|
|
18
|
+
TChatCompletions,
|
|
19
|
+
TClientChat,
|
|
20
|
+
} from "./chat";
|
|
21
|
+
export { createClientChat, DEFAULT_CHAT_TIMEOUT_MS } from "./chat";
|
|
22
|
+
export type {
|
|
23
|
+
TClientDevice,
|
|
24
|
+
TClientDevices,
|
|
25
|
+
TClientDevicesHost,
|
|
26
|
+
TCreateRelayMuxOptions,
|
|
27
|
+
TMuxChannelState,
|
|
28
|
+
TMuxPhase,
|
|
29
|
+
TRelayMux,
|
|
30
|
+
TRelayMuxDependencies,
|
|
31
|
+
TResolvedMuxChannel,
|
|
32
|
+
TSessionLadderDeps,
|
|
33
|
+
} from "./devices";
|
|
34
|
+
export {
|
|
35
|
+
createClientDevices,
|
|
36
|
+
createRelayMux,
|
|
37
|
+
isTransportFailure,
|
|
38
|
+
openOverMuxLadder,
|
|
39
|
+
RTC_SESSION_OPEN_TIMEOUT_MS,
|
|
40
|
+
SESSION_OPEN_TIMEOUT_MS,
|
|
41
|
+
} from "./devices";
|
|
42
|
+
export type { TClientHttp, TClientHttpOptions } from "./http";
|
|
43
|
+
export { createClientHttp } from "./http";
|
|
44
|
+
export type {
|
|
45
|
+
TClientTransport,
|
|
46
|
+
TCreateClientOptions,
|
|
47
|
+
TRtcFactory,
|
|
48
|
+
TWebSocketConstructor,
|
|
49
|
+
} from "./types";
|
|
50
|
+
export type {
|
|
51
|
+
TClientVault,
|
|
52
|
+
TCreateClientVaultOptions,
|
|
53
|
+
TDekBorrow,
|
|
54
|
+
} from "./vault";
|
|
55
|
+
export { createClientVault } from "./vault";
|
|
56
|
+
|
|
57
|
+
export type TOpenllmClient = {
|
|
58
|
+
readonly origin: string;
|
|
59
|
+
readonly apiKey: string;
|
|
60
|
+
readonly transport: TCreateClientOptions["transport"];
|
|
61
|
+
readonly vault: TClientVault;
|
|
62
|
+
readonly http: TClientHttp;
|
|
63
|
+
readonly chat: TClientChat;
|
|
64
|
+
/**
|
|
65
|
+
* Device tunnel facade. Bound when `createClient` is given `devices` host
|
|
66
|
+
* deps (relay send, grants, ladder resolution). Otherwise `open` throws.
|
|
67
|
+
*/
|
|
68
|
+
readonly devices: TClientDevices;
|
|
69
|
+
/** Optional `dek_for_key` carried from create options for a later unlock. */
|
|
70
|
+
readonly dekForKey: string | undefined;
|
|
71
|
+
/** Zero vault material and tear down device mux channels. Idempotent. */
|
|
72
|
+
readonly close: () => void;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const normalizeOrigin = (origin: string): string => origin.replace(/\/+$/, "");
|
|
76
|
+
|
|
77
|
+
/** Shared placeholder for clients that never bind devices (`open` always throws). */
|
|
78
|
+
const unboundRelayMux = createRelayMux();
|
|
79
|
+
const unboundDevices: TClientDevices = {
|
|
80
|
+
open: (): never => {
|
|
81
|
+
throw new Error(
|
|
82
|
+
"createClient was not given devices host deps — pass options.devices",
|
|
83
|
+
);
|
|
84
|
+
},
|
|
85
|
+
relayMux: unboundRelayMux,
|
|
86
|
+
close: (): void => {},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Build a client. Sync on purpose — unwrapping a DEK from `dek_for_key` is
|
|
91
|
+
* async and lives on `client.vault.unlockFromApiKey`. Hosts inject `fetch`,
|
|
92
|
+
* `WebSocket`, an optional RTC factory, and optional device-tunnel host deps.
|
|
93
|
+
*/
|
|
94
|
+
export const createClient = (
|
|
95
|
+
options: TCreateClientOptions & {
|
|
96
|
+
readonly devices?: TClientDevicesHost;
|
|
97
|
+
},
|
|
98
|
+
): TOpenllmClient => {
|
|
99
|
+
const origin = normalizeOrigin(options.origin);
|
|
100
|
+
const vault = createClientVault({
|
|
101
|
+
apiKey: options.apiKey,
|
|
102
|
+
...(options.dek !== undefined ? { dek: options.dek } : {}),
|
|
103
|
+
});
|
|
104
|
+
const http = createClientHttp({
|
|
105
|
+
origin,
|
|
106
|
+
apiKey: options.apiKey,
|
|
107
|
+
fetch: options.transport.fetch,
|
|
108
|
+
});
|
|
109
|
+
const chat = createClientChat(http);
|
|
110
|
+
const devices =
|
|
111
|
+
options.devices !== undefined
|
|
112
|
+
? createClientDevices(options.devices)
|
|
113
|
+
: unboundDevices;
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
origin,
|
|
117
|
+
apiKey: options.apiKey,
|
|
118
|
+
transport: options.transport,
|
|
119
|
+
vault,
|
|
120
|
+
http,
|
|
121
|
+
chat,
|
|
122
|
+
devices,
|
|
123
|
+
dekForKey: options.dekForKey,
|
|
124
|
+
close: (): void => {
|
|
125
|
+
devices.close();
|
|
126
|
+
vault.close();
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
};
|