@fencyai/js 0.1.40 → 0.1.42

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,46 +1,48 @@
1
- import { isApiError } from '../types/ApiError';
2
1
  import { isChatCompletion } from '../types/ChatCompletion';
2
+ import { parseNonOkResponse } from '../utils/parseNonOkResponse';
3
3
  import { getPackageVersion } from '../utils/version';
4
4
  export async function createChatCompletion(params) {
5
5
  const apiUrl = `${params.baseUrl}/v1/pub/chat-completions`;
6
6
  console.log('request', params.request);
7
- const response = await fetch(apiUrl, {
8
- method: 'POST',
9
- headers: {
10
- 'Content-Type': 'application/json',
11
- Authorization: `Bearer ${params.pk}`,
12
- 'X-Fency-Sdk-Version': getPackageVersion(),
13
- },
14
- body: JSON.stringify(params.request),
15
- });
16
- if (!response.ok) {
17
- const error = await response.json();
18
- if (isApiError(error)) {
7
+ try {
8
+ const response = await fetch(apiUrl, {
9
+ method: 'POST',
10
+ headers: {
11
+ 'Content-Type': 'application/json',
12
+ Authorization: `Bearer ${params.pk}`,
13
+ 'X-Fency-Sdk-Version': getPackageVersion(),
14
+ },
15
+ body: JSON.stringify(params.request),
16
+ });
17
+ if (!response.ok) {
18
+ const error = await parseNonOkResponse(response);
19
+ return {
20
+ type: 'error',
21
+ error: error,
22
+ };
23
+ }
24
+ const completion = await response.json();
25
+ if (!isChatCompletion(completion)) {
19
26
  return {
20
27
  type: 'error',
21
- error,
28
+ error: {
29
+ code: 'InvalidResponse',
30
+ message: `Invalid chat completion response: ${JSON.stringify(completion)}`,
31
+ },
22
32
  };
23
33
  }
24
34
  return {
25
- type: 'error',
26
- error: {
27
- code: 'UnknownError',
28
- message: `Unknown error: ${JSON.stringify(error)}`,
29
- },
35
+ type: 'success',
36
+ completion,
30
37
  };
31
38
  }
32
- const completion = await response.json();
33
- if (!isChatCompletion(completion)) {
39
+ catch (error) {
34
40
  return {
35
41
  type: 'error',
36
42
  error: {
37
- code: 'InvalidResponse',
38
- message: `Invalid chat completion response: ${JSON.stringify(completion)}`,
43
+ code: 'UnknownError',
44
+ message: `Unknown error!\n${error ? `Error: ${error}\n` : ''}`.trim(),
39
45
  },
40
46
  };
41
47
  }
42
- return {
43
- type: 'success',
44
- completion,
45
- };
46
48
  }
@@ -1,44 +1,46 @@
1
- import { isApiError } from '../types/ApiError';
2
1
  import { isChatCompletionStream, } from '../types/ChatCompletionStream';
2
+ import { parseNonOkResponse } from '../utils/parseNonOkResponse';
3
3
  import { getPackageVersion } from '../utils/version';
4
4
  export async function createChatCompletionStream(params) {
5
5
  const apiUrl = `${params.baseUrl}/v1/pub/chat-completion-streams`;
6
- const response = await fetch(apiUrl, {
7
- method: 'POST',
8
- headers: {
9
- 'Content-Type': 'application/json',
10
- Authorization: `Bearer ${params.pk}`,
11
- 'X-Fency-Sdk-Version': getPackageVersion(),
12
- },
13
- });
14
- if (!response.ok) {
15
- const error = await response.json();
16
- if (isApiError(error)) {
6
+ try {
7
+ const response = await fetch(apiUrl, {
8
+ method: 'POST',
9
+ headers: {
10
+ 'Content-Type': 'application/json',
11
+ Authorization: `Bearer ${params.pk}`,
12
+ 'X-Fency-Sdk-Version': getPackageVersion(),
13
+ },
14
+ });
15
+ if (!response.ok) {
16
+ const error = await parseNonOkResponse(response);
17
+ return {
18
+ type: 'error',
19
+ error: error,
20
+ };
21
+ }
22
+ const stream = await response.json();
23
+ if (!isChatCompletionStream(stream)) {
17
24
  return {
18
25
  type: 'error',
19
- error,
26
+ error: {
27
+ code: 'InvalidResponse',
28
+ message: `Invalid stream response: ${JSON.stringify(stream)}`,
29
+ },
20
30
  };
21
31
  }
22
32
  return {
23
- type: 'error',
24
- error: {
25
- code: 'UnknownError',
26
- message: `Unknown error: ${JSON.stringify(error)}`,
27
- },
33
+ type: 'success',
34
+ stream: stream,
28
35
  };
29
36
  }
30
- const stream = await response.json();
31
- if (!isChatCompletionStream(stream)) {
37
+ catch (error) {
32
38
  return {
33
39
  type: 'error',
34
40
  error: {
35
- code: 'InvalidResponse',
36
- message: `Invalid stream response: ${JSON.stringify(stream)}`,
41
+ code: 'UnknownError',
42
+ message: `Unknown error!\n${error ? `Error: ${error}\n` : ''}`.trim(),
37
43
  },
38
44
  };
39
45
  }
40
- return {
41
- type: 'success',
42
- stream: stream,
43
- };
44
46
  }
package/lib/index.d.ts CHANGED
@@ -1,21 +1,22 @@
1
1
  import { createChatCompletion } from './api/createChatCompletion';
2
2
  import { createChatCompletionStream } from './api/createChatCompletionStream';
3
3
  import { loadFency } from './loadFency';
4
- export type { FencyInstance, FencyOptions, } from './types';
4
+ export type { FencyInstance, FencyOptions } from './types';
5
+ export type { AnthropicModel } from './types/AnthropicModel';
5
6
  export type { ChatCompletion, isChatCompletion } from './types/ChatCompletion';
6
7
  export type { ChatCompletionMessage } from './types/ChatCompletionMessage';
7
8
  export type { ChatCompletionStream } from './types/ChatCompletionStream';
8
- export type { CreateChatCompletionRequest } from './types/CreateChatCompletionRequest';
9
9
  export type { CreateAnthropicChatCompletionParams } from './types/CreateAnthropicChatCompletionParams';
10
+ export type { CreateChatCompletionRequest } from './types/CreateChatCompletionRequest';
10
11
  export type { CreateGeminiChatCompletionParams } from './types/CreateGeminiChatCompletionParams';
11
12
  export type { CreateOpenAiChatCompletionParams } from './types/CreateOpenAiChatCompletionParams';
12
- export type { AnthropicModel } from './types/AnthropicModel';
13
13
  export type { GeminiModel } from './types/GeminiModel';
14
14
  export type { OpenAiModel } from './types/OpenAiModel';
15
15
  export { createChatCompletion } from './api/createChatCompletion';
16
16
  export type { CreateChatCompletionOptions } from './api/createChatCompletion';
17
17
  export { createChatCompletionStream } from './api/createChatCompletionStream';
18
18
  export { loadFency } from './loadFency';
19
+ export type { ApiError } from './types/ApiError';
19
20
  declare const _default: {
20
21
  loadFency: typeof loadFency;
21
22
  createChatCompletion: typeof createChatCompletion;
@@ -1,4 +1,3 @@
1
- export declare const isApiError: (response: unknown) => response is ApiError;
2
1
  export type ApiError = {
3
2
  code: string;
4
3
  message: string;
@@ -1,8 +1 @@
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
- };
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { ApiError } from '../types/ApiError';
2
+ export declare const parseApiError: (error: unknown) => ApiError | null;
@@ -0,0 +1,14 @@
1
+ export const parseApiError = (error) => {
2
+ if (typeof error === 'object' &&
3
+ error !== null &&
4
+ 'code' in error &&
5
+ 'message' in error &&
6
+ typeof error.code === 'string' &&
7
+ typeof error.message === 'string') {
8
+ return {
9
+ code: error.code,
10
+ message: error.message,
11
+ };
12
+ }
13
+ return null;
14
+ };
@@ -0,0 +1 @@
1
+ export declare const parseErrorBody: (response: Response) => Promise<unknown>;
@@ -0,0 +1,8 @@
1
+ export const parseErrorBody = async (response) => {
2
+ try {
3
+ return await response.json();
4
+ }
5
+ catch (error) {
6
+ return null;
7
+ }
8
+ };
@@ -0,0 +1,2 @@
1
+ import { ApiError } from '../types/ApiError';
2
+ export declare const parseNonOkResponse: (response: Response) => Promise<ApiError>;
@@ -0,0 +1,35 @@
1
+ import { parseApiError } from './parseApiError';
2
+ import { parseErrorBody } from './parseErrorBody';
3
+ export const parseNonOkResponse = async (response) => {
4
+ const error = await parseErrorBody(response);
5
+ const apiError = parseApiError(error);
6
+ if (apiError) {
7
+ return apiError;
8
+ }
9
+ if (apiError) {
10
+ return apiError;
11
+ }
12
+ if (response.status === 401) {
13
+ return {
14
+ code: 'InvalidCredentials',
15
+ message: `Invalid credentials provided.
16
+ Please make sure a valid publishable key is set.
17
+ You can find your publishable key in the Fency dashboard.
18
+ Take a look at https://docs.fency.ai/docs for more information.`,
19
+ };
20
+ }
21
+ if (response.status === 403) {
22
+ return {
23
+ code: 'ForbiddenOperation',
24
+ message: `Forbidden operation!
25
+ If your publishable key is correctly set, you might need to whitelist the domain you're using in the Fency dashboard.
26
+ If you're using localhost you also need to whitelist that.
27
+ Take a look at https://docs.fency.ai/docs for more information.
28
+ `,
29
+ };
30
+ }
31
+ return {
32
+ code: 'UnknownError',
33
+ message: `Unknown error!\n${response.status ? `Status code: ${response.status}\n` : ''}Response Body:${JSON.stringify(error, null, 2)}`.trim(),
34
+ };
35
+ };
@@ -1,7 +1,7 @@
1
1
  // Browser-compatible version utility
2
2
  // The version is set at build time and can be overridden if needed
3
3
  // Build-time version constant - this should be updated during the build process
4
- const SDK_VERSION = '0.1.39';
4
+ const SDK_VERSION = '0.1.41';
5
5
  // Allow runtime override if needed
6
6
  let versionOverride = null;
7
7
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fencyai/js",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
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": "0cab44f006aeac40bf6f333b083a3af7d361a099"
42
+ "gitHead": "a8e44437447dc885782eaf34c5df4cca71012e3f"
43
43
  }