@harnesys/sdk 0.0.0-stage → 1.1.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-value.d.ts +12 -0
- package/dist/families/json-schema-value.js +243 -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 +36 -0
- package/dist/index.js +36 -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 +217 -0
- package/dist/testing/fake-services.d.ts +63 -0
- package/dist/testing/fake-services.js +277 -0
- package/dist/testing/fixtures.d.ts +5 -0
- package/dist/testing/fixtures.js +101 -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-value.ts +322 -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 +345 -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 +350 -0
- package/src/testing/fake-services.ts +345 -0
- package/src/testing/fixtures.ts +106 -0
- package/src/testing/index.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Harnesys
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,81 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @harnesys/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Leaf package for addon authors. Implements the addon platform spec (`docs/ADDONS_PLAN.md`): manifest, contributions, protocol, permissions, `defineAddon`, the host client, and the testing fake host. The package imports nothing from the core (`harnesys`, `@harnesys/contracts`, `apps/server`) — this is gate §16.3.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @harnesys/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Version `1.0.0` tracks the addon platform contract (`platform: 1`); `1.1.0` adds tool schema validation to the fake host. The SDK ships TypeScript sources for Bun and compiled ESM plus declarations in `dist/`; see [Распространение SDK](../../docs/add-ons/sdk.md#распространение-sdk) for the export modes and the consumer `tsconfig` shape.
|
|
12
|
+
|
|
13
|
+
## Layout
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
src/
|
|
17
|
+
manifest/ AddonManifest, Contribution, ConnectionRequirement, L10n, JSON Schema publication, validator
|
|
18
|
+
protocol/ JSON-RPC envelope, CallMeta, errors, handshake, method map + derived types, golden fixtures
|
|
19
|
+
families/ descriptor schemas for all 13 families + FieldSpec + JSON-Schema subset (§6.5)
|
|
20
|
+
permissions/ PermissionRequest, baseline and known permission ids, EffectivePermission
|
|
21
|
+
addon-context.ts AddonContext interface (typed host client surface for handlers)
|
|
22
|
+
addon-context-factory.ts createAddonContext over a HostChannel
|
|
23
|
+
host-client.ts HostChannel + createHostClient (validates results via protocol schemas)
|
|
24
|
+
define-addon.ts defineAddon() + handlers derivation
|
|
25
|
+
runtime.ts in-process runtime: dispatch host→addon calls, deadlines, cancellation
|
|
26
|
+
process-entry.ts NDJSON stdio framing (8 MiB cap, UTF-8), initialize handshake, $/cancel
|
|
27
|
+
testing/ createFakeHost, in-memory db with authorizer, fixtures
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { defineAddon } from '@harnesys/sdk';
|
|
34
|
+
|
|
35
|
+
export const addon = defineAddon({
|
|
36
|
+
tools: { 'github.list_repos': async (input, ctx) => ctx.http.fetch({ url: 'https://api.github.com/user/repos', auth: { connection: 'github', scheme: 'bearer' } }) },
|
|
37
|
+
triggers: { 'telegram.feed': { start: async (params, ctx) => {}, stop: async (params, ctx) => {} } },
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`defineAddon` is a **named export** (the repo lint forbids default exports); addons use `import { defineAddon } from '@harnesys/sdk'` instead of the plan's `export default` example.
|
|
42
|
+
|
|
43
|
+
Entry point for a process addon:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { startAddonProcess } from '@harnesys/sdk';
|
|
47
|
+
|
|
48
|
+
await startAddonProcess({ definition: addon, addon: { id: 'harnesys.demo', version: '1.0.0' } });
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Testing without the host:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { createFakeHost } from '@harnesys/sdk/testing';
|
|
55
|
+
|
|
56
|
+
const host = createFakeHost({ definition: addon, manifest, config, accounts, http });
|
|
57
|
+
await host.callTool('github', 'github.list_repos', {});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Protocol method map
|
|
61
|
+
|
|
62
|
+
Single source in `src/protocol/map.ts`. Every entry: `{ direction, params, result, permission }` on Zod; client/dispatcher/handler types are derived from the map (`HostServiceClient`, `AddonDispatcher`, `AddonHandlerOf`) — a mismatch is a compile error.
|
|
63
|
+
|
|
64
|
+
- host→addon: `initialize`, `shutdown`, `ping`, `lifecycle.upgrade`, `tools.call`, `route.handle`, `trigger.start`, `trigger.stop`, `job.run`, `worker.start`, `worker.stop`, `transport.send`, `transport.edit`, `transport.typing`, `hook.deliver`
|
|
65
|
+
- host→addon notifications: `initialized`, `config.changed`, `permissions.changed`, `accounts.changed`
|
|
66
|
+
- addon→host: `config.get`, `secrets.get`, `db.exec/all/get/run/batch`, `log.write`, `events.emit`, `http.fetch`, `accounts.list/report/access`, `runs.dispatch`, `threads.create`, `triggers.fire`, `bridge.inbound`, `workers.report`
|
|
67
|
+
- notifications (both/addon): `$/cancel` (both), `$/progress` (addon)
|
|
68
|
+
|
|
69
|
+
Permissions: `null` = baseline; otherwise one of `http.fetch`, `accounts.use`, `accounts.credentials`, `runs.dispatch`, `threads.create`, `bridge.inbound`, `trigger`, `worker` (the last two are contribution-derived, see §6.4 "через вклад").
|
|
70
|
+
|
|
71
|
+
## Families
|
|
72
|
+
|
|
73
|
+
`tool-pack`, `trigger`, `job`, `worker`, `route`, `transport`, `hook`, `content`, `config`, `view`, `source-kind`, `mcp`, `lsp` — each with a descriptor interface, Zod schema and schema version (`*_SCHEMA_VERSION = 1`). Tool/route input schemas use the JSON-Schema subset of §6.5 (`src/families/json-schema.ts`): depth ≤ 8, size ≤ 64 KiB, local `$ref` only.
|
|
74
|
+
|
|
75
|
+
## Deviations from the plan
|
|
76
|
+
|
|
77
|
+
- `defineAddon` is a named export (repo lint forbids default exports).
|
|
78
|
+
- `triggers.fire` / `workers.report` use contribution-derived permission ids `trigger` / `worker` — the plan gates them "through a contribution", the map needs a concrete id string.
|
|
79
|
+
- `mcp` / `lsp` / `source-kind` descriptors are minimal v1 stubs (the plan does not define their shapes yet).
|
|
80
|
+
- `AddonErrorData` carries `accountCode?: string` for `account_failed` (plan: AccountErrorCode in `data`).
|
|
81
|
+
- `zod` v4 `z.toJSONSchema` is used for JSON-Schema publication (§4.3).
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AddonContext } from './addon-context.ts';
|
|
2
|
+
import type { HostServiceClient } from './protocol/map.ts';
|
|
3
|
+
import type { CallMeta } from './protocol/meta.ts';
|
|
4
|
+
export type { AddonAccounts, AddonApprovals, AddonBindings, AddonBridge, AddonConfig, AddonContext, AddonDb, AddonEvents, AddonFileChunk, AddonFileStat, AddonFiles, AddonFileWriteResult, AddonHttp, AddonInboundAttachment, AddonLog, AddonLogLevel, AddonOrg, AddonProgress, AddonRuns, AddonSecrets, AddonSkills, AddonThreads, AddonTriggerField, AddonTriggers, AddonTriggerTarget, AddonUsage, AddonWorkers, DbRow, DbStatement, DbWriteResult, SqlParam, } from './addon-context.ts';
|
|
5
|
+
export type ProgressNotify = (method: string, params: unknown) => void;
|
|
6
|
+
export declare function createAddonContext(client: HostServiceClient, meta: CallMeta, signal: AbortSignal, notify?: ProgressNotify): AddonContext;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
function createDb(client, _meta, signal) {
|
|
2
|
+
const track = async (promise) => {
|
|
3
|
+
if (signal.aborted) {
|
|
4
|
+
throw new Error('cancelled');
|
|
5
|
+
}
|
|
6
|
+
return promise;
|
|
7
|
+
};
|
|
8
|
+
return {
|
|
9
|
+
exec: async (sql) => {
|
|
10
|
+
await track(client['db.exec']({ sql }, { signal }));
|
|
11
|
+
},
|
|
12
|
+
all: async (sql, params) => {
|
|
13
|
+
const result = await track(client['db.all']({ sql, params }, { signal }));
|
|
14
|
+
return result.rows;
|
|
15
|
+
},
|
|
16
|
+
get: async (sql, params) => {
|
|
17
|
+
const result = await track(client['db.get']({ sql, params }, { signal }));
|
|
18
|
+
return result.row;
|
|
19
|
+
},
|
|
20
|
+
run: async (sql, params) => {
|
|
21
|
+
const result = await track(client['db.run']({ sql, params }, { signal }));
|
|
22
|
+
return { changes: result.changes, lastInsertRowid: result.lastInsertRowid };
|
|
23
|
+
},
|
|
24
|
+
batch: async (statements) => {
|
|
25
|
+
const result = await track(client['db.batch']({ statements }, { signal }));
|
|
26
|
+
return result.results.map((entry) => ({
|
|
27
|
+
changes: entry.changes,
|
|
28
|
+
lastInsertRowid: entry.lastInsertRowid,
|
|
29
|
+
}));
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function createLog(client, level) {
|
|
34
|
+
return async (message, fields) => {
|
|
35
|
+
await client['log.write']({ level, message, fields });
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function createAddonContext(client, meta, signal, notify) {
|
|
39
|
+
const config = {
|
|
40
|
+
get: async (keys) => {
|
|
41
|
+
const result = await client['config.get']({ keys }, { signal });
|
|
42
|
+
return result.values;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const secrets = {
|
|
46
|
+
get: async (key) => {
|
|
47
|
+
const result = await client['secrets.get']({ key }, { signal });
|
|
48
|
+
return result.value;
|
|
49
|
+
},
|
|
50
|
+
set: async (key, value) => {
|
|
51
|
+
await client['secrets.set']({ key, value }, { signal });
|
|
52
|
+
},
|
|
53
|
+
delete: async (key) => {
|
|
54
|
+
await client['secrets.delete']({ key }, { signal });
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
const log = {
|
|
58
|
+
debug: createLog(client, 'debug'),
|
|
59
|
+
info: createLog(client, 'info'),
|
|
60
|
+
warn: createLog(client, 'warn'),
|
|
61
|
+
error: createLog(client, 'error'),
|
|
62
|
+
};
|
|
63
|
+
const events = {
|
|
64
|
+
emit: async (name, payload) => {
|
|
65
|
+
await client['events.emit']({ name, payload }, { signal });
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
const http = {
|
|
69
|
+
fetch: async (params) => client['http.fetch']({ ...params, method: params.method ?? 'GET' }, { signal }),
|
|
70
|
+
};
|
|
71
|
+
const files = {
|
|
72
|
+
stat: async (path) => client['files.stat']({ path }, { signal }),
|
|
73
|
+
read: async (params) => client['files.read'](params, { signal }),
|
|
74
|
+
write: async (params) => client['files.write'](params, { signal }),
|
|
75
|
+
};
|
|
76
|
+
const bindings = {
|
|
77
|
+
channels: async (provider) => {
|
|
78
|
+
const result = await client['bindings.channels']({ provider }, { signal });
|
|
79
|
+
return result.channels;
|
|
80
|
+
},
|
|
81
|
+
bind: async (params) => client['bindings.bind'](params, { signal }),
|
|
82
|
+
unbind: async (params) => client['bindings.unbind'](params, { signal }),
|
|
83
|
+
};
|
|
84
|
+
const accounts = {
|
|
85
|
+
list: async (connection) => {
|
|
86
|
+
const result = await client['accounts.list']({ connection }, { signal });
|
|
87
|
+
return result.accounts;
|
|
88
|
+
},
|
|
89
|
+
report: async (connection, code, accountId) => {
|
|
90
|
+
await client['accounts.report']({ connection, code, accountId }, { signal });
|
|
91
|
+
},
|
|
92
|
+
access: async (connection, accountId) => {
|
|
93
|
+
const result = await client['accounts.access']({ connection, accountId }, { signal });
|
|
94
|
+
return { credentials: result.credentials, expiresAt: result.expiresAt, app: result.app };
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
meta,
|
|
99
|
+
signal,
|
|
100
|
+
config,
|
|
101
|
+
secrets,
|
|
102
|
+
db: createDb(client, meta, signal),
|
|
103
|
+
log,
|
|
104
|
+
events,
|
|
105
|
+
http,
|
|
106
|
+
files,
|
|
107
|
+
accounts,
|
|
108
|
+
bindings,
|
|
109
|
+
runs: {
|
|
110
|
+
dispatch: async (params) => client['runs.dispatch'](params, { signal }),
|
|
111
|
+
cancel: async (params) => client['runs.cancel'](params, { signal }),
|
|
112
|
+
list: async (params) => (await client['runs.list'](params, { signal })).runs,
|
|
113
|
+
},
|
|
114
|
+
threads: {
|
|
115
|
+
create: async (params) => client['threads.create'](params, { signal }),
|
|
116
|
+
get: async (params) => (await client['threads.get'](params, { signal })).thread ?? null,
|
|
117
|
+
patch: async (params) => {
|
|
118
|
+
await client['threads.patch'](params, { signal });
|
|
119
|
+
},
|
|
120
|
+
compact: async (params) => client['threads.compact'](params, { signal }),
|
|
121
|
+
},
|
|
122
|
+
agents: {
|
|
123
|
+
list: async () => (await client['agents.list']({}, { signal })).agents,
|
|
124
|
+
},
|
|
125
|
+
usage: {
|
|
126
|
+
summary: async (params) => client['usage.summary'](params, { signal }),
|
|
127
|
+
},
|
|
128
|
+
skills: {
|
|
129
|
+
list: async (params) => (await client['skills.list'](params, { signal })).skills,
|
|
130
|
+
},
|
|
131
|
+
triggers: {
|
|
132
|
+
fire: async (params) => client['triggers.fire'](params, { signal }),
|
|
133
|
+
create: async (params) => client['triggers.create'](params, { signal }),
|
|
134
|
+
update: async (params) => client['triggers.update'](params, { signal }),
|
|
135
|
+
},
|
|
136
|
+
bridge: {
|
|
137
|
+
inbound: async (message) => (await client['bridge.inbound']({ message }, { signal })).outcome,
|
|
138
|
+
},
|
|
139
|
+
progress: {
|
|
140
|
+
progress: (token, value) => {
|
|
141
|
+
notify?.('$/progress', { token, value });
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
workers: {
|
|
145
|
+
report: async (workerId, state, code) => {
|
|
146
|
+
await client['workers.report']({ workerId, state, code }, { signal });
|
|
147
|
+
},
|
|
148
|
+
lease: async (workerId, ttlMs) => client['workers.lease']({ workerId, ttlMs }, { signal }),
|
|
149
|
+
release: async (workerId) => {
|
|
150
|
+
await client['workers.release']({ workerId }, { signal });
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
org: {
|
|
154
|
+
settings: async () => client['org.settings.get']({}, { signal }),
|
|
155
|
+
},
|
|
156
|
+
approvals: {
|
|
157
|
+
request: async (params) => client['approvals.request'](params, { signal }),
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import type { CallMeta } from './protocol/meta.ts';
|
|
2
|
+
import type { AppCredentials, BridgeInboundOutcome } from './protocol/shared.ts';
|
|
3
|
+
export type DbRow = Record<string, unknown>;
|
|
4
|
+
export type SqlParam = string | number | boolean | null;
|
|
5
|
+
export interface DbStatement {
|
|
6
|
+
sql: string;
|
|
7
|
+
params?: SqlParam[];
|
|
8
|
+
}
|
|
9
|
+
export interface DbWriteResult {
|
|
10
|
+
changes: number;
|
|
11
|
+
lastInsertRowid: string | number;
|
|
12
|
+
}
|
|
13
|
+
export interface AddonDb {
|
|
14
|
+
exec(sql: string): Promise<void>;
|
|
15
|
+
all(sql: string, params?: SqlParam[]): Promise<DbRow[]>;
|
|
16
|
+
get(sql: string, params?: SqlParam[]): Promise<DbRow | null>;
|
|
17
|
+
run(sql: string, params?: SqlParam[]): Promise<DbWriteResult>;
|
|
18
|
+
batch(statements: DbStatement[]): Promise<DbWriteResult[]>;
|
|
19
|
+
}
|
|
20
|
+
export interface AddonSecrets {
|
|
21
|
+
get(key: string): Promise<string | null>;
|
|
22
|
+
set(key: string, value: string): Promise<void>;
|
|
23
|
+
delete(key: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export interface AddonConfig {
|
|
26
|
+
get(keys?: string[]): Promise<Record<string, unknown>>;
|
|
27
|
+
}
|
|
28
|
+
export type AddonLogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
29
|
+
export interface AddonLog {
|
|
30
|
+
debug(message: string, fields?: Record<string, unknown>): Promise<void>;
|
|
31
|
+
info(message: string, fields?: Record<string, unknown>): Promise<void>;
|
|
32
|
+
warn(message: string, fields?: Record<string, unknown>): Promise<void>;
|
|
33
|
+
error(message: string, fields?: Record<string, unknown>): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export interface AddonEvents {
|
|
36
|
+
emit(name: string, payload: unknown): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
export interface AddonHttp {
|
|
39
|
+
fetch(params: {
|
|
40
|
+
url: string;
|
|
41
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD';
|
|
42
|
+
headers?: Record<string, string>;
|
|
43
|
+
body?: string;
|
|
44
|
+
bodyBase64?: string;
|
|
45
|
+
responseEncoding?: 'text' | 'base64';
|
|
46
|
+
auth?: {
|
|
47
|
+
connection: string;
|
|
48
|
+
accountId?: string;
|
|
49
|
+
scheme: 'bearer' | 'basic' | 'header' | 'path';
|
|
50
|
+
credentialKey?: string;
|
|
51
|
+
header?: string;
|
|
52
|
+
placeholder?: string;
|
|
53
|
+
};
|
|
54
|
+
}): Promise<{
|
|
55
|
+
status: number;
|
|
56
|
+
headers: Record<string, string>;
|
|
57
|
+
body: string | null;
|
|
58
|
+
bodyBase64?: string;
|
|
59
|
+
}>;
|
|
60
|
+
}
|
|
61
|
+
export interface AddonFileStat {
|
|
62
|
+
path: string;
|
|
63
|
+
name: string;
|
|
64
|
+
size: number;
|
|
65
|
+
mediaType: string;
|
|
66
|
+
modifiedAt: string;
|
|
67
|
+
}
|
|
68
|
+
export interface AddonFileChunk {
|
|
69
|
+
data: string;
|
|
70
|
+
eof: boolean;
|
|
71
|
+
size: number;
|
|
72
|
+
mediaType: string;
|
|
73
|
+
}
|
|
74
|
+
export interface AddonFileWriteResult {
|
|
75
|
+
path: string;
|
|
76
|
+
size: number;
|
|
77
|
+
}
|
|
78
|
+
export interface AddonFiles {
|
|
79
|
+
stat(path: string): Promise<AddonFileStat>;
|
|
80
|
+
read(params: {
|
|
81
|
+
path: string;
|
|
82
|
+
offset?: number;
|
|
83
|
+
length?: number;
|
|
84
|
+
}): Promise<AddonFileChunk>;
|
|
85
|
+
write(params: {
|
|
86
|
+
path: string;
|
|
87
|
+
data: string;
|
|
88
|
+
append?: boolean;
|
|
89
|
+
}): Promise<AddonFileWriteResult>;
|
|
90
|
+
}
|
|
91
|
+
export interface AddonBindings {
|
|
92
|
+
channels(provider?: string): Promise<Array<{
|
|
93
|
+
provider: string;
|
|
94
|
+
channel: string;
|
|
95
|
+
threadId: string;
|
|
96
|
+
createdAt: string;
|
|
97
|
+
}>>;
|
|
98
|
+
bind(params: {
|
|
99
|
+
provider: string;
|
|
100
|
+
channel: string;
|
|
101
|
+
threadId: string;
|
|
102
|
+
}): Promise<{
|
|
103
|
+
ok: true;
|
|
104
|
+
}>;
|
|
105
|
+
unbind(params: {
|
|
106
|
+
provider: string;
|
|
107
|
+
channel: string;
|
|
108
|
+
}): Promise<{
|
|
109
|
+
ok: true;
|
|
110
|
+
}>;
|
|
111
|
+
}
|
|
112
|
+
export interface AddonAccounts {
|
|
113
|
+
list(connection: string): Promise<Array<{
|
|
114
|
+
accountId: string;
|
|
115
|
+
label: string;
|
|
116
|
+
externalAccountId: string;
|
|
117
|
+
status: string;
|
|
118
|
+
}>>;
|
|
119
|
+
report(connection: string, code: string, accountId?: string): Promise<void>;
|
|
120
|
+
access(connection: string, accountId?: string): Promise<{
|
|
121
|
+
credentials: unknown;
|
|
122
|
+
expiresAt?: string;
|
|
123
|
+
app?: AppCredentials | null;
|
|
124
|
+
}>;
|
|
125
|
+
}
|
|
126
|
+
export interface AddonRuns {
|
|
127
|
+
dispatch(params: {
|
|
128
|
+
threadId?: string;
|
|
129
|
+
agentId?: string;
|
|
130
|
+
input: string;
|
|
131
|
+
}): Promise<{
|
|
132
|
+
runId: string;
|
|
133
|
+
}>;
|
|
134
|
+
cancel(params: {
|
|
135
|
+
threadId: string;
|
|
136
|
+
runId: string;
|
|
137
|
+
}): Promise<{
|
|
138
|
+
ok: true;
|
|
139
|
+
}>;
|
|
140
|
+
list(params: {
|
|
141
|
+
threadId: string;
|
|
142
|
+
status?: string;
|
|
143
|
+
}): Promise<Array<{
|
|
144
|
+
runId: string;
|
|
145
|
+
status: string;
|
|
146
|
+
startedAt: string | null;
|
|
147
|
+
agentId: string | null;
|
|
148
|
+
}>>;
|
|
149
|
+
}
|
|
150
|
+
export interface AddonThreadView {
|
|
151
|
+
threadId: string;
|
|
152
|
+
kind: string;
|
|
153
|
+
title: string;
|
|
154
|
+
responderMode: 'auto' | 'mention';
|
|
155
|
+
autoReplyAgentId: string | null;
|
|
156
|
+
agents: Array<{
|
|
157
|
+
id: string;
|
|
158
|
+
name: string;
|
|
159
|
+
}>;
|
|
160
|
+
}
|
|
161
|
+
export interface AddonThreads {
|
|
162
|
+
create(params: {
|
|
163
|
+
title?: string;
|
|
164
|
+
agentId: string;
|
|
165
|
+
kind?: 'chat' | 'group';
|
|
166
|
+
responderMode?: 'auto' | 'mention';
|
|
167
|
+
}): Promise<{
|
|
168
|
+
threadId: string;
|
|
169
|
+
}>;
|
|
170
|
+
get(params: {
|
|
171
|
+
threadId: string;
|
|
172
|
+
}): Promise<AddonThreadView | null>;
|
|
173
|
+
patch(params: {
|
|
174
|
+
threadId: string;
|
|
175
|
+
responderMode?: 'auto' | 'mention';
|
|
176
|
+
}): Promise<void>;
|
|
177
|
+
compact(params: {
|
|
178
|
+
threadId: string;
|
|
179
|
+
}): Promise<{
|
|
180
|
+
compacted: true;
|
|
181
|
+
id: string;
|
|
182
|
+
coveredFrom: number;
|
|
183
|
+
coveredUntil: number;
|
|
184
|
+
tokensBefore: number;
|
|
185
|
+
tokensAfter: number;
|
|
186
|
+
} | {
|
|
187
|
+
compacted: false;
|
|
188
|
+
reason?: string;
|
|
189
|
+
}>;
|
|
190
|
+
}
|
|
191
|
+
export interface AddonAgentView {
|
|
192
|
+
id: string;
|
|
193
|
+
name: string;
|
|
194
|
+
status?: string;
|
|
195
|
+
}
|
|
196
|
+
export interface AddonAgents {
|
|
197
|
+
list(): Promise<AddonAgentView[]>;
|
|
198
|
+
}
|
|
199
|
+
export interface AddonUsage {
|
|
200
|
+
summary(params: {
|
|
201
|
+
threadId?: string;
|
|
202
|
+
since?: string;
|
|
203
|
+
}): Promise<{
|
|
204
|
+
inputTokens: number;
|
|
205
|
+
outputTokens: number;
|
|
206
|
+
costUsd: number;
|
|
207
|
+
byModel: Array<{
|
|
208
|
+
model: string;
|
|
209
|
+
inputTokens: number;
|
|
210
|
+
outputTokens: number;
|
|
211
|
+
costUsd: number;
|
|
212
|
+
}>;
|
|
213
|
+
}>;
|
|
214
|
+
}
|
|
215
|
+
export interface AddonSkills {
|
|
216
|
+
list(params: {
|
|
217
|
+
threadId?: string;
|
|
218
|
+
}): Promise<Array<{
|
|
219
|
+
id: string;
|
|
220
|
+
name: string;
|
|
221
|
+
description: string | null;
|
|
222
|
+
source: string;
|
|
223
|
+
}>>;
|
|
224
|
+
}
|
|
225
|
+
export interface AddonTriggerTarget {
|
|
226
|
+
threadId: string;
|
|
227
|
+
agentId: string;
|
|
228
|
+
modeId?: string;
|
|
229
|
+
}
|
|
230
|
+
export interface AddonTriggerField {
|
|
231
|
+
label: string;
|
|
232
|
+
value: string;
|
|
233
|
+
href?: string;
|
|
234
|
+
}
|
|
235
|
+
export interface AddonTriggers {
|
|
236
|
+
fire(params: {
|
|
237
|
+
instanceId: string;
|
|
238
|
+
runKey: string;
|
|
239
|
+
text?: string;
|
|
240
|
+
title?: string;
|
|
241
|
+
fields?: AddonTriggerField[];
|
|
242
|
+
payload?: unknown;
|
|
243
|
+
}): Promise<{
|
|
244
|
+
outcome: 'fired' | 'suppressed' | 'busy' | 'failed';
|
|
245
|
+
runId?: string | null;
|
|
246
|
+
}>;
|
|
247
|
+
create(params: {
|
|
248
|
+
kind: string;
|
|
249
|
+
label: string;
|
|
250
|
+
config: Record<string, unknown>;
|
|
251
|
+
target: AddonTriggerTarget;
|
|
252
|
+
instructions?: string;
|
|
253
|
+
}): Promise<{
|
|
254
|
+
instanceId: string;
|
|
255
|
+
}>;
|
|
256
|
+
update(params: {
|
|
257
|
+
instanceId: string;
|
|
258
|
+
label?: string;
|
|
259
|
+
config?: Record<string, unknown>;
|
|
260
|
+
target?: AddonTriggerTarget;
|
|
261
|
+
instructions?: string;
|
|
262
|
+
enabled?: boolean;
|
|
263
|
+
}): Promise<{
|
|
264
|
+
instanceId: string;
|
|
265
|
+
}>;
|
|
266
|
+
}
|
|
267
|
+
export interface AddonOrg {
|
|
268
|
+
settings(): Promise<{
|
|
269
|
+
timezone: string;
|
|
270
|
+
}>;
|
|
271
|
+
}
|
|
272
|
+
export interface AddonApprovals {
|
|
273
|
+
request(params: {
|
|
274
|
+
interruptId: string;
|
|
275
|
+
agentId: string;
|
|
276
|
+
threadId?: string;
|
|
277
|
+
kind: 'tool' | 'spend' | 'memory' | 'question' | 'file_access';
|
|
278
|
+
question: string;
|
|
279
|
+
options: Array<'approve' | 'approve_once' | 'deny' | 'block_path' | 'close'>;
|
|
280
|
+
context: {
|
|
281
|
+
source: string;
|
|
282
|
+
ops?: string[];
|
|
283
|
+
tool?: {
|
|
284
|
+
name: string;
|
|
285
|
+
inputPreview?: string;
|
|
286
|
+
command?: string;
|
|
287
|
+
truncated?: boolean;
|
|
288
|
+
};
|
|
289
|
+
note?: string;
|
|
290
|
+
};
|
|
291
|
+
}): Promise<{
|
|
292
|
+
id: string;
|
|
293
|
+
}>;
|
|
294
|
+
}
|
|
295
|
+
export interface AddonInboundAttachment {
|
|
296
|
+
name: string;
|
|
297
|
+
mediaType?: string;
|
|
298
|
+
path?: string;
|
|
299
|
+
dataBase64?: string;
|
|
300
|
+
}
|
|
301
|
+
export interface AddonProgress {
|
|
302
|
+
progress(token: string, value: unknown): void;
|
|
303
|
+
}
|
|
304
|
+
export interface AddonBridge {
|
|
305
|
+
inbound(message: {
|
|
306
|
+
channel: string;
|
|
307
|
+
from?: string;
|
|
308
|
+
text: string;
|
|
309
|
+
payload?: unknown;
|
|
310
|
+
attachments?: AddonInboundAttachment[];
|
|
311
|
+
}): Promise<BridgeInboundOutcome>;
|
|
312
|
+
}
|
|
313
|
+
export interface AddonWorkerLease {
|
|
314
|
+
held: boolean;
|
|
315
|
+
epoch: number;
|
|
316
|
+
expiresAt: string | null;
|
|
317
|
+
}
|
|
318
|
+
export interface AddonWorkers {
|
|
319
|
+
report(workerId: string, state: 'running' | 'stopped' | 'failed', code?: string): Promise<void>;
|
|
320
|
+
lease(workerId: string, ttlMs?: number): Promise<AddonWorkerLease>;
|
|
321
|
+
release(workerId: string): Promise<void>;
|
|
322
|
+
}
|
|
323
|
+
export interface AddonContext {
|
|
324
|
+
readonly meta: CallMeta;
|
|
325
|
+
readonly signal: AbortSignal;
|
|
326
|
+
readonly config: AddonConfig;
|
|
327
|
+
readonly secrets: AddonSecrets;
|
|
328
|
+
readonly db: AddonDb;
|
|
329
|
+
readonly log: AddonLog;
|
|
330
|
+
readonly events: AddonEvents;
|
|
331
|
+
readonly http: AddonHttp;
|
|
332
|
+
readonly files: AddonFiles;
|
|
333
|
+
readonly accounts: AddonAccounts;
|
|
334
|
+
readonly bindings: AddonBindings;
|
|
335
|
+
readonly runs: AddonRuns;
|
|
336
|
+
readonly threads: AddonThreads;
|
|
337
|
+
readonly agents: AddonAgents;
|
|
338
|
+
readonly usage: AddonUsage;
|
|
339
|
+
readonly skills: AddonSkills;
|
|
340
|
+
readonly triggers: AddonTriggers;
|
|
341
|
+
readonly bridge: AddonBridge;
|
|
342
|
+
readonly workers: AddonWorkers;
|
|
343
|
+
readonly org: AddonOrg;
|
|
344
|
+
readonly approvals: AddonApprovals;
|
|
345
|
+
readonly progress: AddonProgress;
|
|
346
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { AddonContext } from './addon-context.ts';
|
|
2
|
+
import type { AddonHandlerOf, HostToAddonMethods, NotificationMethods, ParamsOf, ResultOf } from './protocol/map.ts';
|
|
3
|
+
type HostMethodParams<M extends keyof HostToAddonMethods> = ParamsOf<HostToAddonMethods[M]>;
|
|
4
|
+
type HostMethodResult<M extends keyof HostToAddonMethods> = ResultOf<HostToAddonMethods[M]>;
|
|
5
|
+
type NotificationParams<K extends keyof NotificationMethods> = ParamsOf<NotificationMethods[K]>;
|
|
6
|
+
export type ToolHandler = (input: Record<string, unknown>, ctx: AddonContext) => unknown | Promise<unknown>;
|
|
7
|
+
export type RouteHandler = (params: Omit<HostMethodParams<'route.handle'>, '_meta'>, ctx: AddonContext) => HostMethodResult<'route.handle'> | Promise<HostMethodResult<'route.handle'>>;
|
|
8
|
+
export interface TriggerPair {
|
|
9
|
+
start(params: HostMethodParams<'trigger.start'>, ctx: AddonContext): Promise<void> | void;
|
|
10
|
+
stop(params: HostMethodParams<'trigger.stop'>, ctx: AddonContext): Promise<void> | void;
|
|
11
|
+
}
|
|
12
|
+
export type JobHandler = (params: Omit<HostMethodParams<'job.run'>, '_meta'>, ctx: AddonContext) => Promise<void> | void;
|
|
13
|
+
export interface WorkerPair {
|
|
14
|
+
start(params: HostMethodParams<'worker.start'>, ctx: AddonContext): Promise<void> | void;
|
|
15
|
+
stop(params: HostMethodParams<'worker.stop'>, ctx: AddonContext): Promise<void> | void;
|
|
16
|
+
}
|
|
17
|
+
export type HookHandler = (params: Omit<HostMethodParams<'hook.deliver'>, '_meta'>, ctx: AddonContext) => Promise<void> | void;
|
|
18
|
+
export interface TransportHandlers {
|
|
19
|
+
send?(params: HostMethodParams<'transport.send'>, ctx: AddonContext): Promise<HostMethodResult<'transport.send'>> | HostMethodResult<'transport.send'>;
|
|
20
|
+
edit?(params: HostMethodParams<'transport.edit'>, ctx: AddonContext): Promise<void> | void;
|
|
21
|
+
typing?(params: HostMethodParams<'transport.typing'>, ctx: AddonContext): Promise<void> | void;
|
|
22
|
+
}
|
|
23
|
+
export interface SourceBackendHandlers {
|
|
24
|
+
list(params: Omit<HostMethodParams<'source.list'>, '_meta'>, ctx: AddonContext): HostMethodResult<'source.list'> | Promise<HostMethodResult<'source.list'>>;
|
|
25
|
+
stat(params: Omit<HostMethodParams<'source.stat'>, '_meta'>, ctx: AddonContext): HostMethodResult<'source.stat'> | Promise<HostMethodResult<'source.stat'>>;
|
|
26
|
+
download(params: Omit<HostMethodParams<'source.download'>, '_meta'>, ctx: AddonContext): HostMethodResult<'source.download'> | Promise<HostMethodResult<'source.download'>>;
|
|
27
|
+
upload?(params: Omit<HostMethodParams<'source.upload'>, '_meta'>, ctx: AddonContext): HostMethodResult<'source.upload'> | Promise<HostMethodResult<'source.upload'>>;
|
|
28
|
+
mkdir?(params: Omit<HostMethodParams<'source.mkdir'>, '_meta'>, ctx: AddonContext): HostMethodResult<'source.mkdir'> | Promise<HostMethodResult<'source.mkdir'>>;
|
|
29
|
+
remove?(params: Omit<HostMethodParams<'source.remove'>, '_meta'>, ctx: AddonContext): Promise<void> | void;
|
|
30
|
+
export?(params: Omit<HostMethodParams<'source.export'>, '_meta'>, ctx: AddonContext): HostMethodResult<'source.export'> | Promise<HostMethodResult<'source.export'>>;
|
|
31
|
+
}
|
|
32
|
+
export interface LifecycleHandlers {
|
|
33
|
+
upgrade?(params: HostMethodParams<'lifecycle.upgrade'>, ctx: AddonContext): Promise<void> | void;
|
|
34
|
+
}
|
|
35
|
+
export interface NotificationHandlers {
|
|
36
|
+
'config.changed'?(params: NotificationParams<'config.changed'>, ctx: AddonContext): Promise<void> | void;
|
|
37
|
+
'permissions.changed'?(params: NotificationParams<'permissions.changed'>, ctx: AddonContext): Promise<void> | void;
|
|
38
|
+
'accounts.changed'?(params: NotificationParams<'accounts.changed'>, ctx: AddonContext): Promise<void> | void;
|
|
39
|
+
}
|
|
40
|
+
export interface AddonDefinition {
|
|
41
|
+
tools?: Record<string, ToolHandler>;
|
|
42
|
+
routes?: Record<string, RouteHandler>;
|
|
43
|
+
triggers?: Record<string, TriggerPair>;
|
|
44
|
+
jobs?: Record<string, JobHandler>;
|
|
45
|
+
workers?: Record<string, WorkerPair>;
|
|
46
|
+
hooks?: Record<string, HookHandler>;
|
|
47
|
+
transport?: TransportHandlers;
|
|
48
|
+
sources?: Record<string, SourceBackendHandlers>;
|
|
49
|
+
lifecycle?: LifecycleHandlers;
|
|
50
|
+
notifications?: NotificationHandlers;
|
|
51
|
+
}
|
|
52
|
+
export interface ResolvedAddonDefinition extends AddonDefinition {
|
|
53
|
+
readonly handlers: string[];
|
|
54
|
+
}
|
|
55
|
+
export declare function deriveHandlers(definition: AddonDefinition): string[];
|
|
56
|
+
export declare function defineAddon(definition: AddonDefinition): ResolvedAddonDefinition;
|
|
57
|
+
export type { AddonHandlerOf };
|