File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * IAiAdapter Interface Definition
3+ * Defines the contract that any AI provider must adhere to before being plugged into EmbeddedChat.
4+ */
5+
6+ export class AiAdapter {
7+ /**
8+ * @param {string } name - The readable name of the AI service (e.g. "Gemini", "Ollama").
9+ */
10+ constructor ( name ) {
11+ this . name = name ;
12+ this . enabled = true ;
13+ }
14+
15+ /**
16+ * Given the context of the room history, suggest auto-replies.
17+ * @param {Array<Object> } messageContext - The last N messages in the room.
18+ * @returns {Promise<Array<string>> } - An array of suggested replies.
19+ */
20+ async getSmartReplies ( messageContext ) {
21+ throw new Error ( 'Not implemented' ) ;
22+ }
23+
24+ /**
25+ * Summarize the current view for quick catch-up.
26+ * @param {Array<Object> } messages - The raw message payloads.
27+ * @returns {Promise<string> } - The summary text.
28+ */
29+ async getSummary ( messages ) {
30+ throw new Error ( 'Not implemented' ) ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ import { AiAdapter } from './AiAdapter' ;
2+
3+ /**
4+ * Proof of Concept: Integrating Google's Gemini LLM as an EmbeddedChat Plugin.
5+ */
6+ export class GeminiAiAdapter extends AiAdapter {
7+ constructor ( apiKey ) {
8+ super ( 'Gemini Pro' ) ;
9+ this . apiKey = apiKey ;
10+ // Mocking the GoogleGenerativeAI initialization for POC purposes
11+ this . client = {
12+ getGenerativeModel : ( ) => ( {
13+ generateContent : async ( prompt ) => {
14+ console . log ( `Sending to Gemini: ${ prompt } ` ) ;
15+ return {
16+ response : { text : ( ) => 'Sounds great!\nI agree.\nLet me check.' }
17+ } ;
18+ }
19+ } )
20+ } ;
21+ }
22+
23+ async getSmartReplies ( messageContext ) {
24+ if ( ! this . apiKey ) throw new Error ( 'API Key required' ) ;
25+ const model = this . client . getGenerativeModel ( { model : 'gemini-pro' } ) ;
26+
27+ // Simplistic prompt engineering for POC
28+ const textContext = messageContext . map ( m => `${ m . u ?. username || 'user' } : ${ m . msg } ` ) . join ( '\n' ) ;
29+ const prompt = `Based on this chat context:\n${ textContext } \nProvide 3 very short replies. Return them separated by newlines.` ;
30+
31+ const result = await model . generateContent ( prompt ) ;
32+ return result . response . text ( ) . split ( '\n' ) . filter ( Boolean ) ;
33+ }
34+
35+ async getSummary ( messages ) {
36+ const model = this . client . getGenerativeModel ( { model : 'gemini-pro' } ) ;
37+ const textContext = messages . map ( m => `${ m . u ?. username || 'user' } : ${ m . msg } ` ) . join ( '\n' ) ;
38+ const prompt = `Summarize this chat context in 2 sentences:\n${ textContext } ` ;
39+
40+ const result = await model . generateContent ( prompt ) ;
41+ return result . response . text ( ) ;
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * POC: Matrix Federation Identification Utilities
3+ * This utility safely parses and normalizes standard Rocket.Chat user/room IDs
4+ * versus complex Matrix-federated identifiers.
5+ */
6+
7+ /**
8+ * Standardizes displaying a username, detecting if it has a Matrix homeserver attached.
9+ * @param {string } username - Original username from the backend payload
10+ * @returns {Object } - Parsed identity parts
11+ */
12+ export const parseFederatedIdentity = ( username ) => {
13+ if ( ! username ) return { name : 'Unknown' , isFederated : false , server : null } ;
14+
15+ // Matrix pattern: @username :homeserver.org
16+ const matrixRegex = / ^ @ ( [ ^ : ] + ) : ( .+ ) $ / ;
17+ const match = username . match ( matrixRegex ) ;
18+
19+ if ( match ) {
20+ return {
21+ name : match [ 1 ] ,
22+ isFederated : true ,
23+ server : match [ 2 ] ,
24+ original : username
25+ } ;
26+ }
27+
28+ return {
29+ name : username ,
30+ isFederated : false ,
31+ server : null ,
32+ original : username
33+ } ;
34+ } ;
35+
36+ /**
37+ * Decorates a message object with normalized federated IDs for the UI to consume securely.
38+ * @param {Object } message - Raw message payload
39+ * @returns {Object } - Decorated message
40+ */
41+ export const decorateFederatedMessage = ( message ) => {
42+ if ( ! message || ! message . u ) return message ;
43+
44+ const parsedIdentity = parseFederatedIdentity ( message . u . username ) ;
45+
46+ return {
47+ ...message ,
48+ federationMeta : {
49+ ...parsedIdentity
50+ }
51+ } ;
52+ } ;
You can’t perform that action at this time.
0 commit comments