@arthovlabs/liber 0.0.0-stage → 0.5.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.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +348 -2
  3. package/bin/liber +706 -0
  4. package/bin/liber-tui +244 -0
  5. package/bin/liber-tui.py +825 -0
  6. package/bin/liberd-entry +17 -0
  7. package/liber.yaml +84 -0
  8. package/package.json +43 -4
  9. package/resources/icons/appIcon.png +0 -0
  10. package/resources/icons/appIcon.svg +20 -0
  11. package/resources/icons/appIcon@512.png +0 -0
  12. package/resources/icons/atom-transparent-256.png +0 -0
  13. package/resources/icons/atom-transparent.png +0 -0
  14. package/resources/icons/atom-transparent.svg +19 -0
  15. package/resources/icons/make-atom.js +168 -0
  16. package/src/cli/ui.js +520 -0
  17. package/src/daemon/core/auth.js +217 -0
  18. package/src/daemon/core/automations.js +130 -0
  19. package/src/daemon/core/budget.js +145 -0
  20. package/src/daemon/core/circuit-breaker.js +31 -0
  21. package/src/daemon/core/config-apply.js +223 -0
  22. package/src/daemon/core/connectors.js +131 -0
  23. package/src/daemon/core/engine.js +1029 -0
  24. package/src/daemon/core/evidence.js +306 -0
  25. package/src/daemon/core/executor.js +10 -0
  26. package/src/daemon/core/grounding.js +420 -0
  27. package/src/daemon/core/injection.js +24 -0
  28. package/src/daemon/core/journal.js +81 -0
  29. package/src/daemon/core/liber-soul.js +157 -0
  30. package/src/daemon/core/lock.js +32 -0
  31. package/src/daemon/core/memory.js +106 -0
  32. package/src/daemon/core/model-switch.js +58 -0
  33. package/src/daemon/core/policy.js +102 -0
  34. package/src/daemon/core/providers.js +503 -0
  35. package/src/daemon/core/registry.js +356 -0
  36. package/src/daemon/core/secrets.js +35 -0
  37. package/src/daemon/core/skills.js +264 -0
  38. package/src/daemon/core/tools/browser-tool.js +280 -0
  39. package/src/daemon/core/tools/file-tools.js +277 -0
  40. package/src/daemon/core/tools/memory-tool.js +127 -0
  41. package/src/daemon/core/tools/skill-tool.js +44 -0
  42. package/src/daemon/core/tools/task-tool.js +41 -0
  43. package/src/daemon/core/tools/terminal-tool.js +449 -0
  44. package/src/daemon/core/tools/todo-tool.js +100 -0
  45. package/src/daemon/core/tools/web-tool.js +181 -0
  46. package/src/daemon/core/trace.js +40 -0
  47. package/src/daemon/core/validate.js +105 -0
  48. package/src/daemon/core/vault.js +158 -0
  49. package/src/daemon/core/verifier.js +98 -0
  50. package/src/daemon/core/voice.js +83 -0
  51. package/src/daemon/core/workspace.js +199 -0
  52. package/src/daemon/liberd.js +1343 -0
  53. package/src/daemon/llm/adapters/anthropic-adapter.js +190 -0
  54. package/src/daemon/llm/adapters/gemini-adapter.js +148 -0
  55. package/src/daemon/llm/adapters/ollama-adapter.js +74 -0
  56. package/src/daemon/llm/adapters/openai-adapter.js +148 -0
  57. package/src/daemon/llm/base.js +31 -0
  58. package/src/daemon/llm/manager.js +134 -0
  59. package/src/utils/config-loader.js +109 -0
  60. package/src/utils/logger.js +56 -0
