feat(salesforce): add Salesforce SOQL data source

This commit is contained in:
邹超 authored and GitHub committed 2026-09-24 18:25:23 +08:00
1 parent bf3e356540
commit f87b5d1568
125 files changed
+9999 -117

No files matched your search

@@ -3471,6 +3471,7 @@ async function saveMcpPolicy(
databaseScope: "all" | "selected" | "none";
allowedDatabases: string[];
databasePolicies: { databaseName: string; readOnly: boolean; allowDangerousSql: boolean }[];
allowSalesforceDml: boolean;
}[];
groupPolicies?: McpGroupPolicy[];
queryTimeoutSecs?: number | null;
@@ -3659,6 +3660,14 @@ function onMcpGroupExecutionModeChange(groupId: string, mode: McpConnectionExecu
void saveMcpPolicy({ groupPolicies });
}
// A connection rule is only worth persisting when it actually changes something: an
// explicit execution mode, a narrowed database scope, a per-database override, or the
// Salesforce DML opt-in. Rules that merely restate the inherited defaults are dropped so
// the stored policy stays readable and keeps following later global changes.
function mcpConnectionPolicyIsMeaningful(rule: McpConnectionPolicy): boolean {
return rule.executionModeConfigured || rule.databaseScope !== "all" || rule.databasePolicies.length > 0 || rule.allowSalesforceDml;
}
function onMcpConnectionExecutionModeChange(connectionId: string, mode: McpConnectionExecutionMode | "inherit") {
if (mode === "high_risk_write" && !window.confirm(t("settings.mcpExecutionModeHighRiskConfirm"))) return;
const existing = settingsStore.mcpGlobalPolicy.connectionPolicies.find((item) => item.connectionId === connectionId);
@@ -3682,8 +3691,38 @@ function onMcpConnectionExecutionModeChange(connectionId: string, mode: McpConne
databaseScope: existing?.databaseScope ?? ("all" as const),
allowedDatabases: existing?.allowedDatabases ?? [],
databasePolicies: selectedMode.databasePolicies,
// Switching a connection to read-only silently revokes the DML opt-in, matching
// the backend's ceiling: an agent must never keep a write path it lost.
allowSalesforceDml: (existing?.allowSalesforceDml ?? false) && !selectedMode.readOnly,
};
if (next.executionModeConfigured || next.databaseScope !== "all" || next.databasePolicies.length > 0) rules.push(next);
if (mcpConnectionPolicyIsMeaningful(next)) rules.push(next);
void saveMcpPolicy({ connectionPolicies: rules });
}
function onMcpConnectionSalesforceDmlChange(connectionId: string, allowed: boolean) {
const existing = settingsStore.mcpGlobalPolicy.connectionPolicies.find((item) => item.connectionId === connectionId);
if (allowed) {
// Read-only is a hard ceiling on the server too, so letting the box stay checked
// here would only advertise a write path that every prepare call then refuses.
const effectiveMode = mcpEffectiveExecutionMode(mcpInheritedGroupExecutionMode(connectionId), mcpConnectionExecutionMode(connectionId), "inherit");
if (effectiveMode === "read_only") {
toast(t("settings.mcpConnectionPolicyAllowSalesforceDmlReadOnlyBlocked"), 5000);
return;
}
}
const rules = settingsStore.mcpGlobalPolicy.connectionPolicies.filter((item) => item.connectionId !== connectionId);
const next: McpConnectionPolicy = {
connectionId,
readOnly: existing?.readOnly ?? false,
allowDangerousSql: existing?.allowDangerousSql ?? false,
executionModeConfigured: existing?.executionModeConfigured ?? false,
executionModePolicyVersion: existing?.executionModePolicyVersion ?? null,
databaseScope: existing?.databaseScope ?? "all",
allowedDatabases: existing?.allowedDatabases ?? [],
databasePolicies: existing?.databasePolicies ?? [],
allowSalesforceDml: allowed,
};
if (mcpConnectionPolicyIsMeaningful(next)) rules.push(next);
void saveMcpPolicy({ connectionPolicies: rules });
}
@@ -3703,7 +3742,7 @@ function onMcpDatabaseExecutionModeChange(connectionId: string, databaseName: st
databasePolicies,
};
const rules = settingsStore.mcpGlobalPolicy.connectionPolicies.filter((item) => item.connectionId !== connectionId);
if (next.executionModeConfigured || next.databaseScope !== "all" || next.databasePolicies.length > 0) rules.push(next);
if (mcpConnectionPolicyIsMeaningful(next)) rules.push(next);
void saveMcpPolicy({ connectionPolicies: rules });
}
@@ -9938,6 +9977,7 @@ LIMIT 100;</pre
@update:scope="onMcpResourceScopeChange"
@set:group-policy="onMcpGroupExecutionModeChange"
@set:connection-policy="onMcpConnectionExecutionModeChange"
@set:connection-salesforce-dml="onMcpConnectionSalesforceDmlChange"
/>
</div>
</template>
@@ -139,7 +139,9 @@ import type { SqlCompletionColumn, SqlCompletionContext, SqlCompletionReferenced
const props = defineProps<QueryEditorProps>();
function sqlBehaviorDialect(): "mysql" | "postgres" | "sqlserver" | undefined {
return props.syntaxDialect === "clickhouse" ? props.dialect : (props.syntaxDialect ?? props.dialect);
// clickhouse and soql ride the SQL editor but have no matching behavior dialect;
// fall back to the connection's dialect (undefined for Salesforce).
return props.syntaxDialect === "clickhouse" || props.syntaxDialect === "soql" ? props.dialect : (props.syntaxDialect ?? props.dialect);
}
function queryEditorSelectionLanguage(): "sql" | "text" {
@@ -9,9 +9,10 @@ import { type ElasticsearchCompletionItem } from "@/lib/elasticsearch/elasticsea
import { type MongoCompletionItem } from "@/lib/mongo/mongoCompletion";
import { batchColumnSelectionColumnList, batchColumnSelectionInsertReplacement, batchColumnSelectionReplaceTo, isBatchColumnSelectionCompletionActive } from "@/lib/editor/batchColumnSelection";
import { type RedisCompletionItem } from "@/lib/redis/redisCompletion";
import { type SoqlCompletionItem } from "@/lib/soql/soqlCompletion";
import type { SqlCompletionItem } from "@/lib/sql/sqlCompletion";
export type QueryCompletionItem = SqlCompletionItem | ElasticsearchCompletionItem | RedisCompletionItem | MongoCompletionItem;
export type QueryCompletionItem = SqlCompletionItem | ElasticsearchCompletionItem | RedisCompletionItem | MongoCompletionItem | SoqlCompletionItem;
export interface BatchColumnSelectionActionItem {
label: string;
@@ -25,6 +25,7 @@ import { sqlCompletionContextFromSemantic } from "@/lib/sql/semantic/completion"
import { buildSqlSemanticModel } from "@/lib/sql/semantic/model";
import { buildElasticsearchCompletionItemsFromContext, elasticsearchCompletionNeedsFields, getElasticsearchCompletionContext, getElasticsearchCompletionResultValidFor, shouldAutoOpenElasticsearchCompletion, type ElasticsearchCompletionField } from "@/lib/elasticsearch/elasticsearchCompletion";
import { buildMongoCompletionItemsFromContext, getMongoCompletionContext, getMongoCompletionResultValidFor, mongoCompletionNeedsCollections, mongoCompletionNeedsFields, shouldAutoOpenMongoCompletion } from "@/lib/mongo/mongoCompletion";
import { buildSoqlCompletionItems, getSoqlCompletionContext, getSoqlCompletionResultValidFor, resolveSoqlFieldCandidates, resolveSoqlValueField, shouldAutoOpenSoqlCompletion, soqlCompletionNeedsObjects, type SoqlCompletionField, type SoqlCompletionObject } from "@/lib/soql/soqlCompletion";
import {
buildSqlServerUseDatabaseCompletionItems,
mergeSqlCompletionQualifierNames,
@@ -361,7 +362,16 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
}
function shouldInsertSqlCompletionSpace(): boolean {
return props.databaseType !== "mongodb" && props.databaseType !== "redis" && props.databaseType !== "elasticsearch" && props.databaseType !== "easysearch" && props.databaseType !== "meilisearch" && props.databaseType !== "solr" && props.databaseType !== "victoriametrics";
return (
props.databaseType !== "mongodb" &&
props.databaseType !== "redis" &&
props.databaseType !== "elasticsearch" &&
props.databaseType !== "easysearch" &&
props.databaseType !== "meilisearch" &&
props.databaseType !== "solr" &&
props.databaseType !== "victoriametrics" &&
props.databaseType !== "salesforce"
);
}
// Snippet expansion normally follows from the item type; a provider can also
@@ -591,6 +601,59 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
};
}
async function provideSoqlCompletions(currentState: import("@codemirror/state").EditorState, position: number, explicit: boolean) {
if (!props.connectionId) return null;
const epoch = ++completionEpoch;
const fullDoc = currentState.doc.toString();
if (!explicit && !shouldAutoOpenSoqlCompletion(fullDoc, position)) return null;
const completionContext = getSoqlCompletionContext(fullDoc, position);
if (completionContext.mode === "none") return null;
// The Salesforce backend ignores the database parameter for metadata (the whole
// org is one synthesized database), so completion must not gate on props.database
// being populated — a restored/toolbar-opened tab can carry an empty database
// while the connection works fine. Pass it through (possibly "") and let the
// store/backend resolve it.
const database = props.database ?? "";
let objects: SoqlCompletionObject[] = [];
let fields: SoqlCompletionField[] = [];
let valueField: SoqlCompletionField | null = null;
// Field loader backed by the store (which reads the backend describe cache), so
// relationship traversal costs at most one describe per distinct sObject.
const loadFields = (objectName: string) => connectionStore.listSoqlCompletionFields(props.connectionId!, database, objectName);
try {
if (soqlCompletionNeedsObjects(completionContext.mode)) {
objects = await connectionStore.listSoqlCompletionObjects(props.connectionId, database);
}
if (completionContext.mode === "field") {
fields = await resolveSoqlFieldCandidates(completionContext, loadFields);
}
if (completionContext.mode === "value") {
valueField = await resolveSoqlValueField(completionContext, loadFields);
}
} catch (error) {
// Metadata load failed (offline, describe error, no cache yet): fall back to
// keyword/function-only completion. Logged so a persistent empty field list is
// diagnosable instead of silently looking like "custom fields are excluded".
console.debug("[soql-completion] metadata load failed", error);
objects = [];
fields = [];
valueField = null;
}
if (epoch !== completionEpoch) return null;
const items = buildSoqlCompletionItems(completionContext, { objects, fields, valueField });
if (items.length === 0) return null;
return {
from: completionContext.from,
options: items.map((item) => completionOptionForItem(item)),
validFor: getSoqlCompletionResultValidFor(completionContext),
};
}
async function provideSqlCompletions(context: CompletionContext) {
const currentState = context.state;
const position = context.pos;
@@ -612,6 +675,9 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
return provideRedisCompletions(currentState, position, explicit);
}
if (props.databaseType === "victoriametrics") return null;
if (props.databaseType === "salesforce") {
return provideSoqlCompletions(currentState, position, explicit);
}
const hasDatabase = props.database != null;
const sequenceLiteralContext = getPostgresSequenceLiteralCompletionContext(fullDoc, position, props.databaseType);
const databaseLinkContext = oracleDatabaseLinkCompletionContext(fullDoc, position, props.databaseType);
@@ -195,7 +195,7 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
}
function shouldSkipSqlSemanticDiagnostics() {
return props.databaseType === "victoriametrics" || (props.databaseType !== "redis" && props.databaseType !== "mongodb" && !settingsStore.editorSettings.sqlSemanticDiagnosticsEnabled);
return props.databaseType === "victoriametrics" || props.databaseType === "salesforce" || (props.databaseType !== "redis" && props.databaseType !== "mongodb" && !settingsStore.editorSettings.sqlSemanticDiagnosticsEnabled);
}
function rangesOverlap(left: { from: number; to: number }, right: { from: number; to: number }): boolean {
@@ -337,7 +337,7 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
setSemanticDiagnostics([]);
return;
}
if (props.databaseType === "elasticsearch" || props.databaseType === "easysearch" || props.databaseType === "meilisearch" || props.databaseType === "solr" || props.databaseType === "victoriametrics") {
if (props.databaseType === "elasticsearch" || props.databaseType === "easysearch" || props.databaseType === "meilisearch" || props.databaseType === "solr" || props.databaseType === "victoriametrics" || props.databaseType === "salesforce") {
setSemanticDiagnostics([]);
return;
}