Add a packaged-client runtime boundary so the shared UI can talk to local, desktop, remote, and VS Code runtimes through the right transport instead of assuming one same-origin web server. Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and runtime URL helpers, while keeping official OpenCode traffic on the SDK path. Support runtime switching, remote host selection, desktop client credentials, and headless connection links for pairing packaged clients with remote OpenChamber servers. Harden the new auth model by moving long-lived client tokens out of browser URLs, introducing short-lived scoped URL tokens for browser-owned transports, restricting URL-token access to explicit readable/realtime routes, and making client-token management session-scoped or self-scoped as appropriate. Update browser-owned assets and preview proxy flows to work with the split runtime model, including authenticated project icons, preview token propagation, CSP-safe preview bridge injection, and preview proxy auth that survives short-lived URL-token expiry. Tighten Electron security boundaries for packaged clients by gating privileged preload state to trusted origins and requiring explicit confirmation before connect deep-links import or switch remote runtimes. Also refresh agent guidance and project skills so future runtime/API, auth, preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new architecture.
134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
import type { Message, Part } from "@opencode-ai/sdk/v2/client"
|
|
import { useCallback } from "react"
|
|
import { opencodeClient } from "@/lib/opencode/client"
|
|
import { useDirectoryStore, useSyncDirectory } from "./sync-context"
|
|
import { useSync } from "./use-sync"
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Ascending ID generator — monotonic timestamp + sequence counter
|
|
// ---------------------------------------------------------------------------
|
|
|
|
let counter = 0
|
|
|
|
function ascending(prefix: string): string {
|
|
const now = Date.now()
|
|
const seq = (counter++ % 1000).toString().padStart(3, "0")
|
|
return `${prefix}_${now}${seq}`
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prompt submission with optimistic updates
|
|
// Prompt submission with optimistic message insertion
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export type SubmitInput = {
|
|
sessionID: string
|
|
text: string
|
|
parts?: Part[]
|
|
agent: string
|
|
model: { providerID: string; modelID: string }
|
|
variant?: string
|
|
command?: { name: string; arguments: string }
|
|
images?: Array<{ id?: string; type: "file"; mime: string; url: string; filename: string }>
|
|
}
|
|
|
|
export function usePromptSubmit() {
|
|
const store = useDirectoryStore()
|
|
const directory = useSyncDirectory()
|
|
const sync = useSync()
|
|
|
|
const submit = useCallback(
|
|
async (input: SubmitInput) => {
|
|
const messageID = ascending("message")
|
|
|
|
// Build optimistic user message
|
|
const message: Message = {
|
|
id: messageID,
|
|
sessionID: input.sessionID,
|
|
role: "user",
|
|
time: { created: Date.now() },
|
|
agent: input.agent,
|
|
model: input.model,
|
|
variant: input.variant,
|
|
} as Message
|
|
|
|
// Build optimistic parts
|
|
const textPart: Part = {
|
|
id: ascending("part"),
|
|
sessionID: input.sessionID,
|
|
messageID,
|
|
type: "text",
|
|
text: input.text,
|
|
} as Part
|
|
|
|
const optimisticParts: Part[] = [textPart, ...(input.parts ?? [])]
|
|
|
|
// Set busy status optimistically
|
|
store.setState((prev) => ({
|
|
...prev,
|
|
session_status: {
|
|
...prev.session_status,
|
|
[input.sessionID]: { type: "busy" },
|
|
},
|
|
}))
|
|
|
|
// Add optimistic message immediately
|
|
sync.optimistic.add({
|
|
sessionID: input.sessionID,
|
|
message,
|
|
parts: optimisticParts,
|
|
})
|
|
|
|
try {
|
|
if (input.command) {
|
|
// Slash command
|
|
await opencodeClient.sendCommand({
|
|
id: input.sessionID,
|
|
command: input.command?.name ?? "",
|
|
arguments: input.command?.arguments ?? "",
|
|
agent: input.agent,
|
|
providerID: input.model.providerID,
|
|
modelID: input.model.modelID,
|
|
variant: input.variant,
|
|
files: input.images,
|
|
messageId: messageID,
|
|
directory,
|
|
}).then(() => undefined)
|
|
} else {
|
|
// Regular prompt
|
|
await opencodeClient.sendMessage({
|
|
id: input.sessionID,
|
|
agent: input.agent,
|
|
providerID: input.model.providerID,
|
|
modelID: input.model.modelID,
|
|
messageId: messageID,
|
|
text: input.text,
|
|
files: input.images,
|
|
variant: input.variant,
|
|
directory,
|
|
}).then(() => undefined)
|
|
}
|
|
return true
|
|
} catch (error) {
|
|
// Revert optimistic on failure
|
|
sync.optimistic.remove({
|
|
sessionID: input.sessionID,
|
|
messageID,
|
|
})
|
|
// Reset status
|
|
store.setState((prev) => ({
|
|
...prev,
|
|
session_status: {
|
|
...prev.session_status,
|
|
[input.sessionID]: { type: "idle" },
|
|
},
|
|
}))
|
|
throw error
|
|
}
|
|
},
|
|
[directory, store, sync],
|
|
)
|
|
|
|
return submit
|
|
}
|