@knowlearning/agents 0.9.111 → 0.9.113
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.
- package/agents/generic/message-queue.js +5 -1
- package/deno.js +65 -53
- package/package.json +1 -1
|
@@ -10,6 +10,10 @@ function standardJSONPatch(patch) {
|
|
|
10
10
|
})
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
function activePatch(patch) {
|
|
14
|
+
return structuredClone(patch).filter(({ path }) => 'active' === path.shift())
|
|
15
|
+
}
|
|
16
|
+
|
|
13
17
|
function sanitizeJSONPatchPathSegment(s) {
|
|
14
18
|
if (typeof s === "string") return s.replaceAll('~', '~0').replaceAll('/', '~1')
|
|
15
19
|
else return s
|
|
@@ -204,7 +208,7 @@ export default function messageQueue({ token, domain, Connection, watchers, stat
|
|
|
204
208
|
watchers[qualifiedScope]
|
|
205
209
|
.forEach(fn => {
|
|
206
210
|
const state = structuredClone(states[qualifiedScope].active)
|
|
207
|
-
fn({ ...message, state })
|
|
211
|
+
fn({ ...message, patch: activePatch(message.patch), state })
|
|
208
212
|
})
|
|
209
213
|
}
|
|
210
214
|
}
|
package/deno.js
CHANGED
|
@@ -5,75 +5,87 @@ const AGENT_TOKEN = Deno.env.get('AGENT_TOKEN')
|
|
|
5
5
|
|
|
6
6
|
const denoProcess = self
|
|
7
7
|
|
|
8
|
-
function Connection() {
|
|
9
|
-
const
|
|
8
|
+
function Connection(domain) {
|
|
9
|
+
const connection = crypto.randomUUID()
|
|
10
|
+
const c = this
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
c.send = message => denoProcess.postMessage({ ...message, domain, connection })
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
denoProcess.addEventListener('message', ({ data }) => {
|
|
15
|
+
if (data.connection === connection) c.onmessage(data)
|
|
16
|
+
})
|
|
16
17
|
|
|
17
|
-
// TODO: consider what onclose and onerror mean
|
|
18
|
-
|
|
18
|
+
// TODO: consider what onclose and onerror mean
|
|
19
|
+
setTimeout(() => c.onopen())
|
|
20
|
+
|
|
21
|
+
return c
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
const children = {}
|
|
22
25
|
const listeners = {}
|
|
23
26
|
|
|
24
|
-
const
|
|
25
|
-
Connection,
|
|
26
|
-
token: () => AGENT_TOKEN,
|
|
27
|
-
uuid: () => crypto.randomUUID(),
|
|
28
|
-
async log() {
|
|
29
|
-
await new Promise(r => r()) // so we can access our own agent instance
|
|
27
|
+
const agents = {}
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
function getAgent(domain, forceNew) {
|
|
30
|
+
if (agents[domain] && !forceNew) return agents[domain]
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
agents[domain] = new Agent({
|
|
33
|
+
Connection: Connection(domain),
|
|
34
|
+
token: () => AGENT_TOKEN,
|
|
35
|
+
uuid: () => crypto.randomUUID(),
|
|
36
|
+
async log() {
|
|
37
|
+
await new Promise(r => r()) // so we can access our own agent instance
|
|
38
|
+
|
|
39
|
+
const { session } = await agent.environment()
|
|
40
|
+
let value
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
value = structuredClone([...arguments])
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
value = `ERROR: error occurred logging arguments ${error} ${arguments}`
|
|
47
|
+
}
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
agent.interact('sessions', [{ op: 'add', path: ['active', session, 'log'], value }], false, false)
|
|
50
|
+
},
|
|
51
|
+
fetch,
|
|
52
|
+
applyPatch: fastJSONPatch.applyPatch,
|
|
53
|
+
reboot: () => Deno.exit(1),
|
|
54
|
+
handleDomainMessage: ({ type, session, data }, trigger) => {
|
|
55
|
+
// this functionality is only implemented for the deno
|
|
56
|
+
// agent for now, but this central management of children
|
|
57
|
+
// concept probably has a place in the generic agent
|
|
58
|
+
if (type === 'open') {
|
|
59
|
+
const child = {
|
|
60
|
+
environment: data,
|
|
61
|
+
on: (eventType, reaction) => {
|
|
62
|
+
if (!listeners[session]) throw new Error('Error attaching listener, child is closed')
|
|
63
|
+
if (!listeners[session][eventType]) throw new Error('Only "mutate" and "close" events can be listened to with Agent.on')
|
|
64
|
+
listeners[session][eventType].push(reaction)
|
|
65
|
+
}
|
|
57
66
|
}
|
|
67
|
+
listeners[session] = { mutate: [], close: [] }
|
|
68
|
+
children[session] = child
|
|
69
|
+
trigger('child', child)
|
|
70
|
+
}
|
|
71
|
+
else if (type === 'mutate') {
|
|
72
|
+
const filteredData = { ...data, patch: activePatch(data.patch) }
|
|
73
|
+
listeners[session].mutate.forEach(f => f(filteredData))
|
|
74
|
+
}
|
|
75
|
+
else if (type === 'close') {
|
|
76
|
+
listeners[session].close.forEach(f => f(data))
|
|
77
|
+
delete listeners[session]
|
|
78
|
+
delete children[session]
|
|
58
79
|
}
|
|
59
|
-
listeners[session] = { mutate: [], close: [] }
|
|
60
|
-
children[session] = child
|
|
61
|
-
trigger('child', child)
|
|
62
|
-
}
|
|
63
|
-
else if (type === 'mutate') {
|
|
64
|
-
const filteredData = { ...data, patch: activePatch(data.patch) }
|
|
65
|
-
listeners[session].mutate.forEach(f => f(filteredData))
|
|
66
|
-
}
|
|
67
|
-
else if (type === 'close') {
|
|
68
|
-
listeners[session].close.forEach(f => f(data))
|
|
69
|
-
delete listeners[session]
|
|
70
|
-
delete children[session]
|
|
71
80
|
}
|
|
72
|
-
}
|
|
73
|
-
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return agents[domain]
|
|
84
|
+
}
|
|
74
85
|
|
|
75
86
|
function activePatch(patch) {
|
|
76
87
|
return structuredClone(patch).filter(({ path }) => 'active' === path.shift())
|
|
77
88
|
}
|
|
78
89
|
|
|
79
|
-
export default
|
|
90
|
+
export default getAgent(null)
|
|
91
|
+
export { getAgent }
|