@harnesys/sdk 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +80 -2
- package/dist/addon-context-factory.d.ts +6 -0
- package/dist/addon-context-factory.js +160 -0
- package/dist/addon-context.d.ts +346 -0
- package/dist/addon-context.js +0 -0
- package/dist/define-addon.d.ts +57 -0
- package/dist/define-addon.js +53 -0
- package/dist/families/config.d.ts +14 -0
- package/dist/families/config.js +16 -0
- package/dist/families/content.d.ts +28 -0
- package/dist/families/content.js +19 -0
- package/dist/families/field-spec.d.ts +100 -0
- package/dist/families/field-spec.js +85 -0
- package/dist/families/hook.d.ts +37 -0
- package/dist/families/hook.js +25 -0
- package/dist/families/job.d.ts +45 -0
- package/dist/families/job.js +26 -0
- package/dist/families/json-schema.d.ts +36 -0
- package/dist/families/json-schema.js +101 -0
- package/dist/families/lsp.d.ts +18 -0
- package/dist/families/lsp.js +18 -0
- package/dist/families/mcp.d.ts +18 -0
- package/dist/families/mcp.js +18 -0
- package/dist/families/registry.d.ts +9 -0
- package/dist/families/registry.js +34 -0
- package/dist/families/route.d.ts +38 -0
- package/dist/families/route.js +37 -0
- package/dist/families/source-kind.d.ts +24 -0
- package/dist/families/source-kind.js +21 -0
- package/dist/families/tool-pack.d.ts +68 -0
- package/dist/families/tool-pack.js +57 -0
- package/dist/families/transport.d.ts +52 -0
- package/dist/families/transport.js +29 -0
- package/dist/families/trigger.d.ts +97 -0
- package/dist/families/trigger.js +46 -0
- package/dist/families/view.d.ts +255 -0
- package/dist/families/view.js +87 -0
- package/dist/families/viewer.d.ts +31 -0
- package/dist/families/viewer.js +26 -0
- package/dist/families/worker.d.ts +23 -0
- package/dist/families/worker.js +19 -0
- package/dist/host-client.d.ts +11 -0
- package/dist/host-client.js +30 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +35 -0
- package/dist/manifest/branding.d.ts +14 -0
- package/dist/manifest/branding.js +18 -0
- package/dist/manifest/connections.d.ts +21 -0
- package/dist/manifest/connections.js +10 -0
- package/dist/manifest/json-schema.d.ts +2 -0
- package/dist/manifest/json-schema.js +13 -0
- package/dist/manifest/l10n.d.ts +9 -0
- package/dist/manifest/l10n.js +28 -0
- package/dist/manifest/manifest.d.ts +112 -0
- package/dist/manifest/manifest.js +45 -0
- package/dist/manifest/validate.d.ts +14 -0
- package/dist/manifest/validate.js +144 -0
- package/dist/permissions/permissions.d.ts +31 -0
- package/dist/permissions/permissions.js +53 -0
- package/dist/process-entry.d.ts +26 -0
- package/dist/process-entry.js +146 -0
- package/dist/protocol/envelope.d.ts +115 -0
- package/dist/protocol/envelope.js +72 -0
- package/dist/protocol/error.d.ts +67 -0
- package/dist/protocol/error.js +61 -0
- package/dist/protocol/handshake.d.ts +55 -0
- package/dist/protocol/handshake.js +26 -0
- package/dist/protocol/map.d.ts +1090 -0
- package/dist/protocol/map.js +127 -0
- package/dist/protocol/meta.d.ts +31 -0
- package/dist/protocol/meta.js +14 -0
- package/dist/protocol/method-spec.d.ts +13 -0
- package/dist/protocol/method-spec.js +9 -0
- package/dist/protocol/org-service-methods.d.ts +135 -0
- package/dist/protocol/org-service-methods.js +106 -0
- package/dist/protocol/shared.d.ts +389 -0
- package/dist/protocol/shared.js +231 -0
- package/dist/protocol/source-methods.d.ts +218 -0
- package/dist/protocol/source-methods.js +18 -0
- package/dist/protocol/thread-service-methods.d.ts +50 -0
- package/dist/protocol/thread-service-methods.js +32 -0
- package/dist/runtime.d.ts +25 -0
- package/dist/runtime.js +229 -0
- package/dist/testing/fake-db.d.ts +15 -0
- package/dist/testing/fake-db.js +120 -0
- package/dist/testing/fake-files.d.ts +9 -0
- package/dist/testing/fake-files.js +87 -0
- package/dist/testing/fake-host.d.ts +115 -0
- package/dist/testing/fake-host.js +152 -0
- package/dist/testing/fake-services.d.ts +63 -0
- package/dist/testing/fake-services.js +277 -0
- package/dist/testing/fixtures.d.ts +4 -0
- package/dist/testing/fixtures.js +84 -0
- package/dist/testing/index.d.ts +4 -0
- package/dist/testing/index.js +2 -0
- package/package.json +66 -4
- package/src/addon-context-factory.ts +236 -0
- package/src/addon-context.ts +313 -0
- package/src/define-addon.ts +178 -0
- package/src/families/config.ts +24 -0
- package/src/families/content.ts +35 -0
- package/src/families/field-spec.ts +175 -0
- package/src/families/hook.ts +34 -0
- package/src/families/job.ts +41 -0
- package/src/families/json-schema.ts +167 -0
- package/src/families/lsp.ts +28 -0
- package/src/families/mcp.ts +28 -0
- package/src/families/registry.ts +44 -0
- package/src/families/route.ts +52 -0
- package/src/families/source-kind.ts +34 -0
- package/src/families/tool-pack.ts +92 -0
- package/src/families/transport.ts +52 -0
- package/src/families/trigger.ts +92 -0
- package/src/families/view.ts +144 -0
- package/src/families/viewer.ts +43 -0
- package/src/families/worker.ts +30 -0
- package/src/host-client.ts +52 -0
- package/src/index.ts +341 -0
- package/src/manifest/branding.ts +24 -0
- package/src/manifest/connections.ts +21 -0
- package/src/manifest/json-schema.ts +24 -0
- package/src/manifest/l10n.ts +39 -0
- package/src/manifest/manifest.ts +94 -0
- package/src/manifest/validate.ts +200 -0
- package/src/permissions/permissions.ts +79 -0
- package/src/process-entry.ts +205 -0
- package/src/protocol/envelope.ts +126 -0
- package/src/protocol/error.ts +81 -0
- package/src/protocol/handshake.ts +43 -0
- package/src/protocol/map.ts +339 -0
- package/src/protocol/meta.ts +26 -0
- package/src/protocol/method-spec.ts +40 -0
- package/src/protocol/org-service-methods.ts +148 -0
- package/src/protocol/shared.ts +345 -0
- package/src/protocol/source-methods.ts +61 -0
- package/src/protocol/thread-service-methods.ts +50 -0
- package/src/runtime.ts +332 -0
- package/src/testing/fake-db.ts +140 -0
- package/src/testing/fake-files.ts +114 -0
- package/src/testing/fake-host.ts +261 -0
- package/src/testing/fake-services.ts +345 -0
- package/src/testing/fixtures.ts +88 -0
- package/src/testing/index.ts +12 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { isBaselinePermissionId } from "../permissions/permissions.js";
|
|
2
|
+
import { AddonError } from "../protocol/error.js";
|
|
3
|
+
import { executeFilesService } from "./fake-files.js";
|
|
4
|
+
export function createFakeServices(context) {
|
|
5
|
+
const grants = context.options.grants ??
|
|
6
|
+
context.manifest.permissions.map((p) => ({ id: p.id, scope: p.scope }));
|
|
7
|
+
function granted(permission) {
|
|
8
|
+
if (isBaselinePermissionId(permission)) {
|
|
9
|
+
return { id: permission };
|
|
10
|
+
}
|
|
11
|
+
if (permission === 'trigger' &&
|
|
12
|
+
context.manifest.contributions.some((c) => c.family === 'trigger')) {
|
|
13
|
+
return { id: permission };
|
|
14
|
+
}
|
|
15
|
+
if (permission === 'worker' &&
|
|
16
|
+
context.manifest.contributions.some((c) => c.family === 'worker')) {
|
|
17
|
+
return { id: permission };
|
|
18
|
+
}
|
|
19
|
+
return grants.find((grant) => grant.id === permission);
|
|
20
|
+
}
|
|
21
|
+
function hasGrant(permission) {
|
|
22
|
+
return granted(permission) !== undefined;
|
|
23
|
+
}
|
|
24
|
+
async function execute(method, params) {
|
|
25
|
+
switch (method) {
|
|
26
|
+
case 'db.exec':
|
|
27
|
+
case 'db.all':
|
|
28
|
+
case 'db.get':
|
|
29
|
+
case 'db.run':
|
|
30
|
+
case 'db.batch':
|
|
31
|
+
return executeDb(method, params);
|
|
32
|
+
case 'accounts.list':
|
|
33
|
+
case 'accounts.report':
|
|
34
|
+
case 'accounts.access':
|
|
35
|
+
return executeAccounts(method, params);
|
|
36
|
+
case 'http.fetch':
|
|
37
|
+
return executeHttp(params);
|
|
38
|
+
case 'files.stat':
|
|
39
|
+
case 'files.read':
|
|
40
|
+
case 'files.write':
|
|
41
|
+
return executeFilesService(method, params, {
|
|
42
|
+
options: context.options,
|
|
43
|
+
writes: context.sinks.fileWrites,
|
|
44
|
+
});
|
|
45
|
+
case 'triggers.fire':
|
|
46
|
+
return fireTrigger(params);
|
|
47
|
+
case 'triggers.create':
|
|
48
|
+
return createTriggerInstance(params);
|
|
49
|
+
case 'triggers.update':
|
|
50
|
+
return updateTriggerInstance(params);
|
|
51
|
+
default:
|
|
52
|
+
return executeRecord(method, params);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function executeDb(method, params) {
|
|
56
|
+
const db = context.db;
|
|
57
|
+
switch (method) {
|
|
58
|
+
case 'db.exec':
|
|
59
|
+
db.exec(params.sql);
|
|
60
|
+
return {};
|
|
61
|
+
case 'db.all':
|
|
62
|
+
return { rows: db.all(params.sql, params.params) };
|
|
63
|
+
case 'db.get':
|
|
64
|
+
return { row: db.get(params.sql, params.params) };
|
|
65
|
+
case 'db.run':
|
|
66
|
+
return db.run(params.sql, params.params);
|
|
67
|
+
default:
|
|
68
|
+
return { results: db.batch(params.statements) };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function executeAccounts(method, params) {
|
|
72
|
+
const connection = params.connection;
|
|
73
|
+
assertConnection(connection);
|
|
74
|
+
if (method === 'accounts.list') {
|
|
75
|
+
const accounts = boundAccounts(connection);
|
|
76
|
+
return {
|
|
77
|
+
accounts: accounts.map((account) => ({
|
|
78
|
+
accountId: account.accountId,
|
|
79
|
+
label: account.label ?? account.accountId,
|
|
80
|
+
externalAccountId: account.externalAccountId ?? account.accountId,
|
|
81
|
+
status: account.status ?? 'connected',
|
|
82
|
+
})),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (method === 'accounts.report') {
|
|
86
|
+
context.sinks.accountReports.push({
|
|
87
|
+
connection,
|
|
88
|
+
accountId: params.accountId,
|
|
89
|
+
code: params.code,
|
|
90
|
+
});
|
|
91
|
+
return {};
|
|
92
|
+
}
|
|
93
|
+
const account = selectAccount(connection, params.accountId);
|
|
94
|
+
return { credentials: account.credentials ?? null, expiresAt: account.expiresAt };
|
|
95
|
+
}
|
|
96
|
+
async function executeHttp(params) {
|
|
97
|
+
const request = params;
|
|
98
|
+
let url;
|
|
99
|
+
try {
|
|
100
|
+
url = new URL(request.url);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
throw new AddonError('invalid_params', { detail: `url '${request.url}' is not valid` });
|
|
104
|
+
}
|
|
105
|
+
if (url.protocol !== 'https:') {
|
|
106
|
+
throw new AddonError('permission_denied', { detail: 'egress policy allows https only' });
|
|
107
|
+
}
|
|
108
|
+
const grant = granted('http.fetch');
|
|
109
|
+
if (grant?.scope &&
|
|
110
|
+
Array.isArray(grant.scope.hosts) &&
|
|
111
|
+
!grant.scope.hosts.includes(url.host)) {
|
|
112
|
+
throw new AddonError('permission_denied', {
|
|
113
|
+
detail: `host '${url.host}' is outside the granted scope`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (!context.options.http) {
|
|
117
|
+
throw new AddonError('unavailable', { detail: 'fake host has no http handler configured' });
|
|
118
|
+
}
|
|
119
|
+
const response = await context.options.http({
|
|
120
|
+
url: request.url,
|
|
121
|
+
method: request.method,
|
|
122
|
+
headers: request.headers ?? {},
|
|
123
|
+
body: request.body,
|
|
124
|
+
auth: params.auth,
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
status: response.status,
|
|
128
|
+
headers: response.headers ?? {},
|
|
129
|
+
body: response.body ?? null,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function fireTrigger(params) {
|
|
133
|
+
const instanceId = params.instanceId;
|
|
134
|
+
const runKey = params.runKey;
|
|
135
|
+
if (!context.startedTriggers.has(instanceId)) {
|
|
136
|
+
throw new AddonError('not_found', {
|
|
137
|
+
detail: `trigger instance '${instanceId}' was not started`,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const key = `${instanceId}:${runKey}`;
|
|
141
|
+
if (context.firedKeys.has(key)) {
|
|
142
|
+
return { outcome: 'suppressed' };
|
|
143
|
+
}
|
|
144
|
+
context.firedKeys.add(key);
|
|
145
|
+
return { outcome: 'fired' };
|
|
146
|
+
}
|
|
147
|
+
function createTriggerInstance(params) {
|
|
148
|
+
const kind = params.kind;
|
|
149
|
+
const declared = context.manifest.contributions.some((c) => c.family === 'trigger' && c.descriptor.kind === kind);
|
|
150
|
+
if (!declared) {
|
|
151
|
+
throw new AddonError('invalid_params', {
|
|
152
|
+
detail: `trigger kind is not declared: ${kind}`,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const instanceId = `trigger-instance-${context.sinks.createdTriggerInstances.length + 1}`;
|
|
156
|
+
context.sinks.createdTriggerInstances.push({ ...params, instanceId });
|
|
157
|
+
context.startedTriggers.add(instanceId);
|
|
158
|
+
return { instanceId };
|
|
159
|
+
}
|
|
160
|
+
function updateTriggerInstance(params) {
|
|
161
|
+
const instanceId = params.instanceId;
|
|
162
|
+
const stored = context.sinks.createdTriggerInstances.find((row) => row.instanceId === instanceId);
|
|
163
|
+
if (!stored) {
|
|
164
|
+
throw new AddonError('not_found', {
|
|
165
|
+
detail: `trigger instance not found: ${instanceId}`,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
for (const key of ['label', 'config', 'target', 'instructions']) {
|
|
169
|
+
if (params[key] !== undefined) {
|
|
170
|
+
stored[key] = params[key];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return { instanceId };
|
|
174
|
+
}
|
|
175
|
+
function executeRecord(method, params) {
|
|
176
|
+
const sinks = context.sinks;
|
|
177
|
+
switch (method) {
|
|
178
|
+
case 'config.get':
|
|
179
|
+
return configValues(params.keys);
|
|
180
|
+
case 'secrets.get': {
|
|
181
|
+
const value = context.options.secrets?.[params.key];
|
|
182
|
+
return { value: value === undefined ? null : value };
|
|
183
|
+
}
|
|
184
|
+
case 'log.write':
|
|
185
|
+
sinks.logs.push({
|
|
186
|
+
level: params.level,
|
|
187
|
+
message: params.message,
|
|
188
|
+
fields: params.fields,
|
|
189
|
+
});
|
|
190
|
+
return {};
|
|
191
|
+
case 'events.emit': {
|
|
192
|
+
const name = params.name;
|
|
193
|
+
if (!name.startsWith(`${context.manifest.id}.`)) {
|
|
194
|
+
throw new AddonError('invalid_params', {
|
|
195
|
+
detail: `events.emit name '${name}' is outside the addon namespace '${context.manifest.id}.*'`,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
sinks.emitted.push({ name, payload: params.payload });
|
|
199
|
+
return {};
|
|
200
|
+
}
|
|
201
|
+
case 'runs.dispatch':
|
|
202
|
+
sinks.dispatchedRuns.push(params);
|
|
203
|
+
return { runId: `run-${sinks.dispatchedRuns.length}` };
|
|
204
|
+
case 'runs.cancel':
|
|
205
|
+
sinks.cancelledRuns.push(params);
|
|
206
|
+
return { ok: true };
|
|
207
|
+
case 'threads.create': {
|
|
208
|
+
const threadId = `thread-${sinks.createdThreads.length + 1}`;
|
|
209
|
+
sinks.createdThreads.push(threadId);
|
|
210
|
+
return { threadId };
|
|
211
|
+
}
|
|
212
|
+
case 'threads.get':
|
|
213
|
+
return { thread: undefined };
|
|
214
|
+
case 'threads.patch':
|
|
215
|
+
sinks.patchedThreads.push(params);
|
|
216
|
+
return {};
|
|
217
|
+
case 'agents.list':
|
|
218
|
+
return { agents: [] };
|
|
219
|
+
case 'bridge.inbound':
|
|
220
|
+
sinks.inboundMessages.push(params.message);
|
|
221
|
+
return { outcome: 'dispatched' };
|
|
222
|
+
case 'workers.report':
|
|
223
|
+
sinks.workerReports.push(params);
|
|
224
|
+
return {};
|
|
225
|
+
default:
|
|
226
|
+
throw new AddonError('unsupported', {
|
|
227
|
+
detail: `fake host has no service implementation for '${method}'`,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function configValues(keys) {
|
|
232
|
+
const values = { ...context.options.config };
|
|
233
|
+
if (!keys) {
|
|
234
|
+
return { values };
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
values: Object.fromEntries(keys.filter((key) => key in values).map((key) => [key, values[key]])),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function boundAccounts(connection) {
|
|
241
|
+
return (context.options.accounts ?? []).filter((account) => account.connection === connection);
|
|
242
|
+
}
|
|
243
|
+
function assertConnection(connection) {
|
|
244
|
+
const declared = context.manifest.connections.some((requirement) => requirement.id === connection);
|
|
245
|
+
if (!declared) {
|
|
246
|
+
throw new AddonError('invalid_params', {
|
|
247
|
+
detail: `connection '${connection}' is not declared in the manifest`,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function selectAccount(connection, accountId) {
|
|
252
|
+
const accounts = boundAccounts(connection);
|
|
253
|
+
if (accounts.length === 0) {
|
|
254
|
+
throw new AddonError('account_failed', {
|
|
255
|
+
detail: `no account is bound to connection '${connection}'`,
|
|
256
|
+
accountCode: 'not_connected',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (accountId === undefined) {
|
|
260
|
+
if (accounts.length > 1) {
|
|
261
|
+
throw new AddonError('invalid_params', {
|
|
262
|
+
detail: `accountId is required: connection '${connection}' has ${accounts.length} bindings`,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return accounts[0];
|
|
266
|
+
}
|
|
267
|
+
const found = accounts.find((account) => account.accountId === accountId);
|
|
268
|
+
if (!found) {
|
|
269
|
+
throw new AddonError('account_failed', {
|
|
270
|
+
detail: `account '${accountId}' is not bound to '${connection}'`,
|
|
271
|
+
accountCode: 'not_bound',
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return found;
|
|
275
|
+
}
|
|
276
|
+
return { execute, hasGrant };
|
|
277
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { defineAddon } from "../define-addon.js";
|
|
2
|
+
export function validManifest() {
|
|
3
|
+
return {
|
|
4
|
+
id: 'harnesys.demo',
|
|
5
|
+
version: '1.0.0',
|
|
6
|
+
platform: 1,
|
|
7
|
+
title: 'Demo',
|
|
8
|
+
description: 'Demo addon',
|
|
9
|
+
runtime: { entry: './dist/index.js', profile: 'restricted' },
|
|
10
|
+
activation: 'lazy',
|
|
11
|
+
permissions: [
|
|
12
|
+
{ id: 'http.fetch', scope: { hosts: ['api.example.com'] }, reason: 'Calls the example API' },
|
|
13
|
+
],
|
|
14
|
+
connections: [
|
|
15
|
+
{
|
|
16
|
+
id: 'example',
|
|
17
|
+
label: 'Example connection',
|
|
18
|
+
accountKinds: ['example-account'],
|
|
19
|
+
cardinality: 'many',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
contributions: [
|
|
23
|
+
{
|
|
24
|
+
id: 'pack',
|
|
25
|
+
family: 'tool-pack',
|
|
26
|
+
descriptor: {
|
|
27
|
+
name: 'example',
|
|
28
|
+
title: 'Example tools',
|
|
29
|
+
description: 'Tools for the demo',
|
|
30
|
+
connection: 'example',
|
|
31
|
+
tools: [
|
|
32
|
+
{
|
|
33
|
+
name: 'example.ping',
|
|
34
|
+
description: 'Ping',
|
|
35
|
+
inputSchema: { type: 'object' },
|
|
36
|
+
effect: 'read',
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: 'hook',
|
|
43
|
+
family: 'hook',
|
|
44
|
+
descriptor: { id: 'onThread', on: { event: 'thread.*' } },
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export function demoDefinition() {
|
|
50
|
+
return defineAddon({
|
|
51
|
+
tools: {
|
|
52
|
+
'example.ping': (input, ctx) => ({ pong: true, echo: input, org: ctx.meta.correlationId }),
|
|
53
|
+
},
|
|
54
|
+
routes: {
|
|
55
|
+
items: async () => ({ status: 200, body: { rows: [] } }),
|
|
56
|
+
},
|
|
57
|
+
triggers: {
|
|
58
|
+
'telegram.feed': {
|
|
59
|
+
start: async () => undefined,
|
|
60
|
+
stop: async () => undefined,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
jobs: {
|
|
64
|
+
sync: async (_params, ctx) => {
|
|
65
|
+
await ctx.log.info('job ran');
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
hooks: {
|
|
69
|
+
onThread: async () => undefined,
|
|
70
|
+
},
|
|
71
|
+
sources: {
|
|
72
|
+
example: {
|
|
73
|
+
list: async () => ({ entries: [{ id: 'file-1', name: 'notes.txt', kind: 'file' }] }),
|
|
74
|
+
stat: async (params) => ({
|
|
75
|
+
entry: { id: params.fileId, name: 'notes.txt', kind: 'file' },
|
|
76
|
+
}),
|
|
77
|
+
download: async () => ({ data: '', eof: true }),
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
transport: {
|
|
81
|
+
typing: async () => undefined,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { FakeDb } from './fake-db.ts';
|
|
2
|
+
export { createFakeDb, guardSql, SqlAuthorizerError } from './fake-db.ts';
|
|
3
|
+
export type { FakeAccount, FakeGrant, FakeHost, FakeHostOptions, FakeHttpHandler, FakeHttpRequest, FakeHttpResponse, } from './fake-host.ts';
|
|
4
|
+
export { createFakeHost } from './fake-host.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@harnesys/sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Add-on SDK for Harnesys: manifest, contributions, JSON-RPC protocol, permissions, defineAddon, host client and a testing fake host.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"bun": "./src/index.ts",
|
|
13
|
+
"types": "./src/index.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./testing": {
|
|
18
|
+
"bun": "./src/testing/index.ts",
|
|
19
|
+
"types": "./src/testing/index.ts",
|
|
20
|
+
"import": "./dist/testing/index.js",
|
|
21
|
+
"default": "./dist/testing/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"!src/**/*.spec.ts",
|
|
28
|
+
"!src/**/__fixtures__",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"engines": {
|
|
34
|
+
"bun": ">=1.2.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/harnesys/harnesys.git",
|
|
42
|
+
"directory": "packages/sdk"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/harnesys/harnesys/tree/main/packages/sdk#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/harnesys/harnesys/issues"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
50
|
+
"pack": "rm -rf dist && tsc -p tsconfig.build.json && npm pack",
|
|
51
|
+
"typecheck": "tsc --noEmit"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"zod": "^4.4.3"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/bun": "^1.4.2",
|
|
58
|
+
"typescript": "^5.9.3"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"typescript": "^5.9.3"
|
|
62
|
+
},
|
|
63
|
+
"peerDependenciesMeta": {
|
|
64
|
+
"typescript": {
|
|
65
|
+
"optional": true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AddonConfig,
|
|
3
|
+
AddonContext,
|
|
4
|
+
AddonDb,
|
|
5
|
+
AddonEvents,
|
|
6
|
+
AddonFiles,
|
|
7
|
+
AddonHttp,
|
|
8
|
+
AddonLog,
|
|
9
|
+
AddonLogLevel,
|
|
10
|
+
AddonSecrets,
|
|
11
|
+
DbStatement,
|
|
12
|
+
DbWriteResult,
|
|
13
|
+
SqlParam,
|
|
14
|
+
} from './addon-context.ts';
|
|
15
|
+
import type { HostServiceClient } from './protocol/map.ts';
|
|
16
|
+
import type { CallMeta } from './protocol/meta.ts';
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
AddonAccounts,
|
|
20
|
+
AddonApprovals,
|
|
21
|
+
AddonBindings,
|
|
22
|
+
AddonBridge,
|
|
23
|
+
AddonConfig,
|
|
24
|
+
AddonContext,
|
|
25
|
+
AddonDb,
|
|
26
|
+
AddonEvents,
|
|
27
|
+
AddonFileChunk,
|
|
28
|
+
AddonFileStat,
|
|
29
|
+
AddonFiles,
|
|
30
|
+
AddonFileWriteResult,
|
|
31
|
+
AddonHttp,
|
|
32
|
+
AddonInboundAttachment,
|
|
33
|
+
AddonLog,
|
|
34
|
+
AddonLogLevel,
|
|
35
|
+
AddonOrg,
|
|
36
|
+
AddonProgress,
|
|
37
|
+
AddonRuns,
|
|
38
|
+
AddonSecrets,
|
|
39
|
+
AddonSkills,
|
|
40
|
+
AddonThreads,
|
|
41
|
+
AddonTriggerField,
|
|
42
|
+
AddonTriggers,
|
|
43
|
+
AddonTriggerTarget,
|
|
44
|
+
AddonUsage,
|
|
45
|
+
AddonWorkers,
|
|
46
|
+
DbRow,
|
|
47
|
+
DbStatement,
|
|
48
|
+
DbWriteResult,
|
|
49
|
+
SqlParam,
|
|
50
|
+
} from './addon-context.ts';
|
|
51
|
+
|
|
52
|
+
function createDb(client: HostServiceClient, _meta: CallMeta, signal: AbortSignal): AddonDb {
|
|
53
|
+
const track = async <T>(promise: Promise<T>): Promise<T> => {
|
|
54
|
+
if (signal.aborted) {
|
|
55
|
+
throw new Error('cancelled');
|
|
56
|
+
}
|
|
57
|
+
return promise;
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
exec: async (sql: string) => {
|
|
61
|
+
await track(client['db.exec']({ sql }, { signal }));
|
|
62
|
+
},
|
|
63
|
+
all: async (sql: string, params?: SqlParam[]) => {
|
|
64
|
+
const result = await track(client['db.all']({ sql, params }, { signal }));
|
|
65
|
+
return result.rows;
|
|
66
|
+
},
|
|
67
|
+
get: async (sql: string, params?: SqlParam[]) => {
|
|
68
|
+
const result = await track(client['db.get']({ sql, params }, { signal }));
|
|
69
|
+
return result.row;
|
|
70
|
+
},
|
|
71
|
+
run: async (sql: string, params?: SqlParam[]): Promise<DbWriteResult> => {
|
|
72
|
+
const result = await track(client['db.run']({ sql, params }, { signal }));
|
|
73
|
+
return { changes: result.changes, lastInsertRowid: result.lastInsertRowid };
|
|
74
|
+
},
|
|
75
|
+
batch: async (statements: DbStatement[]): Promise<DbWriteResult[]> => {
|
|
76
|
+
const result = await track(client['db.batch']({ statements }, { signal }));
|
|
77
|
+
return result.results.map((entry) => ({
|
|
78
|
+
changes: entry.changes,
|
|
79
|
+
lastInsertRowid: entry.lastInsertRowid,
|
|
80
|
+
}));
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function createLog(
|
|
86
|
+
client: HostServiceClient,
|
|
87
|
+
level: AddonLogLevel,
|
|
88
|
+
): (message: string, fields?: Record<string, unknown>) => Promise<void> {
|
|
89
|
+
return async (message, fields) => {
|
|
90
|
+
await client['log.write']({ level, message, fields });
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type ProgressNotify = (method: string, params: unknown) => void;
|
|
95
|
+
|
|
96
|
+
export function createAddonContext(
|
|
97
|
+
client: HostServiceClient,
|
|
98
|
+
meta: CallMeta,
|
|
99
|
+
signal: AbortSignal,
|
|
100
|
+
notify?: ProgressNotify,
|
|
101
|
+
): AddonContext {
|
|
102
|
+
const config: AddonConfig = {
|
|
103
|
+
get: async (keys?: string[]) => {
|
|
104
|
+
const result = await client['config.get']({ keys }, { signal });
|
|
105
|
+
return result.values;
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
const secrets: AddonSecrets = {
|
|
109
|
+
get: async (key: string) => {
|
|
110
|
+
const result = await client['secrets.get']({ key }, { signal });
|
|
111
|
+
return result.value;
|
|
112
|
+
},
|
|
113
|
+
set: async (key: string, value: string) => {
|
|
114
|
+
await client['secrets.set']({ key, value }, { signal });
|
|
115
|
+
},
|
|
116
|
+
delete: async (key: string) => {
|
|
117
|
+
await client['secrets.delete']({ key }, { signal });
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
const log: AddonLog = {
|
|
121
|
+
debug: createLog(client, 'debug'),
|
|
122
|
+
info: createLog(client, 'info'),
|
|
123
|
+
warn: createLog(client, 'warn'),
|
|
124
|
+
error: createLog(client, 'error'),
|
|
125
|
+
};
|
|
126
|
+
const events: AddonEvents = {
|
|
127
|
+
emit: async (name: string, payload: unknown) => {
|
|
128
|
+
await client['events.emit']({ name, payload }, { signal });
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
const http: AddonHttp = {
|
|
132
|
+
fetch: async (params) =>
|
|
133
|
+
client['http.fetch']({ ...params, method: params.method ?? 'GET' }, { signal }),
|
|
134
|
+
};
|
|
135
|
+
const files: AddonFiles = {
|
|
136
|
+
stat: async (path: string) => client['files.stat']({ path }, { signal }),
|
|
137
|
+
read: async (params) => client['files.read'](params, { signal }),
|
|
138
|
+
write: async (params) => client['files.write'](params, { signal }),
|
|
139
|
+
};
|
|
140
|
+
const bindings = {
|
|
141
|
+
channels: async (provider?: string) => {
|
|
142
|
+
const result = await client['bindings.channels']({ provider }, { signal });
|
|
143
|
+
return result.channels;
|
|
144
|
+
},
|
|
145
|
+
bind: async (params: { provider: string; channel: string; threadId: string }) =>
|
|
146
|
+
client['bindings.bind'](params, { signal }),
|
|
147
|
+
unbind: async (params: { provider: string; channel: string }) =>
|
|
148
|
+
client['bindings.unbind'](params, { signal }),
|
|
149
|
+
};
|
|
150
|
+
const accounts = {
|
|
151
|
+
list: async (connection: string) => {
|
|
152
|
+
const result = await client['accounts.list']({ connection }, { signal });
|
|
153
|
+
return result.accounts;
|
|
154
|
+
},
|
|
155
|
+
report: async (connection: string, code: string, accountId?: string) => {
|
|
156
|
+
await client['accounts.report']({ connection, code, accountId }, { signal });
|
|
157
|
+
},
|
|
158
|
+
access: async (connection: string, accountId?: string) => {
|
|
159
|
+
const result = await client['accounts.access']({ connection, accountId }, { signal });
|
|
160
|
+
return { credentials: result.credentials, expiresAt: result.expiresAt, app: result.app };
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
meta,
|
|
165
|
+
signal,
|
|
166
|
+
config,
|
|
167
|
+
secrets,
|
|
168
|
+
db: createDb(client, meta, signal),
|
|
169
|
+
log,
|
|
170
|
+
events,
|
|
171
|
+
http,
|
|
172
|
+
files,
|
|
173
|
+
accounts,
|
|
174
|
+
bindings,
|
|
175
|
+
runs: {
|
|
176
|
+
dispatch: async (params) => client['runs.dispatch'](params, { signal }),
|
|
177
|
+
cancel: async (params) => client['runs.cancel'](params, { signal }),
|
|
178
|
+
list: async (params) => (await client['runs.list'](params, { signal })).runs,
|
|
179
|
+
},
|
|
180
|
+
threads: {
|
|
181
|
+
create: async (params: {
|
|
182
|
+
title?: string;
|
|
183
|
+
agentId: string;
|
|
184
|
+
kind?: 'chat' | 'group';
|
|
185
|
+
responderMode?: 'auto' | 'mention';
|
|
186
|
+
}) => client['threads.create'](params, { signal }),
|
|
187
|
+
get: async (params: { threadId: string }) =>
|
|
188
|
+
(await client['threads.get'](params, { signal })).thread ?? null,
|
|
189
|
+
patch: async (params: { threadId: string; responderMode?: 'auto' | 'mention' }) => {
|
|
190
|
+
await client['threads.patch'](params, { signal });
|
|
191
|
+
},
|
|
192
|
+
compact: async (params: { threadId: string }) =>
|
|
193
|
+
client['threads.compact'](params, { signal }),
|
|
194
|
+
},
|
|
195
|
+
agents: {
|
|
196
|
+
list: async () => (await client['agents.list']({}, { signal })).agents,
|
|
197
|
+
},
|
|
198
|
+
usage: {
|
|
199
|
+
summary: async (params: { threadId?: string; since?: string }) =>
|
|
200
|
+
client['usage.summary'](params, { signal }),
|
|
201
|
+
},
|
|
202
|
+
skills: {
|
|
203
|
+
list: async (params: { threadId?: string }) =>
|
|
204
|
+
(await client['skills.list'](params, { signal })).skills,
|
|
205
|
+
},
|
|
206
|
+
triggers: {
|
|
207
|
+
fire: async (params) => client['triggers.fire'](params, { signal }),
|
|
208
|
+
create: async (params) => client['triggers.create'](params, { signal }),
|
|
209
|
+
update: async (params) => client['triggers.update'](params, { signal }),
|
|
210
|
+
},
|
|
211
|
+
bridge: {
|
|
212
|
+
inbound: async (message) => (await client['bridge.inbound']({ message }, { signal })).outcome,
|
|
213
|
+
},
|
|
214
|
+
progress: {
|
|
215
|
+
progress: (token: string, value: unknown) => {
|
|
216
|
+
notify?.('$/progress', { token, value });
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
workers: {
|
|
220
|
+
report: async (workerId: string, state: 'running' | 'stopped' | 'failed', code?: string) => {
|
|
221
|
+
await client['workers.report']({ workerId, state, code }, { signal });
|
|
222
|
+
},
|
|
223
|
+
lease: async (workerId: string, ttlMs?: number) =>
|
|
224
|
+
client['workers.lease']({ workerId, ttlMs }, { signal }),
|
|
225
|
+
release: async (workerId: string) => {
|
|
226
|
+
await client['workers.release']({ workerId }, { signal });
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
org: {
|
|
230
|
+
settings: async () => client['org.settings.get']({}, { signal }),
|
|
231
|
+
},
|
|
232
|
+
approvals: {
|
|
233
|
+
request: async (params) => client['approvals.request'](params, { signal }),
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|