@dxos/assistant 0.8.2-main.fbd8ed0 → 0.8.2-staging.7ac8446

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 (175) hide show
  1. package/dist/lib/browser/{chunk-B4HAWUFW.mjs → chunk-PND5VR6D.mjs} +286 -757
  2. package/dist/lib/browser/chunk-PND5VR6D.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +324 -7
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +102 -139
  7. package/dist/lib/browser/testing/index.mjs.map +4 -4
  8. package/dist/lib/node/{chunk-NOB7CIIC.cjs → chunk-IBOBRBDF.cjs} +303 -768
  9. package/dist/lib/node/chunk-IBOBRBDF.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +353 -36
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +93 -128
  14. package/dist/lib/node/testing/index.cjs.map +4 -4
  15. package/dist/lib/node-esm/{chunk-SAWYU3V5.mjs → chunk-DCQJOLSR.mjs} +286 -757
  16. package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +324 -7
  18. package/dist/lib/node-esm/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/testing/index.mjs +102 -139
  21. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  22. package/dist/types/src/ai-service/edge-client.d.ts +5 -4
  23. package/dist/types/src/ai-service/edge-client.d.ts.map +1 -1
  24. package/dist/types/src/ai-service/message-collector.d.ts +6 -0
  25. package/dist/types/src/ai-service/message-collector.d.ts.map +1 -1
  26. package/dist/types/src/ai-service/ollama-client.d.ts +2 -6
  27. package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -1
  28. package/dist/types/src/ai-service/parser.d.ts +3 -0
  29. package/dist/types/src/ai-service/parser.d.ts.map +1 -1
  30. package/dist/types/src/ai-service/service.d.ts +2 -18
  31. package/dist/types/src/ai-service/service.d.ts.map +1 -1
  32. package/dist/types/src/ai-service/stream.d.ts.map +1 -1
  33. package/dist/types/src/ai-service/testing/mock.d.ts +2 -6
  34. package/dist/types/src/ai-service/testing/mock.d.ts.map +1 -1
  35. package/dist/types/src/ai-service/testing/test-stream.d.ts.map +1 -1
  36. package/dist/types/src/ai-service/types.d.ts +101 -144
  37. package/dist/types/src/ai-service/types.d.ts.map +1 -1
  38. package/dist/types/src/ai-service/util/sse.d.ts.map +1 -1
  39. package/dist/types/src/{experimental/conversation → conversation}/conversation.d.ts +21 -2
  40. package/dist/types/src/conversation/conversation.d.ts.map +1 -0
  41. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  42. package/dist/types/src/conversation/index.d.ts.map +1 -0
  43. package/dist/types/src/{experimental/conversation → conversation}/types.d.ts +12 -11
  44. package/dist/types/src/conversation/types.d.ts.map +1 -0
  45. package/dist/types/src/cypher/ast.d.ts.map +1 -0
  46. package/dist/types/src/cypher/combinators.d.ts.map +1 -0
  47. package/dist/types/src/cypher/combinators.test.d.ts.map +1 -0
  48. package/dist/types/src/cypher/echo-data-source.d.ts.map +1 -0
  49. package/dist/types/src/cypher/execution-plan.d.ts.map +1 -0
  50. package/dist/types/src/cypher/execution-plan.test.d.ts.map +1 -0
  51. package/dist/types/src/cypher/index.d.ts.map +1 -0
  52. package/dist/types/src/cypher/parjs.d.ts.map +1 -0
  53. package/dist/types/src/cypher/parser.d.ts.map +1 -0
  54. package/dist/types/src/cypher/query-executor.d.ts.map +1 -0
  55. package/dist/types/src/cypher/query-executor.test.d.ts.map +1 -0
  56. package/dist/types/src/cypher/schema.d.ts.map +1 -0
  57. package/dist/types/src/cypher/schema.test.d.ts.map +1 -0
  58. package/dist/types/src/index.d.ts +2 -1
  59. package/dist/types/src/index.d.ts.map +1 -1
  60. package/dist/types/src/session/index.d.ts +0 -2
  61. package/dist/types/src/session/index.d.ts.map +1 -1
  62. package/dist/types/src/session/session.d.ts +13 -33
  63. package/dist/types/src/session/session.d.ts.map +1 -1
  64. package/dist/types/src/testing/index.d.ts +0 -1
  65. package/dist/types/src/testing/index.d.ts.map +1 -1
  66. package/dist/types/src/testing/logger.d.ts +1 -1
  67. package/dist/types/src/testing/logger.d.ts.map +1 -1
  68. package/dist/types/src/testing/ollama.d.ts.map +1 -1
  69. package/dist/types/src/testing/query-promts.d.ts +4 -4
  70. package/dist/types/src/testing/query-promts.d.ts.map +1 -1
  71. package/dist/types/src/testing/test-data.d.ts +5 -5
  72. package/dist/types/src/testing/test-data.d.ts.map +1 -1
  73. package/dist/types/src/testing/test-schema.d.ts +23 -25
  74. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  75. package/dist/types/src/util/trim.d.ts.map +1 -1
  76. package/dist/types/tsconfig.tsbuildinfo +1 -1
  77. package/package.json +14 -20
  78. package/src/ai-service/client.test.ts +73 -55
  79. package/src/ai-service/edge-client.ts +20 -13
  80. package/src/ai-service/ollama-client.ts +10 -17
  81. package/src/ai-service/parser.ts +2 -13
  82. package/src/ai-service/service.ts +3 -19
  83. package/src/ai-service/testing/mock.ts +1 -9
  84. package/src/ai-service/types.ts +15 -24
  85. package/src/bin/chat.ts +4 -4
  86. package/src/{experimental/conversation → conversation}/conversation.test.ts +5 -5
  87. package/src/{experimental/conversation → conversation}/conversation.ts +119 -4
  88. package/src/{experimental/conversation → conversation}/types.ts +14 -11
  89. package/src/{experimental/cypher → cypher}/combinators.test.ts +4 -4
  90. package/src/{experimental/cypher → cypher}/echo-data-source.ts +11 -11
  91. package/src/{experimental/cypher → cypher}/execution-plan.test.ts +1 -1
  92. package/src/{experimental/cypher → cypher}/query-executor.test.ts +2 -2
  93. package/src/{experimental/cypher → cypher}/schema.test.ts +3 -3
  94. package/src/{experimental/cypher → cypher}/schema.ts +1 -1
  95. package/src/index.ts +2 -3
  96. package/src/session/index.ts +0 -2
  97. package/src/session/session.test.ts +63 -35
  98. package/src/session/session.ts +151 -297
  99. package/src/testing/artifacts.test.ts +22 -22
  100. package/src/testing/index.ts +0 -1
  101. package/src/testing/logger.ts +1 -1
  102. package/src/testing/query-promts.ts +6 -6
  103. package/src/testing/query.test.ts +21 -18
  104. package/src/testing/test-data.ts +72 -79
  105. package/src/testing/test-schema.ts +29 -28
  106. package/dist/lib/browser/chunk-B4HAWUFW.mjs.map +0 -7
  107. package/dist/lib/node/chunk-NOB7CIIC.cjs.map +0 -7
  108. package/dist/lib/node-esm/chunk-SAWYU3V5.mjs.map +0 -7
  109. package/dist/types/src/experimental/conversation/conversation.d.ts.map +0 -1
  110. package/dist/types/src/experimental/conversation/conversation.test.d.ts.map +0 -1
  111. package/dist/types/src/experimental/conversation/index.d.ts.map +0 -1
  112. package/dist/types/src/experimental/conversation/types.d.ts.map +0 -1
  113. package/dist/types/src/experimental/cypher/ast.d.ts.map +0 -1
  114. package/dist/types/src/experimental/cypher/combinators.d.ts.map +0 -1
  115. package/dist/types/src/experimental/cypher/combinators.test.d.ts.map +0 -1
  116. package/dist/types/src/experimental/cypher/echo-data-source.d.ts.map +0 -1
  117. package/dist/types/src/experimental/cypher/execution-plan.d.ts.map +0 -1
  118. package/dist/types/src/experimental/cypher/execution-plan.test.d.ts.map +0 -1
  119. package/dist/types/src/experimental/cypher/index.d.ts.map +0 -1
  120. package/dist/types/src/experimental/cypher/parjs.d.ts.map +0 -1
  121. package/dist/types/src/experimental/cypher/parser.d.ts.map +0 -1
  122. package/dist/types/src/experimental/cypher/query-executor.d.ts.map +0 -1
  123. package/dist/types/src/experimental/cypher/query-executor.test.d.ts.map +0 -1
  124. package/dist/types/src/experimental/cypher/schema.d.ts.map +0 -1
  125. package/dist/types/src/experimental/cypher/schema.test.d.ts.map +0 -1
  126. package/dist/types/src/experimental/index.d.ts +0 -3
  127. package/dist/types/src/experimental/index.d.ts.map +0 -1
  128. package/dist/types/src/experimental/ner/ner.test.d.ts +0 -2
  129. package/dist/types/src/experimental/ner/ner.test.d.ts.map +0 -1
  130. package/dist/types/src/experimental/research/exa.d.ts +0 -6
  131. package/dist/types/src/experimental/research/exa.d.ts.map +0 -1
  132. package/dist/types/src/experimental/research/research.test.d.ts +0 -2
  133. package/dist/types/src/experimental/research/research.test.d.ts.map +0 -1
  134. package/dist/types/src/session/tools.d.ts +0 -18
  135. package/dist/types/src/session/tools.d.ts.map +0 -1
  136. package/dist/types/src/session/version-pin.d.ts +0 -22
  137. package/dist/types/src/session/version-pin.d.ts.map +0 -1
  138. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  139. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  140. package/dist/types/src/testing/printer.d.ts +0 -6
  141. package/dist/types/src/testing/printer.d.ts.map +0 -1
  142. package/src/experimental/index.ts +0 -6
  143. package/src/experimental/ner/ner.test.ts +0 -163
  144. package/src/experimental/research/exa.ts +0 -38
  145. package/src/experimental/research/instructions.tpl +0 -64
  146. package/src/experimental/research/research.test.ts +0 -263
  147. package/src/session/tools.ts +0 -117
  148. package/src/session/version-pin.test.ts +0 -26
  149. package/src/session/version-pin.ts +0 -49
  150. package/src/testing/printer.ts +0 -41
  151. package/src/typedefs.d.ts +0 -8
  152. /package/dist/types/src/{experimental/conversation → conversation}/conversation.test.d.ts +0 -0
  153. /package/dist/types/src/{experimental/conversation → conversation}/index.d.ts +0 -0
  154. /package/dist/types/src/{experimental/cypher → cypher}/ast.d.ts +0 -0
  155. /package/dist/types/src/{experimental/cypher → cypher}/combinators.d.ts +0 -0
  156. /package/dist/types/src/{experimental/cypher → cypher}/combinators.test.d.ts +0 -0
  157. /package/dist/types/src/{experimental/cypher → cypher}/echo-data-source.d.ts +0 -0
  158. /package/dist/types/src/{experimental/cypher → cypher}/execution-plan.d.ts +0 -0
  159. /package/dist/types/src/{experimental/cypher → cypher}/execution-plan.test.d.ts +0 -0
  160. /package/dist/types/src/{experimental/cypher → cypher}/index.d.ts +0 -0
  161. /package/dist/types/src/{experimental/cypher → cypher}/parjs.d.ts +0 -0
  162. /package/dist/types/src/{experimental/cypher → cypher}/parser.d.ts +0 -0
  163. /package/dist/types/src/{experimental/cypher → cypher}/query-executor.d.ts +0 -0
  164. /package/dist/types/src/{experimental/cypher → cypher}/query-executor.test.d.ts +0 -0
  165. /package/dist/types/src/{experimental/cypher → cypher}/schema.d.ts +0 -0
  166. /package/dist/types/src/{experimental/cypher → cypher}/schema.test.d.ts +0 -0
  167. /package/src/{experimental/conversation → conversation}/index.ts +0 -0
  168. /package/src/{experimental/cypher → cypher}/ast.ts +0 -0
  169. /package/src/{experimental/cypher → cypher}/combinators.ts +0 -0
  170. /package/src/{experimental/cypher → cypher}/execution-plan.ts +0 -0
  171. /package/src/{experimental/cypher → cypher}/index.ts +0 -0
  172. /package/src/{experimental/cypher → cypher}/parjs-shims.d.ts +0 -0
  173. /package/src/{experimental/cypher → cypher}/parjs.ts +0 -0
  174. /package/src/{experimental/cypher → cypher}/parser.ts +0 -0
  175. /package/src/{experimental/cypher → cypher}/query-executor.ts +0 -0
