Compare commits
3 Commits
7aa1746f97
...
ai-model-s
| Author | SHA1 | Date | |
|---|---|---|---|
| 54f108ac8d | |||
| 865ef0b316 | |||
| 63b0405a7a |
3
bun.lock
3
bun.lock
@@ -93,6 +93,7 @@
|
|||||||
"type-fest": "^5.7.0",
|
"type-fest": "^5.7.0",
|
||||||
"uploadthing": "^7.7.4",
|
"uploadthing": "^7.7.4",
|
||||||
"vaul": "^1.1.2",
|
"vaul": "^1.1.2",
|
||||||
|
"wavesurfer.js": "^7.12.8",
|
||||||
"zod": "^4.4.3",
|
"zod": "^4.4.3",
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -2434,6 +2435,8 @@
|
|||||||
|
|
||||||
"walker": ["walker@1.0.8", "", { "dependencies": { "makeerror": "1.0.12" } }, "sha512-ts/8E8l5b7kY0vlWLewOkDXMmPdLcVV4GmOQLyxuSswIJsweeFZtAsMF7k1Nszz+TYBQrlYRmzOnr398y1JemQ=="],
|
"walker": ["walker@1.0.8", "", { "dependencies": { "makeerror": "1.0.12" } }, "sha512-ts/8E8l5b7kY0vlWLewOkDXMmPdLcVV4GmOQLyxuSswIJsweeFZtAsMF7k1Nszz+TYBQrlYRmzOnr398y1JemQ=="],
|
||||||
|
|
||||||
|
"wavesurfer.js": ["wavesurfer.js@7.12.8", "", {}, "sha512-G3nxzcC4X+ZWrLtcIV17kCWHVq3ysJCS4dS0YkGKILrQ2esAb8cScw965zKNKYxUvpiZsPK93KLWgWTYdIBQiw=="],
|
||||||
|
|
||||||
"web-namespaces": ["web-namespaces@2.0.1", "", {}, "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ=="],
|
"web-namespaces": ["web-namespaces@2.0.1", "", {}, "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ=="],
|
||||||
|
|
||||||
"web-streams-polyfill": ["web-streams-polyfill@3.3.3", "", {}, "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw=="],
|
"web-streams-polyfill": ["web-streams-polyfill@3.3.3", "", {}, "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw=="],
|
||||||
|
|||||||
@@ -108,6 +108,7 @@
|
|||||||
"type-fest": "^5.7.0",
|
"type-fest": "^5.7.0",
|
||||||
"uploadthing": "^7.7.4",
|
"uploadthing": "^7.7.4",
|
||||||
"vaul": "^1.1.2",
|
"vaul": "^1.1.2",
|
||||||
|
"wavesurfer.js": "^7.12.8",
|
||||||
"zod": "^4.4.3"
|
"zod": "^4.4.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,62 +1,43 @@
|
|||||||
import { useGSAP } from "@gsap/react";
|
|
||||||
import { useRef, type HTMLAttributes, type ReactNode } from "react";
|
import { useRef, type HTMLAttributes, type ReactNode } from "react";
|
||||||
import { useGsapContext } from "~/app/_providers/GsapProvicer";
|
|
||||||
import { SplitText } from "gsap/SplitText";
|
import { SplitText } from "gsap/SplitText";
|
||||||
import gsap from 'gsap'
|
import gsap from 'gsap'
|
||||||
import { cn } from "~/lib/utils";
|
import { cn } from "~/lib/utils";
|
||||||
|
import { useReveal } from "./useReveal";
|
||||||
|
|
||||||
const AnimateTextIn = ({
|
const AnimateTextIn = ({
|
||||||
children,
|
children,
|
||||||
animation = "type",
|
animation = "type",
|
||||||
position = 0,
|
position = 0,
|
||||||
tlId = undefined,
|
|
||||||
speed = 1,
|
speed = 1,
|
||||||
scrollOnly = false,
|
scrollOnly = false,
|
||||||
|
once = false,
|
||||||
className
|
className
|
||||||
}: {
|
}: {
|
||||||
children: ReactNode,
|
children: ReactNode,
|
||||||
animation?: "type" | "slide",
|
animation?: "type" | "slide",
|
||||||
position?: gsap.Position,
|
position?: gsap.Position,
|
||||||
tlId?: string,
|
|
||||||
scrollOnly?: boolean,
|
scrollOnly?: boolean,
|
||||||
|
once?: boolean,
|
||||||
speed?: number,
|
speed?: number,
|
||||||
className?: HTMLAttributes<HTMLDivElement>['className']
|
className?: HTMLAttributes<HTMLDivElement>['className']
|
||||||
}) => {
|
}) => {
|
||||||
const el = useRef<HTMLDivElement>(null)
|
const el = useRef<HTMLDivElement>(null)
|
||||||
const gsapContext = useGsapContext();
|
useReveal(el, {
|
||||||
useGSAP(() => {
|
position,
|
||||||
const rect = el.current?.getBoundingClientRect()
|
scrollOnly,
|
||||||
const scroller = gsapContext?.getScroller()
|
once,
|
||||||
console.log(scroller)
|
debugId: `text-${position}`,
|
||||||
let viewportTop = 0
|
makeReveal: (node) => {
|
||||||
let viewportBottom = window.innerHeight
|
// The wrapper starts at opacity 0 (so there's no flash of unsplit text);
|
||||||
if (scroller && scroller instanceof Element) {
|
// reveal the wrapper and let the per-character tween do the animation.
|
||||||
const scrollerRect = scroller.getBoundingClientRect()
|
gsap.set(node, { opacity: 1 })
|
||||||
viewportTop = scrollerRect.top
|
const split = new SplitText(node, { type: 'chars' })
|
||||||
viewportBottom = scrollerRect.top + scrollerRect.height
|
|
||||||
}
|
|
||||||
const isInView = rect && rect.bottom > viewportTop && rect.top < viewportBottom
|
|
||||||
console.log(isInView)
|
|
||||||
const chars = new SplitText(el.current, { type: 'chars' })
|
|
||||||
gsapContext?.addAnimation(gsap.to(el.current, { opacity: 100, duration: 0 }), 0, tlId)
|
|
||||||
const fromVars = animation === "slide"
|
const fromVars = animation === "slide"
|
||||||
? { opacity: 0, x: -10, duration: 0.2 * speed, stagger: { each: 0.08 * speed }, ease: 'bounce.inOut', onComplete: () => chars.revert() }
|
? { opacity: 0, x: -10, duration: 0.2 * speed, stagger: { each: 0.08 * speed }, ease: 'bounce.inOut' }
|
||||||
: { opacity: 0, duration: 0.01 * speed, stagger: { each: 0.04 * speed }, ease: 'bounce.inOut', onComplete: () => chars.revert() }
|
: { opacity: 0, duration: 0.01 * speed, stagger: { each: 0.04 * speed }, ease: 'bounce.inOut' }
|
||||||
if (isInView && !scrollOnly) {
|
return gsap.from(split.chars, { ...fromVars, paused: true })
|
||||||
gsapContext?.addAnimation(gsap.from(chars.chars, fromVars), position, tlId)
|
},
|
||||||
} else {
|
|
||||||
gsap.from(chars.chars,
|
|
||||||
{
|
|
||||||
...fromVars,
|
|
||||||
scrollTrigger: {
|
|
||||||
trigger: el.current,
|
|
||||||
start: 'top bottom',
|
|
||||||
end: 'bottom top',
|
|
||||||
toggleActions: "play reverse play reverse",
|
|
||||||
scroller
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}, { dependencies: [] })
|
|
||||||
return (
|
return (
|
||||||
<div ref={el} className={cn(className, "opacity-0")}>
|
<div ref={el} className={cn(className, "opacity-0")}>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -6,16 +6,20 @@ const AnimatePopUp = ({
|
|||||||
position,
|
position,
|
||||||
className,
|
className,
|
||||||
duration=1,
|
duration=1,
|
||||||
ease='elastic'
|
ease='elastic',
|
||||||
|
scrollOnly=false,
|
||||||
|
once=false,
|
||||||
}:{
|
}:{
|
||||||
children:ReactNode
|
children:ReactNode
|
||||||
position:gsap.Position,
|
position:gsap.Position,
|
||||||
className?:HTMLAttributes<HTMLDivElement>['className']
|
className?:HTMLAttributes<HTMLDivElement>['className']
|
||||||
duration?:number,
|
duration?:number,
|
||||||
ease?:gsap.EaseString|gsap.EaseFunction
|
ease?:gsap.EaseString|gsap.EaseFunction,
|
||||||
|
scrollOnly?:boolean,
|
||||||
|
once?:boolean,
|
||||||
}) => {
|
}) => {
|
||||||
return (
|
return (
|
||||||
<AnimatedDiv children={children} position={position} className={cn(className,'h-0 translate-y-[50] overflow-hidden')} height='auto' y={0} overflow='' ease={ease} duration={duration} />
|
<AnimatedDiv children={children} position={position} scrollOnly={scrollOnly} once={once} className={cn(className,'h-0 translate-y-[50] overflow-hidden')} height='auto' y={0} overflow='' ease={ease} duration={duration} />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,39 @@
|
|||||||
import gsap from "gsap";
|
import gsap from "gsap";
|
||||||
import { type HTMLAttributes,
|
import { type HTMLAttributes, type ReactNode, useRef } from "react";
|
||||||
type ReactNode, useLayoutEffect, useRef } from "react";
|
import { useReveal } from "./useReveal";
|
||||||
import { useGsapContext } from "~/app/_providers/GsapProvicer";
|
|
||||||
const AnimatedDiv = (
|
const AnimatedDiv = (
|
||||||
{
|
{
|
||||||
children,
|
children,
|
||||||
position,
|
position,
|
||||||
className,
|
className,
|
||||||
animationMode = 'to',
|
animationMode = 'to',
|
||||||
|
scrollOnly = false,
|
||||||
|
once = false,
|
||||||
|
debugId,
|
||||||
...tweenVars
|
...tweenVars
|
||||||
}:
|
}:
|
||||||
gsap.TweenVars & {
|
gsap.TweenVars & {
|
||||||
children: ReactNode,
|
children: ReactNode,
|
||||||
position: gsap.Position,
|
position: gsap.Position,
|
||||||
animationMode?: 'from' | 'to',
|
animationMode?: 'from' | 'to',
|
||||||
|
scrollOnly?: boolean,
|
||||||
|
once?: boolean,
|
||||||
|
debugId?: string,
|
||||||
className?: HTMLAttributes<HTMLDivElement>['className']
|
className?: HTMLAttributes<HTMLDivElement>['className']
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
const div = useRef<HTMLDivElement>(null);
|
const div = useRef<HTMLDivElement>(null)
|
||||||
const gsapContext = useGsapContext()
|
useReveal(div, {
|
||||||
useLayoutEffect(() => {
|
position,
|
||||||
let tween:gsap.core.Tween;
|
scrollOnly,
|
||||||
switch(animationMode) {
|
once,
|
||||||
case 'from':
|
debugId,
|
||||||
tween = gsap.from(div.current,tweenVars);
|
makeReveal: (el) =>
|
||||||
break;
|
animationMode === 'from'
|
||||||
case 'to':
|
? gsap.from(el, { ...tweenVars, paused: true })
|
||||||
tween = gsap.to(div.current,tweenVars);
|
: gsap.to(el, { ...tweenVars, paused: true }),
|
||||||
break;
|
})
|
||||||
}
|
|
||||||
gsapContext?.addAnimation(tween,position)
|
|
||||||
},[])
|
|
||||||
return (
|
return (
|
||||||
<div ref={div} className={className}>
|
<div ref={div} className={className}>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
102
src/app/_components/Animated/useReveal.ts
Normal file
102
src/app/_components/Animated/useReveal.ts
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useGSAP } from "@gsap/react"
|
||||||
|
import { ScrollTrigger } from "gsap/all"
|
||||||
|
import type { RefObject } from "react"
|
||||||
|
import { GSAP_DEBUG, useGsapContext } from "~/app/_providers/GsapProvicer"
|
||||||
|
|
||||||
|
export type UseRevealOptions = {
|
||||||
|
position: gsap.Position
|
||||||
|
/** Skip the orchestrated entrance and let ScrollTrigger drive from the start. */
|
||||||
|
scrollOnly?: boolean
|
||||||
|
/**
|
||||||
|
* Reveal once and keep it: after the element animates in (entrance or first
|
||||||
|
* scroll-in) it never reverses on leave. Default false = animate out at the
|
||||||
|
* top and back in on scroll-up.
|
||||||
|
*/
|
||||||
|
once?: boolean
|
||||||
|
debugId?: string
|
||||||
|
/**
|
||||||
|
* Build the hidden -> shown animation for `el`. It must be a single,
|
||||||
|
* *independent* animation (not added to any timeline): `play()` reveals,
|
||||||
|
* `reverse()` hides. The hook pauses it, schedules its entrance through the
|
||||||
|
* shared timeline, and lets a ScrollTrigger drive the very same animation on
|
||||||
|
* scroll — so the two modes never fight over the element.
|
||||||
|
*/
|
||||||
|
makeReveal: (el: HTMLElement) => gsap.core.Tween | gsap.core.Timeline
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared reveal behavior for cards, text and pop-ups: an element in view at
|
||||||
|
* load plays an orchestrated timeline entrance, then hands the *same* tween to
|
||||||
|
* a ScrollTrigger that animates it out at the top and back in on scroll-up. An
|
||||||
|
* element off-screen at load is ScrollTrigger-driven from the start.
|
||||||
|
*/
|
||||||
|
export function useReveal(
|
||||||
|
ref: RefObject<HTMLElement | null>,
|
||||||
|
{ position, scrollOnly = false, once = false, debugId, makeReveal }: UseRevealOptions,
|
||||||
|
) {
|
||||||
|
const ctx = useGsapContext()
|
||||||
|
useGSAP(() => {
|
||||||
|
const el = ref.current
|
||||||
|
if (!el || !ctx) return
|
||||||
|
|
||||||
|
const scroller = ctx.getScroller()
|
||||||
|
const scrollerEl = scroller instanceof Element ? scroller : undefined
|
||||||
|
|
||||||
|
const rect = el.getBoundingClientRect()
|
||||||
|
let top = 0
|
||||||
|
let bottom = window.innerHeight
|
||||||
|
if (scrollerEl) {
|
||||||
|
const r = scrollerEl.getBoundingClientRect()
|
||||||
|
top = r.top
|
||||||
|
bottom = r.top + r.height
|
||||||
|
}
|
||||||
|
const isInView = rect.bottom > top && rect.top < bottom
|
||||||
|
|
||||||
|
const reveal = makeReveal(el)
|
||||||
|
// A reveal that animates height (pop-ups) shifts every trigger below it.
|
||||||
|
// Re-measure as it animates so positions track the real layout instead of
|
||||||
|
// only correcting at the very end. requestRefresh is throttled + deferred to
|
||||||
|
// the next frame, so this won't re-enter a ScrollTrigger callback.
|
||||||
|
reveal.eventCallback("onUpdate", () => ctx.requestRefresh())
|
||||||
|
reveal.pause()
|
||||||
|
|
||||||
|
const baseTrigger = {
|
||||||
|
trigger: el,
|
||||||
|
start: "top bottom",
|
||||||
|
end: "bottom top",
|
||||||
|
scroller: scrollerEl,
|
||||||
|
markers: GSAP_DEBUG,
|
||||||
|
id: GSAP_DEBUG ? debugId : undefined,
|
||||||
|
}
|
||||||
|
// Full behavior: in at the bottom, out at the top, and back on scroll-up.
|
||||||
|
const addReplayTrigger = () =>
|
||||||
|
ScrollTrigger.create({
|
||||||
|
...baseTrigger,
|
||||||
|
onEnter: () => reveal.play(),
|
||||||
|
onEnterBack: () => reveal.play(),
|
||||||
|
onLeave: () => reveal.reverse(),
|
||||||
|
onLeaveBack: () => reveal.reverse(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (isInView && !scrollOnly) {
|
||||||
|
// The shared timeline only decides *when* the entrance starts; the reveal
|
||||||
|
// plays independently so the ScrollTrigger can take it over afterwards.
|
||||||
|
ctx.schedule(() => reveal.play(), position)
|
||||||
|
// `once` elements keep their revealed state — no scroll trigger at all.
|
||||||
|
if (!once) ctx.onReady(addReplayTrigger)
|
||||||
|
} else if (isInView) {
|
||||||
|
// scrollOnly + already on screen: no enter crossing will fire, so reveal
|
||||||
|
// now. Keep a trigger for scroll-out unless this is a `once` element.
|
||||||
|
reveal.play()
|
||||||
|
if (!once) addReplayTrigger()
|
||||||
|
} else if (once) {
|
||||||
|
// Off-screen: reveal when first reached, then self-destruct so it never
|
||||||
|
// reverses.
|
||||||
|
ScrollTrigger.create({ ...baseTrigger, once: true, onEnter: () => reveal.play() })
|
||||||
|
} else {
|
||||||
|
addReplayTrigger()
|
||||||
|
}
|
||||||
|
}, { dependencies: [] })
|
||||||
|
}
|
||||||
@@ -9,11 +9,32 @@ gsap.registerPlugin(useGSAP)
|
|||||||
gsap.registerPlugin(ScrollTrigger)
|
gsap.registerPlugin(ScrollTrigger)
|
||||||
gsap.registerPlugin(SplitText)
|
gsap.registerPlugin(SplitText)
|
||||||
gsap.registerPlugin(GSDevTools)
|
gsap.registerPlugin(GSDevTools)
|
||||||
|
|
||||||
|
// iOS Safari shows/hides its address bar at the scroll extremes, which resizes
|
||||||
|
// the viewport and makes ScrollTrigger refresh + fire spurious onLeave/onEnter
|
||||||
|
// toggles (text animating out at the bottom and not coming back). Ignoring those
|
||||||
|
// mobile-toolbar resizes keeps the real enter/leave reverse behavior intact.
|
||||||
|
ScrollTrigger.config({ ignoreMobileResize: true })
|
||||||
|
|
||||||
|
// Flip to true to draw ScrollTrigger start/end markers on every animated
|
||||||
|
// element and mount the GSDevTools timeline scrubber. Handy for seeing exactly
|
||||||
|
// where each card's enter/exit lines sit relative to the viewport.
|
||||||
|
export const GSAP_DEBUG = false
|
||||||
const GsapContext = createContext<{
|
const GsapContext = createContext<{
|
||||||
|
// Add a real animation (with its own duration) to the entrance timeline.
|
||||||
addAnimation: (
|
addAnimation: (
|
||||||
animation: gsap.core.TimelineChild,
|
animation: gsap.core.TimelineChild,
|
||||||
position: gsap.Position
|
position: gsap.Position
|
||||||
) => void,
|
) => void,
|
||||||
|
// Schedule a zero-duration callback at `position` — used to *start* an
|
||||||
|
// independent reveal tween so the timeline only orchestrates timing.
|
||||||
|
schedule: (fn: () => void, position: gsap.Position) => void,
|
||||||
|
// Run `cb` once the entrance is done (timeline complete or first user scroll).
|
||||||
|
onReady: (cb: () => void) => void,
|
||||||
|
// Re-measure all ScrollTriggers (throttled to once per frame). Call it
|
||||||
|
// whenever an animation changes content height so trigger positions stay
|
||||||
|
// aligned with the real layout.
|
||||||
|
requestRefresh: () => void,
|
||||||
resetTimeline: () => void,
|
resetTimeline: () => void,
|
||||||
resumeTimeline: () => void,
|
resumeTimeline: () => void,
|
||||||
getScroller: () => Element | Window | null
|
getScroller: () => Element | Window | null
|
||||||
@@ -70,29 +91,104 @@ export default function GsapProvider({ children }: { children: ReactNode }) {
|
|||||||
// }
|
// }
|
||||||
return scrollerRef.current
|
return scrollerRef.current
|
||||||
}, [])
|
}, [])
|
||||||
|
const devToolsCreated = useRef(false)
|
||||||
useGSAP(() => {
|
useGSAP(() => {
|
||||||
if (!tl.current) {
|
if (!tl.current) {
|
||||||
tl.current = gsap.timeline({ paused: true })
|
tl.current = gsap.timeline({ paused: true })
|
||||||
}
|
}
|
||||||
|
if (GSAP_DEBUG && tl.current && !devToolsCreated.current) {
|
||||||
|
devToolsCreated.current = true
|
||||||
|
GSDevTools.create({ animation: tl.current })
|
||||||
|
}
|
||||||
return () => { console.log("gsap cleanup") }
|
return () => { console.log("gsap cleanup") }
|
||||||
})
|
})
|
||||||
|
|
||||||
const addAnimation = useCallback((animation: gsap.core.TimelineChild, position: gsap.Position) => {
|
const addAnimation = useCallback((animation: gsap.core.TimelineChild, position: gsap.Position) => {
|
||||||
console.log("add animation to:", position, tl.current !== undefined)
|
|
||||||
tl.current?.add(animation, position);
|
tl.current?.add(animation, position);
|
||||||
},[])
|
},[])
|
||||||
|
const schedule = useCallback((fn: () => void, position: gsap.Position) => {
|
||||||
|
tl.current?.add(fn, position)
|
||||||
|
},[])
|
||||||
|
|
||||||
|
// Handoff: fire registered callbacks once, when the entrance finishes.
|
||||||
|
const readyFired = useRef(false)
|
||||||
|
const readyCbs = useRef<Array<() => void>>([])
|
||||||
|
const fireReady = useCallback(() => {
|
||||||
|
if (readyFired.current) return
|
||||||
|
readyFired.current = true
|
||||||
|
readyCbs.current.forEach((cb) => cb())
|
||||||
|
readyCbs.current = []
|
||||||
|
},[])
|
||||||
|
const onReady = useCallback((cb: () => void) => {
|
||||||
|
if (readyFired.current) cb()
|
||||||
|
else readyCbs.current.push(cb)
|
||||||
|
},[])
|
||||||
|
|
||||||
|
// Throttle ScrollTrigger.refresh() to once per frame so the ResizeObserver
|
||||||
|
// can fire freely while content height animates.
|
||||||
|
const refreshQueued = useRef(false)
|
||||||
|
const scheduleRefresh = useCallback(() => {
|
||||||
|
if (refreshQueued.current) return
|
||||||
|
refreshQueued.current = true
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
refreshQueued.current = false
|
||||||
|
ScrollTrigger.refresh()
|
||||||
|
})
|
||||||
|
},[])
|
||||||
|
|
||||||
|
const scrollCleanup = useRef<(() => void) | null>(null)
|
||||||
|
const resizeObserver = useRef<ResizeObserver | null>(null)
|
||||||
|
|
||||||
const resetTimeline = useCallback(() => {
|
const resetTimeline = useCallback(() => {
|
||||||
tl.current?.kill()
|
tl.current?.kill()
|
||||||
tl.current?.revert()
|
tl.current?.revert()
|
||||||
ScrollTrigger.getAll().forEach(st => st.kill())
|
ScrollTrigger.getAll().forEach(st => st.kill())
|
||||||
|
resizeObserver.current?.disconnect()
|
||||||
|
scrollCleanup.current?.()
|
||||||
|
scrollCleanup.current = null
|
||||||
|
readyFired.current = false
|
||||||
|
readyCbs.current = []
|
||||||
tl.current = gsap.timeline({paused:true})
|
tl.current = gsap.timeline({paused:true})
|
||||||
},[])
|
},[])
|
||||||
|
|
||||||
const resumeTimeline = useCallback(() => {
|
const resumeTimeline = useCallback(() => {
|
||||||
console.log("resuming timeline:",tl.current)
|
const t = tl.current
|
||||||
tl.current?.resume()
|
if (!t) return
|
||||||
|
// When the orchestrated entrance finishes, hand off to scroll control and
|
||||||
|
// realign triggers against the now-settled layout.
|
||||||
|
t.eventCallback("onComplete", () => { fireReady(); ScrollTrigger.refresh() })
|
||||||
|
|
||||||
|
const scroller = getScroller()
|
||||||
|
// If the user scrolls before the entrance finishes, snap it to the end and
|
||||||
|
// switch to scroll control so the timeline and ScrollTriggers never fight
|
||||||
|
// over the same elements.
|
||||||
|
scrollCleanup.current?.()
|
||||||
|
const onFirstScroll = () => { t.progress(1); fireReady() }
|
||||||
|
scroller?.addEventListener("scroll", onFirstScroll, { once: true, passive: true })
|
||||||
|
scrollCleanup.current = () => scroller?.removeEventListener("scroll", onFirstScroll)
|
||||||
|
|
||||||
|
// Continuously realign triggers while content height changes — entrance
|
||||||
|
// growth, scroll-driven collapses, late-loading media.
|
||||||
|
if (scroller instanceof Element) {
|
||||||
|
const target = scroller.firstElementChild ?? scroller
|
||||||
|
resizeObserver.current?.disconnect()
|
||||||
|
resizeObserver.current = new ResizeObserver(scheduleRefresh)
|
||||||
|
resizeObserver.current.observe(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.resume()
|
||||||
|
},[getScroller, fireReady, scheduleRefresh])
|
||||||
|
|
||||||
|
// Fonts/markdown/images loading also change content height after the triggers
|
||||||
|
// were created; refresh so start/end stay aligned with the real card sizes.
|
||||||
|
useEffect(() => {
|
||||||
|
const refresh = () => ScrollTrigger.refresh()
|
||||||
|
window.addEventListener("load", refresh)
|
||||||
|
document.fonts?.ready.then(refresh).catch(() => {})
|
||||||
|
return () => window.removeEventListener("load", refresh)
|
||||||
}, [])
|
}, [])
|
||||||
return (
|
return (
|
||||||
<GsapContext.Provider value={{ addAnimation, resetTimeline, resumeTimeline, getScroller }}>
|
<GsapContext.Provider value={{ addAnimation, schedule, onReady, requestRefresh: scheduleRefresh, resetTimeline, resumeTimeline, getScroller }}>
|
||||||
{children}
|
{children}
|
||||||
</GsapContext.Provider>
|
</GsapContext.Provider>
|
||||||
)
|
)
|
||||||
|
|||||||
48
src/app/admin/chat/_components/ModelSelector.tsx
Normal file
48
src/app/admin/chat/_components/ModelSelector.tsx
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
'use client'
|
||||||
|
import { trpc } from '~/app/_trpc/Client'
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '~/components/ui/select'
|
||||||
|
|
||||||
|
export default function ModelSelector({ initialValue }: { initialValue: string }) {
|
||||||
|
const utils = trpc.useUtils()
|
||||||
|
const { data: models, isLoading, error } = trpc.chat.listModels.useQuery()
|
||||||
|
const { data: model = initialValue } = trpc.chat.getModel.useQuery(undefined, {
|
||||||
|
initialData: initialValue,
|
||||||
|
})
|
||||||
|
|
||||||
|
const mutation = trpc.chat.updateModel.useMutation({
|
||||||
|
onSuccess: () => utils.chat.getModel.invalidate(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// Ensure the currently-saved model is always selectable, even if the
|
||||||
|
// OpenAI list doesn't include it (e.g. a deprecated model).
|
||||||
|
const options = Array.from(new Set([model, ...(models ?? [])])).filter(Boolean)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<Select value={model} onValueChange={(v) => mutation.mutate({ model: v })}>
|
||||||
|
<SelectTrigger className="w-72">
|
||||||
|
<SelectValue placeholder={isLoading ? 'Loading models…' : 'Select a model'} />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{options.map((id) => (
|
||||||
|
<SelectItem key={id} value={id}>
|
||||||
|
{id}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<div className="flex items-center gap-3 text-sm text-muted-foreground">
|
||||||
|
{mutation.isPending && <span>Saving…</span>}
|
||||||
|
{mutation.isSuccess && !mutation.isPending && <span>Saved</span>}
|
||||||
|
{error && <span className="text-destructive">Failed to load models: {error.message}</span>}
|
||||||
|
{mutation.error && <span className="text-destructive">{mutation.error.message}</span>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,11 +1,23 @@
|
|||||||
import { servTrpc } from '~/app/_trpc/ServerClient'
|
import { servTrpc } from '~/app/_trpc/ServerClient'
|
||||||
import SystemPromptForm from './_components/SystemPromptForm'
|
import SystemPromptForm from './_components/SystemPromptForm'
|
||||||
|
import ModelSelector from './_components/ModelSelector'
|
||||||
|
|
||||||
export default async function SystemPromptPage() {
|
export default async function SystemPromptPage() {
|
||||||
const prompt = await servTrpc.chat.getSystemPrompt()
|
const prompt = await servTrpc.chat.getSystemPrompt()
|
||||||
|
const model = await servTrpc.chat.getModel()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full max-w-2xl p-6 flex flex-col gap-4">
|
<div className="w-full max-w-2xl p-6 flex flex-col gap-8">
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-lg font-semibold">AI Model</h1>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
The OpenAI model used to respond to chat requests.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<ModelSelector initialValue={model} />
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-lg font-semibold">AI System Prompt</h1>
|
<h1 className="text-lg font-semibold">AI System Prompt</h1>
|
||||||
<p className="text-sm text-muted-foreground">
|
<p className="text-sm text-muted-foreground">
|
||||||
@@ -14,5 +26,6 @@ export default async function SystemPromptPage() {
|
|||||||
</div>
|
</div>
|
||||||
<SystemPromptForm initialValue={prompt} />
|
<SystemPromptForm initialValue={prompt} />
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ export async function POST(req: Request) {
|
|||||||
if (!session) return new Response('Session not found', { status: 404 })
|
if (!session) return new Response('Session not found', { status: 404 })
|
||||||
|
|
||||||
const systemPrompt = await servTrpc.chat.getSystemPrompt() || 'You are an AI recruiter assistant.'
|
const systemPrompt = await servTrpc.chat.getSystemPrompt() || 'You are an AI recruiter assistant.'
|
||||||
|
const model = await servTrpc.chat.getModel()
|
||||||
|
|
||||||
// Save the latest user message
|
// Save the latest user message
|
||||||
const lastMessage = messages[messages.length - 1]
|
const lastMessage = messages[messages.length - 1]
|
||||||
@@ -46,7 +47,7 @@ export async function POST(req: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = streamText({
|
const result = streamText({
|
||||||
model: openai('gpt-5-mini'),
|
model: openai(model),
|
||||||
system: systemPrompt,
|
system: systemPrompt,
|
||||||
messages: await convertToModelMessages(messages),
|
messages: await convertToModelMessages(messages),
|
||||||
tools: {
|
tools: {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { useTheme } from "next-themes";
|
||||||
|
import type WaveSurfer from "wavesurfer.js";
|
||||||
import { Download, Loader2, Pause, Play } from "lucide-react";
|
import { Download, Loader2, Pause, Play } from "lucide-react";
|
||||||
import { Slider } from "~/components/ui/slider";
|
|
||||||
import { Button } from "~/components/ui/button";
|
import { Button } from "~/components/ui/button";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
|
||||||
@@ -13,6 +14,20 @@ function formatTime(seconds: number) {
|
|||||||
return `${m}:${s.toString().padStart(2, "0")}`;
|
return `${m}:${s.toString().padStart(2, "0")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cssVar(name: string, fallback: string) {
|
||||||
|
if (typeof window === "undefined") return fallback;
|
||||||
|
const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
||||||
|
return v || fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
function waveColors() {
|
||||||
|
return {
|
||||||
|
waveColor: cssVar("--muted-foreground", "#9ca3af"),
|
||||||
|
progressColor: cssVar("--primary", "#e2761b"),
|
||||||
|
cursorColor: cssVar("--foreground", "#111827"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function AudioPlayer(props: {
|
export default function AudioPlayer(props: {
|
||||||
/** Streaming-friendly source the player actually plays. */
|
/** Streaming-friendly source the player actually plays. */
|
||||||
src: string;
|
src: string;
|
||||||
@@ -20,21 +35,60 @@ export default function AudioPlayer(props: {
|
|||||||
downloadUrl: string;
|
downloadUrl: string;
|
||||||
downloadName: string;
|
downloadName: string;
|
||||||
}) {
|
}) {
|
||||||
const audioRef = useRef<HTMLAudioElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
const wsRef = useRef<WaveSurfer | null>(null);
|
||||||
const [playing, setPlaying] = useState(false);
|
const [playing, setPlaying] = useState(false);
|
||||||
const [current, setCurrent] = useState(0);
|
const [current, setCurrent] = useState(0);
|
||||||
const [duration, setDuration] = useState(0);
|
const [duration, setDuration] = useState(0);
|
||||||
const [seeking, setSeeking] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
const [downloading, setDownloading] = useState(false);
|
const [downloading, setDownloading] = useState(false);
|
||||||
|
const { resolvedTheme } = useTheme();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let ws: WaveSurfer | null = null;
|
||||||
|
let cancelled = false;
|
||||||
|
setReady(false);
|
||||||
|
setCurrent(0);
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const WaveSurferClass = (await import("wavesurfer.js")).default;
|
||||||
|
if (cancelled || !containerRef.current) return;
|
||||||
|
ws = WaveSurferClass.create({
|
||||||
|
container: containerRef.current,
|
||||||
|
url: props.src,
|
||||||
|
height: 44,
|
||||||
|
barWidth: 2,
|
||||||
|
barGap: 1,
|
||||||
|
barRadius: 2,
|
||||||
|
normalize: true,
|
||||||
|
cursorWidth: 1,
|
||||||
|
...waveColors(),
|
||||||
|
});
|
||||||
|
wsRef.current = ws;
|
||||||
|
ws.on("ready", () => {
|
||||||
|
setReady(true);
|
||||||
|
setDuration(ws?.getDuration() ?? 0);
|
||||||
|
});
|
||||||
|
ws.on("play", () => setPlaying(true));
|
||||||
|
ws.on("pause", () => setPlaying(false));
|
||||||
|
ws.on("finish", () => setPlaying(false));
|
||||||
|
ws.on("timeupdate", (t) => setCurrent(t));
|
||||||
|
})();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
ws?.destroy();
|
||||||
|
wsRef.current = null;
|
||||||
|
};
|
||||||
|
}, [props.src]);
|
||||||
|
|
||||||
|
// Re-tint the waveform when the user toggles light/dark.
|
||||||
|
useEffect(() => {
|
||||||
|
wsRef.current?.setOptions(waveColors());
|
||||||
|
}, [resolvedTheme]);
|
||||||
|
|
||||||
function togglePlay() {
|
function togglePlay() {
|
||||||
const audio = audioRef.current;
|
wsRef.current?.playPause();
|
||||||
if (!audio) return;
|
|
||||||
if (audio.paused) {
|
|
||||||
audio.play();
|
|
||||||
} else {
|
|
||||||
audio.pause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleDownload() {
|
async function handleDownload() {
|
||||||
@@ -62,24 +116,12 @@ export default function AudioPlayer(props: {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center gap-3 rounded-lg border bg-transparent px-3 py-2">
|
<div className="flex items-center gap-3 rounded-lg border bg-transparent px-3 py-2">
|
||||||
<audio
|
|
||||||
ref={audioRef}
|
|
||||||
src={props.src}
|
|
||||||
preload="metadata"
|
|
||||||
onPlay={() => setPlaying(true)}
|
|
||||||
onPause={() => setPlaying(false)}
|
|
||||||
onLoadedMetadata={(e) => setDuration(e.currentTarget.duration)}
|
|
||||||
onTimeUpdate={(e) => {
|
|
||||||
if (!seeking) setCurrent(e.currentTarget.currentTime);
|
|
||||||
}}
|
|
||||||
onEnded={() => setPlaying(false)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
size="icon"
|
size="icon"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
aria-label={playing ? "Pause" : "Play"}
|
aria-label={playing ? "Pause" : "Play"}
|
||||||
|
disabled={!ready}
|
||||||
onClick={togglePlay}
|
onClick={togglePlay}
|
||||||
>
|
>
|
||||||
{playing ? <Pause /> : <Play />}
|
{playing ? <Pause /> : <Play />}
|
||||||
@@ -89,21 +131,14 @@ export default function AudioPlayer(props: {
|
|||||||
{formatTime(current)}
|
{formatTime(current)}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<Slider
|
<div className="relative flex-1">
|
||||||
className="flex-1"
|
<div ref={containerRef} className="w-full" />
|
||||||
min={0}
|
{!ready && (
|
||||||
max={duration || 1}
|
<div className="absolute inset-0 flex items-center">
|
||||||
step={0.1}
|
<div className="h-7 w-full animate-pulse rounded bg-muted-foreground/15" />
|
||||||
value={[current]}
|
</div>
|
||||||
onValueChange={([v]) => {
|
)}
|
||||||
setSeeking(true);
|
</div>
|
||||||
setCurrent(v ?? 0);
|
|
||||||
}}
|
|
||||||
onValueCommit={([v]) => {
|
|
||||||
if (audioRef.current) audioRef.current.currentTime = v ?? 0;
|
|
||||||
setSeeking(false);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<span className="w-10 shrink-0 font-mono text-xs text-muted-foreground tabular-nums">
|
<span className="w-10 shrink-0 font-mono text-xs text-muted-foreground tabular-nums">
|
||||||
{formatTime(duration)}
|
{formatTime(duration)}
|
||||||
|
|||||||
@@ -41,10 +41,10 @@ export default function ProjectsPage() {
|
|||||||
<Card.AnimatedCard position={i + 1.2} key={project.id}>
|
<Card.AnimatedCard position={i + 1.2} key={project.id}>
|
||||||
<Card.CardHeader>
|
<Card.CardHeader>
|
||||||
<div className="flex items-start justify-between gap-2 flex-wrap">
|
<div className="flex items-start justify-between gap-2 flex-wrap">
|
||||||
<AnimateTextIn position={i + 1.4} animation="slide"><Card.CardTitle>{project.title}</Card.CardTitle></AnimateTextIn>
|
<AnimateTextIn once position={i + 1.4} animation="slide"><Card.CardTitle>{project.title}</Card.CardTitle></AnimateTextIn>
|
||||||
<div className="flex gap-2 flex-wrap">
|
<div className="flex gap-2 flex-wrap">
|
||||||
{project.sourceType && (
|
{project.sourceType && (
|
||||||
<AnimatePopUp position={i + 2} duration={2}>
|
<AnimatePopUp position={i + 2} duration={2} once>
|
||||||
<Badge variant={project.sourceType === "open" ? "secondary" : "outline"}>
|
<Badge variant={project.sourceType === "open" ? "secondary" : "outline"}>
|
||||||
{project.sourceType === "open" ? "Open Source" : "Closed Source"}
|
{project.sourceType === "open" ? "Open Source" : "Closed Source"}
|
||||||
</Badge>
|
</Badge>
|
||||||
@@ -62,7 +62,7 @@ export default function ProjectsPage() {
|
|||||||
<Card.CardContent className="flex flex-col gap-3">
|
<Card.CardContent className="flex flex-col gap-3">
|
||||||
{project.description && (
|
{project.description && (
|
||||||
<div className="prose prose-sm dark:prose-invert max-w-none text-muted-foreground">
|
<div className="prose prose-sm dark:prose-invert max-w-none text-muted-foreground">
|
||||||
<AnimatePopUp position={i + 1.4} duration={10}>
|
<AnimatePopUp once position={i + 1.4} duration={10}>
|
||||||
<Markdown remarkPlugins={[remarkGfm]}>{project.description}</Markdown>
|
<Markdown remarkPlugins={[remarkGfm]}>{project.description}</Markdown>
|
||||||
</AnimatePopUp>
|
</AnimatePopUp>
|
||||||
</div>
|
</div>
|
||||||
@@ -71,7 +71,7 @@ export default function ProjectsPage() {
|
|||||||
{project.techStack?.stackItems && project.techStack.stackItems.length > 0 && (
|
{project.techStack?.stackItems && project.techStack.stackItems.length > 0 && (
|
||||||
<div className="flex flex-wrap gap-1.5">
|
<div className="flex flex-wrap gap-1.5">
|
||||||
{project.techStack.stackItems.map((item, k) => (
|
{project.techStack.stackItems.map((item, k) => (
|
||||||
<AnimatePopUp key={k} position={(i + 2) + k * 0.5}> <StackBadge key={item} item={item} /> </AnimatePopUp>
|
<AnimatePopUp key={k} position={(i + 2) + k * 0.5} once> <StackBadge key={item} item={item} /> </AnimatePopUp>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -111,6 +111,11 @@ export default function ProjectsPage() {
|
|||||||
<div className="pt-5" />
|
<div className="pt-5" />
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
{/* Scroll runway: lets the last cards' ScrollTrigger exit points be
|
||||||
|
reached past the visible area, so on short viewports the second-to-last
|
||||||
|
card animates out off-screen instead of being frozen mid-exit at the
|
||||||
|
bottom of the scroll. Tune the height if a card is still caught. */}
|
||||||
|
<div aria-hidden className="h-[70dvh] shrink-0" />
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useGSAP } from "@gsap/react"; import * as React from "react"
|
import * as React from "react"
|
||||||
import { useRef } from "react";
|
import { useRef } from "react";
|
||||||
import { useGsapContext } from "~/app/_providers/GsapProvicer";
|
|
||||||
import gsap from 'gsap'
|
import gsap from 'gsap'
|
||||||
|
import { useReveal } from "~/app/_components/Animated/useReveal";
|
||||||
import { cn } from "~/lib/utils"
|
import { cn } from "~/lib/utils"
|
||||||
|
|
||||||
function Card({
|
function Card({
|
||||||
@@ -27,40 +27,17 @@ function AnimatedCard({
|
|||||||
position = 0,
|
position = 0,
|
||||||
size = "default",
|
size = "default",
|
||||||
scrollOnly = false,
|
scrollOnly = false,
|
||||||
|
once = false,
|
||||||
...props
|
...props
|
||||||
}: React.ComponentProps<"div"> & { size?: "default" | "sm", position?: gsap.Position, scrollOnly?: boolean }) {
|
}: React.ComponentProps<"div"> & { size?: "default" | "sm", position?: gsap.Position, scrollOnly?: boolean, once?: boolean }) {
|
||||||
const gsapContext = useGsapContext()
|
|
||||||
const ref = useRef<HTMLDivElement | null>(null)
|
const ref = useRef<HTMLDivElement | null>(null)
|
||||||
useGSAP(() => {
|
useReveal(ref, {
|
||||||
const rect = ref.current?.getBoundingClientRect()
|
position,
|
||||||
const scroller = gsapContext?.getScroller()
|
scrollOnly,
|
||||||
console.log(scroller)
|
once,
|
||||||
let viewportTop = 0
|
debugId: `card-${position}`,
|
||||||
let viewportBottom = window.innerHeight
|
makeReveal: (el) => gsap.from(el, { x: -100, opacity: 0, duration: 0.5, paused: true }),
|
||||||
if (scroller && scroller instanceof Element) {
|
|
||||||
const scrollerRect = scroller.getBoundingClientRect()
|
|
||||||
viewportTop = scrollerRect.top
|
|
||||||
viewportBottom = scrollerRect.top + scrollerRect.height
|
|
||||||
}
|
|
||||||
const isInView = rect && rect.bottom > viewportTop && rect.top < viewportBottom
|
|
||||||
console.log(isInView)
|
|
||||||
const fromVars = { x: -100, opacity: 0, duration: 0.5 }
|
|
||||||
if (isInView && !scrollOnly) {
|
|
||||||
gsapContext?.addAnimation(gsap.from(ref.current, fromVars), position)
|
|
||||||
} else {
|
|
||||||
gsap.from(ref.current,
|
|
||||||
{
|
|
||||||
...fromVars,
|
|
||||||
scrollTrigger: {
|
|
||||||
trigger: ref.current,
|
|
||||||
start: 'top bottom',
|
|
||||||
end: 'bottom top',
|
|
||||||
toggleActions: "play reverse play reverse",
|
|
||||||
scroller
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}, { dependencies: [] })
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={ref}
|
ref={ref}
|
||||||
|
|||||||
@@ -175,6 +175,7 @@ export const chatMessageRelations = relations(chatMessage, ({ one }) => ({
|
|||||||
export const systemSettings = createTable(
|
export const systemSettings = createTable(
|
||||||
"systemSetting",
|
"systemSetting",
|
||||||
(d) => ({
|
(d) => ({
|
||||||
systemPropmt: d.text()
|
systemPropmt: d.text(),
|
||||||
|
model: d.text()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,6 +7,26 @@ import { isAdmin } from '~/app/actions';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { eq } from 'drizzle-orm';
|
import { eq } from 'drizzle-orm';
|
||||||
import { clerkClient, auth } from '@clerk/nextjs/server'
|
import { clerkClient, auth } from '@clerk/nextjs/server'
|
||||||
|
import { env } from '~/env'
|
||||||
|
|
||||||
|
export const DEFAULT_MODEL = 'gpt-5-mini'
|
||||||
|
|
||||||
|
// Models returned by the OpenAI API that aren't usable for chat completions.
|
||||||
|
const NON_CHAT_MODEL = /embedding|image|audio|realtime|transcribe|tts|whisper|moderation|dall-e|search|codex|instruct/
|
||||||
|
|
||||||
|
async function readSettings() {
|
||||||
|
return db.select().from(systemSettings).limit(1).then((r) => r[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeSettings(values: { systemPropmt?: string | null; model?: string | null }) {
|
||||||
|
const current = await readSettings()
|
||||||
|
await db.delete(systemSettings)
|
||||||
|
await db.insert(systemSettings).values({
|
||||||
|
systemPropmt: values.systemPropmt ?? current?.systemPropmt ?? null,
|
||||||
|
model: values.model ?? current?.model ?? null,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const chatRouter = router({
|
export const chatRouter = router({
|
||||||
getSession: publicProcedure.query(async () => {
|
getSession: publicProcedure.query(async () => {
|
||||||
const { userId } = await auth();
|
const { userId } = await auth();
|
||||||
@@ -66,13 +86,34 @@ export const chatRouter = router({
|
|||||||
|
|
||||||
}),
|
}),
|
||||||
getSystemPrompt: publicProcedure.query(async () => {
|
getSystemPrompt: publicProcedure.query(async () => {
|
||||||
const row = await db.select().from(systemSettings).limit(1).then((r) => r[0])
|
const row = await readSettings()
|
||||||
return row?.systemPropmt ?? ''
|
return row?.systemPropmt ?? ''
|
||||||
}),
|
}),
|
||||||
updateSystemPrompt: publicProcedure.input(z.object({ prompt: z.string() })).mutation(async ({ input }) => {
|
updateSystemPrompt: publicProcedure.input(z.object({ prompt: z.string() })).mutation(async ({ input }) => {
|
||||||
if (!(await isAdmin())) throw new TRPCError({ code: 'FORBIDDEN' })
|
if (!(await isAdmin())) throw new TRPCError({ code: 'FORBIDDEN' })
|
||||||
await db.delete(systemSettings)
|
await writeSettings({ systemPropmt: input.prompt })
|
||||||
await db.insert(systemSettings).values({ systemPropmt: input.prompt })
|
}),
|
||||||
|
getModel: publicProcedure.query(async () => {
|
||||||
|
const row = await readSettings()
|
||||||
|
return row?.model ?? DEFAULT_MODEL
|
||||||
|
}),
|
||||||
|
listModels: publicProcedure.query(async () => {
|
||||||
|
if (!(await isAdmin())) throw new TRPCError({ code: 'FORBIDDEN' })
|
||||||
|
const res = await fetch('https://api.openai.com/v1/models', {
|
||||||
|
headers: { Authorization: `Bearer ${env.OPENAI_API_KEY}` },
|
||||||
|
})
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: `failed to fetch models (${res.status})` })
|
||||||
|
}
|
||||||
|
const json = (await res.json()) as { data: { id: string }[] }
|
||||||
|
return json.data
|
||||||
|
.map((m) => m.id)
|
||||||
|
.filter((id) => (id.startsWith('gpt') || /^o\d/.test(id) || id.startsWith('chatgpt')) && !NON_CHAT_MODEL.test(id))
|
||||||
|
.sort()
|
||||||
|
}),
|
||||||
|
updateModel: publicProcedure.input(z.object({ model: z.string() })).mutation(async ({ input }) => {
|
||||||
|
if (!(await isAdmin())) throw new TRPCError({ code: 'FORBIDDEN' })
|
||||||
|
await writeSettings({ model: input.model })
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user