mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-09 19:09:49 -04:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c3820b4462 | |||
| ad9d97a92e | |||
| b8c548b51b | |||
| 29494bbf71 |
@@ -7,16 +7,20 @@ import { DialogVariant } from "./dialog-variant"
|
|||||||
import * as fuzzysort from "fuzzysort"
|
import * as fuzzysort from "fuzzysort"
|
||||||
import { useConnected } from "./use-connected"
|
import { useConnected } from "./use-connected"
|
||||||
import { useData } from "../context/data"
|
import { useData } from "../context/data"
|
||||||
|
import { useLocation } from "../context/location"
|
||||||
|
|
||||||
export function DialogModel(props: { providerID?: string }) {
|
export function DialogModel(props: { providerID?: string }) {
|
||||||
const local = useLocal()
|
const local = useLocal()
|
||||||
const data = useData()
|
const data = useData()
|
||||||
|
const location = useLocation()
|
||||||
const dialog = useDialog()
|
const dialog = useDialog()
|
||||||
const [query, setQuery] = createSignal("")
|
const [query, setQuery] = createSignal("")
|
||||||
|
|
||||||
const connected = useConnected()
|
const connected = useConnected()
|
||||||
const providers = createMemo(() => new Map((data.location.provider.list() ?? []).map((item) => [item.id, item])))
|
const providers = createMemo(
|
||||||
const models = createMemo(() => data.location.model.list() ?? [])
|
() => new Map((data.location.provider.list(location.ref) ?? []).map((item) => [item.id, item])),
|
||||||
|
)
|
||||||
|
const models = createMemo(() => data.location.model.list(location.ref) ?? [])
|
||||||
|
|
||||||
const showExtra = createMemo(() => connected() && !props.providerID)
|
const showExtra = createMemo(() => connected() && !props.providerID)
|
||||||
|
|
||||||
|
|||||||
@@ -360,7 +360,8 @@ export function Prompt(props: PromptProps) {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Initialize agent/model/variant from the durable V2 Session state.
|
// Initialize the agent from the durable V2 Session state. Model hydration is
|
||||||
|
// reactive in LocalProvider so it can wait for the location catalog.
|
||||||
let syncedSessionID: string | undefined
|
let syncedSessionID: string | undefined
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
const sessionID = props.sessionID
|
const sessionID = props.sessionID
|
||||||
@@ -369,10 +370,6 @@ export function Prompt(props: PromptProps) {
|
|||||||
if (!session) return
|
if (!session) return
|
||||||
const agent = session.agent && local.agent.list().find((agent) => agent.id === session.agent)
|
const agent = session.agent && local.agent.list().find((agent) => agent.id === session.agent)
|
||||||
if (agent && !args.agent) local.agent.set(agent.id)
|
if (agent && !args.agent) local.agent.set(agent.id)
|
||||||
if (session.model) {
|
|
||||||
local.model.set({ providerID: session.model.providerID, modelID: session.model.id })
|
|
||||||
local.model.variant.set(session.model.variant)
|
|
||||||
}
|
|
||||||
syncedSessionID = sessionID
|
syncedSessionID = sessionID
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { createMemo } from "solid-js"
|
import { createMemo } from "solid-js"
|
||||||
import { useData } from "../context/data"
|
import { useData } from "../context/data"
|
||||||
|
import { useLocation } from "../context/location"
|
||||||
import { hasConnectedProvider } from "../util/connected-provider"
|
import { hasConnectedProvider } from "../util/connected-provider"
|
||||||
|
|
||||||
export function useConnected() {
|
export function useConnected() {
|
||||||
const data = useData()
|
const data = useData()
|
||||||
return createMemo(() => hasConnectedProvider(data.location.integration.list() ?? []))
|
const location = useLocation()
|
||||||
|
return createMemo(() => hasConnectedProvider(data.location.integration.list(location.ref) ?? []))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import { useToast } from "../ui/toast"
|
|||||||
import { useRoute } from "./route"
|
import { useRoute } from "./route"
|
||||||
import { useData } from "./data"
|
import { useData } from "./data"
|
||||||
import { usePermission } from "./permission"
|
import { usePermission } from "./permission"
|
||||||
|
import { useLocation } from "./location"
|
||||||
|
|
||||||
export function parseModel(model: string) {
|
export function parseModel(model: string) {
|
||||||
const [providerID, ...rest] = model.split("/")
|
const [providerID, ...rest] = model.split("/")
|
||||||
@@ -56,11 +57,12 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
|
|||||||
const args = useArgs()
|
const args = useArgs()
|
||||||
const event = useEvent()
|
const event = useEvent()
|
||||||
const permission = usePermission()
|
const permission = usePermission()
|
||||||
|
const location = useLocation()
|
||||||
|
|
||||||
|
const models = () => data.location.model.list(location.ref)
|
||||||
|
|
||||||
function isModelValid(model: ModelPreferenceModel) {
|
function isModelValid(model: ModelPreferenceModel) {
|
||||||
return !!data.location.model
|
return !!models()?.some((item) => item.providerID === model.providerID && item.id === model.modelID)
|
||||||
.list()
|
|
||||||
?.some((item) => item.providerID === model.providerID && item.id === model.modelID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFirstValidModel(...modelFns: (() => ModelPreferenceModel | undefined)[]) {
|
function getFirstValidModel(...modelFns: (() => ModelPreferenceModel | undefined)[]) {
|
||||||
@@ -73,9 +75,11 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
|
|||||||
|
|
||||||
function createAgent() {
|
function createAgent() {
|
||||||
const agents = createMemo(() =>
|
const agents = createMemo(() =>
|
||||||
(data.location.agent.list() ?? []).filter((agent) => agent.mode !== "subagent" && !agent.hidden),
|
(data.location.agent.list(location.ref) ?? []).filter((agent) => agent.mode !== "subagent" && !agent.hidden),
|
||||||
|
)
|
||||||
|
const visibleAgents = createMemo(() =>
|
||||||
|
(data.location.agent.list(location.ref) ?? []).filter((agent) => !agent.hidden),
|
||||||
)
|
)
|
||||||
const visibleAgents = createMemo(() => (data.location.agent.list() ?? []).filter((agent) => !agent.hidden))
|
|
||||||
const [agentStore, setAgentStore] = createStore({
|
const [agentStore, setAgentStore] = createStore({
|
||||||
current: undefined as string | undefined,
|
current: undefined as string | undefined,
|
||||||
})
|
})
|
||||||
@@ -190,7 +194,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const model = data.location.model.list()?.[0]
|
const model = models()?.[0]
|
||||||
if (!model) return undefined
|
if (!model) return undefined
|
||||||
return {
|
return {
|
||||||
providerID: model.providerID,
|
providerID: model.providerID,
|
||||||
@@ -209,6 +213,33 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let syncedSessionModel: string | undefined
|
||||||
|
createEffect(() => {
|
||||||
|
if (route.data.type !== "session") {
|
||||||
|
syncedSessionModel = undefined
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const session = data.session.get(route.data.sessionID)
|
||||||
|
const selected = session?.model
|
||||||
|
const a = agent.current()
|
||||||
|
if (!selected || !a) return
|
||||||
|
if (args.model) {
|
||||||
|
const requested = parseModel(args.model)
|
||||||
|
if (isModelValid(requested)) return
|
||||||
|
}
|
||||||
|
const model = { providerID: selected.providerID, modelID: selected.id }
|
||||||
|
if (!isModelValid(model)) return
|
||||||
|
const fingerprint = [session.id, a.id, selected.providerID, selected.id, selected.variant ?? "default"].join(
|
||||||
|
":",
|
||||||
|
)
|
||||||
|
if (fingerprint === syncedSessionModel) return
|
||||||
|
syncedSessionModel = fingerprint
|
||||||
|
batch(() => {
|
||||||
|
setModelStore("model", a.id, model)
|
||||||
|
setModelStore("variant", modelPreferenceKey(model), normalizeModelVariant(selected.variant))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
current: currentModel,
|
current: currentModel,
|
||||||
get ready() {
|
get ready() {
|
||||||
@@ -229,10 +260,8 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
|
|||||||
reasoning: false,
|
reasoning: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const provider = data.location.provider.list()?.find((item) => item.id === value.providerID)
|
const provider = data.location.provider.list(location.ref)?.find((item) => item.id === value.providerID)
|
||||||
const info = data.location.model
|
const info = models()?.find((item) => item.providerID === value.providerID && item.id === value.modelID)
|
||||||
.list()
|
|
||||||
?.find((item) => item.providerID === value.providerID && item.id === value.modelID)
|
|
||||||
return {
|
return {
|
||||||
provider: provider?.name ?? value.providerID,
|
provider: provider?.name ?? value.providerID,
|
||||||
model: info?.name ?? value.modelID,
|
model: info?.name ?? value.modelID,
|
||||||
@@ -340,9 +369,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
|
|||||||
list() {
|
list() {
|
||||||
const m = currentModel()
|
const m = currentModel()
|
||||||
if (!m) return []
|
if (!m) return []
|
||||||
const info = data.location.model
|
const info = models()?.find((item) => item.providerID === m.providerID && item.id === m.modelID)
|
||||||
.list()
|
|
||||||
?.find((item) => item.providerID === m.providerID && item.id === m.modelID)
|
|
||||||
return info?.variants?.map((variant) => variant.id) ?? []
|
return info?.variants?.map((variant) => variant.id) ?? []
|
||||||
},
|
},
|
||||||
set(value: string | undefined) {
|
set(value: string | undefined) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { useClient } from "./client"
|
|||||||
import { useData } from "./data"
|
import { useData } from "./data"
|
||||||
|
|
||||||
const context = createContext<{
|
const context = createContext<{
|
||||||
|
readonly ref: LocationRef | undefined
|
||||||
readonly current: LocationGetOutput | undefined
|
readonly current: LocationGetOutput | undefined
|
||||||
set: (location?: LocationRef) => void
|
set: (location?: LocationRef) => void
|
||||||
}>()
|
}>()
|
||||||
@@ -34,6 +35,9 @@ export function LocationProvider(props: ParentProps) {
|
|||||||
return (
|
return (
|
||||||
<context.Provider
|
<context.Provider
|
||||||
value={{
|
value={{
|
||||||
|
get ref() {
|
||||||
|
return ref()
|
||||||
|
},
|
||||||
get current() {
|
get current() {
|
||||||
return current()
|
return current()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -138,46 +138,87 @@ test("session lifecycle updates the terminal title and prints the epilogue after
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test("session startup prompt is submitted exactly once", async () => {
|
test("session startup waits for catalogs and follows durable model changes", async () => {
|
||||||
const setup = await createTestRenderer({ width: 80, height: 24, useThread: false })
|
const setup = await createTestRenderer({ width: 80, height: 24, useThread: false })
|
||||||
const core = await import("@opentui/core")
|
const core = await import("@opentui/core")
|
||||||
mock.module("@opentui/core", () => ({ ...core, createCliRenderer: async () => setup.renderer }))
|
mock.module("@opentui/core", () => ({ ...core, createCliRenderer: async () => setup.renderer }))
|
||||||
const events = createEventStream()
|
const events = createEventStream()
|
||||||
const cwd = process.cwd()
|
const cwd = process.cwd()
|
||||||
const location = { directory: cwd, project: { id: "project", directory: cwd } }
|
const sessionDirectory = `${cwd}/session`
|
||||||
|
const location = (directory: string) => ({ directory, project: { id: "project", directory } })
|
||||||
const session = {
|
const session = {
|
||||||
id: "dummy",
|
id: "dummy",
|
||||||
title: "Demo session",
|
title: "Demo session",
|
||||||
projectID: "project",
|
projectID: "project",
|
||||||
location: { directory: cwd },
|
location: { directory: sessionDirectory },
|
||||||
agent: "build",
|
agent: "build",
|
||||||
model: { providerID: "provider", id: "model" },
|
model: { providerID: "provider", id: "model", variant: "high" },
|
||||||
cost: 0,
|
cost: 0,
|
||||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||||
time: { created: 0, updated: 0 },
|
time: { created: 0, updated: 0 },
|
||||||
}
|
}
|
||||||
const bodies: unknown[] = []
|
const bodies: unknown[] = []
|
||||||
|
const modelSwitches: unknown[] = []
|
||||||
|
const locationRequested = Promise.withResolvers<void>()
|
||||||
|
const targetLocation = Promise.withResolvers<void>()
|
||||||
|
const catalogRequested = Promise.withResolvers<void>()
|
||||||
|
const catalog = Promise.withResolvers<void>()
|
||||||
const promptSubmitted = Promise.withResolvers<void>()
|
const promptSubmitted = Promise.withResolvers<void>()
|
||||||
|
const secondPromptSubmitted = Promise.withResolvers<void>()
|
||||||
const calls = createFetch(async (url, request) => {
|
const calls = createFetch(async (url, request) => {
|
||||||
if (url.pathname === "/api/location") return json(location)
|
const directory = url.searchParams.get("location[directory]") ?? cwd
|
||||||
|
if (url.pathname === "/api/location") {
|
||||||
|
if (directory === sessionDirectory) {
|
||||||
|
locationRequested.resolve()
|
||||||
|
await targetLocation.promise
|
||||||
|
}
|
||||||
|
return json(location(directory))
|
||||||
|
}
|
||||||
if (url.pathname === "/api/session") return json({ data: [session], cursor: {} })
|
if (url.pathname === "/api/session") return json({ data: [session], cursor: {} })
|
||||||
if (url.pathname === "/api/session/dummy") return json({ data: session })
|
if (url.pathname === "/api/session/dummy") return json({ data: session })
|
||||||
if (url.pathname === "/api/session/dummy/message") return json({ data: [], cursor: {} })
|
if (url.pathname === "/api/session/dummy/message") return json({ data: [], cursor: {} })
|
||||||
|
if (url.pathname === "/api/session/dummy/message/msg_model_gamma")
|
||||||
|
return json({
|
||||||
|
data: {
|
||||||
|
id: "msg_model_gamma",
|
||||||
|
type: "model-switched",
|
||||||
|
previous: session.model,
|
||||||
|
model: { providerID: "provider", id: "gamma", variant: "medium" },
|
||||||
|
time: { created: 1 },
|
||||||
|
},
|
||||||
|
})
|
||||||
if (url.pathname === "/api/session/dummy/pending") return json({ data: [] })
|
if (url.pathname === "/api/session/dummy/pending") return json({ data: [] })
|
||||||
if (url.pathname === "/api/session/dummy/permission") return json({ data: [] })
|
if (url.pathname === "/api/session/dummy/permission") return json({ data: [] })
|
||||||
if (url.pathname === "/api/agent")
|
if (url.pathname === "/api/agent")
|
||||||
return json({
|
return json({
|
||||||
location,
|
location: location(directory),
|
||||||
data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }],
|
data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }],
|
||||||
})
|
})
|
||||||
if (url.pathname === "/api/model")
|
if (url.pathname === "/api/model") {
|
||||||
|
if (directory !== sessionDirectory)
|
||||||
|
return json({
|
||||||
|
location: location(directory),
|
||||||
|
data: [{ id: "fallback", providerID: "provider", name: "Fallback", variants: [] }],
|
||||||
|
})
|
||||||
|
catalogRequested.resolve()
|
||||||
|
await catalog.promise
|
||||||
return json({
|
return json({
|
||||||
location,
|
location: location(directory),
|
||||||
data: [{ id: "model", providerID: "provider", name: "Model", variants: [] }],
|
data: [
|
||||||
|
{ id: "model", providerID: "provider", name: "Model", variants: [{ id: "high" }] },
|
||||||
|
{ id: "fallback", providerID: "provider", name: "Fallback", variants: [] },
|
||||||
|
{ id: "gamma", providerID: "provider", name: "Gamma", variants: [{ id: "medium" }] },
|
||||||
|
],
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
if (url.pathname === "/api/session/dummy/model") {
|
||||||
|
modelSwitches.push(await request.json())
|
||||||
|
return new Response(null, { status: 204 })
|
||||||
|
}
|
||||||
if (url.pathname === "/api/session/dummy/prompt") {
|
if (url.pathname === "/api/session/dummy/prompt") {
|
||||||
bodies.push(await request.json())
|
bodies.push(await request.json())
|
||||||
promptSubmitted.resolve()
|
promptSubmitted.resolve()
|
||||||
|
if (bodies.length === 2) secondPromptSubmitted.resolve()
|
||||||
return json({ data: {} })
|
return json({ data: {} })
|
||||||
}
|
}
|
||||||
}, events)
|
}, events)
|
||||||
@@ -196,6 +237,22 @@ test("session startup prompt is submitted exactly once", async () => {
|
|||||||
}).pipe(Effect.provide(AppNodeBuilder.build(Global.node)), Effect.provide(FileSystem.layerNoop({}))),
|
}).pipe(Effect.provide(AppNodeBuilder.build(Global.node)), Effect.provide(FileSystem.layerNoop({}))),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
await locationRequested.promise
|
||||||
|
await Promise.race([
|
||||||
|
promptSubmitted.promise.then(() => {
|
||||||
|
throw new Error("startup prompt was submitted before its location loaded")
|
||||||
|
}),
|
||||||
|
Bun.sleep(100),
|
||||||
|
])
|
||||||
|
targetLocation.resolve()
|
||||||
|
await catalogRequested.promise
|
||||||
|
await Promise.race([
|
||||||
|
promptSubmitted.promise.then(() => {
|
||||||
|
throw new Error("startup prompt was submitted before its model catalog loaded")
|
||||||
|
}),
|
||||||
|
Bun.sleep(100),
|
||||||
|
])
|
||||||
|
catalog.resolve()
|
||||||
await Promise.race([
|
await Promise.race([
|
||||||
promptSubmitted.promise,
|
promptSubmitted.promise,
|
||||||
Bun.sleep(2000).then(() => {
|
Bun.sleep(2000).then(() => {
|
||||||
@@ -203,12 +260,38 @@ test("session startup prompt is submitted exactly once", async () => {
|
|||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
await Bun.sleep(20)
|
await Bun.sleep(20)
|
||||||
setup.renderer.destroy()
|
|
||||||
await task
|
|
||||||
|
|
||||||
expect(bodies).toHaveLength(1)
|
expect(bodies).toHaveLength(1)
|
||||||
expect(bodies[0]).toMatchObject({ text: "RESUME_READY" })
|
expect(bodies[0]).toMatchObject({ text: "RESUME_READY" })
|
||||||
|
expect(modelSwitches).toEqual([])
|
||||||
|
|
||||||
|
events.emit({
|
||||||
|
id: "evt_model_gamma",
|
||||||
|
created: 1,
|
||||||
|
type: "session.model.selected",
|
||||||
|
durable: { aggregateID: session.id, seq: 1, version: 1 },
|
||||||
|
location: session.location,
|
||||||
|
data: {
|
||||||
|
sessionID: session.id,
|
||||||
|
model: { providerID: "provider", id: "gamma", variant: "medium" },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
await Bun.sleep(20)
|
||||||
|
await setup.mockInput.typeText("SECOND")
|
||||||
|
setup.mockInput.pressEnter()
|
||||||
|
await Promise.race([
|
||||||
|
secondPromptSubmitted.promise,
|
||||||
|
Bun.sleep(2000).then(() => {
|
||||||
|
throw new Error("second prompt was not submitted")
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
expect(modelSwitches).toEqual([])
|
||||||
|
await Bun.sleep(20)
|
||||||
|
setup.renderer.destroy()
|
||||||
|
await task
|
||||||
} finally {
|
} finally {
|
||||||
|
targetLocation.resolve()
|
||||||
|
catalog.resolve()
|
||||||
if (!setup.renderer.isDestroyed) setup.renderer.destroy()
|
if (!setup.renderer.isDestroyed) setup.renderer.destroy()
|
||||||
await server.stop()
|
await server.stop()
|
||||||
mock.restore()
|
mock.restore()
|
||||||
|
|||||||
Reference in New Issue
Block a user