UNPKG

@hashbrownai/writer

Version:
128 lines 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.text = text; const tslib_1 = require("tslib"); /* eslint-disable @typescript-eslint/no-explicit-any */ const writer_sdk_1 = require("writer-sdk"); const core_1 = require("@hashbrownai/core"); /** * Streams text from the Writer API. * * @param options - The options for the stream. * @returns An async iterable of Uint8Arrays. */ function text(options) { return tslib_1.__asyncGenerator(this, arguments, function* text_1() { var _a, e_1, _b, _c; const { apiKey, request, transformRequestOptions } = options; const writer = new writer_sdk_1.default({ apiKey, }); try { const baseRequestOptions = { stream: true, model: request.model, tool_choice: request.toolChoice ? { value: request.toolChoice } : undefined, response_format: request.responseFormat ? { type: 'json_schema', json_schema: { strict: true, name: 'schema', description: '', schema: request.responseFormat, }, } : undefined, tools: request.tools && request.tools.length > 0 ? request.tools.map((tool) => ({ type: 'function', function: { name: tool.name, description: tool.description, parameters: tool.parameters, strict: true, }, })) : undefined, messages: [ { role: 'system', content: request.system, }, ...request.messages.map((message) => { switch (message.role) { case 'user': return { role: 'user', content: message.content, }; case 'assistant': return { role: 'assistant', content: message.content, tool_calls: message.toolCalls && message.toolCalls.length > 0 ? message.toolCalls.map((toolCall) => (Object.assign(Object.assign({}, toolCall), { type: 'function', function: Object.assign(Object.assign({}, toolCall.function), { arguments: JSON.stringify(toolCall.function.arguments) }) }))) : undefined, }; case 'tool': return { role: 'tool', content: JSON.stringify(message.content), tool_call_id: message.toolCallId, }; default: throw new Error(`Unsupported message role: ${message.role}`); } }), ], }; const requestOptions = transformRequestOptions ? yield tslib_1.__await(transformRequestOptions(baseRequestOptions)) : baseRequestOptions; const stream = yield tslib_1.__await(writer.chat.chat(Object.assign(Object.assign({}, requestOptions), { stream: true }))); try { for (var _d = true, stream_1 = tslib_1.__asyncValues(stream), stream_1_1; stream_1_1 = yield tslib_1.__await(stream_1.next()), _a = stream_1_1.done, !_a; _d = true) { _c = stream_1_1.value; _d = false; const chunk = _c; const chunkMessage = { choices: chunk.choices.map((choice) => { var _a; return ({ index: choice.index, delta: Object.assign(Object.assign({}, choice.delta), { toolCalls: (_a = choice.delta.tool_calls) !== null && _a !== void 0 ? _a : undefined }), finishReason: choice.finish_reason, }); }), }; yield yield tslib_1.__await((0, core_1.encodeFrame)({ type: 'chunk', chunk: chunkMessage, })); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = stream_1.return)) yield tslib_1.__await(_b.call(stream_1)); } finally { if (e_1) throw e_1.error; } } } catch (error) { yield yield tslib_1.__await((0, core_1.encodeFrame)({ type: 'error', error: error instanceof Error ? error.message : String(error), })); } finally { yield yield tslib_1.__await((0, core_1.encodeFrame)({ type: 'finish', })); } }); } //# sourceMappingURL=text.fn.js.map