@caeliq/llms 1.0.65 → 1.0.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +4 -2
  2. package/dist/cjs/server.cjs +287 -227
  3. package/dist/cjs/server.cjs.map +4 -4
  4. package/dist/cursor-sdk/auth-exchange-cache.d.ts +23 -0
  5. package/dist/cursor-sdk/lifecycle-planner.d.ts +16 -4
  6. package/dist/cursor-sdk/prompt.d.ts +10 -0
  7. package/dist/cursor-sdk/session.d.ts +23 -0
  8. package/dist/cursor-sdk/turn-output.d.ts +3 -0
  9. package/dist/cursor-sdk/usage.d.ts +17 -0
  10. package/dist/esm/server.mjs +287 -227
  11. package/dist/esm/server.mjs.map +4 -4
  12. package/dist/routing/inbound-pipeline.d.ts +6 -2
  13. package/dist/routing/protocol-adapter.d.ts +2 -0
  14. package/dist/routing/protocol-endpoints.d.ts +14 -0
  15. package/dist/server.d.ts +4 -0
  16. package/dist/tests/anthropic.message-start-usage.test.d.ts +1 -0
  17. package/dist/tests/cache-outcome.d.ts +1 -0
  18. package/dist/tests/codex-system-instructions.d.ts +1 -0
  19. package/dist/tests/cross-protocol.matrix.d.ts +1 -0
  20. package/dist/tests/cursor-sdk.auth-exchange-cache.d.ts +1 -0
  21. package/dist/tests/latency-cadence.d.ts +1 -0
  22. package/dist/tests/message-debug.d.ts +1 -0
  23. package/dist/tests/openrouter-headers.d.ts +1 -0
  24. package/dist/tests/proxy-dispatcher-cache.d.ts +1 -0
  25. package/dist/tests/responses.encrypted-content-cache.d.ts +1 -0
  26. package/dist/tests/sse-event-native.d.ts +1 -0
  27. package/dist/tests/tool-content.multimodal.d.ts +1 -0
  28. package/dist/tests/transformer-plan.d.ts +1 -0
  29. package/dist/tests/wire-keep.d.ts +1 -0
  30. package/dist/transformer/antigravity-auth.transformer.d.ts +2 -0
  31. package/dist/transformer/codex.transformer.d.ts +20 -44
  32. package/dist/transformer/cursor-sdk.transformer.d.ts +2 -0
  33. package/dist/transformer/openai.responses.transformer.d.ts +1 -1
  34. package/dist/transformer/openai.transformer.d.ts +2 -0
  35. package/dist/transformer/opencode-headers.transformer.d.ts +3 -0
  36. package/dist/transformer/openrouter.transformer.d.ts +15 -1
  37. package/dist/transformer/reasoning.transformer.d.ts +1 -0
  38. package/dist/types/llm.d.ts +11 -0
  39. package/dist/types/transformer.d.ts +10 -0
  40. package/dist/utils/cache-outcome.d.ts +79 -0
  41. package/dist/utils/cache-prefix-debug.d.ts +21 -1
  42. package/dist/utils/cacheControl.d.ts +8 -0
  43. package/dist/utils/deepseek.util.d.ts +1 -1
  44. package/dist/utils/message-debug.d.ts +45 -0
  45. package/dist/utils/nested-agent.d.ts +26 -0
  46. package/dist/utils/openai.responses.util.d.ts +21 -0
  47. package/dist/utils/paced-sse.d.ts +38 -0
  48. package/dist/utils/request-latency.d.ts +37 -0
  49. package/dist/utils/request.d.ts +8 -1
  50. package/dist/utils/responses.encrypted-content-cache.d.ts +42 -0
  51. package/dist/utils/sse/incremental-parser.d.ts +35 -0
  52. package/dist/utils/sse-debug-tap.d.ts +44 -2
  53. package/dist/utils/stream-peek.d.ts +20 -0
  54. package/dist/utils/stream.d.ts +26 -3
  55. package/dist/utils/token-count-worker.d.ts +12 -0
  56. package/dist/utils/tool-content.d.ts +54 -0
  57. package/dist/utils/transformer-plan.d.ts +30 -0
  58. package/dist/utils/vertex-claude.util.d.ts +1 -1
  59. package/package.json +4 -4
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @cursor/sdk POSTs /auth/exchange_user_api_key on every Agent.create and
3
+ * models.list. Subagent turns mint new session keys, so Agent.create runs
4
+ * again and the SDK does not cache the access token across those calls.
5
+ *
6
+ * Wrap global fetch: coalesce in-flight exchanges per crsr_ key, reuse the
7
+ * access token until a Cursor request with that token returns 401.
8
+ *
9
+ * Critical: never forward a caller's AbortSignal onto the shared exchange.
10
+ * Agent.create often cancels while another waiter still needs the token; if
11
+ * the first caller's signal aborts the shared fetch, every waiter rejects
12
+ * with AbortError and Node surfaces unhandledRejection / late-handled
13
+ * PromiseRejectionHandledWarning.
14
+ */
15
+ /**
16
+ * Drop a cached exchanged token for a dashboard API key (crsr_... or Bearer).
17
+ * Call when Cursor reports auth failure via run status / AuthenticationError —
18
+ * those paths often never surface as an HTTP 401 on the wrapped fetch.
19
+ */
20
+ export declare function invalidateCursorAuthExchange(crsrApiKey: string): void;
21
+ /** Re-wrap if tests or other code replaced globalThis.fetch. */
22
+ export declare function installCursorAuthExchangeCache(): void;
23
+ export declare function __resetCursorAuthExchangeCacheForTests(): void;
@@ -43,7 +43,9 @@ export type CursorLifecycleInput<TParked extends CursorParkedToolRef = CursorPar
43
43
  toolResults: readonly TResult[];
