@animalabs/membrane 0.1.0
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/context/index.d.ts +10 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +9 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/process.d.ts +22 -0
- package/dist/context/process.d.ts.map +1 -0
- package/dist/context/process.js +369 -0
- package/dist/context/process.js.map +1 -0
- package/dist/context/types.d.ts +118 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/context/types.js +60 -0
- package/dist/context/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/membrane.d.ts +96 -0
- package/dist/membrane.d.ts.map +1 -0
- package/dist/membrane.js +893 -0
- package/dist/membrane.js.map +1 -0
- package/dist/providers/anthropic.d.ts +36 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +265 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +8 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +74 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +412 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai.d.ts +69 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +455 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openrouter.d.ts +76 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +492 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/transforms/chat.d.ts +52 -0
- package/dist/transforms/chat.d.ts.map +1 -0
- package/dist/transforms/chat.js +136 -0
- package/dist/transforms/chat.js.map +1 -0
- package/dist/transforms/index.d.ts +6 -0
- package/dist/transforms/index.d.ts.map +1 -0
- package/dist/transforms/index.js +6 -0
- package/dist/transforms/index.js.map +1 -0
- package/dist/transforms/prefill.d.ts +89 -0
- package/dist/transforms/prefill.d.ts.map +1 -0
- package/dist/transforms/prefill.js +401 -0
- package/dist/transforms/prefill.js.map +1 -0
- package/dist/types/config.d.ts +103 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +21 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/content.d.ts +81 -0
- package/dist/types/content.d.ts.map +1 -0
- package/dist/types/content.js +40 -0
- package/dist/types/content.js.map +1 -0
- package/dist/types/errors.d.ts +42 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +208 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +9 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/message.d.ts +46 -0
- package/dist/types/message.d.ts.map +1 -0
- package/dist/types/message.js +38 -0
- package/dist/types/message.js.map +1 -0
- package/dist/types/provider.d.ts +155 -0
- package/dist/types/provider.d.ts.map +1 -0
- package/dist/types/provider.js +5 -0
- package/dist/types/provider.js.map +1 -0
- package/dist/types/request.d.ts +78 -0
- package/dist/types/request.d.ts.map +1 -0
- package/dist/types/request.js +5 -0
- package/dist/types/request.js.map +1 -0
- package/dist/types/response.d.ts +131 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +7 -0
- package/dist/types/response.js.map +1 -0
- package/dist/types/streaming.d.ts +164 -0
- package/dist/types/streaming.d.ts.map +1 -0
- package/dist/types/streaming.js +5 -0
- package/dist/types/streaming.js.map +1 -0
- package/dist/types/tools.d.ts +71 -0
- package/dist/types/tools.d.ts.map +1 -0
- package/dist/types/tools.js +5 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/stream-parser.d.ts +53 -0
- package/dist/utils/stream-parser.d.ts.map +1 -0
- package/dist/utils/stream-parser.js +359 -0
- package/dist/utils/stream-parser.js.map +1 -0
- package/dist/utils/tool-parser.d.ts +130 -0
- package/dist/utils/tool-parser.d.ts.map +1 -0
- package/dist/utils/tool-parser.js +571 -0
- package/dist/utils/tool-parser.js.map +1 -0
- package/package.json +37 -0
- package/src/context/index.ts +24 -0
- package/src/context/process.ts +520 -0
- package/src/context/types.ts +231 -0
- package/src/index.ts +23 -0
- package/src/membrane.ts +1174 -0
- package/src/providers/anthropic.ts +340 -0
- package/src/providers/index.ts +31 -0
- package/src/providers/openai-compatible.ts +570 -0
- package/src/providers/openai.ts +625 -0
- package/src/providers/openrouter.ts +662 -0
- package/src/transforms/chat.ts +212 -0
- package/src/transforms/index.ts +22 -0
- package/src/transforms/prefill.ts +585 -0
- package/src/types/config.ts +172 -0
- package/src/types/content.ts +181 -0
- package/src/types/errors.ts +277 -0
- package/src/types/index.ts +154 -0
- package/src/types/message.ts +89 -0
- package/src/types/provider.ts +249 -0
- package/src/types/request.ts +131 -0
- package/src/types/response.ts +223 -0
- package/src/types/streaming.ts +231 -0
- package/src/types/tools.ts +92 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/stream-parser.ts +440 -0
- package/src/utils/tool-parser.ts +715 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool definition and execution types
|
|
3
|
+
*/
|
|
4
|
+
export interface ToolParameter {
|
|
5
|
+
type: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
enum?: string[];
|
|
8
|
+
items?: ToolParameter;
|
|
9
|
+
properties?: Record<string, ToolParameter>;
|
|
10
|
+
required?: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface ToolDefinition {
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
inputSchema: {
|
|
16
|
+
type: 'object';
|
|
17
|
+
properties: Record<string, ToolParameter>;
|
|
18
|
+
required?: string[];
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface ToolCall {
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
input: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
export interface ToolResult {
|
|
27
|
+
toolUseId: string;
|
|
28
|
+
/**
|
|
29
|
+
* Result content - can be string or structured content blocks (for images).
|
|
30
|
+
* For XML mode, images are noted in text. For native mode, passed as content blocks.
|
|
31
|
+
*/
|
|
32
|
+
content: string | ToolResultContentBlock[];
|
|
33
|
+
isError?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Content block types allowed in tool results
|
|
37
|
+
*/
|
|
38
|
+
export type ToolResultContentBlock = {
|
|
39
|
+
type: 'text';
|
|
40
|
+
text: string;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'image';
|
|
43
|
+
source: {
|
|
44
|
+
type: 'base64';
|
|
45
|
+
data: string;
|
|
46
|
+
mediaType: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export interface ToolContext {
|
|
50
|
+
/** The raw text that contained the tool calls */
|
|
51
|
+
rawText: string;
|
|
52
|
+
/** Text before the tool calls (already streamed to user) */
|
|
53
|
+
preamble: string;
|
|
54
|
+
/** Current depth in tool execution loop */
|
|
55
|
+
depth: number;
|
|
56
|
+
/** Previous tool results in this execution chain */
|
|
57
|
+
previousResults: ToolResult[];
|
|
58
|
+
/** Accumulated output so far */
|
|
59
|
+
accumulated: string;
|
|
60
|
+
}
|
|
61
|
+
export interface ParsedToolCalls {
|
|
62
|
+
/** Parsed tool calls */
|
|
63
|
+
calls: ToolCall[];
|
|
64
|
+
/** Text before the tool calls block */
|
|
65
|
+
beforeText: string;
|
|
66
|
+
/** Text after the tool calls block */
|
|
67
|
+
afterText: string;
|
|
68
|
+
/** The full matched tool calls XML block */
|
|
69
|
+
fullMatch: string;
|
|
70
|
+
}
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SAAS,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC;AAMnF,MAAM,WAAW,WAAW;IAC1B,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC;IAEhB,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IAEjB,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IAEd,oDAAoD;IACpD,eAAe,EAAE,UAAU,EAAE,CAAC;IAE9B,gCAAgC;IAChC,WAAW,EAAE,MAAM,CAAC;CACrB;AAMD,MAAM,WAAW,eAAe;IAC9B,wBAAwB;IACxB,KAAK,EAAE,QAAQ,EAAE,CAAC;IAElB,uCAAuC;IACvC,UAAU,EAAE,MAAM,CAAC;IAEnB,sCAAsC;IACtC,SAAS,EAAE,MAAM,CAAC;IAElB,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/types/tools.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility exports
|
|
3
|
+
*/
|
|
4
|
+
export { parseToolCalls, formatToolResults, formatToolResult, formatToolDefinitions, getToolInstructions, hasUnclosedToolBlock, endsWithPartialToolBlock, unescapeXml, type ToolDefinitionForPrompt, } from './tool-parser.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,oBAAoB,EACpB,wBAAwB,EACxB,WAAW,EACX,KAAK,uBAAuB,GAC7B,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,oBAAoB,EACpB,wBAAwB,EACxB,WAAW,GAEZ,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Incremental XML parser for streaming
|
|
3
|
+
*
|
|
4
|
+
* Tracks nesting depth of XML blocks as tokens arrive, enabling:
|
|
5
|
+
* - False-positive stop sequence detection
|
|
6
|
+
* - Structured block events for UI
|
|
7
|
+
* - Enriched chunk metadata for TTS/display filtering
|
|
8
|
+
*/
|
|
9
|
+
import type { BlockEvent, ChunkMeta, MembraneBlockType } from '../types/streaming.js';
|
|
10
|
+
export interface ProcessChunkResult {
|
|
11
|
+
content: Array<{
|
|
12
|
+
text: string;
|
|
13
|
+
meta: ChunkMeta;
|
|
14
|
+
}>;
|
|
15
|
+
blockEvents: BlockEvent[];
|
|
16
|
+
}
|
|
17
|
+
export declare class IncrementalXmlParser {
|
|
18
|
+
private state;
|
|
19
|
+
constructor();
|
|
20
|
+
push(chunk: string): BlockEvent[];
|
|
21
|
+
isInsideBlock(): boolean;
|
|
22
|
+
isInsideFunctionResults(): boolean;
|
|
23
|
+
isInsideFunctionCalls(): boolean;
|
|
24
|
+
getContext(): string;
|
|
25
|
+
getAccumulated(): string;
|
|
26
|
+
getDepths(): {
|
|
27
|
+
functionCalls: number;
|
|
28
|
+
functionResults: number;
|
|
29
|
+
thinking: number;
|
|
30
|
+
};
|
|
31
|
+
reset(): void;
|
|
32
|
+
finish(): BlockEvent[];
|
|
33
|
+
processChunk(chunk: string): ProcessChunkResult;
|
|
34
|
+
flush(): ProcessChunkResult;
|
|
35
|
+
getCurrentBlockType(): MembraneBlockType;
|
|
36
|
+
private isCompleteMembraneTag;
|
|
37
|
+
private cantBeMembraneTag;
|
|
38
|
+
private handleMembraneTag;
|
|
39
|
+
private ensureBlockStarted;
|
|
40
|
+
private makeBlockStart;
|
|
41
|
+
private makeBlockComplete;
|
|
42
|
+
private getCurrentMeta;
|
|
43
|
+
private scanForDepth;
|
|
44
|
+
private scan;
|
|
45
|
+
private findPartialTagAtEnd;
|
|
46
|
+
}
|
|
47
|
+
export declare function hasUnclosedXmlBlock(text: string): boolean;
|
|
48
|
+
export declare function countTags(text: string, openPattern: RegExp, closePattern: RegExp): {
|
|
49
|
+
open: number;
|
|
50
|
+
close: number;
|
|
51
|
+
depth: number;
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=stream-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-parser.d.ts","sourceRoot":"","sources":["../../src/utils/stream-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,SAAS,EAET,iBAAiB,EAClB,MAAM,uBAAuB,CAAC;AAM/B,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,SAAS,CAAA;KAAE,CAAC,CAAC;IAClD,WAAW,EAAE,UAAU,EAAE,CAAC;CAC3B;AAyED,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,KAAK,CAAc;;IAM3B,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,EAAE;IAKjC,aAAa,IAAI,OAAO;IAQxB,uBAAuB,IAAI,OAAO;IAIlC,qBAAqB,IAAI,OAAO;IAIhC,UAAU,IAAI,MAAM;IAcpB,cAAc,IAAI,MAAM;IAIxB,SAAS,IAAI;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;IAQjF,KAAK,IAAI,IAAI;IAIb,MAAM,IAAI,UAAU,EAAE;IAQtB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,kBAAkB;IAsD/C,KAAK,IAAI,kBAAkB;IAkB3B,mBAAmB,IAAI,iBAAiB;IAWxC,OAAO,CAAC,qBAAqB;IAK7B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,iBAAiB;IAgDzB,OAAO,CAAC,kBAAkB;IAM1B,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,iBAAiB;IAmBzB,OAAO,CAAC,cAAc;IAatB,OAAO,CAAC,YAAY;IAyCpB,OAAO,CAAC,IAAI;IAKZ,OAAO,CAAC,mBAAmB;CAQ5B;AAMD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAIzD;AAED,wBAAgB,SAAS,CACvB,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,GACnB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAUhD"}
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Incremental XML parser for streaming
|
|
3
|
+
*
|
|
4
|
+
* Tracks nesting depth of XML blocks as tokens arrive, enabling:
|
|
5
|
+
* - False-positive stop sequence detection
|
|
6
|
+
* - Structured block events for UI
|
|
7
|
+
* - Enriched chunk metadata for TTS/display filtering
|
|
8
|
+
*/
|
|
9
|
+
function createInitialState() {
|
|
10
|
+
return {
|
|
11
|
+
functionCallsDepth: 0,
|
|
12
|
+
functionResultsDepth: 0,
|
|
13
|
+
thinkingDepth: 0,
|
|
14
|
+
accumulated: '',
|
|
15
|
+
lastScanPos: 0,
|
|
16
|
+
blockIndex: 0,
|
|
17
|
+
currentBlockStarted: false,
|
|
18
|
+
currentBlockContent: '',
|
|
19
|
+
currentBlockType: 'text',
|
|
20
|
+
tagBuffer: '',
|
|
21
|
+
toolCallState: {
|
|
22
|
+
inInvoke: false,
|
|
23
|
+
currentToolName: '',
|
|
24
|
+
currentToolId: '',
|
|
25
|
+
inParameter: false,
|
|
26
|
+
currentParamName: '',
|
|
27
|
+
paramContent: '',
|
|
28
|
+
allParams: {},
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// For finding all membrane tags in one pass
|
|
33
|
+
const ALL_TAGS = /<\/?(?:antml:)?(?:function_calls|function_results|thinking)>/g;
|
|
34
|
+
// For matching complete membrane tags
|
|
35
|
+
const COMPLETE_MEMBRANE_TAG = /^<\/?(?:antml:)?(?:function_calls|function_results|thinking|invoke|parameter)(?:\s[^>]*)?>$/;
|
|
36
|
+
// Known membrane tag prefixes
|
|
37
|
+
const MEMBRANE_TAG_PREFIXES = [
|
|
38
|
+
'<thinking', '<\/thinking>',
|
|
39
|
+
'<function_calls', '<\/function_calls>',
|
|
40
|
+
'<function_results', '<\/function_results>',
|
|
41
|
+
'<invoke', '<\/invoke>',
|
|
42
|
+
'<parameter', '<\/parameter>',
|
|
43
|
+
'<function_calls', '<\/antml:function_calls>',
|
|
44
|
+
'<invoke', '<\/antml:invoke>',
|
|
45
|
+
];
|
|
46
|
+
// ============================================================================
|
|
47
|
+
// Incremental XML Parser
|
|
48
|
+
// ============================================================================
|
|
49
|
+
export class IncrementalXmlParser {
|
|
50
|
+
state;
|
|
51
|
+
constructor() {
|
|
52
|
+
this.state = createInitialState();
|
|
53
|
+
}
|
|
54
|
+
push(chunk) {
|
|
55
|
+
this.state.accumulated += chunk;
|
|
56
|
+
return this.scan();
|
|
57
|
+
}
|
|
58
|
+
isInsideBlock() {
|
|
59
|
+
return (this.state.functionCallsDepth > 0 ||
|
|
60
|
+
this.state.functionResultsDepth > 0 ||
|
|
61
|
+
this.state.thinkingDepth > 0);
|
|
62
|
+
}
|
|
63
|
+
isInsideFunctionResults() {
|
|
64
|
+
return this.state.functionResultsDepth > 0;
|
|
65
|
+
}
|
|
66
|
+
isInsideFunctionCalls() {
|
|
67
|
+
return this.state.functionCallsDepth > 0;
|
|
68
|
+
}
|
|
69
|
+
getContext() {
|
|
70
|
+
const parts = [];
|
|
71
|
+
if (this.state.functionCallsDepth > 0) {
|
|
72
|
+
parts.push('function_calls(' + this.state.functionCallsDepth + ')');
|
|
73
|
+
}
|
|
74
|
+
if (this.state.functionResultsDepth > 0) {
|
|
75
|
+
parts.push('function_results(' + this.state.functionResultsDepth + ')');
|
|
76
|
+
}
|
|
77
|
+
if (this.state.thinkingDepth > 0) {
|
|
78
|
+
parts.push('thinking(' + this.state.thinkingDepth + ')');
|
|
79
|
+
}
|
|
80
|
+
return parts.length > 0 ? parts.join(' > ') : 'none';
|
|
81
|
+
}
|
|
82
|
+
getAccumulated() {
|
|
83
|
+
return this.state.accumulated;
|
|
84
|
+
}
|
|
85
|
+
getDepths() {
|
|
86
|
+
return {
|
|
87
|
+
functionCalls: this.state.functionCallsDepth,
|
|
88
|
+
functionResults: this.state.functionResultsDepth,
|
|
89
|
+
thinking: this.state.thinkingDepth,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
reset() {
|
|
93
|
+
this.state = createInitialState();
|
|
94
|
+
}
|
|
95
|
+
finish() {
|
|
96
|
+
return this.flush().blockEvents;
|
|
97
|
+
}
|
|
98
|
+
// ============================================================================
|
|
99
|
+
// Enriched Streaming API
|
|
100
|
+
// ============================================================================
|
|
101
|
+
processChunk(chunk) {
|
|
102
|
+
const content = [];
|
|
103
|
+
const blockEvents = [];
|
|
104
|
+
// Also update accumulated and scan for depth tracking
|
|
105
|
+
this.state.accumulated += chunk;
|
|
106
|
+
this.scanForDepth();
|
|
107
|
+
let pos = 0;
|
|
108
|
+
while (pos < chunk.length) {
|
|
109
|
+
if (this.state.tagBuffer) {
|
|
110
|
+
const char = chunk[pos];
|
|
111
|
+
this.state.tagBuffer += char;
|
|
112
|
+
pos++;
|
|
113
|
+
if (this.isCompleteMembraneTag(this.state.tagBuffer)) {
|
|
114
|
+
const events = this.handleMembraneTag(this.state.tagBuffer);
|
|
115
|
+
blockEvents.push(...events);
|
|
116
|
+
this.state.tagBuffer = '';
|
|
117
|
+
}
|
|
118
|
+
else if (this.cantBeMembraneTag(this.state.tagBuffer)) {
|
|
119
|
+
this.ensureBlockStarted(blockEvents);
|
|
120
|
+
content.push({
|
|
121
|
+
text: this.state.tagBuffer,
|
|
122
|
+
meta: this.getCurrentMeta()
|
|
123
|
+
});
|
|
124
|
+
this.state.currentBlockContent += this.state.tagBuffer;
|
|
125
|
+
this.state.tagBuffer = '';
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const nextLt = chunk.indexOf('<', pos);
|
|
130
|
+
if (nextLt === -1) {
|
|
131
|
+
const text = chunk.slice(pos);
|
|
132
|
+
if (text) {
|
|
133
|
+
this.ensureBlockStarted(blockEvents);
|
|
134
|
+
content.push({ text, meta: this.getCurrentMeta() });
|
|
135
|
+
this.state.currentBlockContent += text;
|
|
136
|
+
}
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
if (nextLt > pos) {
|
|
141
|
+
const text = chunk.slice(pos, nextLt);
|
|
142
|
+
this.ensureBlockStarted(blockEvents);
|
|
143
|
+
content.push({ text, meta: this.getCurrentMeta() });
|
|
144
|
+
this.state.currentBlockContent += text;
|
|
145
|
+
}
|
|
146
|
+
this.state.tagBuffer = '<';
|
|
147
|
+
pos = nextLt + 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return { content, blockEvents };
|
|
152
|
+
}
|
|
153
|
+
flush() {
|
|
154
|
+
const content = [];
|
|
155
|
+
const blockEvents = [];
|
|
156
|
+
if (this.state.tagBuffer) {
|
|
157
|
+
this.ensureBlockStarted(blockEvents);
|
|
158
|
+
content.push({ text: this.state.tagBuffer, meta: this.getCurrentMeta() });
|
|
159
|
+
this.state.currentBlockContent += this.state.tagBuffer;
|
|
160
|
+
this.state.tagBuffer = '';
|
|
161
|
+
}
|
|
162
|
+
if (this.state.currentBlockStarted) {
|
|
163
|
+
blockEvents.push(this.makeBlockComplete());
|
|
164
|
+
}
|
|
165
|
+
return { content, blockEvents };
|
|
166
|
+
}
|
|
167
|
+
getCurrentBlockType() {
|
|
168
|
+
if (this.state.thinkingDepth > 0)
|
|
169
|
+
return 'thinking';
|
|
170
|
+
if (this.state.functionCallsDepth > 0)
|
|
171
|
+
return 'tool_call';
|
|
172
|
+
if (this.state.functionResultsDepth > 0)
|
|
173
|
+
return 'tool_result';
|
|
174
|
+
return 'text';
|
|
175
|
+
}
|
|
176
|
+
// ============================================================================
|
|
177
|
+
// Private Methods
|
|
178
|
+
// ============================================================================
|
|
179
|
+
isCompleteMembraneTag(buffer) {
|
|
180
|
+
if (!buffer.endsWith('>'))
|
|
181
|
+
return false;
|
|
182
|
+
return COMPLETE_MEMBRANE_TAG.test(buffer);
|
|
183
|
+
}
|
|
184
|
+
cantBeMembraneTag(buffer) {
|
|
185
|
+
if (buffer.endsWith('>')) {
|
|
186
|
+
return !this.isCompleteMembraneTag(buffer);
|
|
187
|
+
}
|
|
188
|
+
for (const prefix of MEMBRANE_TAG_PREFIXES) {
|
|
189
|
+
if (prefix.startsWith(buffer) || buffer.startsWith(prefix.slice(0, buffer.length))) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
handleMembraneTag(tag) {
|
|
196
|
+
const events = [];
|
|
197
|
+
const isClosing = tag.startsWith('</');
|
|
198
|
+
if (tag.includes('thinking')) {
|
|
199
|
+
if (!isClosing) {
|
|
200
|
+
if (this.state.currentBlockStarted) {
|
|
201
|
+
events.push(this.makeBlockComplete());
|
|
202
|
+
}
|
|
203
|
+
this.state.thinkingDepth++;
|
|
204
|
+
this.state.currentBlockType = 'thinking';
|
|
205
|
+
events.push(this.makeBlockStart('thinking'));
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
events.push(this.makeBlockComplete());
|
|
209
|
+
this.state.thinkingDepth--;
|
|
210
|
+
this.state.currentBlockType = this.getCurrentBlockType();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else if (tag.includes('function_calls')) {
|
|
214
|
+
if (!isClosing) {
|
|
215
|
+
if (this.state.currentBlockStarted) {
|
|
216
|
+
events.push(this.makeBlockComplete());
|
|
217
|
+
}
|
|
218
|
+
this.state.functionCallsDepth++;
|
|
219
|
+
this.state.currentBlockType = 'tool_call';
|
|
220
|
+
events.push(this.makeBlockStart('tool_call'));
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
events.push(this.makeBlockComplete());
|
|
224
|
+
this.state.functionCallsDepth--;
|
|
225
|
+
this.state.currentBlockType = this.getCurrentBlockType();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else if (tag.includes('function_results')) {
|
|
229
|
+
if (!isClosing) {
|
|
230
|
+
if (this.state.currentBlockStarted) {
|
|
231
|
+
events.push(this.makeBlockComplete());
|
|
232
|
+
}
|
|
233
|
+
this.state.functionResultsDepth++;
|
|
234
|
+
this.state.currentBlockType = 'tool_result';
|
|
235
|
+
events.push(this.makeBlockStart('tool_result'));
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
events.push(this.makeBlockComplete());
|
|
239
|
+
this.state.functionResultsDepth--;
|
|
240
|
+
this.state.currentBlockType = this.getCurrentBlockType();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return events;
|
|
244
|
+
}
|
|
245
|
+
ensureBlockStarted(events) {
|
|
246
|
+
if (!this.state.currentBlockStarted) {
|
|
247
|
+
events.push(this.makeBlockStart(this.state.currentBlockType));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
makeBlockStart(type) {
|
|
251
|
+
this.state.currentBlockStarted = true;
|
|
252
|
+
this.state.currentBlockContent = '';
|
|
253
|
+
this.state.currentBlockType = type;
|
|
254
|
+
return {
|
|
255
|
+
event: 'block_start',
|
|
256
|
+
index: this.state.blockIndex,
|
|
257
|
+
block: { type }
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
makeBlockComplete() {
|
|
261
|
+
const block = {
|
|
262
|
+
type: this.state.currentBlockType,
|
|
263
|
+
content: this.state.currentBlockContent,
|
|
264
|
+
};
|
|
265
|
+
const event = {
|
|
266
|
+
event: 'block_complete',
|
|
267
|
+
index: this.state.blockIndex,
|
|
268
|
+
block
|
|
269
|
+
};
|
|
270
|
+
this.state.blockIndex++;
|
|
271
|
+
this.state.currentBlockStarted = false;
|
|
272
|
+
this.state.currentBlockContent = '';
|
|
273
|
+
return event;
|
|
274
|
+
}
|
|
275
|
+
getCurrentMeta() {
|
|
276
|
+
const type = this.getCurrentBlockType();
|
|
277
|
+
return {
|
|
278
|
+
type,
|
|
279
|
+
visible: type === 'text',
|
|
280
|
+
blockIndex: this.state.blockIndex,
|
|
281
|
+
depth: Math.max(this.state.functionCallsDepth, this.state.functionResultsDepth),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
scanForDepth() {
|
|
285
|
+
const text = this.state.accumulated;
|
|
286
|
+
const lookbackChars = 30;
|
|
287
|
+
const scanStart = Math.max(0, this.state.lastScanPos - lookbackChars);
|
|
288
|
+
const textToScan = text.slice(scanStart);
|
|
289
|
+
ALL_TAGS.lastIndex = 0;
|
|
290
|
+
let match;
|
|
291
|
+
while ((match = ALL_TAGS.exec(textToScan)) !== null) {
|
|
292
|
+
const absolutePos = scanStart + match.index;
|
|
293
|
+
if (absolutePos < this.state.lastScanPos)
|
|
294
|
+
continue;
|
|
295
|
+
const tag = match[0];
|
|
296
|
+
const isClosing = tag.startsWith('</');
|
|
297
|
+
if (tag.includes('function_calls')) {
|
|
298
|
+
if (isClosing) {
|
|
299
|
+
this.state.functionCallsDepth = Math.max(0, this.state.functionCallsDepth - 1);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
this.state.functionCallsDepth++;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
else if (tag.includes('function_results')) {
|
|
306
|
+
if (isClosing) {
|
|
307
|
+
this.state.functionResultsDepth = Math.max(0, this.state.functionResultsDepth - 1);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
this.state.functionResultsDepth++;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else if (tag.includes('thinking')) {
|
|
314
|
+
if (isClosing) {
|
|
315
|
+
this.state.thinkingDepth = Math.max(0, this.state.thinkingDepth - 1);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
this.state.thinkingDepth++;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const partialTagLen = this.findPartialTagAtEnd(text);
|
|
323
|
+
this.state.lastScanPos = text.length - partialTagLen;
|
|
324
|
+
}
|
|
325
|
+
scan() {
|
|
326
|
+
this.scanForDepth();
|
|
327
|
+
return [];
|
|
328
|
+
}
|
|
329
|
+
findPartialTagAtEnd(text) {
|
|
330
|
+
const tail = text.slice(-30);
|
|
331
|
+
const lastLt = tail.lastIndexOf('<');
|
|
332
|
+
if (lastLt === -1)
|
|
333
|
+
return 0;
|
|
334
|
+
const afterLt = tail.slice(lastLt);
|
|
335
|
+
if (afterLt.includes('>'))
|
|
336
|
+
return 0;
|
|
337
|
+
return afterLt.length;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
// ============================================================================
|
|
341
|
+
// Utility Functions
|
|
342
|
+
// ============================================================================
|
|
343
|
+
export function hasUnclosedXmlBlock(text) {
|
|
344
|
+
const parser = new IncrementalXmlParser();
|
|
345
|
+
parser.push(text);
|
|
346
|
+
return parser.isInsideBlock();
|
|
347
|
+
}
|
|
348
|
+
export function countTags(text, openPattern, closePattern) {
|
|
349
|
+
openPattern.lastIndex = 0;
|
|
350
|
+
closePattern.lastIndex = 0;
|
|
351
|
+
const openMatches = text.match(openPattern) || [];
|
|
352
|
+
const closeMatches = text.match(closePattern) || [];
|
|
353
|
+
return {
|
|
354
|
+
open: openMatches.length,
|
|
355
|
+
close: closeMatches.length,
|
|
356
|
+
depth: openMatches.length - closeMatches.length,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
//# sourceMappingURL=stream-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-parser.js","sourceRoot":"","sources":["../../src/utils/stream-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA4CH,SAAS,kBAAkB;IACzB,OAAO;QACL,kBAAkB,EAAE,CAAC;QACrB,oBAAoB,EAAE,CAAC;QACvB,aAAa,EAAE,CAAC;QAChB,WAAW,EAAE,EAAE;QACf,WAAW,EAAE,CAAC;QACd,UAAU,EAAE,CAAC;QACb,mBAAmB,EAAE,KAAK;QAC1B,mBAAmB,EAAE,EAAE;QACvB,gBAAgB,EAAE,MAAM;QACxB,SAAS,EAAE,EAAE;QACb,aAAa,EAAE;YACb,QAAQ,EAAE,KAAK;YACf,eAAe,EAAE,EAAE;YACnB,aAAa,EAAE,EAAE;YACjB,WAAW,EAAE,KAAK;YAClB,gBAAgB,EAAE,EAAE;YACpB,YAAY,EAAE,EAAE;YAChB,SAAS,EAAE,EAAE;SACd;KACF,CAAC;AACJ,CAAC;AAED,4CAA4C;AAC5C,MAAM,QAAQ,GAAG,+DAA+D,CAAC;AAEjF,wCAAwC;AACxC,MAAM,qBAAqB,GAAG,6FAA6F,CAAC;AAE5H,8BAA8B;AAC9B,MAAM,qBAAqB,GAAG;IAC5B,WAAW,EAAE,cAAc;IAC3B,iBAAiB,EAAE,oBAAoB;IACvC,mBAAmB,EAAE,sBAAsB;IAC3C,SAAS,EAAE,YAAY;IACvB,YAAY,EAAE,eAAe;IAC7B,iBAAiB,EAAE,0BAA0B;IAC7C,SAAS,EAAE,kBAAkB;CAC9B,CAAC;AAEF,+EAA+E;AAC/E,yBAAyB;AACzB,+EAA+E;AAE/E,MAAM,OAAO,oBAAoB;IACvB,KAAK,CAAc;IAE3B;QACE,IAAI,CAAC,KAAK,GAAG,kBAAkB,EAAE,C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sing utilities for XML-based tool calls
|
|
3
|
+
*
|
|
4
|
+
* Supports both plain and antml:-prefixed formats:
|
|
5
|
+
* <function_calls> or <function_calls>
|
|
6
|
+
* <invoke name="..."> or <invoke name="...">
|
|
7
|
+
* <parameter name="..."> or <parameter name="...">
|
|
8
|
+
*
|
|
9
|
+
* Also supports self-closing invoke tags:
|
|
10
|
+
* <invoke name="tool"/> or <invoke name="tool"/>
|
|
11
|
+
*/
|
|
12
|
+
import type { ToolCall, ToolResult, ParsedToolCalls, ContentBlock } from '../types/index.js';
|
|
13
|
+
/**
|
|
14
|
+
* Parse tool calls from text containing XML function_calls blocks
|
|
15
|
+
*
|
|
16
|
+
* Uses "last-unexecuted-block" logic: finds the last function_calls block
|
|
17
|
+
* that doesn't have function_results immediately following it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function parseToolCalls(text: string): ParsedToolCalls | null;
|
|
20
|
+
/**
|
|
21
|
+
* Check if text contains an unclosed function_calls block
|
|
22
|
+
* Used for false-positive stop sequence detection
|
|
23
|
+
* Supports both plain and antml: prefixed tags
|
|
24
|
+
*/
|
|
25
|
+
export declare function hasUnclosedToolBlock(text: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Check if text ends with a partial/unclosed tool block
|
|
28
|
+
* Supports both plain and antml: prefixed tags
|
|
29
|
+
*/
|
|
30
|
+
export declare function endsWithPartialToolBlock(text: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Format tool results as XML for injection.
|
|
33
|
+
* Handles both string content and structured content blocks (with images).
|
|
34
|
+
*/
|
|
35
|
+
export declare function formatToolResults(results: ToolResult[]): string;
|
|
36
|
+
/**
|
|
37
|
+
* Format a single tool result
|
|
38
|
+
*/
|
|
39
|
+
export declare function formatToolResult(result: ToolResult): string;
|
|
40
|
+
export interface ToolDefinitionForPrompt {
|
|
41
|
+
name: string;
|
|
42
|
+
description: string;
|
|
43
|
+
parameters: Record<string, {
|
|
44
|
+
type: string;
|
|
45
|
+
description?: string;
|
|
46
|
+
required?: boolean;
|
|
47
|
+
enum?: string[];
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Format tool definitions as XML for system prompt
|
|
52
|
+
*/
|
|
53
|
+
export declare function formatToolDefinitions(tools: ToolDefinitionForPrompt[]): string;
|
|
54
|
+
/**
|
|
55
|
+
* Parse accumulated assistant text into structured ContentBlock[].
|
|
56
|
+
* Extracts thinking blocks, tool calls, tool results, and plain text.
|
|
57
|
+
*
|
|
58
|
+
* @param text - The accumulated assistant output text
|
|
59
|
+
* @returns Array of ContentBlock in order of appearance
|
|
60
|
+
*/
|
|
61
|
+
export declare function parseAccumulatedIntoBlocks(text: string): {
|
|
62
|
+
blocks: ContentBlock[];
|
|
63
|
+
toolCalls: ToolCall[];
|
|
64
|
+
toolResults: ToolResult[];
|
|
65
|
+
};
|
|
66
|
+
import type { ToolDefinition } from '../types/index.js';
|
|
67
|
+
/**
|
|
68
|
+
* Get tool instructions string for manual placement.
|
|
69
|
+
* Use this when you want to control where tool instructions appear
|
|
70
|
+
* (e.g., injected into conversation rather than system prompt).
|
|
71
|
+
*
|
|
72
|
+
* @param tools - Tool definitions
|
|
73
|
+
* @returns Complete instruction string with definitions and usage example
|
|
74
|
+
*/
|
|
75
|
+
export declare function getToolInstructions(tools: ToolDefinition[]): string;
|
|
76
|
+
/**
|
|
77
|
+
* Provider image block format (Anthropic-style)
|
|
78
|
+
*/
|
|
79
|
+
export interface ProviderImageBlock {
|
|
80
|
+
type: 'image';
|
|
81
|
+
source: {
|
|
82
|
+
type: 'base64';
|
|
83
|
+
media_type: string;
|
|
84
|
+
data: string;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if any tool result contains image content
|
|
89
|
+
*/
|
|
90
|
+
export declare function hasImageInToolResults(results: ToolResult[]): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Result of separating tool result content for split-turn injection.
|
|
93
|
+
*
|
|
94
|
+
* When tool results contain images in prefill mode, we need to:
|
|
95
|
+
* 1. Put text content in the assistant turn (as XML)
|
|
96
|
+
* 2. Extract images into a separate user turn
|
|
97
|
+
* 3. Continue assistant turn with closing XML
|
|
98
|
+
*/
|
|
99
|
+
export interface SplitTurnContent {
|
|
100
|
+
/** XML up to and including text content, ending mid-result if images present */
|
|
101
|
+
beforeImageXml: string;
|
|
102
|
+
/** Images extracted from results (in provider format) */
|
|
103
|
+
images: ProviderImageBlock[];
|
|
104
|
+
/** Closing XML after images (closing result tags, function_results) */
|
|
105
|
+
afterImageXml: string;
|
|
106
|
+
/** Whether any images were found */
|
|
107
|
+
hasImages: boolean;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Format tool results for split-turn injection when images are present.
|
|
111
|
+
*
|
|
112
|
+
* This separates the XML into parts that go in the assistant turn (text)
|
|
113
|
+
* and the user turn (images), with continuation XML for the next assistant turn.
|
|
114
|
+
*
|
|
115
|
+
* Structure when images present:
|
|
116
|
+
* ```
|
|
117
|
+
* Assistant: <function_results>
|
|
118
|
+
* <result tool_use_id="...">
|
|
119
|
+
* text content here
|
|
120
|
+
* [END - mid XML]
|
|
121
|
+
*
|
|
122
|
+
* User: [image blocks]
|
|
123
|
+
*
|
|
124
|
+
* Assistant (prefill): </result>
|
|
125
|
+
* </function_results>
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export declare function formatToolResultsForSplitTurn(results: ToolResult[]): SplitTurnContent;
|
|
129
|
+
export declare function unescapeXml(text: string): string;
|
|
130
|
+
//# sourceMappingURL=tool-parser.d.ts.map
|