Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/tools/get-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { CallToolResult, TextContent, ToolAnnotations } from '@modelcontext
import { makeRestGetRequest } from '../common/utils.js';
import type { MwRestApiPageObject } from '../types/mwRestApi.js';

const CHUNK_SIZE = 90000;

enum ContentFormat {
noContent = 'noContent',
withSource = 'withSource',
Expand Down Expand Up @@ -84,17 +86,33 @@ function getPageToolResult( result: MwRestApiPageObject ): TextContent[] {
];

if ( result.source !== undefined ) {
results.push( {
type: 'text',
text: `Source:\n${ result.source }`
} );
const totalChunks = Math.ceil( result.source.length / CHUNK_SIZE );

for ( let i = 0; i < result.source.length; i += CHUNK_SIZE ) {
const currentChunk = Math.floor( i / CHUNK_SIZE ) + 1;
const prefix = `Source part ${ currentChunk }/${ totalChunks }:\n`;
const sourceChunk = result.source.slice( i, i + CHUNK_SIZE );

results.push( {
type: 'text',
text: `${ prefix }${ sourceChunk }`
} );
}
}

if ( result.html !== undefined ) {
results.push( {
type: 'text',
text: `HTML:\n${ result.html }`
} );
const totalChunks = Math.ceil( result.html.length / CHUNK_SIZE );

for ( let i = 0; i < result.html.length; i += CHUNK_SIZE ) {
const currentChunk = Math.floor( i / CHUNK_SIZE ) + 1;
const prefix = `HTML part ${ currentChunk }/${ totalChunks }:\n`;
const htmlChunk = result.html.slice( i, i + CHUNK_SIZE );

results.push( {
type: 'text',
text: `${ prefix }${ htmlChunk }`
} );
}
}

return results;
Expand Down