Repository navigation
feat: resolve chat title references so that is human readable #10047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,69 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPromptToTitle(t *testing.T) { | ||
| mvRef := `<chat-reference>type="metricsView" metricsView="orders"</chat-reference>` | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| prompt string | ||
| title string | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| prompt: " \n ", | ||
| title: "New Conversation", | ||
| }, | ||
| { | ||
| name: "whitespace is collapsed", | ||
| prompt: " How did\n\nrevenue change? ", | ||
| title: "How did revenue change?", | ||
| }, | ||
| { | ||
| name: "long prompt is truncated", | ||
| prompt: strings.Repeat("a", 60), | ||
| title: strings.Repeat("a", 47) + "...", | ||
| }, | ||
| { | ||
| name: "length is counted in runes", | ||
| prompt: strings.Repeat("ú", 50), | ||
| title: strings.Repeat("ú", 50), | ||
| }, | ||
| { | ||
| name: "truncation doesn't split a rune", | ||
| prompt: strings.Repeat("ú", 51), | ||
| title: strings.Repeat("ú", 47) + "...", | ||
| }, | ||
| { | ||
| name: "reference counts as 10 runes", | ||
| prompt: mvRef + " " + strings.Repeat("a", 39), | ||
| title: mvRef + " " + strings.Repeat("a", 39), | ||
| }, | ||
| { | ||
| name: "text after a reference is truncated", | ||
| prompt: mvRef + " " + strings.Repeat("a", 50), | ||
| title: mvRef + " " + strings.Repeat("a", 36) + "...", | ||
| }, | ||
| { | ||
| name: "reference that doesn't fit is cut before", | ||
| prompt: strings.Repeat("a", 40) + " " + mvRef + " by month", | ||
| title: strings.Repeat("a", 40) + "...", | ||
| }, | ||
| { | ||
| name: "reference that fits exactly is kept", | ||
| prompt: strings.Repeat("a", 37) + mvRef + " by month", | ||
| title: strings.Repeat("a", 37) + mvRef + "...", | ||
| }, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| require.Equal(t, c.title, promptToTitle(c.prompt)) | ||
| }) | ||
| } | ||
| } |
131 changes: 131 additions & 0 deletions
131
web-common/src/features/chat/core/context/conversation-title.spec.ts
This file contains hidden or 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,131 @@ | ||
| import { formatConversationTitle } from "@rilldata/web-common/features/chat/core/context/conversation-title.ts"; | ||
| import type { InlineContextMetadata } from "@rilldata/web-common/features/chat/core/context/metadata.ts"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| const meta: InlineContextMetadata = { | ||
| metricsViewSpecs: { | ||
| orders: { | ||
| metricsViewSpec: { displayName: "Sales" }, | ||
| measures: { revenue: { name: "revenue", displayName: "Net Revenue" } }, | ||
| dimensions: { | ||
| country: { name: "country", displayName: "Customer Country" }, | ||
| }, | ||
| }, | ||
| }, | ||
| canvasSpecs: { overview: { displayName: "Sales Overview" } }, | ||
| componentSpecs: {}, | ||
| }; | ||
|
|
||
| const emptyMeta: InlineContextMetadata = { | ||
| metricsViewSpecs: {}, | ||
| canvasSpecs: {}, | ||
| componentSpecs: {}, | ||
| }; | ||
|
|
||
| describe("formatConversationTitle", () => { | ||
| const testCases: { | ||
| title: string; | ||
| input: string; | ||
| meta?: InlineContextMetadata; | ||
| expected: string; | ||
| }[] = [ | ||
| { | ||
| title: "plain text", | ||
| input: "How did revenue change?", | ||
| expected: "How did revenue change?", | ||
| }, | ||
| { | ||
| title: "metrics view", | ||
| input: `<chat-reference>type="metricsView" metricsView="orders"</chat-reference> by month`, | ||
| expected: "Sales by month", | ||
| }, | ||
| { | ||
| title: "measure", | ||
| input: `What drives <chat-reference>type="measure" metricsView="orders" measure="revenue"</chat-reference>?`, | ||
| expected: "What drives Net Revenue?", | ||
| }, | ||
| { | ||
| title: "dimension", | ||
| input: `Revenue by <chat-reference>type="dimension" metricsView="orders" dimension="country"</chat-reference>`, | ||
| expected: "Revenue by Customer Country", | ||
| }, | ||
| { | ||
| title: "canvas", | ||
| input: `Summarize <chat-reference>type="canvas" canvas="overview"</chat-reference>`, | ||
| expected: "Summarize Sales Overview", | ||
| }, | ||
| { | ||
| title: "skill", | ||
| input: `<chat-reference>type="skill" skill="monthly-close"</chat-reference> for March`, | ||
| expected: "/monthly-close for March", | ||
| }, | ||
| { | ||
| title: "column", | ||
| input: `Nulls in <chat-reference>type="column" model="orders" column="revenue"</chat-reference>`, | ||
| expected: "Nulls in revenue", | ||
| }, | ||
| { | ||
| title: "falls back to names without metadata", | ||
| input: `<chat-reference>type="metricsView" metricsView="orders"</chat-reference> by month`, | ||
| meta: emptyMeta, | ||
| expected: "orders by month", | ||
| }, | ||
| { | ||
| title: "unknown type is dropped", | ||
| input: `<chat-reference>type="unknown" unknown="x"</chat-reference> hello`, | ||
| expected: "hello", | ||
| }, | ||
| { | ||
| title: "inherited object property as type is dropped", | ||
| input: `<chat-reference>type="toString"</chat-reference> <chat-reference>type="constructor"</chat-reference>hello`, | ||
| expected: "hello", | ||
| }, | ||
| { | ||
| title: "dimension values without values", | ||
| input: `<chat-reference>type="dimensionValues" metricsView="orders" dimension="country"</chat-reference>`, | ||
| expected: "Customer Country:", | ||
| }, | ||
| { | ||
| title: "dimension values with values attribute", | ||
| input: `<chat-reference>type="dimensionValues" metricsView="orders" dimension="country" values="US,IN"</chat-reference>`, | ||
| expected: "Customer Country:", | ||
| }, | ||
| { | ||
| title: "reference truncated by the backend", | ||
| input: `<chat-reference>type="metricsView" metricsView="orders"</chat-reference>...`, | ||
| expected: "Sales...", | ||
| }, | ||
| { | ||
| title: "partial reference in an older title is dropped", | ||
| input: `Compare <chat-reference>type="metricsView" metricsVi...`, | ||
| expected: "Compare...", | ||
| }, | ||
| { | ||
| title: "older title cut within the opening tag name", | ||
| // The old backend truncated to `title[:47] + "..."`, so a reference starting at byte 41 was cut to `<chat-`. | ||
| input: `${"a".repeat(40)} <chat-...`, | ||
| expected: `${"a".repeat(40)}...`, | ||
| }, | ||
| { | ||
| title: "older title cut right after the opening angle bracket", | ||
| input: `${"a".repeat(46)}<...`, | ||
| expected: `${"a".repeat(46)}...`, | ||
| }, | ||
| { | ||
| title: "text resembling a tag name prefix without truncation is kept", | ||
| input: "Is x <ch y?", | ||
| expected: "Is x <ch y?", | ||
| }, | ||
| { | ||
| title: "title that is only a partial reference", | ||
| input: `<chat-reference>type="metricsView" metricsVi...`, | ||
| expected: "", | ||
| }, | ||
| ]; | ||
|
|
||
| for (const { title, input, meta: m, expected } of testCases) { | ||
| it(title, () => { | ||
| expect(formatConversationTitle(input, m ?? meta)).toBe(expected); | ||
| }); | ||
| } | ||
| }); |
42 changes: 42 additions & 0 deletions
42
web-common/src/features/chat/core/context/conversation-title.ts
This file contains hidden or 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,42 @@ | ||
| import { InlineContextConfig } from "@rilldata/web-common/features/chat/core/context/config.ts"; | ||
| import { | ||
| convertPromptValueToContext, | ||
| INLINE_CHAT_CONTEXT_TAG, | ||
| InlineContextType, | ||
| } from "@rilldata/web-common/features/chat/core/context/inline-context.ts"; | ||
| import type { InlineContextMetadata } from "@rilldata/web-common/features/chat/core/context/metadata.ts"; | ||
|
|
||
| const CHAT_REFERENCE_REGEX = new RegExp( | ||
| `<${INLINE_CHAT_CONTEXT_TAG}\\b([\\s\\S]*?)</${INLINE_CHAT_CONTEXT_TAG}>`, | ||
| "g", | ||
| ); | ||
| // Older titles were truncated to 47 bytes followed by "..." without regard for references, | ||
| // so they can end in a partial reference, even one cut within its tag name (e.g. `<chat-...`). | ||
| const TAG_NAME_PREFIXES = Array.from(INLINE_CHAT_CONTEXT_TAG, (_, i) => | ||
| INLINE_CHAT_CONTEXT_TAG.slice(0, i), | ||
| ).join("|"); | ||
| const PARTIAL_CHAT_REFERENCE_REGEX = new RegExp( | ||
| `<(?:${INLINE_CHAT_CONTEXT_TAG}\\b[\\s\\S]*|(?:${TAG_NAME_PREFIXES})\\.\\.\\.)$`, | ||
| ); | ||
|
|
||
| /** | ||
| * Converts a conversation title to plain text by replacing the chat references in it with their labels. | ||
| * The backend keeps references in titles as-is so that labels are always up-to-date with the project. | ||
| */ | ||
| export function formatConversationTitle( | ||
| title: string, | ||
| meta: InlineContextMetadata, | ||
| ) { | ||
| const text = title.replace(CHAT_REFERENCE_REGEX, (_, attrs: string) => { | ||
| const ctx = convertPromptValueToContext(attrs); | ||
| if (!ctx) return ""; | ||
| const label = InlineContextConfig[ctx.type].getLabel(ctx, meta); | ||
| return ctx.type === InlineContextType.Skill ? `/${label}` : label; | ||
| }); | ||
|
|
||
| const withoutPartial = text.replace(PARTIAL_CHAT_REFERENCE_REGEX, "").trim(); | ||
| if (withoutPartial !== text.trim() && withoutPartial) { | ||
| return `${withoutPartial}...`; | ||
| } | ||
| return withoutPartial; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.