Napcar

JavaScript API

The navigator.napcar.ai native API — availability, sessions, streaming, structured output, and embeddings.

Detection

const isNapcar = Boolean(globalThis.navigator?.napcar?.ai);
// navigator.napcar.browser => { name: "Napcar", version: "0.1.0", localAI: true }

availability()

const availability = await navigator.napcar.ai.availability({
  task: "chat",
  modality: ["text"],
  localOnly: true,
});
// availability.status: "available" | "downloadable" | "downloading"
//   | "unavailable" | "permission-required"

requestSession()

Triggers a permission prompt on first use for the origin, then selects a model by task and ranking.

const session = await navigator.napcar.ai.requestSession({
  task: "summarization",
  localOnly: true,
  systemPrompt: "You summarize clearly and concisely.",
  temperature: 0.2,
  maxOutputTokens: 500,
});

generate() and generateStreaming()

const result = await session.generate({ input: "Summarize this article..." });
console.log(result.text, result.finishReason, result.usage);

for await (const chunk of session.generateStreaming({ input: "Write a haiku." })) {
  appendToUI(chunk);
}

Structured output

const result = await session.generate({
  input: invoiceText,
  responseFormat: {
    type: "json_schema",
    schema: {
      type: "object",
      properties: {
        vendor: { type: "string" },
        total: { type: "number" },
        dueDate: { type: "string" },
      },
      required: ["vendor", "total"],
    },
  },
});

If the model does not produce valid JSON, Napcar retries once with a repair prompt before returning a typed structured_output_failed error.

Embeddings

const embedding = await navigator.napcar.ai.embed({
  input: "Text to embed",
  modelHint: "embedding",
});
// { embedding: number[], embeddings: number[][], dimensions: number }

Errors

Generation rejects with a typed code:

try {
  await navigator.napcar.ai.requestSession({ localOnly: true });
} catch (e) {
  if (e.code === "model_download_required") {
    // prompt the user to open chrome://llm
  }
}

Codes: permission_denied, model_unavailable, model_download_required, backend_unavailable, quota_exceeded, context_too_large, unsupported_modality, unsupported_response_format, generation_failed, cancelled, cloud_blocked, local_required_but_unavailable.

Model identity

By default websites see a generic alias (napcar-default, napcar-fast, napcar-code, napcar-embedding, napcar-vision) rather than the exact model. A user can grant exact-model disclosure per site in chrome://llm.