fix(ai-zai): require clean api base urls#816
Open
aiirvizionz wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the Z.ai adapter’s endpoint construction by normalizing and validating the configured API base URL before issuing chat completions requests, preventing surprising request targets and double-slash paths.
Changes:
- Normalize
baseUrlwith a newcleanBaseUrl()helper before building/chat/completions. - Reject base URLs containing credentials, query strings, or fragments prior to any network call.
- Extend tests to cover trailing-slash normalization and add validation-focused rejection coverage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/ai/zai/src/index.ts | Adds cleanBaseUrl() and uses it to build a predictable /chat/completions endpoint. |
| packages/ai/zai/src/index.test.ts | Updates the configured base URL test to include a trailing slash and adds a rejection test for “unclean” base URLs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+77
to
+86
| function cleanBaseUrl(value: string): string { | ||
| const url = new URL(value); | ||
| if (url.protocol !== 'https:' && url.protocol !== 'http:') { | ||
| throw new Error('Z.ai baseUrl must use http or https'); | ||
| } | ||
| if (url.username || url.password || url.search || url.hash) { | ||
| throw new Error('Z.ai baseUrl must be a clean API base without credentials, query, or hash'); | ||
| } | ||
| return url.toString().replace(/\/+$/, ''); | ||
| } |
Comment on lines
+105
to
+126
| it('rejects configured base URLs with credentials, query, or hash', async () => { | ||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal('fetch', fetchMock); | ||
|
|
||
| await expect( | ||
| adapter.generate(ctx(), 'hello', {}, { | ||
| baseUrl: 'https://user:pass@example.test/api/paas/v4', | ||
| }), | ||
| ).rejects.toThrow('clean API base'); | ||
| await expect( | ||
| adapter.generate(ctx(), 'hello', {}, { | ||
| baseUrl: 'https://example.test/api/paas/v4?target=chat', | ||
| }), | ||
| ).rejects.toThrow('clean API base'); | ||
| await expect( | ||
| adapter.generate(ctx(), 'hello', {}, { | ||
| baseUrl: 'https://example.test/api/paas/v4#chat', | ||
| }), | ||
| ).rejects.toThrow('clean API base'); | ||
|
|
||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
/api/paas/v4/bases do not produce double-slash request pathsWhy
The adapter previously concatenated
config.baseUrldirectly with/chat/completions. A custom base URL ending in/produced a//chat/completionspath, and URLs with credentials/query/hash could create surprising request targets. This keeps Z.ai endpoint construction explicit and predictable.Validation
vitest run packages/ai/zai/src/index.test.ts(7 passed)tsc -p packages/ai/zai/tsconfig.json --noEmitgit diff --check