fix(teacher): 改善只读工具分页与读取预算

This commit is contained in:
brother7 committed 2026-09-27 10:15:17 +08:00
1 parent 17c7e5c495
commit 56cefe84e7
8 files changed
+297 -56

No files matched your search

+11 -4
View File
@@ -10,7 +10,8 @@ import {
type TeacherAccount,
} from './config-client';
import type { TeacherModelMessage } from './context';
import { createTeacherReadTools, type TeacherReadAccess } from './read-tools';
import { createTeacherReadTools, type TeacherReadAccess, TEACHER_READ_PROTOCOL, TEACHER_READ_ROUNDS,
TEACHER_READ_BYTES, TEACHER_READ_PAGE_BYTES, TEACHER_READ_MIN_BYTES } from './read-tools';
interface TeacherSession {
access_token: string;
@@ -209,6 +210,7 @@ export function prepareCloudTeacher(
|| currentRequest?.presentation === 'discussion-v1';
const localContext = {
id: requestId,
read_protocol: TEACHER_READ_PROTOCOL,
scope: {
project_id: topic.projectId,
source_session_id: currentRequest?.sourceConversationId ?? topic.sourceConversationId,
@@ -274,7 +276,8 @@ export function prepareCloudTeacher(
}
let runId = identifier(queued.run_id),
cursor = '0-0',
rounds = 0;
rounds = 0,
readBytes = 0;
let runText = '',
messageId = '';
while (Date.now() < deadline) {
@@ -308,7 +311,7 @@ export function prepareCloudTeacher(
);
if (!Array.isArray(pending.calls) || !pending.calls.length || pending.calls.length > 32)
throw new TeacherError(502, 'teacher_protocol_invalid', '智能体读取请求格式无效,请重试。');
if (++rounds > 6)
if (++rounds > TEACHER_READ_ROUNDS)
throw new TeacherError(
409,
'teacher_read_limit',
@@ -321,7 +324,11 @@ export function prepareCloudTeacher(
if (typeof call.tool_call_id !== 'string' || typeof call.name !== 'string')
throw new TeacherError(502, 'teacher_protocol_invalid', '智能体读取请求无效。');
reportActivity({ id: requestId + ':' + call.tool_call_id, name: call.name, status: 'running' });
const result = await tools.executeResult(call.name, JSON.stringify(call.arguments), bounded);
const remaining = TEACHER_READ_BYTES - readBytes;
const result = remaining < TEACHER_READ_MIN_BYTES
? { status: 'error' as const, content: 'Read budget exhausted. Answer from the evidence already returned and state any gaps.', truncated: false }
: await tools.executeResult(call.name, JSON.stringify(call.arguments), bounded, Math.min(TEACHER_READ_PAGE_BYTES, remaining));
if (result.status === 'success') readBytes += Buffer.byteLength(result.content);
results.push({
tool_call_id: call.tool_call_id,
...result,