autonomous-agents 0.1.0 → 2.0.1

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 (51) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +9 -0
  3. package/README.md +260 -96
  4. package/dist/actions.d.ts +136 -0
  5. package/dist/actions.d.ts.map +1 -0
  6. package/dist/actions.js +303 -0
  7. package/dist/actions.js.map +1 -0
  8. package/dist/agent.d.ts +49 -0
  9. package/dist/agent.d.ts.map +1 -0
  10. package/dist/agent.js +452 -0
  11. package/dist/agent.js.map +1 -0
  12. package/dist/goals.d.ts +138 -0
  13. package/dist/goals.d.ts.map +1 -0
  14. package/dist/goals.js +342 -0
  15. package/dist/goals.js.map +1 -0
  16. package/dist/index.d.ts +55 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +60 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/metrics.d.ts +245 -0
  21. package/dist/metrics.d.ts.map +1 -0
  22. package/dist/metrics.js +436 -0
  23. package/dist/metrics.js.map +1 -0
  24. package/dist/role.d.ts +122 -0
  25. package/dist/role.d.ts.map +1 -0
  26. package/dist/role.js +393 -0
  27. package/dist/role.js.map +1 -0
  28. package/dist/team.d.ts +152 -0
  29. package/dist/team.d.ts.map +1 -0
  30. package/dist/team.js +347 -0
  31. package/dist/team.js.map +1 -0
  32. package/dist/types.d.ts +327 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/types.js +8 -0
  35. package/dist/types.js.map +1 -0
  36. package/package.json +27 -36
  37. package/src/actions.ts +366 -0
  38. package/src/agent.ts +548 -0
  39. package/src/goals.ts +435 -0
  40. package/src/index.ts +135 -0
  41. package/src/metrics.ts +591 -0
  42. package/src/role.ts +422 -0
  43. package/src/team.ts +466 -0
  44. package/src/types.ts +356 -0
  45. package/test/actions.test.ts +522 -0
  46. package/test/agent.test.ts +490 -0
  47. package/test/goals.test.ts +570 -0
  48. package/test/metrics.test.ts +707 -0
  49. package/test/role.test.ts +423 -0
  50. package/test/team.test.ts +708 -0
  51. package/tsconfig.json +9 -0
