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
1 change: 1 addition & 0 deletions auth-cursor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Tools stay off unless the client sends them. The working directory is always emp
Other limits:

- No token-counting endpoint. `count_tokens` is a character estimate; billing uses counts Cursor reports after a run.
- Responses carry no `usage`. Cursor counts usage per run, and one run covers every tool round of a client turn, so the total re-counts the turn's context once per round. The run total goes only to the `cursor request completed` log line.
- `executor.http_request` returns 501.
- Remote images: only `http`/`https` to a non-loopback, non-private, non-metadata host. Credentials in the URL are refused. The error text never repeats the path or query. The host HTTP client still follows redirects, so a public URL that 302s onto loopback is not blocked here.
- Region/plan refusals fail that one request without parking the credential. A 401 or 429 that arrives as an `SdkErrorCode` still cools or retires the key.
Expand Down
11 changes: 6 additions & 5 deletions auth-cursor/go/chatcompletions.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type chatCompletion struct {
Created int64 `json:"created"`
Model string `json:"model"`
Choices []chatCompletionChoice `json:"choices"`
Usage *chatCompletionUsage `json:"usage,omitempty"`
}

func parseChatRequest(payload []byte) (chatRequest, error) {
Expand Down Expand Up @@ -381,7 +380,11 @@ func convertUsage(usage *sdkv1.TokenUsage) *chatCompletionUsage {
}
}

func buildCompletion(id, model, text string, usage *sdkv1.TokenUsage) ([]byte, error) {
// Replies carry no usage. Cursor reports usage once per run, and a run spans every tool
// round of a client turn, so its total re-counts the whole turn's context on the last
// reply. Callers that size their context from usage would read that as overflow; without
// usage they estimate per request instead.
func buildCompletion(id, model, text string) ([]byte, error) {
finish := "stop"
return json.Marshal(chatCompletion{
ID: id,
Expand All @@ -393,11 +396,10 @@ func buildCompletion(id, model, text string, usage *sdkv1.TokenUsage) ([]byte, e
Message: &chatCompletionMessage{Role: "assistant", Content: text},
FinishReason: &finish,
}},
Usage: convertUsage(usage),
})
}

