digital-workers 2.0.2 → 2.1.3

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 (69) hide show
  1. package/.turbo/turbo-build.log +4 -5
  2. package/CHANGELOG.md +31 -0
  3. package/LICENSE +21 -0
  4. package/README.md +134 -180
  5. package/dist/actions.d.ts.map +1 -1
  6. package/dist/actions.js +1 -0
  7. package/dist/actions.js.map +1 -1
  8. package/dist/agent-comms.d.ts +438 -0
  9. package/dist/agent-comms.d.ts.map +1 -0
  10. package/dist/agent-comms.js +666 -0
  11. package/dist/agent-comms.js.map +1 -0
  12. package/dist/capability-tiers.d.ts +230 -0
  13. package/dist/capability-tiers.d.ts.map +1 -0
  14. package/dist/capability-tiers.js +388 -0
  15. package/dist/capability-tiers.js.map +1 -0
  16. package/dist/cascade-context.d.ts +523 -0
  17. package/dist/cascade-context.d.ts.map +1 -0
  18. package/dist/cascade-context.js +494 -0
  19. package/dist/cascade-context.js.map +1 -0
  20. package/dist/error-escalation.d.ts +416 -0
  21. package/dist/error-escalation.d.ts.map +1 -0
  22. package/dist/error-escalation.js +656 -0
  23. package/dist/error-escalation.js.map +1 -0
  24. package/dist/index.d.ts +10 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +34 -0
  27. package/dist/index.js.map +1 -1
  28. package/dist/load-balancing.d.ts +395 -0
  29. package/dist/load-balancing.d.ts.map +1 -0
  30. package/dist/load-balancing.js +905 -0
  31. package/dist/load-balancing.js.map +1 -0
  32. package/dist/types.d.ts +8 -0
  33. package/dist/types.d.ts.map +1 -1
  34. package/dist/types.js +1 -0
  35. package/dist/types.js.map +1 -1
  36. package/package.json +14 -14
  37. package/src/actions.js +436 -0
  38. package/src/actions.ts +9 -8
  39. package/src/agent-comms.ts +1238 -0
  40. package/src/approve.js +234 -0
  41. package/src/ask.js +226 -0
  42. package/src/capability-tiers.ts +545 -0
  43. package/src/cascade-context.ts +648 -0
  44. package/src/decide.js +244 -0
  45. package/src/do.js +227 -0
  46. package/src/error-escalation.ts +1135 -0
  47. package/src/generate.js +298 -0
  48. package/src/goals.js +205 -0
  49. package/src/index.js +68 -0
  50. package/src/index.ts +223 -0
  51. package/src/is.js +317 -0
  52. package/src/kpis.js +270 -0
  53. package/src/load-balancing.ts +1381 -0
  54. package/src/notify.js +219 -0
  55. package/src/role.js +110 -0
  56. package/src/team.js +130 -0
  57. package/src/transports.js +357 -0
  58. package/src/types.js +71 -0
  59. package/src/types.ts +8 -0
  60. package/test/actions.test.js +401 -0
  61. package/test/agent-comms.test.ts +1397 -0
  62. package/test/capability-tiers.test.ts +631 -0
  63. package/test/cascade-context.test.ts +692 -0
  64. package/test/error-escalation.test.ts +1205 -0
  65. package/test/load-balancing-thread-safety.test.ts +464 -0
  66. package/test/load-balancing.test.ts +1145 -0
  67. package/test/standalone.test.js +250 -0
  68. package/test/types.test.js +371 -0
  69. package/test/types.test.ts +35 -0
