Skip to content

Commit 4c4639e

Browse files
committed
fix(provider): address code review issues for in-memory platform
- Move platform creation outside retry loop to prevent resource accumulation - Implement no-op BlobStore methods to avoid runtime TypeErrors - Use crypto.randomUUID() for agentId and runId to prevent collisions - Add explicit type annotations to store interfaces for type safety
1 parent f8c33f1 commit 4c4639e

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/platform.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
import { createAgentPlatform, InMemoryRunEventNotifier } from "@cursor/sdk";
2+
import type { AgentRunStore, AgentCheckpointStore, RunEventStore, CreateAgentInput } from "@cursor/sdk";
3+
import crypto from "node:crypto";
24

35
/**
46
* Creates a minimal, in-memory platform for @cursor/sdk to avoid
57
* the native sqlite3 dependency which causes issues in CLI environments.
68
*/
79
export async function getInMemoryPlatform() {
8-
const noopStore: any = {
10+
const noopStore: AgentRunStore = {
911
getAgent: async () => null,
10-
createAgent: async (input: any) => ({
11-
agent: { agentId: input.agentId || "default" },
12-
run: { runId: "initial-run" },
12+
createAgent: async (input: CreateAgentInput) => ({
13+
agent: { agentId: input.agentId || `agent-${crypto.randomUUID()}` },
14+
run: { runId: `run-${crypto.randomUUID()}` },
1315
}),
1416
listAgents: async () => ({ items: [] }),
1517
getRun: async () => null,
1618
listRuns: async () => ({ items: [] }),
1719
markRunStarting: async () => {},
1820
markRunTerminal: async () => {},
19-
createFollowUpRun: async () => ({ runId: "follow-up" }),
21+
createFollowUpRun: async () => ({ runId: `run-${crypto.randomUUID()}` }),
2022
archiveAgent: async () => {},
2123
unarchiveAgent: async () => {},
2224
deleteAgent: async () => {},
2325
patchCheckpoint: async () => {},
2426
cancelRun: async () => {},
2527
};
2628

27-
const noopCheckpointStore: any = {
29+
const noopCheckpointStore: AgentCheckpointStore = {
2830
loadLatest: async () => null,
2931
saveCheckpoint: async () => "cp-ref",
30-
getBlobStore: async () => ({}),
32+
getBlobStore: async () => ({
33+
get: async () => null,
34+
put: async () => {},
35+
delete: async () => {},
36+
}),
3137
getFullConversation: async () => ({ turns: [] }),
3238
deleteAgent: async () => {},
3339
};
3440

35-
const noopEventStore: any = {
36-
appendRunEvent: async () => ({ runId: "run", offset: 0 }),
41+
const noopEventStore: RunEventStore = {
42+
appendRunEvent: async () => ({ runId: `run-${crypto.randomUUID()}`, offset: 0 }),
3743
listRunEvents: async () => ({ items: [] }),
3844
deleteRunEvents: async () => {},
3945
};

src/provider.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ async function performAgentCreationAttempt(deps: {
324324
Agent: { create: (opts: AgentCreateOpts) => Promise<unknown> };
325325
apiKey: string;
326326
modelId: string;
327+
platform: any;
327328
log: Logger;
328329
attempt: number;
329330
}): Promise<{ agent: SDKAgent } | { error: unknown; canRetry: boolean; delay: number }> {
330-
const { Agent, apiKey, modelId, log, attempt } = deps;
331+
const { Agent, apiKey, modelId, platform, log, attempt } = deps;
331332
try {
332333
log.debug("cursor-provider: calling Agent.create", { modelId, attempt });
333-
const platform = await getInMemoryPlatform();
334334
const agent = (await Agent.create({
335335
apiKey,
336336
model: { id: modelId },
@@ -354,9 +354,15 @@ async function performAgentCreationAttempt(deps: {
354354
async function createAgentWithRetry(deps: { apiKey: string; modelId: string; log: Logger }): Promise<SDKAgent> {
355355
const { Agent } = await import("@cursor/sdk");
356356
const { log } = deps;
357+
const platform = await getInMemoryPlatform();
357358

358359
for (let attempt = 1; attempt <= 3; attempt++) {
359-
const result = await performAgentCreationAttempt({ Agent: Agent as unknown as { create: (opts: AgentCreateOpts) => Promise<unknown> }, ...deps, attempt });
360+
const result = await performAgentCreationAttempt({
361+
Agent: Agent as unknown as { create: (opts: AgentCreateOpts) => Promise<unknown> },
362+
...deps,
363+
platform,
364+
attempt,
365+
});
360366

361367
if ("agent" in result) return result.agent;
362368
if (!result.canRetry) {

0 commit comments

Comments
 (0)