@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
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import { familyRegistry } from '../families/registry.ts';
|
|
3
|
+
import { isKnownPermissionId } from '../permissions/permissions.ts';
|
|
4
|
+
import { l10nKeys } from './l10n.ts';
|
|
5
|
+
import type { AddonManifest } from './manifest.ts';
|
|
6
|
+
import { AddonManifestSchema } from './manifest.ts';
|
|
7
|
+
|
|
8
|
+
export interface Violation {
|
|
9
|
+
path: string;
|
|
10
|
+
code: string;
|
|
11
|
+
message: string;
|
|
12
|
+
severity: 'error' | 'warning';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ManifestValidation {
|
|
16
|
+
manifest: AddonManifest | null;
|
|
17
|
+
violations: Violation[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface CheckContext {
|
|
21
|
+
manifest: AddonManifest;
|
|
22
|
+
violations: Violation[];
|
|
23
|
+
connectionIds: Set<string>;
|
|
24
|
+
idsByKind: Record<string, Set<string>>;
|
|
25
|
+
contributionIds: Set<string>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function validateManifest(
|
|
29
|
+
raw: unknown,
|
|
30
|
+
options?: { l10nKeys?: string[] },
|
|
31
|
+
): ManifestValidation {
|
|
32
|
+
const violations: Violation[] = [];
|
|
33
|
+
const parsed = AddonManifestSchema.safeParse(raw);
|
|
34
|
+
if (!parsed.success) {
|
|
35
|
+
for (const issue of parsed.error.issues) {
|
|
36
|
+
violations.push({
|
|
37
|
+
path: issue.path.map(String).join('.') || '(root)',
|
|
38
|
+
code: 'schema',
|
|
39
|
+
message: issue.message,
|
|
40
|
+
severity: 'error',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return { manifest: null, violations };
|
|
44
|
+
}
|
|
45
|
+
const context: CheckContext = {
|
|
46
|
+
manifest: parsed.data,
|
|
47
|
+
violations,
|
|
48
|
+
connectionIds: new Set(parsed.data.connections.map((c) => c.id)),
|
|
49
|
+
idsByKind: { route: new Set(), job: new Set(), worker: new Set() },
|
|
50
|
+
contributionIds: new Set<string>(),
|
|
51
|
+
};
|
|
52
|
+
checkPermissions(context);
|
|
53
|
+
checkContributions(context);
|
|
54
|
+
checkL10nKeys(parsed.data, violations, options);
|
|
55
|
+
return { manifest: parsed.data, violations };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function checkPermissions(context: CheckContext): void {
|
|
59
|
+
const { manifest, violations } = context;
|
|
60
|
+
for (const permission of manifest.permissions) {
|
|
61
|
+
if (!isKnownPermissionId(permission.id)) {
|
|
62
|
+
violations.push({
|
|
63
|
+
path: `permissions[${permission.id}]`,
|
|
64
|
+
code: 'unknown_permission',
|
|
65
|
+
message: `unknown permission id '${permission.id}'`,
|
|
66
|
+
severity: 'error',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
manifest.activation === 'resident' &&
|
|
72
|
+
!manifest.permissions.some((p) => p.id === 'lifecycle.resident')
|
|
73
|
+
) {
|
|
74
|
+
violations.push({
|
|
75
|
+
path: 'activation',
|
|
76
|
+
code: 'resident_without_permission',
|
|
77
|
+
message: `activation 'resident' requires the 'lifecycle.resident' permission`,
|
|
78
|
+
severity: 'error',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function checkContributions(context: CheckContext): void {
|
|
84
|
+
const { manifest, violations } = context;
|
|
85
|
+
for (const [index, contribution] of manifest.contributions.entries()) {
|
|
86
|
+
const path = `contributions[${contribution.id || index}]`;
|
|
87
|
+
checkContributionId(context, contribution.id, path);
|
|
88
|
+
const schema: z.ZodType | undefined = familyRegistry[contribution.family]?.schema(
|
|
89
|
+
contribution.v ?? 1,
|
|
90
|
+
);
|
|
91
|
+
if (!schema) {
|
|
92
|
+
violations.push({
|
|
93
|
+
path,
|
|
94
|
+
code: 'unknown_family',
|
|
95
|
+
message: `unknown family '${contribution.family}'`,
|
|
96
|
+
severity: contribution.optional === true ? 'warning' : 'error',
|
|
97
|
+
});
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
checkDescriptorSchema(path, contribution.descriptor, schema, violations);
|
|
101
|
+
checkDescriptorRefs(context, path, contribution.family, contribution.descriptor);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function checkContributionId(context: CheckContext, id: string, path: string): void {
|
|
106
|
+
if (context.contributionIds.has(id)) {
|
|
107
|
+
context.violations.push({
|
|
108
|
+
path,
|
|
109
|
+
code: 'duplicate_contribution_id',
|
|
110
|
+
message: `duplicate contribution id '${id}'`,
|
|
111
|
+
severity: 'error',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
context.contributionIds.add(id);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function checkDescriptorSchema(
|
|
118
|
+
path: string,
|
|
119
|
+
descriptor: Record<string, unknown>,
|
|
120
|
+
schema: z.ZodType,
|
|
121
|
+
violations: Violation[],
|
|
122
|
+
): void {
|
|
123
|
+
const parsed = schema.safeParse(descriptor);
|
|
124
|
+
if (parsed.success) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
for (const issue of parsed.error.issues) {
|
|
128
|
+
violations.push({
|
|
129
|
+
path: `${path}.descriptor.${issue.path.map(String).join('.')}`,
|
|
130
|
+
code: 'descriptor_schema',
|
|
131
|
+
message: issue.message,
|
|
132
|
+
severity: 'error',
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function checkDescriptorRefs(
|
|
138
|
+
context: CheckContext,
|
|
139
|
+
path: string,
|
|
140
|
+
family: string,
|
|
141
|
+
descriptor: Record<string, unknown>,
|
|
142
|
+
): void {
|
|
143
|
+
const connection = descriptor.connection;
|
|
144
|
+
if (typeof connection === 'string' && !context.connectionIds.has(connection)) {
|
|
145
|
+
context.violations.push({
|
|
146
|
+
path,
|
|
147
|
+
code: 'unknown_connection',
|
|
148
|
+
message: `descriptor references undeclared connection '${connection}'`,
|
|
149
|
+
severity: 'error',
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
checkUniqueEntityId(context, family, descriptor, path);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function checkUniqueEntityId(
|
|
156
|
+
context: CheckContext,
|
|
157
|
+
family: string,
|
|
158
|
+
descriptor: Record<string, unknown>,
|
|
159
|
+
path: string,
|
|
160
|
+
): void {
|
|
161
|
+
const ids = context.idsByKind[family];
|
|
162
|
+
if (!ids) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const id = descriptor.id ?? descriptor.name ?? descriptor.kind ?? descriptor.provider;
|
|
166
|
+
if (typeof id !== 'string') {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (ids.has(id)) {
|
|
170
|
+
context.violations.push({
|
|
171
|
+
path,
|
|
172
|
+
code: 'duplicate_id',
|
|
173
|
+
message: `duplicate ${family} id '${id}'`,
|
|
174
|
+
severity: 'error',
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
ids.add(id);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function checkL10nKeys(
|
|
181
|
+
manifest: AddonManifest,
|
|
182
|
+
violations: Violation[],
|
|
183
|
+
options?: { l10nKeys?: string[] },
|
|
184
|
+
): void {
|
|
185
|
+
if (!options?.l10nKeys) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const available = new Set(options.l10nKeys);
|
|
189
|
+
const used = [...l10nKeys(manifest.title), ...l10nKeys(manifest.description)];
|
|
190
|
+
for (const key of used) {
|
|
191
|
+
if (!available.has(key)) {
|
|
192
|
+
violations.push({
|
|
193
|
+
path: 'title',
|
|
194
|
+
code: 'missing_l10n_key',
|
|
195
|
+
message: `l10n key '%${key}%' is not present in l10n/en.json`,
|
|
196
|
+
severity: 'error',
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const JsonObjectSchema = z.record(z.string(), z.unknown());
|
|
4
|
+
export type JsonObject = Record<string, unknown>;
|
|
5
|
+
|
|
6
|
+
export const PermissionScopeSchema = JsonObjectSchema;
|
|
7
|
+
export type PermissionScope = JsonObject;
|
|
8
|
+
|
|
9
|
+
export interface PermissionRequest {
|
|
10
|
+
id: string;
|
|
11
|
+
scope?: JsonObject;
|
|
12
|
+
reason: string;
|
|
13
|
+
optional?: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const PermissionRequestSchema = z.object({
|
|
17
|
+
id: z.string().min(1),
|
|
18
|
+
scope: PermissionScopeSchema.optional(),
|
|
19
|
+
reason: z.string().min(1),
|
|
20
|
+
optional: z.boolean().optional(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export interface EffectivePermission {
|
|
24
|
+
id: string;
|
|
25
|
+
scope?: JsonObject;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const EffectivePermissionSchema = z.object({
|
|
29
|
+
id: z.string().min(1),
|
|
30
|
+
scope: PermissionScopeSchema.optional(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export const BASELINE_PERMISSION_IDS = [
|
|
34
|
+
'config.read',
|
|
35
|
+
'log',
|
|
36
|
+
'db',
|
|
37
|
+
'secrets.own',
|
|
38
|
+
'events.emit.own',
|
|
39
|
+
'org.read',
|
|
40
|
+
] as const;
|
|
41
|
+
|
|
42
|
+
export type BaselinePermissionId = (typeof BASELINE_PERMISSION_IDS)[number];
|
|
43
|
+
|
|
44
|
+
export const KNOWN_PERMISSION_IDS = [
|
|
45
|
+
...BASELINE_PERMISSION_IDS,
|
|
46
|
+
'http.fetch',
|
|
47
|
+
'accounts.use',
|
|
48
|
+
'accounts.credentials',
|
|
49
|
+
'runs.dispatch',
|
|
50
|
+
'runs.read',
|
|
51
|
+
'threads.create',
|
|
52
|
+
'threads.compact',
|
|
53
|
+
'threads.read',
|
|
54
|
+
'threads.write',
|
|
55
|
+
'agents.read',
|
|
56
|
+
'bindings.write',
|
|
57
|
+
'usage.read',
|
|
58
|
+
'skills.read',
|
|
59
|
+
'events.subscribe',
|
|
60
|
+
'files.read',
|
|
61
|
+
'files.write',
|
|
62
|
+
'bridge.inbound',
|
|
63
|
+
'routes.public',
|
|
64
|
+
'lifecycle.resident',
|
|
65
|
+
'trigger',
|
|
66
|
+
'approvals.request',
|
|
67
|
+
'worker',
|
|
68
|
+
'source',
|
|
69
|
+
] as const;
|
|
70
|
+
|
|
71
|
+
export type KnownPermissionId = (typeof KNOWN_PERMISSION_IDS)[number];
|
|
72
|
+
|
|
73
|
+
export function isKnownPermissionId(id: string): boolean {
|
|
74
|
+
return (KNOWN_PERMISSION_IDS as readonly string[]).includes(id);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isBaselinePermissionId(id: string): boolean {
|
|
78
|
+
return (BASELINE_PERMISSION_IDS as readonly string[]).includes(id);
|
|
79
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { ResolvedAddonDefinition } from './define-addon.ts';
|
|
2
|
+
import type { HostChannel } from './host-client.ts';
|
|
3
|
+
import type { JsonRpcResponse } from './protocol/envelope.ts';
|
|
4
|
+
import {
|
|
5
|
+
decodeMessage,
|
|
6
|
+
encodeMessage,
|
|
7
|
+
errorResponse,
|
|
8
|
+
request,
|
|
9
|
+
response,
|
|
10
|
+
} from './protocol/envelope.ts';
|
|
11
|
+
import { AddonError, type AddonErrorReason, isAddonError } from './protocol/error.ts';
|
|
12
|
+
import { type AddonRuntime, createAddonRuntime } from './runtime.ts';
|
|
13
|
+
|
|
14
|
+
export const PROTOCOL_PLATFORM = 1;
|
|
15
|
+
|
|
16
|
+
const ERROR_CODES: Partial<Record<AddonErrorReason, number>> = {
|
|
17
|
+
invalid_params: -32602,
|
|
18
|
+
unsupported: -32601,
|
|
19
|
+
not_found: -32601,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type StdioWiringStdin = AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>;
|
|
23
|
+
|
|
24
|
+
export interface StdioWiring {
|
|
25
|
+
stdin: StdioWiringStdin;
|
|
26
|
+
stdout: { write(chunk: string): unknown };
|
|
27
|
+
stderr: { write(chunk: string): unknown };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function stdinSource(stdin: StdioWiringStdin): AsyncIterable<Uint8Array> {
|
|
31
|
+
if ('values' in stdin && typeof stdin.values === 'function') {
|
|
32
|
+
return stdin.values();
|
|
33
|
+
}
|
|
34
|
+
return stdin as AsyncIterable<Uint8Array>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface StartAddonProcessOptions {
|
|
38
|
+
definition: ResolvedAddonDefinition;
|
|
39
|
+
addon: { id: string; version: string };
|
|
40
|
+
wiring?: StdioWiring;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface PendingCall {
|
|
44
|
+
resolve(value: unknown): void;
|
|
45
|
+
reject(error: unknown): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function createStdioChannel(
|
|
49
|
+
wiring: StdioWiring,
|
|
50
|
+
): HostChannel & { resolveResponse(message: JsonRpcResponse): void } {
|
|
51
|
+
let nextId = 0;
|
|
52
|
+
const pending = new Map<string | number, PendingCall>();
|
|
53
|
+
|
|
54
|
+
function write(line: string): void {
|
|
55
|
+
wiring.stdout.write(line);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
request(method: string, params: unknown, signal?: AbortSignal): Promise<unknown> {
|
|
60
|
+
nextId += 1;
|
|
61
|
+
const id = nextId;
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
pending.set(id, { resolve, reject });
|
|
64
|
+
signal?.addEventListener(
|
|
65
|
+
'abort',
|
|
66
|
+
() => {
|
|
67
|
+
if (pending.has(id)) {
|
|
68
|
+
pending.delete(id);
|
|
69
|
+
reject(new AddonError('cancelled'));
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
{ once: true },
|
|
73
|
+
);
|
|
74
|
+
write(encodeMessage(request(method, id, params)));
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
notify(method: string, params: unknown): void {
|
|
78
|
+
write(encodeMessage({ jsonrpc: '2.0', method, params }));
|
|
79
|
+
},
|
|
80
|
+
resolveResponse(message: JsonRpcResponse): void {
|
|
81
|
+
const call = pending.get(message.id);
|
|
82
|
+
if (!call) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
pending.delete(message.id);
|
|
86
|
+
if (message.error) {
|
|
87
|
+
const data = message.error.data;
|
|
88
|
+
call.reject(
|
|
89
|
+
data === undefined
|
|
90
|
+
? new Error(message.error.message)
|
|
91
|
+
: new AddonError(data.reason, {
|
|
92
|
+
retryable: data.retryable,
|
|
93
|
+
detail: data.detail,
|
|
94
|
+
accountCode: data.accountCode,
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
} else {
|
|
98
|
+
call.resolve(message.result);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function startAddonProcess(options: StartAddonProcessOptions): Promise<void> {
|
|
105
|
+
const wiring = options.wiring ?? {
|
|
106
|
+
stdin: Bun.stdin.stream(),
|
|
107
|
+
stdout: process.stdout,
|
|
108
|
+
stderr: process.stderr,
|
|
109
|
+
};
|
|
110
|
+
const channel = createStdioChannel(wiring);
|
|
111
|
+
const runtime: AddonRuntime = createAddonRuntime({
|
|
112
|
+
definition: options.definition,
|
|
113
|
+
addon: options.addon,
|
|
114
|
+
channel,
|
|
115
|
+
});
|
|
116
|
+
const controllers = new Map<string | number, AbortController>();
|
|
117
|
+
|
|
118
|
+
function reply(id: string | number, result: unknown): void {
|
|
119
|
+
wiring.stdout.write(encodeMessage(response(id, result)));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function replyError(id: string | number | null, error: unknown): void {
|
|
123
|
+
const data = isAddonError(error)
|
|
124
|
+
? error.toData()
|
|
125
|
+
: { reason: 'internal' as const, retryable: false };
|
|
126
|
+
if (!isAddonError(error)) {
|
|
127
|
+
wiring.stderr.write(
|
|
128
|
+
`addon request failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const reason: AddonErrorReason = data.reason;
|
|
132
|
+
wiring.stdout.write(
|
|
133
|
+
encodeMessage(
|
|
134
|
+
errorResponse(id, { code: ERROR_CODES[reason] ?? -32000, message: reason, data }),
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function logFailure(scope: string, error: unknown): void {
|
|
140
|
+
wiring.stderr.write(
|
|
141
|
+
`addon ${scope} failed: ${error instanceof Error ? error.message : String(error)}\n`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function handleLine(line: string): Promise<void> {
|
|
146
|
+
const message = decodeMessage(line);
|
|
147
|
+
if ('method' in message && 'id' in message) {
|
|
148
|
+
await handleHostRequest(message.method, message.id, message.params);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if ('method' in message) {
|
|
152
|
+
await handleHostNotification(message.method, message.params);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
channel.resolveResponse(message);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function handleHostRequest(
|
|
159
|
+
method: string,
|
|
160
|
+
id: string | number,
|
|
161
|
+
params: unknown,
|
|
162
|
+
): Promise<void> {
|
|
163
|
+
const controller = new AbortController();
|
|
164
|
+
controllers.set(id, controller);
|
|
165
|
+
try {
|
|
166
|
+
const result =
|
|
167
|
+
method === 'initialize'
|
|
168
|
+
? runtime.initialize(params as never)
|
|
169
|
+
: await runtime.handleRequest(method, params, controller.signal);
|
|
170
|
+
reply(id, result);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
replyError(id, error);
|
|
173
|
+
} finally {
|
|
174
|
+
controllers.delete(id);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function handleHostNotification(method: string, params: unknown): Promise<void> {
|
|
179
|
+
if (method === '$/cancel') {
|
|
180
|
+
const cancelParams = (params ?? {}) as { id?: string | number };
|
|
181
|
+
if (cancelParams.id !== undefined) {
|
|
182
|
+
controllers.get(cancelParams.id)?.abort();
|
|
183
|
+
}
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
await runtime.handleNotification(method, params, new AbortController().signal);
|
|
188
|
+
} catch (error) {
|
|
189
|
+
logFailure(`notification ${method}`, error);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
for await (const chunk of stdinSource(wiring.stdin)) {
|
|
194
|
+
const text = new TextDecoder().decode(chunk);
|
|
195
|
+
for (const line of text.split('\n')) {
|
|
196
|
+
const trimmed = line.trim();
|
|
197
|
+
if (trimmed.length === 0) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
void handleLine(trimmed).catch((error: unknown) => {
|
|
201
|
+
replyError(null, error);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { AddonErrorDataSchema } from './error.ts';
|
|
3
|
+
|
|
4
|
+
export const JSONRPC_VERSION = '2.0';
|
|
5
|
+
export const MAX_MESSAGE_BYTES = 8 * 1024 * 1024;
|
|
6
|
+
|
|
7
|
+
export const JsonRpcIdSchema = z.union([z.string(), z.number()]);
|
|
8
|
+
|
|
9
|
+
export const JsonRpcRequestSchema = z.object({
|
|
10
|
+
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
11
|
+
id: JsonRpcIdSchema,
|
|
12
|
+
method: z.string().min(1),
|
|
13
|
+
params: z.unknown().optional(),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const JsonRpcNotificationSchema = z.object({
|
|
17
|
+
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
18
|
+
method: z.string().min(1),
|
|
19
|
+
params: z.unknown().optional(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const JsonRpcErrorSchema = z.object({
|
|
23
|
+
code: z.number().int(),
|
|
24
|
+
message: z.string().min(1),
|
|
25
|
+
data: AddonErrorDataSchema.optional(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const JsonRpcResponseSchema = z.object({
|
|
29
|
+
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
30
|
+
id: JsonRpcIdSchema,
|
|
31
|
+
result: z.unknown().optional(),
|
|
32
|
+
error: JsonRpcErrorSchema.optional(),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export type JsonRpcRequest = z.infer<typeof JsonRpcRequestSchema>;
|
|
36
|
+
export type JsonRpcNotification = z.infer<typeof JsonRpcNotificationSchema>;
|
|
37
|
+
export type JsonRpcResponse = z.infer<typeof JsonRpcResponseSchema>;
|
|
38
|
+
export type JsonRpcError = z.infer<typeof JsonRpcErrorSchema>;
|
|
39
|
+
|
|
40
|
+
export const CancelNotificationSchema = z.object({ id: JsonRpcIdSchema });
|
|
41
|
+
export type CancelNotification = z.infer<typeof CancelNotificationSchema>;
|
|
42
|
+
|
|
43
|
+
export const ProgressNotificationSchema = z.object({
|
|
44
|
+
token: z.string().min(1),
|
|
45
|
+
value: z.unknown(),
|
|
46
|
+
});
|
|
47
|
+
export type ProgressNotification = z.infer<typeof ProgressNotificationSchema>;
|
|
48
|
+
|
|
49
|
+
export interface OutgoingRequest {
|
|
50
|
+
jsonrpc: typeof JSONRPC_VERSION;
|
|
51
|
+
id: string | number;
|
|
52
|
+
method: string;
|
|
53
|
+
params?: unknown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface OutgoingNotification {
|
|
57
|
+
jsonrpc: typeof JSONRPC_VERSION;
|
|
58
|
+
method: string;
|
|
59
|
+
params?: unknown;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface OutgoingResponse {
|
|
63
|
+
jsonrpc: typeof JSONRPC_VERSION;
|
|
64
|
+
id: string | number;
|
|
65
|
+
result?: unknown;
|
|
66
|
+
error?: JsonRpcError;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function request(method: string, id: string | number, params?: unknown): OutgoingRequest {
|
|
70
|
+
return { jsonrpc: JSONRPC_VERSION, id, method, params };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function notification(method: string, params?: unknown): OutgoingNotification {
|
|
74
|
+
return { jsonrpc: JSONRPC_VERSION, method, params };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function response(id: string | number, result: unknown): OutgoingResponse {
|
|
78
|
+
return { jsonrpc: JSONRPC_VERSION, id, result };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function errorResponse(
|
|
82
|
+
id: string | number | null,
|
|
83
|
+
error: JsonRpcError,
|
|
84
|
+
): OutgoingErrorResponse {
|
|
85
|
+
return { jsonrpc: JSONRPC_VERSION, id, error };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface OutgoingErrorResponse {
|
|
89
|
+
jsonrpc: typeof JSONRPC_VERSION;
|
|
90
|
+
id: string | number | null;
|
|
91
|
+
error: JsonRpcError;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function encodeMessage(
|
|
95
|
+
message: OutgoingRequest | OutgoingNotification | OutgoingResponse | OutgoingErrorResponse,
|
|
96
|
+
): string {
|
|
97
|
+
const line = `${JSON.stringify(message)}\n`;
|
|
98
|
+
const bytes = new TextEncoder().encode(line);
|
|
99
|
+
if (bytes.length > MAX_MESSAGE_BYTES) {
|
|
100
|
+
throw new Error(`message exceeds the ${MAX_MESSAGE_BYTES} byte protocol cap`);
|
|
101
|
+
}
|
|
102
|
+
return line;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function decodeMessage(
|
|
106
|
+
line: string,
|
|
107
|
+
): JsonRpcRequest | JsonRpcNotification | JsonRpcResponse {
|
|
108
|
+
const bytes = new TextEncoder().encode(line);
|
|
109
|
+
if (bytes.length > MAX_MESSAGE_BYTES) {
|
|
110
|
+
throw new Error(`message exceeds the ${MAX_MESSAGE_BYTES} byte protocol cap`);
|
|
111
|
+
}
|
|
112
|
+
const parsed: unknown = JSON.parse(line);
|
|
113
|
+
const asRequest = JsonRpcRequestSchema.safeParse(parsed);
|
|
114
|
+
if (asRequest.success) {
|
|
115
|
+
return asRequest.data;
|
|
116
|
+
}
|
|
117
|
+
const asNotification = JsonRpcNotificationSchema.safeParse(parsed);
|
|
118
|
+
if (asNotification.success) {
|
|
119
|
+
return asNotification.data;
|
|
120
|
+
}
|
|
121
|
+
const asResponse = JsonRpcResponseSchema.safeParse(parsed);
|
|
122
|
+
if (asResponse.success) {
|
|
123
|
+
return asResponse.data;
|
|
124
|
+
}
|
|
125
|
+
throw new Error('message is not a JSON-RPC 2.0 request, notification or response');
|
|
126
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const AddonErrorReasonSchema = z.enum([
|
|
4
|
+
'invalid_params',
|
|
5
|
+
'invalid_response',
|
|
6
|
+
'permission_denied',
|
|
7
|
+
'not_found',
|
|
8
|
+
'conflict',
|
|
9
|
+
'quota_exceeded',
|
|
10
|
+
'rate_limited',
|
|
11
|
+
'timeout',
|
|
12
|
+
'cancelled',
|
|
13
|
+
'unavailable',
|
|
14
|
+
'unsupported',
|
|
15
|
+
'account_failed',
|
|
16
|
+
'incompatible_handlers',
|
|
17
|
+
'migration_failed',
|
|
18
|
+
'permission_revoked',
|
|
19
|
+
'internal',
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
export type AddonErrorReason = z.infer<typeof AddonErrorReasonSchema>;
|
|
23
|
+
|
|
24
|
+
export interface AddonErrorData {
|
|
25
|
+
reason: AddonErrorReason;
|
|
26
|
+
retryable: boolean;
|
|
27
|
+
detail?: string;
|
|
28
|
+
accountCode?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const AddonErrorDataSchema = z.object({
|
|
32
|
+
reason: AddonErrorReasonSchema,
|
|
33
|
+
retryable: z.boolean(),
|
|
34
|
+
detail: z.string().optional(),
|
|
35
|
+
accountCode: z.string().optional(),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export class AddonError extends Error {
|
|
39
|
+
readonly reason: AddonErrorReason;
|
|
40
|
+
readonly retryable: boolean;
|
|
41
|
+
readonly detail?: string;
|
|
42
|
+
readonly accountCode?: string;
|
|
43
|
+
|
|
44
|
+
constructor(
|
|
45
|
+
reason: AddonErrorReason,
|
|
46
|
+
options?: { retryable?: boolean; detail?: string; accountCode?: string },
|
|
47
|
+
) {
|
|
48
|
+
super(options?.detail ?? reason);
|
|
49
|
+
this.name = 'AddonError';
|
|
50
|
+
this.reason = reason;
|
|
51
|
+
this.retryable = options?.retryable ?? false;
|
|
52
|
+
this.detail = options?.detail;
|
|
53
|
+
this.accountCode = options?.accountCode;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toData(): AddonErrorData {
|
|
57
|
+
return {
|
|
58
|
+
reason: this.reason,
|
|
59
|
+
retryable: this.retryable,
|
|
60
|
+
detail: this.detail,
|
|
61
|
+
accountCode: this.accountCode,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Copy-safe detection: the addon dist bundles its own copy of this module, so
|
|
68
|
+
* in-process hosts see errors whose class identity differs from the host's
|
|
69
|
+
* `AddonError`. Detect structurally instead of by `instanceof` alone.
|
|
70
|
+
*/
|
|
71
|
+
export function isAddonError(error: unknown): error is AddonError {
|
|
72
|
+
if (error instanceof AddonError) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return (
|
|
76
|
+
error instanceof Error &&
|
|
77
|
+
error.name === 'AddonError' &&
|
|
78
|
+
'reason' in error &&
|
|
79
|
+
typeof (error as AddonError).toData === 'function'
|
|
80
|
+
);
|
|
81
|
+
}
|