@xopcai/xopc 0.0.466 → 0.0.467

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 (82) hide show
  1. package/dist/browser-ext/manifest.json +1 -1
  2. package/dist/extensions/telegram/xopc.extension.json +1 -1
  3. package/dist/gateway/static/root/assets/{automations-page-_apIsCPT.js → automations-page-3WGWLdiW.js} +1 -1
  4. package/dist/gateway/static/root/assets/{automations-workspace-WmcQUi9O.js → automations-workspace-CH9gYqIK.js} +2 -2
  5. package/dist/gateway/static/root/assets/{binary-plugins-C7i5pqkO.js → binary-plugins-MFVtTdvy.js} +1 -1
  6. package/dist/gateway/static/root/assets/capabilities-settings-panel-BjaLTd6i.js +8 -0
  7. package/dist/gateway/static/root/assets/chat-page-BtMkdtxJ.js +71 -0
  8. package/dist/gateway/static/root/assets/{home-page-Dgopm60U.js → home-page-BBTtXpDD.js} +3 -3
  9. package/dist/gateway/static/root/assets/index-BJj-uZNl.js +11 -0
  10. package/dist/gateway/static/root/assets/index-DiWUK4sJ.css +1 -0
  11. package/dist/gateway/static/root/assets/{media-plugins-DlMEwcS8.js → media-plugins-CW5UoTx6.js} +1 -1
  12. package/dist/gateway/static/root/assets/{note-detail-panel-yAPC_cZU.js → note-detail-panel-D5BtgBxy.js} +2 -2
  13. package/dist/gateway/static/root/assets/{notes-page-CUMhjKKd.js → notes-page-DyM-3O_9.js} +1 -1
  14. package/dist/gateway/static/root/assets/personal-page-BB4Sv68K.js +1 -0
  15. package/dist/gateway/static/root/assets/{project-detail-page-BjOzJyw1.js → project-detail-page-BG2nDt98.js} +1 -1
  16. package/dist/gateway/static/root/assets/route-preload-BYqg1BYz.js +2 -0
  17. package/dist/gateway/static/root/assets/{routine-panel-DSNsdpy8.js → routine-panel-Coh2B0j7.js} +2 -2
  18. package/dist/gateway/static/root/assets/{settings-page-u-U9D9qT.js → settings-page-D9A2DdJ_.js} +1 -1
  19. package/dist/gateway/static/root/assets/{share-preview-page-BYPdlPIb.js → share-preview-page-CyCv69rh.js} +1 -1
  20. package/dist/gateway/static/root/assets/{task-detail-page-BPHt6S1V.js → task-detail-page-DgsLOz6h.js} +1 -1
  21. package/dist/gateway/static/root/assets/{use-composer-attachments-UhypPQH_.js → use-composer-attachments-C9GpCJQR.js} +1 -1
  22. package/dist/gateway/static/root/assets/{use-file-preview-expanded-BvEF3yHL.js → use-file-preview-expanded-Daf-LmiF.js} +2 -2
  23. package/dist/gateway/static/root/assets/video-generation-D08uZbsu.js +1 -0
  24. package/dist/gateway/static/root/assets/{workspace-file-preview-dialog-BRQIf4sc.js → workspace-file-preview-dialog-JSGIA79T.js} +1 -1
  25. package/dist/gateway/static/root/index.html +9 -10
  26. package/dist/package.js +1 -1
  27. package/dist/packages/gateway-contract/src/index.js +2 -0
  28. package/dist/packages/gateway-contract/src/music-generation.js +6 -0
  29. package/dist/src/agent/inbound/attachment-pipeline.d.ts +1 -0
  30. package/dist/src/agent/inbound/attachment-pipeline.js +5 -1
  31. package/dist/src/agent/prompt/sections/tooling.js +4 -0
  32. package/dist/src/agent/tools/factory.js +14 -1
  33. package/dist/src/agent/tools/music-generation-tool.d.ts +11 -0
  34. package/dist/src/agent/tools/music-generation-tool.js +70 -0
  35. package/dist/src/agent/tools/toolExposure.js +2 -0
  36. package/dist/src/agent/tools/video-draft-tool.d.ts +12 -0
  37. package/dist/src/agent/tools/video-draft-tool.js +75 -0
  38. package/dist/src/cli/commands/doctor/checks/database-schema.js +1 -1
  39. package/dist/src/gateway/hono/routes/lazy-bundles.js +9 -1
  40. package/dist/src/gateway/hono/routes/media.js +1 -20
  41. package/dist/src/gateway/hono/routes/music-generation.d.ts +3 -0
  42. package/dist/src/gateway/hono/routes/music-generation.js +59 -0
  43. package/dist/src/gateway/hono/routes/video-generation.js +51 -26
  44. package/dist/src/gateway/service.d.ts +1 -0
  45. package/dist/src/gateway/service.js +4 -1
  46. package/dist/src/media-shared/http/byte-range.d.ts +6 -0
  47. package/dist/src/media-shared/http/byte-range.js +23 -0
  48. package/dist/src/music-generation/artifact.d.ts +5 -0
  49. package/dist/src/music-generation/artifact.js +105 -0
  50. package/dist/src/music-generation/mureka.d.ts +3 -0
  51. package/dist/src/music-generation/mureka.js +96 -0
  52. package/dist/src/music-generation/provider.d.ts +31 -0
  53. package/dist/src/music-generation/provider.js +4 -0
  54. package/dist/src/music-generation/providers.d.ts +26 -0
  55. package/dist/src/music-generation/providers.js +56 -0
  56. package/dist/src/music-generation/repository.d.ts +50 -0
  57. package/dist/src/music-generation/repository.js +104 -0
  58. package/dist/src/music-generation/settings.d.ts +7 -0
  59. package/dist/src/music-generation/settings.js +24 -0
  60. package/dist/src/music-generation/worker.d.ts +8 -0
  61. package/dist/src/music-generation/worker.js +115 -0
  62. package/dist/src/personal-agent/policy.d.ts +1 -1
  63. package/dist/src/personal-agent/policy.js +3 -1
  64. package/dist/src/personal-agent/prompts.js +2 -0
  65. package/dist/src/session/store.js +8 -2
  66. package/dist/src/storage/sqlite/schema.d.ts +2 -2
  67. package/dist/src/storage/sqlite/schema.js +7 -7
  68. package/dist/src/storage/sqlite/schema.sql +31 -0
  69. package/dist/src/storage/sqlite/transcript-repository.d.ts +1 -0
  70. package/dist/src/storage/sqlite/transcript-repository.js +8 -1
  71. package/dist/src/storage/sqlite/upgrades/277_music_generation.sql +30 -0
  72. package/dist/src/video-generation/drafts.d.ts +17 -0
  73. package/dist/src/video-generation/drafts.js +89 -0
  74. package/dist/src/video-generation/worker.js +1 -1
  75. package/package.json +1 -1
  76. package/dist/gateway/static/root/assets/api-CG-SjNfw.js +0 -1
  77. package/dist/gateway/static/root/assets/capabilities-settings-panel-DuQGhgAl.js +0 -8
  78. package/dist/gateway/static/root/assets/chat-page-BMZwIVXH.js +0 -71
  79. package/dist/gateway/static/root/assets/index-BE3XMuQI.js +0 -11
  80. package/dist/gateway/static/root/assets/index-CY5vdQcc.css +0 -1
  81. package/dist/gateway/static/root/assets/personal-page-POCG1_kK.js +0 -1
  82. package/dist/gateway/static/root/assets/route-preload-DjYsRhY1.js +0 -2
