-
Notifications
You must be signed in to change notification settings - Fork 0
fix(speak): keep help-offer dialogue coherent across worker and client #21
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 1 commit
Commits
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
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
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,39 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { recentDialogueTurnsForNpc } from "./dialogueTurns.js"; | ||
| import type { ChatMessage } from "./types.js"; | ||
|
|
||
| describe("recentDialogueTurnsForNpc", () => { | ||
| it("pairs player lines with matching npc thread only", () => { | ||
| const messages: ChatMessage[] = [ | ||
| { id: "1", role: "player", text: "a", npcId: "npc-5" }, | ||
| { id: "2", role: "npc", text: "r1", npcId: "npc-1" }, | ||
| { id: "3", role: "player", text: "b", npcId: "npc-5" }, | ||
| { id: "4", role: "npc", text: "r5", npcId: "npc-5" }, | ||
| ]; | ||
| expect(recentDialogueTurnsForNpc(messages, "npc-5")).toEqual([ | ||
| { role: "player", text: "b" }, | ||
| { role: "npc", text: "r5" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("excludes interleaved player messages targeting other npcs", () => { | ||
| const messages: ChatMessage[] = [ | ||
| { id: "1", role: "player", text: "to-1a", npcId: "npc-1" }, | ||
| { id: "2", role: "npc", text: "from-1a", npcId: "npc-1" }, | ||
| { id: "3", role: "player", text: "to-5", npcId: "npc-5" }, | ||
| { id: "4", role: "npc", text: "from-5", npcId: "npc-5" }, | ||
| { id: "5", role: "player", text: "to-1b", npcId: "npc-1" }, | ||
| { id: "6", role: "npc", text: "from-1b", npcId: "npc-1" }, | ||
| ]; | ||
| expect(recentDialogueTurnsForNpc(messages, "npc-1")).toEqual([ | ||
| { role: "player", text: "to-1a" }, | ||
| { role: "npc", text: "from-1a" }, | ||
| { role: "player", text: "to-1b" }, | ||
| { role: "npc", text: "from-1b" }, | ||
| ]); | ||
| expect(recentDialogueTurnsForNpc(messages, "npc-5")).toEqual([ | ||
| { role: "player", text: "to-5" }, | ||
| { role: "npc", text: "from-5" }, | ||
| ]); | ||
| }); | ||
| }); |
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,35 @@ | ||
| import type { DialogueTurn } from "@aetherlife/shared"; | ||
| import type { ChatMessage } from "./types.js"; | ||
|
|
||
| /** Completed player↔npc turns for one NPC thread (mirrors game-server dialogue-session). */ | ||
| export function recentDialogueTurnsForNpc( | ||
| messages: readonly ChatMessage[], | ||
| npcId: string, | ||
| limit = 10, | ||
| ): DialogueTurn[] { | ||
| const turns: DialogueTurn[] = []; | ||
| let pendingPlayers: DialogueTurn[] = []; | ||
|
|
||
| for (const m of messages) { | ||
| if (m.role === "error") continue; | ||
| if (m.role === "player") { | ||
| if (m.npcId && m.npcId !== npcId) { | ||
| pendingPlayers = []; | ||
| continue; | ||
| } | ||
| pendingPlayers.push({ role: "player", text: m.text }); | ||
| continue; | ||
| } | ||
| if (m.role === "npc") { | ||
| if (m.npcId !== npcId) { | ||
| pendingPlayers = []; | ||
| continue; | ||
| } | ||
| turns.push(...pendingPlayers); | ||
| pendingPlayers = []; | ||
| turns.push({ role: "npc", text: m.text }); | ||
| } | ||
| } | ||
|
|
||
| return turns.slice(-limit); | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When chats with different NPCs overlap, this clears the pending player line for the target NPC as soon as any other NPC reply is seen. Since
sendMessageonly blocks the samenpcId, a user can send to npc-5, switch to npc-1, receive npc-1's reply, then receive npc-5's reply; the npc-5 turn is omitted fromrecentDialogueTurnsForNpc, so the client may show a casual preview for the next npc-5 follow-up while the server/worker correctly see history. Keep pending turns per NPC or ignore unrelated NPC messages instead of clearing them.Useful? React with 👍 / 👎.