Skip to content

Commit 0996030

Browse files
authored
feat(app): show a free limit panel in place of the composer (#63531)
1 parent 5418e01 commit 0996030

5 files changed

Lines changed: 173 additions & 11 deletions

File tree

‎packages/app/e2e/regression/composer.spec.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ test("closing the @ menu aborts its session search", async ({ page }) => {
149149
}
150150
})
151151

152-
test("the free usage limit opens the Go dialog once, leaves a readable notice, and restores the prompt", async ({
152+
test("the free usage limit opens the Go dialog once, swaps the composer for the free limit panel, and restores the prompt", async ({
153153
page,
154154
context,
155155
}) => {
@@ -251,7 +251,9 @@ test("the free usage limit opens the Go dialog once, leaves a readable notice, a
251251
const upsell = page.getByRole("dialog", { name: "Free limit reached" })
252252
await expect(upsell).toContainText("Your free messages reset at")
253253
await expect(page.getByText(/^You've used today's free messages\. They reset at /)).toBeVisible()
254-
await expect(view.editor).toHaveText("Refactor the parser")
254+
// With no paid provider the panel stands in for the composer until Go is connected.
255+
await expect(page.locator('[data-component="session-free-limit"]')).toContainText("Free usage resets at")
256+
await expect(view.editor).toHaveCount(0)
255257
await expect(page.getByText("Free usage limit reached.")).toHaveCount(0)
256258

257259
// Subscribe -> Go API key -> Continue switches the model in the current session without opening a new draft tab.

‎packages/app/src/runtime/i18n/en.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ export const dict = {
636636
"session.background.moveRunning": "Move to background",
637637
"session.background.moveInline": "Press {{keybind}} to move running work to the background",
638638
"command.session.background": "Move to background",
639+
"session.freeLimit.title": "Free limit reached",
640+
"session.freeLimit.reset": "Free usage resets at {{time}}. Connect a provider or subscribe to Go to continue.",
641+
"session.freeLimit.noReset": "Connect a provider or subscribe to Go to continue.",
642+
"session.freeLimit.connect": "Connect provider",
643+
"session.freeLimit.subscribe": "Subscribe to Go · $10/mo",
639644
"session.goLimit.title": "Go usage limit reached.",
640645
"session.goLimit.titleNamed": "Go {{name}} usage limit reached.",
641646
"session.goLimit.reset": "It resets at {{time}}.",
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { createSignal, onCleanup, Show } from "solid-js"
2+
import { Schema } from "effect"
3+
import { Button } from "@opencode/ui/button"
4+
import { useDialog } from "@opencode/ui/context/dialog"
5+
import { useLanguage } from "@/runtime/i18n/language"
6+
import { usePlatform } from "@/runtime/platform/platform"
7+
import { Persist, persisted } from "@/runtime/persistence/storage"
8+
import { Persistence } from "@/runtime/persistence/schema"
9+
import { useWorkspaceLocation } from "@/workspaces/location"
10+
import type { ModelSelection } from "@/providers/models/selection"
11+
import { limitResetAt } from "../usage-exceeded-dialogs"
12+
13+
const GO_URL = "https://opencode.ai/go"
14+
15+
// OpenCode Zen always says when free usage resets; this covers a refusal that doesn't, or a delay out of range.
16+
const FALLBACK_RESET_MS = 3_600_000
17+
18+
const FreeLimitState = Persistence.struct({ resetAt: Schema.NullOr(Schema.Finite) })
19+
20+
// Free usage is counted per network, not per session, so one refusal anywhere holds until it resets. Connecting a paid
21+
// provider ends it early.
22+
export function useFreeLimit(paid: () => boolean) {
23+
const location = useWorkspaceLocation()
24+
const [state, setState] = persisted(Persist.global("free-limit"), FreeLimitState, { resetAt: null })
25+
const [now, setNow] = createSignal(Date.now())
26+
27+
onCleanup(
28+
location().event.on("session.step.failed", (event) => {
29+
if (event.data.error.type !== "provider.free-limit") return
30+
setState(
31+
"resetAt",
32+
limitResetAt(event.created, event.data.error.retryAfterMs) ?? event.created + FALLBACK_RESET_MS,
33+
)
34+
}),
35+
)
36+
37+
const resetAt = () => state.resetAt
38+
39+
// Re-check when the limit resets, so the composer comes back on its own.
40+
const timer = setInterval(() => setNow(Date.now()), 30_000)
41+
onCleanup(() => clearInterval(timer))
42+
43+
return {
44+
resetAt,
45+
active: () => {
46+
const at = resetAt()
47+
48+
return !paid() && at !== null && at > now()
49+
},
50+
}
51+
}
52+
53+
// Stands in for the composer while free usage is used up: with no paid provider there is nothing the composer can send.
54+
export function SessionFreeLimitPanel(props: { resetAt: number | null; selection: () => ModelSelection | undefined }) {
55+
const language = useLanguage()
56+
const platform = usePlatform()
57+
const dialog = useDialog()
58+
const location = useWorkspaceLocation()
59+
60+
const reset = () => {
61+
const at = props.resetAt
62+
63+
if (at === null) return
64+
const sameDay = new Date(at).toDateString() === new Date().toDateString()
65+
66+
return new Intl.DateTimeFormat(
67+
language.locale(),
68+
sameDay ? { timeStyle: "short" } : { dateStyle: "medium", timeStyle: "short" },
69+
).format(at)
70+
}
71+
72+
const connect = (provider?: string) => {
73+
void import("@/providers/connect/dialog").then((x) => {
74+
const controller = x.useProviderConnectController()
75+
76+
if (provider) controller.select(provider)
77+
void dialog.show(() => (
78+
<x.DialogConnectProvider
79+
controller={controller}
80+
directory={location().directory}
81+
selection={props.selection()}
82+
standalone={!!provider}
83+
/>
84+
))
85+
})
86+
}
87+
88+
const subscribe = () => {
89+
platform.openExternal(GO_URL)
90+
connect("opencode-go")
91+
}
92+
93+
return (
94+
<div
95+
data-component="session-free-limit"
96+
role="status"
97+
class="flex flex-col gap-4 rounded-xl bg-v2-background-bg-base p-2 [[data-color-scheme=dark]_&]:bg-v2-background-bg-layer-01 shadow-[var(--v2-elevation-raised)] [font-family:var(--v2-font-family-sans)]"
98+
>
99+
<div class="flex flex-col gap-0.5 ps-1.5 pe-1 pt-1">
100+
<div class="text-[13px] font-[530] leading-[var(--line-height-base)] text-v2-text-text-base">
101+
{language.t("session.freeLimit.title")}
102+
</div>
103+
<div class="text-[13px] font-[440] leading-[var(--line-height-base)] text-v2-text-text-muted [font-variant-numeric:tabular-nums]">
104+
<Show when={reset()} fallback={language.t("session.freeLimit.noReset")}>
105+
{(time) => language.t("session.freeLimit.reset", { time: time() })}
106+
</Show>
107+
</div>
108+
</div>
109+
<div class="flex items-center justify-end gap-1.5">
110+
<Button
111+
data-action="session-free-limit-subscribe"
112+
type="button"
113+
size="normal"
114+
variant="ghost"
115+
class="![font-weight:530]"
116+
onClick={subscribe}
117+
>
118+
{language.t("session.freeLimit.subscribe")}
119+
</Button>
120+
<Button
121+
data-action="session-free-limit-connect"
122+
type="button"
123+
size="normal"
124+
variant="submit"
125+
class="![font-weight:530]"
126+
onClick={() => connect()}
127+
>
128+
{language.t("session.freeLimit.connect")}
129+
</Button>
130+
</div>
131+
</div>
132+
)
133+
}

‎packages/app/src/session/composer/region.tsx‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { SessionComposerRegion } from "./session-composer-region"
3838
import { createSessionComposerController, type SessionComposerController } from "./controller"
3939
import { SessionQueuePanel } from "./queue-panel"
4040
import { SessionGoLimitTray } from "./go-limit-tray"
41+
import { SessionFreeLimitPanel, useFreeLimit } from "./free-limit-panel"
4142
import { SessionActivityChips, type SessionActivityItem } from "./activity-chips"
4243
import { SessionComposerLane, useComposerLaneAlignment } from "./composer-lane"
4344
import { SessionTodoPill } from "./todo-pill"
@@ -554,6 +555,8 @@ export function ActiveSessionComposerRegion(props: {
554555
return props.session.data.info()?.outcome
555556
}
556557

558+
const freeLimit = useFreeLimit(() => props.model.composer.model.paid)
559+
557560
return (
558561
<SessionComposerRegion
559562
controller={props.model.region}
@@ -606,14 +609,24 @@ export function ActiveSessionComposerRegion(props: {
606609
// The container for the composer's `cqw` caps: only the composer re-evaluates as a panel beside it
607610
// animates the chat's width.
608611
<div class="@container">
609-
<VoiceComposer session={props.session.data.info()}>
610-
<Composer
611-
model={props.model.composer}
612-
borderUnderlay
613-
readOnly={props.model.queue.undoing()}
614-
suggestionBoundary={props.suggestionBoundary}
615-
/>
616-
</VoiceComposer>
612+
<Show
613+
when={!freeLimit.active()}
614+
fallback={
615+
<SessionFreeLimitPanel
616+
resetAt={freeLimit.resetAt()}
617+
selection={() => props.model.composer.model.selection}
618+
/>
619+
}
620+
>
621+
<VoiceComposer session={props.session.data.info()}>
622+
<Composer
623+
model={props.model.composer}
624+
borderUnderlay
625+
readOnly={props.model.queue.undoing()}
626+
suggestionBoundary={props.suggestionBoundary}
627+
/>
628+
</VoiceComposer>
629+
</Show>
617630
</div>
618631
}
619632
>

‎packages/app/src/session/usage-exceeded-dialogs.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const MAX_RETRY_AFTER_MS = 45 * 86_400_000
2222

2323
const CHATGPT_USAGE_LIMIT_WINDOW = 86_400_000 // 24 hrs
2424

25-
export function formatLimitReset(locale: string, created: number, retryAfterMs: number | undefined) {
25+
// When a usage limit resets, or undefined when the refusal's delay is missing or out of range.
26+
export function limitResetAt(created: number, retryAfterMs: number | undefined) {
2627
if (
2728
retryAfterMs === undefined ||
2829
!Number.isFinite(retryAfterMs) ||
@@ -36,6 +37,14 @@ export function formatLimitReset(locale: string, created: number, retryAfterMs:
3637

3738
if (Number.isNaN(new Date(at).getTime())) return undefined
3839

40+
return at
41+
}
42+
43+
export function formatLimitReset(locale: string, created: number, retryAfterMs: number | undefined) {
44+
const at = limitResetAt(created, retryAfterMs)
45+
46+
if (at === undefined) return undefined
47+
3948
const now = Date.now()
4049
const sameDay = new Date(at).toDateString() === new Date(now).toDateString() && at - now < GO_UPSELL_WINDOW
4150

0 commit comments

Comments
 (0)