Skip to content

Commit 5d65184

Browse files
authored
feat: markdown ctx type (#843)
# What Adds a simple `markdown()` ctx helper method to return a text response with the correct MIME type. I will add support for rendering markdown in a separate PR to the playground.
1 parent bc44bf0 commit 5d65184

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

.changeset/dull-ghosts-tap.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gram-ai/create-function": patch
3+
"@gram-ai/functions": patch
4+
---
5+
6+
Adds markdown() response helper to the functions ctx

examples/complete/pylon-basic/CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ async execute(ctx, input) {
9494
}
9595
```
9696

97+
#### `ctx.markdown(data)`
98+
99+
Returns a Markdown response:
100+
101+
```typescript
102+
async execute(ctx, input) {
103+
return ctx.markdown("# Heading");
104+
}
105+
```
106+
97107
#### `ctx.fail(data, options?)`
98108

99109
Throws an error response (never returns):

ts-framework/create-function/gram-template-gram/CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ async execute(ctx, input) {
9595
}
9696
```
9797

98+
#### `ctx.markdown(data)`
99+
100+
Returns a Markdown response:
101+
102+
```typescript
103+
async execute(ctx, input) {
104+
return ctx.markdown("# Heading");
105+
}
106+
```
107+
98108
#### `ctx.fail(data, options?)`
99109

100110
Throws an error response (never returns):

ts-framework/functions/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ async execute(ctx, input) {
111111
}
112112
```
113113

114+
#### `ctx.markdown(data)`
115+
116+
Returns a markdown response:
117+
118+
```typescript
119+
async execute(ctx, input) {
120+
return ctx.markdown("# Heading");
121+
}
122+
```
123+
114124
#### `ctx.html(data)`
115125

116126
Returns an HTML response:

ts-framework/functions/src/framework.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ test("supports text tools", async () => {
158158
expect(data).toBe("HELLO!!!");
159159
});
160160

161+
test("supports markdown tools", async () => {
162+
const g = new Gram().tool({
163+
name: "shout",
164+
description: "Shouts the input",
165+
inputSchema: { message: z.string() },
166+
async execute(ctx, input) {
167+
return ctx.markdown(`# ${input.message.toUpperCase()}!!!`);
168+
},
169+
});
170+
171+
const response = await g.handleToolCall({
172+
name: "shout",
173+
input: { message: "hello" },
174+
});
175+
expect(response.status).toBe(200);
176+
expect(response.headers.get("Content-Type")).toBe(
177+
"text/markdown;charset=UTF-8",
178+
);
179+
180+
const data = await response.text();
181+
expect(data).toBe("# HELLO!!!");
182+
});
183+
161184
test("supports html tools", async () => {
162185
const g = new Gram().tool({
163186
name: "shout",

ts-framework/functions/src/framework.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ class ToolContext<Env> {
138138
}) as JSONResponse<V>;
139139
}
140140

141+
/**
142+
* Constructs a Markdown response
143+
*/
144+
markdown<V extends string>(data: V): TextResponse<V> {
145+
return new Response(data, {
146+
status: 200,
147+
headers: {
148+
"Content-Type": "text/markdown;charset=UTF-8",
149+
},
150+
}) as TextResponse<V>;
151+
}
152+
141153
/**
142154
* Constructs a plain text response
143155
*/

0 commit comments

Comments
 (0)