@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.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/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,298 @@
|
|
|
1
|
+
import { Schema as S } from "effect";
|
|
2
|
+
import { OptionalMediaModel } from "./media-input";
|
|
3
|
+
import { TUNNEL_MEDIA_MAX_BODY_BYTES } from "./mux";
|
|
4
|
+
|
|
5
|
+
/** Image-edit request body bound; matches tunneled media HTTP max. */
|
|
6
|
+
export const IMAGE_EDIT_MAX_BODY_BYTES = TUNNEL_MEDIA_MAX_BODY_BYTES;
|
|
7
|
+
|
|
8
|
+
export const ImageSize = S.String;
|
|
9
|
+
export type TImageSize = S.Schema.Type<typeof ImageSize>;
|
|
10
|
+
|
|
11
|
+
/** Output container for the GPT-image families. DALL·E models reject it. */
|
|
12
|
+
export const ImageOutputFormat = S.Literals(["png", "jpeg", "webp"]);
|
|
13
|
+
export type TImageOutputFormat = S.Schema.Type<typeof ImageOutputFormat>;
|
|
14
|
+
|
|
15
|
+
/** Content-moderation level; GPT-image families only. */
|
|
16
|
+
export const ImageModeration = S.Literals(["low", "auto"]);
|
|
17
|
+
export type TImageModeration = S.Schema.Type<typeof ImageModeration>;
|
|
18
|
+
|
|
19
|
+
export const ImageGenerationRequest = S.Struct({
|
|
20
|
+
model: S.String,
|
|
21
|
+
prompt: S.String,
|
|
22
|
+
n: S.optional(S.Int.check(S.isGreaterThan(0))),
|
|
23
|
+
size: S.optional(ImageSize),
|
|
24
|
+
quality: S.optional(S.String),
|
|
25
|
+
style: S.optional(S.Literals(["vivid", "natural"])),
|
|
26
|
+
response_format: S.optional(S.Literals(["url", "b64_json"])),
|
|
27
|
+
background: S.optional(S.Literals(["transparent", "opaque", "auto"])),
|
|
28
|
+
user: S.optional(S.String),
|
|
29
|
+
/**
|
|
30
|
+
* GPT-image output controls. Previously unmodelled, so Effect's default
|
|
31
|
+
* `onExcessProperty: "ignore"` dropped them before dispatch — a caller
|
|
32
|
+
* asking `gpt-image-1` for WebP silently received PNG. Which family accepts
|
|
33
|
+
* which of these is enforced at dispatch, not here, so OpenAI-compatible
|
|
34
|
+
* vendors with their own model ids stay unvalidated and keep working.
|
|
35
|
+
*/
|
|
36
|
+
output_format: S.optional(ImageOutputFormat),
|
|
37
|
+
output_compression: S.optional(S.Int),
|
|
38
|
+
moderation: S.optional(ImageModeration),
|
|
39
|
+
/**
|
|
40
|
+
* Streaming controls. The gateway serves BOTH modes: `stream: true` answers
|
|
41
|
+
* `text/event-stream` with the spec's named events, `false`/absent answers
|
|
42
|
+
* the JSON body unchanged.
|
|
43
|
+
*
|
|
44
|
+
* `partial_images` is a provider-wide PUBLISHED request domain (0-3,
|
|
45
|
+
* default 0 — "when set to 0, the response will be a single image sent in
|
|
46
|
+
* one streaming event"), so it is validated here as structural input, not
|
|
47
|
+
* as a per-model capability table.
|
|
48
|
+
*/
|
|
49
|
+
stream: S.optional(S.Boolean),
|
|
50
|
+
partial_images: S.optional(
|
|
51
|
+
S.Int.check(S.isGreaterThanOrEqualTo(0), S.isLessThanOrEqualTo(3)),
|
|
52
|
+
),
|
|
53
|
+
});
|
|
54
|
+
export type TImageGenerationRequest = S.Schema.Type<
|
|
55
|
+
typeof ImageGenerationRequest
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
/** A named, pre-dispatch refusal for an image option we cannot serve. */
|
|
59
|
+
export type TImageRequestRejection = {
|
|
60
|
+
readonly param: string;
|
|
61
|
+
readonly message: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* SUPERSEDED REFUSAL — kept as validation, deliberately not deleted.
|
|
66
|
+
*
|
|
67
|
+
* This used to refuse `stream` / `partial_images` outright as unimplemented.
|
|
68
|
+
* That was wrong: image streaming is a universally supported gateway feature
|
|
69
|
+
* and refusing it to make a schema audit pass is not a contract. Both modes
|
|
70
|
+
* are now served.
|
|
71
|
+
*
|
|
72
|
+
* What survives is the reason the check ran on the RAW body in the first
|
|
73
|
+
* place: these fields must be TYPE-validated BEFORE the schema decode, because
|
|
74
|
+
* a malformed value would otherwise be discarded by `onExcessProperty:
|
|
75
|
+
* "ignore"` and the caller would silently get a different response mode than
|
|
76
|
+
* they asked for. Unknown fields are still NOT rejected — only these two are
|
|
77
|
+
* inspected, so vendor extensions on OpenAI-compatible endpoints keep flowing.
|
|
78
|
+
*/
|
|
79
|
+
export const assertImageRequestSupported = (
|
|
80
|
+
raw: unknown,
|
|
81
|
+
): TImageRequestRejection | null => {
|
|
82
|
+
if (raw === null || typeof raw !== "object") return null;
|
|
83
|
+
const body = raw as Record<string, unknown>;
|
|
84
|
+
if (Object.hasOwn(body, "stream")) {
|
|
85
|
+
const value = body.stream;
|
|
86
|
+
if (value !== undefined && typeof value !== "boolean") {
|
|
87
|
+
return {
|
|
88
|
+
param: "stream",
|
|
89
|
+
message: `"stream" must be a boolean; received ${JSON.stringify(value)}.`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (Object.hasOwn(body, "partial_images")) {
|
|
94
|
+
const value = body.partial_images;
|
|
95
|
+
if (
|
|
96
|
+
value !== undefined &&
|
|
97
|
+
value !== null &&
|
|
98
|
+
(typeof value !== "number" ||
|
|
99
|
+
!Number.isInteger(value) ||
|
|
100
|
+
value < 0 ||
|
|
101
|
+
value > 3)
|
|
102
|
+
) {
|
|
103
|
+
return {
|
|
104
|
+
param: "partial_images",
|
|
105
|
+
message: `"partial_images" must be an integer between 0 and 3; received ${JSON.stringify(value)}.`,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const { model: _imageGenerationModel, ...imageGenerationInputFields } =
|
|
113
|
+
ImageGenerationRequest.fields;
|
|
114
|
+
export const ImageGenerationInput = S.Struct({
|
|
115
|
+
...imageGenerationInputFields,
|
|
116
|
+
model: OptionalMediaModel,
|
|
117
|
+
});
|
|
118
|
+
export type TImageGenerationInput = S.Schema.Type<typeof ImageGenerationInput>;
|
|
119
|
+
|
|
120
|
+
const ImageData = S.Struct({
|
|
121
|
+
url: S.optional(S.String),
|
|
122
|
+
b64_json: S.optional(S.String),
|
|
123
|
+
revised_prompt: S.optional(S.String),
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Token accounting for an image generation. Modelled so the usage row can
|
|
128
|
+
* record REAL counts: without it the response decode dropped `usage` and
|
|
129
|
+
* every image request was logged as `tokens_in: 0, tokens_out: 0`. Pricing
|
|
130
|
+
* itself stays on the existing catalog path (`costFor`), which returns 0
|
|
131
|
+
* for an unpriced model — image tokens are NOT priced as chat text here.
|
|
132
|
+
* Detail objects stay open (`S.Record`) because the spec keeps adding
|
|
133
|
+
* breakdown keys and a closed struct would drop them.
|
|
134
|
+
*/
|
|
135
|
+
const ImageUsageCommon = {
|
|
136
|
+
input_tokens: S.Number,
|
|
137
|
+
output_tokens: S.Number,
|
|
138
|
+
total_tokens: S.Number,
|
|
139
|
+
input_tokens_details: S.optional(S.Record(S.String, S.Unknown)),
|
|
140
|
+
} as const;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Usage on the NON-streaming response (spec: `ImageGenUsage`). Carries
|
|
144
|
+
* `output_tokens_details`, which its streaming sibling does not.
|
|
145
|
+
*/
|
|
146
|
+
export const ImageGenUsage = S.Struct({
|
|
147
|
+
...ImageUsageCommon,
|
|
148
|
+
output_tokens_details: S.optional(S.Record(S.String, S.Unknown)),
|
|
149
|
+
});
|
|
150
|
+
export type TImageGenUsage = S.Schema.Type<typeof ImageGenUsage>;
|
|
151
|
+
|
|
152
|
+
export const ImageGenerationResponse = S.Struct({
|
|
153
|
+
created: S.Number,
|
|
154
|
+
data: S.Array(ImageData),
|
|
155
|
+
/** Optional per the spec — DALL·E responses carry no usage block. */
|
|
156
|
+
usage: S.optional(ImageGenUsage),
|
|
157
|
+
/** Echoes of the settings actually used; preserved rather than dropped. */
|
|
158
|
+
background: S.optional(S.Literals(["transparent", "opaque"])),
|
|
159
|
+
output_format: S.optional(ImageOutputFormat),
|
|
160
|
+
size: S.optional(ImageSize),
|
|
161
|
+
quality: S.optional(S.String),
|
|
162
|
+
});
|
|
163
|
+
export type TImageGenerationResponse = S.Schema.Type<
|
|
164
|
+
typeof ImageGenerationResponse
|
|
165
|
+
>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Usage on a STREAMED completion (spec: `ImagesUsage`).
|
|
169
|
+
*
|
|
170
|
+
* These were briefly aliased on the assumption they were the same shape under
|
|
171
|
+
* two names. The spec-surface fixture disproved it: `ImagesUsage` has NO
|
|
172
|
+
* `output_tokens_details`. They share the four common fields — factored into
|
|
173
|
+
* `ImageUsageCommon` so the overlap is declared once — and each is asserted
|
|
174
|
+
* against its OWN spec schema, so a future divergence in either direction is
|
|
175
|
+
* a failing test rather than a silent mismatch.
|
|
176
|
+
*/
|
|
177
|
+
export const ImagesUsage = S.Struct({ ...ImageUsageCommon });
|
|
178
|
+
export type TImagesUsage = S.Schema.Type<typeof ImagesUsage>;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The two streaming events.
|
|
182
|
+
*
|
|
183
|
+
* FRAMING is spec-exact: NAMED SSE events (`event: image_generation.…`) and
|
|
184
|
+
* NO `[DONE]` sentinel — that is chat framing and does not appear in the
|
|
185
|
+
* images spec. `completed` is the terminator.
|
|
186
|
+
*
|
|
187
|
+
* FIELD REQUIREDNESS IS A DELIBERATE, DOCUMENTED RELAXATION — this is NOT
|
|
188
|
+
* spec-exact and must not be described as such. The spec marks `size`,
|
|
189
|
+
* `quality`, `background`, `output_format` (and `usage` on `completed`)
|
|
190
|
+
* REQUIRED, because it describes one provider that always reports them. This
|
|
191
|
+
* gateway serves many, and a provider that answers `stream: true` with a
|
|
192
|
+
* finished image often reports none of them.
|
|
193
|
+
*
|
|
194
|
+
* The choice was: fabricate values to satisfy a required set, or omit what
|
|
195
|
+
* the provider did not tell us. Omitting is the only honest option — a
|
|
196
|
+
* synthesized `size` or a zeroed `usage` is a lie about the generation, and
|
|
197
|
+
* `usage` in particular feeds billing. So these are optional HERE while
|
|
198
|
+
* remaining required on OpenAI's own wire, and a native OpenAI stream passes
|
|
199
|
+
* every field through untouched. `tests/protocol/openai-spec-surface.test.ts`
|
|
200
|
+
* asserts this divergence explicitly, so it stays a recorded decision rather
|
|
201
|
+
* than drift.
|
|
202
|
+
*/
|
|
203
|
+
export const ImageStreamPartialEvent = S.Struct({
|
|
204
|
+
type: S.Literals(["image_generation.partial_image"]),
|
|
205
|
+
b64_json: S.String,
|
|
206
|
+
created_at: S.Number,
|
|
207
|
+
size: S.optional(ImageSize),
|
|
208
|
+
quality: S.optional(S.String),
|
|
209
|
+
background: S.optional(S.String),
|
|
210
|
+
output_format: S.optional(S.String),
|
|
211
|
+
partial_image_index: S.Number,
|
|
212
|
+
});
|
|
213
|
+
export type TImageStreamPartialEvent = S.Schema.Type<
|
|
214
|
+
typeof ImageStreamPartialEvent
|
|
215
|
+
>;
|
|
216
|
+
|
|
217
|
+
export const ImageStreamCompletedEvent = S.Struct({
|
|
218
|
+
type: S.Literals(["image_generation.completed"]),
|
|
219
|
+
b64_json: S.String,
|
|
220
|
+
created_at: S.Number,
|
|
221
|
+
size: S.optional(ImageSize),
|
|
222
|
+
quality: S.optional(S.String),
|
|
223
|
+
background: S.optional(S.String),
|
|
224
|
+
output_format: S.optional(S.String),
|
|
225
|
+
/** Present on the GPT-image families; absent elsewhere. */
|
|
226
|
+
usage: S.optional(ImagesUsage),
|
|
227
|
+
/**
|
|
228
|
+
* OpenLLM EXTENSION, not in the OpenAI spec: the durable media-library URL
|
|
229
|
+
* for the finished image. Emitted only AFTER persistence has succeeded, so
|
|
230
|
+
* its presence is a guarantee the bytes are stored — never a promise made
|
|
231
|
+
* ahead of the fact. The non-streaming path already rewrites `data[].url`
|
|
232
|
+
* this way; without it the streaming path would be the one route that
|
|
233
|
+
* silently loses the library link.
|
|
234
|
+
*/
|
|
235
|
+
url: S.optional(S.String),
|
|
236
|
+
});
|
|
237
|
+
export type TImageStreamCompletedEvent = S.Schema.Type<
|
|
238
|
+
typeof ImageStreamCompletedEvent
|
|
239
|
+
>;
|
|
240
|
+
|
|
241
|
+
export const ImageStreamEvent = S.Union([
|
|
242
|
+
ImageStreamPartialEvent,
|
|
243
|
+
ImageStreamCompletedEvent,
|
|
244
|
+
]);
|
|
245
|
+
export type TImageStreamEvent = S.Schema.Type<typeof ImageStreamEvent>;
|
|
246
|
+
|
|
247
|
+
/** SSE event NAME for an image stream event — the spec frames by `type`. */
|
|
248
|
+
export const imageStreamEventName = (event: TImageStreamEvent): string =>
|
|
249
|
+
event.type;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Canonical single-reference image edit. Generation (`ImageGenerationRequest`)
|
|
253
|
+
* must not be used as a silent stand-in — edits require an explicit image
|
|
254
|
+
* and reject masks / extra references.
|
|
255
|
+
*/
|
|
256
|
+
export const ImageEditReference = S.Struct({
|
|
257
|
+
url: S.String,
|
|
258
|
+
});
|
|
259
|
+
export type TImageEditReference = S.Schema.Type<typeof ImageEditReference>;
|
|
260
|
+
|
|
261
|
+
export const ImageEditRequest = S.Struct({
|
|
262
|
+
model: S.String,
|
|
263
|
+
prompt: S.String,
|
|
264
|
+
image: ImageEditReference,
|
|
265
|
+
n: S.optional(S.Int.check(S.isGreaterThan(0))),
|
|
266
|
+
size: S.optional(ImageSize),
|
|
267
|
+
quality: S.optional(S.String),
|
|
268
|
+
response_format: S.optional(S.Literals(["url", "b64_json"])),
|
|
269
|
+
});
|
|
270
|
+
export type TImageEditRequest = S.Schema.Type<typeof ImageEditRequest>;
|
|
271
|
+
|
|
272
|
+
const { model: _imageEditModel, ...imageEditInputFields } =
|
|
273
|
+
ImageEditRequest.fields;
|
|
274
|
+
export const ImageEditInput = S.Struct({
|
|
275
|
+
...imageEditInputFields,
|
|
276
|
+
model: OptionalMediaModel,
|
|
277
|
+
});
|
|
278
|
+
export type TImageEditInput = S.Schema.Type<typeof ImageEditInput>;
|
|
279
|
+
|
|
280
|
+
export const ImageEditParseErrorCode = S.Literals([
|
|
281
|
+
"image_edit_mask_unsupported",
|
|
282
|
+
"image_edit_multi_reference_unsupported",
|
|
283
|
+
"image_edit_invalid",
|
|
284
|
+
]);
|
|
285
|
+
export type TImageEditParseErrorCode = S.Schema.Type<
|
|
286
|
+
typeof ImageEditParseErrorCode
|
|
287
|
+
>;
|
|
288
|
+
|
|
289
|
+
export type TImageEditParseFailure = {
|
|
290
|
+
readonly code: TImageEditParseErrorCode;
|
|
291
|
+
readonly message: string;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export type TImageEditParseResult =
|
|
295
|
+
| { readonly ok: true; readonly request: TImageEditRequest }
|
|
296
|
+
| { readonly ok: false; readonly error: TImageEditParseFailure };
|
|
297
|
+
|
|
298
|
+
export const decodeImageEditRequest = S.decodeUnknownResult(ImageEditRequest);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export * from "./access";
|
|
2
|
+
export * from "./agent";
|
|
3
|
+
export * from "./agent-prompt-text";
|
|
4
|
+
export * from "./api-key-syntax";
|
|
5
|
+
export * from "./audio";
|
|
6
|
+
export * from "./auth";
|
|
7
|
+
export * from "./changelog";
|
|
8
|
+
export * from "./chat";
|
|
9
|
+
export * from "./client-registry";
|
|
10
|
+
export * from "./config";
|
|
11
|
+
export * from "./control-channel";
|
|
12
|
+
export * from "./cooldown-reason";
|
|
13
|
+
export * from "./cost";
|
|
14
|
+
export * from "./daemon";
|
|
15
|
+
export * from "./daemon-port";
|
|
16
|
+
export * from "./default-chains";
|
|
17
|
+
export * from "./device-state";
|
|
18
|
+
export * from "./doctor-report";
|
|
19
|
+
export * from "./doctor-report-local";
|
|
20
|
+
export * from "./doctor-report-policy";
|
|
21
|
+
export * from "./errors";
|
|
22
|
+
export * from "./execution-selection";
|
|
23
|
+
export * from "./hop-grant";
|
|
24
|
+
export * from "./image-edit-parse";
|
|
25
|
+
export * from "./images";
|
|
26
|
+
export * from "./inference-headers";
|
|
27
|
+
export * from "./keepalive";
|
|
28
|
+
export * from "./local-auth";
|
|
29
|
+
export * from "./local-device-control";
|
|
30
|
+
export * from "./local-provider";
|
|
31
|
+
export * from "./local-session";
|
|
32
|
+
export * from "./marketplace";
|
|
33
|
+
export * from "./media-attempt";
|
|
34
|
+
export * from "./media-bytes";
|
|
35
|
+
export * from "./media-default";
|
|
36
|
+
export * from "./media-dimensions";
|
|
37
|
+
export * from "./media-input";
|
|
38
|
+
export * from "./media-model";
|
|
39
|
+
export * from "./media-option-issue";
|
|
40
|
+
export * from "./media-types";
|
|
41
|
+
export * from "./model-match";
|
|
42
|
+
export * from "./model-settings";
|
|
43
|
+
export * from "./models";
|
|
44
|
+
export * from "./mux";
|
|
45
|
+
export * from "./plan-surface";
|
|
46
|
+
export * from "./provider-models";
|
|
47
|
+
export * from "./provider-status";
|
|
48
|
+
export * from "./provider-tier";
|
|
49
|
+
export * from "./provider-usage";
|
|
50
|
+
export * from "./providers";
|
|
51
|
+
export * from "./public-usage-index";
|
|
52
|
+
export * from "./realtime";
|
|
53
|
+
export * from "./registry-pointer";
|
|
54
|
+
export * from "./relay";
|
|
55
|
+
export * from "./replay-session";
|
|
56
|
+
export * from "./responses";
|
|
57
|
+
export * from "./semver";
|
|
58
|
+
export * from "./setup-mode";
|
|
59
|
+
export * from "./stats";
|
|
60
|
+
export * from "./streaming";
|
|
61
|
+
// Floors + compat are control-plane / dashboard only — keep them OFF this
|
|
62
|
+
// barrel so the CLI/daemon binary closure never pulls publish-time pin
|
|
63
|
+
// metadata in via `import … from "@openllmsh/sdk/protocol"`. Import the
|
|
64
|
+
// dedicated subpaths (`…/subscription-daemon-floors`,
|
|
65
|
+
// `…/subscription-provider-compat`) from the browser / schema / release.
|
|
66
|
+
export * from "./subscription-provider";
|
|
67
|
+
export * from "./team-grant";
|
|
68
|
+
export * from "./team-shareable";
|
|
69
|
+
export * from "./transcription-encoding";
|
|
70
|
+
export * from "./url-locality";
|
|
71
|
+
export * from "./usage-snapshots";
|
|
72
|
+
export * from "./vendor-cli-install";
|
|
73
|
+
export * from "./video-job-plan";
|
|
74
|
+
export * from "./videos";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keepalive responses for slow whole-body endpoints (transcription).
|
|
3
|
+
*
|
|
4
|
+
* The edge proxy in front of the gateway (Cloudflare, ~100 s) drops a
|
|
5
|
+
* response that sends nothing for that long, so a long transcription
|
|
6
|
+
* came back as a 524. A client that sends {@link KEEPALIVE_REQUEST_HEADER}
|
|
7
|
+
* agrees to this instead: an answer ready before
|
|
8
|
+
* {@link KEEPALIVE_FIRST_BYTE_MS} is sent exactly as usual (real status,
|
|
9
|
+
* real headers); a slower one gets `200` headers at once, a space every
|
|
10
|
+
* {@link KEEPALIVE_INTERVAL_MS}, then a {@link TKeepaliveEnvelope} carrying
|
|
11
|
+
* the real status, the `x-openllm-*` headers and the body. Leading spaces
|
|
12
|
+
* are valid JSON, so the body still parses as one document.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const KEEPALIVE_REQUEST_HEADER = "x-openllm-keepalive";
|
|
16
|
+
/** Set on a response that switched to the envelope. */
|
|
17
|
+
export const KEEPALIVE_RESPONSE_HEADER = "x-openllm-keepalive";
|
|
18
|
+
/** Far inside the edge's ~100 s, whose clock also counts the upload and a
|
|
19
|
+
* cold start; every pre-dispatch refusal answers sooner. */
|
|
20
|
+
export const KEEPALIVE_FIRST_BYTE_MS = 30_000;
|
|
21
|
+
export const KEEPALIVE_INTERVAL_MS = 20_000;
|
|
22
|
+
|
|
23
|
+
export type TKeepaliveEnvelope = {
|
|
24
|
+
readonly openllm_keepalive: 1;
|
|
25
|
+
/** The status the response would have had. */
|
|
26
|
+
readonly status: number;
|
|
27
|
+
/** Its `content-type` and `x-openllm-*` headers (attribution, media urls). */
|
|
28
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
29
|
+
/** Its body: parsed JSON when it was JSON, otherwise the text. */
|
|
30
|
+
readonly body: unknown;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
34
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
|
+
|
|
36
|
+
export const isKeepaliveEnvelope = (
|
|
37
|
+
value: unknown,
|
|
38
|
+
): value is TKeepaliveEnvelope =>
|
|
39
|
+
isRecord(value) &&
|
|
40
|
+
value.openllm_keepalive === 1 &&
|
|
41
|
+
typeof value.status === "number" &&
|
|
42
|
+
isRecord(value.headers) &&
|
|
43
|
+
Object.values(value.headers).every((v) => typeof v === "string") &&
|
|
44
|
+
"body" in value;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The response a keepalive envelope stands for, so a client handles it
|
|
48
|
+
* exactly like an answer that arrived in time. Anything that is not an
|
|
49
|
+
* envelope response is returned unchanged.
|
|
50
|
+
*/
|
|
51
|
+
export const unwrapKeepaliveResponse = async (
|
|
52
|
+
res: Response,
|
|
53
|
+
): Promise<Response> => {
|
|
54
|
+
if (res.headers.get(KEEPALIVE_RESPONSE_HEADER) !== "1") return res;
|
|
55
|
+
const text = await res.text();
|
|
56
|
+
let parsed: unknown;
|
|
57
|
+
try {
|
|
58
|
+
parsed = JSON.parse(text);
|
|
59
|
+
} catch {
|
|
60
|
+
return new Response(text, { status: 502 });
|
|
61
|
+
}
|
|
62
|
+
if (!isKeepaliveEnvelope(parsed)) return new Response(text, { status: 502 });
|
|
63
|
+
const headers = new Headers(parsed.headers);
|
|
64
|
+
const body =
|
|
65
|
+
typeof parsed.body === "string" ? parsed.body : JSON.stringify(parsed.body);
|
|
66
|
+
if (typeof parsed.body !== "string")
|
|
67
|
+
headers.set("content-type", "application/json");
|
|
68
|
+
return new Response(body, { status: parsed.status, headers });
|
|
69
|
+
};
|