dsh-observe 0.2.14 → 0.2.16
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/CHANGELOG.md +23 -0
- package/README-es.md +45 -43
- package/README-hi.md +45 -43
- package/README-pt.md +45 -43
- package/README-zh.md +45 -43
- package/README.md +21 -18
- package/lib/index.js +65 -20
- package/lib/types/collector.d.ts.map +1 -1
- package/lib/types/collector.js +5 -2
- package/lib/types/collector.js.map +1 -1
- package/lib/types/project.d.ts +65 -3
- package/lib/types/project.d.ts.map +1 -1
- package/lib/types/project.js +69 -12
- package/lib/types/project.js.map +1 -1
- package/lib/types/version.d.ts +1 -1
- package/lib/types/version.js +1 -1
- package/package.json +20 -21
- package/src/collector.ts +5 -2
- package/src/project.ts +100 -13
- package/src/version.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../../src/project.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAEjE;;;;;;;;GAQG;AACH,wBAAgB,
|
|
1
|
+
{"version":3,"file":"project.d.ts","sourceRoot":"","sources":["../../src/project.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAEjE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,UAAU,sBAAsB;IAC9B,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;IAC5B,QAAQ,CAAC,OAAO,EAAE,SAAS,YAAY,EAAE,CAAA;CAC1C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,sBAAsB,CAK1F;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,MAAM,CAoCtE;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAI9D;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE;IAC1C,QAAQ,CAAC,OAAO,EAAE,SAAS,YAAY,EAAE,CAAA;IACzC,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAC3B,GAAG,MAAM,CAET;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAEvD"}
|
package/lib/types/project.js
CHANGED
|
@@ -6,47 +6,104 @@
|
|
|
6
6
|
* information enters the export).
|
|
7
7
|
* @module dsh-observe/project
|
|
8
8
|
*/
|
|
9
|
+
/**
|
|
10
|
+
* Read-only compatibility guard for the retired V3 tool-result wrapper.
|
|
11
|
+
*
|
|
12
|
+
* Structural, not a cast: a block that does not carry an array `content` is not
|
|
13
|
+
* a wrapper, so a future or foreign block kind that happens to reuse the
|
|
14
|
+
* `'tool-result'` tag cannot make the projection below recurse into nothing.
|
|
15
|
+
*
|
|
16
|
+
* This plugin is a pure consumer of tool results — it holds no path that
|
|
17
|
+
* constructs a message or a content block — so there is no write-side
|
|
18
|
+
* migration to perform here; keeping the read reachable is what lets a
|
|
19
|
+
* pre-upgrade session log still project the output it already showed the model.
|
|
20
|
+
* @param block - any block read off a logged message.
|
|
21
|
+
* @returns whether the block is the retired V3 tool-result wrapper.
|
|
22
|
+
*/
|
|
23
|
+
export function isRetiredToolResultWrapper(block) {
|
|
24
|
+
return block !== null
|
|
25
|
+
&& typeof block === 'object'
|
|
26
|
+
&& block.type === 'tool-result'
|
|
27
|
+
&& Array.isArray(block.content);
|
|
28
|
+
}
|
|
9
29
|
/**
|
|
10
30
|
* Project content blocks to one compact plain-text string. Text and
|
|
11
31
|
* reasoning deltas pass through verbatim; tool calls render as
|
|
12
|
-
* `<tool-call name(arguments)>`; images render as `[image]
|
|
13
|
-
*
|
|
14
|
-
* no projectable text.
|
|
32
|
+
* `<tool-call name(arguments)>`; images render as `[image]`. Unknown block
|
|
33
|
+
* types (merge-extensible) are skipped — they carry no projectable text.
|
|
15
34
|
* @param blocks - the model-facing content blocks.
|
|
16
35
|
* @returns the joined projection.
|
|
17
36
|
*/
|
|
18
37
|
export function projectContent(blocks) {
|
|
19
38
|
const parts = [];
|
|
20
39
|
for (const block of blocks) {
|
|
21
|
-
|
|
40
|
+
if (isRetiredToolResultWrapper(block)) {
|
|
41
|
+
// Read-only compatibility for a log written before the V4 upgrade: the
|
|
42
|
+
// retired wrapper's own `isError` flag is never persisted (the V3
|
|
43
|
+
// migration rejects it, keeping only `content`), so the failure marker is
|
|
44
|
+
// applied at MESSAGE level by `projectToolMessage` instead. A V3-era log
|
|
45
|
+
// therefore loses that marker here — the honest reading of what the V3
|
|
46
|
+
// row actually stored, not a silently invented `[error]`.
|
|
47
|
+
parts.push(projectContent(block.content));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (block === null || typeof block !== 'object')
|
|
51
|
+
continue;
|
|
52
|
+
const typed = block;
|
|
53
|
+
switch (typed.type) {
|
|
22
54
|
case 'text':
|
|
23
|
-
parts.push(
|
|
55
|
+
parts.push(typed.text);
|
|
24
56
|
break;
|
|
25
57
|
case 'reasoning':
|
|
26
|
-
parts.push(
|
|
58
|
+
parts.push(typed.text);
|
|
27
59
|
break;
|
|
28
60
|
case 'tool-call':
|
|
29
|
-
parts.push(`<tool-call ${
|
|
61
|
+
parts.push(`<tool-call ${typed.name}(${typed.arguments})>`);
|
|
30
62
|
break;
|
|
31
63
|
case 'image':
|
|
32
64
|
parts.push('[image]');
|
|
33
65
|
break;
|
|
34
|
-
case 'tool-result':
|
|
35
|
-
parts.push(projectContent(block.content) + (block.isError === true ? ' [error]' : ''));
|
|
36
|
-
break;
|
|
37
66
|
default:
|
|
38
|
-
// Unknown content block types carry no projectable text.
|
|
67
|
+
// Unknown content block types carry no projectable text. `file`,
|
|
68
|
+
// `tool-addition`, and `tool-removal` land here: none of them holds
|
|
69
|
+
// text the model ever saw.
|
|
39
70
|
break;
|
|
40
71
|
}
|
|
41
72
|
}
|
|
42
73
|
return parts.join('\n');
|
|
43
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Project one message's content.
|
|
77
|
+
*
|
|
78
|
+
* A tool-role message carries its failure flag on the MESSAGE (`isError`), not
|
|
79
|
+
* on any block, so it needs `projectToolMessage` to reproduce the `[error]`
|
|
80
|
+
* marker the model saw. Every other role keeps the plain block projection.
|
|
81
|
+
* @param message - the logged message.
|
|
82
|
+
* @returns the projection of its content.
|
|
83
|
+
*/
|
|
84
|
+
export function projectMessageContent(message) {
|
|
85
|
+
return message.role === 'tool'
|
|
86
|
+
? projectToolMessage(message)
|
|
87
|
+
: projectContent(message.content);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Project one first-class V4 tool-role message, surfacing its failure flag.
|
|
91
|
+
*
|
|
92
|
+
* V4 moved the tool result out of the content blocks and onto the message, so
|
|
93
|
+
* the `[error]` marker has to be appended here — projecting the blocks alone
|
|
94
|
+
* would drop it and report a failed tool as a successful one.
|
|
95
|
+
* @param message - the logged tool-role message.
|
|
96
|
+
* @returns the projected result text, marked when the invocation failed.
|
|
97
|
+
*/
|
|
98
|
+
export function projectToolMessage(message) {
|
|
99
|
+
return projectContent(message.content) + (message.isError === true ? ' [error]' : '');
|
|
100
|
+
}
|
|
44
101
|
/**
|
|
45
102
|
* Project one message with its role prefix.
|
|
46
103
|
* @param message - the logged message.
|
|
47
104
|
* @returns `[role] content` on one logical line.
|
|
48
105
|
*/
|
|
49
106
|
export function projectMessage(message) {
|
|
50
|
-
return `[${message.role}] ${
|
|
107
|
+
return `[${message.role}] ${projectMessageContent(message)}`;
|
|
51
108
|
}
|
|
52
109
|
//# sourceMappingURL=project.js.map
|
package/lib/types/project.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/project.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/project.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA6BH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,0BAA0B,CAAC,KAAc;IACvD,OAAO,KAAK,KAAK,IAAI;WAChB,OAAO,KAAK,KAAK,QAAQ;WACxB,KAA4B,CAAC,IAAI,KAAK,aAAa;WACpD,KAAK,CAAC,OAAO,CAAE,KAA+B,CAAC,OAAO,CAAC,CAAA;AAC9D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,MAA+B;IAC5D,MAAM,KAAK,GAAa,EAAE,CAAA;IAC1B,KAAK,MAAM,KAAK,IAAI,MAA4B,EAAE,CAAC;QACjD,IAAI,0BAA0B,CAAC,KAAK,CAAC,EAAE,CAAC;YACtC,uEAAuE;YACvE,kEAAkE;YAClE,0EAA0E;YAC1E,yEAAyE;YACzE,uEAAuE;YACvE,0DAA0D;YAC1D,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAA;YACzC,SAAQ;QACV,CAAC;QACD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,SAAQ;QACzD,MAAM,KAAK,GAAG,KAAqB,CAAA;QACnC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,MAAM;gBACT,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBACtB,MAAK;YACP,KAAK,WAAW;gBACd,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBACtB,MAAK;YACP,KAAK,WAAW;gBACd,KAAK,CAAC,IAAI,CAAC,cAAc,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,CAAA;gBAC3D,MAAK;YACP,KAAK,OAAO;gBACV,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;gBACrB,MAAK;YACP;gBACE,iEAAiE;gBACjE,oEAAoE;gBACpE,2BAA2B;gBAC3B,MAAK;QACT,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACzB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAAgB;IACpD,OAAO,OAAO,CAAC,IAAI,KAAK,MAAM;QAC5B,CAAC,CAAC,kBAAkB,CAAC,OAAO,CAAC;QAC7B,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;AACrC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAGlC;IACC,OAAO,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;AACvF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,qBAAqB,CAAC,OAAO,CAAC,EAAE,CAAA;AAC9D,CAAC"}
|
package/lib/types/version.d.ts
CHANGED
package/lib/types/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-observe",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.16",
|
|
4
4
|
"description": "OpenTelemetry and Langfuse observability exporter for DeepSeek Harness: turn/step/tool/LLM spans, token and cost metrics, and sanitized LLM prompt/completion capture from the session/event stream, with async batching, bounded offline buffering, and retry with backoff.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -71,8 +71,7 @@
|
|
|
71
71
|
],
|
|
72
72
|
"compatibility": {
|
|
73
73
|
"dshVersions": [
|
|
74
|
-
"0.1.
|
|
75
|
-
"0.1.5-rc.2"
|
|
74
|
+
"0.1.7-alpha.2"
|
|
76
75
|
]
|
|
77
76
|
},
|
|
78
77
|
"capability": {
|
|
@@ -105,18 +104,18 @@
|
|
|
105
104
|
],
|
|
106
105
|
"engines": {
|
|
107
106
|
"node": "^22.19.0 || >=24.0.0",
|
|
108
|
-
"dsh": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0"
|
|
107
|
+
"dsh": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0"
|
|
109
108
|
},
|
|
110
109
|
"packageManager": "pnpm@11.7.0",
|
|
111
110
|
"peerDependencies": {
|
|
112
|
-
"@deepseek-ai/cordis": "^4.0.
|
|
113
|
-
"@deepseek-ai/dsh-llm": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0",
|
|
114
|
-
"@deepseek-ai/dsh-session": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0",
|
|
115
|
-
"@deepseek-ai/dsh-storage": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0",
|
|
116
|
-
"@deepseek-ai/dsh-storage-domain": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0",
|
|
117
|
-
"@deepseek-ai/dsh-storage-json": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0",
|
|
118
|
-
"@deepseek-ai/dsh-typert-protocol": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0",
|
|
119
|
-
"@deepseek-ai/schemastery": "^3.18.
|
|
111
|
+
"@deepseek-ai/cordis": "^4.0.3",
|
|
112
|
+
"@deepseek-ai/dsh-llm": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0",
|
|
113
|
+
"@deepseek-ai/dsh-session": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0",
|
|
114
|
+
"@deepseek-ai/dsh-storage": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0",
|
|
115
|
+
"@deepseek-ai/dsh-storage-domain": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0",
|
|
116
|
+
"@deepseek-ai/dsh-storage-json": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0",
|
|
117
|
+
"@deepseek-ai/dsh-typert-protocol": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0",
|
|
118
|
+
"@deepseek-ai/schemastery": "^3.18.3"
|
|
120
119
|
},
|
|
121
120
|
"dependencies": {
|
|
122
121
|
"tsdown": "^0.22.14",
|
|
@@ -124,17 +123,17 @@
|
|
|
124
123
|
"zod": "^4.4.3"
|
|
125
124
|
},
|
|
126
125
|
"devDependencies": {
|
|
127
|
-
"@deepseek-ai/dsh-attachment": "0.1.
|
|
128
|
-
"@deepseek-ai/cordis": "^4.0.
|
|
126
|
+
"@deepseek-ai/dsh-attachment": "0.1.7-alpha.2",
|
|
127
|
+
"@deepseek-ai/cordis": "^4.0.3",
|
|
129
128
|
"@deepseek-ai/cordis-plugin-include": "^1.0.7",
|
|
130
129
|
"@deepseek-ai/cordis-plugin-loader": "^1.0.3",
|
|
131
|
-
"@deepseek-ai/dsh-llm": "0.1.
|
|
132
|
-
"@deepseek-ai/dsh-session": "0.1.
|
|
133
|
-
"@deepseek-ai/dsh-storage": "0.1.
|
|
134
|
-
"@deepseek-ai/dsh-storage-domain": "0.1.
|
|
135
|
-
"@deepseek-ai/dsh-storage-json": "0.1.
|
|
136
|
-
"@deepseek-ai/dsh-typert-protocol": "0.1.
|
|
137
|
-
"@deepseek-ai/schemastery": "^3.18.
|
|
130
|
+
"@deepseek-ai/dsh-llm": "0.1.7-alpha.2",
|
|
131
|
+
"@deepseek-ai/dsh-session": "0.1.7-alpha.2",
|
|
132
|
+
"@deepseek-ai/dsh-storage": "0.1.7-alpha.2",
|
|
133
|
+
"@deepseek-ai/dsh-storage-domain": "0.1.7-alpha.2",
|
|
134
|
+
"@deepseek-ai/dsh-storage-json": "0.1.7-alpha.2",
|
|
135
|
+
"@deepseek-ai/dsh-typert-protocol": "0.1.7-alpha.2",
|
|
136
|
+
"@deepseek-ai/schemastery": "^3.18.3",
|
|
138
137
|
"@types/node": "^22.19.0",
|
|
139
138
|
"@vitest/coverage-v8": "^3.2.0",
|
|
140
139
|
"oxlint": "0.18.1",
|
package/src/collector.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type { EpochHeader, RequestContext, Session, SessionEvent } from '@deepse
|
|
|
18
18
|
import type { ResolvedConfig } from './config.ts'
|
|
19
19
|
import type { JsonScalar, MetricRecord, SpanRecord, TokenCounts } from './model.ts'
|
|
20
20
|
import { findPrice, costUsd } from './pricing.ts'
|
|
21
|
-
import { projectContent, projectMessage } from './project.ts'
|
|
21
|
+
import { projectContent, projectMessage, projectToolMessage } from './project.ts'
|
|
22
22
|
import { sanitizeJsonText, sanitizeText } from './sanitize.ts'
|
|
23
23
|
import type { ObserveLogger } from './sinks.ts'
|
|
24
24
|
|
|
@@ -421,8 +421,11 @@ export class Collector {
|
|
|
421
421
|
attributes['tool.error.code'] = this.attr(event.data.error.code)
|
|
422
422
|
}
|
|
423
423
|
const input = sanitizeJsonText(open.arguments, this.config.sanitize.truncateToolInputChars, this.config.sanitize)
|
|
424
|
+
// V4 carries the tool outcome on the MESSAGE (`isError`), so the projected
|
|
425
|
+
// output must go through `projectToolMessage`; projecting `content` alone
|
|
426
|
+
// would drop the `[error]` marker and report a failed tool as a success.
|
|
424
427
|
const output = sanitizeText(
|
|
425
|
-
|
|
428
|
+
projectToolMessage(event.data.message),
|
|
426
429
|
this.config.sanitize.truncateToolOutputChars,
|
|
427
430
|
this.config.sanitize,
|
|
428
431
|
)
|
package/src/project.ts
CHANGED
|
@@ -9,47 +9,134 @@
|
|
|
9
9
|
|
|
10
10
|
import type { ContentBlock, Message } from '@deepseek-ai/dsh-llm'
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* The **retired** session-format-V3 tool-result wrapper.
|
|
14
|
+
*
|
|
15
|
+
* Through the V3 format a tool result was a content BLOCK nested inside a
|
|
16
|
+
* message's content:
|
|
17
|
+
* `{ type: 'tool-result', source: { callId }, content: [...] }`. Since the V4
|
|
18
|
+
* format a tool result is a first-class `role: 'tool'` message instead
|
|
19
|
+
* (`ToolResultMessage`: top-level `toolCallId` + `content` + optional
|
|
20
|
+
* `isError`), `'tool-result'` is no longer a member of the host's
|
|
21
|
+
* `ContentBlockMap`, and the host refuses the retired wrapper at **physical
|
|
22
|
+
* row admission** — `assertV4ToolResultMessage` rejects any tool-role content
|
|
23
|
+
* that still nests one (`packages/session/session-format-v3-to-v4/src/tool-role.ts`,
|
|
24
|
+
* "content must not contain a released tool-result wrapper").
|
|
25
|
+
*
|
|
26
|
+
* This type exists ONLY so the read path below can still describe, and
|
|
27
|
+
* therefore still project, a log written before the upgrade. It is deliberately
|
|
28
|
+
* absent from `ContentBlock`, so no V4-shaped code can produce one by accident:
|
|
29
|
+
* the sole way to obtain this shape is the {@link isRetiredToolResultWrapper}
|
|
30
|
+
* guard. This module never constructs one.
|
|
31
|
+
*/
|
|
32
|
+
interface RetiredToolResultBlock {
|
|
33
|
+
readonly type: 'tool-result'
|
|
34
|
+
readonly content: readonly ContentBlock[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Read-only compatibility guard for the retired V3 tool-result wrapper.
|
|
39
|
+
*
|
|
40
|
+
* Structural, not a cast: a block that does not carry an array `content` is not
|
|
41
|
+
* a wrapper, so a future or foreign block kind that happens to reuse the
|
|
42
|
+
* `'tool-result'` tag cannot make the projection below recurse into nothing.
|
|
43
|
+
*
|
|
44
|
+
* This plugin is a pure consumer of tool results — it holds no path that
|
|
45
|
+
* constructs a message or a content block — so there is no write-side
|
|
46
|
+
* migration to perform here; keeping the read reachable is what lets a
|
|
47
|
+
* pre-upgrade session log still project the output it already showed the model.
|
|
48
|
+
* @param block - any block read off a logged message.
|
|
49
|
+
* @returns whether the block is the retired V3 tool-result wrapper.
|
|
50
|
+
*/
|
|
51
|
+
export function isRetiredToolResultWrapper(block: unknown): block is RetiredToolResultBlock {
|
|
52
|
+
return block !== null
|
|
53
|
+
&& typeof block === 'object'
|
|
54
|
+
&& (block as { type?: unknown }).type === 'tool-result'
|
|
55
|
+
&& Array.isArray((block as { content?: unknown }).content)
|
|
56
|
+
}
|
|
57
|
+
|
|
12
58
|
/**
|
|
13
59
|
* Project content blocks to one compact plain-text string. Text and
|
|
14
60
|
* reasoning deltas pass through verbatim; tool calls render as
|
|
15
|
-
* `<tool-call name(arguments)>`; images render as `[image]
|
|
16
|
-
*
|
|
17
|
-
* no projectable text.
|
|
61
|
+
* `<tool-call name(arguments)>`; images render as `[image]`. Unknown block
|
|
62
|
+
* types (merge-extensible) are skipped — they carry no projectable text.
|
|
18
63
|
* @param blocks - the model-facing content blocks.
|
|
19
64
|
* @returns the joined projection.
|
|
20
65
|
*/
|
|
21
66
|
export function projectContent(blocks: readonly ContentBlock[]): string {
|
|
22
67
|
const parts: string[] = []
|
|
23
|
-
for (const block of blocks) {
|
|
24
|
-
|
|
68
|
+
for (const block of blocks as readonly unknown[]) {
|
|
69
|
+
if (isRetiredToolResultWrapper(block)) {
|
|
70
|
+
// Read-only compatibility for a log written before the V4 upgrade: the
|
|
71
|
+
// retired wrapper's own `isError` flag is never persisted (the V3
|
|
72
|
+
// migration rejects it, keeping only `content`), so the failure marker is
|
|
73
|
+
// applied at MESSAGE level by `projectToolMessage` instead. A V3-era log
|
|
74
|
+
// therefore loses that marker here — the honest reading of what the V3
|
|
75
|
+
// row actually stored, not a silently invented `[error]`.
|
|
76
|
+
parts.push(projectContent(block.content))
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
if (block === null || typeof block !== 'object') continue
|
|
80
|
+
const typed = block as ContentBlock
|
|
81
|
+
switch (typed.type) {
|
|
25
82
|
case 'text':
|
|
26
|
-
parts.push(
|
|
83
|
+
parts.push(typed.text)
|
|
27
84
|
break
|
|
28
85
|
case 'reasoning':
|
|
29
|
-
parts.push(
|
|
86
|
+
parts.push(typed.text)
|
|
30
87
|
break
|
|
31
88
|
case 'tool-call':
|
|
32
|
-
parts.push(`<tool-call ${
|
|
89
|
+
parts.push(`<tool-call ${typed.name}(${typed.arguments})>`)
|
|
33
90
|
break
|
|
34
91
|
case 'image':
|
|
35
92
|
parts.push('[image]')
|
|
36
93
|
break
|
|
37
|
-
case 'tool-result':
|
|
38
|
-
parts.push(projectContent(block.content) + (block.isError === true ? ' [error]' : ''))
|
|
39
|
-
break
|
|
40
94
|
default:
|
|
41
|
-
// Unknown content block types carry no projectable text.
|
|
95
|
+
// Unknown content block types carry no projectable text. `file`,
|
|
96
|
+
// `tool-addition`, and `tool-removal` land here: none of them holds
|
|
97
|
+
// text the model ever saw.
|
|
42
98
|
break
|
|
43
99
|
}
|
|
44
100
|
}
|
|
45
101
|
return parts.join('\n')
|
|
46
102
|
}
|
|
47
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Project one message's content.
|
|
106
|
+
*
|
|
107
|
+
* A tool-role message carries its failure flag on the MESSAGE (`isError`), not
|
|
108
|
+
* on any block, so it needs `projectToolMessage` to reproduce the `[error]`
|
|
109
|
+
* marker the model saw. Every other role keeps the plain block projection.
|
|
110
|
+
* @param message - the logged message.
|
|
111
|
+
* @returns the projection of its content.
|
|
112
|
+
*/
|
|
113
|
+
export function projectMessageContent(message: Message): string {
|
|
114
|
+
return message.role === 'tool'
|
|
115
|
+
? projectToolMessage(message)
|
|
116
|
+
: projectContent(message.content)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Project one first-class V4 tool-role message, surfacing its failure flag.
|
|
121
|
+
*
|
|
122
|
+
* V4 moved the tool result out of the content blocks and onto the message, so
|
|
123
|
+
* the `[error]` marker has to be appended here — projecting the blocks alone
|
|
124
|
+
* would drop it and report a failed tool as a successful one.
|
|
125
|
+
* @param message - the logged tool-role message.
|
|
126
|
+
* @returns the projected result text, marked when the invocation failed.
|
|
127
|
+
*/
|
|
128
|
+
export function projectToolMessage(message: {
|
|
129
|
+
readonly content: readonly ContentBlock[]
|
|
130
|
+
readonly isError?: boolean
|
|
131
|
+
}): string {
|
|
132
|
+
return projectContent(message.content) + (message.isError === true ? ' [error]' : '')
|
|
133
|
+
}
|
|
134
|
+
|
|
48
135
|
/**
|
|
49
136
|
* Project one message with its role prefix.
|
|
50
137
|
* @param message - the logged message.
|
|
51
138
|
* @returns `[role] content` on one logical line.
|
|
52
139
|
*/
|
|
53
140
|
export function projectMessage(message: Message): string {
|
|
54
|
-
return `[${message.role}] ${
|
|
141
|
+
return `[${message.role}] ${projectMessageContent(message)}`
|
|
55
142
|
}
|
package/src/version.ts
CHANGED