@rolder/kit 3.0.0-alpha.48 → 3.0.0-alpha.49

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.
@@ -1,2 +1,5 @@
1
- export * from './messages';
1
+ export * from './messageIds';
2
+ export * from './messagesMap';
3
+ export * from './send';
2
4
  export * from './states';
5
+ export * from './useInitChat';
@@ -1,2 +1,5 @@
1
- export * from "./messages.js";
1
+ export * from "./messageIds.js";
2
+ export * from "./messagesMap.js";
3
+ export * from "./send.js";
2
4
  export * from "./states.js";
5
+ export * from "./useInitChat.js";
@@ -0,0 +1,8 @@
1
+ import type { UIMessage } from 'ai';
2
+ export declare const $chatMessageIds: import("nanostores").PreinitializedWritableAtom<string[]> & object;
3
+ export declare const getChatMessages: <T extends UIMessage>() => T[];
4
+ export declare const useChatMessageIds: () => string[];
5
+ export declare const addChatMessage: (newMessage: UIMessage) => void;
6
+ export declare const setChatMessages: <T extends UIMessage>(messages: T[]) => void;
7
+ export declare const getIsEmpty: () => boolean;
8
+ export declare const useIsEmpty: () => boolean;
@@ -0,0 +1,36 @@
1
+ import { useStore } from "@nanostores/react";
2
+ import { atom, computed, onSet } from "nanostores";
3
+ import { $chatMessages } from "./messagesMap.js";
4
+ const $chatMessageIds = atom([]);
5
+ const getChatMessages = ()=>$chatMessageIds.get().map((id)=>$chatMessages.get()[id]);
6
+ const useChatMessageIds = ()=>useStore($chatMessageIds);
7
+ onSet($chatMessageIds, ({ newValue, abort })=>{
8
+ const currentValue = $chatMessageIds.get();
9
+ if (currentValue.length === newValue.length && currentValue.every((id, index)=>id === newValue[index])) abort();
10
+ });
11
+ const addChatMessage = (newMessage)=>{
12
+ $chatMessageIds.set([
13
+ ...$chatMessageIds.get(),
14
+ newMessage.id
15
+ ]);
16
+ $chatMessages.set({
17
+ ...$chatMessages.get(),
18
+ [newMessage.id]: newMessage
19
+ });
20
+ };
21
+ const setChatMessages = (messages)=>{
22
+ const newIds = messages.map((msg)=>msg.id);
23
+ $chatMessageIds.set(newIds);
24
+ const currentMessages = $chatMessages.get();
25
+ for (const msg of messages){
26
+ const currentMsg = currentMessages[msg.id];
27
+ if (currentMsg) {
28
+ const isChanged = JSON.stringify(currentMsg) !== JSON.stringify(msg);
29
+ if (isChanged) $chatMessages.setKey(msg.id, msg);
30
+ } else $chatMessages.setKey(msg.id, msg);
31
+ }
32
+ };
33
+ const $isEmpty = computed($chatMessageIds, (messages)=>0 === messages.length);
34
+ const getIsEmpty = ()=>$isEmpty.get();
35
+ const useIsEmpty = ()=>useStore($isEmpty);
36
+ export { $chatMessageIds, addChatMessage, getChatMessages, getIsEmpty, setChatMessages, useChatMessageIds, useIsEmpty };
@@ -0,0 +1,5 @@
1
+ import type { UIDataTypes, UIMessage, UIMessagePart, UITools } from 'ai';
2
+ export declare const $chatMessages: import("nanostores").PreinitializedMapStore<Record<string, UIMessage<unknown, UIDataTypes, UITools>>> & object;
3
+ export declare const getChatMessage: (messageId: string) => UIMessage<unknown, UIDataTypes, UITools>;
4
+ export declare const useChatMessage: <T extends UIMessage>(messageId: string) => T;
5
+ export declare const useChatMessagePart: <M extends UIMessage, T extends UIMessagePart<UIDataTypes, UITools>>(messageId: string, type?: T["type"]) => T | undefined;
@@ -0,0 +1,15 @@
1
+ import { useStore } from "@nanostores/react";
2
+ import { map } from "nanostores";
3
+ const $chatMessages = map({});
4
+ const getChatMessage = (messageId)=>$chatMessages.get()[messageId];
5
+ const useChatMessage = (messageId)=>useStore($chatMessages, {
6
+ keys: [
7
+ messageId
8
+ ]
9
+ })[messageId];
10
+ const useChatMessagePart = (messageId, type)=>{
11
+ const message = useChatMessage(messageId);
12
+ const part = message?.parts?.find((i)=>i.type === type);
13
+ return part;
14
+ };
15
+ export { $chatMessages, getChatMessage, useChatMessage, useChatMessagePart };
@@ -0,0 +1,14 @@
1
+ import type { FileUIPart } from 'ai';
2
+ export type SendMessage = ({ text, file }: {
3
+ text: string;
4
+ file?: FileUIPart;
5
+ }, data?: Record<string, unknown>) => Promise<void>;
6
+ type SendSdkMessage = ({ text, files }: {
7
+ text: string;
8
+ files?: FileUIPart[];
9
+ }, { body }: {
10
+ body?: Record<string, unknown>;
11
+ }) => Promise<void>;
12
+ export declare const setChatSendMessage: (sendMessage: SendSdkMessage) => void;
13
+ export declare const sendMessage: SendMessage;
14
+ export {};
@@ -0,0 +1,16 @@
1
+ import { atom } from "nanostores";
2
+ const $chatSendMessage = atom();
3
+ const getChatSendMessage = ()=>$chatSendMessage.get();
4
+ const setChatSendMessage = (sendMessage)=>$chatSendMessage.set(sendMessage);
5
+ const send_sendMessage = async ({ text, file }, data)=>{
6
+ const files = file ? [
7
+ file
8
+ ] : [];
9
+ await getChatSendMessage()?.({
10
+ text,
11
+ files
12
+ }, {
13
+ body: data
14
+ });
15
+ };
16
+ export { send_sendMessage as sendMessage, setChatSendMessage };
@@ -0,0 +1,3 @@
1
+ import { type UseChatOptions } from '@ai-sdk/react';
2
+ import type { ChatInit, UIMessage } from 'ai';
3
+ export declare const useInitChat: <T extends UIMessage>(props?: UseChatOptions<T> & ChatInit<T>) => void;
@@ -0,0 +1,40 @@
1
+ import { useChat } from "@ai-sdk/react";
2
+ import { notifications } from "@mantine/notifications";
3
+ import { useEffect } from "react";
4
+ import { setChatMessages } from "./messageIds.js";
5
+ import { setChatSendMessage } from "./send.js";
6
+ import { setChatError, setChatStatus } from "./states.js";
7
+ const useInitChat = (props)=>{
8
+ const { messages, status, error, sendMessage } = useChat({
9
+ onError: (e)=>{
10
+ notifications.show({
11
+ title: 'Ошибка сервера ИИ',
12
+ message: e.message,
13
+ color: 'red',
14
+ autoClose: false
15
+ });
16
+ },
17
+ ...props
18
+ });
19
+ useEffect(()=>{
20
+ setChatStatus(status);
21
+ }, [
22
+ status
23
+ ]);
24
+ useEffect(()=>{
25
+ setChatError(error?.message);
26
+ }, [
27
+ error?.message
28
+ ]);
29
+ useEffect(()=>{
30
+ setChatSendMessage(sendMessage);
31
+ }, [
32
+ sendMessage
33
+ ]);
34
+ useEffect(()=>{
35
+ setChatMessages(messages);
36
+ }, [
37
+ messages
38
+ ]);
39
+ };
40
+ export { useInitChat };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolder/kit",
3
- "version": "3.0.0-alpha.48",
3
+ "version": "3.0.0-alpha.49",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -1,19 +0,0 @@
1
- import { type UseChatOptions } from '@ai-sdk/react';
2
- import type { ChatInit, FileUIPart, UIDataTypes, UIMessage, UIMessagePart, UITools } from 'ai';
3
- export declare const $chatMessageIds: import("nanostores").PreinitializedWritableAtom<string[]> & object;
4
- export declare const $chatMessages: import("nanostores").PreinitializedMapStore<Record<string, UIMessage<unknown, UIDataTypes, UITools>>> & object;
5
- export declare const getChatMessage: (messageId: string) => UIMessage<unknown, UIDataTypes, UITools>;
6
- export declare const addChatMessage: (newMessage: UIMessage) => void;
7
- export declare const getChatMessages: <T extends UIMessage>() => T[];
8
- export declare const setChatMessages: <T extends UIMessage>(messages: T[]) => void;
9
- export declare const getIsEmpty: () => boolean;
10
- export declare const useIsEmpty: () => boolean;
11
- export declare const useInitChat: <T extends UIMessage>(props?: UseChatOptions<T> & ChatInit<T>) => void;
12
- export declare const useChatMessageIds: () => string[];
13
- export declare const useChatMessage: <T extends UIMessage>(messageId: string) => T;
14
- export declare const useChatMessagePart: <M extends UIMessage, T extends UIMessagePart<UIDataTypes, UITools>>(messageId: string, type?: T["type"]) => T | undefined;
15
- export type SendMessage = ({ text, file }: {
16
- text: string;
17
- file?: FileUIPart;
18
- }, data?: Record<string, unknown>) => Promise<void>;
19
- export declare const sendMessage: SendMessage;
@@ -1,92 +0,0 @@
1
- import { useChat } from "@ai-sdk/react";
2
- import { notifications } from "@mantine/notifications";
3
- import { useStore } from "@nanostores/react";
4
- import { atom, computed, map, onSet } from "nanostores";
5
- import { useEffect } from "react";
6
- import { setChatError, setChatStatus } from "./states.js";
7
- const $chatMessageIds = atom([]);
8
- onSet($chatMessageIds, ({ newValue, abort })=>{
9
- const currentValue = $chatMessageIds.get();
10
- if (currentValue.length === newValue.length && currentValue.every((id, index)=>id === newValue[index])) abort();
11
- });
12
- const $chatMessages = map({});
13
- const getChatMessage = (messageId)=>$chatMessages.get()[messageId];
14
- const addChatMessage = (newMessage)=>{
15
- $chatMessageIds.set([
16
- ...$chatMessageIds.get(),
17
- newMessage.id
18
- ]);
19
- $chatMessages.set({
20
- ...$chatMessages.get(),
21
- [newMessage.id]: newMessage
22
- });
23
- };
24
- const getChatMessages = ()=>$chatMessageIds.get().map((id)=>$chatMessages.get()[id]);
25
- const setChatMessages = (messages)=>{
26
- $chatMessageIds.set(messages.map((msg)=>msg.id));
27
- const messagesMap = {};
28
- for (const msg of messages)messagesMap[msg.id] = msg;
29
- $chatMessages.set(messagesMap);
30
- };
31
- const $isEmpty = computed($chatMessageIds, (messages)=>0 === messages.length);
32
- const getIsEmpty = ()=>$isEmpty.get();
33
- const useIsEmpty = ()=>useStore($isEmpty);
34
- const useInitChat = (props)=>{
35
- const { messages, status, error, sendMessage } = useChat({
36
- onError: (e)=>{
37
- notifications.show({
38
- title: 'Ошибка сервера ИИ',
39
- message: e.message,
40
- color: 'red',
41
- autoClose: false
42
- });
43
- },
44
- ...props
45
- });
46
- useEffect(()=>{
47
- setChatStatus(status);
48
- }, [
49
- status
50
- ]);
51
- useEffect(()=>{
52
- setChatError(error?.message);
53
- }, [
54
- error?.message
55
- ]);
56
- useEffect(()=>{
57
- setChatSendMessage(sendMessage);
58
- }, [
59
- sendMessage
60
- ]);
61
- useEffect(()=>{
62
- setChatMessages(messages);
63
- }, [
64
- messages
65
- ]);
66
- };
67
- const useChatMessageIds = ()=>useStore($chatMessageIds);
68
- const useChatMessage = (messageId)=>useStore($chatMessages, {
69
- keys: [
70
- messageId
71
- ]
72
- })[messageId];
73
- const useChatMessagePart = (messageId, type)=>{
74
- const message = useChatMessage(messageId);
75
- const part = message?.parts?.find((i)=>i.type === type);
76
- return part;
77
- };
78
- const $chatSendMessage = atom();
79
- const getChatSendMessage = ()=>$chatSendMessage.get();
80
- const setChatSendMessage = (sendMessage)=>$chatSendMessage.set(sendMessage);
81
- const messages_sendMessage = async ({ text, file }, data)=>{
82
- const files = file ? [
83
- file
84
- ] : [];
85
- await getChatSendMessage()?.({
86
- text,
87
- files
88
- }, {
89
- body: data
90
- });
91
- };
92
- export { $chatMessageIds, $chatMessages, addChatMessage, getChatMessage, getChatMessages, getIsEmpty, messages_sendMessage as sendMessage, setChatMessages, useChatMessage, useChatMessageIds, useChatMessagePart, useInitChat, useIsEmpty };