@qvac/harness 1.0.0-rc.20 → 1.0.0-rc.21

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.
Files changed (191) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist-lib/index.d.ts +7 -5
  3. package/dist-lib/index.js +5 -4
  4. package/dist-lib/lib/agent/resolve.js +2 -1
  5. package/dist-lib/lib/capabilities/completion-engine/actors/answering.js +2 -2
  6. package/dist-lib/lib/capabilities/completion-engine/actors/direct-search.js +1 -1
  7. package/dist-lib/lib/capabilities/completion-engine/actors/execute.d.ts +1 -1
  8. package/dist-lib/lib/capabilities/completion-engine/actors/executing.js +8 -6
  9. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +1 -1
  10. package/dist-lib/lib/capabilities/completion-engine/actors/inspect-prepare.js +1 -1
  11. package/dist-lib/lib/capabilities/completion-engine/actors/loading-skill.js +2 -1
  12. package/dist-lib/lib/capabilities/completion-engine/actors/preparing.js +8 -3
  13. package/dist-lib/lib/capabilities/completion-engine/actors/resolving.js +10 -4
  14. package/dist-lib/lib/capabilities/completion-engine/actors/run-prepare.js +2 -1
  15. package/dist-lib/lib/capabilities/completion-engine/actors/streaming.js +8 -8
  16. package/dist-lib/lib/capabilities/completion-engine/actors/wrap-up.js +2 -2
  17. package/dist-lib/lib/capabilities/completion-engine/loop.d.ts +3 -2
  18. package/dist-lib/lib/capabilities/completion-engine/loop.js +1 -0
  19. package/dist-lib/lib/capabilities/completion-engine/round.js +9 -9
  20. package/dist-lib/lib/capabilities/completion-engine/tool-caller.js +3 -1
  21. package/dist-lib/lib/capabilities/completion-engine/tool-execution.d.ts +5 -41
  22. package/dist-lib/lib/capabilities/completion-engine/tool-execution.js +4 -133
  23. package/dist-lib/lib/capabilities/completion-engine/tool-outcome.d.ts +4 -0
  24. package/dist-lib/lib/capabilities/completion-engine/tool-outcome.js +42 -0
  25. package/dist-lib/lib/capabilities/completion-engine/tool-preflight.d.ts +43 -0
  26. package/dist-lib/lib/capabilities/completion-engine/tool-preflight.js +91 -0
  27. package/dist-lib/lib/capabilities/completion-engine/turns.js +7 -4
  28. package/dist-lib/lib/capabilities/tool-turns/attachments.d.ts +7 -0
  29. package/dist-lib/lib/capabilities/tool-turns/attachments.js +39 -0
  30. package/dist-lib/lib/capabilities/tool-turns/copy.d.ts +13 -0
  31. package/dist-lib/lib/capabilities/tool-turns/copy.js +29 -0
  32. package/dist-lib/lib/capabilities/tool-turns/result-turn.d.ts +22 -0
  33. package/dist-lib/lib/capabilities/tool-turns/result-turn.js +120 -0
  34. package/dist-lib/lib/capabilities/tool-turns/run-limits.d.ts +6 -0
  35. package/dist-lib/lib/capabilities/tool-turns/run-limits.js +32 -0
  36. package/dist-lib/lib/capabilities/tool-turns/skill-loads.d.ts +9 -0
  37. package/dist-lib/lib/capabilities/tool-turns/skill-loads.js +71 -0
  38. package/dist-lib/lib/capabilities/tool-turns.d.ts +8 -54
  39. package/dist-lib/lib/capabilities/tool-turns.js +6 -281
  40. package/dist-lib/lib/client/index.js +3 -3
  41. package/dist-lib/lib/connect/abort.d.ts +6 -0
  42. package/dist-lib/lib/connect/abort.js +52 -0
  43. package/dist-lib/lib/connect/bridges.d.ts +21 -0
  44. package/dist-lib/lib/connect/bridges.js +185 -0
  45. package/dist-lib/lib/connect/decode.d.ts +31 -0
  46. package/dist-lib/lib/connect/decode.js +114 -0
  47. package/dist-lib/lib/connect/host-run-log.d.ts +16 -0
  48. package/dist-lib/lib/connect/host-run-log.js +74 -0
  49. package/dist-lib/lib/connect/served.d.ts +7 -0
  50. package/dist-lib/lib/connect/served.js +45 -0
  51. package/dist-lib/lib/connect.d.ts +3 -3
  52. package/dist-lib/lib/connect.js +24 -468
  53. package/dist-lib/lib/context-window.d.ts +13 -7
  54. package/dist-lib/lib/context-window.js +13 -6
  55. package/dist-lib/lib/contract/agent.d.ts +12 -1
  56. package/dist-lib/lib/contract/completion.d.ts +5 -1
  57. package/dist-lib/lib/contract/drive.d.ts +130 -4
  58. package/dist-lib/lib/contract/ocr.d.ts +5 -1
  59. package/dist-lib/lib/contract-types.d.ts +5 -3
  60. package/dist-lib/lib/errors.js +3 -3
  61. package/dist-lib/lib/helpers/goal-check/asks.d.ts +25 -0
  62. package/dist-lib/lib/helpers/goal-check/asks.js +52 -0
  63. package/dist-lib/lib/helpers/goal-check/checks.d.ts +8 -0
  64. package/dist-lib/lib/helpers/goal-check/checks.js +92 -0
  65. package/dist-lib/lib/helpers/goal-check/commands.d.ts +7 -0
  66. package/dist-lib/lib/helpers/goal-check/commands.js +130 -0
  67. package/dist-lib/lib/helpers/goal-check/paths.d.ts +5 -0
  68. package/dist-lib/lib/helpers/goal-check/paths.js +44 -0
  69. package/dist-lib/lib/helpers/goal-check/shell.d.ts +9 -0
  70. package/dist-lib/lib/helpers/goal-check/shell.js +96 -0
  71. package/dist-lib/lib/helpers/goal-check.d.ts +35 -43
  72. package/dist-lib/lib/helpers/goal-check.js +34 -406
  73. package/dist-lib/lib/helpers/recent-map.d.ts +7 -0
  74. package/dist-lib/lib/helpers/recent-map.js +23 -0
  75. package/dist-lib/lib/helpers/tool-call-markup/emit.d.ts +6 -0
  76. package/dist-lib/lib/helpers/tool-call-markup/emit.js +15 -0
  77. package/dist-lib/lib/helpers/tool-call-markup/recover.d.ts +22 -0
  78. package/dist-lib/lib/helpers/tool-call-markup/recover.js +130 -0
  79. package/dist-lib/lib/helpers/tool-call-markup/skill-recovery.d.ts +3 -0
  80. package/dist-lib/lib/helpers/tool-call-markup/skill-recovery.js +56 -0
  81. package/dist-lib/lib/helpers/tool-call-markup/strip.d.ts +4 -0
  82. package/dist-lib/lib/helpers/tool-call-markup/strip.js +149 -0
  83. package/dist-lib/lib/helpers/tool-call-markup.d.ts +19 -24
  84. package/dist-lib/lib/helpers/tool-call-markup.js +17 -343
  85. package/dist-lib/lib/machine/completion/actions/completion.d.ts +12 -18
  86. package/dist-lib/lib/machine/completion/actions/completion.js +19 -178
  87. package/dist-lib/lib/machine/completion/actions/goal.d.ts +10 -0
  88. package/dist-lib/lib/machine/completion/actions/goal.js +93 -0
  89. package/dist-lib/lib/machine/completion/actions/prepare.js +2 -1
  90. package/dist-lib/lib/machine/completion/actions/reject-tool.d.ts +16 -0
  91. package/dist-lib/lib/machine/completion/actions/reject-tool.js +147 -0
  92. package/dist-lib/lib/machine/completion/actions/retry-round.d.ts +6 -0
  93. package/dist-lib/lib/machine/completion/actions/retry-round.js +50 -0
  94. package/dist-lib/lib/machine/completion/actions/rounds.d.ts +5 -0
  95. package/dist-lib/lib/machine/completion/actions/rounds.js +31 -0
  96. package/dist-lib/lib/machine/completion/actions/tools.d.ts +1 -16
  97. package/dist-lib/lib/machine/completion/actions/tools.js +16 -161
  98. package/dist-lib/lib/machine/completion/context.js +3 -3
  99. package/dist-lib/lib/machine/completion/implementations.d.ts +2 -2
  100. package/dist-lib/lib/machine/completion/implementations.js +6 -2
  101. package/dist-lib/lib/machine/completion/machine.d.ts +2 -2
  102. package/dist-lib/lib/machine/completion/metrics.d.ts +25 -0
  103. package/dist-lib/lib/machine/completion/metrics.js +68 -0
  104. package/dist-lib/lib/machine/completion/nudges.d.ts +5 -0
  105. package/dist-lib/lib/machine/completion/nudges.js +33 -0
  106. package/dist-lib/lib/machine/completion/rules.d.ts +5 -34
  107. package/dist-lib/lib/machine/completion/rules.js +4 -120
  108. package/dist-lib/lib/machine/completion/splice.d.ts +9 -0
  109. package/dist-lib/lib/machine/completion/splice.js +19 -0
  110. package/dist-lib/lib/machine/completion/types.d.ts +4 -13
  111. package/dist-lib/lib/machine/index.d.ts +3 -3
  112. package/dist-lib/lib/machine/index.js +2 -2
  113. package/dist-lib/lib/media-host.js +2 -2
  114. package/dist-lib/lib/media-wire.js +2 -2
  115. package/dist-lib/lib/residency/completion-runtime.d.ts +2 -1
  116. package/dist-lib/lib/residency/completion-runtime.js +44 -1
  117. package/dist-lib/lib/residency/index.d.ts +3 -2
  118. package/dist-lib/lib/residency/index.js +2 -2
  119. package/dist-lib/lib/round-host.js +3 -3
  120. package/dist-lib/lib/round-port.js +3 -3
  121. package/dist-lib/lib/run-log/redact.js +7 -7
  122. package/dist-lib/lib/serve/completion.d.ts +7 -0
  123. package/dist-lib/lib/serve/completion.js +553 -0
  124. package/dist-lib/lib/serve/downloads.d.ts +4 -0
  125. package/dist-lib/lib/serve/downloads.js +79 -0
  126. package/dist-lib/lib/serve/models.d.ts +5 -0
  127. package/dist-lib/lib/serve/models.js +120 -0
  128. package/dist-lib/lib/serve/noop.d.ts +1 -0
  129. package/dist-lib/lib/serve/noop.js +1 -0
  130. package/dist-lib/lib/serve/requests.d.ts +147 -0
  131. package/dist-lib/lib/serve/requests.js +1 -0
  132. package/dist-lib/lib/serve/rounds.d.ts +11 -0
  133. package/dist-lib/lib/serve/rounds.js +67 -0
  134. package/dist-lib/lib/serve/run-log-start.d.ts +38 -0
  135. package/dist-lib/lib/serve/run-log-start.js +254 -0
  136. package/dist-lib/lib/serve/skills.d.ts +32 -0
  137. package/dist-lib/lib/serve/skills.js +46 -0
  138. package/dist-lib/lib/serve.d.ts +5 -1
  139. package/dist-lib/lib/serve.js +14 -1071
  140. package/dist-lib/lib/skills-host/index.d.ts +4 -2
  141. package/dist-lib/lib/skills-host/index.js +20 -8
  142. package/dist-lib/lib/tools/fs/drive.d.ts +1 -17
  143. package/dist-lib/lib/tools/fs/remote-protocol.d.ts +1 -81
  144. package/dist-lib/lib/tools/fs/scope.d.ts +2 -17
  145. package/dist-lib/lib/tools/fs/search.d.ts +2 -18
  146. package/dist-lib/lib/tools/fs/walk.d.ts +1 -5
  147. package/dist-lib/lib/tools/pdf/build.d.ts +2 -2
  148. package/dist-lib/lib/tools/pdf/recognize.js +4 -8
  149. package/dist-lib/lib/tools/pdf/request.d.ts +3 -8
  150. package/dist-lib/lib/tools/pdf/request.js +4 -4
  151. package/dist-lib/lib/tools/pdf.d.ts +0 -1
  152. package/dist-lib/lib/tools/pdf.js +0 -1
  153. package/dist-lib/lib/tools/skill.js +13 -4
  154. package/dist-lib/lib/tools/skills/bundled-dir.d.ts +3 -0
  155. package/dist-lib/lib/tools/skills/bundled-dir.js +27 -24
  156. package/dist-lib/lib/tools/skills/bundled-hash.d.ts +1 -0
  157. package/dist-lib/lib/tools/skills/bundled-hash.js +4 -1
  158. package/dist-lib/lib/tools/skills/catalog-loader.d.ts +12 -0
  159. package/dist-lib/lib/tools/skills/catalog-loader.js +173 -0
  160. package/dist-lib/lib/tools/skills/catalog.d.ts +5 -8
  161. package/dist-lib/lib/tools/skills/catalog.js +1 -162
  162. package/dist-lib/lib/tools/skills/index.d.ts +1 -0
  163. package/dist-lib/lib/tools/skills/index.js +1 -0
  164. package/dist-lib/lib/tools/skills/skill-tree.d.ts +5 -0
  165. package/dist-lib/lib/tools/skills/skill-tree.js +54 -0
  166. package/dist-lib/lib/wire/download.d.ts +10 -0
  167. package/dist-lib/lib/wire/download.js +35 -0
  168. package/dist-lib/lib/wire/event.d.ts +41 -0
  169. package/dist-lib/lib/wire/event.js +306 -0
  170. package/dist-lib/lib/wire/fields.d.ts +3 -0
  171. package/dist-lib/lib/wire/fields.js +12 -0
  172. package/dist-lib/lib/wire/knowledge.d.ts +9 -0
  173. package/dist-lib/lib/wire/knowledge.js +68 -0
  174. package/dist-lib/lib/wire/round.d.ts +14 -0
  175. package/dist-lib/lib/wire/round.js +45 -0
  176. package/dist-lib/lib/wire/vectors.d.ts +6 -0
  177. package/dist-lib/lib/wire/vectors.js +33 -0
  178. package/dist-lib/lib/wire.d.ts +28 -77
  179. package/dist-lib/lib/wire.js +24 -489
  180. package/dist-lib/plugins/ocr/coverage.d.ts +3 -0
  181. package/dist-lib/plugins/ocr/coverage.js +67 -0
  182. package/dist-lib/plugins/ocr/ocr-runtime.d.ts +4 -2
  183. package/dist-lib/plugins/ocr/ocr-runtime.js +25 -4
  184. package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
  185. package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
  186. package/dist-lib/schema/harness.js +4 -1
  187. package/dist-lib/spec/hrpc/messages.js +25 -1
  188. package/dist-lib/spec/hyperschema/index.js +25 -1
  189. package/dist-lib/spec/hyperschema/schema.json +6 -1
  190. package/dist-lib/spec/hyperschema/types.d.ts +1 -0
  191. package/package.json +2 -2
