@dxos/assistant 0.8.2-staging.7ac8446 → 0.8.2
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/index.mjs +1830 -228
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +1825 -229
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1831 -228
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/{src/bin → bin}/chat.d.ts.map +1 -1
- package/dist/types/src/blueprint/blueprint.d.ts +79 -0
- package/dist/types/src/blueprint/blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprint/blueprint.test.d.ts +2 -0
- package/dist/types/src/blueprint/blueprint.test.d.ts.map +1 -0
- package/dist/types/src/blueprint/index.d.ts +4 -0
- package/dist/types/src/blueprint/index.d.ts.map +1 -0
- package/dist/types/src/blueprint/logger.d.ts +3 -0
- package/dist/types/src/blueprint/logger.d.ts.map +1 -0
- package/dist/types/src/blueprint/machine.d.ts +113 -0
- package/dist/types/src/blueprint/machine.d.ts.map +1 -0
- package/dist/types/src/blueprint/test-data.d.ts +3 -0
- package/dist/types/src/blueprint/test-data.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +4 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +2 -0
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +210 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +3 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +14 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction.d.ts +242 -0
- package/dist/types/src/extraction/extraction.d.ts.map +1 -0
- package/dist/types/src/extraction/index.d.ts +5 -0
- package/dist/types/src/extraction/index.d.ts.map +1 -0
- package/dist/types/src/extraction/quotes.d.ts +15 -0
- package/dist/types/src/extraction/quotes.d.ts.map +1 -0
- package/dist/types/src/extraction/quotes.test.d.ts +2 -0
- package/dist/types/src/extraction/quotes.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/research/exa.d.ts +7 -0
- package/dist/types/src/research/exa.d.ts.map +1 -0
- package/dist/types/src/research/graph.d.ts +39 -0
- package/dist/types/src/research/graph.d.ts.map +1 -0
- package/dist/types/src/research/graph.test.d.ts +2 -0
- package/dist/types/src/research/graph.test.d.ts.map +1 -0
- package/dist/types/src/research/index.d.ts +4 -0
- package/dist/types/src/research/index.d.ts.map +1 -0
- package/dist/types/src/research/research.d.ts +10 -0
- package/dist/types/src/research/research.d.ts.map +1 -0
- package/dist/types/src/research/research.test.d.ts +2 -0
- package/dist/types/src/research/research.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +1 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +80 -35
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +21 -0
- package/dist/types/src/session/version-pin.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.test.d.ts +2 -0
- package/dist/types/src/session/version-pin.test.d.ts.map +1 -0
- package/dist/types/src/summarization/summarization.test.d.ts +2 -0
- package/dist/types/src/summarization/summarization.test.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +38 -0
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +37 -0
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +58 -0
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -6
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +3 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +25 -35
- package/src/blueprint/blueprint.test.ts +142 -0
- package/src/blueprint/blueprint.ts +95 -0
- package/src/blueprint/index.ts +7 -0
- package/src/blueprint/logger.ts +66 -0
- package/src/blueprint/machine.ts +208 -0
- package/src/blueprint/test-data.ts +55 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +95 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +64 -0
- package/src/extraction/extraction-llm-function/index.ts +5 -0
- package/src/extraction/extraction-llm-function/instructions.tpl +28 -0
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +115 -0
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +54 -0
- package/src/extraction/extraction-ner-function/index.ts +6 -0
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +94 -0
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +97 -0
- package/src/extraction/extraction.ts +75 -0
- package/src/extraction/index.ts +8 -0
- package/src/extraction/quotes.test.ts +27 -0
- package/src/extraction/quotes.ts +57 -0
- package/src/index.ts +4 -3
- package/src/research/exa.ts +100 -0
- package/src/research/graph.test.ts +15 -0
- package/src/research/graph.ts +291 -0
- package/src/research/index.ts +7 -0
- package/src/research/instructions-research.tpl +97 -0
- package/src/research/research.test.ts +189 -0
- package/src/research/research.ts +89 -0
- package/src/session/index.ts +1 -0
- package/src/session/session.test.ts +39 -67
- package/src/session/session.ts +328 -155
- package/src/session/version-pin.test.ts +26 -0
- package/src/session/version-pin.ts +48 -0
- package/src/summarization/summarization.test.ts +216 -0
- package/src/testing/exa-search-1748337321991.ts +131 -0
- package/src/testing/exa-search-1748337331526.ts +144 -0
- package/src/testing/exa-search-1748337344119.ts +133 -0
- package/src/testing/index.ts +8 -7
- package/src/typedefs.d.ts +8 -0
- package/src/types.ts +8 -0
- package/src/typings.d.ts +8 -0
- package/dist/lib/browser/chunk-PND5VR6D.mjs +0 -2315
- package/dist/lib/browser/chunk-PND5VR6D.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -621
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node/chunk-IBOBRBDF.cjs +0 -2326
- package/dist/lib/node/chunk-IBOBRBDF.cjs.map +0 -7
- package/dist/lib/node/testing/index.cjs +0 -637
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-DCQJOLSR.mjs +0 -2317
- package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -622
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/ai-service/client.test.d.ts +0 -2
- package/dist/types/src/ai-service/client.test.d.ts.map +0 -1
- package/dist/types/src/ai-service/defs.d.ts +0 -9
- package/dist/types/src/ai-service/defs.d.ts.map +0 -1
- package/dist/types/src/ai-service/edge-client.d.ts +0 -19
- package/dist/types/src/ai-service/edge-client.d.ts.map +0 -1
- package/dist/types/src/ai-service/index.d.ts +0 -10
- package/dist/types/src/ai-service/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/message-collector.d.ts +0 -88
- package/dist/types/src/ai-service/message-collector.d.ts.map +0 -1
- package/dist/types/src/ai-service/ollama-client.d.ts +0 -39
- package/dist/types/src/ai-service/ollama-client.d.ts.map +0 -1
- package/dist/types/src/ai-service/parser.d.ts +0 -138
- package/dist/types/src/ai-service/parser.d.ts.map +0 -1
- package/dist/types/src/ai-service/service.d.ts +0 -9
- package/dist/types/src/ai-service/service.d.ts.map +0 -1
- package/dist/types/src/ai-service/stream.d.ts +0 -40
- package/dist/types/src/ai-service/stream.d.ts.map +0 -1
- package/dist/types/src/ai-service/stream.test.d.ts +0 -2
- package/dist/types/src/ai-service/stream.test.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/index.d.ts +0 -3
- package/dist/types/src/ai-service/testing/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/mock.d.ts +0 -7
- package/dist/types/src/ai-service/testing/mock.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/test-stream.d.ts +0 -7
- package/dist/types/src/ai-service/testing/test-stream.d.ts.map +0 -1
- package/dist/types/src/ai-service/transform.d.ts +0 -27
- package/dist/types/src/ai-service/transform.d.ts.map +0 -1
- package/dist/types/src/ai-service/types.d.ts +0 -167
- package/dist/types/src/ai-service/types.d.ts.map +0 -1
- package/dist/types/src/ai-service/util/index.d.ts +0 -2
- package/dist/types/src/ai-service/util/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/util/sse.d.ts +0 -39
- package/dist/types/src/ai-service/util/sse.d.ts.map +0 -1
- package/dist/types/src/conversation/conversation.d.ts +0 -80
- package/dist/types/src/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/conversation/conversation.test.d.ts +0 -2
- package/dist/types/src/conversation/conversation.test.d.ts.map +0 -1
- package/dist/types/src/conversation/index.d.ts +0 -2
- package/dist/types/src/conversation/index.d.ts.map +0 -1
- package/dist/types/src/conversation/types.d.ts +0 -79
- package/dist/types/src/conversation/types.d.ts.map +0 -1
- package/dist/types/src/cypher/ast.d.ts +0 -56
- package/dist/types/src/cypher/ast.d.ts.map +0 -1
- package/dist/types/src/cypher/combinators.d.ts +0 -14
- package/dist/types/src/cypher/combinators.d.ts.map +0 -1
- package/dist/types/src/cypher/combinators.test.d.ts +0 -2
- package/dist/types/src/cypher/combinators.test.d.ts.map +0 -1
- package/dist/types/src/cypher/echo-data-source.d.ts +0 -16
- package/dist/types/src/cypher/echo-data-source.d.ts.map +0 -1
- package/dist/types/src/cypher/execution-plan.d.ts +0 -49
- package/dist/types/src/cypher/execution-plan.d.ts.map +0 -1
- package/dist/types/src/cypher/execution-plan.test.d.ts +0 -2
- package/dist/types/src/cypher/execution-plan.test.d.ts.map +0 -1
- package/dist/types/src/cypher/index.d.ts +0 -5
- package/dist/types/src/cypher/index.d.ts.map +0 -1
- package/dist/types/src/cypher/parjs.d.ts +0 -4
- package/dist/types/src/cypher/parjs.d.ts.map +0 -1
- package/dist/types/src/cypher/parser.d.ts +0 -6
- package/dist/types/src/cypher/parser.d.ts.map +0 -1
- package/dist/types/src/cypher/query-executor.d.ts +0 -26
- package/dist/types/src/cypher/query-executor.d.ts.map +0 -1
- package/dist/types/src/cypher/query-executor.test.d.ts +0 -2
- package/dist/types/src/cypher/query-executor.test.d.ts.map +0 -1
- package/dist/types/src/cypher/schema.d.ts +0 -12
- package/dist/types/src/cypher/schema.d.ts.map +0 -1
- package/dist/types/src/cypher/schema.test.d.ts +0 -2
- package/dist/types/src/cypher/schema.test.d.ts.map +0 -1
- package/dist/types/src/testing/artifacts.test.d.ts +0 -1
- package/dist/types/src/testing/artifacts.test.d.ts.map +0 -1
- package/dist/types/src/testing/data.d.ts +0 -2
- package/dist/types/src/testing/data.d.ts.map +0 -1
- package/dist/types/src/testing/defs.d.ts +0 -5
- package/dist/types/src/testing/defs.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -12
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/object-extraction.test.d.ts +0 -1
- package/dist/types/src/testing/object-extraction.test.d.ts.map +0 -1
- package/dist/types/src/testing/ollama.d.ts +0 -6
- package/dist/types/src/testing/ollama.d.ts.map +0 -1
- package/dist/types/src/testing/query-promts.d.ts +0 -5
- package/dist/types/src/testing/query-promts.d.ts.map +0 -1
- package/dist/types/src/testing/query.test.d.ts +0 -2
- package/dist/types/src/testing/query.test.d.ts.map +0 -1
- package/dist/types/src/testing/test-data.d.ts +0 -29
- package/dist/types/src/testing/test-data.d.ts.map +0 -1
- package/dist/types/src/testing/test-schema.d.ts +0 -50
- package/dist/types/src/testing/test-schema.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -2
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/trim.d.ts +0 -5
- package/dist/types/src/util/trim.d.ts.map +0 -1
- package/src/ai-service/client.test.ts +0 -265
- package/src/ai-service/defs.ts +0 -39
- package/src/ai-service/edge-client.ts +0 -67
- package/src/ai-service/index.ts +0 -13
- package/src/ai-service/message-collector.ts +0 -84
- package/src/ai-service/ollama-client.ts +0 -499
- package/src/ai-service/parser.ts +0 -358
- package/src/ai-service/service.ts +0 -15
- package/src/ai-service/stream.test.ts +0 -125
- package/src/ai-service/stream.ts +0 -118
- package/src/ai-service/testing/index.ts +0 -6
- package/src/ai-service/testing/mock.ts +0 -22
- package/src/ai-service/testing/test-stream.ts +0 -93
- package/src/ai-service/transform.ts +0 -121
- package/src/ai-service/types.ts +0 -103
- package/src/ai-service/util/index.ts +0 -5
- package/src/ai-service/util/sse.ts +0 -341
- package/src/bin/chat.ts +0 -79
- package/src/conversation/conversation.test.ts +0 -73
- package/src/conversation/conversation.ts +0 -257
- package/src/conversation/index.ts +0 -5
- package/src/conversation/types.ts +0 -129
- package/src/cypher/ast.ts +0 -75
- package/src/cypher/combinators.test.ts +0 -137
- package/src/cypher/combinators.ts +0 -148
- package/src/cypher/echo-data-source.ts +0 -105
- package/src/cypher/execution-plan.test.ts +0 -44
- package/src/cypher/execution-plan.ts +0 -208
- package/src/cypher/index.ts +0 -8
- package/src/cypher/parjs-shims.d.ts +0 -10
- package/src/cypher/parjs.ts +0 -10
- package/src/cypher/parser.ts +0 -19
- package/src/cypher/query-executor.test.ts +0 -47
- package/src/cypher/query-executor.ts +0 -145
- package/src/cypher/schema.test.ts +0 -17
- package/src/cypher/schema.ts +0 -86
- package/src/testing/artifacts.test.ts +0 -182
- package/src/testing/data.ts +0 -13
- package/src/testing/defs.ts +0 -9
- package/src/testing/logger.ts +0 -129
- package/src/testing/object-extraction.test.ts +0 -71
- package/src/testing/ollama.ts +0 -15
- package/src/testing/query-promts.ts +0 -75
- package/src/testing/query.test.ts +0 -91
- package/src/testing/test-data.ts +0 -372
- package/src/testing/test-schema.ts +0 -80
- package/src/util/index.ts +0 -5
- package/src/util/trim.ts +0 -15
- package/vendor/parjs-browser.mjs +0 -4
- package/vendor/parjs-default.mjs +0 -8
- /package/dist/types/{src/bin → bin}/chat.d.ts +0 -0
|
@@ -1,2326 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
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
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var chunk_IBOBRBDF_exports = {};
|
|
30
|
-
__export(chunk_IBOBRBDF_exports, {
|
|
31
|
-
AIServiceEdgeClient: () => AIServiceEdgeClient,
|
|
32
|
-
DEFAULT_EDGE_MODEL: () => DEFAULT_EDGE_MODEL,
|
|
33
|
-
DEFAULT_EDGE_MODELS: () => DEFAULT_EDGE_MODELS,
|
|
34
|
-
DEFAULT_OLLAMA_ENDPOINT: () => DEFAULT_OLLAMA_ENDPOINT,
|
|
35
|
-
DEFAULT_OLLAMA_MODEL: () => DEFAULT_OLLAMA_MODEL,
|
|
36
|
-
DEFAULT_OLLAMA_MODELS: () => DEFAULT_OLLAMA_MODELS,
|
|
37
|
-
EchoDataSource: () => EchoDataSource,
|
|
38
|
-
GenerateRequest: () => GenerateRequest,
|
|
39
|
-
GenerationStreamImpl: () => GenerationStreamImpl,
|
|
40
|
-
LLMModel: () => LLMModel,
|
|
41
|
-
MessageCollector: () => MessageCollector,
|
|
42
|
-
MixedStreamParser: () => MixedStreamParser,
|
|
43
|
-
OllamaClient: () => OllamaClient,
|
|
44
|
-
StreamTransform: () => StreamTransform,
|
|
45
|
-
ToolTypes: () => ToolTypes,
|
|
46
|
-
createArtifactElement: () => createArtifactElement,
|
|
47
|
-
createGenerationStream: () => createGenerationStream,
|
|
48
|
-
emitMessageAsEvents: () => emitMessageAsEvents,
|
|
49
|
-
executeQuery: () => executeQuery,
|
|
50
|
-
executeQueryPlan: () => executeQueryPlan,
|
|
51
|
-
formatInferredRelationshipLabel: () => formatInferredRelationshipLabel,
|
|
52
|
-
formatJsonSchemaForLLM: () => formatJsonSchemaForLLM,
|
|
53
|
-
formatNodeLabel: () => formatNodeLabel,
|
|
54
|
-
isReference: () => isReference,
|
|
55
|
-
isToolUse: () => isToolUse,
|
|
56
|
-
mergeMessageBlock: () => mergeMessageBlock,
|
|
57
|
-
parseCypherQuery: () => parseCypherQuery,
|
|
58
|
-
runLLM: () => runLLM,
|
|
59
|
-
runTools: () => runTools,
|
|
60
|
-
trim: () => trim
|
|
61
|
-
});
|
|
62
|
-
module.exports = __toCommonJS(chunk_IBOBRBDF_exports);
|
|
63
|
-
var import_async = require("@dxos/async");
|
|
64
|
-
var import_log = require("@dxos/log");
|
|
65
|
-
var import_effect = require("effect");
|
|
66
|
-
var import_artifact = require("@dxos/artifact");
|
|
67
|
-
var import_invariant = require("@dxos/invariant");
|
|
68
|
-
var import_log2 = require("@dxos/log");
|
|
69
|
-
var import_effect2 = require("effect");
|
|
70
|
-
var import_artifact2 = require("@dxos/artifact");
|
|
71
|
-
var import_echo_schema = require("@dxos/echo-schema");
|
|
72
|
-
var import_artifact3 = require("@dxos/artifact");
|
|
73
|
-
var import_echo_schema2 = require("@dxos/echo-schema");
|
|
74
|
-
var import_invariant2 = require("@dxos/invariant");
|
|
75
|
-
var import_log3 = require("@dxos/log");
|
|
76
|
-
var import_effect3 = require("effect");
|
|
77
|
-
var import_artifact4 = require("@dxos/artifact");
|
|
78
|
-
var import_echo_schema3 = require("@dxos/echo-schema");
|
|
79
|
-
var import_invariant3 = require("@dxos/invariant");
|
|
80
|
-
var import_log4 = require("@dxos/log");
|
|
81
|
-
var P = __toESM(require("parsimmon"));
|
|
82
|
-
var import_artifact5 = require("@dxos/artifact");
|
|
83
|
-
var import_async2 = require("@dxos/async");
|
|
84
|
-
var import_echo_schema4 = require("@dxos/echo-schema");
|
|
85
|
-
var import_invariant4 = require("@dxos/invariant");
|
|
86
|
-
var import_log5 = require("@dxos/log");
|
|
87
|
-
var import_util = require("@dxos/util");
|
|
88
|
-
var import_parjs = require("#parjs");
|
|
89
|
-
var import_log6 = require("@dxos/log");
|
|
90
|
-
var import_util2 = require("@dxos/util");
|
|
91
|
-
var import_invariant5 = require("@dxos/invariant");
|
|
92
|
-
var import_log7 = require("@dxos/log");
|
|
93
|
-
var import_debug = require("@dxos/debug");
|
|
94
|
-
var import_echo_schema5 = require("@dxos/echo-schema");
|
|
95
|
-
var import_debug2 = require("@dxos/debug");
|
|
96
|
-
var import_echo_db = require("@dxos/echo-db");
|
|
97
|
-
var import_echo_schema6 = require("@dxos/echo-schema");
|
|
98
|
-
var import_live_object = require("@dxos/live-object");
|
|
99
|
-
var import_log8 = require("@dxos/log");
|
|
100
|
-
var DEFAULT_EDGE_MODEL = "@anthropic/claude-3-5-sonnet-20241022";
|
|
101
|
-
var DEFAULT_EDGE_MODELS = [
|
|
102
|
-
// AI Gateway.
|
|
103
|
-
// https://developers.cloudflare.com/ai-gateway
|
|
104
|
-
"@anthropic/claude-3-5-haiku-20241022",
|
|
105
|
-
"@anthropic/claude-3-5-sonnet-20241022",
|
|
106
|
-
"@anthropic/claude-3-7-sonnet-20250219",
|
|
107
|
-
// Workers AI.
|
|
108
|
-
// https://developers.cloudflare.com/workers-ai/models
|
|
109
|
-
"@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
|
|
110
|
-
"@hf/nousresearch/hermes-2-pro-mistral-7b",
|
|
111
|
-
"@ollama/llama-3-1-nemotron-70b-instruct",
|
|
112
|
-
"@ollama/llama-3-1-nemotron-mini-4b-instruct",
|
|
113
|
-
"@ollama/llama-3-2-3b"
|
|
114
|
-
];
|
|
115
|
-
var DEFAULT_OLLAMA_ENDPOINT = "http://localhost:11434";
|
|
116
|
-
var DEFAULT_OLLAMA_MODEL = "llama3.2:1b";
|
|
117
|
-
var DEFAULT_OLLAMA_MODELS = [
|
|
118
|
-
//
|
|
119
|
-
"llama3.2:1b",
|
|
120
|
-
"llama3:70b",
|
|
121
|
-
"deepseek-r1:latest"
|
|
122
|
-
];
|
|
123
|
-
async function* iterSSEMessages(response, controller) {
|
|
124
|
-
if (!response.body) {
|
|
125
|
-
controller.abort();
|
|
126
|
-
throw new Error("Attempted to iterate over a response with no body");
|
|
127
|
-
}
|
|
128
|
-
const sseDecoder = new SSEDecoder();
|
|
129
|
-
const lineDecoder = new LineDecoder();
|
|
130
|
-
const iter = readableStreamAsyncIterable(response.body);
|
|
131
|
-
for await (const sseChunk of iterSSEChunks(iter)) {
|
|
132
|
-
for (const line of lineDecoder.decode(sseChunk)) {
|
|
133
|
-
const sse = sseDecoder.decode(line);
|
|
134
|
-
if (sse) {
|
|
135
|
-
yield sse;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
for (const line of lineDecoder.flush()) {
|
|
140
|
-
const sse = sseDecoder.decode(line);
|
|
141
|
-
if (sse) {
|
|
142
|
-
yield sse;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
async function* iterSSEChunks(iterator) {
|
|
147
|
-
let data = new Uint8Array();
|
|
148
|
-
for await (const chunk of iterator) {
|
|
149
|
-
if (chunk == null) {
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk;
|
|
153
|
-
const newData = new Uint8Array(data.length + binaryChunk.length);
|
|
154
|
-
newData.set(data);
|
|
155
|
-
newData.set(binaryChunk, data.length);
|
|
156
|
-
data = newData;
|
|
157
|
-
let patternIndex;
|
|
158
|
-
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
|
|
159
|
-
yield data.slice(0, patternIndex);
|
|
160
|
-
data = data.slice(patternIndex);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
if (data.length > 0) {
|
|
164
|
-
yield data;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
var findDoubleNewlineIndex = (buffer) => {
|
|
168
|
-
const newline = 10;
|
|
169
|
-
const carriage = 13;
|
|
170
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
171
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
172
|
-
return i + 2;
|
|
173
|
-
}
|
|
174
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
175
|
-
return i + 2;
|
|
176
|
-
}
|
|
177
|
-
if (buffer[i] === carriage && buffer[i + 1] === newline && i + 3 < buffer.length && buffer[i + 2] === carriage && buffer[i + 3] === newline) {
|
|
178
|
-
return i + 4;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return -1;
|
|
182
|
-
};
|
|
183
|
-
var SSEDecoder = class {
|
|
184
|
-
constructor() {
|
|
185
|
-
this.event = null;
|
|
186
|
-
this.data = [];
|
|
187
|
-
this.chunks = [];
|
|
188
|
-
}
|
|
189
|
-
decode(line) {
|
|
190
|
-
if (line.endsWith("\r")) {
|
|
191
|
-
line = line.substring(0, line.length - 1);
|
|
192
|
-
}
|
|
193
|
-
if (!line) {
|
|
194
|
-
if (!this.event && !this.data.length) {
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
const sse = {
|
|
198
|
-
event: this.event,
|
|
199
|
-
data: this.data.join("\n"),
|
|
200
|
-
raw: this.chunks
|
|
201
|
-
};
|
|
202
|
-
this.event = null;
|
|
203
|
-
this.data = [];
|
|
204
|
-
this.chunks = [];
|
|
205
|
-
return sse;
|
|
206
|
-
}
|
|
207
|
-
this.chunks.push(line);
|
|
208
|
-
if (line.startsWith(":")) {
|
|
209
|
-
return null;
|
|
210
|
-
}
|
|
211
|
-
let [fieldname, _, value] = partition(line, ":");
|
|
212
|
-
if (value.startsWith(" ")) {
|
|
213
|
-
value = value.substring(1);
|
|
214
|
-
}
|
|
215
|
-
if (fieldname === "event") {
|
|
216
|
-
this.event = value;
|
|
217
|
-
} else if (fieldname === "data") {
|
|
218
|
-
this.data.push(value);
|
|
219
|
-
}
|
|
220
|
-
return null;
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
var partition = (str, delimiter) => {
|
|
224
|
-
const index = str.indexOf(delimiter);
|
|
225
|
-
if (index !== -1) {
|
|
226
|
-
return [
|
|
227
|
-
str.substring(0, index),
|
|
228
|
-
delimiter,
|
|
229
|
-
str.substring(index + delimiter.length)
|
|
230
|
-
];
|
|
231
|
-
}
|
|
232
|
-
return [
|
|
233
|
-
str,
|
|
234
|
-
"",
|
|
235
|
-
""
|
|
236
|
-
];
|
|
237
|
-
};
|
|
238
|
-
function readableStreamAsyncIterable(stream) {
|
|
239
|
-
if (stream[Symbol.asyncIterator]) {
|
|
240
|
-
return stream;
|
|
241
|
-
}
|
|
242
|
-
const reader = stream.getReader();
|
|
243
|
-
return {
|
|
244
|
-
next: async () => {
|
|
245
|
-
try {
|
|
246
|
-
const result = await reader.read();
|
|
247
|
-
if (result?.done) {
|
|
248
|
-
reader.releaseLock();
|
|
249
|
-
}
|
|
250
|
-
return result;
|
|
251
|
-
} catch (e) {
|
|
252
|
-
reader.releaseLock();
|
|
253
|
-
throw e;
|
|
254
|
-
}
|
|
255
|
-
},
|
|
256
|
-
return: async () => {
|
|
257
|
-
const cancelPromise = reader.cancel();
|
|
258
|
-
reader.releaseLock();
|
|
259
|
-
await cancelPromise;
|
|
260
|
-
return {
|
|
261
|
-
done: true,
|
|
262
|
-
value: void 0
|
|
263
|
-
};
|
|
264
|
-
},
|
|
265
|
-
[Symbol.asyncIterator]() {
|
|
266
|
-
return this;
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
var LineDecoder = class _LineDecoder {
|
|
271
|
-
static {
|
|
272
|
-
this.NEWLINE_CHARS = /* @__PURE__ */ new Set([
|
|
273
|
-
"\n",
|
|
274
|
-
"\r"
|
|
275
|
-
]);
|
|
276
|
-
}
|
|
277
|
-
static {
|
|
278
|
-
this.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
279
|
-
}
|
|
280
|
-
constructor() {
|
|
281
|
-
this.buffer = [];
|
|
282
|
-
this.trailingCR = false;
|
|
283
|
-
}
|
|
284
|
-
decode(chunk) {
|
|
285
|
-
let text = this.decodeText(chunk);
|
|
286
|
-
if (this.trailingCR) {
|
|
287
|
-
text = "\r" + text;
|
|
288
|
-
this.trailingCR = false;
|
|
289
|
-
}
|
|
290
|
-
if (text.endsWith("\r")) {
|
|
291
|
-
this.trailingCR = true;
|
|
292
|
-
text = text.slice(0, -1);
|
|
293
|
-
}
|
|
294
|
-
if (!text) {
|
|
295
|
-
return [];
|
|
296
|
-
}
|
|
297
|
-
const trailingNewline = _LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || "");
|
|
298
|
-
let lines = text.split(_LineDecoder.NEWLINE_REGEXP);
|
|
299
|
-
if (trailingNewline) {
|
|
300
|
-
lines.pop();
|
|
301
|
-
}
|
|
302
|
-
if (lines.length === 1 && !trailingNewline) {
|
|
303
|
-
this.buffer.push(lines[0]);
|
|
304
|
-
return [];
|
|
305
|
-
}
|
|
306
|
-
if (this.buffer.length > 0) {
|
|
307
|
-
lines = [
|
|
308
|
-
this.buffer.join("") + lines[0],
|
|
309
|
-
...lines.slice(1)
|
|
310
|
-
];
|
|
311
|
-
this.buffer = [];
|
|
312
|
-
}
|
|
313
|
-
if (!trailingNewline) {
|
|
314
|
-
this.buffer = [
|
|
315
|
-
lines.pop() || ""
|
|
316
|
-
];
|
|
317
|
-
}
|
|
318
|
-
return lines;
|
|
319
|
-
}
|
|
320
|
-
decodeText(bytes) {
|
|
321
|
-
if (bytes == null) {
|
|
322
|
-
return "";
|
|
323
|
-
}
|
|
324
|
-
if (typeof bytes === "string") {
|
|
325
|
-
return bytes;
|
|
326
|
-
}
|
|
327
|
-
if (typeof Buffer !== "undefined") {
|
|
328
|
-
if (bytes instanceof Buffer) {
|
|
329
|
-
return bytes.toString();
|
|
330
|
-
}
|
|
331
|
-
if (bytes instanceof Uint8Array) {
|
|
332
|
-
return Buffer.from(bytes).toString();
|
|
333
|
-
}
|
|
334
|
-
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.`);
|
|
335
|
-
}
|
|
336
|
-
if (typeof TextDecoder !== "undefined") {
|
|
337
|
-
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
|
|
338
|
-
this.textDecoder ??= new TextDecoder("utf8");
|
|
339
|
-
return this.textDecoder.decode(bytes);
|
|
340
|
-
}
|
|
341
|
-
throw new Error(`Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform. Please report this error.`);
|
|
342
|
-
}
|
|
343
|
-
throw new Error("Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.");
|
|
344
|
-
}
|
|
345
|
-
flush() {
|
|
346
|
-
if (!this.buffer.length && !this.trailingCR) {
|
|
347
|
-
return [];
|
|
348
|
-
}
|
|
349
|
-
const lines = [
|
|
350
|
-
this.buffer.join("")
|
|
351
|
-
];
|
|
352
|
-
this.buffer = [];
|
|
353
|
-
this.trailingCR = false;
|
|
354
|
-
return lines;
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/stream.ts";
|
|
358
|
-
var createGenerationStream = (response, controller = new AbortController()) => {
|
|
359
|
-
const iterator = async function* () {
|
|
360
|
-
for await (const sse of iterSSEMessages(response, controller)) {
|
|
361
|
-
if (sse.event === "completion") {
|
|
362
|
-
try {
|
|
363
|
-
yield JSON.parse(sse.data);
|
|
364
|
-
} catch (err) {
|
|
365
|
-
import_log.log.error("could not parse message into JSON:", {
|
|
366
|
-
data: sse.data,
|
|
367
|
-
raw: sse.raw
|
|
368
|
-
}, {
|
|
369
|
-
F: __dxlog_file,
|
|
370
|
-
L: 22,
|
|
371
|
-
S: this,
|
|
372
|
-
C: (f, a) => f(...a)
|
|
373
|
-
});
|
|
374
|
-
throw err;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
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") {
|
|
378
|
-
try {
|
|
379
|
-
yield JSON.parse(sse.data);
|
|
380
|
-
} catch (err) {
|
|
381
|
-
import_log.log.error("could not parse message into JSON:", {
|
|
382
|
-
data: sse.data,
|
|
383
|
-
raw: sse.raw
|
|
384
|
-
}, {
|
|
385
|
-
F: __dxlog_file,
|
|
386
|
-
L: 38,
|
|
387
|
-
S: this,
|
|
388
|
-
C: (f, a) => f(...a)
|
|
389
|
-
});
|
|
390
|
-
throw err;
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
if (sse.event === "ping") {
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
if (sse.event === "error") {
|
|
397
|
-
throw new Error(`Message generation error: ${sse.data}`);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
};
|
|
401
|
-
return new GenerationStreamImpl(controller, iterator);
|
|
402
|
-
};
|
|
403
|
-
var GenerationStreamImpl = class {
|
|
404
|
-
constructor(_controller, _getIterator) {
|
|
405
|
-
this._controller = _controller;
|
|
406
|
-
this._getIterator = _getIterator;
|
|
407
|
-
this._done = new import_async.Trigger();
|
|
408
|
-
this._iterator = void 0;
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Returns an iterator over the stream.
|
|
412
|
-
*/
|
|
413
|
-
[Symbol.asyncIterator]() {
|
|
414
|
-
return this._createIterator();
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* Aborts the stream.
|
|
418
|
-
*/
|
|
419
|
-
abort() {
|
|
420
|
-
this._controller.abort();
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Returns the complete message list generated by the AI service.
|
|
424
|
-
*/
|
|
425
|
-
async complete() {
|
|
426
|
-
await this._done.wait();
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Creates an iterator over the stream.
|
|
430
|
-
*/
|
|
431
|
-
_createIterator() {
|
|
432
|
-
const self = this;
|
|
433
|
-
return this._iterator ??= (() => {
|
|
434
|
-
const generator = async function* () {
|
|
435
|
-
try {
|
|
436
|
-
for await (const event of self._getIterator()) {
|
|
437
|
-
yield event;
|
|
438
|
-
}
|
|
439
|
-
this._done.wake();
|
|
440
|
-
} catch (err) {
|
|
441
|
-
this._done.throw(err);
|
|
442
|
-
throw err;
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
return generator.call(this);
|
|
446
|
-
})();
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/edge-client.ts";
|
|
450
|
-
var AIServiceEdgeClient = class {
|
|
451
|
-
constructor({ endpoint }) {
|
|
452
|
-
(0, import_invariant.invariant)(endpoint, "endpoint is required", {
|
|
453
|
-
F: __dxlog_file2,
|
|
454
|
-
L: 23,
|
|
455
|
-
S: this,
|
|
456
|
-
A: [
|
|
457
|
-
"endpoint",
|
|
458
|
-
"'endpoint is required'"
|
|
459
|
-
]
|
|
460
|
-
});
|
|
461
|
-
this._endpoint = endpoint;
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* @deprecated
|
|
465
|
-
*/
|
|
466
|
-
// TODO(burdon): Remove.
|
|
467
|
-
async appendMessages(messages) {
|
|
468
|
-
const url = `${this._endpoint}/message`;
|
|
469
|
-
const res = await fetch(url, {
|
|
470
|
-
method: "POST",
|
|
471
|
-
headers: {
|
|
472
|
-
"Content-Type": "application/json"
|
|
473
|
-
},
|
|
474
|
-
body: JSON.stringify({
|
|
475
|
-
messages: await import_effect.Schema.encodePromise(import_effect.Schema.Array(import_artifact.Message))(messages)
|
|
476
|
-
})
|
|
477
|
-
});
|
|
478
|
-
if (!res.ok) {
|
|
479
|
-
throw new Error(`${await res.text()} [${url}]`);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
/**
|
|
483
|
-
* Process request and open message stream.
|
|
484
|
-
*/
|
|
485
|
-
async exec(request) {
|
|
486
|
-
(0, import_invariant.assertArgument)(typeof request.model === "string", "model is required");
|
|
487
|
-
(0, import_log2.log)("requesting", {
|
|
488
|
-
endpoint: this._endpoint
|
|
489
|
-
}, {
|
|
490
|
-
F: __dxlog_file2,
|
|
491
|
-
L: 51,
|
|
492
|
-
S: this,
|
|
493
|
-
C: (f, a) => f(...a)
|
|
494
|
-
});
|
|
495
|
-
const controller = new AbortController();
|
|
496
|
-
const response = await fetch(`${this._endpoint}/generate`, {
|
|
497
|
-
signal: controller.signal,
|
|
498
|
-
method: "POST",
|
|
499
|
-
headers: {
|
|
500
|
-
"Content-Type": "application/json"
|
|
501
|
-
},
|
|
502
|
-
body: JSON.stringify({
|
|
503
|
-
// TODO(dmaretskyi): Errors if tools are not provided.
|
|
504
|
-
tools: request.tools ?? [],
|
|
505
|
-
...request
|
|
506
|
-
})
|
|
507
|
-
});
|
|
508
|
-
(0, import_invariant.invariant)(response.body instanceof ReadableStream, void 0, {
|
|
509
|
-
F: __dxlog_file2,
|
|
510
|
-
L: 64,
|
|
511
|
-
S: this,
|
|
512
|
-
A: [
|
|
513
|
-
"response.body instanceof ReadableStream",
|
|
514
|
-
""
|
|
515
|
-
]
|
|
516
|
-
});
|
|
517
|
-
return createGenerationStream(response, controller);
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
var MessageCollector = class {
|
|
521
|
-
constructor() {
|
|
522
|
-
this._messages = [];
|
|
523
|
-
this._pendingMessage = void 0;
|
|
524
|
-
}
|
|
525
|
-
get messages() {
|
|
526
|
-
return this._messages;
|
|
527
|
-
}
|
|
528
|
-
get pendingMessage() {
|
|
529
|
-
return this._pendingMessage;
|
|
530
|
-
}
|
|
531
|
-
push(event) {
|
|
532
|
-
switch (event.type) {
|
|
533
|
-
case "message_start":
|
|
534
|
-
this._pendingMessage = event.message;
|
|
535
|
-
break;
|
|
536
|
-
case "message_stop":
|
|
537
|
-
if (this._pendingMessage) {
|
|
538
|
-
this._messages.push(this._pendingMessage);
|
|
539
|
-
this._pendingMessage = void 0;
|
|
540
|
-
}
|
|
541
|
-
break;
|
|
542
|
-
case "content_block_start":
|
|
543
|
-
if (this._pendingMessage) {
|
|
544
|
-
this._pendingMessage.content = this._pendingMessage.content || [];
|
|
545
|
-
this._pendingMessage.content[event.index] = event.content;
|
|
546
|
-
}
|
|
547
|
-
break;
|
|
548
|
-
case "content_block_delta":
|
|
549
|
-
if (this._pendingMessage && this._pendingMessage.content) {
|
|
550
|
-
const block = this._pendingMessage.content[event.index];
|
|
551
|
-
if (block && event.delta.type === "text_delta" && block.type === "text") {
|
|
552
|
-
block.text = (block.text || "") + event.delta.text;
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
break;
|
|
556
|
-
case "content_block_stop":
|
|
557
|
-
if (this._pendingMessage && this._pendingMessage.content) {
|
|
558
|
-
const block = this._pendingMessage.content[event.index];
|
|
559
|
-
if (block) {
|
|
560
|
-
block.pending = false;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
break;
|
|
564
|
-
case "message_delta":
|
|
565
|
-
if (this._pendingMessage && event.delta) {
|
|
566
|
-
Object.assign(this._pendingMessage, event.delta);
|
|
567
|
-
}
|
|
568
|
-
break;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
pushMessage(message) {
|
|
572
|
-
this._messages.push(message);
|
|
573
|
-
}
|
|
574
|
-
};
|
|
575
|
-
function* emitMessageAsEvents(message) {
|
|
576
|
-
yield {
|
|
577
|
-
type: "message_start",
|
|
578
|
-
message
|
|
579
|
-
};
|
|
580
|
-
yield {
|
|
581
|
-
type: "message_stop"
|
|
582
|
-
};
|
|
583
|
-
}
|
|
584
|
-
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
585
|
-
var LLMModel = import_effect2.Schema.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
|
|
586
|
-
var ToolTypes = Object.freeze({
|
|
587
|
-
// TODO(dmaretskyi): Not implemented yet.
|
|
588
|
-
// DatabaseQuery: 'database_query',
|
|
589
|
-
TextToImage: "text_to_image"
|
|
590
|
-
});
|
|
591
|
-
var GenerateRequest = import_effect2.Schema.Struct({
|
|
592
|
-
spaceId: import_effect2.Schema.optional(import_artifact2.SpaceIdSchema),
|
|
593
|
-
threadId: import_effect2.Schema.optional(import_echo_schema.ObjectId),
|
|
594
|
-
/**
|
|
595
|
-
* Preferred model or system default.
|
|
596
|
-
*/
|
|
597
|
-
model: import_effect2.Schema.optional(LLMModel),
|
|
598
|
-
/**
|
|
599
|
-
* Tools available for the LLM.
|
|
600
|
-
*/
|
|
601
|
-
tools: import_effect2.Schema.optional(import_effect2.Schema.Array(import_artifact2.Tool).pipe(import_effect2.Schema.mutable)),
|
|
602
|
-
/**
|
|
603
|
-
* System instructions to the LLM.
|
|
604
|
-
*/
|
|
605
|
-
systemPrompt: import_effect2.Schema.optional(import_effect2.Schema.String),
|
|
606
|
-
/**
|
|
607
|
-
* History of messages to include in the context window.
|
|
608
|
-
*/
|
|
609
|
-
// TODO(burdon): Rename messages.
|
|
610
|
-
history: import_effect2.Schema.optional(import_effect2.Schema.Array(import_artifact2.Message)),
|
|
611
|
-
/**
|
|
612
|
-
* Current request.
|
|
613
|
-
*/
|
|
614
|
-
prompt: import_effect2.Schema.optional(import_artifact2.Message)
|
|
615
|
-
});
|
|
616
|
-
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
|
|
617
|
-
var runLLM = async (params) => {
|
|
618
|
-
let conversationResult = null;
|
|
619
|
-
const history = params.history ?? [];
|
|
620
|
-
const generate = async () => {
|
|
621
|
-
(0, import_log3.log)("llm generate", {
|
|
622
|
-
tools: params.tools
|
|
623
|
-
}, {
|
|
624
|
-
F: __dxlog_file3,
|
|
625
|
-
L: 44,
|
|
626
|
-
S: void 0,
|
|
627
|
-
C: (f, a) => f(...a)
|
|
628
|
-
});
|
|
629
|
-
const beginTs = Date.now();
|
|
630
|
-
const stream = await params.client.exec({
|
|
631
|
-
model: params.model,
|
|
632
|
-
spaceId: params.spaceId,
|
|
633
|
-
threadId: params.threadId,
|
|
634
|
-
history,
|
|
635
|
-
systemPrompt: params.system,
|
|
636
|
-
tools: params.tools
|
|
637
|
-
});
|
|
638
|
-
for await (const event of stream) {
|
|
639
|
-
params.logger?.(event);
|
|
640
|
-
}
|
|
641
|
-
await stream.complete();
|
|
642
|
-
const messages = [];
|
|
643
|
-
const message = messages.at(-1);
|
|
644
|
-
(0, import_invariant2.invariant)(message, void 0, {
|
|
645
|
-
F: __dxlog_file3,
|
|
646
|
-
L: 63,
|
|
647
|
-
S: void 0,
|
|
648
|
-
A: [
|
|
649
|
-
"message",
|
|
650
|
-
""
|
|
651
|
-
]
|
|
652
|
-
});
|
|
653
|
-
(0, import_log3.log)("llm result", {
|
|
654
|
-
time: Date.now() - beginTs,
|
|
655
|
-
message
|
|
656
|
-
}, {
|
|
657
|
-
F: __dxlog_file3,
|
|
658
|
-
L: 65,
|
|
659
|
-
S: void 0,
|
|
660
|
-
C: (f, a) => f(...a)
|
|
661
|
-
});
|
|
662
|
-
(0, import_invariant2.invariant)(message, void 0, {
|
|
663
|
-
F: __dxlog_file3,
|
|
664
|
-
L: 66,
|
|
665
|
-
S: void 0,
|
|
666
|
-
A: [
|
|
667
|
-
"message",
|
|
668
|
-
""
|
|
669
|
-
]
|
|
670
|
-
});
|
|
671
|
-
params.logger?.({
|
|
672
|
-
type: "message",
|
|
673
|
-
message
|
|
674
|
-
});
|
|
675
|
-
history.push(...messages.map((message2) => ({
|
|
676
|
-
...message2,
|
|
677
|
-
content: message2.content.filter((block) => block.type !== "image")
|
|
678
|
-
})));
|
|
679
|
-
const isToolUse2 = message.content.at(-1)?.type === "tool_use";
|
|
680
|
-
if (isToolUse2) {
|
|
681
|
-
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
682
|
-
(0, import_invariant2.invariant)(toolCalls.length === 1, void 0, {
|
|
683
|
-
F: __dxlog_file3,
|
|
684
|
-
L: 77,
|
|
685
|
-
S: void 0,
|
|
686
|
-
A: [
|
|
687
|
-
"toolCalls.length === 1",
|
|
688
|
-
""
|
|
689
|
-
]
|
|
690
|
-
});
|
|
691
|
-
const toolCall = toolCalls[0];
|
|
692
|
-
const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
|
|
693
|
-
if (!tool) {
|
|
694
|
-
throw new Error(`Tool not found: ${toolCall.name}`);
|
|
695
|
-
}
|
|
696
|
-
(0, import_invariant2.invariant)(tool.execute, void 0, {
|
|
697
|
-
F: __dxlog_file3,
|
|
698
|
-
L: 84,
|
|
699
|
-
S: void 0,
|
|
700
|
-
A: [
|
|
701
|
-
"tool.execute",
|
|
702
|
-
""
|
|
703
|
-
]
|
|
704
|
-
});
|
|
705
|
-
const toolResult = await tool.execute(toolCall.input);
|
|
706
|
-
switch (toolResult.kind) {
|
|
707
|
-
case "error": {
|
|
708
|
-
import_log3.log.warn("tool error", {
|
|
709
|
-
message: toolResult.message
|
|
710
|
-
}, {
|
|
711
|
-
F: __dxlog_file3,
|
|
712
|
-
L: 88,
|
|
713
|
-
S: void 0,
|
|
714
|
-
C: (f, a) => f(...a)
|
|
715
|
-
});
|
|
716
|
-
const resultMessage = {
|
|
717
|
-
id: import_echo_schema2.ObjectId.random(),
|
|
718
|
-
spaceId: params.spaceId,
|
|
719
|
-
threadId: params.threadId,
|
|
720
|
-
role: "user",
|
|
721
|
-
content: [
|
|
722
|
-
{
|
|
723
|
-
type: "tool_result",
|
|
724
|
-
toolUseId: toolCall.id,
|
|
725
|
-
content: toolResult.message,
|
|
726
|
-
isError: true
|
|
727
|
-
}
|
|
728
|
-
]
|
|
729
|
-
};
|
|
730
|
-
params.logger?.({
|
|
731
|
-
type: "message",
|
|
732
|
-
message: resultMessage
|
|
733
|
-
});
|
|
734
|
-
history.push(resultMessage);
|
|
735
|
-
return true;
|
|
736
|
-
}
|
|
737
|
-
case "success": {
|
|
738
|
-
(0, import_log3.log)("tool success", {
|
|
739
|
-
result: toolResult.result
|
|
740
|
-
}, {
|
|
741
|
-
F: __dxlog_file3,
|
|
742
|
-
L: 110,
|
|
743
|
-
S: void 0,
|
|
744
|
-
C: (f, a) => f(...a)
|
|
745
|
-
});
|
|
746
|
-
const resultMessage = {
|
|
747
|
-
id: import_echo_schema2.ObjectId.random(),
|
|
748
|
-
spaceId: params.spaceId,
|
|
749
|
-
threadId: params.threadId,
|
|
750
|
-
role: "user",
|
|
751
|
-
content: [
|
|
752
|
-
{
|
|
753
|
-
type: "tool_result",
|
|
754
|
-
toolUseId: toolCall.id,
|
|
755
|
-
content: JSON.stringify(toolResult.result)
|
|
756
|
-
}
|
|
757
|
-
]
|
|
758
|
-
};
|
|
759
|
-
params.logger?.({
|
|
760
|
-
type: "message",
|
|
761
|
-
message: resultMessage
|
|
762
|
-
});
|
|
763
|
-
history.push(resultMessage);
|
|
764
|
-
return true;
|
|
765
|
-
}
|
|
766
|
-
case "break": {
|
|
767
|
-
(0, import_log3.log)("tool break", {
|
|
768
|
-
result: toolResult.result
|
|
769
|
-
}, {
|
|
770
|
-
F: __dxlog_file3,
|
|
771
|
-
L: 131,
|
|
772
|
-
S: void 0,
|
|
773
|
-
C: (f, a) => f(...a)
|
|
774
|
-
});
|
|
775
|
-
conversationResult = toolResult.result;
|
|
776
|
-
return false;
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
return false;
|
|
781
|
-
};
|
|
782
|
-
while (await generate()) {
|
|
783
|
-
}
|
|
784
|
-
return {
|
|
785
|
-
result: conversationResult,
|
|
786
|
-
history
|
|
787
|
-
};
|
|
788
|
-
};
|
|
789
|
-
var isToolUse = (message, { onlyToolNames } = {}) => {
|
|
790
|
-
const block = message.content.at(-1);
|
|
791
|
-
(0, import_invariant2.invariant)(block, void 0, {
|
|
792
|
-
F: __dxlog_file3,
|
|
793
|
-
L: 152,
|
|
794
|
-
S: void 0,
|
|
795
|
-
A: [
|
|
796
|
-
"block",
|
|
797
|
-
""
|
|
798
|
-
]
|
|
799
|
-
});
|
|
800
|
-
return block.type === "tool_use" && (!onlyToolNames || onlyToolNames.includes(block.name));
|
|
801
|
-
};
|
|
802
|
-
var runTools = async ({ message, tools, extensions }) => {
|
|
803
|
-
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
804
|
-
(0, import_invariant2.invariant)(toolCalls.length === 1, void 0, {
|
|
805
|
-
F: __dxlog_file3,
|
|
806
|
-
L: 174,
|
|
807
|
-
S: void 0,
|
|
808
|
-
A: [
|
|
809
|
-
"toolCalls.length === 1",
|
|
810
|
-
""
|
|
811
|
-
]
|
|
812
|
-
});
|
|
813
|
-
const toolCall = toolCalls[0];
|
|
814
|
-
const tool = tools.find((tool2) => tool2.name === toolCall.name);
|
|
815
|
-
if (!tool) {
|
|
816
|
-
const resultMessage = (0, import_echo_schema2.createStatic)(import_artifact3.Message, {
|
|
817
|
-
role: "user",
|
|
818
|
-
content: [
|
|
819
|
-
{
|
|
820
|
-
type: "tool_result",
|
|
821
|
-
toolUseId: toolCall.id,
|
|
822
|
-
content: `Tool not found: ${toolCall.name}`,
|
|
823
|
-
isError: true
|
|
824
|
-
}
|
|
825
|
-
]
|
|
826
|
-
});
|
|
827
|
-
return {
|
|
828
|
-
type: "continue",
|
|
829
|
-
message: resultMessage
|
|
830
|
-
};
|
|
831
|
-
}
|
|
832
|
-
let toolResult;
|
|
833
|
-
try {
|
|
834
|
-
(0, import_invariant2.invariant)(tool.execute, void 0, {
|
|
835
|
-
F: __dxlog_file3,
|
|
836
|
-
L: 199,
|
|
837
|
-
S: void 0,
|
|
838
|
-
A: [
|
|
839
|
-
"tool.execute",
|
|
840
|
-
""
|
|
841
|
-
]
|
|
842
|
-
});
|
|
843
|
-
toolResult = await tool.execute(toolCall.input, {
|
|
844
|
-
extensions
|
|
845
|
-
});
|
|
846
|
-
} catch (error) {
|
|
847
|
-
(0, import_log3.log)("tool error", {
|
|
848
|
-
error
|
|
849
|
-
}, {
|
|
850
|
-
F: __dxlog_file3,
|
|
851
|
-
L: 202,
|
|
852
|
-
S: void 0,
|
|
853
|
-
C: (f, a) => f(...a)
|
|
854
|
-
});
|
|
855
|
-
toolResult = import_artifact3.ToolResult.Error(error.message);
|
|
856
|
-
}
|
|
857
|
-
switch (toolResult.kind) {
|
|
858
|
-
case "error": {
|
|
859
|
-
(0, import_log3.log)("tool error", {
|
|
860
|
-
message: toolResult.message
|
|
861
|
-
}, {
|
|
862
|
-
F: __dxlog_file3,
|
|
863
|
-
L: 208,
|
|
864
|
-
S: void 0,
|
|
865
|
-
C: (f, a) => f(...a)
|
|
866
|
-
});
|
|
867
|
-
const resultMessage = (0, import_echo_schema2.createStatic)(import_artifact3.Message, {
|
|
868
|
-
role: "user",
|
|
869
|
-
content: [
|
|
870
|
-
{
|
|
871
|
-
type: "tool_result",
|
|
872
|
-
toolUseId: toolCall.id,
|
|
873
|
-
content: toolResult.message,
|
|
874
|
-
isError: true
|
|
875
|
-
}
|
|
876
|
-
]
|
|
877
|
-
});
|
|
878
|
-
return {
|
|
879
|
-
type: "continue",
|
|
880
|
-
message: resultMessage
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
|
-
case "success": {
|
|
884
|
-
(0, import_log3.log)("tool success", {
|
|
885
|
-
result: toolResult.result
|
|
886
|
-
}, {
|
|
887
|
-
F: __dxlog_file3,
|
|
888
|
-
L: 229,
|
|
889
|
-
S: void 0,
|
|
890
|
-
C: (f, a) => f(...a)
|
|
891
|
-
});
|
|
892
|
-
const resultMessage = (0, import_echo_schema2.createStatic)(import_artifact3.Message, {
|
|
893
|
-
role: "user",
|
|
894
|
-
content: [
|
|
895
|
-
{
|
|
896
|
-
type: "tool_result",
|
|
897
|
-
toolUseId: toolCall.id,
|
|
898
|
-
content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result) ?? ""
|
|
899
|
-
},
|
|
900
|
-
...toolResult.extractContentBlocks ?? []
|
|
901
|
-
]
|
|
902
|
-
});
|
|
903
|
-
return {
|
|
904
|
-
type: "continue",
|
|
905
|
-
message: resultMessage
|
|
906
|
-
};
|
|
907
|
-
}
|
|
908
|
-
case "break": {
|
|
909
|
-
(0, import_log3.log)("tool break", {
|
|
910
|
-
result: toolResult.result
|
|
911
|
-
}, {
|
|
912
|
-
F: __dxlog_file3,
|
|
913
|
-
L: 250,
|
|
914
|
-
S: void 0,
|
|
915
|
-
C: (f, a) => f(...a)
|
|
916
|
-
});
|
|
917
|
-
return {
|
|
918
|
-
type: "break",
|
|
919
|
-
result: toolResult.result
|
|
920
|
-
};
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
};
|
|
924
|
-
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/ollama-client.ts";
|
|
925
|
-
var OllamaClient = class {
|
|
926
|
-
/**
|
|
927
|
-
* Check if Ollama server is running and accessible.
|
|
928
|
-
* @returns Promise that resolves to true if Ollama is running, false otherwise.
|
|
929
|
-
*/
|
|
930
|
-
static async isRunning() {
|
|
931
|
-
try {
|
|
932
|
-
const response = await fetch(`${DEFAULT_OLLAMA_ENDPOINT}/api/version`, {
|
|
933
|
-
method: "GET",
|
|
934
|
-
headers: {
|
|
935
|
-
"Content-Type": "application/json"
|
|
936
|
-
},
|
|
937
|
-
signal: AbortSignal.timeout(100)
|
|
938
|
-
});
|
|
939
|
-
return response.ok;
|
|
940
|
-
} catch (error) {
|
|
941
|
-
return false;
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
constructor({ endpoint, tools, overrides, maxToolInvocations } = {}) {
|
|
945
|
-
this._endpoint = endpoint ?? DEFAULT_OLLAMA_ENDPOINT;
|
|
946
|
-
this._tools = tools ?? [];
|
|
947
|
-
this._modelOverride = overrides?.model;
|
|
948
|
-
this._temperatureOverride = overrides?.temperature;
|
|
949
|
-
this._maxToolInvocations = maxToolInvocations ?? 3;
|
|
950
|
-
}
|
|
951
|
-
_getEmbededTools(clientTools) {
|
|
952
|
-
return [
|
|
953
|
-
...this._tools,
|
|
954
|
-
...clientTools.filter((tool) => tool.type !== void 0 && WELL_KNOWN_TOOLS[tool.type]).map((tool) => WELL_KNOWN_TOOLS[tool.type])
|
|
955
|
-
];
|
|
956
|
-
}
|
|
957
|
-
async *_generateStream(request, signal) {
|
|
958
|
-
(0, import_invariant3.invariant)(request.prompt === void 0, "Prompt must be in history", {
|
|
959
|
-
F: __dxlog_file4,
|
|
960
|
-
L: 88,
|
|
961
|
-
S: this,
|
|
962
|
-
A: [
|
|
963
|
-
"request.prompt === undefined",
|
|
964
|
-
"'Prompt must be in history'"
|
|
965
|
-
]
|
|
966
|
-
});
|
|
967
|
-
const ollamaRequest = {
|
|
968
|
-
model: this._modelOverride ?? request.model ?? "llama3",
|
|
969
|
-
messages: [],
|
|
970
|
-
stream: true,
|
|
971
|
-
options: {
|
|
972
|
-
temperature: this._temperatureOverride
|
|
973
|
-
}
|
|
974
|
-
};
|
|
975
|
-
if (request.systemPrompt) {
|
|
976
|
-
ollamaRequest.messages.push({
|
|
977
|
-
role: "system",
|
|
978
|
-
content: request.systemPrompt
|
|
979
|
-
});
|
|
980
|
-
}
|
|
981
|
-
if (request.history?.length) {
|
|
982
|
-
for (const message of request.history) {
|
|
983
|
-
const role = message.role === "assistant" ? "assistant" : "user";
|
|
984
|
-
if (message.content.length === 0) {
|
|
985
|
-
continue;
|
|
986
|
-
}
|
|
987
|
-
for (const content of message.content) {
|
|
988
|
-
switch (content.type) {
|
|
989
|
-
case "text": {
|
|
990
|
-
ollamaRequest.messages.push({
|
|
991
|
-
role,
|
|
992
|
-
content: content.text
|
|
993
|
-
});
|
|
994
|
-
break;
|
|
995
|
-
}
|
|
996
|
-
case "tool_use": {
|
|
997
|
-
ollamaRequest.messages.push({
|
|
998
|
-
role: "assistant",
|
|
999
|
-
content: "",
|
|
1000
|
-
tool_calls: [
|
|
1001
|
-
{
|
|
1002
|
-
function: {
|
|
1003
|
-
name: content.name,
|
|
1004
|
-
arguments: content.input
|
|
1005
|
-
}
|
|
1006
|
-
}
|
|
1007
|
-
]
|
|
1008
|
-
});
|
|
1009
|
-
break;
|
|
1010
|
-
}
|
|
1011
|
-
case "tool_result": {
|
|
1012
|
-
ollamaRequest.messages.push({
|
|
1013
|
-
role: "tool",
|
|
1014
|
-
content: JSON.stringify({
|
|
1015
|
-
type: "tool_result",
|
|
1016
|
-
toolUseId: content.toolUseId,
|
|
1017
|
-
content: content.content
|
|
1018
|
-
})
|
|
1019
|
-
});
|
|
1020
|
-
break;
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
if (request.tools?.length || this._tools.length) {
|
|
1027
|
-
const allTools = [
|
|
1028
|
-
...request.tools ?? [],
|
|
1029
|
-
...this._tools
|
|
1030
|
-
];
|
|
1031
|
-
const withWellKnownTools = allTools.map((tool) => tool.type !== void 0 && WELL_KNOWN_TOOLS[tool.type] ? WELL_KNOWN_TOOLS[tool.type] : tool);
|
|
1032
|
-
if (withWellKnownTools.length > 0) {
|
|
1033
|
-
ollamaRequest.tools = withWellKnownTools.map((tool) => ({
|
|
1034
|
-
type: "function",
|
|
1035
|
-
function: {
|
|
1036
|
-
name: tool.name,
|
|
1037
|
-
description: tool.description,
|
|
1038
|
-
parameters: {
|
|
1039
|
-
type: "object",
|
|
1040
|
-
properties: tool.parameters?.properties,
|
|
1041
|
-
required: tool.parameters?.required
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
}));
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
(0, import_log4.log)("generate request", {
|
|
1048
|
-
history: request.history,
|
|
1049
|
-
prompt: request.prompt,
|
|
1050
|
-
ollamaRequest
|
|
1051
|
-
}, {
|
|
1052
|
-
F: __dxlog_file4,
|
|
1053
|
-
L: 182,
|
|
1054
|
-
S: this,
|
|
1055
|
-
C: (f, a) => f(...a)
|
|
1056
|
-
});
|
|
1057
|
-
const response = await fetch(`${this._endpoint}/api/chat`, {
|
|
1058
|
-
method: "POST",
|
|
1059
|
-
headers: {
|
|
1060
|
-
"Content-Type": "application/json"
|
|
1061
|
-
},
|
|
1062
|
-
body: JSON.stringify(ollamaRequest),
|
|
1063
|
-
signal
|
|
1064
|
-
});
|
|
1065
|
-
if (!response.ok) {
|
|
1066
|
-
throw await parseErrorResponse(response);
|
|
1067
|
-
}
|
|
1068
|
-
if (!response.body) {
|
|
1069
|
-
throw new Error("Ollama API returned no response body");
|
|
1070
|
-
}
|
|
1071
|
-
const reader = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new OllamaDecoderStream()).getReader();
|
|
1072
|
-
try {
|
|
1073
|
-
const messageId = import_echo_schema3.ObjectId.random();
|
|
1074
|
-
yield {
|
|
1075
|
-
type: "message_start",
|
|
1076
|
-
message: {
|
|
1077
|
-
id: messageId,
|
|
1078
|
-
role: "assistant",
|
|
1079
|
-
content: []
|
|
1080
|
-
}
|
|
1081
|
-
};
|
|
1082
|
-
const textBlock = {
|
|
1083
|
-
type: "text",
|
|
1084
|
-
text: ""
|
|
1085
|
-
};
|
|
1086
|
-
let currentBlockIndex = 0;
|
|
1087
|
-
yield {
|
|
1088
|
-
type: "content_block_start",
|
|
1089
|
-
index: currentBlockIndex,
|
|
1090
|
-
content: textBlock
|
|
1091
|
-
};
|
|
1092
|
-
while (true) {
|
|
1093
|
-
const { done, value: data } = await reader.read();
|
|
1094
|
-
if (done) {
|
|
1095
|
-
break;
|
|
1096
|
-
}
|
|
1097
|
-
(0, import_log4.log)("ollama data", {
|
|
1098
|
-
data
|
|
1099
|
-
}, {
|
|
1100
|
-
F: __dxlog_file4,
|
|
1101
|
-
L: 244,
|
|
1102
|
-
S: this,
|
|
1103
|
-
C: (f, a) => f(...a)
|
|
1104
|
-
});
|
|
1105
|
-
if (data.message?.content) {
|
|
1106
|
-
yield {
|
|
1107
|
-
type: "content_block_delta",
|
|
1108
|
-
index: 0,
|
|
1109
|
-
delta: {
|
|
1110
|
-
type: "text_delta",
|
|
1111
|
-
text: data.message.content
|
|
1112
|
-
}
|
|
1113
|
-
};
|
|
1114
|
-
}
|
|
1115
|
-
if (data.message?.tool_calls && data.message?.tool_calls?.length > 0) {
|
|
1116
|
-
if (data.message.tool_calls.length > 1) {
|
|
1117
|
-
import_log4.log.warn("Ollama returned multiple tool calls. Only the first one will be used.", void 0, {
|
|
1118
|
-
F: __dxlog_file4,
|
|
1119
|
-
L: 261,
|
|
1120
|
-
S: this,
|
|
1121
|
-
C: (f, a) => f(...a)
|
|
1122
|
-
});
|
|
1123
|
-
}
|
|
1124
|
-
yield {
|
|
1125
|
-
type: "content_block_stop",
|
|
1126
|
-
index: currentBlockIndex++
|
|
1127
|
-
};
|
|
1128
|
-
yield {
|
|
1129
|
-
type: "content_block_start",
|
|
1130
|
-
index: currentBlockIndex,
|
|
1131
|
-
content: {
|
|
1132
|
-
type: "tool_use",
|
|
1133
|
-
id: import_echo_schema3.ObjectId.random(),
|
|
1134
|
-
name: data.message.tool_calls[0].function.name,
|
|
1135
|
-
input: sanitizeToolArguments(data.message.tool_calls[0].function.arguments)
|
|
1136
|
-
}
|
|
1137
|
-
};
|
|
1138
|
-
yield {
|
|
1139
|
-
type: "message_delta",
|
|
1140
|
-
delta: {
|
|
1141
|
-
stopReason: "tool_use"
|
|
1142
|
-
}
|
|
1143
|
-
};
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
yield {
|
|
1147
|
-
type: "content_block_stop",
|
|
1148
|
-
index: currentBlockIndex
|
|
1149
|
-
};
|
|
1150
|
-
yield {
|
|
1151
|
-
type: "message_stop"
|
|
1152
|
-
};
|
|
1153
|
-
} finally {
|
|
1154
|
-
reader.releaseLock();
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
async exec(request) {
|
|
1158
|
-
const controller = new AbortController();
|
|
1159
|
-
try {
|
|
1160
|
-
return new GenerationStreamImpl(controller, async function* () {
|
|
1161
|
-
const collector = new MessageCollector();
|
|
1162
|
-
let toolInvocations = 0;
|
|
1163
|
-
while (true) {
|
|
1164
|
-
for await (const event of this._generateStream({
|
|
1165
|
-
...request,
|
|
1166
|
-
history: [
|
|
1167
|
-
...request.history ?? [],
|
|
1168
|
-
...request.prompt ? [
|
|
1169
|
-
request.prompt
|
|
1170
|
-
] : [],
|
|
1171
|
-
...collector.messages
|
|
1172
|
-
],
|
|
1173
|
-
prompt: void 0
|
|
1174
|
-
}, controller.signal)) {
|
|
1175
|
-
collector.push(event);
|
|
1176
|
-
yield event;
|
|
1177
|
-
}
|
|
1178
|
-
if (collector.messages.length > 0 && !isToolUse(collector.messages.at(-1), {
|
|
1179
|
-
onlyToolNames: this._getEmbededTools(request.tools ?? []).map((tool) => tool.name)
|
|
1180
|
-
})) {
|
|
1181
|
-
break;
|
|
1182
|
-
}
|
|
1183
|
-
if (++toolInvocations > this._maxToolInvocations) {
|
|
1184
|
-
throw new Error("Maximum number of tool invocations reached");
|
|
1185
|
-
}
|
|
1186
|
-
const result = await runTools({
|
|
1187
|
-
message: collector.messages.at(-1),
|
|
1188
|
-
tools: this._getEmbededTools(request.tools ?? [])
|
|
1189
|
-
});
|
|
1190
|
-
switch (result.type) {
|
|
1191
|
-
case "continue": {
|
|
1192
|
-
collector.pushMessage(result.message);
|
|
1193
|
-
yield* emitMessageAsEvents(result.message);
|
|
1194
|
-
break;
|
|
1195
|
-
}
|
|
1196
|
-
case "break": {
|
|
1197
|
-
throw new Error("Tool use breaks not allowed");
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
}.bind(this));
|
|
1202
|
-
} catch (error) {
|
|
1203
|
-
controller.abort();
|
|
1204
|
-
throw error;
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
};
|
|
1208
|
-
var sanitizeToolArguments = (args) => {
|
|
1209
|
-
if (args.type === "object" && typeof args.properties === "object" && Array.isArray(args.required)) {
|
|
1210
|
-
return args.properties;
|
|
1211
|
-
}
|
|
1212
|
-
return args;
|
|
1213
|
-
};
|
|
1214
|
-
var SAMPLE_IMAGE_URL = "https://images.nightcafe.studio/jobs/BNmcRhHCM1JRKoUtqSei/BNmcRhHCM1JRKoUtqSei--1--5b9rv.jpg";
|
|
1215
|
-
var WELL_KNOWN_TOOLS = {
|
|
1216
|
-
[ToolTypes.TextToImage]: (0, import_artifact4.defineTool)("system", {
|
|
1217
|
-
name: "text-to-image",
|
|
1218
|
-
description: "Generate an image from a text prompt",
|
|
1219
|
-
schema: import_effect3.Schema.Struct({
|
|
1220
|
-
prompt: import_effect3.Schema.String.annotations({
|
|
1221
|
-
description: "The text prompt describing the image to generate"
|
|
1222
|
-
})
|
|
1223
|
-
}),
|
|
1224
|
-
execute: async ({ prompt }) => {
|
|
1225
|
-
const image = await fetch(SAMPLE_IMAGE_URL).then(async (res) => Buffer.from(await res.arrayBuffer()));
|
|
1226
|
-
const imageBase64 = image.toString("base64");
|
|
1227
|
-
const id = import_echo_schema3.ObjectId.random();
|
|
1228
|
-
return import_artifact4.ToolResult.Success(`The generation was successful. Image ID: ${id} Prompt: ${prompt}`, [
|
|
1229
|
-
{
|
|
1230
|
-
type: "image",
|
|
1231
|
-
id,
|
|
1232
|
-
source: {
|
|
1233
|
-
type: "base64",
|
|
1234
|
-
mediaType: "image/jpeg",
|
|
1235
|
-
data: imageBase64
|
|
1236
|
-
}
|
|
1237
|
-
}
|
|
1238
|
-
]);
|
|
1239
|
-
}
|
|
1240
|
-
})
|
|
1241
|
-
};
|
|
1242
|
-
var OllamaDecoderStream = class extends TransformStream {
|
|
1243
|
-
constructor() {
|
|
1244
|
-
super({
|
|
1245
|
-
transform: (chunk, controller) => {
|
|
1246
|
-
this._buffer += chunk;
|
|
1247
|
-
const lines = this._buffer.split("\n");
|
|
1248
|
-
this._buffer = lines.pop() || "";
|
|
1249
|
-
for (const line of lines) {
|
|
1250
|
-
controller.enqueue(JSON.parse(line.trim()));
|
|
1251
|
-
}
|
|
1252
|
-
},
|
|
1253
|
-
flush: (controller) => {
|
|
1254
|
-
this._buffer = this._buffer.trim();
|
|
1255
|
-
if (this._buffer.length > 0) {
|
|
1256
|
-
controller.enqueue(JSON.parse(this._buffer));
|
|
1257
|
-
}
|
|
1258
|
-
}
|
|
1259
|
-
});
|
|
1260
|
-
this._buffer = "";
|
|
1261
|
-
}
|
|
1262
|
-
};
|
|
1263
|
-
var ModelDoesNotSupportToolsError = class extends import_effect3.Schema.TaggedError()("ModelDoesNotSupportToolsError", {
|
|
1264
|
-
model: import_effect3.Schema.String
|
|
1265
|
-
}) {
|
|
1266
|
-
};
|
|
1267
|
-
var ModelNotFoundError = class extends import_effect3.Schema.TaggedError()("ModelNotFoundError", {
|
|
1268
|
-
model: import_effect3.Schema.String
|
|
1269
|
-
}) {
|
|
1270
|
-
};
|
|
1271
|
-
var parseOllamaError = (error) => {
|
|
1272
|
-
{
|
|
1273
|
-
const match = error.match(/^([^']+) does not support tools$/);
|
|
1274
|
-
if (match) {
|
|
1275
|
-
return new ModelDoesNotSupportToolsError({
|
|
1276
|
-
model: match[1]
|
|
1277
|
-
});
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
{
|
|
1281
|
-
const match = error.match(/^model "([^"]+)" not found, try pulling it first$/);
|
|
1282
|
-
if (match) {
|
|
1283
|
-
return new ModelNotFoundError({
|
|
1284
|
-
model: match[1]
|
|
1285
|
-
});
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
throw new Error(`Ollama API error: ${error}`);
|
|
1289
|
-
};
|
|
1290
|
-
var parseErrorResponse = async (response) => {
|
|
1291
|
-
if (response.headers.get("content-type")?.includes("application/json")) {
|
|
1292
|
-
const body = await response.json();
|
|
1293
|
-
return parseOllamaError(body.error);
|
|
1294
|
-
}
|
|
1295
|
-
throw new Error(`Ollama API error: ${response.status} ${response.statusText} ${await response.text()}`);
|
|
1296
|
-
};
|
|
1297
|
-
var textChunk = P.regexp(/[^<]+/).map((content) => ({
|
|
1298
|
-
type: "text",
|
|
1299
|
-
content
|
|
1300
|
-
}));
|
|
1301
|
-
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]) => [
|
|
1302
|
-
name,
|
|
1303
|
-
value
|
|
1304
|
-
]);
|
|
1305
|
-
var tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
|
|
1306
|
-
var selfClosingTag = P.seqMap(
|
|
1307
|
-
//
|
|
1308
|
-
P.string("<"),
|
|
1309
|
-
tagName,
|
|
1310
|
-
P.optWhitespace.then(attribute).many(),
|
|
1311
|
-
P.optWhitespace.then(P.string("/>")),
|
|
1312
|
-
P.optWhitespace,
|
|
1313
|
-
(_, tag, attributes, __) => ({
|
|
1314
|
-
type: "tag",
|
|
1315
|
-
tag,
|
|
1316
|
-
attributes: Object.fromEntries(attributes),
|
|
1317
|
-
content: [],
|
|
1318
|
-
selfClosing: true
|
|
1319
|
-
})
|
|
1320
|
-
);
|
|
1321
|
-
var openTag = P.seqMap(
|
|
1322
|
-
//
|
|
1323
|
-
P.string("<"),
|
|
1324
|
-
tagName,
|
|
1325
|
-
P.optWhitespace.then(attribute).many(),
|
|
1326
|
-
P.string(">"),
|
|
1327
|
-
P.regexp(/[^<]*/),
|
|
1328
|
-
(_, tag, attributes, __, content) => ({
|
|
1329
|
-
type: "tag",
|
|
1330
|
-
tag,
|
|
1331
|
-
attributes: Object.fromEntries(attributes),
|
|
1332
|
-
content: content.length ? [
|
|
1333
|
-
{
|
|
1334
|
-
type: "text",
|
|
1335
|
-
content
|
|
1336
|
-
}
|
|
1337
|
-
] : []
|
|
1338
|
-
})
|
|
1339
|
-
);
|
|
1340
|
-
var closeTag = P.seqMap(
|
|
1341
|
-
//
|
|
1342
|
-
P.string("</"),
|
|
1343
|
-
tagName,
|
|
1344
|
-
P.string(">"),
|
|
1345
|
-
P.optWhitespace,
|
|
1346
|
-
(_, tag) => ({
|
|
1347
|
-
type: "tag",
|
|
1348
|
-
tag,
|
|
1349
|
-
content: [],
|
|
1350
|
-
closing: true
|
|
1351
|
-
})
|
|
1352
|
-
);
|
|
1353
|
-
var mixedChunk = P.alt(selfClosingTag, openTag, closeTag, textChunk);
|
|
1354
|
-
var StreamTransform = class {
|
|
1355
|
-
constructor() {
|
|
1356
|
-
this._buffer = "";
|
|
1357
|
-
}
|
|
1358
|
-
transform(chunk) {
|
|
1359
|
-
this._buffer += chunk;
|
|
1360
|
-
const results = [];
|
|
1361
|
-
const parser = mixedChunk.many().skip(P.optWhitespace);
|
|
1362
|
-
const result = parser.parse(this._buffer);
|
|
1363
|
-
if (result.status) {
|
|
1364
|
-
for (const chunk2 of result.value) {
|
|
1365
|
-
results.push(chunk2);
|
|
1366
|
-
}
|
|
1367
|
-
this._buffer = "";
|
|
1368
|
-
}
|
|
1369
|
-
return results;
|
|
1370
|
-
}
|
|
1371
|
-
// TODO(burdon): Process remaining buffer?
|
|
1372
|
-
flush() {
|
|
1373
|
-
const remaining = this._buffer;
|
|
1374
|
-
this._buffer = "";
|
|
1375
|
-
return remaining.length > 0 ? [
|
|
1376
|
-
{
|
|
1377
|
-
type: "text",
|
|
1378
|
-
content: remaining
|
|
1379
|
-
}
|
|
1380
|
-
] : [];
|
|
1381
|
-
}
|
|
1382
|
-
};
|
|
1383
|
-
var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
|
|
1384
|
-
var MixedStreamParser = class {
|
|
1385
|
-
constructor() {
|
|
1386
|
-
this.message = new import_async2.Event();
|
|
1387
|
-
this.block = new import_async2.Event();
|
|
1388
|
-
this.update = new import_async2.Event();
|
|
1389
|
-
this.streamEvent = new import_async2.Event();
|
|
1390
|
-
}
|
|
1391
|
-
_emitBlock(contentBlock, streamBlock) {
|
|
1392
|
-
const messageBlock = streamBlock ? mergeMessageBlock(contentBlock, streamBlock) : contentBlock;
|
|
1393
|
-
if (messageBlock) {
|
|
1394
|
-
if (messageBlock.type === "text" && messageBlock.text.length === 0) {
|
|
1395
|
-
return;
|
|
1396
|
-
}
|
|
1397
|
-
(0, import_invariant4.invariant)(this._message, void 0, {
|
|
1398
|
-
F: __dxlog_file5,
|
|
1399
|
-
L: 49,
|
|
1400
|
-
S: this,
|
|
1401
|
-
A: [
|
|
1402
|
-
"this._message",
|
|
1403
|
-
""
|
|
1404
|
-
]
|
|
1405
|
-
});
|
|
1406
|
-
this._message.content.push(messageBlock);
|
|
1407
|
-
this.block.emit(messageBlock);
|
|
1408
|
-
}
|
|
1409
|
-
}
|
|
1410
|
-
_emitUpdate(contentBlock, streamBlock) {
|
|
1411
|
-
const messageBlock = mergeMessageBlock(contentBlock, streamBlock);
|
|
1412
|
-
if (messageBlock) {
|
|
1413
|
-
messageBlock.pending = true;
|
|
1414
|
-
this.update.emit(messageBlock);
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
/**
|
|
1418
|
-
* Parse stream until end.
|
|
1419
|
-
*/
|
|
1420
|
-
async parse(stream) {
|
|
1421
|
-
const transformer = new StreamTransform();
|
|
1422
|
-
let contentBlock;
|
|
1423
|
-
let streamBlock;
|
|
1424
|
-
const stack = [];
|
|
1425
|
-
const messagesCollected = [];
|
|
1426
|
-
for await (const event of stream) {
|
|
1427
|
-
(0, import_log5.log)("streamEvent", {
|
|
1428
|
-
event
|
|
1429
|
-
}, {
|
|
1430
|
-
F: __dxlog_file5,
|
|
1431
|
-
L: 83,
|
|
1432
|
-
S: this,
|
|
1433
|
-
C: (f, a) => f(...a)
|
|
1434
|
-
});
|
|
1435
|
-
this.streamEvent.emit(event);
|
|
1436
|
-
switch (event.type) {
|
|
1437
|
-
//
|
|
1438
|
-
// Messages.
|
|
1439
|
-
//
|
|
1440
|
-
case "message_start": {
|
|
1441
|
-
if (this._message) {
|
|
1442
|
-
import_log5.log.warn("unexpected message_start", void 0, {
|
|
1443
|
-
F: __dxlog_file5,
|
|
1444
|
-
L: 94,
|
|
1445
|
-
S: this,
|
|
1446
|
-
C: (f, a) => f(...a)
|
|
1447
|
-
});
|
|
1448
|
-
}
|
|
1449
|
-
this._message = (0, import_echo_schema4.createStatic)(import_artifact5.Message, {
|
|
1450
|
-
role: event.message.role,
|
|
1451
|
-
content: [
|
|
1452
|
-
...event.message.content
|
|
1453
|
-
]
|
|
1454
|
-
});
|
|
1455
|
-
this.message.emit(this._message);
|
|
1456
|
-
break;
|
|
1457
|
-
}
|
|
1458
|
-
case "message_delta": {
|
|
1459
|
-
if (!this._message) {
|
|
1460
|
-
import_log5.log.warn("unexpected message_delta", void 0, {
|
|
1461
|
-
F: __dxlog_file5,
|
|
1462
|
-
L: 104,
|
|
1463
|
-
S: this,
|
|
1464
|
-
C: (f, a) => f(...a)
|
|
1465
|
-
});
|
|
1466
|
-
}
|
|
1467
|
-
break;
|
|
1468
|
-
}
|
|
1469
|
-
case "message_stop": {
|
|
1470
|
-
if (!this._message) {
|
|
1471
|
-
import_log5.log.warn("unexpected message_stop", void 0, {
|
|
1472
|
-
F: __dxlog_file5,
|
|
1473
|
-
L: 112,
|
|
1474
|
-
S: this,
|
|
1475
|
-
C: (f, a) => f(...a)
|
|
1476
|
-
});
|
|
1477
|
-
} else {
|
|
1478
|
-
messagesCollected.push(this._message);
|
|
1479
|
-
this._message = void 0;
|
|
1480
|
-
}
|
|
1481
|
-
break;
|
|
1482
|
-
}
|
|
1483
|
-
//
|
|
1484
|
-
// Content blocks.
|
|
1485
|
-
//
|
|
1486
|
-
case "content_block_start": {
|
|
1487
|
-
if (contentBlock) {
|
|
1488
|
-
import_log5.log.warn("unexpected content_block_start", {
|
|
1489
|
-
content: contentBlock
|
|
1490
|
-
}, {
|
|
1491
|
-
F: __dxlog_file5,
|
|
1492
|
-
L: 126,
|
|
1493
|
-
S: this,
|
|
1494
|
-
C: (f, a) => f(...a)
|
|
1495
|
-
});
|
|
1496
|
-
}
|
|
1497
|
-
contentBlock = event.content;
|
|
1498
|
-
break;
|
|
1499
|
-
}
|
|
1500
|
-
case "content_block_delta": {
|
|
1501
|
-
if (!contentBlock) {
|
|
1502
|
-
import_log5.log.warn("unexpected content_block_delta", void 0, {
|
|
1503
|
-
F: __dxlog_file5,
|
|
1504
|
-
L: 136,
|
|
1505
|
-
S: this,
|
|
1506
|
-
C: (f, a) => f(...a)
|
|
1507
|
-
});
|
|
1508
|
-
break;
|
|
1509
|
-
}
|
|
1510
|
-
switch (event.delta.type) {
|
|
1511
|
-
//
|
|
1512
|
-
// Text
|
|
1513
|
-
//
|
|
1514
|
-
case "text_delta": {
|
|
1515
|
-
const chunks = transformer.transform(event.delta.text);
|
|
1516
|
-
for (const chunk of chunks) {
|
|
1517
|
-
(0, import_log5.log)("text_delta", {
|
|
1518
|
-
chunk
|
|
1519
|
-
}, {
|
|
1520
|
-
F: __dxlog_file5,
|
|
1521
|
-
L: 147,
|
|
1522
|
-
S: this,
|
|
1523
|
-
C: (f, a) => f(...a)
|
|
1524
|
-
});
|
|
1525
|
-
switch (streamBlock?.type) {
|
|
1526
|
-
//
|
|
1527
|
-
// XML Fragment.
|
|
1528
|
-
//
|
|
1529
|
-
case "tag": {
|
|
1530
|
-
if (chunk.type === "tag") {
|
|
1531
|
-
if (chunk.selfClosing) {
|
|
1532
|
-
streamBlock.content.push(chunk);
|
|
1533
|
-
} else if (chunk.closing) {
|
|
1534
|
-
if (stack.length > 0) {
|
|
1535
|
-
const top = stack.pop();
|
|
1536
|
-
(0, import_invariant4.invariant)(top && top.type === "tag", void 0, {
|
|
1537
|
-
F: __dxlog_file5,
|
|
1538
|
-
L: 160,
|
|
1539
|
-
S: this,
|
|
1540
|
-
A: [
|
|
1541
|
-
"top && top.type === 'tag'",
|
|
1542
|
-
""
|
|
1543
|
-
]
|
|
1544
|
-
});
|
|
1545
|
-
(0, import_log5.log)("pop", {
|
|
1546
|
-
top
|
|
1547
|
-
}, {
|
|
1548
|
-
F: __dxlog_file5,
|
|
1549
|
-
L: 161,
|
|
1550
|
-
S: this,
|
|
1551
|
-
C: (f, a) => f(...a)
|
|
1552
|
-
});
|
|
1553
|
-
top.content.push(streamBlock);
|
|
1554
|
-
streamBlock = top;
|
|
1555
|
-
} else {
|
|
1556
|
-
this._emitBlock(contentBlock, streamBlock);
|
|
1557
|
-
streamBlock = void 0;
|
|
1558
|
-
}
|
|
1559
|
-
} else {
|
|
1560
|
-
stack.push(streamBlock);
|
|
1561
|
-
streamBlock = chunk;
|
|
1562
|
-
}
|
|
1563
|
-
} else {
|
|
1564
|
-
if (streamBlock.content.length === 0) {
|
|
1565
|
-
streamBlock.content.push(chunk);
|
|
1566
|
-
} else {
|
|
1567
|
-
const last = streamBlock.content.at(-1);
|
|
1568
|
-
(0, import_invariant4.invariant)(last, void 0, {
|
|
1569
|
-
F: __dxlog_file5,
|
|
1570
|
-
L: 178,
|
|
1571
|
-
S: this,
|
|
1572
|
-
A: [
|
|
1573
|
-
"last",
|
|
1574
|
-
""
|
|
1575
|
-
]
|
|
1576
|
-
});
|
|
1577
|
-
if (last.type === "text") {
|
|
1578
|
-
last.content += chunk.content;
|
|
1579
|
-
} else {
|
|
1580
|
-
streamBlock.content.push(chunk);
|
|
1581
|
-
}
|
|
1582
|
-
}
|
|
1583
|
-
}
|
|
1584
|
-
break;
|
|
1585
|
-
}
|
|
1586
|
-
//
|
|
1587
|
-
// Text Fragment.
|
|
1588
|
-
//
|
|
1589
|
-
case "text": {
|
|
1590
|
-
if (chunk.type === "tag") {
|
|
1591
|
-
this._emitBlock(contentBlock, streamBlock);
|
|
1592
|
-
if (chunk.selfClosing) {
|
|
1593
|
-
this._emitBlock(contentBlock, chunk);
|
|
1594
|
-
streamBlock = void 0;
|
|
1595
|
-
} else {
|
|
1596
|
-
streamBlock = chunk;
|
|
1597
|
-
}
|
|
1598
|
-
} else {
|
|
1599
|
-
streamBlock.content += chunk.content;
|
|
1600
|
-
}
|
|
1601
|
-
break;
|
|
1602
|
-
}
|
|
1603
|
-
//
|
|
1604
|
-
// No current chunk.
|
|
1605
|
-
//
|
|
1606
|
-
default: {
|
|
1607
|
-
if (chunk.type === "tag" && chunk.selfClosing) {
|
|
1608
|
-
this._emitBlock(contentBlock, chunk);
|
|
1609
|
-
} else {
|
|
1610
|
-
streamBlock = chunk;
|
|
1611
|
-
}
|
|
1612
|
-
}
|
|
1613
|
-
}
|
|
1614
|
-
}
|
|
1615
|
-
break;
|
|
1616
|
-
}
|
|
1617
|
-
//
|
|
1618
|
-
// JSON
|
|
1619
|
-
//
|
|
1620
|
-
case "input_json_delta": {
|
|
1621
|
-
if (!contentBlock) {
|
|
1622
|
-
import_log5.log.warn("unexpected input_json_delta", {
|
|
1623
|
-
contentBlock
|
|
1624
|
-
}, {
|
|
1625
|
-
F: __dxlog_file5,
|
|
1626
|
-
L: 228,
|
|
1627
|
-
S: this,
|
|
1628
|
-
C: (f, a) => f(...a)
|
|
1629
|
-
});
|
|
1630
|
-
break;
|
|
1631
|
-
}
|
|
1632
|
-
if (streamBlock?.type === "json") {
|
|
1633
|
-
streamBlock.content += event.delta.partial_json;
|
|
1634
|
-
} else {
|
|
1635
|
-
streamBlock = {
|
|
1636
|
-
type: "json",
|
|
1637
|
-
content: event.delta.partial_json
|
|
1638
|
-
};
|
|
1639
|
-
}
|
|
1640
|
-
break;
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
break;
|
|
1644
|
-
}
|
|
1645
|
-
case "content_block_stop": {
|
|
1646
|
-
if (!contentBlock) {
|
|
1647
|
-
import_log5.log.warn("unexpected content_block_stop", void 0, {
|
|
1648
|
-
F: __dxlog_file5,
|
|
1649
|
-
L: 245,
|
|
1650
|
-
S: this,
|
|
1651
|
-
C: (f, a) => f(...a)
|
|
1652
|
-
});
|
|
1653
|
-
break;
|
|
1654
|
-
}
|
|
1655
|
-
this._emitBlock(contentBlock, streamBlock);
|
|
1656
|
-
contentBlock = void 0;
|
|
1657
|
-
streamBlock = void 0;
|
|
1658
|
-
break;
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
if (contentBlock && streamBlock) {
|
|
1662
|
-
this._emitUpdate(contentBlock, streamBlock);
|
|
1663
|
-
}
|
|
1664
|
-
}
|
|
1665
|
-
return messagesCollected;
|
|
1666
|
-
}
|
|
1667
|
-
};
|
|
1668
|
-
var mergeMessageBlock = (contentBlock, streamBlock) => {
|
|
1669
|
-
(0, import_log5.log)("mergeMessageBlock", {
|
|
1670
|
-
contentBlock,
|
|
1671
|
-
streamBlock
|
|
1672
|
-
}, {
|
|
1673
|
-
F: __dxlog_file5,
|
|
1674
|
-
L: 272,
|
|
1675
|
-
S: void 0,
|
|
1676
|
-
C: (f, a) => f(...a)
|
|
1677
|
-
});
|
|
1678
|
-
switch (streamBlock.type) {
|
|
1679
|
-
//
|
|
1680
|
-
// Text
|
|
1681
|
-
//
|
|
1682
|
-
case "text": {
|
|
1683
|
-
return {
|
|
1684
|
-
...contentBlock,
|
|
1685
|
-
type: "text",
|
|
1686
|
-
text: streamBlock.content
|
|
1687
|
-
};
|
|
1688
|
-
}
|
|
1689
|
-
//
|
|
1690
|
-
// XML
|
|
1691
|
-
//
|
|
1692
|
-
case "tag": {
|
|
1693
|
-
switch (streamBlock.tag) {
|
|
1694
|
-
case "cot": {
|
|
1695
|
-
const content = streamBlock.content.map((block) => {
|
|
1696
|
-
switch (block.type) {
|
|
1697
|
-
case "text":
|
|
1698
|
-
return block.content;
|
|
1699
|
-
default:
|
|
1700
|
-
return null;
|
|
1701
|
-
}
|
|
1702
|
-
}).filter(import_util.isNotFalsy).join("\n");
|
|
1703
|
-
return {
|
|
1704
|
-
...contentBlock,
|
|
1705
|
-
type: "text",
|
|
1706
|
-
disposition: "cot",
|
|
1707
|
-
text: content
|
|
1708
|
-
};
|
|
1709
|
-
}
|
|
1710
|
-
case "artifact": {
|
|
1711
|
-
const { attributes } = streamBlock;
|
|
1712
|
-
return {
|
|
1713
|
-
type: "json",
|
|
1714
|
-
disposition: "artifact",
|
|
1715
|
-
json: JSON.stringify(attributes)
|
|
1716
|
-
};
|
|
1717
|
-
}
|
|
1718
|
-
case "suggest": {
|
|
1719
|
-
if (streamBlock.content.length === 1 && streamBlock.content[0].type === "text") {
|
|
1720
|
-
return {
|
|
1721
|
-
type: "json",
|
|
1722
|
-
disposition: "suggest",
|
|
1723
|
-
json: JSON.stringify({
|
|
1724
|
-
text: streamBlock.content[0].content
|
|
1725
|
-
})
|
|
1726
|
-
};
|
|
1727
|
-
}
|
|
1728
|
-
break;
|
|
1729
|
-
}
|
|
1730
|
-
case "select": {
|
|
1731
|
-
return {
|
|
1732
|
-
type: "json",
|
|
1733
|
-
disposition: "select",
|
|
1734
|
-
json: JSON.stringify({
|
|
1735
|
-
options: streamBlock.content.flatMap((content) => content.type === "tag" && content.content.length === 1 && content.content[0].type === "text" ? [
|
|
1736
|
-
content.content[0].content
|
|
1737
|
-
] : [])
|
|
1738
|
-
})
|
|
1739
|
-
};
|
|
1740
|
-
}
|
|
1741
|
-
case "tool-list": {
|
|
1742
|
-
return {
|
|
1743
|
-
type: "json",
|
|
1744
|
-
disposition: "tool_list",
|
|
1745
|
-
json: JSON.stringify({})
|
|
1746
|
-
};
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
break;
|
|
1750
|
-
}
|
|
1751
|
-
//
|
|
1752
|
-
// JSON
|
|
1753
|
-
//
|
|
1754
|
-
case "json": {
|
|
1755
|
-
switch (contentBlock.type) {
|
|
1756
|
-
case "tool_use": {
|
|
1757
|
-
return {
|
|
1758
|
-
...contentBlock,
|
|
1759
|
-
input: (0, import_util.safeParseJson)(streamBlock.content) ?? {}
|
|
1760
|
-
};
|
|
1761
|
-
}
|
|
1762
|
-
}
|
|
1763
|
-
return {
|
|
1764
|
-
...contentBlock,
|
|
1765
|
-
type: "json",
|
|
1766
|
-
disposition: "artifact",
|
|
1767
|
-
json: streamBlock.content
|
|
1768
|
-
};
|
|
1769
|
-
}
|
|
1770
|
-
}
|
|
1771
|
-
};
|
|
1772
|
-
var ws = import_parjs.parjs.whitespace();
|
|
1773
|
-
var nothing = import_parjs.parjs.string("").pipe(import_parjs.parjsCombinators.map(() => null));
|
|
1774
|
-
var optional = (parser) => parser.pipe(import_parjs.parjsCombinators.recover(() => ({
|
|
1775
|
-
kind: "Soft"
|
|
1776
|
-
})), import_parjs.parjsCombinators.or(nothing));
|
|
1777
|
-
var keyword = (word) => import_parjs.parjs.string(word).pipe(import_parjs.parjsCombinators.map(() => word));
|
|
1778
|
-
var identifier = import_parjs.parjs.regexp(/[a-zA-Z_][a-zA-Z0-9_]*/).pipe(import_parjs.parjsCombinators.map((match) => ({
|
|
1779
|
-
astKind: "Identifier",
|
|
1780
|
-
name: match[0]
|
|
1781
|
-
})));
|
|
1782
|
-
var numberLiteral = import_parjs.parjs.regexp(/-?\d+(\.\d+)?/).pipe(import_parjs.parjsCombinators.map(Number));
|
|
1783
|
-
var stringLiteral = import_parjs.parjs.regexp(/"([^"]*)"|'([^']*)'/).pipe(import_parjs.parjsCombinators.map((match) => ({
|
|
1784
|
-
astKind: "StringLiteral",
|
|
1785
|
-
value: match[1] || match[2]
|
|
1786
|
-
})));
|
|
1787
|
-
var variableTag = identifier.pipe(import_parjs.parjsCombinators.map((variable) => variable)).expects("variable tag");
|
|
1788
|
-
var label = identifier.pipe(import_parjs.parjsCombinators.map((name) => name));
|
|
1789
|
-
var property = identifier.pipe(import_parjs.parjsCombinators.then(import_parjs.parjs.string(":"), ws, stringLiteral), import_parjs.parjsCombinators.map(([key, , , value]) => ({
|
|
1790
|
-
key,
|
|
1791
|
-
value
|
|
1792
|
-
})));
|
|
1793
|
-
var properties = property.pipe(import_parjs.parjsCombinators.manySepBy(","), import_parjs.parjsCombinators.between("{", "}"), import_parjs.parjsCombinators.map((properties2) => ({
|
|
1794
|
-
astKind: "Properties",
|
|
1795
|
-
properties: properties2
|
|
1796
|
-
})));
|
|
1797
|
-
var nodePattern = optional(variableTag).pipe(import_parjs.parjsCombinators.then(":"), import_parjs.parjsCombinators.then(ws, label, ws), import_parjs.parjsCombinators.then(optional(properties)), import_parjs.parjsCombinators.between("(", ")"), import_parjs.parjsCombinators.map(([[[variable], _, label2], properties2]) => ({
|
|
1798
|
-
astKind: "NodePattern",
|
|
1799
|
-
variable,
|
|
1800
|
-
label: label2,
|
|
1801
|
-
properties: properties2
|
|
1802
|
-
})));
|
|
1803
|
-
var relationshipPattern = optional(variableTag).pipe(import_parjs.parjsCombinators.then(":"), import_parjs.parjsCombinators.then(ws, label, ws), import_parjs.parjsCombinators.then(optional(properties)), import_parjs.parjsCombinators.between("[", "]"), import_parjs.parjsCombinators.map(([[[variable], _, label2], properties2]) => ({
|
|
1804
|
-
astKind: "RelationshipPattern",
|
|
1805
|
-
variable,
|
|
1806
|
-
label: label2,
|
|
1807
|
-
properties: properties2
|
|
1808
|
-
})));
|
|
1809
|
-
var graphPatternConnector = import_parjs.parjs.string("->").pipe(import_parjs.parjsCombinators.or("<-", "-"), import_parjs.parjsCombinators.map((x) => ({
|
|
1810
|
-
astKind: "GraphPatternConnector",
|
|
1811
|
-
direction: x
|
|
1812
|
-
})));
|
|
1813
|
-
var graphPattern = nodePattern.pipe(import_parjs.parjsCombinators.or(relationshipPattern), import_parjs.parjsCombinators.manySepBy(graphPatternConnector), import_parjs.parjsCombinators.map((segments) => ({
|
|
1814
|
-
astKind: "GraphPattern",
|
|
1815
|
-
segments
|
|
1816
|
-
})));
|
|
1817
|
-
var matchClause = keyword("MATCH").pipe(import_parjs.parjsCombinators.then(ws), import_parjs.parjsCombinators.then(graphPattern), import_parjs.parjsCombinators.map(([, pattern]) => ({
|
|
1818
|
-
astKind: "MatchClause",
|
|
1819
|
-
pattern
|
|
1820
|
-
})));
|
|
1821
|
-
var memberExpression = identifier.pipe(import_parjs.parjsCombinators.manySepBy("."), import_parjs.parjsCombinators.map((path) => ({
|
|
1822
|
-
astKind: "MemberExpression",
|
|
1823
|
-
path
|
|
1824
|
-
})));
|
|
1825
|
-
var equalsPredicate = memberExpression.pipe(import_parjs.parjsCombinators.then(ws, import_parjs.parjs.string("="), ws, stringLiteral), import_parjs.parjsCombinators.map(([left, , , , right]) => ({
|
|
1826
|
-
astKind: "EqualsPredicate",
|
|
1827
|
-
left,
|
|
1828
|
-
right
|
|
1829
|
-
})));
|
|
1830
|
-
var whereClause = keyword("WHERE").pipe(import_parjs.parjsCombinators.then(ws, equalsPredicate.pipe(import_parjs.parjsCombinators.between(ws, ws), import_parjs.parjsCombinators.manySepBy(import_parjs.parjs.string("AND"))), ws), import_parjs.parjsCombinators.map(([, , predicates]) => ({
|
|
1831
|
-
astKind: "WhereClause",
|
|
1832
|
-
predicates
|
|
1833
|
-
})));
|
|
1834
|
-
var returnClause = keyword("RETURN").pipe(import_parjs.parjsCombinators.then(ws, memberExpression.pipe(import_parjs.parjsCombinators.then(optional(import_parjs.parjs.string("AS").pipe(import_parjs.parjsCombinators.between(ws), import_parjs.parjsCombinators.then(identifier))), ws), import_parjs.parjsCombinators.manySepBy(import_parjs.parjs.string(",").pipe(import_parjs.parjsCombinators.then(ws)))), ws), import_parjs.parjsCombinators.map(([, , fields]) => ({
|
|
1835
|
-
astKind: "ReturnClause",
|
|
1836
|
-
fields: fields.map(([f]) => f)
|
|
1837
|
-
})));
|
|
1838
|
-
var cypherQuery = ws.pipe(import_parjs.parjsCombinators.then(matchClause, ws, optional(whereClause), ws, returnClause, ws), import_parjs.parjsCombinators.map(([, matchClause2, , whereClause2, , returnClause2]) => ({
|
|
1839
|
-
astKind: "CypherQuery",
|
|
1840
|
-
matchClause: matchClause2,
|
|
1841
|
-
whereClause: whereClause2,
|
|
1842
|
-
returnClause: returnClause2
|
|
1843
|
-
})));
|
|
1844
|
-
var parseCypherQuery = (query) => {
|
|
1845
|
-
const parseResult = cypherQuery.parse(query);
|
|
1846
|
-
if (parseResult.isOk) {
|
|
1847
|
-
return {
|
|
1848
|
-
ast: parseResult.value
|
|
1849
|
-
};
|
|
1850
|
-
} else {
|
|
1851
|
-
throw new Error(`Failed to parse query: ${parseResult}`);
|
|
1852
|
-
}
|
|
1853
|
-
};
|
|
1854
|
-
var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/execution-plan.ts";
|
|
1855
|
-
var createExecutionPlan = (query) => {
|
|
1856
|
-
return new ExecutionPlanBuilder().build(query);
|
|
1857
|
-
};
|
|
1858
|
-
var ExecutionPlanBuilder = class {
|
|
1859
|
-
constructor() {
|
|
1860
|
-
this.counter = 0;
|
|
1861
|
-
}
|
|
1862
|
-
newVariable() {
|
|
1863
|
-
return `anon_${this.counter++}`;
|
|
1864
|
-
}
|
|
1865
|
-
build(query) {
|
|
1866
|
-
(0, import_log7.log)("Creating execution plan for query", {
|
|
1867
|
-
query
|
|
1868
|
-
}, {
|
|
1869
|
-
F: __dxlog_file6,
|
|
1870
|
-
L: 76,
|
|
1871
|
-
S: this,
|
|
1872
|
-
C: (f, a) => f(...a)
|
|
1873
|
-
});
|
|
1874
|
-
return {
|
|
1875
|
-
steps: [
|
|
1876
|
-
...this.createStepsForMatchClause(query.ast.matchClause),
|
|
1877
|
-
...query.ast.whereClause ? this.createStepsForWhereClause(query.ast.whereClause) : [],
|
|
1878
|
-
...this.createStepsForReturnClause(query.ast.returnClause)
|
|
1879
|
-
],
|
|
1880
|
-
output: {
|
|
1881
|
-
fields: this.getOutputFields(query.ast.returnClause)
|
|
1882
|
-
}
|
|
1883
|
-
};
|
|
1884
|
-
}
|
|
1885
|
-
createStepsForMatchClause(match) {
|
|
1886
|
-
(0, import_invariant5.invariant)(match.pattern.segments.length > 0, void 0, {
|
|
1887
|
-
F: __dxlog_file6,
|
|
1888
|
-
L: 90,
|
|
1889
|
-
S: this,
|
|
1890
|
-
A: [
|
|
1891
|
-
"match.pattern.segments.length > 0",
|
|
1892
|
-
""
|
|
1893
|
-
]
|
|
1894
|
-
});
|
|
1895
|
-
const firstSegment = match.pattern.segments[0];
|
|
1896
|
-
(0, import_invariant5.invariant)(firstSegment.astKind === "NodePattern", void 0, {
|
|
1897
|
-
F: __dxlog_file6,
|
|
1898
|
-
L: 92,
|
|
1899
|
-
S: this,
|
|
1900
|
-
A: [
|
|
1901
|
-
"firstSegment.astKind === 'NodePattern'",
|
|
1902
|
-
""
|
|
1903
|
-
]
|
|
1904
|
-
});
|
|
1905
|
-
let lastNodeVariable = firstSegment.variable?.name ?? this.newVariable();
|
|
1906
|
-
const steps = [];
|
|
1907
|
-
steps.push({
|
|
1908
|
-
type: "NodeScan",
|
|
1909
|
-
label: firstSegment.label.name,
|
|
1910
|
-
into: lastNodeVariable
|
|
1911
|
-
});
|
|
1912
|
-
if (firstSegment.properties) {
|
|
1913
|
-
for (const property2 of firstSegment.properties.properties) {
|
|
1914
|
-
steps.push({
|
|
1915
|
-
type: "FilterByProperty",
|
|
1916
|
-
variable: lastNodeVariable,
|
|
1917
|
-
property: property2.key.name,
|
|
1918
|
-
value: property2.value.value
|
|
1919
|
-
});
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
for (let i = 1; i < match.pattern.segments.length; i++) {
|
|
1923
|
-
const relationshipSegment = match.pattern.segments[i++];
|
|
1924
|
-
(0, import_invariant5.invariant)(relationshipSegment.astKind === "RelationshipPattern", void 0, {
|
|
1925
|
-
F: __dxlog_file6,
|
|
1926
|
-
L: 115,
|
|
1927
|
-
S: this,
|
|
1928
|
-
A: [
|
|
1929
|
-
"relationshipSegment.astKind === 'RelationshipPattern'",
|
|
1930
|
-
""
|
|
1931
|
-
]
|
|
1932
|
-
});
|
|
1933
|
-
const nodeSegment = match.pattern.segments[i];
|
|
1934
|
-
(0, import_invariant5.invariant)(nodeSegment.astKind === "NodePattern", void 0, {
|
|
1935
|
-
F: __dxlog_file6,
|
|
1936
|
-
L: 117,
|
|
1937
|
-
S: this,
|
|
1938
|
-
A: [
|
|
1939
|
-
"nodeSegment.astKind === 'NodePattern'",
|
|
1940
|
-
""
|
|
1941
|
-
]
|
|
1942
|
-
});
|
|
1943
|
-
const leftSeparator = match.pattern.segments.separators[i - 2];
|
|
1944
|
-
const rightSeparator = match.pattern.segments.separators[i - 1];
|
|
1945
|
-
const direction = getDirection(leftSeparator.direction, rightSeparator.direction);
|
|
1946
|
-
const relationshipVariable = relationshipSegment.variable?.name ?? this.newVariable();
|
|
1947
|
-
const nodeVariable = nodeSegment.variable?.name ?? this.newVariable();
|
|
1948
|
-
steps.push({
|
|
1949
|
-
type: "RelationshipScan",
|
|
1950
|
-
label: relationshipSegment.label.name,
|
|
1951
|
-
direction,
|
|
1952
|
-
anchor: lastNodeVariable,
|
|
1953
|
-
relationshipInto: relationshipVariable,
|
|
1954
|
-
nodeInto: nodeVariable
|
|
1955
|
-
});
|
|
1956
|
-
if (relationshipSegment.properties) {
|
|
1957
|
-
for (const property2 of relationshipSegment.properties.properties) {
|
|
1958
|
-
steps.push({
|
|
1959
|
-
type: "FilterByProperty",
|
|
1960
|
-
variable: relationshipVariable,
|
|
1961
|
-
property: property2.key.name,
|
|
1962
|
-
value: property2.value.value
|
|
1963
|
-
});
|
|
1964
|
-
}
|
|
1965
|
-
}
|
|
1966
|
-
if (nodeSegment.label) {
|
|
1967
|
-
steps.push({
|
|
1968
|
-
type: "FilterByLabel",
|
|
1969
|
-
variable: nodeVariable,
|
|
1970
|
-
label: nodeSegment.label.name
|
|
1971
|
-
});
|
|
1972
|
-
}
|
|
1973
|
-
if (nodeSegment.properties) {
|
|
1974
|
-
for (const property2 of nodeSegment.properties.properties) {
|
|
1975
|
-
steps.push({
|
|
1976
|
-
type: "FilterByProperty",
|
|
1977
|
-
variable: nodeVariable,
|
|
1978
|
-
property: property2.key.name,
|
|
1979
|
-
value: property2.value.value
|
|
1980
|
-
});
|
|
1981
|
-
}
|
|
1982
|
-
}
|
|
1983
|
-
lastNodeVariable = nodeVariable;
|
|
1984
|
-
}
|
|
1985
|
-
return steps;
|
|
1986
|
-
}
|
|
1987
|
-
createStepsForWhereClause(where) {
|
|
1988
|
-
return [];
|
|
1989
|
-
}
|
|
1990
|
-
createStepsForReturnClause(returnClause2) {
|
|
1991
|
-
return [
|
|
1992
|
-
{
|
|
1993
|
-
type: "ProduceResults",
|
|
1994
|
-
fields: returnClause2.fields.map((field) => ({
|
|
1995
|
-
name: field.path.map((p) => p.name).join("_"),
|
|
1996
|
-
variable: field.path[0].name,
|
|
1997
|
-
path: field.path.length > 1 ? field.path.slice(1).map((p) => p.name).join(".") : "$"
|
|
1998
|
-
}))
|
|
1999
|
-
}
|
|
2000
|
-
];
|
|
2001
|
-
}
|
|
2002
|
-
getOutputFields(returnClause2) {
|
|
2003
|
-
return returnClause2.fields.map((field) => field.path.map((p) => p.name).join("_"));
|
|
2004
|
-
}
|
|
2005
|
-
};
|
|
2006
|
-
var getDirection = (left, right) => {
|
|
2007
|
-
if (left === "-" && right === "->") {
|
|
2008
|
-
return "out";
|
|
2009
|
-
} else if (left === "<-" && right === "-") {
|
|
2010
|
-
return "in";
|
|
2011
|
-
} else if (left === "-" && right === "-") {
|
|
2012
|
-
return "both";
|
|
2013
|
-
}
|
|
2014
|
-
throw new Error(`Invalid direction: ()${left}[]${right}()`);
|
|
2015
|
-
};
|
|
2016
|
-
var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/query-executor.ts";
|
|
2017
|
-
var executeQueryPlan = async (dataSource, plan) => {
|
|
2018
|
-
let buffer = [];
|
|
2019
|
-
const results = [];
|
|
2020
|
-
for (const step of plan.steps) {
|
|
2021
|
-
(0, import_log6.log)("begin step", {
|
|
2022
|
-
step
|
|
2023
|
-
}, {
|
|
2024
|
-
F: __dxlog_file7,
|
|
2025
|
-
L: 41,
|
|
2026
|
-
S: void 0,
|
|
2027
|
-
C: (f, a) => f(...a)
|
|
2028
|
-
});
|
|
2029
|
-
switch (step.type) {
|
|
2030
|
-
case "NodeScan": {
|
|
2031
|
-
const nodes = await dataSource.getNodes({
|
|
2032
|
-
label: step.label
|
|
2033
|
-
});
|
|
2034
|
-
buffer.push(...nodes.map((node) => ({
|
|
2035
|
-
[step.into]: node
|
|
2036
|
-
})));
|
|
2037
|
-
break;
|
|
2038
|
-
}
|
|
2039
|
-
case "FilterByLabel": {
|
|
2040
|
-
buffer = buffer.filter((item) => {
|
|
2041
|
-
const node = item[step.variable];
|
|
2042
|
-
return node && node.kind === "node" && node.label === step.label;
|
|
2043
|
-
});
|
|
2044
|
-
break;
|
|
2045
|
-
}
|
|
2046
|
-
case "FilterByProperty": {
|
|
2047
|
-
buffer = buffer.filter((item) => {
|
|
2048
|
-
const node = item[step.variable];
|
|
2049
|
-
return node && node.kind === "node" && node.properties[step.property] === step.value;
|
|
2050
|
-
});
|
|
2051
|
-
break;
|
|
2052
|
-
}
|
|
2053
|
-
case "RelationshipScan": {
|
|
2054
|
-
const relationships = await dataSource.getRelationships({
|
|
2055
|
-
label: step.label
|
|
2056
|
-
});
|
|
2057
|
-
for (const tuple of [
|
|
2058
|
-
...buffer
|
|
2059
|
-
]) {
|
|
2060
|
-
if (tuple[step.anchor].kind !== "node") {
|
|
2061
|
-
continue;
|
|
2062
|
-
}
|
|
2063
|
-
const filteredRelationships = relationships.filter((relationship) => {
|
|
2064
|
-
switch (step.direction) {
|
|
2065
|
-
case "in":
|
|
2066
|
-
return relationship.target.id === tuple[step.anchor].id;
|
|
2067
|
-
case "out":
|
|
2068
|
-
return relationship.source.id === tuple[step.anchor].id;
|
|
2069
|
-
case "both":
|
|
2070
|
-
return relationship.source.id === tuple[step.anchor].id || relationship.target.id === tuple[step.anchor].id;
|
|
2071
|
-
default:
|
|
2072
|
-
throw new Error(`Unknown direction: ${step.direction}`);
|
|
2073
|
-
}
|
|
2074
|
-
});
|
|
2075
|
-
for (let i = 0; i < filteredRelationships.length; i++) {
|
|
2076
|
-
const relationship = filteredRelationships[i];
|
|
2077
|
-
const newTuple = i === 0 ? tuple : {
|
|
2078
|
-
...tuple
|
|
2079
|
-
};
|
|
2080
|
-
if (newTuple !== tuple) {
|
|
2081
|
-
buffer.push(newTuple);
|
|
2082
|
-
}
|
|
2083
|
-
newTuple[step.relationshipInto] = relationship;
|
|
2084
|
-
switch (step.direction) {
|
|
2085
|
-
case "in":
|
|
2086
|
-
newTuple[step.nodeInto] = relationship.source;
|
|
2087
|
-
break;
|
|
2088
|
-
case "out":
|
|
2089
|
-
newTuple[step.nodeInto] = relationship.target;
|
|
2090
|
-
break;
|
|
2091
|
-
case "both":
|
|
2092
|
-
newTuple[step.nodeInto] = relationship.source.id === newTuple[step.anchor].id ? relationship.target : relationship.source;
|
|
2093
|
-
break;
|
|
2094
|
-
}
|
|
2095
|
-
}
|
|
2096
|
-
}
|
|
2097
|
-
break;
|
|
2098
|
-
}
|
|
2099
|
-
case "ProduceResults": {
|
|
2100
|
-
for (const tuple of buffer) {
|
|
2101
|
-
const result = {};
|
|
2102
|
-
for (const field of step.fields) {
|
|
2103
|
-
result[field.name] = jsonPathGet(tuple[field.variable].properties, field.path);
|
|
2104
|
-
}
|
|
2105
|
-
results.push(result);
|
|
2106
|
-
}
|
|
2107
|
-
break;
|
|
2108
|
-
}
|
|
2109
|
-
}
|
|
2110
|
-
(0, import_log6.log)("finish step", {
|
|
2111
|
-
buffer: buffer.map((tuple) => (0, import_util2.mapValues)(tuple, (x) => x.id)),
|
|
2112
|
-
results
|
|
2113
|
-
}, {
|
|
2114
|
-
F: __dxlog_file7,
|
|
2115
|
-
L: 118,
|
|
2116
|
-
S: void 0,
|
|
2117
|
-
C: (f, a) => f(...a)
|
|
2118
|
-
});
|
|
2119
|
-
}
|
|
2120
|
-
return results;
|
|
2121
|
-
};
|
|
2122
|
-
var executeQuery = (dataSource, query) => {
|
|
2123
|
-
const plan = createExecutionPlan(parseCypherQuery(query));
|
|
2124
|
-
return executeQueryPlan(dataSource, plan);
|
|
2125
|
-
};
|
|
2126
|
-
var jsonPathGet = (obj, path) => {
|
|
2127
|
-
if (path === "" || path === "$") {
|
|
2128
|
-
return obj;
|
|
2129
|
-
}
|
|
2130
|
-
const parts = path.split(".");
|
|
2131
|
-
if (parts[0] === "$") {
|
|
2132
|
-
parts.shift();
|
|
2133
|
-
}
|
|
2134
|
-
let result = obj;
|
|
2135
|
-
for (const part of parts) {
|
|
2136
|
-
result = result?.[part];
|
|
2137
|
-
}
|
|
2138
|
-
return result;
|
|
2139
|
-
};
|
|
2140
|
-
var trim = (strings, ...values) => {
|
|
2141
|
-
const combined = strings.reduce((acc, str, i) => {
|
|
2142
|
-
return acc + str + (values[i] || "");
|
|
2143
|
-
}, "");
|
|
2144
|
-
return combined.replace("/^\n/", "").replace(/\n$/, "");
|
|
2145
|
-
};
|
|
2146
|
-
var formatJsonSchemaForLLM = (schemaTypes) => {
|
|
2147
|
-
return schemaTypes.flatMap((schema) => [
|
|
2148
|
-
formatNodeSchema(schema),
|
|
2149
|
-
...formatInferredRelationships(schema, schemaTypes)
|
|
2150
|
-
]);
|
|
2151
|
-
};
|
|
2152
|
-
var formatNodeSchema = (schema) => {
|
|
2153
|
-
const label2 = formatNodeLabel(schema.typename ?? (0, import_debug.raise)(new Error("Schema must have $id")));
|
|
2154
|
-
const properties2 = Object.entries(schema.properties).filter(([key, prop]) => key !== "id" && !isReference(prop));
|
|
2155
|
-
return trim`
|
|
2156
|
-
<node>
|
|
2157
|
-
(:${label2})
|
|
2158
|
-
|
|
2159
|
-
<description>${schema.description}</description>
|
|
2160
|
-
|
|
2161
|
-
<properties>
|
|
2162
|
-
${properties2.map(([key, value]) => {
|
|
2163
|
-
return trim`
|
|
2164
|
-
<${key}>
|
|
2165
|
-
<type>${value.type}</type>
|
|
2166
|
-
<description>${value.description}</description>
|
|
2167
|
-
</${key}>
|
|
2168
|
-
`;
|
|
2169
|
-
})}
|
|
2170
|
-
</properties>
|
|
2171
|
-
</node>
|
|
2172
|
-
`;
|
|
2173
|
-
};
|
|
2174
|
-
var formatInferredRelationships = (schema, allSchema) => {
|
|
2175
|
-
const nodeLabel = formatNodeLabel(schema.typename ?? (0, import_debug.raise)(new Error("Schema must have $id")));
|
|
2176
|
-
const relationships = Object.entries(schema.properties).filter(([key, prop]) => isReference(prop));
|
|
2177
|
-
return relationships.map(([key, value]) => {
|
|
2178
|
-
const relationshipLabel = formatInferredRelationshipLabel(schema.typename ?? (0, import_debug.raise)(new Error("Schema must have $id")), key);
|
|
2179
|
-
const targetSchema = allSchema.find((schema2) => `dxn:type/${schema2.typename}` === value.reference?.schema.$ref);
|
|
2180
|
-
const targetLabel = targetSchema ? formatNodeLabel(targetSchema?.typename ?? "") : "";
|
|
2181
|
-
return trim`
|
|
2182
|
-
<relationship>
|
|
2183
|
-
(:${nodeLabel})-[:${relationshipLabel}]->(:${targetLabel})
|
|
2184
|
-
|
|
2185
|
-
<description>${value.description}</description>
|
|
2186
|
-
</relationship>
|
|
2187
|
-
`;
|
|
2188
|
-
});
|
|
2189
|
-
};
|
|
2190
|
-
var formatNodeLabel = (typename) => {
|
|
2191
|
-
return typename.replace(/^example\.com\/type\//g, "");
|
|
2192
|
-
};
|
|
2193
|
-
var formatInferredRelationshipLabel = (typename, property2) => {
|
|
2194
|
-
const nodeLabel = formatNodeLabel(typename);
|
|
2195
|
-
return `${nodeLabel}_${property2}`.toUpperCase();
|
|
2196
|
-
};
|
|
2197
|
-
var isReference = (schema) => schema.$id === import_echo_schema5.JSON_SCHEMA_ECHO_REF_ID || schema.type === "array" && !Array.isArray(schema.items) && typeof schema.items === "object" && "$id" in schema.items && schema.items.$id === import_echo_schema5.JSON_SCHEMA_ECHO_REF_ID;
|
|
2198
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
2199
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2200
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
2201
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2202
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2203
|
-
}
|
|
2204
|
-
var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/echo-data-source.ts";
|
|
2205
|
-
var EchoDataSource = class {
|
|
2206
|
-
constructor(_db) {
|
|
2207
|
-
this._db = _db;
|
|
2208
|
-
}
|
|
2209
|
-
async getNodes({ label: label2 }) {
|
|
2210
|
-
if (!label2) {
|
|
2211
|
-
const { objects: objects2 } = await this._db.query().run();
|
|
2212
|
-
return objects2.map(this._objectToNode);
|
|
2213
|
-
}
|
|
2214
|
-
const schema = (await this._getAllSchema()).find((s) => (0, import_echo_schema6.getSchemaTypename)(s)?.endsWith(label2));
|
|
2215
|
-
if (!schema) {
|
|
2216
|
-
return [];
|
|
2217
|
-
}
|
|
2218
|
-
const { objects } = await this._db.query(import_echo_db.Filter.schema(schema)).run();
|
|
2219
|
-
return objects.map(this._objectToNode);
|
|
2220
|
-
}
|
|
2221
|
-
async getRelationships({ label: label2 }) {
|
|
2222
|
-
const syntheticRefRelationships = (await this._getAllSchema()).filter((schema) => (0, import_echo_schema6.getSchemaTypename)(schema) !== import_echo_schema6.StoredSchema.typename).flatMap((schema) => {
|
|
2223
|
-
const jsonSchema = (0, import_echo_schema6.toJsonSchema)(schema);
|
|
2224
|
-
const relationships = Object.entries(jsonSchema.properties).filter(([key, prop]) => isReference(prop));
|
|
2225
|
-
return relationships.map(([key, value]) => {
|
|
2226
|
-
const relationshipLabel = formatInferredRelationshipLabel(jsonSchema.typename ?? (0, import_debug2.raise)(new Error("Schema must have typename")), key);
|
|
2227
|
-
return {
|
|
2228
|
-
label: relationshipLabel,
|
|
2229
|
-
schema,
|
|
2230
|
-
property: key
|
|
2231
|
-
};
|
|
2232
|
-
});
|
|
2233
|
-
});
|
|
2234
|
-
const relationship = syntheticRefRelationships.find((r) => r.label === label2);
|
|
2235
|
-
if (!relationship) {
|
|
2236
|
-
return [];
|
|
2237
|
-
}
|
|
2238
|
-
const { objects } = await this._db.query(import_echo_db.Filter.schema(relationship.schema)).run();
|
|
2239
|
-
return (await Promise.all(objects.map((object) => this._projectRefRelationship(object, relationship.property)))).flat();
|
|
2240
|
-
}
|
|
2241
|
-
async _getAllSchema() {
|
|
2242
|
-
return [
|
|
2243
|
-
...await this._db.schemaRegistry.query().run(),
|
|
2244
|
-
// TODO(dmaretskyi): Remove once we can serialize recursive schema.
|
|
2245
|
-
...this._db.graph.schemaRegistry.schemas.filter((s) => (0, import_echo_schema6.getSchemaTypename)(s)?.startsWith("example.org"))
|
|
2246
|
-
].filter((schema) => (0, import_echo_schema6.getSchemaTypename)(schema) !== import_echo_schema6.StoredSchema.typename);
|
|
2247
|
-
}
|
|
2248
|
-
_objectToNode(object) {
|
|
2249
|
-
const { id, ...properties2 } = object;
|
|
2250
|
-
return {
|
|
2251
|
-
id,
|
|
2252
|
-
kind: "node",
|
|
2253
|
-
label: formatNodeLabel((0, import_echo_schema6.getSchemaTypename)((0, import_live_object.getSchema)(object))),
|
|
2254
|
-
properties: properties2
|
|
2255
|
-
};
|
|
2256
|
-
}
|
|
2257
|
-
async _projectRefRelationship(object, prop) {
|
|
2258
|
-
if (!object[prop]) {
|
|
2259
|
-
return [];
|
|
2260
|
-
}
|
|
2261
|
-
const value = Array.isArray(object[prop]) ? object[prop] : [
|
|
2262
|
-
object[prop]
|
|
2263
|
-
];
|
|
2264
|
-
return Promise.all(value.map(async (ref) => {
|
|
2265
|
-
const target = await ref.load();
|
|
2266
|
-
return {
|
|
2267
|
-
id: `${object.id}-${prop}-${target.id}`,
|
|
2268
|
-
kind: "relationship",
|
|
2269
|
-
label: formatInferredRelationshipLabel((0, import_echo_schema6.getSchemaTypename)((0, import_live_object.getSchema)(object)), prop),
|
|
2270
|
-
properties: {},
|
|
2271
|
-
source: this._objectToNode(object),
|
|
2272
|
-
target: this._objectToNode(target)
|
|
2273
|
-
};
|
|
2274
|
-
}));
|
|
2275
|
-
}
|
|
2276
|
-
};
|
|
2277
|
-
_ts_decorate([
|
|
2278
|
-
import_log8.log.method(void 0, void 0, {
|
|
2279
|
-
F: __dxlog_file8,
|
|
2280
|
-
L: 17,
|
|
2281
|
-
S: void 0,
|
|
2282
|
-
C: (f, a) => f(...a)
|
|
2283
|
-
})
|
|
2284
|
-
], EchoDataSource.prototype, "getNodes", null);
|
|
2285
|
-
_ts_decorate([
|
|
2286
|
-
import_log8.log.method(void 0, void 0, {
|
|
2287
|
-
F: __dxlog_file8,
|
|
2288
|
-
L: 34,
|
|
2289
|
-
S: void 0,
|
|
2290
|
-
C: (f, a) => f(...a)
|
|
2291
|
-
})
|
|
2292
|
-
], EchoDataSource.prototype, "getRelationships", null);
|
|
2293
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2294
|
-
0 && (module.exports = {
|
|
2295
|
-
AIServiceEdgeClient,
|
|
2296
|
-
DEFAULT_EDGE_MODEL,
|
|
2297
|
-
DEFAULT_EDGE_MODELS,
|
|
2298
|
-
DEFAULT_OLLAMA_ENDPOINT,
|
|
2299
|
-
DEFAULT_OLLAMA_MODEL,
|
|
2300
|
-
DEFAULT_OLLAMA_MODELS,
|
|
2301
|
-
EchoDataSource,
|
|
2302
|
-
GenerateRequest,
|
|
2303
|
-
GenerationStreamImpl,
|
|
2304
|
-
LLMModel,
|
|
2305
|
-
MessageCollector,
|
|
2306
|
-
MixedStreamParser,
|
|
2307
|
-
OllamaClient,
|
|
2308
|
-
StreamTransform,
|
|
2309
|
-
ToolTypes,
|
|
2310
|
-
createArtifactElement,
|
|
2311
|
-
createGenerationStream,
|
|
2312
|
-
emitMessageAsEvents,
|
|
2313
|
-
executeQuery,
|
|
2314
|
-
executeQueryPlan,
|
|
2315
|
-
formatInferredRelationshipLabel,
|
|
2316
|
-
formatJsonSchemaForLLM,
|
|
2317
|
-
formatNodeLabel,
|
|
2318
|
-
isReference,
|
|
2319
|
-
isToolUse,
|
|
2320
|
-
mergeMessageBlock,
|
|
2321
|
-
parseCypherQuery,
|
|
2322
|
-
runLLM,
|
|
2323
|
-
runTools,
|
|
2324
|
-
trim
|
|
2325
|
-
});
|
|
2326
|
-
//# sourceMappingURL=chunk-IBOBRBDF.cjs.map
|