Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/portkey-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import { FetchFunction } from "@ai-sdk/provider-utils";
import { Portkey } from "portkey-ai";
import { defaultHeadersBuilder } from "./utils";
import { withUserAgentSuffix } from "./utils";
import { VERSION } from "./version";
import {
LanguageModelV1,
ProviderV1,
Expand Down Expand Up @@ -56,7 +58,7 @@ export function createPortkey(
): Omit<PortkeyProvider, "chat" | "completions"> {
const portkeyProvider = new Portkey(options) as PortkeyProvider;

const headers = defaultHeadersBuilder(portkeyProvider);
const headers = withUserAgentSuffix(defaultHeadersBuilder(portkeyProvider), `ai-sdk-provider/${VERSION}`);
const getHeaders = () => headers;

const getCommonModelConfig = (modelType: string): CommonModelConfig => ({
Expand Down
32 changes: 32 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,35 @@ export async function fetchImageAsBase64(url: string): Promise<string> {
return btoa(binary);
}
}

/**
* Removes entries from a record where the value is null or undefined.
* @param record - The input object whose entries may be null or undefined.
* @returns A new object containing only entries with non-null and non-undefined values.
*/
export function removeUndefinedEntries<T>(
record: Record<string, T | undefined>,
): Record<string, T> {
return Object.fromEntries(
Object.entries(record).filter(([, value]) => value !== null),
) as Record<string, T>
}

export function withUserAgentSuffix(
headers: HeadersInit | Record<string, string | undefined> | undefined,
...userAgentSuffixParts: string[]
): Record<string, string> {
const cleanedHeaders = removeUndefinedEntries(
(headers as Record<string, string | undefined>) ?? {},
)

const currentUserAgentHeader = cleanedHeaders['user-agent'] || ''
const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts]
.filter(Boolean)
.join(' ')

return {
...cleanedHeaders,
'user-agent': newUserAgent,
}
}
3 changes: 3 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare const __PACKAGE_VERSION__: string | undefined
export const VERSION: string =
__PACKAGE_VERSION__ === undefined ? '0.0.0-test' : __PACKAGE_VERSION__
7 changes: 7 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { defineConfig } from 'tsup';
import { readFileSync } from 'node:fs'

const package_ = JSON.parse(
readFileSync(new URL('package.json', import.meta.url), 'utf8'),
)
export default defineConfig([
{
entry: ['src/index.ts'],
format: ['cjs', 'esm'],
dts: true,
sourcemap: true,
define: {
__PACKAGE_VERSION__: JSON.stringify(package_.version),
},
},
]);
4 changes: 4 additions & 0 deletions vitest.edge.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineConfig } from "vite";
import packageJson from "./package.json";

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -7,4 +8,7 @@ export default defineConfig({
globals: true,
include: ["**/*.test.ts", "**/*.test.tsx"],
},
define: {
__PACKAGE_VERSION__: JSON.stringify(packageJson.version),
},
});
4 changes: 4 additions & 0 deletions vitest.node.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineConfig } from "vite";
import packageJson from "./package.json";

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -7,4 +8,7 @@ export default defineConfig({
globals: true,
include: ["**/*.test.ts", "**/*.test.tsx"],
},
define: {
__PACKAGE_VERSION__: JSON.stringify(packageJson.version),
},
});