@@ -0,0 +1,15 @@
1
+ export function toolCallMarkup(calls) {
2
+ return calls.map(markupForCall).join('\n');
3
+ }
4
+ function markupForCall(call) {
5
+ return `<tool_call>\n${functionMarkup(call)}\n</tool_call>`;
6
+ }
7
+ export function functionMarkup(call) {
8
+ const params = Object.entries(call.arguments ?? {})
9
+ .map(([key, value]) => `<parameter=${key}>\n${markupValue(value)}\n</parameter>\n`)
10
+ .join('');
11
+ return `<function=${call.name}>\n${params}</function>`;
12
+ }
13
+ function markupValue(value) {
14
+ return typeof value === 'string' ? value : JSON.stringify(value ?? null);
15
+ }
@@ -0,0 +1,22 @@
1
+ export declare const MAX_NAME_LENGTH = 64;
2
+ export interface RecoveredToolCall {
3
+ id: string;
4
+ name: string;
5
+ arguments: Record<string, unknown>;
6
+ raw?: string;
7
+ }
8
+ export declare const MAX_RAW_CALL_CHARS = 4096;
9
+ export declare const MALFORMED_CALL_ID = "call-malformed";
10
+ export declare function clipRawCall(raw: string): string;
11
+ export interface KnownTool {
12
+ readonly name: string;
13
+ }
14
+ export declare function recoverMarkupToolCalls(text: string, knownTools: ReadonlyArray<KnownTool>): RecoveredToolCall[];
15
+ export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
16
+ export declare function normalizeToolArgs(args: Record<string, unknown>, declaredKeys: ReadonlyArray<string>): Record<string, unknown>;
17
+ interface DecodedArguments {
18
+ readonly args: Record<string, unknown>;
19
+ readonly ambiguous: boolean;
20
+ }
21
+ export declare function decodeArguments(block: string, maxNameLength?: number): DecodedArguments;
22
+ export {};
@@ -0,0 +1,130 @@
1
+ import { isRecord } from '../../guards.js';
2
+ import { namedMarkupBlocks } from '../markup-scanner.js';
3
+ export const MAX_NAME_LENGTH = 64;
4
+ export const MAX_RAW_CALL_CHARS = 4096;
5
+ export const MALFORMED_CALL_ID = 'call-malformed';
6
+ export function clipRawCall(raw) {
7
+ return raw.length > MAX_RAW_CALL_CHARS ? raw.slice(0, MAX_RAW_CALL_CHARS) : raw;
8
+ }
9
+ export function recoverMarkupToolCalls(text, knownTools) {
10
+ const known = new Set(knownTools.map((tool) => tool.name));
11
+ const calls = [];
12
+ for (const block of namedMarkupBlocks(text, 'function', MAX_NAME_LENGTH)) {
13
+ if (block.nested) {
14
+ calls.push(...recoverLegacyCalls(text.slice(block.start, block.end), known));
15
+ continue;
16
+ }
17
+ if (!block.name || !known.has(block.name)) {
18
+ continue;
19
+ }
20
+ calls.push({
21
+ id: '',
22
+ name: block.name,
23
+ arguments: decodeArguments(block.body).args,
24
+ raw: clipRawCall(text.slice(block.start, block.end))
25
+ });
26
+ }
27
+ return calls.map((call, index) => ({ ...call, id: `recovered-markup-${index}` }));
28
+ }
29
+ function recoverLegacyCalls(text, known) {
30
+ const calls = [];
31
+ let cursor = 0;
32
+ for (;;) {
33
+ const start = text.indexOf('<function=', cursor);
34
+ if (start === -1) {
35
+ return calls;
36
+ }
37
+ const nameStart = start + '<function='.length;
38
+ const nameEnd = text.indexOf('>', nameStart);
39
+ const close = nameEnd === -1 ? -1 : text.indexOf('</function>', nameEnd + 1);
40
+ if (nameEnd === -1 || nameEnd - nameStart > MAX_NAME_LENGTH || close === -1) {
41
+ return calls;
42
+ }
43
+ const name = text.slice(nameStart, nameEnd).trim();
44
+ const end = close + '</function>'.length;
45
+ if (name && known.has(name)) {
46
+ calls.push({
47
+ id: '',
48
+ name,
49
+ arguments: decodeLegacyArguments(text.slice(nameEnd + 1, close), 64),
50
+ raw: clipRawCall(text.slice(start, end))
51
+ });
52
+ }
53
+ cursor = end;
54
+ }
55
+ }
56
+ const ARG_WRAPPER_KEYS = new Set(['parameters', 'arguments', 'args', 'input', 'params']);
57
+ export function isPlainObject(value) {
58
+ return isRecord(value) && !Array.isArray(value);
59
+ }
60
+ export function normalizeToolArgs(args, declaredKeys) {
61
+ const [only, ...rest] = Object.keys(args);
62
+ if (only === undefined || rest.length) {
63
+ return args;
64
+ }
65
+ if (!ARG_WRAPPER_KEYS.has(only) || declaredKeys.includes(only)) {
66
+ return args;
67
+ }
68
+ const inner = args[only];
69
+ if (isPlainObject(inner)) {
70
+ return inner;
71
+ }
72
+ if (typeof inner === 'string') {
73
+ try {
74
+ const parsed = JSON.parse(inner);
75
+ if (isPlainObject(parsed)) {
76
+ return parsed;
77
+ }
78
+ }
79
+ catch {
80
+ return args;
81
+ }
82
+ }
83
+ return args;
84
+ }
85
+ export function decodeArguments(block, maxNameLength = MAX_NAME_LENGTH) {
86
+ const args = {};
87
+ let ambiguous = false;
88
+ for (const param of namedMarkupBlocks(block, 'parameter', maxNameLength)) {
89
+ if (!param.name)
90
+ continue;
91
+ ambiguous ||= param.nested;
92
+ defineArgument(args, param.name, param.body);
93
+ }
94
+ return { args: ambiguous ? decodeLegacyArguments(block, maxNameLength) : args, ambiguous };
95
+ }
96
+ function decodeLegacyArguments(block, maxNameLength) {
97
+ const args = {};
98
+ let cursor = 0;
99
+ for (;;) {
100
+ const start = block.indexOf('<parameter=', cursor);
101
+ if (start === -1) {
102
+ return args;
103
+ }
104
+ const nameStart = start + '<parameter='.length;
105
+ const nameEnd = block.indexOf('>', nameStart);
106
+ if (nameEnd === -1 || nameEnd - nameStart > maxNameLength) {
107
+ return args;
108
+ }
109
+ const end = block.indexOf('</parameter>', nameEnd + 1);
110
+ if (end === -1) {
111
+ return args;
112
+ }
113
+ const name = block.slice(nameStart, nameEnd).trim();
114
+ if (name) {
115
+ defineArgument(args, name, block.slice(nameEnd + 1, end));
116
+ }
117
+ cursor = end + '</parameter>'.length;
118
+ }
119
+ }
120
+ function defineArgument(args, name, body) {
121
+ const raw = body.trim();
122
+ let value;
123
+ try {
124
+ value = JSON.parse(raw);
125
+ }
126
+ catch {
127
+ value = raw;
128
+ }
129
+ Object.defineProperty(args, name, { value, enumerable: true, configurable: true, writable: true });
130
+ }
@@ -0,0 +1,3 @@
1
+ import type { RecoveredToolCall } from './recover.ts';
2
+ export declare function recoverSkillNamedCall(text: string, skillNames: ReadonlyArray<string>, skillToolName: string): RecoveredToolCall[];
3
+ export declare function recoverPrintedSkillLoad(text: string, skillToolName: string): RecoveredToolCall[];
@@ -0,0 +1,56 @@
1
+ import { namedMarkupBlocks } from '../markup-scanner.js';
2
+ import { MAX_NAME_LENGTH, clipRawCall, isPlainObject } from './recover.js';
3
+ export function recoverSkillNamedCall(text, skillNames, skillToolName) {
4
+ const known = new Set(skillNames);
5
+ for (const block of namedMarkupBlocks(text, 'function', MAX_NAME_LENGTH)) {
6
+ if (!known.has(block.name))
7
+ continue;
8
+ return [
9
+ {
10
+ id: 'recovered-skill-named-call',
11
+ name: skillToolName,
12
+ arguments: { name: block.name },
13
+ raw: clipRawCall(text.slice(block.start, block.end))
14
+ }
15
+ ];
16
+ }
17
+ return [];
18
+ }
19
+ const SKILL_LOAD_KEYS = new Set(['name', 'skill', 'skill_name', 'file', 'path', 'filename']);
20
+ export function recoverPrintedSkillLoad(text, skillToolName) {
21
+ for (const match of text.matchAll(/\{[^{}]*\}/g)) {
22
+ const candidate = parsePrintedCandidate(match[0]);
23
+ if (!isPlainObject(candidate)) {
24
+ continue;
25
+ }
26
+ const keys = Object.keys(candidate);
27
+ if (keys.length === 0 || !keys.every((key) => SKILL_LOAD_KEYS.has(key))) {
28
+ continue;
29
+ }
30
+ const name = candidate.name ?? candidate.skill ?? candidate.skill_name;
31
+ const file = candidate.file ?? candidate.path ?? candidate.filename;
32
+ if (typeof name !== 'string' || typeof file !== 'string') {
33
+ continue;
34
+ }
35
+ if (!/\.(md|py)\s*$/i.test(file.replaceAll('"', '').trim())) {
36
+ continue;
37
+ }
38
+ return [
39
+ {
40
+ id: 'recovered-printed-skill-load',
41
+ name: skillToolName,
42
+ arguments: { name, file },
43
+ raw: clipRawCall(match[0])
44
+ }
45
+ ];
46
+ }
47
+ return [];
48
+ }
49
+ function parsePrintedCandidate(text) {
50
+ try {
51
+ return JSON.parse(text);
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ }
@@ -0,0 +1,4 @@
1
+ export declare function redactToolCallMarkup(text: string, redactArgs: (name: string, args: Record<string, unknown>) => Record<string, unknown>): string;
2
+ export declare function redactBareParameterMarkup(text: string, isSensitiveName: (name: string) => boolean): string;
3
+ export declare function stripToolCallMarkup(text: string): string;
4
+ export declare function stripToolResponseBlocks(text: string): string;
@@ -0,0 +1,149 @@
1
+ import { isRecord } from '../../guards.js';
2
+ import { namedMarkupBlocks, scanNamedMarkup } from '../markup-scanner.js';
3
+ import { functionMarkup } from './emit.js';
4
+ import { MAX_NAME_LENGTH, decodeArguments } from './recover.js';
5
+ const TOOL_RESPONSE = { open: '<tool_response>', close: '</tool_response>' };
6
+ const TOOL_CALL = { open: '<tool_call>', close: '</tool_call>' };
7
+ export function redactToolCallMarkup(text, redactArgs) {
8
+ let out = '';
9
+ let cursor = 0;
10
+ let changed = false;
11
+ for (const block of namedMarkupBlocks(text, 'function', Infinity)) {
12
+ const replacement = redactedMarkupBlock(block, redactArgs);
13
+ if (replacement === null) {
14
+ continue;
15
+ }
16
+ out += text.slice(cursor, block.start) + replacement;
17
+ cursor = block.end;
18
+ changed = true;
19
+ }
20
+ out += text.slice(cursor);
21
+ return changed ? out : text;
22
+ }
23
+ function redactedMarkupBlock(block, redactArgs) {
24
+ if (block.nested) {
25
+ return '<function=redacted></function>';
26
+ }
27
+ const { args: decoded, ambiguous } = decodeArguments(block.body, Infinity);
28
+ if (ambiguous) {
29
+ return `<function=${block.name}></function>`;
30
+ }
31
+ const redacted = redactArgs(block.name, decoded);
32
+ const replacement = deepEqual(decoded, redacted)
33
+ ? null
34
+ : functionMarkup({ name: block.name, arguments: redacted });
35
+ return replacement;
36
+ }
37
+ export function redactBareParameterMarkup(text, isSensitiveName) {
38
+ if (!/<parameter/i.test(text)) {
39
+ return text;
40
+ }
41
+ let out = '';
42
+ let cursor = 0;
43
+ let changed = false;
44
+ for (;;) {
45
+ const open = scanNamedMarkup(text, 'parameter', { from: cursor, maxNameLength: Infinity }).token;
46
+ if (!open) {
47
+ break;
48
+ }
49
+ if (open.kind === 'close' || !isSensitiveName(open.name ?? '')) {
50
+ cursor = open.end;
51
+ continue;
52
+ }
53
+ const close = scanNamedMarkup(text, 'parameter', {
54
+ from: open.end,
55
+ maxNameLength: Infinity
56
+ }).token;
57
+ out += text.slice(cursor, open.end);
58
+ changed = true;
59
+ if (close?.kind === 'close') {
60
+ out += `[redacted]${text.slice(close.start, close.end)}`;
61
+ cursor = close.end;
62
+ }
63
+ else {
64
+ out += '[redacted]';
65
+ cursor = text.length;
66
+ break;
67
+ }
68
+ }
69
+ out += text.slice(cursor);
70
+ return redactRunOnParameterOpeners(changed ? out : text, isSensitiveName);
71
+ }
72
+ function redactRunOnParameterOpeners(text, isSensitiveName) {
73
+ const opener = /<parameter\s*=\s*([^\s<>]+)\s+(?=[^\s>])/gi;
74
+ const closer = /<\/parameter>/gi;
75
+ let out = '';
76
+ let cursor = 0;
77
+ for (let match = opener.exec(text); match; match = opener.exec(text)) {
78
+ if (!isSensitiveName(match[1])) {
79
+ continue;
80
+ }
81
+ const valueStart = match.index + match[0].length;
82
+ closer.lastIndex = valueStart;
83
+ const close = closer.exec(text);
84
+ out += `${text.slice(cursor, valueStart)}[redacted]`;
85
+ if (!close) {
86
+ return out;
87
+ }
88
+ cursor = close.index;
89
+ opener.lastIndex = close.index;
90
+ }
91
+ return cursor === 0 ? text : out + text.slice(cursor);
92
+ }
93
+ function deepEqual(a, b) {
94
+ if (a === b)
95
+ return true;
96
+ if (Array.isArray(a) || Array.isArray(b)) {
97
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
98
+ return false;
99
+ }
100
+ return a.every((value, index) => deepEqual(value, b[index]));
101
+ }
102
+ if (!isRecord(a) || !isRecord(b)) {
103
+ return false;
104
+ }
105
+ const aKeys = Object.keys(a);
106
+ const bKeys = Object.keys(b);
107
+ if (aKeys.length !== bKeys.length) {
108
+ return false;
109
+ }
110
+ return aKeys.every((key) => Object.hasOwn(b, key) && deepEqual(a[key], b[key]));
111
+ }
112
+ export function stripToolCallMarkup(text) {
113
+ let out = '';
114
+ let cursor = 0;
115
+ for (const block of namedMarkupBlocks(text, 'function', MAX_NAME_LENGTH)) {
116
+ out += text.slice(cursor, block.start);
117
+ cursor = block.end;
118
+ }
119
+ out += text.slice(cursor);
120
+ const withoutSpans = stripSpans(stripToolResponseBlocks(out), TOOL_CALL);
121
+ return withoutSpans
122
+ .replace(/<\/?(tool_call|tool_response)>/gi, '')
123
+ .replace(/\n{3,}/g, '\n\n')
124
+ .trim();
125
+ }
126
+ function lowerAscii(char) {
127
+ return char.toLowerCase();
128
+ }
129
+ function stripSpans(text, span) {
130
+ const hay = text.replace(/[A-Z]/g, lowerAscii);
131
+ let out = '';
132
+ let cursor = 0;
133
+ for (;;) {
134
+ const start = hay.indexOf(span.open, cursor);
135
+ if (start === -1) {
136
+ break;
137
+ }
138
+ out += text.slice(cursor, start);
139
+ const end = hay.indexOf(span.close, start + span.open.length);
140
+ if (end === -1) {
141
+ return out;
142
+ }
143
+ cursor = end + span.close.length;
144
+ }
145
+ return out + text.slice(cursor);
146
+ }
147
+ export function stripToolResponseBlocks(text) {
148
+ return stripSpans(text, TOOL_RESPONSE);
149
+ }
@@ -1,25 +1,20 @@
1
- export interface RecoveredToolCall {
2
- id: string;
3
- name: string;
4
- arguments: Record<string, unknown>;
5
- raw?: string;
1
+ import { toolCallMarkup } from './tool-call-markup/emit.ts';
2
+ import { clipRawCall, normalizeToolArgs, recoverMarkupToolCalls } from './tool-call-markup/recover.ts';
3
+ import { recoverPrintedSkillLoad, recoverSkillNamedCall } from './tool-call-markup/skill-recovery.ts';
4
+ import { redactBareParameterMarkup, redactToolCallMarkup, stripToolCallMarkup, stripToolResponseBlocks } from './tool-call-markup/strip.ts';
5
+ export type { MarkupToolCall } from './tool-call-markup/emit.ts';
6
+ export type { KnownTool, RecoveredToolCall } from './tool-call-markup/recover.ts';
7
+ export declare class ToolCallMarkup {
8
+ static MALFORMED_CALL_ID: string;
9
+ static MAX_RAW_CALL_CHARS: number;
10
+ static clipRawCall: typeof clipRawCall;
11
+ static normalizeToolArgs: typeof normalizeToolArgs;
12
+ static recoverMarkupToolCalls: typeof recoverMarkupToolCalls;
13
+ static recoverPrintedSkillLoad: typeof recoverPrintedSkillLoad;
14
+ static recoverSkillNamedCall: typeof recoverSkillNamedCall;
15
+ static redactBareParameterMarkup: typeof redactBareParameterMarkup;
16
+ static redactToolCallMarkup: typeof redactToolCallMarkup;
17
+ static stripToolCallMarkup: typeof stripToolCallMarkup;
18
+ static stripToolResponseBlocks: typeof stripToolResponseBlocks;
19
+ static toolCallMarkup: typeof toolCallMarkup;
6
20
  }
7
- export declare const MAX_RAW_CALL_CHARS = 4096;
8
- export declare const MALFORMED_CALL_ID = "call-malformed";
9
- export declare function clipRawCall(raw: string): string;
10
- export interface KnownTool {
11
- readonly name: string;
12
- }
13
- export declare function recoverMarkupToolCalls(text: string, knownTools: ReadonlyArray<KnownTool>): RecoveredToolCall[];
14
- export declare function redactToolCallMarkup(text: string, redactArgs: (name: string, args: Record<string, unknown>) => Record<string, unknown>): string;
15
- export declare function redactBareParameterMarkup(text: string, isSensitiveName: (name: string) => boolean): string;
16
- export declare function stripToolCallMarkup(text: string): string;
17
- export declare function stripToolResponseBlocks(text: string): string;
18
- export interface MarkupToolCall {
19
- readonly name: string;
20
- readonly arguments?: Record<string, unknown>;
21
- }
22
- export declare function toolCallMarkup(calls: ReadonlyArray<MarkupToolCall>): string;
23
- export declare function normalizeToolArgs(args: Record<string, unknown>, declaredKeys: ReadonlyArray<string>): Record<string, unknown>;
24
- export declare function recoverSkillNamedCall(text: string, skillNames: ReadonlyArray<string>, skillToolName: string): RecoveredToolCall[];
25
- export declare function recoverPrintedSkillLoad(text: string, skillToolName: string): RecoveredToolCall[];