@ebony-vinyl/dsh-our-free-model 2.7.4 → 2.8.2

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 (44) hide show
  1. package/README.md +23 -8
  2. package/client.js +894 -6
  3. package/cordis.patch.yml +1 -1
  4. package/index.js +248 -11
  5. package/package.json +6 -2
  6. package/src/adapter.js +54 -6
  7. package/src/aggregate.js +53 -13
  8. package/src/bun-fetch.js +33 -10
  9. package/src/catalog.js +238 -58
  10. package/src/core.js +838 -0
  11. package/src/egress.js +204 -3
  12. package/src/engine-parts/win32-x64.000.part +0 -0
  13. package/src/engine-parts/win32-x64.001.part +0 -0
  14. package/src/engine-parts/win32-x64.002.part +0 -0
  15. package/src/engine-parts/win32-x64.003.part +0 -0
  16. package/src/engine-parts/win32-x64.004.part +0 -0
  17. package/src/engine-parts/win32-x64.005.part +0 -0
  18. package/src/engine-parts/win32-x64.006.part +0 -0
  19. package/src/extensions.js +1888 -0
  20. package/src/feed.js +6 -0
  21. package/src/http.js +15 -2
  22. package/src/kilo.js +55 -5
  23. package/src/magpie-engine.js +61 -2
  24. package/src/magpie-models.js +31 -3
  25. package/src/model-test.js +3 -1
  26. package/src/o2a.js +956 -0
  27. package/src/open-external.js +43 -5
  28. package/src/plugins.js +1240 -0
  29. package/src/providers/registry.json +72 -30
  30. package/src/semver.js +40 -0
  31. package/src/store.js +17 -1
  32. package/src/stream.js +39 -4
  33. package/src/subscription-completion.js +12 -3
  34. package/src/updater.js +5 -33
  35. package/src/upstream.js +283 -363
  36. package/vendor/channel-pack/NOTICE.md +1 -1
  37. package/vendor/channel-pack/pack.js +7242 -3952
  38. package/vendor/magpie/NOTICE.md +159 -6
  39. package/vendor/magpie/engine.json +58 -27
  40. package/src/core/completion.js +0 -196
  41. package/src/core/runtime.js +0 -456
  42. package/src/core/stats.js +0 -98
  43. package/src/engine-parts/win32-x64.007.part +0 -0
  44. package/src/engine-parts/win32-x64.008.part +0 -0
@@ -1,17 +1,22 @@
1
1
  # Magpie engine notices
2
2
 
3
3
  Upstream: https://github.com/yetone/magpie.git
4
- Pinned revision: c551a229975653b7ae5b6a35e2278c983a272e06
5
- Source build version: ofm-c551a2299756
6
-
7
- Built without upstream modifications using the nogui tag. This excludes the native
8
- desktop shell and retains the complete embedded web UI and built-in plugins.
4
+ Pinned revision: 6a900e0d048e592cbc9120dd2c3f515aab9ef04a
5
+ Source build version: ofm-6a900e0d048e
6
+
7
+ Built with 1 guard patch applied on top of the pinned
8
+ revision (0001-agent-sync-skip-an-empty-catalog.patch in scripts/magpie-engine-patches): an agent
9
+ sync that finds an empty catalog skips the write instead of emptying the
10
+ agent's own model list — a provider-less engine must never wipe a wired
11
+ agent's picker. Everything else is built without upstream modifications
12
+ using the nogui tag, which excludes the native desktop shell and retains
13
+ the complete embedded web UI and built-in plugins.
9
14
  The owner disables automatic binary updates; upgrades go through this builder.
10
15
  The upstream MIT license is reproduced in LICENSE. Go dependency license and
11
16
  notice files below are copied verbatim from the modules linked into this build.
12
17
  The Go runtime license is included as well. Build inputs and hashes are in engine.json.
13
18
 
14
- ## github.com/Calcium-Ion/moejs v0.1.0-alpha.4
19
+ ## github.com/MoeForge/moejs v0.1.0-alpha.11
15
20
 
