Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/client/app/app.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<script setup lang="ts">
import { onBeforeUnmount, onMounted } from 'vue'
import { useActiveTurnWakeLock } from './composables/useActiveTurnWakeLock'
import { useHasActiveChatTurn } from './composables/useChatSession'

let visualViewportRef: VisualViewport | null = null
const hasActiveChatTurn = useHasActiveChatTurn()
useActiveTurnWakeLock(hasActiveChatTurn)

const setViewportHeightCssVar = () => {
if (!import.meta.client) {
Expand Down
76 changes: 76 additions & 0 deletions packages/client/app/composables/useActiveTurnWakeLock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { useWakeLock, type UseWakeLockReturn } from '@vueuse/core'
import { onScopeDispose, watch, type Ref } from 'vue'

export type ActiveTurnWakeLock = Pick<
UseWakeLockReturn,
'isSupported' | 'isActive' | 'request' | 'release'
>

export const useActiveTurnWakeLock = (
hasActiveTurn: Readonly<Ref<boolean>>,
createWakeLock: () => ActiveTurnWakeLock = useWakeLock
) => {
const wakeLock = createWakeLock()
let shouldHoldWakeLock = false
let wakeLockRequested = false
let syncOperation = Promise.resolve()

const releaseWakeLock = async () => {
try {
await wakeLock.release()
} catch {
// A normal turn must continue even when the browser denies wake-lock cleanup.
}
}

const syncWakeLock = async () => {
if (!shouldHoldWakeLock) {
if (!wakeLockRequested && !wakeLock.isActive.value) {
return
}
wakeLockRequested = false
await releaseWakeLock()
return
}

if (!wakeLock.isSupported.value) {
return
}

if (wakeLockRequested || wakeLock.isActive.value) {
return
}

wakeLockRequested = true
try {
await wakeLock.request('screen')
Comment thread
comfuture marked this conversation as resolved.
} catch {
wakeLockRequested = false
return
}

if (!shouldHoldWakeLock) {
wakeLockRequested = false
await releaseWakeLock()
}
}

const queueWakeLockSync = () => {
syncOperation = syncOperation.then(syncWakeLock, syncWakeLock)
}

watch(hasActiveTurn, shouldHold => {
shouldHoldWakeLock = shouldHold
queueWakeLockSync()
}, { immediate: true })
Comment thread
comfuture marked this conversation as resolved.

onScopeDispose(() => {
shouldHoldWakeLock = false
queueWakeLockSync()
})

return {
isSupported: wakeLock.isSupported,
isActive: wakeLock.isActive
}
}
11 changes: 10 additions & 1 deletion packages/client/app/composables/useChatSession.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref, type Ref } from 'vue'
import { computed, ref, shallowRef, type Ref } from 'vue'
import type { ChatMessage, SubagentAgentStatus, VisualSubagentPanel } from '~~/shared/codex-chat'
import type { CollaborationModeMask } from '~~/shared/collaboration-mode'
import type { ReasoningEffort } from '~~/shared/generated/codex-app-server/ReasoningEffort'
Expand Down Expand Up @@ -73,6 +73,14 @@ export type ChatSession = {
}

const sessions = new Map<string, ChatSession>()
const registeredSessions = shallowRef<ChatSession[]>([])

export const isChatTurnActive = (status: ChatStatus) =>
status === 'submitted' || status === 'streaming'

export const useHasActiveChatTurn = () => computed(() =>
registeredSessions.value.some(session => isChatTurnActive(session.status.value))
)

const createSession = (): ChatSession => {
const session: ChatSession = {
Expand Down Expand Up @@ -124,6 +132,7 @@ export const useChatSession = (projectId: string) => {

const session = createSession()
sessions.set(projectId, session)
registeredSessions.value = [...registeredSessions.value, session]
return session
}

Expand Down
12 changes: 7 additions & 5 deletions packages/client/app/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ const settingsRoute = computed(() => ({
class="flex min-w-0 items-center gap-3 rounded-xl outline-none transition hover:opacity-80 focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-2 focus-visible:ring-offset-default"
:class="collapsed ? '' : 'flex-1'"
>
<span class="flex size-9 shrink-0 items-center justify-center rounded-xl bg-primary/12 text-primary">
<UIcon
name="i-lucide-terminal-square"
class="size-5"
/>
<span class="flex size-9 shrink-0 items-center justify-center">
<img
src="/icons/codori-192.png"
alt=""
data-testid="sidebar-brand-icon"
class="size-9 rounded-xl"
>
</span>
<span
v-if="!collapsed"
Expand Down
32 changes: 30 additions & 2 deletions packages/client/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import type { ModuleOptions as PwaModuleOptions } from '@vite-pwa/nuxt'
import type { NuxtConfig } from 'nuxt/schema'

const config: NuxtConfig = {
modules: ['@nuxt/ui'],
const config: NuxtConfig & { pwa: PwaModuleOptions } = {
modules: ['@nuxt/ui', '@vite-pwa/nuxt'],
css: ['~/assets/css/main.css'],
ssr: false,
compatibilityDate: '2025-01-15',
app: {
head: {
title: 'Codori',
meta: [
{ name: 'theme-color', content: '#111827' }
],
link: [
{ rel: 'manifest', href: '/manifest.webmanifest' },
{ rel: 'apple-touch-icon', href: '/icons/codori-192.png' }
]
}
},
pwa: {
manifest: false,
registerType: 'autoUpdate',
injectRegister: 'inline',
workbox: {
globPatterns: ['**/*.{js,css,html,png,svg,ico,woff2}'],
maximumFileSizeToCacheInBytes: 4 * 1024 * 1024,
navigateFallback: '/index.html',
navigateFallbackDenylist: [/^\/api\//, /^\/xr(?:\/|$)/],
runtimeCaching: [],
cleanupOutdatedCaches: true,
clientsClaim: true,
skipWaiting: true
}
},
runtimeConfig: {
public: {
serverBase: process.env.CODORI_SERVER_BASE ?? '',
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@iconify-json/hugeicons": "^1.2.32",
"@iconify-json/lucide": "^1.2.122",
"@nuxt/ui": "^4.10.0",
"@vite-pwa/nuxt": "1.1.1",
"@vueuse/core": "14.4.0",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-web-fonts": "0.1.0",
Expand Down
Binary file added packages/client/public/icons/codori-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/client/public/icons/codori-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
199 changes: 199 additions & 0 deletions packages/client/test/active-turn-wake-lock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { computed, effectScope, nextTick, ref } from 'vue'
import { describe, expect, it, vi } from 'vitest'
import {
useActiveTurnWakeLock,
type ActiveTurnWakeLock
} from '../app/composables/useActiveTurnWakeLock'
import { useChatSession, useHasActiveChatTurn } from '../app/composables/useChatSession'

const deferred = () => {
let resolve!: () => void
const promise = new Promise<void>((resolvePromise) => {
resolve = resolvePromise
})
return { promise, resolve }
}

const createFixture = (input?: {
active?: boolean
supported?: boolean
request?: () => Promise<void>
}) => {
const active = ref(input?.active ?? false)
const request = vi.fn(input?.request ?? (async () => {
active.value = true
}))
const release = vi.fn(async () => {
active.value = false
})
const wakeLock: ActiveTurnWakeLock = {
isSupported: computed(() => input?.supported ?? true),
isActive: computed(() => active.value),
request,
release
}

return {
active,
request,
release,
createWakeLock: () => wakeLock
}
}

let sessionCounter = 0
const createSession = () => useChatSession(`wake-lock-test:${sessionCounter += 1}`)

describe('active turn wake lock', () => {
it('aggregates submitted and streaming turns across normal sessions', async () => {
const first = createSession()
const second = createSession()
const hasActiveTurn = useHasActiveChatTurn()

expect(hasActiveTurn.value).toBe(false)
first.status.value = 'submitted'
await nextTick()
expect(hasActiveTurn.value).toBe(true)

first.status.value = 'streaming'
second.status.value = 'submitted'
await nextTick()
expect(hasActiveTurn.value).toBe(true)

first.status.value = 'ready'
await nextTick()
expect(hasActiveTurn.value).toBe(true)

second.status.value = 'error'
await nextTick()
expect(hasActiveTurn.value).toBe(false)
})

it('holds one lock until the final active turn ends', async () => {
const hasActiveTurn = ref(false)
const fixture = createFixture()
const scope = effectScope()
scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock))

hasActiveTurn.value = true
await nextTick()
await vi.waitFor(() => {
expect(fixture.request).toHaveBeenCalledWith('screen')
})

hasActiveTurn.value = true
await nextTick()
expect(fixture.request).toHaveBeenCalledTimes(1)

hasActiveTurn.value = false
await nextTick()
await vi.waitFor(() => {
expect(fixture.release).toHaveBeenCalledTimes(1)
})

scope.stop()
})

it('does not request unsupported locks or surface browser failures', async () => {
const hasActiveTurn = ref(true)
const unsupported = createFixture({ supported: false })
const unsupportedScope = effectScope()
unsupportedScope.run(() =>
useActiveTurnWakeLock(hasActiveTurn, unsupported.createWakeLock)
)
await nextTick()
expect(unsupported.request).not.toHaveBeenCalled()
unsupportedScope.stop()

const failing = createFixture({
request: async () => {
throw new Error('Wake lock denied')
}
})
const failingScope = effectScope()
failingScope.run(() => useActiveTurnWakeLock(hasActiveTurn, failing.createWakeLock))
await nextTick()
await vi.waitFor(() => {
expect(failing.request).toHaveBeenCalledWith('screen')
})
failingScope.stop()
})

it('releases a late request after the final turn already ended', async () => {
const hasActiveTurn = ref(false)
const pendingRequest = deferred()
const fixture = createFixture({ request: () => pendingRequest.promise })
const scope = effectScope()
scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock))

hasActiveTurn.value = true
await nextTick()
await vi.waitFor(() => {
expect(fixture.request).toHaveBeenCalledTimes(1)
})
hasActiveTurn.value = false
await nextTick()
expect(fixture.release).not.toHaveBeenCalled()

pendingRequest.resolve()
await pendingRequest.promise
await vi.waitFor(() => {
expect(fixture.release).toHaveBeenCalledTimes(1)
})

scope.stop()
})

it('coalesces a stop and restart behind an older pending request', async () => {
const hasActiveTurn = ref(false)
const firstRequest = deferred()
const fixture = createFixture({ request: () => firstRequest.promise })
const scope = effectScope()
scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock))

hasActiveTurn.value = true
await nextTick()
await vi.waitFor(() => {
expect(fixture.request).toHaveBeenCalledTimes(1)
})
hasActiveTurn.value = false
await nextTick()
hasActiveTurn.value = true
await nextTick()

expect(fixture.request).toHaveBeenCalledTimes(1)
expect(fixture.release).not.toHaveBeenCalled()

fixture.active.value = true
firstRequest.resolve()
await firstRequest.promise
await vi.waitFor(() => {
expect(fixture.active.value).toBe(true)
})
expect(fixture.request).toHaveBeenCalledTimes(1)
expect(fixture.release).not.toHaveBeenCalled()

hasActiveTurn.value = false
await nextTick()
await vi.waitFor(() => {
expect(fixture.release).toHaveBeenCalledTimes(1)
})

scope.stop()
})

it('releases an active lock when the app-level scope is disposed', async () => {
const hasActiveTurn = ref(true)
const fixture = createFixture()
const scope = effectScope()
scope.run(() => useActiveTurnWakeLock(hasActiveTurn, fixture.createWakeLock))
await vi.waitFor(() => {
expect(fixture.request).toHaveBeenCalledTimes(1)
})

scope.stop()
await vi.waitFor(() => {
expect(fixture.release).toHaveBeenCalledTimes(1)
})
})
})
Loading