@slates/provider-handler 1.0.0-rc.1 → 1.0.0-rc.10
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/index.cjs +1001 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.module.js +973 -0
- package/package.json +16 -10
- package/src/index.ts +595 -111
- package/src/spec.ts +16 -13
- package/src/validation.ts +17 -1
- package/tsconfig.json +0 -8
package/src/spec.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { badRequestError, notFoundError, ServiceError } from '@lowerdeck/error';
|
|
2
|
-
import { SlateAuthenticationMethod, SlatesAction } from '@slates/proto';
|
|
3
|
-
import { Slate, SlateDefaultPollingIntervalSeconds } from '@slates/provider';
|
|
2
|
+
import type { SlateAuthenticationMethod, SlatesAction } from '@slates/proto';
|
|
3
|
+
import { type Slate, SlateDefaultPollingIntervalSeconds } from '@slates/provider';
|
|
4
4
|
import z from 'zod';
|
|
5
|
+
import { toJsonSchema } from './validation';
|
|
5
6
|
|
|
6
7
|
export let getAuthMethod = <ConfigType extends {}, AuthType extends {}>(
|
|
7
8
|
slate: Slate<ConfigType, AuthType>,
|
|
8
9
|
authenticationMethodId: string
|
|
9
10
|
) => {
|
|
10
|
-
let authMethod = slate.spec.auth.authStack.find(m => m.key
|
|
11
|
+
let authMethod = slate.spec.auth.authStack.find(m => m.key === authenticationMethodId);
|
|
11
12
|
if (!authMethod) {
|
|
12
13
|
throw new ServiceError(
|
|
13
14
|
badRequestError({
|
|
@@ -32,12 +33,13 @@ export let mapAuthMethod = <ConfigType extends {}, AuthType extends {}>(
|
|
|
32
33
|
? m.scopes.map(s => ({
|
|
33
34
|
id: s.scope,
|
|
34
35
|
title: s.title,
|
|
35
|
-
description: s.description
|
|
36
|
+
description: s.description,
|
|
37
|
+
defaultChecked: s.defaultChecked
|
|
36
38
|
}))
|
|
37
39
|
: undefined,
|
|
38
40
|
|
|
39
|
-
inputSchema: (m.inputSchema ?? z.object({}))
|
|
40
|
-
outputSchema: slate.spec.auth.outputSchema
|
|
41
|
+
inputSchema: toJsonSchema(m.inputSchema ?? z.object({})),
|
|
42
|
+
outputSchema: toJsonSchema(slate.spec.auth.outputSchema),
|
|
41
43
|
|
|
42
44
|
capabilities: {
|
|
43
45
|
getDefaultInput: { enabled: !!('getDefaultInput' in m && m.getDefaultInput) },
|
|
@@ -55,7 +57,7 @@ export let getAction = <ConfigType extends {}, AuthType extends {}>(
|
|
|
55
57
|
slate: Slate<ConfigType, AuthType>,
|
|
56
58
|
actionId: string
|
|
57
59
|
) => {
|
|
58
|
-
let action = slate.actions.find(m => m.key
|
|
60
|
+
let action = slate.actions.find(m => m.key === actionId);
|
|
59
61
|
if (!action) {
|
|
60
62
|
throw new ServiceError(notFoundError(`action`, actionId));
|
|
61
63
|
}
|
|
@@ -73,7 +75,7 @@ export let getActionWithType = <
|
|
|
73
75
|
actionId: string
|
|
74
76
|
): ReturnType<typeof getAction<ConfigType, AuthType>> & { type: Type } => {
|
|
75
77
|
let action = getAction(slate, actionId);
|
|
76
|
-
if (action.type
|
|
78
|
+
if (action.type !== type) {
|
|
77
79
|
throw new ServiceError(
|
|
78
80
|
badRequestError({
|
|
79
81
|
message: `Action with ID ${actionId} is not of type ${type}`
|
|
@@ -85,7 +87,7 @@ export let getActionWithType = <
|
|
|
85
87
|
};
|
|
86
88
|
|
|
87
89
|
export let mapAction = <ConfigType extends {}, AuthType extends {}>(
|
|
88
|
-
|
|
90
|
+
_slate: Slate<ConfigType, AuthType>,
|
|
89
91
|
a: ReturnType<typeof getAction<ConfigType, AuthType>>
|
|
90
92
|
): SlatesAction => {
|
|
91
93
|
let base = {
|
|
@@ -96,12 +98,13 @@ export let mapAction = <ConfigType extends {}, AuthType extends {}>(
|
|
|
96
98
|
constraints: a.constraints,
|
|
97
99
|
tags: a.tags,
|
|
98
100
|
metadata: a.metadata,
|
|
101
|
+
scopes: a.scopes,
|
|
99
102
|
|
|
100
|
-
inputSchema: a.inputSchema
|
|
101
|
-
outputSchema: a.outputSchema
|
|
103
|
+
inputSchema: toJsonSchema(a.inputSchema),
|
|
104
|
+
outputSchema: toJsonSchema(a.outputSchema)
|
|
102
105
|
};
|
|
103
106
|
|
|
104
|
-
if (a.type
|
|
107
|
+
if (a.type === 'tool') {
|
|
105
108
|
return {
|
|
106
109
|
...base,
|
|
107
110
|
type: 'action.tool',
|
|
@@ -115,7 +118,7 @@ export let mapAction = <ConfigType extends {}, AuthType extends {}>(
|
|
|
115
118
|
capabilities: {},
|
|
116
119
|
|
|
117
120
|
invocation:
|
|
118
|
-
a.source
|
|
121
|
+
a.source === 'polling'
|
|
119
122
|
? {
|
|
120
123
|
type: 'polling',
|
|
121
124
|
intervalSeconds: a.polling.intervalInSeconds ?? SlateDefaultPollingIntervalSeconds
|
package/src/validation.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ServiceError, validationError } from '@lowerdeck/error';
|
|
2
|
-
import z from 'zod';
|
|
2
|
+
import type z from 'zod';
|
|
3
3
|
|
|
4
4
|
export let zodToValidationError = (entity: string, message: string, e: z.ZodError) => {
|
|
5
5
|
return validationError({
|
|
@@ -25,3 +25,19 @@ export let validate = <T>(
|
|
|
25
25
|
|
|
26
26
|
return result.data;
|
|
27
27
|
};
|
|
28
|
+
|
|
29
|
+
export let toJsonSchema = (schema: z.ZodType<any>) =>
|
|
30
|
+
schema.toJSONSchema({
|
|
31
|
+
unrepresentable: 'any',
|
|
32
|
+
override: ctx => {
|
|
33
|
+
let def = ctx.zodSchema._zod.def;
|
|
34
|
+
|
|
35
|
+
if (def.type === 'date') {
|
|
36
|
+
ctx.jsonSchema.type = 'string';
|
|
37
|
+
ctx.jsonSchema.format = 'date-time';
|
|
38
|
+
}
|
|
39
|
+
if (def.type === 'bigint') {
|
|
40
|
+
ctx.jsonSchema.type = 'number';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|