16
21
  ### LICENSE
17
22
 
@@ -594,6 +599,26 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
594
599
  SOFTWARE.
595
600
  ```
596
601
 
602
+ ## github.com/coder/websocket v1.8.14
603
+
604
+ ### LICENSE.txt
605
+
606
+ ```text
607
+ Copyright (c) 2025 Coder
608
+
609
+ Permission to use, copy, modify, and distribute this software for any
610
+ purpose with or without fee is hereby granted, provided that the above
611
+ copyright notice and this permission notice appear in all copies.
612
+
613
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
614
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
615
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
616
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
617
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
618
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
619
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
620
+ ```
621
+
597
622
  ## github.com/dustin/go-humanize v1.0.1
598
623
 
599
624
  ### LICENSE
@@ -650,6 +675,100 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
650
675
  SOFTWARE.
651
676
  ```
652
677
 
678
+ ## github.com/fsnotify/fsnotify v1.9.0
679
+
680
+ ### LICENSE
681
+
682
+ ```text
683
+ Copyright © 2012 The Go Authors. All rights reserved.
684
+ Copyright © fsnotify Authors. All rights reserved.
685
+
686
+ Redistribution and use in source and binary forms, with or without modification,
687
+ are permitted provided that the following conditions are met:
688
+
689
+ * Redistributions of source code must retain the above copyright notice, this
690
+ list of conditions and the following disclaimer.
691
+ * Redistributions in binary form must reproduce the above copyright notice, this
692
+ list of conditions and the following disclaimer in the documentation and/or
693
+ other materials provided with the distribution.
694
+ * Neither the name of Google Inc. nor the names of its contributors may be used
695
+ to endorse or promote products derived from this software without specific
696
+ prior written permission.
697
+
698
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
699
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
700
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
701
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
702
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
703
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
704
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
705
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
706
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
707
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
708
+ ```
709
+
710
+ ## github.com/go-ole/go-ole v1.3.0
711
+
712
+ ### LICENSE
713
+
714
+ ```text
715
+ The MIT License (MIT)
716
+
717
+ Copyright © 2013-2017 Yasuhiro Matsumoto, <mattn.jp@gmail.com>
718
+
719
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
720
+ this software and associated documentation files (the “Software”), to deal in
721
+ the Software without restriction, including without limitation the rights to
722
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
723
+ of the Software, and to permit persons to whom the Software is furnished to do
724
+ so, subject to the following conditions:
725
+
726
+ The above copyright notice and this permission notice shall be included in all
727
+ copies or substantial portions of the Software.
728
+
729
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
730
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
731
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
732
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
733
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
734
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
735
+ SOFTWARE.
736
+ ```
737
+
738
+ ## github.com/google/uuid v1.6.0
739
+
740
+ ### LICENSE
741
+
742
+ ```text
743
+ Copyright (c) 2009,2014 Google Inc. All rights reserved.
744
+
745
+ Redistribution and use in source and binary forms, with or without
746
+ modification, are permitted provided that the following conditions are
747
+ met:
748
+
749
+ * Redistributions of source code must retain the above copyright
750
+ notice, this list of conditions and the following disclaimer.
751
+ * Redistributions in binary form must reproduce the above
752
+ copyright notice, this list of conditions and the following disclaimer
753
+ in the documentation and/or other materials provided with the
754
+ distribution.
755
+ * Neither the name of Google Inc. nor the names of its
756
+ contributors may be used to endorse or promote products derived from
757
+ this software without specific prior written permission.
758
+
759
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
760
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
761
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
762
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
763
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
764
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
765
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
766
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
767
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
768
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
769
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
770
+ ```
771
+
653
772
  ## github.com/klauspost/compress v1.18.3
654
773
 
655
774
  ### LICENSE
