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