-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.ts
84 lines (67 loc) · 3.06 KB
/
weather.ts
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
import { engine, EngineTask, run } from '../src';
import { writeTrace } from './common/writeTrace';
@engine({ id: 'whetherEngine' })
class MyWeatherTask extends EngineTask {
@run()
async fetchCurrentTemperature(city: string) {
return Promise.resolve(`Current Temperature in ${city}: 25°C`);
}
@run()
async fetchDailyForecast(city: string) {
return Promise.resolve(`Daily Forecast in ${city}: Sunny`);
}
@run()
async recommendation(city: string): Promise<[string, void]> {
const vigilanceTask = new MyVigilanceTask();
return Promise.all([vigilanceTask.decideIfIShouldGoOut(city), vigilanceTask.decideIfIShouldGoOutNextYear(city)]);
}
}
@engine({ id: 'whetherEngine' })
class MyVigilanceTask extends EngineTask {
@run({
trace: { id: 'decideIfIShouldGoOut_custom_id', narratives: ['Narrative 0 GoOut'] },
config: { parallel: true, traceExecution: { inputs: true, outputs: true, narratives: ['Narrative 1 GoOut', 'Narrative 2 GoOut'] } }
})
async decideIfIShouldGoOut(city: string) {
const temperature = await new MyWeatherTask().fetchCurrentTemperature(city);
const forecast = await new MyWeatherTask().fetchDailyForecast(city);
const color = this.engine.run((t, f) => 'GREEN', [temperature, forecast], {
trace: { label: 'color' },
config: { parallel: true, traceExecution: true }
})?.outputs;
const decision = this.engine.run((t, f) => 'go out', [temperature, forecast], {
trace: { label: 'decide' },
config: { parallel: true, traceExecution: true }
})?.outputs;
return Promise.resolve(
`As daily Forecast in ${city} is ${forecast} and the temperature is ${temperature}, vigilance is ${color} and you can ${decision}`
);
}
@run({ config: { parallel: true, errors: 'catch', traceExecution: true } })
async decideIfIShouldGoOutNextYear(city: string) {
throw new Error(`Next year too far!, could not decide for ${city}`);
}
@run()
validateDecision(stringDecision: string) {
return stringDecision?.includes('GREEN');
}
}
@engine({ id: 'VigilanceValidationEngine' })
class MyIndependantVigilanceTask extends MyVigilanceTask {}
export async function generate() {
const myWeatherTaskInstance = new MyWeatherTask();
await myWeatherTaskInstance.fetchCurrentTemperature('Monastir');
await myWeatherTaskInstance.fetchDailyForecast('Monastir');
const response = await myWeatherTaskInstance.recommendation('Monastir');
//call validation:
const myVigilanceInstance = new MyVigilanceTask();
myVigilanceInstance.validateDecision(response[0]);
const myWeatherTaskInstanceTraceAfterValidation = myWeatherTaskInstance.engine.getTrace();
//call independent validation: as it is decorated with engineID: "VigilanceValidationEngine"
const myValidationTask = new MyIndependantVigilanceTask();
myValidationTask.validateDecision(response[0]);
const myValidationIndependentTrace = myValidationTask.engine.getTrace();
const jsonString = JSON.stringify(myWeatherTaskInstanceTraceAfterValidation.concat(myValidationIndependentTrace), null, 2);
writeTrace(jsonString);
}
generate().then();