@dxos/assistant 0.8.1 → 0.8.2-main.10c050d

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 (277) hide show
  1. package/dist/lib/browser/index.mjs +1830 -228
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +1825 -229
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +1831 -228
  8. package/dist/lib/node-esm/index.mjs.map +4 -4
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/{src/bin → bin}/chat.d.ts.map +1 -1
  11. package/dist/types/src/blueprint/blueprint.d.ts +79 -0
  12. package/dist/types/src/blueprint/blueprint.d.ts.map +1 -0
  13. package/dist/types/src/blueprint/blueprint.test.d.ts +2 -0
  14. package/dist/types/src/blueprint/blueprint.test.d.ts.map +1 -0
  15. package/dist/types/src/blueprint/index.d.ts +4 -0
  16. package/dist/types/src/blueprint/index.d.ts.map +1 -0
  17. package/dist/types/src/blueprint/logger.d.ts +3 -0
  18. package/dist/types/src/blueprint/logger.d.ts.map +1 -0
  19. package/dist/types/src/blueprint/machine.d.ts +113 -0
  20. package/dist/types/src/blueprint/machine.d.ts.map +1 -0
  21. package/dist/types/src/blueprint/test-data.d.ts +3 -0
  22. package/dist/types/src/blueprint/test-data.d.ts.map +1 -0
  23. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +4 -0
  24. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -0
  25. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +2 -0
  26. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +1 -0
  27. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +2 -0
  28. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +1 -0
  29. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +210 -0
  30. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -0
  31. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +2 -0
  32. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +1 -0
  33. package/dist/types/src/extraction/extraction-ner-function/index.d.ts +3 -0
  34. package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -0
  35. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +14 -0
  36. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -0
  37. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts +2 -0
  38. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts.map +1 -0
  39. package/dist/types/src/extraction/extraction.d.ts +242 -0
  40. package/dist/types/src/extraction/extraction.d.ts.map +1 -0
  41. package/dist/types/src/extraction/index.d.ts +5 -0
  42. package/dist/types/src/extraction/index.d.ts.map +1 -0
  43. package/dist/types/src/extraction/quotes.d.ts +15 -0
  44. package/dist/types/src/extraction/quotes.d.ts.map +1 -0
  45. package/dist/types/src/extraction/quotes.test.d.ts +2 -0
  46. package/dist/types/src/extraction/quotes.test.d.ts.map +1 -0
  47. package/dist/types/src/index.d.ts +4 -3
  48. package/dist/types/src/index.d.ts.map +1 -1
  49. package/dist/types/src/research/exa.d.ts +7 -0
  50. package/dist/types/src/research/exa.d.ts.map +1 -0
  51. package/dist/types/src/research/graph.d.ts +39 -0
  52. package/dist/types/src/research/graph.d.ts.map +1 -0
  53. package/dist/types/src/research/graph.test.d.ts +2 -0
  54. package/dist/types/src/research/graph.test.d.ts.map +1 -0
  55. package/dist/types/src/research/index.d.ts +4 -0
  56. package/dist/types/src/research/index.d.ts.map +1 -0
  57. package/dist/types/src/research/research.d.ts +10 -0
  58. package/dist/types/src/research/research.d.ts.map +1 -0
  59. package/dist/types/src/research/research.test.d.ts +2 -0
  60. package/dist/types/src/research/research.test.d.ts.map +1 -0
  61. package/dist/types/src/session/index.d.ts +1 -0
  62. package/dist/types/src/session/index.d.ts.map +1 -1
  63. package/dist/types/src/session/session.d.ts +80 -35
  64. package/dist/types/src/session/session.d.ts.map +1 -1
  65. package/dist/types/src/session/version-pin.d.ts +21 -0
  66. package/dist/types/src/session/version-pin.d.ts.map +1 -0
  67. package/dist/types/src/session/version-pin.test.d.ts +2 -0
  68. package/dist/types/src/session/version-pin.test.d.ts.map +1 -0
  69. package/dist/types/src/summarization/summarization.test.d.ts +2 -0
  70. package/dist/types/src/summarization/summarization.test.d.ts.map +1 -0
  71. package/dist/types/src/testing/exa-search-1748337321991.d.ts +38 -0
  72. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +1 -0
  73. package/dist/types/src/testing/exa-search-1748337331526.d.ts +37 -0
  74. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +1 -0
  75. package/dist/types/src/testing/exa-search-1748337344119.d.ts +58 -0
  76. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +1 -0
  77. package/dist/types/src/testing/index.d.ts +2 -6
  78. package/dist/types/src/testing/index.d.ts.map +1 -1
  79. package/dist/types/src/types.d.ts +3 -0
  80. package/dist/types/src/types.d.ts.map +1 -0
  81. package/dist/types/tsconfig.tsbuildinfo +1 -1
  82. package/package.json +25 -35
  83. package/src/blueprint/blueprint.test.ts +142 -0
  84. package/src/blueprint/blueprint.ts +95 -0
  85. package/src/blueprint/index.ts +7 -0
  86. package/src/blueprint/logger.ts +66 -0
  87. package/src/blueprint/machine.ts +208 -0
  88. package/src/blueprint/test-data.ts +55 -0
  89. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +95 -0
  90. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +64 -0
  91. package/src/extraction/extraction-llm-function/index.ts +5 -0
  92. package/src/extraction/extraction-llm-function/instructions.tpl +28 -0
  93. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +115 -0
  94. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +54 -0
  95. package/src/extraction/extraction-ner-function/index.ts +6 -0
  96. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +94 -0
  97. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +97 -0
  98. package/src/extraction/extraction.ts +75 -0
  99. package/src/extraction/index.ts +8 -0
  100. package/src/extraction/quotes.test.ts +27 -0
  101. package/src/extraction/quotes.ts +57 -0
  102. package/src/index.ts +4 -3
  103. package/src/research/exa.ts +100 -0
  104. package/src/research/graph.test.ts +15 -0
  105. package/src/research/graph.ts +291 -0
  106. package/src/research/index.ts +7 -0
  107. package/src/research/instructions-research.tpl +97 -0
  108. package/src/research/research.test.ts +189 -0
  109. package/src/research/research.ts +89 -0
  110. package/src/session/index.ts +1 -0
  111. package/src/session/session.test.ts +39 -67
  112. package/src/session/session.ts +328 -155
  113. package/src/session/version-pin.test.ts +26 -0
  114. package/src/session/version-pin.ts +48 -0
  115. package/src/summarization/summarization.test.ts +216 -0
  116. package/src/testing/exa-search-1748337321991.ts +131 -0
  117. package/src/testing/exa-search-1748337331526.ts +144 -0
  118. package/src/testing/exa-search-1748337344119.ts +133 -0
  119. package/src/testing/index.ts +8 -7
  120. package/src/typedefs.d.ts +8 -0
  121. package/src/types.ts +8 -0
  122. package/src/typings.d.ts +8 -0
  123. package/dist/lib/browser/chunk-PND5VR6D.mjs +0 -2315
  124. package/dist/lib/browser/chunk-PND5VR6D.mjs.map +0 -7
  125. package/dist/lib/browser/testing/index.mjs +0 -621
  126. package/dist/lib/browser/testing/index.mjs.map +0 -7
  127. package/dist/lib/node/chunk-IBOBRBDF.cjs +0 -2326
  128. package/dist/lib/node/chunk-IBOBRBDF.cjs.map +0 -7
  129. package/dist/lib/node/testing/index.cjs +0 -637
  130. package/dist/lib/node/testing/index.cjs.map +0 -7
  131. package/dist/lib/node-esm/chunk-DCQJOLSR.mjs +0 -2317
  132. package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +0 -7
  133. package/dist/lib/node-esm/testing/index.mjs +0 -622
  134. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  135. package/dist/types/src/ai-service/client.test.d.ts +0 -2
  136. package/dist/types/src/ai-service/client.test.d.ts.map +0 -1
  137. package/dist/types/src/ai-service/defs.d.ts +0 -9
  138. package/dist/types/src/ai-service/defs.d.ts.map +0 -1
  139. package/dist/types/src/ai-service/edge-client.d.ts +0 -19
  140. package/dist/types/src/ai-service/edge-client.d.ts.map +0 -1
  141. package/dist/types/src/ai-service/index.d.ts +0 -10
  142. package/dist/types/src/ai-service/index.d.ts.map +0 -1
  143. package/dist/types/src/ai-service/message-collector.d.ts +0 -88
  144. package/dist/types/src/ai-service/message-collector.d.ts.map +0 -1
  145. package/dist/types/src/ai-service/ollama-client.d.ts +0 -39
  146. package/dist/types/src/ai-service/ollama-client.d.ts.map +0 -1
  147. package/dist/types/src/ai-service/parser.d.ts +0 -138
  148. package/dist/types/src/ai-service/parser.d.ts.map +0 -1
  149. package/dist/types/src/ai-service/service.d.ts +0 -9
  150. package/dist/types/src/ai-service/service.d.ts.map +0 -1
  151. package/dist/types/src/ai-service/stream.d.ts +0 -40
  152. package/dist/types/src/ai-service/stream.d.ts.map +0 -1
  153. package/dist/types/src/ai-service/stream.test.d.ts +0 -2
  154. package/dist/types/src/ai-service/stream.test.d.ts.map +0 -1
  155. package/dist/types/src/ai-service/testing/index.d.ts +0 -3
  156. package/dist/types/src/ai-service/testing/index.d.ts.map +0 -1
  157. package/dist/types/src/ai-service/testing/mock.d.ts +0 -7
  158. package/dist/types/src/ai-service/testing/mock.d.ts.map +0 -1
  159. package/dist/types/src/ai-service/testing/test-stream.d.ts +0 -7
  160. package/dist/types/src/ai-service/testing/test-stream.d.ts.map +0 -1
  161. package/dist/types/src/ai-service/transform.d.ts +0 -27
  162. package/dist/types/src/ai-service/transform.d.ts.map +0 -1
  163. package/dist/types/src/ai-service/types.d.ts +0 -167
  164. package/dist/types/src/ai-service/types.d.ts.map +0 -1
  165. package/dist/types/src/ai-service/util/index.d.ts +0 -2
  166. package/dist/types/src/ai-service/util/index.d.ts.map +0 -1
  167. package/dist/types/src/ai-service/util/sse.d.ts +0 -39
  168. package/dist/types/src/ai-service/util/sse.d.ts.map +0 -1
  169. package/dist/types/src/conversation/conversation.d.ts +0 -80
  170. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  171. package/dist/types/src/conversation/conversation.test.d.ts +0 -2
  172. package/dist/types/src/conversation/conversation.test.d.ts.map +0 -1
  173. package/dist/types/src/conversation/index.d.ts +0 -2
  174. package/dist/types/src/conversation/index.d.ts.map +0 -1
  175. package/dist/types/src/conversation/types.d.ts +0 -79
  176. package/dist/types/src/conversation/types.d.ts.map +0 -1
  177. package/dist/types/src/cypher/ast.d.ts +0 -56
  178. package/dist/types/src/cypher/ast.d.ts.map +0 -1
  179. package/dist/types/src/cypher/combinators.d.ts +0 -14
  180. package/dist/types/src/cypher/combinators.d.ts.map +0 -1
  181. package/dist/types/src/cypher/combinators.test.d.ts +0 -2
  182. package/dist/types/src/cypher/combinators.test.d.ts.map +0 -1
  183. package/dist/types/src/cypher/echo-data-source.d.ts +0 -16
  184. package/dist/types/src/cypher/echo-data-source.d.ts.map +0 -1
  185. package/dist/types/src/cypher/execution-plan.d.ts +0 -49
  186. package/dist/types/src/cypher/execution-plan.d.ts.map +0 -1
  187. package/dist/types/src/cypher/execution-plan.test.d.ts +0 -2
  188. package/dist/types/src/cypher/execution-plan.test.d.ts.map +0 -1
  189. package/dist/types/src/cypher/index.d.ts +0 -5
  190. package/dist/types/src/cypher/index.d.ts.map +0 -1
  191. package/dist/types/src/cypher/parjs.d.ts +0 -4
  192. package/dist/types/src/cypher/parjs.d.ts.map +0 -1
  193. package/dist/types/src/cypher/parser.d.ts +0 -6
  194. package/dist/types/src/cypher/parser.d.ts.map +0 -1
  195. package/dist/types/src/cypher/query-executor.d.ts +0 -26
  196. package/dist/types/src/cypher/query-executor.d.ts.map +0 -1
  197. package/dist/types/src/cypher/query-executor.test.d.ts +0 -2
  198. package/dist/types/src/cypher/query-executor.test.d.ts.map +0 -1
  199. package/dist/types/src/cypher/schema.d.ts +0 -12
  200. package/dist/types/src/cypher/schema.d.ts.map +0 -1
  201. package/dist/types/src/cypher/schema.test.d.ts +0 -2
  202. package/dist/types/src/cypher/schema.test.d.ts.map +0 -1
  203. package/dist/types/src/testing/artifacts.test.d.ts +0 -1
  204. package/dist/types/src/testing/artifacts.test.d.ts.map +0 -1
  205. package/dist/types/src/testing/data.d.ts +0 -2
  206. package/dist/types/src/testing/data.d.ts.map +0 -1
  207. package/dist/types/src/testing/defs.d.ts +0 -5
  208. package/dist/types/src/testing/defs.d.ts.map +0 -1
  209. package/dist/types/src/testing/logger.d.ts +0 -12
  210. package/dist/types/src/testing/logger.d.ts.map +0 -1
  211. package/dist/types/src/testing/object-extraction.test.d.ts +0 -1
  212. package/dist/types/src/testing/object-extraction.test.d.ts.map +0 -1
  213. package/dist/types/src/testing/ollama.d.ts +0 -6
  214. package/dist/types/src/testing/ollama.d.ts.map +0 -1
  215. package/dist/types/src/testing/query-promts.d.ts +0 -5
  216. package/dist/types/src/testing/query-promts.d.ts.map +0 -1
  217. package/dist/types/src/testing/query.test.d.ts +0 -2
  218. package/dist/types/src/testing/query.test.d.ts.map +0 -1
  219. package/dist/types/src/testing/test-data.d.ts +0 -29
  220. package/dist/types/src/testing/test-data.d.ts.map +0 -1
  221. package/dist/types/src/testing/test-schema.d.ts +0 -50
  222. package/dist/types/src/testing/test-schema.d.ts.map +0 -1
  223. package/dist/types/src/util/index.d.ts +0 -2
  224. package/dist/types/src/util/index.d.ts.map +0 -1
  225. package/dist/types/src/util/trim.d.ts +0 -5
  226. package/dist/types/src/util/trim.d.ts.map +0 -1
  227. package/src/ai-service/client.test.ts +0 -265
  228. package/src/ai-service/defs.ts +0 -39
  229. package/src/ai-service/edge-client.ts +0 -67
  230. package/src/ai-service/index.ts +0 -13
  231. package/src/ai-service/message-collector.ts +0 -84
  232. package/src/ai-service/ollama-client.ts +0 -499
  233. package/src/ai-service/parser.ts +0 -358
  234. package/src/ai-service/service.ts +0 -15
  235. package/src/ai-service/stream.test.ts +0 -125
  236. package/src/ai-service/stream.ts +0 -118
  237. package/src/ai-service/testing/index.ts +0 -6
  238. package/src/ai-service/testing/mock.ts +0 -22
  239. package/src/ai-service/testing/test-stream.ts +0 -93
  240. package/src/ai-service/transform.ts +0 -121
  241. package/src/ai-service/types.ts +0 -103
  242. package/src/ai-service/util/index.ts +0 -5
  243. package/src/ai-service/util/sse.ts +0 -341
  244. package/src/bin/chat.ts +0 -79
  245. package/src/conversation/conversation.test.ts +0 -73
  246. package/src/conversation/conversation.ts +0 -257
  247. package/src/conversation/index.ts +0 -5
  248. package/src/conversation/types.ts +0 -129
  249. package/src/cypher/ast.ts +0 -75
  250. package/src/cypher/combinators.test.ts +0 -137
  251. package/src/cypher/combinators.ts +0 -148
  252. package/src/cypher/echo-data-source.ts +0 -105
  253. package/src/cypher/execution-plan.test.ts +0 -44
  254. package/src/cypher/execution-plan.ts +0 -208
  255. package/src/cypher/index.ts +0 -8
  256. package/src/cypher/parjs-shims.d.ts +0 -10
  257. package/src/cypher/parjs.ts +0 -10
  258. package/src/cypher/parser.ts +0 -19
  259. package/src/cypher/query-executor.test.ts +0 -47
  260. package/src/cypher/query-executor.ts +0 -145
  261. package/src/cypher/schema.test.ts +0 -17
  262. package/src/cypher/schema.ts +0 -86
  263. package/src/testing/artifacts.test.ts +0 -182
  264. package/src/testing/data.ts +0 -13
  265. package/src/testing/defs.ts +0 -9
  266. package/src/testing/logger.ts +0 -129
  267. package/src/testing/object-extraction.test.ts +0 -71
  268. package/src/testing/ollama.ts +0 -15
  269. package/src/testing/query-promts.ts +0 -75
  270. package/src/testing/query.test.ts +0 -91
  271. package/src/testing/test-data.ts +0 -372
  272. package/src/testing/test-schema.ts +0 -80
  273. package/src/util/index.ts +0 -5
  274. package/src/util/trim.ts +0 -15
  275. package/vendor/parjs-browser.mjs +0 -4
  276. package/vendor/parjs-default.mjs +0 -8
  277. /package/dist/types/{src/bin → bin}/chat.d.ts +0 -0
