@nextclaw/ncp 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +29 -1
- package/dist/index.js +195 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -232,6 +232,9 @@ type ListMessagesOptions = {
|
|
|
232
232
|
limit?: number;
|
|
233
233
|
cursor?: string;
|
|
234
234
|
};
|
|
235
|
+
type NcpSessionPatch = {
|
|
236
|
+
metadata?: Record<string, unknown> | null;
|
|
237
|
+
};
|
|
235
238
|
/**
|
|
236
239
|
* API for session list, message history, and session lifecycle.
|
|
237
240
|
* Implementations that support persistence can provide this alongside NcpAgentClientEndpoint.
|
|
@@ -240,6 +243,7 @@ interface NcpSessionApi {
|
|
|
240
243
|
listSessions(options?: ListSessionsOptions): Promise<NcpSessionSummary[]>;
|
|
241
244
|
listSessionMessages(sessionId: string, options?: ListMessagesOptions): Promise<NcpMessage[]>;
|
|
242
245
|
getSession(sessionId: string): Promise<NcpSessionSummary | null>;
|
|
246
|
+
updateSession(sessionId: string, patch: NcpSessionPatch): Promise<NcpSessionSummary | null>;
|
|
243
247
|
deleteSession(sessionId: string): Promise<void>;
|
|
244
248
|
}
|
|
245
249
|
|
|
@@ -939,4 +943,28 @@ type NcpReplyTagParseResult = {
|
|
|
939
943
|
declare function stripReplyTagsFromText(content: string, currentMessageId?: string): NcpReplyTagParseResult;
|
|
940
944
|
declare function sanitizeAssistantReplyTags(message: NcpMessage, currentMessageId?: string): NcpMessage;
|
|
941
945
|
|
|
942
|
-
|
|
946
|
+
type NcpAssistantReasoningNormalizationMode = "off" | "think-tags";
|
|
947
|
+
type NcpAssistantReasoningSegment = {
|
|
948
|
+
type: "text" | "reasoning";
|
|
949
|
+
text: string;
|
|
950
|
+
};
|
|
951
|
+
declare function readAssistantReasoningNormalizationMode(value: unknown): NcpAssistantReasoningNormalizationMode | null;
|
|
952
|
+
declare function readAssistantReasoningNormalizationModeFromMetadata(metadata: Record<string, unknown> | null | undefined): NcpAssistantReasoningNormalizationMode | null;
|
|
953
|
+
declare function writeAssistantReasoningNormalizationModeToMetadata(metadata: Record<string, unknown>, mode: NcpAssistantReasoningNormalizationMode): Record<string, unknown>;
|
|
954
|
+
declare class NcpAssistantTextStreamNormalizer {
|
|
955
|
+
private readonly mode;
|
|
956
|
+
private buffer;
|
|
957
|
+
private phase;
|
|
958
|
+
private stripLeadingTextControlTags;
|
|
959
|
+
constructor(mode?: NcpAssistantReasoningNormalizationMode);
|
|
960
|
+
push(delta: string): NcpAssistantReasoningSegment[];
|
|
961
|
+
finish(): NcpAssistantReasoningSegment[];
|
|
962
|
+
private flush;
|
|
963
|
+
}
|
|
964
|
+
declare function normalizeAssistantText(text: string, mode: NcpAssistantReasoningNormalizationMode): {
|
|
965
|
+
text: string;
|
|
966
|
+
reasoning: string;
|
|
967
|
+
parts: NcpAssistantReasoningSegment[];
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
export { type ListMessagesOptions, type ListSessionsOptions, type NcpActionPart, type NcpAgentClientEndpoint, type NcpAgentConversationHydrationParams, type NcpAgentConversationSnapshot, type NcpAgentConversationStateManager, type NcpAgentRunApi, type NcpAgentRunInput, type NcpAgentRunOptions, type NcpAgentRunSendOptions, type NcpAgentRunStreamOptions, type NcpAgentRuntime, type NcpAgentServerEndpoint, type NcpAgentStreamProvider, type NcpAssistantReasoningNormalizationMode, type NcpAssistantReasoningSegment, NcpAssistantTextStreamNormalizer, type NcpCardPart, type NcpCompletedEnvelope, type NcpContextBuilder, type NcpContextPrepareOptions, type NcpConversationSnapshot, type NcpConversationStateManager, type NcpEncodeContext, type NcpEndpoint, type NcpEndpointEvent, type NcpEndpointKind, type NcpEndpointLatency, type NcpEndpointManifest, type NcpEndpointSubscriber, type NcpError, type NcpErrorCode, NcpEventType, type NcpExtensionPart, type NcpFailedEnvelope, type NcpFilePart, type NcpInvalidToolArgumentsResult, type NcpLLMApi, type NcpLLMApiInput, type NcpLLMApiOptions, type NcpMessage, type NcpMessageAbortPayload, type NcpMessageAcceptedPayload, type NcpMessageDeliveredPayload, type NcpMessagePart, type NcpMessageReactionPayload, type NcpMessageReadPayload, type NcpMessageRecalledPayload, type NcpMessageRole, type NcpMessageSentPayload, type NcpMessageStatus, type NcpPendingToolCall, type NcpPresenceUpdatedPayload, type NcpReasoningDeltaPayload, type NcpReasoningEndPayload, type NcpReasoningPart, type NcpReasoningStartPayload, type NcpReplyTagParseResult, type NcpRequestEnvelope, type NcpResponseEnvelope, type NcpRichTextPart, type NcpRoundBuffer, type NcpRunContext, type NcpRunErrorPayload, type NcpRunFinalMetadata, type NcpRunFinishedPayload, type NcpRunMetadataPayload, type NcpRunReadyMetadata, type NcpRunStartedPayload, type NcpSessionApi, type NcpSessionPatch, type NcpSessionStatus, type NcpSessionSummary, type NcpSourcePart, type NcpStepStartPart, type NcpStreamEncoder, type NcpStreamRequestPayload, type NcpTextDeltaPayload, type NcpTextEndPayload, type NcpTextPart, type NcpTextStartPayload, type NcpTool, type NcpToolCallArgsDeltaPayload, type NcpToolCallArgsPayload, type NcpToolCallEndPayload, type NcpToolCallResult, type NcpToolCallResultPayload, type NcpToolCallStartPayload, type NcpToolDefinition, type NcpToolInvocationPart, type NcpToolRegistry, type NcpTypingEndPayload, type NcpTypingStartPayload, type OpenAIChatChunk, type OpenAIChatMessage, type OpenAIContentPart, type OpenAITool, type OpenAIToolCall, type OpenAIToolCallDelta, normalizeAssistantText, readAssistantReasoningNormalizationMode, readAssistantReasoningNormalizationModeFromMetadata, sanitizeAssistantReplyTags, stripReplyTagsFromText, writeAssistantReasoningNormalizationModeToMetadata };
|
package/dist/index.js
CHANGED
|
@@ -112,8 +112,202 @@ function sanitizeAssistantReplyTags(message, currentMessageId = message.id) {
|
|
|
112
112
|
metadata: nextMetadata
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
// src/toolkit/reasoning-normalization.ts
|
|
117
|
+
var THINK_OPEN_TAG = "<think>";
|
|
118
|
+
var THINK_CLOSE_TAG = "</think>";
|
|
119
|
+
var FINAL_OPEN_TAG = "<final>";
|
|
120
|
+
var FINAL_CLOSE_TAG = "</final>";
|
|
121
|
+
var LEADING_CONTROL_TAGS = [THINK_CLOSE_TAG, FINAL_OPEN_TAG, FINAL_CLOSE_TAG];
|
|
122
|
+
var MAX_REASONING_DELIMITER_TAIL = THINK_CLOSE_TAG.length - 1;
|
|
123
|
+
function isRecord(value) {
|
|
124
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
125
|
+
}
|
|
126
|
+
function isWhitespace(char) {
|
|
127
|
+
return /\s/.test(char);
|
|
128
|
+
}
|
|
129
|
+
function normalizeMode(value) {
|
|
130
|
+
if (value === "off" || value === "think-tags") {
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
function readLeadingThinkTagState(buffer) {
|
|
136
|
+
let cursor = 0;
|
|
137
|
+
while (cursor < buffer.length && isWhitespace(buffer[cursor])) {
|
|
138
|
+
cursor += 1;
|
|
139
|
+
}
|
|
140
|
+
const rest = buffer.slice(cursor);
|
|
141
|
+
if (rest.startsWith(THINK_OPEN_TAG)) {
|
|
142
|
+
return {
|
|
143
|
+
status: "matched",
|
|
144
|
+
consumed: cursor + THINK_OPEN_TAG.length
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (THINK_OPEN_TAG.startsWith(rest)) {
|
|
148
|
+
return { status: "pending" };
|
|
149
|
+
}
|
|
150
|
+
return { status: "rejected" };
|
|
151
|
+
}
|
|
152
|
+
function stripLeadingControlTags(buffer) {
|
|
153
|
+
let nextBuffer = buffer;
|
|
154
|
+
while (true) {
|
|
155
|
+
let cursor = 0;
|
|
156
|
+
while (cursor < nextBuffer.length && isWhitespace(nextBuffer[cursor])) {
|
|
157
|
+
cursor += 1;
|
|
158
|
+
}
|
|
159
|
+
const rest = nextBuffer.slice(cursor);
|
|
160
|
+
if (rest.length === 0) {
|
|
161
|
+
return { buffer: "", pending: false };
|
|
162
|
+
}
|
|
163
|
+
const matchedTag = LEADING_CONTROL_TAGS.find((tag) => rest.startsWith(tag));
|
|
164
|
+
if (matchedTag) {
|
|
165
|
+
nextBuffer = rest.slice(matchedTag.length);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (LEADING_CONTROL_TAGS.some((tag) => tag.startsWith(rest))) {
|
|
169
|
+
return { buffer: rest, pending: true };
|
|
170
|
+
}
|
|
171
|
+
return { buffer: rest, pending: false };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function findReasoningBoundary(buffer) {
|
|
175
|
+
const indices = [
|
|
176
|
+
{ index: buffer.indexOf(THINK_CLOSE_TAG), tag: THINK_CLOSE_TAG },
|
|
177
|
+
{ index: buffer.indexOf(FINAL_OPEN_TAG), tag: FINAL_OPEN_TAG }
|
|
178
|
+
].filter((entry) => entry.index >= 0);
|
|
179
|
+
if (indices.length === 0) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
return indices.reduce((left, right) => right.index < left.index ? right : left);
|
|
183
|
+
}
|
|
184
|
+
function readAssistantReasoningNormalizationMode(value) {
|
|
185
|
+
if (typeof value === "string") {
|
|
186
|
+
return normalizeMode(value);
|
|
187
|
+
}
|
|
188
|
+
if (isRecord(value)) {
|
|
189
|
+
return normalizeMode(value.mode);
|
|
190
|
+
}
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
function readAssistantReasoningNormalizationModeFromMetadata(metadata) {
|
|
194
|
+
if (!metadata) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
return readAssistantReasoningNormalizationMode(metadata.reasoning_normalization) ?? readAssistantReasoningNormalizationMode(metadata.reasoningNormalization) ?? normalizeMode(metadata.reasoning_normalization_mode) ?? normalizeMode(metadata.reasoningNormalizationMode);
|
|
198
|
+
}
|
|
199
|
+
function writeAssistantReasoningNormalizationModeToMetadata(metadata, mode) {
|
|
200
|
+
const nextMetadata = {
|
|
201
|
+
...metadata
|
|
202
|
+
};
|
|
203
|
+
delete nextMetadata.reasoningNormalization;
|
|
204
|
+
delete nextMetadata.reasoningNormalizationMode;
|
|
205
|
+
delete nextMetadata.reasoning_normalization_mode;
|
|
206
|
+
nextMetadata.reasoning_normalization = { mode };
|
|
207
|
+
return nextMetadata;
|
|
208
|
+
}
|
|
209
|
+
var NcpAssistantTextStreamNormalizer = class {
|
|
210
|
+
constructor(mode = "off") {
|
|
211
|
+
this.mode = mode;
|
|
212
|
+
}
|
|
213
|
+
buffer = "";
|
|
214
|
+
phase = "initial";
|
|
215
|
+
stripLeadingTextControlTags = false;
|
|
216
|
+
push(delta) {
|
|
217
|
+
if (!delta) {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
if (this.mode !== "think-tags") {
|
|
221
|
+
return [{ type: "text", text: delta }];
|
|
222
|
+
}
|
|
223
|
+
this.buffer += delta;
|
|
224
|
+
return this.flush(false);
|
|
225
|
+
}
|
|
226
|
+
finish() {
|
|
227
|
+
if (this.mode !== "think-tags") {
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
return this.flush(true);
|
|
231
|
+
}
|
|
232
|
+
flush(final) {
|
|
233
|
+
const segments = [];
|
|
234
|
+
while (true) {
|
|
235
|
+
if (this.phase === "initial") {
|
|
236
|
+
const leadingState = readLeadingThinkTagState(this.buffer);
|
|
237
|
+
if (leadingState.status === "matched") {
|
|
238
|
+
this.buffer = this.buffer.slice(leadingState.consumed);
|
|
239
|
+
this.phase = "reasoning";
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (leadingState.status === "pending" && !final) {
|
|
243
|
+
return segments;
|
|
244
|
+
}
|
|
245
|
+
this.phase = "text";
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (this.phase === "reasoning") {
|
|
249
|
+
const boundary = findReasoningBoundary(this.buffer);
|
|
250
|
+
if (boundary) {
|
|
251
|
+
const reasoningText = this.buffer.slice(0, boundary.index);
|
|
252
|
+
if (reasoningText.length > 0) {
|
|
253
|
+
segments.push({ type: "reasoning", text: reasoningText });
|
|
254
|
+
}
|
|
255
|
+
this.buffer = this.buffer.slice(boundary.index + boundary.tag.length);
|
|
256
|
+
this.phase = "text";
|
|
257
|
+
this.stripLeadingTextControlTags = true;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const safeLength = final ? this.buffer.length : Math.max(0, this.buffer.length - MAX_REASONING_DELIMITER_TAIL);
|
|
261
|
+
if (safeLength === 0) {
|
|
262
|
+
return segments;
|
|
263
|
+
}
|
|
264
|
+
segments.push({
|
|
265
|
+
type: "reasoning",
|
|
266
|
+
text: this.buffer.slice(0, safeLength)
|
|
267
|
+
});
|
|
268
|
+
this.buffer = this.buffer.slice(safeLength);
|
|
269
|
+
if (!final) {
|
|
270
|
+
return segments;
|
|
271
|
+
}
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (this.stripLeadingTextControlTags) {
|
|
275
|
+
const stripped = stripLeadingControlTags(this.buffer);
|
|
276
|
+
if (stripped.pending && !final) {
|
|
277
|
+
this.buffer = stripped.buffer;
|
|
278
|
+
return segments;
|
|
279
|
+
}
|
|
280
|
+
this.buffer = stripped.buffer;
|
|
281
|
+
this.stripLeadingTextControlTags = false;
|
|
282
|
+
}
|
|
283
|
+
if (this.buffer.length === 0) {
|
|
284
|
+
return segments;
|
|
285
|
+
}
|
|
286
|
+
segments.push({
|
|
287
|
+
type: "text",
|
|
288
|
+
text: this.buffer
|
|
289
|
+
});
|
|
290
|
+
this.buffer = "";
|
|
291
|
+
return segments;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
function normalizeAssistantText(text, mode) {
|
|
296
|
+
const normalizer = new NcpAssistantTextStreamNormalizer(mode);
|
|
297
|
+
const parts = [...normalizer.push(text), ...normalizer.finish()].filter((part) => part.text.length > 0);
|
|
298
|
+
return {
|
|
299
|
+
text: parts.filter((part) => part.type === "text").map((part) => part.text).join(""),
|
|
300
|
+
reasoning: parts.filter((part) => part.type === "reasoning").map((part) => part.text).join(""),
|
|
301
|
+
parts
|
|
302
|
+
};
|
|
303
|
+
}
|
|
115
304
|
export {
|
|
305
|
+
NcpAssistantTextStreamNormalizer,
|
|
116
306
|
NcpEventType,
|
|
307
|
+
normalizeAssistantText,
|
|
308
|
+
readAssistantReasoningNormalizationMode,
|
|
309
|
+
readAssistantReasoningNormalizationModeFromMetadata,
|
|
117
310
|
sanitizeAssistantReplyTags,
|
|
118
|
-
stripReplyTagsFromText
|
|
311
|
+
stripReplyTagsFromText,
|
|
312
|
+
writeAssistantReasoningNormalizationModeToMetadata
|
|
119
313
|
};
|