@axium/ai 0.0.0-stage → 0.0.1
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.
- package/LICENSE.md +157 -0
- package/README.md +2 -2
- package/db.json +114 -0
- package/dist/.locale.d.ts +300 -0
- package/dist/.locale.js +1 -0
- package/dist/client/cli.d.ts +1 -0
- package/dist/client/cli.js +224 -0
- package/dist/client/config.d.ts +196 -0
- package/dist/client/config.js +55 -0
- package/dist/client/hooks.d.ts +3 -0
- package/dist/client/hooks.js +32 -0
- package/dist/client/local.d.ts +17 -0
- package/dist/client/local.js +112 -0
- package/dist/common.d.ts +1318 -0
- package/dist/common.js +569 -0
- package/dist/hooks.d.ts +13 -0
- package/dist/hooks.js +34 -0
- package/dist/hub.d.ts +4 -0
- package/dist/hub.js +75 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/integrations/notes.d.ts +1 -0
- package/dist/integrations/notes.js +104 -0
- package/dist/integrations/tasks.d.ts +1 -0
- package/dist/integrations/tasks.js +157 -0
- package/dist/loop.d.ts +75 -0
- package/dist/loop.js +238 -0
- package/dist/memory.d.ts +107 -0
- package/dist/memory.js +193 -0
- package/dist/node/chat.d.ts +50 -0
- package/dist/node/chat.js +60 -0
- package/dist/node/llama.d.ts +115 -0
- package/dist/node/llama.js +385 -0
- package/dist/node/markdown.d.ts +6 -0
- package/dist/node/markdown.js +285 -0
- package/dist/node/models.d.ts +34 -0
- package/dist/node/models.js +287 -0
- package/dist/node/web.d.ts +1 -0
- package/dist/node/web.js +203 -0
- package/dist/server/api/conversations.d.ts +1 -0
- package/dist/server/api/conversations.js +216 -0
- package/dist/server/api/gateway.d.ts +1 -0
- package/dist/server/api/gateway.js +113 -0
- package/dist/server/api/memories.d.ts +1 -0
- package/dist/server/api/memories.js +129 -0
- package/dist/server/api/models.d.ts +1 -0
- package/dist/server/api/models.js +161 -0
- package/dist/server/api/temporary.d.ts +1 -0
- package/dist/server/api/temporary.js +50 -0
- package/dist/server/api/timers.d.ts +1 -0
- package/dist/server/api/timers.js +27 -0
- package/dist/server/api/voice.d.ts +1 -0
- package/dist/server/api/voice.js +188 -0
- package/dist/server/api.d.ts +1 -0
- package/dist/server/api.js +157 -0
- package/dist/server/generation.d.ts +38 -0
- package/dist/server/generation.js +299 -0
- package/dist/server/memory.d.ts +7 -0
- package/dist/server/memory.js +218 -0
- package/dist/server/models.d.ts +13 -0
- package/dist/server/models.js +59 -0
- package/dist/server/preferences.d.ts +3 -0
- package/dist/server/preferences.js +12 -0
- package/dist/server/timers.d.ts +7 -0
- package/dist/server/timers.js +66 -0
- package/dist/speech.d.ts +17 -0
- package/dist/speech.js +69 -0
- package/dist/timers.d.ts +1 -0
- package/dist/timers.js +67 -0
- package/dist/tools.d.ts +70 -0
- package/dist/tools.js +84 -0
- package/lib/ChatMessage.svelte +331 -0
- package/lib/Composer.svelte +234 -0
- package/lib/Conversation.svelte +433 -0
- package/lib/ConversationMenu.svelte +18 -0
- package/lib/ModelActions.svelte +71 -0
- package/lib/ModelOptions.svelte +101 -0
- package/lib/ModelStatus.svelte +49 -0
- package/lib/ToolCall.svelte +171 -0
- package/lib/VoiceMode.svelte +113 -0
- package/lib/actions.ts +21 -0
- package/lib/index.ts +7 -0
- package/lib/live.svelte.ts +113 -0
- package/lib/markdown.ts +40 -0
- package/lib/preferences.ts +13 -0
- package/lib/transition.ts +24 -0
- package/lib/tsconfig.json +14 -0
- package/lib/voice.svelte.ts +476 -0
- package/locales/en.json +316 -0
- package/package.json +139 -5
- package/routes/ai/+layout.svelte +177 -0
- package/routes/ai/+layout.ts +26 -0
- package/routes/ai/+page.svelte +109 -0
- package/routes/ai/TimerAlerts.svelte +51 -0
- package/routes/ai/Timers.svelte +121 -0
- package/routes/ai/[id]/+page.svelte +18 -0
- package/routes/ai/[id]/+page.ts +14 -0
- package/routes/ai/memory/+page.svelte +313 -0
- package/routes/ai/memory/+page.ts +10 -0
- package/routes/ai/memory/MemoryForm.svelte +111 -0
- package/routes/ai/models/+page.svelte +199 -0
- package/routes/ai/models/+page.ts +5 -0
- package/routes/ai/models/AddModel.svelte +240 -0
- package/routes/ai/models/MemoryMeter.svelte +120 -0
- package/routes/ai/models/Roles.svelte +159 -0
- package/routes/ai/models/[id]/+page.svelte +331 -0
- package/routes/ai/models/[id]/+page.ts +6 -0
- package/routes/ai/timers.svelte.ts +94 -0
- package/routes/tsconfig.json +14 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type * as ai from '@axium/ai/common';
|
|
3
|
+
import { text } from '@axium/client';
|
|
4
|
+
import { Icon } from '@axium/client/components';
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
call,
|
|
8
|
+
result,
|
|
9
|
+
decision,
|
|
10
|
+
ondecide,
|
|
11
|
+
}: {
|
|
12
|
+
call: ai.ToolCall;
|
|
13
|
+
/** The call's tool message, once it exists */
|
|
14
|
+
result?: ai.Message;
|
|
15
|
+
/** The user's choice while other calls still await one */
|
|
16
|
+
decision?: ai.Decision;
|
|
17
|
+
ondecide?(decision: ai.Decision): void;
|
|
18
|
+
} = $props();
|
|
19
|
+
|
|
20
|
+
const choices = ['deny', 'once', 'chat', 'always'] as const satisfies ai.Decision[];
|
|
21
|
+
|
|
22
|
+
const status = $derived(result?.status ?? 'pending');
|
|
23
|
+
|
|
24
|
+
const icons: Record<ai.MessageStatus, string> = {
|
|
25
|
+
pending: 'spinner-third',
|
|
26
|
+
done: 'check',
|
|
27
|
+
error: 'xmark',
|
|
28
|
+
cancelled: 'ban',
|
|
29
|
+
awaiting: 'hand',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const input = $derived.by((): unknown => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(call.arguments || '{}');
|
|
35
|
+
} catch {
|
|
36
|
+
return call.arguments;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/** The first text in the input, e.g. the URL being read */
|
|
41
|
+
const preview = $derived(input && typeof input == 'object' ? Object.values(input).find(value => typeof value == 'string') : undefined);
|
|
42
|
+
|
|
43
|
+
/** Characters of a result shown */
|
|
44
|
+
const maxShown = 4000;
|
|
45
|
+
|
|
46
|
+
function pretty(value: string): string {
|
|
47
|
+
let shown: string;
|
|
48
|
+
try {
|
|
49
|
+
shown = JSON.stringify(JSON.parse(value), null, 2);
|
|
50
|
+
} catch {
|
|
51
|
+
shown = value;
|
|
52
|
+
}
|
|
53
|
+
return shown.length > maxShown ? shown.slice(0, maxShown) + '\n…' : shown;
|
|
54
|
+
}
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<div class={['ToolCall', status]}>
|
|
58
|
+
<details open={status == 'awaiting'}>
|
|
59
|
+
<summary>
|
|
60
|
+
<Icon i={icons[status]} --size="14px" class={status == 'pending' ? 'spin' : undefined} />
|
|
61
|
+
<span class="name">{text(`ai.tool.${call.name}`, { $default: call.name })}</span>
|
|
62
|
+
{#if preview}<span class="preview subtle">{preview}</span>{/if}
|
|
63
|
+
</summary>
|
|
64
|
+
<div class="details">
|
|
65
|
+
<span class="subtle">{text('ai.tool_input')}</span>
|
|
66
|
+
<pre>{typeof input == 'string' ? input : JSON.stringify(input, null, 2)}</pre>
|
|
67
|
+
{#if result?.status == 'done'}
|
|
68
|
+
<span class="subtle">{text('ai.tool_result')}</span>
|
|
69
|
+
<pre>{pretty(result.content)}</pre>
|
|
70
|
+
{/if}
|
|
71
|
+
</div>
|
|
72
|
+
</details>
|
|
73
|
+
|
|
74
|
+
{#if result?.status == 'error' && result.error}
|
|
75
|
+
<div class="error">{result.error}</div>
|
|
76
|
+
{/if}
|
|
77
|
+
|
|
78
|
+
{#if status == 'awaiting'}
|
|
79
|
+
<div class="approval">
|
|
80
|
+
<span>{text('ai.tool_awaiting')}</span>
|
|
81
|
+
{#each choices as choice}
|
|
82
|
+
<button class={[choice != 'deny' && 'approve', decision == choice && 'chosen']} onclick={() => ondecide?.(choice)}>
|
|
83
|
+
{text(`ai.tool_decision.${choice}`)}
|
|
84
|
+
</button>
|
|
85
|
+
{/each}
|
|
86
|
+
</div>
|
|
87
|
+
{/if}
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
<style>
|
|
91
|
+
.ToolCall {
|
|
92
|
+
display: flex;
|
|
93
|
+
flex-direction: column;
|
|
94
|
+
gap: 0.5em;
|
|
95
|
+
padding: 0.5em 0.75em;
|
|
96
|
+
border-radius: 0.75em;
|
|
97
|
+
border: var(--border-accent);
|
|
98
|
+
font-size: 0.9em;
|
|
99
|
+
|
|
100
|
+
&.awaiting {
|
|
101
|
+
border: var(--border-strong);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
summary {
|
|
106
|
+
display: flex;
|
|
107
|
+
align-items: center;
|
|
108
|
+
gap: 0.5em;
|
|
109
|
+
cursor: pointer;
|
|
110
|
+
white-space: nowrap;
|
|
111
|
+
overflow: hidden;
|
|
112
|
+
|
|
113
|
+
.preview {
|
|
114
|
+
overflow: hidden;
|
|
115
|
+
text-overflow: ellipsis;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
.details {
|
|
120
|
+
display: flex;
|
|
121
|
+
flex-direction: column;
|
|
122
|
+
gap: 0.25em;
|
|
123
|
+
margin-top: 0.5em;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pre {
|
|
127
|
+
margin: 0 0 0.5em;
|
|
128
|
+
padding: 0.5em 0.75em;
|
|
129
|
+
border-radius: 0.5em;
|
|
130
|
+
background-color: var(--bg-alt);
|
|
131
|
+
max-height: 20em;
|
|
132
|
+
overflow: auto;
|
|
133
|
+
white-space: pre-wrap;
|
|
134
|
+
overflow-wrap: anywhere;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
.approval {
|
|
138
|
+
display: flex;
|
|
139
|
+
flex-wrap: wrap;
|
|
140
|
+
align-items: center;
|
|
141
|
+
gap: 0.5em;
|
|
142
|
+
|
|
143
|
+
span {
|
|
144
|
+
flex: 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.chosen {
|
|
148
|
+
border: var(--border-strong);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.approve {
|
|
152
|
+
background-color: var(--bg-accent);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.error {
|
|
157
|
+
color: var(--fg-error);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@media not (prefers-reduced-motion) {
|
|
161
|
+
.ToolCall :global(.spin) {
|
|
162
|
+
animation: spin 1s linear infinite;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
@keyframes spin {
|
|
167
|
+
to {
|
|
168
|
+
rotate: 360deg;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
</style>
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { text } from '@axium/client';
|
|
3
|
+
import { Icon } from '@axium/client/components';
|
|
4
|
+
import * as voice from './voice.svelte.js';
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
generating = false,
|
|
8
|
+
onstop,
|
|
9
|
+
}: {
|
|
10
|
+
generating?: boolean;
|
|
11
|
+
/** Stops the response being generated */
|
|
12
|
+
onstop?(): void;
|
|
13
|
+
} = $props();
|
|
14
|
+
|
|
15
|
+
const { conversation } = voice;
|
|
16
|
+
|
|
17
|
+
const icons: Record<voice.Phase, string> = {
|
|
18
|
+
idle: 'microphone',
|
|
19
|
+
listening: 'waveform-lines',
|
|
20
|
+
transcribing: 'ellipsis',
|
|
21
|
+
responding: 'ellipsis',
|
|
22
|
+
speaking: 'volume-high',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function press() {
|
|
26
|
+
const interrupting = conversation.phase == 'responding' || conversation.phase == 'speaking';
|
|
27
|
+
if (interrupting && generating) onstop?.();
|
|
28
|
+
voice.toggle();
|
|
29
|
+
}
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<div class="VoiceMode">
|
|
33
|
+
<button
|
|
34
|
+
type="button"
|
|
35
|
+
class={['talk', conversation.phase]}
|
|
36
|
+
style:--level={conversation.level}
|
|
37
|
+
title={text(`ai.voice.action.${conversation.phase}`)}
|
|
38
|
+
aria-label={text(`ai.voice.action.${conversation.phase}`)}
|
|
39
|
+
disabled={conversation.phase == 'transcribing'}
|
|
40
|
+
onclick={press}
|
|
41
|
+
>
|
|
42
|
+
<Icon i={icons[conversation.phase]} --size="1.75em" />
|
|
43
|
+
</button>
|
|
44
|
+
<div class="status" aria-live="polite">
|
|
45
|
+
{#if conversation.error}
|
|
46
|
+
<span class="error">{conversation.error}</span>
|
|
47
|
+
{:else}
|
|
48
|
+
<span>{text(`ai.voice.phase.${conversation.phase}`)}</span>
|
|
49
|
+
{/if}
|
|
50
|
+
</div>
|
|
51
|
+
<button type="button" class="end icon-text" onclick={voice.endConversation}>
|
|
52
|
+
<Icon i="xmark" />
|
|
53
|
+
<span>{text('ai.voice.end')}</span>
|
|
54
|
+
</button>
|
|
55
|
+
</div>
|
|
56
|
+
|
|
57
|
+
<style>
|
|
58
|
+
.VoiceMode {
|
|
59
|
+
display: flex;
|
|
60
|
+
align-items: center;
|
|
61
|
+
gap: 1em;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.talk {
|
|
65
|
+
display: inline-flex;
|
|
66
|
+
align-items: center;
|
|
67
|
+
justify-content: center;
|
|
68
|
+
width: 4em;
|
|
69
|
+
height: 4em;
|
|
70
|
+
border-radius: 50%;
|
|
71
|
+
border: none;
|
|
72
|
+
background-color: var(--bg-accent);
|
|
73
|
+
--shadow-color: var(--bg-accent);
|
|
74
|
+
box-shadow: 0 0 0 calc(var(--level, 0) * 0.75em) color-mix(in srgb, var(--shadow-color) 50%, transparent);
|
|
75
|
+
transition: box-shadow 0.1s;
|
|
76
|
+
|
|
77
|
+
&.listening {
|
|
78
|
+
background-color: var(--bg-error);
|
|
79
|
+
--shadow-color: var(--bg-error);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
&.transcribing,
|
|
83
|
+
&.responding {
|
|
84
|
+
animation: pulse 1.5s ease-in-out infinite;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
&:disabled {
|
|
88
|
+
cursor: progress;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.status {
|
|
93
|
+
flex: 1;
|
|
94
|
+
min-width: 0;
|
|
95
|
+
|
|
96
|
+
.error {
|
|
97
|
+
color: var(--fg-error);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@keyframes pulse {
|
|
102
|
+
50% {
|
|
103
|
+
opacity: 60%;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
@media (prefers-reduced-motion: reduce) {
|
|
108
|
+
.talk {
|
|
109
|
+
animation: none !important;
|
|
110
|
+
transition: none;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
</style>
|
package/lib/actions.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { goto, invalidate } from '$app/navigation';
|
|
2
|
+
import { page } from '$app/state';
|
|
3
|
+
import { fetchAPI, text } from '@axium/client';
|
|
4
|
+
import { toastStatus } from '@axium/client/toast';
|
|
5
|
+
|
|
6
|
+
export function renameConversation(id: string, title: string): Promise<unknown> {
|
|
7
|
+
return toastStatus(
|
|
8
|
+
fetchAPI('PATCH', 'ai/conversations/:id', { title }, id).then(() => invalidate('ai:conversations')),
|
|
9
|
+
text('ai.toast_renamed')
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function deleteConversation(id: string): Promise<unknown> {
|
|
14
|
+
return toastStatus(
|
|
15
|
+
fetchAPI('DELETE', 'ai/conversations/:id', {}, id).then(async () => {
|
|
16
|
+
await invalidate('ai:conversations');
|
|
17
|
+
if (page.params.id == id) await goto('/ai');
|
|
18
|
+
}),
|
|
19
|
+
text('ai.toast_deleted')
|
|
20
|
+
);
|
|
21
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as ChatMessage } from './ChatMessage.svelte';
|
|
2
|
+
export { default as Composer } from './Composer.svelte';
|
|
3
|
+
export { default as Conversation } from './Conversation.svelte';
|
|
4
|
+
export { default as ConversationMenu } from './ConversationMenu.svelte';
|
|
5
|
+
export { default as ModelActions } from './ModelActions.svelte';
|
|
6
|
+
export { default as ModelOptions } from './ModelOptions.svelte';
|
|
7
|
+
export { default as ModelStatus } from './ModelStatus.svelte';
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type * as ai from '@axium/ai/common';
|
|
2
|
+
import type * as memory from '@axium/ai/memory';
|
|
3
|
+
import { addListener, connect, onConnect } from '@axium/client/socket';
|
|
4
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
5
|
+
|
|
6
|
+
export interface ConversationWatcher {
|
|
7
|
+
message(message: ai.Message): void;
|
|
8
|
+
delta(delta: ai.Delta): void;
|
|
9
|
+
/** Messages were deleted, e.g. those after an edited message */
|
|
10
|
+
remove(ids: string[]): void;
|
|
11
|
+
/** Updates sent while disconnected were missed */
|
|
12
|
+
reconnect(): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const watchers = new Map<string, Set<ConversationWatcher>>();
|
|
16
|
+
|
|
17
|
+
const conversationListeners = new Set<(conversation: ai.Conversation) => void>();
|
|
18
|
+
|
|
19
|
+
const memoryUpdateListeners = new Set<(status: memory.Updates) => void>();
|
|
20
|
+
|
|
21
|
+
/** Models that changed since the page loaded, by ID; `null` for removed models */
|
|
22
|
+
export const modelUpdates = new SvelteMap<string, ai.Model | null>();
|
|
23
|
+
|
|
24
|
+
let listening = false;
|
|
25
|
+
|
|
26
|
+
/** Starts receiving live updates. Safe to call more than once. */
|
|
27
|
+
export function listen(): void {
|
|
28
|
+
if (listening) return;
|
|
29
|
+
listening = true;
|
|
30
|
+
|
|
31
|
+
addListener('ai.message', message => {
|
|
32
|
+
for (const watcher of watchers.get(message.conversationId) ?? []) watcher.message(message);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
addListener('ai.delta', delta => {
|
|
36
|
+
for (const watcher of watchers.get(delta.conversationId) ?? []) watcher.delta(delta);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
addListener('ai.message.remove', ({ conversationId, ids }) => {
|
|
40
|
+
for (const watcher of watchers.get(conversationId) ?? []) watcher.remove(ids);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
addListener('ai.conversation', conversation => {
|
|
44
|
+
for (const listener of conversationListeners) listener(conversation);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
addListener('ai.memory_updates', status => {
|
|
48
|
+
for (const listener of memoryUpdateListeners) listener(status);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
addListener('ai.model', model => modelUpdates.set(model.id, model));
|
|
52
|
+
addListener('ai.model.remove', id => modelUpdates.set(id, null));
|
|
53
|
+
|
|
54
|
+
onConnect(() => {
|
|
55
|
+
for (const set of watchers.values()) for (const watcher of set) watcher.reconnect();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
void connect().catch(() => null);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Receive changes to the current user's conversations.
|
|
63
|
+
* @returns a function that stops listening
|
|
64
|
+
*/
|
|
65
|
+
export function onConversationChange(listener: (conversation: ai.Conversation) => void): () => void {
|
|
66
|
+
listen();
|
|
67
|
+
conversationListeners.add(listener);
|
|
68
|
+
return () => conversationListeners.delete(listener);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Receive the state of the current user's periodic memory updates when an update starts or finishes.
|
|
73
|
+
* @returns a function that stops listening
|
|
74
|
+
*/
|
|
75
|
+
export function onMemoryUpdates(listener: (status: memory.Updates) => void): () => void {
|
|
76
|
+
listen();
|
|
77
|
+
memoryUpdateListeners.add(listener);
|
|
78
|
+
return () => memoryUpdateListeners.delete(listener);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** `models` with the live updates received since they were fetched */
|
|
82
|
+
export function withUpdates(models: ai.Model[]): ai.Model[] {
|
|
83
|
+
listen();
|
|
84
|
+
|
|
85
|
+
const updated: ai.Model[] = [];
|
|
86
|
+
for (const model of models) {
|
|
87
|
+
const update = modelUpdates.get(model.id);
|
|
88
|
+
if (update !== null) updated.push(update ?? model);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const [id, update] of modelUpdates) {
|
|
92
|
+
if (update && !models.some(model => model.id == id)) updated.push(update);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return updated;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Receive live updates to a conversation's messages.
|
|
100
|
+
* @returns a function that stops watching
|
|
101
|
+
*/
|
|
102
|
+
export function watchConversation(id: string, watcher: ConversationWatcher): () => void {
|
|
103
|
+
listen();
|
|
104
|
+
|
|
105
|
+
const set = watchers.get(id) ?? new Set();
|
|
106
|
+
set.add(watcher);
|
|
107
|
+
watchers.set(id, set);
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
set.delete(watcher);
|
|
111
|
+
if (!set.size) watchers.delete(id);
|
|
112
|
+
};
|
|
113
|
+
}
|
package/lib/markdown.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ClipboardCopy } from '@axium/client/components';
|
|
2
|
+
import { marked } from 'marked';
|
|
3
|
+
import { mount, unmount } from 'svelte';
|
|
4
|
+
import type { Attachment } from 'svelte/attachments';
|
|
5
|
+
|
|
6
|
+
declare global {
|
|
7
|
+
interface SetHTMLOptions {
|
|
8
|
+
sanitizer?: Sanitizer | SanitizerConfig | 'default';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface Element {
|
|
12
|
+
setHTML(html: string, options?: SetHTMLOptions): void;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Renders sanitized Markdown, with a copy button on each code block */
|
|
17
|
+
export function markdown(source: string): Attachment<HTMLElement> {
|
|
18
|
+
return element => {
|
|
19
|
+
element.setHTML(marked.parse(source, { async: false }));
|
|
20
|
+
|
|
21
|
+
const buttons: ReturnType<typeof mount>[] = [];
|
|
22
|
+
|
|
23
|
+
for (const pre of element.querySelectorAll('pre')) {
|
|
24
|
+
const block = document.createElement('div');
|
|
25
|
+
block.className = 'code-block';
|
|
26
|
+
pre.replaceWith(block);
|
|
27
|
+
block.append(pre);
|
|
28
|
+
|
|
29
|
+
const corner = document.createElement('div');
|
|
30
|
+
corner.className = 'code-copy';
|
|
31
|
+
block.append(corner);
|
|
32
|
+
|
|
33
|
+
buttons.push(mount(ClipboardCopy, { target: corner, props: { value: pre.textContent ?? '' } }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return () => {
|
|
37
|
+
for (const button of buttons) void unmount(button);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as ai from '@axium/ai/common';
|
|
2
|
+
import * as locales from '@james-pre/locales';
|
|
3
|
+
import * as z from 'zod';
|
|
4
|
+
|
|
5
|
+
/** The preferences schema, with the default model chosen from `models` by name */
|
|
6
|
+
export function preferencesSchema(models: ai.Model[]): z.ZodObject {
|
|
7
|
+
if (!models.length) return ai.Preferences;
|
|
8
|
+
|
|
9
|
+
const options: Record<string, string> = {};
|
|
10
|
+
for (const model of models) options[model.name in options ? `${model.name} (${model.id})` : model.name] = model.id;
|
|
11
|
+
|
|
12
|
+
return ai.Preferences.extend({ model: z.enum(options).optional() }).register(locales.zod, { prefix: 'ai.preferences' });
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { tick } from 'svelte';
|
|
2
|
+
|
|
3
|
+
/** Whether the browser supports view transitions and the user hasn't asked for reduced motion */
|
|
4
|
+
export function canTransition(): boolean {
|
|
5
|
+
return !!document.startViewTransition && !matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Applies `update` in a view transition, so elements with a `view-transition-name` animate to their new place.
|
|
10
|
+
* Skipped when the user prefers reduced motion.
|
|
11
|
+
*/
|
|
12
|
+
export async function viewTransition(update: () => unknown): Promise<void> {
|
|
13
|
+
if (!canTransition()) {
|
|
14
|
+
await update();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const transition = document.startViewTransition(async () => {
|
|
19
|
+
await update();
|
|
20
|
+
await tick();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await transition.updateCallbackDone;
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": ["../tsconfig.json", "$app/tsconfig"],
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"typeRoots": null,
|
|
5
|
+
"rootDir": "..",
|
|
6
|
+
"noEmit": true,
|
|
7
|
+
"module": "preserve",
|
|
8
|
+
"moduleResolution": "Bundler",
|
|
9
|
+
"types": ["@sveltejs/kit"]
|
|
10
|
+
},
|
|
11
|
+
"include": ["**/*.svelte", "**/*.ts", "../src/.*.ts", "../../client/dist/.*.ts"],
|
|
12
|
+
"exclude": [],
|
|
13
|
+
"references": [{ "path": ".." }]
|
|
14
|
+
}
|