Skip to content

Commit

Permalink
feat: substantial function secrets support (#908)
Browse files Browse the repository at this point in the history
- Add support for passing secrets to substantial workflows
- Bump version to 0.5.0-rc5

---

- [x] The change comes with new or modified tests
  • Loading branch information
Yohe-Am authored Nov 10, 2024
1 parent d69f9ce commit d669167
Show file tree
Hide file tree
Showing 43 changed files with 619 additions and 359 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude = [
]

[workspace.package]
version = "0.5.0-rc.4"
version = "0.5.0-rc.5"
edition = "2021"

[workspace.dependencies]
Expand Down
116 changes: 116 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/templates/deno/api/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Policy, t, typegraph } from "jsr:@typegraph/sdk@0.5.0-rc.4/index.ts";
import { PythonRuntime } from "jsr:@typegraph/sdk@0.5.0-rc.4/runtimes/python.ts";
import { DenoRuntime } from "jsr:@typegraph/sdk@0.5.0-rc.4/runtimes/deno.ts";
import { Policy, t, typegraph } from "jsr:@typegraph/sdk@0.5.0-rc.5/index.ts";
import { PythonRuntime } from "jsr:@typegraph/sdk@0.5.0-rc.5/runtimes/python.ts";
import { DenoRuntime } from "jsr:@typegraph/sdk@0.5.0-rc.5/runtimes/deno.ts";

