feat(models): add native Anthropic Messages and observed endpoint diagnostics
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { randomUUID, createHash } from 'node:crypto';
|
||||
|
||||
export const APP_VERSION = '1.24.3';
|
||||
export const APP_VERSION = '1.24.4';
|
||||
|
||||
export function executionEnvironment() {
|
||||
const windows = process.platform === 'win32';
|
||||
|
||||
+27
-12
@@ -11,6 +11,7 @@ import { modelWaitSettings, requestDeadline } from '../core/models/request-deadl
|
||||
import { ModelTransport } from '../core/models/transport.mjs';
|
||||
import { localModelEstimate } from '../shared/contracts/model-reservation.mjs';
|
||||
import { ERROR_CAPTURE_LIMIT, readErrorResponse, upstreamCapture } from '../core/models/upstream-error.mjs';
|
||||
import { messagesAuthSettings } from '../shared/contracts/model-endpoint.mjs';
|
||||
|
||||
export const engineToolDefinitions = [
|
||||
{ type: 'function', function: { name: 'inspect_engine_checks', description: '读取当前Task已经保存的外部引擎连接核验:真实安装版本、启动完成信号、SDK会话、进程关闭、原报告工件和能力限制。不会启动引擎或发送prompt;完整原记录通过已有read_artifact读取。', parameters: { type: 'object', properties: {}, additionalProperties: false } } },
|
||||
@@ -154,7 +155,12 @@ export class Provider {
|
||||
async request(config, route, body, signal, key = this.apiKey, onProgress, options = {}) {
|
||||
const base = this.validate({ ...config, mode: 'demo' });
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
if (key) headers.Authorization = 'Bearer ' + key;
|
||||
if (config.protocol === 'anthropic-messages') {
|
||||
headers['anthropic-version'] = '2023-06-01';
|
||||
const { messagesAuth } = messagesAuthSettings(config.protocol, config.messagesAuth);
|
||||
if (key) headers[messagesAuth === 'bearer' ? 'Authorization' : 'x-api-key'] = messagesAuth === 'bearer' ? 'Bearer ' + key : key;
|
||||
} else if (key) headers.Authorization = 'Bearer ' + key;
|
||||
const requestUrl = base + route;
|
||||
const waits = modelWaitSettings(config), timeoutMs = body ? waits.modelResponseTimeoutMs : 20000;
|
||||
const policy = { attempts: 3, delays: [500, 1500], timeoutMs, totalTimeoutMs: timeoutMs,
|
||||
idleTimeoutMs: waits.modelStreamIdleTimeoutMs, ...this.retryPolicy, ...options };
|
||||
@@ -172,10 +178,10 @@ export class Provider {
|
||||
const timeout = requestDeadline({ signal, timeoutMs: Math.max(1, Math.min(policy.timeoutMs, deadline - Date.now())), idleTimeoutMs: policy.idleTimeoutMs });
|
||||
const requestSignal = timeout.signal;
|
||||
let readingStream = false, streamTerminalSeen = false, dispatched = false, usage = null, outcome = 'unknown';
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString(),
|
||||
const receipt = { attemptId: randomUUID(), endpoint: base, requestUrl, model: config.model, protocol: config.protocol ?? 'chat-completions', startedAt: new Date().toISOString(),
|
||||
...(body?{localEstimate:localModelEstimate(body)}:{}) };
|
||||
const context = { endpoint: base, model: config.model, route, attempt, attemptId: receipt.attemptId };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, wireAttemptId: receipt.attemptId });
|
||||
const context = { endpoint: base, requestUrl, model: config.model, route, attempt, attemptId: receipt.attemptId };
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, endpoint: base, route, requestUrl, wireAttemptId: receipt.attemptId });
|
||||
let responseDetails = null;
|
||||
const identityObserver = body ? modelIdentityObserver(body.model, { ...config, ...options }) : null;
|
||||
const publishIdentity = complete => {
|
||||
@@ -200,11 +206,18 @@ export class Provider {
|
||||
dispatched = true;
|
||||
const transport=this.fetcher?null:(this.transport??=new ModelTransport());
|
||||
if(transport)onProgress?.({phase:'running',transport:transport.profile,attempt,maxAttempts:policy.attempts,wireAttemptId:receipt.attemptId});
|
||||
const response = await (this.fetcher??transport.fetch.bind(transport))(base + route, { method: body ? 'POST' : 'GET', headers,
|
||||
body: body ? JSON.stringify(body) : undefined, signal: requestSignal });
|
||||
const response = await (this.fetcher??transport.fetch.bind(transport))(requestUrl, { method: body ? 'POST' : 'GET', headers,
|
||||
body: body ? JSON.stringify(body) : undefined, signal: requestSignal, redirect: 'manual' });
|
||||
// Never forward either credential header to a redirect target. The
|
||||
// observed response URL is diagnostic data; queries/credentials are removed.
|
||||
let responseUrl = null;
|
||||
if (response.url) { const observed = new URL(response.url); observed.username = ''; observed.password = ''; observed.search = ''; observed.hash = ''; responseUrl = this.redact(observed.href, key); }
|
||||
Object.assign(receipt, { responseUrl, redirected: response.redirected === true, redirectMode: 'manual' });
|
||||
Object.assign(context, { responseUrl, redirected: receipt.redirected, redirectMode: 'manual' });
|
||||
const metadata = upstreamCapture({ raw: Buffer.alloc(0), status: response.status, headers: response.headers }, value => this.redact(value, key)).details;
|
||||
receipt.upstreamRequestId = metadata.requestId; receipt.httpStatus = response.status;
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, upstreamResponse: { httpStatus: response.status, requestId: metadata.requestId, headers: metadata.headers } });
|
||||
onProgress?.({ phase: 'running', attempt, maxAttempts: policy.attempts, upstreamResponse: { httpStatus: response.status, requestId: metadata.requestId, headers: metadata.headers,
|
||||
requestUrl, responseUrl, redirected: receipt.redirected, redirectMode: 'manual' } });
|
||||
if (!response.ok) {
|
||||
const received = await readErrorResponse(response, requestSignal);
|
||||
if (!received.truncated && !received.interrupted) { try { usage = reportedUsage(config, JSON.parse(received.raw.toString('utf8'))); } catch {} }
|
||||
@@ -225,7 +238,7 @@ export class Provider {
|
||||
assertIdentity();
|
||||
const value = options.transformResponse ? options.transformResponse(streamed.value) : streamed.value;
|
||||
outcome = 'completed';
|
||||
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, modelIdentity: receipt.modelIdentity, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt, stream: streamed.evidence };
|
||||
this.lastRequest = { source: 'model_connection', ...context, modelIdentity: receipt.modelIdentity, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt, stream: streamed.evidence };
|
||||
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt, stream: streamed.evidence, modelIdentity: receipt.modelIdentity });
|
||||
return value;
|
||||
}
|
||||
@@ -237,7 +250,7 @@ export class Provider {
|
||||
throw new ModelRequestError({ ...context, id: randomUUID(), source: 'model_connection', stage: 'model_request',
|
||||
origin: 'local', upstream,
|
||||
kind: 'protocol', title: '模型接口响应格式不兼容', message: '模型服务没有返回有效 JSON,请核对兼容接口地址。',
|
||||
attempts: attempt, retryable: false, occurredAt: new Date().toISOString(), suggestion: '填写服务实际提供的 Chat Completions 接口根地址。' });
|
||||
attempts: attempt, retryable: false, occurredAt: new Date().toISOString(), suggestion: '核对服务实际提供的协议与本次请求地址。' });
|
||||
}
|
||||
if (result.error || config.protocol === 'responses' && ['failed', 'incomplete', 'cancelled'].includes(result.status)) {
|
||||
const upstream = captureResponse(response, { raw: Buffer.from(text, 'utf8') });
|
||||
@@ -257,7 +270,7 @@ export class Provider {
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
this.lastRequest = { source: 'model_connection', endpoint: base, route, model: config.model, modelIdentity: receipt.modelIdentity, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt };
|
||||
this.lastRequest = { source: 'model_connection', ...context, modelIdentity: receipt.modelIdentity, status: 'completed', attempts: attempt, durationMs: Date.now() - startedAt };
|
||||
outcome = 'completed';
|
||||
onProgress?.({ phase: 'completed', attempt, maxAttempts: policy.attempts, durationMs: Date.now() - startedAt, modelIdentity: receipt.modelIdentity });
|
||||
return result;
|
||||
@@ -266,7 +279,9 @@ export class Provider {
|
||||
signal?.throwIfAborted();
|
||||
let diagnostic = connectionFailure(error, { ...context, method:body?'POST':'GET',responseReceived:receipt.httpStatus!==undefined,
|
||||
transport:this.transport?.profile,timedOut: !!timeout.timeout, timeout: timeout.timeout, redact: value => this.redact(value, key) });
|
||||
diagnostic = { ...diagnostic, origin: diagnostic.origin ?? 'local', wireAttemptId: receipt.attemptId, upstreamRequestId: diagnostic.upstreamRequestId ?? receipt.upstreamRequestId,
|
||||
diagnostic = { ...diagnostic, requestUrl, ...(receipt.responseUrl ? { responseUrl: receipt.responseUrl } : {}),
|
||||
...(receipt.redirectMode ? { redirected: receipt.redirected, redirectMode: receipt.redirectMode } : {}),
|
||||
origin: diagnostic.origin ?? 'local', wireAttemptId: receipt.attemptId, upstreamRequestId: diagnostic.upstreamRequestId ?? receipt.upstreamRequestId,
|
||||
...(responseDetails && !diagnostic.upstream ? { upstream: responseDetails } : {}) };
|
||||
if (readingStream && diagnostic.kind !== 'model_identity') diagnostic = { ...diagnostic, retryable: false, failoverAllowed: false, outcome: streamTerminalSeen ? 'failed' : 'unknown',
|
||||
kind: diagnostic.origin === 'upstream' ? diagnostic.kind : timeout.timeout ? 'timeout' : streamTerminalSeen || diagnostic.kind === 'protocol' ? 'protocol' : 'stream_interrupted',
|
||||
@@ -297,7 +312,7 @@ export class Provider {
|
||||
this.activeRequests--;
|
||||
operation.finish();
|
||||
if (signal?.aborted) this.lastRequest = { source: 'model_connection', stage: 'model_request',
|
||||
endpoint: base, route, model: config.model, status: 'interrupted', attempts,
|
||||
endpoint: base, route, requestUrl, model: config.model, status: 'interrupted', attempts,
|
||||
durationMs: Date.now() - startedAt, occurredAt: new Date().toISOString() };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -552,6 +552,7 @@ export class Store extends EventEmitter {
|
||||
receipts.push({ attemptId: receipt.attemptId, requestId: receipt.requestId ?? null,
|
||||
connectionId: receipt.connectionId ?? null, connectionName: receipt.connectionName ?? null,
|
||||
model: receipt.model, endpoint: receipt.endpoint, protocol: receipt.protocol,
|
||||
...(receipt.requestUrl ? { requestUrl: receipt.requestUrl, responseUrl: receipt.responseUrl ?? null, redirected: receipt.redirected === true, redirectMode: receipt.redirectMode } : {}),
|
||||
startedAt: receipt.startedAt, finishedAt: receipt.finishedAt, outcome: receipt.outcome,
|
||||
usageStatus: receipt.usage ? 'reported' : 'unknown', usage: receipt.usage ?? null,
|
||||
...(receipt.localEstimate?{localEstimate:structuredClone(receipt.localEstimate)}:{}),
|
||||
|
||||
Reference in New Issue
Block a user