Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/node/services/aiService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// For now, the commandProcessor tests demonstrate our testing approach

import { describe, it, expect, beforeEach } from "bun:test";
import { AIService, normalizeAnthropicBaseURL } from "./aiService";
import {
AIService,
normalizeAnthropicBaseURL,
buildAnthropicHeaders,
ANTHROPIC_1M_CONTEXT_HEADER,
} from "./aiService";
import { HistoryService } from "./historyService";
import { PartialService } from "./partialService";
import { InitStateManager } from "./initStateManager";
Expand Down Expand Up @@ -76,3 +81,39 @@ describe("normalizeAnthropicBaseURL", () => {
);
});
});

describe("buildAnthropicHeaders", () => {
it("returns undefined when use1MContext is false and no existing headers", () => {
expect(buildAnthropicHeaders(undefined, false)).toBeUndefined();
});

it("returns existing headers unchanged when use1MContext is false", () => {
const existing = { "x-custom": "value" };
expect(buildAnthropicHeaders(existing, false)).toBe(existing);
});

it("returns existing headers unchanged when use1MContext is undefined", () => {
const existing = { "x-custom": "value" };
expect(buildAnthropicHeaders(existing, undefined)).toBe(existing);
});

it("adds 1M context header when use1MContext is true and no existing headers", () => {
const result = buildAnthropicHeaders(undefined, true);
expect(result).toEqual({ "anthropic-beta": ANTHROPIC_1M_CONTEXT_HEADER });
});

it("merges 1M context header with existing headers when use1MContext is true", () => {
const existing = { "x-custom": "value" };
const result = buildAnthropicHeaders(existing, true);
expect(result).toEqual({
"x-custom": "value",
"anthropic-beta": ANTHROPIC_1M_CONTEXT_HEADER,
});
});

it("overwrites existing anthropic-beta header when use1MContext is true", () => {
const existing = { "anthropic-beta": "other-beta" };
const result = buildAnthropicHeaders(existing, true);
expect(result).toEqual({ "anthropic-beta": ANTHROPIC_1M_CONTEXT_HEADER });
});
});
32 changes: 24 additions & 8 deletions src/node/services/aiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ export function normalizeAnthropicBaseURL(baseURL: string): string {
return `${trimmed}/v1`;
}

/** Header value for Anthropic 1M context beta */
export const ANTHROPIC_1M_CONTEXT_HEADER = "context-1m-2025-08-07";

/**
* Build headers for Anthropic provider, optionally including the 1M context beta header.
* Exported for testing.
*/
export function buildAnthropicHeaders(
existingHeaders: Record<string, string> | undefined,
use1MContext: boolean | undefined
): Record<string, string> | undefined {
if (!use1MContext) {
return existingHeaders;
}
if (existingHeaders) {
return { ...existingHeaders, "anthropic-beta": ANTHROPIC_1M_CONTEXT_HEADER };
}
return { "anthropic-beta": ANTHROPIC_1M_CONTEXT_HEADER };
}

/**
* Preload AI SDK provider modules to avoid race conditions in concurrent test environments.
* This function loads @ai-sdk/anthropic, @ai-sdk/openai, and ollama-ai-provider-v2 eagerly
Expand Down Expand Up @@ -447,14 +467,10 @@ export class AIService extends EventEmitter {
: configWithApiKey;

// Add 1M context beta header if requested
const use1MContext = muxProviderOptions?.anthropic?.use1MContext;
const existingHeaders = normalizedConfig.headers;
const headers =
use1MContext && existingHeaders
? { ...existingHeaders, "anthropic-beta": "context-1m-2025-08-07" }
: use1MContext
? { "anthropic-beta": "context-1m-2025-08-07" }
: existingHeaders;
const headers = buildAnthropicHeaders(
normalizedConfig.headers,
muxProviderOptions?.anthropic?.use1MContext
);

// Lazy-load Anthropic provider to reduce startup time
const { createAnthropic } = await PROVIDER_REGISTRY.anthropic();
Expand Down
115 changes: 0 additions & 115 deletions tests/ipc/anthropic1MContext.test.ts

This file was deleted.