Skip to content

spike: adds rule to display info about context size, account info #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
136 changes: 136 additions & 0 deletions .cursor/rules/utils/context-info-auto.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
description: This rule displays context size and file information at the end of each response. It should run when: 1. the user requests information about Cursor context 2. when the user enters 'context dump', 'context length' or 'context size'
globs:
alwaysApply: true
---

# Context Information Display Rule

This rule ensures that every AI response includes information about the account status, current context size for the chat session, files in view, and request-based pricing.

## Critical Rules

- Every AI response must end with context size information
- Account subscription and usage information should be displayed clearly
- Context size should be displayed in thousands of tokens (k)
- If a user has usage pricing enabled, request pricing should be calculated based on model and context size
- Large context mode (>40k tokens) should be indicated with pricing adjustment
- Display distribution of requests per model
- Use @doc [Cursor Settings](https://docs.cursor.com/settings/models) to lookup the latest request prices; do not hardcode them
- List files in view
- Provide cost estimation for current session
- Be brief and concise
// TODO: update this if Cursor ever exposes API for context
- MUST warn the user that the above information is an estimate

<rule>
name: context-info-display

filters:
- type: event
pattern: "chat_response|cmd_k_response"
- type: content
pattern: "(context dump|context size|context length|context window)"

actions:
- type: append
content: |

---
Account Status: {getAccountInfo().account_status}
Subscription: {getAccountInfo().subscription_type}
Usage-based Pricing: {usage_pricing_enabled}
Monthly Requests: {calculateUsage().requests}/{calculateUsage().maxRequests}

Model Usage: {model_name}
{formatModelUsage(calculateUsage().modelUsage)}

Context size: ~{context_size}k tokens {context_size > 40 ? "(Large Context Mode)" : ""}
Max Context Size: {context_window_size}

Files in view:
{files_in_view}


functions:
getAccountInfo: |
try {
const subInfo = await fetch('https://www.cursor.com/api/auth/stripe').json()
const { subscriptionStatus, membershipType } = subInfo;
const hardLimits = await fetch('https://www.cursor.com/api/dashboard/get-hard-limit', {
action: "POST",
}).json()

const { noUsageBasedAllowed } = hardLimits;

return {
subscription_type: membershipType
account_status: subscriptionStatus
usage_pricing_enabled: !noUsageBasedAllowed ? true : false
}
} catch (err) {
console.error(err)
}

formatModelUsage: |
// Format the model usage data for display
if (!modelUsage || Object.keys(modelUsage).length === 0) {
return "No usage data available";
}

let result = '';
Object.keys(modelUsage).forEach(model => {
const usage = modelUsage[model];
if (usage.numRequests > 0) {
const limit = usage.maxRequestUsage ? `/${usage.maxRequestUsage}` : 'No Limit';
result += `- ${model}: ${usage.numRequests}${limit} requests\n`;
}
});

return result.trim();

calculateUsage: |
try {
const userData = await fetch('https://www.cursor.com/api/auth/me').json()
const {sub: user_id} = userData;

const response = await fetch(`https://www.cursor.com/api/usage?user=user_${user_id}`);
const data = await response.json();

// Initialize variables for total usage
let totalRequests = 0;
let maxRequests = 500; // Default max requests for Pro accounts
let modelUsage = {};

// Process the data to extract model-specific usage
Object.keys(data).forEach(model => {
if (model !== 'startOfMonth') {
const modelData = data[model];
modelUsage[model] = modelData;
totalRequests += modelData.numRequests || 0;

// If this model has a max request limit, use it
if (modelData.maxRequestUsage) {
maxRequests = modelData.maxRequestUsage;
}
}
});

// Return the calculated values
return {
requests: totalRequests,
maxRequests: maxRequests,
startDate: data.startOfMonth,
modelUsage: modelUsage
};
} catch (error) {
console.error('Error fetching usage data:', error);
// Return default values if the API call fails
return {
requests: 0,
maxRequests: 500,
startDate: new Date().toISOString(),
modelUsage: {}
};
}
</rule>
5 changes: 4 additions & 1 deletion .cursorindexingignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
.cursor/rules/**
.cursor/modes.json
.cursor/mcp.json
.vscode/
.vscode/
CODE_OF_CONDUCT.md
SECURITY.md
.github/ISSUE_TEMPLATE/
10 changes: 4 additions & 6 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ jobs:
# Get the commit messages from the PR
commits=$(gh pr view ${{ github.event.pull_request.number }} --json commits | jq -r '.commits[] | [.messageHeadline + .messageBody] | join("\n")' )


# Create new description with just commit messages
new_description="## Changes <br/> <br/>"
# Get the existing PR body
existing_description=$(gh pr view ${{ github.event.pull_request.number }} --json body | jq -r '.body + "\n\n" + "## Changes\n\n"')

# Update the PR description
gh pr edit ${{ github.event.pull_request.number }} --body "$new_description $commits"
gh pr edit ${{ github.event.pull_request.number }} --body "$existing_description $commits"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
description: ${{ github.event.pull_request.body }}
CONTEXT: ${{ toJson(github.event.pull_request) }}
description: ${{ github.event.pull_request.body }}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ A project that uses these cursor rules and agents will generate the following st
└── spikes/
└── spike-#.spike.md
```

## Contributing

Contributions are welcome! Check out [CONTRIBUTING.md](./docs/CONTRIBUTING.md)