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

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
package/src/bin/chat.ts DELETED
@@ -1,79 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import inquirer from 'inquirer';
6
- import { writeFileSync } from 'node:fs';
7
-
8
- import { createUserMessage } from '@dxos/artifact';
9
- import { ObjectId } from '@dxos/echo-schema';
10
- import { SpaceId } from '@dxos/keys';
11
- import { log } from '@dxos/log';
12
-
13
- import { AIServiceEdgeClient, ToolTypes, DEFAULT_EDGE_MODEL } from '../ai-service';
14
- import { runLLM } from '../conversation';
15
- import {
16
- AI_SERVICE_ENDPOINT,
17
- createLogger,
18
- createCypherTool,
19
- createSystemPrompt,
20
- createTestData,
21
- Contact,
22
- Org,
23
- Project,
24
- Task,
25
- } from '../testing';
26
-
27
- // TOOD(burdon): Get from config.
28
- const client = new AIServiceEdgeClient({
29
- endpoint: AI_SERVICE_ENDPOINT.LOCAL,
30
- });
31
-
32
- const dataSource = createTestData();
33
-
34
- const cypherTool = createCypherTool(dataSource);
35
-
36
- const schemaTypes = [Org, Project, Task, Contact];
37
-
38
- const spaceId = SpaceId.random();
39
- const threadId = ObjectId.random();
40
-
41
- while (true) {
42
- const prompt = await inquirer.prompt([
43
- {
44
- type: 'input',
45
- name: 'message',
46
- message: 'Enter a message:',
47
- },
48
- ]);
49
- await client.appendMessages([createUserMessage(spaceId, threadId, prompt.message)]);
50
-
51
- await runLLM({
52
- model: DEFAULT_EDGE_MODEL,
53
- spaceId,
54
- threadId,
55
- system: createSystemPrompt(schemaTypes),
56
- tools: [
57
- cypherTool,
58
- {
59
- name: 'text-to-image',
60
- type: ToolTypes.TextToImage,
61
- },
62
- ],
63
- client,
64
- logger: createLogger({
65
- stream: true,
66
- filter: (e) => {
67
- return true;
68
- },
69
- onImage: (img) => {
70
- const path = `/tmp/image-${img.id}.jpeg`;
71
- writeFileSync(path, Buffer.from(img.source.data, 'base64'));
72
- log('Saved image', { path });
73
- // Print image in iTerm using ANSI escape sequence
74
- const imageData = img.source.data;
75
- process.stdout.write('\x1b]1337;File=inline=1:' + imageData + '\x07');
76
- },
77
- }),
78
- });
79
- }
@@ -1,73 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Schema as S } from 'effect';
6
- import { describe, test } from 'vitest';
7
-
8
- import { createUserMessage, defineTool, ToolResult } from '@dxos/artifact';
9
- import { ObjectId } from '@dxos/echo-schema';
10
- import { SpaceId } from '@dxos/keys';
11
- import { log } from '@dxos/log';
12
-
13
- import { runLLM, type ConversationEvent } from './conversation';
14
- import { AIServiceEdgeClient, DEFAULT_EDGE_MODEL } from '../ai-service';
15
- import { AI_SERVICE_ENDPOINT } from '../testing';
16
-
17
- // TODO(burdon): Local live LLM test.
18
- describe.skip('Conversation tests', () => {
19
- const client = new AIServiceEdgeClient({
20
- endpoint: AI_SERVICE_ENDPOINT.LOCAL,
21
- });
22
-
23
- test('basic', async ({ expect }) => {
24
- const spaceId = SpaceId.random();
25
- const threadId = ObjectId.random();
26
-
27
- const result = await runLLM({
28
- model: DEFAULT_EDGE_MODEL,
29
- history: [createUserMessage(spaceId, threadId, 'Hello, how are you?')],
30
- tools: [],
31
- client,
32
- logger: messageLogger,
33
- });
34
-
35
- log('result', { result });
36
- expect(result.history.length).to.equal(2);
37
- });
38
-
39
- test('tool call', async ({ expect }) => {
40
- const custodian = defineTool('testing', {
41
- name: 'custodian',
42
- description: 'Custodian can tell you the password if you say the magic word',
43
- schema: S.Struct({
44
- magicWord: S.String.annotations({ description: 'The magic word. Should be exactly "pretty please"' }),
45
- }),
46
- execute: async ({ magicWord }) => {
47
- if (magicWord === 'pretty please') {
48
- return ToolResult.Success('The password is: "The sky is gray"');
49
- } else {
50
- return ToolResult.Error('Wrong magic word');
51
- }
52
- },
53
- });
54
-
55
- const spaceId = SpaceId.random();
56
- const threadId = ObjectId.random();
57
-
58
- const result = await runLLM({
59
- model: DEFAULT_EDGE_MODEL,
60
- history: [createUserMessage(spaceId, threadId, 'What is the password? Ask the custodian.')],
61
- tools: [custodian],
62
- client,
63
- logger: messageLogger,
64
- });
65
- log('result', { result });
66
- });
67
-
68
- const messageLogger = (event: ConversationEvent) => {
69
- if (event.type === 'message') {
70
- log('message', { message: event.message });
71
- }
72
- };
73
- });
@@ -1,257 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type Tool, ToolResult, Message } from '@dxos/artifact';
6
- import { createStatic, ObjectId } from '@dxos/echo-schema';
7
- import { invariant } from '@dxos/invariant';
8
- import { type SpaceId } from '@dxos/keys';
9
- import { log } from '@dxos/log';
10
-
11
- import { type AIServiceClient, type GenerationStreamEvent, type LLMModel } from '../ai-service';
12
-
13
- export type CreateLLMConversationParams = {
14
- model: LLMModel;
15
-
16
- spaceId?: SpaceId;
17
- threadId?: ObjectId;
18
-
19
- /**
20
- * System prompt that specifies instructions for the LLM.
21
- */
22
- system?: string;
23
-
24
- tools: Tool[];
25
- history?: Message[];
26
- client: AIServiceClient;
27
-
28
- logger?: (event: ConversationEvent) => void;
29
- };
30
-
31
- export type ConversationEvent =
32
- | {
33
- type: 'message';
34
- message: Message;
35
- }
36
- | GenerationStreamEvent;
37
-
38
- // TODO(burdon): Replace with processor from plugin-automation?
39
- export const runLLM = async (params: CreateLLMConversationParams) => {
40
- let conversationResult: any = null;
41
- const history = params.history ?? [];
42
-
43
- const generate = async () => {
44
- log('llm generate', { tools: params.tools });
45
- const beginTs = Date.now();
46
- const stream = await params.client.exec({
47
- model: params.model,
48
- spaceId: params.spaceId,
49
- threadId: params.threadId,
50
- history,
51
- systemPrompt: params.system,
52
- tools: params.tools as any,
53
- });
54
-
55
- for await (const event of stream) {
56
- params.logger?.(event);
57
- }
58
-
59
- // TODO(burdon): !!!
60
- await stream.complete();
61
- const messages: Message[] = [];
62
- const message = messages.at(-1);
63
- invariant(message);
64
-
65
- log('llm result', { time: Date.now() - beginTs, message });
66
- invariant(message);
67
- params.logger?.({ type: 'message', message });
68
- history.push(
69
- ...messages.map(
70
- (message): Message => ({ ...message, content: message.content.filter((block) => block.type !== 'image') }),
71
- ),
72
- );
73
-
74
- const isToolUse = message.content.at(-1)?.type === 'tool_use';
75
- if (isToolUse) {
76
- const toolCalls = message.content.filter((block) => block.type === 'tool_use');
77
- invariant(toolCalls.length === 1);
78
- const toolCall = toolCalls[0];
79
- const tool = params.tools.find((tool) => tool.name === toolCall.name);
80
- if (!tool) {
81
- throw new Error(`Tool not found: ${toolCall.name}`);
82
- }
83
-
84
- invariant(tool.execute);
85
- const toolResult = await tool.execute(toolCall.input);
86
- switch (toolResult.kind) {
87
- case 'error': {
88
- log.warn('tool error', { message: toolResult.message });
89
- const resultMessage: Message = {
90
- id: ObjectId.random(),
91
- spaceId: params.spaceId,
92
- threadId: params.threadId,
93
- role: 'user',
94
- content: [
95
- {
96
- type: 'tool_result',
97
- toolUseId: toolCall.id,
98
- content: toolResult.message,
99
- isError: true,
100
- },
101
- ],
102
- };
103
- params.logger?.({ type: 'message', message: resultMessage });
104
- history.push(resultMessage);
105
-
106
- return true;
107
- }
108
-
109
- case 'success': {
110
- log('tool success', { result: toolResult.result });
111
- const resultMessage: Message = {
112
- id: ObjectId.random(),
113
- spaceId: params.spaceId,
114
- threadId: params.threadId,
115
- role: 'user',
116
- content: [
117
- {
118
- type: 'tool_result',
119
- toolUseId: toolCall.id,
120
- content: JSON.stringify(toolResult.result),
121
- },
122
- ],
123
- };
124
- params.logger?.({ type: 'message', message: resultMessage });
125
- history.push(resultMessage);
126
-
127
- return true;
128
- }
129
-
130
- case 'break': {
131
- log('tool break', { result: toolResult.result });
132
- conversationResult = toolResult.result;
133
- return false;
134
- }
135
- }
136
- }
137
-
138
- return false;
139
- };
140
-
141
- // eslint-disable-next-line no-empty
142
- while (await generate()) {}
143
-
144
- return {
145
- result: conversationResult,
146
- history,
147
- };
148
- };
149
-
150
- export const isToolUse = (message: Message, { onlyToolNames }: { onlyToolNames?: string[] } = {}) => {
151
- const block = message.content.at(-1);
152
- invariant(block);
153
- return block.type === 'tool_use' && (!onlyToolNames || onlyToolNames.includes(block.name));
154
- };
155
-
156
- export type RunToolsOptions = {
157
- message: Message;
158
- tools: Tool[];
159
- extensions?: ToolContextExtensions;
160
- };
161
-
162
- export type RunToolsResult =
163
- | {
164
- type: 'continue';
165
- message: Message;
166
- }
167
- | {
168
- type: 'break';
169
- result: unknown;
170
- };
171
-
172
- export const runTools = async ({ message, tools, extensions }: RunToolsOptions): Promise<RunToolsResult> => {
173
- const toolCalls = message.content.filter((block) => block.type === 'tool_use');
174
- invariant(toolCalls.length === 1);
175
- const toolCall = toolCalls[0];
176
- const tool = tools.find((tool) => tool.name === toolCall.name);
177
- if (!tool) {
178
- const resultMessage = createStatic(Message, {
179
- role: 'user',
180
- content: [
181
- {
182
- type: 'tool_result',
183
- toolUseId: toolCall.id,
184
- content: `Tool not found: ${toolCall.name}`,
185
- isError: true,
186
- },
187
- ],
188
- });
189
-
190
- // TODO(burdon): Retry count?
191
- return {
192
- type: 'continue',
193
- message: resultMessage,
194
- };
195
- }
196
-
197
- let toolResult: ToolResult;
198
- try {
199
- invariant(tool.execute);
200
- toolResult = await tool.execute(toolCall.input, { extensions });
201
- } catch (error: any) {
202
- log('tool error', { error });
203
- toolResult = ToolResult.Error(error.message);
204
- }
205
-
206
- switch (toolResult.kind) {
207
- case 'error': {
208
- log('tool error', { message: toolResult.message });
209
- const resultMessage = createStatic(Message, {
210
- role: 'user',
211
- content: [
212
- {
213
- type: 'tool_result',
214
- toolUseId: toolCall.id,
215
- content: toolResult.message,
216
- isError: true,
217
- },
218
- ],
219
- });
220
-
221
- // TODO(burdon): Retry count?
222
- return {
223
- type: 'continue',
224
- message: resultMessage,
225
- };
226
- }
227
-
228
- case 'success': {
229
- log('tool success', { result: toolResult.result });
230
- const resultMessage = createStatic(Message, {
231
- role: 'user',
232
- content: [
233
- {
234
- type: 'tool_result',
235
- toolUseId: toolCall.id,
236
- content:
237
- typeof toolResult.result === 'string' ? toolResult.result : JSON.stringify(toolResult.result) ?? '',
238
- },
239
- ...(toolResult.extractContentBlocks ?? []),
240
- ],
241
- });
242
-
243
- return {
244
- type: 'continue',
245
- message: resultMessage,
246
- };
247
- }
248
-
249
- case 'break': {
250
- log('tool break', { result: toolResult.result });
251
- return {
252
- type: 'break',
253
- result: toolResult.result,
254
- };
255
- }
256
- }
257
- };
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- export * from './conversation';
@@ -1,129 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Schema as S } from 'effect';
6
-
7
- import { type Message, type MessageContentBlock } from '@dxos/artifact';
8
- import { toJsonSchema, JsonSchemaType, ObjectId } from '@dxos/echo-schema';
9
- import type { SpaceId } from '@dxos/keys';
10
- import { log } from '@dxos/log';
11
-
12
- export const createUserMessage = (spaceId: SpaceId, threadId: ObjectId, text: string): Message => ({
13
- id: ObjectId.random(),
14
- spaceId,
15
- threadId,
16
- role: 'user',
17
- content: [{ type: 'text', text }],
18
- });
19
-
20
- /**
21
- * @deprecated Use {@link LLMTool} instead.
22
- */
23
- export const ToolDefinition = S.Struct({
24
- name: S.String,
25
- description: S.String,
26
- parameters: JsonSchemaType,
27
- execute: S.Any,
28
- });
29
-
30
- /**
31
- * @deprecated Use {@link LLMTool} instead.
32
- */
33
- export type ToolDefinition = {
34
- name: string;
35
- description: string;
36
- parameters: JsonSchemaType;
37
- execute: (params: unknown, context: ToolExecutionContext) => Promise<ToolResult>;
38
- };
39
-
40
- declare global {
41
- /**
42
- * Extensions to the tool execution context.
43
- *
44
- * Different modules can extend this definition to add their own properties.
45
- *
46
- * @example
47
- * ```ts
48
- * declare global {
49
- * interface ToolContextExtensions {
50
- * chess: {
51
- * board: string;
52
- * };
53
- * }
54
- * }
55
- * ```
56
- *
57
- * @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
58
- */
59
- interface ToolContextExtensions {}
60
- }
61
-
62
- export type ToolExecutionContext = {
63
- extensions: ToolContextExtensions;
64
- };
65
-
66
- export type ToolResult =
67
- | { kind: 'success'; result: unknown; extractContentBlocks?: MessageContentBlock[] }
68
- | { kind: 'error'; message: string }
69
- | { kind: 'break'; result: unknown };
70
-
71
- export const ToolResult = Object.freeze({
72
- /**
73
- * The tool execution was successful.
74
- * Gives the result back to the LLM.
75
- */
76
- Success: (result: unknown, extractContentBlocks?: MessageContentBlock[]): ToolResult => ({
77
- kind: 'success',
78
- result,
79
- extractContentBlocks,
80
- }),
81
-
82
- Error: (message: string): ToolResult => ({ kind: 'error', message }),
83
- /**
84
- * Stop the conversation and return the result.
85
- */
86
- Break: (result: unknown): ToolResult => ({ kind: 'break', result }),
87
- });
88
-
89
- export type DefineToolParams<Params extends S.Schema.AnyNoContext> = {
90
- name: string;
91
- description: string;
92
- schema: Params;
93
- execute: (params: S.Schema.Type<Params>, context: ToolExecutionContext) => Promise<ToolResult>;
94
- };
95
-
96
- export const defineTool = <Params extends S.Schema.AnyNoContext>({
97
- name,
98
- description,
99
- schema,
100
- execute,
101
- }: DefineToolParams<Params>): ToolDefinition => {
102
- return {
103
- name,
104
- description,
105
- parameters: toFunctionParameterSchema(schema),
106
- execute: (params, context) => {
107
- const sanitized = S.decodeSync(schema)(params);
108
- return execute(sanitized, context);
109
- },
110
- };
111
- };
112
-
113
- /**
114
- * Adapts schems to be able to pass to AI providers.
115
- */
116
- const toFunctionParameterSchema = (schema: S.Schema.All) => {
117
- const jsonSchema = toJsonSchema(schema);
118
- log('tool schema', { jsonSchema });
119
- delete jsonSchema.anyOf;
120
- delete jsonSchema.$id;
121
- jsonSchema.type = 'object';
122
- jsonSchema.properties ??= {};
123
- for (const key in jsonSchema.properties) {
124
- if (typeof jsonSchema.properties![key].description !== 'string') {
125
- throw new Error(`Property missing description: ${key}`);
126
- }
127
- }
128
- return jsonSchema;
129
- };
package/src/cypher/ast.ts DELETED
@@ -1,75 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- // TODO(burdon): Why interface not type?
6
-
7
- export interface AstNode<Kind extends string> {
8
- astKind: Kind;
9
- }
10
-
11
- export interface Identifier extends AstNode<'Identifier'> {
12
- name: string;
13
- }
14
-
15
- export interface Property {
16
- key: Identifier;
17
- value: StringLiteral;
18
- }
19
-
20
- export interface Properties extends AstNode<'Properties'> {
21
- properties: Property[];
22
- }
23
-
24
- export interface NodePattern extends AstNode<'NodePattern'> {
25
- variable: Identifier | null;
26
- label: Identifier;
27
- properties: Properties | null;
28
- }
29
-
30
- export interface RelationshipPattern extends AstNode<'RelationshipPattern'> {
31
- variable: Identifier | null;
32
- label: Identifier;
33
- properties: Properties | null;
34
- }
35
-
36
- export interface GraphPatternConnector extends AstNode<'GraphPatternConnector'> {
37
- direction: '-' | '->' | '<-';
38
- }
39
-
40
- export interface GraphPattern extends AstNode<'GraphPattern'> {
41
- segments: (NodePattern | RelationshipPattern)[] & {
42
- separators: GraphPatternConnector[];
43
- };
44
- }
45
-
46
- export interface MatchClause extends AstNode<'MatchClause'> {
47
- pattern: GraphPattern;
48
- }
49
-
50
- export interface StringLiteral extends AstNode<'StringLiteral'> {
51
- value: string;
52
- }
53
-
54
- export interface MemberExpression extends AstNode<'MemberExpression'> {
55
- path: Identifier[];
56
- }
57
-
58
- export interface EqualsPredicate extends AstNode<'EqualsPredicate'> {
59
- left: MemberExpression;
60
- right: StringLiteral;
61
- }
62
-
63
- export interface WhereClause extends AstNode<'WhereClause'> {
64
- predicates: EqualsPredicate[];
65
- }
66
-
67
- export interface ReturnClause extends AstNode<'ReturnClause'> {
68
- fields: MemberExpression[];
69
- }
70
-
71
- export interface CypherQuery extends AstNode<'CypherQuery'> {
72
- matchClause: MatchClause;
73
- whereClause: WhereClause | null;
74
- returnClause: ReturnClause;
75
- }