@dxos/assistant 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef

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 (147) hide show
  1. package/README.md +1 -0
  2. package/dist/lib/neutral/extraction/index.mjs +202 -0
  3. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  4. package/dist/lib/neutral/index.mjs +1731 -0
  5. package/dist/lib/neutral/index.mjs.map +7 -0
  6. package/dist/lib/neutral/meta.json +1 -0
  7. package/dist/types/src/conversation/context.d.ts +120 -64
  8. package/dist/types/src/conversation/context.d.ts.map +1 -1
  9. package/dist/types/src/conversation/context.test.d.ts +2 -0
  10. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  11. package/dist/types/src/conversation/index.d.ts +1 -1
  12. package/dist/types/src/conversation/index.d.ts.map +1 -1
  13. package/dist/types/src/conversation/session.d.ts +78 -0
  14. package/dist/types/src/conversation/session.d.ts.map +1 -0
  15. package/dist/types/src/diff.d.ts +3 -1
  16. package/dist/types/src/diff.d.ts.map +1 -1
  17. package/dist/types/src/errors.d.ts +32 -40
  18. package/dist/types/src/errors.d.ts.map +1 -1
  19. package/dist/types/src/execution-graph.d.ts +5 -2
  20. package/dist/types/src/execution-graph.d.ts.map +1 -1
  21. package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
  22. package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
  23. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  24. package/dist/types/src/extraction/extraction.d.ts +1066 -307
  25. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  26. package/dist/types/src/extraction/index.d.ts +0 -1
  27. package/dist/types/src/extraction/index.d.ts.map +1 -1
  28. package/dist/types/src/extraction/quotes.d.ts +3 -3
  29. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  30. package/dist/types/src/functions/services.d.ts +15 -16
  31. package/dist/types/src/functions/services.d.ts.map +1 -1
  32. package/dist/types/src/index.d.ts +1 -0
  33. package/dist/types/src/index.d.ts.map +1 -1
  34. package/dist/types/src/session/artifact-diff.d.ts +2 -2
  35. package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
  36. package/dist/types/src/session/error-handling.d.ts +2 -2
  37. package/dist/types/src/session/error-handling.d.ts.map +1 -1
  38. package/dist/types/src/session/format.d.ts +8 -177
  39. package/dist/types/src/session/format.d.ts.map +1 -1
  40. package/dist/types/src/session/index.d.ts +2 -1
  41. package/dist/types/src/session/index.d.ts.map +1 -1
  42. package/dist/types/src/session/observer.d.ts +7 -6
  43. package/dist/types/src/session/observer.d.ts.map +1 -1
  44. package/dist/types/src/session/request.d.ts +104 -0
  45. package/dist/types/src/session/request.d.ts.map +1 -0
  46. package/dist/types/src/session/toolkit.d.ts +11 -9
  47. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  48. package/dist/types/src/session/version-pin.d.ts +4 -4
  49. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  50. package/dist/types/src/templates/system.d.ts +1 -2
  51. package/dist/types/src/templates/system.d.ts.map +1 -1
  52. package/dist/types/src/tracing.d.ts +233 -0
  53. package/dist/types/src/tracing.d.ts.map +1 -0
  54. package/dist/types/src/types/artifact.d.ts +28 -0
  55. package/dist/types/src/types/artifact.d.ts.map +1 -0
  56. package/dist/types/src/types/index.d.ts +2 -0
  57. package/dist/types/src/types/index.d.ts.map +1 -0
  58. package/dist/types/tsconfig.tsbuildinfo +1 -1
  59. package/package.json +35 -47
  60. package/src/conversation/context.test.ts +52 -0
  61. package/src/conversation/context.ts +324 -75
  62. package/src/conversation/index.ts +1 -1
  63. package/src/conversation/session.ts +334 -0
  64. package/src/diff.test.ts +37 -6
  65. package/src/diff.ts +18 -4
  66. package/src/errors.ts +2 -2
  67. package/src/execution-graph.test.ts +47 -45
  68. package/src/execution-graph.ts +41 -22
  69. package/src/extraction/extraction-ner-function/index.ts +0 -1
  70. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
  71. package/src/extraction/extraction.ts +44 -15
  72. package/src/extraction/index.ts +0 -1
  73. package/src/extraction/quotes.ts +5 -6
  74. package/src/functions/services.ts +126 -65
  75. package/src/functions.conversations.json +1 -0
  76. package/src/index.ts +1 -0
  77. package/src/session/artifact-diff.ts +2 -2
  78. package/src/session/error-handling.ts +4 -3
  79. package/src/session/format.ts +34 -26
  80. package/src/session/index.ts +2 -1
  81. package/src/session/observer.ts +8 -4
  82. package/src/session/request.conversations.json +1 -0
  83. package/src/session/request.ts +342 -0
  84. package/src/session/toolkit.ts +29 -28
  85. package/src/session/version-pin.ts +7 -7
  86. package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
  87. package/src/templates/{system.tpl → instructions/system.tpl} +50 -2
  88. package/src/templates/system.ts +9 -8
  89. package/src/tracing.ts +57 -0
  90. package/src/{types.ts → types/artifact.ts} +44 -7
  91. package/src/types/index.ts +5 -0
  92. package/dist/lib/browser/chunk-ILL2CN3G.mjs +0 -374
  93. package/dist/lib/browser/chunk-ILL2CN3G.mjs.map +0 -7
  94. package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
  95. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
  96. package/dist/lib/browser/extraction/index.mjs +0 -272
  97. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  98. package/dist/lib/browser/index.mjs +0 -861
  99. package/dist/lib/browser/index.mjs.map +0 -7
  100. package/dist/lib/browser/meta.json +0 -1
  101. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
  102. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +0 -7
  103. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
  104. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
  105. package/dist/lib/node-esm/chunk-GVKA7VL3.mjs +0 -376
  106. package/dist/lib/node-esm/chunk-GVKA7VL3.mjs.map +0 -7
  107. package/dist/lib/node-esm/extraction/index.mjs +0 -273
  108. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  109. package/dist/lib/node-esm/index.mjs +0 -862
  110. package/dist/lib/node-esm/index.mjs.map +0 -7
  111. package/dist/lib/node-esm/meta.json +0 -1
  112. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
  113. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
  114. package/dist/types/src/conversation/conversation.d.ts +0 -41
  115. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  116. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
  117. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
  118. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
  119. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
  120. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
  121. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
  122. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -341
  123. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
  124. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
  125. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
  126. package/dist/types/src/session/format.test.d.ts +0 -2
  127. package/dist/types/src/session/format.test.d.ts.map +0 -1
  128. package/dist/types/src/session/session.d.ts +0 -47
  129. package/dist/types/src/session/session.d.ts.map +0 -1
  130. package/dist/types/src/session/session.test.d.ts +0 -2
  131. package/dist/types/src/session/session.test.d.ts.map +0 -1
  132. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  133. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  134. package/dist/types/src/types.d.ts +0 -16
  135. package/dist/types/src/types.d.ts.map +0 -1
  136. package/src/conversation/conversation.ts +0 -119
  137. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
  138. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -72
  139. package/src/extraction/extraction-llm-function/index.ts +0 -5
  140. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -115
  141. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -64
  142. package/src/session/format.test.ts +0 -60
  143. package/src/session/session.conversations.json +0 -1
  144. package/src/session/session.test.ts +0 -146
  145. package/src/session/session.ts +0 -206
  146. package/src/summarization/summarization.test.ts +0 -201
  147. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -0,0 +1 @@
1
+ {"conversations":[{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[]},"prompt":{"content":[{"role":"user","content":"Hello world!","options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01P4EFfYrn2XLAaSd6uaAwEe","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" user is saying \"Hello world!\" - a classic greeting. I'll respond in a friendly way.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"EpICClkIDRgCKkD/kOgik1cgXVWa8rx+X6LFo9HvEBwZ7hEthnH1/FoiWiE//6BCJe4yobp2eUQ59wsNovlHzvu42pvDz+HrqdXfMg9jbGF1ZGUtb3B1cy00LTY4ABIMVgmAeJGtdlGLI9xhGgzKg78+pGfELSb0yJQiMI/HgJQKbergs7rT4xclgyLM9N/xxHsDdCAY9KUaeEIC3RZR84UAmT5sbfJNA1iviCpnQXuNeYdJDLcmfTityQm+c0fKlzUJ4glWpeLGfc4UiSqUbMk224KJ9U49hylck4vS+bx9qQDztEhnLfyRjuNm2v1p7e3UDxT0AeouZWl9O6mgWbezLG+B06hL9nm7PG8vd4jj8Gwo3RgB"}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"text-start","id":"1","metadata":{}},{"type":"text-delta","id":"1","delta":"Hello! 👋 Welcome! How can I help you today? Whether you have","metadata":{}},{"type":"text-delta","id":"1","delta":" questions, need assistance with something, or just want to chat, I'm here for you. 😊","metadata":{}},{"type":"text-end","id":"1","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":10,"outputTokens":80,"totalTokens":90},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":10,"output_tokens":9,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is 10 + 30?","options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01Tiw1p1D6XEuQVk2bAxrGTn","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" user wants to calculate 10 + 30.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"Et8BClkIDRgCKkB/0K0xQQQoenGVXh0VGAdSnkIX3gjXTelz+K0GBhpvk5FcfTPrgKU5HivTbsmhgLGs2LUMRc2mO/YcVzMMzqJqMg9jbGF1ZGUtb3B1cy00LTY4ABIMjxj4INjd+yZEsnEgGgwh2O9IY8I3CzpgM7siMCmsq9Vx4CquyYUx5D16IaDEyJSziOp/Lmzv45pGPfal+63iVnH2fPCHdd147EFgbCo0U0V5wQb1Ys9h6NU53sdkaZ8heyTJ3ZHIPZ1U3X0BmRKC40w34VJ9o8TCbQZwFjY6l5PRPxgB"}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"tool-params-start","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","name":"Calculator","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","delta":"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","delta":"{\"inpu","metadata":{}},{"type":"tool-params-delta","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","delta":"t\": ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","delta":"\"10 + ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","delta":"30\"}","metadata":{}},{"type":"tool-params-end","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","metadata":{}},{"type":"finish","reason":"tool-calls","usage":{"inputTokens":578,"outputTokens":83,"totalTokens":661},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":578,"output_tokens":9,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is 10 + 30?","options":{}},{"role":"assistant","content":[{"type":"reasoning","text":"The user wants to calculate 10 + 30.","options":{}},{"type":"tool-call","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01MjKitXaVxMmP53UKeVQDnY","name":"Calculator","isFailure":false,"result":{"result":40},"providerExecuted":false,"options":{}}],"options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_016uWLkUCfQZ6mGvDGXoKvSe","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"10","metadata":{}},{"type":"text-delta","id":"0","delta":" + 30 = **40**.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":650,"outputTokens":13,"totalTokens":663},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":650,"output_tokens":1,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"I am testing error handling in tool paramter parsing. I want you to call the calculator tool but omit the input parameter to intentionally differ from the tool schema.","options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01XDAKUT19TrSEueFFq4KJNN","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The user is asking me to intentionally call the Calculator tool without providing the required \"","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"input\" parameter to test error handling. They want to see what happens when a required parameter is omitted.\n\nI should be","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" transparent about this. The \"input\" parameter is marked as \"required\" in the schema","metadata":{}},{"type":"reasoning-delta","id":"0","delta":". Let me attempt the call as the user requests, omitting the required","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" parameter, to help them test error handling.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"EtsEClkIDRgCKkAX2ymsRtz1YoTibcGufK2UrveSRjyGaR8voyX9fdgYa1R/D34pdZw5MFbt5ju52GDm9S8sRTeM71WxMuzN+nC0Mg9jbGF1ZGUtb3B1cy00LTY4ABIM3UiIvuTf53XoYDhAGgwUgDabFIahkO3RmhQiMEo9KTbcz0KD+SM3wYYuJrPB6N1DE1bihQPTIcbsrPnTljVmYBpTcEAR+u7qaTVkbCqvA7gcGvE9JjfVi8j6ChFOQUJW7S3evbtkfr47FmEKgkNdKtabAe/b3nefFYIF6Hojn+LfQdFRnRHfB6iOoXT2/CUWCs1Isv0ETjgAFvKteDJB0biUC81luyCg00egHkySOFGA0Frf7BnDEg3MwXz1opK1D6+HDF6dCkHMDOtL0dkTslV7rZej/iEJigvHbVzoCZ47DWVFitZ/DUqpIn54Z1TPH+m+EyLPCws4yUgzIUy7z/JH51xAb/u1hteAhROcXKS+OrIbGrTIVBVlwUaXxKKL8pT5F30iHdGKifso1HDdoR1Gm1qwoUtONJh0SvyW6ti3zjDMHN4jAG3YsvfXYn0rGGJ/Is2hKmvz+ETEB+9hYTPhDjdpf1VMpn7YYoicUIua3FTYGXvs78cIEqYzbanebT0L88VRo/ZkB+rxRsf6EcAYQjeHX+motmzL7Xo9hKHZCMTuZLJbLd84XcNmnb/Vfrfz3jw/Znu9MW0ed2DyLyqu465mmEOjNteRq0wF/USjFB8F1p0CF/P3kjL5KbbqUK5NTqi/KoZt5MWcsp7lwzdZYdffINteTJ83R6ExGAE="}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"text-start","id":"1","metadata":{}},{"type":"text-delta","id":"1","delta":"I understand you want to test error handling by intent","metadata":{}},{"type":"text-delta","id":"1","delta":"ionally making a malformed tool call. I'll attempt to call the Calculator tool without the required","metadata":{}},{"type":"text-delta","id":"1","delta":" `input` parameter as you've requested:","metadata":{}},{"type":"text-end","id":"1","metadata":{}},{"type":"tool-params-start","id":"toolu_01EN5ueREDjY2Dkn4DtQT98p","name":"Calculator","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01EN5ueREDjY2Dkn4DtQT98p","delta":"","metadata":{}},{"type":"tool-params-end","id":"toolu_01EN5ueREDjY2Dkn4DtQT98p","metadata":{}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"I am testing error handling in tool paramter parsing. I want you to call the calculator tool but omit the input parameter to intentionally differ from the tool schema.","options":{}},{"role":"assistant","content":[{"type":"reasoning","text":"The user is asking me to intentionally call the Calculator tool without providing the required \"input\" parameter to test error handling. They want to see what happens when a required parameter is omitted.\n\nI should be transparent about this. The \"input\" parameter is marked as \"required\" in the schema. Let me attempt the call as the user requests, omitting the required parameter, to help them test error handling.","options":{}},{"type":"text","text":"I understand you want to test error handling by intentionally making a malformed tool call. I'll attempt to call the Calculator tool without the required `input` parameter as you've requested:","options":{}},{"type":"tool-call","id":"toolu_01EN5ueREDjY2Dkn4DtQT98p","name":"Calculator","params":{},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01EN5ueREDjY2Dkn4DtQT98p","name":"Calculator","isFailure":true,"result":"MalformedOutput: { \"module\": \"Toolkit\", \"method\": \"Calculator.handle\", \"description\": \"Failed to decode tool call parameters for tool 'Calculator' from:\\n'{}'\", \"cause\": { readonly input: string }\n└─ [\"input\"]\n └─ is missing }\ncaused by:\nParseError: { readonly input: string }\n└─ [\"input\"]\n └─ is missing","providerExecuted":false,"options":{}}],"options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01H2AnoBfNtCYSMVrB9EofxM","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The error","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" was returned as expected. Let me explain what happened.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"EvwBClkIDRgCKkCwoArrVhufw9O5jGNvlEhPLfP86nuapk2Sn3w7HQPwA6x/ZDavxvxNApMetymuM474yxHB+NZANvov3igozby0Mg9jbGF1ZGUtb3B1cy00LTY4ABIMO0f4i8xYIEfyqjvbGgyTlNkCKKfVpWC42MkiMDyCKZwXLycBfZPby82k9IK+I/jyDYQ1tl32IaSDXneYK9iFqZf0hwUZW/qcivZKWSpRp1Kx07laulxgxFuOuSq4eatbyLPKbwFnPHJhD537B6Q8sU5vq6IhwtasW0ahelaItDeaDp1h/QNeP8gjaqyG2o98UnYi6BaAOLQT9NW2q6fEGAE="}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"text-start","id":"1","metadata":{}},{"type":"text-delta","id":"1","delta":"As expected, the call failed and returned a **parse error**. Here's a","metadata":{}},{"type":"text-delta","id":"1","delta":" breakdown of what happened:\n\n1. **What I did:** Called the `Calculator` tool with an","metadata":{}},{"type":"text-delta","id":"1","delta":" empty parameter set `{}`, omitting the required `input` parameter.\n2. **Error","metadata":{}},{"type":"text-delta","id":"1","delta":" returned:** `MalformedOutput` — The system detected that the tool call parameters","metadata":{}},{"type":"text-delta","id":"1","delta":" failed to decode because the required `input` field (of type `string`) **","metadata":{}},{"type":"text-delta","id":"1","delta":"is missing**.\n3. **Error chain:** The root cause is a `ParseError` confirming that the schema validation","metadata":{}},{"type":"text-delta","id":"1","delta":" caught the missing required field at path `[\"input\"]`.\n\nThis","metadata":{}},{"type":"text-delta","id":"1","delta":" shows that the tool parameter parsing correctly validates against the defined JSON","metadata":{}},{"type":"text-delta","id":"1","delta":" schema and rejects calls that don't include all required parameters. The error","metadata":{}},{"type":"text-delta","id":"1","delta":" message is quite descriptive, pointing to exactly which","metadata":{}},{"type":"text-delta","id":"1","delta":" field is missing and what type it expected. \n\nWould","metadata":{}},{"type":"text-delta","id":"1","delta":" you like to test any other error handling scenarios?","metadata":{}},{"type":"text-end","id":"1","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":809,"outputTokens":222,"totalTokens":1031},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":809,"output_tokens":9,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":false,"tools":[]},"prompt":{"content":[{"role":"system","content":"<instructions>\n You are an agent that summarizes conversations.\n You will be given a conversation and you will need to summarize it to compact context.\n Your summary will be used by AI model to continue the conversation without seeing the original messages.\n Remove unnecessary details and focus on the most important information.\n Keep user requests, method of action, and progress.\n Keep the important objects and references in original format.\n It is very important you maintain unchanged ECHO DXNs and IDs of the objects.\n Output only the summary.\n</instructions>\n\n<example_output>\n Processing a flight booking confirmation email. The user requested a round-trip flight from Warsaw to Barcelona for March 15–22, 2026.\n\n Action taken:\n - Found existing booking object `dxn:echo:@:01KHZWKQ9HPJYG147HV7CP5ARX` (type `dxn:echo:@:01KHXAMH4F3V94RCYC7P7M2H0C` / `com.example.type.flight-booking`) matching confirmation code ABC123.\n - Updated the booking status from \"pending\" to \"confirmed\".\n - Added flight details: Outbound WAW→BCN Mar 15 08:30, Return BCN→WAW Mar 22 14:15, carrier LOT Polish Airlines.\n - Stored passenger info and seat assignments in notes.\n\n Booking details preserved: Confirmation ABC123, total 1,240 PLN, booked 2026-02-18, airline LOT Polish Airlines, flight numbers LO391/LO392.\n\n Status enum mapping for the booking schema: pending=Awaiting payment, confirmed=Booked, cancelled=Cancelled, completed=Flown.\n</example_output>\n\nTASK: Reply with the summary of the prior conversation.","options":{}},{"role":"user","content":"How many apples are in the basket?","options":{}},{"role":"assistant","content":"There are 10 apples in the basket.","options":{}},{"role":"user","content":"<instructions>\n You are an agent that summarizes conversations.\n You will be given a conversation and you will need to summarize it to compact context.\n Your summary will be used by AI model to continue the conversation without seeing the original messages.\n Remove unnecessary details and focus on the most important information.\n Keep user requests, method of action, and progress.\n Keep the important objects and references in original format.\n It is very important you maintain unchanged ECHO DXNs and IDs of the objects.\n Output only the summary.\n</instructions>\n\n<example_output>\n Processing a flight booking confirmation email. The user requested a round-trip flight from Warsaw to Barcelona for March 15–22, 2026.\n\n Action taken:\n - Found existing booking object `dxn:echo:@:01KHZWKQ9HPJYG147HV7CP5ARX` (type `dxn:echo:@:01KHXAMH4F3V94RCYC7P7M2H0C` / `com.example.type.flight-booking`) matching confirmation code ABC123.\n - Updated the booking status from \"pending\" to \"confirmed\".\n - Added flight details: Outbound WAW→BCN Mar 15 08:30, Return BCN→WAW Mar 22 14:15, carrier LOT Polish Airlines.\n - Stored passenger info and seat assignments in notes.\n\n Booking details preserved: Confirmation ABC123, total 1,240 PLN, booked 2026-02-18, airline LOT Polish Airlines, flight numbers LO391/LO392.\n\n Status enum mapping for the booking schema: pending=Awaiting payment, confirmed=Booked, cancelled=Cancelled, completed=Flown.\n</example_output>\n\nTASK: Reply with the summary of the prior conversation.","options":{}}]},"response":[{"type":"response-metadata","id":"msg_013JQyMpNyup8yd9gfQtz1sK","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning","text":"The conversation is very simple - the user asked how many apples are in the basket, and the assistant answered 10. There are no ECHO DXNs, no complex objects, and no real context to preserve. This appears to be a trivial exchange with no prior context or tools involved.","metadata":{"anthropic":{"type":"thinking","signature":"EsoDClkIDRgCKkATKz5csAkAGkfvuEctWM4RDkW59XDzgRDBqVJSoUkrn7sH98NL6X4FerfTUJbq3YrYUxUwj2IjzXTI27XlVqc6Mg9jbGF1ZGUtb3B1cy00LTY4ABIMT/tDfruFSjlciBFYGgyTdl8IxDG9mzlTn5UiMFMOqm36jmi4HNgGu6HR4mwonVPi8KhcOF7flqctd56f4V11qOMTiaX/P9f1rZpB0yqeAlZX160VMP2VXB7IlmZWKlJZBAI3gMIs3ef7s1b+mKSSJf20F4wpg1ECrPdsnnIciHQVKxCc3qoTNnGuwff7C4U5O5s9SDNVhEMNdW8fSy5PAkFPPP/XH7Seac6RzDAIT4aJvRA2UwhqjvDwJszwZQl6Xb7Ng1fsGlAu2ywWBjRzKuoJDJCBySfrxGfjl5cH/Jrg80f9Pp2QHJgIYoTNNtQtB7GS3i1btjNp/N/TsKMf3so5iK2FQ6qtl5Ot9H9qjbYhTRq23zKQnkUkav27evOpE5kSOittzJ3MdblXMXhO09SvJ69n6la0MSIDm50XVFuLDe6819UeGI4LE5S0TQj8bM43n0gk+Z9omi6UAw8JjltCKkaBp5ws6pOozbQYAQ=="}}},{"type":"text","text":"The user asked how many apples are in the basket. The assistant responded that there are 10 apples in the basket. No additional context, objects, or references were involved.","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":896,"outputTokens":119,"totalTokens":1015,"cachedInputTokens":0},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":896,"output_tokens":119,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[]},"prompt":{"content":[{"role":"assistant","content":"This is the continuation of a conversation that was compacted to preserve context-window space. Summary of what happened in this conversation previously: <summary>The user asked how many apples are in the basket. The assistant responded that there are 10 apples in the basket. No additional context, objects, or references were involved.</summary>","options":{}},{"role":"user","content":"What did we talk about?","options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01BWTgasX7BDFvGG5Zp7YLjq","modelId":"claude-opus-4-6","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The user is asking me to recall what we talked about. Based on the summary provided, we discussed","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" how many apples are in a basket, and I told them there are 10 apples in the basket.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"EvECClkIDRgCKkASmeuhdK5nBIprAsBq/0ChahRLAcN/Jh26xQdhMZjSsojjx5UoHW21RiuAHCzVLIvt0E/4fNvj7qhOZXzyhNpPMg9jbGF1ZGUtb3B1cy00LTY4ABIMKSjzFmDf3YdferaTGgwKZDXHIy/bxUiq+BQiMHKqTYVUgDcsD1R9OJWF/fKpB2ko0dBkdb/dd5spULoBmqZvFxmOUAvQz+U0OUEToSrFAWa0zySqK5E/R1ZZ0p75Vf28ONof2AMWDCuNT6A35i8ieabC8YHTh3lwgQyaoljpHVkY4uRKqYzUv7t0XBTeAr3Aduq7U+Lmta2+P7Y/oovoOhtfOjADeywq9/PrpCJONwMSU+jctKGfHaln6aoRn1XnIBAiCiUABYCeS9CzXbVTVAOZBQxgmz2Jw5rJfe/EUQfplrcqa3rXDlUjZ6/DTf/YBLIDeCeZ2gpZbnSTIP+7v9mRiAvWlDQfGqM+2KJ/2EorqdD+GAE="}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"text-start","id":"1","metadata":{}},{"type":"text-delta","id":"1","delta":"Based","metadata":{}},{"type":"text-delta","id":"1","delta":" on our previous conversation, you asked me how many apples are in a basket, and I told you there are **10 apples** in the basket.","metadata":{}},{"type":"text-delta","id":"1","delta":" That's all we discussed!","metadata":{}},{"type":"text-end","id":"1","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":98,"outputTokens":103,"totalTokens":201},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":98,"output_tokens":28,"server_tool_use":null,"service_tier":"standard"}}}}]}]}
@@ -0,0 +1,342 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import type * as AiError from '@effect/ai/AiError';
6
+ import * as LanguageModel from '@effect/ai/LanguageModel';
7
+ import * as Array from 'effect/Array';
8
+ import * as Chunk from 'effect/Chunk';
9
+ import * as Effect from 'effect/Effect';
10
+ import { pipe } from 'effect/Function';
11
+ import * as Option from 'effect/Option';
12
+ import * as Stream from 'effect/Stream';
13
+
14
+ import {
15
+ AiParser,
16
+ AiPreprocessor,
17
+ AiSummarizer,
18
+ type AiToolNotFoundError,
19
+ callTool,
20
+ type OpaqueToolkit,
21
+ type PromptPreprocessingError,
22
+ type ToolExecutionService,
23
+ type ToolResolverService,
24
+ withoutToolCallParising,
25
+ } from '@dxos/ai';
26
+ import { type Blueprint, Trace, Operation, OperationRegistry } from '@dxos/compute';
27
+ import { Database, Obj } from '@dxos/echo';
28
+ import { log } from '@dxos/log';
29
+ import { ContentBlock, Message } from '@dxos/types';
30
+
31
+ import { type AiAssistantError } from '../errors';
32
+ import { CompleteBlock, PartialBlock } from '../tracing';
33
+ import { formatSystemPrompt, formatUserPrompt } from './format';
34
+ import { GenerationObserver } from './observer';
35
+
36
+ export type AiRequestRunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;
37
+
38
+ export type AiRequestRunRequirements =
39
+ | LanguageModel.LanguageModel
40
+ | ToolExecutionService
41
+ | ToolResolverService
42
+ | Database.Service
43
+ | Operation.Service
44
+ | OperationRegistry.Service
45
+ | Trace.TraceService;
46
+
47
+ export type AiRequestOptions = {
48
+ /**
49
+ * Summarize before executing the prompt if the existing history exceeds this threshold.
50
+ */
51
+ summarizationThreshold?: number;
52
+
53
+ // TODO(dmaretskyi): Plan to phase out in favor of TracingService and the return type being a stream.
54
+ observer?: GenerationObserver;
55
+ /**
56
+ * Callback for when a message is received from the user, model, or tool.
57
+ * This is useful for streaming the output to a queue.
58
+ */
59
+ onOutput?: (message: Message.Message) => Effect.Effect<void, never, never>;
60
+ };
61
+
62
+ export type AiRequestRunProps<R = never> = {
63
+ prompt: string | ContentBlock.Any[];
64
+ // TODO(wittjosiah): Rename to systemPrompt.
65
+ system?: string;
66
+ history?: Message.Message[];
67
+ objects?: Obj.Unknown[];
68
+ blueprints?: readonly Blueprint.Blueprint[];
69
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
70
+ };
71
+
72
+ export type AiRequestBeginProps = {
73
+ prompt: string | ContentBlock.Any[];
74
+ system?: string;
75
+ history?: Message.Message[];
76
+ objects?: Obj.Unknown[];
77
+ blueprints?: readonly Blueprint.Blueprint[];
78
+ };
79
+
80
+ export type AiRequestTurnProps<R = never> = {
81
+ system: string;
82
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
83
+ };
84
+
85
+ export type AiRequestTurnResult = {
86
+ messages: Message.Message[];
87
+ done: boolean;
88
+ };
89
+
90
+ /**
91
+ * Contains message history, tools, current context.
92
+ * Current context means the state of the app, time of day, and other contextual information.
93
+ * It makes requests to the model, its a state machine.
94
+ * It keeps track of the current goal.
95
+ * It manages the context window.
96
+ * Tracks the success criteria of reaching the goal, exposing metrics (stretch).
97
+ * Could be run locally in the app or remotely.
98
+ * Could be personal or shared.
99
+ */
100
+ export class AiRequest {
101
+ /** Prevents concurrent execution of session. */
102
+ private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
103
+
104
+ private readonly _observer: GenerationObserver;
105
+ private readonly _onOutput: (message: Message.Message) => Effect.Effect<void, never, never>;
106
+
107
+ /** Message history from queue. */
108
+ // TODO(burdon): Evolve into supporting a git-like graph of messages.
109
+ private _history: Message.Message[] = [];
110
+
111
+ /** Pending messages for this session (incl. the current prompt). */
112
+ private _pending: Message.Message[] = [];
113
+
114
+ private _started = 0;
115
+ private _ended = 0;
116
+ private _toolCalls = 0;
117
+
118
+ constructor(private readonly _options: AiRequestOptions = {}) {
119
+ this._observer = _options.observer ?? GenerationObserver.noop();
120
+ this._onOutput = _options.onOutput ?? (() => Effect.void);
121
+ }
122
+
123
+ get duration(): number {
124
+ return this._ended - this._started;
125
+ }
126
+
127
+ get toolCalls(): number {
128
+ return this._toolCalls;
129
+ }
130
+
131
+ get pending(): readonly Message.Message[] {
132
+ return this._pending;
133
+ }
134
+
135
+ private _submitMessage = (message: Message.Message): Effect.Effect<Message.Message, never, Trace.TraceService> =>
136
+ Effect.gen(this, function* () {
137
+ this._pending.push(message);
138
+ yield* this._observer.onMessage(message);
139
+ for (const block of message.blocks) {
140
+ yield* Trace.write(CompleteBlock, {
141
+ messageId: message.id,
142
+ role: message.sender.role!,
143
+ block,
144
+ });
145
+ }
146
+ yield* this._onOutput(message);
147
+ return message;
148
+ });
149
+
150
+ getToolCalls = () =>
151
+ pipe(
152
+ [...this._history, ...this._pending],
153
+ Array.reverse,
154
+ Array.takeWhile((_) => _.sender.role === 'assistant'),
155
+ Array.flatMap((_) => _.blocks.filter(ContentBlock.is('toolCall')).map((block) => ({ block, message: _ }))),
156
+ Array.filter((_) => !_.block.providerExecuted),
157
+ Array.reverse,
158
+ );
159
+
160
+ /**
161
+ * Initialize a session: set up history, perform summarization if needed, and submit the user prompt.
162
+ * Must be called before `runTurn()`.
163
+ */
164
+ begin = ({
165
+ prompt,
166
+ system,
167
+ history = [],
168
+ blueprints = [],
169
+ objects = [],
170
+ }: AiRequestBeginProps): Effect.Effect<void, AiRequestRunError, AiRequestRunRequirements> =>
171
+ Effect.gen(this, function* () {
172
+ this._started = Date.now();
173
+ this._history = [...history];
174
+ this._pending = [];
175
+
176
+ const systemPrompt = yield* formatSystemPrompt({ system, blueprints, objects }).pipe(Effect.orDie);
177
+
178
+ if (this._options.summarizationThreshold !== undefined) {
179
+ const tokenCount = yield* AiPreprocessor.estimateTokens(
180
+ yield* AiPreprocessor.preprocessPrompt([...this._history], {
181
+ system: systemPrompt,
182
+ }),
183
+ );
184
+ if (tokenCount > this._options.summarizationThreshold) {
185
+ const summary = yield* AiSummarizer.summarize([...this._history]);
186
+ yield* this._submitMessage(summary);
187
+ }
188
+ }
189
+
190
+ yield* this._submitMessage(yield* formatUserPrompt({ prompt, history }));
191
+ }).pipe(Effect.withSpan('AiRequest.begin'));
192
+
193
+ /**
194
+ * Execute a single turn: one LLM generation followed by tool execution.
195
+ * The toolkit and system prompt can be updated between turns to reflect context changes (e.g. dynamically enabled blueprints).
196
+ */
197
+ runAgentTurn = <const R = never>({
198
+ system,
199
+ toolkit: opaqueToolkit,
200
+ }: AiRequestTurnProps<R>): Effect.Effect<AiRequestTurnResult, AiRequestRunError, AiRequestRunRequirements | R> =>
201
+ Effect.gen(this, function* () {
202
+ log.info('request', {
203
+ system: { snippet: createSnippet(system), length: system.length },
204
+ pending: this._pending.length,
205
+ history: this._history.length,
206
+ });
207
+
208
+ const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], {
209
+ system,
210
+ cacheControl: 'ephemeral',
211
+ });
212
+
213
+ const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : undefined;
214
+
215
+ const observer = this._observer;
216
+ let currentMessageId: Obj.ID | null = null;
217
+
218
+ const messages = yield* LanguageModel.streamText({
219
+ prompt,
220
+ toolkit,
221
+ disableToolCallResolution: true,
222
+ }).pipe(
223
+ withoutToolCallParising,
224
+ AiParser.parseResponse({
225
+ emitPartial: true,
226
+ onBegin: () => observer.onBegin(),
227
+ onBlock: (block) => observer.onBlock(block),
228
+ onPart: (part) => observer.onPart(part as any),
229
+ onEnd: (summary) => observer.onEnd(summary),
230
+ }),
231
+ Stream.mapEffect(
232
+ (block) =>
233
+ Effect.gen(this, function* () {
234
+ if (block.pending) {
235
+ currentMessageId ??= Obj.ID.random();
236
+ log('emit ephemeral message', { id: currentMessageId, type: block._tag });
237
+ yield* Trace.write(PartialBlock, {
238
+ messageId: currentMessageId,
239
+ role: 'assistant',
240
+ block,
241
+ });
242
+ return Option.none();
243
+ } else {
244
+ currentMessageId ??= Obj.ID.random();
245
+ const id = currentMessageId;
246
+ currentMessageId = null;
247
+ log('emit complete message', { id, type: block._tag });
248
+ const message = Obj.make(Message.Message, {
249
+ id,
250
+ created: new Date().toISOString(),
251
+ sender: { role: 'assistant' },
252
+ blocks: [block],
253
+ });
254
+ return Option.some(yield* this._submitMessage(message));
255
+ }
256
+ }),
257
+ { concurrency: 1, unordered: false },
258
+ ),
259
+ Stream.filterMap((_) => _),
260
+ Stream.runCollect,
261
+ Effect.map(Chunk.toArray),
262
+ );
263
+ log.info('messages', { messages });
264
+
265
+ const toolCalls = this.getToolCalls();
266
+
267
+ if (toolCalls.length === 0) {
268
+ this._ended = Date.now();
269
+ return { messages, done: true };
270
+ } else if (!toolkit) {
271
+ throw new Error('No toolkit provided');
272
+ }
273
+
274
+ return { messages, done: false };
275
+ }).pipe(Effect.withSpan('AiRequest.runAgentTurn'));
276
+
277
+ runTools = <const R = never>({
278
+ toolkit: opaqueToolkit,
279
+ }: {
280
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
281
+ }): Effect.Effect<void, AiRequestRunError, AiRequestRunRequirements | R> =>
282
+ Effect.gen(this, function* () {
283
+ const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : undefined;
284
+ const toolCalls = this.getToolCalls();
285
+ const toolResults = yield* Effect.forEach(toolCalls, ({ block, message }) => {
286
+ if (!toolkit) {
287
+ throw new Error('No toolkit provided');
288
+ }
289
+ return callTool(toolkit, block);
290
+ });
291
+
292
+ yield* this._submitMessage(
293
+ Obj.make(Message.Message, {
294
+ created: new Date().toISOString(),
295
+ sender: { role: 'tool' },
296
+ blocks: toolResults,
297
+ }),
298
+ );
299
+
300
+ this._toolCalls += toolResults.length;
301
+ }).pipe(Effect.withSpan('AiRequest.runTools'));
302
+
303
+ /**
304
+ * Run a full conversation turn loop. Equivalent to calling `begin()` then `runTurn()` in a loop.
305
+ */
306
+ run = <const R = never>({
307
+ prompt,
308
+ system: systemTemplate,
309
+ history = [],
310
+ objects = [],
311
+ blueprints = [],
312
+ toolkit,
313
+ }: AiRequestRunProps<R>): Effect.Effect<Message.Message[], AiRequestRunError, AiRequestRunRequirements | R> =>
314
+ Effect.gen(this, function* () {
315
+ yield* this.begin({ prompt, system: systemTemplate, history, objects, blueprints });
316
+
317
+ const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects }).pipe(Effect.orDie);
318
+
319
+ do {
320
+ const { done } = yield* this.runAgentTurn({ system, toolkit });
321
+ if (done) {
322
+ break;
323
+ }
324
+ yield* this.runTools({ toolkit });
325
+ } while (true);
326
+
327
+ log('done', { pending: this._pending.length, duration: this.duration, tools: this._toolCalls });
328
+ return this._pending;
329
+ }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiRequest.run'));
330
+ }
331
+
332
+ const createSnippet = (text: string, len = 32) =>
333
+ text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
334
+
335
+ // TODO(dmaretskyi): Extract as a general util.
336
+ const logDump = (message: string, data: unknown) => {
337
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
338
+ const { writeFileSync } = require('node:fs');
339
+ const path = `/tmp/log-data-${Date.now()}.json`;
340
+ writeFileSync(path, JSON.stringify(data, null, 2));
341
+ log.info(message, { path });
342
+ };
@@ -2,45 +2,46 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type Tool, Toolkit } from '@effect/ai';
6
- import { type Context, Effect } from 'effect';
5
+ import * as Toolkit from '@effect/ai/Toolkit';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Layer from 'effect/Layer';
7
8
 
8
- import { type AiToolNotFoundError, ToolExecutionService, type ToolId, ToolResolverService } from '@dxos/ai';
9
- import { type Blueprint } from '@dxos/blueprints';
9
+ import { type AiToolNotFoundError, OpaqueToolkit, ToolExecutionService, ToolResolverService } from '@dxos/ai';
10
+ import { type Blueprint } from '@dxos/compute';
10
11
  import { isTruthy } from '@dxos/util';
11
12
 
12
- export type ToolkitParams<Tools extends Record<string, Tool.Any>> = {
13
- toolkit?: Toolkit.Toolkit<Tools>;
14
- toolIds?: ToolId[];
15
- blueprints?: Blueprint.Blueprint[];
13
+ export type CreateToolkitProps = {
14
+ toolkit?: OpaqueToolkit.Any;
15
+ blueprints?: readonly Blueprint.Blueprint[];
16
+ /**
17
+ * Self-contained with handlers toolkits.
18
+ */
19
+ opaqueToolkits?: readonly OpaqueToolkit.Any[];
16
20
  };
17
21
 
18
22
  /**
19
23
  * Build a combined toolkit from the blueprint tools and the provided toolkit.
20
24
  */
21
- export const createToolkit = <Tools extends Record<string, Tool.Any> = {}>({
22
- toolkit: toolkitParam,
23
- toolIds = [],
25
+ export const createToolkit = ({
26
+ toolkit: toolkitProp,
24
27
  blueprints = [],
25
- }: ToolkitParams<Tools>): Effect.Effect<
26
- Toolkit.WithHandler<any>,
28
+ opaqueToolkits = [],
29
+ }: CreateToolkitProps): Effect.Effect<
30
+ OpaqueToolkit.OpaqueToolkit,
27
31
  AiToolNotFoundError,
28
- ToolResolverService | ToolExecutionService | Tool.HandlersFor<Tools>
32
+ ToolResolverService | ToolExecutionService
29
33
  > =>
30
34
  Effect.gen(function* () {
31
- const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
32
- ...blueprints.flatMap(({ tools }) => tools),
33
- ...toolIds,
34
- ]);
35
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
36
+ const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
37
+ const opaqueToolkit = OpaqueToolkit.merge(...opaqueToolkits);
35
38
 
36
- const blueprintToolkitHandler: Context.Context<Tool.HandlersFor<any>> = yield* blueprintToolkit.toContext(
37
- ToolExecutionService.handlersFor(blueprintToolkit),
39
+ const toolkitDefs = [toolkitProp?.toolkit, blueprintToolkit, opaqueToolkit.toolkit].filter(isTruthy);
40
+ const mergedToolkit = Toolkit.merge(...toolkitDefs);
41
+ const combinedHandlerLayer = Layer.mergeAll(
42
+ Layer.succeedContext(blueprintToolHandler),
43
+ toolkitProp?.layer ?? OpaqueToolkit.empty.layer,
44
+ opaqueToolkit.layer,
38
45
  );
39
-
40
- const toolkit = Toolkit.merge(...[toolkitParam, blueprintToolkit].filter(isTruthy));
41
- return yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler)) as any as Effect.Effect<
42
- Toolkit.WithHandler<any>,
43
- never,
44
- Tool.HandlersFor<Tools>
45
- >;
46
- });
46
+ return OpaqueToolkit.make(mergedToolkit, combinedHandlerLayer as any) as OpaqueToolkit.OpaqueToolkit;
47
+ }) as Effect.Effect<OpaqueToolkit.OpaqueToolkit, AiToolNotFoundError, ToolResolverService | ToolExecutionService>;
@@ -2,12 +2,12 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema } from 'effect';
5
+ import * as Schema from 'effect/Schema';
6
6
 
7
- import { type ObjectVersion } from '@dxos/echo-db';
8
- import { getVersion } from '@dxos/echo-db';
9
- import { type AnyEchoObject, ObjectId } from '@dxos/echo-schema';
10
- import { type ContentBlock } from '@dxos/schema';
7
+ import { type Obj } from '@dxos/echo';
8
+ import { type ObjectVersion, getVersion } from '@dxos/echo-db';
9
+ import { ObjectId } from '@dxos/keys';
10
+ import { type ContentBlock } from '@dxos/types';
11
11
 
12
12
  // TODO(dmaretskyi): Extract.
13
13
  const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
@@ -27,11 +27,11 @@ export interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema>
27
27
 
28
28
  export const VersionPin: typeof VersionPinSchema & {
29
29
  DISPOSITION: 'version-pin';
30
- fromObject: (object: AnyEchoObject) => VersionPin;
30
+ fromObject: (object: Obj.Unknown) => VersionPin;
31
31
  createBlock: (pin: VersionPin) => ContentBlock.Any;
32
32
  } = class extends VersionPinSchema {
33
33
  static readonly DISPOSITION = 'version-pin';
34
- static fromObject(object: AnyEchoObject): VersionPin {
34
+ static fromObject(object: Obj.Unknown): VersionPin {
35
35
  return VersionPin.make({
36
36
  objectId: object.id,
37
37
  version: getVersion(object),
@@ -45,16 +45,13 @@ You can reply with a block reference for objects if you have the DXN. For exampl
45
45
 
46
46
  Each suggestion must in the form of a proposed action enclosed in a <suggestion> tag.
47
47
  Example: <suggestion>Show on a map</suggestion>
48
+ Do not use bullets when listing suggestions.
48
49
 
49
50
  ### Multiple choice questions
50
51
 
51
52
  You may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.
52
53
  Example: <select><option>Yes</option><option>No</option></select>
53
54
 
54
- ## Toolkit
55
-
56
- If the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.
57
-
58
55
  ## Context
59
56
 
60
57
  The current date and time is {{DATETIME}}.
@@ -43,6 +43,54 @@
43
43
  - Suggested actions could include actions that create artifacts.
44
44
  - After creating an artifact use a tool to add it to the chat context.
45
45
 
46
- ## Context
46
+ ## Navigation
47
47
 
48
- Today is {{DATETIME}}.
48
+ - When the user asks to open, go to, navigate to, or show a specific document, page, or object, use the navigation tools instead of loading content inline.
49
+ - First, call the Resolve navigation targets tool to find the navigation path for the requested item.
50
+ - Without arguments, it returns available pages that can be navigated to.
51
+ - With a DXN in the query, it resolves that specific object to a navigation path.
52
+ - If you know the object's DXN (e.g. from a database query or context objects), pass it in the query to get the exact navigation path.
53
+ - Then, call the Open tool with the resolved navigation path(s) in the subject array to navigate the user to that item.
54
+ - Do not respond with the document's content when the user asks to open it — open it in the main content area instead.
55
+ - If multiple targets match, present the options to the user and let them choose which to open.
56
+
57
+ ## Response format
58
+
59
+ - NEVER output narration text outside `<status>` tags or the final user-facing answer. Any progress update, transition sentence, or "let me…" lead-in MUST be wrapped in `<status>...</status>`. Violating this is an error.
60
+ - When updating the user about the progress of the work you are doing, put the update in a <status> XML tag.
61
+ - Only when you need to show the result to the user or ask a question use a text block without status tags.
62
+ - After a `</status>` tag, the next output must be either another `<status>` tag, a tool call, or the final user-facing answer — never a transition sentence.
63
+
64
+ <example>
65
+ // reasoning...
66
+ <status>Looking for emails from Depot</status>
67
+ // tool call..
68
+ <status>Found 2 emails from Depot.</status>
69
+ <status>Loading email details</status>
70
+ // tool call
71
+ <status>Located mailbox</status>
72
+ I found **2 emails from Depot**:
73
+ </example>
74
+
75
+ Do not do (no status tags):
76
+
77
+ <incorrect_example>
78
+ // reasoning...
79
+ // tool call..
80
+ I found 2 emails from Depot. Let me load their details:
81
+ // tool call
82
+ I found **2 emails from Depot**:
83
+ </incorrect_example>
84
+
85
+ Do not do (transition sentence after a status tag, before tool calls):
86
+
87
+ <incorrect_example>
88
+ <status>Doing X</status>
89
+ Let me start by doing X:
90
+ // tool call
91
+ </incorrect_example>
92
+
93
+ - NEVER write a lead-in sentence before a tool call. Use a `<status>` tag instead.
94
+ - Avoid using plain text to tell the user what you are about to do; instead, use status blocks.
95
+ - WRONG: Let me get...
96
+ - CORRECT: <status>Getting...</status>
@@ -5,10 +5,10 @@
5
5
  import defaultsDeep from 'lodash.defaultsdeep';
6
6
 
7
7
  import { type ModelCapabilities } from '@dxos/ai';
8
- import { Template } from '@dxos/blueprints';
8
+ import { Template } from '@dxos/compute';
9
9
 
10
- import FORMAT from './format.tpl?raw';
11
- import SYSTEM from './system.tpl?raw';
10
+ import FORMAT from './instructions/format.tpl?raw';
11
+ import SYSTEM from './instructions/system.tpl?raw';
12
12
 
13
13
  /**
14
14
  * System template variables.
@@ -29,13 +29,14 @@ export const createSystemPrompt = (options: Partial<SystemPromptOptions> = {}) =
29
29
  }),
30
30
  );
31
31
 
32
+ /**
33
+ * NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
34
+ */
35
+ const SYSTEM_ID = '01K2J2FZWH39F55CS7B26GP1Q3';
36
+
32
37
  export const templates = {
33
- /** Editable system prompt.*/
34
38
  system: Template.make({
35
- /**
36
- * NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
37
- */
38
- id: '01K2J2FZWH39F55CS7B26GP1Q3',
39
+ id: SYSTEM_ID,
39
40
  source: SYSTEM,
40
41
  }),
41
42
  };