@dxos/assistant 0.8.1-main.ba2dec9 → 0.8.1-staging.5be625a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/chunk-BDMTBYIM.mjs +2285 -0
- package/dist/lib/browser/chunk-BDMTBYIM.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +313 -1631
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +15 -1
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/chunk-A44NGSCM.cjs +2296 -0
- package/dist/lib/node/chunk-A44NGSCM.cjs.map +7 -0
- package/dist/lib/node/index.cjs +321 -1629
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +18 -7
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node-esm/chunk-J6DMSFSZ.mjs +2287 -0
- package/dist/lib/node-esm/chunk-J6DMSFSZ.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +313 -1631
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +15 -1
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/ai-service/defs.d.ts +8 -2
- package/dist/types/src/ai-service/defs.d.ts.map +1 -1
- package/dist/types/src/ai-service/edge-client.d.ts.map +1 -1
- package/dist/types/src/ai-service/message-collector.d.ts +16 -16
- package/dist/types/src/ai-service/message-collector.d.ts.map +1 -1
- package/dist/types/src/ai-service/ollama-client.d.ts +1 -5
- package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -1
- package/dist/types/src/ai-service/parser.d.ts +8 -8
- package/dist/types/src/ai-service/parser.d.ts.map +1 -1
- package/dist/types/src/ai-service/types.d.ts +90 -156
- package/dist/types/src/ai-service/types.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +8 -8
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +2 -0
- package/dist/types/src/session/index.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +215 -0
- package/dist/types/src/session/session.d.ts.map +1 -0
- package/dist/types/src/session/session.test.d.ts +2 -0
- package/dist/types/src/session/session.test.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/ollama.d.ts +6 -0
- package/dist/types/src/testing/ollama.d.ts.map +1 -0
- package/package.json +13 -13
- package/src/ai-service/client.test.ts +10 -10
- package/src/ai-service/defs.ts +18 -3
- package/src/ai-service/edge-client.ts +3 -2
- package/src/ai-service/ollama-client.ts +49 -23
- package/src/ai-service/parser.ts +1 -0
- package/src/ai-service/types.ts +2 -2
- package/src/bin/chat.ts +2 -2
- package/src/conversation/conversation.test.ts +3 -3
- package/src/index.ts +1 -0
- package/src/session/index.ts +5 -0
- package/src/session/session.test.ts +256 -0
- package/src/session/session.ts +369 -0
- package/src/testing/artifacts.test.ts +2 -2
- package/src/testing/index.ts +1 -0
- package/src/testing/object-extraction.test.ts +1 -1
- package/src/testing/ollama.ts +15 -0
- package/src/testing/query.test.ts +3 -3
- package/dist/lib/browser/chunk-AXUUNA7O.mjs +0 -565
- package/dist/lib/browser/chunk-AXUUNA7O.mjs.map +0 -7
- package/dist/lib/node/chunk-QPHGQINW.cjs +0 -577
- package/dist/lib/node/chunk-QPHGQINW.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-GOVFOBIB.mjs +0 -567
- package/dist/lib/node-esm/chunk-GOVFOBIB.mjs.map +0 -7
|
@@ -1,577 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var chunk_QPHGQINW_exports = {};
|
|
20
|
-
__export(chunk_QPHGQINW_exports, {
|
|
21
|
-
EchoDataSource: () => EchoDataSource,
|
|
22
|
-
executeQuery: () => executeQuery,
|
|
23
|
-
executeQueryPlan: () => executeQueryPlan,
|
|
24
|
-
formatInferredRelationshipLabel: () => formatInferredRelationshipLabel,
|
|
25
|
-
formatJsonSchemaForLLM: () => formatJsonSchemaForLLM,
|
|
26
|
-
formatNodeLabel: () => formatNodeLabel,
|
|
27
|
-
isReference: () => isReference,
|
|
28
|
-
parseCypherQuery: () => parseCypherQuery,
|
|
29
|
-
trim: () => trim
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(chunk_QPHGQINW_exports);
|
|
32
|
-
var import_parjs = require("#parjs");
|
|
33
|
-
var import_log = require("@dxos/log");
|
|
34
|
-
var import_util = require("@dxos/util");
|
|
35
|
-
var import_invariant = require("@dxos/invariant");
|
|
36
|
-
var import_log2 = require("@dxos/log");
|
|
37
|
-
var import_debug = require("@dxos/debug");
|
|
38
|
-
var import_echo_schema = require("@dxos/echo-schema");
|
|
39
|
-
var import_debug2 = require("@dxos/debug");
|
|
40
|
-
var import_echo_db = require("@dxos/echo-db");
|
|
41
|
-
var import_echo_schema2 = require("@dxos/echo-schema");
|
|
42
|
-
var import_live_object = require("@dxos/live-object");
|
|
43
|
-
var import_log3 = require("@dxos/log");
|
|
44
|
-
var ws = import_parjs.parjs.whitespace();
|
|
45
|
-
var nothing = import_parjs.parjs.string("").pipe(import_parjs.parjsCombinators.map(() => null));
|
|
46
|
-
var optional = (parser) => parser.pipe(import_parjs.parjsCombinators.recover(() => ({
|
|
47
|
-
kind: "Soft"
|
|
48
|
-
})), import_parjs.parjsCombinators.or(nothing));
|
|
49
|
-
var keyword = (word) => import_parjs.parjs.string(word).pipe(import_parjs.parjsCombinators.map(() => word));
|
|
50
|
-
var identifier = import_parjs.parjs.regexp(/[a-zA-Z_][a-zA-Z0-9_]*/).pipe(import_parjs.parjsCombinators.map((match) => ({
|
|
51
|
-
astKind: "Identifier",
|
|
52
|
-
name: match[0]
|
|
53
|
-
})));
|
|
54
|
-
var numberLiteral = import_parjs.parjs.regexp(/-?\d+(\.\d+)?/).pipe(import_parjs.parjsCombinators.map(Number));
|
|
55
|
-
var stringLiteral = import_parjs.parjs.regexp(/"([^"]*)"|'([^']*)'/).pipe(import_parjs.parjsCombinators.map((match) => ({
|
|
56
|
-
astKind: "StringLiteral",
|
|
57
|
-
value: match[1] || match[2]
|
|
58
|
-
})));
|
|
59
|
-
var variableTag = identifier.pipe(import_parjs.parjsCombinators.map((variable) => variable)).expects("variable tag");
|
|
60
|
-
var label = identifier.pipe(import_parjs.parjsCombinators.map((name) => name));
|
|
61
|
-
var property = identifier.pipe(import_parjs.parjsCombinators.then(import_parjs.parjs.string(":"), ws, stringLiteral), import_parjs.parjsCombinators.map(([key, , , value]) => ({
|
|
62
|
-
key,
|
|
63
|
-
value
|
|
64
|
-
})));
|
|
65
|
-
var properties = property.pipe(import_parjs.parjsCombinators.manySepBy(","), import_parjs.parjsCombinators.between("{", "}"), import_parjs.parjsCombinators.map((properties2) => ({
|
|
66
|
-
astKind: "Properties",
|
|
67
|
-
properties: properties2
|
|
68
|
-
})));
|
|
69
|
-
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]) => ({
|
|
70
|
-
astKind: "NodePattern",
|
|
71
|
-
variable,
|
|
72
|
-
label: label2,
|
|
73
|
-
properties: properties2
|
|
74
|
-
})));
|
|
75
|
-
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]) => ({
|
|
76
|
-
astKind: "RelationshipPattern",
|
|
77
|
-
variable,
|
|
78
|
-
label: label2,
|
|
79
|
-
properties: properties2
|
|
80
|
-
})));
|
|
81
|
-
var graphPatternConnector = import_parjs.parjs.string("->").pipe(import_parjs.parjsCombinators.or("<-", "-"), import_parjs.parjsCombinators.map((x) => ({
|
|
82
|
-
astKind: "GraphPatternConnector",
|
|
83
|
-
direction: x
|
|
84
|
-
})));
|
|
85
|
-
var graphPattern = nodePattern.pipe(import_parjs.parjsCombinators.or(relationshipPattern), import_parjs.parjsCombinators.manySepBy(graphPatternConnector), import_parjs.parjsCombinators.map((segments) => ({
|
|
86
|
-
astKind: "GraphPattern",
|
|
87
|
-
segments
|
|
88
|
-
})));
|
|
89
|
-
var matchClause = keyword("MATCH").pipe(import_parjs.parjsCombinators.then(ws), import_parjs.parjsCombinators.then(graphPattern), import_parjs.parjsCombinators.map(([, pattern]) => ({
|
|
90
|
-
astKind: "MatchClause",
|
|
91
|
-
pattern
|
|
92
|
-
})));
|
|
93
|
-
var memberExpression = identifier.pipe(import_parjs.parjsCombinators.manySepBy("."), import_parjs.parjsCombinators.map((path) => ({
|
|
94
|
-
astKind: "MemberExpression",
|
|
95
|
-
path
|
|
96
|
-
})));
|
|
97
|
-
var equalsPredicate = memberExpression.pipe(import_parjs.parjsCombinators.then(ws, import_parjs.parjs.string("="), ws, stringLiteral), import_parjs.parjsCombinators.map(([left, , , , right]) => ({
|
|
98
|
-
astKind: "EqualsPredicate",
|
|
99
|
-
left,
|
|
100
|
-
right
|
|
101
|
-
})));
|
|
102
|
-
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]) => ({
|
|
103
|
-
astKind: "WhereClause",
|
|
104
|
-
predicates
|
|
105
|
-
})));
|
|
106
|
-
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]) => ({
|
|
107
|
-
astKind: "ReturnClause",
|
|
108
|
-
fields: fields.map(([f]) => f)
|
|
109
|
-
})));
|
|
110
|
-
var cypherQuery = ws.pipe(import_parjs.parjsCombinators.then(matchClause, ws, optional(whereClause), ws, returnClause, ws), import_parjs.parjsCombinators.map(([, matchClause2, , whereClause2, , returnClause2]) => ({
|
|
111
|
-
astKind: "CypherQuery",
|
|
112
|
-
matchClause: matchClause2,
|
|
113
|
-
whereClause: whereClause2,
|
|
114
|
-
returnClause: returnClause2
|
|
115
|
-
})));
|
|
116
|
-
var parseCypherQuery = (query) => {
|
|
117
|
-
const parseResult = cypherQuery.parse(query);
|
|
118
|
-
if (parseResult.isOk) {
|
|
119
|
-
return {
|
|
120
|
-
ast: parseResult.value
|
|
121
|
-
};
|
|
122
|
-
} else {
|
|
123
|
-
throw new Error(`Failed to parse query: ${parseResult}`);
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/execution-plan.ts";
|
|
127
|
-
var createExecutionPlan = (query) => {
|
|
128
|
-
return new ExecutionPlanBuilder().build(query);
|
|
129
|
-
};
|
|
130
|
-
var ExecutionPlanBuilder = class {
|
|
131
|
-
constructor() {
|
|
132
|
-
this.counter = 0;
|
|
133
|
-
}
|
|
134
|
-
newVariable() {
|
|
135
|
-
return `anon_${this.counter++}`;
|
|
136
|
-
}
|
|
137
|
-
build(query) {
|
|
138
|
-
(0, import_log2.log)("Creating execution plan for query", {
|
|
139
|
-
query
|
|
140
|
-
}, {
|
|
141
|
-
F: __dxlog_file,
|
|
142
|
-
L: 76,
|
|
143
|
-
S: this,
|
|
144
|
-
C: (f, a) => f(...a)
|
|
145
|
-
});
|
|
146
|
-
return {
|
|
147
|
-
steps: [
|
|
148
|
-
...this.createStepsForMatchClause(query.ast.matchClause),
|
|
149
|
-
...query.ast.whereClause ? this.createStepsForWhereClause(query.ast.whereClause) : [],
|
|
150
|
-
...this.createStepsForReturnClause(query.ast.returnClause)
|
|
151
|
-
],
|
|
152
|
-
output: {
|
|
153
|
-
fields: this.getOutputFields(query.ast.returnClause)
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
createStepsForMatchClause(match) {
|
|
158
|
-
(0, import_invariant.invariant)(match.pattern.segments.length > 0, void 0, {
|
|
159
|
-
F: __dxlog_file,
|
|
160
|
-
L: 90,
|
|
161
|
-
S: this,
|
|
162
|
-
A: [
|
|
163
|
-
"match.pattern.segments.length > 0",
|
|
164
|
-
""
|
|
165
|
-
]
|
|
166
|
-
});
|
|
167
|
-
const firstSegment = match.pattern.segments[0];
|
|
168
|
-
(0, import_invariant.invariant)(firstSegment.astKind === "NodePattern", void 0, {
|
|
169
|
-
F: __dxlog_file,
|
|
170
|
-
L: 92,
|
|
171
|
-
S: this,
|
|
172
|
-
A: [
|
|
173
|
-
"firstSegment.astKind === 'NodePattern'",
|
|
174
|
-
""
|
|
175
|
-
]
|
|
176
|
-
});
|
|
177
|
-
let lastNodeVariable = firstSegment.variable?.name ?? this.newVariable();
|
|
178
|
-
const steps = [];
|
|
179
|
-
steps.push({
|
|
180
|
-
type: "NodeScan",
|
|
181
|
-
label: firstSegment.label.name,
|
|
182
|
-
into: lastNodeVariable
|
|
183
|
-
});
|
|
184
|
-
if (firstSegment.properties) {
|
|
185
|
-
for (const property2 of firstSegment.properties.properties) {
|
|
186
|
-
steps.push({
|
|
187
|
-
type: "FilterByProperty",
|
|
188
|
-
variable: lastNodeVariable,
|
|
189
|
-
property: property2.key.name,
|
|
190
|
-
value: property2.value.value
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
for (let i = 1; i < match.pattern.segments.length; i++) {
|
|
195
|
-
const relationshipSegment = match.pattern.segments[i++];
|
|
196
|
-
(0, import_invariant.invariant)(relationshipSegment.astKind === "RelationshipPattern", void 0, {
|
|
197
|
-
F: __dxlog_file,
|
|
198
|
-
L: 115,
|
|
199
|
-
S: this,
|
|
200
|
-
A: [
|
|
201
|
-
"relationshipSegment.astKind === 'RelationshipPattern'",
|
|
202
|
-
""
|
|
203
|
-
]
|
|
204
|
-
});
|
|
205
|
-
const nodeSegment = match.pattern.segments[i];
|
|
206
|
-
(0, import_invariant.invariant)(nodeSegment.astKind === "NodePattern", void 0, {
|
|
207
|
-
F: __dxlog_file,
|
|
208
|
-
L: 117,
|
|
209
|
-
S: this,
|
|
210
|
-
A: [
|
|
211
|
-
"nodeSegment.astKind === 'NodePattern'",
|
|
212
|
-
""
|
|
213
|
-
]
|
|
214
|
-
});
|
|
215
|
-
const leftSeparator = match.pattern.segments.separators[i - 2];
|
|
216
|
-
const rightSeparator = match.pattern.segments.separators[i - 1];
|
|
217
|
-
const direction = getDirection(leftSeparator.direction, rightSeparator.direction);
|
|
218
|
-
const relationshipVariable = relationshipSegment.variable?.name ?? this.newVariable();
|
|
219
|
-
const nodeVariable = nodeSegment.variable?.name ?? this.newVariable();
|
|
220
|
-
steps.push({
|
|
221
|
-
type: "RelationshipScan",
|
|
222
|
-
label: relationshipSegment.label.name,
|
|
223
|
-
direction,
|
|
224
|
-
anchor: lastNodeVariable,
|
|
225
|
-
relationshipInto: relationshipVariable,
|
|
226
|
-
nodeInto: nodeVariable
|
|
227
|
-
});
|
|
228
|
-
if (relationshipSegment.properties) {
|
|
229
|
-
for (const property2 of relationshipSegment.properties.properties) {
|
|
230
|
-
steps.push({
|
|
231
|
-
type: "FilterByProperty",
|
|
232
|
-
variable: relationshipVariable,
|
|
233
|
-
property: property2.key.name,
|
|
234
|
-
value: property2.value.value
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (nodeSegment.label) {
|
|
239
|
-
steps.push({
|
|
240
|
-
type: "FilterByLabel",
|
|
241
|
-
variable: nodeVariable,
|
|
242
|
-
label: nodeSegment.label.name
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
if (nodeSegment.properties) {
|
|
246
|
-
for (const property2 of nodeSegment.properties.properties) {
|
|
247
|
-
steps.push({
|
|
248
|
-
type: "FilterByProperty",
|
|
249
|
-
variable: nodeVariable,
|
|
250
|
-
property: property2.key.name,
|
|
251
|
-
value: property2.value.value
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
lastNodeVariable = nodeVariable;
|
|
256
|
-
}
|
|
257
|
-
return steps;
|
|
258
|
-
}
|
|
259
|
-
createStepsForWhereClause(where) {
|
|
260
|
-
return [];
|
|
261
|
-
}
|
|
262
|
-
createStepsForReturnClause(returnClause2) {
|
|
263
|
-
return [
|
|
264
|
-
{
|
|
265
|
-
type: "ProduceResults",
|
|
266
|
-
fields: returnClause2.fields.map((field) => ({
|
|
267
|
-
name: field.path.map((p) => p.name).join("_"),
|
|
268
|
-
variable: field.path[0].name,
|
|
269
|
-
path: field.path.length > 1 ? field.path.slice(1).map((p) => p.name).join(".") : "$"
|
|
270
|
-
}))
|
|
271
|
-
}
|
|
272
|
-
];
|
|
273
|
-
}
|
|
274
|
-
getOutputFields(returnClause2) {
|
|
275
|
-
return returnClause2.fields.map((field) => field.path.map((p) => p.name).join("_"));
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
var getDirection = (left, right) => {
|
|
279
|
-
if (left === "-" && right === "->") {
|
|
280
|
-
return "out";
|
|
281
|
-
} else if (left === "<-" && right === "-") {
|
|
282
|
-
return "in";
|
|
283
|
-
} else if (left === "-" && right === "-") {
|
|
284
|
-
return "both";
|
|
285
|
-
}
|
|
286
|
-
throw new Error(`Invalid direction: ()${left}[]${right}()`);
|
|
287
|
-
};
|
|
288
|
-
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/query-executor.ts";
|
|
289
|
-
var executeQueryPlan = async (dataSource, plan) => {
|
|
290
|
-
let buffer = [];
|
|
291
|
-
const results = [];
|
|
292
|
-
for (const step of plan.steps) {
|
|
293
|
-
(0, import_log.log)("begin step", {
|
|
294
|
-
step
|
|
295
|
-
}, {
|
|
296
|
-
F: __dxlog_file2,
|
|
297
|
-
L: 41,
|
|
298
|
-
S: void 0,
|
|
299
|
-
C: (f, a) => f(...a)
|
|
300
|
-
});
|
|
301
|
-
switch (step.type) {
|
|
302
|
-
case "NodeScan": {
|
|
303
|
-
const nodes = await dataSource.getNodes({
|
|
304
|
-
label: step.label
|
|
305
|
-
});
|
|
306
|
-
buffer.push(...nodes.map((node) => ({
|
|
307
|
-
[step.into]: node
|
|
308
|
-
})));
|
|
309
|
-
break;
|
|
310
|
-
}
|
|
311
|
-
case "FilterByLabel": {
|
|
312
|
-
buffer = buffer.filter((item) => {
|
|
313
|
-
const node = item[step.variable];
|
|
314
|
-
return node && node.kind === "node" && node.label === step.label;
|
|
315
|
-
});
|
|
316
|
-
break;
|
|
317
|
-
}
|
|
318
|
-
case "FilterByProperty": {
|
|
319
|
-
buffer = buffer.filter((item) => {
|
|
320
|
-
const node = item[step.variable];
|
|
321
|
-
return node && node.kind === "node" && node.properties[step.property] === step.value;
|
|
322
|
-
});
|
|
323
|
-
break;
|
|
324
|
-
}
|
|
325
|
-
case "RelationshipScan": {
|
|
326
|
-
const relationships = await dataSource.getRelationships({
|
|
327
|
-
label: step.label
|
|
328
|
-
});
|
|
329
|
-
for (const tuple of [
|
|
330
|
-
...buffer
|
|
331
|
-
]) {
|
|
332
|
-
if (tuple[step.anchor].kind !== "node") {
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
const filteredRelationships = relationships.filter((relationship) => {
|
|
336
|
-
switch (step.direction) {
|
|
337
|
-
case "in":
|
|
338
|
-
return relationship.target.id === tuple[step.anchor].id;
|
|
339
|
-
case "out":
|
|
340
|
-
return relationship.source.id === tuple[step.anchor].id;
|
|
341
|
-
case "both":
|
|
342
|
-
return relationship.source.id === tuple[step.anchor].id || relationship.target.id === tuple[step.anchor].id;
|
|
343
|
-
default:
|
|
344
|
-
throw new Error(`Unknown direction: ${step.direction}`);
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
for (let i = 0; i < filteredRelationships.length; i++) {
|
|
348
|
-
const relationship = filteredRelationships[i];
|
|
349
|
-
const newTuple = i === 0 ? tuple : {
|
|
350
|
-
...tuple
|
|
351
|
-
};
|
|
352
|
-
if (newTuple !== tuple) {
|
|
353
|
-
buffer.push(newTuple);
|
|
354
|
-
}
|
|
355
|
-
newTuple[step.relationshipInto] = relationship;
|
|
356
|
-
switch (step.direction) {
|
|
357
|
-
case "in":
|
|
358
|
-
newTuple[step.nodeInto] = relationship.source;
|
|
359
|
-
break;
|
|
360
|
-
case "out":
|
|
361
|
-
newTuple[step.nodeInto] = relationship.target;
|
|
362
|
-
break;
|
|
363
|
-
case "both":
|
|
364
|
-
newTuple[step.nodeInto] = relationship.source.id === newTuple[step.anchor].id ? relationship.target : relationship.source;
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
case "ProduceResults": {
|
|
372
|
-
for (const tuple of buffer) {
|
|
373
|
-
const result = {};
|
|
374
|
-
for (const field of step.fields) {
|
|
375
|
-
result[field.name] = jsonPathGet(tuple[field.variable].properties, field.path);
|
|
376
|
-
}
|
|
377
|
-
results.push(result);
|
|
378
|
-
}
|
|
379
|
-
break;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
(0, import_log.log)("finish step", {
|
|
383
|
-
buffer: buffer.map((tuple) => (0, import_util.mapValues)(tuple, (x) => x.id)),
|
|
384
|
-
results
|
|
385
|
-
}, {
|
|
386
|
-
F: __dxlog_file2,
|
|
387
|
-
L: 118,
|
|
388
|
-
S: void 0,
|
|
389
|
-
C: (f, a) => f(...a)
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
return results;
|
|
393
|
-
};
|
|
394
|
-
var executeQuery = (dataSource, query) => {
|
|
395
|
-
const plan = createExecutionPlan(parseCypherQuery(query));
|
|
396
|
-
return executeQueryPlan(dataSource, plan);
|
|
397
|
-
};
|
|
398
|
-
var jsonPathGet = (obj, path) => {
|
|
399
|
-
if (path === "" || path === "$") {
|
|
400
|
-
return obj;
|
|
401
|
-
}
|
|
402
|
-
const parts = path.split(".");
|
|
403
|
-
if (parts[0] === "$") {
|
|
404
|
-
parts.shift();
|
|
405
|
-
}
|
|
406
|
-
let result = obj;
|
|
407
|
-
for (const part of parts) {
|
|
408
|
-
result = result?.[part];
|
|
409
|
-
}
|
|
410
|
-
return result;
|
|
411
|
-
};
|
|
412
|
-
var trim = (strings, ...values) => {
|
|
413
|
-
const combined = strings.reduce((acc, str, i) => {
|
|
414
|
-
return acc + str + (values[i] || "");
|
|
415
|
-
}, "");
|
|
416
|
-
return combined.replace("/^\n/", "").replace(/\n$/, "");
|
|
417
|
-
};
|
|
418
|
-
var formatJsonSchemaForLLM = (schemaTypes) => {
|
|
419
|
-
return schemaTypes.flatMap((schema) => [
|
|
420
|
-
formatNodeSchema(schema),
|
|
421
|
-
...formatInferredRelationships(schema, schemaTypes)
|
|
422
|
-
]);
|
|
423
|
-
};
|
|
424
|
-
var formatNodeSchema = (schema) => {
|
|
425
|
-
const label2 = formatNodeLabel(schema.typename ?? (0, import_debug.raise)(new Error("Schema must have $id")));
|
|
426
|
-
const properties2 = Object.entries(schema.properties).filter(([key, prop]) => key !== "id" && !isReference(prop));
|
|
427
|
-
return trim`
|
|
428
|
-
<node>
|
|
429
|
-
(:${label2})
|
|
430
|
-
|
|
431
|
-
<description>${schema.description}</description>
|
|
432
|
-
|
|
433
|
-
<properties>
|
|
434
|
-
${properties2.map(([key, value]) => {
|
|
435
|
-
return trim`
|
|
436
|
-
<${key}>
|
|
437
|
-
<type>${value.type}</type>
|
|
438
|
-
<description>${value.description}</description>
|
|
439
|
-
</${key}>
|
|
440
|
-
`;
|
|
441
|
-
})}
|
|
442
|
-
</properties>
|
|
443
|
-
</node>
|
|
444
|
-
`;
|
|
445
|
-
};
|
|
446
|
-
var formatInferredRelationships = (schema, allSchema) => {
|
|
447
|
-
const nodeLabel = formatNodeLabel(schema.typename ?? (0, import_debug.raise)(new Error("Schema must have $id")));
|
|
448
|
-
const relationships = Object.entries(schema.properties).filter(([key, prop]) => isReference(prop));
|
|
449
|
-
return relationships.map(([key, value]) => {
|
|
450
|
-
const relationshipLabel = formatInferredRelationshipLabel(schema.typename ?? (0, import_debug.raise)(new Error("Schema must have $id")), key);
|
|
451
|
-
const targetSchema = allSchema.find((schema2) => `dxn:type/${schema2.typename}` === value.reference?.schema.$ref);
|
|
452
|
-
const targetLabel = targetSchema ? formatNodeLabel(targetSchema?.typename ?? "") : "";
|
|
453
|
-
return trim`
|
|
454
|
-
<relationship>
|
|
455
|
-
(:${nodeLabel})-[:${relationshipLabel}]->(:${targetLabel})
|
|
456
|
-
|
|
457
|
-
<description>${value.description}</description>
|
|
458
|
-
</relationship>
|
|
459
|
-
`;
|
|
460
|
-
});
|
|
461
|
-
};
|
|
462
|
-
var formatNodeLabel = (typename) => {
|
|
463
|
-
return typename.replace(/^example\.com\/type\//g, "");
|
|
464
|
-
};
|
|
465
|
-
var formatInferredRelationshipLabel = (typename, property2) => {
|
|
466
|
-
const nodeLabel = formatNodeLabel(typename);
|
|
467
|
-
return `${nodeLabel}_${property2}`.toUpperCase();
|
|
468
|
-
};
|
|
469
|
-
var isReference = (schema) => schema.$id === import_echo_schema.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_schema.JSON_SCHEMA_ECHO_REF_ID;
|
|
470
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
471
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
472
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
473
|
-
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;
|
|
474
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
475
|
-
}
|
|
476
|
-
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/echo-data-source.ts";
|
|
477
|
-
var EchoDataSource = class {
|
|
478
|
-
constructor(_db) {
|
|
479
|
-
this._db = _db;
|
|
480
|
-
}
|
|
481
|
-
async getNodes({ label: label2 }) {
|
|
482
|
-
if (!label2) {
|
|
483
|
-
const { objects: objects2 } = await this._db.query().run();
|
|
484
|
-
return objects2.map(this._objectToNode);
|
|
485
|
-
}
|
|
486
|
-
const schema = (await this._getAllSchema()).find((s) => (0, import_echo_schema2.getSchemaTypename)(s)?.endsWith(label2));
|
|
487
|
-
if (!schema) {
|
|
488
|
-
return [];
|
|
489
|
-
}
|
|
490
|
-
const { objects } = await this._db.query(import_echo_db.Filter.schema(schema)).run();
|
|
491
|
-
return objects.map(this._objectToNode);
|
|
492
|
-
}
|
|
493
|
-
async getRelationships({ label: label2 }) {
|
|
494
|
-
const syntheticRefRelationships = (await this._getAllSchema()).filter((schema) => (0, import_echo_schema2.getSchemaTypename)(schema) !== import_echo_schema2.StoredSchema.typename).flatMap((schema) => {
|
|
495
|
-
const jsonSchema = (0, import_echo_schema2.toJsonSchema)(schema);
|
|
496
|
-
const relationships = Object.entries(jsonSchema.properties).filter(([key, prop]) => isReference(prop));
|
|
497
|
-
return relationships.map(([key, value]) => {
|
|
498
|
-
const relationshipLabel = formatInferredRelationshipLabel(jsonSchema.typename ?? (0, import_debug2.raise)(new Error("Schema must have typename")), key);
|
|
499
|
-
return {
|
|
500
|
-
label: relationshipLabel,
|
|
501
|
-
schema,
|
|
502
|
-
property: key
|
|
503
|
-
};
|
|
504
|
-
});
|
|
505
|
-
});
|
|
506
|
-
const relationship = syntheticRefRelationships.find((r) => r.label === label2);
|
|
507
|
-
if (!relationship) {
|
|
508
|
-
return [];
|
|
509
|
-
}
|
|
510
|
-
const { objects } = await this._db.query(import_echo_db.Filter.schema(relationship.schema)).run();
|
|
511
|
-
return (await Promise.all(objects.map((object) => this._projectRefRelationship(object, relationship.property)))).flat();
|
|
512
|
-
}
|
|
513
|
-
async _getAllSchema() {
|
|
514
|
-
return [
|
|
515
|
-
...await this._db.schemaRegistry.query().run(),
|
|
516
|
-
// TODO(dmaretskyi): Remove once we can serialize recursive schema.
|
|
517
|
-
...this._db.graph.schemaRegistry.schemas.filter((s) => (0, import_echo_schema2.getSchemaTypename)(s)?.startsWith("example.org"))
|
|
518
|
-
].filter((schema) => (0, import_echo_schema2.getSchemaTypename)(schema) !== import_echo_schema2.StoredSchema.typename);
|
|
519
|
-
}
|
|
520
|
-
_objectToNode(object) {
|
|
521
|
-
const { id, ...properties2 } = object;
|
|
522
|
-
return {
|
|
523
|
-
id,
|
|
524
|
-
kind: "node",
|
|
525
|
-
label: formatNodeLabel((0, import_echo_schema2.getSchemaTypename)((0, import_live_object.getSchema)(object))),
|
|
526
|
-
properties: properties2
|
|
527
|
-
};
|
|
528
|
-
}
|
|
529
|
-
async _projectRefRelationship(object, prop) {
|
|
530
|
-
if (!object[prop]) {
|
|
531
|
-
return [];
|
|
532
|
-
}
|
|
533
|
-
const value = Array.isArray(object[prop]) ? object[prop] : [
|
|
534
|
-
object[prop]
|
|
535
|
-
];
|
|
536
|
-
return Promise.all(value.map(async (ref) => {
|
|
537
|
-
const target = await ref.load();
|
|
538
|
-
return {
|
|
539
|
-
id: `${object.id}-${prop}-${target.id}`,
|
|
540
|
-
kind: "relationship",
|
|
541
|
-
label: formatInferredRelationshipLabel((0, import_echo_schema2.getSchemaTypename)((0, import_live_object.getSchema)(object)), prop),
|
|
542
|
-
properties: {},
|
|
543
|
-
source: this._objectToNode(object),
|
|
544
|
-
target: this._objectToNode(target)
|
|
545
|
-
};
|
|
546
|
-
}));
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
_ts_decorate([
|
|
550
|
-
import_log3.log.method(void 0, void 0, {
|
|
551
|
-
F: __dxlog_file3,
|
|
552
|
-
L: 17,
|
|
553
|
-
S: void 0,
|
|
554
|
-
C: (f, a) => f(...a)
|
|
555
|
-
})
|
|
556
|
-
], EchoDataSource.prototype, "getNodes", null);
|
|
557
|
-
_ts_decorate([
|
|
558
|
-
import_log3.log.method(void 0, void 0, {
|
|
559
|
-
F: __dxlog_file3,
|
|
560
|
-
L: 34,
|
|
561
|
-
S: void 0,
|
|
562
|
-
C: (f, a) => f(...a)
|
|
563
|
-
})
|
|
564
|
-
], EchoDataSource.prototype, "getRelationships", null);
|
|
565
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
566
|
-
0 && (module.exports = {
|
|
567
|
-
EchoDataSource,
|
|
568
|
-
executeQuery,
|
|
569
|
-
executeQueryPlan,
|
|
570
|
-
formatInferredRelationshipLabel,
|
|
571
|
-
formatJsonSchemaForLLM,
|
|
572
|
-
formatNodeLabel,
|
|
573
|
-
isReference,
|
|
574
|
-
parseCypherQuery,
|
|
575
|
-
trim
|
|
576
|
-
});
|
|
577
|
-
//# sourceMappingURL=chunk-QPHGQINW.cjs.map
|