Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix: restore structured drafts after core downgrade (#1040)

Mao Mr1a1411342593
diff --git a/src-tauri/src/service/patch/conversation_draft.rs b/src-tauri/src/service/patch/conversation_draft.rs
new file mode 100644
index 00000000..8999134c
--- /dev/null
+++ b/src-tauri/src/service/patch/conversation_draft.rs
@@ -0,0 +1,69 @@
+use std::path::Path;
+
+use crate::utils::{patch_core_file, patch_dsh, PatchOutcome};
+
+const CONVERSATION_CLIENT_JS: &str =
+ "node_modules/@deepseek-ai/dsh-client-ui-conversation/lib/client.js";
+const SET_DRAFT_ORIGINAL: &str =
+ "setDraft(text) {\n\t\t\t\tthis.draftEditor.setDraft(text);\n\t\t\t}";
+const SET_DRAFT_PATCHED: &str = r#"setDraft(text) {
+ if (typeof text === "string") {
+ this.draftEditor.setDraft(text);
+ return;
+ }
+ const draft = text && typeof text.text === "string" ? text.text : "";
+ const references = text?.references;
+ let end = 0;
+ const valid = Array.isArray(references) && references.every((reference) => {
+ if (!reference || typeof reference.source !== "string" || typeof reference.ref !== "string" || typeof reference.label !== "string" || typeof reference.clipboardText !== "string") return false;
+ if (!Number.isSafeInteger(reference.offset) || reference.offset < end || !Number.isSafeInteger(reference.length) || reference.length <= 0) return false;
+ if (reference.appearance !== void 0 && !["session", "file", "folder"].includes(reference.appearance)) return false;
+ if (reference.invalid !== void 0 && typeof reference.invalid !== "boolean") return false;
+ end = reference.offset + reference.length;
+ return reference.length === reference.clipboardText.length && end <= draft.length && draft.slice(reference.offset, end) === reference.clipboardText;
+ });
+ if (valid && references.length > 0) this.draftEditor.restoreDraft(draft, references);
+ else this.draftEditor.setDraft(draft);
+ }"#;
+
+fn patch_source(source: &str) -> PatchOutcome {
+ if source.contains(SET_DRAFT_PATCHED) {
+ return PatchOutcome::AlreadyPatched;
+ }
+ if !source.contains(SET_DRAFT_ORIGINAL) {
+ return PatchOutcome::AnchorMissing;
+ }
+ PatchOutcome::Patched(source.replacen(SET_DRAFT_ORIGINAL, SET_DRAFT_PATCHED, 1))
+}
+
+pub fn apply_at(core_dir: &Path) -> Result<(), String> {
+ patch_core_file(core_dir, CONVERSATION_CLIENT_JS, patch_source)
+}
+
+pub fn apply(app_handle: &tauri::AppHandle) -> Result<(), String> {
+ patch_dsh(app_handle, CONVERSATION_CLIENT_JS, patch_source)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn patches_legacy_set_draft_once() {
+ let source = format!("class SessionInputShell {{ {SET_DRAFT_ORIGINAL} }}");
+ let PatchOutcome::Patched(patched) = patch_source(&source) else {
+ panic!("expected Patched");
+ };
+ assert_eq!(
+ patched,
+ format!("class SessionInputShell {{ {SET_DRAFT_PATCHED} }}")
+ );
+ assert_eq!(patch_source(&patched), PatchOutcome::AlreadyPatched);
+ }
+
+ #[test]
+ fn leaves_new_core_and_missing_anchor_unchanged() {
+ let source = "setDraft(text) { const draft = resolveDraftInput(text); }";
+ assert_eq!(patch_source(source), PatchOutcome::AnchorMissing);
+ }
+}
diff --git a/src-tauri/src/service/patch/mod.rs b/src-tauri/src/service/patch/mod.rs
index 126c80e4..b8d49a7f 100644
--- a/src-tauri/src/service/patch/mod.rs
+++ b/src-tauri/src/service/patch/mod.rs
@@ -10,6 +10,7 @@
//! 失败仅告警。
pub(crate) mod composer;
+pub(crate) mod conversation_draft;
pub(crate) mod llm_session;
pub(crate) mod mcp_startup;
pub(crate) mod mobile_composer;
@@ -34,10 +35,11 @@ use std::path::Path;
/// 让编排层能看见哪一条出了问题。
pub(crate) fn apply_all_at(core_dir: &Path) -> Result<(), String> {
#[allow(clippy::type_complexity)]
- let patches: [(&str, fn(&Path) -> Result<(), String>); 12] = [
+ let patches: [(&str, fn(&Path) -> Result<(), String>); 13] = [
("renderer", renderer::apply_at),
("mcp_startup", mcp_startup::apply_at),
("composer", composer::apply_at),
+ ("conversation_draft", conversation_draft::apply_at),
("mobile_composer", mobile_composer::apply_at),
("mobile_sidebar", mobile_sidebar::apply_at),
("session", session::apply_at),
diff --git a/src-tauri/src/service/workflow/launch.rs b/src-tauri/src/service/workflow/launch.rs
index 757ed8fb..daa1a984 100644
--- a/src-tauri/src/service/workflow/launch.rs
+++ b/src-tauri/src/service/workflow/launch.rs
@@ -419,6 +419,9 @@ pub async fn launch(app_handle: tauri::AppHandle) -> Result<(), String> {
if let Err(e) = crate::service::patch::composer::apply(&app_handle) {
log::warn!("composer workspace-less patch failed: {e}");
}
+ if let Err(e) = crate::service::patch::conversation_draft::apply(&app_handle) {
+ log::warn!("conversation draft compatibility patch failed: {e}");
+ }
if let Err(e) = crate::service::patch::mobile_composer::apply(&app_handle) {
log::warn!("mobile composer patch failed: {e}");
}
diff --git a/test/conversation-draft-patch.test.ts b/test/conversation-draft-patch.test.ts
new file mode 100644
index 00000000..7cbbb69c
--- /dev/null
+++ b/test/conversation-draft-patch.test.ts
@@ -0,0 +1,77 @@
+import { readFileSync } from 'node:fs'
+import { runInNewContext } from 'node:vm'
+import { describe, expect, it } from 'vitest'
+
+const source = readFileSync(new URL('../src-tauri/src/service/patch/conversation_draft.rs', import.meta.url), 'utf8')
+const body = source.match(/const SET_DRAFT_PATCHED: &str = r#"([\s\S]*?)"#;/)?.[1]
+if (!body)
+ throw new Error('Missing production setDraft patch')
+
+function restore(input: unknown) {
+ const calls: unknown[][] = []
+ const shell = runInNewContext(`({ draftEditor, ${body} })`, {
+ draftEditor: {
+ setDraft(text: string) {
+ calls.push(['text', text.replace(/\uFFFC/g, '')])
+ },
+ restoreDraft(text: string, references: unknown[]) {
+ calls.push(['references', text, references])
+ },
+ },
+ })
+ shell.setDraft(input)
+ return calls
+}
+
+const reference = {
+ source: 'file',
+ ref: '/workspace/readme.md',
+ label: 'readme.md',
+ offset: 3,
+ length: 10,
+ clipboardText: '@readme.md',
+}
+
+const draft = { text: '读一下@readme.md\n再解释', references: [reference] }
+
+describe('legacy conversation draft patch', () => {
+ it('restores an alpha structured draft through the legacy reference editor', () => {
+ expect(restore(draft)).toEqual([['references', draft.text, [reference]]])
+ })
+
+ it('preserves legacy string and chip-free structured drafts', () => {
+ expect(restore('旧草稿\n第二行')).toEqual([['text', '旧草稿\n第二行']])
+ expect(restore({ text: '新版草稿\n第二行', references: [] })).toEqual([['text', '新版草稿\n第二行']])
+ expect(restore({ text: '', references: [] })).toEqual([['text', '']])
+ })
+
+ it('falls back to the original text for malformed reference spans without mutating storage', () => {
+ const malformed = { ...draft, references: [{ ...reference, offset: -1 }] }
+ const before = structuredClone(malformed)
+ expect(restore(malformed)).toEqual([['text', draft.text]])
+ expect(malformed).toEqual(before)
+ })
+
+ it.each([
+ null,
+ undefined,
+ 1,
+ [],
+ { text: 1, references: [] },
+ ].map(input => ({ input })))('does not pass invalid persisted data $input to the string editor', ({ input }) => {
+ expect(restore(input)).toEqual([['text', '']])
+ })
+
+ it.each([
+ null,
+ [null],
+ [{ ...reference, source: 1 }],
+ [{ ...reference, offset: 0.5 }],
+ [{ ...reference, length: 1 }],
+ [{ ...reference, offset: 100 }],
+ [{ ...reference, clipboardText: 'wrong text', length: 10 }],
+ [reference, reference],
+ ].map(references => ({ references })))('keeps draft text when reference data $references is invalid', ({ references }) => {
+ expect(restore({ text: draft.text, references })).toEqual([['text', draft.text]])
+ })
+})