Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { startOpenAiProxy } from "./openai-proxy.js";
import { createProviderHook } from "./provider.js";
import { STATIC_FALLBACK_MODELS, makeModelMeta } from "./models.js";

const POOL_CAPACITY = 8;
const POOL_CAPACITY = 10;

/**
* OpenCode client with extended properties used by this plugin.
Expand Down
11 changes: 10 additions & 1 deletion src/openai-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
};
};
Comment thread
yohi marked this conversation as resolved.
Comment thread
yohi marked this conversation as resolved.

while (attempt < maxRetries) {
attempt++;
Expand All @@ -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) });
Expand Down
16 changes: 13 additions & 3 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> };
Agent: { create: (opts: AgentCreateOpts) => Promise<unknown> };
apiKey: string;
modelId: string;
log: Logger;
Expand All @@ -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" });
Expand All @@ -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<unknown> }, ...deps, attempt });
const result = await performAgentCreationAttempt({ Agent: Agent as unknown as { create: (opts: AgentCreateOpts) => Promise<unknown> }, ...deps, attempt });

if ("agent" in result) return result.agent;
if (!result.canRetry) {
Expand Down
5 changes: 4 additions & 1 deletion tests/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
}),
);
});

Expand Down
Loading