test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Test script for GitLab Duo extension
  3. * Run: npx tsx test.ts [model-id] [--thinking]
  4. *
  5. * Examples:
  6. * npx tsx test.ts # Test default (claude-sonnet-4-5-20250929)
  7. * npx tsx test.ts gpt-5-codex # Test GPT-5 Codex
  8. * npx tsx test.ts claude-sonnet-4-5-20250929 --thinking
  9. */
  10. import { type Api, type Context, type Model, registerApiProvider, streamSimple } from "@earendil-works/pi-ai";
  11. import { readFileSync } from "fs";
  12. import { getAgentDir } from "packages/coding-agent/src/config.js";
  13. import { join } from "path";
  14. import { MODELS, streamGitLabDuo } from "./index.ts";
  15. const MODEL_MAP = new Map(MODELS.map((m) => [m.id, m]));
  16. async function main() {
  17. const modelId = process.argv[2] || "claude-sonnet-4-5-20250929";
  18. const useThinking = process.argv.includes("--thinking");
  19. const cfg = MODEL_MAP.get(modelId);
  20. if (!cfg) {
  21. console.error(`Unknown model: ${modelId}`);
  22. console.error("Available:", MODELS.map((m) => m.id).join(", "));
  23. process.exit(1);
  24. }
  25. // Read auth
  26. const authPath = join(getAgentDir(), "extensions", "auth.json");
  27. const authData = JSON.parse(readFileSync(authPath, "utf-8"));
  28. const gitlabCred = authData["gitlab-duo"];
  29. if (!gitlabCred?.access) {
  30. console.error("No gitlab-duo credentials. Run /login gitlab-duo first.");
  31. process.exit(1);
  32. }
  33. // Register provider
  34. registerApiProvider({
  35. api: "gitlab-duo-api" as Api,
  36. stream: streamGitLabDuo,
  37. streamSimple: streamGitLabDuo,
  38. });
  39. // Create model
  40. const model: Model<Api> = {
  41. id: cfg.id,
  42. name: cfg.name,
  43. api: "gitlab-duo-api" as Api,
  44. provider: "gitlab-duo",
  45. baseUrl: cfg.baseUrl,
  46. reasoning: cfg.reasoning,
  47. input: cfg.input,
  48. cost: cfg.cost,
  49. contextWindow: cfg.contextWindow,
  50. maxTokens: cfg.maxTokens,
  51. };
  52. const context: Context = {
  53. messages: [{ role: "user", content: "Say hello in exactly 3 words.", timestamp: Date.now() }],
  54. };
  55. console.log(`Model: ${model.id}, Backend: ${cfg.backend}, Thinking: ${useThinking}`);
  56. const stream = streamSimple(model, context, {
  57. apiKey: gitlabCred.access,
  58. maxTokens: 100,
  59. reasoning: useThinking ? "low" : undefined,
  60. });
  61. for await (const event of stream) {
  62. if (event.type === "thinking_start") console.log("[Thinking]");
  63. else if (event.type === "thinking_delta") process.stdout.write(event.delta);
  64. else if (event.type === "thinking_end") console.log("\n[/Thinking]\n");
  65. else if (event.type === "text_delta") process.stdout.write(event.delta);
  66. else if (event.type === "error") console.error("\nError:", event.error.errorMessage);
  67. else if (event.type === "done") console.log("\n\nDone!", event.reason, event.message.usage);
  68. }
  69. }
  70. main().catch(console.error);