@dxos/compute 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/AiMetering.mjs +1 -0
- package/dist/lib/Hook.mjs +1 -0
- package/dist/lib/Operation.mjs +3 -3
- package/dist/lib/Process.mjs +2 -2
- package/dist/lib/Trace.mjs +1 -1
- package/dist/lib/chunk-AgentIdentity.mjs +1 -1
- package/dist/lib/chunk-AgentService.mjs.map +1 -1
- package/dist/lib/chunk-AiMetering.mjs +77 -0
- package/dist/lib/chunk-AiMetering.mjs.map +1 -0
- package/dist/lib/chunk-Cancellation.mjs +1 -1
- package/dist/lib/chunk-Hook.mjs +78 -0
- package/dist/lib/chunk-Hook.mjs.map +1 -0
- package/dist/lib/chunk-Instructions.mjs +1 -1
- package/dist/lib/chunk-Operation.mjs +169 -5
- package/dist/lib/chunk-Operation.mjs.map +1 -1
- package/dist/lib/chunk-OperationHandlerSet.mjs +1 -1
- package/dist/lib/chunk-OperationRegistry.mjs +1 -1
- package/dist/lib/chunk-OperationRegistry.mjs.map +1 -1
- package/dist/lib/chunk-Process.mjs +18 -10
- package/dist/lib/chunk-Process.mjs.map +1 -1
- package/dist/lib/chunk-Project.mjs +52 -24
- package/dist/lib/chunk-Project.mjs.map +1 -1
- package/dist/lib/chunk-Reducer.mjs +827 -0
- package/dist/lib/chunk-Reducer.mjs.map +1 -0
- package/dist/lib/chunk-Routine.mjs +1 -1
- package/dist/lib/chunk-Runnable.mjs +1 -1
- package/dist/lib/chunk-ServiceResolver.mjs +1 -1
- package/dist/lib/chunk-Skill.mjs +10 -47
- package/dist/lib/chunk-Skill.mjs.map +1 -1
- package/dist/lib/chunk-Spec.mjs +49 -0
- package/dist/lib/chunk-Spec.mjs.map +1 -0
- package/dist/lib/chunk-SpecReview.mjs +98 -0
- package/dist/lib/chunk-SpecReview.mjs.map +1 -0
- package/dist/lib/chunk-SqlService.mjs.map +1 -1
- package/dist/lib/chunk-StorageService.mjs +1 -1
- package/dist/lib/chunk-Template.mjs +2 -2
- package/dist/lib/chunk-Trace.mjs +97 -13
- package/dist/lib/chunk-Trace.mjs.map +1 -1
- package/dist/lib/chunk-Trajectory.mjs +326 -0
- package/dist/lib/chunk-Trajectory.mjs.map +1 -0
- package/dist/lib/chunk-Transcript.mjs +84 -0
- package/dist/lib/chunk-Transcript.mjs.map +1 -0
- package/dist/lib/chunk-definitions.mjs +1 -1
- package/dist/lib/errors.mjs +3 -1
- package/dist/lib/errors.mjs.map +1 -1
- package/dist/lib/index.mjs +14 -8
- package/dist/lib/testing.mjs +2 -2
- package/dist/lib/trajectory/Reducer.mjs +1 -0
- package/dist/lib/trajectory/Trajectory.mjs +1 -0
- package/dist/lib/trajectory/Transcript.mjs +1 -0
- package/dist/lib/types/Project.mjs +1 -1
- package/dist/lib/types/Skill.mjs +1 -1
- package/dist/lib/types/Spec.mjs +1 -0
- package/dist/lib/types/SpecReview.mjs +1 -0
- package/dist/types/src/AgentService.d.ts +9 -0
- package/dist/types/src/AgentService.d.ts.map +1 -1
- package/dist/types/src/AiMetering.d.ts +23 -0
- package/dist/types/src/AiMetering.d.ts.map +1 -0
- package/dist/types/src/AiMetering.test.d.ts +2 -0
- package/dist/types/src/AiMetering.test.d.ts.map +1 -0
- package/dist/types/src/Hook.d.ts +84 -0
- package/dist/types/src/Hook.d.ts.map +1 -0
- package/dist/types/src/Hook.test.d.ts +2 -0
- package/dist/types/src/Hook.test.d.ts.map +1 -0
- package/dist/types/src/Operation.d.ts +276 -2
- package/dist/types/src/Operation.d.ts.map +1 -1
- package/dist/types/src/Process.d.ts +26 -4
- package/dist/types/src/Process.d.ts.map +1 -1
- package/dist/types/src/Script.test.d.ts +2 -0
- package/dist/types/src/Script.test.d.ts.map +1 -0
- package/dist/types/src/Trace.d.ts +103 -1
- package/dist/types/src/Trace.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +30 -0
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/trajectory/Reducer.d.ts +191 -0
- package/dist/types/src/trajectory/Reducer.d.ts.map +1 -0
- package/dist/types/src/trajectory/Reducer.test.d.ts +2 -0
- package/dist/types/src/trajectory/Reducer.test.d.ts.map +1 -0
- package/dist/types/src/trajectory/Trajectory.d.ts +3211 -0
- package/dist/types/src/trajectory/Trajectory.d.ts.map +1 -0
- package/dist/types/src/trajectory/Trajectory.test.d.ts +2 -0
- package/dist/types/src/trajectory/Trajectory.test.d.ts.map +1 -0
- package/dist/types/src/trajectory/Transcript.d.ts +40 -0
- package/dist/types/src/trajectory/Transcript.d.ts.map +1 -0
- package/dist/types/src/trajectory/index.d.ts +4 -0
- package/dist/types/src/trajectory/index.d.ts.map +1 -0
- package/dist/types/src/trajectory/light-threads.test.d.ts +2 -0
- package/dist/types/src/trajectory/light-threads.test.d.ts.map +1 -0
- package/dist/types/src/trajectory/samples.test.d.ts +2 -0
- package/dist/types/src/trajectory/samples.test.d.ts.map +1 -0
- package/dist/types/src/types/Project.d.ts +11 -12
- package/dist/types/src/types/Project.d.ts.map +1 -1
- package/dist/types/src/types/Skill.d.ts +6 -51
- package/dist/types/src/types/Skill.d.ts.map +1 -1
- package/dist/types/src/types/Spec.d.ts +26 -0
- package/dist/types/src/types/Spec.d.ts.map +1 -0
- package/dist/types/src/types/SpecReview.d.ts +76 -0
- package/dist/types/src/types/SpecReview.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +59 -26
- package/src/AgentService.ts +9 -0
- package/src/AiMetering.test.ts +89 -0
- package/src/AiMetering.ts +93 -0
- package/src/Hook.test.ts +75 -0
- package/src/Hook.ts +154 -0
- package/src/Operation.test.ts +23 -0
- package/src/Operation.ts +334 -0
- package/src/Process.ts +33 -6
- package/src/Script.test.ts +74 -0
- package/src/Trace.ts +97 -4
- package/src/errors.ts +3 -0
- package/src/index.ts +3 -1
- package/src/trajectory/Reducer.test.ts +575 -0
- package/src/trajectory/Reducer.ts +1026 -0
- package/src/trajectory/Trajectory.test.ts +114 -0
- package/src/trajectory/Trajectory.ts +440 -0
- package/src/trajectory/Transcript.ts +113 -0
- package/src/trajectory/index.ts +7 -0
- package/src/trajectory/light-threads.test.ts +310 -0
- package/src/trajectory/samples.test.ts +285 -0
- package/src/types/Project.test.ts +2 -1
- package/src/types/Project.ts +43 -27
- package/src/types/Skill.ts +6 -61
- package/src/types/Spec.ts +68 -0
- package/src/types/SpecReview.ts +98 -0
- package/src/types/index.ts +2 -0
- package/dist/lib/McpServer.mjs +0 -1
- package/dist/lib/chunk-McpServer.mjs +0 -124
- package/dist/lib/chunk-McpServer.mjs.map +0 -1
- package/dist/types/src/McpServer.d.ts +0 -113
- package/dist/types/src/McpServer.d.ts.map +0 -1
- package/src/McpServer.ts +0 -142
package/src/Operation.ts
CHANGED
|
@@ -21,6 +21,7 @@ import * as Struct from 'effect/Struct';
|
|
|
21
21
|
import type * as Types from 'effect/Types';
|
|
22
22
|
|
|
23
23
|
import { Annotation, DXN, JsonSchema, type Key, Migration, Obj, Ref, Type } from '@dxos/echo';
|
|
24
|
+
import { BaseError } from '@dxos/errors';
|
|
24
25
|
import { assertArgument, invariant } from '@dxos/invariant';
|
|
25
26
|
import type { URI } from '@dxos/keys';
|
|
26
27
|
import { log } from '@dxos/log';
|
|
@@ -30,6 +31,7 @@ import type { Operation } from './index.ts';
|
|
|
30
31
|
import type * as Process from './Process.ts';
|
|
31
32
|
import type * as StorageService from './StorageService.ts';
|
|
32
33
|
import type * as Trace from './Trace.ts';
|
|
34
|
+
import type * as Script from './types/Script.ts';
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Schema type that accepts any Encoded form but requires no Context.
|
|
@@ -468,6 +470,9 @@ export interface DurableDefinition<_Input, _Output, _Requirements = never, _Rpcs
|
|
|
468
470
|
|
|
469
471
|
readonly services: readonly Context.Key<any, any>[];
|
|
470
472
|
|
|
473
|
+
/** See {@link DurableProps.optionalServices}. */
|
|
474
|
+
readonly optionalServices?: readonly Context.Key<any, any>[];
|
|
475
|
+
|
|
471
476
|
/**
|
|
472
477
|
* Codecs for the process's inputs and outputs, from {@link DurableProps}. Exposed on the
|
|
473
478
|
* interface so a caller that moves a value across a boundary (a remote runtime) can encode it
|
|
@@ -479,6 +484,9 @@ export interface DurableDefinition<_Input, _Output, _Requirements = never, _Rpcs
|
|
|
479
484
|
/** Schemas to register with the process's database; see {@link DurableProps.types}. */
|
|
480
485
|
readonly types?: readonly Type.AnyEntity[];
|
|
481
486
|
|
|
487
|
+
/** See {@link DurableProps.resumable}. */
|
|
488
|
+
readonly resumable?: boolean;
|
|
489
|
+
|
|
482
490
|
// Runtime RPC group, stored as `any`. `RpcGroup`/`RpcClient` are invariant in their type
|
|
483
491
|
// argument (and `DurableHandler.rpcHandlers` is contravariant in it), so referencing `_Rpcs` in the
|
|
484
492
|
// structural fields would block `Durable<…, never>` from being assignable to `Durable.Any`.
|
|
@@ -583,6 +591,12 @@ export interface DurableProps {
|
|
|
583
591
|
readonly input: Schema.Codec<any, any>;
|
|
584
592
|
readonly output: Schema.Codec<any, any>;
|
|
585
593
|
readonly services: readonly Context.Key<any, any>[];
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Services the process uses when the runtime has them: it gets each one that resolves and runs without the rest,
|
|
597
|
+
* so the handler reads them with `Effect.serviceOption`.
|
|
598
|
+
*/
|
|
599
|
+
readonly optionalServices?: readonly Context.Key<any, any>[];
|
|
586
600
|
readonly rpcs?: RpcGroup.RpcGroup<any>;
|
|
587
601
|
|
|
588
602
|
/**
|
|
@@ -594,6 +608,12 @@ export interface DurableProps {
|
|
|
594
608
|
* rather than a missing schema.
|
|
595
609
|
*/
|
|
596
610
|
readonly types?: readonly Type.AnyEntity[];
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* False when a process interrupted by a restart could only fail on resuming. Such a process is kept in
|
|
614
|
+
* memory rather than persisted, unless a persisted parent waits on it. Defaults to true.
|
|
615
|
+
*/
|
|
616
|
+
readonly resumable?: boolean;
|
|
597
617
|
}
|
|
598
618
|
|
|
599
619
|
/**
|
|
@@ -683,6 +703,278 @@ const sanitizeRpcs = <Rpcs extends Rpc.Any>(
|
|
|
683
703
|
return provided;
|
|
684
704
|
};
|
|
685
705
|
|
|
706
|
+
//
|
|
707
|
+
// Script operations.
|
|
708
|
+
//
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Language an inline script snippet is written in.
|
|
712
|
+
*
|
|
713
|
+
* Carried by the snippet rather than by the runtime, because one runtime serves several: a code-mode
|
|
714
|
+
* turn writes `plain` or `effect` against the same host that runs a `typescript` module.
|
|
715
|
+
*/
|
|
716
|
+
export const ScriptDialect = Schema$.Literals(['plain', 'effect', 'javascript', 'typescript']);
|
|
717
|
+
export type ScriptDialect = Schema$.Schema.Type<typeof ScriptDialect>;
|
|
718
|
+
|
|
719
|
+
/** Named {@link ScriptDialect} values, so a caller names one without spelling the literal. */
|
|
720
|
+
export const ScriptDialects = {
|
|
721
|
+
/** Code-mode's plain dialect: `await`-style bindings over the workspace. */
|
|
722
|
+
Plain: 'plain',
|
|
723
|
+
/** Code-mode's Effect dialect: the body of an `Effect.gen` generator. */
|
|
724
|
+
Effect: 'effect',
|
|
725
|
+
JavaScript: 'javascript',
|
|
726
|
+
TypeScript: 'typescript',
|
|
727
|
+
} as const satisfies Record<string, ScriptDialect>;
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* A snippet carried by the operation itself, in the dialect it is written in.
|
|
731
|
+
*
|
|
732
|
+
* Declared as a type here and as a schema in `@dxos/compute-runtime/script-runner`, because the schema of a {@link ScriptRef}
|
|
733
|
+
* needs the `Script` object type, whose module reaches the whole `@dxos/schema` barrel: the kind is on
|
|
734
|
+
* every app's boot path through this module, and the schema is not.
|
|
735
|
+
*/
|
|
736
|
+
export type InlineScript = {
|
|
737
|
+
readonly _tag: 'inline';
|
|
738
|
+
readonly code: string;
|
|
739
|
+
readonly dialect: ScriptDialect;
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* A script stored in ECHO as a `Script` object.
|
|
744
|
+
*
|
|
745
|
+
* The dialect is the object's own (its editor compiles TypeScript), so unlike {@link InlineScript} it
|
|
746
|
+
* is not restated here — a snippet has no object to ask.
|
|
747
|
+
*/
|
|
748
|
+
export type ScriptRef = {
|
|
749
|
+
readonly _tag: 'object';
|
|
750
|
+
readonly script: Ref.Ref<Script.Script>;
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
/** Where a script operation's code comes from: a snippet it carries, or an ECHO object it points at. */
|
|
754
|
+
export type ScriptSource = InlineScript | ScriptRef;
|
|
755
|
+
|
|
756
|
+
/** Options {@link inlineScript} takes. */
|
|
757
|
+
export type InlineScriptOptions = {
|
|
758
|
+
/** The language the snippet is written in; TypeScript when unsaid. */
|
|
759
|
+
readonly dialect?: ScriptDialect;
|
|
760
|
+
};
|
|
761
|
+
|
|
762
|
+
/** A snippet source. */
|
|
763
|
+
export const inlineScript = (code: string, options?: InlineScriptOptions): InlineScript => ({
|
|
764
|
+
_tag: 'inline',
|
|
765
|
+
code,
|
|
766
|
+
dialect: options?.dialect ?? ScriptDialects.TypeScript,
|
|
767
|
+
});
|
|
768
|
+
|
|
769
|
+
/** A source pointing at a `Script` object. */
|
|
770
|
+
export const scriptRef = (script: Ref.Ref<Script.Script>): ScriptRef => ({ _tag: 'object', script });
|
|
771
|
+
|
|
772
|
+
export const isInlineScript = (source: ScriptSource): source is InlineScript => source._tag === 'inline';
|
|
773
|
+
export const isScriptRef = (source: ScriptSource): source is ScriptRef => source._tag === 'object';
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* A script that could not be run: an unsupported dialect, a source that would not resolve, or a
|
|
777
|
+
* runtime with no way to run scripts at all.
|
|
778
|
+
*
|
|
779
|
+
* A failure the SCRIPT raised is not one of these — that is a {@link ScriptResult} carrying `error`,
|
|
780
|
+
* since the caller (a model writing code, a person at `dx eval`) is meant to read it and write
|
|
781
|
+
* different code rather than see the call fail.
|
|
782
|
+
*/
|
|
783
|
+
export class ScriptError extends BaseError.extend('ScriptError', 'Script could not be run') {}
|
|
784
|
+
|
|
785
|
+
/** The host calls one run made: how many, and the wall time spent waiting on them. */
|
|
786
|
+
export const ScriptStats = Schema$.Struct({
|
|
787
|
+
calls: Schema$.Number,
|
|
788
|
+
callMs: Schema$.Number,
|
|
789
|
+
});
|
|
790
|
+
export type ScriptStats = Schema$.Schema.Type<typeof ScriptStats>;
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* One run, as a runtime's script backend receives it.
|
|
794
|
+
*
|
|
795
|
+
* Every field but the source is a scalar, so the same request runs in this process, on a worker, or
|
|
796
|
+
* on EDGE without being rebuilt (its schema is `ScriptRunner.Request` in `@dxos/compute-runtime`). What a request cannot carry
|
|
797
|
+
* is the workspace: the bindings the code runs against are the backend's, built wherever it lives.
|
|
798
|
+
*/
|
|
799
|
+
export type ScriptRequest = {
|
|
800
|
+
readonly source: ScriptSource;
|
|
801
|
+
/**
|
|
802
|
+
* Input handed to the script, bound under `input` by a backend whose dialect can carry it. A
|
|
803
|
+
* backend that evaluates out of process builds its bindings on the far side and may not, so a
|
|
804
|
+
* script that must have its input should read it from the database instead.
|
|
805
|
+
*/
|
|
806
|
+
readonly input?: unknown;
|
|
807
|
+
/** Space the script's calls default to; absent runs against the backend's own default. */
|
|
808
|
+
readonly spaceId?: string;
|
|
809
|
+
/** Wall-clock bound on the run, in milliseconds. */
|
|
810
|
+
readonly timeoutMs?: number;
|
|
811
|
+
/** Characters of printed output returned before truncation. */
|
|
812
|
+
readonly maxOutput?: number;
|
|
813
|
+
/** Skill tokens the caller already holds, which the script's `invoke` presents for it. */
|
|
814
|
+
readonly skillTokens?: readonly string[];
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* What a run produced: what the script printed, why it failed when it did, and the calls it made.
|
|
819
|
+
*
|
|
820
|
+
* This is a script operation's default `output`, so `Operation.invoke` on one answers with it.
|
|
821
|
+
*/
|
|
822
|
+
export const ScriptResult = Schema$.Struct({
|
|
823
|
+
output: Schema$.String,
|
|
824
|
+
/** Present exactly when the script itself failed; `output` then holds what it printed first. */
|
|
825
|
+
error: Schema$.optional(Schema$.String),
|
|
826
|
+
stats: Schema$.optional(ScriptStats),
|
|
827
|
+
});
|
|
828
|
+
export type ScriptResult = Schema$.Schema.Type<typeof ScriptResult>;
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* What a script's body may reach: the operation invoker and the process manager of the process the
|
|
832
|
+
* script is running as.
|
|
833
|
+
*
|
|
834
|
+
* The running script's own, not the caller's, which is what makes a script's calls and the processes
|
|
835
|
+
* it spawns belong to the script's process — a script delegates work the way a handler does.
|
|
836
|
+
*/
|
|
837
|
+
export type ScriptRequirements = Service | Process.ManagerService;
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* The process a script is running as, as its backend sees it.
|
|
841
|
+
*
|
|
842
|
+
* Passed rather than required, so a backend that evaluates code against the workspace can hand the
|
|
843
|
+
* script its own process (`program.pipe(Effect.provide(runtime.context))`) while one that answers
|
|
844
|
+
* through its own host — an MCP server, a remote isolate — declares `run(request)` alone and stays
|
|
845
|
+
* requirement-free.
|
|
846
|
+
*/
|
|
847
|
+
export interface ScriptRuntime {
|
|
848
|
+
readonly context: Context.Context<ScriptRequirements>;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Runs scripts for a runtime.
|
|
853
|
+
*
|
|
854
|
+
* Not a client-facing API: a caller invokes a script operation like any other operation, and the
|
|
855
|
+
* runtime it runs on reaches for this. Several backends implement it — in-process evaluation, a
|
|
856
|
+
* worker thread that can be terminated, a workerd isolate that contains the code — which is why the
|
|
857
|
+
* runtime takes it as a dependency rather than embedding one.
|
|
858
|
+
*/
|
|
859
|
+
export interface ScriptRunner {
|
|
860
|
+
readonly run: (request: ScriptRequest, runtime?: ScriptRuntime) => Effect.Effect<ScriptResult, ScriptError>;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* The dialect a source is written in. A {@link ScriptRef} is TypeScript: a `Script` object is what the
|
|
865
|
+
* script editor writes, and it compiles TypeScript.
|
|
866
|
+
*/
|
|
867
|
+
export const scriptDialectOf = (source: ScriptSource): ScriptDialect =>
|
|
868
|
+
isInlineScript(source) ? source.dialect : ScriptDialects.TypeScript;
|
|
869
|
+
|
|
870
|
+
/** The request that runs `operation` on `input`. */
|
|
871
|
+
export const scriptRequest = (
|
|
872
|
+
operation: ScriptDefinition.Any,
|
|
873
|
+
input?: unknown,
|
|
874
|
+
options?: Omit<ScriptRequest, 'source' | 'input' | 'timeoutMs'>,
|
|
875
|
+
): ScriptRequest => ({
|
|
876
|
+
...options,
|
|
877
|
+
source: operation.source,
|
|
878
|
+
...(input !== undefined ? { input } : {}),
|
|
879
|
+
...(operation.timeout !== undefined ? { timeoutMs: operation.timeout } : {}),
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
export const ScriptDefinitionTypeId = '~@dxos/operation/ScriptDefinition' as const;
|
|
883
|
+
export type ScriptDefinitionTypeId = typeof ScriptDefinitionTypeId;
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* The third kind of operation, beside a non-durable {@link Definition} with a handler and a
|
|
887
|
+
* {@link Durable} one: an operation whose body is a script.
|
|
888
|
+
*
|
|
889
|
+
* It IS a {@link Definition} — `Operation.invoke(script, input, { on })` is the whole client API —
|
|
890
|
+
* and carries no handler at all. The code is data, so the definition crosses a boundary whole and the
|
|
891
|
+
* runtime that runs it is picked at call time; a runtime that cannot run scripts says so when asked,
|
|
892
|
+
* rather than the caller needing a different verb.
|
|
893
|
+
*/
|
|
894
|
+
export interface ScriptDefinition<I, O> extends Definition<I, O> {
|
|
895
|
+
readonly [ScriptDefinitionTypeId]: {
|
|
896
|
+
readonly _Script: Types.Covariant<ScriptSource>;
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
readonly source: ScriptSource;
|
|
900
|
+
|
|
901
|
+
/** Wall-clock bound on one run, in milliseconds; absent leaves the bound to the runtime. */
|
|
902
|
+
readonly timeout?: number;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export declare namespace ScriptDefinition {
|
|
906
|
+
export type Any = ScriptDefinition<any, any>;
|
|
907
|
+
|
|
908
|
+
export type Input<T extends Any> = Definition.Input<T>;
|
|
909
|
+
export type Output<T extends Any> = Definition.Output<T>;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Props {@link makeScript} takes. `input` defaults to `Unknown` and `output` to {@link ScriptResult},
|
|
914
|
+
* since a script answers with what it printed and no dialect returns a typed value yet.
|
|
915
|
+
*/
|
|
916
|
+
export type ScriptProps = {
|
|
917
|
+
readonly meta: Definition<any, any>['meta'];
|
|
918
|
+
readonly source: ScriptSource;
|
|
919
|
+
readonly timeout?: number;
|
|
920
|
+
readonly input?: Schema$.Codec<any, any, never, never>;
|
|
921
|
+
readonly output?: Schema$.Codec<any, any, never, never>;
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Creates a script operation, which is invoked like any other operation.
|
|
926
|
+
*
|
|
927
|
+
* @example
|
|
928
|
+
* ```ts
|
|
929
|
+
* const result = yield* Operation.invoke(
|
|
930
|
+
* Operation.makeScript({
|
|
931
|
+
* meta: { key: DXN.make('example.hello') },
|
|
932
|
+
* source: Operation.inlineScript('yield* print("hi")', { dialect: Operation.ScriptDialects.Effect }),
|
|
933
|
+
* }),
|
|
934
|
+
* undefined,
|
|
935
|
+
* { on: 'local' },
|
|
936
|
+
* );
|
|
937
|
+
* ```
|
|
938
|
+
*/
|
|
939
|
+
export const makeScript = <const P extends Types.NoExcessProperties<ScriptProps, P>>(
|
|
940
|
+
props: P,
|
|
941
|
+
): ScriptDefinition<
|
|
942
|
+
P['input'] extends Schema$.Codec<any, any, any, any> ? Schema$.Schema.Type<P['input']> : unknown,
|
|
943
|
+
P['output'] extends Schema$.Codec<any, any, any, any> ? Schema$.Schema.Type<P['output']> : ScriptResult
|
|
944
|
+
> =>
|
|
945
|
+
({
|
|
946
|
+
[DefinitionTypeId]: {},
|
|
947
|
+
[ScriptDefinitionTypeId]: {},
|
|
948
|
+
...props,
|
|
949
|
+
input: props.input ?? Schema$.Unknown,
|
|
950
|
+
output: props.output ?? ScriptResult,
|
|
951
|
+
executionMode: 'async',
|
|
952
|
+
types: [],
|
|
953
|
+
services: [],
|
|
954
|
+
pipe() {
|
|
955
|
+
// eslint-disable-next-line prefer-rest-params
|
|
956
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
957
|
+
},
|
|
958
|
+
}) as any;
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Whether an operation is the script kind, which a runtime runs rather than looking up a handler for.
|
|
962
|
+
*
|
|
963
|
+
* Narrows a typed definition to the script kind WITH its own input and output types, so a runtime
|
|
964
|
+
* that branches on it keeps the operation's codecs — without them the branch could only be reconciled
|
|
965
|
+
* with a cast.
|
|
966
|
+
*/
|
|
967
|
+
export const isScript: {
|
|
968
|
+
<I, O>(op: Definition<I, O>): op is ScriptDefinition<I, O>;
|
|
969
|
+
<I, O, R, Rpcs extends Rpc.Any>(op: Durable<I, O, R, Rpcs> | ScriptDefinition<I, O>): op is ScriptDefinition<I, O>;
|
|
970
|
+
(value: unknown): value is ScriptDefinition.Any;
|
|
971
|
+
} = (value: unknown): value is ScriptDefinition.Any =>
|
|
972
|
+
typeof value === 'object' && value !== null && ScriptDefinitionTypeId in value;
|
|
973
|
+
|
|
974
|
+
/** Whether `value` names a known {@link ScriptDialect}. */
|
|
975
|
+
export const isScriptDialect = (value: unknown): value is ScriptDialect =>
|
|
976
|
+
typeof value === 'string' && (ScriptDialect.literals as readonly string[]).includes(value);
|
|
977
|
+
|
|
686
978
|
//
|
|
687
979
|
// Tool projection
|
|
688
980
|
//
|
|
@@ -1116,6 +1408,47 @@ export const isVisible = (op: PersistentOperation): boolean =>
|
|
|
1116
1408
|
*/
|
|
1117
1409
|
export const idempotent = annotate(IdempotentAnnotation, true);
|
|
1118
1410
|
|
|
1411
|
+
/**
|
|
1412
|
+
* What an operation requires of the process that invokes it, in the vocabulary of the permission
|
|
1413
|
+
* system that interprets it (`@dxos/permission` requirements). Present ⇒ the runtime refuses the
|
|
1414
|
+
* call unless the invoking process holds a guard that admits it; absent ⇒ the operation is never
|
|
1415
|
+
* checked, which is what keeps permissions opt-in.
|
|
1416
|
+
*/
|
|
1417
|
+
export const RequiresAnnotation = Annotation.make({
|
|
1418
|
+
id: 'org.dxos.operation.requires',
|
|
1419
|
+
schema: Schema$.Array(Schema$.Unknown),
|
|
1420
|
+
});
|
|
1421
|
+
|
|
1422
|
+
/** The operation's declared requirements; empty when it declares none. */
|
|
1423
|
+
export const getRequirements = (op: Definition.Any): readonly unknown[] =>
|
|
1424
|
+
op.meta.annotations
|
|
1425
|
+
? Option.getOrElse(Annotation.getDictionary(op.meta.annotations, RequiresAnnotation), (): readonly unknown[] => [])
|
|
1426
|
+
: [];
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* The input field a consent prompt shows before the person allows the call (the body of an email it
|
|
1430
|
+
* would send), so they approve what is sent and not only that something is.
|
|
1431
|
+
*/
|
|
1432
|
+
export const ConsentPreviewAnnotation = Annotation.make({
|
|
1433
|
+
id: 'org.dxos.operation.consentPreview',
|
|
1434
|
+
schema: Schema$.Struct({ field: Schema$.String, label: Schema$.String }),
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
/** The text a consent prompt previews for this call, when the operation names a string field to show. */
|
|
1438
|
+
export const getConsentPreview = (
|
|
1439
|
+
op: Definition.Any,
|
|
1440
|
+
input: unknown,
|
|
1441
|
+
): { readonly label: string; readonly text: string } | undefined => {
|
|
1442
|
+
const preview = op.meta.annotations
|
|
1443
|
+
? Annotation.getDictionary(op.meta.annotations, ConsentPreviewAnnotation)
|
|
1444
|
+
: Option.none();
|
|
1445
|
+
if (Option.isNone(preview) || input === null || typeof input !== 'object') {
|
|
1446
|
+
return undefined;
|
|
1447
|
+
}
|
|
1448
|
+
const text: unknown = Reflect.get(input, preview.value.field);
|
|
1449
|
+
return typeof text === 'string' && text.trim().length > 0 ? { label: preview.value.label, text } : undefined;
|
|
1450
|
+
};
|
|
1451
|
+
|
|
1119
1452
|
/**
|
|
1120
1453
|
* Operation service interface - provides unified access to operation invocation and scheduling.
|
|
1121
1454
|
* This service is automatically provided to operation handlers.
|
|
@@ -1292,4 +1625,5 @@ export {
|
|
|
1292
1625
|
InvalidOperationOutputError,
|
|
1293
1626
|
InvokerNotInitializedError,
|
|
1294
1627
|
NoHandlerError,
|
|
1628
|
+
OperationDeniedError,
|
|
1295
1629
|
} from './errors.ts';
|
package/src/Process.ts
CHANGED
|
@@ -351,6 +351,22 @@ export interface Process<_Input = any, _Output = any, _Rpcs extends Rpc.Any = an
|
|
|
351
351
|
// `any` Rpcs keeps every process assignable regardless of its concrete RPC group (variance, see design spec §4.4).
|
|
352
352
|
export type Any = Process<any, any, any>;
|
|
353
353
|
|
|
354
|
+
/**
|
|
355
|
+
* Admits or refuses an operation a process invokes, for operations that declare requirements
|
|
356
|
+
* (`Operation.RequiresAnnotation`). Refuses by dying with `Operation.OperationDeniedError`.
|
|
357
|
+
*/
|
|
358
|
+
export interface Guard {
|
|
359
|
+
readonly check: (op: Operation.Definition.Any, input: unknown) => Effect.Effect<void>;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** The guard of the running process; absent when it was spawned without one. */
|
|
363
|
+
export class GuardService extends Context.Service<GuardService, Guard>()('@dxos/compute/Process.GuardService') {}
|
|
364
|
+
|
|
365
|
+
/** A guard that admits only what both admit, so a child spawned with its own guard never widens its parent's. */
|
|
366
|
+
export const bothGuards = (first: Guard, second: Guard): Guard => ({
|
|
367
|
+
check: (op, input) => first.check(op, input).pipe(Effect.andThen(second.check(op, input))),
|
|
368
|
+
});
|
|
369
|
+
|
|
354
370
|
/**
|
|
355
371
|
* Options for spawning a process.
|
|
356
372
|
*/
|
|
@@ -381,6 +397,13 @@ export interface SpawnOptions {
|
|
|
381
397
|
|
|
382
398
|
readonly environment?: Environment;
|
|
383
399
|
|
|
400
|
+
/**
|
|
401
|
+
* Admits the operations this process invokes that declare requirements. Inherited by children;
|
|
402
|
+
* a child's own guard is combined with its parent's, never replaces it. Not persisted, so a
|
|
403
|
+
* rehydrated process holds none and refuses operations that declare requirements.
|
|
404
|
+
*/
|
|
405
|
+
readonly guard?: Guard;
|
|
406
|
+
|
|
384
407
|
/**
|
|
385
408
|
* Who the process's database writes are attributed to (see `Database.Origin`); also the origin of every process it
|
|
386
409
|
* invokes. Persisted with the process, so a restored process keeps it.
|
|
@@ -465,10 +488,13 @@ export interface Manager {
|
|
|
465
488
|
* Spawn a process at `options.location`. A remote host is sent only the definition's key; the
|
|
466
489
|
* definition stays local, supplying the codecs and RPC group the handle is typed by.
|
|
467
490
|
*
|
|
491
|
+
* A script operation spawns like a durable one: the runtime runs its code itself, so the run is a
|
|
492
|
+
* process like any other — a pid, a parent, its guard, children of its own.
|
|
493
|
+
*
|
|
468
494
|
* Dies when the location is a remote runtime that offers no process control.
|
|
469
495
|
*/
|
|
470
496
|
spawn<I, O, Rpcs extends Rpc.Any = never>(
|
|
471
|
-
definition: Operation.Durable<I, O, any, Rpcs>,
|
|
497
|
+
definition: Operation.Durable<I, O, any, Rpcs> | Operation.ScriptDefinition<I, O>,
|
|
472
498
|
options?: SpawnOptions & LocationOptions,
|
|
473
499
|
): Effect.Effect<Process<I, O, Rpcs>>;
|
|
474
500
|
|
|
@@ -528,18 +554,19 @@ export class ManagerService extends Context.Service<ManagerService, Manager>()(
|
|
|
528
554
|
) {}
|
|
529
555
|
|
|
530
556
|
/**
|
|
531
|
-
* Spawns `
|
|
532
|
-
* {@link awaitOutput}.
|
|
557
|
+
* Spawns `definition` — a durable operation or a script operation — through the ambient
|
|
558
|
+
* {@link ManagerService} and submits `input`; read its result with {@link awaitOutput}.
|
|
533
559
|
*/
|
|
534
560
|
export const spawn = <I, O, Rpcs extends Rpc.Any = never>(
|
|
535
|
-
|
|
561
|
+
definition: Operation.Durable<I, O, any, Rpcs> | Operation.ScriptDefinition<I, O>,
|
|
536
562
|
input: I,
|
|
537
563
|
options?: SpawnOptions & LocationOptions,
|
|
538
564
|
): Effect.Effect<Process<I, O, Rpcs>, never, ManagerService> =>
|
|
539
565
|
Effect.gen(function* () {
|
|
540
566
|
const manager = yield* ManagerService;
|
|
541
|
-
const
|
|
542
|
-
|
|
567
|
+
const name = Operation.isScript(definition) ? definition.meta.name : definition.name;
|
|
568
|
+
const handle = yield* manager.spawn(definition, {
|
|
569
|
+
...(name !== undefined ? { name } : {}),
|
|
543
570
|
...options,
|
|
544
571
|
});
|
|
545
572
|
yield* handle.submitInput(input);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import { describe, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { Ref } from '@dxos/echo';
|
|
9
|
+
import { DXN } from '@dxos/keys';
|
|
10
|
+
|
|
11
|
+
import * as Operation from './Operation.ts';
|
|
12
|
+
import * as Script from './types/Script.ts';
|
|
13
|
+
|
|
14
|
+
const KEY = DXN.make('com.example.operation.script');
|
|
15
|
+
|
|
16
|
+
describe('script operations', () => {
|
|
17
|
+
const script = Operation.makeScript({
|
|
18
|
+
meta: { key: KEY, name: 'Inline Script' },
|
|
19
|
+
source: Operation.inlineScript('print(1)', { dialect: Operation.ScriptDialects.Effect }),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('a script operation is an operation, which is what lets `invoke` take it', ({ expect }) => {
|
|
23
|
+
expect(Operation.isOperationDefinition(script)).toBe(true);
|
|
24
|
+
expect(Operation.isScript(script)).toBe(true);
|
|
25
|
+
// It carries no handler and is not a durable operation: the runtime runs it.
|
|
26
|
+
expect(Operation.isOperationWithHandler(script)).toBe(false);
|
|
27
|
+
expect(Operation.isDurable(script)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('its output is the run result, so an invoking caller reads what the script printed', ({ expect }) => {
|
|
31
|
+
expect(Schema.decodeUnknownSync(Schema.toType(script.output))({ output: 'hi' })).toEqual({ output: 'hi' });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('an operation that declares its own output keeps it', ({ expect }) => {
|
|
35
|
+
const typed = Operation.makeScript({
|
|
36
|
+
meta: { key: KEY },
|
|
37
|
+
source: Operation.inlineScript('print(1)', { dialect: Operation.ScriptDialects.Plain }),
|
|
38
|
+
output: Schema.Number,
|
|
39
|
+
});
|
|
40
|
+
expect(Schema.decodeUnknownSync(Schema.toType(typed.output))(3)).toBe(3);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('an inline source carries its dialect; an object source is TypeScript', ({ expect }) => {
|
|
44
|
+
expect(Operation.isInlineScript(script.source)).toBe(true);
|
|
45
|
+
expect(Operation.scriptDialectOf(script.source)).toBe('effect');
|
|
46
|
+
expect(Operation.scriptDialectOf(Operation.scriptRef(Ref.make(Script.make())))).toBe('typescript');
|
|
47
|
+
expect(Operation.isScriptDialect('effect')).toBe(true);
|
|
48
|
+
expect(Operation.isScriptDialect('cobol')).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('Operation.scriptRequest', () => {
|
|
53
|
+
test('carries the operation source, its timeout and the input', ({ expect }) => {
|
|
54
|
+
const operation = Operation.makeScript({
|
|
55
|
+
meta: { key: KEY, name: 'Example' },
|
|
56
|
+
source: Operation.inlineScript('print(input)', { dialect: Operation.ScriptDialects.Effect }),
|
|
57
|
+
timeout: 5_000,
|
|
58
|
+
});
|
|
59
|
+
expect(Operation.scriptRequest(operation, { a: 1 }, { spaceId: 'SPACE' })).toEqual({
|
|
60
|
+
source: operation.source,
|
|
61
|
+
input: { a: 1 },
|
|
62
|
+
timeoutMs: 5_000,
|
|
63
|
+
spaceId: 'SPACE',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('omits an absent input rather than sending undefined', ({ expect }) => {
|
|
68
|
+
const operation = Operation.makeScript({
|
|
69
|
+
meta: { key: KEY },
|
|
70
|
+
source: Operation.inlineScript('print(1)', { dialect: Operation.ScriptDialects.Plain }),
|
|
71
|
+
});
|
|
72
|
+
expect(Operation.scriptRequest(operation)).toEqual({ source: operation.source });
|
|
73
|
+
});
|
|
74
|
+
});
|