<commit_before>// file: src/model/direct.js
const Direct = require("direct-js").DirectAPI;

process.on('message', (msg) => {
  dispatch(msg);
});

const dispatch = (msg) => {
  switch (msg.method) {
  case 'start':
    start(msg, msg.user);
    break;
  case 'getDomains':
    getDomains(msg);
    break;
  case 'getTalks':
    getTalks(msg, msg.domainId);
    break;
  case 'sendTextMessage':
    sendTextMessage(msg, msg.domainId, msg.talkId, msg.content);
    break;
  default:
    console.error(`not implemented: ${msg}`);
  }
};

let direct = null;

const start = (msg, user) => {
  const d = new Direct();
  d.setOptions({
    host: 'api.direct4b.com',
    endpoint: 'wss://api.direct4b.com/albero-app-server/api',
    access_token: user.directApiToken
  });
  d.listen();
  direct = d; // NOTE: global variable
  process.send({method: msg.method, result: 'OK'});
};

const asc   = (a, b) => ((a.high - b.high) || (a.low - b.low));

const getDomains = (msg) => {
  const ds = direct.data.getDomains();
  const str = direct.stringifyInt64;
  const result = ds.sort((a, b) => asc(a.id, b.id)).map(d => ({id: str(d.id), name: d.name}));
  process.send({method: msg.method, result});
};

const getTalks = (msg, domainId) => {
  const ts = direct.data.getTalks();
  const str = direct.stringifyInt64;
  const result = ts
    .filter((t) => str(t.domainId) === domainId)
    .sort((a, b) => asc(a.id, b.id))
    .map(t => ({id: str(t.id), name: t.name, type: t.type[0], userIds: (t.userIds || []).map(i => str(i))}));<commit_msg>Fix DirectAPI instantiation<commit_after>// file: src/model/direct.js
const DirectAPI = require("direct-js").DirectAPI;

process.on('message', (msg) => {
  dispatch(msg);
});

const dispatch = (msg) => {
  switch (msg.method) {
  case 'start':
    start(msg, msg.user);
    break;
  case 'getDomains':
    getDomains(msg);
    break;
  case 'getTalks':
    getTalks(msg, msg.domainId);
    break;
  case 'sendTextMessage':
    sendTextMessage(msg, msg.domainId, msg.talkId, msg.content);
    break;
  default:
    console.error(`not implemented: ${msg}`);
  }
};

let direct = null;

const start = (msg, user) => {
  const d = DirectAPI.getInstance();
  d.setOptions({
    host: 'api.direct4b.com',
    endpoint: 'wss://api.direct4b.com/albero-app-server/api',
    access_token: user.directApiToken
    // TODO: storage_path
  });
  d.listen();
  direct = d; // FIXME: global variable
  process.send({method: msg.method, result: 'OK'});
};

const asc   = (a, b) => ((a.high - b.high) || (a.low - b.low));

const getDomains = (msg) => {
  const ds = direct.data.getDomains();
  const str = direct.stringifyInt64;
  const result = ds.sort((a, b) => asc(a.id, b.id)).map(d => ({id: str(d.id), name: d.name}));
  process.send({method: msg.method, result});
};

const getTalks = (msg, domainId) => {
  const ts = direct.data.getTalks();
  const str = direct.stringifyInt64;
  const result = ts
    .filter((t) => str(t.domainId) === domainId)
    .sort((a, b) => asc(a.id, b.id))
    .map(t => ({id: str(t.id), name: t.name, type: t.type[0], userIds: (t.userIds || []).map(i => str(i))}));
{'fraction_non_alphanumeric': 0.2049469964664311, 'fraction_numerical': 0.004497269514937359, 'mean_word_length': 3.4232954545454546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '725615', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1174, 'n_words': 258}