@knowlearning/agents 0.9.112 → 0.9.114

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 (2) hide show
  1. package/deno.js +64 -51
  2. package/package.json +1 -1
package/deno.js CHANGED
@@ -5,75 +5,88 @@ const AGENT_TOKEN = Deno.env.get('AGENT_TOKEN')
5
5
 
6
6
  const denoProcess = self
7
7
 
8
- function Connection() {
8
+ function Connection(domain) {
9
+ const connection = crypto.randomUUID()
9
10
  const thisConnection = this
10
11
 
11
- thisConnection.send = message => denoProcess.postMessage(message)
12
+ thisConnection.send = message => denoProcess.postMessage({ ...message, domain, connection })
12
13
 
13
- const delay = new Promise(r => setTimeout(r))
14
- delay.then(() => thisConnection.onopen())
15
- denoProcess.onmessage = ({ data }) => thisConnection.onmessage(data)
14
+ denoProcess.addEventListener('message', ({ data }) => {
15
+ if (data.connection === connection) thisConnection.onmessage(data)
16
+ })
17
+
18
+ // TODO: consider what onclose and onerror mean
19
+ setTimeout(() => thisConnection.onopen())
16
20
 
17
- // TODO: consider what onclose and onerror mean in this case
18
21
  return thisConnection
19
22
  }
20
23
 
21
24
  const children = {}
22
25
  const listeners = {}
23
26
 
24
- const agent = new Agent({
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
- const { session } = await agent.environment()
32
- let value
29
+ function getAgent(domain, forceNew) {
30
+ if (agents[domain] && !forceNew) return agents[domain]
33
31
 
34
- try {
35
- value = structuredClone([...arguments])
36
- }
37
- catch (error) {
38
- value = `ERROR: error occurred logging arguments ${error} ${arguments}`
39
- }
32
+ agents[domain] = new Agent({
33
+ Connection: Connection(domain), // TODO: probably don't want to need to pass domain here, as first message from generic agent should pass it
34
+ domain,
35
+ token: () => AGENT_TOKEN,
36
+ uuid: () => crypto.randomUUID(),
37
+ async log() {
38
+ await new Promise(r => r()) // so we can access our own agent instance
39
+
40
+ const { session } = await agent.environment()
41
+ let value
42
+
43
+ try {
44
+ value = structuredClone([...arguments])
45
+ }
46
+ catch (error) {
47
+ value = `ERROR: error occurred logging arguments ${error} ${arguments}`
48
+ }
40
49
 
41
- agent.interact('sessions', [{ op: 'add', path: ['active', session, 'log'], value }], false, false)
42
- },
43
- fetch,
44
- applyPatch: fastJSONPatch.applyPatch,
45
- reboot: () => Deno.exit(1),
46
- handleDomainMessage: ({ type, session, data }, trigger) => {
47
- // this functionality is only implemented for the deno
48
- // agent for now, but this central management of children
49
- // concept probably has a place in the generic agent
50
- if (type === 'open') {
51
- const child = {
52
- environment: data,
53
- on: (eventType, reaction) => {
54
- if (!listeners[session]) throw new Error('Error attaching listener, child is closed')
55
- if (!listeners[session][eventType]) throw new Error('Only "mutate" and "close" events can be listened to with Agent.on')
56
- listeners[session][eventType].push(reaction)
50
+ agent.interact('sessions', [{ op: 'add', path: ['active', session, 'log'], value }], false, false)
51
+ },
52
+ fetch,
53
+ applyPatch: fastJSONPatch.applyPatch,
54
+ reboot: () => Deno.exit(1),
55
+ handleDomainMessage: ({ type, session, data }, trigger) => {
56
+ // this functionality is only implemented for the deno
57
+ // agent for now, but this central management of children
58
+ // concept probably has a place in the generic agent
59
+ if (type === 'open') {
60
+ const child = {
61
+ environment: data,
62
+ on: (eventType, reaction) => {
63
+ if (!listeners[session]) throw new Error('Error attaching listener, child is closed')
64
+ if (!listeners[session][eventType]) throw new Error('Only "mutate" and "close" events can be listened to with Agent.on')
65
+ listeners[session][eventType].push(reaction)
66
+ }
57
67
  }
68
+ listeners[session] = { mutate: [], close: [] }
69
+ children[session] = child
70
+ trigger('child', child)
71
+ }
72
+ else if (type === 'mutate') {
73
+ const filteredData = { ...data, patch: activePatch(data.patch) }
74
+ listeners[session].mutate.forEach(f => f(filteredData))
75
+ }
76
+ else if (type === 'close') {
77
+ listeners[session].close.forEach(f => f(data))
78
+ delete listeners[session]
79
+ delete children[session]
58
80
  }
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
81
  }
72
- }
73
- })
82
+ })
83
+
84
+ return agents[domain]
85
+ }
74
86
 
75
87
  function activePatch(patch) {
76
88
  return structuredClone(patch).filter(({ path }) => 'active' === path.shift())
77
89
  }
78
90
 
79
- export default agent
91
+ export default getAgent(null)
92
+ export { getAgent }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.9.112",
3
+ "version": "0.9.114",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",