Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

172 changes: 167 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,86 @@ export interface Choice {
finish_reason: string | null;
}

export interface SendResponse {
export class SendResponse {
choices: Choice[];
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};

constructor(
choices: Choice[],
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
) {
this.choices = choices;
this.usage = usage;
}

get text(): string | null {
if (this.choices[0]?.message?.content) {
return this.choices[0].message.content;
}
return null;
}

get message() {
return this.choices[0]?.message ?? null;
}

get finishReason(): string | null {
return this.choices[0]?.finish_reason ?? null;
}

get toolCalls(): ToolCall[] | null {
return this.choices[0]?.message?.tool_calls ?? null;
}
}

// Streaming types
export interface StreamDelta {
role?: string;
content?: string;
tool_calls?: ToolCall[];
}

export interface StreamChoice {
index: number;
delta: StreamDelta;
finish_reason?: string | null;
}

export class StreamChunk {
choices: StreamChoice[];

constructor(choices: StreamChoice[]) {
this.choices = choices;
}

get text(): string | null {
if (this.choices[0]?.delta?.content) {
return this.choices[0].delta.content;
}
return null;
}

get role(): string | null {
if (this.choices[0]?.delta?.role) {
return this.choices[0].delta.role;
}
return null;
}

get finishReason(): string | null {
if (this.choices[0]?.finish_reason) {
return this.choices[0].finish_reason;
}
return null;
}
}

export interface EdgeeConfig {
Expand Down Expand Up @@ -123,11 +196,100 @@ export default class Edgee {
body: JSON.stringify(body),
});

const data = (await res.json()) as SendResponse;
if (!res.ok) {
const errorBody = await res.text();
throw new Error(`API error ${res.status}: ${errorBody}`);
}

const data = await res.json() as {
choices: Choice[];
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
};

return new SendResponse(data.choices, data.usage);
}

private async *_handleStreamingResponse(
url: string,
body: Record<string, unknown>
): AsyncGenerator<StreamChunk> {
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify(body),
});

if (!res.ok) {
const errorBody = await res.text();
throw new Error(`API error ${res.status}: ${errorBody}`);
}

if (!res.body) {
throw new Error("Response body is null");
}

const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";

while (true) {
const { done, value } = await reader.read();
if (done) break;

return {
choices: data.choices,
usage: data.usage,
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() || "";

for (const line of lines) {
const trimmed = line.trim();
if (trimmed === "" || !trimmed.startsWith("data: ")) {
continue;
}

const data = trimmed.slice(6);
if (data === "[DONE]") {
return;
}

try {
const parsed = JSON.parse(data);
yield new StreamChunk(parsed.choices);
} catch {
// Skip malformed JSON
continue;
}
}
}
}

async *stream(
model: string,
input: string | InputObject
): AsyncGenerator<StreamChunk> {
const body: Record<string, unknown> = {
model,
messages:
typeof input === "string"
? [{ role: "user", content: input }]
: input.messages,
stream: true,
};

if (typeof input !== "string") {
if (input.tools) body.tools = input.tools;
if (input.tool_choice) body.tool_choice = input.tool_choice;
}

yield* this._handleStreamingResponse(
`${this.baseUrl}/v1/chat/completions`,
body
);
}
}
Loading