Sfoglia il codice sorgente

feat(app): draft prompt state (#31452)

Brendan Allan 2 mesi fa
parent
commit
1a08ee77c7
1 ha cambiato i file con 19 aggiunte e 12 eliminazioni
  1. 19 12
      packages/app/src/context/prompt.tsx

+ 19 - 12
packages/app/src/context/prompt.tsx

@@ -1,6 +1,6 @@
 import { createSimpleContext } from "@opencode-ai/ui/context"
 import { checksum } from "@opencode-ai/core/util/encode"
-import { useParams } from "@solidjs/router"
+import { useParams, useSearchParams } from "@solidjs/router"
 import { batch, createMemo, createRoot, getOwner, onCleanup } from "solid-js"
 import { createStore, type SetStoreFunction } from "solid-js/store"
 import type { FileSelection } from "@/context/file"
@@ -153,9 +153,11 @@ const MAX_PROMPT_SESSIONS = 20
 
 type PromptSession = ReturnType<typeof createPromptSession>
 
-type Scope = {
-  dir: string
-  id?: string
+type Scope = { draftID: string } | { dir: string; id?: string }
+
+function scopeKey(scope: Scope) {
+  if ("draftID" in scope) return `draft:${scope.draftID}`
+  return `${scope.dir}:${scope.id ?? WORKSPACE_KEY}`
 }
 
 type PromptCacheEntry = {
@@ -163,11 +165,15 @@ type PromptCacheEntry = {
   dispose: VoidFunction
 }
 
-function createPromptSession(scope: ServerScope, dir: string, id: string | undefined) {
-  const legacy = `${dir}/prompt${id ? "/" + id : ""}.v2`
+function promptTarget(serverScope: ServerScope, scope: Scope) {
+  if ("draftID" in scope) return Persist.draft(scope.draftID, "prompt")
+  const legacy = `${scope.dir}/prompt${scope.id ? "/" + scope.id : ""}.v2`
+  return Persist.serverScoped(serverScope, scope.dir, scope.id, "prompt", [legacy])
+}
 
+function createPromptSession(serverScope: ServerScope, scope: Scope) {
   const [store, setStore, _, ready] = persisted(
-    Persist.serverScoped(scope, dir, id, "prompt", [legacy]),
+    promptTarget(serverScope, scope),
     createStore<{
       prompt: Prompt
       cursor?: number
@@ -231,6 +237,7 @@ export const { use: usePrompt, provider: PromptProvider } = createSimpleContext(
   gate: false,
   init: () => {
     const params = useParams()
+    const [search] = useSearchParams<{ draftId?: string }>()
     const serverSDK = useServerSDK()
     const cache = new Map<string, PromptCacheEntry>()
 
@@ -254,8 +261,8 @@ export const { use: usePrompt, provider: PromptProvider } = createSimpleContext(
     }
 
     const owner = getOwner()
-    const load = (dir: string, id: string | undefined) => {
-      const key = `${dir}:${id ?? WORKSPACE_KEY}`
+    const load = (scope: Scope) => {
+      const key = scopeKey(scope)
       const existing = cache.get(key)
       if (existing) {
         cache.delete(key)
@@ -265,7 +272,7 @@ export const { use: usePrompt, provider: PromptProvider } = createSimpleContext(
 
       const entry = createRoot(
         (dispose) => ({
-          value: createPromptSession(serverSDK.scope, dir, id),
+          value: createPromptSession(serverSDK.scope, scope),
           dispose,
         }),
         owner,
@@ -276,8 +283,8 @@ export const { use: usePrompt, provider: PromptProvider } = createSimpleContext(
       return entry.value
     }
 
-    const session = createMemo(() => load(params.dir!, params.id))
-    const pick = (scope?: Scope) => (scope ? load(scope.dir, scope.id) : session())
+    const session = createMemo(() => load(search.draftId ? { draftID: search.draftId } : { dir: params.dir!, id: params.id }))
+    const pick = (scope?: Scope) => (scope ? load(scope) : session())
 
     return {
       ready: () => session().ready,