-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathbyok-tools.ts
More file actions
1690 lines (1597 loc) · 71.6 KB
/
Copy pathbyok-tools.ts
File metadata and controls
1690 lines (1597 loc) · 71.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Tool definitions and executors exposed to BYOK chat sessions.
//
// Why this file exists: the BYOK chat proxy (e.g. /api/proxy/senseaudio/stream)
// is a thin pass-through that doesn't have the agent-runtime scaffolding the
// CLI agents (Claude Code / Codex / ...) carry. To let users ask their BYOK
// chat to "draw me a cat" and get an actual rendered PNG back, the daemon
// injects an OpenAI-shaped `tools` definition into the upstream completion
// request, then loops on the model's tool_calls: execute → feed the result
// back as a `role: 'tool'` message → re-issue the completion. The chat surface
// stays the same; the tool dispatch happens entirely daemon-side.
//
// Today we ship image, video, and speech tools backed by SenseAudio endpoints,
// since the BYOK chat session already authenticates with the same API key.
import path from 'node:path';
import { writeFile, readFile, readdir, stat } from 'node:fs/promises';
import { randomBytes } from 'node:crypto';
import { assertExternalAssetUrl, assertAndFetchExternalAsset } from './connectionTest.js';
import { resolveProviderConfig } from './media/config.js';
import { IMAGE_MODELS } from './media/models.js';
import { ensureProject } from './projects.js';
import {
AIHUBMIX_DEFAULT_BASE_URL,
aihubmixHeaders,
aihubmixAppCodeHeader,
aihubmixWireModel,
aihubmixOriginFromBase,
aihubmixGeminiImageUrl,
aihubmixGeminiImageBytes,
classifyAIHubMixModel,
AIHUBMIX_IMAGE_ASPECT_TO_SIZE,
} from './integrations/aihubmix.js';
import {
aihubmixMediaRegistry,
buildVideoRequest,
deriveVideoFamily,
type ModelCapability,
} from './media-adapters/index.js';
// SenseAudio image model allowlist — derived from the shared media-models
// registry so adding a new SenseAudio image model in one place (media-models)
// auto-extends the BYOK tool param enum, the Settings dropdown, and the
// daemon-side validation. No drift, no hand-maintained constant.
export const BYOK_SENSEAUDIO_IMAGE_MODELS: readonly string[] = IMAGE_MODELS
.filter((m) => m.provider === 'senseaudio')
.map((m) => m.id);
// Default falls back to the first entry from the registry (today
// `senseaudio-image-2.0-260319` — the multi-aspect latest). Kept as a
// computed constant so re-ordering the registry rotates the default
// without code edits here.
export const BYOK_SENSEAUDIO_DEFAULT_IMAGE_MODEL =
BYOK_SENSEAUDIO_IMAGE_MODELS[0] ?? 'senseaudio-image-2.0-260319';
export function isSenseAudioImageModel(value: unknown): value is string {
return typeof value === 'string' && BYOK_SENSEAUDIO_IMAGE_MODELS.includes(value);
}
// AIHubMix image-model allowlist — same registry-derived pattern as SenseAudio
// so a new `provider: 'aihubmix'` image entry auto-extends the chat tool enum,
// the Settings dropdown, and the daemon-side validation with no hand edits.
export const BYOK_AIHUBMIX_IMAGE_MODELS: readonly string[] = IMAGE_MODELS
.filter((m) => m.provider === 'aihubmix')
.map((m) => m.id);
export const BYOK_AIHUBMIX_DEFAULT_IMAGE_MODEL =
BYOK_AIHUBMIX_IMAGE_MODELS[0] ?? 'aihubmix-gpt-image-1';
export function isAIHubMixImageModel(value: unknown): value is string {
// AIHubMix image models are discovered live (50+), so the static registry
// only seeds a couple. Any `aihubmix-` prefixed id renders through the same
// OpenAI-compatible endpoint (the prefix is stripped to the wire name), so
// accept the whole namespace rather than just the seeded ids.
return typeof value === 'string'
&& (value.startsWith('aihubmix-') || BYOK_AIHUBMIX_IMAGE_MODELS.includes(value));
}
// AIHubMix video models are discovered live (the `?type=video` catalogue), so —
// like image models — we accept the whole `aihubmix-` namespace rather than a
// hand-maintained list. The prefix is stripped to the wire name before the
// `/videos` call. When neither the composer picker nor the LLM supplies one,
// the executor falls back to BYOK_AIHUBMIX_DEFAULT_VIDEO_MODEL.
export function isAIHubMixVideoModel(value: unknown): value is string {
return typeof value === 'string' && value.startsWith('aihubmix-');
}
// Default AIHubMix video model (catalogue id; the `aihubmix-` prefix is stripped
// to the wire name by aihubmixWireModel). Doubao Seedance is the most broadly
// available text/image-to-video model on the gateway today.
export const BYOK_AIHUBMIX_DEFAULT_VIDEO_MODEL = 'aihubmix-doubao-seedance-2-0-fast-260128';
// AIHubMix speech (TTS) models — discovered live via `?type=tts`; like image and
// video we accept the whole `aihubmix-` namespace (prefix stripped to the wire
// name). Falls back to BYOK_AIHUBMIX_DEFAULT_SPEECH_MODEL when unset.
export function isAIHubMixSpeechModel(value: unknown): value is string {
return typeof value === 'string' && value.startsWith('aihubmix-');
}
export const BYOK_AIHUBMIX_DEFAULT_SPEECH_MODEL = 'aihubmix-tts-1';
const AIHUBMIX_DEFAULT_TTS_VOICE = 'alloy';
// AIHubMix video knobs for the chat `generate_video` tool. The wire shape
// mirrors renderAIHubMixVideo (media.ts): POST /videos → poll /videos/{id} →
// download. Aspect → pixel size duplicated from media.ts's aihubmixVideoSizeFor
// so the chat-tool path and CLI/media path stay in sync.
const AIHUBMIX_VIDEO_ASPECT_RATIOS = ['16:9', '9:16', '1:1', '4:3', '3:4'] as const;
const AIHUBMIX_VIDEO_DURATION_MIN = 4;
const AIHUBMIX_VIDEO_DURATION_MAX = 15;
const AIHUBMIX_VIDEO_DURATION_DEFAULT = 5;
const AIHUBMIX_VIDEO_ASPECT_TO_SIZE: Record<string, string> = {
'16:9': '1280x720',
'9:16': '720x1280',
'1:1': '1024x1024',
'4:3': '960x720',
'3:4': '720x960',
};
// Poll cadence: Sora-class generations routinely take minutes. 5 s interval,
// 144 attempts = 12 min ceiling, matching renderAIHubMixVideo's default.
const AIHUBMIX_VIDEO_POLL_INTERVAL_MS_DEFAULT = 5000;
const AIHUBMIX_VIDEO_MAX_POLLS = 144;
const AIHUBMIX_VIDEO_PROGRESS_LOG_EVERY = 6;
const SENSEAUDIO_DEFAULT_BASE_URL = 'https://api.senseaudio.cn';
const PROMPT_MAX_LENGTH = 2000;
const SENSEAUDIO_TTS_MODEL = 'senseaudio-tts-1.5-260319';
const SENSEAUDIO_DEFAULT_VOICE_ID = 'female_0033_b';
const HEX_AUDIO_PATTERN = /^[0-9a-fA-F]+$/;
function appendSenseAudioApiPath(baseUrl: string, path: string): string {
const url = new URL(baseUrl);
const trimmed = url.pathname.replace(/\/+$/, '');
url.pathname = /\/v\d+(\/|$)/.test(trimmed)
? `${trimmed}${path}`
: `${trimmed}/v1${path}`;
return url.toString();
}
// SenseAudio video — the API only documents one model today, so the
// wire id is a const. The chat tool's `generate_video` param surface
// (prompt, aspect_ratio, duration, resolution, generate_audio) covers
// every knob the doubao-seedance gateway accepts.
const SENSEAUDIO_VIDEO_MODEL = 'doubao-seedance-2-0-260128';
const SENSEAUDIO_VIDEO_ASPECT_RATIOS = ['16:9', '9:16', '4:3', '3:4', '1:1'] as const;
const SENSEAUDIO_VIDEO_RESOLUTIONS = ['480p', '720p', '1080p'] as const;
const SENSEAUDIO_VIDEO_DURATION_MIN = 4;
const SENSEAUDIO_VIDEO_DURATION_MAX = 15;
const SENSEAUDIO_VIDEO_DURATION_DEFAULT = 5;
// Polling: SenseAudio docs recommend 5–10 s intervals; we pick 5 s and
// cap total attempts so a stuck job can't pin the chat stream forever.
// 120 attempts × 5 s = 10 min ceiling — covers the real-world
// doubao-seedance latency range (1080p + audio jobs frequently spend
// 3–8 min on the gateway). Below this, the 5-min cap timed out otherwise
// valid jobs; above this the chat surface starts feeling stuck.
const SENSEAUDIO_VIDEO_POLL_INTERVAL_MS_DEFAULT = 5000;
const SENSEAUDIO_VIDEO_MAX_POLLS = 120;
// Periodic progress log every N polls so a long-running job emits some
// signal to the daemon log — without flooding it with one line per
// 5 s. 6 polls = ~30 s between progress lines.
const SENSEAUDIO_VIDEO_PROGRESS_LOG_EVERY = 6;
// SenseAudio's image gateway rejects non-standard pixel sizes with a 400
// `参数错误:size` (verified against logs from a failed call on
// 2026-05-16). We stick to common 16-multiple HD / SD sizes that the
// gateway is known to accept: 1024×1024 for square, 1280×720 / 720×1280
// for widescreen / portrait, 1024×768 / 768×1024 for the 4:3 family.
// The table is duplicated in renderSenseAudioImage (media.ts) for the
// CLI-agent path so both surfaces stay in sync.
const ASPECT_TO_SIZE: Record<string, string> = {
'1:1': '1024x1024',
'16:9': '1280x720',
'9:16': '720x1280',
'4:3': '1024x768',
'3:4': '768x1024',
};
/**
* OpenAI-compatible tool definition for image generation. Injected into
* the upstream `tools` array on every /api/proxy/senseaudio/stream
* request so the LLM can decide on its own when to call it. The
* description deliberately tells the model to embed the returned URL
* in markdown — the chat UI already renders markdown images inline,
* so no client-side wiring is required for the bytes to show up.
*/
export const BYOK_SENSEAUDIO_TOOLS = [
{
type: 'function' as const,
function: {
name: 'generate_image',
description:
'Generate an image from a text prompt using SenseAudio image models. Returns a URL pointing to the rendered PNG. After this tool succeeds, embed the URL in your reply with markdown image syntax —  — so the user sees the image inline. Use this whenever the user asks to draw, create, generate, design, or illustrate something visual.',
parameters: {
type: 'object',
properties: {
prompt: {
type: 'string',
description:
'Detailed visual description of the image (Chinese or English are both fine). Include subject, style, lighting, composition. Maximum 2000 characters.',
},
aspect_ratio: {
type: 'string',
enum: ['1:1', '16:9', '9:16', '4:3', '3:4'],
description:
'Output aspect ratio. 1:1 for square avatars and product shots, 16:9 for hero banners, 9:16 for vertical phone posters, 4:3 for editorial covers, 3:4 for posters. Defaults to 1:1 when omitted.',
},
model: {
type: 'string',
enum: [...BYOK_SENSEAUDIO_IMAGE_MODELS],
description:
'Optional model override. Omit this to use the user-configured default from Settings (or the SenseAudio 2.0 multi-aspect model when unset). Choose senseaudio-image-2.0-260319 for multi-aspect generation, senseaudio-image-1.0-260319 for standard sizes, or doubao-seedream-5-0-260128 for high-resolution output through the ByteDance Seedream gateway. The user explicitly picked a default in their Settings — only override when the user asks for a different style/resolution.',
},
},
required: ['prompt'],
},
},
},
{
type: 'function' as const,
function: {
name: 'generate_speech',
description:
'Generate a text-to-speech voiceover using SenseAudio TTS. Returns a URL pointing to the rendered MP3. Use this whenever the user asks for narration, voiceover, speech, TTS, or spoken audio. After this tool succeeds, reply with a clickable markdown link to the MP3.',
parameters: {
type: 'object',
properties: {
text: {
type: 'string',
description:
'Exact script to speak. Include only the words that should be spoken, not production notes.',
},
voice_id: {
type: 'string',
description:
`Optional SenseAudio voice id. Defaults to ${SENSEAUDIO_DEFAULT_VOICE_ID}.`,
},
},
required: ['text'],
},
},
},
{
type: 'function' as const,
function: {
name: 'generate_video',
description:
'Generate a short video (4–15 seconds) from a text prompt using SenseAudio\'s ByteDance Seedance gateway. This is an asynchronous call that can take 30 s to a few minutes — the daemon polls the job for you, so the user just sees the chat waiting. After this tool succeeds, embed the returned URL in your reply as a markdown link, e.g. `[▶ Play video](url)`, because the chat\'s markdown renderer does not currently render `<video>` tags inline. Use this whenever the user asks for a video, clip, animation, or motion graphic.',
parameters: {
type: 'object',
properties: {
prompt: {
type: 'string',
description:
'Detailed motion description of the video. Include subject, action / camera move / scene transitions, style, lighting. Chinese or English. Maximum 2000 characters.',
},
aspect_ratio: {
type: 'string',
enum: [...SENSEAUDIO_VIDEO_ASPECT_RATIOS],
description:
'Output aspect ratio. 16:9 for cinematic, 9:16 for vertical (phone / TikTok), 1:1 for social square, 4:3 / 3:4 for editorial. Defaults to 16:9.',
},
duration: {
type: 'integer',
minimum: SENSEAUDIO_VIDEO_DURATION_MIN,
maximum: SENSEAUDIO_VIDEO_DURATION_MAX,
description:
`Video length in seconds (integer). Allowed range ${SENSEAUDIO_VIDEO_DURATION_MIN}–${SENSEAUDIO_VIDEO_DURATION_MAX}; defaults to ${SENSEAUDIO_VIDEO_DURATION_DEFAULT}. Shorter durations finish faster.`,
},
resolution: {
type: 'string',
enum: [...SENSEAUDIO_VIDEO_RESOLUTIONS],
description:
'Output resolution. 480p (fastest), 720p (default, balanced), 1080p (best quality, slowest). Pick 1080p only when the user explicitly asks for high resolution.',
},
generate_audio: {
type: 'boolean',
description:
'Whether the model also synthesises an audio track for the clip (background sound, ambience). Defaults to false to keep generation fast; flip to true when the user asks for sound, music, or a "video with audio".',
},
},
required: ['prompt'],
},
},
},
];
/**
* OpenAI-compatible tool definitions injected into /api/proxy/aihubmix/stream.
* AIHubMix routes image generation to `/v1/images/generations` (OpenAI shape),
* speech to `/v1/audio/speech`, and video to the async `/v1/videos` endpoint
* (Sora-style submit → poll → download), so the chat session gets full
* image + voiceover + video parity with the Media panel.
*/
export const BYOK_AIHUBMIX_TOOLS = [
{
type: 'function' as const,
function: {
name: 'generate_image',
description:
'Generate an image from a text prompt using AIHubMix image models (OpenAI-compatible). Returns a URL pointing to the rendered PNG. After this tool succeeds, embed the URL in your reply with markdown image syntax —  — so the user sees the image inline. Use this whenever the user asks to draw, create, generate, design, or illustrate something visual.',
parameters: {
type: 'object',
properties: {
prompt: {
type: 'string',
description:
'Detailed visual description of the image (Chinese or English are both fine). Include subject, style, lighting, composition. Maximum 2000 characters.',
},
aspect_ratio: {
type: 'string',
enum: ['1:1', '16:9', '9:16', '4:3', '3:4'],
description:
'Output aspect ratio. 1:1 for square avatars and product shots, 16:9 for hero banners, 9:16 for vertical phone posters, 4:3 for editorial covers, 3:4 for posters. Defaults to 1:1 when omitted.',
},
model: {
type: 'string',
enum: [...BYOK_AIHUBMIX_IMAGE_MODELS],
description:
'Optional model override. Omit this to use the user-configured default from Settings (gpt-image-1 when unset).',
},
},
required: ['prompt'],
},
},
},
{
type: 'function' as const,
function: {
name: 'generate_speech',
description:
'Generate a text-to-speech voiceover using AIHubMix TTS (OpenAI-compatible). Returns a URL pointing to the rendered MP3. Use this whenever the user asks for narration, voiceover, speech, TTS, or spoken audio. After this tool succeeds, reply with a clickable markdown link to the MP3.',
parameters: {
type: 'object',
properties: {
text: {
type: 'string',
description:
'Exact script to speak. Include only the words that should be spoken, not production notes.',
},
voice_id: {
type: 'string',
description:
`Optional OpenAI-style voice id (alloy, echo, fable, onyx, nova, shimmer). Defaults to ${AIHUBMIX_DEFAULT_TTS_VOICE}.`,
},
model: {
type: 'string',
description:
'Optional TTS model override (an `aihubmix-` prefixed speech model id). Omit to use the user-configured default from Settings / the composer voice-model picker.',
},
},
required: ['text'],
},
},
},
{
type: 'function' as const,
function: {
name: 'generate_video',
description:
'Generate a short video (4–15 seconds) from a text prompt using AIHubMix video models (e.g. the ByteDance Seedance gateway). This is an asynchronous call that can take 30 s to a few minutes — the daemon polls the job for you, so the user just sees the chat waiting. After this tool succeeds, embed the returned URL in your reply as a markdown link, e.g. `[▶ Play video](url)`, because the chat\'s markdown renderer does not currently render `<video>` tags inline. Use this whenever the user asks for a video, clip, animation, or motion graphic.',
parameters: {
type: 'object',
properties: {
prompt: {
type: 'string',
description:
'Detailed motion description of the video. Include subject, action / camera move / scene transitions, style, lighting. Chinese or English. Maximum 2000 characters.',
},
aspect_ratio: {
type: 'string',
enum: [...AIHUBMIX_VIDEO_ASPECT_RATIOS],
description:
'Output aspect ratio. 16:9 for cinematic, 9:16 for vertical (phone / TikTok), 1:1 for social square, 4:3 / 3:4 for editorial. Defaults to 16:9.',
},
duration: {
type: 'integer',
minimum: AIHUBMIX_VIDEO_DURATION_MIN,
maximum: AIHUBMIX_VIDEO_DURATION_MAX,
description:
`Video length in seconds (integer). Allowed range ${AIHUBMIX_VIDEO_DURATION_MIN}–${AIHUBMIX_VIDEO_DURATION_MAX}; defaults to ${AIHUBMIX_VIDEO_DURATION_DEFAULT}. Shorter durations finish faster.`,
},
model: {
type: 'string',
description:
'Optional model override (an `aihubmix-` prefixed video model id). Omit this to use the user-configured default from Settings / the composer video picker.',
},
image_url: {
type: 'string',
description:
'Reference image for image-to-video (i2v) models — the first frame / character the video animates. Pass the daemon file URL of an image already in this project (e.g. an uploaded reference or a previously generated image, like /api/projects/<id>/files/<name>.png). REQUIRED when the selected model is an i2v model (its id contains "i2v"); for those models, if you omit it the daemon falls back to the most recent image in the project.',
},
},
required: ['prompt'],
},
},
},
];
/**
* Runtime context the BYOK tool executor needs. Passed by the chat
* route on every call so the tool layer stays free of global state and
* can be unit-tested with a temp directory.
*/
export interface BYOKToolContext {
/** Daemon project root — used to look up media-config when the chat
* session key is missing. */
projectRoot: string;
/** Daemon's PROJECTS_DIR (the `<projectRoot>/.od/projects/` folder
* that holds per-project file trees). Generated images land in
* `<projectsRoot>/<projectId>/byok-<id>.png` so the project's
* FileViewer / DesignFilesPanel discover them automatically and
* the file travels with the project on export, archive, rename. */
projectsRoot: string;
/** Active project id from the chat surface. Required — the BYOK
* chat always runs inside a project, so the tool dispatch refuses
* to fire without one rather than dump bytes into a global cache.
* Validated upstream via `isSafeId`. */
projectId: string;
/** The BYOK chat session's API key — first credential we try. Bypasses
* the media-config indirection so the same key the user just pasted
* for chat is the same key the image call uses. */
upstreamApiKey: string;
/** The BYOK chat session's base URL (may be a custom gateway). Falls
* back to api.senseaudio.cn. */
upstreamBaseUrl?: string;
/** Default image model the user picked in BYOK Settings, used when the
* LLM didn't pass `model` in tool args. Validated upstream — anything
* outside `BYOK_SENSEAUDIO_IMAGE_MODELS` is dropped so a stale
* client-side config can't smuggle an unregistered model id through.
* Falls back to `BYOK_SENSEAUDIO_DEFAULT_IMAGE_MODEL` (the registry's
* first SenseAudio image entry) when missing. */
defaultImageModel?: string;
/** Default video model the user picked in BYOK Settings / the composer
* video picker, used when the LLM didn't pass `model` in tool args.
* Validated upstream against `isAIHubMixVideoModel`; falls back to
* `BYOK_AIHUBMIX_DEFAULT_VIDEO_MODEL` when missing. */
defaultVideoModel?: string;
/** Default speech (TTS) model the user picked in the composer; authoritative
* over the LLM's `model` arg. Falls back to BYOK_AIHUBMIX_DEFAULT_SPEECH_MODEL. */
defaultSpeechModel?: string;
/** Default speech voice the user picked in the composer; used when neither the
* LLM nor the caller supplies a `voice_id`. */
defaultSpeechVoice?: string;
/** Test-only override for the video polling interval (ms). Production
* uses 5 s (SenseAudio's recommendation) — tests pass small values
* (e.g. 1 ms) to keep the suite fast without changing the polling
* semantics. */
videoPollIntervalMs?: number;
/** Optional per-request init copied from the live chat turn. Used to
* forward the current proxy dispatcher AND the client-cancellation
* signal into every upstream/download fetch the BYOK tool executor
* performs, so a disconnected client stops the tool loop's paid work. */
requestInit?: Pick<RequestInit, 'dispatcher' | 'signal'>;
}
export interface ImageToolResult {
ok: boolean;
/** Daemon-served URL on success. */
url?: string;
/** Short human-readable failure reason. Stuffed into the `tool` role
* reply so the LLM can apologize / retry. */
error?: string;
}
function withToolRequestInit(
ctx: BYOKToolContext,
init: RequestInit,
): RequestInit {
return {
...ctx.requestInit,
...init,
};
}
export async function executeGenerateSpeech(
args: { text?: unknown; voice_id?: unknown },
ctx: BYOKToolContext,
): Promise<ImageToolResult> {
const text = typeof args.text === 'string' ? args.text.trim() : '';
if (!text) return { ok: false, error: 'text is required' };
let dir: string;
try {
dir = await ensureProject(ctx.projectsRoot, ctx.projectId);
} catch (err) {
return {
ok: false,
error: `invalid projectId for speech storage: ${err instanceof Error ? err.message : String(err)}`,
};
}
const apiKey = ctx.upstreamApiKey;
if (!apiKey) return { ok: false, error: 'no SenseAudio API key available' };
const voiceId =
typeof args.voice_id === 'string' && args.voice_id.trim()
? args.voice_id.trim()
: SENSEAUDIO_DEFAULT_VOICE_ID;
const baseUrl = ctx.upstreamBaseUrl || SENSEAUDIO_DEFAULT_BASE_URL;
let data: {
data?: { audio?: string };
base_resp?: { status_code?: number; status_msg?: string };
};
try {
const resp = await fetch(appendSenseAudioApiPath(baseUrl, '/t2a_v2'), withToolRequestInit(ctx, {
method: 'POST',
redirect: 'error',
headers: {
authorization: `Bearer ${apiKey}`,
'content-type': 'application/json',
},
body: JSON.stringify({
model: SENSEAUDIO_TTS_MODEL,
text,
stream: false,
voice_setting: {
voice_id: voiceId,
speed: 1,
vol: 1,
pitch: 0,
},
audio_setting: {
format: 'mp3',
sample_rate: 32000,
bitrate: 128000,
channel: 2,
},
}),
}));
const respText = await resp.text();
if (!resp.ok) {
return { ok: false, error: `senseaudio speech ${resp.status}: ${respText.slice(0, 240)}` };
}
try {
data = JSON.parse(respText) as typeof data;
} catch {
return { ok: false, error: `senseaudio speech non-JSON: ${respText.slice(0, 200)}` };
}
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err),
};
}
if (data?.base_resp && data.base_resp.status_code !== 0) {
return {
ok: false,
error: `senseaudio speech api error ${data.base_resp.status_code}: ${data.base_resp.status_msg || 'unknown'}`,
};
}
const hex = data?.data?.audio;
if (typeof hex !== 'string' || !hex) {
return { ok: false, error: 'senseaudio speech response missing data.audio' };
}
if (hex.length % 2 !== 0 || !HEX_AUDIO_PATTERN.test(hex)) {
return { ok: false, error: 'senseaudio speech response contained invalid hex audio' };
}
const bytes = Buffer.from(hex, 'hex');
if (bytes.length === 0) return { ok: false, error: 'senseaudio speech decoded zero bytes' };
const id = `${Date.now().toString(36)}-${randomBytes(4).toString('hex')}`;
const filename = `byok-speech-${id}.mp3`;
await writeFile(path.join(dir, filename), bytes);
return {
ok: true,
url: `/api/projects/${encodeURIComponent(ctx.projectId)}/files/${filename}`,
};
}
function sanitizeAspectRatio(raw: unknown): string {
if (typeof raw !== 'string') return '1:1';
return ASPECT_TO_SIZE[raw] ? raw : '1:1';
}
/**
* Execute the `generate_image` tool. Calls SenseAudio /v1/image/sync,
* downloads the rendered bytes, writes them to <byokImagesDir>/<id>.png,
* and returns a daemon-served URL. Pure async — caller is responsible
* for emitting any SSE events (e.g. "tool result ready").
*
* Failure modes return `{ok: false, error}` rather than throwing so the
* caller can feed the message back to the LLM as a tool_result; that
* lets the model apologize / suggest a retry instead of the chat
* silently stopping.
*/
export async function executeGenerateImage(
args: { prompt?: unknown; aspect_ratio?: unknown; model?: unknown },
ctx: BYOKToolContext,
): Promise<ImageToolResult> {
const promptRaw = typeof args.prompt === 'string' ? args.prompt.trim() : '';
if (!promptRaw) return { ok: false, error: 'prompt is required' };
const prompt =
promptRaw.length > PROMPT_MAX_LENGTH
? promptRaw.slice(0, PROMPT_MAX_LENGTH)
: promptRaw;
const aspect = sanitizeAspectRatio(args.aspect_ratio);
const size = ASPECT_TO_SIZE[aspect];
// Model resolution order — LLM args > user's Settings default > registry
// default. The allowlist guards every step so a hallucinated or stale id
// can never reach the senseaudio /v1/image/sync wire — the catalogue is
// the source of truth.
const senseAudioImageModel = isSenseAudioImageModel(args.model)
? args.model
: isSenseAudioImageModel(ctx.defaultImageModel)
? ctx.defaultImageModel
: BYOK_SENSEAUDIO_DEFAULT_IMAGE_MODEL;
// Resolve the project folder up front. ensureProject runs
// `isSafeId` internally, so an attacker who somehow bypassed the
// chat-routes guard and slipped `../escape` into projectId fails
// here before we make any upstream call. The returned `dir` is
// reused at writeFile time below.
let dir: string;
try {
dir = await ensureProject(ctx.projectsRoot, ctx.projectId);
} catch (err) {
return {
ok: false,
error: `invalid projectId for image storage: ${err instanceof Error ? err.message : String(err)}`,
};
}
// Prefer the BYOK session's key (what the user is actively using).
// Fall back to media-config (env var > stored) so a user who set
// OD_SENSEAUDIO_API_KEY but forgot to fill the chat panel still
// gets a working tool call.
let apiKey = ctx.upstreamApiKey;
let baseUrl = ctx.upstreamBaseUrl || SENSEAUDIO_DEFAULT_BASE_URL;
if (!apiKey) {
const resolved = await resolveProviderConfig(ctx.projectRoot, 'senseaudio');
apiKey = resolved.apiKey || '';
if (resolved.baseUrl) baseUrl = resolved.baseUrl;
}
if (!apiKey) {
return { ok: false, error: 'no SenseAudio API key available' };
}
const trimmedBase = baseUrl.replace(/\/+$/, '');
// Log the resolved image model + size before the upstream call so
// `tools-dev logs` shows which SenseAudio image model a chat-driven
// generation actually hit. Mirrors the AIHubMix/video submit logs; it's a
// server-side call, so it never appears in the browser Network tab.
console.log(
`[proxy:senseaudio] generate_image submit POST ${trimmedBase}/v1/image/sync model=${senseAudioImageModel} size=${size}`,
);
let imageUrl: string;
try {
const resp = await fetch(`${trimmedBase}/v1/image/sync`, withToolRequestInit(ctx, {
method: 'POST',
headers: {
authorization: `Bearer ${apiKey}`,
'content-type': 'application/json',
},
body: JSON.stringify({
model: senseAudioImageModel,
prompt,
size,
}),
}));
if (!resp.ok) {
const text = await resp.text().catch(() => '');
return {
ok: false,
error: `senseaudio image ${resp.status}: ${text.slice(0, 240)}`,
};
}
const data = (await resp.json()) as {
url?: string;
error_message?: string;
base_resp?: { status_code?: number; status_msg?: string };
};
if (data?.base_resp && data.base_resp.status_code !== 0) {
return {
ok: false,
error: `senseaudio image api error ${data.base_resp.status_code}: ${data.base_resp.status_msg || 'unknown'}`,
};
}
if (typeof data?.error_message === 'string' && data.error_message) {
return { ok: false, error: `senseaudio image: ${data.error_message}` };
}
if (typeof data?.url !== 'string' || !data.url) {
return { ok: false, error: 'senseaudio image response missing url' };
}
imageUrl = data.url;
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err),
};
}
const imageUrlCheck = await assertExternalAssetUrl(imageUrl);
if (!imageUrlCheck.ok) return { ok: false, error: imageUrlCheck.error };
let bytes: Buffer;
try {
const imgResp = await fetch(imageUrl, withToolRequestInit(ctx, { redirect: 'error' }));
if (!imgResp.ok) {
return { ok: false, error: `image download ${imgResp.status}` };
}
bytes = Buffer.from(await imgResp.arrayBuffer());
} catch (err) {
return {
ok: false,
error: `image download failed: ${err instanceof Error ? err.message : String(err)}`,
};
}
if (bytes.length === 0) {
return { ok: false, error: 'image download returned zero bytes' };
}
// Persist into the active project's folder. `dir` was resolved up
// front via ensureProject — no DB write, no metadata side-effects —
// and the resulting path slots straight into the existing project
// file plumbing: listFiles enumerates it for the FileViewer,
// readProjectFile serves it via GET /api/projects/<id>/files/<filename>,
// and project archive / export pick it up automatically because it
// lives under the project's own directory.
//
// Filename pattern `byok-<timestamp>-<random>.png` keeps tool
// outputs distinguishable from user uploads at a glance while
// staying url-safe.
const id = `${Date.now().toString(36)}-${randomBytes(4).toString('hex')}`;
const filename = `byok-${id}.png`;
await writeFile(path.join(dir, filename), bytes);
// Return a relative URL through the project file serving route. The
// web's Next.js rewrites `/api/:path*` to the daemon (see
// apps/web/next.config.ts), so the chat UI loads the image
// same-origin — satisfying the strict CSP (`img-src 'self' data:
// blob:`) without any CORS plumbing.
return {
ok: true,
url: `/api/projects/${encodeURIComponent(ctx.projectId)}/files/${filename}`,
};
}
function sanitizeVideoAspectRatio(raw: unknown): (typeof SENSEAUDIO_VIDEO_ASPECT_RATIOS)[number] {
if (typeof raw !== 'string') return '16:9';
return (SENSEAUDIO_VIDEO_ASPECT_RATIOS as readonly string[]).includes(raw)
? (raw as (typeof SENSEAUDIO_VIDEO_ASPECT_RATIOS)[number])
: '16:9';
}
function sanitizeVideoResolution(raw: unknown): (typeof SENSEAUDIO_VIDEO_RESOLUTIONS)[number] {
if (typeof raw !== 'string') return '720p';
return (SENSEAUDIO_VIDEO_RESOLUTIONS as readonly string[]).includes(raw)
? (raw as (typeof SENSEAUDIO_VIDEO_RESOLUTIONS)[number])
: '720p';
}
function sanitizeVideoDuration(raw: unknown): number {
if (typeof raw !== 'number' || !Number.isFinite(raw)) return SENSEAUDIO_VIDEO_DURATION_DEFAULT;
const rounded = Math.round(raw);
if (rounded < SENSEAUDIO_VIDEO_DURATION_MIN) return SENSEAUDIO_VIDEO_DURATION_MIN;
if (rounded > SENSEAUDIO_VIDEO_DURATION_MAX) return SENSEAUDIO_VIDEO_DURATION_MAX;
return rounded;
}
const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
/**
* Execute the `generate_video` tool. SenseAudio's video API is
* asynchronous-only: POST /v1/video/create returns a task_id, then
* GET /v1/video/status?id=<task_id> reports `pending` / `processing`
* → `completed` (with `video_url`) or `failed` (with `error_message`).
* We poll every `videoPollIntervalMs` (default 5 s) and bail after
* `SENSEAUDIO_VIDEO_MAX_POLLS` so a stuck upstream can't pin the
* chat stream forever.
*
* The chat tool waits for the whole loop, so the daemon's outbound
* SSE response from /api/proxy/senseaudio/stream stays open for the
* duration. That's intentional — the next chat turn cannot begin
* until we have a URL to feed back into the tool_result.
*/
export async function executeGenerateVideo(
args: {
prompt?: unknown;
aspect_ratio?: unknown;
duration?: unknown;
resolution?: unknown;
generate_audio?: unknown;
},
ctx: BYOKToolContext,
): Promise<ImageToolResult> {
const promptRaw = typeof args.prompt === 'string' ? args.prompt.trim() : '';
if (!promptRaw) return { ok: false, error: 'prompt is required' };
const prompt =
promptRaw.length > PROMPT_MAX_LENGTH
? promptRaw.slice(0, PROMPT_MAX_LENGTH)
: promptRaw;
const ratio = sanitizeVideoAspectRatio(args.aspect_ratio);
const resolution = sanitizeVideoResolution(args.resolution);
const duration = sanitizeVideoDuration(args.duration);
const generateAudio = args.generate_audio === true;
let dir: string;
try {
dir = await ensureProject(ctx.projectsRoot, ctx.projectId);
} catch (err) {
return {
ok: false,
error: `invalid projectId for video storage: ${err instanceof Error ? err.message : String(err)}`,
};
}
let apiKey = ctx.upstreamApiKey;
let baseUrl = ctx.upstreamBaseUrl || SENSEAUDIO_DEFAULT_BASE_URL;
if (!apiKey) {
const resolved = await resolveProviderConfig(ctx.projectRoot, 'senseaudio');
apiKey = resolved.apiKey || '';
if (resolved.baseUrl) baseUrl = resolved.baseUrl;
}
if (!apiKey) {
return { ok: false, error: 'no SenseAudio API key available' };
}
const trimmedBase = baseUrl.replace(/\/+$/, '');
// Step 1: POST /v1/video/create → task_id.
let taskId: string;
try {
const resp = await fetch(`${trimmedBase}/v1/video/create`, withToolRequestInit(ctx, {
method: 'POST',
headers: {
authorization: `Bearer ${apiKey}`,
'content-type': 'application/json',
},
body: JSON.stringify({
model: SENSEAUDIO_VIDEO_MODEL,
content: [{ type: 'text', text: prompt }],
duration,
resolution,
ratio,
provider_specific: { generate_audio: generateAudio },
}),
}));
if (!resp.ok) {
const text = await resp.text().catch(() => '');
return {
ok: false,
error: `senseaudio video create ${resp.status}: ${text.slice(0, 240)}`,
};
}
const data = (await resp.json()) as { task_id?: string };
if (typeof data?.task_id !== 'string' || !data.task_id) {
return { ok: false, error: 'senseaudio video create response missing task_id' };
}
taskId = data.task_id;
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err),
};
}
// Step 2: poll /v1/video/status until completed / failed / timeout.
const pollIntervalMs = ctx.videoPollIntervalMs ?? SENSEAUDIO_VIDEO_POLL_INTERVAL_MS_DEFAULT;
let videoUrl = '';
for (let attempt = 0; attempt < SENSEAUDIO_VIDEO_MAX_POLLS; attempt++) {
await sleep(pollIntervalMs);
let statusResp: Response;
try {
statusResp = await fetch(
`${trimmedBase}/v1/video/status?id=${encodeURIComponent(taskId)}`,
withToolRequestInit(ctx, {
method: 'GET',
headers: { authorization: `Bearer ${apiKey}` },
}),
);
} catch (err) {
return {
ok: false,
error: `senseaudio video poll failed: ${err instanceof Error ? err.message : String(err)}`,
};
}
if (!statusResp.ok) {
const text = await statusResp.text().catch(() => '');
return {
ok: false,
error: `senseaudio video status ${statusResp.status}: ${text.slice(0, 240)}`,
};
}
const data = (await statusResp.json()) as {
status?: string;
progress?: number;
video_url?: string;
error_message?: string;
};
if (data?.status === 'completed') {
if (typeof data.video_url !== 'string' || !data.video_url) {
return { ok: false, error: 'senseaudio video status completed but missing video_url' };
}
videoUrl = data.video_url;
break;
}
if (data?.status === 'failed') {
return {
ok: false,
error: `senseaudio video failed: ${data.error_message || 'unknown reason'}`,
};
}
// pending / processing — continue polling. Emit a periodic log line
// so a stuck job surfaces in the daemon log instead of silently
// burning attempts.
if ((attempt + 1) % SENSEAUDIO_VIDEO_PROGRESS_LOG_EVERY === 0) {
const pct = typeof data.progress === 'number' ? data.progress : '?';
console.log(
`[proxy:senseaudio] generate_video poll ${attempt + 1}/${SENSEAUDIO_VIDEO_MAX_POLLS} task=${taskId} status=${data.status ?? 'unknown'} progress=${pct}`,
);
}
}
if (!videoUrl) {
return {
ok: false,
error: `senseaudio video timed out after ${SENSEAUDIO_VIDEO_MAX_POLLS} polls`,
};
}
// Step 3: download the mp4 bytes and persist into the project folder.
// Re-validate the returned URL through validateBaseUrlResolved so a
// malicious gateway can't point us at 169.254.169.254 (AWS / Azure
// metadata service) or RFC1918 hosts via the response payload.
const videoUrlCheck = await assertExternalAssetUrl(videoUrl);
if (!videoUrlCheck.ok) return { ok: false, error: videoUrlCheck.error };
let bytes: Buffer;
try {
const videoResp = await fetch(videoUrl, withToolRequestInit(ctx, { redirect: 'error' }));
if (!videoResp.ok) {
return { ok: false, error: `video download ${videoResp.status}` };
}
bytes = Buffer.from(await videoResp.arrayBuffer());
} catch (err) {
return {
ok: false,
error: `video download failed: ${err instanceof Error ? err.message : String(err)}`,
};
}
if (bytes.length === 0) {
return { ok: false, error: 'video download returned zero bytes' };
}
const id = `${Date.now().toString(36)}-${randomBytes(4).toString('hex')}`;
const filename = `byok-video-${id}.mp4`;
await writeFile(path.join(dir, filename), bytes);
return {
ok: true,
url: `/api/projects/${encodeURIComponent(ctx.projectId)}/files/${filename}`,
};
}
// ---------------------------------------------------------------------------
// AIHubMix tool executors (OpenAI-wire-compatible).
//
// Unlike the SenseAudio executors above (which hit proprietary /v1/image/sync
// and /v1/t2a_v2 endpoints), AIHubMix speaks the OpenAI image/audio shapes:
// POST /v1/images/generations → { data: [{ b64_json | url }] }
// POST /v1/audio/speech → raw audio bytes
// Every request carries the fixed APP-Code header via aihubmixHeaders().
// ---------------------------------------------------------------------------
function appendOpenAIApiPath(baseUrl: string, suffix: string): string {
const url = new URL(baseUrl);
const trimmed = url.pathname.replace(/\/+$/, '');
url.pathname = /\/v\d+(\/|$)/.test(trimmed)
? `${trimmed}${suffix}`
: `${trimmed}/v1${suffix}`;
return url.toString();
}
async function resolveAIHubMixCredentials(
ctx: BYOKToolContext,
): Promise<{ apiKey: string; baseUrl: string }> {
let apiKey = ctx.upstreamApiKey;
let baseUrl = ctx.upstreamBaseUrl || AIHUBMIX_DEFAULT_BASE_URL;
if (!apiKey) {
const resolved = await resolveProviderConfig(ctx.projectRoot, 'aihubmix');
apiKey = resolved.apiKey || '';
if (resolved.baseUrl) baseUrl = resolved.baseUrl;
}
return { apiKey, baseUrl };
}
export async function executeAIHubMixGenerateImage(
args: { prompt?: unknown; aspect_ratio?: unknown; model?: unknown },
ctx: BYOKToolContext,
): Promise<ImageToolResult> {
const promptRaw = typeof args.prompt === 'string' ? args.prompt.trim() : '';
if (!promptRaw) return { ok: false, error: 'prompt is required' };
const prompt =
promptRaw.length > PROMPT_MAX_LENGTH ? promptRaw.slice(0, PROMPT_MAX_LENGTH) : promptRaw;
const aspect =
typeof args.aspect_ratio === 'string' && AIHUBMIX_IMAGE_ASPECT_TO_SIZE[args.aspect_ratio]
? args.aspect_ratio