This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add functions for parsing chat messages #853
Merged
mattjohnsonpint
merged 10 commits into
main
from
mjp/hyp-3397-model-message-state-reconstruction
May 22, 2025
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
06cbbee
Implement ParseMessages for Go
mattjohnsonpint a44ae50
Implement parseMessages for AssemblyScript
mattjohnsonpint a6b41f9
Update CHANGELOG.md
mattjohnsonpint bf99cc2
Merge branch 'main' into mjp/hyp-3397-model-message-state-reconstruction
mattjohnsonpint 3957c68
.
mattjohnsonpint 2dc067c
workaround json issue
mattjohnsonpint 647a215
.
mattjohnsonpint c41fe88
.
mattjohnsonpint 83ec2f5
.
mattjohnsonpint 88f4629
.
mattjohnsonpint 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2025 Hypermode Inc. | ||
| * Licensed under the terms of the Apache License, Version 2.0 | ||
| * See the LICENSE file that accompanied this code for further details. | ||
| * | ||
| * SPDX-FileCopyrightText: 2025 Hypermode Inc. <[email protected]> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { expect, it, log, run } from "as-test"; | ||
| import { JSON } from "json-as"; | ||
|
|
||
| import { | ||
| SystemMessage, | ||
| UserMessage, | ||
| AssistantMessage, | ||
| RequestMessage, | ||
| parseMessages, | ||
| } from "../../models/openai/chat"; | ||
|
|
||
| it("should round-trip chat messages", () => { | ||
| const msgs: RequestMessage[] = [ | ||
| new SystemMessage("You are a helpful assistant."), | ||
| new UserMessage("What is the capital of France?"), | ||
| new AssistantMessage("The capital of France is Paris."), | ||
| ]; | ||
|
|
||
| const data = JSON.stringify(msgs); | ||
| const parsedMsgs = parseMessages(data); | ||
|
|
||
| expect(parsedMsgs.length).toBe(msgs.length); | ||
| for (let i = 0; i < msgs.length; i++) { | ||
| expect(msgs[i].role).toBe(parsedMsgs[i].role); | ||
| } | ||
|
|
||
| const roundTrip = JSON.stringify(parsedMsgs); | ||
|
|
||
| log(data); | ||
| log(roundTrip); | ||
| expect(roundTrip).toBe(data); // <----- this currently fails | ||
mattjohnsonpint marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| run(); | ||
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,17 @@ | ||
| /* | ||
| * Copyright 2025 Hypermode Inc. | ||
| * Licensed under the terms of the Apache License, Version 2.0 | ||
| * See the LICENSE file that accompanied this code for further details. | ||
| * | ||
| * SPDX-FileCopyrightText: 2025 Hypermode Inc. <[email protected]> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { readFileSync } from "fs"; | ||
| import { instantiate } from "../build/openai.spec.js"; | ||
| const binary = readFileSync("./build/openai.spec.wasm"); | ||
| const module = new WebAssembly.Module(binary); | ||
| instantiate(module, { | ||
| env: {}, | ||
| modus_models: {}, | ||
| }); |
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,53 @@ | ||
| /* | ||
| * Copyright 2025 Hypermode Inc. | ||
| * Licensed under the terms of the Apache License, Version 2.0 | ||
| * See the LICENSE file that accompanied this code for further details. | ||
| * | ||
| * SPDX-FileCopyrightText: 2025 Hypermode Inc. <[email protected]> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package openai_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/hypermodeinc/modus/sdk/go/pkg/models/openai" | ||
| ) | ||
|
|
||
| func TestMessagesRoundTrip(t *testing.T) { | ||
| msgs := []openai.RequestMessage{ | ||
| openai.NewSystemMessage("You are a helpful assistant."), | ||
| openai.NewUserMessage("What is the capital of France?"), | ||
| openai.NewAssistantMessage("The capital of France is Paris."), | ||
| } | ||
|
|
||
| data, err := json.Marshal(msgs) | ||
| if err != nil { | ||
| t.Fatalf("Failed to marshal messages: %v", err) | ||
| } | ||
|
|
||
| parsedMsgs, err := openai.ParseMessages(data) | ||
| if err != nil { | ||
| t.Fatalf("Failed to parse messages: %v", err) | ||
| } | ||
| if len(parsedMsgs) != len(msgs) { | ||
| t.Fatalf("Expected %d messages, but got %d", len(msgs), len(parsedMsgs)) | ||
| } | ||
|
|
||
| for i, msg := range msgs { | ||
| if msg.Role() != parsedMsgs[i].Role() { | ||
| t.Errorf("Expected role %s for message %d, but got %s", msg.Role(), i, parsedMsgs[i].Role()) | ||
| } | ||
| } | ||
|
|
||
| roundTrip, err := json.Marshal(parsedMsgs) | ||
| if err != nil { | ||
| t.Fatalf("Failed to marshal parsed messages: %v", err) | ||
| } | ||
| if !bytes.Equal(data, roundTrip) { | ||
| t.Fatalf("Expected original and parsed messages to have the same JSON representation, but they differ") | ||
| } | ||
| } |
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.