@optimizely-opal/opal-tools-sdk 0.1.6-dev → 0.1.9-dev
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/README.md +201 -216
- package/dist/decorators.d.ts +2 -0
- package/dist/decorators.js +2 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +5 -4
- package/dist/models.d.ts +22 -1
- package/dist/models.js +28 -2
- package/dist/proteus.d.ts +1587 -0
- package/dist/proteus.js +98 -0
- package/dist/registerResource.d.ts +60 -0
- package/dist/registerResource.js +59 -0
- package/dist/registerTool.d.ts +18 -7
- package/dist/registerTool.js +52 -3
- package/dist/service.d.ts +15 -3
- package/dist/service.js +80 -21
- package/package.json +2 -2
- package/scripts/generate-proteus.ts +135 -0
- package/src/decorators.ts +4 -0
- package/src/index.ts +3 -2
- package/src/models.ts +27 -0
- package/src/proteus.ts +2314 -0
- package/src/registerResource.ts +82 -0
- package/src/registerTool.ts +19 -29
- package/src/service.ts +110 -23
- package/tests/integration.test.ts +252 -73
- package/tests/proteus.test.ts +122 -0
- package/dist/block.d.ts +0 -4760
- package/dist/block.js +0 -104
- package/scripts/generate-block.ts +0 -167
- package/src/block.ts +0 -11761
- package/tests/block.test.ts +0 -115
package/dist/block.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Generated by json-schema-to-typescript
|
|
4
|
-
* DO NOT MODIFY - This file is auto-generated from block-document-spec.json
|
|
5
|
-
* Run 'npm run generate:block' to regenerate
|
|
6
|
-
*/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.Block = void 0;
|
|
9
|
-
exports.isBlockResponse = isBlockResponse;
|
|
10
|
-
/**
|
|
11
|
-
* Builder namespace for Adaptive Block Document components.
|
|
12
|
-
*
|
|
13
|
-
* Usage:
|
|
14
|
-
* Block.Document({ children: [...] })
|
|
15
|
-
* Block.Heading({ children: "Title", level: "2" })
|
|
16
|
-
* Block.Input({ name: "field_name", placeholder: "Enter..." })
|
|
17
|
-
*/
|
|
18
|
-
exports.Block = {
|
|
19
|
-
Action: (props) => ({
|
|
20
|
-
$type: "Block.Action",
|
|
21
|
-
...props,
|
|
22
|
-
}),
|
|
23
|
-
Alert: (props) => ({
|
|
24
|
-
$type: "Block.Alert",
|
|
25
|
-
...props,
|
|
26
|
-
}),
|
|
27
|
-
Badge: (props) => ({
|
|
28
|
-
$type: "Block.Badge",
|
|
29
|
-
...props,
|
|
30
|
-
}),
|
|
31
|
-
Box: (props) => ({
|
|
32
|
-
$type: "Block.Box",
|
|
33
|
-
...props,
|
|
34
|
-
}),
|
|
35
|
-
CancelAction: (props) => ({ $type: "Block.CancelAction", ...props }),
|
|
36
|
-
Checkbox: (props) => ({
|
|
37
|
-
$type: "Block.Checkbox",
|
|
38
|
-
...props,
|
|
39
|
-
}),
|
|
40
|
-
Code: (props) => ({
|
|
41
|
-
$type: "Block.Code",
|
|
42
|
-
...props,
|
|
43
|
-
}),
|
|
44
|
-
Document: (props) => ({
|
|
45
|
-
$type: "Block.Document",
|
|
46
|
-
...props,
|
|
47
|
-
}),
|
|
48
|
-
Field: (props) => ({
|
|
49
|
-
$type: "Block.Field",
|
|
50
|
-
...props,
|
|
51
|
-
}),
|
|
52
|
-
Group: (props) => ({
|
|
53
|
-
$type: "Block.Group",
|
|
54
|
-
...props,
|
|
55
|
-
}),
|
|
56
|
-
Heading: (props) => ({
|
|
57
|
-
$type: "Block.Heading",
|
|
58
|
-
...props,
|
|
59
|
-
}),
|
|
60
|
-
Input: (props) => ({
|
|
61
|
-
$type: "Block.Input",
|
|
62
|
-
...props,
|
|
63
|
-
}),
|
|
64
|
-
Link: (props) => ({
|
|
65
|
-
$type: "Block.Link",
|
|
66
|
-
...props,
|
|
67
|
-
}),
|
|
68
|
-
Range: (props) => ({
|
|
69
|
-
$type: "Block.Range",
|
|
70
|
-
...props,
|
|
71
|
-
}),
|
|
72
|
-
Select: (props) => ({
|
|
73
|
-
$type: "Block.Select",
|
|
74
|
-
...props,
|
|
75
|
-
}),
|
|
76
|
-
Separator: (props) => ({
|
|
77
|
-
$type: "Block.Separator",
|
|
78
|
-
...props,
|
|
79
|
-
}),
|
|
80
|
-
Switch: (props) => ({
|
|
81
|
-
$type: "Block.Switch",
|
|
82
|
-
...props,
|
|
83
|
-
}),
|
|
84
|
-
Text: (props) => ({
|
|
85
|
-
$type: "Block.Text",
|
|
86
|
-
...props,
|
|
87
|
-
}),
|
|
88
|
-
Textarea: (props) => ({
|
|
89
|
-
$type: "Block.Textarea",
|
|
90
|
-
...props,
|
|
91
|
-
}),
|
|
92
|
-
};
|
|
93
|
-
/**
|
|
94
|
-
* Type guard to check if a value is a BlockResponse.
|
|
95
|
-
*/
|
|
96
|
-
function isBlockResponse(value) {
|
|
97
|
-
return (typeof value === "object" &&
|
|
98
|
-
value !== null &&
|
|
99
|
-
("content" in value ||
|
|
100
|
-
"data" in value ||
|
|
101
|
-
"artifact" in value ||
|
|
102
|
-
"rollback" in value ||
|
|
103
|
-
"error" in value));
|
|
104
|
-
}
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ts-node
|
|
2
|
-
/**
|
|
3
|
-
* Generate Adaptive Block Document types from JSON schema.
|
|
4
|
-
*
|
|
5
|
-
* This script uses json-schema-to-typescript to generate TypeScript interfaces
|
|
6
|
-
* from the block-document-spec.json schema. The generated types will replace the
|
|
7
|
-
* manual Block builders once we're ready to switch over.
|
|
8
|
-
*
|
|
9
|
-
* Usage:
|
|
10
|
-
* npm run generate:block
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { JSONSchema7 } from "json-schema";
|
|
14
|
-
|
|
15
|
-
import * as fs from "fs";
|
|
16
|
-
import { compile } from "json-schema-to-typescript";
|
|
17
|
-
import * as path from "path";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Generate builder functions and BlockResponse class.
|
|
21
|
-
*
|
|
22
|
-
* The generated TypeScript interfaces are great for validation, but we want to
|
|
23
|
-
* keep the builder API (Block.Document(), etc.) for ease of use.
|
|
24
|
-
*/
|
|
25
|
-
function generateBuilderCode(schema: JSONSchema7): string {
|
|
26
|
-
// Extract all Block components from definitions
|
|
27
|
-
const blockComponents: Array<{ fullName: string; shortName: string }> = [];
|
|
28
|
-
const skipTypes = new Set(["BlockElement", "BlockNode"]);
|
|
29
|
-
|
|
30
|
-
if (schema.definitions) {
|
|
31
|
-
for (const componentName of Object.keys(schema.definitions)) {
|
|
32
|
-
if (componentName.startsWith("Block") && !skipTypes.has(componentName)) {
|
|
33
|
-
// Extract the short name (e.g., "Document" from "BlockDocument")
|
|
34
|
-
const shortName = componentName.substring(5); // Remove "Block" prefix
|
|
35
|
-
blockComponents.push({ fullName: componentName, shortName });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Sort components for consistent output
|
|
41
|
-
blockComponents.sort((a, b) => a.shortName.localeCompare(b.shortName));
|
|
42
|
-
|
|
43
|
-
// Generate factory functions that return properly typed objects
|
|
44
|
-
const builderMethods = blockComponents
|
|
45
|
-
.map(
|
|
46
|
-
({ fullName, shortName }) =>
|
|
47
|
-
` ${shortName}: (props: Omit<${fullName}, '$type'>): ${fullName} => ({ $type: 'Block.${shortName}' as const, ...props }),`,
|
|
48
|
-
)
|
|
49
|
-
.join("\n");
|
|
50
|
-
|
|
51
|
-
return `
|
|
52
|
-
/**
|
|
53
|
-
* Builder namespace for Adaptive Block Document components.
|
|
54
|
-
*
|
|
55
|
-
* Usage:
|
|
56
|
-
* Block.Document({ children: [...] })
|
|
57
|
-
* Block.Heading({ children: "Title", level: "2" })
|
|
58
|
-
* Block.Input({ name: "field_name", placeholder: "Enter..." })
|
|
59
|
-
*/
|
|
60
|
-
export const Block = {
|
|
61
|
-
${builderMethods}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Response type for Adaptive Block Documents.
|
|
66
|
-
*
|
|
67
|
-
* A Block response is a structured tool return value with up to five optional fields:
|
|
68
|
-
* - content: UI representation (BlockDocument)
|
|
69
|
-
* - data: Extra context for LLM (optional, use only when needed beyond artifact/error)
|
|
70
|
-
* - artifact: Object that was impacted (optional)
|
|
71
|
-
* - rollback: Undo operation (optional)
|
|
72
|
-
* - error: Error information (optional)
|
|
73
|
-
*/
|
|
74
|
-
export type BlockResponse = {
|
|
75
|
-
content?: BlockDocument;
|
|
76
|
-
data?: Record<string, unknown>;
|
|
77
|
-
artifact?: {
|
|
78
|
-
type: string;
|
|
79
|
-
id: string;
|
|
80
|
-
data: Record<string, unknown>;
|
|
81
|
-
};
|
|
82
|
-
rollback?: {
|
|
83
|
-
type: 'endpoint' | 'tool';
|
|
84
|
-
config: Record<string, unknown>;
|
|
85
|
-
label?: string;
|
|
86
|
-
};
|
|
87
|
-
error?: {
|
|
88
|
-
message: string;
|
|
89
|
-
code?: string;
|
|
90
|
-
details?: Record<string, unknown>;
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Type guard to check if a value is a BlockResponse.
|
|
96
|
-
*/
|
|
97
|
-
export function isBlockResponse(value: unknown): value is BlockResponse {
|
|
98
|
-
return (
|
|
99
|
-
typeof value === 'object' &&
|
|
100
|
-
value !== null &&
|
|
101
|
-
('content' in value || 'data' in value || 'artifact' in value || 'rollback' in value || 'error' in value)
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function main() {
|
|
108
|
-
// Paths
|
|
109
|
-
const scriptDir = __dirname;
|
|
110
|
-
const sdkRoot = path.join(scriptDir, "..");
|
|
111
|
-
const schemaFile = path.join(sdkRoot, "..", "block-document-spec.json");
|
|
112
|
-
const outputFile = path.join(sdkRoot, "src", "block.ts");
|
|
113
|
-
|
|
114
|
-
if (!fs.existsSync(schemaFile)) {
|
|
115
|
-
console.error(`Error: Schema file not found at ${schemaFile}`);
|
|
116
|
-
process.exit(1);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
// Read the schema
|
|
121
|
-
const schema = JSON.parse(fs.readFileSync(schemaFile, "utf-8"));
|
|
122
|
-
|
|
123
|
-
// Create a new schema that references all definitions to force their generation
|
|
124
|
-
const schemaWithExports = {
|
|
125
|
-
...schema,
|
|
126
|
-
definitions: schema.definitions,
|
|
127
|
-
// Export each definition by creating a oneOf at the root
|
|
128
|
-
oneOf: Object.keys(schema.definitions || {})
|
|
129
|
-
.filter((key) => key.startsWith("Block"))
|
|
130
|
-
.map((key) => ({ $ref: `#/definitions/${key}` })),
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// Generate TypeScript types
|
|
134
|
-
const ts = await compile(schemaWithExports, "BlockDocumentSchema", {
|
|
135
|
-
bannerComment: `/**
|
|
136
|
-
* Generated by json-schema-to-typescript
|
|
137
|
-
* DO NOT MODIFY - This file is auto-generated from block-document-spec.json
|
|
138
|
-
* Run 'npm run generate:block' to regenerate
|
|
139
|
-
*/`,
|
|
140
|
-
declareExternallyReferenced: true,
|
|
141
|
-
strictIndexSignatures: true,
|
|
142
|
-
style: {
|
|
143
|
-
semi: true,
|
|
144
|
-
singleQuote: true,
|
|
145
|
-
},
|
|
146
|
-
unknownAny: false,
|
|
147
|
-
unreachableDefinitions: false,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
// Post-process to add builder functions and BlockResponse
|
|
151
|
-
const finalContent = ts + "\n" + generateBuilderCode(schema);
|
|
152
|
-
|
|
153
|
-
// Write the output file
|
|
154
|
-
fs.writeFileSync(outputFile, finalContent, "utf-8");
|
|
155
|
-
|
|
156
|
-
console.log(`Generated: ${outputFile}`);
|
|
157
|
-
} catch (error) {
|
|
158
|
-
console.error("Error: Generation failed");
|
|
159
|
-
console.error(error);
|
|
160
|
-
process.exit(1);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
main().catch((error) => {
|
|
165
|
-
console.error(error);
|
|
166
|
-
process.exit(1);
|
|
167
|
-
});
|