feat(agent): add execution context recovery

This commit is contained in:
t8y2 committed 2026-06-22 22:15:16 +08:00
1 parent e55dbe2acc
commit 0b6e1579ca
7 files changed
+381 -60

No files matched your search

+62 -34
View File
@@ -46,6 +46,11 @@ import type { SavedSqlFile } from "@/types/database";
const STORAGE_KEY = "dbx-open-tabs";
const ACTIVE_TAB_KEY = "dbx-active-tab";
const ORACLE_LIKE_METADATA_TYPES = new Set<string>(["oracle", "dameng", "oceanbase-oracle"]);
const BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const;
function tabClientSessionId(tab: Pick<QueryTab, "id">, suffix?: (typeof BACKGROUND_CLIENT_SESSION_SUFFIXES)[number]): string {
return suffix ? `${tab.id}:${suffix}` : tab.id;
}
function resultRunCacheKey(tabId: string, runId: string): string {
return `tab:${tabId}:run:${runId}`;
@@ -169,12 +174,19 @@ export const useQueryStore = defineStore("query", () => {
}
}
async function closeClientSessionId(connectionId: string, database: string, clientSessionId: string, logContext: Record<string, unknown> = {}) {
try {
await api.closeClientConnectionSession(connectionId, database, clientSessionId);
} catch (error) {
console.warn("[DBX][client-session:close:error]", { ...logContext, clientSessionId, error });
}
}
async function closeClientConnectionSession(tab: QueryTab | undefined) {
if (!tab?.connectionId) return;
try {
await api.closeClientConnectionSession(tab.connectionId, tab.database, tab.id);
} catch (error) {
console.warn("[DBX][client-session:close:error]", { tabId: tab.id, error });
const clientSessionIds = [tabClientSessionId(tab), ...BACKGROUND_CLIENT_SESSION_SUFFIXES.map((suffix) => tabClientSessionId(tab, suffix))];
for (const clientSessionId of clientSessionIds) {
await closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id });
}
}
@@ -1198,6 +1210,7 @@ export const useQueryStore = defineStore("query", () => {
return;
}
const countSql = options.countSql;
const clientSessionId = tabClientSessionId({ id: options.tabId }, "count");
if (typeof options.pageLimit === "number" && resultRowCount < options.pageLimit) {
setQueryTotalRowCountIfCurrent(options.tabId, options.executionId, options.result, (options.pageOffset ?? 0) + resultRowCount);
@@ -1207,7 +1220,10 @@ export const useQueryStore = defineStore("query", () => {
void (async () => {
try {
console.info("[DBX][executeTabSql:count:start]", { traceId: options.traceId, elapsed: options.elapsed() });
const countResult = await api.executeQuery(options.connectionId, options.database, countSql, options.schema, undefined, { timeoutSecs: options.timeoutSecs });
const countResult = await api.executeQuery(options.connectionId, options.database, countSql, options.schema, undefined, {
clientSessionId,
timeoutSecs: options.timeoutSecs,
});
const total = Number(countResult.rows?.[0]?.[0] ?? 0);
if (!Number.isFinite(total) || total < 0) {
setQueryTotalRowCountIfCurrent(options.tabId, options.executionId, options.result, undefined);
@@ -1226,6 +1242,8 @@ export const useQueryStore = defineStore("query", () => {
elapsed: options.elapsed(),
error,
});
} finally {
void closeClientSessionId(options.connectionId, options.database, clientSessionId, { tabId: options.tabId });
}
})();
}
@@ -1567,7 +1585,7 @@ export const useQueryStore = defineStore("query", () => {
}
console.info("[DBX][executeTabSql:execute-multi:start]", { traceId, elapsed: elapsed() });
const clientSessionId = tab.mode === "query" ? tab.id : undefined;
const clientSessionId = tab.mode === "query" || tab.mode === "data" ? tabClientSessionId(tab) : undefined;
const executionOptions = {
...(typeof pageLimit === "number"
? useAgentResultSession
@@ -1770,8 +1788,10 @@ export const useQueryStore = defineStore("query", () => {
}
tab.explainSql = built.sql;
const clientSessionId = tabClientSessionId(tab, "explain");
try {
const result = await api.executeQuery(tab.connectionId, tab.database, built.sql, tab.schema, executionId, {
clientSessionId,
timeoutSecs: queryTimeoutSecs,
});
const current = tabs.value.find((t) => t.id === id);
@@ -1791,6 +1811,7 @@ export const useQueryStore = defineStore("query", () => {
current.isExplaining = false;
current.explainExecutionId = undefined;
}
void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id });
}
return { ok: true as const, sql: built.sql };
}
@@ -2011,33 +2032,39 @@ export const useQueryStore = defineStore("query", () => {
let columns: string[] = [];
let executionTimeMs = 0;
let offset = 0;
const clientSessionId = tabClientSessionId(tab, "export");
while (true) {
const sql = await api.buildTableSelectSql({
databaseType: effectiveDbType,
schema: tableMeta.schema,
tableName: tableMeta.tableName,
columns: tableMeta.columns.map((column) => column.name),
primaryKeys,
fallbackOrderColumns,
whereInput: tab.whereInput,
orderBy,
limit: pageLimit,
offset,
});
const results = await api.executeMulti(tab.connectionId, tab.database, sql, undefined, undefined, {
maxRows: pageLimit,
fetchSize: pageLimit,
timeoutSecs: queryTimeoutSecs,
});
const result = results[0];
if (!result) break;
if (columns.length === 0) columns = result.columns;
rows.push(...result.rows);
executionTimeMs += result.execution_time_ms ?? 0;
onProgress?.({ rowsExported: rows.length, totalRows });
if (result.rows.length < pageLimit) break;
offset += result.rows.length;
try {
while (true) {
const sql = await api.buildTableSelectSql({
databaseType: effectiveDbType,
schema: tableMeta.schema,
tableName: tableMeta.tableName,
columns: tableMeta.columns.map((column) => column.name),
primaryKeys,
fallbackOrderColumns,
whereInput: tab.whereInput,
orderBy,
limit: pageLimit,
offset,
});
const results = await api.executeMulti(tab.connectionId, tab.database, sql, undefined, undefined, {
maxRows: pageLimit,
fetchSize: pageLimit,
clientSessionId,
timeoutSecs: queryTimeoutSecs,
});
const result = results[0];
if (!result) break;
if (columns.length === 0) columns = result.columns;
rows.push(...result.rows);
executionTimeMs += result.execution_time_ms ?? 0;
onProgress?.({ rowsExported: rows.length, totalRows });
if (result.rows.length < pageLimit) break;
offset += result.rows.length;
}
} finally {
void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id });
}
return {
@@ -2071,7 +2098,7 @@ export const useQueryStore = defineStore("query", () => {
let executionTimeMs = 0;
let offset = 0;
let sessionId: string | undefined;
const clientSessionId = `${tab.id}:export`;
const clientSessionId = tabClientSessionId(tab, "export");
try {
while (true) {
@@ -2091,7 +2118,7 @@ export const useQueryStore = defineStore("query", () => {
clientSessionId,
timeoutSecs: queryTimeoutSecs,
}
: { maxRows: plan.pageLimit, fetchSize: plan.pageLimit, timeoutSecs: queryTimeoutSecs };
: { maxRows: plan.pageLimit, fetchSize: plan.pageLimit, clientSessionId, timeoutSecs: queryTimeoutSecs };
const results = await api.executeMulti(tab.connectionId, tab.database, plan.sqlToExecute, tab.schema, undefined, executionOptions);
const result = results[0];
if (!result) break;
@@ -2106,6 +2133,7 @@ export const useQueryStore = defineStore("query", () => {
}
} finally {
if (sessionId) void api.closeQuerySession(tab.connectionId, tab.database, sessionId, clientSessionId);
void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id });
}
return {