@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.
- package/lib/api/createChatCompletion.d.ts +9 -1
- package/lib/api/createChatCompletion.js +27 -12
- package/lib/api/createChatCompletionStream.d.ts +9 -1
- package/lib/api/createChatCompletionStream.js +26 -11
- package/lib/types/ApiError.d.ts +5 -0
- package/lib/types/ApiError.js +8 -0
- package/lib/types/CreateAnthropicChatCompletionParams.d.ts +3 -0
- package/lib/types/CreateGeminiChatCompletionParams.d.ts +3 -0
- package/lib/types/CreateOpenAiChatCompletionParams.d.ts +2 -0
- package/lib/utils/version.js +1 -1
- package/package.json +2 -2
|
@@ -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<
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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<
|
|
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
|
-
|
|
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
|
-
|
|
32
|
+
return {
|
|
33
|
+
type: 'error',
|
|
34
|
+
error: {
|
|
35
|
+
code: 'InvalidResponse',
|
|
36
|
+
message: `Invalid stream response: ${JSON.stringify(stream)}`,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
27
39
|
}
|
|
28
|
-
return
|
|
40
|
+
return {
|
|
41
|
+
type: 'success',
|
|
42
|
+
stream: stream,
|
|
43
|
+
};
|
|
29
44
|
}
|
package/lib/utils/version.js
CHANGED
|
@@ -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.
|
|
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.
|
|
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": "
|
|
42
|
+
"gitHead": "0cab44f006aeac40bf6f333b083a3af7d361a099"
|
|
43
43
|
}
|