func buildStreamChunk(id, model string, delta chatCompletionDelta, finishReason *string, usage *sdkv1.TokenUsage) []byte {
func buildStreamChunk(id, model string, delta chatCompletionDelta, finishReason *string) []byte {
chunk := chatCompletion{
ID: id,
Object: "chat.completion.chunk",
Expand All @@ -408,7 +410,6 @@ func buildStreamChunk(id, model string, delta chatCompletionDelta, finishReason
Delta: &delta,
FinishReason: finishReason,
}},
Usage: convertUsage(usage),
}
raw, errMarshal := json.Marshal(chunk)
if errMarshal != nil {
Expand Down
10 changes: 5 additions & 5 deletions auth-cursor/go/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func execute(raw []byte) ([]byte, error) {
if len(result.toolCalls) > 0 {
payload, errBody = buildToolCallCompletion(newCompletionID(), model, result.text, result.toolCalls)
} else {
payload, errBody = buildCompletion(newCompletionID(), model, result.text, result.usage)
payload, errBody = buildCompletion(newCompletionID(), model, result.text)
}
if errBody != nil {
logCtx.failed(errBody.Error())
Expand Down Expand Up @@ -125,7 +125,7 @@ func forwardStream(ctx context.Context, streamID, model string, framing streamFr
delta.Role = "assistant"
roleSent = true
}
chunk := buildStreamChunk(completionID, model, delta, nil, nil)
chunk := buildStreamChunk(completionID, model, delta, nil)
return emitPluginStreamChunk(streamID, framing.frame(chunk))
}

Expand All @@ -145,7 +145,7 @@ func forwardStream(ctx context.Context, streamID, model string, framing streamFr
return nil
}
finish := "stop"
final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish, result.usage)
final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish)
if errEmit := emitPluginStreamChunk(streamID, framing.frame(final)); errEmit != nil {
logCtx.failed(errEmit.Error())
return errEmit
Expand All @@ -168,13 +168,13 @@ func emitToolCallStream(streamID, completionID, model string, framing streamFram
delta.Role = "assistant"
*roleSent = true
}
chunk := buildStreamChunk(completionID, model, delta, nil, nil)
chunk := buildStreamChunk(completionID, model, delta, nil)
if errEmit := emitPluginStreamChunk(streamID, framing.frame(chunk)); errEmit != nil {
return errEmit
}
}
finish := "tool_calls"
final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish, nil)
final := buildStreamChunk(completionID, model, chatCompletionDelta{}, &finish)
if errEmit := emitPluginStreamChunk(streamID, framing.frame(final)); errEmit != nil {
return errEmit
}
Expand Down
8 changes: 4 additions & 4 deletions auth-cursor/go/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func TestExecuteAssemblesCompletion(t *testing.T) {
if got := completion.Choices[0].Message.Content; got != "Hello world" {
t.Errorf("content = %q, want %q", got, "Hello world")
}
// Cache reads are prompt tokens served from cache, so they belong on the prompt side.
if completion.Usage == nil || completion.Usage.PromptTokens != 10 || completion.Usage.CompletionTokens != 2 {
t.Errorf("usage = %+v, want prompt=10 completion=2", completion.Usage)
// The run's usage total is not this request's usage, so the reply carries none.
if usage := gjson.GetBytes(response.Payload, "usage"); usage.Exists() {
t.Errorf("usage = %s, want none", usage.Raw)
}
// A non-streaming request has nothing to do with deltas, so it must not ask for them.
if len(bridge.enableDeltas) != 1 || bridge.enableDeltas[0] {
Expand Down Expand Up @@ -1047,7 +1047,7 @@ func TestExecuteReportsAnUnfetchableImageAsARequestFault(t *testing.T) {

func TestBuildStreamChunkEmitsBareChunkJSON(t *testing.T) {
finish := "stop"
chunk := buildStreamChunk("chatcmpl-1", "fake-model", chatCompletionDelta{Content: "Hello"}, &finish, nil)
chunk := buildStreamChunk("chatcmpl-1", "fake-model", chatCompletionDelta{Content: "Hello"}, &finish)
// The chat-completions SSE writer adds the framing, so the chunk itself must stay bare.
if strings.HasPrefix(string(chunk), "data:") {
t.Fatalf("chunk carries SSE framing: %q", chunk)
Expand Down
12 changes: 5 additions & 7 deletions auth-cursor/go/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func decodeCompletion(t *testing.T, raw []byte) chatCompletion {
if errUnmarshal := json.Unmarshal(env.Result, &response); errUnmarshal != nil {
t.Fatalf("decode executor response: %v", errUnmarshal)
}
// Cursor's usage is per run, and a run spans every tool round, so no reply carries it.
if usage := gjson.GetBytes(response.Payload, "usage"); usage.Exists() {
t.Fatalf("usage = %s, want none", usage.Raw)
}
var completion chatCompletion
if errUnmarshal := json.Unmarshal(response.Payload, &completion); errUnmarshal != nil {
t.Fatalf("decode completion: %v", errUnmarshal)
Expand Down Expand Up @@ -259,9 +263,6 @@ func TestExecuteToolCallRoundTrip(t *testing.T) {
if finishReason(t, first) != "tool_calls" {
t.Fatalf("finish_reason = %q, want tool_calls", finishReason(t, first))
}
if first.Usage != nil {
t.Fatalf("usage = %+v, want none on a tool turn", first.Usage)
}
if first.Choices[0].Message == nil || len(first.Choices[0].Message.ToolCalls) != 1 {
t.Fatalf("tool_calls = %+v", first.Choices[0].Message)
}
Expand Down Expand Up @@ -298,9 +299,6 @@ func TestExecuteToolCallRoundTrip(t *testing.T) {
if second.Choices[0].Message.Content != "Hello world" {
t.Errorf("content = %q, want Hello world", second.Choices[0].Message.Content)
}
if second.Usage == nil || second.Usage.PromptTokens != 10 || second.Usage.CompletionTokens != 2 {
t.Errorf("usage = %+v, want prompt=10 completion=2", second.Usage)
}
if got := bridge.closedAgents(); len(got) != 1 || got[0] != "agent-1" {
t.Errorf("closed agents = %v, want [agent-1]", got)
}
Expand Down Expand Up @@ -632,7 +630,7 @@ func TestBuildStreamChunkEmitsIndexedToolCalls(t *testing.T) {
Arguments: `{"city":"Paris"}`,
},
}},
}, &finish, nil)
}, &finish)
if gjson.GetBytes(chunk, "choices.0.delta.tool_calls.0.index").Int() != 0 {
t.Errorf("index omitted: %s", chunk)
}
Expand Down
Loading