~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs-cocore

0
0
main
81 lines · 5.9 KB Raw History
import assert from "node:assert/strict";
import { test } from "node:test";

import { abiTest, loadModule } from "../../test/check.mjs";

const inference = await loadModule("cocore-inference");
abiTest(test, inference);

const API = "https://cocore.dev/api/v1";
// The host's grant: a service-auth token minted per request with the run's session (atlibs http serviceAuth).
const HTTP = { "https://cocore.dev": { serviceAuth: { aud: "did:web:cocore.dev", lxm: "dev.cocore.inference.dispatch" } } };
const COCORE = { provider: "did:plc:prov", receipt: "at://did:plc:prov/dev.cocore.compute.receipt/3r" };

// The session answers getServiceAuth (held to the module's session scope by check.mjs); fetch is cocore.dev.
function host(answer) {
	const sent = [];
	const minted = [];
	const respond = ({ nsid, params }) => (minted.push([nsid, params]), { ok: true, output: { token: "svc-token" } });
	const fetch = async (url, init) => (sent.push({ url, headers: init.headers, body: init.body && JSON.parse(init.body) }), answer(url, init));
	return { sent, minted, options: { respond, fetch, http: HTTP } };
}

test("chat: one chat completion at cocore.dev/api/v1, authorized by the host's service-auth token", async () => {
	const h = host(() => Response.json({ model: "stub", choices: [{ index: 0, message: { role: "assistant", content: "Hello!" }, finish_reason: "stop" }], usage: { prompt_tokens: 3, completion_tokens: 2 }, x_cocore: COCORE }));
	const ran = await inference.handle("chat", { prompt: "Hi", system: "be brief", maxTokens: "16", country: "US" }, h.options);
	assert.equal(ran.error, undefined, ran.error);
	assert.deepEqual(ran.result, { text: "Hello!", finishReason: "stop", toolCalls: [], model: "stub", usage: { prompt_tokens: 3, completion_tokens: 2 }, cocore: COCORE });
	assert.deepEqual(h.minted, [["com.atproto.server.getServiceAuth", { aud: "did:web:cocore.dev", lxm: "dev.cocore.inference.dispatch" }]]);
	assert.equal(h.sent[0].url, `${API}/chat/completions`);
	assert.equal(h.sent[0].headers.authorization, "Bearer svc-token");
	assert.deepEqual(h.sent[0].body, { model: "stub", messages: [{ role: "system", content: "be brief" }, { role: "user", content: "Hi" }], max_tokens: 16, stream: false, country: "US" });
});

test("chat: a streamed answer is collected (text, tool calls, usage, x_cocore); routes", async () => {
	const frames = [
		{ model: "stub", choices: [{ index: 0, delta: { role: "assistant", content: "Hel" } }] },
		{ choices: [{ index: 0, delta: { content: "lo", tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "get_weather", arguments: '{"city":' } }] } }] },
		{ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: '"Amsterdam"}' } }] }, finish_reason: "tool_calls" }] },
		{ choices: [], usage: { prompt_tokens: 4, completion_tokens: 6 }, x_cocore: COCORE },
	];
	const sse = frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n";
	const h = host(() => new Response(sse, { headers: { "content-type": "text/event-stream" } }));
	const ran = await inference.handle("chat", { prompt: "Hi", stream: "true", route: "verified", minTrust: "confidential" }, h.options);
	assert.equal(ran.error, undefined, ran.error);
	assert.deepEqual(ran.result, {
		text: "Hello",
		finishReason: "tool_calls",
		toolCalls: [{ id: "c1", type: "function", function: { name: "get_weather", arguments: '{"city":"Amsterdam"}' } }],
		model: "stub",
		usage: { prompt_tokens: 4, completion_tokens: 6 },
		cocore: COCORE,
	});
	assert.equal(h.sent[0].url, `${API}/verified/chat/completions`);
	assert.equal(h.sent[0].headers.accept, "text/event-stream");
	assert.equal(h.sent[0].body.min_trust, "confidential");
});

test("onboarding_required says to connect co/core once; other errors keep co/core's code and message", async () => {
	const error = (status, body) => host(() => Response.json(body, { status }));
	const onboarding = await inference.handle("chat", { prompt: "Hi" }, error(401, { error: { type: "authentication_error", code: "onboarding_required", message: "..." } }).options);
	assert.equal(onboarding.error, "co/core: this account has not connected co/core yet: sign in at https://cocore.dev once, then try again (onboarding_required)");
	const bad = await inference.handle("chat", { prompt: "Hi" }, error(401, { error: { message: "Invalid API key", type: "authentication_error", code: "invalid_api_key", param: null } }).options);
	assert.equal(bad.error, "co/core HTTP 401 invalid_api_key: Invalid API key");
	const tools = await inference.handle("chat", { prompt: "Hi" }, error(400, { error: { type: "invalid_request_error", code: "tool_calls_not_supported", message: "no canary" } }).options);
	assert.equal(tools.error, "co/core HTTP 400 tool_calls_not_supported: no canary");
	// signed out: the host cannot mint the token
	const signedOut = await inference.handle("chat", { prompt: "Hi" }, { ...host(() => assert.fail("never sent")).options, respond: () => ({ ok: false, error: "no session" }) });
	assert.match(signedOut.error, /needs a service-auth token \(aud did:web:cocore\.dev, lxm dev\.cocore\.inference\.dispatch\) from the run's session: no session/);
});

test("chat checks its input; models reads the public directory", async () => {
	const start = (fields) => inference.run("chat", { did: "did:plc:x", ...fields, state: null, event: { type: "start" } });
	assert.match(start({}).error, /prompt \(or messages with a user message\) is required/);
	assert.match(start({ prompt: "p", maxTokens: "0" }).error, /at least 1/);
	assert.match(start({ prompt: "p", route: "nope" }).error, /open, private, verified or probono/);
	assert.match(start({ messages: "[" }).error, /messages:/);
	const h = host((url) => Response.json({ object: "list", data: [{ id: "stub", object: "model" }], url }));
	const ran = await inference.handle("models", { view: "summary" }, h.options);
	assert.deepEqual(ran.result.data, [{ id: "stub", object: "model" }]);
	assert.equal(h.sent[0].url, `${API}/models?view=summary`);
});