@@ -0,0 +1,31 @@
1
+ import type { MusicMode } from '@xopcai/gateway-contract';
2
+ export interface MusicInput {
3
+ mode: MusicMode;
4
+ prompt: string;
5
+ lyrics: string | null;
6
+ }
7
+ export type MusicProviderStatus = {
8
+ state: 'submitted' | 'running';
9
+ } | {
10
+ state: 'completed';
11
+ url: string;
12
+ durationMs: number | null;
13
+ } | {
14
+ state: 'failed';
15
+ reason: string;
16
+ };
17
+ export interface MusicProvider {
18
+ id: string;
19
+ name: string;
20
+ models: Readonly<Record<string, {
21
+ modes: readonly MusicMode[];
22
+ maxPromptCharacters: number;
23
+ maxLyricsCharacters: number;
24
+ }>>;
25
+ environmentKey(): string | undefined;
26
+ submit(key: string, model: string, input: MusicInput): Promise<string>;
27
+ query(key: string, mode: MusicMode, taskId: string): Promise<MusicProviderStatus>;
28
+ allowsAudioHost(host: string): boolean;
29
+ }
30
+ export declare class MusicRequestRejected extends Error {
31
+ }
@@ -0,0 +1,4 @@
1
+ //#region src/music-generation/provider.ts
2
+ var MusicRequestRejected = class extends Error {};
3
+ //#endregion
4
+ export { MusicRequestRejected };
@@ -0,0 +1,26 @@
1
+ import type { MusicModelSettings } from '@xopcai/gateway-contract';
2
+ import type { MusicProvider } from './provider.js';
3
+ export declare const DEFAULT_MUSIC_MODEL = "mureka-cn/mureka-9.5";
4
+ export declare function resolveMusicModel(ref: string): {
5
+ provider: MusicProvider;
6
+ model: string;
7
+ capabilities: {
8
+ modes: readonly import("@xopcai/gateway-contract").MusicMode[];
9
+ maxPromptCharacters: number;
10
+ maxLyricsCharacters: number;
11
+ };
12
+ };
13
+ export declare function hasMusicProvider(id: string): boolean;
14
+ export declare function resolveMusicKey(provider: MusicProvider): Promise<string | undefined>;
15
+ export declare function configuredMusicModel(): Promise<{
16
+ ref: string;
17
+ defaults: import("@xopcai/gateway-contract").MusicGenerationDefaults;
18
+ provider: MusicProvider;
19
+ model: string;
20
+ capabilities: {
21
+ modes: readonly import("@xopcai/gateway-contract").MusicMode[];
22
+ maxPromptCharacters: number;
23
+ maxLyricsCharacters: number;
24
+ };
25
+ }>;
26
+ export declare function readMusicModelSettings(): Promise<MusicModelSettings>;
@@ -0,0 +1,56 @@
1
+ import { murekaGlobalProvider, murekaProvider } from "./mureka.js";
2
+ import { getMusicDefaults, musicCredentials, storedMusicModel } from "./settings.js";
3
+ //#region src/music-generation/providers.ts
4
+ const providers = new Map([murekaProvider, murekaGlobalProvider].map((provider) => [provider.id, provider]));
5
+ const DEFAULT_MUSIC_MODEL = "mureka-cn/mureka-9.5";
6
+ function resolveMusicModel(ref) {
7
+ const [providerId, model, extra] = ref.split("/");
8
+ const provider = providers.get(providerId);
9
+ if (!model || extra !== void 0 || !provider || !Object.hasOwn(provider.models, model)) return void 0;
10
+ return {
11
+ provider,
12
+ model,
13
+ capabilities: provider.models[model]
14
+ };
15
+ }
16
+ function selectedMusicModel() {
17
+ const ref = storedMusicModel() ?? process.env.XOPC_MUSIC_MODEL?.trim();
18
+ return ref && resolveMusicModel(ref) ? ref : DEFAULT_MUSIC_MODEL;
19
+ }
20
+ function hasMusicProvider(id) {
21
+ return providers.has(id);
22
+ }
23
+ async function resolveMusicKey(provider) {
24
+ return await musicCredentials().revealGatewayStoredApiKey(provider.id) ?? provider.environmentKey();
25
+ }
26
+ async function configuredMusicModel() {
27
+ const ref = selectedMusicModel();
28
+ const route = resolveMusicModel(ref);
29
+ if (!route || !await resolveMusicKey(route.provider)) return void 0;
30
+ return {
31
+ ...route,
32
+ ref,
33
+ defaults: getMusicDefaults(ref, route.capabilities.modes)
34
+ };
35
+ }
36
+ async function readMusicModelSettings() {
37
+ const models = [...providers.values()].flatMap((provider) => Object.entries(provider.models).map(([name, definition]) => ({
38
+ ref: `${provider.id}/${name}`,
39
+ name,
40
+ providerId: provider.id,
41
+ modes: definition.modes
42
+ })));
43
+ const credentials = Object.fromEntries(await Promise.all([...providers.values()].map(async (provider) => {
44
+ const saved = await musicCredentials().revealGatewayStoredApiKey(provider.id);
45
+ return [provider.id, saved ? "saved" : provider.environmentKey() ? "environment" : "missing"];
46
+ })));
47
+ const modelRef = selectedMusicModel();
48
+ return {
49
+ modelRef,
50
+ models,
51
+ credentials,
52
+ defaults: getMusicDefaults(modelRef, models.find((model) => model.ref === modelRef)?.modes ?? [])
53
+ };
54
+ }
55
+ //#endregion
56
+ export { DEFAULT_MUSIC_MODEL, configuredMusicModel, hasMusicProvider, readMusicModelSettings, resolveMusicKey, resolveMusicModel };
@@ -0,0 +1,50 @@
1
+ import type { MusicGenerationJob, MusicJobState, MusicMode } from '@xopcai/gateway-contract';
2
+ export interface MusicJobRow {
3
+ id: string;
4
+ conversation_id: string;
5
+ transcript_id: string;
6
+ work_id: string;
7
+ parent_id: string | null;
8
+ version_number: number;
9
+ model_ref: string;
10
+ request_hash: string;
11
+ title: string;
12
+ mode: MusicMode;
13
+ prompt: string;
14
+ lyrics: string | null;
15
+ state: MusicJobState;
16
+ provider_task_id: string | null;
17
+ duration_ms: number | null;
18
+ error: string | null;
19
+ created_at: number;
20
+ }
21
+ export declare function publicMusicJob(row: MusicJobRow): MusicGenerationJob;
22
+ export declare function listMusicJobs(conversationId: string, transcriptId: string): MusicGenerationJob[];
23
+ export declare function listMusicJobIds(conversationId: string): string[];
24
+ export declare function getMusicJob(conversationId: string, id: string): MusicJobRow | undefined;
25
+ type MusicRequest = {
26
+ parentId?: string;
27
+ title?: string;
28
+ mode?: MusicMode;
29
+ prompt?: string;
30
+ lyrics?: string;
31
+ };
32
+ export declare function enqueueMusicJob(conversationId: string, transcriptId: string, toolCallId: string, request: MusicRequest, signal?: AbortSignal): Promise<{
33
+ error: string;
34
+ job?: undefined;
35
+ } | {
36
+ job: MusicGenerationJob;
37
+ error?: undefined;
38
+ }>;
39
+ export declare function claimMusicJob(): {
40
+ row: MusicJobRow;
41
+ token: `${string}-${string}-${string}-${string}-${string}`;
42
+ };
43
+ export declare function updateMusicJob(id: string, token: string, state: MusicJobState, options?: {
44
+ taskId?: string;
45
+ durationMs?: number | null;
46
+ error?: string;
47
+ delayMs?: number;
48
+ keepLease?: boolean;
49
+ }): boolean;
50
+ export {};
@@ -0,0 +1,104 @@
1
+ import { getSqliteDatabase, init_transaction, runSqliteWriteTransaction } from "../storage/sqlite/transaction.js";
2
+ import { configuredMusicModel } from "./providers.js";
3
+ import { createHash, randomUUID } from "node:crypto";
4
+ //#region src/music-generation/repository.ts
5
+ init_transaction();
6
+ function publicMusicJob(row) {
7
+ return {
8
+ id: row.id,
9
+ workId: row.work_id,
10
+ parentId: row.parent_id,
11
+ versionNumber: row.version_number,
12
+ modelRef: row.model_ref,
13
+ title: row.title,
14
+ mode: row.mode,
15
+ prompt: row.prompt,
16
+ lyrics: row.lyrics,
17
+ state: row.state,
18
+ durationMs: row.duration_ms,
19
+ error: row.error,
20
+ createdAt: row.created_at
21
+ };
22
+ }
23
+ function listMusicJobs(conversationId, transcriptId) {
24
+ return getSqliteDatabase().prepare(`SELECT * FROM music_generation_jobs WHERE conversation_id = ?
25
+ AND transcript_id = ? ORDER BY created_at DESC, rowid DESC LIMIT 100`).all(conversationId, transcriptId).map(publicMusicJob);
26
+ }
27
+ function listMusicJobIds(conversationId) {
28
+ return getSqliteDatabase().prepare("SELECT id FROM music_generation_jobs WHERE conversation_id = ?").all(conversationId).map((row) => String(row.id));
29
+ }
30
+ function getMusicJob(conversationId, id) {
31
+ return getSqliteDatabase().prepare("SELECT * FROM music_generation_jobs WHERE conversation_id = ? AND id = ?").get(conversationId, id);
32
+ }
33
+ async function enqueueMusicJob(conversationId, transcriptId, toolCallId, request, signal) {
34
+ const id = createHash("sha256").update(JSON.stringify([
35
+ conversationId,
36
+ transcriptId,
37
+ toolCallId
38
+ ])).digest("hex");
39
+ const hash = createHash("sha256").update(JSON.stringify([
40
+ request.parentId,
41
+ request.title,
42
+ request.mode,
43
+ request.prompt,
44
+ request.lyrics
45
+ ])).digest("hex");
46
+ const route = await configuredMusicModel();
47
+ signal?.throwIfAborted();
48
+ return runSqliteWriteTransaction((db) => {
49
+ if (db.prepare("SELECT active_transcript_id FROM sessions WHERE conversation_id = ?").get(conversationId)?.active_transcript_id !== transcriptId) return { error: "The chat was reset. Ask the user to send the request again." };
50
+ const previous = getMusicJob(conversationId, id);
51
+ if (previous) return previous.request_hash === hash ? { job: publicMusicJob(previous) } : { error: "This request has already been submitted with different input." };
52
+ const parent = request.parentId ? getMusicJob(conversationId, request.parentId) : void 0;
53
+ if (request.parentId && (parent?.transcript_id !== transcriptId || parent.state !== "completed")) return { error: "Select a completed version from the current chat." };
54
+ if (!route) return { error: "Music generation is unavailable. Configure the Gateway music model and its API key." };
55
+ const mode = request.mode ?? (request.lyrics ? "song" : parent?.mode ?? route.defaults.mode);
56
+ const title = (request.title ?? parent?.title)?.trim();
57
+ const prompt = (request.prompt ?? parent?.prompt)?.trim();
58
+ const lyrics = mode === "song" ? request.lyrics ?? parent?.lyrics ?? null : null;
59
+ if (mode === "song" && !lyrics?.trim()) return { error: "Song generation requires lyrics. Provide complete lyrics, or use instrumental mode." };
60
+ if (!route.capabilities.modes.includes(mode) || !title || !prompt || title.length > 100 || Array.from(prompt).length > route.capabilities.maxPromptCharacters || lyrics !== null && Array.from(lyrics).length > route.capabilities.maxLyricsCharacters || mode === "instrumental" && request.lyrics !== void 0) return { error: "Invalid music input. Check the selected mode, description and lyrics limits before submitting." };
61
+ const now = Date.now();
62
+ const active = db.prepare(`SELECT count(*) AS count FROM music_generation_jobs
63
+ WHERE state IN ('queued', 'submitting', 'submitted', 'running', 'saving')`).get();
64
+ if (Number(active?.count) >= 3) return { error: "The music queue is full. Wait for an active request to finish." };
65
+ const day = Math.floor(now / 864e5);
66
+ const configuredLimit = process.env.XOPC_MUSIC_DAILY_LIMIT?.trim();
67
+ const limit = configuredLimit ? Number(configuredLimit) : 20;
68
+ if (!Number.isSafeInteger(limit) || limit < 0) return { error: "Invalid Gateway music daily limit." };
69
+ if (Number(db.prepare("SELECT count AS used FROM music_generation_daily_usage WHERE day = ?").get(day)?.used ?? 0) >= limit) return { error: "The Gateway has reached its daily music generation limit." };
70
+ db.prepare(`INSERT INTO music_generation_daily_usage (day, count) VALUES (?, 1)
71
+ ON CONFLICT(day) DO UPDATE SET count = count + 1`).run(day);
72
+ const workId = parent?.work_id ?? id;
73
+ const version = Number(db.prepare("SELECT MAX(version_number) AS version FROM music_generation_jobs WHERE work_id = ?").get(workId)?.version ?? 0) + 1;
74
+ db.prepare(`INSERT INTO music_generation_jobs (id, conversation_id, transcript_id, work_id, parent_id, version_number, model_ref,
75
+ request_hash, title, mode, prompt, lyrics, state, next_attempt_at, created_at)
76
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'queued', ?, ?)`).run(id, conversationId, transcriptId, workId, parent?.id ?? null, version, route.ref, hash, title, mode, prompt, lyrics, now, now);
77
+ return { job: publicMusicJob(getMusicJob(conversationId, id)) };
78
+ });
79
+ }
80
+ function claimMusicJob() {
81
+ return runSqliteWriteTransaction((db) => {
82
+ const now = Date.now();
83
+ db.prepare(`UPDATE music_generation_jobs SET state = 'submission_unknown', lease_token = NULL, lease_until = NULL
84
+ WHERE state = 'submitting' AND lease_until <= ?`).run(now);
85
+ const row = db.prepare(`SELECT * FROM music_generation_jobs WHERE state IN ('queued', 'submitted', 'running', 'saving')
86
+ AND next_attempt_at <= ? AND (lease_until IS NULL OR lease_until <= ?) ORDER BY next_attempt_at, rowid LIMIT 1`).get(now, now);
87
+ if (!row) return void 0;
88
+ const token = randomUUID();
89
+ db.prepare("UPDATE music_generation_jobs SET lease_token = ?, lease_until = ? WHERE id = ?").run(token, now + 12e4, row.id);
90
+ return {
91
+ row,
92
+ token
93
+ };
94
+ });
95
+ }
96
+ function updateMusicJob(id, token, state, options = {}) {
97
+ const now = Date.now();
98
+ return getSqliteDatabase().prepare(`UPDATE music_generation_jobs SET state = ?, error = ?,
99
+ provider_task_id = COALESCE(?, provider_task_id), duration_ms = COALESCE(?, duration_ms), next_attempt_at = ?,
100
+ lease_token = CASE WHEN ? THEN lease_token END, lease_until = CASE WHEN ? THEN lease_until END
101
+ WHERE id = ? AND lease_token = ? AND lease_until > ?`).run(state, options.error ?? null, options.taskId ?? null, options.durationMs ?? null, now + (options.delayMs ?? 0), Number(options.keepLease ?? false), Number(options.keepLease ?? false), id, token, now).changes === 1;
102
+ }
103
+ //#endregion
104
+ export { claimMusicJob, enqueueMusicJob, getMusicJob, listMusicJobIds, listMusicJobs, publicMusicJob, updateMusicJob };
@@ -0,0 +1,7 @@
1
+ import type { MusicGenerationDefaults, MusicMode } from '@xopcai/gateway-contract';
2
+ import { CredentialResolver } from '../auth/credentials.js';
3
+ export declare function storedMusicModel(): string | undefined;
4
+ export declare function saveMusicModel(modelRef: string): void;
5
+ export declare function getMusicDefaults(modelRef: string, modes: readonly MusicMode[]): MusicGenerationDefaults;
6
+ export declare function saveMusicDefaults(modelRef: string, settings: MusicGenerationDefaults): void;
7
+ export declare function musicCredentials(): CredentialResolver;
@@ -0,0 +1,24 @@
1
+ import { DurableState } from "../storage/sqlite/durable-state.js";
2
+ import { CredentialResolver, init_credentials } from "../auth/credentials.js";
3
+ //#region src/music-generation/settings.ts
4
+ init_credentials();
5
+ const settings = new DurableState("music-generation");
6
+ const defaults = new DurableState("music-generation-defaults");
7
+ function storedMusicModel() {
8
+ return settings.get("model");
9
+ }
10
+ function saveMusicModel(modelRef) {
11
+ settings.set("model", modelRef);
12
+ }
13
+ function getMusicDefaults(modelRef, modes) {
14
+ const saved = defaults.get(modelRef);
15
+ return { mode: saved && modes.includes(saved.mode) ? saved.mode : modes[0] ?? "song" };
16
+ }
17
+ function saveMusicDefaults(modelRef, settings) {
18
+ defaults.set(modelRef, settings);
19
+ }
20
+ function musicCredentials() {
21
+ return new CredentialResolver();
22
+ }
23
+ //#endregion
24
+ export { getMusicDefaults, musicCredentials, saveMusicDefaults, saveMusicModel, storedMusicModel };
@@ -0,0 +1,8 @@
1
+ export declare class MusicGenerationWorker {
2
+ private timer?;
3
+ private work?;
4
+ start(): void;
5
+ stop(): Promise<void>;
6
+ private tick;
7
+ private runNext;
8
+ }
@@ -0,0 +1,115 @@
1
+ import { createLogger } from "../utils/logger/index.js";
2
+ import { init_logger } from "../utils/logger.js";
3
+ import { MusicRequestRejected } from "./provider.js";
4
+ import { resolveMusicKey, resolveMusicModel } from "./providers.js";
5
+ import { claimMusicJob, getMusicJob, updateMusicJob } from "./repository.js";
6
+ import { deleteMusicArtifact, musicArtifactExists, saveMusicArtifact } from "./artifact.js";
7
+ //#region src/music-generation/worker.ts
8
+ init_logger();
9
+ const log = createLogger("MusicGeneration");
10
+ const pollMs = 8e3;
11
+ const retryMs = 2e4;
12
+ const lifetimeMs = 720 * 6e4;
13
+ var MusicGenerationWorker = class {
14
+ timer;
15
+ work;
16
+ start() {
17
+ if (this.timer) return;
18
+ this.timer = setInterval(() => this.tick(), 1e3);
19
+ this.timer.unref();
20
+ this.tick();
21
+ }
22
+ async stop() {
23
+ clearInterval(this.timer);
24
+ this.timer = void 0;
25
+ await this.work;
26
+ }
27
+ tick() {
28
+ if (this.work) return;
29
+ this.work = this.runNext().catch((err) => log.error({ err }, "Music worker iteration failed")).finally(() => {
30
+ this.work = void 0;
31
+ });
32
+ }
33
+ async runNext() {
34
+ const claimed = claimMusicJob();
35
+ if (!claimed) return;
36
+ const { row, token } = claimed;
37
+ const { id, mode } = row;
38
+ if (row.state === "saving" && await musicArtifactExists(id)) {
39
+ updateMusicJob(id, token, "completed");
40
+ return;
41
+ }
42
+ if (Date.now() - row.created_at >= lifetimeMs) {
43
+ updateMusicJob(id, token, "failed", { error: "The music task or its download is no longer available." });
44
+ return;
45
+ }
46
+ const route = resolveMusicModel(row.model_ref);
47
+ const key = route && await resolveMusicKey(route.provider);
48
+ if (!route || !key) {
49
+ updateMusicJob(id, token, row.state, {
50
+ delayMs: retryMs,
51
+ error: "Restore the original music model credentials to resume."
52
+ });
53
+ return;
54
+ }
55
+ const { provider, model } = route;
56
+ if (row.state === "queued") {
57
+ if (!updateMusicJob(id, token, "submitting", { keepLease: true })) return;
58
+ try {
59
+ updateMusicJob(id, token, "submitted", {
60
+ taskId: await provider.submit(key, model, {
61
+ mode,
62
+ prompt: row.prompt,
63
+ lyrics: row.lyrics
64
+ }),
65
+ delayMs: pollMs
66
+ });
67
+ } catch (err) {
68
+ if (err instanceof MusicRequestRejected) {
69
+ updateMusicJob(id, token, "failed", { error: err.message });
70
+ log.warn({
71
+ jobId: id,
72
+ modelRef: row.model_ref
73
+ }, err.message);
74
+ } else {
75
+ updateMusicJob(id, token, "submission_unknown", { error: "The provider may have accepted this request." });
76
+ log.warn({
77
+ jobId: id,
78
+ modelRef: row.model_ref
79
+ }, "Music submission could not be confirmed");
80
+ }
81
+ }
82
+ return;
83
+ }
84
+ let saving = row.state === "saving";
85
+ try {
86
+ if (!row.provider_task_id) throw new Error("Missing remote music task ID.");
87
+ const result = await provider.query(key, mode, row.provider_task_id);
88
+ switch (result.state) {
89
+ case "submitted":
90
+ case "running":
91
+ updateMusicJob(id, token, result.state, { delayMs: pollMs });
92
+ return;
93
+ case "failed":
94
+ updateMusicJob(id, token, "failed", { error: result.reason });
95
+ return;
96
+ case "completed":
97
+ if (!updateMusicJob(id, token, "saving", {
98
+ durationMs: result.durationMs,
99
+ keepLease: true
100
+ })) return;
101
+ saving = true;
102
+ await saveMusicArtifact(id, provider, result.url, () => updateMusicJob(id, token, "saving", { keepLease: true }));
103
+ if (await musicArtifactExists(id)) updateMusicJob(id, token, "completed");
104
+ if (!getMusicJob(row.conversation_id, id)) await deleteMusicArtifact(id);
105
+ }
106
+ } catch {
107
+ updateMusicJob(id, token, saving ? "saving" : row.state, {
108
+ delayMs: retryMs,
109
+ error: saving ? "Audio download failed. Retrying the same result." : "Status check failed. Retrying."
110
+ });
111
+ }
112
+ }
113
+ };
114
+ //#endregion
115
+ export { MusicGenerationWorker };
@@ -1,3 +1,3 @@
1
- export declare const PERSONAL_MAIN_TOOL_IDS: readonly ["personal_task", "personal_attention", "personal_capability", "personal_request", "session_recall", "read_media", "read_file", "session_search", "personal_read", "personal_result", "user_context_read", "knowledge_read", "personal_memory"];
1
+ export declare const PERSONAL_MAIN_TOOL_IDS: readonly ["personal_task", "personal_attention", "personal_capability", "personal_request", "session_recall", "read_media", "read_file", "session_search", "personal_read", "personal_result", "user_context_read", "knowledge_read", "personal_memory", "video_draft", "music_generation"];
2
2
  export declare const PERSONAL_TOOL_DESCRIPTIONS: Readonly<Record<string, string>>;
