Create ChatData model
This commit is contained in:
parent
48dc0afe1a
commit
a384342fb1
92
lib/4nk/models/ChatData.ts
Normal file
92
lib/4nk/models/ChatData.ts
Normal file
@ -0,0 +1,92 @@
|
||||
import type { RoleDefinition } from "./Roles";
|
||||
|
||||
export interface ChatData {
|
||||
type: string;
|
||||
content: string;
|
||||
metadata: {
|
||||
createdAt: string;
|
||||
lastModified: string;
|
||||
sender: string;
|
||||
recipient: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function isChatData(data: any): data is ChatData {
|
||||
if (typeof data !== 'object' || data === null) return false;
|
||||
|
||||
// Check required fields
|
||||
if (typeof data.type !== 'string' || typeof data.content !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const validTypes = ['text'];
|
||||
if (!validTypes.includes(data.type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check metadata structure
|
||||
if (typeof data.metadata !== 'object' || data.metadata === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const requiredMetadataFields = ['createdAt', 'lastModified', 'sender', 'recipient'];
|
||||
for (const field of requiredMetadataFields) {
|
||||
if (typeof data.metadata[field] !== 'string') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const emptyChatData: ChatData = {
|
||||
type: '',
|
||||
content: '',
|
||||
metadata: {
|
||||
createdAt: '',
|
||||
lastModified: '',
|
||||
sender: '',
|
||||
recipient: '',
|
||||
}
|
||||
};
|
||||
|
||||
const chatDataFields: string[] = Object.keys(emptyChatData);
|
||||
|
||||
const ChatPublicFields: string[] = [];
|
||||
|
||||
// Messages and metadata are private by default
|
||||
export const ChatPrivateFields = [
|
||||
...chatDataFields.filter(key => !ChatPublicFields.includes(key))
|
||||
];
|
||||
|
||||
export interface ChatCreated {
|
||||
processId: string,
|
||||
process: any, // Process
|
||||
data: ChatData,
|
||||
}
|
||||
|
||||
export function setDefaultChatRoles(ownerId: string, recipientId: string): Record<string, RoleDefinition> {
|
||||
return {
|
||||
demiurge: {
|
||||
members: [ownerId, recipientId],
|
||||
validation_rules: [],
|
||||
storages: []
|
||||
},
|
||||
owner: {
|
||||
members: [ownerId, recipientId],
|
||||
validation_rules: [
|
||||
{
|
||||
quorum: 0.5,
|
||||
fields: [...chatDataFields, 'roles'],
|
||||
min_sig_member: 1,
|
||||
},
|
||||
],
|
||||
storages: []
|
||||
},
|
||||
apophis: {
|
||||
members: [ownerId, recipientId],
|
||||
validation_rules: [],
|
||||
storages: []
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user