-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.ts
More file actions
53 lines (41 loc) · 1.74 KB
/
bridge.ts
File metadata and controls
53 lines (41 loc) · 1.74 KB
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
import { askAgentForFix } from "./agent"
const SOURCE_URL = 'http://localhost:3001/data'
const DEST_URL = 'http://localhost:3002/ingest'
// this is the in-memory store we start with "Guessed" mapping, if this is incorrect ai will update this.
let mappingStore: Record<string, string> = {
fullName: "userid", //initial guess
emailAdd: "contact" // intitial guess
}
async function runBridge(){
try{
const sourceResponse = await fetch(SOURCE_URL) // normally its get req to make it a post -> add 2nd arg
const sourceData: any = await sourceResponse.json()
console.log("Received from source", sourceData)
const payload: any = {}
for(const [legacyKey , modernKey] of Object.entries(mappingStore)){
payload[modernKey] = sourceData[legacyKey]
}
console.log("transformed to payload", payload)
const destResponse = await fetch(DEST_URL, { //sending the post req
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload), // body has to be a string
})
const result = await destResponse.json()
if(!destResponse.ok){
console.log('error message:', result)
console.log('calling the ai agent')
//throw new Error(`Destination api rejected data: ${JSON.stringify(result)}` ) //Error message must be string o/w it will show [object Object]
const newMapping = await askAgentForFix(sourceData, result)
console.log("new mapping suggested by AI : ", newMapping)
mappingStore = newMapping
console.log('retrying with newMapping')
return runBridge()
}
console.log('Success', result)
}
catch(e){
console.error("something went wrong with the runBridge",e)
}
}
runBridge();