digital-workers 2.1.1 → 2.3.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 (197) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +136 -180
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +34 -21
  5. package/dist/actions.js.map +1 -1
  6. package/dist/agent-comms.d.ts +438 -0
  7. package/dist/agent-comms.d.ts.map +1 -0
  8. package/dist/agent-comms.js +677 -0
  9. package/dist/agent-comms.js.map +1 -0
  10. package/dist/approve.d.ts +40 -8
  11. package/dist/approve.d.ts.map +1 -1
  12. package/dist/approve.js +86 -20
  13. package/dist/approve.js.map +1 -1
  14. package/dist/ask.d.ts +38 -7
  15. package/dist/ask.d.ts.map +1 -1
  16. package/dist/ask.js +85 -25
  17. package/dist/ask.js.map +1 -1
  18. package/dist/browse.d.ts +223 -0
  19. package/dist/browse.d.ts.map +1 -0
  20. package/dist/browse.js +392 -0
  21. package/dist/browse.js.map +1 -0
  22. package/dist/capability-tiers.d.ts +230 -0
  23. package/dist/capability-tiers.d.ts.map +1 -0
  24. package/dist/capability-tiers.js +388 -0
  25. package/dist/capability-tiers.js.map +1 -0
  26. package/dist/cascade-context.d.ts +523 -0
  27. package/dist/cascade-context.d.ts.map +1 -0
  28. package/dist/cascade-context.js +494 -0
  29. package/dist/cascade-context.js.map +1 -0
  30. package/dist/client.d.ts +162 -0
  31. package/dist/client.d.ts.map +1 -0
  32. package/dist/client.js +64 -0
  33. package/dist/client.js.map +1 -0
  34. package/dist/decide.d.ts +42 -6
  35. package/dist/decide.d.ts.map +1 -1
  36. package/dist/decide.js +54 -11
  37. package/dist/decide.js.map +1 -1
  38. package/dist/do.d.ts +36 -7
  39. package/dist/do.d.ts.map +1 -1
  40. package/dist/do.js +82 -39
  41. package/dist/do.js.map +1 -1
  42. package/dist/error-escalation.d.ts +416 -0
  43. package/dist/error-escalation.d.ts.map +1 -0
  44. package/dist/error-escalation.js +656 -0
  45. package/dist/error-escalation.js.map +1 -0
  46. package/dist/generate.d.ts +48 -7
  47. package/dist/generate.d.ts.map +1 -1
  48. package/dist/generate.js +49 -8
  49. package/dist/generate.js.map +1 -1
  50. package/dist/goals.d.ts +10 -9
  51. package/dist/goals.d.ts.map +1 -1
  52. package/dist/goals.js +30 -24
  53. package/dist/goals.js.map +1 -1
  54. package/dist/image.d.ts +189 -0
  55. package/dist/image.d.ts.map +1 -0
  56. package/dist/image.js +528 -0
  57. package/dist/image.js.map +1 -0
  58. package/dist/index.d.ts +59 -2
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +92 -2
  61. package/dist/index.js.map +1 -1
  62. package/dist/is.d.ts +45 -10
  63. package/dist/is.d.ts.map +1 -1
  64. package/dist/is.js +56 -21
  65. package/dist/is.js.map +1 -1
  66. package/dist/kpis.d.ts +24 -15
  67. package/dist/kpis.d.ts.map +1 -1
  68. package/dist/kpis.js +16 -14
  69. package/dist/kpis.js.map +1 -1
  70. package/dist/load-balancing.d.ts +395 -0
  71. package/dist/load-balancing.d.ts.map +1 -0
  72. package/dist/load-balancing.js +991 -0
  73. package/dist/load-balancing.js.map +1 -0
  74. package/dist/logger.d.ts +76 -0
  75. package/dist/logger.d.ts.map +1 -0
  76. package/dist/logger.js +39 -0
  77. package/dist/logger.js.map +1 -0
  78. package/dist/notify.d.ts +38 -9
  79. package/dist/notify.d.ts.map +1 -1
  80. package/dist/notify.js +72 -17
  81. package/dist/notify.js.map +1 -1
  82. package/dist/role.d.ts +5 -4
  83. package/dist/role.d.ts.map +1 -1
  84. package/dist/role.js +13 -10
  85. package/dist/role.js.map +1 -1
  86. package/dist/runtime.d.ts +310 -0
  87. package/dist/runtime.d.ts.map +1 -0
  88. package/dist/runtime.js +510 -0
  89. package/dist/runtime.js.map +1 -0
  90. package/dist/team.d.ts +11 -6
  91. package/dist/team.d.ts.map +1 -1
  92. package/dist/team.js +22 -15
  93. package/dist/team.js.map +1 -1
  94. package/dist/transports/email.d.ts +318 -0
  95. package/dist/transports/email.d.ts.map +1 -0
  96. package/dist/transports/email.js +779 -0
  97. package/dist/transports/email.js.map +1 -0
  98. package/dist/transports/slack.d.ts +515 -0
  99. package/dist/transports/slack.d.ts.map +1 -0
  100. package/dist/transports/slack.js +844 -0
  101. package/dist/transports/slack.js.map +1 -0
  102. package/dist/transports.d.ts.map +1 -1
  103. package/dist/transports.js +44 -25
  104. package/dist/transports.js.map +1 -1
  105. package/dist/types.d.ts +149 -19
  106. package/dist/types.d.ts.map +1 -1
  107. package/dist/types.js +6 -0
  108. package/dist/types.js.map +1 -1
  109. package/dist/utils/id.d.ts +19 -0
  110. package/dist/utils/id.d.ts.map +1 -0
  111. package/dist/utils/id.js +21 -0
  112. package/dist/utils/id.js.map +1 -0
  113. package/dist/video.d.ts +203 -0
  114. package/dist/video.d.ts.map +1 -0
  115. package/dist/video.js +528 -0
  116. package/dist/video.js.map +1 -0
  117. package/dist/worker.d.ts +343 -0
  118. package/dist/worker.d.ts.map +1 -0
  119. package/dist/worker.js +698 -0
  120. package/dist/worker.js.map +1 -0
  121. package/package.json +24 -5
  122. package/src/actions.ts +48 -38
  123. package/src/agent-comms.ts +1200 -0
  124. package/src/approve.ts +91 -20
  125. package/src/ask.ts +99 -25
  126. package/src/browse.ts +627 -0
  127. package/src/capability-tiers.ts +545 -0
  128. package/src/cascade-context.ts +648 -0
  129. package/src/client.ts +221 -0
  130. package/src/decide.ts +81 -35
  131. package/src/do.ts +98 -52
  132. package/src/error-escalation.ts +1123 -0
  133. package/src/generate.ts +52 -18
  134. package/src/goals.ts +36 -27
  135. package/src/image.ts +816 -0
  136. package/src/index.ts +410 -2
  137. package/src/is.ts +59 -25
  138. package/src/kpis.ts +41 -36
  139. package/src/load-balancing.ts +1467 -0
  140. package/src/logger.ts +93 -0
  141. package/src/notify.ts +78 -17
  142. package/src/role.ts +30 -20
  143. package/src/runtime.ts +796 -0
  144. package/src/team.ts +24 -19
  145. package/src/transports/email.ts +1160 -0
  146. package/src/transports/slack.ts +1320 -0
  147. package/src/transports.ts +58 -43
  148. package/src/types.ts +182 -46
  149. package/src/utils/id.ts +21 -0
  150. package/src/video.ts +906 -0
  151. package/src/worker.ts +1007 -0
  152. package/test/agent-comms.test.ts +1397 -0
  153. package/test/approve.test.ts +305 -0
  154. package/test/ask.test.ts +274 -0
  155. package/test/browse.test.ts +361 -0
  156. package/test/capability-tiers.test.ts +631 -0
  157. package/test/cascade-context.test.ts +692 -0
  158. package/test/decide.test.ts +252 -0
  159. package/test/do.test.ts +144 -0
  160. package/test/error-escalation.test.ts +1205 -0
  161. package/test/error-logging.test.ts +357 -0
  162. package/test/generate.test.ts +319 -0
  163. package/test/image.test.ts +398 -0
  164. package/test/is.test.ts +287 -0
  165. package/test/load-balancing-safety.test.ts +404 -0
  166. package/test/load-balancing-thread-safety.test.ts +464 -0
  167. package/test/load-balancing.test.ts +1145 -0
  168. package/test/notify.test.ts +434 -0
  169. package/test/primitives.test.ts +320 -0
  170. package/test/runtime-integration.test.ts +892 -0
  171. package/test/transports/crypto.test.ts +230 -0
  172. package/test/transports/email.test.ts +866 -0
  173. package/test/transports/id-generation.test.ts +91 -0
  174. package/test/transports/slack.test.ts +760 -0
  175. package/test/type-safety.test.ts +834 -0
  176. package/test/types.test.ts +95 -2
  177. package/test/video.test.ts +530 -0
  178. package/test/worker.test.ts +1433 -0
  179. package/tsconfig.json +4 -1
  180. package/vitest.config.ts +42 -0
  181. package/wrangler.jsonc +36 -0
  182. package/.turbo/turbo-build.log +0 -5
  183. package/src/actions.js +0 -436
  184. package/src/approve.js +0 -234
  185. package/src/ask.js +0 -226
  186. package/src/decide.js +0 -244
  187. package/src/do.js +0 -227
  188. package/src/generate.js +0 -298
  189. package/src/goals.js +0 -205
  190. package/src/index.js +0 -68
  191. package/src/is.js +0 -317
  192. package/src/kpis.js +0 -270
  193. package/src/notify.js +0 -219
  194. package/src/role.js +0 -110
  195. package/src/team.js +0 -130
  196. package/src/transports.js +0 -357
  197. package/src/types.js +0 -71
