Skip to content

Commit

Permalink
Add some basic search tests, add search tool always available, fix ty…
Browse files Browse the repository at this point in the history
…pe/cardType mixup
  • Loading branch information
IanCal committed Jan 14, 2025
1 parent 27fffb0 commit 1e48b7e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/host/app/services/command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ function hasPatchData(payload: any): payload is PatchPayload {

function hasSearchData(payload: any): payload is SearchPayload {
return (
isResolvedCodeRef(payload.attributes?.cardType) ||
isResolvedCodeRef(payload.attributes?.type) ||
payload.attributes?.title ||
payload.attributes?.type
payload.attributes?.cardType
);
}
5 changes: 3 additions & 2 deletions packages/host/app/services/matrix-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import {
basicMappings,
generateJsonSchemaForCardType,
getSearchTool,
getPatchTool,
} from '@cardstack/runtime-common/helpers/ai';

import { getPatchTool } from '@cardstack/runtime-common/helpers/ai';
import { getMatrixUsername } from '@cardstack/runtime-common/matrix-client';

import {
Expand Down Expand Up @@ -70,6 +70,7 @@ import type {
CommandResultWithOutputContent,
} from 'https://cardstack.com/base/matrix-event';

import type { Tool } from 'https://cardstack.com/base/matrix-event';
import { SkillCard } from 'https://cardstack.com/base/skill-card';

import { getCard } from '../resources/card-resource';
Expand Down Expand Up @@ -624,7 +625,7 @@ export default class MatrixService extends Service {
context?: OperatorModeContext,
): Promise<void> {
let html = markdownToHtml(body);
let tools = [getSearchTool()];
let tools: Tool[] = [getSearchTool()];
let attachedOpenCards: CardDef[] = [];
let submode = context?.submode;
if (submode === 'interact') {
Expand Down
104 changes: 104 additions & 0 deletions packages/host/tests/integration/commands/search-command-test.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { module, test } from 'qunit';

import { baseRealm } from '@cardstack/runtime-common';
import { Loader } from '@cardstack/runtime-common/loader';

import { SearchCardsByTypeAndTitleCommand } from '@cardstack/host/commands/search-cards';
import type CommandService from '@cardstack/host/services/command-service';

import {
testRealmURL,
lookupService,
setupCardLogs,
setupIntegrationTestRealm,
setupLocalIndexing,
setupServerSentEvents,
setupOnSave,
lookupLoaderService,
} from '../../helpers';
import { setupMockMatrix } from '../../helpers/mock-matrix';
import { setupRenderingTest } from '../../helpers/setup';

module('Integration | commands | search', function (hooks) {
setupRenderingTest(hooks);

const realmName = 'Operator Mode Workspace';
let loader: Loader;

hooks.beforeEach(function () {
loader = lookupLoaderService().loader;
});

setupLocalIndexing(hooks);
setupOnSave(hooks);
setupCardLogs(
hooks,
async () => await loader.import(`${baseRealm.url}card-api`),
);
setupServerSentEvents(hooks);
setupMockMatrix(hooks, {
loggedInAs: '@testuser:staging',
activeRealms: [testRealmURL],
autostart: true,
});

hooks.beforeEach(async function () {
loader = lookupLoaderService().loader;
let cardApi: typeof import('https://cardstack.com/base/card-api');
let string: typeof import('https://cardstack.com/base/string');

cardApi = await loader.import(`${baseRealm.url}card-api`);
string = await loader.import(`${baseRealm.url}string`);

let { field, contains, CardDef } = cardApi;
let { default: StringField } = string;

class Author extends CardDef {
static displayName = 'Author';
@field firstName = contains(StringField);
@field lastName = contains(StringField);
@field title = contains(StringField, {
computeVia: function (this: Author) {
return [this.firstName, this.lastName].filter(Boolean).join(' ');
},
});
}
await setupIntegrationTestRealm({
loader,
contents: {
'author.gts': { Author },
'Author/r2.json': new Author({ firstName: 'R2-D2' }),
'Author/mark.json': new Author({
firstName: 'Mark',
lastName: 'Jackson',
}),
'.realm.json': `{ "name": "${realmName}", "iconURL": "https://boxel-images.boxel.ai/icons/Letter-o.png" }`,
},
});
});

test('search for a title', async function (assert) {
let commandService = lookupService<CommandService>('command-service');
let searchCommand = new SearchCardsByTypeAndTitleCommand(
commandService.commandContext,
);
let result = await searchCommand.execute({
title: 'Mark Jackson',
cardType: undefined,
});
assert.strictEqual(result.cardIds.length, 1);
assert.strictEqual(result.cardIds[0], 'http://test-realm/test/Author/mark');
});

test('search for a card type', async function (assert) {
let commandService = lookupService<CommandService>('command-service');
let searchCommand = new SearchCardsByTypeAndTitleCommand(
commandService.commandContext,
);
let result = await searchCommand.execute({
cardType: 'Author',
title: undefined,
});
assert.strictEqual(result.cardIds.length, 2);
});
});
2 changes: 1 addition & 1 deletion packages/runtime-common/helpers/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export function getPatchTool(
};
}

export function getSearchTool() {
export function getSearchTool(): Tool {
return {
type: 'function',
function: {
Expand Down

0 comments on commit 1e48b7e

Please sign in to comment.