trellis 2.0.8 → 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +279 -116
  2. package/dist/cli/index.js +655 -4
  3. package/dist/core/index.js +471 -2
  4. package/dist/embeddings/index.js +5 -1
  5. package/dist/{index-s603ev6w.js → index-5b01h414.js} +1 -1
  6. package/dist/index-5m0g9r0y.js +1100 -0
  7. package/dist/{index-zf6htvnm.js → index-7gvjxt27.js} +166 -2
  8. package/dist/index-hybgxe40.js +1174 -0
  9. package/dist/index.js +7 -2
  10. package/dist/transformers.node-bx3q9d7k.js +33130 -0
  11. package/package.json +9 -4
  12. package/src/cli/index.ts +939 -0
  13. package/src/core/agents/harness.ts +380 -0
  14. package/src/core/agents/index.ts +18 -0
  15. package/src/core/agents/types.ts +90 -0
  16. package/src/core/index.ts +85 -2
  17. package/src/core/kernel/trellis-kernel.ts +593 -0
  18. package/src/core/ontology/builtins.ts +248 -0
  19. package/src/core/ontology/index.ts +34 -0
  20. package/src/core/ontology/registry.ts +209 -0
  21. package/src/core/ontology/types.ts +124 -0
  22. package/src/core/ontology/validator.ts +382 -0
  23. package/src/core/persist/backend.ts +10 -0
  24. package/src/core/persist/sqlite-backend.ts +298 -0
  25. package/src/core/plugins/index.ts +17 -0
  26. package/src/core/plugins/registry.ts +322 -0
  27. package/src/core/plugins/types.ts +126 -0
  28. package/src/core/query/datalog.ts +188 -0
  29. package/src/core/query/engine.ts +370 -0
  30. package/src/core/query/index.ts +34 -0
  31. package/src/core/query/parser.ts +481 -0
  32. package/src/core/query/types.ts +200 -0
  33. package/src/embeddings/auto-embed.ts +248 -0
  34. package/src/embeddings/index.ts +7 -0
  35. package/src/embeddings/model.ts +21 -4
  36. package/src/embeddings/types.ts +8 -1
  37. package/src/index.ts +9 -0
  38. package/src/sync/http-transport.ts +144 -0
  39. package/src/sync/index.ts +11 -0
  40. package/src/sync/multi-repo.ts +200 -0
  41. package/src/sync/ws-transport.ts +145 -0
  42. package/dist/index-5bhe57y9.js +0 -326
@@ -1,16 +1,485 @@
1
1
  // @bun
2
+ import {
3
+ DatalogRuntime,
4
+ OntologyRegistry,
5
+ agentOntology,
6
+ builtinOntologies,
7
+ createValidationMiddleware,
8
+ parseQuery,
9
+ parseRule,
10
+ parseSimple,
11
+ projectOntology,
12
+ teamOntology,
13
+ validateEntity,
14
+ validateStore
15
+ } from "../index-5m0g9r0y.js";
2
16
  import {
3
17
  EAVStore,
18
+ QueryEngine,
19
+ SqliteKernelBackend,
20
+ TrellisKernel,
4
21
  flatten,
5
22
  init_eav_store,
6
23
  jsonEntityFacts
7
- } from "../index-5bhe57y9.js";
24
+ } from "../index-hybgxe40.js";
8
25
  import"../index-a76rekgs.js";
9
26
 
10
27
  // src/core/index.ts
11
28
  init_eav_store();