package/src/generate.ts CHANGED
@@ -1,5 +1,34 @@
1
1
  /**
2
2
  * Content generation functionality for digital workers
3
+ *
4
+ * IMPORTANT: Worker-Context Generation vs Direct LLM Generation
5
+ * --------------------------------------------------------------
6
+ * This module provides content generation within a worker context,
7
+ * with rich metadata about the generation process.
8
+ *
9
+ * - `digital-workers.generate()` - Generates content with full metadata
10
+ * (tokens used, duration, model info) and supports multiple content types
11
+ * including text, code, structured, image, video, and audio.
12
+ *
13
+ * - `ai-functions.generate()` - Core generation primitive that powers all
14
+ * ai-functions; lower-level function with type-based dispatch.
15
+ *
16
+ * The key difference is context and metadata:
17
+ * - digital-workers returns `GenerateResult<T>` with content + metadata
18
+ * - ai-functions returns just the generated content
19
+ *
20
+ * Use digital-workers when you need:
21
+ * - Rich metadata about generation (tokens, duration, model)
22
+ * - Content type variants (text, code, structured, media)
23
+ * - Tone, audience, and length modifiers
24
+ * - Iterative refinement workflows
25
+ *
26
+ * Use ai-functions when you need:
27
+ * - Direct LLM generation without metadata
28
+ * - Template literal syntax (`ai\`prompt\``)
29
+ * - Promise pipelining and schema inference
30
+ *
31
+ * @module
3
32
  */
