diff --git a/src/openai-proxy.ts b/src/openai-proxy.ts index 7af77d8..8e01c01 100644 --- a/src/openai-proxy.ts +++ b/src/openai-proxy.ts @@ -152,7 +152,15 @@ async function handleChat(req: IncomingMessage, res: ServerResponse, log: Logger } else { const maxRetries = 3; let attempt = 0; - const { Agent } = (await import("@cursor/sdk")) as { Agent: typeof import("@cursor/sdk").Agent }; + const { Agent } = (await import("@cursor/sdk")) as unknown as { + Agent: { + create: (opts: { + apiKey: string; + model: { id: string }; + local: { cwd: string }; + }) => Promise; + }; + }; while (attempt < maxRetries) { attempt++; @@ -162,6 +170,7 @@ async function handleChat(req: IncomingMessage, res: ServerResponse, log: Logger agent = await Agent.create({ apiKey, model: { id: modelId }, + local: { cwd: process.cwd() }, }); messageToSend = translated.fullPromptOnMiss; log.debug("cursor-openai-proxy: pool miss", { prefixHash: translated.prefixHash.slice(0, 8) }); diff --git a/src/provider.ts b/src/provider.ts index b7c0aca..a5429df 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -309,11 +309,17 @@ async function runDoStream(opts: { return { stream }; } +type AgentCreateOpts = { + apiKey: string; + model: { id: string }; + local: { cwd: string }; +}; + /** * Single attempt to create an agent with error classification and logging. */ async function performAgentCreationAttempt(deps: { - Agent: { create: (opts: { apiKey: string; model: { id: string } }) => Promise }; + Agent: { create: (opts: AgentCreateOpts) => Promise }; apiKey: string; modelId: string; log: Logger; @@ -322,7 +328,11 @@ async function performAgentCreationAttempt(deps: { const { Agent, apiKey, modelId, log, attempt } = deps; try { log.debug("cursor-provider: calling Agent.create", { modelId, attempt }); - const agent = (await Agent.create({ apiKey, model: { id: modelId } })) as SDKAgent; + const agent = (await Agent.create({ + apiKey, + model: { id: modelId }, + local: { cwd: process.cwd() }, + })) as SDKAgent; return { agent }; } catch (err) { const decision = classifyError(err, { phase: "create" }); @@ -342,7 +352,7 @@ async function createAgentWithRetry(deps: { apiKey: string; modelId: string; log const { log } = deps; for (let attempt = 1; attempt <= 3; attempt++) { - const result = await performAgentCreationAttempt({ Agent: Agent as unknown as { create: (opts: { apiKey: string; model: { id: string } }) => Promise }, ...deps, attempt }); + const result = await performAgentCreationAttempt({ Agent: Agent as unknown as { create: (opts: AgentCreateOpts) => Promise }, ...deps, attempt }); if ("agent" in result) return result.agent; if (!result.canRetry) { diff --git a/tests/provider.test.ts b/tests/provider.test.ts index 5e45e1d..26f22a0 100644 --- a/tests/provider.test.ts +++ b/tests/provider.test.ts @@ -138,7 +138,10 @@ describe("createProviderHook.models()", () => { // models1 から生成された doStream なので ctx1 を使うべき expect(resolveApiKey).toHaveBeenCalledWith(ctx1, expect.anything()); expect(sdk.Agent.create).toHaveBeenCalledWith( - expect.objectContaining({ apiKey: "key-1" }), + expect.objectContaining({ + apiKey: "key-1", + local: { cwd: process.cwd() }, + }), ); });