29
+
30
+ // src/core/agents/harness.ts
31
+ class AgentHarness {
32
+ kernel;
33
+ toolHandlers = new Map;
34
+ config;
35
+ runCounter = 0;
36
+ constructor(kernel, config) {
37
+ this.kernel = kernel;
38
+ this.config = {
39
+ recordDecisions: true,
40
+ maxDecisionsPerRun: 100,
41
+ ...config
42
+ };
43
+ }
44
+ async createAgent(def) {
45
+ const id = def.id ?? `agent:${def.name.toLowerCase().replace(/\s+/g, "-")}`;
46
+ await this.kernel.createEntity(id, "Agent", {
47
+ name: def.name,
48
+ ...def.description ? { description: def.description } : {},
49
+ ...def.model ? { model: def.model } : {},
50
+ ...def.provider ? { provider: def.provider } : {},
51
+ ...def.systemPrompt ? { systemPrompt: def.systemPrompt } : {},
52
+ status: def.status ?? "active"
53
+ });
54
+ if (def.capabilities) {
55
+ for (const cap of def.capabilities) {
56
+ await this.kernel.addLink(id, "hasCapability", cap);
57
+ }
58
+ }
59
+ if (def.tools) {
60
+ for (const tool of def.tools) {
61
+ await this.kernel.addLink(id, "hasTool", tool);
62
+ }
63
+ }
64
+ return this.getAgent(id);
65
+ }
66
+ getAgent(id) {
67
+ const entity = this.kernel.getEntity(id);
68
+ if (!entity || entity.type !== "Agent")
69
+ return null;
70
+ const store = this.kernel.getStore();
71
+ const capLinks = store.getLinksByEntityAndAttribute(id, "hasCapability");
72
+ const toolLinks = store.getLinksByEntityAndAttribute(id, "hasTool");
73
+ return {
74
+ id: entity.id,
75
+ name: String(entity.facts.find((f) => f.a === "name")?.v ?? ""),
76
+ description: entity.facts.find((f) => f.a === "description")?.v,
77
+ model: entity.facts.find((f) => f.a === "model")?.v,
78
+ provider: entity.facts.find((f) => f.a === "provider")?.v,
79
+ systemPrompt: entity.facts.find((f) => f.a === "systemPrompt")?.v,
80
+ status: entity.facts.find((f) => f.a === "status")?.v ?? "active",
81
+ capabilities: capLinks.map((l) => l.e2),
82
+ tools: toolLinks.map((l) => l.e2)
83
+ };
84
+ }
85
+ listAgents(status) {
86
+ const entities = this.kernel.listEntities("Agent", status ? { status } : undefined);
87
+ return entities.map((e) => this.getAgent(e.id)).filter((a) => a !== null);
88
+ }
89
+ async registerTool(def, handler) {
90
+ const id = def.id ?? `tool:${def.name.toLowerCase().replace(/\s+/g, "-")}`;
91
+ if (!this.kernel.getEntity(id)) {
92
+ await this.kernel.createEntity(id, "Tool", {
93
+ name: def.name,
94
+ ...def.description ? { description: def.description } : {},
95
+ ...def.schema ? { schema: def.schema } : {},
96
+ ...def.endpoint ? { endpoint: def.endpoint } : {}
97
+ });
98
+ }
99
+ this.toolHandlers.set(id, handler);
100
+ return id;
101
+ }
102
+ getToolHandler(toolId) {
103
+ return this.toolHandlers.get(toolId);
104
+ }
105
+ listTools() {
106
+ return this.kernel.listEntities("Tool").map((e) => ({
107
+ id: e.id,
108
+ name: String(e.facts.find((f) => f.a === "name")?.v ?? ""),
109
+ description: e.facts.find((f) => f.a === "description")?.v,
110
+ schema: e.facts.find((f) => f.a === "schema")?.v,
111
+ endpoint: e.facts.find((f) => f.a === "endpoint")?.v
112
+ }));
113
+ }
114
+ async startRun(agentId, input) {
115
+ const agent = this.getAgent(agentId);
116
+ if (!agent)
117
+ throw new Error(`Agent "${agentId}" not found.`);
118
+ const runId = `run:${agentId.replace("agent:", "")}:${Date.now()}:${++this.runCounter}`;
119
+ await this.kernel.createEntity(runId, "AgentRun", {
120
+ startedAt: new Date().toISOString(),
121
+ status: "running",
122
+ ...input ? { input } : {}
123
+ });
124
+ await this.kernel.addLink(runId, "executedBy", agentId);
125
+ return runId;
126
+ }
127
+ async completeRun(runId, output, tokenCount) {
128
+ const updates = {
129
+ status: "completed",
130
+ completedAt: new Date().toISOString()
131
+ };
132
+ if (output)
133
+ updates.output = output;
134
+ if (tokenCount !== undefined)
135
+ updates.tokenCount = tokenCount;
136
+ await this.kernel.updateEntity(runId, updates);
137
+ }
138
+ async failRun(runId, error) {
139
+ await this.kernel.updateEntity(runId, {
140
+ status: "failed",
141
+ completedAt: new Date().toISOString(),
142
+ output: `Error: ${error}`
143
+ });
144
+ }
145
+ getRun(runId) {
146
+ const entity = this.kernel.getEntity(runId);
147
+ if (!entity || entity.type !== "AgentRun")
148
+ return null;
149
+ const store = this.kernel.getStore();
150
+ const agentLink = store.getLinksByEntityAndAttribute(runId, "executedBy");
151
+ const agentId = agentLink[0]?.e2 ?? "";
152
+ const decisionLinks = store.getLinksByAttribute("belongsToRun");
153
+ const decisionIds = decisionLinks.filter((l) => l.e2 === runId).map((l) => l.e1);
154
+ const decisions = decisionIds.map((did) => this._buildDecisionTrace(did)).filter(Boolean);
155
+ const get = (a) => entity.facts.find((f) => f.a === a)?.v;
156
+ return {
157
+ id: runId,
158
+ agentId,
159
+ startedAt: String(get("startedAt") ?? ""),
160
+ completedAt: get("completedAt"),
161
+ status: get("status") ?? "running",
162
+ input: get("input"),
163
+ output: get("output"),
164
+ tokenCount: get("tokenCount"),
165
+ decisions
166
+ };
167
+ }
168
+ listRuns(agentId) {
169
+ const runs = this.kernel.listEntities("AgentRun");
170
+ return runs.map((e) => this.getRun(e.id)).filter((r) => r !== null).filter((r) => !agentId || r.agentId === agentId).sort((a, b) => new Date(b.startedAt).getTime() - new Date(a.startedAt).getTime());
171
+ }
172
+ async recordDecision(runId, toolName, input, output, opts) {
173
+ const run = this.getRun(runId);
174
+ if (!run)
175
+ throw new Error(`Run "${runId}" not found.`);
176
+ const decId = `decision:${runId.replace("run:", "")}:${Date.now()}`;
177
+ await this.kernel.createEntity(decId, "DecisionTrace", {
178
+ toolName,
179
+ timestamp: new Date().toISOString(),
180
+ ...input ? { input: JSON.stringify(input) } : {},
181
+ ...output ? { output } : {},
182
+ ...opts?.rationale ? { rationale: opts.rationale } : {},
183
+ ...opts?.alternatives ? { alternatives: JSON.stringify(opts.alternatives) } : {}
184
+ });
185
+ await this.kernel.addLink(decId, "belongsToRun", runId);
186
+ await this.kernel.addLink(decId, "madeBy", run.agentId);
187
+ if (opts?.relatedEntities) {
188
+ for (const eid of opts.relatedEntities) {
189
+ await this.kernel.addLink(decId, "relatedTo", eid);
190
+ }
191
+ }
192
+ return decId;
193
+ }
194
+ async invokeTool(runId, toolId, input, opts) {
195
+ const handler = this.toolHandlers.get(toolId);
196
+ if (!handler)
197
+ throw new Error(`No handler registered for tool "${toolId}".`);
198
+ const result = await handler(input);
199
+ if (this.config.recordDecisions) {
200
+ const toolEntity = this.kernel.getEntity(toolId);
201
+ const toolName = toolEntity ? String(toolEntity.facts.find((f) => f.a === "name")?.v ?? toolId) : toolId;
202
+ await this.recordDecision(runId, toolName, input, result.success ? String(result.output ?? "") : `Error: ${result.error}`, opts);
203
+ }
204
+ return result;
205
+ }
206
+ getDecisionChain(entityId) {
207
+ const store = this.kernel.getStore();
208
+ const links = store.getLinksByAttribute("relatedTo");
209
+ const decisionIds = links.filter((l) => l.e2 === entityId).map((l) => l.e1);
210
+ return decisionIds.map((did) => this._buildDecisionTrace(did)).filter(Boolean);
211
+ }
212
+ _buildDecisionTrace(decId) {
213
+ const entity = this.kernel.getEntity(decId);
214
+ if (!entity)
215
+ return null;
216
+ const get = (a) => entity.facts.find((f) => f.a === a)?.v;
217
+ const store = this.kernel.getStore();
218
+ const runLink = store.getLinksByEntityAndAttribute(decId, "belongsToRun");
219
+ const agentLink = store.getLinksByEntityAndAttribute(decId, "madeBy");
220
+ const relatedLinks = store.getLinksByEntityAndAttribute(decId, "relatedTo");
221
+ let inputParsed;
222
+ const inputRaw = get("input");
223
+ if (inputRaw) {
224
+ try {
225
+ inputParsed = JSON.parse(inputRaw);
226
+ } catch {
227
+ inputParsed = { raw: inputRaw };
228
+ }
229
+ }
230
+ let alternatives;
231
+ const altRaw = get("alternatives");
232
+ if (altRaw) {
233
+ try {
234
+ alternatives = JSON.parse(altRaw);
235
+ } catch {
236
+ alternatives = [altRaw];
237
+ }
238
+ }
239
+ return {
240
+ id: decId,
241
+ runId: runLink[0]?.e2 ?? "",
242
+ agentId: agentLink[0]?.e2 ?? "",
243
+ toolName: String(get("toolName") ?? ""),
244
+ input: inputParsed,
245
+ output: get("output"),
246
+ rationale: get("rationale"),
247
+ alternatives,
248
+ timestamp: String(get("timestamp") ?? ""),
249
+ relatedEntities: relatedLinks.map((l) => l.e2)
250
+ };
251
+ }
252
+ }
253
+ // src/core/plugins/registry.ts
254
+ class EventBus {
255
+ handlers = new Map;
256
+ on(event, handler) {
257
+ const set = this.handlers.get(event) ?? new Set;
258
+ set.add(handler);
259
+ this.handlers.set(event, set);
260
+ }
261
+ off(event, handler) {
262
+ const set = this.handlers.get(event);
263
+ if (set) {
264
+ set.delete(handler);
265
+ if (set.size === 0)
266
+ this.handlers.delete(event);
267
+ }
268
+ }
269
+ async emit(event, data) {
270
+ const exact = this.handlers.get(event);
271
+ if (exact) {
272
+ for (const h of exact)
273
+ await h(data);
274
+ }
275
+ for (const [pattern, handlers] of this.handlers) {
276
+ if (pattern === event)
277
+ continue;
278
+ if (pattern.endsWith("*") && event.startsWith(pattern.slice(0, -1))) {
279
+ for (const h of handlers)
280
+ await h(data);
281
+ }
282
+ }
283
+ }
284
+ listEvents() {
285
+ return [...this.handlers.keys()];
286
+ }
287
+ clear() {
288
+ this.handlers.clear();
289
+ }
290
+ }
291
+
292
+ class PluginRegistry {
293
+ plugins = new Map;
294
+ eventBus = new EventBus;
295
+ workspaceConfig = {};
296
+ logs = [];
297
+ register(def) {
298
+ if (this.plugins.has(def.id)) {
299
+ throw new Error(`Plugin "${def.id}" is already registered.`);
300
+ }
301
+ this.plugins.set(def.id, { def, loaded: false });
302
+ }
303
+ async unregister(id) {
304
+ const entry = this.plugins.get(id);
305
+ if (!entry)
306
+ return;
307
+ if (entry.loaded)
308
+ await this.unload(id);
309
+ this.plugins.delete(id);
310
+ }
311
+ async load(id, kernel, ontologyRegistry, queryEngine) {
312
+ const entry = this.plugins.get(id);
313
+ if (!entry)
314
+ throw new Error(`Plugin "${id}" is not registered.`);
315
+ if (entry.loaded)
316
+ return;
317
+ if (entry.def.dependencies) {
318
+ for (const dep of entry.def.dependencies) {
319
+ const depEntry = this.plugins.get(dep);
320
+ if (!depEntry) {
321
+ throw new Error(`Plugin "${id}" depends on "${dep}" which is not registered.`);
322
+ }
323
+ if (!depEntry.loaded) {
324
+ await this.load(dep, kernel, ontologyRegistry, queryEngine);
325
+ }
326
+ }
327
+ }
328
+ if (entry.def.middleware && kernel) {
329
+ for (const mw of entry.def.middleware) {
330
+ kernel.addMiddleware(mw);
331
+ }
332
+ }
333
+ if (entry.def.ontologies && ontologyRegistry) {
334
+ for (const schema of entry.def.ontologies) {
335
+ try {
336
+ ontologyRegistry.register(schema);
337
+ } catch {}
338
+ }
339
+ }
340
+ if (entry.def.rules && queryEngine) {
341
+ for (const rule of entry.def.rules) {
342
+ queryEngine.addRule(rule);
343
+ }
344
+ }
345
+ if (entry.def.eventHandlers) {
346
+ for (const eh of entry.def.eventHandlers) {
347
+ this.eventBus.on(eh.event, eh.handler);
348
+ }
349
+ }
350
+ const ctx = this._buildContext(id);
351
+ if (entry.def.onLoad) {
352
+ await entry.def.onLoad(ctx);
353
+ }
354
+ entry.loaded = true;
355
+ await this.eventBus.emit("plugin:loaded", { pluginId: id });
356
+ }
357
+ async unload(id) {
358
+ const entry = this.plugins.get(id);
359
+ if (!entry || !entry.loaded)
360
+ return;
361
+ const ctx = this._buildContext(id);
362
+ if (entry.def.onUnload) {
363
+ await entry.def.onUnload(ctx);
364
+ }
365
+ if (entry.def.eventHandlers) {
366
+ for (const eh of entry.def.eventHandlers) {
367
+ this.eventBus.off(eh.event, eh.handler);
368
+ }
369
+ }
370
+ entry.loaded = false;
371
+ await this.eventBus.emit("plugin:unloaded", { pluginId: id });
372
+ }
373
+ async loadAll(kernel, ontologyRegistry, queryEngine) {
374
+ const order = this._resolveDependencyOrder();
375
+ for (const id of order) {
376
+ await this.load(id, kernel, ontologyRegistry, queryEngine);
377
+ }
378
+ }
379
+ async unloadAll() {
380
+ const order = this._resolveDependencyOrder().reverse();
381
+ for (const id of order) {
382
+ await this.unload(id);
383
+ }
384
+ }
385
+ get(id) {
386
+ return this.plugins.get(id)?.def;
387
+ }
388
+ isLoaded(id) {
389
+ return this.plugins.get(id)?.loaded ?? false;
390
+ }
391
+ list() {
392
+ return [...this.plugins.values()];
393
+ }
394
+ listLoaded() {
395
+ return [...this.plugins.values()].filter((e) => e.loaded).map((e) => e.def);
396
+ }
397
+ getEventBus() {
398
+ return this.eventBus;
399
+ }
400
+ async emit(event, data) {
401
+ await this.eventBus.emit(event, data);
402
+ }
403
+ on(event, handler) {
404
+ this.eventBus.on(event, handler);
405
+ }
406
+ getWorkspaceConfig() {
407
+ return this.workspaceConfig;
408
+ }
409
+ setWorkspaceConfig(config) {
410
+ this.workspaceConfig = config;
411
+ }
412
+ getConfigValue(key) {
413
+ return this.workspaceConfig.settings?.[key];
414
+ }
415
+ setConfigValue(key, value) {
416
+ if (!this.workspaceConfig.settings)
417
+ this.workspaceConfig.settings = {};
418
+ this.workspaceConfig.settings[key] = value;
419
+ }
420
+ getLogs(pluginId) {
421
+ if (pluginId)
422
+ return this.logs.filter((l) => l.pluginId === pluginId);
423
+ return [...this.logs];
424
+ }
425
+ _buildContext(pluginId) {
426
+ return {
427
+ pluginId,
428
+ on: (event, handler) => this.eventBus.on(event, handler),
429
+ emit: (event, data) => {
430
+ this.eventBus.emit(event, data);
431
+ },
432
+ getConfig: (key) => this.getConfigValue(key),
433
+ log: (message) => {
434
+ this.logs.push({ pluginId, message, timestamp: new Date().toISOString() });
435
+ }
436
+ };
437
+ }
438
+ _resolveDependencyOrder() {
439
+ const visited = new Set;
440
+ const order = [];
441
+ const visit = (id, stack) => {
442
+ if (visited.has(id))
443
+ return;
444
+ if (stack.has(id))
445
+ throw new Error(`Circular dependency detected: ${[...stack, id].join(" \u2192 ")}`);
446
+ stack.add(id);
447
+ const entry = this.plugins.get(id);
448
+ if (entry?.def.dependencies) {
449
+ for (const dep of entry.def.dependencies) {
450
+ visit(dep, stack);
451
+ }
452
+ }
453
+ stack.delete(id);
454
+ visited.add(id);
455
+ order.push(id);
456
+ };
457
+ for (const id of this.plugins.keys()) {
458
+ visit(id, new Set);
459
+ }
460
+ return order;
461
+ }
462
+ }
12
463
  export {
464
+ validateStore,
465
+ validateEntity,
466
+ teamOntology,
467
+ projectOntology,
468
+ parseSimple,
469
+ parseRule,
470
+ parseQuery,
13
471
  jsonEntityFacts,
14
472
  flatten,
15
- EAVStore
473
+ createValidationMiddleware,
474
+ builtinOntologies,
475
+ agentOntology,
476
+ TrellisKernel,
477
+ SqliteKernelBackend,
478
+ QueryEngine,
479
+ PluginRegistry,
480
+ OntologyRegistry,
481
+ EventBus,
482
+ EAVStore,
483
+ DatalogRuntime,
484
+ AgentHarness
16
485
  };
