parse all messages for rules and add them (#28)
This commit is contained in:
parent
371853de44
commit
7cd104b27b
3 changed files with 51 additions and 13 deletions
|
|
@ -28,7 +28,7 @@ export const get_enabled = query({
|
||||||
.withIndex('by_user', (q) => q.eq('user_id', session.userId))
|
.withIndex('by_user', (q) => q.eq('user_id', session.userId))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
return array.toMap(models, (m) => [getModelKey(m), m]);
|
return array.toRecord(models, (m) => [getModelKey(m), m]);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,34 @@ export function sum<T>(arr: T[], fn: (item: T) => number): number {
|
||||||
return total;
|
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
|
/** Maps the provided array into a map
|
||||||
*
|
*
|
||||||
* @param arr Array of items to be entered 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 }
|
* 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[],
|
arr: T[],
|
||||||
fn: (item: T, index: number) => [key: string, value: V]
|
fn: (item: T, index: number) => [key: K, value: V]
|
||||||
): Record<string, V> {
|
): Map<K, V> {
|
||||||
const map: Record<string, V> = {};
|
const map: Map<K, V> = new Map();
|
||||||
|
|
||||||
for (let i = 0; i < arr.length; i++) {
|
for (let i = 0; i < arr.length; i++) {
|
||||||
const [key, value] = fn(arr[i]!, i);
|
const [key, value] = fn(arr[i]!, i);
|
||||||
|
|
||||||
map[key] = value;
|
map.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import OpenAI from 'openai';
|
||||||
import { z } from 'zod/v4';
|
import { z } from 'zod/v4';
|
||||||
import { generationAbortControllers } from './cache.js';
|
import { generationAbortControllers } from './cache.js';
|
||||||
import { md } from '$lib/utils/markdown-it.js';
|
import { md } from '$lib/utils/markdown-it.js';
|
||||||
|
import * as array from '$lib/utils/array';
|
||||||
|
|
||||||
// Set to true to enable debug logging
|
// Set to true to enable debug logging
|
||||||
const ENABLE_LOGGING = true;
|
const ENABLE_LOGGING = true;
|
||||||
|
|
@ -371,13 +372,22 @@ async function generateAIResponse({
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const attachedRules = [
|
let attachedRules = rulesResult.value.filter((r) => r.attach === 'always');
|
||||||
...rulesResult.value.filter((r) => r.attach === 'always'),
|
|
||||||
...parseMessageForRules(
|
for (const message of messages) {
|
||||||
userMessage.content,
|
const parsedRules = parseMessageForRules(
|
||||||
|
message.content,
|
||||||
rulesResult.value.filter((r) => r.attach === 'manual')
|
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);
|
log(`Background: ${attachedRules.length} rules attached`, startTime);
|
||||||
|
|
||||||
|
|
@ -412,7 +422,7 @@ async function generateAIResponse({
|
||||||
...formattedMessages,
|
...formattedMessages,
|
||||||
{
|
{
|
||||||
role: 'system' as const,
|
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:
|
Rules to follow:
|
||||||
${attachedRules.map((r) => `- ${r.name}: ${r.rule}`).join('\n')}`,
|
${attachedRules.map((r) => `- ${r.name}: ${r.rule}`).join('\n')}`,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue