@kubohiroya/turbowarp-openai-realtime-api 0.1.1 → 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.
@@ -7,1855 +7,2195 @@
7
7
  (function (Scratch) {
8
8
  'use strict';
9
9
 
10
- //#region src/config.ts
11
- var extensionConfig = {
12
- id: "kubohiroyaopenairealtime",
13
- slug: "turbowarp-openai-realtime-api",
14
- name: "TurboWarp-OpenAI-Realtime-API",
15
- description: "Talk with the OpenAI Realtime API from TurboWarp through a localhost relay.",
16
- author: "Hiroya Kubo",
17
- license: "MPL-2.0",
18
- unsandboxed: true,
19
- docsURI: "https://kubohiroya.github.io/turbowarp-openai-realtime-api/",
20
- blockIconURI: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCI+PHBhdGggZD0iTTggMTBoMzJhNCA0IDAgMCAxIDQgNHYxNmE0IDQgMCAwIDEtNCA0SDIybC05IDd2LTdIOGE0IDQgMCAwIDEtNC00VjE0YTQgNCAwIDAgMSA0LTR6IiBmaWxsPSIjMTBBMzdGIi8+PGcgc3Ryb2tlPSIjZmZmIiBzdHJva2Utd2lkdGg9IjMiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCI+PHBhdGggZD0iTTE0IDE5djYiLz48cGF0aCBkPSJNMjAgMTZ2MTIiLz48cGF0aCBkPSJNMjYgMTh2OCIvPjxwYXRoIGQ9Ik0zMiAxNXYxNCIvPjxwYXRoIGQ9Ik0zOCAyMHY0Ii8+PC9nPjwvc3ZnPg=="
21
- };
22
- var block_editor_fixed_inputs_schema_default = {
23
- $schema: "http://json-schema.org/draft-07/schema#",
24
- $id: "https://kubohiroya.github.io/turbowarp-openai-realtime-api/schemas/block-editor-fixed-inputs.schema.json",
25
- title: "turbowarp-openai-realtime-api fixed editing contracts",
26
- $defs: {
27
- "setOutputMode": {
28
- "type": "object",
29
- "properties": { "MODE": {
30
- "type": "string",
31
- "enum": ["audio", "text"]
32
- } },
33
- "additionalProperties": false
34
- },
35
- "setSessionTimeLimit": {
36
- "type": "object",
37
- "properties": { "SECONDS": {
38
- "type": "number",
39
- "minimum": 0
40
- } },
41
- "additionalProperties": false
42
- },
43
- "connect": {
44
- "type": "object",
45
- "properties": { "MICROPHONE": {
46
- "type": "string",
47
- "enum": ["on", "off"]
48
- } },
49
- "additionalProperties": false
50
- },
51
- "defineFunction": {
52
- "type": "object",
53
- "properties": { "EXPORT": {
54
- "type": "string",
55
- "enum": ["tool", "none"]
56
- } },
57
- "additionalProperties": false
58
- },
59
- "usageValue": {
60
- "type": "object",
61
- "properties": { "FIELD": {
62
- "type": "string",
63
- "enum": [
64
- "costUSD",
65
- "responses",
66
- "inputTokens",
67
- "outputTokens",
68
- "cachedInputTokens",
69
- "textInputTokens",
70
- "audioInputTokens",
71
- "textOutputTokens",
72
- "audioOutputTokens"
73
- ]
74
- } },
75
- "additionalProperties": false
76
- }
77
- }
78
- };
79
- //#endregion
80
- //#region src/fixed-input-editors.ts
81
- var contracts = block_editor_fixed_inputs_schema_default.$defs;
82
- var repository = "turbowarp-openai-realtime-api";
83
- var extensionId = "kubohiroyaopenairealtime";
84
- var token = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/u;
85
- function readNumber(value) {
86
- if (typeof value !== "string" && typeof value !== "number") throw new TypeError("Invalid numeric projection.");
87
- if (typeof value === "string" && value.trim() === "") throw new TypeError("Existing blank field is readonly.");
88
- const number = Number(value);
89
- if (!Number.isFinite(number)) throw new TypeError("Existing nonfinite field is readonly.");
90
- return number;
91
- }
92
- function newNumberToken(value) {
93
- if (typeof value === "number") {
94
- if (!Number.isFinite(value)) throw new TypeError("Invalid new number.");
95
- return Object.is(value, -0) ? "-0" : String(value);
96
- }
97
- if (typeof value !== "string" || !token.test(value.trim()) || !Number.isFinite(Number(value))) throw new TypeError("Invalid new number.");
98
- return value.trim();
99
- }
100
- function existingChoice(_opcode, value) {
101
- if (typeof value !== "string") throw new TypeError("Invalid string projection.");
102
- return value;
103
- }
104
- function candidateValid(_opcode, _argument, schema, value) {
105
- if (schema.type === "number" || schema.type === "integer") {
106
- let number;
107
- try {
108
- number = readNumber(value);
109
- } catch {
110
- return false;
111
- }
112
- if (schema.type === "integer" && !Number.isInteger(number)) return false;
113
- if (schema.minimum !== void 0 && number < schema.minimum) return false;
114
- if (schema.maximum !== void 0 && number > schema.maximum) return false;
115
- if (schema.exclusiveMinimum !== void 0 && number <= schema.exclusiveMinimum) return false;
116
- if (schema.enum && !schema.enum.includes(number)) return false;
117
- return true;
118
- }
119
- return typeof value === "string" && schema.enum.includes(value);
120
- }
121
- function fixedInputEditorOptions() {
122
- return Object.entries(contracts).map(([opcode, contract]) => ({
123
- apiVersion: 1,
124
- extensionId,
125
- editorId: `${extensionId}.fixed.${opcode}`,
126
- opcodes: [`${extensionId}_${opcode}`],
127
- contractId: `${repository}.${opcode}.fixed`,
128
- contractRevision: "fixed-inputs-v1",
129
- validationContext: () => ({
130
- consumerId: repository,
131
- nowMs: 0,
132
- settings: null
133
- }),
134
- bindings: Object.entries(contract.properties).map(([argument, schema]) => {
135
- const numeric = schema.type === "number" || schema.type === "integer";
136
- const source = {
137
- repository,
138
- path: "schemas/block-editor-fixed-inputs.schema.json",
139
- symbol: `/$defs/${opcode}/properties/${argument}`
140
- };
141
- return {
142
- argument,
143
- schemaBundle: {
144
- identity: {
145
- ownerId: repository,
146
- contractId: `${opcode}.${argument}`,
147
- revision: "fixed-inputs-v1",
148
- variantId: "fixed"
149
- },
150
- dialect: "draft-07",
151
- root: {
152
- retrievalUri: `${block_editor_fixed_inputs_schema_default.$id}?binding=${opcode}.${argument}`,
153
- document: schema,
154
- artifact: {
155
- role: "canonical-schema",
156
- source
157
- }
158
- },
159
- resources: [],
160
- provenance: {
161
- canonicalSources: [
162
- source,
163
- {
164
- repository,
165
- path: "src/block-definitions.json"
166
- },
167
- {
168
- repository,
169
- path: "src/extension.ts",
170
- symbol: opcode
171
- }
172
- ],
173
- generatedFiles: [],
174
- referenceSchemas: [],
175
- generationSources: []
176
- }
177
- },
178
- control: numeric ? { kind: "number" } : {
179
- kind: "enum",
180
- choices: schema.enum.map((value) => ({
181
- value,
182
- label: String(value)
183
- }))
184
- },
185
- read: (value) => numeric ? readNumber(value) : existingChoice(opcode, value),
186
- encode: (value) => {
187
- if (numeric) return newNumberToken(value);
188
- if (typeof value !== "string" || !schema.enum.includes(value)) throw new TypeError("Invalid new enum value.");
189
- return value;
190
- },
191
- validateCandidate: (value) => candidateValid(opcode, argument, schema, value) ? [] : [{
192
- path: argument,
193
- message: "Check this value before saving."
194
- }]
195
- };
196
- })
197
- }));
198
- }
199
- /** Register only visible static contracts; ordinary composition consumers load no editor runtime. */
200
- function registerFixedInputEditors(scratch, runtime, visibleBlocks) {
201
- const api = scratch.gui?.blockEditors;
202
- if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function") return void 0;
203
- const visibleOpcodes = new Set(visibleBlocks.flatMap((block) => {
204
- if (typeof block !== "object" || block === null || !("opcode" in block)) return [];
205
- return typeof block.opcode === "string" ? [block.opcode] : [];
206
- }));
207
- const events = runtime;
208
- const removers = [];
209
- let active = true;
210
- const dispose = () => {
211
- if (!active) return;
212
- active = false;
213
- events?.off?.("RUNTIME_DISPOSED", dispose);
214
- for (const remove of removers.splice(0).reverse()) remove();
215
- };
216
- try {
217
- for (const options of fixedInputEditorOptions()) if (options.opcodes.some((opcode) => visibleOpcodes.has(opcode.slice(25)))) removers.push(api.registerFixedInputEditor.call(api, options));
218
- events?.on?.("RUNTIME_DISPOSED", dispose);
219
- } catch (error) {
220
- dispose();
221
- throw error;
222
- }
223
- return dispose;
224
- }
225
- var block_definitions_default = {
226
- extensionName: "OpenAI Realtime",
227
- blocks: [
228
- {
229
- "opcode": "configureRelay",
230
- "blockType": "COMMAND",
231
- "text": "configure local relay [ENDPOINT]",
232
- "description": "Sets the loopback origin of the local capability-proxy relay. Clears any previous pairing.",
233
- "descriptionJa": "localhostで動くcapability-proxy中継のループバックoriginを設定します。以前のペアリングは消去されます。",
234
- "arguments": { "ENDPOINT": {
235
- "type": "STRING",
236
- "defaultValue": "http://127.0.0.1:8787"
237
- } }
238
- },
239
- {
240
- "opcode": "pairRelay",
241
- "blockType": "COMMAND",
242
- "text": "pair local relay with one-time code [CODE]",
243
- "description": "Exchanges the eight-digit code printed by the relay for a session token kept only in memory.",
244
- "descriptionJa": "中継が表示した8桁のコードを、メモリだけに保持するセッションtokenと交換します。",
245
- "arguments": { "CODE": {
246
- "type": "STRING",
247
- "defaultValue": "00000000"
248
- } }
249
- },
250
- {
251
- "opcode": "isRelayPaired",
252
- "blockType": "BOOLEAN",
253
- "text": "local relay paired?",
254
- "description": "Reports whether an unexpired relay session token is held in memory.",
255
- "descriptionJa": "有効期限内の中継セッションtokenをメモリに保持しているかを返します。",
256
- "arguments": {}
257
- },
258
- {
259
- "opcode": "setInstructions",
260
- "blockType": "COMMAND",
261
- "text": "set instructions to [TEXT]",
262
- "description": "Sets the system instructions used by the next connection.",
263
- "descriptionJa": "次の接続で使うシステムへの指示(instructions)を設定します。",
264
- "arguments": { "TEXT": {
265
- "type": "STRING",
266
- "defaultValue": "You are a friendly assistant. Answer briefly."
267
- } }
268
- },
269
- {
270
- "opcode": "setVoice",
271
- "blockType": "COMMAND",
272
- "text": "set voice to [VOICE]",
273
- "description": "Sets the output voice used by the next connection.",
274
- "descriptionJa": "次の接続で使う応答の声を設定します。",
275
- "arguments": { "VOICE": {
276
- "type": "STRING",
277
- "menu": "voices",
278
- "defaultValue": "marin"
279
- } }
280
- },
281
- {
282
- "opcode": "setOutputMode",
283
- "blockType": "COMMAND",
284
- "text": "set output to [MODE]",
285
- "description": "Chooses spoken audio or text-only responses for the next connection.",
286
- "descriptionJa": "次の接続で、音声で応答するかテキストだけで応答するかを選びます。",
287
- "arguments": { "MODE": {
288
- "type": "STRING",
289
- "menu": "outputModes",
290
- "defaultValue": "audio"
291
- } }
292
- },
293
- {
294
- "opcode": "setModel",
295
- "blockType": "COMMAND",
296
- "text": "set model to [MODEL]",
297
- "description": "Chooses the Realtime model for the next connection. The relay only accepts models its operator allows; empty uses the relay default.",
298
- "descriptionJa": "次の接続で使うRealtimeのモデルを選びます。中継の運用者が許可したモデルだけが使えます。空にすると中継の既定のモデルを使います。",
299
- "arguments": { "MODEL": {
300
- "type": "STRING",
301
- "menu": "models",
302
- "defaultValue": "gpt-realtime-2.1-mini"
303
- } }
304
- },
305
- {
306
- "opcode": "setSessionTimeLimit",
307
- "blockType": "COMMAND",
308
- "text": "set session time limit to [SECONDS] seconds",
309
- "description": "Disconnects automatically after this many seconds from the next connection on. 0 disables the limit.",
310
- "descriptionJa": "次の接続から、指定した秒数が経つと自動で切断します。0にすると制限しません。",
311
- "arguments": { "SECONDS": {
312
- "type": "NUMBER",
313
- "defaultValue": "600"
314
- } }
315
- },
316
- {
317
- "opcode": "connect",
318
- "blockType": "COMMAND",
319
- "text": "connect to Realtime with microphone [MICROPHONE]",
320
- "description": "Mints an ephemeral key through the relay and opens a WebRTC session. Exported functions become tools.",
321
- "descriptionJa": "中継を通じて一時キーを発行し、WebRTCのセッションを開きます。ツールとして公開した関数はモデルから呼べるようになります。",
322
- "arguments": { "MICROPHONE": {
323
- "type": "STRING",
324
- "menu": "onOff",
325
- "defaultValue": "on"
326
- } }
327
- },
328
- {
329
- "opcode": "disconnect",
330
- "blockType": "COMMAND",
331
- "text": "disconnect from Realtime",
332
- "description": "Closes the WebRTC session, stops the microphone, and fails pending function calls.",
333
- "descriptionJa": "WebRTCのセッションを閉じてマイクを止め、実行中の関数呼び出しを失敗させます。",
334
- "arguments": {}
335
- },
336
- {
337
- "opcode": "isConnected",
338
- "blockType": "BOOLEAN",
339
- "text": "connected to Realtime?",
340
- "description": "Reports whether the Realtime session is open.",
341
- "descriptionJa": "Realtimeのセッションが開いているかを返します。",
342
- "arguments": {}
343
- },
344
- {
345
- "opcode": "connectionState",
346
- "blockType": "REPORTER",
347
- "text": "Realtime connection state",
348
- "description": "Reports disconnected, connecting, connected, or failed.",
349
- "descriptionJa": "disconnected、connecting、connected、failedのいずれかを返します。",
350
- "arguments": {}
351
- },
352
- {
353
- "opcode": "currentModel",
354
- "blockType": "REPORTER",
355
- "text": "Realtime model",
356
- "description": "Reports the model the relay used for the current or last connection.",
357
- "descriptionJa": "現在または直前の接続で中継が使ったモデルを返します。",
358
- "arguments": {}
359
- },
360
- {
361
- "opcode": "sessionElapsed",
362
- "blockType": "REPORTER",
363
- "text": "session elapsed seconds",
364
- "description": "Reports seconds since the current session connected, or 0 when disconnected.",
365
- "descriptionJa": "現在のセッションが接続してからの秒数を返します。切断中は0を返します。",
366
- "arguments": {}
367
- },
368
- {
369
- "opcode": "whenSessionTimeLimitReached",
370
- "blockType": "HAT",
371
- "text": "when session time limit is reached",
372
- "description": "Starts after the session time limit disconnects the session.",
373
- "descriptionJa": "セッションの時間上限によって切断されたときに起動します。",
374
- "arguments": {}
375
- },
376
- {
377
- "opcode": "sendText",
378
- "blockType": "COMMAND",
379
- "text": "send text [TEXT]",
380
- "description": "Adds a user text message to the conversation and asks for a response.",
381
- "descriptionJa": "利用者のテキストメッセージを会話に追加し、応答を要求します。",
382
- "arguments": { "TEXT": {
383
- "type": "STRING",
384
- "defaultValue": "Hello!"
385
- } }
386
- },
387
- {
388
- "opcode": "whenResponseDone",
389
- "blockType": "HAT",
390
- "text": "when assistant finishes responding",
391
- "description": "Starts when a response that contains assistant text or transcript completes.",
392
- "descriptionJa": "アシスタントのテキストまたは音声の書き起こしを含む応答が完了したときに起動します。",
393
- "arguments": {}
394
- },
395
- {
396
- "opcode": "lastResponseText",
397
- "blockType": "REPORTER",
398
- "text": "last assistant response",
399
- "description": "Reports the text or audio transcript of the most recent completed assistant response.",
400
- "descriptionJa": "直近に完了したアシスタントの応答のテキスト、または音声の書き起こしを返します。",
401
- "arguments": {}
402
- },
403
- {
404
- "opcode": "defineFunction",
405
- "blockType": "HAT",
406
- "text": "define function [NAME] description [DESCRIPTION] args schema [SCHEMA] export as [EXPORT]",
407
- "description": "Defines a function value. With export as tool, the model can call it; NAME, DESCRIPTION, and SCHEMA must be literal text.",
408
- "descriptionJa": "関数を定義します。export asをtoolにすると、モデルから呼べるツールになります。NAME、DESCRIPTION、SCHEMAには文字列を直接書く必要があります。",
409
- "arguments": {
410
- "NAME": {
411
- "type": "STRING",
412
- "defaultValue": "get_score"
413
- },
414
- "DESCRIPTION": {
415
- "type": "STRING",
416
- "defaultValue": "Returns the player's current score."
417
- },
418
- "SCHEMA": {
419
- "type": "STRING",
420
- "defaultValue": "{\"type\":\"object\",\"properties\":{}}"
421
- },
422
- "EXPORT": {
423
- "type": "STRING",
424
- "menu": "exportModes",
425
- "defaultValue": "tool"
426
- }
427
- }
428
- },
429
- {
430
- "opcode": "functionArgument",
431
- "blockType": "REPORTER",
432
- "text": "function argument [PATH]",
433
- "description": "Inside a function, reports the argument at a dotted path such as city or items.0.name.",
434
- "descriptionJa": "関数の中で、cityやitems.0.nameのようなドット区切りのパスにある引数を返します。",
435
- "arguments": { "PATH": {
436
- "type": "STRING",
437
- "defaultValue": "city"
438
- } }
439
- },
440
- {
441
- "opcode": "functionArgumentsJson",
442
- "blockType": "REPORTER",
443
- "text": "function arguments JSON",
444
- "description": "Inside a function, reports all arguments as JSON text.",
445
- "descriptionJa": "関数の中で、すべての引数をJSONテキストとして返します。",
446
- "arguments": {}
447
- },
448
- {
449
- "opcode": "returnValue",
450
- "blockType": "COMMAND",
451
- "text": "return [VALUE]",
452
- "description": "Inside a function, returns a value and ends the script. JSON text is returned as JSON; other text is returned as a string.",
453
- "descriptionJa": "関数の中で値を返し、スクリプトを終了します。JSONテキストはJSONとして、それ以外のテキストは文字列として返します。",
454
- "arguments": { "VALUE": {
455
- "type": "STRING",
456
- "defaultValue": "{\"score\":10}"
457
- } }
458
- },
459
- {
460
- "opcode": "usageValue",
461
- "blockType": "REPORTER",
462
- "text": "Realtime usage [FIELD]",
463
- "description": "Reports token usage totals since the last reset. costUSD is an estimate from published prices; the OpenAI dashboard is authoritative.",
464
- "descriptionJa": "最後にリセットしてからの使用量の合計を返します。costUSDは公開価格からの概算で、正確な請求額はOpenAIの管理画面で確認してください。",
465
- "arguments": { "FIELD": {
466
- "type": "STRING",
467
- "menu": "usageFields",
468
- "defaultValue": "costUSD"
469
- } }
470
- },
471
- {
472
- "opcode": "resetUsage",
473
- "blockType": "COMMAND",
474
- "text": "reset Realtime usage",
475
- "description": "Clears the usage totals.",
476
- "descriptionJa": "使用量の合計を0に戻します。",
477
- "arguments": {}
478
- },
479
- {
480
- "opcode": "lastError",
481
- "blockType": "REPORTER",
482
- "text": "last Realtime error",
483
- "description": "Reports the most recent relay, connection, or API error, or an empty string.",
484
- "descriptionJa": "直近の中継、接続、APIのエラーを返します。エラーがなければ空文字列を返します。",
485
- "arguments": {}
486
- }
487
- ],
488
- menus: {
489
- "voices": {
490
- "acceptReporters": true,
491
- "items": [
492
- "alloy",
493
- "ash",
494
- "ballad",
495
- "cedar",
496
- "coral",
497
- "echo",
498
- "marin",
499
- "sage",
500
- "shimmer",
501
- "verse"
502
- ]
503
- },
504
- "outputModes": {
505
- "acceptReporters": false,
506
- "items": ["audio", "text"]
507
- },
508
- "onOff": {
509
- "acceptReporters": false,
510
- "items": ["on", "off"]
511
- },
512
- "exportModes": {
513
- "acceptReporters": false,
514
- "items": ["tool", "none"]
515
- },
516
- "models": {
517
- "acceptReporters": true,
518
- "items": ["gpt-realtime-2.1-mini", "gpt-realtime-2.1"]
519
- },
520
- "usageFields": {
521
- "acceptReporters": false,
522
- "items": [
523
- "costUSD",
524
- "responses",
525
- "inputTokens",
526
- "outputTokens",
527
- "cachedInputTokens",
528
- "textInputTokens",
529
- "audioInputTokens",
530
- "textOutputTokens",
531
- "audioOutputTokens"
532
- ]
533
- }
534
- }
535
- };
536
- //#endregion
537
- //#region node_modules/.pnpm/@kubohiroya+turbowarp-named-functions@0.1.1_react@18.3.1/node_modules/@kubohiroya/turbowarp-named-functions/dist/composition.js
538
- /**
539
- * A small JSON Schema validator for function arguments and returns. It interprets the schema at run time and
540
- * never generates code, so it also works where `eval` is unavailable (for example Cloudflare Workers).
541
- *
542
- * Supported keywords: type, properties, required, additionalProperties (boolean), items, enum,
543
- * const, minimum, maximum, minLength, maxLength, minItems, maxItems. Other keywords, such as
544
- * description and title, are ignored.
545
- */
546
- function validateAgainstSchema(value, schema, path = "$") {
547
- const rule = asRecord$2(schema);
548
- if (!rule) throw new Error(`${path} must be a JSON Schema object`);
549
- assertSchemaNode(rule, path);
550
- return validateValue(value, rule, path);
551
- }
552
- /** Reject malformed schemas before a function definition or call can use them. */
553
- function validateSchemaDefinition(schema, label = "schema") {
554
- const rule = asRecord$2(schema);
555
- if (!rule) throw new Error(`${label} must be a JSON object`);
556
- assertSchemaNode(rule, label);
557
- }
558
- function validateValue(value, rule, path) {
559
- const errors = [];
560
- if (rule.type !== void 0) {
561
- const types = Array.isArray(rule.type) ? rule.type : [rule.type];
562
- if (!types.some((type) => matchesType(value, type))) return [`${path} must be ${types.join(" or ")}`];
563
- }
564
- if (Array.isArray(rule.enum) && !rule.enum.some((candidate) => deepEqual(candidate, value))) errors.push(`${path} must be one of ${rule.enum.map((item) => JSON.stringify(item)).join(", ")}`);
565
- if ("const" in rule && !deepEqual(rule.const, value)) errors.push(`${path} must be ${JSON.stringify(rule.const)}`);
566
- if (typeof value === "number") {
567
- if (typeof rule.minimum === "number" && value < rule.minimum) errors.push(`${path} must be >= ${rule.minimum}`);
568
- if (typeof rule.maximum === "number" && value > rule.maximum) errors.push(`${path} must be <= ${rule.maximum}`);
569
- }
570
- if (typeof value === "string") {
571
- const length = [...value].length;
572
- if (typeof rule.minLength === "number" && length < rule.minLength) errors.push(`${path} must have at least ${rule.minLength} characters`);
573
- if (typeof rule.maxLength === "number" && length > rule.maxLength) errors.push(`${path} must have at most ${rule.maxLength} characters`);
574
- }
575
- if (Array.isArray(value)) {
576
- if (typeof rule.minItems === "number" && value.length < rule.minItems) errors.push(`${path} must have at least ${rule.minItems} items`);
577
- if (typeof rule.maxItems === "number" && value.length > rule.maxItems) errors.push(`${path} must have at most ${rule.maxItems} items`);
578
- if (rule.items !== void 0) value.forEach((item, index) => errors.push(...validateValue(item, rule.items, `${path}[${index}]`)));
579
- }
580
- const object = asRecord$2(value);
581
- if (object) {
582
- const properties = asRecord$2(rule.properties) ?? {};
583
- if (Array.isArray(rule.required)) {
584
- for (const name of rule.required) if (typeof name === "string" && !Object.hasOwn(object, name)) errors.push(`${path}.${name} is required`);
585
- }
586
- for (const [name, child] of Object.entries(object)) if (Object.hasOwn(properties, name)) errors.push(...validateValue(child, properties[name], `${path}.${name}`));
587
- else if (rule.additionalProperties === false) errors.push(`${path}.${name} is not allowed`);
588
- }
589
- return errors;
590
- }
591
- function assertSchemaNode(rule, path) {
592
- const validTypes = /* @__PURE__ */ new Set([
593
- "object",
594
- "array",
595
- "string",
596
- "number",
597
- "integer",
598
- "boolean",
599
- "null"
600
- ]);
601
- if (rule.type !== void 0) {
602
- const types = Array.isArray(rule.type) ? rule.type : [rule.type];
603
- if (types.length === 0 || types.some((type) => typeof type !== "string" || !validTypes.has(type))) throw new Error(`${path}.type must be a supported JSON Schema type or non-empty type array`);
604
- }
605
- if (rule.properties !== void 0) {
606
- const properties = asRecord$2(rule.properties);
607
- if (!properties) throw new Error(`${path}.properties must be an object`);
608
- for (const [name, child] of Object.entries(properties)) {
609
- const childRule = asRecord$2(child);
610
- if (!childRule) throw new Error(`${path}.properties.${name} must be a JSON Schema object`);
611
- assertSchemaNode(childRule, `${path}.properties.${name}`);
612
- }
613
- }
614
- if (rule.required !== void 0 && (!Array.isArray(rule.required) || rule.required.some((name) => typeof name !== "string"))) throw new Error(`${path}.required must be an array of strings`);
615
- if (rule.additionalProperties !== void 0 && typeof rule.additionalProperties !== "boolean") throw new Error(`${path}.additionalProperties must be a boolean`);
616
- if (rule.items !== void 0) {
617
- const items = asRecord$2(rule.items);
618
- if (!items) throw new Error(`${path}.items must be a JSON Schema object`);
619
- assertSchemaNode(items, `${path}.items`);
620
- }
621
- if (rule.enum !== void 0 && (!Array.isArray(rule.enum) || rule.enum.length === 0)) throw new Error(`${path}.enum must be a non-empty array`);
622
- for (const keyword of ["minimum", "maximum"]) if (rule[keyword] !== void 0 && (typeof rule[keyword] !== "number" || !Number.isFinite(rule[keyword]))) throw new Error(`${path}.${keyword} must be a finite number`);
623
- for (const keyword of [
624
- "minLength",
625
- "maxLength",
626
- "minItems",
627
- "maxItems"
628
- ]) {
629
- const value = rule[keyword];
630
- if (value !== void 0 && (typeof value !== "number" || !Number.isInteger(value) || value < 0)) throw new Error(`${path}.${keyword} must be a non-negative integer`);
631
- }
632
- if (typeof rule.minimum === "number" && typeof rule.maximum === "number" && rule.minimum > rule.maximum) throw new Error(`${path}.minimum cannot exceed maximum`);
633
- if (typeof rule.minLength === "number" && typeof rule.maxLength === "number" && rule.minLength > rule.maxLength) throw new Error(`${path}.minLength cannot exceed maxLength`);
634
- if (typeof rule.minItems === "number" && typeof rule.maxItems === "number" && rule.minItems > rule.maxItems) throw new Error(`${path}.minItems cannot exceed maxItems`);
635
- }
636
- function matchesType(value, type) {
637
- switch (type) {
638
- case "object": return asRecord$2(value) !== null;
639
- case "array": return Array.isArray(value);
640
- case "string": return typeof value === "string";
641
- case "number": return typeof value === "number" && Number.isFinite(value);
642
- case "integer": return typeof value === "number" && Number.isInteger(value);
643
- case "boolean": return typeof value === "boolean";
644
- case "null": return value === null;
645
- default: return false;
646
- }
647
- }
648
- function deepEqual(left, right) {
649
- return JSON.stringify(left) === JSON.stringify(right);
650
- }
651
- function asRecord$2(value) {
652
- return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
653
- }
654
- var FunctionDispatcher = class {
655
- constructor(runtime, options) {
656
- this.runtime = runtime;
657
- this.options = options;
658
- this.queue = [];
659
- this.running = /* @__PURE__ */ new Map();
660
- this.byThread = /* @__PURE__ */ new Map();
661
- this.starting = null;
662
- this.step = 0;
663
- this.onAfterExecute = () => this.afterStep();
664
- this.onStopAll = () => this.cancelAll("The project was stopped.");
665
- this.timeoutMs = options.timeoutMs ?? 3e4;
666
- this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
667
- this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
668
- runtime.on("AFTER_EXECUTE", this.onAfterExecute);
669
- runtime.on("PROJECT_STOP_ALL", this.onStopAll);
670
- }
671
- /**
672
- * Starts a named function. `caller` is the thread of the block making the call, if any; it is used
673
- * to detect reentrant calls.
674
- */
675
- invoke(name, args, caller, returnSchema) {
676
- if (!this.options.knownNames().has(name)) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${name}`));
677
- const parentChain = caller ? this.byThread.get(caller)?.chain ?? [] : [];
678
- if (parentChain.includes(name)) return Promise.reject(/* @__PURE__ */ new Error(`Reentrant call: ${[...parentChain, name].join(" -> ")}. Use a custom block for recursion.`));
679
- return new Promise((resolve, reject) => {
680
- const invocation = {
681
- name,
682
- args,
683
- ...returnSchema === void 0 ? {} : { returnSchema },
684
- chain: [...parentChain, name],
685
- resolve,
686
- reject,
687
- thread: null,
688
- startedAtStep: -1,
689
- timer: null,
690
- settled: false
691
- };
692
- invocation.timer = this.setTimer(() => this.settle(invocation, /* @__PURE__ */ new Error(`Function ${name} timed out.`)), this.timeoutMs);
693
- this.queue.push(invocation);
694
- this.pump();
695
- });
696
- }
697
- /** Hat predicate: true only for the script that should run the invocation being started. */
698
- matchHat(name, thread) {
699
- const invocation = this.starting;
700
- if (!invocation || !thread || invocation.name !== name.trim()) return false;
701
- invocation.thread = thread;
702
- this.byThread.set(thread, invocation);
703
- this.starting = null;
704
- return true;
705
- }
706
- argumentsFor(thread) {
707
- const invocation = thread ? this.byThread.get(thread) : void 0;
708
- if (!invocation) throw new Error("This block can only be used inside a running function.");
709
- return invocation.args;
710
- }
711
- returnFrom(thread, value) {
712
- const invocation = thread ? this.byThread.get(thread) : void 0;
713
- if (!invocation) throw new Error("return can only be used inside a running function.");
714
- this.settleWithValue(invocation, value);
715
- }
716
- cancelAll(reason) {
717
- for (const invocation of [...this.queue, ...this.running.values()]) this.settle(invocation, new Error(reason));
718
- if (this.starting) this.settle(this.starting, new Error(reason));
719
- }
720
- get pendingCount() {
721
- return this.queue.length + this.running.size;
722
- }
723
- release() {
724
- this.cancelAll("Named functions were released.");
725
- this.runtime.off?.("AFTER_EXECUTE", this.onAfterExecute);
726
- this.runtime.off?.("PROJECT_STOP_ALL", this.onStopAll);
727
- }
728
- pump() {
729
- if (this.starting) return;
730
- const index = this.queue.findIndex((invocation) => !this.running.has(invocation.name));
731
- if (index < 0) return;
732
- const [invocation] = this.queue.splice(index, 1);
733
- if (!invocation) return;
734
- this.running.set(invocation.name, invocation);
735
- this.starting = invocation;
736
- invocation.startedAtStep = this.step;
737
- this.runtime.startHats(this.options.hatOpcode);
738
- }
739
- afterStep() {
740
- this.step += 1;
741
- const starting = this.starting;
742
- if (starting && this.step - starting.startedAtStep > 2) this.settle(starting, /* @__PURE__ */ new Error(`Function ${starting.name} did not start. Is its script already running?`));
743
- for (const invocation of this.running.values()) if (invocation.thread && !this.runtime.threads.includes(invocation.thread)) this.settleWithValue(invocation, null);
744
- this.pump();
745
- }
746
- settle(invocation, error, value) {
747
- if (invocation.settled) return;
748
- invocation.settled = true;
749
- this.clearTimer(invocation.timer);
750
- const queued = this.queue.indexOf(invocation);
751
- if (queued >= 0) this.queue.splice(queued, 1);
752
- if (this.running.get(invocation.name) === invocation) this.running.delete(invocation.name);
753
- if (invocation.thread) this.byThread.delete(invocation.thread);
754
- if (this.starting === invocation) this.starting = null;
755
- if (error) invocation.reject(error);
756
- else invocation.resolve(value);
757
- }
758
- settleWithValue(invocation, value) {
759
- if (invocation.returnSchema !== void 0) {
760
- const errors = validateAgainstSchema(value, invocation.returnSchema);
761
- if (errors.length > 0) {
762
- this.settle(invocation, /* @__PURE__ */ new Error(`Invalid return value for ${invocation.name}: ${errors.join("; ")}`));
763
- return;
764
- }
765
- }
766
- this.settle(invocation, null, value);
767
- }
768
- };
769
- /** Resolves a dotted path such as `items.0.name` inside parsed JSON arguments. */
770
- function readArgumentPath(args, path) {
771
- const trimmed = path.trim();
772
- if (trimmed.length === 0) return args;
773
- let current = args;
774
- for (const segment of trimmed.split(".")) {
775
- if (current === null || typeof current !== "object") return void 0;
776
- current = current[segment];
777
- }
778
- return current;
779
- }
780
- /** Converts a value for a Scratch reporter: objects and arrays become JSON text. */
781
- function toScratchValue(value) {
782
- if (value === void 0 || value === null) return "";
783
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
784
- return JSON.stringify(value);
785
- }
786
- /** Parses block text: JSON text becomes JSON, anything else stays a string. */
787
- function parseJsonOrText(text) {
788
- const trimmed = text.trim();
789
- if (trimmed.length === 0) return "";
790
- try {
791
- return JSON.parse(trimmed);
792
- } catch {
793
- return text;
794
- }
795
- }
796
- var FUNCTION_NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/u;
797
- var MAX_DESCRIPTION_LENGTH = 1024;
798
- /**
799
- * Reads every `define function` hat in the project. NAME, DESCRIPTION, and SCHEMA are part of the
800
- * tool contract sent to the model before any script runs, so they must be literal text.
801
- */
802
- function scanFunctionDefinitions(targets, hatOpcode) {
803
- const functions = [];
804
- const errors = [];
805
- const seen = /* @__PURE__ */ new Map();
806
- for (const target of targets) {
807
- if (target.isOriginal === false) continue;
808
- const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
809
- const blocks = target.blocks._blocks;
810
- for (const block of Object.values(blocks)) {
811
- if (block.opcode !== hatOpcode || block.topLevel === false) continue;
812
- const where = `${targetName} (block ${block.id})`;
813
- try {
814
- const definition = readDefinition(block, blocks, targetName);
815
- const previous = seen.get(definition.name);
816
- if (previous !== void 0) throw new Error(`function "${definition.name}" is already defined in ${previous}`);
817
- seen.set(definition.name, where);
818
- functions.push(definition);
819
- } catch (error) {
820
- errors.push(`${where}: ${error instanceof Error ? error.message : String(error)}`);
821
- }
822
- }
823
- }
824
- functions.sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
825
- return {
826
- functions,
827
- errors
828
- };
829
- }
830
- function toFunctionTools(functions) {
831
- return functions.filter((definition) => definition.exportAs === "tool").map((definition) => ({
832
- type: "function",
833
- name: definition.name,
834
- description: definition.description,
835
- parameters: definition.parameters
836
- }));
837
- }
838
- function readDefinition(block, blocks, targetName) {
839
- const name = readLiteralInput(block, blocks, "NAME").trim();
840
- if (!FUNCTION_NAME_PATTERN.test(name)) throw new Error("function name must be 1-64 letters, digits, \"_\" or \"-\"");
841
- const description = readLiteralInput(block, blocks, "DESCRIPTION").trim();
842
- if (description.length > 1024) throw new Error(`description must be at most ${MAX_DESCRIPTION_LENGTH} characters`);
843
- const parameters = parseSchema(readLiteralInput(block, blocks, "SCHEMA"), "args schema", true);
844
- const returnsText = readOptionalLiteralInput(block, blocks, "RETURNS");
845
- const returns = returnsText === void 0 || returnsText.trim() === "" ? void 0 : parseSchema(returnsText, "returns schema", false);
846
- const exportAs = String(block.fields?.EXPORT?.value ?? "none") === "tool" ? "tool" : "none";
847
- if (exportAs === "tool" && description.length === 0) throw new Error("a function exported as a tool needs a description");
848
- return {
849
- name,
850
- description,
851
- parameters,
852
- ...returns ? { returns } : {},
853
- exportAs,
854
- targetName,
855
- blockId: block.id
856
- };
857
- }
858
- function readOptionalLiteralInput(block, blocks, inputName) {
859
- if (!block.inputs?.[inputName]) return void 0;
860
- return readLiteralInput(block, blocks, inputName);
861
- }
862
- function readLiteralInput(block, blocks, inputName) {
863
- const input = block.inputs?.[inputName];
864
- if (!input) throw new Error(`${inputName} is missing`);
865
- if (input.block && input.block !== input.shadow) throw new Error(`${inputName} must be literal text, not a reporter block`);
866
- const shadowId = input.shadow ?? input.block;
867
- const shadow = shadowId ? blocks[shadowId] : void 0;
868
- const field = shadow?.fields ? Object.values(shadow.fields)[0] : void 0;
869
- if (!field) throw new Error(`${inputName} is missing`);
870
- return String(field.value ?? "");
871
- }
872
- function parseSchema(text, label, requireObjectType) {
873
- let value;
874
- try {
875
- value = JSON.parse(text);
876
- } catch {
877
- throw new Error(`${label} must be valid JSON`);
878
- }
879
- if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${label} must be a JSON object`);
880
- const schema = value;
881
- if (requireObjectType && schema.type !== "object") throw new Error("args schema must have \"type\": \"object\"");
882
- validateSchemaDefinition(schema, label);
883
- return schema;
884
- }
885
- /**
886
- * Composition API: named functions without TurboWarp block definitions.
887
- *
888
- * A consumer extension owns its `define function` hat (and its opcode) and forwards the hat
889
- * predicate and the argument/return blocks here. Importing this module does not register a
890
- * TurboWarp extension and does not touch the `Scratch` global.
891
- */
892
- function createNamedFunctions(options) {
893
- return new NamedFunctionsImpl(options);
894
- }
895
- var NamedFunctionsImpl = class {
896
- constructor(options) {
897
- this.options = options;
898
- this.promises = /* @__PURE__ */ new Map();
899
- this.waiting = [];
900
- this.inFlight = 0;
901
- this.nextPromiseId = 1;
902
- this.maxInFlight = Math.max(1, options.maxInFlight ?? 8);
903
- this.maxRetained = Math.max(1, options.maxRetainedPromises ?? 256);
904
- const dispatcherOptions = {
905
- hatOpcode: options.functionHatOpcode,
906
- knownNames: () => new Set(this.scan().functions.map((definition) => definition.name)),
907
- ...options.timeoutMs === void 0 ? {} : { timeoutMs: options.timeoutMs },
908
- ...options.setTimer ? { setTimer: options.setTimer } : {},
909
- ...options.clearTimer ? { clearTimer: options.clearTimer } : {}
910
- };
911
- this.dispatcher = new FunctionDispatcher(options.runtime, dispatcherOptions);
912
- options.runtime.on("PROJECT_STOP_ALL", () => this.promises.clear());
913
- }
914
- scan() {
915
- return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode);
916
- }
917
- tools() {
918
- const scan = this.scan();
919
- if (scan.errors.length > 0) throw new Error(`Invalid function definitions: ${scan.errors.join("; ")}`);
920
- return toFunctionTools(scan.functions);
921
- }
922
- isDefined(name) {
923
- return this.scan().functions.some((definition) => definition.name === name.trim());
924
- }
925
- call(name, args, options = {}) {
926
- const trimmed = name.trim();
927
- const definition = this.scan().functions.find((candidate) => candidate.name === trimmed);
928
- if (!definition) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${trimmed}`));
929
- if (options.exportedOnly && definition.exportAs !== "tool") return Promise.reject(/* @__PURE__ */ new Error(`Function ${trimmed} is not exported as a tool.`));
930
- const errors = validateAgainstSchema(args, definition.parameters);
931
- if (errors.length > 0) return Promise.reject(/* @__PURE__ */ new Error(`Invalid arguments for ${trimmed}: ${errors.join("; ")}`));
932
- return this.dispatcher.invoke(trimmed, args, options.caller, definition.returns);
933
- }
934
- start(name, args, options = {}) {
935
- const id = `p_${this.nextPromiseId++}`;
936
- const promise = this.acquireSlot().then(() => this.call(name, args, options).finally(() => this.releaseSlot()));
937
- const entry = {
938
- promise,
939
- settled: false
940
- };
941
- promise.then(() => entry.settled = true, () => entry.settled = true);
942
- this.promises.set(id, entry);
943
- this.evictSettled();
944
- return { $promise: id };
945
- }
946
- await(ref) {
947
- const entry = this.lookup(ref);
948
- return entry ? entry.promise : Promise.reject(/* @__PURE__ */ new Error("Unknown or expired promise reference."));
949
- }
950
- async awaitAll(refs) {
951
- if (!Array.isArray(refs)) throw new Error("await all needs a JSON array of promise references.");
952
- return (await Promise.allSettled(refs.map((ref) => this.await(ref)))).map((result) => result.status === "fulfilled" ? result.value : { $error: messageOf$1(result.reason) });
953
- }
954
- isSettled(ref) {
955
- return this.lookup(ref)?.settled ?? false;
956
- }
957
- matchHat(name, thread) {
958
- return this.dispatcher.matchHat(name, thread);
959
- }
960
- argumentsFor(thread) {
961
- return this.dispatcher.argumentsFor(thread);
962
- }
963
- returnFrom(thread, value) {
964
- this.dispatcher.returnFrom(thread, value);
965
- }
966
- cancelAll(reason) {
967
- this.dispatcher.cancelAll(reason);
968
- }
969
- release() {
970
- this.dispatcher.release();
971
- this.promises.clear();
972
- }
973
- lookup(ref) {
974
- const id = typeof ref === "object" && ref !== null && typeof ref.$promise === "string" ? ref.$promise : void 0;
975
- return id ? this.promises.get(id) : void 0;
976
- }
977
- acquireSlot() {
978
- if (this.inFlight < this.maxInFlight) {
979
- this.inFlight += 1;
980
- return Promise.resolve();
981
- }
982
- return new Promise((resolve) => this.waiting.push(() => {
983
- this.inFlight += 1;
984
- resolve();
985
- }));
986
- }
987
- releaseSlot() {
988
- this.inFlight -= 1;
989
- this.waiting.shift()?.();
990
- }
991
- evictSettled() {
992
- if (this.promises.size <= this.maxRetained) return;
993
- for (const [id, entry] of this.promises) {
994
- if (this.promises.size <= this.maxRetained) break;
995
- if (entry.settled) this.promises.delete(id);
996
- }
997
- }
998
- };
999
- function messageOf$1(error) {
1000
- return error instanceof Error ? error.message : String(error);
1001
- }
1002
- //#endregion
1003
- //#region src/realtime-session.ts
1004
- /**
1005
- * Conversation logic over a transport: sending user text, collecting assistant output, and
1006
- * answering function calls with `function_call_output` followed by one `response.create`.
1007
- */
1008
- var RealtimeSession = class {
1009
- constructor(hooks) {
1010
- this.hooks = hooks;
1011
- this.stateValue = "disconnected";
1012
- this.transport = null;
1013
- this.generation = 0;
1014
- }
1015
- get state() {
1016
- return this.stateValue;
1017
- }
1018
- async open(transport, clientSecret, microphone) {
1019
- this.close();
1020
- const generation = ++this.generation;
1021
- this.transport = transport;
1022
- this.setState("connecting");
1023
- try {
1024
- await transport.connect({
1025
- clientSecret,
1026
- microphone,
1027
- onEvent: (event) => {
1028
- if (generation === this.generation) this.handleEvent(event);
1029
- },
1030
- onClose: (reason) => {
1031
- if (generation !== this.generation) return;
1032
- this.transport = null;
1033
- this.hooks.onError(reason);
1034
- this.setState("failed");
1035
- }
1036
- });
1037
- } catch (error) {
1038
- if (generation === this.generation) {
1039
- this.transport = null;
1040
- this.setState("failed");
1041
- }
1042
- throw error;
1043
- }
1044
- if (generation === this.generation) this.setState("connected");
1045
- }
1046
- close() {
1047
- this.generation += 1;
1048
- const transport = this.transport;
1049
- this.transport = null;
1050
- transport?.close();
1051
- if (this.stateValue !== "disconnected") this.setState("disconnected");
1052
- }
1053
- sendText(text) {
1054
- this.send({
1055
- type: "conversation.item.create",
1056
- item: {
1057
- type: "message",
1058
- role: "user",
1059
- content: [{
1060
- type: "input_text",
1061
- text
1062
- }]
1063
- }
1064
- });
1065
- this.send({ type: "response.create" });
1066
- }
1067
- send(event) {
1068
- if (!this.transport || this.stateValue !== "connected") throw new Error("Not connected to Realtime.");
1069
- this.transport.send(event);
1070
- }
1071
- async handleEvent(event) {
1072
- if (event.type === "error") {
1073
- this.hooks.onError(describeApiError(event));
1074
- return;
1075
- }
1076
- if (event.type !== "response.done") return;
1077
- const response = event.response;
1078
- if (typeof response === "object" && response !== null && "usage" in response) this.hooks.onUsage?.(response.usage);
1079
- const output = readResponseOutput(event);
1080
- if (output.text.length > 0) this.hooks.onResponseText(output.text);
1081
- if (output.calls.length === 0) return;
1082
- const generation = this.generation;
1083
- const results = await Promise.all(output.calls.map((call) => this.runCall(call)));
1084
- if (generation !== this.generation || !this.transport) return;
1085
- for (const result of results) this.transport.send({
1086
- type: "conversation.item.create",
1087
- item: {
1088
- type: "function_call_output",
1089
- call_id: result.callId,
1090
- output: result.output
1091
- }
1092
- });
1093
- this.transport.send({ type: "response.create" });
1094
- }
1095
- async runCall(call) {
1096
- let args;
1097
- try {
1098
- args = call.argumentsJson.trim().length === 0 ? {} : JSON.parse(call.argumentsJson);
1099
- } catch {
1100
- return {
1101
- callId: call.callId,
1102
- output: JSON.stringify({ error: "Arguments were not valid JSON." })
1103
- };
1104
- }
1105
- try {
1106
- const value = await this.hooks.callFunction(call.name, args);
1107
- return {
1108
- callId: call.callId,
1109
- output: JSON.stringify(value ?? null)
1110
- };
1111
- } catch (error) {
1112
- const message = error instanceof Error ? error.message : String(error);
1113
- this.hooks.onError(message);
1114
- return {
1115
- callId: call.callId,
1116
- output: JSON.stringify({ error: message })
1117
- };
1118
- }
1119
- }
1120
- setState(state) {
1121
- this.stateValue = state;
1122
- this.hooks.onStateChange?.(state);
1123
- }
1124
- };
1125
- function readResponseOutput(event) {
1126
- const response = asRecord$1(event.response);
1127
- const items = Array.isArray(response?.output) ? response.output : [];
1128
- const texts = [];
1129
- const calls = [];
1130
- for (const rawItem of items) {
1131
- const item = asRecord$1(rawItem);
1132
- if (!item) continue;
1133
- if (item.type === "function_call" && typeof item.name === "string" && typeof item.call_id === "string") calls.push({
1134
- callId: item.call_id,
1135
- name: item.name,
1136
- argumentsJson: typeof item.arguments === "string" ? item.arguments : ""
1137
- });
1138
- else if (item.type === "message" && Array.isArray(item.content)) for (const rawPart of item.content) {
1139
- const part = asRecord$1(rawPart);
1140
- if (typeof part?.text === "string") texts.push(part.text);
1141
- else if (typeof part?.transcript === "string") texts.push(part.transcript);
1142
- }
1143
- }
1144
- return {
1145
- text: texts.join(""),
1146
- calls
1147
- };
1148
- }
1149
- function describeApiError(event) {
1150
- const error = asRecord$1(event.error);
1151
- return `${typeof error?.message === "string" ? error.message : "Unknown Realtime API error."}${typeof error?.code === "string" ? ` (${error.code})` : ""}`;
1152
- }
1153
- function asRecord$1(value) {
1154
- return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
1155
- }
1156
- //#endregion
1157
- //#region src/relay-client.ts
1158
- var LOOPBACK_HOSTNAMES = /* @__PURE__ */ new Set([
1159
- "127.0.0.1",
1160
- "localhost",
1161
- "[::1]"
1162
- ]);
1163
- var TOKEN_PATTERN = /^[A-Za-z0-9_-]{20,}$/u;
1164
- var CLIENT_SECRET_PATTERN = /^ek_[A-Za-z0-9_-]+$/u;
1165
- /** Accepts only a plain loopback HTTP origin so the relay token can never leave this machine. */
1166
- function normalizeRelayEndpoint(endpoint) {
1167
- let url;
1168
- try {
1169
- url = new URL(endpoint.trim());
1170
- } catch {
1171
- throw new TypeError("Relay endpoint must be a valid URL.");
1172
- }
1173
- if (url.protocol !== "http:" || !LOOPBACK_HOSTNAMES.has(url.hostname)) throw new TypeError("Relay endpoint must use HTTP on a loopback hostname.");
1174
- if (url.username.length > 0 || url.password.length > 0 || url.search.length > 0 || url.hash.length > 0 || url.pathname !== "/" && url.pathname !== "") throw new TypeError("Relay endpoint must contain only its loopback origin.");
1175
- return url.origin;
1176
- }
1177
- async function pairWithRelay(endpoint, code, fetcher = fetch, now = Date.now) {
1178
- const origin = normalizeRelayEndpoint(endpoint);
1179
- const trimmed = code.trim();
1180
- if (!/^\d{8}$/u.test(trimmed)) throw new TypeError("Relay pairing code must contain exactly eight digits.");
1181
- const record = requireRecord(await requestJson(fetcher, `${origin}/v1/pair`, {
1182
- method: "POST",
1183
- headers: { "content-type": "application/json" },
1184
- body: JSON.stringify({ code: trimmed }),
1185
- redirect: "error"
1186
- }), "Relay returned an invalid pairing response.");
1187
- const token = String(record.token ?? "");
1188
- if (!TOKEN_PATTERN.test(token)) throw new Error("Relay returned an invalid pairing token.");
1189
- if (typeof record.expiresAt !== "number" || !Number.isFinite(record.expiresAt) || record.expiresAt <= now()) throw new Error("Relay returned an invalid session expiration.");
1190
- return {
1191
- endpoint: origin,
1192
- token,
1193
- expiresAt: record.expiresAt
1194
- };
1195
- }
1196
- async function requestClientSecret(session, request, fetcher = fetch, now = Date.now) {
1197
- if (session.expiresAt <= now()) throw new Error("Relay session has expired. Pair with the local relay again.");
1198
- const data = requireRecord(requireRecord(await requestJson(fetcher, `${session.endpoint}/v1/openai/realtime/client-secrets`, {
1199
- method: "POST",
1200
- headers: {
1201
- "content-type": "application/json",
1202
- authorization: `Bearer ${session.token}`
1203
- },
1204
- body: JSON.stringify({ session: request }),
1205
- redirect: "error"
1206
- }), "Relay returned an invalid client secret response.").data, "Relay returned an invalid client secret response.");
1207
- const value = String(data.value ?? "");
1208
- if (!CLIENT_SECRET_PATTERN.test(value)) throw new Error("Relay returned an invalid client secret.");
1209
- if (typeof data.expiresAt !== "number" || !Number.isFinite(data.expiresAt) || data.expiresAt <= now()) throw new Error("Relay returned an expired client secret.");
1210
- return {
1211
- value,
1212
- expiresAt: data.expiresAt,
1213
- model: String(data.model ?? "")
1214
- };
1215
- }
1216
- async function requestJson(fetcher, url, init) {
1217
- const response = await fetcher(url, init);
1218
- const text = await response.text();
1219
- let result = null;
1220
- if (text.length > 0) try {
1221
- result = JSON.parse(text);
1222
- } catch {
1223
- throw new Error(`Relay returned a non-JSON response (${response.status}).`);
1224
- }
1225
- if (!response.ok) throw new Error(`Relay request failed (${response.status}): ${errorDetail(result)}`);
1226
- return result;
1227
- }
1228
- function requireRecord(value, message) {
1229
- if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(message);
1230
- return value;
1231
- }
1232
- function errorDetail(value) {
1233
- if (typeof value === "object" && value !== null) {
1234
- const error = value.error;
1235
- if (typeof error === "object" && error !== null) {
1236
- const message = error.message;
1237
- if (typeof message === "string") return message;
1238
- }
1239
- }
1240
- return "unknown error";
1241
- }
1242
- //#endregion
1243
- //#region src/session-config.ts
1244
- var MAX_INSTRUCTIONS_LENGTH = 16384;
1245
- var VOICE_PATTERN = /^[a-z0-9_-]{1,32}$/u;
1246
- var MODEL_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/u;
1247
- function defaultSessionSettings() {
1248
- return {
1249
- model: "",
1250
- instructions: "",
1251
- voice: "marin",
1252
- outputMode: "audio"
1253
- };
1254
- }
1255
- /** Accepts an empty string (relay default) or a model identifier; the relay decides what is allowed. */
1256
- function normalizeModel(value) {
1257
- const model = value.trim();
1258
- if (model.length > 0 && !MODEL_PATTERN.test(model)) throw new TypeError("Model must be a model identifier.");
1259
- return model;
1260
- }
1261
- function normalizeVoice(value) {
1262
- const voice = value.trim().toLowerCase();
1263
- if (!VOICE_PATTERN.test(voice)) throw new TypeError("Voice must be a lowercase identifier.");
1264
- return voice;
1265
- }
1266
- function normalizeInstructions(value) {
1267
- if (value.length > 16384) throw new TypeError(`Instructions must be at most ${MAX_INSTRUCTIONS_LENGTH} characters.`);
1268
- return value;
1269
- }
1270
- function normalizeOutputMode(value) {
1271
- if (value === "audio" || value === "text") return value;
1272
- throw new TypeError("Output must be audio or text.");
1273
- }
1274
- function buildSessionRequest(settings, tools) {
1275
- if (tools.length > 32) throw new TypeError(`At most 32 functions can be exported as tools.`);
1276
- const request = {
1277
- voice: settings.voice,
1278
- outputModalities: [settings.outputMode]
1279
- };
1280
- if (settings.model.length > 0) request.model = settings.model;
1281
- if (settings.instructions.length > 0) request.instructions = settings.instructions;
1282
- if (tools.length > 0) request.tools = tools.map((tool) => ({ ...tool }));
1283
- return request;
1284
- }
1285
- //#endregion
1286
- //#region src/usage.ts
1287
- var MODEL_PRICES = {
1288
- "gpt-realtime-2.1": {
1289
- textInput: 4,
1290
- cachedTextInput: .4,
1291
- textOutput: 24,
1292
- audioInput: 32,
1293
- cachedAudioInput: .4,
1294
- audioOutput: 64
1295
- },
1296
- "gpt-realtime-2.1-mini": {
1297
- textInput: .6,
1298
- cachedTextInput: .06,
1299
- textOutput: 2.4,
1300
- audioInput: 10,
1301
- cachedAudioInput: .3,
1302
- audioOutput: 20
1303
- }
1304
- };
1305
- function emptyUsage() {
1306
- return {
1307
- responses: 0,
1308
- inputTokens: 0,
1309
- outputTokens: 0,
1310
- cachedInputTokens: 0,
1311
- textInputTokens: 0,
1312
- audioInputTokens: 0,
1313
- cachedTextInputTokens: 0,
1314
- cachedAudioInputTokens: 0,
1315
- textOutputTokens: 0,
1316
- audioOutputTokens: 0,
1317
- estimatedCostUsd: 0,
1318
- unpricedResponses: 0
1319
- };
1320
- }
1321
- /** Adds one `response.usage` object to the totals. Unknown or malformed usage counts as zero tokens. */
1322
- function addUsage(totals, usage, model) {
1323
- const root = asRecord(usage);
1324
- if (!root) return totals;
1325
- const input = asRecord(root.input_token_details);
1326
- const output = asRecord(root.output_token_details);
1327
- const cachedDetails = asRecord(input?.cached_tokens_details);
1328
- const textInput = count(input?.text_tokens);
1329
- const audioInput = count(input?.audio_tokens);
1330
- const cached = count(input?.cached_tokens);
1331
- const cachedText = cachedDetails ? count(cachedDetails.text_tokens) : Math.min(cached, textInput);
1332
- const cachedAudio = cachedDetails ? count(cachedDetails.audio_tokens) : Math.max(0, cached - cachedText);
1333
- const textOutput = count(output?.text_tokens);
1334
- const audioOutput = count(output?.audio_tokens);
1335
- const next = {
1336
- responses: totals.responses + 1,
1337
- inputTokens: totals.inputTokens + count(root.input_tokens),
1338
- outputTokens: totals.outputTokens + count(root.output_tokens),
1339
- cachedInputTokens: totals.cachedInputTokens + cached,
1340
- textInputTokens: totals.textInputTokens + textInput,
1341
- audioInputTokens: totals.audioInputTokens + audioInput,
1342
- cachedTextInputTokens: totals.cachedTextInputTokens + cachedText,
1343
- cachedAudioInputTokens: totals.cachedAudioInputTokens + cachedAudio,
1344
- textOutputTokens: totals.textOutputTokens + textOutput,
1345
- audioOutputTokens: totals.audioOutputTokens + audioOutput,
1346
- estimatedCostUsd: totals.estimatedCostUsd,
1347
- unpricedResponses: totals.unpricedResponses
1348
- };
1349
- const price = MODEL_PRICES[model];
1350
- if (!price) {
1351
- next.unpricedResponses += 1;
1352
- return next;
1353
- }
1354
- next.estimatedCostUsd += (Math.max(0, textInput - cachedText) * price.textInput + cachedText * price.cachedTextInput + Math.max(0, audioInput - cachedAudio) * price.audioInput + cachedAudio * price.cachedAudioInput + textOutput * price.textOutput + audioOutput * price.audioOutput) / 1e6;
1355
- return next;
1356
- }
1357
- function count(value) {
1358
- return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
1359
- }
1360
- function asRecord(value) {
1361
- return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
1362
- }
1363
- //#endregion
1364
- //#region src/webrtc-transport.ts
1365
- var REALTIME_CALLS_URL = "https://api.openai.com/v1/realtime/calls";
1366
- var EVENTS_CHANNEL = "oai-events";
1367
- function browserWebRtcDependencies() {
1368
- return {
1369
- createPeerConnection: () => new RTCPeerConnection(),
1370
- getUserMedia: (constraints) => navigator.mediaDevices.getUserMedia(constraints),
1371
- createAudioElement: () => {
1372
- const element = document.createElement("audio");
1373
- element.autoplay = true;
1374
- return element;
1375
- },
1376
- fetch: (input, init) => fetch(input, init)
1377
- };
1378
- }
1379
- /** Browser-to-OpenAI WebRTC session authenticated with an ephemeral client secret. */
1380
- var WebRtcTransport = class {
1381
- constructor(deps = browserWebRtcDependencies()) {
1382
- this.deps = deps;
1383
- this.peer = null;
1384
- this.channel = null;
1385
- this.microphone = null;
1386
- this.audio = null;
1387
- this.closed = false;
1388
- }
1389
- async connect(options) {
1390
- this.closed = false;
1391
- try {
1392
- const peer = this.deps.createPeerConnection();
1393
- this.peer = peer;
1394
- const audio = this.deps.createAudioElement();
1395
- this.audio = audio;
1396
- peer.ontrack = (event) => {
1397
- audio.srcObject = event.streams[0] ?? null;
1398
- };
1399
- if (options.microphone) {
1400
- const stream = await this.deps.getUserMedia({ audio: true });
1401
- this.microphone = stream;
1402
- for (const track of stream.getAudioTracks()) peer.addTrack(track, stream);
1403
- } else peer.addTransceiver("audio", { direction: "recvonly" });
1404
- const channel = peer.createDataChannel(EVENTS_CHANNEL);
1405
- this.channel = channel;
1406
- channel.onmessage = (message) => {
1407
- const event = parseEvent(message.data);
1408
- if (event) options.onEvent(event);
1409
- };
1410
- channel.onclose = () => this.handleClose(options, "The Realtime data channel closed.");
1411
- peer.onconnectionstatechange = () => {
1412
- if (peer.connectionState === "failed" || peer.connectionState === "closed") this.handleClose(options, `The Realtime connection ${peer.connectionState}.`);
1413
- };
1414
- const opened = waitForOpen(channel, this.deps.channelOpenTimeoutMs ?? 15e3);
1415
- opened.catch(() => void 0);
1416
- const offer = await peer.createOffer();
1417
- await peer.setLocalDescription(offer);
1418
- const response = await this.deps.fetch(REALTIME_CALLS_URL, {
1419
- method: "POST",
1420
- body: offer.sdp ?? "",
1421
- headers: {
1422
- authorization: `Bearer ${options.clientSecret}`,
1423
- "content-type": "application/sdp"
1424
- },
1425
- redirect: "error"
1426
- });
1427
- const answer = await response.text();
1428
- if (!response.ok) throw new Error(`Realtime call setup failed (${response.status}).`);
1429
- await peer.setRemoteDescription({
1430
- type: "answer",
1431
- sdp: answer
1432
- });
1433
- await opened;
1434
- } catch (error) {
1435
- this.close();
1436
- throw error;
1437
- }
1438
- }
1439
- send(event) {
1440
- if (!this.channel || this.channel.readyState !== "open") throw new Error("Not connected to Realtime.");
1441
- this.channel.send(JSON.stringify(event));
1442
- }
1443
- close() {
1444
- this.closed = true;
1445
- const channel = this.channel;
1446
- const peer = this.peer;
1447
- this.channel = null;
1448
- this.peer = null;
1449
- if (channel) {
1450
- channel.onmessage = null;
1451
- channel.onclose = null;
1452
- channel.close();
1453
- }
1454
- if (peer) {
1455
- peer.onconnectionstatechange = null;
1456
- peer.ontrack = null;
1457
- peer.close();
1458
- }
1459
- for (const track of this.microphone?.getTracks() ?? []) track.stop();
1460
- this.microphone = null;
1461
- if (this.audio) {
1462
- this.audio.srcObject = null;
1463
- this.audio = null;
1464
- }
1465
- }
1466
- handleClose(options, reason) {
1467
- if (this.closed) return;
1468
- this.close();
1469
- options.onClose(reason);
1470
- }
1471
- };
1472
- function parseEvent(data) {
1473
- if (typeof data !== "string") return null;
1474
- try {
1475
- const value = JSON.parse(data);
1476
- if (typeof value === "object" && value !== null && typeof value.type === "string") return value;
1477
- } catch {}
1478
- return null;
1479
- }
1480
- function waitForOpen(channel, timeoutMs) {
1481
- if (channel.readyState === "open") return Promise.resolve();
1482
- return new Promise((resolve, reject) => {
1483
- const timer = setTimeout(() => reject(/* @__PURE__ */ new Error("Timed out opening the Realtime data channel.")), timeoutMs);
1484
- channel.addEventListener("open", () => {
1485
- clearTimeout(timer);
1486
- resolve();
1487
- }, { once: true });
1488
- });
1489
- }
1490
- //#endregion
1491
- //#region src/composition.ts
1492
- /**
1493
- * Composition API: the OpenAI Realtime capability without TurboWarp block definitions.
1494
- *
1495
- * A downstream extension (for example `@kubohiroya/turbowarp-voice-chat`) owns its own blocks and
1496
- * hats, and passes its own `define function` hat opcode. Importing this module does not register
1497
- * any TurboWarp extension and does not touch the `Scratch` global.
1498
- */
1499
- var DEFAULT_RELAY_ENDPOINT = "http://127.0.0.1:8787";
1500
- function createRealtimeComposition(options) {
1501
- return new Composition(options);
1502
- }
1503
- var Composition = class {
1504
- constructor(options) {
1505
- this.options = options;
1506
- this.relayEndpoint = DEFAULT_RELAY_ENDPOINT;
1507
- this.relaySession = null;
1508
- this.settingsValue = defaultSessionSettings();
1509
- this.timeLimitSeconds = 0;
1510
- this.connectedAt = null;
1511
- this.timeLimitTimer = null;
1512
- this.activeModelValue = "";
1513
- this.lastResponse = "";
1514
- this.usageTotals = emptyUsage();
1515
- this.listeners = /* @__PURE__ */ new Set();
1516
- this.fetcher = options.fetch ?? ((input, init) => fetch(input, init));
1517
- this.createTransport = options.createTransport ?? (() => new WebRtcTransport());
1518
- this.now = options.now ?? Date.now;
1519
- this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
1520
- this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
1521
- this.ownsFunctions = options.functions === void 0;
1522
- this.namedFunctions = options.functions ?? createNamedFunctions({
1523
- runtime: options.runtime,
1524
- functionHatOpcode: options.functionHatOpcode
1525
- });
1526
- this.session = new RealtimeSession({
1527
- callFunction: (name, args) => this.namedFunctions.call(name, args, { exportedOnly: true }),
1528
- onResponseText: (text) => {
1529
- this.lastResponse = text;
1530
- this.emit({
1531
- type: "response",
1532
- text
1533
- });
1534
- },
1535
- onError: (message) => this.emit({
1536
- type: "error",
1537
- message
1538
- }),
1539
- onStateChange: (state) => this.handleState(state),
1540
- onUsage: (usage) => {
1541
- this.usageTotals = addUsage(this.usageTotals, usage, this.activeModelValue);
1542
- this.emit({
1543
- type: "usage",
1544
- usage: this.usage()
1545
- });
1546
- }
1547
- });
1548
- }
1549
- configureRelay(endpoint) {
1550
- this.relayEndpoint = normalizeRelayEndpoint(endpoint);
1551
- this.relaySession = null;
1552
- }
1553
- async pairRelay(code) {
1554
- this.relaySession = await pairWithRelay(this.relayEndpoint, code, this.fetcher, this.now);
1555
- }
1556
- isRelayPaired() {
1557
- return this.relaySession !== null && this.relaySession.expiresAt > this.now();
1558
- }
1559
- setModel(model) {
1560
- this.settingsValue = {
1561
- ...this.settingsValue,
1562
- model: normalizeModel(model)
1563
- };
1564
- }
1565
- setInstructions(text) {
1566
- this.settingsValue = {
1567
- ...this.settingsValue,
1568
- instructions: normalizeInstructions(text)
1569
- };
1570
- }
1571
- setVoice(voice) {
1572
- this.settingsValue = {
1573
- ...this.settingsValue,
1574
- voice: normalizeVoice(voice)
1575
- };
1576
- }
1577
- setOutputMode(mode) {
1578
- this.settingsValue = {
1579
- ...this.settingsValue,
1580
- outputMode: normalizeOutputMode(mode)
1581
- };
1582
- }
1583
- setSessionTimeLimit(seconds) {
1584
- if (!Number.isFinite(seconds) || seconds < 0) throw new TypeError("Session time limit must be zero or a positive number of seconds.");
1585
- this.timeLimitSeconds = seconds;
1586
- }
1587
- get settings() {
1588
- return { ...this.settingsValue };
1589
- }
1590
- get sessionTimeLimitSeconds() {
1591
- return this.timeLimitSeconds;
1592
- }
1593
- scanFunctions() {
1594
- return this.namedFunctions.scan();
1595
- }
1596
- get functions() {
1597
- return this.namedFunctions;
1598
- }
1599
- async connect(options) {
1600
- if (!this.relaySession || !this.isRelayPaired()) throw new Error("Pair with the local relay first.");
1601
- const request = buildSessionRequest(this.settingsValue, this.namedFunctions.tools());
1602
- const secret = await requestClientSecret(this.relaySession, request, this.fetcher, this.now);
1603
- this.activeModelValue = secret.model || this.settingsValue.model;
1604
- await this.session.open(this.createTransport(), secret.value, options.microphone);
1605
- }
1606
- disconnect() {
1607
- this.session.close();
1608
- }
1609
- get state() {
1610
- return this.session.state;
1611
- }
1612
- get activeModel() {
1613
- return this.activeModelValue;
1614
- }
1615
- sessionElapsedSeconds() {
1616
- return this.connectedAt === null ? 0 : Math.max(0, (this.now() - this.connectedAt) / 1e3);
1617
- }
1618
- sendText(text) {
1619
- this.session.sendText(text);
1620
- }
1621
- get lastResponseText() {
1622
- return this.lastResponse;
1623
- }
1624
- matchFunctionHat(name, thread) {
1625
- return this.namedFunctions.matchHat(name, thread);
1626
- }
1627
- functionArguments(thread) {
1628
- return this.namedFunctions.argumentsFor(thread);
1629
- }
1630
- returnFromFunction(thread, value) {
1631
- this.namedFunctions.returnFrom(thread, value);
1632
- }
1633
- usage() {
1634
- return { ...this.usageTotals };
1635
- }
1636
- resetUsage() {
1637
- this.usageTotals = emptyUsage();
1638
- this.emit({
1639
- type: "usage",
1640
- usage: this.usage()
1641
- });
1642
- }
1643
- subscribe(listener) {
1644
- this.listeners.add(listener);
1645
- return () => this.listeners.delete(listener);
1646
- }
1647
- release() {
1648
- this.disconnect();
1649
- this.listeners.clear();
1650
- if (this.ownsFunctions) this.namedFunctions.release();
1651
- }
1652
- handleState(state) {
1653
- if (state === "connected") {
1654
- this.connectedAt = this.now();
1655
- if (this.timeLimitSeconds > 0) this.timeLimitTimer = this.setTimer(() => this.reachTimeLimit(), this.timeLimitSeconds * 1e3);
1656
- } else if (state !== "connecting") {
1657
- this.connectedAt = null;
1658
- this.cancelTimeLimit();
1659
- this.namedFunctions.cancelAll("The Realtime session ended.");
1660
- }
1661
- this.emit({
1662
- type: "state",
1663
- state
1664
- });
1665
- }
1666
- reachTimeLimit() {
1667
- this.timeLimitTimer = null;
1668
- if (this.session.state !== "connected") return;
1669
- this.session.close();
1670
- this.emit({ type: "sessionTimeLimitReached" });
1671
- }
1672
- cancelTimeLimit() {
1673
- if (this.timeLimitTimer !== null) this.clearTimer(this.timeLimitTimer);
1674
- this.timeLimitTimer = null;
1675
- }
1676
- emit(event) {
1677
- for (const listener of [...this.listeners]) listener(event);
1678
- }
1679
- };
1680
- //#endregion
1681
- //#region src/extension.ts
1682
- var blockDefinitions = block_definitions_default.blocks;
1683
- var menuDefinitions = block_definitions_default.menus;
1684
- var DEFINE_FUNCTION_OPCODE = `${extensionConfig.id}_defineFunction`;
1685
- var RESPONSE_DONE_OPCODE = `${extensionConfig.id}_whenResponseDone`;
1686
- var TIME_LIMIT_OPCODE = `${extensionConfig.id}_whenSessionTimeLimitReached`;
1687
- var USAGE_FIELDS = {
1688
- costUSD: (usage) => Math.round(usage.estimatedCostUsd * 1e6) / 1e6,
1689
- responses: (usage) => usage.responses,
1690
- inputTokens: (usage) => usage.inputTokens,
1691
- outputTokens: (usage) => usage.outputTokens,
1692
- cachedInputTokens: (usage) => usage.cachedInputTokens,
1693
- textInputTokens: (usage) => usage.textInputTokens,
1694
- audioInputTokens: (usage) => usage.audioInputTokens,
1695
- textOutputTokens: (usage) => usage.textOutputTokens,
1696
- audioOutputTokens: (usage) => usage.audioOutputTokens
1697
- };
1698
- /** Block surface over the Realtime composition. */
1699
- var OpenAIRealtimeExtension = class {
1700
- constructor(deps) {
1701
- this.lastErrorMessage = "";
1702
- this.fixedEditorRuntime = deps.runtime;
1703
- this.realtime = createRealtimeComposition({
1704
- ...deps,
1705
- functionHatOpcode: DEFINE_FUNCTION_OPCODE
1706
- });
1707
- this.realtime.subscribe((event) => {
1708
- if (event.type === "response") deps.runtime.startHats(RESPONSE_DONE_OPCODE);
1709
- else if (event.type === "sessionTimeLimitReached") deps.runtime.startHats(TIME_LIMIT_OPCODE);
1710
- else if (event.type === "error") this.lastErrorMessage = event.message;
1711
- });
1712
- }
1713
- getInfo() {
1714
- const info = {
1715
- id: extensionConfig.id,
1716
- name: Scratch.translate(block_definitions_default.extensionName),
1717
- docsURI: extensionConfig.docsURI,
1718
- blockIconURI: extensionConfig.blockIconURI,
1719
- blocks: blockDefinitions.map((block) => this.toScratchBlock(block)),
1720
- menus: Object.fromEntries(Object.entries(menuDefinitions).map(([id, menu]) => [id, {
1721
- acceptReporters: menu.acceptReporters,
1722
- items: [...menu.items]
1723
- }]))
1724
- };
1725
- this.fixedEditorsDispose ?? (this.fixedEditorsDispose = registerFixedInputEditors(Scratch, this.fixedEditorRuntime, info.blocks));
1726
- return info;
1727
- }
1728
- configureRelay(args) {
1729
- this.record(() => this.realtime.configureRelay(Scratch.Cast.toString(args.ENDPOINT)));
1730
- }
1731
- pairRelay(args) {
1732
- return this.recordAsync(() => this.realtime.pairRelay(Scratch.Cast.toString(args.CODE)));
1733
- }
1734
- isRelayPaired() {
1735
- return this.realtime.isRelayPaired();
1736
- }
1737
- setInstructions(args) {
1738
- this.record(() => this.realtime.setInstructions(Scratch.Cast.toString(args.TEXT)));
1739
- }
1740
- setVoice(args) {
1741
- this.record(() => this.realtime.setVoice(Scratch.Cast.toString(args.VOICE)));
1742
- }
1743
- setOutputMode(args) {
1744
- this.record(() => this.realtime.setOutputMode(normalizeOutputMode(Scratch.Cast.toString(args.MODE))));
1745
- }
1746
- setModel(args) {
1747
- this.record(() => this.realtime.setModel(Scratch.Cast.toString(args.MODEL)));
1748
- }
1749
- setSessionTimeLimit(args) {
1750
- this.record(() => this.realtime.setSessionTimeLimit(Scratch.Cast.toNumber(args.SECONDS)));
1751
- }
1752
- connect(args) {
1753
- return this.recordAsync(() => this.realtime.connect({ microphone: Scratch.Cast.toString(args.MICROPHONE) !== "off" }));
1754
- }
1755
- disconnect() {
1756
- this.realtime.disconnect();
1757
- }
1758
- isConnected() {
1759
- return this.realtime.state === "connected";
1760
- }
1761
- connectionState() {
1762
- return this.realtime.state;
1763
- }
1764
- currentModel() {
1765
- return this.realtime.activeModel;
1766
- }
1767
- sessionElapsed() {
1768
- return Math.floor(this.realtime.sessionElapsedSeconds());
1769
- }
1770
- whenSessionTimeLimitReached() {
1771
- return true;
1772
- }
1773
- sendText(args) {
1774
- this.record(() => this.realtime.sendText(Scratch.Cast.toString(args.TEXT)));
1775
- }
1776
- whenResponseDone() {
1777
- return true;
1778
- }
1779
- lastResponseText() {
1780
- return this.realtime.lastResponseText;
1781
- }
1782
- defineFunction(args, util) {
1783
- return this.realtime.matchFunctionHat(Scratch.Cast.toString(args.NAME), util?.thread);
1784
- }
1785
- functionArgument(args, util) {
1786
- try {
1787
- return toScratchValue(readArgumentPath(this.realtime.functionArguments(util?.thread), Scratch.Cast.toString(args.PATH)));
1788
- } catch (error) {
1789
- this.lastErrorMessage = messageOf(error);
1790
- return "";
1791
- }
1792
- }
1793
- functionArgumentsJson(_args, util) {
1794
- try {
1795
- return JSON.stringify(this.realtime.functionArguments(util?.thread) ?? null);
1796
- } catch (error) {
1797
- this.lastErrorMessage = messageOf(error);
1798
- return "";
1799
- }
1800
- }
1801
- returnValue(args, util) {
1802
- try {
1803
- this.realtime.returnFromFunction(util?.thread, parseJsonOrText(Scratch.Cast.toString(args.VALUE)));
1804
- util?.stopThisScript?.();
1805
- } catch (error) {
1806
- this.lastErrorMessage = messageOf(error);
1807
- }
1808
- }
1809
- usageValue(args) {
1810
- const read = USAGE_FIELDS[Scratch.Cast.toString(args.FIELD)];
1811
- return read ? read(this.realtime.usage()) : "";
1812
- }
1813
- resetUsage() {
1814
- this.realtime.resetUsage();
1815
- }
1816
- lastError() {
1817
- return this.lastErrorMessage;
1818
- }
1819
- record(action) {
1820
- try {
1821
- action();
1822
- this.lastErrorMessage = "";
1823
- } catch (error) {
1824
- this.lastErrorMessage = messageOf(error);
1825
- }
1826
- }
1827
- async recordAsync(action) {
1828
- try {
1829
- await action();
1830
- this.lastErrorMessage = "";
1831
- } catch (error) {
1832
- this.lastErrorMessage = messageOf(error);
1833
- }
1834
- }
1835
- toScratchBlock(block) {
1836
- const scratchBlock = {
1837
- opcode: block.opcode,
1838
- blockType: Scratch.BlockType[block.blockType],
1839
- text: Scratch.translate(block.text),
1840
- arguments: Object.fromEntries(Object.entries(block.arguments).map(([name, argument]) => [name, {
1841
- type: Scratch.ArgumentType[argument.type],
1842
- defaultValue: argument.defaultValue,
1843
- ...argument.menu ? { menu: argument.menu } : {}
1844
- }]))
1845
- };
1846
- if (block.blockType === "HAT") scratchBlock.isEdgeActivated = false;
1847
- return scratchBlock;
1848
- }
1849
- };
1850
- function messageOf(error) {
1851
- return error instanceof Error ? error.message : String(error);
1852
- }
1853
- //#endregion
1854
- //#region src/index.ts
1855
- if (extensionConfig.unsandboxed && !Scratch.extensions.unsandboxed) throw new Error(`${extensionConfig.name} must run unsandboxed.`);
1856
- var runtime = Scratch.vm?.runtime;
1857
- if (!runtime) throw new Error(`${extensionConfig.name} requires access to the TurboWarp VM runtime.`);
1858
- Scratch.extensions.register(new OpenAIRealtimeExtension({ runtime }));
1859
- //#endregion
10
+ //#region src/config.ts
11
+ var extensionConfig = {
12
+ id: "kubohiroyaopenairealtime",
13
+ slug: "turbowarp-openai-realtime-api",
14
+ name: "TurboWarp-OpenAI-Realtime-API",
15
+ description: "Talk with the OpenAI Realtime API from TurboWarp through a localhost relay.",
16
+ author: "Hiroya Kubo",
17
+ license: "MPL-2.0",
18
+ unsandboxed: true,
19
+ docsURI: "https://kubohiroya.github.io/turbowarp-openai-realtime-api/",
20
+ blockIconURI: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCI+PHBhdGggZD0iTTggMTBoMzJhNCA0IDAgMCAxIDQgNHYxNmE0IDQgMCAwIDEtNCA0SDIybC05IDd2LTdIOGE0IDQgMCAwIDEtNC00VjE0YTQgNCAwIDAgMSA0LTR6IiBmaWxsPSIjMTBBMzdGIi8+PGcgc3Ryb2tlPSIjZmZmIiBzdHJva2Utd2lkdGg9IjMiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCI+PHBhdGggZD0iTTE0IDE5djYiLz48cGF0aCBkPSJNMjAgMTZ2MTIiLz48cGF0aCBkPSJNMjYgMTh2OCIvPjxwYXRoIGQ9Ik0zMiAxNXYxNCIvPjxwYXRoIGQ9Ik0zOCAyMHY0Ii8+PC9nPjwvc3ZnPg=="
21
+ };
22
+ var block_editor_fixed_inputs_schema_default = {
23
+ $schema: "http://json-schema.org/draft-07/schema#",
24
+ $id: "https://kubohiroya.github.io/turbowarp-openai-realtime-api/schemas/block-editor-fixed-inputs.schema.json",
25
+ title: "turbowarp-openai-realtime-api fixed editing contracts",
26
+ $defs: {
27
+ "setOutputMode": {
28
+ "type": "object",
29
+ "properties": { "MODE": {
30
+ "type": "string",
31
+ "enum": ["audio", "text"]
32
+ } },
33
+ "additionalProperties": false
34
+ },
35
+ "setSessionTimeLimit": {
36
+ "type": "object",
37
+ "properties": { "SECONDS": {
38
+ "type": "number",
39
+ "minimum": 0
40
+ } },
41
+ "additionalProperties": false
42
+ },
43
+ "connect": {
44
+ "type": "object",
45
+ "properties": { "MICROPHONE": {
46
+ "type": "string",
47
+ "enum": ["on", "off"]
48
+ } },
49
+ "additionalProperties": false
50
+ },
51
+ "defineFunction": {
52
+ "type": "object",
53
+ "properties": { "EXPORT": {
54
+ "type": "string",
55
+ "enum": ["tool", "none"]
56
+ } },
57
+ "additionalProperties": false
58
+ },
59
+ "usageValue": {
60
+ "type": "object",
61
+ "properties": { "FIELD": {
62
+ "type": "string",
63
+ "enum": [
64
+ "costUSD",
65
+ "responses",
66
+ "inputTokens",
67
+ "outputTokens",
68
+ "cachedInputTokens",
69
+ "textInputTokens",
70
+ "audioInputTokens",
71
+ "textOutputTokens",
72
+ "audioOutputTokens"
73
+ ]
74
+ } },
75
+ "additionalProperties": false
76
+ }
77
+ }
78
+ };
79
+ //#endregion
80
+ //#region src/fixed-input-editors.ts
81
+ var contracts = block_editor_fixed_inputs_schema_default.$defs;
82
+ var repository = "turbowarp-openai-realtime-api";
83
+ var extensionId = "kubohiroyaopenairealtime";
84
+ var token = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/u;
85
+ function readNumber(value) {
86
+ if (typeof value !== "string" && typeof value !== "number") throw new TypeError("Invalid numeric projection.");
87
+ if (typeof value === "string" && value.trim() === "") throw new TypeError("Existing blank field is readonly.");
88
+ const number = Number(value);
89
+ if (!Number.isFinite(number)) throw new TypeError("Existing nonfinite field is readonly.");
90
+ return number;
91
+ }
92
+ function newNumberToken(value) {
93
+ if (typeof value === "number") {
94
+ if (!Number.isFinite(value)) throw new TypeError("Invalid new number.");
95
+ return Object.is(value, -0) ? "-0" : String(value);
96
+ }
97
+ if (typeof value !== "string" || !token.test(value.trim()) || !Number.isFinite(Number(value))) throw new TypeError("Invalid new number.");
98
+ return value.trim();
99
+ }
100
+ function existingChoice(_opcode, value) {
101
+ if (typeof value !== "string") throw new TypeError("Invalid string projection.");
102
+ return value;
103
+ }
104
+ function candidateValid(_opcode, _argument, schema, value) {
105
+ if (schema.type === "number" || schema.type === "integer") {
106
+ let number;
107
+ try {
108
+ number = readNumber(value);
109
+ } catch {
110
+ return false;
111
+ }
112
+ if (schema.type === "integer" && !Number.isInteger(number)) return false;
113
+ if (schema.minimum !== void 0 && number < schema.minimum) return false;
114
+ if (schema.maximum !== void 0 && number > schema.maximum) return false;
115
+ if (schema.exclusiveMinimum !== void 0 && number <= schema.exclusiveMinimum) return false;
116
+ if (schema.enum && !schema.enum.includes(number)) return false;
117
+ return true;
118
+ }
119
+ return typeof value === "string" && schema.enum.includes(value);
120
+ }
121
+ function fixedInputEditorOptions() {
122
+ return Object.entries(contracts).map(([opcode, contract]) => ({
123
+ apiVersion: 1,
124
+ extensionId,
125
+ editorId: `${extensionId}.fixed.${opcode}`,
126
+ opcodes: [`${extensionId}_${opcode}`],
127
+ contractId: `${repository}.${opcode}.fixed`,
128
+ contractRevision: "fixed-inputs-v1",
129
+ validationContext: () => ({
130
+ consumerId: repository,
131
+ nowMs: 0,
132
+ settings: null
133
+ }),
134
+ bindings: Object.entries(contract.properties).map(([argument, schema]) => {
135
+ const numeric = schema.type === "number" || schema.type === "integer";
136
+ const source = {
137
+ repository,
138
+ path: "schemas/block-editor-fixed-inputs.schema.json",
139
+ symbol: `/$defs/${opcode}/properties/${argument}`
140
+ };
141
+ return {
142
+ argument,
143
+ schemaBundle: {
144
+ identity: {
145
+ ownerId: repository,
146
+ contractId: `${opcode}.${argument}`,
147
+ revision: "fixed-inputs-v1",
148
+ variantId: "fixed"
149
+ },
150
+ dialect: "draft-07",
151
+ root: {
152
+ retrievalUri: `${block_editor_fixed_inputs_schema_default.$id}?binding=${opcode}.${argument}`,
153
+ document: schema,
154
+ artifact: {
155
+ role: "canonical-schema",
156
+ source
157
+ }
158
+ },
159
+ resources: [],
160
+ provenance: {
161
+ canonicalSources: [
162
+ source,
163
+ {
164
+ repository,
165
+ path: "src/block-definitions.json"
166
+ },
167
+ {
168
+ repository,
169
+ path: "src/extension.ts",
170
+ symbol: opcode
171
+ }
172
+ ],
173
+ generatedFiles: [],
174
+ referenceSchemas: [],
175
+ generationSources: []
176
+ }
177
+ },
178
+ control: numeric ? { kind: "number" } : {
179
+ kind: "enum",
180
+ choices: schema.enum.map((value) => ({
181
+ value,
182
+ label: String(value)
183
+ }))
184
+ },
185
+ read: (value) => numeric ? readNumber(value) : existingChoice(opcode, value),
186
+ encode: (value) => {
187
+ if (numeric) return newNumberToken(value);
188
+ if (typeof value !== "string" || !schema.enum.includes(value)) throw new TypeError("Invalid new enum value.");
189
+ return value;
190
+ },
191
+ validateCandidate: (value) => candidateValid(opcode, argument, schema, value) ? [] : [{
192
+ path: argument,
193
+ message: "Check this value before saving."
194
+ }]
195
+ };
196
+ })
197
+ }));
198
+ }
199
+ /** Register only visible static contracts; ordinary composition consumers load no editor runtime. */
200
+ function registerFixedInputEditors(scratch, runtime, visibleBlocks) {
201
+ const api = scratch.gui?.blockEditors;
202
+ if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function") return void 0;
203
+ const visibleOpcodes = new Set(visibleBlocks.flatMap((block) => {
204
+ if (typeof block !== "object" || block === null || !("opcode" in block)) return [];
205
+ return typeof block.opcode === "string" ? [block.opcode] : [];
206
+ }));
207
+ const events = runtime;
208
+ const removers = [];
209
+ let active = true;
210
+ const dispose = () => {
211
+ if (!active) return;
212
+ active = false;
213
+ events?.off?.("RUNTIME_DISPOSED", dispose);
214
+ for (const remove of removers.splice(0).reverse()) remove();
215
+ };
216
+ try {
217
+ for (const options of fixedInputEditorOptions()) if (options.opcodes.some((opcode) => visibleOpcodes.has(opcode.slice(25)))) removers.push(api.registerFixedInputEditor.call(api, options));
218
+ events?.on?.("RUNTIME_DISPOSED", dispose);
219
+ } catch (error) {
220
+ dispose();
221
+ throw error;
222
+ }
223
+ return dispose;
224
+ }
225
+ var block_definitions_default = {
226
+ extensionName: "OpenAI Realtime",
227
+ blocks: [
228
+ {
229
+ "opcode": "configureRelay",
230
+ "blockType": "COMMAND",
231
+ "text": "configure local relay [ENDPOINT]",
232
+ "description": "Sets the loopback origin of the local capability-proxy relay. Clears any previous pairing.",
233
+ "descriptionJa": "localhostで動くcapability-proxy中継のループバックoriginを設定します。以前のペアリングは消去されます。",
234
+ "arguments": { "ENDPOINT": {
235
+ "type": "STRING",
236
+ "defaultValue": "http://127.0.0.1:8787"
237
+ } }
238
+ },
239
+ {
240
+ "opcode": "pairRelay",
241
+ "blockType": "COMMAND",
242
+ "text": "pair local relay with one-time code [CODE]",
243
+ "description": "Exchanges the eight-digit code printed by the relay for a session token kept only in memory.",
244
+ "descriptionJa": "中継が表示した8桁のコードを、メモリだけに保持するセッションtokenと交換します。",
245
+ "arguments": { "CODE": {
246
+ "type": "STRING",
247
+ "defaultValue": "00000000"
248
+ } }
249
+ },
250
+ {
251
+ "opcode": "isRelayPaired",
252
+ "blockType": "BOOLEAN",
253
+ "text": "local relay paired?",
254
+ "description": "Reports whether an unexpired relay session token is held in memory.",
255
+ "descriptionJa": "有効期限内の中継セッションtokenをメモリに保持しているかを返します。",
256
+ "arguments": {}
257
+ },
258
+ {
259
+ "opcode": "setInstructions",
260
+ "blockType": "COMMAND",
261
+ "text": "set instructions to [TEXT]",
262
+ "description": "Sets the system instructions used by the next connection.",
263
+ "descriptionJa": "次の接続で使うシステムへの指示(instructions)を設定します。",
264
+ "arguments": { "TEXT": {
265
+ "type": "STRING",
266
+ "defaultValue": "You are a friendly assistant. Answer briefly."
267
+ } }
268
+ },
269
+ {
270
+ "opcode": "setVoice",
271
+ "blockType": "COMMAND",
272
+ "text": "set voice to [VOICE]",
273
+ "description": "Sets the output voice used by the next connection.",
274
+ "descriptionJa": "次の接続で使う応答の声を設定します。",
275
+ "arguments": { "VOICE": {
276
+ "type": "STRING",
277
+ "menu": "voices",
278
+ "defaultValue": "marin"
279
+ } }
280
+ },
281
+ {
282
+ "opcode": "setOutputMode",
283
+ "blockType": "COMMAND",
284
+ "text": "set output to [MODE]",
285
+ "description": "Chooses spoken audio or text-only responses for the next connection.",
286
+ "descriptionJa": "次の接続で、音声で応答するかテキストだけで応答するかを選びます。",
287
+ "arguments": { "MODE": {
288
+ "type": "STRING",
289
+ "menu": "outputModes",
290
+ "defaultValue": "audio"
291
+ } }
292
+ },
293
+ {
294
+ "opcode": "setModel",
295
+ "blockType": "COMMAND",
296
+ "text": "set model to [MODEL]",
297
+ "description": "Chooses the Realtime model for the next connection. The relay only accepts models its operator allows; empty uses the relay default.",
298
+ "descriptionJa": "次の接続で使うRealtimeのモデルを選びます。中継の運用者が許可したモデルだけが使えます。空にすると中継の既定のモデルを使います。",
299
+ "arguments": { "MODEL": {
300
+ "type": "STRING",
301
+ "menu": "models",
302
+ "defaultValue": "gpt-realtime-2.1-mini"
303
+ } }
304
+ },
305
+ {
306
+ "opcode": "setSessionTimeLimit",
307
+ "blockType": "COMMAND",
308
+ "text": "set session time limit to [SECONDS] seconds",
309
+ "description": "Disconnects automatically after this many seconds from the next connection on. 0 disables the limit.",
310
+ "descriptionJa": "次の接続から、指定した秒数が経つと自動で切断します。0にすると制限しません。",
311
+ "arguments": { "SECONDS": {
312
+ "type": "NUMBER",
313
+ "defaultValue": "600"
314
+ } }
315
+ },
316
+ {
317
+ "opcode": "connect",
318
+ "blockType": "COMMAND",
319
+ "text": "connect to Realtime with microphone [MICROPHONE]",
320
+ "description": "Mints an ephemeral key through the relay and opens a WebRTC session. Exported functions become tools.",
321
+ "descriptionJa": "中継を通じて一時キーを発行し、WebRTCのセッションを開きます。ツールとして公開した関数はモデルから呼べるようになります。",
322
+ "arguments": { "MICROPHONE": {
323
+ "type": "STRING",
324
+ "menu": "onOff",
325
+ "defaultValue": "on"
326
+ } }
327
+ },
328
+ {
329
+ "opcode": "disconnect",
330
+ "blockType": "COMMAND",
331
+ "text": "disconnect from Realtime",
332
+ "description": "Closes the WebRTC session, stops the microphone, and fails pending function calls.",
333
+ "descriptionJa": "WebRTCのセッションを閉じてマイクを止め、実行中の関数呼び出しを失敗させます。",
334
+ "arguments": {}
335
+ },
336
+ {
337
+ "opcode": "isConnected",
338
+ "blockType": "BOOLEAN",
339
+ "text": "connected to Realtime?",
340
+ "description": "Reports whether the Realtime session is open.",
341
+ "descriptionJa": "Realtimeのセッションが開いているかを返します。",
342
+ "arguments": {}
343
+ },
344
+ {
345
+ "opcode": "connectionState",
346
+ "blockType": "REPORTER",
347
+ "text": "Realtime connection state",
348
+ "description": "Reports disconnected, connecting, connected, or failed.",
349
+ "descriptionJa": "disconnected、connecting、connected、failedのいずれかを返します。",
350
+ "arguments": {}
351
+ },
352
+ {
353
+ "opcode": "currentModel",
354
+ "blockType": "REPORTER",
355
+ "text": "Realtime model",
356
+ "description": "Reports the model the relay used for the current or last connection.",
357
+ "descriptionJa": "現在または直前の接続で中継が使ったモデルを返します。",
358
+ "arguments": {}
359
+ },
360
+ {
361
+ "opcode": "sessionElapsed",
362
+ "blockType": "REPORTER",
363
+ "text": "session elapsed seconds",
364
+ "description": "Reports seconds since the current session connected, or 0 when disconnected.",
365
+ "descriptionJa": "現在のセッションが接続してからの秒数を返します。切断中は0を返します。",
366
+ "arguments": {}
367
+ },
368
+ {
369
+ "opcode": "whenSessionTimeLimitReached",
370
+ "blockType": "HAT",
371
+ "text": "when session time limit is reached",
372
+ "description": "Starts after the session time limit disconnects the session.",
373
+ "descriptionJa": "セッションの時間上限によって切断されたときに起動します。",
374
+ "arguments": {}
375
+ },
376
+ {
377
+ "opcode": "sendText",
378
+ "blockType": "COMMAND",
379
+ "text": "send text [TEXT]",
380
+ "description": "Adds a user text message to the conversation and asks for a response.",
381
+ "descriptionJa": "利用者のテキストメッセージを会話に追加し、応答を要求します。",
382
+ "arguments": { "TEXT": {
383
+ "type": "STRING",
384
+ "defaultValue": "Hello!"
385
+ } }
386
+ },
387
+ {
388
+ "opcode": "whenResponseDone",
389
+ "blockType": "HAT",
390
+ "text": "when assistant finishes responding",
391
+ "description": "Starts when a response that contains assistant text or transcript completes.",
392
+ "descriptionJa": "アシスタントのテキストまたは音声の書き起こしを含む応答が完了したときに起動します。",
393
+ "arguments": {}
394
+ },
395
+ {
396
+ "opcode": "lastResponseText",
397
+ "blockType": "REPORTER",
398
+ "text": "last assistant response",
399
+ "description": "Reports the text or audio transcript of the most recent completed assistant response.",
400
+ "descriptionJa": "直近に完了したアシスタントの応答のテキスト、または音声の書き起こしを返します。",
401
+ "arguments": {}
402
+ },
403
+ {
404
+ "opcode": "defineFunction",
405
+ "blockType": "HAT",
406
+ "text": "define function [NAME] description [DESCRIPTION] args schema [SCHEMA] export as [EXPORT]",
407
+ "description": "Defines a function value. With export as tool, the model can call it; NAME, DESCRIPTION, and SCHEMA must be literal text.",
408
+ "descriptionJa": "関数を定義します。export asをtoolにすると、モデルから呼べるツールになります。NAME、DESCRIPTION、SCHEMAには文字列を直接書く必要があります。",
409
+ "arguments": {
410
+ "NAME": {
411
+ "type": "STRING",
412
+ "defaultValue": "get_score"
413
+ },
414
+ "DESCRIPTION": {
415
+ "type": "STRING",
416
+ "defaultValue": "Returns the player's current score."
417
+ },
418
+ "SCHEMA": {
419
+ "type": "STRING",
420
+ "defaultValue": "{\"type\":\"object\",\"properties\":{}}"
421
+ },
422
+ "EXPORT": {
423
+ "type": "STRING",
424
+ "menu": "exportModes",
425
+ "defaultValue": "tool"
426
+ }
427
+ }
428
+ },
429
+ {
430
+ "opcode": "functionArgument",
431
+ "blockType": "REPORTER",
432
+ "text": "function argument [PATH]",
433
+ "description": "Inside a function, reports the argument at a dotted path such as city or items.0.name.",
434
+ "descriptionJa": "関数の中で、cityやitems.0.nameのようなドット区切りのパスにある引数を返します。",
435
+ "arguments": { "PATH": {
436
+ "type": "STRING",
437
+ "defaultValue": "city"
438
+ } }
439
+ },
440
+ {
441
+ "opcode": "functionArgumentsJson",
442
+ "blockType": "REPORTER",
443
+ "text": "function arguments JSON",
444
+ "description": "Inside a function, reports all arguments as JSON text.",
445
+ "descriptionJa": "関数の中で、すべての引数をJSONテキストとして返します。",
446
+ "arguments": {}
447
+ },
448
+ {
449
+ "opcode": "returnValue",
450
+ "blockType": "COMMAND",
451
+ "text": "return [VALUE]",
452
+ "description": "Inside a function, returns a value and ends the script. JSON text is returned as JSON; other text is returned as a string.",
453
+ "descriptionJa": "関数の中で値を返し、スクリプトを終了します。JSONテキストはJSONとして、それ以外のテキストは文字列として返します。",
454
+ "arguments": { "VALUE": {
455
+ "type": "STRING",
456
+ "defaultValue": "{\"score\":10}"
457
+ } }
458
+ },
459
+ {
460
+ "opcode": "usageValue",
461
+ "blockType": "REPORTER",
462
+ "text": "Realtime usage [FIELD]",
463
+ "description": "Reports token usage totals since the last reset. costUSD is an estimate from published prices; the OpenAI dashboard is authoritative.",
464
+ "descriptionJa": "最後にリセットしてからの使用量の合計を返します。costUSDは公開価格からの概算で、正確な請求額はOpenAIの管理画面で確認してください。",
465
+ "arguments": { "FIELD": {
466
+ "type": "STRING",
467
+ "menu": "usageFields",
468
+ "defaultValue": "costUSD"
469
+ } }
470
+ },
471
+ {
472
+ "opcode": "resetUsage",
473
+ "blockType": "COMMAND",
474
+ "text": "reset Realtime usage",
475
+ "description": "Clears the usage totals.",
476
+ "descriptionJa": "使用量の合計を0に戻します。",
477
+ "arguments": {}
478
+ },
479
+ {
480
+ "opcode": "lastError",
481
+ "blockType": "REPORTER",
482
+ "text": "last Realtime error",
483
+ "description": "Reports the most recent relay, connection, or API error, or an empty string.",
484
+ "descriptionJa": "直近の中継、接続、APIのエラーを返します。エラーがなければ空文字列を返します。",
485
+ "arguments": {}
486
+ }
487
+ ],
488
+ menus: {
489
+ "voices": {
490
+ "acceptReporters": true,
491
+ "items": [
492
+ "alloy",
493
+ "ash",
494
+ "ballad",
495
+ "cedar",
496
+ "coral",
497
+ "echo",
498
+ "marin",
499
+ "sage",
500
+ "shimmer",
501
+ "verse"
502
+ ]
503
+ },
504
+ "outputModes": {
505
+ "acceptReporters": false,
506
+ "items": ["audio", "text"]
507
+ },
508
+ "onOff": {
509
+ "acceptReporters": false,
510
+ "items": ["on", "off"]
511
+ },
512
+ "exportModes": {
513
+ "acceptReporters": false,
514
+ "items": ["tool", "none"]
515
+ },
516
+ "models": {
517
+ "acceptReporters": true,
518
+ "items": ["gpt-realtime-2.1-mini", "gpt-realtime-2.1"]
519
+ },
520
+ "usageFields": {
521
+ "acceptReporters": false,
522
+ "items": [
523
+ "costUSD",
524
+ "responses",
525
+ "inputTokens",
526
+ "outputTokens",
527
+ "cachedInputTokens",
528
+ "textInputTokens",
529
+ "audioInputTokens",
530
+ "textOutputTokens",
531
+ "audioOutputTokens"
532
+ ]
533
+ }
534
+ }
535
+ };
536
+ //#endregion
537
+ //#region node_modules/.pnpm/@kubohiroya+turbowarp-named-functions@0.2.0_react@18.3.1/node_modules/@kubohiroya/turbowarp-named-functions/dist/composition.js
538
+ /**
539
+ * A small JSON Schema validator for function arguments and returns. It interprets the schema at run time and
540
+ * never generates code, so it also works where `eval` is unavailable (for example Cloudflare Workers).
541
+ *
542
+ * Supported keywords: type, properties, required, additionalProperties (boolean), items, enum,
543
+ * const, minimum, maximum, minLength, maxLength, minItems, maxItems. Other keywords, such as
544
+ * description and title, are ignored.
545
+ */
546
+ function validateAgainstSchema(value, schema, path = "$") {
547
+ const rule = asRecord$2(schema);
548
+ if (!rule) throw new Error(`${path} must be a JSON Schema object`);
549
+ assertSchemaNode(rule, path);
550
+ return validateValue(value, rule, path);
551
+ }
552
+ /** Reject malformed schemas before a function definition or call can use them. */
553
+ function validateSchemaDefinition(schema, label = "schema") {
554
+ const rule = asRecord$2(schema);
555
+ if (!rule) throw new Error(`${label} must be a JSON object`);
556
+ assertSchemaNode(rule, label);
557
+ }
558
+ function validateValue(value, rule, path) {
559
+ const errors = [];
560
+ if (rule.type !== void 0) {
561
+ const types = Array.isArray(rule.type) ? rule.type : [rule.type];
562
+ if (!types.some((type) => matchesType(value, type))) return [`${path} must be ${types.join(" or ")}`];
563
+ }
564
+ if (Array.isArray(rule.enum) && !rule.enum.some((candidate) => deepEqual(candidate, value))) errors.push(`${path} must be one of ${rule.enum.map((item) => JSON.stringify(item)).join(", ")}`);
565
+ if ("const" in rule && !deepEqual(rule.const, value)) errors.push(`${path} must be ${JSON.stringify(rule.const)}`);
566
+ if (typeof value === "number") {
567
+ if (typeof rule.minimum === "number" && value < rule.minimum) errors.push(`${path} must be >= ${rule.minimum}`);
568
+ if (typeof rule.maximum === "number" && value > rule.maximum) errors.push(`${path} must be <= ${rule.maximum}`);
569
+ }
570
+ if (typeof value === "string") {
571
+ const length = [...value].length;
572
+ if (typeof rule.minLength === "number" && length < rule.minLength) errors.push(`${path} must have at least ${rule.minLength} characters`);
573
+ if (typeof rule.maxLength === "number" && length > rule.maxLength) errors.push(`${path} must have at most ${rule.maxLength} characters`);
574
+ }
575
+ if (Array.isArray(value)) {
576
+ if (typeof rule.minItems === "number" && value.length < rule.minItems) errors.push(`${path} must have at least ${rule.minItems} items`);
577
+ if (typeof rule.maxItems === "number" && value.length > rule.maxItems) errors.push(`${path} must have at most ${rule.maxItems} items`);
578
+ if (rule.items !== void 0) value.forEach((item, index) => errors.push(...validateValue(item, rule.items, `${path}[${index}]`)));
579
+ }
580
+ const object = asRecord$2(value);
581
+ if (object) {
582
+ const properties = asRecord$2(rule.properties) ?? {};
583
+ if (Array.isArray(rule.required)) {
584
+ for (const name of rule.required) if (typeof name === "string" && !Object.hasOwn(object, name)) errors.push(`${path}.${name} is required`);
585
+ }
586
+ for (const [name, child] of Object.entries(object)) if (Object.hasOwn(properties, name)) errors.push(...validateValue(child, properties[name], `${path}.${name}`));
587
+ else if (rule.additionalProperties === false) errors.push(`${path}.${name} is not allowed`);
588
+ }
589
+ return errors;
590
+ }
591
+ function assertSchemaNode(rule, path) {
592
+ const validTypes = /* @__PURE__ */ new Set([
593
+ "object",
594
+ "array",
595
+ "string",
596
+ "number",
597
+ "integer",
598
+ "boolean",
599
+ "null"
600
+ ]);
601
+ if (rule.type !== void 0) {
602
+ const types = Array.isArray(rule.type) ? rule.type : [rule.type];
603
+ if (types.length === 0 || types.some((type) => typeof type !== "string" || !validTypes.has(type))) throw new Error(`${path}.type must be a supported JSON Schema type or non-empty type array`);
604
+ }
605
+ if (rule.properties !== void 0) {
606
+ const properties = asRecord$2(rule.properties);
607
+ if (!properties) throw new Error(`${path}.properties must be an object`);
608
+ for (const [name, child] of Object.entries(properties)) {
609
+ const childRule = asRecord$2(child);
610
+ if (!childRule) throw new Error(`${path}.properties.${name} must be a JSON Schema object`);
611
+ assertSchemaNode(childRule, `${path}.properties.${name}`);
612
+ }
613
+ }
614
+ if (rule.required !== void 0 && (!Array.isArray(rule.required) || rule.required.some((name) => typeof name !== "string"))) throw new Error(`${path}.required must be an array of strings`);
615
+ if (rule.additionalProperties !== void 0 && typeof rule.additionalProperties !== "boolean") throw new Error(`${path}.additionalProperties must be a boolean`);
616
+ if (rule.items !== void 0) {
617
+ const items = asRecord$2(rule.items);
618
+ if (!items) throw new Error(`${path}.items must be a JSON Schema object`);
619
+ assertSchemaNode(items, `${path}.items`);
620
+ }
621
+ if (rule.enum !== void 0 && (!Array.isArray(rule.enum) || rule.enum.length === 0)) throw new Error(`${path}.enum must be a non-empty array`);
622
+ for (const keyword of ["minimum", "maximum"]) if (rule[keyword] !== void 0 && (typeof rule[keyword] !== "number" || !Number.isFinite(rule[keyword]))) throw new Error(`${path}.${keyword} must be a finite number`);
623
+ for (const keyword of [
624
+ "minLength",
625
+ "maxLength",
626
+ "minItems",
627
+ "maxItems"
628
+ ]) {
629
+ const value = rule[keyword];
630
+ if (value !== void 0 && (typeof value !== "number" || !Number.isInteger(value) || value < 0)) throw new Error(`${path}.${keyword} must be a non-negative integer`);
631
+ }
632
+ if (typeof rule.minimum === "number" && typeof rule.maximum === "number" && rule.minimum > rule.maximum) throw new Error(`${path}.minimum cannot exceed maximum`);
633
+ if (typeof rule.minLength === "number" && typeof rule.maxLength === "number" && rule.minLength > rule.maxLength) throw new Error(`${path}.minLength cannot exceed maxLength`);
634
+ if (typeof rule.minItems === "number" && typeof rule.maxItems === "number" && rule.minItems > rule.maxItems) throw new Error(`${path}.minItems cannot exceed maxItems`);
635
+ }
636
+ function matchesType(value, type) {
637
+ switch (type) {
638
+ case "object": return asRecord$2(value) !== null;
639
+ case "array": return Array.isArray(value);
640
+ case "string": return typeof value === "string";
641
+ case "number": return typeof value === "number" && Number.isFinite(value);
642
+ case "integer": return typeof value === "number" && Number.isInteger(value);
643
+ case "boolean": return typeof value === "boolean";
644
+ case "null": return value === null;
645
+ default: return false;
646
+ }
647
+ }
648
+ function deepEqual(left, right) {
649
+ return JSON.stringify(left) === JSON.stringify(right);
650
+ }
651
+ function asRecord$2(value) {
652
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
653
+ }
654
+ var FunctionDispatcher = class {
655
+ constructor(runtime, options) {
656
+ this.runtime = runtime;
657
+ this.options = options;
658
+ this.queue = [];
659
+ this.running = /* @__PURE__ */ new Map();
660
+ this.byThread = /* @__PURE__ */ new Map();
661
+ this.starting = null;
662
+ this.step = 0;
663
+ this.onAfterExecute = () => this.afterStep();
664
+ this.onStopAll = () => this.cancelAll("The project was stopped.");
665
+ this.timeoutMs = options.timeoutMs ?? 3e4;
666
+ this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
667
+ this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
668
+ runtime.on("AFTER_EXECUTE", this.onAfterExecute);
669
+ runtime.on("PROJECT_STOP_ALL", this.onStopAll);
670
+ }
671
+ /**
672
+ * Starts a named function. `caller` is the thread of the block making the call, if any; it is used
673
+ * to detect reentrant calls.
674
+ */
675
+ invoke(name, args, caller, returnSchema) {
676
+ if (!this.options.knownNames().has(name)) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${name}`));
677
+ const parentChain = caller ? this.byThread.get(caller)?.chain ?? [] : [];
678
+ if (parentChain.includes(name)) return Promise.reject(/* @__PURE__ */ new Error(`Reentrant call: ${[...parentChain, name].join(" -> ")}. Use a custom block for recursion.`));
679
+ return new Promise((resolve, reject) => {
680
+ const invocation = {
681
+ name,
682
+ args,
683
+ ...returnSchema === void 0 ? {} : { returnSchema },
684
+ chain: [...parentChain, name],
685
+ resolve,
686
+ reject,
687
+ thread: null,
688
+ startedAtStep: -1,
689
+ timer: null,
690
+ settled: false
691
+ };
692
+ invocation.timer = this.setTimer(() => this.settle(invocation, /* @__PURE__ */ new Error(`Function ${name} timed out.`)), this.timeoutMs);
693
+ this.queue.push(invocation);
694
+ this.pump();
695
+ });
696
+ }
697
+ /** Hat predicate: true only for the script that should run the invocation being started. */
698
+ matchHat(name, thread) {
699
+ const invocation = this.starting;
700
+ if (!invocation || !thread || invocation.name !== name.trim()) return false;
701
+ invocation.thread = thread;
702
+ this.byThread.set(thread, invocation);
703
+ this.starting = null;
704
+ return true;
705
+ }
706
+ argumentsFor(thread) {
707
+ const invocation = thread ? this.byThread.get(thread) : void 0;
708
+ if (!invocation) throw new Error("This block can only be used inside a running function.");
709
+ return invocation.args;
710
+ }
711
+ returnFrom(thread, value) {
712
+ const invocation = thread ? this.byThread.get(thread) : void 0;
713
+ if (!invocation) throw new Error("return can only be used inside a running function.");
714
+ this.settleWithValue(invocation, value);
715
+ }
716
+ cancelAll(reason) {
717
+ for (const invocation of [...this.queue, ...this.running.values()]) this.settle(invocation, new Error(reason));
718
+ if (this.starting) this.settle(this.starting, new Error(reason));
719
+ }
720
+ get pendingCount() {
721
+ return this.queue.length + this.running.size;
722
+ }
723
+ release() {
724
+ this.cancelAll("Named functions were released.");
725
+ this.runtime.off?.("AFTER_EXECUTE", this.onAfterExecute);
726
+ this.runtime.off?.("PROJECT_STOP_ALL", this.onStopAll);
727
+ }
728
+ pump() {
729
+ if (this.starting) return;
730
+ const index = this.queue.findIndex((invocation) => !this.running.has(invocation.name));
731
+ if (index < 0) return;
732
+ const [invocation] = this.queue.splice(index, 1);
733
+ if (!invocation) return;
734
+ this.running.set(invocation.name, invocation);
735
+ this.starting = invocation;
736
+ invocation.startedAtStep = this.step;
737
+ this.runtime.startHats(this.options.hatOpcode);
738
+ }
739
+ afterStep() {
740
+ this.step += 1;
741
+ const starting = this.starting;
742
+ if (starting && this.step - starting.startedAtStep > 2) this.settle(starting, /* @__PURE__ */ new Error(`Function ${starting.name} did not start. Is its script already running?`));
743
+ for (const invocation of this.running.values()) if (invocation.thread && !this.runtime.threads.includes(invocation.thread)) this.settleWithValue(invocation, null);
744
+ this.pump();
745
+ }
746
+ settle(invocation, error, value) {
747
+ if (invocation.settled) return;
748
+ invocation.settled = true;
749
+ this.clearTimer(invocation.timer);
750
+ const queued = this.queue.indexOf(invocation);
751
+ if (queued >= 0) this.queue.splice(queued, 1);
752
+ if (this.running.get(invocation.name) === invocation) this.running.delete(invocation.name);
753
+ if (invocation.thread) this.byThread.delete(invocation.thread);
754
+ if (this.starting === invocation) this.starting = null;
755
+ if (error) invocation.reject(error);
756
+ else invocation.resolve(value);
757
+ }
758
+ settleWithValue(invocation, value) {
759
+ if (invocation.returnSchema !== void 0) {
760
+ const errors = validateAgainstSchema(value, invocation.returnSchema);
761
+ if (errors.length > 0) {
762
+ this.settle(invocation, /* @__PURE__ */ new Error(`Invalid return value for ${invocation.name}: ${errors.join("; ")}`));
763
+ return;
764
+ }
765
+ }
766
+ this.settle(invocation, null, value);
767
+ }
768
+ };
769
+ /** Resolves a dotted path such as `items.0.name` inside parsed JSON arguments. */
770
+ function readArgumentPath(args, path) {
771
+ const trimmed = path.trim();
772
+ if (trimmed.length === 0) return args;
773
+ let current = args;
774
+ for (const segment of trimmed.split(".")) {
775
+ if (current === null || typeof current !== "object") return void 0;
776
+ current = current[segment];
777
+ }
778
+ return current;
779
+ }
780
+ /** Converts a value for a Scratch reporter: objects and arrays become JSON text. */
781
+ function toScratchValue(value) {
782
+ if (value === void 0 || value === null) return "";
783
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
784
+ return JSON.stringify(value);
785
+ }
786
+ /** Parses block text: JSON text becomes JSON, anything else stays a string. */
787
+ function parseJsonOrText(text) {
788
+ const trimmed = text.trim();
789
+ if (trimmed.length === 0) return "";
790
+ try {
791
+ return JSON.parse(trimmed);
792
+ } catch {
793
+ return text;
794
+ }
795
+ }
796
+ function readLiteralInput(block, blocks, inputName) {
797
+ const input = block.inputs?.[inputName];
798
+ if (!input) throw new Error(`${inputName} is missing`);
799
+ if (input.block && input.block !== input.shadow) throw new Error(`${inputName} must be literal text, not a reporter block`);
800
+ const shadowId = input.shadow ?? input.block;
801
+ const shadow = shadowId ? blocks[shadowId] : void 0;
802
+ const field = shadow?.fields ? Object.values(shadow.fields)[0] : void 0;
803
+ if (!field) throw new Error(`${inputName} is missing`);
804
+ return String(field.value ?? "");
805
+ }
806
+ function readOptionalLiteralInput(block, blocks, inputName) {
807
+ if (!block.inputs?.[inputName]) return void 0;
808
+ return readLiteralInput(block, blocks, inputName);
809
+ }
810
+ /** Reads a dropdown that does not accept reporters; the VM stores it as a field on the block. */
811
+ function readMenuField(block, fieldName) {
812
+ const field = block.fields?.[fieldName];
813
+ if (!field) throw new Error(`${fieldName} is missing`);
814
+ return String(field.value ?? "");
815
+ }
816
+ /**
817
+ * The blocks stacked under `block`, in order. Shadow blocks are skipped, and a `next` chain that
818
+ * loops or leaves the container stops the walk instead of hanging.
819
+ */
820
+ function stackBelow(block, blocks) {
821
+ const stack = [];
822
+ const seen = /* @__PURE__ */ new Set([block.id]);
823
+ let nextId = block.next ?? null;
824
+ while (nextId) {
825
+ if (seen.has(nextId)) break;
826
+ seen.add(nextId);
827
+ const next = blocks[nextId];
828
+ if (!next) break;
829
+ if (next.shadow !== true) stack.push(next);
830
+ nextId = next.next ?? null;
831
+ }
832
+ return stack;
833
+ }
834
+ var FUNCTION_NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/u;
835
+ var MAX_DESCRIPTION_LENGTH = 1024;
836
+ /**
837
+ * Reads every `define function` hat in the project. NAME, DESCRIPTION, and SCHEMA are part of the
838
+ * tool contract sent to the model before any script runs, so they must be literal text.
839
+ */
840
+ function scanFunctionDefinitions(targets, hatOpcode, options = {}) {
841
+ const functions = [];
842
+ const errors = [];
843
+ const seen = /* @__PURE__ */ new Map();
844
+ for (const target of targets) {
845
+ if (target.isOriginal === false) continue;
846
+ const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
847
+ const blocks = target.blocks._blocks;
848
+ for (const block of Object.values(blocks)) {
849
+ if (block.opcode !== hatOpcode || block.topLevel === false) continue;
850
+ const where = `${targetName} (block ${block.id})`;
851
+ try {
852
+ const definition = readDefinition(block, blocks, targetName, options);
853
+ const previous = seen.get(definition.name);
854
+ if (previous !== void 0) throw new Error(`function "${definition.name}" is already defined in ${previous}`);
855
+ seen.set(definition.name, where);
856
+ functions.push(definition);
857
+ } catch (error) {
858
+ errors.push(`${where}: ${error instanceof Error ? error.message : String(error)}`);
859
+ }
860
+ }
861
+ }
862
+ functions.sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
863
+ return {
864
+ functions,
865
+ errors
866
+ };
867
+ }
868
+ function toFunctionTools(functions) {
869
+ return functions.filter((definition) => definition.exportAs === "tool").map((definition) => ({
870
+ type: "function",
871
+ name: definition.name,
872
+ description: definition.description,
873
+ parameters: definition.parameters
874
+ }));
875
+ }
876
+ function readDefinition(block, blocks, targetName, options) {
877
+ const name = readLiteralInput(block, blocks, "NAME").trim();
878
+ if (!FUNCTION_NAME_PATTERN.test(name)) throw new Error("function name must be 1-64 letters, digits, \"_\" or \"-\"");
879
+ const description = readLiteralInput(block, blocks, "DESCRIPTION").trim();
880
+ if (description.length > 1024) throw new Error(`description must be at most ${MAX_DESCRIPTION_LENGTH} characters`);
881
+ const parameters = parseSchema(readLiteralInput(block, blocks, "SCHEMA"), "args schema", true, options);
882
+ const returnsText = readOptionalLiteralInput(block, blocks, "RETURNS");
883
+ const returns = returnsText === void 0 || returnsText.trim() === "" ? void 0 : parseSchema(returnsText, "returns schema", false, options);
884
+ const exportAs = String(block.fields?.EXPORT?.value ?? "none") === "tool" ? "tool" : "none";
885
+ if (exportAs === "tool" && description.length === 0) throw new Error("a function exported as a tool needs a description");
886
+ return {
887
+ name,
888
+ description,
889
+ parameters,
890
+ ...returns ? { returns } : {},
891
+ exportAs,
892
+ targetName,
893
+ blockId: block.id
894
+ };
895
+ }
896
+ /**
897
+ * SCHEMA and RETURNS hold either JSON Schema text or, when the consumer supports it, the name of a
898
+ * `define schema` hat. Text that is not JSON is treated as a name, so existing JSON projects are
899
+ * unaffected.
900
+ */
901
+ function parseSchema(text, label, requireObjectType, options) {
902
+ let value;
903
+ try {
904
+ value = JSON.parse(text);
905
+ } catch {
906
+ if (options.resolveSchema) return resolveNamed(text, label, requireObjectType, options.resolveSchema);
907
+ throw new Error(`${label} must be valid JSON`);
908
+ }
909
+ if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${label} must be a JSON object`);
910
+ const schema = value;
911
+ if (requireObjectType && schema.type !== "object") throw new Error("args schema must have \"type\": \"object\"");
912
+ validateSchemaDefinition(schema, label);
913
+ return schema;
914
+ }
915
+ function resolveNamed(text, label, requireObjectType, resolveSchema) {
916
+ const schema = resolveSchema(text, label);
917
+ if (requireObjectType && schema.type !== "object") throw new Error("args schema must have \"type\": \"object\"");
918
+ validateSchemaDefinition(schema, label);
919
+ return schema;
920
+ }
921
+ /**
922
+ * Lowers `define schema` block stacks into the canonical JSON Schema objects consumed by
923
+ * `validateAgainstSchema`.
924
+ *
925
+ * The builder is declarative: it reads literal block fields before any script runs and never
926
+ * executes the blocks it reads. Every schema it produces is checked with `validateSchemaDefinition`,
927
+ * so a block-built schema and a hand-written JSON one go through one validator and one diagnostic
928
+ * format.
929
+ */
930
+ var SCHEMA_NAME_PATTERN = /^[A-Za-z0-9_.-]{1,128}$/u;
931
+ /** Complete SemVer, as the named-data schema identity contract requires. */
932
+ var SCHEMA_VERSION_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u;
933
+ var PROPERTY_TYPES = {
934
+ string: () => ({ type: "string" }),
935
+ number: () => ({ type: "number" }),
936
+ integer: () => ({ type: "integer" }),
937
+ boolean: () => ({ type: "boolean" }),
938
+ null: () => ({ type: "null" }),
939
+ object: () => ({ type: "object" }),
940
+ array: () => ({ type: "array" }),
941
+ "array of string": () => ({
942
+ type: "array",
943
+ items: { type: "string" }
944
+ }),
945
+ "array of number": () => ({
946
+ type: "array",
947
+ items: { type: "number" }
948
+ }),
949
+ "array of integer": () => ({
950
+ type: "array",
951
+ items: { type: "integer" }
952
+ }),
953
+ "array of boolean": () => ({
954
+ type: "array",
955
+ items: { type: "boolean" }
956
+ }),
957
+ "array of object": () => ({
958
+ type: "array",
959
+ items: { type: "object" }
960
+ })
961
+ };
962
+ var CONSTRAINT_KEYWORDS = /* @__PURE__ */ new Set([
963
+ "minimum",
964
+ "maximum",
965
+ "minLength",
966
+ "maxLength",
967
+ "minItems",
968
+ "maxItems"
969
+ ]);
970
+ /** Keywords that describe the array itself rather than each of its items. */
971
+ var ARRAY_LEVEL_KEYWORDS = /* @__PURE__ */ new Set(["minItems", "maxItems"]);
972
+ /**
973
+ * Reads every `define schema` hat in the project and lowers it to a canonical JSON Schema.
974
+ * A hat that cannot be read is reported in `errors` and left out of `schemas`.
975
+ */
976
+ function scanSchemaDefinitions(targets, opcodes) {
977
+ const drafts = [];
978
+ const errors = [];
979
+ const seen = /* @__PURE__ */ new Map();
980
+ for (const target of targets) {
981
+ if (target.isOriginal === false) continue;
982
+ const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
983
+ const blocks = target.blocks._blocks;
984
+ for (const block of Object.values(blocks)) {
985
+ if (block.opcode !== opcodes.hat || block.topLevel === false) continue;
986
+ const where = `${targetName} (block ${block.id})`;
987
+ try {
988
+ const draft = readDraft(block, blocks, opcodes, targetName);
989
+ const key = `${draft.name}@${draft.version}`;
990
+ const previous = seen.get(key);
991
+ if (previous !== void 0) throw new Error(`schema "${key}" is already defined in ${previous}`);
992
+ seen.set(key, where);
993
+ drafts.push(draft);
994
+ } catch (error) {
995
+ errors.push(`${where}: ${messageOf$1(error)}`);
996
+ }
997
+ }
998
+ }
999
+ const byKey = new Map(drafts.map((draft) => [`${draft.name}@${draft.version}`, draft]));
1000
+ const schemas = [];
1001
+ for (const draft of drafts) try {
1002
+ const schema = lowerDraft(draft, byKey, []);
1003
+ validateSchemaDefinition(schema, `schema ${draft.name}`);
1004
+ schemas.push({
1005
+ name: draft.name,
1006
+ version: draft.version,
1007
+ schema,
1008
+ targetName: draft.targetName,
1009
+ blockId: draft.blockId
1010
+ });
1011
+ } catch (error) {
1012
+ errors.push(`${draft.targetName} (block ${draft.blockId}): ${messageOf$1(error)}`);
1013
+ }
1014
+ schemas.sort((left, right) => compare(`${left.name}@${left.version}`, `${right.name}@${right.version}`));
1015
+ return {
1016
+ schemas,
1017
+ errors
1018
+ };
1019
+ }
1020
+ /** Parses `Name` or `Name@1.2.3`. */
1021
+ function parseSchemaReference(text, label) {
1022
+ const trimmed = text.trim();
1023
+ const at = trimmed.lastIndexOf("@");
1024
+ const name = at > 0 ? trimmed.slice(0, at) : trimmed;
1025
+ const version = at > 0 ? trimmed.slice(at + 1) : void 0;
1026
+ if (!SCHEMA_NAME_PATTERN.test(name)) throw new Error(`${label} must be 1-128 letters, digits, ".", "_" or "-"`);
1027
+ if (version !== void 0 && !SCHEMA_VERSION_PATTERN.test(version)) throw new Error(`${label} version "${version}" must be a complete SemVer value such as 1.0.0`);
1028
+ return version === void 0 ? { name } : {
1029
+ name,
1030
+ version
1031
+ };
1032
+ }
1033
+ /**
1034
+ * Resolves a `Name` or `Name@1.2.3` reference against scanned schemas. An unknown name or version
1035
+ * fails; a name with several versions and no pinned version is ambiguous rather than guessed.
1036
+ */
1037
+ function resolveSchemaReference(schemas, text, label) {
1038
+ const reference = parseSchemaReference(text, label);
1039
+ const matches = schemas.filter((candidate) => candidate.name === reference.name);
1040
+ if (matches.length === 0) throw new Error(`${label} refers to unknown schema "${reference.name}"`);
1041
+ if (reference.version !== void 0) {
1042
+ const exact = matches.find((candidate) => candidate.version === reference.version);
1043
+ if (!exact) {
1044
+ const available = matches.map((candidate) => candidate.version).join(", ");
1045
+ throw new Error(`${label} refers to unknown version "${reference.version}" of schema "${reference.name}" (defined: ${available})`);
1046
+ }
1047
+ return exact.schema;
1048
+ }
1049
+ if (matches.length > 1) {
1050
+ const available = matches.map((candidate) => candidate.version).join(", ");
1051
+ throw new Error(`${label} refers to schema "${reference.name}", which has several versions (${available}); pin one as "${reference.name}@${matches[0]?.version}"`);
1052
+ }
1053
+ return matches[0].schema;
1054
+ }
1055
+ function readDraft(hat, blocks, opcodes, targetName) {
1056
+ const name = readLiteralInput(hat, blocks, "NAME").trim();
1057
+ if (!SCHEMA_NAME_PATTERN.test(name)) throw new Error("schema name must be 1-128 letters, digits, \".\", \"_\" or \"-\"");
1058
+ const version = readLiteralInput(hat, blocks, "VERSION").trim();
1059
+ if (!SCHEMA_VERSION_PATTERN.test(version)) throw new Error(`schema version "${version}" must be a complete SemVer value such as 1.0.0`);
1060
+ const draft = {
1061
+ name,
1062
+ version,
1063
+ properties: [],
1064
+ targetName,
1065
+ blockId: hat.id
1066
+ };
1067
+ const declared = /* @__PURE__ */ new Set();
1068
+ for (const block of stackBelow(hat, blocks)) if (block.opcode === opcodes.property) draft.properties.push(readProperty(block, blocks, declared));
1069
+ else if (block.opcode === opcodes.propertySchema || block.opcode === opcodes.propertyArraySchema) draft.properties.push(readReferenceProperty(block, blocks, declared, block.opcode === opcodes.propertyArraySchema));
1070
+ else if (block.opcode === opcodes.constraint) applyConstraint(block, blocks, draft);
1071
+ else if (block.opcode === opcodes.allowedValues) applyAllowedValues(block, blocks, draft);
1072
+ else if (block.opcode === opcodes.additionalProperties) {
1073
+ if (draft.additionalProperties !== void 0) throw new Error("\"allow undeclared properties\" is used more than once in this schema");
1074
+ draft.additionalProperties = readMenuField(block, "ALLOW") === "yes";
1075
+ } else throw new Error(`${block.opcode} cannot be used inside a "define schema" stack`);
1076
+ return draft;
1077
+ }
1078
+ function readProperty(block, blocks, declared) {
1079
+ const name = readPropertyName(block, blocks, declared);
1080
+ const typeName = readMenuField(block, "TYPE");
1081
+ const build = PROPERTY_TYPES[typeName];
1082
+ if (!build) throw new Error(`property "${name}" has unsupported type "${typeName}"`);
1083
+ return {
1084
+ name,
1085
+ required: readPresence(block),
1086
+ schema: build()
1087
+ };
1088
+ }
1089
+ function readReferenceProperty(block, blocks, declared, asArray) {
1090
+ const name = readPropertyName(block, blocks, declared);
1091
+ const reference = parseSchemaReference(readLiteralInput(block, blocks, "SCHEMA"), `property "${name}" schema`);
1092
+ return {
1093
+ name,
1094
+ required: readPresence(block),
1095
+ reference,
1096
+ referenceIsArray: asArray
1097
+ };
1098
+ }
1099
+ function readPropertyName(block, blocks, declared) {
1100
+ const name = readLiteralInput(block, blocks, "NAME").trim();
1101
+ if (name.length === 0) throw new Error("a property needs a name");
1102
+ if (declared.has(name)) throw new Error(`property "${name}" is declared more than once`);
1103
+ declared.add(name);
1104
+ return name;
1105
+ }
1106
+ function readPresence(block) {
1107
+ return readMenuField(block, "PRESENCE") === "required";
1108
+ }
1109
+ function applyConstraint(block, blocks, draft) {
1110
+ const keyword = readMenuField(block, "KEYWORD");
1111
+ if (!CONSTRAINT_KEYWORDS.has(keyword)) throw new Error(`unsupported limit "${keyword}"`);
1112
+ const target = constraintTarget(draft, `limit "${keyword}"`, keyword);
1113
+ const raw = readLiteralInput(block, blocks, "VALUE").trim();
1114
+ const value = Number(raw);
1115
+ if (raw.length === 0 || !Number.isFinite(value)) throw new Error(`limit "${keyword}" needs a number, not "${raw}"`);
1116
+ if (target[keyword] !== void 0) throw new Error(`limit "${keyword}" is set more than once on property "${lastProperty(draft).name}"`);
1117
+ target[keyword] = value;
1118
+ }
1119
+ function applyAllowedValues(block, blocks, draft) {
1120
+ const target = constraintTarget(draft, "\"allowed values\"", "enum");
1121
+ const property = lastProperty(draft);
1122
+ if (target.enum !== void 0) throw new Error(`"allowed values" is set more than once on property "${property.name}"`);
1123
+ const values = parseAllowedValues(readLiteralInput(block, blocks, "VALUES"), property.name);
1124
+ const type = target.type;
1125
+ if (type !== void 0) {
1126
+ for (const value of values) if (validateAgainstSchema(value, { type }).length > 0) throw new Error(`allowed value ${JSON.stringify(value)} does not match the declared type of property "${property.name}"`);
1127
+ }
1128
+ target.enum = values;
1129
+ }
1130
+ /**
1131
+ * Where a limit or an allowed-values list belongs. For an array property the item keywords describe
1132
+ * each item, while `minItems`/`maxItems` describe the array.
1133
+ */
1134
+ function constraintTarget(draft, label, keyword) {
1135
+ const property = lastProperty(draft, label);
1136
+ if (!property.schema) throw new Error(`${label} cannot be used on property "${property.name}", which uses another schema`);
1137
+ if (!(property.schema.type === "array")) {
1138
+ if (ARRAY_LEVEL_KEYWORDS.has(keyword)) throw new Error(`${label} can only be used on an array property, not "${property.name}"`);
1139
+ return property.schema;
1140
+ }
1141
+ if (ARRAY_LEVEL_KEYWORDS.has(keyword)) return property.schema;
1142
+ const items = property.schema.items;
1143
+ if (items === void 0 || typeof items !== "object" || items === null || Array.isArray(items)) throw new Error(`${label} needs property "${property.name}" to declare an item type, such as "array of number"`);
1144
+ return items;
1145
+ }
1146
+ function lastProperty(draft, label = "this block") {
1147
+ const property = draft.properties.at(-1);
1148
+ if (!property) throw new Error(`${label} must come after a property block`);
1149
+ return property;
1150
+ }
1151
+ /** JSON array text, or a comma-separated list of strings. */
1152
+ function parseAllowedValues(text, propertyName) {
1153
+ const trimmed = text.trim();
1154
+ if (trimmed.length === 0) throw new Error(`"allowed values" for property "${propertyName}" is empty`);
1155
+ if (trimmed.startsWith("[")) {
1156
+ let parsed;
1157
+ try {
1158
+ parsed = JSON.parse(trimmed);
1159
+ } catch {
1160
+ throw new Error(`"allowed values" for property "${propertyName}" must be valid JSON`);
1161
+ }
1162
+ if (!Array.isArray(parsed) || parsed.length === 0) throw new Error(`"allowed values" for property "${propertyName}" must be a non-empty JSON array`);
1163
+ return parsed;
1164
+ }
1165
+ const values = trimmed.split(",").map((value) => value.trim()).filter((value) => value.length > 0);
1166
+ if (values.length === 0) throw new Error(`"allowed values" for property "${propertyName}" is empty`);
1167
+ return values;
1168
+ }
1169
+ function lowerDraft(draft, byKey, stack) {
1170
+ const key = `${draft.name}@${draft.version}`;
1171
+ if (stack.includes(key)) throw new Error(`schema reference cycle: ${[...stack, key].join(" -> ")}`);
1172
+ if (stack.length >= 16) throw new Error(`schema references nest deeper than 16 levels`);
1173
+ const nextStack = [...stack, key];
1174
+ const properties = {};
1175
+ const required = [];
1176
+ for (const property of draft.properties) {
1177
+ properties[property.name] = property.reference ? lowerReference(property, draft, byKey, nextStack) : property.schema;
1178
+ if (property.required) required.push(property.name);
1179
+ }
1180
+ return {
1181
+ type: "object",
1182
+ properties,
1183
+ ...required.length > 0 ? { required } : {},
1184
+ ...draft.additionalProperties === void 0 ? {} : { additionalProperties: draft.additionalProperties }
1185
+ };
1186
+ }
1187
+ function lowerReference(property, draft, byKey, stack) {
1188
+ const reference = property.reference;
1189
+ const lowered = lowerDraft(findDraft(byKey, reference, `property "${property.name}" of schema ${draft.name}`), byKey, stack);
1190
+ return property.referenceIsArray ? {
1191
+ type: "array",
1192
+ items: lowered
1193
+ } : lowered;
1194
+ }
1195
+ function findDraft(byKey, reference, label) {
1196
+ if (reference.version !== void 0) {
1197
+ const exact = byKey.get(`${reference.name}@${reference.version}`);
1198
+ if (!exact) throw new Error(`${label} refers to unknown schema "${reference.name}@${reference.version}"`);
1199
+ return exact;
1200
+ }
1201
+ const matches = [...byKey.values()].filter((candidate) => candidate.name === reference.name);
1202
+ if (matches.length === 0) throw new Error(`${label} refers to unknown schema "${reference.name}"`);
1203
+ if (matches.length > 1) {
1204
+ const available = matches.map((candidate) => candidate.version).join(", ");
1205
+ throw new Error(`${label} refers to schema "${reference.name}", which has several versions (${available}); pin one as "${reference.name}@${matches[0]?.version}"`);
1206
+ }
1207
+ return matches[0];
1208
+ }
1209
+ function compare(left, right) {
1210
+ return left < right ? -1 : left > right ? 1 : 0;
1211
+ }
1212
+ function messageOf$1(error) {
1213
+ return error instanceof Error ? error.message : String(error);
1214
+ }
1215
+ /**
1216
+ * Composition API: named functions without TurboWarp block definitions.
1217
+ *
1218
+ * A consumer extension owns its `define function` hat (and its opcode) and forwards the hat
1219
+ * predicate and the argument/return blocks here. Importing this module does not register a
1220
+ * TurboWarp extension and does not touch the `Scratch` global.
1221
+ */
1222
+ function createNamedFunctions(options) {
1223
+ return new NamedFunctionsImpl(options);
1224
+ }
1225
+ var NamedFunctionsImpl = class {
1226
+ constructor(options) {
1227
+ this.options = options;
1228
+ this.promises = /* @__PURE__ */ new Map();
1229
+ this.waiting = [];
1230
+ this.inFlight = 0;
1231
+ this.nextPromiseId = 1;
1232
+ this.maxInFlight = Math.max(1, options.maxInFlight ?? 8);
1233
+ this.maxRetained = Math.max(1, options.maxRetainedPromises ?? 256);
1234
+ const dispatcherOptions = {
1235
+ hatOpcode: options.functionHatOpcode,
1236
+ knownNames: () => new Set(this.scan().functions.map((definition) => definition.name)),
1237
+ ...options.timeoutMs === void 0 ? {} : { timeoutMs: options.timeoutMs },
1238
+ ...options.setTimer ? { setTimer: options.setTimer } : {},
1239
+ ...options.clearTimer ? { clearTimer: options.clearTimer } : {}
1240
+ };
1241
+ this.dispatcher = new FunctionDispatcher(options.runtime, dispatcherOptions);
1242
+ options.runtime.on("PROJECT_STOP_ALL", () => this.promises.clear());
1243
+ }
1244
+ scan() {
1245
+ if (!this.options.schemaBlockOpcodes) return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode);
1246
+ const schemas = this.schemas().schemas;
1247
+ return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode, { resolveSchema: (reference, label) => resolveSchemaReference(schemas, reference, label) });
1248
+ }
1249
+ schemas() {
1250
+ const opcodes = this.options.schemaBlockOpcodes;
1251
+ if (!opcodes) return {
1252
+ schemas: [],
1253
+ errors: []
1254
+ };
1255
+ return scanSchemaDefinitions(this.options.runtime.targets, opcodes);
1256
+ }
1257
+ tools() {
1258
+ const errors = [...this.schemas().errors, ...this.scan().errors];
1259
+ if (errors.length > 0) throw new Error(`Invalid function definitions: ${errors.join("; ")}`);
1260
+ return toFunctionTools(this.scan().functions);
1261
+ }
1262
+ isDefined(name) {
1263
+ return this.scan().functions.some((definition) => definition.name === name.trim());
1264
+ }
1265
+ call(name, args, options = {}) {
1266
+ const trimmed = name.trim();
1267
+ const definition = this.scan().functions.find((candidate) => candidate.name === trimmed);
1268
+ if (!definition) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${trimmed}`));
1269
+ if (options.exportedOnly && definition.exportAs !== "tool") return Promise.reject(/* @__PURE__ */ new Error(`Function ${trimmed} is not exported as a tool.`));
1270
+ const errors = validateAgainstSchema(args, definition.parameters);
1271
+ if (errors.length > 0) return Promise.reject(/* @__PURE__ */ new Error(`Invalid arguments for ${trimmed}: ${errors.join("; ")}`));
1272
+ return this.dispatcher.invoke(trimmed, args, options.caller, definition.returns);
1273
+ }
1274
+ start(name, args, options = {}) {
1275
+ const id = `p_${this.nextPromiseId++}`;
1276
+ const promise = this.acquireSlot().then(() => this.call(name, args, options).finally(() => this.releaseSlot()));
1277
+ const entry = {
1278
+ promise,
1279
+ settled: false
1280
+ };
1281
+ promise.then(() => entry.settled = true, () => entry.settled = true);
1282
+ this.promises.set(id, entry);
1283
+ this.evictSettled();
1284
+ return { $promise: id };
1285
+ }
1286
+ await(ref) {
1287
+ const entry = this.lookup(ref);
1288
+ return entry ? entry.promise : Promise.reject(/* @__PURE__ */ new Error("Unknown or expired promise reference."));
1289
+ }
1290
+ async awaitAll(refs) {
1291
+ if (!Array.isArray(refs)) throw new Error("await all needs a JSON array of promise references.");
1292
+ return (await Promise.allSettled(refs.map((ref) => this.await(ref)))).map((result) => result.status === "fulfilled" ? result.value : { $error: messageOf$2(result.reason) });
1293
+ }
1294
+ isSettled(ref) {
1295
+ return this.lookup(ref)?.settled ?? false;
1296
+ }
1297
+ matchHat(name, thread) {
1298
+ return this.dispatcher.matchHat(name, thread);
1299
+ }
1300
+ argumentsFor(thread) {
1301
+ return this.dispatcher.argumentsFor(thread);
1302
+ }
1303
+ returnFrom(thread, value) {
1304
+ this.dispatcher.returnFrom(thread, value);
1305
+ }
1306
+ cancelAll(reason) {
1307
+ this.dispatcher.cancelAll(reason);
1308
+ }
1309
+ release() {
1310
+ this.dispatcher.release();
1311
+ this.promises.clear();
1312
+ }
1313
+ lookup(ref) {
1314
+ const id = typeof ref === "object" && ref !== null && typeof ref.$promise === "string" ? ref.$promise : void 0;
1315
+ return id ? this.promises.get(id) : void 0;
1316
+ }
1317
+ acquireSlot() {
1318
+ if (this.inFlight < this.maxInFlight) {
1319
+ this.inFlight += 1;
1320
+ return Promise.resolve();
1321
+ }
1322
+ return new Promise((resolve) => this.waiting.push(() => {
1323
+ this.inFlight += 1;
1324
+ resolve();
1325
+ }));
1326
+ }
1327
+ releaseSlot() {
1328
+ this.inFlight -= 1;
1329
+ this.waiting.shift()?.();
1330
+ }
1331
+ evictSettled() {
1332
+ if (this.promises.size <= this.maxRetained) return;
1333
+ for (const [id, entry] of this.promises) {
1334
+ if (this.promises.size <= this.maxRetained) break;
1335
+ if (entry.settled) this.promises.delete(id);
1336
+ }
1337
+ }
1338
+ };
1339
+ function messageOf$2(error) {
1340
+ return error instanceof Error ? error.message : String(error);
1341
+ }
1342
+ //#endregion
1343
+ //#region src/realtime-session.ts
1344
+ /**
1345
+ * Conversation logic over a transport: sending user text, collecting assistant output, and
1346
+ * answering function calls with `function_call_output` followed by one `response.create`.
1347
+ */
1348
+ var RealtimeSession = class {
1349
+ constructor(hooks) {
1350
+ this.hooks = hooks;
1351
+ this.stateValue = "disconnected";
1352
+ this.transport = null;
1353
+ this.generation = 0;
1354
+ }
1355
+ get state() {
1356
+ return this.stateValue;
1357
+ }
1358
+ async open(transport, clientSecret, microphone) {
1359
+ this.close();
1360
+ const generation = ++this.generation;
1361
+ this.transport = transport;
1362
+ this.setState("connecting");
1363
+ try {
1364
+ await transport.connect({
1365
+ clientSecret,
1366
+ microphone,
1367
+ onEvent: (event) => {
1368
+ if (generation === this.generation) this.handleEvent(event);
1369
+ },
1370
+ onClose: (reason) => {
1371
+ if (generation !== this.generation) return;
1372
+ this.transport = null;
1373
+ this.hooks.onError(reason);
1374
+ this.setState("failed");
1375
+ }
1376
+ });
1377
+ } catch (error) {
1378
+ if (generation === this.generation) {
1379
+ this.transport = null;
1380
+ this.setState("failed");
1381
+ }
1382
+ throw error;
1383
+ }
1384
+ if (generation === this.generation) this.setState("connected");
1385
+ }
1386
+ close() {
1387
+ this.generation += 1;
1388
+ const transport = this.transport;
1389
+ this.transport = null;
1390
+ transport?.close();
1391
+ if (this.stateValue !== "disconnected") this.setState("disconnected");
1392
+ }
1393
+ sendText(text) {
1394
+ this.send({
1395
+ type: "conversation.item.create",
1396
+ item: {
1397
+ type: "message",
1398
+ role: "user",
1399
+ content: [{
1400
+ type: "input_text",
1401
+ text
1402
+ }]
1403
+ }
1404
+ });
1405
+ this.send({ type: "response.create" });
1406
+ }
1407
+ send(event) {
1408
+ if (!this.transport || this.stateValue !== "connected") throw new Error("Not connected to Realtime.");
1409
+ this.transport.send(event);
1410
+ }
1411
+ async handleEvent(event) {
1412
+ if (event.type === "error") {
1413
+ this.hooks.onError(describeApiError(event));
1414
+ return;
1415
+ }
1416
+ if (event.type !== "response.done") return;
1417
+ const response = event.response;
1418
+ if (typeof response === "object" && response !== null && "usage" in response) this.hooks.onUsage?.(response.usage);
1419
+ const output = readResponseOutput(event);
1420
+ if (output.text.length > 0) this.hooks.onResponseText(output.text);
1421
+ if (output.calls.length === 0) return;
1422
+ const generation = this.generation;
1423
+ const results = await Promise.all(output.calls.map((call) => this.runCall(call)));
1424
+ if (generation !== this.generation || !this.transport) return;
1425
+ for (const result of results) this.transport.send({
1426
+ type: "conversation.item.create",
1427
+ item: {
1428
+ type: "function_call_output",
1429
+ call_id: result.callId,
1430
+ output: result.output
1431
+ }
1432
+ });
1433
+ this.transport.send({ type: "response.create" });
1434
+ }
1435
+ async runCall(call) {
1436
+ let args;
1437
+ try {
1438
+ args = call.argumentsJson.trim().length === 0 ? {} : JSON.parse(call.argumentsJson);
1439
+ } catch {
1440
+ return {
1441
+ callId: call.callId,
1442
+ output: JSON.stringify({ error: "Arguments were not valid JSON." })
1443
+ };
1444
+ }
1445
+ try {
1446
+ const value = await this.hooks.callFunction(call.name, args);
1447
+ return {
1448
+ callId: call.callId,
1449
+ output: JSON.stringify(value ?? null)
1450
+ };
1451
+ } catch (error) {
1452
+ const message = error instanceof Error ? error.message : String(error);
1453
+ this.hooks.onError(message);
1454
+ return {
1455
+ callId: call.callId,
1456
+ output: JSON.stringify({ error: message })
1457
+ };
1458
+ }
1459
+ }
1460
+ setState(state) {
1461
+ this.stateValue = state;
1462
+ this.hooks.onStateChange?.(state);
1463
+ }
1464
+ };
1465
+ function readResponseOutput(event) {
1466
+ const response = asRecord$1(event.response);
1467
+ const items = Array.isArray(response?.output) ? response.output : [];
1468
+ const texts = [];
1469
+ const calls = [];
1470
+ for (const rawItem of items) {
1471
+ const item = asRecord$1(rawItem);
1472
+ if (!item) continue;
1473
+ if (item.type === "function_call" && typeof item.name === "string" && typeof item.call_id === "string") calls.push({
1474
+ callId: item.call_id,
1475
+ name: item.name,
1476
+ argumentsJson: typeof item.arguments === "string" ? item.arguments : ""
1477
+ });
1478
+ else if (item.type === "message" && Array.isArray(item.content)) for (const rawPart of item.content) {
1479
+ const part = asRecord$1(rawPart);
1480
+ if (typeof part?.text === "string") texts.push(part.text);
1481
+ else if (typeof part?.transcript === "string") texts.push(part.transcript);
1482
+ }
1483
+ }
1484
+ return {
1485
+ text: texts.join(""),
1486
+ calls
1487
+ };
1488
+ }
1489
+ function describeApiError(event) {
1490
+ const error = asRecord$1(event.error);
1491
+ return `${typeof error?.message === "string" ? error.message : "Unknown Realtime API error."}${typeof error?.code === "string" ? ` (${error.code})` : ""}`;
1492
+ }
1493
+ function asRecord$1(value) {
1494
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
1495
+ }
1496
+ //#endregion
1497
+ //#region src/relay-client.ts
1498
+ var LOOPBACK_HOSTNAMES = /* @__PURE__ */ new Set([
1499
+ "127.0.0.1",
1500
+ "localhost",
1501
+ "[::1]"
1502
+ ]);
1503
+ var TOKEN_PATTERN = /^[A-Za-z0-9_-]{20,}$/u;
1504
+ var CLIENT_SECRET_PATTERN = /^ek_[A-Za-z0-9_-]+$/u;
1505
+ /** Accepts only a plain loopback HTTP origin so the relay token can never leave this machine. */
1506
+ function normalizeRelayEndpoint(endpoint) {
1507
+ let url;
1508
+ try {
1509
+ url = new URL(endpoint.trim());
1510
+ } catch {
1511
+ throw new TypeError("Relay endpoint must be a valid URL.");
1512
+ }
1513
+ if (url.protocol !== "http:" || !LOOPBACK_HOSTNAMES.has(url.hostname)) throw new TypeError("Relay endpoint must use HTTP on a loopback hostname.");
1514
+ if (url.username.length > 0 || url.password.length > 0 || url.search.length > 0 || url.hash.length > 0 || url.pathname !== "/" && url.pathname !== "") throw new TypeError("Relay endpoint must contain only its loopback origin.");
1515
+ return url.origin;
1516
+ }
1517
+ async function pairWithRelay(endpoint, code, fetcher = fetch, now = Date.now) {
1518
+ const origin = normalizeRelayEndpoint(endpoint);
1519
+ const trimmed = code.trim();
1520
+ if (!/^\d{8}$/u.test(trimmed)) throw new TypeError("Relay pairing code must contain exactly eight digits.");
1521
+ const record = requireRecord(await requestJson(fetcher, `${origin}/v1/pair`, {
1522
+ method: "POST",
1523
+ headers: { "content-type": "application/json" },
1524
+ body: JSON.stringify({ code: trimmed }),
1525
+ redirect: "error"
1526
+ }), "Relay returned an invalid pairing response.");
1527
+ const token = String(record.token ?? "");
1528
+ if (!TOKEN_PATTERN.test(token)) throw new Error("Relay returned an invalid pairing token.");
1529
+ if (typeof record.expiresAt !== "number" || !Number.isFinite(record.expiresAt) || record.expiresAt <= now()) throw new Error("Relay returned an invalid session expiration.");
1530
+ return {
1531
+ endpoint: origin,
1532
+ token,
1533
+ expiresAt: record.expiresAt
1534
+ };
1535
+ }
1536
+ async function requestClientSecret(session, request, fetcher = fetch, now = Date.now) {
1537
+ if (session.expiresAt <= now()) throw new Error("Relay session has expired. Pair with the local relay again.");
1538
+ const data = requireRecord(requireRecord(await requestJson(fetcher, `${session.endpoint}/v1/openai/realtime/client-secrets`, {
1539
+ method: "POST",
1540
+ headers: {
1541
+ "content-type": "application/json",
1542
+ authorization: `Bearer ${session.token}`
1543
+ },
1544
+ body: JSON.stringify({ session: request }),
1545
+ redirect: "error"
1546
+ }), "Relay returned an invalid client secret response.").data, "Relay returned an invalid client secret response.");
1547
+ const value = String(data.value ?? "");
1548
+ if (!CLIENT_SECRET_PATTERN.test(value)) throw new Error("Relay returned an invalid client secret.");
1549
+ if (typeof data.expiresAt !== "number" || !Number.isFinite(data.expiresAt) || data.expiresAt <= now()) throw new Error("Relay returned an expired client secret.");
1550
+ return {
1551
+ value,
1552
+ expiresAt: data.expiresAt,
1553
+ model: String(data.model ?? "")
1554
+ };
1555
+ }
1556
+ async function requestJson(fetcher, url, init) {
1557
+ const response = await fetcher(url, init);
1558
+ const text = await response.text();
1559
+ let result = null;
1560
+ if (text.length > 0) try {
1561
+ result = JSON.parse(text);
1562
+ } catch {
1563
+ throw new Error(`Relay returned a non-JSON response (${response.status}).`);
1564
+ }
1565
+ if (!response.ok) throw new Error(`Relay request failed (${response.status}): ${errorDetail(result)}`);
1566
+ return result;
1567
+ }
1568
+ function requireRecord(value, message) {
1569
+ if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(message);
1570
+ return value;
1571
+ }
1572
+ function errorDetail(value) {
1573
+ if (typeof value === "object" && value !== null) {
1574
+ const error = value.error;
1575
+ if (typeof error === "object" && error !== null) {
1576
+ const message = error.message;
1577
+ if (typeof message === "string") return message;
1578
+ }
1579
+ }
1580
+ return "unknown error";
1581
+ }
1582
+ //#endregion
1583
+ //#region src/session-config.ts
1584
+ var MAX_INSTRUCTIONS_LENGTH = 16384;
1585
+ var VOICE_PATTERN = /^[a-z0-9_-]{1,32}$/u;
1586
+ var MODEL_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/u;
1587
+ function defaultSessionSettings() {
1588
+ return {
1589
+ model: "",
1590
+ instructions: "",
1591
+ voice: "marin",
1592
+ outputMode: "audio"
1593
+ };
1594
+ }
1595
+ /** Accepts an empty string (relay default) or a model identifier; the relay decides what is allowed. */
1596
+ function normalizeModel(value) {
1597
+ const model = value.trim();
1598
+ if (model.length > 0 && !MODEL_PATTERN.test(model)) throw new TypeError("Model must be a model identifier.");
1599
+ return model;
1600
+ }
1601
+ function normalizeVoice(value) {
1602
+ const voice = value.trim().toLowerCase();
1603
+ if (!VOICE_PATTERN.test(voice)) throw new TypeError("Voice must be a lowercase identifier.");
1604
+ return voice;
1605
+ }
1606
+ function normalizeInstructions(value) {
1607
+ if (value.length > 16384) throw new TypeError(`Instructions must be at most ${MAX_INSTRUCTIONS_LENGTH} characters.`);
1608
+ return value;
1609
+ }
1610
+ function normalizeOutputMode(value) {
1611
+ if (value === "audio" || value === "text") return value;
1612
+ throw new TypeError("Output must be audio or text.");
1613
+ }
1614
+ function buildSessionRequest(settings, tools) {
1615
+ if (tools.length > 32) throw new TypeError(`At most 32 functions can be exported as tools.`);
1616
+ const request = {
1617
+ voice: settings.voice,
1618
+ outputModalities: [settings.outputMode]
1619
+ };
1620
+ if (settings.model.length > 0) request.model = settings.model;
1621
+ if (settings.instructions.length > 0) request.instructions = settings.instructions;
1622
+ if (tools.length > 0) request.tools = tools.map((tool) => ({ ...tool }));
1623
+ return request;
1624
+ }
1625
+ //#endregion
1626
+ //#region src/usage.ts
1627
+ var MODEL_PRICES = {
1628
+ "gpt-realtime-2.1": {
1629
+ textInput: 4,
1630
+ cachedTextInput: .4,
1631
+ textOutput: 24,
1632
+ audioInput: 32,
1633
+ cachedAudioInput: .4,
1634
+ audioOutput: 64
1635
+ },
1636
+ "gpt-realtime-2.1-mini": {
1637
+ textInput: .6,
1638
+ cachedTextInput: .06,
1639
+ textOutput: 2.4,
1640
+ audioInput: 10,
1641
+ cachedAudioInput: .3,
1642
+ audioOutput: 20
1643
+ }
1644
+ };
1645
+ function emptyUsage() {
1646
+ return {
1647
+ responses: 0,
1648
+ inputTokens: 0,
1649
+ outputTokens: 0,
1650
+ cachedInputTokens: 0,
1651
+ textInputTokens: 0,
1652
+ audioInputTokens: 0,
1653
+ cachedTextInputTokens: 0,
1654
+ cachedAudioInputTokens: 0,
1655
+ textOutputTokens: 0,
1656
+ audioOutputTokens: 0,
1657
+ estimatedCostUsd: 0,
1658
+ unpricedResponses: 0
1659
+ };
1660
+ }
1661
+ /** Adds one `response.usage` object to the totals. Unknown or malformed usage counts as zero tokens. */
1662
+ function addUsage(totals, usage, model) {
1663
+ const root = asRecord(usage);
1664
+ if (!root) return totals;
1665
+ const input = asRecord(root.input_token_details);
1666
+ const output = asRecord(root.output_token_details);
1667
+ const cachedDetails = asRecord(input?.cached_tokens_details);
1668
+ const textInput = count(input?.text_tokens);
1669
+ const audioInput = count(input?.audio_tokens);
1670
+ const cached = count(input?.cached_tokens);
1671
+ const cachedText = cachedDetails ? count(cachedDetails.text_tokens) : Math.min(cached, textInput);
1672
+ const cachedAudio = cachedDetails ? count(cachedDetails.audio_tokens) : Math.max(0, cached - cachedText);
1673
+ const textOutput = count(output?.text_tokens);
1674
+ const audioOutput = count(output?.audio_tokens);
1675
+ const next = {
1676
+ responses: totals.responses + 1,
1677
+ inputTokens: totals.inputTokens + count(root.input_tokens),
1678
+ outputTokens: totals.outputTokens + count(root.output_tokens),
1679
+ cachedInputTokens: totals.cachedInputTokens + cached,
1680
+ textInputTokens: totals.textInputTokens + textInput,
1681
+ audioInputTokens: totals.audioInputTokens + audioInput,
1682
+ cachedTextInputTokens: totals.cachedTextInputTokens + cachedText,
1683
+ cachedAudioInputTokens: totals.cachedAudioInputTokens + cachedAudio,
1684
+ textOutputTokens: totals.textOutputTokens + textOutput,
1685
+ audioOutputTokens: totals.audioOutputTokens + audioOutput,
1686
+ estimatedCostUsd: totals.estimatedCostUsd,
1687
+ unpricedResponses: totals.unpricedResponses
1688
+ };
1689
+ const price = MODEL_PRICES[model];
1690
+ if (!price) {
1691
+ next.unpricedResponses += 1;
1692
+ return next;
1693
+ }
1694
+ next.estimatedCostUsd += (Math.max(0, textInput - cachedText) * price.textInput + cachedText * price.cachedTextInput + Math.max(0, audioInput - cachedAudio) * price.audioInput + cachedAudio * price.cachedAudioInput + textOutput * price.textOutput + audioOutput * price.audioOutput) / 1e6;
1695
+ return next;
1696
+ }
1697
+ function count(value) {
1698
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
1699
+ }
1700
+ function asRecord(value) {
1701
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
1702
+ }
1703
+ //#endregion
1704
+ //#region src/webrtc-transport.ts
1705
+ var REALTIME_CALLS_URL = "https://api.openai.com/v1/realtime/calls";
1706
+ var EVENTS_CHANNEL = "oai-events";
1707
+ function browserWebRtcDependencies() {
1708
+ return {
1709
+ createPeerConnection: () => new RTCPeerConnection(),
1710
+ getUserMedia: (constraints) => navigator.mediaDevices.getUserMedia(constraints),
1711
+ createAudioElement: () => {
1712
+ const element = document.createElement("audio");
1713
+ element.autoplay = true;
1714
+ return element;
1715
+ },
1716
+ fetch: (input, init) => fetch(input, init)
1717
+ };
1718
+ }
1719
+ /** Browser-to-OpenAI WebRTC session authenticated with an ephemeral client secret. */
1720
+ var WebRtcTransport = class {
1721
+ constructor(deps = browserWebRtcDependencies()) {
1722
+ this.deps = deps;
1723
+ this.peer = null;
1724
+ this.channel = null;
1725
+ this.microphone = null;
1726
+ this.audio = null;
1727
+ this.closed = false;
1728
+ }
1729
+ async connect(options) {
1730
+ this.closed = false;
1731
+ try {
1732
+ const peer = this.deps.createPeerConnection();
1733
+ this.peer = peer;
1734
+ const audio = this.deps.createAudioElement();
1735
+ this.audio = audio;
1736
+ peer.ontrack = (event) => {
1737
+ audio.srcObject = event.streams[0] ?? null;
1738
+ };
1739
+ if (options.microphone) {
1740
+ const stream = await this.deps.getUserMedia({ audio: true });
1741
+ this.microphone = stream;
1742
+ for (const track of stream.getAudioTracks()) peer.addTrack(track, stream);
1743
+ } else peer.addTransceiver("audio", { direction: "recvonly" });
1744
+ const channel = peer.createDataChannel(EVENTS_CHANNEL);
1745
+ this.channel = channel;
1746
+ channel.onmessage = (message) => {
1747
+ const event = parseEvent(message.data);
1748
+ if (event) options.onEvent(event);
1749
+ };
1750
+ channel.onclose = () => this.handleClose(options, "The Realtime data channel closed.");
1751
+ peer.onconnectionstatechange = () => {
1752
+ if (peer.connectionState === "failed" || peer.connectionState === "closed") this.handleClose(options, `The Realtime connection ${peer.connectionState}.`);
1753
+ };
1754
+ const opened = waitForOpen(channel, this.deps.channelOpenTimeoutMs ?? 15e3);
1755
+ opened.catch(() => void 0);
1756
+ const offer = await peer.createOffer();
1757
+ await peer.setLocalDescription(offer);
1758
+ const response = await this.deps.fetch(REALTIME_CALLS_URL, {
1759
+ method: "POST",
1760
+ body: offer.sdp ?? "",
1761
+ headers: {
1762
+ authorization: `Bearer ${options.clientSecret}`,
1763
+ "content-type": "application/sdp"
1764
+ },
1765
+ redirect: "error"
1766
+ });
1767
+ const answer = await response.text();
1768
+ if (!response.ok) throw new Error(`Realtime call setup failed (${response.status}).`);
1769
+ await peer.setRemoteDescription({
1770
+ type: "answer",
1771
+ sdp: answer
1772
+ });
1773
+ await opened;
1774
+ } catch (error) {
1775
+ this.close();
1776
+ throw error;
1777
+ }
1778
+ }
1779
+ send(event) {
1780
+ if (!this.channel || this.channel.readyState !== "open") throw new Error("Not connected to Realtime.");
1781
+ this.channel.send(JSON.stringify(event));
1782
+ }
1783
+ close() {
1784
+ this.closed = true;
1785
+ const channel = this.channel;
1786
+ const peer = this.peer;
1787
+ this.channel = null;
1788
+ this.peer = null;
1789
+ if (channel) {
1790
+ channel.onmessage = null;
1791
+ channel.onclose = null;
1792
+ channel.close();
1793
+ }
1794
+ if (peer) {
1795
+ peer.onconnectionstatechange = null;
1796
+ peer.ontrack = null;
1797
+ peer.close();
1798
+ }
1799
+ for (const track of this.microphone?.getTracks() ?? []) track.stop();
1800
+ this.microphone = null;
1801
+ if (this.audio) {
1802
+ this.audio.srcObject = null;
1803
+ this.audio = null;
1804
+ }
1805
+ }
1806
+ handleClose(options, reason) {
1807
+ if (this.closed) return;
1808
+ this.close();
1809
+ options.onClose(reason);
1810
+ }
1811
+ };
1812
+ function parseEvent(data) {
1813
+ if (typeof data !== "string") return null;
1814
+ try {
1815
+ const value = JSON.parse(data);
1816
+ if (typeof value === "object" && value !== null && typeof value.type === "string") return value;
1817
+ } catch {}
1818
+ return null;
1819
+ }
1820
+ function waitForOpen(channel, timeoutMs) {
1821
+ if (channel.readyState === "open") return Promise.resolve();
1822
+ return new Promise((resolve, reject) => {
1823
+ const timer = setTimeout(() => reject(/* @__PURE__ */ new Error("Timed out opening the Realtime data channel.")), timeoutMs);
1824
+ channel.addEventListener("open", () => {
1825
+ clearTimeout(timer);
1826
+ resolve();
1827
+ }, { once: true });
1828
+ });
1829
+ }
1830
+ //#endregion
1831
+ //#region src/composition.ts
1832
+ /**
1833
+ * Composition API: the OpenAI Realtime capability without TurboWarp block definitions.
1834
+ *
1835
+ * A downstream extension (for example `@kubohiroya/turbowarp-voice-chat`) owns its own blocks and
1836
+ * hats, and passes its own `define function` hat opcode. Importing this module does not register
1837
+ * any TurboWarp extension and does not touch the `Scratch` global.
1838
+ */
1839
+ var DEFAULT_RELAY_ENDPOINT = "http://127.0.0.1:8787";
1840
+ function createRealtimeComposition(options) {
1841
+ return new Composition(options);
1842
+ }
1843
+ var Composition = class {
1844
+ constructor(options) {
1845
+ this.options = options;
1846
+ this.relayEndpoint = DEFAULT_RELAY_ENDPOINT;
1847
+ this.relaySession = null;
1848
+ this.settingsValue = defaultSessionSettings();
1849
+ this.timeLimitSeconds = 0;
1850
+ this.connectedAt = null;
1851
+ this.timeLimitTimer = null;
1852
+ this.activeModelValue = "";
1853
+ this.lastResponse = "";
1854
+ this.usageTotals = emptyUsage();
1855
+ this.listeners = /* @__PURE__ */ new Set();
1856
+ this.fetcher = options.fetch ?? ((input, init) => fetch(input, init));
1857
+ this.createTransport = options.createTransport ?? (() => new WebRtcTransport());
1858
+ this.now = options.now ?? Date.now;
1859
+ this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
1860
+ this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
1861
+ this.ownsFunctions = options.functions === void 0;
1862
+ this.namedFunctions = options.functions ?? createNamedFunctions({
1863
+ runtime: options.runtime,
1864
+ functionHatOpcode: options.functionHatOpcode
1865
+ });
1866
+ this.session = new RealtimeSession({
1867
+ callFunction: (name, args) => this.namedFunctions.call(name, args, { exportedOnly: true }),
1868
+ onResponseText: (text) => {
1869
+ this.lastResponse = text;
1870
+ this.emit({
1871
+ type: "response",
1872
+ text
1873
+ });
1874
+ },
1875
+ onError: (message) => this.emit({
1876
+ type: "error",
1877
+ message
1878
+ }),
1879
+ onStateChange: (state) => this.handleState(state),
1880
+ onUsage: (usage) => {
1881
+ this.usageTotals = addUsage(this.usageTotals, usage, this.activeModelValue);
1882
+ this.emit({
1883
+ type: "usage",
1884
+ usage: this.usage()
1885
+ });
1886
+ }
1887
+ });
1888
+ }
1889
+ configureRelay(endpoint) {
1890
+ this.relayEndpoint = normalizeRelayEndpoint(endpoint);
1891
+ this.relaySession = null;
1892
+ }
1893
+ async pairRelay(code) {
1894
+ this.relaySession = await pairWithRelay(this.relayEndpoint, code, this.fetcher, this.now);
1895
+ }
1896
+ isRelayPaired() {
1897
+ return this.relaySession !== null && this.relaySession.expiresAt > this.now();
1898
+ }
1899
+ setModel(model) {
1900
+ this.settingsValue = {
1901
+ ...this.settingsValue,
1902
+ model: normalizeModel(model)
1903
+ };
1904
+ }
1905
+ setInstructions(text) {
1906
+ this.settingsValue = {
1907
+ ...this.settingsValue,
1908
+ instructions: normalizeInstructions(text)
1909
+ };
1910
+ }
1911
+ setVoice(voice) {
1912
+ this.settingsValue = {
1913
+ ...this.settingsValue,
1914
+ voice: normalizeVoice(voice)
1915
+ };
1916
+ }
1917
+ setOutputMode(mode) {
1918
+ this.settingsValue = {
1919
+ ...this.settingsValue,
1920
+ outputMode: normalizeOutputMode(mode)
1921
+ };
1922
+ }
1923
+ setSessionTimeLimit(seconds) {
1924
+ if (!Number.isFinite(seconds) || seconds < 0) throw new TypeError("Session time limit must be zero or a positive number of seconds.");
1925
+ this.timeLimitSeconds = seconds;
1926
+ }
1927
+ get settings() {
1928
+ return { ...this.settingsValue };
1929
+ }
1930
+ get sessionTimeLimitSeconds() {
1931
+ return this.timeLimitSeconds;
1932
+ }
1933
+ scanFunctions() {
1934
+ return this.namedFunctions.scan();
1935
+ }
1936
+ get functions() {
1937
+ return this.namedFunctions;
1938
+ }
1939
+ async connect(options) {
1940
+ if (!this.relaySession || !this.isRelayPaired()) throw new Error("Pair with the local relay first.");
1941
+ const request = buildSessionRequest(this.settingsValue, this.namedFunctions.tools());
1942
+ const secret = await requestClientSecret(this.relaySession, request, this.fetcher, this.now);
1943
+ this.activeModelValue = secret.model || this.settingsValue.model;
1944
+ await this.session.open(this.createTransport(), secret.value, options.microphone);
1945
+ }
1946
+ disconnect() {
1947
+ this.session.close();
1948
+ }
1949
+ get state() {
1950
+ return this.session.state;
1951
+ }
1952
+ get activeModel() {
1953
+ return this.activeModelValue;
1954
+ }
1955
+ sessionElapsedSeconds() {
1956
+ return this.connectedAt === null ? 0 : Math.max(0, (this.now() - this.connectedAt) / 1e3);
1957
+ }
1958
+ sendText(text) {
1959
+ this.session.sendText(text);
1960
+ }
1961
+ get lastResponseText() {
1962
+ return this.lastResponse;
1963
+ }
1964
+ matchFunctionHat(name, thread) {
1965
+ return this.namedFunctions.matchHat(name, thread);
1966
+ }
1967
+ functionArguments(thread) {
1968
+ return this.namedFunctions.argumentsFor(thread);
1969
+ }
1970
+ returnFromFunction(thread, value) {
1971
+ this.namedFunctions.returnFrom(thread, value);
1972
+ }
1973
+ usage() {
1974
+ return { ...this.usageTotals };
1975
+ }
1976
+ resetUsage() {
1977
+ this.usageTotals = emptyUsage();
1978
+ this.emit({
1979
+ type: "usage",
1980
+ usage: this.usage()
1981
+ });
1982
+ }
1983
+ subscribe(listener) {
1984
+ this.listeners.add(listener);
1985
+ return () => this.listeners.delete(listener);
1986
+ }
1987
+ release() {
1988
+ this.disconnect();
1989
+ this.listeners.clear();
1990
+ if (this.ownsFunctions) this.namedFunctions.release();
1991
+ }
1992
+ handleState(state) {
1993
+ if (state === "connected") {
1994
+ this.connectedAt = this.now();
1995
+ if (this.timeLimitSeconds > 0) this.timeLimitTimer = this.setTimer(() => this.reachTimeLimit(), this.timeLimitSeconds * 1e3);
1996
+ } else if (state !== "connecting") {
1997
+ this.connectedAt = null;
1998
+ this.cancelTimeLimit();
1999
+ this.namedFunctions.cancelAll("The Realtime session ended.");
2000
+ }
2001
+ this.emit({
2002
+ type: "state",
2003
+ state
2004
+ });
2005
+ }
2006
+ reachTimeLimit() {
2007
+ this.timeLimitTimer = null;
2008
+ if (this.session.state !== "connected") return;
2009
+ this.session.close();
2010
+ this.emit({ type: "sessionTimeLimitReached" });
2011
+ }
2012
+ cancelTimeLimit() {
2013
+ if (this.timeLimitTimer !== null) this.clearTimer(this.timeLimitTimer);
2014
+ this.timeLimitTimer = null;
2015
+ }
2016
+ emit(event) {
2017
+ for (const listener of [...this.listeners]) listener(event);
2018
+ }
2019
+ };
2020
+ //#endregion
2021
+ //#region src/extension.ts
2022
+ var blockDefinitions = block_definitions_default.blocks;
2023
+ var menuDefinitions = block_definitions_default.menus;
2024
+ var DEFINE_FUNCTION_OPCODE = `${extensionConfig.id}_defineFunction`;
2025
+ var RESPONSE_DONE_OPCODE = `${extensionConfig.id}_whenResponseDone`;
2026
+ var TIME_LIMIT_OPCODE = `${extensionConfig.id}_whenSessionTimeLimitReached`;
2027
+ var USAGE_FIELDS = {
2028
+ costUSD: (usage) => Math.round(usage.estimatedCostUsd * 1e6) / 1e6,
2029
+ responses: (usage) => usage.responses,
2030
+ inputTokens: (usage) => usage.inputTokens,
2031
+ outputTokens: (usage) => usage.outputTokens,
2032
+ cachedInputTokens: (usage) => usage.cachedInputTokens,
2033
+ textInputTokens: (usage) => usage.textInputTokens,
2034
+ audioInputTokens: (usage) => usage.audioInputTokens,
2035
+ textOutputTokens: (usage) => usage.textOutputTokens,
2036
+ audioOutputTokens: (usage) => usage.audioOutputTokens
2037
+ };
2038
+ /** Block surface over the Realtime composition. */
2039
+ var OpenAIRealtimeExtension = class {
2040
+ constructor(deps) {
2041
+ this.lastErrorMessage = "";
2042
+ this.fixedEditorRuntime = deps.runtime;
2043
+ this.realtime = createRealtimeComposition({
2044
+ ...deps,
2045
+ functionHatOpcode: DEFINE_FUNCTION_OPCODE
2046
+ });
2047
+ this.realtime.subscribe((event) => {
2048
+ if (event.type === "response") deps.runtime.startHats(RESPONSE_DONE_OPCODE);
2049
+ else if (event.type === "sessionTimeLimitReached") deps.runtime.startHats(TIME_LIMIT_OPCODE);
2050
+ else if (event.type === "error") this.lastErrorMessage = event.message;
2051
+ });
2052
+ }
2053
+ getInfo() {
2054
+ const info = {
2055
+ id: extensionConfig.id,
2056
+ name: Scratch.translate(block_definitions_default.extensionName),
2057
+ docsURI: extensionConfig.docsURI,
2058
+ blockIconURI: extensionConfig.blockIconURI,
2059
+ blocks: blockDefinitions.map((block) => this.toScratchBlock(block)),
2060
+ menus: Object.fromEntries(Object.entries(menuDefinitions).map(([id, menu]) => [id, {
2061
+ acceptReporters: menu.acceptReporters,
2062
+ items: [...menu.items]
2063
+ }]))
2064
+ };
2065
+ this.fixedEditorsDispose ?? (this.fixedEditorsDispose = registerFixedInputEditors(Scratch, this.fixedEditorRuntime, info.blocks));
2066
+ return info;
2067
+ }
2068
+ configureRelay(args) {
2069
+ this.record(() => this.realtime.configureRelay(Scratch.Cast.toString(args.ENDPOINT)));
2070
+ }
2071
+ pairRelay(args) {
2072
+ return this.recordAsync(() => this.realtime.pairRelay(Scratch.Cast.toString(args.CODE)));
2073
+ }
2074
+ isRelayPaired() {
2075
+ return this.realtime.isRelayPaired();
2076
+ }
2077
+ setInstructions(args) {
2078
+ this.record(() => this.realtime.setInstructions(Scratch.Cast.toString(args.TEXT)));
2079
+ }
2080
+ setVoice(args) {
2081
+ this.record(() => this.realtime.setVoice(Scratch.Cast.toString(args.VOICE)));
2082
+ }
2083
+ setOutputMode(args) {
2084
+ this.record(() => this.realtime.setOutputMode(normalizeOutputMode(Scratch.Cast.toString(args.MODE))));
2085
+ }
2086
+ setModel(args) {
2087
+ this.record(() => this.realtime.setModel(Scratch.Cast.toString(args.MODEL)));
2088
+ }
2089
+ setSessionTimeLimit(args) {
2090
+ this.record(() => this.realtime.setSessionTimeLimit(Scratch.Cast.toNumber(args.SECONDS)));
2091
+ }
2092
+ connect(args) {
2093
+ return this.recordAsync(() => this.realtime.connect({ microphone: Scratch.Cast.toString(args.MICROPHONE) !== "off" }));
2094
+ }
2095
+ disconnect() {
2096
+ this.realtime.disconnect();
2097
+ }
2098
+ isConnected() {
2099
+ return this.realtime.state === "connected";
2100
+ }
2101
+ connectionState() {
2102
+ return this.realtime.state;
2103
+ }
2104
+ currentModel() {
2105
+ return this.realtime.activeModel;
2106
+ }
2107
+ sessionElapsed() {
2108
+ return Math.floor(this.realtime.sessionElapsedSeconds());
2109
+ }
2110
+ whenSessionTimeLimitReached() {
2111
+ return true;
2112
+ }
2113
+ sendText(args) {
2114
+ this.record(() => this.realtime.sendText(Scratch.Cast.toString(args.TEXT)));
2115
+ }
2116
+ whenResponseDone() {
2117
+ return true;
2118
+ }
2119
+ lastResponseText() {
2120
+ return this.realtime.lastResponseText;
2121
+ }
2122
+ defineFunction(args, util) {
2123
+ return this.realtime.matchFunctionHat(Scratch.Cast.toString(args.NAME), util?.thread);
2124
+ }
2125
+ functionArgument(args, util) {
2126
+ try {
2127
+ return toScratchValue(readArgumentPath(this.realtime.functionArguments(util?.thread), Scratch.Cast.toString(args.PATH)));
2128
+ } catch (error) {
2129
+ this.lastErrorMessage = messageOf(error);
2130
+ return "";
2131
+ }
2132
+ }
2133
+ functionArgumentsJson(_args, util) {
2134
+ try {
2135
+ return JSON.stringify(this.realtime.functionArguments(util?.thread) ?? null);
2136
+ } catch (error) {
2137
+ this.lastErrorMessage = messageOf(error);
2138
+ return "";
2139
+ }
2140
+ }
2141
+ returnValue(args, util) {
2142
+ try {
2143
+ this.realtime.returnFromFunction(util?.thread, parseJsonOrText(Scratch.Cast.toString(args.VALUE)));
2144
+ util?.stopThisScript?.();
2145
+ } catch (error) {
2146
+ this.lastErrorMessage = messageOf(error);
2147
+ }
2148
+ }
2149
+ usageValue(args) {
2150
+ const read = USAGE_FIELDS[Scratch.Cast.toString(args.FIELD)];
2151
+ return read ? read(this.realtime.usage()) : "";
2152
+ }
2153
+ resetUsage() {
2154
+ this.realtime.resetUsage();
2155
+ }
2156
+ lastError() {
2157
+ return this.lastErrorMessage;
2158
+ }
2159
+ record(action) {
2160
+ try {
2161
+ action();
2162
+ this.lastErrorMessage = "";
2163
+ } catch (error) {
2164
+ this.lastErrorMessage = messageOf(error);
2165
+ }
2166
+ }
2167
+ async recordAsync(action) {
2168
+ try {
2169
+ await action();
2170
+ this.lastErrorMessage = "";
2171
+ } catch (error) {
2172
+ this.lastErrorMessage = messageOf(error);
2173
+ }
2174
+ }
2175
+ toScratchBlock(block) {
2176
+ const scratchBlock = {
2177
+ opcode: block.opcode,
2178
+ blockType: Scratch.BlockType[block.blockType],
2179
+ text: Scratch.translate(block.text),
2180
+ arguments: Object.fromEntries(Object.entries(block.arguments).map(([name, argument]) => [name, {
2181
+ type: Scratch.ArgumentType[argument.type],
2182
+ defaultValue: argument.defaultValue,
2183
+ ...argument.menu ? { menu: argument.menu } : {}
2184
+ }]))
2185
+ };
2186
+ if (block.blockType === "HAT") scratchBlock.isEdgeActivated = false;
2187
+ return scratchBlock;
2188
+ }
2189
+ };
2190
+ function messageOf(error) {
2191
+ return error instanceof Error ? error.message : String(error);
2192
+ }
2193
+ //#endregion
2194
+ //#region src/index.ts
2195
+ if (extensionConfig.unsandboxed && !Scratch.extensions.unsandboxed) throw new Error(`${extensionConfig.name} must run unsandboxed.`);
2196
+ var runtime = Scratch.vm?.runtime;
2197
+ if (!runtime) throw new Error(`${extensionConfig.name} requires access to the TurboWarp VM runtime.`);
2198
+ Scratch.extensions.register(new OpenAIRealtimeExtension({ runtime }));
2199
+ //#endregion
1860
2200
 
1861
2201
  })(Scratch);