Skip to content

Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base

add workflow trigger to ui

Hugo Richard4960d1f32dcc
diff --git a/apps/chat/app/components/UserMenu.vue b/apps/chat/app/components/UserMenu.vue
index 14c91f7..e3f14d7 100644
--- a/apps/chat/app/components/UserMenu.vue
+++ b/apps/chat/app/components/UserMenu.vue
@@ -1,5 +1,6 @@
<script setup lang="ts">
import type { DropdownMenuItem } from '@nuxt/ui'
+import type { SourcesResponse } from '@savoir/sdk'
defineProps<{
collapsed?: boolean
@@ -8,10 +9,64 @@ defineProps<{
const colorMode = useColorMode()
const appConfig = useAppConfig()
const { user, clear } = useUserSession()
+const toast = useToast()
const colors = ['red', 'orange', 'amber', 'yellow', 'lime', 'green', 'emerald', 'teal', 'cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 'fuchsia', 'pink', 'rose']
const neutrals = ['slate', 'gray', 'zinc', 'neutral', 'stone']
+const { data: sources } = useLazyFetch<SourcesResponse>('/api/admin/sources')
+
+async function triggerSync() {
+ try {
+ await $fetch('/api/admin/sync', { method: 'POST' })
+ toast.add({
+ title: 'Sync started',
+ description: 'Documentation sync workflow has been triggered.',
+ color: 'success',
+ })
+ } catch (error) {
+ toast.add({
+ title: 'Sync failed',
+ description: error instanceof Error ? error.message : 'Unknown error',
+ color: 'error',
+ })
+ }
+}
+
+async function triggerSyncSource(sourceId: string, label: string) {
+ try {
+ await $fetch(`/api/admin/sync/${sourceId}`, { method: 'POST' })
+ toast.add({
+ title: 'Sync started',
+ description: `Sync workflow for "${label}" has been triggered.`,
+ color: 'success',
+ })
+ } catch (error) {
+ toast.add({
+ title: 'Sync failed',
+ description: error instanceof Error ? error.message : 'Unknown error',
+ color: 'error',
+ })
+ }
+}
+
+async function triggerSnapshot() {
+ try {
+ await $fetch('/api/admin/snapshot', { method: 'POST' })
+ toast.add({
+ title: 'Snapshot started',
+ description: 'Snapshot creation workflow has been triggered.',
+ color: 'success',
+ })
+ } catch (error) {
+ toast.add({
+ title: 'Snapshot failed',
+ description: error instanceof Error ? error.message : 'Unknown error',
+ color: 'error',
+ })
+ }
+}
+
const items = computed<DropdownMenuItem[][]>(() => ([
[
{
@@ -41,7 +96,7 @@ const items = computed<DropdownMenuItem[][]>(() => ([
slot: 'chip',
checked: appConfig.ui.colors.primary === color,
type: 'checkbox',
- onSelect: (e) => {
+ onSelect: (e: Event) => {
e.preventDefault()
appConfig.ui.colors.primary = color
@@ -61,7 +116,7 @@ const items = computed<DropdownMenuItem[][]>(() => ([
slot: 'chip',
type: 'checkbox',
checked: appConfig.ui.colors.neutral === color,
- onSelect: (e) => {
+ onSelect: (e: Event) => {
e.preventDefault()
appConfig.ui.colors.neutral = color
@@ -78,7 +133,7 @@ const items = computed<DropdownMenuItem[][]>(() => ([
icon: 'i-lucide-sun',
type: 'checkbox',
checked: colorMode.value === 'light',
- onSelect(e: Event) {
+ onSelect: (e: Event) => {
e.preventDefault()
colorMode.preference = 'light'
@@ -146,6 +201,39 @@ const items = computed<DropdownMenuItem[][]>(() => ([
to: 'https://github.com/nuxt-ui-templates/chat',
target: '_blank'
}
+ ], [
+ {
+ label: 'Admin',
+ icon: 'i-lucide-settings',
+ children: [
+ {
+ label: 'Sync all',
+ icon: 'i-lucide-refresh-cw',
+ onSelect: triggerSync,
+ },
+ {
+ label: 'Sync source',
+ icon: 'i-lucide-git-branch',
+ children: sources.value ? [
+ ...sources.value.github.sources.map(source => ({
+ label: source.label,
+ icon: 'i-simple-icons-github',
+ onSelect: () => triggerSyncSource(source.id, source.label),
+ })),
+ ...sources.value.youtube.sources.map(source => ({
+ label: source.label,
+ icon: 'i-simple-icons-youtube',
+ onSelect: () => triggerSyncSource(source.id, source.label),
+ })),
+ ] : [{ label: 'Loading...', disabled: true }],
+ },
+ {
+ label: 'Create snapshot',
+ icon: 'i-lucide-camera',
+ onSelect: triggerSnapshot,
+ },
+ ],
+ },
], [
{
label: 'Log out',
diff --git a/apps/chat/server/api/admin/snapshot.post.ts b/apps/chat/server/api/admin/snapshot.post.ts
new file mode 100644
index 0000000..95e4425
--- /dev/null
+++ b/apps/chat/server/api/admin/snapshot.post.ts
@@ -0,0 +1,16 @@
+import { createSavoir } from '@savoir/sdk'
+
+/**
+ * POST /api/admin/snapshot
+ * Trigger snapshot creation workflow
+ */
+export default defineEventHandler(async () => {
+ const { savoir: savoirConfig } = useRuntimeConfig()
+
+ const savoir = createSavoir({
+ apiUrl: savoirConfig.apiUrl,
+ apiKey: savoirConfig.apiKey || undefined,
+ })
+
+ return await savoir.client.createSnapshot()
+})
diff --git a/apps/chat/server/api/admin/sources.get.ts b/apps/chat/server/api/admin/sources.get.ts
new file mode 100644
index 0000000..e90095d
--- /dev/null
+++ b/apps/chat/server/api/admin/sources.get.ts
@@ -0,0 +1,16 @@
+import { createSavoir } from '@savoir/sdk'
+
+/**
+ * GET /api/admin/sources
+ * Get list of configured sources
+ */
+export default defineEventHandler(async () => {
+ const { savoir: savoirConfig } = useRuntimeConfig()
+
+ const savoir = createSavoir({
+ apiUrl: savoirConfig.apiUrl,
+ apiKey: savoirConfig.apiKey || undefined,
+ })
+
+ return await savoir.client.getSources()
+})
diff --git a/apps/chat/server/api/admin/sync.post.ts b/apps/chat/server/api/admin/sync.post.ts
new file mode 100644
index 0000000..8df2637
--- /dev/null
+++ b/apps/chat/server/api/admin/sync.post.ts
@@ -0,0 +1,16 @@
+import { createSavoir } from '@savoir/sdk'
+
+/**
+ * POST /api/admin/sync
+ * Trigger documentation sync workflow
+ */
+export default defineEventHandler(async () => {
+ const { savoir: savoirConfig } = useRuntimeConfig()
+
+ const savoir = createSavoir({
+ apiUrl: savoirConfig.apiUrl,
+ apiKey: savoirConfig.apiKey || undefined,
+ })
+
+ return await savoir.client.sync()
+})
diff --git a/apps/chat/server/api/admin/sync/[source].post.ts b/apps/chat/server/api/admin/sync/[source].post.ts
new file mode 100644
index 0000000..49415eb
--- /dev/null
+++ b/apps/chat/server/api/admin/sync/[source].post.ts
@@ -0,0 +1,21 @@
+import { createSavoir } from '@savoir/sdk'
+
+/**
+ * POST /api/admin/sync/:source
+ * Trigger sync workflow for a specific source
+ */
+export default defineEventHandler(async (event) => {
+ const source = getRouterParam(event, 'source')
+ if (!source) {
+ throw createError({ statusCode: 400, statusMessage: 'Source ID is required' })
+ }
+
+ const { savoir: savoirConfig } = useRuntimeConfig()
+
+ const savoir = createSavoir({
+ apiUrl: savoirConfig.apiUrl,
+ apiKey: savoirConfig.apiKey || undefined,
+ })
+
+ return await savoir.client.syncSource(source)
+})
diff --git a/packages/sdk/src/client.ts b/packages/sdk/src/client.ts
index 92d6d7f..a203a99 100644
--- a/packages/sdk/src/client.ts
+++ b/packages/sdk/src/client.ts
@@ -3,6 +3,11 @@ import type {
ReadResponse,
SavoirConfig,
SearchAndReadResponse,
+ SnapshotResponse,
+ SourcesResponse,
+ SyncOptions,
+ SyncResponse,
+ SyncSourceResponse,
} from './types'
import { NetworkError, SavoirError } from './errors'
@@ -44,11 +49,48 @@ export class SavoirClient {
}
/**
- * Make a request to the API
+ * Make a GET request to the API
*/
- private async request<T>(
+ private async get<T>(path: string): Promise<T> {
+ const url = `${this.apiUrl}${path}`
+
+ const headers: Record<string, string> = {}
+
+ if (this.apiKey) {
+ headers['Authorization'] = `Bearer ${this.apiKey}`
+ }
+
+ try {
+ const response = await fetch(url, { method: 'GET', headers })
+ const data = await response.json()
+
+ if (!response.ok) {
+ throw new SavoirError({
+ statusCode: response.status,
+ message: data.message || 'Unknown error',
+ error: data.error,
+ })
+ }
+
+ return data as T
+ } catch (error) {
+ if (error instanceof SavoirError) {
+ throw error
+ }
+
+ throw new NetworkError(
+ `Failed to connect to Savoir API: ${error instanceof Error ? error.message : 'Unknown error'}`,
+ error instanceof Error ? error : undefined,
+ )
+ }
+ }
+
+ /**
+ * Make a POST request to the API
+ */
+ private async post<T>(
path: string,
- body: Record<string, unknown>,
+ body: Record<string, unknown> = {},
): Promise<T> {
const url = `${this.apiUrl}${path}`
@@ -105,7 +147,7 @@ export class SavoirClient {
query: string,
limit: number = 20,
): Promise<SearchAndReadResponse> {
- return await this.request<SearchAndReadResponse>('/api/sandbox/search-and-read', {
+ return await this.post<SearchAndReadResponse>('/api/sandbox/search-and-read', {
query,
limit,
})
@@ -115,9 +157,43 @@ export class SavoirClient {
* Read specific files by path
*/
async read(paths: string[]): Promise<ReadResponse> {
- return await this.request<ReadResponse>('/api/sandbox/read', {
+ return await this.post<ReadResponse>('/api/sandbox/read', {
paths,
})
}
+ /**
+ * Get list of configured sources
+ */
+ async getSources(): Promise<SourcesResponse> {
+ return await this.get<SourcesResponse>('/api/sources')
+ }
+
+ /**
+ * Trigger documentation sync workflow for all sources
+ */
+ async sync(options?: SyncOptions): Promise<SyncResponse> {
+ return await this.post<SyncResponse>('/api/sync', {
+ reset: options?.reset ?? false,
+ push: options?.push ?? true,
+ })
+ }
+
+ /**
+ * Trigger documentation sync workflow for a specific source
+ */
+ async syncSource(sourceId: string, options?: SyncOptions): Promise<SyncSourceResponse> {
+ return await this.post<SyncSourceResponse>(`/api/sync/${sourceId}`, {
+ reset: options?.reset ?? false,
+ push: options?.push ?? true,
+ })
+ }
+
+ /**
+ * Trigger snapshot creation workflow
+ */
+ async createSnapshot(): Promise<SnapshotResponse> {
+ return await this.post<SnapshotResponse>('/api/sandbox/snapshot', {})
+ }
+
}
diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts
index 95f0666..5802b48 100644
--- a/packages/sdk/src/index.ts
+++ b/packages/sdk/src/index.ts
@@ -2,7 +2,7 @@ import { SavoirClient } from './client'
import { createReadTool, createSearchAndReadTool } from './tools'
import type { SavoirConfig } from './types'
-export type { SavoirConfig, SearchResult, FileContent, SearchAndReadResponse, ReadResponse } from './types'
+export type { SavoirConfig, SearchResult, FileContent, SearchAndReadResponse, ReadResponse, SyncOptions, SyncResponse, SnapshotResponse, GitHubSource, YouTubeSource, SourcesResponse, SyncSourceResponse } from './types'
export { SavoirError, NetworkError } from './errors'
export { SavoirClient } from './client'
diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts
index 0ed3777..3878b97 100644
--- a/packages/sdk/src/types.ts
+++ b/packages/sdk/src/types.ts
@@ -62,3 +62,86 @@ export interface ApiErrorResponse {
message: string
error?: string
}
+
+/**
+ * Options for the sync operation
+ */
+export interface SyncOptions {
+ /** Clear all content before sync */
+ reset?: boolean
+ /** Push to snapshot repo after sync */
+ push?: boolean
+}
+
+/**
+ * Response from the sync endpoint
+ */
+export interface SyncResponse {
+ status: 'started'
+ message: string
+ options: {
+ reset: boolean
+ push: boolean
+ }
+}
+
+/**
+ * Response from the snapshot endpoint
+ */
+export interface SnapshotResponse {
+ status: 'started'
+ message: string
+}
+
+/**
+ * GitHub source configuration
+ */
+export interface GitHubSource {
+ id: string
+ label: string
+ type: 'github'
+ repo: string
+ branch: string
+ outputPath: string
+ readmeOnly: boolean
+}
+
+/**
+ * YouTube source configuration
+ */
+export interface YouTubeSource {
+ id: string
+ label: string
+ type: 'youtube'
+ channelId: string
+ handle: string
+}
+
+/**
+ * Response from the sources endpoint
+ */
+export interface SourcesResponse {
+ total: number
+ github: {
+ count: number
+ sources: GitHubSource[]
+ }
+ youtube: {
+ count: number
+ sources: YouTubeSource[]
+ }
+}
+
+/**
+ * Response from the sync source endpoint
+ */
+export interface SyncSourceResponse {
+ status: 'started'
+ message: string
+ source: string
+ options: {
+ reset: boolean
+ push: boolean
+ sourceFilter: string
+ }
+}