test-agent-direct.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env npx tsx
  2. /**
  3. * Direct test: Ask agent to run ls and check if it actually executes
  4. */
  5. import { createOpencodeClient } from '@opencode-ai/sdk';
  6. const baseUrl = process.argv[2] || 'http://127.0.0.1:3000';
  7. const agentToUse = process.argv[3] || 'opencoder';
  8. async function test() {
  9. console.log(`Connecting to ${baseUrl}...`);
  10. console.log(`Using agent: ${agentToUse}`);
  11. const client = createOpencodeClient({ baseUrl });
  12. // Create a new session
  13. console.log('\n1. Creating session...');
  14. const sessionResp = await client.session.create({
  15. body: { title: 'Direct Tool Test' }
  16. });
  17. const sessionId = sessionResp.data?.id;
  18. console.log(` Session: ${sessionId}`);
  19. if (!sessionId) {
  20. console.log('Failed to create session');
  21. return;
  22. }
  23. // Send a simple prompt using the correct API
  24. console.log('\n2. Sending prompt: "Run ls in the current directory"');
  25. console.log(' (prompt() should block until complete)');
  26. const startTime = Date.now();
  27. try {
  28. const response = await client.session.prompt({
  29. path: { id: sessionId },
  30. body: {
  31. parts: [{ type: 'text', text: 'Run ls in the current directory' }],
  32. agent: agentToUse,
  33. model: {
  34. providerID: 'anthropic',
  35. modelID: 'claude-sonnet-4-5'
  36. }
  37. }
  38. });
  39. const elapsed = Date.now() - startTime;
  40. console.log(` Prompt completed in ${elapsed}ms`);
  41. console.log(` Response has data: ${!!response.data}`);
  42. // Check response directly
  43. if (response.data) {
  44. console.log(` Response info role: ${response.data.info?.role}`);
  45. console.log(` Response parts: ${response.data.parts?.length || 0}`);
  46. for (const part of response.data.parts || []) {
  47. console.log(` - Part type: ${part.type}`);
  48. if (part.type === 'tool') {
  49. console.log(` Tool: ${part.tool}, Status: ${part.state?.status}`);
  50. }
  51. }
  52. }
  53. } catch (error) {
  54. const elapsed = Date.now() - startTime;
  55. console.log(` Error after ${elapsed}ms:`, (error as Error).message);
  56. }
  57. // No artificial wait - prompt() should have blocked until complete
  58. console.log('\n3. Checking messages...');
  59. // Get messages
  60. console.log('\n4. Checking response...');
  61. const messagesResp = await client.session.messages({ path: { id: sessionId } });
  62. const messages = messagesResp.data || [];
  63. console.log(` Total messages: ${messages.length}`);
  64. // Check for tool usage
  65. let toolCount = 0;
  66. let bashOutput = '';
  67. for (const msg of messages) {
  68. if (msg.info?.role === 'assistant') {
  69. for (const part of msg.parts || []) {
  70. if (part.type === 'tool') {
  71. toolCount++;
  72. console.log(`\n TOOL FOUND: ${part.tool}`);
  73. console.log(` Status: ${part.state?.status || part.status}`);
  74. if (part.tool === 'bash') {
  75. console.log(` Command: ${part.state?.input?.command || part.input?.command}`);
  76. bashOutput = part.state?.output || part.output || '';
  77. if (bashOutput) {
  78. console.log(` Output preview: ${String(bashOutput).substring(0, 500)}`);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. console.log('\n=== RESULT ===');
  86. if (toolCount > 0) {
  87. console.log(`✅ Agent used ${toolCount} tool(s)`);
  88. if (bashOutput) {
  89. console.log('✅ Got bash output - tools are working!');
  90. }
  91. } else {
  92. console.log('❌ Agent did NOT use any tools');
  93. console.log('\nAgent response (text only):');
  94. for (const msg of messages) {
  95. if (msg.info?.role === 'assistant') {
  96. for (const part of msg.parts || []) {
  97. if (part.type === 'text') {
  98. console.log(part.text?.substring(0, 1000));
  99. }
  100. }
  101. }
  102. }
  103. }
  104. // Cleanup
  105. console.log('\n5. Cleaning up...');
  106. try {
  107. await client.session.delete({ path: { id: sessionId } });
  108. console.log(' Session deleted');
  109. } catch {
  110. console.log(' Could not delete session');
  111. }
  112. }
  113. test().catch(console.error);