diff --git a/packages/core/aqua-to-js/package.json b/packages/core/aqua-to-js/package.json index 474df3e8..33cd1d42 100644 --- a/packages/core/aqua-to-js/package.json +++ b/packages/core/aqua-to-js/package.json @@ -23,8 +23,6 @@ "@fluencelabs/interfaces": "workspace:*", "@fluencelabs/js-client": "workspace:^", "@fluencelabs/registry": "0.8.7", - "@fluencelabs/spell": "0.5.20", - "@fluencelabs/trust-graph": "0.4.7", "vitest": "0.34.6", "zod": "3.22.4" } diff --git a/packages/core/aqua-to-js/src/common.ts b/packages/core/aqua-to-js/src/common.ts index 5dbf4e0e..5a125a88 100644 --- a/packages/core/aqua-to-js/src/common.ts +++ b/packages/core/aqua-to-js/src/common.ts @@ -120,10 +120,7 @@ export function typeToTs(t: NonArrowType | ArrowType): string { return [name, typeToTs(type)]; }); - // JS-client argument - if (domain.tag !== "unlabeledProduct") { - args.push(["callParams", `ParticleContext$$`]); - } + args.push(["callParams", `ParticleContext$$`]); const funcArgs = args .map(([name, type]) => { diff --git a/packages/core/js-client/src/api.ts b/packages/core/js-client/src/api.ts index ab618d23..77702849 100644 --- a/packages/core/js-client/src/api.ts +++ b/packages/core/js-client/src/api.ts @@ -27,7 +27,6 @@ import { aqua2ts, ts2aqua, wrapFunction, - wrapServiceFunction, } from "./compilerSupport/conversions.js"; import { ServiceImpl } from "./compilerSupport/types.js"; import { FluencePeer } from "./jsPeer/FluencePeer.js"; @@ -43,7 +42,7 @@ import { callAquaFunction, Fluence, registerService } from "./index.js"; * @param script - air script with function execution logic generated by the Aqua compiler */ export const v5_callFunction = async ( - args: (JSONValue | ((...args: JSONValue[]) => JSONValue))[], + args: (JSONValue | ServiceImpl[string])[], def: FunctionCallDef, script: string, ): Promise => { @@ -75,9 +74,7 @@ export const v5_callFunction = async ( ); } - const callArgs = Object.fromEntries< - JSONValue | ((...args: JSONValue[]) => JSONValue) - >( + const callArgs = Object.fromEntries( args.slice(0, argCount).map((arg, i) => { const argSchema = functionArgs[argNames[i]]; @@ -160,7 +157,7 @@ export const v5_registerService = (args: unknown[], def: ServiceDef): void => { const wrappedServiceImpl = Object.fromEntries( Object.entries(serviceImpl).map(([name, func]) => { - return [name, wrapServiceFunction(func, serviceSchema[name])]; + return [name, wrapFunction(func, serviceSchema[name])]; }), ); diff --git a/packages/core/js-client/src/compilerSupport/callFunction.ts b/packages/core/js-client/src/compilerSupport/callFunction.ts index cc927e81..a062b326 100644 --- a/packages/core/js-client/src/compilerSupport/callFunction.ts +++ b/packages/core/js-client/src/compilerSupport/callFunction.ts @@ -17,7 +17,6 @@ import { JSONValue } from "@fluencelabs/interfaces"; import { FluencePeer } from "../jsPeer/FluencePeer.js"; -import { ParticleContext } from "../jsServiceHost/interfaces.js"; import { logger } from "../util/logger.js"; import { ArgCallbackFunction } from "../util/testUtils.js"; @@ -74,16 +73,7 @@ export const callAquaFunction = async ({ let service: ServiceDescription; if (typeof argVal === "function") { - service = userHandlerService( - "callbackSrv", - name, - (...args: [...JSONValue[], ParticleContext]) => { - // Impossible to extract all element except the last one and coerce type - // eslint-disable-next-line @typescript-eslint/consistent-type-assertions - const jsonArgs = args.slice(0, args.length - 1) as JSONValue[]; - return argVal(jsonArgs); - }, - ); + service = userHandlerService("callbackSrv", name, argVal); } else { service = injectValueService("getDataSrv", name, argVal); } diff --git a/packages/core/js-client/src/compilerSupport/conversions.ts b/packages/core/js-client/src/compilerSupport/conversions.ts index 17ca76db..cfb6cf5c 100644 --- a/packages/core/js-client/src/compilerSupport/conversions.ts +++ b/packages/core/js-client/src/compilerSupport/conversions.ts @@ -15,15 +15,17 @@ */ import { - ArrowWithCallbacks, + ArrowType, ArrowWithoutCallbacks, JSONValue, + LabeledProductType, NonArrowSimpleType, + SimpleTypes, } from "@fluencelabs/interfaces"; import { ParticleContext } from "../jsServiceHost/interfaces.js"; -import { MaybePromise } from "./types.js"; +import { ServiceImpl } from "./types.js"; export function aqua2ts( value: JSONValue, @@ -123,30 +125,9 @@ export function ts2aqua( } export const wrapFunction = ( - value: (...args: JSONValue[]) => JSONValue, - schema: ArrowWithoutCallbacks, -): ((...args: JSONValue[]) => JSONValue) => { - return (...args) => { - const schemaArgs = - schema.codomain.tag === "nil" ? [] : schema.codomain.items; - - const tsArgs = args.map((arg, i) => { - return aqua2ts(arg, schemaArgs[i]); - }); - - const result = value(...tsArgs); - return ts2aqua(result, schema.codomain); - }; -}; - -export const wrapServiceFunction = ( - value: ( - ...args: [...JSONValue[], ParticleContext] - ) => MaybePromise, - schema: ArrowWithCallbacks, -): (( - ...args: [...JSONValue[], ParticleContext] -) => MaybePromise) => { + value: ServiceImpl[string], + schema: ArrowWithoutCallbacks | ArrowType>, +): ServiceImpl[string] => { return async (...args) => { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const jsonArgs = args.slice(0, args.length - 1) as JSONValue[]; @@ -154,13 +135,24 @@ export const wrapServiceFunction = ( const context = args[args.length - 1] as ParticleContext; const schemaArgs = - schema.codomain.tag === "nil" ? [] : schema.codomain.items; + schema.domain.tag === "nil" + ? [] + : schema.domain.tag === "unlabeledProduct" + ? schema.domain.items + : Object.values(schema.domain.fields); const tsArgs = jsonArgs.map((arg, i) => { return aqua2ts(arg, schemaArgs[i]); }); const result = await value(...tsArgs, context); - return ts2aqua(result, schema.codomain); + + const valueSchema = + schema.codomain.tag === "unlabeledProduct" && + schema.codomain.items.length === 1 + ? schema.codomain.items[0] + : schema.codomain; + + return ts2aqua(result, valueSchema); }; }; diff --git a/packages/core/js-client/src/util/testUtils.ts b/packages/core/js-client/src/util/testUtils.ts index 99f3eee3..da7967ff 100644 --- a/packages/core/js-client/src/util/testUtils.ts +++ b/packages/core/js-client/src/util/testUtils.ts @@ -28,6 +28,7 @@ import { Subject, Subscribable } from "rxjs"; import { ClientPeer, makeClientPeerConfig } from "../clientPeer/ClientPeer.js"; import { ClientConfig, RelayOptions } from "../clientPeer/types.js"; import { callAquaFunction } from "../compilerSupport/callFunction.js"; +import { ServiceImpl } from "../compilerSupport/types.js"; import { IConnection } from "../connection/interfaces.js"; import { DEFAULT_CONFIG, FluencePeer } from "../jsPeer/FluencePeer.js"; import { CallServiceResultType } from "../jsServiceHost/interfaces.js"; @@ -76,9 +77,7 @@ interface FunctionInfo { /** * Type for callback passed as aqua function argument */ -export type ArgCallbackFunction = ( - ...args: JSONValue[] -) => JSONValue | Promise; +export type ArgCallbackFunction = ServiceImpl[string]; /** * Arguments passed to Aqua function diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a7fe0a59..084305ae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -176,17 +176,11 @@ importers: specifier: workspace:* version: link:../interfaces '@fluencelabs/js-client': - specifier: 0.4.3 + specifier: workspace:^ version: link:../js-client '@fluencelabs/registry': specifier: 0.8.7 version: 0.8.7 - '@fluencelabs/spell': - specifier: 0.5.20 - version: 0.5.20 - '@fluencelabs/trust-graph': - specifier: 0.4.7 - version: 0.4.7 vitest: specifier: 0.34.6 version: 0.34.6 @@ -3731,10 +3725,6 @@ packages: '@fluencelabs/trust-graph': 0.4.1 dev: true - /@fluencelabs/spell@0.5.20: - resolution: {integrity: sha512-QFbknWwALLUWMzpWkFt34McuwTz9xwQuiPP1zXqhPqVZ1J6g8F3gwHHtzgHFW5Z7WrRmwsL+IQtFJy8YZubhDw==} - dev: true - /@fluencelabs/threads@2.0.0: resolution: {integrity: sha512-dgYpZg55OcEmop1U3G2bFKEJXg2avjXWYfWsdPlkSbHOHguaRifvr5bgwIYTg1wxoPGcn0jegcjKKwrY0qrV+g==} dependencies: @@ -3754,12 +3744,6 @@ packages: '@fluencelabs/aqua-lib': 0.7.3 dev: true - /@fluencelabs/trust-graph@0.4.7: - resolution: {integrity: sha512-e4TxWimUh9GBWjqSO8WGsSqjZfyIs6f39/8Pzfo6PCcNoSf8FPaaO817Pw4FmAXYEKR1IalIUX3CDdym3NlHWw==} - dependencies: - '@fluencelabs/aqua-lib': 0.7.3 - dev: true - /@fluencelabs/trust-graph@3.1.2: resolution: {integrity: sha512-HpyHtiomh09wv6/83z+bhbkqVngIUdqNGEXRTIPg4sArVPMZ9UCXBrkQsHDRqdMUx0lBAcgB3IjlbdhkwHGaXA==} dependencies: