@stackfactor/agent-utils 1.13.4 → 1.32.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 +667 -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 +105 -0
  63. package/dist/cjs/learningContentPlan.d.ts.map +1 -0
  64. package/dist/cjs/learningContentPlan.js +305 -0
  65. package/dist/cjs/learningContentThread.d.ts +184 -0
  66. package/dist/cjs/learningContentThread.d.ts.map +1 -0
  67. package/dist/cjs/learningContentThread.js +407 -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 +105 -0
  188. package/dist/esm/learningContentPlan.d.ts.map +1 -0
  189. package/dist/esm/learningContentPlan.js +299 -0
  190. package/dist/esm/learningContentThread.d.ts +184 -0
  191. package/dist/esm/learningContentThread.d.ts.map +1 -0
  192. package/dist/esm/learningContentThread.js +364 -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,55 +1,38 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sourceGuidanceFrom = exports.citedReferences = exports.toStoredReference = exports.referencesForModel = exports.referenceForModel = exports.composeSourceInstructions = exports.toolsFor = exports.referencePassagesForPrompt = exports.gatherReferenceContext = exports.KnowledgeAccessError = exports.REFERENCE_PASSAGES_KEY = void 0;
7
- //
8
- // Reference context — gathering what a list of references points at
9
- //
10
- // An agent that reads its references never discovers new sources. It reads
11
- // what they point at:
3
+ exports.citedReferences = exports.toStoredReference = exports.referencesForModel = exports.referenceForModel = exports.KnowledgeAccessError = void 0;
12
4
  //
13
- // - pages: WEB and PUBLICATION references with an http(s) url, which the
14
- // agent opens through Tavily `extract`. A STACKFACTOR reference's url is
15
- // the document's page at its source and is never opened as a page;
16
- // - passages: the passages of the knowledge base documents STACKFACTOR
17
- // references cite, searched under the run's own token, so a run reads only
18
- // what the person it acts for may open.
5
+ // References — how a run's references are shown to a model and stored, and
6
+ // the error a run stops with when the documents they cite cannot be read
19
7
  //
20
- // A cited knowledge base that cannot be read stops the run (Candin,
21
- // 2026-10-13): with no token, a refusal or a failed search, the gathering
22
- // throws a KnowledgeAccessError naming the documents, and the agent answers
23
- // with it rather than writing as if the content cited nothing. A search that
24
- // works and finds nothing is not an error.
8
+ // Reading what references point at is `sourceStore`'s work. What stays here:
25
9
  //
26
- // A reference is cited by its short id `r<N>`, N being its index in the
27
- // supplied list, whatever its kind.
10
+ // - `KnowledgeAccessError`: a cited knowledge base that cannot be read stops
11
+ // the run (Candin, 2026-10-13). With no token, a refusal or a failed read,
12
+ // `sourceStore.prepareSources` throws it naming the documents, and the
13
+ // agent answers with it rather than writing as if the content cited
14
+ // nothing;
15
+ // - `referencesForModel`, `citedReferences`, `toStoredReference`: a
16
+ // reference is cited by its short id `r<N>`, N being its index in the
17
+ // supplied list, whatever its kind.
28
18
  //
29
- const logger_js_1 = __importDefault(require("./logger.js"));
30
19
  const stackfactorKnowledge_js_1 = require("./stackfactorKnowledge.js");
31
- /** How many passages one search asks for when the caller does not say. */
32
- const DEFAULT_REFERENCE_TOP_K = 5;
33
- /** How many passages a gathering keeps when the caller does not say: the best-ranked. */
34
- const DEFAULT_MAX_PASSAGES = 40;
35
- /** The prompt key the passages are handed to the model under. */
36
- exports.REFERENCE_PASSAGES_KEY = "Knowledge Base Passages";
37
20
  const text = (value) => (typeof value === "string" ? value.trim() : "");
38
21
  /**
39
22
  * The knowledge base documents a run's references cite could not be read: the
40
23
  * run has no token, the person may not open a cited knowledge base (401, 403,
41
- * 404), or its search failed. The agent answers with it — `code` and `message`
24
+ * 404), or reading it failed. The agent answers with it — `code` and `message`
42
25
  * are what the person sees — and generates nothing.
43
26
  */
