mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
feat(assistant): an attached file stays with its conversation
A file attached in the trip chat lived only as a buffer for the one request that read it, so the assistant could not look at it again and nothing could keep it in Files. Now its bytes go to storage before the turn writes anything (under the trip files' category, which is never served by path) and a row in the new assistant_attachments table keeps its name, type, size, hash and the text TREK read out of it, written in the turn's opening transaction. The message's attachment part carries the row's id; old messages without one still read. The text is read the way the AI import reads it, through a new public LlmParseService.readText, and a file whose text cannot be read is kept all the same. A storage that refuses the bytes refuses the turn before any row exists. Deleting a thread takes its files along, and the nightly retention sweep drops files older than seven days and bytes no row names any more.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
|
||||
import { AssistantAttachmentsRepository } from '../repositories/AssistantAttachments.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { AssistantThreads } from './AssistantThreads.entity';
|
||||
import { TripFiles } from './TripFiles.entity';
|
||||
import { Trips } from './Trips.entity';
|
||||
import { Users } from './Users.entity';
|
||||
|
||||
export class AssistantAttachments {
|
||||
[EntityRepositoryType]?: AssistantAttachmentsRepository;
|
||||
id!: string;
|
||||
thread!: Ref<AssistantThreads>;
|
||||
thread_id!: number;
|
||||
user!: Ref<Users>;
|
||||
user_id!: number;
|
||||
trip!: Ref<Trips>;
|
||||
trip_id!: number;
|
||||
name!: string;
|
||||
mime!: string;
|
||||
size!: number;
|
||||
sha256!: string;
|
||||
storage_key!: string;
|
||||
text?: string | null;
|
||||
text_status!: string;
|
||||
shared_at?: string | null;
|
||||
file?: Ref<TripFiles> | null;
|
||||
file_id?: number | null;
|
||||
created_at!: string & Opt;
|
||||
}
|
||||
|
||||
export const AssistantAttachmentsSchema = defineEntity({
|
||||
class: AssistantAttachments,
|
||||
repository: () => AssistantAttachmentsRepository,
|
||||
indexes: [
|
||||
{
|
||||
name: 'idx_assistant_attachments_thread',
|
||||
properties: ['thread_id', 'created_at'],
|
||||
},
|
||||
],
|
||||
properties: {
|
||||
id: p.text().primary(),
|
||||
thread: () => p.manyToOne(AssistantThreads).ref().deleteRule('cascade').hidden(),
|
||||
thread_id: p.integer().persist(false),
|
||||
user: () => p.manyToOne(Users).ref().deleteRule('cascade').hidden().index('idx_assistant_attachments_user'),
|
||||
user_id: p.integer().persist(false).index('idx_assistant_attachments_user'),
|
||||
trip: () => p.manyToOne(Trips).ref().deleteRule('cascade').hidden().index('idx_assistant_attachments_trip'),
|
||||
trip_id: p.integer().persist(false).index('idx_assistant_attachments_trip'),
|
||||
name: p.text(),
|
||||
mime: p.text(),
|
||||
size: p.integer(),
|
||||
sha256: p.text(),
|
||||
storage_key: p.text(),
|
||||
text: p.text().nullable(),
|
||||
text_status: p.text(),
|
||||
shared_at: p.type(DbTimestampType).nullable(),
|
||||
file: () => p.manyToOne(TripFiles).ref().nullable().hidden().index('idx_assistant_attachments_file'),
|
||||
file_id: p.integer().nullable().persist(false).index('idx_assistant_attachments_file'),
|
||||
created_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`).index('idx_assistant_attachments_created'),
|
||||
},
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
|
||||
import { AssistantThreadsRepository } from '../repositories/AssistantThreads.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { AssistantAttachments } from './AssistantAttachments.entity';
|
||||
import { AssistantMessages } from './AssistantMessages.entity';
|
||||
import { AssistantToolCalls } from './AssistantToolCalls.entity';
|
||||
import { Journeys } from './Journeys.entity';
|
||||
@@ -19,6 +20,7 @@ export class AssistantThreads {
|
||||
title!: string;
|
||||
created_at!: string & Opt;
|
||||
last_message_at!: string & Opt;
|
||||
assistant_attachments_collection = new Collection<AssistantAttachments>(this);
|
||||
assistant_messages_collection = new Collection<AssistantMessages>(this);
|
||||
assistant_tool_calls_collection = new Collection<AssistantToolCalls>(this);
|
||||
}
|
||||
@@ -47,6 +49,7 @@ export const AssistantThreadsSchema = defineEntity({
|
||||
title: p.text(),
|
||||
created_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
|
||||
last_message_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
|
||||
assistant_attachments_collection: () => p.oneToMany(AssistantAttachments).mappedBy('thread').hidden(),
|
||||
assistant_messages_collection: () => p.oneToMany(AssistantMessages).mappedBy('thread').hidden(),
|
||||
assistant_tool_calls_collection: () => p.oneToMany(AssistantToolCalls).mappedBy('thread').hidden(),
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
|
||||
import { TripFilesRepository } from '../repositories/TripFiles.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { AssistantAttachments } from './AssistantAttachments.entity';
|
||||
import { CollabMessages } from './CollabMessages.entity';
|
||||
import { CollabNotes } from './CollabNotes.entity';
|
||||
import { DocumentSyncItems } from './DocumentSyncItems.entity';
|
||||
@@ -33,6 +34,7 @@ export class TripFiles {
|
||||
message?: Ref<CollabMessages> | null;
|
||||
message_id?: number | null;
|
||||
uploadedByRef?: Ref<Users> | null;
|
||||
assistant_attachments_collection = new Collection<AssistantAttachments>(this);
|
||||
document_sync_items_collection = new Collection<DocumentSyncItems>(this);
|
||||
file_links_collection = new Collection<FileLinks>(this);
|
||||
}
|
||||
@@ -62,6 +64,7 @@ export const TripFilesSchema = defineEntity({
|
||||
message: () => p.manyToOne(CollabMessages).ref().deleteRule('cascade').nullable().hidden().index('idx_trip_files_message_id'),
|
||||
message_id: p.integer().nullable().persist(false).index('idx_trip_files_message_id'),
|
||||
uploadedByRef: () => p.manyToOne(Users).ref().joinColumn('uploaded_by').nullable().hidden().index('idx_trip_files_uploaded_by'),
|
||||
assistant_attachments_collection: () => p.oneToMany(AssistantAttachments).mappedBy('file').hidden(),
|
||||
document_sync_items_collection: () => p.oneToMany(DocumentSyncItems).mappedBy('file').hidden(),
|
||||
file_links_collection: () => p.oneToMany(FileLinks).mappedBy('file').hidden(),
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
|
||||
import { TripsRepository } from '../repositories/Trips.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { AssistantAttachments } from './AssistantAttachments.entity';
|
||||
import { AssistantThreads } from './AssistantThreads.entity';
|
||||
import { BudgetCategoryOrder } from './BudgetCategoryOrder.entity';
|
||||
import { BudgetItems } from './BudgetItems.entity';
|
||||
@@ -57,6 +58,7 @@ export class Trips {
|
||||
created_at?: string | null;
|
||||
updated_at?: string | null;
|
||||
reminder_sent_for?: string | null;
|
||||
assistant_attachments_collection = new Collection<AssistantAttachments>(this);
|
||||
assistant_threads_collection = new Collection<AssistantThreads>(this);
|
||||
budget_category_order_collection = new Collection<BudgetCategoryOrder>(this);
|
||||
budget_items_collection = new Collection<BudgetItems>(this);
|
||||
@@ -122,6 +124,7 @@ export const TripsSchema = defineEntity({
|
||||
created_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`).index('idx_trips_created_at'),
|
||||
updated_at: p.type(DbTimestampType).nullable().defaultRaw(`CURRENT_TIMESTAMP`),
|
||||
reminder_sent_for: p.text().nullable(),
|
||||
assistant_attachments_collection: () => p.oneToMany(AssistantAttachments).mappedBy('trip').hidden(),
|
||||
assistant_threads_collection: () => p.oneToMany(AssistantThreads).mappedBy('trip').hidden(),
|
||||
budget_category_order_collection: () => p.oneToMany(BudgetCategoryOrder).mappedBy('trip').hidden(),
|
||||
budget_items_collection: () => p.oneToMany(BudgetItems).mappedBy('trip').hidden(),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { UsersRepository } from '../repositories/Users.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { AiConnections } from './AiConnections.entity';
|
||||
import { AssignmentParticipants } from './AssignmentParticipants.entity';
|
||||
import { AssistantAttachments } from './AssistantAttachments.entity';
|
||||
import { AssistantThreads } from './AssistantThreads.entity';
|
||||
import { AssistantUsage } from './AssistantUsage.entity';
|
||||
import { AuditLog } from './AuditLog.entity';
|
||||
@@ -135,6 +136,7 @@ export class Users {
|
||||
email_self_asserted: number & Opt = 0;
|
||||
ai_connections_collection = new Collection<AiConnections>(this);
|
||||
assignment_participants_collection = new Collection<AssignmentParticipants>(this);
|
||||
assistant_attachments_collection = new Collection<AssistantAttachments>(this);
|
||||
assistant_threads_collection = new Collection<AssistantThreads>(this);
|
||||
assistant_usage_collection = new Collection<AssistantUsage>(this);
|
||||
audit_log_collection = new Collection<AuditLog>(this);
|
||||
@@ -293,6 +295,7 @@ export const UsersSchema = defineEntity({
|
||||
email_self_asserted: p.integer().default(0),
|
||||
ai_connections_collection: () => p.oneToMany(AiConnections).mappedBy('user').hidden(),
|
||||
assignment_participants_collection: () => p.oneToMany(AssignmentParticipants).mappedBy('user').hidden(),
|
||||
assistant_attachments_collection: () => p.oneToMany(AssistantAttachments).mappedBy('user').hidden(),
|
||||
assistant_threads_collection: () => p.oneToMany(AssistantThreads).mappedBy('user').hidden(),
|
||||
assistant_usage_collection: () => p.oneToMany(AssistantUsage).mappedBy('user').hidden(),
|
||||
audit_log_collection: () => p.oneToMany(AuditLog).mappedBy('user').hidden(),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { AddonsSchema } from './Addons.entity';
|
||||
import { AiConnectionsSchema } from './AiConnections.entity';
|
||||
import { AppSettingsSchema } from './AppSettings.entity';
|
||||
import { AssignmentParticipantsSchema } from './AssignmentParticipants.entity';
|
||||
import { AssistantAttachmentsSchema } from './AssistantAttachments.entity';
|
||||
import { AssistantMessagesSchema } from './AssistantMessages.entity';
|
||||
import { AssistantThreadsSchema } from './AssistantThreads.entity';
|
||||
import { AssistantToolCallsSchema } from './AssistantToolCalls.entity';
|
||||
@@ -166,6 +167,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
|
||||
AiConnectionsSchema,
|
||||
AppSettingsSchema,
|
||||
AssignmentParticipantsSchema,
|
||||
AssistantAttachmentsSchema,
|
||||
AssistantMessagesSchema,
|
||||
AssistantThreadsSchema,
|
||||
AssistantToolCallsSchema,
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
|
||||
// change the migration, then run `npm run gen:db-types`.
|
||||
import type { InsertOptional } from './columns';
|
||||
|
||||
export interface AssistantAttachmentsTable {
|
||||
id: string;
|
||||
thread_id: number;
|
||||
user_id: number;
|
||||
trip_id: number;
|
||||
name: string;
|
||||
mime: string;
|
||||
size: number;
|
||||
sha256: string;
|
||||
storage_key: string;
|
||||
text: InsertOptional<string | null>;
|
||||
text_status: string;
|
||||
shared_at: InsertOptional<string | null>;
|
||||
file_id: InsertOptional<number | null>;
|
||||
created_at: InsertOptional<string>;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import type { AddonsTable } from './Addons.table';
|
||||
import type { AiConnectionsTable } from './AiConnections.table';
|
||||
import type { AppSettingsTable } from './AppSettings.table';
|
||||
import type { AssignmentParticipantsTable } from './AssignmentParticipants.table';
|
||||
import type { AssistantAttachmentsTable } from './AssistantAttachments.table';
|
||||
import type { AssistantMessagesTable } from './AssistantMessages.table';
|
||||
import type { AssistantThreadsTable } from './AssistantThreads.table';
|
||||
import type { AssistantToolCallsTable } from './AssistantToolCalls.table';
|
||||
@@ -173,6 +174,7 @@ export interface DB {
|
||||
ai_connections: AiConnectionsTable;
|
||||
app_settings: AppSettingsTable;
|
||||
assignment_participants: AssignmentParticipantsTable;
|
||||
assistant_attachments: AssistantAttachmentsTable;
|
||||
assistant_messages: AssistantMessagesTable;
|
||||
assistant_threads: AssistantThreadsTable;
|
||||
assistant_tool_calls: AssistantToolCallsTable;
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* A file the user attaches in the trip chat stays with the conversation. Until
|
||||
* now it lived only as a buffer for the one request that read it, so the
|
||||
* assistant could neither look at it again nor keep it in the trip's Files.
|
||||
*
|
||||
* `assistant_attachments` keeps one row per attached file: who attached it,
|
||||
* in which thread of which trip, its name, type, size and hash, the storage
|
||||
* key its bytes sit under, and the text TREK read out of it with how that
|
||||
* went (`text_status`). The text reaches the AI provider only after the user
|
||||
* agreed for this one file (`shared_at`), and a file kept in Files names the
|
||||
* trip file it became (`file_id`).
|
||||
*
|
||||
* The rows go with their thread, their user and their trip; a trip file the
|
||||
* user deletes later only clears `file_id`. The retention sweep removes a row
|
||||
* and its bytes after a week, or as soon as its thread is gone. Every foreign
|
||||
* key leads an index, and `created_at` has one for that sweep.
|
||||
*/
|
||||
export class Migration20200101046300_an_attached_file_stays_with_its_conversation extends Migration {
|
||||
override name = 'Migration20200101046300_an_attached_file_stays_with_its_conversation';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS assistant_attachments (
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
thread_id INTEGER NOT NULL REFERENCES assistant_threads(id) ON DELETE CASCADE,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
trip_id INTEGER NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
mime TEXT NOT NULL,
|
||||
size INTEGER NOT NULL,
|
||||
sha256 TEXT NOT NULL,
|
||||
storage_key TEXT NOT NULL,
|
||||
text TEXT,
|
||||
text_status TEXT NOT NULL CHECK(text_status IN ('ok', 'empty', 'scanned', 'unsupported', 'failed')),
|
||||
shared_at DATETIME,
|
||||
file_id INTEGER REFERENCES trip_files(id) ON DELETE SET NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
this.addSql(
|
||||
'CREATE INDEX IF NOT EXISTS idx_assistant_attachments_thread ON assistant_attachments(thread_id, created_at)',
|
||||
);
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_attachments_user ON assistant_attachments(user_id)');
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_attachments_trip ON assistant_attachments(trip_id)');
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_attachments_file ON assistant_attachments(file_id)');
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_attachments_created ON assistant_attachments(created_at)');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import type { AssistantAttachments } from '../entities/AssistantAttachments.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { AssertRowKeys } from './_shared/rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
/** How TREK's reading of an attached file went: text it read, none, a scan, a type it does not read, a failure. */
|
||||
export type AssistantAttachmentTextStatus = 'ok' | 'empty' | 'scanned' | 'unsupported' | 'failed';
|
||||
|
||||
/** One `assistant_attachments` row as stored, the text included. */
|
||||
export interface AssistantAttachmentRow {
|
||||
id: string;
|
||||
thread_id: number;
|
||||
user_id: number;
|
||||
trip_id: number;
|
||||
name: string;
|
||||
mime: string;
|
||||
size: number;
|
||||
sha256: string;
|
||||
storage_key: string;
|
||||
text: string | null;
|
||||
text_status: string;
|
||||
shared_at: string | null;
|
||||
file_id: number | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
const _assistantAttachmentRowKeys: AssertRowKeys<AssistantAttachmentRow, AssistantAttachments> = true;
|
||||
|
||||
/** An attachment as a turn lists it: everything but its text. */
|
||||
export type AssistantAttachmentSummary = Omit<AssistantAttachmentRow, 'text'>;
|
||||
|
||||
/** A new attachment, as AssistantAttachmentsService writes it. */
|
||||
export interface AssistantAttachmentInsert {
|
||||
id: string;
|
||||
threadId: number;
|
||||
userId: number;
|
||||
tripId: number;
|
||||
name: string;
|
||||
mime: string;
|
||||
size: number;
|
||||
sha256: string;
|
||||
storageKey: string;
|
||||
text: string | null;
|
||||
textStatus: AssistantAttachmentTextStatus;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
/** Where an attachment's bytes sit, for the sweep and a thread's deletion. */
|
||||
export interface AssistantAttachmentObject {
|
||||
id: string;
|
||||
storage_key: string;
|
||||
}
|
||||
|
||||
type AttachmentsKyselyDB = Pick<DB, 'assistant_attachments'>;
|
||||
|
||||
const SUMMARY_COLUMNS = [
|
||||
'id',
|
||||
'thread_id',
|
||||
'user_id',
|
||||
'trip_id',
|
||||
'name',
|
||||
'mime',
|
||||
'size',
|
||||
'sha256',
|
||||
'storage_key',
|
||||
'text_status',
|
||||
'shared_at',
|
||||
'file_id',
|
||||
'created_at',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* `assistant_attachments`: the files users attached in a trip chat. Every
|
||||
* read a turn makes names the thread and the user next to the id, so an
|
||||
* attachment is only ever reached in the conversation it was attached in,
|
||||
* by the user who attached it.
|
||||
*/
|
||||
export class AssistantAttachmentsRepository extends TrekRepository<AssistantAttachments> {
|
||||
private db() {
|
||||
return this.kysely<AttachmentsKyselyDB>();
|
||||
}
|
||||
|
||||
async insertAttachment(row: AssistantAttachmentInsert): Promise<void> {
|
||||
await this.db()
|
||||
.insertInto('assistant_attachments')
|
||||
.values({
|
||||
id: row.id,
|
||||
thread_id: row.threadId,
|
||||
user_id: row.userId,
|
||||
trip_id: row.tripId,
|
||||
name: row.name,
|
||||
mime: row.mime,
|
||||
size: row.size,
|
||||
sha256: row.sha256,
|
||||
storage_key: row.storageKey,
|
||||
text: row.text,
|
||||
text_status: row.textStatus,
|
||||
created_at: row.createdAt,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** One attachment of the user's in this thread, with its text; null for anyone else's or another thread's. */
|
||||
async findInThread(id: string, threadId: number, userId: number): Promise<AssistantAttachmentRow | null> {
|
||||
const row = await this.db()
|
||||
.selectFrom('assistant_attachments')
|
||||
.selectAll()
|
||||
.where('id', '=', id)
|
||||
.where('thread_id', '=', threadId)
|
||||
.where('user_id', '=', userId)
|
||||
.executeTakeFirst();
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
/** The user's attachments in this thread, oldest first, without their text. */
|
||||
async listForThread(threadId: number, userId: number): Promise<AssistantAttachmentSummary[]> {
|
||||
return this.db()
|
||||
.selectFrom('assistant_attachments')
|
||||
.select(SUMMARY_COLUMNS)
|
||||
.where('thread_id', '=', threadId)
|
||||
.where('user_id', '=', userId)
|
||||
.orderBy('created_at', 'asc')
|
||||
.orderBy('id', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The texts of this thread's attachments the user let the model read, by id. */
|
||||
async listSharedTexts(threadId: number, userId: number): Promise<Array<{ id: string; text: string }>> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('assistant_attachments')
|
||||
.select(['id', 'text'])
|
||||
.where('thread_id', '=', threadId)
|
||||
.where('user_id', '=', userId)
|
||||
.where('shared_at', 'is not', null)
|
||||
.where('text', 'is not', null)
|
||||
.execute();
|
||||
return rows.flatMap((row) => (row.text === null ? [] : [{ id: row.id, text: row.text }]));
|
||||
}
|
||||
|
||||
/** Records that the user let the model read the attachment; the first agreement's time stays. */
|
||||
async markShared(id: string, threadId: number, userId: number, at: string): Promise<boolean> {
|
||||
const result = await this.db()
|
||||
.updateTable('assistant_attachments')
|
||||
.set({ shared_at: at })
|
||||
.where('id', '=', id)
|
||||
.where('thread_id', '=', threadId)
|
||||
.where('user_id', '=', userId)
|
||||
.where('shared_at', 'is', null)
|
||||
.executeTakeFirst();
|
||||
return Number(result.numUpdatedRows) > 0;
|
||||
}
|
||||
|
||||
/** Names the trip file the attachment was kept as. */
|
||||
async setFile(id: string, fileId: number): Promise<void> {
|
||||
await this.db().updateTable('assistant_attachments').set({ file_id: fileId }).where('id', '=', id).execute();
|
||||
}
|
||||
|
||||
/** Where the bytes of the user's attachments in this thread sit. */
|
||||
async listObjectsOfThread(threadId: number, userId: number): Promise<AssistantAttachmentObject[]> {
|
||||
return this.db()
|
||||
.selectFrom('assistant_attachments')
|
||||
.select(['id', 'storage_key'])
|
||||
.where('thread_id', '=', threadId)
|
||||
.where('user_id', '=', userId)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Retention: the attachments made before `cutoff`, oldest first, at most `limit`. */
|
||||
async listCreatedBefore(cutoff: string, limit: number): Promise<AssistantAttachmentObject[]> {
|
||||
return this.db()
|
||||
.selectFrom('assistant_attachments')
|
||||
.select(['id', 'storage_key'])
|
||||
.where('created_at', '<', cutoff)
|
||||
.orderBy('created_at', 'asc')
|
||||
.limit(limit)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Which of these storage keys a row still names. */
|
||||
async listKnownKeys(keys: string[]): Promise<string[]> {
|
||||
if (keys.length === 0) return [];
|
||||
const rows = await this.db()
|
||||
.selectFrom('assistant_attachments')
|
||||
.select('storage_key')
|
||||
.where('storage_key', 'in', keys)
|
||||
.execute();
|
||||
return rows.map((row) => row.storage_key);
|
||||
}
|
||||
|
||||
/** Drops these attachments; answers how many went. */
|
||||
async deleteByIds(ids: string[]): Promise<number> {
|
||||
if (ids.length === 0) return 0;
|
||||
const result = await this.db().deleteFrom('assistant_attachments').where('id', 'in', ids).executeTakeFirst();
|
||||
return Number(result.numDeletedRows);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
import { AssistantAttachments } from '../../db/entities/AssistantAttachments.entity';
|
||||
import type {
|
||||
AssistantAttachmentObject,
|
||||
AssistantAttachmentsRepository,
|
||||
AssistantAttachmentSummary,
|
||||
AssistantAttachmentTextStatus,
|
||||
} from '../../db/repositories/AssistantAttachments.repository';
|
||||
import { dbNow } from '../../db/types';
|
||||
import { logError, logInfo } from '../audit/audit-log.logger';
|
||||
import { LlmParseService } from '../llm-parse/llm-parse.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { LLM_PHOTO_EXTENSIONS } from '@trek/shared';
|
||||
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { extname } from 'node:path';
|
||||
import { Readable } from 'node:stream';
|
||||
|
||||
/** How long an attached file is kept with its conversation. */
|
||||
export const ASSISTANT_ATTACHMENT_TTL_DAYS = 7;
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* Where the bytes sit: under the trip files' category, which is never served
|
||||
* by path (`/uploads/files` refuses every request), in a folder of their own.
|
||||
*/
|
||||
const STORAGE_CATEGORY = 'files';
|
||||
const KEY_PREFIX = 'assistant/';
|
||||
|
||||
/** The most text kept of one file; a model is sent a window of it, never all of it. */
|
||||
const MAX_STORED_TEXT_CHARS = 200_000;
|
||||
|
||||
/** Rows one sweep pass takes; the next night takes the rest. */
|
||||
const SWEEP_BATCH = 500;
|
||||
|
||||
/** Bytes put moments ago may belong to a turn whose row is not written yet; the sweep leaves them for an hour. */
|
||||
const ORPHAN_GRACE_MS = 60 * 60_000;
|
||||
|
||||
/** Files whose bytes hold no text TREK reads: photos, and the zipped wallet passes. */
|
||||
const UNREAD_EXTENSIONS: ReadonlySet<string> = new Set([...LLM_PHOTO_EXTENSIONS, '.pkpass']);
|
||||
|
||||
/** A file stored for a turn that has not written its rows yet. */
|
||||
export interface PreparedAttachment {
|
||||
id: string;
|
||||
name: string;
|
||||
mime: string;
|
||||
size: number;
|
||||
sha256: string;
|
||||
storageKey: string;
|
||||
text: string | null;
|
||||
textStatus: AssistantAttachmentTextStatus;
|
||||
}
|
||||
|
||||
/** A plain extension of a file name for its storage key, or none when the name carries something else. */
|
||||
function keyExtension(name: string): string {
|
||||
const ext = extname(name).toLowerCase();
|
||||
return /^\.[a-z0-9]{1,10}$/.test(ext) ? ext : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* The files users attach in the trip chat, kept with the conversation
|
||||
* (`assistant_attachments`): the bytes in storage, the text TREK read out of
|
||||
* them in the row. The text stays on the server; it reaches the model only
|
||||
* where the user agreed for that one file, and never a message part.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AssistantAttachmentsService {
|
||||
constructor(
|
||||
@InjectRepository(AssistantAttachments) private readonly rows: AssistantAttachmentsRepository,
|
||||
private readonly storage: StorageService,
|
||||
private readonly llmParse: LlmParseService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Puts the file's bytes into storage and reads its text, before the turn
|
||||
* writes anything: a storage that refuses the bytes refuses the turn. A file
|
||||
* whose text cannot be read is kept all the same, as `failed`.
|
||||
*/
|
||||
async prepare(file: Express.Multer.File, name: string): Promise<PreparedAttachment> {
|
||||
const id = randomUUID();
|
||||
const storageKey = `${KEY_PREFIX}${id}${keyExtension(name)}`;
|
||||
const mime = file.mimetype || 'application/octet-stream';
|
||||
await this.storage.put(STORAGE_CATEGORY, storageKey, Readable.from([file.buffer]), { contentType: mime });
|
||||
const sha256 = createHash('sha256').update(file.buffer).digest('hex');
|
||||
return { id, name, mime, size: file.size, sha256, storageKey, ...(await this.textOf(file.buffer, name)) };
|
||||
}
|
||||
|
||||
/** The row of a prepared file, in the thread its turn opened; written inside the turn's opening transaction. */
|
||||
async record(prepared: PreparedAttachment, at: { threadId: number; userId: number; tripId: number }): Promise<void> {
|
||||
await this.rows.insertAttachment({
|
||||
id: prepared.id,
|
||||
threadId: at.threadId,
|
||||
userId: at.userId,
|
||||
tripId: at.tripId,
|
||||
name: prepared.name,
|
||||
mime: prepared.mime,
|
||||
size: prepared.size,
|
||||
sha256: prepared.sha256,
|
||||
storageKey: prepared.storageKey,
|
||||
text: prepared.text,
|
||||
textStatus: prepared.textStatus,
|
||||
createdAt: dbNow(),
|
||||
});
|
||||
}
|
||||
|
||||
/** The bytes of a file whose turn never began; the sweep would find them otherwise, a night later. */
|
||||
async discard(prepared: PreparedAttachment): Promise<void> {
|
||||
await this.deleteObject(prepared.storageKey);
|
||||
}
|
||||
|
||||
/** The user's attachments in a thread, without their text. */
|
||||
listForThread(threadId: number, userId: number): Promise<AssistantAttachmentSummary[]> {
|
||||
return this.rows.listForThread(threadId, userId);
|
||||
}
|
||||
|
||||
/** Where the bytes of a thread's attachments sit, read before the thread is deleted. */
|
||||
objectsOfThread(threadId: number, userId: number): Promise<AssistantAttachmentObject[]> {
|
||||
return this.rows.listObjectsOfThread(threadId, userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the bytes of a thread that is gone; its rows went with it. An
|
||||
* object that cannot be deleted now is left for the sweep.
|
||||
*/
|
||||
async dropObjects(objects: readonly AssistantAttachmentObject[]): Promise<void> {
|
||||
await Promise.all(objects.map((object) => this.deleteObject(object.storage_key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The nightly sweep: attachments older than the TTL (bytes first, then the
|
||||
* row, so no row ever names bytes that are gone), then bytes no row names
|
||||
* any more (a thread or a user deleted with its rows, a turn that never
|
||||
* began). Answers how many attachments and loose objects went.
|
||||
*
|
||||
* @txIndependent a retention sweep in passes: each object is deleted from
|
||||
* storage on its own, and a row only after its bytes.
|
||||
*/
|
||||
async sweep(now: Date = new Date()): Promise<{ expired: number; orphaned: number }> {
|
||||
const cutoff = dbNow(new Date(now.getTime() - ASSISTANT_ATTACHMENT_TTL_DAYS * DAY_MS));
|
||||
const old = await this.rows.listCreatedBefore(cutoff, SWEEP_BATCH);
|
||||
const gone = await Promise.all(
|
||||
old.map(async (row) => ((await this.deleteObject(row.storage_key)) ? row.id : null)),
|
||||
);
|
||||
const expired = await this.rows.deleteByIds(gone.filter((id): id is string => id !== null));
|
||||
const orphaned = await this.sweepOrphans(now);
|
||||
if (expired + orphaned > 0) {
|
||||
logInfo(`Assistant retention: removed ${expired} attached file(s) and ${orphaned} loose object(s)`);
|
||||
}
|
||||
return { expired, orphaned };
|
||||
}
|
||||
|
||||
/** Objects under the attachments' folder that no row names, older than the grace period. */
|
||||
private async sweepOrphans(now: Date): Promise<number> {
|
||||
const listed: string[] = [];
|
||||
for await (const stat of this.storage.list(STORAGE_CATEGORY, KEY_PREFIX)) {
|
||||
if (now.getTime() - stat.mtimeMs > ORPHAN_GRACE_MS) listed.push(stat.key);
|
||||
}
|
||||
const pages = Array.from({ length: Math.ceil(listed.length / SWEEP_BATCH) }, (_page, i) =>
|
||||
listed.slice(i * SWEEP_BATCH, (i + 1) * SWEEP_BATCH),
|
||||
);
|
||||
// One page after another, so a large backlog never asks the database for all its keys at once.
|
||||
return pages.reduce<Promise<number>>(async (previous, page) => {
|
||||
const done = await previous;
|
||||
const known = new Set(await this.rows.listKnownKeys(page));
|
||||
const deleted = await Promise.all(page.filter((key) => !known.has(key)).map((key) => this.deleteObject(key)));
|
||||
return done + deleted.filter(Boolean).length;
|
||||
}, Promise.resolve(0));
|
||||
}
|
||||
|
||||
/** Deletes one object; a failure is logged and answered as false, the next sweep tries again. */
|
||||
private async deleteObject(key: string): Promise<boolean> {
|
||||
try {
|
||||
await this.storage.delete(STORAGE_CATEGORY, key);
|
||||
return true;
|
||||
} catch (err) {
|
||||
logError(
|
||||
`[assistant] could not delete attached file ${key}: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** The text TREK reads out of a file, and how that went. Never throws. */
|
||||
private async textOf(
|
||||
buffer: Buffer,
|
||||
name: string,
|
||||
): Promise<{ text: string | null; textStatus: AssistantAttachmentTextStatus }> {
|
||||
const ext = extname(name).toLowerCase();
|
||||
if (UNREAD_EXTENSIONS.has(ext)) return { text: null, textStatus: 'unsupported' };
|
||||
try {
|
||||
const text = (await this.llmParse.readText(buffer, name)).trim();
|
||||
if (text) return { text: text.slice(0, MAX_STORED_TEXT_CHARS), textStatus: 'ok' };
|
||||
return { text: null, textStatus: ext === '.pdf' ? 'scanned' : 'empty' };
|
||||
} catch (err) {
|
||||
logError(`[assistant] could not read the text of ${name}: ${err instanceof Error ? err.message : String(err)}`);
|
||||
return { text: null, textStatus: 'failed' };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { AiConnectionsService } from '../ai-connections/ai-connections.service';
|
||||
import { AiUsageService } from '../ai-connections/ai-usage.service';
|
||||
import { logError, logInfo } from '../audit/audit-log.logger';
|
||||
import { CronRegistrarService } from '../scheduling/cron-registrar.service';
|
||||
import { AssistantAttachmentsService } from './assistant-attachments.service';
|
||||
import { decisionCutoff } from './assistant-tools.constants';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
|
||||
@@ -20,8 +21,9 @@ const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
* Nightly sweep of the assistant's conversations: threads nobody wrote in
|
||||
* for longer than the admin's retention (none under "forever"), threads of
|
||||
* users who no longer belong to their trip or journey, changes that waited a day
|
||||
* without a decision (expired, so no card waits forever), and usage days
|
||||
* older than the usage retention.
|
||||
* without a decision (expired, so no card waits forever), usage days
|
||||
* older than the usage retention, and the files attached in the chat once
|
||||
* they are a week old or their conversation is gone.
|
||||
*/
|
||||
@Injectable()
|
||||
export class AssistantRetentionJob implements OnApplicationBootstrap {
|
||||
@@ -31,6 +33,7 @@ export class AssistantRetentionJob implements OnApplicationBootstrap {
|
||||
private readonly connections: AiConnectionsService,
|
||||
private readonly usage: AiUsageService,
|
||||
private readonly registrar: CronRegistrarService,
|
||||
private readonly attachments: AssistantAttachmentsService,
|
||||
) {}
|
||||
|
||||
onApplicationBootstrap(): void {
|
||||
@@ -54,6 +57,7 @@ export class AssistantRetentionJob implements OnApplicationBootstrap {
|
||||
const expired = await this.toolCalls.expireBefore(decisionCutoff(now));
|
||||
if (expired.length > 0) logInfo(`Assistant retention: ${expired.length} proposed change(s) expired`);
|
||||
await this.usage.purgeOlderThan(ASSISTANT_USAGE_RETENTION_DAYS);
|
||||
await this.attachments.sweep(now);
|
||||
} catch (err: unknown) {
|
||||
logError(`Assistant retention: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AssistantAttachments } from '../../db/entities/AssistantAttachments.entity';
|
||||
import { AssistantMessages } from '../../db/entities/AssistantMessages.entity';
|
||||
import { AssistantThreads } from '../../db/entities/AssistantThreads.entity';
|
||||
import { AssistantToolCalls } from '../../db/entities/AssistantToolCalls.entity';
|
||||
@@ -10,14 +11,17 @@ import { CollabModule } from '../collab/collab.module';
|
||||
import { RateLimitModule } from '../common/rate-limit.module';
|
||||
import { JourneyBookModule } from '../journey/journey-book.module';
|
||||
import { JourneyDomainModule } from '../journey/journey-domain.module';
|
||||
import { LlmParseModule } from '../llm-parse/llm-parse.module';
|
||||
import { McpSharedModule } from '../mcp-shared/mcp-shared.module';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { PhotoSignalsModule } from '../photos/photo-signals.module';
|
||||
import { SchedulingModule } from '../scheduling/scheduling.module';
|
||||
import { SettingsModule } from '../settings/settings.module';
|
||||
import { StorageModule } from '../storage/storage.module';
|
||||
import { TourFilesModule } from '../tours/tour-files.module';
|
||||
import { TripMembershipModule } from '../trip-membership/trip-membership.module';
|
||||
import { AssistantAttachmentsController } from './assistant-attachments.controller';
|
||||
import { AssistantAttachmentsService } from './assistant-attachments.service';
|
||||
import { AssistantBookContext } from './assistant-book-context';
|
||||
import { AssistantBookGate } from './assistant-book-gate';
|
||||
import { AssistantBookingImportService } from './assistant-booking-import.service';
|
||||
@@ -84,7 +88,11 @@ import { Module } from '@nestjs/common';
|
||||
TripMembershipModule,
|
||||
// P7: a route file attached in the chat, read into its tracks.
|
||||
TourFilesModule,
|
||||
MikroOrmModule.forFeature([AssistantThreads, AssistantMessages, AssistantToolCalls]),
|
||||
// A file attached in the chat is kept with its conversation: its bytes in storage, its text read as the
|
||||
// AI import reads it.
|
||||
StorageModule,
|
||||
LlmParseModule,
|
||||
MikroOrmModule.forFeature([AssistantThreads, AssistantMessages, AssistantToolCalls, AssistantAttachments]),
|
||||
],
|
||||
controllers: [
|
||||
AssistantController,
|
||||
@@ -101,6 +109,7 @@ import { Module } from '@nestjs/common';
|
||||
AssistantToolsService,
|
||||
AssistantBookingImportService,
|
||||
AssistantFileAnswers,
|
||||
AssistantAttachmentsService,
|
||||
AssistantTourFileService,
|
||||
AssistantFoundPlacesService,
|
||||
AssistantProposalsService,
|
||||
|
||||
@@ -34,6 +34,7 @@ import { UnitOfWork } from '../database/unit-of-work';
|
||||
import { SettingsService } from '../settings/settings.service';
|
||||
import { runAgentLoop, type AgentStore, type HeldCall, type StepRecord } from './assistant-agent';
|
||||
import type { AttachedFileReader } from './assistant-attached-file';
|
||||
import { AssistantAttachmentsService } from './assistant-attachments.service';
|
||||
import { AssistantBookingImportService } from './assistant-booking-import.service';
|
||||
import { mentionItems } from './assistant-cards.helpers';
|
||||
import { todayIn } from './assistant-context';
|
||||
@@ -131,6 +132,15 @@ interface TurnSpec {
|
||||
renders?: boolean;
|
||||
}
|
||||
|
||||
/** What a turn opens with: its thread (null for a new one), the user's message, and what is written with them. */
|
||||
interface TurnOpening {
|
||||
threadId: number | null;
|
||||
titleText: string;
|
||||
model: string;
|
||||
userParts: AssistantMessagePart[];
|
||||
alongside?: (threadId: number) => Promise<void>;
|
||||
}
|
||||
|
||||
function summaryOf(thread: AssistantThreadRow): AssistantThreadSummary {
|
||||
return {
|
||||
id: thread.id,
|
||||
@@ -170,6 +180,7 @@ export class AssistantService {
|
||||
private readonly tools: AssistantToolsService,
|
||||
private readonly bookings: AssistantBookingImportService,
|
||||
private readonly fileAnswers: AssistantFileAnswers,
|
||||
private readonly attachments: AssistantAttachmentsService,
|
||||
private readonly tourFiles: AssistantTourFileService,
|
||||
private readonly settings: SettingsService,
|
||||
private readonly rateLimit: RateLimitService,
|
||||
@@ -192,8 +203,11 @@ export class AssistantService {
|
||||
return { thread: summaryOf(thread), messages: toThreadMessages(rows, await this.callsOf(rows), new Date()) };
|
||||
}
|
||||
|
||||
/** Deletes the thread with its rows, then the bytes of the files attached in it. */
|
||||
async deleteThread(userId: number, scope: AssistantScope, threadId: number): Promise<void> {
|
||||
const kept = scope.kind === 'trip' ? await this.attachments.objectsOfThread(threadId, userId) : [];
|
||||
if (!(await scopedThreads(this.threads, scope).remove(threadId, userId))) throw threadNotFound();
|
||||
await this.attachments.dropObjects(kept);
|
||||
}
|
||||
|
||||
/** Stops the user's running turn, wherever it runs; nothing to stop is not an error. */
|
||||
@@ -219,10 +233,15 @@ export class AssistantService {
|
||||
// The labels the message keeps come from the trip, never from the request.
|
||||
const card = mentions.length > 0 && tripId !== null ? await readTripCard(this.tools, userId, tripId) : null;
|
||||
const items = card ? mentionItems(card, mentions, req.locale) : [];
|
||||
const begun = await this.beginTurn(userId, scope, req.threadId, req.text, connection.model, [
|
||||
{ type: 'text', text: req.text },
|
||||
...(items.length > 0 ? [{ type: 'data-trek-mentions' as const, data: { items } }] : []),
|
||||
]);
|
||||
const begun = await this.beginTurn(userId, scope, {
|
||||
threadId: req.threadId,
|
||||
titleText: req.text,
|
||||
model: connection.model,
|
||||
userParts: [
|
||||
{ type: 'text', text: req.text },
|
||||
...(items.length > 0 ? [{ type: 'data-trek-mentions' as const, data: { items } }] : []),
|
||||
],
|
||||
});
|
||||
const stream = channel.open();
|
||||
const w = new UiStreamWriter(stream);
|
||||
w.start(messageIdOf(begun.rowId));
|
||||
@@ -274,9 +293,18 @@ export class AssistantService {
|
||||
await this.withinDailyLimit(userId, connection);
|
||||
await this.claimed(userId, channel.signal, async (signal) => {
|
||||
const name = file.originalname.slice(0, MAX_FILE_NAME_CHARS);
|
||||
const begun = await this.beginTurn(userId, tripScope(tripId), req.threadId ?? null, name, connection.model, [
|
||||
{ type: 'attachment', name, mime: file.mimetype || 'application/octet-stream', size: file.size },
|
||||
]);
|
||||
// The file is kept before anything is written: a storage that refuses it refuses the turn.
|
||||
const kept = await this.attachments.prepare(file, name);
|
||||
const begun = await this.beginTurn(userId, tripScope(tripId), {
|
||||
threadId: req.threadId ?? null,
|
||||
titleText: name,
|
||||
model: connection.model,
|
||||
userParts: [{ type: 'attachment', name, mime: kept.mime, size: file.size, attachmentId: kept.id }],
|
||||
alongside: (threadId) => this.attachments.record(kept, { threadId, userId, tripId }),
|
||||
}).catch(async (err: unknown) => {
|
||||
await this.attachments.discard(kept);
|
||||
throw err;
|
||||
});
|
||||
const stream = channel.open();
|
||||
const w = new UiStreamWriter(stream);
|
||||
w.start(messageIdOf(begun.rowId));
|
||||
@@ -499,17 +527,12 @@ export class AssistantService {
|
||||
/**
|
||||
* A turn's opening rows, in one transaction: the thread (found or, with
|
||||
* `threadId` null, created and titled from `titleText`), the user's message,
|
||||
* and the row the answer's first step writes into. Changes still waiting in
|
||||
* and the row the answer's first step writes into, with whatever goes
|
||||
* `alongside` them (the row of an attached file). Changes still waiting in
|
||||
* the thread are cancelled: the user moved on.
|
||||
*/
|
||||
private async beginTurn(
|
||||
userId: number,
|
||||
scope: AssistantScope,
|
||||
threadId: number | null,
|
||||
titleText: string,
|
||||
model: string,
|
||||
userParts: AssistantMessagePart[],
|
||||
) {
|
||||
private async beginTurn(userId: number, scope: AssistantScope, opening: TurnOpening) {
|
||||
const { threadId, titleText, model, userParts, alongside } = opening;
|
||||
const now = dbNow();
|
||||
const threads = scopedThreads(this.threads, scope);
|
||||
return this.uow.transactional(async () => {
|
||||
@@ -535,6 +558,7 @@ export class AssistantService {
|
||||
});
|
||||
const rowId = await this.messages.append({ ...base, role: 'assistant', parts: '[]', status: 'aborted', model });
|
||||
await this.threads.touch(id, now);
|
||||
await alongside?.(id);
|
||||
return { threadId: id, title, created: threadId === null, rowId };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -69,6 +69,25 @@ export class LlmParseService {
|
||||
return this.llmConfig.readInstanceEndpoint();
|
||||
}
|
||||
|
||||
/**
|
||||
* The text of a document as the AI fallback reads it (an e-mail's body, a
|
||||
* PDF's text layer, a page without its markup), for a caller outside this
|
||||
* module: the assistant keeps it with a file attached in the chat. A scanned
|
||||
* PDF answers an empty string. Asks no model.
|
||||
*/
|
||||
readText(buffer: Buffer, fileName: string): Promise<string> {
|
||||
return extractText(buffer, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* A text cut to what one model request should carry of it, its start and
|
||||
* its end kept: the cloud window, or the compact one for a server whose
|
||||
* context may be small (text-window.ts).
|
||||
*/
|
||||
fitText(text: string, compact: boolean): string {
|
||||
return fitTextWindow(text, compact ? COMPACT_TEXT_WINDOW : CLOUD_TEXT_WINDOW).text;
|
||||
}
|
||||
|
||||
/** True when the addon is enabled AND a usable config resolves for this user. */
|
||||
async isAvailable(userId: number): Promise<boolean> {
|
||||
return (await this.llmConfig.resolve(userId)) !== null;
|
||||
|
||||
Reference in New Issue
Block a user