Skip to content

fix(ai-zai): require clean api base urls#816

Open
aiirvizionz wants to merge 2 commits into
profullstack:masterfrom
aiirvizionz:clientkit/zai-clean-base-url
Open

fix(ai-zai): require clean api base urls#816
aiirvizionz wants to merge 2 commits into
profullstack:masterfrom
aiirvizionz:clientkit/zai-clean-base-url

Conversation

@aiirvizionz

Copy link
Copy Markdown
Contributor

Summary

  • normalize the Z.ai API base URL before building the chat completions endpoint
  • strip trailing slashes so custom /api/paas/v4/ bases do not produce double-slash request paths
  • reject base URLs containing credentials, query strings, or fragments before any network call

Why

The adapter previously concatenated config.baseUrl directly with /chat/completions. A custom base URL ending in / produced a //chat/completions path, 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 --noEmit
  • git diff --check

Copilot AI review requested due to automatic review settings July 23, 2026 17:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 baseUrl with a new cleanBaseUrl() 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();
});
Copilot AI review requested due to automatic review settings July 23, 2026 17:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants