@skyold/model-protocol 0.1.0-beta.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 (104) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +22 -0
  3. package/README.zh-CN.md +17 -0
  4. package/dist/chat-egress.d.ts +12 -0
  5. package/dist/chat-egress.d.ts.map +1 -0
  6. package/dist/chat-egress.js +144 -0
  7. package/dist/chat-egress.js.map +1 -0
  8. package/dist/chat-ingress.d.ts +39 -0
  9. package/dist/chat-ingress.d.ts.map +1 -0
  10. package/dist/chat-ingress.js +323 -0
  11. package/dist/chat-ingress.js.map +1 -0
  12. package/dist/chat-stream-egress.d.ts +8 -0
  13. package/dist/chat-stream-egress.d.ts.map +1 -0
  14. package/dist/chat-stream-egress.js +129 -0
  15. package/dist/chat-stream-egress.js.map +1 -0
  16. package/dist/contracts.d.ts +61 -0
  17. package/dist/contracts.d.ts.map +1 -0
  18. package/dist/contracts.js +2 -0
  19. package/dist/contracts.js.map +1 -0
  20. package/dist/egress-codec-registry.d.ts +36 -0
  21. package/dist/egress-codec-registry.d.ts.map +1 -0
  22. package/dist/egress-codec-registry.js +125 -0
  23. package/dist/egress-codec-registry.js.map +1 -0
  24. package/dist/index.d.ts +39 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +25 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/ingress-codec-registry.d.ts +26 -0
  29. package/dist/ingress-codec-registry.d.ts.map +1 -0
  30. package/dist/ingress-codec-registry.js +106 -0
  31. package/dist/ingress-codec-registry.js.map +1 -0
  32. package/dist/media-images.d.ts +154 -0
  33. package/dist/media-images.d.ts.map +1 -0
  34. package/dist/media-images.js +721 -0
  35. package/dist/media-images.js.map +1 -0
  36. package/dist/media-video.d.ts +68 -0
  37. package/dist/media-video.d.ts.map +1 -0
  38. package/dist/media-video.js +196 -0
  39. package/dist/media-video.js.map +1 -0
  40. package/dist/native-images.d.ts +68 -0
  41. package/dist/native-images.d.ts.map +1 -0
  42. package/dist/native-images.js +424 -0
  43. package/dist/native-images.js.map +1 -0
  44. package/dist/openai-embeddings-egress.d.ts +4 -0
  45. package/dist/openai-embeddings-egress.d.ts.map +1 -0
  46. package/dist/openai-embeddings-egress.js +21 -0
  47. package/dist/openai-embeddings-egress.js.map +1 -0
  48. package/dist/openai-embeddings.d.ts +66 -0
  49. package/dist/openai-embeddings.d.ts.map +1 -0
  50. package/dist/openai-embeddings.js +442 -0
  51. package/dist/openai-embeddings.js.map +1 -0
  52. package/dist/openai-responses-egress.d.ts +17 -0
  53. package/dist/openai-responses-egress.d.ts.map +1 -0
  54. package/dist/openai-responses-egress.js +99 -0
  55. package/dist/openai-responses-egress.js.map +1 -0
  56. package/dist/openai-responses-stream-egress.d.ts +4 -0
  57. package/dist/openai-responses-stream-egress.d.ts.map +1 -0
  58. package/dist/openai-responses-stream-egress.js +252 -0
  59. package/dist/openai-responses-stream-egress.js.map +1 -0
  60. package/dist/openai-responses.d.ts +15 -0
  61. package/dist/openai-responses.d.ts.map +1 -0
  62. package/dist/openai-responses.js +363 -0
  63. package/dist/openai-responses.js.map +1 -0
  64. package/dist/provider-source.d.ts +20 -0
  65. package/dist/provider-source.d.ts.map +1 -0
  66. package/dist/provider-source.js +41 -0
  67. package/dist/provider-source.js.map +1 -0
  68. package/dist/provider-transport.d.ts +5 -0
  69. package/dist/provider-transport.d.ts.map +1 -0
  70. package/dist/provider-transport.js +114 -0
  71. package/dist/provider-transport.js.map +1 -0
  72. package/dist/runtime-v2-validation.d.ts +16 -0
  73. package/dist/runtime-v2-validation.d.ts.map +1 -0
  74. package/dist/runtime-v2-validation.js +915 -0
  75. package/dist/runtime-v2-validation.js.map +1 -0
  76. package/dist/runtime-v2.d.ts +462 -0
  77. package/dist/runtime-v2.d.ts.map +1 -0
  78. package/dist/runtime-v2.js +56 -0
  79. package/dist/runtime-v2.js.map +1 -0
  80. package/dist/tokenforge-task-projection.d.ts +8 -0
  81. package/dist/tokenforge-task-projection.d.ts.map +1 -0
  82. package/dist/tokenforge-task-projection.js +247 -0
  83. package/dist/tokenforge-task-projection.js.map +1 -0
  84. package/dist/tokenforge-tasks.d.ts +241 -0
  85. package/dist/tokenforge-tasks.d.ts.map +1 -0
  86. package/dist/tokenforge-tasks.js +1132 -0
  87. package/dist/tokenforge-tasks.js.map +1 -0
  88. package/dist/usage-reservation.d.ts +11 -0
  89. package/dist/usage-reservation.d.ts.map +1 -0
  90. package/dist/usage-reservation.js +40 -0
  91. package/dist/usage-reservation.js.map +1 -0
  92. package/dist/validation.d.ts +22 -0
  93. package/dist/validation.d.ts.map +1 -0
  94. package/dist/validation.js +415 -0
  95. package/dist/validation.js.map +1 -0
  96. package/dist/video-ingress.d.ts +31 -0
  97. package/dist/video-ingress.d.ts.map +1 -0
  98. package/dist/video-ingress.js +246 -0
  99. package/dist/video-ingress.js.map +1 -0
  100. package/dist/volc-video-task-codec.d.ts +59 -0
  101. package/dist/volc-video-task-codec.d.ts.map +1 -0
  102. package/dist/volc-video-task-codec.js +123 -0
  103. package/dist/volc-video-task-codec.js.map +1 -0
  104. package/package.json +54 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 TokenForge Contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # @skyold/model-protocol
