-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhelpers.ts
823 lines (763 loc) · 24.4 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import {
LooseCardResource,
type LooseSingleCardDocument,
type CardResource,
} from '@cardstack/runtime-common';
import { ToolChoice } from '@cardstack/runtime-common/helpers/ai';
import { CommandRequest } from '@cardstack/runtime-common/commands';
import type {
MatrixEvent as DiscreteMatrixEvent,
CardFragmentContent,
Tool,
SkillsConfigEvent,
ActiveLLMEvent,
CardMessageEvent,
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_REQUESTS_KEY,
APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE,
APP_BOXEL_COMMAND_RESULT_EVENT_TYPE,
APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE,
} from '../runtime-common/matrix-constants';
import {
APP_BOXEL_CARDFRAGMENT_MSGTYPE,
APP_BOXEL_MESSAGE_MSGTYPE,
APP_BOXEL_ROOM_SKILLS_EVENT_TYPE,
DEFAULT_LLM,
APP_BOXEL_ACTIVE_LLM,
} from '@cardstack/runtime-common/matrix-constants';
let log = logger('ai-bot');
const MODIFY_SYSTEM_MESSAGE =
'\
The user is using an application called Boxel, where they are working on editing "Cards" which are data models representable as JSON. \
The user may be non-technical and should not need to understand the inner workings of Boxel. \
The user may be asking questions about the contents of the cards rather than help editing them. Use your world knowledge to help them. \
If the user request is unclear, you may ask clarifying questions. \
You may make multiple function calls, all calls are gated by the user so multiple options can be explored.\
If a user asks you about things in the world, use your existing knowledge to help them. Only if necessary, add a *small* caveat at the end of your message to explain that you do not have live external data. \
\
If you need access to the cards the user can see, you can ask them to attach the cards. \
If you encounter JSON structures, please enclose them within backticks to ensure they are displayed stylishly in Markdown. \
If you encounter code, please indent code using 2 spaces per tab stop and enclose the code within triple backticks and indicate the language after the opening backticks so that the code is displayed stylishly in Markdown.';
export const SKILL_INSTRUCTIONS_MESSAGE =
'\nThe user has given you the following instructions. You must obey these instructions when responding to the user:\n';
type CommandMessage = {
type: 'command';
content: any;
};
type TextMessage = {
type: 'text';
content: string;
complete: boolean;
};
export type PromptParts =
| {
shouldRespond: true;
tools: Tool[];
messages: OpenAIPromptMessage[];
model: string;
history: DiscreteMatrixEvent[];
toolChoice: ToolChoice;
}
| {
shouldRespond: false;
tools: undefined;
messages: undefined;
model: undefined;
history: undefined;
toolChoice: undefined;
};
export type Message = CommandMessage | TextMessage;
export class HistoryConstructionError extends Error {
constructor(message: string) {
super(message);
this.name = 'HistoryConstructionError';
}
}
export async function getPromptParts(
eventList: DiscreteMatrixEvent[],
aiBotUserId: string,
): Promise<PromptParts> {
let cardFragments: Map<string, CardFragmentContent> =
extractCardFragmentsFromEvents(eventList);
let history: DiscreteMatrixEvent[] = constructHistory(
eventList,
cardFragments,
);
let shouldRespond = getShouldRespond(history);
if (!shouldRespond) {
return {
shouldRespond: false,
tools: undefined,
messages: undefined,
model: undefined,
history: undefined,
toolChoice: undefined,
};
}
let skills = getEnabledSkills(eventList, cardFragments);
let tools = getTools(history, skills, aiBotUserId);
let toolChoice = getToolChoice(history, aiBotUserId);
let messages = await getModifyPrompt(history, aiBotUserId, tools, skills);
let model = getModel(eventList);
return {
shouldRespond,
tools,
messages,
model,
history,
toolChoice: toolChoice,
};
}
export function extractCardFragmentsFromEvents(
eventList: IRoomEvent[],
): Map<string, CardFragmentContent> {
const fragments = new Map<string, CardFragmentContent>(); // eventId => fragment
for (let event of eventList) {
if (event.type === 'm.room.message') {
if (event.content.msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE) {
fragments.set(event.event_id, event.content as CardFragmentContent);
}
}
}
return fragments;
}
export function constructHistory(
eventlist: IRoomEvent[],
cardFragments: Map<string, CardFragmentContent>,
) {
/**
* We send a lot of events to create messages,
* as we stream updates to the UI. This works by
* sending a new event with the full content and
* information about which event it should replace
*
* This function is to construct the chat as a user
* would see it - with only the latest event for each
* message.
*/
const latestEventsMap = new Map<string, DiscreteMatrixEvent>();
for (let rawEvent of eventlist) {
if (rawEvent.content.data) {
try {
rawEvent.content.data = JSON.parse(rawEvent.content.data);
} catch (e) {
Sentry.captureException(e, {
attachments: [
{
data: rawEvent.content.data,
filename: 'rawEventContentData.txt',
},
],
});
log.error('Error parsing JSON', e);
throw new HistoryConstructionError((e as Error).message);
}
}
let event = { ...rawEvent } as DiscreteMatrixEvent;
if (
event.type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
event.content.msgtype == APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE
) {
let { cardEventId } = event.content.data;
event.content.data.card = serializedCardFromFragments(
cardEventId,
cardFragments,
);
}
if (
event.type !== 'm.room.message' &&
event.type !== APP_BOXEL_COMMAND_RESULT_EVENT_TYPE
) {
continue;
}
let eventId = event.event_id!;
if (event.content.msgtype === APP_BOXEL_CARDFRAGMENT_MSGTYPE) {
continue;
}
if (event.content.msgtype === APP_BOXEL_MESSAGE_MSGTYPE) {
let { attachedCardsEventIds } = event.content.data;
if (attachedCardsEventIds && attachedCardsEventIds.length > 0) {
event.content.data.attachedCards = attachedCardsEventIds.map(
(id: string) => serializedCardFromFragments(id, cardFragments),
);
}
}
if (event.content['m.relates_to']?.rel_type === 'm.replace') {
eventId = event.content['m.relates_to']!.event_id!;
event.event_id = eventId;
}
const existingEvent = latestEventsMap.get(eventId);
if (
!existingEvent ||
// we check the timestamps of the events because the existing event may
// itself be an already replaced event. The idea is that you can perform
// multiple replacements on an event. In order to prevent backing out a
// subsequent replacement we also assert that the replacement timestamp is
// after the event that it is replacing
existingEvent.origin_server_ts < event.origin_server_ts
) {
latestEventsMap.set(eventId, event);
}
}
let latestEvents = Array.from(latestEventsMap.values());
latestEvents.sort((a, b) => a.origin_server_ts - b.origin_server_ts);
return latestEvents;
}
function getShouldRespond(history: DiscreteMatrixEvent[]): boolean {
// If the aibot is awaiting command results, it should not respond yet.
let lastEventExcludingCommandResults = history.findLast(
(event) => event.type !== APP_BOXEL_COMMAND_RESULT_EVENT_TYPE,
);
let commandRequests =
lastEventExcludingCommandResults.content[APP_BOXEL_COMMAND_REQUESTS_KEY];
if (!commandRequests || commandRequests.length === 0) {
return true;
}
let lastEventIndex = history.indexOf(lastEventExcludingCommandResults);
let allCommandsHaveResults = commandRequests.every(
(commandRequest: CommandRequest) => {
return history.slice(lastEventIndex).some((event) => {
return (
event.type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
event.content.commandRequestId === commandRequest.id
);
});
},
);
return allCommandsHaveResults;
}
function getEnabledSkills(
eventlist: DiscreteMatrixEvent[],
cardFragments: Map<string, CardFragmentContent>,
): LooseCardResource[] {
let skillsConfigEvent = eventlist.findLast(
(event) => event.type === APP_BOXEL_ROOM_SKILLS_EVENT_TYPE,
) as SkillsConfigEvent;
if (!skillsConfigEvent) {
return [];
}
let enabledEventIds = skillsConfigEvent.content.enabledEventIds;
return enabledEventIds.map(
(id: string) => serializedCardFromFragments(id, cardFragments).data,
);
}
function serializedCardFromFragments(
eventId: string,
fragments: Map<string, CardFragmentContent>,
): LooseSingleCardDocument {
let fragment = fragments.get(eventId);
if (!fragment) {
throw new Error(
`No card fragment found in fragments cache for event id ${eventId}`,
);
}
let cardFragments: CardFragmentContent[] = [];
let currentFragment: string | undefined = eventId;
do {
let fragment = fragments.get(currentFragment);
if (!fragment) {
throw new Error(
`No card fragment found in cache for event id ${eventId}`,
);
}
cardFragments.push(fragment);
currentFragment = fragment.data.nextFragment;
} while (currentFragment);
cardFragments.sort((a, b) => (a.data.index = b.data.index));
if (cardFragments.length !== cardFragments[0].data.totalParts) {
throw new Error(
`Expected to find ${cardFragments[0].data.totalParts} fragments for fragment of event id ${eventId} but found ${cardFragments.length} fragments`,
);
}
return JSON.parse(
cardFragments.map((f) => f.data.cardFragment).join(''),
) as LooseSingleCardDocument;
}
export interface OpenAIPromptMessage {
/**
* The contents of the message. `content` is required for all messages, and may be
* null for assistant messages with function calls.
*/
content: string | null;
/**
* The role of the messages author. One of `system`, `user`, `assistant`, or
* `function`.
*/
role: 'system' | 'user' | 'assistant' | 'tool';
tool_calls?: ChatCompletionMessageToolCall[];
tool_call_id?: string;
}
function setRelevantCards(
cardMap: Map<string, CardResource> = new Map(),
cards: LooseSingleCardDocument[] = [],
) {
for (let card of cards) {
if (card.data.id) {
cardMap.set(card.data.id, card.data as CardResource);
} else {
throw new Error(`bug: don't know how to handle card without ID`);
}
}
return cardMap;
}
interface RelevantCards {
mostRecentlyAttachedCard: LooseCardResource | undefined;
attachedCards: LooseCardResource[];
}
function getMostRecentlyAttachedCard(attachedCards: LooseSingleCardDocument[]) {
let cardResources = attachedCards.filter((c) => c.data.id).map((c) => c.data);
return cardResources.length
? cardResources[cardResources.length - 1]
: undefined;
}
export function getRelevantCards(
history: DiscreteMatrixEvent[],
aiBotUserId: string,
): RelevantCards {
let mostRecentlyAttachedCard: LooseCardResource | undefined;
let attachedCardMap = new Map<string, CardResource>();
for (let event of history) {
if (event.type !== 'm.room.message') {
continue;
}
if (event.sender !== aiBotUserId) {
let { content } = event;
if (content.msgtype === APP_BOXEL_MESSAGE_MSGTYPE) {
setRelevantCards(attachedCardMap, content.data?.attachedCards);
if (content.data?.attachedCards) {
mostRecentlyAttachedCard = getMostRecentlyAttachedCard(
content.data?.attachedCards,
);
}
}
}
}
// Return the cards in a consistent manner
let sortedCards = Array.from(attachedCardMap.values()).sort((a, b) => {
return a.id.localeCompare(b.id);
});
return {
mostRecentlyAttachedCard: mostRecentlyAttachedCard,
attachedCards: sortedCards,
};
}
export async function loadCurrentlyAttachedFiles(
history: DiscreteMatrixEvent[],
aiBotUserId: string,
): Promise<
{
url: string;
name: string;
contentType?: string;
content: string | undefined;
error: string | undefined;
}[]
> {
let lastMessageEventByUser = history.findLast(
(event) => event.sender !== aiBotUserId,
);
let mostRecentUserMessageContent = lastMessageEventByUser?.content as {
msgtype?: string;
data?: {
attachedFiles?: { url: string; name: string; contentType?: string }[];
};
};
if (
!mostRecentUserMessageContent ||
mostRecentUserMessageContent.msgtype !== APP_BOXEL_MESSAGE_MSGTYPE
) {
return [];
}
// We are only interested in downloading the most recently attached files -
// downloading older ones is not needed since the prompt that is being constructed
// should operate on fresh data
if (!mostRecentUserMessageContent.data?.attachedFiles?.length) {
return [];
}
let attachedFiles = mostRecentUserMessageContent.data.attachedFiles;
return Promise.all(
attachedFiles.map(
async (attachedFile: {
url: string;
name: string;
contentType?: string;
}) => {
try {
let content: string | undefined;
let error: string | undefined;
if (attachedFile.contentType?.startsWith('text/')) {
let response = await (globalThis as any).fetch(attachedFile.url);
if (!response.ok) {
throw new Error(`HTTP error. Status: ${response.status}`);
}
content = await response.text();
} else {
error = `Unsupported file type: ${attachedFile.contentType}. For now, only text files are supported.`;
}
return {
url: attachedFile.url,
name: attachedFile.name,
contentType: attachedFile.contentType,
content,
error,
};
} catch (error) {
log.error(`Failed to fetch file ${attachedFile.url}:`, error);
Sentry.captureException(error, {
extra: { fileUrl: attachedFile.url, fileName: attachedFile.name },
});
return {
url: attachedFile.url,
name: attachedFile.name,
contentType: attachedFile.contentType,
content: undefined,
error: `Error loading attached file: ${(error as Error).message}`,
};
}
},
),
);
}
export function attachedFilesToPrompt(
attachedFiles: {
url: string;
name: string;
contentType?: string;
content: string | undefined;
error: string | undefined;
}[],
): string {
if (!attachedFiles.length) {
return 'No attached files';
}
return attachedFiles
.map((f) => {
if (f.error) {
return `${f.name}: ${f.error}`;
}
return `${f.name}: ${f.content}`;
})
.join('\n');
}
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;
}
if (event.content.msgtype === APP_BOXEL_MESSAGE_MSGTYPE) {
let eventTools = event.content.data.context.tools;
if (eventTools?.length) {
for (let tool of eventTools) {
toolMap.set(tool.function.name, tool);
}
}
}
}
return Array.from(toolMap.values()).sort((a, b) =>
a.function.name.localeCompare(b.function.name),
);
}
export function getToolChoice(
history: DiscreteMatrixEvent[],
aiBotUserId: string,
): ToolChoice {
const lastUserMessage = history.findLast(
(event) => event.sender !== aiBotUserId,
);
if (
!lastUserMessage ||
lastUserMessage.type !== 'm.room.message' ||
lastUserMessage.content.msgtype !== APP_BOXEL_MESSAGE_MSGTYPE
) {
// If the last message is not a user message, auto is safe
return 'auto';
}
const messageContext = lastUserMessage.content.data.context;
if (messageContext?.requireToolCall) {
let tools = messageContext.tools || [];
if (tools.length != 1) {
throw new Error('Forced tool calls only work with a single tool');
}
return {
type: 'function',
function: {
name: tools[0].function.name,
},
};
}
return 'auto';
}
function getCommandResults(
cardMessageEvent: CardMessageEvent,
history: DiscreteMatrixEvent[],
) {
let commandResultEvents = history.filter((e) => {
if (
isCommandResultEvent(e) &&
e.content['m.relates_to']?.event_id === cardMessageEvent.event_id
) {
return true;
}
return false;
}) as CommandResultEvent[];
return commandResultEvents;
}
function toToolCalls(event: CardMessageEvent): ChatCompletionMessageToolCall[] {
return (event.content[APP_BOXEL_COMMAND_REQUESTS_KEY] ?? []).map(
(commandRequest: CommandRequest) => {
return {
id: commandRequest.id,
function: {
name: commandRequest.name,
arguments: JSON.stringify(commandRequest.arguments),
},
type: 'function',
};
},
);
}
function toPromptMessageWithToolResults(
event: CardMessageEvent,
history: DiscreteMatrixEvent[],
): OpenAIPromptMessage[] {
let commandResults = getCommandResults(event, history);
return (event.content[APP_BOXEL_COMMAND_REQUESTS_KEY] ?? []).map(
(commandRequest: CommandRequest) => {
let content = 'pending';
let commandResult = commandResults.find(
(commandResult) =>
commandResult.content.commandRequestId === commandRequest.id,
);
if (commandResult) {
let status = commandResult.content['m.relates_to']?.key;
if (
commandResult.content.msgtype ===
APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE
) {
content = `Command ${status}, with result card: ${JSON.stringify(
commandResult.content.data.card,
)}.\n`;
} else {
content = `Command ${status}.\n`;
}
}
return {
role: 'tool',
tool_call_id: commandRequest.id,
content,
};
},
);
}
export async function getModifyPrompt(
history: DiscreteMatrixEvent[],
aiBotUserId: string,
tools: Tool[] = [],
skillCards: LooseCardResource[] = [],
) {
// Need to make sure the passed in username is a full id
if (
aiBotUserId.indexOf(':') === -1 ||
aiBotUserId.startsWith('@') === false
) {
throw new Error("Username must be a full id, e.g. '@ai-bot:localhost'");
}
let historicalMessages: OpenAIPromptMessage[] = [];
for (let event of history) {
if (event.type !== 'm.room.message') {
continue;
}
if (isCommandResultEvent(event)) {
continue;
}
if (
'isStreamingFinished' in event.content &&
event.content.isStreamingFinished === false
) {
continue;
}
let body = event.content.body;
if (body) {
if (event.sender === aiBotUserId) {
let toolCalls = toToolCalls(event);
let historicalMessage: OpenAIPromptMessage = {
role: 'assistant',
content: body,
};
if (toolCalls.length) {
historicalMessage.tool_calls = toolCalls;
}
historicalMessages.push(historicalMessage);
if (toolCalls.length) {
toPromptMessageWithToolResults(event, history).forEach((message) =>
historicalMessages.push(message),
);
}
} else {
if (
event.content.msgtype === APP_BOXEL_MESSAGE_MSGTYPE &&
event.content.data?.context?.openCardIds
) {
body = `User message: ${body}
Context: the user has the following cards open: ${JSON.stringify(
event.content.data.context.openCardIds,
)}`;
} else {
body = `User message: ${body}
Context: the user has no open cards.`;
}
historicalMessages.push({
role: 'user',
content: body,
});
}
}
}
let { mostRecentlyAttachedCard, attachedCards } = getRelevantCards(
history,
aiBotUserId,
);
let attachedFiles = await loadCurrentlyAttachedFiles(history, aiBotUserId);
let systemMessage = `${MODIFY_SYSTEM_MESSAGE}
The user currently has given you the following data to work with:
Cards: ${attachedCardsToMessage(mostRecentlyAttachedCard, attachedCards)}
Attached files:
${attachedFilesToPrompt(attachedFiles)}
`;
if (skillCards.length) {
systemMessage += SKILL_INSTRUCTIONS_MESSAGE;
systemMessage += skillCardsToMessage(skillCards);
systemMessage += '\n';
}
if (tools.length == 0) {
systemMessage +=
'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.';
}
let messages: OpenAIPromptMessage[] = [
{
role: 'system',
content: systemMessage,
},
];
messages = messages.concat(historicalMessages);
return messages;
}
export const attachedCardsToMessage = (
mostRecentlyAttachedCard: LooseCardResource | undefined,
attachedCards: LooseCardResource[],
) => {
let a =
mostRecentlyAttachedCard !== undefined
? `Most recently shared card: ${JSON.stringify(
mostRecentlyAttachedCard,
)}.\n`
: ``;
let b =
attachedCards.length > 0
? `All previously shared cards: ${JSON.stringify(attachedCards)}.\n`
: ``;
return a + b;
};
export const skillCardsToMessage = (cards: LooseCardResource[]) => {
return cards.map((card) => card.attributes?.instructions).join('\n');
};
export function cleanContent(content: string) {
content = content.trim();
if (content.endsWith('json')) {
content = content.slice(0, -4);
}
return content.trim();
}
export const isCommandResultStatusApplied = (event?: MatrixEvent) => {
if (event === undefined) {
return false;
}
return (
isCommandResultEvent(event.event as DiscreteMatrixEvent) &&
event.getContent()['m.relates_to']?.key === 'applied'
);
};
function getModel(eventlist: DiscreteMatrixEvent[]): string {
let activeLLMEvent = eventlist.findLast(
(event) => event.type === APP_BOXEL_ACTIVE_LLM,
) as ActiveLLMEvent;
if (!activeLLMEvent) {
return DEFAULT_LLM;
}
return activeLLMEvent.content.model;
}
export function isCommandResultEvent(
event?: DiscreteMatrixEvent,
): event is CommandResultEvent {
if (event === undefined) {
return false;
}
return (
event.type === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
event.content['m.relates_to']?.rel_type === 'm.annotation'
);
}
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 ||
event.getContent().msgtype === APP_BOXEL_COMMAND_DEFINITIONS_MSGTYPE
) {
return false;
}
return true;
}
// If it's a command result with output, we should respond
if (
event.getType() === APP_BOXEL_COMMAND_RESULT_EVENT_TYPE &&
event.getContent().msgtype === APP_BOXEL_COMMAND_RESULT_WITH_OUTPUT_MSGTYPE
) {
return true;
}
// If it's a different type, or a command result without output, we should not respond
return false;
}