-
Notifications
You must be signed in to change notification settings - Fork 167
Add GLM-4 Plus as a model option #60
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
Open
devin-ai-integration
wants to merge
4
commits into
main
Choose a base branch
from
devin/1772486666-add-glm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e185b92
Add GLM-4 Plus as a model option
devin-ai-integration[bot] 9bd7804
Remove unused Rect import from GlmIcon
devin-ai-integration[bot] 73fbde4
Fix TextDecoder to use stream mode for multi-byte UTF-8 safety
devin-ai-integration[bot] 96cc673
Handle missing response body to prevent hanging connection
devin-ai-integration[bot] 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,29 @@ | ||
| import Svg, { Path } from 'react-native-svg'; | ||
|
|
||
| interface IGlmIcon { | ||
| size: number | ||
| theme: any | ||
| selected: boolean | ||
| } | ||
|
|
||
| export function GlmIcon({ | ||
| size, | ||
| theme, | ||
| selected, | ||
| ...props | ||
| }: IGlmIcon) { | ||
| const fill = selected ? theme.tintTextColor : theme.textColor | ||
| return ( | ||
| <Svg | ||
| {...props} | ||
| width={size} | ||
| height={size} | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <Path | ||
| d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 3c1.66 0 3 1.34 3 3s-1.34 3-3 3-3-1.34-3-3 1.34-3 3-3zm0 14.2c-2.5 0-4.71-1.28-6-3.22.03-1.99 4-3.08 6-3.08 1.99 0 5.97 1.09 6 3.08-1.29 1.94-3.5 3.22-6 3.22z" | ||
| fill={fill} | ||
| /> | ||
| </Svg> | ||
| ) | ||
| } |
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| ANTHROPIC_API_KEY="" | ||
| OPENAI_API_KEY="" | ||
| GEMINI_API_KEY="" | ||
| GLM_API_KEY="" |
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,95 @@ | ||
| import { Request, Response } from "express" | ||
| import asyncHandler from 'express-async-handler' | ||
|
|
||
| type ModelLabel = 'glm4Plus' | ||
| type ModelName = 'glm-4-plus' | ||
|
|
||
| const models: Record<ModelLabel, ModelName> = { | ||
| glm4Plus: 'glm-4-plus', | ||
| } | ||
|
|
||
| interface RequestBody { | ||
| prompt: string; | ||
| model: ModelLabel; | ||
| } | ||
|
|
||
| export const glm = asyncHandler(async (req: Request, res: Response) => { | ||
| try { | ||
| res.writeHead(200, { | ||
| 'Content-Type': 'text/event-stream', | ||
| 'Connection': 'keep-alive', | ||
| 'Cache-Control': 'no-cache' | ||
| }) | ||
|
|
||
| const { prompt, model }: RequestBody = req.body | ||
| const selectedModel = models[model] | ||
|
|
||
| if (!selectedModel) { | ||
| res.write('data: [DONE]\n\n') | ||
| res.end() | ||
| return | ||
| } | ||
|
|
||
| const decoder = new TextDecoder() | ||
| const response = await fetch('https://open.bigmodel.cn/api/paas/v4/chat/completions', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${process.env.GLM_API_KEY || ''}` | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: selectedModel, | ||
| messages: [{ role: 'user', content: prompt }], | ||
| stream: true | ||
| }) | ||
| }) | ||
|
|
||
| const reader = response.body?.getReader() | ||
| if (reader) { | ||
| let brokenLine = '' | ||
|
|
||
| while (true) { | ||
| const { done, value } = await reader.read() | ||
|
|
||
| if (done) { | ||
| break | ||
| } | ||
|
|
||
| let chunk = decoder.decode(value, {stream: true}) | ||
|
|
||
| if (brokenLine) { | ||
| chunk = brokenLine + chunk | ||
| brokenLine = '' | ||
| } | ||
|
|
||
| const lines = chunk.split('\n') | ||
|
|
||
| for (const line of lines) { | ||
| const trimmed = line.trim() | ||
| if (!trimmed || !trimmed.startsWith('data: ')) continue | ||
| const data = trimmed.replace('data: ', '') | ||
| if (data === '[DONE]') continue | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(data) | ||
| if (parsed.choices?.[0]?.delta?.content) { | ||
| res.write(`data: ${JSON.stringify(parsed.choices[0].delta)}\n\n`) | ||
| } | ||
| } catch { | ||
| brokenLine = line | ||
| } | ||
| } | ||
| } | ||
|
|
||
| res.write('data: [DONE]\n\n') | ||
| res.end() | ||
| } else { | ||
| res.write('data: [DONE]\n\n') | ||
| res.end() | ||
| } | ||
devin-ai-integration[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (err) { | ||
| console.log('error in GLM chat: ', err) | ||
| res.write('data: [DONE]\n\n') | ||
| res.end() | ||
| } | ||
| }) | ||
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.
🟡 SSE stream parsing silently drops messages when chunk boundary splits within the
data:prefixThe SSE stream parser in
glm.tssilently discards entire messages when a TCP/HTTP chunk boundary falls within thedata:prefix of an SSE line.Root Cause
The
brokenLinerecovery mechanism (lines 60-63, 78-80) only saves a line asbrokenLinewhenJSON.parsefails in thecatchblock. However, if a chunk boundary splits within thedata:prefix itself (e.g., chunk 1 ends with\ndatand chunk 2 starts witha: {"choices":[...]}), the partial linedatis trimmed and skipped at line 69 (continue) because it doesn't start withdata:. Sincecontinuebypasses thecatchblock,brokenLineis never set.On the next chunk, the continuation
a: {"choices":[...]}also doesn't start withdata:and is similarly skipped. The entire SSE message is silently lost.Impact: Occasional dropped tokens in streamed responses. In practice this is rare since chunk boundaries typically align to newlines in SSE streams, but it can happen under network conditions that fragment TCP segments at unfortunate boundaries.
Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.
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.
Acknowledged — this is a valid edge case but extremely unlikely in practice since SSE servers typically flush on line boundaries. The existing GPT and Claude handlers in this repo use similar patterns. Happy to apply the fix if the maintainer wants it, but leaving as-is for now to stay consistent with the rest of the codebase.