@@ -1,93 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { ObjectId } from '@dxos/echo-schema';
6
-
7
- import { type GenerationStreamEvent } from '../types';
8
-
9
- /**
10
- * Mock server-side events (SSE) stream.
11
- */
12
- export const createTestSSEStream = (
13
- pars: (string | object)[],
14
- { splitBy = 'word' }: { splitBy?: 'word' | 'character' } = {},
15
- ): ReadableStream => {
16
- const encoder = new TextEncoder();
17
-
18
- return new ReadableStream({
19
- start: (controller) => {
20
- const push = (event: GenerationStreamEvent) => {
21
- controller.enqueue(encoder.encode(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`));
22
- };
23
-
24
- push({
25
- type: 'message_start',
26
- message: {
27
- id: ObjectId.random(),
28
- role: 'assistant',
29
- content: [],
30
- },
31
- });
32
-
33
- for (const block of pars) {
34
- let index = 0;
35
- if (typeof block === 'string') {
36
- push({
37
- type: 'content_block_start',
38
- index,
39
- content: {
40
- type: 'text',
41
- text: '',
42
- },
43
- });
44
-
45
- for (const word of block.split(splitBy === 'word' ? /([ \t\n]+)/ : '')) {
46
- push({
47
- type: 'content_block_delta',
48
- index,
49
- delta: {
50
- type: 'text_delta',
51
- text: word,
52
- },
53
- });
54
- index += word.length + 1;
55
- }
56
- } else {
57
- push({
58
- type: 'content_block_start',
59
- index,
60
- content: {
61
- type: 'json',
62
- json: '',
63
- },
64
- });
65
-
66
- const text = JSON.stringify(block, null, 2);
67
- for (const line of text.split('\n')) {
68
- push({
69
- type: 'content_block_delta',
70
- index,
71
- delta: {
72
- type: 'input_json_delta',
73
- partial_json: line,
74
- },
75
- });
76
- index += line.length + 1;
77
- }
78
- }
79
-
80
- push({
81
- type: 'content_block_stop',
82
- index,
83
- });
84
- }
85
-
86
- push({
87
- type: 'message_stop',
88
- });
89
-
90
- controller.close();
91
- },
92
- });
93
- };
@@ -1,121 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import * as P from 'parsimmon';
6
-
7
- /**
8
- * Parsed block.
9
- */
10
- export type StreamBlock =
11
- | {
12
- type: 'tag';
13
- tag: string;
14
- attributes?: Record<string, string>;
15
- content: StreamBlock[];
16
- closing?: boolean;
17
- selfClosing?: boolean;
18
- }
19
- | { type: 'text'; content: string }
20
- | { type: 'json'; disposition?: string; content: string };
21
-
22
- const textChunk: P.Parser<StreamBlock> = P.regexp(/[^<]+/).map((content) => ({
23
- type: 'text',
24
- content,
25
- }));
26
-
27
- const attribute: P.Parser<[string, string]> = P.seq(
28
- P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/),
29
- P.string('=').trim(P.optWhitespace),
30
- P.alt(
31
- P.regexp(/"[^"]*"/).map((s) => s.slice(1, -1)),
32
- P.regexp(/'[^']*'/).map((s) => s.slice(1, -1)),
33
- ),
34
- ).map(([name, _, value]) => [name, value]);
35
-
36
- const tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
37
-
38
- const selfClosingTag: P.Parser<StreamBlock> = P.seqMap(
39
- //
40
- P.string('<'),
41
- tagName,
42
- P.optWhitespace.then(attribute).many(),
43
- P.optWhitespace.then(P.string('/>')),
44
- P.optWhitespace,
45
- (_, tag, attributes, __) => ({
46
- type: 'tag',
47
- tag,
48
- attributes: Object.fromEntries(attributes),
49
- content: [],
50
- selfClosing: true,
51
- }),
52
- );
53
-
54
- const openTag: P.Parser<StreamBlock> = P.seqMap(
55
- //
56
- P.string('<'),
57
- tagName,
58
- P.optWhitespace.then(attribute).many(),
59
- P.string('>'),
60
- P.regexp(/[^<]*/),
61
- (_, tag, attributes, __, content) => ({
62
- type: 'tag',
63
- tag,
64
- attributes: Object.fromEntries(attributes),
65
- content: content.length ? [{ type: 'text', content }] : [],
66
- }),
67
- );
68
-
69
- const closeTag: P.Parser<StreamBlock> = P.seqMap(
70
- //
71
- P.string('</'),
72
- tagName,
73
- P.string('>'),
74
- P.optWhitespace,
75
- (_, tag) => ({
76
- type: 'tag',
77
- tag,
78
- content: [],
79
- closing: true,
80
- }),
81
- );
82
-
83
- const mixedChunk: P.Parser<StreamBlock> = P.alt(selfClosingTag, openTag, closeTag, textChunk);
84
-
85
- /**
86
- * Permissive streaming transformer that processes mixed content (plain text and XML fragments) from the AI service.
87
- */
88
- export class StreamTransform {
89
- private _buffer = '';
90
-
91
- transform(chunk: string): StreamBlock[] {
92
- this._buffer += chunk;
93
-
94
- const results: StreamBlock[] = [];
95
- const parser = mixedChunk.many().skip(P.optWhitespace);
96
- const result = parser.parse(this._buffer);
97
- if (result.status) {
98
- for (const chunk of result.value) {
99
- results.push(chunk);
100
- }
101
-
102
- this._buffer = '';
103
- }
104
-
105
- return results;
106
- }
107
-
108
- // TODO(burdon): Process remaining buffer?
109
- flush(): StreamBlock[] {
110
- const remaining = this._buffer;
111
- this._buffer = '';
112
- return remaining.length > 0
113
- ? [
114
- {
115
- type: 'text',
116
- content: remaining,
117
- },
118
- ]
119
- : [];
120
- }
121
- }
@@ -1,103 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema as S } from 'effect';
6
-
7
- import { Tool, Message, type MessageContentBlock, SpaceIdSchema } from '@dxos/artifact';
8
- import { ObjectId } from '@dxos/echo-schema';
9
-
10
- import { DEFAULT_EDGE_MODELS, DEFAULT_OLLAMA_MODELS } from './defs';
11
-
12
- export const createArtifactElement = (id: ObjectId) => `<artifact id=${id} />`;
13
-
14
- export const LLMModel = S.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
15
-
16
- export type LLMModel = S.Schema.Type<typeof LLMModel>;
17
-
18
- export const ToolTypes = Object.freeze({
19
- // TODO(dmaretskyi): Not implemented yet.
20
- // DatabaseQuery: 'database_query',
21
- TextToImage: 'text_to_image',
22
- });
23
-
24
- /**
25
- * Client GPT request.
26
- */
27
- export const GenerateRequest = S.Struct({
28
- spaceId: S.optional(SpaceIdSchema),
29
- threadId: S.optional(ObjectId),
30
-
31
- /**
32
- * Preferred model or system default.
33
- */
34
- model: S.optional(LLMModel),
35
-
36
- /**
37
- * Tools available for the LLM.
38
- */
39
- tools: S.optional(S.Array(Tool).pipe(S.mutable)),
40
-
41
- /**
42
- * System instructions to the LLM.
43
- */
44
- systemPrompt: S.optional(S.String),
45
-
46
- /**
47
- * History of messages to include in the context window.
48
- */
49
- // TODO(burdon): Rename messages.
50
- history: S.optional(S.Array(Message)),
51
-
52
- /**
53
- * Current request.
54
- */
55
- prompt: S.optional(Message),
56
- });
57
-
58
- export type GenerateRequest = S.Schema.Type<typeof GenerateRequest>;
59
-
60
- /**
61
- * Server-Sent Events (SSE) stream from the AI service.
62
- * https://docs.anthropic.com/en/api/streaming
63
- * https://platform.openai.com/docs/api-reference/streaming
64
- * https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
65
- */
66
- // TODO(dmaretskyi): Effect schema.
67
- export type GenerationStreamEvent =
68
- | {
69
- // TODO(dmaretskyi): Normalize types to our schema.
70
- type: 'message_start';
71
- message: Omit<Message, 'spaceId' | 'threadId'>;
72
- }
73
- | {
74
- type: 'message_delta';
75
- delta: {
76
- stopReason: 'tool_use' | 'end_turn';
77
- };
78
- }
79
- | {
80
- type: 'message_stop';
81
- }
82
- | {
83
- type: 'content_block_start';
84
- index: number;
85
- content: MessageContentBlock;
86
- }
87
- | {
88
- type: 'content_block_delta';
89
- index: number;
90
- delta:
91
- | {
92
- type: 'text_delta';
93
- text: string;
94
- }
95
- | {
96
- type: 'input_json_delta';
97
- partial_json: string;
98
- };
99
- }
100
- | {
101
- type: 'content_block_stop';
102
- index: number;
103
- };
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export * from './sse';
@@ -1,341 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- // Copied from anthropic SDK source
4
- //
5
-
6
- /**
7
- * Server sent events (SSE) iterator.
8
- * @param response
9
- * @param controller
10
- */
11
- export async function* iterSSEMessages(
12
- response: Response,
13
- controller: AbortController,
14
- ): AsyncGenerator<ServerSentEvent, void, unknown> {
15
- if (!response.body) {
16
- controller.abort();
17
- throw new Error('Attempted to iterate over a response with no body');
18
- }
19
-
20
- const sseDecoder = new SSEDecoder();
21
- const lineDecoder = new LineDecoder();
22
-
23
- const iter = readableStreamAsyncIterable<Bytes>(response.body);
24
- for await (const sseChunk of iterSSEChunks(iter)) {
25
- for (const line of lineDecoder.decode(sseChunk)) {
26
- const sse = sseDecoder.decode(line);
27
- if (sse) {
28
- yield sse;
29
- }
30
- }
31
- }
32
-
33
- for (const line of lineDecoder.flush()) {
34
- const sse = sseDecoder.decode(line);
35
- if (sse) {
36
- yield sse;
37
- }
38
- }
39
- }
40
-
41
- export type ServerSentEvent = {
42
- event: string | null;
43
- data: string;
44
- raw: string[];
45
- };
46
-
47
- /**
48
- * Given an async iterable iterator, iterates over it and yields full
49
- * SSE chunks, i.e. yields when a double new-line is encountered.
50
- */
51
- async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
52
- let data = new Uint8Array();
53
-
54
- for await (const chunk of iterator) {
55
- if (chunk == null) {
56
- continue;
57
- }
58
-
59
- const binaryChunk =
60
- chunk instanceof ArrayBuffer
61
- ? new Uint8Array(chunk)
62
- : typeof chunk === 'string'
63
- ? new TextEncoder().encode(chunk)
64
- : chunk;
65
-
66
- const newData = new Uint8Array(data.length + binaryChunk.length);
67
- newData.set(data);
68
- newData.set(binaryChunk, data.length);
69
- data = newData;
70
-
71
- let patternIndex;
72
- while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
73
- yield data.slice(0, patternIndex);
74
- data = data.slice(patternIndex);
75
- }
76
- }
77
-
78
- if (data.length > 0) {
79
- yield data;
80
- }
81
- }
82
-
83
- const findDoubleNewlineIndex = (buffer: Uint8Array): number => {
84
- // This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
85
- // and returns the index right after the first occurrence of any pattern,
86
- // or -1 if none of the patterns are found.
87
- const newline = 0x0a; // \n
88
- const carriage = 0x0d; // \r
89
-
90
- for (let i = 0; i < buffer.length - 2; i++) {
91
- if (buffer[i] === newline && buffer[i + 1] === newline) {
92
- // \n\n
93
- return i + 2;
94
- }
95
- if (buffer[i] === carriage && buffer[i + 1] === carriage) {
96
- // \r\r
97
- return i + 2;
98
- }
99
- if (
100
- buffer[i] === carriage &&
101
- buffer[i + 1] === newline &&
102
- i + 3 < buffer.length &&
103
- buffer[i + 2] === carriage &&
104
- buffer[i + 3] === newline
105
- ) {
106
- // \r\n\r\n
107
- return i + 4;
108
- }
109
- }
110
-
111
- return -1;
112
- };
113
-
114
- class SSEDecoder {
115
- private data: string[];
116
- private event: string | null;
117
- private chunks: string[];
118
-
119
- constructor() {
120
- this.event = null;
121
- this.data = [];
122
- this.chunks = [];
123
- }
124
-
125
- decode(line: string) {
126
- if (line.endsWith('\r')) {
127
- line = line.substring(0, line.length - 1);
128
- }
129
-
130
- if (!line) {
131
- // empty line and we didn't previously encounter any messages
132
- if (!this.event && !this.data.length) {
133
- return null;
134
- }
135
-
136
- const sse: ServerSentEvent = {
137
- event: this.event,
138
- data: this.data.join('\n'),
139
- raw: this.chunks,
140
- };
141
-
142
- this.event = null;
143
- this.data = [];
144
- this.chunks = [];
145
-
146
- return sse;
147
- }
148
-
149
- this.chunks.push(line);
150
-
151
- if (line.startsWith(':')) {
152
- return null;
153
- }
154
-
155
- let [fieldname, _, value] = partition(line, ':');
156
-
157
- if (value.startsWith(' ')) {
158
- value = value.substring(1);
159
- }
160
-
161
- if (fieldname === 'event') {
162
- this.event = value;
163
- } else if (fieldname === 'data') {
164
- this.data.push(value);
165
- }
166
-
167
- return null;
168
- }
169
- }
170
-
171
- /** This is an internal helper function that's just used for testing */
172
- export const _decodeChunks = (chunks: string[]): string[] => {
173
- const decoder = new LineDecoder();
174
- const lines: string[] = [];
175
- for (const chunk of chunks) {
176
- lines.push(...decoder.decode(chunk));
177
- }
178
-
179
- return lines;
180
- };
181
-
182
- const partition = (str: string, delimiter: string): [string, string, string] => {
183
- const index = str.indexOf(delimiter);
184
- if (index !== -1) {
185
- return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
186
- }
187
-
188
- return [str, '', ''];
189
- };
190
-
191
- /**
192
- * Most browsers don't yet have async iterable support for ReadableStream,
193
- * and Node has a very different way of reading bytes from its "ReadableStream".
194
- *
195
- * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
196
- */
197
- export function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
198
- if (stream[Symbol.asyncIterator]) {
199
- return stream;
200
- }
201
-
202
- const reader = stream.getReader();
203
- return {
204
- next: async () => {
205
- try {
206
- const result = await reader.read();
207
- if (result?.done) {
208
- reader.releaseLock();
209
- } // release lock when stream becomes closed
210
- return result;
211
- } catch (e) {
212
- reader.releaseLock(); // release lock when stream becomes errored
213
- throw e;
214
- }
215
- },
216
- return: async () => {
217
- const cancelPromise = reader.cancel();
218
- reader.releaseLock();
219
- await cancelPromise;
220
- return { done: true, value: undefined };
221
- },
222
- [Symbol.asyncIterator]() {
223
- return this;
224
- },
225
- };
226
- }
227
-
228
- export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
229
-
230
- /**
231
- * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
232
- * reading lines from text.
233
- *
234
- * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
235
- */
236
- export class LineDecoder {
237
- // prettier-ignore
238
- static NEWLINE_CHARS = new Set(['\n', '\r']);
239
- static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
240
-
241
- buffer: string[];
242
- trailingCR: boolean;
243
- textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
244
-
245
- constructor() {
246
- this.buffer = [];
247
- this.trailingCR = false;
248
- }
249
-
250
- decode(chunk: Bytes): string[] {
251
- let text = this.decodeText(chunk);
252
-
253
- if (this.trailingCR) {
254
- text = '\r' + text;
255
- this.trailingCR = false;
256
- }
257
- if (text.endsWith('\r')) {
258
- this.trailingCR = true;
259
- text = text.slice(0, -1);
260
- }
261
-
262
- if (!text) {
263
- return [];
264
- }
265
-
266
- const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
267
- let lines = text.split(LineDecoder.NEWLINE_REGEXP);
268
-
269
- // if there is a trailing new line then the last entry will be an empty
270
- // string which we don't care about
271
- if (trailingNewline) {
272
- lines.pop();
273
- }
274
-
275
- if (lines.length === 1 && !trailingNewline) {
276
- this.buffer.push(lines[0]!);
277
- return [];
278
- }
279
-
280
- if (this.buffer.length > 0) {
281
- lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
282
- this.buffer = [];
283
- }
284
-
285
- if (!trailingNewline) {
286
- this.buffer = [lines.pop() || ''];
287
- }
288
-
289
- return lines;
290
- }
291
-
292
- decodeText(bytes: Bytes): string {
293
- if (bytes == null) {
294
- return '';
295
- }
296
- if (typeof bytes === 'string') {
297
- return bytes;
298
- }
299
-
300
- // Node:
301
- if (typeof Buffer !== 'undefined') {
302
- if (bytes instanceof Buffer) {
303
- return bytes.toString();
304
- }
305
- if (bytes instanceof Uint8Array) {
306
- return Buffer.from(bytes).toString();
307
- }
308
-
309
- throw new Error(
310
- `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`,
311
- );
312
- }
313
-
314
- // Browser
315
- if (typeof TextDecoder !== 'undefined') {
316
- if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
317
- this.textDecoder ??= new TextDecoder('utf8');
318
- return this.textDecoder.decode(bytes);
319
- }
320
-
321
- throw new Error(
322
- `Unexpected: received non-Uint8Array/ArrayBuffer (${
323
- (bytes as any).constructor.name
324
- }) in a web platform. Please report this error.`,
325
- );
326
- }
327
-
328
- throw new Error('Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.');
329
- }
330
-
331
- flush(): string[] {
332
- if (!this.buffer.length && !this.trailingCR) {
333
- return [];
334
- }
335
-
336
- const lines = [this.buffer.join('')];
337
- this.buffer = [];
338
- this.trailingCR = false;
339
- return lines;
340
- }
341
- }