|
1 | 1 | import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test" |
2 | 2 | import path from "path" |
3 | | -import type { ModelMessage } from "ai" |
| 3 | +import { tool, type ModelMessage } from "ai" |
| 4 | +import z from "zod" |
4 | 5 | import { LLM } from "../../src/session/llm" |
5 | 6 | import { Global } from "../../src/global" |
6 | 7 | import { Instance } from "../../src/project/instance" |
@@ -325,6 +326,95 @@ describe("session.llm.stream", () => { |
325 | 326 | }) |
326 | 327 | }) |
327 | 328 |
|
| 329 | + test("keeps tools enabled by prompt permissions", async () => { |
| 330 | + const server = state.server |
| 331 | + if (!server) { |
| 332 | + throw new Error("Server not initialized") |
| 333 | + } |
| 334 | + |
| 335 | + const providerID = "alibaba" |
| 336 | + const modelID = "qwen-plus" |
| 337 | + const fixture = await loadFixture(providerID, modelID) |
| 338 | + const model = fixture.model |
| 339 | + |
| 340 | + const request = waitRequest( |
| 341 | + "/chat/completions", |
| 342 | + new Response(createChatStream("Hello"), { |
| 343 | + status: 200, |
| 344 | + headers: { "Content-Type": "text/event-stream" }, |
| 345 | + }), |
| 346 | + ) |
| 347 | + |
| 348 | + await using tmp = await tmpdir({ |
| 349 | + init: async (dir) => { |
| 350 | + await Bun.write( |
| 351 | + path.join(dir, "opencode.json"), |
| 352 | + JSON.stringify({ |
| 353 | + $schema: "https://opencode.ai/config.json", |
| 354 | + enabled_providers: [providerID], |
| 355 | + provider: { |
| 356 | + [providerID]: { |
| 357 | + options: { |
| 358 | + apiKey: "test-key", |
| 359 | + baseURL: `${server.url.origin}/v1`, |
| 360 | + }, |
| 361 | + }, |
| 362 | + }, |
| 363 | + }), |
| 364 | + ) |
| 365 | + }, |
| 366 | + }) |
| 367 | + |
| 368 | + await Instance.provide({ |
| 369 | + directory: tmp.path, |
| 370 | + fn: async () => { |
| 371 | + const resolved = await Provider.getModel(providerID, model.id) |
| 372 | + const sessionID = "session-test-tools" |
| 373 | + const agent = { |
| 374 | + name: "test", |
| 375 | + mode: "primary", |
| 376 | + options: {}, |
| 377 | + permission: [{ permission: "question", pattern: "*", action: "deny" }], |
| 378 | + } satisfies Agent.Info |
| 379 | + |
| 380 | + const user = { |
| 381 | + id: "user-tools", |
| 382 | + sessionID, |
| 383 | + role: "user", |
| 384 | + time: { created: Date.now() }, |
| 385 | + agent: agent.name, |
| 386 | + model: { providerID, modelID: resolved.id }, |
| 387 | + tools: { question: true }, |
| 388 | + } satisfies MessageV2.User |
| 389 | + |
| 390 | + const stream = await LLM.stream({ |
| 391 | + user, |
| 392 | + sessionID, |
| 393 | + model: resolved, |
| 394 | + agent, |
| 395 | + permission: [{ permission: "question", pattern: "*", action: "allow" }], |
| 396 | + system: ["You are a helpful assistant."], |
| 397 | + abort: new AbortController().signal, |
| 398 | + messages: [{ role: "user", content: "Hello" }], |
| 399 | + tools: { |
| 400 | + question: tool({ |
| 401 | + description: "Ask a question", |
| 402 | + inputSchema: z.object({}), |
| 403 | + execute: async () => ({ output: "" }), |
| 404 | + }), |
| 405 | + }, |
| 406 | + }) |
| 407 | + |
| 408 | + for await (const _ of stream.fullStream) { |
| 409 | + } |
| 410 | + |
| 411 | + const capture = await request |
| 412 | + const tools = capture.body.tools as Array<{ function?: { name?: string } }> | undefined |
| 413 | + expect(tools?.some((item) => item.function?.name === "question")).toBe(true) |
| 414 | + }, |
| 415 | + }) |
| 416 | + }) |
| 417 | + |
328 | 418 | test("sends responses API payload for OpenAI models", async () => { |
329 | 419 | const server = state.server |
330 | 420 | if (!server) { |
|
0 commit comments