3
3
  export declare const PERSONAL_READ_TOOL_ALIASES: Readonly<Record<string, string>>;
@@ -12,7 +12,9 @@ const PERSONAL_MAIN_TOOL_IDS = [
12
12
  "personal_result",
13
13
  "user_context_read",
14
14
  "knowledge_read",
15
- "personal_memory"
15
+ "personal_memory",
16
+ "video_draft",
17
+ "music_generation"
16
18
  ];
17
19
  const PERSONAL_TOOL_DESCRIPTIONS = {
18
20
  session_recall: "Find exact facts in this conversation, including compacted history. Cannot access other chats.",
@@ -100,6 +100,8 @@ function projectPersonalInstructions(instructions, toolNames) {
100
100
  if (group.requires.every((tool) => tools.has(tool))) continue;
101
101
  for (const rule of group.rules) projected = omitInstructionBlock(projected, rule);
102
102
  }
103
+ if (tools.has("music_generation")) projected += "\nUse music_generation for song or instrumental requests. Explicit requests can enqueue directly. If the user asks to review lyrics first, present them in chat and wait for their request to generate.";
104
+ if (tools.has("video_draft")) projected += "\nFor video generation in this chat, use video_draft directly to prepare or revise the draft and let the user confirm it in chat.";
103
105
  return projected;
104
106
  }
