@superinterface/react 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/package.json +41 -0
  2. package/src/components/examples/Examples/Example/index.tsx +65 -0
  3. package/src/components/examples/Examples/index.tsx +85 -0
  4. package/src/components/messageGroups/MessagesGroupBase/AssistantAvatar.tsx +18 -0
  5. package/src/components/messageGroups/MessagesGroupBase/Name.tsx +24 -0
  6. package/src/components/messageGroups/MessagesGroupBase/index.tsx +29 -0
  7. package/src/components/messages/Form/Submit/index.tsx +29 -0
  8. package/src/components/messages/Form/index.tsx +150 -0
  9. package/src/components/messages/Message/TextContent/index.tsx +22 -0
  10. package/src/components/messages/Message/index.tsx +71 -0
  11. package/src/components/messages/Messages/Content/MessageGroup/Content/index.tsx +22 -0
  12. package/src/components/messages/Messages/Content/MessageGroup/index.tsx +47 -0
  13. package/src/components/messages/Messages/Content/index.tsx +26 -0
  14. package/src/components/messages/Messages/ProgressMessage/index.tsx +31 -0
  15. package/src/components/messages/Messages/index.tsx +102 -0
  16. package/src/components/runSteps/RunStep/ToolCalls/Starting/index.tsx +28 -0
  17. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/CodeInterpreter/index.tsx +17 -0
  18. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/Availabilities/Title.tsx +29 -0
  19. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/Availabilities/index.tsx +34 -0
  20. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/Content/index.tsx +58 -0
  21. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/Icon.tsx +28 -0
  22. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/Scores/Title.tsx +29 -0
  23. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/Scores/index.tsx +34 -0
  24. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/Fn/index.tsx +31 -0
  25. package/src/components/runSteps/RunStep/ToolCalls/ToolCall/index.tsx +44 -0
  26. package/src/components/runSteps/RunStep/ToolCalls/index.tsx +31 -0
  27. package/src/components/runSteps/RunStep/index.tsx +21 -0
  28. package/src/components/runSteps/RunSteps/index.tsx +22 -0
  29. package/src/components/skeletons/MessagesSkeleton/index.tsx +50 -0
  30. package/src/components/skeletons/RunSkeleton/index.tsx +34 -0
  31. package/src/components/skeletons/Skeleton/index.tsx +17 -0
  32. package/src/components/skeletons/StartingContentSkeleton/index.tsx +13 -0
  33. package/src/components/skeletons/StartingSkeleton/index.tsx +33 -0
  34. package/src/components/spinners/Spinner/index.tsx +17 -0
  35. package/src/components/textareas/TextareaBase/index.tsx +28 -0
  36. package/src/components/toolCalls/ToolCallBase/ToolCallTitle.tsx +17 -0
  37. package/src/components/toolCalls/ToolCallBase/index.tsx +34 -0
  38. package/src/contexts/assistants/AssistantNameContext/index.tsx +3 -0
  39. package/src/contexts/avatars/AssistantAvatarContext/index.tsx +14 -0
  40. package/src/contexts/markdown/MarkdownContext/index.ts +7 -0
  41. package/src/contexts/markdown/MarkdownContext/lib/components/Link.tsx +13 -0
  42. package/src/contexts/markdown/MarkdownContext/lib/components/ListItem.tsx +13 -0
  43. package/src/contexts/markdown/MarkdownContext/lib/components/OrderedList.tsx +16 -0
  44. package/src/contexts/markdown/MarkdownContext/lib/components/Paragraph.tsx +17 -0
  45. package/src/contexts/markdown/MarkdownContext/lib/components/Strong.tsx +9 -0
  46. package/src/contexts/markdown/MarkdownContext/lib/components/UnorderedList.tsx +16 -0
  47. package/src/contexts/markdown/MarkdownContext/lib/components/index.tsx +16 -0
  48. package/src/hooks/actions/useHandleAction/index.tsx +30 -0
  49. package/src/hooks/actions/useHandleAction/lib/mutationOptions/index.ts +14 -0
  50. package/src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/index.ts +39 -0
  51. package/src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/toolOutput/index.ts +29 -0
  52. package/src/hooks/actions/useHandleAction/lib/mutationOptions/onSettled.ts +22 -0
  53. package/src/hooks/actions/useManageActions/index.tsx +43 -0
  54. package/src/hooks/messageGroups/useMessageGroups/index.ts +18 -0
  55. package/src/hooks/messageGroups/useMessageGroups/lib/messageGroups/index.ts +37 -0
  56. package/src/hooks/messageGroups/useMessageGroups/lib/messageGroups/newGroup/index.ts +14 -0
  57. package/src/hooks/messageGroups/useMessageGroups/lib/messageGroups/newGroup/newGroupItem.ts +12 -0
  58. package/src/hooks/messages/useCreateMessage/index.ts +30 -0
  59. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/index.ts +24 -0
  60. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/mutationFn.ts +25 -0
  61. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/onError.ts +27 -0
  62. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/onMutate/data.ts +62 -0
  63. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/onMutate/index.ts +25 -0
  64. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/onSettled.ts +16 -0
  65. package/src/hooks/messages/useCreateMessage/lib/mutationOptions/onSuccess.ts +48 -0
  66. package/src/hooks/messages/useLatestMessage/index.ts +20 -0
  67. package/src/hooks/messages/useMessages/index.tsx +41 -0
  68. package/src/hooks/messages/useMessages/lib/queryOptions/index.ts +20 -0
  69. package/src/hooks/messages/useMessages/lib/queryOptions/queryFn/data/index.ts +32 -0
  70. package/src/hooks/messages/useMessages/lib/queryOptions/queryFn/data/runMessages/getLatestRun.ts +15 -0
  71. package/src/hooks/messages/useMessages/lib/queryOptions/queryFn/data/runMessages/index.ts +44 -0
  72. package/src/hooks/messages/useMessages/lib/queryOptions/queryFn/hasNextPage.ts +12 -0
  73. package/src/hooks/messages/useMessages/lib/queryOptions/queryFn/index.ts +32 -0
  74. package/src/hooks/messages/useMessages/lib/queryOptions/queryFn/messagesLimit.ts +1 -0
  75. package/src/hooks/messages/useMessages/lib/queryOptions/queryKey.ts +3 -0
  76. package/src/hooks/misc/useInfiniteScroll/index.tsx +42 -0
  77. package/src/hooks/misc/useThrottledEffect/index.tsx +19 -0
  78. package/src/hooks/runs/useCreateRun/index.ts +30 -0
  79. package/src/hooks/runs/useCreateRun/lib/mutationOptions/index.ts +16 -0
  80. package/src/hooks/runs/useCreateRun/lib/mutationOptions/mutationFn.ts +30 -0
  81. package/src/hooks/runs/useCreateRun/lib/mutationOptions/onSettled.ts +22 -0
  82. package/src/hooks/runs/useCreateRun/lib/mutationOptions/onSuccess.ts +44 -0
  83. package/src/hooks/runs/useIsRunActive/index.tsx +65 -0
  84. package/src/hooks/runs/useLatestRun/index.ts +20 -0
  85. package/src/hooks/runs/useManageRuns/index.tsx +55 -0
  86. package/src/hooks/runs/usePolling/index.tsx +39 -0
  87. package/src/hooks/runs/usePolling/lib/refetch.ts +20 -0
  88. package/src/hooks/runs/useRuns/index.tsx +27 -0
  89. package/src/hooks/runs/useRuns/lib/getRuns.ts +16 -0
  90. package/src/hooks/runs/useRuns/lib/queryOptions/index.ts +21 -0
  91. package/src/hooks/runs/useRuns/lib/queryOptions/queryFn.ts +22 -0
  92. package/src/hooks/runs/useRuns/lib/queryOptions/queryKey.ts +3 -0
  93. package/src/hooks/threads/useThreadLifecycles/index.tsx +41 -0
  94. package/src/index.ts +12 -0
  95. package/src/lib/ai/index.ts +15 -0
  96. package/src/lib/index.ts +1 -0
  97. package/src/lib/messages/extendMessage.ts +25 -0
  98. package/src/lib/messages/order.ts +9 -0
  99. package/src/lib/optimistic/isOptimistic.ts +11 -0
  100. package/src/lib/optimistic/optimisticId.ts +5 -0
  101. package/src/lib/runSteps/getRunSteps/index.ts +18 -0
  102. package/src/lib/runs/isRunEditingMessage/index.ts +21 -0
  103. package/src/mutationFns.ts +3 -0
  104. package/src/queryFns.ts +2 -0
  105. package/src/queryKeys.ts +2 -0
  106. package/src/types/index.ts +38 -0
  107. package/tsconfig.json +10 -0
