Skip to content
Open
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
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"chroma-js": "^3.1.2",
"clsx": "^2.1.1",
"date-fns": "^4.1.0",
"diff": "^8.0.2",
"dompurify": "^3.3.0",
"effect": "^3.19.4",
"json5": "^2.2.3",
Expand Down
30 changes: 30 additions & 0 deletions apps/desktop/src/store/tinybase/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ export const StoreComponent = ({ persist = true }: { persist?: boolean }) => {
deleted: !cells,
updated: !!cells,
});

if (tableId === TABLE_SESSIONS && cells && "raw_md" in cells) {
const rawMd = store.getCell("sessions", rowId, "raw_md");
if (typeof rawMd === "string") {
const userId = store.getCell("sessions", rowId, "user_id");
const createdAtMs = Date.now();

let transcriptId: string | undefined;
store.forEachRow("transcripts", (tId, _forEachCell) => {
const tSessionId = store.getCell(
"transcripts",
tId,
"session_id",
);
if (tSessionId === rowId) {
transcriptId = tId;
}
});

const historyId = crypto.randomUUID();
store.setRow("note_history", historyId, {
user_id: typeof userId === "string" ? userId : DEFAULT_USER_ID,
created_at: new Date().toISOString(),
session_id: rowId,
content: rawMd,
created_at_ms: createdAtMs,
transcript_id: transcriptId,
});
}
}
});
});
},
Expand Down
22 changes: 22 additions & 0 deletions apps/desktop/src/store/tinybase/schema-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
mappingSessionParticipantSchema as baseMappingSessionParticipantSchema,
mappingTagSessionSchema as baseMappingTagSessionSchema,
memorySchema as baseMemorySchema,
noteHistorySchema as baseNoteHistorySchema,
organizationSchema as baseOrganizationSchema,
sessionSchema as baseSessionSchema,
speakerHintSchema as baseSpeakerHintSchema,
Expand Down Expand Up @@ -116,6 +117,17 @@ export const memorySchema = baseMemorySchema.omit({ id: true }).extend({
created_at: z.string(),
});

export const noteHistorySchema = baseNoteHistorySchema
.omit({ id: true })
.extend({
created_at: z.string(),
created_at_ms: z.number(),
transcript_id: z.preprocess(
(val) => val ?? undefined,
z.string().optional(),
),
});

