You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/gram/gram-functions/functions-framework.mdx
+131-1Lines changed: 131 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -163,5 +163,135 @@ const gram = new Gram().tool({
163
163
});
164
164
```
165
165
166
+
## Composability
167
+
168
+
Gram instances can be composed together using the `.extend()` method, allowing tool definitions to be split across multiple files and modules. This pattern is similar to Hono's grouping pattern and helps organize larger codebases.
169
+
170
+
### Basic composition
171
+
172
+
Split tool definitions into separate modules and combine them:
173
+
174
+
```typescript filename="train.ts"
175
+
import { Gram } from"@gram-ai/functions";
176
+
import*aszfrom"zod/mini";
177
+
178
+
exportconst trainGram =newGram({
179
+
envSchema: {
180
+
TRAIN_API_KEY: z.string().describe("API key for the train service"),
When composing Gram instances, environment schemas are automatically merged. Each module can define its own environment variables, and the final composed instance will validate all required variables:
249
+
250
+
```typescript
251
+
// Each module defines its own environment requirements
252
+
const weatherGram =newGram({
253
+
envSchema: {
254
+
WEATHER_API_KEY: z.string(),
255
+
},
256
+
}).tool({
257
+
name: "get_weather",
258
+
inputSchema: { city: z.string() },
259
+
async execute(ctx, input) {
260
+
// Access environment variable from this module
261
+
const apiKey =ctx.env.WEATHER_API_KEY;
262
+
returnctx.json({ temperature: 72 });
263
+
},
264
+
});
265
+
266
+
const newsGram =newGram({
267
+
envSchema: {
268
+
NEWS_API_KEY: z.string(),
269
+
},
270
+
}).tool({
271
+
name: "get_news",
272
+
inputSchema: { topic: z.string() },
273
+
async execute(ctx, input) {
274
+
// Access environment variable from this module
275
+
const apiKey =ctx.env.NEWS_API_KEY;
276
+
returnctx.json({ articles: [] });
277
+
},
278
+
});
279
+
280
+
// Composed instance requires both environment variables
281
+
const gram =newGram()
282
+
.extend(weatherGram)
283
+
.extend(newsGram);
284
+
// Both WEATHER_API_KEY and NEWS_API_KEY must be provided
285
+
```
286
+
287
+
### Benefits of composition
288
+
289
+
Composing Gram instances provides several advantages:
290
+
291
+
-**Modularity**: Organize related tools into separate files
292
+
-**Reusability**: Share tool definitions across different Gram instances
293
+
-**Maintainability**: Easier to manage large codebases with many tools
294
+
-**Team collaboration**: Different team members can work on separate modules
295
+
166
296
## Next steps
167
-
-[Build and deploy](/docs/gram/gram-functions/build-deploy)
297
+
-[Build and deploy](/docs/gram/gram-functions/build-deploy)
0 commit comments