feat(salesforce): add Salesforce SOQL data source
This commit is contained in:
125 files changed
+9999
-117
No files matched your search
+131
@@ -0,0 +1,131 @@
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
import type { SalesforceCurrentUser } from "@/types/salesforce";
|
||||
|
||||
function installLocalStorage() {
|
||||
const data = new Map<string, string>();
|
||||
vi.stubGlobal("localStorage", {
|
||||
getItem: vi.fn((key: string) => data.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => data.set(key, value)),
|
||||
removeItem: vi.fn((key: string) => data.delete(key)),
|
||||
});
|
||||
}
|
||||
|
||||
function salesforceConnection(): ConnectionConfig {
|
||||
return {
|
||||
id: "sf-1",
|
||||
name: "Salesforce",
|
||||
db_type: "salesforce",
|
||||
host: "acme--qas1.sandbox.my.salesforce.com",
|
||||
port: 443,
|
||||
username: "ada@example.com",
|
||||
password: "",
|
||||
database: "",
|
||||
read_only: false,
|
||||
} as ConnectionConfig;
|
||||
}
|
||||
|
||||
const identity: SalesforceCurrentUser = {
|
||||
userId: "005xx000001Swag",
|
||||
name: "Ada Lovelace",
|
||||
email: "ada@example.com",
|
||||
organizationId: "00Dxx0000001gEQ",
|
||||
username: "ada@example.com.qas1",
|
||||
profileName: "System Administrator",
|
||||
isAdmin: true,
|
||||
orgName: "Acme QA Sandbox",
|
||||
};
|
||||
|
||||
async function createStore(salesforceCurrentUser: ReturnType<typeof vi.fn>) {
|
||||
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||
vi.doMock("@/lib/backend/api", () => ({
|
||||
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||
connectionDatabaseInfo: vi.fn().mockResolvedValue(null),
|
||||
connectionIdentifierQuote: vi.fn().mockResolvedValue(undefined),
|
||||
salesforceCurrentUser,
|
||||
}));
|
||||
|
||||
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||
const store = useConnectionStore();
|
||||
store.connections = [salesforceConnection()];
|
||||
// Already connected: the identity lookup must never trigger a connect of its own.
|
||||
store.connectedIds.add("sf-1");
|
||||
return store;
|
||||
}
|
||||
|
||||
describe("connectionStore salesforce identity", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.unstubAllGlobals();
|
||||
installLocalStorage();
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
it("resolves the identity once per connection and serves it from cache afterwards", async () => {
|
||||
const api = vi.fn().mockResolvedValue(identity);
|
||||
const store = await createStore(api);
|
||||
|
||||
expect(store.salesforceCurrentUser("sf-1")).toBeNull();
|
||||
await expect(store.loadSalesforceCurrentUser("sf-1")).resolves.toEqual(identity);
|
||||
await expect(store.loadSalesforceCurrentUser("sf-1")).resolves.toEqual(identity);
|
||||
|
||||
expect(store.salesforceCurrentUser("sf-1")).toEqual(identity);
|
||||
expect(api).toHaveBeenCalledTimes(1);
|
||||
expect(api).toHaveBeenCalledWith("sf-1");
|
||||
});
|
||||
|
||||
it("deduplicates concurrent lookups into one request", async () => {
|
||||
const api = vi.fn().mockResolvedValue(identity);
|
||||
const store = await createStore(api);
|
||||
|
||||
const [first, second] = await Promise.all([store.loadSalesforceCurrentUser("sf-1"), store.loadSalesforceCurrentUser("sf-1")]);
|
||||
|
||||
expect(first).toEqual(identity);
|
||||
expect(second).toEqual(identity);
|
||||
expect(api).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("hides the badge on failure without caching it, so a later lookup can recover", async () => {
|
||||
const api = vi.fn().mockRejectedValueOnce(new Error("Connection not found")).mockResolvedValueOnce(identity);
|
||||
const store = await createStore(api);
|
||||
|
||||
// The badge and the save-confirmation warning are advisory: a failed probe
|
||||
// must resolve quietly instead of throwing into the caller.
|
||||
await expect(store.loadSalesforceCurrentUser("sf-1")).resolves.toBeNull();
|
||||
expect(store.salesforceCurrentUser("sf-1")).toBeNull();
|
||||
|
||||
await expect(store.loadSalesforceCurrentUser("sf-1")).resolves.toEqual(identity);
|
||||
expect(api).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("does not cache a response that carries no identity", async () => {
|
||||
const api = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ ...identity, userId: "", username: "", name: "" })
|
||||
.mockResolvedValueOnce(identity);
|
||||
const store = await createStore(api);
|
||||
|
||||
await store.loadSalesforceCurrentUser("sf-1");
|
||||
expect(store.salesforceCurrentUser("sf-1")).toBeNull();
|
||||
|
||||
await expect(store.loadSalesforceCurrentUser("sf-1")).resolves.toEqual(identity);
|
||||
expect(api).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("keeps an identity whose admin probe did not resolve", async () => {
|
||||
const api = vi.fn().mockResolvedValue({ ...identity, isAdmin: null, profileName: undefined });
|
||||
const store = await createStore(api);
|
||||
|
||||
await expect(store.loadSalesforceCurrentUser("sf-1")).resolves.toMatchObject({ username: "ada@example.com.qas1", isAdmin: null });
|
||||
expect(store.salesforceCurrentUser("sf-1")?.profileName).toBeUndefined();
|
||||
});
|
||||
|
||||
it("resolves quietly without a connection id", async () => {
|
||||
const api = vi.fn().mockResolvedValue(identity);
|
||||
const store = await createStore(api);
|
||||
|
||||
await expect(store.loadSalesforceCurrentUser("")).resolves.toBeNull();
|
||||
expect(api).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -697,6 +697,17 @@ describe("normalizeMcpGlobalPolicy", () => {
|
||||
expect(policy.connectionPolicies[0].executionModePolicyVersion).toBeNull();
|
||||
});
|
||||
|
||||
it("defaults the Salesforce DML opt-in to off and revokes it under read-only", () => {
|
||||
// Policies saved before the switch existed carry no field at all.
|
||||
expect(normalizeMcpGlobalPolicy({ connectionPolicies: [{ connectionId: "sfdc", databaseScope: "all" } as any] }).connectionPolicies[0].allowSalesforceDml).toBe(false);
|
||||
|
||||
expect(normalizeMcpGlobalPolicy({ connectionPolicies: [{ connectionId: "sfdc", allowSalesforceDml: true } as any] }).connectionPolicies[0].allowSalesforceDml).toBe(true);
|
||||
|
||||
expect(normalizeMcpGlobalPolicy({ connectionPolicies: [{ connectionId: "sfdc", readOnly: true, allowSalesforceDml: true } as any] }).connectionPolicies[0].allowSalesforceDml).toBe(false);
|
||||
|
||||
expect(normalizeMcpGlobalPolicy({ connectionPolicies: [{ connectionId: "sfdc", allowSalesforceDml: "yes" } as any] }).connectionPolicies[0].allowSalesforceDml).toBe(false);
|
||||
});
|
||||
|
||||
it("round-trips queryTimeoutSecs null, undefined and positive numbers", () => {
|
||||
expect(normalizeMcpGlobalPolicy({ queryTimeoutSecs: null }).queryTimeoutSecs).toBeNull();
|
||||
expect(normalizeMcpGlobalPolicy({ queryTimeoutSecs: undefined } as any).queryTimeoutSecs).toBeNull();
|
||||
|
||||
@@ -165,6 +165,8 @@ import { useSavedSqlStore } from "@/stores/savedSqlStore";
|
||||
import { decorateDatabaseSavedSqlTreeNodes, indexSavedSqlFilesByDatabase, stripDatabaseSavedSqlTreeNodes, withDatabaseSavedSqlRoot } from "@/lib/savedSql/savedSqlDatabaseTree";
|
||||
import { encodeSqlServerLinkedSchema, parseSqlServerLinkedSchema } from "@/lib/database/sqlServerLinkedServers";
|
||||
import { inferMongoCompletionFields, type MongoCompletionField } from "@/lib/mongo/mongoCompletion";
|
||||
import type { SoqlCompletionField, SoqlCompletionObject } from "@/lib/soql/soqlCompletion";
|
||||
import type { SalesforceCurrentUser } from "@/types/salesforce";
|
||||
import { flattenElasticsearchMappingFields, type ElasticsearchCompletionField } from "@/lib/elasticsearch/elasticsearchCompletion";
|
||||
import { isMongoLegacyDriverProfile } from "@/lib/mongo/mongoCapabilities";
|
||||
import { mongoCollectionKindFromNode, toMongoCollectionKind, visibleMongoCollections } from "@/lib/sidebar/mongoCollectionMutation";
|
||||
@@ -532,6 +534,11 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
const redisCommandDocsCacheGeneration = new Map<string, number>();
|
||||
const mongoCompletionCollectionsCache = ref<Record<string, string[]>>({});
|
||||
const mongoCompletionFieldsCache = ref<Record<string, MongoCompletionField[]>>({});
|
||||
const soqlCompletionObjectsCache = ref<Record<string, SoqlCompletionObject[]>>({});
|
||||
const soqlCompletionFieldsCache = ref<Record<string, SoqlCompletionField[]>>({});
|
||||
// One entry per connection: the authenticated Salesforce user never changes for
|
||||
// the life of a connection (token refreshes reuse the same identity).
|
||||
const salesforceCurrentUserCache = ref<Record<string, SalesforceCurrentUser>>({});
|
||||
const schemaListCache = ref<Record<string, string[]>>({});
|
||||
const sidebarSearchQuery = ref("");
|
||||
const sidebarTableSearchQueries = ref<Record<string, string>>({});
|
||||
@@ -1666,6 +1673,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
oscar: "神通 OSCAR",
|
||||
influxdb: "InfluxDB",
|
||||
victoriametrics: "VictoriaMetrics",
|
||||
salesforce: "Salesforce",
|
||||
};
|
||||
|
||||
const profile = config.driver_profile || config.db_type;
|
||||
@@ -3805,6 +3813,12 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
for (const key of Object.keys(mongoCompletionFieldsCache.value)) {
|
||||
if (key === exactCacheKey || key.startsWith(cachePrefix)) delete mongoCompletionFieldsCache.value[key];
|
||||
}
|
||||
for (const key of Object.keys(soqlCompletionObjectsCache.value)) {
|
||||
if (key === exactCacheKey || key.startsWith(cachePrefix)) delete soqlCompletionObjectsCache.value[key];
|
||||
}
|
||||
for (const key of Object.keys(soqlCompletionFieldsCache.value)) {
|
||||
if (key === exactCacheKey || key.startsWith(cachePrefix)) delete soqlCompletionFieldsCache.value[key];
|
||||
}
|
||||
for (const key of completionTableIndex.keys()) {
|
||||
if (key.startsWith(cachePrefix)) completionTableIndex.delete(key);
|
||||
}
|
||||
@@ -5182,7 +5196,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
}
|
||||
if (!connectedIds.value.has(connectionId)) return;
|
||||
const config = getConfig(connectionId);
|
||||
if (!config || ["redis", "etcd", "zookeeper", "consul", "mongodb", "dynamodb", "elasticsearch", "easysearch", "meilisearch", "solr", "milvus", "qdrant", "weaviate", "chromadb", "mq", "nacos"].includes(config.db_type)) return;
|
||||
if (!config || ["redis", "etcd", "zookeeper", "consul", "mongodb", "dynamodb", "elasticsearch", "easysearch", "meilisearch", "solr", "milvus", "qdrant", "weaviate", "chromadb", "mq", "nacos", "salesforce"].includes(config.db_type)) return;
|
||||
const node = findConnectionNode(connectionId);
|
||||
if (!node || node.type !== "connection" || hasConnectionMetadataChildren(node.children)) return;
|
||||
const scope = { kind: "connection-databases" as const, connectionId, driverProfile: metadataDriverProfile(config) };
|
||||
@@ -8511,6 +8525,105 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
});
|
||||
}
|
||||
|
||||
// Map a Salesforce describe column to a SOQL completion field. The backend packs
|
||||
// relationshipName / referenceTo / label into ColumnInfo.extra (JSON) and active
|
||||
// picklist values into enum_values; parse defensively since extra may be absent.
|
||||
function soqlFieldFromColumnInfo(column: ColumnInfo): SoqlCompletionField {
|
||||
let extra: { relationshipName?: string; referenceTo?: string[]; label?: string } | null = null;
|
||||
if (column.extra) {
|
||||
try {
|
||||
extra = JSON.parse(column.extra);
|
||||
} catch {
|
||||
extra = null;
|
||||
}
|
||||
}
|
||||
return {
|
||||
name: column.name,
|
||||
type: column.data_type || undefined,
|
||||
label: extra?.label ?? column.comment ?? undefined,
|
||||
picklistValues: column.enum_values?.length ? column.enum_values : undefined,
|
||||
relationshipName: extra?.relationshipName ?? undefined,
|
||||
referenceTo: extra?.referenceTo?.length ? extra.referenceTo : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
async function listSoqlCompletionObjects(connectionId: string, database: string): Promise<SoqlCompletionObject[]> {
|
||||
// No `database` guard: the Salesforce backend ignores the database parameter
|
||||
// (the whole org is one synthesized database), and a restored query tab can
|
||||
// legitimately carry an empty database while the connection still works.
|
||||
const cacheKey = `${connectionId}:${database}`;
|
||||
const cached = soqlCompletionObjectsCache.value[cacheKey];
|
||||
if (cached) return cached;
|
||||
return withCompletionInFlight(`${cacheKey}:soql-objects`, async () => {
|
||||
await ensureConnected(connectionId);
|
||||
const tables = await api.listTables(connectionId, database, "");
|
||||
const labelByName = new Map(tables.map((t) => [t.name, t.comment ?? undefined]));
|
||||
const objects = sortSidebarNames(tables.map((t) => t.name)).map((name) => ({ name, label: labelByName.get(name) }));
|
||||
// Never cache an empty list: a transient failure would otherwise stick for the
|
||||
// whole session and silently degrade SOQL completion to keywords-only.
|
||||
if (objects.length > 0) {
|
||||
soqlCompletionObjectsCache.value[cacheKey] = objects;
|
||||
evictOldestCacheEntries(soqlCompletionObjectsCache.value, COMPLETION_CACHE_MAX);
|
||||
}
|
||||
return objects;
|
||||
});
|
||||
}
|
||||
|
||||
async function listSoqlCompletionFields(connectionId: string, database: string, objectName: string): Promise<SoqlCompletionField[]> {
|
||||
if (!objectName) return [];
|
||||
const cacheKey = `${connectionId}:${database}:${objectName}`;
|
||||
const cached = soqlCompletionFieldsCache.value[cacheKey];
|
||||
if (cached) return cached;
|
||||
return withCompletionInFlight(`${cacheKey}:soql-fields`, async () => {
|
||||
await ensureConnected(connectionId);
|
||||
// Backed by the driver's in-memory describe cache, so repeated loads (e.g. one
|
||||
// per keystroke while traversing a relationship) do not re-hit the Salesforce API.
|
||||
const columns = await api.getColumns(connectionId, database, "", objectName);
|
||||
const fields = columns.map(soqlFieldFromColumnInfo);
|
||||
// Never cache an empty list: a transient describe failure would otherwise stick
|
||||
// and make field completion silently vanish for this object all session.
|
||||
if (fields.length > 0) {
|
||||
soqlCompletionFieldsCache.value[cacheKey] = fields;
|
||||
evictOldestCacheEntries(soqlCompletionFieldsCache.value, COMPLETION_CACHE_MAX);
|
||||
}
|
||||
return fields;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Identity behind a Salesforce connection (`GET /services/oauth2/userinfo` plus an
|
||||
* admin probe), resolved at most once per connection and cached for the session.
|
||||
* Deliberately failure-tolerant: the toolbar badge and the non-admin hint on the
|
||||
* DML confirmation are advisory, so a failed lookup returns null instead of
|
||||
* surfacing an error or blocking an edit.
|
||||
*/
|
||||
async function loadSalesforceCurrentUser(connectionId: string): Promise<SalesforceCurrentUser | null> {
|
||||
if (!connectionId) return null;
|
||||
const cached = salesforceCurrentUserCache.value[connectionId];
|
||||
if (cached) return cached;
|
||||
try {
|
||||
return await withCompletionInFlight(`${connectionId}:salesforce-current-user`, async () => {
|
||||
await ensureConnected(connectionId);
|
||||
const user = await api.salesforceCurrentUser(connectionId);
|
||||
// Never cache an identity-less result: a partial failure would otherwise
|
||||
// stick for the session and keep the badge blank.
|
||||
if (user && (user.userId || user.username)) {
|
||||
salesforceCurrentUserCache.value[connectionId] = user;
|
||||
evictOldestCacheEntries(salesforceCurrentUserCache.value, COMPLETION_CACHE_MAX);
|
||||
}
|
||||
return user ?? null;
|
||||
});
|
||||
} catch (error) {
|
||||
console.debug("[salesforce] current user lookup failed", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Last known Salesforce identity for a connection, or null before it resolves. */
|
||||
function salesforceCurrentUser(connectionId: string): SalesforceCurrentUser | null {
|
||||
return salesforceCurrentUserCache.value[connectionId] ?? null;
|
||||
}
|
||||
|
||||
function listCompletionTableMetadata(connectionId: string, database: string, schema: string, filter?: string, limit?: number, catalog?: string): Promise<TableInfo[]> {
|
||||
if (catalog) return api.listTables(connectionId, database, schema, filter, limit, undefined, undefined, catalog);
|
||||
return api.listTables(connectionId, database, schema, filter, limit);
|
||||
@@ -9946,6 +10059,10 @@ export const useConnectionStore = defineStore("connection", () => {
|
||||
listRedisCompletionCommandDocs,
|
||||
listMongoCompletionCollections,
|
||||
listMongoCompletionFields,
|
||||
listSoqlCompletionObjects,
|
||||
listSoqlCompletionFields,
|
||||
loadSalesforceCurrentUser,
|
||||
salesforceCurrentUser,
|
||||
invalidateCompletionCache,
|
||||
invalidateCompletionTableCache,
|
||||
completionCacheRevision,
|
||||
|
||||
@@ -86,6 +86,13 @@ export interface McpConnectionPolicy {
|
||||
databaseScope: "all" | "selected" | "none";
|
||||
allowedDatabases: string[];
|
||||
databasePolicies: McpDatabasePolicy[];
|
||||
/**
|
||||
* Per-connection opt-in for AI-agent writes to a Salesforce org. Off by default and
|
||||
* forced off by `readOnly`: SOQL reads need nothing beyond the execution mode, but
|
||||
* Salesforce DML has no transaction and no rollback, so it also requires the
|
||||
* two-step prepare/apply confirmation on every single write.
|
||||
*/
|
||||
allowSalesforceDml: boolean;
|
||||
}
|
||||
|
||||
export interface McpDatabasePolicy {
|
||||
@@ -174,6 +181,9 @@ export function normalizeMcpGlobalPolicy(policy: Partial<McpGlobalPolicy> | null
|
||||
databaseScope,
|
||||
allowedDatabases,
|
||||
databasePolicies,
|
||||
// Same fail-closed shape as allowDangerousSql: read-only wins, and a policy
|
||||
// saved before the switch existed (field absent) normalizes to off.
|
||||
allowSalesforceDml: rule.readOnly !== true && rule.allowSalesforceDml === true,
|
||||
};
|
||||
return rules;
|
||||
}, {}),
|
||||
|
||||
Reference in new issue
Block a user