Skip to content

Commit eaf21ff

Browse files
committed
docs: make the cookbook's order writes repeatable and say where each source directory takes its workspace
The orders table's orderId is unique, so the cookbook and the Support workflow upsert on it instead of inserting a second row for the same order.
1 parent fb42142 commit eaf21ff

7 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎packages/sim-cli/src/commands/protocol/source-loop-help.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ function loopRows(rows: ReadonlyArray<readonly [string, string]>): string {
1313
}
1414

1515
/** Where the commands of either group find their workspace. */
16-
const WORKSPACE_LINE = `Workspace: a command run on a directory reads it from .sim/base.json; any other takes -w <id>
17-
or SIM_WORKSPACE (export it once, or keep only the ID in a variable: a variable holding
18-
"--workspace <id>" is one argument, an unknown option alone and the whole ID after -w).`
16+
const WORKSPACE_LINE = `Workspace: a directory records it (.sim/base.json, or a starter's .sim/starter.json); any other
17+
command takes -w <id>, SIM_WORKSPACE or the profile's (export SIM_WORKSPACE once, or keep only the
18+
ID in a variable: one holding "--workspace <id>" is one argument, an unknown option alone and the
19+
whole ID after -w).`
1920

2021
/** The status line both groups end with, for a directory at `dir`. */
2122
function statusLine(group: string, dir: string): string {

‎packages/sim-cli/src/commands/protocol/workflow-dir.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ The primer above is enough to edit; read a section here when a step needs it.
220220
221221
## Edit loop in detail
222222
223-
1. Edit \`${SOURCE_ENTRY_FILE}\` (and \`functions/*\`). \`${SOURCE_EXAMPLES_DIR}/\` holds correct examples to copy from: \`cookbook.ts\` (agent with structured output, condition, function, table query and insert, knowledge search, Slack, human in the loop, loop, response), \`integrations.ts\` (webhook trigger, weekday cron schedule, HTTP request with headers, table update by row id, Gmail, an error path) and \`agents.ts\` (router, agent tools: custom tool, MCP tool, integration operation, child workflow; run a child workflow; parallel). Their resource keys are examples; this workspace's are in \`${WORKSPACE_TYPES_FILE}\`.
223+
1. Edit \`${SOURCE_ENTRY_FILE}\` (and \`functions/*\`). \`${SOURCE_EXAMPLES_DIR}/\` holds correct examples to copy from: \`cookbook.ts\` (agent with structured output, condition, function, table query and upsert, knowledge search, Slack, human in the loop, loop, response), \`integrations.ts\` (webhook trigger, weekday cron schedule, HTTP request with headers, table update by row id, Gmail, an error path) and \`agents.ts\` (router, agent tools: custom tool, MCP tool, integration operation, child workflow; run a child workflow; parallel). Their resource keys are examples; this workspace's are in \`${WORKSPACE_TYPES_FILE}\`.
224224
2. \`sim workflows source check --dir .\`: compiles offline with the server's compiler, in milliseconds. It catches what \`tsc\` cannot (duplicate names, cycles, literal secrets), and fails (exit 1) on a missing required field, an unknown option or output path, a wrong value kind, an out-of-range value, an unwired condition or router branch or a literal secret in a block you added (\`--strict\`: in every block). It also adds the blocks the source now uses to \`tsconfig.json\`.
225225
3. \`${typecheckCommand()}\`: about a second; catches what step 2 and the dry run do not (such as a reference to a start input the trigger does not declare). It runs the TypeScript release the declarations are built for (the directory has no \`package.json\`; plain \`bunx tsc\` would run whatever version it finds). \`tsconfig.json\` includes only the blocks the source used at the last check, get or apply: a builder from another block fails with \`has no exported member\` until you run step 2 (or use \`${typecheckCommand(TSCONFIG_FULL_FILE)}\`, every current block). The dry run is the authority: a printed line you did not edit that \`tsc\` flags and the dry run accepts is fine.
226226
4. \`sim workflows source status --dir .\`: the files you changed (\`sim workflows source diff --dir .\` shows the edits), whether the workflow changed on the server since the get, and the command to run next (\`--offline\`: skip the server). Then \`sim workflows source apply --dir . --dry-run\` until it exits 0.

‎packages/sim-cli/src/program.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ Profiles work like the AWS CLI: settings live in ~/.sim/config, keys in
2828
with -P, --profile, or SIM_PROFILE.
2929
3030
A command targets the profile's workspace unless -w <id> (--workspace <id>) or
31-
SIM_WORKSPACE names another; a source directory keeps to the workspace its
32-
.sim/base.json records, and refuses a -w naming another. In a script, export
31+
SIM_WORKSPACE names another. A source directory records its own: a workflow
32+
directory acts on the workflow its .sim/base.json names, a workspace directory
33+
on the workspace its base names (refusing a -w naming another), and a starter
34+
on the one .sim/starter.json names unless -w names another. In a script, export
3335
SIM_WORKSPACE=<id> once, or keep only the ID in a variable and write -w "$WS".
3436
A variable holding "--workspace <id>" is one argument: alone it is an unknown
3537
option, and after -w all of it is taken as the ID.

‎packages/workflow-sdk/cookbook/examples/cookbook.ts‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,18 @@ export default workflow('Cookbook', { description: 'One example of each common b
6666
code: js`return { id: ${start.orderId}, cents: Math.round(${start.amount} * 100) }`,
6767
})
6868

69-
// Tables: query rows (each row is { id, data: { column } }) and insert a row.
69+
// Tables: query rows (each row is { id, data: { column } }) and write one.
7070
const recent = table.queryRows('Recent Orders', {
7171
tableId: table('orders'),
7272
filterInput: { all: [{ field: 'status', op: 'eq', value: 'open' }] },
7373
limit: 50,
7474
})
75-
const save = table.insertRow('Save Order', {
76-
tableId: table('orders'),
77-
data: { orderId: start.orderId, status: 'open' },
78-
})
7975
// Upsert: updates the row whose conflictColumn (a unique column, by name) matches data, else
80-
// inserts one. data is keyed by column name, as for insertRow.
81-
const touch = table.upsertRow('Touch Order', {
76+
// inserts one, so a second run for the same order updates it. table.insertRow takes the same
77+
// data without conflictColumn and adds a row every run.
78+
const save = table.upsertRow('Save Order', {
8279
tableId: table('orders'),
83-
data: { orderId: start.orderId, status: 'answered' },
80+
data: { orderId: start.orderId, status: 'open' },
8481
conflictColumn: 'orderId',
8582
})
8683

@@ -146,8 +143,7 @@ export default workflow('Cookbook', { description: 'One example of each common b
146143
// Wiring: a.to(b) chains; a block can fan out to several targets.
147144
start.to(check)
148145
check.to(save)
149-
save.to(touch)
150-
touch.to(route)
146+
save.to(route)
151147
route.high.to(alert)
152148
alert.to(approval)
153149
approval.to(decided)

‎packages/workflow-sdk/cookbook/workspace/workflows/support/workflow.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ export default workflow('Support', { description: 'Answers an order question.' }
3232
tools: [customTool('lookup-order')],
3333
skills: [skill('tone')],
3434
})
35-
const log = table.insertRow('Log Question', {
35+
const log = table.upsertRow('Log Question', {
3636
tableId: table('orders'),
3737
data: { orderId: start.orderId, status: 'answered' },
38+
conflictColumn: 'orderId',
3839
})
3940

4041
start.to(policy)

0 commit comments

Comments
 (0)