44
44
  };
45
45
  };
46
- export type CursorLifecycleRetirementReason = "poisoned-session" | "inconsistent-unused-session" | "unknown-context-alignment" | "divergent-context-alignment" | "active-different-turn" | "dead-parked-run" | "parked-turn-has-steering" | "parked-tool-results-mismatch" | "orphaned-tool-results";
46
+ /** Hard remint only soft alignment cases stay on the same agent. */
47
+ export type CursorLifecycleRetirementReason = "poisoned-session";
48
+ export type CursorLifecycleIncrementalReason = "strictly-aligned-idle-session" | "unknown-context-alignment" | "divergent-context-alignment" | "active-different-turn" | "dead-parked-run" | "parked-turn-has-steering" | "parked-tool-results-mismatch" | "orphaned-tool-results";
47
49
  export type CursorLifecycleRetirementPlan = {
48
50
  action: "retire-and-replay-full";
49
51
  reason: CursorLifecycleRetirementReason;
@@ -54,13 +56,23 @@ export type CursorLifecyclePlan<TParked extends CursorParkedToolRef = CursorPark
54
56
  matches: ExactParkedResultMatch<TParked, TResult>;
55
57
  } | {
56
58
  action: "send-full";
57
- reason: "unused-session";
59
+ reason: "unused-session" | "inconsistent-unused-session";
58
60
  } | {
59
61
  action: "send-incremental";
60
- reason: "strictly-aligned-idle-session";
62
+ reason: CursorLifecycleIncrementalReason;
61
63
  } | CursorLifecycleRetirementPlan;
62
64
  /**
63
- * Choose the only safe lifecycle action for a new, non-coalesced host turn.
65
+ * Soft incremental reasons where the runner must cancel/abandon any prior
66
+ * active or parked run before `agent.send` — without reminting the agent.
67
+ */
68
+ export declare const CURSOR_SOFT_CANCEL_THEN_INCREMENTAL: Set<CursorLifecycleIncrementalReason>;
69
+ /**
70
+ * Choose the lifecycle action for a new, non-coalesced host turn.
71
+ *
72
+ * Soft transcript-alignment mismatches stay on the same Cursor agent via
73
+ * `send-incremental` (caller cancels parked/active runs when needed). Hard
74
+ * remint (`retire-and-replay-full`) is reserved for poisoned sessions; send/
75
+ * stream failure recovery remints in the runner, not here.
64
76
  *
65
77
  * This function is intentionally pure. It neither resolves parked tools nor
66
78
  * changes session state; the caller executes the returned plan under its
@@ -3,6 +3,11 @@ import { type UnifiedTurnIntent } from "../types/turn-intent";
3
3
  import type { SDKUserMessage } from "@cursor/sdk";
4
4
  import { type HostEnvironment } from "./host-env";
5
5
  export declare function buildBridgeSystemGuidance(request: UnifiedChatRequest, workspaceDir: string, hostEnv?: HostEnvironment): string;
6
+ /**
7
+ * Identity of the bridge preamble actually sent on an agent. Unchanged
8
+ * follow-ups skip re-sending it so Cursor history does not stack copies.
9
+ */
10
+ export declare function bridgePromptGuidanceFingerprint(request: UnifiedChatRequest, workspaceDir: string, hostEnv: HostEnvironment): string;
6
11
  /**
7
12
  * Short restatement appended after the transcript. The guidance above is far
8
13
  * from the generation point once history is flattened in; this is the last
@@ -37,6 +42,11 @@ export declare function toSdkPrompt(request: UnifiedChatRequest, options: {
37
42
  hostEnv?: HostEnvironment;
38
43
  /** Protocol semantics recovered before Anthropic content was flattened. */
39
44
  turnIntent?: UnifiedTurnIntent;
45
+ /**
46
+ * Bridge preamble + tail reminder. Default true. Set false on follow-ups
47
+ * whose tool catalog and host env already went out on this agent.
48
+ */
49
+ includeBridgeGuidance?: boolean;
40
50
  }): SDKUserMessage;