4
33
 
5
34
  import { generateObject, generateText } from 'ai-functions'
@@ -7,23 +36,31 @@ import type { SimpleSchema } from 'ai-functions'
7
36
  import type { GenerateResult, GenerateOptions } from './types.js'
8
37
 
9
38
  /**
10
- * Generate content
39
+ * Generate content with rich metadata and multiple content type support.
11
40
  *
12
- * Uses AI to generate various types of content including text,
13
- * code, structured data, images, video, and audio.
41
+ * **Key Difference from ai-functions.generate():**
42
+ * Unlike `ai-functions.generate()` which is a lower-level type-dispatch
43
+ * function, this function returns a `GenerateResult` wrapper with:
44
+ * - The generated content
45
+ * - Generation metadata (model, tokens, duration)
46
+ * - Content type information
47
+ *
48
+ * This provides better observability for worker-based content generation.
14
49
  *
15
50
  * @param prompt - What to generate
16
- * @param options - Generation options
17
- * @returns Promise resolving to generated content
51
+ * @param options - Generation options (type, schema, instructions, model)
52
+ * @returns Promise resolving to GenerateResult with content and metadata
18
53
  *
19
54
  * @example
20
55
  * ```ts
21
- * // Generate text content
56
+ * // Generate text content with metadata
22
57
  * const result = await generate('Write a product description for wireless earbuds', {
23
58
  * type: 'text',
24
59
  * instructions: 'Focus on sound quality and battery life. Keep it under 100 words.',
25
60
  * })
26
- * console.log(result.content)
61
+ * console.log(result.content) // The generated text
62
+ * console.log(result.metadata.tokens) // Tokens used
63
+ * console.log(result.metadata.duration) // Generation time in ms
27
64
  * ```
28
65
  *
29
66
  * @example
@@ -52,18 +89,17 @@ import type { GenerateResult, GenerateOptions } from './types.js'
52
89
  * instructions: 'Use TypeScript and hooks. Include prop types.',
53
90
  * })
54
91
  * console.log(result.content) // TypeScript React component code
92
+ * console.log(result.metadata.language) // 'typescript'
55
93
  * ```
94
+ *
95
+ * @see {@link ai-functions#generate} for lower-level type-dispatch generation
96
+ * @see {@link ai-functions#ai} for template literal generation syntax
56
97
  */
