Skip to content

feat(core): run a function's source as an automation's code - #68392

Open
rekram1-node wants to merge 5 commits into
devfrom
code-mode-functions
Open

rekram1-node wants to merge 5 commits into
devfrom
code-mode-functions

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

N/A

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

In Code Mode, a code automation's code is JavaScript inside a string inside JavaScript, so quotes and regexes get escaped twice. Now a program can write a real function and pass its source:

// before
code: "if (runs < 2) return; return { send: { prompt: \"Review the build.\" } }"

// after
code: (async (input) => {
  if (input.runs < 2) return
  return { send: { prompt: "Review the build." } }
}).toString()

.toString() runs inside the program, so the tool still gets a string. When an automation saves a code action whose code is a single function expression, it stores return await (<fn>)(input), so it's still a plain program and nothing else changes. The action.code description shows the function form.

Creating a code automation now also refuses code that uses a name which won't exist when it runs, such as a variable of the script that created it:

`channel` won't exist when this automation runs. Its code runs on its own later, so it can't use variables from the script that created it: pass values in action.args and read them as input.args.

The names come from psychoscript 0.12.0's script.globals (worked out from the compiled program, cached, nothing extra per run), checked against what a run binds: JavaScript's globals, tools, search, fetch, input, state, now, runs, args, plus event for event triggers, Bun/path with full_codemode, and inputs when the code has input blocks. Only creation checks; existing automations are untouched. psychoscript 0.12.0 also makes String(fn) and template strings give a function's source, as in JavaScript.

No flag: a code action that was only a function expression used to do nothing, and code using an undefined name failed on every run.

How did you verify your code works?

  • New test/automation-code-body.test.ts, and unbound-name cases in test/automation.test.ts.
  • codemode package tests (337 pass) on psychoscript 0.12.0.
  • bun run script/test.ts for automation and automation tool tests, bun run check.

Screenshots / recordings

N/A, no UI.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as described. I created an automation with an inline arrow function through the real Automation service. The saved program was return await (<fn>)(input), the tool call inside it was authorised at creation, and two ticks ran it with the right input. The new tests and the nearby automation and codemode suites pass (87 tests), and typecheck:tsc passes in core and schema.

Public API: adds an optional codemode.functionSources to the tool Options in the plugin API, and changes the automation.create action.code description. Code actions that are a single function expression are now saved wrapped as return await (<fn>)(input); before, such a program did nothing. Backwards compatible.

Two points, inline: the outer-variable check matches by name rather than scope, so it can miss real outer variables or refuse valid functions; and nothing validates functionSources when a tool registers, so one malformed plugin would break every Code Mode run.

const declared = declarations(program)
for (const item of found) {
const inside = (at: number) => at >= item.fn.start && at < item.fn.end
const own = new Set(declared.filter((d) => inside(d.at)).map((d) => d.name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check compares names, not scopes, so it can be wrong both ways:

  • Misses an outer variable: const x = 1 followed by code: async () => { [1].map((x) => x); return x } is accepted. The inner callback's x puts x in own. The automation then throws a ReferenceError on every run, not at creation.
  • Refuses a valid function: items.map((state) => …) anywhere in the script makes code: async () => ({ state: state ?? 0 }) be refused. Here state is the documented run global (the same applies to now, runs and args).

A small scope walk would cover both: resolve each identifier through the function's nested scopes, and look only at bindings on the enclosing chain.

Comment thread packages/core/src/codemode/tool.ts Outdated
return new Map(
Array.from(inventory.tools.values()).flatMap((registration) => {
const codemode = registration.options?.codemode
const paths = typeof codemode === "object" && codemode !== null ? codemode.functionSources : undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functionSources is now part of the plugin API, but the registration check in src/tool.ts (around line 967) doesn't validate it. A plugin passing functionSources: "action.code" (a string) gets past paths.length === 0, and then paths.map throws here. This runs over the whole inventory on every prepare, so that one tool would break every Code Mode run in the session. Could you check for an array of non-empty strings when the tool registers?

@rekram1-node rekram1-node changed the title feat(core): accept inline functions for automation code in Code Mode feat(core): run a function's source as an automation's code Dec 4, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new version works as described. Both earlier points are resolved: the source rewrite and the functionSources option are gone. I ran a real Code Mode program through the tool-automation harness: (async (input) => {…}).toString() returns the function's exact source (comments, backticks, regexes and escaped quotes unchanged), and automation.create gets it as a plain string. AutomationCode.body is the same as in the previous commit, which I checked end to end against the real Automation service. The automation and automation tool tests pass, and typecheck:tsc passes in core and schema.

Public API: a code action that is a single function expression is now saved as return await (<fn>)(input); before, such code did nothing. The action.code description changed too. Backwards compatible.

Not blocking: nothing checks anymore whether the function uses variables from the surrounding script. That mistake now gets through creation and fails on every run, not when the automation is saved. The description does warn about it.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing in this pull request changed since my last review: the new commits rebase it onto a newer dev, and its four files are the same as before. On the new head, the automation tests pass and typecheck:tsc passes in core, so my previous review stands. Both earlier threads were about the source rewrite and the functionSources option, which have been removed.

Public API: a code action that is a single function expression is now saved as return await (<fn>)(input); before, such code did nothing. The action.code description changed too. Backwards compatible.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit only regenerates openapi.json for the new action.code description. check:generated passes in packages/protocol, so the spec matches the schema. My previous review still applies, and the two old threads were about code that has since been removed.

Public API: the OpenAPI description of the automation code action's code field changes. Code actions that are a single function expression are now saved as return await (<fn>)(input); before, such code did nothing. Backwards compatible.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant