@dxos/assistant 0.8.2-main.f11618f → 0.8.2-main.fbd8ed0

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 (157) hide show
  1. package/dist/lib/browser/{chunk-DEIGOD4U.mjs → chunk-B4HAWUFW.mjs} +710 -246
  2. package/dist/lib/browser/chunk-B4HAWUFW.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +4 -443
  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 +117 -80
  7. package/dist/lib/browser/testing/index.mjs.map +4 -4
  8. package/dist/lib/node/{chunk-7VH7NMIM.cjs → chunk-NOB7CIIC.cjs} +716 -259
  9. package/dist/lib/node/chunk-NOB7CIIC.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +34 -470
  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 +104 -69
  14. package/dist/lib/node/testing/index.cjs.map +4 -4
  15. package/dist/lib/node-esm/{chunk-7JI4MDGL.mjs → chunk-SAWYU3V5.mjs} +710 -246
  16. package/dist/lib/node-esm/chunk-SAWYU3V5.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +4 -443
  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 +117 -80
  21. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  22. package/dist/types/src/ai-service/parser.d.ts.map +1 -1
  23. package/dist/types/src/ai-service/stream.d.ts.map +1 -1
  24. package/dist/types/src/ai-service/testing/test-stream.d.ts.map +1 -1
  25. package/dist/types/src/ai-service/types.d.ts +133 -133
  26. package/dist/types/src/ai-service/types.d.ts.map +1 -1
  27. package/dist/types/src/ai-service/util/sse.d.ts.map +1 -1
  28. package/dist/types/src/{conversation → experimental/conversation}/conversation.d.ts +2 -18
  29. package/dist/types/src/experimental/conversation/conversation.d.ts.map +1 -0
  30. package/dist/types/src/experimental/conversation/conversation.test.d.ts.map +1 -0
  31. package/dist/types/src/experimental/conversation/index.d.ts.map +1 -0
  32. package/dist/types/src/{conversation → experimental/conversation}/types.d.ts +9 -9
  33. package/dist/types/src/experimental/conversation/types.d.ts.map +1 -0
  34. package/dist/types/src/experimental/cypher/ast.d.ts.map +1 -0
  35. package/dist/types/src/experimental/cypher/combinators.d.ts.map +1 -0
  36. package/dist/types/src/experimental/cypher/combinators.test.d.ts.map +1 -0
  37. package/dist/types/src/experimental/cypher/echo-data-source.d.ts.map +1 -0
  38. package/dist/types/src/experimental/cypher/execution-plan.d.ts.map +1 -0
  39. package/dist/types/src/experimental/cypher/execution-plan.test.d.ts.map +1 -0
  40. package/dist/types/src/experimental/cypher/index.d.ts.map +1 -0
  41. package/dist/types/src/experimental/cypher/parjs.d.ts.map +1 -0
  42. package/dist/types/src/experimental/cypher/parser.d.ts.map +1 -0
  43. package/dist/types/src/experimental/cypher/query-executor.d.ts.map +1 -0
  44. package/dist/types/src/experimental/cypher/query-executor.test.d.ts.map +1 -0
  45. package/dist/types/src/experimental/cypher/schema.d.ts.map +1 -0
  46. package/dist/types/src/experimental/cypher/schema.test.d.ts.map +1 -0
  47. package/dist/types/src/experimental/index.d.ts +3 -0
  48. package/dist/types/src/experimental/index.d.ts.map +1 -0
  49. package/dist/types/src/experimental/ner/ner.test.d.ts +2 -0
  50. package/dist/types/src/experimental/ner/ner.test.d.ts.map +1 -0
  51. package/dist/types/src/experimental/research/exa.d.ts +6 -0
  52. package/dist/types/src/experimental/research/exa.d.ts.map +1 -0
  53. package/dist/types/src/experimental/research/research.test.d.ts +2 -0
  54. package/dist/types/src/experimental/research/research.test.d.ts.map +1 -0
  55. package/dist/types/src/index.d.ts +1 -2
  56. package/dist/types/src/index.d.ts.map +1 -1
  57. package/dist/types/src/session/index.d.ts +1 -0
  58. package/dist/types/src/session/index.d.ts.map +1 -1
  59. package/dist/types/src/session/session.d.ts +4 -3
  60. package/dist/types/src/session/session.d.ts.map +1 -1
  61. package/dist/types/src/session/tools.d.ts +18 -0
  62. package/dist/types/src/session/tools.d.ts.map +1 -0
  63. package/dist/types/src/session/version-pin.d.ts +5 -0
  64. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  65. package/dist/types/src/testing/index.d.ts +1 -0
  66. package/dist/types/src/testing/index.d.ts.map +1 -1
  67. package/dist/types/src/testing/logger.d.ts +1 -1
  68. package/dist/types/src/testing/logger.d.ts.map +1 -1
  69. package/dist/types/src/testing/ollama.d.ts.map +1 -1
  70. package/dist/types/src/testing/printer.d.ts +6 -0
  71. package/dist/types/src/testing/printer.d.ts.map +1 -0
  72. package/dist/types/src/testing/query-promts.d.ts +4 -4
  73. package/dist/types/src/testing/query-promts.d.ts.map +1 -1
  74. package/dist/types/src/testing/test-data.d.ts +5 -5
  75. package/dist/types/src/testing/test-data.d.ts.map +1 -1
  76. package/dist/types/src/testing/test-schema.d.ts +25 -23
  77. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  78. package/dist/types/src/util/trim.d.ts.map +1 -1
  79. package/dist/types/tsconfig.tsbuildinfo +1 -1
  80. package/package.json +20 -14
  81. package/src/ai-service/client.test.ts +5 -5
  82. package/src/ai-service/ollama-client.ts +8 -8
  83. package/src/ai-service/types.ts +14 -14
  84. package/src/bin/chat.ts +1 -1
  85. package/src/{conversation → experimental/conversation}/conversation.test.ts +5 -5
  86. package/src/{conversation → experimental/conversation}/conversation.ts +3 -112
  87. package/src/{conversation → experimental/conversation}/types.ts +10 -10
  88. package/src/{cypher → experimental/cypher}/echo-data-source.ts +11 -10
  89. package/src/{cypher → experimental/cypher}/query-executor.test.ts +1 -1
  90. package/src/{cypher → experimental/cypher}/schema.test.ts +1 -1
  91. package/src/{cypher → experimental/cypher}/schema.ts +1 -1
  92. package/src/experimental/index.ts +6 -0
  93. package/src/experimental/ner/ner.test.ts +163 -0
  94. package/src/experimental/research/exa.ts +38 -0
  95. package/src/experimental/research/instructions.tpl +64 -0
  96. package/src/experimental/research/research.test.ts +263 -0
  97. package/src/index.ts +3 -2
  98. package/src/session/index.ts +1 -0
  99. package/src/session/session.test.ts +28 -57
  100. package/src/session/session.ts +26 -16
  101. package/src/session/tools.ts +117 -0
  102. package/src/session/version-pin.ts +6 -0
  103. package/src/testing/artifacts.test.ts +22 -22
  104. package/src/testing/index.ts +1 -0
  105. package/src/testing/logger.ts +1 -1
  106. package/src/testing/printer.ts +41 -0
  107. package/src/testing/query-promts.ts +6 -6
  108. package/src/testing/query.test.ts +1 -2
  109. package/src/testing/test-data.ts +58 -51
  110. package/src/testing/test-schema.ts +27 -28
  111. package/src/typedefs.d.ts +8 -0
  112. package/dist/lib/browser/chunk-DEIGOD4U.mjs.map +0 -7
  113. package/dist/lib/node/chunk-7VH7NMIM.cjs.map +0 -7
  114. package/dist/lib/node-esm/chunk-7JI4MDGL.mjs.map +0 -7
  115. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  116. package/dist/types/src/conversation/conversation.test.d.ts.map +0 -1
  117. package/dist/types/src/conversation/index.d.ts.map +0 -1
  118. package/dist/types/src/conversation/types.d.ts.map +0 -1
  119. package/dist/types/src/cypher/ast.d.ts.map +0 -1
  120. package/dist/types/src/cypher/combinators.d.ts.map +0 -1
  121. package/dist/types/src/cypher/combinators.test.d.ts.map +0 -1
  122. package/dist/types/src/cypher/echo-data-source.d.ts.map +0 -1
  123. package/dist/types/src/cypher/execution-plan.d.ts.map +0 -1
  124. package/dist/types/src/cypher/execution-plan.test.d.ts.map +0 -1
  125. package/dist/types/src/cypher/index.d.ts.map +0 -1
  126. package/dist/types/src/cypher/parjs.d.ts.map +0 -1
  127. package/dist/types/src/cypher/parser.d.ts.map +0 -1
  128. package/dist/types/src/cypher/query-executor.d.ts.map +0 -1
  129. package/dist/types/src/cypher/query-executor.test.d.ts.map +0 -1
  130. package/dist/types/src/cypher/schema.d.ts.map +0 -1
  131. package/dist/types/src/cypher/schema.test.d.ts.map +0 -1
  132. /package/dist/types/src/{conversation → experimental/conversation}/conversation.test.d.ts +0 -0
  133. /package/dist/types/src/{conversation → experimental/conversation}/index.d.ts +0 -0
  134. /package/dist/types/src/{cypher → experimental/cypher}/ast.d.ts +0 -0
  135. /package/dist/types/src/{cypher → experimental/cypher}/combinators.d.ts +0 -0
  136. /package/dist/types/src/{cypher → experimental/cypher}/combinators.test.d.ts +0 -0
  137. /package/dist/types/src/{cypher → experimental/cypher}/echo-data-source.d.ts +0 -0
  138. /package/dist/types/src/{cypher → experimental/cypher}/execution-plan.d.ts +0 -0
  139. /package/dist/types/src/{cypher → experimental/cypher}/execution-plan.test.d.ts +0 -0
  140. /package/dist/types/src/{cypher → experimental/cypher}/index.d.ts +0 -0
  141. /package/dist/types/src/{cypher → experimental/cypher}/parjs.d.ts +0 -0
  142. /package/dist/types/src/{cypher → experimental/cypher}/parser.d.ts +0 -0
  143. /package/dist/types/src/{cypher → experimental/cypher}/query-executor.d.ts +0 -0
  144. /package/dist/types/src/{cypher → experimental/cypher}/query-executor.test.d.ts +0 -0
  145. /package/dist/types/src/{cypher → experimental/cypher}/schema.d.ts +0 -0
  146. /package/dist/types/src/{cypher → experimental/cypher}/schema.test.d.ts +0 -0
  147. /package/src/{conversation → experimental/conversation}/index.ts +0 -0
  148. /package/src/{cypher → experimental/cypher}/ast.ts +0 -0
  149. /package/src/{cypher → experimental/cypher}/combinators.test.ts +0 -0
  150. /package/src/{cypher → experimental/cypher}/combinators.ts +0 -0
  151. /package/src/{cypher → experimental/cypher}/execution-plan.test.ts +0 -0
  152. /package/src/{cypher → experimental/cypher}/execution-plan.ts +0 -0
  153. /package/src/{cypher → experimental/cypher}/index.ts +0 -0
  154. /package/src/{cypher → experimental/cypher}/parjs-shims.d.ts +0 -0
  155. /package/src/{cypher → experimental/cypher}/parjs.ts +0 -0
  156. /package/src/{cypher → experimental/cypher}/parser.ts +0 -0
  157. /package/src/{cypher → experimental/cypher}/query-executor.ts +0 -0