105
107
  const PERSONAL_MEMORY_RULE = "A self-description or preference without a request to persist it is handled by background learning; adapt to it in the current reply, do not call personal_memory or claim it has already been saved. For an explicit request to remember, correct, forget, or limit saved memory, use personal_memory directly instead of delegating or promising background learning. Quote the current owner request in userEvidence. User identity/preferences use target=user; project facts, decisions and lessons use target=knowledge. Before changing an existing item read its id and version using the matching read tool. Resolve scope from what the user asks: global, current agent, workspace, project or session. Never broaden a local request. Missing current project is an error, not permission to fall back globally. Only say saved after status=saved with usable=true; pending means saved for review but not in use. A receipt records a past commit; available/usable describe current state on retries. Forgotten means future automatic recall stops, while originals and already running prompts may retain content. Keep acknowledgements short, name the actual scope, and offer correction only when useful. Memory cannot authorize external actions.";
@@ -20,11 +20,14 @@ import { ACCEPTANCE_REPAIR_READ_TOOLS, canResumeAfterModelFailure } from "../age
20
20
  import { transcriptRowsToClientHistory } from "./client-history.js";
21
21
  import { SessionCompactor } from "../agent/memory/compaction.js";
22
22
  import { resolveCompactionPolicy } from "../agent/memory/compaction-policy.js";
