n8n
Version:
n8n Workflow Automation Tool
125 lines • 5.64 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DbSnapshotStorage = void 0;
const di_1 = require("@n8n/di");
const n8n_workflow_1 = require("n8n-workflow");
const instance_ai_run_snapshot_repository_1 = require("../repositories/instance-ai-run-snapshot.repository");
let DbSnapshotStorage = class DbSnapshotStorage {
constructor(repo) {
this.repo = repo;
}
async getLatest(threadId, options = {}) {
const { messageGroupId, runId } = options;
const row = messageGroupId
? await this.repo.findOne({
where: { threadId, messageGroupId },
order: { createdAt: 'DESC' },
})
: runId
? await this.repo.findOne({
where: { threadId, runId },
order: { createdAt: 'DESC' },
})
: await this.repo.findOne({
where: { threadId },
order: { createdAt: 'DESC' },
});
if (!row)
return undefined;
return {
tree: (0, n8n_workflow_1.jsonParse)(row.tree),
runId: row.runId,
messageGroupId: row.messageGroupId ?? undefined,
runIds: row.runIds ?? undefined,
langsmithRunId: row.langsmithRunId ?? undefined,
langsmithTraceId: row.langsmithTraceId ?? undefined,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
async save(threadId, agentTree, runId, options = {}) {
const { messageGroupId, runIds, langsmithRunId, langsmithTraceId } = options;
await this.repo.upsert({
threadId,
runId,
messageGroupId: messageGroupId ?? null,
runIds: runIds ?? null,
tree: JSON.stringify(agentTree),
langsmithRunId: langsmithRunId ?? null,
langsmithTraceId: langsmithTraceId ?? null,
}, ['threadId', 'runId']);
}
async updateLast(threadId, agentTree, runId, options = {}) {
const { messageGroupId, runIds, langsmithRunId, langsmithTraceId } = options;
if (messageGroupId) {
const existing = await this.repo.findOne({
where: { threadId, messageGroupId },
order: { createdAt: 'DESC' },
});
if (existing) {
await this.repo.update({ threadId: existing.threadId, runId: existing.runId }, {
runId,
tree: JSON.stringify(agentTree),
messageGroupId,
runIds: runIds ?? existing.runIds,
langsmithRunId: langsmithRunId ?? existing.langsmithRunId,
langsmithTraceId: langsmithTraceId ?? existing.langsmithTraceId,
});
return;
}
}
const byRunId = await this.repo.findOneBy({ threadId, runId });
if (byRunId) {
await this.repo.update({ threadId, runId }, {
tree: JSON.stringify(agentTree),
messageGroupId: messageGroupId ?? byRunId.messageGroupId,
runIds: runIds ?? byRunId.runIds,
langsmithRunId: langsmithRunId ?? byRunId.langsmithRunId,
langsmithTraceId: langsmithTraceId ?? byRunId.langsmithTraceId,
});
return;
}
await this.save(threadId, agentTree, runId, options);
}
async getAll(threadId) {
const rows = await this.repo.find({
where: { threadId },
order: { createdAt: 'ASC' },
});
return rows.map((r) => ({
tree: (0, n8n_workflow_1.jsonParse)(r.tree),
runId: r.runId,
messageGroupId: r.messageGroupId ?? undefined,
runIds: r.runIds ?? undefined,
langsmithRunId: r.langsmithRunId ?? undefined,
langsmithTraceId: r.langsmithTraceId ?? undefined,
createdAt: r.createdAt,
updatedAt: r.updatedAt,
}));
}
async findLangsmithAnchor(threadId, responseId) {
const byGroup = await this.repo.findOne({
where: { threadId, messageGroupId: responseId },
order: { createdAt: 'ASC' },
});
const row = byGroup ?? (await this.repo.findOneBy({ threadId, runId: responseId }));
if (!row?.langsmithRunId || !row.langsmithTraceId)
return undefined;
return { langsmithRunId: row.langsmithRunId, langsmithTraceId: row.langsmithTraceId };
}
};
exports.DbSnapshotStorage = DbSnapshotStorage;
exports.DbSnapshotStorage = DbSnapshotStorage = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [instance_ai_run_snapshot_repository_1.InstanceAiRunSnapshotRepository])
], DbSnapshotStorage);
//# sourceMappingURL=db-snapshot-storage.js.map