svector-sdk
Version:
Official JavaScript and TypeScript SDK for accessing SVECTOR APIs.
120 lines (119 loc) • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatCompletions = void 0;
class ChatCompletions {
constructor(client) {
this.client = client;
}
async create(params, options) {
if (params.stream) {
throw new Error('Use createStream() for streaming responses');
}
const processedParams = this.processSystemPrompts(params);
return this.client.request('POST', '/api/chat/completions', processedParams, options);
}
processSystemPrompts(params) {
const messages = [...params.messages];
const processedMessages = messages.map(message => {
if (message.role === 'system') {
return {
...message,
content: this.normalizeSystemContent(message.content)
};
}
return message;
});
return {
...params,
messages: processedMessages
};
}
normalizeSystemContent(content) {
if (typeof content === 'string') {
return content;
}
if (Array.isArray(content)) {
return content;
}
if (typeof content === 'function') {
try {
const result = content();
return typeof result === 'string' ? result : String(result);
}
catch (error) {
console.warn('SVECTOR: Failed to execute system content function:', error);
return 'You are a helpful assistant.';
}
}
if (content && typeof content === 'object') {
if (content.content)
return String(content.content);
if (content.text)
return String(content.text);
if (content.value)
return String(content.value);
try {
return JSON.stringify(content);
}
catch (error) {
return String(content);
}
}
return content ? String(content) : 'You are a helpful assistant.';
}
async createStream(params, options) {
const processedParams = this.processSystemPrompts(params);
const response = await this.client.requestStream('POST', '/api/chat/completions', { ...processedParams, stream: true }, options);
return this.parseSSEStream(response);
}
async createWithResponse(params, options) {
if (params.stream) {
throw new Error('Use createStreamWithResponse() for streaming responses');
}
return this.client.withResponse('POST', '/api/chat/completions', params, options);
}
async createStreamWithResponse(params, options) {
const response = await this.client.requestStream('POST', '/api/chat/completions', { ...params, stream: true }, options);
return {
data: this.parseSSEStream(response),
response
};
}
async *parseSSEStream(response) {
if (!response.body) {
throw new Error('No response body for streaming');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
try {
while (true) {
const { done, value } = await reader.read();
if (done)
break;
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 === 'data: [DONE]')
continue;
if (trimmed.startsWith('data: ')) {
try {
const data = trimmed.slice(6);
const parsed = JSON.parse(data);
yield parsed;
}
catch (e) {
continue;
}
}
}
}
}
finally {
reader.releaseLock();
}
}
}
exports.ChatCompletions = ChatCompletions;