diff options
author | bobzel <zzzman@gmail.com> | 2024-05-19 00:05:18 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2024-05-19 00:05:18 -0400 |
commit | 38742d5f491ed5232a381da63e126b609cf14aad (patch) | |
tree | a723b0c9dfffa717b70df383d21fb3b14567308f /src/fields/ScriptField.ts | |
parent | a3784cd3ab990d8016b1168eb0cbf7e9a2f22301 (diff) | |
parent | 0b451af28e5aef6b749da61e8a9fcd0a840789ac (diff) |
Merge branch 'restoringEslint' into aisosa-starter
Diffstat (limited to 'src/fields/ScriptField.ts')
-rw-r--r-- | src/fields/ScriptField.ts | 97 |
1 files changed, 58 insertions, 39 deletions
diff --git a/src/fields/ScriptField.ts b/src/fields/ScriptField.ts index 8b51088b2..8fe365ac2 100644 --- a/src/fields/ScriptField.ts +++ b/src/fields/ScriptField.ts @@ -1,17 +1,16 @@ import { action, makeObservable, observable } from 'mobx'; import { computedFn } from 'mobx-utils'; -import { createSimpleSchema, custom, map, object, primitive, PropSchema, serializable, SKIP } from 'serializr'; -import { DocServer } from '../client/DocServer'; -import { CompiledScript, CompileScript, ScriptOptions, Transformer } from '../client/util/Scripting'; -import { scriptingGlobal, ScriptingGlobals } from '../client/util/ScriptingGlobals'; -import { autoObject, Deserializable } from '../client/util/SerializationHelper'; +import { PropSchema, SKIP, createSimpleSchema, custom, map, object, primitive, serializable } from 'serializr'; import { numberRange } from '../Utils'; -import { Doc, Field, FieldResult, Opt } from './Doc'; +import { GPTCallType, gptAPICall } from '../client/apis/gpt/GPT'; +import { CompileScript, CompiledScript, ScriptOptions, Transformer } from '../client/util/Scripting'; +import { ScriptingGlobals, scriptingGlobal } from '../client/util/ScriptingGlobals'; +import { Deserializable, autoObject } from '../client/util/SerializationHelper'; +import { Doc, Field, FieldType, FieldResult, Opt } from './Doc'; import { Copy, FieldChanged, Id, ToJavascriptString, ToScriptString, ToString, ToValue } from './FieldSymbols'; import { List } from './List'; -import { ObjectField } from './ObjectField'; +import { ObjGetRefField, ObjectField } from './ObjectField'; import { Cast, StrCast } from './Types'; -import { GPTCallType, gptAPICall } from '../client/apis/gpt/GPT'; function optional(propSchema: PropSchema) { return custom( @@ -44,7 +43,9 @@ const scriptSchema = createSimpleSchema({ originalScript: true, }); -function finalizeScript(script: ScriptField) { +// eslint-disable-next-line no-use-before-define +function finalizeScript(scriptIn: ScriptField) { + const script = scriptIn; const comp = CompileScript(script.script.originalScript, script.script.options); if (!comp.compiled) { throw new Error("Couldn't compile loaded script"); @@ -54,11 +55,13 @@ function finalizeScript(script: ScriptField) { if (!compset.compiled) { throw new Error("Couldn't compile setter script"); } - (script as any).setterscript = compset; + script.setterscript = compset; } return comp; } -async function deserializeScript(script: ScriptField) { +// eslint-disable-next-line no-use-before-define +async function deserializeScript(scriptIn: ScriptField) { + const script = scriptIn; if (script.captures) { const captured: any = {}; (script.script.options as ScriptOptions).capturedVariables = captured; @@ -68,13 +71,16 @@ async function deserializeScript(script: ScriptField) { const val = capture.split(':')[1]; if (val === 'true') captured[key] = true; else if (val === 'false') captured[key] = false; - else if (val.startsWith('ID->')) captured[key] = await DocServer.GetRefField(val.replace('ID->', '')); + else if (val.startsWith('ID->')) captured[key] = await ObjGetRefField(val.replace('ID->', '')); else if (!isNaN(Number(val))) captured[key] = Number(val); else captured[key] = val; }) - ).then(() => ((script as any).script = finalizeScript(script))); + ).then(() => { + script.script = finalizeScript(script); + }); } else { - (script as any).script = ScriptField.GetScriptFieldCache(script.script.originalScript) ?? finalizeScript(script); + // eslint-disable-next-line no-use-before-define + script.script = ScriptField.GetScriptFieldCache(script.script.originalScript) ?? finalizeScript(script); } } @@ -84,9 +90,9 @@ export class ScriptField extends ObjectField { @serializable readonly rawscript: string | undefined; @serializable(object(scriptSchema)) - readonly script: CompiledScript; + script: CompiledScript; @serializable(object(scriptSchema)) - readonly setterscript: CompiledScript | undefined; + setterscript: CompiledScript | undefined; @serializable @observable _cachedResult: FieldResult = undefined; @@ -131,11 +137,11 @@ export class ScriptField extends ObjectField { [ToString]() { return this.script.originalScript; } + // eslint-disable-next-line default-param-last public static CompileScript(script: string, params: object = {}, addReturn = false, capturedVariables?: { [name: string]: Doc | string | number | boolean }, transformer?: Transformer) { return CompileScript(script, { params: { this: Doc?.name || 'Doc', // this is the doc that executes the script - self: Doc?.name || 'Doc', // self is the root doc of the doc that executes the script documentView: 'any', _last_: 'any', // _last_ is the previous value of a computed field when it is being triggered to re-run. _setCacheResult_: 'any', // set the cached value of the function @@ -150,20 +156,23 @@ export class ScriptField extends ObjectField { }); } + // eslint-disable-next-line default-param-last public static MakeFunction(script: string, params: object = {}, capturedVariables?: { [name: string]: Doc | string | number | boolean }) { const compiled = ScriptField.CompileScript(script, params, true, capturedVariables); return compiled.compiled ? new ScriptField(compiled) : undefined; } + // eslint-disable-next-line default-param-last public static MakeScript(script: string, params: object = {}, capturedVariables?: { [name: string]: Doc | string | number | boolean }) { const compiled = ScriptField.CompileScript(script, params, false, capturedVariables); return compiled.compiled ? new ScriptField(compiled) : undefined; } - public static CallGpt(queryText: string, setVal: (val: FieldResult) => void, target: Doc) { + public static CallGpt(queryTextIn: string, setVal: (val: FieldResult) => void, target: Doc) { + let queryText = queryTextIn; if (typeof queryText === 'string' && setVal) { while (queryText.match(/\(this\.[a-zA-Z_]*\)/)?.length) { const fieldRef = queryText.split('(this.')[1].replace(/\).*/, ''); - queryText = queryText.replace(/\(this\.[a-zA-Z_]*\)/, Field.toString(target[fieldRef] as Field)); + queryText = queryText.replace(/\(this\.[a-zA-Z_]*\)/, Field.toString(target[fieldRef] as FieldType)); } setVal(`Chat Pending: ${queryText}`); gptAPICall(queryText, GPTCallType.COMPLETION).then(result => { @@ -198,24 +207,29 @@ export class ComputedField extends ScriptField { } _lastComputedResult: FieldResult; - value = (doc:Doc) => (this._lastComputedResult = this._cachedResult ?? - computedFn((doc: Doc) => - this.script.compiled && - this.script.run( { - this: doc, - //value: '', - _setCacheResult_: this.setCacheResult, - _last_: this._lastComputedResult, - _readOnly_: true, - }, - console.log - ).result - )(doc) - ); // prettier-ignore + value = (doc: Doc) => { + this._lastComputedResult = + this._cachedResult ?? + computedFn(() => + this.script.compiled && + this.script.run( + { + this: doc, + // value: '', + _setCacheResult_: this.setCacheResult, + _last_: this._lastComputedResult, + _readOnly_: true, + }, + console.log + ).result + )(); // prettier-ignore + return this._lastComputedResult; + }; - [ToValue](doc: Doc) { if (ComputedField.useComputed) return { value: this.value(doc) }; } // prettier-ignore + [ToValue](doc: Doc) { return ComputedField.useComputed ? { value: this.value(doc) } : undefined; } // prettier-ignore [Copy](): ObjectField { return new ComputedField(this.script, this.setterscript, this.rawscript); } // prettier-ignore + // eslint-disable-next-line default-param-last public static MakeFunction(script: string, params: object = {}, capturedVariables?: { [name: string]: Doc | string | number | boolean }, setterscript?: string) { const compiled = ScriptField.CompileScript(script, params, true, { value: '', ...capturedVariables }); const compiledsetter = setterscript ? ScriptField.CompileScript(setterscript, { ...params, value: 'any' }, false, capturedVariables) : undefined; @@ -225,7 +239,7 @@ export class ComputedField extends ScriptField { public static MakeInterpolatedNumber(fieldKey: string, interpolatorKey: string, doc: Doc, curTimecode: number, defaultVal: Opt<number>) { if (!doc[`${fieldKey}_indexed`]) { - const flist = new List<number>(numberRange(curTimecode + 1).map(i => undefined) as any as number[]); + const flist = new List<number>(numberRange(curTimecode + 1).map(() => undefined) as any as number[]); flist[curTimecode] = Cast(doc[fieldKey], 'number', null); doc[`${fieldKey}_indexed`] = flist; } @@ -235,7 +249,7 @@ export class ComputedField extends ScriptField { } public static MakeInterpolatedString(fieldKey: string, interpolatorKey: string, doc: Doc, curTimecode: number) { if (!doc[`${fieldKey}_`]) { - const flist = new List<string>(numberRange(curTimecode + 1).map(i => undefined) as any as string[]); + const flist = new List<string>(numberRange(curTimecode + 1).map(() => undefined) as any as string[]); flist[curTimecode] = StrCast(doc[fieldKey]); doc[`${fieldKey}_indexed`] = flist; } @@ -244,9 +258,9 @@ export class ComputedField extends ScriptField { return getField.compiled ? new ComputedField(getField, setField?.compiled ? setField : undefined) : undefined; } public static MakeInterpolatedDataField(fieldKey: string, interpolatorKey: string, doc: Doc, curTimecode: number) { - if (doc[`${fieldKey}`] instanceof List) return; + if (doc[`${fieldKey}`] instanceof List) return undefined; if (!doc[`${fieldKey}_indexed`]) { - const flist = new List<Field>(numberRange(curTimecode + 1).map(i => undefined) as any as Field[]); + const flist = new List<FieldType>(numberRange(curTimecode + 1).map(() => undefined) as any as FieldType[]); flist[curTimecode] = Field.Copy(doc[fieldKey]); doc[`${fieldKey}_indexed`] = flist; } @@ -257,11 +271,13 @@ export class ComputedField extends ScriptField { false, {} ); - return (doc[`${fieldKey}`] = getField.compiled ? new ComputedField(getField, setField?.compiled ? setField : undefined) : undefined); + doc[fieldKey] = getField.compiled ? new ComputedField(getField, setField?.compiled ? setField : undefined) : undefined; + return doc[fieldKey]; } } ScriptingGlobals.add( + // eslint-disable-next-line prefer-arrow-callback function setIndexVal(list: any[], index: number, value: any) { while (list.length <= index) list.push(undefined); list[index] = value; @@ -271,6 +287,7 @@ ScriptingGlobals.add( ); ScriptingGlobals.add( + // eslint-disable-next-line prefer-arrow-callback function getIndexVal(list: any[], index: number, defaultVal: Opt<number> = undefined) { return list?.reduce((p, x, i) => ((i <= index && x !== undefined) || p === undefined ? x : p), defaultVal); }, @@ -279,12 +296,14 @@ ScriptingGlobals.add( ); ScriptingGlobals.add( + // eslint-disable-next-line prefer-arrow-callback function makeScript(script: string) { return ScriptField.MakeScript(script); }, 'returns the value at a given index of a list', '(list: any[], index: number)' ); +// eslint-disable-next-line prefer-arrow-callback ScriptingGlobals.add(function dashCallChat(setVal: (val: FieldResult) => void, target: Doc, queryText: string) { ScriptField.CallGpt(queryText, setVal, target); }, 'calls chat gpt for the query string and then calls setVal with the result'); |