The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- The SDK must load from disk beside its Webpack chunks. |
| 2 | import * as NodeModule from "node:module"; |
| 3 | |
| 4 | // createRequire preserves adjacent Webpack chunks and native modules in bundled/SEA builds. |
| 5 | const requireCursorSdk = NodeModule.createRequire(import.meta.url); |
| 6 | let sdk: typeof import("@cursor/sdk") | undefined; |
| 7 | let sqlite: typeof import("@cursor/sdk/sqlite") | undefined; |
| 8 | |
| 9 | export function loadCursorSdk(): typeof import("@cursor/sdk") { |
| 10 | if (sdk !== undefined) return sdk; |
| 11 | const start = performance.now(); |
| 12 | sdk = requireCursorSdk("@cursor/sdk") as typeof import("@cursor/sdk"); |
| 13 | if (process.env.T3CODE_STARTUP_PROFILE_DIR) { |
| 14 | performance.measure("provider.cursor.importSdk", { start }); |
| 15 | } |
| 16 | return sdk; |
| 17 | } |
| 18 | |
| 19 | export function loadCursorSqlite(): typeof import("@cursor/sdk/sqlite") { |
| 20 | if (sqlite !== undefined) return sqlite; |
| 21 | const start = performance.now(); |
| 22 | sqlite = requireCursorSdk("@cursor/sdk/sqlite") as typeof import("@cursor/sdk/sqlite"); |
| 23 | if (process.env.T3CODE_STARTUP_PROFILE_DIR) { |
| 24 | performance.measure("provider.cursor.importSqliteStore", { start }); |
| 25 | } |
| 26 | return sqlite; |
| 27 | } |
| 28 | |
| 29 | export type CursorSdkErrorKind = "AuthenticationError" | "ConfigurationError" | "CursorSdkError"; |
| 30 | |
| 31 | const CURSOR_SDK_ERROR_KIND = Symbol.for("@t3tools/provider-cursor/sdkErrorKind"); |
| 32 | |
| 33 | export function tagCursorSdkError(error: Error, kind: CursorSdkErrorKind): Error { |
| 34 | Reflect.set(error, CURSOR_SDK_ERROR_KIND, kind); |
| 35 | return error; |
| 36 | } |
| 37 | |
| 38 | export function getCursorSdkErrorKind(cause: unknown): CursorSdkErrorKind | undefined { |
| 39 | if (typeof cause !== "object" || cause === null) return undefined; |
| 40 | const tagged = Reflect.get(cause, CURSOR_SDK_ERROR_KIND); |
| 41 | if ( |
| 42 | tagged === "AuthenticationError" || |
| 43 | tagged === "ConfigurationError" || |
| 44 | tagged === "CursorSdkError" |
| 45 | ) { |
| 46 | return tagged; |
| 47 | } |
| 48 | if (isCursorSdkError(cause, "AuthenticationError")) return "AuthenticationError"; |
| 49 | if (isCursorSdkError(cause, "ConfigurationError")) return "ConfigurationError"; |
| 50 | if (isCursorSdkError(cause, "CursorSdkError")) return "CursorSdkError"; |
| 51 | return undefined; |
| 52 | } |
| 53 | |
| 54 | // Error classification must not initialize the SDK when the load itself failed. |
| 55 | export function isCursorSdkError<K extends CursorSdkErrorKind>( |
| 56 | cause: unknown, |
| 57 | kind: K, |
| 58 | ): cause is InstanceType<(typeof import("@cursor/sdk"))[K]> { |
| 59 | if (typeof cause === "object" && cause !== null) { |
| 60 | const tagged = Reflect.get(cause, CURSOR_SDK_ERROR_KIND); |
| 61 | if ( |
| 62 | tagged === kind || |
| 63 | (kind === "CursorSdkError" && |
| 64 | (tagged === "AuthenticationError" || tagged === "ConfigurationError")) |
| 65 | ) { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | return sdk !== undefined && cause instanceof sdk[kind]; |
| 70 | } |