package/src/goals.ts ADDED
@@ -0,0 +1,435 @@
1
+ /**
2
+ * Goals - Define and track goals for agents and teams
3
+ *
4
+ * Goals provide direction and measurable outcomes for autonomous agents
5
+ * and teams to work towards.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+
10
+ import type { Goal, GoalsConfig, Priority } from './types.js'
11
+
12
+ /**
13
+ * Create a goals configuration
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { Goals } from 'autonomous-agents'
18
+ *
19
+ * const goals = Goals({
20
+ * goals: [
21
+ * {
22
+ * id: 'revenue-q1',
23
+ * description: 'Increase Q1 revenue by 20%',
24
+ * target: 120000,
25
+ * progress: 45000,
26
+ * priority: 'high',
27
+ * deadline: new Date('2024-03-31'),
28
+ * status: 'active',
29
+ * },
30
+ * {
31
+ * id: 'feature-launch',
32
+ * description: 'Launch new AI features',
33
+ * target: '100%',
34
+ * progress: '60%',
35
+ * priority: 'high',
36
+ * deadline: new Date('2024-02-15'),
37
+ * status: 'active',
38
+ * subgoals: [
39
+ * {
40
+ * id: 'feature-design',
41
+ * description: 'Complete feature designs',
42
+ * target: '100%',
43
+ * progress: '100%',
44
+ * status: 'completed',
45
+ * },
46
+ * {
47
+ * id: 'feature-dev',
48
+ * description: 'Implement features',
49
+ * target: '100%',
50
+ * progress: '80%',
51
+ * status: 'active',
52
+ * },
53
+ * ],
54
+ * },
55
+ * ],
56
+ * strategy: 'Focus on high-impact features that drive revenue growth',
57
+ * timeHorizon: 'Q1 2024',
58
+ * })
59
+ * ```
60
+ */
61
+ export function Goals(config: GoalsConfig): GoalsInstance {
62
+ const state = {
63
+ goals: config.goals,
64
+ strategy: config.strategy,
65
+ timeHorizon: config.timeHorizon,
66
+ }
67
+
68
+ return {
69
+ ...state,
70
+ addGoal,
71
+ removeGoal,
72
+ getGoal,
73
+ getGoals,
74
+ getActiveGoals,
75
+ getCompletedGoals,
76
+ getBlockedGoals,
77
+ getGoalsByPriority,
78
+ updateGoal,
79
+ updateProgress,
80
+ markCompleted,
81
+ markBlocked,
82
+ getProgress,
83
+ getOverallProgress,
84
+ }
85
+
86
+ /**
87
+ * Add a new goal
88
+ */
89
+ function addGoal(goal: Goal): void {
90
+ state.goals.push(goal)
91
+ }
92
+
93
+ /**
94
+ * Remove a goal
95
+ */
96
+ function removeGoal(goalId: string): boolean {
97
+ const index = state.goals.findIndex(g => g.id === goalId)
98
+ if (index === -1) return false
99
+ state.goals.splice(index, 1)
100
+ return true
101
+ }
102
+
103
+ /**
104
+ * Get a specific goal
105
+ */
106
+ function getGoal(goalId: string): Goal | undefined {
107
+ return findGoalRecursive(state.goals, goalId)
108
+ }
109
+
110
+ /**
111
+ * Get all goals
112
+ */
113
+ function getGoals(): Goal[] {
114
+ return [...state.goals]
115
+ }
116
+
117
+ /**
118
+ * Get active goals
119
+ */
120
+ function getActiveGoals(): Goal[] {
121
+ return state.goals.filter(g => g.status === 'active')
122
+ }
123
+
124
+ /**
125
+ * Get completed goals
126
+ */
127
+ function getCompletedGoals(): Goal[] {
128
+ return state.goals.filter(g => g.status === 'completed')
129
+ }
130
+
131
+ /**
132
+ * Get blocked goals
133
+ */
134
+ function getBlockedGoals(): Goal[] {
135
+ return state.goals.filter(g => g.status === 'blocked')
136
+ }
137
+
138
+ /**
139
+ * Get goals by priority
140
+ */
141
+ function getGoalsByPriority(priority: Priority): Goal[] {
142
+ return state.goals.filter(g => g.priority === priority)
143
+ }
144
+
145
+ /**
146
+ * Update a goal
147
+ */
148
+ function updateGoal(goalId: string, updates: Partial<Omit<Goal, 'id'>>): void {
149
+ const goal = findGoalRecursive(state.goals, goalId)
150
+ if (!goal) {
151
+ throw new Error(`Goal with id ${goalId} not found`)
152
+ }
153
+ Object.assign(goal, updates)
154
+ }
155
+
156
+ /**
157
+ * Update goal progress
158
+ */
159
+ function updateProgress(goalId: string, progress: string | number): void {
160
+ const goal = findGoalRecursive(state.goals, goalId)
161
+ if (!goal) {
162
+ throw new Error(`Goal with id ${goalId} not found`)
163
+ }
164
+ goal.progress = progress
165
+
166
+ // Auto-complete if progress reaches target
167
+ if (isGoalComplete(goal)) {
168
+ goal.status = 'completed'
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Mark goal as completed
174
+ */
175
+ function markCompleted(goalId: string): void {
176
+ const goal = findGoalRecursive(state.goals, goalId)
177
+ if (!goal) {
178
+ throw new Error(`Goal with id ${goalId} not found`)
179
+ }
180
+ goal.status = 'completed'
181
+ goal.progress = goal.target
182
+ }
183
+
184
+ /**
185
+ * Mark goal as blocked
186
+ */
187
+ function markBlocked(goalId: string, reason?: string): void {
188
+ const goal = findGoalRecursive(state.goals, goalId)
189
+ if (!goal) {
190
+ throw new Error(`Goal with id ${goalId} not found`)
191
+ }
192
+ goal.status = 'blocked'
193
+ }
194
+
195
+ /**
196
+ * Get progress for a specific goal
197
+ */
198
+ function getProgress(goalId: string): number {
199
+ const goal = findGoalRecursive(state.goals, goalId)
200
+ if (!goal) return 0
201
+ return calculateProgress(goal)
202
+ }
203
+
204
+ /**
205
+ * Get overall progress across all goals
206
+ */
207
+ function getOverallProgress(): number {
208
+ if (state.goals.length === 0) return 0
209
+
210
+ const totalProgress = state.goals.reduce(
211
+ (sum, goal) => sum + calculateProgress(goal),
212
+ 0
213
+ )
214
+ return totalProgress / state.goals.length
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Goals instance with methods
220
+ */
221
+ export interface GoalsInstance extends GoalsConfig {
222
+ /** Add a new goal */
223
+ addGoal(goal: Goal): void
224
+ /** Remove a goal */
225
+ removeGoal(goalId: string): boolean
226
+ /** Get a specific goal */
227
+ getGoal(goalId: string): Goal | undefined
228
+ /** Get all goals */
229
+ getGoals(): Goal[]
230
+ /** Get active goals */
231
+ getActiveGoals(): Goal[]
232
+ /** Get completed goals */
233
+ getCompletedGoals(): Goal[]
234
+ /** Get blocked goals */
235
+ getBlockedGoals(): Goal[]
236
+ /** Get goals by priority */
237
+ getGoalsByPriority(priority: Priority): Goal[]
238
+ /** Update a goal */
239
+ updateGoal(goalId: string, updates: Partial<Omit<Goal, 'id'>>): void
240
+ /** Update goal progress */
241
+ updateProgress(goalId: string, progress: string | number): void
242
+ /** Mark goal as completed */
243
+ markCompleted(goalId: string): void
244
+ /** Mark goal as blocked */
245
+ markBlocked(goalId: string, reason?: string): void
246
+ /** Get progress for a specific goal */
247
+ getProgress(goalId: string): number
248
+ /** Get overall progress across all goals */
249
+ getOverallProgress(): number
250
+ }
251
+
252
+ /**
253
+ * Find a goal recursively (including subgoals)
254
+ */
255
+ function findGoalRecursive(goals: Goal[], goalId: string): Goal | undefined {
256
+ for (const goal of goals) {
257
+ if (goal.id === goalId) return goal
258
+ if (goal.subgoals) {
259
+ const found = findGoalRecursive(goal.subgoals, goalId)
260
+ if (found) return found
261
+ }
262
+ }
263
+ return undefined
264
+ }
265
+
266
+ /**
267
+ * Calculate progress percentage for a goal
268
+ */
269
+ function calculateProgress(goal: Goal): number {
270
+ // If no progress set, return 0
271
+ if (goal.progress === undefined) return 0
272
+
273
+ // If goal is completed, return 100
274
+ if (goal.status === 'completed') return 100
275
+
276
+ // Handle numeric progress
277
+ if (typeof goal.progress === 'number' && typeof goal.target === 'number') {
278
+ return Math.min(100, (goal.progress / goal.target) * 100)
279
+ }
280
+
281
+ // Handle percentage strings
282
+ if (typeof goal.progress === 'string' && typeof goal.target === 'string') {
283
+ const progressMatch = goal.progress.match(/(\d+)%?/)
284
+ const targetMatch = goal.target.match(/(\d+)%?/)
285
+
286
+ if (progressMatch && targetMatch) {
287
+ const progressNum = parseInt(progressMatch[1]!, 10)
288
+ const targetNum = parseInt(targetMatch[1]!, 10)
289
+ return Math.min(100, (progressNum / targetNum) * 100)
290
+ }
291
+ }
292
+
293
+ // If we have subgoals, calculate from them
294
+ if (goal.subgoals && goal.subgoals.length > 0) {
295
+ const subgoalProgress = goal.subgoals.reduce(
296
+ (sum, subgoal) => sum + calculateProgress(subgoal),
297
+ 0
298
+ )
299
+ return subgoalProgress / goal.subgoals.length
300
+ }
301
+
302
+ return 0
303
+ }
304
+
305
+ /**
306
+ * Check if a goal is complete
307
+ */
308
+ function isGoalComplete(goal: Goal): boolean {
309
+ // Check status
310
+ if (goal.status === 'completed') return true
311
+
312
+ // Check numeric progress vs target
313
+ if (typeof goal.progress === 'number' && typeof goal.target === 'number') {
314
+ return goal.progress >= goal.target
315
+ }
316
+
317
+ // Check percentage strings
318
+ if (typeof goal.progress === 'string' && typeof goal.target === 'string') {
319
+ const progressMatch = goal.progress.match(/(\d+)%?/)
320
+ const targetMatch = goal.target.match(/(\d+)%?/)
321
+
322
+ if (progressMatch && targetMatch) {
323
+ return parseInt(progressMatch[1]!, 10) >= parseInt(targetMatch[1]!, 10)
324
+ }
325
+ }
326
+
327
+ // Check subgoals
328
+ if (goal.subgoals && goal.subgoals.length > 0) {
329
+ return goal.subgoals.every(subgoal => isGoalComplete(subgoal))
330
+ }
331
+
332
+ return false
333
+ }
334
+
335
+ /**
336
+ * Create a simple goal
337
+ */
338
+ export function createGoal(config: {
339
+ id: string
340
+ description: string
341
+ target: string | number
342
+ priority?: Priority
343
+ deadline?: Date
344
+ }): Goal {
345
+ return {
346
+ id: config.id,
347
+ description: config.description,
348
+ target: config.target,
349
+ priority: config.priority || 'medium',
350
+ deadline: config.deadline,
351
+ status: 'active',
352
+ }
353
+ }
354
+
355
+ /**
356
+ * Create a goal with subgoals
357
+ */
358
+ export function createGoalWithSubgoals(config: {
359
+ id: string
360
+ description: string
361
+ target: string | number
362
+ subgoals: Goal[]
363
+ priority?: Priority
364
+ deadline?: Date
365
+ }): Goal {
366
+ return {
367
+ id: config.id,
368
+ description: config.description,
369
+ target: config.target,
370
+ subgoals: config.subgoals,
371
+ priority: config.priority || 'medium',
372
+ deadline: config.deadline,
373
+ status: 'active',
374
+ }
375
+ }
376
+
377
+ /**
378
+ * Check if a goal is overdue
379
+ */
380
+ export function isGoalOverdue(goal: Goal): boolean {
381
+ if (!goal.deadline) return false
382
+ return new Date() > goal.deadline && goal.status !== 'completed'
383
+ }
384
+
385
+ /**
386
+ * Get goals that are overdue
387
+ */
388
+ export function getOverdueGoals(goals: Goal[]): Goal[] {
389
+ return goals.filter(isGoalOverdue)
390
+ }
391
+
392
+ /**
393
+ * Get goals due soon (within specified days)
394
+ */
395
+ export function getGoalsDueSoon(goals: Goal[], days: number = 7): Goal[] {
396
+ const now = new Date()
397
+ const threshold = new Date(now.getTime() + days * 24 * 60 * 60 * 1000)
398
+
399
+ return goals.filter(goal => {
400
+ if (!goal.deadline || goal.status === 'completed') return false
401
+ return goal.deadline <= threshold && goal.deadline > now
402
+ })
403
+ }
404
+
405
+ /**
406
+ * Get goals by status
407
+ */
408
+ export function getGoalsByStatus(
409
+ goals: Goal[],
410
+ status: 'active' | 'completed' | 'blocked' | 'cancelled'
411
+ ): Goal[] {
412
+ return goals.filter(g => g.status === status)
413
+ }
414
+
415
+ /**
416
+ * Calculate time remaining until deadline
417
+ */
418
+ export function getTimeRemaining(goal: Goal): {
419
+ days: number
420
+ hours: number
421
+ minutes: number
422
+ } | null {
423
+ if (!goal.deadline) return null
424
+
425
+ const now = new Date()
426
+ const diff = goal.deadline.getTime() - now.getTime()
427
+
428
+ if (diff < 0) return { days: 0, hours: 0, minutes: 0 }
429
+
430
+ const days = Math.floor(diff / (1000 * 60 * 60 * 24))
431
+ const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
432
+ const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
433
+
434
+ return { days, hours, minutes }
435
+ }
package/src/index.ts ADDED
@@ -0,0 +1,135 @@
1
+ /**
2
+ * autonomous-agents - Primitives for building and orchestrating autonomous AI agents
3
+ *
4
+ * This package provides primitives for creating autonomous AI agents that can:
5
+ * - Execute tasks and make decisions
6
+ * - Work in teams with other agents and humans
7
+ * - Track goals and metrics (KPIs, OKRs)
8
+ * - Request approvals and human oversight
9
+ * - Operate within defined roles and responsibilities
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { Agent, Role, Team, Goals } from 'autonomous-agents'
14
+ *
15
+ * // Create a role
16
+ * const productManager = Role({
17
+ * name: 'Product Manager',
18
+ * description: 'Manages product strategy and roadmap',
19
+ * skills: ['product strategy', 'user research', 'roadmap planning'],
20
+ * })
21
+ *
22
+ * // Create an agent
23
+ * const agent = Agent({
24
+ * name: 'ProductAgent',
25
+ * role: productManager,
26
+ * mode: 'autonomous',
27
+ * goals: [
28
+ * { id: 'g1', description: 'Define Q1 roadmap', target: '100%' }
29
+ * ],
30
+ * })
31
+ *
32
+ * // Execute tasks
33
+ * const result = await agent.do('Create product brief for feature X')
34
+ *
35
+ * // Make decisions
36
+ * const choice = await agent.decide(['A', 'B', 'C'], 'Which feature to prioritize?')
37
+ *
38
+ * // Request approval
39
+ * const approval = await agent.approve({
40
+ * title: 'Budget Request',
41
+ * description: 'Request $50k for research',
42
+ * data: { amount: 50000 },
43
+ * })
44
+ * ```
45
+ *
46
+ * @packageDocumentation
47
+ */
48
+
49
+ // Core types
50
+ export type {
51
+ Agent as AgentType,
52
+ AgentConfig,
53
+ AgentMode,
54
+ AgentStatus,
55
+ AgentHistoryEntry,
56
+ Role as RoleType,
57
+ Team as TeamType,
58
+ TeamMember,
59
+ Goal,
60
+ GoalsConfig,
61
+ KPI,
62
+ OKR,
63
+ KeyResult,
64
+ Priority,
65
+ ApprovalRequest,
66
+ ApprovalResult,
67
+ ApprovalStatus,
68
+ NotificationOptions,
69
+ CommunicationChannel,
70
+ } from './types.js'
71
+
72
+ // Agent creation and management
73
+ export { Agent } from './agent.js'
74
+
75
+ // Role definitions
76
+ export {
77
+ Role,
78
+ Roles,
79
+ hasPermission,
80
+ hasSkill,
81
+ getPermissions,
82
+ getSkills,
83
+ mergeRoles,
84
+ } from './role.js'
85
+
86
+ // Team collaboration
87
+ export {
88
+ Team,
89
+ type TeamInstance,
90
+ createTeamMember,
91
+ teamMemberFromAgent,
92
+ calculateTeamCapacity,
93
+ getTeamSkills,
94
+ teamHasSkill,
95
+ findBestMemberForTask,
96
+ } from './team.js'
97
+
98
+ // Goals and objectives
99
+ export {
100
+ Goals,
101
+ type GoalsInstance,
102
+ createGoal,
103
+ createGoalWithSubgoals,
104
+ isGoalOverdue,
105
+ getOverdueGoals,
106
+ getGoalsDueSoon,
107
+ getGoalsByStatus,
108
+ getTimeRemaining,
109
+ } from './goals.js'
110
+
111
+ // Action primitives
112
+ export {
113
+ do,
114
+ doAction,
115
+ ask,
116
+ decide,
117
+ approve,
118
+ generate,
119
+ is,
120
+ notify,
121
+ } from './actions.js'
122
+
123
+ // Metrics and performance tracking
124
+ export {
125
+ kpi,
126
+ kpis,
127
+ okr,
128
+ okrs,
129
+ type KPIInstance,
130
+ type KPIsCollection,
131
+ type OKRInstance,
132
+ type OKRsCollection,
133
+ createKeyResult,
134
+ updateKeyResultStatus,
135
+ } from './metrics.js'