@open-wa/schema 5.0.0 → 5.2.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/dist/generated/index.cjs +8 -2
- package/dist/generated/index.cjs.map +1 -1
- package/dist/generated/index.d.cts +7868 -994
- package/dist/generated/index.d.cts.map +1 -1
- package/dist/generated/index.d.mts +7868 -994
- package/dist/generated/index.d.mts.map +1 -1
- package/dist/generated/index.mjs +8 -2
- package/dist/generated/index.mjs.map +1 -1
- package/dist/index-C9Ad0lEr.d.cts +4729 -0
- package/dist/index-C9Ad0lEr.d.cts.map +1 -0
- package/dist/index-D0QN2PaW.d.mts +4729 -0
- package/dist/index-D0QN2PaW.d.mts.map +1 -0
- package/dist/index.cjs +109 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +53 -2
- package/dist/index.mjs.map +1 -1
- package/dist/methods/index.cjs +6 -1
- package/dist/methods/index.d.cts +2 -2
- package/dist/methods/index.d.mts +2 -2
- package/dist/methods/index.mjs +2 -2
- package/dist/{methods-Dizm7Fn9.cjs → methods-BGmdzjd3.cjs} +904 -76
- package/dist/methods-BGmdzjd3.cjs.map +1 -0
- package/dist/{methods-CZqeUhKy.mjs → methods-BhLsQNtL.mjs} +587 -77
- package/dist/methods-BhLsQNtL.mjs.map +1 -0
- package/package.json +4 -3
- package/src/__tests__/generators.contract.test.ts +181 -0
- package/src/__tests__/registry.contract.test.ts +81 -0
- package/src/client-config.ts +30 -0
- package/src/codecs.test.ts +41 -0
- package/src/codecs.ts +191 -0
- package/src/common-types.ts +287 -0
- package/src/config.ts +47 -0
- package/src/enums.ts +63 -0
- package/src/events/groups.ts +33 -0
- package/src/events/index.ts +4 -0
- package/src/events/messaging.ts +86 -0
- package/src/events/registry.ts +124 -0
- package/src/events/state.ts +59 -0
- package/src/generated/AliasMap.ts +216 -0
- package/src/generated/BaseClient.ts +878 -0
- package/src/generated/BaseNamespacedClient.ts +214 -0
- package/src/generated/index.ts +5 -0
- package/src/generated/openapi.json +30049 -0
- package/src/generated/types.ts +383 -0
- package/src/http-manifest.ts +97 -0
- package/src/implementor.test.ts +88 -0
- package/src/implementor.ts +75 -0
- package/src/index.ts +46 -0
- package/src/interactive.ts +151 -0
- package/src/methods/business.ts +52 -0
- package/src/methods/chats.ts +264 -0
- package/src/methods/communities.ts +121 -0
- package/src/methods/contacts.ts +160 -0
- package/src/methods/groups.ts +378 -0
- package/src/methods/index.ts +34 -0
- package/src/methods/interactive.ts +21 -0
- package/src/methods/labels.ts +88 -0
- package/src/methods/media.ts +36 -0
- package/src/methods/messaging.ts +836 -0
- package/src/methods/session.ts +262 -0
- package/src/methods/status.ts +122 -0
- package/src/output-json-schema.ts +27 -0
- package/src/parameters.ts +237 -0
- package/src/registry.test.ts +30 -0
- package/src/registry.ts +601 -0
- package/src/return-types.ts +125 -0
- package/dist/index-BHcd0h7x.d.mts +0 -1265
- package/dist/index-BHcd0h7x.d.mts.map +0 -1
- package/dist/index-ChYFQM1K.d.cts +0 -1265
- package/dist/index-ChYFQM1K.d.cts.map +0 -1
- package/dist/methods-CZqeUhKy.mjs.map +0 -1
- package/dist/methods-Dizm7Fn9.cjs.map +0 -1
package/src/registry.ts
ADDED
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ActionType, LicenseTier, FunctionalityScope, HttpMethod } from './enums';
|
|
3
|
+
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// Legacy Registry (Kept for backward compatibility)
|
|
6
|
+
// ============================================================================
|
|
7
|
+
|
|
8
|
+
export type CapabilityType = 'method' | 'event';
|
|
9
|
+
export type CapabilityStatus = 'stable' | 'deprecated' | 'experimental' | 'beta';
|
|
10
|
+
|
|
11
|
+
export interface CapabilityMetadata {
|
|
12
|
+
description: string;
|
|
13
|
+
tags?: string[];
|
|
14
|
+
since?: string;
|
|
15
|
+
status?: CapabilityStatus;
|
|
16
|
+
deprecated?: string; // Reason
|
|
17
|
+
category?: string;
|
|
18
|
+
example?: any;
|
|
19
|
+
requiresSession?: boolean; // Default true
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CapabilityDefinition<
|
|
23
|
+
Input extends z.ZodTypeAny = z.ZodTypeAny,
|
|
24
|
+
Output extends z.ZodTypeAny = z.ZodTypeAny
|
|
25
|
+
> {
|
|
26
|
+
name: string;
|
|
27
|
+
type: CapabilityType;
|
|
28
|
+
metadata: CapabilityMetadata;
|
|
29
|
+
inputSchema: Input;
|
|
30
|
+
outputSchema: Output;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class Registry {
|
|
34
|
+
private static methods = new Map<string, CapabilityDefinition>();
|
|
35
|
+
private static events = new Map<string, CapabilityDefinition>();
|
|
36
|
+
|
|
37
|
+
static registerMethod<Input extends z.ZodTypeAny, Output extends z.ZodTypeAny>(
|
|
38
|
+
def: CapabilityDefinition<Input, Output>
|
|
39
|
+
) {
|
|
40
|
+
if (this.methods.has(def.name)) {
|
|
41
|
+
throw new Error(`Method ${def.name} already registered`);
|
|
42
|
+
}
|
|
43
|
+
this.methods.set(def.name, def);
|
|
44
|
+
return def;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static registerEvent<Input extends z.ZodTypeAny, Output extends z.ZodTypeAny>(
|
|
48
|
+
def: CapabilityDefinition<Input, Output>
|
|
49
|
+
) {
|
|
50
|
+
if (this.events.has(def.name)) {
|
|
51
|
+
throw new Error(`Event ${def.name} already registered`);
|
|
52
|
+
}
|
|
53
|
+
this.events.set(def.name, def);
|
|
54
|
+
return def;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static getMethod(name: string) {
|
|
58
|
+
return this.methods.get(name);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static getEvent(name: string) {
|
|
62
|
+
return this.events.get(name);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static getAllMethods() {
|
|
66
|
+
return Array.from(this.methods.values());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static getAllEvents() {
|
|
70
|
+
return Array.from(this.events.values());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ============================================================================
|
|
75
|
+
// V2 Registry Infrastructure (Dual-Mode Support)
|
|
76
|
+
// ============================================================================
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Metadata for client function capabilities
|
|
80
|
+
* Supports RBAC, licensing, and automation features
|
|
81
|
+
*/
|
|
82
|
+
export interface ClientFunctionMetadata {
|
|
83
|
+
functionName: string; // Required now
|
|
84
|
+
wapiOverride?: string; // If the functionName is different from the WAPI name
|
|
85
|
+
id?: string; // Fallback to functionName
|
|
86
|
+
slug?: string; // Fallback to kebab-case(functionName)
|
|
87
|
+
namespace?: string; // e.g., "chats", "contacts", "stories"
|
|
88
|
+
action?: ActionType; // RBAC action type, defaults to 'read'
|
|
89
|
+
api_slug?: string; // Default to namespace ? namespace/slug : slug
|
|
90
|
+
description?: string; // Markdown TSDoc
|
|
91
|
+
deprecated?: boolean; // Whether the function is deprecated
|
|
92
|
+
license?: LicenseTier; // Defaults to 'none'
|
|
93
|
+
functionality?: FunctionalityScope; // Default to 'both'
|
|
94
|
+
httpMethod?: HttpMethod; // Defaults to 'AUTO'
|
|
95
|
+
parameterOrder: string[]; // Critical for mapping positional args to object keys
|
|
96
|
+
aliases?: AliasMetadata;
|
|
97
|
+
namespacedName?: string;
|
|
98
|
+
allAliases?: string[];
|
|
99
|
+
deprecatedAliases?: string[];
|
|
100
|
+
|
|
101
|
+
// Stored directly for generator access
|
|
102
|
+
inputSchema: z.ZodObject<any>;
|
|
103
|
+
outputSchema: z.ZodTypeAny;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Complete method definition with schema and metadata
|
|
108
|
+
*/
|
|
109
|
+
export interface MethodDefinition {
|
|
110
|
+
schema: z.ZodFunction<any, any>;
|
|
111
|
+
meta: ClientFunctionMetadata;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type OpenWAMethodSchema<
|
|
115
|
+
T extends z.ZodObject<any>,
|
|
116
|
+
R extends z.ZodTypeAny
|
|
117
|
+
> = z.ZodFunction<z.ZodUnion<[z.ZodTuple<[T]>, z.ZodTuple<any>]>, R> & {
|
|
118
|
+
openWAInput: T;
|
|
119
|
+
openWAOutput: R;
|
|
120
|
+
openWAFunctionName: string;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Metadata for individual parameters
|
|
125
|
+
* Used for documentation and example generation
|
|
126
|
+
*/
|
|
127
|
+
export interface ParameterMetadata {
|
|
128
|
+
example: string | number | boolean | string[];
|
|
129
|
+
brandedType?: string;
|
|
130
|
+
formatDescription?: string;
|
|
131
|
+
pattern?: string;
|
|
132
|
+
additionalExamples?: Record<string, string>;
|
|
133
|
+
keyAliases?: string[];
|
|
134
|
+
deprecatedKeyAliases?: string[];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface AliasMetadata {
|
|
138
|
+
verb?: string;
|
|
139
|
+
noun?: string;
|
|
140
|
+
extra?: string;
|
|
141
|
+
explicit?: string[];
|
|
142
|
+
deprecated?: string[];
|
|
143
|
+
namespacedName?: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const parameterRegistry = z.registry<ParameterMetadata>();
|
|
147
|
+
|
|
148
|
+
const STRIP_NAMESPACE_NOUN = true;
|
|
149
|
+
|
|
150
|
+
const namespaceNounMap: Record<string, string[]> = {
|
|
151
|
+
chats: ['Chats', 'Chat'],
|
|
152
|
+
messages: ['Messages', 'Message'],
|
|
153
|
+
contacts: ['Contacts', 'Contact'],
|
|
154
|
+
groups: ['Groups', 'Group'],
|
|
155
|
+
communities: ['Communities', 'Community'],
|
|
156
|
+
status: ['Statuses', 'Status'],
|
|
157
|
+
labels: ['Labels', 'Label'],
|
|
158
|
+
business: ['Businesses', 'Business'],
|
|
159
|
+
media: ['Media'],
|
|
160
|
+
session: ['Session'],
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
function uniqueStrings(values: Array<string | undefined>): string[] {
|
|
164
|
+
const seen = new Set<string>();
|
|
165
|
+
const result: string[] = [];
|
|
166
|
+
|
|
167
|
+
for (const value of values) {
|
|
168
|
+
if (!value) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const normalized = value.trim();
|
|
173
|
+
if (!normalized || seen.has(normalized)) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
seen.add(normalized);
|
|
178
|
+
result.push(normalized);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function getNamespaceNouns(namespace?: string): string[] {
|
|
185
|
+
if (!namespace) {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return namespaceNounMap[namespace] ?? [namespace.charAt(0).toUpperCase() + namespace.slice(1)];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function deriveNamespacedMethodName(functionName: string, namespace?: string): string {
|
|
193
|
+
if (!namespace || !STRIP_NAMESPACE_NOUN) {
|
|
194
|
+
return functionName;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const nouns = getNamespaceNouns(namespace).sort((left, right) => right.length - left.length);
|
|
198
|
+
|
|
199
|
+
for (const noun of nouns) {
|
|
200
|
+
if (functionName.endsWith(noun)) {
|
|
201
|
+
const stripped = functionName.slice(0, -noun.length);
|
|
202
|
+
if (stripped) {
|
|
203
|
+
return stripped;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
for (const noun of nouns) {
|
|
209
|
+
const index = functionName.indexOf(noun);
|
|
210
|
+
if (index > 0) {
|
|
211
|
+
const stripped = `${functionName.slice(0, index)}${functionName.slice(index + noun.length)}`;
|
|
212
|
+
if (stripped) {
|
|
213
|
+
return stripped;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return functionName;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function isFlatAlias(alias: string): boolean {
|
|
222
|
+
return !alias.includes('.');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function toNamespacedAlias(alias: string, namespace?: string): string | undefined {
|
|
226
|
+
if (!namespace) {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (alias.includes('.')) {
|
|
231
|
+
return alias;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return `${namespace}.${deriveNamespacedMethodName(alias, namespace)}`;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function getResolvedNamespacedName(meta: ClientFunctionMetadata): string {
|
|
238
|
+
return meta.aliases?.namespacedName ?? deriveNamespacedMethodName(meta.functionName, meta.namespace);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function getFlatAliases(meta: ClientFunctionMetadata): { aliases: string[]; deprecated: string[] } {
|
|
242
|
+
const explicit = uniqueStrings(meta.aliases?.explicit ?? []);
|
|
243
|
+
const deprecated = uniqueStrings(meta.aliases?.deprecated ?? []);
|
|
244
|
+
const aliases: string[] = [...explicit, ...deprecated];
|
|
245
|
+
|
|
246
|
+
if (meta.functionName.startsWith('getAll')) {
|
|
247
|
+
const suffix = meta.functionName.slice('getAll'.length);
|
|
248
|
+
if (suffix) {
|
|
249
|
+
aliases.push(`list${suffix}`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
aliases: uniqueStrings(aliases),
|
|
255
|
+
deprecated,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function getMethodAliases(meta: ClientFunctionMetadata): string[] {
|
|
260
|
+
const namespace = meta.namespace;
|
|
261
|
+
const namespacedName = getResolvedNamespacedName(meta);
|
|
262
|
+
const defaultNamespacedName = deriveNamespacedMethodName(meta.functionName, namespace);
|
|
263
|
+
const namespacedAliases = namespace ? [`${namespace}.${namespacedName}`] : [];
|
|
264
|
+
|
|
265
|
+
if (namespace && defaultNamespacedName !== namespacedName) {
|
|
266
|
+
namespacedAliases.push(`${namespace}.${defaultNamespacedName}`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const { aliases: flatAliases } = getFlatAliases(meta);
|
|
270
|
+
const derivedNamespacedAliases = flatAliases
|
|
271
|
+
.map((alias) => toNamespacedAlias(alias, namespace))
|
|
272
|
+
.filter((alias): alias is string => Boolean(alias));
|
|
273
|
+
|
|
274
|
+
if (namespace && meta.functionName.startsWith('getAll')) {
|
|
275
|
+
namespacedAliases.push(`${namespace}.all`);
|
|
276
|
+
namespacedAliases.push(`${namespace}.list`);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return uniqueStrings([
|
|
280
|
+
...flatAliases,
|
|
281
|
+
...namespacedAliases,
|
|
282
|
+
...derivedNamespacedAliases,
|
|
283
|
+
]).filter((alias) => alias !== meta.functionName);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function getDeprecatedAliases(meta: ClientFunctionMetadata): string[] {
|
|
287
|
+
const namespace = meta.namespace;
|
|
288
|
+
const deprecated = uniqueStrings(meta.aliases?.deprecated ?? []);
|
|
289
|
+
|
|
290
|
+
return uniqueStrings([
|
|
291
|
+
...deprecated,
|
|
292
|
+
...deprecated
|
|
293
|
+
.map((alias) => toNamespacedAlias(alias, namespace))
|
|
294
|
+
.filter((alias): alias is string => Boolean(alias)),
|
|
295
|
+
]);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function unwrapSchema(schema: z.ZodTypeAny): z.ZodTypeAny {
|
|
299
|
+
let current = schema;
|
|
300
|
+
|
|
301
|
+
while (true) {
|
|
302
|
+
const candidate = current as {
|
|
303
|
+
unwrap?: () => z.ZodTypeAny;
|
|
304
|
+
removeDefault?: () => z.ZodTypeAny;
|
|
305
|
+
_def?: { innerType?: z.ZodTypeAny };
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
if (parameterRegistry.get(current)) {
|
|
309
|
+
return current;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (typeof candidate.unwrap === 'function') {
|
|
313
|
+
current = candidate.unwrap();
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (typeof candidate.removeDefault === 'function') {
|
|
318
|
+
current = candidate.removeDefault();
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (candidate._def?.innerType) {
|
|
323
|
+
current = candidate._def.innerType;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return current;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function getParameterMetadata(schema: z.ZodTypeAny): ParameterMetadata | undefined {
|
|
332
|
+
return parameterRegistry.get(schema) ?? parameterRegistry.get(unwrapSchema(schema));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function buildKeyAliasMap(inputShape: Record<string, z.ZodTypeAny>): Record<string, string> {
|
|
336
|
+
const keyAliasMap: Record<string, string> = {};
|
|
337
|
+
|
|
338
|
+
for (const [canonicalKey, schema] of Object.entries(inputShape)) {
|
|
339
|
+
const metadata = getParameterMetadata(schema);
|
|
340
|
+
|
|
341
|
+
for (const alias of metadata?.keyAliases ?? []) {
|
|
342
|
+
keyAliasMap[alias] = canonicalKey;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
for (const alias of metadata?.deprecatedKeyAliases ?? []) {
|
|
346
|
+
keyAliasMap[alias] = canonicalKey;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return keyAliasMap;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function normalizeParameterKeys(
|
|
354
|
+
data: unknown,
|
|
355
|
+
keyAliasMap: Record<string, string>
|
|
356
|
+
): unknown {
|
|
357
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
358
|
+
return data;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const normalized: Record<string, unknown> = {};
|
|
362
|
+
|
|
363
|
+
for (const [key, value] of Object.entries(data)) {
|
|
364
|
+
const canonicalKey = keyAliasMap[key] ?? key;
|
|
365
|
+
if (!(canonicalKey in normalized)) {
|
|
366
|
+
normalized[canonicalKey] = value;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return normalized;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Primary registry: keyed by function name
|
|
375
|
+
*/
|
|
376
|
+
const methodsByName = new Map<string, MethodDefinition>();
|
|
377
|
+
const methodsByAlias = new Map<string, string>();
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Reverse lookup: for implementMethod() ergonomics
|
|
381
|
+
* WeakMap so dynamically-created schemas don't leak
|
|
382
|
+
*/
|
|
383
|
+
const nameBySchema = new WeakMap<z.ZodFunction<any, any>, string>();
|
|
384
|
+
|
|
385
|
+
export const clientRegistry = {
|
|
386
|
+
/**
|
|
387
|
+
* Register a method definition
|
|
388
|
+
* @throws Error if method name is already registered
|
|
389
|
+
*/
|
|
390
|
+
register(def: MethodDefinition): z.ZodFunction<any, any> {
|
|
391
|
+
const name = def.meta.functionName;
|
|
392
|
+
|
|
393
|
+
if (methodsByName.has(name)) {
|
|
394
|
+
throw new Error(`Method "${name}" already registered`);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
def.meta.namespacedName = getResolvedNamespacedName(def.meta);
|
|
398
|
+
def.meta.allAliases = getMethodAliases(def.meta);
|
|
399
|
+
def.meta.deprecatedAliases = getDeprecatedAliases(def.meta);
|
|
400
|
+
|
|
401
|
+
for (const alias of def.meta.allAliases) {
|
|
402
|
+
const existing = methodsByAlias.get(alias);
|
|
403
|
+
if (existing && existing !== name) {
|
|
404
|
+
throw new Error(
|
|
405
|
+
`Alias collision: "${alias}" is claimed by both "${existing}" and "${name}"`
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
methodsByAlias.set(alias, name);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
methodsByName.set(name, def);
|
|
413
|
+
nameBySchema.set(def.schema, name);
|
|
414
|
+
|
|
415
|
+
return def.schema;
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Get method by name (primary lookup)
|
|
420
|
+
*/
|
|
421
|
+
get(name: string): MethodDefinition | undefined {
|
|
422
|
+
return methodsByName.get(name);
|
|
423
|
+
},
|
|
424
|
+
|
|
425
|
+
resolve(nameOrAlias: string): MethodDefinition | undefined {
|
|
426
|
+
const primary = methodsByAlias.get(nameOrAlias) ?? nameOrAlias;
|
|
427
|
+
return methodsByName.get(primary);
|
|
428
|
+
},
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Get method by schema (for implementMethod compatibility)
|
|
432
|
+
*/
|
|
433
|
+
getBySchema(schema: z.ZodFunction<any, any>): MethodDefinition | undefined {
|
|
434
|
+
const name = nameBySchema.get(schema);
|
|
435
|
+
return name ? methodsByName.get(name) : undefined;
|
|
436
|
+
},
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Check if method exists
|
|
440
|
+
*/
|
|
441
|
+
has(name: string): boolean {
|
|
442
|
+
return methodsByName.has(name);
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Get all registered methods
|
|
447
|
+
*/
|
|
448
|
+
getAll(): MethodDefinition[] {
|
|
449
|
+
return Array.from(methodsByName.values());
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Get all method names
|
|
454
|
+
*/
|
|
455
|
+
getNames(): string[] {
|
|
456
|
+
return Array.from(methodsByName.keys());
|
|
457
|
+
},
|
|
458
|
+
|
|
459
|
+
getAliasMap(): Record<string, string> {
|
|
460
|
+
return Object.fromEntries(methodsByAlias.entries());
|
|
461
|
+
},
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Get methods by namespace
|
|
465
|
+
*/
|
|
466
|
+
getByNamespace(namespace: string): MethodDefinition[] {
|
|
467
|
+
return this.getAll().filter(def => def.meta.namespace === namespace);
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Clear registry (for testing)
|
|
472
|
+
*/
|
|
473
|
+
clear(): void {
|
|
474
|
+
methodsByName.clear();
|
|
475
|
+
methodsByAlias.clear();
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Helper to convert a Zod Object shape into a Zod Tuple based on key order
|
|
481
|
+
* This enables positional argument support while maintaining type safety
|
|
482
|
+
*
|
|
483
|
+
* @param zodObj - The Zod object schema
|
|
484
|
+
* @param keys - The order of keys for positional arguments
|
|
485
|
+
* @returns A Zod tuple schema matching the key order
|
|
486
|
+
*/
|
|
487
|
+
export function zObjectToTuple<
|
|
488
|
+
Shape extends z.ZodRawShape,
|
|
489
|
+
Keys extends readonly [keyof Shape, ...Array<keyof Shape>]
|
|
490
|
+
>(
|
|
491
|
+
zodObj: z.ZodObject<Shape>,
|
|
492
|
+
keys: Keys
|
|
493
|
+
): z.ZodTuple<[Shape[Keys[0]], ...{ [K in keyof Keys]: K extends 0 ? never : Shape[Keys[K]] }[number][]]> {
|
|
494
|
+
// Runtime: Map the keys to the actual schemas in the object
|
|
495
|
+
const schemas = keys.map((k) => zodObj.shape[k]);
|
|
496
|
+
|
|
497
|
+
// Cast to any because TS struggles to map the array runtime to the tuple type
|
|
498
|
+
return z.tuple(schemas as any) as any;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* V2 defineMethod - Creates a dual-mode function schema
|
|
503
|
+
* Supports both positional arguments and named object parameters
|
|
504
|
+
*
|
|
505
|
+
* @param name - Function name
|
|
506
|
+
* @param params - Configuration including metadata, input schema, parameter order, and output schema
|
|
507
|
+
* @returns A Zod function schema with dual-mode input support
|
|
508
|
+
*/
|
|
509
|
+
export function defineMethodV2<
|
|
510
|
+
T extends z.ZodObject<any>,
|
|
511
|
+
R extends z.ZodTypeAny
|
|
512
|
+
>(
|
|
513
|
+
name: string,
|
|
514
|
+
params: {
|
|
515
|
+
meta: Omit<ClientFunctionMetadata, 'functionName' | 'parameterOrder' | 'inputSchema' | 'outputSchema'>;
|
|
516
|
+
input: T;
|
|
517
|
+
parameterOrder: Array<keyof T['shape']>;
|
|
518
|
+
output: R;
|
|
519
|
+
}
|
|
520
|
+
): OpenWAMethodSchema<T, R> {
|
|
521
|
+
const tupleSchema = zObjectToTuple(params.input, params.parameterOrder as any);
|
|
522
|
+
|
|
523
|
+
const inputUnion = z.union([
|
|
524
|
+
z.tuple([params.input]),
|
|
525
|
+
tupleSchema
|
|
526
|
+
]);
|
|
527
|
+
|
|
528
|
+
const funcSchema = Object.assign(
|
|
529
|
+
z.function({
|
|
530
|
+
input: inputUnion,
|
|
531
|
+
output: params.output
|
|
532
|
+
}),
|
|
533
|
+
{
|
|
534
|
+
openWAInput: params.input,
|
|
535
|
+
openWAOutput: params.output,
|
|
536
|
+
openWAFunctionName: name,
|
|
537
|
+
}
|
|
538
|
+
) as OpenWAMethodSchema<T, R>;
|
|
539
|
+
|
|
540
|
+
clientRegistry.register({
|
|
541
|
+
schema: funcSchema,
|
|
542
|
+
meta: {
|
|
543
|
+
functionName: name,
|
|
544
|
+
parameterOrder: params.parameterOrder as string[],
|
|
545
|
+
inputSchema: params.input,
|
|
546
|
+
outputSchema: params.output,
|
|
547
|
+
...params.meta
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
return funcSchema;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// ============================================================================
|
|
555
|
+
// Legacy defineMethod (Backward Compatibility)
|
|
556
|
+
// ============================================================================
|
|
557
|
+
|
|
558
|
+
export function defineMethod<
|
|
559
|
+
Input extends z.ZodTypeAny,
|
|
560
|
+
Output extends z.ZodTypeAny
|
|
561
|
+
>(
|
|
562
|
+
name: string,
|
|
563
|
+
params: {
|
|
564
|
+
meta: CapabilityMetadata;
|
|
565
|
+
input: Input;
|
|
566
|
+
output: Output;
|
|
567
|
+
}
|
|
568
|
+
): CapabilityDefinition<Input, Output> {
|
|
569
|
+
return Registry.registerMethod({
|
|
570
|
+
name,
|
|
571
|
+
type: 'method',
|
|
572
|
+
metadata: params.meta,
|
|
573
|
+
inputSchema: params.input,
|
|
574
|
+
outputSchema: params.output,
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
export function defineEvent<
|
|
579
|
+
Input extends z.ZodTypeAny,
|
|
580
|
+
Output extends z.ZodTypeAny
|
|
581
|
+
>(
|
|
582
|
+
name: string,
|
|
583
|
+
params: {
|
|
584
|
+
meta: CapabilityMetadata;
|
|
585
|
+
input: Input;
|
|
586
|
+
output: Output;
|
|
587
|
+
}
|
|
588
|
+
): CapabilityDefinition<Input, Output> {
|
|
589
|
+
return Registry.registerEvent({
|
|
590
|
+
name,
|
|
591
|
+
type: 'event',
|
|
592
|
+
metadata: params.meta,
|
|
593
|
+
inputSchema: params.input,
|
|
594
|
+
outputSchema: params.output,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export type InferInput<T extends CapabilityDefinition> = z.infer<T['inputSchema']>;
|
|
599
|
+
export type InferOutput<T extends CapabilityDefinition> = z.infer<T['outputSchema']>;
|
|
600
|
+
|
|
601
|
+
export { z };
|