@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,92 @@
1
+ import { z } from 'zod';
2
+ import type { L10n } from '../manifest/l10n.ts';
3
+ import { L10nSchema } from '../manifest/l10n.ts';
4
+ import type { FieldSpec } from './field-spec.ts';
5
+ import { FieldSpecSchema } from './field-spec.ts';
6
+ import {
7
+ type JsonSchemaSubset,
8
+ JsonSchemaSubsetSchema,
9
+ validateInputSchema,
10
+ } from './json-schema.ts';
11
+
12
+ export const TOOL_PACK_SCHEMA_VERSION = 1;
13
+
14
+ export type ToolEffect = 'read' | 'write' | 'destructive';
15
+
16
+ export interface ToolDescriptor {
17
+ name: string;
18
+ description: L10n;
19
+ inputSchema: JsonSchemaSubset;
20
+ outputSchema?: JsonSchemaSubset;
21
+ effect: ToolEffect;
22
+ idempotent?: boolean;
23
+ timeoutSec?: number;
24
+ maxResultKb?: number;
25
+ }
26
+
27
+ export interface ToolPackDescriptor {
28
+ name: string;
29
+ title: L10n;
30
+ description: L10n;
31
+ icon?: string;
32
+ connection?: string;
33
+ specFields?: FieldSpec[];
34
+ skills?: string[];
35
+ tools: ToolDescriptor[];
36
+ }
37
+
38
+ const inputSchemaIssues = (descriptor: ToolPackDescriptor, ctx: z.RefinementCtx): void => {
39
+ descriptor.tools.forEach((tool, index) => {
40
+ for (const violation of validateInputSchema(tool.inputSchema)) {
41
+ ctx.addIssue({
42
+ code: 'custom',
43
+ path: ['tools', index, 'inputSchema', violation.path],
44
+ message: `${violation.code}: ${violation.message}`,
45
+ });
46
+ }
47
+ if (tool.outputSchema !== undefined) {
48
+ for (const violation of validateInputSchema(tool.outputSchema)) {
49
+ ctx.addIssue({
50
+ code: 'custom',
51
+ path: ['tools', index, 'outputSchema', violation.path],
52
+ message: `${violation.code}: ${violation.message}`,
53
+ });
54
+ }
55
+ }
56
+ });
57
+ };
58
+
59
+ export const ToolDescriptorSchema = z.object({
60
+ name: z.string().min(1),
61
+ description: L10nSchema,
62
+ inputSchema: JsonSchemaSubsetSchema,
63
+ outputSchema: JsonSchemaSubsetSchema.optional(),
64
+ effect: z.enum(['read', 'write', 'destructive']),
65
+ idempotent: z.boolean().optional(),
66
+ timeoutSec: z.number().int().positive().optional(),
67
+ maxResultKb: z.number().int().positive().optional(),
68
+ });
69
+
70
+ export const ToolPackDescriptorSchema = z
71
+ .object({
72
+ name: z.string().min(1),
73
+ title: L10nSchema,
74
+ description: L10nSchema,
75
+ icon: z.string().optional(),
76
+ connection: z.string().min(1).optional(),
77
+ specFields: z.array(FieldSpecSchema).optional(),
78
+ skills: z.array(z.string().min(1)).optional(),
79
+ tools: z.array(ToolDescriptorSchema).min(1),
80
+ })
81
+ .superRefine(inputSchemaIssues);
82
+
83
+ export const toolPackFamily = {
84
+ family: 'tool-pack',
85
+ versions: [TOOL_PACK_SCHEMA_VERSION],
86
+ schema: (v: number): z.ZodType => {
87
+ if (v !== TOOL_PACK_SCHEMA_VERSION) {
88
+ throw new Error(`tool-pack descriptor schema version ${v} is not supported`);
89
+ }
90
+ return ToolPackDescriptorSchema;
91
+ },
92
+ };
@@ -0,0 +1,52 @@
1
+ import { z } from 'zod';
2
+ import type { L10n } from '../manifest/l10n.ts';
3
+ import { L10nSchema } from '../manifest/l10n.ts';
4
+
5
+ export const TRANSPORT_SCHEMA_VERSION = 1;
6
+
7
+ export interface TransportCapabilities {
8
+ threads: boolean;
9
+ edit: boolean;
10
+ typing: boolean;
11
+ attachments: boolean;
12
+ topics?: boolean;
13
+ reactions?: boolean;
14
+ readReceipts?: boolean;
15
+ }
16
+
17
+ export interface TransportDescriptor {
18
+ provider: string;
19
+ title: L10n;
20
+ connection: string;
21
+ capabilities: TransportCapabilities;
22
+ limits: { textMax: number };
23
+ }
24
+
25
+ export const TransportCapabilitiesSchema = z.object({
26
+ threads: z.boolean(),
27
+ edit: z.boolean(),
28
+ typing: z.boolean(),
29
+ attachments: z.boolean(),
30
+ topics: z.boolean().optional(),
31
+ reactions: z.boolean().optional(),
32
+ readReceipts: z.boolean().optional(),
33
+ });
34
+
35
+ export const TransportDescriptorSchema = z.object({
36
+ provider: z.string().min(1),
37
+ title: L10nSchema,
38
+ connection: z.string().min(1),
39
+ capabilities: TransportCapabilitiesSchema,
40
+ limits: z.object({ textMax: z.number().int().positive() }),
41
+ });
42
+
43
+ export const transportFamily = {
44
+ family: 'transport',
45
+ versions: [TRANSPORT_SCHEMA_VERSION],
46
+ schema: (v: number): z.ZodType => {
47
+ if (v !== TRANSPORT_SCHEMA_VERSION) {
48
+ throw new Error(`transport descriptor schema version ${v} is not supported`);
49
+ }
50
+ return TransportDescriptorSchema;
51
+ },
52
+ };
@@ -0,0 +1,92 @@
1
+ import { z } from 'zod';
2
+ import { HexColorSchema, LucideIconNameSchema } from '../manifest/branding.ts';
3
+ import type { L10n } from '../manifest/l10n.ts';
4
+ import { L10nSchema } from '../manifest/l10n.ts';
5
+ import type { FieldSpec } from './field-spec.ts';
6
+ import { FieldSpecSchema } from './field-spec.ts';
7
+
8
+ export const TRIGGER_SCHEMA_VERSION = 1;
9
+
10
+ export interface TriggerLimits {
11
+ perDay?: number;
12
+ throttleSec?: number;
13
+ maxBurst?: number;
14
+ }
15
+
16
+ /**
17
+ * Overrides for the standard trigger-form blocks the host renders for every
18
+ * trigger (name + agent, wake thread, run mode, instructions). Developers get
19
+ * them out of the box; each block can be relabeled or disabled.
20
+ */
21
+ export interface StandardFieldOverride {
22
+ disabled?: boolean;
23
+ label?: string;
24
+ help?: string;
25
+ required?: boolean;
26
+ }
27
+
28
+ export interface TriggerStandardBlocks {
29
+ name?: StandardFieldOverride;
30
+ agent?: StandardFieldOverride;
31
+ target?: StandardFieldOverride;
32
+ mode?: StandardFieldOverride;
33
+ instructions?: StandardFieldOverride;
34
+ }
35
+
36
+ export const StandardFieldOverrideSchema = z.object({
37
+ disabled: z.boolean().optional(),
38
+ label: z.string().optional(),
39
+ help: z.string().optional(),
40
+ required: z.boolean().optional(),
41
+ });
42
+
43
+ export interface TriggerDescriptor {
44
+ kind: string;
45
+ title: L10n;
46
+ description: L10n;
47
+ /** Lucide icon name shown on trigger cards (picker, lists). */
48
+ icon?: string;
49
+ /** Brand color as `#RRGGBB`; falls back to the addon manifest color. */
50
+ color?: string;
51
+ configFields: FieldSpec[];
52
+ connection?: string;
53
+ limits?: TriggerLimits;
54
+ standard?: TriggerStandardBlocks;
55
+ }
56
+
57
+ export const TriggerDescriptorSchema = z.object({
58
+ kind: z.string().min(1),
59
+ title: L10nSchema,
60
+ description: L10nSchema,
61
+ icon: LucideIconNameSchema.optional(),
62
+ color: HexColorSchema.optional(),
63
+ configFields: z.array(FieldSpecSchema),
64
+ connection: z.string().min(1).optional(),
65
+ limits: z
66
+ .object({
67
+ perDay: z.number().int().positive().optional(),
68
+ throttleSec: z.number().int().nonnegative().optional(),
69
+ maxBurst: z.number().int().positive().optional(),
70
+ })
71
+ .optional(),
72
+ standard: z
73
+ .object({
74
+ name: StandardFieldOverrideSchema.optional(),
75
+ agent: StandardFieldOverrideSchema.optional(),
76
+ target: StandardFieldOverrideSchema.optional(),
77
+ mode: StandardFieldOverrideSchema.optional(),
78
+ instructions: StandardFieldOverrideSchema.optional(),
79
+ })
80
+ .optional(),
81
+ });
82
+
83
+ export const triggerFamily = {
84
+ family: 'trigger',
85
+ versions: [TRIGGER_SCHEMA_VERSION],
86
+ schema: (v: number): z.ZodType => {
87
+ if (v !== TRIGGER_SCHEMA_VERSION) {
88
+ throw new Error(`trigger descriptor schema version ${v} is not supported`);
89
+ }
90
+ return TriggerDescriptorSchema;
91
+ },
92
+ };
@@ -0,0 +1,144 @@
1
+ import { z } from 'zod';
2
+ import type { L10n } from '../manifest/l10n.ts';
3
+ import { L10nSchema } from '../manifest/l10n.ts';
4
+ import type { FieldSpec } from './field-spec.ts';
5
+ import { FieldSpecSchema } from './field-spec.ts';
6
+
7
+ export const VIEW_SCHEMA_VERSION = 1;
8
+
9
+ export interface RouteRef {
10
+ route: string;
11
+ params?: Record<string, string>;
12
+ }
13
+
14
+ export interface Column {
15
+ key: string;
16
+ label: L10n;
17
+ format: 'text' | 'datetime' | 'number' | 'badge' | 'link' | 'copy';
18
+ }
19
+
20
+ export interface Action {
21
+ id: string;
22
+ label: L10n;
23
+ route: RouteRef;
24
+ confirm?: L10n;
25
+ destructive?: boolean;
26
+ }
27
+
28
+ export type ViewSection =
29
+ | { type: 'status' }
30
+ | { type: 'capabilities' }
31
+ | { type: 'permissions' }
32
+ | { type: 'config'; fields?: string[] }
33
+ | { type: 'connections'; connections?: string[] }
34
+ | { type: 'automations'; triggers?: string[] }
35
+ | { type: 'jobs' }
36
+ | {
37
+ type: 'table';
38
+ title: L10n;
39
+ source: RouteRef;
40
+ columns: Column[];
41
+ rowActions?: Action[];
42
+ pageSize?: number;
43
+ }
44
+ | { type: 'form'; title: L10n; fields: FieldSpec[]; submit: RouteRef; confirm?: L10n }
45
+ | { type: 'actions'; actions: Action[] }
46
+ | { type: 'markdown'; body: L10n }
47
+ | { type: 'frame'; route: string; height?: number };
48
+
49
+ export interface ViewDescriptor {
50
+ id: string;
51
+ title: L10n;
52
+ icon?: string;
53
+ sections: ViewSection[];
54
+ }
55
+
56
+ export const RouteRefSchema = z.object({
57
+ route: z.string().min(1),
58
+ params: z.record(z.string(), z.string()).optional(),
59
+ });
60
+
61
+ export const ColumnSchema = z.object({
62
+ key: z.string().min(1),
63
+ label: L10nSchema,
64
+ format: z.enum(['text', 'datetime', 'number', 'badge', 'link', 'copy']),
65
+ });
66
+
67
+ export const ActionSchema = z.object({
68
+ id: z.string().min(1),
69
+ label: L10nSchema,
70
+ route: RouteRefSchema,
71
+ confirm: L10nSchema.optional(),
72
+ destructive: z.boolean().optional(),
73
+ });
74
+
75
+ const baseSections = {
76
+ status: z.object({ type: z.literal('status') }),
77
+ capabilities: z.object({ type: z.literal('capabilities') }),
78
+ permissions: z.object({ type: z.literal('permissions') }),
79
+ config: z.object({ type: z.literal('config'), fields: z.array(z.string()).optional() }),
80
+ connections: z.object({
81
+ type: z.literal('connections'),
82
+ connections: z.array(z.string()).optional(),
83
+ }),
84
+ automations: z.object({
85
+ type: z.literal('automations'),
86
+ triggers: z.array(z.string()).optional(),
87
+ }),
88
+ jobs: z.object({ type: z.literal('jobs') }),
89
+ table: z.object({
90
+ type: z.literal('table'),
91
+ title: L10nSchema,
92
+ source: RouteRefSchema,
93
+ columns: z.array(ColumnSchema).min(1),
94
+ rowActions: z.array(ActionSchema).optional(),
95
+ pageSize: z.number().int().positive().optional(),
96
+ }),
97
+ form: z.object({
98
+ type: z.literal('form'),
99
+ title: L10nSchema,
100
+ fields: z.array(FieldSpecSchema).min(1),
101
+ submit: RouteRefSchema,
102
+ confirm: L10nSchema.optional(),
103
+ }),
104
+ actions: z.object({ type: z.literal('actions'), actions: z.array(ActionSchema).min(1) }),
105
+ markdown: z.object({ type: z.literal('markdown'), body: L10nSchema }),
106
+ frame: z.object({
107
+ type: z.literal('frame'),
108
+ route: z.string().min(1),
109
+ height: z.number().int().positive().optional(),
110
+ }),
111
+ };
112
+
113
+ export const ViewSectionSchema = z.union([
114
+ baseSections.status,
115
+ baseSections.capabilities,
116
+ baseSections.permissions,
117
+ baseSections.config,
118
+ baseSections.connections,
119
+ baseSections.automations,
120
+ baseSections.jobs,
121
+ baseSections.table,
122
+ baseSections.form,
123
+ baseSections.actions,
124
+ baseSections.markdown,
125
+ baseSections.frame,
126
+ ]);
127
+
128
+ export const ViewDescriptorSchema = z.object({
129
+ id: z.string().min(1),
130
+ title: L10nSchema,
131
+ icon: z.string().optional(),
132
+ sections: z.array(ViewSectionSchema).min(1),
133
+ });
134
+
135
+ export const viewFamily = {
136
+ family: 'view',
137
+ versions: [VIEW_SCHEMA_VERSION],
138
+ schema: (v: number): z.ZodType => {
139
+ if (v !== VIEW_SCHEMA_VERSION) {
140
+ throw new Error(`view descriptor schema version ${v} is not supported`);
141
+ }
142
+ return ViewDescriptorSchema;
143
+ },
144
+ };
@@ -0,0 +1,43 @@
1
+ import { z } from 'zod';
2
+ import type { L10n } from '../manifest/l10n.ts';
3
+ import { L10nSchema } from '../manifest/l10n.ts';
4
+
5
+ export const VIEWER_SCHEMA_VERSION = 1;
6
+
7
+ export interface ViewerDescriptor {
8
+ id: string;
9
+ title: L10n;
10
+ /** Dot-prefixed file extensions this viewer handles, lowercase, e.g. ['.drawio']. */
11
+ extensions: string[];
12
+ /** Add-on route (GET) that serves the editor/preview HTML document. */
13
+ route: string;
14
+ mode: 'view' | 'edit';
15
+ /** Host offers "New <title> file" in the sources tree when true. */
16
+ create?: boolean;
17
+ }
18
+
19
+ export const ViewerExtensionSchema = z
20
+ .string()
21
+ .regex(/^\.[A-Za-z0-9][A-Za-z0-9_.-]*$/, 'extension must be a dot-prefixed file extension');
22
+
23
+ export const ViewerDescriptorSchema = z.object({
24
+ id: z.string().min(1),
25
+ title: L10nSchema,
26
+ extensions: z.array(ViewerExtensionSchema).min(1),
27
+ route: z
28
+ .string()
29
+ .regex(/^\/[A-Za-z0-9/_:.-]*$/, 'route must start with / and use :param segments'),
30
+ mode: z.enum(['view', 'edit']),
31
+ create: z.boolean().optional(),
32
+ });
33
+
34
+ export const viewerFamily = {
35
+ family: 'viewer',
36
+ versions: [VIEWER_SCHEMA_VERSION],
37
+ schema: (v: number): z.ZodType => {
38
+ if (v !== VIEWER_SCHEMA_VERSION) {
39
+ throw new Error(`viewer descriptor schema version ${v} is not supported`);
40
+ }
41
+ return ViewerDescriptorSchema;
42
+ },
43
+ } as const;
@@ -0,0 +1,30 @@
1
+ import { z } from 'zod';
2
+ import type { L10n } from '../manifest/l10n.ts';
3
+ import { L10nSchema } from '../manifest/l10n.ts';
4
+
5
+ export const WORKER_SCHEMA_VERSION = 1;
6
+
7
+ export interface WorkerDescriptor {
8
+ id: string;
9
+ title: L10n;
10
+ autostart: boolean;
11
+ restart: 'on-failure' | 'never';
12
+ }
13
+
14
+ export const WorkerDescriptorSchema = z.object({
15
+ id: z.string().min(1),
16
+ title: L10nSchema,
17
+ autostart: z.boolean(),
18
+ restart: z.enum(['on-failure', 'never']),
19
+ });
20
+
21
+ export const workerFamily = {
22
+ family: 'worker',
23
+ versions: [WORKER_SCHEMA_VERSION],
24
+ schema: (v: number): z.ZodType => {
25
+ if (v !== WORKER_SCHEMA_VERSION) {
26
+ throw new Error(`worker descriptor schema version ${v} is not supported`);
27
+ }
28
+ return WorkerDescriptorSchema;
29
+ },
30
+ };
@@ -0,0 +1,52 @@
1
+ import { type AddonContext, createAddonContext } from './addon-context-factory.ts';
2
+ import { type HostServiceClient, methodSpec, protocolEntries } from './protocol/map.ts';
3
+ import { type CallMeta, CallMetaSchema } from './protocol/meta.ts';
4
+
5
+ export interface HostChannel {
6
+ request(method: string, params: unknown, signal?: AbortSignal): Promise<unknown>;
7
+ notify(method: string, params: unknown): void;
8
+ }
9
+
10
+ export function createHostClient(channel: HostChannel, getMeta: () => CallMeta): HostServiceClient {
11
+ const client = {} as Record<
12
+ string,
13
+ (params: unknown, options?: { signal?: AbortSignal }) => Promise<unknown>
14
+ >;
15
+ for (const entry of protocolEntries()) {
16
+ if (entry.spec.direction !== 'addon-to-host') {
17
+ continue;
18
+ }
19
+ client[entry.method] = async (params: unknown, options?: { signal?: AbortSignal }) => {
20
+ const spec = methodSpec(entry.method);
21
+ if (!spec) {
22
+ throw new Error(`method '${entry.method}' is not in the protocol map`);
23
+ }
24
+ const parsedParams = spec.params.parse({
25
+ ...(params as Record<string, unknown>),
26
+ _meta: getMeta(),
27
+ });
28
+ const raw = await channel.request(entry.method, parsedParams, options?.signal);
29
+ return spec.result ? spec.result.parse(raw) : raw;
30
+ };
31
+ }
32
+ return client as HostServiceClient;
33
+ }
34
+
35
+ export function parseCallMeta(raw: unknown): CallMeta {
36
+ return CallMetaSchema.parse(raw);
37
+ }
38
+
39
+ export function createAddonContextFor(
40
+ channel: HostChannel,
41
+ meta: CallMeta,
42
+ signal: AbortSignal,
43
+ ): AddonContext {
44
+ return createAddonContext(
45
+ createHostClient(channel, () => meta),
46
+ meta,
47
+ signal,
48
+ (method, params) => channel.notify(method, params),
49
+ );
50
+ }
51
+
52
+ export type { AddonContext } from './addon-context.ts';