mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 23:13:35 +00:00
🪓 refactor: Separate Local Prompt Storage from Content Resolution (#17285)
Some checks failed
Publish `@librechat/client` to NPM / pack (push) Has been cancelled
Publish `librechat-data-provider` to NPM / pack (push) Has been cancelled
Publish `@librechat/data-schemas` to NPM / pack (push) Has been cancelled
Docker Dev Images Build / publish (push) Has been cancelled
Sync Helm Chart Tags / Ignore non-main push (push) Has been cancelled
Sync Helm Chart Tags / Sync chart tags (push) Has been cancelled
Publish `@librechat/client` to NPM / publish-npm (push) Has been cancelled
Publish `librechat-data-provider` to NPM / publish-npm (push) Has been cancelled
Publish `@librechat/data-schemas` to NPM / publish-npm (push) Has been cancelled
Some checks failed
Publish `@librechat/client` to NPM / pack (push) Has been cancelled
Publish `librechat-data-provider` to NPM / pack (push) Has been cancelled
Publish `@librechat/data-schemas` to NPM / pack (push) Has been cancelled
Docker Dev Images Build / publish (push) Has been cancelled
Sync Helm Chart Tags / Ignore non-main push (push) Has been cancelled
Sync Helm Chart Tags / Sync chart tags (push) Has been cancelled
Publish `@librechat/client` to NPM / publish-npm (push) Has been cancelled
Publish `librechat-data-provider` to NPM / publish-npm (push) Has been cancelled
Publish `@librechat/data-schemas` to NPM / publish-npm (push) Has been cancelled
Move every local prompt group and native-revision read and write behind
one PromptStore (createPromptStore) and reduce the native adapter to
resolvePrompt, the contract the Langfuse adapter already has. The
service receives { store, sources } and dispatches resolution by group
source. Service behavior, errors (including unsupported), database
calls and result shapes are unchanged.
Co-authored-by: Rebase Probe <probe@example.com>
This commit is contained in:
parent
a4c80ee0b4
commit
a378657583
9 changed files with 457 additions and 416 deletions
|
|
@ -3,7 +3,7 @@ import { MongoMemoryServer } from 'mongodb-memory-server';
|
|||
import { createModels, createMethods } from '@librechat/data-schemas';
|
||||
import type { PromptDatabase, PromptRecord } from './types';
|
||||
import { createPromptAccessResolvers } from './access';
|
||||
import { createNativePromptAdapter } from './native';
|
||||
import { createPromptStore } from './store';
|
||||
|
||||
let mongo: MongoMemoryServer;
|
||||
let db: PromptDatabase;
|
||||
|
|
@ -17,7 +17,7 @@ beforeAll(async () => {
|
|||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const created = await createNativePromptAdapter(db).createPromptGroup({
|
||||
const created = await createPromptStore(db).createPromptGroup({
|
||||
prompt: { prompt: 'Prompt', type: 'text' },
|
||||
group: { name: 'Access group' },
|
||||
author: new Types.ObjectId().toString(),
|
||||
|
|
|
|||
|
|
@ -11,4 +11,5 @@ export * from './records';
|
|||
export * from './schemas';
|
||||
export { createPromptService } from './service';
|
||||
export type { PromptService, PromptServiceDependencies, PromptServiceListResult } from './service';
|
||||
export * from './store';
|
||||
export * from './types';
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ import mongoose, { Types } from 'mongoose';
|
|||
import { MongoMemoryServer } from 'mongodb-memory-server';
|
||||
import { createModels, createMethods } from '@librechat/data-schemas';
|
||||
import type { PromptDatabase, PromptGroupRecord, PromptRecord } from './types';
|
||||
import { createNativePromptAdapter, createPromptCatalogStore } from './native';
|
||||
import { createNativePromptAdapter } from './native';
|
||||
import { createPromptStore } from './store';
|
||||
|
||||
let mongo: MongoMemoryServer;
|
||||
let db: PromptDatabase;
|
||||
|
|
@ -20,22 +21,20 @@ beforeAll(async () => {
|
|||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const created = await adapter.createPromptGroup({
|
||||
const store = createPromptStore(db);
|
||||
const created = await store.createPromptGroup({
|
||||
prompt: { prompt: 'Production prompt', type: 'text' },
|
||||
group: { name: 'Native group' },
|
||||
author,
|
||||
authorName: 'Author',
|
||||
});
|
||||
production = created.prompt as PromptRecord;
|
||||
({ prompt: draft } = (await adapter.savePrompt?.({
|
||||
({ prompt: draft } = await store.savePrompt({
|
||||
groupId: production.groupId,
|
||||
prompt: { prompt: 'Draft prompt', type: 'chat' },
|
||||
author,
|
||||
})) as { prompt: PromptRecord });
|
||||
group = (await createPromptCatalogStore(db).getPromptGroup(
|
||||
production.groupId,
|
||||
)) as PromptGroupRecord;
|
||||
}));
|
||||
group = (await store.getPromptGroup(production.groupId)) as PromptGroupRecord;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
|
|
@ -50,25 +49,8 @@ afterAll(async () => {
|
|||
});
|
||||
|
||||
describe('createNativePromptAdapter', () => {
|
||||
it('returns records with string IDs', () => {
|
||||
expect(group).toEqual(
|
||||
expect.objectContaining({
|
||||
_id: production.groupId,
|
||||
author,
|
||||
source: 'native',
|
||||
productionId: production._id,
|
||||
productionPrompt: expect.objectContaining({
|
||||
_id: production._id,
|
||||
groupId: production.groupId,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(typeof draft._id).toBe('string');
|
||||
expect(draft).toEqual(expect.objectContaining({ groupId: group._id, author, type: 'chat' }));
|
||||
});
|
||||
|
||||
it('resolves Production from the group inlined revision without reading it again', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
const readPrompt = jest.spyOn(db, 'getPrompt');
|
||||
|
||||
await expect(
|
||||
|
|
@ -87,7 +69,7 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('reads the Production revision when the group has no inlined revision', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
const readPrompt = jest.spyOn(db, 'getPrompt');
|
||||
const { productionPrompt: _productionPrompt, ...groupWithoutInlinedRevision } = group;
|
||||
|
||||
|
|
@ -101,7 +83,7 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('resolves an exact revision from a loaded revision without another read', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
const readPrompt = jest.spyOn(db, 'getPrompt');
|
||||
|
||||
await expect(
|
||||
|
|
@ -124,7 +106,7 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('reads an exact revision when the loaded revision is a different revision', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
const readPrompt = jest.spyOn(db, 'getPrompt');
|
||||
|
||||
await expect(
|
||||
|
|
@ -138,7 +120,7 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('returns unavailable_selection for a missing revision or group with no Production', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
|
||||
await expect(
|
||||
adapter.resolvePrompt({
|
||||
|
|
@ -161,7 +143,7 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('does not resolve a revision belonging to another group', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
const otherGroup = { ...group, _id: new Types.ObjectId().toString() };
|
||||
|
||||
await expect(
|
||||
|
|
@ -182,7 +164,7 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('returns unsupported_selection for a version selection', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
|
||||
await expect(
|
||||
adapter.resolvePrompt({ group, selection: { type: 'version', version: 1 } }),
|
||||
|
|
@ -193,14 +175,12 @@ describe('createNativePromptAdapter', () => {
|
|||
});
|
||||
|
||||
it('resolves a stored revision without a type as text', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const adapter = createNativePromptAdapter(createPromptStore(db));
|
||||
await mongoose.models.Prompt.collection.updateMany(
|
||||
{ groupId: new Types.ObjectId(group._id) },
|
||||
{ $unset: { type: '' } },
|
||||
);
|
||||
const loaded = (await createPromptCatalogStore(db).getPromptGroup(
|
||||
group._id,
|
||||
)) as PromptGroupRecord;
|
||||
const loaded = (await createPromptStore(db).getPromptGroup(group._id)) as PromptGroupRecord;
|
||||
|
||||
await expect(
|
||||
adapter.resolvePrompt({ group: loaded, selection: { type: 'production' } }),
|
||||
|
|
@ -212,143 +192,4 @@ describe('createNativePromptAdapter', () => {
|
|||
}),
|
||||
).resolves.toMatchObject({ ok: true, value: { promptId: draft._id, type: 'text' } });
|
||||
});
|
||||
|
||||
it('returns null for an absent revision', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
const missing = new Types.ObjectId().toString();
|
||||
|
||||
await expect(adapter.getPrompt(missing)).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('returns the group revisions newest first', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
await mongoose.models.Prompt.collection.updateOne(
|
||||
{ _id: new Types.ObjectId(draft._id) },
|
||||
{ $set: { createdAt: new Date(Date.now() + 1000) } },
|
||||
);
|
||||
|
||||
const revisions = await adapter.getPrompts(group._id);
|
||||
|
||||
expect(revisions.map((revision) => revision._id)).toEqual([draft._id, production._id]);
|
||||
});
|
||||
|
||||
it('promotes a revision and throws for a missing revision', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
|
||||
await adapter.makePromptProduction?.(draft._id);
|
||||
|
||||
await expect(createPromptCatalogStore(db).getPromptGroup(group._id)).resolves.toMatchObject({
|
||||
productionId: draft._id,
|
||||
});
|
||||
await expect(adapter.makePromptProduction?.(new Types.ObjectId().toString())).rejects.toThrow(
|
||||
'Prompt not found',
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a string group ID when deleting the last revision deletes the group', async () => {
|
||||
const adapter = createNativePromptAdapter(db);
|
||||
|
||||
await expect(
|
||||
adapter.deletePrompt?.({ groupId: group._id, promptId: draft._id }),
|
||||
).resolves.toEqual({ prompt: 'Prompt deleted successfully' });
|
||||
await expect(
|
||||
adapter.deletePrompt?.({ groupId: group._id, promptId: production._id }),
|
||||
).resolves.toEqual({
|
||||
prompt: 'Prompt deleted successfully',
|
||||
promptGroup: { message: 'Prompt group deleted successfully', id: group._id },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createPromptCatalogStore', () => {
|
||||
it('returns a group with string IDs through getPromptGroup', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
|
||||
await expect(catalog.getPromptGroup(production.groupId)).resolves.toEqual(group);
|
||||
});
|
||||
|
||||
it('returns null for a missing group', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
|
||||
await expect(catalog.getPromptGroup(new Types.ObjectId().toString())).resolves.toBeNull();
|
||||
});
|
||||
|
||||
describe('findSourceGroup and createSourceGroup', () => {
|
||||
const identity = {
|
||||
sourceDestination: 'us',
|
||||
sourceProjectId: 'project-1',
|
||||
sourcePromptName: 'synced-prompt',
|
||||
};
|
||||
|
||||
it('returns null for an identity with no stored group', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
|
||||
await expect(catalog.findSourceGroup(identity)).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('creates a source group and then finds it by identity', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
|
||||
const created = await catalog.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
expect(created).toEqual({ ok: true, value: { _id: expect.any(String) } });
|
||||
await expect(catalog.findSourceGroup(identity)).resolves.toEqual({
|
||||
_id: created.ok ? created.value._id : undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a duplicate error for a second insert with the same identity', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
await catalog.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
const result = await catalog.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt again',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
expect(result).toEqual({ ok: false, error: { code: 'duplicate' } });
|
||||
});
|
||||
|
||||
it('returns an invalid error with a cause for an empty name', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
|
||||
const result = await catalog.createSourceGroup({
|
||||
identity,
|
||||
name: '',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
ok: false,
|
||||
error: { code: 'invalid', cause: expect.any(Error) },
|
||||
});
|
||||
});
|
||||
|
||||
it('rethrows a non-Mongo, non-validation error', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
jest.spyOn(db, 'createLangfusePromptGroup').mockRejectedValueOnce(new Error('unavailable'));
|
||||
|
||||
await expect(
|
||||
catalog.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
}),
|
||||
).rejects.toThrow('unavailable');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
import type {
|
||||
StoredId,
|
||||
PromptStore,
|
||||
PromptRecord,
|
||||
PromptDatabase,
|
||||
PromptSelection,
|
||||
PromptProjection,
|
||||
PromptGroupRecord,
|
||||
PromptCatalogStore,
|
||||
NativePromptAdapter,
|
||||
PromptSourceAdapter,
|
||||
NativeResolvedPrompt,
|
||||
} from './types';
|
||||
import { toPromptGroupRecord, toPromptRecord } from './records';
|
||||
|
||||
type ResolvablePrompt = Pick<PromptRecord, '_id' | 'groupId' | 'prompt' | 'type'>;
|
||||
type NativeResolvedValue = Omit<NativeResolvedPrompt, 'source'>;
|
||||
|
|
@ -37,30 +34,17 @@ function resolveValue(revision: ResolvablePrompt): NativeResolvedValue {
|
|||
};
|
||||
}
|
||||
|
||||
async function getPrompt(db: PromptDatabase, promptId: string): Promise<PromptRecord | null> {
|
||||
const record = await db.getPrompt({ _id: promptId });
|
||||
return record == null ? null : toPromptRecord(record);
|
||||
}
|
||||
|
||||
async function getPromptGroup(
|
||||
db: PromptDatabase,
|
||||
groupId: string,
|
||||
): Promise<PromptGroupRecord | null> {
|
||||
const record = await db.getPromptGroup({ _id: groupId });
|
||||
return record == null ? null : toPromptGroupRecord(record);
|
||||
}
|
||||
|
||||
/** Resolves an exact revision, reading it when `loadedRevision` does not already
|
||||
* match this group and promptId. */
|
||||
async function resolveExact(
|
||||
db: PromptDatabase,
|
||||
store: Pick<PromptStore, 'getPrompt'>,
|
||||
groupId: string,
|
||||
promptId: string,
|
||||
loadedRevision?: PromptRecord | null,
|
||||
): Promise<NativeResolvedValue | null> {
|
||||
let revision = loadedRevision;
|
||||
if (!isMatchingRevision(revision, groupId, promptId)) {
|
||||
revision = await getPrompt(db, promptId);
|
||||
revision = await store.getPrompt(promptId);
|
||||
}
|
||||
return isMatchingRevision(revision, groupId, promptId) ? resolveValue(revision) : null;
|
||||
}
|
||||
|
|
@ -71,7 +55,7 @@ async function resolveExact(
|
|||
* `productionPrompt` is absent or stale it falls back to one `getPrompt` read.
|
||||
*/
|
||||
async function resolveProduction(
|
||||
db: PromptDatabase,
|
||||
store: Pick<PromptStore, 'getPrompt'>,
|
||||
group: PromptGroupRecord,
|
||||
): Promise<NativeResolvedValue | null> {
|
||||
if (group.productionId == null) {
|
||||
|
|
@ -80,33 +64,21 @@ async function resolveProduction(
|
|||
if (isMatchingRevision(group.productionPrompt, group._id, group.productionId)) {
|
||||
return resolveValue(group.productionPrompt);
|
||||
}
|
||||
const revision = await getPrompt(db, group.productionId);
|
||||
const revision = await store.getPrompt(group.productionId);
|
||||
return isMatchingRevision(revision, group._id, group.productionId)
|
||||
? resolveValue(revision)
|
||||
: null;
|
||||
}
|
||||
|
||||
function toIdString(id: StoredId): string {
|
||||
return typeof id === 'string' ? id : id.toString();
|
||||
}
|
||||
|
||||
/** The AI-2272 partial unique index is defense in depth: this maps the resulting
|
||||
* duplicate-key race to `createSourceGroup`'s `duplicate` outcome. */
|
||||
function isDuplicateKeyError(error: unknown): boolean {
|
||||
return (error as { code?: number } | undefined)?.code === 11000;
|
||||
}
|
||||
|
||||
/** A document validation failure, mapped to `createSourceGroup`'s `invalid` outcome. */
|
||||
function isValidationError(error: unknown): boolean {
|
||||
return error instanceof Error && error.name === 'ValidationError';
|
||||
}
|
||||
|
||||
export function selectionUnavailableReason(selection: PromptSelection): 'production' | 'revision' {
|
||||
return selection.type === 'production' ? 'production' : 'revision';
|
||||
}
|
||||
|
||||
/** Native prompts stored in the LibreChat database. */
|
||||
export function createNativePromptAdapter(db: PromptDatabase): NativePromptAdapter {
|
||||
/** Native prompts stored in the LibreChat database; resolves content through the
|
||||
* store's `getPrompt`. */
|
||||
export function createNativePromptAdapter(
|
||||
store: Pick<PromptStore, 'getPrompt'>,
|
||||
): PromptSourceAdapter {
|
||||
return {
|
||||
resolvePrompt: async ({ group, selection, loadedRevision }) => {
|
||||
if (selection.type === 'version') {
|
||||
|
|
@ -114,8 +86,8 @@ export function createNativePromptAdapter(db: PromptDatabase): NativePromptAdapt
|
|||
}
|
||||
const resolved =
|
||||
selection.type === 'exact'
|
||||
? await resolveExact(db, group._id, selection.promptId, loadedRevision)
|
||||
: await resolveProduction(db, group);
|
||||
? await resolveExact(store, group._id, selection.promptId, loadedRevision)
|
||||
: await resolveProduction(store, group);
|
||||
if (resolved == null) {
|
||||
return {
|
||||
ok: false,
|
||||
|
|
@ -124,75 +96,5 @@ export function createNativePromptAdapter(db: PromptDatabase): NativePromptAdapt
|
|||
}
|
||||
return { ok: true, value: { source: 'native', ...resolved } };
|
||||
},
|
||||
getPrompt: (promptId) => getPrompt(db, promptId),
|
||||
getPrompts: async (groupId) => (await db.getPrompts({ groupId })).map(toPromptRecord),
|
||||
createPromptGroup: async ({ prompt, group = {}, author, authorName }) => {
|
||||
const result = await db.createPromptGroup({ prompt, group, author, authorName });
|
||||
return {
|
||||
prompt: result.prompt == null ? null : toPromptRecord(result.prompt),
|
||||
group: toPromptGroupRecord(result.group),
|
||||
};
|
||||
},
|
||||
savePrompt: async ({ groupId, prompt, author }) => {
|
||||
const result = await db.savePrompt({ prompt: { ...prompt, groupId }, author });
|
||||
return { prompt: toPromptRecord(result.prompt) };
|
||||
},
|
||||
makePromptProduction: (promptId) => db.makePromptProduction(promptId),
|
||||
deletePrompt: async ({ groupId, promptId }) => {
|
||||
const result = await db.deletePrompt({ groupId, promptId });
|
||||
if (result.promptGroup == null) {
|
||||
return { prompt: result.prompt };
|
||||
}
|
||||
return {
|
||||
prompt: result.prompt,
|
||||
promptGroup: { message: result.promptGroup.message, id: toIdString(result.promptGroup.id) },
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Local catalog operations over the LibreChat prompt group collection. */
|
||||
export function createPromptCatalogStore(db: PromptDatabase): PromptCatalogStore {
|
||||
return {
|
||||
getPromptGroup: (groupId) => getPromptGroup(db, groupId),
|
||||
getListPromptGroupsByAccess: async ({ accessibleIds, name, category, limit, after }) => {
|
||||
const result = await db.getListPromptGroupsByAccess({
|
||||
accessibleIds: [...accessibleIds],
|
||||
name,
|
||||
category,
|
||||
limit,
|
||||
after,
|
||||
});
|
||||
return {
|
||||
data: result.data.map(toPromptGroupRecord),
|
||||
has_more: result.has_more,
|
||||
after: result.after,
|
||||
};
|
||||
},
|
||||
updatePromptGroup: async (groupId, updates) =>
|
||||
toPromptGroupRecord(await db.updatePromptGroup({ _id: groupId }, updates)),
|
||||
incrementPromptGroupUsage: (groupId) => db.incrementPromptGroupUsage(groupId),
|
||||
deletePromptGroup: (groupId) => db.deletePromptGroup({ _id: groupId }),
|
||||
findSourceGroup: (identity) => db.findLangfusePromptGroup(identity),
|
||||
createSourceGroup: async (input) => {
|
||||
const { identity, name, author, authorName } = input;
|
||||
try {
|
||||
const value = await db.createLangfusePromptGroup({
|
||||
...identity,
|
||||
name,
|
||||
author,
|
||||
authorName,
|
||||
});
|
||||
return { ok: true, value };
|
||||
} catch (error) {
|
||||
if (isDuplicateKeyError(error)) {
|
||||
return { ok: false, error: { code: 'duplicate' } };
|
||||
}
|
||||
if (isValidationError(error)) {
|
||||
return { ok: false, error: { code: 'invalid', cause: error } };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,9 @@ import type {
|
|||
} from './types';
|
||||
import type { PromptServiceDependencies, PromptService } from './service';
|
||||
import { createPromptServiceFromAdapters, createPromptService } from './service';
|
||||
import { createPromptCatalogStore, createNativePromptAdapter } from './native';
|
||||
import { createNativePromptAdapter } from './native';
|
||||
import { PromptStoreError } from './errors';
|
||||
import { createPromptStore } from './store';
|
||||
import { CONSTANTS } from '~/mcp/enum';
|
||||
|
||||
const filters: FiltersConfig = {
|
||||
|
|
@ -69,9 +70,7 @@ beforeEach(async () => {
|
|||
throw new Error('Test group was not created');
|
||||
}
|
||||
production = created.value.prompt;
|
||||
group = (await createPromptCatalogStore(db).getPromptGroup(
|
||||
production.groupId,
|
||||
)) as PromptGroupRecord;
|
||||
group = (await createPromptStore(db).getPromptGroup(production.groupId)) as PromptGroupRecord;
|
||||
grantPermission.mockClear();
|
||||
});
|
||||
|
||||
|
|
@ -214,13 +213,14 @@ describe('createPromptService', () => {
|
|||
});
|
||||
|
||||
it('throws a PromptStoreError when the group read fails', async () => {
|
||||
const native = createNativePromptAdapter(db);
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
jest.spyOn(catalog, 'getPromptGroup').mockRejectedValue(new Error('group read failed'));
|
||||
const store = createPromptStore(db);
|
||||
jest.spyOn(store, 'getPromptGroup').mockRejectedValue(new Error('group read failed'));
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native,
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
catalog,
|
||||
store,
|
||||
sources: {
|
||||
native: createNativePromptAdapter(store),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
},
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
@ -501,8 +501,8 @@ describe('createPromptService', () => {
|
|||
});
|
||||
|
||||
it('treats a race where findSourceGroup misses a row that already exists as unchanged, without granting', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
const created = await catalog.createSourceGroup({
|
||||
const store = createPromptStore(db);
|
||||
const created = await store.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
|
|
@ -511,12 +511,14 @@ describe('createPromptService', () => {
|
|||
if (!created.ok) {
|
||||
throw new Error('Setup insert failed');
|
||||
}
|
||||
jest.spyOn(catalog, 'findSourceGroup').mockResolvedValue(null);
|
||||
jest.spyOn(store, 'findSourceGroup').mockResolvedValue(null);
|
||||
const grantPublicViewer = jest.fn();
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
catalog,
|
||||
store,
|
||||
sources: {
|
||||
native: createNativePromptAdapter(store),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
},
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer,
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
@ -553,13 +555,15 @@ describe('createPromptService', () => {
|
|||
});
|
||||
|
||||
it('deletes the newly created group and rethrows when the grant fails', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
const store = createPromptStore(db);
|
||||
const grantPublicViewer = jest.fn().mockRejectedValue(new Error('grant failed'));
|
||||
const logError = jest.fn();
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
catalog,
|
||||
store,
|
||||
sources: {
|
||||
native: createNativePromptAdapter(store),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
},
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer,
|
||||
logger: { warn: jest.fn(), error: logError },
|
||||
|
|
@ -574,7 +578,7 @@ describe('createPromptService', () => {
|
|||
}),
|
||||
).rejects.toThrow('grant failed');
|
||||
|
||||
await expect(catalog.findSourceGroup(identity)).resolves.toBeNull();
|
||||
await expect(store.findSourceGroup(identity)).resolves.toBeNull();
|
||||
expect(logError).toHaveBeenCalledTimes(1);
|
||||
expect(logError).toHaveBeenCalledWith(expect.stringContaining('[importSourceGroup]'), {
|
||||
groupId: expect.any(String),
|
||||
|
|
@ -583,14 +587,16 @@ describe('createPromptService', () => {
|
|||
});
|
||||
|
||||
it('still rethrows the grant error and logs two errors when the rollback delete also fails', async () => {
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
jest.spyOn(catalog, 'deletePromptGroup').mockRejectedValue(new Error('delete failed'));
|
||||
const store = createPromptStore(db);
|
||||
jest.spyOn(store, 'deletePromptGroup').mockRejectedValue(new Error('delete failed'));
|
||||
const grantPublicViewer = jest.fn().mockRejectedValue(new Error('grant failed'));
|
||||
const logError = jest.fn();
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
catalog,
|
||||
store,
|
||||
sources: {
|
||||
native: createNativePromptAdapter(store),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
},
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer,
|
||||
logger: { warn: jest.fn(), error: logError },
|
||||
|
|
@ -927,16 +933,23 @@ describe('createPromptService', () => {
|
|||
savePrompt: _save,
|
||||
makePromptProduction: _promote,
|
||||
deletePrompt: _delete,
|
||||
...native
|
||||
} = createNativePromptAdapter(db);
|
||||
...store
|
||||
} = createPromptStore(db);
|
||||
const readOnly = createPromptServiceFromAdapters({
|
||||
native,
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
catalog: createPromptCatalogStore(db),
|
||||
store,
|
||||
sources: {
|
||||
native: createNativePromptAdapter(store),
|
||||
langfuse: { resolvePrompt: jest.fn() },
|
||||
},
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
});
|
||||
const readPrompt = jest.spyOn(db, 'getPrompt');
|
||||
const readGroup = jest.spyOn(db, 'getPromptGroup');
|
||||
const savePrompt = jest.spyOn(db, 'savePrompt');
|
||||
const makePromptProduction = jest.spyOn(db, 'makePromptProduction');
|
||||
const deletePrompt = jest.spyOn(db, 'deletePrompt');
|
||||
|
||||
await expect(
|
||||
readOnly.savePrompt({ groupId: group._id, prompt: undefined, author }),
|
||||
|
|
@ -949,6 +962,12 @@ describe('createPromptService', () => {
|
|||
readOnly.deletePrompt({ groupId: group._id, promptId: production._id }),
|
||||
).resolves.toEqual({ ok: false, error: { type: 'unsupported', operation: 'deletePrompt' } });
|
||||
await expect(readOnly.getPrompts({ groupId: group._id })).resolves.toHaveLength(1);
|
||||
|
||||
expect(readPrompt).not.toHaveBeenCalled();
|
||||
expect(readGroup).not.toHaveBeenCalled();
|
||||
expect(savePrompt).not.toHaveBeenCalled();
|
||||
expect(makePromptProduction).not.toHaveBeenCalled();
|
||||
expect(deletePrompt).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -1009,10 +1028,10 @@ describe('createPromptService', () => {
|
|||
const langfuseGroup = makeLangfuseGroup();
|
||||
const resolved = makeLangfuseResolved({ groupId: langfuseGroup._id });
|
||||
const resolvePrompt = jest.fn().mockResolvedValue({ ok: true, value: resolved });
|
||||
const store = createPromptStore(db);
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt },
|
||||
catalog: createPromptCatalogStore(db),
|
||||
store,
|
||||
sources: { native: createNativePromptAdapter(store), langfuse: { resolvePrompt } },
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
@ -1035,10 +1054,10 @@ describe('createPromptService', () => {
|
|||
it('rejects a disallowed source before calling the adapter', async () => {
|
||||
const langfuseGroup = makeLangfuseGroup();
|
||||
const resolvePrompt = jest.fn();
|
||||
const store = createPromptStore(db);
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt },
|
||||
catalog: createPromptCatalogStore(db),
|
||||
store,
|
||||
sources: { native: createNativePromptAdapter(store), langfuse: { resolvePrompt } },
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
@ -1065,10 +1084,10 @@ describe('createPromptService', () => {
|
|||
prompt: 'Contains PRIVATE-VALUE',
|
||||
});
|
||||
const resolvePrompt = jest.fn().mockResolvedValue({ ok: true, value: resolved });
|
||||
const store = createPromptStore(db);
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt },
|
||||
catalog: createPromptCatalogStore(db),
|
||||
store,
|
||||
sources: { native: createNativePromptAdapter(store), langfuse: { resolvePrompt } },
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
@ -1227,15 +1246,13 @@ describe('createPromptService', () => {
|
|||
|
||||
it('rejects a disallowed source for an exact selection with nothing preloaded', async () => {
|
||||
const langfuseGroup = makeLangfuseGroup();
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
jest.spyOn(catalog, 'getPromptGroup').mockResolvedValue(langfuseGroup);
|
||||
const native = createNativePromptAdapter(db);
|
||||
const revisionRead = jest.spyOn(native, 'getPrompt');
|
||||
const store = createPromptStore(db);
|
||||
jest.spyOn(store, 'getPromptGroup').mockResolvedValue(langfuseGroup);
|
||||
const revisionRead = jest.spyOn(store, 'getPrompt');
|
||||
const resolvePrompt = jest.fn();
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native,
|
||||
langfuse: { resolvePrompt },
|
||||
catalog,
|
||||
store,
|
||||
sources: { native: createNativePromptAdapter(store), langfuse: { resolvePrompt } },
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
@ -1257,16 +1274,15 @@ describe('createPromptService', () => {
|
|||
|
||||
it('returns unsupported_selection for an exact selection dispatched to the Langfuse adapter', async () => {
|
||||
const langfuseGroup = makeLangfuseGroup();
|
||||
const catalog = createPromptCatalogStore(db);
|
||||
jest.spyOn(catalog, 'getPromptGroup').mockResolvedValue(langfuseGroup);
|
||||
const store = createPromptStore(db);
|
||||
jest.spyOn(store, 'getPromptGroup').mockResolvedValue(langfuseGroup);
|
||||
const resolvePrompt = jest.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
error: { type: 'unsupported_selection', source: 'langfuse' },
|
||||
});
|
||||
const routed = createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: { resolvePrompt },
|
||||
catalog,
|
||||
store,
|
||||
sources: { native: createNativePromptAdapter(store), langfuse: { resolvePrompt } },
|
||||
grantCreatorOwnership: jest.fn(),
|
||||
grantPublicViewer: jest.fn(),
|
||||
logger: { warn: jest.fn(), error: jest.fn() },
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import type {
|
|||
PromptGroupRecord,
|
||||
ResolvePromptInput,
|
||||
PromptServiceError,
|
||||
PromptSourceAdapter,
|
||||
PromptServiceResult,
|
||||
ImportSourceGroupInput,
|
||||
PromptCreationResult,
|
||||
|
|
@ -32,15 +31,12 @@ import {
|
|||
buildPromptGroupFilter,
|
||||
filterAccessibleIdsBySharedLogic,
|
||||
} from './format';
|
||||
import {
|
||||
createPromptCatalogStore,
|
||||
createNativePromptAdapter,
|
||||
selectionUnavailableReason,
|
||||
} from './native';
|
||||
import { safeValidatePromptGroupUpdate, safeValidatePromptPayload } from './schemas';
|
||||
import { createNativePromptAdapter, selectionUnavailableReason } from './native';
|
||||
import { createLangfuseSourceResolver } from '../langfuse/promptSync';
|
||||
import { createLangfusePromptAdapter } from './langfuse';
|
||||
import { getSafeErrorMetadata } from '~/utils';
|
||||
import { createPromptStore } from './store';
|
||||
import { withPromptStage } from './errors';
|
||||
import { CONSTANTS } from '~/mcp/enum';
|
||||
|
||||
|
|
@ -142,8 +138,8 @@ function readOnly(source: PromptGroupRecord['source']): PromptServiceResult<neve
|
|||
return { ok: false, error: { type: 'read_only', source } };
|
||||
}
|
||||
|
||||
/** A group not sourced from the native adapter is managed externally and rejects
|
||||
* every mutation. A missing group is left to the write or read it guards; only a
|
||||
/** A group whose source is not native is managed externally and rejects every
|
||||
* mutation. A missing group is left to the write or read it guards; only a
|
||||
* group that is known and external triggers the rejection. */
|
||||
function isExternal(group: PromptGroupRecord): boolean {
|
||||
return group.source !== 'native';
|
||||
|
|
@ -157,24 +153,19 @@ function inspect(
|
|||
return finding == null ? null : { ok: false, error: { type: 'blocked_content', finding } };
|
||||
}
|
||||
|
||||
/** Builds the prompt service from a native adapter, a Langfuse adapter, a catalog
|
||||
* store and an ownership grant. */
|
||||
/** Builds the prompt service from a store, source adapters and permission grants. */
|
||||
export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters): PromptService {
|
||||
const { native, langfuse, catalog, grantCreatorOwnership, grantPublicViewer, logger } = adapters;
|
||||
const adaptersBySource: Record<PromptGroupRecord['source'], PromptSourceAdapter> = {
|
||||
native,
|
||||
langfuse,
|
||||
};
|
||||
const { store, sources, grantCreatorOwnership, grantPublicViewer, logger } = adapters;
|
||||
|
||||
const readPrompt = (promptId: string, loaded?: PromptRecord | null) =>
|
||||
loaded?._id === promptId
|
||||
? Promise.resolve(loaded)
|
||||
: withPromptStage('read', () => native.getPrompt(promptId));
|
||||
: withPromptStage('read', () => store.getPrompt(promptId));
|
||||
|
||||
const readGroup = (groupId: string, loaded?: PromptGroupRecord | null) =>
|
||||
loaded?._id === groupId
|
||||
? Promise.resolve(loaded)
|
||||
: withPromptStage('read', () => catalog.getPromptGroup(groupId));
|
||||
: withPromptStage('read', () => store.getPromptGroup(groupId));
|
||||
|
||||
return {
|
||||
async resolvePrompt({
|
||||
|
|
@ -195,7 +186,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
if (allowedSources != null && !allowedSources.includes(group.source)) {
|
||||
return { ok: false, error: { type: 'unsupported_source', source: group.source } };
|
||||
}
|
||||
const adapter = adaptersBySource[group.source];
|
||||
const adapter = sources[group.source];
|
||||
const resolved = await adapter.resolvePrompt({ group, selection, loadedRevision });
|
||||
if (!resolved.ok) {
|
||||
return resolved;
|
||||
|
|
@ -212,7 +203,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
publicPromptGroupIds: input.publiclyAccessibleIds,
|
||||
ownedPromptGroupIds: input.ownedPromptGroupIds,
|
||||
});
|
||||
const result = await catalog.getListPromptGroupsByAccess({
|
||||
const result = await store.getListPromptGroupsByAccess({
|
||||
accessibleIds,
|
||||
name,
|
||||
category,
|
||||
|
|
@ -232,7 +223,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
async getPrompts({ groupId, filters }) {
|
||||
// Keyed by groupId against the native `Prompt` collection: a Langfuse group has no
|
||||
// rows there, so this already resolves to `[]` for one without a source check.
|
||||
const prompts = await withPromptStage('read', () => native.getPrompts(groupId));
|
||||
const prompts = await withPromptStage('read', () => store.getPrompts(groupId));
|
||||
return projectStoredPrompts(prompts, filters);
|
||||
},
|
||||
|
||||
|
|
@ -263,7 +254,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
if (rejection != null) {
|
||||
return rejection;
|
||||
}
|
||||
const value = await native.createPromptGroup({ ...input, group, prompt: validation.data });
|
||||
const value = await store.createPromptGroup({ ...input, group, prompt: validation.data });
|
||||
const groupId = value.prompt?.groupId;
|
||||
if (value.prompt?._id && groupId) {
|
||||
try {
|
||||
|
|
@ -279,7 +270,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
},
|
||||
|
||||
async savePrompt({ groupId, prompt, author, filters, loadedGroup }) {
|
||||
if (!native.savePrompt) {
|
||||
if (!store.savePrompt) {
|
||||
return unsupported('savePrompt');
|
||||
}
|
||||
if (!prompt) {
|
||||
|
|
@ -297,7 +288,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
if (rejection != null) {
|
||||
return rejection;
|
||||
}
|
||||
const save = native.savePrompt;
|
||||
const save = store.savePrompt;
|
||||
const value = await withPromptStage('write', () =>
|
||||
save({ groupId, prompt: validation.data, author }),
|
||||
);
|
||||
|
|
@ -325,7 +316,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
return inspect({ prompt: revision }, filters) ?? { ok: true, value: revision };
|
||||
},
|
||||
|
||||
incrementPromptGroupUsage: (groupId) => catalog.incrementPromptGroupUsage(groupId),
|
||||
incrementPromptGroupUsage: (groupId) => store.incrementPromptGroupUsage(groupId),
|
||||
|
||||
async updatePromptGroup({ groupId, updates, filters, loadedGroup }) {
|
||||
const group = await readGroup(groupId, loadedGroup);
|
||||
|
|
@ -341,13 +332,13 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
return rejection;
|
||||
}
|
||||
const value = await withPromptStage('write', () =>
|
||||
catalog.updatePromptGroup(groupId, validation.data),
|
||||
store.updatePromptGroup(groupId, validation.data),
|
||||
);
|
||||
return { ok: true, value };
|
||||
},
|
||||
|
||||
async makePromptProduction({ promptId, loadedRevision, filters }) {
|
||||
if (!native.makePromptProduction) {
|
||||
if (!store.makePromptProduction) {
|
||||
return unsupported('makePromptProduction');
|
||||
}
|
||||
const revision = await readPrompt(promptId, loadedRevision);
|
||||
|
|
@ -361,13 +352,13 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
if (rejection != null) {
|
||||
return rejection;
|
||||
}
|
||||
const promote = native.makePromptProduction;
|
||||
const promote = store.makePromptProduction;
|
||||
const value = await withPromptStage('write', () => promote(promptId));
|
||||
return { ok: true, value, groupId: revision?.groupId };
|
||||
},
|
||||
|
||||
async deletePrompt(input) {
|
||||
if (!native.deletePrompt) {
|
||||
if (!store.deletePrompt) {
|
||||
return unsupported('deletePrompt');
|
||||
}
|
||||
const revision = await readPrompt(input.promptId, input.loadedRevision);
|
||||
|
|
@ -377,7 +368,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
return readOnly(group.source);
|
||||
}
|
||||
}
|
||||
return { ok: true, value: await native.deletePrompt(input) };
|
||||
return { ok: true, value: await store.deletePrompt(input) };
|
||||
},
|
||||
|
||||
async deletePromptGroup({ groupId, loadedGroup, collectPromptIds }) {
|
||||
|
|
@ -391,7 +382,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
let promptIds: readonly string[] | undefined;
|
||||
if (collectPromptIds) {
|
||||
try {
|
||||
promptIds = (await withPromptStage('read', () => native.getPrompts(groupId))).map(
|
||||
promptIds = (await withPromptStage('read', () => store.getPrompts(groupId))).map(
|
||||
(revision) => revision._id,
|
||||
);
|
||||
} catch (error) {
|
||||
|
|
@ -402,19 +393,19 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
promptIds = [];
|
||||
}
|
||||
}
|
||||
const value = await catalog.deletePromptGroup(groupId);
|
||||
const value = await store.deletePromptGroup(groupId);
|
||||
return { ok: true, value: promptIds == null ? value : { ...value, promptIds } };
|
||||
},
|
||||
|
||||
async importSourceGroup({ identity, name, author, authorName }) {
|
||||
// Insertion and the initial PUBLIC grant are separate writes. If a run stops between
|
||||
// them, a later import leaves the existing group unchanged and does not retry the grant.
|
||||
const existing = await catalog.findSourceGroup(identity);
|
||||
const existing = await store.findSourceGroup(identity);
|
||||
if (existing) {
|
||||
return 'unchanged';
|
||||
}
|
||||
|
||||
const created = await catalog.createSourceGroup({ identity, name, author, authorName });
|
||||
const created = await store.createSourceGroup({ identity, name, author, authorName });
|
||||
if (!created.ok) {
|
||||
if (created.error.code === 'duplicate') {
|
||||
return 'unchanged';
|
||||
|
|
@ -436,7 +427,7 @@ export function createPromptServiceFromAdapters(adapters: PromptServiceAdapters)
|
|||
{ groupId, ...getSafeErrorMetadata(error) },
|
||||
);
|
||||
try {
|
||||
await catalog.deletePromptGroup(groupId);
|
||||
await store.deletePromptGroup(groupId);
|
||||
} catch (deleteError) {
|
||||
logger.error('[importSourceGroup] Could not roll back an ungranted source group', {
|
||||
groupId,
|
||||
|
|
@ -482,16 +473,19 @@ export function createPromptService({
|
|||
grantPermission,
|
||||
getAppConfig,
|
||||
}: PromptServiceDependencies): PromptService {
|
||||
const store = createPromptStore(db);
|
||||
return createPromptServiceFromAdapters({
|
||||
native: createNativePromptAdapter(db),
|
||||
langfuse: createLangfusePromptAdapter({
|
||||
resolveSource: createLangfuseSourceResolver({
|
||||
findBaseConfig: () =>
|
||||
db.findConfigByPrincipal(PrincipalType.ROLE, BASE_CONFIG_PRINCIPAL_ID),
|
||||
getAppConfig,
|
||||
store,
|
||||
sources: {
|
||||
native: createNativePromptAdapter(store),
|
||||
langfuse: createLangfusePromptAdapter({
|
||||
resolveSource: createLangfuseSourceResolver({
|
||||
findBaseConfig: () =>
|
||||
db.findConfigByPrincipal(PrincipalType.ROLE, BASE_CONFIG_PRINCIPAL_ID),
|
||||
getAppConfig,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
catalog: createPromptCatalogStore(db),
|
||||
},
|
||||
grantCreatorOwnership: async ({ userId, groupId }) => {
|
||||
await grantPermission({
|
||||
principalType: PrincipalType.USER,
|
||||
|
|
|
|||
204
packages/api/src/prompts/store.spec.ts
Normal file
204
packages/api/src/prompts/store.spec.ts
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
import mongoose, { Types } from 'mongoose';
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server';
|
||||
import { createModels, createMethods } from '@librechat/data-schemas';
|
||||
import type { PromptDatabase, PromptGroupRecord, PromptRecord } from './types';
|
||||
import { createPromptStore } from './store';
|
||||
|
||||
let mongo: MongoMemoryServer;
|
||||
let db: PromptDatabase;
|
||||
let group: PromptGroupRecord;
|
||||
let production: PromptRecord;
|
||||
let draft: PromptRecord;
|
||||
const author = new Types.ObjectId().toString();
|
||||
|
||||
beforeAll(async () => {
|
||||
mongo = await MongoMemoryServer.create({ instance: { args: ['--nounixsocket'] } });
|
||||
await mongoose.connect(mongo.getUri());
|
||||
createModels(mongoose);
|
||||
db = createMethods(mongoose);
|
||||
await mongoose.models.PromptGroup.syncIndexes();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const store = createPromptStore(db);
|
||||
const created = await store.createPromptGroup({
|
||||
prompt: { prompt: 'Production prompt', type: 'text' },
|
||||
group: { name: 'Native group' },
|
||||
author,
|
||||
authorName: 'Author',
|
||||
});
|
||||
production = created.prompt as PromptRecord;
|
||||
({ prompt: draft } = await store.savePrompt({
|
||||
groupId: production.groupId,
|
||||
prompt: { prompt: 'Draft prompt', type: 'chat' },
|
||||
author,
|
||||
}));
|
||||
group = (await store.getPromptGroup(production.groupId)) as PromptGroupRecord;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
jest.restoreAllMocks();
|
||||
await mongoose.models.Prompt.deleteMany({});
|
||||
await mongoose.models.PromptGroup.deleteMany({});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await mongoose.disconnect();
|
||||
await mongo?.stop();
|
||||
});
|
||||
|
||||
describe('createPromptStore', () => {
|
||||
it('returns records with string IDs', () => {
|
||||
expect(group).toEqual(
|
||||
expect.objectContaining({
|
||||
_id: production.groupId,
|
||||
author,
|
||||
source: 'native',
|
||||
productionId: production._id,
|
||||
productionPrompt: expect.objectContaining({
|
||||
_id: production._id,
|
||||
groupId: production.groupId,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(typeof draft._id).toBe('string');
|
||||
expect(draft).toEqual(expect.objectContaining({ groupId: group._id, author, type: 'chat' }));
|
||||
});
|
||||
|
||||
it('returns null for an absent revision', async () => {
|
||||
const store = createPromptStore(db);
|
||||
const missing = new Types.ObjectId().toString();
|
||||
|
||||
await expect(store.getPrompt(missing)).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('returns the group revisions newest first', async () => {
|
||||
const store = createPromptStore(db);
|
||||
await mongoose.models.Prompt.collection.updateOne(
|
||||
{ _id: new Types.ObjectId(draft._id) },
|
||||
{ $set: { createdAt: new Date(Date.now() + 1000) } },
|
||||
);
|
||||
|
||||
const revisions = await store.getPrompts(group._id);
|
||||
|
||||
expect(revisions.map((revision) => revision._id)).toEqual([draft._id, production._id]);
|
||||
});
|
||||
|
||||
it('promotes a revision and throws for a missing revision', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
await store.makePromptProduction(draft._id);
|
||||
|
||||
await expect(store.getPromptGroup(group._id)).resolves.toMatchObject({
|
||||
productionId: draft._id,
|
||||
});
|
||||
await expect(store.makePromptProduction(new Types.ObjectId().toString())).rejects.toThrow(
|
||||
'Prompt not found',
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a string group ID when deleting the last revision deletes the group', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
await expect(store.deletePrompt({ groupId: group._id, promptId: draft._id })).resolves.toEqual({
|
||||
prompt: 'Prompt deleted successfully',
|
||||
});
|
||||
await expect(
|
||||
store.deletePrompt({ groupId: group._id, promptId: production._id }),
|
||||
).resolves.toEqual({
|
||||
prompt: 'Prompt deleted successfully',
|
||||
promptGroup: { message: 'Prompt group deleted successfully', id: group._id },
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a group with string IDs through getPromptGroup', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
await expect(store.getPromptGroup(production.groupId)).resolves.toEqual(group);
|
||||
});
|
||||
|
||||
it('returns null for a missing group', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
await expect(store.getPromptGroup(new Types.ObjectId().toString())).resolves.toBeNull();
|
||||
});
|
||||
|
||||
describe('findSourceGroup and createSourceGroup', () => {
|
||||
const identity = {
|
||||
sourceDestination: 'us',
|
||||
sourceProjectId: 'project-1',
|
||||
sourcePromptName: 'synced-prompt',
|
||||
};
|
||||
|
||||
it('returns null for an identity with no stored group', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
await expect(store.findSourceGroup(identity)).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('creates a source group and then finds it by identity', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
const created = await store.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
expect(created).toEqual({ ok: true, value: { _id: expect.any(String) } });
|
||||
await expect(store.findSourceGroup(identity)).resolves.toEqual({
|
||||
_id: created.ok ? created.value._id : undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a duplicate error for a second insert with the same identity', async () => {
|
||||
const store = createPromptStore(db);
|
||||
await store.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
const result = await store.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt again',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
expect(result).toEqual({ ok: false, error: { code: 'duplicate' } });
|
||||
});
|
||||
|
||||
it('returns an invalid error with a cause for an empty name', async () => {
|
||||
const store = createPromptStore(db);
|
||||
|
||||
const result = await store.createSourceGroup({
|
||||
identity,
|
||||
name: '',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
ok: false,
|
||||
error: { code: 'invalid', cause: expect.any(Error) },
|
||||
});
|
||||
});
|
||||
|
||||
it('rethrows a non-Mongo, non-validation error', async () => {
|
||||
const store = createPromptStore(db);
|
||||
jest.spyOn(db, 'createLangfusePromptGroup').mockRejectedValueOnce(new Error('unavailable'));
|
||||
|
||||
await expect(
|
||||
store.createSourceGroup({
|
||||
identity,
|
||||
name: 'Synced prompt',
|
||||
author,
|
||||
authorName: 'Langfuse Sync',
|
||||
}),
|
||||
).rejects.toThrow('unavailable');
|
||||
});
|
||||
});
|
||||
});
|
||||
94
packages/api/src/prompts/store.ts
Normal file
94
packages/api/src/prompts/store.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import type { StoredId, PromptStore, PromptDatabase } from './types';
|
||||
import { toPromptGroupRecord, toPromptRecord } from './records';
|
||||
|
||||
function toIdString(id: StoredId): string {
|
||||
return typeof id === 'string' ? id : id.toString();
|
||||
}
|
||||
|
||||
/** The AI-2272 partial unique index is defense in depth: this maps the resulting
|
||||
* duplicate-key race to `createSourceGroup`'s `duplicate` outcome. */
|
||||
function isDuplicateKeyError(error: unknown): boolean {
|
||||
return (error as { code?: number } | undefined)?.code === 11000;
|
||||
}
|
||||
|
||||
/** A document validation failure, mapped to `createSourceGroup`'s `invalid` outcome. */
|
||||
function isValidationError(error: unknown): boolean {
|
||||
return error instanceof Error && error.name === 'ValidationError';
|
||||
}
|
||||
|
||||
/** Local storage for prompt groups and native revisions over the LibreChat prompt
|
||||
* collections. */
|
||||
export function createPromptStore(db: PromptDatabase): Required<PromptStore> {
|
||||
return {
|
||||
getPromptGroup: async (groupId) => {
|
||||
const record = await db.getPromptGroup({ _id: groupId });
|
||||
return record == null ? null : toPromptGroupRecord(record);
|
||||
},
|
||||
getListPromptGroupsByAccess: async ({ accessibleIds, name, category, limit, after }) => {
|
||||
const result = await db.getListPromptGroupsByAccess({
|
||||
accessibleIds: [...accessibleIds],
|
||||
name,
|
||||
category,
|
||||
limit,
|
||||
after,
|
||||
});
|
||||
return {
|
||||
data: result.data.map(toPromptGroupRecord),
|
||||
has_more: result.has_more,
|
||||
after: result.after,
|
||||
};
|
||||
},
|
||||
updatePromptGroup: async (groupId, updates) =>
|
||||
toPromptGroupRecord(await db.updatePromptGroup({ _id: groupId }, updates)),
|
||||
incrementPromptGroupUsage: (groupId) => db.incrementPromptGroupUsage(groupId),
|
||||
deletePromptGroup: (groupId) => db.deletePromptGroup({ _id: groupId }),
|
||||
findSourceGroup: (identity) => db.findLangfusePromptGroup(identity),
|
||||
createSourceGroup: async (input) => {
|
||||
const { identity, name, author, authorName } = input;
|
||||
try {
|
||||
const value = await db.createLangfusePromptGroup({
|
||||
...identity,
|
||||
name,
|
||||
author,
|
||||
authorName,
|
||||
});
|
||||
return { ok: true, value };
|
||||
} catch (error) {
|
||||
if (isDuplicateKeyError(error)) {
|
||||
return { ok: false, error: { code: 'duplicate' } };
|
||||
}
|
||||
if (isValidationError(error)) {
|
||||
return { ok: false, error: { code: 'invalid', cause: error } };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
getPrompt: async (promptId) => {
|
||||
const record = await db.getPrompt({ _id: promptId });
|
||||
return record == null ? null : toPromptRecord(record);
|
||||
},
|
||||
getPrompts: async (groupId) => (await db.getPrompts({ groupId })).map(toPromptRecord),
|
||||
createPromptGroup: async ({ prompt, group = {}, author, authorName }) => {
|
||||
const result = await db.createPromptGroup({ prompt, group, author, authorName });
|
||||
return {
|
||||
prompt: result.prompt == null ? null : toPromptRecord(result.prompt),
|
||||
group: toPromptGroupRecord(result.group),
|
||||
};
|
||||
},
|
||||
savePrompt: async ({ groupId, prompt, author }) => {
|
||||
const result = await db.savePrompt({ prompt: { ...prompt, groupId }, author });
|
||||
return { prompt: toPromptRecord(result.prompt) };
|
||||
},
|
||||
makePromptProduction: (promptId) => db.makePromptProduction(promptId),
|
||||
deletePrompt: async ({ groupId, promptId }) => {
|
||||
const result = await db.deletePrompt({ groupId, promptId });
|
||||
if (result.promptGroup == null) {
|
||||
return { prompt: result.prompt };
|
||||
}
|
||||
return {
|
||||
prompt: result.prompt,
|
||||
promptGroup: { message: result.promptGroup.message, id: toIdString(result.promptGroup.id) },
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -218,31 +218,15 @@ export interface PromptSourceAdapter {
|
|||
}
|
||||
|
||||
/**
|
||||
* The native adapter additionally performs every operation that is keyed by revision ID
|
||||
* rather than by group. Mutations are optional: a source that cannot perform one leaves
|
||||
* it out, and the service returns an `unsupported` result. The service also guards
|
||||
* `savePrompt`, `makePromptProduction` and `deletePrompt` against an external group,
|
||||
* returning `read_only` before any of these are called.
|
||||
* All local group and native-revision operations: the group-keyed storage methods and the
|
||||
* revision-keyed native methods. Three mutations, `savePrompt`, `makePromptProduction` and
|
||||
* `deletePrompt`, are optional: a store that cannot perform one leaves it out, and the
|
||||
* service returns an `unsupported` result. The service returns `read_only` for an
|
||||
* external group before calling `savePrompt`, `makePromptProduction`, `deletePrompt`,
|
||||
* `updatePromptGroup` or `deletePromptGroup` on a caller's request; the `importSourceGroup`
|
||||
* rollback deletes the external group it created without that check.
|
||||
*/
|
||||
export interface NativePromptAdapter extends PromptSourceAdapter {
|
||||
getPrompt(promptId: string): Promise<PromptRecord | null>;
|
||||
getPrompts(groupId: string): Promise<readonly PromptRecord[]>;
|
||||
createPromptGroup(input: CreatePromptGroupInput): Promise<PromptCreationResult>;
|
||||
savePrompt?(input: AddPromptRevisionInput): Promise<{ readonly prompt: PromptRecord }>;
|
||||
/** Throws when the revision does not exist. */
|
||||
makePromptProduction?(promptId: string): Promise<TMakePromptProductionResponse>;
|
||||
deletePrompt?(input: {
|
||||
readonly groupId: string;
|
||||
readonly promptId: string;
|
||||
}): Promise<TDeletePromptResponse>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local catalog operations that do not depend on the content source. The service guards
|
||||
* `updatePromptGroup` and `deletePromptGroup` against an external group, returning
|
||||
* `read_only` before either reaches here.
|
||||
*/
|
||||
export interface PromptCatalogStore {
|
||||
export interface PromptStore {
|
||||
getPromptGroup(groupId: string): Promise<PromptGroupRecord | null>;
|
||||
getListPromptGroupsByAccess(
|
||||
input: Pick<PromptListInput, 'accessibleIds' | 'name' | 'category' | 'limit' | 'after'>,
|
||||
|
|
@ -257,9 +241,19 @@ export interface PromptCatalogStore {
|
|||
* Returns a typed failure for a duplicate-key race or a document validation
|
||||
* failure; any other failure throws. */
|
||||
createSourceGroup(input: ImportSourceGroupInput): Promise<CreateSourceGroupResult>;
|
||||
getPrompt(promptId: string): Promise<PromptRecord | null>;
|
||||
getPrompts(groupId: string): Promise<readonly PromptRecord[]>;
|
||||
createPromptGroup(input: CreatePromptGroupInput): Promise<PromptCreationResult>;
|
||||
savePrompt?(input: AddPromptRevisionInput): Promise<{ readonly prompt: PromptRecord }>;
|
||||
/** Throws when the revision does not exist. */
|
||||
makePromptProduction?(promptId: string): Promise<TMakePromptProductionResponse>;
|
||||
deletePrompt?(input: {
|
||||
readonly groupId: string;
|
||||
readonly promptId: string;
|
||||
}): Promise<TDeletePromptResponse>;
|
||||
}
|
||||
|
||||
/** The data-schemas prompt methods the native adapter and catalog store use. */
|
||||
/** The data-schemas methods the prompt store and `createPromptService` use. */
|
||||
export interface PromptDatabase {
|
||||
getPromptGroup(filter: { _id: string }): Promise<object | null>;
|
||||
getPrompt(filter: { _id: string }): Promise<object | null>;
|
||||
|
|
@ -308,15 +302,10 @@ export interface PromptDatabase {
|
|||
}
|
||||
|
||||
export interface PromptServiceAdapters {
|
||||
/**
|
||||
* The content source for a group whose `source` is `native`, and for every operation
|
||||
* keyed by revision ID rather than by group (`createPromptGroup`, `getPrompt`,
|
||||
* `savePrompt`, `makePromptProduction`, `deletePrompt`), which stay native-only.
|
||||
*/
|
||||
readonly native: NativePromptAdapter;
|
||||
/** The content source for a group whose `source` is `langfuse`. */
|
||||
readonly langfuse: PromptSourceAdapter;
|
||||
readonly catalog: PromptCatalogStore;
|
||||
/** All local group and native-revision storage. */
|
||||
readonly store: PromptStore;
|
||||
/** The content source per `PromptGroupSource`, resolving a selection only. */
|
||||
readonly sources: Record<PromptGroupSource, PromptSourceAdapter>;
|
||||
readonly grantCreatorOwnership: (input: {
|
||||
readonly userId: string;
|
||||
readonly groupId: string;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue