Skip to content

Instantly share code, notes, and snippets.

@slashthinking
slashthinking / provider_tools.ts
Created December 15, 2025 04:50
使用ProviderTool和AI Gateway的例子
import 'dotenv/config';
import { streamText } from 'ai';
import { anthropicTools } from '@ai-sdk/anthropic/internal';
const result = await streamText({
model: 'anthropic/claude-sonnet-4.5',
tools: {
codeExecution: anthropicTools.codeExecution_20250522(),
},
messages: [{ role: 'user', content: 'Run: print(1+1)' }],
@slashthinking
slashthinking / code-execution_20250522.ts
Created December 15, 2025 04:48
使用ProviderToolSchema
// packages/anthropic/src/tool/code-execution_20250522.ts
import {
createProviderToolFactoryWithOutputSchema,
lazySchema,
zodSchema,
} from '@ai-sdk/provider-utils';
import { z } from 'zod/v4';
export const codeExecution_20250522OutputSchema = lazySchema(() =>
@slashthinking
slashthinking / tools.ts
Created December 15, 2025 04:45
AI SDK - AnthropicTools
// packages/anthropic/src/anthropic-tools.ts
export const anthropicTools = {
/**
* bash 工具使 Claude 能够在持久的 bash 会话中执行 shell 命令,
* 实现系统操作、脚本执行和命令行自动化。
*
* 支持图像结果。
*/
bash_20241022,
import { streamText, jsonSchema } from 'ai';
import 'dotenv/config';
const result = await streamText({
model: 'anthropic/claude-sonnet-4.5',
tools: {
// 创建订单工具 - 根据 orderType 有不同的必填字段
createOrder: {
description: '创建一个新订单,根据订单类型需要不同的信息',
inputSchema: jsonSchema<{
@slashthinking
slashthinking / firebase_snapshot_parent.js
Created June 20, 2015 09:42
Firebase: Get the parent of a snapshot.
function getParent(snapshot) {
// You can get the reference (A Firebase object) from a snapshot
// using .ref().
var ref = snapshot.ref();
// Now simply find the parent and return the name.
return ref.parent().name();
}
var testRef = new Firebase("https://example.firebaseIO-demo.com/foo/bar");
testRef.once("value", function(snapshot) {
@slashthinking
slashthinking / firebase_remove_pushed.js
Created June 20, 2015 09:42
Firebase: Removing an item you've pushed. This snippet shows how to remove an object that you just added using push().
function pushSomething(ref) {
// Let's push something. push() returns a reference that you can hold onto!
var justPushed = ref.push({test: "push"});
// We return a reference, but you can also return the name of the newly
// created object with .name().
return justPushed;
}
function removeItem(ref) {
// Now we can get back to that item we just pushed via .child().
@slashthinking
slashthinking / firebase_player_assignment.js
Created June 20, 2015 09:41
Firebase: Assigning players in a multiplayer game. This snippet assigns you a spot in a game if the game isn't full yet.
function go() {
var userId = prompt('Username?', 'Guest');
// Consider adding '/<unique id>' if you have multiple games.
var gameRef = new Firebase(GAME_LOCATION);
assignPlayerNumberAndPlayGame(userId, gameRef);
};
// The maximum number of players. If there are already
// NUM_PLAYERS assigned, users won't be able to join the game.
var NUM_PLAYERS = 4;
@slashthinking
slashthinking / firebase_create.js
Created June 20, 2015 09:40
Firebase: Creating data if it doesn't exist. This snippet creates a user only if it doesn't already exist.
function go() {
var userId = prompt('Username?', 'Guest');
var userData = { name: userId };
tryCreateUser(userId, userData);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userCreated(userId, success) {
if (!success) {
@slashthinking
slashthinking / jwtparser.js
Created June 20, 2015 09:39
Extracting claims from a JWT
// Helper function to extract claims from a JWT. Does *not* verify the
// validity of the token.
// credits: https://github.com/firebase/angularFire/blob/master/angularFire.js#L370
// polyfill window.atob() for IE8: https://github.com/davidchambers/Base64.js
// or really fast Base64 by Fred Palmer: https://code.google.com/p/javascriptbase64/
function deconstructJWT(token) {
var segments = token.split(".");
if (!segments instanceof Array || segments.length !== 3) {
throw new Error("Invalid JWT");
}
@slashthinking
slashthinking / limit_endat.js
Created June 20, 2015 09:38
获得列表第一个
// pros: does not fetch entire record set
// cons: grabs the last record which needs to be ignored
var first = true;
var ref = new Firebase(...);
ref.endAt().limit(1).on("child_added", function(snap) {
if( first ) {
first = false;
}
else {
console.log('new record', snap.val());