2
+
3
+ Normalized public contracts shared by TokenForge Protocol Engine, Provider
4
+ Adapters, and embedding Hosts.
5
+
6
+ The package includes versioned task and capability facts, Transport ports,
7
+ normalized results and stream events, usage facts, TokenForge Task decoding,
8
+ and asynchronous Provider job contracts. It contains no credentials, network
9
+ client, database, routing policy, or billing implementation.
10
+
11
+ Import only from the package root:
12
+
13
+ ```ts
14
+ import type {
15
+ ProviderTransport,
16
+ ResolvedProviderTarget,
17
+ TaskEnvelopeV2,
18
+ } from '@skyold/model-protocol';
19
+ ```
20
+
21
+ See the repository [usage guide](../../docs/guides/PROTOCOL_ENGINE.md) for the
22
+ complete integration flow.
@@ -0,0 +1,17 @@
1
+ # @skyold/model-protocol
2
+
3
+ TokenForge Protocol Engine、Provider Adapter 与接入 Host 共享的规范化公共契约。
4
+
5
+ 该包包含版本化 task 与 capability 事实、Transport 端口、统一 result 与 stream event、usage、TokenForge Task 解码和异步 Provider job 契约;不包含凭证、网络客户端、数据库、选路策略或计费实现。
6
+
7
+ 只从包根导入:
8
+
9
+ ```ts
10
+ import type {
11
+ ProviderTransport,
12
+ ResolvedProviderTarget,
13
+ TaskEnvelopeV2,
14
+ } from '@skyold/model-protocol';
15
+ ```
16
+
17
+ 完整接入流程见仓库[使用指南](../../docs/guides/PROTOCOL_ENGINE.md)。
@@ -0,0 +1,12 @@
1
+ import type { ChatOutputV1, NormalizedUsageV1 } from './runtime-v2.js';
2
+ export declare function encodeAnthropicMessagesEgressV1(output: ChatOutputV1, usage: NormalizedUsageV1 | undefined, model: string): Record<string, unknown>;
3
+ export interface OpenAiChatEgressOptionsV1 {
4
+ readonly responseId?: string;
5
+ readonly model?: string;
6
+ readonly createdUnixSeconds?: number;
7
+ }
8
+ /** Decode a TokenForge/OpenAI-compatible Host response for internal consumers. */
9
+ export declare function decodeOpenAiChatHostEgressTextV1(body: unknown): string;
10
+ export declare function encodeChatAssistantTextEgressV1(output: ChatOutputV1, protocol: 'anthropic' | 'openai'): string | null;
11
+ export declare function encodeOpenAiChatEgressV1(output: ChatOutputV1, usage: NormalizedUsageV1 | undefined, options?: OpenAiChatEgressOptionsV1): Record<string, unknown>;
12
+ //# sourceMappingURL=chat-egress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-egress.d.ts","sourceRoot":"","sources":["../src/chat-egress.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEvE,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,iBAAiB,GAAG,SAAS,EACpC,KAAK,EAAE,MAAM,GACZ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAwBzB;AAED,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC;CACtC;AAED,kFAAkF;AAClF,wBAAgB,gCAAgC,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAetE;AAED,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,YAAY,EACpB,QAAQ,EAAE,WAAW,GAAG,QAAQ,GAC/B,MAAM,GAAG,IAAI,CASf;AAED,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,iBAAiB,GAAG,SAAS,EACpC,OAAO,GAAE,yBAA8B,GACtC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAwCzB"}
@@ -0,0 +1,144 @@
1
+ export function encodeAnthropicMessagesEgressV1(output, usage, model) {
2
+ const content = [];
3
+ for (const item of output.items) {
4
+ if (item.type === 'text' || item.type === 'refusal') {
5
+ content.push({ type: 'text', text: item.text });
6
+ }
7
+ else {
8
+ content.push({
9
+ type: 'tool_use',
10
+ id: item.callId,
11
+ name: item.name,
12
+ input: safeJsonObject(item.arguments),
13
+ });
14
+ }
15
+ }
16
+ return {
17
+ id: 'msg_tokenforge',
18
+ type: 'message',
19
+ role: 'assistant',
20
+ model,
21
+ content,
22
+ stop_reason: anthropicFinishReason(output.finishReason),
23
+ stop_sequence: null,
24
+ usage: anthropicUsage(usage),
25
+ };
26
+ }
27
+ /** Decode a TokenForge/OpenAI-compatible Host response for internal consumers. */
28
+ export function decodeOpenAiChatHostEgressTextV1(body) {
29
+ if (!isRecord(body))
30
+ throw new Error('OPENAI_CHAT_HOST_EGRESS_INVALID');
31
+ const choices = body['choices'];
32
+ if (!Array.isArray(choices) || choices.length === 0) {
33
+ throw new Error('OPENAI_CHAT_HOST_EGRESS_INVALID');
34
+ }
35
+ const first = choices[0];
36
+ if (!isRecord(first) || !isRecord(first['message'])) {
37
+ throw new Error('OPENAI_CHAT_HOST_EGRESS_INVALID');
38
+ }
39
+ const content = first['message']['content'];
40
+ if (typeof content !== 'string' || content.length === 0) {
41
+ throw new Error('OPENAI_CHAT_HOST_EGRESS_TEXT_REQUIRED');
42
+ }
43
+ return content;
44
+ }
45
+ export function encodeChatAssistantTextEgressV1(output, protocol) {
46
+ const text = output.items
47
+ .filter((item) => item.type === 'text' || item.type === 'refusal')
48
+ .map((item) => item.text)
49
+ .join(protocol === 'anthropic' ? '\n' : '');
50
+ return text || null;
51
+ }
52
+ export function encodeOpenAiChatEgressV1(output, usage, options = {}) {
53
+ const text = encodeChatAssistantTextEgressV1(output, 'openai');
54
+ const toolCalls = output.items
55
+ .filter((item) => item.type === 'function_call')
56
+ .map((item) => ({
57
+ id: item.callId,
58
+ type: 'function',
59
+ function: { name: item.name, arguments: item.arguments },
60
+ }));
61
+ return {
62
+ id: options.responseId ?? 'chatcmpl_tokenforge',
63
+ object: 'chat.completion',
64
+ ...(options.createdUnixSeconds === undefined ? {} : { created: options.createdUnixSeconds }),
65
+ ...(options.model === undefined ? {} : { model: options.model }),
66
+ choices: [
67
+ {
68
+ index: 0,
69
+ message: {
70
+ role: 'assistant',
71
+ content: text,
72
+ ...(toolCalls.length === 0 ? {} : { tool_calls: toolCalls }),
73
+ },
74
+ finish_reason: openAiFinishReason(output.finishReason),
75
+ },
76
+ ],
77
+ usage: {
78
+ prompt_tokens: usage?.inputTokens ?? 0,
79
+ completion_tokens: usage?.outputTokens ?? 0,
80
+ total_tokens: usage?.totalTokens ?? (usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0),
81
+ ...(usage?.cachedInputTokens === undefined
82
+ ? {}
83
+ : { prompt_tokens_details: { cached_tokens: usage.cachedInputTokens } }),
84
+ ...(usage?.reasoningOutputTokens === undefined
85
+ ? {}
86
+ : { completion_tokens_details: { reasoning_tokens: usage.reasoningOutputTokens } }),
87
+ },
88
+ };
89
+ }
90
+ function openAiFinishReason(reason) {
91
+ if (reason === 'end_turn')
92
+ return 'stop';
93
+ if (reason === 'tool_use')
94
+ return 'tool_calls';
95
+ if (reason === 'max_tokens')
96
+ return 'length';
97
+ return reason;
98
+ }
99
+ function anthropicFinishReason(reason) {
100
+ if (reason === 'stop')
101
+ return 'end_turn';
102
+ if (reason === 'tool_calls')
103
+ return 'tool_use';
104
+ if (reason === 'length')
105
+ return 'max_tokens';
106
+ return reason;
107
+ }
108
+ function anthropicUsage(usage) {
109
+ const hasCacheCreationBreakdown = usage?.cacheCreation5mInputTokens !== undefined ||
110
+ usage?.cacheCreation1hInputTokens !== undefined;
111
+ return {
112
+ input_tokens: usage?.inputTokens ?? 0,
113
+ output_tokens: usage?.outputTokens ?? 0,
114
+ ...(usage?.cacheCreationInputTokens === undefined
115
+ ? {}
116
+ : { cache_creation_input_tokens: usage.cacheCreationInputTokens }),
117
+ ...(usage?.cachedInputTokens === undefined
118
+ ? {}
119
+ : { cache_read_input_tokens: usage.cachedInputTokens }),
120
+ ...(hasCacheCreationBreakdown
121
+ ? {
122
+ cache_creation: {
123
+ ephemeral_5m_input_tokens: usage?.cacheCreation5mInputTokens ?? 0,
124
+ ephemeral_1h_input_tokens: usage?.cacheCreation1hInputTokens ?? 0,
125
+ },
126
+ }
127
+ : {}),
128
+ };
129
+ }
130
+ function safeJsonObject(value) {
131
+ try {
132
+ const parsed = JSON.parse(value);
133
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
134
+ ? parsed
135
+ : {};
136
+ }
137
+ catch {
138
+ return {};
139
+ }
140
+ }
141
+ function isRecord(value) {
142
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
143
+ }
144
+ //# sourceMappingURL=chat-egress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-egress.js","sourceRoot":"","sources":["../src/chat-egress.ts"],"names":[],"mappings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,EAAE,KAAK,CAAC,iBAAiB,EAAE,CAAC;QACzD,GAAG,CAAC,yBAAyB;YAC3B,CAAC,CAAC;gBACE,cAAc,EAAE;oBACd,yBAAyB,EAAE,KAAK,EAAE,0BAA0B,IAAI,CAAC;oBACjE,yBAAyB,EAAE,KAAK,EAAE,0BAA0B,IAAI,CAAC;iBAClE;aACF;YACH,CAAC,CAAC,EAAE,CAAC;KACR,CAAC;AACJ,CAAC;AAED,SAAS,cAAc,CAAC,KAAa;IACnC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAY,CAAC;QAC5C,OAAO,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;YACnE,CAAC,CAAE,MAAkC;YACrC,CAAC,CAAC,EAAE,CAAC;IACT,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,39 @@
1
+ import type { ProtocolFamily } from './contracts.js';
2
+ import type { DecodedTaskV1 } from './runtime-v2.js';
3
+ declare const OPENAI_CHAT_PROTOCOL: "openai-chat/v1";
4
+ declare const ANTHROPIC_MESSAGES_PROTOCOL: "anthropic-messages/v1";
5
+ export type ChatIngressProtocolFamilyV1 = typeof OPENAI_CHAT_PROTOCOL | typeof ANTHROPIC_MESSAGES_PROTOCOL;
6
+ export type ChatIngressRequestErrorCodeV1 = 'CHAT_INGRESS_BODY_REQUIRED' | 'CHAT_INGRESS_MODEL_REQUIRED' | 'CHAT_INGRESS_PROTOCOL_UNSUPPORTED' | 'OPENAI_CHAT_REQUEST_UNSUPPORTED_FIELD' | 'OPENAI_CHAT_REQUEST_INVALID' | 'ANTHROPIC_MESSAGES_REQUEST_UNSUPPORTED_FIELD' | 'ANTHROPIC_MESSAGES_REQUEST_INVALID';
7
+ export declare class ChatIngressRequestError extends Error {
8
+ readonly code: ChatIngressRequestErrorCodeV1;
9
+ readonly name = "ChatIngressRequestError";
10
+ constructor(code: ChatIngressRequestErrorCodeV1);
11
+ }
12
+ export interface TextChatIngressRequestV1 {
13
+ readonly protocolFamily: ChatIngressProtocolFamilyV1;
14
+ readonly path: '/v1/chat/completions' | '/v1/messages';
15
+ readonly body: Readonly<Record<string, unknown>>;
16
+ readonly decoded: DecodedTaskV1;
17
+ }
18
+ export interface OpenAiChatInvocationV1 {
19
+ readonly model: string;
20
+ readonly input: unknown;
21
+ readonly parameters?: Readonly<Record<string, unknown>>;
22
+ readonly stream: boolean;
23
+ }
24
+ /** Decode native Chat ingress before Host target, credential, or policy resolution. */
25
+ export declare function decodeChatIngressRequestV1(body: unknown, protocolFamily: ProtocolFamily): DecodedTaskV1;
26
+ /** Decode the legacy split messages/parameters invocation without Host wire reconstruction. */
27
+ export declare function decodeOpenAiChatInvocationV1(input: OpenAiChatInvocationV1): DecodedTaskV1;
28
+ /** Build the common system+user text invocation used by embedded Host features. */
29
+ export declare function createTextChatIngressRequestV1(input: {
30
+ readonly protocolFamily: ChatIngressProtocolFamilyV1;
31
+ readonly model: string;
32
+ readonly system: string;
33
+ readonly user: string;
34
+ readonly maxOutputTokens: number;
35
+ }): TextChatIngressRequestV1;
36
+ /** Shared request validator used by ingress Codecs and outbound Adapters. */
37
+ export declare function validateChatProviderInputV1(input: Readonly<Record<string, unknown>>, protocolFamily: ChatIngressProtocolFamilyV1): void;
38
+ export {};
39
+ //# sourceMappingURL=chat-ingress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-ingress.d.ts","sourceRoot":"","sources":["../src/chat-ingress.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,C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