@dxos/shell 0.6.13-main.548ca8d → 0.6.13-main.ed424a1
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/dist/bundle/shell.mjs +4501 -4408
- package/dist/bundle/shell.mjs.map +1 -1
- package/dist/bundle/shell.umd.js +58 -58
- package/dist/bundle/shell.umd.js.map +1 -1
- package/dist/bundle/style.css +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/react.mjs +193 -95
- package/dist/lib/browser/react.mjs.map +4 -4
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.d.ts +1 -1
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.d.ts.map +1 -1
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.js +10 -108
- package/dist/types/src/panels/IdentityPanel/useAgentHandlers.js.map +1 -1
- package/dist/types/src/panels/IdentityPanel/useEdgeAgentsHandlers.d.ts +8 -0
- package/dist/types/src/panels/IdentityPanel/useEdgeAgentsHandlers.d.ts.map +1 -0
- package/dist/types/src/panels/IdentityPanel/useEdgeAgentsHandlers.js +84 -0
- package/dist/types/src/panels/IdentityPanel/useEdgeAgentsHandlers.js.map +1 -0
- package/dist/types/src/panels/IdentityPanel/useKubeAgentHandlers.d.ts +8 -0
- package/dist/types/src/panels/IdentityPanel/useKubeAgentHandlers.d.ts.map +1 -0
- package/dist/types/src/panels/IdentityPanel/useKubeAgentHandlers.js +119 -0
- package/dist/types/src/panels/IdentityPanel/useKubeAgentHandlers.js.map +1 -0
- package/package.json +24 -24
- package/src/panels/IdentityPanel/useAgentHandlers.ts +9 -114
- package/src/panels/IdentityPanel/useEdgeAgentsHandlers.ts +96 -0
- package/src/panels/IdentityPanel/useKubeAgentHandlers.ts +128 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useEffect, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type CancellableInvitation } from '@dxos/client-protocol';
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
9
|
+
import { QueryAgentStatusResponse } from '@dxos/protocols/proto/dxos/client/services';
|
|
10
|
+
import { useClient } from '@dxos/react-client';
|
|
11
|
+
import { type Identity } from '@dxos/react-client/halo';
|
|
12
|
+
|
|
13
|
+
import { type AgentFormProps } from '../../components';
|
|
14
|
+
|
|
15
|
+
export const useEdgeAgentHandlers = ({
|
|
16
|
+
identity,
|
|
17
|
+
}: {
|
|
18
|
+
invitations: CancellableInvitation[];
|
|
19
|
+
identity: Identity | null;
|
|
20
|
+
}): AgentFormProps => {
|
|
21
|
+
const [validationMessage, setValidationMessage] = useState('');
|
|
22
|
+
const [lastReceivedStatus, setLastReceivedStatus] = useState<QueryAgentStatusResponse.AgentStatus>(
|
|
23
|
+
QueryAgentStatusResponse.AgentStatus.UNKNOWN,
|
|
24
|
+
);
|
|
25
|
+
const [agentStatus, setAgentStatus] = useState<AgentFormProps['agentStatus']>('getting');
|
|
26
|
+
const client = useClient();
|
|
27
|
+
const agentHostingEnabled = Boolean(client.config.values.runtime?.client?.edgeFeatures?.agents);
|
|
28
|
+
|
|
29
|
+
const handleStatus = (status: QueryAgentStatusResponse.AgentStatus) => {
|
|
30
|
+
switch (status) {
|
|
31
|
+
case QueryAgentStatusResponse.AgentStatus.ACTIVE:
|
|
32
|
+
case QueryAgentStatusResponse.AgentStatus.INACTIVE:
|
|
33
|
+
setAgentStatus('created');
|
|
34
|
+
break;
|
|
35
|
+
case QueryAgentStatusResponse.AgentStatus.NOT_FOUND:
|
|
36
|
+
setAgentStatus('creatable');
|
|
37
|
+
break;
|
|
38
|
+
case QueryAgentStatusResponse.AgentStatus.UNKNOWN:
|
|
39
|
+
setAgentStatus('getting');
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const stream = client.services.services.EdgeAgentService?.queryAgentStatus();
|
|
46
|
+
stream?.subscribe(({ status }) => {
|
|
47
|
+
setLastReceivedStatus(status);
|
|
48
|
+
handleStatus(status);
|
|
49
|
+
});
|
|
50
|
+
return () => {
|
|
51
|
+
void stream?.close().catch(() => {});
|
|
52
|
+
};
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
const onAgentCreate = async () => {
|
|
56
|
+
invariant(identity, 'Identity not found');
|
|
57
|
+
const service = client.services.services.EdgeAgentService;
|
|
58
|
+
if (!service) {
|
|
59
|
+
setAgentStatus('error');
|
|
60
|
+
setValidationMessage('Agent service not initialized.');
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!agentHostingEnabled) {
|
|
64
|
+
setAgentStatus('error');
|
|
65
|
+
setValidationMessage('Edge agents are disabled in config.');
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
setAgentStatus('creating');
|
|
69
|
+
setValidationMessage('');
|
|
70
|
+
try {
|
|
71
|
+
await service.createAgent();
|
|
72
|
+
setAgentStatus('created');
|
|
73
|
+
} catch (err: any) {
|
|
74
|
+
setAgentStatus('error');
|
|
75
|
+
setValidationMessage(`Error creating an agent: ${err.message}`);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const onAgentDestroy = async () => {
|
|
80
|
+
setValidationMessage('Agents are non-destructible.');
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const onAgentRefresh = async () => {
|
|
84
|
+
setValidationMessage('');
|
|
85
|
+
handleStatus(lastReceivedStatus);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
onAgentCreate,
|
|
90
|
+
onAgentDestroy,
|
|
91
|
+
onAgentRefresh,
|
|
92
|
+
validationMessage,
|
|
93
|
+
agentStatus,
|
|
94
|
+
agentHostingEnabled,
|
|
95
|
+
};
|
|
96
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type CancellableInvitation } from '@dxos/client-protocol';
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
import { useClient, useAgentHostingClient } from '@dxos/react-client';
|
|
11
|
+
import { type Identity } from '@dxos/react-client/halo';
|
|
12
|
+
import { Invitation, InvitationEncoder } from '@dxos/react-client/invitations';
|
|
13
|
+
|
|
14
|
+
import { type AgentFormProps } from '../../components';
|
|
15
|
+
|
|
16
|
+
export const useKubeAgentHandlers = ({
|
|
17
|
+
invitations,
|
|
18
|
+
identity,
|
|
19
|
+
}: {
|
|
20
|
+
invitations: CancellableInvitation[];
|
|
21
|
+
identity: Identity | null;
|
|
22
|
+
}): AgentFormProps => {
|
|
23
|
+
const [validationMessage, setValidationMessage] = useState('');
|
|
24
|
+
const [agentStatus, setAgentStatus] = useState<AgentFormProps['agentStatus']>('getting');
|
|
25
|
+
const agentHostingProviderClient = useAgentHostingClient();
|
|
26
|
+
const client = useClient();
|
|
27
|
+
|
|
28
|
+
const updateAgentStatus = async () => {
|
|
29
|
+
// TODO(nf): if agent exists, subscribe to updates instead of oneshot status
|
|
30
|
+
if (agentHostingProviderClient) {
|
|
31
|
+
setAgentStatus('getting');
|
|
32
|
+
try {
|
|
33
|
+
const agentId = await agentHostingProviderClient?.getAgent(identity?.identityKey.truncate() ?? 'never');
|
|
34
|
+
if (agentId) {
|
|
35
|
+
setAgentStatus('created');
|
|
36
|
+
} else {
|
|
37
|
+
setAgentStatus('creatable');
|
|
38
|
+
}
|
|
39
|
+
} catch (err: any) {
|
|
40
|
+
setAgentStatus('error');
|
|
41
|
+
setValidationMessage(`Couldn’t check agent status: ${err.message}`);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
setAgentStatus('error');
|
|
45
|
+
setValidationMessage('No agent provider configured.');
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const createAgent = async (invitationCode: string) => {
|
|
50
|
+
invariant(identity, 'Identity not found');
|
|
51
|
+
if (agentHostingProviderClient) {
|
|
52
|
+
setAgentStatus('creating');
|
|
53
|
+
setValidationMessage('');
|
|
54
|
+
try {
|
|
55
|
+
const agentId = await agentHostingProviderClient?.createAgent(invitationCode, identity.identityKey.truncate());
|
|
56
|
+
if (agentId) {
|
|
57
|
+
setAgentStatus('created');
|
|
58
|
+
} else {
|
|
59
|
+
setAgentStatus('creatable');
|
|
60
|
+
setValidationMessage('Error creating agent.');
|
|
61
|
+
}
|
|
62
|
+
} catch (err: any) {
|
|
63
|
+
setValidationMessage(`Error creating agent: ${err.message}`);
|
|
64
|
+
return updateAgentStatus();
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
setAgentStatus('error');
|
|
68
|
+
setValidationMessage('No agent provider configured.');
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const destroyAgent = async () => {
|
|
73
|
+
invariant(identity, 'Identity not found');
|
|
74
|
+
if (agentHostingProviderClient) {
|
|
75
|
+
setAgentStatus('destroying');
|
|
76
|
+
setValidationMessage('');
|
|
77
|
+
try {
|
|
78
|
+
await agentHostingProviderClient?.destroyAgent(identity.identityKey.truncate());
|
|
79
|
+
setAgentStatus('creatable');
|
|
80
|
+
} catch (err: any) {
|
|
81
|
+
setValidationMessage(`Error destroying agent: ${err.message}`);
|
|
82
|
+
return updateAgentStatus();
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
setAgentStatus('error');
|
|
86
|
+
setValidationMessage('No agent provider configured.');
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
void updateAgentStatus();
|
|
92
|
+
}, []);
|
|
93
|
+
|
|
94
|
+
const handleAgentCreate = useCallback(async (invitation: Invitation) => {
|
|
95
|
+
const invitationCode = InvitationEncoder.encode(invitation);
|
|
96
|
+
if (invitation.state === Invitation.State.CONNECTING) {
|
|
97
|
+
log.info(JSON.stringify({ invitationCode, authCode: invitation.authCode }));
|
|
98
|
+
return createAgent(invitationCode);
|
|
99
|
+
}
|
|
100
|
+
}, []);
|
|
101
|
+
|
|
102
|
+
const onAgentCreate = async () => {
|
|
103
|
+
invitations.forEach((invitation) => invitation.cancel());
|
|
104
|
+
|
|
105
|
+
// TODO(nf): do this work in the hosting provider client?
|
|
106
|
+
const invitation = client.halo.share({
|
|
107
|
+
type: Invitation.Type.INTERACTIVE,
|
|
108
|
+
authMethod: Invitation.AuthMethod.NONE,
|
|
109
|
+
multiUse: true,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
invitation.subscribe(handleAgentCreate);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const onAgentRefresh = async () => {
|
|
116
|
+
setValidationMessage('');
|
|
117
|
+
await updateAgentStatus();
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
onAgentCreate,
|
|
122
|
+
onAgentDestroy: destroyAgent,
|
|
123
|
+
onAgentRefresh,
|
|
124
|
+
validationMessage,
|
|
125
|
+
agentStatus,
|
|
126
|
+
agentHostingEnabled: !!agentHostingProviderClient,
|
|
127
|
+
};
|
|
128
|
+
};
|