@kubohiroya/turbowarp-openai-realtime-api 0.1.0 → 0.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/README.ja.md +4 -2
- package/README.md +16 -2
- package/assets/block-icon.svg +1 -0
- package/dist/turbowarp-openai-realtime-api.js +2190 -1570
- package/dist/types/extension.d.ts +2 -0
- package/dist/types/fixed-input-editors.d.ts +15 -0
- package/package.json +15 -6
- package/schemas/block-editor-fixed-inputs.schema.json +67 -0
- package/src/extension.ts +8 -1
- package/src/fixed-input-editors.ts +214 -0
- package/dist/types/extension-manifest.d.ts +0 -30
- package/schemas/extension-manifest.schema.json +0 -49
- package/src/extension-manifest.ts +0 -149
|
@@ -7,6 +7,8 @@ export type RealtimeExtensionDependencies = Omit<RealtimeCompositionOptions, 'fu
|
|
|
7
7
|
type Args = Record<string, unknown>;
|
|
8
8
|
/** Block surface over the Realtime composition. */
|
|
9
9
|
export declare class OpenAIRealtimeExtension implements TurboWarpExtension {
|
|
10
|
+
private readonly fixedEditorRuntime;
|
|
11
|
+
private fixedEditorsDispose;
|
|
10
12
|
private lastErrorMessage;
|
|
11
13
|
private readonly realtime;
|
|
12
14
|
constructor(deps: RealtimeExtensionDependencies);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FixedInputEditorOptions, JsonValue, LiteralValue } from "@kubohiroya/turbowarp-extension-data-editor";
|
|
2
|
+
interface InputSchema {
|
|
3
|
+
readonly type: string;
|
|
4
|
+
readonly enum?: readonly (string | number)[];
|
|
5
|
+
readonly minimum?: number;
|
|
6
|
+
readonly maximum?: number;
|
|
7
|
+
readonly exclusiveMinimum?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function readNumber(value: LiteralValue): number;
|
|
10
|
+
export declare function newNumberToken(value: JsonValue): string;
|
|
11
|
+
export declare function candidateValid(_opcode: string, _argument: string, schema: InputSchema, value: LiteralValue): boolean;
|
|
12
|
+
export declare function fixedInputEditorOptions(): readonly FixedInputEditorOptions[];
|
|
13
|
+
/** Register only visible static contracts; ordinary composition consumers load no editor runtime. */
|
|
14
|
+
export declare function registerFixedInputEditors(scratch: unknown, runtime: unknown, visibleBlocks: readonly unknown[]): (() => void) | undefined;
|
|
15
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-openai-realtime-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A TurboWarp extension for the OpenAI Realtime API, using a localhost relay for credentials.",
|
|
5
5
|
"author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -25,12 +25,13 @@
|
|
|
25
25
|
],
|
|
26
26
|
"type": "module",
|
|
27
27
|
"files": [
|
|
28
|
+
"assets/",
|
|
28
29
|
"dist/",
|
|
29
|
-
"schemas/",
|
|
30
30
|
"src/",
|
|
31
31
|
"README.md",
|
|
32
32
|
"README.ja.md",
|
|
33
|
-
"LICENSE"
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"schemas"
|
|
34
35
|
],
|
|
35
36
|
"publishConfig": {
|
|
36
37
|
"access": "public",
|
|
@@ -56,14 +57,16 @@
|
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"@eslint/js": "^9.39.5",
|
|
59
|
-
"@kubohiroya/
|
|
60
|
+
"@kubohiroya/turbowarp-extension-manifest": "^0.3.2",
|
|
61
|
+
"@kubohiroya/vite-plugin-turbowarp-extension": "^0.8.0",
|
|
60
62
|
"@types/node": "^24.13.3",
|
|
61
63
|
"eslint": "^9.39.5",
|
|
62
64
|
"jiti": "^2.7.0",
|
|
63
65
|
"typescript": "^5.9.3",
|
|
64
66
|
"typescript-eslint": "^8.66.0",
|
|
65
67
|
"vite": "^8.2.2",
|
|
66
|
-
"vitest": "^5.0.0"
|
|
68
|
+
"vitest": "^5.0.0",
|
|
69
|
+
"@kubohiroya/turbowarp-extension-data-editor": "^0.3.4"
|
|
67
70
|
},
|
|
68
71
|
"exports": {
|
|
69
72
|
"./composition": {
|
|
@@ -74,6 +77,12 @@
|
|
|
74
77
|
"./package.json": "./package.json"
|
|
75
78
|
},
|
|
76
79
|
"dependencies": {
|
|
77
|
-
"@kubohiroya/turbowarp-named-functions": "0.
|
|
80
|
+
"@kubohiroya/turbowarp-named-functions": "^0.2.0"
|
|
81
|
+
},
|
|
82
|
+
"gitHead": "4f6667801dc77fff26e1bd65d6b163d8d27399cf",
|
|
83
|
+
"x-turbowarp-source": {
|
|
84
|
+
"revision": "4f6667801dc77fff26e1bd65d6b163d8d27399cf",
|
|
85
|
+
"revisionRole": "base-commit",
|
|
86
|
+
"sourceState": "local-release-overlay"
|
|
78
87
|
}
|
|
79
88
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"$id": "https://kubohiroya.github.io/turbowarp-openai-realtime-api/schemas/block-editor-fixed-inputs.schema.json",
|
|
4
|
+
"title": "turbowarp-openai-realtime-api fixed editing contracts",
|
|
5
|
+
"$defs": {
|
|
6
|
+
"setOutputMode": {
|
|
7
|
+
"type": "object",
|
|
8
|
+
"properties": {
|
|
9
|
+
"MODE": {
|
|
10
|
+
"type": "string",
|
|
11
|
+
"enum": ["audio", "text"]
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"additionalProperties": false
|
|
15
|
+
},
|
|
16
|
+
"setSessionTimeLimit": {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"properties": {
|
|
19
|
+
"SECONDS": {
|
|
20
|
+
"type": "number",
|
|
21
|
+
"minimum": 0
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"additionalProperties": false
|
|
25
|
+
},
|
|
26
|
+
"connect": {
|
|
27
|
+
"type": "object",
|
|
28
|
+
"properties": {
|
|
29
|
+
"MICROPHONE": {
|
|
30
|
+
"type": "string",
|
|
31
|
+
"enum": ["on", "off"]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"additionalProperties": false
|
|
35
|
+
},
|
|
36
|
+
"defineFunction": {
|
|
37
|
+
"type": "object",
|
|
38
|
+
"properties": {
|
|
39
|
+
"EXPORT": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"enum": ["tool", "none"]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"additionalProperties": false
|
|
45
|
+
},
|
|
46
|
+
"usageValue": {
|
|
47
|
+
"type": "object",
|
|
48
|
+
"properties": {
|
|
49
|
+
"FIELD": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"enum": [
|
|
52
|
+
"costUSD",
|
|
53
|
+
"responses",
|
|
54
|
+
"inputTokens",
|
|
55
|
+
"outputTokens",
|
|
56
|
+
"cachedInputTokens",
|
|
57
|
+
"textInputTokens",
|
|
58
|
+
"audioInputTokens",
|
|
59
|
+
"textOutputTokens",
|
|
60
|
+
"audioOutputTokens"
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"additionalProperties": false
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
package/src/extension.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {registerFixedInputEditors} from './fixed-input-editors.js';
|
|
1
2
|
import {extensionConfig} from './config';
|
|
2
3
|
import definitions from './block-definitions.json';
|
|
3
4
|
import {
|
|
@@ -61,10 +62,14 @@ const USAGE_FIELDS: Record<string, (usage: UsageTotals) => number> = {
|
|
|
61
62
|
|
|
62
63
|
/** Block surface over the Realtime composition. */
|
|
63
64
|
export class OpenAIRealtimeExtension implements TurboWarpExtension {
|
|
65
|
+
private readonly fixedEditorRuntime: unknown;
|
|
66
|
+
private fixedEditorsDispose: (() => void) | undefined;
|
|
67
|
+
|
|
64
68
|
private lastErrorMessage = '';
|
|
65
69
|
private readonly realtime: RealtimeComposition;
|
|
66
70
|
|
|
67
71
|
public constructor(deps: RealtimeExtensionDependencies) {
|
|
72
|
+
this.fixedEditorRuntime = deps.runtime;
|
|
68
73
|
this.realtime = createRealtimeComposition({...deps, functionHatOpcode: DEFINE_FUNCTION_OPCODE});
|
|
69
74
|
this.realtime.subscribe((event) => {
|
|
70
75
|
if (event.type === 'response') deps.runtime.startHats(RESPONSE_DONE_OPCODE);
|
|
@@ -74,7 +79,7 @@ export class OpenAIRealtimeExtension implements TurboWarpExtension {
|
|
|
74
79
|
}
|
|
75
80
|
|
|
76
81
|
public getInfo(): Record<string, unknown> {
|
|
77
|
-
|
|
82
|
+
const info = {
|
|
78
83
|
id: extensionConfig.id,
|
|
79
84
|
name: Scratch.translate(definitions.extensionName),
|
|
80
85
|
docsURI: extensionConfig.docsURI,
|
|
@@ -87,6 +92,8 @@ export class OpenAIRealtimeExtension implements TurboWarpExtension {
|
|
|
87
92
|
])
|
|
88
93
|
)
|
|
89
94
|
};
|
|
95
|
+
this.fixedEditorsDispose ??= registerFixedInputEditors(Scratch, this.fixedEditorRuntime, info.blocks);
|
|
96
|
+
return info;
|
|
90
97
|
}
|
|
91
98
|
|
|
92
99
|
// ---- relay ------------------------------------------------------------------------------
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FixedInputBinding,
|
|
3
|
+
FixedInputEditorOptions,
|
|
4
|
+
JsonValue,
|
|
5
|
+
LiteralValue,
|
|
6
|
+
} from "@kubohiroya/turbowarp-extension-data-editor";
|
|
7
|
+
import type { SchemaBundle } from "@kubohiroya/turbowarp-extension-data-editor/schema";
|
|
8
|
+
import schemas from "../schemas/block-editor-fixed-inputs.schema.json" with { type: "json" };
|
|
9
|
+
interface InputSchema {
|
|
10
|
+
readonly type: string;
|
|
11
|
+
readonly enum?: readonly (string | number)[];
|
|
12
|
+
readonly minimum?: number;
|
|
13
|
+
readonly maximum?: number;
|
|
14
|
+
readonly exclusiveMinimum?: number;
|
|
15
|
+
}
|
|
16
|
+
const contracts = schemas.$defs as Readonly<
|
|
17
|
+
Record<string, { readonly properties: Readonly<Record<string, InputSchema>> }>
|
|
18
|
+
>;
|
|
19
|
+
const repository = "turbowarp-openai-realtime-api";
|
|
20
|
+
const extensionId = "kubohiroyaopenairealtime";
|
|
21
|
+
const token = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/u;
|
|
22
|
+
export function readNumber(value: LiteralValue): number {
|
|
23
|
+
if (typeof value !== "string" && typeof value !== "number")
|
|
24
|
+
throw new TypeError("Invalid numeric projection.");
|
|
25
|
+
if (typeof value === "string" && value.trim() === "")
|
|
26
|
+
throw new TypeError("Existing blank field is readonly.");
|
|
27
|
+
const number = Number(value);
|
|
28
|
+
if (!Number.isFinite(number))
|
|
29
|
+
throw new TypeError("Existing nonfinite field is readonly.");
|
|
30
|
+
return number;
|
|
31
|
+
}
|
|
32
|
+
export function newNumberToken(value: JsonValue): string {
|
|
33
|
+
if (typeof value === "number") {
|
|
34
|
+
if (!Number.isFinite(value)) throw new TypeError("Invalid new number.");
|
|
35
|
+
return Object.is(value, -0) ? "-0" : String(value);
|
|
36
|
+
}
|
|
37
|
+
if (
|
|
38
|
+
typeof value !== "string" ||
|
|
39
|
+
!token.test(value.trim()) ||
|
|
40
|
+
!Number.isFinite(Number(value))
|
|
41
|
+
)
|
|
42
|
+
throw new TypeError("Invalid new number.");
|
|
43
|
+
return value.trim();
|
|
44
|
+
}
|
|
45
|
+
function existingChoice(_opcode: string, value: LiteralValue): string {
|
|
46
|
+
if (typeof value !== "string")
|
|
47
|
+
throw new TypeError("Invalid string projection.");
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
export function candidateValid(
|
|
51
|
+
_opcode: string,
|
|
52
|
+
_argument: string,
|
|
53
|
+
schema: InputSchema,
|
|
54
|
+
value: LiteralValue,
|
|
55
|
+
): boolean {
|
|
56
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
57
|
+
let number;
|
|
58
|
+
try {
|
|
59
|
+
number = readNumber(value);
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (schema.type === "integer" && !Number.isInteger(number)) return false;
|
|
64
|
+
if (schema.minimum !== undefined && number < schema.minimum) return false;
|
|
65
|
+
if (schema.maximum !== undefined && number > schema.maximum) return false;
|
|
66
|
+
if (
|
|
67
|
+
schema.exclusiveMinimum !== undefined &&
|
|
68
|
+
number <= schema.exclusiveMinimum
|
|
69
|
+
)
|
|
70
|
+
return false;
|
|
71
|
+
if (schema.enum && !schema.enum!.includes(number)) return false;
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return typeof value === "string" && schema.enum!.includes(value);
|
|
75
|
+
}
|
|
76
|
+
export function fixedInputEditorOptions(): readonly FixedInputEditorOptions[] {
|
|
77
|
+
return Object.entries(contracts).map(([opcode, contract]) => ({
|
|
78
|
+
apiVersion: 1,
|
|
79
|
+
extensionId,
|
|
80
|
+
editorId: `${extensionId}.fixed.${opcode}`,
|
|
81
|
+
opcodes: [`${extensionId}_${opcode}`],
|
|
82
|
+
contractId: `${repository}.${opcode}.fixed`,
|
|
83
|
+
contractRevision: "fixed-inputs-v1",
|
|
84
|
+
validationContext: () => ({
|
|
85
|
+
consumerId: repository,
|
|
86
|
+
nowMs: 0,
|
|
87
|
+
settings: null,
|
|
88
|
+
}),
|
|
89
|
+
bindings: Object.entries(contract.properties).map(
|
|
90
|
+
([argument, schema]): FixedInputBinding => {
|
|
91
|
+
const numeric = schema.type === "number" || schema.type === "integer";
|
|
92
|
+
const source = {
|
|
93
|
+
repository,
|
|
94
|
+
path: "schemas/block-editor-fixed-inputs.schema.json",
|
|
95
|
+
symbol: `/$defs/${opcode}/properties/${argument}`,
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
argument,
|
|
99
|
+
schemaBundle: {
|
|
100
|
+
identity: {
|
|
101
|
+
ownerId: repository,
|
|
102
|
+
contractId: `${opcode}.${argument}`,
|
|
103
|
+
revision: "fixed-inputs-v1",
|
|
104
|
+
variantId: "fixed",
|
|
105
|
+
},
|
|
106
|
+
dialect: "draft-07",
|
|
107
|
+
root: {
|
|
108
|
+
retrievalUri: `${schemas.$id}?binding=${opcode}.${argument}`,
|
|
109
|
+
document: schema as unknown as SchemaBundle["root"]["document"],
|
|
110
|
+
artifact: { role: "canonical-schema", source },
|
|
111
|
+
},
|
|
112
|
+
resources: [],
|
|
113
|
+
provenance: {
|
|
114
|
+
canonicalSources: [
|
|
115
|
+
source,
|
|
116
|
+
{ repository, path: "src/block-definitions.json" },
|
|
117
|
+
{ repository, path: "src/extension.ts", symbol: opcode },
|
|
118
|
+
],
|
|
119
|
+
generatedFiles: [],
|
|
120
|
+
referenceSchemas: [],
|
|
121
|
+
generationSources: [],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
control: numeric
|
|
125
|
+
? { kind: "number" }
|
|
126
|
+
: {
|
|
127
|
+
kind: "enum",
|
|
128
|
+
choices: schema.enum!.map((value) => ({
|
|
129
|
+
value,
|
|
130
|
+
label: String(value),
|
|
131
|
+
})),
|
|
132
|
+
},
|
|
133
|
+
read: (value) =>
|
|
134
|
+
numeric ? readNumber(value) : existingChoice(opcode, value),
|
|
135
|
+
encode: (value) => {
|
|
136
|
+
if (numeric) return newNumberToken(value);
|
|
137
|
+
if (typeof value !== "string" || !schema.enum!.includes(value))
|
|
138
|
+
throw new TypeError("Invalid new enum value.");
|
|
139
|
+
return value;
|
|
140
|
+
},
|
|
141
|
+
validateCandidate: (value) =>
|
|
142
|
+
candidateValid(opcode, argument, schema, value)
|
|
143
|
+
? []
|
|
144
|
+
: [
|
|
145
|
+
{
|
|
146
|
+
path: argument,
|
|
147
|
+
message: "Check this value before saving.",
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
),
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface RegistrationRuntime {
|
|
157
|
+
on?(event: string, listener: () => void): void;
|
|
158
|
+
off?(event: string, listener: () => void): void;
|
|
159
|
+
}
|
|
160
|
+
/** Register only visible static contracts; ordinary composition consumers load no editor runtime. */
|
|
161
|
+
export function registerFixedInputEditors(
|
|
162
|
+
scratch: unknown,
|
|
163
|
+
runtime: unknown,
|
|
164
|
+
visibleBlocks: readonly unknown[],
|
|
165
|
+
): (() => void) | undefined {
|
|
166
|
+
const api = (
|
|
167
|
+
scratch as {
|
|
168
|
+
gui?: {
|
|
169
|
+
blockEditors?: {
|
|
170
|
+
apiVersion?: number;
|
|
171
|
+
registerFixedInputEditor?: (
|
|
172
|
+
options: FixedInputEditorOptions,
|
|
173
|
+
) => () => void;
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
).gui?.blockEditors;
|
|
178
|
+
if (
|
|
179
|
+
api?.apiVersion !== 1 ||
|
|
180
|
+
typeof api.registerFixedInputEditor !== "function"
|
|
181
|
+
)
|
|
182
|
+
return undefined;
|
|
183
|
+
const visibleOpcodes = new Set(
|
|
184
|
+
visibleBlocks.flatMap((block) => {
|
|
185
|
+
if (typeof block !== "object" || block === null || !("opcode" in block))
|
|
186
|
+
return [];
|
|
187
|
+
return typeof block.opcode === "string" ? [block.opcode] : [];
|
|
188
|
+
}),
|
|
189
|
+
);
|
|
190
|
+
const events = runtime as RegistrationRuntime | undefined;
|
|
191
|
+
const removers: (() => void)[] = [];
|
|
192
|
+
let active = true;
|
|
193
|
+
const dispose = () => {
|
|
194
|
+
if (!active) return;
|
|
195
|
+
active = false;
|
|
196
|
+
events?.off?.("RUNTIME_DISPOSED", dispose);
|
|
197
|
+
for (const remove of removers.splice(0).reverse()) remove();
|
|
198
|
+
};
|
|
199
|
+
try {
|
|
200
|
+
for (const options of fixedInputEditorOptions()) {
|
|
201
|
+
if (
|
|
202
|
+
options.opcodes.some((opcode) =>
|
|
203
|
+
visibleOpcodes.has(opcode.slice(extensionId.length + 1)),
|
|
204
|
+
)
|
|
205
|
+
)
|
|
206
|
+
removers.push(api.registerFixedInputEditor.call(api, options));
|
|
207
|
+
}
|
|
208
|
+
events?.on?.("RUNTIME_DISPOSED", dispose);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
dispose();
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
return dispose;
|
|
214
|
+
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from 'vite';
|
|
2
|
-
export declare const EXTENSION_MANIFEST_FORMAT_VERSION: 1;
|
|
3
|
-
export interface ExtensionManifestArgument {
|
|
4
|
-
id: string;
|
|
5
|
-
type: string;
|
|
6
|
-
menu?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface ExtensionManifestBlock {
|
|
9
|
-
opcode: string;
|
|
10
|
-
blockType: string;
|
|
11
|
-
arguments: ExtensionManifestArgument[];
|
|
12
|
-
}
|
|
13
|
-
export interface ExtensionManifestMenu {
|
|
14
|
-
id: string;
|
|
15
|
-
acceptReporters: boolean;
|
|
16
|
-
}
|
|
17
|
-
export interface ExtensionManifest {
|
|
18
|
-
formatVersion: typeof EXTENSION_MANIFEST_FORMAT_VERSION;
|
|
19
|
-
id: string;
|
|
20
|
-
blocks: ExtensionManifestBlock[];
|
|
21
|
-
menus: ExtensionManifestMenu[];
|
|
22
|
-
}
|
|
23
|
-
export interface ExtensionManifestPluginOptions {
|
|
24
|
-
id: string;
|
|
25
|
-
definitions: unknown;
|
|
26
|
-
fileName?: string;
|
|
27
|
-
}
|
|
28
|
-
export declare function createExtensionManifest(id: string, definitions: unknown): ExtensionManifest;
|
|
29
|
-
export declare function serializeExtensionManifest(id: string, definitions: unknown): string;
|
|
30
|
-
export declare function extensionManifestPlugin(options: ExtensionManifestPluginOptions): Plugin;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
-
"$id": "https://raw.githubusercontent.com/kubohiroya/turbowarp-openai-realtime-api/main/schemas/extension-manifest.schema.json",
|
|
4
|
-
"title": "TurboWarp Extension API Manifest",
|
|
5
|
-
"type": "object",
|
|
6
|
-
"additionalProperties": false,
|
|
7
|
-
"required": ["formatVersion", "id", "blocks", "menus"],
|
|
8
|
-
"properties": {
|
|
9
|
-
"formatVersion": {"const": 1},
|
|
10
|
-
"id": {"type": "string", "pattern": "^[a-z0-9]+$"},
|
|
11
|
-
"blocks": {
|
|
12
|
-
"type": "array",
|
|
13
|
-
"items": {
|
|
14
|
-
"type": "object",
|
|
15
|
-
"additionalProperties": false,
|
|
16
|
-
"required": ["opcode", "blockType", "arguments"],
|
|
17
|
-
"properties": {
|
|
18
|
-
"opcode": {"type": "string", "minLength": 1},
|
|
19
|
-
"blockType": {"type": "string", "minLength": 1},
|
|
20
|
-
"arguments": {
|
|
21
|
-
"type": "array",
|
|
22
|
-
"items": {
|
|
23
|
-
"type": "object",
|
|
24
|
-
"additionalProperties": false,
|
|
25
|
-
"required": ["id", "type"],
|
|
26
|
-
"properties": {
|
|
27
|
-
"id": {"type": "string", "minLength": 1},
|
|
28
|
-
"type": {"type": "string", "minLength": 1},
|
|
29
|
-
"menu": {"type": "string", "minLength": 1}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
"menus": {
|
|
37
|
-
"type": "array",
|
|
38
|
-
"items": {
|
|
39
|
-
"type": "object",
|
|
40
|
-
"additionalProperties": false,
|
|
41
|
-
"required": ["id", "acceptReporters"],
|
|
42
|
-
"properties": {
|
|
43
|
-
"id": {"type": "string", "minLength": 1},
|
|
44
|
-
"acceptReporters": {"type": "boolean"}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import type {Plugin} from 'vite';
|
|
2
|
-
|
|
3
|
-
export const EXTENSION_MANIFEST_FORMAT_VERSION = 1 as const;
|
|
4
|
-
|
|
5
|
-
export interface ExtensionManifestArgument {
|
|
6
|
-
id: string;
|
|
7
|
-
type: string;
|
|
8
|
-
menu?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ExtensionManifestBlock {
|
|
12
|
-
opcode: string;
|
|
13
|
-
blockType: string;
|
|
14
|
-
arguments: ExtensionManifestArgument[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface ExtensionManifestMenu {
|
|
18
|
-
id: string;
|
|
19
|
-
acceptReporters: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface ExtensionManifest {
|
|
23
|
-
formatVersion: typeof EXTENSION_MANIFEST_FORMAT_VERSION;
|
|
24
|
-
id: string;
|
|
25
|
-
blocks: ExtensionManifestBlock[];
|
|
26
|
-
menus: ExtensionManifestMenu[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface ExtensionManifestPluginOptions {
|
|
30
|
-
id: string;
|
|
31
|
-
definitions: unknown;
|
|
32
|
-
fileName?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function createExtensionManifest(id: string, definitions: unknown): ExtensionManifest {
|
|
36
|
-
if (!/^[a-z0-9]+$/.test(id)) {
|
|
37
|
-
throw new TypeError('Extension manifest ID must contain only lowercase letters and numbers.');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const source = requireRecord(definitions, 'Block definitions');
|
|
41
|
-
const sourceBlocks = source.blocks;
|
|
42
|
-
if (!Array.isArray(sourceBlocks)) {
|
|
43
|
-
throw new TypeError('Block definitions must contain a blocks array.');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const menus = normalizeMenus(source.menus);
|
|
47
|
-
const menuIds = new Set(menus.map((menu) => menu.id));
|
|
48
|
-
const seenOpcodes = new Set<string>();
|
|
49
|
-
const blocks = sourceBlocks.map((block, index) => {
|
|
50
|
-
const normalized = normalizeBlock(block, index, menuIds);
|
|
51
|
-
if (seenOpcodes.has(normalized.opcode)) {
|
|
52
|
-
throw new TypeError(`Duplicate block opcode: ${normalized.opcode}`);
|
|
53
|
-
}
|
|
54
|
-
seenOpcodes.add(normalized.opcode);
|
|
55
|
-
return normalized;
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
formatVersion: EXTENSION_MANIFEST_FORMAT_VERSION,
|
|
60
|
-
id,
|
|
61
|
-
blocks: blocks.sort((left, right) => compareIds(left.opcode, right.opcode)),
|
|
62
|
-
menus
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function serializeExtensionManifest(id: string, definitions: unknown): string {
|
|
67
|
-
return `${JSON.stringify(createExtensionManifest(id, definitions), null, 2)}\n`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function extensionManifestPlugin(options: ExtensionManifestPluginOptions): Plugin {
|
|
71
|
-
return {
|
|
72
|
-
name: 'extension-api-manifest',
|
|
73
|
-
apply: 'build',
|
|
74
|
-
enforce: 'post',
|
|
75
|
-
generateBundle() {
|
|
76
|
-
this.emitFile({
|
|
77
|
-
type: 'asset',
|
|
78
|
-
fileName: options.fileName ?? 'extension-manifest.json',
|
|
79
|
-
source: serializeExtensionManifest(options.id, options.definitions)
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function normalizeBlock(
|
|
86
|
-
value: unknown,
|
|
87
|
-
index: number,
|
|
88
|
-
menuIds: ReadonlySet<string>
|
|
89
|
-
): ExtensionManifestBlock {
|
|
90
|
-
const block = requireRecord(value, `Block at index ${index}`);
|
|
91
|
-
const opcode = requireNonEmptyString(block.opcode, `Block at index ${index} opcode`);
|
|
92
|
-
const blockType = requireNonEmptyString(block.blockType, `Block ${opcode} blockType`);
|
|
93
|
-
const sourceArguments = block.arguments ?? {};
|
|
94
|
-
const argumentRecord = requireRecord(sourceArguments, `Block ${opcode} arguments`);
|
|
95
|
-
const argumentsList = Object.entries(argumentRecord).map(([argumentId, argument]) => {
|
|
96
|
-
requireNonEmptyString(argumentId, `Block ${opcode} argument ID`);
|
|
97
|
-
const definition = requireRecord(argument, `Block ${opcode} argument ${argumentId}`);
|
|
98
|
-
const type = requireNonEmptyString(
|
|
99
|
-
definition.type,
|
|
100
|
-
`Block ${opcode} argument ${argumentId} type`
|
|
101
|
-
);
|
|
102
|
-
const menu = definition.menu;
|
|
103
|
-
if (menu !== undefined && (typeof menu !== 'string' || !menuIds.has(menu))) {
|
|
104
|
-
throw new TypeError(`Block ${opcode} argument ${argumentId} references unknown menu: ${menu}`);
|
|
105
|
-
}
|
|
106
|
-
return menu === undefined ? {id: argumentId, type} : {id: argumentId, type, menu};
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
opcode,
|
|
111
|
-
blockType,
|
|
112
|
-
arguments: argumentsList.sort((left, right) => compareIds(left.id, right.id))
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function normalizeMenus(value: unknown): ExtensionManifestMenu[] {
|
|
117
|
-
const menuRecord = requireRecord(value ?? {}, 'Block definition menus');
|
|
118
|
-
return Object.entries(menuRecord)
|
|
119
|
-
.map(([id, menu]) => {
|
|
120
|
-
requireNonEmptyString(id, 'Menu ID');
|
|
121
|
-
const definition = requireRecord(menu, `Menu ${id}`);
|
|
122
|
-
const acceptReporters = definition.acceptReporters ?? false;
|
|
123
|
-
if (typeof acceptReporters !== 'boolean') {
|
|
124
|
-
throw new TypeError(`Menu ${id} acceptReporters must be a boolean.`);
|
|
125
|
-
}
|
|
126
|
-
return {id, acceptReporters};
|
|
127
|
-
})
|
|
128
|
-
.sort((left, right) => compareIds(left.id, right.id));
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
132
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
133
|
-
throw new TypeError(`${label} must be an object.`);
|
|
134
|
-
}
|
|
135
|
-
return value as Record<string, unknown>;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function requireNonEmptyString(value: unknown, label: string): string {
|
|
139
|
-
if (typeof value !== 'string' || value.length === 0) {
|
|
140
|
-
throw new TypeError(`${label} must be a non-empty string.`);
|
|
141
|
-
}
|
|
142
|
-
return value;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function compareIds(left: string, right: string): number {
|
|
146
|
-
if (left < right) return -1;
|
|
147
|
-
if (left > right) return 1;
|
|
148
|
-
return 0;
|
|
149
|
-
}
|