@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,218 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Knowledge-source backend calls: the host mirror pipeline drives an add-on through
4
+ * these methods and the add-on owns vendor auth and API calls.
5
+ */
6
+ export declare const sourceMethods: {
7
+ 'source.list': import("./method-spec.ts").MethodSpec<z.ZodObject<{
8
+ folderId: z.ZodString;
9
+ pageToken: z.ZodOptional<z.ZodString>;
10
+ sourceKind: z.ZodString;
11
+ accountId: z.ZodOptional<z.ZodString>;
12
+ _meta: z.ZodObject<{
13
+ correlationId: z.ZodString;
14
+ deadline: z.ZodNullable<z.ZodNumber>;
15
+ principal: z.ZodObject<{
16
+ type: z.ZodEnum<{
17
+ agent: "agent";
18
+ user: "user";
19
+ system: "system";
20
+ }>;
21
+ id: z.ZodOptional<z.ZodString>;
22
+ }, z.core.$strip>;
23
+ agentId: z.ZodOptional<z.ZodString>;
24
+ threadId: z.ZodOptional<z.ZodString>;
25
+ runId: z.ZodOptional<z.ZodString>;
26
+ orgId: z.ZodOptional<z.ZodString>;
27
+ locale: z.ZodString;
28
+ }, z.core.$strip>;
29
+ }, z.core.$strip>, z.ZodObject<{
30
+ entries: z.ZodArray<z.ZodObject<{
31
+ id: z.ZodString;
32
+ name: z.ZodString;
33
+ kind: z.ZodEnum<{
34
+ file: "file";
35
+ folder: "folder";
36
+ }>;
37
+ size: z.ZodOptional<z.ZodNumber>;
38
+ modifiedAt: z.ZodOptional<z.ZodString>;
39
+ mimeType: z.ZodOptional<z.ZodString>;
40
+ export: z.ZodOptional<z.ZodObject<{
41
+ mimeType: z.ZodString;
42
+ extension: z.ZodString;
43
+ }, z.core.$strip>>;
44
+ }, z.core.$strip>>;
45
+ nextPageToken: z.ZodOptional<z.ZodString>;
46
+ }, z.core.$strip>, "host-to-addon">;
47
+ 'source.stat': import("./method-spec.ts").MethodSpec<z.ZodObject<{
48
+ fileId: z.ZodString;
49
+ sourceKind: z.ZodString;
50
+ accountId: z.ZodOptional<z.ZodString>;
51
+ _meta: z.ZodObject<{
52
+ correlationId: z.ZodString;
53
+ deadline: z.ZodNullable<z.ZodNumber>;
54
+ principal: z.ZodObject<{
55
+ type: z.ZodEnum<{
56
+ agent: "agent";
57
+ user: "user";
58
+ system: "system";
59
+ }>;
60
+ id: z.ZodOptional<z.ZodString>;
61
+ }, z.core.$strip>;
62
+ agentId: z.ZodOptional<z.ZodString>;
63
+ threadId: z.ZodOptional<z.ZodString>;
64
+ runId: z.ZodOptional<z.ZodString>;
65
+ orgId: z.ZodOptional<z.ZodString>;
66
+ locale: z.ZodString;
67
+ }, z.core.$strip>;
68
+ }, z.core.$strip>, z.ZodObject<{
69
+ entry: z.ZodObject<{
70
+ id: z.ZodString;
71
+ name: z.ZodString;
72
+ kind: z.ZodEnum<{
73
+ file: "file";
74
+ folder: "folder";
75
+ }>;
76
+ size: z.ZodOptional<z.ZodNumber>;
77
+ modifiedAt: z.ZodOptional<z.ZodString>;
78
+ mimeType: z.ZodOptional<z.ZodString>;
79
+ export: z.ZodOptional<z.ZodObject<{
80
+ mimeType: z.ZodString;
81
+ extension: z.ZodString;
82
+ }, z.core.$strip>>;
83
+ }, z.core.$strip>;
84
+ }, z.core.$strip>, "host-to-addon">;
85
+ 'source.download': import("./method-spec.ts").MethodSpec<z.ZodObject<{
86
+ fileId: z.ZodString;
87
+ offset: z.ZodOptional<z.ZodNumber>;
88
+ length: z.ZodOptional<z.ZodNumber>;
89
+ sourceKind: z.ZodString;
90
+ accountId: z.ZodOptional<z.ZodString>;
91
+ _meta: z.ZodObject<{
92
+ correlationId: z.ZodString;
93
+ deadline: z.ZodNullable<z.ZodNumber>;
94
+ principal: z.ZodObject<{
95
+ type: z.ZodEnum<{
96
+ agent: "agent";
97
+ user: "user";
98
+ system: "system";
99
+ }>;
100
+ id: z.ZodOptional<z.ZodString>;
101
+ }, z.core.$strip>;
102
+ agentId: z.ZodOptional<z.ZodString>;
103
+ threadId: z.ZodOptional<z.ZodString>;
104
+ runId: z.ZodOptional<z.ZodString>;
105
+ orgId: z.ZodOptional<z.ZodString>;
106
+ locale: z.ZodString;
107
+ }, z.core.$strip>;
108
+ }, z.core.$strip>, z.ZodObject<{
109
+ data: z.ZodString;
110
+ eof: z.ZodBoolean;
111
+ }, z.core.$strip>, "host-to-addon">;
112
+ 'source.upload': import("./method-spec.ts").MethodSpec<z.ZodObject<{
113
+ parentId: z.ZodString;
114
+ fileId: z.ZodOptional<z.ZodString>;
115
+ name: z.ZodString;
116
+ data: z.ZodString;
117
+ mimeType: z.ZodOptional<z.ZodString>;
118
+ sourceKind: z.ZodString;
119
+ accountId: z.ZodOptional<z.ZodString>;
120
+ _meta: z.ZodObject<{
121
+ correlationId: z.ZodString;
122
+ deadline: z.ZodNullable<z.ZodNumber>;
123
+ principal: z.ZodObject<{
124
+ type: z.ZodEnum<{
125
+ agent: "agent";
126
+ user: "user";
127
+ system: "system";
128
+ }>;
129
+ id: z.ZodOptional<z.ZodString>;
130
+ }, z.core.$strip>;
131
+ agentId: z.ZodOptional<z.ZodString>;
132
+ threadId: z.ZodOptional<z.ZodString>;
133
+ runId: z.ZodOptional<z.ZodString>;
134
+ orgId: z.ZodOptional<z.ZodString>;
135
+ locale: z.ZodString;
136
+ }, z.core.$strip>;
137
+ }, z.core.$strip>, z.ZodObject<{
138
+ id: z.ZodString;
139
+ name: z.ZodString;
140
+ modifiedAt: z.ZodOptional<z.ZodString>;
141
+ }, z.core.$strip>, "host-to-addon">;
142
+ 'source.mkdir': import("./method-spec.ts").MethodSpec<z.ZodObject<{
143
+ parentId: z.ZodString;
144
+ name: z.ZodString;
145
+ sourceKind: z.ZodString;
146
+ accountId: z.ZodOptional<z.ZodString>;
147
+ _meta: z.ZodObject<{
148
+ correlationId: z.ZodString;
149
+ deadline: z.ZodNullable<z.ZodNumber>;
150
+ principal: z.ZodObject<{
151
+ type: z.ZodEnum<{
152
+ agent: "agent";
153
+ user: "user";
154
+ system: "system";
155
+ }>;
156
+ id: z.ZodOptional<z.ZodString>;
157
+ }, z.core.$strip>;
158
+ agentId: z.ZodOptional<z.ZodString>;
159
+ threadId: z.ZodOptional<z.ZodString>;
160
+ runId: z.ZodOptional<z.ZodString>;
161
+ orgId: z.ZodOptional<z.ZodString>;
162
+ locale: z.ZodString;
163
+ }, z.core.$strip>;
164
+ }, z.core.$strip>, z.ZodObject<{
165
+ id: z.ZodString;
166
+ name: z.ZodString;
167
+ }, z.core.$strip>, "host-to-addon">;
168
+ 'source.remove': import("./method-spec.ts").MethodSpec<z.ZodObject<{
169
+ fileId: z.ZodString;
170
+ sourceKind: z.ZodString;
171
+ accountId: z.ZodOptional<z.ZodString>;
172
+ _meta: z.ZodObject<{
173
+ correlationId: z.ZodString;
174
+ deadline: z.ZodNullable<z.ZodNumber>;
175
+ principal: z.ZodObject<{
176
+ type: z.ZodEnum<{
177
+ agent: "agent";
178
+ user: "user";
179
+ system: "system";
180
+ }>;
181
+ id: z.ZodOptional<z.ZodString>;
182
+ }, z.core.$strip>;
183
+ agentId: z.ZodOptional<z.ZodString>;
184
+ threadId: z.ZodOptional<z.ZodString>;
185
+ runId: z.ZodOptional<z.ZodString>;
186
+ orgId: z.ZodOptional<z.ZodString>;
187
+ locale: z.ZodString;
188
+ }, z.core.$strip>;
189
+ }, z.core.$strip>, z.ZodObject<{}, z.core.$strip>, "host-to-addon">;
190
+ 'source.export': import("./method-spec.ts").MethodSpec<z.ZodObject<{
191
+ fileId: z.ZodString;
192
+ mimeType: z.ZodString;
193
+ offset: z.ZodOptional<z.ZodNumber>;
194
+ length: z.ZodOptional<z.ZodNumber>;
195
+ sourceKind: z.ZodString;
196
+ accountId: z.ZodOptional<z.ZodString>;
197
+ _meta: z.ZodObject<{
198
+ correlationId: z.ZodString;
199
+ deadline: z.ZodNullable<z.ZodNumber>;
200
+ principal: z.ZodObject<{
201
+ type: z.ZodEnum<{
202
+ agent: "agent";
203
+ user: "user";
204
+ system: "system";
205
+ }>;
206
+ id: z.ZodOptional<z.ZodString>;
207
+ }, z.core.$strip>;
208
+ agentId: z.ZodOptional<z.ZodString>;
209
+ threadId: z.ZodOptional<z.ZodString>;
210
+ runId: z.ZodOptional<z.ZodString>;
211
+ orgId: z.ZodOptional<z.ZodString>;
212
+ locale: z.ZodString;
213
+ }, z.core.$strip>;
214
+ }, z.core.$strip>, z.ZodObject<{
215
+ data: z.ZodString;
216
+ eof: z.ZodBoolean;
217
+ }, z.core.$strip>, "host-to-addon">;
218
+ };
@@ -0,0 +1,18 @@
1
+ import { z } from 'zod';
2
+ import { CallMetaSchema } from "./meta.js";
3
+ import { hostMethod } from "./method-spec.js";
4
+ import { SourceBytesResultSchema, SourceExportParamsSchema, SourceListParamsSchema, SourceListResultSchema, SourceMkdirParamsSchema, SourceMkdirResultSchema, SourceReadParamsSchema, SourceRemoveParamsSchema, SourceStatParamsSchema, SourceStatResultSchema, SourceUploadParamsSchema, SourceUploadResultSchema, } from "./shared.js";
5
+ const empty = z.object({});
6
+ /**
7
+ * Knowledge-source backend calls: the host mirror pipeline drives an add-on through
8
+ * these methods and the add-on owns vendor auth and API calls.
9
+ */
10
+ export const sourceMethods = {
11
+ 'source.list': hostMethod(SourceListParamsSchema.extend({ _meta: CallMetaSchema }), SourceListResultSchema, 'source'),
12
+ 'source.stat': hostMethod(SourceStatParamsSchema.extend({ _meta: CallMetaSchema }), SourceStatResultSchema, 'source'),
13
+ 'source.download': hostMethod(SourceReadParamsSchema.extend({ _meta: CallMetaSchema }), SourceBytesResultSchema, 'source'),
14
+ 'source.upload': hostMethod(SourceUploadParamsSchema.extend({ _meta: CallMetaSchema }), SourceUploadResultSchema, 'source'),
15
+ 'source.mkdir': hostMethod(SourceMkdirParamsSchema.extend({ _meta: CallMetaSchema }), SourceMkdirResultSchema, 'source'),
16
+ 'source.remove': hostMethod(SourceRemoveParamsSchema.extend({ _meta: CallMetaSchema }), empty, 'source'),
17
+ 'source.export': hostMethod(SourceExportParamsSchema.extend({ _meta: CallMetaSchema }), SourceBytesResultSchema, 'source'),
18
+ };
@@ -0,0 +1,50 @@
1
+ import { z } from 'zod';
2
+ /** Threads read/write and the agents roster: addon-to-host service methods. */
3
+ export declare const threadServiceMethods: {
4
+ 'threads.create': import("./method-spec.ts").MethodSpec<z.ZodObject<{
5
+ title: z.ZodOptional<z.ZodString>;
6
+ agentId: z.ZodString;
7
+ kind: z.ZodOptional<z.ZodEnum<{
8
+ chat: "chat";
9
+ group: "group";
10
+ }>>;
11
+ responderMode: z.ZodOptional<z.ZodEnum<{
12
+ auto: "auto";
13
+ mention: "mention";
14
+ }>>;
15
+ }, z.core.$strip>, z.ZodObject<{
16
+ threadId: z.ZodString;
17
+ }, z.core.$strip>, "addon-to-host">;
18
+ 'threads.get': import("./method-spec.ts").MethodSpec<z.ZodObject<{
19
+ threadId: z.ZodString;
20
+ }, z.core.$strip>, z.ZodObject<{
21
+ thread: z.ZodOptional<z.ZodObject<{
22
+ threadId: z.ZodString;
23
+ kind: z.ZodString;
24
+ title: z.ZodString;
25
+ responderMode: z.ZodEnum<{
26
+ auto: "auto";
27
+ mention: "mention";
28
+ }>;
29
+ autoReplyAgentId: z.ZodNullable<z.ZodString>;
30
+ agents: z.ZodArray<z.ZodObject<{
31
+ id: z.ZodString;
32
+ name: z.ZodString;
33
+ }, z.core.$strip>>;
34
+ }, z.core.$strip>>;
35
+ }, z.core.$strip>, "addon-to-host">;
36
+ 'threads.patch': import("./method-spec.ts").MethodSpec<z.ZodObject<{
37
+ threadId: z.ZodString;
38
+ responderMode: z.ZodOptional<z.ZodEnum<{
39
+ auto: "auto";
40
+ mention: "mention";
41
+ }>>;
42
+ }, z.core.$strip>, z.ZodObject<{}, z.core.$strip>, "addon-to-host">;
43
+ 'agents.list': import("./method-spec.ts").MethodSpec<z.ZodObject<{}, z.core.$strip>, z.ZodObject<{
44
+ agents: z.ZodArray<z.ZodObject<{
45
+ id: z.ZodString;
46
+ name: z.ZodString;
47
+ status: z.ZodOptional<z.ZodString>;
48
+ }, z.core.$strip>>;
49
+ }, z.core.$strip>, "addon-to-host">;
50
+ };
@@ -0,0 +1,32 @@
1
+ import { z } from 'zod';
2
+ import { addonMethod } from "./method-spec.js";
3
+ const str = z.string();
4
+ const empty = z.object({});
5
+ /** Threads read/write and the agents roster: addon-to-host service methods. */
6
+ export const threadServiceMethods = {
7
+ 'threads.create': addonMethod(z.object({
8
+ title: str.optional(),
9
+ agentId: z.string().uuid(),
10
+ kind: z.enum(['chat', 'group']).optional(),
11
+ responderMode: z.enum(['auto', 'mention']).optional(),
12
+ }), z.object({ threadId: str }), 'threads.create'),
13
+ 'threads.get': addonMethod(z.object({ threadId: str }), z.object({
14
+ thread: z
15
+ .object({
16
+ threadId: str,
17
+ kind: str,
18
+ title: str,
19
+ responderMode: z.enum(['auto', 'mention']),
20
+ autoReplyAgentId: str.nullable(),
21
+ agents: z.array(z.object({ id: str, name: str })),
22
+ })
23
+ .optional(),
24
+ }), 'threads.read'),
25
+ 'threads.patch': addonMethod(z.object({
26
+ threadId: str,
27
+ responderMode: z.enum(['auto', 'mention']).optional(),
28
+ }), empty, 'threads.write'),
29
+ 'agents.list': addonMethod(z.object({}), z.object({
30
+ agents: z.array(z.object({ id: str, name: str, status: str.optional() })),
31
+ }), 'agents.read'),
32
+ };
@@ -0,0 +1,25 @@
1
+ import type { ResolvedAddonDefinition } from './define-addon.ts';
2
+ import { type HostChannel } from './host-client.ts';
3
+ import { type AddonErrorReason } from './protocol/error.ts';
4
+ import { type InitializeParams, type InitializeResult } from './protocol/handshake.ts';
5
+ export interface AddonRuntimeOptions {
6
+ definition: ResolvedAddonDefinition;
7
+ addon: {
8
+ id: string;
9
+ version: string;
10
+ };
11
+ channel: HostChannel;
12
+ now?(): number;
13
+ }
14
+ export interface AddonRuntime {
15
+ readonly addon: {
16
+ id: string;
17
+ version: string;
18
+ };
19
+ readonly handlers: string[];
20
+ initialize(params: InitializeParams): InitializeResult;
21
+ handleRequest(method: string, params: unknown, signal: AbortSignal): Promise<unknown>;
22
+ handleNotification(method: string, params: unknown, signal: AbortSignal): Promise<void>;
23
+ }
24
+ export declare function createAddonRuntime(options: AddonRuntimeOptions): AddonRuntime;
25
+ export type { AddonErrorReason };
@@ -0,0 +1,229 @@
1
+ import { createAddonContextFor } from "./host-client.js";
2
+ import { AddonError } from "./protocol/error.js";
3
+ import { InitializeResultSchema, } from "./protocol/handshake.js";
4
+ import { methodSpec } from "./protocol/map.js";
5
+ import { CallMetaSchema } from "./protocol/meta.js";
6
+ export function createAddonRuntime(options) {
7
+ const { definition, addon, channel } = options;
8
+ const now = options.now ?? (() => Date.now());
9
+ const declared = new Set(definition.handlers);
10
+ const activeTriggerKinds = new Map();
11
+ let instanceOrgId;
12
+ function dispatch(method, params, ctx) {
13
+ const lookup = dispatchLookup(method, params, ctx);
14
+ if (lookup !== undefined) {
15
+ return lookup;
16
+ }
17
+ return dispatchManaged(method, params, ctx);
18
+ }
19
+ function dispatchLookup(method, params, ctx) {
20
+ if (method.startsWith('source.')) {
21
+ return sourceLookup(method, params, ctx);
22
+ }
23
+ const lookups = {
24
+ 'tools.call': () => toolTarget(params, ctx),
25
+ 'route.handle': () => handlerTarget(definition.routes?.[str(params.routeId)], params, ctx),
26
+ 'job.run': () => handlerTarget(definition.jobs?.[str(params.jobId)], params, ctx),
27
+ 'hook.deliver': () => handlerTarget(definition.hooks?.[str(params.hookId)], params, ctx),
28
+ };
29
+ return lookups[method]?.();
30
+ }
31
+ function toolTarget(params, ctx) {
32
+ const handler = definition.tools?.[str(params.tool)];
33
+ if (handler === undefined) {
34
+ return null;
35
+ }
36
+ return async () => ({ output: await handler(asInput(params.input), ctx) });
37
+ }
38
+ function sourceLookup(method, params, ctx) {
39
+ const op = method.slice('source.'.length);
40
+ const backend = definition.sources?.[str(params.sourceKind)];
41
+ const handler = backend?.[op];
42
+ return handler ? () => handler(withoutMeta(params), ctx) : null;
43
+ }
44
+ function dispatchManaged(method, params, ctx) {
45
+ switch (method) {
46
+ case 'trigger.start': {
47
+ const kind = str(params.kind);
48
+ if (kind) {
49
+ activeTriggerKinds.set(str(params.instanceId), kind);
50
+ }
51
+ return pairTarget(definition.triggers, kind, 'start', params, ctx);
52
+ }
53
+ case 'trigger.stop': {
54
+ const instanceId = str(params.instanceId);
55
+ const kind = activeTriggerKinds.get(instanceId) ?? '';
56
+ activeTriggerKinds.delete(instanceId);
57
+ return pairTarget(definition.triggers, kind, 'stop', params, ctx);
58
+ }
59
+ case 'worker.start':
60
+ return pairTarget(definition.workers, str(params.workerId), 'start', params, ctx);
61
+ case 'worker.stop':
62
+ return pairTarget(definition.workers, str(params.workerId), 'stop', params, ctx);
63
+ case 'transport.send':
64
+ return optionalTarget(definition.transport?.send, params, ctx);
65
+ case 'transport.edit':
66
+ return optionalTarget(definition.transport?.edit, params, ctx);
67
+ case 'transport.typing':
68
+ return optionalTarget(definition.transport?.typing, params, ctx);
69
+ case 'lifecycle.upgrade':
70
+ return optionalTarget(definition.lifecycle?.upgrade, params, ctx);
71
+ case 'ping':
72
+ case 'shutdown':
73
+ return () => ({});
74
+ default:
75
+ return null;
76
+ }
77
+ }
78
+ async function handleRequest(method, params, signal) {
79
+ const spec = methodSpec(method);
80
+ if (spec?.direction !== 'host-to-addon') {
81
+ throw new AddonError('unsupported', {
82
+ detail: `method '${method}' is not a host-to-addon request`,
83
+ });
84
+ }
85
+ if (!declared.has(method)) {
86
+ throw new AddonError('unsupported', {
87
+ detail: `handler '${method}' was not declared in initialize.handlers`,
88
+ });
89
+ }
90
+ const incoming = (params ?? {});
91
+ // Tolerant fallback (§6.3 safety net): hosts that predate mandatory _meta
92
+ // get a synthesized one so the schema parse below still succeeds.
93
+ const guarded = isCallMeta(incoming._meta)
94
+ ? incoming
95
+ : { ...incoming, _meta: fallbackMeta('host-request', now() + FALLBACK_DEADLINE_MS) };
96
+ const parsed = parseParams(spec, guarded);
97
+ const meta = CallMetaSchema.parse(parsed._meta);
98
+ if (instanceOrgId !== undefined) {
99
+ meta.orgId = instanceOrgId;
100
+ }
101
+ if (meta.deadline !== null && meta.deadline < now()) {
102
+ throw new AddonError('timeout', { detail: `deadline ${meta.deadline} has passed` });
103
+ }
104
+ const ctx = createAddonContextFor(channel, meta, signal);
105
+ const target = dispatch(method, parsed, ctx);
106
+ if (!target) {
107
+ throw new AddonError('unsupported', { detail: `no handler registered for '${method}'` });
108
+ }
109
+ const raw = await runWithDeadline(target(), meta.deadline, now, signal);
110
+ return parseResult(spec, raw);
111
+ }
112
+ function parseResult(spec, raw) {
113
+ if (!spec.result) {
114
+ return raw;
115
+ }
116
+ try {
117
+ return spec.result.parse(raw ?? {});
118
+ }
119
+ catch (error) {
120
+ throw new AddonError('invalid_response', {
121
+ detail: error instanceof Error ? error.message : String(error),
122
+ });
123
+ }
124
+ }
125
+ function parseParams(spec, params) {
126
+ try {
127
+ return spec.params.parse(params);
128
+ }
129
+ catch (error) {
130
+ throw new AddonError('invalid_params', {
131
+ detail: `${error instanceof Error ? error.message : String(error)}`,
132
+ });
133
+ }
134
+ }
135
+ async function handleNotification(method, params, signal) {
136
+ if (method === 'initialized') {
137
+ return;
138
+ }
139
+ const spec = methodSpec(method);
140
+ if (spec?.direction !== 'notification' || spec.sender === 'addon') {
141
+ throw new AddonError('unsupported', {
142
+ detail: `method '${method}' is not a host notification`,
143
+ });
144
+ }
145
+ const parsed = spec.params.parse(params);
146
+ const handler = definition.notifications?.[method];
147
+ if (!handler) {
148
+ return;
149
+ }
150
+ const ctx = createAddonContextFor(channel, fallbackMeta('notification', Number.MAX_SAFE_INTEGER), signal);
151
+ await handler(parsed, ctx);
152
+ }
153
+ function initialize(params) {
154
+ instanceOrgId = params.instance.orgId;
155
+ return InitializeResultSchema.parse({
156
+ addon: { id: addon.id, version: addon.version },
157
+ handlers: definition.handlers,
158
+ });
159
+ }
160
+ return { addon, handlers: definition.handlers, initialize, handleRequest, handleNotification };
161
+ }
162
+ function str(value) {
163
+ return typeof value === 'string' ? value : '';
164
+ }
165
+ function asInput(input) {
166
+ return typeof input === 'object' && input !== null ? input : {};
167
+ }
168
+ function withoutMeta(params) {
169
+ const copy = { ...params };
170
+ delete copy._meta;
171
+ return copy;
172
+ }
173
+ function handlerTarget(handler, params, ctx) {
174
+ if (typeof handler !== 'function') {
175
+ return null;
176
+ }
177
+ return () => handler(withoutMeta(params), ctx);
178
+ }
179
+ const FALLBACK_DEADLINE_MS = 5 * 60_000;
180
+ function isCallMeta(value) {
181
+ return typeof value === 'object' && value !== null && 'correlationId' in value;
182
+ }
183
+ function fallbackMeta(correlationId, deadline) {
184
+ return {
185
+ correlationId,
186
+ deadline,
187
+ principal: { type: 'system' },
188
+ locale: 'en',
189
+ };
190
+ }
191
+ function pairTarget(table, key, phase, params, ctx) {
192
+ const handler = table?.[key]?.[phase];
193
+ if (typeof handler !== 'function') {
194
+ return null;
195
+ }
196
+ return () => handler(params, ctx);
197
+ }
198
+ function optionalTarget(handler, params, ctx) {
199
+ if (typeof handler !== 'function') {
200
+ return null;
201
+ }
202
+ return () => handler(params, ctx);
203
+ }
204
+ async function runWithDeadline(work, deadline, now, signal) {
205
+ if (signal.aborted) {
206
+ throw new AddonError('cancelled');
207
+ }
208
+ if (deadline !== null && deadline - now() <= 0) {
209
+ throw new AddonError('timeout');
210
+ }
211
+ let timer;
212
+ const timeout = deadline === null
213
+ ? null
214
+ : new Promise((_resolve, reject) => {
215
+ timer = setTimeout(() => reject(new AddonError('timeout', { detail: `deadline ${deadline} exceeded` })), deadline - now());
216
+ });
217
+ const abort = new Promise((_resolve, reject) => {
218
+ signal.addEventListener('abort', () => reject(new AddonError('cancelled')), { once: true });
219
+ });
220
+ const contenders = timeout === null ? [Promise.resolve(work), abort] : [Promise.resolve(work), timeout, abort];
221
+ try {
222
+ return await Promise.race(contenders);
223
+ }
224
+ finally {
225
+ if (timer !== undefined) {
226
+ clearTimeout(timer);
227
+ }
228
+ }
229
+ }
@@ -0,0 +1,15 @@
1
+ import type { DbRow, DbStatement, DbWriteResult, SqlParam } from '../addon-context.ts';
2
+ export declare class SqlAuthorizerError extends Error {
3
+ readonly reason = "permission_denied";
4
+ constructor(message: string);
5
+ }
6
+ export declare function guardSql(sql: string): void;
7
+ export interface FakeDb {
8
+ exec(sql: string): void;
9
+ all(sql: string, params?: SqlParam[]): DbRow[];
10
+ get(sql: string, params?: SqlParam[]): DbRow | null;
11
+ run(sql: string, params?: SqlParam[]): DbWriteResult;
12
+ batch(statements: DbStatement[]): DbWriteResult[];
13
+ close(): void;
14
+ }
15
+ export declare function createFakeDb(): FakeDb;