parse all messages for rules and add them (#28)

This commit is contained in:
Aidan Bleser 2025-06-19 08:01:59 -05:00 committed by GitHub
parent 371853de44
commit 7cd104b27b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 13 deletions

View file

@ -28,7 +28,7 @@ export const get_enabled = query({
.withIndex('by_user', (q) => q.eq('user_id', session.userId))
.collect();
return array.toMap(models, (m) => [getModelKey(m), m]);
return array.toRecord(models, (m) => [getModelKey(m), m]);
},
});

View file

@ -61,6 +61,34 @@ export function sum<T>(arr: T[], fn: (item: T) => number): number {
return total;
}
/** Maps the provided array into a record
*
* @param arr Array of items to be entered into a record
* @param fn A mapping function to transform each item into a key value pair
* @returns
*
* ## Usage
* ```ts
* const record = toRecord([5, 4, 3, 2, 1], (item, i) => [i, item]);
*
* console.log(record); // { "0": 5, "1": 4, "2": 3, "3": 2, "4": 1 }
* ```
*/
export function toRecord<T, V>(
arr: T[],
fn: (item: T, index: number) => [key: string, value: V]
): Record<string, V> {
const record: Record<string, V> = {};
for (let i = 0; i < arr.length; i++) {
const [key, value] = fn(arr[i]!, i);
record[key] = value;
}
return record;
}
/** Maps the provided array into a map
*
* @param arr Array of items to be entered into a map
@ -74,16 +102,16 @@ export function sum<T>(arr: T[], fn: (item: T) => number): number {
* console.log(map); // Map(5) { 0 => 5, 1 => 4, 2 => 3, 3 => 2, 4 => 1 }
* ```
*/
export function toMap<T, V>(
export function toMap<T, K, V>(
arr: T[],
fn: (item: T, index: number) => [key: string, value: V]
): Record<string, V> {
const map: Record<string, V> = {};
fn: (item: T, index: number) => [key: K, value: V]
): Map<K, V> {
const map: Map<K, V> = new Map();
for (let i = 0; i < arr.length; i++) {
const [key, value] = fn(arr[i]!, i);
map[key] = value;
map.set(key, value);
}
return map;

View file

@ -12,6 +12,7 @@ import OpenAI from 'openai';
import { z } from 'zod/v4';
import { generationAbortControllers } from './cache.js';
import { md } from '$lib/utils/markdown-it.js';
import * as array from '$lib/utils/array';
// Set to true to enable debug logging
const ENABLE_LOGGING = true;
@ -371,13 +372,22 @@ async function generateAIResponse({
return;
}
const attachedRules = [
...rulesResult.value.filter((r) => r.attach === 'always'),
...parseMessageForRules(
userMessage.content,
let attachedRules = rulesResult.value.filter((r) => r.attach === 'always');
for (const message of messages) {
const parsedRules = parseMessageForRules(
message.content,
rulesResult.value.filter((r) => r.attach === 'manual')
),
];
);
attachedRules.push(...parsedRules);
}
// remove duplicates
attachedRules = array.fromMap(
array.toMap(attachedRules, (r) => [r._id, r]),
(_k, v) => v
);
log(`Background: ${attachedRules.length} rules attached`, startTime);
@ -412,7 +422,7 @@ async function generateAIResponse({
...formattedMessages,
{
role: 'system' as const,
content: `Respond in markdown format. The user may have mentioned one or more rules to follow with the @<rule_name> syntax. Please follow these rules.
content: `The user has mentioned one or more rules to follow with the @<rule_name> syntax. Please follow these rules as they apply.
Rules to follow:
${attachedRules.map((r) => `- ${r.name}: ${r.rule}`).join('\n')}`,
},