@fencyai/js 0.1.38 → 0.1.40

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,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.37';
4
+ const SDK_VERSION = '0.1.39';
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.38",
3
+ "version": "0.1.40",
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": "13bb1e815dadeb5800be355190ce8486121db136"
42
+ "gitHead": "0cab44f006aeac40bf6f333b083a3af7d361a099"
43
43
  }