@@ -3,6 +3,7 @@ import {
3
3
  DEFAULT_MODEL_CONFIG,
4
4
  EmbeddingManager,
5
5
  VectorStore,
6
+ buildRAGContext,
6
7
  chunkCodeEntities,
7
8
  chunkDecision,
8
9
  chunkDocComments,
@@ -12,13 +13,14 @@ import {
12
13
  chunkMilestone,
13
14
  chunkSummary,
14
15
  cosineSimilarity,
16
+ createAutoEmbedMiddleware,
15
17
  embed,
16
18
  embedBatch,
17
19
  init_embeddings,
18
20
  loadModel,
19
21
  resetModel,
20
22
  slidingWindow
21
- } from "../index-zf6htvnm.js";
23
+ } from "../index-7gvjxt27.js";
22
24
  import"../index-a76rekgs.js";
23
25
  init_embeddings();
24
26
 
@@ -28,6 +30,7 @@ export {
28
30
  loadModel,
29
31
  embedBatch,
30
32
  embed,
33
+ createAutoEmbedMiddleware,
31
34
  cosineSimilarity,
32
35
  chunkSummary,
33
36
  chunkMilestone,
@@ -37,6 +40,7 @@ export {
37
40
  chunkDocComments,
38
41
  chunkDecision,
39
42
  chunkCodeEntities,
43
+ buildRAGContext,
40
44
  VectorStore,
41
45
  EmbeddingManager,
42
46
  DEFAULT_MODEL_CONFIG
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  EAVStore,
4
4
  init_eav_store
5
- } from "./index-5bhe57y9.js";
5
+ } from "./index-hybgxe40.js";
6
6
  import {
7
7
  BlobStore,
8
8
  addCriterion,