@dxos/assistant 0.8.4-main.67995b8 → 0.8.4-main.a4bbb77

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 (166) hide show
  1. package/dist/lib/browser/chunk-ILL2CN3G.mjs +374 -0
  2. package/dist/lib/browser/chunk-ILL2CN3G.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-6WRGDEMH.mjs → chunk-UB26N5UF.mjs} +2 -2
  4. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +32 -29
  6. package/dist/lib/browser/extraction/index.mjs.map +4 -4
  7. package/dist/lib/browser/index.mjs +682 -1035
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/{named-entity-recognition-O46UTCLR.mjs → named-entity-recognition-QPYHML66.mjs} +2 -2
  11. package/dist/lib/node-esm/{chunk-3UU5V4FS.mjs → chunk-BCFXTNSK.mjs} +2 -2
  12. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
  13. package/dist/lib/node-esm/chunk-GVKA7VL3.mjs +376 -0
  14. package/dist/lib/node-esm/chunk-GVKA7VL3.mjs.map +7 -0
  15. package/dist/lib/node-esm/extraction/index.mjs +32 -29
  16. package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
  17. package/dist/lib/node-esm/index.mjs +682 -1035
  18. package/dist/lib/node-esm/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/{named-entity-recognition-W4VMRCH6.mjs → named-entity-recognition-2HDF46YX.mjs} +2 -2
  21. package/dist/types/src/{context/binding.d.ts → conversation/context.d.ts} +45 -3
  22. package/dist/types/src/conversation/context.d.ts.map +1 -0
  23. package/dist/types/src/conversation/conversation.d.ts +24 -21
  24. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  25. package/dist/types/src/conversation/index.d.ts +1 -0
  26. package/dist/types/src/conversation/index.d.ts.map +1 -1
  27. package/dist/types/src/diff.d.ts +15 -0
  28. package/dist/types/src/diff.d.ts.map +1 -0
  29. package/dist/types/src/diff.test.d.ts +2 -0
  30. package/dist/types/src/diff.test.d.ts.map +1 -0
  31. package/dist/types/src/errors.d.ts +46 -9
  32. package/dist/types/src/errors.d.ts.map +1 -1
  33. package/dist/types/src/execution-graph.d.ts +82 -0
  34. package/dist/types/src/execution-graph.d.ts.map +1 -0
  35. package/dist/types/src/execution-graph.test.d.ts +2 -0
  36. package/dist/types/src/execution-graph.test.d.ts.map +1 -0
  37. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  38. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  39. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +94 -55
  40. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  41. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  42. package/dist/types/src/extraction/extraction.d.ts +250 -216
  43. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  44. package/dist/types/src/functions/services.d.ts +15 -3
  45. package/dist/types/src/functions/services.d.ts.map +1 -1
  46. package/dist/types/src/index.d.ts +3 -2
  47. package/dist/types/src/index.d.ts.map +1 -1
  48. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  49. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  50. package/dist/types/src/session/error-handling.d.ts +4 -0
  51. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  52. package/dist/types/src/session/format.d.ts +184 -0
  53. package/dist/types/src/session/format.d.ts.map +1 -0
  54. package/dist/types/src/session/format.test.d.ts +2 -0
  55. package/dist/types/src/session/format.test.d.ts.map +1 -0
  56. package/dist/types/src/session/index.d.ts +3 -0
  57. package/dist/types/src/session/index.d.ts.map +1 -1
  58. package/dist/types/src/session/observer.d.ts +22 -0
  59. package/dist/types/src/session/observer.d.ts.map +1 -0
  60. package/dist/types/src/session/session.d.ts +23 -236
  61. package/dist/types/src/session/session.d.ts.map +1 -1
  62. package/dist/types/src/session/toolkit.d.ts +14 -0
  63. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  64. package/dist/types/src/session/version-pin.d.ts +2 -2
  65. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  66. package/dist/types/src/templates/index.d.ts +1 -1
  67. package/dist/types/src/templates/system.d.ts +18 -0
  68. package/dist/types/src/templates/system.d.ts.map +1 -0
  69. package/dist/types/src/types.d.ts +3 -0
  70. package/dist/types/src/types.d.ts.map +1 -1
  71. package/dist/types/tsconfig.tsbuildinfo +1 -1
  72. package/package.json +34 -33
  73. package/src/conversation/context.ts +154 -0
  74. package/src/conversation/conversation.ts +76 -53
  75. package/src/conversation/index.ts +1 -0
  76. package/src/diff.test.ts +43 -0
  77. package/src/diff.ts +79 -0
  78. package/src/errors.ts +6 -1
  79. package/src/execution-graph.test.ts +738 -0
  80. package/src/execution-graph.ts +500 -0
  81. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +8 -7
  82. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +12 -10
  83. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
  84. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +13 -6
  85. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -1
  86. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
  87. package/src/extraction/quotes.test.ts +3 -3
  88. package/src/extraction/quotes.ts +2 -2
  89. package/src/functions/services.ts +97 -43
  90. package/src/index.ts +3 -2
  91. package/src/session/artifact-diff.ts +41 -0
  92. package/src/session/error-handling.ts +40 -0
  93. package/src/session/format.test.ts +60 -0
  94. package/src/session/format.ts +139 -0
  95. package/src/session/index.ts +3 -0
  96. package/src/session/observer.ts +58 -0
  97. package/src/session/session.conversations.json +1 -0
  98. package/src/session/session.test.ts +33 -197
  99. package/src/session/session.ts +119 -249
  100. package/src/session/toolkit.ts +46 -0
  101. package/src/session/version-pin.ts +4 -4
  102. package/src/summarization/summarization.test.ts +9 -7
  103. package/src/templates/format.tpl +60 -0
  104. package/src/templates/index.ts +1 -1
  105. package/src/templates/system.tpl +48 -0
  106. package/src/templates/system.ts +41 -0
  107. package/src/types.ts +10 -5
  108. package/src/typings.d.ts +5 -0
  109. package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +0 -7
  110. package/dist/lib/browser/chunk-SIXTNSGV.mjs +0 -250
  111. package/dist/lib/browser/chunk-SIXTNSGV.mjs.map +0 -7
  112. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +0 -7
  113. package/dist/lib/node-esm/chunk-FD5NXKDP.mjs +0 -252
  114. package/dist/lib/node-esm/chunk-FD5NXKDP.mjs.map +0 -7
  115. package/dist/types/bin/chat.d.ts +0 -2
  116. package/dist/types/bin/chat.d.ts.map +0 -1
  117. package/dist/types/src/context/binder.d.ts +0 -34
  118. package/dist/types/src/context/binder.d.ts.map +0 -1
  119. package/dist/types/src/context/binding.d.ts.map +0 -1
  120. package/dist/types/src/context/index.d.ts +0 -3
  121. package/dist/types/src/context/index.d.ts.map +0 -1
  122. package/dist/types/src/research/exa.d.ts +0 -12
  123. package/dist/types/src/research/exa.d.ts.map +0 -1
  124. package/dist/types/src/research/graph.d.ts +0 -51
  125. package/dist/types/src/research/graph.d.ts.map +0 -1
  126. package/dist/types/src/research/graph.test.d.ts +0 -2
  127. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  128. package/dist/types/src/research/index.d.ts +0 -4
  129. package/dist/types/src/research/index.d.ts.map +0 -1
  130. package/dist/types/src/research/research.d.ts +0 -10
  131. package/dist/types/src/research/research.d.ts.map +0 -1
  132. package/dist/types/src/research/research.test.d.ts +0 -2
  133. package/dist/types/src/research/research.test.d.ts.map +0 -1
  134. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  135. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  136. package/dist/types/src/templates/prompt.d.ts +0 -30
  137. package/dist/types/src/templates/prompt.d.ts.map +0 -1
  138. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  139. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  140. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  141. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  142. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  143. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  144. package/dist/types/src/testing/index.d.ts +0 -3
  145. package/dist/types/src/testing/index.d.ts.map +0 -1
  146. package/src/context/binder.ts +0 -103
  147. package/src/context/binding.ts +0 -32
  148. package/src/context/index.ts +0 -6
  149. package/src/research/exa.ts +0 -97
  150. package/src/research/graph.test.ts +0 -64
  151. package/src/research/graph.ts +0 -369
  152. package/src/research/index.ts +0 -7
  153. package/src/research/instructions-research.tpl +0 -98
  154. package/src/research/research.test.ts +0 -190
  155. package/src/research/research.ts +0 -87
  156. package/src/session/__snapshots__/session.test.ts.snap +0 -32
  157. package/src/session/version-pin.test.ts +0 -7
  158. package/src/templates/assistant.tpl +0 -111
  159. package/src/templates/prompt.ts +0 -45
  160. package/src/testing/exa-search-1748337321991.ts +0 -131
  161. package/src/testing/exa-search-1748337331526.ts +0 -144
  162. package/src/testing/exa-search-1748337344119.ts +0 -133
  163. package/src/testing/index.ts +0 -11
  164. package/src/typedefs.d.ts +0 -8
  165. /package/dist/lib/browser/{named-entity-recognition-O46UTCLR.mjs.map → named-entity-recognition-QPYHML66.mjs.map} +0 -0
  166. /package/dist/lib/node-esm/{named-entity-recognition-W4VMRCH6.mjs.map → named-entity-recognition-2HDF46YX.mjs.map} +0 -0
