Skip to content

Commit 7c04b24

Browse files
committed
docs(workflow-code): describe the bare-reference splice rule the executor now applies
1 parent f37fd73 commit 7c04b24

4 files changed

Lines changed: 13 additions & 13 deletions

File tree

‎packages/workflow-code/src/compile/function-returns.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* literal and each sets it to an expression whose kind is certain (a literal, arithmetic, a
1111
* comparison, `String(…)`, the `.length` of a string or an array, a local variable never
1212
* reassigned to another kind, …). Anything else stays unknown, and the reference keeps its
13-
* warning: a wrong "typed" would store a string bare and break the JSON at run time.
13+
* warning: a wrong "typed" would store a string bare, where text that reads as JSON stops being text.
1414
*/
1515

1616
import { parse } from '@babel/parser'

‎packages/workflow-code/src/compile/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ function compileEntry(
784784
}
785785
checkErrorOutputRefs(ctx, erroring)
786786
// A reference to an output that does not exist, or to `x.error` where nothing errors, is that
787-
// one finding: whether text there would break the JSON is moot until the path is fixed. An input
787+
// one finding: whether text there stays text is moot until the path is fixed. An input
788788
// the start does not declare is not: a caller may still pass text there.
789789
const unknownAt = new Set([
790790
...reporter.reportedAt('W_UNKNOWN_OUTPUT_PATH'),
@@ -797,7 +797,7 @@ function compileEntry(
797797
if (unknownAt.has(`${ref.range.file}:${ref.range.start}`)) continue
798798
reporter.report(
799799
'W_JSON_REF_UNTYPED',
800-
`Nothing declares the type of ${ref.token}; it is stored bare, so a string value would break the JSON.`,
800+
`Nothing declares the type of ${ref.token}; it is stored bare, so text that reads as JSON (123, true) would become that value, not text.`,
801801
ref.range,
802802
{
803803
hint: `Leave it bare when it holds an array, object, number or boolean (it is spliced in as JSON); only when it holds text write t\`\${${text.slice(ref.range.start, ref.range.end)}}\`.`,

‎packages/workflow-code/src/diagnostics/codes.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const COMPILE_CATALOG_CODES = [
271271
'W_OUTPUT_PATH_UNDECLARED',
272272
/**
273273
* A bare reference in a JSON field literal whose output type nothing declares: it is stored bare
274-
* (typed), so a string value would break the JSON (`json-fields.ts`).
274+
* (typed), so text that reads as JSON would become that value, not text (`json-fields.ts`).
275275
*/
276276
'W_JSON_REF_UNTYPED',
277277
/**

‎packages/workflow-code/src/json-fields.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* JSON code fields (a `code` subBlock holding JSON text: an agent's `responseFormat`, table row
33
* `data`, a response body) written as object literals with typed references at their leaves.
44
*
5-
* The executor resolves a reference inside such text by splicing the value's text where the token
6-
* stands (`resolveTemplate` → `formatValueForBlock`): a string goes in raw, any other value as its
7-
* JSON. So a token keeps its value's type only when it sits bare for a non-string value
8-
* (`"n": <score.value>` → `"n": 3`) and quoted for a string (`"s": "<ticket.subject>"`). A bare
9-
* reference in a literal is stored by its output type: quoted when the catalog types it `string`,
10-
* bare otherwise, and an untyped one bare with `W_JSON_REF_UNTYPED`, since a string there would
11-
* break the JSON. A string in the literal (`` t`Hi ${x.name}` ``, `'plain'`) is always a JSON
5+
* The executor resolves a reference inside such text where the token stands (`resolveTemplate`):
6+
* inside a JSON string it splices the value escaped as string content; bare, it splices the value
7+
* as JSON, a string as a JSON string unless the string already reads as JSON. So a token keeps a
8+
* string value as text only when it sits quoted (`"s": "<ticket.subject>"`): bare, text that reads
9+
* as JSON (`123`, `true`) turns into that value. A bare reference in a literal is stored by its
10+
* output type: quoted when the catalog types it `string`, bare otherwise, and an untyped one bare
11+
* with `W_JSON_REF_UNTYPED`, since text there may not stay text. A string in the literal (`` t`Hi ${x.name}` ``, `'plain'`) is always a JSON
1212
* string, which is how a value is turned into text on purpose.
1313
*
1414
* The printer writes stored text back as a literal only when serializing that literal reproduces
@@ -274,8 +274,8 @@ export function refValueKind(
274274
* Whether a stored token is a string by declaration (the catalog, a variable's type, an input or
275275
* response format, an environment variable), not only by what a Function's code or a child
276276
* workflow's Response infers. Only such a token prints as a bare reference where it is stored
277-
* quoted: a bare reference typed by inference would compile bare, and break the JSON, once an edit
278-
* to that other code leaves its type unreadable, so the printer writes it as `` t`${ref}` ``,
277+
* quoted: a bare reference typed by inference would compile bare, and stop storing text as text,
278+
* once an edit to that other code leaves its type unreadable, so the printer writes it as `` t`${ref}` ``,
279279
* which is text whatever the code returns.
280280
*/
281281
export function refDeclaresString(token: string, scope: RefTypeScope): boolean {

0 commit comments

Comments
 (0)