-
Notifications
You must be signed in to change notification settings - Fork 0
Make sample 318 stdin-runnable for the daily Rig sampler #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,50 +3,21 @@ | |
| ```rig | ||
| import { agent, p, s, defineTool, repair } from "rig"; | ||
| import { readFile } from "node:fs/promises"; | ||
|
|
||
| // Agent role: generate a barrel index.ts that re-exports all exported symbols from TypeScript files in a directory. | ||
| // Agent role: generate a barrel file for exported symbols in skills/rig/engines. | ||
| const tsBarrelReExporter = agent({ | ||
| model: "small", | ||
| input: s.object({ | ||
| sourceDir: s.path, | ||
| outputFile: s.path, | ||
| }), | ||
| instructions: p`You are a TypeScript barrel re-exporter. | ||
|
|
||
| Find TypeScript source files to barrel (excluding index files): | ||
| ${p.bash("find . -name '*.ts' -not -name 'index.ts' -not -name '*.test.ts' -not -name '*.d.ts' -not -path '*/node_modules/*' | head -50")} | ||
|
|
||
| For each TypeScript file in the source directory, call extractExportedSymbols to discover exported names. | ||
| Then write the barrel file to ${p.writeInput("outputFile", "barrelContent")}. | ||
| Return the declared output.`, | ||
| tools: [ | ||
| defineTool("extractExportedSymbols", { | ||
| description: "Read a TypeScript file and extract all top-level exported symbol names", | ||
| parameters: s.object({ filePath: s.path }), | ||
| async handler({ filePath }) { | ||
| const content = await readFile(filePath, "utf8"); | ||
| const symbols: string[] = []; | ||
| const exportRegex = /^export\s+(?:(?:default\s+)?(?:function|class|const|let|var|type|interface|enum)\s+(\w+)|(\{[^}]+\}))/gm; | ||
| for (const match of content.matchAll(exportRegex)) { | ||
| if (match[1]) { | ||
| symbols.push(match[1]); | ||
| } else if (match[2]) { | ||
| const named = match[2].replace(/[{}]/g, "").split(",").map((s: string) => s.trim().split(/\s+as\s+/)[0].trim()).filter(Boolean); | ||
| symbols.push(...named); | ||
| } | ||
| } | ||
| return { filePath, symbols }; | ||
| }, | ||
| }), | ||
| ], | ||
| output: s.object({ | ||
| exports: s.array(s.string), | ||
| outputFile: s.path, | ||
| totalExports: s.int, | ||
| filesScanned: s.int, | ||
| }), | ||
| instructions: p`Generate a TypeScript barrel for skills/rig/engines from ${p.bash("find skills/rig/engines -name '*.ts' -not -name 'index.ts' -not -name '*.d.ts' | head -20")}. Use extractExportedSymbols for each file, set outputFile to /tmp/rig-barrel.ts, write barrelContent to ${p.writeOutput("barrelContent", "/tmp/rig-barrel.ts")}, and return the declared output.`, | ||
| tools: [defineTool("extractExportedSymbols", { | ||
| description: "Read a TypeScript file and extract top-level exported symbol names", | ||
| parameters: s.object({ filePath: s.path }), | ||
| async handler({ filePath }) { | ||
| const content = await readFile(filePath, "utf8"); | ||
| const symbols = [...content.matchAll(/^export\s+(?:(?:default\s+)?(?:function|class|const|let|var|type|interface|enum)\s+(\w+)|(\{[^}]+\}))/gm)].flatMap((match) => match[1] ? [match[1]] : match[2]!.replace(/[{}]/g, "").split(",").map((name) => name.trim().split(/\s+as\s+/)[0].trim()).filter(Boolean)); | ||
| return { filePath, symbols }; | ||
| }, | ||
| })], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] The tool definition and output schema are now one-liners, which keeps file size down but harms readability in a sample whose purpose is to teach. Samples are documentation first. 💡 Suggested refactorExpand the output schema and tool definition to multi-line form, consistent with every other sample in the directory: output: s.object({
barrelContent: s.string,
exports: s.array(s.string),
outputFile: s.path,
totalExports: s.int,
filesScanned: s.int,
}),Similarly, break the tool handler body out of the one-liner |
||
| output: s.object({ barrelContent: s.string, exports: s.array(s.string), outputFile: s.path, totalExports: s.int, filesScanned: s.int }), | ||
| addons: [repair()], | ||
| }); | ||
|
|
||
| export default tsBarrelReExporter; | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/codebase-design] The instructions are now a single dense line, sacrificing the readability that made the original multi-line template literal easy to scan.
💡 Suggested refactor
Split the template literal across lines:
This matches the style of other samples and keeps each prompt step scannable at a glance.