Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
568e675
feat(opencode): add custom block integration
Danigm-dev Mar 25, 2026
3146562
feat(opencode): add optional runtime overlay
Danigm-dev Mar 25, 2026
7014fa4
fix(opencode): harden external runtime contract
Danigm-dev Mar 25, 2026
33b834b
docs(opencode): add deployment checklists
Danigm-dev Mar 25, 2026
cacd46a
test(opencode): cover route contracts
Danigm-dev Mar 25, 2026
6160d1c
fix(opencode): address review feedback
Danigm-dev Mar 25, 2026
8ec0c5a
fix(opencode): harden runtime defaults
Danigm-dev Mar 25, 2026
d59f7a1
fix(opencode): narrow stale session retries
Danigm-dev Mar 25, 2026
1e174f7
fix(opencode): avoid redundant resolution and url leaks
Danigm-dev Mar 25, 2026
35fac8d
fix(opencode): clean up low severity review notes
Danigm-dev Mar 25, 2026
35949bb
fix(opencode): harden root path and retry errors
Danigm-dev Mar 25, 2026
3458868
refactor(opencode): keep base url helper private
Danigm-dev Mar 25, 2026
a27de0d
fix(editor): avoid stale open-change fetch gating
Danigm-dev Mar 25, 2026
a8fb073
fix(opencode): persist fresh retry sessions
Danigm-dev Mar 25, 2026
2bb744a
fix(opencode): tighten retry and entrypoint guards
Danigm-dev Mar 25, 2026
5ab2b5f
fix(editor): stabilize async option refetching
Danigm-dev Mar 25, 2026
d246b50
docs(opencode): add branch status summary
Danigm-dev Mar 25, 2026
f1156e9
docs(opencode): clarify branch scope and overlap
Danigm-dev Mar 25, 2026
cb57bad
docs(opencode): remove internal branch note from PR
Danigm-dev Mar 26, 2026
e4c40ae
fix opencode review follow-ups
Danigm-dev Mar 26, 2026
d543a9d
fix opencode async selector refresh
Danigm-dev Mar 26, 2026
59be14f
fix opencode async selector force default
Danigm-dev Mar 26, 2026
0ca5e24
clean up opencode async selector refs
Danigm-dev Mar 26, 2026
d77d875
guard opencode async selector stale fetches
Danigm-dev Mar 26, 2026
02bcfa0
Merge branch 'staging' into feat/opencode-optional-runtime
Danigm-dev Mar 26, 2026
e77557c
fix opencode selector stale reload and lint
Danigm-dev Mar 26, 2026
4ab5c68
fix opencode docker script permissions
Danigm-dev Mar 26, 2026
314e410
fix opencode client reuse and selector refetch
Danigm-dev Mar 26, 2026
780311e
fix opencode connectivity error classification
Danigm-dev Mar 26, 2026
7fc5621
fix opencode client refresh and selector loading
Danigm-dev Mar 26, 2026
2b76ffa
fix opencode client cleanup and prompt schema naming
Danigm-dev Mar 26, 2026
13f4b56
fix opencode client key helper reuse
Danigm-dev Mar 26, 2026
9bb48d0
Merge branch 'staging' into feat/opencode-optional-runtime
Danigm-dev Apr 1, 2026
76f36ea
Merge branch 'staging' into feat/opencode-optional-runtime
Danigm-dev Apr 7, 2026
c0cd00a
fix: avoid forced async selector refetch on open
Danigm-dev Apr 7, 2026
f791d66
fix: align async selector fetch signatures
Danigm-dev Apr 7, 2026
98cae4c
fix: pass subblock id to option hydration
Danigm-dev Apr 7, 2026
ae3b633
Merge branch 'staging' into feat/opencode-optional-runtime
Danigm-dev Apr 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix opencode client reuse and selector refetch
  • Loading branch information
