|
| 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 | +} |
0 commit comments