Implement extensions/vision/src/tool.ts (the vision ToolDefinition) #258
Labels
No labels
bug
chore
documentation
enhancement
feature
ready
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
david/pi-extensions-and-skills#258
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Create
extensions/vision/src/tool.ts: thevisiontool definition — its TypeBox parameter schema, name/label/description, prompt metadata, and theexecute()that validates images, calls the client, maps usage, and handles the response edge cases.Background
Depends on: #253, #255, #256, #257
This is the module the LLM actually sees. It owns three things that the lower layers deliberately do not:
prompt,images, optionaldetail, optionalthinking— plus the prompt metadata (description,promptSnippet,promptGuidelines) that tells the model when to reach for this tool.promptGuidelinesbullets are appended flat to pi's guidelines with no tool-name prefix, so each bullet must namevisionexplicitly (pinned pi docs).thinkingvalue (per-call param, elseconfig.defaultThinking), validate images againstctx.cwd, build the body, call the client, map usage, and shapedetails.contentempty) must not silently return an empty string to the LLM.Every failure must be thrown as a
ToolError; a rawErrorfrom a lower layer still needs to be wrapped by a finalcatchin categoryunexpected. pi marks a tool failed only whenexecute()throws.StringEnumis deliberately not used: this repo's precedent isType.Union([Type.Literal(...)])(seeextensions/forgejo/src/index.ts), and the plan pins that choice.Documentation Required
A separate process downloads these into the listed folders before this issue is implemented. Check the folder for the actual reference material before starting.
docs/reference/pi-coding-agent/node_modules/@earendil-works/pi-coding-agent/docs/extensions.md— theToolDefinitioncontract:name/label/description/parameters/execute(toolCallId, params, signal, onUpdate, ctx),promptSnippet,promptGuidelines, the flat-guidelines rule ("Each guideline must name the tool it refers to"), the return shape (content,details,usage), and "Signaling errors" (throw to fail).usagereturned fromexecuteis persisted on the tool result.docs/reference/typebox/Type.Object,Type.String,Type.Array,Type.Optional,Type.Union,Type.Literal, andType.Staticfor the inferred params type. This is thetypeboxv1 package (sinclairzx81) already inpackage.json; import from"typebox"like the other extensions do.docs/reference/deepseek-api/choices[0].message.content,reasoning_content,finish_reason, and theusagefields.Implementation Details
Public surface:
Parameters (
Type.Object, repo precedentType.Union([Type.Literal(...)])):Definition metadata:
name: "vision",label: "Vision".description: names the tool, states it sends local images todeepseek-flashand returns text, and says to use it when the current model cannot read an image.promptSnippet: "Read or analyze local image files with DeepSeek vision".promptGuidelines: an explicit bullet beginning "Use vision when …" that names the tool (guidelines are un-prefixed).execute(toolCallId, params, signal, onUpdate, ctx):thinking=params.thinking ?? config.defaultThinking;await loadAndValidateImages(params.images, ctx.cwd);buildRequestBody({ prompt, images, detail: params.detail, thinking }, config);await client.complete(body, signal)(pi may passsignal: undefined; the client still applies the deadline);mapUsage(response.usage);{ content: [{ type: "text", text }], details, usage }.Response handling (order matters):
message.content⇒ return it.contentwithfinish_reason === "length"⇒ToolError(response) whose message mentionsVISION_MAX_TOKENSand that reasoning tokens count toward the cap when thinking is on.contentotherwise ⇒ToolError(response) mentioning that the model may have returned only reasoning and the call can be retried withthinking: true.reasoning_contentis discarded (never returned to the LLM).executebody in atry/catch: re-throw aToolErrorunchanged, wrap anything else innew ToolError(..., "unexpected"). No raw stack trace reaches the model.detailsis populated for every successful call:model, effectivethinking,detail(only when supplied), the per-image metadata (path/mime/width/height/bytes, no data URL),durationMs(measure around the client call or the whole execute — pick one and document it), andfinishReason.Test file
extensions/vision/src/tool.test.ts(TDD, fakeVisionClient— never the network):vision.details+ mappedusagethrough a fake client;details.thinkingreflects the param and, when the param is omitted,config.defaultThinking.ToolErrorwithcategory === "image"and the client is never called.ToolError(response).finish_reason: "length"with empty content ⇒ToolErrormentioningVISION_MAX_TOKENS.ToolError(aborted) without leaking a raw error.ToolError(unexpected).Acceptance Criteria
vision/Visionwith a description,promptSnippet, and apromptGuidelinesbullet that names the tool.prompt, a non-emptyimagesarray, and optionaldetail/thinkingwith the documented descriptions.executeresolvesthinkingfrom the param withconfig.defaultThinkingas the fallback and passes it through to the request body.details(model, thinking, detail, per-image metadata, durationMs, finishReason) and a mappedusage.finish_reason: "length"raises aToolErrornamingVISION_MAX_TOKENS; empty content otherwise raises aresponseToolError.ToolError, including theunexpectedwrapper;reasoning_contentnever reaches the LLM.extensions/vision/src/tool.test.tspasses withnode --test extensions/vision/src/tool.test.tsand uses only a fake client.Test Plan
Expected: all tests pass with no network access and no real image files required beyond temp fixtures.
Manual shape inspection (no network — uses a fake client):
Expected:
vision Vision [ 'prompt', 'images', 'detail', 'thinking' ].pi-loop opened and merged a pull request for this issue: #272