cline-sdk
Version:
Comprehensive SDK for Cline - AI-powered development assistant with database integration, execution modes, and custom functions
257 lines • 9.2 kB
JavaScript
"use strict";
/**
* Supabase Storage Integration for Cline SDK
* Provides cloud-based file storage as alternative to local filesystem
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupabaseStorage = void 0;
const supabase_js_1 = require("@supabase/supabase-js");
const path = __importStar(require("path"));
class SupabaseStorage {
constructor(config) {
// Use service key for admin access if provided, otherwise use anon key
const keyToUse = config.serviceKey || config.anonKey;
const options = config.serviceKey
? {
auth: {
autoRefreshToken: false,
persistSession: false,
},
}
: undefined;
this.client = (0, supabase_js_1.createClient)(config.url, keyToUse, options);
this.bucketName = config.bucketName;
this.projectPath = config.projectPath || "";
console.log(`🔑 Using ${config.serviceKey ? "service role key (admin)" : "anon key (public)"} for Supabase auth`);
}
/**
* Write a file to Supabase Storage
*/
async writeFile(filePath, content) {
try {
const fullPath = this.getFullPath(filePath);
// Convert content to buffer for Node.js compatibility
const buffer = Buffer.from(content, 'utf8');
// Upload to Supabase Storage
const { data, error } = await this.client.storage.from(this.bucketName).upload(fullPath, buffer, {
cacheControl: "3600",
upsert: true, // Overwrite if exists
contentType: "text/plain"
});
if (error) {
console.error("Supabase storage error details:", {
error,
message: error.message,
details: error.details,
hint: error.hint,
code: error.code,
});
throw error;
}
// Get public URL
const { data: urlData } = this.client.storage.from(this.bucketName).getPublicUrl(fullPath);
return {
success: true,
message: `Successfully uploaded ${filePath} to Supabase Storage`,
file_path: filePath, // Return original path, not fullPath with projectPath
size: buffer.length,
url: urlData.publicUrl,
};
}
catch (error) {
console.error("Supabase upload error:", error);
return {
success: false,
message: `Failed to upload file: ${error instanceof Error ? error.message : "Unknown error"}`,
file_path: filePath,
};
}
}
/**
* Read a file from Supabase Storage
*/
async readFile(filePath) {
try {
const fullPath = this.getFullPath(filePath);
// Download file from Supabase Storage
const { data, error } = await this.client.storage.from(this.bucketName).download(fullPath);
if (error) {
throw error;
}
if (!data) {
throw new Error("No data received");
}
// Convert blob to text
const content = await data.text();
return {
success: true,
content,
message: `Successfully downloaded ${filePath} from Supabase Storage`,
file_path: fullPath,
};
}
catch (error) {
return {
success: false,
message: `Failed to download file: ${error instanceof Error ? error.message : "Unknown error"}`,
file_path: filePath,
};
}
}
/**
* List files in a directory
*/
async listFiles(directoryPath = "") {
try {
const fullPath = this.getFullPath(directoryPath);
const { data, error } = await this.client.storage.from(this.bucketName).list(fullPath);
if (error) {
throw error;
}
const files = data?.map((file) => file.name) || [];
return {
success: true,
files,
message: `Found ${files.length} files in ${directoryPath || "root"}`,
};
}
catch (error) {
return {
success: false,
message: `Failed to list files: ${error instanceof Error ? error.message : "Unknown error"}`,
};
}
}
/**
* Delete a file from Supabase Storage
*/
async deleteFile(filePath) {
try {
const fullPath = this.getFullPath(filePath);
const { error } = await this.client.storage.from(this.bucketName).remove([fullPath]);
if (error) {
throw error;
}
return {
success: true,
message: `Successfully deleted ${filePath} from Supabase Storage`,
file_path: fullPath,
};
}
catch (error) {
return {
success: false,
message: `Failed to delete file: ${error instanceof Error ? error.message : "Unknown error"}`,
file_path: filePath,
};
}
}
/**
* Check if file exists
*/
async fileExists(filePath) {
try {
const fullPath = this.getFullPath(filePath);
const { data, error } = await this.client.storage.from(this.bucketName).list(path.posix.dirname(fullPath), {
search: path.posix.basename(fullPath),
});
if (error) {
return false;
}
return data?.some((file) => file.name === path.posix.basename(fullPath)) || false;
}
catch (error) {
return false;
}
}
/**
* Get public URL for a file
*/
getPublicUrl(filePath) {
const fullPath = this.getFullPath(filePath);
const { data } = this.client.storage.from(this.bucketName).getPublicUrl(fullPath);
return data.publicUrl;
}
/**
* Create directory structure (Supabase doesn't have directories, but we can simulate with empty files)
*/
async createDirectory(directoryPath) {
try {
// Create a placeholder file to represent the directory
const placeholderPath = path.posix.join(directoryPath, ".gitkeep");
return await this.writeFile(placeholderPath, "# Directory placeholder\n");
}
catch (error) {
return {
success: false,
message: `Failed to create directory: ${error instanceof Error ? error.message : "Unknown error"}`,
file_path: directoryPath,
};
}
}
/**
* Get full path with project prefix
*/
getFullPath(filePath) {
if (this.projectPath) {
return path.posix.join(this.projectPath, filePath);
}
return filePath;
}
/**
* Get storage info
*/
async getStorageInfo() {
try {
// Test connection by listing root
await this.client.storage.from(this.bucketName).list("", { limit: 1 });
return {
bucket: this.bucketName,
projectPath: this.projectPath,
connected: true,
};
}
catch (error) {
return {
bucket: this.bucketName,
projectPath: this.projectPath,
connected: false,
};
}
}
}
exports.SupabaseStorage = SupabaseStorage;
//# sourceMappingURL=supabase-storage.js.map