Skip to content
Open
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
10 changes: 7 additions & 3 deletions packages/elements/src/chain-of-thought/ChainOfThought.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import type { HTMLAttributes, Ref } from 'vue'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { useVModel } from '@vueuse/core'
import { provide } from 'vue'
import { ChainOfThoughtContextKey } from './context'
import { provideChainOfThought } from './context'

interface ChainOfThoughtProps {
modelValue?: boolean
Expand All @@ -28,7 +27,12 @@ const isOpen = useVModel(props, 'modelValue', emit, {
passive: true,
})

provide(ChainOfThoughtContextKey, isOpen as Ref<boolean>)
provideChainOfThought({
isOpen: isOpen as Ref<boolean>,
setIsOpen: (open: boolean) => {
isOpen.value = open
},
})
</script>

<template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const props = defineProps<{
v-bind="$attrs"
>
<div
class="relative flex max-h-[22rem] items-center justify-center overflow-hidden rounded-lg bg-muted p-3"
class="relative flex max-h-88 items-center justify-center overflow-hidden rounded-lg bg-muted p-3"
>
<slot />
</div>
Expand Down
27 changes: 7 additions & 20 deletions packages/elements/src/chain-of-thought/context.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import type { InjectionKey, Ref } from 'vue'
import { inject } from 'vue'
import type { Ref } from 'vue'
import { createContext } from 'reka-ui'

export const ChainOfThoughtContextKey: InjectionKey<Ref<boolean>> = Symbol(
'ChainOfThoughtContext',
)

export function useChainOfThought() {
const isOpen = inject(ChainOfThoughtContextKey)

if (!isOpen) {
throw new Error(
'useChainOfThought must be used within a <ChainOfThought> component',
)
}

const setIsOpen = (open: boolean) => {
isOpen.value = open
}

return { isOpen, setIsOpen }
export interface ChainOfThoughtContextValue {
isOpen: Ref<boolean>
setIsOpen: (open: boolean) => void
}

export const [useChainOfThought, provideChainOfThought] = createContext<ChainOfThoughtContextValue>('ChainOfThought')
12 changes: 6 additions & 6 deletions packages/elements/src/code-block/CodeBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { BundledLanguage } from 'shiki'
import type { HTMLAttributes } from 'vue'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { reactiveOmit, useDebounceFn } from '@vueuse/core'
import { computed, onBeforeUnmount, provide, ref, watch } from 'vue'
import { CodeBlockKey } from './context'
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { provideCodeBlock } from './context'
import { highlightCode } from './utils'

const props = withDefaults(
Expand All @@ -24,10 +24,6 @@ const delegatedProps = reactiveOmit(props, 'code', 'language', 'showLineNumbers'
const html = ref('')
const darkHtml = ref('')

provide(CodeBlockKey, {
code: computed(() => props.code),
})

let requestId = 0
let isUnmounted = false

Expand All @@ -48,6 +44,10 @@ const updateHighlight = useDebounceFn(async (code: string, language: BundledLang
}
}, 100)

provideCodeBlock({
code: computed(() => props.code),
})

watch(
() => [props.code, props.language, props.showLineNumbers] as const,
([code, language, showLineNumbers]) => {
Expand Down
11 changes: 3 additions & 8 deletions packages/elements/src/code-block/CodeBlockCopyButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Button } from '@repo/shadcn-vue/components/ui/button'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { reactiveOmit } from '@vueuse/core'
import { CheckIcon, CopyIcon } from 'lucide-vue-next'
import { computed, inject, onBeforeUnmount, ref } from 'vue'
import { CodeBlockKey } from './context'
import { computed, onBeforeUnmount, ref } from 'vue'
import { useCodeBlock } from './context'

const props = withDefaults(
defineProps<{
Expand All @@ -24,12 +24,7 @@ const emit = defineEmits<{

const delegatedProps = reactiveOmit(props, 'timeout', 'class')

const context = inject(CodeBlockKey)

if (!context)
throw new Error('CodeBlockCopyButton must be used within a <CodeBlock />')

const { code } = context
const { code } = useCodeBlock()

const isCopied = ref(false)
let resetTimer: ReturnType<typeof setTimeout> | undefined
Expand Down
7 changes: 4 additions & 3 deletions packages/elements/src/code-block/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ComputedRef, InjectionKey } from 'vue'
import type { ComputedRef } from 'vue'
import { createContext } from 'reka-ui'

export interface CodeBlockContext {
export interface CodeBlockContextValue {
code: ComputedRef<string>
}

export const CodeBlockKey: InjectionKey<CodeBlockContext> = Symbol('CodeBlock')
export const [useCodeBlock, provideCodeBlock] = createContext<CodeBlockContextValue>('CodeBlock')
6 changes: 3 additions & 3 deletions packages/elements/src/confirmation/Confirmation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import type { HTMLAttributes } from 'vue'
import type { ToolUIPartApproval } from './context'
import { Alert } from '@repo/shadcn-vue/components/ui/alert'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { provide, toRef } from 'vue'
import { ConfirmationKey } from './context'
import { toRef } from 'vue'
import { provideConfirmation } from './context'

const props = defineProps<{
approval?: ToolUIPartApproval
state: ToolUIPart['state']
class?: HTMLAttributes['class']
}>()

provide(ConfirmationKey, {
provideConfirmation({
approval: toRef(props, 'approval'),
state: toRef(props, 'state'),
})
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/confirmation/ConfirmationAccepted.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useConfirmationContext } from './context'
import { useConfirmation } from './context'
const { approval, state } = useConfirmationContext()
const { approval, state } = useConfirmation()
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/confirmation/ConfirmationActions.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script setup lang="ts">
import type { HTMLAttributes } from 'vue'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { useConfirmationContext } from './context'
import { useConfirmation } from './context'

const props = defineProps<{
class?: HTMLAttributes['class']
}>()

const { state } = useConfirmationContext()
const { state } = useConfirmation()
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/confirmation/ConfirmationRejected.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useConfirmationContext } from './context'
import { useConfirmation } from './context'

const { approval, state } = useConfirmationContext()
const { approval, state } = useConfirmation()
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/confirmation/ConfirmationRequest.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useConfirmationContext } from './context'
import { useConfirmation } from './context'

const { state } = useConfirmationContext()
const { state } = useConfirmation()
</script>

<template>
Expand Down
14 changes: 3 additions & 11 deletions packages/elements/src/confirmation/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ToolUIPart } from 'ai'
import type { InjectionKey, Ref } from 'vue'
import { inject } from 'vue'
import type { Ref } from 'vue'
import { createContext } from 'reka-ui'

export type ToolUIPartApproval
= | {
Expand Down Expand Up @@ -35,12 +35,4 @@ export interface ConfirmationContextValue {
state: Ref<ToolUIPart['state']>
}

export const ConfirmationKey: InjectionKey<ConfirmationContextValue>
= Symbol('ConfirmationContext')

export function useConfirmationContext() {
const context = inject<ConfirmationContextValue | null>(ConfirmationKey, null)
if (!context)
throw new Error('Confirmation components must be used within <Confirmation>')
return context
}
export const [useConfirmation, provideConfirmation] = createContext<ConfirmationContextValue>('Confirmation')
6 changes: 3 additions & 3 deletions packages/elements/src/context/Context.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import type { LanguageModelUsage } from 'ai'
import type { ModelId } from './context'
import { HoverCard } from '@repo/shadcn-vue/components/ui/hover-card'
import { computed, provide } from 'vue'
import { ContextKey } from './context'
import { computed } from 'vue'
import { provideContext } from './context'

interface Props {
usedTokens: number
Expand All @@ -14,7 +14,7 @@ interface Props {

const props = defineProps<Props>()

provide(ContextKey, {
provideContext({
usedTokens: computed(() => props.usedTokens),
maxTokens: computed(() => props.maxTokens),
usage: computed(() => props.usage),
Expand Down
15 changes: 3 additions & 12 deletions packages/elements/src/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LanguageModelUsage } from 'ai'
import type { ComputedRef, InjectionKey } from 'vue'
import { inject } from 'vue'
import type { ComputedRef } from 'vue'
import { createContext } from 'reka-ui'

export type ModelId = string

Expand All @@ -11,13 +11,4 @@ export interface ContextContextValue {
modelId: ComputedRef<ModelId | undefined>
}

export const ContextKey: InjectionKey<ContextContextValue>
= Symbol('ContextContext')

export function useContextValue(): ContextContextValue {
const context = inject<ContextContextValue>(ContextKey)
if (!context) {
throw new Error('Context components must be used within Context')
}
return context
}
export const [useContextValue, provideContext] = createContext<ContextContextValue>('Context')
11 changes: 4 additions & 7 deletions packages/elements/src/message/MessageBranch.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script setup lang="ts">
import type { HTMLAttributes, VNode } from 'vue'
import type { MessageBranchContextType } from './context'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { provide, readonly, ref } from 'vue'
import { MessageBranchKey } from './context'
import { readonly, ref } from 'vue'
import { provideMessageBranch } from './context'

interface Props {
defaultBranch?: number
Expand Down Expand Up @@ -44,16 +43,14 @@ function setBranches(count: number) {
totalBranches.value = count
}

const contextValue: MessageBranchContextType = {
provideMessageBranch({
currentBranch: readonly(currentBranch),
totalBranches: readonly(totalBranches),
goToPrevious,
goToNext,
branches,
setBranches,
}

provide(MessageBranchKey, contextValue)
})
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/message/MessageBranchContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { HTMLAttributes } from 'vue'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { computed, Fragment, isVNode, onMounted, useSlots, watch } from 'vue'
import { useMessageBranchContext } from './context'
import { useMessageBranch } from './context'

interface Props {
class?: HTMLAttributes['class']
Expand All @@ -11,7 +11,7 @@ interface Props {
const props = defineProps<Props>()
const slots = useSlots()

const { currentBranch, setBranches } = useMessageBranchContext()
const { currentBranch, setBranches } = useMessageBranch()

const branchVNodes = computed(() => {
const nodes = slots.default?.() ?? []
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/message/MessageBranchNext.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script setup lang="ts">
import { Button } from '@repo/shadcn-vue/components/ui/button'
import { ChevronRightIcon } from 'lucide-vue-next'
import { useMessageBranchContext } from './context'
import { useMessageBranch } from './context'

const { goToNext, totalBranches } = useMessageBranchContext()
const { goToNext, totalBranches } = useMessageBranch()
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/message/MessageBranchPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import type { HTMLAttributes } from 'vue'
import { ButtonGroupText } from '@repo/shadcn-vue/components/ui/button-group'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { useMessageBranchContext } from './context'
import { useMessageBranch } from './context'
interface Props {
class?: HTMLAttributes['class']
}
const props = defineProps<Props>()
const { currentBranch, totalBranches } = useMessageBranchContext()
const { currentBranch, totalBranches } = useMessageBranch()
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/message/MessageBranchPrevious.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script setup lang="ts">
import { Button } from '@repo/shadcn-vue/components/ui/button'
import { ChevronLeftIcon } from 'lucide-vue-next'
import { useMessageBranchContext } from './context'
import { useMessageBranch } from './context'

const { goToPrevious, totalBranches } = useMessageBranchContext()
const { goToPrevious, totalBranches } = useMessageBranch()
</script>

<template>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/src/message/MessageBranchSelector.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script setup lang="ts">
import type { UIMessage } from 'ai'
import { ButtonGroup } from '@repo/shadcn-vue/components/ui/button-group'
import { useMessageBranchContext } from './context'
import { useMessageBranch } from './context'

interface Props {
from: UIMessage['role']
}
defineProps<Props>()

const { totalBranches } = useMessageBranchContext()
const { totalBranches } = useMessageBranch()
</script>

<template>
Expand Down
15 changes: 3 additions & 12 deletions packages/elements/src/message/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { InjectionKey, Ref, VNode } from 'vue'
import { inject } from 'vue'
import type { Ref, VNode } from 'vue'
import { createContext } from 'reka-ui'

export interface MessageBranchContextType<T = VNode[]> {
currentBranch: Readonly<Ref<number>>
Expand All @@ -10,13 +10,4 @@ export interface MessageBranchContextType<T = VNode[]> {
setBranches: (count: number) => void
}

export const MessageBranchKey: InjectionKey<MessageBranchContextType>
= Symbol('MessageBranch')

export function useMessageBranchContext(): MessageBranchContextType {
const ctx = inject(MessageBranchKey)
if (!ctx) {
throw new Error('Message Branch components must be used within Message Branch')
}
return ctx
}
export const [useMessageBranch, provideMessageBranch] = createContext<MessageBranchContextType>('MessageBranch')
Loading