export const enhancedNoteSchema = z.object({
user_id: z.string(),
created_at: z.string(),
Expand Down Expand Up @@ -165,6 +177,7 @@ export type TemplateSection = z.infer<typeof templateSectionSchema>;
export type ChatGroup = z.infer<typeof chatGroupSchema>;
export type ChatMessage = z.infer<typeof chatMessageSchema>;
export type Memory = z.infer<typeof memorySchema>;
export type NoteHistory = z.infer<typeof noteHistorySchema>;
export type EnhancedNote = z.infer<typeof enhancedNoteSchema>;

export type SessionStorage = ToStorageType<typeof sessionSchema>;
Expand All @@ -176,6 +189,7 @@ export type SpeakerHintStorage = ToStorageType<
export type TemplateStorage = ToStorageType<typeof templateSchema>;
export type ChatMessageStorage = ToStorageType<typeof chatMessageSchema>;
export type MemoryStorage = ToStorageType<typeof memorySchema>;
export type NoteHistoryStorage = ToStorageType<typeof noteHistorySchema>;
export type EnhancedNoteStorage = ToStorageType<typeof enhancedNoteSchema>;

export const externalTableSchemaForTinybase = {
Expand Down Expand Up @@ -296,6 +310,14 @@ export const externalTableSchemaForTinybase = {
type: { type: "string" },
text: { type: "string" },
} satisfies InferTinyBaseSchema<typeof memorySchema>,
note_history: {
user_id: { type: "string" },
created_at: { type: "string" },
session_id: { type: "string" },
content: { type: "string" },
created_at_ms: { type: "number" },
transcript_id: { type: "string" },
} satisfies InferTinyBaseSchema<typeof noteHistorySchema>,
enhanced_notes: {
user_id: { type: "string" },
created_at: { type: "string" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Diff from "diff";

import type { TaskArgsMap, TaskArgsMapTransformed, TaskConfig } from ".";
import {
buildSegments,
Expand Down Expand Up @@ -54,16 +56,21 @@ async function transformArgs(
sessionData: sessionContext.sessionData,
participants: sessionContext.participants,
segments: sessionContext.segments,
noteHistory: sessionContext.noteHistory,
template,
};
}

function getSessionContext(sessionId: string, store: MainStore) {
const transcripts = collectTranscripts(sessionId, store);
const noteHistory = getNoteHistory(sessionId, transcripts, store);

return {
rawMd: getStringCell(store, "sessions", sessionId, "raw_md"),
sessionData: getSessionData(sessionId, store),
participants: getParticipants(sessionId, store),
segments: getTranscriptSegments(sessionId, store),
noteHistory,
};
}

Expand Down Expand Up @@ -377,3 +384,148 @@ function getNumberCell(
const value = store.getCell(tableId, rowId, columnId);
return typeof value === "number" ? value : undefined;
}

type NoteHistoryEntry = {
content: string;
created_at_ms: number;
transcript_id?: string;
};

type TemporalNoteHistory = {
beforeMeeting?: string;
duringMeeting?: string;
afterMeeting?: string;
};

function getNoteHistory(
sessionId: string,
transcripts: readonly TranscriptMeta[],
store: MainStore,
): TemporalNoteHistory | undefined {
const historyEntries: NoteHistoryEntry[] = [];

store.forEachRow("note_history", (historyId, _forEachCell) => {
const historySessionId = getOptionalStringCell(
store,
"note_history",
historyId,
"session_id",
);
if (historySessionId !== sessionId) {
return;
}

const content = getStringCell(store, "note_history", historyId, "content");
const createdAtMs = getNumberCell(
store,
"note_history",
historyId,
"created_at_ms",
);
const transcriptId = getOptionalStringCell(
store,
"note_history",
historyId,
"transcript_id",
);

if (content && createdAtMs !== undefined) {
historyEntries.push({
content,
created_at_ms: createdAtMs,
transcript_id: transcriptId,
});
}
});

if (historyEntries.length === 0) {
return undefined;
}

historyEntries.sort((a, b) => a.created_at_ms - b.created_at_ms);

if (transcripts.length === 0) {
const diffs = computeDiffs(historyEntries);
return {
beforeMeeting: diffs.length > 0 ? diffs.join("\n\n") : undefined,
};
}

const firstTranscriptStart = Math.min(...transcripts.map((t) => t.startedAt));
const lastTranscriptStart = Math.max(...transcripts.map((t) => t.startedAt));

const beforeEntries = historyEntries.filter(
(entry) => entry.created_at_ms < firstTranscriptStart,
);
const duringEntries = historyEntries.filter(
(entry) =>
entry.created_at_ms >= firstTranscriptStart &&
entry.created_at_ms <= lastTranscriptStart,
);
const afterEntries = historyEntries.filter(
(entry) => entry.created_at_ms > lastTranscriptStart,
);

const result: TemporalNoteHistory = {};

if (beforeEntries.length > 0) {
const diffs = computeDiffs(beforeEntries);
if (diffs.length > 0) {
result.beforeMeeting = diffs.join("\n\n");
}
}

if (duringEntries.length > 0) {
const diffs = computeDiffs(duringEntries);
if (diffs.length > 0) {
result.duringMeeting = diffs.join("\n\n");
}
}

if (afterEntries.length > 0) {
const diffs = computeDiffs(afterEntries);
if (diffs.length > 0) {
result.afterMeeting = diffs.join("\n\n");
}
}

if (!result.beforeMeeting && !result.duringMeeting && !result.afterMeeting) {
return undefined;
}

return result;
}

function computeDiffs(entries: readonly NoteHistoryEntry[]): string[] {
if (entries.length === 0) {
return [];
}

const diffs: string[] = [];

for (let i = 0; i < entries.length; i++) {
if (i === 0) {
if (entries[i].content.trim()) {
diffs.push(`Initial notes:\n${entries[i].content}`);
}
} else {
const prevContent = entries[i - 1].content;
const currentContent = entries[i].content;

const changes = Diff.diffLines(prevContent, currentContent);
const addedLines: string[] = [];

for (const change of changes) {
if (change.added) {
addedLines.push(change.value.trim());
}
}

if (addedLines.length > 0) {
diffs.push(`Added:\n${addedLines.join("\n")}`);
}
}
}

return diffs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ async function getSystemPrompt(args: TaskArgsMapTransformed["enhance"]) {
}

async function getUserPrompt(args: TaskArgsMapTransformed["enhance"]) {
const { rawMd, sessionData, participants, template, segments } = args;
const { rawMd, sessionData, participants, template, segments, noteHistory } =
args;

const result = await templateCommands.render("enhance.user", {
content: rawMd,
session: sessionData,
participants,
template,
segments,
note_history: noteHistory,
});

if (result.status === "error") {
Expand Down
5 changes: 5 additions & 0 deletions apps/desktop/src/store/zustand/ai-task/task-configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface TaskArgsMapTransformed {
end_ms: number;
}>;
}>;
noteHistory?: {
beforeMeeting?: string;
duringMeeting?: string;
afterMeeting?: string;
};
template?: Pick<Template, "sections">;
};
title: {
Expand Down
20 changes: 20 additions & 0 deletions crates/template/assets/enhance.user.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,24 @@ Sections to cover:

{% endif %}

{%- if note_history %}
{%- if note_history.beforeMeeting %}

Notes written before the meeting (research, planning):
{{ note_history.beforeMeeting }}
{%- endif %}

{%- if note_history.duringMeeting %}

Notes written during the meeting:
{{ note_history.duringMeeting }}
{%- endif %}

{%- if note_history.afterMeeting %}

Notes written after the meeting:
{{ note_history.afterMeeting }}
{%- endif %}
{%- endif %}

{{ segments | transcript }}
18 changes: 18 additions & 0 deletions packages/db/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,23 @@ export const memories = pgTable(
(table) => createPolicies(TABLE_MEMORIES, table.user_id),
).enableRLS();

export const TABLE_NOTE_HISTORY = "note_history";
export const noteHistory = pgTable(
TABLE_NOTE_HISTORY,
{
...SHARED,
session_id: uuid("session_id")
.notNull()
.references(() => sessions.id, { onDelete: "cascade" }),
content: text("content").notNull(),
created_at_ms: integer("created_at_ms").notNull(),
transcript_id: uuid("transcript_id").references(() => transcripts.id, {
onDelete: "set null",
}),
},
(table) => createPolicies(TABLE_NOTE_HISTORY, table.user_id),
).enableRLS();

export const humanSchema = createSelectSchema(humans);
export const organizationSchema = createSelectSchema(organizations);
export const folderSchema = createSelectSchema(folders);
Expand All @@ -320,6 +337,7 @@ export const templateSchema = createSelectSchema(templates);
export const chatGroupSchema = createSelectSchema(chatGroups);
export const chatMessageSchema = createSelectSchema(chatMessages);
export const memorySchema = createSelectSchema(memories);
export const noteHistorySchema = createSelectSchema(noteHistory);

export const providerSpeakerIndexSchema = z.object({
speaker_index: z.number(),
Expand Down
Loading
Loading