@antcde/connect-ts 0.4.26 → 0.4.27-rc.10
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/README.md +67 -3
- package/build/AntConnect.d.ts +8 -0
- package/build/AntConnect.d.ts.map +1 -1
- package/build/AntConnect.js +8 -0
- package/build/AntHttpClient.d.ts +0 -5
- package/build/AntHttpClient.d.ts.map +1 -1
- package/build/AntHttpClient.js +0 -5
- package/build/ai/index.d.ts +5 -0
- package/build/ai/index.d.ts.map +1 -0
- package/build/ai/index.js +3 -0
- package/build/ai/mutationDescriptors.d.ts +3 -0
- package/build/ai/mutationDescriptors.d.ts.map +1 -0
- package/build/ai/mutationDescriptors.js +207 -0
- package/build/ai/mutationTargets.d.ts +21 -0
- package/build/ai/mutationTargets.d.ts.map +1 -0
- package/build/ai/mutationTargets.js +72 -0
- package/build/ai/queryDescriptors.d.ts +3 -0
- package/build/ai/queryDescriptors.d.ts.map +1 -0
- package/build/ai/queryDescriptors.js +174 -0
- package/build/ai/types.d.ts +35 -0
- package/build/ai/types.d.ts.map +1 -0
- package/build/comms/client.d.ts.map +1 -1
- package/build/comms/client.js +39 -41
- package/build/comms/connection.d.ts +32 -0
- package/build/comms/connection.d.ts.map +1 -0
- package/build/comms/connection.js +159 -0
- package/build/comms/handshake.d.ts +16 -0
- package/build/comms/handshake.d.ts.map +1 -0
- package/build/comms/handshake.js +73 -0
- package/build/comms/host.d.ts.map +1 -1
- package/build/comms/host.js +22 -26
- package/build/comms/hostAPI.js +1 -1
- package/build/comms/hostSlot.d.ts +23 -0
- package/build/comms/hostSlot.d.ts.map +1 -0
- package/build/comms/hostSlot.js +55 -0
- package/build/comms/models.d.ts +31 -2
- package/build/comms/models.d.ts.map +1 -1
- package/build/comms/signalChangeTypes.d.ts +27 -1
- package/build/comms/signalChangeTypes.d.ts.map +1 -1
- package/build/comms/signalChangeTypes.js +7 -0
- package/build/comms/signalTypes.d.ts +21 -3
- package/build/comms/signalTypes.d.ts.map +1 -1
- package/build/comms/transport.d.ts +18 -2
- package/build/comms/transport.d.ts.map +1 -1
- package/build/comms/transport.js +25 -10
- package/build/index.d.ts +8 -35
- package/build/index.d.ts.map +1 -1
- package/build/index.js +8 -3
- package/build/service-runtime.d.ts +9 -0
- package/build/service-runtime.d.ts.map +1 -0
- package/build/service-runtime.js +10 -0
- package/build/service-types-app.d.ts +2 -0
- package/build/service-types-app.d.ts.map +1 -0
- package/build/service-types-app.js +3 -0
- package/build/service-types.d.ts +37 -0
- package/build/service-types.d.ts.map +1 -0
- package/build/service-types.js +7 -0
- package/build/services/AiService.d.ts +18 -0
- package/build/services/AiService.d.ts.map +1 -0
- package/build/services/AiService.js +9 -0
- package/build/services/AppService/factory/accessRequests.d.ts +19 -0
- package/build/services/AppService/factory/accessRequests.d.ts.map +1 -0
- package/build/services/AppService/factory/accessRequests.js +17 -0
- package/build/services/AppService/factory/assignments.d.ts +57 -0
- package/build/services/AppService/factory/assignments.d.ts.map +1 -0
- package/build/services/AppService/factory/assignments.js +30 -0
- package/build/services/AppService/factory/categories.d.ts +30 -0
- package/build/services/AppService/factory/categories.d.ts.map +1 -0
- package/build/services/AppService/factory/categories.js +11 -0
- package/build/services/AppService/factory/channels.d.ts +35 -0
- package/build/services/AppService/factory/channels.d.ts.map +1 -0
- package/build/services/AppService/factory/channels.js +18 -0
- package/build/services/AppService/factory/formData.d.ts +14 -0
- package/build/services/AppService/factory/formData.d.ts.map +1 -0
- package/build/services/AppService/factory/formData.js +50 -0
- package/build/services/AppService/factory/media.d.ts +17 -0
- package/build/services/AppService/factory/media.d.ts.map +1 -0
- package/build/services/AppService/factory/media.js +10 -0
- package/build/services/AppService/factory/resourceAccess.d.ts +21 -0
- package/build/services/AppService/factory/resourceAccess.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceAccess.js +49 -0
- package/build/services/AppService/factory/resourceAssignments.d.ts +66 -0
- package/build/services/AppService/factory/resourceAssignments.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceAssignments.js +12 -0
- package/build/services/AppService/factory/resourceChannels.d.ts +89 -0
- package/build/services/AppService/factory/resourceChannels.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceChannels.js +19 -0
- package/build/services/AppService/factory/resourceCommerce.d.ts +52 -0
- package/build/services/AppService/factory/resourceCommerce.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceCommerce.js +18 -0
- package/build/services/AppService/factory/resourceSnapshot.d.ts +75 -0
- package/build/services/AppService/factory/resourceSnapshot.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceSnapshot.js +55 -0
- package/build/services/AppService/factory/resourceUploads.d.ts +37 -0
- package/build/services/AppService/factory/resourceUploads.d.ts.map +1 -0
- package/build/services/AppService/factory/resourceUploads.js +8 -0
- package/build/services/AppService/factory/resources.d.ts +167 -0
- package/build/services/AppService/factory/resources.d.ts.map +1 -0
- package/build/services/AppService/factory/resources.js +45 -0
- package/build/services/AppService/factory/subscriptions.d.ts +73 -0
- package/build/services/AppService/factory/subscriptions.d.ts.map +1 -0
- package/build/services/AppService/factory/subscriptions.js +39 -0
- package/build/services/AppService/factory/taxonomy.d.ts +25 -0
- package/build/services/AppService/factory/taxonomy.d.ts.map +1 -0
- package/build/services/AppService/factory/taxonomy.js +5 -0
- package/build/services/AppService/index.d.ts +58 -0
- package/build/services/AppService/index.d.ts.map +1 -0
- package/build/services/AppService/index.js +74 -0
- package/build/services/AppService/types/accessRequests.d.ts +164 -0
- package/build/services/AppService/types/accessRequests.d.ts.map +1 -0
- package/build/services/AppService/types/app.d.ts +124 -0
- package/build/services/AppService/types/app.d.ts.map +1 -0
- package/build/services/AppService/types/app.js +6 -0
- package/build/services/AppService/types/appCategories.d.ts +44 -0
- package/build/services/AppService/types/appCategories.d.ts.map +1 -0
- package/build/services/AppService/types/appCategories.js +1 -0
- package/build/services/AppService/types/appChangelog.d.ts +31 -0
- package/build/services/AppService/types/appChangelog.d.ts.map +1 -0
- package/build/services/AppService/types/appChangelog.js +1 -0
- package/build/services/AppService/types/appConfigStatus.d.ts +68 -0
- package/build/services/AppService/types/appConfigStatus.d.ts.map +1 -0
- package/build/services/AppService/types/appConfigStatus.js +11 -0
- package/build/services/AppService/types/appList.d.ts +211 -0
- package/build/services/AppService/types/appList.d.ts.map +1 -0
- package/build/services/AppService/types/appList.js +1 -0
- package/build/services/AppService/types/appManifest.d.ts +100 -0
- package/build/services/AppService/types/appManifest.d.ts.map +1 -0
- package/build/services/AppService/types/appManifest.js +4 -0
- package/build/services/AppService/types/appMedia.d.ts +136 -0
- package/build/services/AppService/types/appMedia.d.ts.map +1 -0
- package/build/services/AppService/types/appMedia.js +1 -0
- package/build/services/AppService/types/appRequests.d.ts +126 -0
- package/build/services/AppService/types/appRequests.d.ts.map +1 -0
- package/build/services/AppService/types/appRequests.js +1 -0
- package/build/services/AppService/types/appScopes.d.ts +165 -0
- package/build/services/AppService/types/appScopes.d.ts.map +1 -0
- package/build/services/AppService/types/appScopes.js +1 -0
- package/build/services/AppService/types/channels.d.ts +45 -0
- package/build/services/AppService/types/channels.d.ts.map +1 -0
- package/build/services/AppService/types/channels.js +1 -0
- package/build/services/AppService/types/pricingTiers.d.ts +111 -0
- package/build/services/AppService/types/pricingTiers.d.ts.map +1 -0
- package/build/services/AppService/types/pricingTiers.js +1 -0
- package/build/services/AppService/types/publisherStats.d.ts +20 -0
- package/build/services/AppService/types/publisherStats.d.ts.map +1 -0
- package/build/services/AppService/types/publisherStats.js +1 -0
- package/build/services/AppService/types/releaseChannels.d.ts +127 -0
- package/build/services/AppService/types/releaseChannels.d.ts.map +1 -0
- package/build/services/AppService/types/releaseChannels.js +1 -0
- package/build/services/AppService/types/resourceAssignments.d.ts +38 -0
- package/build/services/AppService/types/resourceAssignments.d.ts.map +1 -0
- package/build/services/AppService/types/resourceAssignments.js +10 -0
- package/build/services/AppService/types/resourceEntitlements.d.ts +118 -0
- package/build/services/AppService/types/resourceEntitlements.d.ts.map +1 -0
- package/build/services/AppService/types/resourceEntitlements.js +1 -0
- package/build/services/AppService/types/resourceReleases.d.ts +230 -0
- package/build/services/AppService/types/resourceReleases.d.ts.map +1 -0
- package/build/services/AppService/types/resourceReleases.js +1 -0
- package/build/services/AppService/types/resourceRequests.d.ts +115 -0
- package/build/services/AppService/types/resourceRequests.d.ts.map +1 -0
- package/build/services/AppService/types/resourceRequests.js +1 -0
- package/build/services/AppService/types/seats.d.ts +220 -0
- package/build/services/AppService/types/seats.d.ts.map +1 -0
- package/build/services/AppService/types/seats.js +4 -0
- package/build/services/AppService/types/sellableResources.d.ts +231 -0
- package/build/services/AppService/types/sellableResources.d.ts.map +1 -0
- package/build/services/AppService/types/sellableResources.js +1 -0
- package/build/services/AppService/types/snapshotFileSource.d.ts +110 -0
- package/build/services/AppService/types/snapshotFileSource.d.ts.map +1 -0
- package/build/services/AppService/types/snapshotFileSource.js +8 -0
- package/build/services/AppService/types/snapshotPayload.d.ts +166 -0
- package/build/services/AppService/types/snapshotPayload.d.ts.map +1 -0
- package/build/services/AppService/types/snapshotPayload.js +19 -0
- package/build/services/AppService/types/snapshotSources.d.ts +116 -0
- package/build/services/AppService/types/snapshotSources.d.ts.map +1 -0
- package/build/services/AppService/types/snapshotSources.js +7 -0
- package/build/services/AppService/types/snapshotUploads.d.ts +109 -0
- package/build/services/AppService/types/snapshotUploads.d.ts.map +1 -0
- package/build/services/AppService/types/snapshotUploads.js +20 -0
- package/build/services/AppService/types/subscriptions.d.ts +182 -0
- package/build/services/AppService/types/subscriptions.d.ts.map +1 -0
- package/build/services/AppService/types/subscriptions.js +1 -0
- package/build/services/AppService/types/taxonomy.d.ts +76 -0
- package/build/services/AppService/types/taxonomy.d.ts.map +1 -0
- package/build/services/AppService/types/taxonomy.js +1 -0
- package/build/services/AppService/types.d.ts +27 -0
- package/build/services/AppService/types.d.ts.map +1 -0
- package/build/services/AppService/types.js +29 -0
- package/build/services/DmsService.d.ts +6 -151
- package/build/services/DmsService.d.ts.map +1 -1
- package/build/services/DmsService.types.d.ts +169 -0
- package/build/services/DmsService.types.d.ts.map +1 -0
- package/build/services/DmsService.types.js +1 -0
- package/build/services/DmsServiceImpl.d.ts.map +1 -1
- package/build/services/DmsServiceImpl.js +8 -0
- package/build/services/LicenseService.d.ts +1 -1
- package/build/services/LicenseService.d.ts.map +1 -1
- package/build/services/LicenseService.js +1 -1
- package/build/services/OAuthClientService.d.ts +20 -0
- package/build/services/OAuthClientService.d.ts.map +1 -0
- package/build/services/OAuthClientService.js +12 -0
- package/build/services/OAuthClientService.types.d.ts +37 -0
- package/build/services/OAuthClientService.types.d.ts.map +1 -0
- package/build/services/OAuthClientService.types.js +4 -0
- package/build/services/ProjectService.d.ts +21 -0
- package/build/services/ProjectService.d.ts.map +1 -1
- package/build/services/ProjectService.js +1 -0
- package/build/services/ReleaseService.d.ts +1 -0
- package/build/services/ReleaseService.d.ts.map +1 -1
- package/build/services/SecretService.d.ts +22 -0
- package/build/services/SecretService.d.ts.map +1 -0
- package/build/services/SecretService.js +21 -0
- package/build/services/SecretService.types.d.ts +147 -0
- package/build/services/SecretService.types.d.ts.map +1 -0
- package/build/services/SecretService.types.js +1 -0
- package/build/services/SecretSharing.types.d.ts +112 -0
- package/build/services/SecretSharing.types.d.ts.map +1 -0
- package/build/services/SecretSharing.types.js +1 -0
- package/build/services/TableService.js +7 -7
- package/build/services/TaskService.d.ts +3 -3
- package/build/services/TaskService.d.ts.map +1 -1
- package/build/services/TaskService.js +1 -1
- package/build/services/TaskService.requests.d.ts +61 -0
- package/build/services/TaskService.requests.d.ts.map +1 -0
- package/build/services/TaskService.requests.js +1 -0
- package/build/services/TaskService.types.d.ts +21 -53
- package/build/services/TaskService.types.d.ts.map +1 -1
- package/build/services/TaskServiceQueries.d.ts +2 -0
- package/build/services/TaskServiceQueries.d.ts.map +1 -1
- package/build/services/TaskServiceQueries.js +13 -2
- package/build/services/TaskTemplate.types.d.ts +74 -1
- package/build/services/TaskTemplate.types.d.ts.map +1 -1
- package/build/services/TokenService.d.ts +5 -1
- package/build/services/TokenService.d.ts.map +1 -1
- package/build/services/TokenService.js +1 -2
- package/build/services/UserService.d.ts +3 -0
- package/build/services/UserService.d.ts.map +1 -1
- package/build/services/UserService.js +4 -4
- package/build/services/Webhook/EventService.d.ts +7 -0
- package/build/services/Webhook/EventService.d.ts.map +1 -1
- package/build/services/Webhook/TriggerHistoryService.d.ts +1 -1
- package/build/services/Webhook/TriggerHistoryService.d.ts.map +1 -1
- package/build/services/Webhook/TriggerSecretService.d.ts +88 -0
- package/build/services/Webhook/TriggerSecretService.d.ts.map +1 -0
- package/build/services/Webhook/TriggerSecretService.js +9 -0
- package/build/services/Webhook/TriggerService.d.ts +108 -0
- package/build/services/Webhook/TriggerService.d.ts.map +1 -1
- package/build/services/Webhook/TriggerService.js +25 -0
- package/build/services/Websockets/StoreEvents.d.ts +44 -0
- package/build/services/Websockets/StoreEvents.d.ts.map +1 -0
- package/build/services/Websockets/StoreEvents.js +5 -0
- package/build/services/userPreferences.d.ts +47 -0
- package/build/services/userPreferences.d.ts.map +1 -0
- package/build/services/userPreferences.js +42 -0
- package/package.json +8 -7
- package/schemas/app-config.schema.json +85 -8
- package/build/services/AppManifest.d.ts +0 -39
- package/build/services/AppManifest.d.ts.map +0 -1
- package/build/services/AppService.d.ts +0 -57
- package/build/services/AppService.d.ts.map +0 -1
- package/build/services/AppService.js +0 -47
- package/build/services/AppService.types.d.ts +0 -173
- package/build/services/AppService.types.d.ts.map +0 -1
- /package/build/{services/AppManifest.js → ai/types.js} +0 -0
- /package/build/services/{AppService.types.js → AppService/types/accessRequests.js} +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import qs from 'qs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
// Case-insensitive substring filter over a list, on a chosen text field.
|
|
4
|
+
function bySearch(rows, search, field) {
|
|
5
|
+
const s = String(search ?? '').toLowerCase();
|
|
6
|
+
return s ? rows.filter(r => field(r).toLowerCase().includes(s)) : rows;
|
|
7
|
+
}
|
|
8
|
+
// The read-only query tools. Each fetches through the client and returns the raw
|
|
9
|
+
// domain rows (Project[] / ListApp[] / Task[]) — the consumer normalises them.
|
|
10
|
+
// Row types are inferred from the client method signatures, so no domain-type
|
|
11
|
+
// imports are needed here.
|
|
12
|
+
export const queryDescriptors = [
|
|
13
|
+
{
|
|
14
|
+
name: 'query_projects',
|
|
15
|
+
kind: 'project',
|
|
16
|
+
description: 'List the projects in the current license. Use when the user asks to see or find projects.',
|
|
17
|
+
inputSchema: z.object({ search: z.string().optional().describe('Filter projects by name') }),
|
|
18
|
+
requiredInputs: [],
|
|
19
|
+
run: async (ctx, input) => {
|
|
20
|
+
const rows = await ctx.client.projects.getProjects(ctx.licenseId);
|
|
21
|
+
return bySearch(rows, input.search, p => p.name);
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: 'query_apps',
|
|
26
|
+
kind: 'app',
|
|
27
|
+
description: 'List the apps available in the current license. Use when the user asks which apps they have.',
|
|
28
|
+
inputSchema: z.object({ search: z.string().optional().describe('Filter apps by title') }),
|
|
29
|
+
requiredInputs: [],
|
|
30
|
+
run: async (ctx, input) => {
|
|
31
|
+
const response = await ctx.client.apps.fetchApps({ config: ['capabilities', 'signals'] });
|
|
32
|
+
return bySearch(response?.data ?? [], input.search, a => a.title);
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'query_tasks',
|
|
37
|
+
kind: 'task',
|
|
38
|
+
description: 'List the tasks in a project. The project id is optional — if omitted the caller supplies one.',
|
|
39
|
+
inputSchema: z.object({
|
|
40
|
+
project: z.string().optional().describe('Id of the project whose tasks to list'),
|
|
41
|
+
search: z.string().optional().describe('Filter tasks by title'),
|
|
42
|
+
}),
|
|
43
|
+
requiredInputs: ['project'],
|
|
44
|
+
run: async (ctx, input) => {
|
|
45
|
+
// ant-os resolves a project name → id before calling; fall back to the active project.
|
|
46
|
+
const projectId = (typeof input.project === 'string' && input.project) || ctx.projectId;
|
|
47
|
+
if (!projectId)
|
|
48
|
+
return [];
|
|
49
|
+
const query = qs.stringify({
|
|
50
|
+
filters: {
|
|
51
|
+
project: { $eq: projectId },
|
|
52
|
+
is_template: { $eq: false },
|
|
53
|
+
parent: { $eq: null },
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
const response = await ctx.client.tasks.getV2Tasks(ctx.licenseId, query);
|
|
57
|
+
return bySearch(response?.data ?? [], input.search, t => t.title);
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'query_documents',
|
|
62
|
+
kind: 'document',
|
|
63
|
+
description: 'List or search the documents and folders (DMS) of a project. Use when the user asks to find a document, file, drawing or folder.',
|
|
64
|
+
inputSchema: z.object({
|
|
65
|
+
project: z.string().optional().describe('Id of the project whose documents to list'),
|
|
66
|
+
search: z.string().optional().describe('Search documents by name'),
|
|
67
|
+
}),
|
|
68
|
+
requiredInputs: ['project'],
|
|
69
|
+
run: async (ctx, input) => {
|
|
70
|
+
const projectId = (typeof input.project === 'string' && input.project) || ctx.projectId;
|
|
71
|
+
if (!projectId)
|
|
72
|
+
return [];
|
|
73
|
+
const search = String(input.search ?? '').trim();
|
|
74
|
+
const response = search
|
|
75
|
+
? await ctx.client.dms.searchFiles(projectId, { q: search })
|
|
76
|
+
: await ctx.client.dms.getProjectFiles(projectId);
|
|
77
|
+
return response?.data ?? [];
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: 'query_sbs',
|
|
82
|
+
kind: 'sbs',
|
|
83
|
+
description: 'List or search the structure breakdown (SBS) nodes of a project. Use when the user asks about the SBS, breakdown structure or a structure code.',
|
|
84
|
+
inputSchema: z.object({
|
|
85
|
+
project: z.string().optional().describe('Id of the project whose SBS to list'),
|
|
86
|
+
search: z.string().optional().describe('Search SBS nodes by code or label'),
|
|
87
|
+
}),
|
|
88
|
+
requiredInputs: ['project'],
|
|
89
|
+
run: async (ctx, input) => {
|
|
90
|
+
const projectId = (typeof input.project === 'string' && input.project) || ctx.projectId;
|
|
91
|
+
if (!projectId)
|
|
92
|
+
return [];
|
|
93
|
+
const search = String(input.search ?? '').trim();
|
|
94
|
+
return search
|
|
95
|
+
? await ctx.client.sbs.searchSbs(projectId, search)
|
|
96
|
+
: await ctx.client.sbs.allSbs(projectId);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'query_tables',
|
|
101
|
+
kind: 'table',
|
|
102
|
+
description: 'List the dynamic tables of a project. Use when the user asks which tables exist.',
|
|
103
|
+
inputSchema: z.object({
|
|
104
|
+
project: z.string().optional().describe('Id of the project whose tables to list'),
|
|
105
|
+
search: z.string().optional().describe('Filter tables by name'),
|
|
106
|
+
}),
|
|
107
|
+
requiredInputs: ['project'],
|
|
108
|
+
run: async (ctx, input) => {
|
|
109
|
+
const projectId = (typeof input.project === 'string' && input.project) || ctx.projectId;
|
|
110
|
+
if (!projectId)
|
|
111
|
+
return [];
|
|
112
|
+
const rows = await ctx.client.tables.getTables(projectId);
|
|
113
|
+
return bySearch(rows, input.search, t => t.name);
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'query_records',
|
|
118
|
+
kind: 'record',
|
|
119
|
+
description: 'Show the records of a dynamic table in a project. Use when the user asks to see the contents/rows of a named table.',
|
|
120
|
+
inputSchema: z.object({
|
|
121
|
+
project: z.string().optional().describe('Id of the project the table belongs to'),
|
|
122
|
+
table: z.string().optional().describe('Id or name of the table whose records to show'),
|
|
123
|
+
search: z.string().optional().describe('Filter records by any cell value'),
|
|
124
|
+
}),
|
|
125
|
+
requiredInputs: ['project', 'table'],
|
|
126
|
+
run: async (ctx, input) => {
|
|
127
|
+
const projectId = (typeof input.project === 'string' && input.project) || ctx.projectId;
|
|
128
|
+
const term = String(input.table ?? '').trim();
|
|
129
|
+
if (!projectId || !term)
|
|
130
|
+
return [];
|
|
131
|
+
// The prompt may name the table; the form may pick an id — accept both.
|
|
132
|
+
const tables = await ctx.client.tables.getTables(projectId);
|
|
133
|
+
const table = tables.find(t => t.id === term)
|
|
134
|
+
?? tables.find(t => t.name.toLowerCase().includes(term.toLowerCase()));
|
|
135
|
+
if (!table)
|
|
136
|
+
return [];
|
|
137
|
+
const result = await ctx.client.tables.queryTables({
|
|
138
|
+
tables: [{ id: table.id, columns: ['*'], limit: 50, project: projectId }],
|
|
139
|
+
});
|
|
140
|
+
// The response is keyed by table NAME (or `as` alias), not id.
|
|
141
|
+
const records = (result?.[table.name] ?? Object.values(result ?? {})[0])?.records ?? [];
|
|
142
|
+
return bySearch(records, input.search, r => Object.values(r).join(' '));
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'query_licenses',
|
|
147
|
+
kind: 'license',
|
|
148
|
+
description: 'List the licenses the user belongs to. Use when the user asks which licenses or organizations they have access to.',
|
|
149
|
+
inputSchema: z.object({ search: z.string().optional().describe('Filter licenses by name') }),
|
|
150
|
+
requiredInputs: [],
|
|
151
|
+
run: async (ctx, input) => {
|
|
152
|
+
const rows = await ctx.client.licenses.getLicenses();
|
|
153
|
+
return bySearch(rows, input.search, l => l.name);
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: 'query_members',
|
|
158
|
+
kind: 'member',
|
|
159
|
+
description: 'List the members (users) of a project. Use when the user asks who is on or works in a project.',
|
|
160
|
+
inputSchema: z.object({
|
|
161
|
+
project: z.string().optional().describe('Id of the project whose members to list'),
|
|
162
|
+
search: z.string().optional().describe('Search members by name or email'),
|
|
163
|
+
}),
|
|
164
|
+
requiredInputs: ['project'],
|
|
165
|
+
run: async (ctx, input) => {
|
|
166
|
+
const projectId = (typeof input.project === 'string' && input.project) || ctx.projectId;
|
|
167
|
+
if (!projectId)
|
|
168
|
+
return [];
|
|
169
|
+
const search = String(input.search ?? '').trim();
|
|
170
|
+
const response = await ctx.client.projectUsers.fetchUsers(projectId, search ? { q: search, type: 'all' } : undefined);
|
|
171
|
+
return response?.data ?? [];
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ZodType } from 'zod';
|
|
2
|
+
import type { AntConnect } from '../AntConnect';
|
|
3
|
+
export type QueryKind = 'project' | 'task' | 'app' | 'document' | 'sbs' | 'table' | 'record' | 'license' | 'member';
|
|
4
|
+
export interface AiToolContext {
|
|
5
|
+
client: AntConnect;
|
|
6
|
+
licenseId: string;
|
|
7
|
+
projectId?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AiToolDescriptor {
|
|
10
|
+
name: string;
|
|
11
|
+
kind: QueryKind;
|
|
12
|
+
description: string;
|
|
13
|
+
inputSchema: ZodType;
|
|
14
|
+
requiredInputs: readonly string[];
|
|
15
|
+
run: (ctx: AiToolContext, input: Record<string, unknown>) => Promise<unknown[]>;
|
|
16
|
+
}
|
|
17
|
+
export type MutationTarget = 'table' | 'column' | 'record';
|
|
18
|
+
export type MutationOperation = 'create' | 'update' | 'delete' | 'clear';
|
|
19
|
+
/**
|
|
20
|
+
* Write counterpart of `AiToolDescriptor`. Deliberately a separate interface
|
|
21
|
+
* rather than a widened one: `run` returns a single result here, and forcing the
|
|
22
|
+
* query descriptors' array return to accommodate that would loosen the type for
|
|
23
|
+
* the nine read tools that genuinely do return rows.
|
|
24
|
+
*/
|
|
25
|
+
export interface AiMutationDescriptor {
|
|
26
|
+
name: string;
|
|
27
|
+
target: MutationTarget;
|
|
28
|
+
operation: MutationOperation;
|
|
29
|
+
description: string;
|
|
30
|
+
inputSchema: ZodType;
|
|
31
|
+
requiredInputs: readonly string[];
|
|
32
|
+
destructive: boolean;
|
|
33
|
+
run: (ctx: AiToolContext, input: Record<string, unknown>) => Promise<unknown>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/ai/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAA;AAClC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAM/C,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,MAAM,GAAG,KAAK,GAAG,UAAU,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,CAAA;AAKnH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,UAAU,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,SAAS,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;IAEnB,WAAW,EAAE,OAAO,CAAA;IAGpB,cAAc,EAAE,SAAS,MAAM,EAAE,CAAA;IAGjC,GAAG,EAAE,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,EAAE,CAAC,CAAA;CAChF;AAED,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAC1D,MAAM,MAAM,iBAAiB,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,OAAO,CAAA;AAExE;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,cAAc,CAAA;IACtB,SAAS,EAAE,iBAAiB,CAAA;IAC5B,WAAW,EAAE,MAAM,CAAA;IACnB,WAAW,EAAE,OAAO,CAAA;IACpB,cAAc,EAAE,SAAS,MAAM,EAAE,CAAA;IAIjC,WAAW,EAAE,OAAO,CAAA;IACpB,GAAG,EAAE,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAC9E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/comms/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/comms/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAA;AACtC,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACxE,OAAO,KAAK,EAAU,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAEpE,OAAO,KAAK,EAAE,UAAU,EAAE,sBAAoB;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;AACxD,OAAO,EAML,YAAY,EACb,MAAM,MAAM,CAAA;AAMb,KAAK,iBAAiB,GAAG,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAA;AACvE,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,GAAG,IAAI,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAA;AAE1F,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,UAAU,CAAA;IACpB,QAAQ,CAAC,EAAE,WAAW,CAAA;IACtB,gHAAgH;IAChH,iBAAiB,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAC;QAAC,IAAI,CAAC,EAAE,WAAW,CAAA;KAAE,CAAA;CAC/E;AAED,MAAM,WAAW,MAAO,SAAQ,YAAY;IAC1C,OAAO,EAAE,OAAO,CAAA;IAChB,aAAa,EAAE,aAAa,CAAA;IAC5B,OAAO,EAAE,UAAU,CAAA;IACnB,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;IAC5B,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG;QAC7B,GAAG,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,KAAK,IAAI,CAAA;QACpE,GAAG,EAAE,MAAM,OAAO,CAAA;KACnB,CAAA;IACD,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,GAAG;QAC7B,GAAG,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAA;QAC1B,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KACtC,CAAA;IACD,MAAM,EAAE,YAAY,GAAG,cAAc,CAAA;IACrC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IACrF,iBAAiB,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;CAChG;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CA+F3D"}
|
package/build/comms/client.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import * as Comlink from 'comlink';
|
|
2
1
|
import { BehaviorSubject, distinctUntilChanged, map, shareReplay, Subject, Subscription, } from 'rxjs';
|
|
2
|
+
import { newHostConnection } from './connection';
|
|
3
3
|
import { notificationTypes } from './models';
|
|
4
4
|
import { newSignaler } from './signal';
|
|
5
|
-
import { receivePort, requestPort } from './transport';
|
|
6
5
|
import { clone, newScope, takeUntilClosed } from './utils';
|
|
7
6
|
export function newCommsClient(params) {
|
|
8
7
|
const subscription = newScope();
|
|
@@ -10,49 +9,48 @@ export function newCommsClient(params) {
|
|
|
10
9
|
const toolbarSubject = new BehaviorSubject({ menu: [], search: { enabled: false, input: '' }, isLoading: false });
|
|
11
10
|
const appStateSubject = new BehaviorSubject({ timestamp: 0, state: '' });
|
|
12
11
|
const incomingSignals = new Subject();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}));
|
|
31
|
-
await proxy.onAppState(Comlink.proxy((state) => appStateSubject.next(state)));
|
|
32
|
-
await proxy.onToolbarClick(Comlink.proxy((index) => toolbarSubject.value.menu[index]?.onClick?.()));
|
|
33
|
-
await proxy.onToolbarSearch(Comlink.proxy((value) => {
|
|
12
|
+
const { host, close } = newHostConnection({
|
|
13
|
+
parent: params.parent,
|
|
14
|
+
manifest: params.manifest,
|
|
15
|
+
initial: params.initialConnection?.port
|
|
16
|
+
? {
|
|
17
|
+
port: params.initialConnection.port,
|
|
18
|
+
context: params.initialConnection.context,
|
|
19
|
+
// Carried through so the relayed handover pins the same clientId that
|
|
20
|
+
// later handovers are checked against.
|
|
21
|
+
clientId: params.initialConnection.clientId,
|
|
22
|
+
}
|
|
23
|
+
: undefined,
|
|
24
|
+
onContext: context => contextSubject.next(context),
|
|
25
|
+
onSignal: signals => incomingSignals.next({ data: signals, origin: '', source: null }),
|
|
26
|
+
onAppState: state => appStateSubject.next(state),
|
|
27
|
+
onToolbarClick: index => toolbarSubject.value.menu[index]?.onClick?.(),
|
|
28
|
+
onToolbarSearch: (value) => {
|
|
34
29
|
const t = toolbarSubject.value;
|
|
35
30
|
toolbarSubject.next({ ...t, search: { ...t.search, input: value ?? '' } });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
subscription.add(close);
|
|
34
|
+
// Subscribed once and resolved per emission: a re-adopted proxy has to receive
|
|
35
|
+
// toolbar updates too, which a `then`-captured reference would not.
|
|
36
|
+
toolbarSubject.pipe(takeUntilClosed(subscription)).subscribe(t => void host()
|
|
37
|
+
.then(p => p.setToolbar({ ...t, menu: t.menu.map(({ onClick: _onClick, ...item }) => item) }))
|
|
38
|
+
.catch(() => { }));
|
|
43
39
|
const signal = newSignaler({
|
|
44
|
-
send: async (signals) => (await
|
|
40
|
+
send: async (signals) => (await host()).sendSignal(clone(signals)),
|
|
45
41
|
receiver: incomingSignals.asObservable(),
|
|
46
42
|
});
|
|
47
43
|
const notepad = {
|
|
48
|
-
show: tab =>
|
|
49
|
-
showTask: (task, select) =>
|
|
50
|
-
hide: () =>
|
|
51
|
-
selectSbs: item =>
|
|
44
|
+
show: (tab, options) => host().then(p => p.notepad('show/tab', { tab, options })),
|
|
45
|
+
showTask: (task, select) => host().then(p => p.notepad('show/task', { task, select })),
|
|
46
|
+
hide: () => host().then(p => p.notepad('hide', {})),
|
|
47
|
+
selectSbs: item => host().then(p => p.notepad('sbs/select', { item })),
|
|
52
48
|
};
|
|
53
49
|
const notifications = notificationTypes.reduce((result, type) => Object.assign(result, {
|
|
50
|
+
// Fire-and-forget at every call site, so a missing host must not surface as
|
|
51
|
+
// an unhandled rejection on top of the error `host()` already logs.
|
|
54
52
|
[type]: (message) => {
|
|
55
|
-
const promise =
|
|
53
|
+
const promise = host().then(p => p.notify({ type, message })).catch(() => undefined);
|
|
56
54
|
return Object.assign(promise, new Subscription());
|
|
57
55
|
},
|
|
58
56
|
}), {});
|
|
@@ -62,7 +60,7 @@ export function newCommsClient(params) {
|
|
|
62
60
|
connect: params.connect ?? new Proxy({}, {
|
|
63
61
|
get: (_, service) => new Proxy({}, {
|
|
64
62
|
get: (_, name) => typeof service === 'string' && typeof name === 'string'
|
|
65
|
-
? (...args) =>
|
|
63
|
+
? (...args) => host().then(p => p.connect(service, name, clone(args)))
|
|
66
64
|
: undefined,
|
|
67
65
|
}),
|
|
68
66
|
}),
|
|
@@ -76,11 +74,11 @@ export function newCommsClient(params) {
|
|
|
76
74
|
get: () => toolbarSubject.value,
|
|
77
75
|
}),
|
|
78
76
|
appState: Object.assign(appStateSubject.pipe(map(e => e.state), distinctUntilChanged()), {
|
|
79
|
-
get: () =>
|
|
80
|
-
set: (state) =>
|
|
77
|
+
get: () => host().then(p => p.getAppState()).then(s => s.state),
|
|
78
|
+
set: (state) => host().then(p => p.setAppState({ timestamp: Date.now(), state })).then(() => { }),
|
|
81
79
|
}),
|
|
82
80
|
signal,
|
|
83
|
-
request: (req, _controller) =>
|
|
84
|
-
deprecatedRequest: (req, _controller) =>
|
|
81
|
+
request: (req, _controller) => host().then(p => p.request(req)),
|
|
82
|
+
deprecatedRequest: (req, _controller) => host().then(p => p.deprecatedRequest(req)),
|
|
85
83
|
});
|
|
86
84
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HostAPI } from './hostAPI';
|
|
2
|
+
import type { Context } from './models';
|
|
3
|
+
import type { Signal } from './signal';
|
|
4
|
+
import type { PortHandover } from './transport';
|
|
5
|
+
import type { AppStateData } from './utils';
|
|
6
|
+
import type { AppManifest } from "../services/AppService";
|
|
7
|
+
import * as Comlink from 'comlink';
|
|
8
|
+
export declare const NO_HOST_CONNECTION = "ant-comms: no connection to the ANT OS host \u2014 the app cannot reach the API";
|
|
9
|
+
export interface HostConnectionParams {
|
|
10
|
+
parent: Window;
|
|
11
|
+
manifest?: AppManifest | null;
|
|
12
|
+
/** Pre-established port for nested clients (e.g. Streamlit behind a relay). */
|
|
13
|
+
initial?: PortHandover;
|
|
14
|
+
onContext: (context: Context) => void;
|
|
15
|
+
onSignal: (signals: Signal[]) => void;
|
|
16
|
+
onAppState: (state: AppStateData) => void;
|
|
17
|
+
onToolbarClick: (index: number) => void;
|
|
18
|
+
onToolbarSearch: (value: string) => void;
|
|
19
|
+
}
|
|
20
|
+
export interface HostConnection {
|
|
21
|
+
/**
|
|
22
|
+
* The live host proxy, or a rejection once it is clear there will not be one.
|
|
23
|
+
*
|
|
24
|
+
* Call this per request rather than capturing the result: a retried handshake
|
|
25
|
+
* replaces the port, and anything issued afterwards has to travel over the
|
|
26
|
+
* port that is actually open.
|
|
27
|
+
*/
|
|
28
|
+
host: () => Promise<Comlink.Remote<HostAPI>>;
|
|
29
|
+
close: () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare function newHostConnection(params: HostConnectionParams): HostConnection;
|
|
32
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/comms/connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;AACxD,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AA2ClC,eAAO,MAAM,kBAAkB,oFACiD,CAAA;AAehF,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,CAAC,EAAE,WAAW,GAAG,IAAI,CAAA;IAC7B,+EAA+E;IAC/E,OAAO,CAAC,EAAE,YAAY,CAAA;IACtB,SAAS,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;IACrC,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAA;IACrC,UAAU,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IACzC,cAAc,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IACvC,eAAe,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CACzC;AAED,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,IAAI,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;IAC5C,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,oBAAoB,GAAG,cAAc,CAwH9E"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { newHostSlot } from './hostSlot';
|
|
3
|
+
import { onPort, requestPort } from './transport';
|
|
4
|
+
/**
|
|
5
|
+
* Owns the port handover between an app and its ANT OS host, and keeps it alive.
|
|
6
|
+
*
|
|
7
|
+
* Both halves of the handover are single unbuffered postMessages: the client
|
|
8
|
+
* announces READY, the host answers with INIT and a transferred port. Either can
|
|
9
|
+
* land while the far side has no listener yet — the host only arms its READY
|
|
10
|
+
* listener once it knows which window to accept it from, and the client's is not
|
|
11
|
+
* installed until its bundle runs. One attempt each meant a miss was permanent:
|
|
12
|
+
* the app mounted, rendered, and then never fetched anything, because every
|
|
13
|
+
* `connect()` call awaited a proxy that would never arrive.
|
|
14
|
+
*
|
|
15
|
+
* So both sides now keep trying, which is what the legacy post-robot path has
|
|
16
|
+
* always done with its init interval (see comms/compat.ts) and why it never
|
|
17
|
+
* showed this failure. The host re-exposes on every READY it sees; this side
|
|
18
|
+
* re-announces until a port lands, and adopts each port the host offers.
|
|
19
|
+
*/
|
|
20
|
+
/** Re-announce this often until the host hands a port over. */
|
|
21
|
+
const HANDSHAKE_RETRY_MS = 500;
|
|
22
|
+
/**
|
|
23
|
+
* Hold off re-announcing for this long after a port arrives.
|
|
24
|
+
*
|
|
25
|
+
* Negotiating one costs several round trips, and the host closes the previous
|
|
26
|
+
* port every time it answers an announcement — so announcing again mid-negotiation
|
|
27
|
+
* would pull the port out from under the negotiation that is already succeeding,
|
|
28
|
+
* on repeat. A bounded pause rather than a flag: if negotiation genuinely stalls
|
|
29
|
+
* (a port whose host has gone away never settles), retrying has to resume rather
|
|
30
|
+
* than wait on a promise that will not.
|
|
31
|
+
*/
|
|
32
|
+
const NEGOTIATION_GRACE_MS = 2000;
|
|
33
|
+
/**
|
|
34
|
+
* Stop waiting for a host after this long and reject calls instead of leaving
|
|
35
|
+
* them pending forever. A permanently unresolved promise is the worst failure
|
|
36
|
+
* mode available here: no request, no error, no timeout — nothing to see.
|
|
37
|
+
*/
|
|
38
|
+
const HANDSHAKE_TIMEOUT_MS = 20000;
|
|
39
|
+
export const NO_HOST_CONNECTION = 'ant-comms: no connection to the ANT OS host — the app cannot reach the API';
|
|
40
|
+
/**
|
|
41
|
+
* Whether there is a host to announce to at all.
|
|
42
|
+
*
|
|
43
|
+
* A standalone page is its own parent and has none, and should not spend the
|
|
44
|
+
* tab's lifetime posting messages to itself. Compared against `window` rather
|
|
45
|
+
* than `globalThis` because only the realm's window object is type-compatible
|
|
46
|
+
* with the parent handle; where there is no DOM at all (tests) the caller's
|
|
47
|
+
* parent is a stand-in and worth announcing to.
|
|
48
|
+
*/
|
|
49
|
+
function isEmbedded(parent) {
|
|
50
|
+
return typeof window === 'undefined' || parent !== window;
|
|
51
|
+
}
|
|
52
|
+
export function newHostConnection(params) {
|
|
53
|
+
let releaseCurrent = null;
|
|
54
|
+
/**
|
|
55
|
+
* Bumped per handover. One that finishes negotiating after a newer one started
|
|
56
|
+
* is holding a port the host has already closed, so adopting it would produce
|
|
57
|
+
* a client that looks connected and answers nothing.
|
|
58
|
+
*/
|
|
59
|
+
let generation = 0;
|
|
60
|
+
let closed = false;
|
|
61
|
+
/**
|
|
62
|
+
* The host's own id, learned from the first handover. Later handovers must
|
|
63
|
+
* carry the same one: a forged INIT that reached us despite the source check
|
|
64
|
+
* must not be able to swap the adopted proxy for one it controls.
|
|
65
|
+
*/
|
|
66
|
+
let acceptedClientId = null;
|
|
67
|
+
const slot = newHostSlot(HANDSHAKE_TIMEOUT_MS, NO_HOST_CONNECTION);
|
|
68
|
+
const negotiate = async (handover) => {
|
|
69
|
+
if (closed) {
|
|
70
|
+
handover.port.close();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const mine = ++generation;
|
|
74
|
+
params.onContext(handover.context);
|
|
75
|
+
const candidate = Comlink.wrap(handover.port);
|
|
76
|
+
const release = () => {
|
|
77
|
+
candidate[Comlink.releaseProxy]();
|
|
78
|
+
handover.port.close();
|
|
79
|
+
};
|
|
80
|
+
// Registered together rather than in sequence: each is its own round trip,
|
|
81
|
+
// and five of them stretch the window during which a retry could fire and
|
|
82
|
+
// pull this port out from under us.
|
|
83
|
+
await Promise.all([
|
|
84
|
+
candidate.onContext(Comlink.proxy(params.onContext)),
|
|
85
|
+
candidate.onSignal(Comlink.proxy(params.onSignal)),
|
|
86
|
+
candidate.onAppState(Comlink.proxy(params.onAppState)),
|
|
87
|
+
candidate.onToolbarClick(Comlink.proxy(params.onToolbarClick)),
|
|
88
|
+
candidate.onToolbarSearch(Comlink.proxy(params.onToolbarSearch)),
|
|
89
|
+
]);
|
|
90
|
+
// Last, so readiness still means "every callback is wired".
|
|
91
|
+
await candidate.ready(params.manifest ?? null);
|
|
92
|
+
if (mine !== generation || closed) {
|
|
93
|
+
release();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
acceptedClientId ?? (acceptedClientId = handover.clientId);
|
|
97
|
+
releaseCurrent?.();
|
|
98
|
+
releaseCurrent = release;
|
|
99
|
+
slot.set(candidate);
|
|
100
|
+
};
|
|
101
|
+
// Negotiation over a port the host has since closed simply never settles, so
|
|
102
|
+
// the generation check decides the winner rather than the order these resolve.
|
|
103
|
+
let quietUntil = 0;
|
|
104
|
+
const stopListening = onPort(isEmbedded(params.parent) ? params.parent : null, (handover) => {
|
|
105
|
+
// A handover naming a different host than the one we already accepted is not
|
|
106
|
+
// ours to adopt, whatever its source claims to be.
|
|
107
|
+
if (acceptedClientId !== null && handover.clientId !== acceptedClientId) {
|
|
108
|
+
handover.port.close();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
quietUntil = Date.now() + NEGOTIATION_GRACE_MS;
|
|
112
|
+
void negotiate(handover).catch(() => { });
|
|
113
|
+
});
|
|
114
|
+
let announceTimer = null;
|
|
115
|
+
let giveUpTimer = null;
|
|
116
|
+
const stopAnnouncing = () => {
|
|
117
|
+
if (announceTimer !== null) {
|
|
118
|
+
clearInterval(announceTimer);
|
|
119
|
+
announceTimer = null;
|
|
120
|
+
}
|
|
121
|
+
if (giveUpTimer !== null) {
|
|
122
|
+
clearTimeout(giveUpTimer);
|
|
123
|
+
giveUpTimer = null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
if (isEmbedded(params.parent)) {
|
|
127
|
+
requestPort(params.parent);
|
|
128
|
+
announceTimer = setInterval(() => {
|
|
129
|
+
if (slot.peek())
|
|
130
|
+
return stopAnnouncing();
|
|
131
|
+
if (Date.now() < quietUntil)
|
|
132
|
+
return;
|
|
133
|
+
requestPort(params.parent);
|
|
134
|
+
}, HANDSHAKE_RETRY_MS);
|
|
135
|
+
// Give up in step with `host()`, so a genuinely host-less page stops posting
|
|
136
|
+
// rather than retrying for the lifetime of the tab. A host that shows up
|
|
137
|
+
// later still re-exposes on its own, and `onPort` is still listening.
|
|
138
|
+
giveUpTimer = setTimeout(stopAnnouncing, HANDSHAKE_TIMEOUT_MS);
|
|
139
|
+
}
|
|
140
|
+
if (params.initial)
|
|
141
|
+
void negotiate(params.initial).catch(() => { });
|
|
142
|
+
return {
|
|
143
|
+
host: slot.get,
|
|
144
|
+
close: () => {
|
|
145
|
+
if (closed)
|
|
146
|
+
return;
|
|
147
|
+
closed = true;
|
|
148
|
+
// Advance past every in-flight negotiation so none can adopt after this,
|
|
149
|
+
// and fail anything already waiting rather than leaving it to time out
|
|
150
|
+
// against a connection that is never coming back.
|
|
151
|
+
generation++;
|
|
152
|
+
stopAnnouncing();
|
|
153
|
+
stopListening();
|
|
154
|
+
releaseCurrent?.();
|
|
155
|
+
releaseCurrent = null;
|
|
156
|
+
slot.reset();
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BehaviorSubject, Observable, Subscription } from 'rxjs';
|
|
2
|
+
import type { HostAPI } from './hostAPI';
|
|
3
|
+
import type { Context } from './models';
|
|
4
|
+
export interface HandshakeParams {
|
|
5
|
+
clientId: string;
|
|
6
|
+
scope: Subscription;
|
|
7
|
+
/** Emits the pane's iframe window, and again whenever it is replaced. */
|
|
8
|
+
windowChanges: Observable<Window | undefined>;
|
|
9
|
+
contextChanges: BehaviorSubject<Context>;
|
|
10
|
+
connectionInitialized: Observable<void>;
|
|
11
|
+
hostAPI: HostAPI;
|
|
12
|
+
/** Drops callbacks bound to a port that is about to be replaced. */
|
|
13
|
+
resetCallbacks: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function installHandshake(params: HandshakeParams): void;
|
|
16
|
+
//# sourceMappingURL=handshake.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handshake.d.ts","sourceRoot":"","sources":["../../src/comms/handshake.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AACrE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AA+BvC,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,YAAY,CAAA;IACnB,yEAAyE;IACzE,aAAa,EAAE,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IAC7C,cAAc,EAAE,eAAe,CAAC,OAAO,CAAC,CAAA;IACxC,qBAAqB,EAAE,UAAU,CAAC,IAAI,CAAC,CAAA;IACvC,OAAO,EAAE,OAAO,CAAA;IAChB,oEAAoE;IACpE,cAAc,EAAE,MAAM,IAAI,CAAA;CAC3B;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAoD9D"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { filter, take } from 'rxjs';
|
|
3
|
+
import { onPortRequest, transferPort } from './transport';
|
|
4
|
+
import { takeUntilClosed } from './utils';
|
|
5
|
+
/**
|
|
6
|
+
* The host half of the port handover, and keeping it alive.
|
|
7
|
+
*
|
|
8
|
+
* The handover is one unbuffered postMessage in each direction: the client
|
|
9
|
+
* announces READY, the host answers with INIT and a transferred port. Either can
|
|
10
|
+
* land while the far side has no listener yet — the client's bundle may not have
|
|
11
|
+
* run when the host first exposes, and the host's READY listener is only armed
|
|
12
|
+
* once it knows which window to accept it from.
|
|
13
|
+
*
|
|
14
|
+
* Answering only the first announcement turned a single miss into a permanently
|
|
15
|
+
* dead channel: every `connect()` call from the app awaited a proxy that never
|
|
16
|
+
* arrived, with no request, no error and no timeout to show for it. So the client
|
|
17
|
+
* re-announces until a port lands (comms/connection.ts) and the host re-exposes
|
|
18
|
+
* for every announcement it sees. Convergence needs both halves. This is what the
|
|
19
|
+
* legacy post-robot path has always done with its init interval (comms/compat.ts),
|
|
20
|
+
* and why it never showed this failure.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* How long a bound client may go without completing the handshake before the host
|
|
24
|
+
* says so. Matches the client's own give-up window so both ends report the same
|
|
25
|
+
* failure at the same time.
|
|
26
|
+
*/
|
|
27
|
+
const HANDSHAKE_WATCHDOG_MS = 20000;
|
|
28
|
+
export function installHandshake(params) {
|
|
29
|
+
const { clientId, scope, hostAPI, resetCallbacks } = params;
|
|
30
|
+
let currentPort = null;
|
|
31
|
+
let disposeReadyListener = null;
|
|
32
|
+
let watchdog = null;
|
|
33
|
+
const expose = (target) => {
|
|
34
|
+
currentPort?.close();
|
|
35
|
+
currentPort = transferPort(target, { clientId, context: params.contextChanges.value });
|
|
36
|
+
Comlink.expose(hostAPI, currentPort);
|
|
37
|
+
};
|
|
38
|
+
// Declared ahead of the subscription below: windowChanges is a ReplaySubject,
|
|
39
|
+
// so subscribing can emit synchronously, and anything the callback reaches for
|
|
40
|
+
// must already be initialised.
|
|
41
|
+
const clearWatchdog = () => {
|
|
42
|
+
if (watchdog !== null) {
|
|
43
|
+
clearTimeout(watchdog);
|
|
44
|
+
// Nulled, not just cleared, so a later window re-arms it — otherwise a
|
|
45
|
+
// replaced iframe that never connects is never reported.
|
|
46
|
+
watchdog = null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
params.windowChanges.pipe(filter(Boolean), takeUntilClosed(scope)).subscribe((iframeWindow) => {
|
|
50
|
+
// Re-armed per window: the previous listener only accepts READY from the
|
|
51
|
+
// window it was created for, which is no longer the one in the pane.
|
|
52
|
+
disposeReadyListener?.();
|
|
53
|
+
disposeReadyListener = onPortRequest(iframeWindow, (source) => {
|
|
54
|
+
resetCallbacks();
|
|
55
|
+
expose(source);
|
|
56
|
+
});
|
|
57
|
+
expose(iframeWindow);
|
|
58
|
+
// Report a channel that never came up from this side too. The embedder clears
|
|
59
|
+
// its loading overlay on a grace period, so an app whose handshake failed
|
|
60
|
+
// looks loaded and merely idle, and the app's own error lands in the iframe's
|
|
61
|
+
// console rather than the one anyone opens first. Armed on the first window
|
|
62
|
+
// rather than at construction, so a pane that never opens an app stays quiet.
|
|
63
|
+
watchdog ?? (watchdog = setTimeout(() => {
|
|
64
|
+
console.warn(`ant-comms: client ${clientId} never completed the port handshake — the app cannot reach the API`);
|
|
65
|
+
}, HANDSHAKE_WATCHDOG_MS));
|
|
66
|
+
});
|
|
67
|
+
params.connectionInitialized.pipe(take(1), takeUntilClosed(scope)).subscribe(clearWatchdog);
|
|
68
|
+
scope.add(() => {
|
|
69
|
+
disposeReadyListener?.();
|
|
70
|
+
clearWatchdog();
|
|
71
|
+
currentPort?.close();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/comms/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,MAAM,CAAA;AACjE,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,WAAW,CAAA;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAE1F,OAAO,KAAK,EAAE,UAAU,EAAE,sBAAoB;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/comms/host.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,MAAM,CAAA;AACjE,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,WAAW,CAAA;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAE1F,OAAO,KAAK,EAAE,UAAU,EAAE,sBAAoB;AAC9C,OAAO,KAAK,EAAE,WAAW,EAAE,+BAA6B;AACxD,OAAO,EACL,eAAe,EAKf,aAAa,EAIb,YAAY,EAEb,MAAM,MAAM,CAAA;AAQb,YAAY,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAchD,MAAM,MAAM,qBAAqB,GAAG,CAClC,IAAI,EAAE,MAAM,YAAY,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,EACxC,EAAE,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,EAC7B,OAAO,EAAE,MAAM,EAAE,EACjB,QAAQ,EAAE,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,KAAK,IAAI,EACvC,KAAK,CAAC,EAAE,CAAC,SAAS,GAAG,SAAS,CAAC,EAAE,KAC9B,MAAM,IAAI,CAAA;AAEf,MAAM,WAAW,UAAU;IACzB,aAAa,EAAE,UAAU,CAAC,MAAM,CAAC,CAAA;IACjC,cAAc,EAAE,eAAe,CAAC,OAAO,CAAC,CAAA;IACxC,eAAe,EAAE,CAAC,YAAY,EAAE,YAAY,KAAK,IAAI,CAAA;IACrD,OAAO,EAAE,UAAU,CAAA;IACnB,OAAO,EAAE,OAAO,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iBAAiB,EAAE,qBAAqB,CAAA;CACzC;AAED,MAAM,WAAW,GAAG;IAClB,MAAM,EAAE,UAAU,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,YAAY,CAAA;IACnB,qBAAqB,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IACpC,eAAe,EAAE,aAAa,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;CACnD;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;AAErG,MAAM,WAAW,IAAK,SAAQ,gBAAgB;IAC5C,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG;QAAE,GAAG,EAAE,MAAM,OAAO,CAAC;QAAC,GAAG,EAAE;YAAE,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;SAAE,CAAA;KAAE,CAAA;IAC3G,uBAAuB,EAAE,UAAU,CAAC,OAAO,CAAC,CAAA;IAC5C,QAAQ,EAAE,UAAU,CAAC,WAAW,GAAG,IAAI,CAAC,CAAA;IACxC,MAAM,EAAE,cAAc,GAAG,YAAY,CAAA;IACrC,QAAQ,EAAE;QACR,OAAO,EAAE,UAAU,CAAC,MAAM,CAAC,CAAA;QAC3B,GAAG,EAAE,MAAM,MAAM,CAAA;QACjB,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;KAC7B,CAAA;IACD,KAAK,EAAE,YAAY,CAAA;CACpB;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAmGrD"}
|