@@ -1784,6 +1903,40 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1784
1903
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1785
1904
  ```
1786
1905
 
1906
+ ## golang.org/x/image v0.43.0
1907
+
1908
+ ### LICENSE
1909
+
1910
+ ```text
1911
+ Copyright 2009 The Go Authors.
1912
+
1913
+ Redistribution and use in source and binary forms, with or without
1914
+ modification, are permitted provided that the following conditions are
1915
+ met:
1916
+
1917
+ * Redistributions of source code must retain the above copyright
1918
+ notice, this list of conditions and the following disclaimer.
1919
+ * Redistributions in binary form must reproduce the above
1920
+ copyright notice, this list of conditions and the following disclaimer
1921
+ in the documentation and/or other materials provided with the
1922
+ distribution.
1923
+ * Neither the name of Google LLC nor the names of its
1924
+ contributors may be used to endorse or promote products derived from
1925
+ this software without specific prior written permission.
1926
+
1927
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1928
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1929
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1930
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1931
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1932
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1933
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1934
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1935
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1936
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1937
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1938
+ ```
1939
+
1787
1940
  ## golang.org/x/net v0.56.0
1788
1941
 
1789
1942
  ### LICENSE
@@ -2,8 +2,8 @@
2
2
  "schemaVersion": 1,
3
3
  "name": "magpie",
4
4
  "upstream": "https://github.com/yetone/magpie.git",
5
- "revision": "c551a229975653b7ae5b6a35e2278c983a272e06",
6
- "version": "ofm-c551a2299756",
5
+ "revision": "6a900e0d048e592cbc9120dd2c3f515aab9ef04a",
6
+ "version": "ofm-6a900e0d048e",
7
7
  "license": "MIT",
8
8
  "build": {
9
9
  "command": [
@@ -14,65 +14,61 @@
14
14
  "-trimpath",
15
15
  "-buildvcs=true",
16
16
  "-ldflags",
17
- "-X main.version=ofm-c551a2299756",
17
+ "-s -w -X main.version=ofm-6a900e0d048e",
18
18
  "."
19
19
  ],
20
20
  "go": "go version go1.27.1 windows/amd64"
21
21
  },
22
+ "patches": [
23
+ {
24
+ "name": "0001-agent-sync-skip-an-empty-catalog.patch",
25
+ "sha256": "50acab57addbd3821a6a293f2a6be4f496b4fb4a8e118b3cfd224f29e83747aa"
26
+ }
27
+ ],
22
28
  "binaries": {
23
29
  "win32-x64": {
24
30
  "path": "bin/magpie-win32-x64.exe",
25
- "sha256": "6490f7ed2c5f001adc97284276af453f2a2b1711712e175e6fc780e09046fa3b",
31
+ "sha256": "5fafc3a970d9144fedd21820c6afec55b86c8206f916498144c3611ec9795bc2",
26
32
  "compressed": {
27
33
  "algorithm": "gzip",
28
- "size": 35128000,
29
- "sha256": "5964bf859e7de297bdc755d9fb4d540b816df87efd6fd28bbf9e1103c326730e",
30
- "outputSize": 68578304,
34
+ "size": 27224805,
35
+ "sha256": "086f69612914773fa96749bea7ddf4a7b319ff8e3e7404df14a298d69221bcb5",
36
+ "outputSize": 64812032,
31
37
  "parts": [
32
38
  {
33
39
  "path": "src/engine-parts/win32-x64.000.part",
34
40
  "size": 4194304,
35
- "sha256": "ee492eaa5d992b8703ede29821e07b4bd46a9ff0395faba2128211e1363fa4b9"
41
+ "sha256": "e9848284c36cdbe9b7c33b6d6dadbc351c1cf1d2dda75bae334d09efa34821c3"
36
42
  },
37
43
  {
38
44
  "path": "src/engine-parts/win32-x64.001.part",
39
45
  "size": 4194304,
40
- "sha256": "141c520cf249a6930618c198c6072ccf04fab7edee93ab4f28da41c4619e61aa"
46
+ "sha256": "001b67dde65246e33a1fb285eb299a816126db1168d9ee8459f59b25a22536d1"
41
47
  },
42
48
  {
43
49
  "path": "src/engine-parts/win32-x64.002.part",
44
50
  "size": 4194304,
45
- "sha256": "854178a956532bdfc7b6eee89d179c6a5ea5a29fef9a0a2d19f396938ca93fe7"
51
+ "sha256": "80d3425897688eb84c797f70a00fae1c347a68e2d42b8963f8969eede24acfd9"
46
52
  },
47
53
  {
48
54
  "path": "src/engine-parts/win32-x64.003.part",
49
55
  "size": 4194304,
50
- "sha256": "37740d6058361640c8a46cbcb26576ed88ba41f99c6162a495475828d5c7bff1"
56
+ "sha256": "29c1553354b563cc322e270b1b1a062fb8375ef484ef9bd855ee731c85435b77"
51
57
  },
52
58
  {
53
59
  "path": "src/engine-parts/win32-x64.004.part",
54
60
  "size": 4194304,
55
- "sha256": "228b98336fef117087c55d5ab85cc972cc42f61ea1d4b19ced145ac408723525"
61
+ "sha256": "4a436e1d813407269d29291d349ec345c71532f18b9c7cd6fab3c770190fb336"
56
62
  },
57
63
  {
58
64
  "path": "src/engine-parts/win32-x64.005.part",
59
65
  "size": 4194304,
60
- "sha256": "4c723b29f4d69861fd29751951e7c480ae35ad981489b406cb0765b78cc5a26f"
66
+ "sha256": "105729c3b79ef03cb5d8af809b331ba901bfd57fda2e417a0104753c4186ed4a"
61
67
  },
62
68
  {
63
69
  "path": "src/engine-parts/win32-x64.006.part",
64
- "size": 4194304,
65
- "sha256": "397e3c199a0ed6ef6ffdf7a9de62cc7467f5583a2a5ecef3d5867cde9d14ce86"
66
- },
67
- {
68
- "path": "src/engine-parts/win32-x64.007.part",
69
- "size": 4194304,
70
- "sha256": "18cb47f20ab54d013aaf9f7471695bb5a6e7ef5b8ed39a088c61a63a8ce73132"
71
- },
72
- {
73
- "path": "src/engine-parts/win32-x64.008.part",
74
- "size": 1573568,
75
- "sha256": "4a0877e8067b71b5968304008afeb8b96fd578ac581a52e2281df45b95e0d3ce"
70
+ "size": 2058981,
71
+ "sha256": "e21f28935ce2fa13dad0b3746a3799db6340346d4fc35fd21f6b20f00c0f36d7"
76
72
  }
77
73
  ]
78
74
  }
@@ -80,8 +76,8 @@
80
76
  },
81
77
  "modules": [
82
78
  {
83
- "path": "github.com/Calcium-Ion/moejs",
84
- "version": "v0.1.0-alpha.4",
79
+ "path": "github.com/MoeForge/moejs",
80
+ "version": "v0.1.0-alpha.11",
85
81
  "licenses": [
86
82
  "LICENSE"
87
83
  ]
@@ -171,6 +167,13 @@
171
167
  "LICENSE"
172
168
  ]
173
169
  },
170
+ {
171
+ "path": "github.com/coder/websocket",
172
+ "version": "v1.8.14",
173
+ "licenses": [
174
+ "LICENSE.txt"
175
+ ]
176
+ },
174
177
  {
175
178
  "path": "github.com/dustin/go-humanize",
176
179
  "version": "v1.0.1",
@@ -185,6 +188,27 @@
185
188
  "LICENSE"
186
189
  ]
187
190
  },
191
+ {
192
+ "path": "github.com/fsnotify/fsnotify",
193
+ "version": "v1.9.0",
194
+ "licenses": [
195
+ "LICENSE"
196
+ ]
197
+ },
198
+ {
199
+ "path": "github.com/go-ole/go-ole",
200
+ "version": "v1.3.0",
201
+ "licenses": [
202
+ "LICENSE"
203
+ ]
204
+ },
205
+ {
206
+ "path": "github.com/google/uuid",
207
+ "version": "v1.6.0",
208
+ "licenses": [
209
+ "LICENSE"
210
+ ]
211
+ },
188
212
  {
189
213
  "path": "github.com/klauspost/compress",
190
214
  "version": "v1.18.3",
@@ -317,6 +341,13 @@
317
341
  "LICENSE"
318
342
  ]
319
343
  },
344
+ {
345
+ "path": "golang.org/x/image",
346
+ "version": "v0.43.0",
347
+ "licenses": [
348
+ "LICENSE"
349
+ ]
350
+ },
320
351
  {
321
352
  "path": "golang.org/x/net",
322
353
  "version": "v0.56.0",
@@ -1,196 +0,0 @@
1
- import { ROUTE_MAIN, ROUTE_REGION } from '../adapter.js'
2
- import { toOpenAiUsage } from '../forward.js'
3
- import { carryFailure } from '../failure.js'
4
- import { effortsFor, defaultEffortFor } from '../effort.js'
5
-
6
- export function routableModelIds(snapshot) {
7
- const membership = new Set(snapshot.membership[ROUTE_MAIN] ?? [])
8
- if (snapshot.settings.exposeRegionModels !== false) {
9
- for (const id of snapshot.membership[ROUTE_REGION] ?? []) membership.add(id)
10
- }
11
- return membership
12
- }
13
-
14
- export function publicModelRows(snapshot) {
15
- const membership = routableModelIds(snapshot)
16
- return snapshot.catalog
17
- .filter(entry => membership.has(entry.id))
18
- .map(entry => {
19
- const efforts = effortsFor(entry, undefined, snapshot.settings.defaultMaxTokens)
20
- return {
21
- id: entry.id, object: 'model',
22
- created: Math.floor(Date.now() / 1000), owned_by: 'our-free-model',
23
- ...entry.contextWindow === undefined ? {} : { context_window: entry.contextWindow },
24
- ...efforts === undefined ? {} : {
25
- x_ofm_efforts: efforts.map(effort => effort.id),
26
- x_ofm_effort_default: defaultEffortFor(entry),
27
- },
28
- }
29
- })
30
- }
31
-
32
- /** 固定本次请求的模型和配置快照,两个入口使用同一条推理链路。 */
33
- export function createForwardCompletion({ adapter, state, signal }) {
34
- return async (request, onChunk) => {
35
- if (signal?.aborted) throw Object.assign(new Error('model runtime is stopped'), { statusCode: 503 })
36
- const snapshot = state()
37
- const entry = snapshot.catalog.find(candidate => candidate.id === request.model)
38
- if (entry === undefined || !routableModelIds(snapshot).has(entry.id)) {
39
- throw Object.assign(new Error(`model "${request.model}" not found`), { statusCode: 404 })
40
- }
41
- const openAi = request.openAi ?? {}
42
- // Two callers converge here. The OpenAI/Responses listener hands over a raw
43
- // body; the Anthropic listener hands over harness messages it already
44
- // projected through `fromAnthropicRequest`, because re-spelling them as
45
- // OpenAI blocks and back would lose thinking blocks and block ordering. Both
46
- // shapes end at the same adapter call, so there is exactly one place that
47
- // decides which model runs.
48
- const messages = Array.isArray(request.messages)
49
- ? request.messages
50
- : fromOpenAiMessages(openAi, request.responses === true, entry.id)
51
- const tools = Array.isArray(request.tools)
52
- ? request.tools
53
- : (openAi.tools ?? []).map(normalizeTool).filter(Boolean)
54
- const handler = typeof onChunk === 'function' ? onChunk : () => {}
55
- const outcome = { text: '', toolCalls: [], usage: undefined, truncated: false, error: undefined }
56
- const maxTokens = Number.isFinite(request.maxTokens) ? request.maxTokens
57
- : Number.isFinite(openAi.max_tokens) ? openAi.max_tokens : undefined
58
- const temperature = Number.isFinite(request.temperature) ? request.temperature
59
- : Number.isFinite(openAi.temperature) ? openAi.temperature : undefined
60
- const reasoningEffort = typeof request.reasoningEffort === 'string' ? request.reasoningEffort
61
- : typeof openAi.reasoning_effort === 'string' ? openAi.reasoning_effort : undefined
62
- const toolChoice = request.toolChoice === undefined ? openAi.tool_choice : request.toolChoice
63
- const options = {
64
- provider: ROUTE_MAIN, model: entry.id,
65
- messages,
66
- tools: tools.length > 0 ? tools : undefined,
67
- ...temperature === undefined ? {} : { temperature },
68
- ...maxTokens === undefined ? {} : { maxTokens },
69
- ...reasoningEffort === undefined ? {} : { reasoningEffort },
70
- ...toolChoice === undefined ? {} : { toolChoice },
71
- sessionId: `forward:${String(openAi.user ?? openAi.conversation ?? request.sessionId ?? 'shared')}`,
72
- signal: request.signal && signal ? AbortSignal.any([request.signal, signal]) : request.signal ?? signal,
73
- }
74
- for await (const chunk of adapter.stream(options, entry, snapshot)) {
75
- handler(chunk)
76
- foldForwardOutcome(outcome, chunk)
77
- }
78
- if (outcome.truncated === true) {
79
- outcome.toolCalls = outcome.toolCalls.filter(call => {
80
- try { JSON.parse(call.arguments === '' ? '{}' : call.arguments); return true } catch { return false }
81
- })
82
- }
83
- return outcome
84
- }
85
- }
86
-
87
- export function fromOpenAiMessages(body, isResponses, modelId) {
88
- const out = []
89
- const rows = isResponses
90
- ? normaliseResponsesInput(body.input)
91
- : (Array.isArray(body.messages) ? body.messages : [])
92
- for (const row of rows) {
93
- const role = row.role ?? 'user'
94
- const content = []
95
- if (typeof row.content === 'string') {
96
- if (row.content !== '') content.push({ type: 'text', text: row.content })
97
- } else if (Array.isArray(row.content)) {
98
- for (const part of row.content) {
99
- if (typeof part === 'string') { if (part !== '') content.push({ type: 'text', text: part }); continue }
100
- const text = part?.text ?? part?.input_text ?? part?.output_text
101
- if (typeof text === 'string' && text !== '') content.push({ type: 'text', text })
102
- const image = part?.image_url?.url ?? part?.image_url
103
- if (typeof image === 'string' && image !== '') {
104
- content.push({ type: 'image', attachment: { attachmentId: `url:${image.slice(0, 64)}`, mediaType: 'image/png', bytes: 0, width: 0, height: 0, url: image } })
105
- }
106
- }
107
- }
108
- if (role === 'tool') {
109
- out.push({ role: 'tool', content: [{ type: 'text', text: typeof row.content === 'string' ? row.content : JSON.stringify(row.content ?? '') }], toolCallId: row.tool_call_id ?? '', source: { kind: 'tool', callId: row.tool_call_id ?? '' } })
110
- continue
111
- }
112
- if (role === 'assistant' && Array.isArray(row.tool_calls)) {
113
- for (const call of row.tool_calls) {
114
- content.push({ type: 'tool-call', id: call.id ?? '', name: call.function?.name ?? '', arguments: call.function?.arguments ?? '{}' })
115
- }
116
- }
117
- if (content.length === 0) continue
118
- out.push({
119
- role: role === 'developer' ? 'developer' : role === 'system' ? 'system' : role === 'assistant' ? 'assistant' : 'user',
120
- content,
121
- ...(role === 'assistant' && typeof row.reasoning_content === 'string'
122
- ? { reasoning_content: row.reasoning_content }
123
- : {}),
124
- ...role === 'assistant' ? { source: { kind: 'model', provider: ROUTE_MAIN, model: String(modelId ?? '') } } : {},
125
- })
126
- }
127
- return out
128
- }
129
-
130
- function normaliseResponsesInput(input) {
131
- if (typeof input === 'string') return [{ role: 'user', content: input }]
132
- if (!Array.isArray(input)) return []
133
- return input.map(row => {
134
- if (typeof row === 'string') return { role: 'user', content: row }
135
- if (row.type === 'function_call') return { role: 'assistant', content: [], tool_calls: [{ id: row.call_id, function: { name: row.name, arguments: row.arguments } }] }
136
- if (row.type === 'function_call_output') return { role: 'tool', content: String(row.output ?? ''), tool_call_id: row.call_id }
137
- return row
138
- })
139
- }
140
-
141
- function normalizeTool(tool) {
142
- const name = tool?.name ?? tool?.function?.name
143
- if (typeof name !== 'string' || name.trim() === '') return null
144
- const parameters = tool?.parameters ?? tool?.function?.parameters ?? { type: 'object', properties: {} }
145
- return { name, description: String(tool?.description ?? tool?.function?.description ?? ''), parameters }
146
- }
147
-
148
- export function foldForwardOutcome(outcome, chunk) {
149
- switch (chunk.type) {
150
- case 'text-delta': outcome.text += chunk.text; break
151
- case 'tool-call-delta': {
152
- let call = outcome.toolCalls.find(candidate => candidate.slot === chunk.index)
153
- if (call === undefined) { call = { slot: chunk.index, id: chunk.id ?? '', name: chunk.name ?? '', arguments: chunk.argumentsDelta ?? '' }; outcome.toolCalls.push(call) }
154
- else call.arguments += chunk.argumentsDelta ?? ''
155
- if (chunk.name) call.name = chunk.name
156
- if (chunk.id) call.id = chunk.id
157
- break
158
- }
159
- case 'block-end':
160
- if (chunk.block?.type === 'tool-call') {
161
- // Match by slot first: the call's deltas were folded under this same
162
- // block index, and a relayed stream can carry its id late — matching
163
- // by id alone would miss the slot's entry and fold one call into two.
164
- // The block-end carries the assembled arguments, so a slot match
165
- // adopts them; an id match only fills identity fields.
166
- const bySlot = outcome.toolCalls.find(candidate => candidate.slot === chunk.index)
167
- if (bySlot !== undefined) {
168
- if (typeof chunk.block.id === 'string' && chunk.block.id !== '') bySlot.id = chunk.block.id
169
- if (typeof chunk.block.name === 'string' && chunk.block.name !== '') bySlot.name = chunk.block.name
170
- if (typeof chunk.block.arguments === 'string') bySlot.arguments = chunk.block.arguments
171
- break
172
- }
173
- const byId = outcome.toolCalls.find(candidate => typeof candidate.id === 'string' && candidate.id !== ''
174
- && candidate.id === chunk.block.id)
175
- if (byId === undefined) {
176
- outcome.toolCalls.push({
177
- slot: chunk.index,
178
- id: typeof chunk.block.id === 'string' ? chunk.block.id : '',
179
- name: typeof chunk.block.name === 'string' ? chunk.block.name : '',
180
- arguments: typeof chunk.block.arguments === 'string' ? chunk.block.arguments : '',
181
- })
182
- }
183
- }
184
- break
185
- case 'usage': outcome.usage = toOpenAiUsage(chunk.usage); break
186
- case 'finish':
187
- if (chunk.reason?.kind === 'max-tokens') outcome.truncated = true
188
- if (chunk.reason?.kind === 'error' || chunk.reason?.kind === 'aborted') {
189
- outcome.failure = carryFailure(chunk.reason.failure, chunk.reason.kind)
190
- outcome.error = outcome.failure.message
191
- }
192
- break
193
- default: break
194
- }
195
- return outcome
196
- }