@@ -0,0 +1,190 @@
1
+ // src/daemon/llm/adapters/anthropic-adapter.js — Anthropic Messages API.
2
+ // Translates Liber's OpenAI-shaped conversation (system/user/assistant/tool roles,
3
+ // tool_calls blocks) into Anthropic content blocks and back.
4
+ const LLMAdapter = require('../base');
5
+
6
+ class AnthropicAdapter extends LLMAdapter {
7
+ constructor(config) {
8
+ super(config);
9
+ if (!this.config.endpoint) this.config.endpoint = 'https://api.anthropic.com';
10
+ }
11
+
12
+ buildMessages(messages) {
13
+ let system = '';
14
+ const out = [];
15
+ let lastRole = null;
16
+
17
+ const pushMerged = (role, content) => {
18
+ if (role === lastRole) {
19
+ const prev = out[out.length - 1];
20
+ if (typeof prev.content === 'string') prev.content += '\n\n' + content;
21
+ else if (Array.isArray(prev.content)) prev.content.push({ type: 'text', text: content });
22
+ } else {
23
+ out.push({ role, content });
24
+ lastRole = role;
25
+ }
26
+ };
27
+
28
+ for (const m of messages) {
29
+ if (m.role === 'system') {
30
+ system += (system ? '\n\n' : '') + String(m.content ?? '');
31
+ continue;
32
+ }
33
+ if (Array.isArray(m.tool_calls) && m.tool_calls.length) {
34
+ const blocks = m.tool_calls.map(tc => ({
35
+ type: 'tool_use',
36
+ id: tc.id || `toolu_${Math.random().toString(36).slice(2, 10)}`,
37
+ name: tc.function?.name || 'tool',
38
+ input: (() => {
39
+ try { return JSON.parse(tc.function?.arguments || '{}'); }
40
+ catch { return { raw: tc.function?.arguments || '' }; }
41
+ })()
42
+ }));
43
+ if (String(m.content ?? '').trim()) {
44
+ blocks.unshift({ type: 'text', text: String(m.content) });
45
+ }
46
+ pushMerged('assistant', blocks);
47
+ continue;
48
+ }
49
+ if (m.role === 'tool') {
50
+ pushMerged('user', [{ type: 'tool_result', tool_use_id: m.tool_call_id || 'unknown', content: String(m.content ?? '') }]);
51
+ continue;
52
+ }
53
+ const role = m.role === 'assistant' ? 'assistant' : 'user';
54
+ pushMerged(role, String(m.content ?? ''));
55
+ }
56
+
57
+ return { system: system.slice(0, 12000) || undefined, messages: out };
58
+ }
59
+
60
+ requestOptions(messages, options) {
61
+ const { system, messages: msgs } = this.buildMessages(messages);
62
+ const body = {
63
+ model: this.config.model,
64
+ max_tokens: options.max_tokens || this.config.max_tokens || 4096,
65
+ temperature: options.temperature ?? this.config.temperature ?? 0.7,
66
+ messages: msgs
67
+ };
68
+ if (system) body.system = system;
69
+ if (Array.isArray(options.tools) && options.tools.length) {
70
+ body.tools = options.tools.map(t => ({
71
+ name: t.function?.name || 'tool',
72
+ description: t.function?.description || '',
73
+ input_schema: t.function?.parameters || { type: 'object' }
74
+ }));
75
+ }
76
+ return {
77
+ url: `${String(this.config.endpoint).replace(/\/+$/, '')}/v1/messages`,
78
+ headers: {
79
+ 'x-api-key': this.config.apiKey,
80
+ 'anthropic-version': '2023-06-01',
81
+ 'content-type': 'application/json'
82
+ },
83
+ body: JSON.stringify(body)
84
+ };
85
+ }
86
+
87
+ parseContent(data) {
88
+ const blocks = Array.isArray(data.content) ? data.content : [];
89
+ const text = blocks.filter(b => b.type === 'text').map(b => b.text).join('');
90
+ const toolCalls = blocks
91
+ .filter(b => b.type === 'tool_use')
92
+ .map(b => ({
93
+ id: b.id,
94
+ type: 'function',
95
+ function: { name: b.name, arguments: JSON.stringify(b.input ?? {}) }
96
+ }));
97
+ return { text, toolCalls };
98
+ }
99
+
100
+ async generate(messages, options = {}) {
101
+ const { url, headers, body } = this.requestOptions(messages, options);
102
+ const response = await fetch(url, { method: 'POST', headers, body });
103
+ const data = await response.json().catch(() => ({}));
104
+ if (!response.ok) {
105
+ throw new Error(`HTTP ${response.status}: ${data.error?.message || data.error || response.statusText}`);
106
+ }
107
+ if (data.usage) {
108
+ this.lastUsage = {
109
+ prompt_tokens: data.usage.input_tokens || 0,
110
+ completion_tokens: data.usage.output_tokens || 0,
111
+ total_tokens: (data.usage.input_tokens || 0) + (data.usage.output_tokens || 0)
112
+ };
113
+ }
114
+ return this.parseContent(data).text;
115
+ }
116
+
117
+ async chat(messages, options = {}) {
118
+ const { url, headers, body } = this.requestOptions(messages, options);
119
+ const response = await fetch(url, { method: 'POST', headers, body });
120
+ const data = await response.json().catch(() => ({}));
121
+ if (!response.ok) {
122
+ throw new Error(`HTTP ${response.status}: ${data.error?.message || data.error || response.statusText}`);
123
+ }
124
+ if (data.usage) {
125
+ this.lastUsage = {
126
+ prompt_tokens: data.usage.input_tokens || 0,
127
+ completion_tokens: data.usage.output_tokens || 0,
128
+ total_tokens: (data.usage.input_tokens || 0) + (data.usage.output_tokens || 0)
129
+ };
130
+ }
131
+ const { text, toolCalls } = this.parseContent(data);
132
+ return { content: text, tool_calls: toolCalls.length ? toolCalls : null };
133
+ }
134
+
135
+ async stream(messages, onChunk, options = {}) {
136
+ const { url, headers, body } = this.requestOptions(messages, options);
137
+ const response = await fetch(url, { method: 'POST', headers, body: JSON.stringify({ ...JSON.parse(body), stream: true }) });
138
+ if (!response.ok) {
139
+ const errText = await response.text().catch(() => '');
140
+ throw new Error(`HTTP ${response.status}: ${errText.slice(0, 300) || response.statusText}`);
141
+ }
142
+
143
+ const reader = response.body.getReader();
144
+ const decoder = new TextDecoder();
145
+ let buffer = '';
146
+ let full = '';
147
+ let reasoning = '';
148
+ const onReasoning = typeof options.onReasoning === 'function' ? options.onReasoning : () => {};
149
+
150
+ const handleLine = (raw) => {
151
+ const line = raw.trim();
152
+ if (!line || !line.startsWith('data:')) return;
153
+ try {
154
+ const json = JSON.parse(line.slice(5).trim());
155
+ if (json.type === 'content_block_delta' && json.delta?.type === 'text_delta' && json.delta.text) {
156
+ full += json.delta.text;
157
+ onChunk(json.delta.text);
158
+ }
159
+ // Extended thinking streams as thinking_delta before the text blocks.
160
+ if (json.type === 'content_block_delta' && json.delta?.type === 'thinking_delta' && json.delta.thinking) {
161
+ reasoning += json.delta.thinking;
162
+ onReasoning(json.delta.thinking);
163
+ }
164
+ if (json.type === 'message_delta' && json.usage) {
165
+ this.lastUsage = {
166
+ prompt_tokens: json.usage.input_tokens || 0,
167
+ completion_tokens: json.usage.output_tokens || 0,
168
+ total_tokens: (json.usage.input_tokens || 0) + (json.usage.output_tokens || 0)
169
+ };
170
+ }
171
+ } catch { /* incomplete line */ }
172
+ };
173
+
174
+ while (true) {
175
+ const { done, value } = await reader.read();
176
+ if (done) break;
177
+ buffer += decoder.decode(value, { stream: true });
178
+ const lines = buffer.split('\n');
179
+ buffer = lines.pop();
180
+ for (const line of lines) handleLine(line);
181
+ }
182
+ buffer += decoder.decode();
183
+ if (buffer.trim()) handleLine(buffer);
184
+
185
+ if (reasoning.trim()) this.lastReasoning = reasoning;
186
+ return full;
187
+ }
188
+ }
189
+
190
+ module.exports = AnthropicAdapter;
@@ -0,0 +1,148 @@
1
+ // src/daemon/llm/adapters/gemini-adapter.js — Google Gemini generateContent API.
2
+ // Translates Liber's conversation into contents/systemInstruction. Native tool
3
+ // calling is not implemented: the engine falls back to the text protocol.
4
+ const LLMAdapter = require('../base');
5
+
6
+ class GeminiAdapter extends LLMAdapter {
7
+ constructor(config) {
8
+ super(config);
9
+ if (!this.config.endpoint) this.config.endpoint = 'https://generativelanguage.googleapis.com';
10
+ }
11
+
12
+ buildBody(messages, options = {}) {
13
+ let system = '';
14
+ const contents = [];
15
+ let lastRole = null;
16
+
17
+ for (const m of messages) {
18
+ const text = String(m.content ?? '');
19
+ if (m.role === 'system') {
20
+ system += (system ? '\n\n' : '') + text;
21
+ continue;
22
+ }
23
+ const role = m.role === 'assistant' ? 'model' : 'user';
24
+ if (role === lastRole) {
25
+ const prev = contents[contents.length - 1];
26
+ prev.parts.push({ text });
27
+ } else {
28
+ contents.push({ role, parts: [{ text }] });
29
+ lastRole = role;
30
+ }
31
+ }
32
+
33
+ const body = {
34
+ contents,
35
+ generationConfig: {
36
+ temperature: options.temperature ?? this.config.temperature ?? 0.7,
37
+ maxOutputTokens: options.max_tokens || this.config.max_tokens || 4096
38
+ }
39
+ };
40
+ if (system) body.systemInstruction = { parts: [{ text: system.slice(0, 12000) }] };
41
+ return body;
42
+ }
43
+
44
+ endpoint(path) {
45
+ return `${String(this.config.endpoint).replace(/\/+$/, '')}${path}`;
46
+ }
47
+
48
+ async generate(messages, options = {}) {
49
+ const response = await fetch(
50
+ `${this.endpoint('/v1beta/models/')}${this.config.model}:generateContent`,
51
+ {
52
+ method: 'POST',
53
+ headers: {
54
+ 'x-goog-api-key': this.config.apiKey,
55
+ 'content-type': 'application/json'
56
+ },
57
+ body: JSON.stringify(this.buildBody(messages, options))
58
+ }
59
+ );
60
+
61
+ const data = await response.json().catch(() => ({}));
62
+ if (!response.ok) {
63
+ throw new Error(`HTTP ${response.status}: ${data.error?.message || data.error || response.statusText}`);
64
+ }
65
+
66
+ const usage = data.usageMetadata;
67
+ if (usage) {
68
+ this.lastUsage = {
69
+ prompt_tokens: usage.promptTokenCount || 0,
70
+ completion_tokens: usage.candidatesTokenCount || 0,
71
+ total_tokens: (usage.promptTokenCount || 0) + (usage.candidatesTokenCount || 0)
72
+ };
73
+ }
74
+
75
+ const parts = data.candidates?.[0]?.content?.parts || [];
76
+ return parts.filter(p => typeof p.text === 'string').map(p => p.text).join('');
77
+ }
78
+
79
+ async stream(messages, onChunk, options = {}) {
80
+ const response = await fetch(
81
+ `${this.endpoint('/v1beta/models/')}${this.config.model}:streamGenerateContent?alt=sse`,
82
+ {
83
+ method: 'POST',
84
+ headers: {
85
+ 'x-goog-api-key': this.config.apiKey,
86
+ 'content-type': 'application/json'
87
+ },
88
+ body: JSON.stringify(this.buildBody(messages, options))
89
+ }
90
+ );
91
+
92
+ if (!response.ok) {
93
+ const errText = await response.text().catch(() => '');
94
+ throw new Error(`HTTP ${response.status}: ${errText.slice(0, 300) || response.statusText}`);
95
+ }
96
+
97
+ const reader = response.body.getReader();
98
+ const decoder = new TextDecoder();
99
+ let buffer = '';
100
+ let full = '';
101
+ let reasoning = '';
102
+ const onReasoning = typeof options.onReasoning === 'function' ? options.onReasoning : () => {};
103
+
104
+ const handleLine = (raw) => {
105
+ const line = raw.trim();
106
+ if (!line || !line.startsWith('data:')) return;
107
+ try {
108
+ const json = JSON.parse(line.slice(5).trim());
109
+ for (const part of json.candidates?.[0]?.content?.parts || []) {
110
+ if (typeof part.text === 'string' && part.text) {
111
+ // Gemini marks thought summaries with thought:true.
112
+ if (part.thought === true) {
113
+ reasoning += part.text;
114
+ onReasoning(part.text);
115
+ } else {
116
+ full += part.text;
117
+ onChunk(part.text);
118
+ }
119
+ }
120
+ }
121
+ const usage = json.usageMetadata;
122
+ if (usage) {
123
+ this.lastUsage = {
124
+ prompt_tokens: usage.promptTokenCount || 0,
125
+ completion_tokens: usage.candidatesTokenCount || 0,
126
+ total_tokens: (usage.promptTokenCount || 0) + (usage.candidatesTokenCount || 0)
127
+ };
128
+ }
129
+ } catch { /* incomplete line */ }
130
+ };
131
+
132
+ while (true) {
133
+ const { done, value } = await reader.read();
134
+ if (done) break;
135
+ buffer += decoder.decode(value, { stream: true });
136
+ const lines = buffer.split('\n');
137
+ buffer = lines.pop();
138
+ for (const line of lines) handleLine(line);
139
+ }
140
+ buffer += decoder.decode();
141
+ if (buffer.trim()) handleLine(buffer);
142
+
143
+ if (reasoning.trim()) this.lastReasoning = reasoning;
144
+ return full;
145
+ }
146
+ }
147
+
148
+ module.exports = GeminiAdapter;
@@ -0,0 +1,74 @@
1
+ // src/daemon/llm/adapters/ollama-adapter.js
2
+ const LLMAdapter = require('../base');
3
+
4
+ class OllamaAdapter extends LLMAdapter {
5
+ async generate(messages, options = {}) {
6
+ const response = await fetch(`${this.config.endpoint}/api/chat`, {
7
+ method: 'POST',
8
+ body: JSON.stringify({
9
+ model: this.config.model,
10
+ messages: messages,
11
+ stream: false,
12
+ options: options
13
+ })
14
+ });
15
+ const data = await response.json();
16
+ return data.message.content;
17
+ }
18
+
19
+ async stream(messages, onChunk, options = {}) {
20
+ const response = await fetch(`${this.config.endpoint}/api/chat`, {
21
+ method: 'POST',
22
+ body: JSON.stringify({
23
+ model: this.config.model,
24
+ messages: messages,
25
+ stream: true,
26
+ options: options
27
+ })
28
+ });
29
+
30
+ if (!response.ok) {
31
+ const errText = await response.text().catch(() => '');
32
+ throw new Error(`HTTP ${response.status}: ${errText.slice(0, 300) || response.statusText}`);
33
+ }
34
+
35
+ const reader = response.body.getReader();
36
+ const decoder = new TextDecoder();
37
+ let buffer = '';
38
+ let full = '';
39
+
40
+ const handleLine = (raw) => {
41
+ const line = raw.trim();
42
+ if (!line) return;
43
+ try {
44
+ const json = JSON.parse(line);
45
+ if (json.message?.content) {
46
+ full += json.message.content;
47
+ onChunk(json.message.content);
48
+ }
49
+ if (json.eval_count && !this.lastUsage) {
50
+ this.lastUsage = { prompt_tokens: json.prompt_eval_count || 0, completion_tokens: json.eval_count, total_tokens: (json.prompt_eval_count || 0) + json.eval_count };
51
+ }
52
+ } catch {
53
+ // incomplete JSON line: the buffer will complete it on the next read
54
+ }
55
+ };
56
+
57
+ while (true) {
58
+ const { done, value } = await reader.read();
59
+ if (done) break;
60
+
61
+ buffer += decoder.decode(value, { stream: true });
62
+ const lines = buffer.split('\n');
63
+ buffer = lines.pop();
64
+ for (const line of lines) handleLine(line);
65
+ }
66
+
67
+ buffer += decoder.decode();
68
+ if (buffer.trim()) handleLine(buffer);
69
+
70
+ return full;
71
+ }
72
+ }
73
+
74
+ module.exports = OllamaAdapter;
@@ -0,0 +1,148 @@
1
+ // src/daemon/llm/adapters/openai-adapter.js
2
+ const LLMAdapter = require('../base');
3
+
4
+ class OpenAIAdapter extends LLMAdapter {
5
+ async generate(messages, options = {}) {
6
+ const response = await fetch(`${this.config.endpoint}/chat/completions`, {
7
+ method: 'POST',
8
+ headers: {
9
+ 'Authorization': `Bearer ${this.config.apiKey}`,
10
+ 'Content-Type': 'application/json'
11
+ },
12
+ body: JSON.stringify({
13
+ model: this.config.model,
14
+ messages: messages,
15
+ temperature: options.temperature ?? this.config.temperature ?? 0.7,
16
+ max_tokens: options.max_tokens || this.config.max_tokens || undefined,
17
+ stream: false
18
+ })
19
+ });
20
+ const data = await response.json().catch(() => ({}));
21
+ if (!response.ok || !data.choices) {
22
+ throw new Error('HTTP ' + response.status + ': ' + (data.error?.message || data.error || response.statusText));
23
+ }
24
+ this.lastUsage = data.usage || null;
25
+ this.lastReasoning = data.choices[0].message?.reasoning_content || data.choices[0].message?.reasoning || null;
26
+ return data.choices[0].message.content;
27
+ }
28
+
29
+ /**
30
+ * Native tool calling (OpenAI-compatible). Returns the assistant message fields.
31
+ */
32
+ async chat(messages, options = {}) {
33
+ const body = {
34
+ model: this.config.model,
35
+ messages: messages,
36
+ temperature: options.temperature ?? this.config.temperature ?? 0.7,
37
+ max_tokens: options.max_tokens || this.config.max_tokens || undefined,
38
+ stream: false
39
+ };
40
+ const effort = options.reasoning_effort ?? this.config.reasoning_effort;
41
+ if (effort && effort !== 'auto') body.reasoning_effort = effort;
42
+ if (Array.isArray(options.tools) && options.tools.length) {
43
+ body.tools = options.tools;
44
+ body.tool_choice = options.tool_choice || 'auto';
45
+ }
46
+
47
+ const response = await fetch(`${this.config.endpoint}/chat/completions`, {
48
+ method: 'POST',
49
+ headers: {
50
+ 'Authorization': `Bearer ${this.config.apiKey}`,
51
+ 'Content-Type': 'application/json'
52
+ },
53
+ body: JSON.stringify(body)
54
+ });
55
+
56
+ const data = await response.json().catch(() => ({}));
57
+ if (!response.ok || !data.choices) {
58
+ throw new Error('HTTP ' + response.status + ': ' + (data.error?.message || data.error || response.statusText));
59
+ }
60
+
61
+ this.lastUsage = data.usage || null;
62
+ const msg = data.choices[0].message || {};
63
+ this.lastReasoning = msg.reasoning_content || msg.reasoning || null;
64
+ return {
65
+ content: msg.content || '',
66
+ tool_calls: Array.isArray(msg.tool_calls) && msg.tool_calls.length ? msg.tool_calls : null,
67
+ finish_reason: data.choices[0].finish_reason || null
68
+ };
69
+ }
70
+
71
+ async stream(messages, onChunk, options = {}) {
72
+ // Reasoning models only stream reasoning_content when asked for it.
73
+ const effort = options.reasoning_effort ?? this.config.reasoning_effort;
74
+ const response = await fetch(`${this.config.endpoint}/chat/completions`, {
75
+ method: 'POST',
76
+ headers: {
77
+ 'Authorization': `Bearer ${this.config.apiKey}`,
78
+ 'Content-Type': 'application/json'
79
+ },
80
+ body: JSON.stringify({
81
+ model: this.config.model,
82
+ messages: messages,
83
+ temperature: options.temperature ?? this.config.temperature ?? 0.7,
84
+ max_tokens: options.max_tokens || this.config.max_tokens || undefined,
85
+ stream: true,
86
+ reasoning_effort: effort && effort !== 'auto' ? effort : undefined,
87
+ // Ask the provider to emit a final usage chunk (OpenAI-compatible).
88
+ stream_options: this.config.stream_usage === false ? undefined : { include_usage: true }
89
+ })
90
+ });
91
+
92
+ if (!response.ok) {
93
+ const errText = await response.text().catch(() => '');
94
+ throw new Error(`HTTP ${response.status}: ${errText.slice(0, 300) || response.statusText}`);
95
+ }
96
+
97
+ const reader = response.body.getReader();
98
+ const decoder = new TextDecoder();
99
+ let buffer = '';
100
+ let full = '';
101
+ let reasoning = '';
102
+ const onReasoning = typeof options.onReasoning === 'function' ? options.onReasoning : () => {};
103
+
104
+ const handleLine = (raw) => {
105
+ const line = raw.trim();
106
+ if (!line || line === 'data: [DONE]' || !line.startsWith('data:')) return;
107
+ try {
108
+ const json = JSON.parse(line.slice(5).trim());
109
+ if (json.usage) this.lastUsage = json.usage;
110
+ const delta = json.choices?.[0]?.delta;
111
+ if (!delta) return;
112
+ // DeepSeek / OpenRouter reasoners stream chain-of-thought here.
113
+ const thought = delta.reasoning_content ?? delta.reasoning;
114
+ if (typeof thought === 'string' && thought) {
115
+ reasoning += thought;
116
+ onReasoning(thought);
117
+ }
118
+ const content = delta.content;
119
+ if (content) {
120
+ full += content;
121
+ onChunk(content);
122
+ }
123
+ } catch {
124
+ // incomplete JSON line: the buffer will complete it on the next read
125
+ }
126
+ };
127
+
128
+ while (true) {
129
+ const { done, value } = await reader.read();
130
+ if (done) break;
131
+
132
+ // Buffer across reads: a JSON line can be split by the network.
133
+ buffer += decoder.decode(value, { stream: true });
134
+ const lines = buffer.split('\n');
135
+ buffer = lines.pop();
136
+ for (const line of lines) handleLine(line);
137
+ }
138
+
139
+ buffer += decoder.decode();
140
+ if (buffer.trim()) handleLine(buffer);
141
+
142
+ if (reasoning.trim()) this.lastReasoning = reasoning;
143
+ return full;
144
+ }
145
+ }
146
+
147
+ module.exports = OpenAIAdapter;
148
+
@@ -0,0 +1,31 @@
1
+ // src/daemon/llm/base.js
2
+ /**
3
+ * Base LLM Adapter
4
+ * Semua provider LLM harus mengimplementasikan class ini.
5
+ */
6
+ class LLMAdapter {
7
+ constructor(config) {
8
+ this.config = config;
9
+ }
10
+
11
+ /**
12
+ * Mengirim prompt dan menerima respon teks tunggal.
13
+ * @param {Array} messages - Format: [{ role: 'user', content: '...' }]
14
+ * @param {Object} options - Parameter tambahan (temperature, max_tokens, dll)
15
+ */
16
+ async generate(messages, options = {}) {
17
+ throw new Error("Method 'generate()' must be implemented.");
18
+ }
19
+
20
+ /**
21
+ * Mengirim prompt dan menerima respon secara streaming.
22
+ * @param {Array} messages
23
+ * @param {Function} onChunk - Callback untuk setiap potongan teks yang diterima.
24
+ * @param {Object} options
25
+ */
26
+ async stream(messages, onChunk, options = {}) {
27
+ throw new Error("Method 'stream()' must be implemented.");
28
+ }
29
+ }
30
+
31
+ module.exports = LLMAdapter;