MCPcopy Create free account
hub / github.com/anomalyco/opencode / createWorkspaceTerminalSession

Function createWorkspaceTerminalSession

packages/app/src/context/terminal.tsx:146–371  ·  view source on GitHub ↗
(
  sdk: DirectorySDK,
  dir: string,
  scope: ServerScopeValue,
  legacySessionID?: string,
)

Source from the content-addressed store, hash-verified

144}
145
146function createWorkspaceTerminalSession(
147 sdk: DirectorySDK,
148 dir: string,
149 scope: ServerScopeValue,
150 legacySessionID?: string,
151) {
152 const legacy = scope === ServerScope.local ? getLegacyTerminalStorageKeys(dir, legacySessionID) : []
153
154 const [store, setStore, _, ready] = persisted(
155 {
156 ...terminalPersistTarget(scope, dir, legacy),
157 migrate: migrateTerminalState,
158 },
159 createStore<{
160 active?: string
161 all: LocalPTY[]
162 }>({
163 all: [],
164 }),
165 )
166
167 const pickNextTerminalNumber = () => {
168 const existingTitleNumbers = new Set(
169 store.all.flatMap((pty) => {
170 const direct = Number.isFinite(pty.titleNumber) && pty.titleNumber > 0 ? pty.titleNumber : undefined
171 if (direct !== undefined) return [direct]
172 const parsed = numberFromTitle(pty.title)
173 if (parsed === undefined) return []
174 return [parsed]
175 }),
176 )
177
178 return (
179 Array.from({ length: existingTitleNumbers.size + 1 }, (_, index) => index + 1).find(
180 (number) => !existingTitleNumbers.has(number),
181 ) ?? 1
182 )
183 }
184
185 const removeExited = (id: string) => {
186 const all = store.all
187 const index = all.findIndex((x) => x.id === id)
188 if (index === -1) return
189 const active = store.active === id ? (index === 0 ? all[1]?.id : all[0]?.id) : store.active
190 batch(() => {
191 setStore("active", active)
192 setStore(
193 "all",
194 produce((draft) => {
195 draft.splice(index, 1)
196 }),
197 )
198 })
199 }
200
201 const unsub = sdk.event.on("pty.exited", (event: { properties: { id: string } }) => {
202 removeExited(event.properties.id)
203 })

Callers 1

loadWorkspaceFunction · 0.85

Calls 5

persistedFunction · 0.90
terminalPersistTargetFunction · 0.85
removeExitedFunction · 0.85
onMethod · 0.80

Tested by

no test coverage detected