@paraharness/claude 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +63 -2
- package/dist/builders/claude-account-seat-builder.d.ts +44 -0
- package/dist/builders/claude-account-seat-builder.d.ts.map +1 -0
- package/dist/builders/claude-account-seat-builder.js +2 -0
- package/dist/builders/claude-account-seat-builder.js.map +1 -0
- package/dist/builders/claude-model-builder.d.ts +52 -0
- package/dist/builders/claude-model-builder.d.ts.map +1 -0
- package/dist/builders/claude-model-builder.js +2 -0
- package/dist/builders/claude-model-builder.js.map +1 -0
- package/dist/builders/claude-model-options.d.ts +13 -0
- package/dist/builders/claude-model-options.d.ts.map +1 -0
- package/dist/builders/claude-model-options.js +2 -0
- package/dist/builders/claude-model-options.js.map +1 -0
- package/dist/builders/claude-namespace-contract.d.ts +49 -0
- package/dist/builders/claude-namespace-contract.d.ts.map +1 -0
- package/dist/builders/claude-namespace-contract.js +2 -0
- package/dist/builders/claude-namespace-contract.js.map +1 -0
- package/dist/builders/claude-namespace.d.ts +4 -0
- package/dist/builders/claude-namespace.d.ts.map +1 -0
- package/dist/builders/claude-namespace.js +9 -0
- package/dist/builders/claude-namespace.js.map +1 -0
- package/dist/builders/create-claude-account-seat-builder.d.ts +18 -0
- package/dist/builders/create-claude-account-seat-builder.d.ts.map +1 -0
- package/dist/builders/create-claude-account-seat-builder.js +44 -0
- package/dist/builders/create-claude-account-seat-builder.js.map +1 -0
- package/dist/builders/create-claude-model-builder.d.ts +19 -0
- package/dist/builders/create-claude-model-builder.d.ts.map +1 -0
- package/dist/builders/create-claude-model-builder.js +49 -0
- package/dist/builders/create-claude-model-builder.js.map +1 -0
- package/dist/driver/claude-executors.constants.d.ts +4 -0
- package/dist/driver/claude-executors.constants.d.ts.map +1 -0
- package/dist/driver/claude-executors.constants.js +4 -0
- package/dist/driver/claude-executors.constants.js.map +1 -0
- package/dist/driver/claude-http-request.d.ts +23 -0
- package/dist/driver/claude-http-request.d.ts.map +1 -0
- package/dist/driver/claude-http-request.js +2 -0
- package/dist/driver/claude-http-request.js.map +1 -0
- package/dist/driver/claude-http-response.d.ts +19 -0
- package/dist/driver/claude-http-response.d.ts.map +1 -0
- package/dist/driver/claude-http-response.js +2 -0
- package/dist/driver/claude-http-response.js.map +1 -0
- package/dist/driver/claude-http-transport.d.ts +24 -0
- package/dist/driver/claude-http-transport.d.ts.map +1 -0
- package/dist/driver/claude-http-transport.js +2 -0
- package/dist/driver/claude-http-transport.js.map +1 -0
- package/dist/driver/claude-model-driver.d.ts +71 -0
- package/dist/driver/claude-model-driver.d.ts.map +1 -0
- package/dist/driver/claude-model-driver.js +105 -0
- package/dist/driver/claude-model-driver.js.map +1 -0
- package/dist/driver/create-claude-executors.d.ts +14 -0
- package/dist/driver/create-claude-executors.d.ts.map +1 -0
- package/dist/driver/create-claude-executors.js +20 -0
- package/dist/driver/create-claude-executors.js.map +1 -0
- package/dist/driver/default-claude-http-transport.constants.d.ts +14 -0
- package/dist/driver/default-claude-http-transport.constants.d.ts.map +1 -0
- package/dist/driver/default-claude-http-transport.constants.js +38 -0
- package/dist/driver/default-claude-http-transport.constants.js.map +1 -0
- package/dist/driver/internal/build-claude-paired-fork-tool-frame.d.ts +14 -0
- package/dist/driver/internal/build-claude-paired-fork-tool-frame.d.ts.map +1 -0
- package/dist/driver/internal/build-claude-paired-fork-tool-frame.js +56 -0
- package/dist/driver/internal/build-claude-paired-fork-tool-frame.js.map +1 -0
- package/dist/driver/internal/build-claude-user-frame.d.ts +26 -0
- package/dist/driver/internal/build-claude-user-frame.d.ts.map +1 -0
- package/dist/driver/internal/build-claude-user-frame.js +29 -0
- package/dist/driver/internal/build-claude-user-frame.js.map +1 -0
- package/dist/driver/internal/claude-append-tool-result-notes.d.ts +28 -0
- package/dist/driver/internal/claude-append-tool-result-notes.d.ts.map +1 -0
- package/dist/driver/internal/claude-append-tool-result-notes.js +124 -0
- package/dist/driver/internal/claude-append-tool-result-notes.js.map +1 -0
- package/dist/driver/internal/claude-prepare-fork.d.ts +14 -0
- package/dist/driver/internal/claude-prepare-fork.d.ts.map +1 -0
- package/dist/driver/internal/claude-prepare-fork.js +59 -0
- package/dist/driver/internal/claude-prepare-fork.js.map +1 -0
- package/dist/driver/internal/claude-seat-auth.d.ts +14 -0
- package/dist/driver/internal/claude-seat-auth.d.ts.map +1 -0
- package/dist/driver/internal/claude-seat-auth.js +2 -0
- package/dist/driver/internal/claude-seat-auth.js.map +1 -0
- package/dist/driver/internal/claude-wire-converter.d.ts +27 -0
- package/dist/driver/internal/claude-wire-converter.d.ts.map +1 -0
- package/dist/driver/internal/claude-wire-converter.js +83 -0
- package/dist/driver/internal/claude-wire-converter.js.map +1 -0
- package/dist/driver/internal/compute-anthropic-turn-usage.d.ts +12 -0
- package/dist/driver/internal/compute-anthropic-turn-usage.d.ts.map +1 -0
- package/dist/driver/internal/compute-anthropic-turn-usage.js +27 -0
- package/dist/driver/internal/compute-anthropic-turn-usage.js.map +1 -0
- package/dist/driver/internal/create-anthropic-sdk-fetch.d.ts +23 -0
- package/dist/driver/internal/create-anthropic-sdk-fetch.d.ts.map +1 -0
- package/dist/driver/internal/create-anthropic-sdk-fetch.js +21 -0
- package/dist/driver/internal/create-anthropic-sdk-fetch.js.map +1 -0
- package/dist/driver/internal/decode-frame-to-anthropic-message.d.ts +14 -0
- package/dist/driver/internal/decode-frame-to-anthropic-message.d.ts.map +1 -0
- package/dist/driver/internal/decode-frame-to-anthropic-message.js +29 -0
- package/dist/driver/internal/decode-frame-to-anthropic-message.js.map +1 -0
- package/dist/driver/internal/extract-anthropic-content-blocks.d.ts +12 -0
- package/dist/driver/internal/extract-anthropic-content-blocks.d.ts.map +1 -0
- package/dist/driver/internal/extract-anthropic-content-blocks.js +36 -0
- package/dist/driver/internal/extract-anthropic-content-blocks.js.map +1 -0
- package/dist/driver/internal/extract-claude-response-body.d.ts +29 -0
- package/dist/driver/internal/extract-claude-response-body.d.ts.map +1 -0
- package/dist/driver/internal/extract-claude-response-body.js +36 -0
- package/dist/driver/internal/extract-claude-response-body.js.map +1 -0
- package/dist/driver/internal/extract-oauth-token-from-config-dir.d.ts +12 -0
- package/dist/driver/internal/extract-oauth-token-from-config-dir.d.ts.map +1 -0
- package/dist/driver/internal/extract-oauth-token-from-config-dir.js +99 -0
- package/dist/driver/internal/extract-oauth-token-from-config-dir.js.map +1 -0
- package/dist/driver/internal/extracted-anthropic-content-blocks.d.ts +15 -0
- package/dist/driver/internal/extracted-anthropic-content-blocks.d.ts.map +1 -0
- package/dist/driver/internal/extracted-anthropic-content-blocks.js +2 -0
- package/dist/driver/internal/extracted-anthropic-content-blocks.js.map +1 -0
- package/dist/driver/internal/format-claude-fork-prompt-text.d.ts +12 -0
- package/dist/driver/internal/format-claude-fork-prompt-text.d.ts.map +1 -0
- package/dist/driver/internal/format-claude-fork-prompt-text.js +20 -0
- package/dist/driver/internal/format-claude-fork-prompt-text.js.map +1 -0
- package/dist/driver/internal/parse-claude-retry-after-reset-at-ms.d.ts +11 -0
- package/dist/driver/internal/parse-claude-retry-after-reset-at-ms.d.ts.map +1 -0
- package/dist/driver/internal/parse-claude-retry-after-reset-at-ms.js +22 -0
- package/dist/driver/internal/parse-claude-retry-after-reset-at-ms.js.map +1 -0
- package/dist/driver/internal/project-canonical-to-anthropic-message.d.ts +13 -0
- package/dist/driver/internal/project-canonical-to-anthropic-message.d.ts.map +1 -0
- package/dist/driver/internal/project-canonical-to-anthropic-message.js +80 -0
- package/dist/driver/internal/project-canonical-to-anthropic-message.js.map +1 -0
- package/dist/driver/internal/resolve-anthropic-sdk-base-url.d.ts +11 -0
- package/dist/driver/internal/resolve-anthropic-sdk-base-url.d.ts.map +1 -0
- package/dist/driver/internal/resolve-anthropic-sdk-base-url.js +19 -0
- package/dist/driver/internal/resolve-anthropic-sdk-base-url.js.map +1 -0
- package/dist/driver/internal/resolve-anthropic-wire-tools.d.ts +13 -0
- package/dist/driver/internal/resolve-anthropic-wire-tools.d.ts.map +1 -0
- package/dist/driver/internal/resolve-anthropic-wire-tools.js +25 -0
- package/dist/driver/internal/resolve-anthropic-wire-tools.js.map +1 -0
- package/dist/driver/internal/resolve-claude-seat-auth.d.ts +28 -0
- package/dist/driver/internal/resolve-claude-seat-auth.d.ts.map +1 -0
- package/dist/driver/internal/resolve-claude-seat-auth.js +85 -0
- package/dist/driver/internal/resolve-claude-seat-auth.js.map +1 -0
- package/dist/driver/internal/send-claude-messages-via-sdk.d.ts +15 -0
- package/dist/driver/internal/send-claude-messages-via-sdk.d.ts.map +1 -0
- package/dist/driver/internal/send-claude-messages-via-sdk.js +98 -0
- package/dist/driver/internal/send-claude-messages-via-sdk.js.map +1 -0
- package/dist/driver/internal/slice-claude-parent-chain.d.ts +12 -0
- package/dist/driver/internal/slice-claude-parent-chain.d.ts.map +1 -0
- package/dist/driver/internal/slice-claude-parent-chain.js +23 -0
- package/dist/driver/internal/slice-claude-parent-chain.js.map +1 -0
- package/dist/driver/internal/transform-claude-fork-frame.d.ts +27 -0
- package/dist/driver/internal/transform-claude-fork-frame.d.ts.map +1 -0
- package/dist/driver/internal/transform-claude-fork-frame.js +61 -0
- package/dist/driver/internal/transform-claude-fork-frame.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -3
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { buildClaudePairedForkToolFrame } from './build-claude-paired-fork-tool-frame.js';
|
|
2
|
+
import { buildClaudeUserFrame } from './build-claude-user-frame.js';
|
|
3
|
+
import { formatClaudeForkPromptText } from './format-claude-fork-prompt-text.js';
|
|
4
|
+
import { sliceClaudeParentChain } from './slice-claude-parent-chain.js';
|
|
5
|
+
import { transformClaudeForkFrame } from './transform-claude-fork-frame.js';
|
|
6
|
+
/**
|
|
7
|
+
* Synthesizes the initial `WireFrameRecord[]` chain for an Anthropic downward fork or cross-model escalation.
|
|
8
|
+
*
|
|
9
|
+
* Handles `'shared-prefix-only'` clean starts, `'clone-transcript'` with `substituteTombstones` (`false` vs `true`),
|
|
10
|
+
* and full-turn `(1..k)` `tool_result` pairing whenever `ctx.forkCallId` is set on an assistant tool-call turn.
|
|
11
|
+
*
|
|
12
|
+
* @param {ForkContext} ctx - Fork lineage, history mode, and assistant turn-batch metadata.
|
|
13
|
+
* @param {PrepareForkTarget} target - Target model configuration and `substituteTombstones` flag.
|
|
14
|
+
*
|
|
15
|
+
* @returns {readonly WireFrameRecord[]} Synthesized wire frame chain to persist in `LedgerStore`.
|
|
16
|
+
*/
|
|
17
|
+
export function claudePrepareFork(ctx, target) {
|
|
18
|
+
const combinedPrompt = formatClaudeForkPromptText(ctx);
|
|
19
|
+
if (ctx.historyMode === 'shared-prefix-only') {
|
|
20
|
+
if (combinedPrompt.length === 0) {
|
|
21
|
+
return Object.freeze([]);
|
|
22
|
+
}
|
|
23
|
+
return Object.freeze([
|
|
24
|
+
buildClaudeUserFrame({
|
|
25
|
+
messageId: `msg:fork:${target.modelId}:turn-0`,
|
|
26
|
+
parentMessageId: null,
|
|
27
|
+
text: combinedPrompt,
|
|
28
|
+
}),
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
const sliced = sliceClaudeParentChain(ctx);
|
|
32
|
+
const frames = [];
|
|
33
|
+
for (let idx = 0; idx < sliced.length; idx += 1) {
|
|
34
|
+
const sourceFrame = sliced[idx];
|
|
35
|
+
if (sourceFrame !== undefined) {
|
|
36
|
+
frames.push(transformClaudeForkFrame({
|
|
37
|
+
frame: sourceFrame,
|
|
38
|
+
substituteTombstones: target.substituteTombstones,
|
|
39
|
+
isForkPoint: idx === sliced.length - 1,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const lastFrame = frames[frames.length - 1];
|
|
44
|
+
if (lastFrame !== undefined &&
|
|
45
|
+
(lastFrame.canonical.toolCalls?.length ?? 0) > 0 &&
|
|
46
|
+
ctx.forkCallId !== undefined) {
|
|
47
|
+
frames.push(buildClaudePairedForkToolFrame(ctx, lastFrame));
|
|
48
|
+
}
|
|
49
|
+
else if (ctx.childRoleContextFrameText !== undefined &&
|
|
50
|
+
ctx.childRoleContextFrameText.length > 0) {
|
|
51
|
+
frames.push(buildClaudeUserFrame({
|
|
52
|
+
messageId: `${lastFrame?.messageId ?? 'root'}:fork-context`,
|
|
53
|
+
parentMessageId: lastFrame?.messageId ?? null,
|
|
54
|
+
text: combinedPrompt,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
return Object.freeze(frames);
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=claude-prepare-fork.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-prepare-fork.js","sourceRoot":"","sources":["../../../src/driver/internal/claude-prepare-fork.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,8BAA8B,EAAE,MAAM,0CAA0C,CAAC;AAC1F,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,0BAA0B,EAAE,MAAM,qCAAqC,CAAC;AACjF,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AAE5E;;;;;;;;;;GAUG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAAgB,EAChB,MAAyB;IAEzB,MAAM,cAAc,GAAG,0BAA0B,CAAC,GAAG,CAAC,CAAC;IACvD,IAAI,GAAG,CAAC,WAAW,KAAK,oBAAoB,EAAE,CAAC;QAC7C,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAChC,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAC3B,CAAC;QACD,OAAO,MAAM,CAAC,MAAM,CAAC;YACnB,oBAAoB,CAAC;gBACnB,SAAS,EAAE,YAAY,MAAM,CAA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E,cAAc;SACrB,CAAC,CACH,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolved Anthropic authentication headers and active seat metadata for a single turn invocation.
|
|
3
|
+
*
|
|
4
|
+
* Produced by `resolveClaudeSeatAuth()` before dispatching an Anthropic `/v1/messages` request.
|
|
5
|
+
*/
|
|
6
|
+
export interface ClaudeSeatAuth {
|
|
7
|
+
/** Active `ClaudeAccountSeatNode` identifier selected for the turn, when available. */
|
|
8
|
+
readonly seatId: string | undefined;
|
|
9
|
+
/** Authentication mechanism applied (`'cli-oauth'`, `'api-key'`, or `'none'`). */
|
|
10
|
+
readonly authSource: 'cli-oauth' | 'api-key' | 'none';
|
|
11
|
+
/** Outbound HTTP request headers including `anthropic-version` and credentials. */
|
|
12
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=claude-seat-auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-seat-auth.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/claude-seat-auth.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,uFAAuF;IACvF,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,kFAAkF;IAClF,QAAQ,CAAC,UAAU,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,CAAC;IACtD,mFAAmF;IACnF,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACpD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-seat-auth.js","sourceRoot":"","sources":["../../../src/driver/internal/claude-seat-auth.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ModelTurnRequest, type ModelTurnResponse } from '@paraharness/core';
|
|
2
|
+
/**
|
|
3
|
+
* Assembles an Anthropic `/v1/messages` request payload from a `ModelTurnRequest`.
|
|
4
|
+
*
|
|
5
|
+
* Pins `system` and `tools` from `CompiledWirePrefix`, preserves `tools` when `toolChoice === 'none'`,
|
|
6
|
+
* and configures native `output_config.format` JSON-schema enforcement when `outputSchema` is present.
|
|
7
|
+
*
|
|
8
|
+
* @param {ModelTurnRequest} req - Turn invocation request parameters.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Readonly<Record<string, unknown>>} Serialized Anthropic `/v1/messages` request body.
|
|
11
|
+
*/
|
|
12
|
+
export declare function buildAnthropicMessagesRequest(req: ModelTurnRequest): Readonly<Record<string, unknown>>;
|
|
13
|
+
/**
|
|
14
|
+
* Parses an Anthropic `/v1/messages` response payload into a `ModelTurnResponse`.
|
|
15
|
+
*
|
|
16
|
+
* Preserves exact `thinking` and `signature` blocks in `wireFrame`, extracts `tool_use` calls into
|
|
17
|
+
* `canonical.toolCalls`, and validates `structuredOutput` when `canonical.toolCalls` is empty.
|
|
18
|
+
*
|
|
19
|
+
* @param {Readonly<Record<string, unknown>>} responseBody - Raw JSON object returned by `/v1/messages`.
|
|
20
|
+
* @param {ModelTurnRequest} req - Originating turn request with optional `outputSchema`.
|
|
21
|
+
*
|
|
22
|
+
* @returns {ModelTurnResponse} Parsed turn response with raw wire bytes, canonical message, and usage.
|
|
23
|
+
*
|
|
24
|
+
* @throws {Error} If `req.outputSchema` is present and the response JSON fails schema validation.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseAnthropicMessagesResponse(responseBody: Readonly<Record<string, unknown>>, req: ModelTurnRequest): ModelTurnResponse;
|
|
27
|
+
//# sourceMappingURL=claude-wire-converter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-wire-converter.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/claude-wire-converter.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACvB,MAAM,mBAAmB,CAAC;AAU3B;;;;;;;;;GASG;AACH,wBAAgB,6BAA6B,CAC3C,GAAG,EAAE,gBAAgB,GACpB,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAgCnC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,8BAA8B,CAC5C,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC/C,GAAG,EAAE,gBAAgB,GACpB,iBAAiB,CAsBnB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { zodToJsonSchema, } from '@paraharness/core';
|
|
2
|
+
import { CLAUDE_THINKING_BUDGETS, DEFAULT_CLAUDE_MAX_TOKENS, } from '../default-claude-http-transport.constants.js';
|
|
3
|
+
import { computeAnthropicTurnUsage } from './compute-anthropic-turn-usage.js';
|
|
4
|
+
import { decodeFrameToAnthropicMessage } from './decode-frame-to-anthropic-message.js';
|
|
5
|
+
import { extractAnthropicContentBlocks } from './extract-anthropic-content-blocks.js';
|
|
6
|
+
import { resolveAnthropicWireTools } from './resolve-anthropic-wire-tools.js';
|
|
7
|
+
/**
|
|
8
|
+
* Assembles an Anthropic `/v1/messages` request payload from a `ModelTurnRequest`.
|
|
9
|
+
*
|
|
10
|
+
* Pins `system` and `tools` from `CompiledWirePrefix`, preserves `tools` when `toolChoice === 'none'`,
|
|
11
|
+
* and configures native `output_config.format` JSON-schema enforcement when `outputSchema` is present.
|
|
12
|
+
*
|
|
13
|
+
* @param {ModelTurnRequest} req - Turn invocation request parameters.
|
|
14
|
+
*
|
|
15
|
+
* @returns {Readonly<Record<string, unknown>>} Serialized Anthropic `/v1/messages` request body.
|
|
16
|
+
*/
|
|
17
|
+
export function buildAnthropicMessagesRequest(req) {
|
|
18
|
+
const messages = [];
|
|
19
|
+
for (const frame of req.history) {
|
|
20
|
+
messages.push(decodeFrameToAnthropicMessage(frame));
|
|
21
|
+
}
|
|
22
|
+
const tools = resolveAnthropicWireTools(req);
|
|
23
|
+
const body = {
|
|
24
|
+
model: req.modelId,
|
|
25
|
+
max_tokens: DEFAULT_CLAUDE_MAX_TOKENS,
|
|
26
|
+
messages: Object.freeze(messages),
|
|
27
|
+
};
|
|
28
|
+
if (req.wirePrefix !== undefined && req.wirePrefix.systemPromptText.length > 0) {
|
|
29
|
+
body['system'] = req.wirePrefix.systemPromptText;
|
|
30
|
+
}
|
|
31
|
+
if (tools.length > 0) {
|
|
32
|
+
body['tools'] = tools;
|
|
33
|
+
}
|
|
34
|
+
if (req.toolChoice !== undefined) {
|
|
35
|
+
body['tool_choice'] = Object.freeze({ type: req.toolChoice });
|
|
36
|
+
}
|
|
37
|
+
if (req.effort !== undefined) {
|
|
38
|
+
body['thinking'] = Object.freeze({
|
|
39
|
+
type: 'enabled',
|
|
40
|
+
budget_tokens: CLAUDE_THINKING_BUDGETS[req.effort],
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (req.outputSchema !== undefined) {
|
|
44
|
+
body['output_config'] = Object.freeze({
|
|
45
|
+
format: Object.freeze({ type: 'json_schema', schema: zodToJsonSchema(req.outputSchema) }),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return Object.freeze(body);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Parses an Anthropic `/v1/messages` response payload into a `ModelTurnResponse`.
|
|
52
|
+
*
|
|
53
|
+
* Preserves exact `thinking` and `signature` blocks in `wireFrame`, extracts `tool_use` calls into
|
|
54
|
+
* `canonical.toolCalls`, and validates `structuredOutput` when `canonical.toolCalls` is empty.
|
|
55
|
+
*
|
|
56
|
+
* @param {Readonly<Record<string, unknown>>} responseBody - Raw JSON object returned by `/v1/messages`.
|
|
57
|
+
* @param {ModelTurnRequest} req - Originating turn request with optional `outputSchema`.
|
|
58
|
+
*
|
|
59
|
+
* @returns {ModelTurnResponse} Parsed turn response with raw wire bytes, canonical message, and usage.
|
|
60
|
+
*
|
|
61
|
+
* @throws {Error} If `req.outputSchema` is present and the response JSON fails schema validation.
|
|
62
|
+
*/
|
|
63
|
+
export function parseAnthropicMessagesResponse(responseBody, req) {
|
|
64
|
+
const { rawContent, visibleText, toolCalls } = extractAnthropicContentBlocks(responseBody);
|
|
65
|
+
const wireFrame = new TextEncoder().encode(JSON.stringify({ role: 'assistant', content: rawContent }));
|
|
66
|
+
const shouldParseOutput = toolCalls.length === 0 && req.outputSchema !== undefined && visibleText.trim().length > 0;
|
|
67
|
+
const structuredOutput = shouldParseOutput
|
|
68
|
+
? req.outputSchema?.parse(JSON.parse(visibleText))
|
|
69
|
+
: undefined;
|
|
70
|
+
const canonical = Object.freeze({
|
|
71
|
+
role: 'assistant',
|
|
72
|
+
visibleText,
|
|
73
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
74
|
+
toolResults: undefined,
|
|
75
|
+
});
|
|
76
|
+
return Object.freeze({
|
|
77
|
+
wireFrame,
|
|
78
|
+
canonical,
|
|
79
|
+
structuredOutput,
|
|
80
|
+
usage: computeAnthropicTurnUsage(responseBody),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=claude-wire-converter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-wire-converter.js","sourceRoot":"","sources":["../../../src/driver/internal/claude-wire-converter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,GAIhB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC;AAC9E,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AACvF,OAAO,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AACtF,OAAO,EAAE,y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QACT,SAAS;QACT,gBAAgB;QAChB,KAAK,EAAE,yBAAyB,CAAC,YAAY,CAAC;KAC/C,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ModelTurnResponse } from '@paraharness/core';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts token usage and USD cost metrics from an Anthropic `/v1/messages` response payload.
|
|
4
|
+
*
|
|
5
|
+
* Reads `input_tokens`, `cache_read_input_tokens`, `output_tokens`, and `cost_usd` from `responseBody.usage`.
|
|
6
|
+
*
|
|
7
|
+
* @param {Readonly<Record<string, unknown>>} responseBody - Parsed JSON object returned by `/v1/messages`.
|
|
8
|
+
*
|
|
9
|
+
* @returns {ModelTurnResponse['usage']} Frozen token usage and cost summary.
|
|
10
|
+
*/
|
|
11
|
+
export declare function computeAnthropicTurnUsage(responseBody: Readonly<Record<string, unknown>>): ModelTurnResponse['usage'];
|
|
12
|
+
//# sourceMappingURL=compute-anthropic-turn-usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compute-anthropic-turn-usage.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/compute-anthropic-turn-usage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAE3D;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CACvC,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC9C,iBAAiB,CAAC,OAAO,CAAC,CAmB5B"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts token usage and USD cost metrics from an Anthropic `/v1/messages` response payload.
|
|
3
|
+
*
|
|
4
|
+
* Reads `input_tokens`, `cache_read_input_tokens`, `output_tokens`, and `cost_usd` from `responseBody.usage`.
|
|
5
|
+
*
|
|
6
|
+
* @param {Readonly<Record<string, unknown>>} responseBody - Parsed JSON object returned by `/v1/messages`.
|
|
7
|
+
*
|
|
8
|
+
* @returns {ModelTurnResponse['usage']} Frozen token usage and cost summary.
|
|
9
|
+
*/
|
|
10
|
+
export function computeAnthropicTurnUsage(responseBody) {
|
|
11
|
+
const usageRaw = (responseBody['usage'] ?? {});
|
|
12
|
+
const inputTokens = typeof usageRaw['input_tokens'] === 'number' ? usageRaw['input_tokens'] : 0;
|
|
13
|
+
const cachedInputTokens = typeof usageRaw['cache_read_input_tokens'] === 'number'
|
|
14
|
+
? usageRaw['cache_read_input_tokens']
|
|
15
|
+
: 0;
|
|
16
|
+
const outputTokens = typeof usageRaw['output_tokens'] === 'number' ? usageRaw['output_tokens'] : 0;
|
|
17
|
+
const costUsd = typeof usageRaw['cost_usd'] === 'number'
|
|
18
|
+
? usageRaw['cost_usd']
|
|
19
|
+
: (inputTokens * 0.003 + outputTokens * 0.015) / 1_000;
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
inputTokens,
|
|
22
|
+
cachedInputTokens,
|
|
23
|
+
outputTokens,
|
|
24
|
+
costUsd,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=compute-anthropic-turn-usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compute-anthropic-turn-usage.js","sourceRoot":"","sources":["../../../src/driver/internal/compute-anthropic-turn-usage.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CACvC,YAA+C;IAE/C,MAAM,QAAQ,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,EAAE,CAAsC,CAAC;IACpF,MAAM,WAAW,GAAG,OAAO,QAAQ,CAAC,cAAc,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAChG,MAAM,iBAAiB,GACrB,OAAO,QAAQ,CAAC,yBAAyB,CAAC,KAAK,QAAQ;QACrD,CAAC,CAAC,QAAQ,CAAC,yBAAyB,CAAC;QACrC,CAAC,CAAC,CAAC,CAAC;IACR,MAAM,YAAY,GAChB,OAAO,QAAQ,CAAC,eAAe,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAChF,MAAM,OAAO,GACX,OAAO,QAAQ,CAAC,UAAU,CAAC,KAAK,QAAQ;QACtC,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC;QACtB,CAAC,CAAC,CAAC,WAAW,GAAG,KAAK,GAAG,YAAY,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;IAC3D,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,WAAW;QACX,iBAAiB;QACjB,YAAY;QACZ,OAAO;KACR,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parameters for constructing an `@anthropic-ai/sdk` `fetch` wrapper via `createAnthropicSdkFetch()`.
|
|
3
|
+
*
|
|
4
|
+
* Controls whether placeholder SDK auth headers are stripped when no seat credential is configured.
|
|
5
|
+
*/
|
|
6
|
+
export interface CreateAnthropicSdkFetchParams {
|
|
7
|
+
/** Whether the active seat provided an explicit API key or OAuth bearer token. */
|
|
8
|
+
readonly hasExplicitAuth: boolean;
|
|
9
|
+
/** Underlying `fetch` implementation to invoke. */
|
|
10
|
+
readonly baseFetch: typeof globalThis.fetch;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Wraps the target `fetch` function used by `@anthropic-ai/sdk` to preserve exact seat headers.
|
|
14
|
+
*
|
|
15
|
+
* Removes placeholder `authorization` and `x-api-key` headers injected by the SDK when
|
|
16
|
+
* `params.hasExplicitAuth` is `false`.
|
|
17
|
+
*
|
|
18
|
+
* @param {CreateAnthropicSdkFetchParams} params - Explicit auth flag and underlying `fetch` function.
|
|
19
|
+
*
|
|
20
|
+
* @returns {typeof globalThis.fetch} Wrapped `fetch` function passed to `new Anthropic({ fetch })`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createAnthropicSdkFetch(params: CreateAnthropicSdkFetchParams): typeof globalThis.fetch;
|
|
23
|
+
//# sourceMappingURL=create-anthropic-sdk-fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-anthropic-sdk-fetch.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/create-anthropic-sdk-fetch.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,6BAA6B;IAC5C,kFAAkF;IAClF,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;IAClC,mDAAmD;IACnD,QAAQ,CAAC,SAAS,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CAC7C;AAED;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,6BAA6B,GACpC,OAAO,UAAU,CAAC,KAAK,CASzB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps the target `fetch` function used by `@anthropic-ai/sdk` to preserve exact seat headers.
|
|
3
|
+
*
|
|
4
|
+
* Removes placeholder `authorization` and `x-api-key` headers injected by the SDK when
|
|
5
|
+
* `params.hasExplicitAuth` is `false`.
|
|
6
|
+
*
|
|
7
|
+
* @param {CreateAnthropicSdkFetchParams} params - Explicit auth flag and underlying `fetch` function.
|
|
8
|
+
*
|
|
9
|
+
* @returns {typeof globalThis.fetch} Wrapped `fetch` function passed to `new Anthropic({ fetch })`.
|
|
10
|
+
*/
|
|
11
|
+
export function createAnthropicSdkFetch(params) {
|
|
12
|
+
return (input, init) => {
|
|
13
|
+
const mergedHeaders = new Headers(init?.headers);
|
|
14
|
+
if (!params.hasExplicitAuth) {
|
|
15
|
+
mergedHeaders.delete('authorization');
|
|
16
|
+
mergedHeaders.delete('x-api-key');
|
|
17
|
+
}
|
|
18
|
+
return params.baseFetch(input, { ...init, headers: mergedHeaders });
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=create-anthropic-sdk-fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-anthropic-sdk-fetch.js","sourceRoot":"","sources":["../../../src/driver/internal/create-anthropic-sdk-fetch.ts"],"names":[],"mappings":"AAYA;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CACrC,MAAqC;IAErC,OAAO,CAAC,KAA6B,EAAE,IAAkB,EAAqB,EAAE;QAC9E,MAAM,aAAa,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;YAC5B,aAAa,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;YACtC,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC,CAAC;IACtE,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { WireFrameRecord } from '@paraharness/core';
|
|
2
|
+
/**
|
|
3
|
+
* Decodes a single `WireFrameRecord` into an Anthropic `/v1/messages` wire message object.
|
|
4
|
+
*
|
|
5
|
+
* Uses `rawWireFrame` directly when it already holds a valid Anthropic `{ role, content }` message
|
|
6
|
+
* (preserving `thinking` blocks, `signature` fields, and `cache_control`), and otherwise projects
|
|
7
|
+
* from `frame.canonical`.
|
|
8
|
+
*
|
|
9
|
+
* @param {WireFrameRecord} frame - Conversation history frame record.
|
|
10
|
+
*
|
|
11
|
+
* @returns {Readonly<Record<string, unknown>>} Anthropic wire message object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function decodeFrameToAnthropicMessage(frame: WireFrameRecord): Readonly<Record<string, unknown>>;
|
|
14
|
+
//# sourceMappingURL=decode-frame-to-anthropic-message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-frame-to-anthropic-message.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/decode-frame-to-anthropic-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGzD;;;;;;;;;;GAUG;AACH,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,eAAe,GACrB,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAgBnC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { projectCanonicalToAnthropicMessage } from './project-canonical-to-anthropic-message.js';
|
|
2
|
+
/**
|
|
3
|
+
* Decodes a single `WireFrameRecord` into an Anthropic `/v1/messages` wire message object.
|
|
4
|
+
*
|
|
5
|
+
* Uses `rawWireFrame` directly when it already holds a valid Anthropic `{ role, content }` message
|
|
6
|
+
* (preserving `thinking` blocks, `signature` fields, and `cache_control`), and otherwise projects
|
|
7
|
+
* from `frame.canonical`.
|
|
8
|
+
*
|
|
9
|
+
* @param {WireFrameRecord} frame - Conversation history frame record.
|
|
10
|
+
*
|
|
11
|
+
* @returns {Readonly<Record<string, unknown>>} Anthropic wire message object.
|
|
12
|
+
*/
|
|
13
|
+
export function decodeFrameToAnthropicMessage(frame) {
|
|
14
|
+
const text = new TextDecoder().decode(frame.rawWireFrame).trim();
|
|
15
|
+
if (text.startsWith('{')) {
|
|
16
|
+
try {
|
|
17
|
+
const parsed = JSON.parse(text);
|
|
18
|
+
if ((parsed['role'] === 'user' || parsed['role'] === 'assistant') &&
|
|
19
|
+
Array.isArray(parsed['content'])) {
|
|
20
|
+
return parsed;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return projectCanonicalToAnthropicMessage(frame.canonical);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return projectCanonicalToAnthropicMessage(frame.canonical);
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=decode-frame-to-anthropic-message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-frame-to-anthropic-message.js","sourceRoot":"","sources":["../../../src/driver/internal/decode-frame-to-anthropic-message.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kCAAkC,EAAE,MAAM,6CAA6C,CAAC;AAEjG;;;;;;;;;;GAUG;AACH,MAAM,UAAU,6BAA6B,CAC3C,KAAsB;IAEtB,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,IAAI,EAAE,CAAC;IACjE,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACzB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAsC,CAAC;YACrE,IACE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC;gBAC7D,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,EAChC,CAAC;gBACD,OAAO,MAAM,CAAC;YAChB,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,kCAAkC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC;IACD,OAAO,kCAAkC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAC7D,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ExtractedAnthropicContentBlocks } from './extracted-anthropic-content-blocks.js';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts raw content blocks, visible text, and canonical tool calls from an Anthropic `/v1/messages` response body.
|
|
4
|
+
*
|
|
5
|
+
* Preserves `rawContent` (including `thinking` and `signature` fields) intact for `wireFrame` serialization.
|
|
6
|
+
*
|
|
7
|
+
* @param {Readonly<Record<string, unknown>>} responseBody - Raw JSON object returned by `/v1/messages`.
|
|
8
|
+
*
|
|
9
|
+
* @returns {ExtractedAnthropicContentBlocks} Frozen extracted content blocks, visible text, and tool calls.
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractAnthropicContentBlocks(responseBody: Readonly<Record<string, unknown>>): ExtractedAnthropicContentBlocks;
|
|
12
|
+
//# sourceMappingURL=extract-anthropic-content-blocks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-anthropic-content-blocks.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/extract-anthropic-content-blocks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,+BAA+B,EAAE,MAAM,yCAAyC,CAAC;AAE/F;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAC3C,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC9C,+BAA+B,CA4BjC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts raw content blocks, visible text, and canonical tool calls from an Anthropic `/v1/messages` response body.
|
|
3
|
+
*
|
|
4
|
+
* Preserves `rawContent` (including `thinking` and `signature` fields) intact for `wireFrame` serialization.
|
|
5
|
+
*
|
|
6
|
+
* @param {Readonly<Record<string, unknown>>} responseBody - Raw JSON object returned by `/v1/messages`.
|
|
7
|
+
*
|
|
8
|
+
* @returns {ExtractedAnthropicContentBlocks} Frozen extracted content blocks, visible text, and tool calls.
|
|
9
|
+
*/
|
|
10
|
+
export function extractAnthropicContentBlocks(responseBody) {
|
|
11
|
+
const rawContent = Array.isArray(responseBody['content'])
|
|
12
|
+
? responseBody['content']
|
|
13
|
+
: [];
|
|
14
|
+
const textParts = [];
|
|
15
|
+
const toolCalls = [];
|
|
16
|
+
for (const block of rawContent) {
|
|
17
|
+
if (block['type'] === 'text' && typeof block['text'] === 'string') {
|
|
18
|
+
textParts.push(block['text']);
|
|
19
|
+
}
|
|
20
|
+
else if (block['type'] === 'tool_use' &&
|
|
21
|
+
typeof block['id'] === 'string' &&
|
|
22
|
+
typeof block['name'] === 'string') {
|
|
23
|
+
toolCalls.push(Object.freeze({
|
|
24
|
+
id: block['id'],
|
|
25
|
+
name: block['name'],
|
|
26
|
+
argsJson: JSON.stringify(block['input'] ?? {}),
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
rawContent,
|
|
32
|
+
visibleText: textParts.join(''),
|
|
33
|
+
toolCalls: Object.freeze(toolCalls),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=extract-anthropic-content-blocks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-anthropic-content-blocks.js","sourceRoot":"","sources":["../../../src/driver/internal/extract-anthropic-content-blocks.ts"],"names":[],"mappings":"AAGA;;;;;;;;GAQG;AACH,MAAM,UAAU,6BAA6B,CAC3C,YAA+C;IAE/C,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;QACvD,CAAC,CAAE,YAAY,CAAC,SAAS,CAAkD;QAC3E,CAAC,CAAC,EAAE,CAAC;IACP,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,MAAM,SAAS,GAAwB,EAAE,CAAC;IAC1C,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE,CAAC;QAC/B,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,QAAQ,EAAE,CAAC;YAClE,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;QAChC,CAAC;aAAM,IACL,KAAK,CAAC,MAAM,CAAC,KAAK,UAAU;YAC5B,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,QAAQ;YAC/B,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,QAAQ,EACjC,CAAC;YACD,SAAS,CAAC,IAAI,CACZ,MAAM,CAAC,MAAM,CAAoB;gBAC/B,EAAE,EAAE,KAAK,CAAC,IAAI,CAAC;gBACf,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC;gBACnB,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;aAC/C,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAkC;QACpD,UAAU;QACV,WAAW,EAAE,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC;KACpC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ClaudeHttpResponse } from '../claude-http-response.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for validating an Anthropic HTTP response and extracting its JSON body.
|
|
4
|
+
*
|
|
5
|
+
* Used by `extractClaudeResponseBody()` to surface structured `429` rate-limit and non-2xx errors.
|
|
6
|
+
*/
|
|
7
|
+
export interface ExtractClaudeResponseBodyParams {
|
|
8
|
+
/** HTTP response envelope returned by `ClaudeHttpTransport.sendMessagesRequest()`. */
|
|
9
|
+
readonly response: ClaudeHttpResponse;
|
|
10
|
+
/** Target Anthropic model identifier for diagnostic error messages. */
|
|
11
|
+
readonly modelId: string;
|
|
12
|
+
/** Optional active `ClaudeAccountSeatNode` identifier for diagnostic error messages. */
|
|
13
|
+
readonly seatId: string | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Validates the HTTP status on a `ClaudeHttpResponse` and returns the parsed JSON body.
|
|
17
|
+
*
|
|
18
|
+
* Checks HTTP status codes before invoking `JSON.parse(response.bodyText)` so non-JSON error
|
|
19
|
+
* gateways (`502 Bad Gateway`) never throw `SyntaxError`, and attaches `{ status: 429, resetAtMs }`
|
|
20
|
+
* on rate-limit responses.
|
|
21
|
+
*
|
|
22
|
+
* @param {ExtractClaudeResponseBodyParams} params - HTTP response envelope, model ID, and seat ID.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Readonly<Record<string, unknown>>} Validated JSON response payload object.
|
|
25
|
+
*
|
|
26
|
+
* @throws {Error} With `{ status: 429, resetAtMs }` on HTTP `429` or `{ status }` on non-2xx responses.
|
|
27
|
+
*/
|
|
28
|
+
export declare function extractClaudeResponseBody(params: ExtractClaudeResponseBodyParams): Readonly<Record<string, unknown>>;
|
|
29
|
+
//# sourceMappingURL=extract-claude-response-body.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-claude-response-body.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/extract-claude-response-body.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAGrE;;;;GAIG;AACH,MAAM,WAAW,+BAA+B;IAC9C,sFAAsF;IACtF,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,wFAAwF;IACxF,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;CACrC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,+BAA+B,GACtC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAiCnC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { parseClaudeRetryAfterResetAtMs } from './parse-claude-retry-after-reset-at-ms.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates the HTTP status on a `ClaudeHttpResponse` and returns the parsed JSON body.
|
|
4
|
+
*
|
|
5
|
+
* Checks HTTP status codes before invoking `JSON.parse(response.bodyText)` so non-JSON error
|
|
6
|
+
* gateways (`502 Bad Gateway`) never throw `SyntaxError`, and attaches `{ status: 429, resetAtMs }`
|
|
7
|
+
* on rate-limit responses.
|
|
8
|
+
*
|
|
9
|
+
* @param {ExtractClaudeResponseBodyParams} params - HTTP response envelope, model ID, and seat ID.
|
|
10
|
+
*
|
|
11
|
+
* @returns {Readonly<Record<string, unknown>>} Validated JSON response payload object.
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} With `{ status: 429, resetAtMs }` on HTTP `429` or `{ status }` on non-2xx responses.
|
|
14
|
+
*/
|
|
15
|
+
export function extractClaudeResponseBody(params) {
|
|
16
|
+
const { response, modelId, seatId } = params;
|
|
17
|
+
const resetAtMs = response.resetAtMs ??
|
|
18
|
+
parseClaudeRetryAfterResetAtMs(response.headers?.['retry-after'] ?? response.headers?.['Retry-After']);
|
|
19
|
+
if (response.status === 429) {
|
|
20
|
+
throw Object.assign(new Error(`Anthropic /v1/messages rate limit (429) on model '${modelId}' (seat '${seatId ?? 'default'}').`), { status: 429, resetAtMs: resetAtMs ?? Date.now() + 60_000 });
|
|
21
|
+
}
|
|
22
|
+
if (response.status < 200 || response.status >= 300) {
|
|
23
|
+
const trimmedText = response.bodyText?.trim() ?? '';
|
|
24
|
+
const detail = trimmedText.length > 0 ? `: ${trimmedText}` : '';
|
|
25
|
+
throw Object.assign(new Error(`Anthropic /v1/messages failed with HTTP ${String(response.status)} for model '${modelId}'${detail}.`), { status: response.status });
|
|
26
|
+
}
|
|
27
|
+
if (response.body !== undefined) {
|
|
28
|
+
return response.body;
|
|
29
|
+
}
|
|
30
|
+
const trimmedBodyText = response.bodyText?.trim() ?? '';
|
|
31
|
+
if (trimmedBodyText.length > 0) {
|
|
32
|
+
return JSON.parse(trimmedBodyText);
|
|
33
|
+
}
|
|
34
|
+
return Object.freeze({});
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=extract-claude-response-body.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-claude-response-body.js","sourceRoot":"","sources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AAS,EAAE,CAAC;QAChC,OAAO,QAAQ,CAAC,IAAI,CAAC;IACvB,CAAC;IACD,MAAM,eAAe,GAAG,QAAQ,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxD,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,eAAe,CAAsC,CAAC;IAC1E,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts an OAuth bearer token string from a Claude CLI `configDir` path on disk when present.
|
|
3
|
+
*
|
|
4
|
+
* Searches standard credential JSON files inside `configDir` (or `configDir` directly if it is a file)
|
|
5
|
+
* and reads `claudeAiOauth.accessToken`, `accessToken`, `access_token`, `oauthToken`, or `token`.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} configDir - Filesystem path to the Claude CLI configuration directory.
|
|
8
|
+
*
|
|
9
|
+
* @returns {string | undefined} Extracted OAuth bearer token, or `undefined` if no credential file exists.
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractOAuthTokenFromConfigDir(configDir: string): string | undefined;
|
|
12
|
+
//# sourceMappingURL=extract-oauth-token-from-config-dir.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-oauth-token-from-config-dir.d.ts","sourceRoot":"","sources":["../../../src/driver/internal/extract-oauth-token-from-config-dir.ts"],"names":[],"mappings":"AAuEA;;;;;;;;;GASG;AACH,wBAAgB,8BAA8B,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAoBpF"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { CLAUDE_CREDENTIAL_FILENAMES } from '../default-claude-http-transport.constants.js';
|
|
5
|
+
/**
|
|
6
|
+
* Expands a leading `'~/'` home-directory prefix on a Claude CLI `configDir` path.
|
|
7
|
+
*
|
|
8
|
+
* Returns `configDir` unchanged when it does not begin with `'~/'`.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} configDir - Raw `configDir` path from a `ClaudeAccountSeatNode`.
|
|
11
|
+
*
|
|
12
|
+
* @returns {string} Filesystem path with leading `'~/'` expanded.
|
|
13
|
+
*/
|
|
14
|
+
function expandClaudeConfigDirHome(configDir) {
|
|
15
|
+
if (configDir.startsWith('~/')) {
|
|
16
|
+
return join(homedir(), configDir.slice(2));
|
|
17
|
+
}
|
|
18
|
+
return configDir;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Resolves the ordered list of candidate credential file paths for a Claude CLI `configDir`.
|
|
22
|
+
*
|
|
23
|
+
* Returns `[resolvedDir]` when `resolvedDir` is a regular file, and otherwise joins each
|
|
24
|
+
* filename in `CLAUDE_CREDENTIAL_FILENAMES`.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} resolvedDir - Existing filesystem path to a file or directory.
|
|
27
|
+
*
|
|
28
|
+
* @returns {readonly string[]} Candidate credential file paths to inspect.
|
|
29
|
+
*/
|
|
30
|
+
function resolveClaudeCredentialPaths(resolvedDir) {
|
|
31
|
+
if (statSync(resolvedDir).isFile()) {
|
|
32
|
+
return Object.freeze([resolvedDir]);
|
|
33
|
+
}
|
|
34
|
+
const candidatePaths = [];
|
|
35
|
+
for (const filename of CLAUDE_CREDENTIAL_FILENAMES) {
|
|
36
|
+
candidatePaths.push(join(resolvedDir, filename));
|
|
37
|
+
}
|
|
38
|
+
return Object.freeze(candidatePaths);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Extracts a bearer token string from the raw text of a Claude CLI credential file.
|
|
42
|
+
*
|
|
43
|
+
* Parses JSON objects for `claudeAiOauth.accessToken`, `accessToken`, `access_token`, `oauthToken`,
|
|
44
|
+
* or `token`, and falls back to the raw trimmed string if the file holds a plain-text token.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} rawText - Trimmed non-empty file contents.
|
|
47
|
+
*
|
|
48
|
+
* @returns {string | undefined} Extracted OAuth bearer token, or `undefined` if no token field is present.
|
|
49
|
+
*/
|
|
50
|
+
function parseClaudeCredentialFileText(rawText) {
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(rawText);
|
|
53
|
+
const nested = parsed['claudeAiOauth'];
|
|
54
|
+
const token = nested?.['accessToken'] ??
|
|
55
|
+
parsed['accessToken'] ??
|
|
56
|
+
parsed['access_token'] ??
|
|
57
|
+
parsed['oauthToken'] ??
|
|
58
|
+
parsed['token'];
|
|
59
|
+
if (typeof token === 'string' && token.trim().length > 0) {
|
|
60
|
+
return token.trim();
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return rawText;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Extracts an OAuth bearer token string from a Claude CLI `configDir` path on disk when present.
|
|
70
|
+
*
|
|
71
|
+
* Searches standard credential JSON files inside `configDir` (or `configDir` directly if it is a file)
|
|
72
|
+
* and reads `claudeAiOauth.accessToken`, `accessToken`, `access_token`, `oauthToken`, or `token`.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} configDir - Filesystem path to the Claude CLI configuration directory.
|
|
75
|
+
*
|
|
76
|
+
* @returns {string | undefined} Extracted OAuth bearer token, or `undefined` if no credential file exists.
|
|
77
|
+
*/
|
|
78
|
+
export function extractOAuthTokenFromConfigDir(configDir) {
|
|
79
|
+
const resolvedDir = expandClaudeConfigDirHome(configDir);
|
|
80
|
+
if (!existsSync(resolvedDir)) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const candidatePaths = resolveClaudeCredentialPaths(resolvedDir);
|
|
84
|
+
for (const filePath of candidatePaths) {
|
|
85
|
+
if (!existsSync(filePath)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const rawText = readFileSync(filePath, 'utf8').trim();
|
|
89
|
+
if (rawText.length === 0) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const token = parseClaudeCredentialFileText(rawText);
|
|
93
|
+
if (token !== undefined) {
|
|
94
|
+
return token;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=extract-oauth-token-from-config-dir.js.map
|