@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.
Files changed (109) hide show
  1. package/LICENSE.md +157 -0
  2. package/README.md +2 -2
  3. package/db.json +114 -0
  4. package/dist/.locale.d.ts +300 -0
  5. package/dist/.locale.js +1 -0
  6. package/dist/client/cli.d.ts +1 -0
  7. package/dist/client/cli.js +224 -0
  8. package/dist/client/config.d.ts +196 -0
  9. package/dist/client/config.js +55 -0
  10. package/dist/client/hooks.d.ts +3 -0
  11. package/dist/client/hooks.js +32 -0
  12. package/dist/client/local.d.ts +17 -0
  13. package/dist/client/local.js +112 -0
  14. package/dist/common.d.ts +1318 -0
  15. package/dist/common.js +569 -0
  16. package/dist/hooks.d.ts +13 -0
  17. package/dist/hooks.js +34 -0
  18. package/dist/hub.d.ts +4 -0
  19. package/dist/hub.js +75 -0
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.js +1 -0
  22. package/dist/integrations/notes.d.ts +1 -0
  23. package/dist/integrations/notes.js +104 -0
  24. package/dist/integrations/tasks.d.ts +1 -0
  25. package/dist/integrations/tasks.js +157 -0
  26. package/dist/loop.d.ts +75 -0
  27. package/dist/loop.js +238 -0
  28. package/dist/memory.d.ts +107 -0
  29. package/dist/memory.js +193 -0
  30. package/dist/node/chat.d.ts +50 -0
  31. package/dist/node/chat.js +60 -0
  32. package/dist/node/llama.d.ts +115 -0
  33. package/dist/node/llama.js +385 -0
  34. package/dist/node/markdown.d.ts +6 -0
  35. package/dist/node/markdown.js +285 -0
  36. package/dist/node/models.d.ts +34 -0
  37. package/dist/node/models.js +287 -0
  38. package/dist/node/web.d.ts +1 -0
  39. package/dist/node/web.js +203 -0
  40. package/dist/server/api/conversations.d.ts +1 -0
  41. package/dist/server/api/conversations.js +216 -0
  42. package/dist/server/api/gateway.d.ts +1 -0
  43. package/dist/server/api/gateway.js +113 -0
  44. package/dist/server/api/memories.d.ts +1 -0
  45. package/dist/server/api/memories.js +129 -0
  46. package/dist/server/api/models.d.ts +1 -0
  47. package/dist/server/api/models.js +161 -0
  48. package/dist/server/api/temporary.d.ts +1 -0
  49. package/dist/server/api/temporary.js +50 -0
  50. package/dist/server/api/timers.d.ts +1 -0
  51. package/dist/server/api/timers.js +27 -0
  52. package/dist/server/api/voice.d.ts +1 -0
  53. package/dist/server/api/voice.js +188 -0
  54. package/dist/server/api.d.ts +1 -0
  55. package/dist/server/api.js +157 -0
  56. package/dist/server/generation.d.ts +38 -0
  57. package/dist/server/generation.js +299 -0
  58. package/dist/server/memory.d.ts +7 -0
  59. package/dist/server/memory.js +218 -0
  60. package/dist/server/models.d.ts +13 -0
  61. package/dist/server/models.js +59 -0
  62. package/dist/server/preferences.d.ts +3 -0
  63. package/dist/server/preferences.js +12 -0
  64. package/dist/server/timers.d.ts +7 -0
  65. package/dist/server/timers.js +66 -0
  66. package/dist/speech.d.ts +17 -0
  67. package/dist/speech.js +69 -0
  68. package/dist/timers.d.ts +1 -0
  69. package/dist/timers.js +67 -0
  70. package/dist/tools.d.ts +70 -0
  71. package/dist/tools.js +84 -0
  72. package/lib/ChatMessage.svelte +331 -0
  73. package/lib/Composer.svelte +234 -0
  74. package/lib/Conversation.svelte +433 -0
  75. package/lib/ConversationMenu.svelte +18 -0
  76. package/lib/ModelActions.svelte +71 -0
  77. package/lib/ModelOptions.svelte +101 -0
  78. package/lib/ModelStatus.svelte +49 -0
  79. package/lib/ToolCall.svelte +171 -0
  80. package/lib/VoiceMode.svelte +113 -0
  81. package/lib/actions.ts +21 -0
  82. package/lib/index.ts +7 -0
  83. package/lib/live.svelte.ts +113 -0
  84. package/lib/markdown.ts +40 -0
  85. package/lib/preferences.ts +13 -0
  86. package/lib/transition.ts +24 -0
  87. package/lib/tsconfig.json +14 -0
  88. package/lib/voice.svelte.ts +476 -0
  89. package/locales/en.json +316 -0
  90. package/package.json +139 -5
  91. package/routes/ai/+layout.svelte +177 -0
  92. package/routes/ai/+layout.ts +26 -0
  93. package/routes/ai/+page.svelte +109 -0
  94. package/routes/ai/TimerAlerts.svelte +51 -0
  95. package/routes/ai/Timers.svelte +121 -0
  96. package/routes/ai/[id]/+page.svelte +18 -0
  97. package/routes/ai/[id]/+page.ts +14 -0
  98. package/routes/ai/memory/+page.svelte +313 -0
  99. package/routes/ai/memory/+page.ts +10 -0
  100. package/routes/ai/memory/MemoryForm.svelte +111 -0
  101. package/routes/ai/models/+page.svelte +199 -0
  102. package/routes/ai/models/+page.ts +5 -0
  103. package/routes/ai/models/AddModel.svelte +240 -0
  104. package/routes/ai/models/MemoryMeter.svelte +120 -0
  105. package/routes/ai/models/Roles.svelte +159 -0
  106. package/routes/ai/models/[id]/+page.svelte +331 -0
  107. package/routes/ai/models/[id]/+page.ts +6 -0
  108. package/routes/ai/timers.svelte.ts +94 -0
  109. package/routes/tsconfig.json +14 -0
