|
| 1 | +/** |
| 2 | + * Example: Using GitClaw SDK with Lyzr AI Studio |
| 3 | + * |
| 4 | + * Prerequisites: |
| 5 | + * 1. Set your Lyzr API key: export LYZR_API_KEY="sk-default-..." |
| 6 | + * 2. Set a dummy OpenAI key (needed by pi-ai): export OPENAI_API_KEY="dummy" |
| 7 | + * 3. Have an agent directory with agent.yaml: ~/assistant/ |
| 8 | + * |
| 9 | + * Run: |
| 10 | + * npx tsx examples/lyzr-sdk.ts |
| 11 | + */ |
| 12 | + |
| 13 | +import { query } from "../dist/exports.js"; |
| 14 | + |
| 15 | +const LYZR_API_KEY = process.env.LYZR_API_KEY; |
| 16 | +if (!LYZR_API_KEY) { |
| 17 | + console.error("Error: Set LYZR_API_KEY environment variable"); |
| 18 | + process.exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +// Ensure pi-ai can find an API key (it checks OPENAI_API_KEY for openai-completions API) |
| 22 | +if (!process.env.OPENAI_API_KEY) { |
| 23 | + process.env.OPENAI_API_KEY = LYZR_API_KEY; |
| 24 | +} |
| 25 | + |
| 26 | +// Your Lyzr agent ID (created via studio.lyzr.ai or the install.sh setup) |
| 27 | +const LYZR_AGENT_ID = process.env.GITCLAW_LYZR_AGENT_ID || "your-agent-id-here"; |
| 28 | + |
| 29 | +async function main() { |
| 30 | + console.log("Starting GitClaw with Lyzr backend...\n"); |
| 31 | + |
| 32 | + const result = query({ |
| 33 | + prompt: "Hello! What can you help me with today?", |
| 34 | + dir: process.env.HOME + "/assistant", |
| 35 | + |
| 36 | + // Model format: lyzr:<agent-id>@<base-url> |
| 37 | + // The OpenAI SDK appends /chat/completions to the base URL |
| 38 | + model: `lyzr:${LYZR_AGENT_ID}@https://agent-prod.studio.lyzr.ai/v4`, |
| 39 | + |
| 40 | + // Optional: disable filesystem tools for a pure chat agent |
| 41 | + // replaceBuiltinTools: true, |
| 42 | + |
| 43 | + // Optional: limit turns and temperature |
| 44 | + constraints: { |
| 45 | + temperature: 0.7, |
| 46 | + maxTokens: 2000, |
| 47 | + }, |
| 48 | + maxTurns: 5, |
| 49 | + }); |
| 50 | + |
| 51 | + // Stream messages as they arrive |
| 52 | + for await (const msg of result) { |
| 53 | + switch (msg.type) { |
| 54 | + case "system": |
| 55 | + console.log(`[${msg.subtype}] ${msg.content}`); |
| 56 | + break; |
| 57 | + |
| 58 | + case "delta": |
| 59 | + // Real-time text streaming |
| 60 | + process.stdout.write(msg.content); |
| 61 | + break; |
| 62 | + |
| 63 | + case "assistant": |
| 64 | + // Final complete message |
| 65 | + console.log(`\n\nAgent: ${msg.content}`); |
| 66 | + if (msg.usage) { |
| 67 | + console.log(` Tokens: ${msg.usage.inputTokens} in / ${msg.usage.outputTokens} out`); |
| 68 | + } |
| 69 | + break; |
| 70 | + |
| 71 | + case "tool_use": |
| 72 | + console.log(`\n[tool] ${msg.toolName}(${JSON.stringify(msg.args).slice(0, 100)})`); |
| 73 | + break; |
| 74 | + |
| 75 | + case "tool_result": |
| 76 | + console.log(`[result] ${msg.toolName}: ${msg.content.slice(0, 200)}`); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Print cost summary |
| 82 | + const costs = result.costs(); |
| 83 | + console.log("\n--- Session Summary ---"); |
| 84 | + console.log(`Total requests: ${costs.totalRequests}`); |
| 85 | + console.log(`Total tokens: ${costs.totalInputTokens} in / ${costs.totalOutputTokens} out`); |
| 86 | + console.log(`Total cost: $${costs.totalCostUsd.toFixed(4)}`); |
| 87 | +} |
| 88 | + |
| 89 | +main().catch(console.error); |
0 commit comments