@mupt-ai/dari-gateway 0.0.0-stage → 0.2.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.
Files changed (133) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +135 -2
  3. package/dari-gateway.example.yaml +28 -0
  4. package/package.json +42 -4
  5. package/src/bun.ts +4 -0
  6. package/src/context/eval_catalog.ts +11 -0
  7. package/src/context/interaction_fingerprint.ts +691 -0
  8. package/src/context/interaction_fingerprint_format.ts +189 -0
  9. package/src/context/interaction_lease_depth.ts +150 -0
  10. package/src/context/lease_history.ts +200 -0
  11. package/src/context/pricing.ts +30 -0
  12. package/src/context/provider_prompt_accounting.ts +241 -0
  13. package/src/context/routing_context.ts +337 -0
  14. package/src/http/auth.ts +43 -0
  15. package/src/http/errors.ts +114 -0
  16. package/src/http/handler.ts +110 -0
  17. package/src/http/http2_server.ts +122 -0
  18. package/src/http/serve.ts +119 -0
  19. package/src/index.ts +22 -0
  20. package/src/ingress/anthropic_messages.ts +162 -0
  21. package/src/ingress/anthropic_models.ts +28 -0
  22. package/src/ingress/chat_completions.ts +49 -0
  23. package/src/ingress/claude_code/request_context.ts +36 -0
  24. package/src/ingress/codex.ts +81 -0
  25. package/src/ingress/codex_models.ts +113 -0
  26. package/src/ingress/json.ts +113 -0
  27. package/src/ingress/models.ts +55 -0
  28. package/src/interaction/capabilities.ts +30 -0
  29. package/src/interaction/image.ts +12 -0
  30. package/src/interaction/portable_reasoning_state.ts +25 -0
  31. package/src/interaction/provider_continuation_state.ts +36 -0
  32. package/src/interaction/tool_identity.ts +65 -0
  33. package/src/interaction/types.ts +282 -0
  34. package/src/models/context_limits.ts +8 -0
  35. package/src/models/execution_provider.ts +22 -0
  36. package/src/models/gateways.ts +73 -0
  37. package/src/models/ids.ts +26 -0
  38. package/src/models/provider_defaults.ts +47 -0
  39. package/src/models/runtime_overrides.ts +221 -0
  40. package/src/observability/errors.ts +25 -0
  41. package/src/platform/platform.ts +204 -0
  42. package/src/platform/routing_contract.ts +6 -0
  43. package/src/protocol/input/anthropic_messages.ts +531 -0
  44. package/src/protocol/input/chat_completions.ts +255 -0
  45. package/src/protocol/input/chat_completions_validation.ts +293 -0
  46. package/src/protocol/input/openai_responses.ts +749 -0
  47. package/src/protocol/input/router_model.ts +53 -0
  48. package/src/protocol/output/anthropic_messages.ts +32 -0
  49. package/src/protocol/output/anthropic_messages_output.ts +129 -0
  50. package/src/protocol/output/anthropic_messages_stream.ts +281 -0
  51. package/src/protocol/output/chat_completions.ts +451 -0
  52. package/src/protocol/output/chat_reasoning_detail.ts +19 -0
  53. package/src/protocol/output/delivered_stream_snapshot.ts +31 -0
  54. package/src/protocol/output/openai_responses.ts +30 -0
  55. package/src/protocol/output/openai_responses_output.ts +159 -0
  56. package/src/protocol/output/openai_responses_stream.ts +631 -0
  57. package/src/protocol/output/sse_stream.ts +102 -0
  58. package/src/providers/anthropic_safeguards.ts +115 -0
  59. package/src/providers/aws_assume_role.ts +147 -0
  60. package/src/providers/cancel_estimate.ts +140 -0
  61. package/src/providers/execution.ts +800 -0
  62. package/src/providers/model_capabilities.ts +50 -0
  63. package/src/providers/pi.ts +499 -0
  64. package/src/providers/pi_context.ts +466 -0
  65. package/src/providers/pi_options.ts +416 -0
  66. package/src/providers/pi_output.ts +500 -0
  67. package/src/providers/provider_attempt_runner.ts +153 -0
  68. package/src/providers/provider_call.ts +433 -0
  69. package/src/providers/provider_reported_cost.ts +138 -0
  70. package/src/providers/reasoning.ts +59 -0
  71. package/src/providers/response_capture.ts +65 -0
  72. package/src/providers/stream_finalization.ts +264 -0
  73. package/src/providers/subscription_limits.ts +208 -0
  74. package/src/providers/subscription_usage.ts +99 -0
  75. package/src/runtime/claude_code/auxiliary_routing.ts +80 -0
  76. package/src/runtime/customer_disconnect.ts +66 -0
  77. package/src/runtime/explicit_model.ts +86 -0
  78. package/src/runtime/harness_identity/conversation.ts +87 -0
  79. package/src/runtime/harness_identity/extract.ts +323 -0
  80. package/src/runtime/harness_identity/index.ts +14 -0
  81. package/src/runtime/prepare_routed_completion.ts +224 -0
  82. package/src/runtime/provider_stream_lifecycle.ts +105 -0
  83. package/src/runtime/provider_stream_outcome.ts +83 -0
  84. package/src/runtime/request_tags.ts +34 -0
  85. package/src/runtime/routed_completion.ts +102 -0
  86. package/src/runtime/routed_protocol.ts +33 -0
  87. package/src/runtime/routed_runtime.ts +722 -0
  88. package/src/runtime/routing_lease.ts +273 -0
  89. package/src/runtime/runtime_reporter.ts +115 -0
  90. package/src/self_host/config.ts +173 -0
  91. package/src/self_host/main.ts +104 -0
  92. package/src/self_host/platform.ts +63 -0
  93. package/src/self_host/prefix_store.ts +315 -0
  94. package/src/self_host/resolve.ts +240 -0
  95. package/src/self_host/subscriptions/access.ts +43 -0
  96. package/src/self_host/subscriptions/http.ts +204 -0
  97. package/src/self_host/subscriptions/index.ts +43 -0
  98. package/src/self_host/subscriptions/oauth.ts +395 -0
  99. package/src/self_host/subscriptions/store.ts +339 -0
  100. package/src/strategies/auto_routing_selector.ts +72 -0
  101. package/src/strategies/eval_relevance.ts +57 -0
  102. package/src/strategies/fallback_decision.ts +30 -0
  103. package/src/strategies/priority_selector.ts +40 -0
  104. package/src/strategies/route_decision.ts +106 -0
  105. package/src/strategies/routing.ts +455 -0
  106. package/src/strategies/selector_projection.ts +207 -0
  107. package/src/telemetry/background.ts +40 -0
  108. package/src/telemetry/benchmark_replay.ts +160 -0
  109. package/src/telemetry/billing_finalization.ts +167 -0
  110. package/src/telemetry/ingest_values.ts +15 -0
  111. package/src/telemetry/model_cost.ts +14 -0
  112. package/src/telemetry/prefix_state.ts +183 -0
  113. package/src/telemetry/report.ts +832 -0
  114. package/src/telemetry/requests/interaction_observability.ts +258 -0
  115. package/src/telemetry/requests/retained_interaction_request.ts +20 -0
  116. package/src/telemetry/scrub.ts +38 -0
  117. package/src/telemetry/skills/claude/catalog.ts +119 -0
  118. package/src/telemetry/skills/claude/file_reads.ts +153 -0
  119. package/src/telemetry/skills/claude/observations.ts +450 -0
  120. package/src/telemetry/skills/codex/catalog.ts +171 -0
  121. package/src/telemetry/skills/codex/exec_file_reads.ts +55 -0
  122. package/src/telemetry/skills/codex/explicit.ts +116 -0
  123. package/src/telemetry/skills/codex/file_reads.ts +51 -0
  124. package/src/telemetry/skills/codex/identity.ts +21 -0
  125. package/src/telemetry/skills/observations.ts +94 -0
  126. package/src/telemetry/skills/paths.ts +82 -0
  127. package/src/telemetry/skills/pi.ts +276 -0
  128. package/src/telemetry/skills/shell_file_reads.ts +267 -0
  129. package/src/telemetry/skills/shell_words.ts +202 -0
  130. package/src/telemetry/tools/input.ts +16 -0
  131. package/src/telemetry/tools/observations.ts +349 -0
  132. package/src/telemetry/usage.ts +173 -0
  133. package/src/types.ts +150 -0