57
98
  export async function generate<T = string>(
58
99
  prompt: string,
59
100
  options: GenerateOptions = {}
60
101
  ): Promise<GenerateResult<T>> {
61
- const {
62
- type = 'text',
63
- schema,
64
- instructions,
65
- model = 'sonnet',
66
- } = options
102
+ const { type = 'text', schema, instructions, model = 'sonnet' } = options
67
103
 
68
104
  const startTime = Date.now()
69
105
 
@@ -111,7 +147,7 @@ export async function generate<T = string>(
111
147
  type: 'structured',
112
148
  metadata: {
113
149
  model,
114
- tokens: result.usage?.totalTokens,
150
+ tokens: (result.usage as { totalTokens?: number } | undefined)?.totalTokens,
115
151
  duration: Date.now() - startTime,
116
152
  },
117
153
  }
@@ -144,7 +180,7 @@ export async function generate<T = string>(
144
180
  type: 'code',
145
181
  metadata: {
146
182
  model,
147
- tokens: result.usage?.totalTokens,
183
+ tokens: (result.usage as { totalTokens?: number } | undefined)?.totalTokens,
148
184
  duration: Date.now() - startTime,
149
185
  language: codeResult.language,
150
186
  explanation: codeResult.explanation,
@@ -199,9 +235,7 @@ generate.variations = async <T = string>(
199
235
  count: number,
200
236
  options: GenerateOptions = {}
201
237
  ): Promise<Array<GenerateResult<T>>> => {
202
- return Promise.all(
203
- Array.from({ length: count }, () => generate<T>(prompt, options))
204
- )
238
+ return Promise.all(Array.from({ length: count }, () => generate<T>(prompt, options)))
205
239
  }
206
240
 
207
241
  /**
package/src/goals.ts CHANGED
@@ -2,7 +2,10 @@
2
2
  * Goals definition for digital workers
3
3
  */
4
4
 
5
- import type { WorkerGoals, KPI } from './types.js'
5
+ import { calculateProgress, isOnTrack } from 'org.ai'
6
+ import type { WorkerGoals, WorkerKPI } from './types.js'
7
+
8
+ // Note: Goal, Goals types are re-exported from types.ts which imports from org.ai
6
9
 
7
10
  /**
8
11
  * Define worker goals
@@ -15,7 +18,7 @@ import type { WorkerGoals, KPI } from './types.js'
15
18
  *
16
19
  * @example
17
20
  * ```ts
18
- * const engineeringGoals = Goals({
21
+ * const engineeringGoals = defineGoals({
19
22
  * shortTerm: [
20
23
  * 'Complete Q1 roadmap features',
21
24
  * 'Reduce bug backlog by 30%',
@@ -55,7 +58,7 @@ import type { WorkerGoals, KPI } from './types.js'
55
58
  *
56
59
  * @example
57
60
  * ```ts
58
- * const supportGoals = Goals({
61
+ * const supportGoals = defineGoals({
59
62
  * shortTerm: [
60
63
  * 'Achieve 95% customer satisfaction',
61
64
  * 'Reduce average response time to < 5 min',
@@ -78,7 +81,7 @@ import type { WorkerGoals, KPI } from './types.js'
78
81
  * })
79
82
  * ```
80
83
  */
81
- export function Goals(definition: WorkerGoals): WorkerGoals {
84
+ export function defineGoals(definition: WorkerGoals): WorkerGoals {
82
85
  return definition
83
86
  }
84
87
 
@@ -91,10 +94,10 @@ export function Goals(definition: WorkerGoals): WorkerGoals {
91
94
  *
92
95
  * @example
93
96
  * ```ts
94
- * const updated = Goals.addShortTerm(engineeringGoals, 'Complete security audit')
97
+ * const updated = defineGoals.addShortTerm(engineeringGoals, 'Complete security audit')
95
98
  * ```
96
99
  */
97
- Goals.addShortTerm = (goals: WorkerGoals, goal: string): WorkerGoals => ({
100
+ defineGoals.addShortTerm = (goals: WorkerGoals, goal: string): WorkerGoals => ({
98
101
  ...goals,
99
102
  shortTerm: [...goals.shortTerm, goal],
100
103
  })
@@ -108,10 +111,10 @@ Goals.addShortTerm = (goals: WorkerGoals, goal: string): WorkerGoals => ({
108
111
  *
109
112
  * @example
110
113
  * ```ts
111
- * const updated = Goals.addLongTerm(engineeringGoals, 'Build ML platform')
114
+ * const updated = defineGoals.addLongTerm(engineeringGoals, 'Build ML platform')
112
115
  * ```
113
116
  */
114
- Goals.addLongTerm = (goals: WorkerGoals, goal: string): WorkerGoals => ({
117
+ defineGoals.addLongTerm = (goals: WorkerGoals, goal: string): WorkerGoals => ({
115
118
  ...goals,
116
119
  longTerm: [...goals.longTerm, goal],
117
120
  })
@@ -125,10 +128,10 @@ Goals.addLongTerm = (goals: WorkerGoals, goal: string): WorkerGoals => ({
125
128
  *
126
129
  * @example
127
130
  * ```ts
128
- * const updated = Goals.addStrategic(engineeringGoals, 'Become carbon neutral')
131
+ * const updated = defineGoals.addStrategic(engineeringGoals, 'Become carbon neutral')
129
132
  * ```
130
133
  */
131
- Goals.addStrategic = (goals: WorkerGoals, goal: string): WorkerGoals => ({
134
+ defineGoals.addStrategic = (goals: WorkerGoals, goal: string): WorkerGoals => ({
132
135
  ...goals,
133
136
  strategic: [...(goals.strategic || []), goal],
134
137
  })
@@ -142,7 +145,7 @@ Goals.addStrategic = (goals: WorkerGoals, goal: string): WorkerGoals => ({
142
145
  *
143
146
  * @example
144
147
  * ```ts
145
- * const updated = Goals.addMetric(engineeringGoals, {
148
+ * const updated = defineGoals.addMetric(engineeringGoals, {
146
149
  * name: 'Code Quality',
147
150
  * description: 'Code quality score from SonarQube',
148
151
  * current: 85,
@@ -153,7 +156,7 @@ Goals.addStrategic = (goals: WorkerGoals, goal: string): WorkerGoals => ({
153
156
  * })
154
157
  * ```
155
158
  */
156
- Goals.addMetric = (goals: WorkerGoals, kpi: KPI): WorkerGoals => ({
159
+ defineGoals.addMetric = (goals: WorkerGoals, kpi: WorkerKPI): WorkerGoals => ({
157
160
  ...goals,
158
161
  metrics: [...(goals.metrics || []), kpi],
159
162
  })
@@ -168,22 +171,26 @@ Goals.addMetric = (goals: WorkerGoals, kpi: KPI): WorkerGoals => ({
168
171
  *
169
172
  * @example
170
173
  * ```ts
171
- * const updated = Goals.updateMetric(engineeringGoals, 'Deployment Frequency', {
174
+ * const updated = defineGoals.updateMetric(engineeringGoals, 'Deployment Frequency', {
172
175
  * current: 8,
173
176
  * trend: 'up',
174
177
  * })
175
178
  * ```
176
179
  */
177
- Goals.updateMetric = (
180
+ defineGoals.updateMetric = (
178
181
  goals: WorkerGoals,
179
182
  name: string,
180
- updates: Partial<Omit<KPI, 'name'>>
181
- ): WorkerGoals => ({
182
- ...goals,
183
- metrics: goals.metrics?.map((kpi) =>
183
+ updates: Partial<Omit<WorkerKPI, 'name'>>
184
+ ): WorkerGoals => {
185
+ const updatedMetrics = goals.metrics?.map((kpi) =>
184
186
  kpi.name === name ? { ...kpi, ...updates } : kpi
185
- ),
186
- })
187
+ )
188
+ const result: WorkerGoals = { ...goals }
189
+ if (updatedMetrics) {
190
+ result.metrics = updatedMetrics
191
+ }
192
+ return result
193
+ }
187
194
 
188
195
  /**
189
196
  * Get progress for a KPI (0-1)
@@ -194,12 +201,11 @@ Goals.updateMetric = (
194
201
  * @example
195
202
  * ```ts
196
203
  * const kpi = { current: 75, target: 100 }
197
- * const progress = Goals.progress(kpi) // 0.75
204
+ * const progress = defineGoals.progress(kpi) // 0.75
198
205
  * ```
199
206
  */
200
- Goals.progress = (kpi: Pick<KPI, 'current' | 'target'>): number => {
201
- if (kpi.target === 0) return 0
202
- return Math.min(1, Math.max(0, kpi.current / kpi.target))
207
+ defineGoals.progress = (kpi: Pick<WorkerKPI, 'current' | 'target'>): number => {
208
+ return calculateProgress(kpi)
203
209
  }
204
210
 
205
211
  /**
@@ -212,9 +218,12 @@ Goals.progress = (kpi: Pick<KPI, 'current' | 'target'>): number => {
212
218
  * @example
213
219
  * ```ts
214
220
  * const kpi = { current: 85, target: 100 }
215
- * const onTrack = Goals.onTrack(kpi) // true (85% >= 80% threshold)
221
+ * const onTrack = defineGoals.onTrack(kpi) // true (85% >= 80% threshold)
216
222
  * ```
217
223
  */
218
- Goals.onTrack = (kpi: Pick<KPI, 'current' | 'target'>, threshold = 0.8): boolean => {
219
- return Goals.progress(kpi) >= threshold
224
+ defineGoals.onTrack = (kpi: Pick<WorkerKPI, 'current' | 'target'>, threshold = 0.8): boolean => {
225
+ return isOnTrack(kpi, threshold)
220
226
  }
227
+
228
+ // Legacy alias for backward compatibility
229
+ export { defineGoals as Goals }