aipack 0.8.25

Command Agent runner to accelerate production coding with genai.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use crate::Result;
use crate::model::{EndState, Id, LogKind, ModelManager, RunBmc, RunForUpdate, RunStep, TaskBmc, TaskForUpdate};
use crate::runtime::{RtLog, Runtime};
use crate::support::time::now_micro;
use derive_more::From;

#[derive(Debug, From)]
pub struct RtStep<'a> {
	runtime: &'a Runtime,
}

/// Constructor & Core Getters
impl<'a> RtStep<'a> {
	pub(super) fn new(runtime: &'a Runtime) -> Self {
		Self { runtime }
	}

	fn mm(&self) -> &ModelManager {
		self.runtime.mm()
	}

	fn rt_log(&self) -> RtLog<'_> {
		RtLog::new(self.runtime)
	}
}

/// Run Steps
impl<'a> RtStep<'a> {
	pub async fn step_run_start(&self, run_id: Id) -> Result<Id> {
		// -- Update start time
		RunBmc::update(
			self.mm(),
			run_id,
			RunForUpdate {
				start: Some(now_micro().into()),
				..Default::default()
			},
		)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::Start), None, Some(LogKind::RunStep))
			.await?;

		Ok(run_id)
	}

	pub async fn step_ba_start(&self, run_id: Id) -> Result<()> {
		// -- Update Run State
		let run_u = RunForUpdate {
			ba_start: Some(now_micro().into()),
			..Default::default()
		};
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::BaStart), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	pub async fn step_ba_end(&self, run_id: Id) -> Result<()> {
		// -- Update Run State
		let run_u = RunForUpdate {
			ba_end: Some(now_micro().into()),
			..Default::default()
		};
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::BaEnd), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	/// Mark the start of Tasks execution.
	pub async fn step_tasks_start(&self, run_id: Id) -> Result<()> {
		// -- Update Run State
		let run_u = RunForUpdate {
			tasks_start: Some(now_micro().into()),
			..Default::default()
		};
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::TasksStart), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	/// Mark the end of Tasks execution.
	pub async fn step_tasks_end(&self, run_id: Id) -> Result<()> {
		// -- Update Run State
		let run_u = RunForUpdate {
			tasks_end: Some(now_micro().into()),
			..Default::default()
		};
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::TasksEnd), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	pub async fn step_task_start(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			start: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskStart),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_schedule(&self, run_id: Id, task_id: Id) -> Result<()> {
		self.step_task_start(run_id, task_id).await
	}

	pub async fn step_task_data_start(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			data_start: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskDataStart),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_data_end(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			data_end: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskDataEnd),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_ai_start(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			ai_start: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskAiStart),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_ai_gen_start(&self, run_id: Id, task_id: Id, prompt_size: i64) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			ai_gen_start: Some(now_micro().into()),
			prompt_size: Some(prompt_size),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskAiStart),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_ai_gen_end(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			ai_gen_end: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskAiStart),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_ai_end(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			ai_end: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskAiEnd),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_output_start(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			output_start: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskOutputStart),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	pub async fn step_task_output_end(&self, run_id: Id, task_id: Id) -> Result<()> {
		// -- Update Task State
		let task_u = TaskForUpdate {
			output_end: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(self.mm(), task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskOutputEnd),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	/// NOTE: Will update end_state if None
	pub async fn step_task_end_ok(&self, run_id: Id, task_id: Id) -> Result<()> {
		let mm = self.mm();

		let task = TaskBmc::get(mm, task_id)?;

		// Only update end state if none
		let end_state = if task.end_state.is_none() {
			Some(EndState::Ok)
		} else {
			None
		};

		// -- Update Task State
		let task_u = TaskForUpdate {
			end: Some(now_micro().into()),
			end_state,
			..Default::default()
		};
		TaskBmc::update(mm, task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskEnd),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	/// Note will update
	pub async fn step_task_end_err(&self, run_id: Id, task_id: Id, err: &crate::Error) -> Result<()> {
		let mm = self.mm();

		let task = TaskBmc::get(mm, task_id)?;

		// -- if we do not have a end_err_id, we set this one
		// Note: this will se the end_state as well
		if task.end_err_id.is_none() {
			TaskBmc::set_end_error_no_end(mm, task_id, None, err)?;
		}

		// -- Update Task State
		let task_u = TaskForUpdate {
			end: Some(now_micro().into()),
			..Default::default()
		};
		TaskBmc::update(mm, task_id, task_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(
				run_id,
				Some(task_id),
				Some(RunStep::TaskEnd),
				None,
				Some(LogKind::RunStep),
			)
			.await?;

		Ok(())
	}

	/// Mark the start of After All execution.
	pub async fn step_aa_start(&self, run_id: Id) -> Result<()> {
		// -- Update Run State
		let run_u = RunForUpdate {
			aa_start: Some(now_micro().into()),
			..Default::default()
		};
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::AaStart), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	/// Mark the end of After All execution.
	pub async fn step_aa_end(&self, run_id: Id) -> Result<()> {
		// -- Update Run State
		let run_u = RunForUpdate {
			aa_end: Some(now_micro().into()),
			..Default::default()
		};
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::AaEnd), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	/// Mark the run as completed (end time, end_state)
	pub async fn step_run_end_ok(&self, run_id: Id) -> Result<()> {
		let mm = self.mm();

		let run = RunBmc::get(mm, run_id)?;

		// Only update end state if none
		let end_state = if run.end_state.is_none() {
			Some(EndState::Ok)
		} else {
			None
		};

		// -- Update Run State
		let run_u = get_run_u_for_end(mm, run_id, end_state)?;
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::End), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	/// Mark the run as completed (end time, end_state)
	pub async fn step_run_end_canceled(&self, run_id: Id) -> Result<()> {
		let mm = self.mm();

		let run = RunBmc::get(mm, run_id)?;

		// Only update end state if none
		let end_state = if run.end_state.is_none() {
			Some(EndState::Cancel)
		} else {
			None
		};

		// -- Update Run State
		let run_u = get_run_u_for_end(mm, run_id, end_state)?;
		RunBmc::update(self.mm(), run_id, run_u)?;

		// -- Update the tasks that are not ended
		TaskBmc::cancel_all_not_ended_for_run(mm, run_id)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::End), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}

	/// Mark the run as ended but with error
	/// NOTE: if the err_id is already there, just do not create a new one.
	///       However, it not there, will create new one.
	pub async fn step_run_end_err(&self, run_id: Id, err: &crate::Error) -> Result<()> {
		let mm = self.mm();

		let run = RunBmc::get(mm, run_id)?;

		// -- if we do not have a end_err_id, we set this one
		// Note: this will se the end_state as well
		if run.end_err_id.is_none() {
			RunBmc::set_end_error(mm, run_id, None, err)?;
		}

		// -- Then set the end time
		// No need to set end_state was done by set_end_error
		let run_u = get_run_u_for_end(mm, run_id, None)?;

		RunBmc::update(mm, run_id, run_u)?;

		// -- Now update all the tasks of the run
		TaskBmc::cancel_all_not_ended_for_run(mm, run_id)?;

		// -- Add log line
		self.rt_log()
			.rec_log_no_msg(run_id, None, Some(RunStep::End), None, Some(LogKind::RunStep))
			.await?;

		Ok(())
	}
}

// region:    --- Support

fn get_run_u_for_end(mm: &ModelManager, run_id: Id, end_state: Option<EndState>) -> Result<RunForUpdate> {
	let tasks = TaskBmc::list_for_run(mm, run_id)?;

	let mut total_task_us = 0;
	for task in tasks {
		if let (Some(start), Some(end)) = (task.start, task.end) {
			let dur = end.as_i64() - start.as_i64();
			if dur > 0 {
				total_task_us += dur;
			}
		}
	}

	let total_task_ms: i64 = (total_task_us as f64 / 1000.).round() as i64;

	let run_u = RunForUpdate {
		end: Some(now_micro().into()),
		total_task_ms: Some(total_task_ms),
		end_state,
		..Default::default()
	};

	Ok(run_u)
}

// endregion: --- Support