Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(frontend): 通用选择器创建临时数据源前先校验可创建 (#4050)
linus-liu-webaefe6d22e96d
diff --git a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.tempDataSource.test.ts b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.tempDataSource.test.tsnew file mode 100644index 000000000..6adc6abef--- /dev/null+++ b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.tempDataSource.test.ts@@ -0,0 +1,603 @@+import assert from 'node:assert/strict'+import { readFileSync } from 'node:fs'+import { createRequire } from 'node:module'+import { fileURLToPath } from 'node:url'+import { runInNewContext } from 'node:vm'+import test from 'node:test'+import { compileScript, parse } from '@vue/compiler-sfc'+import ts from 'typescript'+import { createRenderer, h, nextTick, reactive, ref } from 'vue'++// The draft data source the resource picker lists through. A picker cannot call+// the resource endpoint before a data source row exists, so the dialog creates a+// paused one on first use — but only when the create endpoint could accept it:+// a request without a knowledge base (ErrKnowledgeBaseNotFound) or without a+// registered connector type (ErrConnectorNotFound) is rejected before the+// connector is reached, so it is never sent, never reported, and only logged.+// These tests pin that contract for the generic picker and for the Feishu/Lark+// Drive root loader, which obtains the same draft the same way (and additionally+// has to flush the folder token before listing): the four scenarios (normal+// load, missing parameters, retry after restoring them, error report), the four+// things a shared draft has to get right (one creation for concurrent callers,+// an answer without an id treated as a failure, a failed attempt the next one+// can retry, and an in-flight create abandoned by closing or reopening the+// drawer), and the diagnostic a skipped create leaves behind.+const require = createRequire(import.meta.url)+const filename = fileURLToPath(new URL('./DataSourceEditorDialog.vue', import.meta.url))+const { descriptor } = parse(readFileSync(filename, 'utf8'), { filename })+const script = compileScript(descriptor, { id: 'datasource-editor-tempds-test' }).content+ .replace('__expose();', '')+ .replace('return __returned__', '__expose(__returned__); return __returned__')+const compiled = ts.transpileModule(script, {+ compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },+}).outputText++// A lazy tree of two roots, which is all the picker needs to show that a+// listing went through.+const TREE_RESOURCES = [+ { external_id: 'page-1', name: 'Handbook', type: 'page', has_children: false },+ { external_id: 'page-2', name: 'Policies', type: 'page', has_children: false },+]++// A Drive root listing: the folder token is the root, and the single row below+// it is what makes loadDriveRoot reveal the tree.+const DRIVE_RESOURCES = [+ {+ external_id: 'folderTokenSynthetic:fileA', name: 'A.docx', type: 'file',+ parent_id: 'folderTokenSynthetic', has_children: false,+ },+]++async function fixture({+ resources = [] as any[],+ createResult = { id: 'temp-one' } as any,+ createFails = false,+ listFails = null as Error | null,+ // How many leading creates to hold open, so a test can act while the request+ // is still in flight (closing or reopening the drawer) and answer it later.+ deferCreates = 0,+} = {}) {+ const calls: Array<{ method: string; args: any[] }> = []+ // Resource listings are counted apart from the data source writes: the tests+ // assert which id a listing went through (and that a skipped load sent none).+ const resourceCalls: string[] = []+ // MessagePlugin.error texts, so "reported nothing" and "reported this" are+ // both assertions rather than assumptions.+ const messages: string[] = []+ // deleteDataSource ids, so an abandoned draft is shown to be cleaned up+ // rather than assumed to be.+ const deletes: string[] = []+ // console.warn lines: what a "I pressed load and nothing happened" report has+ // to go on when the form cannot produce a data source at all.+ const warnings: string[] = []+ // Resolvers for the creates held open by deferCreates.+ const held: Array<() => void> = []+ let createCount = 0+ // Mutable per test: a case flips the create result or the failure on to show+ // what the next attempt does with it.+ const state = { createResult, createFails, listFails }+ const api = {+ async createDataSource(data: any) {+ calls.push({ method: 'createDataSource', args: [JSON.parse(JSON.stringify(data))] })+ // The answer is fixed when the request is sent, like a real create: a+ // later test step may change what the *next* create answers with without+ // changing this one.+ const result = state.createResult+ if (createCount++ < deferCreates) {+ await new Promise<void>(resolve => held.push(resolve))+ }+ if (state.createFails) throw new Error('create failed')+ return result+ },+ async updateDataSource(id: string, data: any) {+ calls.push({ method: 'updateDataSource', args: [id, JSON.parse(JSON.stringify(data))] })+ },+ async listResources(id: string, parentId?: string) {+ resourceCalls.push(parentId ?? id)+ if (state.listFails) throw state.listFails+ return { data: resources }+ },+ async resolveResourceAncestors() { return { data: { ancestors: [] } } },+ async deleteDataSource(id: string) { deletes.push(id) },+ async triggerSync() {},+ async validateConnection() {},+ async validateCredentials() {},+ async putDataSourceCredentials() {},+ async deleteDataSourceCredentials() {},+ }+ const props = reactive<{ visible: boolean; kbId: string; dataSource: any }>({+ visible: false, kbId: 'kb-one',+ // Create mode: no data source row exists yet, so the picker has to create+ // the draft it lists through. Edit-mode cases swap a row in and reopen.+ dataSource: null,+ })+ // The component's only console use is the diagnostic for a skipped create;+ // inherit the real console so anything else would still behave, and capture+ // the warning so it can be asserted on.+ const vmConsole: any = Object.create(console)+ vmConsole.warn = (...args: any[]) => { warnings.push(args.map(String).join(' ')) }+ const exports: any = {}+ runInNewContext(compiled, {+ exports,+ require(name: string) {+ if (name === 'vue') return require('vue')+ if (name === 'vue-i18n') return { useI18n: () => ({ t: (key: string) => key }) }+ if (name === 'tdesign-vue-next') {+ return {+ MessagePlugin: {+ warning() {}, success() {},+ error(msg: string) { messages.push(msg) },+ },+ }+ }+ if (name === '@/api/datasource') return api+ return { default: {} }+ },+ URL, console: vmConsole,+ })+ const component = exports.default+ component.render = () => null+ const renderer = createRenderer<any, any>({+ createElement: () => ({}), createText: () => ({}), createComment: () => ({}),+ insert() {}, remove() {}, setElementText() {}, setText() {}, patchProp() {},+ parentNode: () => null, nextSibling: () => null,+ })+ const instance = ref<any>()+ const app = renderer.createApp({ render: () => h(component, { ...props, ref: instance }) })+ app.mount({})+ props.visible = true+ await nextTick()+ const vm = instance.value+ const creates = () => calls.filter(call => call.method === 'createDataSource').length+ // Let the oldest held create answer.+ const releaseCreate = () => { const resolve = held.shift(); if (resolve) resolve() }+ return {+ vm, props, calls, resourceCalls, messages, warnings, deletes, state, creates,+ releaseCreate, close: () => app.unmount(),+ }+}++test('a load with a usable form creates one paused draft and lists through it', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ f.vm.form.type = 'notion'+ await f.vm.loadResources()+ assert.equal(f.creates(), 1)+ const payload = f.calls[0].args[0]+ assert.equal(payload.type, 'notion')+ assert.equal(payload.knowledge_base_id, 'kb-one')+ assert.equal(payload.status, 'paused')+ assert.equal(f.vm.tempDsId, 'temp-one')+ assert.deepEqual(f.resourceCalls, ['temp-one'])+ assert.deepEqual([...f.vm.resources].map((r: any) => r.external_id), ['page-1', 'page-2'])+ assert.equal(f.vm.loadingResources, false)+ assert.deepEqual(f.messages, [])++ // The draft is remembered: a second load keeps it in step with the form+ // (create mode) instead of creating another row.+ await f.vm.loadResources()+ assert.equal(f.creates(), 1)+ assert.deepEqual(f.calls.map(call => call.method), ['createDataSource', 'updateDataSource'])+ assert.deepEqual(f.resourceCalls, ['temp-one', 'temp-one'])+ } finally { f.close() }+})++test('a load without a connector type sends no request and reports nothing', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ assert.equal(f.vm.form.type, '')+ await f.vm.loadResources()+ assert.deepEqual(f.calls, [])+ assert.deepEqual(f.resourceCalls, [])+ assert.equal(f.vm.tempDsId, '')+ assert.equal(f.vm.resources.length, 0)+ assert.equal(f.vm.loadingResources, false)+ assert.deepEqual(f.messages, [])++ // Whitespace is as unusable as empty.+ f.vm.form.type = ' '+ await f.vm.loadResources()+ assert.deepEqual(f.calls, [])+ assert.deepEqual(f.resourceCalls, [])+ assert.equal(f.vm.tempDsId, '')+ assert.equal(f.vm.loadingResources, false)+ assert.deepEqual(f.messages, [])+ } finally { f.close() }+})++test('a load without a knowledge base sends no request and reports nothing', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ f.vm.form.type = 'notion'+ f.props.kbId = ' '+ await nextTick()+ await f.vm.loadResources()+ assert.deepEqual(f.calls, [])+ assert.deepEqual(f.resourceCalls, [])+ assert.equal(f.vm.tempDsId, '')+ assert.equal(f.vm.loadingResources, false)+ assert.deepEqual(f.messages, [])+ } finally { f.close() }+})++test('restoring the parameters lets the same dialog create the draft on the next attempt', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ await f.vm.loadResources()+ assert.equal(f.creates(), 0)++ f.vm.form.type = 'notion'+ f.props.kbId = ' '+ await nextTick()+ await f.vm.loadResources()+ assert.equal(f.creates(), 0)++ // A skipped attempt is not a failure: the form is filled in, and the very+ // next load creates the draft and lists through it.+ f.props.kbId = 'kb-one'+ await nextTick()+ await f.vm.loadResources()+ assert.equal(f.creates(), 1)+ assert.equal(f.vm.tempDsId, 'temp-one')+ assert.deepEqual(f.resourceCalls, ['temp-one'])+ assert.deepEqual([...f.vm.resources].map((r: any) => r.external_id), ['page-1', 'page-2'])+ } finally { f.close() }+})++test('two concurrent loads share one draft creation', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ f.vm.form.type = 'notion'+ // Both loads reach the create before either has an id to show for it; the+ // second must join the first instead of creating a second row.+ await Promise.all([f.vm.loadResources(), f.vm.loadResources()])+ assert.equal(f.creates(), 1)+ assert.equal(f.vm.tempDsId, 'temp-one')+ assert.deepEqual(f.resourceCalls, ['temp-one', 'temp-one'])+ } finally { f.close() }+})++test('a create that answers without an id is reported and not remembered', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ f.vm.form.type = 'notion'+ f.state.createResult = { data: {} }+ await f.vm.loadResources()+ assert.equal(f.creates(), 1)+ // An id-less row cannot be listed through, so the failure is reported and+ // no listing is sent against an id that does not exist.+ assert.deepEqual(f.messages, ['datasource.saveFailed'])+ assert.deepEqual(f.resourceCalls, [])+ assert.equal(f.vm.tempDsId, '')+ assert.equal(f.vm.loadingResources, false)++ // The next attempt is free to create a usable draft — and the id it answers+ // with is normalized, so the stored one is exactly what a request may carry.+ f.state.createResult = { id: ' temp-two ' }+ await f.vm.loadResources()+ assert.equal(f.creates(), 2)+ assert.equal(f.vm.tempDsId, 'temp-two')+ assert.deepEqual(f.resourceCalls, ['temp-two'])+ // The report belongs to the failed attempt alone.+ assert.deepEqual(f.messages, ['datasource.saveFailed'])+ } finally { f.close() }+})++test('a failed create is not remembered and is retried', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ f.vm.form.type = 'notion'+ f.state.createFails = true+ await f.vm.loadResources()+ assert.equal(f.creates(), 1)+ assert.equal(f.vm.tempDsId, '')+ assert.deepEqual(f.resourceCalls, [])+ assert.deepEqual(f.messages, ['create failed'])+ assert.equal(f.vm.loadingResources, false)++ f.state.createFails = false+ await f.vm.loadResources()+ assert.equal(f.creates(), 2)+ assert.equal(f.vm.tempDsId, 'temp-one')+ assert.deepEqual(f.resourceCalls, ['temp-one'])+ assert.deepEqual(f.messages, ['create failed'])+ } finally { f.close() }+})++test('a Drive root load creates the draft, lists the folder and reveals the tree', async () => {+ for (const type of ['feishu_drive', 'lark_drive']) {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = type+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 1, type)+ assert.equal(f.calls[0].args[0].type, type)+ assert.equal(f.calls[0].args[0].knowledge_base_id, 'kb-one')+ assert.equal(f.calls[0].args[0].status, 'paused')+ assert.equal(f.vm.tempDsId, 'temp-one', type)+ assert.deepEqual(f.resourceCalls, ['temp-one'], type)+ assert.equal(f.vm.driveRootLoaded, true, type)+ assert.deepEqual([...f.vm.resources].map((r: any) => r.external_id), ['folderTokenSynthetic:fileA'], type)+ assert.deepEqual(f.messages, [], type)+ } finally { f.close() }+ }+})++test('a Drive root load without a connector type or a knowledge base sends nothing', async () => {+ for (const type of ['feishu_drive', 'lark_drive']) {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ // No connector type picked yet: the token is kept, the request is not sent.+ await f.vm.loadDriveRoot()+ assert.deepEqual(f.calls, [], type)+ assert.deepEqual(f.resourceCalls, [], type)+ assert.equal(f.vm.tempDsId, '', type)+ assert.equal(f.vm.driveRootLoaded, false, type)+ assert.equal(f.vm.driveFolderToken, 'folderTokenSynthetic', type)+ assert.deepEqual(f.messages, [], type)++ f.vm.form.type = ' '+ await f.vm.loadDriveRoot()+ assert.deepEqual(f.calls, [], type)+ assert.deepEqual(f.resourceCalls, [], type)++ f.vm.form.type = type+ f.props.kbId = ' '+ await nextTick()+ await f.vm.loadDriveRoot()+ assert.deepEqual(f.calls, [], type)+ assert.deepEqual(f.resourceCalls, [], type)+ assert.equal(f.vm.driveRootLoaded, false, type)+ assert.deepEqual(f.messages, [], type)+ assert.equal(f.vm.loadingResources, false, type)+ } finally { f.close() }+ }+})++test('restoring the parameters lets the Drive root load proceed', async () => {+ for (const type of ['feishu_drive', 'lark_drive']) {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 0, type)++ f.vm.form.type = type+ f.props.kbId = 'kb-one'+ await nextTick()+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 1, type)+ assert.equal(f.vm.tempDsId, 'temp-one', type)+ assert.deepEqual(f.resourceCalls, ['temp-one'], type)+ assert.equal(f.vm.driveRootLoaded, true, type)+ assert.deepEqual(f.messages, [], type)+ } finally { f.close() }+ }+})++test('a Drive root load on an existing draft updates it instead of creating one', async () => {+ for (const type of ['feishu_drive', 'lark_drive']) {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = type+ // A draft already exists (edit mode, or a previous listing): the guard+ // must not stand in the way of persisting the new folder token.+ f.vm.tempDsId = 'source-one'+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 0, type)+ assert.deepEqual(f.calls.map(call => call.method), ['updateDataSource'], type)+ assert.equal(f.calls[0].args[0], 'source-one', type)+ assert.equal(f.calls[0].args[1].config.resource_ids[0], 'folderTokenSynthetic', type)+ assert.deepEqual(f.resourceCalls, ['source-one'], type)+ assert.equal(f.vm.driveRootLoaded, true, type)+ } finally { f.close() }+ }+})++test('a Drive root load that fails reports the error and clears the spinner', async () => {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = 'feishu_drive'+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ // A folder the app was never shared with: the user gets the actionable+ // hint, not the raw Feishu body.+ f.state.listFails = new Error('status=403 forbidden')+ await f.vm.loadDriveRoot()+ assert.deepEqual(f.messages, ['datasource.drive.loadForbiddenHint'])+ assert.equal(f.vm.driveRootLoaded, false)+ assert.equal(f.vm.loadingResources, false)+ } finally { f.close() }+})++test('a Drive root load whose draft create fails reports it and keeps no draft', async () => {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = 'feishu_drive'+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ f.state.createFails = true+ await f.vm.loadDriveRoot()+ assert.deepEqual(f.messages, ['create failed'])+ assert.equal(f.vm.tempDsId, '')+ assert.deepEqual(f.resourceCalls, [])+ assert.equal(f.vm.driveRootLoaded, false)+ assert.equal(f.vm.loadingResources, false)++ // The failure is not remembered: the next attempt creates the draft.+ f.state.createFails = false+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 2)+ assert.equal(f.vm.tempDsId, 'temp-one')+ assert.deepEqual(f.resourceCalls, ['temp-one'])+ assert.equal(f.vm.driveRootLoaded, true)+ } finally { f.close() }+})++test('a Drive root load with no folder token reports the inline error and sends nothing', async () => {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = 'feishu_drive'+ f.vm.driveFolderToken = ''+ await f.vm.loadDriveRoot()+ assert.equal(f.vm.driveFolderTokenError, 'datasource.drive.folderTokenRequired')+ assert.deepEqual(f.calls, [])+ assert.deepEqual(f.resourceCalls, [])+ assert.deepEqual(f.messages, [])+ } finally { f.close() }+})++test('two concurrent Drive root loads share one draft creation', async () => {+ for (const type of ['feishu_drive', 'lark_drive']) {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = type+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ // The input binds Enter to loadDriveRoot and Enter is not gated by the+ // button's :loading state, so two quick presses reach the create before+ // either has an id. The second must join the first: a second row would+ // never be deleted, because only one id can be remembered.+ await Promise.all([f.vm.loadDriveRoot(), f.vm.loadDriveRoot()])+ assert.equal(f.creates(), 1, type)+ assert.equal(f.vm.tempDsId, 'temp-one', type)+ assert.deepEqual(f.resourceCalls, ['temp-one', 'temp-one'], type)+ assert.equal(f.vm.driveRootLoaded, true, type)+ assert.deepEqual(f.messages, [], type)+ } finally { f.close() }+ }+})++test('a Drive root load whose create answers without an id reports it and sends no listing', async () => {+ for (const type of ['feishu_drive', 'lark_drive']) {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ f.vm.form.type = type+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ f.state.createResult = { data: {} }+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 1, type)+ // No id means there is nothing a listing could go through: the request+ // that would have been sent as /datasource//resources is not sent, and+ // the user is told the create failed instead of nothing happening.+ assert.deepEqual(f.resourceCalls, [], type)+ assert.equal(f.vm.tempDsId, '', type)+ assert.equal(f.vm.driveRootLoaded, false, type)+ assert.equal(f.vm.loadingResources, false, type)+ assert.deepEqual(f.deletes, [], type)+ assert.deepEqual(f.messages, ['datasource.saveFailed'], type)++ // The id-less answer is not remembered: the next attempt creates a+ // usable draft and normal loading resumes.+ f.state.createResult = { id: 'temp-two' }+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 2, type)+ assert.equal(f.vm.tempDsId, 'temp-two', type)+ assert.deepEqual(f.resourceCalls, ['temp-two'], type)+ assert.equal(f.vm.driveRootLoaded, true, type)+ } finally { f.close() }+ }+})++test('a Drive root load in edit mode flushes the new folder token before listing', async () => {+ const f = await fixture({ resources: DRIVE_RESOURCES })+ try {+ // Edit mode: the row already exists, so ensureTempDataSource must not skip+ // the update — the listing reads the folder token from the stored row, and+ // a skipped flush would list the old folder instead of the new one.+ f.props.dataSource = {+ id: 'source-one', type: 'feishu_drive', name: 'Drive',+ config: { resource_ids: ['oldToken'] },+ }+ f.props.visible = false+ await nextTick()+ f.props.visible = true+ await nextTick()+ assert.equal(f.vm.tempDsId, 'source-one')++ f.vm.driveFolderToken = 'newToken'+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 0)+ assert.deepEqual(f.calls.map(call => call.method), ['updateDataSource'])+ assert.equal(f.calls[0].args[0], 'source-one')+ assert.equal(f.calls[0].args[1].config.resource_ids[0], 'newToken')+ assert.deepEqual(f.resourceCalls, ['source-one'])+ assert.equal(f.vm.driveRootLoaded, true)+ assert.deepEqual(f.messages, [])+ } finally { f.close() }+})++test('closing the drawer while the draft create is in flight deletes the row it answers with', async () => {+ const f = await fixture({ resources: TREE_RESOURCES, deferCreates: 1 })+ try {+ f.vm.form.type = 'notion'+ const load = f.vm.loadResources()+ // The drawer closes before the create answers: tempDsId is still empty, so+ // the close handler has no id to delete and nothing else would clean up.+ f.props.visible = false+ await nextTick()+ assert.deepEqual(f.deletes, [])++ f.releaseCreate()+ await load+ // The late answer belongs to a session that is over: it must not be written+ // into the dialog, and its row must not be left behind as an orphan.+ assert.equal(f.vm.tempDsId, '')+ assert.deepEqual(f.deletes, ['temp-one'])+ assert.deepEqual(f.resourceCalls, [])+ assert.deepEqual(f.messages, [])+ assert.equal(f.vm.loadingResources, false)+ } finally { f.close() }+})++test('reopening the drawer while a create is in flight gives the new session its own draft', async () => {+ const f = await fixture({ resources: TREE_RESOURCES, deferCreates: 1 })+ try {+ f.vm.form.type = 'notion'+ const first = f.vm.loadResources()++ // Closed and reopened before the create answers, with an empty form: the+ // new session must create its own draft rather than joining — and then+ // listing through — the one the previous session's form produced.+ f.props.visible = false+ await nextTick()+ f.props.visible = true+ await nextTick()+ f.vm.form.type = 'notion'+ f.state.createResult = { id: 'temp-two' }+ const second = f.vm.loadResources()+ // Let the abandoned create answer (releasing first rather than awaiting it+ // keeps a session that wrongly joined it from deadlocking the test).+ f.releaseCreate()+ await Promise.all([first, second])++ assert.equal(f.creates(), 2)+ assert.equal(f.vm.tempDsId, 'temp-two')+ // The abandoned row is deleted, and it must not have overwritten the id the+ // new session is listing through.+ assert.deepEqual(f.deletes, ['temp-one'])+ assert.deepEqual(f.resourceCalls, ['temp-two'])+ assert.deepEqual(f.messages, [])+ } finally { f.close() }+})++test('a skipped create leaves a diagnostic behind', async () => {+ const f = await fixture({ resources: TREE_RESOURCES })+ try {+ // Nothing the user can see happens on a skipped create (by design: the+ // request could not have succeeded), so the console line is the only trace+ // a "I pressed load and nothing happened" report can be checked against.+ await f.vm.loadResources()+ assert.equal(f.creates(), 0)+ assert.equal(f.warnings.length, 1)+ assert.match(f.warnings[0], /skipped creating a temporary data source/)++ // The Drive path reports the same way.+ f.vm.driveFolderToken = 'folderTokenSynthetic'+ await f.vm.loadDriveRoot()+ assert.equal(f.creates(), 0)+ assert.equal(f.warnings.length, 2)+ assert.match(f.warnings[1], /skipped creating a temporary data source/)+ assert.deepEqual(f.messages, [])+ } finally { f.close() }+})diff --git a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vueindex 94d9409d0..a3641460d 100644--- a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue+++ b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue@@ -348,25 +348,18 @@ async function loadDriveRoot() {driveRootLoaded.value = falseloadingResources.value = truetry {- if (!tempDsId.value) {- const res = await createDataSource({- ...form.value,- knowledge_base_id: props.kbId,- status: 'paused',- } as any)- const created = res?.data || res- tempDsId.value = created.id- } else {- // Edit mode OR a previously-created temp row: persist the new folder_token- // so listResources sees the updated config. Previously this branch skipped- // updates in edit mode, leaving listResources reading the old folder_token.- await updateDataSource(tempDsId.value, {- ...form.value,- knowledge_base_id: props.kbId,- } as any)- }-- const res = await listResources(tempDsId.value)+ // The Drive root lists through the very same single draft as the generic+ // picker, obtained through the same guard: an answer without an id is a+ // reported failure instead of a listing against an empty id, and two quick+ // Enter presses share one create instead of leaving a second paused row+ // behind. Drive additionally needs the new folder_token stored before it+ // lists (the listing reads the token from the row), so it flushes the form+ // even in edit mode — which the generic picker deliberately does not do,+ // leaving the existing row untouched until submit.+ const dsId = await ensureTempDataSource(true)+ if (!dsId) return++ const res = await listResources(dsId)resources.value = res?.data || res || []if (resources.value.length > 0) {// Mirror loadResources' tree initialization: index parents that already@@ -395,8 +388,11 @@ async function loadDriveRoot() {}} catch (e: any) {MessagePlugin.error(classifyDriveLoadError(e))+ } finally {+ // A skipped load returns from the try block, so the spinner is cleared here+ // rather than after the block.+ loadingResources.value = false}- loadingResources.value = false}// classifyDriveLoadError turns a raw Drive list error into an actionable i18n@@ -800,7 +796,21 @@ const displayedCredentialFields = computed(() => {})// --- Drawer lifecycle ---++// The draft the picker lists through is per-session state: one open drawer, one+// draft. `drawerSession` counts opens and closes; a create that was sent in an+// earlier session is recognized by the mismatch and cleans its own row up (see+// ensureTempDataSource), instead of writing its id into a drawer that may by+// then be closed — or reopened with a different form.+let drawerSession = 0+let tempDsPromise: Promise<string> | null = null+watch(visible, async (v) => {+ // Both edges start a new session: opening begins one, closing ends it.+ drawerSession+++ // A create from the session that was just left is not something a session+ // starting now may join, so the shared slot is emptied on both edges.+ tempDsPromise = nullif (!v) {if (!isEdit.value && tempDsId.value) {try {@@ -997,26 +1007,116 @@ async function testConnection() {}// --- Load resources ----async function loadResources() {- loadingResources.value = true- try {- syncConfluencePublicFieldsToSettings()- if (!tempDsId.value) {- const res = await createDataSource({++// canCreateTempDataSource reports whether the create endpoint could accept a+// request built from the form as it stands. The endpoint rejects a request+// without a knowledge base (ErrKnowledgeBaseNotFound) and one whose type is not+// a registered connector (ErrConnectorNotFound) before the connector is ever+// reached, so a request missing either is certain to fail and must not be sent.+// The one thing left that the endpoint checks — the credentials a connector+// validates against the remote service — can only be answered by making the+// call, so it is deliberately not pre-empted here.+function canCreateTempDataSource(): boolean {+ return (props.kbId || '').trim() !== '' && String(form.value.type || '').trim() !== ''+}++// ensureTempDataSource returns the data source the picker lists through,+// creating the paused draft on first use (a data source must exist before the+// resource endpoint can be called) and keeping it in step with the form while+// the dialog is still a draft.+//+// This is not a side-effect-free lookup: with a draft already in hand it PUTs+// the current form to it (create mode), which is what keeps the row equal to+// the form the user sees. `persist` extends that flush to edit mode, where the+// row is otherwise left alone until submit — the Drive root loader needs it,+// because the listing it is about to make reads the folder token from the row.+//+// A single in-flight creation is shared, so two callers cannot create two rows+// for one dialog. The shared attempt is scoped to the drawer session that+// started it (see the visible watcher above): if the drawer was closed or+// reopened while the create was in flight, the row it produced is deleted and+// no id is handed back, so a late answer can neither be adopted by a new+// session nor be left behind as an orphan.+async function ensureTempDataSource(persist = false): Promise<string> {+ if (tempDsId.value) {+ if (persist || !isEdit.value) {+ await updateDataSource(tempDsId.value, {...form.value,knowledge_base_id: props.kbId,- status: 'paused',} as any)- const created = res?.data || res- tempDsId.value = created.id- } else if (!isEdit.value) {- await updateDataSource(tempDsId.value, {+ }+ return tempDsId.value+ }+ // A form that cannot produce a data source yet has nothing to list through,+ // so the create is skipped rather than sent: no request, no error, and — this+ // is the point of returning early — no cached promise, so a later attempt+ // with a real type creates the draft normally.+ if (!canCreateTempDataSource()) {+ // The drawer cannot reach this state through its own UI (a connector type+ // is always picked before step 2, and the parent only renders the dialog+ // once it has a knowledge base), so there is no form field the user could+ // be pointed at. A console line is what makes a "I pressed load and nothing+ // happened" report diagnosable.+ console.warn(+ '[DataSourceEditorDialog] skipped creating a temporary data source: ' ++ `connector type ${JSON.stringify(form.value.type)} or knowledge base id ${JSON.stringify(props.kbId)} is empty`,+ )+ return ''+ }+ if (!tempDsPromise) {+ const ownSession = drawerSession+ let pending: Promise<string>+ pending = (async () => {+ const res = await createDataSource({...form.value,knowledge_base_id: props.kbId,+ status: 'paused',} as any)- }+ const created = res?.data || res+ const createdId = typeof created?.id === 'string' ? created.id.trim() : ''+ if (drawerSession !== ownSession) {+ // The drawer was closed (or closed and reopened) while the create was+ // in flight. Nobody will ever list through — or clean up — this row, so+ // it is removed here, and '' tells the stale caller to stop rather than+ // report an error about a drawer the user already dismissed.+ if (createdId) {+ try {+ await deleteDataSource(createdId)+ } catch {+ // Ignore cleanup errors+ }+ }+ return ''+ }+ if (!createdId) {+ // A create that answered without an id cannot be listed through, and+ // /datasource//resources is certain to fail. Failing here instead keeps+ // tempDsId unset, so the next attempt can create a usable draft.+ throw new Error(t('datasource.saveFailed'))+ }+ tempDsId.value = createdId+ return createdId+ })().finally(() => {+ // A settled attempt is never cached — a failed create must stay+ // retryable — but it must not clear a create a later session started.+ if (tempDsPromise === pending) tempDsPromise = null+ })+ tempDsPromise = pending+ }+ return tempDsPromise+}- const res = await listResources(tempDsId.value)+async function loadResources() {+ loadingResources.value = true+ try {+ syncConfluencePublicFieldsToSettings()+ // No id means the form cannot be created yet (a blank connector type, or a+ // missing knowledge base): the tree simply does not load, and nothing is+ // reported — the request that would have produced that error is not sent.+ const dsId = await ensureTempDataSource()+ if (!dsId) return++ const res = await listResources(dsId)resources.value = res?.data || res || []// Any parent that already arrived with children (connectors returning the// full tree, e.g. Notion) needs no further lazy fetch.@@ -1045,8 +1145,11 @@ async function loadResources() {}} catch (e: any) {MessagePlugin.error(e?.message || e?.error || t('datasource.resourceLoadFailed'))+ } finally {+ // A skipped load returns from the try block, so the spinner is cleared here+ // rather than after the block.+ loadingResources.value = false}- loadingResources.value = false}// revealExistingSelections asks the backend which ancestors must be expanded to