@@ -0,0 +1,48 @@
1
+ {{! System Prompt }}
2
+
3
+ - You are a helpful assistant called Kai.
4
+ - Your name is Kai and you are powered by different language models.
5
+ - You were created by DXOS and operate inside of Composer and on the DXOS EDGE network.
6
+ - In your initial greeting state your name and make some suggestions based on the current context objects.
7
+ - You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.
8
+ - Your task is to process user commands and questions and decide how best to respond.
9
+ - In general be concise and direct.
10
+ - Follow all instructions carefully.
11
+
12
+ ## Planning
13
+
14
+ - Analyze the structure and type of the content in the user's message.
15
+ - Determine if you complete the task using the available blueprint definitions?
16
+ - If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.
17
+ - Identify which blueprint definitions are relevant to the user's request.
18
+ - Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.
19
+ - Make a final decision on whether to create an artifact and explain your reasoning.
20
+ - Are the required blueprint definitions already available?
21
+ - If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.
22
+ - The require'd artifact tools will be available for use after require.
23
+ - If creating an artifact, outline how you will structure it within the response.
24
+ - Decide if the artifact needs to be shown to the user.
25
+ - Call the show tool to show the artifact to the user.
26
+ - Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.
27
+
28
+ ## Blueprints and Artifacts
29
+
30
+ - Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.
31
+ - Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.
32
+ - If it is ambiguous, query for existing artifacts first and then decide.
33
+ - If you decide to create an artifact, call the associated tool to create the artifact.
34
+ - Artifacts are stored in the database. Tools are used to create and query artifacts.
35
+ - If you are unsure about creating an artifact ask the user for clarification.
36
+ - Artifacts are mutable objects that can change over the course of the conversation.
37
+ - Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.
38
+ - You must never generate the id of the artifact; only recall the ids that are already in the history.
39
+ - Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.
40
+ - You can add suggested actions at the end of your response.
41
+ - Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.
42
+ - Suggested actions must be in the form of a user instruction that you can follow.
43
+ - Suggested actions could include actions that create artifacts.
44
+ - After creating an artifact use a tool to add it to the chat context.
45
+
46
+ ## Context
47
+
48
+ Today is {{DATETIME}}.
@@ -0,0 +1,41 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import defaultsDeep from 'lodash.defaultsdeep';
6
+
7
+ import { type ModelCapabilities } from '@dxos/ai';
8
+ import { Template } from '@dxos/blueprints';
9
+
10
+ import FORMAT from './format.tpl?raw';
11
+ import SYSTEM from './system.tpl?raw';
12
+
13
+ /**
14
+ * System template variables.
15
+ */
16
+ export type SystemPromptOptions = ModelCapabilities & {
17
+ DATETIME: string;
18
+ };
19
+
20
+ /**
21
+ * Base system prompt.
22
+ * NOTE: This contains protocol instructions that can not be dynamically edited.
23
+ */
24
+ export const createSystemPrompt = (options: Partial<SystemPromptOptions> = {}) =>
25
+ Template.process<SystemPromptOptions>(
26
+ FORMAT,
27
+ defaultsDeep(options, {
28
+ DATETIME: new Date().toISOString(),
29
+ }),
30
+ );
31
+
32
+ export const templates = {
33
+ /** Editable system prompt.*/
34
+ 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
+ source: SYSTEM,
40
+ }),
41
+ };
package/src/types.ts CHANGED
@@ -6,21 +6,26 @@ import { Schema } from 'effect';
6
6
 
7
7
  import { type ObjectId } from '@dxos/echo-schema';
8
8
  import { DXN, LOCAL_SPACE_TAG, type SpaceId } from '@dxos/keys';
9
+ import { trim } from '@dxos/util';
9
10
 
11
+ /**
12
+ * @deprecated
13
+ */
10
14
  export const createArtifactElement = (id: ObjectId) => `<artifact id=${id} />`;
11
15
 
12
16
  /**
13
17
  * A model-friendly way to reference an object.
14
18
  * Supports vairous formats that will be normalized to a DXN.
15
19
  */
