@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,19 @@
|
|
|
1
|
+
# Contributing to `@openllmsh/sdk/wire`
|
|
2
|
+
|
|
3
|
+
This repository is a **read-only mirror**, regenerated from the OpenLLM
|
|
4
|
+
monorepo on every release. Do not expect merges here to stick — the mirror's
|
|
5
|
+
`main` is force-updated from upstream each release.
|
|
6
|
+
|
|
7
|
+
**PRs are welcome and are ingested upstream.** A maintainer pulls your change
|
|
8
|
+
into the monorepo (your commit + authorship preserved), it goes through review
|
|
9
|
+
there, and the next release re-mirrors it back. Your PR is then closed as
|
|
10
|
+
"landed upstream in vX".
|
|
11
|
+
|
|
12
|
+
## License of contributions
|
|
13
|
+
|
|
14
|
+
`wire` is **source-available under the Business Source License 1.1**
|
|
15
|
+
(BUSL-1.1), with OpenLLM, INC as the sole commercial Licensor. Because
|
|
16
|
+
inbound=outbound does not hold cleanly for BUSL, contributions require a
|
|
17
|
+
**Contributor License Agreement (CLA)** so OpenLLM, INC can keep granting
|
|
18
|
+
commercial licenses over — and perform the eventual MIT relicense of — the
|
|
19
|
+
contributed code. The CLA-assistant bot will prompt you on your first PR.
|
package/wire/LICENSE
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
Licensor: OpenLLM, INC
|
|
4
|
+
Licensed Work: OpenLLM Wire
|
|
5
|
+
The Licensed Work is (c) 2026 OpenLLM, INC.
|
|
6
|
+
Additional Use Grant: You may use the Licensed Work for internal business purposes, personal use, development, testing, evaluation, non-production deployments, and self-hosted production deployments that are not offered to third parties as a hosted, managed, or paid service that competes with OpenLLM or any substantially similar AI model gateway, proxy, credential-management, routing, memory, indexing, or inference orchestration product or service. In addition, further uses may be listed and defined at https://github.com/openllmsh/license-grants.
|
|
7
|
+
Change Date: The earlier of the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work, or a date specified at https://github.com/openllmsh/license-grants.
|
|
8
|
+
Change License: MIT License
|
|
9
|
+
|
|
10
|
+
Terms
|
|
11
|
+
|
|
12
|
+
The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use.
|
|
13
|
+
|
|
14
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the Change License, and the rights granted in the paragraph above terminate.
|
|
15
|
+
|
|
16
|
+
If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work.
|
|
17
|
+
|
|
18
|
+
All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor.
|
|
19
|
+
|
|
20
|
+
You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from any third party, you must retain all copyright, patent, trademark, and attribution notices contained in the Licensed Work.
|
|
21
|
+
|
|
22
|
+
Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work.
|
|
23
|
+
|
|
24
|
+
This License does not grant you any right in any trademark or logo of Licensor or its affiliates.
|
|
25
|
+
|
|
26
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE.
|
|
27
|
+
|
|
28
|
+
Business Source License 1.1 Parameters
|
|
29
|
+
|
|
30
|
+
Licensor: OpenLLM, INC
|
|
31
|
+
Licensed Work: OpenLLM Wire
|
|
32
|
+
Change Date: The earlier of the fourth anniversary of first publication, or a date specified at https://github.com/openllmsh/license-grants.
|
|
33
|
+
Change License: MIT License
|
package/wire/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: light)" srcset="./assets/openllm-light.svg">
|
|
4
|
+
<img alt="OpenLLM" src="./assets/openllm.svg" width="300">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<p align="center"><b>wire</b> — the wire-format engine behind OpenLLM.</p>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<a href="./LICENSE"><img alt="License: BUSL-1.1" src="https://img.shields.io/badge/license-BUSL--1.1-blue.svg"></a>
|
|
12
|
+
<img alt="source-available" src="https://img.shields.io/badge/source-available-informational.svg">
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
The pure transforms that let one request speak every dialect — the
|
|
18
|
+
**Anthropic ↔ OpenAI ↔ Responses** translation layer, with no DI, no
|
|
19
|
+
providers, and no `fetch`. Same code the hosted gateway and the local
|
|
20
|
+
daemon both run:
|
|
21
|
+
|
|
22
|
+
- request / response / streaming adapters across all three wire formats
|
|
23
|
+
- reasoning-signature codec · canonical-message + token-estimate helpers
|
|
24
|
+
- SSE primitives (accumulate · tool-strip · provider-decode · heartbeat)
|
|
25
|
+
- the single upstream-request builder (model/stream pinning, Anthropic
|
|
26
|
+
adaptive-thinking + `anthropic-beta`)
|
|
27
|
+
|
|
28
|
+
Depends only on [`@openllmsh/sdk/protocol`](https://github.com/openllmsh/sdk)
|
|
29
|
+
+ `effect`.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
bun install github:openllmsh/sdk#<tag>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { toAnthropicMessagesResponse } from "@openllmsh/sdk/wire/adapters/messages/response";
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
**Source-available** under the [Business Source License 1.1](./LICENSE)
|
|
44
|
+
(© OpenLLM, INC) — use it freely except to run a competing hosted service;
|
|
45
|
+
converts to MIT on the Change Date. This is not OSI open-source.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
> **Read-only mirror.** Regenerated from the OpenLLM monorepo each release.
|
|
50
|
+
> PRs welcome — ingested upstream with your authorship preserved. BUSL
|
|
51
|
+
> contributions require the CLA (the bot will prompt you).
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { TAnthropicStopReason } from "@openllmsh/sdk/protocol";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Visible answer after stripping a signed thought the model repeated
|
|
5
|
+
* as `content`. Prefix / exact restatement → hide; thought then extra
|
|
6
|
+
* → only the extra; anything else → the content as-is.
|
|
7
|
+
*/
|
|
8
|
+
export const visibleAnswerAfterThought = (
|
|
9
|
+
contentSoFar: string,
|
|
10
|
+
thought: string,
|
|
11
|
+
): string => {
|
|
12
|
+
if (thought.length === 0) return contentSoFar;
|
|
13
|
+
if (thought.startsWith(contentSoFar)) return "";
|
|
14
|
+
if (contentSoFar.startsWith(thought)) {
|
|
15
|
+
return contentSoFar.slice(thought.length);
|
|
16
|
+
}
|
|
17
|
+
return contentSoFar;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const anthropicStopReasonFrom = (
|
|
21
|
+
finish: string | null | undefined,
|
|
22
|
+
): TAnthropicStopReason | null => {
|
|
23
|
+
if (finish == null) return null;
|
|
24
|
+
switch (finish) {
|
|
25
|
+
case "stop":
|
|
26
|
+
return "end_turn";
|
|
27
|
+
case "length":
|
|
28
|
+
return "max_tokens";
|
|
29
|
+
case "tool_calls":
|
|
30
|
+
case "function_call":
|
|
31
|
+
return "tool_use";
|
|
32
|
+
case "content_filter":
|
|
33
|
+
return "refusal";
|
|
34
|
+
default:
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flattens LiteLLM-shaped `reasoning_items` summary text for Anthropic `/compact`.
|
|
3
|
+
*
|
|
4
|
+
* Ref:
|
|
5
|
+
* `ref/.litellm/litellm/litellm/completion_extras/litellm_responses_transformation/transformation.py`
|
|
6
|
+
* — `_build_reasoning_item` (dict branch ~78–82) builds each `summary` row as
|
|
7
|
+
* `{ "type": s.get("type", "summary_text"), "text": s.get("text", "") }`.
|
|
8
|
+
* `translate_responses_chunk_to_openai_stream` attaches those items on
|
|
9
|
+
* `response.completed` as `Delta(reasoning_items=...)` (~1321–1356).
|
|
10
|
+
*
|
|
11
|
+
* LiteLLM keeps structured `reasoning_items`; OpenLLM joins non-empty `text`
|
|
12
|
+
* fields with newlines so clients that require user-visible `text` still work.
|
|
13
|
+
*/
|
|
14
|
+
export const plainTextFromReasoningItems = (
|
|
15
|
+
items: ReadonlyArray<unknown> | null | undefined,
|
|
16
|
+
): string => {
|
|
17
|
+
if (items === undefined || items === null || !Array.isArray(items)) {
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
const parts: string[] = [];
|
|
21
|
+
for (const item of items) {
|
|
22
|
+
if (item === null || typeof item !== "object") continue;
|
|
23
|
+
const rec = item as Record<string, unknown>;
|
|
24
|
+
if (rec.type !== "reasoning") continue;
|
|
25
|
+
const summaryRaw = rec.summary;
|
|
26
|
+
if (!Array.isArray(summaryRaw)) continue;
|
|
27
|
+
for (const s of summaryRaw) {
|
|
28
|
+
if (typeof s === "object" && s !== null && !Array.isArray(s)) {
|
|
29
|
+
const o = s as Record<string, unknown>;
|
|
30
|
+
const text = typeof o.text === "string" ? o.text : "";
|
|
31
|
+
if (text.length > 0) {
|
|
32
|
+
parts.push(text);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return parts.join("\n");
|
|
38
|
+
};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-provider reasoning round-trip codec.
|
|
3
|
+
*
|
|
4
|
+
* THE BUG THIS FIXES: when an Anthropic-format client (Claude Code) is
|
|
5
|
+
* served by a *reasoning* upstream on the OpenAI Responses API
|
|
6
|
+
* (ChatGPT/Codex, `store: false` + `include: ["reasoning.encrypted_content"]`),
|
|
7
|
+
* the model emits a `reasoning` output item whose `encrypted_content`
|
|
8
|
+
* encodes its chain-of-thought for the turn. The Responses API REQUIRES
|
|
9
|
+
* that item to be echoed back, in order, immediately before its
|
|
10
|
+
* `function_call`, on the next request. If it is dropped the model
|
|
11
|
+
* restarts reasoning every turn, re-derives the identical tool call,
|
|
12
|
+
* and loops forever (the "70k tokens, never exits" symptom).
|
|
13
|
+
*
|
|
14
|
+
* Anthropic's wire format has exactly one channel that a client
|
|
15
|
+
* preserves and replays verbatim across a tool turn: the `thinking`
|
|
16
|
+
* block's opaque `signature`. So we smuggle the litellm-shaped
|
|
17
|
+
* `reasoning` item(s) through that field — encoded here on the way out,
|
|
18
|
+
* decoded here on the way back in. Both directions go through this one
|
|
19
|
+
* module so the encode/decode contract can never drift.
|
|
20
|
+
*
|
|
21
|
+
* Mirrors litellm `completion_extras/litellm_responses_transformation/
|
|
22
|
+
* transformation.py`: `_build_reasoning_item` (~69-96) and
|
|
23
|
+
* `_reasoning_item_to_response_input` (~99-111).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
// runtime-only: litellm `ChatCompletionReasoningItem` shape.
|
|
27
|
+
export type TReasoningSummaryPart = {
|
|
28
|
+
readonly type: string;
|
|
29
|
+
readonly text: string;
|
|
30
|
+
};
|
|
31
|
+
export type TReasoningItem = {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly type: "reasoning";
|
|
34
|
+
readonly encrypted_content: string | null;
|
|
35
|
+
readonly summary: ReadonlyArray<TReasoningSummaryPart>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// runtime-only: a `reasoning` item in the Responses API `input` array.
|
|
39
|
+
export type TReasoningResponsesInput = {
|
|
40
|
+
readonly type: "reasoning";
|
|
41
|
+
readonly id?: string;
|
|
42
|
+
readonly summary: ReadonlyArray<TReasoningSummaryPart>;
|
|
43
|
+
readonly encrypted_content?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Tag prefix so {@link decodeReasoningSignature} only ever consumes a
|
|
48
|
+
* signature WE produced. A genuine Anthropic `signature` (native
|
|
49
|
+
* pass-through path) lacks this prefix and is left untouched.
|
|
50
|
+
*/
|
|
51
|
+
const SIGNATURE_PREFIX = "openllm-rs1:";
|
|
52
|
+
const NATIVE_ASSISTANT_SIGNATURE_PREFIX = "openllm-cn1:";
|
|
53
|
+
export const CLAUDE_NATIVE_ASSISTANT_CARRIER = "claude_native_assistant";
|
|
54
|
+
export const CLAUDE_NATIVE_ASSISTANT_PENDING =
|
|
55
|
+
"claude_native_assistant_pending";
|
|
56
|
+
|
|
57
|
+
/** Native assistant history is NOT Responses ciphertext. Keep its wire
|
|
58
|
+
* envelope separate; the Claude facade validates model/projection/blocks
|
|
59
|
+
* before restoring it. This codec only transports the opaque carrier. */
|
|
60
|
+
export const encodeNativeAssistantSignature = (
|
|
61
|
+
items: ReadonlyArray<unknown> | null | undefined,
|
|
62
|
+
): string | null => {
|
|
63
|
+
const native = (items ?? []).filter(
|
|
64
|
+
(item) =>
|
|
65
|
+
typeof item === "object" &&
|
|
66
|
+
item !== null &&
|
|
67
|
+
(item as { type?: unknown }).type === CLAUDE_NATIVE_ASSISTANT_CARRIER,
|
|
68
|
+
);
|
|
69
|
+
return native.length > 0
|
|
70
|
+
? NATIVE_ASSISTANT_SIGNATURE_PREFIX + toBase64(JSON.stringify(native))
|
|
71
|
+
: null;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const decodeNativeAssistantSignature = (
|
|
75
|
+
signature: string | null | undefined,
|
|
76
|
+
): ReadonlyArray<unknown> | null => {
|
|
77
|
+
if (
|
|
78
|
+
typeof signature !== "string" ||
|
|
79
|
+
!signature.startsWith(NATIVE_ASSISTANT_SIGNATURE_PREFIX)
|
|
80
|
+
)
|
|
81
|
+
return null;
|
|
82
|
+
try {
|
|
83
|
+
const parsed: unknown = JSON.parse(
|
|
84
|
+
fromBase64(signature.slice(NATIVE_ASSISTANT_SIGNATURE_PREFIX.length)),
|
|
85
|
+
);
|
|
86
|
+
if (!Array.isArray(parsed) || parsed.length === 0) return null;
|
|
87
|
+
return parsed.every(
|
|
88
|
+
(item) =>
|
|
89
|
+
typeof item === "object" &&
|
|
90
|
+
item !== null &&
|
|
91
|
+
(item as { type?: unknown }).type === CLAUDE_NATIVE_ASSISTANT_CARRIER,
|
|
92
|
+
)
|
|
93
|
+
? parsed
|
|
94
|
+
: null;
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const toBase64 = (s: string): string => {
|
|
101
|
+
if (typeof Buffer !== "undefined") {
|
|
102
|
+
return Buffer.from(s, "utf8").toString("base64");
|
|
103
|
+
}
|
|
104
|
+
return btoa(unescape(encodeURIComponent(s)));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const fromBase64 = (s: string): string => {
|
|
108
|
+
if (typeof Buffer !== "undefined") {
|
|
109
|
+
return Buffer.from(s, "base64").toString("utf8");
|
|
110
|
+
}
|
|
111
|
+
return decodeURIComponent(escape(atob(s)));
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const normalizeSummary = (raw: unknown): TReasoningSummaryPart[] => {
|
|
115
|
+
if (!Array.isArray(raw)) return [];
|
|
116
|
+
const out: TReasoningSummaryPart[] = [];
|
|
117
|
+
for (const s of raw) {
|
|
118
|
+
if (s !== null && typeof s === "object" && !Array.isArray(s)) {
|
|
119
|
+
const o = s as Record<string, unknown>;
|
|
120
|
+
out.push({
|
|
121
|
+
type: typeof o.type === "string" ? o.type : "summary_text",
|
|
122
|
+
text: typeof o.text === "string" ? o.text : "",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Build a {@link TReasoningItem} from raw Responses-API output. Mirrors
|
|
131
|
+
* litellm `_build_reasoning_item`.
|
|
132
|
+
*/
|
|
133
|
+
export const buildReasoningItem = (
|
|
134
|
+
id: string,
|
|
135
|
+
encryptedContent: string | null | undefined,
|
|
136
|
+
summaryRaw: unknown,
|
|
137
|
+
): TReasoningItem => ({
|
|
138
|
+
id,
|
|
139
|
+
type: "reasoning",
|
|
140
|
+
encrypted_content:
|
|
141
|
+
typeof encryptedContent === "string" ? encryptedContent : null,
|
|
142
|
+
summary: normalizeSummary(summaryRaw),
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Coerce the canonical `reasoning_items` carrier (`S.Array(S.Unknown)`
|
|
147
|
+
* on the chat schema) into typed reasoning items. The single
|
|
148
|
+
* normalizer used everywhere a reasoning item leaves the canonical
|
|
149
|
+
* shape (Anthropic signature emit + Responses `input` build).
|
|
150
|
+
*/
|
|
151
|
+
export const reasoningItemsFromUnknown = (
|
|
152
|
+
items: ReadonlyArray<unknown> | null | undefined,
|
|
153
|
+
): TReasoningItem[] => {
|
|
154
|
+
if (items === undefined || items === null || !Array.isArray(items)) {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
const out: TReasoningItem[] = [];
|
|
158
|
+
for (const item of items) {
|
|
159
|
+
if (item === null || typeof item !== "object") continue;
|
|
160
|
+
const rec = item as Record<string, unknown>;
|
|
161
|
+
if (rec.type !== "reasoning") continue;
|
|
162
|
+
out.push(
|
|
163
|
+
buildReasoningItem(
|
|
164
|
+
typeof rec.id === "string" ? rec.id : "",
|
|
165
|
+
typeof rec.encrypted_content === "string"
|
|
166
|
+
? rec.encrypted_content
|
|
167
|
+
: null,
|
|
168
|
+
rec.summary,
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Encode reasoning items into an Anthropic `thinking.signature`. Returns
|
|
177
|
+
* `null` when there is nothing worth round-tripping (no item carries
|
|
178
|
+
* `encrypted_content`, so the upstream cannot resume from it anyway).
|
|
179
|
+
*/
|
|
180
|
+
export const encodeReasoningSignature = (
|
|
181
|
+
items: ReadonlyArray<TReasoningItem>,
|
|
182
|
+
): string | null => {
|
|
183
|
+
const withState = items.filter(
|
|
184
|
+
(i) =>
|
|
185
|
+
typeof i.encrypted_content === "string" && i.encrypted_content.length > 0,
|
|
186
|
+
);
|
|
187
|
+
if (withState.length === 0) return null;
|
|
188
|
+
return SIGNATURE_PREFIX + toBase64(JSON.stringify(withState));
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Decode a `thinking.signature` produced by
|
|
193
|
+
* {@link encodeReasoningSignature}. Returns `null` for any signature we
|
|
194
|
+
* did not produce (genuine Anthropic signatures, malformed input) so
|
|
195
|
+
* callers fall back to their existing `reasoning_content` text path.
|
|
196
|
+
*/
|
|
197
|
+
export const decodeReasoningSignature = (
|
|
198
|
+
signature: string | null | undefined,
|
|
199
|
+
): TReasoningItem[] | null => {
|
|
200
|
+
if (
|
|
201
|
+
typeof signature !== "string" ||
|
|
202
|
+
!signature.startsWith(SIGNATURE_PREFIX)
|
|
203
|
+
) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const json = fromBase64(signature.slice(SIGNATURE_PREFIX.length));
|
|
208
|
+
const parsed: unknown = JSON.parse(json);
|
|
209
|
+
if (!Array.isArray(parsed)) return null;
|
|
210
|
+
const items = reasoningItemsFromUnknown(parsed);
|
|
211
|
+
return items.length > 0 ? items : null;
|
|
212
|
+
} catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Convert a stored reasoning item back to a Responses API `input` item.
|
|
219
|
+
* Mirrors litellm `_reasoning_item_to_response_input`: `summary` is
|
|
220
|
+
* always required (even empty); `encrypted_content` is only sent when
|
|
221
|
+
* present. The ChatGPT/Codex encoder always sends `store: false`, so an
|
|
222
|
+
* item without resumable ciphertext must not name an unpersisted item via
|
|
223
|
+
* `id` (the upstream rejects that combination).
|
|
224
|
+
*/
|
|
225
|
+
export const reasoningItemToResponsesInput = (
|
|
226
|
+
item: TReasoningItem,
|
|
227
|
+
): TReasoningResponsesInput => {
|
|
228
|
+
const encryptedContent =
|
|
229
|
+
typeof item.encrypted_content === "string" &&
|
|
230
|
+
item.encrypted_content.length > 0
|
|
231
|
+
? item.encrypted_content
|
|
232
|
+
: null;
|
|
233
|
+
return {
|
|
234
|
+
type: "reasoning",
|
|
235
|
+
summary: item.summary,
|
|
236
|
+
...(encryptedContent !== null
|
|
237
|
+
? {
|
|
238
|
+
id: item.id.length > 0 ? item.id : `rs_${crypto.randomUUID()}`,
|
|
239
|
+
encrypted_content: encryptedContent,
|
|
240
|
+
}
|
|
241
|
+
: {}),
|
|
242
|
+
};
|
|
243
|
+
};
|