@27works/chat-core 0.1.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.
- package/README.md +776 -0
- package/dist/components/index.d.ts +18 -0
- package/dist/components/index.js +609 -0
- package/dist/contexts/index.d.ts +11 -0
- package/dist/contexts/index.js +175 -0
- package/dist/hooks/index.d.ts +76 -0
- package/dist/hooks/index.js +406 -0
- package/dist/server/index.d.ts +227 -0
- package/dist/server/index.js +15561 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.js +0 -0
- package/dist/utils/index.d.ts +19 -0
- package/dist/utils/index.js +56 -0
- package/package.json +72 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { ToolSet, UIMessageStreamWriter, Tool, ToolExecutionOptions } from 'ai';
|
|
2
|
+
import { Ratelimit } from '@upstash/ratelimit';
|
|
3
|
+
import * as _supabase_supabase_js from '@supabase/supabase-js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fetch a chat session and validate ownership in one call.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} chatId
|
|
9
|
+
* @param {Object} options
|
|
10
|
+
* @param {string} options.select - Columns to select (default: '*')
|
|
11
|
+
* @param {string} options.accessMode - 'owner-only' | 'owner-or-public' | 'none'
|
|
12
|
+
* @param {string} options.forbiddenMessage - Custom 403 message
|
|
13
|
+
* @returns {{ chat: Object, ownership: Object }}
|
|
14
|
+
*/
|
|
15
|
+
declare function fetchChatWithOwnership(chatId: string, options?: {
|
|
16
|
+
select: string;
|
|
17
|
+
accessMode: string;
|
|
18
|
+
forbiddenMessage: string;
|
|
19
|
+
}): {
|
|
20
|
+
chat: any;
|
|
21
|
+
ownership: any;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
declare function createRagHandler({ model, embeddingModel, systemPrompt, tools, toolHandlers, useCache, ragOptions }: {
|
|
25
|
+
model: any;
|
|
26
|
+
embeddingModel: any;
|
|
27
|
+
systemPrompt: any;
|
|
28
|
+
tools: any;
|
|
29
|
+
toolHandlers?: {};
|
|
30
|
+
useCache?: boolean;
|
|
31
|
+
ragOptions?: {};
|
|
32
|
+
}): {
|
|
33
|
+
POST: (req: any) => Promise<Response>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a POST handler that proxies link-click beacons from the browser
|
|
38
|
+
* to Caruuto, server-to-server, with the project's API key — sendBeacon
|
|
39
|
+
* can't carry custom headers, so the browser can't call Caruuto directly.
|
|
40
|
+
* Mirrors createRagHandler's factory shape so every consuming app gets the
|
|
41
|
+
* same proxy with no per-app logic.
|
|
42
|
+
*
|
|
43
|
+
* Always responds 204: sendBeacon discards the response, and a tracking
|
|
44
|
+
* failure must never surface to the user — errors are logged and swallowed.
|
|
45
|
+
*/
|
|
46
|
+
declare function createLinkClickHandler(): {
|
|
47
|
+
POST: (req: any) => Promise<Response>;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
declare function handleAPIError(error: any): Response;
|
|
51
|
+
declare namespace apiErrors {
|
|
52
|
+
function rateLimit(retryAfter?: number): {
|
|
53
|
+
message: string;
|
|
54
|
+
code: string;
|
|
55
|
+
statusCode: number;
|
|
56
|
+
retryAfter: number;
|
|
57
|
+
};
|
|
58
|
+
function notFound(resource?: string): {
|
|
59
|
+
message: string;
|
|
60
|
+
code: string;
|
|
61
|
+
statusCode: number;
|
|
62
|
+
};
|
|
63
|
+
function validation(message: any): {
|
|
64
|
+
message: any;
|
|
65
|
+
code: string;
|
|
66
|
+
statusCode: number;
|
|
67
|
+
};
|
|
68
|
+
function unauthorized(): {
|
|
69
|
+
message: string;
|
|
70
|
+
code: string;
|
|
71
|
+
statusCode: number;
|
|
72
|
+
};
|
|
73
|
+
function forbidden(message?: string): {
|
|
74
|
+
message: string;
|
|
75
|
+
code: string;
|
|
76
|
+
statusCode: number;
|
|
77
|
+
};
|
|
78
|
+
function openai(_originalError: any): {
|
|
79
|
+
message: string;
|
|
80
|
+
code: string;
|
|
81
|
+
statusCode: number;
|
|
82
|
+
};
|
|
83
|
+
function database(_originalError: any): {
|
|
84
|
+
message: string;
|
|
85
|
+
code: string;
|
|
86
|
+
statusCode: number;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Normalize question text for consistent caching.
|
|
92
|
+
*/
|
|
93
|
+
declare function normalizeQuestion(text: any): string;
|
|
94
|
+
/**
|
|
95
|
+
* Create SHA-256 hash of normalized question + model ID.
|
|
96
|
+
* modelId must be supplied by the caller (no hardcoded default).
|
|
97
|
+
*/
|
|
98
|
+
declare function hashQuestion(normalizedText: any, modelId: any): any;
|
|
99
|
+
/**
|
|
100
|
+
* Check if a message is the first user question in a conversation.
|
|
101
|
+
*/
|
|
102
|
+
declare function isFirstQuestion(messages: any): boolean;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Create a new chat session in the database.
|
|
106
|
+
*/
|
|
107
|
+
declare function createChatSession({ chatId, projectId, messages, ownerId, forkedFrom }: {
|
|
108
|
+
chatId: any;
|
|
109
|
+
projectId: any;
|
|
110
|
+
messages: any;
|
|
111
|
+
ownerId: any;
|
|
112
|
+
forkedFrom?: any;
|
|
113
|
+
}): Promise<any>;
|
|
114
|
+
/**
|
|
115
|
+
* Save or update a chat session in the database.
|
|
116
|
+
* Handles usage token array accumulation and owner/visibility defaults for new chats.
|
|
117
|
+
*/
|
|
118
|
+
declare function saveChat({ chatId, messages, usage, ownerId, projectId }: {
|
|
119
|
+
chatId: any;
|
|
120
|
+
messages: any;
|
|
121
|
+
usage: any;
|
|
122
|
+
ownerId: any;
|
|
123
|
+
projectId: any;
|
|
124
|
+
}): Promise<any>;
|
|
125
|
+
|
|
126
|
+
type HumanInTheLoopUIMessage = any;
|
|
127
|
+
/**
|
|
128
|
+
* Process tool calls that required human confirmation on the client.
|
|
129
|
+
* Executes the tool's server-side function when the user approved.
|
|
130
|
+
*/
|
|
131
|
+
declare function processToolCalls<Tools extends ToolSet, ExecutableTools extends {
|
|
132
|
+
[Tool in keyof Tools as Tools[Tool] extends {
|
|
133
|
+
execute: Function;
|
|
134
|
+
} ? never : Tool]: Tools[Tool];
|
|
135
|
+
}>({ writer, messages }: {
|
|
136
|
+
tools: Tools;
|
|
137
|
+
writer: UIMessageStreamWriter;
|
|
138
|
+
messages: HumanInTheLoopUIMessage[];
|
|
139
|
+
}, executeFunctions: {
|
|
140
|
+
[K in keyof Tools & keyof ExecutableTools]?: (args: ExecutableTools[K] extends Tool<infer P> ? P : never, context: ToolExecutionOptions) => Promise<any>;
|
|
141
|
+
}): Promise<HumanInTheLoopUIMessage[]>;
|
|
142
|
+
/**
|
|
143
|
+
* Sanitize messages before saving to database.
|
|
144
|
+
* Strips PII from emailCapture tool outputs — the owner sees the personalized
|
|
145
|
+
* confirmation in real-time, but the database only stores a generic message.
|
|
146
|
+
*/
|
|
147
|
+
declare const sanitizeMessagesForStorage: (messages: any[]) => any[];
|
|
148
|
+
|
|
149
|
+
declare function getOwnerId(): Promise<any>;
|
|
150
|
+
declare function getOwnerIdIfExists(): Promise<string>;
|
|
151
|
+
/**
|
|
152
|
+
* Validate ownership of a chat by comparing the browser cookie with the DB value.
|
|
153
|
+
*/
|
|
154
|
+
declare function validateOwnership(chatId: any, dbOwnerId: any): Promise<{
|
|
155
|
+
isOwner: boolean;
|
|
156
|
+
reason: string;
|
|
157
|
+
}>;
|
|
158
|
+
|
|
159
|
+
declare const chatRateLimit: Ratelimit;
|
|
160
|
+
declare function checkRateLimit(req: any, rateLimiter: any): Promise<Response>;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Inject pre-built service clients into chat-core.
|
|
164
|
+
*
|
|
165
|
+
* Call this once at app startup (e.g. in lib/server/services.js) before any
|
|
166
|
+
* chat-core server functions run. If not called, the package falls back to
|
|
167
|
+
* the SUPABASE_URL / SUPABASE_SERVICE_ROLE_KEY / CARUUTO_URL / CARUUTO_API_KEY
|
|
168
|
+
* environment variables — existing apps work without any changes.
|
|
169
|
+
*
|
|
170
|
+
* @param {object} clients
|
|
171
|
+
* @param {import('@supabase/supabase-js').SupabaseClient} [clients.supabase]
|
|
172
|
+
* @param {import('@caruuto/caruuto-js').CaruutoClient} [clients.caruuto]
|
|
173
|
+
*/
|
|
174
|
+
declare function configure({ supabase, caruuto }?: {
|
|
175
|
+
supabase?: _supabase_supabase_js.SupabaseClient;
|
|
176
|
+
caruuto?: any;
|
|
177
|
+
}): void;
|
|
178
|
+
declare const supabaseAdmin: {};
|
|
179
|
+
declare const caruutoAdmin: {};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolve the project_id for the current tenant from the CARUUTO_PROJECT env var.
|
|
183
|
+
*/
|
|
184
|
+
declare function getProjectId(): Promise<any>;
|
|
185
|
+
/**
|
|
186
|
+
* Extract media items with metadata from a document's properties.
|
|
187
|
+
* Handles parallel arrays format (images + imageMetadata) and legacy formats.
|
|
188
|
+
*/
|
|
189
|
+
declare function extractMediaWithMetadata(imagesField: any, imageMetadataField?: any[]): ({
|
|
190
|
+
mediaId: any;
|
|
191
|
+
url: string;
|
|
192
|
+
metadata: {
|
|
193
|
+
caption?: undefined;
|
|
194
|
+
altText?: undefined;
|
|
195
|
+
credit?: undefined;
|
|
196
|
+
tags?: undefined;
|
|
197
|
+
};
|
|
198
|
+
} | {
|
|
199
|
+
mediaId: any;
|
|
200
|
+
metadata: {
|
|
201
|
+
caption: any;
|
|
202
|
+
altText: any;
|
|
203
|
+
credit: any;
|
|
204
|
+
tags: any;
|
|
205
|
+
};
|
|
206
|
+
url?: undefined;
|
|
207
|
+
})[];
|
|
208
|
+
/**
|
|
209
|
+
* Resolve media URLs and merge with metadata from documents.
|
|
210
|
+
*/
|
|
211
|
+
declare function resolveMediaUrls(mediaItems: any): Promise<Map<any, any>>;
|
|
212
|
+
/**
|
|
213
|
+
* Fetch parent documents by content IDs.
|
|
214
|
+
*/
|
|
215
|
+
declare function getParentDocuments(sourceContentIds: any): Promise<Map<any, any>>;
|
|
216
|
+
/**
|
|
217
|
+
* Split a full name string into firstName and lastName.
|
|
218
|
+
*/
|
|
219
|
+
declare function splitName(fullName: any): {
|
|
220
|
+
firstName: any;
|
|
221
|
+
lastName: any;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
declare function createCachedResponseStream(cachedAnswer: any, questionId: any, chatId: any, originalMessages: any, ownerId: any, projectId: any): Promise<Response>;
|
|
225
|
+
declare function createStaticErrorStream(errorMessage: any): Response;
|
|
226
|
+
|
|
227
|
+
export { apiErrors, caruutoAdmin, chatRateLimit, checkRateLimit, configure, createCachedResponseStream, createChatSession, createLinkClickHandler, createRagHandler, createStaticErrorStream, extractMediaWithMetadata, fetchChatWithOwnership, getOwnerId, getOwnerIdIfExists, getParentDocuments, getProjectId, handleAPIError, hashQuestion, isFirstQuestion, normalizeQuestion, processToolCalls, resolveMediaUrls, sanitizeMessagesForStorage, saveChat, splitName, supabaseAdmin, validateOwnership };
|