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
6 changes: 6 additions & 0 deletions .changeset/itchy-hounds-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'mycoder-agent': patch
'mycoder': patch
---

Add ability to enable/disable token caching via config values
1 change: 1 addition & 0 deletions packages/agent/src/core/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class TokenUsage {
export class TokenTracker {
public tokenUsage = new TokenUsage();
public children: TokenTracker[] = [];
public tokenCache?: boolean;

constructor(
public readonly name: string = 'unnamed',
Expand Down
19 changes: 14 additions & 5 deletions packages/agent/src/core/toolAgent/toolAgentCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ export const toolAgent = async (
});
});

// Apply cache control to messages for token caching
const messagesWithCacheControl = [
createCacheControlMessageFromSystemPrompt(systemPrompt),
...addCacheControlToMessages(messages),
];
// Apply cache control to messages for token caching if enabled
const messagesWithCacheControl =
tokenTracker.tokenCache !== false && context.tokenCache !== false
? [
createCacheControlMessageFromSystemPrompt(systemPrompt),
...addCacheControlToMessages(messages),
]
: [
{
role: 'system',
content: systemPrompt,
} as CoreMessage,
...messages,
];

const generateTextProps = {
model: config.model,
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
tokenTracker: TokenTracker;
githubMode: boolean;
customPrompt?: string;
tokenCache?: boolean;
};

export type Tool<TParams = Record<string, any>, TReturn = any> = {

Check warning on line 24 in packages/agent/src/core/types.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type

Check warning on line 24 in packages/agent/src/core/types.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type
name: string;
description: string;
parameters: z.ZodType<TParams>;
Expand All @@ -40,7 +41,7 @@
export type ToolCall = {
id: string;
name: string;
input: any;

Check warning on line 44 in packages/agent/src/core/types.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type
};

export type TextContent = {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ mycoder --modelProvider openai --modelName gpt-4o-2024-05-13 "Your prompt here"
- `pageFilter`: Method to process webpage content: 'simple', 'none', or 'readability' (default: `none`)
- `ollamaBaseUrl`: Base URL for Ollama API (default: `http://localhost:11434/api`)
- `customPrompt`: Custom instructions to append to the system prompt for both main agent and sub-agents (default: `""`)
- `tokenCache`: Enable token caching for LLM API calls (default: `true`)

Example:

Expand All @@ -143,6 +144,9 @@ mycoder config set ollamaBaseUrl http://your-ollama-server:11434/api

# Set custom instructions for the agent
mycoder config set customPrompt "Always prioritize readability and simplicity in your code. Prefer TypeScript over JavaScript when possible."

# Disable token caching for LLM API calls
mycoder config set tokenCache false
```

## Environment Variables
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/commands/$default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export const command: CommandModule<SharedOptions, DefaultArgs> = {
try {
// Get configuration for model provider and name
const userConfig = getConfig();
// Use command line option if provided, otherwise use config value
tokenTracker.tokenCache =
argv.tokenCache !== undefined ? argv.tokenCache : userConfig.tokenCache;

const userModelProvider = argv.modelProvider || userConfig.modelProvider;
const userModelName = argv.modelName || userConfig.modelName;

Expand Down Expand Up @@ -177,6 +181,8 @@ export const command: CommandModule<SharedOptions, DefaultArgs> = {
tokenTracker,
githubMode: config.githubMode,
customPrompt: config.customPrompt,
tokenCache:
argv.tokenCache !== undefined ? argv.tokenCache : config.tokenCache,
});

const output =
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type SharedOptions = {
readonly modelProvider?: string;
readonly modelName?: string;
readonly profile?: boolean;
readonly tokenCache?: boolean;
};

export const sharedOptions = {
Expand Down Expand Up @@ -72,4 +73,8 @@ export const sharedOptions = {
description: 'Custom Sentry DSN for error tracking',
hidden: true,
} as const,
tokenCache: {
type: 'boolean',
description: 'Enable token caching for LLM API calls',
} as const,
};
1 change: 1 addition & 0 deletions packages/cli/src/settings/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultConfig = {
ollamaBaseUrl: 'http://localhost:11434/api',
customPrompt: '',
profile: false,
tokenCache: true,
};

export type Config = typeof defaultConfig;
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/tests/settings/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Config', () => {
ollamaBaseUrl: 'http://localhost:11434/api',
profile: false,
customPrompt: '',
tokenCache: true,
});
expect(fs.existsSync).toHaveBeenCalledWith(mockConfigFile);
});
Expand Down Expand Up @@ -80,6 +81,7 @@ describe('Config', () => {
ollamaBaseUrl: 'http://localhost:11434/api',
profile: false,
customPrompt: '',
tokenCache: true,
});
});
});
Expand Down
Loading