|
|
|
|
@ -156,6 +156,8 @@ function opBudgets(state) {
@@ -156,6 +156,8 @@ function opBudgets(state) {
|
|
|
|
|
return { groups: out, alwaysCount: alwaysCount, alwaysLimit: ALWAYS_LIMIT, totalEntries: state.entries.length }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function normWorkspace(w) { return String(w || '').replace(/\\/g, '/').toLowerCase(); } |
|
|
|
|
|
|
|
|
|
function validateEntry(e) { |
|
|
|
|
if (!e.track || (e.track !== 'user' && e.track !== 'agent')) throw new Error('track user or agent'); |
|
|
|
|
if (!e.scope || (e.scope !== 'user-global' && e.scope !== 'workspace')) throw new Error('scope user-global or workspace'); |
|
|
|
|
@ -176,6 +178,10 @@ function opWrite(state, args) {
@@ -176,6 +178,10 @@ function opWrite(state, args) {
|
|
|
|
|
validateEntry(e); |
|
|
|
|
const alwaysCount = state.entries.filter(function (x) { return x.importance === 'always'; }).length; |
|
|
|
|
if (e.importance === 'always' && alwaysCount >= ALWAYS_LIMIT) throw new Error('E_TOO_MANY_ALWAYS: limit ' + ALWAYS_LIMIT); |
|
|
|
|
if (String(e.key || '').indexOf('directive_') === 0) { |
|
|
|
|
const dirCount = state.entries.filter(function (x) { return String(x.key).indexOf('directive_') === 0 && x.state === 'active' && (x.chatId || '') === (e.chatId || ''); }).length; |
|
|
|
|
if (dirCount >= DIRECTIVES_PER_CHAT) throw new Error('E_TOO_MANY_DIRECTIVES: limit ' + DIRECTIVES_PER_CHAT + ' per chat'); |
|
|
|
|
} |
|
|
|
|
const entry = { |
|
|
|
|
id: newId(), track: e.track, scope: e.scope, workspaceKey: String(e.workspaceKey || ''), |
|
|
|
|
key: e.key, text: e.text, importance: e.importance, tags: Array.isArray(e.tags) ? e.tags.slice(0, TAGS_LIMIT) : [], |
|
|
|
|
@ -192,7 +198,13 @@ function opWrite(state, args) {
@@ -192,7 +198,13 @@ function opWrite(state, args) {
|
|
|
|
|
if (!e) throw new Error('ENTRY_NOT_FOUND'); |
|
|
|
|
if (typeof args.text === 'string' && args.text) { e.text = args.text; e.bytes = byteLen(args.text); } |
|
|
|
|
if (Array.isArray(args.tags)) e.tags = args.tags.slice(0, TAGS_LIMIT); |
|
|
|
|
if (args.importance === 'always' || args.importance === 'called') e.importance = args.importance; |
|
|
|
|
if (args.importance === 'always' || args.importance === 'called') { |
|
|
|
|
if (args.importance === 'always' && e.importance !== 'always') { |
|
|
|
|
const alwaysCount = state.entries.filter(function (x) { return x.importance === 'always'; }).length; |
|
|
|
|
if (alwaysCount >= ALWAYS_LIMIT) throw new Error('E_TOO_MANY_ALWAYS: limit ' + ALWAYS_LIMIT); |
|
|
|
|
} |
|
|
|
|
e.importance = args.importance; |
|
|
|
|
} |
|
|
|
|
if (args.state === 'active' || args.state === 'done' || args.state === 'archive') e.state = args.state; |
|
|
|
|
if (typeof args.chatId === 'string') e.chatId = args.chatId; |
|
|
|
|
e.version = (e.version || 1) + 1; |
|
|
|
|
@ -239,10 +251,11 @@ function query(state, args) {
@@ -239,10 +251,11 @@ function query(state, args) {
|
|
|
|
|
throw new Error('unknown op: ' + op); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getInjection(state, chatId) { |
|
|
|
|
function getInjection(state, chatId, workspaceKey) { |
|
|
|
|
const entries = []; |
|
|
|
|
for (const e of state.entries) { |
|
|
|
|
if (e.key && e.key.indexOf('directive_') === 0 && e.scope === 'workspace' && e.state === 'active') { |
|
|
|
|
var ew = normWorkspace(e.workspaceKey); var cw = normWorkspace(workspaceKey); if (ew && cw && ew !== cw) continue; |
|
|
|
|
if (!chatId || !e.chatId || e.chatId === chatId) { entries.push(e); } |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|