@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.
Files changed (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +80 -2
  3. package/dist/addon-context-factory.d.ts +6 -0
  4. package/dist/addon-context-factory.js +160 -0
  5. package/dist/addon-context.d.ts +346 -0
  6. package/dist/addon-context.js +0 -0
  7. package/dist/define-addon.d.ts +57 -0
  8. package/dist/define-addon.js +53 -0
  9. package/dist/families/config.d.ts +14 -0
  10. package/dist/families/config.js +16 -0
  11. package/dist/families/content.d.ts +28 -0
  12. package/dist/families/content.js +19 -0
  13. package/dist/families/field-spec.d.ts +100 -0
  14. package/dist/families/field-spec.js +85 -0
  15. package/dist/families/hook.d.ts +37 -0
  16. package/dist/families/hook.js +25 -0
  17. package/dist/families/job.d.ts +45 -0
  18. package/dist/families/job.js +26 -0
  19. package/dist/families/json-schema-value.d.ts +12 -0
  20. package/dist/families/json-schema-value.js +243 -0
  21. package/dist/families/json-schema.d.ts +36 -0
  22. package/dist/families/json-schema.js +101 -0
  23. package/dist/families/lsp.d.ts +18 -0
  24. package/dist/families/lsp.js +18 -0
  25. package/dist/families/mcp.d.ts +18 -0
  26. package/dist/families/mcp.js +18 -0
  27. package/dist/families/registry.d.ts +9 -0
  28. package/dist/families/registry.js +34 -0
  29. package/dist/families/route.d.ts +38 -0
  30. package/dist/families/route.js +37 -0
  31. package/dist/families/source-kind.d.ts +24 -0
  32. package/dist/families/source-kind.js +21 -0
  33. package/dist/families/tool-pack.d.ts +68 -0
  34. package/dist/families/tool-pack.js +57 -0
  35. package/dist/families/transport.d.ts +52 -0
  36. package/dist/families/transport.js +29 -0
  37. package/dist/families/trigger.d.ts +97 -0
  38. package/dist/families/trigger.js +46 -0
  39. package/dist/families/view.d.ts +255 -0
  40. package/dist/families/view.js +87 -0
  41. package/dist/families/viewer.d.ts +31 -0
  42. package/dist/families/viewer.js +26 -0
  43. package/dist/families/worker.d.ts +23 -0
  44. package/dist/families/worker.js +19 -0
  45. package/dist/host-client.d.ts +11 -0
  46. package/dist/host-client.js +30 -0
  47. package/dist/index.d.ts +36 -0
  48. package/dist/index.js +36 -0
  49. package/dist/manifest/branding.d.ts +14 -0
  50. package/dist/manifest/branding.js +18 -0
  51. package/dist/manifest/connections.d.ts +21 -0
  52. package/dist/manifest/connections.js +10 -0
  53. package/dist/manifest/json-schema.d.ts +2 -0
  54. package/dist/manifest/json-schema.js +13 -0
  55. package/dist/manifest/l10n.d.ts +9 -0
  56. package/dist/manifest/l10n.js +28 -0
  57. package/dist/manifest/manifest.d.ts +112 -0
  58. package/dist/manifest/manifest.js +45 -0
  59. package/dist/manifest/validate.d.ts +14 -0
  60. package/dist/manifest/validate.js +144 -0
  61. package/dist/permissions/permissions.d.ts +31 -0
  62. package/dist/permissions/permissions.js +53 -0
  63. package/dist/process-entry.d.ts +26 -0
  64. package/dist/process-entry.js +146 -0
  65. package/dist/protocol/envelope.d.ts +115 -0
  66. package/dist/protocol/envelope.js +72 -0
  67. package/dist/protocol/error.d.ts +67 -0
  68. package/dist/protocol/error.js +61 -0
  69. package/dist/protocol/handshake.d.ts +55 -0
  70. package/dist/protocol/handshake.js +26 -0
  71. package/dist/protocol/map.d.ts +1090 -0
  72. package/dist/protocol/map.js +127 -0
  73. package/dist/protocol/meta.d.ts +31 -0
  74. package/dist/protocol/meta.js +14 -0
  75. package/dist/protocol/method-spec.d.ts +13 -0
  76. package/dist/protocol/method-spec.js +9 -0
  77. package/dist/protocol/org-service-methods.d.ts +135 -0
  78. package/dist/protocol/org-service-methods.js +106 -0
  79. package/dist/protocol/shared.d.ts +389 -0
  80. package/dist/protocol/shared.js +231 -0
  81. package/dist/protocol/source-methods.d.ts +218 -0
  82. package/dist/protocol/source-methods.js +18 -0
  83. package/dist/protocol/thread-service-methods.d.ts +50 -0
  84. package/dist/protocol/thread-service-methods.js +32 -0
  85. package/dist/runtime.d.ts +25 -0
  86. package/dist/runtime.js +229 -0
  87. package/dist/testing/fake-db.d.ts +15 -0
  88. package/dist/testing/fake-db.js +120 -0
  89. package/dist/testing/fake-files.d.ts +9 -0
  90. package/dist/testing/fake-files.js +87 -0
  91. package/dist/testing/fake-host.d.ts +115 -0
  92. package/dist/testing/fake-host.js +217 -0
  93. package/dist/testing/fake-services.d.ts +63 -0
  94. package/dist/testing/fake-services.js +277 -0
  95. package/dist/testing/fixtures.d.ts +5 -0
  96. package/dist/testing/fixtures.js +101 -0
  97. package/dist/testing/index.d.ts +4 -0
  98. package/dist/testing/index.js +2 -0
  99. package/package.json +66 -4
  100. package/src/addon-context-factory.ts +236 -0
  101. package/src/addon-context.ts +313 -0
  102. package/src/define-addon.ts +178 -0
  103. package/src/families/config.ts +24 -0
  104. package/src/families/content.ts +35 -0
  105. package/src/families/field-spec.ts +175 -0
  106. package/src/families/hook.ts +34 -0
  107. package/src/families/job.ts +41 -0
  108. package/src/families/json-schema-value.ts +322 -0
  109. package/src/families/json-schema.ts +167 -0
  110. package/src/families/lsp.ts +28 -0
  111. package/src/families/mcp.ts +28 -0
  112. package/src/families/registry.ts +44 -0
  113. package/src/families/route.ts +52 -0
  114. package/src/families/source-kind.ts +34 -0
  115. package/src/families/tool-pack.ts +92 -0
  116. package/src/families/transport.ts +52 -0
  117. package/src/families/trigger.ts +92 -0
  118. package/src/families/view.ts +144 -0
  119. package/src/families/viewer.ts +43 -0
  120. package/src/families/worker.ts +30 -0
  121. package/src/host-client.ts +52 -0
  122. package/src/index.ts +345 -0
  123. package/src/manifest/branding.ts +24 -0
  124. package/src/manifest/connections.ts +21 -0
  125. package/src/manifest/json-schema.ts +24 -0
  126. package/src/manifest/l10n.ts +39 -0
  127. package/src/manifest/manifest.ts +94 -0
  128. package/src/manifest/validate.ts +200 -0
  129. package/src/permissions/permissions.ts +79 -0
  130. package/src/process-entry.ts +205 -0
  131. package/src/protocol/envelope.ts +126 -0
  132. package/src/protocol/error.ts +81 -0
  133. package/src/protocol/handshake.ts +43 -0
  134. package/src/protocol/map.ts +339 -0
  135. package/src/protocol/meta.ts +26 -0
  136. package/src/protocol/method-spec.ts +40 -0
  137. package/src/protocol/org-service-methods.ts +148 -0
  138. package/src/protocol/shared.ts +345 -0
  139. package/src/protocol/source-methods.ts +61 -0
  140. package/src/protocol/thread-service-methods.ts +50 -0
  141. package/src/runtime.ts +332 -0
  142. package/src/testing/fake-db.ts +140 -0
  143. package/src/testing/fake-files.ts +114 -0
  144. package/src/testing/fake-host.ts +350 -0
  145. package/src/testing/fake-services.ts +345 -0
  146. package/src/testing/fixtures.ts +106 -0
  147. package/src/testing/index.ts +12 -0
@@ -0,0 +1,43 @@
1
+ import { z } from 'zod';
2
+ import { EffectivePermissionSchema } from '../permissions/permissions.ts';
3
+
4
+ export interface InitializeParams {
5
+ platform: number;
6
+ host: { version: string; features: string[] };
7
+ instance: { orgId: string; generation: number; dataDir: string; locale: string };
8
+ permissions: Array<{ id: string; scope?: Record<string, unknown> }>;
9
+ experimental: string[];
10
+ deprecations: string[];
11
+ }
12
+
13
+ export interface InitializeResult {
14
+ addon: { id: string; version: string };
15
+ handlers: string[];
16
+ deactivateTimeoutMs?: number;
17
+ }
18
+
19
+ export const InitializeParamsSchema = z.object({
20
+ platform: z.number().int().positive(),
21
+ host: z.object({
22
+ version: z.string().min(1),
23
+ features: z.array(z.string()),
24
+ }),
25
+ instance: z.object({
26
+ orgId: z.string().min(1),
27
+ generation: z.number().int().nonnegative(),
28
+ dataDir: z.string().min(1),
29
+ locale: z.string().min(1),
30
+ }),
31
+ permissions: z.array(EffectivePermissionSchema),
32
+ experimental: z.array(z.string()),
33
+ deprecations: z.array(z.string()),
34
+ });
35
+
36
+ export const InitializeResultSchema = z.object({
37
+ addon: z.object({
38
+ id: z.string().min(1),
39
+ version: z.string().min(1),
40
+ }),
41
+ handlers: z.array(z.string()),
42
+ deactivateTimeoutMs: z.number().int().positive().optional(),
43
+ });
@@ -0,0 +1,339 @@
1
+ import { z } from 'zod';
2
+ import { EffectivePermissionSchema } from '../permissions/permissions.ts';
3
+ import { CancelNotificationSchema, ProgressNotificationSchema } from './envelope.ts';
4
+ import { InitializeParamsSchema, InitializeResultSchema } from './handshake.ts';
5
+ import { CallMetaSchema } from './meta.ts';
6
+ import type { MethodDirection, MethodSpec } from './method-spec.ts';
7
+ import { addonMethod, hostMethod, notificationSpec } from './method-spec.ts';
8
+ import { orgServiceMethods } from './org-service-methods.ts';
9
+ import {
10
+ AccountSummarySchema,
11
+ AppCredentialsSchema,
12
+ BindingChannelParamsSchema,
13
+ BindingWriteParamsSchema,
14
+ BridgeInboundOutcomeSchema,
15
+ DbRowSchema,
16
+ FileReadParamsSchema,
17
+ FileReadResultSchema,
18
+ FileStatParamsSchema,
19
+ FileStatResultSchema,
20
+ FileWriteParamsSchema,
21
+ FileWriteResultSchema,
22
+ HttpFetchParamsSchema,
23
+ HttpFetchResultSchema,
24
+ InboundMessageSchema,
25
+ LogWriteParamsSchema,
26
+ OkResultSchema,
27
+ RouteHandleResultSchema,
28
+ RouteRequestSchema,
29
+ SqlParamSchema,
30
+ TransportBindingSummarySchema,
31
+ TransportMessageSchema,
32
+ } from './shared.ts';
33
+ import { sourceMethods } from './source-methods.ts';
34
+ import { threadServiceMethods } from './thread-service-methods.ts';
35
+
36
+ export type { MethodDirection, MethodSpec, NotificationSender } from './method-spec.ts';
37
+ export { addonMethod, hostMethod, notificationSpec } from './method-spec.ts';
38
+
39
+ const empty = z.object({});
40
+ const str = z.string().min(1);
41
+ const secretKey = z.string().regex(/^[A-Za-z0-9_.-]{1,128}$/);
42
+ const MAX_SECRET_BYTES = 8 * 1024;
43
+
44
+ export const protocolMap = {
45
+ ...sourceMethods,
46
+ ...orgServiceMethods,
47
+ ...threadServiceMethods,
48
+ initialize: hostMethod(InitializeParamsSchema, InitializeResultSchema),
49
+ initialized: notificationSpec(empty, 'host'),
50
+ shutdown: hostMethod(
51
+ z
52
+ .object({ reason: z.string().min(1), deadlineMs: z.number().int().positive() })
53
+ .extend({ _meta: CallMetaSchema }),
54
+ empty,
55
+ ),
56
+ ping: hostMethod(empty.extend({ _meta: CallMetaSchema }), empty),
57
+ 'config.changed': notificationSpec(z.object({ keys: z.array(z.string()) }), 'host'),
58
+ 'permissions.changed': notificationSpec(
59
+ z.object({ permissions: z.array(EffectivePermissionSchema) }),
60
+ 'host',
61
+ ),
62
+ 'accounts.changed': notificationSpec(
63
+ z.object({ connection: str, accountId: str, status: z.string() }),
64
+ 'host',
65
+ ),
66
+ 'lifecycle.upgrade': hostMethod(
67
+ z.object({ fromVersion: str }).extend({ _meta: CallMetaSchema }),
68
+ empty,
69
+ ),
70
+ 'tools.call': hostMethod(
71
+ z
72
+ .object({ pack: str, tool: str, input: z.record(z.string(), z.unknown()).default({}) })
73
+ .extend({ _meta: CallMetaSchema }),
74
+ z.object({ output: z.unknown() }),
75
+ ),
76
+ 'route.handle': hostMethod(
77
+ z.object({ routeId: str, request: RouteRequestSchema }).extend({ _meta: CallMetaSchema }),
78
+ RouteHandleResultSchema,
79
+ ),
80
+ 'trigger.start': hostMethod(
81
+ z
82
+ .object({
83
+ instanceId: str,
84
+ kind: str,
85
+ label: str.optional(),
86
+ config: z.record(z.string(), z.unknown()),
87
+ target: z.unknown().optional(),
88
+ })
89
+ .extend({ _meta: CallMetaSchema }),
90
+ empty,
91
+ 'trigger',
92
+ ),
93
+ 'trigger.stop': hostMethod(
94
+ z
95
+ .object({
96
+ instanceId: str,
97
+ reason: z.enum(['paused', 'removed']).optional(),
98
+ })
99
+ .extend({ _meta: CallMetaSchema }),
100
+ empty,
101
+ 'trigger',
102
+ ),
103
+ 'job.run': hostMethod(z.object({ jobId: str }).extend({ _meta: CallMetaSchema }), empty),
104
+ 'worker.start': hostMethod(z.object({ workerId: str }).extend({ _meta: CallMetaSchema }), empty),
105
+ 'worker.stop': hostMethod(z.object({ workerId: str }).extend({ _meta: CallMetaSchema }), empty),
106
+ 'transport.send': hostMethod(
107
+ z.object({ message: TransportMessageSchema }).extend({ _meta: CallMetaSchema }),
108
+ z.object({ messageId: str.optional() }),
109
+ ),
110
+ 'transport.edit': hostMethod(
111
+ z.object({ messageId: str, message: TransportMessageSchema }).extend({ _meta: CallMetaSchema }),
112
+ empty,
113
+ ),
114
+ 'transport.typing': hostMethod(
115
+ z.object({ channel: str, on: z.boolean() }).extend({ _meta: CallMetaSchema }),
116
+ empty,
117
+ ),
118
+ 'hook.deliver': hostMethod(
119
+ z
120
+ .object({ hookId: str, event: z.record(z.string(), z.unknown()) })
121
+ .extend({ _meta: CallMetaSchema }),
122
+ empty,
123
+ ),
124
+ 'config.get': addonMethod(
125
+ z.object({ keys: z.array(z.string()).optional() }),
126
+ z.object({ values: z.record(z.string(), z.unknown()) }),
127
+ null,
128
+ ),
129
+ 'secrets.get': addonMethod(
130
+ z.object({ key: secretKey }),
131
+ z.object({ value: z.string().nullable() }),
132
+ null,
133
+ ),
134
+ 'secrets.set': addonMethod(
135
+ z.object({ key: secretKey, value: z.string().min(1).max(MAX_SECRET_BYTES) }),
136
+ empty,
137
+ null,
138
+ ),
139
+ 'secrets.delete': addonMethod(z.object({ key: secretKey }), empty, null),
140
+ 'db.exec': addonMethod(z.object({ sql: str }), empty, null),
141
+ 'db.all': addonMethod(
142
+ z.object({ sql: str, params: z.array(SqlParamSchema).optional() }),
143
+ z.object({ rows: z.array(DbRowSchema) }),
144
+ null,
145
+ ),
146
+ 'db.get': addonMethod(
147
+ z.object({ sql: str, params: z.array(SqlParamSchema).optional() }),
148
+ z.object({ row: DbRowSchema.nullable() }),
149
+ null,
150
+ ),
151
+ 'db.run': addonMethod(
152
+ z.object({ sql: str, params: z.array(SqlParamSchema).optional() }),
153
+ z.object({
154
+ changes: z.number().int().nonnegative(),
155
+ lastInsertRowid: z.union([z.string(), z.number()]),
156
+ }),
157
+ null,
158
+ ),
159
+ 'db.batch': addonMethod(
160
+ z.object({
161
+ statements: z
162
+ .array(z.object({ sql: str, params: z.array(SqlParamSchema).optional() }))
163
+ .max(100),
164
+ }),
165
+ z.object({
166
+ results: z.array(
167
+ z.object({
168
+ changes: z.number().int().nonnegative(),
169
+ lastInsertRowid: z.union([z.string(), z.number()]),
170
+ }),
171
+ ),
172
+ }),
173
+ null,
174
+ ),
175
+ 'log.write': addonMethod(LogWriteParamsSchema, empty, null),
176
+ 'events.emit': addonMethod(z.object({ name: str, payload: z.unknown() }), empty, null),
177
+ 'http.fetch': addonMethod(HttpFetchParamsSchema, HttpFetchResultSchema, 'http.fetch'),
178
+ 'files.stat': addonMethod(FileStatParamsSchema, FileStatResultSchema, 'files.read'),
179
+ 'files.read': addonMethod(FileReadParamsSchema, FileReadResultSchema, 'files.read'),
180
+ 'files.write': addonMethod(FileWriteParamsSchema, FileWriteResultSchema, 'files.write'),
181
+ 'bindings.channels': addonMethod(
182
+ z.object({ provider: str.optional() }),
183
+ z.object({ channels: z.array(TransportBindingSummarySchema) }),
184
+ null,
185
+ ),
186
+ 'bindings.bind': addonMethod(BindingWriteParamsSchema, OkResultSchema, 'bindings.write'),
187
+ 'bindings.unbind': addonMethod(BindingChannelParamsSchema, OkResultSchema, 'bindings.write'),
188
+ 'accounts.list': addonMethod(
189
+ z.object({ connection: str }),
190
+ z.object({ accounts: z.array(AccountSummarySchema) }),
191
+ 'accounts.use',
192
+ ),
193
+ 'accounts.report': addonMethod(
194
+ z.object({ connection: str, accountId: str.optional(), code: str }),
195
+ empty,
196
+ 'accounts.use',
197
+ ),
198
+ 'accounts.access': addonMethod(
199
+ z.object({ connection: str, accountId: str.optional() }),
200
+ z.object({
201
+ credentials: z.unknown(),
202
+ expiresAt: str.optional(),
203
+ app: AppCredentialsSchema.nullable().optional(),
204
+ }),
205
+ 'accounts.credentials',
206
+ ),
207
+ 'runs.dispatch': addonMethod(
208
+ z.object({ threadId: str.optional(), agentId: str.optional(), input: str }),
209
+ z.object({ runId: str }),
210
+ 'runs.dispatch',
211
+ ),
212
+ 'runs.cancel': addonMethod(
213
+ z.object({ threadId: str, runId: str }),
214
+ z.object({ ok: z.literal(true) }),
215
+ 'runs.dispatch',
216
+ ),
217
+ 'bridge.inbound': addonMethod(
218
+ z.object({ message: InboundMessageSchema }),
219
+ z.object({ outcome: BridgeInboundOutcomeSchema }),
220
+ 'bridge.inbound',
221
+ ),
222
+ 'workers.lease': addonMethod(
223
+ z.object({
224
+ workerId: str,
225
+ ttlMs: z.number().int().min(5_000).max(3_600_000).optional(),
226
+ }),
227
+ z.object({
228
+ held: z.boolean(),
229
+ epoch: z.number().int().nonnegative(),
230
+ expiresAt: z.string().nullable(),
231
+ }),
232
+ 'worker',
233
+ ),
234
+ 'workers.release': addonMethod(z.object({ workerId: str }), empty, 'worker'),
235
+ 'workers.report': addonMethod(
236
+ z.object({
237
+ workerId: str,
238
+ state: z.enum(['running', 'stopped', 'failed']),
239
+ code: str.optional(),
240
+ }),
241
+ empty,
242
+ 'worker',
243
+ ),
244
+ '$/cancel': notificationSpec(CancelNotificationSchema, 'both'),
245
+ '$/progress': notificationSpec(ProgressNotificationSchema, 'addon'),
246
+ };
247
+
248
+ export type ProtocolMethodMap = typeof protocolMap;
249
+ export const PROTOCOL_METHODS = Object.keys(protocolMap);
250
+
251
+ export type ParamsOf<S> = S extends { readonly params: infer SParams }
252
+ ? SParams extends z.ZodType
253
+ ? z.output<SParams>
254
+ : never
255
+ : never;
256
+ export type ResultOf<S> = S extends { readonly result: infer SResult }
257
+ ? SResult extends null
258
+ ? undefined
259
+ : SResult extends z.ZodType
260
+ ? z.output<SResult>
261
+ : never
262
+ : never;
263
+ export type DirectionOf<S> = S extends { readonly direction: infer D } ? D : never;
264
+
265
+ export type MethodsOf<D extends MethodDirection> = {
266
+ [K in keyof ProtocolMethodMap as DirectionOf<ProtocolMethodMap[K]> extends D
267
+ ? K
268
+ : never]: ProtocolMethodMap[K];
269
+ };
270
+
271
+ export type HostToAddonMethods = MethodsOf<'host-to-addon'>;
272
+ export type AddonToHostMethods = MethodsOf<'addon-to-host'>;
273
+ export type NotificationMethods = MethodsOf<'notification'>;
274
+
275
+ export type HostRequestName = keyof HostToAddonMethods;
276
+ export type HostServiceName = keyof AddonToHostMethods;
277
+ export type NotificationName = keyof NotificationMethods;
278
+
279
+ export type HostRequestParams<M extends HostRequestName> = ParamsOf<HostToAddonMethods[M]>;
280
+ export type HostRequestResult<M extends HostRequestName> = ResultOf<HostToAddonMethods[M]>;
281
+ export type HostServiceParams<M extends HostServiceName> = ParamsOf<AddonToHostMethods[M]>;
282
+ export type HostServiceResult<M extends HostServiceName> = ResultOf<AddonToHostMethods[M]>;
283
+
284
+ export type AddonDispatcher = {
285
+ [K in keyof HostToAddonMethods]: (
286
+ params: ParamsOf<HostToAddonMethods[K]>,
287
+ ) => Promise<ResultOf<HostToAddonMethods[K]>>;
288
+ };
289
+
290
+ export type HostServiceClient = {
291
+ [K in keyof AddonToHostMethods]: (
292
+ params: Omit<ParamsOf<AddonToHostMethods[K]>, '_meta'>,
293
+ options?: { signal?: AbortSignal },
294
+ ) => Promise<ResultOf<AddonToHostMethods[K]>>;
295
+ };
296
+
297
+ export type AddonHandlerOf<S, Ctx> = S extends { readonly params: infer SParams }
298
+ ? SParams extends z.ZodType
299
+ ? (params: z.output<SParams>, ctx: Ctx) => ResultOf<S> | Promise<ResultOf<S>>
300
+ : never
301
+ : never;
302
+
303
+ export type HostNotify = {
304
+ [K in keyof NotificationMethods as NotificationMethods[K] extends { sender: infer S }
305
+ ? S extends 'host' | 'both'
306
+ ? K
307
+ : never
308
+ : never]: (params: ParamsOf<NotificationMethods[K]>) => void;
309
+ };
310
+
311
+ export type AddonNotify = {
312
+ [K in keyof NotificationMethods as NotificationMethods[K] extends { sender: infer S }
313
+ ? S extends 'addon' | 'both'
314
+ ? K
315
+ : never
316
+ : never]: (params: ParamsOf<NotificationMethods[K]>) => void;
317
+ };
318
+
319
+ export function stripMeta<P extends { _meta?: unknown }>(params: P): Omit<P, '_meta'> {
320
+ const copy = { ...params };
321
+ delete copy._meta;
322
+ return copy;
323
+ }
324
+
325
+ export interface ProtocolEntry {
326
+ method: string;
327
+ spec: MethodSpec<z.ZodType, z.ZodType | null, MethodDirection>;
328
+ }
329
+
330
+ export function protocolEntries(): ProtocolEntry[] {
331
+ return Object.entries(protocolMap).map(([method, spec]) => ({ method, spec }) as ProtocolEntry);
332
+ }
333
+
334
+ export function methodSpec(
335
+ method: string,
336
+ ): MethodSpec<z.ZodType, z.ZodType | null, MethodDirection> | undefined {
337
+ const spec: unknown = protocolMap[method as keyof ProtocolMethodMap];
338
+ return spec as MethodSpec<z.ZodType, z.ZodType | null, MethodDirection> | undefined;
339
+ }
@@ -0,0 +1,26 @@
1
+ import { z } from 'zod';
2
+
3
+ export interface CallMeta {
4
+ correlationId: string;
5
+ deadline: number | null;
6
+ principal: { type: 'agent' | 'user' | 'system'; id?: string };
7
+ agentId?: string;
8
+ threadId?: string;
9
+ runId?: string;
10
+ orgId?: string;
11
+ locale: string;
12
+ }
13
+
14
+ export const CallMetaSchema = z.object({
15
+ correlationId: z.string().min(1),
16
+ deadline: z.number().nullable(),
17
+ principal: z.object({
18
+ type: z.enum(['agent', 'user', 'system']),
19
+ id: z.string().optional(),
20
+ }),
21
+ agentId: z.string().optional(),
22
+ threadId: z.string().optional(),
23
+ runId: z.string().optional(),
24
+ orgId: z.string().optional(),
25
+ locale: z.string().min(1),
26
+ });
@@ -0,0 +1,40 @@
1
+ import type { z } from 'zod';
2
+
3
+ export type MethodDirection = 'host-to-addon' | 'addon-to-host' | 'notification';
4
+ export type NotificationSender = 'host' | 'addon' | 'both';
5
+
6
+ export interface MethodSpec<
7
+ SParams extends z.ZodType,
8
+ SResult extends z.ZodType | null,
9
+ D extends MethodDirection = MethodDirection,
10
+ > {
11
+ readonly direction: D;
12
+ readonly params: SParams;
13
+ readonly result: SResult;
14
+ readonly permission: string | null;
15
+ readonly sender?: NotificationSender;
16
+ }
17
+
18
+ export function hostMethod<SParams extends z.ZodType, SResult extends z.ZodType>(
19
+ params: SParams,
20
+ result: SResult,
21
+ permission: string | null = null,
22
+ ): MethodSpec<SParams, SResult, 'host-to-addon'> {
23
+ return { direction: 'host-to-addon', params, result, permission };
24
+ }
25
+
26
+ export function addonMethod<SParams extends z.ZodType, SResult extends z.ZodType>(
27
+ params: SParams,
28
+ result: SResult,
29
+ permission: string | null,
30
+ ): MethodSpec<SParams, SResult, 'addon-to-host'> {
31
+ return { direction: 'addon-to-host', params, result, permission };
32
+ }
33
+
34
+ export function notificationSpec<SParams extends z.ZodType>(
35
+ params: SParams,
36
+ sender: NotificationSender,
37
+ permission: string | null = null,
38
+ ): MethodSpec<SParams, null, 'notification'> {
39
+ return { direction: 'notification', params, result: null, permission, sender };
40
+ }
@@ -0,0 +1,148 @@
1
+ import { z } from 'zod';
2
+ import { addonMethod } from './method-spec.ts';
3
+ import { TriggerFireOutcomeSchema } from './shared.ts';
4
+
5
+ const str = z.string().min(1);
6
+
7
+ /** Read/action methods over org-scoped thread, run, usage and skill state (§6.4). */
8
+ export const orgServiceMethods = {
9
+ 'runs.list': addonMethod(
10
+ z.object({ threadId: str, status: z.string().optional() }),
11
+ z.object({
12
+ runs: z.array(
13
+ z.object({
14
+ runId: str,
15
+ status: z.string(),
16
+ startedAt: z.string().nullable(),
17
+ agentId: z.string().nullable(),
18
+ }),
19
+ ),
20
+ }),
21
+ 'runs.read',
22
+ ),
23
+ 'threads.compact': addonMethod(
24
+ z.object({ threadId: str }),
25
+ z.union([
26
+ z.object({
27
+ compacted: z.literal(true),
28
+ id: str,
29
+ coveredFrom: z.number(),
30
+ coveredUntil: z.number(),
31
+ tokensBefore: z.number(),
32
+ tokensAfter: z.number(),
33
+ }),
34
+ z.object({ compacted: z.literal(false), reason: z.string().optional() }),
35
+ ]),
36
+ 'threads.compact',
37
+ ),
38
+ 'usage.summary': addonMethod(
39
+ z.object({ threadId: str.optional(), since: str.optional() }),
40
+ z.object({
41
+ inputTokens: z.number(),
42
+ outputTokens: z.number(),
43
+ costUsd: z.number(),
44
+ byModel: z.array(
45
+ z.object({
46
+ model: str,
47
+ inputTokens: z.number(),
48
+ outputTokens: z.number(),
49
+ costUsd: z.number(),
50
+ }),
51
+ ),
52
+ }),
53
+ 'usage.read',
54
+ ),
55
+ 'skills.list': addonMethod(
56
+ z.object({ threadId: str.optional() }),
57
+ z.object({
58
+ skills: z.array(
59
+ z.object({
60
+ id: str,
61
+ name: str,
62
+ description: z.string().nullable(),
63
+ source: z.string(),
64
+ }),
65
+ ),
66
+ }),
67
+ 'skills.read',
68
+ ),
69
+ 'org.settings.get': addonMethod(z.object({}), z.object({ timezone: str }), 'org.read'),
70
+ 'approvals.request': addonMethod(
71
+ z.object({
72
+ interruptId: str,
73
+ agentId: str,
74
+ threadId: str.optional(),
75
+ kind: z.enum(['tool', 'spend', 'memory', 'question', 'file_access']),
76
+ question: str,
77
+ options: z.array(z.enum(['approve', 'approve_once', 'deny', 'block_path', 'close'])).min(1),
78
+ context: z.object({
79
+ source: str,
80
+ ops: z.array(str).optional(),
81
+ tool: z
82
+ .object({
83
+ name: str,
84
+ inputPreview: z.string().optional(),
85
+ command: z.string().optional(),
86
+ truncated: z.boolean().optional(),
87
+ })
88
+ .optional(),
89
+ note: z.string().optional(),
90
+ }),
91
+ }),
92
+ z.object({ id: str }),
93
+ 'approvals.request',
94
+ ),
95
+ 'triggers.fire': addonMethod(
96
+ z.object({
97
+ instanceId: str,
98
+ runKey: str,
99
+ text: z.string().optional(),
100
+ title: z.string().optional(),
101
+ fields: z
102
+ .array(
103
+ z.object({
104
+ label: str,
105
+ value: str,
106
+ href: str.optional(),
107
+ }),
108
+ )
109
+ .optional(),
110
+ payload: z.unknown().optional(),
111
+ }),
112
+ TriggerFireOutcomeSchema,
113
+ 'trigger',
114
+ ),
115
+ 'triggers.create': addonMethod(
116
+ z.object({
117
+ kind: str,
118
+ label: str,
119
+ config: z.record(z.string(), z.unknown()).default({}),
120
+ target: z.object({
121
+ threadId: str,
122
+ agentId: str,
123
+ modeId: str.optional(),
124
+ }),
125
+ instructions: str.optional(),
126
+ }),
127
+ z.object({ instanceId: str }),
128
+ 'trigger',
129
+ ),
130
+ 'triggers.update': addonMethod(
131
+ z.object({
132
+ instanceId: str,
133
+ label: str.optional(),
134
+ config: z.record(z.string(), z.unknown()).optional(),
135
+ target: z
136
+ .object({
137
+ threadId: str,
138
+ agentId: str,
139
+ modeId: str.optional(),
140
+ })
141
+ .optional(),
142
+ instructions: str.optional(),
143
+ enabled: z.boolean().optional(),
144
+ }),
145
+ z.object({ instanceId: str }),
146
+ 'trigger',
147
+ ),
148
+ };