openrouterlite-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,10 @@
1
+ import { BaseClient } from "../client.js";
2
+ import { ChatCompletionChunk, ChatCompletionRequest } from "../types/chat.js";
3
+ export declare class ChatCompletions {
4
+ private client;
5
+ constructor(client: BaseClient);
6
+ create(payload: ChatCompletionRequest, opts?: {
7
+ signal?: AbortSignal;
8
+ }): Promise<AsyncGenerator<ChatCompletionChunk | "[DONE]"> | Record<string, any>>;
9
+ }
10
+ //# sourceMappingURL=completions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"completions.d.ts","sourceRoot":"","sources":["../../src/chat/completions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAG9E,qBAAa,eAAe;IACd,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,UAAU;IAEhC,MAAM,CACV,OAAO,EAAE,qBAAqB,EAC9B,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GAC9B,OAAO,CACR,cAAc,CAAC,mBAAmB,GAAG,QAAQ,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CACrE;CAaF"}
@@ -0,0 +1,18 @@
1
+ import { streamResponse } from "./stream.js";
2
+ export class ChatCompletions {
3
+ client;
4
+ constructor(client) {
5
+ this.client = client;
6
+ }
7
+ async create(payload, opts) {
8
+ const res = await this.client.fetch("/chat/completions", {
9
+ method: "POST",
10
+ body: JSON.stringify(payload),
11
+ signal: opts?.signal,
12
+ });
13
+ if (!payload.stream) {
14
+ return res.json();
15
+ }
16
+ return streamResponse(res);
17
+ }
18
+ }
@@ -0,0 +1,3 @@
1
+ import { ChatCompletionChunk } from "../types/chat.js";
2
+ export declare function streamResponse(res: Response): AsyncGenerator<ChatCompletionChunk | "[DONE]", void, unknown>;
3
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/chat/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAGvD,wBAAuB,cAAc,CACnC,GAAG,EAAE,QAAQ,GACZ,cAAc,CAAC,mBAAmB,GAAG,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAI/D"}
@@ -0,0 +1,6 @@
1
+ import { parseSSE } from "../utils/sse.js";
2
+ export async function* streamResponse(res) {
3
+ for await (const chunk of parseSSE(res)) {
4
+ yield chunk;
5
+ }
6
+ }
@@ -0,0 +1,7 @@
1
+ export declare class BaseClient {
2
+ apiKey: string;
3
+ baseURL: string;
4
+ constructor(apiKey: string, baseURL?: string);
5
+ fetch(path: string, init: RequestInit): Promise<Response>;
6
+ }
7
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,qBAAa,UAAU;IAEZ,MAAM,EAAE,MAAM;IACd,OAAO;gBADP,MAAM,EAAE,MAAM,EACd,OAAO,SAA8B;IAG9C,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW;CAGtC"}
package/dist/client.js ADDED
@@ -0,0 +1,12 @@
1
+ import { apiFetch } from "./utils/fetch.js";
2
+ export class BaseClient {
3
+ apiKey;
4
+ baseURL;
5
+ constructor(apiKey, baseURL = "http://localhost:5513/api") {
6
+ this.apiKey = apiKey;
7
+ this.baseURL = baseURL;
8
+ }
9
+ fetch(path, init) {
10
+ return apiFetch(this.baseURL, this.apiKey, path, init);
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ import { ChatCompletions } from "./chat/completions.js";
2
+ export declare class OpenRouterLite {
3
+ chat: {
4
+ completions: ChatCompletions;
5
+ };
6
+ constructor(opts: {
7
+ apiKey: string;
8
+ baseURL?: string;
9
+ });
10
+ }
11
+ export * from "./types/chat.js";
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,qBAAa,cAAc;IACzB,IAAI,EAAE;QACJ,WAAW,EAAE,eAAe,CAAC;KAC9B,CAAC;gBAEU,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE;CAMvD;AAED,cAAc,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { BaseClient } from "./client.js";
2
+ import { ChatCompletions } from "./chat/completions.js";
3
+ export class OpenRouterLite {
4
+ chat;
5
+ constructor(opts) {
6
+ const client = new BaseClient(opts.apiKey, opts.baseURL);
7
+ this.chat = {
8
+ completions: new ChatCompletions(client),
9
+ };
10
+ }
11
+ }
12
+ export * from "./types/chat.js";
@@ -0,0 +1,19 @@
1
+ export type ChatMessage = {
2
+ role: "system" | "user" | "assistant";
3
+ content: string;
4
+ };
5
+ export type ChatCompletionRequest = {
6
+ model: string;
7
+ messages: ChatMessage[];
8
+ stream?: boolean;
9
+ max_tokens?: number;
10
+ };
11
+ export type ChatCompletionChunk = {
12
+ choices: Array<{
13
+ delta?: {
14
+ content?: string;
15
+ };
16
+ finish_reason?: "stop" | "credits" | "error";
17
+ }>;
18
+ };
19
+ //# sourceMappingURL=chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/types/chat.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;IACtC,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,KAAK,CAAC;QACb,KAAK,CAAC,EAAE;YACN,OAAO,CAAC,EAAE,MAAM,CAAC;SAClB,CAAC;QACF,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC;KAC9C,CAAC,CAAC;CACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ export type StreamChunk = {
2
+ choices: {
3
+ delta?: {
4
+ content?: string;
5
+ };
6
+ finish_reason?: string;
7
+ }[];
8
+ } | "[DONE]";
9
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/types/stream.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GACnB;IAAE,OAAO,EAAE;QAAE,KAAK,CAAC,EAAE;YAAE,OAAO,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAAE,GACvE,QAAQ,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function apiFetch(baseURL: string, apiKey: string, path: string, init: RequestInit): Promise<Response>;
2
+ //# sourceMappingURL=fetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA,wBAAsB,QAAQ,CAC5B,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,qBAUlB"}
@@ -0,0 +1,10 @@
1
+ export async function apiFetch(baseURL, apiKey, path, init) {
2
+ return fetch(`${baseURL}${path}`, {
3
+ ...init,
4
+ headers: {
5
+ Authorization: `Bearer ${apiKey}`,
6
+ "Content-Type": "application/json",
7
+ ...(init.headers || {}),
8
+ },
9
+ });
10
+ }
@@ -0,0 +1,2 @@
1
+ export declare function parseSSE(res: Response): AsyncGenerator<any, void, unknown>;
2
+ //# sourceMappingURL=sse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/utils/sse.ts"],"names":[],"mappings":"AAAA,wBAAuB,QAAQ,CAAC,GAAG,EAAE,QAAQ,sCA6B5C"}
@@ -0,0 +1,25 @@
1
+ export async function* parseSSE(res) {
2
+ if (!res.body)
3
+ throw new Error("Response body is empty");
4
+ const reader = res.body.getReader();
5
+ const decoder = new TextDecoder();
6
+ let buffer = "";
7
+ while (true) {
8
+ const { value, done } = await reader.read();
9
+ if (done)
10
+ break;
11
+ buffer += decoder.decode(value, { stream: true });
12
+ const parts = buffer.split("\n\n");
13
+ buffer = parts.pop() || "";
14
+ for (const part of parts) {
15
+ if (!part.startsWith("data: "))
16
+ continue;
17
+ const data = part.slice(6).trim();
18
+ if (data === "[DONE]") {
19
+ yield "[DONE]";
20
+ return;
21
+ }
22
+ yield JSON.parse(data);
23
+ }
24
+ }
25
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "openrouterlite-sdk",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "exports": {
7
+ ".": "./dist/index.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "clean": "rimraf dist",
15
+ "prepublishOnly": "npm run clean && npm run build"
16
+ },
17
+ "devDependencies": {
18
+ "rimraf": "^6.1.3",
19
+ "typescript": "^5.9.3"
20
+ }
21
+ }