Skip to content

Commit 131d142

Browse files
committed
class documentation
1 parent b695144 commit 131d142

File tree

5 files changed

+336
-111
lines changed

5 files changed

+336
-111
lines changed

index.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@ import { Broker } from "./src/broker/index.js";
22
import { Worker } from "./src/worker/index.js";
33

44
// define your environment variables in the .env file
5-
console.log(process.env)
5+
// console.log(process.env)
6+
67
let yjs_url = process.env.YJS_ENV== "REMOTE" ? process.env.YJS_REMOTE_URL : process.env.YJS_LOCAL_URL
78
//'wss://ylm-websocket.glitch.me'// "ws://localhost:1234"
89
let yjs_room = process.env.YJS_MARKET_ROOM
910

1011
let options = {
11-
1212
yjs_url: yjs_url,
1313
yjs_room: yjs_room
1414
}
1515

1616
console.log("OPTIONS", options)
1717

18-
1918
let broker = new Broker({
2019
name: "Broker",
2120
yjs_url: yjs_url,
2221
yjs_room: yjs_room,
23-
2422
})
2523

26-
27-
2824
let worker1 = new Worker({
2925
name: "worker1",
3026
yjs_url: yjs_url,

src/broker/index.js

+101-86
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,129 @@
11
import { Base } from "../base/index.js";
2-
32
import { YjsConnector } from "../yjsConnector/index.js";
43

4+
/**
5+
* @class Broker
6+
* @classdesc Classe pour gérer un Broker dans l'application
7+
* @author scenaristeur
8+
*/
59
export class Broker extends Base {
10+
/**
11+
* Constructeur de la classe Broker
12+
* @param {Object} options Options pour initialiser le Broker
13+
*/
614
constructor(options = {}) {
715
super(options);
16+
/**
17+
* Type du Broker
18+
* @type {String}
19+
*/
820
this.options.type = "broker";
21+
/**
22+
* Style de la console pour les logs du Broker
23+
* @type {String}
24+
*/
925
this.options.style = "normal";
26+
/**
27+
* Flag pour les logs du Broker
28+
* @type {String}
29+
*/
1030
this.flag = "[BROKER][" + this.options.name + "]";
31+
/**
32+
* Couleur pour les logs du Broker
33+
* @type {Function}
34+
*/
1135
this.chalk = this.chalk.blue;
36+
/**
37+
* Instance de YjsConnector pour communiquer avec les autres agents
38+
* @type {YjsConnector}
39+
*/
1240
this.yjs = new YjsConnector(this.options);
41+
/**
42+
* Méthode pour écouter les changements d'état dans l'awareness
43+
*/
1344
this.listenAwareness();
45+
/**
46+
* Méthode pour mettre à jour l'état de l'awareness
47+
*/
1448
this.updateAwareness();
49+
/**
50+
* Map de todos pour les tâches en attente
51+
* @type {Y.Map<String, Task>}
52+
*/
1553
this.todos = this.yjs.doc.getMap("todos");
54+
/**
55+
* Map de prepared pour les tâches prêtes à être exécutées
56+
* @type {Y.Map<String, Task>}
57+
*/
1658
this.prepared = this.yjs.doc.getMap("prepared");
17-
this.listenTodos()
59+
/**
60+
* Méthode pour écouter les changements sur la map todos
61+
*/
62+
this.listenTodos();
1863
}
64+
/**
65+
* Méthode pour écouter les changements sur la map todos
66+
*/
1967
listenTodos() {
20-
2168
this.todos.observeDeep((events, transaction) => {
22-
// console.log("events", events, transaction)
23-
this.prepare();
24-
25-
})
69+
// console.log("events", events, transaction)
70+
this.prepare();
71+
});
2672
}
2773

28-
29-
prepare() {
30-
if(this.activeBroker.get("active")==this.id){ //if this broker is the active broker
31-
32-
let todos = Array.from(this.todos.values())
33-
console.log("TODOS tasks", todos.length, todos)
34-
35-
36-
todos.forEach((todo)=>{
37-
let job = this.todos.get(todo.id);
38-
console.log("job", job)
39-
let workers = Array.from(this.yjs.awareness.getStates().values()).filter(
40-
(a) => {
41-
return a.agent.type == job.type && a.agent.state=='ready';
74+
/**
75+
* Méthode pour préparer les tâches en attente
76+
*/
77+
prepare() {
78+
if (this.activeBroker.get("active") == this.id) {
79+
//if this broker is the active broker
80+
let todos = Array.from(this.todos.values());
81+
console.log("TODOS tasks", todos.length, todos);
82+
83+
todos.forEach((todo) => {
84+
let job = this.todos.get(todo.id);
85+
console.log("job", job);
86+
let workers = Array.from(
87+
this.yjs.awareness.getStates().values()
88+
).filter((a) => {
89+
return a.agent.type == job.type && a.agent.state == "ready";
90+
});
91+
console.log("workers", workers.length, workers);
92+
if (workers.length > 0) {
93+
job.worker = workers[0].agent.id;
94+
job.state = "prepared";
95+
job.worker = workers[0].agent.id;
96+
job.attemps = 1;
97+
job.start = Date.now();
98+
this.prepared.set(job.id, job);
99+
this.todos.delete(job.id);
100+
console.log(job);
101+
this.log("prepare job", job.id, "for worker ", workers[0].agent.id);
102+
} else {
103+
this.log("no workers for job", job.id);
42104
}
43-
)
44-
console.log("workers", workers.length, workers)
45-
if (workers.length > 0) {
46-
47-
48-
job.worker = workers[0].agent.id;
49-
job.state = "prepared";
50-
job.worker = workers[0].agent.id;
51-
job.attemps = 1;
52-
job.start = Date.now();
53-
this.prepared.set(job.id, job);
54-
this.todos.delete(job.id);
55-
console.log(job)
56-
this.log("prepare job", job.id, "for worker ", workers[0].agent.id)
57-
}else{
58-
this.log("no workers for job", job.id)
105+
});
59106
}
60-
61-
})
62-
63107
}
64108

65-
}
66-
67-
68-
// prepare1() {
69-
// console.log("prepare TODOS")
70-
// console.log((this.activeBroker.get("active")==this.id))
71-
// if(this.activeBroker.get("active")==this.id){ //if this broker is the active broker
72-
// console.log(Array.from(this.todos.keys()))
73-
// let task_id = Array.from(this.todos.keys())[0];
74-
// let task = this.todos.get(task_id);
75-
// console.log("currenttask", task);
76-
// if (task != undefined){
77-
78-
79-
// let workers = Array.from(this.yjs.awareness.getStates().values()).filter(
80-
// (a) => {
81-
// return a.agent.type == task.type && a.agent.state=='ready';
82-
// }
83-
// )
84-
// console.log("workers", workers.length, workers)
85-
// if(workers.length>0){
86-
// task.state = "doing";
87-
// task.worker = workers[0].agent.id;
88-
// task.attemps = 1;
89-
// task.start = Date.now();
90-
// this.doing.set(task_id, task);
91-
// this.todos.delete(task_id);
92-
// console.log(task)
93-
// this.log("prepare task", task_id, "for worker ", workers[0].agent.id)
94-
// }else{
95-
// this.log("no workers available for task", task)
96-
// }
97-
// }
98-
// }
99-
100-
101-
102-
// }
103-
109+
/**
110+
* Méthode pour écouter les changements d'état des agents
111+
*/
104112
listenAwareness() {
105-
this.activeBroker = this.yjs.doc.getMap("activeBroker");
113+
this.activeBroker = this.yjs.doc.getMap("activeBroker");
106114
let awareness = this.yjs.awareness;
107115
awareness.on("change", (changes) => {
116+
console.log(changes)
108117
let agents = Array.from(awareness.getStates().values());
109-
console.log("######BROKER AWARENESS", agents.length);
118+
this.log("######BROKER AWARENESS", agents.length, "agents");
110119
let brokers = [];
111120
agents.forEach((a) => {
112121
try {
113122
this.log(
114123
a.agent.name,
115124
a.agent.type,
116125
// a.agent.type,
117-
a.agent.state,
126+
a.agent.state
118127
// a.agent.name,
119128
// a.agent.id,
120129
// a.agent.style
@@ -133,16 +142,20 @@ prepare() {
133142
}
134143
});
135144
brokers = brokers.sort((a, b) => a.date - b.date);
136-
// console.log("brokers", brokers);
145+
// console.log("brokers", brokers);
137146
let active = brokers[0].id;
138-
console.log("active", brokers[0]);
147+
139148
if (this.activeBroker.get("active") != active) {
140149
this.activeBroker.set("active", active);
141150
}
142-
this.log("activeBroker", JSON.stringify(this.activeBroker.toJSON()));
143-
console.log("#####", agents.length);
151+
this.log("active broker", brokers[0].id + " " + brokers[0].name);
152+
this.log("######BROKER AWARENESS", brokers.length, "brokers");
144153
});
145154
}
155+
156+
/**
157+
* Méthode pour mettre à jour l'état local de l'agent dans l'awareness
158+
*/
146159
updateAwareness() {
147160
this.yjs.awareness.setLocalStateField("agent", {
148161
id: this.id,
@@ -153,3 +166,5 @@ prepare() {
153166
});
154167
}
155168
}
169+
170+

src/mcConnector/index.js

+68-2
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,103 @@ import {
1212
// import { get_encoding, encoding_for_model } from "tiktoken";
1313
// const enc = get_encoding("cl100k_base"); // encoding_for_model("gpt-4-0125-preview");
1414

15+
/**
16+
* The module's entry point.
17+
* @module mcConnector
18+
*/
1519
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1620
const llama = await getLlama({ gpu: false });
1721
let model = undefined;
1822

23+
/**
24+
* The list of prompts to be processed.
25+
* @type {Array}
26+
*/
1927
let prompts = [];
28+
29+
/**
30+
* The map of sessions, indexed by the channel/user pair.
31+
* @type {Object}
32+
*/
2033
let sessions = {};
2134

35+
/**
36+
* The connector class for the Multi-Channel ASSISTANT.
37+
* Extends the base connector class.
38+
* @extends Base
39+
*/
2240
export class McConnector extends Base {
41+
/**
42+
* Initializes the instance.
43+
* @param {Object} options - The options as a name/value map.
44+
*/
2345
constructor(options = {}) {
2446
super(options);
47+
48+
/**
49+
* The name of the LLM model to be used.
50+
* @type {string}
51+
*/
2552
this.modelName = options.modelName || "vicuna-7b-v1.5-16k.Q2_K.gguf";
2653

54+
/**
55+
* The path to the LLM model.
56+
* @type {string}
57+
*/
2758
const modelPath = path.join(process.cwd(), "models", this.modelName);
28-
console.log("Loading LLM model from", modelPath);
59+
60+
/**
61+
* The message to be printed when the model is loaded.
62+
* @type {string}
63+
*/
64+
const modelLoadedMessage = "Loading LLM model from";
65+
66+
console.log(modelLoadedMessage, modelPath);
2967

3068
model = new LlamaModel({
3169
llama,
3270
modelPath: modelPath,
3371
// gpuLayers: 64 // or any other number of layers you want for use with gpu
3472
});
73+
74+
/**
75+
* The flag that is added to the output text.
76+
* @type {string}
77+
*/
3578
this.flag = "[MULTI-CHANNEL]";
79+
80+
/**
81+
* The chalk instance used for coloring the flag.
82+
* @type {Chalk}
83+
*/
3684
this.chalk = this.chalk.rgb(145, 167, 45); //.hex('#DEADED')
3785

3886
this._init();
3987
}
88+
89+
/**
90+
* Initializes the module if needed.
91+
* @returns {Promise} The promise that resolves when initialized.
92+
* @private
93+
*/
4094
async _init() {
4195
if (this.options.runMcTest == true) {
4296
await this.test();
4397
} else {
4498
this.state = "ready";
4599
}
46100
}
47-
101+
/**
102+
* Handle a message from the assistant.
103+
* @param {Object} options - The options for the request.
104+
* @param {string} options.user - The user making the request.
105+
* @param {string} options.id - The id of the current request.
106+
* @param {string} options.prompt - The prompt for the request.
107+
* @param {string} options.seed - The seed for the generation.
108+
* @param {number} options.temperature - The temperature for the generation.
109+
* @param {Function} cb - The callback function.
110+
* @returns {Promise} - The promise that resolves when the message has been processed.
111+
*/
48112
chat = async (options, cb) => {
49113
const that = this;
50114
console.log(options);
@@ -74,6 +138,8 @@ export class McConnector extends Base {
74138
contextSize: Math.min(4096, model.trainContextSize),
75139
});
76140

141+
this.log("context",context)
142+
77143
const session = new LlamaChatSession({
78144
contextSequence: context.getSequence(),
79145
});

0 commit comments

Comments
 (0)