-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: ollama-deepseek reasoning support #1080
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
mrubens
merged 1 commit into
RooCodeInc:main
from
System233:feat-ollama-deepseek-reasoning
Feb 20, 2025
Merged
Changes from all commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { XmlMatcher } from "../xml-matcher" | ||
|
|
||
| describe("XmlMatcher", () => { | ||
| it("only match at position 0", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("<think>data</think>"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: true, | ||
| data: "data", | ||
| }, | ||
| ]) | ||
| }) | ||
| it("tag with space", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("< think >data</ think >"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: true, | ||
| data: "data", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("invalid tag", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("< think 1>data</ think >"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: false, | ||
| data: "< think 1>data</ think >", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("anonymous tag", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("<>data</>"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: false, | ||
| data: "<>data</>", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("streaming push", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [ | ||
| ...matcher.update("<thi"), | ||
| ...matcher.update("nk"), | ||
| ...matcher.update(">dat"), | ||
| ...matcher.update("a</"), | ||
| ...matcher.update("think>"), | ||
| ] | ||
| expect(chunks).toHaveLength(2) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: true, | ||
| data: "dat", | ||
| }, | ||
| { | ||
| matched: true, | ||
| data: "a", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("nested tag", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("<think>X<think>Y</think>Z</think>"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: true, | ||
| data: "X<think>Y</think>Z", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("nested invalid tag", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("<think>X<think>Y</thxink>Z</think>"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(2) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: true, | ||
| data: "X<think>Y</thxink>Z", | ||
| }, | ||
| { | ||
| matched: true, | ||
| data: "</think>", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("Wrong matching position", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("1<think>data</think>"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: false, | ||
| data: "1<think>data</think>", | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("Unclosed tag", () => { | ||
| const matcher = new XmlMatcher("think") | ||
| const chunks = [...matcher.update("<think>data"), ...matcher.final()] | ||
| expect(chunks).toHaveLength(1) | ||
| expect(chunks).toEqual([ | ||
| { | ||
| matched: true, | ||
| data: "data", | ||
| }, | ||
| ]) | ||
| }) | ||
| }) |
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,105 @@ | ||
| export interface XmlMatcherResult { | ||
| matched: boolean | ||
| data: string | ||
| } | ||
| export class XmlMatcher<Result = XmlMatcherResult> { | ||
| index = 0 | ||
| chunks: XmlMatcherResult[] = [] | ||
| cached: string[] = [] | ||
| matched: boolean = false | ||
| state: "TEXT" | "TAG_OPEN" | "TAG_CLOSE" = "TEXT" | ||
| depth = 0 | ||
| pointer = 0 | ||
| constructor( | ||
| readonly tagName: string, | ||
| readonly transform?: (chunks: XmlMatcherResult) => Result, | ||
| readonly position = 0, | ||
| ) {} | ||
| private collect() { | ||
| if (!this.cached.length) { | ||
| return | ||
| } | ||
| const last = this.chunks.at(-1) | ||
| const data = this.cached.join("") | ||
| const matched = this.matched | ||
| if (last?.matched === matched) { | ||
| last.data += data | ||
| } else { | ||
| this.chunks.push({ | ||
| data, | ||
| matched, | ||
| }) | ||
| } | ||
| this.cached = [] | ||
| } | ||
| private pop() { | ||
| const chunks = this.chunks | ||
| this.chunks = [] | ||
| if (!this.transform) { | ||
| return chunks as Result[] | ||
| } | ||
| return chunks.map(this.transform) | ||
| } | ||
|
|
||
| private _update(chunk: string) { | ||
| for (let i = 0; i < chunk.length; i++) { | ||
| const char = chunk[i] | ||
| this.cached.push(char) | ||
| this.pointer++ | ||
|
|
||
| if (this.state === "TEXT") { | ||
| if (char === "<" && (this.pointer <= this.position + 1 || this.matched)) { | ||
| this.state = "TAG_OPEN" | ||
| this.index = 0 | ||
| } else { | ||
| this.collect() | ||
| } | ||
| } else if (this.state === "TAG_OPEN") { | ||
| if (char === ">" && this.index === this.tagName.length) { | ||
| this.state = "TEXT" | ||
| if (!this.matched) { | ||
| this.cached = [] | ||
| } | ||
| this.depth++ | ||
| this.matched = true | ||
| } else if (this.index === 0 && char === "/") { | ||
| this.state = "TAG_CLOSE" | ||
| } else if (char === " " && (this.index === 0 || this.index === this.tagName.length)) { | ||
| continue | ||
| } else if (this.tagName[this.index] === char) { | ||
| this.index++ | ||
| } else { | ||
| this.state = "TEXT" | ||
| this.collect() | ||
| } | ||
| } else if (this.state === "TAG_CLOSE") { | ||
| if (char === ">" && this.index === this.tagName.length) { | ||
| this.state = "TEXT" | ||
| this.depth-- | ||
| this.matched = this.depth > 0 | ||
| if (!this.matched) { | ||
| this.cached = [] | ||
| } | ||
| } else if (char === " " && (this.index === 0 || this.index === this.tagName.length)) { | ||
| continue | ||
| } else if (this.tagName[this.index] === char) { | ||
| this.index++ | ||
| } else { | ||
| this.state = "TEXT" | ||
| this.collect() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| final(chunk?: string) { | ||
| if (chunk) { | ||
| this._update(chunk) | ||
| } | ||
| this.collect() | ||
| return this.pop() | ||
| } | ||
| update(chunk: string) { | ||
| this._update(chunk) | ||
| return this.pop() | ||
| } | ||
| } |
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.
Consider adding error handling (try/catch) around matcher.update() in the stream loop. This ensures that any errors in the XML parsing logic within XmlMatcher are caught and handled appropriately.