@stackfactor/agent-utils 1.13.4 → 1.33.0

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 (252) hide show
  1. package/README.md +688 -23
  2. package/dist/cjs/agentProto.d.ts +13 -3
  3. package/dist/cjs/agentProto.d.ts.map +1 -1
  4. package/dist/cjs/agentProto.js +25 -4
  5. package/dist/cjs/backoff.d.ts +17 -0
  6. package/dist/cjs/backoff.d.ts.map +1 -0
  7. package/dist/cjs/backoff.js +26 -0
  8. package/dist/cjs/cancellation.d.ts +22 -0
  9. package/dist/cjs/cancellation.d.ts.map +1 -0
  10. package/dist/cjs/cancellation.js +36 -0
  11. package/dist/cjs/cli.d.ts +3 -0
  12. package/dist/cjs/cli.d.ts.map +1 -0
  13. package/dist/cjs/cli.js +85 -0
  14. package/dist/cjs/client.d.ts +11 -0
  15. package/dist/cjs/client.d.ts.map +1 -1
  16. package/dist/cjs/client.js +27 -0
  17. package/dist/cjs/const.d.ts +23 -0
  18. package/dist/cjs/const.d.ts.map +1 -1
  19. package/dist/cjs/const.js +23 -0
  20. package/dist/cjs/daemon.d.ts +94 -0
  21. package/dist/cjs/daemon.d.ts.map +1 -0
  22. package/dist/cjs/daemon.js +507 -0
  23. package/dist/cjs/daemonConfig.d.ts +45 -0
  24. package/dist/cjs/daemonConfig.d.ts.map +1 -0
  25. package/dist/cjs/daemonConfig.js +148 -0
  26. package/dist/cjs/daemonEnv.d.ts +10 -0
  27. package/dist/cjs/daemonEnv.d.ts.map +1 -0
  28. package/dist/cjs/daemonEnv.js +12 -0
  29. package/dist/cjs/durable/clientTool.d.ts +39 -0
  30. package/dist/cjs/durable/clientTool.d.ts.map +1 -0
  31. package/dist/cjs/durable/clientTool.js +90 -0
  32. package/dist/cjs/durable/index.d.ts +9 -0
  33. package/dist/cjs/durable/index.d.ts.map +1 -0
  34. package/dist/cjs/durable/index.js +16 -0
  35. package/dist/cjs/durable/journal.d.ts +218 -0
  36. package/dist/cjs/durable/journal.d.ts.map +1 -0
  37. package/dist/cjs/durable/journal.js +392 -0
  38. package/dist/cjs/durable/pause.d.ts +84 -0
  39. package/dist/cjs/durable/pause.d.ts.map +1 -0
  40. package/dist/cjs/durable/pause.js +241 -0
  41. package/dist/cjs/durable/resume.d.ts +27 -0
  42. package/dist/cjs/durable/resume.d.ts.map +1 -0
  43. package/dist/cjs/durable/resume.js +80 -0
  44. package/dist/cjs/durable/snapshotSaver.d.ts +38 -0
  45. package/dist/cjs/durable/snapshotSaver.d.ts.map +1 -0
  46. package/dist/cjs/durable/snapshotSaver.js +81 -0
  47. package/dist/cjs/durable/step.d.ts +28 -0
  48. package/dist/cjs/durable/step.d.ts.map +1 -0
  49. package/dist/cjs/durable/step.js +43 -0
  50. package/dist/cjs/index.d.ts +30 -7
  51. package/dist/cjs/index.d.ts.map +1 -1
  52. package/dist/cjs/index.js +28 -1
  53. package/dist/cjs/infrastructure.d.ts +50 -0
  54. package/dist/cjs/infrastructure.d.ts.map +1 -0
  55. package/dist/cjs/infrastructure.js +163 -0
  56. package/dist/cjs/langChain.d.ts +15 -3
  57. package/dist/cjs/langChain.d.ts.map +1 -1
  58. package/dist/cjs/langChain.js +986 -180
  59. package/dist/cjs/learningActivityMedia.d.ts +196 -0
  60. package/dist/cjs/learningActivityMedia.d.ts.map +1 -0
  61. package/dist/cjs/learningActivityMedia.js +861 -0
  62. package/dist/cjs/learningContentPlan.d.ts +117 -0
  63. package/dist/cjs/learningContentPlan.d.ts.map +1 -0
  64. package/dist/cjs/learningContentPlan.js +327 -0
  65. package/dist/cjs/learningContentThread.d.ts +213 -0
  66. package/dist/cjs/learningContentThread.d.ts.map +1 -0
  67. package/dist/cjs/learningContentThread.js +448 -0
  68. package/dist/cjs/logger.d.ts +1 -0
  69. package/dist/cjs/logger.d.ts.map +1 -1
  70. package/dist/cjs/logger.js +17 -3
  71. package/dist/cjs/modelRates.d.ts +40 -0
  72. package/dist/cjs/modelRates.d.ts.map +1 -0
  73. package/dist/cjs/modelRates.js +76 -0
  74. package/dist/cjs/referenceContext.d.ts +4 -145
  75. package/dist/cjs/referenceContext.d.ts.map +1 -1
  76. package/dist/cjs/referenceContext.js +16 -251
  77. package/dist/cjs/retry.d.ts +64 -0
  78. package/dist/cjs/retry.d.ts.map +1 -0
  79. package/dist/cjs/retry.js +138 -0
  80. package/dist/cjs/runtimeContext.d.ts +8 -1
  81. package/dist/cjs/runtimeContext.d.ts.map +1 -1
  82. package/dist/cjs/runtimeContext.js +3 -1
  83. package/dist/cjs/serve.d.ts +3 -2
  84. package/dist/cjs/serve.d.ts.map +1 -1
  85. package/dist/cjs/serve.js +32 -2
  86. package/dist/cjs/sourceStore.d.ts +378 -0
  87. package/dist/cjs/sourceStore.d.ts.map +1 -0
  88. package/dist/cjs/sourceStore.js +872 -0
  89. package/dist/cjs/stackfactorKnowledge.d.ts +91 -7
  90. package/dist/cjs/stackfactorKnowledge.d.ts.map +1 -1
  91. package/dist/cjs/stackfactorKnowledge.js +108 -44
  92. package/dist/cjs/tavily.d.ts +108 -4
  93. package/dist/cjs/tavily.d.ts.map +1 -1
  94. package/dist/cjs/tavily.js +242 -21
  95. package/dist/cjs/usage.d.ts.map +1 -1
  96. package/dist/cjs/usage.js +9 -15
  97. package/dist/cjs/version.d.ts +3 -0
  98. package/dist/cjs/version.d.ts.map +1 -0
  99. package/dist/cjs/version.js +5 -0
  100. package/dist/cjs/workflow/bookkeeping.d.ts +78 -0
  101. package/dist/cjs/workflow/bookkeeping.d.ts.map +1 -0
  102. package/dist/cjs/workflow/bookkeeping.js +75 -0
  103. package/dist/cjs/workflow/index.d.ts +13 -0
  104. package/dist/cjs/workflow/index.d.ts.map +1 -0
  105. package/dist/cjs/workflow/index.js +51 -0
  106. package/dist/cjs/workflow/limits.d.ts +52 -0
  107. package/dist/cjs/workflow/limits.d.ts.map +1 -0
  108. package/dist/cjs/workflow/limits.js +62 -0
  109. package/dist/cjs/workflow/platformTransport.d.ts +39 -0
  110. package/dist/cjs/workflow/platformTransport.d.ts.map +1 -0
  111. package/dist/cjs/workflow/platformTransport.js +115 -0
  112. package/dist/cjs/workflow/retrying.d.ts +44 -0
  113. package/dist/cjs/workflow/retrying.d.ts.map +1 -0
  114. package/dist/cjs/workflow/retrying.js +82 -0
  115. package/dist/cjs/workflow/run.d.ts +195 -0
  116. package/dist/cjs/workflow/run.d.ts.map +1 -0
  117. package/dist/cjs/workflow/run.js +858 -0
  118. package/dist/cjs/workflow/testing/index.d.ts +3 -0
  119. package/dist/cjs/workflow/testing/index.d.ts.map +1 -0
  120. package/dist/cjs/workflow/testing/index.js +6 -0
  121. package/dist/cjs/workflow/testing/memoryTransport.d.ts +203 -0
  122. package/dist/cjs/workflow/testing/memoryTransport.d.ts.map +1 -0
  123. package/dist/cjs/workflow/testing/memoryTransport.js +1171 -0
  124. package/dist/cjs/workflow/transport.d.ts +219 -0
  125. package/dist/cjs/workflow/transport.d.ts.map +1 -0
  126. package/dist/cjs/workflow/transport.js +19 -0
  127. package/dist/esm/agentProto.d.ts +13 -3
  128. package/dist/esm/agentProto.d.ts.map +1 -1
  129. package/dist/esm/agentProto.js +23 -3
  130. package/dist/esm/backoff.d.ts +17 -0
  131. package/dist/esm/backoff.d.ts.map +1 -0
  132. package/dist/esm/backoff.js +22 -0
  133. package/dist/esm/cancellation.d.ts +22 -0
  134. package/dist/esm/cancellation.d.ts.map +1 -0
  135. package/dist/esm/cancellation.js +27 -0
  136. package/dist/esm/cli.d.ts +3 -0
  137. package/dist/esm/cli.d.ts.map +1 -0
  138. package/dist/esm/cli.js +80 -0
  139. package/dist/esm/client.d.ts +11 -0
  140. package/dist/esm/client.d.ts.map +1 -1
  141. package/dist/esm/client.js +25 -1
  142. package/dist/esm/const.d.ts +23 -0
  143. package/dist/esm/const.d.ts.map +1 -1
  144. package/dist/esm/const.js +23 -0
  145. package/dist/esm/daemon.d.ts +94 -0
  146. package/dist/esm/daemon.d.ts.map +1 -0
  147. package/dist/esm/daemon.js +499 -0
  148. package/dist/esm/daemonConfig.d.ts +45 -0
  149. package/dist/esm/daemonConfig.d.ts.map +1 -0
  150. package/dist/esm/daemonConfig.js +142 -0
  151. package/dist/esm/daemonEnv.d.ts +10 -0
  152. package/dist/esm/daemonEnv.d.ts.map +1 -0
  153. package/dist/esm/daemonEnv.js +9 -0
  154. package/dist/esm/durable/clientTool.d.ts +39 -0
  155. package/dist/esm/durable/clientTool.d.ts.map +1 -0
  156. package/dist/esm/durable/clientTool.js +82 -0
  157. package/dist/esm/durable/index.d.ts +9 -0
  158. package/dist/esm/durable/index.d.ts.map +1 -0
  159. package/dist/esm/durable/index.js +8 -0
  160. package/dist/esm/durable/journal.d.ts +218 -0
  161. package/dist/esm/durable/journal.d.ts.map +1 -0
  162. package/dist/esm/durable/journal.js +374 -0
  163. package/dist/esm/durable/pause.d.ts +84 -0
  164. package/dist/esm/durable/pause.d.ts.map +1 -0
  165. package/dist/esm/durable/pause.js +226 -0
  166. package/dist/esm/durable/resume.d.ts +27 -0
  167. package/dist/esm/durable/resume.d.ts.map +1 -0
  168. package/dist/esm/durable/resume.js +72 -0
  169. package/dist/esm/durable/snapshotSaver.d.ts +38 -0
  170. package/dist/esm/durable/snapshotSaver.d.ts.map +1 -0
  171. package/dist/esm/durable/snapshotSaver.js +77 -0
  172. package/dist/esm/durable/step.d.ts +28 -0
  173. package/dist/esm/durable/step.d.ts.map +1 -0
  174. package/dist/esm/durable/step.js +36 -0
  175. package/dist/esm/index.d.ts +30 -7
  176. package/dist/esm/index.d.ts.map +1 -1
  177. package/dist/esm/index.js +20 -4
  178. package/dist/esm/infrastructure.d.ts +50 -0
  179. package/dist/esm/infrastructure.d.ts.map +1 -0
  180. package/dist/esm/infrastructure.js +156 -0
  181. package/dist/esm/langChain.d.ts +15 -3
  182. package/dist/esm/langChain.d.ts.map +1 -1
  183. package/dist/esm/langChain.js +984 -179
  184. package/dist/esm/learningActivityMedia.d.ts +196 -0
  185. package/dist/esm/learningActivityMedia.d.ts.map +1 -0
  186. package/dist/esm/learningActivityMedia.js +807 -0
  187. package/dist/esm/learningContentPlan.d.ts +117 -0
  188. package/dist/esm/learningContentPlan.d.ts.map +1 -0
  189. package/dist/esm/learningContentPlan.js +320 -0
  190. package/dist/esm/learningContentThread.d.ts +213 -0
  191. package/dist/esm/learningContentThread.d.ts.map +1 -0
  192. package/dist/esm/learningContentThread.js +403 -0
  193. package/dist/esm/logger.d.ts +1 -0
  194. package/dist/esm/logger.d.ts.map +1 -1
  195. package/dist/esm/logger.js +17 -3
  196. package/dist/esm/modelRates.d.ts +40 -0
  197. package/dist/esm/modelRates.d.ts.map +1 -0
  198. package/dist/esm/modelRates.js +68 -0
  199. package/dist/esm/referenceContext.d.ts +4 -145
  200. package/dist/esm/referenceContext.d.ts.map +1 -1
  201. package/dist/esm/referenceContext.js +16 -243
  202. package/dist/esm/retry.d.ts +64 -0
  203. package/dist/esm/retry.d.ts.map +1 -0
  204. package/dist/esm/retry.js +127 -0
  205. package/dist/esm/runtimeContext.d.ts +8 -1
  206. package/dist/esm/runtimeContext.d.ts.map +1 -1
  207. package/dist/esm/runtimeContext.js +3 -1
  208. package/dist/esm/serve.d.ts +3 -2
  209. package/dist/esm/serve.d.ts.map +1 -1
  210. package/dist/esm/serve.js +33 -3
  211. package/dist/esm/sourceStore.d.ts +378 -0
  212. package/dist/esm/sourceStore.d.ts.map +1 -0
  213. package/dist/esm/sourceStore.js +855 -0
  214. package/dist/esm/stackfactorKnowledge.d.ts +91 -7
  215. package/dist/esm/stackfactorKnowledge.d.ts.map +1 -1
  216. package/dist/esm/stackfactorKnowledge.js +101 -41
  217. package/dist/esm/tavily.d.ts +108 -4
  218. package/dist/esm/tavily.d.ts.map +1 -1
  219. package/dist/esm/tavily.js +237 -20
  220. package/dist/esm/usage.d.ts.map +1 -1
  221. package/dist/esm/usage.js +9 -15
  222. package/dist/esm/version.d.ts +3 -0
  223. package/dist/esm/version.d.ts.map +1 -0
  224. package/dist/esm/version.js +2 -0
  225. package/dist/esm/workflow/bookkeeping.d.ts +78 -0
  226. package/dist/esm/workflow/bookkeeping.d.ts.map +1 -0
  227. package/dist/esm/workflow/bookkeeping.js +67 -0
  228. package/dist/esm/workflow/index.d.ts +13 -0
  229. package/dist/esm/workflow/index.d.ts.map +1 -0
  230. package/dist/esm/workflow/index.js +10 -0
  231. package/dist/esm/workflow/limits.d.ts +52 -0
  232. package/dist/esm/workflow/limits.d.ts.map +1 -0
  233. package/dist/esm/workflow/limits.js +59 -0
  234. package/dist/esm/workflow/platformTransport.d.ts +39 -0
  235. package/dist/esm/workflow/platformTransport.d.ts.map +1 -0
  236. package/dist/esm/workflow/platformTransport.js +110 -0
  237. package/dist/esm/workflow/retrying.d.ts +44 -0
  238. package/dist/esm/workflow/retrying.d.ts.map +1 -0
  239. package/dist/esm/workflow/retrying.js +73 -0
  240. package/dist/esm/workflow/run.d.ts +195 -0
  241. package/dist/esm/workflow/run.d.ts.map +1 -0
  242. package/dist/esm/workflow/run.js +850 -0
  243. package/dist/esm/workflow/testing/index.d.ts +3 -0
  244. package/dist/esm/workflow/testing/index.d.ts.map +1 -0
  245. package/dist/esm/workflow/testing/index.js +2 -0
  246. package/dist/esm/workflow/testing/memoryTransport.d.ts +203 -0
  247. package/dist/esm/workflow/testing/memoryTransport.d.ts.map +1 -0
  248. package/dist/esm/workflow/testing/memoryTransport.js +1167 -0
  249. package/dist/esm/workflow/transport.d.ts +219 -0
  250. package/dist/esm/workflow/transport.d.ts.map +1 -0
  251. package/dist/esm/workflow/transport.js +16 -0
  252. package/package.json +8 -7