@@ -0,0 +1,66 @@
1
+ import * as jobs from '@axium/server/jobs';
2
+ import { notify } from '@axium/server/notifications';
3
+ import { error } from '@axium/server/requests';
4
+ import { io as socketServer } from '@axium/server/socket';
5
+ import { timeSpan } from 'utilium/format';
6
+ import * as z from 'zod';
7
+ import * as ai from '../common.js';
8
+ const TimerInput = z.object({
9
+ label: ai.TimerLabel.nullable(),
10
+ message: ai.TimerMessage.nullable(),
11
+ duration: z.int().positive(),
12
+ });
13
+ /** The most timers a user may have running */
14
+ const maxTimers = 50;
15
+ const timers = jobs.define('ai.timer', {
16
+ input: TimerInput,
17
+ async run({ label, message }, { key: id, userId }) {
18
+ if (!id || !userId)
19
+ return;
20
+ socketServer?.to('user:' + userId).emit('ai.timer.remove', { id, done: true });
21
+ await notify(userId, { title: label ?? 'Timer', body: message ?? "Time's up", url: '/ai', tag: 'ai.timer:' + id });
22
+ },
23
+ });
24
+ function toTimer(job) {
25
+ return { id: job.key, ...job.input, created: job.createdAt, ends: job.runAt };
26
+ }
27
+ export async function list(userId) {
28
+ const pending = await timers.list({ userId });
29
+ return pending.filter(job => job.state != 'failed').map(toTimer);
30
+ }
31
+ export async function start(userId, init) {
32
+ if ((await list(userId)).length >= maxTimers)
33
+ error(409, `There are already ${maxTimers} timers running; cancel one first`);
34
+ const now = Date.now();
35
+ const ends = 'at' in init ? Temporal.Instant.from(init.at).epochMilliseconds : now + init.duration * 1000;
36
+ const duration = Math.round((ends - now) / 1000);
37
+ if (duration <= 0)
38
+ error(400, 'That time has already passed');
39
+ if (duration > ai.maxTimerDuration)
40
+ error(400, `Timers can end at most ${timeSpan(ai.maxTimerDuration * 1000)} from now`);
41
+ const id = crypto.randomUUID();
42
+ const input = { label: init.label ?? null, message: init.message ?? null, duration };
43
+ await timers.enqueue(input, { key: id, userId, at: new Date(ends) });
44
+ const job = await timers.get(id);
45
+ if (!job)
46
+ error(500, 'Could not start the timer');
47
+ const timer = toTimer(job);
48
+ socketServer?.to('user:' + userId).emit('ai.timer', timer);
49
+ return timer;
50
+ }
51
+ export async function cancel(userId, id) {
52
+ const job = await timers.get(id);
53
+ if (!job || job.userId != userId)
54
+ error(404, 'No timer has the ID ' + id);
55
+ await timers.cancel(id);
56
+ socketServer?.to('user:' + userId).emit('ai.timer.remove', { id, done: false });
57
+ return toTimer(job);
58
+ }
59
+ /** Timers for tools run by the server's loop */
60
+ export function forUser(userId) {
61
+ return {
62
+ start: init => start(userId, init),
63
+ list: () => list(userId),
64
+ cancel: id => cancel(userId, id),
65
+ };
66
+ }
@@ -0,0 +1,17 @@
1
+ /** Markdown as it should be read aloud: no code blocks, markup or link targets */
2
+ export declare function speakable(markdown: string): string;
3
+ /** The most characters spoken in one request, which keeps each part well within what the model generates at once */
4
+ export declare const maxPartLength = 300;
5
+ /** Splits text into parts to speak one after another, at sentence and line boundaries where possible */
6
+ export declare function speechParts(text: string): string[];
7
+ /**
8
+ * Collects Markdown as it streams and gives the parts that are ready to be spoken:
9
+ * whole sentences and lines, never the inside of a code block.
10
+ */
11
+ export declare class SpeechBuffer {
12
+ protected text: string;
13
+ /** Adds streamed text, returning the parts it completed */
14
+ push(text: string): string[];
15
+ /** Returns whatever is left, once the text is complete */
16
+ end(): string[];
17
+ }
package/dist/speech.js ADDED
@@ -0,0 +1,69 @@
1
+ /** Markdown as it should be read aloud: no code blocks, markup or link targets */
2
+ export function speakable(markdown) {
3
+ return markdown
4
+ .replace(/```[\s\S]*?(?:```|$)/g, ' ')
5
+ .replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1')
6
+ .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
7
+ .replace(/<[^>]+>/g, ' ')
8
+ .replace(/^\s*(?:#{1,6}|>|[-*+]|\d+[.)])\s+/gm, '')
9
+ .replace(/^[ \t]*\|?[ \t:|-]+\|[ \t:|-]*$/gm, '')
10
+ .replace(/^[ \t]*\|[ \t]*|[ \t]*\|[ \t]*$/gm, '')
11
+ .replace(/[ \t]*\|[ \t]*/g, ', ')
12
+ .replace(/[*_~`]+/g, '')
13
+ .replace(/[ \t]+/g, ' ')
14
+ .replace(/([^\s.!?:;,])[ \t]*\n+/g, '$1.\n')
15
+ .replace(/\s*\n\s*/g, '\n')
16
+ .trim();
17
+ }
18
+ /** The most characters spoken in one request, which keeps each part well within what the model generates at once */
19
+ export const maxPartLength = 300;
20
+ /** Splits text into parts to speak one after another, at sentence and line boundaries where possible */
21
+ export function speechParts(text) {
22
+ const sentences = text.match(/[^.!?\n]+(?:[.!?]+["')\]]*|\n+|$)\s*/g) ?? [];
23
+ const parts = [];
24
+ let current = '';
25
+ for (const sentence of sentences) {
26
+ if (current && current.length + sentence.length > maxPartLength) {
27
+ parts.push(current.trim());
28
+ current = '';
29
+ }
30
+ let rest = sentence;
31
+ while (rest.length > maxPartLength) {
32
+ const space = rest.lastIndexOf(' ', maxPartLength);
33
+ const end = space > 0 ? space : maxPartLength;
34
+ parts.push(rest.slice(0, end).trim());
35
+ rest = rest.slice(end).trimStart();
36
+ }
37
+ current += rest;
38
+ }
39
+ if (current.trim())
40
+ parts.push(current.trim());
41
+ return parts.filter(part => /[\p{L}\p{N}]/u.test(part));
42
+ }
43
+ /**
44
+ * Collects Markdown as it streams and gives the parts that are ready to be spoken:
45
+ * whole sentences and lines, never the inside of a code block.
46
+ */
47
+ export class SpeechBuffer {
48
+ text = '';
49
+ /** Adds streamed text, returning the parts it completed */
50
+ push(text) {
51
+ this.text += text;
52
+ const fences = this.text.match(/```/g)?.length ?? 0;
53
+ const searchable = fences % 2 ? this.text.slice(0, this.text.lastIndexOf('```')) : this.text;
54
+ let end = -1;
55
+ for (const match of searchable.matchAll(/[.!?]["')\]]*\s|\n/g))
56
+ end = match.index + match[0].length;
57
+ if (end == -1)
58
+ return [];
59
+ const ready = this.text.slice(0, end);
60
+ this.text = this.text.slice(end);
61
+ return speechParts(speakable(ready));
62
+ }
63
+ /** Returns whatever is left, once the text is complete */
64
+ end() {
65
+ const rest = this.text;
66
+ this.text = '';
67
+ return speechParts(speakable(rest));
68
+ }
69
+ }
@@ -0,0 +1 @@
1
+ export {};
package/dist/timers.js ADDED
@@ -0,0 +1,67 @@
1
+ import { timeSpan } from 'utilium/format';
2
+ import * as z from 'zod';
3
+ import * as ai from './common.js';
4
+ import * as tools from './tools.js';
5
+ function describe(timer) {
6
+ return {
7
+ id: timer.id,
8
+ label: timer.label ?? undefined,
9
+ message: timer.message ?? undefined,
10
+ duration: timeSpan(timer.duration * 1000, 3),
11
+ remaining: timeSpan(new Date(timer.ends).getTime() - Date.now()),
12
+ ends: new Date(timer.ends).toISOString(),
13
+ };
14
+ }
15
+ const Count = z.int().nonnegative().optional();
16
+ tools.add({
17
+ name: 'set_timer',
18
+ description: 'Starts a timer that notifies the user when it ends. ' +
19
+ 'For a length of time, e.g. "in 10 minutes", use the duration fields. ' +
20
+ 'For a time of day, e.g. "at 3" or "after class", use `at` instead of working out the length.',
21
+ environment: null,
22
+ access: 'write',
23
+ approval: 'allow',
24
+ // @todo Temporal
25
+ input: z.object({
26
+ hours: Count,
27
+ minutes: Count,
28
+ seconds: Count,
29
+ at: ai.Instant.optional().describe("When the timer ends, as an ISO 8601 date and time with the user's UTC offset, e.g. 2026-11-16T15:30-06:00"),
30
+ label: ai.TimerLabel.optional().describe('What the timer is for, e.g. "Pasta", shown when it ends'),
31
+ message: ai.TimerMessage.optional().describe('Shown when the timer ends, only when there is something to do or remember beyond the label, e.g. "Drain the pasta and add the sauce"'),
32
+ }),
33
+ async run({ hours = 0, minutes = 0, seconds = 0, at, label, message }, { timers }) {
34
+ const duration = hours * 3600 + minutes * 60 + seconds;
35
+ if (at && duration)
36
+ throw new Error('Give either `at` or a duration, not both');
37
+ if (at)
38
+ return describe(await timers.start({ label, message, at }));
39
+ if (!duration)
40
+ throw new Error('Give the length of the timer, or when it ends with `at`');
41
+ if (duration > ai.maxTimerDuration)
42
+ throw new Error(`Timers can be at most ${timeSpan(ai.maxTimerDuration * 1000)} long`);
43
+ return describe(await timers.start({ label, message, duration }));
44
+ },
45
+ });
46
+ tools.add({
47
+ name: 'list_timers',
48
+ description: "Lists the user's running timers with the time left on each.",
49
+ environment: null,
50
+ access: 'read',
51
+ input: z.object({}),
52
+ async run(_, { timers }) {
53
+ const running = await timers.list();
54
+ return running.length ? running.map(describe) : 'No timers are running.';
55
+ },
56
+ });
57
+ tools.add({
58
+ name: 'cancel_timer',
59
+ description: 'Cancels a running timer.',
60
+ environment: null,
61
+ access: 'write',
62
+ approval: 'allow',
63
+ input: z.object({ id: z.uuid().describe("The timer's ID, from set_timer or list_timers") }),
64
+ async run({ id }, { timers }) {
65
+ return { cancelled: describe(await timers.cancel(id)) };
66
+ },
67
+ });
@@ -0,0 +1,70 @@
1
+ import type { $API, APIParameters, Endpoint, RequestBody, Result } from '@axium/core/api';
2
+ import type { RequestMethod } from '@axium/core/requests';
3
+ import * as z from 'zod';
4
+ import type * as ai from './common.js';
5
+ /** Calls an Axium API route as the user, e.g. `fetchAPI` on a client or `callAPI` on the server */
6
+ export type APICaller = <const E extends Endpoint, const M extends keyof $API[E] & RequestMethod>(method: M, endpoint: E, data?: RequestBody<M, E>, ...params: APIParameters<E>) => Promise<Result<M, E>>;
7
+ /** Timers kept by the host the loop runs on, which notifies the user when they end */
8
+ export interface Timers {
9
+ start(init: ai.TimerInit): Promise<ai.Timer>;
10
+ list(): Promise<ai.Timer[]>;
11
+ cancel(id: string): Promise<ai.Timer>;
12
+ }
13
+ export interface Context {
14
+ /** The user the tool acts for */
15
+ userId: string;
16
+ /** The conversation the call is from, when it is saved */
17
+ conversationId?: string;
18
+ api: APICaller;
19
+ signal: AbortSignal;
20
+ timers: Timers;
21
+ }
22
+ export interface Tool<Input extends z.ZodObject = z.ZodObject> {
23
+ /** Unique, in `snake_case` */
24
+ name: string;
25
+ /** Shown to the model; formats belong in the input schema's descriptions */
26
+ description: string;
27
+ environment: ai.Environment;
28
+ access: ai.ToolAccess;
29
+ /** Whether write calls ask first, unless the user changed it. Defaults to `ask`. */
30
+ approval?: ai.Approval;
31
+ input: Input;
32
+ /** Whether the tool can be offered, e.g. `false` while its configuration is missing */
33
+ available?(): boolean;
34
+ /** The result is given to the model as-is when it's a string, and as JSON otherwise */
35
+ run(input: z.infer<Input>, context: Context): Promise<unknown>;
36
+ }
37
+ /** @throws if a tool with the same name exists */
38
+ export declare function add<const Input extends z.ZodObject>(tool: Tool<Input>): void;
39
+ /** The tools allowed by the user's preferences and the instance's features */
40
+ export interface AllowedOptions {
41
+ /** Leaves out tools that use the user's data or memories */
42
+ anonymous?: boolean;
43
+ /** Leaves out tools that change memories */
44
+ temporary?: boolean;
45
+ }
46
+ export declare function allowed(preferences: ai.Preferences['tools'], { anonymous, temporary }?: AllowedOptions): Tool[];
47
+ /**
48
+ * Whether a call must wait for the user's approval.
49
+ * @param allowed Tools the user allowed for the rest of the conversation
50
+ */
51
+ export declare function needsApproval(tool: Tool | undefined, preferences: ai.Preferences['tools'], allowed: ReadonlySet<string>): boolean;
52
+ /** Tools in the OpenAI chat completions format */
53
+ export declare function definitions(offered: Tool[]): {
54
+ type: "function";
55
+ function: {
56
+ name: string;
57
+ description: string;
58
+ parameters: z.core.ZodStandardJSONSchemaPayload<z.ZodObject<z.core.$ZodLooseShape, z.core.$strip>>;
59
+ };
60
+ }[];
61
+ export interface CallResult {
62
+ status: 'done' | 'error';
63
+ /** What the model sees */
64
+ content: string;
65
+ /** What the user sees */
66
+ error: string | null;
67
+ }
68
+ /** Validates a call's arguments and runs it. Never throws: failures are described for the model. */
69
+ export declare function run(tool: Tool | undefined, call: ai.ToolCall, context: Context): Promise<CallResult>;
70
+ export declare function get(name: string): Tool | undefined;
package/dist/tools.js ADDED
@@ -0,0 +1,84 @@
1
+ import features from '@axium/core/features';
2
+ import * as io from 'ioium';
3
+ import * as z from 'zod';
4
+ const registry = new Map();
5
+ /** @throws if a tool with the same name exists */
6
+ export function add(tool) {
7
+ if (registry.has(tool.name))
8
+ throw new ReferenceError(`Tool "${tool.name}" already exists`);
9
+ registry.set(tool.name, tool);
10
+ }
11
+ export function allowed(preferences, { anonymous, temporary } = {}) {
12
+ return Array.from(registry.values()).filter(tool => {
13
+ if (tool.available && !tool.available())
14
+ return false;
15
+ switch (tool.environment) {
16
+ case null:
17
+ return true;
18
+ case 'web':
19
+ return preferences.web && features('ai-web-tools');
20
+ case 'account':
21
+ if (anonymous || !features('ai-account-access'))
22
+ return false;
23
+ return preferences.account_access == 'write' || (preferences.account_access == 'read' && tool.access == 'read');
24
+ case 'memory':
25
+ return preferences.memory && features('ai-memory') && !anonymous && (tool.access == 'read' || !temporary);
26
+ }
27
+ });
28
+ }
29
+ /**
30
+ * Whether a call must wait for the user's approval.
31
+ * @param allowed Tools the user allowed for the rest of the conversation
32
+ */
33
+ export function needsApproval(tool, preferences, allowed) {
34
+ if (tool?.access != 'write' || !preferences.confirm_writes || allowed.has(tool.name))
35
+ return false;
36
+ return (preferences.approvals[tool.name] ?? tool.approval ?? 'ask') == 'ask';
37
+ }
38
+ /** Tools in the OpenAI chat completions format */
39
+ export function definitions(offered) {
40
+ return offered.map(tool => ({
41
+ type: 'function',
42
+ function: { name: tool.name, description: tool.description, parameters: z.toJSONSchema(tool.input) },
43
+ }));
44
+ }
45
+ /** Characters of a result given to the model */
46
+ const maxResultLength = 32_000;
47
+ function failure(error) {
48
+ return { status: 'error', content: 'Error: ' + error, error };
49
+ }
50
+ /** Validates a call's arguments and runs it. Never throws: failures are described for the model. */
51
+ export async function run(tool, call, context) {
52
+ if (!tool)
53
+ return failure(`The tool "${call.name}" is not available`);
54
+ let input;
55
+ try {
56
+ input = JSON.parse(call.arguments || '{}');
57
+ }
58
+ catch {
59
+ return failure('The arguments are not valid JSON');
60
+ }
61
+ const parsed = tool.input.safeParse(input);
62
+ if (!parsed.success)
63
+ return failure('Invalid arguments:\n' + z.prettifyError(parsed.error));
64
+ try {
65
+ const result = await tool.run(parsed.data, context);
66
+ const text = typeof result == 'string' ? result : JSON.stringify(result);
67
+ if (text.length <= maxResultLength)
68
+ return { status: 'done', content: text, error: null };
69
+ if (typeof result != 'string')
70
+ return failure(`The result is too large (${text.length} characters); ask for less`);
71
+ return {
72
+ status: 'done',
73
+ content: text.slice(0, maxResultLength) + `\n[Truncated: showing ${maxResultLength} of ${text.length} characters]`,
74
+ error: null,
75
+ };
76
+ }
77
+ catch (error) {
78
+ io.debug('AI: tool', tool.name, 'failed:', io.errorText(error));
79
+ return failure(io.errorText(error));
80
+ }
81
+ }
82
+ export function get(name) {
83
+ return registry.get(name);
84
+ }