diff --git a/apps/claude-sdk-cli/src/runAgent.ts b/apps/claude-sdk-cli/src/runAgent.ts index 5b95384..4c7a9ed 100644 --- a/apps/claude-sdk-cli/src/runAgent.ts +++ b/apps/claude-sdk-cli/src/runAgent.ts @@ -1,4 +1,4 @@ -import { AnthropicBeta, type AnyToolDefinition, type IAnthropicAgent, type SdkMessage } from '@shellicar/claude-sdk'; +import { AnthropicBeta, type AnyToolDefinition, CacheTtl, type IAnthropicAgent, type SdkMessage } from '@shellicar/claude-sdk'; import { CreateFile } from '@shellicar/claude-sdk-tools/CreateFile'; import { DeleteDirectory } from '@shellicar/claude-sdk-tools/DeleteDirectory'; import { DeleteFile } from '@shellicar/claude-sdk-tools/DeleteFile'; @@ -29,7 +29,7 @@ export async function runAgent(agent: IAnthropicAgent, prompt: string, layout: A const cwd = process.cwd(); const model = 'claude-sonnet-4-6'; - const cacheTtl = '5m' as const; + const cacheTtl = CacheTtl.OneHour; const transformToolResult = (toolName: string, output: unknown): unknown => { const result = refTransform(toolName, output); @@ -44,10 +44,10 @@ export async function runAgent(agent: IAnthropicAgent, prompt: string, layout: A const { port, done } = agent.runAgent({ model, - maxTokens: 8000, + maxTokens: 32000, messages: [prompt], systemPrompts, - cacheTtl: '1h', + cacheTtl, transformToolResult, pauseAfterCompact: true, compactInputTokens: 150_000, diff --git a/packages/claude-sdk/src/index.ts b/packages/claude-sdk/src/index.ts index 15adc04..33b4866 100644 --- a/packages/claude-sdk/src/index.ts +++ b/packages/claude-sdk/src/index.ts @@ -3,13 +3,12 @@ import type { AuthCredentials } from './private/Auth/types'; import { calculateCost } from './private/pricing'; import { createAnthropicAgent } from './public/createAnthropicAgent'; import { defineTool } from './public/defineTool'; -import { AnthropicBeta } from './public/enums'; +import { AnthropicBeta, CacheTtl } from './public/enums'; import { IAnthropicAgent } from './public/interfaces'; import type { AnthropicAgentOptions, AnthropicBetaFlags, AnyToolDefinition, - CacheTtl, ConsumerMessage, ILogger, RunAgentQuery, @@ -28,26 +27,5 @@ import type { } from './public/types'; export type { BetaMessageParam } from '@anthropic-ai/sdk/resources/beta.js'; -export type { - AnthropicAgentOptions, - AnthropicBetaFlags, - AnyToolDefinition, - AuthCredentials, - CacheTtl, - ConsumerMessage, - ILogger, - RunAgentQuery, - RunAgentResult, - SdkDone, - SdkError, - SdkMessage, - SdkMessageEnd, - SdkMessageStart, - SdkMessageText, - SdkMessageUsage, - SdkQuerySummary, - SdkToolApprovalRequest, - ToolDefinition, - ToolOperation, -}; -export { AnthropicAuth, AnthropicBeta, calculateCost, createAnthropicAgent, defineTool, IAnthropicAgent }; +export type { AnthropicAgentOptions, AnthropicBetaFlags, AnyToolDefinition, AuthCredentials, ConsumerMessage, ILogger, RunAgentQuery, RunAgentResult, SdkDone, SdkError, SdkMessage, SdkMessageEnd, SdkMessageStart, SdkMessageText, SdkMessageUsage, SdkQuerySummary, SdkToolApprovalRequest, ToolDefinition, ToolOperation }; +export { AnthropicAuth, AnthropicBeta, CacheTtl, calculateCost, createAnthropicAgent, defineTool, IAnthropicAgent }; diff --git a/packages/claude-sdk/src/private/AgentRun.ts b/packages/claude-sdk/src/private/AgentRun.ts index f85f772..970e95c 100644 --- a/packages/claude-sdk/src/private/AgentRun.ts +++ b/packages/claude-sdk/src/private/AgentRun.ts @@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto'; import type { MessagePort } from 'node:worker_threads'; import type { Anthropic } from '@anthropic-ai/sdk'; import type { BetaCompactionBlockParam, BetaTextBlockParam, BetaThinkingBlockParam, BetaToolUseBlockParam } from '@anthropic-ai/sdk/resources/beta.mjs'; +import { CacheTtl } from '../public/enums'; import type { AnyToolDefinition, ILogger, RunAgentQuery, SdkMessage } from '../public/types'; import { AgentChannel } from './AgentChannel'; import { ApprovalState } from './ApprovalState'; @@ -79,7 +80,7 @@ export class AgentRun { return; } - const cacheTtl = this.#options.cacheTtl ?? '5m'; + const cacheTtl = this.#options.cacheTtl ?? CacheTtl.OneHour; const costUsd = calculateCost(result.usage, this.#options.model, cacheTtl); const contextWindow = getContextWindow(this.#options.model); this.#channel.send({ type: 'message_usage', ...result.usage, costUsd, contextWindow } satisfies SdkMessage); diff --git a/packages/claude-sdk/src/private/RequestBuilder.ts b/packages/claude-sdk/src/private/RequestBuilder.ts index 119c4f0..b957bfb 100644 --- a/packages/claude-sdk/src/private/RequestBuilder.ts +++ b/packages/claude-sdk/src/private/RequestBuilder.ts @@ -1,8 +1,8 @@ import type { Anthropic } from '@anthropic-ai/sdk'; import type { BetaMessageStreamParams } from '@anthropic-ai/sdk/resources/beta/messages.js'; import type { BetaCacheControlEphemeral, BetaClearThinking20251015Edit, BetaClearToolUses20250919Edit, BetaCompact20260112Edit, BetaContentBlockParam, BetaContextManagementConfig, BetaTextBlockParam, BetaToolUnion } from '@anthropic-ai/sdk/resources/beta.mjs'; -import { AnthropicBeta } from '../public/enums'; -import { CacheTtl, type RunAgentQuery } from '../public/types'; +import { AnthropicBeta, CacheTtl } from '../public/enums'; +import type { RunAgentQuery } from '../public/types'; import { AGENT_SDK_PREFIX } from './consts'; export type RequestParams = { diff --git a/packages/claude-sdk/src/private/pricing.ts b/packages/claude-sdk/src/private/pricing.ts index 970ce78..52f9085 100644 --- a/packages/claude-sdk/src/private/pricing.ts +++ b/packages/claude-sdk/src/private/pricing.ts @@ -1,4 +1,4 @@ -export type CacheTtl = '5m' | '1h'; +import type { CacheTtl } from '../public/enums'; type ModelRates = { input: number; diff --git a/packages/claude-sdk/src/public/enums.ts b/packages/claude-sdk/src/public/enums.ts index c29ea9a..4f9c0b8 100644 --- a/packages/claude-sdk/src/public/enums.ts +++ b/packages/claude-sdk/src/public/enums.ts @@ -1,3 +1,8 @@ +export enum CacheTtl { + FiveMinutes = '5m', + OneHour = '1h', +} + export enum AnthropicBeta { /** * @see https://platform.claude.com/docs/en/build-with-claude/compaction diff --git a/packages/claude-sdk/src/public/types.ts b/packages/claude-sdk/src/public/types.ts index 0926490..cc2f982 100644 --- a/packages/claude-sdk/src/public/types.ts +++ b/packages/claude-sdk/src/public/types.ts @@ -1,7 +1,7 @@ import type { MessagePort } from 'node:worker_threads'; import type { Model } from '@anthropic-ai/sdk/resources/messages'; import type { z } from 'zod'; -import type { AnthropicBeta } from './enums'; +import type { AnthropicBeta, CacheTtl } from './enums'; export type ToolOperation = 'read' | 'write' | 'delete'; @@ -25,11 +25,6 @@ export type AnyToolDefinition = { export type AnthropicBetaFlags = Partial>; -export enum CacheTtl { - FiveMinutes = '5m', - OneHour = '1h', -} - export type RunAgentQuery = { model: Model; thinking?: boolean; @@ -47,7 +42,6 @@ export type RunAgentQuery = { }; /** Messages sent from the SDK to the consumer via the MessagePort. */ - export type SdkMessageStart = { type: 'message_start' }; export type SdkMessageText = { type: 'message_text'; text: string }; export type SdkMessageThinking = { type: 'message_thinking'; text: string }; diff --git a/packages/claude-sdk/test/RequestBuilder.spec.ts b/packages/claude-sdk/test/RequestBuilder.spec.ts index 0e5e3b2..a43efe9 100644 --- a/packages/claude-sdk/test/RequestBuilder.spec.ts +++ b/packages/claude-sdk/test/RequestBuilder.spec.ts @@ -3,8 +3,8 @@ import { describe, expect, it } from 'vitest'; import type { BetaMessageParam } from '../src/index.js'; import { AGENT_SDK_PREFIX } from '../src/private/consts.js'; import { buildRequestParams } from '../src/private/RequestBuilder.js'; -import { AnthropicBeta } from '../src/public/enums.js'; -import { type AnyToolDefinition, CacheTtl, type RunAgentQuery } from '../src/public/types.js'; +import { AnthropicBeta, CacheTtl } from '../src/public/enums.js'; +import type { AnyToolDefinition, RunAgentQuery } from '../src/public/types.js'; // --------------------------------------------------------------------------- // Helpers