@@ -0,0 +1,116 @@
1
+ import type {
2
+ InteractionMessage,
3
+ InteractionRequest,
4
+ } from "../../../interaction/types";
5
+ import type { SkillObservation } from "../observations";
6
+ import {
7
+ codexSkillCapabilityId,
8
+ normalizedCodexSkillName,
9
+ } from "./identity";
10
+
11
+ const MAX_SKILL_PATH_CHARS = 16 * 1024;
12
+ const UNSAFE_DISPLAY_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u;
13
+ const SKILL_MENTION = /\$([A-Za-z0-9][A-Za-z0-9_:-]*)/gu;
14
+
15
+ export function codexExplicitSkillObservations(
16
+ request: InteractionRequest,
17
+ currentTurnId: string,
18
+ limit: number,
19
+ ): SkillObservation[] {
20
+ let currentUserStart = request.items.length;
21
+ while (currentUserStart > 0) {
22
+ const item = request.items[currentUserStart - 1];
23
+ if (item.type !== "message" || item.role !== "user") break;
24
+ currentUserStart -= 1;
25
+ }
26
+ const parsed: Array<{
27
+ message: InteractionMessage;
28
+ skill?: { name: string; path: string };
29
+ }> = [];
30
+ for (let index = currentUserStart; index < request.items.length; index += 1) {
31
+ const message = request.items[index];
32
+ if (message.type !== "message" || message.role !== "user") continue;
33
+ parsed.push({ message, skill: parseCodexSkillMessage(message) });
34
+ }
35
+
36
+ const mentionedNames = new Set<string>();
37
+ const observed: SkillObservation[] = [];
38
+ const observedInjections = new Set<string>();
39
+ for (const { message, skill } of parsed) {
40
+ if (skill !== undefined) {
41
+ const injection = skillInjectionKey(skill);
42
+ if (
43
+ (
44
+ message.clientTurnId?.toLowerCase() === currentTurnId.toLowerCase()
45
+ || (
46
+ message.clientTurnId === undefined
47
+ && mentionedNames.has(skill.name)
48
+ )
49
+ )
50
+ && !observedInjections.has(injection)
51
+ ) {
52
+ observedInjections.add(injection);
53
+ observed.push(observation(skill.name));
54
+ if (observed.length === limit) break;
55
+ }
56
+ continue;
57
+ }
58
+ for (const match of textParts(message).join("").matchAll(SKILL_MENTION)) {
59
+ if (match[1] !== undefined) mentionedNames.add(match[1]);
60
+ }
61
+ }
62
+ return observed;
63
+ }
64
+
65
+ function observation(name: string): SkillObservation {
66
+ return {
67
+ eventType: "skill_use",
68
+ toolCallId: null,
69
+ capabilityId: codexSkillCapabilityId(name),
70
+ observedName: name,
71
+ evidenceSource: "skill_content_injection",
72
+ };
73
+ }
74
+
75
+ function parseCodexSkillMessage(
76
+ message: InteractionMessage,
77
+ ): { name: string; path: string } | undefined {
78
+ if (message.content.length !== 1 || message.content[0].type !== "text") {
79
+ return undefined;
80
+ }
81
+ const text = message.content[0].text;
82
+ const prefix = "<skill>\n<name>";
83
+ if (!text.startsWith(prefix) || !text.endsWith("\n</skill>")) return undefined;
84
+
85
+ const nameEnd = text.indexOf("</name>\n<path>", prefix.length);
86
+ if (nameEnd < 0) return undefined;
87
+ const pathStart = nameEnd + "</name>\n<path>".length;
88
+ const pathEnd = text.indexOf("</path>\n", pathStart);
89
+ if (pathEnd < 0) return undefined;
90
+ const bodyStart = pathEnd + "</path>\n".length;
91
+ const bodyEnd = text.length - "\n</skill>".length;
92
+ if (bodyEnd <= bodyStart || text.slice(bodyStart, bodyEnd).trim().length === 0) {
93
+ return undefined;
94
+ }
95
+
96
+ const name = normalizedCodexSkillName(text.slice(prefix.length, nameEnd));
97
+ const path = normalizedSkillPath(text.slice(pathStart, pathEnd));
98
+ return name === undefined || path === undefined ? undefined : { name, path };
99
+ }
100
+
101
+ function skillInjectionKey(skill: { name: string; path: string }): string {
102
+ return `${skill.name}\u0000${skill.path}`;
103
+ }
104
+
105
+ function normalizedSkillPath(value: string): string | undefined {
106
+ const normalized = value.normalize("NFC");
107
+ return normalized.length > 0
108
+ && normalized.length <= MAX_SKILL_PATH_CHARS
109
+ && !UNSAFE_DISPLAY_CHARACTERS.test(normalized)
110
+ ? normalized
111
+ : undefined;
112
+ }
113
+
114
+ function textParts(message: InteractionMessage): string[] {
115
+ return message.content.flatMap((part) => part.type === "text" ? [part.text] : []);
116
+ }
@@ -0,0 +1,51 @@
1
+ import type {
2
+ InteractionOutput,
3
+ InteractionRequest,
4
+ } from "../../../interaction/types";
5
+ import {
6
+ codexExecCommand,
7
+ readSkillsFromCodexCommand,
8
+ } from "./exec_file_reads";
9
+ import { codexFileSkillCatalog } from "./catalog";
10
+ import { codexSkillCapabilityId } from "./identity";
11
+ import type { SkillObservation } from "../observations";
12
+
13
+ /** Match model-emitted Codex file reads to the exact current skill catalog. */
14
+ export function codexAutomaticSkillObservations(
15
+ request: InteractionRequest,
16
+ output: InteractionOutput,
17
+ limit: number,
18
+ ): SkillObservation[] {
19
+ if (
20
+ limit <= 0
21
+ || request.sourceProtocol !== "openai_responses"
22
+ || !request.tools.some(
23
+ (tool) =>
24
+ tool.kind === "function"
25
+ && tool.name === "exec_command"
26
+ && tool.namespace === undefined,
27
+ )
28
+ ) return [];
29
+ const catalog = codexFileSkillCatalog(request);
30
+ if (catalog === undefined) return [];
31
+
32
+ const observedPaths = new Set<string>();
33
+ const observed: SkillObservation[] = [];
34
+ for (const item of output.items) {
35
+ const input = codexExecCommand(item);
36
+ if (input === undefined) continue;
37
+ for (const skill of readSkillsFromCodexCommand(input, catalog)) {
38
+ if (observedPaths.has(skill.path.key)) continue;
39
+ observedPaths.add(skill.path.key);
40
+ observed.push({
41
+ eventType: "skill_load",
42
+ toolCallId: input.callId,
43
+ capabilityId: codexSkillCapabilityId(skill.name),
44
+ observedName: skill.name,
45
+ evidenceSource: "skill_file_read",
46
+ });
47
+ if (observed.length === limit) return observed;
48
+ }
49
+ }
50
+ return observed;
51
+ }
@@ -0,0 +1,21 @@
1
+ const CAPABILITY_PREFIX = "codex:name:";
2
+ const MAX_CAPABILITY_ID_CHARS = 512;
3
+ const MAX_SKILL_NAME_CHARS = 512;
4
+ const SKILL_NAME = /^[A-Za-z0-9][A-Za-z0-9_:-]*$/u;
5
+ const UNSAFE_DISPLAY_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u;
6
+
7
+ export function codexSkillCapabilityId(name: string): string {
8
+ return `${CAPABILITY_PREFIX}${name}`;
9
+ }
10
+
11
+ export function normalizedCodexSkillName(value: unknown): string | undefined {
12
+ if (typeof value !== "string") return undefined;
13
+ const normalized = value.normalize("NFC").trim();
14
+ return normalized.length > 0
15
+ && normalized.length <= MAX_SKILL_NAME_CHARS
16
+ && normalized.length <= MAX_CAPABILITY_ID_CHARS - CAPABILITY_PREFIX.length
17
+ && SKILL_NAME.test(normalized)
18
+ && !UNSAFE_DISPLAY_CHARACTERS.test(normalized)
19
+ ? normalized
20
+ : undefined;
21
+ }
@@ -0,0 +1,94 @@
1
+ import { isBackgroundRequest, type InteractionOutput, type InteractionRequest } from "../../interaction/types";
2
+ import type { HarnessConversationIdentity } from "../../runtime/harness_identity";
3
+ import {
4
+ piStyleAutomaticSkillObservations,
5
+ piStyleExplicitSkillObservations,
6
+ } from "./pi";
7
+ import { claudeFileReadObservations } from "./claude/file_reads";
8
+ import { claudeSkillObservations } from "./claude/observations";
9
+ import { codexExplicitSkillObservations } from "./codex/explicit";
10
+ import { codexAutomaticSkillObservations } from "./codex/file_reads";
11
+
12
+ export type SkillEvidenceSource =
13
+ | "skill_content_injection"
14
+ | "skill_command_expansion"
15
+ | "skill_tool_activation"
16
+ | "skill_file_read";
17
+
18
+ export type SkillObservation = {
19
+ eventType: "skill_use" | "skill_load";
20
+ toolCallId: string | null;
21
+ capabilityId: string;
22
+ observedName: string;
23
+ evidenceSource: SkillEvidenceSource;
24
+ };
25
+
26
+ export function inputSkillObservations(args: {
27
+ request: InteractionRequest;
28
+ harnessConversationIdentity?: HarnessConversationIdentity;
29
+ limit: number;
30
+ }): SkillObservation[] {
31
+ if (args.limit <= 0 || isBackgroundRequest(args.request)) return [];
32
+ if (
33
+ args.request.requestPurpose === undefined
34
+ && args.request.sourceProtocol === "openai_chat"
35
+ ) {
36
+ return piStyleExplicitSkillObservations(args.request, args.limit);
37
+ }
38
+ if (
39
+ args.request.requestPurpose === undefined
40
+ && args.request.sourceProtocol === "openai_responses"
41
+ && args.harnessConversationIdentity?.kind === "codex_thread"
42
+ && args.harnessConversationIdentity.userTurnId !== undefined
43
+ ) {
44
+ return codexExplicitSkillObservations(
45
+ args.request,
46
+ args.harnessConversationIdentity.userTurnId,
47
+ args.limit,
48
+ );
49
+ }
50
+ if (
51
+ args.request.sourceProtocol === "anthropic_messages"
52
+ && args.harnessConversationIdentity?.kind === "claude_code_session"
53
+ ) {
54
+ return claudeSkillObservations(args.request, args.limit);
55
+ }
56
+ return [];
57
+ }
58
+
59
+ export function outputSkillObservations(args: {
60
+ request: InteractionRequest;
61
+ output: InteractionOutput;
62
+ harnessConversationIdentity?: HarnessConversationIdentity;
63
+ limit: number;
64
+ }): SkillObservation[] {
65
+ if (args.limit <= 0 || isBackgroundRequest(args.request)) return [];
66
+ if (args.request.sourceProtocol === "openai_chat") {
67
+ return piStyleAutomaticSkillObservations(
68
+ args.request,
69
+ args.output,
70
+ args.limit,
71
+ );
72
+ }
73
+ if (
74
+ args.request.sourceProtocol === "openai_responses"
75
+ && args.harnessConversationIdentity?.kind === "codex_thread"
76
+ ) {
77
+ return codexAutomaticSkillObservations(
78
+ args.request,
79
+ args.output,
80
+ args.limit,
81
+ );
82
+ }
83
+ if (
84
+ args.request.sourceProtocol === "anthropic_messages"
85
+ && args.harnessConversationIdentity?.kind === "claude_code_session"
86
+ ) {
87
+ return claudeFileReadObservations(
88
+ args.request,
89
+ args.output,
90
+ args.limit,
91
+ );
92
+ }
93
+ return [];
94
+ }
@@ -0,0 +1,82 @@
1
+ import { posix, win32 } from "node:path";
2
+
3
+ const MAX_SKILL_PATH_CHARS = 16 * 1024;
4
+ const UNSAFE_DISPLAY_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u;
5
+
6
+ export type PathPlatform = "posix" | "windows";
7
+
8
+ export type NormalizedSkillPath = {
9
+ platform: PathPlatform;
10
+ value: string;
11
+ key: string;
12
+ };
13
+
14
+ export function normalizedAbsoluteSkillPath(
15
+ value: unknown,
16
+ ): NormalizedSkillPath | undefined {
17
+ if (
18
+ typeof value !== "string"
19
+ || value.length === 0
20
+ || value.length > MAX_SKILL_PATH_CHARS
21
+ || UNSAFE_DISPLAY_CHARACTERS.test(value)
22
+ || hasParentPathSegment(value)
23
+ ) return undefined;
24
+ if (posix.isAbsolute(value)) {
25
+ const normalized = posix.normalize(value);
26
+ return { platform: "posix", value: normalized, key: `posix:${normalized}` };
27
+ }
28
+ if (win32.isAbsolute(value)) {
29
+ const normalized = win32.normalize(value);
30
+ return {
31
+ platform: "windows",
32
+ value: normalized,
33
+ key: `windows:${normalized.toLowerCase()}`,
34
+ };
35
+ }
36
+ return undefined;
37
+ }
38
+
39
+ export function resolvedSkillPath(
40
+ value: string,
41
+ workdir: NormalizedSkillPath | undefined,
42
+ ): NormalizedSkillPath | undefined {
43
+ if (hasParentPathSegment(value) || /^[A-Za-z]:[^\\/]/u.test(value)) {
44
+ return undefined;
45
+ }
46
+ const absolute = normalizedAbsoluteSkillPath(value);
47
+ if (absolute !== undefined) return absolute;
48
+ if (workdir === undefined) return undefined;
49
+ const resolved = workdir.platform === "windows"
50
+ ? win32.resolve(workdir.value, value)
51
+ : posix.resolve(workdir.value, value);
52
+ const normalized = normalizedAbsoluteSkillPath(resolved);
53
+ return normalized?.platform === workdir.platform ? normalized : undefined;
54
+ }
55
+
56
+ export function joinedSkillPath(
57
+ root: NormalizedSkillPath,
58
+ suffix: string,
59
+ ): NormalizedSkillPath | undefined {
60
+ if (hasParentPathSegment(suffix)) return undefined;
61
+ const joined = root.platform === "windows"
62
+ ? win32.join(root.value, ...suffix.split("/"))
63
+ : posix.join(root.value, suffix);
64
+ const normalized = normalizedAbsoluteSkillPath(joined);
65
+ return normalized?.platform === root.platform ? normalized : undefined;
66
+ }
67
+
68
+ export function skillPathBasename(path: NormalizedSkillPath): string {
69
+ return path.platform === "windows"
70
+ ? win32.basename(path.value)
71
+ : posix.basename(path.value);
72
+ }
73
+
74
+ export function skillPathSegments(path: NormalizedSkillPath): string[] {
75
+ const implementation = path.platform === "windows" ? win32 : posix;
76
+ const root = implementation.parse(path.value).root;
77
+ return path.value.slice(root.length).split(implementation.sep).filter(Boolean);
78
+ }
79
+
80
+ function hasParentPathSegment(value: string): boolean {
81
+ return value.split(/[\\/]/u).includes("..");
82
+ }
@@ -0,0 +1,276 @@
1
+ import type {
2
+ InteractionMessage,
3
+ InteractionOutput,
4
+ InteractionRequest,
5
+ } from "../../interaction/types";
6
+ import { interactionToolInput } from "../tools/input";
7
+ import type { SkillObservation } from "./observations";
8
+
9
+ const CAPABILITY_PREFIX = "unattributed:name:";
10
+ const MAX_CAPABILITY_ID_CHARS = 512;
11
+ const MAX_SKILL_NAME_CHARS = 64;
12
+ const MAX_SKILL_PATH_CHARS = 16 * 1024;
13
+ const PI_SKILL_CLOSE = "\n</skill>";
14
+ const SKILL_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u;
15
+ const UNSAFE_DISPLAY_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/u;
16
+ const XML_ENTITY = /&(amp|lt|gt|quot|apos);/gu;
17
+ const INVALID_XML_ENTITY = /&(?!(?:amp|lt|gt|quot|apos);)/u;
18
+ const PI_SKILL_BLOCK = /(?:^|\n) <skill>\n([\s\S]*?)\n <\/skill>(?=\n|$)/gu;
19
+ const PI_SKILL_FIELDS = /^ <name>([^\n]*)<\/name>\n <description>[\s\S]*<\/description>\n <location>([^\n]*)<\/location>$/u;
20
+
21
+ /** Parse the exact expansion emitted by Pi for `/skill:name [arguments]`. */
22
+ export function piStyleExplicitSkillObservations(
23
+ request: InteractionRequest,
24
+ limit: number,
25
+ ): SkillObservation[] {
26
+ if (limit <= 0) return [];
27
+ return trailingUserMessages(request)
28
+ .flatMap((message) => {
29
+ const textParts = message.content.filter((part) => part.type === "text");
30
+ if (
31
+ textParts.length !== 1
32
+ || message.content.some(
33
+ (part) => part.type !== "text" && part.type !== "image",
34
+ )
35
+ ) return [];
36
+ const name = piStyleSkillName(textParts[0].text);
37
+ return name === undefined
38
+ ? []
39
+ : [observation(name, "skill_use", "skill_command_expansion", null)];
40
+ })
41
+ .slice(0, limit);
42
+ }
43
+
44
+ /** Match a current model-emitted Pi `read` call to its exact skill catalog entry. */
45
+ export function piStyleAutomaticSkillObservations(
46
+ request: InteractionRequest,
47
+ output: InteractionOutput,
48
+ limit: number,
49
+ ): SkillObservation[] {
50
+ if (
51
+ limit <= 0
52
+ || request.sourceProtocol !== "openai_chat"
53
+ || !request.tools.some(
54
+ (tool) =>
55
+ tool.kind === "function"
56
+ && tool.name === "read"
57
+ && tool.namespace === undefined,
58
+ )
59
+ ) {
60
+ return [];
61
+ }
62
+
63
+ const skillsByLocation = piStyleAvailableSkills(request);
64
+ if (skillsByLocation.size === 0) return [];
65
+
66
+ const observedCapabilities = new Set<string>();
67
+ const observed: SkillObservation[] = [];
68
+ for (const item of output.items) {
69
+ const read = piSkillRead(item);
70
+ if (read === undefined) continue;
71
+ const name = skillsByLocation.get(read.path);
72
+ if (name === undefined) continue;
73
+ const skill = observation(name, "skill_load", "skill_file_read", read.toolCallId);
74
+ if (observedCapabilities.has(skill.capabilityId)) continue;
75
+ observedCapabilities.add(skill.capabilityId);
76
+ observed.push(skill);
77
+ if (observed.length === limit) break;
78
+ }
79
+ return observed;
80
+ }
81
+
82
+ function observation(
83
+ name: string,
84
+ eventType: SkillObservation["eventType"],
85
+ evidenceSource: SkillObservation["evidenceSource"],
86
+ toolCallId: string | null,
87
+ ): SkillObservation {
88
+ return {
89
+ eventType,
90
+ toolCallId,
91
+ capabilityId: `${CAPABILITY_PREFIX}${name}`,
92
+ observedName: name,
93
+ evidenceSource,
94
+ };
95
+ }
96
+
97
+ function piStyleAvailableSkills(
98
+ request: InteractionRequest,
99
+ ): Map<string, string> {
100
+ const skills = new Map<string, string>();
101
+ const ambiguousLocations = new Set<string>();
102
+ for (const item of request.items) {
103
+ if (
104
+ item.type !== "message"
105
+ || (item.role !== "system" && item.role !== "developer")
106
+ ) {
107
+ continue;
108
+ }
109
+ for (const part of item.content) {
110
+ if (part.type !== "text") continue;
111
+ for (const section of part.text.matchAll(
112
+ /(?:^|\n)<available_skills>\n([\s\S]*?)\n<\/available_skills>(?=\n|$)/gu,
113
+ )) {
114
+ const body = section[1];
115
+ if (body === undefined) continue;
116
+ for (const block of body.matchAll(PI_SKILL_BLOCK)) {
117
+ const fields = block[1]?.match(PI_SKILL_FIELDS);
118
+ if (fields === null || fields === undefined) continue;
119
+ const nameValue = decodePiXmlText(fields[1]);
120
+ const location = normalizedSkillPath(decodePiXmlText(fields[2]));
121
+ const name = nameValue === undefined
122
+ ? undefined
123
+ : normalizedSkillName(nameValue);
124
+ if (
125
+ name === undefined
126
+ || location === undefined
127
+ || ambiguousLocations.has(location)
128
+ ) {
129
+ continue;
130
+ }
131
+ const previous = skills.get(location);
132
+ if (previous !== undefined && previous !== name) {
133
+ skills.delete(location);
134
+ ambiguousLocations.add(location);
135
+ } else {
136
+ skills.set(location, name);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+ return skills;
143
+ }
144
+
145
+ function piSkillRead(
146
+ item: InteractionOutput["items"][number],
147
+ ): { path: string; toolCallId: string } | undefined {
148
+ if (
149
+ item.type !== "tool_call"
150
+ || item.kind !== "function"
151
+ || item.namespace !== undefined
152
+ || item.name !== "read"
153
+ ) {
154
+ return undefined;
155
+ }
156
+ const input = interactionToolInput(item);
157
+ const path = input === undefined ? undefined : normalizedSkillPath(input.path);
158
+ return path === undefined ? undefined : { path, toolCallId: item.callId };
159
+ }
160
+
161
+ function decodePiXmlText(value: string | undefined): string | undefined {
162
+ if (value === undefined || INVALID_XML_ENTITY.test(value)) return undefined;
163
+ const entities: Record<string, string> = {
164
+ amp: "&",
165
+ lt: "<",
166
+ gt: ">",
167
+ quot: '"',
168
+ apos: "'",
169
+ };
170
+ return value.replace(XML_ENTITY, (_entity, name: string) => entities[name] ?? "");
171
+ }
172
+
173
+ function normalizedSkillPath(value: unknown): string | undefined {
174
+ if (typeof value !== "string") return undefined;
175
+ return value.length > 0
176
+ && value.length <= MAX_SKILL_PATH_CHARS
177
+ && !UNSAFE_DISPLAY_CHARACTERS.test(value)
178
+ ? value
179
+ : undefined;
180
+ }
181
+
182
+ function trailingUserMessages(
183
+ request: InteractionRequest,
184
+ ): InteractionMessage[] {
185
+ let index = request.items.length - 1;
186
+ while (index >= 0) {
187
+ const item = request.items[index];
188
+ if (
189
+ item.type === "message"
190
+ && (item.role === "system" || item.role === "developer")
191
+ ) {
192
+ index -= 1;
193
+ continue;
194
+ }
195
+ break;
196
+ }
197
+
198
+ const messages: InteractionMessage[] = [];
199
+ while (index >= 0) {
200
+ const item = request.items[index];
201
+ if (item.type !== "message" || item.role !== "user") break;
202
+ messages.push(item);
203
+ index -= 1;
204
+ }
205
+ return messages.reverse();
206
+ }
207
+
208
+ function piStyleSkillName(text: string): string | undefined {
209
+ const namePrefix = '<skill name="';
210
+ if (!text.startsWith(namePrefix)) return undefined;
211
+
212
+ const nameEnd = text.indexOf('" location="', namePrefix.length);
213
+ if (nameEnd < 0) return undefined;
214
+ const locationStart = nameEnd + '" location="'.length;
215
+ const locationEnd = text.indexOf('">\n', locationStart);
216
+ if (locationEnd < 0) return undefined;
217
+
218
+ const bodyStart = locationEnd + '">\n'.length;
219
+ const bodyEnd = piStyleSkillBodyEnd(text, bodyStart);
220
+ if (bodyEnd < 0) return undefined;
221
+
222
+ const name = normalizedSkillName(text.slice(namePrefix.length, nameEnd));
223
+ const location = text.slice(locationStart, locationEnd);
224
+ const body = text.slice(bodyStart, bodyEnd);
225
+ const referencesPrefix = "References are relative to ";
226
+ const referencesEnd = body.indexOf(".\n\n", referencesPrefix.length);
227
+ if (
228
+ name === undefined
229
+ || location.length === 0
230
+ || location.length > MAX_SKILL_PATH_CHARS
231
+ || UNSAFE_DISPLAY_CHARACTERS.test(location)
232
+ || !body.startsWith(referencesPrefix)
233
+ || referencesEnd < 0
234
+ || body.slice(referencesEnd + ".\n\n".length).trim() === ""
235
+ ) {
236
+ return undefined;
237
+ }
238
+ return name;
239
+ }
240
+
241
+ function piStyleSkillBodyEnd(
242
+ text: string,
243
+ bodyStart: number,
244
+ ): number {
245
+ let lastNonWhitespace = text.length - 1;
246
+ while (lastNonWhitespace >= 0 && /\s/u.test(text[lastNonWhitespace])) {
247
+ lastNonWhitespace -= 1;
248
+ }
249
+
250
+ let candidate = text.indexOf(PI_SKILL_CLOSE, bodyStart);
251
+ while (candidate >= 0) {
252
+ const suffixStart = candidate + PI_SKILL_CLOSE.length;
253
+ if (
254
+ suffixStart === text.length
255
+ || (
256
+ text.startsWith("\n\n", suffixStart)
257
+ && suffixStart + 2 <= lastNonWhitespace
258
+ )
259
+ ) {
260
+ return candidate;
261
+ }
262
+ candidate = text.indexOf(PI_SKILL_CLOSE, suffixStart);
263
+ }
264
+ return -1;
265
+ }
266
+
267
+ function normalizedSkillName(value: string): string | undefined {
268
+ const normalized = value.normalize("NFC").trim();
269
+ return normalized.length > 0
270
+ && normalized.length <= MAX_SKILL_NAME_CHARS
271
+ && normalized.length <= MAX_CAPABILITY_ID_CHARS - CAPABILITY_PREFIX.length
272
+ && SKILL_NAME.test(normalized)
273
+ && !UNSAFE_DISPLAY_CHARACTERS.test(normalized)
274
+ ? normalized
275
+ : undefined;
276
+ }