Skip to content

Commit

Permalink
fix (ai/provider-utils): expose size argument in generateId (#3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Sep 14, 2024
1 parent 461c9fd commit 273f696
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-otters-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/provider-utils': patch
---

fix (ai/provider-utils): expose size argument in generateId
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const id = generateId();
{
name: 'size',
type: 'number',
description: 'The length of the generated ID. It defaults to 8.',
description: 'The length of the generated ID. It defaults to 7.',
},
]}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/core/generate-object/generate-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { NoObjectGeneratedError } from './no-object-generated-error';
import { getOutputStrategy } from './output-strategy';
import { validateObjectGenerationInput } from './validate-object-generation-input';

const originalGenerateId = createIdGenerator({ prefix: 'aiobj-', length: 24 });
const originalGenerateId = createIdGenerator({ prefix: 'aiobj-', size: 24 });

/**
Generate a structured, typed object for a given prompt and schema using a language model.
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/core/generate-object/stream-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { createIdGenerator } from '@ai-sdk/provider-utils';
import { prepareOutgoingHttpHeaders } from '../util/prepare-outgoing-http-headers';
import { writeToServerResponse } from '../util/write-to-server-response';

const originalGenerateId = createIdGenerator({ prefix: 'aiobj-', length: 24 });
const originalGenerateId = createIdGenerator({ prefix: 'aiobj-', size: 24 });

type OnFinishCallback<RESULT> = (event: {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/core/generate-text/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { toResponseMessages } from './to-response-messages';
import { ToToolCallArray, parseToolCall } from './tool-call';
import { ToToolResultArray } from './tool-result';

const originalGenerateId = createIdGenerator({ prefix: 'aitxt-', length: 24 });
const originalGenerateId = createIdGenerator({ prefix: 'aitxt-', size: 24 });

/**
Generate a text and call tools for a given prompt using a language model.
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/core/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { ToToolCall } from './tool-call';
import { ToToolResult } from './tool-result';
import { StepResult } from './step-result';

const originalGenerateId = createIdGenerator({ prefix: 'aitxt-', length: 24 });
const originalGenerateId = createIdGenerator({ prefix: 'aitxt-', size: 24 });

/**
Generate a text and call tools for a given prompt using a language model.
Expand Down
17 changes: 17 additions & 0 deletions packages/provider-utils/src/generate-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, it } from 'vitest';
import { generateId } from './generate-id';

it('should generate an ID with the correct length', () => {
expect(generateId(10)).toHaveLength(10);
});

it('should generate an ID with the correct default length', () => {
expect(generateId()).toHaveLength(7);
});

it('should generate unique IDs', () => {
const id1 = generateId();
const id2 = generateId();

expect(id1).not.toBe(id2);
});
18 changes: 10 additions & 8 deletions packages/provider-utils/src/generate-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import { customAlphabet } from 'nanoid/non-secure';
*
* @param alphabet - The alphabet to use for the ID. Default: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.
* @param prefix - The prefix of the ID to generate. Default: ''.
* @param length - The length of the random part of the ID to generate. Default: 7.
* @param size - The size of the random part of the ID to generate. Default: 7.
*/
// TODO change length to 16 in 4.0
//TODO change default size to 16 in 4.0
export const createIdGenerator = ({
prefix = '',
length = 7,
size: defaultSize = 7,
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
}: {
prefix?: string;
length?: number;
size?: number;
alphabet?: string;
} = {}): (() => string) => {
const generator = customAlphabet(alphabet, length);
return () => `${prefix}${generator()}`;
} = {}): ((size?: number) => string) => {
const generator = customAlphabet(alphabet, defaultSize);
return size => `${prefix}${generator(size)}`;
};

/**
* Generates a 7-character random string to use for IDs. Not secure.
*
* @param size - The size of the ID to generate. Default: 7.
*/
//TODO change length to 16 in 4.0
//TODO change default size to 16 in 4.0
export const generateId = createIdGenerator();

0 comments on commit 273f696

Please sign in to comment.