### Clone and Setup YOLOv7 for SnapML Export Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md This snippet details the steps to clone the YOLOv7 repository, checkout a specific branch for SnapML export, and install necessary Python dependencies for the training and export process. ```bash # Quick Start Workflow # 1. Set up environment in Paperspace with PyTorch template git clone https://github.com/Snapchat/snapml-templates.git git clone https://github.com/hartwoolery/yolov7 cd yolov7 git checkout export-snapml pip install -r requirements.txt ``` -------------------------------- ### Development Environment Setup (Bash) Source: https://github.com/snapchat/spectacles-sample/blob/main/DJ Specs/README.md Commands for setting up the development environment, including cloning the repository, initializing Git LFS for large assets, and installing pre-commit hooks. ```bash # Clone the repository git clone cd DJ-Specs # Initialize Git LFS for large files git lfs install git lfs pull # Set up pre-commit hooks pre-commit install ``` -------------------------------- ### Full ASR Example Script (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/asr-module.md A complete TypeScript example demonstrating the ASR Module's lifecycle within a Lens Studio component. It includes initializing the module, setting up callbacks, starting transcription on awake, and stopping the session. ```typescript @component export class AsrExample extends BaseScriptComponent { private asrModule = require('LensStudio:AsrModule'); private onTranscriptionUpdate(eventArgs: AsrModule.TranscriptionUpdateEvent) { print( `onTranscriptionUpdateCallback text=${eventArgs.text}, isFinal=${eventArgs.isFinal}` ); } private onTranscriptionError(eventArgs: AsrModule.AsrStatusCode) { print(`onTranscriptionErrorCallback errorCode: ${eventArgs}`); switch (eventArgs) { case AsrModule.AsrStatusCode.InternalError: print('stopTranscribing: Internal Error'); break; case AsrModule.AsrStatusCode.Unauthenticated: print('stopTranscribing: Unauthenticated'); break; case AsrModule.AsrStatusCode.NoInternet: print('stopTranscribing: No Internet'); break; } } onAwake(): void { const options = AsrModule.AsrTranscriptionOptions.create(); options.silenceUntilTerminationMs = 1000; options.mode = AsrModule.AsrMode.HighAccuracy; options.onTranscriptionUpdateEvent.add((eventArgs) => this.onTranscriptionUpdate(eventArgs); ); options.onTranscriptionErrorEvent.add((eventArgs) => this.onTranscriptionError(eventArgs); ); this.asrModule.startTranscribing(options); } private stopSession(): void { this.asrModule.stopTranscribing(); } } ``` -------------------------------- ### Complete Persistent Storage Script Example Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This is a comprehensive script combining all functionalities: accessing storage, defining keys, reading, writing integers, binding events, and handling storage full conditions. It provides a complete implementation for managing a persistent score. ```javascript // Create key for the score variable var store = global.persistentStorageSystem.store; var scoreKey = 'totalScore'; // Retrieve the score from persistent storage var currentGameScore = store.getInt(scoreKey); // Print the score print('Loaded score: ' + currentGameScore); // Function that increments the score function incrementScore() { // Increment the score by 1 currentGameScore += 1; // Store the current score in persistent storage store.putInt(scoreKey, currentGameScore); // Print the current score print('Current score: ' + currentGameScore); } // Bind the incrementScore function to the "Mouth Opened" event script.createEvent('MouthOpenedEvent').bind(incrementScore); // If persistent storage is full, clear it store.onStoreFull = function () { store.clear(); print('Storage cleared'); }; ``` -------------------------------- ### Diagram Storage Structure Example Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Illustrates the structured format for storing diagram definitions, including the title, number of nodes, node types, content, and image/3D prompts. ```plaintext DiagramTitle: 'Solar System Overview' Nodes: 5 Node 1: text Title: 'Introduction' Content: 'The solar system consists of...' Node 2: image Title: 'The Sun' Content: 'Our star is...' Image prompt: 'realistic image of the sun with solar flares' ``` -------------------------------- ### OpenAIAssistant System Prompt - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Defines the system prompt for the OpenAIAssistant, an educational AI tutor. It emphasizes clear explanations, breaking down complex topics, using examples, and critical response length limitations (exactly 300 characters). ```typescript private readonly instructions: string = `You are an educational AI tutor designed to help students learn and understand complex topics. Your primary goals are to: - Provide clear, accurate explanations of educational concepts - Break down complex topics into digestible parts - Use examples and analogies to enhance understanding - Ask clarifying questions to gauge comprehension - Encourage critical thinking and curiosity - Adapt your teaching style to the student's level 🔥 CRITICAL RESPONSE LENGTH REQUIREMENT: - Your responses MUST be limited to exactly 300 characters or fewer - This is a HARD LIMIT that cannot be exceeded under any circumstances - Count characters carefully and stop exactly at 300 characters - Be concise while maintaining educational value - If a topic needs more explanation, invite follow-up questions - Prioritize the most important information within the character limit Always maintain an encouraging, patient, and supportive tone. Focus on helping students build knowledge and confidence in their learning journey within the strict 300-character limit.`; ``` -------------------------------- ### Start Transcribing (JavaScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/asr-module.md Initiates the speech-to-text transcription process by passing the configured options object to the `startTranscribing` function of the AsrModule. ```javascript asrModule.startTranscribing(options); ``` -------------------------------- ### Access Persistent Storage Data Store Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This snippet shows how to get a reference to the data store within the Persistent Storage system. This is the first step to interact with persistent storage. ```javascript var store = global.persistentStorageSystem.store; ``` -------------------------------- ### Example Debug Output for Agent Orchestrator and Tools Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md This example demonstrates the format of debug output logs generated by the AgentOrchestrator and specific tools like the SummaryTool. It provides insights into AI routing decisions, the reasoning behind them, and the processing details of queries. ```log AgentOrchestrator: 🧠 AI routing decision: "summary_tool" for query: "What did the teacher say about..." ToolRouter: 💡 Reasoning: Focuses on previous lecture summary content SummaryTool: 📋 Processing summary-focused query with character limit 150 ``` -------------------------------- ### TypeScript WebSocket Example Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/websocket.md A comprehensive TypeScript example showcasing the usage of the Spectacles WebSocket API within a Lens Studio script. ```APIDOC ## TypeScript WebSocket Usage ### Description This code snippet demonstrates how to use the `InternetModule` to create and manage a WebSocket connection in a Lens Studio script. It covers event handling for connection opening, receiving messages (both text and binary), connection closing, and errors. ### Method N/A (Client-side script) ### Endpoint N/A (Client-side script) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```typescript @component export class WebSocketExample extends BaseScriptComponent { @input remoteServiceModule: RemoteServiceModule; private socket!: WebSocket; async onAwake() { this.socket = this.remoteServiceModule.createWebSocket('wss://'); this.socket.binaryType = 'blob'; this.socket.onopen = (event: WebSocketEvent) => { this.socket.send('Message 1'); const message: number[] = [77, 101, 115, 115, 97, 103, 101, 32, 50]; const bytes = new Uint8Array(message); this.socket.send(bytes); }; this.socket.onmessage = async (event: WebSocketMessageEvent) => { if (event.data instanceof Blob) { const bytes = await event.data.bytes(); const text = await event.data.text(); print('Received binary message, printing as text: ' + text); } else { const text: string = event.data; print('Received text message: ' + text); } }; this.socket.onclose = (event: WebSocketCloseEvent) => { if (event.wasClean) { print('Socket closed cleanly'); } else { print('Socket closed with error, code: ' + event.code); } }; this.socket.onerror = (event: WebSocketEvent) => { print('Socket error'); }; } } ``` ### Response N/A (This is a client-side script example, responses are handled within the `onmessage` event listener.) ### Error Handling Errors are handled via the `onerror` event listener for the WebSocket object. ``` -------------------------------- ### Diagram Creator Tool System Prompt (JavaScript/JSON) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Defines system prompts for the DiagramCreatorTool, which assists in generating educational topics for diagrams. It includes an example query-based prompt for topic generation and a system message setting the AI's role as an educational assistant focused on clear topic titles. ```javascript // Example topic generation prompt within DiagramCreatorTool: const prompt = `Based on this query about creating a diagram: "${query}", generate 5-8 relevant educational topics that should be included in the diagram. Format each topic as a clear, concise title.`; ``` ```json // System message for context: { "role": "system", "content": "You are an educational assistant helping to create meaningful diagrams. Generate clear, relevant topic titles." } ``` -------------------------------- ### Build Spatial System Prompt (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Generates a system prompt for an AI model designed to assist students during lectures by referencing both the lecture content and the visual environment. It includes parameters for spatial context and whether camera input is enabled. The prompt guides the AI to use visual perception for contextual responses. ```typescript private buildSpatialSystemPrompt(spatialContext: string, enableImageInput: boolean): string { let prompt = "You are answering specific questions regarding a lecture that is going on now and the student sees the surrounding environment.\n\n"; prompt += "SPATIAL CONTEXT:\n"; if (spatialContext) { prompt += `Environment details: ${spatialContext}\n`; } if (enableImageInput) { prompt += "IMPORTANT: You have real-time camera input enabled and can see the current environment.\n"; prompt += "You should analyze what you see in front of you and describe the visual environment.\n"; prompt += "Use your visual perception to answer questions about what is currently visible.\n"; prompt += "When asked 'what do you see', describe exactly what is in your current field of view.\n"; } prompt += "\nINSTRUCTIONS:\n"; prompt += "- Answer questions based on the current lecture environment and visual context\n"; prompt += "- Reference what you can see or understand about the current setting\n"; prompt += "- Help the student understand concepts in relation to their current learning environment\n"; prompt += "- If visual input is available, use it to provide specific, contextual responses\n"; prompt += "- Focus on real-time educational assistance during live lectures\n"; prompt += "- Connect visual observations to educational concepts when relevant\n"; prompt += "- Maintain awareness of the spatial/physical learning context\n"; return prompt; } ``` -------------------------------- ### OpenAI Chat Completions API Call (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md Example of making a text-only chat completion request to the OpenAI API using the provided package. It demonstrates setting the model, messages, temperature, and max tokens for the request. ```typescript import { OpenAI } from "Remote Service Gateway.lspkg/HostedExternal/OpenAI"; // Text-only generation const response = await OpenAI.chatCompletions({ model: 'gpt-4o-mini', messages: formattedMessages, temperature: 0.7, max_tokens: 150 }); ``` -------------------------------- ### Gemini Generate Content API Call (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md Example of interacting with the Gemini API for multimodal content generation, including image input. It showcases how to specify the model and structure the content with text and image data for processing. ```typescript import { Gemini } from "Remote Service Gateway.lspkg/HostedExternal/Gemini"; // Multimodal generation with image const response = await Gemini.generateContent({ model: 'gemini-2.0-flash-live-preview', contents: [{ parts: [{ text: query }, { image: imageData }] }] }); ``` -------------------------------- ### Animate with LSTween (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Essentials/README.md Provides examples of using the LSTween library for animations, including scaling objects and transitioning material colors. It requires importing LSTween and EasingFunction. Input parameters include the target object, properties to animate, duration, and easing functions. ```typescript // Import the tweening library import { LSTween, EasingFunction } from './LSTween'; // Scale animation LSTween.scaleToWorld( this.sceneObject, new vec3(1.2, 1.2, 1.2), 0.5, EasingFunction.OutBack ); // Color transition const material = this.getComponent("Component.RenderMeshVisual").getMaterial(0); LSTween.colorTo( material, "diffuse", new vec4(1, 0, 0, 1), 0.3, EasingFunction.Linear ); ``` -------------------------------- ### Connect and Communicate with WebSocket in TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/websocket.md This TypeScript example demonstrates how to establish a WebSocket connection using the `createWebSocket` command. It includes handling connection opening, sending text and binary messages, processing incoming messages (both text and binary), and managing connection closure and errors. ```typescript import { BaseScriptComponent } from '@snapchat/lens-engine'; import { InternetModule, RemoteServiceModule, WebSocket, WebSocketEvent, WebSocketMessageEvent, WebSocketCloseEvent } from '@snapchat/lens-engine/internet'; @component export class WebSocketExample extends BaseScriptComponent { @input remoteServiceModule: RemoteServiceModule; private socket!: WebSocket; // Method called when the script is awake async onAwake() { this.socket = this.remoteServiceModule.createWebSocket('wss://'); this.socket.binaryType = 'blob'; // Listen for the open event this.socket.onopen = (event: WebSocketEvent) => { // Socket has opened, send a message back to the server this.socket.send('Message 1'); // Try sending a binary message // (the bytes below spell 'Message 2') const message: number[] = [77, 101, 115, 115, 97, 103, 101, 32, 50]; const bytes = new Uint8Array(message); this.socket.send(bytes); }; // Listen for messages this.socket.onmessage = async (event: WebSocketMessageEvent) => { if (event.data instanceof Blob) { // Binary frame, can be retrieved as either Uint8Array or string const bytes = await event.data.bytes(); const text = await event.data.text(); print('Received binary message, printing as text: ' + text); } else { // Text frame const text: string = event.data; print('Received text message: ' + text); } }; this.socket.onclose = (event: WebSocketCloseEvent) => { if (event.wasClean) { print('Socket closed cleanly'); } else { print('Socket closed with error, code: ' + event.code); } }; this.socket.onerror = (event: WebSocketEvent) => { print('Socket error'); }; } } ``` -------------------------------- ### Create and Access Cloud Store Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md Demonstrates how to create a CloudStore instance using CloudStorageOptions and retrieve it via the CloudStorageModule. It includes callback functions for successful creation ('onCloudStoreReady') and error handling ('onError'). Note that the returned CloudStore instance is always the same, regardless of options passed after the first call. ```javascript const cloudStorageOptions = CloudStorageOptions.create(); script.cloudStorageModule.getCloudStore( cloudStorageOptions, onCloudStoreReady, onError ); function onCloudStoreReady(store) { print('CloudStore created'); script.store = store; } function onError(code, message) { print('Error: ' + code + ' ' + message); } ``` -------------------------------- ### OpenAI Chat Completions Example - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/remote-service-gateway.md Demonstrates how to use the OpenAI chat completions API with Spectacles Lenses. It sends a system message and a user prompt to a GPT model and prints the response or any errors encountered. Requires the Remote Service Gateway package and a valid API token. ```typescript import { OpenAI } from 'Remote Service Gateway.lspkg/HostedExternal/OpenAI'; @component export class OpenAIExample extends BaseScriptComponent { onAwake() { this.createEvent('OnStartEvent').bind(() => { this.doChatCompletions(); }); } doChatCompletions() { OpenAI.chatCompletions({ model: 'gpt-4.1-nano', messages: [ { role: 'system', content: "You are an incredibly smart but witty AI assistant who likes to answers life's greatest mysteries in under two sentences", }, { role: 'user', content: 'Is a hotdog a sandwich?', }, ], temperature: 0.7, }) .then((response) => { print(response.choices[0].message.content); }) .catch((error) => { print('Error: ' + error); }); } } ``` -------------------------------- ### DeepSeek Chat Completions in TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/remote-service-gateway.md Demonstrates integrating DeepSeek's R1 Reasoning API for chat completions in Spectacles Lenses. This example includes system instructions and a user prompt, printing both the reasoning and final answer, or any errors encountered. Note that DeepSeek may have extended response times. ```typescript import { DeepSeek } from 'Remote Service Gateway.lspkg/HostedSnap/Deepseek'; import { DeepSeekTypes } from 'Remote Service Gateway.lspkg/HostedSnap/DeepSeekTypes'; @component export class DeepSeekExample extends BaseScriptComponent { onAwake() { this.createEvent('OnStartEvent').bind(() => { this.doChatCompletions(); }); } doChatCompletions() { let messageArray: Array = [ { role: 'system', content: "You are an incredibly smart but witty AI assistant who likes to answers life's greatest mysteries in under two sentences", }, { role: 'user', content: 'Is a hotdog a sandwich?', }, ]; const deepSeekRequest: DeepSeekTypes.ChatCompletions.Request = { model: 'DeepSeek-R1', messages: messageArray, max_tokens: 2048, temperature: 0.7, }; DeepSeek.chatCompletions(deepSeekRequest) .then((response) => { let reasoningContent = response?.choices[0]?.message?.reasoning_content; let messageContent = response?.choices[0]?.message?.content; print('Reasoning: ' + reasoningContent); print('Final answer: ' + messageContent); }) .catch((error) => { print('Error: ' + error); }); } } ``` -------------------------------- ### Download Dataset and Pre-trained Weights Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md This snippet shows how to download a dataset using the Roboflow Python package and fetch pre-trained YOLOv7 weights, essential for the model training process. ```bash # 2. Download dataset from Roboflow in YOLOv7 format pip install --upgrade roboflow # Use Roboflow API to download your dataset # 3. Download pre-trained weights wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt ``` -------------------------------- ### AI Playground Sample Components Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Lists the core components within the `/Context/ai-playground-scripts` folder for the AI Playground sample, including direct OpenAI and Gemini integrations, image and 3D model generation, and voice transcription. ```TypeScript // OpenAIAssistant.ts - Direct OpenAI integration // GeminiAssistant.ts - Google Gemini multimodal AI // ImageGenerator.ts - AI image creation // InteractableSnap3DGenerator.ts - 3D model generation // ASRQueryController.ts - Voice transcription ``` -------------------------------- ### Get Cloud Storage Module Instance Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This snippet shows how to obtain an instance of the CloudStorageModule, which is necessary to interact with the persistent cloud storage. It requires a CloudStorageModule asset to be attached to the scene object containing the script. ```javascript // @input Asset.CloudStorageModule cloudStorageModule ``` -------------------------------- ### Train YOLOv7 Model for Object Detection Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md This code snippet demonstrates how to initiate the training of a YOLOv7 model using a custom dataset. It specifies the data configuration, model architecture, pre-trained weights, image size, batch size, epochs, and output names. ```bash # 4. Train model python train.py --data your-dataset/data.yaml --cfg cfg/training/yolov7-tiny.yaml \ --weights yolov7-tiny.pt --img 224 224 --batch-size 64 --epochs 200 \ --name detection --device 0 --hyp data/hyp.scratch.tiny.yaml ``` -------------------------------- ### Presentation Initialization and URL Parameter Handling (JavaScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Public Speaker/SyncPresentationOnWeb/direct-controller.html Sets up the initial state of the application and handles URL parameters for presentation ID and access token. It automatically attempts to load the presentation if both parameters are present. ```javascript // State let presentationData = null; let currentSlideIndex = 0; let slides = []; let socket = null; // Initialize function initialize() { // Set up event listeners loadButton.addEventListener("click", loadPresentation); connectButton.addEventListener("click", connect); disconnectButton.addEventListener("click", disconnect); previousButton.addEventListener("click", () => { goToPreviousSlide(); sendCommand("previous"); }); nextButton.addEventListener("click", () => { goToNextSlide(); sendCommand("next"); }); // Log initialization addLogEntry("Page initialized", "info"); // Check for URL parameters const urlParams = new URLSearchParams(window.location.search); const presentationId = urlParams.get("id"); const accessToken = urlParams.get("token"); if (presentationId) { presentationIdInput.value = presentationId; } if (accessToken) { accessTokenInput.value = accessToken; } if (presentationId && accessToken) { loadPresentation(); } } ``` -------------------------------- ### GeminiAssistant: Multimodal AI Tutor Prompt Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md Defines instructions for a multimodal AI tutor, emphasizing clear explanations and a supportive tone with responses kept under 300 characters. ```typescript const instructions = `You are an educational AI tutor. Provide clear, accurate explanations of educational concepts. Keep responses under 300 characters. Be encouraging and supportive.`; ``` -------------------------------- ### Build Summary System Prompt (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Constructs a system prompt for an AI model to generate concise summaries of provided document content. It includes specific instructions on response length, conciseness, and focus. Dependencies include the 'summaryContext' object containing summaries and an 'educationalFocus' boolean. ```typescript private buildSummarySystemPrompt(summaryContext: any, educationalFocus: boolean): string { let prompt = "You are answering specific questions regarding this document:\n\n"; // Inject summary content into system prompt if (summaryContext && summaryContext.summaries && Array.isArray(summaryContext.summaries)) { prompt += "DOCUMENT SUMMARY:\n"; summaryContext.summaries.forEach((summary: any, index: number) => { if (summary.title && summary.content) { prompt += `\nSection ${index + 1}: ${summary.title}\n`; prompt += `${summary.content}\n`; } }); prompt += "\n"; } prompt += "CRITICAL INSTRUCTIONS:\n"; prompt += "- MAXIMUM RESPONSE LENGTH: 150 characters (this is a hard limit for AR display)\n"; prompt += "- Be EXTREMELY concise - use short phrases, not full sentences when possible\n"; prompt += "- Answer with key facts only, no elaboration\n"; prompt += "- If asked about multiple topics, focus on the most important one\n"; prompt += "- Omit pleasantries, transitional phrases, and filler words\n"; prompt += "- Use bullet points or numbered lists for multiple items\n"; prompt += "- Focus on the core learning point only\n"; prompt += "- Example good response: 'Neural networks: layers process data, learn patterns'\n"; prompt += "- Example bad response: 'Neural networks are computational systems that process data through multiple layers to learn patterns'\n"; return prompt; } ``` -------------------------------- ### Path Creation and State Management (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Path Pioneer/README.md PathMaker.ts controls the various states involved in creating paths. It differentiates between a 'path' (start and finish lines) and a 'loop' (only a start line). It also manages visual elements like preview points for arrows and a 'pathRmv' parameter for a trailing mesh. ```typescript class PathMaker { // ... implementation details for path creation states } ``` ```typescript class BuildingPathState { // ... implementation details for the building path state, including preview points and pathRmv } ``` -------------------------------- ### Clone Project Repository (Bash) Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md Instructions for cloning the project repository using Git, emphasizing the necessity of Git LFS for handling large project assets. This is the initial step to obtain the project files. ```bash git clone [repository-url] cd agentic-playground ``` -------------------------------- ### Content Positioning Utilities - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md Offers utilities for positioning AR content, matching transforms, and rendering lines. Includes helper scripts for managing object placement and spatial relationships. ```typescript import SmartTether from "./SmartTether.ts"; import MatchTransform from "./MatchTransform.ts"; import MatchTransformLocal from "./MatchTransformLocal.ts"; import InBetween from "./InBetween.ts"; import Line from "./Line.ts"; // Utilities for content positioning, transform matching, and line rendering. // Assists in accurately placing and aligning AR content in the scene. ``` -------------------------------- ### Camera Service and Texture Management - TypeScript/JavaScript Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md Provides access to camera input and manages camera textures for use in ML detection and AR rendering. Essential for real-time visual data processing. ```typescript import CameraService from "./CameraService"; import CameraTexture from "./CameraTexture.js"; // Handles camera access and manages camera textures. // Crucial for feeding visual data to the ML model and rendering. ``` -------------------------------- ### World Query Module Spatializer - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md Manages object detection, hit-testing, and placement onto real-world surfaces using the World Query Module. Relies on PinholeCapture for camera pose and MLSpatializer for ML model execution. ```typescript import WorldQueryModuleSpatializer from "./WorldQueryModuleSpatializer"; import PinholeCapture from "./PinholeCapture"; import MLSpatializer from "./MLSpatializer"; import DetectionContainer from "./DetectionContainer"; import ClosedPolyline from "./ClosedPolyline"; // Main controller for World Query Module mode // Handles detection, hit-testing, and placement of detected objects. // Requires WorldQueryModuleSpatializer, PinholeCapture, and MLSpatializer scripts. ``` -------------------------------- ### Start BLE Device Scanning Source: https://github.com/snapchat/spectacles-sample/blob/main/BLE Playground/README.md Initiates the Bluetooth Low Energy scanning process to discover nearby compatible devices. This function is part of the BLE service handler. ```typescript // Example: Start scanning for devices bleServiceHandler.onStartScan(); ``` -------------------------------- ### Bind Function to Mouth Opened Event Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This code binds the `incrementScore` function to the `MouthOpenedEvent`. This means the function will execute every time the user opens their mouth while the Lens is active. ```javascript script.createEvent('MouthOpenedEvent').bind(incrementScore); ``` -------------------------------- ### Summary Generation Flow Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md Illustrates the data flow for generating lecture summaries, starting from teacher's speech to the display of interactive summary cards on the UI. It involves ASR, storage, summarization, and UI components. ```text Teacher speaks → SummaryASRController → SummaryStorage → AISummarizer → SummaryBridge → SummaryComponent ``` -------------------------------- ### GeminiAssistant System Prompt - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Defines the system prompt for the GeminiAssistant, also an educational AI tutor. It prioritizes clear explanations and adherence to a strict character limit of under 300 characters, while maintaining a supportive tone. ```typescript private readonly instructions: string = `You are an educational AI tutor. Provide clear, accurate explanations of educational concepts. Keep responses under 300 characters. Be encouraging and supportive.`; ``` -------------------------------- ### Modular Tween System for Animations Source: https://github.com/snapchat/spectacles-sample/blob/main/DJ Specs/README.md A central animation system utilizing a modular tweening approach for creating smooth animations. It allows starting, stopping, and pausing animations on specified objects and properties. ```javascript // TweenManager.js - Central animation system TweenManager.startTween(object, property, target, duration, easing) TweenManager.stopTween(tweenId) TweenManager.pauseAllTweens() ``` -------------------------------- ### Lens Initialization and Ground Offset Caching (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Path Pioneer/README.md The LensInitializer.ts script is responsible for starting the lens and caching the ground offset from the camera, which represents the player's height. It's a foundational script for lens functionality. ```typescript class LensInitializer { // ... implementation details for initializing the lens and caching ground offset } ``` -------------------------------- ### Basic Object Instantiation (TypeScript/JavaScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Essentials/README.md Demonstrates simplified object instantiation within specific areas like circles, colliders, and squares. ```typescript /** * Simplified circle instantiation logic. * Assumes a 'createObject' function is available in the scope. */ function instantiateCircle() { // Example: Create an object at the center of a circle // createObject({ position: { x: 0, y: 0, z: 0 } }); console.log('Simplified circle instantiation.'); } /** * Instantiates objects with colliders. * Assumes a 'createObjectWithCollider' function is available. */ function instantiateWithColliders() { // Example: Create an object with a default collider // createObjectWithCollider({ position: { x: 1, y: 1, z: 1 }, colliderType: 'box' }); console.log('Instantiating objects with colliders.'); } /** * Instantiates objects within a square area. * Assumes a 'createObject' function is available. */ function instantiateInSquareArea() { // Example: Create an object within a 10x10 square area around the origin // const x = (Math.random() - 0.5) * 10; // const z = (Math.random() - 0.5) * 10; // createObject({ position: { x: x, y: 0, z: z } }); console.log('Instantiating objects in a square area.'); } ``` ```javascript /** * Simplified circle instantiation logic. * Assumes a 'createObject' function is available in the scope. */ function instantiateCircle() { // Example: Create an object at the center of a circle // createObject({ position: { x: 0, y: 0, z: 0 } }); console.log('Simplified circle instantiation.'); } /** * Instantiates objects with colliders. * Assumes a 'createObjectWithCollider' function is available. */ function instantiateWithColliders() { // Example: Create an object with a default collider // createObjectWithCollider({ position: { x: 1, y: 1, z: 1 }, colliderType: 'box' }); console.log('Instantiating objects with colliders.'); } /** * Instantiates objects within a square area. * Assumes a 'createObject' function is available. */ function instantiateInSquareArea() { // Example: Create an object within a 10x10 square area around the origin // const x = (Math.random() - 0.5) * 10; // const z = (Math.random() - 0.5) * 10; // createObject({ position: { x: x, y: 0, z: z } }); console.log('Instantiating objects in a square area.'); } ``` -------------------------------- ### JavaScript ASR Module Usage for Speech Transcription Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/asr-module.md This snippet shows how to initialize, start, and stop speech transcription using the LensStudio ASR module. It includes callback functions for handling transcription updates and errors. ```javascript const asrModule = require('LensStudio:AsrModule'); function onTranscriptionError(errorCode) { print(`onTranscriptionErrorCallback errorCode: ${errorCode}`); switch (errorCode) { case AsrModule.AsrStatusCode.InternalError: print('stopTranscribing: Internal Error'); break; case AsrModule.AsrStatusCode.Unauthenticated: print('stopTranscribing: Unauthenticated'); break; case AsrModule.AsrStatusCode.NoInternet: print('stopTranscribing: No Internet'); break; } } function onTranscriptionUpdate(eventArgs) { var text = eventArgs.text; var isFinal = eventArgs.isFinal; print(`onTranscriptionUpdateCallback text=${text}, isFinal=${isFinal}`); } function startSession() { var options = AsrModule.AsrTranscriptionOptions.create(); options.silenceUntilTerminationMs = 1000; options.mode = AsrModule.AsrMode.HighAccuracy; options.onTranscriptionUpdateEvent.add(onTranscriptionUpdateCallback); options.onTranscriptionErrorEvent.add(onTranscriptionErrorCallback); // Start session asrModule.startTranscribing(options); } function stopSession() { asrModule.stopTranscribing().then(function () { print(`stopTranscribing successfully`); }); } ``` -------------------------------- ### SpatializerUtils.ts: Utility Functions Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md A utility script containing helper functions for smoothing, linear interpolation (lerping), and other geometry-related operations essential for smooth AR effect transitions. ```typescript class SpatializerUtils { static smoothValue(current: number, target: number, smoothFactor: number): number { // Implementation for smoothing a value return current + (target - current) * smoothFactor; } static lerp(a: number, b: number, t: number): number { // Implementation for linear interpolation return a + (b - a) * t; } } ``` -------------------------------- ### Define Key for Persistent Storage Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This code defines a string key that will be used to access and manipulate specific data entries within the persistent storage. It's essential for reading and writing values. ```javascript var scoreKey = 'totalScore'; ``` -------------------------------- ### UI Helper for Recording Activation (TypeScript) Source: https://github.com/snapchat/spectacles-sample/blob/main/Voice Playback/README.md A UI helper script that monitors the activation state of the microphone recorder. It listens for events indicating that a recording has started and can be used to update UI elements or trigger visual feedback for the user. ```typescript import { Script } from "@vnpt/spectacles-scripting"; export class ActivateMicrophoneRecorder extends Script { // Reference to the MicrophoneRecorder script, assuming it's a component on the same object or accessible // private microphoneRecorder: MicrophoneRecorder; // You would typically get a reference to the recorder script in `awake` or `start` // awake(): void { // this.microphoneRecorder = this.getComponent(MicrophoneRecorder); // } // Example method to be called when recording starts onRecordingStart(): void { console.log("UI Feedback: Recording has started."); // Update UI elements, show a "recording" indicator, etc. // e.g., this.getSceneObject("RecordingIndicator").enabled = true; } // Example method to be called when recording stops onRecordingStop(): void { console.log("UI Feedback: Recording has stopped."); // Update UI elements, hide the "recording" indicator, etc. // e.g., this.getSceneObject("RecordingIndicator").enabled = false; } // This script would likely subscribe to events emitted by MicrophoneRecorder // start(): void { // // Assuming MicrophoneRecorder emits events like 'recordingStarted' and 'recordingStopped' // this.microphoneRecorder.on("recordingStarted", this.onRecordingStart); // this.microphoneRecorder.on("recordingStopped", this.onRecordingStop); // } // end(): void { // // Clean up event listeners // // this.microphoneRecorder.off("recordingStarted", this.onRecordingStart); // // this.microphoneRecorder.off("recordingStopped", this.onRecordingStop); // } } ``` -------------------------------- ### OpenAIAssistant: Primary AI Tutor Prompt Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/README.md Provides instructions for the primary AI tutor, focusing on educational explanations, character limits, and maintaining an encouraging tone. It emphasizes a strict 300-character limit for responses. ```typescript const instructions = `You are an educational AI tutor designed to help students learn and understand complex topics. Your primary goals are to: - Provide clear, accurate explanations of educational concepts - Break down complex topics into digestible parts - Use examples and analogies to enhance understanding - Ask clarifying questions to gauge comprehension - Encourage critical thinking and curiosity - Adapt your teaching style to the student's level 🔥 CRITICAL RESPONSE LENGTH REQUIREMENT: - Your responses MUST be limited to exactly 300 characters or fewer - This is a HARD LIMIT that cannot be exceeded under any circumstances - Count characters carefully and stop exactly at 300 characters - Be concise while maintaining educational value - If a topic needs more explanation, invite follow-up questions - Prioritize the most important information within the character limit Always maintain an encouraging, patient, and supportive tone. Focus on helping students build knowledge and confidence in their learning journey within the strict 300-character limit.`; ``` -------------------------------- ### Summary Tool Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Specialized tool for summary-focused interactions, querying summarized content and using the orchestrator's default model. ```typescript class SummaryTool { // Handles questions about summarized content // Sources: User query + summary storage data // Uses default model from orchestrator // Specialized for educational content questions } ``` -------------------------------- ### Read Integer from Persistent Storage Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This snippet demonstrates how to retrieve an integer value from persistent storage using a predefined key. If the key does not exist, it defaults to 0. This is useful for loading game scores or other numerical data. ```javascript var currentGameScore = store.getInt(scoreKey); print('Loaded score: ' + currentGameScore); ``` -------------------------------- ### GeneralConversationTool System Prompt - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Defines the system prompt for the GeneralConversationTool, a helpful and friendly AI assistant for educational support. It includes conversational style guidelines and a hard character limit for responses. ```typescript const systemPrompt = `You are a helpful and friendly AI assistant with a focus on educational support. RESPONSE REQUIREMENTS: - Your responses MUST be limited to exactly ${validMaxLength} characters or fewer - This is a HARD LIMIT that cannot be exceeded under any circumstances - Be conversational, friendly, and helpful ${educationalFocus ? '- Maintain an educational focus when appropriate' : ''} - Use a natural, engaging tone - If the question is very general (like greetings), offer to help with specific topics CONVERSATION STYLE: - Be warm and approachable - Ask follow-up questions to better assist the user - Provide helpful suggestions when appropriate - Keep responses concise but informative within the character limit Remember: Be helpful, friendly, and educational while staying within the ${validMaxLength} character limit.`; ``` -------------------------------- ### DeepSeek Chat Completions in JavaScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/remote-service-gateway.md Demonstrates how to use the DeepSeek service to get chat completions. It sends a system message and a user message, then prints the reasoning and content of the AI's response. Requires the 'Remote Service Gateway.lspkg/HostedSnap/Deepseek' module. ```javascript const DeepSeek = require('Remote Service Gateway.lspkg/HostedSnap/Deepseek').DeepSeek; script.createEvent('OnStartEvent').bind(() => { doChatCompletions(); }); function doChatCompletions() { let messageArray = [ { role: 'system', content: "You are an incredibly smart but witty AI assistant who likes to answers life's greatest mysteries in under two sentences", }, { role: 'user', content: 'Is a hotdog a sandwich?', }, ]; const deepSeekRequest = { model: 'DeepSeek-R1', messages: messageArray, max_tokens: 2048, temperature: 0.7, }; DeepSeek.chatCompletions(deepSeekRequest) .then((response) => { let reasoningContent = response.choices[0].message.reasoning_content; let messageContent = response.choices[0].message.content; print('Reasoning: ' + reasoningContent); print('Final answer: ' + messageContent); }) .catch((error) => { print('Error: ' + error); }); } ``` -------------------------------- ### Export Trained YOLOv7 Model to ONNX for SnapML Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md This snippet details the command to export a trained YOLOv7 model to the ONNX format, optimized for use with SnapML. It includes options for model simplification and specifying input image sizes. ```bash # 5. Export to ONNX for SnapML python export.py --weights runs/train/detection/weights/best.pt --grid \ --simplify --export-snapml --img-size 224 224 --max-wh 224 ``` -------------------------------- ### DebugVisualizer.ts: Debugging Visualization Source: https://github.com/snapchat/spectacles-sample/blob/main/SnapML Starter/README.md An optional script used for visualizing detection points and bounding boxes in 2D for debugging purposes. This script helps in understanding the detection pipeline's output. ```typescript class DebugVisualizer { visualizeDetections(detections: DetectionResult[]): void { // Draw bounding boxes and points on the screen } } ``` -------------------------------- ### Handle Persistent Storage Full Event Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/persistent-storage.md This callback function is executed when persistent storage becomes full. It automatically clears all stored data to make space for new entries, preventing data loss due to capacity limits. ```javascript store.onStoreFull = function () { store.clear(); print('Storage cleared'); }; ``` -------------------------------- ### ToolRouter AI Prompt - TypeScript Source: https://github.com/snapchat/spectacles-sample/blob/main/Agentic Playground/Context/tutorial-script.md Defines the system prompt for the ToolRouter AI, which intelligently selects the most appropriate tool based on user queries and predefined routing rules. It dynamically includes tool descriptions and the user's query, aiming for optimized character limits for AR displays. ```typescript const routingPrompt = `You are an intelligent tool router for an educational AI assistant. Analyze the user query and select the most appropriate tool.\n\nAVAILABLE TOOLS:\n${toolDescriptions}\n\nUSER QUERY: "${query}"\n\nROUTING RULES:\n1. If user asks about "the lecture" or lecture content, and summary context is available, use "summary_tool"\n2. If user requests diagrams, visualizations, or mind maps, use "diagram_tool" \n3. If user asks about current/live environment or "what do you see", use "spatial_tool"\n4. For general questions without specific tool needs, use "general_conversation"\n\nRespond with ONLY the tool name (e.g., "summary_tool", "diagram_tool", "spatial_tool", "general_conversation`);`; ```