Open History to see the last commit
1import type { SDKModel, SDKUser } from "@cursor/sdk";
2import * as Cache from "effect/Cache";
3import * as Context from "effect/Context";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as Exit from "effect/Exit";
7import * as Layer from "effect/Layer";
8import * as Schema from "effect/Schema";
9
10import { AuthenticationError, Cursor, CursorSdkError } from "./sdk.ts";
11
12export interface CursorSdkCatalogSnapshot {
13 readonly user: SDKUser;
14 readonly models: ReadonlyArray<SDKModel>;
15}
16
17export class CursorSdkCatalogError extends Schema.TaggedError<CursorSdkCatalogError>()(
18 "CursorSdkCatalogError",
19 {
20 authenticationFailure: Schema.Boolean,
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return this.authenticationFailure
26 ? "Cursor SDK authentication failed."
27 : "Cursor SDK catalog request failed.";
28 }
29
30 get planRequired(): boolean {
31 return this.cause instanceof CursorSdkError && this.cause.code === "plan_required";
32 }
33}
34
35export class CursorSdkCatalog extends Context.Service<
36 CursorSdkCatalog,
37 {
38 readonly read: (
39 apiKey: string,
40 ) => Effect.Effect<CursorSdkCatalogSnapshot, CursorSdkCatalogError>;
41 }
42>()("@t3tools/provider-cursor/server/CursorSdkCatalog") {}
43
44function isAuthenticationFailure(cause: unknown): boolean {
45 return (
46 cause instanceof AuthenticationError ||
47 (cause instanceof CursorSdkError && cause.status === 401)
48 );
49}
50
51export interface CursorSdkCatalogProbes {
52 readonly readUser: (apiKey: string) => Effect.Effect<SDKUser, CursorSdkCatalogError>;
53 readonly readModels: (
54 apiKey: string,
55 ) => Effect.Effect<ReadonlyArray<SDKModel>, CursorSdkCatalogError>;
56}
57
58const sdkRequest = <A>(tryRequest: () => Promise<A>) =>
59 Effect.tryPromise({
60 try: tryRequest,
61 catch: (cause) =>
62 new CursorSdkCatalogError({
63 authenticationFailure: isAuthenticationFailure(cause),
64 cause,
65 }),
66 });
67
68const liveProbes: CursorSdkCatalogProbes = {
69 readUser: (apiKey) => sdkRequest(() => Cursor.me({ apiKey })),
70 readModels: (apiKey) => sdkRequest(() => Cursor.models.list({ apiKey })),
71};
72
73export const makeCursorSdkCatalog = Effect.fn("CursorSdkCatalog.make")(function* (
74 probes: CursorSdkCatalogProbes = liveProbes,
75) {
76 const modelCache = yield* Cache.makeWith(probes.readModels, {
77 capacity: 32,
78 timeToLive: (exit) =>
79 Exit.isSuccess(exit) && exit.value.length > 0 ? Duration.minutes(30) : Duration.zero,
80 });
81 return CursorSdkCatalog.of({
82 read: (apiKey) =>
83 Effect.all(
84 {
85 user: probes.readUser(apiKey),
86 models: Cache.get(modelCache, apiKey),
87 },
88 { concurrency: "unbounded" },
89 ),
90 });
91});
92
93export const layer = Layer.effect(CursorSdkCatalog, makeCursorSdkCatalog());
94
95export function layerTest(
96 read: CursorSdkCatalog["Service"]["read"],
97): Layer.Layer<CursorSdkCatalog> {
98 return Layer.succeed(CursorSdkCatalog, CursorSdkCatalog.of({ read }));
99}