@jeffusion/bungee-llms 3.2.1 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,33 +16,31 @@ export declare class AnthropicToOpenAIConverter implements AIConverter {
16
16
  readonly from = "anthropic";
17
17
  readonly to = "openai";
18
18
  private configuredApiMode?;
19
- private readonly protocolConversion;
20
- private readonly runtime;
21
- constructor();
22
19
  setApiMode(mode: unknown): void;
23
20
  setRuntimeOptions(options: unknown): void;
24
- private parseIntegerOption;
25
- private resolveReasoningMaxCompletionTokens;
26
21
  private resolveTargetApiMode;
27
22
  /**
28
23
  * 修改请求:转换为 OpenAI Chat Completions 格式
29
24
  */
30
25
  onBeforeRequest(ctx: MutableRequestContext): Promise<void>;
31
- private canUseRuntimeRequestConversion;
32
26
  private isAnthropicCountTokensPath;
33
27
  private buildOpenAICountTokensRequest;
34
28
  /**
35
29
  * 构建 OpenAI 请求
36
30
  */
37
31
  private buildOpenAIRequest;
38
- private isAnthropicThinkingEnabled;
39
- private resolveAnthropicThinkingEffort;
40
- private resolveOutputConfigEffort;
41
- private normalizeOpenAIReasoningEffort;
42
- private supportsXHighReasoningEffort;
32
+ private isOpenAINumericOSeriesModel;
43
33
  private buildOpenAIResponsesRequest;
34
+ private convertAnthropicSystemToInstructions;
35
+ private convertAnthropicMessagesToResponsesInput;
36
+ private convertAnthropicImageSourceToUrl;
37
+ private mapAnthropicToolChoiceToResponses;
38
+ private resolveAnthropicReasoningEffort;
39
+ private supportsReasoningEffort;
44
40
  private convertSystemMessages;
45
- private validateAndCleanToolCalls;
41
+ private sanitizeSystemText;
42
+ private extractTextFromOpenAIContent;
43
+ private normalizeOpenAISystemMessages;
46
44
  /**
47
45
  * 转换 User 消息
48
46
  */
@@ -53,10 +51,10 @@ export declare class AnthropicToOpenAIConverter implements AIConverter {
53
51
  * 转换 Assistant 消息
54
52
  */
55
53
  private convertAssistantMessage;
56
- private normalizeToolCallId;
57
- private mapAnthropicToolChoiceToOpenAI;
58
- private parseOpenAIImageUrlSource;
54
+ private normalizeOpenAIMessageContent;
59
55
  private normalizeToolParametersSchema;
56
+ private cleanOpenAIToolSchema;
57
+ private resolvePromptCacheKey;
60
58
  private normalizeJsonSchema;
61
59
  /**
62
60
  * 处理非流式响应:OpenAI → Anthropic
@@ -70,25 +68,31 @@ export declare class AnthropicToOpenAIConverter implements AIConverter {
70
68
  private isLikelyOpenAISuccessBody;
71
69
  private isResponsesApiBody;
72
70
  private extractReasoningSummaryTexts;
73
- private appendUniqueThinkingBlocks;
74
- private appendParsedContent;
75
- private extractChatCompletionReasoningTexts;
71
+ private extractStrictResponsesReasoningText;
76
72
  private extractReasoningTextFromDeltaContentPart;
77
73
  private extractChatThinkingDelta;
78
74
  private getChatUsedIndices;
79
75
  private allocateChatContentBlockIndex;
80
76
  private ensureChatTextBlockStarted;
81
77
  private ensureChatThinkingBlockStarted;
78
+ private closeChatNonToolBlock;
82
79
  private resolveChatToolIndex;
83
80
  private resolveResponsesReasoningIndex;
81
+ private nextResponsesContentIndex;
82
+ private resolveResponsesContentIndex;
83
+ private ensureResponsesTextBlockStarted;
84
+ private resolveResponsesToolIndex;
84
85
  private ensureResponsesThinkingBlockStarted;
85
86
  private emitResponsesThinkingDelta;
86
87
  private extractResponsesReasoningDeltaText;
87
88
  private convertOpenAIResponsesToAnthropic;
89
+ private mapResponsesStopReason;
90
+ private buildAnthropicUsageFromResponses;
88
91
  /**
89
92
  * 转换 OpenAI 响应为 Anthropic 格式
90
93
  */
91
94
  private convertOpenAIResponseToAnthropic;
95
+ private parseToolArguments;
92
96
  /**
93
97
  * 处理流式响应:OpenAI chunk → Anthropic SSE
94
98
  */
@@ -0,0 +1,47 @@
1
+ import type { LLMProvider } from '../core/types';
2
+ import type { ProviderTokenAccountingCapabilities } from './types';
3
+ export declare const PROVIDER_TOKEN_ACCOUNTING_CAPABILITY_MATRIX: {
4
+ readonly openai: {
5
+ readonly provider: "openai";
6
+ readonly supportsOfficialResponseUsage: true;
7
+ readonly supportsStreamingResponseUsage: true;
8
+ readonly supportsDedicatedCountEndpoint: true;
9
+ readonly supportsLocalTokenizer: true;
10
+ readonly supportsHeuristicFallback: true;
11
+ readonly dedicatedCountEndpoint: {
12
+ readonly supported: true;
13
+ readonly mode: "input_estimate_only";
14
+ readonly defaultEnabled: false;
15
+ readonly endpoint: "/responses/input_tokens";
16
+ readonly livePathAuthority: false;
17
+ };
18
+ };
19
+ readonly anthropic: {
20
+ readonly provider: "anthropic";
21
+ readonly supportsOfficialResponseUsage: true;
22
+ readonly supportsStreamingResponseUsage: true;
23
+ readonly supportsDedicatedCountEndpoint: true;
24
+ readonly supportsLocalTokenizer: true;
25
+ readonly supportsHeuristicFallback: true;
26
+ readonly dedicatedCountEndpoint: {
27
+ readonly supported: true;
28
+ readonly mode: "input_estimate_only";
29
+ readonly livePathAuthority: false;
30
+ readonly endpoint: "/v1/messages/count_tokens";
31
+ };
32
+ };
33
+ readonly gemini: {
34
+ readonly provider: "gemini";
35
+ readonly supportsOfficialResponseUsage: true;
36
+ readonly supportsStreamingResponseUsage: true;
37
+ readonly supportsDedicatedCountEndpoint: true;
38
+ readonly supportsLocalTokenizer: true;
39
+ readonly supportsHeuristicFallback: true;
40
+ readonly countTokens: {
41
+ readonly supported: true;
42
+ readonly mode: "input_estimate_only";
43
+ readonly livePathAuthority: false;
44
+ };
45
+ };
46
+ };
47
+ export declare function getProviderTokenAccountingCapabilities(provider: LLMProvider): ProviderTokenAccountingCapabilities;
@@ -0,0 +1,16 @@
1
+ import type { CanonicalTokenAccountingEventV2, TokenAccountingOutcome, TokenAccountingSessionState } from './types';
2
+ type JsonRecord = Record<string, unknown>;
3
+ export declare function isRecord(value: unknown): value is JsonRecord;
4
+ export declare function asString(value: unknown): string | undefined;
5
+ export declare function asFiniteNumber(value: unknown): number | undefined;
6
+ export declare function readNumber(record: JsonRecord | undefined, key: string): number | undefined;
7
+ export declare function readRecord(record: JsonRecord | undefined, key: string): JsonRecord | undefined;
8
+ export declare function countTextTokens(text: string): number;
9
+ export declare function countModelTextTokens(text: string, model?: string): number;
10
+ export declare function countJsonLikeTokens(value: unknown): number;
11
+ export declare function clampTokens(value: number | undefined): number | undefined;
12
+ export declare function buildBaseEvent(state: TokenAccountingSessionState, overrides: Partial<CanonicalTokenAccountingEventV2>): CanonicalTokenAccountingEventV2;
13
+ export declare function createPartialTokenAccountingEvent(state: TokenAccountingSessionState, overrides?: Partial<CanonicalTokenAccountingEventV2>): CanonicalTokenAccountingEventV2;
14
+ export declare function finalizeTokenAccountingEvent(state: TokenAccountingSessionState, overrides?: Partial<CanonicalTokenAccountingEventV2>, outcome?: TokenAccountingOutcome): CanonicalTokenAccountingEventV2;
15
+ export declare function assertCanonicalTokenAccountingEventV2(event: unknown): asserts event is CanonicalTokenAccountingEventV2;
16
+ export {};
@@ -0,0 +1,4 @@
1
+ export * from './types';
2
+ export * from './helpers';
3
+ export * from './capabilities';
4
+ export * from './session';
@@ -0,0 +1,2 @@
1
+ import type { TokenAccountingSession, TokenAccountingSessionInput } from './types';
2
+ export declare function createTokenAccountingSession(input: TokenAccountingSessionInput): TokenAccountingSession;
@@ -0,0 +1,96 @@
1
+ import type { LLMProvider } from '../core/types';
2
+ export declare const SUPPORTED_TOKEN_ACCOUNTING_PROVIDERS: readonly ["openai", "anthropic", "gemini"];
3
+ export type SupportedTokenAccountingProvider = typeof SUPPORTED_TOKEN_ACCOUNTING_PROVIDERS[number];
4
+ export declare const TOKEN_ACCOUNTING_AUTHORITIES: readonly ["official", "local", "heuristic", "partial", "none"];
5
+ export declare const TOKEN_ACCOUNTING_OUTCOMES: readonly ["completed", "aborted", "failed"];
6
+ export type TokenAccountingAuthority = typeof TOKEN_ACCOUNTING_AUTHORITIES[number];
7
+ export type TokenAccountingOutcome = typeof TOKEN_ACCOUNTING_OUTCOMES[number];
8
+ export interface CanonicalTokenAccountingEventV2 {
9
+ requestId: string;
10
+ attemptId: string;
11
+ routeId: string;
12
+ upstreamId: string;
13
+ provider: LLMProvider;
14
+ model: string;
15
+ streaming: boolean;
16
+ inputTokens?: number;
17
+ outputTokens?: number;
18
+ cacheReadTokens?: number;
19
+ cacheWriteTokens?: number;
20
+ inputAuthority: TokenAccountingAuthority;
21
+ outputAuthority: TokenAccountingAuthority;
22
+ final: boolean;
23
+ outcome: TokenAccountingOutcome;
24
+ countedAt: string;
25
+ }
26
+ export interface TokenAccountingSessionInput {
27
+ provider: LLMProvider;
28
+ model?: string;
29
+ routeId: string;
30
+ upstreamId: string;
31
+ requestId: string;
32
+ attemptId: string;
33
+ streaming: boolean;
34
+ }
35
+ export interface TokenAccountingRequestInput {
36
+ body: Record<string, unknown>;
37
+ }
38
+ export interface TokenAccountingResponseInput {
39
+ body: Record<string, unknown>;
40
+ }
41
+ export interface TokenAccountingStreamChunkInput {
42
+ chunk: Record<string, unknown>;
43
+ }
44
+ export interface TokenAccountingSession {
45
+ consumeRequest(input: TokenAccountingRequestInput): void;
46
+ consumeResponse(input: TokenAccountingResponseInput): CanonicalTokenAccountingEventV2;
47
+ consumeStreamChunk(input: TokenAccountingStreamChunkInput): CanonicalTokenAccountingEventV2 | null;
48
+ finalizeAbortedStream(): CanonicalTokenAccountingEventV2;
49
+ }
50
+ export interface DedicatedCountEndpointCapability {
51
+ supported: boolean;
52
+ mode: 'input_estimate_only';
53
+ livePathAuthority: false;
54
+ defaultEnabled?: false;
55
+ endpoint?: string;
56
+ }
57
+ export interface ProviderTokenAccountingCapabilities {
58
+ provider: SupportedTokenAccountingProvider;
59
+ supportsOfficialResponseUsage: boolean;
60
+ supportsStreamingResponseUsage: boolean;
61
+ supportsDedicatedCountEndpoint: boolean;
62
+ supportsLocalTokenizer: boolean;
63
+ supportsHeuristicFallback: boolean;
64
+ dedicatedCountEndpoint?: DedicatedCountEndpointCapability;
65
+ countTokens?: {
66
+ supported: boolean;
67
+ mode: 'input_estimate_only';
68
+ livePathAuthority: false;
69
+ };
70
+ }
71
+ export type ProviderTokenAccountingCapabilityMatrix = Record<SupportedTokenAccountingProvider, ProviderTokenAccountingCapabilities>;
72
+ export interface TokenAccountingSessionState {
73
+ provider: LLMProvider;
74
+ routeId: string;
75
+ upstreamId: string;
76
+ requestId: string;
77
+ attemptId: string;
78
+ streaming: boolean;
79
+ model?: string;
80
+ requestBody?: Record<string, unknown>;
81
+ estimatedInputTokens?: number;
82
+ estimatedInputAuthority?: TokenAccountingAuthority;
83
+ outputTokens?: number;
84
+ outputAuthority?: TokenAccountingAuthority;
85
+ cacheReadTokens?: number;
86
+ cacheWriteTokens?: number;
87
+ finalReceived: boolean;
88
+ }
89
+ export interface ProviderTokenAccountingAdapter {
90
+ readonly provider: LLMProvider;
91
+ readonly capabilities: ProviderTokenAccountingCapabilities;
92
+ consumeRequest(state: TokenAccountingSessionState, body: Record<string, unknown>): void;
93
+ consumeResponse(state: TokenAccountingSessionState, body: Record<string, unknown>): CanonicalTokenAccountingEventV2;
94
+ consumeStreamChunk(state: TokenAccountingSessionState, chunk: Record<string, unknown>): CanonicalTokenAccountingEventV2 | null;
95
+ finalizeAbortedStream(state: TokenAccountingSessionState): CanonicalTokenAccountingEventV2;
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeffusion/bungee-llms",
3
- "version": "3.2.1",
3
+ "version": "3.3.1",
4
4
  "description": "Extensible LLM adapters and protocol conversion package for Bungee",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "prepublishOnly": "bun run build"
12
12
  },
13
13
  "dependencies": {
14
- "@jeffusion/bungee-types": "^3.2.1"
14
+ "@jeffusion/bungee-types": "^3.3.1"
15
15
  },
16
16
  "devDependencies": {
17
17
  "bun-types": "^1.3.2",