@fencyai/js 0.1.37 → 0.1.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { ApiError } from '../types/ApiError';
1
2
  import { ChatCompletion } from '../types/ChatCompletion';
2
3
  import { CreateChatCompletionRequest } from '../types/CreateChatCompletionRequest';
3
4
  export interface CreateChatCompletionOptions {
@@ -13,8 +14,15 @@ export interface CreateChatCompletionOptions {
13
14
  * @returns A promise that resolves to a ChatCompletionResponse
14
15
  * @throws Error if the request fails or the response is invalid
15
16
  */
17
+ export type CreateChatCompletionResponse = {
18
+ type: 'success';
19
+ completion: ChatCompletion;
20
+ } | {
21
+ type: 'error';
22
+ error: ApiError;
23
+ };
16
24
  export declare function createChatCompletion(params: {
17
25
  pk: string;
18
26
  request: CreateChatCompletionRequest;
19
27
  baseUrl: string;
20
- }): Promise<ChatCompletion>;
28
+ }): Promise<CreateChatCompletionResponse>;
@@ -1,16 +1,9 @@
1
+ import { isApiError } from '../types/ApiError';
1
2
  import { isChatCompletion } from '../types/ChatCompletion';
2
3
  import { getPackageVersion } from '../utils/version';
3
- /**
4
- * Creates a chat completion by making a POST request to the Fency API.
5
- *
6
- * @param pk - The publishable key (used as Bearer token)
7
- * @param streamId - The stream ID to associate with the chat completion
8
- * @param options - Optional configuration (apiUrl, request overrides)
9
- * @returns A promise that resolves to a ChatCompletionResponse
10
- * @throws Error if the request fails or the response is invalid
11
- */
12
4
  export async function createChatCompletion(params) {
13
5
  const apiUrl = `${params.baseUrl}/v1/pub/chat-completions`;
6
+ console.log('request', params.request);
14
7
  const response = await fetch(apiUrl, {
15
8
  method: 'POST',
16
9
  headers: {
@@ -21,11 +14,33 @@ export async function createChatCompletion(params) {
21
14
  body: JSON.stringify(params.request),
22
15
  });
23
16
  if (!response.ok) {
24
- throw new Error(`Failed to create chat completion: ${response.status} ${response.statusText}`);
17
+ const error = await response.json();
18
+ if (isApiError(error)) {
19
+ return {
20
+ type: 'error',
21
+ error,
22
+ };
23
+ }
24
+ return {
25
+ type: 'error',
26
+ error: {
27
+ code: 'UnknownError',
28
+ message: `Unknown error: ${JSON.stringify(error)}`,
29
+ },
30
+ };
25
31
  }
26
32
  const completion = await response.json();
27
33
  if (!isChatCompletion(completion)) {
28
- throw new Error('Invalid chat completion response');
34
+ return {
35
+ type: 'error',
36
+ error: {
37
+ code: 'InvalidResponse',
38
+ message: `Invalid chat completion response: ${JSON.stringify(completion)}`,
39
+ },
40
+ };
29
41
  }
30
- return completion;
42
+ return {
43
+ type: 'success',
44
+ completion,
45
+ };
31
46
  }
@@ -1,3 +1,4 @@
1
+ import { ApiError } from '../types/ApiError';
1
2
  import { ChatCompletionStream } from '../types/ChatCompletionStream';
2
3
  /**
3
4
  * Creates a new stream by making a POST request to the Fency API.
@@ -7,7 +8,14 @@ import { ChatCompletionStream } from '../types/ChatCompletionStream';
7
8
  * @returns A promise that resolves to a ChatCompletionStream
8
9
  * @throws Error if the request fails or the response is invalid
9
10
  */
11
+ export type CreateChatCompletionStreamResponse = {
12
+ type: 'success';
13
+ stream: ChatCompletionStream;
14
+ } | {
15
+ type: 'error';
16
+ error: ApiError;
17
+ };
10
18
  export declare function createChatCompletionStream(params: {
11
19
  pk: string;
12
20
  baseUrl: string;
13
- }): Promise<ChatCompletionStream>;
21
+ }): Promise<CreateChatCompletionStreamResponse>;
@@ -1,13 +1,6 @@
1
+ import { isApiError } from '../types/ApiError';
1
2
  import { isChatCompletionStream, } from '../types/ChatCompletionStream';
2
3
  import { getPackageVersion } from '../utils/version';
3
- /**
4
- * Creates a new stream by making a POST request to the Fency API.
5
- *
6
- * @param pk - The publishable key (used as Bearer token and default name)
7
- * @param options - Optional configuration (apiUrl, name)
8
- * @returns A promise that resolves to a ChatCompletionStream
9
- * @throws Error if the request fails or the response is invalid
10
- */
11
4
  export async function createChatCompletionStream(params) {
12
5
  const apiUrl = `${params.baseUrl}/v1/pub/chat-completion-streams`;
13
6
  const response = await fetch(apiUrl, {
@@ -19,11 +12,33 @@ export async function createChatCompletionStream(params) {
19
12
  },
20
13
  });
21
14
  if (!response.ok) {
22
- throw new Error(`Failed to create stream: ${response.status} ${response.statusText}`);
15
+ const error = await response.json();
16
+ if (isApiError(error)) {
17
+ return {
18
+ type: 'error',
19
+ error,
20
+ };
21
+ }
22
+ return {
23
+ type: 'error',
24
+ error: {
25
+ code: 'UnknownError',
26
+ message: `Unknown error: ${JSON.stringify(error)}`,
27
+ },
28
+ };
23
29
  }
24
30
  const stream = await response.json();
25
31
  if (!isChatCompletionStream(stream)) {
26
- throw new Error('Invalid stream response');
32
+ return {
33
+ type: 'error',
34
+ error: {
35
+ code: 'InvalidResponse',
36
+ message: `Invalid stream response: ${JSON.stringify(stream)}`,
37
+ },
38
+ };
27
39
  }
28
- return stream;
40
+ return {
41
+ type: 'success',
42
+ stream: stream,
43
+ };
29
44
  }