Danigm-dev committed Mar 26, 2026
commit 314e41011a482715cabee825bec2fadf7f3fe312
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const ComboBox = memo(function ComboBox({
const optionsFetchVersionRef = useRef(0)
const isOptionsFetchInFlightRef = useRef(false)
const hasAttemptedOptionsFetchRef = useRef(false)
const fetchOptionsIfNeededRef = useRef<((force?: boolean) => Promise<void>) | null>(null)

/**
* Fetches options from the async fetchOptions function if provided
Expand Down Expand Up @@ -171,7 +172,7 @@ export const ComboBox = memo(function ComboBox({
isOptionsFetchInFlightRef.current = false

if (shouldTriggerReplacementFetch) {
void fetchOptionsIfNeeded(true)
void fetchOptionsIfNeededRef.current?.(true)
} else {
setIsLoadingOptions(false)
}
Expand All @@ -180,6 +181,10 @@ export const ComboBox = memo(function ComboBox({
[fetchOptions, blockId, subBlockId, isPreview, disabled]
)
Comment thread
Danigm-dev marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

useEffect(() => {
fetchOptionsIfNeededRef.current = fetchOptionsIfNeeded
}, [fetchOptionsIfNeeded])

// Determine the active value based on mode (preview vs. controlled vs. store)
const value = isPreview ? previewValue : propValue !== undefined ? propValue : storeValue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const Dropdown = memo(function Dropdown({
const optionsFetchVersionRef = useRef(0)
const isOptionsFetchInFlightRef = useRef(false)
const hasAttemptedOptionsFetchRef = useRef(false)
const fetchOptionsIfNeededRef = useRef<((force?: boolean) => Promise<void>) | null>(null)

const [builderData, setBuilderData] = useSubBlockValue<any[]>(blockId, 'builderData')
const [data, setData] = useSubBlockValue<string>(blockId, 'data')
Expand Down Expand Up @@ -196,7 +197,7 @@ export const Dropdown = memo(function Dropdown({
isOptionsFetchInFlightRef.current = false

if (shouldTriggerReplacementFetch) {
void fetchOptionsIfNeeded(true)
void fetchOptionsIfNeededRef.current?.(true)
} else {
setIsLoadingOptions(false)
}
Expand All @@ -205,6 +206,10 @@ export const Dropdown = memo(function Dropdown({
[fetchOptions, blockId, subBlockId, isPreview, disabled]
)

useEffect(() => {
fetchOptionsIfNeededRef.current = fetchOptionsIfNeeded
}, [fetchOptionsIfNeeded])

/**
* Handles combobox open state changes to trigger option fetching
*/
Expand Down
52 changes: 52 additions & 0 deletions apps/sim/lib/opencode/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @vitest-environment node
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

const { mockCreateOpencodeClient } = vi.hoisted(() => ({
mockCreateOpencodeClient: vi.fn(),
}))

vi.mock('@opencode-ai/sdk', () => ({
createOpencodeClient: mockCreateOpencodeClient,
}))

import { createOpenCodeClient, resetOpenCodeClientForTesting } from '@/lib/opencode/client'

describe('createOpenCodeClient', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubEnv('OPENCODE_BASE_URL', 'http://localhost:4096')
vi.stubEnv('OPENCODE_SERVER_USERNAME', 'opencode')
vi.stubEnv('OPENCODE_SERVER_PASSWORD', 'password')
mockCreateOpencodeClient.mockReturnValue({ session: {} })
resetOpenCodeClientForTesting()
})

afterEach(() => {
vi.unstubAllEnvs()
resetOpenCodeClientForTesting()
})

it('reuses the same client instance across calls', () => {
const firstClient = createOpenCodeClient()
const secondClient = createOpenCodeClient()

expect(firstClient).toBe(secondClient)
expect(mockCreateOpencodeClient).toHaveBeenCalledTimes(1)
})

it('recreates the client after resetting the cache', () => {
const firstClient = { session: { id: 'first' } }
const secondClient = { session: { id: 'second' } }
mockCreateOpencodeClient.mockReturnValueOnce(firstClient).mockReturnValueOnce(secondClient)

const initialClient = createOpenCodeClient()
resetOpenCodeClientForTesting()
const recreatedClient = createOpenCodeClient()

expect(initialClient).toBe(firstClient)
expect(recreatedClient).toBe(secondClient)
expect(mockCreateOpencodeClient).toHaveBeenCalledTimes(2)
})
})
21 changes: 15 additions & 6 deletions apps/sim/lib/opencode/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const OPEN_CODE_HOST = 'opencode'
const OPEN_CODE_LOCALHOST = '127.0.0.1'
const OPEN_CODE_DEFAULT_PORT = '4096'
const IS_DOCKER_RUNTIME = existsSync('/.dockerenv')
let cachedOpenCodeClient: ReturnType<typeof createOpencodeClient> | null = null

function getOpenCodeBasicAuthHeader(): string {
const username = process.env.OPENCODE_SERVER_USERNAME
Expand All @@ -30,10 +31,18 @@ function getOpenCodeBaseUrl(): string {
}
Comment thread
Danigm-dev marked this conversation as resolved.

export function createOpenCodeClient() {
return createOpencodeClient({
baseUrl: getOpenCodeBaseUrl(),
headers: {
Authorization: getOpenCodeBasicAuthHeader(),
},
})
if (!cachedOpenCodeClient) {
cachedOpenCodeClient = createOpencodeClient({
baseUrl: getOpenCodeBaseUrl(),
headers: {
Authorization: getOpenCodeBasicAuthHeader(),
},
})
}

return cachedOpenCodeClient
}
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

export function resetOpenCodeClientForTesting(): void {
cachedOpenCodeClient = null
}
Loading