@tormentalabs/claude-code-wire-compat 0.7.1 → 0.8.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/CHANGELOG.md +74 -0
- package/README.md +27 -4
- package/dist/account-features.d.ts +11 -0
- package/dist/account-features.d.ts.map +1 -0
- package/dist/account-features.js +98 -0
- package/dist/account-features.js.map +1 -0
- package/dist/anti-verbosity.d.ts.map +1 -1
- package/dist/anti-verbosity.js +3 -3
- package/dist/anti-verbosity.js.map +1 -1
- package/dist/betas.d.ts +74 -2
- package/dist/betas.d.ts.map +1 -1
- package/dist/betas.js +158 -18
- package/dist/betas.js.map +1 -1
- package/dist/build-request.d.ts.map +1 -1
- package/dist/build-request.js +201 -19
- package/dist/build-request.js.map +1 -1
- package/dist/contracts.d.ts +181 -2
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js.map +1 -1
- package/dist/fingerprint.d.ts +28 -5
- package/dist/fingerprint.d.ts.map +1 -1
- package/dist/fingerprint.js +63 -5
- package/dist/fingerprint.js.map +1 -1
- package/dist/headers.d.ts +60 -1
- package/dist/headers.d.ts.map +1 -1
- package/dist/headers.js +176 -23
- package/dist/headers.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/model-capabilities.d.ts +8 -4
- package/dist/model-capabilities.d.ts.map +1 -1
- package/dist/model-capabilities.js +14 -7
- package/dist/model-capabilities.js.map +1 -1
- package/dist/model-identity.d.ts +15 -2
- package/dist/model-identity.d.ts.map +1 -1
- package/dist/model-identity.js +44 -1
- package/dist/model-identity.js.map +1 -1
- package/dist/model-queries.d.ts +11 -1
- package/dist/model-queries.d.ts.map +1 -1
- package/dist/model-queries.js +51 -7
- package/dist/model-queries.js.map +1 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +2 -2
- package/dist/models.js.map +1 -1
- package/dist/profile-behaviors.d.ts +72 -0
- package/dist/profile-behaviors.d.ts.map +1 -1
- package/dist/profile-behaviors.js +66 -15
- package/dist/profile-behaviors.js.map +1 -1
- package/dist/profiles/beta-registry-2.1.329.d.ts +229 -0
- package/dist/profiles/beta-registry-2.1.329.d.ts.map +1 -0
- package/dist/profiles/beta-registry-2.1.329.js +281 -0
- package/dist/profiles/beta-registry-2.1.329.js.map +1 -0
- package/dist/profiles/claude-code-2.1.329.d.ts +3 -0
- package/dist/profiles/claude-code-2.1.329.d.ts.map +1 -0
- package/dist/profiles/claude-code-2.1.329.js +466 -0
- package/dist/profiles/claude-code-2.1.329.js.map +1 -0
- package/dist/redaction.d.ts.map +1 -1
- package/dist/redaction.js +1 -0
- package/dist/redaction.js.map +1 -1
- package/dist/request-body.d.ts +27 -1
- package/dist/request-body.d.ts.map +1 -1
- package/dist/request-body.js +184 -16
- package/dist/request-body.js.map +1 -1
- package/dist/tools-used.d.ts +52 -0
- package/dist/tools-used.d.ts.map +1 -0
- package/dist/tools-used.js +116 -0
- package/dist/tools-used.js.map +1 -0
- package/package.json +5 -1
- package/src/account-features.ts +114 -0
- package/src/anti-verbosity.ts +8 -3
- package/src/betas.ts +252 -22
- package/src/build-request.ts +245 -22
- package/src/contracts.ts +185 -3
- package/src/fingerprint.ts +84 -7
- package/src/headers.ts +221 -21
- package/src/index.ts +10 -0
- package/src/model-capabilities.ts +16 -7
- package/src/model-identity.ts +62 -2
- package/src/model-queries.ts +71 -8
- package/src/models.ts +2 -1
- package/src/profile-behaviors.ts +150 -16
- package/src/profiles/beta-registry-2.1.329.ts +298 -0
- package/src/profiles/claude-code-2.1.329.ts +471 -0
- package/src/redaction.ts +1 -0
- package/src/request-body.ts +242 -17
- package/src/tools-used.ts +134 -0
package/src/build-request.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
import { MAX_COMPOSITE_SIZE, MAX_INPUT_SIZE } from "./limits.js";
|
|
4
4
|
|
|
5
|
+
import { validateAccountFeatures } from "./account-features.js";
|
|
5
6
|
import { composeBetas, composeBetasWithAudit } from "./betas.js";
|
|
6
7
|
import type {
|
|
7
8
|
BuiltClaudeCodeCountTokensRequest,
|
|
8
9
|
BuiltClaudeCodeRequest,
|
|
10
|
+
ClaudeCodeAccountFeatures,
|
|
9
11
|
ClaudeCodeBetaOverrides,
|
|
10
12
|
ClaudeCodeCapabilities,
|
|
11
13
|
ClaudeCodeCapabilityDecisions,
|
|
@@ -14,6 +16,7 @@ import type {
|
|
|
14
16
|
ClaudeCodeProtocolProfile,
|
|
15
17
|
ClaudeCodeCountTokensInput,
|
|
16
18
|
ClaudeCodeRequestInput,
|
|
19
|
+
ClaudeCodeTurnPosition,
|
|
17
20
|
HeaderPair,
|
|
18
21
|
RedactedRequestEvidence,
|
|
19
22
|
} from "./contracts.js";
|
|
@@ -23,25 +26,36 @@ import {
|
|
|
23
26
|
filterCountTokensBetas,
|
|
24
27
|
TOKEN_COUNTING_BETA,
|
|
25
28
|
} from "./count-tokens.js";
|
|
26
|
-
import { createBillingBlock } from "./fingerprint.js";
|
|
27
|
-
import {
|
|
29
|
+
import { CCH_PATTERN, createBillingBlock } from "./fingerprint.js";
|
|
30
|
+
import {
|
|
31
|
+
buildOrderedHeaderPlan,
|
|
32
|
+
buildOrderedHeaders,
|
|
33
|
+
GENUINE_HEADER_NAMES,
|
|
34
|
+
GENUINE_RESERVED_NAMES,
|
|
35
|
+
headerNamesFor,
|
|
36
|
+
} from "./headers.js";
|
|
28
37
|
import {
|
|
29
38
|
buildCorrelatedMetadata,
|
|
30
39
|
validateRuntimeIdentity,
|
|
31
40
|
} from "./metadata.js";
|
|
32
41
|
import { resolveModel } from "./models.js";
|
|
42
|
+
import { profileBehaviors } from "./profile-behaviors.js";
|
|
33
43
|
import { CLAUDE_CODE_2_1_195_PROFILE } from "./profiles/claude-code-2.1.195.js";
|
|
34
44
|
import { CLAUDE_CODE_2_1_233_PROFILE } from "./profiles/claude-code-2.1.233.js";
|
|
35
45
|
import { CLAUDE_CODE_2_1_280_PROFILE } from "./profiles/claude-code-2.1.280.js";
|
|
46
|
+
import { CLAUDE_CODE_2_1_329_PROFILE } from "./profiles/claude-code-2.1.329.js";
|
|
36
47
|
import type { NormalizedRequestInput } from "./redaction.js";
|
|
37
48
|
import { buildRedactedEvidence, toSafeErrorDetails } from "./redaction.js";
|
|
38
49
|
import {
|
|
39
50
|
buildCanonicalBody,
|
|
40
51
|
canonicalCountTokensLists,
|
|
52
|
+
validatePreviousMessageId,
|
|
41
53
|
} from "./request-body.js";
|
|
42
54
|
import { sha256Hex } from "./sha256.js";
|
|
43
55
|
import { buildCanonicalSystem, IDENTITY_TEXT } from "./system-prompt.js";
|
|
44
56
|
import { isThinkingActive, isThinkingDisplayActive } from "./thinking.js";
|
|
57
|
+
import type { ToolsUsedEntry } from "./tools-used.js";
|
|
58
|
+
import { parseToolsUsedHeaderValue, validateToolsUsed } from "./tools-used.js";
|
|
45
59
|
import {
|
|
46
60
|
inspectText,
|
|
47
61
|
TEXT_POLICY_IDENTIFIER,
|
|
@@ -97,8 +111,17 @@ const INPUT_KEYS = new Set([
|
|
|
97
111
|
"extraHeaderPolicy",
|
|
98
112
|
"previousRequestId",
|
|
99
113
|
"promptId",
|
|
114
|
+
"cch",
|
|
115
|
+
"turnOrigin",
|
|
116
|
+
"turnPosition",
|
|
117
|
+
"toolsUsed",
|
|
118
|
+
"previousMessageId",
|
|
119
|
+
"accountFeatures",
|
|
120
|
+
"sideQuery",
|
|
121
|
+
"agenticQuery",
|
|
100
122
|
"crypto",
|
|
101
123
|
]);
|
|
124
|
+
const TURN_POSITION_KEYS = new Set(["promptIndex", "turnIndex"]);
|
|
102
125
|
const BETA_OVERRIDE_KEYS = new Set(["use1MContext"]);
|
|
103
126
|
const METADATA_OVERRIDE_KEYS = new Set(["userId", "userIdFields"]);
|
|
104
127
|
const COUNT_TOKENS_INPUT_KEYS = new Set([
|
|
@@ -377,6 +400,7 @@ const ACCEPTED_PROFILES: ReadonlySet<ClaudeCodeProtocolProfile> = new Set([
|
|
|
377
400
|
CLAUDE_CODE_2_1_195_PROFILE,
|
|
378
401
|
CLAUDE_CODE_2_1_233_PROFILE,
|
|
379
402
|
CLAUDE_CODE_2_1_280_PROFILE,
|
|
403
|
+
CLAUDE_CODE_2_1_329_PROFILE,
|
|
380
404
|
]);
|
|
381
405
|
|
|
382
406
|
/**
|
|
@@ -394,7 +418,7 @@ const ACCEPTED_PROFILES: ReadonlySet<ClaudeCodeProtocolProfile> = new Set([
|
|
|
394
418
|
* from `src/index.ts`: the public runtime surface stays closed.
|
|
395
419
|
*/
|
|
396
420
|
export const DEFAULT_PROFILE: ClaudeCodeProtocolProfile =
|
|
397
|
-
|
|
421
|
+
CLAUDE_CODE_2_1_329_PROFILE;
|
|
398
422
|
|
|
399
423
|
function validateProfile(
|
|
400
424
|
profile: ClaudeCodeProtocolProfile,
|
|
@@ -787,10 +811,14 @@ function applyEffectiveProfileHeaders(
|
|
|
787
811
|
): readonly HeaderPair[] {
|
|
788
812
|
return Object.freeze(
|
|
789
813
|
headers.map(([name, value]): HeaderPair => {
|
|
790
|
-
|
|
814
|
+
// Case-insensitive so both layouts' spellings match (`user-agent`,
|
|
815
|
+
// `User-Agent`). Legacy output is unchanged: every legacy name is
|
|
816
|
+
// lowercase, and a caller extra can never carry either name.
|
|
817
|
+
const folded = name.toLowerCase();
|
|
818
|
+
if (folded === "user-agent") {
|
|
791
819
|
return Object.freeze([name, profile.userAgent]);
|
|
792
820
|
}
|
|
793
|
-
if (
|
|
821
|
+
if (folded === "x-stainless-package-version") {
|
|
794
822
|
return Object.freeze([name, profile.sdkVersion]);
|
|
795
823
|
}
|
|
796
824
|
return Object.freeze([name, value]);
|
|
@@ -810,6 +838,14 @@ function validateInput(input: ClaudeCodeRequestInput): {
|
|
|
810
838
|
readonly preserveThinkingBlockCacheControl: boolean;
|
|
811
839
|
readonly previousRequestId: string | undefined;
|
|
812
840
|
readonly promptId: string | undefined;
|
|
841
|
+
readonly cch: string | undefined;
|
|
842
|
+
readonly turnOrigin: string | undefined;
|
|
843
|
+
readonly turnPosition: ClaudeCodeTurnPosition | undefined;
|
|
844
|
+
readonly toolsUsed: readonly ToolsUsedEntry[] | undefined;
|
|
845
|
+
readonly previousMessageId: string | null | undefined;
|
|
846
|
+
readonly accountFeatures: ClaudeCodeAccountFeatures | undefined;
|
|
847
|
+
readonly sideQuery: boolean;
|
|
848
|
+
readonly agenticQuery: boolean;
|
|
813
849
|
} {
|
|
814
850
|
if (!isRecord(input)) fail();
|
|
815
851
|
assertExactKeys(input, INPUT_KEYS);
|
|
@@ -876,6 +912,36 @@ function validateInput(input: ClaudeCodeRequestInput): {
|
|
|
876
912
|
const promptId = Object.hasOwn(input, "promptId")
|
|
877
913
|
? validateBillingChainId(ownValue(input, "promptId"))
|
|
878
914
|
: undefined;
|
|
915
|
+
const cch = Object.hasOwn(input, "cch")
|
|
916
|
+
? validateCch(ownValue(input, "cch"))
|
|
917
|
+
: undefined;
|
|
918
|
+
const turnOrigin = Object.hasOwn(input, "turnOrigin")
|
|
919
|
+
? validateBillingChainId(ownValue(input, "turnOrigin"))
|
|
920
|
+
: undefined;
|
|
921
|
+
const turnPosition = Object.hasOwn(input, "turnPosition")
|
|
922
|
+
? validateTurnPosition(ownValue(input, "turnPosition"))
|
|
923
|
+
: undefined;
|
|
924
|
+
// Validated on every profile; only the 2.1.329 header layout emits it.
|
|
925
|
+
const toolsUsed = Object.hasOwn(input, "toolsUsed")
|
|
926
|
+
? validateToolsUsed(ownValue(input, "toolsUsed"))
|
|
927
|
+
: undefined;
|
|
928
|
+
// Validated on every profile; only the 2.1.329 body carries it.
|
|
929
|
+
const previousMessageId = Object.hasOwn(input, "previousMessageId")
|
|
930
|
+
? validatePreviousMessageId(ownValue(input, "previousMessageId"))
|
|
931
|
+
: undefined;
|
|
932
|
+
// Validated on every profile; only profiles honouring account-served inputs
|
|
933
|
+
// act on it.
|
|
934
|
+
const accountFeatures = Object.hasOwn(input, "accountFeatures")
|
|
935
|
+
? validateAccountFeatures(ownValue(input, "accountFeatures"))
|
|
936
|
+
: undefined;
|
|
937
|
+
// Validated on every profile; only profiles honouring account-served inputs
|
|
938
|
+
// act on them.
|
|
939
|
+
const sideQuery = Object.hasOwn(input, "sideQuery")
|
|
940
|
+
? validateStrictBoolean(ownValue(input, "sideQuery"))
|
|
941
|
+
: false;
|
|
942
|
+
const agenticQuery = Object.hasOwn(input, "agenticQuery")
|
|
943
|
+
? validateStrictBoolean(ownValue(input, "agenticQuery"))
|
|
944
|
+
: false;
|
|
879
945
|
return {
|
|
880
946
|
source: input,
|
|
881
947
|
clientRequestId,
|
|
@@ -888,9 +954,64 @@ function validateInput(input: ClaudeCodeRequestInput): {
|
|
|
888
954
|
preserveThinkingBlockCacheControl,
|
|
889
955
|
previousRequestId,
|
|
890
956
|
promptId,
|
|
957
|
+
cch,
|
|
958
|
+
turnOrigin,
|
|
959
|
+
turnPosition,
|
|
960
|
+
toolsUsed,
|
|
961
|
+
previousMessageId,
|
|
962
|
+
accountFeatures,
|
|
963
|
+
sideQuery,
|
|
964
|
+
agenticQuery,
|
|
891
965
|
};
|
|
892
966
|
}
|
|
893
967
|
|
|
968
|
+
function validateStrictBoolean(value: unknown): boolean {
|
|
969
|
+
if (typeof value !== "boolean") fail();
|
|
970
|
+
return value;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* Whether the caller's tool list carries the advisor tool. A defensive scan:
|
|
975
|
+
* the tool list is validated in full later, by the body builder, so a malformed
|
|
976
|
+
* entry here is simply not the advisor tool.
|
|
977
|
+
*/
|
|
978
|
+
function hasAdvisorTool(tools: unknown): boolean {
|
|
979
|
+
return (
|
|
980
|
+
Array.isArray(tools) &&
|
|
981
|
+
tools.some(
|
|
982
|
+
(tool: unknown): boolean =>
|
|
983
|
+
isRecord(tool) &&
|
|
984
|
+
Object.hasOwn(tool, "type") &&
|
|
985
|
+
tool["type"] === "advisor_20260301",
|
|
986
|
+
)
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Unlike the chaining ids, the `cch` value is validated HERE, and a malformed
|
|
992
|
+
* one is `INVALID_INPUT`: it is a value the caller computed to replace a
|
|
993
|
+
* placeholder, not conversation state upstream would silently drop, so a
|
|
994
|
+
* wrong one is a caller bug that must not pass quietly.
|
|
995
|
+
*/
|
|
996
|
+
function validateCch(value: unknown): string {
|
|
997
|
+
if (typeof value !== "string" || !CCH_PATTERN.test(value)) fail();
|
|
998
|
+
return value;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/**
|
|
1002
|
+
* Type and shape check only. The RANGE of both indexes is deliberately not
|
|
1003
|
+
* checked here: upstream guards them at the point of emission and drops the
|
|
1004
|
+
* pair silently, and `createBillingBlock` owns that guard.
|
|
1005
|
+
*/
|
|
1006
|
+
function validateTurnPosition(value: unknown): ClaudeCodeTurnPosition {
|
|
1007
|
+
if (!isRecord(value)) fail();
|
|
1008
|
+
assertExactKeys(value, TURN_POSITION_KEYS);
|
|
1009
|
+
const promptIndex = ownValue(value, "promptIndex");
|
|
1010
|
+
const turnIndex = ownValue(value, "turnIndex");
|
|
1011
|
+
if (typeof promptIndex !== "number" || typeof turnIndex !== "number") fail();
|
|
1012
|
+
return { promptIndex, turnIndex };
|
|
1013
|
+
}
|
|
1014
|
+
|
|
894
1015
|
/**
|
|
895
1016
|
* Type check only. The FORMAT of these two ids is deliberately not checked
|
|
896
1017
|
* here: upstream guards them at the point of emission and drops a value it
|
|
@@ -1327,14 +1448,69 @@ function parsedBodySessionId(body: UnknownRecord): string {
|
|
|
1327
1448
|
return sessionId;
|
|
1328
1449
|
}
|
|
1329
1450
|
|
|
1330
|
-
|
|
1331
|
-
|
|
1451
|
+
/**
|
|
1452
|
+
* Reads the one header called `name`. Under the genuine layout the match folds
|
|
1453
|
+
* case, so a request whose names were re-cased is still READ; whether it is
|
|
1454
|
+
* ACCEPTED is decided later by the exact byte comparison against the rebuilt
|
|
1455
|
+
* list. Two matches (for example a forged lowercase `user-agent` beside the
|
|
1456
|
+
* real `User-Agent`) are refused here.
|
|
1457
|
+
*/
|
|
1458
|
+
function headerValue(
|
|
1459
|
+
headers: readonly HeaderPair[],
|
|
1460
|
+
name: string,
|
|
1461
|
+
foldCase = false,
|
|
1462
|
+
): string {
|
|
1463
|
+
const wanted = foldCase ? name.toLowerCase() : name;
|
|
1464
|
+
const matches = headers.filter(
|
|
1465
|
+
([candidate]) =>
|
|
1466
|
+
(foldCase ? candidate.toLowerCase() : candidate) === wanted,
|
|
1467
|
+
);
|
|
1332
1468
|
if (matches.length !== 1) fail();
|
|
1333
1469
|
const match = matches[0];
|
|
1334
1470
|
if (match === undefined) fail();
|
|
1335
1471
|
return match[1];
|
|
1336
1472
|
}
|
|
1337
1473
|
|
|
1474
|
+
/**
|
|
1475
|
+
* The genuine-layout counterpart of `splitDynamicAndExtraHeaders`. The list is
|
|
1476
|
+
* sorted rather than positional, so the dynamic headers are looked up by their
|
|
1477
|
+
* (lowercase) names and the caller extras are whatever carries no name the
|
|
1478
|
+
* layout owns. A header with an owned name that the rebuild does not account
|
|
1479
|
+
* for makes the exact comparison fail, so nothing owned can hide among extras.
|
|
1480
|
+
*/
|
|
1481
|
+
function splitGenuineHeaders(headers: readonly HeaderPair[]): {
|
|
1482
|
+
readonly stainlessHelper: string | undefined;
|
|
1483
|
+
readonly claudeRemoteContainerId: string | undefined;
|
|
1484
|
+
readonly claudeRemoteSessionId: string | undefined;
|
|
1485
|
+
readonly clientApp: string | undefined;
|
|
1486
|
+
readonly anthropicAdditionalProtection: string | undefined;
|
|
1487
|
+
readonly promptId: string | undefined;
|
|
1488
|
+
readonly toolsUsed: readonly ToolsUsedEntry[] | undefined;
|
|
1489
|
+
readonly dispatchId: string | undefined;
|
|
1490
|
+
readonly extraHeaders: readonly HeaderPair[];
|
|
1491
|
+
} {
|
|
1492
|
+
const names = GENUINE_HEADER_NAMES;
|
|
1493
|
+
const first = (name: string): string | undefined =>
|
|
1494
|
+
headers.find(([candidate]) => candidate === name)?.[1];
|
|
1495
|
+
const toolsUsedValue = first(names.sandyPlover);
|
|
1496
|
+
return {
|
|
1497
|
+
stainlessHelper: first(names.stainlessHelper),
|
|
1498
|
+
claudeRemoteContainerId: first(names.remoteContainerId),
|
|
1499
|
+
claudeRemoteSessionId: first(names.remoteSessionId),
|
|
1500
|
+
clientApp: first(names.clientApp),
|
|
1501
|
+
anthropicAdditionalProtection: first(names.additionalProtection),
|
|
1502
|
+
promptId: first(names.promptId),
|
|
1503
|
+
dispatchId: first(names.dispatchId),
|
|
1504
|
+
toolsUsed:
|
|
1505
|
+
toolsUsedValue === undefined
|
|
1506
|
+
? undefined
|
|
1507
|
+
: parseToolsUsedHeaderValue(toolsUsedValue),
|
|
1508
|
+
extraHeaders: headers.filter(
|
|
1509
|
+
([name]) => !GENUINE_RESERVED_NAMES.has(name.toLowerCase()),
|
|
1510
|
+
),
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1338
1514
|
function splitDynamicAndExtraHeaders(headers: readonly HeaderPair[]): {
|
|
1339
1515
|
readonly stainlessHelper: string | undefined;
|
|
1340
1516
|
readonly claudeRemoteContainerId: string | undefined;
|
|
@@ -1513,6 +1689,9 @@ export async function buildClaudeCodeCountTokensRequest(
|
|
|
1513
1689
|
extraHeaders: validated.source.extraHeaders ?? [],
|
|
1514
1690
|
extraHeaderPolicy: validated.extraHeaderPolicy ?? "strict",
|
|
1515
1691
|
profile: pinnedProfile,
|
|
1692
|
+
// The genuine layout omits the request-class, prompt-id and tools-used
|
|
1693
|
+
// headers here (and, later, the dispatch id).
|
|
1694
|
+
requestKind: "count_tokens",
|
|
1516
1695
|
});
|
|
1517
1696
|
const headers = applyEffectiveProfileHeaders(
|
|
1518
1697
|
headerPlan.headers,
|
|
@@ -1618,6 +1797,13 @@ export async function buildClaudeCodeRequest(
|
|
|
1618
1797
|
...(validated.promptId !== undefined && {
|
|
1619
1798
|
promptId: validated.promptId,
|
|
1620
1799
|
}),
|
|
1800
|
+
...(validated.cch !== undefined && { cch: validated.cch }),
|
|
1801
|
+
...(validated.turnOrigin !== undefined && {
|
|
1802
|
+
turnOrigin: validated.turnOrigin,
|
|
1803
|
+
}),
|
|
1804
|
+
...(validated.turnPosition !== undefined && {
|
|
1805
|
+
turnPosition: validated.turnPosition,
|
|
1806
|
+
}),
|
|
1621
1807
|
},
|
|
1622
1808
|
);
|
|
1623
1809
|
const metadata = buildCorrelatedMetadata(
|
|
@@ -1667,6 +1853,12 @@ export async function buildClaudeCodeRequest(
|
|
|
1667
1853
|
...(validated.betaOverrides?.use1MContext === undefined
|
|
1668
1854
|
? {}
|
|
1669
1855
|
: { use1MContextOverride: validated.betaOverrides.use1MContext }),
|
|
1856
|
+
...(validated.accountFeatures === undefined
|
|
1857
|
+
? {}
|
|
1858
|
+
: { accountFeatures: validated.accountFeatures }),
|
|
1859
|
+
hasAdvisorTool: hasAdvisorTool(validated.source.tools),
|
|
1860
|
+
sideQuery: validated.sideQuery,
|
|
1861
|
+
agenticQuery: validated.agenticQuery,
|
|
1670
1862
|
},
|
|
1671
1863
|
effectiveProfile,
|
|
1672
1864
|
);
|
|
@@ -1677,6 +1869,16 @@ export async function buildClaudeCodeRequest(
|
|
|
1677
1869
|
metadata,
|
|
1678
1870
|
effectiveProfile,
|
|
1679
1871
|
composedBetas.thinkingDisplayOverride,
|
|
1872
|
+
{
|
|
1873
|
+
contextManagementComposed: composedBetas.contextManagementComposed,
|
|
1874
|
+
cacheDiagnosisComposed: composedBetas.cacheDiagnosisComposed,
|
|
1875
|
+
...(validated.previousMessageId === undefined
|
|
1876
|
+
? {}
|
|
1877
|
+
: { previousMessageId: validated.previousMessageId }),
|
|
1878
|
+
...(composedBetas.thread === undefined
|
|
1879
|
+
? {}
|
|
1880
|
+
: { thread: composedBetas.thread }),
|
|
1881
|
+
},
|
|
1680
1882
|
);
|
|
1681
1883
|
const betas = composedBetas.betas;
|
|
1682
1884
|
const headerPlan = buildOrderedHeaderPlan({
|
|
@@ -1695,6 +1897,16 @@ export async function buildClaudeCodeRequest(
|
|
|
1695
1897
|
extraHeaders: validated.source.extraHeaders ?? [],
|
|
1696
1898
|
extraHeaderPolicy: validated.extraHeaderPolicy ?? "strict",
|
|
1697
1899
|
profile: pinnedProfile,
|
|
1900
|
+
requestKind: "messages",
|
|
1901
|
+
...(validated.promptId === undefined
|
|
1902
|
+
? {}
|
|
1903
|
+
: { promptId: validated.promptId }),
|
|
1904
|
+
...(validated.toolsUsed === undefined
|
|
1905
|
+
? {}
|
|
1906
|
+
: { toolsUsed: validated.toolsUsed }),
|
|
1907
|
+
...(validated.accountFeatures?.dispatchId === undefined
|
|
1908
|
+
? {}
|
|
1909
|
+
: { dispatchId: validated.accountFeatures.dispatchId }),
|
|
1698
1910
|
});
|
|
1699
1911
|
const headers = applyEffectiveProfileHeaders(
|
|
1700
1912
|
headerPlan.headers,
|
|
@@ -1809,26 +2021,33 @@ export function parseBuiltClaudeCodeRequest(
|
|
|
1809
2021
|
) {
|
|
1810
2022
|
fail();
|
|
1811
2023
|
}
|
|
1812
|
-
|
|
1813
|
-
|
|
2024
|
+
// The layout decides which spelling of each name is read (legacy: exact
|
|
2025
|
+
// lowercase; genuine: folded, table of capitalised names).
|
|
2026
|
+
const genuine = profileBehaviors(pinnedProfile).genuineHeaderLayout;
|
|
2027
|
+
const names = headerNamesFor(pinnedProfile);
|
|
2028
|
+
const read = (name: string): string => headerValue(headers, name, genuine);
|
|
2029
|
+
const sessionId = read(names.sessionId);
|
|
2030
|
+
const additionalHeaders = genuine
|
|
2031
|
+
? splitGenuineHeaders(headers)
|
|
2032
|
+
: {
|
|
2033
|
+
...splitDynamicAndExtraHeaders(headers),
|
|
2034
|
+
promptId: undefined,
|
|
2035
|
+
toolsUsed: undefined,
|
|
2036
|
+
dispatchId: undefined,
|
|
2037
|
+
};
|
|
1814
2038
|
const expectedHeaders = buildOrderedHeaders({
|
|
1815
|
-
accessToken:
|
|
1816
|
-
/^Bearer /u,
|
|
1817
|
-
"",
|
|
1818
|
-
),
|
|
2039
|
+
accessToken: read(names.authorization).replace(/^Bearer /u, ""),
|
|
1819
2040
|
runtime: {
|
|
1820
2041
|
sessionId,
|
|
1821
|
-
runtime:
|
|
1822
|
-
runtimeVersion:
|
|
1823
|
-
os:
|
|
1824
|
-
arch:
|
|
2042
|
+
runtime: read(names.runtime),
|
|
2043
|
+
runtimeVersion: read(names.runtimeVersion),
|
|
2044
|
+
os: read(names.os),
|
|
2045
|
+
arch: read(names.arch),
|
|
1825
2046
|
},
|
|
1826
|
-
clientRequestId:
|
|
2047
|
+
clientRequestId: read(names.clientRequestId),
|
|
1827
2048
|
betaFeatures: evidence.betaFeatures,
|
|
1828
|
-
app:
|
|
1829
|
-
stainlessRetryCount: Number(
|
|
1830
|
-
headerValue(headers, "x-stainless-retry-count"),
|
|
1831
|
-
),
|
|
2049
|
+
app: read(names.app),
|
|
2050
|
+
stainlessRetryCount: Number(read(names.retryCount)),
|
|
1832
2051
|
stainlessHelper: additionalHeaders.stainlessHelper,
|
|
1833
2052
|
claudeRemoteContainerId: additionalHeaders.claudeRemoteContainerId,
|
|
1834
2053
|
claudeRemoteSessionId: additionalHeaders.claudeRemoteSessionId,
|
|
@@ -1837,6 +2056,10 @@ export function parseBuiltClaudeCodeRequest(
|
|
|
1837
2056
|
additionalHeaders.anthropicAdditionalProtection,
|
|
1838
2057
|
extraHeaders: additionalHeaders.extraHeaders,
|
|
1839
2058
|
profile: pinnedProfile,
|
|
2059
|
+
requestKind: "messages",
|
|
2060
|
+
promptId: additionalHeaders.promptId,
|
|
2061
|
+
toolsUsed: additionalHeaders.toolsUsed,
|
|
2062
|
+
dispatchId: additionalHeaders.dispatchId,
|
|
1840
2063
|
});
|
|
1841
2064
|
if (JSON.stringify(headers) !== JSON.stringify(expectedHeaders)) fail();
|
|
1842
2065
|
if (
|
package/src/contracts.ts
CHANGED
|
@@ -282,6 +282,31 @@ export interface Message {
|
|
|
282
282
|
readonly role: "user" | "assistant";
|
|
283
283
|
readonly content: MessageContent;
|
|
284
284
|
}
|
|
285
|
+
|
|
286
|
+
/** The effort statement a per-turn control message carries. */
|
|
287
|
+
export interface ClaudeCodeControlMessageOutputConfig {
|
|
288
|
+
readonly effort?: ClaudeCodeEffort;
|
|
289
|
+
readonly timing?: {
|
|
290
|
+
readonly type: "now";
|
|
291
|
+
readonly now: string | number;
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* A mid-conversation control message: the wire shape the genuine 2.1.329 client
|
|
297
|
+
* uses to change effort (or report timing) between turns without rewriting the
|
|
298
|
+
* cached prefix. It is a `role: "system"` entry of `messages` carrying
|
|
299
|
+
* `output_config` and, for a pure statement, empty `content`.
|
|
300
|
+
*
|
|
301
|
+
* Accepted only by profiles whose behaviour set honours it (2.1.329); every
|
|
302
|
+
* other profile rejects it with `INVALID_INPUT`, exactly as before. It is
|
|
303
|
+
* passed through verbatim after validation and never derived by this package.
|
|
304
|
+
*/
|
|
305
|
+
export interface ClaudeCodeControlMessage {
|
|
306
|
+
readonly role: "system";
|
|
307
|
+
readonly content: MessageContent;
|
|
308
|
+
readonly output_config: ClaudeCodeControlMessageOutputConfig;
|
|
309
|
+
}
|
|
285
310
|
export type SystemInput = string | TextBlock;
|
|
286
311
|
|
|
287
312
|
export interface ToolInputSchema {
|
|
@@ -832,11 +857,62 @@ export interface ClaudeCodeMetadataOverrides {
|
|
|
832
857
|
*/
|
|
833
858
|
export type ClaudeCodeExtraHeaderPolicy = "strict" | "dropConflicting";
|
|
834
859
|
|
|
860
|
+
/**
|
|
861
|
+
* Where a request sits in the conversation, as the genuine client counts it:
|
|
862
|
+
* which prompt of the session and which model turn within that prompt. See
|
|
863
|
+
* `ClaudeCodeRequestInput.turnPosition`.
|
|
864
|
+
*/
|
|
865
|
+
export interface ClaudeCodeTurnPosition {
|
|
866
|
+
readonly promptIndex: number;
|
|
867
|
+
readonly turnIndex: number;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* One tool the conversation has used, as reported in the
|
|
872
|
+
* `x-claude-code-sandy-plover` header. See `ClaudeCodeRequestInput.toolsUsed`.
|
|
873
|
+
*/
|
|
874
|
+
export interface ClaudeCodeToolUsed {
|
|
875
|
+
readonly name: string;
|
|
876
|
+
readonly mcp?: boolean;
|
|
877
|
+
readonly url?: string;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* The values of the `anthropic-dispatch-id` header the genuine client sends
|
|
882
|
+
* when the account is served the dispatch experiment. See
|
|
883
|
+
* `ClaudeCodeAccountFeatures.dispatchId`.
|
|
884
|
+
*/
|
|
885
|
+
export type ClaudeCodeDispatchId = "v2d" | "v2s" | "v2p";
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* Conversation-thread state for the message-threads feature. See
|
|
889
|
+
* `ClaudeCodeAccountFeatures.thread`.
|
|
890
|
+
*/
|
|
891
|
+
export type ClaudeCodeThreadInput =
|
|
892
|
+
| { readonly type: "create" }
|
|
893
|
+
| { readonly type: "continue"; readonly previousMessageId: string };
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* Features the genuine client turns on only for accounts the server has
|
|
897
|
+
* enrolled. The package cannot observe that enrolment, so each member is an
|
|
898
|
+
* explicit opt-in by the caller. See `ClaudeCodeRequestInput.accountFeatures`.
|
|
899
|
+
*/
|
|
900
|
+
export interface ClaudeCodeAccountFeatures {
|
|
901
|
+
/** Value of the `anthropic-dispatch-id` header. */
|
|
902
|
+
readonly dispatchId?: ClaudeCodeDispatchId;
|
|
903
|
+
/** Opts into the inline-tools beta. */
|
|
904
|
+
readonly inlineTools?: boolean;
|
|
905
|
+
/** Opts into the tool-provenance beta (off by default, as in the genuine client's OAuth traffic). */
|
|
906
|
+
readonly toolProvenance?: boolean;
|
|
907
|
+
/** Message-thread state. */
|
|
908
|
+
readonly thread?: ClaudeCodeThreadInput;
|
|
909
|
+
}
|
|
910
|
+
|
|
835
911
|
export interface ClaudeCodeRequestInput {
|
|
836
912
|
readonly accessToken: string;
|
|
837
913
|
readonly model: string;
|
|
838
914
|
readonly maxTokens: number;
|
|
839
|
-
readonly messages: readonly Message[];
|
|
915
|
+
readonly messages: readonly (Message | ClaudeCodeControlMessage)[];
|
|
840
916
|
readonly system?: readonly SystemInput[];
|
|
841
917
|
readonly tools?: readonly ToolDefinition[];
|
|
842
918
|
readonly cacheControl?: ClaudeCodeCacheControlInput | null;
|
|
@@ -1063,6 +1139,96 @@ export interface ClaudeCodeRequestInput {
|
|
|
1063
1139
|
* mirroring upstream. A non-string value is still `INVALID_INPUT`.
|
|
1064
1140
|
*/
|
|
1065
1141
|
readonly promptId?: string;
|
|
1142
|
+
/**
|
|
1143
|
+
* Supplies the value of the `cch=` segment of the canonical billing block.
|
|
1144
|
+
*
|
|
1145
|
+
* Exactly five lowercase hexadecimal characters; anything else is
|
|
1146
|
+
* `INVALID_INPUT`. Honoured by every profile. When omitted the segment keeps
|
|
1147
|
+
* the placeholder `00000`, so a request built without it is byte-identical to
|
|
1148
|
+
* one built before this field existed. Computing the value is the caller's
|
|
1149
|
+
* business: this package only places it. Ignored when `suppressBillingBlock`
|
|
1150
|
+
* is set, because there is no block to carry it.
|
|
1151
|
+
*/
|
|
1152
|
+
readonly cch?: string;
|
|
1153
|
+
/**
|
|
1154
|
+
* Supplies the `cc_turn_origin` segment of the canonical billing block: who
|
|
1155
|
+
* started the turn (for example `human`).
|
|
1156
|
+
*
|
|
1157
|
+
* Emitted only by profiles that carry the segment (2.1.329); older profiles
|
|
1158
|
+
* ignore it. A value that does not match `/^[a-z][a-z_]{0,31}$/` is SILENTLY
|
|
1159
|
+
* omitted, mirroring upstream. A non-string value is `INVALID_INPUT`.
|
|
1160
|
+
*/
|
|
1161
|
+
readonly turnOrigin?: string;
|
|
1162
|
+
/**
|
|
1163
|
+
* Supplies the `cc_prompt_index` and `cc_turn_index` segments of the
|
|
1164
|
+
* canonical billing block.
|
|
1165
|
+
*
|
|
1166
|
+
* Emitted only by profiles that carry the segments (2.1.329); older profiles
|
|
1167
|
+
* ignore it. Both indexes must be integers (`promptIndex` from 0, `turnIndex`
|
|
1168
|
+
* from 1, neither above ten million) or the pair is SILENTLY omitted,
|
|
1169
|
+
* mirroring upstream. A value that is not an object with exactly those two
|
|
1170
|
+
* number-typed keys is `INVALID_INPUT`.
|
|
1171
|
+
*/
|
|
1172
|
+
readonly turnPosition?: ClaudeCodeTurnPosition;
|
|
1173
|
+
/**
|
|
1174
|
+
* Supplies the tools the conversation has used, which the genuine client
|
|
1175
|
+
* reports in the `x-claude-code-sandy-plover` header.
|
|
1176
|
+
*
|
|
1177
|
+
* Emitted only by profiles that carry the header (2.1.329), where it is
|
|
1178
|
+
* always present and defaults to `[]`; older profiles validate and ignore it.
|
|
1179
|
+
* Entry order is the caller's (the genuine client lists non-built-in tools
|
|
1180
|
+
* first, which is the host's job). At most 64 entries. Each entry carries
|
|
1181
|
+
* only `name`, `mcp` and `url`; `name` and `url` are printable ASCII.
|
|
1182
|
+
* Entries with an empty name or a name over 256 bytes are SILENTLY dropped,
|
|
1183
|
+
* mirroring upstream. A serialized value over 7168 bytes is `INVALID_INPUT`
|
|
1184
|
+
* (the genuine client truncates and appends a sentinel; this package refuses
|
|
1185
|
+
* instead because the sentinel is not transcribed).
|
|
1186
|
+
*/
|
|
1187
|
+
readonly toolsUsed?: readonly ClaudeCodeToolUsed[];
|
|
1188
|
+
/**
|
|
1189
|
+
* Supplies the id of the previous response message of this conversation,
|
|
1190
|
+
* which the genuine client echoes in the body's `diagnostics` object
|
|
1191
|
+
* (`diagnostics.previous_message_id`) alongside the cache-diagnosis beta.
|
|
1192
|
+
*
|
|
1193
|
+
* Emitted only by profiles that carry the body field (2.1.329), where
|
|
1194
|
+
* `diagnostics` is always present when the beta is composed: `null` when this
|
|
1195
|
+
* field is omitted or `null` (the first request), the id otherwise. Older
|
|
1196
|
+
* profiles validate and ignore it. A string that does not match
|
|
1197
|
+
* `/^msg_[A-Za-z0-9_]{1,64}$/` is `INVALID_INPUT`, as is a collision with an
|
|
1198
|
+
* `experimentalBodyFields.diagnostics` key.
|
|
1199
|
+
*/
|
|
1200
|
+
readonly previousMessageId?: string | null;
|
|
1201
|
+
/**
|
|
1202
|
+
* Opts into features the genuine client enables per account (server-served).
|
|
1203
|
+
*
|
|
1204
|
+
* Validated on every profile (unknown keys and values outside the closed
|
|
1205
|
+
* domains are `INVALID_INPUT`); only profiles that honour account-served
|
|
1206
|
+
* inputs (2.1.329) act on it, and older profiles ignore it entirely. There:
|
|
1207
|
+
* `dispatchId` adds the `anthropic-dispatch-id` header to messages requests
|
|
1208
|
+
* (never to count-tokens); `inlineTools` composes the inline-tools beta when
|
|
1209
|
+
* the mid-conversation tool-change beta is composed; `toolProvenance`
|
|
1210
|
+
* composes the tool-provenance beta (OAuth only); `thread` composes the
|
|
1211
|
+
* message-threads beta and adds the body's `thread` object (`{type:
|
|
1212
|
+
* "create"}` or `{type: "continue", previous_message_id}`), which is dropped
|
|
1213
|
+
* again when `suppressBetas` removes that beta.
|
|
1214
|
+
*/
|
|
1215
|
+
readonly accountFeatures?: ClaudeCodeAccountFeatures;
|
|
1216
|
+
/**
|
|
1217
|
+
* Marks the request as a side query (the plugin sets it on session-title
|
|
1218
|
+
* requests), which composes the claude-code-side-query beta.
|
|
1219
|
+
*
|
|
1220
|
+
* Validated on every profile (a non-boolean is `INVALID_INPUT`); honoured
|
|
1221
|
+
* only by profiles that honour account-served inputs (2.1.329).
|
|
1222
|
+
*/
|
|
1223
|
+
readonly sideQuery?: boolean;
|
|
1224
|
+
/**
|
|
1225
|
+
* Marks the request as an agentic query, which composes the claude-code beta
|
|
1226
|
+
* for a model the per-model table excludes it from (the haiku models).
|
|
1227
|
+
*
|
|
1228
|
+
* Validated on every profile (a non-boolean is `INVALID_INPUT`); honoured
|
|
1229
|
+
* only by profiles that honour account-served inputs (2.1.329).
|
|
1230
|
+
*/
|
|
1231
|
+
readonly agenticQuery?: boolean;
|
|
1066
1232
|
/** Injects the Web Crypto provider used to hash the request body. */
|
|
1067
1233
|
readonly crypto?: Pick<Crypto, "subtle">;
|
|
1068
1234
|
}
|
|
@@ -1085,7 +1251,6 @@ export type ClaudeCodeCountTokensInput = Pick<
|
|
|
1085
1251
|
ClaudeCodeRequestInput,
|
|
1086
1252
|
| "accessToken"
|
|
1087
1253
|
| "model"
|
|
1088
|
-
| "messages"
|
|
1089
1254
|
| "tools"
|
|
1090
1255
|
| "runtime"
|
|
1091
1256
|
| "clientRequestId"
|
|
@@ -1100,7 +1265,10 @@ export type ClaudeCodeCountTokensInput = Pick<
|
|
|
1100
1265
|
| "anthropicAdditionalProtection"
|
|
1101
1266
|
| "extraHeaders"
|
|
1102
1267
|
| "extraHeaderPolicy"
|
|
1103
|
-
|
|
1268
|
+
> & {
|
|
1269
|
+
/** The count-tokens endpoint has no control-message seam. */
|
|
1270
|
+
readonly messages: readonly Message[];
|
|
1271
|
+
};
|
|
1104
1272
|
|
|
1105
1273
|
export interface BuiltClaudeCodeCountTokensRequest extends Omit<
|
|
1106
1274
|
BuiltClaudeCodeRequest,
|
|
@@ -1136,6 +1304,20 @@ export interface ClaudeCodeProtocolProfile {
|
|
|
1136
1304
|
readonly anthropicVersion: "2023-06-01";
|
|
1137
1305
|
readonly contextHintEnabled: boolean;
|
|
1138
1306
|
readonly betaPolicy: ClaudeCodeBetaPolicy;
|
|
1307
|
+
/**
|
|
1308
|
+
* The `X-Stainless-Runtime-Version` the genuine client of this release
|
|
1309
|
+
* reports. Optional profile DATA: only profiles that record it carry it, so
|
|
1310
|
+
* older profiles are unaffected. This package never applies it -- the caller
|
|
1311
|
+
* still supplies `runtime.runtimeVersion` -- it exists so a host can read the
|
|
1312
|
+
* value the genuine binary sent.
|
|
1313
|
+
*/
|
|
1314
|
+
readonly runtimeVersion?: string;
|
|
1315
|
+
/**
|
|
1316
|
+
* The OAuth scopes the genuine client requests on its authorize URL, in the
|
|
1317
|
+
* order it lists them. Optional profile DATA for a host that builds the
|
|
1318
|
+
* authorize URL; this package builds none.
|
|
1319
|
+
*/
|
|
1320
|
+
readonly oauthAuthorizeScopes?: readonly string[];
|
|
1139
1321
|
readonly supportedModels: Readonly<Record<string, ClaudeCodeCatalogueEntry>>;
|
|
1140
1322
|
}
|
|
1141
1323
|
|