20
+ // TODO(burdon): Rename ObjectReference?
16
21
  export const ArtifactId: Schema.Schema<string> & {
17
22
  toDXN: (reference: ArtifactId, owningSpaceId?: SpaceId) => DXN;
18
23
  } = class extends Schema.String.annotations({
19
- description: `
20
- The ID of the referenced object. Formats accepted:
21
- - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
22
- - space ID, object ID tuple (spaceID:objectID)
23
- - Only object ID that is assumed to be in the current space (XXXXX)
24
+ description: trim`
25
+ The ID of the referenced object. Formats accepted:
26
+ - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
27
+ - space ID, object ID tuple (spaceID:objectID)
28
+ - Only object ID that is assumed to be in the current space (XXXXX)
24
29
  `,
25
30
  examples: ['dxn:echo:@:XXXXX', '@dxn:echo:@:XXXXX', 'spaceID:objectID', 'XXXXX'],
26
31
  }) {
package/src/typings.d.ts CHANGED
@@ -2,6 +2,11 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ declare module '*?raw' {
6
+ const content: string;
7
+ export default content;
8
+ }
9
+
5
10
  declare module '*.tpl?raw' {
6
11
  const content: string;
7
12
  export default content;
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/extraction/extraction-ner-function/named-entity-recognition.ts"],
4
- "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport {\n type TokenClassificationOutput,\n type TokenClassificationPipelineType,\n type TokenClassificationSingle,\n pipeline,\n} from '@xenova/transformers';\n\nimport { log } from '@dxos/log';\n\nlet _ner: Promise<TokenClassificationPipelineType>;\n/**\n * Named Entity Recognition pipeline.\n * Initializes the pipeline on first call.\n * @returns The singleton promise that resolves to a token classification pipeline.\n */\nexport const getNer = () => {\n if (!_ner) {\n _ner = pipeline('ner', 'Xenova/bert-base-NER').then((ner) => {\n log.info('NER model is ready');\n return ner;\n });\n }\n return _ner;\n};\n\n/**\n * Extracts entities from text using the named entity recognition pipeline and does simple grouping of tokens.\n */\nexport const extractFullEntities = async (text: string): Promise<TokenClassificationSingle[]> => {\n const ner = await getNer();\n const entities = (await ner(text)) as TokenClassificationOutput;\n const tokenGroups = createTokenGroups(entities);\n const combinedEntities = tokenGroups.map(combineNerTokens);\n return combinedEntities;\n};\n\nexport const createTokenGroups = (tokens: TokenClassificationSingle[]): TokenClassificationSingle[][] => {\n const tokenGroups: TokenClassificationSingle[][] = [];\n\n const canBeCombined = (prevToken: TokenClassificationSingle, currentToken: TokenClassificationSingle) => {\n const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O\n const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O\n if (prevEntity !== currentEntity) {\n return false;\n }\n if (prevState === 'O') {\n return false;\n }\n if (currentState === 'I') {\n return true;\n }\n if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {\n return currentToken.word.startsWith('#');\n }\n return false;\n };\n\n for (const token of tokens) {\n if (tokenGroups.length === 0) {\n tokenGroups.push([token]);\n continue;\n }\n const lastTokenGroup = tokenGroups.at(-1)!;\n if (canBeCombined(lastTokenGroup.at(-1)!, token)) {\n lastTokenGroup.push(token);\n continue;\n }\n tokenGroups.push([token]);\n }\n\n return tokenGroups;\n};\n\nexport const combineNerTokens = (group: TokenClassificationSingle[]): TokenClassificationSingle => {\n const combinedToken: TokenClassificationSingle = {\n entity: group.at(0)!.entity.split('-')[1],\n score: group.reduce((acc, token) => acc + token.score, 0) / group.length,\n index: group.at(0)!.index,\n word: group.at(0)!.word,\n start: group.at(0)!.start,\n end: group.at(-1)!.end,\n };\n\n for (const token of group.slice(1)) {\n if (token.word.startsWith('#')) {\n combinedToken.word += token.word.replace(/^#+/, '');\n } else {\n combinedToken.word += ' ' + token.word;\n }\n }\n\n return combinedToken;\n};\n"],
5
- "mappings": ";AAIA,SAIEA,gBACK;AAEP,SAASC,WAAW;;AAEpB,IAAIC;AAMG,IAAMC,SAAS,MAAA;AACpB,MAAI,CAACD,MAAM;AACTA,WAAOF,SAAS,OAAO,sBAAA,EAAwBI,KAAK,CAACC,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EAAK,CAAA;IACvCY,OAAOF,MAAMG,OAAO,CAACC,KAAKT,UAAUS,MAAMT,MAAMO,OAAO,CAAA,IAAKF,MAAMJ;IAClES,OAAOL,MAAMD,GAAG,CAAA,EAAIM;IACpBZ,MAAMO,MAAMD,GAAG,CAAA,EAAIN;IACnBa,OAAON,MAAMD,GAAG,CAAA,EAAIO;IACpBC,KAAKP,MAAMD,GAAG,EAAC,EAAIQ;EACrB;AAEA,aAAWZ,SAASK,MAAMQ,MAAM,CAAA,GAAI;AAClC,QAAIb,MAAMF,KAAKC,WAAW,GAAA,GAAM;AAC9BO,oBAAcR,QAAQE,MAAMF,KAAKgB,QAAQ,OAAO,EAAA;IAClD,OAAO;AACLR,oBAAcR,QAAQ,MAAME,MAAMF;IACpC;EACF;AAEA,SAAOQ;AACT;",
6
- "names": ["pipeline", "log", "_ner", "getNer", "then", "ner", "info", "extractFullEntities", "text", "entities", "tokenGroups", "createTokenGroups", "combinedEntities", "map", "combineNerTokens", "tokens", "canBeCombined", "prevToken", "currentToken", "currentState", "currentEntity", "entity", "split", "prevState", "prevEntity", "word", "startsWith", "token", "length", "push", "lastTokenGroup", "at", "group", "combinedToken", "score", "reduce", "acc", "index", "start", "end", "slice", "replace"]
7
- }
@@ -1,250 +0,0 @@
1
- // src/session/session.ts
2
- import { AiLanguageModel, AiToolkit } from "@effect/ai";
3
- import { Chunk, Context, Effect, Option, pipe, Stream } from "effect";
4
- import { Array, String } from "effect";
5
- import { AiParser, AiPreprocessor, getToolCalls, runTool, ToolExecutionService, ToolResolverService } from "@dxos/ai";
6
- import { Event } from "@dxos/async";
7
- import { todo } from "@dxos/debug";
8
- import { Obj } from "@dxos/echo";
9
- import { ObjectVersion } from "@dxos/echo-db";
10
- import { DatabaseService } from "@dxos/functions";
11
- import { log } from "@dxos/log";
12
- import { DataType } from "@dxos/schema";
13
-
14
- // src/errors.ts
15
- import { BaseError } from "@dxos/errors";
16
- var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR") {
17
- };
18
-
19
- // src/session/session.ts
20
- var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
21
- var AiSession = class {
22
- constructor(_options = {}) {
23
- this._options = _options;
24
- this._semaphore = Effect.runSync(Effect.makeSemaphore(1));
25
- this._pending = [];
26
- this._history = [];
27
- this.message = new Event();
28
- this.block = new Event();
29
- this.update = new Event();
30
- this.streamEvent = new Event();
31
- this.userMessage = new Event();
32
- this.statusReport = new Event();
33
- this.done = new Event();
34
- this.run = (options) => Effect.gen(this, function* () {
35
- this._history = [
36
- ...options.history
37
- ];
38
- const promptMessages = yield* this._formatUserPrompt(options.prompt, options.history);
39
- this._pending = [
40
- promptMessages
41
- ];
42
- this.userMessage.emit(promptMessages);
43
- do {
44
- log("request", {
45
- pending: this._pending.length,
46
- history: this._history.length,
47
- tools: Object.values(options.toolkit?.tools ?? {}).map((tool) => tool.name)
48
- }, {
49
- F: __dxlog_file,
50
- L: 126,
51
- S: this,
52
- C: (f, a) => f(...a)
53
- });
54
- const prompt = yield* AiPreprocessor.preprocessAiInput([
55
- ...this._history,
56
- ...this._pending
57
- ]);
58
- const blueprints = options.blueprints ?? [];
59
- const systemPrompt = yield* pipe(blueprints, Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)), Effect.flatMap(Effect.forEach((template) => DatabaseService.loadRef(template.source))), Effect.map(Array.map((template) => `
60
-
61
- <blueprint>${template.content}</blueprint>`)), Effect.map(Array.reduce(options.systemPrompt ?? "", String.concat)));
62
- const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap((bp) => bp.tools));
63
- const blueprintToolkitHandler = yield* blueprintToolkit.toContext(yield* ToolExecutionService.handlersFor(blueprintToolkit));
64
- const toolkit = options.toolkit != null ? AiToolkit.merge(options.toolkit, blueprintToolkit) : blueprintToolkit;
65
- const toolkitWithBlueprintHandlers = yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler));
66
- log.info("run", {
67
- systemPrompt: [
68
- systemPrompt.slice(0, 32),
69
- "...",
70
- systemPrompt.slice(-32),
71
- systemPrompt.length
72
- ].join(" "),
73
- tools: Object.keys(blueprintToolkit.tools)
74
- }, {
75
- F: __dxlog_file,
76
- L: 155,
77
- S: this,
78
- C: (f, a) => f(...a)
79
- });
80
- const blocks = yield* AiLanguageModel.streamText({
81
- prompt,
82
- system: systemPrompt,
83
- toolkit: toolkitWithBlueprintHandlers,
84
- disableToolCallResolution: true
85
- }).pipe(AiParser.parseGptStream({
86
- onBlock: (block) => Effect.gen(this, function* () {
87
- if (block.pending) {
88
- this.update.emit(block);
89
- } else {
90
- this.block.emit(block);
91
- }
92
- }),
93
- onPart: (part) => Effect.gen(this, function* () {
94
- this.streamEvent.emit(part);
95
- })
96
- }), Stream.runCollect, Effect.map(Chunk.toArray));
97
- const response = Obj.make(DataType.Message, {
98
- created: (/* @__PURE__ */ new Date()).toISOString(),
99
- sender: {
100
- role: "assistant"
101
- },
102
- blocks
103
- });
104
- this._pending.push(response);
105
- this.message.emit(response);
106
- const toolCalls = getToolCalls(response);
107
- if (toolCalls.length === 0) {
108
- break;
109
- }
110
- const toolResults = yield* runTools(toolCalls, toolkitWithBlueprintHandlers);
111
- this._pending.push(Obj.make(DataType.Message, {
112
- created: (/* @__PURE__ */ new Date()).toISOString(),
113
- sender: {
114
- role: "user"
115
- },
116
- blocks: toolResults
117
- }));
118
- } while (true);
119
- return this._pending;
120
- }).pipe(this._semaphore.withPermits(1), Effect.withSpan("AiSession.run"));
121
- this._formatUserPrompt = (prompt, history) => Effect.gen(function* () {
122
- const prelude = [];
123
- const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
124
- if (Option.isSome(artifactDiffResolver)) {
125
- const versions = gatherObjectVersions(history);
126
- const artifactDiff = yield* Effect.tryPromise({
127
- try: () => artifactDiffResolver.value.resolve([
128
- ...versions.entries()
129
- ].map(([id, version]) => ({
130
- id,
131
- lastVersion: version
132
- }))),
133
- catch: AiAssistantError.wrap("Artifact diff resolution error")
134
- });
135
- log.info("vision", {
136
- artifactDiff,
137
- versions
138
- }, {
139
- F: __dxlog_file,
140
- L: 240,
141
- S: this,
142
- C: (f, a) => f(...a)
143
- });
144
- for (const [id, { version }] of [
145
- ...artifactDiff.entries()
146
- ]) {
147
- if (ObjectVersion.equals(version, versions.get(id))) {
148
- artifactDiff.delete(id);
149
- continue;
150
- }
151
- prelude.push({
152
- _tag: "anchor",
153
- objectId: id,
154
- version
155
- });
156
- }
157
- if (artifactDiff.size > 0) {
158
- prelude.push(createArtifactUpdateBlock(artifactDiff));
159
- }
160
- }
161
- return Obj.make(DataType.Message, {
162
- created: (/* @__PURE__ */ new Date()).toISOString(),
163
- sender: {
164
- role: "user"
165
- },
166
- blocks: [
167
- ...prelude,
168
- {
169
- _tag: "text",
170
- text: prompt
171
- }
172
- ]
173
- });
174
- });
175
- }
176
- async runStructured(schema, options) {
177
- return todo();
178
- }
179
- abort() {
180
- this._stream?.abort();
181
- }
182
- };
183
- var gatherObjectVersions = (messages) => {
184
- const artifactIds = /* @__PURE__ */ new Map();
185
- for (const message of messages) {
186
- for (const block of message.blocks) {
187
- if (block._tag === "anchor") {
188
- artifactIds.set(block.objectId, block.version);
189
- }
190
- }
191
- }
192
- return artifactIds;
193
- };
194
- var createArtifactUpdateBlock = (artifactDiff) => {
195
- return {
196
- _tag: "text",
197
- // TODO(dmaretskyi): Does this need to be a special content-block?
198
- disposition: "artifact-update",
199
- text: `
200
- The following artifacts have been updated since the last message:
201
- ${[
202
- ...artifactDiff.entries()
203
- ].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
204
- ${diff}` : ""}</changed-artifact>`).join("\n")}
205
- `
206
- };
207
- };
208
- var ArtifactDiffResolver = class extends Context.Tag("@dxos/assistant/ArtifactDiffResolver")() {
209
- };
210
- var runTools = Effect.fn("runTools")(function* (toolCalls, toolkit) {
211
- const toolkitWithHandlers = Effect.isEffect(toolkit) ? yield* toolkit : toolkit;
212
- return yield* Effect.forEach(toolCalls, (toolCall) => runTool(toolkitWithHandlers, toolCall));
213
- });
214
-
215
- // src/session/version-pin.ts
216
- import { Schema } from "effect";
217
- import { getVersion } from "@dxos/echo-db";
218
- import { ObjectId } from "@dxos/echo-schema";
219
- var ObjectVersionSchema = Schema.Unknown;
220
- var VersionPinSchema = Schema.Struct({
221
- // TODO(dmaretskyi): Use Ref when those support encoding.
222
- objectId: ObjectId,
223
- // TODO(dmaretskyi): Could be opaque
224
- version: ObjectVersionSchema
225
- });
226
- var VersionPin = class extends VersionPinSchema {
227
- static {
228
- this.DISPOSITION = "version-pin";
229
- }
230
- static fromObject(object) {
231
- return VersionPin.make({
232
- objectId: object.id,
233
- version: getVersion(object)
234
- });
235
- }
236
- static createBlock(pin) {
237
- return {
238
- _tag: "json",
239
- disposition: VersionPin.DISPOSITION,
240
- data: JSON.stringify(pin)
241
- };
242
- }
243
- };
244
-
245
- export {
246
- AiSession,
247
- ArtifactDiffResolver,
248
- VersionPin
249
- };
250
- //# sourceMappingURL=chunk-SIXTNSGV.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/session/session.ts", "../../../src/errors.ts", "../../../src/session/version-pin.ts"],
4
- "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport { AiLanguageModel, AiToolkit, type AiError, type AiResponse, type AiTool } from '@effect/ai';\nimport { Chunk, Context, Effect, Option, pipe, Stream, type Schema } from 'effect';\nimport { Array, String } from 'effect';\n\nimport {\n AiParser,\n AiPreprocessor,\n getToolCalls,\n runTool,\n ToolExecutionService,\n ToolResolverService,\n type AgentStatus,\n type AiInputPreprocessingError,\n type AiToolNotFoundError,\n type GenerationStream,\n} from '@dxos/ai';\nimport { Event } from '@dxos/async';\nimport { type Blueprint } from '@dxos/blueprints';\nimport { todo } from '@dxos/debug';\nimport { Obj } from '@dxos/echo';\nimport { ObjectVersion } from '@dxos/echo-db';\nimport { type ObjectId } from '@dxos/echo-schema';\nimport { DatabaseService } from '@dxos/functions';\nimport { log } from '@dxos/log';\nimport { DataType, type ContentBlock } from '@dxos/schema';\n\nimport { AiAssistantError } from '../errors';\n\nexport type AiSessionOptions = {};\n\nexport type SessionRunOptions<Tools extends AiTool.Any> = {\n prompt: string;\n history: DataType.Message[];\n systemPrompt?: string;\n toolkit?: AiToolkit.AiToolkit<Tools>;\n blueprints?: Blueprint.Blueprint[];\n};\n\n/**\n * Contains message history, tools, current context.\n * Current context means the state of the app, time of day, and other contextual information.\n * It makes requests to the model, its a state machine.\n * It keeps track of the current goal.\n * It manages the context window.\n * Tracks the success criteria of reaching the goal, exposing metrics (stretch).\n * Could be run locally in the app or remotely.\n * Could be personal or shared.\n */\nexport class AiSession {\n // TODO(burdon): Move to conversation.\n private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));\n\n /** Pending messages (incl. the current user request). */\n private _pending: DataType.Message[] = [];\n\n /** Current streaming response. */\n private _stream: GenerationStream | undefined;\n\n /** Prior history from queue. */\n private _history: DataType.Message[] = [];\n\n /**\n * New message.\n */\n public readonly message = new Event<DataType.Message>();\n\n /**\n * Complete block added to Message.\n */\n public readonly block = new Event<ContentBlock.Any>();\n\n /**\n * Update partial block (while streaming).\n */\n public readonly update = new Event<ContentBlock.Any>();\n\n /**\n * Unparsed events from the underlying generation stream.\n */\n public readonly streamEvent = new Event<AiResponse.Part>();\n\n /**\n * User prompt or tool result.\n */\n public readonly userMessage = new Event<DataType.Message>();\n\n /**\n * Agent self-reporting its status.\n * Triggered by the model.\n */\n public readonly statusReport = new Event<AgentStatus>();\n\n /**\n * Emits when the session is done.\n */\n public readonly done = new Event<void>();\n\n constructor(private readonly _options: AiSessionOptions = {}) {}\n\n /**\n * Runs the AI model loop interacting with tools and artifacts.\n * @param options - The session options.\n * @returns The messages generated by the session, including the user's prompt.\n */\n // TODO(dmaretskyi): Toolkit context doesn't get added to the effect type.\n run = <Tools extends AiTool.Any>(\n options: SessionRunOptions<Tools>,\n ): Effect.Effect<\n DataType.Message[],\n AiAssistantError | AiInputPreprocessingError | AiToolNotFoundError | AiError.AiError,\n AiLanguageModel.AiLanguageModel | ToolResolverService | ToolExecutionService | AiTool.ToHandler<Tools>\n > =>\n Effect.gen(this, function* () {\n this._history = [...options.history];\n\n const promptMessages = yield* this._formatUserPrompt(options.prompt, options.history);\n this._pending = [promptMessages];\n this.userMessage.emit(promptMessages);\n\n // Potential tool use loop.\n do {\n log('request', {\n pending: this._pending.length,\n history: this._history.length,\n tools: (Object.values(options.toolkit?.tools ?? {}) as AiTool.Any[]).map((tool: AiTool.Any) => tool.name),\n });\n\n const prompt = yield* AiPreprocessor.preprocessAiInput([...this._history, ...this._pending]);\n\n const blueprints = options.blueprints ?? [];\n\n // Build system prompt from blueprint templates.\n // TODO(dmaretskyi): Loading BP from the Database should be done at the higher level. We need a type for the resolved blueprint.\n const systemPrompt = yield* pipe(\n blueprints,\n Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),\n Effect.flatMap(Effect.forEach((template) => DatabaseService.loadRef(template.source))),\n Effect.map(Array.map((template) => `\\n\\n<blueprint>${template.content}</blueprint>`)),\n Effect.map(Array.reduce(options.systemPrompt ?? '', String.concat)),\n );\n\n // Build a combined toolkit from the blueprint tools and the provided toolkit.\n const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap((bp) => bp.tools));\n const blueprintToolkitHandler: Context.Context<AiTool.ToHandler<AiTool.Any>> =\n yield* blueprintToolkit.toContext(yield* ToolExecutionService.handlersFor(blueprintToolkit));\n const toolkit = options.toolkit != null ? AiToolkit.merge(options.toolkit, blueprintToolkit) : blueprintToolkit;\n const toolkitWithBlueprintHandlers = yield* toolkit.pipe(\n Effect.provide(blueprintToolkitHandler) as any,\n ) as Effect.Effect<AiToolkit.ToHandler<any>, never, AiTool.ToHandler<Tools>>;\n\n log.info('run', {\n systemPrompt: [systemPrompt.slice(0, 32), '...', systemPrompt.slice(-32), systemPrompt.length].join(' '),\n tools: Object.keys(blueprintToolkit.tools),\n });\n\n const blocks = yield* AiLanguageModel.streamText({\n prompt,\n system: systemPrompt,\n toolkit: toolkitWithBlueprintHandlers,\n disableToolCallResolution: true,\n }).pipe(\n AiParser.parseGptStream({\n onBlock: (block) =>\n Effect.gen(this, function* () {\n if (block.pending) {\n this.update.emit(block);\n } else {\n this.block.emit(block);\n }\n }),\n onPart: (part) =>\n Effect.gen(this, function* () {\n this.streamEvent.emit(part);\n }),\n }),\n Stream.runCollect,\n Effect.map(Chunk.toArray),\n );\n\n const response = Obj.make(DataType.Message, {\n created: new Date().toISOString(),\n sender: { role: 'assistant' },\n blocks,\n });\n this._pending.push(response);\n this.message.emit(response);\n\n const toolCalls = getToolCalls(response);\n if (toolCalls.length === 0) {\n break;\n }\n\n const toolResults = yield* runTools(toolCalls, toolkitWithBlueprintHandlers as any);\n this._pending.push(\n Obj.make(DataType.Message, {\n created: new Date().toISOString(),\n sender: { role: 'user' },\n blocks: toolResults,\n }),\n );\n } while (true);\n\n return this._pending;\n }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));\n\n async runStructured<S extends Schema.Schema.AnyNoContext>(\n schema: S,\n options: SessionRunOptions<AiTool.Any>,\n ): Promise<Schema.Schema.Type<S>> {\n return todo();\n // const parser = structuredOutputParser(schema);\n // const result = await this.run({\n // ...options,\n // executableTools: [...(options.executableTools ?? []), parser.tool],\n // });\n // return parser.getResult(result);\n }\n\n private _formatUserPrompt = (prompt: string, history: DataType.Message[]) =>\n Effect.gen(function* () {\n const prelude: ContentBlock.Any[] = [];\n\n // TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.\n const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);\n if (Option.isSome(artifactDiffResolver)) {\n const versions = gatherObjectVersions(history);\n\n const artifactDiff = yield* Effect.tryPromise({\n try: () =>\n artifactDiffResolver.value.resolve(\n [...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),\n ),\n catch: AiAssistantError.wrap('Artifact diff resolution error'),\n });\n\n log.info('vision', {\n artifactDiff,\n versions,\n });\n\n for (const [id, { version }] of [...artifactDiff.entries()]) {\n if (ObjectVersion.equals(version, versions.get(id)!)) {\n artifactDiff.delete(id);\n continue;\n }\n\n prelude.push({ _tag: 'anchor', objectId: id, version });\n }\n if (artifactDiff.size > 0) {\n prelude.push(createArtifactUpdateBlock(artifactDiff));\n }\n }\n\n return Obj.make(DataType.Message, {\n created: new Date().toISOString(),\n sender: { role: 'user' },\n blocks: [...prelude, { _tag: 'text', text: prompt }],\n });\n });\n\n abort(): void {\n this._stream?.abort();\n }\n}\n\nconst gatherObjectVersions = (messages: DataType.Message[]): Map<ObjectId, ObjectVersion> => {\n const artifactIds = new Map<ObjectId, ObjectVersion>();\n for (const message of messages) {\n for (const block of message.blocks) {\n if (block._tag === 'anchor') {\n artifactIds.set(block.objectId, block.version as ObjectVersion);\n }\n }\n }\n\n return artifactIds;\n};\n\nconst createArtifactUpdateBlock = (\n artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,\n): ContentBlock.Any => {\n return {\n _tag: 'text',\n // TODO(dmaretskyi): Does this need to be a special content-block?\n disposition: 'artifact-update',\n text: `\n The following artifacts have been updated since the last message:\n ${[...artifactDiff.entries()]\n .map(([id, { diff }]) => `<changed-artifact id=\"${id}\">${diff ? `\\n${diff}` : ''}</changed-artifact>`)\n .join('\\n')}\n `,\n };\n};\n\n/**\n * Resolves artifact ids to their versions.\n * Used to give the model a sense of the changes to the artifacts made by users during the conversation.\n * The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,\n * a diff is inserted into the conversation.\n *\n * Can be optionally provided to the session run call.\n */\n// TODO(dmaretskyi): Convert to Context.Reference\nexport class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<\n ArtifactDiffResolver,\n ArtifactDiffResolver.Service\n>() {}\n\nexport namespace ArtifactDiffResolver {\n export type Service = {\n resolve: (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<\n Map<\n ObjectId,\n {\n version: ObjectVersion;\n diff?: string;\n }\n >\n >;\n };\n}\n\nconst runTools: <Tools extends AiTool.Any>(\n toolCalls: ContentBlock.ToolCall[],\n toolkit: AiToolkit.AiToolkit<Tools>,\n) => Effect.Effect<ContentBlock.ToolResult[], AiError.AiError, AiTool.ToHandler<Tools>> = Effect.fn('runTools')(\n function* (toolCalls, toolkit) {\n const toolkitWithHandlers = Effect.isEffect(toolkit) ? yield* toolkit : toolkit;\n\n return yield* Effect.forEach(toolCalls, (toolCall) => runTool(toolkitWithHandlers, toolCall));\n },\n);\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { BaseError } from '@dxos/errors';\n\n/**\n * Generic error for AI agent execution.\n */\nexport class AiAssistantError extends BaseError.extend('AI_ASSISTANT_ERROR') {}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { type ObjectVersion } from '@dxos/echo-db';\nimport { getVersion } from '@dxos/echo-db';\nimport { ObjectId, type BaseEchoObject } from '@dxos/echo-schema';\nimport { type ContentBlock } from '@dxos/schema';\n\n// TODO(dmaretskyi): Extract.\nconst ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;\n\nconst VersionPinSchema = Schema.Struct({\n // TODO(dmaretskyi): Use Ref when those support encoding.\n objectId: ObjectId,\n // TODO(dmaretskyi): Could be opaque\n version: ObjectVersionSchema,\n});\n\n/**\n * Used to pin a specific version of an object during the course of a conversation.\n * This allows us to detect when the object changes during the course of the conversation.\n */\nexport interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema> {}\n\nexport const VersionPin: typeof VersionPinSchema & {\n DISPOSITION: 'version-pin';\n fromObject: (object: BaseEchoObject) => VersionPin;\n createBlock: (pin: VersionPin) => ContentBlock.Any;\n} = class extends VersionPinSchema {\n static readonly DISPOSITION = 'version-pin';\n static fromObject(object: BaseEchoObject): VersionPin {\n return VersionPin.make({\n objectId: object.id,\n version: getVersion(object),\n });\n }\n\n static createBlock(pin: VersionPin): ContentBlock.Any {\n return {\n _tag: 'json',\n disposition: VersionPin.DISPOSITION,\n data: JSON.stringify(pin),\n };\n }\n};\n"],
5
- "mappings": ";AAIA,SAASA,iBAAiBC,iBAA6D;AACvF,SAASC,OAAOC,SAASC,QAAQC,QAAQC,MAAMC,cAA2B;AAC1E,SAASC,OAAOC,cAAc;AAE9B,SACEC,UACAC,gBACAC,cACAC,SACAC,sBACAC,2BAKK;AACP,SAASC,aAAa;AAEtB,SAASC,YAAY;AACrB,SAASC,WAAW;AACpB,SAASC,qBAAqB;AAE9B,SAASC,uBAAuB;AAChC,SAASC,WAAW;AACpB,SAASC,gBAAmC;;;ACxB5C,SAASC,iBAAiB;AAKnB,IAAMC,mBAAN,cAA+BC,UAAUC,OAAO,oBAAA,EAAA;AAAuB;;;;AD2CvE,IAAMC,YAAN,MAAMA;EAiDX,YAA6BC,WAA6B,CAAC,GAAG;SAAjCA,WAAAA;SA/CZC,aAAaC,OAAOC,QAAQD,OAAOE,cAAc,CAAA,CAAA;SAG1DC,WAA+B,CAAA;SAM/BC,WAA+B,CAAA;SAKvBC,UAAU,IAAIC,MAAAA;SAKdC,QAAQ,IAAID,MAAAA;SAKZE,SAAS,IAAIF,MAAAA;SAKbG,cAAc,IAAIH,MAAAA;SAKlBI,cAAc,IAAIJ,MAAAA;SAMlBK,eAAe,IAAIL,MAAAA;SAKnBM,OAAO,IAAIN,MAAAA;SAU3BO,MAAM,CACJC,YAMAd,OAAOe,IAAI,MAAM,aAAA;AACf,WAAKX,WAAW;WAAIU,QAAQE;;AAE5B,YAAMC,iBAAiB,OAAO,KAAKC,kBAAkBJ,QAAQK,QAAQL,QAAQE,OAAO;AACpF,WAAKb,WAAW;QAACc;;AACjB,WAAKP,YAAYU,KAAKH,cAAAA;AAGtB,SAAG;AACDI,YAAI,WAAW;UACbC,SAAS,KAAKnB,SAASoB;UACvBP,SAAS,KAAKZ,SAASmB;UACvBC,OAAQC,OAAOC,OAAOZ,QAAQa,SAASH,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,UAAUnH,MAAMyG,OAAO;MAC/C;IACF;EACF;AAEA,SAAOoB;AACT;AAEA,IAAMR,4BAA4B,CAChCnB,iBAAAA;AAEA,SAAO;IACLgB,MAAM;;IAENc,aAAa;IACbV,MAAM;;QAEF;SAAIpB,aAAaK,QAAO;MACvBlF,IAAI,CAAC,CAACmF,IAAI,EAAEyB,KAAI,CAAE,MAAM,yBAAyBzB,EAAAA,KAAOyB,OAAO;EAAKA,IAAAA,KAAS,EAAA,qBAAuB,EACpGxE,KAAK,IAAA,CAAA;;EAEZ;AACF;AAWO,IAAMoC,uBAAN,cAAmCqC,QAAQC,IAAI,sCAAA,EAAA,EAAA;AAGjD;AAgBL,IAAM5C,WAGoF9F,OAAO2I,GAAG,UAAA,EAClG,WAAWhD,WAAWhE,SAAO;AAC3B,QAAMiH,sBAAsB5I,OAAO6I,SAASlH,OAAAA,IAAW,OAAOA,UAAUA;AAExE,SAAO,OAAO3B,OAAOoC,QAAQuD,WAAW,CAACmD,aAAaC,QAAQH,qBAAqBE,QAAAA,CAAAA;AACrF,CAAA;;;AE1UF,SAASE,cAAc;AAGvB,SAASC,kBAAkB;AAC3B,SAASC,gBAAqC;AAI9C,IAAMC,sBAAsBC,OAAOC;AAEnC,IAAMC,mBAAmBF,OAAOG,OAAO;;EAErCC,UAAUC;;EAEVC,SAASP;AACX,CAAA;AAQO,IAAMQ,aAIT,cAAcL,iBAAAA;EAChB;SAAgBM,cAAc;;EAC9B,OAAOC,WAAWC,QAAoC;AACpD,WAAOH,WAAWI,KAAK;MACrBP,UAAUM,OAAOE;MACjBN,SAASO,WAAWH,MAAAA;IACtB,CAAA;EACF;EAEA,OAAOI,YAAYC,KAAmC;AACpD,WAAO;MACLC,MAAM;MACNC,aAAaV,WAAWC;MACxBU,MAAMC,KAAKC,UAAUL,GAAAA;IACvB;EACF;AACF;",
6
- "names": ["AiLanguageModel", "AiToolkit", "Chunk", "Context", "Effect", "Option", "pipe", "Stream", "Array", "String", "AiParser", "AiPreprocessor", "getToolCalls", "runTool", "ToolExecutionService", "ToolResolverService", "Event", "todo", "Obj", "ObjectVersion", "DatabaseService", "log", "DataType", "BaseError", "AiAssistantError", "BaseError", "extend", "AiSession", "_options", "_semaphore", "Effect", "runSync", "makeSemaphore", "_pending", "_history", "message", "Event", "block", "update", "streamEvent", "userMessage", "statusReport", "done", "run", "options", "gen", "history", "promptMessages", "_formatUserPrompt", "prompt", "emit", "log", "pending", "length", "tools", "Object", "values", "toolkit", "map", "tool", "name", "AiPreprocessor", "preprocessAiInput", "blueprints", "systemPrompt", "pipe", "forEach", "blueprint", "succeed", "instructions", "flatMap", "template", "DatabaseService", "loadRef", "source", "Array", "content", "reduce", "String", "concat", "blueprintToolkit", "ToolResolverService", "resolveToolkit", "bp", "blueprintToolkitHandler", "toContext", "ToolExecutionService", "handlersFor", "AiToolkit", "merge", "toolkitWithBlueprintHandlers", "provide", "info", "slice", "join", "keys", "blocks", "AiLanguageModel", "streamText", "system", "disableToolCallResolution", "AiParser", "parseGptStream", "onBlock", "onPart", "part", "Stream", "runCollect", "Chunk", "toArray", "response", "Obj", "make", "DataType", "Message", "created", "Date", "toISOString", "sender", "role", "push", "toolCalls", "getToolCalls", "toolResults", "runTools", "withPermits", "withSpan", "prelude", "artifactDiffResolver", "serviceOption", "ArtifactDiffResolver", "Option", "isSome", "versions", "gatherObjectVersions", "artifactDiff", "tryPromise", "try", "value", "resolve", "entries", "id", "version", "lastVersion", "catch", "AiAssistantError", "wrap", "ObjectVersion", "equals", "get", "delete", "_tag", "objectId", "size", "createArtifactUpdateBlock", "text", "runStructured", "schema", "todo", "abort", "_stream", "messages", "artifactIds", "Map", "set", "disposition", "diff", "Context", "Tag", "fn", "toolkitWithHandlers", "isEffect", "toolCall", "runTool", "Schema", "getVersion", "ObjectId", "ObjectVersionSchema", "Schema", "Unknown", "VersionPinSchema", "Struct", "objectId", "ObjectId", "version", "VersionPin", "DISPOSITION", "fromObject", "object", "make", "id", "getVersion", "createBlock", "pin", "_tag", "disposition", "data", "JSON", "stringify"]
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/extraction/extraction-ner-function/named-entity-recognition.ts"],
4
- "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport {\n type TokenClassificationOutput,\n type TokenClassificationPipelineType,\n type TokenClassificationSingle,\n pipeline,\n} from '@xenova/transformers';\n\nimport { log } from '@dxos/log';\n\nlet _ner: Promise<TokenClassificationPipelineType>;\n/**\n * Named Entity Recognition pipeline.\n * Initializes the pipeline on first call.\n * @returns The singleton promise that resolves to a token classification pipeline.\n */\nexport const getNer = () => {\n if (!_ner) {\n _ner = pipeline('ner', 'Xenova/bert-base-NER').then((ner) => {\n log.info('NER model is ready');\n return ner;\n });\n }\n return _ner;\n};\n\n/**\n * Extracts entities from text using the named entity recognition pipeline and does simple grouping of tokens.\n */\nexport const extractFullEntities = async (text: string): Promise<TokenClassificationSingle[]> => {\n const ner = await getNer();\n const entities = (await ner(text)) as TokenClassificationOutput;\n const tokenGroups = createTokenGroups(entities);\n const combinedEntities = tokenGroups.map(combineNerTokens);\n return combinedEntities;\n};\n\nexport const createTokenGroups = (tokens: TokenClassificationSingle[]): TokenClassificationSingle[][] => {\n const tokenGroups: TokenClassificationSingle[][] = [];\n\n const canBeCombined = (prevToken: TokenClassificationSingle, currentToken: TokenClassificationSingle) => {\n const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O\n const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O\n if (prevEntity !== currentEntity) {\n return false;\n }\n if (prevState === 'O') {\n return false;\n }\n if (currentState === 'I') {\n return true;\n }\n if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {\n return currentToken.word.startsWith('#');\n }\n return false;\n };\n\n for (const token of tokens) {\n if (tokenGroups.length === 0) {\n tokenGroups.push([token]);\n continue;\n }\n const lastTokenGroup = tokenGroups.at(-1)!;\n if (canBeCombined(lastTokenGroup.at(-1)!, token)) {\n lastTokenGroup.push(token);\n continue;\n }\n tokenGroups.push([token]);\n }\n\n return tokenGroups;\n};\n\nexport const combineNerTokens = (group: TokenClassificationSingle[]): TokenClassificationSingle => {\n const combinedToken: TokenClassificationSingle = {\n entity: group.at(0)!.entity.split('-')[1],\n score: group.reduce((acc, token) => acc + token.score, 0) / group.length,\n index: group.at(0)!.index,\n word: group.at(0)!.word,\n start: group.at(0)!.start,\n end: group.at(-1)!.end,\n };\n\n for (const token of group.slice(1)) {\n if (token.word.startsWith('#')) {\n combinedToken.word += token.word.replace(/^#+/, '');\n } else {\n combinedToken.word += ' ' + token.word;\n }\n }\n\n return combinedToken;\n};\n"],
5
- "mappings": ";;;AAIA,SAIEA,gBACK;AAEP,SAASC,WAAW;;AAEpB,IAAIC;AAMG,IAAMC,SAAS,MAAA;AACpB,MAAI,CAACD,MAAM;AACTA,WAAOF,SAAS,OAAO,sBAAA,EAAwBI,KAAK,CAACC,QAAAA;AACnDJ,UAAIK,KAAK,sBAAA,QAAA;;;;;;AACT,aAAOD;IACT,CAAA;EACF;AACA,SAAOH;AACT;AAKO,IAAMK,sBAAsB,OAAOC,SAAAA;AACxC,QAAMH,MAAM,MAAMF,OAAAA;AAClB,QAAMM,WAAY,MAAMJ,IAAIG,IAAAA;AAC5B,QAAME,cAAcC,kBAAkBF,QAAAA;AACtC,QAAMG,mBAAmBF,YAAYG,IAAIC,gBAAAA;AACzC,SAAOF;AACT;AAEO,IAAMD,oBAAoB,CAACI,WAAAA;AAChC,QAAML,cAA6C,CAAA;AAEnD,QAAMM,gBAAgB,CAACC,WAAsCC,iBAAAA;AAC3D,UAAM,CAACC,cAAcC,aAAAA,IAAiBF,aAAaG,OAAOC,MAAM,GAAA;AAChE,UAAM,CAACC,WAAWC,UAAAA,IAAcP,UAAUI,OAAOC,MAAM,GAAA;AACvD,QAAIE,eAAeJ,eAAe;AAChC,aAAO;IACT;AACA,QAAIG,cAAc,KAAK;AACrB,aAAO;IACT;AACA,QAAIJ,iBAAiB,KAAK;AACxB,aAAO;IACT;AACA,SAAKI,cAAc,OAAOA,cAAc,QAAQJ,iBAAiB,KAAK;AACpE,aAAOD,aAAaO,KAAKC,WAAW,GAAA;IACtC;AACA,WAAO;EACT;AAEA,aAAWC,SAASZ,QAAQ;AAC1B,QAAIL,YAAYkB,WAAW,GAAG;AAC5BlB,kBAAYmB,KAAK;QAACF;OAAM;AACxB;IACF;AACA,UAAMG,iBAAiBpB,YAAYqB,GAAG,EAAC;AACvC,QAAIf,cAAcc,eAAeC,GAAG,EAAC,GAAKJ,KAAAA,GAAQ;AAChDG,qBAAeD,KAAKF,KAAAA;AACpB;IACF;AACAjB,gBAAYmB,KAAK;MAACF;KAAM;EAC1B;AAEA,SAAOjB;AACT;AAEO,IAAMI,mBAAmB,CAACkB,UAAAA;AAC/B,QAAMC,gBAA2C;IAC/CZ,QAAQW,MAAMD,GAAG,CAAA,EAAIV,OAAOC,MAAM,GAAA,EAAK,CAAA;IACvCY,OAAOF,MAAMG,OAAO,CAACC,KAAKT,UAAUS,MAAMT,MAAMO,OAAO,CAAA,IAAKF,MAAMJ;IAClES,OAAOL,MAAMD,GAAG,CAAA,EAAIM;IACpBZ,MAAMO,MAAMD,GAAG,CAAA,EAAIN;IACnBa,OAAON,MAAMD,GAAG,CAAA,EAAIO;IACpBC,KAAKP,MAAMD,GAAG,EAAC,EAAIQ;EACrB;AAEA,aAAWZ,SAASK,MAAMQ,MAAM,CAAA,GAAI;AAClC,QAAIb,MAAMF,KAAKC,WAAW,GAAA,GAAM;AAC9BO,oBAAcR,QAAQE,MAAMF,KAAKgB,QAAQ,OAAO,EAAA;IAClD,OAAO;AACLR,oBAAcR,QAAQ,MAAME,MAAMF;IACpC;EACF;AAEA,SAAOQ;AACT;",
6
- "names": ["pipeline", "log", "_ner", "getNer", "then", "ner", "info", "extractFullEntities", "text", "entities", "tokenGroups", "createTokenGroups", "combinedEntities", "map", "combineNerTokens", "tokens", "canBeCombined", "prevToken", "currentToken", "currentState", "currentEntity", "entity", "split", "prevState", "prevEntity", "word", "startsWith", "token", "length", "push", "lastTokenGroup", "at", "group", "combinedToken", "score", "reduce", "acc", "index", "start", "end", "slice", "replace"]
7
- }