Skip to content

Commit

Permalink
fix: references missing (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Q1w1N authored Jan 8, 2025
1 parent 8a83294 commit cb21f9a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-cheetahs-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@callstack/byorg-core': patch
---

Fixed missing references
44 changes: 44 additions & 0 deletions packages/core/src/__tests__/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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);
Expand Down

0 comments on commit cb21f9a

Please sign in to comment.