@exulu/backend 5.5.1 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ZWFL6YUH.js → chunk-TAXHJJ5R.js} +176 -11
- package/dist/{convert-exulu-tools-to-ai-sdk-tools-C5LYP4UN.js → convert-exulu-tools-to-ai-sdk-tools-BPZSN22N.js} +1 -1
- package/dist/index.cjs +2071 -587
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1679 -390
- package/ee/queues/import-queue.ts +27 -0
- package/ee/queues/prune-job-results.test.ts +105 -1
- package/ee/queues/prune-job-results.ts +85 -9
- package/ee/queues/queues.ts +1 -0
- package/ee/tokenizer.test.ts +178 -0
- package/ee/tokenizer.ts +80 -6
- package/ee/workers.ts +173 -2
- package/package.json +1 -1
|
@@ -1723,7 +1723,7 @@ var ExuluTool = class _ExuluTool {
|
|
|
1723
1723
|
if (!agent) {
|
|
1724
1724
|
throw new Error("Agent not found.");
|
|
1725
1725
|
}
|
|
1726
|
-
const { convertExuluToolsToAiSdkTools: convertExuluToolsToAiSdkTools2 } = await import("./convert-exulu-tools-to-ai-sdk-tools-
|
|
1726
|
+
const { convertExuluToolsToAiSdkTools: convertExuluToolsToAiSdkTools2 } = await import("./convert-exulu-tools-to-ai-sdk-tools-BPZSN22N.js");
|
|
1727
1727
|
const tools = await convertExuluToolsToAiSdkTools2(
|
|
1728
1728
|
[this],
|
|
1729
1729
|
[],
|
|
@@ -5104,6 +5104,19 @@ var getS3ObjectContent = async (key, config) => {
|
|
|
5104
5104
|
}
|
|
5105
5105
|
return response.Body.transformToString("utf-8");
|
|
5106
5106
|
};
|
|
5107
|
+
var getS3ObjectStream = async (key, config) => {
|
|
5108
|
+
if (!config.fileUploads) {
|
|
5109
|
+
throw new Error("File uploads are not configured");
|
|
5110
|
+
}
|
|
5111
|
+
const client = getS3Client(config);
|
|
5112
|
+
const bucket = config.fileUploads.s3Bucket;
|
|
5113
|
+
const command = new GetObjectCommand({ Bucket: bucket, Key: key });
|
|
5114
|
+
const response = await client.send(command);
|
|
5115
|
+
if (!response.Body) {
|
|
5116
|
+
throw new Error(`Empty body for S3 key: ${key}`);
|
|
5117
|
+
}
|
|
5118
|
+
return response.Body;
|
|
5119
|
+
};
|
|
5107
5120
|
var getS3ObjectEtag = async (key, config) => {
|
|
5108
5121
|
if (!config.fileUploads) {
|
|
5109
5122
|
throw new Error("File uploads are not configured");
|
|
@@ -5763,23 +5776,57 @@ import { Tiktoken } from "tiktoken/lite";
|
|
|
5763
5776
|
import { load } from "tiktoken/load";
|
|
5764
5777
|
import registry from "tiktoken/registry.json" with { type: "json" };
|
|
5765
5778
|
import models from "tiktoken/model_to_encoding.json" with { type: "json" };
|
|
5779
|
+
var ranksByEncoding = /* @__PURE__ */ new Map();
|
|
5780
|
+
var loadRanks = (encoding) => {
|
|
5781
|
+
const cached = ranksByEncoding.get(encoding);
|
|
5782
|
+
if (cached) return cached;
|
|
5783
|
+
const time = performance.now();
|
|
5784
|
+
console.log("[EXULU] Loading tokenizer ranks.", encoding);
|
|
5785
|
+
const pending = load(registry[encoding]).then((ranks) => {
|
|
5786
|
+
console.log("[EXULU] Loaded tokenizer ranks.", encoding, performance.now() - time);
|
|
5787
|
+
return ranks;
|
|
5788
|
+
}).catch((error) => {
|
|
5789
|
+
ranksByEncoding.delete(encoding);
|
|
5790
|
+
throw error;
|
|
5791
|
+
});
|
|
5792
|
+
ranksByEncoding.set(encoding, pending);
|
|
5793
|
+
return pending;
|
|
5794
|
+
};
|
|
5766
5795
|
var ExuluTokenizer = class {
|
|
5767
5796
|
constructor() {
|
|
5768
5797
|
}
|
|
5769
5798
|
encoder = null;
|
|
5799
|
+
/** Remembered so `ensure()` can rebuild an encoder that `free()` released. */
|
|
5800
|
+
modelName = null;
|
|
5770
5801
|
async create(modelName) {
|
|
5802
|
+
this.modelName = modelName;
|
|
5771
5803
|
if (this.encoder) {
|
|
5772
5804
|
return this.encoder;
|
|
5773
5805
|
}
|
|
5774
|
-
const
|
|
5775
|
-
|
|
5776
|
-
|
|
5777
|
-
console.log("[EXULU] Loaded tokenizer.", modelName, performance.now() - time);
|
|
5778
|
-
const encoder = new Tiktoken(model.bpe_ranks, model.special_tokens, model.pat_str);
|
|
5779
|
-
console.log("[EXULU] Set encoder.");
|
|
5806
|
+
const ranks = await loadRanks(models[modelName]);
|
|
5807
|
+
const encoder = new Tiktoken(ranks.bpe_ranks, ranks.special_tokens, ranks.pat_str);
|
|
5808
|
+
console.log("[EXULU] Set encoder.", modelName);
|
|
5780
5809
|
this.encoder = encoder;
|
|
5781
5810
|
return encoder;
|
|
5782
5811
|
}
|
|
5812
|
+
/**
|
|
5813
|
+
* The encoder, rebuilt if a previous `chunk()` freed it.
|
|
5814
|
+
*
|
|
5815
|
+
* Both chunkers free their tokenizer at the end of every `chunk()`, so an
|
|
5816
|
+
* instance that is used twice — directly, or via `chunkBatch`, which calls
|
|
5817
|
+
* `chunk()` once per text — needs its encoder back. Rebuilding is cheap:
|
|
5818
|
+
* the ranks are already cached process-wide, so this costs a `Tiktoken`
|
|
5819
|
+
* construction and no network.
|
|
5820
|
+
*/
|
|
5821
|
+
async ensure() {
|
|
5822
|
+
if (this.encoder) {
|
|
5823
|
+
return this.encoder;
|
|
5824
|
+
}
|
|
5825
|
+
if (!this.modelName) {
|
|
5826
|
+
throw new Error("Tokenizer not initialized");
|
|
5827
|
+
}
|
|
5828
|
+
return this.create(this.modelName);
|
|
5829
|
+
}
|
|
5783
5830
|
async decode(tokens) {
|
|
5784
5831
|
if (!this.encoder) {
|
|
5785
5832
|
throw new Error("Tokenizer not initialized");
|
|
@@ -5824,6 +5871,7 @@ var ExuluTokenizer = class {
|
|
|
5824
5871
|
console.log("[EXULU] Freeing tokenizer.");
|
|
5825
5872
|
if (this.encoder) {
|
|
5826
5873
|
this.encoder.free();
|
|
5874
|
+
this.encoder = null;
|
|
5827
5875
|
}
|
|
5828
5876
|
}
|
|
5829
5877
|
};
|
|
@@ -6315,6 +6363,7 @@ var SentenceChunker = class _SentenceChunker extends BaseChunker {
|
|
|
6315
6363
|
if (!text.trim()) {
|
|
6316
6364
|
return [];
|
|
6317
6365
|
}
|
|
6366
|
+
await this.tokenizer.ensure();
|
|
6318
6367
|
const sentences = await this._prepareSentences(text);
|
|
6319
6368
|
if (!sentences.length) {
|
|
6320
6369
|
return [];
|
|
@@ -8723,6 +8772,7 @@ var TERMINAL_JOB_STATES = ["completed", "failed", "filtered", "cancelled"];
|
|
|
8723
8772
|
// ee/queues/prune-job-results.ts
|
|
8724
8773
|
var MAX_TERMINAL = 1e4;
|
|
8725
8774
|
var PRUNE_EVERY = 100;
|
|
8775
|
+
var MAX_IMPORT_PER_CONTEXT = 200;
|
|
8726
8776
|
var sinceLastPrune = 0;
|
|
8727
8777
|
var pruning = false;
|
|
8728
8778
|
async function maybePruneJobResults(db2) {
|
|
@@ -8731,9 +8781,26 @@ async function maybePruneJobResults(db2) {
|
|
|
8731
8781
|
sinceLastPrune = 0;
|
|
8732
8782
|
pruning = true;
|
|
8733
8783
|
try {
|
|
8734
|
-
|
|
8784
|
+
await pruneGenericJobResults(db2);
|
|
8785
|
+
await pruneImportJobResults(db2);
|
|
8786
|
+
} finally {
|
|
8787
|
+
pruning = false;
|
|
8788
|
+
}
|
|
8789
|
+
}
|
|
8790
|
+
function excludeImportType(builder) {
|
|
8791
|
+
return builder.where(function() {
|
|
8792
|
+
this.whereNot({ type: "import" }).orWhereNull("type");
|
|
8793
|
+
});
|
|
8794
|
+
}
|
|
8795
|
+
async function pruneGenericJobResults(db2) {
|
|
8796
|
+
try {
|
|
8797
|
+
const boundary = await excludeImportType(
|
|
8798
|
+
db2("job_results").whereIn("state", TERMINAL_JOB_STATES)
|
|
8799
|
+
).orderBy("createdAt", "desc").offset(MAX_TERMINAL).limit(1).first();
|
|
8735
8800
|
if (boundary?.createdAt) {
|
|
8736
|
-
const deleted = await
|
|
8801
|
+
const deleted = await excludeImportType(
|
|
8802
|
+
db2("job_results").whereIn("state", TERMINAL_JOB_STATES)
|
|
8803
|
+
).where("createdAt", "<=", boundary.createdAt).del();
|
|
8737
8804
|
if (deleted) {
|
|
8738
8805
|
console.log(
|
|
8739
8806
|
`[EXULU] pruned ${deleted} terminal job_results rows (cap ${MAX_TERMINAL}).`
|
|
@@ -8742,8 +8809,30 @@ async function maybePruneJobResults(db2) {
|
|
|
8742
8809
|
}
|
|
8743
8810
|
} catch (err3) {
|
|
8744
8811
|
console.error("[EXULU] job_results prune failed", err3);
|
|
8745
|
-
}
|
|
8746
|
-
|
|
8812
|
+
}
|
|
8813
|
+
}
|
|
8814
|
+
async function pruneImportJobResults(db2) {
|
|
8815
|
+
if (db2.client?.config?.client !== "pg") return;
|
|
8816
|
+
try {
|
|
8817
|
+
const statePlaceholders = TERMINAL_JOB_STATES.map(() => "?").join(", ");
|
|
8818
|
+
const deleted = await db2("job_results").where({ type: "import" }).whereRaw(
|
|
8819
|
+
`id in (
|
|
8820
|
+
select id from (
|
|
8821
|
+
select id, row_number() over (partition by context order by "createdAt" desc) as rn
|
|
8822
|
+
from job_results
|
|
8823
|
+
where type = 'import' and state in (${statePlaceholders})
|
|
8824
|
+
) ranked
|
|
8825
|
+
where rn > ?
|
|
8826
|
+
)`,
|
|
8827
|
+
[...TERMINAL_JOB_STATES, MAX_IMPORT_PER_CONTEXT]
|
|
8828
|
+
).del();
|
|
8829
|
+
if (deleted) {
|
|
8830
|
+
console.log(
|
|
8831
|
+
`[EXULU] pruned ${deleted} terminal import job_results rows (cap ${MAX_IMPORT_PER_CONTEXT}/context).`
|
|
8832
|
+
);
|
|
8833
|
+
}
|
|
8834
|
+
} catch (err3) {
|
|
8835
|
+
console.error("[EXULU] import job_results prune failed", err3);
|
|
8747
8836
|
}
|
|
8748
8837
|
}
|
|
8749
8838
|
|
|
@@ -10240,6 +10329,28 @@ var agentSessionsSchema = {
|
|
|
10240
10329
|
name: "currenttask",
|
|
10241
10330
|
type: "text",
|
|
10242
10331
|
required: false
|
|
10332
|
+
},
|
|
10333
|
+
// Chat-list activity (see src/exulu/session-activity.ts). All three stay
|
|
10334
|
+
// NULL on existing rows, so no historical conversation starts out looking
|
|
10335
|
+
// unread.
|
|
10336
|
+
{
|
|
10337
|
+
// Non-null while a turn is producing an answer.
|
|
10338
|
+
name: "generating_since",
|
|
10339
|
+
type: "date",
|
|
10340
|
+
required: false
|
|
10341
|
+
},
|
|
10342
|
+
{
|
|
10343
|
+
// When the most recent answer finished.
|
|
10344
|
+
name: "last_answer_at",
|
|
10345
|
+
type: "date",
|
|
10346
|
+
required: false
|
|
10347
|
+
},
|
|
10348
|
+
{
|
|
10349
|
+
// When someone last had the session open. last_answer_at later than
|
|
10350
|
+
// this is an answer nobody has read.
|
|
10351
|
+
name: "last_seen_at",
|
|
10352
|
+
type: "date",
|
|
10353
|
+
required: false
|
|
10243
10354
|
}
|
|
10244
10355
|
]
|
|
10245
10356
|
};
|
|
@@ -11116,6 +11227,58 @@ var contextPresetsSchema = {
|
|
|
11116
11227
|
}
|
|
11117
11228
|
]
|
|
11118
11229
|
};
|
|
11230
|
+
var contextFoldersSchema = {
|
|
11231
|
+
type: "context_folders",
|
|
11232
|
+
name: {
|
|
11233
|
+
plural: "context_folders",
|
|
11234
|
+
singular: "context_folder"
|
|
11235
|
+
},
|
|
11236
|
+
RBAC: false,
|
|
11237
|
+
apiWrites: "super_admin",
|
|
11238
|
+
fields: [
|
|
11239
|
+
{
|
|
11240
|
+
// `index: true` is advisory here, not a DB index: init-exulu-db.ts's
|
|
11241
|
+
// mapType call for core tables only forwards `unique`, never `index`,
|
|
11242
|
+
// to knex. Kept on the field because it documents intent (and other,
|
|
11243
|
+
// non-core-table callers of mapType do read it) and the schema test
|
|
11244
|
+
// asserts it, but creating this table does not add a Postgres index.
|
|
11245
|
+
name: "name",
|
|
11246
|
+
type: "text",
|
|
11247
|
+
required: true,
|
|
11248
|
+
index: true
|
|
11249
|
+
},
|
|
11250
|
+
{
|
|
11251
|
+
name: "description",
|
|
11252
|
+
type: "text"
|
|
11253
|
+
},
|
|
11254
|
+
{
|
|
11255
|
+
name: "icon",
|
|
11256
|
+
type: "text"
|
|
11257
|
+
},
|
|
11258
|
+
{
|
|
11259
|
+
// `default: 0` is advisory here, not a DB default: map-types.ts's
|
|
11260
|
+
// json/number branches only call `.defaultTo(defaultValue)` when
|
|
11261
|
+
// defaultValue is truthy, and 0 is falsy, so no column default is ever
|
|
11262
|
+
// emitted for this field. New rows rely on the GraphQL input/resolver
|
|
11263
|
+
// defaulting sort_order, not a Postgres-level default.
|
|
11264
|
+
name: "sort_order",
|
|
11265
|
+
type: "number",
|
|
11266
|
+
default: 0
|
|
11267
|
+
},
|
|
11268
|
+
{
|
|
11269
|
+
// Unlike sort_order's `0`, "[]" is a truthy string, so map-types.ts's
|
|
11270
|
+
// json branch does emit `.defaultTo("[]")`: a CreateOne that omits
|
|
11271
|
+
// context_ids stores an empty array instead of NULL. That matters
|
|
11272
|
+
// because the generated GraphQL output type is JSON! (non-null) —
|
|
11273
|
+
// without this default a bare NULL would break context_foldersPagination
|
|
11274
|
+
// for every reader, not just the row that omitted the field.
|
|
11275
|
+
name: "context_ids",
|
|
11276
|
+
type: "json",
|
|
11277
|
+
required: true,
|
|
11278
|
+
default: "[]"
|
|
11279
|
+
}
|
|
11280
|
+
]
|
|
11281
|
+
};
|
|
11119
11282
|
var addCoreFields = (schema) => {
|
|
11120
11283
|
schema.fields.forEach((field) => {
|
|
11121
11284
|
if (field.type === "file") {
|
|
@@ -11200,6 +11363,7 @@ var coreSchemas = {
|
|
|
11200
11363
|
contextProjectionsSchema: () => addCoreFields(contextProjectionsSchema),
|
|
11201
11364
|
contextMapTopicsSchema: () => addCoreFields(contextMapTopicsSchema),
|
|
11202
11365
|
contextPresetsSchema: () => addCoreFields(contextPresetsSchema),
|
|
11366
|
+
contextFoldersSchema: () => addCoreFields(contextFoldersSchema),
|
|
11203
11367
|
sharedArtifactsSchema: () => addCoreFields(sharedArtifactsSchema),
|
|
11204
11368
|
transcriptionJobsSchema: () => addCoreFields(transcriptionJobsSchema),
|
|
11205
11369
|
imageGenerationsSchema: () => addCoreFields(imageGenerationsSchema),
|
|
@@ -15693,6 +15857,7 @@ export {
|
|
|
15693
15857
|
listS3ObjectsByPrefix,
|
|
15694
15858
|
copyS3Object,
|
|
15695
15859
|
getS3ObjectContent,
|
|
15860
|
+
getS3ObjectStream,
|
|
15696
15861
|
getS3ObjectEtag,
|
|
15697
15862
|
getS3ObjectBytes,
|
|
15698
15863
|
deleteS3Object,
|