23
+ import { listMusicJobIds } from "../music-generation/repository.js";
24
+ import { listVideoJobIds } from "../video-generation/repository.js";
25
+ import { deleteVideoDraft } from "../video-generation/drafts.js";
23
26
  import { IdleCompactionScheduler } from "../agent/memory/idle-compaction-scheduler.js";
24
27
  import { readPostCompactionContext } from "../agent/reply/post-compaction-context.js";
25
28
  import { promoteCompactionLedger } from "../agent/memory/compaction-promotion.js";
29
+ import { deleteMusicArtifact } from "../music-generation/artifact.js";
26
30
  import { deleteVideoArtifact } from "../video-generation/artifact.js";
27
- import { listVideoJobIds } from "../video-generation/repository.js";
28
31
  import { SlidingWindow } from "../agent/memory/window.js";
29
32
  import { isAppendOnlyLlmTranscriptMessage } from "./transcript-stats.js";
30
33
  import { compactHistory } from "./compact-history.js";
@@ -338,6 +341,7 @@ var SessionStore = class {
338
341
  requireXopcDatabase();
339
342
  const task = resetSessionRecord(key, this.resolveWorkspaceCwd(key));
340
343
  if (task) {
344
+ deleteVideoDraft(key);
341
345
  this.clearCompactionCheckpoint(key);
342
346
  log.info({
343
347
  key,
@@ -352,9 +356,11 @@ var SessionStore = class {
352
356
  return this.runStoreMutation(async () => {
353
357
  requireXopcDatabase();
354
358
  const videoJobs = listVideoJobIds(key);
359
+ const musicJobs = listMusicJobIds(key);
355
360
  const ok = deleteSessionRecord(key);
356
361
  if (ok) {
357
- await Promise.allSettled(videoJobs.map(deleteVideoArtifact));
362
+ deleteVideoDraft(key);
363
+ await Promise.allSettled([...videoJobs.map(deleteVideoArtifact), ...musicJobs.map(deleteMusicArtifact)]);
358
364
  this.clearCompactionCheckpoint(key);
359
365
  log.info({ key }, "Session deleted");
360
366
  }
@@ -1,7 +1,7 @@
1
1
  import type { DatabaseSync } from 'node:sqlite';
2
2
  export { SCHEMA_META_SCHEMA_VERSION_KEY, readSchemaVersionForTest, readSchemaVersion, setSchemaVersion, ensureSchemaMetaTable } from './schema-version.js';
3
- export declare const XOPC_DB_SCHEMA_VERSION = 276;
4
- export declare const XOPC_DB_BASELINE_SCHEMA_VERSION = 276;
3
+ export declare const XOPC_DB_SCHEMA_VERSION = 277;
4
+ export declare const XOPC_DB_BASELINE_SCHEMA_VERSION = 277;
5
5
  export { XOPC_DB_MINIMUM_SCHEMA_VERSION } from './schema-upgrade.js';
6
6
  /** Initialize fresh databases directly; upgrade supported installed databases transactionally. */
7
7
  export declare function ensureXopcDatabaseSchema(db: DatabaseSync): void;
@@ -14,18 +14,18 @@ function ensureXopcDatabaseSchema(db) {
14
14
  const hasMetadata = tables.some((row) => row.name === "schema_meta");
15
15
  if (!hasMetadata && tables.length) throw new Error("An unversioned database cannot be initialized");
16
16
  const version = hasMetadata ? readSchemaVersion(db) : 0;
17
- if (version > 276) throw new DatabaseSchemaTooNewError(version, 276);
17
+ if (version > 277) throw new DatabaseSchemaTooNewError(version, 277);
18
18
  if (version > 0 && version < 202) throw new DatabaseSchemaTooOldError(version, 202);
19
- if (version >= 202 && version < 276) {
20
- upgradeXopcDatabase(db, version, 276);
19
+ if (version >= 202 && version < 277) {
20
+ upgradeXopcDatabase(db, version, 277);
21
21
  return;
22
22
  }
23
- if (version === 276) return;
23
+ if (version === 277) return;
24
24
  if (db.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'schema_meta' AND name NOT LIKE 'sqlite_%' LIMIT 1").get()) throw new Error("An unversioned database cannot be initialized");
25
25
  db.exec("BEGIN IMMEDIATE");
26
26
  try {
27
27
  db.exec(readSqliteAsset("schema.sql"));
28
- setSchemaVersion(db, 276);
28
+ setSchemaVersion(db, 277);
29
29
  db.exec("COMMIT");
30
30
  } catch (error) {
31
31
  db.exec("ROLLBACK");
@@ -40,8 +40,8 @@ var init_schema = __esmMin((() => {
40
40
  init_schema_version();
41
41
  init_schema_errors();
42
42
  init_schema_upgrade();
43
- XOPC_DB_SCHEMA_VERSION = 276;
44
- XOPC_DB_BASELINE_SCHEMA_VERSION = 276;
43
+ XOPC_DB_SCHEMA_VERSION = 277;
44
+ XOPC_DB_BASELINE_SCHEMA_VERSION = 277;
45
45
  }));
46
46
  //#endregion
47
47
  init_schema();
@@ -3738,3 +3738,34 @@ CREATE TABLE video_generation_provider_budget (
3738
3738
  reserved_fen INTEGER NOT NULL,
3739
3739
  PRIMARY KEY (provider, day)
3740
3740
  );
3741
+
3742
+ CREATE TABLE music_generation_jobs (
3743
+ id TEXT PRIMARY KEY,
3744
+ conversation_id TEXT NOT NULL REFERENCES sessions(conversation_id) ON DELETE CASCADE,
3745
+ transcript_id TEXT NOT NULL,
3746
+ work_id TEXT NOT NULL,
3747
+ parent_id TEXT,
3748
+ version_number INTEGER NOT NULL,
3749
+ model_ref TEXT NOT NULL,
3750
+ request_hash TEXT NOT NULL,
3751
+ title TEXT NOT NULL,
3752
+ mode TEXT NOT NULL CHECK (mode IN ('song', 'instrumental')),
3753
+ prompt TEXT NOT NULL,
3754
+ lyrics TEXT,
3755
+ state TEXT NOT NULL CHECK (state IN ('queued', 'submitting', 'submitted', 'running', 'saving', 'completed', 'failed', 'submission_unknown')),
3756
+ provider_task_id TEXT,
3757
+ duration_ms INTEGER,
3758
+ error TEXT,
3759
+ next_attempt_at INTEGER NOT NULL,
3760
+ lease_token TEXT,
3761
+ lease_until INTEGER,
3762
+ created_at INTEGER NOT NULL,
3763
+ UNIQUE (work_id, version_number)
3764
+ );
3765
+ CREATE INDEX music_generation_due ON music_generation_jobs(next_attempt_at)
3766
+ WHERE state IN ('queued', 'submitting', 'submitted', 'running', 'saving');
3767
+ CREATE INDEX music_generation_transcript ON music_generation_jobs(conversation_id, transcript_id, created_at DESC);
3768
+ CREATE TABLE music_generation_daily_usage (
3769
+ day INTEGER PRIMARY KEY,
3770
+ count INTEGER NOT NULL
3771
+ );
@@ -38,6 +38,7 @@ export declare function searchSessionTranscript(conversationId: string, query: s
38
38
  matchMode?: 'keyword' | 'phrase';
39
39
  }): SessionTranscriptRecallMatch[];
40
40
  export declare function loadTranscriptRowsForSession(conversationId: string): TranscriptStoredRow[];
41
+ export declare function loadRecentUserTranscriptRows(conversationId: string, limit: number): TranscriptStoredRow[];
41
42
  export declare function loadTranscriptHistoryRowsForSession(conversationId: string): TranscriptStoredRow[];
42
43
  export declare function loadTranscriptRows(transcriptId: string): TranscriptStoredRow[];
43
44
  export declare function loadLlmMessagesForSession(conversationId: string): AgentMessage[];
@@ -254,6 +254,13 @@ function loadTranscriptRowsForSession(conversationId) {
254
254
  if (!transcriptId) return [];
255
255
  return loadTranscriptRows(transcriptId);
256
256
  }
257
+ function loadRecentUserTranscriptRows(conversationId, limit) {
258
+ const transcriptId = getCurrentTranscriptId(conversationId);
259
+ if (!transcriptId) return [];
260
+ return getSqliteDatabase().prepare(`SELECT entry_id, transcript_id, seq, entry_kind, role, payload_json, created_at
261
+ FROM transcript_entries WHERE transcript_id = ? AND entry_kind = 'message' AND role = 'user'
262
+ ORDER BY seq DESC LIMIT ?`).all(transcriptId, limit).map(transcriptEntryRowToStoredRow);
263
+ }
257
264
  function loadTranscriptHistoryRowsForSession(conversationId) {
258
265
  if (!getCurrentTranscriptId(conversationId)) return [];
259
266
  return getSqliteDatabase().prepare(`SELECT e.entry_id, e.transcript_id, e.seq, e.entry_kind, e.role, e.payload_json, e.created_at
@@ -492,4 +499,4 @@ function restoreBeforeCompactionBoundary(conversationId, compactionId) {
492
499
  });
493
500
  }
494
501
  //#endregion
495
- export { appendCompactionBoundaryIfUnchanged, appendMediaToAssistantTranscriptEntry, appendTranscriptEntry, archiveFailedModelAttempts, findLatestAssistantTranscriptEntryId, listCompactionBoundaries, loadCompactionSourceSnapshot, loadLlmMessagesForSession, loadTranscriptHistoryRowsForSession, loadTranscriptRows, loadTranscriptRowsForSession, paginateTranscriptMessages, replaceTranscriptRows, restoreBeforeCompactionBoundary, searchSessionTranscript };
502
+ export { appendCompactionBoundaryIfUnchanged, appendMediaToAssistantTranscriptEntry, appendTranscriptEntry, archiveFailedModelAttempts, findLatestAssistantTranscriptEntryId, listCompactionBoundaries, loadCompactionSourceSnapshot, loadLlmMessagesForSession, loadRecentUserTranscriptRows, loadTranscriptHistoryRowsForSession, loadTranscriptRows, loadTranscriptRowsForSession, paginateTranscriptMessages, replaceTranscriptRows, restoreBeforeCompactionBoundary, searchSessionTranscript };
@@ -0,0 +1,30 @@
1
+ CREATE TABLE music_generation_jobs (
2
+ id TEXT PRIMARY KEY,
3
+ conversation_id TEXT NOT NULL REFERENCES sessions(conversation_id) ON DELETE CASCADE,
4
+ transcript_id TEXT NOT NULL,
5
+ work_id TEXT NOT NULL,
6
+ parent_id TEXT,
7
+ version_number INTEGER NOT NULL,
8
+ model_ref TEXT NOT NULL,
9
+ request_hash TEXT NOT NULL,
10
+ title TEXT NOT NULL,
11
+ mode TEXT NOT NULL CHECK (mode IN ('song', 'instrumental')),
12
+ prompt TEXT NOT NULL,
13
+ lyrics TEXT,
14
+ state TEXT NOT NULL CHECK (state IN ('queued', 'submitting', 'submitted', 'running', 'saving', 'completed', 'failed', 'submission_unknown')),
15
+ provider_task_id TEXT,
16
+ duration_ms INTEGER,
17
+ error TEXT,
18
+ next_attempt_at INTEGER NOT NULL,
19
+ lease_token TEXT,
20
+ lease_until INTEGER,
21
+ created_at INTEGER NOT NULL,
22
+ UNIQUE (work_id, version_number)
23
+ );
24
+ CREATE INDEX music_generation_due ON music_generation_jobs(next_attempt_at)
25
+ WHERE state IN ('queued', 'submitting', 'submitted', 'running', 'saving');
26
+ CREATE INDEX music_generation_transcript ON music_generation_jobs(conversation_id, transcript_id, created_at DESC);
27
+ CREATE TABLE music_generation_daily_usage (
28
+ day INTEGER PRIMARY KEY,
29
+ count INTEGER NOT NULL
30
+ );