@@ -359,6 +359,8 @@ var GenerationStreamImpl = class {
359
359
  };
360
360
 
361
361
  // packages/experimental/assistant/src/ai-service/edge-client.ts
362
+ import { Schema as S } from "effect";
363
+ import { Message } from "@dxos/artifact";
362
364
  import { assertArgument, invariant } from "@dxos/invariant";
363
365
  import { log as log2 } from "@dxos/log";
364
366
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/edge-client.ts";
@@ -366,7 +368,7 @@ var AIServiceEdgeClient = class {
366
368
  constructor({ endpoint }) {
367
369
  invariant(endpoint, "endpoint is required", {
368
370
  F: __dxlog_file2,
369
- L: 20,
371
+ L: 23,
370
372
  S: this,
371
373
  A: [
372
374
  "endpoint",
@@ -376,21 +378,34 @@ var AIServiceEdgeClient = class {
376
378
  this._endpoint = endpoint;
377
379
  }
378
380
  /**
379
- * Generate non-streaming response.
381
+ * @deprecated
380
382
  */
381
- async exec(request) {
382
- throw new Error("Not implemented");
383
+ // TODO(burdon): Remove.
384
+ async appendMessages(messages) {
385
+ const url = `${this._endpoint}/message`;
386
+ const res = await fetch(url, {
387
+ method: "POST",
388
+ headers: {
389
+ "Content-Type": "application/json"
390
+ },
391
+ body: JSON.stringify({
392
+ messages: await S.encodePromise(S.Array(Message))(messages)
393
+ })
394
+ });
395
+ if (!res.ok) {
396
+ throw new Error(`${await res.text()} [${url}]`);
397
+ }
383
398
  }
384
399
  /**
385
400
  * Process request and open message stream.
386
401
  */
387
- async execStream(request) {
402
+ async exec(request) {
388
403
  assertArgument(typeof request.model === "string", "model is required");
389
404
  log2("requesting", {
390
405
  endpoint: this._endpoint
391
406
  }, {
392
407
  F: __dxlog_file2,
393
- L: 36,
408
+ L: 51,
394
409
  S: this,
395
410
  C: (f, a) => f(...a)
396
411
  });
@@ -407,12 +422,9 @@ var AIServiceEdgeClient = class {
407
422
  ...request
408
423
  })
409
424
  });
410
- if (!response.ok) {
411
- throw await parseErrorResponse(response);
412
- }
413
425
  invariant(response.body instanceof ReadableStream, void 0, {
414
426
  F: __dxlog_file2,
415
- L: 53,
427
+ L: 64,
416
428
  S: this,
417
429
  A: [
418
430
  "response.body instanceof ReadableStream",
@@ -422,9 +434,6 @@ var AIServiceEdgeClient = class {
422
434
  return createGenerationStream(response, controller);
423
435
  }
424
436
  };
425
- var parseErrorResponse = async (response) => {
426
- throw new Error(`AI Service error: ${response.status} ${response.statusText} ${await response.text()}`);
427
- };
428
437
 
429
438
  // packages/experimental/assistant/src/ai-service/message-collector.ts
430
439
  var MessageCollector = class {
@@ -493,57 +502,225 @@ function* emitMessageAsEvents(message) {
493
502
  }
494
503
 
495
504
  // packages/experimental/assistant/src/ai-service/types.ts
496
- import { Schema } from "effect";
497
- import { Tool, Message } from "@dxos/artifact";
505
+ import { Schema as S2 } from "effect";
506
+ import { Tool, Message as Message2, SpaceIdSchema } from "@dxos/artifact";
507
+ import { ObjectId } from "@dxos/echo-schema";
498
508
  var createArtifactElement = (id) => `<artifact id=${id} />`;
499
- var LLMModel = Schema.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
509
+ var LLMModel = S2.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
500
510
  var ToolTypes = Object.freeze({
501
511
  // TODO(dmaretskyi): Not implemented yet.
502
512
  // DatabaseQuery: 'database_query',
503
513
  TextToImage: "text_to_image"
504
514
  });
505
- var GenerateRequest = Schema.Struct({
515
+ var GenerateRequest = S2.Struct({
516
+ spaceId: S2.optional(SpaceIdSchema),
517
+ threadId: S2.optional(ObjectId),
506
518
  /**
507
519
  * Preferred model or system default.
508
520
  */
509
- model: Schema.optional(LLMModel),
521
+ model: S2.optional(LLMModel),
510
522
  /**
511
523
  * Tools available for the LLM.
512
524
  */
513
- tools: Schema.optional(Schema.Array(Tool).pipe(Schema.mutable)),
525
+ tools: S2.optional(S2.Array(Tool).pipe(S2.mutable)),
514
526
  /**
515
527
  * System instructions to the LLM.
516
528
  */
517
- systemPrompt: Schema.optional(Schema.String),
529
+ systemPrompt: S2.optional(S2.String),
518
530
  /**
519
531
  * History of messages to include in the context window.
520
532
  */
521
533
  // TODO(burdon): Rename messages.
522
- history: Schema.optional(Schema.Array(Message)),
534
+ history: S2.optional(S2.Array(Message2)),
523
535
  /**
524
536
  * Current request.
525
537
  */
526
- prompt: Schema.optional(Message)
527
- });
528
- var GenerateResponse = Schema.Struct({
529
- messages: Schema.Array(Message),
530
- /**
531
- * Number of tokens used in the response.
532
- */
533
- tokenCount: Schema.optional(Schema.Number)
538
+ prompt: S2.optional(Message2)
534
539
  });
535
540
 
536
- // packages/experimental/assistant/src/session/tools.ts
537
- import { Message as Message2, ToolResult } from "@dxos/artifact";
538
- import { create } from "@dxos/echo-schema";
541
+ // packages/experimental/assistant/src/conversation/conversation.ts
542
+ import { ToolResult, Message as Message3 } from "@dxos/artifact";
543
+ import { createStatic, ObjectId as ObjectId2 } from "@dxos/echo-schema";
539
544
  import { invariant as invariant2 } from "@dxos/invariant";
540
545
  import { log as log3 } from "@dxos/log";
541
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/session/tools.ts";
546
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
547
+ var runLLM = async (params) => {
548
+ let conversationResult = null;
549
+ const history = params.history ?? [];
550
+ const generate = async () => {
551
+ log3("llm generate", {
552
+ tools: params.tools
553
+ }, {
554
+ F: __dxlog_file3,
555
+ L: 44,
556
+ S: void 0,
557
+ C: (f, a) => f(...a)
558
+ });
559
+ const beginTs = Date.now();
560
+ const stream = await params.client.exec({
561
+ model: params.model,
562
+ spaceId: params.spaceId,
563
+ threadId: params.threadId,
564
+ history,
565
+ systemPrompt: params.system,
566
+ tools: params.tools
567
+ });
568
+ for await (const event of stream) {
569
+ params.logger?.(event);
570
+ }
571
+ await stream.complete();
572
+ const messages = [];
573
+ const message = messages.at(-1);
574
+ invariant2(message, void 0, {
575
+ F: __dxlog_file3,
576
+ L: 63,
577
+ S: void 0,
578
+ A: [
579
+ "message",
580
+ ""
581
+ ]
582
+ });
583
+ log3("llm result", {
584
+ time: Date.now() - beginTs,
585
+ message
586
+ }, {
587
+ F: __dxlog_file3,
588
+ L: 65,
589
+ S: void 0,
590
+ C: (f, a) => f(...a)
591
+ });
592
+ invariant2(message, void 0, {
593
+ F: __dxlog_file3,
594
+ L: 66,
595
+ S: void 0,
596
+ A: [
597
+ "message",
598
+ ""
599
+ ]
600
+ });
601
+ params.logger?.({
602
+ type: "message",
603
+ message
604
+ });
605
+ history.push(...messages.map((message2) => ({
606
+ ...message2,
607
+ content: message2.content.filter((block) => block.type !== "image")
608
+ })));
609
+ const isToolUse2 = message.content.at(-1)?.type === "tool_use";
610
+ if (isToolUse2) {
611
+ const toolCalls = message.content.filter((block) => block.type === "tool_use");
612
+ invariant2(toolCalls.length === 1, void 0, {
613
+ F: __dxlog_file3,
614
+ L: 77,
615
+ S: void 0,
616
+ A: [
617
+ "toolCalls.length === 1",
618
+ ""
619
+ ]
620
+ });
621
+ const toolCall = toolCalls[0];
622
+ const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
623
+ if (!tool) {
624
+ throw new Error(`Tool not found: ${toolCall.name}`);
625
+ }
626
+ invariant2(tool.execute, void 0, {
627
+ F: __dxlog_file3,
628
+ L: 84,
629
+ S: void 0,
630
+ A: [
631
+ "tool.execute",
632
+ ""
633
+ ]
634
+ });
635
+ const toolResult = await tool.execute(toolCall.input);
636
+ switch (toolResult.kind) {
637
+ case "error": {
638
+ log3.warn("tool error", {
639
+ message: toolResult.message
640
+ }, {
641
+ F: __dxlog_file3,
642
+ L: 88,
643
+ S: void 0,
644
+ C: (f, a) => f(...a)
645
+ });
646
+ const resultMessage = {
647
+ id: ObjectId2.random(),
648
+ spaceId: params.spaceId,
649
+ threadId: params.threadId,
650
+ role: "user",
651
+ content: [
652
+ {
653
+ type: "tool_result",
654
+ toolUseId: toolCall.id,
655
+ content: toolResult.message,
656
+ isError: true
657
+ }
658
+ ]
659
+ };
660
+ params.logger?.({
661
+ type: "message",
662
+ message: resultMessage
663
+ });
664
+ history.push(resultMessage);
665
+ return true;
666
+ }
667
+ case "success": {
668
+ log3("tool success", {
669
+ result: toolResult.result
670
+ }, {
671
+ F: __dxlog_file3,
672
+ L: 110,
673
+ S: void 0,
674
+ C: (f, a) => f(...a)
675
+ });
676
+ const resultMessage = {
677
+ id: ObjectId2.random(),
678
+ spaceId: params.spaceId,
679
+ threadId: params.threadId,
680
+ role: "user",
681
+ content: [
682
+ {
683
+ type: "tool_result",
684
+ toolUseId: toolCall.id,
685
+ content: JSON.stringify(toolResult.result)
686
+ }
687
+ ]
688
+ };
689
+ params.logger?.({
690
+ type: "message",
691
+ message: resultMessage
692
+ });
693
+ history.push(resultMessage);
694
+ return true;
695
+ }
696
+ case "break": {
697
+ log3("tool break", {
698
+ result: toolResult.result
699
+ }, {
700
+ F: __dxlog_file3,
701
+ L: 131,
702
+ S: void 0,
703
+ C: (f, a) => f(...a)
704
+ });
705
+ conversationResult = toolResult.result;
706
+ return false;
707
+ }
708
+ }
709
+ }
710
+ return false;
711
+ };
712
+ while (await generate()) {
713
+ }
714
+ return {
715
+ result: conversationResult,
716
+ history
717
+ };
718
+ };
542
719
  var isToolUse = (message, { onlyToolNames } = {}) => {
543
720
  const block = message.content.at(-1);
544
721
  invariant2(block, void 0, {
545
722
  F: __dxlog_file3,
546
- L: 12,
723
+ L: 152,
547
724
  S: void 0,
548
725
  A: [
549
726
  "block",
@@ -556,7 +733,7 @@ var runTools = async ({ message, tools, extensions }) => {
556
733
  const toolCalls = message.content.filter((block) => block.type === "tool_use");
557
734
  invariant2(toolCalls.length === 1, void 0, {
558
735
  F: __dxlog_file3,
559
- L: 34,
736
+ L: 174,
560
737
  S: void 0,
561
738
  A: [
562
739
  "toolCalls.length === 1",
@@ -566,7 +743,7 @@ var runTools = async ({ message, tools, extensions }) => {
566
743
  const toolCall = toolCalls[0];
567
744
  const tool = tools.find((tool2) => tool2.name === toolCall.name);
568
745
  if (!tool) {
569
- const resultMessage = create(Message2, {
746
+ const resultMessage = createStatic(Message3, {
570
747
  role: "user",
571
748
  content: [
572
749
  {
@@ -586,7 +763,7 @@ var runTools = async ({ message, tools, extensions }) => {
586
763
  try {
587
764
  invariant2(tool.execute, void 0, {
588
765
  F: __dxlog_file3,
589
- L: 59,
766
+ L: 199,
590
767
  S: void 0,
591
768
  A: [
592
769
  "tool.execute",
@@ -601,7 +778,7 @@ var runTools = async ({ message, tools, extensions }) => {
601
778
  error
602
779
  }, {
603
780
  F: __dxlog_file3,
604
- L: 62,
781
+ L: 202,
605
782
  S: void 0,
606
783
  C: (f, a) => f(...a)
607
784
  });
@@ -613,11 +790,11 @@ var runTools = async ({ message, tools, extensions }) => {
613
790
  message: toolResult.message
614
791
  }, {
615
792
  F: __dxlog_file3,
616
- L: 68,
793
+ L: 208,
617
794
  S: void 0,
618
795
  C: (f, a) => f(...a)
619
796
  });
620
- const resultMessage = create(Message2, {
797
+ const resultMessage = createStatic(Message3, {
621
798
  role: "user",
622
799
  content: [
623
800
  {
@@ -638,11 +815,11 @@ var runTools = async ({ message, tools, extensions }) => {
638
815
  result: toolResult.result
639
816
  }, {
640
817
  F: __dxlog_file3,
641
- L: 89,
818
+ L: 229,
642
819
  S: void 0,
643
820
  C: (f, a) => f(...a)
644
821
  });
645
- const resultMessage = create(Message2, {
822
+ const resultMessage = createStatic(Message3, {
646
823
  role: "user",
647
824
  content: [
648
825
  {
@@ -663,7 +840,7 @@ var runTools = async ({ message, tools, extensions }) => {
663
840
  result: toolResult.result
664
841
  }, {
665
842
  F: __dxlog_file3,
666
- L: 110,
843
+ L: 250,
667
844
  S: void 0,
668
845
  C: (f, a) => f(...a)
669
846
  });
@@ -675,49 +852,10 @@ var runTools = async ({ message, tools, extensions }) => {
675
852
  }
676
853
  };
677
854
 
678
- // packages/experimental/assistant/src/session/version-pin.ts
679
- import { Schema as Schema2 } from "effect";
680
- import { getVersion } from "@dxos/echo-db";
681
- import { ObjectId } from "@dxos/echo-schema";
682
- var ObjectVersionSchema = Schema2.Unknown;
683
- var VersionPinSchema = Schema2.Struct({
684
- // TODO(dmaretskyi): Use Ref when those support encoding.
685
- objectId: ObjectId,
686
- // TODO(dmaretskyi): Could be opaque
687
- version: ObjectVersionSchema
688
- });
689
- var VersionPin = class extends VersionPinSchema {
690
- static {
691
- this.DISPOSITION = "version-pin";
692
- }
693
- static fromObject(object) {
694
- return VersionPin.make({
695
- objectId: object.id,
696
- version: getVersion(object)
697
- });
698
- }
699
- static createBlock(pin) {
700
- return {
701
- type: "json",
702
- disposition: VersionPin.DISPOSITION,
703
- json: JSON.stringify(pin)
704
- };
705
- }
706
- };
707
-
708
- // packages/experimental/assistant/src/session/session.ts
709
- import { Option, Schema as Schema4 } from "effect";
710
- import { defineTool as defineTool2, Message as Message4, structuredOutputParser, ToolResult as ToolResult3 } from "@dxos/artifact";
711
- import { Event as Event2, synchronized } from "@dxos/async";
712
- import { ObjectVersion } from "@dxos/echo-db";
713
- import { create as create3 } from "@dxos/echo-schema";
714
- import { invariant as invariant5 } from "@dxos/invariant";
715
- import { log as log6 } from "@dxos/log";
716
-
717
855
  // packages/experimental/assistant/src/ai-service/ollama-client.ts
718
- import { Schema as Schema3 } from "effect";
856
+ import { Schema as S3 } from "effect";
719
857
  import { defineTool, ToolResult as ToolResult2 } from "@dxos/artifact";
720
- import { ObjectId as ObjectId2 } from "@dxos/echo-schema";
858
+ import { ObjectId as ObjectId3 } from "@dxos/echo-schema";
721
859
  import { invariant as invariant3 } from "@dxos/invariant";
722
860
  import { log as log4 } from "@dxos/log";
723
861
  var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/ollama-client.ts";
@@ -862,14 +1000,14 @@ var OllamaClient = class {
862
1000
  signal
863
1001
  });
864
1002
  if (!response.ok) {
865
- throw await parseErrorResponse2(response);
1003
+ throw await parseErrorResponse(response);
866
1004
  }
867
1005
  if (!response.body) {
868
1006
  throw new Error("Ollama API returned no response body");
869
1007
  }
870
1008
  const reader = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new OllamaDecoderStream()).getReader();
871
1009
  try {
872
- const messageId = ObjectId2.random();
1010
+ const messageId = ObjectId3.random();
873
1011
  yield {
874
1012
  type: "message_start",
875
1013
  message: {
@@ -929,7 +1067,7 @@ var OllamaClient = class {
929
1067
  index: currentBlockIndex,
930
1068
  content: {
931
1069
  type: "tool_use",
932
- id: ObjectId2.random(),
1070
+ id: ObjectId3.random(),
933
1071
  name: data.message.tool_calls[0].function.name,
934
1072
  input: sanitizeToolArguments(data.message.tool_calls[0].function.arguments)
935
1073
  }
@@ -953,13 +1091,7 @@ var OllamaClient = class {
953
1091
  reader.releaseLock();
954
1092
  }
955
1093
  }
956
- /**
957
- * Generate non-streaming response.
958
- */
959
1094
  async exec(request) {
960
- throw new Error("Not implemented");
961
- }
962
- async execStream(request) {
963
1095
  const controller = new AbortController();
964
1096
  try {
965
1097
  return new GenerationStreamImpl(controller, async function* () {
@@ -1021,15 +1153,15 @@ var WELL_KNOWN_TOOLS = {
1021
1153
  [ToolTypes.TextToImage]: defineTool("system", {
1022
1154
  name: "text-to-image",
1023
1155
  description: "Generate an image from a text prompt",
1024
- schema: Schema3.Struct({
1025
- prompt: Schema3.String.annotations({
1156
+ schema: S3.Struct({
1157
+ prompt: S3.String.annotations({
1026
1158
  description: "The text prompt describing the image to generate"
1027
1159
  })
1028
1160
  }),
1029
1161
  execute: async ({ prompt }) => {
1030
1162
  const image = await fetch(SAMPLE_IMAGE_URL).then(async (res) => Buffer.from(await res.arrayBuffer()));
1031
1163
  const imageBase64 = image.toString("base64");
1032
- const id = ObjectId2.random();
1164
+ const id = ObjectId3.random();
1033
1165
  return ToolResult2.Success(`The generation was successful. Image ID: ${id} Prompt: ${prompt}`, [
1034
1166
  {
1035
1167
  type: "image",
@@ -1065,12 +1197,12 @@ var OllamaDecoderStream = class extends TransformStream {
1065
1197
  this._buffer = "";
1066
1198
  }
1067
1199
  };
1068
- var ModelDoesNotSupportToolsError = class extends Schema3.TaggedError()("ModelDoesNotSupportToolsError", {
1069
- model: Schema3.String
1200
+ var ModelDoesNotSupportToolsError = class extends S3.TaggedError()("ModelDoesNotSupportToolsError", {
1201
+ model: S3.String
1070
1202
  }) {
1071
1203
  };
1072
- var ModelNotFoundError = class extends Schema3.TaggedError()("ModelNotFoundError", {
1073
- model: Schema3.String
1204
+ var ModelNotFoundError = class extends S3.TaggedError()("ModelNotFoundError", {
1205
+ model: S3.String
1074
1206
  }) {
1075
1207
  };
1076
1208
  var parseOllamaError = (error) => {
@@ -1092,7 +1224,7 @@ var parseOllamaError = (error) => {
1092
1224
  }
1093
1225
  throw new Error(`Ollama API error: ${error}`);
1094
1226
  };
1095
- var parseErrorResponse2 = async (response) => {
1227
+ var parseErrorResponse = async (response) => {
1096
1228
  if (response.headers.get("content-type")?.includes("application/json")) {
1097
1229
  const body = await response.json();
1098
1230
  return parseOllamaError(body.error);
@@ -1100,14 +1232,6 @@ var parseErrorResponse2 = async (response) => {
1100
1232
  throw new Error(`Ollama API error: ${response.status} ${response.statusText} ${await response.text()}`);
1101
1233
  };
1102
1234
 
1103
- // packages/experimental/assistant/src/ai-service/parser.ts
1104
- import { Message as Message3 } from "@dxos/artifact";
1105
- import { Event } from "@dxos/async";
1106
- import { create as create2 } from "@dxos/echo-schema";
1107
- import { invariant as invariant4 } from "@dxos/invariant";
1108
- import { log as log5 } from "@dxos/log";
1109
- import { isNotFalsy, safeParseJson } from "@dxos/util";
1110
-
1111
1235
  // packages/experimental/assistant/src/ai-service/transform.ts
1112
1236
  import * as P from "parsimmon";
1113
1237
  var textChunk = P.regexp(/[^<]+/).map((content) => ({
@@ -1198,6 +1322,12 @@ var StreamTransform = class {
1198
1322
  };
1199
1323
 
1200
1324
  // packages/experimental/assistant/src/ai-service/parser.ts
1325
+ import { Message as Message4 } from "@dxos/artifact";
1326
+ import { Event } from "@dxos/async";
1327
+ import { createStatic as createStatic2 } from "@dxos/echo-schema";
1328
+ import { invariant as invariant4 } from "@dxos/invariant";
1329
+ import { log as log5 } from "@dxos/log";
1330
+ import { isNotFalsy, safeParseJson } from "@dxos/util";
1201
1331
  var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
1202
1332
  var MixedStreamParser = class {
1203
1333
  constructor() {
@@ -1273,7 +1403,7 @@ var MixedStreamParser = class {
1273
1403
  C: (f, a) => f(...a)
1274
1404
  });
1275
1405
  }
1276
- this._message = create2(Message3, {
1406
+ this._message = createStatic2(Message4, {
1277
1407
  role: event.message.role,
1278
1408
  content: [
1279
1409
  ...event.message.content
@@ -1554,18 +1684,6 @@ var mergeMessageBlock = (contentBlock, streamBlock) => {
1554
1684
  }
1555
1685
  break;
1556
1686
  }
1557
- case "proposal": {
1558
- if (streamBlock.content.length === 1 && streamBlock.content[0].type === "text") {
1559
- return {
1560
- type: "json",
1561
- disposition: "proposal",
1562
- json: JSON.stringify({
1563
- text: streamBlock.content[0].content
1564
- })
1565
- };
1566
- }
1567
- break;
1568
- }
1569
1687
  case "select": {
1570
1688
  return {
1571
1689
  type: "json",
@@ -1609,596 +1727,10 @@ var mergeMessageBlock = (contentBlock, streamBlock) => {
1609
1727
  }
1610
1728
  };
1611
1729
 
1612
- // packages/experimental/assistant/src/session/session.ts
1613
- function _ts_decorate(decorators, target, key, desc) {
1614
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1615
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1616
- 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;
1617
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1618
- }
1619
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/session/session.ts";
1620
- var AISession = class _AISession {
1621
- constructor(_options) {
1622
- this._options = _options;
1623
- this._parser = new MixedStreamParser();
1624
- this._pending = [];
1625
- this._history = [];
1626
- this.message = this._parser.message;
1627
- this.block = this._parser.block;
1628
- this.update = this._parser.update;
1629
- this.streamEvent = this._parser.streamEvent;
1630
- this.userMessage = new Event2();
1631
- this._parser.message.on((message) => {
1632
- this._pending.push(message);
1633
- });
1634
- }
1635
- async run(options) {
1636
- const systemTools = [];
1637
- switch (this._options.operationModel) {
1638
- case "planning":
1639
- systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createPlanningTool(options));
1640
- break;
1641
- case "importing":
1642
- systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createRequireTool(options.artifacts, (artifactDefinitionIds) => {
1643
- for (const artifactDefinitionId of artifactDefinitionIds) {
1644
- requiredArtifactIds.add(artifactDefinitionId);
1645
- }
1646
- }));
1647
- break;
1648
- case "configured":
1649
- break;
1650
- default:
1651
- throw new TypeError(`Invalid operation model: ${this._options.operationModel}`);
1652
- }
1653
- this._history = [
1654
- ...options.history
1655
- ];
1656
- this._pending = [
1657
- await this._formatUserPrompt(options.artifactDiffResolver, options.prompt, options.history)
1658
- ];
1659
- this.userMessage.emit(this._pending.at(-1));
1660
- this._stream = void 0;
1661
- let error;
1662
- const requiredArtifactIds = new Set(options.requiredArtifactIds ?? []);
1663
- try {
1664
- let more = false;
1665
- do {
1666
- const tools = [
1667
- ...systemTools,
1668
- ...options.tools,
1669
- ...options.artifacts.filter((artifact) => requiredArtifactIds.has(artifact.id)).flatMap((artifact) => artifact.tools)
1670
- ];
1671
- log6("request", {
1672
- pending: this._pending.length,
1673
- history: this._history.length,
1674
- tools: tools.map((tool) => tool.name)
1675
- }, {
1676
- F: __dxlog_file6,
1677
- L: 181,
1678
- S: this,
1679
- C: (f, a) => f(...a)
1680
- });
1681
- this._stream = await options.client.execStream({
1682
- ...options.generationOptions ?? {},
1683
- // TODO(burdon): Rename messages or separate history/message.
1684
- history: [
1685
- ...this._history,
1686
- ...this._pending
1687
- ],
1688
- tools,
1689
- systemPrompt: options.systemPrompt ?? createBaseInstructions({
1690
- availableArtifacts: Array.from(requiredArtifactIds),
1691
- operationModel: this._options.operationModel
1692
- })
1693
- });
1694
- await this._parser.parse(this._stream);
1695
- await this._stream.complete();
1696
- log6("response", {
1697
- pending: this._pending
1698
- }, {
1699
- F: __dxlog_file6,
1700
- L: 206,
1701
- S: this,
1702
- C: (f, a) => f(...a)
1703
- });
1704
- more = false;
1705
- const message = this._pending.at(-1);
1706
- invariant5(message, void 0, {
1707
- F: __dxlog_file6,
1708
- L: 211,
1709
- S: this,
1710
- A: [
1711
- "message",
1712
- ""
1713
- ]
1714
- });
1715
- if (isToolUse(message)) {
1716
- log6("tool request...", void 0, {
1717
- F: __dxlog_file6,
1718
- L: 213,
1719
- S: this,
1720
- C: (f, a) => f(...a)
1721
- });
1722
- const response = await runTools({
1723
- message: this._pending.at(-1),
1724
- tools,
1725
- extensions: options.extensions ?? {}
1726
- });
1727
- log6("tool response", {
1728
- response
1729
- }, {
1730
- F: __dxlog_file6,
1731
- L: 220,
1732
- S: this,
1733
- C: (f, a) => f(...a)
1734
- });
1735
- switch (response.type) {
1736
- case "continue": {
1737
- this._pending = [
1738
- ...this._pending,
1739
- response.message
1740
- ];
1741
- this.userMessage.emit(response.message);
1742
- more = true;
1743
- break;
1744
- }
1745
- }
1746
- }
1747
- } while (more);
1748
- } catch (err) {
1749
- log6.catch(err, void 0, {
1750
- F: __dxlog_file6,
1751
- L: 232,
1752
- S: this,
1753
- C: (f, a) => f(...a)
1754
- });
1755
- if (err instanceof Error && err.message.includes("Overloaded")) {
1756
- error = new AIServiecOverloadedError("AI service overloaded", {
1757
- cause: err
1758
- });
1759
- } else {
1760
- error = new Error("AI service error", {
1761
- cause: err
1762
- });
1763
- }
1764
- } finally {
1765
- this._stream = void 0;
1766
- }
1767
- if (error) {
1768
- throw error;
1769
- }
1770
- return this._pending;
1771
- }
1772
- async runStructured(schema, options) {
1773
- const parser = structuredOutputParser(schema);
1774
- const result = await this.run({
1775
- ...options,
1776
- tools: [
1777
- ...options.tools,
1778
- parser.tool
1779
- ]
1780
- });
1781
- return parser.getResult(result);
1782
- }
1783
- async _formatUserPrompt(artifactDiffResolver, prompt, history) {
1784
- const prelude = [];
1785
- if (artifactDiffResolver) {
1786
- const versions = gatherObjectVersions(history);
1787
- const artifactDiff = await artifactDiffResolver(Array.from(versions.entries()).map(([id, version]) => ({
1788
- id,
1789
- lastVersion: version
1790
- })));
1791
- log6.info("vision", {
1792
- artifactDiff,
1793
- versions
1794
- }, {
1795
- F: __dxlog_file6,
1796
- L: 272,
1797
- S: this,
1798
- C: (f, a) => f(...a)
1799
- });
1800
- for (const [id, { version }] of [
1801
- ...artifactDiff.entries()
1802
- ]) {
1803
- if (ObjectVersion.equals(version, versions.get(id))) {
1804
- artifactDiff.delete(id);
1805
- continue;
1806
- }
1807
- prelude.push(VersionPin.createBlock(VersionPin.make({
1808
- objectId: id,
1809
- version
1810
- })));
1811
- }
1812
- if (artifactDiff.size > 0) {
1813
- prelude.push(createArtifactUpdateBlock(artifactDiff));
1814
- }
1815
- }
1816
- return create3(Message4, {
1817
- role: "user",
1818
- content: [
1819
- ...prelude,
1820
- {
1821
- type: "text",
1822
- text: prompt
1823
- }
1824
- ]
1825
- });
1826
- }
1827
- _createQueryArtifactsTool(artifacts) {
1828
- return defineTool2("system", {
1829
- name: "query_artifact_definitions",
1830
- description: "Query the available artifact definitions",
1831
- schema: Schema4.Struct({}),
1832
- execute: async () => {
1833
- return ToolResult3.Success({
1834
- artifactDefinitions: artifacts.map((artifact) => ({
1835
- id: artifact.id,
1836
- name: artifact.name,
1837
- description: artifact.description
1838
- }))
1839
- });
1840
- }
1841
- });
1842
- }
1843
- _createRequireTool(artifacts, onRequire) {
1844
- return defineTool2("system", {
1845
- name: "require_artifact_definitions",
1846
- description: "Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.",
1847
- schema: Schema4.Struct({
1848
- artifactDefinitionIds: Schema4.Array(Schema4.String).annotations({
1849
- description: "The ids of the artifact definitions to require",
1850
- examples: [
1851
- [
1852
- "artifact:dxos.org/example/Test"
1853
- ]
1854
- ]
1855
- })
1856
- }),
1857
- execute: async ({ artifactDefinitionIds }) => {
1858
- const missingArtifactDefinitionIds = artifactDefinitionIds.filter((artifactId) => !artifacts.some((artifact) => artifact.id === artifactId));
1859
- if (missingArtifactDefinitionIds.length > 0) {
1860
- return ToolResult3.Error(`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(", ")}`);
1861
- }
1862
- onRequire(artifactDefinitionIds);
1863
- return ToolResult3.Success({});
1864
- }
1865
- });
1866
- }
1867
- _createPlanningTool(options) {
1868
- return defineTool2("system", {
1869
- name: "create_plan",
1870
- description: "Create a plan. Make sure that each step is independent and can be executed solely on the data returned by the previous step. The steps only share the data that is specified in the plan.",
1871
- schema: Schema4.Struct({
1872
- goal: Schema4.String.annotations({
1873
- description: "The goal that the plan will achieve."
1874
- }),
1875
- steps: Schema4.Array(Schema4.Struct({
1876
- action: Schema4.String.annotations({
1877
- description: "Complete, detailed action to perform."
1878
- }),
1879
- input: Schema4.String.annotations({
1880
- description: "The required input data for this step. First step must not require any data."
1881
- }),
1882
- output: Schema4.String.annotations({
1883
- description: "The output data from this step. This is passed to the next step as input."
1884
- }),
1885
- requiredArtifactIds: Schema4.Array(Schema4.String).annotations({
1886
- description: "The ids of the artifacts required to perform this step.",
1887
- examples: [
1888
- [
1889
- "artifact:dxos.org/example/Test"
1890
- ]
1891
- ]
1892
- })
1893
- })).annotations({
1894
- description: "Steps"
1895
- })
1896
- }),
1897
- execute: async ({ goal, steps }) => {
1898
- const missingArtifactIds = steps.flatMap((step) => step.requiredArtifactIds).filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
1899
- if (missingArtifactIds.length > 0) {
1900
- return ToolResult3.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(", ")}`);
1901
- }
1902
- const stepResults = [];
1903
- for (const step of steps) {
1904
- log6("executing step", {
1905
- action: step.action
1906
- }, {
1907
- F: __dxlog_file6,
1908
- L: 379,
1909
- S: this,
1910
- C: (f, a) => f(...a)
1911
- });
1912
- const session = new _AISession({
1913
- operationModel: "importing"
1914
- });
1915
- session.message.on((ev) => this.message.emit(ev));
1916
- session.block.on((ev) => this.block.emit(ev));
1917
- session.update.on((ev) => this.update.emit(ev));
1918
- session.streamEvent.on((ev) => this.streamEvent.emit(ev));
1919
- session.userMessage.on((ev) => this.userMessage.emit(ev));
1920
- const messages = await session.run({
1921
- client: options.client,
1922
- artifacts: options.artifacts,
1923
- tools: options.tools,
1924
- history: options.history,
1925
- extensions: options.extensions,
1926
- generationOptions: options.generationOptions,
1927
- requiredArtifactIds: step.requiredArtifactIds.slice(),
1928
- artifactDiffResolver: options.artifactDiffResolver,
1929
- prompt: `
1930
- You are an agent that executes the subtask in a plan.
1931
-
1932
- While you know the plan's goal only focus on your subtask
1933
- Use the data from the previous step to complete your task.
1934
- Return all of the data from "output" in your last message.
1935
-
1936
- Current step (that you need to complete):
1937
- ${JSON.stringify({
1938
- goal,
1939
- input: step.input,
1940
- output: step.output,
1941
- yourTask: step.action
1942
- }, null, 2)}
1943
-
1944
- Previous step results:
1945
- ${JSON.stringify(stepResults.at(-1))}.
1946
- `
1947
- });
1948
- const result = messages.at(-1);
1949
- stepResults.push(result);
1950
- }
1951
- return ToolResult3.Success({
1952
- stepResults
1953
- });
1954
- }
1955
- });
1956
- }
1957
- abort() {
1958
- this._stream?.abort();
1959
- }
1960
- };
1961
- _ts_decorate([
1962
- synchronized
1963
- ], AISession.prototype, "run", null);
1964
- var createBaseInstructions = ({ availableArtifacts, operationModel }) => `
1965
- You are a friendly, advanced AI assistant capable of creating and managing artifacts from available data and tools.
1966
- Your task is to process user commands and questions and decide how best to respond.
1967
- In some cases, you will need to create or reference data objects called artifacts.
1968
-
1969
- Follow these guidelines carefully:
1970
-
1971
- Decision-making:
1972
-
1973
- - Analyze the structure and type of the content in the user's message.
1974
-
1975
-
1976
- ${operationModel === "planning" ? `
1977
- - Can you complete the task using the available artifacts?
1978
- - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
1979
- - Identify which artifacts are relevant to the user's request.
1980
- - Break down the user's request into a step-by-step plan that you will use to execute the user's request, the plan items should include artifacts which will be used to perform the actions.
1981
- ` : ""}
1982
- ${operationModel === "importing" ? `
1983
- - Can you complete the task using the available artifacts?
1984
- - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
1985
- - Identify which artifacts are relevant to the user's request.
1986
- - Are the required artifacts already available?
1987
- - If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
1988
- - The require'd artifact tools will be available for use after require.
1989
- ` : ""}
1990
- ${operationModel === "configured" ? `
1991
- - Select the most relevant artifact(s) to complete the task.
1992
- - Call the appropriate tool to use the artifact(s).
1993
- ` : ""}
1994
-
1995
- ${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join("\n")}` : ""}
1996
- `;
1997
- var AIServiecOverloadedError = class extends Schema4.TaggedError()("AIServiecOverloadedError", {}) {
1998
- };
1999
- var gatherObjectVersions = (messages) => {
2000
- const artifactIds = /* @__PURE__ */ new Map();
2001
- for (const message of messages) {
2002
- for (const block of message.content) {
2003
- if (block.type === "json" && block.disposition === VersionPin.DISPOSITION) {
2004
- const pin = VersionPin.pipe(Schema4.decodeOption)(JSON.parse(block.json)).pipe(Option.getOrUndefined);
2005
- if (!pin) {
2006
- continue;
2007
- }
2008
- artifactIds.set(pin.objectId, pin.version);
2009
- }
2010
- }
2011
- }
2012
- return artifactIds;
2013
- };
2014
- var createArtifactUpdateBlock = (artifactDiff) => {
2015
- return {
2016
- type: "text",
2017
- disposition: "artifact-update",
2018
- text: `
2019
- The following artifacts have been updated since the last message:
2020
- ${Array.from(artifactDiff.entries()).map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
2021
- ${diff}` : ""}</changed-artifact>`).join("\n")}
2022
- `
2023
- };
2024
- };
2025
-
2026
- // packages/experimental/assistant/src/experimental/conversation/conversation.ts
2027
- import { ObjectId as ObjectId3 } from "@dxos/echo-schema";
2028
- import { invariant as invariant6 } from "@dxos/invariant";
2029
- import { log as log7 } from "@dxos/log";
2030
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/experimental/conversation/conversation.ts";
2031
- var runLLM = async (params) => {
2032
- let conversationResult = null;
2033
- const history = params.history ?? [];
2034
- const generate = async () => {
2035
- log7("llm generate", {
2036
- tools: params.tools
2037
- }, {
2038
- F: __dxlog_file7,
2039
- L: 44,
2040
- S: void 0,
2041
- C: (f, a) => f(...a)
2042
- });
2043
- const beginTs = Date.now();
2044
- const stream = await params.client.execStream({
2045
- model: params.model,
2046
- history,
2047
- systemPrompt: params.system,
2048
- tools: params.tools
2049
- });
2050
- for await (const event of stream) {
2051
- params.logger?.(event);
2052
- }
2053
- await stream.complete();
2054
- const messages = [];
2055
- const message = messages.at(-1);
2056
- invariant6(message, void 0, {
2057
- F: __dxlog_file7,
2058
- L: 61,
2059
- S: void 0,
2060
- A: [
2061
- "message",
2062
- ""
2063
- ]
2064
- });
2065
- log7("llm result", {
2066
- time: Date.now() - beginTs,
2067
- message
2068
- }, {
2069
- F: __dxlog_file7,
2070
- L: 63,
2071
- S: void 0,
2072
- C: (f, a) => f(...a)
2073
- });
2074
- invariant6(message, void 0, {
2075
- F: __dxlog_file7,
2076
- L: 64,
2077
- S: void 0,
2078
- A: [
2079
- "message",
2080
- ""
2081
- ]
2082
- });
2083
- params.logger?.({
2084
- type: "message",
2085
- message
2086
- });
2087
- history.push(...messages.map((message2) => ({
2088
- ...message2,
2089
- content: message2.content.filter((block) => block.type !== "image")
2090
- })));
2091
- const isToolUse2 = message.content.at(-1)?.type === "tool_use";
2092
- if (isToolUse2) {
2093
- const toolCalls = message.content.filter((block) => block.type === "tool_use");
2094
- invariant6(toolCalls.length === 1, void 0, {
2095
- F: __dxlog_file7,
2096
- L: 75,
2097
- S: void 0,
2098
- A: [
2099
- "toolCalls.length === 1",
2100
- ""
2101
- ]
2102
- });
2103
- const toolCall = toolCalls[0];
2104
- const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
2105
- if (!tool) {
2106
- throw new Error(`Tool not found: ${toolCall.name}`);
2107
- }
2108
- invariant6(tool.execute, void 0, {
2109
- F: __dxlog_file7,
2110
- L: 82,
2111
- S: void 0,
2112
- A: [
2113
- "tool.execute",
2114
- ""
2115
- ]
2116
- });
2117
- const toolResult = await tool.execute(toolCall.input);
2118
- switch (toolResult.kind) {
2119
- case "error": {
2120
- log7.warn("tool error", {
2121
- message: toolResult.message
2122
- }, {
2123
- F: __dxlog_file7,
2124
- L: 86,
2125
- S: void 0,
2126
- C: (f, a) => f(...a)
2127
- });
2128
- const resultMessage = {
2129
- id: ObjectId3.random(),
2130
- role: "user",
2131
- content: [
2132
- {
2133
- type: "tool_result",
2134
- toolUseId: toolCall.id,
2135
- content: toolResult.message,
2136
- isError: true
2137
- }
2138
- ]
2139
- };
2140
- params.logger?.({
2141
- type: "message",
2142
- message: resultMessage
2143
- });
2144
- history.push(resultMessage);
2145
- return true;
2146
- }
2147
- case "success": {
2148
- log7("tool success", {
2149
- result: toolResult.result
2150
- }, {
2151
- F: __dxlog_file7,
2152
- L: 106,
2153
- S: void 0,
2154
- C: (f, a) => f(...a)
2155
- });
2156
- const resultMessage = {
2157
- id: ObjectId3.random(),
2158
- role: "user",
2159
- content: [
2160
- {
2161
- type: "tool_result",
2162
- toolUseId: toolCall.id,
2163
- content: JSON.stringify(toolResult.result)
2164
- }
2165
- ]
2166
- };
2167
- params.logger?.({
2168
- type: "message",
2169
- message: resultMessage
2170
- });
2171
- history.push(resultMessage);
2172
- return true;
2173
- }
2174
- case "break": {
2175
- log7("tool break", {
2176
- result: toolResult.result
2177
- }, {
2178
- F: __dxlog_file7,
2179
- L: 125,
2180
- S: void 0,
2181
- C: (f, a) => f(...a)
2182
- });
2183
- conversationResult = toolResult.result;
2184
- return false;
2185
- }
2186
- }
2187
- }
2188
- return false;
2189
- };
2190
- while (await generate()) {
2191
- }
2192
- return {
2193
- result: conversationResult,
2194
- history
2195
- };
2196
- };
2197
-
2198
- // packages/experimental/assistant/src/experimental/cypher/parjs.ts
1730
+ // packages/experimental/assistant/src/cypher/parjs.ts
2199
1731
  import { parjsCombinators, parjs } from "#parjs";
2200
1732
 
2201
- // packages/experimental/assistant/src/experimental/cypher/combinators.ts
1733
+ // packages/experimental/assistant/src/cypher/combinators.ts
2202
1734
  var ws = parjs.whitespace();
2203
1735
  var nothing = parjs.string("").pipe(parjsCombinators.map(() => null));
2204
1736
  var optional = (parser) => parser.pipe(parjsCombinators.recover(() => ({
@@ -2272,7 +1804,7 @@ var cypherQuery = ws.pipe(parjsCombinators.then(matchClause, ws, optional(whereC
2272
1804
  returnClause: returnClause2
2273
1805
  })));
2274
1806
 
2275
- // packages/experimental/assistant/src/experimental/cypher/parser.ts
1807
+ // packages/experimental/assistant/src/cypher/parser.ts
2276
1808
  var parseCypherQuery = (query) => {
2277
1809
  const parseResult = cypherQuery.parse(query);
2278
1810
  if (parseResult.isOk) {
@@ -2284,14 +1816,14 @@ var parseCypherQuery = (query) => {
2284
1816
  }
2285
1817
  };
2286
1818
 
2287
- // packages/experimental/assistant/src/experimental/cypher/query-executor.ts
2288
- import { log as log9 } from "@dxos/log";
1819
+ // packages/experimental/assistant/src/cypher/query-executor.ts
1820
+ import { log as log7 } from "@dxos/log";
2289
1821
  import { mapValues } from "@dxos/util";
2290
1822
 
2291
- // packages/experimental/assistant/src/experimental/cypher/execution-plan.ts
2292
- import { invariant as invariant7 } from "@dxos/invariant";
2293
- import { log as log8 } from "@dxos/log";
2294
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/experimental/cypher/execution-plan.ts";
1823
+ // packages/experimental/assistant/src/cypher/execution-plan.ts
1824
+ import { invariant as invariant5 } from "@dxos/invariant";
1825
+ import { log as log6 } from "@dxos/log";
1826
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/execution-plan.ts";
2295
1827
  var createExecutionPlan = (query) => {
2296
1828
  return new ExecutionPlanBuilder().build(query);
2297
1829
  };
@@ -2303,10 +1835,10 @@ var ExecutionPlanBuilder = class {
2303
1835
  return `anon_${this.counter++}`;
2304
1836
  }
2305
1837
  build(query) {
2306
- log8("Creating execution plan for query", {
1838
+ log6("Creating execution plan for query", {
2307
1839
  query
2308
1840
  }, {
2309
- F: __dxlog_file8,
1841
+ F: __dxlog_file6,
2310
1842
  L: 76,
2311
1843
  S: this,
2312
1844
  C: (f, a) => f(...a)
@@ -2323,8 +1855,8 @@ var ExecutionPlanBuilder = class {
2323
1855
  };
2324
1856
  }
2325
1857
  createStepsForMatchClause(match) {
2326
- invariant7(match.pattern.segments.length > 0, void 0, {
2327
- F: __dxlog_file8,
1858
+ invariant5(match.pattern.segments.length > 0, void 0, {
1859
+ F: __dxlog_file6,
2328
1860
  L: 90,
2329
1861
  S: this,
2330
1862
  A: [
@@ -2333,8 +1865,8 @@ var ExecutionPlanBuilder = class {
2333
1865
  ]
2334
1866
  });
2335
1867
  const firstSegment = match.pattern.segments[0];
2336
- invariant7(firstSegment.astKind === "NodePattern", void 0, {
2337
- F: __dxlog_file8,
1868
+ invariant5(firstSegment.astKind === "NodePattern", void 0, {
1869
+ F: __dxlog_file6,
2338
1870
  L: 92,
2339
1871
  S: this,
2340
1872
  A: [
@@ -2361,8 +1893,8 @@ var ExecutionPlanBuilder = class {
2361
1893
  }
2362
1894
  for (let i = 1; i < match.pattern.segments.length; i++) {
2363
1895
  const relationshipSegment = match.pattern.segments[i++];
2364
- invariant7(relationshipSegment.astKind === "RelationshipPattern", void 0, {
2365
- F: __dxlog_file8,
1896
+ invariant5(relationshipSegment.astKind === "RelationshipPattern", void 0, {
1897
+ F: __dxlog_file6,
2366
1898
  L: 115,
2367
1899
  S: this,
2368
1900
  A: [
@@ -2371,8 +1903,8 @@ var ExecutionPlanBuilder = class {
2371
1903
  ]
2372
1904
  });
2373
1905
  const nodeSegment = match.pattern.segments[i];
2374
- invariant7(nodeSegment.astKind === "NodePattern", void 0, {
2375
- F: __dxlog_file8,
1906
+ invariant5(nodeSegment.astKind === "NodePattern", void 0, {
1907
+ F: __dxlog_file6,
2376
1908
  L: 117,
2377
1909
  S: this,
2378
1910
  A: [
@@ -2454,16 +1986,16 @@ var getDirection = (left, right) => {
2454
1986
  throw new Error(`Invalid direction: ()${left}[]${right}()`);
2455
1987
  };
2456
1988
 
2457
- // packages/experimental/assistant/src/experimental/cypher/query-executor.ts
2458
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/experimental/cypher/query-executor.ts";
1989
+ // packages/experimental/assistant/src/cypher/query-executor.ts
1990
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/query-executor.ts";
2459
1991
  var executeQueryPlan = async (dataSource, plan) => {
2460
1992
  let buffer = [];
2461
1993
  const results = [];
2462
1994
  for (const step of plan.steps) {
2463
- log9("begin step", {
1995
+ log7("begin step", {
2464
1996
  step
2465
1997
  }, {
2466
- F: __dxlog_file9,
1998
+ F: __dxlog_file7,
2467
1999
  L: 41,
2468
2000
  S: void 0,
2469
2001
  C: (f, a) => f(...a)
@@ -2549,11 +2081,11 @@ var executeQueryPlan = async (dataSource, plan) => {
2549
2081
  break;
2550
2082
  }
2551
2083
  }
2552
- log9("finish step", {
2084
+ log7("finish step", {
2553
2085
  buffer: buffer.map((tuple) => mapValues(tuple, (x) => x.id)),
2554
2086
  results
2555
2087
  }, {
2556
- F: __dxlog_file9,
2088
+ F: __dxlog_file7,
2557
2089
  L: 118,
2558
2090
  S: void 0,
2559
2091
  C: (f, a) => f(...a)
@@ -2580,7 +2112,7 @@ var jsonPathGet = (obj, path) => {
2580
2112
  return result;
2581
2113
  };
2582
2114
 
2583
- // packages/experimental/assistant/src/experimental/cypher/schema.ts
2115
+ // packages/experimental/assistant/src/cypher/schema.ts
2584
2116
  import { raise } from "@dxos/debug";
2585
2117
  import { JSON_SCHEMA_ECHO_REF_ID } from "@dxos/echo-schema";
2586
2118
 
@@ -2592,7 +2124,7 @@ var trim = (strings, ...values) => {
2592
2124
  return combined.replace("/^\n/", "").replace(/\n$/, "");
2593
2125
  };
2594
2126
 
2595
- // packages/experimental/assistant/src/experimental/cypher/schema.ts
2127
+ // packages/experimental/assistant/src/cypher/schema.ts
2596
2128
  var formatJsonSchemaForLLM = (schemaTypes) => {
2597
2129
  return schemaTypes.flatMap((schema) => [
2598
2130
  formatNodeSchema(schema),
@@ -2646,18 +2178,19 @@ var formatInferredRelationshipLabel = (typename, property2) => {
2646
2178
  };
2647
2179
  var isReference = (schema) => schema.$id === JSON_SCHEMA_ECHO_REF_ID || schema.type === "array" && !Array.isArray(schema.items) && typeof schema.items === "object" && "$id" in schema.items && schema.items.$id === JSON_SCHEMA_ECHO_REF_ID;
2648
2180
 
2649
- // packages/experimental/assistant/src/experimental/cypher/echo-data-source.ts
2181
+ // packages/experimental/assistant/src/cypher/echo-data-source.ts
2650
2182
  import { raise as raise2 } from "@dxos/debug";
2651
2183
  import { Filter } from "@dxos/echo-db";
2652
- import { getSchema, getSchemaTypename, StoredSchema, toJsonSchema } from "@dxos/echo-schema";
2653
- import { log as log10 } from "@dxos/log";
2654
- function _ts_decorate2(decorators, target, key, desc) {
2184
+ import { getSchemaTypename, StoredSchema, toJsonSchema } from "@dxos/echo-schema";
2185
+ import { getSchema } from "@dxos/live-object";
2186
+ import { log as log8 } from "@dxos/log";
2187
+ function _ts_decorate(decorators, target, key, desc) {
2655
2188
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2656
2189
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2657
2190
  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;
2658
2191
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2659
2192
  }
2660
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/experimental/cypher/echo-data-source.ts";
2193
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/echo-data-source.ts";
2661
2194
  var EchoDataSource = class {
2662
2195
  constructor(_db) {
2663
2196
  this._db = _db;
@@ -2667,11 +2200,11 @@ var EchoDataSource = class {
2667
2200
  const { objects: objects2 } = await this._db.query().run();
2668
2201
  return objects2.map(this._objectToNode);
2669
2202
  }
2670
- const schema = (await this._getAllSchema()).find((schema2) => getSchemaTypename(schema2)?.endsWith(label2));
2203
+ const schema = (await this._getAllSchema()).find((s) => getSchemaTypename(s)?.endsWith(label2));
2671
2204
  if (!schema) {
2672
2205
  return [];
2673
2206
  }
2674
- const { objects } = await this._db.query(Filter.type(schema)).run();
2207
+ const { objects } = await this._db.query(Filter.schema(schema)).run();
2675
2208
  return objects.map(this._objectToNode);
2676
2209
  }
2677
2210
  async getRelationships({ label: label2 }) {
@@ -2691,14 +2224,14 @@ var EchoDataSource = class {
2691
2224
  if (!relationship) {
2692
2225
  return [];
2693
2226
  }
2694
- const { objects } = await this._db.query(Filter.type(relationship.schema)).run();
2227
+ const { objects } = await this._db.query(Filter.schema(relationship.schema)).run();
2695
2228
  return (await Promise.all(objects.map((object) => this._projectRefRelationship(object, relationship.property)))).flat();
2696
2229
  }
2697
2230
  async _getAllSchema() {
2698
2231
  return [
2699
2232
  ...await this._db.schemaRegistry.query().run(),
2700
2233
  // TODO(dmaretskyi): Remove once we can serialize recursive schema.
2701
- ...this._db.graph.schemaRegistry.schemas.filter((schema) => getSchemaTypename(schema)?.startsWith("example.org"))
2234
+ ...this._db.graph.schemaRegistry.schemas.filter((s) => getSchemaTypename(s)?.startsWith("example.org"))
2702
2235
  ].filter((schema) => getSchemaTypename(schema) !== StoredSchema.typename);
2703
2236
  }
2704
2237
  _objectToNode(object) {
@@ -2730,17 +2263,17 @@ var EchoDataSource = class {
2730
2263
  }));
2731
2264
  }
2732
2265
  };
2733
- _ts_decorate2([
2734
- log10.method(void 0, void 0, {
2735
- F: __dxlog_file10,
2736
- L: 18,
2266
+ _ts_decorate([
2267
+ log8.method(void 0, void 0, {
2268
+ F: __dxlog_file8,
2269
+ L: 17,
2737
2270
  S: void 0,
2738
2271
  C: (f, a) => f(...a)
2739
2272
  })
2740
2273
  ], EchoDataSource.prototype, "getNodes", null);
2741
- _ts_decorate2([
2742
- log10.method(void 0, void 0, {
2743
- F: __dxlog_file10,
2274
+ _ts_decorate([
2275
+ log8.method(void 0, void 0, {
2276
+ F: __dxlog_file8,
2744
2277
  L: 34,
2745
2278
  S: void 0,
2746
2279
  C: (f, a) => f(...a)
@@ -2762,17 +2295,13 @@ export {
2762
2295
  LLMModel,
2763
2296
  ToolTypes,
2764
2297
  GenerateRequest,
2765
- GenerateResponse,
2298
+ runLLM,
2766
2299
  isToolUse,
2767
2300
  runTools,
2768
- VersionPin,
2769
- AISession,
2770
- AIServiecOverloadedError,
2771
2301
  OllamaClient,
2772
2302
  StreamTransform,
2773
2303
  MixedStreamParser,
2774
2304
  mergeMessageBlock,
2775
- runLLM,
2776
2305
  parseCypherQuery,
2777
2306
  executeQueryPlan,
2778
2307
  executeQuery,
@@ -2783,4 +2312,4 @@ export {
2783
2312
  isReference,
2784
2313
  EchoDataSource
2785
2314
  };
2786
- //# sourceMappingURL=chunk-B4HAWUFW.mjs.map
2315
+ //# sourceMappingURL=chunk-PND5VR6D.mjs.map