-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6278c5
commit f919604
Showing
4 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { MaybeLLMOutputMatch } from "@llm-ui/react"; | ||
import { describe, expect, it } from "vitest"; | ||
import { findCompleteCsvBlock, findPartialCsvBlock } from "./matchers"; | ||
import { CsvBlockOptions } from "./options"; | ||
|
||
type TestCase = { | ||
name: string; | ||
input: string; | ||
expected: MaybeLLMOutputMatch; | ||
options?: CsvBlockOptions; | ||
}; | ||
describe("findCompleteCsvBlock", () => { | ||
const testCases: TestCase[] = [ | ||
{ | ||
name: "single char", | ||
input: "⦅a⦆", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 3, | ||
outputRaw: "⦅a⦆", | ||
}, | ||
}, | ||
{ | ||
name: "multiple chars", | ||
input: "⦅abc⦆", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 5, | ||
outputRaw: "⦅abc⦆", | ||
}, | ||
}, | ||
{ | ||
name: "delimited", | ||
input: "⦅a,b,c⦆", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 7, | ||
outputRaw: "⦅a,b,c⦆", | ||
}, | ||
}, | ||
{ | ||
name: "text before", | ||
input: "abc⦅a,b,c⦆", | ||
expected: { | ||
startIndex: 3, | ||
endIndex: 10, | ||
outputRaw: "⦅a,b,c⦆", | ||
}, | ||
}, | ||
{ | ||
name: "text after", | ||
input: "⦅a,b,c⦆ def", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 7, | ||
outputRaw: "⦅a,b,c⦆", | ||
}, | ||
}, | ||
{ | ||
name: "text before and after", | ||
input: "abc⦅a,b,c⦆ def", | ||
expected: { | ||
startIndex: 3, | ||
endIndex: 10, | ||
outputRaw: "⦅a,b,c⦆", | ||
}, | ||
}, | ||
{ | ||
name: "not a block", | ||
input: "```\nhello\n```", | ||
expected: undefined, | ||
}, | ||
{ | ||
name: "unfinished block", | ||
input: "⦅a,b,c", | ||
expected: undefined, | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ name, input, expected, options }) => { | ||
it(name, () => { | ||
const result = findCompleteCsvBlock(options)(input); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("findPartialCsvBlock", () => { | ||
const testCases: TestCase[] = [ | ||
{ | ||
name: "single char", | ||
input: "⦅a", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 2, | ||
outputRaw: "⦅a", | ||
}, | ||
}, | ||
{ | ||
name: "multiple chars", | ||
input: "⦅abc", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 4, | ||
outputRaw: "⦅abc", | ||
}, | ||
}, | ||
{ | ||
name: "delimited", | ||
input: "⦅a,b,c", | ||
expected: { | ||
startIndex: 0, | ||
endIndex: 6, | ||
outputRaw: "⦅a,b,c", | ||
}, | ||
}, | ||
{ | ||
name: "text before", | ||
input: "abc⦅a,b,c", | ||
expected: { | ||
startIndex: 3, | ||
endIndex: 9, | ||
outputRaw: "⦅a,b,c", | ||
}, | ||
}, | ||
{ | ||
name: "not a block", | ||
input: "```\nhello\n```", | ||
expected: undefined, | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ name, input, expected, options }) => { | ||
it(name, () => { | ||
const result = findPartialCsvBlock(options)(input); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { LLMOutputMatcher } from "@llm-ui/react"; | ||
import { CsvBlockOptions, getOptions } from "./options"; | ||
|
||
import { regexMatcher } from "@llm-ui/shared"; | ||
|
||
export const findCompleteCsvBlock = ( | ||
userOptions?: Partial<CsvBlockOptions>, | ||
): LLMOutputMatcher => { | ||
const { startChar, endChar } = getOptions(userOptions); | ||
|
||
const regex = new RegExp(`${startChar}([\\s\\S]*?)${endChar}`); | ||
return regexMatcher(regex); | ||
}; | ||
|
||
export const findPartialCsvBlock = ( | ||
userOptions?: Partial<CsvBlockOptions>, | ||
): LLMOutputMatcher => { | ||
const { startChar } = getOptions(userOptions); | ||
|
||
const regex = new RegExp(`${startChar}([\\s\\S]*)`); | ||
return regexMatcher(regex); | ||
}; |