41
51
  export type TrailingCursorToolTurn = {
42
52
  toolResults: Array<{
@@ -63,6 +63,11 @@ export type CursorSdkSession = {
63
63
  compatibilityStamp?: string;
64
64
  /** Fingerprint of the host env baked into the workspace rules/deny hooks. */
65
65
  guidanceFingerprint?: string;
66
+ /**
67
+ * Bridge preamble last actually sent on this agent (host env + tool catalog).
68
+ * Unchanged follow-ups omit the preamble so history does not stack copies.
69
+ */
70
+ lastBridgePromptGuidanceFingerprint?: string;
66
71
  /**
67
72
  * Set when the local SDK handles are no longer trustworthy. The manager must
68
73
  * not hand this agent out for a future request.
@@ -138,11 +143,29 @@ export declare function shouldEnableCursorSandbox(requested?: boolean): boolean;
138
143
  * path correction) is updated here regardless.
139
144
  */
140
145
  export declare function refreshWorkspaceGuidance(session: CursorSdkSession, hostEnv: HostEnvironment): boolean;
146
+ /**
147
+ * Cursor SDK session directory key. Prefer explicit client conversation ids
148
+ * over hashing prompt text — never include system / harness version.
149
+ *
150
+ * Claude Code subagents share the parent session id. Mix first-user text so
151
+ * parallel Task agents each get their own Cursor Agent instead of collapsing
152
+ * onto one turn registry slot.
153
+ */
141
154
  export declare function buildSessionKey(input: {
142
155
  headerSession?: string;
156
+ /** Inbound-captured Claude/OpenCode session id (protocolContext / req). */
157
+ clientSessionId?: string;
143
158
  metadataUserId?: string;
144
159
  model?: string;
160
+ /** Anonymous clients only: first user text (not system). */
161
+ firstUserText?: string;
162
+ /** @deprecated Use firstUserText. Kept for call-site compatibility. */
145
163
  systemAndFirstUser?: string;
164
+ /**
165
+ * Claude Code Task / subagent turn. Parent session id alone is not unique
166
+ * across parallel subagents.
167
+ */
168
+ isSubagent?: boolean;
146
169
  }): string;
147
170
  /** True while a session has a live run, open stream, or unresolved host tools. */
148
171
  export declare function isSessionInFlight(session: CursorSdkSession): boolean;
@@ -71,6 +71,9 @@ export declare class CursorTurnRegistry {
71
71
  responseKind: "stream" | "json";
72
72
  signal?: AbortSignal;
73
73
  }): Promise<CursorTurnLease>;
74
+ peekActive(sessionKey: string): {
75
+ fingerprint: string;
76
+ } | undefined;
74
77
  clear(): void;
75
78
  }
76
79
  export declare const globalCursorTurnRegistry: CursorTurnRegistry;
@@ -7,6 +7,13 @@ export type OpenAiUsage = {
7
7
  cached_tokens?: number;
8
8
  };
9
9
  };
10
+ export type CursorUsageCounters = {
11
+ inputTokens: number;
12
+ outputTokens: number;
13
+ cacheRead: number;
14
+ cacheWrite: number;
15
+ reasoningTokens: number;
16
+ };
10
17
  /**
11
18
  * Rough char→token estimate for mid-turn usage before Cursor emits a usage
12
19
  * event (and as the host-facing request usage even when it does).
@@ -18,11 +25,21 @@ export declare function estimateTokens(chars: number): number;
18
25
  export declare function estimateRequestPromptTokens(request: UnifiedChatRequest): number;
19
26
  /** Build OpenAI-style usage for a single CCR request (not Cursor session totals). */
20
27
  export declare function requestUsageFromEstimate(promptTokens: number, outputChars: number, cacheReadTokens?: number): OpenAiUsage;
28
+ export declare function cursorUsageCountersFromSdk(raw: OpenAiUsage | undefined): CursorUsageCounters | undefined;
21
29
  /**
22
30
  * Cursor SDK usage is session-cumulative, while Claude Code needs per-request
23
31
  * usage. Use SDK usage only as a cache-read ratio and apply it to CCR's
24
32
  * current-request prompt estimate.
33
+ *
34
+ * Kept for tests; prefer buildAccurateUsageFromSdk for turn-end reporting.
25
35
  */
26
36
  export declare function cacheReadFromSdkDelta(current: OpenAiUsage | undefined, previous: OpenAiUsage | undefined, promptTokens: number): number;
37
+ /**
38
+ * Build per-request OpenAI usage from SDK TurnEnded counters, normalized to
39
+ * the per-request prompt estimate but preserving Cursor's cache proportions
40
+ * (same math as cursor-opencode-provider buildLanguageModelV3UsageFromCounters).
41
+ * Falls back to chars/4-based completion tokens when SDK output is absent.
42
+ */
43
+ export declare function buildAccurateUsageFromSdk(sdkRaw: OpenAiUsage | undefined, promptTokens: number, outputChars: number, priorRaw?: OpenAiUsage | undefined): OpenAiUsage;
27
44
  /** Map SDK TokenUsage / usage message into OpenAI shape (diagnostics only). */
28
45
  export declare function usageFromSdk(message: any): OpenAiUsage | undefined;