mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-13 04:59:51 +08:00
fix(chat): use the AI SDK's own mechanisms for quota, retries, stops and notices
Server
- Record quota tokens in onStepFinish and await the write, so a step that
fails or is stopped later still counts; drop the onFinish/onAbort copies
- validate-diagram and the connection test end their model call with the
request (req.signal) and use the SDK's timeout option; validate-diagram
reports to Langfuse (functionId "validate-diagram")
- The connection test no longer counts an invalid tool call as support
- Validate incoming messages with safeValidateUIMessages (400 otherwise),
drop client system messages explicitly, prune empty model messages with
pruneMessages
- Chat tools move to lib/chat-tools.ts with an output schema for the
client-side tools
Client
- Every automatic retry sends the canvas as it is then
(prepareSendMessagesRequest): a drawing is committed before its
screenshot check can fail, so the model must go on from it
- A notice when the output budget ran out before any tool call
(finishReason "length")
- Error notices are assistant messages with a data-notice part instead of
role "system" messages (lib/chat-message.ts: ChatUIMessage with metadata,
data part and InferUITools types)
- regenerate, edit and retry use sendMessage({ messageId }); isResending
and flushSync go
- isToolUIPart/getToolName instead of slicing "tool-"; ToolPartLike derives
from the SDK's part types; addToolOutput typed with ChatAddToolOutputFunction
Tests: step failure after a finished step, abort of the screenshot check,
invalid tool call in the connection test, malformed messages.
This commit is contained in:
@@ -141,6 +141,24 @@ describe("POST /api/validate-model", () => {
|
||||
expect(data.valid).toBe(true)
|
||||
expect(data.warning).toMatch(/without calling a tool/)
|
||||
})
|
||||
|
||||
it("warns when the model calls a tool that does not exist", async () => {
|
||||
// The SDK passes the call on, marked invalid
|
||||
streamReply({
|
||||
role: "assistant",
|
||||
tool_calls: [
|
||||
{
|
||||
index: 0,
|
||||
id: "call_1",
|
||||
type: "function",
|
||||
function: { name: "pong", arguments: "{}" },
|
||||
},
|
||||
],
|
||||
})
|
||||
const data = await testGlm()
|
||||
expect(data.valid).toBe(true)
|
||||
expect(data.warning).toMatch(/without calling a tool/)
|
||||
})
|
||||
})
|
||||
|
||||
describe("chat requests to a client base URL", () => {
|
||||
|
||||
Reference in New Issue
Block a user