@dxos/assistant 0.8.0 → 0.8.1-main.81238a8
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.
- package/dist/lib/browser/chunk-RFTY3OMK.mjs +2276 -0
- package/dist/lib/browser/chunk-RFTY3OMK.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +33 -1236
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +17 -3
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/chunk-4ZDZUCAS.cjs +2287 -0
- package/dist/lib/node/chunk-4ZDZUCAS.cjs.map +7 -0
- package/dist/lib/node/index.cjs +40 -1236
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +40 -29
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node-esm/chunk-JUVGBQBL.mjs +2278 -0
- package/dist/lib/node-esm/chunk-JUVGBQBL.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +33 -1236
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +17 -3
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/ai-service/defs.d.ts +8 -2
- package/dist/types/src/ai-service/defs.d.ts.map +1 -1
- package/dist/types/src/ai-service/edge-client.d.ts +19 -0
- package/dist/types/src/ai-service/edge-client.d.ts.map +1 -0
- package/dist/types/src/ai-service/index.d.ts +4 -1
- package/dist/types/src/ai-service/index.d.ts.map +1 -1
- package/dist/types/src/ai-service/message-collector.d.ts +88 -0
- package/dist/types/src/ai-service/message-collector.d.ts.map +1 -0
- package/dist/types/src/ai-service/ollama-client.d.ts +39 -0
- package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -0
- package/dist/types/src/ai-service/parser.d.ts +11 -9
- package/dist/types/src/ai-service/parser.d.ts.map +1 -1
- package/dist/types/src/ai-service/service.d.ts +9 -0
- package/dist/types/src/ai-service/service.d.ts.map +1 -0
- package/dist/types/src/ai-service/stream.d.ts +2 -1
- package/dist/types/src/ai-service/stream.d.ts.map +1 -1
- package/dist/types/src/ai-service/testing/mock.d.ts +2 -2
- package/dist/types/src/ai-service/testing/mock.d.ts.map +1 -1
- package/dist/types/src/ai-service/types.d.ts +91 -157
- package/dist/types/src/ai-service/types.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +11 -9
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/types.d.ts +1 -1
- package/dist/types/src/conversation/types.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/ollama.d.ts +6 -0
- package/dist/types/src/testing/ollama.d.ts.map +1 -0
- package/dist/types/src/testing/query-promts.d.ts +1 -1
- package/dist/types/src/testing/query-promts.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +1 -1
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +1 -1
- package/dist/types/src/testing/test-schema.d.ts.map +1 -1
- package/package.json +17 -14
- package/src/ai-service/client.test.ts +100 -13
- package/src/ai-service/defs.ts +17 -2
- package/src/ai-service/{client.ts → edge-client.ts} +15 -24
- package/src/ai-service/index.ts +4 -1
- package/src/ai-service/message-collector.ts +84 -0
- package/src/ai-service/ollama-client.ts +499 -0
- package/src/ai-service/parser.ts +7 -2
- package/src/ai-service/service.ts +15 -0
- package/src/ai-service/stream.ts +5 -3
- package/src/ai-service/testing/mock.ts +7 -2
- package/src/ai-service/types.ts +3 -3
- package/src/bin/chat.ts +3 -3
- package/src/conversation/conversation.test.ts +5 -5
- package/src/conversation/conversation.ts +3 -2
- package/src/conversation/types.ts +1 -1
- package/src/testing/artifacts.test.ts +4 -4
- package/src/testing/index.ts +1 -0
- package/src/testing/object-extraction.test.ts +1 -1
- package/src/testing/ollama.ts +15 -0
- package/src/testing/query-promts.ts +1 -1
- package/src/testing/query.test.ts +4 -4
- package/src/testing/test-data.ts +1 -1
- package/src/testing/test-schema.ts +1 -1
- package/dist/lib/browser/chunk-AXUUNA7O.mjs +0 -565
- package/dist/lib/browser/chunk-AXUUNA7O.mjs.map +0 -7
- package/dist/lib/node/chunk-QPHGQINW.cjs +0 -577
- package/dist/lib/node/chunk-QPHGQINW.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-GOVFOBIB.mjs +0 -567
- package/dist/lib/node-esm/chunk-GOVFOBIB.mjs.map +0 -7
- package/dist/types/src/ai-service/client.d.ts +0 -25
- package/dist/types/src/ai-service/client.d.ts.map +0 -1
package/dist/lib/node/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,1255 +15,61 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var node_exports = {};
|
|
30
20
|
__export(node_exports, {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
21
|
+
AIServiceEdgeClient: () => import_chunk_4ZDZUCAS.AIServiceEdgeClient,
|
|
22
|
+
DEFAULT_EDGE_MODEL: () => import_chunk_4ZDZUCAS.DEFAULT_EDGE_MODEL,
|
|
23
|
+
DEFAULT_EDGE_MODELS: () => import_chunk_4ZDZUCAS.DEFAULT_EDGE_MODELS,
|
|
24
|
+
DEFAULT_OLLAMA_ENDPOINT: () => import_chunk_4ZDZUCAS.DEFAULT_OLLAMA_ENDPOINT,
|
|
25
|
+
DEFAULT_OLLAMA_MODEL: () => import_chunk_4ZDZUCAS.DEFAULT_OLLAMA_MODEL,
|
|
26
|
+
DEFAULT_OLLAMA_MODELS: () => import_chunk_4ZDZUCAS.DEFAULT_OLLAMA_MODELS,
|
|
27
|
+
EchoDataSource: () => import_chunk_4ZDZUCAS.EchoDataSource,
|
|
28
|
+
GenerateRequest: () => import_chunk_4ZDZUCAS.GenerateRequest,
|
|
29
|
+
GenerationStreamImpl: () => import_chunk_4ZDZUCAS.GenerationStreamImpl,
|
|
30
|
+
LLMModel: () => import_chunk_4ZDZUCAS.LLMModel,
|
|
31
|
+
MessageCollector: () => import_chunk_4ZDZUCAS.MessageCollector,
|
|
32
|
+
MixedStreamParser: () => import_chunk_4ZDZUCAS.MixedStreamParser,
|
|
33
|
+
OllamaClient: () => import_chunk_4ZDZUCAS.OllamaClient,
|
|
34
|
+
StreamTransform: () => import_chunk_4ZDZUCAS.StreamTransform,
|
|
35
|
+
ToolTypes: () => import_chunk_4ZDZUCAS.ToolTypes,
|
|
36
|
+
createArtifactElement: () => import_chunk_4ZDZUCAS.createArtifactElement,
|
|
37
|
+
createGenerationStream: () => import_chunk_4ZDZUCAS.createGenerationStream,
|
|
38
|
+
emitMessageAsEvents: () => import_chunk_4ZDZUCAS.emitMessageAsEvents,
|
|
39
|
+
executeQuery: () => import_chunk_4ZDZUCAS.executeQuery,
|
|
40
|
+
executeQueryPlan: () => import_chunk_4ZDZUCAS.executeQueryPlan,
|
|
41
|
+
formatInferredRelationshipLabel: () => import_chunk_4ZDZUCAS.formatInferredRelationshipLabel,
|
|
42
|
+
formatJsonSchemaForLLM: () => import_chunk_4ZDZUCAS.formatJsonSchemaForLLM,
|
|
43
|
+
formatNodeLabel: () => import_chunk_4ZDZUCAS.formatNodeLabel,
|
|
44
|
+
isReference: () => import_chunk_4ZDZUCAS.isReference,
|
|
45
|
+
isToolUse: () => import_chunk_4ZDZUCAS.isToolUse,
|
|
46
|
+
mergeMessageBlock: () => import_chunk_4ZDZUCAS.mergeMessageBlock,
|
|
47
|
+
parseCypherQuery: () => import_chunk_4ZDZUCAS.parseCypherQuery,
|
|
48
|
+
runLLM: () => import_chunk_4ZDZUCAS.runLLM,
|
|
49
|
+
runTools: () => import_chunk_4ZDZUCAS.runTools
|
|
54
50
|
});
|
|
55
51
|
module.exports = __toCommonJS(node_exports);
|
|
56
|
-
var
|
|
57
|
-
var import_schema = require("@effect/schema");
|
|
58
|
-
var import_artifact = require("@dxos/artifact");
|
|
59
|
-
var import_invariant = require("@dxos/invariant");
|
|
60
|
-
var import_log = require("@dxos/log");
|
|
61
|
-
var import_async = require("@dxos/async");
|
|
62
|
-
var import_log2 = require("@dxos/log");
|
|
63
|
-
var import_artifact2 = require("@dxos/artifact");
|
|
64
|
-
var import_async2 = require("@dxos/async");
|
|
65
|
-
var import_echo_schema = require("@dxos/echo-schema");
|
|
66
|
-
var import_invariant2 = require("@dxos/invariant");
|
|
67
|
-
var import_log3 = require("@dxos/log");
|
|
68
|
-
var import_util = require("@dxos/util");
|
|
69
|
-
var P = __toESM(require("parsimmon"));
|
|
70
|
-
var import_schema2 = require("@effect/schema");
|
|
71
|
-
var import_artifact3 = require("@dxos/artifact");
|
|
72
|
-
var import_echo_schema2 = require("@dxos/echo-schema");
|
|
73
|
-
var import_artifact4 = require("@dxos/artifact");
|
|
74
|
-
var import_echo_schema3 = require("@dxos/echo-schema");
|
|
75
|
-
var import_invariant3 = require("@dxos/invariant");
|
|
76
|
-
var import_log4 = require("@dxos/log");
|
|
77
|
-
async function* iterSSEMessages(response, controller) {
|
|
78
|
-
if (!response.body) {
|
|
79
|
-
controller.abort();
|
|
80
|
-
throw new Error("Attempted to iterate over a response with no body");
|
|
81
|
-
}
|
|
82
|
-
const sseDecoder = new SSEDecoder();
|
|
83
|
-
const lineDecoder = new LineDecoder();
|
|
84
|
-
const iter = readableStreamAsyncIterable(response.body);
|
|
85
|
-
for await (const sseChunk of iterSSEChunks(iter)) {
|
|
86
|
-
for (const line of lineDecoder.decode(sseChunk)) {
|
|
87
|
-
const sse = sseDecoder.decode(line);
|
|
88
|
-
if (sse) {
|
|
89
|
-
yield sse;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
for (const line of lineDecoder.flush()) {
|
|
94
|
-
const sse = sseDecoder.decode(line);
|
|
95
|
-
if (sse) {
|
|
96
|
-
yield sse;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
async function* iterSSEChunks(iterator) {
|
|
101
|
-
let data = new Uint8Array();
|
|
102
|
-
for await (const chunk of iterator) {
|
|
103
|
-
if (chunk == null) {
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk;
|
|
107
|
-
const newData = new Uint8Array(data.length + binaryChunk.length);
|
|
108
|
-
newData.set(data);
|
|
109
|
-
newData.set(binaryChunk, data.length);
|
|
110
|
-
data = newData;
|
|
111
|
-
let patternIndex;
|
|
112
|
-
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
|
|
113
|
-
yield data.slice(0, patternIndex);
|
|
114
|
-
data = data.slice(patternIndex);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (data.length > 0) {
|
|
118
|
-
yield data;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
var findDoubleNewlineIndex = (buffer) => {
|
|
122
|
-
const newline = 10;
|
|
123
|
-
const carriage = 13;
|
|
124
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
125
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
126
|
-
return i + 2;
|
|
127
|
-
}
|
|
128
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
129
|
-
return i + 2;
|
|
130
|
-
}
|
|
131
|
-
if (buffer[i] === carriage && buffer[i + 1] === newline && i + 3 < buffer.length && buffer[i + 2] === carriage && buffer[i + 3] === newline) {
|
|
132
|
-
return i + 4;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return -1;
|
|
136
|
-
};
|
|
137
|
-
var SSEDecoder = class {
|
|
138
|
-
constructor() {
|
|
139
|
-
this.event = null;
|
|
140
|
-
this.data = [];
|
|
141
|
-
this.chunks = [];
|
|
142
|
-
}
|
|
143
|
-
decode(line) {
|
|
144
|
-
if (line.endsWith("\r")) {
|
|
145
|
-
line = line.substring(0, line.length - 1);
|
|
146
|
-
}
|
|
147
|
-
if (!line) {
|
|
148
|
-
if (!this.event && !this.data.length) {
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
const sse = {
|
|
152
|
-
event: this.event,
|
|
153
|
-
data: this.data.join("\n"),
|
|
154
|
-
raw: this.chunks
|
|
155
|
-
};
|
|
156
|
-
this.event = null;
|
|
157
|
-
this.data = [];
|
|
158
|
-
this.chunks = [];
|
|
159
|
-
return sse;
|
|
160
|
-
}
|
|
161
|
-
this.chunks.push(line);
|
|
162
|
-
if (line.startsWith(":")) {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
let [fieldname, _, value] = partition(line, ":");
|
|
166
|
-
if (value.startsWith(" ")) {
|
|
167
|
-
value = value.substring(1);
|
|
168
|
-
}
|
|
169
|
-
if (fieldname === "event") {
|
|
170
|
-
this.event = value;
|
|
171
|
-
} else if (fieldname === "data") {
|
|
172
|
-
this.data.push(value);
|
|
173
|
-
}
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
var partition = (str, delimiter) => {
|
|
178
|
-
const index = str.indexOf(delimiter);
|
|
179
|
-
if (index !== -1) {
|
|
180
|
-
return [
|
|
181
|
-
str.substring(0, index),
|
|
182
|
-
delimiter,
|
|
183
|
-
str.substring(index + delimiter.length)
|
|
184
|
-
];
|
|
185
|
-
}
|
|
186
|
-
return [
|
|
187
|
-
str,
|
|
188
|
-
"",
|
|
189
|
-
""
|
|
190
|
-
];
|
|
191
|
-
};
|
|
192
|
-
function readableStreamAsyncIterable(stream) {
|
|
193
|
-
if (stream[Symbol.asyncIterator]) {
|
|
194
|
-
return stream;
|
|
195
|
-
}
|
|
196
|
-
const reader = stream.getReader();
|
|
197
|
-
return {
|
|
198
|
-
next: async () => {
|
|
199
|
-
try {
|
|
200
|
-
const result = await reader.read();
|
|
201
|
-
if (result?.done) {
|
|
202
|
-
reader.releaseLock();
|
|
203
|
-
}
|
|
204
|
-
return result;
|
|
205
|
-
} catch (e) {
|
|
206
|
-
reader.releaseLock();
|
|
207
|
-
throw e;
|
|
208
|
-
}
|
|
209
|
-
},
|
|
210
|
-
return: async () => {
|
|
211
|
-
const cancelPromise = reader.cancel();
|
|
212
|
-
reader.releaseLock();
|
|
213
|
-
await cancelPromise;
|
|
214
|
-
return {
|
|
215
|
-
done: true,
|
|
216
|
-
value: void 0
|
|
217
|
-
};
|
|
218
|
-
},
|
|
219
|
-
[Symbol.asyncIterator]() {
|
|
220
|
-
return this;
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
var LineDecoder = class _LineDecoder {
|
|
225
|
-
static {
|
|
226
|
-
this.NEWLINE_CHARS = /* @__PURE__ */ new Set([
|
|
227
|
-
"\n",
|
|
228
|
-
"\r"
|
|
229
|
-
]);
|
|
230
|
-
}
|
|
231
|
-
static {
|
|
232
|
-
this.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
233
|
-
}
|
|
234
|
-
constructor() {
|
|
235
|
-
this.buffer = [];
|
|
236
|
-
this.trailingCR = false;
|
|
237
|
-
}
|
|
238
|
-
decode(chunk) {
|
|
239
|
-
let text = this.decodeText(chunk);
|
|
240
|
-
if (this.trailingCR) {
|
|
241
|
-
text = "\r" + text;
|
|
242
|
-
this.trailingCR = false;
|
|
243
|
-
}
|
|
244
|
-
if (text.endsWith("\r")) {
|
|
245
|
-
this.trailingCR = true;
|
|
246
|
-
text = text.slice(0, -1);
|
|
247
|
-
}
|
|
248
|
-
if (!text) {
|
|
249
|
-
return [];
|
|
250
|
-
}
|
|
251
|
-
const trailingNewline = _LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || "");
|
|
252
|
-
let lines = text.split(_LineDecoder.NEWLINE_REGEXP);
|
|
253
|
-
if (trailingNewline) {
|
|
254
|
-
lines.pop();
|
|
255
|
-
}
|
|
256
|
-
if (lines.length === 1 && !trailingNewline) {
|
|
257
|
-
this.buffer.push(lines[0]);
|
|
258
|
-
return [];
|
|
259
|
-
}
|
|
260
|
-
if (this.buffer.length > 0) {
|
|
261
|
-
lines = [
|
|
262
|
-
this.buffer.join("") + lines[0],
|
|
263
|
-
...lines.slice(1)
|
|
264
|
-
];
|
|
265
|
-
this.buffer = [];
|
|
266
|
-
}
|
|
267
|
-
if (!trailingNewline) {
|
|
268
|
-
this.buffer = [
|
|
269
|
-
lines.pop() || ""
|
|
270
|
-
];
|
|
271
|
-
}
|
|
272
|
-
return lines;
|
|
273
|
-
}
|
|
274
|
-
decodeText(bytes) {
|
|
275
|
-
if (bytes == null) {
|
|
276
|
-
return "";
|
|
277
|
-
}
|
|
278
|
-
if (typeof bytes === "string") {
|
|
279
|
-
return bytes;
|
|
280
|
-
}
|
|
281
|
-
if (typeof Buffer !== "undefined") {
|
|
282
|
-
if (bytes instanceof Buffer) {
|
|
283
|
-
return bytes.toString();
|
|
284
|
-
}
|
|
285
|
-
if (bytes instanceof Uint8Array) {
|
|
286
|
-
return Buffer.from(bytes).toString();
|
|
287
|
-
}
|
|
288
|
-
throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`);
|
|
289
|
-
}
|
|
290
|
-
if (typeof TextDecoder !== "undefined") {
|
|
291
|
-
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
|
|
292
|
-
this.textDecoder ??= new TextDecoder("utf8");
|
|
293
|
-
return this.textDecoder.decode(bytes);
|
|
294
|
-
}
|
|
295
|
-
throw new Error(`Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform. Please report this error.`);
|
|
296
|
-
}
|
|
297
|
-
throw new Error("Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.");
|
|
298
|
-
}
|
|
299
|
-
flush() {
|
|
300
|
-
if (!this.buffer.length && !this.trailingCR) {
|
|
301
|
-
return [];
|
|
302
|
-
}
|
|
303
|
-
const lines = [
|
|
304
|
-
this.buffer.join("")
|
|
305
|
-
];
|
|
306
|
-
this.buffer = [];
|
|
307
|
-
this.trailingCR = false;
|
|
308
|
-
return lines;
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/stream.ts";
|
|
312
|
-
var createGenerationStream = (response, controller = new AbortController()) => {
|
|
313
|
-
const iterator = async function* () {
|
|
314
|
-
for await (const sse of iterSSEMessages(response, controller)) {
|
|
315
|
-
if (sse.event === "completion") {
|
|
316
|
-
try {
|
|
317
|
-
yield JSON.parse(sse.data);
|
|
318
|
-
} catch (err) {
|
|
319
|
-
import_log2.log.error("could not parse message into JSON:", {
|
|
320
|
-
data: sse.data,
|
|
321
|
-
raw: sse.raw
|
|
322
|
-
}, {
|
|
323
|
-
F: __dxlog_file,
|
|
324
|
-
L: 21,
|
|
325
|
-
S: this,
|
|
326
|
-
C: (f, a) => f(...a)
|
|
327
|
-
});
|
|
328
|
-
throw err;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
if (sse.event === "message_start" || sse.event === "message_delta" || sse.event === "message_stop" || sse.event === "content_block_start" || sse.event === "content_block_delta" || sse.event === "content_block_stop") {
|
|
332
|
-
try {
|
|
333
|
-
yield JSON.parse(sse.data);
|
|
334
|
-
} catch (err) {
|
|
335
|
-
import_log2.log.error("could not parse message into JSON:", {
|
|
336
|
-
data: sse.data,
|
|
337
|
-
raw: sse.raw
|
|
338
|
-
}, {
|
|
339
|
-
F: __dxlog_file,
|
|
340
|
-
L: 37,
|
|
341
|
-
S: this,
|
|
342
|
-
C: (f, a) => f(...a)
|
|
343
|
-
});
|
|
344
|
-
throw err;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
if (sse.event === "ping") {
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
|
-
if (sse.event === "error") {
|
|
351
|
-
throw new Error(`Message generation error: ${sse.data}`);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
return new GenerationStream(controller, iterator);
|
|
356
|
-
};
|
|
357
|
-
var GenerationStream = class {
|
|
358
|
-
constructor(_controller, _getIterator) {
|
|
359
|
-
this._controller = _controller;
|
|
360
|
-
this._getIterator = _getIterator;
|
|
361
|
-
this._done = new import_async.Trigger();
|
|
362
|
-
this._iterator = void 0;
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Returns an iterator over the stream.
|
|
366
|
-
*/
|
|
367
|
-
[Symbol.asyncIterator]() {
|
|
368
|
-
return this._createIterator();
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Aborts the stream.
|
|
372
|
-
*/
|
|
373
|
-
abort() {
|
|
374
|
-
this._controller.abort();
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Returns the complete message list generated by the AI service.
|
|
378
|
-
*/
|
|
379
|
-
async complete() {
|
|
380
|
-
await this._done.wait();
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Creates an iterator over the stream.
|
|
384
|
-
*/
|
|
385
|
-
_createIterator() {
|
|
386
|
-
const self = this;
|
|
387
|
-
return this._iterator ??= (() => {
|
|
388
|
-
const generator = async function* () {
|
|
389
|
-
try {
|
|
390
|
-
for await (const event of self._getIterator()) {
|
|
391
|
-
yield event;
|
|
392
|
-
}
|
|
393
|
-
this._done.wake();
|
|
394
|
-
} catch (err) {
|
|
395
|
-
this._done.throw(err);
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
return generator.call(this);
|
|
399
|
-
})();
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/client.ts";
|
|
403
|
-
var AIServiceClientImpl = class {
|
|
404
|
-
constructor({ endpoint }) {
|
|
405
|
-
(0, import_invariant.invariant)(endpoint, "endpoint is required", {
|
|
406
|
-
F: __dxlog_file2,
|
|
407
|
-
L: 34,
|
|
408
|
-
S: this,
|
|
409
|
-
A: [
|
|
410
|
-
"endpoint",
|
|
411
|
-
"'endpoint is required'"
|
|
412
|
-
]
|
|
413
|
-
});
|
|
414
|
-
this._endpoint = endpoint;
|
|
415
|
-
}
|
|
416
|
-
async appendMessages(messages) {
|
|
417
|
-
const url = `${this._endpoint}/message`;
|
|
418
|
-
const res = await fetch(url, {
|
|
419
|
-
method: "POST",
|
|
420
|
-
headers: {
|
|
421
|
-
"Content-Type": "application/json"
|
|
422
|
-
},
|
|
423
|
-
body: JSON.stringify({
|
|
424
|
-
messages: await import_schema.Schema.encodePromise(import_schema.Schema.Array(import_artifact.Message))(messages)
|
|
425
|
-
})
|
|
426
|
-
});
|
|
427
|
-
if (!res.ok) {
|
|
428
|
-
throw new Error(`${await res.text()} [${url}]`);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* Process request and open message stream.
|
|
433
|
-
*/
|
|
434
|
-
async exec(request) {
|
|
435
|
-
request = {
|
|
436
|
-
tools: request.tools ?? [],
|
|
437
|
-
...request
|
|
438
|
-
};
|
|
439
|
-
import_log.log.info("requesting", {
|
|
440
|
-
endpoint: this._endpoint
|
|
441
|
-
}, {
|
|
442
|
-
F: __dxlog_file2,
|
|
443
|
-
L: 63,
|
|
444
|
-
S: this,
|
|
445
|
-
C: (f, a) => f(...a)
|
|
446
|
-
});
|
|
447
|
-
const controller = new AbortController();
|
|
448
|
-
const response = await fetch(`${this._endpoint}/generate`, {
|
|
449
|
-
signal: controller.signal,
|
|
450
|
-
method: "POST",
|
|
451
|
-
headers: {
|
|
452
|
-
"Content-Type": "application/json"
|
|
453
|
-
},
|
|
454
|
-
body: JSON.stringify(request)
|
|
455
|
-
});
|
|
456
|
-
(0, import_invariant.invariant)(response.body instanceof ReadableStream, void 0, {
|
|
457
|
-
F: __dxlog_file2,
|
|
458
|
-
L: 72,
|
|
459
|
-
S: this,
|
|
460
|
-
A: [
|
|
461
|
-
"response.body instanceof ReadableStream",
|
|
462
|
-
""
|
|
463
|
-
]
|
|
464
|
-
});
|
|
465
|
-
return createGenerationStream(response, controller);
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
var DEFAULT_LLM_MODEL = "@anthropic/claude-3-5-sonnet-20241022";
|
|
469
|
-
var DEFAULT_LLM_MODELS = [
|
|
470
|
-
// AI Gateway.
|
|
471
|
-
// https://developers.cloudflare.com/ai-gateway
|
|
472
|
-
"@anthropic/claude-3-5-haiku-20241022",
|
|
473
|
-
"@anthropic/claude-3-5-sonnet-20241022",
|
|
474
|
-
"@anthropic/claude-3-7-sonnet-20250219",
|
|
475
|
-
// Workers AI.
|
|
476
|
-
// https://developers.cloudflare.com/workers-ai/models
|
|
477
|
-
"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
|
|
478
|
-
"@hf/nousresearch/hermes-2-pro-mistral-7b",
|
|
479
|
-
"@ollama/llama-3-1-nemotron-70b-instruct",
|
|
480
|
-
"@ollama/llama-3-1-nemotron-mini-4b-instruct",
|
|
481
|
-
"@ollama/llama-3-2-3b"
|
|
482
|
-
];
|
|
483
|
-
var textChunk = P.regexp(/[^<]+/).map((content) => ({
|
|
484
|
-
type: "text",
|
|
485
|
-
content
|
|
486
|
-
}));
|
|
487
|
-
var attribute = P.seq(P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/), P.string("=").trim(P.optWhitespace), P.alt(P.regexp(/"[^"]*"/).map((s) => s.slice(1, -1)), P.regexp(/'[^']*'/).map((s) => s.slice(1, -1)))).map(([name, _, value]) => [
|
|
488
|
-
name,
|
|
489
|
-
value
|
|
490
|
-
]);
|
|
491
|
-
var tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
|
|
492
|
-
var selfClosingTag = P.seqMap(
|
|
493
|
-
//
|
|
494
|
-
P.string("<"),
|
|
495
|
-
tagName,
|
|
496
|
-
P.optWhitespace.then(attribute).many(),
|
|
497
|
-
P.optWhitespace.then(P.string("/>")),
|
|
498
|
-
P.optWhitespace,
|
|
499
|
-
(_, tag, attributes, __) => ({
|
|
500
|
-
type: "tag",
|
|
501
|
-
tag,
|
|
502
|
-
attributes: Object.fromEntries(attributes),
|
|
503
|
-
content: [],
|
|
504
|
-
selfClosing: true
|
|
505
|
-
})
|
|
506
|
-
);
|
|
507
|
-
var openTag = P.seqMap(
|
|
508
|
-
//
|
|
509
|
-
P.string("<"),
|
|
510
|
-
tagName,
|
|
511
|
-
P.optWhitespace.then(attribute).many(),
|
|
512
|
-
P.string(">"),
|
|
513
|
-
P.regexp(/[^<]*/),
|
|
514
|
-
(_, tag, attributes, __, content) => ({
|
|
515
|
-
type: "tag",
|
|
516
|
-
tag,
|
|
517
|
-
attributes: Object.fromEntries(attributes),
|
|
518
|
-
content: content.length ? [
|
|
519
|
-
{
|
|
520
|
-
type: "text",
|
|
521
|
-
content
|
|
522
|
-
}
|
|
523
|
-
] : []
|
|
524
|
-
})
|
|
525
|
-
);
|
|
526
|
-
var closeTag = P.seqMap(
|
|
527
|
-
//
|
|
528
|
-
P.string("</"),
|
|
529
|
-
tagName,
|
|
530
|
-
P.string(">"),
|
|
531
|
-
P.optWhitespace,
|
|
532
|
-
(_, tag) => ({
|
|
533
|
-
type: "tag",
|
|
534
|
-
tag,
|
|
535
|
-
content: [],
|
|
536
|
-
closing: true
|
|
537
|
-
})
|
|
538
|
-
);
|
|
539
|
-
var mixedChunk = P.alt(selfClosingTag, openTag, closeTag, textChunk);
|
|
540
|
-
var StreamTransform = class {
|
|
541
|
-
constructor() {
|
|
542
|
-
this._buffer = "";
|
|
543
|
-
}
|
|
544
|
-
transform(chunk) {
|
|
545
|
-
this._buffer += chunk;
|
|
546
|
-
const results = [];
|
|
547
|
-
const parser = mixedChunk.many().skip(P.optWhitespace);
|
|
548
|
-
const result = parser.parse(this._buffer);
|
|
549
|
-
if (result.status) {
|
|
550
|
-
for (const chunk2 of result.value) {
|
|
551
|
-
results.push(chunk2);
|
|
552
|
-
}
|
|
553
|
-
this._buffer = "";
|
|
554
|
-
}
|
|
555
|
-
return results;
|
|
556
|
-
}
|
|
557
|
-
// TODO(burdon): Process remaining buffer?
|
|
558
|
-
flush() {
|
|
559
|
-
const remaining = this._buffer;
|
|
560
|
-
this._buffer = "";
|
|
561
|
-
return remaining.length > 0 ? [
|
|
562
|
-
{
|
|
563
|
-
type: "text",
|
|
564
|
-
content: remaining
|
|
565
|
-
}
|
|
566
|
-
] : [];
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
|
|
570
|
-
var MixedStreamParser = class {
|
|
571
|
-
constructor() {
|
|
572
|
-
this.message = new import_async2.Event();
|
|
573
|
-
this.block = new import_async2.Event();
|
|
574
|
-
this.update = new import_async2.Event();
|
|
575
|
-
}
|
|
576
|
-
_emitBlock(contentBlock, streamBlock) {
|
|
577
|
-
const messageBlock = streamBlock ? mergeMessageBlock(contentBlock, streamBlock) : contentBlock;
|
|
578
|
-
if (messageBlock) {
|
|
579
|
-
if (messageBlock.type === "text" && messageBlock.text.length === 0) {
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
(0, import_invariant2.invariant)(this._message, void 0, {
|
|
583
|
-
F: __dxlog_file3,
|
|
584
|
-
L: 46,
|
|
585
|
-
S: this,
|
|
586
|
-
A: [
|
|
587
|
-
"this._message",
|
|
588
|
-
""
|
|
589
|
-
]
|
|
590
|
-
});
|
|
591
|
-
this._message.content.push(messageBlock);
|
|
592
|
-
this.block.emit(messageBlock);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
_emitUpdate(contentBlock, streamBlock) {
|
|
596
|
-
const messageBlock = mergeMessageBlock(contentBlock, streamBlock);
|
|
597
|
-
if (messageBlock) {
|
|
598
|
-
messageBlock.pending = true;
|
|
599
|
-
this.update.emit(messageBlock);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* Parse stream until end.
|
|
604
|
-
*/
|
|
605
|
-
async parse(stream) {
|
|
606
|
-
const transformer = new StreamTransform();
|
|
607
|
-
let contentBlock;
|
|
608
|
-
let streamBlock;
|
|
609
|
-
const stack = [];
|
|
610
|
-
const messagesCollected = [];
|
|
611
|
-
for await (const event of stream) {
|
|
612
|
-
switch (event.type) {
|
|
613
|
-
//
|
|
614
|
-
// Messages.
|
|
615
|
-
//
|
|
616
|
-
case "message_start": {
|
|
617
|
-
if (this._message) {
|
|
618
|
-
import_log3.log.warn("unexpected message_start", void 0, {
|
|
619
|
-
F: __dxlog_file3,
|
|
620
|
-
L: 88,
|
|
621
|
-
S: this,
|
|
622
|
-
C: (f, a) => f(...a)
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
this._message = (0, import_echo_schema.createStatic)(import_artifact2.Message, {
|
|
626
|
-
role: "assistant",
|
|
627
|
-
content: []
|
|
628
|
-
});
|
|
629
|
-
this.message.emit(this._message);
|
|
630
|
-
break;
|
|
631
|
-
}
|
|
632
|
-
case "message_delta": {
|
|
633
|
-
if (!this._message) {
|
|
634
|
-
import_log3.log.warn("unexpected message_delta", void 0, {
|
|
635
|
-
F: __dxlog_file3,
|
|
636
|
-
L: 98,
|
|
637
|
-
S: this,
|
|
638
|
-
C: (f, a) => f(...a)
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
break;
|
|
642
|
-
}
|
|
643
|
-
case "message_stop": {
|
|
644
|
-
if (!this._message) {
|
|
645
|
-
import_log3.log.warn("unexpected message_stop", void 0, {
|
|
646
|
-
F: __dxlog_file3,
|
|
647
|
-
L: 106,
|
|
648
|
-
S: this,
|
|
649
|
-
C: (f, a) => f(...a)
|
|
650
|
-
});
|
|
651
|
-
} else {
|
|
652
|
-
messagesCollected.push(this._message);
|
|
653
|
-
this._message = void 0;
|
|
654
|
-
}
|
|
655
|
-
break;
|
|
656
|
-
}
|
|
657
|
-
//
|
|
658
|
-
// Content blocks.
|
|
659
|
-
//
|
|
660
|
-
case "content_block_start": {
|
|
661
|
-
if (contentBlock) {
|
|
662
|
-
import_log3.log.warn("unexpected content_block_start", {
|
|
663
|
-
content: contentBlock
|
|
664
|
-
}, {
|
|
665
|
-
F: __dxlog_file3,
|
|
666
|
-
L: 120,
|
|
667
|
-
S: this,
|
|
668
|
-
C: (f, a) => f(...a)
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
contentBlock = event.content;
|
|
672
|
-
break;
|
|
673
|
-
}
|
|
674
|
-
case "content_block_delta": {
|
|
675
|
-
if (!contentBlock) {
|
|
676
|
-
import_log3.log.warn("unexpected content_block_delta", void 0, {
|
|
677
|
-
F: __dxlog_file3,
|
|
678
|
-
L: 130,
|
|
679
|
-
S: this,
|
|
680
|
-
C: (f, a) => f(...a)
|
|
681
|
-
});
|
|
682
|
-
break;
|
|
683
|
-
}
|
|
684
|
-
switch (event.delta.type) {
|
|
685
|
-
//
|
|
686
|
-
// Text
|
|
687
|
-
//
|
|
688
|
-
case "text_delta": {
|
|
689
|
-
const chunks = transformer.transform(event.delta.text);
|
|
690
|
-
for (const chunk of chunks) {
|
|
691
|
-
switch (streamBlock?.type) {
|
|
692
|
-
//
|
|
693
|
-
// XML Fragment.
|
|
694
|
-
//
|
|
695
|
-
case "tag": {
|
|
696
|
-
if (chunk.type === "tag") {
|
|
697
|
-
if (chunk.selfClosing) {
|
|
698
|
-
streamBlock.content.push(chunk);
|
|
699
|
-
} else if (chunk.closing) {
|
|
700
|
-
if (stack.length > 0) {
|
|
701
|
-
const top = stack.pop();
|
|
702
|
-
(0, import_invariant2.invariant)(top && top.type === "tag", void 0, {
|
|
703
|
-
F: __dxlog_file3,
|
|
704
|
-
L: 154,
|
|
705
|
-
S: this,
|
|
706
|
-
A: [
|
|
707
|
-
"top && top.type === 'tag'",
|
|
708
|
-
""
|
|
709
|
-
]
|
|
710
|
-
});
|
|
711
|
-
top.content.push(streamBlock);
|
|
712
|
-
streamBlock = top;
|
|
713
|
-
} else {
|
|
714
|
-
this._emitBlock(contentBlock, streamBlock);
|
|
715
|
-
streamBlock = void 0;
|
|
716
|
-
}
|
|
717
|
-
} else {
|
|
718
|
-
stack.push(streamBlock);
|
|
719
|
-
streamBlock = chunk;
|
|
720
|
-
}
|
|
721
|
-
} else {
|
|
722
|
-
if (streamBlock.content.length === 0) {
|
|
723
|
-
streamBlock.content.push(chunk);
|
|
724
|
-
} else {
|
|
725
|
-
const last = streamBlock.content.at(-1);
|
|
726
|
-
(0, import_invariant2.invariant)(last, void 0, {
|
|
727
|
-
F: __dxlog_file3,
|
|
728
|
-
L: 171,
|
|
729
|
-
S: this,
|
|
730
|
-
A: [
|
|
731
|
-
"last",
|
|
732
|
-
""
|
|
733
|
-
]
|
|
734
|
-
});
|
|
735
|
-
if (last.type === "text") {
|
|
736
|
-
last.content += chunk.content;
|
|
737
|
-
} else {
|
|
738
|
-
streamBlock.content.push(chunk);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
break;
|
|
743
|
-
}
|
|
744
|
-
//
|
|
745
|
-
// Text Fragment.
|
|
746
|
-
//
|
|
747
|
-
case "text": {
|
|
748
|
-
if (chunk.type === "tag") {
|
|
749
|
-
this._emitBlock(contentBlock, streamBlock);
|
|
750
|
-
if (chunk.selfClosing) {
|
|
751
|
-
this._emitBlock(contentBlock, chunk);
|
|
752
|
-
streamBlock = void 0;
|
|
753
|
-
} else {
|
|
754
|
-
streamBlock = chunk;
|
|
755
|
-
}
|
|
756
|
-
} else {
|
|
757
|
-
streamBlock.content += chunk.content;
|
|
758
|
-
}
|
|
759
|
-
break;
|
|
760
|
-
}
|
|
761
|
-
//
|
|
762
|
-
// No current chunk.
|
|
763
|
-
//
|
|
764
|
-
default: {
|
|
765
|
-
if (chunk.type === "tag" && chunk.selfClosing) {
|
|
766
|
-
this._emitBlock(contentBlock, chunk);
|
|
767
|
-
} else {
|
|
768
|
-
streamBlock = chunk;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
break;
|
|
774
|
-
}
|
|
775
|
-
//
|
|
776
|
-
// JSON
|
|
777
|
-
//
|
|
778
|
-
case "input_json_delta": {
|
|
779
|
-
if (!contentBlock) {
|
|
780
|
-
import_log3.log.warn("unexpected input_json_delta", {
|
|
781
|
-
contentBlock
|
|
782
|
-
}, {
|
|
783
|
-
F: __dxlog_file3,
|
|
784
|
-
L: 221,
|
|
785
|
-
S: this,
|
|
786
|
-
C: (f, a) => f(...a)
|
|
787
|
-
});
|
|
788
|
-
break;
|
|
789
|
-
}
|
|
790
|
-
if (streamBlock?.type === "json") {
|
|
791
|
-
streamBlock.content += event.delta.partial_json;
|
|
792
|
-
} else {
|
|
793
|
-
streamBlock = {
|
|
794
|
-
type: "json",
|
|
795
|
-
content: event.delta.partial_json
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
break;
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
break;
|
|
802
|
-
}
|
|
803
|
-
case "content_block_stop": {
|
|
804
|
-
if (!contentBlock) {
|
|
805
|
-
import_log3.log.warn("unexpected content_block_stop", void 0, {
|
|
806
|
-
F: __dxlog_file3,
|
|
807
|
-
L: 238,
|
|
808
|
-
S: this,
|
|
809
|
-
C: (f, a) => f(...a)
|
|
810
|
-
});
|
|
811
|
-
break;
|
|
812
|
-
}
|
|
813
|
-
this._emitBlock(contentBlock, streamBlock);
|
|
814
|
-
contentBlock = void 0;
|
|
815
|
-
streamBlock = void 0;
|
|
816
|
-
break;
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
if (contentBlock && streamBlock) {
|
|
820
|
-
this._emitUpdate(contentBlock, streamBlock);
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
return messagesCollected;
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
var mergeMessageBlock = (contentBlock, streamBlock) => {
|
|
827
|
-
(0, import_log3.log)("mergeMessageBlock", {
|
|
828
|
-
contentBlock,
|
|
829
|
-
streamBlock
|
|
830
|
-
}, {
|
|
831
|
-
F: __dxlog_file3,
|
|
832
|
-
L: 265,
|
|
833
|
-
S: void 0,
|
|
834
|
-
C: (f, a) => f(...a)
|
|
835
|
-
});
|
|
836
|
-
switch (streamBlock.type) {
|
|
837
|
-
//
|
|
838
|
-
// Text
|
|
839
|
-
//
|
|
840
|
-
case "text": {
|
|
841
|
-
return {
|
|
842
|
-
...contentBlock,
|
|
843
|
-
type: "text",
|
|
844
|
-
text: streamBlock.content
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
//
|
|
848
|
-
// XML
|
|
849
|
-
//
|
|
850
|
-
case "tag": {
|
|
851
|
-
switch (streamBlock.tag) {
|
|
852
|
-
case "cot": {
|
|
853
|
-
const content = streamBlock.content.map((block) => {
|
|
854
|
-
switch (block.type) {
|
|
855
|
-
case "text":
|
|
856
|
-
return block.content;
|
|
857
|
-
default:
|
|
858
|
-
return null;
|
|
859
|
-
}
|
|
860
|
-
}).filter(import_util.isNotFalsy).join("\n");
|
|
861
|
-
return {
|
|
862
|
-
...contentBlock,
|
|
863
|
-
type: "text",
|
|
864
|
-
disposition: "cot",
|
|
865
|
-
text: content
|
|
866
|
-
};
|
|
867
|
-
}
|
|
868
|
-
case "artifact": {
|
|
869
|
-
const { attributes } = streamBlock;
|
|
870
|
-
return {
|
|
871
|
-
type: "json",
|
|
872
|
-
disposition: "artifact",
|
|
873
|
-
json: JSON.stringify(attributes)
|
|
874
|
-
};
|
|
875
|
-
}
|
|
876
|
-
case "suggest": {
|
|
877
|
-
if (streamBlock.content.length === 1 && streamBlock.content[0].type === "text") {
|
|
878
|
-
return {
|
|
879
|
-
type: "json",
|
|
880
|
-
disposition: "suggest",
|
|
881
|
-
json: JSON.stringify({
|
|
882
|
-
text: streamBlock.content[0].content
|
|
883
|
-
})
|
|
884
|
-
};
|
|
885
|
-
}
|
|
886
|
-
break;
|
|
887
|
-
}
|
|
888
|
-
case "select": {
|
|
889
|
-
return {
|
|
890
|
-
type: "json",
|
|
891
|
-
disposition: "select",
|
|
892
|
-
json: JSON.stringify({
|
|
893
|
-
options: streamBlock.content.flatMap((content) => content.type === "tag" && content.content.length === 1 && content.content[0].type === "text" ? [
|
|
894
|
-
content.content[0].content
|
|
895
|
-
] : [])
|
|
896
|
-
})
|
|
897
|
-
};
|
|
898
|
-
}
|
|
899
|
-
case "tool-list": {
|
|
900
|
-
return {
|
|
901
|
-
type: "json",
|
|
902
|
-
disposition: "tool_list",
|
|
903
|
-
json: JSON.stringify({})
|
|
904
|
-
};
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
break;
|
|
908
|
-
}
|
|
909
|
-
//
|
|
910
|
-
// JSON
|
|
911
|
-
//
|
|
912
|
-
case "json": {
|
|
913
|
-
switch (contentBlock.type) {
|
|
914
|
-
case "tool_use": {
|
|
915
|
-
return {
|
|
916
|
-
...contentBlock,
|
|
917
|
-
input: (0, import_util.safeParseJson)(streamBlock.content) ?? {}
|
|
918
|
-
};
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
return {
|
|
922
|
-
...contentBlock,
|
|
923
|
-
type: "json",
|
|
924
|
-
disposition: "artifact",
|
|
925
|
-
json: streamBlock.content
|
|
926
|
-
};
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
};
|
|
930
|
-
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
931
|
-
var LLMModel = import_schema2.Schema.Literal(...DEFAULT_LLM_MODELS);
|
|
932
|
-
var ToolTypes = Object.freeze({
|
|
933
|
-
// TODO(dmaretskyi): Not implemented yet.
|
|
934
|
-
// DatabaseQuery: 'database_query',
|
|
935
|
-
TextToImage: "text_to_image"
|
|
936
|
-
});
|
|
937
|
-
var GenerateRequest = import_schema2.Schema.Struct({
|
|
938
|
-
spaceId: import_schema2.Schema.optional(import_artifact3.SpaceIdSchema),
|
|
939
|
-
threadId: import_schema2.Schema.optional(import_echo_schema2.ObjectId),
|
|
940
|
-
/**
|
|
941
|
-
* Preferred model or system default.
|
|
942
|
-
*/
|
|
943
|
-
model: import_schema2.Schema.optional(LLMModel),
|
|
944
|
-
/**
|
|
945
|
-
* Tools available for the LLM.
|
|
946
|
-
*/
|
|
947
|
-
tools: import_schema2.Schema.optional(import_schema2.Schema.Array(import_artifact3.Tool).pipe(import_schema2.Schema.mutable)),
|
|
948
|
-
/**
|
|
949
|
-
* System instructions to the LLM.
|
|
950
|
-
*/
|
|
951
|
-
systemPrompt: import_schema2.Schema.optional(import_schema2.Schema.String),
|
|
952
|
-
/**
|
|
953
|
-
* History of messages to include in the context window.
|
|
954
|
-
*/
|
|
955
|
-
// TODO(burdon): Rename messages.
|
|
956
|
-
history: import_schema2.Schema.optional(import_schema2.Schema.Array(import_artifact3.Message)),
|
|
957
|
-
/**
|
|
958
|
-
* Current request.
|
|
959
|
-
*/
|
|
960
|
-
prompt: import_schema2.Schema.optional(import_artifact3.Message)
|
|
961
|
-
});
|
|
962
|
-
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
|
|
963
|
-
var runLLM = async (params) => {
|
|
964
|
-
let conversationResult = null;
|
|
965
|
-
const history = params.history ?? [];
|
|
966
|
-
const generate = async () => {
|
|
967
|
-
(0, import_log4.log)("llm generate", {
|
|
968
|
-
tools: params.tools
|
|
969
|
-
}, {
|
|
970
|
-
F: __dxlog_file4,
|
|
971
|
-
L: 44,
|
|
972
|
-
S: void 0,
|
|
973
|
-
C: (f, a) => f(...a)
|
|
974
|
-
});
|
|
975
|
-
const beginTs = Date.now();
|
|
976
|
-
const stream = await params.client.exec({
|
|
977
|
-
model: params.model,
|
|
978
|
-
spaceId: params.spaceId,
|
|
979
|
-
threadId: params.threadId,
|
|
980
|
-
history,
|
|
981
|
-
systemPrompt: params.system,
|
|
982
|
-
tools: params.tools
|
|
983
|
-
});
|
|
984
|
-
for await (const event of stream) {
|
|
985
|
-
params.logger?.(event);
|
|
986
|
-
}
|
|
987
|
-
await stream.complete();
|
|
988
|
-
const messages = [];
|
|
989
|
-
const message = messages.at(-1);
|
|
990
|
-
(0, import_invariant3.invariant)(message, void 0, {
|
|
991
|
-
F: __dxlog_file4,
|
|
992
|
-
L: 63,
|
|
993
|
-
S: void 0,
|
|
994
|
-
A: [
|
|
995
|
-
"message",
|
|
996
|
-
""
|
|
997
|
-
]
|
|
998
|
-
});
|
|
999
|
-
(0, import_log4.log)("llm result", {
|
|
1000
|
-
time: Date.now() - beginTs,
|
|
1001
|
-
message
|
|
1002
|
-
}, {
|
|
1003
|
-
F: __dxlog_file4,
|
|
1004
|
-
L: 65,
|
|
1005
|
-
S: void 0,
|
|
1006
|
-
C: (f, a) => f(...a)
|
|
1007
|
-
});
|
|
1008
|
-
(0, import_invariant3.invariant)(message, void 0, {
|
|
1009
|
-
F: __dxlog_file4,
|
|
1010
|
-
L: 66,
|
|
1011
|
-
S: void 0,
|
|
1012
|
-
A: [
|
|
1013
|
-
"message",
|
|
1014
|
-
""
|
|
1015
|
-
]
|
|
1016
|
-
});
|
|
1017
|
-
params.logger?.({
|
|
1018
|
-
type: "message",
|
|
1019
|
-
message
|
|
1020
|
-
});
|
|
1021
|
-
history.push(...messages.map((message2) => ({
|
|
1022
|
-
...message2,
|
|
1023
|
-
content: message2.content.filter((block) => block.type !== "image")
|
|
1024
|
-
})));
|
|
1025
|
-
const isToolUse2 = message.content.at(-1)?.type === "tool_use";
|
|
1026
|
-
if (isToolUse2) {
|
|
1027
|
-
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
1028
|
-
(0, import_invariant3.invariant)(toolCalls.length === 1, void 0, {
|
|
1029
|
-
F: __dxlog_file4,
|
|
1030
|
-
L: 77,
|
|
1031
|
-
S: void 0,
|
|
1032
|
-
A: [
|
|
1033
|
-
"toolCalls.length === 1",
|
|
1034
|
-
""
|
|
1035
|
-
]
|
|
1036
|
-
});
|
|
1037
|
-
const toolCall = toolCalls[0];
|
|
1038
|
-
const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
|
|
1039
|
-
if (!tool) {
|
|
1040
|
-
throw new Error(`Tool not found: ${toolCall.name}`);
|
|
1041
|
-
}
|
|
1042
|
-
(0, import_invariant3.invariant)(tool.execute, void 0, {
|
|
1043
|
-
F: __dxlog_file4,
|
|
1044
|
-
L: 84,
|
|
1045
|
-
S: void 0,
|
|
1046
|
-
A: [
|
|
1047
|
-
"tool.execute",
|
|
1048
|
-
""
|
|
1049
|
-
]
|
|
1050
|
-
});
|
|
1051
|
-
const toolResult = await tool.execute(toolCall.input);
|
|
1052
|
-
switch (toolResult.kind) {
|
|
1053
|
-
case "error": {
|
|
1054
|
-
import_log4.log.warn("tool error", {
|
|
1055
|
-
message: toolResult.message
|
|
1056
|
-
}, {
|
|
1057
|
-
F: __dxlog_file4,
|
|
1058
|
-
L: 88,
|
|
1059
|
-
S: void 0,
|
|
1060
|
-
C: (f, a) => f(...a)
|
|
1061
|
-
});
|
|
1062
|
-
const resultMessage = {
|
|
1063
|
-
id: import_echo_schema3.ObjectId.random(),
|
|
1064
|
-
spaceId: params.spaceId,
|
|
1065
|
-
threadId: params.threadId,
|
|
1066
|
-
role: "user",
|
|
1067
|
-
content: [
|
|
1068
|
-
{
|
|
1069
|
-
type: "tool_result",
|
|
1070
|
-
toolUseId: toolCall.id,
|
|
1071
|
-
content: toolResult.message,
|
|
1072
|
-
isError: true
|
|
1073
|
-
}
|
|
1074
|
-
]
|
|
1075
|
-
};
|
|
1076
|
-
params.logger?.({
|
|
1077
|
-
type: "message",
|
|
1078
|
-
message: resultMessage
|
|
1079
|
-
});
|
|
1080
|
-
history.push(resultMessage);
|
|
1081
|
-
return true;
|
|
1082
|
-
}
|
|
1083
|
-
case "success": {
|
|
1084
|
-
(0, import_log4.log)("tool success", {
|
|
1085
|
-
result: toolResult.result
|
|
1086
|
-
}, {
|
|
1087
|
-
F: __dxlog_file4,
|
|
1088
|
-
L: 110,
|
|
1089
|
-
S: void 0,
|
|
1090
|
-
C: (f, a) => f(...a)
|
|
1091
|
-
});
|
|
1092
|
-
const resultMessage = {
|
|
1093
|
-
id: import_echo_schema3.ObjectId.random(),
|
|
1094
|
-
spaceId: params.spaceId,
|
|
1095
|
-
threadId: params.threadId,
|
|
1096
|
-
role: "user",
|
|
1097
|
-
content: [
|
|
1098
|
-
{
|
|
1099
|
-
type: "tool_result",
|
|
1100
|
-
toolUseId: toolCall.id,
|
|
1101
|
-
content: JSON.stringify(toolResult.result)
|
|
1102
|
-
}
|
|
1103
|
-
]
|
|
1104
|
-
};
|
|
1105
|
-
params.logger?.({
|
|
1106
|
-
type: "message",
|
|
1107
|
-
message: resultMessage
|
|
1108
|
-
});
|
|
1109
|
-
history.push(resultMessage);
|
|
1110
|
-
return true;
|
|
1111
|
-
}
|
|
1112
|
-
case "break": {
|
|
1113
|
-
(0, import_log4.log)("tool break", {
|
|
1114
|
-
result: toolResult.result
|
|
1115
|
-
}, {
|
|
1116
|
-
F: __dxlog_file4,
|
|
1117
|
-
L: 131,
|
|
1118
|
-
S: void 0,
|
|
1119
|
-
C: (f, a) => f(...a)
|
|
1120
|
-
});
|
|
1121
|
-
conversationResult = toolResult.result;
|
|
1122
|
-
return false;
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
return false;
|
|
1127
|
-
};
|
|
1128
|
-
while (await generate()) {
|
|
1129
|
-
}
|
|
1130
|
-
return {
|
|
1131
|
-
result: conversationResult,
|
|
1132
|
-
history
|
|
1133
|
-
};
|
|
1134
|
-
};
|
|
1135
|
-
var isToolUse = (message) => {
|
|
1136
|
-
const block = message.content.at(-1);
|
|
1137
|
-
(0, import_invariant3.invariant)(block, void 0, {
|
|
1138
|
-
F: __dxlog_file4,
|
|
1139
|
-
L: 152,
|
|
1140
|
-
S: void 0,
|
|
1141
|
-
A: [
|
|
1142
|
-
"block",
|
|
1143
|
-
""
|
|
1144
|
-
]
|
|
1145
|
-
});
|
|
1146
|
-
return block.type === "tool_use";
|
|
1147
|
-
};
|
|
1148
|
-
var runTools = async ({ message, tools, extensions }) => {
|
|
1149
|
-
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
1150
|
-
(0, import_invariant3.invariant)(toolCalls.length === 1, void 0, {
|
|
1151
|
-
F: __dxlog_file4,
|
|
1152
|
-
L: 174,
|
|
1153
|
-
S: void 0,
|
|
1154
|
-
A: [
|
|
1155
|
-
"toolCalls.length === 1",
|
|
1156
|
-
""
|
|
1157
|
-
]
|
|
1158
|
-
});
|
|
1159
|
-
const toolCall = toolCalls[0];
|
|
1160
|
-
const tool = tools.find((tool2) => tool2.name === toolCall.name);
|
|
1161
|
-
if (!tool) {
|
|
1162
|
-
throw new Error(`Tool not found: ${toolCall.name}`);
|
|
1163
|
-
}
|
|
1164
|
-
let toolResult;
|
|
1165
|
-
try {
|
|
1166
|
-
(0, import_invariant3.invariant)(tool.execute, void 0, {
|
|
1167
|
-
F: __dxlog_file4,
|
|
1168
|
-
L: 183,
|
|
1169
|
-
S: void 0,
|
|
1170
|
-
A: [
|
|
1171
|
-
"tool.execute",
|
|
1172
|
-
""
|
|
1173
|
-
]
|
|
1174
|
-
});
|
|
1175
|
-
toolResult = await tool.execute(toolCall.input, {
|
|
1176
|
-
extensions
|
|
1177
|
-
});
|
|
1178
|
-
} catch (error) {
|
|
1179
|
-
(0, import_log4.log)("tool error", {
|
|
1180
|
-
error
|
|
1181
|
-
}, {
|
|
1182
|
-
F: __dxlog_file4,
|
|
1183
|
-
L: 186,
|
|
1184
|
-
S: void 0,
|
|
1185
|
-
C: (f, a) => f(...a)
|
|
1186
|
-
});
|
|
1187
|
-
toolResult = import_artifact4.ToolResult.Error(error.message);
|
|
1188
|
-
}
|
|
1189
|
-
switch (toolResult.kind) {
|
|
1190
|
-
case "error": {
|
|
1191
|
-
(0, import_log4.log)("tool error", {
|
|
1192
|
-
message: toolResult.message
|
|
1193
|
-
}, {
|
|
1194
|
-
F: __dxlog_file4,
|
|
1195
|
-
L: 192,
|
|
1196
|
-
S: void 0,
|
|
1197
|
-
C: (f, a) => f(...a)
|
|
1198
|
-
});
|
|
1199
|
-
const resultMessage = (0, import_echo_schema3.createStatic)(import_artifact4.Message, {
|
|
1200
|
-
role: "user",
|
|
1201
|
-
content: [
|
|
1202
|
-
{
|
|
1203
|
-
type: "tool_result",
|
|
1204
|
-
toolUseId: toolCall.id,
|
|
1205
|
-
content: toolResult.message,
|
|
1206
|
-
isError: true
|
|
1207
|
-
}
|
|
1208
|
-
]
|
|
1209
|
-
});
|
|
1210
|
-
return {
|
|
1211
|
-
type: "continue",
|
|
1212
|
-
message: resultMessage
|
|
1213
|
-
};
|
|
1214
|
-
}
|
|
1215
|
-
case "success": {
|
|
1216
|
-
(0, import_log4.log)("tool success", {
|
|
1217
|
-
result: toolResult.result
|
|
1218
|
-
}, {
|
|
1219
|
-
F: __dxlog_file4,
|
|
1220
|
-
L: 213,
|
|
1221
|
-
S: void 0,
|
|
1222
|
-
C: (f, a) => f(...a)
|
|
1223
|
-
});
|
|
1224
|
-
const resultMessage = (0, import_echo_schema3.createStatic)(import_artifact4.Message, {
|
|
1225
|
-
role: "user",
|
|
1226
|
-
content: [
|
|
1227
|
-
{
|
|
1228
|
-
type: "tool_result",
|
|
1229
|
-
toolUseId: toolCall.id,
|
|
1230
|
-
content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result) ?? ""
|
|
1231
|
-
}
|
|
1232
|
-
]
|
|
1233
|
-
});
|
|
1234
|
-
return {
|
|
1235
|
-
type: "continue",
|
|
1236
|
-
message: resultMessage
|
|
1237
|
-
};
|
|
1238
|
-
}
|
|
1239
|
-
case "break": {
|
|
1240
|
-
(0, import_log4.log)("tool break", {
|
|
1241
|
-
result: toolResult.result
|
|
1242
|
-
}, {
|
|
1243
|
-
F: __dxlog_file4,
|
|
1244
|
-
L: 233,
|
|
1245
|
-
S: void 0,
|
|
1246
|
-
C: (f, a) => f(...a)
|
|
1247
|
-
});
|
|
1248
|
-
return {
|
|
1249
|
-
type: "break",
|
|
1250
|
-
result: toolResult.result
|
|
1251
|
-
};
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
};
|
|
52
|
+
var import_chunk_4ZDZUCAS = require("./chunk-4ZDZUCAS.cjs");
|
|
1255
53
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1256
54
|
0 && (module.exports = {
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
55
|
+
AIServiceEdgeClient,
|
|
56
|
+
DEFAULT_EDGE_MODEL,
|
|
57
|
+
DEFAULT_EDGE_MODELS,
|
|
58
|
+
DEFAULT_OLLAMA_ENDPOINT,
|
|
59
|
+
DEFAULT_OLLAMA_MODEL,
|
|
60
|
+
DEFAULT_OLLAMA_MODELS,
|
|
1260
61
|
EchoDataSource,
|
|
1261
62
|
GenerateRequest,
|
|
1262
|
-
|
|
63
|
+
GenerationStreamImpl,
|
|
1263
64
|
LLMModel,
|
|
65
|
+
MessageCollector,
|
|
1264
66
|
MixedStreamParser,
|
|
67
|
+
OllamaClient,
|
|
1265
68
|
StreamTransform,
|
|
1266
69
|
ToolTypes,
|
|
1267
70
|
createArtifactElement,
|
|
1268
71
|
createGenerationStream,
|
|
72
|
+
emitMessageAsEvents,
|
|
1269
73
|
executeQuery,
|
|
1270
74
|
executeQueryPlan,
|
|
1271
75
|
formatInferredRelationshipLabel,
|