package/src/kpis.js ADDED
@@ -0,0 +1,270 @@
1
+ /**
2
+ * KPI and OKR tracking functionality for digital workers
3
+ */
4
+ export function kpis(definition) {
5
+ return definition;
6
+ }
7
+ /**
8
+ * Update a KPI's current value
9
+ *
10
+ * @param kpi - The KPI to update
11
+ * @param current - New current value
12
+ * @returns Updated KPI
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const updated = kpis.update(deploymentFrequency, 8)
17
+ * console.log(updated.current) // 8
18
+ * console.log(updated.trend) // 'up' (automatically determined)
19
+ * ```
20
+ */
21
+ kpis.update = (kpi, current) => {
22
+ // Determine trend
23
+ let trend = 'stable';
24
+ if (current > kpi.current) {
25
+ trend = 'up';
26
+ }
27
+ else if (current < kpi.current) {
28
+ trend = 'down';
29
+ }
30
+ return {
31
+ ...kpi,
32
+ current,
33
+ trend,
34
+ };
35
+ };
36
+ /**
37
+ * Calculate progress towards target (0-1)
38
+ *
39
+ * @param kpi - The KPI
40
+ * @returns Progress as a decimal (0-1)
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const kpi = { current: 75, target: 100 }
45
+ * const progress = kpis.progress(kpi) // 0.75
46
+ * ```
47
+ */
48
+ kpis.progress = (kpi) => {
49
+ if (kpi.target === 0)
50
+ return 0;
51
+ return Math.min(1, Math.max(0, kpi.current / kpi.target));
52
+ };
53
+ /**
54
+ * Check if a KPI is on track
55
+ *
56
+ * @param kpi - The KPI
57
+ * @param threshold - Minimum progress to be "on track" (default: 0.8)
58
+ * @returns Whether the KPI is on track
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const kpi = { current: 85, target: 100 }
63
+ * const onTrack = kpis.onTrack(kpi) // true (85% >= 80%)
64
+ * ```
65
+ */
66
+ kpis.onTrack = (kpi, threshold = 0.8) => {
67
+ return kpis.progress(kpi) >= threshold;
68
+ };
69
+ /**
70
+ * Get the gap to target
71
+ *
72
+ * @param kpi - The KPI
73
+ * @returns Difference between target and current
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const kpi = { current: 75, target: 100 }
78
+ * const gap = kpis.gap(kpi) // 25
79
+ * ```
80
+ */
81
+ kpis.gap = (kpi) => {
82
+ return kpi.target - kpi.current;
83
+ };
84
+ /**
85
+ * Format a KPI for display
86
+ *
87
+ * @param kpi - The KPI
88
+ * @returns Formatted string
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const kpi = {
93
+ * name: 'Deployment Frequency',
94
+ * current: 5,
95
+ * target: 10,
96
+ * unit: 'deploys/week',
97
+ * trend: 'up',
98
+ * }
99
+ * const formatted = kpis.format(kpi)
100
+ * // "Deployment Frequency: 5/10 deploys/week (50%, trending up)"
101
+ * ```
102
+ */
103
+ kpis.format = (kpi) => {
104
+ const progress = kpis.progress(kpi);
105
+ const progressPercent = Math.round(progress * 100);
106
+ const trendEmoji = kpi.trend === 'up' ? '↑' : kpi.trend === 'down' ? '↓' : '→';
107
+ return `${kpi.name}: ${kpi.current}/${kpi.target} ${kpi.unit} (${progressPercent}%, trending ${kpi.trend} ${trendEmoji})`;
108
+ };
109
+ /**
110
+ * Compare two KPI snapshots to see change over time
111
+ *
112
+ * @param previous - Previous KPI state
113
+ * @param current - Current KPI state
114
+ * @returns Change analysis
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const change = kpis.compare(previousKPI, currentKPI)
119
+ * console.log(change.delta) // 5
120
+ * console.log(change.percentChange) // 10
121
+ * console.log(change.improved) // true
122
+ * ```
123
+ */
124
+ kpis.compare = (previous, current) => {
125
+ const delta = current.current - previous.current;
126
+ const percentChange = previous.current !== 0
127
+ ? (delta / previous.current) * 100
128
+ : 0;
129
+ // Improved if we got closer to the target
130
+ const previousGap = Math.abs(previous.target - previous.current);
131
+ const currentGap = Math.abs(current.target - current.current);
132
+ const improved = currentGap < previousGap;
133
+ return {
134
+ delta,
135
+ percentChange,
136
+ improved,
137
+ };
138
+ };
139
+ /**
140
+ * Define OKRs (Objectives and Key Results)
141
+ *
142
+ * @param definition - OKR definition
143
+ * @returns The defined OKR
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * const engineeringOKR = okrs({
148
+ * objective: 'Improve development velocity',
149
+ * keyResults: [
150
+ * {
151
+ * name: 'Deployment Frequency',
152
+ * current: 5,
153
+ * target: 10,
154
+ * unit: 'deploys/week',
155
+ * },
156
+ * {
157
+ * name: 'Lead Time',
158
+ * current: 48,
159
+ * target: 24,
160
+ * unit: 'hours',
161
+ * },
162
+ * {
163
+ * name: 'Change Failure Rate',
164
+ * current: 15,
165
+ * target: 5,
166
+ * unit: '%',
167
+ * },
168
+ * ],
169
+ * owner: 'engineering-team',
170
+ * dueDate: new Date('2024-03-31'),
171
+ * })
172
+ * ```
173
+ */
174
+ export function okrs(definition) {
175
+ return definition;
176
+ }
177
+ /**
178
+ * Calculate overall OKR progress
179
+ *
180
+ * @param okr - The OKR
181
+ * @returns Average progress across all key results (0-1)
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * const progress = okrs.progress(engineeringOKR)
186
+ * console.log(progress) // 0.67 (67% complete)
187
+ * ```
188
+ */
189
+ okrs.progress = (okr) => {
190
+ if (okr.keyResults.length === 0)
191
+ return 0;
192
+ const totalProgress = okr.keyResults.reduce((sum, kr) => {
193
+ return sum + kpis.progress(kr);
194
+ }, 0);
195
+ return totalProgress / okr.keyResults.length;
196
+ };
197
+ /**
198
+ * Update a key result in an OKR
199
+ *
200
+ * @param okr - The OKR
201
+ * @param keyResultName - Name of key result to update
202
+ * @param current - New current value
203
+ * @returns Updated OKR
204
+ *
205
+ * @example
206
+ * ```ts
207
+ * const updated = okrs.updateKeyResult(
208
+ * engineeringOKR,
209
+ * 'Deployment Frequency',
210
+ * 8
211
+ * )
212
+ * ```
213
+ */
214
+ okrs.updateKeyResult = (okr, keyResultName, current) => {
215
+ return {
216
+ ...okr,
217
+ keyResults: okr.keyResults.map((kr) => kr.name === keyResultName ? { ...kr, current } : kr),
218
+ progress: undefined, // Will be recalculated
219
+ };
220
+ };
221
+ /**
222
+ * Check if OKR is on track
223
+ *
224
+ * @param okr - The OKR
225
+ * @param threshold - Minimum progress to be "on track" (default: 0.7)
226
+ * @returns Whether the OKR is on track
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * const onTrack = okrs.onTrack(engineeringOKR)
231
+ * ```
232
+ */
233
+ okrs.onTrack = (okr, threshold = 0.7) => {
234
+ return okrs.progress(okr) >= threshold;
235
+ };
236
+ /**
237
+ * Format OKR for display
238
+ *
239
+ * @param okr - The OKR
240
+ * @returns Formatted string
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * const formatted = okrs.format(engineeringOKR)
245
+ * console.log(formatted)
246
+ * // Improve development velocity (67% complete)
247
+ * // • Deployment Frequency: 5/10 deploys/week (50%)
248
+ * // • Lead Time: 48/24 hours (200%)
249
+ * // • Change Failure Rate: 15/5 % (300%)
250
+ * ```
251
+ */
252
+ okrs.format = (okr) => {
253
+ const progress = okrs.progress(okr);
254
+ const progressPercent = Math.round(progress * 100);
255
+ const lines = [
256
+ `${okr.objective} (${progressPercent}% complete)`,
257
+ ...okr.keyResults.map((kr) => {
258
+ const krProgress = kpis.progress(kr);
259
+ const krPercent = Math.round(krProgress * 100);
260
+ return ` • ${kr.name}: ${kr.current}/${kr.target} ${kr.unit} (${krPercent}%)`;
261
+ }),
262
+ ];
263
+ if (okr.owner) {
264
+ lines.push(` Owner: ${okr.owner}`);
265
+ }
266
+ if (okr.dueDate) {
267
+ lines.push(` Due: ${okr.dueDate.toLocaleDateString()}`);
268
+ }
269
+ return lines.join('\n');
270
+ };