@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,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound header allow-list (C1) — the pure policy, shared by the cloud
|
|
3
|
+
* gateway (`@openllm/api`/`@openllm/core`) AND the coreless daemon walker
|
|
4
|
+
* (`@openllmsh/daemon`).
|
|
5
|
+
*
|
|
6
|
+
* Every header in this set survives the proxy hop verbatim — the
|
|
7
|
+
* gateway used to silently strip them, breaking SDK ergonomics and
|
|
8
|
+
* provider-side request correlation. Concretely:
|
|
9
|
+
*
|
|
10
|
+
* - `user-agent` / `x-app` — SDK identity strings the Anthropic /
|
|
11
|
+
* OpenAI dashboards group requests by. Without forwarding, every
|
|
12
|
+
* request appears as the gateway's UA and per-app analytics
|
|
13
|
+
* collapse to one giant bucket.
|
|
14
|
+
* - `anthropic-dangerous-direct-browser-access` — Anthropic-specific
|
|
15
|
+
* CORS-bypass opt-in; clients that set it expect it to reach
|
|
16
|
+
* api.anthropic.com or they error out.
|
|
17
|
+
* - `x-stainless-*` — Stainless-generated SDKs (Anthropic, OpenAI)
|
|
18
|
+
* stamp ~10 of these for runtime/lang/version diagnostics. We
|
|
19
|
+
* match the prefix because the exact suffix set evolves.
|
|
20
|
+
* - `anthropic-version` — the Anthropic API version the client
|
|
21
|
+
* pinned. Without forwarding, every request lands as our
|
|
22
|
+
* hardcoded default and a client that pinned a newer version
|
|
23
|
+
* can't tell their pin had no effect. (C2 makes the runner
|
|
24
|
+
* actually honour it; C1 just carries it through.)
|
|
25
|
+
* - `openai-organization` / `openai-project` — billing / scoping
|
|
26
|
+
* headers that OpenAI dashboards respect.
|
|
27
|
+
* - `openai-beta` — feature-flag opt-ins for preview APIs.
|
|
28
|
+
* - `accept-language` / `accept-encoding` — locale + transport
|
|
29
|
+
* preferences. accept-encoding in particular: an inbound `gzip`
|
|
30
|
+
* should let the upstream send gzip back so we don't pay for an
|
|
31
|
+
* uncompressed round-trip the client would have accepted.
|
|
32
|
+
*
|
|
33
|
+
* NOT in this set (B2 adds them with a `custom:`-only gate, which stays
|
|
34
|
+
* in `@openllm/api` since it's a gateway concept):
|
|
35
|
+
* - `idempotency-key` — the client's gateway-side key must not leak
|
|
36
|
+
* to first-party providers.
|
|
37
|
+
* - `x-openllm-pin-model` — meaningful only within the openllm
|
|
38
|
+
* gateway stack.
|
|
39
|
+
*
|
|
40
|
+
* Auth (`authorization` / `x-api-key`) and `content-type` are
|
|
41
|
+
* IDENTIFIED EXPLICITLY by the caller (the provider's `authHeaders`, or
|
|
42
|
+
* the daemon's injected subscription bearer); if a client somehow inserts
|
|
43
|
+
* one of those, the caller's value wins on collision (spread AFTER). Same
|
|
44
|
+
* for `anthropic-beta` — it's composed from inbound + body-derived +
|
|
45
|
+
* OAuth-derived betas, so the inbound is consumed via a separate field,
|
|
46
|
+
* not forwarded as a raw header.
|
|
47
|
+
*/
|
|
48
|
+
const FORWARD_EXACT: ReadonlySet<string> = new Set([
|
|
49
|
+
"user-agent",
|
|
50
|
+
"x-app",
|
|
51
|
+
"anthropic-dangerous-direct-browser-access",
|
|
52
|
+
"anthropic-version",
|
|
53
|
+
"openai-organization",
|
|
54
|
+
"openai-project",
|
|
55
|
+
"openai-beta",
|
|
56
|
+
"accept-language",
|
|
57
|
+
"accept-encoding",
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const FORWARD_PREFIX: ReadonlyArray<string> = ["x-stainless-"];
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Walks a `Headers` object and returns a flat lowercase map of every
|
|
64
|
+
* header the allow-list matches. Returned record is safe to spread into
|
|
65
|
+
* a fetch `headers` init — caller auth / content-type override on
|
|
66
|
+
* collision because they're spread AFTER.
|
|
67
|
+
*/
|
|
68
|
+
export const forwardableHeadersFrom = (
|
|
69
|
+
headers: Headers,
|
|
70
|
+
): Record<string, string> => {
|
|
71
|
+
const out: Record<string, string> = {};
|
|
72
|
+
headers.forEach((value, key) => {
|
|
73
|
+
const lk = key.toLowerCase();
|
|
74
|
+
if (FORWARD_EXACT.has(lk)) {
|
|
75
|
+
out[lk] = value;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
for (const prefix of FORWARD_PREFIX) {
|
|
79
|
+
if (lk.startsWith(prefix)) {
|
|
80
|
+
out[lk] = value;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return out;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/** `Request`-level convenience over {@link forwardableHeadersFrom}. */
|
|
89
|
+
export const captureForwardableHeaders = (
|
|
90
|
+
req: Request,
|
|
91
|
+
): Record<string, string> => forwardableHeadersFrom(req.headers);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Originator passthrough DENYLIST (the daemon's policy).
|
|
95
|
+
*
|
|
96
|
+
* Where the cloud gateway curates an allow-list (it's a multi-tenant BYOK proxy
|
|
97
|
+
* that must NOT leak arbitrary/gateway-internal headers to first-party
|
|
98
|
+
* providers), the local daemon is a transparent reverse proxy in front of the
|
|
99
|
+
* user's OWN subscription CLI: it should carry the ORIGINATOR's headers verbatim
|
|
100
|
+
* so a genuine vendor-CLI request reaches the vendor byte-for-byte, and an
|
|
101
|
+
* unsupported one is rejected by the vendor (terms compliance — we don't launder
|
|
102
|
+
* it). So instead of naming every header to KEEP (which needs updating as SDKs
|
|
103
|
+
* and vendors add headers), we name only the small, stable set to DROP and pass
|
|
104
|
+
* everything else through.
|
|
105
|
+
*
|
|
106
|
+
* Dropped, and why:
|
|
107
|
+
* - `authorization` / `x-api-key` — the inbound gateway key; the daemon
|
|
108
|
+
* replaces it with the subscription bearer.
|
|
109
|
+
* - `host` / `content-length` / `content-type` / `content-encoding` /
|
|
110
|
+
* `accept-encoding` — transport + body framing the daemon re-derives (it
|
|
111
|
+
* re-serializes the body and may re-encode the response), plus the wire
|
|
112
|
+
* builder owns `content-type`.
|
|
113
|
+
* - hop-by-hop (`connection`, `keep-alive`, `transfer-encoding`, `te`,
|
|
114
|
+
* `trailer`, `upgrade`, `proxy-authorization`, `proxy-connection`) — never
|
|
115
|
+
* forwarded across a proxy hop.
|
|
116
|
+
* - BROWSER-HOP headers: `origin` / `referer` + the `sec-*` client hints
|
|
117
|
+
* (`sec-fetch-*`, `sec-ch-ua*`, `sec-websocket-*`). These describe the
|
|
118
|
+
* client→daemon hop, not the daemon→vendor hop, and forwarding them is
|
|
119
|
+
* actively harmful: an `Origin` makes Anthropic treat the request as a
|
|
120
|
+
* browser CORS call and reject it ("CORS requests must set
|
|
121
|
+
* 'anthropic-dangerous-direct-browser-access'"). The daemon is a
|
|
122
|
+
* server-to-server proxy, not a browser.
|
|
123
|
+
* - `cookie` — the client's cookies are not the vendor's.
|
|
124
|
+
* - `anthropic-beta` — NOT a raw passthrough: it's composed from inbound +
|
|
125
|
+
* body-derived + OAuth betas by the wire builder (`inboundBeta`), which
|
|
126
|
+
* layers the authoritative value on top.
|
|
127
|
+
*
|
|
128
|
+
* NOT dropped (deliberately, vs the old capture-fixture denylist): per-request
|
|
129
|
+
* volatiles like `session-id` / `x-client-request-id` / `x-codex-*`. Those were
|
|
130
|
+
* stripped when we REPLAYED a captured fixture (a stale captured value would
|
|
131
|
+
* pin every request); now we forward the ORIGINATOR's live value, which is
|
|
132
|
+
* correct.
|
|
133
|
+
*/
|
|
134
|
+
const ORIGINATOR_DENY_EXACT: ReadonlySet<string> = new Set([
|
|
135
|
+
"authorization",
|
|
136
|
+
"x-api-key",
|
|
137
|
+
"host",
|
|
138
|
+
"content-length",
|
|
139
|
+
"content-type",
|
|
140
|
+
"content-encoding",
|
|
141
|
+
"accept-encoding",
|
|
142
|
+
"transfer-encoding",
|
|
143
|
+
"connection",
|
|
144
|
+
"keep-alive",
|
|
145
|
+
"te",
|
|
146
|
+
"trailer",
|
|
147
|
+
"upgrade",
|
|
148
|
+
"proxy-authorization",
|
|
149
|
+
"proxy-connection",
|
|
150
|
+
"cookie",
|
|
151
|
+
"anthropic-beta",
|
|
152
|
+
// Browser-hop headers (client→daemon only). `origin` in particular makes
|
|
153
|
+
// Anthropic gate the request behind `anthropic-dangerous-direct-browser-access`.
|
|
154
|
+
"origin",
|
|
155
|
+
"referer",
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
/** Browser-set header families never meant for the daemon→vendor hop:
|
|
159
|
+
* `sec-fetch-*`, `sec-ch-ua*`, `sec-websocket-*` (codex opens `/responses` as a
|
|
160
|
+
* WS upgrade; the daemon serves plain HTTP POST). */
|
|
161
|
+
const ORIGINATOR_DENY_PREFIX: ReadonlyArray<string> = ["sec-"];
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Pass through EVERY inbound header except the stable denylist above. Returned
|
|
165
|
+
* record is safe to spread into a fetch `headers` init as the BASE — the daemon
|
|
166
|
+
* spreads the injected bearer / credential-account headers and the wire-derived
|
|
167
|
+
* headers AFTER, so they win on collision.
|
|
168
|
+
*/
|
|
169
|
+
export const originatorHeadersFrom = (
|
|
170
|
+
headers: Headers,
|
|
171
|
+
): Record<string, string> => {
|
|
172
|
+
const out: Record<string, string> = {};
|
|
173
|
+
headers.forEach((value, key) => {
|
|
174
|
+
const lk = key.toLowerCase();
|
|
175
|
+
if (ORIGINATOR_DENY_EXACT.has(lk)) return;
|
|
176
|
+
if (ORIGINATOR_DENY_PREFIX.some((p) => lk.startsWith(p))) return;
|
|
177
|
+
out[lk] = value;
|
|
178
|
+
});
|
|
179
|
+
return out;
|
|
180
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { TCooldownReason } from "@openllmsh/sdk/protocol";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Optional per-provider overrides for hop-error classification. Pure data —
|
|
5
|
+
* a rule returns only a reason (never abort). Custom providers (`custom:*`)
|
|
6
|
+
* and unknown names get the empty list so they always use the shared
|
|
7
|
+
* defaults; never accept user-supplied rules.
|
|
8
|
+
*/
|
|
9
|
+
export type TProviderErrorRule = {
|
|
10
|
+
readonly statuses?: readonly number[];
|
|
11
|
+
readonly code?: string;
|
|
12
|
+
readonly message?: RegExp;
|
|
13
|
+
readonly reason: TCooldownReason;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Ships empty. Built-in providers use the shared defaults until a rule is added. */
|
|
17
|
+
export const PROVIDER_ERROR_RULES: Readonly<
|
|
18
|
+
Record<string, readonly TProviderErrorRule[]>
|
|
19
|
+
> = {};
|
|
20
|
+
|
|
21
|
+
export const errorRulesFor = (
|
|
22
|
+
provider: string | undefined,
|
|
23
|
+
rules: Readonly<
|
|
24
|
+
Record<string, readonly TProviderErrorRule[]>
|
|
25
|
+
> = PROVIDER_ERROR_RULES,
|
|
26
|
+
): readonly TProviderErrorRule[] => {
|
|
27
|
+
if (provider === undefined || provider.length === 0) return [];
|
|
28
|
+
if (provider.startsWith("custom:")) return [];
|
|
29
|
+
return rules[provider] ?? [];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const providerRuleMatches = (
|
|
33
|
+
rule: TProviderErrorRule,
|
|
34
|
+
input: {
|
|
35
|
+
readonly status: number;
|
|
36
|
+
readonly code?: string | undefined;
|
|
37
|
+
readonly message: string;
|
|
38
|
+
},
|
|
39
|
+
): boolean => {
|
|
40
|
+
if (rule.statuses !== undefined && !rule.statuses.includes(input.status)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (rule.code !== undefined && input.code !== rule.code) return false;
|
|
44
|
+
if (rule.message !== undefined && !rule.message.test(input.message)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TChatCompletionChunk,
|
|
3
|
+
TChatCompletionResponse,
|
|
4
|
+
} from "@openllmsh/sdk/protocol";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ─── Structured refusal detection (provider-independent) ───────────────
|
|
8
|
+
*
|
|
9
|
+
* Some providers answer `200 OK` with a body that IS a refusal rather
|
|
10
|
+
* than a transport error — an Anthropic policy/safety block
|
|
11
|
+
* (`stop_reason: "refusal"`), an OpenAI `finish_reason: "content_filter"`,
|
|
12
|
+
* a Gemini `promptFeedback.blockReason`, etc. Every provider adapter
|
|
13
|
+
* normalises these to the SAME canonical shape: a choice whose
|
|
14
|
+
* `finish_reason === "content_filter"`. So detection is a single
|
|
15
|
+
* canonical-field check — NEVER a match on model ids, request ids,
|
|
16
|
+
* provider names, links, or human-readable policy prose.
|
|
17
|
+
*
|
|
18
|
+
* With a multi-provider fallback chain, "the model refused" is the wrong
|
|
19
|
+
* terminal default: another candidate may not refuse the same canonical
|
|
20
|
+
* request. These predicates let a caller treat a pre-commit structured
|
|
21
|
+
* refusal like any other pre-output failure — walk to the next hop —
|
|
22
|
+
* while a refusal that arrives AFTER real output stays committed (the
|
|
23
|
+
* caller decides based on WHEN it sees the refusal, not just whether).
|
|
24
|
+
*
|
|
25
|
+
* Shared by the cloud dispatch chain (`@openllm/core`) and the coreless
|
|
26
|
+
* daemon walker so the two paths cannot drift — the same rule the shared
|
|
27
|
+
* streaming peek follows.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** A single decoded chunk that carries a canonical structured refusal. */
|
|
31
|
+
export const isRefusalChunk = (chunk: TChatCompletionChunk): boolean =>
|
|
32
|
+
chunk.choices.some((choice) => choice.finish_reason === "content_filter");
|
|
33
|
+
|
|
34
|
+
/** A fully-decoded non-streaming response that IS a canonical refusal. */
|
|
35
|
+
export const isCanonicalRefusal = (
|
|
36
|
+
response: TChatCompletionResponse,
|
|
37
|
+
): boolean =>
|
|
38
|
+
response.choices.some((choice) => choice.finish_reason === "content_filter");
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI's hosted Responses `apply_patch` tool ↔ a canonical function tool.
|
|
3
|
+
*
|
|
4
|
+
* Clients built on the OpenAI Agents SDK (Pomerado, the SDK's sandbox agents)
|
|
5
|
+
* declare `{type:"apply_patch"}` and expect `apply_patch_call` output items
|
|
6
|
+
* carrying `{type, path, diff}` operations. No upstream but OpenAI's own API
|
|
7
|
+
* honours the hosted tool (chatgpt.com 400s `Unsupported tool type:
|
|
8
|
+
* apply_patch`), and canonical requests model only function tools. So the
|
|
9
|
+
* inbound adapter declares it as a function under a reserved name, and the
|
|
10
|
+
* outbound encoders turn calls to that name back into `apply_patch_call` items.
|
|
11
|
+
* The reserved name never collides with a client's own `apply_patch` function
|
|
12
|
+
* (older Codex builds declare one with a different argument shape).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const APPLY_PATCH_FUNCTION_NAME = "openllm_apply_patch";
|
|
16
|
+
|
|
17
|
+
/** The Responses function definition the hosted tool is declared as. */
|
|
18
|
+
export const applyPatchFunctionTool = {
|
|
19
|
+
type: "function",
|
|
20
|
+
name: APPLY_PATCH_FUNCTION_NAME,
|
|
21
|
+
description:
|
|
22
|
+
"Create, update or delete one file. create_file and update_file require diff, a V4A diff: lines prefixed '+' add, '-' remove and ' ' keep, with '@@' context headers for updates. A create_file diff contains only '+' lines. delete_file takes no diff.",
|
|
23
|
+
parameters: {
|
|
24
|
+
type: "object",
|
|
25
|
+
properties: {
|
|
26
|
+
type: {
|
|
27
|
+
type: "string",
|
|
28
|
+
enum: ["create_file", "update_file", "delete_file"],
|
|
29
|
+
},
|
|
30
|
+
path: { type: "string" },
|
|
31
|
+
diff: { type: "string" },
|
|
32
|
+
},
|
|
33
|
+
required: ["type", "path"],
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
},
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
export const isHostedApplyPatchTool = (t: unknown): boolean =>
|
|
39
|
+
typeof t === "object" &&
|
|
40
|
+
t !== null &&
|
|
41
|
+
(t as { type?: unknown }).type === "apply_patch";
|
|
42
|
+
|
|
43
|
+
export type TApplyPatchOperation = {
|
|
44
|
+
readonly type: "create_file" | "update_file" | "delete_file";
|
|
45
|
+
readonly path: string;
|
|
46
|
+
readonly diff?: string;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const OPERATION_TYPES = new Set(["create_file", "update_file", "delete_file"]);
|
|
50
|
+
|
|
51
|
+
/** The operation in a call's JSON arguments, or undefined if malformed. */
|
|
52
|
+
export const applyPatchOperationOf = (
|
|
53
|
+
args: string,
|
|
54
|
+
): TApplyPatchOperation | undefined => {
|
|
55
|
+
let parsed: unknown;
|
|
56
|
+
try {
|
|
57
|
+
parsed = JSON.parse(args);
|
|
58
|
+
} catch {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
62
|
+
const { type, path, diff } = parsed as Record<string, unknown>;
|
|
63
|
+
if (typeof type !== "string" || !OPERATION_TYPES.has(type)) return undefined;
|
|
64
|
+
if (typeof path !== "string") return undefined;
|
|
65
|
+
// create_file and update_file carry their content in the diff; the Agents
|
|
66
|
+
// SDK rejects either without one.
|
|
67
|
+
const hasDiff = typeof diff === "string" && diff.length > 0;
|
|
68
|
+
if (type !== "delete_file" && !hasDiff) return undefined;
|
|
69
|
+
return {
|
|
70
|
+
type: type as TApplyPatchOperation["type"],
|
|
71
|
+
path,
|
|
72
|
+
...(hasDiff ? { diff } : {}),
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A finished call to the reserved function as a Responses `apply_patch_call`
|
|
78
|
+
* item, or undefined when its arguments are not a valid operation (the caller
|
|
79
|
+
* then emits a plain `function_call`, which the client reports as an error).
|
|
80
|
+
*/
|
|
81
|
+
export const toApplyPatchCallItem = (
|
|
82
|
+
id: string,
|
|
83
|
+
callId: string,
|
|
84
|
+
args: string,
|
|
85
|
+
):
|
|
86
|
+
| {
|
|
87
|
+
readonly type: "apply_patch_call";
|
|
88
|
+
readonly id: string;
|
|
89
|
+
readonly call_id: string;
|
|
90
|
+
readonly status: "completed";
|
|
91
|
+
readonly operation: TApplyPatchOperation;
|
|
92
|
+
}
|
|
93
|
+
| undefined => {
|
|
94
|
+
const operation = applyPatchOperationOf(args);
|
|
95
|
+
return operation === undefined
|
|
96
|
+
? undefined
|
|
97
|
+
: {
|
|
98
|
+
type: "apply_patch_call",
|
|
99
|
+
id: id.replace(/^fc_/, "apc_"),
|
|
100
|
+
call_id: callId,
|
|
101
|
+
status: "completed",
|
|
102
|
+
operation,
|
|
103
|
+
};
|
|
104
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Responses-API `namespace` tools ↔ canonical flat function names.
|
|
3
|
+
*
|
|
4
|
+
* Codex groups tools — every MCP server, sub-agent controls, … — as
|
|
5
|
+
* `{type:"namespace", name:"mcp__openllm", tools:[{type:"function", …}]}` and
|
|
6
|
+
* only routes a call back to the tool when the returned `function_call` carries
|
|
7
|
+
* BOTH `namespace` and `name` (a flat `mcp__openllm__tool` is rejected as
|
|
8
|
+
* "unsupported call"). Canonical requests have no namespace concept, so the
|
|
9
|
+
* Responses adapters flatten each member to `<namespace>--<name>` and split it
|
|
10
|
+
* again on the way out. Codex identifiers use `_`, never `--`, so the join is
|
|
11
|
+
* unambiguous; a plain top-level function never contains the separator.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const RESPONSES_NAMESPACE_SEPARATOR = "--";
|
|
15
|
+
|
|
16
|
+
/** Canonical function name for a namespace member. */
|
|
17
|
+
export const joinNamespacedToolName = (
|
|
18
|
+
namespace: string,
|
|
19
|
+
name: string,
|
|
20
|
+
): string => `${namespace}${RESPONSES_NAMESPACE_SEPARATOR}${name}`;
|
|
21
|
+
|
|
22
|
+
/** Split a canonical name back into its Responses `namespace` + `name`. */
|
|
23
|
+
export const splitNamespacedToolName = (
|
|
24
|
+
joined: string,
|
|
25
|
+
): { readonly namespace?: string; readonly name: string } => {
|
|
26
|
+
const at = joined.indexOf(RESPONSES_NAMESPACE_SEPARATOR);
|
|
27
|
+
if (at <= 0 || at + RESPONSES_NAMESPACE_SEPARATOR.length >= joined.length) {
|
|
28
|
+
return { name: joined };
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
namespace: joined.slice(0, at),
|
|
32
|
+
name: joined.slice(at + RESPONSES_NAMESPACE_SEPARATOR.length),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** `{ name }` or `{ namespace, name }` — spread into a Responses `function_call`. */
|
|
37
|
+
export const responsesCallIdentity = (
|
|
38
|
+
joined: string,
|
|
39
|
+
): { readonly name: string; readonly namespace?: string } => {
|
|
40
|
+
const split = splitNamespacedToolName(joined);
|
|
41
|
+
return split.namespace === undefined
|
|
42
|
+
? { name: split.name }
|
|
43
|
+
: { namespace: split.namespace, name: split.name };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Canonical name for an incoming Responses item that may carry `namespace`. */
|
|
47
|
+
export const canonicalToolNameOf = (
|
|
48
|
+
name: string,
|
|
49
|
+
namespace: unknown,
|
|
50
|
+
): string =>
|
|
51
|
+
typeof namespace === "string" && namespace.length > 0
|
|
52
|
+
? joinNamespacedToolName(namespace, name)
|
|
53
|
+
: name;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scrub credential material from upstream text before it leaves the gateway:
|
|
3
|
+
* error envelopes returned to a caller and debug logs. Removes each exact
|
|
4
|
+
* secret, its masked-tail and prefix forms, and common key/Bearer patterns.
|
|
5
|
+
* Pure and dependency-free so `core` (runner debug logs), `api` (team
|
|
6
|
+
* credential hops) and the daemon share one implementation.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const SK_LIKE = /\b(?:sk|rk|key)[-_][A-Za-z0-9_-]{8,}\b/gi;
|
|
10
|
+
const BEARER_LIKE = /\bBearer\s+[A-Za-z0-9._\-+/=]{8,}/gi;
|
|
11
|
+
// Symbol masks start with a non-word char, so a leading \b would only match
|
|
12
|
+
// when a letter precedes the mask; keep the boundary for the `xx` form only.
|
|
13
|
+
const LAST4_HINT = /(?:\*{2,}|•{2,}|\.{2,}|…|\bx{2,})[A-Za-z0-9]{4}\b/gi;
|
|
14
|
+
|
|
15
|
+
const escapeRegExp = (value: string): string =>
|
|
16
|
+
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Remove exact secrets and common masked/prefix forms from a string.
|
|
20
|
+
* `secrets` should include the full api_key and any base_url that must not
|
|
21
|
+
* leak. Returns a new string; never throws.
|
|
22
|
+
*/
|
|
23
|
+
export const scrubSecretText = (
|
|
24
|
+
text: string,
|
|
25
|
+
secrets: ReadonlyArray<string> = [],
|
|
26
|
+
): string => {
|
|
27
|
+
let out = text;
|
|
28
|
+
for (const secret of secrets) {
|
|
29
|
+
if (secret.length === 0) continue;
|
|
30
|
+
out = out.split(secret).join("[redacted]");
|
|
31
|
+
// URL secrets (a custom base_url): exact removal only — the prefix/tail
|
|
32
|
+
// heuristics below would match every URL with the same scheme or TLD.
|
|
33
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(secret)) continue;
|
|
34
|
+
if (secret.length >= 4) {
|
|
35
|
+
const last4 = secret.slice(-4);
|
|
36
|
+
const last4Re = new RegExp(
|
|
37
|
+
`(?:\\*{2,}|\\u2022{2,}|\\.{2,}|x{2,}|…)${escapeRegExp(last4)}`,
|
|
38
|
+
"gi",
|
|
39
|
+
);
|
|
40
|
+
out = out.replace(last4Re, "[redacted]");
|
|
41
|
+
// Also scrub a bare "...abcd" / "sk-...abcd" style suffix when the
|
|
42
|
+
// upstream echoes only the tail.
|
|
43
|
+
const tailRe = new RegExp(
|
|
44
|
+
`\\b[A-Za-z0-9_-]*${escapeRegExp(last4)}\\b`,
|
|
45
|
+
"g",
|
|
46
|
+
);
|
|
47
|
+
// Only rewrite tokens that are longer than the last4 alone (avoid
|
|
48
|
+
// nuking unrelated 4-char words).
|
|
49
|
+
out = out.replace(tailRe, (match) =>
|
|
50
|
+
match.length > 4 ? "[redacted]" : match,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (secret.length >= 8) {
|
|
54
|
+
const prefix = secret.slice(0, 8);
|
|
55
|
+
const prefixRe = new RegExp(
|
|
56
|
+
`${escapeRegExp(prefix)}[A-Za-z0-9._\\-+/=]*`,
|
|
57
|
+
"g",
|
|
58
|
+
);
|
|
59
|
+
out = out.replace(prefixRe, "[redacted]");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
out = out.replace(SK_LIKE, "[redacted]");
|
|
63
|
+
out = out.replace(BEARER_LIKE, "Bearer [redacted]");
|
|
64
|
+
out = out.replace(LAST4_HINT, "[redacted]");
|
|
65
|
+
return out;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type TScrubbableErrorEnvelope = {
|
|
69
|
+
readonly error: {
|
|
70
|
+
readonly message: string;
|
|
71
|
+
readonly type: string;
|
|
72
|
+
readonly code?: string | null;
|
|
73
|
+
readonly param?: string | null;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** Return a copy of an OpenAI-shaped error envelope with secrets scrubbed. */
|
|
78
|
+
export const scrubSecretErrorEnvelope = <T extends TScrubbableErrorEnvelope>(
|
|
79
|
+
envelope: T,
|
|
80
|
+
secrets: ReadonlyArray<string>,
|
|
81
|
+
): T => {
|
|
82
|
+
const err = envelope.error;
|
|
83
|
+
return {
|
|
84
|
+
...envelope,
|
|
85
|
+
error: {
|
|
86
|
+
...err,
|
|
87
|
+
message: scrubSecretText(err.message, secrets),
|
|
88
|
+
type: scrubSecretText(err.type, secrets),
|
|
89
|
+
...(err.code !== undefined && err.code !== null
|
|
90
|
+
? { code: scrubSecretText(String(err.code), secrets) }
|
|
91
|
+
: err.code === null
|
|
92
|
+
? { code: null }
|
|
93
|
+
: {}),
|
|
94
|
+
...(err.param !== undefined && err.param !== null
|
|
95
|
+
? { param: scrubSecretText(String(err.param), secrets) }
|
|
96
|
+
: err.param === null
|
|
97
|
+
? { param: null }
|
|
98
|
+
: {}),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
};
|