@@ -0,0 +1,5 @@
1
+ export declare const isApiError: (response: unknown) => response is ApiError;
2
+ export type ApiError = {
3
+ code: string;
4
+ message: string;
5
+ };
@@ -0,0 +1,8 @@
1
+ export const isApiError = (response) => {
2
+ return (typeof response === 'object' &&
3
+ response !== null &&
4
+ 'code' in response &&
5
+ 'message' in response &&
6
+ typeof response.code === 'string' &&
7
+ typeof response.message === 'string');
8
+ };
@@ -3,4 +3,7 @@ import { ChatCompletionMessage } from './ChatCompletionMessage';
3
3
  export type CreateAnthropicChatCompletionParams = {
4
4
  model: AnthropicModel;
5
5
  messages: Array<ChatCompletionMessage>;
6
+ temperature?: number;
7
+ topP?: number;
8
+ topK?: number;
6
9
  };
@@ -3,4 +3,7 @@ export type CreateGeminiChatCompletionParams = {
3
3
  model: GeminiModel;
4
4
  content: string;
5
5
  responseJsonSchema?: string;
6
+ temperature?: number;
7
+ topP?: number;
8
+ topK?: number;
6
9
  };
@@ -4,4 +4,6 @@ export type CreateOpenAiChatCompletionParams = {
4
4
  model: OpenAiModel;
5
5
  messages: Array<ChatCompletionMessage>;
6
6
  responseJsonSchema?: string;
7
+ temperature?: number;
8
+ topP?: number;
7
9
  };
@@ -1,5 +1,14 @@
1
1
  /**
2
- * Get the current package version from package.json
2
+ * Get the current package version
3
3
  * @returns The package version as a string
4
4
  */
5
5
  export declare function getPackageVersion(): string;
6
+ /**
7
+ * Override the SDK version (useful for testing or custom builds)
8
+ * @param version - The version to use
9
+ */
10
+ export declare function setPackageVersion(version: string): void;
11
+ /**
12
+ * Reset the version to the default build-time version
13
+ */
14
+ export declare function resetPackageVersion(): void;
@@ -1,29 +1,26 @@
1
- import { readFileSync } from 'fs';
2
- import { dirname, join } from 'path';
3
- import { fileURLToPath } from 'url';
4
- // Cache the version to avoid reading the file multiple times
5
- let cachedVersion = null;
1
+ // Browser-compatible version utility
2
+ // The version is set at build time and can be overridden if needed
3
+ // Build-time version constant - this should be updated during the build process
4
+ const SDK_VERSION = '0.1.38';
5
+ // Allow runtime override if needed
6
+ let versionOverride = null;
6
7
  /**
7
- * Get the current package version from package.json
8
+ * Get the current package version
8
9
  * @returns The package version as a string
9
10
  */
10
11
  export function getPackageVersion() {
11
- if (cachedVersion !== null) {
12
- return cachedVersion;
13
- }
14
- try {
15
- // Get the directory of the current file
16
- const __filename = fileURLToPath(import.meta.url);
17
- const __dirname = dirname(__filename);
18
- // Navigate to the package.json file (2 levels up: utils -> src -> js -> package.json)
19
- const packageJsonPath = join(__dirname, '../../package.json');
20
- const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
21
- return packageJson.version;
22
- }
23
- catch (error) {
24
- // Fallback to a default version if we can't read the package.json
25
- console.warn('Could not read package version, using fallback:', error);
26
- cachedVersion = '0.1.35';
27
- return cachedVersion;
28
- }
12
+ return versionOverride || SDK_VERSION;
13
+ }
14
+ /**
15
+ * Override the SDK version (useful for testing or custom builds)
16
+ * @param version - The version to use
17
+ */
18
+ export function setPackageVersion(version) {
19
+ versionOverride = version;
20
+ }
21
+ /**
22
+ * Reset the version to the default build-time version
23
+ */
24
+ export function resetPackageVersion() {
25
+ versionOverride = null;
29
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fencyai/js",
3
- "version": "0.1.37",
3
+ "version": "0.1.39",
4
4
  "description": "> TODO: description",
5
5
  "author": "staklau <steinaageklaussen@gmail.com>",
6
6
  "homepage": "",
@@ -39,5 +39,5 @@
39
39
  "ts-jest": "^29.1.1",
40
40
  "typescript": "^5.3.3"
41
41
  },
42
- "gitHead": "dec184b1288042cf37fa26c8fbf2f648f7ce533e"
42
+ "gitHead": "799ae72310f87de040cd45875f4a718df1807764"
43
43
  }