Skip to content

Commit

Permalink
knowpro: Time range scoping (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
umeshma authored Feb 5, 2025
1 parent 9093b8d commit aea8e30
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 73 deletions.
2 changes: 1 addition & 1 deletion ts/examples/chat/src/memory/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export function argChunkSize(defaultValue?: number | undefined): ArgDef {
};
}

export function recordFromArgs(
export function keyValuesFromNamedArgs(
args: NamedArgs,
metadata?: CommandMetadata,
): Record<string, string> {
Expand Down
25 changes: 21 additions & 4 deletions ts/examples/chat/src/memory/knowproMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
argSourceFile,
argToDate,
parseFreeAndNamedArguments,
recordFromArgs,
keyValuesFromNamedArgs,
} from "./common.js";
import { dateTime, ensureDir, readJsonFile, writeJsonFile } from "typeagent";
import path from "path";
Expand Down Expand Up @@ -217,6 +217,8 @@ export async function createKnowproCommands(
description ?? "Search current knowPro conversation by terms",
options: {
maxToDisplay: argNum("Maximum matches to display", 25),
startMinute: argNum("Starting at minute."),
endMinute: argNum("Ending minute."),
},
};
if (kType === undefined) {
Expand Down Expand Up @@ -249,8 +251,8 @@ export async function createKnowproCommands(
const matches = await kp.searchConversation(
conversation,
terms,
recordFromArgs(namedArgs, commandDef),
filterFromArgs(namedArgs),
keyValuesFromNamedArgs(namedArgs, commandDef),
filterFromNamedArgs(namedArgs),
);
if (matches === undefined || matches.size === 0) {
context.printer.writeLine("No matches");
Expand All @@ -268,10 +270,25 @@ export async function createKnowproCommands(
}
}

function filterFromArgs(namedArgs: NamedArgs) {
function filterFromNamedArgs(namedArgs: NamedArgs) {
let filter: kp.SearchFilter = {
type: namedArgs.ktype,
};
const dateRange = kp.getTimeRangeForConversation(context.podcast!);
if (dateRange && namedArgs.startMinute >= 0) {
filter.dateRange = {
start: dateTime.addMinutesToDate(
dateRange.start,
namedArgs.startMinute,
),
};
if (namedArgs.endMinute) {
filter.dateRange.end = dateTime.addMinutesToDate(
dateRange.start,
namedArgs.endMinute,
);
}
}
return filter;
}

Expand Down
12 changes: 12 additions & 0 deletions ts/examples/chat/src/memory/knowproPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export class KnowProPrinter extends ChatPrinter {
super();
}

public writeDateRange(dateTime: kp.DateRange) {
this.writeLine(`Started: ${dateTime.start}`);
if (dateTime.end) {
this.writeLine(`Ended: ${dateTime.end}`);
}
}

public writeMessage(message: kp.IMessage) {
const prevColor = this.setForeColor(chalk.cyan);
try {
Expand Down Expand Up @@ -170,6 +177,11 @@ export class KnowProPrinter extends ChatPrinter {

public writeConversationInfo(conversation: kp.IConversation) {
this.writeTitle(conversation.nameTag);
const timeRange = kp.getTimeRangeForConversation(conversation);
if (timeRange) {
this.write("Time range: ");
this.writeDateRange(timeRange);
}
this.writeLine(`${conversation.messages.length} messages`);
return this;
}
Expand Down
14 changes: 13 additions & 1 deletion ts/packages/knowPro/src/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export class MessageAccumulator extends MatchAccumulator<IMessage> {}
export class TextRangeCollection {
// Maintains ranges sorted by message index
private ranges: TextRange[] = [];
private sorted: boolean = false;

constructor() {}

Expand All @@ -372,7 +373,9 @@ export class TextRangeCollection {

public addRange(textRange: TextRange) {
// Future: merge ranges
collections.insertIntoSorted(this.ranges, textRange, this.comparer);
//collections.insertIntoSorted(this.ranges, textRange, this.comparer);
this.ranges.push(textRange);
this.sorted = false;
}

public addRanges(textRanges: TextRange[]) {
Expand All @@ -382,6 +385,8 @@ export class TextRangeCollection {
}

public isInRange(rangeToMatch: TextRange): boolean {
this.ensureSorted();

let i = collections.binarySearchFirst(
this.ranges,
rangeToMatch,
Expand All @@ -403,6 +408,13 @@ export class TextRangeCollection {
return false;
}

private ensureSorted() {
if (!this.sorted) {
this.ranges.sort(this.comparer);
this.sorted = true;
}
}

private comparer(x: TextRange, y: TextRange): number {
return x.start.messageIndex - y.start.messageIndex;
}
Expand Down
19 changes: 19 additions & 0 deletions ts/packages/knowPro/src/conversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { DateRange, IConversation } from "./dataFormat.js";

export function getTimeRangeForConversation(
conversation: IConversation,
): DateRange | undefined {
const messages = conversation.messages;
const start = messages[0].timestamp;
const end = messages[messages.length - 1].timestamp;
if (start !== undefined) {
return {
start: new Date(start),
end: end ? new Date(end) : undefined,
};
}
return undefined;
}
6 changes: 3 additions & 3 deletions ts/packages/knowPro/src/conversationIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ function createKnowledgeModel() {
}

function textLocationFromLocation(
messageIndex: number,
messageIndex: MessageIndex,
chunkIndex = 0,
): TextLocation {
return { messageIndex, chunkIndex };
}

function textRangeFromLocation(
messageIndex: number,
export function textRangeFromLocation(
messageIndex: MessageIndex,
chunkIndex = 0,
): TextRange {
return {
Expand Down
1 change: 1 addition & 0 deletions ts/packages/knowPro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

export * from "./import.js";
export * from "./dataFormat.js";
export * from "./conversation.js";
export * from "./conversationIndex.js";
export * from "./relatedTermsIndex.js";
export * from "./search.js";
Expand Down
Loading

0 comments on commit aea8e30

Please sign in to comment.