Skip to content

Commit 0e7a491

Browse files
committed
Do not tell the AI the user is unable to edit cards when there is an attached file and no cards
1 parent f737dd9 commit 0e7a491

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

Diff for: packages/ai-bot/helpers.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,11 @@ ${attachedFilesToPrompt(attachedFiles)}
748748
systemMessage += '\n';
749749
}
750750

751-
if (tools.length == 0) {
751+
let cardPatchTool = tools.find((tool) => tool.function.name === 'patchCard');
752+
753+
if (attachedFiles.length == 0 && attachedCards.length > 0 && !cardPatchTool) {
752754
systemMessage +=
753-
'You are unable to edit any cards, the user has not given you access, they need to open the card on the stack and let it be auto-attached.';
755+
'You are unable to edit any cards, the user has not given you access, they need to open the card and let it be auto-attached.';
754756
}
755757

756758
let messages: OpenAIPromptMessage[] = [

Diff for: packages/ai-bot/tests/prompt-construction-test.ts

+115
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,121 @@ file-that-does-not-exist.txt: Error loading attached file: HTTP error. Status: 4
10781078
});
10791079
});
10801080

1081+
test.only('Adds the "unable to edit cards" only if there are attached cards and no tools', async () => {
1082+
const history: DiscreteMatrixEvent[] = [
1083+
{
1084+
type: 'm.room.message',
1085+
sender: '@ian:localhost',
1086+
content: {
1087+
msgtype: APP_BOXEL_MESSAGE_MSGTYPE,
1088+
format: 'org.matrix.custom.html',
1089+
body: 'set the name to dave',
1090+
formatted_body: '<p>set the name to dave</p>\n',
1091+
data: {
1092+
context: {
1093+
openCardIds: ['http://localhost:4201/drafts/Author/1'],
1094+
tools: [],
1095+
submode: 'code',
1096+
},
1097+
attachedCards: [
1098+
{
1099+
data: {
1100+
type: 'card',
1101+
id: 'http://localhost:4201/drafts/Author/1',
1102+
attributes: {
1103+
firstName: 'Alice',
1104+
lastName: 'Enwunder',
1105+
photo: null,
1106+
body: 'Alice is a software engineer at Google.',
1107+
description: null,
1108+
thumbnailURL: null,
1109+
},
1110+
meta: {
1111+
adoptsFrom: {
1112+
module: 'http://localhost:4201/drafts/author',
1113+
name: 'Author',
1114+
},
1115+
},
1116+
},
1117+
},
1118+
],
1119+
},
1120+
},
1121+
room_id: 'room1',
1122+
origin_server_ts: 1696813813166,
1123+
unsigned: {
1124+
age: 115498,
1125+
transaction_id: '1',
1126+
},
1127+
event_id: '1',
1128+
status: EventStatus.SENT,
1129+
},
1130+
];
1131+
1132+
let historyWithStringifiedData = (history: DiscreteMatrixEvent[]) => {
1133+
return history.map((event) => ({
1134+
...event,
1135+
content: {
1136+
...event.content,
1137+
data: JSON.stringify(event.content.data),
1138+
},
1139+
}));
1140+
};
1141+
1142+
const { messages } = await getPromptParts(
1143+
historyWithStringifiedData(history),
1144+
'@aibot:localhost',
1145+
);
1146+
1147+
let nonEditableCardsMessage =
1148+
'You are unable to edit any cards, the user has not given you access, they need to open the card and let it be auto-attached.';
1149+
1150+
assert.ok(
1151+
messages[0].content.includes(nonEditableCardsMessage),
1152+
'System message should include the "unable to edit cards" message when there are attached cards and no tools, and no attached files',
1153+
);
1154+
1155+
// Now add a tool
1156+
history[0].content.data.context.tools = [
1157+
getPatchTool('http://localhost:4201/drafts/Author/1', {
1158+
attributes: { firstName: { type: 'string' } },
1159+
}),
1160+
];
1161+
1162+
const { messages: messages2 } = await getPromptParts(
1163+
historyWithStringifiedData(history),
1164+
'@aibot:localhost',
1165+
);
1166+
1167+
assert.ok(
1168+
!messages2[0].content.includes(nonEditableCardsMessage),
1169+
'System message should not include the "unable to edit cards" message when there are attached cards and a tool',
1170+
);
1171+
1172+
// Now remove cards, tools, and add an attached file
1173+
history[0].content.data.context.openCardIds = [];
1174+
history[0].content.data.context.tools = [];
1175+
history[0].content.data.attachedFiles = [
1176+
{
1177+
url: 'https://example.com/file.txt',
1178+
sourceUrl: 'https://example.com/file.txt',
1179+
name: 'file.txt',
1180+
contentType: 'text/plain',
1181+
content: 'Hello, world!',
1182+
},
1183+
];
1184+
1185+
const { messages: messages3 } = await getPromptParts(
1186+
historyWithStringifiedData(history),
1187+
'@aibot:localhost',
1188+
);
1189+
1190+
assert.ok(
1191+
!messages3[0].content.includes(nonEditableCardsMessage),
1192+
'System message should not include the "unable to edit cards" message when there is an attached file',
1193+
);
1194+
});
1195+
10811196
test('Gets only the latest functions', () => {
10821197
const history: DiscreteMatrixEvent[] = [
10831198
{

0 commit comments

Comments
 (0)