@@ -1,54 +1,19 @@
1
- import { KnowledgeSearch, SkippedSource } from "./stackfactorKnowledge.js";
2
- /** The prompt key the passages are handed to the model under. */
3
- export declare const REFERENCE_PASSAGES_KEY = "Knowledge Base Passages";
4
- /** A web or publication page a reference points at. */
5
- export interface ReferencePage {
6
- /** The reference's short id, "r0"… */
7
- id: string;
8
- title: string;
9
- url: string;
10
- }
11
- /** A passage of a document that references cite. */
12
- export interface ReferencePassage {
13
- /**
14
- * The short ids of the STACKFACTOR references that cite this passage's chunk
15
- * (`knowledgeBase.chunkId`); when none does, of every one citing its document.
16
- */
17
- references: string[];
18
- document: string | null;
19
- section: string | null;
20
- text: string;
21
- chunkId: string;
22
- documentId: string;
23
- knowledgeBaseId: string;
24
- }
25
- /** What a list of references points at. */
26
- export interface ReferenceContext {
27
- pages: ReferencePage[];
28
- passages: ReferencePassage[];
29
- /**
30
- * Always empty from `gatherReferenceContext`: a cited knowledge base that
31
- * cannot be read throws KnowledgeAccessError, which carries the skips.
32
- */
33
- skipped: SkippedSource[];
34
- /** True when the references cite at least one knowledge base document (all of them readable, or it would have thrown). */
35
- citesKnowledge: boolean;
36
- }
1
+ import { SkippedSource } from "./stackfactorKnowledge.js";
37
2
  /**
38
3
  * The knowledge base documents a run's references cite could not be read: the
39
4
  * run has no token, the person may not open a cited knowledge base (401, 403,
40
- * 404), or its search failed. The agent answers with it — `code` and `message`
5
+ * 404), or reading it failed. The agent answers with it — `code` and `message`
41
6
  * are what the person sees — and generates nothing.
42
7
  */
