-
Notifications
You must be signed in to change notification settings - Fork 108
ITO-448: Create Interactions Directly from transcribe stream #455
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,10 @@ services: | |
| container_name: ito-server | ||
| restart: always | ||
| depends_on: | ||
| - db | ||
| db: | ||
| condition: service_started | ||
| createbuckets: | ||
| condition: service_completed_successfully | ||
| environment: | ||
| NODE_ENV: development | ||
|
|
||
|
|
@@ -44,9 +47,31 @@ services: | |
| - minio_data:/data | ||
| healthcheck: | ||
| test: ['CMD', 'mc', 'ready', 'local'] | ||
| interval: 30s | ||
| timeout: 20s | ||
| retries: 3 | ||
| interval: 5s | ||
| timeout: 5s | ||
| retries: 5 | ||
|
|
||
| createbuckets: | ||
| image: minio/mc:latest | ||
| container_name: ito-minio-init | ||
| depends_on: | ||
| minio: | ||
| condition: service_healthy | ||
| entrypoint: > | ||
| /bin/sh -c " | ||
| /usr/bin/mc alias set myminio http://minio:9000 $${S3_ACCESS_KEY_ID} $${S3_SECRET_ACCESS_KEY}; | ||
| /usr/bin/mc mb myminio/$${BLOB_STORAGE_BUCKET} --ignore-existing; | ||
| /usr/bin/mc mb myminio/$${TIMING_BUCKET} --ignore-existing; | ||
| /usr/bin/mc anonymous set download myminio/$${BLOB_STORAGE_BUCKET}; | ||
| /usr/bin/mc anonymous set download myminio/$${TIMING_BUCKET}; | ||
| echo 'Buckets created successfully'; | ||
| exit 0; | ||
| " | ||
|
Comment on lines
+54
to
+69
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running docker compose up, this script will get called and the minio buckets will get created. This makes it a little more automated to get the local stack running |
||
| environment: | ||
| S3_ACCESS_KEY_ID: '$S3_ACCESS_KEY_ID' | ||
| S3_SECRET_ACCESS_KEY: '$S3_SECRET_ACCESS_KEY' | ||
| BLOB_STORAGE_BUCKET: '$BLOB_STORAGE_BUCKET' | ||
| TIMING_BUCKET: '$TIMING_BUCKET' | ||
|
|
||
| volumes: | ||
| pgdata: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { FastifyRequest } from 'fastify' | ||
|
|
||
| /** | ||
| * Extracts the user ID from a Fastify request. | ||
| * In production (requireAuth=true), returns the authenticated user's sub from JWT. | ||
| * In dev mode (requireAuth=false), returns 'self-hosted' as a default user ID. | ||
| * | ||
| * @param request - The Fastify request object | ||
| * @param requireAuth - Whether authentication is required (from REQUIRE_AUTH env var) | ||
| * @returns The user ID, or undefined if not authenticated and auth is required | ||
| */ | ||
| export function getUserIdFromRequest( | ||
| request: FastifyRequest, | ||
| requireAuth: boolean, | ||
| ): string | undefined { | ||
| // Try to get authenticated user ID from JWT | ||
| const authenticatedUserId = (request as any).user?.sub | ||
|
|
||
| if (authenticatedUserId) { | ||
| return authenticatedUserId | ||
| } | ||
|
|
||
| // In dev mode (auth disabled), use 'self-hosted' as default user | ||
| if (!requireAuth) { | ||
| return 'self-hosted' | ||
| } | ||
|
|
||
| // Auth required but no user found | ||
| return undefined | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { v4 as uuidv4 } from 'uuid' | ||
| import { getStorageClient } from '../../clients/s3storageClient.js' | ||
| import { createAudioKey } from '../../constants/storage.js' | ||
| import { InteractionsRepository } from '../../db/repo.js' | ||
| import type { Interaction } from '../../db/models.js' | ||
|
|
||
| export interface CreateInteractionParams { | ||
| id: string | ||
| userId: string | ||
| title: string | ||
| asrOutput: string | ||
| llmOutput: string | null | ||
| durationMs: number | ||
| rawAudio?: Buffer | ||
| } | ||
|
|
||
| /** | ||
| * Creates an interaction in the database, optionally uploading audio to S3. | ||
| * This helper is shared between the gRPC createInteraction endpoint and | ||
| * the transcribeStreamV2Handler. | ||
| */ | ||
| export async function createInteractionWithAudio( | ||
| params: CreateInteractionParams, | ||
| ): Promise<Interaction> { | ||
| const { id, userId, title, asrOutput, llmOutput, durationMs, rawAudio } = | ||
| params | ||
|
|
||
| let rawAudioId: string | undefined | ||
|
|
||
| // If raw audio is provided, upload to S3 | ||
| if (rawAudio && rawAudio.length > 0) { | ||
| const storageClient = getStorageClient() | ||
| rawAudioId = uuidv4() | ||
| const audioKey = createAudioKey(userId, rawAudioId) | ||
|
|
||
| await storageClient.uploadObject( | ||
|
||
| audioKey, | ||
| rawAudio, | ||
| undefined, // ContentType | ||
| { | ||
| userId, | ||
| interactionId: id, | ||
| timestamp: new Date().toISOString(), | ||
| }, | ||
| ) | ||
|
|
||
| console.log( | ||
| `✅ [${new Date().toISOString()}] Uploaded audio to S3: ${audioKey}`, | ||
| ) | ||
| } | ||
|
|
||
| // Create interaction in database | ||
| const interaction = await InteractionsRepository.create({ | ||
| id, | ||
| userId, | ||
| title, | ||
| asrOutput, | ||
| llmOutput, | ||
| rawAudioId, | ||
| durationMs, | ||
| }) | ||
|
|
||
| console.log(`✅ [${new Date().toISOString()}] Created interaction: ${id}`) | ||
|
|
||
| return interaction | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to make this change to get my stack to work locally -- this is necessary since this is accessed within docker compose