await typegraph("example", (g) => {
const pub = Policy.public();
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/deno/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.4
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.5
restart: always
ports:
- "7890:7890"
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/node/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.4
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.5
restart: always
ports:
- "7890:7890"
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "MCLI_LOADER_CMD='npm x tsx' meta dev"
},
"dependencies": {
"@typegraph/sdk": "^0.5.0-rc.4"
"@typegraph/sdk": "^0.5.0-rc.5"
},
"devDependencies": {
"tsx": "^3.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/python/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.4
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.5
restart: always
ports:
- "7890:7890"
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[tool.poetry]
name = "example"
version = "0.5.0-rc.4"
version = "0.5.0-rc.5"
description = ""
authors = []

[tool.poetry.dependencies]
python = ">=3.8,<4.0"
typegraph = "0.5.0-rc.4"
typegraph = "0.5.0-rc.5"

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[tool.poetry]
name = "metatype"
version = "0.5.0-rc.4"
version = "0.5.0-rc.5"
description = ""
authors = []

Expand Down
2 changes: 1 addition & 1 deletion src/meta-lsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "VSCode extension for Metatype support",
"icon": "logo.png",
"author": "Metatype Team",
"version": "0.5.0-rc.4",
"version": "0.5.0-rc.5",
"repository": {
"type": "git",
"url": "https://github.com/metatypedev/metatype"
Expand Down
2 changes: 1 addition & 1 deletion src/meta-lsp/ts-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "typegraph-ts-server",
"description": "TypeScript language server for TypeGraph",
"author": "Metatype Team",
"version": "0.5.0-rc.4",
"version": "0.5.0-rc.5",
"repository": {
"type": "git",
"url": "https://github.com/metatypedev/metatype"
Expand Down
2 changes: 1 addition & 1 deletion src/meta-lsp/vscode-metatype-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-metatype-support",
"description": "VSCode extension for Metatype support",
"author": "Metatype Team",
"version": "0.5.0-rc.4",
"version": "0.5.0-rc.5",
"repository": {
"type": "git",
"url": "https://github.com/metatypedev/metatype"
Expand Down
2 changes: 1 addition & 1 deletion src/pyrt_wit_wire/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyrt_wit_wire"
version = "0.5.0-rc.4"
version = "0.5.0-rc.5"
description = "Wasm component implementing the PythonRuntime host using wit_wire protocol."
license = "MPL-2.0"
readme = "README.md"
Expand Down
58 changes: 37 additions & 21 deletions src/typegate/src/runtimes/substantial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import { Backend } from "../../engine/runtime.js";
import {
Agent,
AgentConfig,
type StdKwargs,
WorkflowDescription,
} from "./substantial/agent.ts";
import { closestWord } from "../utils.ts";
import { InternalAuth } from "../services/auth/protocols/internal.ts";
import { TaskContext } from "./deno/shared_types.ts";
import { globalConfig } from "../config.ts";

const logger = getLogger(import.meta);

Expand Down Expand Up @@ -68,6 +67,7 @@ export class SubstantialRuntime extends Runtime {
queue: string,
agent: Agent,
typegate: Typegate,
private secrets: Record<string, string>,
) {
super(typegraphName);
this.logger = getLogger(`substantial:'${typegraphName}'`);
Expand Down Expand Up @@ -112,20 +112,7 @@ export class SubstantialRuntime extends Runtime {
maxAcquirePerTick: typegate.config.base.substantial_max_acquire_per_tick,
} satisfies AgentConfig;

// Note: required for ctx.gql()
const token = await InternalAuth.emit(typegate.cryptoKeys);
const internalTCtx = {
context: {},
secrets: {},
effect: null,
meta: {
url: `http://127.0.0.1:${globalConfig.tg_port}/${tgName}`,
token,
},
headers: {},
} satisfies TaskContext;

const agent = new Agent(backend, queue, agentConfig, internalTCtx);
const agent = new Agent(backend, queue, agentConfig);

const wfDescriptions = await getWorkflowDescriptions(
tgName,
Expand All @@ -144,6 +131,7 @@ export class SubstantialRuntime extends Runtime {
queue,
agent,
typegate,
secrets,
);
await instance.#prepareWorkflowFiles(
tg.meta.artifacts,
Expand Down Expand Up @@ -211,9 +199,9 @@ export class SubstantialRuntime extends Runtime {

switch (matName) {
case "start":
return this.#startResolver(false);
return this.#startResolver(mat, false);
case "start_raw":
return this.#startResolver(true);
return this.#startResolver(mat, true);
case "stop":
return this.#stopResolver();
case "send":
Expand All @@ -240,13 +228,41 @@ export class SubstantialRuntime extends Runtime {
}
}

#startResolver(enableGenerics: boolean): Resolver {
return async ({ name: workflowName, kwargs }) => {
#startResolver(mat: TypeMaterializer, enableGenerics: boolean): Resolver {
const secrets = ((mat.data.secrets as []) ?? []).reduce(
(agg, secretName) => ({ ...agg, [secretName]: this.secrets[secretName] }),
{},
);
return async ({
name: workflowName,
kwargs,
_: {
context,
parent,
info: { url, headers },
},
}) => {
this.#checkWorkflowExistOrThrow(workflowName);

const runId = Agent.nextId(workflowName);
const schedule = new Date().toJSON();

const token = await InternalAuth.emit(this.typegate.cryptoKeys);
const stdKwargs = {
kwargs: enableGenerics ? JSON.parse(kwargs) : kwargs,
taskContext: {
parent,
context,
secrets,
effect: mat.effect.effect ?? null,
meta: {
url: `${url.protocol}//${url.host}/${this.typegraphName}`,
token,
},
headers,
},
} satisfies StdKwargs;

logger.info(
`Start request "${workflowName}" received: new run "${runId}" should be scheduled.`,
);
Expand All @@ -259,7 +275,7 @@ export class SubstantialRuntime extends Runtime {
at: schedule,
event: {
type: "Start",
kwargs: enableGenerics ? JSON.parse(kwargs) : kwargs,
kwargs: stdKwargs,
},
},
});
Expand Down
11 changes: 8 additions & 3 deletions src/typegate/src/runtimes/substantial/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import { RunId, WorkerManager } from "./workflow_worker_manager.ts";

const logger = getLogger();

export interface StdKwargs {
taskContext: TaskContext;
kwargs: Record<string, string>;
}

export interface WorkflowDescription {
name: string;
path: string;
Expand All @@ -39,7 +44,6 @@ export class Agent {
private backend: Backend,
private queue: string,
private config: AgentConfig,
private internalTCtx: TaskContext,
) {}

async schedule(input: AddScheduleInput) {
Expand Down Expand Up @@ -247,15 +251,16 @@ export class Agent {
return;
}

const { taskContext, kwargs } = first.event.kwargs as unknown as StdKwargs;
try {
this.workerManager.triggerStart(
workflow.name,
next.run_id,
workflow.path,
run,
next.schedule_date,
first.event.kwargs,
this.internalTCtx,
kwargs,
taskContext,
);

this.workerManager.listen(
Expand Down
3 changes: 0 additions & 3 deletions src/typegate/src/runtimes/substantial/deno_context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0

// FIXME: DO NOT IMPORT any file that refers to Meta, this will be instantiated in a Worker
// import { sleep } from "../../utils.ts"; // will silently fail??

import { make_internal } from "../../worker_utils.ts";
import { TaskContext } from "../deno/shared_types.ts";
import { appendIfOngoing, Interrupt, OperationEvent, Run } from "./types.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/typegate/src/runtimes/substantial/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ self.onmessage = async function (event) {

runCtx = new Context(run, kwargs, internal);

workflowFn(runCtx)
workflowFn(runCtx, internal)
.then((wfResult: unknown) => {
self.postMessage(
Ok(
Expand Down
2 changes: 1 addition & 1 deletion src/typegate/src/runtimes/wit_wire/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getLogger } from "../../log.ts";

const logger = getLogger(import.meta);

const METATYPE_VERSION = "0.5.0-rc.4";
const METATYPE_VERSION = "0.5.0-rc.5";

export class WitWireMessenger {
static async init(
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typegraph_core"
version = "0.5.0-rc.4"
version = "0.5.0-rc.5"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/core/src/global_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const PREDEFINED_DENO_FUNCTIONS: &[&str] = &["identity", "true"];

thread_local! {
pub static STORE: RefCell<Store> = RefCell::new(Store::new());
pub static SDK_VERSION: String = "0.5.0-rc.4".to_owned();
pub static SDK_VERSION: String = "0.5.0-rc.5".to_owned();
}

fn with_store<T, F: FnOnce(&Store) -> T>(f: F) -> T {
Expand Down
Loading

0 comments on commit d669167

Please sign in to comment.