Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
99 changes: 52 additions & 47 deletions apps/desktop/src/utils/tag-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,68 @@ import { commands as templateCommands } from "@hypr/plugin-template";
import { generateText, localProviderName, modelProvider } from "@hypr/utils/ai";

export async function generateTagsForSession(sessionId: string): Promise<string[]> {
const { type: connectionType } = await connectorCommands.getLlmConnection();
try {
const { type: connectionType } = await connectorCommands.getLlmConnection();

const config = await dbCommands.getConfig();
const session = await dbCommands.getSession({ id: sessionId });
if (!session) {
throw new Error("Session not found");
}
const config = await dbCommands.getConfig();
const session = await dbCommands.getSession({ id: sessionId });
if (!session) {
throw new Error("Session not found");
}

const historicalTags = await dbCommands.listAllTags();
const currentTags = await dbCommands.listSessionTags(sessionId);
const historicalTags = await dbCommands.listAllTags();
const currentTags = await dbCommands.listSessionTags(sessionId);

const extractHashtags = (text: string): string[] => {
const hashtagRegex = /#(\w+)/g;
return Array.from(text.matchAll(hashtagRegex), match => match[1]);
};
const extractHashtags = (text: string): string[] => {
const hashtagRegex = /#(\w+)/g;
return Array.from(text.matchAll(hashtagRegex), match => match[1]);
};

const existingHashtags = extractHashtags(session.raw_memo_html);
const existingHashtags = extractHashtags(session.raw_memo_html);

const systemPrompt = await templateCommands.render(
"suggest_tags.system",
{ config, type: connectionType },
);
const systemPrompt = await templateCommands.render(
"suggest_tags.system",
{ config, type: connectionType },
);

const userPrompt = await templateCommands.render(
"suggest_tags.user",
{
title: session.title,
content: session.raw_memo_html,
existing_hashtags: existingHashtags,
formal_tags: currentTags.map(t => t.name),
historical_tags: historicalTags.slice(0, 20).map(t => t.name),
},
);
const userPrompt = await templateCommands.render(
"suggest_tags.user",
{
title: session.title,
content: session.raw_memo_html,
existing_hashtags: existingHashtags,
formal_tags: currentTags.map(t => t.name),
historical_tags: historicalTags.slice(0, 20).map(t => t.name),
},
);

const provider = await modelProvider();
const model = provider.languageModel("defaultModel");
const provider = await modelProvider();
const model = provider.languageModel("defaultModel");

const result = await generateText({
model,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
providerOptions: {
[localProviderName]: {
metadata: {
grammar: "tags",
const result = await generateText({
model,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
providerOptions: {
[localProviderName]: {
metadata: {
grammar: "tags",
},
},
},
},
});
});

const schema = z.preprocess(
(val) => (typeof val === "string" ? JSON.parse(val) : val),
z.array(z.string().min(1)).min(1).max(5),
);
const schema = z.preprocess(
(val) => (typeof val === "string" ? JSON.parse(val) : val),
z.array(z.string().min(1)).min(1).max(5),
);

const parsed = schema.safeParse(result.text);
return parsed.success ? parsed.data : [];
const parsed = schema.safeParse(result.text);
return parsed.success ? parsed.data : [];
} catch (error) {
console.error("Tag generation failed:", error);
return [];
}
}
2 changes: 1 addition & 1 deletion crates/gbnf/assets/tags.gbnf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
root ::= "[" "'" word "'" ("," ws "'" word "'")* "]"
root ::= "[" "\"" word "\"" ("," ws "\"" word "\"")* "]"
word ::= [a-zA-Z0-9_-]+
ws ::= " "*
4 changes: 2 additions & 2 deletions crates/gbnf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ mod tests {
let gbnf = gbnf_validator::Validator::new().unwrap();

for (input, expected) in vec![
("['meeting', 'summary']", true),
("['meeting', 'summary', '']", false),
("[\"meeting\", \"summary\"]", true),
("[\"meeting\", \"summary\", \"\"]", false),
] {
let result = gbnf.validate(TAGS, input).unwrap();
assert_eq!(result, expected, "failed: {}", input);
Expand Down
Loading