44
27
  class KnowledgeAccessError extends Error {
45
28
  /** 403 when every skipped knowledge base refused the person (or there was no token), else 502. */
46
29
  code;
47
- /** The knowledge bases that could not be searched, and why; empty when there was no token. */
30
+ /** The knowledge bases that could not be read, and why; empty when there was no token. */
48
31
  skipped;
49
32
  /** The titles of the STACKFACTOR references citing those knowledge bases, each once. */
50
33
  documents;
51
34
  /**
52
- * @param details `skipped` (from the search) and `documents` (titles); the
35
+ * @param details `skipped` (from the read) and `documents` (titles); the
53
36
  * code and the message follow from them
54
37
  */
55
38
  constructor(details = {}) {
@@ -59,7 +42,7 @@ class KnowledgeAccessError extends Error {
59
42
  const titles = documents.map((title) => `"${title}"`).join(", ");
60
43
  super(code === 403
61
44
  ? `The knowledge base documents this content cites could not be read with your access${titles ? `: ${titles}` : ""}.`
62
- : `The knowledge base documents this content cites could not be read: the knowledge base search failed${titles ? ` for ${titles}` : ""}.`);
45
+ : `The knowledge base documents this content cites could not be read: reading the knowledge base failed${titles ? ` for ${titles}` : ""}.`);
63
46
  this.name = "KnowledgeAccessError";
64
47
  this.code = code;
65
48
  this.skipped = skipped;
@@ -68,208 +51,7 @@ class KnowledgeAccessError extends Error {
68
51
  }
69
52
  }
70
53
  exports.KnowledgeAccessError = KnowledgeAccessError;
71
- const id = (value) => typeof value === "string" ? value.trim() : value != null && typeof value === "object" ? String(value) : "";
72
54
  const shortId = (index) => `r${index}`;
73
- /** The document a STACKFACTOR reference cites, or "" when it cites none. */
74
- const documentOf = (reference) => {
75
- const target = reference.target ?? {};
76
- return (id(reference.knowledgeBase?.documentId) ||
77
- (target.elementType === stackfactorKnowledge_js_1.STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE ? id(target.elementId) : ""));
78
- };
79
- /** The knowledge base a STACKFACTOR reference's document is in, or "" when it cites no document. */
80
- const knowledgeBaseOf = (reference) => {
81
- const target = reference.target ?? {};
82
- return (id(reference.knowledgeBase?.knowledgeBaseId) ||
83
- (target.elementType === stackfactorKnowledge_js_1.STACKFACTOR_REFERENCE.TARGET_ELEMENT_TYPE ? id(target.parentId) : ""));
84
- };
85
- /** A cap the caller passed (a whole number, 0 or more), else `fallback`. */
86
- const capOf = (value, fallback) => typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : fallback;
87
- /** The kinds the platform retired; it reads them as PUBLICATION. */
88
- const RETIRED_KINDS = new Set(["DOCUMENT", "KNOWLEDGE_BASE", "NOTE"]);
89
- /** A reference's kind as the platform reads it: missing or retired is PUBLICATION. */
90
- const resolveKind = (reference) => {
91
- const kind = reference?.kind;
92
- return typeof kind !== "string" || kind === "" || RETIRED_KINDS.has(kind) ? "PUBLICATION" : kind;
93
- };
94
- /** The pages among the references, each url once, under the first reference that has it. */
95
- const pagesOf = (references) => {
96
- const pages = [];
97
- const seen = new Set();
98
- references.forEach((reference, index) => {
99
- if (!reference || typeof reference !== "object")
100
- return;
101
- const kind = resolveKind(reference);
102
- if (kind !== "WEB" && kind !== "PUBLICATION")
103
- return;
104
- const url = text(reference.url);
105
- if (!/^https?:\/\//i.test(url) || seen.has(url))
106
- return;
107
- seen.add(url);
108
- pages.push({ id: shortId(index), title: text(reference.title) || url, url });
109
- });
110
- return pages;
111
- };
112
- /**
113
- * The short ids of the STACKFACTOR references citing each document, and of
114
- * those citing each chunk (`knowledgeBase.chunkId`) of a document.
115
- */
116
- const citersOf = (references) => {
117
- const byDocument = new Map();
118
- const byChunk = new Map();
119
- references.forEach((reference, index) => {
120
- if (reference?.kind !== stackfactorKnowledge_js_1.STACKFACTOR_REFERENCE.KIND)
121
- return;
122
- const documentId = documentOf(reference);
123
- if (!documentId)
124
- return;
125
- byDocument.set(documentId, [...(byDocument.get(documentId) ?? []), shortId(index)]);
126
- const chunkId = id(reference.knowledgeBase?.chunkId);
127
- if (chunkId)
128
- byChunk.set(chunkId, [...(byChunk.get(chunkId) ?? []), shortId(index)]);
129
- });
130
- return { byChunk, byDocument };
131
- };
132
- /**
133
- * The error for cited knowledge bases that could not be read: it names the
134
- * documents of the STACKFACTOR references citing them.
135
- */
136
- const accessError = (references, unread, skipped) => new KnowledgeAccessError({
137
- documents: references
138
- .filter((reference) => reference?.kind === stackfactorKnowledge_js_1.STACKFACTOR_REFERENCE.KIND && documentOf(reference) && unread.has(knowledgeBaseOf(reference)))
139
- .map((reference) => reference.title),
140
- skipped,
141
- });
142
- /**
143
- * What a list of references points at: its pages, and the passages of the
144
- * documents its STACKFACTOR references cite. One search per knowledge base per
145
- * query (per batch of 500 documents), however many references there are.
146
- *
147
- * References that cite a knowledge base the run cannot read stop it: with no
148
- * token, or when any cited knowledge base is skipped (refused or failed), it
149
- * throws a KnowledgeAccessError naming the documents. A search that works and
150
- * finds nothing is not an error. With no knowledge cited, no token is needed.
151
- *
152
- * A passage is attributed to the references citing its chunk, else to every
153
- * reference citing its document. The best-ranked `maxPassages` are kept, and
154
- * the first `maxPages` pages in reference order.
155
- * @param options `references` (a skill's, a microskill's…), `queries` to
156
- * search the cited documents with, `search` (client-api
157
- * `knowledgeBaseAgent.search`), `token` (the run's AGENT token), `topK` per
158
- * search (default 5), `maxPassages` (default 40), `maxPages` (default: all)
159
- * @returns `{ pages, passages, skipped, citesKnowledge }`; `skipped` is always
160
- * empty, since a skip throws
161
- * @throws KnowledgeAccessError When cited knowledge bases cannot be read
162
- */
163
- const gatherReferenceContext = async (options) => {
164
- const references = Array.isArray(options.references) ? options.references : [];
165
- const sources = (0, stackfactorKnowledge_js_1.sourcesFromReferences)(references);
166
- const citesKnowledge = sources.length > 0;
167
- const pages = pagesOf(references).slice(0, capOf(options.maxPages, Infinity));
168
- if (!citesKnowledge) {
169
- logContext(pages.length, 0, []);
170
- return { citesKnowledge, pages, passages: [], skipped: [] };
171
- }
172
- if (!options.token) {
173
- const error = accessError(references, new Set(sources.map((source) => source.knowledgeBaseId)), []);
174
- logContext(pages.length, 0, [], { error, noToken: true });
175
- throw error;
176
- }
177
- const found = await (0, stackfactorKnowledge_js_1.retrievePassages)({
178
- queries: options.queries,
179
- search: options.search,
180
- sources,
181
- token: options.token,
182
- topK: options.topK ?? DEFAULT_REFERENCE_TOP_K,
183
- });
184
- if (found.skipped.length > 0) {
185
- const unread = new Set(found.skipped.map((source) => source.knowledgeBaseId));
186
- const error = accessError(references, unread, found.skipped);
187
- logContext(pages.length, 0, found.skipped, { error });
188
- throw error;
189
- }
190
- const citers = citersOf(references);
191
- const passages = found.passages
192
- .filter((passage) => citers.byDocument.has(passage.documentId))
193
- .slice(0, capOf(options.maxPassages, DEFAULT_MAX_PASSAGES))
194
- .map((passage) => ({
195
- chunkId: passage.chunkId,
196
- document: passage.documentName,
197
- documentId: passage.documentId,
198
- knowledgeBaseId: passage.knowledgeBaseId,
199
- references: citers.byChunk.get(passage.chunkId) ?? citers.byDocument.get(passage.documentId),
200
- section: passage.sectionTitle,
201
- text: passage.text,
202
- }));
203
- logContext(pages.length, passages.length, []);
204
- return { citesKnowledge, pages, passages, skipped: [] };
205
- };
206
- exports.gatherReferenceContext = gatherReferenceContext;
207
- /**
208
- * The one summary line of a gathering, for the logs: a warning when it was
209
- * refused. Document titles stay out of the logs.
210
- */
211
- const logContext = (pages, passages, skipped, refusal) => {
212
- const parts = [`${pages} page${pages === 1 ? "" : "s"}`, `${passages} passage${passages === 1 ? "" : "s"}`];
213
- if (skipped.length > 0) {
214
- parts.push(`skipped knowledge bases: ${skipped.map((s) => `${s.knowledgeBaseId} (noAccess: ${s.noAccess})`).join(", ")}`);
215
- }
216
- if (refusal?.noToken)
217
- parts.push("no token");
218
- if (refusal) {
219
- const count = refusal.error.documents.length;
220
- parts.push(`refused (${refusal.error.code}): ${count} document${count === 1 ? "" : "s"} unreadable`);
221
- }
222
- logger_js_1.default.log(null, refusal ? logger_js_1.default.levels.warn : logger_js_1.default.levels.info, `Reference context: ${parts.join("; ")}`);
223
- };
224
- /**
225
- * The passages as a prompt shows them: who cites each, where it came from, and
226
- * its text. The ids that locate it in the platform are left out.
227
- * @param context From `gatherReferenceContext`
228
- * @returns `[{ references, document, section, text }]`, for the model's input
229
- */
230
- const referencePassagesForPrompt = (context) => context.passages.map((passage) => ({
231
- document: passage.document,
232
- references: passage.references,
233
- section: passage.section,
234
- text: passage.text,
235
- }));
236
- exports.referencePassagesForPrompt = referencePassagesForPrompt;
237
- /**
238
- * The tools a run is given. Never `search`, `map` or `crawl`: a run reads what
239
- * its references point at and discovers nothing. `extract` only, only when
240
- * there are pages to open, and kept to those pages: any other url the model
241
- * asks for is refused (`allowedUrls`).
242
- * @param context From `gatherReferenceContext`
243
- * @returns `{ tavily: { tools: ["extract"], allowedUrls } }` with pages, else
244
- * `{}`. With no pages it returns `{}`, so a caller spreading it over a config
245
- * that already has a `tavily` key keeps that key (and `tavily: true` means
246
- * search plus extract): callers must not carry a base `tavily` key.
247
- */
248
- const toolsFor = (context) => context.pages.length > 0
249
- ? { tavily: { tools: ["extract"], allowedUrls: context.pages.map((page) => page.url) } }
250
- : {};
251
- exports.toolsFor = toolsFor;
252
- /**
253
- * The stage prompt followed by the guidance for each kind of source the run
254
- * has: pages, then passages; when it has neither (knowledge cited and readable
255
- * but no passage found included), the `none` guidance. An empty guidance adds
256
- * nothing.
257
- * @param stagePrompt The stage's own prompt
258
- * @param context From `gatherReferenceContext`
259
- * @param guidance From `sourceGuidanceFrom`
260
- * @returns The sections joined with a blank line
261
- */
262
- const composeSourceInstructions = (stagePrompt, context, guidance) => {
263
- const sections = [];
264
- if (context.pages.length > 0)
265
- sections.push(guidance.pages);
266
- if (context.passages.length > 0)
267
- sections.push(guidance.passages);
268
- if (sections.length === 0)
269
- sections.push(guidance.none);
270
- return [stagePrompt, ...sections].filter(Boolean).join("\n\n");
271
- };
272
- exports.composeSourceInstructions = composeSourceInstructions;
273
55
  /**
274
56
  * A reference as a model is shown it. A STACKFACTOR reference loses its pointer
275
57
  * and its url (ids mean nothing to a model, and the url is behind the
@@ -373,27 +155,10 @@ const citedReferences = (ids, supplied, origin) => {
373
155
  return cited;
374
156
  };
375
157
  exports.citedReferences = citedReferences;
376
- /**
377
- * The three guidance texts an agent's config holds. A missing or non-string
378
- * value is an empty string.
379
- * @param config The agent's config
380
- */
381
- const sourceGuidanceFrom = (config) => ({
382
- pages: text(config?.referencedPagesGuidance),
383
- passages: text(config?.knowledgeBasePassagesGuidance),
384
- none: text(config?.noReferencesGuidance),
385
- });
386
- exports.sourceGuidanceFrom = sourceGuidanceFrom;
387
158
  exports.default = {
388
159
  KnowledgeAccessError,
389
- REFERENCE_PASSAGES_KEY: exports.REFERENCE_PASSAGES_KEY,
390
160
  citedReferences: exports.citedReferences,
391
- composeSourceInstructions: exports.composeSourceInstructions,
392
- gatherReferenceContext: exports.gatherReferenceContext,
393
161
  referenceForModel: exports.referenceForModel,
394
- referencePassagesForPrompt: exports.referencePassagesForPrompt,
395
162
  referencesForModel: exports.referencesForModel,
396
- sourceGuidanceFrom: exports.sourceGuidanceFrom,
397
163
  toStoredReference: exports.toStoredReference,
398
- toolsFor: exports.toolsFor,
399
164
  };
@@ -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"}