43
8
  export declare class KnowledgeAccessError extends Error {
44
9
  /** 403 when every skipped knowledge base refused the person (or there was no token), else 502. */
45
10
  readonly code: 403 | 502;
46
- /** The knowledge bases that could not be searched, and why; empty when there was no token. */
11
+ /** The knowledge bases that could not be read, and why; empty when there was no token. */
47
12
  readonly skipped: SkippedSource[];
48
13
  /** The titles of the STACKFACTOR references citing those knowledge bases, each once. */
49
14
  readonly documents: string[];
50
15
  /**
51
- * @param details `skipped` (from the search) and `documents` (titles); the
16
+ * @param details `skipped` (from the read) and `documents` (titles); the
52
17
  * code and the message follow from them
53
18
  */
54
19
  constructor(details?: {
@@ -56,54 +21,6 @@ export declare class KnowledgeAccessError extends Error {
56
21
  documents?: unknown[];
57
22
  });
58
23
  }
59
- /**
60
- * What a list of references points at: its pages, and the passages of the
61
- * documents its STACKFACTOR references cite. One search per knowledge base per
62
- * query (per batch of 500 documents), however many references there are.
63
- *
64
- * References that cite a knowledge base the run cannot read stop it: with no
65
- * token, or when any cited knowledge base is skipped (refused or failed), it
66
- * throws a KnowledgeAccessError naming the documents. A search that works and
67
- * finds nothing is not an error. With no knowledge cited, no token is needed.
68
- *
69
- * A passage is attributed to the references citing its chunk, else to every
70
- * reference citing its document. The best-ranked `maxPassages` are kept, and
71
- * the first `maxPages` pages in reference order.
72
- * @param options `references` (a skill's, a microskill's…), `queries` to
73
- * search the cited documents with, `search` (client-api
74
- * `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK` per
75
- * search (default 5), `maxPassages` (default 40), `maxPages` (default: all)
76
- * @returns `{ pages, passages, skipped, citesKnowledge }`; `skipped` is always
77
- * empty, since a skip throws
78
- * @throws KnowledgeAccessError When cited knowledge bases cannot be read
79
- */
80
- export declare const gatherReferenceContext: (options: {
81
- references: unknown;
82
- queries: string[];
83
- search: KnowledgeSearch;
84
- token: string | null | undefined;
85
- topK?: number;
86
- maxPassages?: number;
87
- maxPages?: number;
88
- }) => Promise<ReferenceContext>;
89
- /**
90
- * The passages as a prompt shows them: who cites each, where it came from, and
91
- * its text. The ids that locate it in the platform are left out.
92
- * @param context From `gatherReferenceContext`
93
- * @returns `[{ references, document, section, text }]`, for the model's input
94
- */
95
- export declare const referencePassagesForPrompt: (context: ReferenceContext) => {
96
- references: string[];
97
- document: string | null;
98
- section: string | null;
99
- text: string;
100
- }[];
101
- /** The guidance an agent's config gives for each kind of source it may be handed. */
102
- export interface SourceGuidance {
103
- pages: string;
104
- passages: string;
105
- none: string;
106
- }
107
24
  /** A reference as the platform stores it. `visibility` is left to the caller. */
108
25
  export interface StoredReference {
109
26
  kind: string;
@@ -131,34 +48,6 @@ export interface StoredReference {
131
48
  sectionTitle?: string | null;
132
49
  };
133
50
  }
134
- /**
135
- * The tools a run is given. Never `search`, `map` or `crawl`: a run reads what
136
- * its references point at and discovers nothing. `extract` only, only when
137
- * there are pages to open, and kept to those pages: any other url the model
138
- * asks for is refused (`allowedUrls`).
139
- * @param context From `gatherReferenceContext`
140
- * @returns `{ tavily: { tools: ["extract"], allowedUrls } }` with pages, else
141
- * `{}`. With no pages it returns `{}`, so a caller spreading it over a config
142
- * that already has a `tavily` key keeps that key (and `tavily: true` means
143
- * search plus extract): callers must not carry a base `tavily` key.
144
- */
145
- export declare const toolsFor: (context: ReferenceContext) => {
146
- tavily?: {
147
- tools: string[];
148
- allowedUrls: string[];
149
- };
150
- };
151
- /**
152
- * The stage prompt followed by the guidance for each kind of source the run
153
- * has: pages, then passages; when it has neither (knowledge cited and readable
154
- * but no passage found included), the `none` guidance. An empty guidance adds
155
- * nothing.
156
- * @param stagePrompt The stage's own prompt
157
- * @param context From `gatherReferenceContext`
158
- * @param guidance From `sourceGuidanceFrom`
159
- * @returns The sections joined with a blank line
160
- */
161
- export declare const composeSourceInstructions: (stagePrompt: string, context: ReferenceContext, guidance: SourceGuidance) => string;
162
51
  /**
163
52
  * A reference as a model is shown it. A STACKFACTOR reference loses its pointer
164
53
  * and its url (ids mean nothing to a model, and the url is behind the
@@ -196,44 +85,14 @@ export declare const toStoredReference: (reference: any, origin?: string) => Sto
196
85
  * @param origin Default `"AI_GENERATED"`
197
86
  */
198
87
  export declare const citedReferences: (ids: unknown, supplied: unknown, origin?: string) => StoredReference[];
199
- /**
200
- * The three guidance texts an agent's config holds. A missing or non-string
201
- * value is an empty string.
202
- * @param config The agent's config
203
- */
204
- export declare const sourceGuidanceFrom: (config: Record<string, unknown>) => SourceGuidance;
205
88
  declare const _default: {
206
89
  KnowledgeAccessError: typeof KnowledgeAccessError;
207
- REFERENCE_PASSAGES_KEY: string;
208
90
  citedReferences: (ids: unknown, supplied: unknown, origin?: string) => StoredReference[];
209
- composeSourceInstructions: (stagePrompt: string, context: ReferenceContext, guidance: SourceGuidance) => string;
210
- gatherReferenceContext: (options: {
211
- references: unknown;
212
- queries: string[];
213
- search: KnowledgeSearch;
214
- token: string | null | undefined;
215
- topK?: number;
216
- maxPassages?: number;
217
- maxPages?: number;
218
- }) => Promise<ReferenceContext>;
219
91
  referenceForModel: <T extends {
220
92
  kind?: string;
221
93
  }>(reference: T) => T;
222
- referencePassagesForPrompt: (context: ReferenceContext) => {
223
- references: string[];
224
- document: string | null;
225
- section: string | null;
226
- text: string;
227
- }[];
228
94
  referencesForModel: (references: unknown) => Array<Record<string, unknown>>;
229
- sourceGuidanceFrom: (config: Record<string, unknown>) => SourceGuidance;
230
95
  toStoredReference: (reference: any, origin?: string) => StoredReference;
231
- toolsFor: (context: ReferenceContext) => {
232
- tavily?: {
233
- tools: string[];
234
- allowedUrls: string[];
235
- };
236
- };
237
96
  };
238
97
  export default _default;
239
98
  //# sourceMappingURL=referenceContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"referenceContext.d.ts","sourceRoot":"","sources":["../../src/referenceContext.ts"],"names":[],"mappings":"AAuBA,OAAO,EACL,eAAe,EACf,aAAa,EAId,MAAM,2BAA2B,CAAC;AAQnC,iEAAiE;AACjE,eAAO,MAAM,sBAAsB,4BAA4B,CAAC;AAEhE,uDAAuD;AACvD,MAAM,WAAW,aAAa;IAC5B,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,oDAAoD;AACpD,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,2CAA2C;AAC3C,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,aAAa,EAAE,CAAC;IACvB,QAAQ,EAAE,gBAAgB,EAAE,CAAC;IAC7B;;;OAGG;IACH,OAAO,EAAE,aAAa,EA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,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACvE,aAAa,CAAC,EAAE;QACd,eAAe,EAAE,MAAM,CAAC;QACxB,UAAU,EAAE,MAAM,CAAC;QACnB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAC9B,CAAC;CACH;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,WAAW,CAAC,KAAG,CAY7E,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAAI,YAAY,OAAO,KAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQrF,CAAC;AAmBF;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAAI,WAAW,GAAG,EAAE,SAAQ,MAAuB,KAAG,eAoBnF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,OAAO,EAAE,UAAU,OAAO,EAAE,SAAS,MAAM,KAAG,eAAe,EAYjG,CAAC;;;2BAZmC,OAAO,YAAY,OAAO,WAAW,MAAM,KAAG,eAAe,EAAE;wBAtFlE,CAAC,SAAS;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,aAAa,CAAC,KAAG,CAAC;qCAqBhC,OAAO,KAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;mCAmC1C,GAAG,WAAU,MAAM,KAAoB,eAAe;;AA4CnG,wBAME"}
@@ -1,49 +1,35 @@
1
1
  //
2
- // Reference context — gathering what a list of references points at
2
+ // References — how a run's references are shown to a model and stored, and
3
+ // the error a run stops with when the documents they cite cannot be read
3
4
  //
4
- // An agent that reads its references never discovers new sources. It reads
5
- // what they point at:
5
+ // Reading what references point at is `sourceStore`'s work. What stays here:
6
6
  //
7
- // - pages: WEB and PUBLICATION references with an http(s) url, which the
8
- // agent opens through Tavily `extract`. A STACKFACTOR reference's url is
9
- // the document's page at its source and is never opened as a page;
10
- // - passages: the passages of the knowledge base documents STACKFACTOR
11
- // references cite, searched under the run's own token, so a run reads only
12
- // what the person it acts for may open.
7
+ // - `KnowledgeAccessError`: a cited knowledge base that cannot be read stops
8
+ // the run (Candin, 2026-10-13). With no token, a refusal or a failed read,
9
+ // `sourceStore.prepareSources` throws it naming the documents, and the
10
+ // agent answers with it rather than writing as if the content cited
11
+ // nothing;
12
+ // - `referencesForModel`, `citedReferences`, `toStoredReference`: a
13
+ // reference is cited by its short id `r<N>`, N being its index in the
14
+ // supplied list, whatever its kind.
13
15
  //
14
- // A cited knowledge base that cannot be read stops the run (Candin,
15
- // 2026-10-13): with no token, a refusal or a failed search, the gathering
16
- // throws a KnowledgeAccessError naming the documents, and the agent answers
17
- // with it rather than writing as if the content cited nothing. A search that
18
- // works and finds nothing is not an error.
19
- //
20
- // A reference is cited by its short id `r<N>`, N being its index in the
21
- // supplied list, whatever its kind.
22
- //
23
- import logger from "./logger.js";
24
- import { STACKFACTOR_REFERENCE, retrievePassages, sourcesFromReferences, } from "./stackfactorKnowledge.js";
25
- /** How many passages one search asks for when the caller does not say. */
26
- const DEFAULT_REFERENCE_TOP_K = 5;
27
- /** How many passages a gathering keeps when the caller does not say: the best-ranked. */
28
- const DEFAULT_MAX_PASSAGES = 40;
29
- /** The prompt key the passages are handed to the model under. */
30
- export const REFERENCE_PASSAGES_KEY = "Knowledge Base Passages";
16
+ import { STACKFACTOR_REFERENCE } from "./stackfactorKnowledge.js";
31
17
  const text = (value) => (typeof value === "string" ? value.trim() : "");
32
18
  /**
33
19
  * The knowledge base documents a run's references cite could not be read: the
34
20
  * run has no token, the person may not open a cited knowledge base (401, 403,
35
- * 404), or its search failed. The agent answers with it — `code` and `message`
21
+ * 404), or reading it failed. The agent answers with it — `code` and `message`
36
22
  * are what the person sees — and generates nothing.
37
23
  */
38
24
  export class KnowledgeAccessError extends Error {
39
25
  /** 403 when every skipped knowledge base refused the person (or there was no token), else 502. */
40
26
  code;
41
- /** The knowledge bases that could not be searched, and why; empty when there was no token. */
27
+ /** The knowledge bases that could not be read, and why; empty when there was no token. */
42
28
  skipped;
43
29
  /** The titles of the STACKFACTOR references citing those knowledge bases, each once. */
44
30
  documents;
45
31
  /**
46
- * @param details `skipped` (from the search) and `documents` (titles); the
32
+ * @param details `skipped` (from the read) and `documents` (titles); the
47
33
  * code and the message follow from them
48
34
  */
49
35
  constructor(details = {}) {
@@ -53,7 +39,7 @@ export class KnowledgeAccessError extends Error {
53
39
  const titles = documents.map((title) => `"${title}"`).join(", ");
54
40
  super(code === 403
55
41
  ? `The knowledge base documents this content cites could not be read with your access${titles ? `: ${titles}` : ""}.`
56
- : `The knowledge base documents this content cites could not be read: the knowledge base search failed${titles ? ` for ${titles}` : ""}.`);
42
+ : `The knowledge base documents this content cites could not be read: reading the knowledge base failed${titles ? ` for ${titles}` : ""}.`);
57
43
  this.name = "KnowledgeAccessError";
58
44
  this.code = code;
59
45
  this.skipped = skipped;
@@ -61,204 +47,7 @@ export class KnowledgeAccessError extends Error {
61
47
  Object.setPrototypeOf(this, KnowledgeAccessError.prototype);
62
48
  }
63
49
  }
64
- const id = (value) => typeof value === "string" ? value.trim() : value != null && typeof value === "object" ? String(value) : "";
65
50
  const shortId = (index) => `r${index}`;
66
- /** The document a STACKFACTOR reference cites, or "" when it cites none. */
67
- const documentOf = (reference) => {
68
- const target = reference.target ?? {};
69
- return (id(reference.knowledgeBase?.documentId) ||
70
- (target.elementType === STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE ? id(target.elementId) : ""));
71
- };
72
- /** The knowledge base a STACKFACTOR reference's document is in, or "" when it cites no document. */
73
- const knowledgeBaseOf = (reference) => {
74
- const target = reference.target ?? {};
75
- return (id(reference.knowledgeBase?.knowledgeBaseId) ||
76
- (target.elementType === STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE ? id(target.parentId) : ""));
77
- };
78
- /** A cap the caller passed (a whole number, 0 or more), else `fallback`. */
79
- const capOf = (value, fallback) => typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : fallback;
80
- /** The kinds the platform retired; it reads them as PUBLICATION. */
81
- const RETIRED_KINDS = new Set(["DOCUMENT", "KNOWLEDGE_BASE", "NOTE"]);
82
- /** A reference's kind as the platform reads it: missing or retired is PUBLICATION. */
83
- const resolveKind = (reference) => {
84
- const kind = reference?.kind;
85
- return typeof kind !== "string" || kind === "" || RETIRED_KINDS.has(kind) ? "PUBLICATION" : kind;
86
- };
87
- /** The pages among the references, each url once, under the first reference that has it. */
88
- const pagesOf = (references) => {
89
- const pages = [];
90
- const seen = new Set();
91
- references.forEach((reference, index) => {
92
- if (!reference || typeof reference !== "object")
93
- return;
94
- const kind = resolveKind(reference);
95
- if (kind !== "WEB" && kind !== "PUBLICATION")
96
- return;
97
- const url = text(reference.url);
98
- if (!/^https?:\/\//i.test(url) || seen.has(url))
99
- return;
100
- seen.add(url);
101
- pages.push({ id: shortId(index), title: text(reference.title) || url, url });
102
- });
103
- return pages;
104
- };
105
- /**
106
- * The short ids of the STACKFACTOR references citing each document, and of
107
- * those citing each chunk (`knowledgeBase.chunkId`) of a document.
108
- */
109
- const citersOf = (references) => {
110
- const byDocument = new Map();
111
- const byChunk = new Map();
112
- references.forEach((reference, index) => {
113
- if (reference?.kind !== STACKFACTOR_REFERENCE.KIND)
114
- return;
115
- const documentId = documentOf(reference);
116
- if (!documentId)
117
- return;
118
- byDocument.set(documentId, [...(byDocument.get(documentId) ?? []), shortId(index)]);
119
- const chunkId = id(reference.knowledgeBase?.chunkId);
120
- if (chunkId)
121
- byChunk.set(chunkId, [...(byChunk.get(chunkId) ?? []), shortId(index)]);
122
- });
123
- return { byChunk, byDocument };
124
- };
125
- /**
126
- * The error for cited knowledge bases that could not be read: it names the
127
- * documents of the STACKFACTOR references citing them.
128
- */
129
- const accessError = (references, unread, skipped) => new KnowledgeAccessError({
130
- documents: references
131
- .filter((reference) => reference?.kind === STACKFACTOR_REFERENCE.KIND && documentOf(reference) && unread.has(knowledgeBaseOf(reference)))
132
- .map((reference) => reference.title),
133
- skipped,
134
- });
135
- /**
136
- * What a list of references points at: its pages, and the passages of the
137
- * documents its STACKFACTOR references cite. One search per knowledge base per
138
- * query (per batch of 500 documents), however many references there are.
139
- *
140
- * References that cite a knowledge base the run cannot read stop it: with no
141
- * token, or when any cited knowledge base is skipped (refused or failed), it
142
- * throws a KnowledgeAccessError naming the documents. A search that works and
143
- * finds nothing is not an error. With no knowledge cited, no token is needed.
144
- *
145
- * A passage is attributed to the references citing its chunk, else to every
146
- * reference citing its document. The best-ranked `maxPassages` are kept, and
147
- * the first `maxPages` pages in reference order.
148
- * @param options `references` (a skill's, a microskill's…), `queries` to
149
- * search the cited documents with, `search` (client-api
150
- * `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK` per
151
- * search (default 5), `maxPassages` (default 40), `maxPages` (default: all)
152
- * @returns `{ pages, passages, skipped, citesKnowledge }`; `skipped` is always
153
- * empty, since a skip throws
154
- * @throws KnowledgeAccessError When cited knowledge bases cannot be read
155
- */
156
- export const gatherReferenceContext = async (options) => {
157
- const references = Array.isArray(options.references) ? options.references : [];
158
- const sources = sourcesFromReferences(references);
159
- const citesKnowledge = sources.length > 0;
160
- const pages = pagesOf(references).slice(0, capOf(options.maxPages, Infinity));
161
- if (!citesKnowledge) {
162
- logContext(pages.length, 0, []);
163
- return { citesKnowledge, pages, passages: [], skipped: [] };
164
- }
165
- if (!options.token) {
166
- const error = accessError(references, new Set(sources.map((source) => source.knowledgeBaseId)), []);
167
- logContext(pages.length, 0, [], { error, noToken: true });
168
- throw error;
169
- }
170
- const found = await retrievePassages({
171
- queries: options.queries,
172
- search: options.search,
173
- sources,
174
- token: options.token,
175
- topK: options.topK ?? DEFAULT_REFERENCE_TOP_K,
176
- });
177
- if (found.skipped.length > 0) {
178
- const unread = new Set(found.skipped.map((source) => source.knowledgeBaseId));
179
- const error = accessError(references, unread, found.skipped);
180
- logContext(pages.length, 0, found.skipped, { error });
181
- throw error;
182
- }
183
- const citers = citersOf(references);
184
- const passages = found.passages
185
- .filter((passage) => citers.byDocument.has(passage.documentId))
186
- .slice(0, capOf(options.maxPassages, DEFAULT_MAX_PASSAGES))
187
- .map((passage) => ({
188
- chunkId: passage.chunkId,
189
- document: passage.documentName,
190
- documentId: passage.documentId,
191
- knowledgeBaseId: passage.knowledgeBaseId,
192
- references: citers.byChunk.get(passage.chunkId) ?? citers.byDocument.get(passage.documentId),
193
- section: passage.sectionTitle,
194
- text: passage.text,
195
- }));
196
- logContext(pages.length, passages.length, []);
197
- return { citesKnowledge, pages, passages, skipped: [] };
198
- };
199
- /**
200
- * The one summary line of a gathering, for the logs: a warning when it was
201
- * refused. Document titles stay out of the logs.
202
- */
203
- const logContext = (pages, passages, skipped, refusal) => {
204
- const parts = [`${pages} page${pages === 1 ? "" : "s"}`, `${passages} passage${passages === 1 ? "" : "s"}`];
205
- if (skipped.length > 0) {
206
- parts.push(`skipped knowledge bases: ${skipped.map((s) => `${s.knowledgeBaseId} (noAccess: ${s.noAccess})`).join(", ")}`);
207
- }
208
- if (refusal?.noToken)
209
- parts.push("no token");
210
- if (refusal) {
211
- const count = refusal.error.documents.length;
212
- parts.push(`refused (${refusal.error.code}): ${count} document${count === 1 ? "" : "s"} unreadable`);
213
- }
214
- logger.log(null, refusal ? logger.levels.warn : logger.levels.info, `Reference context: ${parts.join("; ")}`);
215
- };
216
- /**
217
- * The passages as a prompt shows them: who cites each, where it came from, and
218
- * its text. The ids that locate it in the platform are left out.
219
- * @param context From `gatherReferenceContext`
220
- * @returns `[{ references, document, section, text }]`, for the model's input
221
- */
222
- export const referencePassagesForPrompt = (context) => context.passages.map((passage) => ({
223
- document: passage.document,
224
- references: passage.references,
225
- section: passage.section,
226
- text: passage.text,
227
- }));
228
- /**
229
- * The tools a run is given. Never `search`, `map` or `crawl`: a run reads what
230
- * its references point at and discovers nothing. `extract` only, only when
231
- * there are pages to open, and kept to those pages: any other url the model
232
- * asks for is refused (`allowedUrls`).
233
- * @param context From `gatherReferenceContext`
234
- * @returns `{ tavily: { tools: ["extract"], allowedUrls } }` with pages, else
235
- * `{}`. With no pages it returns `{}`, so a caller spreading it over a config
236
- * that already has a `tavily` key keeps that key (and `tavily: true` means
237
- * search plus extract): callers must not carry a base `tavily` key.
238
- */
239
- export const toolsFor = (context) => context.pages.length > 0
240
- ? { tavily: { tools: ["extract"], allowedUrls: context.pages.map((page) => page.url) } }
241
- : {};
242
- /**
243
- * The stage prompt followed by the guidance for each kind of source the run
244
- * has: pages, then passages; when it has neither (knowledge cited and readable
245
- * but no passage found included), the `none` guidance. An empty guidance adds
246
- * nothing.
247
- * @param stagePrompt The stage's own prompt
248
- * @param context From `gatherReferenceContext`
249
- * @param guidance From `sourceGuidanceFrom`
250
- * @returns The sections joined with a blank line
251
- */
252
- export const composeSourceInstructions = (stagePrompt, context, guidance) => {
253
- const sections = [];
254
- if (context.pages.length > 0)
255
- sections.push(guidance.pages);
256
- if (context.passages.length > 0)
257
- sections.push(guidance.passages);
258
- if (sections.length === 0)
259
- sections.push(guidance.none);
260
- return [stagePrompt, ...sections].filter(Boolean).join("\n\n");
261
- };
262
51
  /**
263
52
  * A reference as a model is shown it. A STACKFACTOR reference loses its pointer
264
53
  * and its url (ids mean nothing to a model, and the url is behind the
@@ -358,26 +147,10 @@ export const citedReferences = (ids, supplied, origin) => {
358
147
  }
359
148
  return cited;
360
149
  };
361
- /**
362
- * The three guidance texts an agent's config holds. A missing or non-string
363
- * value is an empty string.
364
- * @param config The agent's config
365
- */
366
- export const sourceGuidanceFrom = (config) => ({
367
- pages: text(config?.referencedPagesGuidance),
368
- passages: text(config?.knowledgeBasePassagesGuidance),
369
- none: text(config?.noReferencesGuidance),
370
- });
371
150
  export default {
372
151
  KnowledgeAccessError,
373
- REFERENCE_PASSAGES_KEY,
374
152
  citedReferences,
375
- composeSourceInstructions,
376
- gatherReferenceContext,
377
153
  referenceForModel,
378
- referencePassagesForPrompt,
379
154
  referencesForModel,
380
- sourceGuidanceFrom,
381
155
  toStoredReference,
382
- toolsFor,
383
156
  };
@@ -0,0 +1,64 @@
1
+ /** How long to wait before each retry, in milliseconds: one retry each. */
2
+ export declare const RETRY_WAITS: readonly number[];
3
+ export interface RetryOptions {
4
+ /** How long to wait before each retry, in milliseconds: as many retries as waits. Default RETRY_WAITS. */
5
+ waits?: readonly number[];
6
+ /**
7
+ * The breaker: the keys whose retries a call has spent. A call whose key is
8
+ * in it is made once, with no retry and no wait; one that spends its
9
+ * retries adds its key; one that answers takes it out.
10
+ */
11
+ breaker?: Set<string>;
12
+ /** The call's key in the breaker (a knowledge base id); with none, the breaker is left alone. */
13
+ key?: string | null;
14
+ /**
15
+ * Asked after each failure and again after each wait; true when retrying
16
+ * has become pointless (the knowledge base refused the person meanwhile):
17
+ * the failure is passed on at once, and the breaker is left alone.
18
+ */
19
+ stopWhen?: () => boolean;
20
+ /** How to wait; swappable in tests. */
21
+ sleep?: (ms: number) => Promise<void>;
22
+ }
23
+ /**
24
+ * A failure's message: an Error's own, any other value as text.
25
+ * @param error What a call failed with
26
+ */
27
+ export declare const messageOf: (error: unknown) => string;
28
+ /**
29
+ * The status a failed platform call answered with, as an axios error carries
30
+ * it: on `status`, else on `response.status`; a status sent as text is read
31
+ * as its number. Null when there is none.
32
+ * @param error What the call failed with
33
+ */
34
+ export declare const statusOf: (error: unknown) => number | null;
35
+ /**
36
+ * Whether a failed call may succeed when made again: its status is one of
37
+ * TRANSIENT_STATUSES; or it has none and the network failed it — a TypeError
38
+ * only as fetch's "fetch failed"; any other failure by a code of
39
+ * NETWORK_CODES, or a message NETWORK_MESSAGE matches (a failure that is no
40
+ * Error is read as its text).
41
+ * @param error What the call failed with
42
+ */
43
+ export declare const isTransient: (error: unknown) => boolean;
44
+ /**
45
+ * Whether a call got no answer, or lost it part-way, because of the network:
46
+ * a failure with no status that may pass (isTransient). One that carries a
47
+ * status was answered, and is its caller's to read.
48
+ * @param error What the call failed with
49
+ */
50
+ export declare const isNetworkFailure: (error: unknown) => boolean;
51
+ /**
52
+ * Makes a platform call, and makes it again after each of the waits while it
53
+ * fails for a reason that may pass (isTransient), logging each retry.
54
+ * @param what The call, as the log names it (`search "C#" in knowledge base kb-1`)
55
+ * @param call The call
56
+ * @param options `waits` (default RETRY_WAITS), `breaker` and `key`,
57
+ * `stopWhen`, `sleep`
58
+ * @returns What the call answered
59
+ * @throws What the call failed with: at once when it may not pass, or when
60
+ * `stopWhen` says retrying is pointless; the last failure once the
61
+ * retries are spent
62
+ */
63
+ export declare const retryTransient: <T>(what: string, call: () => Promise<T>, options?: RetryOptions) => Promise<T>;
64
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/retry.ts"],"names":[],"mappings":"AA4CA,2EAA2E;AAC3E,eAAO,MAAM,WAAW,EAAE,SAAS,MAAM,EAAgC,CAAC;AAE1E,MAAM,WAAW,YAAY;IAC3B,0GAA0G;IAC1G,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B;;;;OAIG;IACH,OAAO,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtB,iGAAiG;IACjG,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,OAAO,CAAC;IACzB,uCAAuC;IACvC,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAID;;;GAGG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,KAAG,MAAkE,CAAC;AAE9G;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,IAMlD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,KAAG,OAO5C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,OAAO,KAAG,OAAyD,CAAC;AAE5G;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc,GAAU,CAAC,EAAE,MAAM,MAAM,EAAE,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,UAAS,YAAiB,KAAG,OAAO,CAAC,CAAC,CA6BnH,CAAC"}