@kubohiroya/turbowarp-openai-realtime-api 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ja.md CHANGED
@@ -55,7 +55,7 @@ OpenAI Realtime APIと音声やテキストで会話し、ブロックで書い
55
55
  npm packageとして使う場合は、検証済みのversionをexact pinします。
56
56
 
57
57
  ```bash
58
- pnpm add --save-exact @kubohiroya/turbowarp-openai-realtime-api@0.5.1
58
+ pnpm add --save-exact @kubohiroya/turbowarp-openai-realtime-api@0.5.2
59
59
  ```
60
60
 
61
61
  ## クイックスタート
package/README.md CHANGED
@@ -61,7 +61,7 @@ The reviewed JavaScript build is committed to this repository, so users do not n
61
61
  Install an exact version that you have reviewed:
62
62
 
63
63
  ```bash
64
- pnpm add --save-exact @kubohiroya/turbowarp-openai-realtime-api@0.5.1
64
+ pnpm add --save-exact @kubohiroya/turbowarp-openai-realtime-api@0.5.2
65
65
  ```
66
66
 
67
67
  Standalone bundle:
@@ -558,7 +558,7 @@ var block_definitions_default = {
558
558
  }
559
559
  };
560
560
  //#endregion
561
- //#region node_modules/.pnpm/@kubohiroya+turbowarp-named-functions@0.4.1-rc.2/node_modules/@kubohiroya/turbowarp-named-functions/dist/composition.js
561
+ //#region node_modules/.pnpm/@kubohiroya+turbowarp-named-functions@0.4.2/node_modules/@kubohiroya/turbowarp-named-functions/dist/composition.js
562
562
  /**
563
563
  * A small JSON Schema validator for function arguments and returns. It interprets the schema at run time and
564
564
  * never generates code, so it also works where `eval` is unavailable (for example Cloudflare Workers).
@@ -675,12 +675,18 @@ function deepEqual(left, right) {
675
675
  function asRecord$2(value) {
676
676
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
677
677
  }
678
+ function outcomeToPromise(outcome) {
679
+ if (outcome.state === "fulfilled") return Promise.resolve(outcome.value);
680
+ if (outcome.state === "rejected") return Promise.reject(outcome.error);
681
+ return outcome.promise;
682
+ }
678
683
  var FunctionDispatcher = class {
679
684
  constructor(runtime, options) {
680
685
  this.runtime = runtime;
681
686
  this.options = options;
682
687
  this.queue = [];
683
688
  this.running = /* @__PURE__ */ new Map();
689
+ this.direct = /* @__PURE__ */ new Set();
684
690
  this.byThread = /* @__PURE__ */ new Map();
685
691
  this.starting = null;
686
692
  this.step = 0;
@@ -689,39 +695,62 @@ var FunctionDispatcher = class {
689
695
  this.timeoutMs = options.timeoutMs ?? 3e4;
690
696
  this.setTimer = options.setTimer ?? ((callback, ms) => setTimeout(callback, ms));
691
697
  this.clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
698
+ const port = runtime.getThreadExecutionPort?.();
699
+ this.port = port?.version === 1 ? port : null;
692
700
  runtime.on("AFTER_EXECUTE", this.onAfterExecute);
693
701
  runtime.on("PROJECT_STOP_ALL", this.onStopAll);
694
702
  }
703
+ /** True when calls run inside the calling block and may recurse. */
704
+ get runsSynchronously() {
705
+ return this.port !== null;
706
+ }
695
707
  /**
696
708
  * Starts a named function. `caller` is the thread of the block making the call, if any; it is used
697
709
  * to detect reentrant calls.
698
710
  */
