@infrgate/botchain-sdk 0.1.0

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.
@@ -0,0 +1,91 @@
1
+ interface ClientOptions {
2
+ apiKey?: string;
3
+ baseURL?: string;
4
+ timeout?: number;
5
+ headers?: Record<string, string>;
6
+ }
7
+ type Role = "system" | "user" | "assistant" | "tool";
8
+ interface ChatMessage {
9
+ role: Role;
10
+ content: string;
11
+ name?: string;
12
+ }
13
+ interface ChatCompletionCreateParamsBase {
14
+ model: string;
15
+ messages: ChatMessage[];
16
+ temperature?: number;
17
+ top_p?: number;
18
+ max_tokens?: number;
19
+ stop?: string | string[];
20
+ }
21
+ interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase {
22
+ stream?: false;
23
+ }
24
+ interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase {
25
+ stream: true;
26
+ }
27
+ type ChatCompletionCreateParams = ChatCompletionCreateParamsNonStreaming | ChatCompletionCreateParamsStreaming;
28
+ interface ChatCompletionChoice {
29
+ index: number;
30
+ message: ChatMessage;
31
+ finish_reason: string | null;
32
+ }
33
+ interface ChatCompletionUsage {
34
+ prompt_tokens: number;
35
+ completion_tokens: number;
36
+ total_tokens: number;
37
+ }
38
+ interface ChatCompletion {
39
+ id: string;
40
+ object: "chat.completion";
41
+ created: number;
42
+ model: string;
43
+ choices: ChatCompletionChoice[];
44
+ usage?: ChatCompletionUsage;
45
+ }
46
+ interface ChatCompletionChunkDelta {
47
+ role?: Role;
48
+ content?: string;
49
+ }
50
+ interface ChatCompletionChunkChoice {
51
+ index: number;
52
+ delta: ChatCompletionChunkDelta;
53
+ finish_reason: string | null;
54
+ }
55
+ interface ChatCompletionChunk {
56
+ id: string;
57
+ object: "chat.completion.chunk";
58
+ created: number;
59
+ model: string;
60
+ choices: ChatCompletionChunkChoice[];
61
+ }
62
+ declare class InfrgateError extends Error {
63
+ readonly status: number;
64
+ readonly body: unknown;
65
+ constructor(message: string, status: number, body: unknown);
66
+ }
67
+
68
+ declare class Chat {
69
+ private readonly client;
70
+ constructor(client: {
71
+ request: (path: string, options: RequestInit) => Promise<Response>;
72
+ });
73
+ completions: {
74
+ create: {
75
+ (params: ChatCompletionCreateParamsStreaming): Promise<AsyncIterable<ChatCompletionChunk>>;
76
+ (params: ChatCompletionCreateParamsNonStreaming): Promise<ChatCompletion>;
77
+ (params: ChatCompletionCreateParams): Promise<ChatCompletion | AsyncIterable<ChatCompletionChunk>>;
78
+ };
79
+ };
80
+ }
81
+
82
+ declare class Infrgate {
83
+ readonly apiKey: string;
84
+ readonly baseURL: string;
85
+ readonly timeout: number;
86
+ readonly chat: Chat;
87
+ constructor(options?: ClientOptions);
88
+ request(path: string, init: RequestInit): Promise<Response>;
89
+ }
90
+
91
+ export { type ChatCompletion, type ChatCompletionChoice, type ChatCompletionChunk, type ChatCompletionChunkChoice, type ChatCompletionChunkDelta, type ChatCompletionCreateParams, type ChatCompletionCreateParamsBase, type ChatCompletionCreateParamsNonStreaming, type ChatCompletionCreateParamsStreaming, type ChatCompletionUsage, type ChatMessage, type ClientOptions, Infrgate, InfrgateError, type Role, Infrgate as default };
@@ -0,0 +1,91 @@
1
+ interface ClientOptions {
2
+ apiKey?: string;
3
+ baseURL?: string;
4
+ timeout?: number;
5
+ headers?: Record<string, string>;
6
+ }
7
+ type Role = "system" | "user" | "assistant" | "tool";
8
+ interface ChatMessage {
9
+ role: Role;
10
+ content: string;
11
+ name?: string;
12
+ }
13
+ interface ChatCompletionCreateParamsBase {
14
+ model: string;
15
+ messages: ChatMessage[];
16
+ temperature?: number;
17
+ top_p?: number;
18
+ max_tokens?: number;
19
+ stop?: string | string[];
20
+ }
21
+ interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase {
22
+ stream?: false;
23
+ }
24
+ interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase {
25
+ stream: true;
26
+ }
27
+ type ChatCompletionCreateParams = ChatCompletionCreateParamsNonStreaming | ChatCompletionCreateParamsStreaming;
28
+ interface ChatCompletionChoice {
29
+ index: number;
30
+ message: ChatMessage;
31
+ finish_reason: string | null;
32
+ }
33
+ interface ChatCompletionUsage {
34
+ prompt_tokens: number;
35
+ completion_tokens: number;
36
+ total_tokens: number;
37
+ }
38
+ interface ChatCompletion {
39
+ id: string;
40
+ object: "chat.completion";
41
+ created: number;
42
+ model: string;
43
+ choices: ChatCompletionChoice[];
44
+ usage?: ChatCompletionUsage;
45
+ }
46
+ interface ChatCompletionChunkDelta {
47
+ role?: Role;
48
+ content?: string;
49
+ }
50
+ interface ChatCompletionChunkChoice {
51
+ index: number;
52
+ delta: ChatCompletionChunkDelta;
53
+ finish_reason: string | null;
54
+ }
55
+ interface ChatCompletionChunk {
56
+ id: string;
57
+ object: "chat.completion.chunk";
58
+ created: number;
59
+ model: string;
60
+ choices: ChatCompletionChunkChoice[];
61
+ }
62
+ declare class InfrgateError extends Error {
63
+ readonly status: number;
64
+ readonly body: unknown;
65
+ constructor(message: string, status: number, body: unknown);
66
+ }
67
+
68
+ declare class Chat {
69
+ private readonly client;
70
+ constructor(client: {
71
+ request: (path: string, options: RequestInit) => Promise<Response>;
72
+ });
73
+ completions: {
74
+ create: {
75
+ (params: ChatCompletionCreateParamsStreaming): Promise<AsyncIterable<ChatCompletionChunk>>;
76
+ (params: ChatCompletionCreateParamsNonStreaming): Promise<ChatCompletion>;
77
+ (params: ChatCompletionCreateParams): Promise<ChatCompletion | AsyncIterable<ChatCompletionChunk>>;
78
+ };
79
+ };
80
+ }
81
+
82
+ declare class Infrgate {
83
+ readonly apiKey: string;
84
+ readonly baseURL: string;
85
+ readonly timeout: number;
86
+ readonly chat: Chat;
87
+ constructor(options?: ClientOptions);
88
+ request(path: string, init: RequestInit): Promise<Response>;
89
+ }
90
+
91
+ export { type ChatCompletion, type ChatCompletionChoice, type ChatCompletionChunk, type ChatCompletionChunkChoice, type ChatCompletionChunkDelta, type ChatCompletionCreateParams, type ChatCompletionCreateParamsBase, type ChatCompletionCreateParamsNonStreaming, type ChatCompletionCreateParamsStreaming, type ChatCompletionUsage, type ChatMessage, type ClientOptions, Infrgate, InfrgateError, type Role, Infrgate as default };
package/dist/index.js ADDED
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Infrgate: () => Infrgate,
24
+ InfrgateError: () => InfrgateError,
25
+ default: () => index_default
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/types.ts
30
+ var InfrgateError = class extends Error {
31
+ constructor(message, status, body) {
32
+ super(message);
33
+ this.status = status;
34
+ this.body = body;
35
+ this.name = "InfrgateError";
36
+ }
37
+ status;
38
+ body;
39
+ };
40
+
41
+ // src/streaming.ts
42
+ async function* parseServerSentEvents(response) {
43
+ if (!response.body) {
44
+ throw new Error("Response body is null, cannot stream chunks.");
45
+ }
46
+ const reader = response.body.getReader();
47
+ const decoder = new TextDecoder("utf-8");
48
+ let buffer = "";
49
+ try {
50
+ while (true) {
51
+ const { done, value } = await reader.read();
52
+ if (done) break;
53
+ buffer += decoder.decode(value, { stream: true });
54
+ const lines = buffer.split("\n");
55
+ buffer = lines.pop() || "";
56
+ for (const line of lines) {
57
+ const trimmed = line.trim();
58
+ if (!trimmed || trimmed.startsWith(":")) continue;
59
+ if (trimmed.startsWith("data: ")) {
60
+ const payload = trimmed.slice(6).trim();
61
+ if (payload === "[DONE]") {
62
+ return;
63
+ }
64
+ try {
65
+ const parsed = JSON.parse(payload);
66
+ yield parsed;
67
+ } catch {
68
+ }
69
+ }
70
+ }
71
+ }
72
+ } finally {
73
+ reader.releaseLock();
74
+ }
75
+ }
76
+
77
+ // src/resources/chat.ts
78
+ var Chat = class {
79
+ constructor(client) {
80
+ this.client = client;
81
+ }
82
+ client;
83
+ completions = {
84
+ create: (async (params) => {
85
+ const response = await this.client.request("/chat/completions", {
86
+ method: "POST",
87
+ headers: { "Content-Type": "application/json" },
88
+ body: JSON.stringify(params)
89
+ });
90
+ if (!response.ok) {
91
+ let errBody;
92
+ try {
93
+ errBody = await response.json();
94
+ } catch {
95
+ errBody = await response.text();
96
+ }
97
+ throw new InfrgateError(
98
+ `Infrgate API Error (${response.status}): ${response.statusText}`,
99
+ response.status,
100
+ errBody
101
+ );
102
+ }
103
+ if (params.stream) {
104
+ return parseServerSentEvents(response);
105
+ }
106
+ return await response.json();
107
+ })
108
+ };
109
+ };
110
+
111
+ // src/index.ts
112
+ var Infrgate = class {
113
+ apiKey;
114
+ baseURL;
115
+ timeout;
116
+ chat;
117
+ constructor(options = {}) {
118
+ this.apiKey = options.apiKey || process.env.INFRGATE_API_KEY || "";
119
+ this.baseURL = (options.baseURL || process.env.INFRGATE_BASE_URL || "https://api.infrgate.io/v1").replace(/\/+$/, "");
120
+ this.timeout = options.timeout || 6e4;
121
+ this.chat = new Chat(this);
122
+ }
123
+ async request(path, init) {
124
+ const url = `${this.baseURL}${path.startsWith("/") ? path : `/${path}`}`;
125
+ const headers = new Headers(init.headers);
126
+ if (this.apiKey) {
127
+ headers.set("Authorization", `Bearer ${this.apiKey}`);
128
+ }
129
+ const controller = new AbortController();
130
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
131
+ try {
132
+ return await fetch(url, {
133
+ ...init,
134
+ headers,
135
+ signal: controller.signal
136
+ });
137
+ } finally {
138
+ clearTimeout(timeoutId);
139
+ }
140
+ }
141
+ };
142
+ var index_default = Infrgate;
143
+ // Annotate the CommonJS export names for ESM import in node:
144
+ 0 && (module.exports = {
145
+ Infrgate,
146
+ InfrgateError
147
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ // src/types.ts
2
+ var InfrgateError = class extends Error {
3
+ constructor(message, status, body) {
4
+ super(message);
5
+ this.status = status;
6
+ this.body = body;
7
+ this.name = "InfrgateError";
8
+ }
9
+ status;
10
+ body;
11
+ };
12
+
13
+ // src/streaming.ts
14
+ async function* parseServerSentEvents(response) {
15
+ if (!response.body) {
16
+ throw new Error("Response body is null, cannot stream chunks.");
17
+ }
18
+ const reader = response.body.getReader();
19
+ const decoder = new TextDecoder("utf-8");
20
+ let buffer = "";
21
+ try {
22
+ while (true) {
23
+ const { done, value } = await reader.read();
24
+ if (done) break;
25
+ buffer += decoder.decode(value, { stream: true });
26
+ const lines = buffer.split("\n");
27
+ buffer = lines.pop() || "";
28
+ for (const line of lines) {
29
+ const trimmed = line.trim();
30
+ if (!trimmed || trimmed.startsWith(":")) continue;
31
+ if (trimmed.startsWith("data: ")) {
32
+ const payload = trimmed.slice(6).trim();
33
+ if (payload === "[DONE]") {
34
+ return;
35
+ }
36
+ try {
37
+ const parsed = JSON.parse(payload);
38
+ yield parsed;
39
+ } catch {
40
+ }
41
+ }
42
+ }
43
+ }
44
+ } finally {
45
+ reader.releaseLock();
46
+ }
47
+ }
48
+
49
+ // src/resources/chat.ts
50
+ var Chat = class {
51
+ constructor(client) {
52
+ this.client = client;
53
+ }
54
+ client;
55
+ completions = {
56
+ create: (async (params) => {
57
+ const response = await this.client.request("/chat/completions", {
58
+ method: "POST",
59
+ headers: { "Content-Type": "application/json" },
60
+ body: JSON.stringify(params)
61
+ });
62
+ if (!response.ok) {
63
+ let errBody;
64
+ try {
65
+ errBody = await response.json();
66
+ } catch {
67
+ errBody = await response.text();
68
+ }
69
+ throw new InfrgateError(
70
+ `Infrgate API Error (${response.status}): ${response.statusText}`,
71
+ response.status,
72
+ errBody
73
+ );
74
+ }
75
+ if (params.stream) {
76
+ return parseServerSentEvents(response);
77
+ }
78
+ return await response.json();
79
+ })
80
+ };
81
+ };
82
+
83
+ // src/index.ts
84
+ var Infrgate = class {
85
+ apiKey;
86
+ baseURL;
87
+ timeout;
88
+ chat;
89
+ constructor(options = {}) {
90
+ this.apiKey = options.apiKey || process.env.INFRGATE_API_KEY || "";
91
+ this.baseURL = (options.baseURL || process.env.INFRGATE_BASE_URL || "https://api.infrgate.io/v1").replace(/\/+$/, "");
92
+ this.timeout = options.timeout || 6e4;
93
+ this.chat = new Chat(this);
94
+ }
95
+ async request(path, init) {
96
+ const url = `${this.baseURL}${path.startsWith("/") ? path : `/${path}`}`;
97
+ const headers = new Headers(init.headers);
98
+ if (this.apiKey) {
99
+ headers.set("Authorization", `Bearer ${this.apiKey}`);
100
+ }
101
+ const controller = new AbortController();
102
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
103
+ try {
104
+ return await fetch(url, {
105
+ ...init,
106
+ headers,
107
+ signal: controller.signal
108
+ });
109
+ } finally {
110
+ clearTimeout(timeoutId);
111
+ }
112
+ }
113
+ };
114
+ var index_default = Infrgate;
115
+ export {
116
+ Infrgate,
117
+ InfrgateError,
118
+ index_default as default
119
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@infrgate/botchain-sdk",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript SDK for Infrgate AI Gateway on BOT Chain",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.js"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "keywords": ["ai", "agents", "botchain", "llm-gateway", "infrgate"],
24
+ "author": "",
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "tsup": "^8.0.2",
28
+ "typescript": "^5.4.0"
29
+ },
30
+ "engines": {
31
+ "node": ">=18.0.0"
32
+ }
33
+ }