reliable-requests 0.0.0-stage → 0.2.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,69 @@
1
+ export class InvalidStreamEventError extends Error {
2
+ constructor(protocol, field) {
3
+ super('Invalid ' + (protocol === 'openai' ? 'OpenAI-compatible' : 'Ollama') + ' stream event' + (field ? ': ' + field : ''));
4
+ this.name = 'InvalidStreamEventError';
5
+ this.code = 'INVALID_STREAM_EVENT';
6
+ this.field = field;
7
+ }
8
+ }
9
+
10
+ const isObject = value => value !== null && typeof value === 'object' && !Array.isArray(value);
11
+ const isOptionalText = value => value === undefined || value === null || typeof value === 'string';
12
+
13
+ function requireField(condition, protocol, field) {
14
+ if (!condition) throw new InvalidStreamEventError(protocol, field);
15
+ }
16
+
17
+ export function parseEvent(text, protocol) {
18
+ let event;
19
+ try {
20
+ event = JSON.parse(text);
21
+ } catch {
22
+ throw new InvalidStreamEventError(protocol);
23
+ }
24
+ requireField(isObject(event), protocol, 'event');
25
+ return event;
26
+ }
27
+
28
+ export function openaiChoice(event) {
29
+ const protocol = 'openai';
30
+ if (event.error != null) {
31
+ requireField(typeof event.error === 'string' || isObject(event.error), protocol, 'error');
32
+ if (isObject(event.error)) requireField(isOptionalText(event.error.message), protocol, 'error.message');
33
+ throw new Error(typeof event.error === 'string' ? event.error : event.error.message || 'Model stream error');
34
+ }
35
+ if (event.choices === undefined) return undefined;
36
+ requireField(Array.isArray(event.choices), protocol, 'choices');
37
+ requireField(event.choices.length <= 1, protocol, 'choices (only one generation is supported)');
38
+ if (!event.choices.length) return undefined;
39
+ const choice = event.choices[0];
40
+ requireField(isObject(choice), protocol, 'choices[]');
41
+ requireField(choice.index === undefined || choice.index === 0, protocol, 'choice.index (only index 0 is supported)');
42
+ requireField(isOptionalText(choice.finish_reason), protocol, 'finish_reason');
43
+ const { delta } = choice;
44
+ if (delta != null) {
45
+ requireField(isObject(delta), protocol, 'delta');
46
+ requireField(isOptionalText(delta.content), protocol, 'delta.content');
47
+ requireField(isOptionalText(delta.role), protocol, 'delta.role');
48
+ requireField(delta.tool_calls == null || Array.isArray(delta.tool_calls), protocol, 'delta.tool_calls');
49
+ requireField(delta.function_call == null || isObject(delta.function_call), protocol, 'delta.function_call');
50
+ }
51
+ return choice;
52
+ }
53
+
54
+ export function ollamaMessage(event) {
55
+ const protocol = 'ollama';
56
+ if (event.error != null) {
57
+ requireField(typeof event.error === 'string', protocol, 'error');
58
+ throw new Error(event.error);
59
+ }
60
+ requireField(event.done === undefined || typeof event.done === 'boolean', protocol, 'done');
61
+ requireField(isOptionalText(event.done_reason), protocol, 'done_reason');
62
+ const { message } = event;
63
+ if (message == null) return undefined;
64
+ requireField(isObject(message), protocol, 'message');
65
+ requireField(isOptionalText(message.content), protocol, 'message.content');
66
+ requireField(isOptionalText(message.role), protocol, 'message.role');
67
+ requireField(message.tool_calls == null || Array.isArray(message.tool_calls), protocol, 'message.tool_calls');
68
+ return message;
69
+ }