@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,350 @@
|
|
|
1
|
+
import type { ResolvedAddonDefinition } from '../define-addon.ts';
|
|
2
|
+
import { type SchemaValueIssue, schemaValueIssues } from '../families/json-schema-value.ts';
|
|
3
|
+
import type { HostChannel } from '../host-client.ts';
|
|
4
|
+
import type { AddonManifest } from '../manifest/manifest.ts';
|
|
5
|
+
import { AddonError } from '../protocol/error.ts';
|
|
6
|
+
import { methodSpec } from '../protocol/map.ts';
|
|
7
|
+
import type { CallMeta } from '../protocol/meta.ts';
|
|
8
|
+
import { type AddonRuntime, createAddonRuntime } from '../runtime.ts';
|
|
9
|
+
import { createFakeDb, type FakeDb } from './fake-db.ts';
|
|
10
|
+
import { createFakeServices } from './fake-services.ts';
|
|
11
|
+
|
|
12
|
+
export interface FakeAccount {
|
|
13
|
+
connection: string;
|
|
14
|
+
accountId: string;
|
|
15
|
+
label?: string;
|
|
16
|
+
externalAccountId?: string;
|
|
17
|
+
status?: string;
|
|
18
|
+
credentials?: unknown;
|
|
19
|
+
expiresAt?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface FakeHttpRequest {
|
|
23
|
+
url: string;
|
|
24
|
+
method: string;
|
|
25
|
+
headers: Record<string, string>;
|
|
26
|
+
body?: string;
|
|
27
|
+
auth?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface FakeHttpResponse {
|
|
31
|
+
status: number;
|
|
32
|
+
headers?: Record<string, string>;
|
|
33
|
+
body?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type FakeHttpHandler = (
|
|
37
|
+
request: FakeHttpRequest,
|
|
38
|
+
) => FakeHttpResponse | Promise<FakeHttpResponse>;
|
|
39
|
+
|
|
40
|
+
export interface FakeGrant {
|
|
41
|
+
id: string;
|
|
42
|
+
scope?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface FakeHostOptions {
|
|
46
|
+
definition: ResolvedAddonDefinition;
|
|
47
|
+
manifest: AddonManifest;
|
|
48
|
+
config?: Record<string, unknown>;
|
|
49
|
+
secrets?: Record<string, string>;
|
|
50
|
+
accounts?: FakeAccount[];
|
|
51
|
+
http?: FakeHttpHandler;
|
|
52
|
+
files?: Record<string, string>;
|
|
53
|
+
grants?: FakeGrant[];
|
|
54
|
+
now?(): number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface FakeHost {
|
|
58
|
+
readonly manifest: AddonManifest;
|
|
59
|
+
logs: Array<{ level: string; message: string; fields?: Record<string, unknown> }>;
|
|
60
|
+
emitted: Array<{ name: string; payload: unknown }>;
|
|
61
|
+
dispatchedRuns: Array<{ threadId?: string; agentId?: string; input: string }>;
|
|
62
|
+
cancelledRuns: Array<{ threadId: string; runId: string }>;
|
|
63
|
+
createdThreads: string[];
|
|
64
|
+
patchedThreads: Array<{ threadId: string; responderMode?: 'auto' | 'mention' }>;
|
|
65
|
+
accountReports: Array<{ connection: string; accountId?: string; code: string }>;
|
|
66
|
+
workerReports: Array<{ workerId: string; state: string; code?: string }>;
|
|
67
|
+
inboundMessages: Array<{ channel: string; from?: string; text: string; payload?: unknown }>;
|
|
68
|
+
fileWrites: Array<{ path: string; size: number }>;
|
|
69
|
+
createdTriggerInstances: Array<Record<string, unknown>>;
|
|
70
|
+
files: Record<string, string>;
|
|
71
|
+
progress: Array<{ token: string; value: unknown }>;
|
|
72
|
+
now(): number;
|
|
73
|
+
advanceTime(ms: number): void;
|
|
74
|
+
callTool(
|
|
75
|
+
pack: string,
|
|
76
|
+
tool: string,
|
|
77
|
+
input?: Record<string, unknown>,
|
|
78
|
+
): Promise<{ output: unknown }>;
|
|
79
|
+
callRoute(
|
|
80
|
+
routeId: string,
|
|
81
|
+
request: {
|
|
82
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
83
|
+
path?: string;
|
|
84
|
+
query?: Record<string, string>;
|
|
85
|
+
body?: unknown;
|
|
86
|
+
},
|
|
87
|
+
): Promise<{ status: number; body?: unknown }>;
|
|
88
|
+
runJob(jobId: string): Promise<void>;
|
|
89
|
+
startTrigger(
|
|
90
|
+
instanceId: string,
|
|
91
|
+
kind: string,
|
|
92
|
+
config?: Record<string, unknown>,
|
|
93
|
+
target?: unknown,
|
|
94
|
+
): Promise<void>;
|
|
95
|
+
stopTrigger(instanceId: string): Promise<void>;
|
|
96
|
+
startWorker(workerId: string): Promise<void>;
|
|
97
|
+
emit(event: Record<string, unknown>, hookId: string): Promise<void>;
|
|
98
|
+
notify(method: string, params: unknown): Promise<void>;
|
|
99
|
+
deliver(method: string, params: unknown): Promise<unknown>;
|
|
100
|
+
service(method: string, params: unknown): Promise<unknown>;
|
|
101
|
+
close(): void;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function createFakeHost(options: FakeHostOptions): FakeHost {
|
|
105
|
+
const { definition, manifest } = options;
|
|
106
|
+
let clockOffset = 0;
|
|
107
|
+
let correlation = 0;
|
|
108
|
+
const base = options.now ?? ((): number => Date.now());
|
|
109
|
+
const now = (): number => base() + clockOffset;
|
|
110
|
+
const db: FakeDb = createFakeDb();
|
|
111
|
+
const startedTriggers = new Set<string>();
|
|
112
|
+
const firedKeys = new Set<string>();
|
|
113
|
+
const progress: Array<{ token: string; value: unknown }> = [];
|
|
114
|
+
const sinks = {
|
|
115
|
+
logs: [] as Array<{ level: string; message: string; fields?: Record<string, unknown> }>,
|
|
116
|
+
emitted: [] as Array<{ name: string; payload: unknown }>,
|
|
117
|
+
dispatchedRuns: [] as Array<{ threadId?: string; agentId?: string; input: string }>,
|
|
118
|
+
cancelledRuns: [] as Array<{ threadId: string; runId: string }>,
|
|
119
|
+
createdThreads: [] as string[],
|
|
120
|
+
patchedThreads: [],
|
|
121
|
+
accountReports: [] as Array<{ connection: string; accountId?: string; code: string }>,
|
|
122
|
+
workerReports: [] as Array<{ workerId: string; state: string; code?: string }>,
|
|
123
|
+
inboundMessages: [] as Array<{
|
|
124
|
+
channel: string;
|
|
125
|
+
from?: string;
|
|
126
|
+
text: string;
|
|
127
|
+
payload?: unknown;
|
|
128
|
+
}>,
|
|
129
|
+
fileWrites: [] as Array<{ path: string; size: number }>,
|
|
130
|
+
createdTriggerInstances: [] as Array<Record<string, unknown>>,
|
|
131
|
+
};
|
|
132
|
+
const files: Record<string, string> = options.files ?? {};
|
|
133
|
+
const services = createFakeServices({
|
|
134
|
+
manifest,
|
|
135
|
+
options: { ...options, files },
|
|
136
|
+
db,
|
|
137
|
+
sinks,
|
|
138
|
+
startedTriggers,
|
|
139
|
+
firedKeys,
|
|
140
|
+
});
|
|
141
|
+
const fakeChannel: HostChannel = {
|
|
142
|
+
request(method: string, params: unknown): Promise<unknown> {
|
|
143
|
+
return dispatchAddonToHost(method, params);
|
|
144
|
+
},
|
|
145
|
+
notify(method: string, params: unknown): void {
|
|
146
|
+
if (method === '$/progress') {
|
|
147
|
+
progress.push(params as { token: string; value: unknown });
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const runtime: AddonRuntime = createAddonRuntime({
|
|
153
|
+
definition,
|
|
154
|
+
addon: { id: manifest.id, version: manifest.version },
|
|
155
|
+
channel: fakeChannel,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const host: FakeHost = {
|
|
159
|
+
manifest,
|
|
160
|
+
logs: sinks.logs,
|
|
161
|
+
emitted: sinks.emitted,
|
|
162
|
+
dispatchedRuns: sinks.dispatchedRuns,
|
|
163
|
+
cancelledRuns: sinks.cancelledRuns,
|
|
164
|
+
createdThreads: sinks.createdThreads,
|
|
165
|
+
patchedThreads: sinks.patchedThreads,
|
|
166
|
+
accountReports: sinks.accountReports,
|
|
167
|
+
workerReports: sinks.workerReports,
|
|
168
|
+
inboundMessages: sinks.inboundMessages,
|
|
169
|
+
fileWrites: sinks.fileWrites,
|
|
170
|
+
createdTriggerInstances: sinks.createdTriggerInstances,
|
|
171
|
+
files,
|
|
172
|
+
progress,
|
|
173
|
+
now,
|
|
174
|
+
advanceTime(ms: number) {
|
|
175
|
+
clockOffset += ms;
|
|
176
|
+
},
|
|
177
|
+
async callTool(pack, tool, input = {}) {
|
|
178
|
+
const result = (await deliver('tools.call', { pack, tool, input })) as { output: unknown };
|
|
179
|
+
return { output: result.output };
|
|
180
|
+
},
|
|
181
|
+
async callRoute(routeId, request) {
|
|
182
|
+
const result = (await deliver('route.handle', {
|
|
183
|
+
routeId,
|
|
184
|
+
request: {
|
|
185
|
+
method: request.method ?? 'GET',
|
|
186
|
+
path: request.path ?? '/',
|
|
187
|
+
query: request.query ?? {},
|
|
188
|
+
headers: {},
|
|
189
|
+
body: request.body,
|
|
190
|
+
},
|
|
191
|
+
})) as { status: number; body?: unknown };
|
|
192
|
+
return { status: result.status, body: result.body };
|
|
193
|
+
},
|
|
194
|
+
async runJob(jobId) {
|
|
195
|
+
await deliver('job.run', { jobId });
|
|
196
|
+
},
|
|
197
|
+
async startTrigger(instanceId, kind, config = {}, target) {
|
|
198
|
+
await deliver('trigger.start', { instanceId, kind, config, target });
|
|
199
|
+
startedTriggers.add(instanceId);
|
|
200
|
+
},
|
|
201
|
+
async stopTrigger(instanceId) {
|
|
202
|
+
startedTriggers.delete(instanceId);
|
|
203
|
+
await deliver('trigger.stop', { instanceId });
|
|
204
|
+
},
|
|
205
|
+
async startWorker(workerId) {
|
|
206
|
+
await deliver('worker.start', { workerId });
|
|
207
|
+
},
|
|
208
|
+
async emit(event, hookId) {
|
|
209
|
+
await deliver('hook.deliver', { hookId, event });
|
|
210
|
+
},
|
|
211
|
+
async notify(method, params) {
|
|
212
|
+
await runtime.handleNotification(method, params, new AbortController().signal);
|
|
213
|
+
},
|
|
214
|
+
async deliver(method, params) {
|
|
215
|
+
return deliver(method, params);
|
|
216
|
+
},
|
|
217
|
+
async service(method, params) {
|
|
218
|
+
return dispatchAddonToHost(method, params);
|
|
219
|
+
},
|
|
220
|
+
close() {
|
|
221
|
+
db.close();
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
function deliver(method: string, params: unknown): Promise<unknown> {
|
|
226
|
+
correlation += 1;
|
|
227
|
+
const meta: CallMeta = {
|
|
228
|
+
correlationId: `fake-${correlation}`,
|
|
229
|
+
deadline: base() + 5000,
|
|
230
|
+
principal: { type: 'system' },
|
|
231
|
+
locale: 'en',
|
|
232
|
+
};
|
|
233
|
+
const controller = new AbortController();
|
|
234
|
+
const withMeta =
|
|
235
|
+
method === 'initialize' ? params : { ...(params as Record<string, unknown>), _meta: meta };
|
|
236
|
+
if (method === 'tools.call') {
|
|
237
|
+
return deliverToolCall(withMeta as Record<string, unknown>, controller.signal);
|
|
238
|
+
}
|
|
239
|
+
return runtime.handleRequest(method, withMeta, controller.signal);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Mirrors the real host: tool input is validated against the descriptor's
|
|
243
|
+
// `inputSchema` before the call and the output against `outputSchema` after
|
|
244
|
+
// it (see docs/external-addons/contributions.md, tool-pack).
|
|
245
|
+
async function deliverToolCall(
|
|
246
|
+
params: Record<string, unknown>,
|
|
247
|
+
signal: AbortSignal,
|
|
248
|
+
): Promise<unknown> {
|
|
249
|
+
const tool = typeof params.tool === 'string' ? params.tool : '';
|
|
250
|
+
const descriptor = toolDescriptor(tool);
|
|
251
|
+
const input = isPlainObject(params.input) ? params.input : {};
|
|
252
|
+
const inputIssues = descriptor?.inputSchema
|
|
253
|
+
? schemaValueIssues(input, descriptor.inputSchema)
|
|
254
|
+
: [];
|
|
255
|
+
if (inputIssues.length > 0) {
|
|
256
|
+
throw new AddonError('invalid_params', {
|
|
257
|
+
detail: `tools.call ${tool}: ${describeIssue(inputIssues)}`,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
const result = (await runtime.handleRequest('tools.call', params, signal)) as {
|
|
261
|
+
output: unknown;
|
|
262
|
+
};
|
|
263
|
+
const outputIssues = descriptor?.outputSchema
|
|
264
|
+
? schemaValueIssues(result.output ?? {}, descriptor.outputSchema)
|
|
265
|
+
: [];
|
|
266
|
+
if (outputIssues.length > 0) {
|
|
267
|
+
throw new AddonError('invalid_response', {
|
|
268
|
+
detail: `tools.call ${tool}: ${describeIssue(outputIssues)}`,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function toolDescriptor(tool: string): ToolSchemaDescriptor | null {
|
|
275
|
+
return toolSchemaDescriptor(manifest, tool);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function describeIssue(issues: SchemaValueIssue[]): string {
|
|
279
|
+
const issue = issues[0];
|
|
280
|
+
if (!issue) {
|
|
281
|
+
return 'validation failed';
|
|
282
|
+
}
|
|
283
|
+
return `${issue.path === '$' ? 'params' : issue.path}: ${issue.message}`;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async function dispatchAddonToHost(method: string, rawParams: unknown): Promise<unknown> {
|
|
287
|
+
const spec = methodSpec(method);
|
|
288
|
+
if (spec?.direction !== 'addon-to-host') {
|
|
289
|
+
throw new AddonError('unsupported', {
|
|
290
|
+
detail: `method '${method}' is not an addon-to-host service`,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
await checkPermission(spec.permission);
|
|
294
|
+
const params = spec.params.parse(rawParams) as Record<string, unknown>;
|
|
295
|
+
const result = await services.execute(method, params);
|
|
296
|
+
return spec.result ? spec.result.parse(result) : result;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function checkPermission(permission: string | null): void {
|
|
300
|
+
if (permission === null || services.hasGrant(permission)) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
throw new AddonError('permission_denied', {
|
|
304
|
+
detail: `permission '${permission}' was not granted`,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return host;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
312
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
interface ToolSchemaDescriptor {
|
|
316
|
+
inputSchema?: Record<string, unknown>;
|
|
317
|
+
outputSchema?: Record<string, unknown>;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function toolSchemaDescriptor(manifest: AddonManifest, tool: string): ToolSchemaDescriptor | null {
|
|
321
|
+
for (const contribution of manifest.contributions) {
|
|
322
|
+
if (contribution.family !== 'tool-pack') {
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const entry = toolEntry(contribution.descriptor, tool);
|
|
326
|
+
if (entry) {
|
|
327
|
+
return {
|
|
328
|
+
...(isPlainObject(entry.inputSchema) ? { inputSchema: entry.inputSchema } : {}),
|
|
329
|
+
...(isPlainObject(entry.outputSchema) ? { outputSchema: entry.outputSchema } : {}),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function toolEntry(
|
|
337
|
+
descriptor: Record<string, unknown>,
|
|
338
|
+
tool: string,
|
|
339
|
+
): Record<string, unknown> | null {
|
|
340
|
+
const tools = descriptor.tools;
|
|
341
|
+
if (!Array.isArray(tools)) {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
return (
|
|
345
|
+
tools.find(
|
|
346
|
+
(candidate): candidate is Record<string, unknown> =>
|
|
347
|
+
isPlainObject(candidate) && candidate.name === tool,
|
|
348
|
+
) ?? null
|
|
349
|
+
);
|
|
350
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import type { AddonManifest } from '../manifest/manifest.ts';
|
|
2
|
+
import { isBaselinePermissionId } from '../permissions/permissions.ts';
|
|
3
|
+
import { AddonError } from '../protocol/error.ts';
|
|
4
|
+
import type { FakeDb } from './fake-db.ts';
|
|
5
|
+
import { executeFilesService } from './fake-files.ts';
|
|
6
|
+
import type { FakeAccount, FakeHostOptions } from './fake-host.ts';
|
|
7
|
+
|
|
8
|
+
export interface FakeServiceSinks {
|
|
9
|
+
logs: Array<{ level: string; message: string; fields?: Record<string, unknown> }>;
|
|
10
|
+
emitted: Array<{ name: string; payload: unknown }>;
|
|
11
|
+
dispatchedRuns: Array<{ threadId?: string; agentId?: string; input: string }>;
|
|
12
|
+
cancelledRuns: Array<{ threadId: string; runId: string }>;
|
|
13
|
+
createdThreads: string[];
|
|
14
|
+
patchedThreads: Array<{ threadId: string; responderMode?: 'auto' | 'mention' }>;
|
|
15
|
+
accountReports: Array<{ connection: string; accountId?: string; code: string }>;
|
|
16
|
+
fileWrites: Array<{ path: string; size: number }>;
|
|
17
|
+
workerReports: Array<{ workerId: string; state: string; code?: string }>;
|
|
18
|
+
inboundMessages: Array<{ channel: string; from?: string; text: string; payload?: unknown }>;
|
|
19
|
+
createdTriggerInstances: Array<Record<string, unknown>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface FakeServiceContext {
|
|
23
|
+
manifest: AddonManifest;
|
|
24
|
+
options: FakeHostOptions;
|
|
25
|
+
db: FakeDb;
|
|
26
|
+
sinks: FakeServiceSinks;
|
|
27
|
+
startedTriggers: Set<string>;
|
|
28
|
+
firedKeys: Set<string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type ServiceParams = Record<string, unknown>;
|
|
32
|
+
|
|
33
|
+
export function createFakeServices(context: FakeServiceContext): {
|
|
34
|
+
execute(method: string, params: ServiceParams): Promise<unknown>;
|
|
35
|
+
hasGrant(permission: string): boolean;
|
|
36
|
+
} {
|
|
37
|
+
const grants: Array<{ id: string; scope?: Record<string, unknown> }> =
|
|
38
|
+
context.options.grants ??
|
|
39
|
+
context.manifest.permissions.map((p) => ({ id: p.id, scope: p.scope }));
|
|
40
|
+
|
|
41
|
+
function granted(
|
|
42
|
+
permission: string,
|
|
43
|
+
): { id: string; scope?: Record<string, unknown> } | undefined {
|
|
44
|
+
if (isBaselinePermissionId(permission)) {
|
|
45
|
+
return { id: permission };
|
|
46
|
+
}
|
|
47
|
+
if (
|
|
48
|
+
permission === 'trigger' &&
|
|
49
|
+
context.manifest.contributions.some((c) => c.family === 'trigger')
|
|
50
|
+
) {
|
|
51
|
+
return { id: permission };
|
|
52
|
+
}
|
|
53
|
+
if (
|
|
54
|
+
permission === 'worker' &&
|
|
55
|
+
context.manifest.contributions.some((c) => c.family === 'worker')
|
|
56
|
+
) {
|
|
57
|
+
return { id: permission };
|
|
58
|
+
}
|
|
59
|
+
return grants.find((grant) => grant.id === permission);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function hasGrant(permission: string): boolean {
|
|
63
|
+
return granted(permission) !== undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function execute(method: string, params: ServiceParams): Promise<unknown> {
|
|
67
|
+
switch (method) {
|
|
68
|
+
case 'db.exec':
|
|
69
|
+
case 'db.all':
|
|
70
|
+
case 'db.get':
|
|
71
|
+
case 'db.run':
|
|
72
|
+
case 'db.batch':
|
|
73
|
+
return executeDb(method, params);
|
|
74
|
+
case 'accounts.list':
|
|
75
|
+
case 'accounts.report':
|
|
76
|
+
case 'accounts.access':
|
|
77
|
+
return executeAccounts(method, params);
|
|
78
|
+
case 'http.fetch':
|
|
79
|
+
return executeHttp(params);
|
|
80
|
+
case 'files.stat':
|
|
81
|
+
case 'files.read':
|
|
82
|
+
case 'files.write':
|
|
83
|
+
return executeFilesService(method, params, {
|
|
84
|
+
options: context.options,
|
|
85
|
+
writes: context.sinks.fileWrites,
|
|
86
|
+
});
|
|
87
|
+
case 'triggers.fire':
|
|
88
|
+
return fireTrigger(params);
|
|
89
|
+
case 'triggers.create':
|
|
90
|
+
return createTriggerInstance(params);
|
|
91
|
+
case 'triggers.update':
|
|
92
|
+
return updateTriggerInstance(params);
|
|
93
|
+
default:
|
|
94
|
+
return executeRecord(method, params);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function executeDb(method: string, params: ServiceParams): unknown {
|
|
99
|
+
const db = context.db;
|
|
100
|
+
switch (method) {
|
|
101
|
+
case 'db.exec':
|
|
102
|
+
db.exec(params.sql as string);
|
|
103
|
+
return {};
|
|
104
|
+
case 'db.all':
|
|
105
|
+
return { rows: db.all(params.sql as string, params.params as never) };
|
|
106
|
+
case 'db.get':
|
|
107
|
+
return { row: db.get(params.sql as string, params.params as never) };
|
|
108
|
+
case 'db.run':
|
|
109
|
+
return db.run(params.sql as string, params.params as never);
|
|
110
|
+
default:
|
|
111
|
+
return { results: db.batch(params.statements as never) };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function executeAccounts(method: string, params: ServiceParams): Promise<unknown> {
|
|
116
|
+
const connection = params.connection as string;
|
|
117
|
+
assertConnection(connection);
|
|
118
|
+
if (method === 'accounts.list') {
|
|
119
|
+
const accounts = boundAccounts(connection);
|
|
120
|
+
return {
|
|
121
|
+
accounts: accounts.map((account) => ({
|
|
122
|
+
accountId: account.accountId,
|
|
123
|
+
label: account.label ?? account.accountId,
|
|
124
|
+
externalAccountId: account.externalAccountId ?? account.accountId,
|
|
125
|
+
status: account.status ?? 'connected',
|
|
126
|
+
})),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (method === 'accounts.report') {
|
|
130
|
+
context.sinks.accountReports.push({
|
|
131
|
+
connection,
|
|
132
|
+
accountId: params.accountId as string | undefined,
|
|
133
|
+
code: params.code as string,
|
|
134
|
+
});
|
|
135
|
+
return {};
|
|
136
|
+
}
|
|
137
|
+
const account = selectAccount(connection, params.accountId as string | undefined);
|
|
138
|
+
return { credentials: account.credentials ?? null, expiresAt: account.expiresAt };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function executeHttp(params: ServiceParams): Promise<unknown> {
|
|
142
|
+
const request = params as {
|
|
143
|
+
url: string;
|
|
144
|
+
method: string;
|
|
145
|
+
headers?: Record<string, string>;
|
|
146
|
+
body?: string;
|
|
147
|
+
};
|
|
148
|
+
let url: URL;
|
|
149
|
+
try {
|
|
150
|
+
url = new URL(request.url);
|
|
151
|
+
} catch {
|
|
152
|
+
throw new AddonError('invalid_params', { detail: `url '${request.url}' is not valid` });
|
|
153
|
+
}
|
|
154
|
+
if (url.protocol !== 'https:') {
|
|
155
|
+
throw new AddonError('permission_denied', { detail: 'egress policy allows https only' });
|
|
156
|
+
}
|
|
157
|
+
const grant = granted('http.fetch');
|
|
158
|
+
if (
|
|
159
|
+
grant?.scope &&
|
|
160
|
+
Array.isArray(grant.scope.hosts) &&
|
|
161
|
+
!(grant.scope.hosts as string[]).includes(url.host)
|
|
162
|
+
) {
|
|
163
|
+
throw new AddonError('permission_denied', {
|
|
164
|
+
detail: `host '${url.host}' is outside the granted scope`,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
if (!context.options.http) {
|
|
168
|
+
throw new AddonError('unavailable', { detail: 'fake host has no http handler configured' });
|
|
169
|
+
}
|
|
170
|
+
const response = await context.options.http({
|
|
171
|
+
url: request.url,
|
|
172
|
+
method: request.method,
|
|
173
|
+
headers: request.headers ?? {},
|
|
174
|
+
body: request.body,
|
|
175
|
+
auth: params.auth,
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
status: response.status,
|
|
179
|
+
headers: response.headers ?? {},
|
|
180
|
+
body: response.body ?? null,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function fireTrigger(params: ServiceParams): unknown {
|
|
185
|
+
const instanceId = params.instanceId as string;
|
|
186
|
+
const runKey = params.runKey as string;
|
|
187
|
+
if (!context.startedTriggers.has(instanceId)) {
|
|
188
|
+
throw new AddonError('not_found', {
|
|
189
|
+
detail: `trigger instance '${instanceId}' was not started`,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const key = `${instanceId}:${runKey}`;
|
|
193
|
+
if (context.firedKeys.has(key)) {
|
|
194
|
+
return { outcome: 'suppressed' };
|
|
195
|
+
}
|
|
196
|
+
context.firedKeys.add(key);
|
|
197
|
+
return { outcome: 'fired' };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function createTriggerInstance(params: ServiceParams): unknown {
|
|
201
|
+
const kind = params.kind as string;
|
|
202
|
+
const declared = context.manifest.contributions.some(
|
|
203
|
+
(c) => c.family === 'trigger' && (c.descriptor as { kind?: string }).kind === kind,
|
|
204
|
+
);
|
|
205
|
+
if (!declared) {
|
|
206
|
+
throw new AddonError('invalid_params', {
|
|
207
|
+
detail: `trigger kind is not declared: ${kind}`,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
const instanceId = `trigger-instance-${context.sinks.createdTriggerInstances.length + 1}`;
|
|
211
|
+
context.sinks.createdTriggerInstances.push({ ...params, instanceId });
|
|
212
|
+
context.startedTriggers.add(instanceId);
|
|
213
|
+
return { instanceId };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function updateTriggerInstance(params: ServiceParams): unknown {
|
|
217
|
+
const instanceId = params.instanceId as string;
|
|
218
|
+
const stored = context.sinks.createdTriggerInstances.find(
|
|
219
|
+
(row) => row.instanceId === instanceId,
|
|
220
|
+
);
|
|
221
|
+
if (!stored) {
|
|
222
|
+
throw new AddonError('not_found', {
|
|
223
|
+
detail: `trigger instance not found: ${instanceId}`,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
for (const key of ['label', 'config', 'target', 'instructions'] as const) {
|
|
227
|
+
if (params[key] !== undefined) {
|
|
228
|
+
stored[key] = params[key];
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return { instanceId };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function executeRecord(method: string, params: ServiceParams): unknown {
|
|
235
|
+
const sinks = context.sinks;
|
|
236
|
+
switch (method) {
|
|
237
|
+
case 'config.get':
|
|
238
|
+
return configValues(params.keys as string[] | undefined);
|
|
239
|
+
case 'secrets.get': {
|
|
240
|
+
const value = context.options.secrets?.[params.key as string];
|
|
241
|
+
return { value: value === undefined ? null : value };
|
|
242
|
+
}
|
|
243
|
+
case 'log.write':
|
|
244
|
+
sinks.logs.push({
|
|
245
|
+
level: params.level as string,
|
|
246
|
+
message: params.message as string,
|
|
247
|
+
fields: params.fields as never,
|
|
248
|
+
});
|
|
249
|
+
return {};
|
|
250
|
+
case 'events.emit': {
|
|
251
|
+
const name = params.name as string;
|
|
252
|
+
if (!name.startsWith(`${context.manifest.id}.`)) {
|
|
253
|
+
throw new AddonError('invalid_params', {
|
|
254
|
+
detail: `events.emit name '${name}' is outside the addon namespace '${context.manifest.id}.*'`,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
sinks.emitted.push({ name, payload: params.payload });
|
|
258
|
+
return {};
|
|
259
|
+
}
|
|
260
|
+
case 'runs.dispatch':
|
|
261
|
+
sinks.dispatchedRuns.push(params as never);
|
|
262
|
+
return { runId: `run-${sinks.dispatchedRuns.length}` };
|
|
263
|
+
case 'runs.cancel':
|
|
264
|
+
sinks.cancelledRuns.push(params as never);
|
|
265
|
+
return { ok: true };
|
|
266
|
+
case 'threads.create': {
|
|
267
|
+
const threadId = `thread-${sinks.createdThreads.length + 1}`;
|
|
268
|
+
sinks.createdThreads.push(threadId);
|
|
269
|
+
return { threadId };
|
|
270
|
+
}
|
|
271
|
+
case 'threads.get':
|
|
272
|
+
return { thread: undefined };
|
|
273
|
+
case 'threads.patch':
|
|
274
|
+
sinks.patchedThreads.push(params as never);
|
|
275
|
+
return {};
|
|
276
|
+
case 'agents.list':
|
|
277
|
+
return { agents: [] };
|
|
278
|
+
case 'bridge.inbound':
|
|
279
|
+
sinks.inboundMessages.push(params.message as never);
|
|
280
|
+
return { outcome: 'dispatched' };
|
|
281
|
+
case 'workers.report':
|
|
282
|
+
sinks.workerReports.push(params as never);
|
|
283
|
+
return {};
|
|
284
|
+
default:
|
|
285
|
+
throw new AddonError('unsupported', {
|
|
286
|
+
detail: `fake host has no service implementation for '${method}'`,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function configValues(keys: string[] | undefined): unknown {
|
|
292
|
+
const values: Record<string, unknown> = { ...context.options.config };
|
|
293
|
+
if (!keys) {
|
|
294
|
+
return { values };
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
values: Object.fromEntries(
|
|
298
|
+
keys.filter((key) => key in values).map((key) => [key, values[key]]),
|
|
299
|
+
),
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function boundAccounts(connection: string): FakeAccount[] {
|
|
304
|
+
return (context.options.accounts ?? []).filter((account) => account.connection === connection);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function assertConnection(connection: string): void {
|
|
308
|
+
const declared = context.manifest.connections.some(
|
|
309
|
+
(requirement) => requirement.id === connection,
|
|
310
|
+
);
|
|
311
|
+
if (!declared) {
|
|
312
|
+
throw new AddonError('invalid_params', {
|
|
313
|
+
detail: `connection '${connection}' is not declared in the manifest`,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function selectAccount(connection: string, accountId: string | undefined): FakeAccount {
|
|
319
|
+
const accounts = boundAccounts(connection);
|
|
320
|
+
if (accounts.length === 0) {
|
|
321
|
+
throw new AddonError('account_failed', {
|
|
322
|
+
detail: `no account is bound to connection '${connection}'`,
|
|
323
|
+
accountCode: 'not_connected',
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
if (accountId === undefined) {
|
|
327
|
+
if (accounts.length > 1) {
|
|
328
|
+
throw new AddonError('invalid_params', {
|
|
329
|
+
detail: `accountId is required: connection '${connection}' has ${accounts.length} bindings`,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return accounts[0] as FakeAccount;
|
|
333
|
+
}
|
|
334
|
+
const found = accounts.find((account) => account.accountId === accountId);
|
|
335
|
+
if (!found) {
|
|
336
|
+
throw new AddonError('account_failed', {
|
|
337
|
+
detail: `account '${accountId}' is not bound to '${connection}'`,
|
|
338
|
+
accountCode: 'not_bound',
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
return found;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return { execute, hasGrant };
|
|
345
|
+
}
|