-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
164 lines (154 loc) · 5.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {
AssistantChat,
ToolsDispatcher,
FirebaseQueueTaskScheduler,
ChatData,
ChatState,
TaskScheduler,
CommandScheduler,
ToolsContinuationScheduler,
toolContinuationSchedulerFactory, toolContinuationDispatcherFactory
} from "@motorro/firebase-ai-chat-core";
import {AiWrapper} from "./aichat/AiWrapper";
import {OpenAiChatWorker} from "./aichat/OpenAiChatWorker";
import {Functions} from "firebase-admin/lib/functions";
import {firestore} from "firebase-admin";
import Firestore = firestore.Firestore;
import {OpenAiAssistantConfig} from "./aichat/data/OpenAiAssistantConfig";
import {OpenAICommandScheduler} from "./aichat/OpenAICommandScheduler";
import OpenAI from "openai";
import {OpenAiWrapper} from "./aichat/OpenAiWrapper";
export {
AssistantChat,
ChatData,
ChatState,
ChatStatus,
ChatMessage,
Meta,
ChatMeta,
Logger,
setLogger,
TaskScheduler,
CommandScheduler,
Collections,
SystemInstructions,
AiExample,
AiResponseExample,
AiFunctionCallExample,
printAiExample
} from "@motorro/firebase-ai-chat-core";
export {
ChatDispatchData,
FunctionSuccess,
ReducerSuccess,
DispatchError,
DispatchResult,
ToolDispatcherReturnValue,
ToolsDispatcher,
isDispatchResult,
getDispatchError,
isDispatchError,
getFunctionSuccess,
getReducerSuccess,
isFunctionSuccess,
isReducerSuccess
} from "@motorro/firebase-ai-chat-core";
export {ChatCommand, BoundChatCommand, isChatCommand, isBoundChatCommand} from "@motorro/firebase-ai-chat-core";
export {FirebaseQueueTaskScheduler} from "@motorro/firebase-ai-chat-core";
export {Continuation, SuspendedContinuation, ResolvedContinuation} from "@motorro/firebase-ai-chat-core";
export {
ContinuationRequest,
ContinuationCommand,
ToolCall,
ContinuationRequestToolData,
ToolCallRequest,
ToolCallResponse,
ToolCallsResult,
isContinuationRequest,
isContinuationCommand,
isContinuationCommandRequest
} from "@motorro/firebase-ai-chat-core";
export {
AiWrapper,
OpenAiChatWorker
};
export {OpenAiAssistantConfig} from "./aichat/data/OpenAiAssistantConfig";
export {OpenAiChatCommand} from "./aichat/data/OpenAiChatCommand";
/**
* Chat state for OpenAI chats
*/
export type OpenAiChatState<DATA extends ChatData> = ChatState<OpenAiAssistantConfig, DATA>
/**
* AI chat components to build Firestore functions
*/
export interface AiChat {
/**
* Creates default command scheduler that schedules commands to dispatch
* @param queueName Provides queue name to schedule tasks to
* @param taskScheduler Provides task-schedule to put tasks to queue
* @return Array of default task schedulers for this library
*/
createDefaultCommandSchedulers: (queueName: string, taskScheduler: TaskScheduler) => ReadonlyArray<CommandScheduler>
/**
* Chat user-facing callable functions
* @param queueName Chat dispatcher function (queue) name to dispatch work
* @return Chat interface
* @see worker
*/
chat<DATA extends ChatData>(queueName: string): AssistantChat<DATA>
/**
* Chat worker to use in Firebase tasks
* @param openAi OpenAI instance
* @param dispatchers Tools dispatchers
* @return Worker interface
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
worker(openAi: OpenAI, dispatchers: Readonly<Record<string, ToolsDispatcher<any>>>): OpenAiChatWorker
/**
* Creates a tool continuation scheduler to continue tools dispatch
* @param queueName The name of the queue the dispatch will be continued on
* @returns Continuation scheduler to resume tools dispatch
*/
continuationScheduler<DATA extends ChatData>(queueName: string): ToolsContinuationScheduler<DATA>
}
/**
* Chat tools factory
* @param firestore Firestore instance
* @param functions Functions instance
* @param location Function location
* @param taskScheduler Task scheduler that puts tasks to queue
* @return Chat tools interface
*/
export function factory(
firestore: Firestore,
functions: Functions,
location: string,
taskScheduler?: TaskScheduler
): AiChat {
const _taskScheduler = taskScheduler || new FirebaseQueueTaskScheduler(functions, location);
const _continuationSchedulerFactory = toolContinuationSchedulerFactory(firestore, _taskScheduler);
function defaultSchedulers(queueName: string, taskScheduler: TaskScheduler): ReadonlyArray<CommandScheduler> {
return [new OpenAICommandScheduler(queueName, taskScheduler)];
}
return {
createDefaultCommandSchedulers: defaultSchedulers,
chat: function<DATA extends ChatData>(
queueName: string,
commandSchedulers: (queueName: string, taskScheduler: TaskScheduler) => ReadonlyArray<CommandScheduler> = defaultSchedulers
): AssistantChat<DATA> {
return new AssistantChat<DATA>(firestore, commandSchedulers(queueName, _taskScheduler));
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
worker(openAi: OpenAI, dispatchers: Readonly<Record<string, ToolsDispatcher<any>>>): OpenAiChatWorker {
return new OpenAiChatWorker(
firestore,
_taskScheduler,
new OpenAiWrapper(openAi),
toolContinuationDispatcherFactory(firestore, dispatchers, _taskScheduler)
);
},
continuationScheduler<DATA extends ChatData>(queueName: string): ToolsContinuationScheduler<DATA> {
return _continuationSchedulerFactory.create(queueName);
}
};
}