-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
70 lines (60 loc) · 2.42 KB
/
Copy pathindex.ts
File metadata and controls
70 lines (60 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { Plugin, ProviderHookContext, Config } from "@opencode-ai/plugin";
import { config as loadDotenv } from "dotenv";
import { resolveApiKey, cursorAuthHook } from "./auth.js";
import { createAgentPool } from "./agent-pool.js";
import { ensureCursorProviderConfig } from "./config.js";
import { createLogger } from "./logger.js";
import { startOpenAiProxy } from "./openai-proxy.js";
import { createProviderHook } from "./provider.js";
import { STATIC_FALLBACK_MODELS, makeModelMeta } from "./models.js";
const POOL_CAPACITY = 10;
/**
* OpenCode client with extended properties used by this plugin.
*/
interface ExtendedClient {
app: {
log: Parameters<typeof createLogger>[0];
cwd?: string;
};
auth?: unknown;
}
const CursorProviderPlugin: Plugin = async (input) => {
const client = input.client as ExtendedClient;
if (process.env.NODE_ENV !== "test") {
loadDotenv();
}
const log = createLogger(typeof client.app.log === "function" ? (client.app.log as Function).bind(client.app) : client.app.log);
const pool = createAgentPool({ log, capacity: POOL_CAPACITY });
const proxy = await startOpenAiProxy(log, pool);
return {
config: async (config: Config) => {
// Set CURSOR_REPO_URL from config if present
const cursorOpts = config.provider?.cursor?.options as Record<string, unknown> | undefined;
const repoUrl = cursorOpts?.repoUrl;
if (repoUrl && typeof repoUrl === "string") {
process.env.CURSOR_REPO_URL = repoUrl;
log.debug("cursor-provider: repoUrl set from config", { repoUrl });
}
const repoBranch = cursorOpts?.repoBranch;
if (repoBranch && typeof repoBranch === "string") {
process.env.CURSOR_REPO_BRANCH = repoBranch;
log.debug("cursor-provider: repoBranch set from config", { repoBranch });
}
ensureCursorProviderConfig(config, { baseURL: proxy.baseURL });
// Inject models if already configured to show them in the UI immediately
if (config.provider?.cursor) {
const models = config.provider.cursor.models || {};
const sourceModels = STATIC_FALLBACK_MODELS;
for (const m of sourceModels) {
if (!models[m.id]) {
models[m.id] = makeModelMeta(m);
}
}
config.provider.cursor.models = models;
}
},
auth: cursorAuthHook,
provider: createProviderHook({ resolveApiKey, log, pool }),
};
};
export default CursorProviderPlugin;