Luigit
repositories / will

will

owned by admin

scripts/zai-canary.ts

Raw
/**
 * Scheduled ZAI canary (spec AC23): a small, non-deploying contract check for
 * text and media compatibility with bounded cost. Run by Luci on a schedule
 * once ZAI secrets are configured; exits non-zero on drift.
 */
import { loadAgentConfig } from '../src/agent/config.ts'
import { ZaiRestClient } from '../src/agent/zai/client.ts'

const config = loadAgentConfig()
if (!config.zai.apiKey) {
  console.error('canary: ZAI_API_KEY missing')
  process.exit(2)
}
const client = new ZaiRestClient(config.zai.apiKey, config.zai.baseUrl, {
  retries: 1,
  timeoutMs: 30_000,
})

let failures = 0
const check = async (name: string, fn: () => Promise<unknown>) => {
  try {
    await fn()
    console.log(`canary: ${name} ok`)
  } catch (err) {
    failures++
    console.error(`canary: ${name} FAILED: ${err instanceof Error ? err.message : String(err)}`)
  }
}

// 1x1 transparent PNG as the media payload (bounded, deterministic).
const png = Buffer.from(
  'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
  'base64',
)

await check('text chat completion', () =>
  client.chatCompletion({
    model: config.zai.analysisModel,
    messages: [{ role: 'user', content: 'Reply with the single word: ok' }],
  }),
)
await check('vision image input', () =>
  client.chatCompletion({
    model: config.zai.analysisModel,
    messages: [
      {
        role: 'user',
        content: [
          { type: 'text', text: 'Describe this image in one word.' },
          {
            type: 'image_url',
            image_url: { url: `data:image/png;base64,${png.toString('base64')}` },
          },
        ],
      },
    ],
  }),
)
await check('layout parsing endpoint', () =>
  client.layoutParsing(new Uint8Array(png), { fileName: 'canary.png' }),
)

process.exit(failures > 0 ? 1 : 0)