Skip to content

Commit 3f8b9a8

Browse files
authored
Merge pull request #23 from ivo-toby/decrease_token_usage
Decrease token usage
2 parents 891a915 + 16a921b commit 3f8b9a8

File tree

6 files changed

+147
-11
lines changed

6 files changed

+147
-11
lines changed

src/handlers/asset-handlers.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,37 @@ const getCurrentAsset = async (params: BaseAssetParams) => {
1717
return contentfulClient.asset.get(params)
1818
}
1919

20+
import { summarizeData } from "../utils/summarizer.js"
21+
2022
export const assetHandlers = {
23+
listAssets: async (args: {
24+
spaceId: string
25+
environmentId: string
26+
limit: number
27+
skip: number
28+
}) => {
29+
const spaceId = process.env.SPACE_ID || args.spaceId
30+
const environmentId = process.env.ENVIRONMENT_ID || args.environmentId
31+
32+
const params = {
33+
spaceId,
34+
environmentId,
35+
query: {
36+
limit: Math.min(args.limit || 3, 3),
37+
skip: args.skip || 0,
38+
},
39+
}
40+
41+
const contentfulClient = await getContentfulClient()
42+
const assets = await contentfulClient.asset.getMany(params)
43+
44+
const summarized = summarizeData(assets, {
45+
maxItems: 3,
46+
remainingMessage: "To see more assets, please ask me to retrieve the next page.",
47+
})
48+
49+
return formatResponse(summarized)
50+
},
2151
uploadAsset: async (args: {
2252
spaceId: string
2353
environmentId: string

src/handlers/content-type-handlers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const contentTypeHandlers = {
1818
content: [{ type: "text", text: JSON.stringify(contentTypes, null, 2) }],
1919
}
2020
},
21-
2221
getContentType: async (args: {
2322
spaceId: string
2423
environmentId: string

src/handlers/entry-handlers.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { getContentfulClient } from "../config/client.js"
3+
import { summarizeData } from "../utils/summarizer.js"
34
import { CreateEntryProps, EntryProps, QueryOptions } from "contentful-management"
45

56
export const entryHandlers = {
@@ -15,11 +16,20 @@ export const entryHandlers = {
1516
const contentfulClient = await getContentfulClient()
1617
const entries = await contentfulClient.entry.getMany({
1718
...params,
18-
query: args.query,
19+
query: {
20+
...args.query,
21+
limit: Math.min(args.query.limit || 3, 3),
22+
skip: args.query.skip || 0
23+
},
24+
})
25+
26+
const summarized = summarizeData(entries, {
27+
maxItems: 3,
28+
remainingMessage: "To see more entries, please ask me to retrieve the next page."
1929
})
2030

2131
return {
22-
content: [{ type: "text", text: JSON.stringify(entries, null, 2) }],
32+
content: [{ type: "text", text: JSON.stringify(summarized, null, 2) }],
2333
}
2434
},
2535
createEntry: async (args: {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function getHandler(name: string) {
9191
delete_asset: assetHandlers.deleteAsset,
9292
publish_asset: assetHandlers.publishAsset,
9393
unpublish_asset: assetHandlers.unpublishAsset,
94+
list_assets: assetHandlers.listAssets,
9495

9596
// Space & Environment operations
9697
list_spaces: spaceHandlers.listSpaces,

src/types/tools.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export const getEntryTools = () => {
3232
return {
3333
SEARCH_ENTRIES: {
3434
name: "search_entries",
35-
description: "Search for entries using query parameters",
35+
description:
36+
"Search for entries using query parameters. Returns a maximum of 3 items per request. Use skip parameter to paginate through results.",
3637
inputSchema: getSpaceEnvProperties({
3738
type: "object",
3839
properties: {
@@ -42,19 +43,30 @@ export const getEntryTools = () => {
4243
properties: {
4344
content_type: { type: "string" },
4445
select: { type: "string" },
45-
limit: { type: "number" },
46-
skip: { type: "number" },
46+
limit: {
47+
type: "number",
48+
default: 3,
49+
maximum: 3,
50+
description: "Maximum number of items to return (max: 3)",
51+
},
52+
skip: {
53+
type: "number",
54+
default: 0,
55+
description: "Number of items to skip for pagination",
56+
},
4757
order: { type: "string" },
4858
query: { type: "string" },
4959
},
60+
required: ["limit", "skip"],
5061
},
5162
},
5263
required: ["query"],
5364
}),
5465
},
5566
CREATE_ENTRY: {
5667
name: "create_entry",
57-
description: "Create a new entry in Contentful",
68+
description:
69+
"Create a new entry in Contentful, before executing this function, you need to know the contentTypeId and the fields of that contentType, you can get the fields definition by using the GET_CONTENT_TYPE tool. ",
5870
inputSchema: getSpaceEnvProperties({
5971
type: "object",
6072
properties: {
@@ -130,8 +142,29 @@ export const getEntryTools = () => {
130142
// Tool definitions for Asset operations
131143
export const getAssetTools = () => {
132144
return {
145+
LIST_ASSETS: {
146+
name: "list_assets",
147+
description: "List assets in a space. Returns a maximum of 3 items per request. Use skip parameter to paginate through results.",
148+
inputSchema: getSpaceEnvProperties({
149+
type: "object",
150+
properties: {
151+
limit: {
152+
type: "number",
153+
default: 3,
154+
maximum: 3,
155+
description: "Maximum number of items to return (max: 3)"
156+
},
157+
skip: {
158+
type: "number",
159+
default: 0,
160+
description: "Number of items to skip for pagination"
161+
}
162+
},
163+
required: ["limit", "skip"]
164+
}),
165+
},
133166
UPLOAD_ASSET: {
134-
name: "upload_asset",
167+
name: "upload_asset",
135168
description: "Upload a new asset",
136169
inputSchema: getSpaceEnvProperties({
137170
type: "object",
@@ -226,11 +259,23 @@ export const getContentTypeTools = () => {
226259
LIST_CONTENT_TYPES: {
227260
name: "list_content_types",
228261
description:
229-
"List content types in a space. Requires either spaceId parameter to identify the target space.",
262+
"List content types in a space. Returns a maximum of 10 items per request. Use skip parameter to paginate through results.",
230263
inputSchema: getSpaceEnvProperties({
231264
type: "object",
232-
properties: {},
233-
required: [],
265+
properties: {
266+
limit: {
267+
type: "number",
268+
default: 10,
269+
maximum: 20,
270+
description: "Maximum number of items to return (max: 3)",
271+
},
272+
skip: {
273+
type: "number",
274+
default: 0,
275+
description: "Number of items to skip for pagination",
276+
},
277+
},
278+
required: ["limit", "skip"],
234279
}),
235280
},
236281
GET_CONTENT_TYPE: {

src/utils/summarizer.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
3+
export interface SummarizeOptions {
4+
maxItems?: number
5+
indent?: number
6+
showTotal?: boolean
7+
remainingMessage?: string
8+
}
9+
10+
export const summarizeData = (data: any, options: SummarizeOptions = {}): any => {
11+
const { maxItems = 3, remainingMessage = "To see more items, please ask me to retrieve the next page." } =
12+
options
13+
14+
// Handle Contentful-style responses with items and total
15+
if (data && typeof data === "object" && "items" in data && "total" in data) {
16+
const items = data.items
17+
const total = data.total
18+
19+
if (items.length <= maxItems) {
20+
return data
21+
}
22+
23+
return {
24+
items: items.slice(0, maxItems),
25+
total: total,
26+
showing: maxItems,
27+
remaining: total - maxItems,
28+
message: remainingMessage,
29+
skip: maxItems // Add skip value for next page
30+
}
31+
}
32+
33+
// Handle plain arrays
34+
if (Array.isArray(data)) {
35+
if (data.length <= maxItems) {
36+
return data
37+
}
38+
39+
return {
40+
items: data.slice(0, maxItems),
41+
total: data.length,
42+
showing: maxItems,
43+
remaining: data.length - maxItems,
44+
message: remainingMessage,
45+
skip: maxItems // Add skip value for next page
46+
}
47+
}
48+
49+
// Return non-array data as-is
50+
return data
51+
}

0 commit comments

Comments
 (0)