Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: references missing #43

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: references missing
Q1w1N committed Jan 7, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 95206e49c93d1bb08aade70a32677a9b744becd6
44 changes: 44 additions & 0 deletions packages/core/src/__tests__/application.test.ts
Original file line number Diff line number Diff line change
@@ -137,3 +137,47 @@ test('uses chat model from context', async () => {
expect(altModel.calls.length).toBe(1);
expect(baseModel.calls.length).toBe(0);
});

test('adds unique and returns references', async () => {
const baseModel = createMockChatModel({ delay: 0, seed: 3 });
const refFunc = vitest.fn();

const addReferenceMiddlewareMock = vitest.fn(
async (context: RequestContext, next: () => Promise<MessageResponse>) => {
context.references.addReferences([{ title: 'Test', url: 'test.com' }]);
return await next();
},
);

const getReferenceMiddlewareMock = vitest.fn(
async (context: RequestContext, next: () => Promise<MessageResponse>) => {
const response = await next();
refFunc(context.references.getReferences());
return response;
},
);

const app = createApp({
chatModel: baseModel,
plugins: [
{
name: 'add',
middleware: addReferenceMiddlewareMock,
},
{
name: 'get',
middleware: getReferenceMiddlewareMock,
},
],
});

expect(refFunc).not.toBeCalled();
await app.processMessages(messages);
expect(addReferenceMiddlewareMock).toBeCalledTimes(1);
expect(getReferenceMiddlewareMock).toBeCalledTimes(1);
expect(refFunc).toBeCalledWith([{ title: 'Test', url: 'test.com' }]);
await app.processMessages(messages);
expect(addReferenceMiddlewareMock).toBeCalledTimes(2);
expect(getReferenceMiddlewareMock).toBeCalledTimes(2);
expect(refFunc).toBeCalledWith([{ title: 'Test', url: 'test.com' }]);
});
4 changes: 2 additions & 2 deletions packages/core/src/references.ts
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ export const getReferenceStorage = (): ReferenceStorage => {
const getKey = (ref: DocumentReference) => ref.url;

return {
addReferences(references: DocumentReference[]): void {
for (const ref of references) {
addReferences(referencesToAdd: DocumentReference[]): void {
for (const ref of referencesToAdd) {
const refKey = getKey(ref);
if (!referencesKeys.has(refKey)) {
referencesKeys.add(refKey);