699
711
  invoke(name, args, caller, returnSchema) {
700
- if (!this.options.knownNames().has(name)) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${name}`));
712
+ return outcomeToPromise(this.invokeNow({
713
+ name,
714
+ args,
715
+ caller,
716
+ returnSchema
717
+ }));
718
+ }
719
+ /**
720
+ * Starts a named function and reports right away whether it already settled. With the port and a
721
+ * `script`, a function that does not wait settles before this returns.
722
+ */
723
+ invokeNow(request) {
724
+ const { name, caller } = request;
725
+ if (!this.options.knownNames().has(name)) return {
726
+ state: "rejected",
727
+ error: /* @__PURE__ */ new Error(`Unknown function: ${name}`)
728
+ };
701
729
  const parentChain = caller ? this.byThread.get(caller)?.chain ?? [] : [];
702
- if (parentChain.includes(name)) return Promise.reject(/* @__PURE__ */ new Error(`Reentrant call: ${[...parentChain, name].join(" -> ")}. Use a custom block for recursion.`));
703
- return new Promise((resolve, reject) => {
704
- const invocation = {
705
- name,
706
- args,
707
- ...returnSchema === void 0 ? {} : { returnSchema },
708
- chain: [...parentChain, name],
709
- resolve,
710
- reject,
711
- thread: null,
712
- startedAtStep: -1,
713
- timer: null,
714
- settled: false
715
- };
716
- invocation.timer = this.setTimer(() => this.settle(invocation, /* @__PURE__ */ new Error(`Function ${name} timed out.`)), this.timeoutMs);
717
- this.queue.push(invocation);
718
- this.pump();
719
- });
730
+ const chain = [...parentChain, name];
731
+ if (this.port && request.script) return this.runDirect(this.port, request, chain);
732
+ if (parentChain.includes(name)) return {
733
+ state: "rejected",
734
+ error: /* @__PURE__ */ new Error(`Reentrant call: ${chain.join(" -> ")}. Recursion needs the TurboWarp Plus runtime; use a custom block for recursion here.`)
735
+ };
736
+ const invocation = this.createInvocation(request, chain);
737
+ this.armTimer(invocation);
738
+ this.queue.push(invocation);
739
+ this.pump();
740
+ return invocation.outcome ?? {
741
+ state: "pending",
742
+ promise: invocation.promise
743
+ };
720
744
  }
721
745
  /** Hat predicate: true only for the script that should run the invocation being started. */
722
746
  matchHat(name, thread) {
747
+ if (!thread) return false;
748
+ const bound = this.byThread.get(thread);
749
+ if (bound) return bound.name === name.trim();
723
750
  const invocation = this.starting;
724
- if (!invocation || !thread || invocation.name !== name.trim()) return false;
751
+ if (!invocation || invocation.name !== name.trim()) return false;
752
+ const script = invocation.script;
753
+ if (script && (thread.topBlock !== script.blockId || thread.target && thread.target !== script.target)) return false;
725
754
  invocation.thread = thread;
726
755
  this.byThread.set(thread, invocation);
727
756
  this.starting = null;
@@ -738,17 +767,68 @@ var FunctionDispatcher = class {
738
767
  this.settleWithValue(invocation, value);
739
768
  }
740
769
  cancelAll(reason) {
741
- for (const invocation of [...this.queue, ...this.running.values()]) this.settle(invocation, new Error(reason));
770
+ for (const invocation of [
771
+ ...this.queue,
772
+ ...this.running.values(),
773
+ ...this.direct
774
+ ]) this.settle(invocation, new Error(reason));
742
775
  if (this.starting) this.settle(this.starting, new Error(reason));
743
776
  }
744
777
  get pendingCount() {
745
- return this.queue.length + this.running.size;
778
+ return this.queue.length + this.running.size + this.direct.size;
746
779
  }
747
780
  release() {
748
781
  this.cancelAll("Named functions were released.");
749
782
  this.runtime.off?.("AFTER_EXECUTE", this.onAfterExecute);
750
783
  this.runtime.off?.("PROJECT_STOP_ALL", this.onStopAll);
751
784
  }
785
+ runDirect(port, request, chain) {
786
+ const invocation = this.createInvocation(request, chain);
787
+ invocation.promise.catch(() => void 0);
788
+ const script = request.script;
789
+ try {
790
+ const thread = port.startScript(script.blockId, script.target);
791
+ invocation.thread = thread;
792
+ this.byThread.set(thread, invocation);
793
+ this.direct.add(invocation);
794
+ if (chain.length <= 128 && port.runNow(thread) === "done") this.settleWithValue(invocation, null);
795
+ } catch (error) {
796
+ this.settle(invocation, error instanceof Error ? error : new Error(String(error)));
797
+ }
798
+ if (invocation.outcome) return invocation.outcome;
799
+ this.armTimer(invocation);
800
+ return {
801
+ state: "pending",
802
+ promise: invocation.promise
803
+ };
804
+ }
805
+ createInvocation(request, chain) {
806
+ let resolve = () => void 0;
807
+ let reject = () => void 0;
808
+ const promise = new Promise((resolvePromise, rejectPromise) => {
809
+ resolve = resolvePromise;
810
+ reject = rejectPromise;
811
+ });
812
+ return {
813
+ name: request.name,
814
+ args: request.args,
815
+ ...request.returnSchema === void 0 ? {} : { returnSchema: request.returnSchema },
816
+ chain,
817
+ ...request.script ? { script: request.script } : {},
818
+ promise,
819
+ resolve,
820
+ reject,
821
+ thread: null,
822
+ startedAtStep: -1,
823
+ timer: null,
824
+ settled: false,
825
+ outcome: null
826
+ };
827
+ }
828
+ armTimer(invocation) {
829
+ if (invocation.settled) return;
830
+ invocation.timer = this.setTimer(() => this.settle(invocation, /* @__PURE__ */ new Error(`Function ${invocation.name} timed out.`)), this.timeoutMs);
831
+ }
752
832
  pump() {
753
833
  if (this.starting) return;
754
834
  const index = this.queue.findIndex((invocation) => !this.running.has(invocation.name));
@@ -764,20 +844,32 @@ var FunctionDispatcher = class {
764
844
  this.step += 1;
765
845
  const starting = this.starting;
766
846
  if (starting && this.step - starting.startedAtStep > 2) this.settle(starting, /* @__PURE__ */ new Error(`Function ${starting.name} did not start. Is its script already running?`));
767
- for (const invocation of this.running.values()) if (invocation.thread && !this.runtime.threads.includes(invocation.thread)) this.settleWithValue(invocation, null);
847
+ for (const invocation of [...this.running.values(), ...this.direct]) if (invocation.thread && !this.runtime.threads.includes(invocation.thread)) this.settleWithValue(invocation, null);
768
848
  this.pump();
769
849
  }
770
850
  settle(invocation, error, value) {
771
851
  if (invocation.settled) return;
772
852
  invocation.settled = true;
773
- this.clearTimer(invocation.timer);
853
+ if (invocation.timer !== null) this.clearTimer(invocation.timer);
774
854
  const queued = this.queue.indexOf(invocation);
775
855
  if (queued >= 0) this.queue.splice(queued, 1);
776
856
  if (this.running.get(invocation.name) === invocation) this.running.delete(invocation.name);
857
+ this.direct.delete(invocation);
777
858
  if (invocation.thread) this.byThread.delete(invocation.thread);
778
859
  if (this.starting === invocation) this.starting = null;
779
- if (error) invocation.reject(error);
780
- else invocation.resolve(value);
860
+ if (error) {
861
+ invocation.outcome = {
862
+ state: "rejected",
863
+ error
864
+ };
865
+ invocation.reject(error);
866
+ } else {
867
+ invocation.outcome = {
868
+ state: "fulfilled",
869
+ value
870
+ };
871
+ invocation.resolve(value);
872
+ }
781
873
  }
782
874
  settleWithValue(invocation, value) {
783
875
  if (invocation.returnSchema !== void 0) {
@@ -862,9 +954,8 @@ var MAX_DESCRIPTION_LENGTH = 1024;
862
954
  * tool contract sent to the model before any script runs, so they must be literal text.
863
955
  */
864
956
  function scanFunctionDefinitions(targets, hatOpcode, options = {}) {
865
- const functions = [];
957
+ const scanned = [];
866
958
  const errors = [];
867
- const seen = /* @__PURE__ */ new Map();
868
959
  for (const target of targets) {
869
960
  if (target.isOriginal === false) continue;
870
961
  const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
@@ -873,22 +964,78 @@ function scanFunctionDefinitions(targets, hatOpcode, options = {}) {
873
964
  if (block.opcode !== hatOpcode || block.topLevel === false) continue;
874
965
  const where = `${targetName} (block ${block.id})`;
875
966
  try {
876
- const definition = readDefinition(block, blocks, targetName, options);
877
- const previous = seen.get(definition.name);
878
- if (previous !== void 0) throw new Error(`function "${definition.name}" is already defined in ${previous}`);
879
- seen.set(definition.name, where);
880
- functions.push(definition);
967
+ scanned.push({
968
+ definition: readDefinition(block, blocks, targetName, target, options),
969
+ where
970
+ });
881
971
  } catch (error) {
882
972
  errors.push(`${where}: ${error instanceof Error ? error.message : String(error)}`);
883
973
  }
884
974
  }
885
975
  }
976
+ const functions = [];
977
+ const globals = /* @__PURE__ */ new Map();
978
+ for (const { definition, where } of scanned) {
979
+ if (definition.scope !== "global") continue;
980
+ const previous = globals.get(definition.name);
981
+ if (previous !== void 0) {
982
+ errors.push(`${where}: function "${definition.name}" is already defined in ${previous}`);
983
+ continue;
984
+ }
985
+ globals.set(definition.name, where);
986
+ functions.push(definition);
987
+ }
988
+ const locals = /* @__PURE__ */ new Map();
989
+ for (const { definition, where } of scanned) {
990
+ if (definition.scope !== "sprite") continue;
991
+ const global = globals.get(definition.name);
992
+ if (global !== void 0) {
993
+ errors.push(`${where}: function "${definition.name}" is already defined for all sprites in ${global}`);
994
+ continue;
995
+ }
996
+ const own = locals.get(definition.target) ?? /* @__PURE__ */ new Map();
997
+ locals.set(definition.target, own);
998
+ const previous = own.get(definition.name);
999
+ if (previous !== void 0) {
1000
+ errors.push(`${where}: function "${definition.name}" is already defined in ${previous}`);
1001
+ continue;
1002
+ }
1003
+ own.set(definition.name, where);
1004
+ functions.push(definition);
1005
+ }
886
1006
  functions.sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
887
1007
  return {
888
1008
  functions,
889
1009
  errors
890
1010
  };
891
1011
  }
1012
+ /** True when both are the same sprite: the original, one of its clones, or the stage itself. */
1013
+ function sameSprite(left, right) {
1014
+ return left === right || left.sprite !== void 0 && left.sprite === right.sprite;
1015
+ }
1016
+ /**
1017
+ * Finds the function a call names. A caller's own sprite function runs as the caller (a clone runs it
1018
+ * as itself, as with a custom block); a global function runs as the sprite or stage that defines it.
1019
+ * Without a caller (a call from outside the project), only global functions are visible.
1020
+ */
1021
+ function resolveFunction(functions, name, caller) {
1022
+ if (caller) {
1023
+ const own = functions.find((definition) => definition.scope === "sprite" && definition.name === name && sameSprite(definition.target, caller));
1024
+ if (own) return {
1025
+ definition: own,
1026
+ runAs: caller
1027
+ };
1028
+ }
1029
+ const global = functions.find((definition) => definition.scope === "global" && definition.name === name);
1030
+ return global ? {
1031
+ definition: global,
1032
+ runAs: global.target
1033
+ } : void 0;
1034
+ }
1035
+ /** Functions a script of `caller` can call: its sprite's own functions, then the global ones. */
1036
+ function visibleFunctions(functions, caller) {
1037
+ return functions.filter((definition) => definition.scope === "global" || caller !== void 0 && sameSprite(definition.target, caller));
1038
+ }
892
1039
  function toFunctionTools(functions) {
893
1040
  return functions.filter((definition) => definition.exportAs === "tool").map((definition) => ({
894
1041
  type: "function",
@@ -897,7 +1044,7 @@ function toFunctionTools(functions) {
897
1044
  parameters: definition.parameters
898
1045
  }));
899
1046
  }
900
- function readDefinition(block, blocks, targetName, options) {
1047
+ function readDefinition(block, blocks, targetName, target, options) {
901
1048
  const name = readLiteralInput(block, blocks, "NAME").trim();
902
1049
  if (!FUNCTION_NAME_PATTERN.test(name)) throw new Error("function name must be 1-64 letters, digits, \"_\" or \"-\"");
903
1050
  const description = readLiteralInput(block, blocks, "DESCRIPTION").trim();
@@ -907,16 +1054,27 @@ function readDefinition(block, blocks, targetName, options) {
907
1054
  const returns = returnsText === void 0 || returnsText.trim() === "" ? void 0 : parseSchema(returnsText, "returns schema", false, options);
908
1055
  const exportAs = String(block.fields?.EXPORT?.value ?? "none") === "tool" ? "tool" : "none";
909
1056
  if (exportAs === "tool" && description.length === 0) throw new Error("a function exported as a tool needs a description");
1057
+ const scope = readScope(block);
1058
+ if (exportAs === "tool" && scope !== "global") throw new Error("a function exported as a tool must be for all sprites");
910
1059
  return {
911
1060
  name,
1061
+ scope,
912
1062
  description,
913
1063
  parameters,
914
1064
  ...returns ? { returns } : {},
915
1065
  exportAs,
916
1066
  targetName,
1067
+ target,
917
1068
  blockId: block.id
918
1069
  };
919
1070
  }
1071
+ /** SCOPE is a menu field. Hats saved before it existed have none and keep their global meaning. */
1072
+ function readScope(block) {
1073
+ const value = block.fields?.SCOPE?.value;
1074
+ if (value === void 0 || value === null || value === "global") return "global";
1075
+ if (value === "sprite") return "sprite";
1076
+ throw new Error(`unknown function scope "${String(value)}"`);
1077
+ }
920
1078
  /**
921
1079
  * SCHEMA and RETURNS hold either JSON Schema text or, when the consumer supports it, the name of a
922
1080
  * `define schema` hat. Text that is not JSON is treated as a name, so existing JSON projects are
@@ -1283,17 +1441,39 @@ var NamedFunctionsImpl = class {
1283
1441
  if (errors.length > 0) throw new Error(`Invalid function definitions: ${errors.join("; ")}`);
1284
1442
  return toFunctionTools(this.scan().functions);
1285
1443
  }
1286
- isDefined(name) {
1287
- return this.scan().functions.some((definition) => definition.name === name.trim());
1444
+ isDefined(name, caller) {
1445
+ return resolveFunction(this.scan().functions, name.trim(), caller) !== void 0;
1446
+ }
1447
+ visible(caller) {
1448
+ const functions = visibleFunctions(this.scan().functions, caller);
1449
+ return [...functions.filter((definition) => definition.scope === "sprite"), ...functions.filter((definition) => definition.scope === "global")];
1450
+ }
1451
+ get runsSynchronously() {
1452
+ return this.dispatcher.runsSynchronously;
1288
1453
  }
1289
1454
  call(name, args, options = {}) {
1455
+ return outcomeToPromise(this.callNow(name, args, options));
1456
+ }
1457
+ callNow(name, args, options = {}) {
1290
1458
  const trimmed = name.trim();
1291
- const definition = this.scan().functions.find((candidate) => candidate.name === trimmed);
1292
- if (!definition) return Promise.reject(/* @__PURE__ */ new Error(`Unknown function: ${trimmed}`));
1293
- if (options.exportedOnly && definition.exportAs !== "tool") return Promise.reject(/* @__PURE__ */ new Error(`Function ${trimmed} is not exported as a tool.`));
1459
+ const scan = this.scan();
1460
+ const caller = options.caller?.target;
1461
+ const resolved = resolveFunction(scan.functions, trimmed, caller);
1462
+ if (!resolved) return rejected(unknownFunctionMessage(scan.functions, trimmed, caller));
1463
+ const { definition, runAs } = resolved;
1464
+ if (options.exportedOnly && definition.exportAs !== "tool") return rejected(`Function ${trimmed} is not exported as a tool.`);
1294
1465
  const errors = validateAgainstSchema(args, definition.parameters);
1295
- if (errors.length > 0) return Promise.reject(/* @__PURE__ */ new Error(`Invalid arguments for ${trimmed}: ${errors.join("; ")}`));
1296
- return this.dispatcher.invoke(trimmed, args, options.caller, definition.returns);
1466
+ if (errors.length > 0) return rejected(`Invalid arguments for ${trimmed}: ${errors.join("; ")}`);
1467
+ return this.dispatcher.invokeNow({
1468
+ name: trimmed,
1469
+ args,
1470
+ caller: options.caller,
1471
+ returnSchema: definition.returns,
1472
+ script: {
1473
+ target: runAs,
1474
+ blockId: definition.blockId
1475
+ }
1476
+ });
1297
1477
  }
1298
1478
  start(name, args, options = {}) {
1299
1479
  const id = `p_${this.nextPromiseId++}`;
@@ -1360,6 +1540,18 @@ var NamedFunctionsImpl = class {
1360
1540
  }
1361
1541
  }
1362
1542
  };
1543
+ /** Names the sprite that owns a function the caller cannot see, so the fix is obvious. */
1544
+ function unknownFunctionMessage(functions, name, caller) {
1545
+ const elsewhere = functions.find((definition) => definition.name === name);
1546
+ if (!elsewhere) return `Unknown function: ${name}`;
1547
+ return caller ? `Function ${name} belongs to ${elsewhere.targetName}; make it for all sprites to call it from here.` : `Function ${name} belongs to ${elsewhere.targetName}; only functions for all sprites can be called from outside.`;
1548
+ }
1549
+ function rejected(message) {
1550
+ return {
1551
+ state: "rejected",
1552
+ error: new Error(message)
1553
+ };
1554
+ }
1363
1555
  function messageOf$2(error) {
1364
1556
  return error instanceof Error ? error.message : String(error);
1365
1557
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-openai-realtime-api",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "A TurboWarp extension for the OpenAI Realtime API, using a localhost relay for credentials.",
5
5
  "author": {
6
6
  "name": "Hiroya Kubo",
@@ -73,7 +73,7 @@
73
73
  "typescript-eslint": "^8.66.0",
74
74
  "vite": "^8.2.2",
75
75
  "vitest": "^5.0.0",
76
- "@kubohiroya/turbowarp-extension-data-editor": "^0.8.1"
76
+ "@kubohiroya/turbowarp-extension-data-editor": "^0.10.0"
77
77
  },
78
78
  "exports": {
79
79
  "./composition": {
@@ -84,7 +84,7 @@
84
84
  "./package.json": "./package.json"
85
85
  },
86
86
  "dependencies": {
87
- "@kubohiroya/turbowarp-named-functions": "^0.4.1-rc.2"
87
+ "@kubohiroya/turbowarp-named-functions": "^0.4.2"
88
88
  },
89
89
  "gitHead": "2386a92f281d6285af4c9c61f19e2577453708e5",
90
90
  "x-turbowarp-source": {