@@ -493,223 +493,57 @@ function* emitMessageAsEvents(message) {
493
493
  }
494
494
 
495
495
  // packages/experimental/assistant/src/ai-service/types.ts
496
- import { Schema as S } from "effect";
496
+ import { Schema } from "effect";
497
497
  import { Tool, Message } from "@dxos/artifact";
498
498
  var createArtifactElement = (id) => `<artifact id=${id} />`;
499
- var LLMModel = S.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
499
+ var LLMModel = Schema.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
500
500
  var ToolTypes = Object.freeze({
501
501
  // TODO(dmaretskyi): Not implemented yet.
502
502
  // DatabaseQuery: 'database_query',
503
503
  TextToImage: "text_to_image"
504
504
  });
505
- var GenerateRequest = S.Struct({
505
+ var GenerateRequest = Schema.Struct({
506
506
  /**
507
507
  * Preferred model or system default.
508
508
  */
509
- model: S.optional(LLMModel),
509
+ model: Schema.optional(LLMModel),
510
510
  /**
511
511
  * Tools available for the LLM.
512
512
  */
513
- tools: S.optional(S.Array(Tool).pipe(S.mutable)),
513
+ tools: Schema.optional(Schema.Array(Tool).pipe(Schema.mutable)),
514
514
  /**
515
515
  * System instructions to the LLM.
516
516
  */
517
- systemPrompt: S.optional(S.String),
517
+ systemPrompt: Schema.optional(Schema.String),
518
518
  /**
519
519
  * History of messages to include in the context window.
520
520
  */
521
521
  // TODO(burdon): Rename messages.
522
- history: S.optional(S.Array(Message)),
522
+ history: Schema.optional(Schema.Array(Message)),
523
523
  /**
524
524
  * Current request.
525
525
  */
526
- prompt: S.optional(Message)
526
+ prompt: Schema.optional(Message)
527
527
  });
528
- var GenerateResponse = S.Struct({
529
- messages: S.Array(Message),
528
+ var GenerateResponse = Schema.Struct({
529
+ messages: Schema.Array(Message),
530
530
  /**
531
531
  * Number of tokens used in the response.
532
532
  */
533
- tokenCount: S.optional(S.Number)
533
+ tokenCount: Schema.optional(Schema.Number)
534
534
  });
535
535
 
536
- // packages/experimental/assistant/src/conversation/conversation.ts
537
- import { ToolResult, Message as Message2 } from "@dxos/artifact";
538
- import { create, ObjectId } from "@dxos/echo-schema";
536
+ // packages/experimental/assistant/src/session/tools.ts
537
+ import { Message as Message2, ToolResult } from "@dxos/artifact";
538
+ import { create } from "@dxos/echo-schema";
539
539
  import { invariant as invariant2 } from "@dxos/invariant";
540
540
  import { log as log3 } from "@dxos/log";
541
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
542
- var runLLM = async (params) => {
543
- let conversationResult = null;
544
- const history = params.history ?? [];
545
- const generate = async () => {
546
- log3("llm generate", {
547
- tools: params.tools
548
- }, {
549
- F: __dxlog_file3,
550
- L: 44,
551
- S: void 0,
552
- C: (f, a) => f(...a)
553
- });
554
- const beginTs = Date.now();
555
- const stream = await params.client.execStream({
556
- model: params.model,
557
- history,
558
- systemPrompt: params.system,
559
- tools: params.tools
560
- });
561
- for await (const event of stream) {
562
- params.logger?.(event);
563
- }
564
- await stream.complete();
565
- const messages = [];
566
- const message = messages.at(-1);
567
- invariant2(message, void 0, {
568
- F: __dxlog_file3,
569
- L: 61,
570
- S: void 0,
571
- A: [
572
- "message",
573
- ""
574
- ]
575
- });
576
- log3("llm result", {
577
- time: Date.now() - beginTs,
578
- message
579
- }, {
580
- F: __dxlog_file3,
581
- L: 63,
582
- S: void 0,
583
- C: (f, a) => f(...a)
584
- });
585
- invariant2(message, void 0, {
586
- F: __dxlog_file3,
587
- L: 64,
588
- S: void 0,
589
- A: [
590
- "message",
591
- ""
592
- ]
593
- });
594
- params.logger?.({
595
- type: "message",
596
- message
597
- });
598
- history.push(...messages.map((message2) => ({
599
- ...message2,
600
- content: message2.content.filter((block) => block.type !== "image")
601
- })));
602
- const isToolUse2 = message.content.at(-1)?.type === "tool_use";
603
- if (isToolUse2) {
604
- const toolCalls = message.content.filter((block) => block.type === "tool_use");
605
- invariant2(toolCalls.length === 1, void 0, {
606
- F: __dxlog_file3,
607
- L: 75,
608
- S: void 0,
609
- A: [
610
- "toolCalls.length === 1",
611
- ""
612
- ]
613
- });
614
- const toolCall = toolCalls[0];
615
- const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
616
- if (!tool) {
617
- throw new Error(`Tool not found: ${toolCall.name}`);
618
- }
619
- invariant2(tool.execute, void 0, {
620
- F: __dxlog_file3,
621
- L: 82,
622
- S: void 0,
623
- A: [
624
- "tool.execute",
625
- ""
626
- ]
627
- });
628
- const toolResult = await tool.execute(toolCall.input);
629
- switch (toolResult.kind) {
630
- case "error": {
631
- log3.warn("tool error", {
632
- message: toolResult.message
633
- }, {
634
- F: __dxlog_file3,
635
- L: 86,
636
- S: void 0,
637
- C: (f, a) => f(...a)
638
- });
639
- const resultMessage = {
640
- id: ObjectId.random(),
641
- role: "user",
642
- content: [
643
- {
644
- type: "tool_result",
645
- toolUseId: toolCall.id,
646
- content: toolResult.message,
647
- isError: true
648
- }
649
- ]
650
- };
651
- params.logger?.({
652
- type: "message",
653
- message: resultMessage
654
- });
655
- history.push(resultMessage);
656
- return true;
657
- }
658
- case "success": {
659
- log3("tool success", {
660
- result: toolResult.result
661
- }, {
662
- F: __dxlog_file3,
663
- L: 106,
664
- S: void 0,
665
- C: (f, a) => f(...a)
666
- });
667
- const resultMessage = {
668
- id: ObjectId.random(),
669
- role: "user",
670
- content: [
671
- {
672
- type: "tool_result",
673
- toolUseId: toolCall.id,
674
- content: JSON.stringify(toolResult.result)
675
- }
676
- ]
677
- };
678
- params.logger?.({
679
- type: "message",
680
- message: resultMessage
681
- });
682
- history.push(resultMessage);
683
- return true;
684
- }
685
- case "break": {
686
- log3("tool break", {
687
- result: toolResult.result
688
- }, {
689
- F: __dxlog_file3,
690
- L: 125,
691
- S: void 0,
692
- C: (f, a) => f(...a)
693
- });
694
- conversationResult = toolResult.result;
695
- return false;
696
- }
697
- }
698
- }
699
- return false;
700
- };
701
- while (await generate()) {
702
- }
703
- return {
704
- result: conversationResult,
705
- history
706
- };
707
- };
541
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/session/tools.ts";
708
542
  var isToolUse = (message, { onlyToolNames } = {}) => {
709
543
  const block = message.content.at(-1);
710
544
  invariant2(block, void 0, {
711
545
  F: __dxlog_file3,
712
- L: 146,
546
+ L: 12,
713
547
  S: void 0,
714
548
  A: [
715
549
  "block",
@@ -722,7 +556,7 @@ var runTools = async ({ message, tools, extensions }) => {
722
556
  const toolCalls = message.content.filter((block) => block.type === "tool_use");
723
557
  invariant2(toolCalls.length === 1, void 0, {
724
558
  F: __dxlog_file3,
725
- L: 168,
559
+ L: 34,
726
560
  S: void 0,
727
561
  A: [
728
562
  "toolCalls.length === 1",
@@ -752,7 +586,7 @@ var runTools = async ({ message, tools, extensions }) => {
752
586
  try {
753
587
  invariant2(tool.execute, void 0, {
754
588
  F: __dxlog_file3,
755
- L: 193,
589
+ L: 59,
756
590
  S: void 0,
757
591
  A: [
758
592
  "tool.execute",
@@ -767,7 +601,7 @@ var runTools = async ({ message, tools, extensions }) => {
767
601
  error
768
602
  }, {
769
603
  F: __dxlog_file3,
770
- L: 196,
604
+ L: 62,
771
605
  S: void 0,
772
606
  C: (f, a) => f(...a)
773
607
  });
@@ -779,7 +613,7 @@ var runTools = async ({ message, tools, extensions }) => {
779
613
  message: toolResult.message
780
614
  }, {
781
615
  F: __dxlog_file3,
782
- L: 202,
616
+ L: 68,
783
617
  S: void 0,
784
618
  C: (f, a) => f(...a)
785
619
  });
@@ -804,7 +638,7 @@ var runTools = async ({ message, tools, extensions }) => {
804
638
  result: toolResult.result
805
639
  }, {
806
640
  F: __dxlog_file3,
807
- L: 223,
641
+ L: 89,
808
642
  S: void 0,
809
643
  C: (f, a) => f(...a)
810
644
  });
@@ -829,7 +663,7 @@ var runTools = async ({ message, tools, extensions }) => {
829
663
  result: toolResult.result
830
664
  }, {
831
665
  F: __dxlog_file3,
832
- L: 244,
666
+ L: 110,
833
667
  S: void 0,
834
668
  C: (f, a) => f(...a)
835
669
  });
@@ -841,8 +675,47 @@ var runTools = async ({ message, tools, extensions }) => {
841
675
  }
842
676
  };
843
677
 
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
+
844
717
  // packages/experimental/assistant/src/ai-service/ollama-client.ts
845
- import { Schema as S2 } from "effect";
718
+ import { Schema as Schema3 } from "effect";
846
719
  import { defineTool, ToolResult as ToolResult2 } from "@dxos/artifact";
847
720
  import { ObjectId as ObjectId2 } from "@dxos/echo-schema";
848
721
  import { invariant as invariant3 } from "@dxos/invariant";
@@ -1148,8 +1021,8 @@ var WELL_KNOWN_TOOLS = {
1148
1021
  [ToolTypes.TextToImage]: defineTool("system", {
1149
1022
  name: "text-to-image",
1150
1023
  description: "Generate an image from a text prompt",
1151
- schema: S2.Struct({
1152
- prompt: S2.String.annotations({
1024
+ schema: Schema3.Struct({
1025
+ prompt: Schema3.String.annotations({
1153
1026
  description: "The text prompt describing the image to generate"
1154
1027
  })
1155
1028
  }),
@@ -1192,12 +1065,12 @@ var OllamaDecoderStream = class extends TransformStream {
1192
1065
  this._buffer = "";
1193
1066
  }
1194
1067
  };
1195
- var ModelDoesNotSupportToolsError = class extends S2.TaggedError()("ModelDoesNotSupportToolsError", {
1196
- model: S2.String
1068
+ var ModelDoesNotSupportToolsError = class extends Schema3.TaggedError()("ModelDoesNotSupportToolsError", {
1069
+ model: Schema3.String
1197
1070
  }) {
1198
1071
  };
1199
- var ModelNotFoundError = class extends S2.TaggedError()("ModelNotFoundError", {
1200
- model: S2.String
1072
+ var ModelNotFoundError = class extends Schema3.TaggedError()("ModelNotFoundError", {
1073
+ model: Schema3.String
1201
1074
  }) {
1202
1075
  };
1203
1076
  var parseOllamaError = (error) => {
@@ -1227,6 +1100,14 @@ var parseErrorResponse2 = async (response) => {
1227
1100
  throw new Error(`Ollama API error: ${response.status} ${response.statusText} ${await response.text()}`);
1228
1101
  };
1229
1102
 
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
+
1230
1111
  // packages/experimental/assistant/src/ai-service/transform.ts
1231
1112
  import * as P from "parsimmon";
1232
1113
  var textChunk = P.regexp(/[^<]+/).map((content) => ({
@@ -1317,12 +1198,6 @@ var StreamTransform = class {
1317
1198
  };
1318
1199
 
1319
1200
  // packages/experimental/assistant/src/ai-service/parser.ts
1320
- import { Message as Message3 } from "@dxos/artifact";
1321
- import { Event } from "@dxos/async";
1322
- import { create as create2 } from "@dxos/echo-schema";
1323
- import { invariant as invariant4 } from "@dxos/invariant";
1324
- import { log as log5 } from "@dxos/log";
1325
- import { isNotFalsy, safeParseJson } from "@dxos/util";
1326
1201
  var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
1327
1202
  var MixedStreamParser = class {
1328
1203
  constructor() {
@@ -1734,10 +1609,596 @@ var mergeMessageBlock = (contentBlock, streamBlock) => {
1734
1609
  }
1735
1610
  };
1736
1611
 
1737
- // packages/experimental/assistant/src/cypher/parjs.ts
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
1738
2199
  import { parjsCombinators, parjs } from "#parjs";
1739
2200
 
1740
- // packages/experimental/assistant/src/cypher/combinators.ts
2201
+ // packages/experimental/assistant/src/experimental/cypher/combinators.ts
1741
2202
  var ws = parjs.whitespace();
1742
2203
  var nothing = parjs.string("").pipe(parjsCombinators.map(() => null));
1743
2204
  var optional = (parser) => parser.pipe(parjsCombinators.recover(() => ({
@@ -1811,7 +2272,7 @@ var cypherQuery = ws.pipe(parjsCombinators.then(matchClause, ws, optional(whereC
1811
2272
  returnClause: returnClause2
1812
2273
  })));
1813
2274
 
1814
- // packages/experimental/assistant/src/cypher/parser.ts
2275
+ // packages/experimental/assistant/src/experimental/cypher/parser.ts
1815
2276
  var parseCypherQuery = (query) => {
1816
2277
  const parseResult = cypherQuery.parse(query);
1817
2278
  if (parseResult.isOk) {
@@ -1823,14 +2284,14 @@ var parseCypherQuery = (query) => {
1823
2284
  }
1824
2285
  };
1825
2286
 
1826
- // packages/experimental/assistant/src/cypher/query-executor.ts
1827
- import { log as log7 } from "@dxos/log";
2287
+ // packages/experimental/assistant/src/experimental/cypher/query-executor.ts
2288
+ import { log as log9 } from "@dxos/log";
1828
2289
  import { mapValues } from "@dxos/util";
1829
2290
 
1830
- // packages/experimental/assistant/src/cypher/execution-plan.ts
1831
- import { invariant as invariant5 } from "@dxos/invariant";
1832
- import { log as log6 } from "@dxos/log";
1833
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/execution-plan.ts";
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";
1834
2295
  var createExecutionPlan = (query) => {
1835
2296
  return new ExecutionPlanBuilder().build(query);
1836
2297
  };
@@ -1842,10 +2303,10 @@ var ExecutionPlanBuilder = class {
1842
2303
  return `anon_${this.counter++}`;
1843
2304
  }
1844
2305
  build(query) {
1845
- log6("Creating execution plan for query", {
2306
+ log8("Creating execution plan for query", {
1846
2307
  query
1847
2308
  }, {
1848
- F: __dxlog_file6,
2309
+ F: __dxlog_file8,
1849
2310
  L: 76,
1850
2311
  S: this,
1851
2312
  C: (f, a) => f(...a)
@@ -1862,8 +2323,8 @@ var ExecutionPlanBuilder = class {
1862
2323
  };
1863
2324
  }
1864
2325
  createStepsForMatchClause(match) {
1865
- invariant5(match.pattern.segments.length > 0, void 0, {
1866
- F: __dxlog_file6,
2326
+ invariant7(match.pattern.segments.length > 0, void 0, {
2327
+ F: __dxlog_file8,
1867
2328
  L: 90,
1868
2329
  S: this,
1869
2330
  A: [
@@ -1872,8 +2333,8 @@ var ExecutionPlanBuilder = class {
1872
2333
  ]
1873
2334
  });
1874
2335
  const firstSegment = match.pattern.segments[0];
1875
- invariant5(firstSegment.astKind === "NodePattern", void 0, {
1876
- F: __dxlog_file6,
2336
+ invariant7(firstSegment.astKind === "NodePattern", void 0, {
2337
+ F: __dxlog_file8,
1877
2338
  L: 92,
1878
2339
  S: this,
1879
2340
  A: [
@@ -1900,8 +2361,8 @@ var ExecutionPlanBuilder = class {
1900
2361
  }
1901
2362
  for (let i = 1; i < match.pattern.segments.length; i++) {
1902
2363
  const relationshipSegment = match.pattern.segments[i++];
1903
- invariant5(relationshipSegment.astKind === "RelationshipPattern", void 0, {
1904
- F: __dxlog_file6,
2364
+ invariant7(relationshipSegment.astKind === "RelationshipPattern", void 0, {
2365
+ F: __dxlog_file8,
1905
2366
  L: 115,
1906
2367
  S: this,
1907
2368
  A: [
@@ -1910,8 +2371,8 @@ var ExecutionPlanBuilder = class {
1910
2371
  ]
1911
2372
  });
1912
2373
  const nodeSegment = match.pattern.segments[i];
1913
- invariant5(nodeSegment.astKind === "NodePattern", void 0, {
1914
- F: __dxlog_file6,
2374
+ invariant7(nodeSegment.astKind === "NodePattern", void 0, {
2375
+ F: __dxlog_file8,
1915
2376
  L: 117,
1916
2377
  S: this,
1917
2378
  A: [
@@ -1993,16 +2454,16 @@ var getDirection = (left, right) => {
1993
2454
  throw new Error(`Invalid direction: ()${left}[]${right}()`);
1994
2455
  };
1995
2456
 
1996
- // packages/experimental/assistant/src/cypher/query-executor.ts
1997
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/query-executor.ts";
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";
1998
2459
  var executeQueryPlan = async (dataSource, plan) => {
1999
2460
  let buffer = [];
2000
2461
  const results = [];
2001
2462
  for (const step of plan.steps) {
2002
- log7("begin step", {
2463
+ log9("begin step", {
2003
2464
  step
2004
2465
  }, {
2005
- F: __dxlog_file7,
2466
+ F: __dxlog_file9,
2006
2467
  L: 41,
2007
2468
  S: void 0,
2008
2469
  C: (f, a) => f(...a)
@@ -2088,11 +2549,11 @@ var executeQueryPlan = async (dataSource, plan) => {
2088
2549
  break;
2089
2550
  }
2090
2551
  }
2091
- log7("finish step", {
2552
+ log9("finish step", {
2092
2553
  buffer: buffer.map((tuple) => mapValues(tuple, (x) => x.id)),
2093
2554
  results
2094
2555
  }, {
2095
- F: __dxlog_file7,
2556
+ F: __dxlog_file9,
2096
2557
  L: 118,
2097
2558
  S: void 0,
2098
2559
  C: (f, a) => f(...a)
@@ -2119,7 +2580,7 @@ var jsonPathGet = (obj, path) => {
2119
2580
  return result;
2120
2581
  };
2121
2582
 
2122
- // packages/experimental/assistant/src/cypher/schema.ts
2583
+ // packages/experimental/assistant/src/experimental/cypher/schema.ts
2123
2584
  import { raise } from "@dxos/debug";
2124
2585
  import { JSON_SCHEMA_ECHO_REF_ID } from "@dxos/echo-schema";
2125
2586
 
@@ -2131,7 +2592,7 @@ var trim = (strings, ...values) => {
2131
2592
  return combined.replace("/^\n/", "").replace(/\n$/, "");
2132
2593
  };
2133
2594
 
2134
- // packages/experimental/assistant/src/cypher/schema.ts
2595
+ // packages/experimental/assistant/src/experimental/cypher/schema.ts
2135
2596
  var formatJsonSchemaForLLM = (schemaTypes) => {
2136
2597
  return schemaTypes.flatMap((schema) => [
2137
2598
  formatNodeSchema(schema),
@@ -2185,18 +2646,18 @@ var formatInferredRelationshipLabel = (typename, property2) => {
2185
2646
  };
2186
2647
  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;
2187
2648
 
2188
- // packages/experimental/assistant/src/cypher/echo-data-source.ts
2649
+ // packages/experimental/assistant/src/experimental/cypher/echo-data-source.ts
2189
2650
  import { raise as raise2 } from "@dxos/debug";
2190
2651
  import { Filter } from "@dxos/echo-db";
2191
2652
  import { getSchema, getSchemaTypename, StoredSchema, toJsonSchema } from "@dxos/echo-schema";
2192
- import { log as log8 } from "@dxos/log";
2193
- function _ts_decorate(decorators, target, key, desc) {
2653
+ import { log as log10 } from "@dxos/log";
2654
+ function _ts_decorate2(decorators, target, key, desc) {
2194
2655
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2195
2656
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2196
2657
  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;
2197
2658
  return c > 3 && r && Object.defineProperty(target, key, r), r;
2198
2659
  }
2199
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/echo-data-source.ts";
2660
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/experimental/cypher/echo-data-source.ts";
2200
2661
  var EchoDataSource = class {
2201
2662
  constructor(_db) {
2202
2663
  this._db = _db;
@@ -2206,11 +2667,11 @@ var EchoDataSource = class {
2206
2667
  const { objects: objects2 } = await this._db.query().run();
2207
2668
  return objects2.map(this._objectToNode);
2208
2669
  }
2209
- const schema = (await this._getAllSchema()).find((s) => getSchemaTypename(s)?.endsWith(label2));
2670
+ const schema = (await this._getAllSchema()).find((schema2) => getSchemaTypename(schema2)?.endsWith(label2));
2210
2671
  if (!schema) {
2211
2672
  return [];
2212
2673
  }
2213
- const { objects } = await this._db.query(Filter.schema(schema)).run();
2674
+ const { objects } = await this._db.query(Filter.type(schema)).run();
2214
2675
  return objects.map(this._objectToNode);
2215
2676
  }
2216
2677
  async getRelationships({ label: label2 }) {
@@ -2230,14 +2691,14 @@ var EchoDataSource = class {
2230
2691
  if (!relationship) {
2231
2692
  return [];
2232
2693
  }
2233
- const { objects } = await this._db.query(Filter.schema(relationship.schema)).run();
2694
+ const { objects } = await this._db.query(Filter.type(relationship.schema)).run();
2234
2695
  return (await Promise.all(objects.map((object) => this._projectRefRelationship(object, relationship.property)))).flat();
2235
2696
  }
2236
2697
  async _getAllSchema() {
2237
2698
  return [
2238
2699
  ...await this._db.schemaRegistry.query().run(),
2239
2700
  // TODO(dmaretskyi): Remove once we can serialize recursive schema.
2240
- ...this._db.graph.schemaRegistry.schemas.filter((s) => getSchemaTypename(s)?.startsWith("example.org"))
2701
+ ...this._db.graph.schemaRegistry.schemas.filter((schema) => getSchemaTypename(schema)?.startsWith("example.org"))
2241
2702
  ].filter((schema) => getSchemaTypename(schema) !== StoredSchema.typename);
2242
2703
  }
2243
2704
  _objectToNode(object) {
@@ -2269,18 +2730,18 @@ var EchoDataSource = class {
2269
2730
  }));
2270
2731
  }
2271
2732
  };
2272
- _ts_decorate([
2273
- log8.method(void 0, void 0, {
2274
- F: __dxlog_file8,
2275
- L: 16,
2733
+ _ts_decorate2([
2734
+ log10.method(void 0, void 0, {
2735
+ F: __dxlog_file10,
2736
+ L: 18,
2276
2737
  S: void 0,
2277
2738
  C: (f, a) => f(...a)
2278
2739
  })
2279
2740
  ], EchoDataSource.prototype, "getNodes", null);
2280
- _ts_decorate([
2281
- log8.method(void 0, void 0, {
2282
- F: __dxlog_file8,
2283
- L: 33,
2741
+ _ts_decorate2([
2742
+ log10.method(void 0, void 0, {
2743
+ F: __dxlog_file10,
2744
+ L: 34,
2284
2745
  S: void 0,
2285
2746
  C: (f, a) => f(...a)
2286
2747
  })
@@ -2302,13 +2763,16 @@ export {
2302
2763
  ToolTypes,
2303
2764
  GenerateRequest,
2304
2765
  GenerateResponse,
2305
- runLLM,
2306
2766
  isToolUse,
2307
2767
  runTools,
2768
+ VersionPin,
2769
+ AISession,
2770
+ AIServiecOverloadedError,
2308
2771
  OllamaClient,
2309
2772
  StreamTransform,
2310
2773
  MixedStreamParser,
2311
2774
  mergeMessageBlock,
2775
+ runLLM,
2312
2776
  parseCypherQuery,
2313
2777
  executeQueryPlan,
2314
2778
  executeQuery,
@@ -2319,4 +2783,4 @@ export {
2319
2783
  isReference,
2320
2784
  EchoDataSource
2321
2785
  };
2322
- //# sourceMappingURL=chunk-DEIGOD4U.mjs.map
2786
+ //# sourceMappingURL=chunk-B4HAWUFW.mjs.map