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

Commands on skill cards #2132

Merged
merged 63 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
45951e5
Skills can contain links to commands
IanCal Feb 9, 2025
bc512a3
Add sending of command definitions
IanCal Feb 10, 2025
5d68bc2
Add function name to commands in skills, support in aibot
IanCal Feb 10, 2025
8eb5e5b
Add lookup and loading of the commands on the host side
IanCal Feb 10, 2025
94d149a
Remove logging
IanCal Feb 10, 2025
e1a463e
Improve comment to explain what we're actually doing
IanCal Feb 10, 2025
1e96eb4
Export default and class name
IanCal Feb 10, 2025
4423aed
Undo erroneous capitalisation
IanCal Feb 10, 2025
69d94e3
No default export, keep original casing
IanCal Feb 11, 2025
e0912ca
Fix imports
IanCal Feb 11, 2025
7ab89f8
Fix more named imports
IanCal Feb 11, 2025
d97f4c8
Inconsistent casing
IanCal Feb 11, 2025
c0e3850
Stronger typing for TS
IanCal Feb 11, 2025
3eb3739
Remove unused import
IanCal Feb 11, 2025
4039f66
Add boxel UI state command to shimmed modules and fix import
IanCal Feb 11, 2025
7efc2d6
Fix casing in import
IanCal Feb 11, 2025
e068363
Again, casing issues
IanCal Feb 11, 2025
b6d0297
Test running command from skill
IanCal Feb 11, 2025
63d3519
Test for commands on skills
IanCal Feb 11, 2025
56b2c12
Fix hash suffix
IanCal Feb 11, 2025
b0f66c3
Update hashing function
IanCal Feb 11, 2025
a69a31a
Remove logging
IanCal Feb 11, 2025
a65d474
Avoid creating room resources in multiple places
IanCal Feb 11, 2025
43e109e
Improve setting of active llm
IanCal Feb 11, 2025
7d986f9
Load skills as we load events
IanCal Feb 11, 2025
7fb4bf4
Remove unneccessary async
IanCal Feb 11, 2025
4cbc99a
Support processing more out of order, add members and user ids as qui…
IanCal Feb 12, 2025
fcfd03c
Iterate specifically
IanCal Feb 12, 2025
1f7fe65
test removing restriction on users for tests
IanCal Feb 12, 2025
8a22952
Bring back limits for room resource loading
IanCal Feb 12, 2025
a7ea41d
Remove pauses?
IanCal Feb 13, 2025
54beb97
Merge branch 'main' into return-of-the-skill-commands
IanCal Feb 13, 2025
bd907fa
Fix eslint issues
lukemelia Feb 17, 2025
a85f478
Lint fixes
lukemelia Feb 17, 2025
7df2e0e
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 17, 2025
78860a3
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 17, 2025
738afb3
Eliminate a relative reference in seed realm that shouldn't be relative
lukemelia Feb 17, 2025
393fe3e
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 17, 2025
d01b19a
Better handling of commands exported as default
lukemelia Feb 18, 2025
7544d50
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 18, 2025
22ecb4e
Merge branch 'faster-matrix-specs' into return-of-the-skill-commands
lukemelia Feb 18, 2025
5c32d82
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 18, 2025
2d3ad9d
Safer checking for toolCall
lukemelia Feb 18, 2025
447be78
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 19, 2025
b20fb12
Fix an import in generator code
lukemelia Feb 19, 2025
167b145
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 19, 2025
ba864b2
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 19, 2025
7be953a
Merge branch 'reuse-room-resource' into return-of-the-skill-commands
lukemelia Feb 19, 2025
8b99982
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 19, 2025
bad2fd1
Merge branch 'check-room-members-before-processing-events' into retur…
lukemelia Feb 21, 2025
65b9a47
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 21, 2025
5e51d57
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 24, 2025
d46fd35
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 24, 2025
0182bd5
Remove unneeded changes
lukemelia Feb 24, 2025
a88f906
AI test fixes
lukemelia Feb 24, 2025
0ffa9f1
Merge branch 'code-ref-modules' into return-of-the-skill-commands
lukemelia Feb 24, 2025
57d4693
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 24, 2025
3a8219e
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 25, 2025
2c43b0e
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 25, 2025
af764f2
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 25, 2025
519e618
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 25, 2025
4309af8
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 25, 2025
e218961
Merge branch 'main' into return-of-the-skill-commands
lukemelia Feb 26, 2025
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
44 changes: 42 additions & 2 deletions packages/ai-bot/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import type {
SkillsConfigEvent,
ActiveLLMEvent,
CommandResultEvent,
CommandDefinitionsEvent,
} from 'https://cardstack.com/base/matrix-event';
import { MatrixEvent, type IRoomEvent } from 'matrix-js-sdk';
import { ChatCompletionMessageToolCall } from 'openai/resources/chat/completions';
import * as Sentry from '@sentry/node';
import { logger } from '@cardstack/runtime-common';
import {
APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE,
APP_BOXEL_COMMAND_RESULT_EVENT_TYPE,
APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE,
} from '../runtime-common/matrix-constants';
Expand Down Expand Up @@ -86,7 +88,7 @@ export function getPromptParts(
cardFragments,
);
let skills = getEnabledSkills(eventList, cardFragments);
let tools = getTools(history, aiBotUserId);
let tools = getTools(history, skills, aiBotUserId);
let toolChoice = getToolChoice(history, aiBotUserId);
let messages = getModifyPrompt(history, aiBotUserId, tools, skills);
let model = getModel(eventList);
Expand Down Expand Up @@ -322,10 +324,45 @@ export function getRelevantCards(

export function getTools(
history: DiscreteMatrixEvent[],
enabledSkills: LooseCardResource[],
aiBotUserId: string,
): Tool[] {
// Build map directly from messages
let enabledCommandNames = new Set<string>();
let toolMap = new Map<string, Tool>();

// Get the list of all names from enabled skills
for (let skill of enabledSkills) {
if (skill.attributes?.commands) {
let { commands } = skill.attributes;
for (let command of commands) {
enabledCommandNames.add(command.functionName);
}
}
}

// Iterate over the command definitions, and add any tools that are in
// enabled skills to the tool map
let commandDefinitionEvents: CommandDefinitionsEvent[] = history.filter(
(event) =>
event.type === 'm.room.message' &&
event.content.msgtype === APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE,
) as CommandDefinitionsEvent[];

for (let event of commandDefinitionEvents) {
let { content } = event;
let { commandDefinitions } = content.data;
for (let commandDefinition of commandDefinitions) {
if (enabledCommandNames.has(commandDefinition.tool.function.name)) {
toolMap.set(
commandDefinition.tool.function.name,
commandDefinition.tool,
);
}
}
}

// Add in tools from the user's messages
for (let event of history) {
if (event.type !== 'm.room.message' || event.sender == aiBotUserId) {
continue;
Expand Down Expand Up @@ -608,7 +645,10 @@ export function isCommandResultEvent(
export function eventRequiresResponse(event: MatrixEvent) {
// If it's a message, we should respond unless it's a card fragment
if (event.getType() === 'm.room.message') {
if (event.getContent().msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE) {
if (
event.getContent().msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE ||
event.getContent().msgtype === APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE
) {
return false;
}
return true;
Expand Down
32 changes: 32 additions & 0 deletions packages/ai-bot/tests/prompt-construction-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,38 @@ test('Tools calls are connected to their results', () => {
);
});

test('Tools on enabled skills are available in prompt', () => {
const eventList: DiscreteMatrixEvent[] = JSON.parse(
readFileSync(
path.join(__dirname, 'resources/chats/enabled-skill-with-commands.json'),
),
);

const { tools } = getPromptParts(eventList, '@aibot:localhost');
assert.true(tools.length > 0, 'Should have tools available');

// Verify that the tools array contains the command from the skill
const switchSubmodeTool = tools.find(
(tool) => tool.function?.name === 'SwitchSubmodeCommand_e82a',
);
assert.ok(
switchSubmodeTool,
'Should have SwitchSubmodeCommand function available',
);
});

test('No tools are available if skill is not enabled', () => {
const eventList: DiscreteMatrixEvent[] = JSON.parse(
readFileSync(
path.join(__dirname, 'resources/chats/disabled-skill-with-commands.json'),
),
);

const { tools } = getPromptParts(eventList, '@aibot:localhost');
// we should not have any tools available
assert.true(tools.length == 0, 'Should not have tools available');
});

module('set model in prompt', () => {
test('default active LLM must be equal to `DEFAULT_LLM`', () => {
const eventList: DiscreteMatrixEvent[] = JSON.parse(
Expand Down
Loading