@@ -0,0 +1,39 @@
1
+ import {
2
+ useQueryClient,
3
+ UseInfiniteQueryOptions,
4
+ InfiniteData,
5
+ } from '@tanstack/react-query'
6
+ import { useIsRunActive } from '@/hooks/runs/useIsRunActive'
7
+ import { useInterval } from 'react-use'
8
+ import { refetch } from './lib/refetch'
9
+ import { MessagesPage, RunsPage } from '@/types'
10
+
11
+ type Args = {
12
+ messagesQueryOptions: UseInfiniteQueryOptions<InfiniteData<MessagesPage>>
13
+ runsQueryOptions: UseInfiniteQueryOptions<InfiniteData<RunsPage>>
14
+ }
15
+
16
+ export const usePolling = ({
17
+ messagesQueryOptions,
18
+ runsQueryOptions,
19
+ }: Args) => {
20
+ const queryClient = useQueryClient()
21
+
22
+ const isRunActiveProps = useIsRunActive({
23
+ messagesQueryOptions,
24
+ runsQueryOptions,
25
+ })
26
+
27
+ useInterval(() => {
28
+ refetch({
29
+ queryClient,
30
+ latestRun: isRunActiveProps.latestRun,
31
+ })
32
+
33
+ console.log('poll refetched')
34
+ },
35
+ isRunActiveProps.isRunActive ? 3000 : null
36
+ )
37
+
38
+ return null
39
+ }
@@ -0,0 +1,20 @@
1
+ import { Run } from '@/types'
2
+ import { queryKey as messagesQueryKey } from '@/hooks/messages/useMessages/lib/queryOptions/queryKey'
3
+ import { queryKey as runsQueryKey } from '@/hooks/runs/useRuns/lib/queryOptions/queryKey'
4
+
5
+ type Args = {
6
+ queryClient: any
7
+ latestRun: Run
8
+ }
9
+
10
+ export const refetch = async ({
11
+ queryClient,
12
+ latestRun,
13
+ }: Args) => {
14
+ await queryClient.invalidateQueries({
15
+ queryKey: messagesQueryKey(),
16
+ })
17
+ await queryClient.invalidateQueries({
18
+ queryKey: runsQueryKey(),
19
+ })
20
+ }
@@ -0,0 +1,27 @@
1
+ import { useMemo } from 'react'
2
+ import {
3
+ useInfiniteQuery,
4
+ UseInfiniteQueryOptions,
5
+ InfiniteData,
6
+ } from '@tanstack/react-query'
7
+ import { queryOptions as defaultQueryOptions } from './lib/queryOptions'
8
+ import { getRuns } from './lib/getRuns'
9
+ import { RunsPage } from '@/types'
10
+
11
+ type Args = {
12
+ runsQueryOptions: UseInfiniteQueryOptions<InfiniteData<RunsPage>>
13
+ }
14
+
15
+ export const useRuns = ({
16
+ runsQueryOptions,
17
+ }: Args) => {
18
+ const props = useInfiniteQuery({
19
+ ...defaultQueryOptions,
20
+ ...runsQueryOptions,
21
+ })
22
+
23
+ return useMemo(() => ({
24
+ ...props,
25
+ runs: getRuns({ data: props.data }),
26
+ }), [props])
27
+ }
@@ -0,0 +1,16 @@
1
+ import { InfiniteData } from '@tanstack/react-query'
2
+ import { RunsPage, Run } from '@/types'
3
+
4
+ type Args = {
5
+ data: InfiniteData<RunsPage> | undefined
6
+ }
7
+
8
+ export const getRuns = ({
9
+ data,
10
+ }: Args) => {
11
+ if (!data) return []
12
+
13
+ return data.pages.reduce<Run[]>((acc, page) => (
14
+ acc.concat(page.data)
15
+ ), [])
16
+ }
@@ -0,0 +1,21 @@
1
+ import _ from 'lodash'
2
+ import { RunsPage } from '@/types'
3
+ import { queryKey } from './queryKey'
4
+
5
+ export const queryOptions = {
6
+ queryKey: queryKey(),
7
+ // queryFn: ({
8
+ // pageParam,
9
+ // }: { pageParam?: string }) => (
10
+ // queryFn({
11
+ // assistantConversationId,
12
+ // })
13
+ // ),
14
+ initialPageParam: undefined,
15
+ getNextPageParam: (lastPage: RunsPage) => {
16
+ if (!lastPage.hasNextPage) return null
17
+
18
+ return lastPage.lastId
19
+ },
20
+ limit: 10,
21
+ }
@@ -0,0 +1,22 @@
1
+ import { RunsPage } from '@/types'
2
+ import { client } from '@/lib/ai'
3
+
4
+ type Args = {
5
+ threadId: string
6
+ }
7
+
8
+ export const queryFn = async ({
9
+ threadId,
10
+ }: Args): Promise<RunsPage> => {
11
+ const response = await client.beta.threads.runs.list(threadId, {
12
+ limit: 1,
13
+ })
14
+
15
+ return {
16
+ // @ts-ignore-next-line
17
+ data: response.data,
18
+ hasNextPage: response.hasNextPage(),
19
+ // @ts-ignore-next-line
20
+ lastId: response.body.last_id,
21
+ }
22
+ }
@@ -0,0 +1,3 @@
1
+ export const queryKey = () => (
2
+ ['runs']
3
+ )
@@ -0,0 +1,41 @@
1
+ import {
2
+ UseInfiniteQueryOptions,
3
+ UseMutationOptions,
4
+ InfiniteData,
5
+ } from '@tanstack/react-query'
6
+ import { useManageRuns } from '@/hooks/runs/useManageRuns'
7
+ import { usePolling } from '@/hooks/runs/usePolling'
8
+ import { useManageActions } from '@/hooks/actions/useManageActions'
9
+ import { MessagesPage, RunsPage, Run } from '@/types'
10
+
11
+ type Args = {
12
+ messagesQueryOptions: UseInfiniteQueryOptions<InfiniteData<MessagesPage>>
13
+ runsQueryOptions: UseInfiniteQueryOptions<InfiniteData<RunsPage>>
14
+ createRunMutationOptions: UseMutationOptions<{ run: Run }>
15
+ handleActionMutationOptions: UseMutationOptions<{ run: Run }>
16
+ }
17
+
18
+ export const useThreadLifecycles = ({
19
+ messagesQueryOptions,
20
+ runsQueryOptions,
21
+ createRunMutationOptions,
22
+ handleActionMutationOptions,
23
+ }: Args) => {
24
+ useManageRuns({
25
+ messagesQueryOptions,
26
+ runsQueryOptions,
27
+ createRunMutationOptions,
28
+ })
29
+
30
+ useManageActions({
31
+ runsQueryOptions,
32
+ handleActionMutationOptions,
33
+ })
34
+
35
+ usePolling({
36
+ messagesQueryOptions,
37
+ runsQueryOptions,
38
+ })
39
+
40
+ return null
41
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export { Messages } from '@/components/messages/Messages'
2
+ export { Form } from '@/components/messages/Form'
3
+ export {
4
+ QueryClient,
5
+ QueryClientProvider,
6
+ useQueryClient,
7
+ } from '@tanstack/react-query'
8
+ export { useThreadLifecycles } from '@/hooks/threads/useThreadLifecycles'
9
+ export { useMessages } from '@/hooks/messages/useMessages'
10
+ export { useLatestMessage } from '@/hooks/messages/useLatestMessage'
11
+ export { useCreateMessage } from '@/hooks/messages/useCreateMessage'
12
+ export { useIsRunActive } from '@/hooks/runs/useIsRunActive'
@@ -0,0 +1,15 @@
1
+ import OpenAI, { ClientOptions } from 'openai'
2
+
3
+ export const clientOptions: ClientOptions = {
4
+ apiKey: process.env.OPENAI_API_KEY!,
5
+ // @ts-ignore-next-line
6
+ fetch: (url: RequestInfo, init?: RequestInit): Promise<Response> => (
7
+ fetch(url, {
8
+ ...(init || {}),
9
+ // @ts-ignore-next-line
10
+ cache: 'no-store',
11
+ })
12
+ )
13
+ }
14
+
15
+ export const client = new OpenAI(clientOptions)
@@ -0,0 +1 @@
1
+ export { optimisticId } from '@/lib/optimistic/optimisticId'
@@ -0,0 +1,25 @@
1
+ import OpenAI from 'openai'
2
+ import { getRunSteps } from '@/lib/runSteps/getRunSteps'
3
+
4
+ type Args = {
5
+ message: OpenAI.Beta.Threads.Messages.ThreadMessage
6
+ }
7
+
8
+ export const extendMessage = async ({
9
+ message,
10
+ }: Args) => {
11
+ if (!message.run_id) {
12
+ return {
13
+ ...message,
14
+ runSteps: [],
15
+ }
16
+ }
17
+
18
+ return {
19
+ ...message,
20
+ runSteps: await getRunSteps({
21
+ threadId: message.thread_id,
22
+ runId: message.run_id,
23
+ }),
24
+ }
25
+ }
@@ -0,0 +1,9 @@
1
+ import { sort } from 'radash'
2
+ import { Message } from '@/types'
3
+
4
+ type Args = {
5
+ messages: Message[]
6
+ }
7
+
8
+ export const order = ({ messages }: Args) =>
9
+ sort(messages, m => m.created_at, true)
@@ -0,0 +1,11 @@
1
+ import _ from 'lodash'
2
+
3
+ type Args = {
4
+ id: string
5
+ }
6
+
7
+ export const isOptimistic = ({
8
+ id,
9
+ }: Args) => (
10
+ _.startsWith(id, '-')
11
+ )
@@ -0,0 +1,5 @@
1
+ import { uid } from 'radash'
2
+
3
+ export const optimisticId = () => (
4
+ `-${uid(24)}`
5
+ )
@@ -0,0 +1,18 @@
1
+ import { client } from '@/lib/ai'
2
+
3
+ type Args = {
4
+ threadId: string
5
+ runId: string
6
+ }
7
+
8
+ export const getRunSteps = async ({
9
+ threadId,
10
+ runId,
11
+ }: Args) => {
12
+ const runStepsResponse = await client.beta.threads.runs.steps.list(
13
+ threadId,
14
+ runId,
15
+ )
16
+
17
+ return runStepsResponse.data
18
+ }
@@ -0,0 +1,21 @@
1
+ import OpenAI from 'openai'
2
+ import _ from 'lodash'
3
+
4
+ type Args = {
5
+ message: OpenAI.Beta.Threads.ThreadMessage
6
+ }
7
+
8
+ export const isRunEditingMessage = ({
9
+ message,
10
+ }: Args) => {
11
+ if (!message) return false
12
+ if (message.role === 'user') return false
13
+ if (!message.run_id) return false
14
+
15
+ const hasContent = _.some(message.content, (content) => (
16
+ content.type !== 'text' ||
17
+ (content.type === 'text' && content.text?.value !== '')
18
+ ))
19
+
20
+ return !hasContent
21
+ }
@@ -0,0 +1,3 @@
1
+ export { mutationFn as createMessageMutationFn } from '@/hooks/messages/useCreateMessage/lib/mutationOptions/mutationFn'
2
+ export { mutationFn as handleActionMutationFn } from '@/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn'
3
+ export { mutationFn as createRunMutationFn } from '@/hooks/runs/useCreateRun/lib/mutationOptions/mutationFn'
@@ -0,0 +1,2 @@
1
+ export { queryFn as messagesQueryFn } from '@/hooks/messages/useMessages/lib/queryOptions/queryFn'
2
+ export { queryFn as runsQueryFn } from '@/hooks/runs/useRuns/lib/queryOptions/queryFn'
@@ -0,0 +1,2 @@
1
+ export { queryKey as messagesQueryKey } from '@/hooks/messages/useMessages/lib/queryOptions/queryKey'
2
+ export { queryKey as runsQueryKey } from '@/hooks/runs/useRuns/lib/queryOptions/queryKey'
@@ -0,0 +1,38 @@
1
+ import OpenAI from 'openai'
2
+
3
+ export type Message = OpenAI.Beta.Threads.Messages.ThreadMessage & {
4
+ runSteps: OpenAI.Beta.Threads.Runs.RunStep[]
5
+ }
6
+
7
+ export type MessagesPage = {
8
+ data: Message[],
9
+ hasNextPage: boolean,
10
+ lastId: string
11
+ }
12
+
13
+ export type RunStepsPage = {
14
+ data: OpenAI.Beta.Threads.Runs.RunStep[],
15
+ hasNextPage: boolean,
16
+ lastId: string
17
+ }
18
+
19
+ export type MessageGroup = {
20
+ id: string
21
+ role: "user" | "assistant"
22
+ messages: Message[]
23
+ createdAt: number
24
+ }
25
+
26
+ export type Run = OpenAI.Beta.Threads.Runs.Run
27
+
28
+ export type RunsPage = {
29
+ data: Run[],
30
+ hasNextPage: boolean,
31
+ lastId: string
32
+ }
33
+
34
+ type Fn = (args: any) => Promise<string>
35
+
36
+ export type Functions = {
37
+ [key: string]: Fn
38
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist",
6
+ "paths": {
7
+ "@/*": ["./src/*"]
8
+ }
9
+ }
10
+ }